Skip to content

Commit 2026873

Browse files
authored
Add enable/disable api for SystemRuntime plugins (llvm#133794)
This commit adds support for enabling and disabling plugins by name. The changes are made generically in the `PluginInstances` class, but currently we only expose the ability to SystemRuntime plugins. Other plugins types can be added easily. We had a few design goals for how disabled plugins should work 1. Plugins that are disabled should still be visible to the system. This allows us to dynamically enable and disable plugins and report their state to the user. 2. Plugin order should be stable across disable and enable changes. We want avoid changing the order of plugin lookup. When a plugin is re-enabled it should return to its original slot in the creation order. 3. Disabled plugins should not appear in PluginManager operations. Clients should be able to assume that only enabled plugins will be returned from the PluginManager. For the implementation we modify the plugin instance to maintain a bool of its enabled state. Existing clients external to the Instances class expect to iterate over only enabled instance so we skip over disabed instances in the query and snapshot apis. This way the client does not have to manually check which instances are enabled.
1 parent c59d3a2 commit 2026873

File tree

4 files changed

+433
-3
lines changed

4 files changed

+433
-3
lines changed

lldb/include/lldb/Core/PluginManager.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#include <cstddef>
2424
#include <cstdint>
25+
#include <vector>
2526

2627
#define LLDB_PLUGIN_DEFINE_ADV(ClassName, PluginName) \
2728
namespace lldb_private { \
@@ -47,6 +48,12 @@ class CommandInterpreter;
4748
class Debugger;
4849
class StringList;
4950

51+
struct RegisteredPluginInfo {
52+
llvm::StringRef name = "";
53+
llvm::StringRef description = "";
54+
bool enabled = false;
55+
};
56+
5057
class PluginManager {
5158
public:
5259
static void Initialize();
@@ -168,6 +175,12 @@ class PluginManager {
168175
static SystemRuntimeCreateInstance
169176
GetSystemRuntimeCreateCallbackAtIndex(uint32_t idx);
170177

178+
static std::vector<RegisteredPluginInfo> GetSystemRuntimePluginInfo();
179+
180+
// Modify the enabled state of a SystemRuntime plugin.
181+
// Returns false if the plugin name is not found.
182+
static bool SetSystemRuntimePluginEnabled(llvm::StringRef name, bool enabled);
183+
171184
// ObjectFile
172185
static bool
173186
RegisterPlugin(llvm::StringRef name, llvm::StringRef description,

lldb/source/Core/PluginManager.cpp

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,13 @@ template <typename Callback> struct PluginInstance {
188188
PluginInstance(llvm::StringRef name, llvm::StringRef description,
189189
Callback create_callback,
190190
DebuggerInitializeCallback debugger_init_callback = nullptr)
191-
: name(name), description(description), create_callback(create_callback),
191+
: name(name), description(description), enabled(true),
192+
create_callback(create_callback),
192193
debugger_init_callback(debugger_init_callback) {}
193194

194195
llvm::StringRef name;
195196
llvm::StringRef description;
197+
bool enabled;
196198
Callback create_callback;
197199
DebuggerInitializeCallback debugger_init_callback;
198200
};
@@ -250,7 +252,9 @@ template <typename Instance> class PluginInstances {
250252
}
251253

252254
void PerformDebuggerCallback(Debugger &debugger) {
253-
for (auto &instance : m_instances) {
255+
for (const auto &instance : m_instances) {
256+
if (!instance.enabled)
257+
continue;
254258
if (instance.debugger_init_callback)
255259
instance.debugger_init_callback(debugger);
256260
}
@@ -260,7 +264,14 @@ template <typename Instance> class PluginInstances {
260264
// Note that this is a copy of the internal state so modifications
261265
// to the returned instances will not be reflected back to instances
262266
// stored by the PluginInstances object.
263-
std::vector<Instance> GetSnapshot() { return m_instances; }
267+
std::vector<Instance> GetSnapshot() {
268+
std::vector<Instance> enabled_instances;
269+
for (const auto &instance : m_instances) {
270+
if (instance.enabled)
271+
enabled_instances.push_back(instance);
272+
}
273+
return enabled_instances;
274+
}
264275

265276
const Instance *GetInstanceAtIndex(uint32_t idx) {
266277
uint32_t count = 0;
@@ -280,12 +291,41 @@ template <typename Instance> class PluginInstances {
280291
const Instance *
281292
FindEnabledInstance(std::function<bool(const Instance &)> predicate) const {
282293
for (const auto &instance : m_instances) {
294+
if (!instance.enabled)
295+
continue;
283296
if (predicate(instance))
284297
return &instance;
285298
}
286299
return nullptr;
287300
}
288301

302+
// Return a list of all the registered plugin instances. This includes both
303+
// enabled and disabled instances. The instances are listed in the order they
304+
// were registered which is the order they would be queried if they were all
305+
// enabled.
306+
std::vector<RegisteredPluginInfo> GetPluginInfoForAllInstances() {
307+
// Lookup the plugin info for each instance in the sorted order.
308+
std::vector<RegisteredPluginInfo> plugin_infos;
309+
plugin_infos.reserve(m_instances.size());
310+
for (const Instance &instance : m_instances)
311+
plugin_infos.push_back(
312+
{instance.name, instance.description, instance.enabled});
313+
314+
return plugin_infos;
315+
}
316+
317+
bool SetInstanceEnabled(llvm::StringRef name, bool enable) {
318+
auto it = std::find_if(
319+
m_instances.begin(), m_instances.end(),
320+
[&](const Instance &instance) { return instance.name == name; });
321+
322+
if (it == m_instances.end())
323+
return false;
324+
325+
it->enabled = enable;
326+
return true;
327+
}
328+
289329
private:
290330
std::vector<Instance> m_instances;
291331
};
@@ -627,6 +667,15 @@ PluginManager::GetSystemRuntimeCreateCallbackAtIndex(uint32_t idx) {
627667
return GetSystemRuntimeInstances().GetCallbackAtIndex(idx);
628668
}
629669

670+
std::vector<RegisteredPluginInfo> PluginManager::GetSystemRuntimePluginInfo() {
671+
return GetSystemRuntimeInstances().GetPluginInfoForAllInstances();
672+
}
673+
674+
bool PluginManager::SetSystemRuntimePluginEnabled(llvm::StringRef name,
675+
bool enable) {
676+
return GetSystemRuntimeInstances().SetInstanceEnabled(name, enable);
677+
}
678+
630679
#pragma mark ObjectFile
631680

632681
struct ObjectFileInstance : public PluginInstance<ObjectFileCreateInstance> {

lldb/unittests/Core/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ add_lldb_unittest(LLDBCoreTests
77
FormatEntityTest.cpp
88
MangledTest.cpp
99
ModuleSpecTest.cpp
10+
PluginManagerTest.cpp
1011
ProgressReportTest.cpp
1112
RichManglingContextTest.cpp
1213
SourceLocationSpecTest.cpp

0 commit comments

Comments
 (0)