1 //===-- CommandInterpreter.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 //===----------------------------------------------------------------------===//
14 #include "CommandObjectScript.h"
15 #include "lldb/Interpreter/CommandObjectRegexCommand.h"
17 #include "../Commands/CommandObjectApropos.h"
18 #include "../Commands/CommandObjectArgs.h"
19 #include "../Commands/CommandObjectBreakpoint.h"
20 #include "../Commands/CommandObjectBugreport.h"
21 #include "../Commands/CommandObjectDisassemble.h"
22 #include "../Commands/CommandObjectExpression.h"
23 #include "../Commands/CommandObjectFrame.h"
24 #include "../Commands/CommandObjectGUI.h"
25 #include "../Commands/CommandObjectHelp.h"
26 #include "../Commands/CommandObjectLog.h"
27 #include "../Commands/CommandObjectMemory.h"
28 #include "../Commands/CommandObjectPlatform.h"
29 #include "../Commands/CommandObjectPlugin.h"
30 #include "../Commands/CommandObjectProcess.h"
31 #include "../Commands/CommandObjectQuit.h"
32 #include "../Commands/CommandObjectRegister.h"
33 #include "../Commands/CommandObjectSettings.h"
34 #include "../Commands/CommandObjectSource.h"
35 #include "../Commands/CommandObjectCommands.h"
36 #include "../Commands/CommandObjectSyntax.h"
37 #include "../Commands/CommandObjectTarget.h"
38 #include "../Commands/CommandObjectThread.h"
39 #include "../Commands/CommandObjectType.h"
40 #include "../Commands/CommandObjectVersion.h"
41 #include "../Commands/CommandObjectWatchpoint.h"
42 #include "../Commands/CommandObjectLanguage.h"
44 #include "lldb/Core/Debugger.h"
45 #include "lldb/Core/Log.h"
46 #include "lldb/Core/PluginManager.h"
47 #include "lldb/Core/State.h"
48 #include "lldb/Core/Stream.h"
49 #include "lldb/Core/StreamFile.h"
50 #include "lldb/Core/Timer.h"
52 #ifndef LLDB_DISABLE_LIBEDIT
53 #include "lldb/Host/Editline.h"
55 #include "lldb/Host/Host.h"
56 #include "lldb/Host/HostInfo.h"
58 #include "lldb/Interpreter/Args.h"
59 #include "lldb/Interpreter/CommandCompletions.h"
60 #include "lldb/Interpreter/CommandInterpreter.h"
61 #include "lldb/Interpreter/CommandReturnObject.h"
62 #include "lldb/Interpreter/Options.h"
63 #include "lldb/Interpreter/OptionValueProperties.h"
64 #include "lldb/Interpreter/Property.h"
67 #include "lldb/Target/Process.h"
68 #include "lldb/Target/Thread.h"
69 #include "lldb/Target/TargetList.h"
71 #include "lldb/Utility/CleanUp.h"
73 #include "llvm/ADT/SmallString.h"
74 #include "llvm/ADT/STLExtras.h"
75 #include "llvm/Support/Path.h"
78 using namespace lldb_private;
80 static const char *k_white_space = " \t\v";
82 static PropertyDefinition
85 { "expand-regex-aliases", OptionValue::eTypeBoolean, true, false, nullptr, nullptr, "If true, regular expression alias commands will show the expanded command that will be executed. This can be used to debug new regular expression alias commands." },
86 { "prompt-on-quit", OptionValue::eTypeBoolean, true, true, nullptr, nullptr, "If true, LLDB will prompt you before quitting if there are any live processes being debugged. If false, LLDB will quit without asking in any case." },
87 { "stop-command-source-on-error", OptionValue::eTypeBoolean, true, true, nullptr, nullptr, "If true, LLDB will stop running a 'command source' script upon encountering an error." },
88 { "space-repl-prompts", OptionValue::eTypeBoolean, true, false, nullptr, nullptr, "If true, blank lines will be printed between between REPL submissions." },
89 { nullptr , OptionValue::eTypeInvalid, true, 0 , nullptr, nullptr, nullptr }
94 ePropertyExpandRegexAliases = 0,
95 ePropertyPromptOnQuit = 1,
96 ePropertyStopCmdSourceOnError = 2,
101 CommandInterpreter::GetStaticBroadcasterClass ()
103 static ConstString class_name ("lldb.commandInterpreter");
107 CommandInterpreter::CommandInterpreter(Debugger &debugger, ScriptLanguage script_language, bool synchronous_execution)
108 : Broadcaster(&debugger, CommandInterpreter::GetStaticBroadcasterClass().AsCString()),
109 Properties(OptionValuePropertiesSP(new OptionValueProperties(ConstString("interpreter")))),
110 IOHandlerDelegate(IOHandlerDelegate::Completion::LLDBCommand),
111 m_debugger(debugger),
112 m_synchronous_execution(synchronous_execution),
113 m_skip_lldbinit_files(false),
114 m_skip_app_init_files(false),
115 m_script_interpreter_sp(),
116 m_command_io_handler_sp(),
118 m_batch_command_mode(false),
119 m_truncation_warning(eNoTruncation),
120 m_command_source_depth(0),
122 m_quit_requested(false),
123 m_stopped_for_crash(false)
125 debugger.SetScriptLanguage (script_language);
126 SetEventName (eBroadcastBitThreadShouldExit, "thread-should-exit");
127 SetEventName (eBroadcastBitResetPrompt, "reset-prompt");
128 SetEventName (eBroadcastBitQuitCommandReceived, "quit");
129 CheckInWithManager ();
130 m_collection_sp->Initialize (g_properties);
134 CommandInterpreter::GetExpandRegexAliases () const
136 const uint32_t idx = ePropertyExpandRegexAliases;
137 return m_collection_sp->GetPropertyAtIndexAsBoolean (nullptr, idx, g_properties[idx].default_uint_value != 0);
141 CommandInterpreter::GetPromptOnQuit () const
143 const uint32_t idx = ePropertyPromptOnQuit;
144 return m_collection_sp->GetPropertyAtIndexAsBoolean (nullptr, idx, g_properties[idx].default_uint_value != 0);
148 CommandInterpreter::SetPromptOnQuit (bool b)
150 const uint32_t idx = ePropertyPromptOnQuit;
151 m_collection_sp->SetPropertyAtIndexAsBoolean (nullptr, idx, b);
155 CommandInterpreter::ResolveCommand(const char *command_line, CommandReturnObject &result)
157 std::string command = command_line;
158 if (ResolveCommandImpl(command, result) != nullptr) {
159 result.AppendMessageWithFormat("%s", command.c_str());
160 result.SetStatus(eReturnStatusSuccessFinishResult);
166 CommandInterpreter::GetStopCmdSourceOnError () const
168 const uint32_t idx = ePropertyStopCmdSourceOnError;
169 return m_collection_sp->GetPropertyAtIndexAsBoolean (nullptr, idx, g_properties[idx].default_uint_value != 0);
173 CommandInterpreter::GetSpaceReplPrompts () const
175 const uint32_t idx = eSpaceReplPrompts;
176 return m_collection_sp->GetPropertyAtIndexAsBoolean (nullptr, idx, g_properties[idx].default_uint_value != 0);
180 CommandInterpreter::Initialize ()
182 Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
184 CommandReturnObject result;
186 LoadCommandDictionary ();
188 // Set up some initial aliases.
189 CommandObjectSP cmd_obj_sp = GetCommandSPExact ("quit", false);
192 AddAlias ("q", cmd_obj_sp);
193 AddAlias ("exit", cmd_obj_sp);
196 cmd_obj_sp = GetCommandSPExact ("_regexp-attach",false);
199 AddAlias ("attach", cmd_obj_sp);
202 cmd_obj_sp = GetCommandSPExact ("process detach",false);
205 AddAlias ("detach", cmd_obj_sp);
208 cmd_obj_sp = GetCommandSPExact ("process continue", false);
211 AddAlias ("c", cmd_obj_sp);
212 AddAlias ("continue", cmd_obj_sp);
215 cmd_obj_sp = GetCommandSPExact ("_regexp-break",false);
217 AddAlias ("b", cmd_obj_sp);
219 cmd_obj_sp = GetCommandSPExact ("_regexp-tbreak",false);
221 AddAlias ("tbreak", cmd_obj_sp);
223 cmd_obj_sp = GetCommandSPExact ("thread step-inst", false);
226 AddAlias ("stepi", cmd_obj_sp);
227 AddAlias ("si", cmd_obj_sp);
230 cmd_obj_sp = GetCommandSPExact ("thread step-inst-over", false);
233 AddAlias ("nexti", cmd_obj_sp);
234 AddAlias ("ni", cmd_obj_sp);
237 cmd_obj_sp = GetCommandSPExact ("thread step-in", false);
240 AddAlias ("s", cmd_obj_sp);
241 AddAlias ("step", cmd_obj_sp);
244 cmd_obj_sp = GetCommandSPExact ("thread step-over", false);
247 AddAlias ("n", cmd_obj_sp);
248 AddAlias ("next", cmd_obj_sp);
251 cmd_obj_sp = GetCommandSPExact ("thread step-out", false);
254 AddAlias ("finish", cmd_obj_sp);
257 cmd_obj_sp = GetCommandSPExact ("frame select", false);
260 AddAlias ("f", cmd_obj_sp);
263 cmd_obj_sp = GetCommandSPExact ("thread select", false);
266 AddAlias ("t", cmd_obj_sp);
269 cmd_obj_sp = GetCommandSPExact ("_regexp-jump",false);
272 AddAlias ("j", cmd_obj_sp);
273 AddAlias ("jump", cmd_obj_sp);
276 cmd_obj_sp = GetCommandSPExact ("_regexp-list", false);
279 AddAlias ("l", cmd_obj_sp);
280 AddAlias ("list", cmd_obj_sp);
283 cmd_obj_sp = GetCommandSPExact ("_regexp-env", false);
286 AddAlias ("env", cmd_obj_sp);
289 cmd_obj_sp = GetCommandSPExact ("memory read", false);
291 AddAlias ("x", cmd_obj_sp);
293 cmd_obj_sp = GetCommandSPExact ("_regexp-up", false);
295 AddAlias ("up", cmd_obj_sp);
297 cmd_obj_sp = GetCommandSPExact ("_regexp-down", false);
299 AddAlias ("down", cmd_obj_sp);
301 cmd_obj_sp = GetCommandSPExact ("_regexp-display", false);
303 AddAlias ("display", cmd_obj_sp);
305 cmd_obj_sp = GetCommandSPExact ("disassemble", false);
307 AddAlias ("dis", cmd_obj_sp);
309 cmd_obj_sp = GetCommandSPExact ("disassemble", false);
311 AddAlias ("di", cmd_obj_sp);
315 cmd_obj_sp = GetCommandSPExact ("_regexp-undisplay", false);
317 AddAlias ("undisplay", cmd_obj_sp);
319 cmd_obj_sp = GetCommandSPExact ("_regexp-bt", false);
321 AddAlias ("bt", cmd_obj_sp);
323 cmd_obj_sp = GetCommandSPExact ("target create", false);
325 AddAlias ("file", cmd_obj_sp);
327 cmd_obj_sp = GetCommandSPExact ("target modules", false);
329 AddAlias ("image", cmd_obj_sp);
332 OptionArgVectorSP alias_arguments_vector_sp (new OptionArgVector);
334 cmd_obj_sp = GetCommandSPExact ("expression", false);
337 ProcessAliasOptionsArgs (cmd_obj_sp, "--", alias_arguments_vector_sp);
338 AddAlias ("p", cmd_obj_sp);
339 AddAlias ("print", cmd_obj_sp);
340 AddAlias ("call", cmd_obj_sp);
341 AddOrReplaceAliasOptions ("p", alias_arguments_vector_sp);
342 AddOrReplaceAliasOptions ("print", alias_arguments_vector_sp);
343 AddOrReplaceAliasOptions ("call", alias_arguments_vector_sp);
345 alias_arguments_vector_sp.reset (new OptionArgVector);
346 ProcessAliasOptionsArgs (cmd_obj_sp, "-O -- ", alias_arguments_vector_sp);
347 AddAlias ("po", cmd_obj_sp);
348 AddOrReplaceAliasOptions ("po", alias_arguments_vector_sp);
351 cmd_obj_sp = GetCommandSPExact ("process kill", false);
354 AddAlias ("kill", cmd_obj_sp);
357 cmd_obj_sp = GetCommandSPExact ("process launch", false);
360 alias_arguments_vector_sp.reset (new OptionArgVector);
361 #if defined (__arm__) || defined (__arm64__) || defined (__aarch64__)
362 ProcessAliasOptionsArgs (cmd_obj_sp, "--", alias_arguments_vector_sp);
364 #if defined(__APPLE__)
365 std::string shell_option;
366 shell_option.append("--shell-expand-args");
367 shell_option.append(" true");
368 shell_option.append(" --");
369 ProcessAliasOptionsArgs (cmd_obj_sp, shell_option.c_str(), alias_arguments_vector_sp);
371 std::string shell_option;
372 shell_option.append("--shell=");
373 shell_option.append(HostInfo::GetDefaultShell().GetPath());
374 shell_option.append(" --");
375 ProcessAliasOptionsArgs (cmd_obj_sp, shell_option.c_str(), alias_arguments_vector_sp);
378 AddAlias ("r", cmd_obj_sp);
379 AddAlias ("run", cmd_obj_sp);
380 AddOrReplaceAliasOptions ("r", alias_arguments_vector_sp);
381 AddOrReplaceAliasOptions ("run", alias_arguments_vector_sp);
384 cmd_obj_sp = GetCommandSPExact ("target symbols add", false);
387 AddAlias ("add-dsym", cmd_obj_sp);
390 cmd_obj_sp = GetCommandSPExact ("breakpoint set", false);
393 alias_arguments_vector_sp.reset (new OptionArgVector);
394 ProcessAliasOptionsArgs (cmd_obj_sp, "--func-regex %1", alias_arguments_vector_sp);
395 AddAlias ("rbreak", cmd_obj_sp);
396 AddOrReplaceAliasOptions("rbreak", alias_arguments_vector_sp);
401 CommandInterpreter::Clear()
403 m_command_io_handler_sp.reset();
405 if (m_script_interpreter_sp)
406 m_script_interpreter_sp->Clear();
410 CommandInterpreter::ProcessEmbeddedScriptCommands (const char *arg)
412 // This function has not yet been implemented.
414 // Look for any embedded script command
416 // get interpreter object from the command dictionary,
417 // call execute_one_command on it,
418 // get the results as a string,
419 // substitute that string for current stuff.
426 CommandInterpreter::LoadCommandDictionary ()
428 Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
430 lldb::ScriptLanguage script_language = m_debugger.GetScriptLanguage();
432 m_command_dict["apropos"] = CommandObjectSP (new CommandObjectApropos (*this));
433 m_command_dict["breakpoint"]= CommandObjectSP (new CommandObjectMultiwordBreakpoint (*this));
434 m_command_dict["bugreport"] = CommandObjectSP (new CommandObjectMultiwordBugreport (*this));
435 m_command_dict["command"] = CommandObjectSP (new CommandObjectMultiwordCommands (*this));
436 m_command_dict["disassemble"] = CommandObjectSP (new CommandObjectDisassemble (*this));
437 m_command_dict["expression"]= CommandObjectSP (new CommandObjectExpression (*this));
438 m_command_dict["frame"] = CommandObjectSP (new CommandObjectMultiwordFrame (*this));
439 m_command_dict["gui"] = CommandObjectSP (new CommandObjectGUI (*this));
440 m_command_dict["help"] = CommandObjectSP (new CommandObjectHelp (*this));
441 m_command_dict["log"] = CommandObjectSP (new CommandObjectLog (*this));
442 m_command_dict["memory"] = CommandObjectSP (new CommandObjectMemory (*this));
443 m_command_dict["platform"] = CommandObjectSP (new CommandObjectPlatform (*this));
444 m_command_dict["plugin"] = CommandObjectSP (new CommandObjectPlugin (*this));
445 m_command_dict["process"] = CommandObjectSP (new CommandObjectMultiwordProcess (*this));
446 m_command_dict["quit"] = CommandObjectSP (new CommandObjectQuit (*this));
447 m_command_dict["register"] = CommandObjectSP (new CommandObjectRegister (*this));
448 m_command_dict["script"] = CommandObjectSP (new CommandObjectScript (*this, script_language));
449 m_command_dict["settings"] = CommandObjectSP (new CommandObjectMultiwordSettings (*this));
450 m_command_dict["source"] = CommandObjectSP (new CommandObjectMultiwordSource (*this));
451 m_command_dict["target"] = CommandObjectSP (new CommandObjectMultiwordTarget (*this));
452 m_command_dict["thread"] = CommandObjectSP (new CommandObjectMultiwordThread (*this));
453 m_command_dict["type"] = CommandObjectSP (new CommandObjectType (*this));
454 m_command_dict["version"] = CommandObjectSP (new CommandObjectVersion (*this));
455 m_command_dict["watchpoint"]= CommandObjectSP (new CommandObjectMultiwordWatchpoint (*this));
456 m_command_dict["language"] = CommandObjectSP (new CommandObjectLanguage(*this));
458 const char *break_regexes[][2] = {{"^(.*[^[:space:]])[[:space:]]*:[[:space:]]*([[:digit:]]+)[[:space:]]*$", "breakpoint set --file '%1' --line %2"},
459 {"^/([^/]+)/$", "breakpoint set --source-pattern-regexp '%1'"},
460 {"^([[:digit:]]+)[[:space:]]*$", "breakpoint set --line %1"},
461 {"^\\*?(0x[[:xdigit:]]+)[[:space:]]*$", "breakpoint set --address %1"},
462 {"^[\"']?([-+]?\\[.*\\])[\"']?[[:space:]]*$", "breakpoint set --name '%1'"},
463 {"^(-.*)$", "breakpoint set %1"},
464 {"^(.*[^[:space:]])`(.*[^[:space:]])[[:space:]]*$", "breakpoint set --name '%2' --shlib '%1'"},
465 {"^\\&(.*[^[:space:]])[[:space:]]*$", "breakpoint set --name '%1' --skip-prologue=0"},
466 {"^[\"']?(.*[^[:space:]\"'])[\"']?[[:space:]]*$", "breakpoint set --name '%1'"}};
468 size_t num_regexes = llvm::array_lengthof(break_regexes);
470 std::unique_ptr<CommandObjectRegexCommand>
471 break_regex_cmd_ap(new CommandObjectRegexCommand (*this,
473 "Set a breakpoint using a regular expression to specify the location, where <linenum> is in decimal and <address> is in hex.\n",
474 "\n_regexp-break <filename>:<linenum> # _regexp-break main.c:12 // Break on line 12 of main.c\n"
475 "_regexp-break <linenum> # _regexp-break 12 // Break on line 12 of current file\n"
476 "_regexp-break <address> # _regexp-break 0x1234000 // Break on address 0x1234000\n"
477 "_regexp-break <name> # _regexp-break main // Break in 'main' after the prologue\n"
478 "_regexp-break &<name> # _regexp-break &main // Break on the first instruction in 'main'\n"
479 "_regexp-break <module>`<name> # _regexp-break libc.so`malloc // Break in 'malloc' only in the 'libc.so' shared library\n"
480 "_regexp-break /<source-regex>/ # _regexp-break /break here/ // Break on all lines that match the regular expression 'break here' in the current file.\n",
482 CommandCompletions::eSymbolCompletion |
483 CommandCompletions::eSourceFileCompletion,
486 if (break_regex_cmd_ap.get())
489 for (size_t i = 0; i < num_regexes; i++)
491 success = break_regex_cmd_ap->AddRegexCommand (break_regexes[i][0], break_regexes[i][1]);
495 success = break_regex_cmd_ap->AddRegexCommand("^$", "breakpoint list --full");
499 CommandObjectSP break_regex_cmd_sp(break_regex_cmd_ap.release());
500 m_command_dict[break_regex_cmd_sp->GetCommandName ()] = break_regex_cmd_sp;
504 std::unique_ptr<CommandObjectRegexCommand>
505 tbreak_regex_cmd_ap(new CommandObjectRegexCommand (*this,
507 "Set a one shot breakpoint using a regular expression to specify the location, where <linenum> is in decimal and <address> is in hex.",
508 "_regexp-tbreak [<filename>:<linenum>]\n_regexp-break [<linenum>]\n_regexp-break [<address>]\n_regexp-break <...>",
510 CommandCompletions::eSymbolCompletion |
511 CommandCompletions::eSourceFileCompletion,
514 if (tbreak_regex_cmd_ap.get())
517 for (size_t i = 0; i < num_regexes; i++)
519 // If you add a resultant command string longer than 1024 characters be sure to increase the size of this buffer.
521 int num_printed = snprintf(buffer, 1024, "%s %s", break_regexes[i][1], "-o");
522 assert (num_printed < 1024);
523 UNUSED_IF_ASSERT_DISABLED(num_printed);
524 success = tbreak_regex_cmd_ap->AddRegexCommand (break_regexes[i][0], buffer);
528 success = tbreak_regex_cmd_ap->AddRegexCommand("^$", "breakpoint list --full");
532 CommandObjectSP tbreak_regex_cmd_sp(tbreak_regex_cmd_ap.release());
533 m_command_dict[tbreak_regex_cmd_sp->GetCommandName ()] = tbreak_regex_cmd_sp;
537 std::unique_ptr<CommandObjectRegexCommand>
538 attach_regex_cmd_ap(new CommandObjectRegexCommand (*this,
540 "Attach to a process id if in decimal, otherwise treat the argument as a process name to attach to.",
541 "_regexp-attach [<pid>]\n_regexp-attach [<process-name>]",
545 if (attach_regex_cmd_ap.get())
547 if (attach_regex_cmd_ap->AddRegexCommand("^([0-9]+)[[:space:]]*$", "process attach --pid %1") &&
548 attach_regex_cmd_ap->AddRegexCommand("^(-.*|.* -.*)$", "process attach %1") && // Any options that are specified get passed to 'process attach'
549 attach_regex_cmd_ap->AddRegexCommand("^(.+)$", "process attach --name '%1'") &&
550 attach_regex_cmd_ap->AddRegexCommand("^$", "process attach"))
552 CommandObjectSP attach_regex_cmd_sp(attach_regex_cmd_ap.release());
553 m_command_dict[attach_regex_cmd_sp->GetCommandName ()] = attach_regex_cmd_sp;
557 std::unique_ptr<CommandObjectRegexCommand>
558 down_regex_cmd_ap(new CommandObjectRegexCommand (*this,
560 "Go down \"n\" frames in the stack (1 frame by default).",
565 if (down_regex_cmd_ap.get())
567 if (down_regex_cmd_ap->AddRegexCommand("^$", "frame select -r -1") &&
568 down_regex_cmd_ap->AddRegexCommand("^([0-9]+)$", "frame select -r -%1"))
570 CommandObjectSP down_regex_cmd_sp(down_regex_cmd_ap.release());
571 m_command_dict[down_regex_cmd_sp->GetCommandName ()] = down_regex_cmd_sp;
575 std::unique_ptr<CommandObjectRegexCommand>
576 up_regex_cmd_ap(new CommandObjectRegexCommand (*this,
578 "Go up \"n\" frames in the stack (1 frame by default).",
583 if (up_regex_cmd_ap.get())
585 if (up_regex_cmd_ap->AddRegexCommand("^$", "frame select -r 1") &&
586 up_regex_cmd_ap->AddRegexCommand("^([0-9]+)$", "frame select -r %1"))
588 CommandObjectSP up_regex_cmd_sp(up_regex_cmd_ap.release());
589 m_command_dict[up_regex_cmd_sp->GetCommandName ()] = up_regex_cmd_sp;
593 std::unique_ptr<CommandObjectRegexCommand>
594 display_regex_cmd_ap(new CommandObjectRegexCommand (*this,
596 "Add an expression evaluation stop-hook.",
597 "_regexp-display expression",
601 if (display_regex_cmd_ap.get())
603 if (display_regex_cmd_ap->AddRegexCommand("^(.+)$", "target stop-hook add -o \"expr -- %1\""))
605 CommandObjectSP display_regex_cmd_sp(display_regex_cmd_ap.release());
606 m_command_dict[display_regex_cmd_sp->GetCommandName ()] = display_regex_cmd_sp;
610 std::unique_ptr<CommandObjectRegexCommand>
611 undisplay_regex_cmd_ap(new CommandObjectRegexCommand (*this,
613 "Remove an expression evaluation stop-hook.",
614 "_regexp-undisplay stop-hook-number",
618 if (undisplay_regex_cmd_ap.get())
620 if (undisplay_regex_cmd_ap->AddRegexCommand("^([0-9]+)$", "target stop-hook delete %1"))
622 CommandObjectSP undisplay_regex_cmd_sp(undisplay_regex_cmd_ap.release());
623 m_command_dict[undisplay_regex_cmd_sp->GetCommandName ()] = undisplay_regex_cmd_sp;
627 std::unique_ptr<CommandObjectRegexCommand>
628 connect_gdb_remote_cmd_ap(new CommandObjectRegexCommand (*this,
630 "Connect to a remote GDB server. If no hostname is provided, localhost is assumed.",
631 "gdb-remote [<hostname>:]<portnum>",
635 if (connect_gdb_remote_cmd_ap.get())
637 if (connect_gdb_remote_cmd_ap->AddRegexCommand("^([^:]+:[[:digit:]]+)$", "process connect --plugin gdb-remote connect://%1") &&
638 connect_gdb_remote_cmd_ap->AddRegexCommand("^([[:digit:]]+)$", "process connect --plugin gdb-remote connect://localhost:%1"))
640 CommandObjectSP command_sp(connect_gdb_remote_cmd_ap.release());
641 m_command_dict[command_sp->GetCommandName ()] = command_sp;
645 std::unique_ptr<CommandObjectRegexCommand>
646 connect_kdp_remote_cmd_ap(new CommandObjectRegexCommand (*this,
648 "Connect to a remote KDP server. udp port 41139 is the default port number.",
649 "kdp-remote <hostname>[:<portnum>]",
653 if (connect_kdp_remote_cmd_ap.get())
655 if (connect_kdp_remote_cmd_ap->AddRegexCommand("^([^:]+:[[:digit:]]+)$", "process connect --plugin kdp-remote udp://%1") &&
656 connect_kdp_remote_cmd_ap->AddRegexCommand("^(.+)$", "process connect --plugin kdp-remote udp://%1:41139"))
658 CommandObjectSP command_sp(connect_kdp_remote_cmd_ap.release());
659 m_command_dict[command_sp->GetCommandName ()] = command_sp;
663 std::unique_ptr<CommandObjectRegexCommand>
664 bt_regex_cmd_ap(new CommandObjectRegexCommand (*this,
666 "Show a backtrace. An optional argument is accepted; if that argument is a number, it specifies the number of frames to display. If that argument is 'all', full backtraces of all threads are displayed.",
671 if (bt_regex_cmd_ap.get())
673 // accept but don't document "bt -c <number>" -- before bt was a regex command if you wanted to backtrace
674 // three frames you would do "bt -c 3" but the intention is to have this emulate the gdb "bt" command and
675 // so now "bt 3" is the preferred form, in line with gdb.
676 if (bt_regex_cmd_ap->AddRegexCommand("^([[:digit:]]+)$", "thread backtrace -c %1") &&
677 bt_regex_cmd_ap->AddRegexCommand("^-c ([[:digit:]]+)$", "thread backtrace -c %1") &&
678 bt_regex_cmd_ap->AddRegexCommand("^all$", "thread backtrace all") &&
679 bt_regex_cmd_ap->AddRegexCommand("^$", "thread backtrace"))
681 CommandObjectSP command_sp(bt_regex_cmd_ap.release());
682 m_command_dict[command_sp->GetCommandName ()] = command_sp;
686 std::unique_ptr<CommandObjectRegexCommand>
687 list_regex_cmd_ap(new CommandObjectRegexCommand (*this,
689 "Implements the GDB 'list' command in all of its forms except FILE:FUNCTION and maps them to the appropriate 'source list' commands.",
690 "_regexp-list [<line>]\n_regexp-list [<file>:<line>]\n_regexp-list [<file>:<line>]",
692 CommandCompletions::eSourceFileCompletion,
694 if (list_regex_cmd_ap.get())
696 if (list_regex_cmd_ap->AddRegexCommand("^([0-9]+)[[:space:]]*$", "source list --line %1") &&
697 list_regex_cmd_ap->AddRegexCommand("^(.*[^[:space:]])[[:space:]]*:[[:space:]]*([[:digit:]]+)[[:space:]]*$", "source list --file '%1' --line %2") &&
698 list_regex_cmd_ap->AddRegexCommand("^\\*?(0x[[:xdigit:]]+)[[:space:]]*$", "source list --address %1") &&
699 list_regex_cmd_ap->AddRegexCommand("^-[[:space:]]*$", "source list --reverse") &&
700 list_regex_cmd_ap->AddRegexCommand("^-([[:digit:]]+)[[:space:]]*$", "source list --reverse --count %1") &&
701 list_regex_cmd_ap->AddRegexCommand("^(.+)$", "source list --name \"%1\"") &&
702 list_regex_cmd_ap->AddRegexCommand("^$", "source list"))
704 CommandObjectSP list_regex_cmd_sp(list_regex_cmd_ap.release());
705 m_command_dict[list_regex_cmd_sp->GetCommandName ()] = list_regex_cmd_sp;
709 std::unique_ptr<CommandObjectRegexCommand>
710 env_regex_cmd_ap(new CommandObjectRegexCommand (*this,
712 "Implements a shortcut to viewing and setting environment variables.",
713 "_regexp-env\n_regexp-env FOO=BAR",
717 if (env_regex_cmd_ap.get())
719 if (env_regex_cmd_ap->AddRegexCommand("^$", "settings show target.env-vars") &&
720 env_regex_cmd_ap->AddRegexCommand("^([A-Za-z_][A-Za-z_0-9]*=.*)$", "settings set target.env-vars %1"))
722 CommandObjectSP env_regex_cmd_sp(env_regex_cmd_ap.release());
723 m_command_dict[env_regex_cmd_sp->GetCommandName ()] = env_regex_cmd_sp;
727 std::unique_ptr<CommandObjectRegexCommand>
728 jump_regex_cmd_ap(new CommandObjectRegexCommand (*this,
730 "Sets the program counter to a new address.",
731 "_regexp-jump [<line>]\n"
732 "_regexp-jump [<+-lineoffset>]\n"
733 "_regexp-jump [<file>:<line>]\n"
734 "_regexp-jump [*<addr>]\n",
738 if (jump_regex_cmd_ap.get())
740 if (jump_regex_cmd_ap->AddRegexCommand("^\\*(.*)$", "thread jump --addr %1") &&
741 jump_regex_cmd_ap->AddRegexCommand("^([0-9]+)$", "thread jump --line %1") &&
742 jump_regex_cmd_ap->AddRegexCommand("^([^:]+):([0-9]+)$", "thread jump --file %1 --line %2") &&
743 jump_regex_cmd_ap->AddRegexCommand("^([+\\-][0-9]+)$", "thread jump --by %1"))
745 CommandObjectSP jump_regex_cmd_sp(jump_regex_cmd_ap.release());
746 m_command_dict[jump_regex_cmd_sp->GetCommandName ()] = jump_regex_cmd_sp;
753 CommandInterpreter::GetCommandNamesMatchingPartialString (const char *cmd_str, bool include_aliases,
756 CommandObject::AddNamesMatchingPartialString (m_command_dict, cmd_str, matches);
760 CommandObject::AddNamesMatchingPartialString (m_alias_dict, cmd_str, matches);
763 return matches.GetSize();
767 CommandInterpreter::GetCommandSP (const char *cmd_cstr, bool include_aliases, bool exact, StringList *matches)
769 CommandObject::CommandMap::iterator pos;
770 CommandObjectSP command_sp;
772 std::string cmd(cmd_cstr);
776 pos = m_command_dict.find(cmd);
777 if (pos != m_command_dict.end())
778 command_sp = pos->second;
781 if (include_aliases && HasAliases())
783 pos = m_alias_dict.find(cmd);
784 if (pos != m_alias_dict.end())
785 command_sp = pos->second;
788 if (HasUserCommands())
790 pos = m_user_dict.find(cmd);
791 if (pos != m_user_dict.end())
792 command_sp = pos->second;
795 if (!exact && !command_sp)
797 // We will only get into here if we didn't find any exact matches.
799 CommandObjectSP user_match_sp, alias_match_sp, real_match_sp;
801 StringList local_matches;
802 if (matches == nullptr)
803 matches = &local_matches;
805 unsigned int num_cmd_matches = 0;
806 unsigned int num_alias_matches = 0;
807 unsigned int num_user_matches = 0;
809 // Look through the command dictionaries one by one, and if we get only one match from any of
810 // them in toto, then return that, otherwise return an empty CommandObjectSP and the list of matches.
814 num_cmd_matches = CommandObject::AddNamesMatchingPartialString (m_command_dict, cmd_cstr, *matches);
817 if (num_cmd_matches == 1)
819 cmd.assign(matches->GetStringAtIndex(0));
820 pos = m_command_dict.find(cmd);
821 if (pos != m_command_dict.end())
822 real_match_sp = pos->second;
825 if (include_aliases && HasAliases())
827 num_alias_matches = CommandObject::AddNamesMatchingPartialString (m_alias_dict, cmd_cstr, *matches);
831 if (num_alias_matches == 1)
833 cmd.assign(matches->GetStringAtIndex (num_cmd_matches));
834 pos = m_alias_dict.find(cmd);
835 if (pos != m_alias_dict.end())
836 alias_match_sp = pos->second;
839 if (HasUserCommands())
841 num_user_matches = CommandObject::AddNamesMatchingPartialString (m_user_dict, cmd_cstr, *matches);
844 if (num_user_matches == 1)
846 cmd.assign (matches->GetStringAtIndex (num_cmd_matches + num_alias_matches));
848 pos = m_user_dict.find (cmd);
849 if (pos != m_user_dict.end())
850 user_match_sp = pos->second;
853 // If we got exactly one match, return that, otherwise return the match list.
855 if (num_user_matches + num_cmd_matches + num_alias_matches == 1)
858 return real_match_sp;
859 else if (num_alias_matches)
860 return alias_match_sp;
862 return user_match_sp;
865 else if (matches && command_sp)
867 matches->AppendString (cmd_cstr);
875 CommandInterpreter::AddCommand (const char *name, const lldb::CommandObjectSP &cmd_sp, bool can_replace)
879 std::string name_sstr(name);
880 bool found = (m_command_dict.find (name_sstr) != m_command_dict.end());
881 if (found && !can_replace)
883 if (found && m_command_dict[name_sstr]->IsRemovable() == false)
885 m_command_dict[name_sstr] = cmd_sp;
892 CommandInterpreter::AddUserCommand (std::string name,
893 const lldb::CommandObjectSP &cmd_sp,
899 const char* name_cstr = name.c_str();
901 // do not allow replacement of internal commands
902 if (CommandExists(name_cstr))
904 if (can_replace == false)
906 if (m_command_dict[name]->IsRemovable() == false)
910 if (UserCommandExists(name_cstr))
912 if (can_replace == false)
914 if (m_user_dict[name]->IsRemovable() == false)
918 m_user_dict[name] = cmd_sp;
925 CommandInterpreter::GetCommandSPExact (const char *cmd_cstr, bool include_aliases)
927 Args cmd_words (cmd_cstr); // Break up the command string into words, in case it's a multi-word command.
928 CommandObjectSP ret_val; // Possibly empty return value.
930 if (cmd_cstr == nullptr)
933 if (cmd_words.GetArgumentCount() == 1)
934 return GetCommandSP(cmd_cstr, include_aliases, true, nullptr);
937 // We have a multi-word command (seemingly), so we need to do more work.
938 // First, get the cmd_obj_sp for the first word in the command.
939 CommandObjectSP cmd_obj_sp = GetCommandSP (cmd_words.GetArgumentAtIndex (0), include_aliases, true, nullptr);
940 if (cmd_obj_sp.get() != nullptr)
942 // Loop through the rest of the words in the command (everything passed in was supposed to be part of a
943 // command name), and find the appropriate sub-command SP for each command word....
944 size_t end = cmd_words.GetArgumentCount();
945 for (size_t j= 1; j < end; ++j)
947 if (cmd_obj_sp->IsMultiwordObject())
949 cmd_obj_sp = cmd_obj_sp->GetSubcommandSP (cmd_words.GetArgumentAtIndex (j));
950 if (cmd_obj_sp.get() == nullptr)
951 // The sub-command name was invalid. Fail and return the empty 'ret_val'.
955 // We have more words in the command name, but we don't have a multiword object. Fail and return
959 // We successfully looped through all the command words and got valid command objects for them. Assign the
960 // last object retrieved to 'ret_val'.
961 ret_val = cmd_obj_sp;
968 CommandInterpreter::GetCommandObjectExact (const char *cmd_cstr, bool include_aliases)
970 return GetCommandSPExact (cmd_cstr, include_aliases).get();
974 CommandInterpreter::GetCommandObject (const char *cmd_cstr, StringList *matches)
976 CommandObject *command_obj = GetCommandSP (cmd_cstr, false, true, matches).get();
978 // If we didn't find an exact match to the command string in the commands, look in
984 command_obj = GetCommandSP (cmd_cstr, true, true, matches).get();
989 // If there wasn't an exact match then look for an inexact one in just the commands
990 command_obj = GetCommandSP(cmd_cstr, false, false, nullptr).get();
992 // Finally, if there wasn't an inexact match among the commands, look for an inexact
993 // match in both the commands and aliases.
998 matches->AppendString(command_obj->GetCommandName());
1002 return GetCommandSP(cmd_cstr, true, false, matches).get();
1006 CommandInterpreter::CommandExists (const char *cmd)
1008 return m_command_dict.find(cmd) != m_command_dict.end();
1012 CommandInterpreter::ProcessAliasOptionsArgs (lldb::CommandObjectSP &cmd_obj_sp,
1013 const char *options_args,
1014 OptionArgVectorSP &option_arg_vector_sp)
1016 bool success = true;
1017 OptionArgVector *option_arg_vector = option_arg_vector_sp.get();
1019 if (!options_args || (strlen (options_args) < 1))
1022 std::string options_string (options_args);
1023 Args args (options_args);
1024 CommandReturnObject result;
1025 // Check to see if the command being aliased can take any command options.
1026 Options *options = cmd_obj_sp->GetOptions ();
1029 // See if any options were specified as part of the alias; if so, handle them appropriately.
1030 options->NotifyOptionParsingStarting ();
1031 args.Unshift ("dummy_arg");
1032 args.ParseAliasOptions (*options, result, option_arg_vector, options_string);
1034 if (result.Succeeded())
1035 options->VerifyPartialOptions (result);
1036 if (!result.Succeeded() && result.GetStatus() != lldb::eReturnStatusStarted)
1038 result.AppendError ("Unable to create requested alias.\n");
1043 if (!options_string.empty())
1045 if (cmd_obj_sp->WantsRawCommandString ())
1046 option_arg_vector->push_back (OptionArgPair ("<argument>",
1051 const size_t argc = args.GetArgumentCount();
1052 for (size_t i = 0; i < argc; ++i)
1053 if (strcmp (args.GetArgumentAtIndex (i), "") != 0)
1054 option_arg_vector->push_back
1055 (OptionArgPair ("<argument>",
1057 std::string (args.GetArgumentAtIndex (i)))));
1065 CommandInterpreter::GetAliasFullName (const char *cmd, std::string &full_name)
1067 bool exact_match = (m_alias_dict.find(cmd) != m_alias_dict.end());
1070 full_name.assign(cmd);
1076 size_t num_alias_matches;
1077 num_alias_matches = CommandObject::AddNamesMatchingPartialString (m_alias_dict, cmd, matches);
1078 if (num_alias_matches == 1)
1080 // Make sure this isn't shadowing a command in the regular command space:
1081 StringList regular_matches;
1082 const bool include_aliases = false;
1083 const bool exact = false;
1084 CommandObjectSP cmd_obj_sp(GetCommandSP (cmd, include_aliases, exact, ®ular_matches));
1085 if (cmd_obj_sp || regular_matches.GetSize() > 0)
1089 full_name.assign (matches.GetStringAtIndex(0));
1099 CommandInterpreter::AliasExists (const char *cmd)
1101 return m_alias_dict.find(cmd) != m_alias_dict.end();
1105 CommandInterpreter::UserCommandExists (const char *cmd)
1107 return m_user_dict.find(cmd) != m_user_dict.end();
1111 CommandInterpreter::AddAlias (const char *alias_name, CommandObjectSP& command_obj_sp)
1113 command_obj_sp->SetIsAlias (true);
1114 m_alias_dict[alias_name] = command_obj_sp;
1118 CommandInterpreter::RemoveAlias (const char *alias_name)
1120 CommandObject::CommandMap::iterator pos = m_alias_dict.find(alias_name);
1121 if (pos != m_alias_dict.end())
1123 m_alias_dict.erase(pos);
1130 CommandInterpreter::RemoveCommand (const char *cmd)
1132 auto pos = m_command_dict.find(cmd);
1133 if (pos != m_command_dict.end())
1135 if (pos->second->IsRemovable())
1137 // Only regular expression objects or python commands are removable
1138 m_command_dict.erase(pos);
1145 CommandInterpreter::RemoveUser (const char *alias_name)
1147 CommandObject::CommandMap::iterator pos = m_user_dict.find(alias_name);
1148 if (pos != m_user_dict.end())
1150 m_user_dict.erase(pos);
1157 CommandInterpreter::GetAliasHelp (const char *alias_name, const char *command_name, StreamString &help_string)
1159 help_string.Printf ("'%s", command_name);
1160 OptionArgVectorSP option_arg_vector_sp = GetAliasOptions (alias_name);
1162 if (option_arg_vector_sp)
1164 OptionArgVector *options = option_arg_vector_sp.get();
1165 for (size_t i = 0; i < options->size(); ++i)
1167 OptionArgPair cur_option = (*options)[i];
1168 std::string opt = cur_option.first;
1169 OptionArgValue value_pair = cur_option.second;
1170 std::string value = value_pair.second;
1171 if (opt.compare("<argument>") == 0)
1173 help_string.Printf (" %s", value.c_str());
1177 help_string.Printf (" %s", opt.c_str());
1178 if ((value.compare ("<no-argument>") != 0)
1179 && (value.compare ("<need-argument") != 0))
1181 help_string.Printf (" %s", value.c_str());
1187 help_string.Printf ("'");
1191 CommandInterpreter::FindLongestCommandWord (CommandObject::CommandMap &dict)
1193 CommandObject::CommandMap::const_iterator pos;
1194 CommandObject::CommandMap::const_iterator end = dict.end();
1197 for (pos = dict.begin(); pos != end; ++pos)
1199 size_t len = pos->first.size();
1207 CommandInterpreter::GetHelp (CommandReturnObject &result,
1210 const char * help_prologue = GetDebugger().GetIOHandlerHelpPrologue();
1211 if (help_prologue != NULL)
1213 OutputFormattedHelpText(result.GetOutputStream(), NULL, help_prologue);
1216 CommandObject::CommandMap::const_iterator pos;
1217 size_t max_len = FindLongestCommandWord (m_command_dict);
1219 if ( (cmd_types & eCommandTypesBuiltin) == eCommandTypesBuiltin )
1221 result.AppendMessage("Debugger commands:");
1222 result.AppendMessage("");
1224 for (pos = m_command_dict.begin(); pos != m_command_dict.end(); ++pos)
1226 if (!(cmd_types & eCommandTypesHidden) && (pos->first.compare(0, 1, "_") == 0))
1229 OutputFormattedHelpText (result.GetOutputStream(), pos->first.c_str(), "--", pos->second->GetHelp(),
1232 result.AppendMessage("");
1236 if (!m_alias_dict.empty() && ( (cmd_types & eCommandTypesAliases) == eCommandTypesAliases ))
1238 result.AppendMessageWithFormat("Current command abbreviations "
1239 "(type '%shelp command alias' for more info):\n",
1240 GetCommandPrefix());
1241 result.AppendMessage("");
1242 max_len = FindLongestCommandWord (m_alias_dict);
1244 for (pos = m_alias_dict.begin(); pos != m_alias_dict.end(); ++pos)
1247 StreamString translation_and_help;
1248 std::string entry_name = pos->first;
1249 std::string second_entry = pos->second.get()->GetCommandName();
1250 GetAliasHelp (pos->first.c_str(), pos->second->GetCommandName(), sstr);
1252 translation_and_help.Printf ("(%s) %s", sstr.GetData(), pos->second->GetHelp());
1253 OutputFormattedHelpText (result.GetOutputStream(), pos->first.c_str(), "--",
1254 translation_and_help.GetData(), max_len);
1256 result.AppendMessage("");
1259 if (!m_user_dict.empty() && ( (cmd_types & eCommandTypesUserDef) == eCommandTypesUserDef ))
1261 result.AppendMessage ("Current user-defined commands:");
1262 result.AppendMessage("");
1263 max_len = FindLongestCommandWord (m_user_dict);
1264 for (pos = m_user_dict.begin(); pos != m_user_dict.end(); ++pos)
1266 OutputFormattedHelpText (result.GetOutputStream(), pos->first.c_str(), "--", pos->second->GetHelp(),
1269 result.AppendMessage("");
1272 result.AppendMessageWithFormat("For more information on any command, type '%shelp <command-name>'.\n",
1273 GetCommandPrefix());
1277 CommandInterpreter::GetCommandObjectForCommand (std::string &command_string)
1279 // This function finds the final, lowest-level, alias-resolved command object whose 'Execute' function will
1280 // eventually be invoked by the given command line.
1282 CommandObject *cmd_obj = nullptr;
1283 size_t start = command_string.find_first_not_of (k_white_space);
1288 if (start != std::string::npos)
1290 // Get the next word from command_string.
1291 end = command_string.find_first_of (k_white_space, start);
1292 if (end == std::string::npos)
1293 end = command_string.size();
1294 std::string cmd_word = command_string.substr (start, end - start);
1296 if (cmd_obj == nullptr)
1297 // Since cmd_obj is NULL we are on our first time through this loop. Check to see if cmd_word is a valid
1298 // command or alias.
1299 cmd_obj = GetCommandObject (cmd_word.c_str());
1300 else if (cmd_obj->IsMultiwordObject ())
1302 // Our current object is a multi-word object; see if the cmd_word is a valid sub-command for our object.
1303 CommandObject *sub_cmd_obj = cmd_obj->GetSubcommandObject (cmd_word.c_str());
1305 cmd_obj = sub_cmd_obj;
1306 else // cmd_word was not a valid sub-command word, so we are done
1310 // We have a cmd_obj and it is not a multi-word object, so we are done.
1313 // If we didn't find a valid command object, or our command object is not a multi-word object, or
1314 // we are at the end of the command_string, then we are done. Otherwise, find the start of the
1317 if (!cmd_obj || !cmd_obj->IsMultiwordObject() || end >= command_string.size())
1320 start = command_string.find_first_not_of (k_white_space, end);
1323 // Unable to find any more words.
1327 if (end == command_string.size())
1328 command_string.clear();
1330 command_string = command_string.substr(end);
1335 static const char *k_valid_command_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_";
1337 StripLeadingSpaces (std::string &s)
1341 size_t pos = s.find_first_not_of (k_white_space);
1342 if (pos == std::string::npos)
1351 FindArgumentTerminator (const std::string &s)
1353 const size_t s_len = s.size();
1355 while (offset < s_len)
1357 size_t pos = s.find ("--", offset);
1358 if (pos == std::string::npos)
1362 if (isspace(s[pos-1]))
1364 // Check if the string ends "\s--" (where \s is a space character)
1365 // or if we have "\s--\s".
1366 if ((pos + 2 >= s_len) || isspace(s[pos+2]))
1374 return std::string::npos;
1378 ExtractCommand (std::string &command_string, std::string &command, std::string &suffix, char "e_char)
1382 StripLeadingSpaces (command_string);
1384 bool result = false;
1387 if (!command_string.empty())
1389 const char first_char = command_string[0];
1390 if (first_char == '\'' || first_char == '"')
1392 quote_char = first_char;
1393 const size_t end_quote_pos = command_string.find (quote_char, 1);
1394 if (end_quote_pos == std::string::npos)
1396 command.swap (command_string);
1397 command_string.erase ();
1401 command.assign (command_string, 1, end_quote_pos - 1);
1402 if (end_quote_pos + 1 < command_string.size())
1403 command_string.erase (0, command_string.find_first_not_of (k_white_space, end_quote_pos + 1));
1405 command_string.erase ();
1410 const size_t first_space_pos = command_string.find_first_of (k_white_space);
1411 if (first_space_pos == std::string::npos)
1413 command.swap (command_string);
1414 command_string.erase();
1418 command.assign (command_string, 0, first_space_pos);
1419 command_string.erase(0, command_string.find_first_not_of (k_white_space, first_space_pos));
1426 if (!command.empty())
1428 // actual commands can't start with '-' or '_'
1429 if (command[0] != '-' && command[0] != '_')
1431 size_t pos = command.find_first_not_of(k_valid_command_chars);
1432 if (pos > 0 && pos != std::string::npos)
1434 suffix.assign (command.begin() + pos, command.end());
1435 command.erase (pos);
1444 CommandInterpreter::BuildAliasResult (const char *alias_name,
1445 std::string &raw_input_string,
1446 std::string &alias_result,
1447 CommandReturnObject &result)
1449 CommandObject *alias_cmd_obj = nullptr;
1450 Args cmd_args (raw_input_string);
1451 alias_cmd_obj = GetCommandObject (alias_name);
1452 StreamString result_str;
1456 std::string alias_name_str = alias_name;
1457 if ((cmd_args.GetArgumentCount() == 0)
1458 || (alias_name_str.compare (cmd_args.GetArgumentAtIndex(0)) != 0))
1459 cmd_args.Unshift (alias_name);
1461 result_str.Printf ("%s", alias_cmd_obj->GetCommandName ());
1462 OptionArgVectorSP option_arg_vector_sp = GetAliasOptions (alias_name);
1464 if (option_arg_vector_sp.get())
1466 OptionArgVector *option_arg_vector = option_arg_vector_sp.get();
1468 for (size_t i = 0; i < option_arg_vector->size(); ++i)
1470 OptionArgPair option_pair = (*option_arg_vector)[i];
1471 OptionArgValue value_pair = option_pair.second;
1472 int value_type = value_pair.first;
1473 std::string option = option_pair.first;
1474 std::string value = value_pair.second;
1475 if (option.compare ("<argument>") == 0)
1476 result_str.Printf (" %s", value.c_str());
1479 result_str.Printf (" %s", option.c_str());
1480 if (value_type != OptionParser::eNoArgument)
1482 if (value_type != OptionParser::eOptionalArgument)
1483 result_str.Printf (" ");
1484 int index = GetOptionArgumentPosition (value.c_str());
1486 result_str.Printf ("%s", value.c_str());
1487 else if (static_cast<size_t>(index) >= cmd_args.GetArgumentCount())
1490 result.AppendErrorWithFormat
1491 ("Not enough arguments provided; you need at least %d arguments to use this alias.\n",
1493 result.SetStatus (eReturnStatusFailed);
1498 size_t strpos = raw_input_string.find (cmd_args.GetArgumentAtIndex (index));
1499 if (strpos != std::string::npos)
1500 raw_input_string = raw_input_string.erase (strpos,
1501 strlen (cmd_args.GetArgumentAtIndex (index)));
1502 result_str.Printf ("%s", cmd_args.GetArgumentAtIndex (index));
1509 alias_result = result_str.GetData();
1511 return alias_cmd_obj;
1515 CommandInterpreter::PreprocessCommand (std::string &command)
1517 // The command preprocessor needs to do things to the command
1518 // line before any parsing of arguments or anything else is done.
1519 // The only current stuff that gets preprocessed is anything enclosed
1520 // in backtick ('`') characters is evaluated as an expression and
1521 // the result of the expression must be a scalar that can be substituted
1522 // into the command. An example would be:
1523 // (lldb) memory read `$rsp + 20`
1524 Error error; // Error for any expressions that might not evaluate
1525 size_t start_backtick;
1527 while ((start_backtick = command.find ('`', pos)) != std::string::npos)
1529 if (start_backtick > 0 && command[start_backtick-1] == '\\')
1531 // The backtick was preceded by a '\' character, remove the slash
1532 // and don't treat the backtick as the start of an expression
1533 command.erase(start_backtick-1, 1);
1534 // No need to add one to start_backtick since we just deleted a char
1535 pos = start_backtick;
1539 const size_t expr_content_start = start_backtick + 1;
1540 const size_t end_backtick = command.find ('`', expr_content_start);
1541 if (end_backtick == std::string::npos)
1543 else if (end_backtick == expr_content_start)
1545 // Empty expression (two backticks in a row)
1546 command.erase (start_backtick, 2);
1550 std::string expr_str (command, expr_content_start, end_backtick - expr_content_start);
1552 ExecutionContext exe_ctx(GetExecutionContext());
1553 Target *target = exe_ctx.GetTargetPtr();
1554 // Get a dummy target to allow for calculator mode while processing backticks.
1555 // This also helps break the infinite loop caused when target is null.
1557 target = m_debugger.GetDummyTarget();
1560 ValueObjectSP expr_result_valobj_sp;
1562 EvaluateExpressionOptions options;
1563 options.SetCoerceToId(false);
1564 options.SetUnwindOnError(true);
1565 options.SetIgnoreBreakpoints(true);
1566 options.SetKeepInMemory(false);
1567 options.SetTryAllThreads(true);
1568 options.SetTimeoutUsec(0);
1570 ExpressionResults expr_result = target->EvaluateExpression (expr_str.c_str(),
1571 exe_ctx.GetFramePtr(),
1572 expr_result_valobj_sp,
1575 if (expr_result == eExpressionCompleted)
1578 if (expr_result_valobj_sp)
1579 expr_result_valobj_sp = expr_result_valobj_sp->GetQualifiedRepresentationIfAvailable(expr_result_valobj_sp->GetDynamicValueType(), true);
1580 if (expr_result_valobj_sp->ResolveValue (scalar))
1582 command.erase (start_backtick, end_backtick - start_backtick + 1);
1583 StreamString value_strm;
1584 const bool show_type = false;
1585 scalar.GetValue (&value_strm, show_type);
1586 size_t value_string_size = value_strm.GetSize();
1587 if (value_string_size)
1589 command.insert (start_backtick, value_strm.GetData(), value_string_size);
1590 pos = start_backtick + value_string_size;
1595 error.SetErrorStringWithFormat("expression value didn't result in a scalar value for the expression '%s'", expr_str.c_str());
1600 error.SetErrorStringWithFormat("expression value didn't result in a scalar value for the expression '%s'", expr_str.c_str());
1605 if (expr_result_valobj_sp)
1606 error = expr_result_valobj_sp->GetError();
1607 if (error.Success())
1610 switch (expr_result)
1612 case eExpressionSetupError:
1613 error.SetErrorStringWithFormat("expression setup error for the expression '%s'", expr_str.c_str());
1615 case eExpressionParseError:
1616 error.SetErrorStringWithFormat ("expression parse error for the expression '%s'", expr_str.c_str());
1618 case eExpressionResultUnavailable:
1619 error.SetErrorStringWithFormat ("expression error fetching result for the expression '%s'", expr_str.c_str());
1620 case eExpressionCompleted:
1622 case eExpressionDiscarded:
1623 error.SetErrorStringWithFormat("expression discarded for the expression '%s'", expr_str.c_str());
1625 case eExpressionInterrupted:
1626 error.SetErrorStringWithFormat("expression interrupted for the expression '%s'", expr_str.c_str());
1628 case eExpressionHitBreakpoint:
1629 error.SetErrorStringWithFormat("expression hit breakpoint for the expression '%s'", expr_str.c_str());
1631 case eExpressionTimedOut:
1632 error.SetErrorStringWithFormat("expression timed out for the expression '%s'", expr_str.c_str());
1634 case eExpressionStoppedForDebug:
1635 error.SetErrorStringWithFormat("expression stop at entry point for debugging for the expression '%s'", expr_str.c_str());
1651 CommandInterpreter::HandleCommand (const char *command_line,
1652 LazyBool lazy_add_to_history,
1653 CommandReturnObject &result,
1654 ExecutionContext *override_context,
1655 bool repeat_on_empty_command,
1656 bool no_context_switching)
1660 std::string command_string (command_line);
1661 std::string original_command_string (command_line);
1663 Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_COMMANDS));
1664 Host::SetCrashDescriptionWithFormat ("HandleCommand(command = \"%s\")", command_line);
1666 // Make a scoped cleanup object that will clear the crash description string
1667 // on exit of this function.
1668 lldb_utility::CleanUp <const char *> crash_description_cleanup(nullptr, Host::SetCrashDescription);
1671 log->Printf ("Processing command: %s", command_line);
1673 Timer scoped_timer (__PRETTY_FUNCTION__, "Handling command: %s.", command_line);
1675 if (!no_context_switching)
1676 UpdateExecutionContext (override_context);
1678 bool add_to_history;
1679 if (lazy_add_to_history == eLazyBoolCalculate)
1680 add_to_history = (m_command_source_depth == 0);
1682 add_to_history = (lazy_add_to_history == eLazyBoolYes);
1684 bool empty_command = false;
1685 bool comment_command = false;
1686 if (command_string.empty())
1687 empty_command = true;
1690 const char *k_space_characters = "\t\n\v\f\r ";
1692 size_t non_space = command_string.find_first_not_of (k_space_characters);
1693 // Check for empty line or comment line (lines whose first
1694 // non-space character is the comment character for this interpreter)
1695 if (non_space == std::string::npos)
1696 empty_command = true;
1697 else if (command_string[non_space] == m_comment_char)
1698 comment_command = true;
1699 else if (command_string[non_space] == CommandHistory::g_repeat_char)
1701 const char *history_string = m_command_history.FindString(command_string.c_str() + non_space);
1702 if (history_string == nullptr)
1704 result.AppendErrorWithFormat ("Could not find entry: %s in history", command_string.c_str());
1705 result.SetStatus(eReturnStatusFailed);
1708 add_to_history = false;
1709 command_string = history_string;
1710 original_command_string = history_string;
1716 if (repeat_on_empty_command)
1718 if (m_command_history.IsEmpty())
1720 result.AppendError ("empty command");
1721 result.SetStatus(eReturnStatusFailed);
1726 command_line = m_repeat_command.c_str();
1727 command_string = command_line;
1728 original_command_string = command_line;
1729 if (m_repeat_command.empty())
1731 result.AppendErrorWithFormat("No auto repeat.\n");
1732 result.SetStatus (eReturnStatusFailed);
1736 add_to_history = false;
1740 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1744 else if (comment_command)
1746 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1751 Error error (PreprocessCommand (command_string));
1755 result.AppendError (error.AsCString());
1756 result.SetStatus(eReturnStatusFailed);
1762 // Before we do ANY kind of argument processing, we need to figure out what
1763 // the real/final command object is for the specified command. This gets
1764 // complicated by the fact that the user could have specified an alias, and,
1765 // in translating the alias, there may also be command options and/or even
1766 // data (including raw text strings) that need to be found and inserted into
1767 // the command line as part of the translation. So this first step is plain
1768 // look-up and replacement, resulting in:
1769 // 1. the command object whose Execute method will actually be called
1770 // 2. a revised command string, with all substitutions and replacements
1772 // From 1 above, we can determine whether the Execute function wants raw
1775 CommandObject *cmd_obj = ResolveCommandImpl(command_string, result);
1777 // Although the user may have abbreviated the command, the command_string now
1778 // has the command expanded to the full name. For example, if the input
1779 // was "br s -n main", command_string is now "breakpoint set -n main".
1783 log->Printf("HandleCommand, cmd_obj : '%s'", cmd_obj ? cmd_obj->GetCommandName() : "<not found>");
1784 log->Printf("HandleCommand, (revised) command_string: '%s'", command_string.c_str());
1785 const bool wants_raw_input = (cmd_obj != NULL) ? cmd_obj->WantsRawCommandString() : false;
1786 log->Printf("HandleCommand, wants_raw_input:'%s'", wants_raw_input ? "True" : "False");
1790 // Take care of things like setting up the history command & calling the appropriate Execute method on the
1791 // CommandObject, with the appropriate arguments.
1793 if (cmd_obj != nullptr)
1797 Args command_args (command_string);
1798 const char *repeat_command = cmd_obj->GetRepeatCommand(command_args, 0);
1799 if (repeat_command != nullptr)
1800 m_repeat_command.assign(repeat_command);
1802 m_repeat_command.assign(original_command_string.c_str());
1804 m_command_history.AppendString (original_command_string);
1807 std::string remainder;
1808 const std::size_t actual_cmd_name_len = strlen (cmd_obj->GetCommandName());
1809 if (actual_cmd_name_len < command_string.length())
1810 remainder = command_string.substr (actual_cmd_name_len);
1812 // Remove any initial spaces
1813 size_t pos = remainder.find_first_not_of (k_white_space);
1814 if (pos != 0 && pos != std::string::npos)
1815 remainder.erase(0, pos);
1818 log->Printf ("HandleCommand, command line after removing command name(s): '%s'", remainder.c_str());
1820 cmd_obj->Execute (remainder.c_str(), result);
1824 // We didn't find the first command object, so complete the first argument.
1825 Args command_args (command_string);
1828 int cursor_index = 0;
1829 int cursor_char_position = strlen (command_args.GetArgumentAtIndex(0));
1831 num_matches = HandleCompletionMatches (command_args,
1833 cursor_char_position,
1839 if (num_matches > 0)
1841 std::string error_msg;
1842 error_msg.assign ("ambiguous command '");
1843 error_msg.append(command_args.GetArgumentAtIndex(0));
1844 error_msg.append ("'.");
1846 error_msg.append (" Possible completions:");
1847 for (int i = 0; i < num_matches; i++)
1849 error_msg.append ("\n\t");
1850 error_msg.append (matches.GetStringAtIndex (i));
1852 error_msg.append ("\n");
1853 result.AppendRawError (error_msg.c_str());
1856 result.AppendErrorWithFormat ("Unrecognized command '%s'.\n", command_args.GetArgumentAtIndex (0));
1858 result.SetStatus (eReturnStatusFailed);
1862 log->Printf ("HandleCommand, command %s", (result.Succeeded() ? "succeeded" : "did not succeed"));
1864 return result.Succeeded();
1868 CommandInterpreter::HandleCompletionMatches (Args &parsed_line,
1870 int &cursor_char_position,
1871 int match_start_point,
1872 int max_return_elements,
1873 bool &word_complete,
1874 StringList &matches)
1876 int num_command_matches = 0;
1877 bool look_for_subcommand = false;
1879 // For any of the command completions a unique match will be a complete word.
1880 word_complete = true;
1882 if (cursor_index == -1)
1884 // We got nothing on the command line, so return the list of commands
1885 bool include_aliases = true;
1886 num_command_matches = GetCommandNamesMatchingPartialString ("", include_aliases, matches);
1888 else if (cursor_index == 0)
1890 // The cursor is in the first argument, so just do a lookup in the dictionary.
1891 CommandObject *cmd_obj = GetCommandObject (parsed_line.GetArgumentAtIndex(0), &matches);
1892 num_command_matches = matches.GetSize();
1894 if (num_command_matches == 1
1895 && cmd_obj && cmd_obj->IsMultiwordObject()
1896 && matches.GetStringAtIndex(0) != nullptr
1897 && strcmp (parsed_line.GetArgumentAtIndex(0), matches.GetStringAtIndex(0)) == 0)
1899 if (parsed_line.GetArgumentCount() == 1)
1901 word_complete = true;
1905 look_for_subcommand = true;
1906 num_command_matches = 0;
1907 matches.DeleteStringAtIndex(0);
1908 parsed_line.AppendArgument ("");
1910 cursor_char_position = 0;
1915 if (cursor_index > 0 || look_for_subcommand)
1917 // We are completing further on into a commands arguments, so find the command and tell it
1918 // to complete the command.
1919 // First see if there is a matching initial command:
1920 CommandObject *command_object = GetCommandObject (parsed_line.GetArgumentAtIndex(0));
1921 if (command_object == nullptr)
1927 parsed_line.Shift();
1929 num_command_matches = command_object->HandleCompletion (parsed_line,
1931 cursor_char_position,
1933 max_return_elements,
1939 return num_command_matches;
1944 CommandInterpreter::HandleCompletion (const char *current_line,
1946 const char *last_char,
1947 int match_start_point,
1948 int max_return_elements,
1949 StringList &matches)
1951 // We parse the argument up to the cursor, so the last argument in parsed_line is
1952 // the one containing the cursor, and the cursor is after the last character.
1954 Args parsed_line(llvm::StringRef(current_line, last_char - current_line));
1955 Args partial_parsed_line(llvm::StringRef(current_line, cursor - current_line));
1957 // Don't complete comments, and if the line we are completing is just the history repeat character,
1958 // substitute the appropriate history line.
1959 const char *first_arg = parsed_line.GetArgumentAtIndex(0);
1962 if (first_arg[0] == m_comment_char)
1964 else if (first_arg[0] == CommandHistory::g_repeat_char)
1966 const char *history_string = m_command_history.FindString (first_arg);
1967 if (history_string != nullptr)
1970 matches.InsertStringAtIndex(0, history_string);
1980 int num_args = partial_parsed_line.GetArgumentCount();
1981 int cursor_index = partial_parsed_line.GetArgumentCount() - 1;
1982 int cursor_char_position;
1984 if (cursor_index == -1)
1985 cursor_char_position = 0;
1987 cursor_char_position = strlen (partial_parsed_line.GetArgumentAtIndex(cursor_index));
1989 if (cursor > current_line && cursor[-1] == ' ')
1991 // We are just after a space. If we are in an argument, then we will continue
1992 // parsing, but if we are between arguments, then we have to complete whatever the next
1993 // element would be.
1994 // We can distinguish the two cases because if we are in an argument (e.g. because the space is
1995 // protected by a quote) then the space will also be in the parsed argument...
1997 const char *current_elem = partial_parsed_line.GetArgumentAtIndex(cursor_index);
1998 if (cursor_char_position == 0 || current_elem[cursor_char_position - 1] != ' ')
2000 parsed_line.InsertArgumentAtIndex(cursor_index + 1, "", '\0');
2002 cursor_char_position = 0;
2006 int num_command_matches;
2010 // Only max_return_elements == -1 is supported at present:
2011 assert (max_return_elements == -1);
2013 num_command_matches = HandleCompletionMatches (parsed_line,
2015 cursor_char_position,
2017 max_return_elements,
2021 if (num_command_matches <= 0)
2022 return num_command_matches;
2026 // If we got an empty string, insert nothing.
2027 matches.InsertStringAtIndex(0, "");
2031 // Now figure out if there is a common substring, and if so put that in element 0, otherwise
2032 // put an empty string in element 0.
2033 std::string command_partial_str;
2034 if (cursor_index >= 0)
2035 command_partial_str.assign(parsed_line.GetArgumentAtIndex(cursor_index),
2036 parsed_line.GetArgumentAtIndex(cursor_index) + cursor_char_position);
2038 std::string common_prefix;
2039 matches.LongestCommonPrefix (common_prefix);
2040 const size_t partial_name_len = command_partial_str.size();
2041 common_prefix.erase (0, partial_name_len);
2043 // If we matched a unique single command, add a space...
2044 // Only do this if the completer told us this was a complete word, however...
2045 if (num_command_matches == 1 && word_complete)
2047 char quote_char = parsed_line.GetArgumentQuoteCharAtIndex(cursor_index);
2048 common_prefix = Args::EscapeLLDBCommandArgument(common_prefix, quote_char);
2049 if (quote_char != '\0')
2050 common_prefix.push_back(quote_char);
2051 common_prefix.push_back(' ');
2053 matches.InsertStringAtIndex(0, common_prefix.c_str());
2055 return num_command_matches;
2059 CommandInterpreter::~CommandInterpreter ()
2064 CommandInterpreter::UpdatePrompt (const char *new_prompt)
2066 EventSP prompt_change_event_sp (new Event(eBroadcastBitResetPrompt, new EventDataBytes (new_prompt)));;
2067 BroadcastEvent (prompt_change_event_sp);
2068 if (m_command_io_handler_sp)
2069 m_command_io_handler_sp->SetPrompt(new_prompt);
2074 CommandInterpreter::Confirm (const char *message, bool default_answer)
2076 // Check AutoConfirm first:
2077 if (m_debugger.GetAutoConfirm())
2078 return default_answer;
2080 IOHandlerConfirm *confirm = new IOHandlerConfirm(m_debugger,
2083 IOHandlerSP io_handler_sp (confirm);
2084 m_debugger.RunIOHandler (io_handler_sp);
2085 return confirm->GetResponse();
2089 CommandInterpreter::GetAliasOptions (const char *alias_name)
2091 OptionArgMap::iterator pos;
2092 OptionArgVectorSP ret_val;
2094 std::string alias (alias_name);
2096 if (HasAliasOptions())
2098 pos = m_alias_options.find (alias);
2099 if (pos != m_alias_options.end())
2100 ret_val = pos->second;
2107 CommandInterpreter::RemoveAliasOptions (const char *alias_name)
2109 OptionArgMap::iterator pos = m_alias_options.find(alias_name);
2110 if (pos != m_alias_options.end())
2112 m_alias_options.erase (pos);
2117 CommandInterpreter::AddOrReplaceAliasOptions (const char *alias_name, OptionArgVectorSP &option_arg_vector_sp)
2119 m_alias_options[alias_name] = option_arg_vector_sp;
2123 CommandInterpreter::HasCommands ()
2125 return (!m_command_dict.empty());
2129 CommandInterpreter::HasAliases ()
2131 return (!m_alias_dict.empty());
2135 CommandInterpreter::HasUserCommands ()
2137 return (!m_user_dict.empty());
2141 CommandInterpreter::HasAliasOptions ()
2143 return (!m_alias_options.empty());
2147 CommandInterpreter::BuildAliasCommandArgs (CommandObject *alias_cmd_obj,
2148 const char *alias_name,
2150 std::string &raw_input_string,
2151 CommandReturnObject &result)
2153 OptionArgVectorSP option_arg_vector_sp = GetAliasOptions (alias_name);
2155 bool wants_raw_input = alias_cmd_obj->WantsRawCommandString();
2157 // Make sure that the alias name is the 0th element in cmd_args
2158 std::string alias_name_str = alias_name;
2159 if (alias_name_str.compare (cmd_args.GetArgumentAtIndex(0)) != 0)
2160 cmd_args.Unshift (alias_name);
2162 Args new_args (alias_cmd_obj->GetCommandName());
2163 if (new_args.GetArgumentCount() == 2)
2166 if (option_arg_vector_sp.get())
2168 if (wants_raw_input)
2170 // We have a command that both has command options and takes raw input. Make *sure* it has a
2171 // " -- " in the right place in the raw_input_string.
2172 size_t pos = raw_input_string.find(" -- ");
2173 if (pos == std::string::npos)
2175 // None found; assume it goes at the beginning of the raw input string
2176 raw_input_string.insert (0, " -- ");
2180 OptionArgVector *option_arg_vector = option_arg_vector_sp.get();
2181 const size_t old_size = cmd_args.GetArgumentCount();
2182 std::vector<bool> used (old_size + 1, false);
2186 for (size_t i = 0; i < option_arg_vector->size(); ++i)
2188 OptionArgPair option_pair = (*option_arg_vector)[i];
2189 OptionArgValue value_pair = option_pair.second;
2190 int value_type = value_pair.first;
2191 std::string option = option_pair.first;
2192 std::string value = value_pair.second;
2193 if (option.compare ("<argument>") == 0)
2195 if (!wants_raw_input
2196 || (value.compare("--") != 0)) // Since we inserted this above, make sure we don't insert it twice
2197 new_args.AppendArgument (value.c_str());
2201 if (value_type != OptionParser::eOptionalArgument)
2202 new_args.AppendArgument (option.c_str());
2203 if (value.compare ("<no-argument>") != 0)
2205 int index = GetOptionArgumentPosition (value.c_str());
2208 // value was NOT a positional argument; must be a real value
2209 if (value_type != OptionParser::eOptionalArgument)
2210 new_args.AppendArgument (value.c_str());
2214 ::snprintf (buffer, sizeof (buffer), "%s%s", option.c_str(), value.c_str());
2215 new_args.AppendArgument (buffer);
2219 else if (static_cast<size_t>(index) >= cmd_args.GetArgumentCount())
2221 result.AppendErrorWithFormat
2222 ("Not enough arguments provided; you need at least %d arguments to use this alias.\n",
2224 result.SetStatus (eReturnStatusFailed);
2229 // Find and remove cmd_args.GetArgumentAtIndex(i) from raw_input_string
2230 size_t strpos = raw_input_string.find (cmd_args.GetArgumentAtIndex (index));
2231 if (strpos != std::string::npos)
2233 raw_input_string = raw_input_string.erase (strpos, strlen (cmd_args.GetArgumentAtIndex (index)));
2236 if (value_type != OptionParser::eOptionalArgument)
2237 new_args.AppendArgument (cmd_args.GetArgumentAtIndex (index));
2241 ::snprintf (buffer, sizeof(buffer), "%s%s", option.c_str(),
2242 cmd_args.GetArgumentAtIndex (index));
2243 new_args.AppendArgument (buffer);
2251 for (size_t j = 0; j < cmd_args.GetArgumentCount(); ++j)
2253 if (!used[j] && !wants_raw_input)
2254 new_args.AppendArgument (cmd_args.GetArgumentAtIndex (j));
2258 cmd_args.SetArguments (new_args.GetArgumentCount(), new_args.GetConstArgumentVector());
2262 result.SetStatus (eReturnStatusSuccessFinishNoResult);
2263 // This alias was not created with any options; nothing further needs to be done, unless it is a command that
2264 // wants raw input, in which case we need to clear the rest of the data from cmd_args, since its in the raw
2266 if (wants_raw_input)
2269 cmd_args.SetArguments (new_args.GetArgumentCount(), new_args.GetConstArgumentVector());
2274 result.SetStatus (eReturnStatusSuccessFinishNoResult);
2280 CommandInterpreter::GetOptionArgumentPosition (const char *in_string)
2282 int position = 0; // Any string that isn't an argument position, i.e. '%' followed by an integer, gets a position
2285 const char *cptr = in_string;
2287 // Does it start with '%'
2292 // Is the rest of it entirely digits?
2293 if (isdigit (cptr[0]))
2295 const char *start = cptr;
2296 while (isdigit (cptr[0]))
2299 // We've gotten to the end of the digits; are we at the end of the string?
2300 if (cptr[0] == '\0')
2301 position = atoi (start);
2309 CommandInterpreter::SourceInitFile (bool in_cwd, CommandReturnObject &result)
2314 // In the current working directory we don't load any program specific
2315 // .lldbinit files, we only look for a "./.lldbinit" file.
2316 if (m_skip_lldbinit_files)
2319 init_file.SetFile ("./.lldbinit", true);
2323 // If we aren't looking in the current working directory we are looking
2324 // in the home directory. We will first see if there is an application
2325 // specific ".lldbinit" file whose name is "~/.lldbinit" followed by a
2326 // "-" and the name of the program. If this file doesn't exist, we fall
2327 // back to just the "~/.lldbinit" file. We also obey any requests to not
2328 // load the init files.
2329 llvm::SmallString<64> home_dir_path;
2330 llvm::sys::path::home_directory(home_dir_path);
2331 FileSpec profilePath(home_dir_path.c_str(), false);
2332 profilePath.AppendPathComponent(".lldbinit");
2333 std::string init_file_path = profilePath.GetPath();
2335 if (m_skip_app_init_files == false)
2337 FileSpec program_file_spec(HostInfo::GetProgramFileSpec());
2338 const char *program_name = program_file_spec.GetFilename().AsCString();
2342 char program_init_file_name[PATH_MAX];
2343 ::snprintf (program_init_file_name, sizeof(program_init_file_name), "%s-%s", init_file_path.c_str(), program_name);
2344 init_file.SetFile (program_init_file_name, true);
2345 if (!init_file.Exists())
2350 if (!init_file && !m_skip_lldbinit_files)
2351 init_file.SetFile (init_file_path.c_str(), false);
2354 // If the file exists, tell HandleCommand to 'source' it; this will do the actual broadcasting
2355 // of the commands back to any appropriate listener (see CommandObjectSource::Execute for more details).
2357 if (init_file.Exists())
2359 const bool saved_batch = SetBatchCommandMode (true);
2360 CommandInterpreterRunOptions options;
2361 options.SetSilent (true);
2362 options.SetStopOnError (false);
2363 options.SetStopOnContinue (true);
2365 HandleCommandsFromFile (init_file,
2366 nullptr, // Execution context
2369 SetBatchCommandMode (saved_batch);
2373 // nothing to be done if the file doesn't exist
2374 result.SetStatus(eReturnStatusSuccessFinishNoResult);
2379 CommandInterpreter::GetCommandPrefix()
2381 const char * prefix = GetDebugger().GetIOHandlerCommandPrefix();
2382 return prefix == NULL ? "" : prefix;
2386 CommandInterpreter::GetPlatform (bool prefer_target_platform)
2388 PlatformSP platform_sp;
2389 if (prefer_target_platform)
2391 ExecutionContext exe_ctx(GetExecutionContext());
2392 Target *target = exe_ctx.GetTargetPtr();
2394 platform_sp = target->GetPlatform();
2398 platform_sp = m_debugger.GetPlatformList().GetSelectedPlatform();
2403 CommandInterpreter::HandleCommands (const StringList &commands,
2404 ExecutionContext *override_context,
2405 CommandInterpreterRunOptions &options,
2406 CommandReturnObject &result)
2408 size_t num_lines = commands.GetSize();
2410 // If we are going to continue past a "continue" then we need to run the commands synchronously.
2411 // Make sure you reset this value anywhere you return from the function.
2413 bool old_async_execution = m_debugger.GetAsyncExecution();
2415 // If we've been given an execution context, set it at the start, but don't keep resetting it or we will
2416 // cause series of commands that change the context, then do an operation that relies on that context to fail.
2418 if (override_context != nullptr)
2419 UpdateExecutionContext (override_context);
2421 if (!options.GetStopOnContinue())
2423 m_debugger.SetAsyncExecution (false);
2426 for (size_t idx = 0; idx < num_lines; idx++)
2428 const char *cmd = commands.GetStringAtIndex(idx);
2432 if (options.GetEchoCommands())
2434 result.AppendMessageWithFormat ("%s %s\n",
2435 m_debugger.GetPrompt(),
2439 CommandReturnObject tmp_result;
2440 // If override_context is not NULL, pass no_context_switching = true for
2441 // HandleCommand() since we updated our context already.
2443 // We might call into a regex or alias command, in which case the add_to_history will get lost. This
2444 // m_command_source_depth dingus is the way we turn off adding to the history in that case, so set it up here.
2445 if (!options.GetAddToHistory())
2446 m_command_source_depth++;
2447 bool success = HandleCommand(cmd, options.m_add_to_history, tmp_result,
2448 nullptr, /* override_context */
2449 true, /* repeat_on_empty_command */
2450 override_context != nullptr /* no_context_switching */);
2451 if (!options.GetAddToHistory())
2452 m_command_source_depth--;
2454 if (options.GetPrintResults())
2456 if (tmp_result.Succeeded())
2457 result.AppendMessageWithFormat("%s", tmp_result.GetOutputData());
2460 if (!success || !tmp_result.Succeeded())
2462 const char *error_msg = tmp_result.GetErrorData();
2463 if (error_msg == nullptr || error_msg[0] == '\0')
2464 error_msg = "<unknown error>.\n";
2465 if (options.GetStopOnError())
2467 result.AppendErrorWithFormat("Aborting reading of commands after command #%" PRIu64 ": '%s' failed with %s",
2468 (uint64_t)idx, cmd, error_msg);
2469 result.SetStatus (eReturnStatusFailed);
2470 m_debugger.SetAsyncExecution (old_async_execution);
2473 else if (options.GetPrintResults())
2475 result.AppendMessageWithFormat ("Command #%" PRIu64 " '%s' failed with %s",
2482 if (result.GetImmediateOutputStream())
2483 result.GetImmediateOutputStream()->Flush();
2485 if (result.GetImmediateErrorStream())
2486 result.GetImmediateErrorStream()->Flush();
2488 // N.B. Can't depend on DidChangeProcessState, because the state coming into the command execution
2489 // could be running (for instance in Breakpoint Commands.
2490 // So we check the return value to see if it is has running in it.
2491 if ((tmp_result.GetStatus() == eReturnStatusSuccessContinuingNoResult)
2492 || (tmp_result.GetStatus() == eReturnStatusSuccessContinuingResult))
2494 if (options.GetStopOnContinue())
2496 // If we caused the target to proceed, and we're going to stop in that case, set the
2497 // status in our real result before returning. This is an error if the continue was not the
2498 // last command in the set of commands to be run.
2499 if (idx != num_lines - 1)
2500 result.AppendErrorWithFormat("Aborting reading of commands after command #%" PRIu64 ": '%s' continued the target.\n",
2501 (uint64_t)idx + 1, cmd);
2503 result.AppendMessageWithFormat("Command #%" PRIu64 " '%s' continued the target.\n", (uint64_t)idx + 1, cmd);
2505 result.SetStatus(tmp_result.GetStatus());
2506 m_debugger.SetAsyncExecution (old_async_execution);
2512 // Also check for "stop on crash here:
2513 bool should_stop = false;
2514 if (tmp_result.GetDidChangeProcessState() && options.GetStopOnCrash())
2516 TargetSP target_sp (m_debugger.GetTargetList().GetSelectedTarget());
2519 ProcessSP process_sp (target_sp->GetProcessSP());
2522 for (ThreadSP thread_sp : process_sp->GetThreadList().Threads())
2524 StopReason reason = thread_sp->GetStopReason();
2525 if (reason == eStopReasonSignal || reason == eStopReasonException || reason == eStopReasonInstrumentation)
2535 if (idx != num_lines - 1)
2536 result.AppendErrorWithFormat("Aborting reading of commands after command #%" PRIu64 ": '%s' stopped with a signal or exception.\n",
2537 (uint64_t)idx + 1, cmd);
2539 result.AppendMessageWithFormat("Command #%" PRIu64 " '%s' stopped with a signal or exception.\n", (uint64_t)idx + 1, cmd);
2541 result.SetStatus(tmp_result.GetStatus());
2542 m_debugger.SetAsyncExecution (old_async_execution);
2550 result.SetStatus (eReturnStatusSuccessFinishResult);
2551 m_debugger.SetAsyncExecution (old_async_execution);
2556 // Make flags that we can pass into the IOHandler so our delegates can do the right thing
2558 eHandleCommandFlagStopOnContinue = (1u << 0),
2559 eHandleCommandFlagStopOnError = (1u << 1),
2560 eHandleCommandFlagEchoCommand = (1u << 2),
2561 eHandleCommandFlagPrintResult = (1u << 3),
2562 eHandleCommandFlagStopOnCrash = (1u << 4)
2566 CommandInterpreter::HandleCommandsFromFile (FileSpec &cmd_file,
2567 ExecutionContext *context,
2568 CommandInterpreterRunOptions &options,
2569 CommandReturnObject &result)
2571 if (cmd_file.Exists())
2573 StreamFileSP input_file_sp (new StreamFile());
2575 std::string cmd_file_path = cmd_file.GetPath();
2576 Error error = input_file_sp->GetFile().Open(cmd_file_path.c_str(), File::eOpenOptionRead);
2578 if (error.Success())
2580 Debugger &debugger = GetDebugger();
2584 if (options.m_stop_on_continue == eLazyBoolCalculate)
2586 if (m_command_source_flags.empty())
2588 // Stop on continue by default
2589 flags |= eHandleCommandFlagStopOnContinue;
2591 else if (m_command_source_flags.back() & eHandleCommandFlagStopOnContinue)
2593 flags |= eHandleCommandFlagStopOnContinue;
2596 else if (options.m_stop_on_continue == eLazyBoolYes)
2598 flags |= eHandleCommandFlagStopOnContinue;
2601 if (options.m_stop_on_error == eLazyBoolCalculate)
2603 if (m_command_source_flags.empty())
2605 if (GetStopCmdSourceOnError())
2606 flags |= eHandleCommandFlagStopOnError;
2608 else if (m_command_source_flags.back() & eHandleCommandFlagStopOnError)
2610 flags |= eHandleCommandFlagStopOnError;
2613 else if (options.m_stop_on_error == eLazyBoolYes)
2615 flags |= eHandleCommandFlagStopOnError;
2618 if (options.GetStopOnCrash())
2620 if (m_command_source_flags.empty())
2622 // Echo command by default
2623 flags |= eHandleCommandFlagStopOnCrash;
2625 else if (m_command_source_flags.back() & eHandleCommandFlagStopOnCrash)
2627 flags |= eHandleCommandFlagStopOnCrash;
2631 if (options.m_echo_commands == eLazyBoolCalculate)
2633 if (m_command_source_flags.empty())
2635 // Echo command by default
2636 flags |= eHandleCommandFlagEchoCommand;
2638 else if (m_command_source_flags.back() & eHandleCommandFlagEchoCommand)
2640 flags |= eHandleCommandFlagEchoCommand;
2643 else if (options.m_echo_commands == eLazyBoolYes)
2645 flags |= eHandleCommandFlagEchoCommand;
2648 if (options.m_print_results == eLazyBoolCalculate)
2650 if (m_command_source_flags.empty())
2652 // Print output by default
2653 flags |= eHandleCommandFlagPrintResult;
2655 else if (m_command_source_flags.back() & eHandleCommandFlagPrintResult)
2657 flags |= eHandleCommandFlagPrintResult;
2660 else if (options.m_print_results == eLazyBoolYes)
2662 flags |= eHandleCommandFlagPrintResult;
2665 if (flags & eHandleCommandFlagPrintResult)
2667 debugger.GetOutputFile()->Printf("Executing commands in '%s'.\n", cmd_file_path.c_str());
2670 // Used for inheriting the right settings when "command source" might have
2671 // nested "command source" commands
2672 lldb::StreamFileSP empty_stream_sp;
2673 m_command_source_flags.push_back(flags);
2674 IOHandlerSP io_handler_sp (new IOHandlerEditline (debugger,
2675 IOHandler::Type::CommandInterpreter,
2677 empty_stream_sp, // Pass in an empty stream so we inherit the top input reader output stream
2678 empty_stream_sp, // Pass in an empty stream so we inherit the top input reader error stream
2680 nullptr, // Pass in NULL for "editline_name" so no history is saved, or written
2681 debugger.GetPrompt(),
2683 false, // Not multi-line
2684 debugger.GetUseColor(),
2687 const bool old_async_execution = debugger.GetAsyncExecution();
2689 // Set synchronous execution if we are not stopping on continue
2690 if ((flags & eHandleCommandFlagStopOnContinue) == 0)
2691 debugger.SetAsyncExecution (false);
2693 m_command_source_depth++;
2695 debugger.RunIOHandler(io_handler_sp);
2696 if (!m_command_source_flags.empty())
2697 m_command_source_flags.pop_back();
2698 m_command_source_depth--;
2699 result.SetStatus (eReturnStatusSuccessFinishNoResult);
2700 debugger.SetAsyncExecution (old_async_execution);
2704 result.AppendErrorWithFormat ("error: an error occurred read file '%s': %s\n", cmd_file_path.c_str(), error.AsCString());
2705 result.SetStatus (eReturnStatusFailed);
2712 result.AppendErrorWithFormat ("Error reading commands from file %s - file not found.\n",
2713 cmd_file.GetFilename().AsCString("<Unknown>"));
2714 result.SetStatus (eReturnStatusFailed);
2720 CommandInterpreter::GetScriptInterpreter(bool can_create)
2722 if (m_script_interpreter_sp)
2723 return m_script_interpreter_sp.get();
2728 lldb::ScriptLanguage script_lang = GetDebugger().GetScriptLanguage();
2729 m_script_interpreter_sp = PluginManager::GetScriptInterpreterForLanguage(script_lang, *this);
2730 return m_script_interpreter_sp.get();
2734 CommandInterpreter::GetSynchronous ()
2736 return m_synchronous_execution;
2740 CommandInterpreter::SetSynchronous (bool value)
2742 m_synchronous_execution = value;
2746 CommandInterpreter::OutputFormattedHelpText (Stream &strm,
2748 const char *help_text)
2750 const uint32_t max_columns = m_debugger.GetTerminalWidth();
2754 size_t prefix_width = strlen(prefix);
2755 size_t line_width_max = max_columns - prefix_width;
2756 const char *help_text_end = help_text + strlen(help_text);
2757 const char *line_start = help_text;
2758 if (line_width_max < 16)
2759 line_width_max = help_text_end - help_text + prefix_width;
2761 strm.IndentMore (prefix_width);
2762 while (line_start < help_text_end)
2764 // Break each line at the first newline or last space/tab before
2765 // the maximum number of characters that fit on a line. Lines with no
2766 // natural break are left unbroken to wrap.
2767 const char *line_end = help_text_end;
2768 const char *line_scan = line_start;
2769 const char *line_scan_end = help_text_end;
2770 while (line_scan < line_scan_end)
2772 char next = *line_scan;
2773 if (next == '\t' || next == ' ')
2775 line_end = line_scan;
2776 line_scan_end = line_start + line_width_max;
2778 else if (next == '\n' || next == '\0')
2780 line_end = line_scan;
2786 // Prefix the first line, indent subsequent lines to line up
2787 if (line_start == help_text)
2788 strm.Write (prefix, prefix_width);
2791 strm.Write (line_start, line_end - line_start);
2794 // When a line breaks at whitespace consume it before continuing
2795 line_start = line_end;
2796 char next = *line_start;
2799 else while (next == ' ' || next == '\t')
2800 next = *(++line_start);
2802 strm.IndentLess (prefix_width);
2806 CommandInterpreter::OutputFormattedHelpText (Stream &strm,
2807 const char *word_text,
2808 const char *separator,
2809 const char *help_text,
2810 size_t max_word_len)
2812 StreamString prefix_stream;
2813 prefix_stream.Printf (" %-*s %s ", (int)max_word_len, word_text, separator);
2814 OutputFormattedHelpText (strm, prefix_stream.GetData(), help_text);
2818 CommandInterpreter::OutputHelpText (Stream &strm,
2819 const char *word_text,
2820 const char *separator,
2821 const char *help_text,
2822 uint32_t max_word_len)
2824 int indent_size = max_word_len + strlen (separator) + 2;
2826 strm.IndentMore (indent_size);
2828 StreamString text_strm;
2829 text_strm.Printf ("%-*s %s %s", max_word_len, word_text, separator, help_text);
2831 const uint32_t max_columns = m_debugger.GetTerminalWidth();
2833 size_t len = text_strm.GetSize();
2834 const char *text = text_strm.GetData();
2836 uint32_t chars_left = max_columns;
2838 for (uint32_t i = 0; i < len; i++)
2840 if ((text[i] == ' ' && ::strchr((text+i+1), ' ') && chars_left < ::strchr((text+i+1), ' ')-(text+i)) || text[i] == '\n')
2842 chars_left = max_columns - indent_size;
2848 strm.PutChar(text[i]);
2855 strm.IndentLess(indent_size);
2859 CommandInterpreter::FindCommandsForApropos (const char *search_word, StringList &commands_found,
2860 StringList &commands_help, bool search_builtin_commands, bool search_user_commands)
2862 CommandObject::CommandMap::const_iterator pos;
2864 if (search_builtin_commands)
2866 for (pos = m_command_dict.begin(); pos != m_command_dict.end(); ++pos)
2868 const char *command_name = pos->first.c_str();
2869 CommandObject *cmd_obj = pos->second.get();
2871 if (cmd_obj->HelpTextContainsWord (search_word))
2873 commands_found.AppendString (command_name);
2874 commands_help.AppendString (cmd_obj->GetHelp());
2877 if (cmd_obj->IsMultiwordObject())
2878 cmd_obj->AproposAllSubCommands (command_name,
2886 if (search_user_commands)
2888 for (pos = m_user_dict.begin(); pos != m_user_dict.end(); ++pos)
2890 const char *command_name = pos->first.c_str();
2891 CommandObject *cmd_obj = pos->second.get();
2893 if (cmd_obj->HelpTextContainsWord (search_word))
2895 commands_found.AppendString (command_name);
2896 commands_help.AppendString (cmd_obj->GetHelp());
2899 if (cmd_obj->IsMultiwordObject())
2900 cmd_obj->AproposAllSubCommands (command_name,
2910 CommandInterpreter::UpdateExecutionContext (ExecutionContext *override_context)
2912 if (override_context != nullptr)
2914 m_exe_ctx_ref = *override_context;
2918 const bool adopt_selected = true;
2919 m_exe_ctx_ref.SetTargetPtr (m_debugger.GetSelectedTarget().get(), adopt_selected);
2925 CommandInterpreter::GetProcessOutput ()
2927 // The process has stuff waiting for stderr; get it and write it out to the appropriate place.
2928 char stdio_buffer[1024];
2930 size_t total_bytes = 0;
2932 TargetSP target_sp (m_debugger.GetTargetList().GetSelectedTarget());
2935 ProcessSP process_sp (target_sp->GetProcessSP());
2938 while ((len = process_sp->GetSTDOUT (stdio_buffer, sizeof (stdio_buffer), error)) > 0)
2940 size_t bytes_written = len;
2941 m_debugger.GetOutputFile()->Write (stdio_buffer, bytes_written);
2944 while ((len = process_sp->GetSTDERR (stdio_buffer, sizeof (stdio_buffer), error)) > 0)
2946 size_t bytes_written = len;
2947 m_debugger.GetErrorFile()->Write (stdio_buffer, bytes_written);
2956 CommandInterpreter::IOHandlerInputComplete (IOHandler &io_handler, std::string &line)
2958 const bool is_interactive = io_handler.GetIsInteractive();
2959 if (is_interactive == false)
2961 // When we are not interactive, don't execute blank lines. This will happen
2962 // sourcing a commands file. We don't want blank lines to repeat the previous
2963 // command and cause any errors to occur (like redefining an alias, get an error
2964 // and stop parsing the commands file).
2968 // When using a non-interactive file handle (like when sourcing commands from a file)
2969 // we need to echo the command out so we don't just see the command output and no
2971 if (io_handler.GetFlags().Test(eHandleCommandFlagEchoCommand))
2972 io_handler.GetOutputStreamFile()->Printf("%s%s\n", io_handler.GetPrompt(), line.c_str());
2975 lldb_private::CommandReturnObject result;
2976 HandleCommand(line.c_str(), eLazyBoolCalculate, result);
2978 // Now emit the command output text from the command we just executed
2979 if (io_handler.GetFlags().Test(eHandleCommandFlagPrintResult))
2981 // Display any STDOUT/STDERR _prior_ to emitting the command result text
2982 GetProcessOutput ();
2984 if (!result.GetImmediateOutputStream())
2986 const char *output = result.GetOutputData();
2987 if (output && output[0])
2988 io_handler.GetOutputStreamFile()->PutCString(output);
2991 // Now emit the command error text from the command we just executed
2992 if (!result.GetImmediateErrorStream())
2994 const char *error = result.GetErrorData();
2995 if (error && error[0])
2996 io_handler.GetErrorStreamFile()->PutCString(error);
3000 switch (result.GetStatus())
3002 case eReturnStatusInvalid:
3003 case eReturnStatusSuccessFinishNoResult:
3004 case eReturnStatusSuccessFinishResult:
3005 case eReturnStatusStarted:
3008 case eReturnStatusSuccessContinuingNoResult:
3009 case eReturnStatusSuccessContinuingResult:
3010 if (io_handler.GetFlags().Test(eHandleCommandFlagStopOnContinue))
3011 io_handler.SetIsDone(true);
3014 case eReturnStatusFailed:
3016 if (io_handler.GetFlags().Test(eHandleCommandFlagStopOnError))
3017 io_handler.SetIsDone(true);
3020 case eReturnStatusQuit:
3021 m_quit_requested = true;
3022 io_handler.SetIsDone(true);
3026 // Finally, if we're going to stop on crash, check that here:
3027 if (!m_quit_requested
3028 && result.GetDidChangeProcessState()
3029 && io_handler.GetFlags().Test(eHandleCommandFlagStopOnCrash))
3031 bool should_stop = false;
3032 TargetSP target_sp (m_debugger.GetTargetList().GetSelectedTarget());
3035 ProcessSP process_sp (target_sp->GetProcessSP());
3038 for (ThreadSP thread_sp : process_sp->GetThreadList().Threads())
3040 StopReason reason = thread_sp->GetStopReason();
3041 if ((reason == eStopReasonSignal
3042 || reason == eStopReasonException
3043 || reason == eStopReasonInstrumentation)
3044 && !result.GetAbnormalStopWasExpected())
3054 io_handler.SetIsDone(true);
3055 m_stopped_for_crash = true;
3061 CommandInterpreter::IOHandlerInterrupt (IOHandler &io_handler)
3063 ExecutionContext exe_ctx (GetExecutionContext());
3064 Process *process = exe_ctx.GetProcessPtr();
3068 StateType state = process->GetState();
3069 if (StateIsRunningState(state))
3072 return true; // Don't do any updating when we are running
3076 ScriptInterpreter *script_interpreter = GetScriptInterpreter (false);
3077 if (script_interpreter)
3079 if (script_interpreter->Interrupt())
3086 CommandInterpreter::GetLLDBCommandsFromIOHandler (const char *prompt,
3087 IOHandlerDelegate &delegate,
3088 bool asynchronously,
3091 Debugger &debugger = GetDebugger();
3092 IOHandlerSP io_handler_sp (new IOHandlerEditline (debugger,
3093 IOHandler::Type::CommandList,
3094 "lldb", // Name of input reader for history
3096 NULL, // Continuation prompt
3097 true, // Get multiple lines
3098 debugger.GetUseColor(),
3099 0, // Don't show line numbers
3100 delegate)); // IOHandlerDelegate
3104 io_handler_sp->SetUserData (baton);
3106 debugger.PushIOHandler(io_handler_sp);
3108 debugger.RunIOHandler(io_handler_sp);
3115 CommandInterpreter::GetPythonCommandsFromIOHandler (const char *prompt,
3116 IOHandlerDelegate &delegate,
3117 bool asynchronously,
3120 Debugger &debugger = GetDebugger();
3121 IOHandlerSP io_handler_sp (new IOHandlerEditline (debugger,
3122 IOHandler::Type::PythonCode,
3123 "lldb-python", // Name of input reader for history
3125 NULL, // Continuation prompt
3126 true, // Get multiple lines
3127 debugger.GetUseColor(),
3128 0, // Don't show line numbers
3129 delegate)); // IOHandlerDelegate
3133 io_handler_sp->SetUserData (baton);
3135 debugger.PushIOHandler(io_handler_sp);
3137 debugger.RunIOHandler(io_handler_sp);
3143 CommandInterpreter::IsActive ()
3145 return m_debugger.IsTopIOHandler (m_command_io_handler_sp);
3149 CommandInterpreter::GetIOHandler(bool force_create, CommandInterpreterRunOptions *options)
3151 // Always re-create the IOHandlerEditline in case the input
3152 // changed. The old instance might have had a non-interactive
3153 // input and now it does or vice versa.
3154 if (force_create || !m_command_io_handler_sp)
3156 // Always re-create the IOHandlerEditline in case the input
3157 // changed. The old instance might have had a non-interactive
3158 // input and now it does or vice versa.
3163 if (options->m_stop_on_continue == eLazyBoolYes)
3164 flags |= eHandleCommandFlagStopOnContinue;
3165 if (options->m_stop_on_error == eLazyBoolYes)
3166 flags |= eHandleCommandFlagStopOnError;
3167 if (options->m_stop_on_crash == eLazyBoolYes)
3168 flags |= eHandleCommandFlagStopOnCrash;
3169 if (options->m_echo_commands != eLazyBoolNo)
3170 flags |= eHandleCommandFlagEchoCommand;
3171 if (options->m_print_results != eLazyBoolNo)
3172 flags |= eHandleCommandFlagPrintResult;
3176 flags = eHandleCommandFlagEchoCommand | eHandleCommandFlagPrintResult;
3179 m_command_io_handler_sp.reset(new IOHandlerEditline (m_debugger,
3180 IOHandler::Type::CommandInterpreter,
3181 m_debugger.GetInputFile(),
3182 m_debugger.GetOutputFile(),
3183 m_debugger.GetErrorFile(),
3186 m_debugger.GetPrompt(),
3187 NULL, // Continuation prompt
3188 false, // Don't enable multiple line input, just single line commands
3189 m_debugger.GetUseColor(),
3190 0, // Don't show line numbers
3193 return m_command_io_handler_sp;
3197 CommandInterpreter::RunCommandInterpreter(bool auto_handle_events,
3199 CommandInterpreterRunOptions &options)
3201 // Always re-create the command interpreter when we run it in case
3202 // any file handles have changed.
3203 bool force_create = true;
3204 m_debugger.PushIOHandler(GetIOHandler(force_create, &options));
3205 m_stopped_for_crash = false;
3207 if (auto_handle_events)
3208 m_debugger.StartEventHandlerThread();
3212 m_debugger.StartIOHandlerThread();
3216 m_debugger.ExecuteIOHandlers();
3218 if (auto_handle_events)
3219 m_debugger.StopEventHandlerThread();
3225 CommandInterpreter::ResolveCommandImpl(std::string &command_line, CommandReturnObject &result)
3227 std::string scratch_command(command_line); // working copy so we don't modify command_line unless we succeed
3228 CommandObject *cmd_obj = nullptr;
3229 StreamString revised_command_line;
3230 bool wants_raw_input = false;
3231 size_t actual_cmd_name_len = 0;
3232 std::string next_word;
3237 char quote_char = '\0';
3239 ExtractCommand(scratch_command, next_word, suffix, quote_char);
3240 if (cmd_obj == nullptr)
3242 std::string full_name;
3243 if (GetAliasFullName(next_word.c_str(), full_name))
3245 std::string alias_result;
3246 cmd_obj = BuildAliasResult(full_name.c_str(), scratch_command, alias_result, result);
3247 revised_command_line.Printf("%s", alias_result.c_str());
3250 wants_raw_input = cmd_obj->WantsRawCommandString();
3251 actual_cmd_name_len = strlen(cmd_obj->GetCommandName());
3256 cmd_obj = GetCommandObject(next_word.c_str(), &matches);
3259 actual_cmd_name_len += strlen(cmd_obj->GetCommandName());
3260 revised_command_line.Printf("%s", cmd_obj->GetCommandName());
3261 wants_raw_input = cmd_obj->WantsRawCommandString();
3265 revised_command_line.Printf ("%s", next_word.c_str());
3271 if (cmd_obj->IsMultiwordObject ())
3273 CommandObject *sub_cmd_obj = cmd_obj->GetSubcommandObject(next_word.c_str());
3276 // The subcommand's name includes the parent command's name,
3277 // so restart rather than append to the revised_command_line.
3278 actual_cmd_name_len = strlen(sub_cmd_obj->GetCommandName()) + 1;
3279 revised_command_line.Clear();
3280 revised_command_line.Printf("%s", sub_cmd_obj->GetCommandName());
3281 cmd_obj = sub_cmd_obj;
3282 wants_raw_input = cmd_obj->WantsRawCommandString();
3287 revised_command_line.Printf(" %c%s%s%c", quote_char, next_word.c_str(), suffix.c_str(), quote_char);
3289 revised_command_line.Printf(" %s%s", next_word.c_str(), suffix.c_str());
3296 revised_command_line.Printf(" %c%s%s%c", quote_char, next_word.c_str(), suffix.c_str(), quote_char);
3298 revised_command_line.Printf(" %s%s", next_word.c_str(), suffix.c_str());
3303 if (cmd_obj == nullptr)
3305 const size_t num_matches = matches.GetSize();
3306 if (matches.GetSize() > 1) {
3307 StreamString error_msg;
3308 error_msg.Printf("Ambiguous command '%s'. Possible matches:\n", next_word.c_str());
3310 for (uint32_t i = 0; i < num_matches; ++i) {
3311 error_msg.Printf("\t%s\n", matches.GetStringAtIndex(i));
3313 result.AppendRawError(error_msg.GetString().c_str());
3315 // We didn't have only one match, otherwise we wouldn't get here.
3316 assert(num_matches == 0);
3317 result.AppendErrorWithFormat("'%s' is not a valid command.\n", next_word.c_str());
3319 result.SetStatus(eReturnStatusFailed);
3323 if (cmd_obj->IsMultiwordObject())
3325 if (!suffix.empty())
3327 result.AppendErrorWithFormat("command '%s' did not recognize '%s%s%s' as valid (subcommand might be invalid).\n",
3328 cmd_obj->GetCommandName(),
3329 next_word.empty() ? "" : next_word.c_str(),
3330 next_word.empty() ? " -- " : " ",
3332 result.SetStatus(eReturnStatusFailed);
3338 // If we found a normal command, we are done
3340 if (!suffix.empty())
3345 // GDB format suffixes
3347 Options *command_options = cmd_obj->GetOptions();
3348 if (command_options && command_options->SupportsLongOption("gdb-format"))
3350 std::string gdb_format_option("--gdb-format=");
3351 gdb_format_option += (suffix.c_str() + 1);
3353 bool inserted = false;
3354 std::string &cmd = revised_command_line.GetString();
3355 size_t arg_terminator_idx = FindArgumentTerminator(cmd);
3356 if (arg_terminator_idx != std::string::npos)
3358 // Insert the gdb format option before the "--" that terminates options
3359 gdb_format_option.append(1,' ');
3360 cmd.insert(arg_terminator_idx, gdb_format_option);
3365 revised_command_line.Printf(" %s", gdb_format_option.c_str());
3367 if (wants_raw_input && FindArgumentTerminator(cmd) == std::string::npos)
3368 revised_command_line.PutCString(" --");
3372 result.AppendErrorWithFormat("the '%s' command doesn't support the --gdb-format option\n",
3373 cmd_obj->GetCommandName());
3374 result.SetStatus(eReturnStatusFailed);
3381 result.AppendErrorWithFormat("unknown command shorthand suffix: '%s'\n",
3383 result.SetStatus(eReturnStatusFailed);
3388 if (scratch_command.empty())
3392 if (!scratch_command.empty())
3393 revised_command_line.Printf(" %s", scratch_command.c_str());
3395 if (cmd_obj != NULL)
3396 command_line = revised_command_line.GetData();