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