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
ScriptOptionEnum()41 static constexpr OptionEnumValues ScriptOptionEnum() {
42 return OptionEnumValues(g_script_option_enumeration);
43 }
44
45 #define LLDB_OPTIONS_script
46 #include "CommandOptions.inc"
47
SetOptionValue(uint32_t option_idx,llvm::StringRef option_arg,ExecutionContext * execution_context)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
OptionParsingStarting(ExecutionContext * execution_context)70 void CommandObjectScript::CommandOptions::OptionParsingStarting(
71 ExecutionContext *execution_context) {
72 language = lldb::eScriptLanguageNone;
73 }
74
75 llvm::ArrayRef<OptionDefinition>
GetDefinitions()76 CommandObjectScript::CommandOptions::GetDefinitions() {
77 return llvm::makeArrayRef(g_script_options);
78 }
79
CommandObjectScript(CommandInterpreter & interpreter)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
~CommandObjectScript()87 CommandObjectScript::~CommandObjectScript() {}
88
DoExecute(llvm::StringRef command,CommandReturnObject & result)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 result.SetStatus(eReturnStatusFailed);
109 return false;
110 }
111
112 ScriptInterpreter *script_interpreter =
113 GetDebugger().GetScriptInterpreter(true, language);
114
115 if (script_interpreter == nullptr) {
116 result.AppendError("no script interpreter");
117 result.SetStatus(eReturnStatusFailed);
118 return false;
119 }
120
121 // Script might change Python code we use for formatting. Make sure we keep
122 // up to date with it.
123 DataVisualization::ForceUpdate();
124
125 if (command.empty()) {
126 script_interpreter->ExecuteInterpreterLoop();
127 result.SetStatus(eReturnStatusSuccessFinishNoResult);
128 return result.Succeeded();
129 }
130
131 // We can do better when reporting the status of one-liner script execution.
132 if (script_interpreter->ExecuteOneLine(command, &result))
133 result.SetStatus(eReturnStatusSuccessFinishNoResult);
134 else
135 result.SetStatus(eReturnStatusFailed);
136
137 return result.Succeeded();
138 }
139