LLDB mainline
CommandObjectCommands.cpp
Go to the documentation of this file.
1//===-- CommandObjectCommands.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 "CommandObjectHelp.h"
12#include "lldb/Core/Debugger.h"
13#include "lldb/Core/IOHandler.h"
24#include "lldb/Utility/Args.h"
26#include "llvm/ADT/StringRef.h"
27#include <optional>
28
29using namespace lldb;
30using namespace lldb_private;
31
32// CommandObjectCommandsSource
33
34#define LLDB_OPTIONS_source
35#include "CommandOptions.inc"
36
38public:
41 interpreter, "command source",
42 "Read and execute LLDB commands from the file <filename>.",
43 nullptr) {
45 }
46
47 ~CommandObjectCommandsSource() override = default;
48
49 std::optional<std::string> GetRepeatCommand(Args &current_command_args,
50 uint32_t index) override {
51 return std::string("");
52 }
53
54 Options *GetOptions() override { return &m_options; }
55
56protected:
57 class CommandOptions : public Options {
58 public:
60 : m_stop_on_error(true), m_silent_run(false), m_stop_on_continue(true),
62
63 ~CommandOptions() override = default;
64
65 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
66 ExecutionContext *execution_context) override {
68 const int short_option = m_getopt_table[option_idx].val;
69
70 switch (short_option) {
71 case 'e':
73 break;
74
75 case 'c':
77 break;
78
79 case 'C':
81 break;
82
83 case 's':
85 break;
86
87 default:
88 llvm_unreachable("Unimplemented option");
89 }
90
91 return error;
92 }
93
94 void OptionParsingStarting(ExecutionContext *execution_context) override {
99 }
100
101 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
102 return llvm::ArrayRef(g_source_options);
103 }
104
105 // Instance variables to hold the values for command options.
106
111 };
112
113 void DoExecute(Args &command, CommandReturnObject &result) override {
114 if (command.GetArgumentCount() != 1) {
116 "'%s' takes exactly one executable filename argument.\n",
117 GetCommandName().str().c_str());
118 return;
119 }
120
121 FileSpec source_dir = {};
124 if (!source_dir) {
125 result.AppendError("command source -C can only be specified "
126 "from a command file");
128 return;
129 }
130 }
131
132 FileSpec cmd_file(command[0].ref());
133 if (source_dir) {
134 // Prepend the source_dir to the cmd_file path:
135 if (!cmd_file.IsRelative()) {
136 result.AppendError("command source -C can only be used "
137 "with a relative path.");
139 return;
140 }
141 cmd_file.MakeAbsolute(source_dir);
142 }
143
144 FileSystem::Instance().Resolve(cmd_file);
145
147 // If any options were set, then use them
152 options.SetStopOnContinue(
154
157
158 // Individual silent setting is override for global command echo settings.
160 options.SetSilent(true);
161 } else {
162 options.SetPrintResults(true);
163 options.SetPrintErrors(true);
166 }
167 }
168
169 m_interpreter.HandleCommandsFromFile(cmd_file, options, result);
170 }
171
173};
174
175#pragma mark CommandObjectCommandsAlias
176// CommandObjectCommandsAlias
177
178#define LLDB_OPTIONS_alias
179#include "CommandOptions.inc"
180
182 "Enter your Python command(s). Type 'DONE' to end.\n"
183 "You must define a Python function with this signature:\n"
184 "def my_command_impl(debugger, args, exe_ctx, result, internal_dict):\n";
185
187protected:
189 public:
190 CommandOptions() = default;
191
192 ~CommandOptions() override = default;
193
194 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
195 return llvm::ArrayRef(g_alias_options);
196 }
197
198 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_value,
199 ExecutionContext *execution_context) override {
201
202 const int short_option = GetDefinitions()[option_idx].short_option;
203 std::string option_str(option_value);
204
205 switch (short_option) {
206 case 'h':
207 m_help.SetCurrentValue(option_str);
209 break;
210
211 case 'H':
212 m_long_help.SetCurrentValue(option_str);
214 break;
215
216 default:
217 llvm_unreachable("Unimplemented option");
218 }
219
220 return error;
221 }
222
223 void OptionParsingStarting(ExecutionContext *execution_context) override {
224 m_help.Clear();
226 }
227
230 };
231
234
235public:
236 Options *GetOptions() override { return &m_option_group; }
237
240 interpreter, "command alias",
241 "Define a custom command in terms of an existing command.") {
244
246 "'alias' allows the user to create a short-cut or abbreviation for long \
247commands, multi-word commands, and commands that take particular options. \
248Below are some simple examples of how one might use the 'alias' command:"
249 R"(
250
251(lldb) command alias sc script
252
253 Creates the abbreviation 'sc' for the 'script' command.
254
255(lldb) command alias bp breakpoint
256
257)"
258 " Creates the abbreviation 'bp' for the 'breakpoint' command. Since \
259breakpoint commands are two-word commands, the user would still need to \
260enter the second word after 'bp', e.g. 'bp enable' or 'bp delete'."
261 R"(
262
263(lldb) command alias bpl breakpoint list
264
265 Creates the abbreviation 'bpl' for the two-word command 'breakpoint list'.
266
267)"
268 "An alias can include some options for the command, with the values either \
269filled in at the time the alias is created, or specified as positional \
270arguments, to be filled in when the alias is invoked. The following example \
271shows how to create aliases with options:"
272 R"(
273
274(lldb) command alias bfl breakpoint set -f %1 -l %2
275
276)"
277 " Creates the abbreviation 'bfl' (for break-file-line), with the -f and -l \
278options already part of the alias. So if the user wants to set a breakpoint \
279by file and line without explicitly having to use the -f and -l options, the \
280user can now use 'bfl' instead. The '%1' and '%2' are positional placeholders \
281for the actual arguments that will be passed when the alias command is used. \
282The number in the placeholder refers to the position/order the actual value \
283occupies when the alias is used. All the occurrences of '%1' in the alias \
284will be replaced with the first argument, all the occurrences of '%2' in the \
285alias will be replaced with the second argument, and so on. This also allows \
286actual arguments to be used multiple times within an alias (see 'process \
287launch' example below)."
288 R"(
289
290)"
291 "Note: the positional arguments must substitute as whole words in the resultant \
292command, so you can't at present do something like this to append the file extension \
293\".cpp\":"
294 R"(
295
296(lldb) command alias bcppfl breakpoint set -f %1.cpp -l %2
297
298)"
299 "For more complex aliasing, use the \"command regex\" command instead. In the \
300'bfl' case above, the actual file value will be filled in with the first argument \
301following 'bfl' and the actual line number value will be filled in with the second \
302argument. The user would use this alias as follows:"
303 R"(
304
305(lldb) command alias bfl breakpoint set -f %1 -l %2
306(lldb) bfl my-file.c 137
307
308This would be the same as if the user had entered 'breakpoint set -f my-file.c -l 137'.
309
310Another example:
311
312(lldb) command alias pltty process launch -s -o %1 -e %1
313(lldb) pltty /dev/tty0
314
315 Interpreted as 'process launch -s -o /dev/tty0 -e /dev/tty0'
316
317)"
318 "If the user always wanted to pass the same value to a particular option, the \
319alias could be defined with that value directly in the alias as a constant, \
320rather than using a positional placeholder:"
321 R"(
322
323(lldb) command alias bl3 breakpoint set -f %1 -l 3
324
325 Always sets a breakpoint on line 3 of whatever file is indicated.
327)"
328
329 "If the alias abbreviation or the full alias command collides with another \
330existing command, the command resolver will prefer to use the alias over any \
331other command as far as there is only one alias command match.");
332
336 CommandArgumentData alias_arg;
337 CommandArgumentData cmd_arg;
338 CommandArgumentData options_arg;
339
340 // Define the first (and only) variant of this arg.
341 alias_arg.arg_type = eArgTypeAliasName;
343
344 // There is only one variant this argument could be; put it into the
345 // argument entry.
346 arg1.push_back(alias_arg);
347
348 // Define the first (and only) variant of this arg.
351
352 // There is only one variant this argument could be; put it into the
353 // argument entry.
354 arg2.push_back(cmd_arg);
355
356 // Define the first (and only) variant of this arg.
357 options_arg.arg_type = eArgTypeAliasOptions;
359
360 // There is only one variant this argument could be; put it into the
361 // argument entry.
362 arg3.push_back(options_arg);
363
364 // Push the data for the first argument into the m_arguments vector.
365 m_arguments.push_back(arg1);
366 m_arguments.push_back(arg2);
367 m_arguments.push_back(arg3);
368 }
369
370 ~CommandObjectCommandsAlias() override = default;
371
372protected:
373 void DoExecute(llvm::StringRef raw_command_line,
374 CommandReturnObject &result) override {
375 if (raw_command_line.empty()) {
376 result.AppendError("'command alias' requires at least two arguments");
377 return;
378 }
379
382
383 OptionsWithRaw args_with_suffix(raw_command_line);
384
385 if (args_with_suffix.HasArgs())
386 if (!ParseOptionsAndNotify(args_with_suffix.GetArgs(), result,
387 m_option_group, exe_ctx))
388 return;
389
390 llvm::StringRef raw_command_string = args_with_suffix.GetRawPart();
391 Args args(raw_command_string);
392
393 if (args.GetArgumentCount() < 2) {
394 result.AppendError("'command alias' requires at least two arguments");
395 return;
396 }
397
398 // Get the alias command.
399
400 auto alias_command = args[0].ref();
401 if (alias_command.starts_with("-")) {
402 result.AppendError("aliases starting with a dash are not supported");
403 if (alias_command == "--help" || alias_command == "--long-help") {
404 result.AppendWarning("if trying to pass options to 'command alias' add "
405 "a -- at the end of the options");
406 }
407 return;
408 }
409
410 // Strip the new alias name off 'raw_command_string' (leave it on args,
411 // which gets passed to 'Execute', which does the stripping itself.
412 size_t pos = raw_command_string.find(alias_command);
413 if (pos == 0) {
414 raw_command_string = raw_command_string.substr(alias_command.size());
415 pos = raw_command_string.find_first_not_of(' ');
416 if ((pos != std::string::npos) && (pos > 0))
417 raw_command_string = raw_command_string.substr(pos);
418 } else {
419 result.AppendError("Error parsing command string. No alias created.");
420 return;
421 }
422
423 // Verify that the command is alias-able.
424 if (m_interpreter.CommandExists(alias_command)) {
426 "'%s' is a permanent debugger command and cannot be redefined.\n",
427 args[0].c_str());
428 return;
429 }
430
431 if (m_interpreter.UserMultiwordCommandExists(alias_command)) {
433 "'%s' is a user container command and cannot be overwritten.\n"
434 "Delete it first with 'command container delete'\n",
435 args[0].c_str());
436 return;
437 }
438
439 // Get CommandObject that is being aliased. The command name is read from
440 // the front of raw_command_string. raw_command_string is returned with the
441 // name of the command object stripped off the front.
442 llvm::StringRef original_raw_command_string = raw_command_string;
443 CommandObject *cmd_obj =
444 m_interpreter.GetCommandObjectForCommand(raw_command_string);
445
446 if (!cmd_obj) {
447 result.AppendErrorWithFormat("invalid command given to 'command alias'. "
448 "'%s' does not begin with a valid command."
449 " No alias created.",
450 original_raw_command_string.str().c_str());
451 } else if (!cmd_obj->WantsRawCommandString()) {
452 // Note that args was initialized with the original command, and has not
453 // been updated to this point. Therefore can we pass it to the version of
454 // Execute that does not need/expect raw input in the alias.
455 HandleAliasingNormalCommand(args, result);
456 } else {
457 HandleAliasingRawCommand(alias_command, raw_command_string, *cmd_obj,
458 result);
459 }
460 }
461
462 bool HandleAliasingRawCommand(llvm::StringRef alias_command,
463 llvm::StringRef raw_command_string,
464 CommandObject &cmd_obj,
465 CommandReturnObject &result) {
466 // Verify & handle any options/arguments passed to the alias command
467
468 OptionArgVectorSP option_arg_vector_sp =
470
471 const bool include_aliases = true;
472 // Look up the command using command's name first. This is to resolve
473 // aliases when you are making nested aliases. But if you don't find
474 // it that way, then it wasn't an alias and we can just use the object
475 // we were passed in.
477 cmd_obj.GetCommandName(), include_aliases);
478 if (!cmd_obj_sp)
479 cmd_obj_sp = cmd_obj.shared_from_this();
480
481 if (m_interpreter.AliasExists(alias_command) ||
482 m_interpreter.UserCommandExists(alias_command)) {
484 "Overwriting existing definition for '%s'.\n",
485 alias_command.str().c_str());
486 }
488 alias_command, cmd_obj_sp, raw_command_string)) {
490 alias->SetHelp(m_command_options.m_help.GetCurrentValue());
492 alias->SetHelpLong(m_command_options.m_long_help.GetCurrentValue());
494 } else {
495 result.AppendError("Unable to create requested alias.\n");
496 }
497 return result.Succeeded();
498 }
499
501 size_t argc = args.GetArgumentCount();
502
503 if (argc < 2) {
504 result.AppendError("'command alias' requires at least two arguments");
505 return false;
506 }
507
508 // Save these in std::strings since we're going to shift them off.
509 const std::string alias_command(std::string(args[0].ref()));
510 const std::string actual_command(std::string(args[1].ref()));
511
512 args.Shift(); // Shift the alias command word off the argument vector.
513 args.Shift(); // Shift the old command word off the argument vector.
514
515 // Verify that the command is alias'able, and get the appropriate command
516 // object.
517
518 if (m_interpreter.CommandExists(alias_command)) {
520 "'%s' is a permanent debugger command and cannot be redefined.\n",
521 alias_command.c_str());
522 return false;
523 }
524
525 if (m_interpreter.UserMultiwordCommandExists(alias_command)) {
527 "'%s' is user container command and cannot be overwritten.\n"
528 "Delete it first with 'command container delete'",
529 alias_command.c_str());
530 return false;
531 }
532
533 CommandObjectSP command_obj_sp(
534 m_interpreter.GetCommandSPExact(actual_command, true));
535 CommandObjectSP subcommand_obj_sp;
536 bool use_subcommand = false;
537 if (!command_obj_sp) {
538 result.AppendErrorWithFormat("'%s' is not an existing command.\n",
539 actual_command.c_str());
540 return false;
541 }
542 CommandObject *cmd_obj = command_obj_sp.get();
543 CommandObject *sub_cmd_obj = nullptr;
544 OptionArgVectorSP option_arg_vector_sp =
546
547 while (cmd_obj->IsMultiwordObject() && !args.empty()) {
548 auto sub_command = args[0].ref();
549 assert(!sub_command.empty());
550 subcommand_obj_sp = cmd_obj->GetSubcommandSP(sub_command);
551 if (!subcommand_obj_sp) {
553 "'%s' is not a valid sub-command of '%s'. "
554 "Unable to create alias.\n",
555 args[0].c_str(), actual_command.c_str());
556 return false;
557 }
558
559 sub_cmd_obj = subcommand_obj_sp.get();
560 use_subcommand = true;
561 args.Shift(); // Shift the sub_command word off the argument vector.
562 cmd_obj = sub_cmd_obj;
563 }
564
565 // Verify & handle any options/arguments passed to the alias command
566
567 std::string args_string;
568
569 if (!args.empty()) {
570 CommandObjectSP tmp_sp =
572 if (use_subcommand)
573 tmp_sp = m_interpreter.GetCommandSPExact(sub_cmd_obj->GetCommandName());
574
575 args.GetCommandString(args_string);
576 }
577
578 if (m_interpreter.AliasExists(alias_command) ||
579 m_interpreter.UserCommandExists(alias_command)) {
581 "Overwriting existing definition for '%s'.\n", alias_command.c_str());
582 }
583
585 alias_command, use_subcommand ? subcommand_obj_sp : command_obj_sp,
586 args_string)) {
588 alias->SetHelp(m_command_options.m_help.GetCurrentValue());
590 alias->SetHelpLong(m_command_options.m_long_help.GetCurrentValue());
592 } else {
593 result.AppendError("Unable to create requested alias.\n");
594 return false;
595 }
596
597 return result.Succeeded();
598 }
599};
600
601#pragma mark CommandObjectCommandsUnalias
602// CommandObjectCommandsUnalias
603
605public:
608 interpreter, "command unalias",
609 "Delete one or more custom commands defined by 'command alias'.",
610 nullptr) {
612 }
613
614 ~CommandObjectCommandsUnalias() override = default;
615
616 void
618 OptionElementVector &opt_element_vector) override {
619 if (!m_interpreter.HasCommands() || request.GetCursorIndex() != 0)
620 return;
621
622 for (const auto &ent : m_interpreter.GetAliases()) {
623 request.TryCompleteCurrentArg(ent.first, ent.second->GetHelp());
624 }
625 }
626
627protected:
628 void DoExecute(Args &args, CommandReturnObject &result) override {
629 CommandObject::CommandMap::iterator pos;
630 CommandObject *cmd_obj;
631
632 if (args.empty()) {
633 result.AppendError("must call 'unalias' with a valid alias");
634 return;
635 }
636
637 auto command_name = args[0].ref();
638 cmd_obj = m_interpreter.GetCommandObject(command_name);
639 if (!cmd_obj) {
641 "'%s' is not a known command.\nTry 'help' to see a "
642 "current list of commands.\n",
643 args[0].c_str());
644 return;
645 }
646
647 if (m_interpreter.CommandExists(command_name)) {
648 if (cmd_obj->IsRemovable()) {
650 "'%s' is not an alias, it is a debugger command which can be "
651 "removed using the 'command delete' command.\n",
652 args[0].c_str());
653 } else {
655 "'%s' is a permanent debugger command and cannot be removed.\n",
656 args[0].c_str());
657 }
658 return;
659 }
660
661 if (!m_interpreter.RemoveAlias(command_name)) {
662 if (m_interpreter.AliasExists(command_name))
664 "Error occurred while attempting to unalias '%s'.\n",
665 args[0].c_str());
666 else
667 result.AppendErrorWithFormat("'%s' is not an existing alias.\n",
668 args[0].c_str());
669 return;
670 }
671
673 }
674};
675
676#pragma mark CommandObjectCommandsDelete
677// CommandObjectCommandsDelete
678
680public:
683 interpreter, "command delete",
684 "Delete one or more custom commands defined by 'command regex'.",
685 nullptr) {
687 }
688
689 ~CommandObjectCommandsDelete() override = default;
690
691 void
693 OptionElementVector &opt_element_vector) override {
694 if (!m_interpreter.HasCommands() || request.GetCursorIndex() != 0)
695 return;
696
697 for (const auto &ent : m_interpreter.GetCommands()) {
698 if (ent.second->IsRemovable())
699 request.TryCompleteCurrentArg(ent.first, ent.second->GetHelp());
700 }
701 }
702
703protected:
704 void DoExecute(Args &args, CommandReturnObject &result) override {
705 CommandObject::CommandMap::iterator pos;
706
707 if (args.empty()) {
708 result.AppendErrorWithFormat("must call '%s' with one or more valid user "
709 "defined regular expression command names",
710 GetCommandName().str().c_str());
711 return;
712 }
713
714 auto command_name = args[0].ref();
715 if (!m_interpreter.CommandExists(command_name)) {
716 StreamString error_msg_stream;
717 const bool generate_upropos = true;
718 const bool generate_type_lookup = false;
720 &error_msg_stream, command_name, llvm::StringRef(), llvm::StringRef(),
721 generate_upropos, generate_type_lookup);
722 result.AppendError(error_msg_stream.GetString());
723 return;
724 }
725
726 if (!m_interpreter.RemoveCommand(command_name)) {
728 "'%s' is a permanent debugger command and cannot be removed.\n",
729 args[0].c_str());
730 return;
731 }
732
734 }
735};
736
737// CommandObjectCommandsAddRegex
738
739#define LLDB_OPTIONS_regex
740#include "CommandOptions.inc"
741
742#pragma mark CommandObjectCommandsAddRegex
743
746public:
749 interpreter, "command regex",
750 "Define a custom command in terms of "
751 "existing commands by matching "
752 "regular expressions.",
753 "command regex <cmd-name> [s/<regex>/<subst>/ ...]"),
757 R"(
758)"
759 "This command allows the user to create powerful regular expression commands \
760with substitutions. The regular expressions and substitutions are specified \
761using the regular expression substitution format of:"
762 R"(
763
764 s/<regex>/<subst>/
765
766)"
767 "<regex> is a regular expression that can use parenthesis to capture regular \
768expression input and substitute the captured matches in the output using %1 \
769for the first match, %2 for the second, and so on."
770 R"(
771
772)"
773 "The regular expressions can all be specified on the command line if more than \
774one argument is provided. If just the command name is provided on the command \
775line, then the regular expressions and substitutions can be entered on separate \
776lines, followed by an empty line to terminate the command definition."
777 R"(
779EXAMPLES
780
782 "The following example will define a regular expression command named 'f' that \
783will call 'finish' if there are no arguments, or 'frame select <frame-idx>' if \
784a number follows 'f':"
785 R"(
786
787 (lldb) command regex f s/^$/finish/ 's/([0-9]+)/frame select %1/')");
789 }
790
791 ~CommandObjectCommandsAddRegex() override = default;
792
793protected:
794 void IOHandlerActivated(IOHandler &io_handler, bool interactive) override {
795 StreamFileSP output_sp(io_handler.GetOutputStreamFileSP());
796 if (output_sp && interactive) {
797 output_sp->PutCString("Enter one or more sed substitution commands in "
798 "the form: 's/<regex>/<subst>/'.\nTerminate the "
799 "substitution list with an empty line.\n");
800 output_sp->Flush();
801 }
802 }
803
804 void IOHandlerInputComplete(IOHandler &io_handler,
805 std::string &data) override {
806 io_handler.SetIsDone(true);
807 if (m_regex_cmd_up) {
808 StringList lines;
809 if (lines.SplitIntoLines(data)) {
810 bool check_only = false;
811 for (const std::string &line : lines) {
812 Status error = AppendRegexSubstitution(line, check_only);
813 if (error.Fail()) {
814 if (!GetDebugger().GetCommandInterpreter().GetBatchCommandMode()) {
816 out_stream->Printf("error: %s\n", error.AsCString());
817 }
818 }
819 }
820 }
821 if (m_regex_cmd_up->HasRegexEntries()) {
822 CommandObjectSP cmd_sp(m_regex_cmd_up.release());
823 m_interpreter.AddCommand(cmd_sp->GetCommandName(), cmd_sp, true);
824 }
825 }
826 }
827
828 void DoExecute(Args &command, CommandReturnObject &result) override {
829 const size_t argc = command.GetArgumentCount();
830 if (argc == 0) {
831 result.AppendError("usage: 'command regex <command-name> "
832 "[s/<regex1>/<subst1>/ s/<regex2>/<subst2>/ ...]'\n");
833 return;
834 }
835
837 auto name = command[0].ref();
838 m_regex_cmd_up = std::make_unique<CommandObjectRegexCommand>(
840 true);
841
842 if (argc == 1) {
843 Debugger &debugger = GetDebugger();
844 bool color_prompt = debugger.GetUseColor();
845 const bool multiple_lines = true; // Get multiple lines
846 IOHandlerSP io_handler_sp(new IOHandlerEditline(
847 debugger, IOHandler::Type::Other,
848 "lldb-regex", // Name of input reader for history
849 llvm::StringRef("> "), // Prompt
850 llvm::StringRef(), // Continuation prompt
851 multiple_lines, color_prompt,
852 0, // Don't show line numbers
853 *this));
854
855 if (io_handler_sp) {
856 debugger.RunIOHandlerAsync(io_handler_sp);
858 }
859 } else {
860 for (auto &entry : command.entries().drop_front()) {
861 bool check_only = false;
862 error = AppendRegexSubstitution(entry.ref(), check_only);
863 if (error.Fail())
864 break;
865 }
866
867 if (error.Success()) {
869 }
870 }
871 if (error.Fail()) {
872 result.AppendError(error.AsCString());
873 }
874 }
875
876 Status AppendRegexSubstitution(const llvm::StringRef &regex_sed,
877 bool check_only) {
879
880 if (!m_regex_cmd_up) {
881 error.SetErrorStringWithFormat(
882 "invalid regular expression command object for: '%.*s'",
883 (int)regex_sed.size(), regex_sed.data());
884 return error;
885 }
886
887 size_t regex_sed_size = regex_sed.size();
888
889 if (regex_sed_size <= 1) {
890 error.SetErrorStringWithFormat(
891 "regular expression substitution string is too short: '%.*s'",
892 (int)regex_sed.size(), regex_sed.data());
893 return error;
894 }
895
896 if (regex_sed[0] != 's') {
897 error.SetErrorStringWithFormat("regular expression substitution string "
898 "doesn't start with 's': '%.*s'",
899 (int)regex_sed.size(), regex_sed.data());
900 return error;
901 }
902 const size_t first_separator_char_pos = 1;
903 // use the char that follows 's' as the regex separator character so we can
904 // have "s/<regex>/<subst>/" or "s|<regex>|<subst>|"
905 const char separator_char = regex_sed[first_separator_char_pos];
906 const size_t second_separator_char_pos =
907 regex_sed.find(separator_char, first_separator_char_pos + 1);
908
909 if (second_separator_char_pos == std::string::npos) {
910 error.SetErrorStringWithFormat(
911 "missing second '%c' separator char after '%.*s' in '%.*s'",
912 separator_char,
913 (int)(regex_sed.size() - first_separator_char_pos - 1),
914 regex_sed.data() + (first_separator_char_pos + 1),
915 (int)regex_sed.size(), regex_sed.data());
916 return error;
917 }
918
919 const size_t third_separator_char_pos =
920 regex_sed.find(separator_char, second_separator_char_pos + 1);
921
922 if (third_separator_char_pos == std::string::npos) {
923 error.SetErrorStringWithFormat(
924 "missing third '%c' separator char after '%.*s' in '%.*s'",
925 separator_char,
926 (int)(regex_sed.size() - second_separator_char_pos - 1),
927 regex_sed.data() + (second_separator_char_pos + 1),
928 (int)regex_sed.size(), regex_sed.data());
929 return error;
930 }
931
932 if (third_separator_char_pos != regex_sed_size - 1) {
933 // Make sure that everything that follows the last regex separator char
934 if (regex_sed.find_first_not_of("\t\n\v\f\r ",
935 third_separator_char_pos + 1) !=
936 std::string::npos) {
937 error.SetErrorStringWithFormat(
938 "extra data found after the '%.*s' regular expression substitution "
939 "string: '%.*s'",
940 (int)third_separator_char_pos + 1, regex_sed.data(),
941 (int)(regex_sed.size() - third_separator_char_pos - 1),
942 regex_sed.data() + (third_separator_char_pos + 1));
943 return error;
944 }
945 } else if (first_separator_char_pos + 1 == second_separator_char_pos) {
946 error.SetErrorStringWithFormat(
947 "<regex> can't be empty in 's%c<regex>%c<subst>%c' string: '%.*s'",
948 separator_char, separator_char, separator_char, (int)regex_sed.size(),
949 regex_sed.data());
950 return error;
951 } else if (second_separator_char_pos + 1 == third_separator_char_pos) {
952 error.SetErrorStringWithFormat(
953 "<subst> can't be empty in 's%c<regex>%c<subst>%c' string: '%.*s'",
954 separator_char, separator_char, separator_char, (int)regex_sed.size(),
955 regex_sed.data());
956 return error;
957 }
959 if (!check_only) {
960 std::string regex(std::string(regex_sed.substr(
961 first_separator_char_pos + 1,
962 second_separator_char_pos - first_separator_char_pos - 1)));
963 std::string subst(std::string(regex_sed.substr(
964 second_separator_char_pos + 1,
965 third_separator_char_pos - second_separator_char_pos - 1)));
966 m_regex_cmd_up->AddRegexCommand(regex, subst);
967 }
968 return error;
969 }
973 if (m_regex_cmd_up->HasRegexEntries()) {
975 m_interpreter.AddCommand(cmd_sp->GetCommandName(), cmd_sp, true);
977 }
978 }
979
980private:
981 std::unique_ptr<CommandObjectRegexCommand> m_regex_cmd_up;
982
983 class CommandOptions : public Options {
984 public:
985 CommandOptions() = default;
986
987 ~CommandOptions() override = default;
988
989 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
990 ExecutionContext *execution_context) override {
992 const int short_option = m_getopt_table[option_idx].val;
993
994 switch (short_option) {
995 case 'h':
996 m_help.assign(std::string(option_arg));
997 break;
998 case 's':
999 m_syntax.assign(std::string(option_arg));
1000 break;
1001 default:
1002 llvm_unreachable("Unimplemented option");
1003 }
1005 return error;
1007
1008 void OptionParsingStarting(ExecutionContext *execution_context) override {
1009 m_help.clear();
1010 m_syntax.clear();
1013 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
1014 return llvm::ArrayRef(g_regex_options);
1016
1017 llvm::StringRef GetHelp() { return m_help; }
1018
1019 llvm::StringRef GetSyntax() { return m_syntax; }
1020
1021 protected:
1022 // Instance variables to hold the values for command options.
1023
1024 std::string m_help;
1025 std::string m_syntax;
1026 };
1027
1028 Options *GetOptions() override { return &m_options; }
1029
1030 CommandOptions m_options;
1031};
1032
1034public:
1035 CommandObjectPythonFunction(CommandInterpreter &interpreter, std::string name,
1036 std::string funct, std::string help,
1038 CompletionType completion_type)
1039 : CommandObjectRaw(interpreter, name), m_function_name(funct),
1040 m_synchro(synch), m_completion_type(completion_type) {
1041 if (!help.empty())
1042 SetHelp(help);
1043 else {
1044 StreamString stream;
1045 stream.Printf("For more information run 'help %s'", name.c_str());
1046 SetHelp(stream.GetString());
1047 }
1048 }
1049
1050 ~CommandObjectPythonFunction() override = default;
1051
1052 bool IsRemovable() const override { return true; }
1053
1054 const std::string &GetFunctionName() { return m_function_name; }
1055
1057
1058 llvm::StringRef GetHelpLong() override {
1060 return CommandObjectRaw::GetHelpLong();
1061
1063 if (!scripter)
1064 return CommandObjectRaw::GetHelpLong();
1065
1066 std::string docstring;
1068 scripter->GetDocumentationForItem(m_function_name.c_str(), docstring);
1069 if (!docstring.empty())
1070 SetHelpLong(docstring);
1071 return CommandObjectRaw::GetHelpLong();
1072 }
1073
1074 void
1076 OptionElementVector &opt_element_vector) override {
1078 GetCommandInterpreter(), m_completion_type, request, nullptr);
1079 }
1080
1081 bool WantsCompletion() override { return true; }
1082
1083protected:
1084 void DoExecute(llvm::StringRef raw_command_line,
1085 CommandReturnObject &result) override {
1087
1089
1090 Status error;
1091
1093
1094 if (!scripter || !scripter->RunScriptBasedCommand(
1095 m_function_name.c_str(), raw_command_line, m_synchro,
1096 result, error, m_exe_ctx)) {
1097 result.AppendError(error.AsCString());
1098 } else {
1099 // Don't change the status if the command already set it...
1100 if (result.GetStatus() == eReturnStatusInvalid) {
1101 if (result.GetOutputData().empty())
1103 else
1105 }
1106 }
1107 }
1108
1109private:
1110 std::string m_function_name;
1114};
1115
1116/// This class implements a "raw" scripted command. lldb does no parsing of the
1117/// command line, instead passing the line unaltered (except for backtick
1118/// substitution).
1120public:
1122 std::string name,
1123 StructuredData::GenericSP cmd_obj_sp,
1125 CompletionType completion_type)
1126 : CommandObjectRaw(interpreter, name), m_cmd_obj_sp(cmd_obj_sp),
1127 m_synchro(synch), m_fetched_help_short(false),
1128 m_fetched_help_long(false), m_completion_type(completion_type) {
1129 StreamString stream;
1130 stream.Printf("For more information run 'help %s'", name.c_str());
1131 SetHelp(stream.GetString());
1132 if (ScriptInterpreter *scripter = GetDebugger().GetScriptInterpreter())
1133 GetFlags().Set(scripter->GetFlagsForCommandObject(cmd_obj_sp));
1134 }
1135
1137
1138 void
1140 OptionElementVector &opt_element_vector) override {
1142 GetCommandInterpreter(), m_completion_type, request, nullptr);
1143 }
1144
1145 bool WantsCompletion() override { return true; }
1146
1147 bool IsRemovable() const override { return true; }
1148
1150
1151 std::optional<std::string> GetRepeatCommand(Args &args,
1152 uint32_t index) override {
1154 if (!scripter)
1155 return std::nullopt;
1156
1157 return scripter->GetRepeatCommandForScriptedCommand(m_cmd_obj_sp, args);
1158 }
1159
1160 llvm::StringRef GetHelp() override {
1162 return CommandObjectRaw::GetHelp();
1164 if (!scripter)
1165 return CommandObjectRaw::GetHelp();
1166 std::string docstring;
1168 scripter->GetShortHelpForCommandObject(m_cmd_obj_sp, docstring);
1169 if (!docstring.empty())
1170 SetHelp(docstring);
1171
1172 return CommandObjectRaw::GetHelp();
1173 }
1174
1175 llvm::StringRef GetHelpLong() override {
1177 return CommandObjectRaw::GetHelpLong();
1178
1180 if (!scripter)
1181 return CommandObjectRaw::GetHelpLong();
1182
1183 std::string docstring;
1185 scripter->GetLongHelpForCommandObject(m_cmd_obj_sp, docstring);
1186 if (!docstring.empty())
1187 SetHelpLong(docstring);
1188 return CommandObjectRaw::GetHelpLong();
1189 }
1190
1191protected:
1192 void DoExecute(llvm::StringRef raw_command_line,
1193 CommandReturnObject &result) override {
1195
1196 Status error;
1197
1199
1200 if (!scripter ||
1201 !scripter->RunScriptBasedCommand(m_cmd_obj_sp, raw_command_line,
1202 m_synchro, result, error, m_exe_ctx)) {
1203 result.AppendError(error.AsCString());
1204 } else {
1205 // Don't change the status if the command already set it...
1206 if (result.GetStatus() == eReturnStatusInvalid) {
1207 if (result.GetOutputData().empty())
1209 else
1211 }
1212 }
1213 }
1214
1215private:
1221};
1222
1223
1224/// This command implements a lldb parsed scripted command. The command
1225/// provides a definition of the options and arguments, and a option value
1226/// setting callback, and then the command's execution function gets passed
1227/// just the parsed arguments.
1228/// Note, implementing a command in Python using these base interfaces is a bit
1229/// of a pain, but it is much easier to export this low level interface, and
1230/// then make it nicer on the Python side, than to try to do that in a
1231/// script language neutral way.
1232/// So I've also added a base class in Python that provides a table-driven
1233/// way of defining the options and arguments, which automatically fills the
1234/// option values, making them available as properties in Python.
1235///
1237private:
1238 class CommandOptions : public Options {
1239 public:
1241 StructuredData::GenericSP cmd_obj_sp) : m_interpreter(interpreter),
1242 m_cmd_obj_sp(cmd_obj_sp) {}
1243
1244 ~CommandOptions() override = default;
1245
1246 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1247 ExecutionContext *execution_context) override {
1248 Status error;
1249 ScriptInterpreter *scripter =
1251 if (!scripter) {
1252 error.SetErrorString("No script interpreter for SetOptionValue.");
1253 return error;
1254 }
1255 if (!m_cmd_obj_sp) {
1256 error.SetErrorString("SetOptionValue called with empty cmd_obj.");
1257 return error;
1258 }
1260 error.SetErrorString("SetOptionValue called before options definitions "
1261 "were created.");
1262 return error;
1263 }
1264 // Pass the long option, since you aren't actually required to have a
1265 // short_option, and for those options the index or short option character
1266 // aren't meaningful on the python side.
1267 const char * long_option =
1268 m_options_definition_up.get()[option_idx].long_option;
1269 bool success = scripter->SetOptionValueForCommandObject(m_cmd_obj_sp,
1270 execution_context, long_option, option_arg);
1271 if (!success)
1272 error.SetErrorStringWithFormatv("Error setting option: {0} to {1}",
1273 long_option, option_arg);
1274 return error;
1275 }
1276
1277 void OptionParsingStarting(ExecutionContext *execution_context) override {
1278 ScriptInterpreter *scripter =
1280 if (!scripter || !m_cmd_obj_sp)
1281 return;
1282
1284 }
1285
1286 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
1288 return {};
1289 return llvm::ArrayRef(m_options_definition_up.get(), m_num_options);
1290 }
1291
1293 size_t counter, uint32_t &usage_mask) {
1294 // If the usage entry is not provided, we use LLDB_OPT_SET_ALL.
1295 // If the usage mask is a UINT, the option belongs to that group.
1296 // If the usage mask is a vector of UINT's, the option belongs to all the
1297 // groups listed.
1298 // If a subelement of the vector is a vector of two ints, then the option
1299 // belongs to the inclusive range from the first to the second element.
1300 Status error;
1301 if (!obj_sp) {
1302 usage_mask = LLDB_OPT_SET_ALL;
1303 return error;
1304 }
1305
1306 usage_mask = 0;
1307
1309 obj_sp->GetAsUnsignedInteger();
1310 if (uint_val) {
1311 // If this is an integer, then this specifies a single group:
1312 uint32_t value = uint_val->GetValue();
1313 if (value == 0) {
1314 error.SetErrorStringWithFormatv(
1315 "0 is not a valid group for option {0}", counter);
1316 return error;
1317 }
1318 usage_mask = (1 << (value - 1));
1319 return error;
1320 }
1321 // Otherwise it has to be an array:
1322 StructuredData::Array *array_val = obj_sp->GetAsArray();
1323 if (!array_val) {
1324 error.SetErrorStringWithFormatv(
1325 "required field is not a array for option {0}", counter);
1326 return error;
1327 }
1328 // This is the array ForEach for accumulating a group usage mask from
1329 // an array of string descriptions of groups.
1330 auto groups_accumulator
1331 = [counter, &usage_mask, &error]
1332 (StructuredData::Object *obj) -> bool {
1334 if (int_val) {
1335 uint32_t value = int_val->GetValue();
1336 if (value == 0) {
1337 error.SetErrorStringWithFormatv(
1338 "0 is not a valid group for element {0}", counter);
1339 return false;
1340 }
1341 usage_mask |= (1 << (value - 1));
1342 return true;
1343 }
1344 StructuredData::Array *arr_val = obj->GetAsArray();
1345 if (!arr_val) {
1346 error.SetErrorStringWithFormatv(
1347 "Group element not an int or array of integers for element {0}",
1348 counter);
1349 return false;
1350 }
1351 size_t num_range_elem = arr_val->GetSize();
1352 if (num_range_elem != 2) {
1353 error.SetErrorStringWithFormatv(
1354 "Subranges of a group not a start and a stop for element {0}",
1355 counter);
1356 return false;
1357 }
1358 int_val = arr_val->GetItemAtIndex(0)->GetAsUnsignedInteger();
1359 if (!int_val) {
1360 error.SetErrorStringWithFormatv("Start element of a subrange of a "
1361 "group not unsigned int for element {0}", counter);
1362 return false;
1363 }
1364 uint32_t start = int_val->GetValue();
1365 int_val = arr_val->GetItemAtIndex(1)->GetAsUnsignedInteger();
1366 if (!int_val) {
1367 error.SetErrorStringWithFormatv("End element of a subrange of a group"
1368 " not unsigned int for element {0}", counter);
1369 return false;
1370 }
1371 uint32_t end = int_val->GetValue();
1372 if (start == 0 || end == 0 || start > end) {
1373 error.SetErrorStringWithFormatv("Invalid subrange of a group: {0} - "
1374 "{1} for element {2}", start, end, counter);
1375 return false;
1376 }
1377 for (uint32_t i = start; i <= end; i++) {
1378 usage_mask |= (1 << (i - 1));
1379 }
1380 return true;
1381 };
1382 array_val->ForEach(groups_accumulator);
1383 return error;
1384 }
1385
1386
1388 Status error;
1389 m_num_options = options.GetSize();
1391 // We need to hand out pointers to contents of these vectors; we reserve
1392 // as much as we'll need up front so they don't get freed on resize...
1396
1397 size_t counter = 0;
1398 size_t short_opt_counter = 0;
1399 // This is the Array::ForEach function for adding option elements:
1400 auto add_element = [this, &error, &counter, &short_opt_counter]
1401 (llvm::StringRef long_option, StructuredData::Object *object) -> bool {
1402 StructuredData::Dictionary *opt_dict = object->GetAsDictionary();
1403 if (!opt_dict) {
1404 error.SetErrorString("Value in options dictionary is not a dictionary");
1405 return false;
1406 }
1407 OptionDefinition &option_def = m_options_definition_up.get()[counter];
1408
1409 // We aren't exposing the validator yet, set it to null
1410 option_def.validator = nullptr;
1411 // We don't require usage masks, so set it to one group by default:
1412 option_def.usage_mask = 1;
1413
1414 // Now set the fields of the OptionDefinition Array from the dictionary:
1415 //
1416 // Note that I don't check for unknown fields in the option dictionaries
1417 // so a scriptor can add extra elements that are helpful when they go to
1418 // do "set_option_value"
1419
1420 // Usage Mask:
1421 StructuredData::ObjectSP obj_sp = opt_dict->GetValueForKey("groups");
1422 if (obj_sp) {
1423 error = ParseUsageMaskFromArray(obj_sp, counter,
1424 option_def.usage_mask);
1425 if (error.Fail())
1426 return false;
1427 }
1428
1429 // Required:
1430 option_def.required = false;
1431 obj_sp = opt_dict->GetValueForKey("required");
1432 if (obj_sp) {
1433 StructuredData::Boolean *boolean_val = obj_sp->GetAsBoolean();
1434 if (!boolean_val) {
1435 error.SetErrorStringWithFormatv("'required' field is not a boolean "
1436 "for option {0}", counter);
1437 return false;
1438 }
1439 option_def.required = boolean_val->GetValue();
1440 }
1441
1442 // Short Option:
1443 int short_option;
1444 obj_sp = opt_dict->GetValueForKey("short_option");
1445 if (obj_sp) {
1446 // The value is a string, so pull the
1447 llvm::StringRef short_str = obj_sp->GetStringValue();
1448 if (short_str.empty()) {
1449 error.SetErrorStringWithFormatv("short_option field empty for "
1450 "option {0}", counter);
1451 return false;
1452 } else if (short_str.size() != 1) {
1453 error.SetErrorStringWithFormatv("short_option field has extra "
1454 "characters for option {0}", counter);
1455 return false;
1456 }
1457 short_option = (int) short_str[0];
1458 } else {
1459 // If the short option is not provided, then we need a unique value
1460 // less than the lowest printable ASCII character.
1461 short_option = short_opt_counter++;
1462 }
1463 option_def.short_option = short_option;
1464
1465 // Long Option is the key from the outer dict:
1466 if (long_option.empty()) {
1467 error.SetErrorStringWithFormatv("empty long_option for option {0}",
1468 counter);
1469 return false;
1470 }
1471 auto inserted = g_string_storer.insert(long_option.str());
1472 option_def.long_option = ((*(inserted.first)).data());
1473
1474 // Value Type:
1475 obj_sp = opt_dict->GetValueForKey("value_type");
1476 if (obj_sp) {
1478 = obj_sp->GetAsUnsignedInteger();
1479 if (!uint_val) {
1480 error.SetErrorStringWithFormatv("Value type must be an unsigned "
1481 "integer");
1482 return false;
1483 }
1484 uint64_t val_type = uint_val->GetValue();
1485 if (val_type >= eArgTypeLastArg) {
1486 error.SetErrorStringWithFormatv("Value type {0} beyond the "
1487 "CommandArgumentType bounds", val_type);
1488 return false;
1489 }
1490 option_def.argument_type = (CommandArgumentType) val_type;
1491 option_def.option_has_arg = true;
1492 } else {
1493 option_def.argument_type = eArgTypeNone;
1494 option_def.option_has_arg = false;
1495 }
1496
1497 // Completion Type:
1498 obj_sp = opt_dict->GetValueForKey("completion_type");
1499 if (obj_sp) {
1501 if (!uint_val) {
1502 error.SetErrorStringWithFormatv("Completion type must be an "
1503 "unsigned integer for option {0}", counter);
1504 return false;
1505 }
1506 uint64_t completion_type = uint_val->GetValue();
1507 if (completion_type > eCustomCompletion) {
1508 error.SetErrorStringWithFormatv("Completion type for option {0} "
1509 "beyond the CompletionType bounds", completion_type);
1510 return false;
1511 }
1512 option_def.completion_type = (CommandArgumentType) completion_type;
1513 } else
1514 option_def.completion_type = eNoCompletion;
1515
1516 // Usage Text:
1517 std::string usage_text;
1518 obj_sp = opt_dict->GetValueForKey("help");
1519 if (!obj_sp) {
1520 error.SetErrorStringWithFormatv("required usage missing from option "
1521 "{0}", counter);
1522 return false;
1523 }
1524 llvm::StringRef usage_stref;
1525 usage_stref = obj_sp->GetStringValue();
1526 if (usage_stref.empty()) {
1527 error.SetErrorStringWithFormatv("empty usage text for option {0}",
1528 counter);
1529 return false;
1530 }
1531 m_usage_container[counter] = usage_stref.str().c_str();
1532 option_def.usage_text = m_usage_container[counter].data();
1533
1534 // Enum Values:
1535
1536 obj_sp = opt_dict->GetValueForKey("enum_values");
1537 if (obj_sp) {
1538 StructuredData::Array *array = obj_sp->GetAsArray();
1539 if (!array) {
1540 error.SetErrorStringWithFormatv("enum values must be an array for "
1541 "option {0}", counter);
1542 return false;
1543 }
1544 size_t num_elem = array->GetSize();
1545 size_t enum_ctr = 0;
1546 m_enum_storage[counter] = std::vector<EnumValueStorage>(num_elem);
1547 std::vector<EnumValueStorage> &curr_elem = m_enum_storage[counter];
1548
1549 // This is the Array::ForEach function for adding enum elements:
1550 // Since there are only two fields to specify the enum, use a simple
1551 // two element array with value first, usage second.
1552 // counter is only used for reporting so I pass it by value here.
1553 auto add_enum = [&enum_ctr, &curr_elem, counter, &error]
1554 (StructuredData::Object *object) -> bool {
1555 StructuredData::Array *enum_arr = object->GetAsArray();
1556 if (!enum_arr) {
1557 error.SetErrorStringWithFormatv("Enum values for option {0} not "
1558 "an array", counter);
1559 return false;
1560 }
1561 size_t num_enum_elements = enum_arr->GetSize();
1562 if (num_enum_elements != 2) {
1563 error.SetErrorStringWithFormatv("Wrong number of elements: {0} "
1564 "for enum {1} in option {2}",
1565 num_enum_elements, enum_ctr, counter);
1566 return false;
1567 }
1568 // Enum Value:
1569 StructuredData::ObjectSP obj_sp = enum_arr->GetItemAtIndex(0);
1570 llvm::StringRef val_stref = obj_sp->GetStringValue();
1571 std::string value_cstr_str = val_stref.str().c_str();
1572
1573 // Enum Usage:
1574 obj_sp = enum_arr->GetItemAtIndex(1);
1575 if (!obj_sp) {
1576 error.SetErrorStringWithFormatv("No usage for enum {0} in option "
1577 "{1}", enum_ctr, counter);
1578 return false;
1579 }
1580 llvm::StringRef usage_stref = obj_sp->GetStringValue();
1581 std::string usage_cstr_str = usage_stref.str().c_str();
1582 curr_elem[enum_ctr] = EnumValueStorage(value_cstr_str,
1583 usage_cstr_str, enum_ctr);
1584
1585 enum_ctr++;
1586 return true;
1587 }; // end of add_enum
1588
1589 array->ForEach(add_enum);
1590 if (!error.Success())
1591 return false;
1592 // We have to have a vector of elements to set in the options, make
1593 // that here:
1594 for (auto &elem : curr_elem)
1595 m_enum_vector[counter].emplace_back(elem.element);
1596
1597 option_def.enum_values = llvm::ArrayRef(m_enum_vector[counter]);
1598 }
1599 counter++;
1600 return true;
1601 }; // end of add_element
1602
1603 options.ForEach(add_element);
1604 return error;
1605 }
1606
1607 size_t GetNumOptions() { return m_num_options; }
1608
1609 private:
1612 element.string_value = "value not set";
1613 element.usage = "usage not set";
1614 element.value = 0;
1615 }
1616
1617 EnumValueStorage(std::string in_str_val, std::string in_usage,
1618 size_t in_value) : value(std::move(in_str_val)), usage(std::move(in_usage)) {
1619 SetElement(in_value);
1620 }
1621
1623 usage(in.usage) {
1625 }
1626
1628 value = in.value;
1629 usage = in.usage;
1631 return *this;
1632 }
1633
1634 void SetElement(size_t in_value) {
1635 element.value = in_value;
1636 element.string_value = value.data();
1637 element.usage = usage.data();
1638 }
1639
1640 std::string value;
1641 std::string usage;
1643 };
1644 // We have to provide char * values for the long option, usage and enum
1645 // values, that's what the option definitions hold.
1646 // The long option strings are quite likely to be reused in other added
1647 // commands, so those are stored in a global set: g_string_storer.
1648 // But the usages are much less likely to be reused, so those are stored in
1649 // a vector in the command instance. It gets resized to the correct size
1650 // and then filled with null-terminated strings in the std::string, so the
1651 // are valid C-strings that won't move around.
1652 // The enum values and descriptions are treated similarly - these aren't
1653 // all that common so it's not worth the effort to dedup them.
1654 size_t m_num_options = 0;
1655 std::unique_ptr<OptionDefinition> m_options_definition_up;
1656 std::vector<std::vector<EnumValueStorage>> m_enum_storage;
1657 std::vector<std::vector<OptionEnumValueElement>> m_enum_vector;
1658 std::vector<std::string> m_usage_container;
1661 static std::unordered_set<std::string> g_string_storer;
1662 };
1663
1664public:
1666 std::string name,
1667 StructuredData::GenericSP cmd_obj_sp,
1669 CommandReturnObject &result) {
1671 interpreter, name, cmd_obj_sp, synch));
1672
1674 = static_cast<CommandObjectScriptingObjectParsed *>(new_cmd_sp.get());
1675 // Now check all the failure modes, and report if found.
1676 Status opt_error = parsed_cmd->GetOptionsError();
1677 Status arg_error = parsed_cmd->GetArgsError();
1678
1679 if (opt_error.Fail())
1680 result.AppendErrorWithFormat("failed to parse option definitions: %s",
1681 opt_error.AsCString());
1682 if (arg_error.Fail())
1683 result.AppendErrorWithFormat("%sfailed to parse argument definitions: %s",
1684 opt_error.Fail() ? ", also " : "",
1685 arg_error.AsCString());
1686
1687 if (!result.Succeeded())
1688 return {};
1689
1690 return new_cmd_sp;
1691 }
1692
1694 std::string name,
1695 StructuredData::GenericSP cmd_obj_sp,
1697 : CommandObjectParsed(interpreter, name.c_str()),
1698 m_cmd_obj_sp(cmd_obj_sp), m_synchro(synch),
1699 m_options(interpreter, cmd_obj_sp), m_fetched_help_short(false),
1700 m_fetched_help_long(false) {
1701 StreamString stream;
1703 if (!scripter) {
1704 m_options_error.SetErrorString("No script interpreter");
1705 return;
1706 }
1707
1708 // Set the flags:
1709 GetFlags().Set(scripter->GetFlagsForCommandObject(cmd_obj_sp));
1710
1711 // Now set up the options definitions from the options:
1712 StructuredData::ObjectSP options_object_sp
1713 = scripter->GetOptionsForCommandObject(cmd_obj_sp);
1714 // It's okay not to have an options dict.
1715 if (options_object_sp) {
1716 // The options come as a dictionary of dictionaries. The key of the
1717 // outer dict is the long option name (since that's required). The
1718 // value holds all the other option specification bits.
1719 StructuredData::Dictionary *options_dict
1720 = options_object_sp->GetAsDictionary();
1721 // but if it exists, it has to be an array.
1722 if (options_dict) {
1724 // If we got an error don't bother with the arguments...
1725 if (m_options_error.Fail())
1726 return;
1727 } else {
1728 m_options_error.SetErrorString("Options array not an array");
1729 return;
1730 }
1731 }
1732 // Then fetch the args. Since the arguments can have usage masks you need
1733 // an array of arrays.
1734 StructuredData::ObjectSP args_object_sp
1735 = scripter->GetArgumentsForCommandObject(cmd_obj_sp);
1736 if (args_object_sp) {
1737 StructuredData::Array *args_array = args_object_sp->GetAsArray();
1738 if (!args_array) {
1739 m_args_error.SetErrorString("Argument specification is not an array");
1740 return;
1741 }
1742 size_t counter = 0;
1743
1744 // This is the Array::ForEach function that handles the
1745 // CommandArgumentEntry arrays one by one:
1746 auto arg_array_adder = [this, &counter] (StructuredData::Object *object)
1747 -> bool {
1748 // This is the Array::ForEach function to add argument entries:
1749 CommandArgumentEntry this_entry;
1750 size_t elem_counter = 0;
1751 auto args_adder = [this, counter, &elem_counter, &this_entry]
1752 (StructuredData::Object *object) -> bool {
1753 // The arguments definition has three fields, the argument type, the
1754 // repeat and the usage mask.
1757 uint32_t arg_opt_set_association;
1758
1759 auto report_error = [this, elem_counter, counter]
1760 (const char *err_txt) -> bool {
1761 m_args_error.SetErrorStringWithFormatv("Element {0} of arguments "
1762 "list element {1}: %s.", elem_counter, counter, err_txt);
1763 return false;
1764 };
1765
1766 StructuredData::Dictionary *arg_dict = object->GetAsDictionary();
1767 if (!arg_dict) {
1768 report_error("is not a dictionary.");
1769 return false;
1770 }
1771 // Argument Type:
1773 = arg_dict->GetValueForKey("arg_type");
1774 if (obj_sp) {
1776 = obj_sp->GetAsUnsignedInteger();
1777 if (!uint_val) {
1778 report_error("value type must be an unsigned integer");
1779 return false;
1780 }
1781 uint64_t arg_type_int = uint_val->GetValue();
1782 if (arg_type_int >= eArgTypeLastArg) {
1783 report_error("value type beyond ArgumentRepetitionType bounds");
1784 return false;
1785 }
1786 arg_type = (CommandArgumentType) arg_type_int;
1787 }
1788 // Repeat Value:
1789 obj_sp = arg_dict->GetValueForKey("repeat");
1790 std::optional<ArgumentRepetitionType> repeat;
1791 if (obj_sp) {
1792 llvm::StringRef repeat_str = obj_sp->GetStringValue();
1793 if (repeat_str.empty()) {
1794 report_error("repeat value is empty");
1795 return false;
1796 }
1797 repeat = ArgRepetitionFromString(repeat_str);
1798 if (!repeat) {
1799 report_error("invalid repeat value");
1800 return false;
1801 }
1802 arg_repetition = *repeat;
1803 }
1804
1805 // Usage Mask:
1806 obj_sp = arg_dict->GetValueForKey("groups");
1808 counter, arg_opt_set_association);
1809 this_entry.emplace_back(arg_type, arg_repetition,
1810 arg_opt_set_association);
1811 elem_counter++;
1812 return true;
1813 };
1814 StructuredData::Array *args_array = object->GetAsArray();
1815 if (!args_array) {
1816 m_args_error.SetErrorStringWithFormatv("Argument definition element "
1817 "{0} is not an array", counter);
1818 }
1819
1820 args_array->ForEach(args_adder);
1821 if (m_args_error.Fail())
1822 return false;
1823 if (this_entry.empty()) {
1824 m_args_error.SetErrorStringWithFormatv("Argument definition element "
1825 "{0} is empty", counter);
1826 return false;
1827 }
1828 m_arguments.push_back(this_entry);
1829 counter++;
1830 return true;
1831 }; // end of arg_array_adder
1832 // Here we actually parse the args definition:
1833 args_array->ForEach(arg_array_adder);
1834 }
1835 }
1836
1838
1841 bool WantsCompletion() override { return true; }
1842
1843 bool IsRemovable() const override { return true; }
1844
1846
1847 std::optional<std::string> GetRepeatCommand(Args &args,
1848 uint32_t index) override {
1850 if (!scripter)
1851 return std::nullopt;
1852
1853 return scripter->GetRepeatCommandForScriptedCommand(m_cmd_obj_sp, args);
1854 }
1855
1856 llvm::StringRef GetHelp() override {
1858 return CommandObjectParsed::GetHelp();
1860 if (!scripter)
1861 return CommandObjectParsed::GetHelp();
1862 std::string docstring;
1864 scripter->GetShortHelpForCommandObject(m_cmd_obj_sp, docstring);
1865 if (!docstring.empty())
1866 SetHelp(docstring);
1867
1868 return CommandObjectParsed::GetHelp();
1869 }
1870
1871 llvm::StringRef GetHelpLong() override {
1873 return CommandObjectParsed::GetHelpLong();
1874
1876 if (!scripter)
1877 return CommandObjectParsed::GetHelpLong();
1878
1879 std::string docstring;
1881 scripter->GetLongHelpForCommandObject(m_cmd_obj_sp, docstring);
1882 if (!docstring.empty())
1883 SetHelpLong(docstring);
1884 return CommandObjectParsed::GetHelpLong();
1885 }
1886
1887 Options *GetOptions() override {
1888 // CommandObjectParsed requires that a command with no options return
1889 // nullptr.
1890 if (m_options.GetNumOptions() == 0)
1891 return nullptr;
1892 return &m_options;
1893 }
1894
1895protected:
1896 void DoExecute(Args &args,
1897 CommandReturnObject &result) override {
1899
1900 Status error;
1901
1903
1904 if (!scripter ||
1906 m_synchro, result, error, m_exe_ctx)) {
1907 result.AppendError(error.AsCString());
1908 } else {
1909 // Don't change the status if the command already set it...
1910 if (result.GetStatus() == eReturnStatusInvalid) {
1911 if (result.GetOutputData().empty())
1913 else
1915 }
1916 }
1917 }
1918
1919private:
1927};
1928
1929std::unordered_set<std::string>
1931
1932// CommandObjectCommandsScriptImport
1933#define LLDB_OPTIONS_script_import
1934#include "CommandOptions.inc"
1935
1937public:
1939 : CommandObjectParsed(interpreter, "command script import",
1940 "Import a scripting module in LLDB.", nullptr) {
1942 }
1943
1945
1946 Options *GetOptions() override { return &m_options; }
1947
1948protected:
1949 class CommandOptions : public Options {
1950 public:
1951 CommandOptions() = default;
1952
1953 ~CommandOptions() override = default;
1954
1955 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1956 ExecutionContext *execution_context) override {
1957 Status error;
1958 const int short_option = m_getopt_table[option_idx].val;
1959
1960 switch (short_option) {
1961 case 'r':
1962 // NO-OP
1963 break;
1964 case 'c':
1966 break;
1967 case 's':
1968 silent = true;
1969 break;
1970 default:
1971 llvm_unreachable("Unimplemented option");
1972 }
1973
1974 return error;
1975 }
1976
1977 void OptionParsingStarting(ExecutionContext *execution_context) override {
1979 }
1980
1981 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
1982 return llvm::ArrayRef(g_script_import_options);
1983 }
1985 bool silent = false;
1986 };
1987
1988 void DoExecute(Args &command, CommandReturnObject &result) override {
1989 if (command.empty()) {
1990 result.AppendError("command script import needs one or more arguments");
1991 return;
1992 }
1993
1994 FileSpec source_dir = {};
1997 if (!source_dir) {
1998 result.AppendError("command script import -c can only be specified "
1999 "from a command file");
2000 return;
2001 }
2002 }
2003
2004 for (auto &entry : command.entries()) {
2005 Status error;
2006
2007 LoadScriptOptions options;
2008 options.SetInitSession(true);
2009 options.SetSilent(m_options.silent);
2010
2011 // FIXME: this is necessary because CommandObject::CheckRequirements()
2012 // assumes that commands won't ever be recursively invoked, but it's
2013 // actually possible to craft a Python script that does other "command
2014 // script imports" in __lldb_init_module the real fix is to have
2015 // recursive commands possible with a CommandInvocation object separate
2016 // from the CommandObject itself, so that recursive command invocations
2017 // won't stomp on each other (wrt to execution contents, options, and
2018 // more)
2019 m_exe_ctx.Clear();
2020 if (GetDebugger().GetScriptInterpreter()->LoadScriptingModule(
2021 entry.c_str(), options, error, /*module_sp=*/nullptr,
2022 source_dir)) {
2024 } else {
2025 result.AppendErrorWithFormat("module importing failed: %s",
2026 error.AsCString());
2027 }
2028 }
2029 }
2030
2032};
2033
2034#define LLDB_OPTIONS_script_add
2035#include "CommandOptions.inc"
2036
2039public:
2041 : CommandObjectParsed(interpreter, "command script add",
2042 "Add a scripted function as an LLDB command.",
2043 "Add a scripted function as an lldb command. "
2044 "If you provide a single argument, the command "
2045 "will be added at the root level of the command "
2046 "hierarchy. If there are more arguments they "
2047 "must be a path to a user-added container "
2048 "command, and the last element will be the new "
2049 "command name."),
2052 }
2053
2055
2056 Options *GetOptions() override { return &m_options; }
2057
2058 void
2060 OptionElementVector &opt_element_vector) override {
2062 opt_element_vector);
2063 }
2064
2065protected:
2066 class CommandOptions : public Options {
2067 public:
2068 CommandOptions() = default;
2069
2070 ~CommandOptions() override = default;
2071
2072 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
2073 ExecutionContext *execution_context) override {
2074 Status error;
2075 const int short_option = m_getopt_table[option_idx].val;
2076
2077 switch (short_option) {
2078 case 'f':
2079 if (!option_arg.empty())
2080 m_funct_name = std::string(option_arg);
2081 break;
2082 case 'c':
2083 if (!option_arg.empty())
2084 m_class_name = std::string(option_arg);
2085 break;
2086 case 'h':
2087 if (!option_arg.empty())
2088 m_short_help = std::string(option_arg);
2089 break;
2090 case 'o':
2092 break;
2093 case 'p':
2094 m_parsed_command = true;
2095 break;
2096 case 's':
2099 option_arg, GetDefinitions()[option_idx].enum_values, 0, error);
2100 if (!error.Success())
2101 error.SetErrorStringWithFormat(
2102 "unrecognized value for synchronicity '%s'",
2103 option_arg.str().c_str());
2104 break;
2105 case 'C': {
2106 Status error;
2107 OptionDefinition definition = GetDefinitions()[option_idx];
2108 lldb::CompletionType completion_type =
2110 option_arg, definition.enum_values, eNoCompletion, error));
2111 if (!error.Success())
2112 error.SetErrorStringWithFormat(
2113 "unrecognized value for command completion type '%s'",
2114 option_arg.str().c_str());
2115 m_completion_type = completion_type;
2116 } break;
2117 default:
2118 llvm_unreachable("Unimplemented option");
2119 }
2120
2121 return error;
2122 }
2123
2124 void OptionParsingStarting(ExecutionContext *execution_context) override {
2125 m_class_name.clear();
2126 m_funct_name.clear();
2127 m_short_help.clear();
2131 m_parsed_command = false;
2132 }
2133
2134 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
2135 return llvm::ArrayRef(g_script_add_options);
2136 }
2137
2138 // Instance variables to hold the values for command options.
2139
2140 std::string m_class_name;
2141 std::string m_funct_name;
2142 std::string m_short_help;
2147 bool m_parsed_command = false;
2148 };
2149
2150 void IOHandlerActivated(IOHandler &io_handler, bool interactive) override {
2151 StreamFileSP output_sp(io_handler.GetOutputStreamFileSP());
2152 if (output_sp && interactive) {
2153 output_sp->PutCString(g_python_command_instructions);
2154 output_sp->Flush();
2155 }
2156 }
2157
2159 std::string &data) override {
2160 StreamFileSP error_sp = io_handler.GetErrorStreamFileSP();
2161
2163 if (interpreter) {
2164 StringList lines;
2165 lines.SplitIntoLines(data);
2166 if (lines.GetSize() > 0) {
2167 std::string funct_name_str;
2168 if (interpreter->GenerateScriptAliasFunction(lines, funct_name_str)) {
2169 if (funct_name_str.empty()) {
2170 error_sp->Printf("error: unable to obtain a function name, didn't "
2171 "add python command.\n");
2172 error_sp->Flush();
2173 } else {
2174 // everything should be fine now, let's add this alias
2175
2177 m_interpreter, m_cmd_name, funct_name_str, m_short_help,
2179 if (!m_container) {
2181 m_cmd_name, command_obj_sp, m_overwrite);
2182 if (error.Fail()) {
2183 error_sp->Printf("error: unable to add selected command: '%s'",
2184 error.AsCString());
2185 error_sp->Flush();
2186 }
2187 } else {
2188 llvm::Error llvm_error = m_container->LoadUserSubcommand(
2189 m_cmd_name, command_obj_sp, m_overwrite);
2190 if (llvm_error) {
2191 error_sp->Printf("error: unable to add selected command: '%s'",
2192 llvm::toString(std::move(llvm_error)).c_str());
2193 error_sp->Flush();
2194 }
2195 }
2196 }
2197 } else {
2198 error_sp->Printf(
2199 "error: unable to create function, didn't add python command\n");
2200 error_sp->Flush();
2201 }
2202 } else {
2203 error_sp->Printf("error: empty function, didn't add python command\n");
2204 error_sp->Flush();
2205 }
2206 } else {
2207 error_sp->Printf(
2208 "error: script interpreter missing, didn't add python command\n");
2209 error_sp->Flush();
2210 }
2211
2212 io_handler.SetIsDone(true);
2213 }
2214
2215 void DoExecute(Args &command, CommandReturnObject &result) override {
2216 if (GetDebugger().GetScriptLanguage() != lldb::eScriptLanguagePython) {
2217 result.AppendError("only scripting language supported for scripted "
2218 "commands is currently Python");
2219 return;
2220 }
2221
2222 if (command.GetArgumentCount() == 0) {
2223 result.AppendError("'command script add' requires at least one argument");
2224 return;
2225 }
2226 // Store the options in case we get multi-line input, also figure out the
2227 // default if not user supplied:
2228 switch (m_options.m_overwrite_lazy) {
2229 case eLazyBoolCalculate:
2231 break;
2232 case eLazyBoolYes:
2233 m_overwrite = true;
2234 break;
2235 case eLazyBoolNo:
2236 m_overwrite = false;
2237 }
2238
2239 Status path_error;
2241 command, true, path_error);
2242
2243 if (path_error.Fail()) {
2244 result.AppendErrorWithFormat("error in command path: %s",
2245 path_error.AsCString());
2246 return;
2247 }
2248
2249 if (!m_container) {
2250 // This is getting inserted into the root of the interpreter.
2251 m_cmd_name = std::string(command[0].ref());
2252 } else {
2253 size_t num_args = command.GetArgumentCount();
2254 m_cmd_name = std::string(command[num_args - 1].ref());
2255 }
2256
2260
2261 // Handle the case where we prompt for the script code first:
2262 if (m_options.m_class_name.empty() && m_options.m_funct_name.empty()) {
2264 *this); // IOHandlerDelegate
2265 return;
2266 }
2267
2268 CommandObjectSP new_cmd_sp;
2269 if (m_options.m_class_name.empty()) {
2270 new_cmd_sp.reset(new CommandObjectPythonFunction(
2273 } else {
2275 if (!interpreter) {
2276 result.AppendError("cannot find ScriptInterpreter");
2277 return;
2278 }
2279
2280 auto cmd_obj_sp = interpreter->CreateScriptCommandObject(
2281 m_options.m_class_name.c_str());
2282 if (!cmd_obj_sp) {
2283 result.AppendErrorWithFormatv("cannot create helper object for: "
2284 "'{0}'", m_options.m_class_name);
2285 return;
2286 }
2287
2290 m_cmd_name, cmd_obj_sp, m_synchronicity, result);
2291 if (!result.Succeeded())
2292 return;
2293 } else
2294 new_cmd_sp.reset(new CommandObjectScriptingObjectRaw(
2297 }
2298
2299 // Assume we're going to succeed...
2301 if (!m_container) {
2302 Status add_error =
2304 if (add_error.Fail())
2305 result.AppendErrorWithFormat("cannot add command: %s",
2306 add_error.AsCString());
2307 } else {
2308 llvm::Error llvm_error =
2310 if (llvm_error)
2311 result.AppendErrorWithFormat(
2312 "cannot add command: %s",
2313 llvm::toString(std::move(llvm_error)).c_str());
2314 }
2315 }
2316
2318 std::string m_cmd_name;
2320 std::string m_short_help;
2321 bool m_overwrite = false;
2325};
2326
2327// CommandObjectCommandsScriptList
2328
2330public:
2332 : CommandObjectParsed(interpreter, "command script list",
2333 "List defined top-level scripted commands.",
2334 nullptr) {}
2335
2337
2338 void DoExecute(Args &command, CommandReturnObject &result) override {
2340
2342 }
2343};
2344
2345// CommandObjectCommandsScriptClear
2346
2348public:
2350 : CommandObjectParsed(interpreter, "command script clear",
2351 "Delete all scripted commands.", nullptr) {}
2352
2354
2355protected:
2356 void DoExecute(Args &command, CommandReturnObject &result) override {
2358
2360 }
2361};
2362
2363// CommandObjectCommandsScriptDelete
2364
2366public:
2369 interpreter, "command script delete",
2370 "Delete a scripted command by specifying the path to the command.",
2371 nullptr) {
2373 }
2374
2376
2377 void
2379 OptionElementVector &opt_element_vector) override {
2381 m_interpreter, request, opt_element_vector);
2382 }
2383
2384protected:
2385 void DoExecute(Args &command, CommandReturnObject &result) override {
2386
2387 llvm::StringRef root_cmd = command[0].ref();
2388 size_t num_args = command.GetArgumentCount();
2389
2390 if (root_cmd.empty()) {
2391 result.AppendErrorWithFormat("empty root command name");
2392 return;
2393 }
2396 result.AppendErrorWithFormat("can only delete user defined commands, "
2397 "but no user defined commands found");
2398 return;
2399 }
2400
2402 if (!cmd_sp) {
2403 result.AppendErrorWithFormat("command '%s' not found.",
2404 command[0].c_str());
2405 return;
2406 }
2407 if (!cmd_sp->IsUserCommand()) {
2408 result.AppendErrorWithFormat("command '%s' is not a user command.",
2409 command[0].c_str());
2410 return;
2411 }
2412 if (cmd_sp->GetAsMultiwordCommand() && num_args == 1) {
2413 result.AppendErrorWithFormat("command '%s' is a multi-word command.\n "
2414 "Delete with \"command container delete\"",
2415 command[0].c_str());
2416 return;
2417 }
2418
2419 if (command.GetArgumentCount() == 1) {
2420 m_interpreter.RemoveUser(root_cmd);
2422 return;
2423 }
2424 // We're deleting a command from a multiword command. Verify the command
2425 // path:
2426 Status error;
2427 CommandObjectMultiword *container =
2429 error);
2430 if (error.Fail()) {
2431 result.AppendErrorWithFormat("could not resolve command path: %s",
2432 error.AsCString());
2433 return;
2434 }
2435 if (!container) {
2436 // This means that command only had a leaf command, so the container is
2437 // the root. That should have been handled above.
2438 result.AppendErrorWithFormat("could not find a container for '%s'",
2439 command[0].c_str());
2440 return;
2441 }
2442 const char *leaf_cmd = command[num_args - 1].c_str();
2443 llvm::Error llvm_error =
2444 container->RemoveUserSubcommand(leaf_cmd,
2445 /* multiword not okay */ false);
2446 if (llvm_error) {
2447 result.AppendErrorWithFormat(
2448 "could not delete command '%s': %s", leaf_cmd,
2449 llvm::toString(std::move(llvm_error)).c_str());
2450 return;
2451 }
2452
2453 Stream &out_stream = result.GetOutputStream();
2454
2455 out_stream << "Deleted command:";
2456 for (size_t idx = 0; idx < num_args; idx++) {
2457 out_stream << ' ';
2458 out_stream << command[idx].c_str();
2459 }
2460 out_stream << '\n';
2462 }
2463};
2464
2465#pragma mark CommandObjectMultiwordCommandsScript
2466
2467// CommandObjectMultiwordCommandsScript
2468
2470public:
2473 interpreter, "command script",
2474 "Commands for managing custom "
2475 "commands implemented by "
2476 "interpreter scripts.",
2477 "command script <subcommand> [<subcommand-options>]") {
2479 new CommandObjectCommandsScriptAdd(interpreter)));
2481 "delete",
2484 "clear",
2487 interpreter)));
2489 "import",
2491 }
2492
2494};
2495
2496#pragma mark CommandObjectCommandContainer
2497#define LLDB_OPTIONS_container_add
2498#include "CommandOptions.inc"
2499
2501public:
2504 interpreter, "command container add",
2505 "Add a container command to lldb. Adding to built-"
2506 "in container commands is not allowed.",
2507 "command container add [[path1]...] container-name") {
2509 }
2510
2512
2513 Options *GetOptions() override { return &m_options; }
2514
2515 void
2517 OptionElementVector &opt_element_vector) override {
2519 m_interpreter, request, opt_element_vector);
2520 }
2521
2522protected:
2523 class CommandOptions : public Options {
2524 public:
2525 CommandOptions() = default;
2526
2527 ~CommandOptions() override = default;
2528
2529 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
2530 ExecutionContext *execution_context) override {
2531 Status error;
2532 const int short_option = m_getopt_table[option_idx].val;
2533
2534 switch (short_option) {
2535 case 'h':
2536 if (!option_arg.empty())
2537 m_short_help = std::string(option_arg);
2538 break;
2539 case 'o':
2540 m_overwrite = true;
2541 break;
2542 case 'H':
2543 if (!option_arg.empty())
2544 m_long_help = std::string(option_arg);
2545 break;
2546 default:
2547 llvm_unreachable("Unimplemented option");
2548 }
2549
2550 return error;
2551 }
2552
2553 void OptionParsingStarting(ExecutionContext *execution_context) override {
2554 m_short_help.clear();
2555 m_long_help.clear();
2556 m_overwrite = false;
2557 }
2558
2559 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
2560 return llvm::ArrayRef(g_container_add_options);
2561 }
2562
2563 // Instance variables to hold the values for command options.
2564
2565 std::string m_short_help;
2566 std::string m_long_help;
2567 bool m_overwrite = false;
2568 };
2569 void DoExecute(Args &command, CommandReturnObject &result) override {
2570 size_t num_args = command.GetArgumentCount();
2571
2572 if (num_args == 0) {
2573 result.AppendError("no command was specified");
2574 return;
2575 }
2576
2577 if (num_args == 1) {
2578 // We're adding this as a root command, so use the interpreter.
2579 const char *cmd_name = command.GetArgumentAtIndex(0);
2580 auto cmd_sp = CommandObjectSP(new CommandObjectMultiword(
2581 GetCommandInterpreter(), cmd_name, m_options.m_short_help.c_str(),
2582 m_options.m_long_help.c_str()));
2583 cmd_sp->GetAsMultiwordCommand()->SetRemovable(true);
2585 cmd_name, cmd_sp, m_options.m_overwrite);
2586 if (add_error.Fail()) {
2587 result.AppendErrorWithFormat("error adding command: %s",
2588 add_error.AsCString());
2589 return;
2590 }
2592 return;
2593 }
2594
2595 // We're adding this to a subcommand, first find the subcommand:
2596 Status path_error;
2597 CommandObjectMultiword *add_to_me =
2599 path_error);
2600
2601 if (!add_to_me) {
2602 result.AppendErrorWithFormat("error adding command: %s",
2603 path_error.AsCString());
2604 return;
2605 }
2606
2607 const char *cmd_name = command.GetArgumentAtIndex(num_args - 1);
2608 auto cmd_sp = CommandObjectSP(new CommandObjectMultiword(
2609 GetCommandInterpreter(), cmd_name, m_options.m_short_help.c_str(),
2610 m_options.m_long_help.c_str()));
2611 llvm::Error llvm_error =
2612 add_to_me->LoadUserSubcommand(cmd_name, cmd_sp, m_options.m_overwrite);
2613 if (llvm_error) {
2614 result.AppendErrorWithFormat("error adding subcommand: %s",
2615 llvm::toString(std::move(llvm_error)).c_str());
2616 return;
2617 }
2618
2620 }
2621
2622private:
2624};
2625
2626#define LLDB_OPTIONS_multiword_delete
2627#include "CommandOptions.inc"
2629public:
2632 interpreter, "command container delete",
2633 "Delete a container command previously added to "
2634 "lldb.",
2635 "command container delete [[path1] ...] container-cmd") {
2637 }
2638
2640
2641 void
2643 OptionElementVector &opt_element_vector) override {
2645 m_interpreter, request, opt_element_vector);
2646 }
2647
2648protected:
2649 void DoExecute(Args &command, CommandReturnObject &result) override {
2650 size_t num_args = command.GetArgumentCount();
2651
2652 if (num_args == 0) {
2653 result.AppendError("No command was specified.");
2654 return;
2655 }
2656
2657 if (num_args == 1) {
2658 // We're removing a root command, so we need to delete it from the
2659 // interpreter.
2660 const char *cmd_name = command.GetArgumentAtIndex(0);
2661 // Let's do a little more work here so we can do better error reporting.
2663 CommandObjectSP cmd_sp = interp.GetCommandSPExact(cmd_name);
2664 if (!cmd_sp) {
2665 result.AppendErrorWithFormat("container command %s doesn't exist.",
2666 cmd_name);
2667 return;
2668 }
2669 if (!cmd_sp->IsUserCommand()) {
2670 result.AppendErrorWithFormat(
2671 "container command %s is not a user command", cmd_name);
2672 return;
2673 }
2674 if (!cmd_sp->GetAsMultiwordCommand()) {
2675 result.AppendErrorWithFormat("command %s is not a container command",
2676 cmd_name);
2677 return;
2678 }
2679
2680 bool did_remove = GetCommandInterpreter().RemoveUserMultiword(cmd_name);
2681 if (!did_remove) {
2682 result.AppendErrorWithFormat("error removing command %s.", cmd_name);
2683 return;
2684 }
2685
2687 return;
2688 }
2689
2690 // We're removing a subcommand, first find the subcommand's owner:
2691 Status path_error;
2692 CommandObjectMultiword *container =
2694 path_error);
2695
2696 if (!container) {
2697 result.AppendErrorWithFormat("error removing container command: %s",
2698 path_error.AsCString());
2699 return;
2700 }
2701 const char *leaf = command.GetArgumentAtIndex(num_args - 1);
2702 llvm::Error llvm_error =
2703 container->RemoveUserSubcommand(leaf, /* multiword okay */ true);
2704 if (llvm_error) {
2705 result.AppendErrorWithFormat("error removing container command: %s",
2706 llvm::toString(std::move(llvm_error)).c_str());
2707 return;
2708 }
2710 }
2711};
2712
2714public:
2717 interpreter, "command container",
2718 "Commands for adding container commands to lldb. "
2719 "Container commands are containers for other commands. You can "
2720 "add nested container commands by specifying a command path, "
2721 "but you can't add commands into the built-in command hierarchy.",
2722 "command container <subcommand> [<subcommand-options>]") {
2724 interpreter)));
2726 "delete",
2728 }
2729
2731};
2732
2733#pragma mark CommandObjectMultiwordCommands
2734
2735// CommandObjectMultiwordCommands
2736
2738 CommandInterpreter &interpreter)
2739 : CommandObjectMultiword(interpreter, "command",
2740 "Commands for managing custom LLDB commands.",
2741 "command <subcommand> [<subcommand-options>]") {
2742 LoadSubCommand("source",
2744 LoadSubCommand("alias",
2747 new CommandObjectCommandsUnalias(interpreter)));
2748 LoadSubCommand("delete",
2751 interpreter)));
2753 "regex", CommandObjectSP(new CommandObjectCommandsAddRegex(interpreter)));
2755 "script",
2757}
2758
static const char * g_python_command_instructions
static llvm::raw_ostream & error(Stream &strm)
CommandObjectCommandContainer(CommandInterpreter &interpreter)
~CommandObjectCommandContainer() override=default
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
std::unique_ptr< CommandObjectRegexCommand > m_regex_cmd_up
void IOHandlerInputComplete(IOHandler &io_handler, std::string &data) override
Called when a line or lines have been retrieved.
~CommandObjectCommandsAddRegex() override=default
CommandObjectCommandsAddRegex(CommandInterpreter &interpreter)
Status AppendRegexSubstitution(const llvm::StringRef &regex_sed, bool check_only)
void DoExecute(Args &command, CommandReturnObject &result) override
void IOHandlerActivated(IOHandler &io_handler, bool interactive) override
void OptionParsingStarting(ExecutionContext *execution_context) override
Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_value, ExecutionContext *execution_context) override
llvm::ArrayRef< OptionDefinition > GetDefinitions() override
bool HandleAliasingNormalCommand(Args &args, CommandReturnObject &result)
void DoExecute(llvm::StringRef raw_command_line, CommandReturnObject &result) override
~CommandObjectCommandsAlias() override=default
bool HandleAliasingRawCommand(llvm::StringRef alias_command, llvm::StringRef raw_command_string, CommandObject &cmd_obj, CommandReturnObject &result)
CommandObjectCommandsAlias(CommandInterpreter &interpreter)
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.
~CommandObjectCommandsContainerAdd() override=default
void HandleArgumentCompletion(CompletionRequest &request, OptionElementVector &opt_element_vector) override
The default version handles argument definitions that have only one argument type,...
CommandObjectCommandsContainerAdd(CommandInterpreter &interpreter)
void DoExecute(Args &command, CommandReturnObject &result) override
CommandObjectCommandsContainerDelete(CommandInterpreter &interpreter)
void HandleArgumentCompletion(CompletionRequest &request, OptionElementVector &opt_element_vector) override
The default version handles argument definitions that have only one argument type,...
~CommandObjectCommandsContainerDelete() override=default
void DoExecute(Args &command, CommandReturnObject &result) override
CommandObjectCommandsDelete(CommandInterpreter &interpreter)
void DoExecute(Args &args, CommandReturnObject &result) override
void HandleArgumentCompletion(CompletionRequest &request, OptionElementVector &opt_element_vector) override
The default version handles argument definitions that have only one argument type,...
~CommandObjectCommandsDelete() override=default
Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, ExecutionContext *execution_context) override
Set the value of an option.
void OptionParsingStarting(ExecutionContext *execution_context) override
llvm::ArrayRef< OptionDefinition > GetDefinitions() override
void IOHandlerActivated(IOHandler &io_handler, bool interactive) override
void HandleArgumentCompletion(CompletionRequest &request, OptionElementVector &opt_element_vector) override
The default version handles argument definitions that have only one argument type,...
void DoExecute(Args &command, CommandReturnObject &result) override
~CommandObjectCommandsScriptAdd() override=default
void IOHandlerInputComplete(IOHandler &io_handler, std::string &data) override
Called when a line or lines have been retrieved.
ScriptedCommandSynchronicity m_synchronicity
CommandObjectCommandsScriptAdd(CommandInterpreter &interpreter)
CommandObjectCommandsScriptClear(CommandInterpreter &interpreter)
~CommandObjectCommandsScriptClear() override=default
void DoExecute(Args &command, CommandReturnObject &result) override
~CommandObjectCommandsScriptDelete() override=default
void DoExecute(Args &command, CommandReturnObject &result) override
CommandObjectCommandsScriptDelete(CommandInterpreter &interpreter)
void HandleArgumentCompletion(CompletionRequest &request, OptionElementVector &opt_element_vector) override
The default version handles argument definitions that have only one argument type,...
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.
llvm::ArrayRef< OptionDefinition > GetDefinitions() override
CommandObjectCommandsScriptImport(CommandInterpreter &interpreter)
void DoExecute(Args &command, CommandReturnObject &result) override
~CommandObjectCommandsScriptImport() override=default
void DoExecute(Args &command, CommandReturnObject &result) override
~CommandObjectCommandsScriptList() override=default
CommandObjectCommandsScriptList(CommandInterpreter &interpreter)
Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, ExecutionContext *execution_context) override
Set the value of an option.
void OptionParsingStarting(ExecutionContext *execution_context) override
llvm::ArrayRef< OptionDefinition > GetDefinitions() override
std::optional< std::string > GetRepeatCommand(Args &current_command_args, uint32_t index) override
Get the command that appropriate for a "repeat" of the current command.
CommandObjectCommandsSource(CommandInterpreter &interpreter)
void DoExecute(Args &command, CommandReturnObject &result) override
~CommandObjectCommandsSource() override=default
void HandleArgumentCompletion(CompletionRequest &request, OptionElementVector &opt_element_vector) override
The default version handles argument definitions that have only one argument type,...
void DoExecute(Args &args, CommandReturnObject &result) override
CommandObjectCommandsUnalias(CommandInterpreter &interpreter)
~CommandObjectCommandsUnalias() override=default
~CommandObjectMultiwordCommandsScript() override=default
CommandObjectMultiwordCommandsScript(CommandInterpreter &interpreter)
void DoExecute(llvm::StringRef raw_command_line, CommandReturnObject &result) override
void HandleArgumentCompletion(CompletionRequest &request, OptionElementVector &opt_element_vector) override
The default version handles argument definitions that have only one argument type,...
ScriptedCommandSynchronicity GetSynchronicity()
CommandObjectPythonFunction(CommandInterpreter &interpreter, std::string name, std::string funct, std::string help, ScriptedCommandSynchronicity synch, CompletionType completion_type)
ScriptedCommandSynchronicity m_synchro
llvm::StringRef GetHelpLong() override
~CommandObjectPythonFunction() override=default
static Status ParseUsageMaskFromArray(StructuredData::ObjectSP obj_sp, size_t counter, uint32_t &usage_mask)
std::unique_ptr< OptionDefinition > m_options_definition_up
void OptionParsingStarting(ExecutionContext *execution_context) override
static std::unordered_set< std::string > g_string_storer
llvm::ArrayRef< OptionDefinition > GetDefinitions() override
CommandOptions(CommandInterpreter &interpreter, StructuredData::GenericSP cmd_obj_sp)
Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, ExecutionContext *execution_context) override
Set the value of an option.
std::vector< std::vector< OptionEnumValueElement > > m_enum_vector
std::vector< std::vector< EnumValueStorage > > m_enum_storage
Status SetOptionsFromArray(StructuredData::Dictionary &options)
This command implements a lldb parsed scripted command.
CommandObjectScriptingObjectParsed(CommandInterpreter &interpreter, std::string name, StructuredData::GenericSP cmd_obj_sp, ScriptedCommandSynchronicity synch)
void DoExecute(Args &args, CommandReturnObject &result) override
std::optional< std::string > GetRepeatCommand(Args &args, uint32_t index) override
Get the command that appropriate for a "repeat" of the current command.
~CommandObjectScriptingObjectParsed() override=default
ScriptedCommandSynchronicity m_synchro
ScriptedCommandSynchronicity GetSynchronicity()
static CommandObjectSP Create(CommandInterpreter &interpreter, std::string name, StructuredData::GenericSP cmd_obj_sp, ScriptedCommandSynchronicity synch, CommandReturnObject &result)
This class implements a "raw" scripted command.
void HandleArgumentCompletion(CompletionRequest &request, OptionElementVector &opt_element_vector) override
The default version handles argument definitions that have only one argument type,...
StructuredData::GenericSP m_cmd_obj_sp
std::optional< std::string > GetRepeatCommand(Args &args, uint32_t index) override
Get the command that appropriate for a "repeat" of the current command.
llvm::StringRef GetHelpLong() override
ScriptedCommandSynchronicity m_synchro
~CommandObjectScriptingObjectRaw() override=default
CommandObjectScriptingObjectRaw(CommandInterpreter &interpreter, std::string name, StructuredData::GenericSP cmd_obj_sp, ScriptedCommandSynchronicity synch, CompletionType completion_type)
ScriptedCommandSynchronicity GetSynchronicity()
void DoExecute(llvm::StringRef raw_command_line, CommandReturnObject &result) override
A command line argument class.
Definition: Args.h:33
void Shift()
Shifts the first argument C string value of the array off the argument array.
Definition: Args.cpp:285
size_t GetArgumentCount() const
Gets the number of arguments left in this command object.
Definition: Args.h:116
llvm::ArrayRef< ArgEntry > entries() const
Definition: Args.h:128
const char * GetArgumentAtIndex(size_t idx) const
Gets the NULL terminated C string argument pointer for the argument at index idx.
Definition: Args.cpp:263
bool GetCommandString(std::string &command) const
Definition: Args.cpp:211
bool empty() const
Definition: Args.h:118
static bool InvokeCommonCompletionCallbacks(CommandInterpreter &interpreter, uint32_t completion_mask, lldb_private::CompletionRequest &request, SearchFilter *searcher)
static void CompleteModifiableCmdPathArgs(CommandInterpreter &interpreter, CompletionRequest &request, OptionElementVector &opt_element_vector)
This completer works for commands whose only arguments are a command path.
void SetStopOnContinue(bool stop_on_continue)
bool UserMultiwordCommandExists(llvm::StringRef cmd) const
Determine whether a root-level user multiword command with this name exists.
void IncreaseCommandUsage(const CommandObject &cmd_obj)
bool RemoveAlias(llvm::StringRef alias_name)
CommandAlias * AddAlias(llvm::StringRef alias_name, lldb::CommandObjectSP &command_obj_sp, llvm::StringRef args_string=llvm::StringRef())
CommandObject * GetCommandObject(llvm::StringRef cmd, StringList *matches=nullptr, StringList *descriptions=nullptr) const
ExecutionContext GetExecutionContext() const
CommandObjectMultiword * VerifyUserMultiwordCmdPath(Args &path, bool leaf_is_command, Status &result)
Look up the command pointed to by path encoded in the arguments of the incoming command object.
Status AddUserCommand(llvm::StringRef name, const lldb::CommandObjectSP &cmd_sp, bool can_replace)
bool AddCommand(llvm::StringRef name, const lldb::CommandObjectSP &cmd_sp, bool can_replace)
bool AliasExists(llvm::StringRef cmd) const
Determine whether an alias command with this name exists.
void GetHelp(CommandReturnObject &result, uint32_t types=eCommandTypesAllThem)
bool CommandExists(llvm::StringRef cmd) const
Determine whether a root level, built-in command with this name exists.
void GetPythonCommandsFromIOHandler(const char *prompt, IOHandlerDelegate &delegate, void *baton=nullptr)
lldb::CommandObjectSP GetCommandSPExact(llvm::StringRef cmd, bool include_aliases=false) const
bool RemoveUser(llvm::StringRef alias_name)
CommandObject * GetCommandObjectForCommand(llvm::StringRef &command_line)
bool RemoveUserMultiword(llvm::StringRef multiword_name)
bool UserCommandExists(llvm::StringRef cmd) const
Determine whether a root-level user command with this name exists.
const CommandObject::CommandMap & GetCommands() const
void HandleCommandsFromFile(FileSpec &file, const ExecutionContext &context, const CommandInterpreterRunOptions &options, CommandReturnObject &result)
Execute a list of commands from a file.
bool RemoveCommand(llvm::StringRef cmd, bool force=false)
Remove a command if it is removable (python or regex command).
const CommandObject::CommandMap & GetAliases() const
static void GenerateAdditionalHelpAvenuesMessage(Stream *s, llvm::StringRef command, llvm::StringRef prefix, llvm::StringRef subcommand, bool include_upropos=true, bool include_type_lookup=true)
CommandObjectMultiwordCommands(CommandInterpreter &interpreter)
llvm::Error LoadUserSubcommand(llvm::StringRef cmd_name, const lldb::CommandObjectSP &command_obj, bool can_replace) override
bool LoadSubCommand(llvm::StringRef cmd_name, const lldb::CommandObjectSP &command_obj) override
llvm::Error RemoveUserSubcommand(llvm::StringRef cmd_name, bool multiword_okay)
std::vector< CommandArgumentData > CommandArgumentEntry
virtual void SetHelpLong(llvm::StringRef str)
virtual bool WantsRawCommandString()=0
void AddSimpleArgumentList(lldb::CommandArgumentType arg_type, ArgumentRepetitionType repetition_type=eArgRepeatPlain)
bool ParseOptionsAndNotify(Args &args, CommandReturnObject &result, OptionGroupOptions &group_options, ExecutionContext &exe_ctx)
llvm::StringRef GetCommandName() const
static std::optional< ArgumentRepetitionType > ArgRepetitionFromString(llvm::StringRef string)
virtual bool IsMultiwordObject()
ExecutionContext m_exe_ctx
std::vector< CommandArgumentEntry > m_arguments
CommandInterpreter & GetCommandInterpreter()
CommandInterpreter & m_interpreter
Flags & GetFlags()
The flags accessor.
virtual bool IsRemovable() const
virtual void SetHelp(llvm::StringRef str)
virtual lldb::CommandObjectSP GetSubcommandSP(llvm::StringRef sub_cmd, StringList *matches=nullptr)
void AppendErrorWithFormatv(const char *format, Args &&... args)
void void AppendError(llvm::StringRef in_string)
void AppendWarningWithFormat(const char *format,...) __attribute__((format(printf
void SetStatus(lldb::ReturnStatus status)
void AppendErrorWithFormat(const char *format,...) __attribute__((format(printf
lldb::ReturnStatus GetStatus() const
void void AppendWarning(llvm::StringRef in_string)
"lldb/Utility/ArgCompletionRequest.h"
void TryCompleteCurrentArg(llvm::StringRef completion, llvm::StringRef description="")
Adds a possible completion string if the completion would complete the current argument.
A class to manage flag bits.
Definition: Debugger.h:80
lldb::StreamSP GetAsyncOutputStream()
Definition: Debugger.cpp:1289
CommandInterpreter & GetCommandInterpreter()
Definition: Debugger.h:168
bool GetUseColor() const
Definition: Debugger.cpp:403
void RunIOHandlerAsync(const lldb::IOHandlerSP &reader_sp, bool cancel_top_handler=true)
Run the given IO handler and return immediately.
Definition: Debugger.cpp:1189
ScriptInterpreter * GetScriptInterpreter(bool can_create=true, std::optional< lldb::ScriptLanguage > language={})
Definition: Debugger.cpp:1684
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
void Clear()
Clear the object's state.
A file utility class.
Definition: FileSpec.h:56
bool IsRelative() const
Returns true if the filespec represents a relative path.
Definition: FileSpec.cpp:507
void MakeAbsolute(const FileSpec &dir)
Make the FileSpec absolute by treating it relative to dir.
Definition: FileSpec.cpp:530
void Resolve(llvm::SmallVectorImpl< char > &path)
Resolve path to make it canonical.
static FileSystem & Instance()
ValueType Set(ValueType mask)
Set one or more flags by logical OR'ing mask with the current flags.
Definition: Flags.h:73
A delegate class for use with IOHandler subclasses.
Definition: IOHandler.h:190
lldb::StreamFileSP GetErrorStreamFileSP()
Definition: IOHandler.cpp:107
lldb::StreamFileSP GetOutputStreamFileSP()
Definition: IOHandler.cpp:105
void SetIsDone(bool b)
Definition: IOHandler.h:86
LoadScriptOptions & SetInitSession(bool b)
LoadScriptOptions & SetSilent(bool b)
void Append(OptionGroup *group)
Append options from a OptionGroup class.
Definition: Options.cpp:755
Status SetValueFromString(llvm::StringRef value, VarSetOperationType op=eVarSetOperationAssign) override
const char * GetCurrentValue() const
Status SetCurrentValue(llvm::StringRef value)
A pair of an option list with a 'raw' string as a suffix.
Definition: Args.h:315
A command line option parsing protocol class.
Definition: Options.h:58
void NotifyOptionParsingStarting(ExecutionContext *execution_context)
Definition: Options.cpp:33
std::vector< Option > m_getopt_table
Definition: Options.h:198
virtual bool SetOptionValueForCommandObject(StructuredData::GenericSP cmd_obj_sp, ExecutionContext *exe_ctx, llvm::StringRef long_option, llvm::StringRef value)
virtual StructuredData::GenericSP CreateScriptCommandObject(const char *class_name)
virtual bool RunScriptBasedCommand(const char *impl_function, llvm::StringRef args, ScriptedCommandSynchronicity synchronicity, lldb_private::CommandReturnObject &cmd_retobj, Status &error, const lldb_private::ExecutionContext &exe_ctx)
virtual bool GenerateScriptAliasFunction(StringList &input, std::string &output)
virtual std::optional< std::string > GetRepeatCommandForScriptedCommand(StructuredData::GenericSP impl_obj_sp, Args &args)
virtual bool GetDocumentationForItem(const char *item, std::string &dest)
virtual bool RunScriptBasedParsedCommand(StructuredData::GenericSP impl_obj_sp, Args &args, ScriptedCommandSynchronicity synchronicity, lldb_private::CommandReturnObject &cmd_retobj, Status &error, const lldb_private::ExecutionContext &exe_ctx)
virtual StructuredData::ObjectSP GetOptionsForCommandObject(StructuredData::GenericSP cmd_obj_sp)
virtual StructuredData::ObjectSP GetArgumentsForCommandObject(StructuredData::GenericSP cmd_obj_sp)
virtual void OptionParsingStartedForCommandObject(StructuredData::GenericSP cmd_obj_sp)
virtual bool GetLongHelpForCommandObject(StructuredData::GenericSP cmd_obj_sp, std::string &dest)
virtual uint32_t GetFlagsForCommandObject(StructuredData::GenericSP cmd_obj_sp)
virtual bool GetShortHelpForCommandObject(StructuredData::GenericSP cmd_obj_sp, std::string &dest)
An error handling class.
Definition: Status.h:44
void SetErrorStringWithFormatv(const char *format, Args &&... args)
Definition: Status.h:169
bool Fail() const
Test for error condition.
Definition: Status.cpp:180
const char * AsCString(const char *default_error_str="unknown error") const
Get the error string associated with the current error.
Definition: Status.cpp:129
void SetErrorString(llvm::StringRef err_str)
Set the current error string to err_str.
Definition: Status.cpp:232
llvm::StringRef GetString() const
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:134
size_t SplitIntoLines(const std::string &lines)
Definition: StringList.cpp:152
size_t GetSize() const
Definition: StringList.cpp:74
ObjectSP GetItemAtIndex(size_t idx) const
bool ForEach(std::function< bool(Object *object)> const &foreach_callback) const
ObjectSP GetValueForKey(llvm::StringRef key) const
void ForEach(std::function< bool(llvm::StringRef key, Object *object)> const &callback) const
UnsignedInteger * GetAsUnsignedInteger()
std::shared_ptr< Generic > GenericSP
std::shared_ptr< Object > ObjectSP
#define LLDB_OPT_SET_ALL
Definition: lldb-defines.h:110
A class that represents a running process on the host machine.
std::vector< OptionArgElement > OptionElementVector
Definition: Options.h:43
std::shared_ptr< OptionArgVector > OptionArgVectorSP
Definition: Options.h:30
std::vector< std::tuple< std::string, int, std::string > > OptionArgVector
Definition: Options.h:29
Definition: SBAddress.h:15
@ eScriptLanguagePython
std::shared_ptr< lldb_private::IOHandler > IOHandlerSP
Definition: lldb-forward.h:359
std::shared_ptr< lldb_private::CommandObject > CommandObjectSP
Definition: lldb-forward.h:331
std::shared_ptr< lldb_private::Stream > StreamSP
Definition: lldb-forward.h:428
@ eReturnStatusFailed
@ eReturnStatusSuccessFinishResult
@ eReturnStatusInvalid
@ eReturnStatusSuccessFinishNoResult
@ eArgTypeSEDStylePair
@ eArgTypeFilename
@ eArgTypeCommandName
@ eArgTypeAliasName
@ eArgTypeAliasOptions
std::shared_ptr< lldb_private::StreamFile > StreamFileSP
Definition: lldb-forward.h:429
EnumValueStorage(std::string in_str_val, std::string in_usage, size_t in_value)
Used to build individual command argument lists.
Definition: CommandObject.h:93
static int64_t ToOptionEnum(llvm::StringRef s, const OptionEnumValues &enum_values, int32_t fail_value, Status &error)
OptionValidator * validator
If non-NULL, option is valid iff |validator->IsValid()|, otherwise always valid.
const char * long_option
Full name for this option.
const char * usage_text
Full text explaining what this options does and what (if any) argument to pass it.
bool required
This option is required (in the current usage level).
uint32_t completion_type
The kind of completion for this option.
int option_has_arg
no_argument, required_argument or optional_argument
uint32_t usage_mask
Used to mark options that can be used together.
lldb::CommandArgumentType argument_type
Type of argument this option takes.
OptionEnumValues enum_values
If not empty, an array of enum values.
int short_option
Single character for this option.