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