xref: /llvm-project/lldb/source/Commands/CommandObjectPlugin.cpp (revision ae34ed2c0d2fba36d8363ba7fffc1dbe18878335)
1 //===-- CommandObjectPlugin.cpp ---------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "CommandObjectPlugin.h"
10 #include "lldb/Host/Host.h"
11 #include "lldb/Interpreter/CommandInterpreter.h"
12 #include "lldb/Interpreter/CommandReturnObject.h"
13 
14 using namespace lldb;
15 using namespace lldb_private;
16 
17 class CommandObjectPluginLoad : public CommandObjectParsed {
18 public:
19   CommandObjectPluginLoad(CommandInterpreter &interpreter)
20       : CommandObjectParsed(interpreter, "plugin load",
21                             "Import a dylib that implements an LLDB plugin.",
22                             nullptr) {
23     CommandArgumentEntry arg1;
24     CommandArgumentData cmd_arg;
25 
26     // Define the first (and only) variant of this arg.
27     cmd_arg.arg_type = eArgTypeFilename;
28     cmd_arg.arg_repetition = eArgRepeatPlain;
29 
30     // There is only one variant this argument could be; put it into the
31     // argument entry.
32     arg1.push_back(cmd_arg);
33 
34     // Push the data for the first argument into the m_arguments vector.
35     m_arguments.push_back(arg1);
36   }
37 
38   ~CommandObjectPluginLoad() override = default;
39 
40   void
41   HandleArgumentCompletion(CompletionRequest &request,
42                            OptionElementVector &opt_element_vector) override {
43     CommandCompletions::InvokeCommonCompletionCallbacks(
44         GetCommandInterpreter(), CommandCompletions::eDiskFileCompletion,
45         request, nullptr);
46   }
47 
48 protected:
49   bool DoExecute(Args &command, CommandReturnObject &result) override {
50     size_t argc = command.GetArgumentCount();
51 
52     if (argc != 1) {
53       result.AppendError("'plugin load' requires one argument");
54       result.SetStatus(eReturnStatusFailed);
55       return false;
56     }
57 
58     Status error;
59 
60     FileSpec dylib_fspec(command[0].ref);
61     FileSystem::Instance().Resolve(dylib_fspec);
62 
63     if (GetDebugger().LoadPlugin(dylib_fspec, error))
64       result.SetStatus(eReturnStatusSuccessFinishResult);
65     else {
66       result.AppendError(error.AsCString());
67       result.SetStatus(eReturnStatusFailed);
68     }
69 
70     return result.Succeeded();
71   }
72 };
73 
74 CommandObjectPlugin::CommandObjectPlugin(CommandInterpreter &interpreter)
75     : CommandObjectMultiword(interpreter, "plugin",
76                              "Commands for managing LLDB plugins.",
77                              "plugin <subcommand> [<subcommand-options>]") {
78   LoadSubCommand("load",
79                  CommandObjectSP(new CommandObjectPluginLoad(interpreter)));
80 }
81 
82 CommandObjectPlugin::~CommandObjectPlugin() = default;
83