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

source code of lldb/source/Commands/CommandObjectCommands.cpp