Skip to content

Commit d4fe522

Browse files
authored
Add commands to list/enable/disable plugins (llvm#134418)
This commit adds three new commands for managing plugins. The `list` command will show which plugins are currently registered and their enabled state. The `enable` and `disable` commands can be used to enable or disable plugins. A disabled plugin will not show up to the PluginManager when it iterates over available plugins of a particular type. The purpose of these commands is to provide more visibility into registered plugins and allow users to disable plugins for experimental perf reasons. There are a few limitations to the current implementation 1. Only SystemRuntime and InstrumentationRuntime plugins are currently supported. We can easily extend the existing implementation to support more types. The scope was limited to these plugins to keep the PR size manageable. 2. Only "statically" know plugin types are supported (i.e. those managed by the PluginManager and not from `plugin load`). It is possibly we could support dynamic plugins as well, but I have not looked into it yet.
1 parent 61cdba6 commit d4fe522

File tree

13 files changed

+782
-1
lines changed

13 files changed

+782
-1
lines changed

lldb/include/lldb/Core/PluginManager.h

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@
1919
#include "lldb/lldb-enumerations.h"
2020
#include "lldb/lldb-forward.h"
2121
#include "lldb/lldb-private-interfaces.h"
22+
#include "llvm/ADT/ArrayRef.h"
2223
#include "llvm/ADT/StringRef.h"
24+
#include "llvm/Support/JSON.h"
2325

2426
#include <cstddef>
2527
#include <cstdint>
28+
#include <functional>
2629
#include <vector>
2730

2831
#define LLDB_PLUGIN_DEFINE_ADV(ClassName, PluginName) \
@@ -55,12 +58,67 @@ struct RegisteredPluginInfo {
5558
bool enabled = false;
5659
};
5760

61+
// Define some data structures to describe known plugin "namespaces".
62+
// The PluginManager is organized into a series of static functions
63+
// that operate on different types of plugins. For example SystemRuntime
64+
// and ObjectFile plugins.
65+
//
66+
// The namespace name is used a prefix when matching plugin names. For example,
67+
// if we have an "macosx" plugin in the "system-runtime" namespace then we will
68+
// match a plugin name pattern against the "system-runtime.macosx" name.
69+
//
70+
// The plugin namespace here is used so we can operate on all the plugins
71+
// of a given type so it is easy to enable or disable them as a group.
72+
using GetPluginInfo = std::function<std::vector<RegisteredPluginInfo>()>;
73+
using SetPluginEnabled = std::function<bool(llvm::StringRef, bool)>;
74+
struct PluginNamespace {
75+
llvm::StringRef name;
76+
GetPluginInfo get_info;
77+
SetPluginEnabled set_enabled;
78+
};
79+
5880
class PluginManager {
5981
public:
6082
static void Initialize();
6183

6284
static void Terminate();
6385

86+
// Support for enabling and disabling plugins.
87+
88+
// Return the plugins that can be enabled or disabled by the user.
89+
static llvm::ArrayRef<PluginNamespace> GetPluginNamespaces();
90+
91+
// Generate a json object that describes the plugins that are available.
92+
// This is a json representation of the plugin info returned by
93+
// GetPluginNamespaces().
94+
//
95+
// {
96+
// <plugin-namespace>: [
97+
// {
98+
// "enabled": <bool>,
99+
// "name": <plugin-name>,
100+
// },
101+
// ...
102+
// ],
103+
// ...
104+
// }
105+
//
106+
// If pattern is given it will be used to filter the plugins that are
107+
// are returned. The pattern filters the plugin names using the
108+
// PluginManager::MatchPluginName() function.
109+
static llvm::json::Object GetJSON(llvm::StringRef pattern = "");
110+
111+
// Return true if the pattern matches the plugin name.
112+
//
113+
// The pattern matches the name if it is exactly equal to the namespace name
114+
// or if it is equal to the qualified name, which is the namespace name
115+
// followed by a dot and the plugin name (e.g. "system-runtime.foo").
116+
//
117+
// An empty pattern matches all plugins.
118+
static bool MatchPluginName(llvm::StringRef pattern,
119+
const PluginNamespace &plugin_ns,
120+
const RegisteredPluginInfo &plugin);
121+
64122
// ABI
65123
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description,
66124
ABICreateInstance create_callback);
@@ -491,6 +549,12 @@ class PluginManager {
491549
static InstrumentationRuntimeCreateInstance
492550
GetInstrumentationRuntimeCreateCallbackAtIndex(uint32_t idx);
493551

552+
static std::vector<RegisteredPluginInfo>
553+
GetInstrumentationRuntimePluginInfo();
554+
555+
static bool SetInstrumentationRuntimePluginEnabled(llvm::StringRef name,
556+
bool enabled);
557+
494558
// TypeSystem
495559
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description,
496560
TypeSystemCreateInstance create_callback,

lldb/include/lldb/Interpreter/CommandOptionArgumentTable.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ static constexpr CommandObject::ArgumentTableEntry g_argument_table[] = {
314314
{ lldb::eArgTypeModule, "module", lldb::CompletionType::eModuleCompletion, {}, { nullptr, false }, "The name of a module loaded into the current target." },
315315
{ lldb::eArgTypeCPUName, "cpu-name", lldb::CompletionType::eNoCompletion, {}, { nullptr, false }, "The name of a CPU." },
316316
{ lldb::eArgTypeCPUFeatures, "cpu-features", lldb::CompletionType::eNoCompletion, {}, { nullptr, false }, "The CPU feature string." },
317+
{ lldb::eArgTypeManagedPlugin, "managed-plugin", lldb::CompletionType::eNoCompletion, {}, { nullptr, false }, "Plugins managed by the PluginManager" },
317318
// clang-format on
318319
};
319320

lldb/include/lldb/Target/Statistics.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,21 @@ struct StatisticsOptions {
196196
return !GetSummaryOnly();
197197
}
198198

199+
void SetIncludePlugins(bool value) { m_include_plugins = value; }
200+
bool GetIncludePlugins() const {
201+
if (m_include_plugins.has_value())
202+
return m_include_plugins.value();
203+
// Default to true in both default mode and summary mode.
204+
return true;
205+
}
206+
199207
private:
200208
std::optional<bool> m_summary_only;
201209
std::optional<bool> m_load_all_debug_info;
202210
std::optional<bool> m_include_targets;
203211
std::optional<bool> m_include_modules;
204212
std::optional<bool> m_include_transcript;
213+
std::optional<bool> m_include_plugins;
205214
};
206215

207216
/// A class that represents statistics about a TypeSummaryProviders invocations

lldb/include/lldb/lldb-enumerations.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,7 @@ enum CommandArgumentType {
663663
eArgTypeModule,
664664
eArgTypeCPUName,
665665
eArgTypeCPUFeatures,
666+
eArgTypeManagedPlugin,
666667
eArgTypeLastArg // Always keep this entry as the last entry in this
667668
// enumeration!!
668669
};

0 commit comments

Comments
 (0)