1 //===-- CommandObjectScript.cpp -------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "CommandObjectScript.h" 10 #include "lldb/Core/Debugger.h" 11 #include "lldb/DataFormatters/DataVisualization.h" 12 #include "lldb/Host/Config.h" 13 #include "lldb/Host/OptionParser.h" 14 #include "lldb/Interpreter/CommandInterpreter.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" 19 20 using namespace lldb; 21 using namespace lldb_private; 22 23 static constexpr OptionEnumValueElement g_script_option_enumeration[] = { 24 { 25 eScriptLanguagePython, 26 "python", 27 "Python", 28 }, 29 { 30 eScriptLanguageLua, 31 "lua", 32 "Lua", 33 }, 34 { 35 eScriptLanguageNone, 36 "default", 37 "The default scripting language.", 38 }, 39 }; 40 41 static constexpr OptionEnumValues ScriptOptionEnum() { 42 return OptionEnumValues(g_script_option_enumeration); 43 } 44 45 #define LLDB_OPTIONS_script 46 #include "CommandOptions.inc" 47 48 Status CommandObjectScript::CommandOptions::SetOptionValue( 49 uint32_t option_idx, llvm::StringRef option_arg, 50 ExecutionContext *execution_context) { 51 Status error; 52 const int short_option = m_getopt_table[option_idx].val; 53 54 switch (short_option) { 55 case 'l': 56 language = (lldb::ScriptLanguage)OptionArgParser::ToOptionEnum( 57 option_arg, GetDefinitions()[option_idx].enum_values, 58 eScriptLanguageNone, error); 59 if (!error.Success()) 60 error.SetErrorStringWithFormat("unrecognized value for language '%s'", 61 option_arg.str().c_str()); 62 break; 63 default: 64 llvm_unreachable("Unimplemented option"); 65 } 66 67 return error; 68 } 69 70 void CommandObjectScript::CommandOptions::OptionParsingStarting( 71 ExecutionContext *execution_context) { 72 language = lldb::eScriptLanguageNone; 73 } 74 75 llvm::ArrayRef<OptionDefinition> 76 CommandObjectScript::CommandOptions::GetDefinitions() { 77 return llvm::makeArrayRef(g_script_options); 78 } 79 80 CommandObjectScript::CommandObjectScript(CommandInterpreter &interpreter) 81 : CommandObjectRaw( 82 interpreter, "script", 83 "Invoke the script interpreter with provided code and display any " 84 "results. Start the interactive interpreter if no code is supplied.", 85 "script [--language <scripting-language> --] [<script-code>]") {} 86 87 CommandObjectScript::~CommandObjectScript() = default; 88 89 bool CommandObjectScript::DoExecute(llvm::StringRef command, 90 CommandReturnObject &result) { 91 // Try parsing the language option but when the command contains a raw part 92 // separated by the -- delimiter. 93 OptionsWithRaw raw_args(command); 94 if (raw_args.HasArgs()) { 95 if (!ParseOptions(raw_args.GetArgs(), result)) 96 return false; 97 command = raw_args.GetRawPart(); 98 } 99 100 lldb::ScriptLanguage language = 101 (m_options.language == lldb::eScriptLanguageNone) 102 ? m_interpreter.GetDebugger().GetScriptLanguage() 103 : m_options.language; 104 105 if (language == lldb::eScriptLanguageNone) { 106 result.AppendError( 107 "the script-lang setting is set to none - scripting not available"); 108 return false; 109 } 110 111 ScriptInterpreter *script_interpreter = 112 GetDebugger().GetScriptInterpreter(true, language); 113 114 if (script_interpreter == nullptr) { 115 result.AppendError("no script interpreter"); 116 return false; 117 } 118 119 // Script might change Python code we use for formatting. Make sure we keep 120 // up to date with it. 121 DataVisualization::ForceUpdate(); 122 123 if (command.empty()) { 124 script_interpreter->ExecuteInterpreterLoop(); 125 result.SetStatus(eReturnStatusSuccessFinishNoResult); 126 return result.Succeeded(); 127 } 128 129 // We can do better when reporting the status of one-liner script execution. 130 if (script_interpreter->ExecuteOneLine(command, &result)) 131 result.SetStatus(eReturnStatusSuccessFinishNoResult); 132 else 133 result.SetStatus(eReturnStatusFailed); 134 135 return result.Succeeded(); 136 } 137