LLDB  mainline
CommandObjectScript.cpp
Go to the documentation of this file.
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"
12 #include "lldb/Host/Config.h"
13 #include "lldb/Host/OptionParser.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  {
26  "python",
27  "Python",
28  },
29  {
31  "lua",
32  "Lua",
33  },
34  {
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,
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)
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 
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 =
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.
122 
123  if (command.empty()) {
124  script_interpreter->ExecuteInterpreterLoop();
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))
132  else
134 
135  return result.Succeeded();
136 }
lldb_private::ExecutionContext
Definition: ExecutionContext.h:292
lldb::ScriptLanguage
ScriptLanguage
Script interpreter types.
Definition: lldb-enumerations.h:216
lldb_private::CommandObject::ParseOptions
bool ParseOptions(Args &args, CommandReturnObject &result)
Definition: CommandObject.cpp:104
lldb_private::OptionsWithRaw::GetArgs
Args & GetArgs()
Returns the list of arguments.
Definition: Args.h:335
lldb_private::CommandObjectScript::CommandOptions::language
lldb::ScriptLanguage language
Definition: CommandObjectScript.h:30
lldb_private::OptionsWithRaw::GetRawPart
const std::string & GetRawPart() const
Returns the raw suffix part of the parsed string.
Definition: Args.h:368
lldb_private::CommandObjectScript::DoExecute
bool DoExecute(llvm::StringRef command, CommandReturnObject &result) override
Definition: CommandObjectScript.cpp:89
OptionArgParser.h
g_script_option_enumeration
static constexpr OptionEnumValueElement g_script_option_enumeration[]
Definition: CommandObjectScript.cpp:23
lldb_private::CommandObjectScript::m_options
CommandOptions m_options
Definition: CommandObjectScript.h:37
lldb_private::Debugger::GetScriptLanguage
lldb::ScriptLanguage GetScriptLanguage() const
Definition: Debugger.cpp:306
lldb_private::DataVisualization::ForceUpdate
static void ForceUpdate()
Definition: DataVisualization.cpp:20
CommandReturnObject.h
Debugger.h
lldb_private::Debugger::GetScriptInterpreter
ScriptInterpreter * GetScriptInterpreter(bool can_create=true, llvm::Optional< lldb::ScriptLanguage > language={})
Definition: Debugger.cpp:1275
lldb_private::ScriptInterpreter::ExecuteOneLine
virtual bool ExecuteOneLine(llvm::StringRef command, CommandReturnObject *result, const ExecuteScriptOptions &options=ExecuteScriptOptions())=0
lldb::eScriptLanguageNone
@ eScriptLanguageNone
Definition: lldb-enumerations.h:217
lldb::eReturnStatusSuccessFinishNoResult
@ eReturnStatusSuccessFinishNoResult
Definition: lldb-enumerations.h:260
lldb_private::CommandObjectScript::~CommandObjectScript
~CommandObjectScript() override
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
Args.h
lldb_private::OptionsWithRaw::HasArgs
bool HasArgs() const
Returns true if there are any arguments before the raw suffix.
Definition: Args.h:330
lldb_private::CommandReturnObject::SetStatus
void SetStatus(lldb::ReturnStatus status)
Definition: CommandReturnObject.cpp:121
ScriptOptionEnum
static constexpr OptionEnumValues ScriptOptionEnum()
Definition: CommandObjectScript.cpp:41
lldb::eScriptLanguagePython
@ eScriptLanguagePython
Definition: lldb-enumerations.h:218
lldb_private::ScriptInterpreter::ExecuteInterpreterLoop
virtual void ExecuteInterpreterLoop()=0
lldb_private::CommandInterpreter
Definition: CommandInterpreter.h:214
lldb::eReturnStatusFailed
@ eReturnStatusFailed
Definition: lldb-enumerations.h:265
lldb_private::CommandObject::m_interpreter
CommandInterpreter & m_interpreter
Definition: CommandObject.h:373
lldb_private::CommandObjectRaw
Definition: CommandObject.h:411
lldb_private::Status
Definition: Status.h:44
DataVisualization.h
lldb_private::CommandReturnObject
Definition: CommandReturnObject.h:25
uint32_t
lldb_private::CommandInterpreter::GetDebugger
Debugger & GetDebugger()
Definition: CommandInterpreter.h:436
CommandObjectScript.h
lldb_private::OptionsWithRaw
Definition: Args.h:319
lldb_private::CommandReturnObject::Succeeded
bool Succeeded()
Definition: CommandReturnObject.cpp:125
lldb_private::ScriptInterpreter
Definition: ScriptInterpreter.h:126
OptionParser.h
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
CommandInterpreter.h
lldb::eScriptLanguageLua
@ eScriptLanguageLua
Definition: lldb-enumerations.h:219
lldb_private::CommandReturnObject::AppendError
void void AppendError(llvm::StringRef in_string)
Definition: CommandReturnObject.cpp:100
ScriptInterpreter.h
lldb
Definition: SBAddress.h:15
lldb_private::CommandObject::GetDebugger
Debugger & GetDebugger()
Definition: CommandObject.cpp:51