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