xref: /freebsd/contrib/llvm-project/lldb/source/Commands/CommandObjectPlugin.cpp (revision 9dba64be9536c28e4800e06512b7f29b43ade345)
10b57cec5SDimitry Andric //===-- CommandObjectPlugin.cpp ---------------------------------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "CommandObjectPlugin.h"
100b57cec5SDimitry Andric #include "lldb/Host/Host.h"
110b57cec5SDimitry Andric #include "lldb/Interpreter/CommandInterpreter.h"
120b57cec5SDimitry Andric #include "lldb/Interpreter/CommandReturnObject.h"
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric using namespace lldb;
150b57cec5SDimitry Andric using namespace lldb_private;
160b57cec5SDimitry Andric 
170b57cec5SDimitry Andric class CommandObjectPluginLoad : public CommandObjectParsed {
180b57cec5SDimitry Andric public:
190b57cec5SDimitry Andric   CommandObjectPluginLoad(CommandInterpreter &interpreter)
200b57cec5SDimitry Andric       : CommandObjectParsed(interpreter, "plugin load",
210b57cec5SDimitry Andric                             "Import a dylib that implements an LLDB plugin.",
220b57cec5SDimitry Andric                             nullptr) {
230b57cec5SDimitry Andric     CommandArgumentEntry arg1;
240b57cec5SDimitry Andric     CommandArgumentData cmd_arg;
250b57cec5SDimitry Andric 
260b57cec5SDimitry Andric     // Define the first (and only) variant of this arg.
270b57cec5SDimitry Andric     cmd_arg.arg_type = eArgTypeFilename;
280b57cec5SDimitry Andric     cmd_arg.arg_repetition = eArgRepeatPlain;
290b57cec5SDimitry Andric 
300b57cec5SDimitry Andric     // There is only one variant this argument could be; put it into the
310b57cec5SDimitry Andric     // argument entry.
320b57cec5SDimitry Andric     arg1.push_back(cmd_arg);
330b57cec5SDimitry Andric 
340b57cec5SDimitry Andric     // Push the data for the first argument into the m_arguments vector.
350b57cec5SDimitry Andric     m_arguments.push_back(arg1);
360b57cec5SDimitry Andric   }
370b57cec5SDimitry Andric 
380b57cec5SDimitry Andric   ~CommandObjectPluginLoad() override = default;
390b57cec5SDimitry Andric 
40*9dba64beSDimitry Andric   void
41*9dba64beSDimitry Andric   HandleArgumentCompletion(CompletionRequest &request,
420b57cec5SDimitry Andric                            OptionElementVector &opt_element_vector) override {
430b57cec5SDimitry Andric     CommandCompletions::InvokeCommonCompletionCallbacks(
440b57cec5SDimitry Andric         GetCommandInterpreter(), CommandCompletions::eDiskFileCompletion,
450b57cec5SDimitry Andric         request, nullptr);
460b57cec5SDimitry Andric   }
470b57cec5SDimitry Andric 
480b57cec5SDimitry Andric protected:
490b57cec5SDimitry Andric   bool DoExecute(Args &command, CommandReturnObject &result) override {
500b57cec5SDimitry Andric     size_t argc = command.GetArgumentCount();
510b57cec5SDimitry Andric 
520b57cec5SDimitry Andric     if (argc != 1) {
530b57cec5SDimitry Andric       result.AppendError("'plugin load' requires one argument");
540b57cec5SDimitry Andric       result.SetStatus(eReturnStatusFailed);
550b57cec5SDimitry Andric       return false;
560b57cec5SDimitry Andric     }
570b57cec5SDimitry Andric 
580b57cec5SDimitry Andric     Status error;
590b57cec5SDimitry Andric 
60*9dba64beSDimitry Andric     FileSpec dylib_fspec(command[0].ref());
610b57cec5SDimitry Andric     FileSystem::Instance().Resolve(dylib_fspec);
620b57cec5SDimitry Andric 
630b57cec5SDimitry Andric     if (GetDebugger().LoadPlugin(dylib_fspec, error))
640b57cec5SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishResult);
650b57cec5SDimitry Andric     else {
660b57cec5SDimitry Andric       result.AppendError(error.AsCString());
670b57cec5SDimitry Andric       result.SetStatus(eReturnStatusFailed);
680b57cec5SDimitry Andric     }
690b57cec5SDimitry Andric 
700b57cec5SDimitry Andric     return result.Succeeded();
710b57cec5SDimitry Andric   }
720b57cec5SDimitry Andric };
730b57cec5SDimitry Andric 
740b57cec5SDimitry Andric CommandObjectPlugin::CommandObjectPlugin(CommandInterpreter &interpreter)
750b57cec5SDimitry Andric     : CommandObjectMultiword(interpreter, "plugin",
760b57cec5SDimitry Andric                              "Commands for managing LLDB plugins.",
770b57cec5SDimitry Andric                              "plugin <subcommand> [<subcommand-options>]") {
780b57cec5SDimitry Andric   LoadSubCommand("load",
790b57cec5SDimitry Andric                  CommandObjectSP(new CommandObjectPluginLoad(interpreter)));
800b57cec5SDimitry Andric }
810b57cec5SDimitry Andric 
820b57cec5SDimitry Andric CommandObjectPlugin::~CommandObjectPlugin() = default;
83