xref: /llvm-project/lldb/source/Commands/CommandObjectPlugin.cpp (revision b9c1b51e45b845debb76d8658edabca70ca56079)
1 //===-- CommandObjectPlugin.cpp ---------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // C Includes
11 // C++ Includes
12 // Other libraries and framework includes
13 // Project includes
14 #include "CommandObjectPlugin.h"
15 #include "lldb/Host/Host.h"
16 #include "lldb/Interpreter/CommandInterpreter.h"
17 #include "lldb/Interpreter/CommandReturnObject.h"
18 
19 using namespace lldb;
20 using namespace lldb_private;
21 
22 class CommandObjectPluginLoad : public CommandObjectParsed {
23 public:
24   CommandObjectPluginLoad(CommandInterpreter &interpreter)
25       : CommandObjectParsed(interpreter, "plugin load",
26                             "Import a dylib that implements an LLDB plugin.",
27                             nullptr) {
28     CommandArgumentEntry arg1;
29     CommandArgumentData cmd_arg;
30 
31     // Define the first (and only) variant of this arg.
32     cmd_arg.arg_type = eArgTypeFilename;
33     cmd_arg.arg_repetition = eArgRepeatPlain;
34 
35     // There is only one variant this argument could be; put it into the
36     // argument entry.
37     arg1.push_back(cmd_arg);
38 
39     // Push the data for the first argument into the m_arguments vector.
40     m_arguments.push_back(arg1);
41   }
42 
43   ~CommandObjectPluginLoad() override = default;
44 
45   int HandleArgumentCompletion(Args &input, int &cursor_index,
46                                int &cursor_char_position,
47                                OptionElementVector &opt_element_vector,
48                                int match_start_point, int max_return_elements,
49                                bool &word_complete,
50                                StringList &matches) override {
51     std::string completion_str(input.GetArgumentAtIndex(cursor_index));
52     completion_str.erase(cursor_char_position);
53 
54     CommandCompletions::InvokeCommonCompletionCallbacks(
55         GetCommandInterpreter(), CommandCompletions::eDiskFileCompletion,
56         completion_str.c_str(), match_start_point, max_return_elements, nullptr,
57         word_complete, matches);
58     return matches.GetSize();
59   }
60 
61 protected:
62   bool DoExecute(Args &command, CommandReturnObject &result) override {
63     size_t argc = command.GetArgumentCount();
64 
65     if (argc != 1) {
66       result.AppendError("'plugin load' requires one argument");
67       result.SetStatus(eReturnStatusFailed);
68       return false;
69     }
70 
71     const char *path = command.GetArgumentAtIndex(0);
72 
73     Error error;
74 
75     FileSpec dylib_fspec(path, true);
76 
77     if (m_interpreter.GetDebugger().LoadPlugin(dylib_fspec, error))
78       result.SetStatus(eReturnStatusSuccessFinishResult);
79     else {
80       result.AppendError(error.AsCString());
81       result.SetStatus(eReturnStatusFailed);
82     }
83 
84     return result.Succeeded();
85   }
86 };
87 
88 CommandObjectPlugin::CommandObjectPlugin(CommandInterpreter &interpreter)
89     : CommandObjectMultiword(interpreter, "plugin",
90                              "Commands for managing LLDB plugins.",
91                              "plugin <subcommand> [<subcommand-options>]") {
92   LoadSubCommand("load",
93                  CommandObjectSP(new CommandObjectPluginLoad(interpreter)));
94 }
95 
96 CommandObjectPlugin::~CommandObjectPlugin() = default;
97