LLDB mainline
CommandObjectScripting.cpp
Go to the documentation of this file.
1//===-- CommandObjectScripting.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"
13#include "lldb/Host/Config.h"
21#include "lldb/Utility/Args.h"
22
23using namespace lldb;
24using namespace lldb_private;
25
26#define LLDB_OPTIONS_scripting_run
27#include "CommandOptions.inc"
28
30public:
33 interpreter, "scripting run",
34 "Invoke the script interpreter with provided code and display any "
35 "results. Start the interactive interpreter if no code is "
36 "supplied.",
37 "scripting run [--language <scripting-language> --] "
38 "[<script-code>]") {}
39
40 ~CommandObjectScriptingRun() override = default;
41
42 Options *GetOptions() override { return &m_options; }
43
44 class CommandOptions : public Options {
45 public:
46 CommandOptions() = default;
47 ~CommandOptions() override = default;
48 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
49 ExecutionContext *execution_context) override {
51 const int short_option = m_getopt_table[option_idx].val;
52
53 switch (short_option) {
54 case 'l':
56 option_arg, GetDefinitions()[option_idx].enum_values,
58 if (!error.Success())
60 "unrecognized value for language '%s'", option_arg.str().c_str());
61 break;
62 default:
63 llvm_unreachable("Unimplemented option");
64 }
65
66 return error;
67 }
68
69 void OptionParsingStarting(ExecutionContext *execution_context) override {
71 }
72
73 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
74 return llvm::ArrayRef(g_scripting_run_options);
75 }
76
78 };
79
80protected:
81 void DoExecute(llvm::StringRef command,
82 CommandReturnObject &result) override {
83 // Try parsing the language option but when the command contains a raw part
84 // separated by the -- delimiter.
85 OptionsWithRaw raw_args(command);
86 if (raw_args.HasArgs()) {
87 if (!ParseOptions(raw_args.GetArgs(), result))
88 return;
89 command = raw_args.GetRawPart();
90 }
91
92 lldb::ScriptLanguage language =
96
97 if (language == lldb::eScriptLanguageNone) {
98 result.AppendError(
99 "the script-lang setting is set to none - scripting not available");
100 return;
101 }
102
103 ScriptInterpreter *script_interpreter =
104 GetDebugger().GetScriptInterpreter(true, language);
105
106 if (script_interpreter == nullptr) {
107 result.AppendError("no script interpreter");
108 return;
109 }
110
111 // Script might change Python code we use for formatting. Make sure we keep
112 // up to date with it.
114
115 if (command.empty()) {
116 script_interpreter->ExecuteInterpreterLoop();
118 return;
119 }
120
121 // We can do better when reporting the status of one-liner script execution.
122 if (script_interpreter->ExecuteOneLine(command, &result))
124 else
126 }
127
128private:
130};
131
132#define LLDB_OPTIONS_scripting_extension_list
133#include "CommandOptions.inc"
134
136public:
139 interpreter, "scripting extension list",
140 "List all the available scripting extension templates. ",
141 "scripting template list [--language <scripting-language> --]") {}
142
144
145 Options *GetOptions() override { return &m_options; }
146
147 class CommandOptions : public Options {
148 public:
149 CommandOptions() = default;
150 ~CommandOptions() override = default;
151 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
152 ExecutionContext *execution_context) override {
154 const int short_option = m_getopt_table[option_idx].val;
155
156 switch (short_option) {
157 case 'l':
159 option_arg, GetDefinitions()[option_idx].enum_values,
161 if (!error.Success())
163 "unrecognized value for language '{0}'", option_arg);
164 break;
165 default:
166 llvm_unreachable("Unimplemented option");
167 }
168
169 return error;
170 }
171
172 void OptionParsingStarting(ExecutionContext *execution_context) override {
174 }
175
176 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
177 return llvm::ArrayRef(g_scripting_extension_list_options);
178 }
179
181 };
182
183protected:
184 void DoExecute(Args &command, CommandReturnObject &result) override {
185 Stream &s = result.GetOutputStream();
186 s.Printf("Available scripted extension templates:");
187
188 auto print_field = [&s](llvm::StringRef key, llvm::StringRef value) {
189 if (!value.empty()) {
190 s.IndentMore();
191 s.Indent();
192 s << key << ": " << value << '\n';
193 s.IndentLess();
194 }
195 };
196
197 size_t num_listed_interface = 0;
198 size_t num_extensions = PluginManager::GetNumScriptedInterfaces();
199 for (size_t i = 0; i < num_extensions; i++) {
200 llvm::StringRef plugin_name =
202 if (plugin_name.empty())
203 break;
204
207 if (lang != m_options.m_language)
208 continue;
209
210 if (!num_listed_interface)
211 s.EOL();
212
213 num_listed_interface++;
214
215 llvm::StringRef desc =
219
220 print_field("Name", plugin_name);
221 print_field("Language", ScriptInterpreter::LanguageToString(lang));
222 print_field("Description", desc);
223 usages.Dump(s, ScriptedInterfaceUsages::UsageKind::API);
224 usages.Dump(s, ScriptedInterfaceUsages::UsageKind::CommandInterpreter);
225
226 if (i != num_extensions - 1)
227 s.EOL();
228 }
229
230 if (!num_listed_interface)
231 s << " None\n";
232 }
233
234private:
236};
237
239public:
242 interpreter, "scripting extension",
243 "Commands for operating on the scripting extensions.",
244 "scripting extension [<subcommand-options>]") {
246 "list",
248 }
249
251};
252
254 CommandInterpreter &interpreter)
256 interpreter, "scripting",
257 "Commands for operating on the scripting functionalities.",
258 "scripting <subcommand> [<subcommand-options>]") {
259 LoadSubCommand("run",
261 LoadSubCommand("extension",
263 interpreter)));
264}
265
static llvm::raw_ostream & error(Stream &strm)
CommandObjectMultiwordScriptingExtension(CommandInterpreter &interpreter)
~CommandObjectMultiwordScriptingExtension() override=default
llvm::ArrayRef< OptionDefinition > GetDefinitions() override
void OptionParsingStarting(ExecutionContext *execution_context) override
Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, ExecutionContext *execution_context) override
Set the value of an option.
void DoExecute(Args &command, CommandReturnObject &result) override
~CommandObjectScriptingExtensionList() override=default
CommandObjectScriptingExtensionList(CommandInterpreter &interpreter)
void OptionParsingStarting(ExecutionContext *execution_context) override
llvm::ArrayRef< OptionDefinition > GetDefinitions() override
Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, ExecutionContext *execution_context) override
Set the value of an option.
CommandObjectScriptingRun(CommandInterpreter &interpreter)
void DoExecute(llvm::StringRef command, CommandReturnObject &result) override
~CommandObjectScriptingRun() override=default
A command line argument class.
Definition: Args.h:33
CommandObjectMultiwordScripting(CommandInterpreter &interpreter)
bool LoadSubCommand(llvm::StringRef cmd_name, const lldb::CommandObjectSP &command_obj) override
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:1719
"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:319
bool HasArgs() const
Returns true if there are any arguments before the raw suffix.
Definition: Args.h:330
Args & GetArgs()
Returns the list of arguments.
Definition: Args.h:335
const std::string & GetRawPart() const
Returns the raw suffix part of the parsed string.
Definition: Args.h:368
A command line option parsing protocol class.
Definition: Options.h:58
std::vector< Option > m_getopt_table
Definition: Options.h:198
static lldb::ScriptLanguage GetScriptedInterfaceLanguageAtIndex(uint32_t idx)
static uint32_t GetNumScriptedInterfaces()
static llvm::StringRef GetScriptedInterfaceDescriptionAtIndex(uint32_t idx)
static llvm::StringRef GetScriptedInterfaceNameAtIndex(uint32_t idx)
static ScriptedInterfaceUsages GetScriptedInterfaceUsagesAtIndex(uint32_t idx)
virtual void ExecuteInterpreterLoop()=0
virtual bool ExecuteOneLine(llvm::StringRef command, CommandReturnObject *result, const ExecuteScriptOptions &options=ExecuteScriptOptions())=0
static std::string LanguageToString(lldb::ScriptLanguage language)
void Dump(Stream &s, UsageKind kind) const
An error handling class.
Definition: Status.h:115
static Status FromErrorStringWithFormat(const char *format,...) __attribute__((format(printf
Definition: Status.cpp:106
static Status static Status FromErrorStringWithFormatv(const char *format, Args &&...args)
Definition: Status.h:148
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
size_t Indent(llvm::StringRef s="")
Indent the current line in the stream.
Definition: Stream.cpp:157
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:134
size_t EOL()
Output and End of Line character to the stream.
Definition: Stream.cpp:155
void IndentLess(unsigned amount=2)
Decrement the current indentation level.
Definition: Stream.cpp:198
void IndentMore(unsigned amount=2)
Increment the current indentation level.
Definition: Stream.cpp:195
A class that represents a running process on the host machine.
Definition: SBAddress.h:15
ScriptLanguage
Script interpreter types.
@ eScriptLanguageDefault
@ eScriptLanguageNone
std::shared_ptr< lldb_private::CommandObject > CommandObjectSP
Definition: lldb-forward.h:333
@ eReturnStatusFailed
@ eReturnStatusSuccessFinishNoResult
static int64_t ToOptionEnum(llvm::StringRef s, const OptionEnumValues &enum_values, int32_t fail_value, Status &error)