From 9b2246d9fad9fe62be26d22e184f241304779551 Mon Sep 17 00:00:00 2001 From: Jonas Devlieghere Date: Fri, 3 Nov 2023 18:07:57 -0700 Subject: [PATCH] [lldb] Move FindSymbolFileInBundle to SymbolLocator plugin (#71247) This builds on top of the work started in c3a302d to convert LocateSymbolFile to a SymbolLocator plugin. This commit moves FindSymbolFileInBundle. --- lldb/include/lldb/Core/PluginManager.h | 14 ++++-- lldb/include/lldb/Symbol/LocateSymbolFile.h | 4 -- lldb/include/lldb/lldb-private-interfaces.h | 2 + lldb/source/Core/PluginManager.cpp | 28 +++++++++-- .../Platform/MacOSX/PlatformDarwin.cpp | 4 +- .../SymbolLocatorDebugSymbols.cpp | 48 +++++++++++++++++-- .../DebugSymbols/SymbolLocatorDebugSymbols.h | 4 ++ lldb/source/Symbol/LocateSymbolFile.cpp | 7 --- lldb/source/Symbol/LocateSymbolFileMacOSX.cpp | 44 ++--------------- 9 files changed, 88 insertions(+), 67 deletions(-) diff --git a/lldb/include/lldb/Core/PluginManager.h b/lldb/include/lldb/Core/PluginManager.h index feaa351b71fa..b69aff9e0065 100644 --- a/lldb/include/lldb/Core/PluginManager.h +++ b/lldb/include/lldb/Core/PluginManager.h @@ -346,10 +346,12 @@ public: GetSymbolVendorCreateCallbackAtIndex(uint32_t idx); // SymbolLocator - static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description, - SymbolLocatorCreateInstance create_callback, - SymbolLocatorLocateExecutableObjectFile - locate_executable_object_file = nullptr); + static bool RegisterPlugin( + llvm::StringRef name, llvm::StringRef description, + SymbolLocatorCreateInstance create_callback, + SymbolLocatorLocateExecutableObjectFile locate_executable_object_file = + nullptr, + SymbolLocatorFindSymbolFileInBundle find_symbol_file_in_bundle = nullptr); static bool UnregisterPlugin(SymbolLocatorCreateInstance create_callback); @@ -358,6 +360,10 @@ public: static ModuleSpec LocateExecutableObjectFile(const ModuleSpec &module_spec); + static FileSpec FindSymbolFileInBundle(const FileSpec &dsym_bundle_fspec, + const UUID *uuid, + const ArchSpec *arch); + // Trace static bool RegisterPlugin( llvm::StringRef name, llvm::StringRef description, diff --git a/lldb/include/lldb/Symbol/LocateSymbolFile.h b/lldb/include/lldb/Symbol/LocateSymbolFile.h index 8cd62cd3f73e..03d3d9a2d440 100644 --- a/lldb/include/lldb/Symbol/LocateSymbolFile.h +++ b/lldb/include/lldb/Symbol/LocateSymbolFile.h @@ -32,10 +32,6 @@ public: LocateExecutableSymbolFile(const ModuleSpec &module_spec, const FileSpecList &default_search_paths); - static FileSpec FindSymbolFileInBundle(const FileSpec &dsym_bundle_fspec, - const lldb_private::UUID *uuid, - const ArchSpec *arch); - // Locate the object and symbol file given a module specification. // // Locating the file can try to download the file from a corporate build diff --git a/lldb/include/lldb/lldb-private-interfaces.h b/lldb/include/lldb/lldb-private-interfaces.h index a22a3e9792b3..faf85833fe9f 100644 --- a/lldb/include/lldb/lldb-private-interfaces.h +++ b/lldb/include/lldb/lldb-private-interfaces.h @@ -92,6 +92,8 @@ typedef SymbolVendor *(*SymbolVendorCreateInstance)( typedef SymbolLocator *(*SymbolLocatorCreateInstance)(); typedef std::optional (*SymbolLocatorLocateExecutableObjectFile)( const ModuleSpec &module_spec); +typedef std::optional (*SymbolLocatorFindSymbolFileInBundle)( + const FileSpec &dsym_bundle_fspec, const UUID *uuid, const ArchSpec *arch); typedef bool (*BreakpointHitCallback)(void *baton, StoppointCallbackContext *context, lldb::user_id_t break_id, diff --git a/lldb/source/Core/PluginManager.cpp b/lldb/source/Core/PluginManager.cpp index 85e3f1cdcf12..85c6b1d948a2 100644 --- a/lldb/source/Core/PluginManager.cpp +++ b/lldb/source/Core/PluginManager.cpp @@ -1088,12 +1088,15 @@ struct SymbolLocatorInstance SymbolLocatorInstance( llvm::StringRef name, llvm::StringRef description, CallbackType create_callback, - SymbolLocatorLocateExecutableObjectFile locate_executable_object_file) + SymbolLocatorLocateExecutableObjectFile locate_executable_object_file, + SymbolLocatorFindSymbolFileInBundle find_symbol_file_in_bundle) : PluginInstance(name, description, create_callback), - locate_executable_object_file(locate_executable_object_file) {} + locate_executable_object_file(locate_executable_object_file), + find_symbol_file_in_bundle(find_symbol_file_in_bundle) {} SymbolLocatorLocateExecutableObjectFile locate_executable_object_file; + SymbolLocatorFindSymbolFileInBundle find_symbol_file_in_bundle; }; typedef PluginInstances SymbolLocatorInstances; @@ -1105,9 +1108,11 @@ static SymbolLocatorInstances &GetSymbolLocatorInstances() { bool PluginManager::RegisterPlugin( llvm::StringRef name, llvm::StringRef description, SymbolLocatorCreateInstance create_callback, - SymbolLocatorLocateExecutableObjectFile locate_executable_object_file) { + SymbolLocatorLocateExecutableObjectFile locate_executable_object_file, + SymbolLocatorFindSymbolFileInBundle find_symbol_file_in_bundle) { return GetSymbolLocatorInstances().RegisterPlugin( - name, description, create_callback, locate_executable_object_file); + name, description, create_callback, locate_executable_object_file, + find_symbol_file_in_bundle); } bool PluginManager::UnregisterPlugin( @@ -1134,6 +1139,21 @@ PluginManager::LocateExecutableObjectFile(const ModuleSpec &module_spec) { return {}; } +FileSpec PluginManager::FindSymbolFileInBundle(const FileSpec &symfile_bundle, + const UUID *uuid, + const ArchSpec *arch) { + auto &instances = GetSymbolLocatorInstances().GetInstances(); + for (auto &instance : instances) { + if (instance.find_symbol_file_in_bundle) { + std::optional result = + instance.find_symbol_file_in_bundle(symfile_bundle, uuid, arch); + if (result) + return *result; + } + } + return {}; +} + #pragma mark Trace struct TraceInstance diff --git a/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp b/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp index 765902c7cf29..ab7012e3d21e 100644 --- a/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp +++ b/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp @@ -321,8 +321,8 @@ Status PlatformDarwin::ResolveSymbolFile(Target &target, FileSpec &sym_file) { sym_file = sym_spec.GetSymbolFileSpec(); if (FileSystem::Instance().IsDirectory(sym_file)) { - sym_file = Symbols::FindSymbolFileInBundle(sym_file, sym_spec.GetUUIDPtr(), - sym_spec.GetArchitecturePtr()); + sym_file = PluginManager::FindSymbolFileInBundle( + sym_file, sym_spec.GetUUIDPtr(), sym_spec.GetArchitecturePtr()); } return {}; } diff --git a/lldb/source/Plugins/SymbolLocator/DebugSymbols/SymbolLocatorDebugSymbols.cpp b/lldb/source/Plugins/SymbolLocator/DebugSymbols/SymbolLocatorDebugSymbols.cpp index ef19507e87e5..217080bfd3ef 100644 --- a/lldb/source/Plugins/SymbolLocator/DebugSymbols/SymbolLocatorDebugSymbols.cpp +++ b/lldb/source/Plugins/SymbolLocator/DebugSymbols/SymbolLocatorDebugSymbols.cpp @@ -63,9 +63,9 @@ LLDB_PLUGIN_DEFINE(SymbolLocatorDebugSymbols) SymbolLocatorDebugSymbols::SymbolLocatorDebugSymbols() : SymbolLocator() {} void SymbolLocatorDebugSymbols::Initialize() { - PluginManager::RegisterPlugin(GetPluginNameStatic(), - GetPluginDescriptionStatic(), CreateInstance, - LocateExecutableObjectFile); + PluginManager::RegisterPlugin( + GetPluginNameStatic(), GetPluginDescriptionStatic(), CreateInstance, + LocateExecutableObjectFile, FindSymbolFileInBundle); } void SymbolLocatorDebugSymbols::Terminate() { @@ -153,8 +153,8 @@ std::optional SymbolLocatorDebugSymbols::LocateExecutableObjectFile( FileSystem::Instance().Resolve(dsym_filespec); if (FileSystem::Instance().IsDirectory(dsym_filespec)) { - dsym_filespec = - Symbols::FindSymbolFileInBundle(dsym_filespec, uuid, arch); + dsym_filespec = PluginManager::FindSymbolFileInBundle( + dsym_filespec, uuid, arch); ++items_found; } else { ++items_found; @@ -325,3 +325,41 @@ std::optional SymbolLocatorDebugSymbols::LocateExecutableObjectFile( return {}; } + +std::optional SymbolLocatorDebugSymbols::FindSymbolFileInBundle( + const FileSpec &dsym_bundle_fspec, const UUID *uuid, const ArchSpec *arch) { + std::string dsym_bundle_path = dsym_bundle_fspec.GetPath(); + llvm::SmallString<128> buffer(dsym_bundle_path); + llvm::sys::path::append(buffer, "Contents", "Resources", "DWARF"); + + std::error_code EC; + llvm::IntrusiveRefCntPtr vfs = + FileSystem::Instance().GetVirtualFileSystem(); + llvm::vfs::recursive_directory_iterator Iter(*vfs, buffer.str(), EC); + llvm::vfs::recursive_directory_iterator End; + for (; Iter != End && !EC; Iter.increment(EC)) { + llvm::ErrorOr Status = vfs->status(Iter->path()); + if (Status->isDirectory()) + continue; + + FileSpec dsym_fspec(Iter->path()); + ModuleSpecList module_specs; + if (ObjectFile::GetModuleSpecifications(dsym_fspec, 0, 0, module_specs)) { + ModuleSpec spec; + for (size_t i = 0; i < module_specs.GetSize(); ++i) { + bool got_spec = module_specs.GetModuleSpecAtIndex(i, spec); + assert(got_spec); // The call has side-effects so can't be inlined. + UNUSED_IF_ASSERT_DISABLED(got_spec); + if ((uuid == nullptr || + (spec.GetUUIDPtr() && spec.GetUUID() == *uuid)) && + (arch == nullptr || + (spec.GetArchitecturePtr() && + spec.GetArchitecture().IsCompatibleMatch(*arch)))) { + return dsym_fspec; + } + } + } + } + + return {}; +} diff --git a/lldb/source/Plugins/SymbolLocator/DebugSymbols/SymbolLocatorDebugSymbols.h b/lldb/source/Plugins/SymbolLocator/DebugSymbols/SymbolLocatorDebugSymbols.h index a7641b28c0dc..256ac9372edd 100644 --- a/lldb/source/Plugins/SymbolLocator/DebugSymbols/SymbolLocatorDebugSymbols.h +++ b/lldb/source/Plugins/SymbolLocator/DebugSymbols/SymbolLocatorDebugSymbols.h @@ -37,6 +37,10 @@ public: // current computers global settings. static std::optional LocateExecutableObjectFile(const ModuleSpec &module_spec); + + static std::optional + FindSymbolFileInBundle(const FileSpec &dsym_bundle_fspec, const UUID *uuid, + const ArchSpec *arch); }; } // namespace lldb_private diff --git a/lldb/source/Symbol/LocateSymbolFile.cpp b/lldb/source/Symbol/LocateSymbolFile.cpp index b23b53230a4f..d9414ef93aa7 100644 --- a/lldb/source/Symbol/LocateSymbolFile.cpp +++ b/lldb/source/Symbol/LocateSymbolFile.cpp @@ -408,13 +408,6 @@ void Symbols::DownloadSymbolFileAsync(const UUID &uuid) { #if !defined(__APPLE__) -FileSpec Symbols::FindSymbolFileInBundle(const FileSpec &symfile_bundle, - const lldb_private::UUID *uuid, - const ArchSpec *arch) { - // FIXME - return FileSpec(); -} - bool Symbols::DownloadObjectAndSymbolFile(ModuleSpec &module_spec, Status &error, bool force_lookup, bool copy_executable) { diff --git a/lldb/source/Symbol/LocateSymbolFileMacOSX.cpp b/lldb/source/Symbol/LocateSymbolFileMacOSX.cpp index 943eae7e066f..be0bb37bab53 100644 --- a/lldb/source/Symbol/LocateSymbolFileMacOSX.cpp +++ b/lldb/source/Symbol/LocateSymbolFileMacOSX.cpp @@ -21,6 +21,7 @@ #include "lldb/Core/Module.h" #include "lldb/Core/ModuleList.h" #include "lldb/Core/ModuleSpec.h" +#include "lldb/Core/PluginManager.h" #include "lldb/Host/Host.h" #include "lldb/Host/HostInfo.h" #include "lldb/Symbol/ObjectFile.h" @@ -119,8 +120,8 @@ int LocateMacOSXFilesUsingDebugSymbols(const ModuleSpec &module_spec, FileSystem::Instance().Resolve(dsym_filespec); if (FileSystem::Instance().IsDirectory(dsym_filespec)) { - dsym_filespec = - Symbols::FindSymbolFileInBundle(dsym_filespec, uuid, arch); + dsym_filespec = PluginManager::FindSymbolFileInBundle( + dsym_filespec, uuid, arch); ++items_found; } else { ++items_found; @@ -289,45 +290,6 @@ int LocateMacOSXFilesUsingDebugSymbols(const ModuleSpec &module_spec, return items_found; } -FileSpec Symbols::FindSymbolFileInBundle(const FileSpec &dsym_bundle_fspec, - const lldb_private::UUID *uuid, - const ArchSpec *arch) { - std::string dsym_bundle_path = dsym_bundle_fspec.GetPath(); - llvm::SmallString<128> buffer(dsym_bundle_path); - llvm::sys::path::append(buffer, "Contents", "Resources", "DWARF"); - - std::error_code EC; - llvm::IntrusiveRefCntPtr vfs = - FileSystem::Instance().GetVirtualFileSystem(); - llvm::vfs::recursive_directory_iterator Iter(*vfs, buffer.str(), EC); - llvm::vfs::recursive_directory_iterator End; - for (; Iter != End && !EC; Iter.increment(EC)) { - llvm::ErrorOr Status = vfs->status(Iter->path()); - if (Status->isDirectory()) - continue; - - FileSpec dsym_fspec(Iter->path()); - ModuleSpecList module_specs; - if (ObjectFile::GetModuleSpecifications(dsym_fspec, 0, 0, module_specs)) { - ModuleSpec spec; - for (size_t i = 0; i < module_specs.GetSize(); ++i) { - bool got_spec = module_specs.GetModuleSpecAtIndex(i, spec); - assert(got_spec); // The call has side-effects so can't be inlined. - UNUSED_IF_ASSERT_DISABLED(got_spec); - if ((uuid == nullptr || - (spec.GetUUIDPtr() && spec.GetUUID() == *uuid)) && - (arch == nullptr || - (spec.GetArchitecturePtr() && - spec.GetArchitecture().IsCompatibleMatch(*arch)))) { - return dsym_fspec; - } - } - } - } - - return {}; -} - static bool GetModuleSpecInfoFromUUIDDictionary(CFDictionaryRef uuid_dict, ModuleSpec &module_spec, Status &error,