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