Skip to content

Add enable/disable api for SystemRuntime plugins #133794

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 1 commit into from
Apr 2, 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
13 changes: 13 additions & 0 deletions lldb/include/lldb/Core/PluginManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include <cstddef>
#include <cstdint>
#include <vector>

#define LLDB_PLUGIN_DEFINE_ADV(ClassName, PluginName) \
namespace lldb_private { \
Expand All @@ -47,6 +48,12 @@ class CommandInterpreter;
class Debugger;
class StringList;

struct RegisteredPluginInfo {
llvm::StringRef name = "";
llvm::StringRef description = "";
bool enabled = false;
};

class PluginManager {
public:
static void Initialize();
Expand Down Expand Up @@ -168,6 +175,12 @@ class PluginManager {
static SystemRuntimeCreateInstance
GetSystemRuntimeCreateCallbackAtIndex(uint32_t idx);

static std::vector<RegisteredPluginInfo> GetSystemRuntimePluginInfo();

// Modify the enabled state of a SystemRuntime plugin.
// Returns false if the plugin name is not found.
static bool SetSystemRuntimePluginEnabled(llvm::StringRef name, bool enabled);

// ObjectFile
static bool
RegisterPlugin(llvm::StringRef name, llvm::StringRef description,
Expand Down
55 changes: 52 additions & 3 deletions lldb/source/Core/PluginManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,13 @@ template <typename Callback> struct PluginInstance {
PluginInstance(llvm::StringRef name, llvm::StringRef description,
Callback create_callback,
DebuggerInitializeCallback debugger_init_callback = nullptr)
: name(name), description(description), create_callback(create_callback),
: name(name), description(description), enabled(true),
create_callback(create_callback),
debugger_init_callback(debugger_init_callback) {}

llvm::StringRef name;
llvm::StringRef description;
bool enabled;
Callback create_callback;
DebuggerInitializeCallback debugger_init_callback;
};
Expand Down Expand Up @@ -250,7 +252,9 @@ template <typename Instance> class PluginInstances {
}

void PerformDebuggerCallback(Debugger &debugger) {
for (auto &instance : m_instances) {
for (const auto &instance : m_instances) {
if (!instance.enabled)
continue;
if (instance.debugger_init_callback)
instance.debugger_init_callback(debugger);
}
Expand All @@ -260,7 +264,14 @@ template <typename Instance> class PluginInstances {
// Note that this is a copy of the internal state so modifications
// to the returned instances will not be reflected back to instances
// stored by the PluginInstances object.
std::vector<Instance> GetSnapshot() { return m_instances; }
std::vector<Instance> GetSnapshot() {
std::vector<Instance> enabled_instances;
for (const auto &instance : m_instances) {
if (instance.enabled)
enabled_instances.push_back(instance);
}
return enabled_instances;
}

const Instance *GetInstanceAtIndex(uint32_t idx) {
uint32_t count = 0;
Expand All @@ -280,12 +291,41 @@ template <typename Instance> class PluginInstances {
const Instance *
FindEnabledInstance(std::function<bool(const Instance &)> predicate) const {
for (const auto &instance : m_instances) {
if (!instance.enabled)
continue;
if (predicate(instance))
return &instance;
}
return nullptr;
}

// Return a list of all the registered plugin instances. This includes both
// enabled and disabled instances. The instances are listed in the order they
// were registered which is the order they would be queried if they were all
// enabled.
std::vector<RegisteredPluginInfo> GetPluginInfoForAllInstances() {
// Lookup the plugin info for each instance in the sorted order.
std::vector<RegisteredPluginInfo> plugin_infos;
plugin_infos.reserve(m_instances.size());
for (const Instance &instance : m_instances)
plugin_infos.push_back(
{instance.name, instance.description, instance.enabled});

return plugin_infos;
}

bool SetInstanceEnabled(llvm::StringRef name, bool enable) {
auto it = std::find_if(
m_instances.begin(), m_instances.end(),
[&](const Instance &instance) { return instance.name == name; });

if (it == m_instances.end())
return false;

it->enabled = enable;
return true;
}

private:
std::vector<Instance> m_instances;
};
Expand Down Expand Up @@ -627,6 +667,15 @@ PluginManager::GetSystemRuntimeCreateCallbackAtIndex(uint32_t idx) {
return GetSystemRuntimeInstances().GetCallbackAtIndex(idx);
}

std::vector<RegisteredPluginInfo> PluginManager::GetSystemRuntimePluginInfo() {
return GetSystemRuntimeInstances().GetPluginInfoForAllInstances();
}

bool PluginManager::SetSystemRuntimePluginEnabled(llvm::StringRef name,
bool enable) {
return GetSystemRuntimeInstances().SetInstanceEnabled(name, enable);
}

#pragma mark ObjectFile

struct ObjectFileInstance : public PluginInstance<ObjectFileCreateInstance> {
Expand Down
1 change: 1 addition & 0 deletions lldb/unittests/Core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ add_lldb_unittest(LLDBCoreTests
FormatEntityTest.cpp
MangledTest.cpp
ModuleSpecTest.cpp
PluginManagerTest.cpp
ProgressReportTest.cpp
RichManglingContextTest.cpp
SourceLocationSpecTest.cpp
Expand Down
Loading