Skip to content

Commit 7e70d70

Browse files
authored
[LLDB][NFC] Remove Debugger dependency in SystemLifetimeManager (#134383)
It reduces the memory usage in lldb-server.
1 parent 3b84b1e commit 7e70d70

File tree

8 files changed

+55
-50
lines changed

8 files changed

+55
-50
lines changed

lldb/include/lldb/API/SBDebugger.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
namespace lldb_private {
1818
class CommandPluginInterfaceImplementation;
19+
class SystemInitializerFull;
1920
namespace python {
2021
class SWIGBridge;
2122
}
@@ -508,6 +509,7 @@ class LLDB_API SBDebugger {
508509
protected:
509510
friend class lldb_private::CommandPluginInterfaceImplementation;
510511
friend class lldb_private::python::SWIGBridge;
512+
friend class lldb_private::SystemInitializerFull;
511513

512514
SBDebugger(const lldb::DebuggerSP &debugger_sp);
513515

lldb/include/lldb/Initialization/SystemLifetimeManager.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ class SystemLifetimeManager {
2323
SystemLifetimeManager();
2424
~SystemLifetimeManager();
2525

26-
llvm::Error Initialize(std::unique_ptr<SystemInitializer> initializer,
27-
LoadPluginCallbackType plugin_callback);
26+
llvm::Error Initialize(std::unique_ptr<SystemInitializer> initializer);
2827
void Terminate();
2928

3029
private:

lldb/source/API/SBDebugger.cpp

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -179,48 +179,9 @@ void SBDebugger::Initialize() {
179179
lldb::SBError SBDebugger::InitializeWithErrorHandling() {
180180
LLDB_INSTRUMENT();
181181

182-
auto LoadPlugin = [](const lldb::DebuggerSP &debugger_sp,
183-
const FileSpec &spec,
184-
Status &error) -> llvm::sys::DynamicLibrary {
185-
llvm::sys::DynamicLibrary dynlib =
186-
llvm::sys::DynamicLibrary::getPermanentLibrary(spec.GetPath().c_str());
187-
if (dynlib.isValid()) {
188-
typedef bool (*LLDBCommandPluginInit)(lldb::SBDebugger debugger);
189-
190-
lldb::SBDebugger debugger_sb(debugger_sp);
191-
// This calls the bool lldb::PluginInitialize(lldb::SBDebugger debugger)
192-
// function.
193-
// TODO: mangle this differently for your system - on OSX, the first
194-
// underscore needs to be removed and the second one stays
195-
LLDBCommandPluginInit init_func =
196-
(LLDBCommandPluginInit)(uintptr_t)dynlib.getAddressOfSymbol(
197-
"_ZN4lldb16PluginInitializeENS_10SBDebuggerE");
198-
if (init_func) {
199-
if (init_func(debugger_sb))
200-
return dynlib;
201-
else
202-
error = Status::FromErrorString(
203-
"plug-in refused to load "
204-
"(lldb::PluginInitialize(lldb::SBDebugger) "
205-
"returned false)");
206-
} else {
207-
error = Status::FromErrorString(
208-
"plug-in is missing the required initialization: "
209-
"lldb::PluginInitialize(lldb::SBDebugger)");
210-
}
211-
} else {
212-
if (FileSystem::Instance().Exists(spec))
213-
error = Status::FromErrorString(
214-
"this file does not represent a loadable dylib");
215-
else
216-
error = Status::FromErrorString("no such file");
217-
}
218-
return llvm::sys::DynamicLibrary();
219-
};
220-
221182
SBError error;
222183
if (auto e = g_debugger_lifetime->Initialize(
223-
std::make_unique<SystemInitializerFull>(), LoadPlugin)) {
184+
std::make_unique<SystemInitializerFull>())) {
224185
error.SetError(Status::FromError(std::move(e)));
225186
}
226187
return error;

lldb/source/API/SystemInitializerFull.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "SystemInitializerFull.h"
1010
#include "lldb/API/SBCommandInterpreter.h"
11+
#include "lldb/API/SBDebugger.h"
1112
#include "lldb/Core/Debugger.h"
1213
#include "lldb/Core/PluginManager.h"
1314
#include "lldb/Core/Progress.h"
@@ -86,10 +87,53 @@ llvm::Error SystemInitializerFull::Initialize() {
8687

8788
LLDB_LOG(GetLog(SystemLog::System), "{0}", GetVersion());
8889

90+
auto LoadPlugin = [](const lldb::DebuggerSP &debugger_sp,
91+
const FileSpec &spec,
92+
Status &error) -> llvm::sys::DynamicLibrary {
93+
llvm::sys::DynamicLibrary dynlib =
94+
llvm::sys::DynamicLibrary::getPermanentLibrary(spec.GetPath().c_str());
95+
if (dynlib.isValid()) {
96+
typedef bool (*LLDBCommandPluginInit)(lldb::SBDebugger debugger);
97+
98+
lldb::SBDebugger debugger_sb(debugger_sp);
99+
// This calls the bool lldb::PluginInitialize(lldb::SBDebugger debugger)
100+
// function.
101+
// TODO: mangle this differently for your system - on OSX, the first
102+
// underscore needs to be removed and the second one stays
103+
LLDBCommandPluginInit init_func =
104+
(LLDBCommandPluginInit)(uintptr_t)dynlib.getAddressOfSymbol(
105+
"_ZN4lldb16PluginInitializeENS_10SBDebuggerE");
106+
if (init_func) {
107+
if (init_func(debugger_sb))
108+
return dynlib;
109+
else
110+
error = Status::FromErrorString(
111+
"plug-in refused to load "
112+
"(lldb::PluginInitialize(lldb::SBDebugger) "
113+
"returned false)");
114+
} else {
115+
error = Status::FromErrorString(
116+
"plug-in is missing the required initialization: "
117+
"lldb::PluginInitialize(lldb::SBDebugger)");
118+
}
119+
} else {
120+
if (FileSystem::Instance().Exists(spec))
121+
error = Status::FromErrorString(
122+
"this file does not represent a loadable dylib");
123+
else
124+
error = Status::FromErrorString("no such file");
125+
}
126+
return llvm::sys::DynamicLibrary();
127+
};
128+
129+
Debugger::Initialize(LoadPlugin);
130+
89131
return llvm::Error::success();
90132
}
91133

92134
void SystemInitializerFull::Terminate() {
135+
Debugger::Terminate();
136+
93137
Debugger::SettingsTerminate();
94138

95139
// Terminate plug-ins in core LLDB.

lldb/source/Initialization/SystemLifetimeManager.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
#include "lldb/Initialization/SystemLifetimeManager.h"
1010

11-
#include "lldb/Core/Debugger.h"
1211
#include "lldb/Initialization/SystemInitializer.h"
1312

1413
#include <utility>
@@ -23,8 +22,7 @@ SystemLifetimeManager::~SystemLifetimeManager() {
2322
}
2423

2524
llvm::Error SystemLifetimeManager::Initialize(
26-
std::unique_ptr<SystemInitializer> initializer,
27-
LoadPluginCallbackType plugin_callback) {
25+
std::unique_ptr<SystemInitializer> initializer) {
2826
std::lock_guard<std::recursive_mutex> guard(m_mutex);
2927
if (!m_initialized) {
3028
assert(!m_initializer && "Attempting to call "
@@ -35,8 +33,6 @@ llvm::Error SystemLifetimeManager::Initialize(
3533

3634
if (auto e = m_initializer->Initialize())
3735
return e;
38-
39-
Debugger::Initialize(plugin_callback);
4036
}
4137

4238
return llvm::Error::success();
@@ -46,7 +42,6 @@ void SystemLifetimeManager::Terminate() {
4642
std::lock_guard<std::recursive_mutex> guard(m_mutex);
4743

4844
if (m_initialized) {
49-
Debugger::Terminate();
5045
m_initializer->Terminate();
5146

5247
m_initializer.reset();

lldb/tools/lldb-server/lldb-server.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ int main_platform(int argc, char *argv[]);
4141
namespace llgs {
4242
static void initialize() {
4343
if (auto e = g_debugger_lifetime->Initialize(
44-
std::make_unique<SystemInitializerLLGS>(), nullptr))
44+
std::make_unique<SystemInitializerLLGS>()))
4545
llvm::consumeError(std::move(e));
4646
}
4747

lldb/tools/lldb-test/SystemInitializerTest.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,14 @@ llvm::Error SystemInitializerTest::Initialize() {
5151
// Settings must be initialized AFTER PluginManager::Initialize is called.
5252
Debugger::SettingsInitialize();
5353

54+
Debugger::Initialize(nullptr);
55+
5456
return llvm::Error::success();
5557
}
5658

5759
void SystemInitializerTest::Terminate() {
60+
Debugger::Terminate();
61+
5862
Debugger::SettingsTerminate();
5963

6064
// Terminate and unload and loaded system or user LLDB plug-ins

lldb/tools/lldb-test/lldb-test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1247,7 +1247,7 @@ int main(int argc, const char *argv[]) {
12471247

12481248
SystemLifetimeManager DebuggerLifetime;
12491249
if (auto e = DebuggerLifetime.Initialize(
1250-
std::make_unique<SystemInitializerTest>(), nullptr)) {
1250+
std::make_unique<SystemInitializerTest>())) {
12511251
WithColor::error() << "initialization failed: " << toString(std::move(e))
12521252
<< '\n';
12531253
return 1;

0 commit comments

Comments
 (0)