xref: /llvm-project/lldb/source/Commands/CommandObjectPlugin.cpp (revision e1cfbc79420fee0b71bad62f8d413b68a0eca91e)
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 {
24 public:
25     CommandObjectPluginLoad (CommandInterpreter &interpreter) :
26         CommandObjectParsed(interpreter,
27                             "plugin load",
28                             "Import a dylib that implements an LLDB plugin.",
29                             nullptr)
30     {
31         CommandArgumentEntry arg1;
32         CommandArgumentData cmd_arg;
33 
34         // Define the first (and only) variant of this arg.
35         cmd_arg.arg_type = eArgTypeFilename;
36         cmd_arg.arg_repetition = eArgRepeatPlain;
37 
38         // There is only one variant this argument could be; put it into the argument entry.
39         arg1.push_back (cmd_arg);
40 
41         // Push the data for the first argument into the m_arguments vector.
42         m_arguments.push_back (arg1);
43     }
44 
45     ~CommandObjectPluginLoad() override = default;
46 
47     int
48     HandleArgumentCompletion (Args &input,
49                               int &cursor_index,
50                               int &cursor_char_position,
51                               OptionElementVector &opt_element_vector,
52                               int match_start_point,
53                               int max_return_elements,
54                               bool &word_complete,
55                               StringList &matches) override
56     {
57         std::string completion_str (input.GetArgumentAtIndex(cursor_index));
58         completion_str.erase (cursor_char_position);
59 
60         CommandCompletions::InvokeCommonCompletionCallbacks(GetCommandInterpreter(),
61                                                             CommandCompletions::eDiskFileCompletion,
62                                                             completion_str.c_str(),
63                                                             match_start_point,
64                                                             max_return_elements,
65                                                             nullptr,
66                                                             word_complete,
67                                                             matches);
68         return matches.GetSize();
69     }
70 
71 protected:
72     bool
73     DoExecute (Args& command, CommandReturnObject &result) override
74     {
75         size_t argc = command.GetArgumentCount();
76 
77         if (argc != 1)
78         {
79             result.AppendError ("'plugin load' requires one argument");
80             result.SetStatus (eReturnStatusFailed);
81             return false;
82         }
83 
84         const char* path = command.GetArgumentAtIndex(0);
85 
86         Error error;
87 
88         FileSpec dylib_fspec(path,true);
89 
90         if (m_interpreter.GetDebugger().LoadPlugin(dylib_fspec, error))
91             result.SetStatus(eReturnStatusSuccessFinishResult);
92         else
93         {
94             result.AppendError(error.AsCString());
95             result.SetStatus(eReturnStatusFailed);
96         }
97 
98         return result.Succeeded();
99     }
100 };
101 
102 CommandObjectPlugin::CommandObjectPlugin(CommandInterpreter &interpreter)
103     : CommandObjectMultiword(interpreter, "plugin", "Commands for managing LLDB plugins.",
104                              "plugin <subcommand> [<subcommand-options>]")
105 {
106     LoadSubCommand ("load",  CommandObjectSP (new CommandObjectPluginLoad (interpreter)));
107 }
108 
109 CommandObjectPlugin::~CommandObjectPlugin() = default;
110