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
10#include "lldb/Core/Debugger.h"
12#include "lldb/Host/Config.h"
19#include "lldb/Utility/Args.h"
20
21using namespace lldb;
22using namespace lldb_private;
23
24#define LLDB_OPTIONS_script
25#include "CommandOptions.inc"
26
28 uint32_t option_idx, llvm::StringRef option_arg,
29 ExecutionContext *execution_context) {
31 const int short_option = m_getopt_table[option_idx].val;
32
33 switch (short_option) {
34 case 'l':
36 option_arg, GetDefinitions()[option_idx].enum_values,
38 if (!error.Success())
39 error.SetErrorStringWithFormat("unrecognized value for language '%s'",
40 option_arg.str().c_str());
41 break;
42 default:
43 llvm_unreachable("Unimplemented option");
44 }
45
46 return error;
47}
48
50 ExecutionContext *execution_context) {
52}
53
54llvm::ArrayRef<OptionDefinition>
56 return llvm::ArrayRef(g_script_options);
57}
58
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>]") {}
65
67
68void 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))
75 return;
76 command = raw_args.GetRawPart();
77 }
78
79 lldb::ScriptLanguage language =
83
84 if (language == lldb::eScriptLanguageNone) {
85 result.AppendError(
86 "the script-lang setting is set to none - scripting not available");
87 return;
88 }
89
90 ScriptInterpreter *script_interpreter =
91 GetDebugger().GetScriptInterpreter(true, language);
92
93 if (script_interpreter == nullptr) {
94 result.AppendError("no script interpreter");
95 return;
96 }
97
98 // Script might change Python code we use for formatting. Make sure we keep
99 // up to date with it.
101
102 if (command.empty()) {
103 script_interpreter->ExecuteInterpreterLoop();
105 return;
106 }
107
108 // We can do better when reporting the status of one-liner script execution.
109 if (script_interpreter->ExecuteOneLine(command, &result))
111 else
113}
static llvm::raw_ostream & error(Stream &strm)
Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, ExecutionContext *execution_context) override
Set the value of an option.
llvm::ArrayRef< OptionDefinition > GetDefinitions() override
void OptionParsingStarting(ExecutionContext *execution_context) override
void DoExecute(llvm::StringRef command, CommandReturnObject &result) override
CommandObjectScript(CommandInterpreter &interpreter)
CommandInterpreter & m_interpreter
bool ParseOptions(Args &args, CommandReturnObject &result)
void void AppendError(llvm::StringRef in_string)
void SetStatus(lldb::ReturnStatus status)
lldb::ScriptLanguage GetScriptLanguage() const
Definition: Debugger.cpp:345
ScriptInterpreter * GetScriptInterpreter(bool can_create=true, std::optional< lldb::ScriptLanguage > language={})
Definition: Debugger.cpp:1649
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
A pair of an option list with a 'raw' string as a suffix.
Definition: Args.h:315
bool HasArgs() const
Returns true if there are any arguments before the raw suffix.
Definition: Args.h:326
Args & GetArgs()
Returns the list of arguments.
Definition: Args.h:331
const std::string & GetRawPart() const
Returns the raw suffix part of the parsed string.
Definition: Args.h:364
std::vector< Option > m_getopt_table
Definition: Options.h:198
virtual void ExecuteInterpreterLoop()=0
virtual bool ExecuteOneLine(llvm::StringRef command, CommandReturnObject *result, const ExecuteScriptOptions &options=ExecuteScriptOptions())=0
An error handling class.
Definition: Status.h:44
A class that represents a running process on the host machine.
Definition: SBAttachInfo.h:14
Definition: SBAddress.h:15
ScriptLanguage
Script interpreter types.
@ eScriptLanguageNone
@ eReturnStatusFailed
@ eReturnStatusSuccessFinishNoResult
static int64_t ToOptionEnum(llvm::StringRef s, const OptionEnumValues &enum_values, int32_t fail_value, Status &error)