1*dda28197Spatrick //===-- CommandObjectPlugin.cpp -------------------------------------------===//
2061da546Spatrick //
3061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4061da546Spatrick // See https://llvm.org/LICENSE.txt for license information.
5061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6061da546Spatrick //
7061da546Spatrick //===----------------------------------------------------------------------===//
8061da546Spatrick 
9061da546Spatrick #include "CommandObjectPlugin.h"
10061da546Spatrick #include "lldb/Interpreter/CommandInterpreter.h"
11061da546Spatrick #include "lldb/Interpreter/CommandReturnObject.h"
12061da546Spatrick 
13061da546Spatrick using namespace lldb;
14061da546Spatrick using namespace lldb_private;
15061da546Spatrick 
16061da546Spatrick class CommandObjectPluginLoad : public CommandObjectParsed {
17061da546Spatrick public:
CommandObjectPluginLoad(CommandInterpreter & interpreter)18061da546Spatrick   CommandObjectPluginLoad(CommandInterpreter &interpreter)
19061da546Spatrick       : CommandObjectParsed(interpreter, "plugin load",
20061da546Spatrick                             "Import a dylib that implements an LLDB plugin.",
21061da546Spatrick                             nullptr) {
22061da546Spatrick     CommandArgumentEntry arg1;
23061da546Spatrick     CommandArgumentData cmd_arg;
24061da546Spatrick 
25061da546Spatrick     // Define the first (and only) variant of this arg.
26061da546Spatrick     cmd_arg.arg_type = eArgTypeFilename;
27061da546Spatrick     cmd_arg.arg_repetition = eArgRepeatPlain;
28061da546Spatrick 
29061da546Spatrick     // There is only one variant this argument could be; put it into the
30061da546Spatrick     // argument entry.
31061da546Spatrick     arg1.push_back(cmd_arg);
32061da546Spatrick 
33061da546Spatrick     // Push the data for the first argument into the m_arguments vector.
34061da546Spatrick     m_arguments.push_back(arg1);
35061da546Spatrick   }
36061da546Spatrick 
37061da546Spatrick   ~CommandObjectPluginLoad() override = default;
38061da546Spatrick 
39061da546Spatrick   void
HandleArgumentCompletion(CompletionRequest & request,OptionElementVector & opt_element_vector)40061da546Spatrick   HandleArgumentCompletion(CompletionRequest &request,
41061da546Spatrick                            OptionElementVector &opt_element_vector) override {
42061da546Spatrick     CommandCompletions::InvokeCommonCompletionCallbacks(
43061da546Spatrick         GetCommandInterpreter(), CommandCompletions::eDiskFileCompletion,
44061da546Spatrick         request, nullptr);
45061da546Spatrick   }
46061da546Spatrick 
47061da546Spatrick protected:
DoExecute(Args & command,CommandReturnObject & result)48061da546Spatrick   bool DoExecute(Args &command, CommandReturnObject &result) override {
49061da546Spatrick     size_t argc = command.GetArgumentCount();
50061da546Spatrick 
51061da546Spatrick     if (argc != 1) {
52061da546Spatrick       result.AppendError("'plugin load' requires one argument");
53061da546Spatrick       return false;
54061da546Spatrick     }
55061da546Spatrick 
56061da546Spatrick     Status error;
57061da546Spatrick 
58061da546Spatrick     FileSpec dylib_fspec(command[0].ref());
59061da546Spatrick     FileSystem::Instance().Resolve(dylib_fspec);
60061da546Spatrick 
61061da546Spatrick     if (GetDebugger().LoadPlugin(dylib_fspec, error))
62061da546Spatrick       result.SetStatus(eReturnStatusSuccessFinishResult);
63061da546Spatrick     else {
64061da546Spatrick       result.AppendError(error.AsCString());
65061da546Spatrick     }
66061da546Spatrick 
67061da546Spatrick     return result.Succeeded();
68061da546Spatrick   }
69061da546Spatrick };
70061da546Spatrick 
CommandObjectPlugin(CommandInterpreter & interpreter)71061da546Spatrick CommandObjectPlugin::CommandObjectPlugin(CommandInterpreter &interpreter)
72061da546Spatrick     : CommandObjectMultiword(interpreter, "plugin",
73061da546Spatrick                              "Commands for managing LLDB plugins.",
74061da546Spatrick                              "plugin <subcommand> [<subcommand-options>]") {
75061da546Spatrick   LoadSubCommand("load",
76061da546Spatrick                  CommandObjectSP(new CommandObjectPluginLoad(interpreter)));
77061da546Spatrick }
78061da546Spatrick 
79061da546Spatrick CommandObjectPlugin::~CommandObjectPlugin() = default;
80