Skip to content

Commit 1b5ed3a

Browse files
committed
Add enable/disable api for SystemRuntime plugins
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. We explored two other ways to implement this change: 1. Keep two separate lists and move the instances between them when enabling and disabling plugins. This requires maintaining the original insert order to satisfy the goals above and thus adds some complexity to the code. 2. Return a copy of the instances list with disabled plugins removed when clients iterate over them. This requires extra copies and would potentially lead to the copies getting out of sync with the actual stored instances because any modifications to the instances would be lost. Instead, we modify the plugin instance to maintain a bool of its enabled state. Clients external to Instances class expect to iterate over only enabled instance so we skip over disabed instances in the `ForEachEabledPlugin` callback. This way the client does not have to manually check which instances are enabled.
1 parent a818f7d commit 1b5ed3a

File tree

4 files changed

+425
-3
lines changed

4 files changed

+425
-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: 44 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,10 +252,11 @@ template <typename Instance> class PluginInstances {
250252
}
251253

252254
void PerformDebuggerCallback(Debugger &debugger) {
253-
for (auto &instance : m_instances) {
255+
ForEachEnabledPlugin([&](const Instance &instance) {
254256
if (instance.debugger_init_callback)
255257
instance.debugger_init_callback(debugger);
256-
}
258+
return IterationAction::Continue;
259+
});
257260
}
258261

259262
const Instance *GetInstanceAtIndex(uint32_t idx) {
@@ -278,6 +281,8 @@ template <typename Instance> class PluginInstances {
278281
void ForEachEnabledPlugin(
279282
std::function<IterationAction(const Instance &)> callback) const {
280283
for (auto &instance : m_instances) {
284+
if (!instance.enabled)
285+
continue;
281286
if (callback(instance) == IterationAction::Stop)
282287
return;
283288
}
@@ -296,6 +301,33 @@ template <typename Instance> class PluginInstances {
296301
return found;
297302
}
298303

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

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

642683
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)