1 //===-- CommandObjectScript.cpp -------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
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/CommandOptionArgumentTable.h"
16 #include "lldb/Interpreter/CommandReturnObject.h"
17 #include "lldb/Interpreter/OptionArgParser.h"
18 #include "lldb/Interpreter/ScriptInterpreter.h"
19 #include "lldb/Utility/Args.h"
22 using namespace lldb_private
;
24 #define LLDB_OPTIONS_script
25 #include "CommandOptions.inc"
27 Status
CommandObjectScript::CommandOptions::SetOptionValue(
28 uint32_t option_idx
, llvm::StringRef option_arg
,
29 ExecutionContext
*execution_context
) {
31 const int short_option
= m_getopt_table
[option_idx
].val
;
33 switch (short_option
) {
35 language
= (lldb::ScriptLanguage
)OptionArgParser::ToOptionEnum(
36 option_arg
, GetDefinitions()[option_idx
].enum_values
,
37 eScriptLanguageNone
, error
);
39 error
.SetErrorStringWithFormat("unrecognized value for language '%s'",
40 option_arg
.str().c_str());
43 llvm_unreachable("Unimplemented option");
49 void CommandObjectScript::CommandOptions::OptionParsingStarting(
50 ExecutionContext
*execution_context
) {
51 language
= lldb::eScriptLanguageNone
;
54 llvm::ArrayRef
<OptionDefinition
>
55 CommandObjectScript::CommandOptions::GetDefinitions() {
56 return llvm::ArrayRef(g_script_options
);
59 CommandObjectScript::CommandObjectScript(CommandInterpreter
&interpreter
)
61 interpreter
, "script",
62 "Invoke the script interpreter with provided code and display any "
63 "results. Start the interactive interpreter if no code is supplied.",
64 "script [--language <scripting-language> --] [<script-code>]") {}
66 CommandObjectScript::~CommandObjectScript() = default;
68 void CommandObjectScript::DoExecute(llvm::StringRef command
,
69 CommandReturnObject
&result
) {
70 // Try parsing the language option but when the command contains a raw part
71 // separated by the -- delimiter.
72 OptionsWithRaw
raw_args(command
);
73 if (raw_args
.HasArgs()) {
74 if (!ParseOptions(raw_args
.GetArgs(), result
))
76 command
= raw_args
.GetRawPart();
79 lldb::ScriptLanguage language
=
80 (m_options
.language
== lldb::eScriptLanguageNone
)
81 ? m_interpreter
.GetDebugger().GetScriptLanguage()
84 if (language
== lldb::eScriptLanguageNone
) {
86 "the script-lang setting is set to none - scripting not available");
90 ScriptInterpreter
*script_interpreter
=
91 GetDebugger().GetScriptInterpreter(true, language
);
93 if (script_interpreter
== nullptr) {
94 result
.AppendError("no script interpreter");
98 // Script might change Python code we use for formatting. Make sure we keep
99 // up to date with it.
100 DataVisualization::ForceUpdate();
102 if (command
.empty()) {
103 script_interpreter
->ExecuteInterpreterLoop();
104 result
.SetStatus(eReturnStatusSuccessFinishNoResult
);
108 // We can do better when reporting the status of one-liner script execution.
109 if (script_interpreter
->ExecuteOneLine(command
, &result
))
110 result
.SetStatus(eReturnStatusSuccessFinishNoResult
);
112 result
.SetStatus(eReturnStatusFailed
);