13#include "lldb/Host/Config.h"
27#include "llvm/ADT/StringMap.h"
32#define LLDB_OPTIONS_scripting_run
33#include "CommandOptions.inc"
39 interpreter,
"scripting run",
40 "Invoke the script interpreter with provided code and display any "
41 "results. Start the interactive interpreter if no code is "
43 "scripting run [--language <scripting-language> --] "
59 switch (short_option) {
66 "unrecognized value for language '%s'", option_arg.str().c_str());
69 llvm_unreachable(
"Unimplemented option");
80 return llvm::ArrayRef(g_scripting_run_options);
105 "the script-lang setting is set to none - scripting not available");
112 if (script_interpreter ==
nullptr) {
121 if (command.empty()) {
138#define LLDB_OPTIONS_scripting_extension_list
139#include "CommandOptions.inc"
145 interpreter,
"scripting extension list",
146 "List all the available scripting extension templates. ",
147 "scripting extension list [--language <scripting-language> --] "
148 "[--json --] [<extension-name> ...]") {
173 switch (short_option) {
178 if (!
error.Success())
180 "unrecognized value for language '{0}'", option_arg);
186 llvm_unreachable(
"Unimplemented option");
198 return llvm::ArrayRef(g_scripting_extension_list_options);
207 llvm::StringMap<std::vector<size_t>> grouped_by_extension;
214 llvm::StringLiteral extension_name =
216 if (grouped_by_extension.contains(extension_name))
217 grouped_by_extension[extension_name].push_back(i);
219 grouped_by_extension[extension_name] = {i};
223 llvm::StringMap<std::vector<size_t>> filtered;
232 llvm::StringLiteral extension_name =
234 auto it = grouped_by_extension.find(extension_name);
235 if (it != grouped_by_extension.end())
236 filtered[extension_name] = it->second;
238 grouped_by_extension = std::move(filtered);
257 std::vector<std::string>
259 std::vector<std::string> languages;
260 for (
const size_t idx : indices) {
271 const llvm::StringMap<std::vector<size_t>> &grouped_by_extension,
273 llvm::json::Array extensions;
274 for (
const auto &extension_pair : grouped_by_extension) {
279 llvm::json::Array languages;
280 for (
const std::string &lang :
282 languages.push_back(lang);
283 if (languages.empty())
286 llvm::StringRef desc =
288 extension_pair.second[0]);
291 extension_pair.second[0]);
293 llvm::json::Array api_usages;
295 api_usages.push_back(usage.str());
297 llvm::json::Array cmd_usages;
299 cmd_usages.push_back(usage.str());
301 extensions.push_back(llvm::json::Object{
302 {
"name", extension_pair.first().str()},
303 {
"description", desc.str()},
304 {
"languages", std::move(languages)},
305 {
"api_usages", std::move(api_usages)},
306 {
"command_interpreter_usages", std::move(cmd_usages)},
311 llvm::raw_string_ostream os(str);
312 os << llvm::formatv(
"{0:2}", llvm::json::Value(std::move(extensions)));
318 const llvm::StringMap<std::vector<size_t>> &grouped_by_extension,
321 const bool use_color = s.
AsRawOstream().colors_enabled();
323 auto ansi_code = [use_color](llvm::StringRef code) {
330 const std::string separator =
GetSeparator(debugger, use_color);
332 s.
PutCString(
"Available scripted extension templates:");
334 auto print_field = [&](llvm::StringRef key, llvm::StringRef value,
335 bool is_title =
false) {
340 s << label_prefix << key <<
": " << label_suffix;
342 s << title_prefix << value << title_suffix;
349 size_t num_listed_interface = 0;
350 for (
const auto &extension_pair : grouped_by_extension) {
351 std::vector<std::string> languages =
353 if (languages.empty())
355 num_listed_interface++;
361 llvm::StringRef desc =
363 extension_pair.second[0]);
366 extension_pair.second[0]);
368 print_field(
"Name", extension_pair.first(),
true);
369 print_field(
"Description", desc);
370 print_field(
"Language", llvm::join(languages,
""));
376 if (!num_listed_interface)
386#define LLDB_OPTIONS_scripting_extension_generate
387#include "CommandOptions.inc"
393 "Generate a scripting extension template. ",
394 "scripting extension generate") {
409 const char short_option =
410 g_scripting_extension_generate_options[option_idx].short_option;
412 switch (short_option) {
420 if (!
error.Success())
422 "unrecognized value for language '{0}'", option_arg);
437 "invalid boolean value for -e: '{0}'", option_arg);
440 llvm_unreachable(
"Unimplemented option");
455 return llvm::ArrayRef(g_scripting_extension_generate_options);
472 llvm::ArrayRef<OptionDefinition> defs =
m_options.GetDefinitions();
474 if (elem.opt_defs_index < 0 ||
475 static_cast<size_t>(elem.opt_defs_index) >= defs.size())
477 if (defs[elem.opt_defs_index].short_option !=
'l' ||
478 elem.opt_arg_pos <= 0)
480 llvm::StringRef value =
485 value, defs[elem.opt_defs_index].enum_values,
488 language = candidate;
505 std::vector<ScriptInterpreter::ExtensionTemplateRequest> extension_requests;
509 llvm::SmallVector<llvm::StringRef> extension_components;
510 extension_name.split(extension_components,
".");
515 "unknown scripted extension: '{0}'", extension_name));
518 extension_requests.push_back({extension_name, extension_components});
528 "the script-lang setting is set to none - scripting not available");
535 if (script_interpreter ==
nullptr) {
541 m_options.m_generated_class_prefix, extension_requests,
543 if (!generated_file_or_err) {
544 result.
SetError(generated_file_or_err.takeError());
548 bool should_open_editor =
false;
551 should_open_editor =
true;
554 should_open_editor =
false;
561 if (should_open_editor) {
563 "", *generated_file_or_err, 1,
true)) {
565 "OpenFileInExternalEditor failed: {0}");
569 "Generated scripting extension template: {0}",
570 generated_file_or_err->GetPath());
582 interpreter,
"scripting extension",
583 "Commands for operating on the scripting extensions.",
584 "scripting extension [<subcommand-options>]") {
599 interpreter,
"scripting",
600 "Commands for operating on the scripting functionalities.",
601 "scripting <subcommand> [<subcommand-options>]") {
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOG_ERROR(log, error,...)
CommandObjectMultiwordScriptingExtension(CommandInterpreter &interpreter)
~CommandObjectMultiwordScriptingExtension() override=default
~CommandOptions() override=default
void OptionParsingStarting(ExecutionContext *execution_context) override
lldb::ScriptLanguage m_language
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
std::string m_output_filepath
std::string m_generated_class_prefix
bool m_generate_non_abstract_methods
~CommandObjectScriptingExtensionGenerate() override=default
void HandleArgumentCompletion(CompletionRequest &request, OptionElementVector &opt_element_vector) override
The default version handles argument definitions that have only one argument type,...
CommandObjectScriptingExtensionGenerate(CommandInterpreter &interpreter)
void DoExecute(Args &command, CommandReturnObject &result) override
Options * GetOptions() override
lldb::ScriptLanguage m_language
llvm::ArrayRef< OptionDefinition > GetDefinitions() override
void OptionParsingStarting(ExecutionContext *execution_context) override
~CommandOptions() override=default
Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, ExecutionContext *execution_context) override
Set the value of an option.
void HandleArgumentCompletion(CompletionRequest &request, OptionElementVector &opt_element_vector) override
The default version handles argument definitions that have only one argument type,...
Options * GetOptions() override
void DoExecute(Args &command, CommandReturnObject &result) override
std::vector< std::string > GetLanguagesForExtension(const std::vector< size_t > &indices)
void OutputJsonFormat(const llvm::StringMap< std::vector< size_t > > &grouped_by_extension, CommandReturnObject &result)
~CommandObjectScriptingExtensionList() override=default
void OutputTextFormat(const llvm::StringMap< std::vector< size_t > > &grouped_by_extension, CommandReturnObject &result)
static std::string GetSeparator(const Debugger &debugger, bool use_color)
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.
~CommandOptions() override=default
lldb::ScriptLanguage language
Options * GetOptions() override
CommandObjectScriptingRun(CommandInterpreter &interpreter)
void DoExecute(llvm::StringRef command, CommandReturnObject &result) override
~CommandObjectScriptingRun() override=default
A command line argument class.
size_t GetArgumentCount() const
Gets the number of arguments left in this command object.
llvm::ArrayRef< ArgEntry > entries() const
const char * GetArgumentAtIndex(size_t idx) const
Gets the null-terminated C string argument pointer for the argument at index idx.
static bool InvokeCommonCompletionCallbacks(CommandInterpreter &interpreter, uint32_t completion_mask, lldb_private::CompletionRequest &request, SearchFilter *searcher)
~CommandObjectMultiwordScripting() override
CommandObjectMultiwordScripting(CommandInterpreter &interpreter)
bool LoadSubCommand(llvm::StringRef cmd_name, const lldb::CommandObjectSP &command_obj) override
CommandObjectMultiword(CommandInterpreter &interpreter, const char *name, const char *help=nullptr, const char *syntax=nullptr, uint32_t flags=0)
friend class CommandInterpreter
CommandObjectParsed(CommandInterpreter &interpreter, const char *name, const char *help=nullptr, const char *syntax=nullptr, uint32_t flags=0)
CommandObjectRaw(CommandInterpreter &interpreter, llvm::StringRef name, llvm::StringRef help="", llvm::StringRef syntax="", uint32_t flags=0)
void AddSimpleArgumentList(lldb::CommandArgumentType arg_type, ArgumentRepetitionType repetition_type=eArgRepeatPlain)
CommandInterpreter & GetCommandInterpreter()
CommandInterpreter & m_interpreter
bool ParseOptions(Args &args, CommandReturnObject &result)
void AppendMessage(llvm::StringRef in_string)
bool GetInteractive() const
void AppendError(llvm::StringRef in_string)
void SetStatus(lldb::ReturnStatus status)
void SetError(Status error)
void AppendErrorWithFormat(const char *format,...) __attribute__((format(printf
void void AppendMessageWithFormatv(const char *format, Args &&...args)
Stream & GetOutputStream()
"lldb/Utility/ArgCompletionRequest.h"
const Args & GetParsedLine() const
llvm::StringRef GetCursorArgumentPrefix() const
static void ForceUpdate()
A class to manage flag bits.
llvm::StringRef GetLabelAnsiSuffix() const
llvm::StringRef GetDividerAnsiPrefix() const
uint64_t GetTerminalWidth() const
llvm::StringRef GetDividerAnsiSuffix() const
llvm::StringRef GetTitleAnsiPrefix() const
llvm::StringRef GetTitleAnsiSuffix() const
llvm::StringRef GetLabelAnsiPrefix() const
ScriptInterpreter * GetScriptInterpreter(bool can_create=true, std::optional< lldb::ScriptLanguage > language={})
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
static llvm::Error OpenFileInExternalEditor(llvm::StringRef editor, const FileSpec &file_spec, uint32_t line_no, bool foreground=false)
A pair of an option list with a 'raw' string as a suffix.
bool HasArgs() const
Returns true if there are any arguments before the raw suffix.
Args & GetArgs()
Returns the list of arguments.
const std::string & GetRawPart() const
Returns the raw suffix part of the parsed string.
A command line option parsing protocol class.
std::vector< Option > m_getopt_table
static void AutoCompleteScriptedExtension(llvm::StringRef partial_name, CompletionRequest &request, lldb::ScriptLanguage language=lldb::eScriptLanguageUnknown)
static lldb::ScriptLanguage GetScriptedInterfaceLanguageAtIndex(uint32_t idx)
static uint32_t GetNumScriptedInterfaces()
static lldb::ScriptedExtension GetScriptedInterfaceExtensionAtIndex(uint32_t idx)
static llvm::StringRef GetScriptedInterfaceDescriptionAtIndex(uint32_t idx)
static ScriptedInterfaceUsages GetScriptedInterfaceUsagesAtIndex(uint32_t idx)
virtual llvm::Expected< FileSpec > GenerateExtensionTemplate(const std::string &name, std::vector< ExtensionTemplateRequest > &extensions, bool generate_non_abstract_methods, std::string output_file)
virtual void ExecuteInterpreterLoop()=0
static llvm::StringLiteral ExtensionToString(lldb::ScriptedExtension extension)
static lldb::ScriptedExtension StringToExtension(llvm::StringRef string)
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, bool use_color=false) const
const std::vector< llvm::StringRef > & GetCommandInterpreterUsages() const
const std::vector< llvm::StringRef > & GetSBAPIUsages() const
static Status FromErrorStringWithFormat(const char *format,...) __attribute__((format(printf
static Status FromErrorString(const char *str)
static Status static Status FromErrorStringWithFormatv(const char *format, Args &&...args)
A stream class that can stream formatted output to a file.
llvm::raw_ostream & AsRawOstream()
Returns a raw_ostream that forwards the data to this Stream object.
size_t Indent(llvm::StringRef s="")
Indent the current line in the stream.
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
size_t EOL()
Output and End of Line character to the stream.
void IndentLess(unsigned amount=2)
Decrement the current indentation level.
void IndentMore(unsigned amount=2)
Increment the current indentation level.
std::string FormatAnsiTerminalCodes(llvm::StringRef format, bool do_color=true)
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
std::vector< OptionArgElement > OptionElementVector
@ eScriptedExtensionCompletion
ScriptLanguage
Script interpreter types.
ScriptedExtension
Scripting extension types.
@ eScriptedExtensionInvalid
std::shared_ptr< lldb_private::CommandObject > CommandObjectSP
@ eReturnStatusSuccessFinishResult
@ eReturnStatusSuccessFinishNoResult
@ eArgTypeScriptedExtension
static int64_t ToOptionEnum(llvm::StringRef s, const OptionEnumValues &enum_values, int32_t fail_value, Status &error)
static bool ToBoolean(llvm::StringRef s, bool fail_value, bool *success_ptr)