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.GetBroadcasterManager(), 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 // An alias arguments vector to reuse - reset it before use...
189 OptionArgVectorSP alias_arguments_vector_sp (new OptionArgVector);
191 // Set up some initial aliases.
192 CommandObjectSP cmd_obj_sp = GetCommandSPExact ("quit", false);
195 AddAlias ("q", cmd_obj_sp);
196 AddAlias ("exit", cmd_obj_sp);
199 cmd_obj_sp = GetCommandSPExact ("_regexp-attach",false);
201 AddAlias ("attach", cmd_obj_sp)->SetSyntax(cmd_obj_sp->GetSyntax());
203 cmd_obj_sp = GetCommandSPExact ("process detach",false);
206 AddAlias ("detach", cmd_obj_sp);
209 cmd_obj_sp = GetCommandSPExact ("process continue", false);
212 AddAlias ("c", cmd_obj_sp);
213 AddAlias ("continue", cmd_obj_sp);
216 cmd_obj_sp = GetCommandSPExact ("_regexp-break",false);
218 AddAlias ("b", cmd_obj_sp)->SetSyntax(cmd_obj_sp->GetSyntax());
220 cmd_obj_sp = GetCommandSPExact ("_regexp-tbreak",false);
222 AddAlias ("tbreak", cmd_obj_sp)->SetSyntax(cmd_obj_sp->GetSyntax());
224 cmd_obj_sp = GetCommandSPExact ("thread step-inst", false);
227 AddAlias ("stepi", cmd_obj_sp);
228 AddAlias ("si", cmd_obj_sp);
231 cmd_obj_sp = GetCommandSPExact ("thread step-inst-over", false);
234 AddAlias ("nexti", cmd_obj_sp);
235 AddAlias ("ni", cmd_obj_sp);
238 cmd_obj_sp = GetCommandSPExact ("thread step-in", false);
241 AddAlias ("s", cmd_obj_sp);
242 AddAlias ("step", cmd_obj_sp);
243 CommandAlias *sif_alias = AddAlias ("sif", cmd_obj_sp, "--end-linenumber block --step-in-target %1");
246 sif_alias->SetHelp("Step through the current block, stopping if you step "
247 "directly into a function whose name matches the TargetFunctionName.");
248 sif_alias->SetSyntax("sif <TargetFunctionName>");
252 cmd_obj_sp = GetCommandSPExact ("thread step-over", false);
255 AddAlias ("n", cmd_obj_sp);
256 AddAlias ("next", cmd_obj_sp);
259 cmd_obj_sp = GetCommandSPExact ("thread step-out", false);
262 AddAlias ("finish", cmd_obj_sp);
265 cmd_obj_sp = GetCommandSPExact ("frame select", false);
268 AddAlias ("f", cmd_obj_sp);
271 cmd_obj_sp = GetCommandSPExact ("thread select", false);
274 AddAlias ("t", cmd_obj_sp);
277 cmd_obj_sp = GetCommandSPExact ("_regexp-jump",false);
280 AddAlias ("j", cmd_obj_sp)->SetSyntax(cmd_obj_sp->GetSyntax());
281 AddAlias ("jump", cmd_obj_sp)->SetSyntax(cmd_obj_sp->GetSyntax());
284 cmd_obj_sp = GetCommandSPExact ("_regexp-list", false);
287 AddAlias ("l", cmd_obj_sp)->SetSyntax(cmd_obj_sp->GetSyntax());
288 AddAlias ("list", cmd_obj_sp)->SetSyntax(cmd_obj_sp->GetSyntax());
291 cmd_obj_sp = GetCommandSPExact ("_regexp-env", false);
293 AddAlias ("env", cmd_obj_sp)->SetSyntax(cmd_obj_sp->GetSyntax());
295 cmd_obj_sp = GetCommandSPExact ("memory read", false);
297 AddAlias ("x", cmd_obj_sp);
299 cmd_obj_sp = GetCommandSPExact ("_regexp-up", false);
301 AddAlias ("up", cmd_obj_sp)->SetSyntax(cmd_obj_sp->GetSyntax());
303 cmd_obj_sp = GetCommandSPExact ("_regexp-down", false);
305 AddAlias ("down", cmd_obj_sp)->SetSyntax(cmd_obj_sp->GetSyntax());
307 cmd_obj_sp = GetCommandSPExact ("_regexp-display", false);
309 AddAlias ("display", cmd_obj_sp)->SetSyntax(cmd_obj_sp->GetSyntax());
311 cmd_obj_sp = GetCommandSPExact ("disassemble", false);
313 AddAlias ("dis", cmd_obj_sp);
315 cmd_obj_sp = GetCommandSPExact ("disassemble", false);
317 AddAlias ("di", cmd_obj_sp);
321 cmd_obj_sp = GetCommandSPExact ("_regexp-undisplay", false);
323 AddAlias ("undisplay", cmd_obj_sp)->SetSyntax(cmd_obj_sp->GetSyntax());
325 cmd_obj_sp = GetCommandSPExact ("_regexp-bt", false);
327 AddAlias ("bt", cmd_obj_sp)->SetSyntax(cmd_obj_sp->GetSyntax());
329 cmd_obj_sp = GetCommandSPExact ("target create", false);
331 AddAlias ("file", cmd_obj_sp);
333 cmd_obj_sp = GetCommandSPExact ("target modules", false);
335 AddAlias ("image", cmd_obj_sp);
338 alias_arguments_vector_sp.reset(new OptionArgVector);
340 cmd_obj_sp = GetCommandSPExact ("expression", false);
343 AddAlias ("p", cmd_obj_sp, "--")->SetHelpLong("");
344 AddAlias ("print", cmd_obj_sp, "--")->SetHelpLong("");
345 AddAlias ("call", cmd_obj_sp, "--")->SetHelpLong("");
346 if (auto po = AddAlias ("po", cmd_obj_sp, "-O --"))
348 po->SetHelp("Evaluate an expression on the current thread. Displays any returned value with formatting "
349 "controlled by the type's author.");
352 AddAlias("parray", cmd_obj_sp, "--element-count %1 --")->SetHelpLong("");
353 AddAlias("poarray", cmd_obj_sp, "--object-description --element-count %1 --")->SetHelpLong("");
356 cmd_obj_sp = GetCommandSPExact ("process kill", false);
359 AddAlias ("kill", cmd_obj_sp);
362 cmd_obj_sp = GetCommandSPExact ("process launch", false);
365 alias_arguments_vector_sp.reset (new OptionArgVector);
366 #if defined (__arm__) || defined (__arm64__) || defined (__aarch64__)
367 AddAlias ("r", cmd_obj_sp, "--");
368 AddAlias ("run", cmd_obj_sp, "--");
370 #if defined(__APPLE__)
371 std::string shell_option;
372 shell_option.append("--shell-expand-args");
373 shell_option.append(" true");
374 shell_option.append(" --");
375 AddAlias ("r", cmd_obj_sp, "--shell-expand-args true --");
376 AddAlias ("run", cmd_obj_sp, "--shell-expand-args true --");
378 StreamString defaultshell;
379 defaultshell.Printf("--shell=%s --", HostInfo::GetDefaultShell().GetPath().c_str());
380 AddAlias ("r", cmd_obj_sp, defaultshell.GetData());
381 AddAlias ("run", cmd_obj_sp, defaultshell.GetData());
386 cmd_obj_sp = GetCommandSPExact ("target symbols add", false);
389 AddAlias ("add-dsym", cmd_obj_sp);
392 cmd_obj_sp = GetCommandSPExact ("breakpoint set", false);
395 AddAlias ("rbreak", cmd_obj_sp, "--func-regex %1");
400 CommandInterpreter::Clear()
402 m_command_io_handler_sp.reset();
404 if (m_script_interpreter_sp)
405 m_script_interpreter_sp->Clear();
409 CommandInterpreter::ProcessEmbeddedScriptCommands (const char *arg)
411 // This function has not yet been implemented.
413 // Look for any embedded script command
415 // get interpreter object from the command dictionary,
416 // call execute_one_command on it,
417 // get the results as a string,
418 // substitute that string for current stuff.
425 CommandInterpreter::LoadCommandDictionary ()
427 Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
429 lldb::ScriptLanguage script_language = m_debugger.GetScriptLanguage();
431 m_command_dict["apropos"] = CommandObjectSP (new CommandObjectApropos (*this));
432 m_command_dict["breakpoint"]= CommandObjectSP (new CommandObjectMultiwordBreakpoint (*this));
433 m_command_dict["bugreport"] = CommandObjectSP (new CommandObjectMultiwordBugreport (*this));
434 m_command_dict["command"] = CommandObjectSP (new CommandObjectMultiwordCommands (*this));
435 m_command_dict["disassemble"] = CommandObjectSP (new CommandObjectDisassemble (*this));
436 m_command_dict["expression"]= CommandObjectSP (new CommandObjectExpression (*this));
437 m_command_dict["frame"] = CommandObjectSP (new CommandObjectMultiwordFrame (*this));
438 m_command_dict["gui"] = CommandObjectSP (new CommandObjectGUI (*this));
439 m_command_dict["help"] = CommandObjectSP (new CommandObjectHelp (*this));
440 m_command_dict["log"] = CommandObjectSP (new CommandObjectLog (*this));
441 m_command_dict["memory"] = CommandObjectSP (new CommandObjectMemory (*this));
442 m_command_dict["platform"] = CommandObjectSP (new CommandObjectPlatform (*this));
443 m_command_dict["plugin"] = CommandObjectSP (new CommandObjectPlugin (*this));
444 m_command_dict["process"] = CommandObjectSP (new CommandObjectMultiwordProcess (*this));
445 m_command_dict["quit"] = CommandObjectSP (new CommandObjectQuit (*this));
446 m_command_dict["register"] = CommandObjectSP (new CommandObjectRegister (*this));
447 m_command_dict["script"] = CommandObjectSP (new CommandObjectScript (*this, script_language));
448 m_command_dict["settings"] = CommandObjectSP (new CommandObjectMultiwordSettings (*this));
449 m_command_dict["source"] = CommandObjectSP (new CommandObjectMultiwordSource (*this));
450 m_command_dict["target"] = CommandObjectSP (new CommandObjectMultiwordTarget (*this));
451 m_command_dict["thread"] = CommandObjectSP (new CommandObjectMultiwordThread (*this));
452 m_command_dict["type"] = CommandObjectSP (new CommandObjectType (*this));
453 m_command_dict["version"] = CommandObjectSP (new CommandObjectVersion (*this));
454 m_command_dict["watchpoint"]= CommandObjectSP (new CommandObjectMultiwordWatchpoint (*this));
455 m_command_dict["language"] = CommandObjectSP (new CommandObjectLanguage(*this));
457 const char *break_regexes[][2] = {{"^(.*[^[:space:]])[[:space:]]*:[[:space:]]*([[:digit:]]+)[[:space:]]*$", "breakpoint set --file '%1' --line %2"},
458 {"^/([^/]+)/$", "breakpoint set --source-pattern-regexp '%1'"},
459 {"^([[:digit:]]+)[[:space:]]*$", "breakpoint set --line %1"},
460 {"^\\*?(0x[[:xdigit:]]+)[[:space:]]*$", "breakpoint set --address %1"},
461 {"^[\"']?([-+]?\\[.*\\])[\"']?[[:space:]]*$", "breakpoint set --name '%1'"},
462 {"^(-.*)$", "breakpoint set %1"},
463 {"^(.*[^[:space:]])`(.*[^[:space:]])[[:space:]]*$", "breakpoint set --name '%2' --shlib '%1'"},
464 {"^\\&(.*[^[:space:]])[[:space:]]*$", "breakpoint set --name '%1' --skip-prologue=0"},
465 {"^[\"']?(.*[^[:space:]\"'])[\"']?[[:space:]]*$", "breakpoint set --name '%1'"}};
467 size_t num_regexes = llvm::array_lengthof(break_regexes);
469 std::unique_ptr<CommandObjectRegexCommand> break_regex_cmd_ap(new CommandObjectRegexCommand(
470 *this, "_regexp-break", "Set a breakpoint using one of several shorthand formats.\n",
472 "_regexp-break <filename>:<linenum>\n"
473 " main.c:12 // Break at line 12 of main.c\n\n"
474 "_regexp-break <linenum>\n"
475 " 12 // Break at line 12 of current file\n\n"
476 "_regexp-break 0x<address>\n"
477 " 0x1234000 // Break at address 0x1234000\n\n"
478 "_regexp-break <name>\n"
479 " main // Break in 'main' after the prologue\n\n"
480 "_regexp-break &<name>\n"
481 " &main // Break at first instruction in 'main'\n\n"
482 "_regexp-break <module>`<name>\n"
483 " libc.so`malloc // Break in 'malloc' from 'libc.so'\n\n"
484 "_regexp-break /<source-regex>/\n"
485 " /break here/ // Break on source lines in current file\n"
486 " // containing text 'break here'.\n",
487 2, CommandCompletions::eSymbolCompletion | CommandCompletions::eSourceFileCompletion, false));
489 if (break_regex_cmd_ap.get())
492 for (size_t i = 0; i < num_regexes; i++)
494 success = break_regex_cmd_ap->AddRegexCommand (break_regexes[i][0], break_regexes[i][1]);
498 success = break_regex_cmd_ap->AddRegexCommand("^$", "breakpoint list --full");
502 CommandObjectSP break_regex_cmd_sp(break_regex_cmd_ap.release());
503 m_command_dict[break_regex_cmd_sp->GetCommandName ()] = break_regex_cmd_sp;
507 std::unique_ptr<CommandObjectRegexCommand> tbreak_regex_cmd_ap(new CommandObjectRegexCommand(
508 *this, "_regexp-tbreak", "Set a one-shot breakpoint using one of several shorthand formats.\n",
510 "_regexp-break <filename>:<linenum>\n"
511 " main.c:12 // Break at line 12 of main.c\n\n"
512 "_regexp-break <linenum>\n"
513 " 12 // Break at line 12 of current file\n\n"
514 "_regexp-break 0x<address>\n"
515 " 0x1234000 // Break at address 0x1234000\n\n"
516 "_regexp-break <name>\n"
517 " main // Break in 'main' after the prologue\n\n"
518 "_regexp-break &<name>\n"
519 " &main // Break at first instruction in 'main'\n\n"
520 "_regexp-break <module>`<name>\n"
521 " libc.so`malloc // Break in 'malloc' from 'libc.so'\n\n"
522 "_regexp-break /<source-regex>/\n"
523 " /break here/ // Break on source lines in current file\n"
524 " // containing text 'break here'.\n",
525 2, CommandCompletions::eSymbolCompletion | CommandCompletions::eSourceFileCompletion, false));
527 if (tbreak_regex_cmd_ap.get())
530 for (size_t i = 0; i < num_regexes; i++)
532 // If you add a resultant command string longer than 1024 characters be sure to increase the size of this buffer.
534 int num_printed = snprintf(buffer, 1024, "%s %s", break_regexes[i][1], "-o");
535 assert (num_printed < 1024);
536 UNUSED_IF_ASSERT_DISABLED(num_printed);
537 success = tbreak_regex_cmd_ap->AddRegexCommand (break_regexes[i][0], buffer);
541 success = tbreak_regex_cmd_ap->AddRegexCommand("^$", "breakpoint list --full");
545 CommandObjectSP tbreak_regex_cmd_sp(tbreak_regex_cmd_ap.release());
546 m_command_dict[tbreak_regex_cmd_sp->GetCommandName ()] = tbreak_regex_cmd_sp;
550 std::unique_ptr<CommandObjectRegexCommand> attach_regex_cmd_ap(
551 new CommandObjectRegexCommand(*this, "_regexp-attach", "Attach to process by ID or name.",
552 "_regexp-attach <pid> | <process-name>", 2, 0, false));
553 if (attach_regex_cmd_ap.get())
555 if (attach_regex_cmd_ap->AddRegexCommand("^([0-9]+)[[:space:]]*$", "process attach --pid %1") &&
556 attach_regex_cmd_ap->AddRegexCommand("^(-.*|.* -.*)$", "process attach %1") && // Any options that are specified get passed to 'process attach'
557 attach_regex_cmd_ap->AddRegexCommand("^(.+)$", "process attach --name '%1'") &&
558 attach_regex_cmd_ap->AddRegexCommand("^$", "process attach"))
560 CommandObjectSP attach_regex_cmd_sp(attach_regex_cmd_ap.release());
561 m_command_dict[attach_regex_cmd_sp->GetCommandName ()] = attach_regex_cmd_sp;
565 std::unique_ptr<CommandObjectRegexCommand> down_regex_cmd_ap(new CommandObjectRegexCommand(
566 *this, "_regexp-down", "Select a newer stack frame. Defaults to moving one frame, a numeric argument can "
567 "specify an arbitrary number.",
568 "_regexp-down [<count>]", 2, 0, false));
569 if (down_regex_cmd_ap.get())
571 if (down_regex_cmd_ap->AddRegexCommand("^$", "frame select -r -1") &&
572 down_regex_cmd_ap->AddRegexCommand("^([0-9]+)$", "frame select -r -%1"))
574 CommandObjectSP down_regex_cmd_sp(down_regex_cmd_ap.release());
575 m_command_dict[down_regex_cmd_sp->GetCommandName ()] = down_regex_cmd_sp;
579 std::unique_ptr<CommandObjectRegexCommand> up_regex_cmd_ap(
580 new CommandObjectRegexCommand(*this, "_regexp-up", "Select an older stack frame. Defaults to moving one "
581 "frame, a numeric argument can specify an arbitrary number.",
582 "_regexp-up [<count>]", 2, 0, false));
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> display_regex_cmd_ap(new CommandObjectRegexCommand(
594 *this, "_regexp-display", "Evaluate an expression at every stop (see 'help target stop-hook'.)",
595 "_regexp-display expression", 2, 0, false));
596 if (display_regex_cmd_ap.get())
598 if (display_regex_cmd_ap->AddRegexCommand("^(.+)$", "target stop-hook add -o \"expr -- %1\""))
600 CommandObjectSP display_regex_cmd_sp(display_regex_cmd_ap.release());
601 m_command_dict[display_regex_cmd_sp->GetCommandName ()] = display_regex_cmd_sp;
605 std::unique_ptr<CommandObjectRegexCommand> undisplay_regex_cmd_ap(new CommandObjectRegexCommand(
606 *this, "_regexp-undisplay", "Stop displaying expression at every stop (specified by stop-hook index.)",
607 "_regexp-undisplay stop-hook-number", 2, 0, false));
608 if (undisplay_regex_cmd_ap.get())
610 if (undisplay_regex_cmd_ap->AddRegexCommand("^([0-9]+)$", "target stop-hook delete %1"))
612 CommandObjectSP undisplay_regex_cmd_sp(undisplay_regex_cmd_ap.release());
613 m_command_dict[undisplay_regex_cmd_sp->GetCommandName ()] = undisplay_regex_cmd_sp;
617 std::unique_ptr<CommandObjectRegexCommand> connect_gdb_remote_cmd_ap(new CommandObjectRegexCommand(
619 "Connect to a process via remote GDB server. If no host is specifed, localhost is assumed.",
620 "gdb-remote [<hostname>:]<portnum>", 2, 0, false));
621 if (connect_gdb_remote_cmd_ap.get())
623 if (connect_gdb_remote_cmd_ap->AddRegexCommand("^([^:]+:[[:digit:]]+)$", "process connect --plugin gdb-remote connect://%1") &&
624 connect_gdb_remote_cmd_ap->AddRegexCommand("^([[:digit:]]+)$", "process connect --plugin gdb-remote connect://localhost:%1"))
626 CommandObjectSP command_sp(connect_gdb_remote_cmd_ap.release());
627 m_command_dict[command_sp->GetCommandName ()] = command_sp;
631 std::unique_ptr<CommandObjectRegexCommand> connect_kdp_remote_cmd_ap(new CommandObjectRegexCommand(
633 "Connect to a process via remote KDP server. If no UDP port is specified, port 41139 is assumed.",
634 "kdp-remote <hostname>[:<portnum>]", 2, 0, false));
635 if (connect_kdp_remote_cmd_ap.get())
637 if (connect_kdp_remote_cmd_ap->AddRegexCommand("^([^:]+:[[:digit:]]+)$", "process connect --plugin kdp-remote udp://%1") &&
638 connect_kdp_remote_cmd_ap->AddRegexCommand("^(.+)$", "process connect --plugin kdp-remote udp://%1:41139"))
640 CommandObjectSP command_sp(connect_kdp_remote_cmd_ap.release());
641 m_command_dict[command_sp->GetCommandName ()] = command_sp;
645 std::unique_ptr<CommandObjectRegexCommand> bt_regex_cmd_ap(new CommandObjectRegexCommand(
646 *this, "_regexp-bt", "Show the current thread's call stack. Any numeric argument displays at most that many "
647 "frames. The argument 'all' displays all threads.",
648 "bt [<digit> | all]", 2, 0, false));
649 if (bt_regex_cmd_ap.get())
651 // accept but don't document "bt -c <number>" -- before bt was a regex command if you wanted to backtrace
652 // three frames you would do "bt -c 3" but the intention is to have this emulate the gdb "bt" command and
653 // so now "bt 3" is the preferred form, in line with gdb.
654 if (bt_regex_cmd_ap->AddRegexCommand("^([[:digit:]]+)$", "thread backtrace -c %1") &&
655 bt_regex_cmd_ap->AddRegexCommand("^-c ([[:digit:]]+)$", "thread backtrace -c %1") &&
656 bt_regex_cmd_ap->AddRegexCommand("^all$", "thread backtrace all") &&
657 bt_regex_cmd_ap->AddRegexCommand("^$", "thread backtrace"))
659 CommandObjectSP command_sp(bt_regex_cmd_ap.release());
660 m_command_dict[command_sp->GetCommandName ()] = command_sp;
664 std::unique_ptr<CommandObjectRegexCommand> list_regex_cmd_ap(new CommandObjectRegexCommand(
665 *this, "_regexp-list", "List relevant source code using one of several shorthand formats.",
667 "_regexp-list <file>:<line> // List around specific file/line\n"
668 "_regexp-list <line> // List current file around specified line\n"
669 "_regexp-list <function-name> // List specified function\n"
670 "_regexp-list 0x<address> // List around specified address\n"
671 "_regexp-list -[<count>] // List previous <count> lines\n"
672 "_regexp-list // List subsequent lines",
673 2, CommandCompletions::eSourceFileCompletion, false));
674 if (list_regex_cmd_ap.get())
676 if (list_regex_cmd_ap->AddRegexCommand("^([0-9]+)[[:space:]]*$", "source list --line %1") &&
677 list_regex_cmd_ap->AddRegexCommand("^(.*[^[:space:]])[[:space:]]*:[[:space:]]*([[:digit:]]+)[[:space:]]*$", "source list --file '%1' --line %2") &&
678 list_regex_cmd_ap->AddRegexCommand("^\\*?(0x[[:xdigit:]]+)[[:space:]]*$", "source list --address %1") &&
679 list_regex_cmd_ap->AddRegexCommand("^-[[:space:]]*$", "source list --reverse") &&
680 list_regex_cmd_ap->AddRegexCommand("^-([[:digit:]]+)[[:space:]]*$", "source list --reverse --count %1") &&
681 list_regex_cmd_ap->AddRegexCommand("^(.+)$", "source list --name \"%1\"") &&
682 list_regex_cmd_ap->AddRegexCommand("^$", "source list"))
684 CommandObjectSP list_regex_cmd_sp(list_regex_cmd_ap.release());
685 m_command_dict[list_regex_cmd_sp->GetCommandName ()] = list_regex_cmd_sp;
689 std::unique_ptr<CommandObjectRegexCommand> env_regex_cmd_ap(
690 new CommandObjectRegexCommand(*this, "_regexp-env", "Shorthand for viewing and setting environment variables.",
692 "_regexp-env // Show enrivonment\n"
693 "_regexp-env <name>=<value> // Set an environment variable",
695 if (env_regex_cmd_ap.get())
697 if (env_regex_cmd_ap->AddRegexCommand("^$", "settings show target.env-vars") &&
698 env_regex_cmd_ap->AddRegexCommand("^([A-Za-z_][A-Za-z_0-9]*=.*)$", "settings set target.env-vars %1"))
700 CommandObjectSP env_regex_cmd_sp(env_regex_cmd_ap.release());
701 m_command_dict[env_regex_cmd_sp->GetCommandName ()] = env_regex_cmd_sp;
705 std::unique_ptr<CommandObjectRegexCommand> jump_regex_cmd_ap(
706 new CommandObjectRegexCommand(*this, "_regexp-jump", "Set the program counter to a new address.",
708 "_regexp-jump <line>\n"
709 "_regexp-jump +<line-offset> | -<line-offset>\n"
710 "_regexp-jump <file>:<line>\n"
711 "_regexp-jump *<addr>\n",
713 if (jump_regex_cmd_ap.get())
715 if (jump_regex_cmd_ap->AddRegexCommand("^\\*(.*)$", "thread jump --addr %1") &&
716 jump_regex_cmd_ap->AddRegexCommand("^([0-9]+)$", "thread jump --line %1") &&
717 jump_regex_cmd_ap->AddRegexCommand("^([^:]+):([0-9]+)$", "thread jump --file %1 --line %2") &&
718 jump_regex_cmd_ap->AddRegexCommand("^([+\\-][0-9]+)$", "thread jump --by %1"))
720 CommandObjectSP jump_regex_cmd_sp(jump_regex_cmd_ap.release());
721 m_command_dict[jump_regex_cmd_sp->GetCommandName ()] = jump_regex_cmd_sp;
728 CommandInterpreter::GetCommandNamesMatchingPartialString (const char *cmd_str, bool include_aliases,
731 AddNamesMatchingPartialString (m_command_dict, cmd_str, matches);
735 AddNamesMatchingPartialString (m_alias_dict, cmd_str, matches);
738 return matches.GetSize();
742 CommandInterpreter::GetCommandSP (const char *cmd_cstr, bool include_aliases, bool exact, StringList *matches)
744 CommandObject::CommandMap::iterator pos;
745 CommandObjectSP command_sp;
747 std::string cmd(cmd_cstr);
751 pos = m_command_dict.find(cmd);
752 if (pos != m_command_dict.end())
753 command_sp = pos->second;
756 if (include_aliases && HasAliases())
758 auto alias_pos = m_alias_dict.find(cmd);
759 if (alias_pos != m_alias_dict.end())
760 command_sp = alias_pos->second;
763 if (HasUserCommands())
765 pos = m_user_dict.find(cmd);
766 if (pos != m_user_dict.end())
767 command_sp = pos->second;
770 if (!exact && !command_sp)
772 // We will only get into here if we didn't find any exact matches.
774 CommandObjectSP user_match_sp, alias_match_sp, real_match_sp;
776 StringList local_matches;
777 if (matches == nullptr)
778 matches = &local_matches;
780 unsigned int num_cmd_matches = 0;
781 unsigned int num_alias_matches = 0;
782 unsigned int num_user_matches = 0;
784 // Look through the command dictionaries one by one, and if we get only one match from any of
785 // them in toto, then return that, otherwise return an empty CommandObjectSP and the list of matches.
789 num_cmd_matches = AddNamesMatchingPartialString (m_command_dict, cmd_cstr, *matches);
792 if (num_cmd_matches == 1)
794 cmd.assign(matches->GetStringAtIndex(0));
795 pos = m_command_dict.find(cmd);
796 if (pos != m_command_dict.end())
797 real_match_sp = pos->second;
800 if (include_aliases && HasAliases())
802 num_alias_matches = AddNamesMatchingPartialString (m_alias_dict, cmd_cstr, *matches);
806 if (num_alias_matches == 1)
808 cmd.assign(matches->GetStringAtIndex (num_cmd_matches));
809 auto alias_pos = m_alias_dict.find(cmd);
810 if (alias_pos != m_alias_dict.end())
811 alias_match_sp = alias_pos->second;
814 if (HasUserCommands())
816 num_user_matches = AddNamesMatchingPartialString (m_user_dict, cmd_cstr, *matches);
819 if (num_user_matches == 1)
821 cmd.assign (matches->GetStringAtIndex (num_cmd_matches + num_alias_matches));
823 pos = m_user_dict.find (cmd);
824 if (pos != m_user_dict.end())
825 user_match_sp = pos->second;
828 // If we got exactly one match, return that, otherwise return the match list.
830 if (num_user_matches + num_cmd_matches + num_alias_matches == 1)
833 return real_match_sp;
834 else if (num_alias_matches)
835 return alias_match_sp;
837 return user_match_sp;
840 else if (matches && command_sp)
842 matches->AppendString (cmd_cstr);
850 CommandInterpreter::AddCommand (const char *name, const lldb::CommandObjectSP &cmd_sp, bool can_replace)
853 assert((this == &cmd_sp->GetCommandInterpreter()) && "tried to add a CommandObject from a different interpreter");
857 std::string name_sstr(name);
858 bool found = (m_command_dict.find (name_sstr) != m_command_dict.end());
859 if (found && !can_replace)
861 if (found && m_command_dict[name_sstr]->IsRemovable() == false)
863 m_command_dict[name_sstr] = cmd_sp;
870 CommandInterpreter::AddUserCommand (std::string name,
871 const lldb::CommandObjectSP &cmd_sp,
875 assert((this == &cmd_sp->GetCommandInterpreter()) && "tried to add a CommandObject from a different interpreter");
879 const char* name_cstr = name.c_str();
881 // do not allow replacement of internal commands
882 if (CommandExists(name_cstr))
884 if (can_replace == false)
886 if (m_command_dict[name]->IsRemovable() == false)
890 if (UserCommandExists(name_cstr))
892 if (can_replace == false)
894 if (m_user_dict[name]->IsRemovable() == false)
898 m_user_dict[name] = cmd_sp;
905 CommandInterpreter::GetCommandSPExact (const char *cmd_cstr, bool include_aliases)
907 Args cmd_words (cmd_cstr); // Break up the command string into words, in case it's a multi-word command.
908 CommandObjectSP ret_val; // Possibly empty return value.
910 if (cmd_cstr == nullptr)
913 if (cmd_words.GetArgumentCount() == 1)
914 return GetCommandSP(cmd_cstr, include_aliases, true, nullptr);
917 // We have a multi-word command (seemingly), so we need to do more work.
918 // First, get the cmd_obj_sp for the first word in the command.
919 CommandObjectSP cmd_obj_sp = GetCommandSP (cmd_words.GetArgumentAtIndex (0), include_aliases, true, nullptr);
920 if (cmd_obj_sp.get() != nullptr)
922 // Loop through the rest of the words in the command (everything passed in was supposed to be part of a
923 // command name), and find the appropriate sub-command SP for each command word....
924 size_t end = cmd_words.GetArgumentCount();
925 for (size_t j= 1; j < end; ++j)
927 if (cmd_obj_sp->IsMultiwordObject())
929 cmd_obj_sp = cmd_obj_sp->GetSubcommandSP (cmd_words.GetArgumentAtIndex (j));
930 if (cmd_obj_sp.get() == nullptr)
931 // The sub-command name was invalid. Fail and return the empty 'ret_val'.
935 // We have more words in the command name, but we don't have a multiword object. Fail and return
939 // We successfully looped through all the command words and got valid command objects for them. Assign the
940 // last object retrieved to 'ret_val'.
941 ret_val = cmd_obj_sp;
948 CommandInterpreter::GetCommandObjectExact (const char *cmd_cstr, bool include_aliases)
950 return GetCommandSPExact (cmd_cstr, include_aliases).get();
954 CommandInterpreter::GetCommandObject (const char *cmd_cstr, StringList *matches)
956 CommandObject *command_obj = GetCommandSP (cmd_cstr, false, true, matches).get();
958 // If we didn't find an exact match to the command string in the commands, look in
964 command_obj = GetCommandSP (cmd_cstr, true, true, matches).get();
969 // If there wasn't an exact match then look for an inexact one in just the commands
970 command_obj = GetCommandSP(cmd_cstr, false, false, nullptr).get();
972 // Finally, if there wasn't an inexact match among the commands, look for an inexact
973 // match in both the commands and aliases.
978 matches->AppendString(command_obj->GetCommandName());
982 return GetCommandSP(cmd_cstr, true, false, matches).get();
986 CommandInterpreter::CommandExists (const char *cmd)
988 return m_command_dict.find(cmd) != m_command_dict.end();
992 CommandInterpreter::GetAliasFullName (const char *cmd, std::string &full_name)
994 bool exact_match = (m_alias_dict.find(cmd) != m_alias_dict.end());
997 full_name.assign(cmd);
1003 size_t num_alias_matches;
1004 num_alias_matches = AddNamesMatchingPartialString (m_alias_dict, cmd, matches);
1005 if (num_alias_matches == 1)
1007 // Make sure this isn't shadowing a command in the regular command space:
1008 StringList regular_matches;
1009 const bool include_aliases = false;
1010 const bool exact = false;
1011 CommandObjectSP cmd_obj_sp(GetCommandSP (cmd, include_aliases, exact, ®ular_matches));
1012 if (cmd_obj_sp || regular_matches.GetSize() > 0)
1016 full_name.assign (matches.GetStringAtIndex(0));
1026 CommandInterpreter::AliasExists (const char *cmd)
1028 return m_alias_dict.find(cmd) != m_alias_dict.end();
1032 CommandInterpreter::UserCommandExists (const char *cmd)
1034 return m_user_dict.find(cmd) != m_user_dict.end();
1038 CommandInterpreter::AddAlias (const char *alias_name,
1039 lldb::CommandObjectSP& command_obj_sp,
1040 const char *args_string)
1042 if (command_obj_sp.get())
1043 assert((this == &command_obj_sp->GetCommandInterpreter()) && "tried to add a CommandObject from a different interpreter");
1045 std::unique_ptr<CommandAlias> command_alias_up(new CommandAlias(*this,
1050 if (command_alias_up && command_alias_up->IsValid())
1052 m_alias_dict[alias_name] = CommandObjectSP(command_alias_up.get());
1053 return command_alias_up.release();
1060 CommandInterpreter::RemoveAlias (const char *alias_name)
1062 auto pos = m_alias_dict.find(alias_name);
1063 if (pos != m_alias_dict.end())
1065 m_alias_dict.erase(pos);
1072 CommandInterpreter::RemoveCommand (const char *cmd)
1074 auto pos = m_command_dict.find(cmd);
1075 if (pos != m_command_dict.end())
1077 if (pos->second->IsRemovable())
1079 // Only regular expression objects or python commands are removable
1080 m_command_dict.erase(pos);
1087 CommandInterpreter::RemoveUser (const char *alias_name)
1089 CommandObject::CommandMap::iterator pos = m_user_dict.find(alias_name);
1090 if (pos != m_user_dict.end())
1092 m_user_dict.erase(pos);
1099 CommandInterpreter::GetHelp (CommandReturnObject &result,
1102 const char * help_prologue = GetDebugger().GetIOHandlerHelpPrologue();
1103 if (help_prologue != NULL)
1105 OutputFormattedHelpText(result.GetOutputStream(), NULL, help_prologue);
1108 CommandObject::CommandMap::const_iterator pos;
1109 size_t max_len = FindLongestCommandWord (m_command_dict);
1111 if ( (cmd_types & eCommandTypesBuiltin) == eCommandTypesBuiltin )
1113 result.AppendMessage("Debugger commands:");
1114 result.AppendMessage("");
1116 for (pos = m_command_dict.begin(); pos != m_command_dict.end(); ++pos)
1118 if (!(cmd_types & eCommandTypesHidden) && (pos->first.compare(0, 1, "_") == 0))
1121 OutputFormattedHelpText (result.GetOutputStream(), pos->first.c_str(), "--", pos->second->GetHelp(),
1124 result.AppendMessage("");
1128 if (!m_alias_dict.empty() && ( (cmd_types & eCommandTypesAliases) == eCommandTypesAliases ))
1130 result.AppendMessageWithFormat("Current command abbreviations "
1131 "(type '%shelp command alias' for more info):\n",
1132 GetCommandPrefix());
1133 result.AppendMessage("");
1134 max_len = FindLongestCommandWord (m_alias_dict);
1136 for (auto alias_pos = m_alias_dict.begin(); alias_pos != m_alias_dict.end(); ++alias_pos)
1138 OutputFormattedHelpText (result.GetOutputStream(), alias_pos->first.c_str(), "--", alias_pos->second->GetHelp(),
1141 result.AppendMessage("");
1144 if (!m_user_dict.empty() && ( (cmd_types & eCommandTypesUserDef) == eCommandTypesUserDef ))
1146 result.AppendMessage ("Current user-defined commands:");
1147 result.AppendMessage("");
1148 max_len = FindLongestCommandWord (m_user_dict);
1149 for (pos = m_user_dict.begin(); pos != m_user_dict.end(); ++pos)
1151 OutputFormattedHelpText (result.GetOutputStream(), pos->first.c_str(), "--", pos->second->GetHelp(),
1154 result.AppendMessage("");
1157 result.AppendMessageWithFormat("For more information on any command, type '%shelp <command-name>'.\n",
1158 GetCommandPrefix());
1162 CommandInterpreter::GetCommandObjectForCommand (std::string &command_string)
1164 // This function finds the final, lowest-level, alias-resolved command object whose 'Execute' function will
1165 // eventually be invoked by the given command line.
1167 CommandObject *cmd_obj = nullptr;
1168 size_t start = command_string.find_first_not_of (k_white_space);
1173 if (start != std::string::npos)
1175 // Get the next word from command_string.
1176 end = command_string.find_first_of (k_white_space, start);
1177 if (end == std::string::npos)
1178 end = command_string.size();
1179 std::string cmd_word = command_string.substr (start, end - start);
1181 if (cmd_obj == nullptr)
1182 // Since cmd_obj is NULL we are on our first time through this loop. Check to see if cmd_word is a valid
1183 // command or alias.
1184 cmd_obj = GetCommandObject (cmd_word.c_str());
1185 else if (cmd_obj->IsMultiwordObject ())
1187 // Our current object is a multi-word object; see if the cmd_word is a valid sub-command for our object.
1188 CommandObject *sub_cmd_obj = cmd_obj->GetSubcommandObject (cmd_word.c_str());
1190 cmd_obj = sub_cmd_obj;
1191 else // cmd_word was not a valid sub-command word, so we are done
1195 // We have a cmd_obj and it is not a multi-word object, so we are done.
1198 // If we didn't find a valid command object, or our command object is not a multi-word object, or
1199 // we are at the end of the command_string, then we are done. Otherwise, find the start of the
1202 if (!cmd_obj || !cmd_obj->IsMultiwordObject() || end >= command_string.size())
1205 start = command_string.find_first_not_of (k_white_space, end);
1208 // Unable to find any more words.
1212 if (end == command_string.size())
1213 command_string.clear();
1215 command_string = command_string.substr(end);
1220 static const char *k_valid_command_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_";
1222 StripLeadingSpaces (std::string &s)
1226 size_t pos = s.find_first_not_of (k_white_space);
1227 if (pos == std::string::npos)
1236 FindArgumentTerminator (const std::string &s)
1238 const size_t s_len = s.size();
1240 while (offset < s_len)
1242 size_t pos = s.find ("--", offset);
1243 if (pos == std::string::npos)
1247 if (isspace(s[pos-1]))
1249 // Check if the string ends "\s--" (where \s is a space character)
1250 // or if we have "\s--\s".
1251 if ((pos + 2 >= s_len) || isspace(s[pos+2]))
1259 return std::string::npos;
1263 ExtractCommand (std::string &command_string, std::string &command, std::string &suffix, char "e_char)
1267 StripLeadingSpaces (command_string);
1269 bool result = false;
1272 if (!command_string.empty())
1274 const char first_char = command_string[0];
1275 if (first_char == '\'' || first_char == '"')
1277 quote_char = first_char;
1278 const size_t end_quote_pos = command_string.find (quote_char, 1);
1279 if (end_quote_pos == std::string::npos)
1281 command.swap (command_string);
1282 command_string.erase ();
1286 command.assign (command_string, 1, end_quote_pos - 1);
1287 if (end_quote_pos + 1 < command_string.size())
1288 command_string.erase (0, command_string.find_first_not_of (k_white_space, end_quote_pos + 1));
1290 command_string.erase ();
1295 const size_t first_space_pos = command_string.find_first_of (k_white_space);
1296 if (first_space_pos == std::string::npos)
1298 command.swap (command_string);
1299 command_string.erase();
1303 command.assign (command_string, 0, first_space_pos);
1304 command_string.erase(0, command_string.find_first_not_of (k_white_space, first_space_pos));
1311 if (!command.empty())
1313 // actual commands can't start with '-' or '_'
1314 if (command[0] != '-' && command[0] != '_')
1316 size_t pos = command.find_first_not_of(k_valid_command_chars);
1317 if (pos > 0 && pos != std::string::npos)
1319 suffix.assign (command.begin() + pos, command.end());
1320 command.erase (pos);
1329 CommandInterpreter::BuildAliasResult (const char *alias_name,
1330 std::string &raw_input_string,
1331 std::string &alias_result,
1332 CommandReturnObject &result)
1334 CommandObject *alias_cmd_obj = nullptr;
1335 Args cmd_args (raw_input_string);
1336 alias_cmd_obj = GetCommandObject (alias_name);
1337 StreamString result_str;
1339 if (alias_cmd_obj && alias_cmd_obj->IsAlias())
1341 std::pair<CommandObjectSP, OptionArgVectorSP> desugared = ((CommandAlias*)alias_cmd_obj)->Desugar();
1342 OptionArgVectorSP option_arg_vector_sp = desugared.second;
1343 alias_cmd_obj = desugared.first.get();
1344 std::string alias_name_str = alias_name;
1345 if ((cmd_args.GetArgumentCount() == 0)
1346 || (alias_name_str.compare (cmd_args.GetArgumentAtIndex(0)) != 0))
1347 cmd_args.Unshift (alias_name);
1349 result_str.Printf ("%s", alias_cmd_obj->GetCommandName ());
1351 if (option_arg_vector_sp.get())
1353 OptionArgVector *option_arg_vector = option_arg_vector_sp.get();
1355 for (size_t i = 0; i < option_arg_vector->size(); ++i)
1357 OptionArgPair option_pair = (*option_arg_vector)[i];
1358 OptionArgValue value_pair = option_pair.second;
1359 int value_type = value_pair.first;
1360 std::string option = option_pair.first;
1361 std::string value = value_pair.second;
1362 if (option.compare ("<argument>") == 0)
1363 result_str.Printf (" %s", value.c_str());
1366 result_str.Printf (" %s", option.c_str());
1367 if (value_type != OptionParser::eNoArgument)
1369 if (value_type != OptionParser::eOptionalArgument)
1370 result_str.Printf (" ");
1371 int index = GetOptionArgumentPosition (value.c_str());
1373 result_str.Printf ("%s", value.c_str());
1374 else if (static_cast<size_t>(index) >= cmd_args.GetArgumentCount())
1377 result.AppendErrorWithFormat
1378 ("Not enough arguments provided; you need at least %d arguments to use this alias.\n",
1380 result.SetStatus (eReturnStatusFailed);
1385 size_t strpos = raw_input_string.find (cmd_args.GetArgumentAtIndex (index));
1386 if (strpos != std::string::npos)
1387 raw_input_string = raw_input_string.erase (strpos,
1388 strlen (cmd_args.GetArgumentAtIndex (index)));
1389 result_str.Printf ("%s", cmd_args.GetArgumentAtIndex (index));
1396 alias_result = result_str.GetData();
1398 return alias_cmd_obj;
1402 CommandInterpreter::PreprocessCommand (std::string &command)
1404 // The command preprocessor needs to do things to the command
1405 // line before any parsing of arguments or anything else is done.
1406 // The only current stuff that gets preprocessed is anything enclosed
1407 // in backtick ('`') characters is evaluated as an expression and
1408 // the result of the expression must be a scalar that can be substituted
1409 // into the command. An example would be:
1410 // (lldb) memory read `$rsp + 20`
1411 Error error; // Error for any expressions that might not evaluate
1412 size_t start_backtick;
1414 while ((start_backtick = command.find ('`', pos)) != std::string::npos)
1416 if (start_backtick > 0 && command[start_backtick-1] == '\\')
1418 // The backtick was preceded by a '\' character, remove the slash
1419 // and don't treat the backtick as the start of an expression
1420 command.erase(start_backtick-1, 1);
1421 // No need to add one to start_backtick since we just deleted a char
1422 pos = start_backtick;
1426 const size_t expr_content_start = start_backtick + 1;
1427 const size_t end_backtick = command.find ('`', expr_content_start);
1428 if (end_backtick == std::string::npos)
1430 else if (end_backtick == expr_content_start)
1432 // Empty expression (two backticks in a row)
1433 command.erase (start_backtick, 2);
1437 std::string expr_str (command, expr_content_start, end_backtick - expr_content_start);
1439 ExecutionContext exe_ctx(GetExecutionContext());
1440 Target *target = exe_ctx.GetTargetPtr();
1441 // Get a dummy target to allow for calculator mode while processing backticks.
1442 // This also helps break the infinite loop caused when target is null.
1444 target = m_debugger.GetDummyTarget();
1447 ValueObjectSP expr_result_valobj_sp;
1449 EvaluateExpressionOptions options;
1450 options.SetCoerceToId(false);
1451 options.SetUnwindOnError(true);
1452 options.SetIgnoreBreakpoints(true);
1453 options.SetKeepInMemory(false);
1454 options.SetTryAllThreads(true);
1455 options.SetTimeoutUsec(0);
1457 ExpressionResults expr_result = target->EvaluateExpression (expr_str.c_str(),
1458 exe_ctx.GetFramePtr(),
1459 expr_result_valobj_sp,
1462 if (expr_result == eExpressionCompleted)
1465 if (expr_result_valobj_sp)
1466 expr_result_valobj_sp = expr_result_valobj_sp->GetQualifiedRepresentationIfAvailable(expr_result_valobj_sp->GetDynamicValueType(), true);
1467 if (expr_result_valobj_sp->ResolveValue (scalar))
1469 command.erase (start_backtick, end_backtick - start_backtick + 1);
1470 StreamString value_strm;
1471 const bool show_type = false;
1472 scalar.GetValue (&value_strm, show_type);
1473 size_t value_string_size = value_strm.GetSize();
1474 if (value_string_size)
1476 command.insert (start_backtick, value_strm.GetData(), value_string_size);
1477 pos = start_backtick + value_string_size;
1482 error.SetErrorStringWithFormat("expression value didn't result in a scalar value for the expression '%s'", expr_str.c_str());
1487 error.SetErrorStringWithFormat("expression value didn't result in a scalar value for the expression '%s'", expr_str.c_str());
1492 if (expr_result_valobj_sp)
1493 error = expr_result_valobj_sp->GetError();
1494 if (error.Success())
1497 switch (expr_result)
1499 case eExpressionSetupError:
1500 error.SetErrorStringWithFormat("expression setup error for the expression '%s'", expr_str.c_str());
1502 case eExpressionParseError:
1503 error.SetErrorStringWithFormat ("expression parse error for the expression '%s'", expr_str.c_str());
1505 case eExpressionResultUnavailable:
1506 error.SetErrorStringWithFormat ("expression error fetching result for the expression '%s'", expr_str.c_str());
1508 case eExpressionCompleted:
1510 case eExpressionDiscarded:
1511 error.SetErrorStringWithFormat("expression discarded for the expression '%s'", expr_str.c_str());
1513 case eExpressionInterrupted:
1514 error.SetErrorStringWithFormat("expression interrupted for the expression '%s'", expr_str.c_str());
1516 case eExpressionHitBreakpoint:
1517 error.SetErrorStringWithFormat("expression hit breakpoint for the expression '%s'", expr_str.c_str());
1519 case eExpressionTimedOut:
1520 error.SetErrorStringWithFormat("expression timed out for the expression '%s'", expr_str.c_str());
1522 case eExpressionStoppedForDebug:
1523 error.SetErrorStringWithFormat("expression stop at entry point for debugging for the expression '%s'", expr_str.c_str());
1539 CommandInterpreter::HandleCommand (const char *command_line,
1540 LazyBool lazy_add_to_history,
1541 CommandReturnObject &result,
1542 ExecutionContext *override_context,
1543 bool repeat_on_empty_command,
1544 bool no_context_switching)
1548 std::string command_string (command_line);
1549 std::string original_command_string (command_line);
1551 Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_COMMANDS));
1552 Host::SetCrashDescriptionWithFormat ("HandleCommand(command = \"%s\")", command_line);
1554 // Make a scoped cleanup object that will clear the crash description string
1555 // on exit of this function.
1556 lldb_utility::CleanUp <const char *> crash_description_cleanup(nullptr, Host::SetCrashDescription);
1559 log->Printf ("Processing command: %s", command_line);
1561 Timer scoped_timer (__PRETTY_FUNCTION__, "Handling command: %s.", command_line);
1563 if (!no_context_switching)
1564 UpdateExecutionContext (override_context);
1566 bool add_to_history;
1567 if (lazy_add_to_history == eLazyBoolCalculate)
1568 add_to_history = (m_command_source_depth == 0);
1570 add_to_history = (lazy_add_to_history == eLazyBoolYes);
1572 bool empty_command = false;
1573 bool comment_command = false;
1574 if (command_string.empty())
1575 empty_command = true;
1578 const char *k_space_characters = "\t\n\v\f\r ";
1580 size_t non_space = command_string.find_first_not_of (k_space_characters);
1581 // Check for empty line or comment line (lines whose first
1582 // non-space character is the comment character for this interpreter)
1583 if (non_space == std::string::npos)
1584 empty_command = true;
1585 else if (command_string[non_space] == m_comment_char)
1586 comment_command = true;
1587 else if (command_string[non_space] == CommandHistory::g_repeat_char)
1589 const char *history_string = m_command_history.FindString(command_string.c_str() + non_space);
1590 if (history_string == nullptr)
1592 result.AppendErrorWithFormat ("Could not find entry: %s in history", command_string.c_str());
1593 result.SetStatus(eReturnStatusFailed);
1596 add_to_history = false;
1597 command_string = history_string;
1598 original_command_string = history_string;
1604 if (repeat_on_empty_command)
1606 if (m_command_history.IsEmpty())
1608 result.AppendError ("empty command");
1609 result.SetStatus(eReturnStatusFailed);
1614 command_line = m_repeat_command.c_str();
1615 command_string = command_line;
1616 original_command_string = command_line;
1617 if (m_repeat_command.empty())
1619 result.AppendErrorWithFormat("No auto repeat.\n");
1620 result.SetStatus (eReturnStatusFailed);
1624 add_to_history = false;
1628 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1632 else if (comment_command)
1634 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1639 Error error (PreprocessCommand (command_string));
1643 result.AppendError (error.AsCString());
1644 result.SetStatus(eReturnStatusFailed);
1650 // Before we do ANY kind of argument processing, we need to figure out what
1651 // the real/final command object is for the specified command. This gets
1652 // complicated by the fact that the user could have specified an alias, and,
1653 // in translating the alias, there may also be command options and/or even
1654 // data (including raw text strings) that need to be found and inserted into
1655 // the command line as part of the translation. So this first step is plain
1656 // look-up and replacement, resulting in:
1657 // 1. the command object whose Execute method will actually be called
1658 // 2. a revised command string, with all substitutions and replacements
1660 // From 1 above, we can determine whether the Execute function wants raw
1663 CommandObject *cmd_obj = ResolveCommandImpl(command_string, result);
1665 // Although the user may have abbreviated the command, the command_string now
1666 // has the command expanded to the full name. For example, if the input
1667 // was "br s -n main", command_string is now "breakpoint set -n main".
1671 log->Printf("HandleCommand, cmd_obj : '%s'", cmd_obj ? cmd_obj->GetCommandName() : "<not found>");
1672 log->Printf("HandleCommand, (revised) command_string: '%s'", command_string.c_str());
1673 const bool wants_raw_input = (cmd_obj != NULL) ? cmd_obj->WantsRawCommandString() : false;
1674 log->Printf("HandleCommand, wants_raw_input:'%s'", wants_raw_input ? "True" : "False");
1678 // Take care of things like setting up the history command & calling the appropriate Execute method on the
1679 // CommandObject, with the appropriate arguments.
1681 if (cmd_obj != nullptr)
1685 Args command_args (command_string);
1686 const char *repeat_command = cmd_obj->GetRepeatCommand(command_args, 0);
1687 if (repeat_command != nullptr)
1688 m_repeat_command.assign(repeat_command);
1690 m_repeat_command.assign(original_command_string.c_str());
1692 m_command_history.AppendString (original_command_string);
1695 std::string remainder;
1696 const std::size_t actual_cmd_name_len = strlen (cmd_obj->GetCommandName());
1697 if (actual_cmd_name_len < command_string.length())
1698 remainder = command_string.substr (actual_cmd_name_len);
1700 // Remove any initial spaces
1701 size_t pos = remainder.find_first_not_of (k_white_space);
1702 if (pos != 0 && pos != std::string::npos)
1703 remainder.erase(0, pos);
1706 log->Printf ("HandleCommand, command line after removing command name(s): '%s'", remainder.c_str());
1708 cmd_obj->Execute (remainder.c_str(), result);
1712 // We didn't find the first command object, so complete the first argument.
1713 Args command_args (command_string);
1716 int cursor_index = 0;
1717 int cursor_char_position = strlen (command_args.GetArgumentAtIndex(0));
1719 num_matches = HandleCompletionMatches (command_args,
1721 cursor_char_position,
1727 if (num_matches > 0)
1729 std::string error_msg;
1730 error_msg.assign ("ambiguous command '");
1731 error_msg.append(command_args.GetArgumentAtIndex(0));
1732 error_msg.append ("'.");
1734 error_msg.append (" Possible completions:");
1735 for (int i = 0; i < num_matches; i++)
1737 error_msg.append ("\n\t");
1738 error_msg.append (matches.GetStringAtIndex (i));
1740 error_msg.append ("\n");
1741 result.AppendRawError (error_msg.c_str());
1744 result.AppendErrorWithFormat ("Unrecognized command '%s'.\n", command_args.GetArgumentAtIndex (0));
1746 result.SetStatus (eReturnStatusFailed);
1750 log->Printf ("HandleCommand, command %s", (result.Succeeded() ? "succeeded" : "did not succeed"));
1752 return result.Succeeded();
1756 CommandInterpreter::HandleCompletionMatches (Args &parsed_line,
1758 int &cursor_char_position,
1759 int match_start_point,
1760 int max_return_elements,
1761 bool &word_complete,
1762 StringList &matches)
1764 int num_command_matches = 0;
1765 bool look_for_subcommand = false;
1767 // For any of the command completions a unique match will be a complete word.
1768 word_complete = true;
1770 if (cursor_index == -1)
1772 // We got nothing on the command line, so return the list of commands
1773 bool include_aliases = true;
1774 num_command_matches = GetCommandNamesMatchingPartialString ("", include_aliases, matches);
1776 else if (cursor_index == 0)
1778 // The cursor is in the first argument, so just do a lookup in the dictionary.
1779 CommandObject *cmd_obj = GetCommandObject (parsed_line.GetArgumentAtIndex(0), &matches);
1780 num_command_matches = matches.GetSize();
1782 if (num_command_matches == 1
1783 && cmd_obj && cmd_obj->IsMultiwordObject()
1784 && matches.GetStringAtIndex(0) != nullptr
1785 && strcmp (parsed_line.GetArgumentAtIndex(0), matches.GetStringAtIndex(0)) == 0)
1787 if (parsed_line.GetArgumentCount() == 1)
1789 word_complete = true;
1793 look_for_subcommand = true;
1794 num_command_matches = 0;
1795 matches.DeleteStringAtIndex(0);
1796 parsed_line.AppendArgument ("");
1798 cursor_char_position = 0;
1803 if (cursor_index > 0 || look_for_subcommand)
1805 // We are completing further on into a commands arguments, so find the command and tell it
1806 // to complete the command.
1807 // First see if there is a matching initial command:
1808 CommandObject *command_object = GetCommandObject (parsed_line.GetArgumentAtIndex(0));
1809 if (command_object == nullptr)
1815 parsed_line.Shift();
1817 num_command_matches = command_object->HandleCompletion (parsed_line,
1819 cursor_char_position,
1821 max_return_elements,
1827 return num_command_matches;
1832 CommandInterpreter::HandleCompletion (const char *current_line,
1834 const char *last_char,
1835 int match_start_point,
1836 int max_return_elements,
1837 StringList &matches)
1839 // We parse the argument up to the cursor, so the last argument in parsed_line is
1840 // the one containing the cursor, and the cursor is after the last character.
1842 Args parsed_line(llvm::StringRef(current_line, last_char - current_line));
1843 Args partial_parsed_line(llvm::StringRef(current_line, cursor - current_line));
1845 // Don't complete comments, and if the line we are completing is just the history repeat character,
1846 // substitute the appropriate history line.
1847 const char *first_arg = parsed_line.GetArgumentAtIndex(0);
1850 if (first_arg[0] == m_comment_char)
1852 else if (first_arg[0] == CommandHistory::g_repeat_char)
1854 const char *history_string = m_command_history.FindString (first_arg);
1855 if (history_string != nullptr)
1858 matches.InsertStringAtIndex(0, history_string);
1868 int num_args = partial_parsed_line.GetArgumentCount();
1869 int cursor_index = partial_parsed_line.GetArgumentCount() - 1;
1870 int cursor_char_position;
1872 if (cursor_index == -1)
1873 cursor_char_position = 0;
1875 cursor_char_position = strlen (partial_parsed_line.GetArgumentAtIndex(cursor_index));
1877 if (cursor > current_line && cursor[-1] == ' ')
1879 // We are just after a space. If we are in an argument, then we will continue
1880 // parsing, but if we are between arguments, then we have to complete whatever the next
1881 // element would be.
1882 // We can distinguish the two cases because if we are in an argument (e.g. because the space is
1883 // protected by a quote) then the space will also be in the parsed argument...
1885 const char *current_elem = partial_parsed_line.GetArgumentAtIndex(cursor_index);
1886 if (cursor_char_position == 0 || current_elem[cursor_char_position - 1] != ' ')
1888 parsed_line.InsertArgumentAtIndex(cursor_index + 1, "", '\0');
1890 cursor_char_position = 0;
1894 int num_command_matches;
1898 // Only max_return_elements == -1 is supported at present:
1899 assert (max_return_elements == -1);
1901 num_command_matches = HandleCompletionMatches (parsed_line,
1903 cursor_char_position,
1905 max_return_elements,
1909 if (num_command_matches <= 0)
1910 return num_command_matches;
1914 // If we got an empty string, insert nothing.
1915 matches.InsertStringAtIndex(0, "");
1919 // Now figure out if there is a common substring, and if so put that in element 0, otherwise
1920 // put an empty string in element 0.
1921 std::string command_partial_str;
1922 if (cursor_index >= 0)
1923 command_partial_str.assign(parsed_line.GetArgumentAtIndex(cursor_index),
1924 parsed_line.GetArgumentAtIndex(cursor_index) + cursor_char_position);
1926 std::string common_prefix;
1927 matches.LongestCommonPrefix (common_prefix);
1928 const size_t partial_name_len = command_partial_str.size();
1929 common_prefix.erase (0, partial_name_len);
1931 // If we matched a unique single command, add a space...
1932 // Only do this if the completer told us this was a complete word, however...
1933 if (num_command_matches == 1 && word_complete)
1935 char quote_char = parsed_line.GetArgumentQuoteCharAtIndex(cursor_index);
1936 common_prefix = Args::EscapeLLDBCommandArgument(common_prefix, quote_char);
1937 if (quote_char != '\0')
1938 common_prefix.push_back(quote_char);
1939 common_prefix.push_back(' ');
1941 matches.InsertStringAtIndex(0, common_prefix.c_str());
1943 return num_command_matches;
1947 CommandInterpreter::~CommandInterpreter ()
1952 CommandInterpreter::UpdatePrompt (const char *new_prompt)
1954 EventSP prompt_change_event_sp (new Event(eBroadcastBitResetPrompt, new EventDataBytes (new_prompt)));;
1955 BroadcastEvent (prompt_change_event_sp);
1956 if (m_command_io_handler_sp)
1957 m_command_io_handler_sp->SetPrompt(new_prompt);
1962 CommandInterpreter::Confirm (const char *message, bool default_answer)
1964 // Check AutoConfirm first:
1965 if (m_debugger.GetAutoConfirm())
1966 return default_answer;
1968 IOHandlerConfirm *confirm = new IOHandlerConfirm(m_debugger,
1971 IOHandlerSP io_handler_sp (confirm);
1972 m_debugger.RunIOHandler (io_handler_sp);
1973 return confirm->GetResponse();
1977 CommandInterpreter::GetAlias (const char *alias_name)
1979 OptionArgVectorSP ret_val;
1981 std::string alias (alias_name);
1983 auto pos = m_alias_dict.find(alias);
1984 if (pos != m_alias_dict.end())
1985 return (CommandAlias*)pos->second.get();
1991 CommandInterpreter::HasCommands ()
1993 return (!m_command_dict.empty());
1997 CommandInterpreter::HasAliases ()
1999 return (!m_alias_dict.empty());
2003 CommandInterpreter::HasUserCommands ()
2005 return (!m_user_dict.empty());
2009 CommandInterpreter::HasAliasOptions ()
2011 return HasAliases();
2015 CommandInterpreter::BuildAliasCommandArgs (CommandObject *alias_cmd_obj,
2016 const char *alias_name,
2018 std::string &raw_input_string,
2019 CommandReturnObject &result)
2021 OptionArgVectorSP option_arg_vector_sp = GetAlias(alias_name)->GetOptionArguments();
2023 bool wants_raw_input = alias_cmd_obj->WantsRawCommandString();
2025 // Make sure that the alias name is the 0th element in cmd_args
2026 std::string alias_name_str = alias_name;
2027 if (alias_name_str.compare (cmd_args.GetArgumentAtIndex(0)) != 0)
2028 cmd_args.Unshift (alias_name);
2030 Args new_args (alias_cmd_obj->GetCommandName());
2031 if (new_args.GetArgumentCount() == 2)
2034 if (option_arg_vector_sp.get())
2036 if (wants_raw_input)
2038 // We have a command that both has command options and takes raw input. Make *sure* it has a
2039 // " -- " in the right place in the raw_input_string.
2040 size_t pos = raw_input_string.find(" -- ");
2041 if (pos == std::string::npos)
2043 // None found; assume it goes at the beginning of the raw input string
2044 raw_input_string.insert (0, " -- ");
2048 OptionArgVector *option_arg_vector = option_arg_vector_sp.get();
2049 const size_t old_size = cmd_args.GetArgumentCount();
2050 std::vector<bool> used (old_size + 1, false);
2054 for (size_t i = 0; i < option_arg_vector->size(); ++i)
2056 OptionArgPair option_pair = (*option_arg_vector)[i];
2057 OptionArgValue value_pair = option_pair.second;
2058 int value_type = value_pair.first;
2059 std::string option = option_pair.first;
2060 std::string value = value_pair.second;
2061 if (option.compare ("<argument>") == 0)
2063 if (!wants_raw_input
2064 || (value.compare("--") != 0)) // Since we inserted this above, make sure we don't insert it twice
2065 new_args.AppendArgument (value.c_str());
2069 if (value_type != OptionParser::eOptionalArgument)
2070 new_args.AppendArgument (option.c_str());
2071 if (value.compare ("<no-argument>") != 0)
2073 int index = GetOptionArgumentPosition (value.c_str());
2076 // value was NOT a positional argument; must be a real value
2077 if (value_type != OptionParser::eOptionalArgument)
2078 new_args.AppendArgument (value.c_str());
2082 ::snprintf (buffer, sizeof (buffer), "%s%s", option.c_str(), value.c_str());
2083 new_args.AppendArgument (buffer);
2087 else if (static_cast<size_t>(index) >= cmd_args.GetArgumentCount())
2089 result.AppendErrorWithFormat
2090 ("Not enough arguments provided; you need at least %d arguments to use this alias.\n",
2092 result.SetStatus (eReturnStatusFailed);
2097 // Find and remove cmd_args.GetArgumentAtIndex(i) from raw_input_string
2098 size_t strpos = raw_input_string.find (cmd_args.GetArgumentAtIndex (index));
2099 if (strpos != std::string::npos)
2101 raw_input_string = raw_input_string.erase (strpos, strlen (cmd_args.GetArgumentAtIndex (index)));
2104 if (value_type != OptionParser::eOptionalArgument)
2105 new_args.AppendArgument (cmd_args.GetArgumentAtIndex (index));
2109 ::snprintf (buffer, sizeof(buffer), "%s%s", option.c_str(),
2110 cmd_args.GetArgumentAtIndex (index));
2111 new_args.AppendArgument (buffer);
2119 for (size_t j = 0; j < cmd_args.GetArgumentCount(); ++j)
2121 if (!used[j] && !wants_raw_input)
2122 new_args.AppendArgument (cmd_args.GetArgumentAtIndex (j));
2126 cmd_args.SetArguments (new_args.GetArgumentCount(), new_args.GetConstArgumentVector());
2130 result.SetStatus (eReturnStatusSuccessFinishNoResult);
2131 // This alias was not created with any options; nothing further needs to be done, unless it is a command that
2132 // wants raw input, in which case we need to clear the rest of the data from cmd_args, since its in the raw
2134 if (wants_raw_input)
2137 cmd_args.SetArguments (new_args.GetArgumentCount(), new_args.GetConstArgumentVector());
2142 result.SetStatus (eReturnStatusSuccessFinishNoResult);
2148 CommandInterpreter::GetOptionArgumentPosition (const char *in_string)
2150 int position = 0; // Any string that isn't an argument position, i.e. '%' followed by an integer, gets a position
2153 const char *cptr = in_string;
2155 // Does it start with '%'
2160 // Is the rest of it entirely digits?
2161 if (isdigit (cptr[0]))
2163 const char *start = cptr;
2164 while (isdigit (cptr[0]))
2167 // We've gotten to the end of the digits; are we at the end of the string?
2168 if (cptr[0] == '\0')
2169 position = atoi (start);
2177 CommandInterpreter::SourceInitFile (bool in_cwd, CommandReturnObject &result)
2182 ExecutionContext exe_ctx(GetExecutionContext());
2183 Target *target = exe_ctx.GetTargetPtr();
2186 // In the current working directory we don't load any program specific
2187 // .lldbinit files, we only look for a ".lldbinit" file.
2188 if (m_skip_lldbinit_files)
2191 LoadCWDlldbinitFile should_load = target->TargetProperties::GetLoadCWDlldbinitFile ();
2192 if (should_load == eLoadCWDlldbinitWarn)
2194 FileSpec dot_lldb (".lldbinit", true);
2195 llvm::SmallString<64> home_dir_path;
2196 llvm::sys::path::home_directory (home_dir_path);
2197 FileSpec homedir_dot_lldb (home_dir_path.c_str(), false);
2198 homedir_dot_lldb.AppendPathComponent (".lldbinit");
2199 homedir_dot_lldb.ResolvePath ();
2200 if (dot_lldb.Exists ()
2201 && dot_lldb.GetDirectory() != homedir_dot_lldb.GetDirectory())
2203 result.AppendErrorWithFormat (
2204 "There is a .lldbinit file in the current directory which is not being read.\n"
2205 "To silence this warning without sourcing in the local .lldbinit,\n"
2206 "add the following to the lldbinit file in your home directory:\n"
2207 " settings set target.load-cwd-lldbinit false\n"
2208 "To allow lldb to source .lldbinit files in the current working directory,\n"
2209 "set the value of this variable to true. Only do so if you understand and\n"
2210 "accept the security risk.");
2211 result.SetStatus (eReturnStatusFailed);
2215 else if (should_load == eLoadCWDlldbinitTrue)
2217 init_file.SetFile ("./.lldbinit", true);
2223 // If we aren't looking in the current working directory we are looking
2224 // in the home directory. We will first see if there is an application
2225 // specific ".lldbinit" file whose name is "~/.lldbinit" followed by a
2226 // "-" and the name of the program. If this file doesn't exist, we fall
2227 // back to just the "~/.lldbinit" file. We also obey any requests to not
2228 // load the init files.
2229 llvm::SmallString<64> home_dir_path;
2230 llvm::sys::path::home_directory(home_dir_path);
2231 FileSpec profilePath(home_dir_path.c_str(), false);
2232 profilePath.AppendPathComponent(".lldbinit");
2233 std::string init_file_path = profilePath.GetPath();
2235 if (m_skip_app_init_files == false)
2237 FileSpec program_file_spec(HostInfo::GetProgramFileSpec());
2238 const char *program_name = program_file_spec.GetFilename().AsCString();
2242 char program_init_file_name[PATH_MAX];
2243 ::snprintf (program_init_file_name, sizeof(program_init_file_name), "%s-%s", init_file_path.c_str(), program_name);
2244 init_file.SetFile (program_init_file_name, true);
2245 if (!init_file.Exists())
2250 if (!init_file && !m_skip_lldbinit_files)
2251 init_file.SetFile (init_file_path.c_str(), false);
2254 // If the file exists, tell HandleCommand to 'source' it; this will do the actual broadcasting
2255 // of the commands back to any appropriate listener (see CommandObjectSource::Execute for more details).
2257 if (init_file.Exists())
2259 const bool saved_batch = SetBatchCommandMode (true);
2260 CommandInterpreterRunOptions options;
2261 options.SetSilent (true);
2262 options.SetStopOnError (false);
2263 options.SetStopOnContinue (true);
2265 HandleCommandsFromFile (init_file,
2266 nullptr, // Execution context
2269 SetBatchCommandMode (saved_batch);
2273 // nothing to be done if the file doesn't exist
2274 result.SetStatus(eReturnStatusSuccessFinishNoResult);
2279 CommandInterpreter::GetCommandPrefix()
2281 const char * prefix = GetDebugger().GetIOHandlerCommandPrefix();
2282 return prefix == NULL ? "" : prefix;
2286 CommandInterpreter::GetPlatform (bool prefer_target_platform)
2288 PlatformSP platform_sp;
2289 if (prefer_target_platform)
2291 ExecutionContext exe_ctx(GetExecutionContext());
2292 Target *target = exe_ctx.GetTargetPtr();
2294 platform_sp = target->GetPlatform();
2298 platform_sp = m_debugger.GetPlatformList().GetSelectedPlatform();
2303 CommandInterpreter::HandleCommands (const StringList &commands,
2304 ExecutionContext *override_context,
2305 CommandInterpreterRunOptions &options,
2306 CommandReturnObject &result)
2308 size_t num_lines = commands.GetSize();
2310 // If we are going to continue past a "continue" then we need to run the commands synchronously.
2311 // Make sure you reset this value anywhere you return from the function.
2313 bool old_async_execution = m_debugger.GetAsyncExecution();
2315 // If we've been given an execution context, set it at the start, but don't keep resetting it or we will
2316 // cause series of commands that change the context, then do an operation that relies on that context to fail.
2318 if (override_context != nullptr)
2319 UpdateExecutionContext (override_context);
2321 if (!options.GetStopOnContinue())
2323 m_debugger.SetAsyncExecution (false);
2326 for (size_t idx = 0; idx < num_lines; idx++)
2328 const char *cmd = commands.GetStringAtIndex(idx);
2332 if (options.GetEchoCommands())
2334 result.AppendMessageWithFormat ("%s %s\n",
2335 m_debugger.GetPrompt(),
2339 CommandReturnObject tmp_result;
2340 // If override_context is not NULL, pass no_context_switching = true for
2341 // HandleCommand() since we updated our context already.
2343 // We might call into a regex or alias command, in which case the add_to_history will get lost. This
2344 // m_command_source_depth dingus is the way we turn off adding to the history in that case, so set it up here.
2345 if (!options.GetAddToHistory())
2346 m_command_source_depth++;
2347 bool success = HandleCommand(cmd, options.m_add_to_history, tmp_result,
2348 nullptr, /* override_context */
2349 true, /* repeat_on_empty_command */
2350 override_context != nullptr /* no_context_switching */);
2351 if (!options.GetAddToHistory())
2352 m_command_source_depth--;
2354 if (options.GetPrintResults())
2356 if (tmp_result.Succeeded())
2357 result.AppendMessageWithFormat("%s", tmp_result.GetOutputData());
2360 if (!success || !tmp_result.Succeeded())
2362 const char *error_msg = tmp_result.GetErrorData();
2363 if (error_msg == nullptr || error_msg[0] == '\0')
2364 error_msg = "<unknown error>.\n";
2365 if (options.GetStopOnError())
2367 result.AppendErrorWithFormat("Aborting reading of commands after command #%" PRIu64 ": '%s' failed with %s",
2368 (uint64_t)idx, cmd, error_msg);
2369 result.SetStatus (eReturnStatusFailed);
2370 m_debugger.SetAsyncExecution (old_async_execution);
2373 else if (options.GetPrintResults())
2375 result.AppendMessageWithFormat ("Command #%" PRIu64 " '%s' failed with %s",
2382 if (result.GetImmediateOutputStream())
2383 result.GetImmediateOutputStream()->Flush();
2385 if (result.GetImmediateErrorStream())
2386 result.GetImmediateErrorStream()->Flush();
2388 // N.B. Can't depend on DidChangeProcessState, because the state coming into the command execution
2389 // could be running (for instance in Breakpoint Commands.
2390 // So we check the return value to see if it is has running in it.
2391 if ((tmp_result.GetStatus() == eReturnStatusSuccessContinuingNoResult)
2392 || (tmp_result.GetStatus() == eReturnStatusSuccessContinuingResult))
2394 if (options.GetStopOnContinue())
2396 // If we caused the target to proceed, and we're going to stop in that case, set the
2397 // status in our real result before returning. This is an error if the continue was not the
2398 // last command in the set of commands to be run.
2399 if (idx != num_lines - 1)
2400 result.AppendErrorWithFormat("Aborting reading of commands after command #%" PRIu64 ": '%s' continued the target.\n",
2401 (uint64_t)idx + 1, cmd);
2403 result.AppendMessageWithFormat("Command #%" PRIu64 " '%s' continued the target.\n", (uint64_t)idx + 1, cmd);
2405 result.SetStatus(tmp_result.GetStatus());
2406 m_debugger.SetAsyncExecution (old_async_execution);
2412 // Also check for "stop on crash here:
2413 bool should_stop = false;
2414 if (tmp_result.GetDidChangeProcessState() && options.GetStopOnCrash())
2416 TargetSP target_sp (m_debugger.GetTargetList().GetSelectedTarget());
2419 ProcessSP process_sp (target_sp->GetProcessSP());
2422 for (ThreadSP thread_sp : process_sp->GetThreadList().Threads())
2424 StopReason reason = thread_sp->GetStopReason();
2425 if (reason == eStopReasonSignal || reason == eStopReasonException || reason == eStopReasonInstrumentation)
2435 if (idx != num_lines - 1)
2436 result.AppendErrorWithFormat("Aborting reading of commands after command #%" PRIu64 ": '%s' stopped with a signal or exception.\n",
2437 (uint64_t)idx + 1, cmd);
2439 result.AppendMessageWithFormat("Command #%" PRIu64 " '%s' stopped with a signal or exception.\n", (uint64_t)idx + 1, cmd);
2441 result.SetStatus(tmp_result.GetStatus());
2442 m_debugger.SetAsyncExecution (old_async_execution);
2450 result.SetStatus (eReturnStatusSuccessFinishResult);
2451 m_debugger.SetAsyncExecution (old_async_execution);
2456 // Make flags that we can pass into the IOHandler so our delegates can do the right thing
2458 eHandleCommandFlagStopOnContinue = (1u << 0),
2459 eHandleCommandFlagStopOnError = (1u << 1),
2460 eHandleCommandFlagEchoCommand = (1u << 2),
2461 eHandleCommandFlagPrintResult = (1u << 3),
2462 eHandleCommandFlagStopOnCrash = (1u << 4)
2466 CommandInterpreter::HandleCommandsFromFile (FileSpec &cmd_file,
2467 ExecutionContext *context,
2468 CommandInterpreterRunOptions &options,
2469 CommandReturnObject &result)
2471 if (cmd_file.Exists())
2473 StreamFileSP input_file_sp (new StreamFile());
2475 std::string cmd_file_path = cmd_file.GetPath();
2476 Error error = input_file_sp->GetFile().Open(cmd_file_path.c_str(), File::eOpenOptionRead);
2478 if (error.Success())
2480 Debugger &debugger = GetDebugger();
2484 if (options.m_stop_on_continue == eLazyBoolCalculate)
2486 if (m_command_source_flags.empty())
2488 // Stop on continue by default
2489 flags |= eHandleCommandFlagStopOnContinue;
2491 else if (m_command_source_flags.back() & eHandleCommandFlagStopOnContinue)
2493 flags |= eHandleCommandFlagStopOnContinue;
2496 else if (options.m_stop_on_continue == eLazyBoolYes)
2498 flags |= eHandleCommandFlagStopOnContinue;
2501 if (options.m_stop_on_error == eLazyBoolCalculate)
2503 if (m_command_source_flags.empty())
2505 if (GetStopCmdSourceOnError())
2506 flags |= eHandleCommandFlagStopOnError;
2508 else if (m_command_source_flags.back() & eHandleCommandFlagStopOnError)
2510 flags |= eHandleCommandFlagStopOnError;
2513 else if (options.m_stop_on_error == eLazyBoolYes)
2515 flags |= eHandleCommandFlagStopOnError;
2518 if (options.GetStopOnCrash())
2520 if (m_command_source_flags.empty())
2522 // Echo command by default
2523 flags |= eHandleCommandFlagStopOnCrash;
2525 else if (m_command_source_flags.back() & eHandleCommandFlagStopOnCrash)
2527 flags |= eHandleCommandFlagStopOnCrash;
2531 if (options.m_echo_commands == eLazyBoolCalculate)
2533 if (m_command_source_flags.empty())
2535 // Echo command by default
2536 flags |= eHandleCommandFlagEchoCommand;
2538 else if (m_command_source_flags.back() & eHandleCommandFlagEchoCommand)
2540 flags |= eHandleCommandFlagEchoCommand;
2543 else if (options.m_echo_commands == eLazyBoolYes)
2545 flags |= eHandleCommandFlagEchoCommand;
2548 if (options.m_print_results == eLazyBoolCalculate)
2550 if (m_command_source_flags.empty())
2552 // Print output by default
2553 flags |= eHandleCommandFlagPrintResult;
2555 else if (m_command_source_flags.back() & eHandleCommandFlagPrintResult)
2557 flags |= eHandleCommandFlagPrintResult;
2560 else if (options.m_print_results == eLazyBoolYes)
2562 flags |= eHandleCommandFlagPrintResult;
2565 if (flags & eHandleCommandFlagPrintResult)
2567 debugger.GetOutputFile()->Printf("Executing commands in '%s'.\n", cmd_file_path.c_str());
2570 // Used for inheriting the right settings when "command source" might have
2571 // nested "command source" commands
2572 lldb::StreamFileSP empty_stream_sp;
2573 m_command_source_flags.push_back(flags);
2574 IOHandlerSP io_handler_sp (new IOHandlerEditline (debugger,
2575 IOHandler::Type::CommandInterpreter,
2577 empty_stream_sp, // Pass in an empty stream so we inherit the top input reader output stream
2578 empty_stream_sp, // Pass in an empty stream so we inherit the top input reader error stream
2580 nullptr, // Pass in NULL for "editline_name" so no history is saved, or written
2581 debugger.GetPrompt(),
2583 false, // Not multi-line
2584 debugger.GetUseColor(),
2587 const bool old_async_execution = debugger.GetAsyncExecution();
2589 // Set synchronous execution if we are not stopping on continue
2590 if ((flags & eHandleCommandFlagStopOnContinue) == 0)
2591 debugger.SetAsyncExecution (false);
2593 m_command_source_depth++;
2595 debugger.RunIOHandler(io_handler_sp);
2596 if (!m_command_source_flags.empty())
2597 m_command_source_flags.pop_back();
2598 m_command_source_depth--;
2599 result.SetStatus (eReturnStatusSuccessFinishNoResult);
2600 debugger.SetAsyncExecution (old_async_execution);
2604 result.AppendErrorWithFormat ("error: an error occurred read file '%s': %s\n", cmd_file_path.c_str(), error.AsCString());
2605 result.SetStatus (eReturnStatusFailed);
2612 result.AppendErrorWithFormat ("Error reading commands from file %s - file not found.\n",
2613 cmd_file.GetFilename().AsCString("<Unknown>"));
2614 result.SetStatus (eReturnStatusFailed);
2620 CommandInterpreter::GetScriptInterpreter(bool can_create)
2622 if (m_script_interpreter_sp)
2623 return m_script_interpreter_sp.get();
2628 lldb::ScriptLanguage script_lang = GetDebugger().GetScriptLanguage();
2629 m_script_interpreter_sp = PluginManager::GetScriptInterpreterForLanguage(script_lang, *this);
2630 return m_script_interpreter_sp.get();
2634 CommandInterpreter::GetSynchronous ()
2636 return m_synchronous_execution;
2640 CommandInterpreter::SetSynchronous (bool value)
2642 m_synchronous_execution = value;
2646 CommandInterpreter::OutputFormattedHelpText (Stream &strm,
2648 const char *help_text)
2650 const uint32_t max_columns = m_debugger.GetTerminalWidth();
2654 size_t prefix_width = strlen(prefix);
2655 size_t line_width_max = max_columns - prefix_width;
2656 const char *help_text_end = help_text + strlen(help_text);
2657 const char *line_start = help_text;
2658 if (line_width_max < 16)
2659 line_width_max = help_text_end - help_text + prefix_width;
2661 strm.IndentMore (prefix_width);
2662 while (line_start < help_text_end)
2664 // Break each line at the first newline or last space/tab before
2665 // the maximum number of characters that fit on a line. Lines with no
2666 // natural break are left unbroken to wrap.
2667 const char *line_end = help_text_end;
2668 const char *line_scan = line_start;
2669 const char *line_scan_end = help_text_end;
2670 while (line_scan < line_scan_end)
2672 char next = *line_scan;
2673 if (next == '\t' || next == ' ')
2675 line_end = line_scan;
2676 line_scan_end = line_start + line_width_max;
2678 else if (next == '\n' || next == '\0')
2680 line_end = line_scan;
2686 // Prefix the first line, indent subsequent lines to line up
2687 if (line_start == help_text)
2688 strm.Write (prefix, prefix_width);
2691 strm.Write (line_start, line_end - line_start);
2694 // When a line breaks at whitespace consume it before continuing
2695 line_start = line_end;
2696 char next = *line_start;
2699 else while (next == ' ' || next == '\t')
2700 next = *(++line_start);
2702 strm.IndentLess (prefix_width);
2706 CommandInterpreter::OutputFormattedHelpText (Stream &strm,
2707 const char *word_text,
2708 const char *separator,
2709 const char *help_text,
2710 size_t max_word_len)
2712 StreamString prefix_stream;
2713 prefix_stream.Printf (" %-*s %s ", (int)max_word_len, word_text, separator);
2714 OutputFormattedHelpText (strm, prefix_stream.GetData(), help_text);
2718 CommandInterpreter::OutputHelpText (Stream &strm,
2719 const char *word_text,
2720 const char *separator,
2721 const char *help_text,
2722 uint32_t max_word_len)
2724 int indent_size = max_word_len + strlen (separator) + 2;
2726 strm.IndentMore (indent_size);
2728 StreamString text_strm;
2729 text_strm.Printf ("%-*s %s %s", max_word_len, word_text, separator, help_text);
2731 const uint32_t max_columns = m_debugger.GetTerminalWidth();
2733 size_t len = text_strm.GetSize();
2734 const char *text = text_strm.GetData();
2736 uint32_t chars_left = max_columns;
2738 for (uint32_t i = 0; i < len; i++)
2740 if ((text[i] == ' ' && ::strchr((text+i+1), ' ') && chars_left < ::strchr((text+i+1), ' ')-(text+i)) || text[i] == '\n')
2742 chars_left = max_columns - indent_size;
2748 strm.PutChar(text[i]);
2755 strm.IndentLess(indent_size);
2759 CommandInterpreter::FindCommandsForApropos (const char *search_word,
2760 StringList &commands_found,
2761 StringList &commands_help,
2762 CommandObject::CommandMap &command_map)
2764 CommandObject::CommandMap::const_iterator pos;
2766 for (pos = command_map.begin(); pos != command_map.end(); ++pos)
2768 const char *command_name = pos->first.c_str();
2769 CommandObject *cmd_obj = pos->second.get();
2771 const bool search_short_help = true;
2772 const bool search_long_help = false;
2773 const bool search_syntax = false;
2774 const bool search_options = false;
2775 if (strcasestr(command_name, search_word) ||
2776 cmd_obj->HelpTextContainsWord (search_word,
2782 commands_found.AppendString (cmd_obj->GetCommandName());
2783 commands_help.AppendString (cmd_obj->GetHelp());
2786 if (cmd_obj->IsMultiwordObject())
2788 CommandObjectMultiword *cmd_multiword = cmd_obj->GetAsMultiwordCommand();
2789 FindCommandsForApropos(search_word,
2792 cmd_multiword->GetSubcommandDictionary());
2799 CommandInterpreter::FindCommandsForApropos (const char *search_word,
2800 StringList &commands_found,
2801 StringList &commands_help,
2802 bool search_builtin_commands,
2803 bool search_user_commands,
2804 bool search_alias_commands)
2806 CommandObject::CommandMap::const_iterator pos;
2808 if (search_builtin_commands)
2809 FindCommandsForApropos(search_word, commands_found, commands_help, m_command_dict);
2811 if (search_user_commands)
2812 FindCommandsForApropos(search_word, commands_found, commands_help, m_user_dict);
2814 if (search_alias_commands)
2815 FindCommandsForApropos(search_word, commands_found, commands_help, m_alias_dict);
2819 CommandInterpreter::UpdateExecutionContext (ExecutionContext *override_context)
2821 if (override_context != nullptr)
2823 m_exe_ctx_ref = *override_context;
2827 const bool adopt_selected = true;
2828 m_exe_ctx_ref.SetTargetPtr (m_debugger.GetSelectedTarget().get(), adopt_selected);
2834 CommandInterpreter::GetProcessOutput ()
2836 // The process has stuff waiting for stderr; get it and write it out to the appropriate place.
2837 char stdio_buffer[1024];
2839 size_t total_bytes = 0;
2841 TargetSP target_sp (m_debugger.GetTargetList().GetSelectedTarget());
2844 ProcessSP process_sp (target_sp->GetProcessSP());
2847 while ((len = process_sp->GetSTDOUT (stdio_buffer, sizeof (stdio_buffer), error)) > 0)
2849 size_t bytes_written = len;
2850 m_debugger.GetOutputFile()->Write (stdio_buffer, bytes_written);
2853 while ((len = process_sp->GetSTDERR (stdio_buffer, sizeof (stdio_buffer), error)) > 0)
2855 size_t bytes_written = len;
2856 m_debugger.GetErrorFile()->Write (stdio_buffer, bytes_written);
2865 CommandInterpreter::IOHandlerInputComplete (IOHandler &io_handler, std::string &line)
2867 const bool is_interactive = io_handler.GetIsInteractive();
2868 if (is_interactive == false)
2870 // When we are not interactive, don't execute blank lines. This will happen
2871 // sourcing a commands file. We don't want blank lines to repeat the previous
2872 // command and cause any errors to occur (like redefining an alias, get an error
2873 // and stop parsing the commands file).
2877 // When using a non-interactive file handle (like when sourcing commands from a file)
2878 // we need to echo the command out so we don't just see the command output and no
2880 if (io_handler.GetFlags().Test(eHandleCommandFlagEchoCommand))
2881 io_handler.GetOutputStreamFile()->Printf("%s%s\n", io_handler.GetPrompt(), line.c_str());
2884 lldb_private::CommandReturnObject result;
2885 HandleCommand(line.c_str(), eLazyBoolCalculate, result);
2887 // Now emit the command output text from the command we just executed
2888 if (io_handler.GetFlags().Test(eHandleCommandFlagPrintResult))
2890 // Display any STDOUT/STDERR _prior_ to emitting the command result text
2891 GetProcessOutput ();
2893 if (!result.GetImmediateOutputStream())
2895 const char *output = result.GetOutputData();
2896 if (output && output[0])
2897 io_handler.GetOutputStreamFile()->PutCString(output);
2900 // Now emit the command error text from the command we just executed
2901 if (!result.GetImmediateErrorStream())
2903 const char *error = result.GetErrorData();
2904 if (error && error[0])
2905 io_handler.GetErrorStreamFile()->PutCString(error);
2909 switch (result.GetStatus())
2911 case eReturnStatusInvalid:
2912 case eReturnStatusSuccessFinishNoResult:
2913 case eReturnStatusSuccessFinishResult:
2914 case eReturnStatusStarted:
2917 case eReturnStatusSuccessContinuingNoResult:
2918 case eReturnStatusSuccessContinuingResult:
2919 if (io_handler.GetFlags().Test(eHandleCommandFlagStopOnContinue))
2920 io_handler.SetIsDone(true);
2923 case eReturnStatusFailed:
2925 if (io_handler.GetFlags().Test(eHandleCommandFlagStopOnError))
2926 io_handler.SetIsDone(true);
2929 case eReturnStatusQuit:
2930 m_quit_requested = true;
2931 io_handler.SetIsDone(true);
2935 // Finally, if we're going to stop on crash, check that here:
2936 if (!m_quit_requested
2937 && result.GetDidChangeProcessState()
2938 && io_handler.GetFlags().Test(eHandleCommandFlagStopOnCrash))
2940 bool should_stop = false;
2941 TargetSP target_sp (m_debugger.GetTargetList().GetSelectedTarget());
2944 ProcessSP process_sp (target_sp->GetProcessSP());
2947 for (ThreadSP thread_sp : process_sp->GetThreadList().Threads())
2949 StopReason reason = thread_sp->GetStopReason();
2950 if ((reason == eStopReasonSignal
2951 || reason == eStopReasonException
2952 || reason == eStopReasonInstrumentation)
2953 && !result.GetAbnormalStopWasExpected())
2963 io_handler.SetIsDone(true);
2964 m_stopped_for_crash = true;
2970 CommandInterpreter::IOHandlerInterrupt (IOHandler &io_handler)
2972 ExecutionContext exe_ctx (GetExecutionContext());
2973 Process *process = exe_ctx.GetProcessPtr();
2977 StateType state = process->GetState();
2978 if (StateIsRunningState(state))
2981 return true; // Don't do any updating when we are running
2985 ScriptInterpreter *script_interpreter = GetScriptInterpreter (false);
2986 if (script_interpreter)
2988 if (script_interpreter->Interrupt())
2995 CommandInterpreter::GetLLDBCommandsFromIOHandler (const char *prompt,
2996 IOHandlerDelegate &delegate,
2997 bool asynchronously,
3000 Debugger &debugger = GetDebugger();
3001 IOHandlerSP io_handler_sp (new IOHandlerEditline (debugger,
3002 IOHandler::Type::CommandList,
3003 "lldb", // Name of input reader for history
3005 NULL, // Continuation prompt
3006 true, // Get multiple lines
3007 debugger.GetUseColor(),
3008 0, // Don't show line numbers
3009 delegate)); // IOHandlerDelegate
3013 io_handler_sp->SetUserData (baton);
3015 debugger.PushIOHandler(io_handler_sp);
3017 debugger.RunIOHandler(io_handler_sp);
3024 CommandInterpreter::GetPythonCommandsFromIOHandler (const char *prompt,
3025 IOHandlerDelegate &delegate,
3026 bool asynchronously,
3029 Debugger &debugger = GetDebugger();
3030 IOHandlerSP io_handler_sp (new IOHandlerEditline (debugger,
3031 IOHandler::Type::PythonCode,
3032 "lldb-python", // Name of input reader for history
3034 NULL, // Continuation prompt
3035 true, // Get multiple lines
3036 debugger.GetUseColor(),
3037 0, // Don't show line numbers
3038 delegate)); // IOHandlerDelegate
3042 io_handler_sp->SetUserData (baton);
3044 debugger.PushIOHandler(io_handler_sp);
3046 debugger.RunIOHandler(io_handler_sp);
3052 CommandInterpreter::IsActive ()
3054 return m_debugger.IsTopIOHandler (m_command_io_handler_sp);
3058 CommandInterpreter::GetIOHandler(bool force_create, CommandInterpreterRunOptions *options)
3060 // Always re-create the IOHandlerEditline in case the input
3061 // changed. The old instance might have had a non-interactive
3062 // input and now it does or vice versa.
3063 if (force_create || !m_command_io_handler_sp)
3065 // Always re-create the IOHandlerEditline in case the input
3066 // changed. The old instance might have had a non-interactive
3067 // input and now it does or vice versa.
3072 if (options->m_stop_on_continue == eLazyBoolYes)
3073 flags |= eHandleCommandFlagStopOnContinue;
3074 if (options->m_stop_on_error == eLazyBoolYes)
3075 flags |= eHandleCommandFlagStopOnError;
3076 if (options->m_stop_on_crash == eLazyBoolYes)
3077 flags |= eHandleCommandFlagStopOnCrash;
3078 if (options->m_echo_commands != eLazyBoolNo)
3079 flags |= eHandleCommandFlagEchoCommand;
3080 if (options->m_print_results != eLazyBoolNo)
3081 flags |= eHandleCommandFlagPrintResult;
3085 flags = eHandleCommandFlagEchoCommand | eHandleCommandFlagPrintResult;
3088 m_command_io_handler_sp.reset(new IOHandlerEditline (m_debugger,
3089 IOHandler::Type::CommandInterpreter,
3090 m_debugger.GetInputFile(),
3091 m_debugger.GetOutputFile(),
3092 m_debugger.GetErrorFile(),
3095 m_debugger.GetPrompt(),
3096 NULL, // Continuation prompt
3097 false, // Don't enable multiple line input, just single line commands
3098 m_debugger.GetUseColor(),
3099 0, // Don't show line numbers
3102 return m_command_io_handler_sp;
3106 CommandInterpreter::RunCommandInterpreter(bool auto_handle_events,
3108 CommandInterpreterRunOptions &options)
3110 // Always re-create the command interpreter when we run it in case
3111 // any file handles have changed.
3112 bool force_create = true;
3113 m_debugger.PushIOHandler(GetIOHandler(force_create, &options));
3114 m_stopped_for_crash = false;
3116 if (auto_handle_events)
3117 m_debugger.StartEventHandlerThread();
3121 m_debugger.StartIOHandlerThread();
3125 m_debugger.ExecuteIOHandlers();
3127 if (auto_handle_events)
3128 m_debugger.StopEventHandlerThread();
3134 CommandInterpreter::ResolveCommandImpl(std::string &command_line, CommandReturnObject &result)
3136 std::string scratch_command(command_line); // working copy so we don't modify command_line unless we succeed
3137 CommandObject *cmd_obj = nullptr;
3138 StreamString revised_command_line;
3139 bool wants_raw_input = false;
3140 size_t actual_cmd_name_len = 0;
3141 std::string next_word;
3146 char quote_char = '\0';
3148 ExtractCommand(scratch_command, next_word, suffix, quote_char);
3149 if (cmd_obj == nullptr)
3151 std::string full_name;
3152 bool is_alias = GetAliasFullName(next_word.c_str(), full_name);
3153 cmd_obj = GetCommandObject(next_word.c_str(), &matches);
3154 bool is_real_command = (is_alias == false) || (cmd_obj != nullptr && cmd_obj->IsAlias() == false);
3155 if (!is_real_command)
3158 std::string alias_result;
3159 cmd_obj = BuildAliasResult(full_name.c_str(), scratch_command, alias_result, result);
3160 revised_command_line.Printf("%s", alias_result.c_str());
3163 wants_raw_input = cmd_obj->WantsRawCommandString();
3164 actual_cmd_name_len = strlen(cmd_obj->GetCommandName());
3170 cmd_obj = GetCommandObject(next_word.c_str(), &matches);
3173 actual_cmd_name_len += strlen(cmd_obj->GetCommandName());
3174 revised_command_line.Printf("%s", cmd_obj->GetCommandName());
3175 wants_raw_input = cmd_obj->WantsRawCommandString();
3179 revised_command_line.Printf ("%s", next_word.c_str());
3185 if (cmd_obj->IsMultiwordObject ())
3187 CommandObject *sub_cmd_obj = cmd_obj->GetSubcommandObject(next_word.c_str());
3190 // The subcommand's name includes the parent command's name,
3191 // so restart rather than append to the revised_command_line.
3192 actual_cmd_name_len = strlen(sub_cmd_obj->GetCommandName()) + 1;
3193 revised_command_line.Clear();
3194 revised_command_line.Printf("%s", sub_cmd_obj->GetCommandName());
3195 cmd_obj = sub_cmd_obj;
3196 wants_raw_input = cmd_obj->WantsRawCommandString();
3201 revised_command_line.Printf(" %c%s%s%c", quote_char, next_word.c_str(), suffix.c_str(), quote_char);
3203 revised_command_line.Printf(" %s%s", next_word.c_str(), suffix.c_str());
3210 revised_command_line.Printf(" %c%s%s%c", quote_char, next_word.c_str(), suffix.c_str(), quote_char);
3212 revised_command_line.Printf(" %s%s", next_word.c_str(), suffix.c_str());
3217 if (cmd_obj == nullptr)
3219 const size_t num_matches = matches.GetSize();
3220 if (matches.GetSize() > 1) {
3221 StreamString error_msg;
3222 error_msg.Printf("Ambiguous command '%s'. Possible matches:\n", next_word.c_str());
3224 for (uint32_t i = 0; i < num_matches; ++i) {
3225 error_msg.Printf("\t%s\n", matches.GetStringAtIndex(i));
3227 result.AppendRawError(error_msg.GetString().c_str());
3229 // We didn't have only one match, otherwise we wouldn't get here.
3230 assert(num_matches == 0);
3231 result.AppendErrorWithFormat("'%s' is not a valid command.\n", next_word.c_str());
3233 result.SetStatus(eReturnStatusFailed);
3237 if (cmd_obj->IsMultiwordObject())
3239 if (!suffix.empty())
3241 result.AppendErrorWithFormat("command '%s' did not recognize '%s%s%s' as valid (subcommand might be invalid).\n",
3242 cmd_obj->GetCommandName(),
3243 next_word.empty() ? "" : next_word.c_str(),
3244 next_word.empty() ? " -- " : " ",
3246 result.SetStatus(eReturnStatusFailed);
3252 // If we found a normal command, we are done
3254 if (!suffix.empty())
3259 // GDB format suffixes
3261 Options *command_options = cmd_obj->GetOptions();
3262 if (command_options && command_options->SupportsLongOption("gdb-format"))
3264 std::string gdb_format_option("--gdb-format=");
3265 gdb_format_option += (suffix.c_str() + 1);
3267 bool inserted = false;
3268 std::string &cmd = revised_command_line.GetString();
3269 size_t arg_terminator_idx = FindArgumentTerminator(cmd);
3270 if (arg_terminator_idx != std::string::npos)
3272 // Insert the gdb format option before the "--" that terminates options
3273 gdb_format_option.append(1,' ');
3274 cmd.insert(arg_terminator_idx, gdb_format_option);
3279 revised_command_line.Printf(" %s", gdb_format_option.c_str());
3281 if (wants_raw_input && FindArgumentTerminator(cmd) == std::string::npos)
3282 revised_command_line.PutCString(" --");
3286 result.AppendErrorWithFormat("the '%s' command doesn't support the --gdb-format option\n",
3287 cmd_obj->GetCommandName());
3288 result.SetStatus(eReturnStatusFailed);
3295 result.AppendErrorWithFormat("unknown command shorthand suffix: '%s'\n",
3297 result.SetStatus(eReturnStatusFailed);
3302 if (scratch_command.empty())
3306 if (!scratch_command.empty())
3307 revised_command_line.Printf(" %s", scratch_command.c_str());
3309 if (cmd_obj != NULL)
3310 command_line = revised_command_line.GetData();