Path: blob/main/contrib/llvm-project/lldb/source/Commands/CommandObjectScripting.cpp
39587 views
//===-- CommandObjectScripting.cpp ----------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "CommandObjectScripting.h"9#include "lldb/Core/Debugger.h"10#include "lldb/DataFormatters/DataVisualization.h"11#include "lldb/Host/Config.h"12#include "lldb/Host/OptionParser.h"13#include "lldb/Interpreter/CommandInterpreter.h"14#include "lldb/Interpreter/CommandOptionArgumentTable.h"15#include "lldb/Interpreter/CommandReturnObject.h"16#include "lldb/Interpreter/OptionArgParser.h"17#include "lldb/Interpreter/ScriptInterpreter.h"18#include "lldb/Utility/Args.h"1920using namespace lldb;21using namespace lldb_private;2223#define LLDB_OPTIONS_scripting_run24#include "CommandOptions.inc"2526class CommandObjectScriptingRun : public CommandObjectRaw {27public:28CommandObjectScriptingRun(CommandInterpreter &interpreter)29: CommandObjectRaw(30interpreter, "scripting run",31"Invoke the script interpreter with provided code and display any "32"results. Start the interactive interpreter if no code is "33"supplied.",34"scripting run [--language <scripting-language> --] "35"[<script-code>]") {}3637~CommandObjectScriptingRun() override = default;3839Options *GetOptions() override { return &m_options; }4041class CommandOptions : public Options {42public:43CommandOptions() = default;44~CommandOptions() override = default;45Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,46ExecutionContext *execution_context) override {47Status error;48const int short_option = m_getopt_table[option_idx].val;4950switch (short_option) {51case 'l':52language = (lldb::ScriptLanguage)OptionArgParser::ToOptionEnum(53option_arg, GetDefinitions()[option_idx].enum_values,54eScriptLanguageNone, error);55if (!error.Success())56error.SetErrorStringWithFormat("unrecognized value for language '%s'",57option_arg.str().c_str());58break;59default:60llvm_unreachable("Unimplemented option");61}6263return error;64}6566void OptionParsingStarting(ExecutionContext *execution_context) override {67language = lldb::eScriptLanguageNone;68}6970llvm::ArrayRef<OptionDefinition> GetDefinitions() override {71return llvm::ArrayRef(g_scripting_run_options);72}7374lldb::ScriptLanguage language = lldb::eScriptLanguageNone;75};7677protected:78void DoExecute(llvm::StringRef command,79CommandReturnObject &result) override {80// Try parsing the language option but when the command contains a raw part81// separated by the -- delimiter.82OptionsWithRaw raw_args(command);83if (raw_args.HasArgs()) {84if (!ParseOptions(raw_args.GetArgs(), result))85return;86command = raw_args.GetRawPart();87}8889lldb::ScriptLanguage language =90(m_options.language == lldb::eScriptLanguageNone)91? m_interpreter.GetDebugger().GetScriptLanguage()92: m_options.language;9394if (language == lldb::eScriptLanguageNone) {95result.AppendError(96"the script-lang setting is set to none - scripting not available");97return;98}99100ScriptInterpreter *script_interpreter =101GetDebugger().GetScriptInterpreter(true, language);102103if (script_interpreter == nullptr) {104result.AppendError("no script interpreter");105return;106}107108// Script might change Python code we use for formatting. Make sure we keep109// up to date with it.110DataVisualization::ForceUpdate();111112if (command.empty()) {113script_interpreter->ExecuteInterpreterLoop();114result.SetStatus(eReturnStatusSuccessFinishNoResult);115return;116}117118// We can do better when reporting the status of one-liner script execution.119if (script_interpreter->ExecuteOneLine(command, &result))120result.SetStatus(eReturnStatusSuccessFinishNoResult);121else122result.SetStatus(eReturnStatusFailed);123}124125private:126CommandOptions m_options;127};128129#pragma mark CommandObjectMultiwordScripting130131// CommandObjectMultiwordScripting132133CommandObjectMultiwordScripting::CommandObjectMultiwordScripting(134CommandInterpreter &interpreter)135: CommandObjectMultiword(136interpreter, "scripting",137"Commands for operating on the scripting functionnalities.",138"scripting <subcommand> [<subcommand-options>]") {139LoadSubCommand("run",140CommandObjectSP(new CommandObjectScriptingRun(interpreter)));141}142143CommandObjectMultiwordScripting::~CommandObjectMultiwordScripting() = default;144145146