xref: /freebsd/contrib/llvm-project/lldb/source/Commands/CommandObjectPlugin.cpp (revision 5ffd83dbcc34f10e07f6d3e968ae6365869615f4)
1*5ffd83dbSDimitry Andric //===-- CommandObjectPlugin.cpp -------------------------------------------===//
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/Interpreter/CommandInterpreter.h"
110b57cec5SDimitry Andric #include "lldb/Interpreter/CommandReturnObject.h"
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric using namespace lldb;
140b57cec5SDimitry Andric using namespace lldb_private;
150b57cec5SDimitry Andric 
160b57cec5SDimitry Andric class CommandObjectPluginLoad : public CommandObjectParsed {
170b57cec5SDimitry Andric public:
180b57cec5SDimitry Andric   CommandObjectPluginLoad(CommandInterpreter &interpreter)
190b57cec5SDimitry Andric       : CommandObjectParsed(interpreter, "plugin load",
200b57cec5SDimitry Andric                             "Import a dylib that implements an LLDB plugin.",
210b57cec5SDimitry Andric                             nullptr) {
220b57cec5SDimitry Andric     CommandArgumentEntry arg1;
230b57cec5SDimitry Andric     CommandArgumentData cmd_arg;
240b57cec5SDimitry Andric 
250b57cec5SDimitry Andric     // Define the first (and only) variant of this arg.
260b57cec5SDimitry Andric     cmd_arg.arg_type = eArgTypeFilename;
270b57cec5SDimitry Andric     cmd_arg.arg_repetition = eArgRepeatPlain;
280b57cec5SDimitry Andric 
290b57cec5SDimitry Andric     // There is only one variant this argument could be; put it into the
300b57cec5SDimitry Andric     // argument entry.
310b57cec5SDimitry Andric     arg1.push_back(cmd_arg);
320b57cec5SDimitry Andric 
330b57cec5SDimitry Andric     // Push the data for the first argument into the m_arguments vector.
340b57cec5SDimitry Andric     m_arguments.push_back(arg1);
350b57cec5SDimitry Andric   }
360b57cec5SDimitry Andric 
370b57cec5SDimitry Andric   ~CommandObjectPluginLoad() override = default;
380b57cec5SDimitry Andric 
399dba64beSDimitry Andric   void
409dba64beSDimitry Andric   HandleArgumentCompletion(CompletionRequest &request,
410b57cec5SDimitry Andric                            OptionElementVector &opt_element_vector) override {
420b57cec5SDimitry Andric     CommandCompletions::InvokeCommonCompletionCallbacks(
430b57cec5SDimitry Andric         GetCommandInterpreter(), CommandCompletions::eDiskFileCompletion,
440b57cec5SDimitry Andric         request, nullptr);
450b57cec5SDimitry Andric   }
460b57cec5SDimitry Andric 
470b57cec5SDimitry Andric protected:
480b57cec5SDimitry Andric   bool DoExecute(Args &command, CommandReturnObject &result) override {
490b57cec5SDimitry Andric     size_t argc = command.GetArgumentCount();
500b57cec5SDimitry Andric 
510b57cec5SDimitry Andric     if (argc != 1) {
520b57cec5SDimitry Andric       result.AppendError("'plugin load' requires one argument");
530b57cec5SDimitry Andric       result.SetStatus(eReturnStatusFailed);
540b57cec5SDimitry Andric       return false;
550b57cec5SDimitry Andric     }
560b57cec5SDimitry Andric 
570b57cec5SDimitry Andric     Status error;
580b57cec5SDimitry Andric 
599dba64beSDimitry Andric     FileSpec dylib_fspec(command[0].ref());
600b57cec5SDimitry Andric     FileSystem::Instance().Resolve(dylib_fspec);
610b57cec5SDimitry Andric 
620b57cec5SDimitry Andric     if (GetDebugger().LoadPlugin(dylib_fspec, error))
630b57cec5SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishResult);
640b57cec5SDimitry Andric     else {
650b57cec5SDimitry Andric       result.AppendError(error.AsCString());
660b57cec5SDimitry Andric       result.SetStatus(eReturnStatusFailed);
670b57cec5SDimitry Andric     }
680b57cec5SDimitry Andric 
690b57cec5SDimitry Andric     return result.Succeeded();
700b57cec5SDimitry Andric   }
710b57cec5SDimitry Andric };
720b57cec5SDimitry Andric 
730b57cec5SDimitry Andric CommandObjectPlugin::CommandObjectPlugin(CommandInterpreter &interpreter)
740b57cec5SDimitry Andric     : CommandObjectMultiword(interpreter, "plugin",
750b57cec5SDimitry Andric                              "Commands for managing LLDB plugins.",
760b57cec5SDimitry Andric                              "plugin <subcommand> [<subcommand-options>]") {
770b57cec5SDimitry Andric   LoadSubCommand("load",
780b57cec5SDimitry Andric                  CommandObjectSP(new CommandObjectPluginLoad(interpreter)));
790b57cec5SDimitry Andric }
800b57cec5SDimitry Andric 
810b57cec5SDimitry Andric CommandObjectPlugin::~CommandObjectPlugin() = default;
82