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);
248 std::vector<std::string>
250 std::vector<std::string> languages;
251 for (
const size_t idx : indices) {
262 const llvm::StringMap<std::vector<size_t>> &grouped_by_extension,
264 llvm::json::Array extensions;
265 for (
const auto &extension_pair : grouped_by_extension) {
270 llvm::json::Array languages;
271 for (
const std::string &lang :
273 languages.push_back(lang);
274 if (languages.empty())
277 llvm::StringRef desc =
279 extension_pair.second[0]);
282 extension_pair.second[0]);
284 llvm::json::Array api_usages;
286 api_usages.push_back(usage.str());
288 llvm::json::Array cmd_usages;
290 cmd_usages.push_back(usage.str());
292 extensions.push_back(llvm::json::Object{
293 {
"name", extension_pair.first().str()},
294 {
"description", desc.str()},
295 {
"languages", std::move(languages)},
296 {
"api_usages", std::move(api_usages)},
297 {
"command_interpreter_usages", std::move(cmd_usages)},
302 llvm::raw_string_ostream os(str);
303 os << llvm::formatv(
"{0:2}", llvm::json::Value(std::move(extensions)));
309 const llvm::StringMap<std::vector<size_t>> &grouped_by_extension,
312 const bool use_color = s.
AsRawOstream().colors_enabled();
313 auto ansi_code = [use_color](llvm::StringRef code) {
316 const std::string label_color = ansi_code(
"${ansi.fg.green}${ansi.bold}");
317 const std::string name_color = ansi_code(
"${ansi.fg.cyan}${ansi.bold}");
318 const std::string sep_color = ansi_code(
"${ansi.faint}");
319 const std::string reset = ansi_code(
"${ansi.normal}");
320 const std::string separator(
321 std::min<uint64_t>(
GetDebugger().GetTerminalWidth(), 80),
'-');
323 s.
PutCString(
"Available scripted extension templates:");
325 auto print_field = [&](llvm::StringRef key, llvm::StringRef value,
326 const std::string &value_color = std::string()) {
331 s << label_color << key <<
": " << reset;
332 if (!value_color.empty())
333 s << value_color << value << reset;
340 size_t num_listed_interface = 0;
341 for (
const auto &extension_pair : grouped_by_extension) {
342 std::vector<std::string> languages =
344 if (languages.empty())
346 num_listed_interface++;
349 s << sep_color << separator << reset;
352 llvm::StringRef desc =
354 extension_pair.second[0]);
357 extension_pair.second[0]);
359 print_field(
"Name", extension_pair.first(), name_color);
360 print_field(
"Description", desc);
361 print_field(
"Language", llvm::join(languages,
""));
367 if (!num_listed_interface)
377#define LLDB_OPTIONS_scripting_extension_generate
378#include "CommandOptions.inc"
384 "Generate a scripting extension template. ",
385 "scripting extension generate") {
400 const char short_option =
401 g_scripting_extension_generate_options[option_idx].short_option;
403 switch (short_option) {
411 if (!
error.Success())
413 "unrecognized value for language '{0}'", option_arg);
428 "invalid boolean value for -e: '{0}'", option_arg);
431 llvm_unreachable(
"Unimplemented option");
446 return llvm::ArrayRef(g_scripting_extension_generate_options);
463 llvm::ArrayRef<OptionDefinition> defs =
m_options.GetDefinitions();
465 if (elem.opt_defs_index < 0 ||
466 static_cast<size_t>(elem.opt_defs_index) >= defs.size())
468 if (defs[elem.opt_defs_index].short_option !=
'l' ||
469 elem.opt_arg_pos <= 0)
471 llvm::StringRef value =
476 value, defs[elem.opt_defs_index].enum_values,
479 language = candidate;
496 std::vector<ScriptInterpreter::ExtensionTemplateRequest> extension_requests;
500 llvm::SmallVector<llvm::StringRef> extension_components;
501 extension_name.split(extension_components,
".");
506 "unknown scripted extension: '{0}'", extension_name));
509 extension_requests.push_back({extension_name, extension_components});
519 "the script-lang setting is set to none - scripting not available");
526 if (script_interpreter ==
nullptr) {
532 m_options.m_generated_class_prefix, extension_requests,
534 if (!generated_file_or_err) {
535 result.
SetError(generated_file_or_err.takeError());
539 bool should_open_editor =
false;
542 should_open_editor =
true;
545 should_open_editor =
false;
552 if (should_open_editor) {
554 "", *generated_file_or_err, 1,
true)) {
556 "OpenFileInExternalEditor failed: {0}");
560 "Generated scripting extension template: {0}",
561 generated_file_or_err->GetPath());
573 interpreter,
"scripting extension",
574 "Commands for operating on the scripting extensions.",
575 "scripting extension [<subcommand-options>]") {
590 interpreter,
"scripting",
591 "Commands for operating on the scripting functionalities.",
592 "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)
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()
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)