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 the available scripting extension templates, or with "
147 "--instances, the instantiated scripting extensions grouped by "
149 "scripting extension list [--language <scripting-language>] "
150 "[--json] [--instances] [<extension-name> ...]") {
175 switch (short_option) {
180 if (!
error.Success())
182 "unrecognized value for language '{0}'", option_arg);
191 llvm_unreachable(
"Unimplemented option");
204 return llvm::ArrayRef(g_scripting_extension_list_options);
219 llvm::StringMap<std::vector<size_t>> grouped_by_extension;
226 llvm::StringLiteral extension_name =
228 if (grouped_by_extension.contains(extension_name))
229 grouped_by_extension[extension_name].push_back(i);
231 grouped_by_extension[extension_name] = {i};
235 llvm::StringMap<std::vector<size_t>> filtered;
244 llvm::StringLiteral extension_name =
246 auto it = grouped_by_extension.find(extension_name);
247 if (it != grouped_by_extension.end())
248 filtered[extension_name] = it->second;
250 grouped_by_extension = std::move(filtered);
270 std::vector<lldb::ScriptedExtension> extensions;
279 extensions.push_back(extension);
282 std::vector<ScriptedInstanceGroup> groups;
287 script_interpreter->GetScriptedInstanceRegistry()->GetInstanceGroups(
293 groups_array.
AddItem(group.ToStructuredData());
301 const bool use_color = s.
AsRawOstream().colors_enabled();
303 const std::string separator =
GetSeparator(debugger, use_color);
305 s.
PutCString(
"Instantiated scripted extensions:");
310 s << separator <<
'\n';
311 group.Dump(s, debugger, use_color);
316 std::vector<std::string>
318 std::vector<std::string> languages;
319 for (
const size_t idx : indices) {
330 const llvm::StringMap<std::vector<size_t>> &grouped_by_extension,
332 llvm::json::Array extensions;
333 for (
const auto &extension_pair : grouped_by_extension) {
338 llvm::json::Array languages;
339 for (
const std::string &lang :
341 languages.push_back(lang);
342 if (languages.empty())
345 llvm::StringRef desc =
347 extension_pair.second[0]);
350 extension_pair.second[0]);
352 llvm::json::Array api_usages;
354 api_usages.push_back(usage.str());
356 llvm::json::Array cmd_usages;
358 cmd_usages.push_back(usage.str());
360 extensions.push_back(llvm::json::Object{
361 {
"name", extension_pair.first().str()},
362 {
"description", desc.str()},
363 {
"languages", std::move(languages)},
364 {
"api_usages", std::move(api_usages)},
365 {
"command_interpreter_usages", std::move(cmd_usages)},
370 llvm::raw_string_ostream os(str);
371 os << llvm::formatv(
"{0:2}", llvm::json::Value(std::move(extensions)));
377 const llvm::StringMap<std::vector<size_t>> &grouped_by_extension,
380 const bool use_color = s.
AsRawOstream().colors_enabled();
382 auto ansi_code = [use_color](llvm::StringRef code) {
389 const std::string separator =
GetSeparator(debugger, use_color);
391 s.
PutCString(
"Available scripted extension templates:");
393 auto print_field = [&](llvm::StringRef key, llvm::StringRef value,
394 bool is_title =
false) {
399 s << label_prefix << key <<
": " << label_suffix;
401 s << title_prefix << value << title_suffix;
408 size_t num_listed_interface = 0;
409 for (
const auto &extension_pair : grouped_by_extension) {
410 std::vector<std::string> languages =
412 if (languages.empty())
414 num_listed_interface++;
420 llvm::StringRef desc =
422 extension_pair.second[0]);
425 extension_pair.second[0]);
427 print_field(
"Name", extension_pair.first(),
true);
428 print_field(
"Description", desc);
429 print_field(
"Language", llvm::join(languages,
""));
435 if (!num_listed_interface)
445#define LLDB_OPTIONS_scripting_extension_generate
446#include "CommandOptions.inc"
452 "Generate a scripting extension template. ",
453 "scripting extension generate") {
468 const char short_option =
469 g_scripting_extension_generate_options[option_idx].short_option;
471 switch (short_option) {
479 if (!
error.Success())
481 "unrecognized value for language '{0}'", option_arg);
496 "invalid boolean value for -e: '{0}'", option_arg);
499 llvm_unreachable(
"Unimplemented option");
514 return llvm::ArrayRef(g_scripting_extension_generate_options);
531 llvm::ArrayRef<OptionDefinition> defs =
m_options.GetDefinitions();
533 if (elem.opt_defs_index < 0 ||
534 static_cast<size_t>(elem.opt_defs_index) >= defs.size())
536 if (defs[elem.opt_defs_index].short_option !=
'l' ||
537 elem.opt_arg_pos <= 0)
539 llvm::StringRef value =
544 value, defs[elem.opt_defs_index].enum_values,
547 language = candidate;
564 std::vector<ScriptInterpreter::ExtensionTemplateRequest> extension_requests;
568 llvm::SmallVector<llvm::StringRef> extension_components;
569 extension_name.split(extension_components,
".");
574 "unknown scripted extension: '{0}'", extension_name));
577 extension_requests.push_back({extension_name, extension_components});
587 "the script-lang setting is set to none - scripting not available");
594 if (script_interpreter ==
nullptr) {
600 m_options.m_generated_class_prefix, extension_requests,
602 if (!generated_file_or_err) {
603 result.
SetError(generated_file_or_err.takeError());
607 bool should_open_editor =
false;
610 should_open_editor =
true;
613 should_open_editor =
false;
620 if (should_open_editor) {
622 "", *generated_file_or_err, 1,
true)) {
624 "OpenFileInExternalEditor failed: {0}");
628 "Generated scripting extension template: {0}",
629 generated_file_or_err->GetPath());
641 interpreter,
"scripting extension",
642 "Commands for operating on the scripting extensions.",
643 "scripting extension [<subcommand-options>]") {
658 interpreter,
"scripting",
659 "Commands for operating on the scripting functionalities.",
660 "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)
void ListInstances(Args &command, CommandReturnObject &result)
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 size_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.
void AddItem(const ObjectSP &item)
void Dump(lldb_private::Stream &s, bool pretty_print=true) const
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)
The live instances of one extension class.