1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
|
//===-- CommandObjectPlugin.cpp -------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "CommandObjectPlugin.h"
#include "lldb/Core/PluginManager.h"
#include "lldb/Host/OptionParser.h"
#include "lldb/Interpreter/CommandInterpreter.h"
#include "lldb/Interpreter/CommandReturnObject.h"
using namespace lldb;
using namespace lldb_private;
class CommandObjectPluginLoad : public CommandObjectParsed {
public:
CommandObjectPluginLoad(CommandInterpreter &interpreter)
: CommandObjectParsed(interpreter, "plugin load",
"Import a dylib that implements an LLDB plugin.",
nullptr) {
AddSimpleArgumentList(eArgTypeFilename);
}
~CommandObjectPluginLoad() override = default;
protected:
void DoExecute(Args &command, CommandReturnObject &result) override {
size_t argc = command.GetArgumentCount();
if (argc != 1) {
result.AppendError("'plugin load' requires one argument");
return;
}
Status error;
FileSpec dylib_fspec(command[0].ref());
FileSystem::Instance().Resolve(dylib_fspec);
if (GetDebugger().LoadPlugin(dylib_fspec, error))
result.SetStatus(eReturnStatusSuccessFinishResult);
else {
result.AppendError(error.AsCString());
}
}
};
namespace {
// Helper function to perform an action on each matching plugin.
// The action callback is given the containing namespace along with plugin info
// for each matching plugin.
static int ActOnMatchingPlugins(
const llvm::StringRef pattern,
std::function<void(const PluginNamespace &plugin_namespace,
const std::vector<RegisteredPluginInfo> &plugin_info)>
action) {
int num_matching = 0;
for (const PluginNamespace &plugin_namespace :
PluginManager::GetPluginNamespaces()) {
std::vector<RegisteredPluginInfo> matching_plugins;
for (const RegisteredPluginInfo &plugin_info :
plugin_namespace.get_info()) {
if (PluginManager::MatchPluginName(pattern, plugin_namespace,
plugin_info))
matching_plugins.push_back(plugin_info);
}
if (!matching_plugins.empty()) {
num_matching += matching_plugins.size();
action(plugin_namespace, matching_plugins);
}
}
return num_matching;
}
// Call the "SetEnable" function for each matching plugins.
// Used to share the majority of the code between the enable
// and disable commands.
int SetEnableOnMatchingPlugins(const llvm::StringRef &pattern,
CommandReturnObject &result, bool enabled) {
return ActOnMatchingPlugins(
pattern, [&](const PluginNamespace &plugin_namespace,
const std::vector<RegisteredPluginInfo> &plugins) {
result.AppendMessage(plugin_namespace.name);
for (const auto &plugin : plugins) {
if (!plugin_namespace.set_enabled(plugin.name, enabled)) {
result.AppendErrorWithFormat("failed to enable plugin %s.%s",
plugin_namespace.name.data(),
plugin.name.data());
continue;
}
result.AppendMessageWithFormat(
" %s %-30s %s\n", enabled ? "[+]" : "[-]", plugin.name.data(),
plugin.description.data());
}
});
}
static std::string ConvertJSONToPrettyString(const llvm::json::Value &json) {
std::string str;
llvm::raw_string_ostream os(str);
os << llvm::formatv("{0:2}", json).str();
os.flush();
return str;
}
#define LLDB_OPTIONS_plugin_list
#include "CommandOptions.inc"
// These option definitions are used by the plugin list command.
class PluginListCommandOptions : public Options {
public:
PluginListCommandOptions() = default;
~PluginListCommandOptions() override = default;
Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
ExecutionContext *execution_context) override {
Status error;
const int short_option = m_getopt_table[option_idx].val;
switch (short_option) {
case 'j':
m_json_format = true;
break;
default:
llvm_unreachable("Unimplemented option");
}
return error;
}
void OptionParsingStarting(ExecutionContext *execution_context) override {
m_json_format = false;
}
llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
return llvm::ArrayRef(g_plugin_list_options);
}
// Instance variables to hold the values for command options.
bool m_json_format = false;
};
} // namespace
class CommandObjectPluginList : public CommandObjectParsed {
public:
CommandObjectPluginList(CommandInterpreter &interpreter)
: CommandObjectParsed(interpreter, "plugin list",
"Report info about registered LLDB plugins.",
nullptr) {
AddSimpleArgumentList(eArgTypeManagedPlugin);
SetHelpLong(R"(
Display information about registered plugins.
The plugin information is formatted as shown below:
<plugin-namespace>
[+] <plugin-name> Plugin #1 description
[-] <plugin-name> Plugin #2 description
An enabled plugin is marked with [+] and a disabled plugin is marked with [-].
Plugins can be listed by namespace and name with:
plugin list <plugin-namespace>[.<plugin-name>]
Plugins can be listed by namespace alone or with a fully qualified name. When listed
with just a namespace all plugins in that namespace are listed. When no arguments
are given all plugins are listed.
Examples:
List all plugins
(lldb) plugin list
List all plugins in the system-runtime namespace
(lldb) plugin list system-runtime
List only the plugin 'foo' matching a fully qualified name exactly
(lldb) plugin list system-runtime.foo
)");
}
~CommandObjectPluginList() override = default;
Options *GetOptions() override { return &m_options; }
protected:
void DoExecute(Args &command, CommandReturnObject &result) override {
size_t argc = command.GetArgumentCount();
result.SetStatus(eReturnStatusSuccessFinishResult);
// Create a temporary vector to hold the patterns to simplify the logic
// for the case when the user passes no patterns
std::vector<llvm::StringRef> patterns;
patterns.reserve(argc == 0 ? 1 : argc);
if (argc == 0)
patterns.push_back("");
else
for (size_t i = 0; i < argc; ++i)
patterns.push_back(command[i].ref());
if (m_options.m_json_format)
OutputJsonFormat(patterns, result);
else
OutputTextFormat(patterns, result);
}
private:
void OutputJsonFormat(const std::vector<llvm::StringRef> &patterns,
CommandReturnObject &result) {
llvm::json::Object obj;
bool found_empty = false;
for (const llvm::StringRef pattern : patterns) {
llvm::json::Object pat_obj = PluginManager::GetJSON(pattern);
if (pat_obj.empty()) {
found_empty = true;
result.AppendErrorWithFormat(
"Found no matching plugins for pattern '%s'", pattern.data());
break;
}
for (auto &entry : pat_obj) {
obj[entry.first] = std::move(entry.second);
}
}
if (!found_empty) {
result.AppendMessage(ConvertJSONToPrettyString(std::move(obj)));
}
}
void OutputTextFormat(const std::vector<llvm::StringRef> &patterns,
CommandReturnObject &result) {
for (const llvm::StringRef pattern : patterns) {
int num_matching = ActOnMatchingPlugins(
pattern, [&](const PluginNamespace &plugin_namespace,
const std::vector<RegisteredPluginInfo> &plugins) {
result.AppendMessage(plugin_namespace.name);
for (auto &plugin : plugins) {
result.AppendMessageWithFormat(
" %s %-30s %s\n", plugin.enabled ? "[+]" : "[-]",
plugin.name.data(), plugin.description.data());
}
});
if (num_matching == 0) {
result.AppendErrorWithFormat(
"Found no matching plugins for pattern '%s'", pattern.data());
break;
}
}
}
PluginListCommandOptions m_options;
};
static void DoPluginEnableDisable(Args &command, CommandReturnObject &result,
bool enable) {
const char *name = enable ? "enable" : "disable";
size_t argc = command.GetArgumentCount();
if (argc == 0) {
result.AppendErrorWithFormat("'plugin %s' requires one or more arguments",
name);
return;
}
result.SetStatus(eReturnStatusSuccessFinishResult);
for (size_t i = 0; i < argc; ++i) {
llvm::StringRef pattern = command[i].ref();
int num_matching = SetEnableOnMatchingPlugins(pattern, result, enable);
if (num_matching == 0) {
result.AppendErrorWithFormat(
"Found no matching plugins to %s for pattern '%s'", name,
pattern.data());
break;
}
}
}
class CommandObjectPluginEnable : public CommandObjectParsed {
public:
CommandObjectPluginEnable(CommandInterpreter &interpreter)
: CommandObjectParsed(interpreter, "plugin enable",
"Enable registered LLDB plugins.", nullptr) {
AddSimpleArgumentList(eArgTypeManagedPlugin);
}
~CommandObjectPluginEnable() override = default;
protected:
void DoExecute(Args &command, CommandReturnObject &result) override {
DoPluginEnableDisable(command, result, /*enable=*/true);
}
};
class CommandObjectPluginDisable : public CommandObjectParsed {
public:
CommandObjectPluginDisable(CommandInterpreter &interpreter)
: CommandObjectParsed(interpreter, "plugin disable",
"Disable registered LLDB plugins.", nullptr) {
AddSimpleArgumentList(eArgTypeManagedPlugin);
}
~CommandObjectPluginDisable() override = default;
protected:
void DoExecute(Args &command, CommandReturnObject &result) override {
DoPluginEnableDisable(command, result, /*enable=*/false);
}
};
CommandObjectPlugin::CommandObjectPlugin(CommandInterpreter &interpreter)
: CommandObjectMultiword(interpreter, "plugin",
"Commands for managing LLDB plugins.",
"plugin <subcommand> [<subcommand-options>]") {
LoadSubCommand("load",
CommandObjectSP(new CommandObjectPluginLoad(interpreter)));
LoadSubCommand("list",
CommandObjectSP(new CommandObjectPluginList(interpreter)));
LoadSubCommand("enable",
CommandObjectSP(new CommandObjectPluginEnable(interpreter)));
LoadSubCommand("disable",
CommandObjectSP(new CommandObjectPluginDisable(interpreter)));
}
CommandObjectPlugin::~CommandObjectPlugin() = default;
|