Skip to content

[LLDB][NFC] Remove Debugger dependency in SystemLifetimeManager #134383

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lldb/include/lldb/API/SBDebugger.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

namespace lldb_private {
class CommandPluginInterfaceImplementation;
class SystemInitializerFull;
namespace python {
class SWIGBridge;
}
Expand Down Expand Up @@ -508,6 +509,7 @@ class LLDB_API SBDebugger {
protected:
friend class lldb_private::CommandPluginInterfaceImplementation;
friend class lldb_private::python::SWIGBridge;
friend class lldb_private::SystemInitializerFull;

SBDebugger(const lldb::DebuggerSP &debugger_sp);

Expand Down
3 changes: 1 addition & 2 deletions lldb/include/lldb/Initialization/SystemLifetimeManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ class SystemLifetimeManager {
SystemLifetimeManager();
~SystemLifetimeManager();

llvm::Error Initialize(std::unique_ptr<SystemInitializer> initializer,
LoadPluginCallbackType plugin_callback);
llvm::Error Initialize(std::unique_ptr<SystemInitializer> initializer);
void Terminate();

private:
Expand Down
41 changes: 1 addition & 40 deletions lldb/source/API/SBDebugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,48 +179,9 @@ void SBDebugger::Initialize() {
lldb::SBError SBDebugger::InitializeWithErrorHandling() {
LLDB_INSTRUMENT();

auto LoadPlugin = [](const lldb::DebuggerSP &debugger_sp,
const FileSpec &spec,
Status &error) -> llvm::sys::DynamicLibrary {
llvm::sys::DynamicLibrary dynlib =
llvm::sys::DynamicLibrary::getPermanentLibrary(spec.GetPath().c_str());
if (dynlib.isValid()) {
typedef bool (*LLDBCommandPluginInit)(lldb::SBDebugger debugger);

lldb::SBDebugger debugger_sb(debugger_sp);
// This calls the bool lldb::PluginInitialize(lldb::SBDebugger debugger)
// function.
// TODO: mangle this differently for your system - on OSX, the first
// underscore needs to be removed and the second one stays
LLDBCommandPluginInit init_func =
(LLDBCommandPluginInit)(uintptr_t)dynlib.getAddressOfSymbol(
"_ZN4lldb16PluginInitializeENS_10SBDebuggerE");
if (init_func) {
if (init_func(debugger_sb))
return dynlib;
else
error = Status::FromErrorString(
"plug-in refused to load "
"(lldb::PluginInitialize(lldb::SBDebugger) "
"returned false)");
} else {
error = Status::FromErrorString(
"plug-in is missing the required initialization: "
"lldb::PluginInitialize(lldb::SBDebugger)");
}
} else {
if (FileSystem::Instance().Exists(spec))
error = Status::FromErrorString(
"this file does not represent a loadable dylib");
else
error = Status::FromErrorString("no such file");
}
return llvm::sys::DynamicLibrary();
};

SBError error;
if (auto e = g_debugger_lifetime->Initialize(
std::make_unique<SystemInitializerFull>(), LoadPlugin)) {
std::make_unique<SystemInitializerFull>())) {
error.SetError(Status::FromError(std::move(e)));
}
return error;
Expand Down
44 changes: 44 additions & 0 deletions lldb/source/API/SystemInitializerFull.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "SystemInitializerFull.h"
#include "lldb/API/SBCommandInterpreter.h"
#include "lldb/API/SBDebugger.h"
#include "lldb/Core/Debugger.h"
#include "lldb/Core/PluginManager.h"
#include "lldb/Core/Progress.h"
Expand Down Expand Up @@ -86,10 +87,53 @@ llvm::Error SystemInitializerFull::Initialize() {

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

auto LoadPlugin = [](const lldb::DebuggerSP &debugger_sp,
const FileSpec &spec,
Status &error) -> llvm::sys::DynamicLibrary {
llvm::sys::DynamicLibrary dynlib =
llvm::sys::DynamicLibrary::getPermanentLibrary(spec.GetPath().c_str());
if (dynlib.isValid()) {
typedef bool (*LLDBCommandPluginInit)(lldb::SBDebugger debugger);

lldb::SBDebugger debugger_sb(debugger_sp);
// This calls the bool lldb::PluginInitialize(lldb::SBDebugger debugger)
// function.
// TODO: mangle this differently for your system - on OSX, the first
// underscore needs to be removed and the second one stays
LLDBCommandPluginInit init_func =
(LLDBCommandPluginInit)(uintptr_t)dynlib.getAddressOfSymbol(
"_ZN4lldb16PluginInitializeENS_10SBDebuggerE");
if (init_func) {
if (init_func(debugger_sb))
return dynlib;
else
error = Status::FromErrorString(
"plug-in refused to load "
"(lldb::PluginInitialize(lldb::SBDebugger) "
"returned false)");
} else {
error = Status::FromErrorString(
"plug-in is missing the required initialization: "
"lldb::PluginInitialize(lldb::SBDebugger)");
}
} else {
if (FileSystem::Instance().Exists(spec))
error = Status::FromErrorString(
"this file does not represent a loadable dylib");
else
error = Status::FromErrorString("no such file");
}
return llvm::sys::DynamicLibrary();
};

Debugger::Initialize(LoadPlugin);

return llvm::Error::success();
}

void SystemInitializerFull::Terminate() {
Debugger::Terminate();

Debugger::SettingsTerminate();

// Terminate plug-ins in core LLDB.
Expand Down
7 changes: 1 addition & 6 deletions lldb/source/Initialization/SystemLifetimeManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

#include "lldb/Initialization/SystemLifetimeManager.h"

#include "lldb/Core/Debugger.h"
#include "lldb/Initialization/SystemInitializer.h"

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

llvm::Error SystemLifetimeManager::Initialize(
std::unique_ptr<SystemInitializer> initializer,
LoadPluginCallbackType plugin_callback) {
std::unique_ptr<SystemInitializer> initializer) {
std::lock_guard<std::recursive_mutex> guard(m_mutex);
if (!m_initialized) {
assert(!m_initializer && "Attempting to call "
Expand All @@ -35,8 +33,6 @@ llvm::Error SystemLifetimeManager::Initialize(

if (auto e = m_initializer->Initialize())
return e;

Debugger::Initialize(plugin_callback);
}

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

if (m_initialized) {
Debugger::Terminate();
m_initializer->Terminate();

m_initializer.reset();
Expand Down
2 changes: 1 addition & 1 deletion lldb/tools/lldb-server/lldb-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ int main_platform(int argc, char *argv[]);
namespace llgs {
static void initialize() {
if (auto e = g_debugger_lifetime->Initialize(
std::make_unique<SystemInitializerLLGS>(), nullptr))
std::make_unique<SystemInitializerLLGS>()))
llvm::consumeError(std::move(e));
}

Expand Down
4 changes: 4 additions & 0 deletions lldb/tools/lldb-test/SystemInitializerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,14 @@ llvm::Error SystemInitializerTest::Initialize() {
// Settings must be initialized AFTER PluginManager::Initialize is called.
Debugger::SettingsInitialize();

Debugger::Initialize(nullptr);

return llvm::Error::success();
}

void SystemInitializerTest::Terminate() {
Debugger::Terminate();

Debugger::SettingsTerminate();

// Terminate and unload and loaded system or user LLDB plug-ins
Expand Down
2 changes: 1 addition & 1 deletion lldb/tools/lldb-test/lldb-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1247,7 +1247,7 @@ int main(int argc, const char *argv[]) {

SystemLifetimeManager DebuggerLifetime;
if (auto e = DebuggerLifetime.Initialize(
std::make_unique<SystemInitializerTest>(), nullptr)) {
std::make_unique<SystemInitializerTest>())) {
WithColor::error() << "initialization failed: " << toString(std::move(e))
<< '\n';
return 1;
Expand Down