1 //===-- CommandObjectCommands.cpp -------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 // Other libraries and framework includes
13 #include "llvm/ADT/StringRef.h"
16 #include "CommandObjectCommands.h"
17 #include "CommandObjectHelp.h"
18 #include "lldb/Core/Debugger.h"
19 #include "lldb/Core/IOHandler.h"
20 #include "lldb/Core/StringList.h"
21 #include "lldb/Interpreter/Args.h"
22 #include "lldb/Interpreter/CommandHistory.h"
23 #include "lldb/Interpreter/CommandInterpreter.h"
24 #include "lldb/Interpreter/CommandObjectRegexCommand.h"
25 #include "lldb/Interpreter/CommandReturnObject.h"
26 #include "lldb/Interpreter/OptionValueBoolean.h"
27 #include "lldb/Interpreter/OptionValueString.h"
28 #include "lldb/Interpreter/OptionValueUInt64.h"
29 #include "lldb/Interpreter/Options.h"
30 #include "lldb/Interpreter/ScriptInterpreter.h"
33 using namespace lldb_private;
35 //-------------------------------------------------------------------------
36 // CommandObjectCommandsSource
37 //-------------------------------------------------------------------------
39 static OptionDefinition g_history_options[] = {
41 { LLDB_OPT_SET_1, false, "count", 'c', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeUnsignedInteger, "How many history commands to print." },
42 { LLDB_OPT_SET_1, false, "start-index", 's', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeUnsignedInteger, "Index at which to start printing history commands (or end to mean tail mode)." },
43 { LLDB_OPT_SET_1, false, "end-index", 'e', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeUnsignedInteger, "Index at which to stop printing history commands." },
44 { LLDB_OPT_SET_2, false, "clear", 'C', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeBoolean, "Clears the current command history." },
48 class CommandObjectCommandsHistory : public CommandObjectParsed {
50 CommandObjectCommandsHistory(CommandInterpreter &interpreter)
51 : CommandObjectParsed(interpreter, "command history",
52 "Dump the history of commands in this session.",
56 ~CommandObjectCommandsHistory() override = default;
58 Options *GetOptions() override { return &m_options; }
61 class CommandOptions : public Options {
64 : Options(), m_start_idx(0), m_stop_idx(0), m_count(0), m_clear(false) {
67 ~CommandOptions() override = default;
69 Error SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
70 ExecutionContext *execution_context) override {
72 const int short_option = m_getopt_table[option_idx].val;
74 switch (short_option) {
76 error = m_count.SetValueFromString(option_arg, eVarSetOperationAssign);
79 if (option_arg == "end") {
80 m_start_idx.SetCurrentValue(UINT64_MAX);
81 m_start_idx.SetOptionWasSet();
83 error = m_start_idx.SetValueFromString(option_arg,
84 eVarSetOperationAssign);
88 m_stop_idx.SetValueFromString(option_arg, eVarSetOperationAssign);
91 m_clear.SetCurrentValue(true);
92 m_clear.SetOptionWasSet();
95 error.SetErrorStringWithFormat("unrecognized option '%c'",
103 void OptionParsingStarting(ExecutionContext *execution_context) override {
110 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
111 return llvm::makeArrayRef(g_history_options);
114 // Instance variables to hold the values for command options.
116 OptionValueUInt64 m_start_idx;
117 OptionValueUInt64 m_stop_idx;
118 OptionValueUInt64 m_count;
119 OptionValueBoolean m_clear;
122 bool DoExecute(Args &command, CommandReturnObject &result) override {
123 if (m_options.m_clear.GetCurrentValue() &&
124 m_options.m_clear.OptionWasSet()) {
125 m_interpreter.GetCommandHistory().Clear();
126 result.SetStatus(lldb::eReturnStatusSuccessFinishNoResult);
128 if (m_options.m_start_idx.OptionWasSet() &&
129 m_options.m_stop_idx.OptionWasSet() &&
130 m_options.m_count.OptionWasSet()) {
131 result.AppendError("--count, --start-index and --end-index cannot be "
132 "all specified in the same invocation");
133 result.SetStatus(lldb::eReturnStatusFailed);
135 std::pair<bool, uint64_t> start_idx(
136 m_options.m_start_idx.OptionWasSet(),
137 m_options.m_start_idx.GetCurrentValue());
138 std::pair<bool, uint64_t> stop_idx(
139 m_options.m_stop_idx.OptionWasSet(),
140 m_options.m_stop_idx.GetCurrentValue());
141 std::pair<bool, uint64_t> count(m_options.m_count.OptionWasSet(),
142 m_options.m_count.GetCurrentValue());
144 const CommandHistory &history(m_interpreter.GetCommandHistory());
146 if (start_idx.first && start_idx.second == UINT64_MAX) {
148 start_idx.second = history.GetSize() - count.second;
149 stop_idx.second = history.GetSize() - 1;
150 } else if (stop_idx.first) {
151 start_idx.second = stop_idx.second;
152 stop_idx.second = history.GetSize() - 1;
154 start_idx.second = 0;
155 stop_idx.second = history.GetSize() - 1;
158 if (!start_idx.first && !stop_idx.first && !count.first) {
159 start_idx.second = 0;
160 stop_idx.second = history.GetSize() - 1;
161 } else if (start_idx.first) {
163 stop_idx.second = start_idx.second + count.second - 1;
164 } else if (!stop_idx.first) {
165 stop_idx.second = history.GetSize() - 1;
167 } else if (stop_idx.first) {
169 if (stop_idx.second >= count.second)
170 start_idx.second = stop_idx.second - count.second + 1;
172 start_idx.second = 0;
174 } else /* if (count.first) */
176 start_idx.second = 0;
177 stop_idx.second = count.second - 1;
180 history.Dump(result.GetOutputStream(), start_idx.second,
184 return result.Succeeded();
187 CommandOptions m_options;
190 //-------------------------------------------------------------------------
191 // CommandObjectCommandsSource
192 //-------------------------------------------------------------------------
194 static OptionDefinition g_source_options[] = {
196 { LLDB_OPT_SET_ALL, false, "stop-on-error", 'e', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeBoolean, "If true, stop executing commands on error." },
197 { LLDB_OPT_SET_ALL, false, "stop-on-continue", 'c', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeBoolean, "If true, stop executing commands on continue." },
198 { LLDB_OPT_SET_ALL, false, "silent-run", 's', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeBoolean, "If true don't echo commands while executing." },
202 class CommandObjectCommandsSource : public CommandObjectParsed {
204 CommandObjectCommandsSource(CommandInterpreter &interpreter)
205 : CommandObjectParsed(
206 interpreter, "command source",
207 "Read and execute LLDB commands from the file <filename>.",
210 CommandArgumentEntry arg;
211 CommandArgumentData file_arg;
213 // Define the first (and only) variant of this arg.
214 file_arg.arg_type = eArgTypeFilename;
215 file_arg.arg_repetition = eArgRepeatPlain;
217 // There is only one variant this argument could be; put it into the
219 arg.push_back(file_arg);
221 // Push the data for the first argument into the m_arguments vector.
222 m_arguments.push_back(arg);
225 ~CommandObjectCommandsSource() override = default;
227 const char *GetRepeatCommand(Args ¤t_command_args,
228 uint32_t index) override {
232 int HandleArgumentCompletion(Args &input, int &cursor_index,
233 int &cursor_char_position,
234 OptionElementVector &opt_element_vector,
235 int match_start_point, int max_return_elements,
237 StringList &matches) override {
238 auto completion_str = input[cursor_index].ref;
239 completion_str = completion_str.take_front(cursor_char_position);
241 CommandCompletions::InvokeCommonCompletionCallbacks(
242 GetCommandInterpreter(), CommandCompletions::eDiskFileCompletion,
243 completion_str, match_start_point, max_return_elements, nullptr,
244 word_complete, matches);
245 return matches.GetSize();
248 Options *GetOptions() override { return &m_options; }
251 class CommandOptions : public Options {
254 : Options(), m_stop_on_error(true), m_silent_run(false),
255 m_stop_on_continue(true) {}
257 ~CommandOptions() override = default;
259 Error SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
260 ExecutionContext *execution_context) override {
262 const int short_option = m_getopt_table[option_idx].val;
264 switch (short_option) {
266 error = m_stop_on_error.SetValueFromString(option_arg);
270 error = m_stop_on_continue.SetValueFromString(option_arg);
274 error = m_silent_run.SetValueFromString(option_arg);
278 error.SetErrorStringWithFormat("unrecognized option '%c'",
286 void OptionParsingStarting(ExecutionContext *execution_context) override {
287 m_stop_on_error.Clear();
288 m_silent_run.Clear();
289 m_stop_on_continue.Clear();
292 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
293 return llvm::makeArrayRef(g_source_options);
296 // Instance variables to hold the values for command options.
298 OptionValueBoolean m_stop_on_error;
299 OptionValueBoolean m_silent_run;
300 OptionValueBoolean m_stop_on_continue;
303 bool DoExecute(Args &command, CommandReturnObject &result) override {
304 if (command.GetArgumentCount() != 1) {
305 result.AppendErrorWithFormat(
306 "'%s' takes exactly one executable filename argument.\n",
307 GetCommandName().str().c_str());
308 result.SetStatus(eReturnStatusFailed);
312 FileSpec cmd_file(command[0].ref, true);
313 ExecutionContext *exe_ctx = nullptr; // Just use the default context.
315 // If any options were set, then use them
316 if (m_options.m_stop_on_error.OptionWasSet() ||
317 m_options.m_silent_run.OptionWasSet() ||
318 m_options.m_stop_on_continue.OptionWasSet()) {
319 // Use user set settings
320 CommandInterpreterRunOptions options;
321 options.SetStopOnContinue(m_options.m_stop_on_continue.GetCurrentValue());
322 options.SetStopOnError(m_options.m_stop_on_error.GetCurrentValue());
323 options.SetEchoCommands(!m_options.m_silent_run.GetCurrentValue());
324 options.SetPrintResults(!m_options.m_silent_run.GetCurrentValue());
326 m_interpreter.HandleCommandsFromFile(cmd_file, exe_ctx, options, result);
328 // No options were set, inherit any settings from nested "command
330 // or set to sane default settings...
331 CommandInterpreterRunOptions options;
332 m_interpreter.HandleCommandsFromFile(cmd_file, exe_ctx, options, result);
334 return result.Succeeded();
337 CommandOptions m_options;
340 #pragma mark CommandObjectCommandsAlias
341 //-------------------------------------------------------------------------
342 // CommandObjectCommandsAlias
343 //-------------------------------------------------------------------------
345 static OptionDefinition g_alias_options[] = {
347 { LLDB_OPT_SET_ALL, false, "help", 'h', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeHelpText, "Help text for this command" },
348 { LLDB_OPT_SET_ALL, false, "long-help", 'H', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeHelpText, "Long help text for this command" },
352 static const char *g_python_command_instructions =
353 "Enter your Python command(s). Type 'DONE' to end.\n"
354 "You must define a Python function with this signature:\n"
355 "def my_command_impl(debugger, args, result, internal_dict):\n";
357 class CommandObjectCommandsAlias : public CommandObjectRaw {
359 class CommandOptions : public OptionGroup {
361 CommandOptions() : OptionGroup(), m_help(), m_long_help() {}
363 ~CommandOptions() override = default;
365 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
366 return llvm::makeArrayRef(g_alias_options);
369 Error SetOptionValue(uint32_t option_idx, llvm::StringRef option_value,
370 ExecutionContext *execution_context) override {
373 const int short_option = GetDefinitions()[option_idx].short_option;
374 std::string option_str(option_value);
376 switch (short_option) {
378 m_help.SetCurrentValue(option_str);
379 m_help.SetOptionWasSet();
383 m_long_help.SetCurrentValue(option_str);
384 m_long_help.SetOptionWasSet();
388 error.SetErrorStringWithFormat("invalid short option character '%c'",
396 void OptionParsingStarting(ExecutionContext *execution_context) override {
401 OptionValueString m_help;
402 OptionValueString m_long_help;
405 OptionGroupOptions m_option_group;
406 CommandOptions m_command_options;
409 Options *GetOptions() override { return &m_option_group; }
411 CommandObjectCommandsAlias(CommandInterpreter &interpreter)
413 interpreter, "command alias",
414 "Define a custom command in terms of an existing command."),
415 m_option_group(), m_command_options() {
416 m_option_group.Append(&m_command_options);
417 m_option_group.Finalize();
420 "'alias' allows the user to create a short-cut or abbreviation for long \
421 commands, multi-word commands, and commands that take particular options. \
422 Below are some simple examples of how one might use the 'alias' command:"
425 (lldb) command alias sc script
427 Creates the abbreviation 'sc' for the 'script' command.
429 (lldb) command alias bp breakpoint
432 " Creates the abbreviation 'bp' for the 'breakpoint' command. Since \
433 breakpoint commands are two-word commands, the user would still need to \
434 enter the second word after 'bp', e.g. 'bp enable' or 'bp delete'."
437 (lldb) command alias bpl breakpoint list
439 Creates the abbreviation 'bpl' for the two-word command 'breakpoint list'.
442 "An alias can include some options for the command, with the values either \
443 filled in at the time the alias is created, or specified as positional \
444 arguments, to be filled in when the alias is invoked. The following example \
445 shows how to create aliases with options:"
448 (lldb) command alias bfl breakpoint set -f %1 -l %2
451 " Creates the abbreviation 'bfl' (for break-file-line), with the -f and -l \
452 options already part of the alias. So if the user wants to set a breakpoint \
453 by file and line without explicitly having to use the -f and -l options, the \
454 user can now use 'bfl' instead. The '%1' and '%2' are positional placeholders \
455 for the actual arguments that will be passed when the alias command is used. \
456 The number in the placeholder refers to the position/order the actual value \
457 occupies when the alias is used. All the occurrences of '%1' in the alias \
458 will be replaced with the first argument, all the occurrences of '%2' in the \
459 alias will be replaced with the second argument, and so on. This also allows \
460 actual arguments to be used multiple times within an alias (see 'process \
461 launch' example below)."
465 "Note: the positional arguments must substitute as whole words in the resultant \
466 command, so you can't at present do something like this to append the file extension \
470 (lldb) command alias bcppfl breakpoint set -f %1.cpp -l %2
473 "For more complex aliasing, use the \"command regex\" command instead. In the \
474 'bfl' case above, the actual file value will be filled in with the first argument \
475 following 'bfl' and the actual line number value will be filled in with the second \
476 argument. The user would use this alias as follows:"
479 (lldb) command alias bfl breakpoint set -f %1 -l %2
480 (lldb) bfl my-file.c 137
482 This would be the same as if the user had entered 'breakpoint set -f my-file.c -l 137'.
486 (lldb) command alias pltty process launch -s -o %1 -e %1
487 (lldb) pltty /dev/tty0
489 Interpreted as 'process launch -s -o /dev/tty0 -e /dev/tty0'
492 "If the user always wanted to pass the same value to a particular option, the \
493 alias could be defined with that value directly in the alias as a constant, \
494 rather than using a positional placeholder:"
497 (lldb) command alias bl3 breakpoint set -f %1 -l 3
499 Always sets a breakpoint on line 3 of whatever file is indicated.)");
501 CommandArgumentEntry arg1;
502 CommandArgumentEntry arg2;
503 CommandArgumentEntry arg3;
504 CommandArgumentData alias_arg;
505 CommandArgumentData cmd_arg;
506 CommandArgumentData options_arg;
508 // Define the first (and only) variant of this arg.
509 alias_arg.arg_type = eArgTypeAliasName;
510 alias_arg.arg_repetition = eArgRepeatPlain;
512 // There is only one variant this argument could be; put it into the
514 arg1.push_back(alias_arg);
516 // Define the first (and only) variant of this arg.
517 cmd_arg.arg_type = eArgTypeCommandName;
518 cmd_arg.arg_repetition = eArgRepeatPlain;
520 // There is only one variant this argument could be; put it into the
522 arg2.push_back(cmd_arg);
524 // Define the first (and only) variant of this arg.
525 options_arg.arg_type = eArgTypeAliasOptions;
526 options_arg.arg_repetition = eArgRepeatOptional;
528 // There is only one variant this argument could be; put it into the
530 arg3.push_back(options_arg);
532 // Push the data for the first argument into the m_arguments vector.
533 m_arguments.push_back(arg1);
534 m_arguments.push_back(arg2);
535 m_arguments.push_back(arg3);
538 ~CommandObjectCommandsAlias() override = default;
541 bool DoExecute(const char *raw_command_line,
542 CommandReturnObject &result) override {
543 if (!raw_command_line || !raw_command_line[0]) {
544 result.AppendError("'command alias' requires at least two arguments");
548 ExecutionContext exe_ctx = GetCommandInterpreter().GetExecutionContext();
549 m_option_group.NotifyOptionParsingStarting(&exe_ctx);
551 const char *remainder = nullptr;
553 if (raw_command_line[0] == '-') {
554 // We have some options and these options MUST end with --.
555 const char *end_options = nullptr;
556 const char *s = raw_command_line;
558 end_options = ::strstr(s, "--");
560 end_options += 2; // Get past the "--"
561 if (::isspace(end_options[0])) {
562 remainder = end_options;
563 while (::isspace(*remainder))
573 llvm::StringRef(raw_command_line, end_options - raw_command_line));
574 if (!ParseOptions(args, result))
577 Error error(m_option_group.NotifyOptionParsingFinished(&exe_ctx));
579 result.AppendError(error.AsCString());
580 result.SetStatus(eReturnStatusFailed);
585 if (nullptr == remainder)
586 remainder = raw_command_line;
588 llvm::StringRef raw_command_string(remainder);
589 Args args(raw_command_string);
591 if (args.GetArgumentCount() < 2) {
592 result.AppendError("'command alias' requires at least two arguments");
593 result.SetStatus(eReturnStatusFailed);
597 // Get the alias command.
599 auto alias_command = args[0].ref;
600 if (alias_command.startswith("-")) {
601 result.AppendError("aliases starting with a dash are not supported");
602 if (alias_command == "--help" || alias_command == "--long-help") {
603 result.AppendWarning("if trying to pass options to 'command alias' add "
604 "a -- at the end of the options");
606 result.SetStatus(eReturnStatusFailed);
610 // Strip the new alias name off 'raw_command_string' (leave it on args,
611 // which gets passed to 'Execute', which
612 // does the stripping itself.
613 size_t pos = raw_command_string.find(alias_command);
615 raw_command_string = raw_command_string.substr(alias_command.size());
616 pos = raw_command_string.find_first_not_of(' ');
617 if ((pos != std::string::npos) && (pos > 0))
618 raw_command_string = raw_command_string.substr(pos);
620 result.AppendError("Error parsing command string. No alias created.");
621 result.SetStatus(eReturnStatusFailed);
625 // Verify that the command is alias-able.
626 if (m_interpreter.CommandExists(alias_command)) {
627 result.AppendErrorWithFormat(
628 "'%s' is a permanent debugger command and cannot be redefined.\n",
630 result.SetStatus(eReturnStatusFailed);
634 // Get CommandObject that is being aliased. The command name is read from
635 // the front of raw_command_string. raw_command_string is returned with the
636 // name of the command object stripped off the front.
637 llvm::StringRef original_raw_command_string = raw_command_string;
638 CommandObject *cmd_obj =
639 m_interpreter.GetCommandObjectForCommand(raw_command_string);
642 result.AppendErrorWithFormat("invalid command given to 'command alias'. "
643 "'%s' does not begin with a valid command."
644 " No alias created.",
645 original_raw_command_string.str().c_str());
646 result.SetStatus(eReturnStatusFailed);
648 } else if (!cmd_obj->WantsRawCommandString()) {
649 // Note that args was initialized with the original command, and has not
650 // been updated to this point.
651 // Therefore can we pass it to the version of Execute that does not
652 // need/expect raw input in the alias.
653 return HandleAliasingNormalCommand(args, result);
655 return HandleAliasingRawCommand(alias_command, raw_command_string,
658 return result.Succeeded();
661 bool HandleAliasingRawCommand(llvm::StringRef alias_command,
662 llvm::StringRef raw_command_string,
663 CommandObject &cmd_obj,
664 CommandReturnObject &result) {
665 // Verify & handle any options/arguments passed to the alias command
667 OptionArgVectorSP option_arg_vector_sp =
668 OptionArgVectorSP(new OptionArgVector);
670 if (CommandObjectSP cmd_obj_sp =
671 m_interpreter.GetCommandSPExact(cmd_obj.GetCommandName(), false)) {
672 if (m_interpreter.AliasExists(alias_command) ||
673 m_interpreter.UserCommandExists(alias_command)) {
674 result.AppendWarningWithFormat(
675 "Overwriting existing definition for '%s'.\n",
676 alias_command.str().c_str());
678 if (CommandAlias *alias = m_interpreter.AddAlias(
679 alias_command, cmd_obj_sp, raw_command_string)) {
680 if (m_command_options.m_help.OptionWasSet())
681 alias->SetHelp(m_command_options.m_help.GetCurrentValue());
682 if (m_command_options.m_long_help.OptionWasSet())
683 alias->SetHelpLong(m_command_options.m_long_help.GetCurrentValue());
684 result.SetStatus(eReturnStatusSuccessFinishNoResult);
686 result.AppendError("Unable to create requested alias.\n");
687 result.SetStatus(eReturnStatusFailed);
691 result.AppendError("Unable to create requested alias.\n");
692 result.SetStatus(eReturnStatusFailed);
695 return result.Succeeded();
698 bool HandleAliasingNormalCommand(Args &args, CommandReturnObject &result) {
699 size_t argc = args.GetArgumentCount();
702 result.AppendError("'command alias' requires at least two arguments");
703 result.SetStatus(eReturnStatusFailed);
707 // Save these in std::strings since we're going to shift them off.
708 const std::string alias_command(args[0].ref);
709 const std::string actual_command(args[1].ref);
711 args.Shift(); // Shift the alias command word off the argument vector.
712 args.Shift(); // Shift the old command word off the argument vector.
714 // Verify that the command is alias'able, and get the appropriate command
717 if (m_interpreter.CommandExists(alias_command)) {
718 result.AppendErrorWithFormat(
719 "'%s' is a permanent debugger command and cannot be redefined.\n",
720 alias_command.c_str());
721 result.SetStatus(eReturnStatusFailed);
725 CommandObjectSP command_obj_sp(
726 m_interpreter.GetCommandSPExact(actual_command, true));
727 CommandObjectSP subcommand_obj_sp;
728 bool use_subcommand = false;
729 if (!command_obj_sp) {
730 result.AppendErrorWithFormat("'%s' is not an existing command.\n",
731 actual_command.c_str());
732 result.SetStatus(eReturnStatusFailed);
735 CommandObject *cmd_obj = command_obj_sp.get();
736 CommandObject *sub_cmd_obj = nullptr;
737 OptionArgVectorSP option_arg_vector_sp =
738 OptionArgVectorSP(new OptionArgVector);
740 while (cmd_obj->IsMultiwordObject() && !args.empty()) {
741 auto sub_command = args[0].ref;
742 assert(!sub_command.empty());
743 subcommand_obj_sp = cmd_obj->GetSubcommandSP(sub_command);
744 if (!subcommand_obj_sp) {
745 result.AppendErrorWithFormat(
746 "'%s' is not a valid sub-command of '%s'. "
747 "Unable to create alias.\n",
748 args[0].c_str(), actual_command.c_str());
749 result.SetStatus(eReturnStatusFailed);
753 sub_cmd_obj = subcommand_obj_sp.get();
754 use_subcommand = true;
755 args.Shift(); // Shift the sub_command word off the argument vector.
756 cmd_obj = sub_cmd_obj;
759 // Verify & handle any options/arguments passed to the alias command
761 std::string args_string;
764 CommandObjectSP tmp_sp =
765 m_interpreter.GetCommandSPExact(cmd_obj->GetCommandName(), false);
767 tmp_sp = m_interpreter.GetCommandSPExact(sub_cmd_obj->GetCommandName(),
770 args.GetCommandString(args_string);
773 if (m_interpreter.AliasExists(alias_command) ||
774 m_interpreter.UserCommandExists(alias_command)) {
775 result.AppendWarningWithFormat(
776 "Overwriting existing definition for '%s'.\n", alias_command.c_str());
779 if (CommandAlias *alias = m_interpreter.AddAlias(
780 alias_command, use_subcommand ? subcommand_obj_sp : command_obj_sp,
782 if (m_command_options.m_help.OptionWasSet())
783 alias->SetHelp(m_command_options.m_help.GetCurrentValue());
784 if (m_command_options.m_long_help.OptionWasSet())
785 alias->SetHelpLong(m_command_options.m_long_help.GetCurrentValue());
786 result.SetStatus(eReturnStatusSuccessFinishNoResult);
788 result.AppendError("Unable to create requested alias.\n");
789 result.SetStatus(eReturnStatusFailed);
793 return result.Succeeded();
797 #pragma mark CommandObjectCommandsUnalias
798 //-------------------------------------------------------------------------
799 // CommandObjectCommandsUnalias
800 //-------------------------------------------------------------------------
802 class CommandObjectCommandsUnalias : public CommandObjectParsed {
804 CommandObjectCommandsUnalias(CommandInterpreter &interpreter)
805 : CommandObjectParsed(
806 interpreter, "command unalias",
807 "Delete one or more custom commands defined by 'command alias'.",
809 CommandArgumentEntry arg;
810 CommandArgumentData alias_arg;
812 // Define the first (and only) variant of this arg.
813 alias_arg.arg_type = eArgTypeAliasName;
814 alias_arg.arg_repetition = eArgRepeatPlain;
816 // There is only one variant this argument could be; put it into the
818 arg.push_back(alias_arg);
820 // Push the data for the first argument into the m_arguments vector.
821 m_arguments.push_back(arg);
824 ~CommandObjectCommandsUnalias() override = default;
827 bool DoExecute(Args &args, CommandReturnObject &result) override {
828 CommandObject::CommandMap::iterator pos;
829 CommandObject *cmd_obj;
832 result.AppendError("must call 'unalias' with a valid alias");
833 result.SetStatus(eReturnStatusFailed);
837 auto command_name = args[0].ref;
838 cmd_obj = m_interpreter.GetCommandObject(command_name);
840 result.AppendErrorWithFormat(
841 "'%s' is not a known command.\nTry 'help' to see a "
842 "current list of commands.\n",
844 result.SetStatus(eReturnStatusFailed);
848 if (m_interpreter.CommandExists(command_name)) {
849 if (cmd_obj->IsRemovable()) {
850 result.AppendErrorWithFormat(
851 "'%s' is not an alias, it is a debugger command which can be "
852 "removed using the 'command delete' command.\n",
855 result.AppendErrorWithFormat(
856 "'%s' is a permanent debugger command and cannot be removed.\n",
859 result.SetStatus(eReturnStatusFailed);
863 if (!m_interpreter.RemoveAlias(command_name)) {
864 if (m_interpreter.AliasExists(command_name))
865 result.AppendErrorWithFormat(
866 "Error occurred while attempting to unalias '%s'.\n",
869 result.AppendErrorWithFormat("'%s' is not an existing alias.\n",
871 result.SetStatus(eReturnStatusFailed);
875 result.SetStatus(eReturnStatusSuccessFinishNoResult);
876 return result.Succeeded();
880 #pragma mark CommandObjectCommandsDelete
881 //-------------------------------------------------------------------------
882 // CommandObjectCommandsDelete
883 //-------------------------------------------------------------------------
885 class CommandObjectCommandsDelete : public CommandObjectParsed {
887 CommandObjectCommandsDelete(CommandInterpreter &interpreter)
888 : CommandObjectParsed(
889 interpreter, "command delete",
890 "Delete one or more custom commands defined by 'command regex'.",
892 CommandArgumentEntry arg;
893 CommandArgumentData alias_arg;
895 // Define the first (and only) variant of this arg.
896 alias_arg.arg_type = eArgTypeCommandName;
897 alias_arg.arg_repetition = eArgRepeatPlain;
899 // There is only one variant this argument could be; put it into the
901 arg.push_back(alias_arg);
903 // Push the data for the first argument into the m_arguments vector.
904 m_arguments.push_back(arg);
907 ~CommandObjectCommandsDelete() override = default;
910 bool DoExecute(Args &args, CommandReturnObject &result) override {
911 CommandObject::CommandMap::iterator pos;
914 result.AppendErrorWithFormat("must call '%s' with one or more valid user "
915 "defined regular expression command names",
916 GetCommandName().str().c_str());
917 result.SetStatus(eReturnStatusFailed);
920 auto command_name = args[0].ref;
921 if (!m_interpreter.CommandExists(command_name)) {
922 StreamString error_msg_stream;
923 const bool generate_apropos = true;
924 const bool generate_type_lookup = false;
925 CommandObjectHelp::GenerateAdditionalHelpAvenuesMessage(
926 &error_msg_stream, command_name, llvm::StringRef(), llvm::StringRef(),
927 generate_apropos, generate_type_lookup);
928 result.AppendError(error_msg_stream.GetString());
929 result.SetStatus(eReturnStatusFailed);
933 if (!m_interpreter.RemoveCommand(command_name)) {
934 result.AppendErrorWithFormat(
935 "'%s' is a permanent debugger command and cannot be removed.\n",
937 result.SetStatus(eReturnStatusFailed);
941 result.SetStatus(eReturnStatusSuccessFinishNoResult);
946 //-------------------------------------------------------------------------
947 // CommandObjectCommandsAddRegex
948 //-------------------------------------------------------------------------
950 static OptionDefinition g_regex_options[] = {
952 { LLDB_OPT_SET_1, false, "help" , 'h', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeNone, "The help text to display for this command." },
953 { LLDB_OPT_SET_1, false, "syntax", 's', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeNone, "A syntax string showing the typical usage syntax." },
957 #pragma mark CommandObjectCommandsAddRegex
959 class CommandObjectCommandsAddRegex : public CommandObjectParsed,
960 public IOHandlerDelegateMultiline {
962 CommandObjectCommandsAddRegex(CommandInterpreter &interpreter)
963 : CommandObjectParsed(
964 interpreter, "command regex", "Define a custom command in terms of "
965 "existing commands by matching "
966 "regular expressions.",
967 "command regex <cmd-name> [s/<regex>/<subst>/ ...]"),
968 IOHandlerDelegateMultiline("",
969 IOHandlerDelegate::Completion::LLDBCommand),
974 "This command allows the user to create powerful regular expression commands \
975 with substitutions. The regular expressions and substitutions are specified \
976 using the regular expression substitution format of:"
982 "<regex> is a regular expression that can use parenthesis to capture regular \
983 expression input and substitute the captured matches in the output using %1 \
984 for the first match, %2 for the second, and so on."
988 "The regular expressions can all be specified on the command line if more than \
989 one argument is provided. If just the command name is provided on the command \
990 line, then the regular expressions and substitutions can be entered on separate \
991 lines, followed by an empty line to terminate the command definition."
997 "The following example will define a regular expression command named 'f' that \
998 will call 'finish' if there are no arguments, or 'frame select <frame-idx>' if \
999 a number follows 'f':"
1002 (lldb) command regex f s/^$/finish/ 's/([0-9]+)/frame select %1/')");
1005 ~CommandObjectCommandsAddRegex() override = default;
1008 void IOHandlerActivated(IOHandler &io_handler) override {
1009 StreamFileSP output_sp(io_handler.GetOutputStreamFile());
1011 output_sp->PutCString("Enter one of more sed substitution commands in "
1012 "the form: 's/<regex>/<subst>/'.\nTerminate the "
1013 "substitution list with an empty line.\n");
1018 void IOHandlerInputComplete(IOHandler &io_handler,
1019 std::string &data) override {
1020 io_handler.SetIsDone(true);
1021 if (m_regex_cmd_ap) {
1023 if (lines.SplitIntoLines(data)) {
1024 const size_t num_lines = lines.GetSize();
1025 bool check_only = false;
1026 for (size_t i = 0; i < num_lines; ++i) {
1027 llvm::StringRef bytes_strref(lines[i]);
1028 Error error = AppendRegexSubstitution(bytes_strref, check_only);
1030 if (!m_interpreter.GetDebugger()
1031 .GetCommandInterpreter()
1032 .GetBatchCommandMode()) {
1033 StreamSP out_stream =
1034 m_interpreter.GetDebugger().GetAsyncOutputStream();
1035 out_stream->Printf("error: %s\n", error.AsCString());
1040 if (m_regex_cmd_ap->HasRegexEntries()) {
1041 CommandObjectSP cmd_sp(m_regex_cmd_ap.release());
1042 m_interpreter.AddCommand(cmd_sp->GetCommandName(), cmd_sp, true);
1047 bool DoExecute(Args &command, CommandReturnObject &result) override {
1048 const size_t argc = command.GetArgumentCount();
1050 result.AppendError("usage: 'command regex <command-name> "
1051 "[s/<regex1>/<subst1>/ s/<regex2>/<subst2>/ ...]'\n");
1052 result.SetStatus(eReturnStatusFailed);
1057 auto name = command[0].ref;
1058 m_regex_cmd_ap = llvm::make_unique<CommandObjectRegexCommand>(
1059 m_interpreter, name, m_options.GetHelp(), m_options.GetSyntax(), 10, 0,
1063 Debugger &debugger = m_interpreter.GetDebugger();
1064 bool color_prompt = debugger.GetUseColor();
1065 const bool multiple_lines = true; // Get multiple lines
1066 IOHandlerSP io_handler_sp(new IOHandlerEditline(
1067 debugger, IOHandler::Type::Other,
1068 "lldb-regex", // Name of input reader for history
1069 llvm::StringRef("> "), // Prompt
1070 llvm::StringRef(), // Continuation prompt
1071 multiple_lines, color_prompt,
1072 0, // Don't show line numbers
1075 if (io_handler_sp) {
1076 debugger.PushIOHandler(io_handler_sp);
1077 result.SetStatus(eReturnStatusSuccessFinishNoResult);
1080 for (auto &entry : command.entries().drop_front()) {
1081 bool check_only = false;
1082 error = AppendRegexSubstitution(entry.ref, check_only);
1087 if (error.Success()) {
1088 AddRegexCommandToInterpreter();
1092 result.AppendError(error.AsCString());
1093 result.SetStatus(eReturnStatusFailed);
1096 return result.Succeeded();
1099 Error AppendRegexSubstitution(const llvm::StringRef ®ex_sed,
1103 if (!m_regex_cmd_ap) {
1104 error.SetErrorStringWithFormat(
1105 "invalid regular expression command object for: '%.*s'",
1106 (int)regex_sed.size(), regex_sed.data());
1110 size_t regex_sed_size = regex_sed.size();
1112 if (regex_sed_size <= 1) {
1113 error.SetErrorStringWithFormat(
1114 "regular expression substitution string is too short: '%.*s'",
1115 (int)regex_sed.size(), regex_sed.data());
1119 if (regex_sed[0] != 's') {
1120 error.SetErrorStringWithFormat("regular expression substitution string "
1121 "doesn't start with 's': '%.*s'",
1122 (int)regex_sed.size(), regex_sed.data());
1125 const size_t first_separator_char_pos = 1;
1126 // use the char that follows 's' as the regex separator character
1127 // so we can have "s/<regex>/<subst>/" or "s|<regex>|<subst>|"
1128 const char separator_char = regex_sed[first_separator_char_pos];
1129 const size_t second_separator_char_pos =
1130 regex_sed.find(separator_char, first_separator_char_pos + 1);
1132 if (second_separator_char_pos == std::string::npos) {
1133 error.SetErrorStringWithFormat(
1134 "missing second '%c' separator char after '%.*s' in '%.*s'",
1136 (int)(regex_sed.size() - first_separator_char_pos - 1),
1137 regex_sed.data() + (first_separator_char_pos + 1),
1138 (int)regex_sed.size(), regex_sed.data());
1142 const size_t third_separator_char_pos =
1143 regex_sed.find(separator_char, second_separator_char_pos + 1);
1145 if (third_separator_char_pos == std::string::npos) {
1146 error.SetErrorStringWithFormat(
1147 "missing third '%c' separator char after '%.*s' in '%.*s'",
1149 (int)(regex_sed.size() - second_separator_char_pos - 1),
1150 regex_sed.data() + (second_separator_char_pos + 1),
1151 (int)regex_sed.size(), regex_sed.data());
1155 if (third_separator_char_pos != regex_sed_size - 1) {
1156 // Make sure that everything that follows the last regex
1158 if (regex_sed.find_first_not_of("\t\n\v\f\r ",
1159 third_separator_char_pos + 1) !=
1160 std::string::npos) {
1161 error.SetErrorStringWithFormat(
1162 "extra data found after the '%.*s' regular expression substitution "
1164 (int)third_separator_char_pos + 1, regex_sed.data(),
1165 (int)(regex_sed.size() - third_separator_char_pos - 1),
1166 regex_sed.data() + (third_separator_char_pos + 1));
1169 } else if (first_separator_char_pos + 1 == second_separator_char_pos) {
1170 error.SetErrorStringWithFormat(
1171 "<regex> can't be empty in 's%c<regex>%c<subst>%c' string: '%.*s'",
1172 separator_char, separator_char, separator_char, (int)regex_sed.size(),
1175 } else if (second_separator_char_pos + 1 == third_separator_char_pos) {
1176 error.SetErrorStringWithFormat(
1177 "<subst> can't be empty in 's%c<regex>%c<subst>%c' string: '%.*s'",
1178 separator_char, separator_char, separator_char, (int)regex_sed.size(),
1184 std::string regex(regex_sed.substr(first_separator_char_pos + 1,
1185 second_separator_char_pos -
1186 first_separator_char_pos - 1));
1187 std::string subst(regex_sed.substr(second_separator_char_pos + 1,
1188 third_separator_char_pos -
1189 second_separator_char_pos - 1));
1190 m_regex_cmd_ap->AddRegexCommand(regex.c_str(), subst.c_str());
1195 void AddRegexCommandToInterpreter() {
1196 if (m_regex_cmd_ap) {
1197 if (m_regex_cmd_ap->HasRegexEntries()) {
1198 CommandObjectSP cmd_sp(m_regex_cmd_ap.release());
1199 m_interpreter.AddCommand(cmd_sp->GetCommandName(), cmd_sp, true);
1205 std::unique_ptr<CommandObjectRegexCommand> m_regex_cmd_ap;
1207 class CommandOptions : public Options {
1209 CommandOptions() : Options() {}
1211 ~CommandOptions() override = default;
1213 Error SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1214 ExecutionContext *execution_context) override {
1216 const int short_option = m_getopt_table[option_idx].val;
1218 switch (short_option) {
1220 m_help.assign(option_arg);
1223 m_syntax.assign(option_arg);
1226 error.SetErrorStringWithFormat("unrecognized option '%c'",
1234 void OptionParsingStarting(ExecutionContext *execution_context) override {
1239 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
1240 return llvm::makeArrayRef(g_regex_options);
1243 // TODO: Convert these functions to return StringRefs.
1244 const char *GetHelp() {
1245 return (m_help.empty() ? nullptr : m_help.c_str());
1248 const char *GetSyntax() {
1249 return (m_syntax.empty() ? nullptr : m_syntax.c_str());
1253 // Instance variables to hold the values for command options.
1256 std::string m_syntax;
1259 Options *GetOptions() override { return &m_options; }
1261 CommandOptions m_options;
1264 class CommandObjectPythonFunction : public CommandObjectRaw {
1266 CommandObjectPythonFunction(CommandInterpreter &interpreter, std::string name,
1267 std::string funct, std::string help,
1268 ScriptedCommandSynchronicity synch)
1269 : CommandObjectRaw(interpreter, name),
1270 m_function_name(funct), m_synchro(synch), m_fetched_help_long(false) {
1274 StreamString stream;
1275 stream.Printf("For more information run 'help %s'", name.c_str());
1276 SetHelp(stream.GetString());
1280 ~CommandObjectPythonFunction() override = default;
1282 bool IsRemovable() const override { return true; }
1284 const std::string &GetFunctionName() { return m_function_name; }
1286 ScriptedCommandSynchronicity GetSynchronicity() { return m_synchro; }
1288 llvm::StringRef GetHelpLong() override {
1289 if (m_fetched_help_long)
1290 return CommandObjectRaw::GetHelpLong();
1292 ScriptInterpreter *scripter = m_interpreter.GetScriptInterpreter();
1294 return CommandObjectRaw::GetHelpLong();
1296 std::string docstring;
1297 m_fetched_help_long =
1298 scripter->GetDocumentationForItem(m_function_name.c_str(), docstring);
1299 if (!docstring.empty())
1300 SetHelpLong(docstring);
1301 return CommandObjectRaw::GetHelpLong();
1305 bool DoExecute(const char *raw_command_line,
1306 CommandReturnObject &result) override {
1307 ScriptInterpreter *scripter = m_interpreter.GetScriptInterpreter();
1311 result.SetStatus(eReturnStatusInvalid);
1314 !scripter->RunScriptBasedCommand(m_function_name.c_str(),
1315 raw_command_line, m_synchro, result,
1316 error, m_exe_ctx)) {
1317 result.AppendError(error.AsCString());
1318 result.SetStatus(eReturnStatusFailed);
1320 // Don't change the status if the command already set it...
1321 if (result.GetStatus() == eReturnStatusInvalid) {
1322 if (result.GetOutputData().empty())
1323 result.SetStatus(eReturnStatusSuccessFinishNoResult);
1325 result.SetStatus(eReturnStatusSuccessFinishResult);
1329 return result.Succeeded();
1333 std::string m_function_name;
1334 ScriptedCommandSynchronicity m_synchro;
1335 bool m_fetched_help_long;
1338 class CommandObjectScriptingObject : public CommandObjectRaw {
1340 CommandObjectScriptingObject(CommandInterpreter &interpreter,
1342 StructuredData::GenericSP cmd_obj_sp,
1343 ScriptedCommandSynchronicity synch)
1344 : CommandObjectRaw(interpreter, name),
1345 m_cmd_obj_sp(cmd_obj_sp), m_synchro(synch), m_fetched_help_short(false),
1346 m_fetched_help_long(false) {
1347 StreamString stream;
1348 stream.Printf("For more information run 'help %s'", name.c_str());
1349 SetHelp(stream.GetString());
1350 if (ScriptInterpreter *scripter = m_interpreter.GetScriptInterpreter())
1351 GetFlags().Set(scripter->GetFlagsForCommandObject(cmd_obj_sp));
1354 ~CommandObjectScriptingObject() override = default;
1356 bool IsRemovable() const override { return true; }
1358 StructuredData::GenericSP GetImplementingObject() { return m_cmd_obj_sp; }
1360 ScriptedCommandSynchronicity GetSynchronicity() { return m_synchro; }
1362 llvm::StringRef GetHelp() override {
1363 if (m_fetched_help_short)
1364 return CommandObjectRaw::GetHelp();
1365 ScriptInterpreter *scripter = m_interpreter.GetScriptInterpreter();
1367 return CommandObjectRaw::GetHelp();
1368 std::string docstring;
1369 m_fetched_help_short =
1370 scripter->GetShortHelpForCommandObject(m_cmd_obj_sp, docstring);
1371 if (!docstring.empty())
1374 return CommandObjectRaw::GetHelp();
1377 llvm::StringRef GetHelpLong() override {
1378 if (m_fetched_help_long)
1379 return CommandObjectRaw::GetHelpLong();
1381 ScriptInterpreter *scripter = m_interpreter.GetScriptInterpreter();
1383 return CommandObjectRaw::GetHelpLong();
1385 std::string docstring;
1386 m_fetched_help_long =
1387 scripter->GetLongHelpForCommandObject(m_cmd_obj_sp, docstring);
1388 if (!docstring.empty())
1389 SetHelpLong(docstring);
1390 return CommandObjectRaw::GetHelpLong();
1394 bool DoExecute(const char *raw_command_line,
1395 CommandReturnObject &result) override {
1396 ScriptInterpreter *scripter = m_interpreter.GetScriptInterpreter();
1400 result.SetStatus(eReturnStatusInvalid);
1403 !scripter->RunScriptBasedCommand(m_cmd_obj_sp, raw_command_line,
1404 m_synchro, result, error, m_exe_ctx)) {
1405 result.AppendError(error.AsCString());
1406 result.SetStatus(eReturnStatusFailed);
1408 // Don't change the status if the command already set it...
1409 if (result.GetStatus() == eReturnStatusInvalid) {
1410 if (result.GetOutputData().empty())
1411 result.SetStatus(eReturnStatusSuccessFinishNoResult);
1413 result.SetStatus(eReturnStatusSuccessFinishResult);
1417 return result.Succeeded();
1421 StructuredData::GenericSP m_cmd_obj_sp;
1422 ScriptedCommandSynchronicity m_synchro;
1423 bool m_fetched_help_short : 1;
1424 bool m_fetched_help_long : 1;
1427 //-------------------------------------------------------------------------
1428 // CommandObjectCommandsScriptImport
1429 //-------------------------------------------------------------------------
1431 OptionDefinition g_script_import_options[] = {
1433 { LLDB_OPT_SET_1, false, "allow-reload", 'r', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Allow the script to be loaded even if it was already loaded before. This argument exists for backwards compatibility, but reloading is always allowed, whether you specify it or not." },
1437 class CommandObjectCommandsScriptImport : public CommandObjectParsed {
1439 CommandObjectCommandsScriptImport(CommandInterpreter &interpreter)
1440 : CommandObjectParsed(interpreter, "command script import",
1441 "Import a scripting module in LLDB.", nullptr),
1443 CommandArgumentEntry arg1;
1444 CommandArgumentData cmd_arg;
1446 // Define the first (and only) variant of this arg.
1447 cmd_arg.arg_type = eArgTypeFilename;
1448 cmd_arg.arg_repetition = eArgRepeatPlus;
1450 // There is only one variant this argument could be; put it into the
1452 arg1.push_back(cmd_arg);
1454 // Push the data for the first argument into the m_arguments vector.
1455 m_arguments.push_back(arg1);
1458 ~CommandObjectCommandsScriptImport() override = default;
1460 int HandleArgumentCompletion(Args &input, int &cursor_index,
1461 int &cursor_char_position,
1462 OptionElementVector &opt_element_vector,
1463 int match_start_point, int max_return_elements,
1464 bool &word_complete,
1465 StringList &matches) override {
1466 llvm::StringRef completion_str = input[cursor_index].ref;
1467 completion_str = completion_str.take_front(cursor_char_position);
1469 CommandCompletions::InvokeCommonCompletionCallbacks(
1470 GetCommandInterpreter(), CommandCompletions::eDiskFileCompletion,
1471 completion_str, match_start_point, max_return_elements, nullptr,
1472 word_complete, matches);
1473 return matches.GetSize();
1476 Options *GetOptions() override { return &m_options; }
1479 class CommandOptions : public Options {
1481 CommandOptions() : Options() {}
1483 ~CommandOptions() override = default;
1485 Error SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1486 ExecutionContext *execution_context) override {
1488 const int short_option = m_getopt_table[option_idx].val;
1490 switch (short_option) {
1492 m_allow_reload = true;
1495 error.SetErrorStringWithFormat("unrecognized option '%c'",
1503 void OptionParsingStarting(ExecutionContext *execution_context) override {
1504 m_allow_reload = true;
1507 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
1508 return llvm::makeArrayRef(g_script_import_options);
1511 // Instance variables to hold the values for command options.
1513 bool m_allow_reload;
1516 bool DoExecute(Args &command, CommandReturnObject &result) override {
1517 if (m_interpreter.GetDebugger().GetScriptLanguage() !=
1518 lldb::eScriptLanguagePython) {
1519 result.AppendError("only scripting language supported for module "
1520 "importing is currently Python");
1521 result.SetStatus(eReturnStatusFailed);
1525 if (command.empty()) {
1526 result.AppendError("command script import needs one or more arguments");
1527 result.SetStatus(eReturnStatusFailed);
1531 for (auto &entry : command.entries()) {
1534 const bool init_session = true;
1535 // FIXME: this is necessary because CommandObject::CheckRequirements()
1536 // assumes that commands won't ever be recursively invoked, but it's
1537 // actually possible to craft a Python script that does other "command
1538 // script imports" in __lldb_init_module the real fix is to have recursive
1539 // commands possible with a CommandInvocation object separate from the
1540 // CommandObject itself, so that recursive command invocations won't stomp
1541 // on each other (wrt to execution contents, options, and more)
1543 if (m_interpreter.GetScriptInterpreter()->LoadScriptingModule(
1544 entry.c_str(), m_options.m_allow_reload, init_session, error)) {
1545 result.SetStatus(eReturnStatusSuccessFinishNoResult);
1547 result.AppendErrorWithFormat("module importing failed: %s",
1549 result.SetStatus(eReturnStatusFailed);
1553 return result.Succeeded();
1556 CommandOptions m_options;
1559 //-------------------------------------------------------------------------
1560 // CommandObjectCommandsScriptAdd
1561 //-------------------------------------------------------------------------
1563 static OptionEnumValueElement g_script_synchro_type[] = {
1564 {eScriptedCommandSynchronicitySynchronous, "synchronous",
1566 {eScriptedCommandSynchronicityAsynchronous, "asynchronous",
1567 "Run asynchronous"},
1568 {eScriptedCommandSynchronicityCurrentValue, "current",
1569 "Do not alter current setting"},
1570 {0, nullptr, nullptr}};
1572 static OptionDefinition g_script_add_options[] = {
1574 { LLDB_OPT_SET_1, false, "function", 'f', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypePythonFunction, "Name of the Python function to bind to this command name." },
1575 { LLDB_OPT_SET_2, false, "class", 'c', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypePythonClass, "Name of the Python class to bind to this command name." },
1576 { LLDB_OPT_SET_1, false, "help" , 'h', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeHelpText, "The help text to display for this command." },
1577 { LLDB_OPT_SET_ALL, false, "synchronicity", 's', OptionParser::eRequiredArgument, nullptr, g_script_synchro_type, 0, eArgTypeScriptedCommandSynchronicity, "Set the synchronicity of this command's executions with regard to LLDB event system." },
1581 class CommandObjectCommandsScriptAdd : public CommandObjectParsed,
1582 public IOHandlerDelegateMultiline {
1584 CommandObjectCommandsScriptAdd(CommandInterpreter &interpreter)
1585 : CommandObjectParsed(interpreter, "command script add",
1586 "Add a scripted function as an LLDB command.",
1588 IOHandlerDelegateMultiline("DONE"), m_options() {
1589 CommandArgumentEntry arg1;
1590 CommandArgumentData cmd_arg;
1592 // Define the first (and only) variant of this arg.
1593 cmd_arg.arg_type = eArgTypeCommandName;
1594 cmd_arg.arg_repetition = eArgRepeatPlain;
1596 // There is only one variant this argument could be; put it into the
1598 arg1.push_back(cmd_arg);
1600 // Push the data for the first argument into the m_arguments vector.
1601 m_arguments.push_back(arg1);
1604 ~CommandObjectCommandsScriptAdd() override = default;
1606 Options *GetOptions() override { return &m_options; }
1609 class CommandOptions : public Options {
1612 : Options(), m_class_name(), m_funct_name(), m_short_help(),
1613 m_synchronicity(eScriptedCommandSynchronicitySynchronous) {}
1615 ~CommandOptions() override = default;
1617 Error SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1618 ExecutionContext *execution_context) override {
1620 const int short_option = m_getopt_table[option_idx].val;
1622 switch (short_option) {
1624 if (!option_arg.empty())
1625 m_funct_name = option_arg;
1628 if (!option_arg.empty())
1629 m_class_name = option_arg;
1632 if (!option_arg.empty())
1633 m_short_help = option_arg;
1637 (ScriptedCommandSynchronicity)Args::StringToOptionEnum(
1638 option_arg, GetDefinitions()[option_idx].enum_values, 0, error);
1639 if (!error.Success())
1640 error.SetErrorStringWithFormat(
1641 "unrecognized value for synchronicity '%s'",
1642 option_arg.str().c_str());
1645 error.SetErrorStringWithFormat("unrecognized option '%c'",
1653 void OptionParsingStarting(ExecutionContext *execution_context) override {
1654 m_class_name.clear();
1655 m_funct_name.clear();
1656 m_short_help.clear();
1657 m_synchronicity = eScriptedCommandSynchronicitySynchronous;
1660 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
1661 return llvm::makeArrayRef(g_script_add_options);
1664 // Instance variables to hold the values for command options.
1666 std::string m_class_name;
1667 std::string m_funct_name;
1668 std::string m_short_help;
1669 ScriptedCommandSynchronicity m_synchronicity;
1672 void IOHandlerActivated(IOHandler &io_handler) override {
1673 StreamFileSP output_sp(io_handler.GetOutputStreamFile());
1675 output_sp->PutCString(g_python_command_instructions);
1680 void IOHandlerInputComplete(IOHandler &io_handler,
1681 std::string &data) override {
1682 StreamFileSP error_sp = io_handler.GetErrorStreamFile();
1684 ScriptInterpreter *interpreter = m_interpreter.GetScriptInterpreter();
1688 lines.SplitIntoLines(data);
1689 if (lines.GetSize() > 0) {
1690 std::string funct_name_str;
1691 if (interpreter->GenerateScriptAliasFunction(lines, funct_name_str)) {
1692 if (funct_name_str.empty()) {
1693 error_sp->Printf("error: unable to obtain a function name, didn't "
1694 "add python command.\n");
1697 // everything should be fine now, let's add this alias
1699 CommandObjectSP command_obj_sp(new CommandObjectPythonFunction(
1700 m_interpreter, m_cmd_name, funct_name_str, m_short_help,
1703 if (!m_interpreter.AddUserCommand(m_cmd_name, command_obj_sp,
1705 error_sp->Printf("error: unable to add selected command, didn't "
1706 "add python command.\n");
1712 "error: unable to create function, didn't add python command.\n");
1716 error_sp->Printf("error: empty function, didn't add python command.\n");
1721 "error: script interpreter missing, didn't add python command.\n");
1725 io_handler.SetIsDone(true);
1729 bool DoExecute(Args &command, CommandReturnObject &result) override {
1730 if (m_interpreter.GetDebugger().GetScriptLanguage() !=
1731 lldb::eScriptLanguagePython) {
1732 result.AppendError("only scripting language supported for scripted "
1733 "commands is currently Python");
1734 result.SetStatus(eReturnStatusFailed);
1738 if (command.GetArgumentCount() != 1) {
1739 result.AppendError("'command script add' requires one argument");
1740 result.SetStatus(eReturnStatusFailed);
1744 // Store the options in case we get multi-line input
1745 m_cmd_name = command[0].ref;
1746 m_short_help.assign(m_options.m_short_help);
1747 m_synchronicity = m_options.m_synchronicity;
1749 if (m_options.m_class_name.empty()) {
1750 if (m_options.m_funct_name.empty()) {
1751 m_interpreter.GetPythonCommandsFromIOHandler(
1753 *this, // IOHandlerDelegate
1754 true, // Run IOHandler in async mode
1755 nullptr); // Baton for the "io_handler" that will be passed back
1756 // into our IOHandlerDelegate functions
1758 CommandObjectSP new_cmd(new CommandObjectPythonFunction(
1759 m_interpreter, m_cmd_name, m_options.m_funct_name,
1760 m_options.m_short_help, m_synchronicity));
1761 if (m_interpreter.AddUserCommand(m_cmd_name, new_cmd, true)) {
1762 result.SetStatus(eReturnStatusSuccessFinishNoResult);
1764 result.AppendError("cannot add command");
1765 result.SetStatus(eReturnStatusFailed);
1769 ScriptInterpreter *interpreter =
1770 GetCommandInterpreter().GetScriptInterpreter();
1772 result.AppendError("cannot find ScriptInterpreter");
1773 result.SetStatus(eReturnStatusFailed);
1777 auto cmd_obj_sp = interpreter->CreateScriptCommandObject(
1778 m_options.m_class_name.c_str());
1780 result.AppendError("cannot create helper object");
1781 result.SetStatus(eReturnStatusFailed);
1785 CommandObjectSP new_cmd(new CommandObjectScriptingObject(
1786 m_interpreter, m_cmd_name, cmd_obj_sp, m_synchronicity));
1787 if (m_interpreter.AddUserCommand(m_cmd_name, new_cmd, true)) {
1788 result.SetStatus(eReturnStatusSuccessFinishNoResult);
1790 result.AppendError("cannot add command");
1791 result.SetStatus(eReturnStatusFailed);
1795 return result.Succeeded();
1798 CommandOptions m_options;
1799 std::string m_cmd_name;
1800 std::string m_short_help;
1801 ScriptedCommandSynchronicity m_synchronicity;
1804 //-------------------------------------------------------------------------
1805 // CommandObjectCommandsScriptList
1806 //-------------------------------------------------------------------------
1808 class CommandObjectCommandsScriptList : public CommandObjectParsed {
1810 CommandObjectCommandsScriptList(CommandInterpreter &interpreter)
1811 : CommandObjectParsed(interpreter, "command script list",
1812 "List defined scripted commands.", nullptr) {}
1814 ~CommandObjectCommandsScriptList() override = default;
1816 bool DoExecute(Args &command, CommandReturnObject &result) override {
1817 m_interpreter.GetHelp(result, CommandInterpreter::eCommandTypesUserDef);
1819 result.SetStatus(eReturnStatusSuccessFinishResult);
1825 //-------------------------------------------------------------------------
1826 // CommandObjectCommandsScriptClear
1827 //-------------------------------------------------------------------------
1829 class CommandObjectCommandsScriptClear : public CommandObjectParsed {
1831 CommandObjectCommandsScriptClear(CommandInterpreter &interpreter)
1832 : CommandObjectParsed(interpreter, "command script clear",
1833 "Delete all scripted commands.", nullptr) {}
1835 ~CommandObjectCommandsScriptClear() override = default;
1838 bool DoExecute(Args &command, CommandReturnObject &result) override {
1839 m_interpreter.RemoveAllUser();
1841 result.SetStatus(eReturnStatusSuccessFinishResult);
1847 //-------------------------------------------------------------------------
1848 // CommandObjectCommandsScriptDelete
1849 //-------------------------------------------------------------------------
1851 class CommandObjectCommandsScriptDelete : public CommandObjectParsed {
1853 CommandObjectCommandsScriptDelete(CommandInterpreter &interpreter)
1854 : CommandObjectParsed(interpreter, "command script delete",
1855 "Delete a scripted command.", nullptr) {
1856 CommandArgumentEntry arg1;
1857 CommandArgumentData cmd_arg;
1859 // Define the first (and only) variant of this arg.
1860 cmd_arg.arg_type = eArgTypeCommandName;
1861 cmd_arg.arg_repetition = eArgRepeatPlain;
1863 // There is only one variant this argument could be; put it into the
1865 arg1.push_back(cmd_arg);
1867 // Push the data for the first argument into the m_arguments vector.
1868 m_arguments.push_back(arg1);
1871 ~CommandObjectCommandsScriptDelete() override = default;
1874 bool DoExecute(Args &command, CommandReturnObject &result) override {
1876 if (command.GetArgumentCount() != 1) {
1877 result.AppendError("'command script delete' requires one argument");
1878 result.SetStatus(eReturnStatusFailed);
1882 auto cmd_name = command[0].ref;
1884 if (cmd_name.empty() || !m_interpreter.HasUserCommands() ||
1885 !m_interpreter.UserCommandExists(cmd_name)) {
1886 result.AppendErrorWithFormat("command %s not found", command[0].c_str());
1887 result.SetStatus(eReturnStatusFailed);
1891 m_interpreter.RemoveUser(cmd_name);
1892 result.SetStatus(eReturnStatusSuccessFinishResult);
1897 #pragma mark CommandObjectMultiwordCommandsScript
1899 //-------------------------------------------------------------------------
1900 // CommandObjectMultiwordCommandsScript
1901 //-------------------------------------------------------------------------
1903 class CommandObjectMultiwordCommandsScript : public CommandObjectMultiword {
1905 CommandObjectMultiwordCommandsScript(CommandInterpreter &interpreter)
1906 : CommandObjectMultiword(
1907 interpreter, "command script", "Commands for managing custom "
1908 "commands implemented by "
1909 "interpreter scripts.",
1910 "command script <subcommand> [<subcommand-options>]") {
1911 LoadSubCommand("add", CommandObjectSP(
1912 new CommandObjectCommandsScriptAdd(interpreter)));
1915 CommandObjectSP(new CommandObjectCommandsScriptDelete(interpreter)));
1918 CommandObjectSP(new CommandObjectCommandsScriptClear(interpreter)));
1919 LoadSubCommand("list", CommandObjectSP(new CommandObjectCommandsScriptList(
1923 CommandObjectSP(new CommandObjectCommandsScriptImport(interpreter)));
1926 ~CommandObjectMultiwordCommandsScript() override = default;
1929 #pragma mark CommandObjectMultiwordCommands
1931 //-------------------------------------------------------------------------
1932 // CommandObjectMultiwordCommands
1933 //-------------------------------------------------------------------------
1935 CommandObjectMultiwordCommands::CommandObjectMultiwordCommands(
1936 CommandInterpreter &interpreter)
1937 : CommandObjectMultiword(interpreter, "command",
1938 "Commands for managing custom LLDB commands.",
1939 "command <subcommand> [<subcommand-options>]") {
1940 LoadSubCommand("source",
1941 CommandObjectSP(new CommandObjectCommandsSource(interpreter)));
1942 LoadSubCommand("alias",
1943 CommandObjectSP(new CommandObjectCommandsAlias(interpreter)));
1944 LoadSubCommand("unalias", CommandObjectSP(
1945 new CommandObjectCommandsUnalias(interpreter)));
1946 LoadSubCommand("delete",
1947 CommandObjectSP(new CommandObjectCommandsDelete(interpreter)));
1949 "regex", CommandObjectSP(new CommandObjectCommandsAddRegex(interpreter)));
1950 LoadSubCommand("history", CommandObjectSP(
1951 new CommandObjectCommandsHistory(interpreter)));
1954 CommandObjectSP(new CommandObjectMultiwordCommandsScript(interpreter)));
1957 CommandObjectMultiwordCommands::~CommandObjectMultiwordCommands() = default;