Skip to content

Commit c3a302d

Browse files
authored
[lldb] Convert LocateSymbolFile into a plugin (#71151)
This commit contains the initial scaffolding to convert the functionality currently implemented in LocateSymbolFile to a plugin architecture. The plugin approach allows us to easily add new ways to find symbols and fixes some issues with the current implementation. For instance, currently we (ab)use the host OS to include support for querying the DebugSymbols framework on macOS. The plugin approach retains all the benefits (including the ability to compile this out on other platforms) while maintaining a higher level of separation with the platform independent code. To limit the scope of this patch, I've only converted a single function: LocateExecutableObjectFile. Future commits will convert the remaining LocateSymbolFile functions and eventually remove LocateSymbolFile. To make reviewing easier, that will done as follow-ups.
1 parent eb16603 commit c3a302d

18 files changed

+623
-32
lines changed

lldb/include/lldb/Core/PluginManager.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,19 @@ class PluginManager {
345345
static SymbolVendorCreateInstance
346346
GetSymbolVendorCreateCallbackAtIndex(uint32_t idx);
347347

348+
// SymbolLocator
349+
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description,
350+
SymbolLocatorCreateInstance create_callback,
351+
SymbolLocatorLocateExecutableObjectFile
352+
locate_executable_object_file = nullptr);
353+
354+
static bool UnregisterPlugin(SymbolLocatorCreateInstance create_callback);
355+
356+
static SymbolLocatorCreateInstance
357+
GetSymbolLocatorCreateCallbackAtIndex(uint32_t idx);
358+
359+
static ModuleSpec LocateExecutableObjectFile(const ModuleSpec &module_spec);
360+
348361
// Trace
349362
static bool RegisterPlugin(
350363
llvm::StringRef name, llvm::StringRef description,

lldb/include/lldb/Symbol/LocateSymbolFile.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,6 @@ class UUID;
2424

2525
class Symbols {
2626
public:
27-
// Locate the executable file given a module specification.
28-
//
29-
// Locating the file should happen only on the local computer or using the
30-
// current computers global settings.
31-
static ModuleSpec LocateExecutableObjectFile(const ModuleSpec &module_spec);
32-
3327
// Locate the symbol file given a module specification.
3428
//
3529
// Locating the file should happen only on the local computer or using the
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===-- SymbolLocator.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_SYMBOLLOCATOR_H
10+
#define LLDB_SYMBOL_SYMBOLLOCATOR_H
11+
12+
#include "lldb/Core/PluginInterface.h"
13+
14+
namespace lldb_private {
15+
16+
class SymbolLocator : public PluginInterface {
17+
public:
18+
SymbolLocator() = default;
19+
};
20+
21+
} // namespace lldb_private
22+
23+
#endif // LLDB_SYMBOL_SYMBOLFILELOCATOR_H

lldb/include/lldb/lldb-forward.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ class SymbolContextScope;
217217
class SymbolContextSpecifier;
218218
class SymbolFile;
219219
class SymbolFileType;
220+
class SymbolLocator;
220221
class SymbolVendor;
221222
class Symtab;
222223
class SyntheticChildren;

lldb/include/lldb/lldb-private-interfaces.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ typedef SymbolVendor *(*SymbolVendorCreateInstance)(
8989
const lldb::ModuleSP &module_sp,
9090
lldb_private::Stream
9191
*feedback_strm); // Module can be NULL for default system symbol vendor
92+
typedef SymbolLocator *(*SymbolLocatorCreateInstance)();
93+
typedef std::optional<ModuleSpec> (*SymbolLocatorLocateExecutableObjectFile)(
94+
const ModuleSpec &module_spec);
9295
typedef bool (*BreakpointHitCallback)(void *baton,
9396
StoppointCallbackContext *context,
9497
lldb::user_id_t break_id,

lldb/source/Core/DynamicLoader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ ModuleSP DynamicLoader::LoadBinaryWithUUIDAndAddress(
221221
module_spec.GetSymbolFileSpec() =
222222
Symbols::LocateExecutableSymbolFile(module_spec, search_paths);
223223
ModuleSpec objfile_module_spec =
224-
Symbols::LocateExecutableObjectFile(module_spec);
224+
PluginManager::LocateExecutableObjectFile(module_spec);
225225
module_spec.GetFileSpec() = objfile_module_spec.GetFileSpec();
226226
if (FileSystem::Instance().Exists(module_spec.GetFileSpec()) &&
227227
FileSystem::Instance().Exists(module_spec.GetSymbolFileSpec())) {

lldb/source/Core/ModuleList.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "lldb/Core/ModuleList.h"
1010
#include "lldb/Core/Module.h"
1111
#include "lldb/Core/ModuleSpec.h"
12+
#include "lldb/Core/PluginManager.h"
1213
#include "lldb/Host/FileSystem.h"
1314
#include "lldb/Interpreter/OptionValueFileSpec.h"
1415
#include "lldb/Interpreter/OptionValueFileSpecList.h"
@@ -906,7 +907,7 @@ ModuleList::GetSharedModule(const ModuleSpec &module_spec, ModuleSP &module_sp,
906907
// Fixup the incoming path in case the path points to a valid file, yet the
907908
// arch or UUID (if one was passed in) don't match.
908909
ModuleSpec located_binary_modulespec =
909-
Symbols::LocateExecutableObjectFile(module_spec);
910+
PluginManager::LocateExecutableObjectFile(module_spec);
910911

911912
// Don't look for the file if it appears to be the same one we already
912913
// checked for above...

lldb/source/Core/PluginManager.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,6 +1081,59 @@ PluginManager::GetSymbolVendorCreateCallbackAtIndex(uint32_t idx) {
10811081
return GetSymbolVendorInstances().GetCallbackAtIndex(idx);
10821082
}
10831083

1084+
#pragma mark SymbolLocator
1085+
1086+
struct SymbolLocatorInstance
1087+
: public PluginInstance<SymbolLocatorCreateInstance> {
1088+
SymbolLocatorInstance(
1089+
llvm::StringRef name, llvm::StringRef description,
1090+
CallbackType create_callback,
1091+
SymbolLocatorLocateExecutableObjectFile locate_executable_object_file)
1092+
: PluginInstance<SymbolLocatorCreateInstance>(name, description,
1093+
create_callback),
1094+
locate_executable_object_file(locate_executable_object_file) {}
1095+
1096+
SymbolLocatorLocateExecutableObjectFile locate_executable_object_file;
1097+
};
1098+
typedef PluginInstances<SymbolLocatorInstance> SymbolLocatorInstances;
1099+
1100+
static SymbolLocatorInstances &GetSymbolLocatorInstances() {
1101+
static SymbolLocatorInstances g_instances;
1102+
return g_instances;
1103+
}
1104+
1105+
bool PluginManager::RegisterPlugin(
1106+
llvm::StringRef name, llvm::StringRef description,
1107+
SymbolLocatorCreateInstance create_callback,
1108+
SymbolLocatorLocateExecutableObjectFile locate_executable_object_file) {
1109+
return GetSymbolLocatorInstances().RegisterPlugin(
1110+
name, description, create_callback, locate_executable_object_file);
1111+
}
1112+
1113+
bool PluginManager::UnregisterPlugin(
1114+
SymbolLocatorCreateInstance create_callback) {
1115+
return GetSymbolLocatorInstances().UnregisterPlugin(create_callback);
1116+
}
1117+
1118+
SymbolLocatorCreateInstance
1119+
PluginManager::GetSymbolLocatorCreateCallbackAtIndex(uint32_t idx) {
1120+
return GetSymbolLocatorInstances().GetCallbackAtIndex(idx);
1121+
}
1122+
1123+
ModuleSpec
1124+
PluginManager::LocateExecutableObjectFile(const ModuleSpec &module_spec) {
1125+
auto &instances = GetSymbolLocatorInstances().GetInstances();
1126+
for (auto &instance : instances) {
1127+
if (instance.locate_executable_object_file) {
1128+
std::optional<ModuleSpec> result =
1129+
instance.locate_executable_object_file(module_spec);
1130+
if (result)
1131+
return *result;
1132+
}
1133+
}
1134+
return {};
1135+
}
1136+
10841137
#pragma mark Trace
10851138

10861139
struct TraceInstance

lldb/source/Plugins/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ add_subdirectory(ScriptInterpreter)
2020
add_subdirectory(StructuredData)
2121
add_subdirectory(SymbolFile)
2222
add_subdirectory(SystemRuntime)
23+
add_subdirectory(SymbolLocator)
2324
add_subdirectory(SymbolVendor)
2425
add_subdirectory(Trace)
2526
add_subdirectory(TraceExporter)

lldb/source/Plugins/Process/MacOSX-Kernel/ProcessKDP.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ Status ProcessKDP::DoConnectRemote(llvm::StringRef remote_url) {
282282
search_paths);
283283
if (module_spec.GetSymbolFileSpec()) {
284284
ModuleSpec executable_module_spec =
285-
Symbols::LocateExecutableObjectFile(module_spec);
285+
PluginManager::LocateExecutableObjectFile(module_spec);
286286
if (FileSystem::Instance().Exists(
287287
executable_module_spec.GetFileSpec())) {
288288
module_spec.GetFileSpec() =
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
add_subdirectory(Default)
2+
if (CMAKE_SYSTEM_NAME MATCHES "Darwin")
3+
add_subdirectory(DebugSymbols)
4+
endif()
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
add_lldb_library(lldbPluginSymbolLocatorDebugSymbols PLUGIN
2+
SymbolLocatorDebugSymbols.cpp
3+
4+
LINK_LIBS
5+
lldbCore
6+
lldbHost
7+
lldbSymbol
8+
)

0 commit comments

Comments
 (0)