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