|
| 1 | +//===-- NetworkSymbolManager.h --------------------------------*- C++ -*-===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#ifndef LLDB_SYMBOL_NETWORKSYMBOLMANAGER_H |
| 10 | +#define LLDB_SYMBOL_NETWORKSYMBOLMANAGER_H |
| 11 | + |
| 12 | +#include "lldb/Utility/Status.h" |
| 13 | +#include "lldb/lldb-forward.h" |
| 14 | +#include "llvm/ADT/StringMap.h" |
| 15 | +#include "llvm/ADT/StringRef.h" |
| 16 | + |
| 17 | +#include <chrono> |
| 18 | +#include <mutex> |
| 19 | + |
| 20 | +namespace lldb { |
| 21 | +class SBDebugger; |
| 22 | +} |
| 23 | + |
| 24 | +namespace lldb_private { |
| 25 | + |
| 26 | +/// NetworkSymbolManager provides centralized management of network-based |
| 27 | +/// symbol loading optimizations, including server availability caching, |
| 28 | +/// adaptive timeout management, and intelligent fallback strategies. |
| 29 | +/// |
| 30 | +/// This class implements the architectural separation between DAP protocol |
| 31 | +/// handling and network symbol optimization logic, addressing reviewer |
| 32 | +/// concerns about layer violations in PR #150777. |
| 33 | +class NetworkSymbolManager { |
| 34 | +public: |
| 35 | + /// Configuration for network symbol optimization |
| 36 | + struct Configuration { |
| 37 | + /// Enable intelligent server availability caching |
| 38 | + bool enable_server_caching = true; |
| 39 | + |
| 40 | + /// Default timeout for debuginfod requests (milliseconds) |
| 41 | + uint32_t debuginfod_timeout_ms = 2000; |
| 42 | + |
| 43 | + /// Default timeout for symbol server requests (milliseconds) |
| 44 | + uint32_t symbol_server_timeout_ms = 2000; |
| 45 | + |
| 46 | + /// Completely disable network symbol loading |
| 47 | + bool disable_network_symbols = false; |
| 48 | + |
| 49 | + /// Enable adaptive timeout adjustment based on server response history |
| 50 | + bool enable_adaptive_timeouts = true; |
| 51 | + |
| 52 | + /// Cache TTL for server availability information (minutes) |
| 53 | + uint32_t cache_ttl_minutes = 5; |
| 54 | + }; |
| 55 | + |
| 56 | + /// Server availability information with response time tracking |
| 57 | + struct ServerAvailability { |
| 58 | + bool is_responsive = false; |
| 59 | + std::chrono::steady_clock::time_point last_checked; |
| 60 | + std::chrono::milliseconds average_response_time{0}; |
| 61 | + uint32_t success_count = 0; |
| 62 | + uint32_t failure_count = 0; |
| 63 | + uint32_t consecutive_failures = 0; |
| 64 | + std::chrono::steady_clock::time_point first_failure_time; |
| 65 | + |
| 66 | + ServerAvailability() : last_checked(std::chrono::steady_clock::now()) {} |
| 67 | + |
| 68 | + /// Calculate reliability score (0.0 = unreliable, 1.0 = highly reliable) |
| 69 | + double GetReliabilityScore() const; |
| 70 | + |
| 71 | + /// Check if cached information is still valid |
| 72 | + bool IsValid(std::chrono::minutes ttl) const; |
| 73 | + |
| 74 | + /// Check if server should be temporarily blacklisted due to consecutive failures |
| 75 | + bool IsTemporarilyBlacklisted() const; |
| 76 | + |
| 77 | + /// Get recommended backoff time before next attempt |
| 78 | + std::chrono::minutes GetBackoffTime() const; |
| 79 | + }; |
| 80 | + |
| 81 | + NetworkSymbolManager(); |
| 82 | + ~NetworkSymbolManager(); |
| 83 | + |
| 84 | + /// Configure network symbol optimization settings. |
| 85 | + /// This method respects existing user settings and provides opt-in behavior. |
| 86 | + Status Configure(const Configuration &config, |
| 87 | + bool respect_user_settings = true); |
| 88 | + |
| 89 | + /// Get current configuration |
| 90 | + const Configuration &GetConfiguration() const { return config_; } |
| 91 | + |
| 92 | + /// Test server availability with intelligent caching. |
| 93 | + /// Returns cached result if available and valid, otherwise performs test. |
| 94 | + bool IsServerResponsive( |
| 95 | + llvm::StringRef server_url, |
| 96 | + std::chrono::milliseconds test_timeout = std::chrono::milliseconds(1000)); |
| 97 | + |
| 98 | + /// Get adaptive timeout for a specific server based on response history. |
| 99 | + std::chrono::milliseconds GetAdaptiveTimeout( |
| 100 | + llvm::StringRef server_url) const; |
| 101 | + |
| 102 | + /// Record server response for adaptive timeout calculation. |
| 103 | + void RecordServerResponse(llvm::StringRef server_url, |
| 104 | + std::chrono::milliseconds response_time, |
| 105 | + bool success); |
| 106 | + |
| 107 | + /// Clear all cached server availability information |
| 108 | + void ClearServerCache(); |
| 109 | + |
| 110 | + /// Apply network symbol optimizations to LLDB settings. |
| 111 | + /// This method queries existing settings before making changes. |
| 112 | + Status ApplyOptimizations(Debugger &debugger); |
| 113 | + |
| 114 | + /// Apply optimizations using SBDebugger interface (for DAP layer) |
| 115 | + Status ApplyOptimizations(lldb::SBDebugger &debugger); |
| 116 | + |
| 117 | + /// Restore original LLDB settings (for cleanup or user preference changes). |
| 118 | + Status RestoreOriginalSettings(Debugger &debugger); |
| 119 | + |
| 120 | + /// Restore settings using SBDebugger interface (for DAP layer) |
| 121 | + Status RestoreOriginalSettings(lldb::SBDebugger &debugger); |
| 122 | + |
| 123 | + /// Check if network symbol loading should be disabled based on configuration |
| 124 | + bool ShouldDisableNetworkSymbols() const; |
| 125 | + |
| 126 | + /// Get recommended timeout for debuginfod based on server availability |
| 127 | + std::chrono::milliseconds GetRecommendedDebuginfodTimeout() const; |
| 128 | + |
| 129 | + /// Get recommended timeout for symbol servers based on server availability |
| 130 | + std::chrono::milliseconds GetRecommendedSymbolServerTimeout() const; |
| 131 | + |
| 132 | + /// Attempt symbol resolution with intelligent fallback strategies. |
| 133 | + /// Returns true if symbols should be attempted from network, false if should |
| 134 | + /// skip. |
| 135 | + bool ShouldAttemptNetworkSymbolResolution( |
| 136 | + llvm::StringRef server_url) const; |
| 137 | + |
| 138 | + /// Get list of responsive servers for symbol resolution. |
| 139 | + std::vector<std::string> GetResponsiveServers( |
| 140 | + llvm::ArrayRef<llvm::StringRef> server_urls) const; |
| 141 | + |
| 142 | + /// Validate configuration parameters |
| 143 | + static Status ValidateConfiguration(const Configuration &config); |
| 144 | + |
| 145 | +private: |
| 146 | + /// Current configuration |
| 147 | + Configuration config_; |
| 148 | + |
| 149 | + /// Server availability cache with thread safety |
| 150 | + mutable std::mutex server_cache_mutex_; |
| 151 | + llvm::StringMap<ServerAvailability> server_availability_cache_; |
| 152 | + |
| 153 | + /// Original LLDB settings for restoration |
| 154 | + mutable std::mutex settings_mutex_; |
| 155 | + llvm::StringMap<std::string> original_settings_; |
| 156 | + bool settings_applied_ = false; |
| 157 | + |
| 158 | + /// Test server connectivity (implementation detail) |
| 159 | + bool TestServerConnectivity(llvm::StringRef server_url, |
| 160 | + std::chrono::milliseconds timeout); |
| 161 | + |
| 162 | + /// Query existing LLDB setting value |
| 163 | + Status QueryExistingSetting(Debugger &debugger, |
| 164 | + llvm::StringRef setting_name, |
| 165 | + std::string &value); |
| 166 | + |
| 167 | + /// Apply single LLDB setting with backup |
| 168 | + Status ApplySetting(Debugger &debugger, |
| 169 | + llvm::StringRef setting_name, |
| 170 | + llvm::StringRef value); |
| 171 | +}; |
| 172 | + |
| 173 | +} // namespace lldb_private |
| 174 | + |
| 175 | +#endif // LLDB_SYMBOL_NETWORKSYMBOLMANAGER_H |
0 commit comments