20#include "llvm/ADT/StringExtras.h"
21#include "llvm/HTTP/HTTPClient.h"
22#include "llvm/HTTP/StreamedHTTPResponseHandler.h"
23#include "llvm/Support/Caching.h"
24#include "llvm/Support/Endian.h"
25#include "llvm/Support/FileSystem.h"
26#include "llvm/Support/FormatVariadic.h"
27#include "llvm/Support/Path.h"
28#include "llvm/Support/raw_ostream.h"
37#define LLDB_PROPERTIES_symbollocatorsymstore
38#include "SymbolLocatorSymStoreProperties.inc"
41#define LLDB_PROPERTIES_symbollocatorsymstore
42#include "SymbolLocatorSymStorePropertiesEnum.inc"
47 static llvm::StringRef GetSettingName() {
52 m_collection_sp = std::make_shared<OptionValueProperties>(GetSettingName());
53 m_collection_sp->Initialize(g_symbollocatorsymstore_properties_def);
56 Args GetURLs()
const {
58 m_collection_sp->GetPropertyAtIndexAsArgs(ePropertySymStoreURLs, urls);
62 std::string GetCachePath()
const {
63 OptionValueString *s =
64 m_collection_sp->GetPropertyAtIndexAsOptionValueString(
71 std::optional<std::string> GetTLSCertFingerprint()
const {
72 OptionValueString *s =
73 m_collection_sp->GetPropertyAtIndexAsOptionValueString(
74 ePropertyTLSCertFingerprint);
80 if (val.size() != 64 || !llvm::all_of(val, llvm::isHexDigit)) {
82 "plugin.symbol-locator.symstore.tls-cert-fingerprint: expected a "
83 "64-character hex string (SHA-256), but got '{0}', ignoring",
94 static PluginProperties g_settings;
105 llvm::HTTPClient::initialize();
108 if (std::error_code ec = llvm::sys::fs::create_directories(default_cache)) {
110 "default SymStore cache directory '{0}' is not accessible: {1}",
111 default_cache, ec.message()));
117 debugger, PluginProperties::GetSettingName())) {
118 constexpr bool is_global_setting =
true;
121 "Properties for the SymStore Symbol Locator plug-in.",
128 llvm::HTTPClient::cleanup();
132 return "Symbol locator for PDB in SymStore";
143 entry.
source = source.str();
144 entry.
cache = std::nullopt;
149 llvm::StringRef cache) {
151 entry.
source = source.str();
152 entry.
cache = cache.str();
156std::vector<SymbolLocatorSymStore::LookupEntry> GetGlobalLookupOrder() {
157 std::vector<SymbolLocatorSymStore::LookupEntry> result;
159 const char *sym_path = std::getenv(
"_NT_SYMBOL_PATH");
161 result.push_back(std::move(entry));
163 const char *alt_path = std::getenv(
"_NT_ALT_SYMBOL_PATH");
165 result.push_back(std::move(entry));
168 result.push_back(MakeLookupEntry(url.ref()));
173std::optional<SymbolLocatorSymStore::LookupEntry>
174ParseSrvEntry(llvm::StringRef entry) {
175 llvm::SmallVector<llvm::StringRef, 4> parts;
176 entry.trim().split(parts,
'*');
179 switch (parts.size()) {
181 return MakeLookupEntry(parts[1]);
184 if (parts[1].empty())
185 return MakeLookupEntry(parts[2],
187 return MakeLookupEntry(parts[2], parts[1]);
194std::optional<std::string> ParseCacheEntry(llvm::StringRef entry) {
195 llvm::SmallVector<llvm::StringRef, 2> parts;
196 entry.trim().split(parts,
'*');
199 if (parts.size() > 2)
203 llvm::StringRef value;
204 if (parts.size() == 2)
205 value = parts.back();
221std::string FormatSymStoreKey(
const UUID &uuid) {
222 llvm::ArrayRef<uint8_t> bytes = uuid.
GetBytes();
223 uint32_t age = llvm::support::endian::read32be(bytes.data() + 16);
224 constexpr bool lower_case =
false;
225 return llvm::toHex(bytes.slice(0, 16), lower_case) + std::to_string(age);
228bool HasUnsafeCharacters(llvm::StringRef s) {
229 for (
unsigned char c : s) {
231 if ((c >=
'A' && c <=
'Z') || (c >=
'a' && c <=
'z') ||
232 (c >=
'0' && c <=
'9') || c ==
'-' || c ==
'.' || c ==
'_' ||
241 return s ==
"." || s ==
"..";
244std::optional<FileSpec>
245RequestFileFromSymStoreServerHTTP(llvm::StringRef base_url, llvm::StringRef key,
246 llvm::StringRef pdb_name) {
247 using namespace llvm::sys;
250 if (HasUnsafeCharacters(pdb_name)) {
252 "rejecting HTTP lookup for PDB file due to unsafe characters in "
259 llvm::SmallString<128> tmp_file;
260 constexpr bool erase_on_reboot =
true;
261 path::system_temp_directory(erase_on_reboot, tmp_file);
262 path::append(tmp_file, llvm::formatv(
"lldb_symstore_{0}_{1}", key, pdb_name));
265 std::string source_url =
266 llvm::formatv(
"{0}/{1}/{2}/{1}", base_url, pdb_name, key);
268 if (!llvm::HTTPClient::isAvailable()) {
270 "HTTP client is not available for SymStore download");
274 llvm::HTTPClient client;
277 client.setTimeout(std::chrono::seconds(60));
279 llvm::StreamedHTTPResponseHandler Handler(
280 [dest = tmp_file.str().str()]()
281 -> llvm::Expected<std::unique_ptr<llvm::CachedFileStream>> {
283 auto os = std::make_unique<llvm::raw_fd_ostream>(dest, ec);
285 return llvm::createStringError(ec,
"Failed to open file for writing");
286 return std::make_unique<llvm::CachedFileStream>(std::move(os), dest);
290 llvm::HTTPRequest request(source_url);
291 request.PinnedCertFingerprint =
293 if (llvm::Error Err = client.perform(request, Handler)) {
295 llvm::formatv(
"failed to download from SymStore '{0}': {1}", source_url,
296 llvm::toString(std::move(Err))));
299 if (llvm::Error Err = Handler.commit()) {
301 llvm::formatv(
"failed to download from SymStore '{0}': {1}", source_url,
302 llvm::toString(std::move(Err))));
306 unsigned responseCode = client.responseCode();
307 switch (responseCode) {
314 "failed to download from SymStore '{0}': response code {1}", source_url,
320std::optional<FileSpec> FindFileInLocalSymStore(llvm::StringRef root_dir,
322 llvm::StringRef pdb_name) {
323 llvm::SmallString<256> path;
324 llvm::sys::path::append(path, root_dir, pdb_name, key, pdb_name);
332std::optional<FileSpec> MoveToLocalSymStore(llvm::StringRef cache,
334 llvm::StringRef pdb_name,
337 llvm::SmallString<256> dest_dir;
338 llvm::sys::path::append(dest_dir, cache, pdb_name, key);
339 if (std::error_code ec = llvm::sys::fs::create_directories(dest_dir)) {
341 llvm::formatv(
"failed to create SymStore cache directory '{0}': {1}",
342 dest_dir, ec.message()));
346 llvm::SmallString<256> dest;
347 llvm::sys::path::append(dest, dest_dir, pdb_name);
348 std::error_code ec = llvm::sys::fs::rename(tmp_file.
GetPath(), dest);
351 if (ec == std::errc::cross_device_link) {
352 ec = llvm::sys::fs::copy_file(tmp_file.
GetPath(), dest);
354 llvm::sys::fs::remove(tmp_file.
GetPath());
358 llvm::formatv(
"failed to move '{0}' to SymStore cache '{1}': {2}",
359 tmp_file.
GetPath(), dest, ec.message()));
366std::string SelectSymStoreCache(std::optional<std::string> sympath_cache) {
367 llvm::SmallVector<std::string, 2> candidates;
371 assert(!sympath_cache->empty() &&
"Empty entries resolve to default cache");
372 candidates.push_back(*sympath_cache);
379 for (
const auto &path : candidates) {
380 if (llvm::sys::fs::is_directory(path))
382 if (std::error_code ec = llvm::sys::fs::create_directories(path)) {
383 LLDB_LOG(log,
"Ignoring invalid SymStore cache directory '{0}': {1}",
394std::optional<FileSpec>
396 llvm::StringRef key, llvm::StringRef pdb_name) {
398 llvm::StringRef url = entry.
source;
399 if (url.starts_with(
"http://") || url.starts_with(
"https://")) {
401 std::string cache_path = SelectSymStoreCache(entry.
cache);
402 if (
auto spec = FindFileInLocalSymStore(cache_path, key, pdb_name)) {
403 LLDB_LOG(log,
"Found {0} in SymStore cache {1}", pdb_name, cache_path);
408 if (
auto tmp_file = RequestFileFromSymStoreServerHTTP(url, key, pdb_name)) {
409 LLDB_LOG(log,
"Downloaded {0} from SymStore {1}", pdb_name, url);
410 auto spec = MoveToLocalSymStore(cache_path, key, pdb_name, *tmp_file);
415 spec = MoveToLocalSymStore(cache_path, key, pdb_name, *tmp_file);
419 LLDB_LOG(log,
"Added {0} to SymStore cache {1}", pdb_name, cache_path);
426 llvm::StringRef file = entry.
source;
427 if (file.starts_with(
"file://"))
428 file = file.drop_front(7);
429 if (
auto spec = FindFileInLocalSymStore(file, key, pdb_name)) {
447 std::string pdb_name =
449 if (pdb_name.empty()) {
451 "Failed to resolve symbol PDB module: PDB name empty");
462 std::string key = FormatSymStoreKey(uuid);
463 for (
const LookupEntry &entry : GetGlobalLookupOrder()) {
464 if (
auto spec = LocateSymStoreEntry(entry, key, pdb_name))
471std::vector<SymbolLocatorSymStore::LookupEntry>
476 std::vector<LookupEntry> result;
477 std::optional<std::string> implicit_cache;
478 llvm::SmallVector<llvm::StringRef, 2> entries;
479 val.split(entries,
';');
481 for (llvm::StringRef raw : entries) {
482 llvm::StringRef entry = raw.trim();
488 if (entry.starts_with_insensitive(
"cache*")) {
489 if (
auto cache = ParseCacheEntry(entry))
490 implicit_cache = *cache;
496 if (entry.starts_with_insensitive(
"symsrv*")) {
498 llvm::formatv(
"ignoring unsupported entry in env: {0}", entry));
504 if (entry.starts_with_insensitive(
"srv*")) {
505 if (
auto lookup_entry = ParseSrvEntry(entry)) {
506 if (!lookup_entry->cache && implicit_cache)
507 lookup_entry->cache = implicit_cache;
508 result.push_back(*lookup_entry);
514 result.push_back(MakeLookupEntry(entry));
522 llvm::SmallString<128> cache_dir;
523 if (llvm::sys::path::cache_directory(cache_dir)) {
524 llvm::sys::path::append(cache_dir,
"lldb",
"symstore");
525 return cache_dir.str().str();
528 constexpr bool erase_on_reboot =
false;
529 llvm::sys::path::system_temp_directory(erase_on_reboot, cache_dir);
530 llvm::sys::path::append(cache_dir,
"lldb",
"symstore");
531 return cache_dir.str().str();
static PluginProperties & GetGlobalPluginProperties()
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
#define LLDB_LOG_VERBOSE(log,...)
#define LLDB_PLUGIN_DEFINE(PluginName)
static PluginProperties & GetGlobalPluginProperties()
llvm::StringRef GetStringRef() const
Get the string value as a llvm::StringRef.
A class to manage flag bits.
static void ReportWarning(std::string message, std::optional< lldb::user_id_t > debugger_id=std::nullopt, std::once_flag *once=nullptr)
Report warning events.
const ConstString & GetFilename() const
Filename string const get accessor.
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
static FileSystem & Instance()
static ModuleListProperties & GetGlobalModuleListProperties()
FileSpec & GetSymbolFileSpec()
llvm::StringRef GetCurrentValueAsRef() const
const char * GetCurrentValue() const
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description, ABICreateInstance create_callback)
static lldb::OptionValuePropertiesSP GetSettingForSymbolLocatorPlugin(Debugger &debugger, llvm::StringRef setting_name)
static bool UnregisterPlugin(ABICreateInstance create_callback)
static bool CreateSettingForSymbolLocatorPlugin(Debugger &debugger, const lldb::OptionValuePropertiesSP &properties_sp, llvm::StringRef description, bool is_global_property)
This plugin implements lookup in Microsoft SymStore instances.
static void DebuggerInitialize(Debugger &debugger)
static lldb_private::SymbolLocator * CreateInstance()
static llvm::StringRef GetPluginNameStatic()
static llvm::StringRef GetPluginDescriptionStatic()
static std::optional< FileSpec > LocateExecutableSymbolFile(const ModuleSpec &module_spec, const FileSpecList &default_search_paths)
static std::string GetSystemDefaultCachePath()
static std::vector< LookupEntry > ParseEnvSymbolPaths(llvm::StringRef val)
Represents UUID's of various sizes.
llvm::ArrayRef< uint8_t > GetBytes() const
std::string GetAsString(llvm::StringRef separator="-") const
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
std::optional< std::string > cache