1 //===-- CommandInterpreter.cpp ----------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include <stdlib.h> 10 #include <string> 11 #include <vector> 12 13 #include "CommandObjectScript.h" 14 #include "lldb/Interpreter/CommandObjectRegexCommand.h" 15 16 #include "Commands/CommandObjectApropos.h" 17 #include "Commands/CommandObjectBreakpoint.h" 18 #include "Commands/CommandObjectBugreport.h" 19 #include "Commands/CommandObjectCommands.h" 20 #include "Commands/CommandObjectDisassemble.h" 21 #include "Commands/CommandObjectExpression.h" 22 #include "Commands/CommandObjectFrame.h" 23 #include "Commands/CommandObjectGUI.h" 24 #include "Commands/CommandObjectHelp.h" 25 #include "Commands/CommandObjectLanguage.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/CommandObjectReproducer.h" 34 #include "Commands/CommandObjectSettings.h" 35 #include "Commands/CommandObjectSource.h" 36 #include "Commands/CommandObjectStats.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 43 #include "lldb/Core/Debugger.h" 44 #include "lldb/Core/PluginManager.h" 45 #include "lldb/Core/StreamFile.h" 46 #include "lldb/Utility/Log.h" 47 #include "lldb/Utility/State.h" 48 #include "lldb/Utility/Stream.h" 49 #include "lldb/Utility/Timer.h" 50 51 #ifndef LLDB_DISABLE_LIBEDIT 52 #include "lldb/Host/Editline.h" 53 #endif 54 #include "lldb/Host/Host.h" 55 #include "lldb/Host/HostInfo.h" 56 57 #include "lldb/Interpreter/CommandCompletions.h" 58 #include "lldb/Interpreter/CommandInterpreter.h" 59 #include "lldb/Interpreter/CommandReturnObject.h" 60 #include "lldb/Interpreter/OptionValueProperties.h" 61 #include "lldb/Interpreter/Options.h" 62 #include "lldb/Interpreter/Property.h" 63 #include "lldb/Utility/Args.h" 64 65 #include "lldb/Target/Process.h" 66 #include "lldb/Target/TargetList.h" 67 #include "lldb/Target/Thread.h" 68 69 #include "llvm/ADT/STLExtras.h" 70 #include "llvm/ADT/SmallString.h" 71 #include "llvm/Support/Path.h" 72 #include "llvm/Support/PrettyStackTrace.h" 73 74 using namespace lldb; 75 using namespace lldb_private; 76 77 static const char *k_white_space = " \t\v"; 78 79 static constexpr bool NoGlobalSetting = true; 80 static constexpr uintptr_t DefaultValueTrue = true; 81 static constexpr uintptr_t DefaultValueFalse = false; 82 static constexpr const char *NoCStrDefault = nullptr; 83 84 static constexpr PropertyDefinition g_properties[] = { 85 {"expand-regex-aliases", OptionValue::eTypeBoolean, NoGlobalSetting, 86 DefaultValueFalse, NoCStrDefault, {}, 87 "If true, regular expression alias commands will show the " 88 "expanded command that will be executed. This can be used to " 89 "debug new regular expression alias commands."}, 90 {"prompt-on-quit", OptionValue::eTypeBoolean, NoGlobalSetting, 91 DefaultValueTrue, NoCStrDefault, {}, 92 "If true, LLDB will prompt you before quitting if there are any live " 93 "processes being debugged. If false, LLDB will quit without asking in any " 94 "case."}, 95 {"stop-command-source-on-error", OptionValue::eTypeBoolean, NoGlobalSetting, 96 DefaultValueTrue, NoCStrDefault, {}, 97 "If true, LLDB will stop running a 'command source' " 98 "script upon encountering an error."}, 99 {"space-repl-prompts", OptionValue::eTypeBoolean, NoGlobalSetting, 100 DefaultValueFalse, NoCStrDefault, {}, 101 "If true, blank lines will be printed between between REPL submissions."}, 102 {"echo-commands", OptionValue::eTypeBoolean, NoGlobalSetting, 103 DefaultValueTrue, NoCStrDefault, {}, 104 "If true, commands will be echoed before they are evaluated."}, 105 {"echo-comment-commands", OptionValue::eTypeBoolean, NoGlobalSetting, 106 DefaultValueTrue, NoCStrDefault, {}, 107 "If true, commands will be echoed even if they are pure comment lines."}}; 108 109 enum { 110 ePropertyExpandRegexAliases = 0, 111 ePropertyPromptOnQuit = 1, 112 ePropertyStopCmdSourceOnError = 2, 113 eSpaceReplPrompts = 3, 114 eEchoCommands = 4, 115 eEchoCommentCommands = 5 116 }; 117 118 ConstString &CommandInterpreter::GetStaticBroadcasterClass() { 119 static ConstString class_name("lldb.commandInterpreter"); 120 return class_name; 121 } 122 123 CommandInterpreter::CommandInterpreter(Debugger &debugger, 124 ScriptLanguage script_language, 125 bool synchronous_execution) 126 : Broadcaster(debugger.GetBroadcasterManager(), 127 CommandInterpreter::GetStaticBroadcasterClass().AsCString()), 128 Properties(OptionValuePropertiesSP( 129 new OptionValueProperties(ConstString("interpreter")))), 130 IOHandlerDelegate(IOHandlerDelegate::Completion::LLDBCommand), 131 m_debugger(debugger), m_synchronous_execution(synchronous_execution), 132 m_skip_lldbinit_files(false), m_skip_app_init_files(false), 133 m_script_interpreter_sp(), m_command_io_handler_sp(), m_comment_char('#'), 134 m_batch_command_mode(false), m_truncation_warning(eNoTruncation), 135 m_command_source_depth(0), m_num_errors(0), m_quit_requested(false), 136 m_stopped_for_crash(false) { 137 debugger.SetScriptLanguage(script_language); 138 SetEventName(eBroadcastBitThreadShouldExit, "thread-should-exit"); 139 SetEventName(eBroadcastBitResetPrompt, "reset-prompt"); 140 SetEventName(eBroadcastBitQuitCommandReceived, "quit"); 141 CheckInWithManager(); 142 m_collection_sp->Initialize(g_properties); 143 } 144 145 bool CommandInterpreter::GetExpandRegexAliases() const { 146 const uint32_t idx = ePropertyExpandRegexAliases; 147 return m_collection_sp->GetPropertyAtIndexAsBoolean( 148 nullptr, idx, g_properties[idx].default_uint_value != 0); 149 } 150 151 bool CommandInterpreter::GetPromptOnQuit() const { 152 const uint32_t idx = ePropertyPromptOnQuit; 153 return m_collection_sp->GetPropertyAtIndexAsBoolean( 154 nullptr, idx, g_properties[idx].default_uint_value != 0); 155 } 156 157 void CommandInterpreter::SetPromptOnQuit(bool b) { 158 const uint32_t idx = ePropertyPromptOnQuit; 159 m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, b); 160 } 161 162 bool CommandInterpreter::GetEchoCommands() const { 163 const uint32_t idx = eEchoCommands; 164 return m_collection_sp->GetPropertyAtIndexAsBoolean( 165 nullptr, idx, g_properties[idx].default_uint_value != 0); 166 } 167 168 void CommandInterpreter::SetEchoCommands(bool b) { 169 const uint32_t idx = eEchoCommands; 170 m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, b); 171 } 172 173 bool CommandInterpreter::GetEchoCommentCommands() const { 174 const uint32_t idx = eEchoCommentCommands; 175 return m_collection_sp->GetPropertyAtIndexAsBoolean( 176 nullptr, idx, g_properties[idx].default_uint_value != 0); 177 } 178 179 void CommandInterpreter::SetEchoCommentCommands(bool b) { 180 const uint32_t idx = eEchoCommentCommands; 181 m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, b); 182 } 183 184 void CommandInterpreter::AllowExitCodeOnQuit(bool allow) { 185 m_allow_exit_code = allow; 186 if (!allow) 187 m_quit_exit_code.reset(); 188 } 189 190 bool CommandInterpreter::SetQuitExitCode(int exit_code) { 191 if (!m_allow_exit_code) 192 return false; 193 m_quit_exit_code = exit_code; 194 return true; 195 } 196 197 int CommandInterpreter::GetQuitExitCode(bool &exited) const { 198 exited = m_quit_exit_code.hasValue(); 199 if (exited) 200 return *m_quit_exit_code; 201 return 0; 202 } 203 204 void CommandInterpreter::ResolveCommand(const char *command_line, 205 CommandReturnObject &result) { 206 std::string command = command_line; 207 if (ResolveCommandImpl(command, result) != nullptr) { 208 result.AppendMessageWithFormat("%s", command.c_str()); 209 result.SetStatus(eReturnStatusSuccessFinishResult); 210 } 211 } 212 213 bool CommandInterpreter::GetStopCmdSourceOnError() const { 214 const uint32_t idx = ePropertyStopCmdSourceOnError; 215 return m_collection_sp->GetPropertyAtIndexAsBoolean( 216 nullptr, idx, g_properties[idx].default_uint_value != 0); 217 } 218 219 bool CommandInterpreter::GetSpaceReplPrompts() const { 220 const uint32_t idx = eSpaceReplPrompts; 221 return m_collection_sp->GetPropertyAtIndexAsBoolean( 222 nullptr, idx, g_properties[idx].default_uint_value != 0); 223 } 224 225 void CommandInterpreter::Initialize() { 226 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION); 227 Timer scoped_timer(func_cat, LLVM_PRETTY_FUNCTION); 228 229 CommandReturnObject result; 230 231 LoadCommandDictionary(); 232 233 // An alias arguments vector to reuse - reset it before use... 234 OptionArgVectorSP alias_arguments_vector_sp(new OptionArgVector); 235 236 // Set up some initial aliases. 237 CommandObjectSP cmd_obj_sp = GetCommandSPExact("quit", false); 238 if (cmd_obj_sp) { 239 AddAlias("q", cmd_obj_sp); 240 AddAlias("exit", cmd_obj_sp); 241 } 242 243 cmd_obj_sp = GetCommandSPExact("_regexp-attach", false); 244 if (cmd_obj_sp) 245 AddAlias("attach", cmd_obj_sp)->SetSyntax(cmd_obj_sp->GetSyntax()); 246 247 cmd_obj_sp = GetCommandSPExact("process detach", false); 248 if (cmd_obj_sp) { 249 AddAlias("detach", cmd_obj_sp); 250 } 251 252 cmd_obj_sp = GetCommandSPExact("process continue", false); 253 if (cmd_obj_sp) { 254 AddAlias("c", cmd_obj_sp); 255 AddAlias("continue", cmd_obj_sp); 256 } 257 258 cmd_obj_sp = GetCommandSPExact("_regexp-break", false); 259 if (cmd_obj_sp) 260 AddAlias("b", cmd_obj_sp)->SetSyntax(cmd_obj_sp->GetSyntax()); 261 262 cmd_obj_sp = GetCommandSPExact("_regexp-tbreak", false); 263 if (cmd_obj_sp) 264 AddAlias("tbreak", cmd_obj_sp)->SetSyntax(cmd_obj_sp->GetSyntax()); 265 266 cmd_obj_sp = GetCommandSPExact("thread step-inst", false); 267 if (cmd_obj_sp) { 268 AddAlias("stepi", cmd_obj_sp); 269 AddAlias("si", cmd_obj_sp); 270 } 271 272 cmd_obj_sp = GetCommandSPExact("thread step-inst-over", false); 273 if (cmd_obj_sp) { 274 AddAlias("nexti", cmd_obj_sp); 275 AddAlias("ni", cmd_obj_sp); 276 } 277 278 cmd_obj_sp = GetCommandSPExact("thread step-in", false); 279 if (cmd_obj_sp) { 280 AddAlias("s", cmd_obj_sp); 281 AddAlias("step", cmd_obj_sp); 282 CommandAlias *sif_alias = AddAlias( 283 "sif", cmd_obj_sp, "--end-linenumber block --step-in-target %1"); 284 if (sif_alias) { 285 sif_alias->SetHelp("Step through the current block, stopping if you step " 286 "directly into a function whose name matches the " 287 "TargetFunctionName."); 288 sif_alias->SetSyntax("sif <TargetFunctionName>"); 289 } 290 } 291 292 cmd_obj_sp = GetCommandSPExact("thread step-over", false); 293 if (cmd_obj_sp) { 294 AddAlias("n", cmd_obj_sp); 295 AddAlias("next", cmd_obj_sp); 296 } 297 298 cmd_obj_sp = GetCommandSPExact("thread step-out", false); 299 if (cmd_obj_sp) { 300 AddAlias("finish", cmd_obj_sp); 301 } 302 303 cmd_obj_sp = GetCommandSPExact("frame select", false); 304 if (cmd_obj_sp) { 305 AddAlias("f", cmd_obj_sp); 306 } 307 308 cmd_obj_sp = GetCommandSPExact("thread select", false); 309 if (cmd_obj_sp) { 310 AddAlias("t", cmd_obj_sp); 311 } 312 313 cmd_obj_sp = GetCommandSPExact("_regexp-jump", false); 314 if (cmd_obj_sp) { 315 AddAlias("j", cmd_obj_sp)->SetSyntax(cmd_obj_sp->GetSyntax()); 316 AddAlias("jump", cmd_obj_sp)->SetSyntax(cmd_obj_sp->GetSyntax()); 317 } 318 319 cmd_obj_sp = GetCommandSPExact("_regexp-list", false); 320 if (cmd_obj_sp) { 321 AddAlias("l", cmd_obj_sp)->SetSyntax(cmd_obj_sp->GetSyntax()); 322 AddAlias("list", cmd_obj_sp)->SetSyntax(cmd_obj_sp->GetSyntax()); 323 } 324 325 cmd_obj_sp = GetCommandSPExact("_regexp-env", false); 326 if (cmd_obj_sp) 327 AddAlias("env", cmd_obj_sp)->SetSyntax(cmd_obj_sp->GetSyntax()); 328 329 cmd_obj_sp = GetCommandSPExact("memory read", false); 330 if (cmd_obj_sp) 331 AddAlias("x", cmd_obj_sp); 332 333 cmd_obj_sp = GetCommandSPExact("_regexp-up", false); 334 if (cmd_obj_sp) 335 AddAlias("up", cmd_obj_sp)->SetSyntax(cmd_obj_sp->GetSyntax()); 336 337 cmd_obj_sp = GetCommandSPExact("_regexp-down", false); 338 if (cmd_obj_sp) 339 AddAlias("down", cmd_obj_sp)->SetSyntax(cmd_obj_sp->GetSyntax()); 340 341 cmd_obj_sp = GetCommandSPExact("_regexp-display", false); 342 if (cmd_obj_sp) 343 AddAlias("display", cmd_obj_sp)->SetSyntax(cmd_obj_sp->GetSyntax()); 344 345 cmd_obj_sp = GetCommandSPExact("disassemble", false); 346 if (cmd_obj_sp) 347 AddAlias("dis", cmd_obj_sp); 348 349 cmd_obj_sp = GetCommandSPExact("disassemble", false); 350 if (cmd_obj_sp) 351 AddAlias("di", cmd_obj_sp); 352 353 cmd_obj_sp = GetCommandSPExact("_regexp-undisplay", false); 354 if (cmd_obj_sp) 355 AddAlias("undisplay", cmd_obj_sp)->SetSyntax(cmd_obj_sp->GetSyntax()); 356 357 cmd_obj_sp = GetCommandSPExact("_regexp-bt", false); 358 if (cmd_obj_sp) 359 AddAlias("bt", cmd_obj_sp)->SetSyntax(cmd_obj_sp->GetSyntax()); 360 361 cmd_obj_sp = GetCommandSPExact("target create", false); 362 if (cmd_obj_sp) 363 AddAlias("file", cmd_obj_sp); 364 365 cmd_obj_sp = GetCommandSPExact("target modules", false); 366 if (cmd_obj_sp) 367 AddAlias("image", cmd_obj_sp); 368 369 alias_arguments_vector_sp.reset(new OptionArgVector); 370 371 cmd_obj_sp = GetCommandSPExact("expression", false); 372 if (cmd_obj_sp) { 373 AddAlias("p", cmd_obj_sp, "--")->SetHelpLong(""); 374 AddAlias("print", cmd_obj_sp, "--")->SetHelpLong(""); 375 AddAlias("call", cmd_obj_sp, "--")->SetHelpLong(""); 376 if (auto po = AddAlias("po", cmd_obj_sp, "-O --")) { 377 po->SetHelp("Evaluate an expression on the current thread. Displays any " 378 "returned value with formatting " 379 "controlled by the type's author."); 380 po->SetHelpLong(""); 381 } 382 AddAlias("parray", cmd_obj_sp, "--element-count %1 --")->SetHelpLong(""); 383 AddAlias("poarray", cmd_obj_sp, 384 "--object-description --element-count %1 --") 385 ->SetHelpLong(""); 386 } 387 388 cmd_obj_sp = GetCommandSPExact("process kill", false); 389 if (cmd_obj_sp) { 390 AddAlias("kill", cmd_obj_sp); 391 } 392 393 cmd_obj_sp = GetCommandSPExact("process launch", false); 394 if (cmd_obj_sp) { 395 alias_arguments_vector_sp.reset(new OptionArgVector); 396 #if defined(__arm__) || defined(__arm64__) || defined(__aarch64__) 397 AddAlias("r", cmd_obj_sp, "--"); 398 AddAlias("run", cmd_obj_sp, "--"); 399 #else 400 #if defined(__APPLE__) 401 std::string shell_option; 402 shell_option.append("--shell-expand-args"); 403 shell_option.append(" true"); 404 shell_option.append(" --"); 405 AddAlias("r", cmd_obj_sp, "--shell-expand-args true --"); 406 AddAlias("run", cmd_obj_sp, "--shell-expand-args true --"); 407 #else 408 StreamString defaultshell; 409 defaultshell.Printf("--shell=%s --", 410 HostInfo::GetDefaultShell().GetPath().c_str()); 411 AddAlias("r", cmd_obj_sp, defaultshell.GetString()); 412 AddAlias("run", cmd_obj_sp, defaultshell.GetString()); 413 #endif 414 #endif 415 } 416 417 cmd_obj_sp = GetCommandSPExact("target symbols add", false); 418 if (cmd_obj_sp) { 419 AddAlias("add-dsym", cmd_obj_sp); 420 } 421 422 cmd_obj_sp = GetCommandSPExact("breakpoint set", false); 423 if (cmd_obj_sp) { 424 AddAlias("rbreak", cmd_obj_sp, "--func-regex %1"); 425 } 426 427 cmd_obj_sp = GetCommandSPExact("frame variable", false); 428 if (cmd_obj_sp) { 429 AddAlias("v", cmd_obj_sp); 430 AddAlias("var", cmd_obj_sp); 431 AddAlias("vo", cmd_obj_sp, "--object-description"); 432 } 433 } 434 435 void CommandInterpreter::Clear() { 436 m_command_io_handler_sp.reset(); 437 438 if (m_script_interpreter_sp) 439 m_script_interpreter_sp->Clear(); 440 } 441 442 const char *CommandInterpreter::ProcessEmbeddedScriptCommands(const char *arg) { 443 // This function has not yet been implemented. 444 445 // Look for any embedded script command 446 // If found, 447 // get interpreter object from the command dictionary, 448 // call execute_one_command on it, 449 // get the results as a string, 450 // substitute that string for current stuff. 451 452 return arg; 453 } 454 455 void CommandInterpreter::LoadCommandDictionary() { 456 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION); 457 Timer scoped_timer(func_cat, LLVM_PRETTY_FUNCTION); 458 459 lldb::ScriptLanguage script_language = m_debugger.GetScriptLanguage(); 460 461 m_command_dict["apropos"] = CommandObjectSP(new CommandObjectApropos(*this)); 462 m_command_dict["breakpoint"] = 463 CommandObjectSP(new CommandObjectMultiwordBreakpoint(*this)); 464 m_command_dict["bugreport"] = 465 CommandObjectSP(new CommandObjectMultiwordBugreport(*this)); 466 m_command_dict["command"] = 467 CommandObjectSP(new CommandObjectMultiwordCommands(*this)); 468 m_command_dict["disassemble"] = 469 CommandObjectSP(new CommandObjectDisassemble(*this)); 470 m_command_dict["expression"] = 471 CommandObjectSP(new CommandObjectExpression(*this)); 472 m_command_dict["frame"] = 473 CommandObjectSP(new CommandObjectMultiwordFrame(*this)); 474 m_command_dict["gui"] = CommandObjectSP(new CommandObjectGUI(*this)); 475 m_command_dict["help"] = CommandObjectSP(new CommandObjectHelp(*this)); 476 m_command_dict["log"] = CommandObjectSP(new CommandObjectLog(*this)); 477 m_command_dict["memory"] = CommandObjectSP(new CommandObjectMemory(*this)); 478 m_command_dict["platform"] = 479 CommandObjectSP(new CommandObjectPlatform(*this)); 480 m_command_dict["plugin"] = CommandObjectSP(new CommandObjectPlugin(*this)); 481 m_command_dict["process"] = 482 CommandObjectSP(new CommandObjectMultiwordProcess(*this)); 483 m_command_dict["quit"] = CommandObjectSP(new CommandObjectQuit(*this)); 484 m_command_dict["register"] = 485 CommandObjectSP(new CommandObjectRegister(*this)); 486 m_command_dict["reproducer"] = 487 CommandObjectSP(new CommandObjectReproducer(*this)); 488 m_command_dict["script"] = 489 CommandObjectSP(new CommandObjectScript(*this, script_language)); 490 m_command_dict["settings"] = 491 CommandObjectSP(new CommandObjectMultiwordSettings(*this)); 492 m_command_dict["source"] = 493 CommandObjectSP(new CommandObjectMultiwordSource(*this)); 494 m_command_dict["statistics"] = CommandObjectSP(new CommandObjectStats(*this)); 495 m_command_dict["target"] = 496 CommandObjectSP(new CommandObjectMultiwordTarget(*this)); 497 m_command_dict["thread"] = 498 CommandObjectSP(new CommandObjectMultiwordThread(*this)); 499 m_command_dict["type"] = CommandObjectSP(new CommandObjectType(*this)); 500 m_command_dict["version"] = CommandObjectSP(new CommandObjectVersion(*this)); 501 m_command_dict["watchpoint"] = 502 CommandObjectSP(new CommandObjectMultiwordWatchpoint(*this)); 503 m_command_dict["language"] = 504 CommandObjectSP(new CommandObjectLanguage(*this)); 505 506 const char *break_regexes[][2] = { 507 {"^(.*[^[:space:]])[[:space:]]*:[[:space:]]*([[:digit:]]+)[[:space:]]*$", 508 "breakpoint set --file '%1' --line %2"}, 509 {"^/([^/]+)/$", "breakpoint set --source-pattern-regexp '%1'"}, 510 {"^([[:digit:]]+)[[:space:]]*$", "breakpoint set --line %1"}, 511 {"^\\*?(0x[[:xdigit:]]+)[[:space:]]*$", "breakpoint set --address %1"}, 512 {"^[\"']?([-+]?\\[.*\\])[\"']?[[:space:]]*$", 513 "breakpoint set --name '%1'"}, 514 {"^(-.*)$", "breakpoint set %1"}, 515 {"^(.*[^[:space:]])`(.*[^[:space:]])[[:space:]]*$", 516 "breakpoint set --name '%2' --shlib '%1'"}, 517 {"^\\&(.*[^[:space:]])[[:space:]]*$", 518 "breakpoint set --name '%1' --skip-prologue=0"}, 519 {"^[\"']?(.*[^[:space:]\"'])[\"']?[[:space:]]*$", 520 "breakpoint set --name '%1'"}}; 521 522 size_t num_regexes = llvm::array_lengthof(break_regexes); 523 524 std::unique_ptr<CommandObjectRegexCommand> break_regex_cmd_ap( 525 new CommandObjectRegexCommand( 526 *this, "_regexp-break", 527 "Set a breakpoint using one of several shorthand formats.", 528 "\n" 529 "_regexp-break <filename>:<linenum>\n" 530 " main.c:12 // Break at line 12 of " 531 "main.c\n\n" 532 "_regexp-break <linenum>\n" 533 " 12 // Break at line 12 of current " 534 "file\n\n" 535 "_regexp-break 0x<address>\n" 536 " 0x1234000 // Break at address " 537 "0x1234000\n\n" 538 "_regexp-break <name>\n" 539 " main // Break in 'main' after the " 540 "prologue\n\n" 541 "_regexp-break &<name>\n" 542 " &main // Break at first instruction " 543 "in 'main'\n\n" 544 "_regexp-break <module>`<name>\n" 545 " libc.so`malloc // Break in 'malloc' from " 546 "'libc.so'\n\n" 547 "_regexp-break /<source-regex>/\n" 548 " /break here/ // Break on source lines in " 549 "current file\n" 550 " // containing text 'break " 551 "here'.\n", 552 2, CommandCompletions::eSymbolCompletion | 553 CommandCompletions::eSourceFileCompletion, 554 false)); 555 556 if (break_regex_cmd_ap.get()) { 557 bool success = true; 558 for (size_t i = 0; i < num_regexes; i++) { 559 success = break_regex_cmd_ap->AddRegexCommand(break_regexes[i][0], 560 break_regexes[i][1]); 561 if (!success) 562 break; 563 } 564 success = 565 break_regex_cmd_ap->AddRegexCommand("^$", "breakpoint list --full"); 566 567 if (success) { 568 CommandObjectSP break_regex_cmd_sp(break_regex_cmd_ap.release()); 569 m_command_dict[break_regex_cmd_sp->GetCommandName()] = break_regex_cmd_sp; 570 } 571 } 572 573 std::unique_ptr<CommandObjectRegexCommand> tbreak_regex_cmd_ap( 574 new CommandObjectRegexCommand( 575 *this, "_regexp-tbreak", 576 "Set a one-shot breakpoint using one of several shorthand formats.", 577 "\n" 578 "_regexp-break <filename>:<linenum>\n" 579 " main.c:12 // Break at line 12 of " 580 "main.c\n\n" 581 "_regexp-break <linenum>\n" 582 " 12 // Break at line 12 of current " 583 "file\n\n" 584 "_regexp-break 0x<address>\n" 585 " 0x1234000 // Break at address " 586 "0x1234000\n\n" 587 "_regexp-break <name>\n" 588 " main // Break in 'main' after the " 589 "prologue\n\n" 590 "_regexp-break &<name>\n" 591 " &main // Break at first instruction " 592 "in 'main'\n\n" 593 "_regexp-break <module>`<name>\n" 594 " libc.so`malloc // Break in 'malloc' from " 595 "'libc.so'\n\n" 596 "_regexp-break /<source-regex>/\n" 597 " /break here/ // Break on source lines in " 598 "current file\n" 599 " // containing text 'break " 600 "here'.\n", 601 2, CommandCompletions::eSymbolCompletion | 602 CommandCompletions::eSourceFileCompletion, 603 false)); 604 605 if (tbreak_regex_cmd_ap.get()) { 606 bool success = true; 607 for (size_t i = 0; i < num_regexes; i++) { 608 // If you add a resultant command string longer than 1024 characters be 609 // sure to increase the size of this buffer. 610 char buffer[1024]; 611 int num_printed = 612 snprintf(buffer, 1024, "%s %s", break_regexes[i][1], "-o 1"); 613 lldbassert(num_printed < 1024); 614 UNUSED_IF_ASSERT_DISABLED(num_printed); 615 success = 616 tbreak_regex_cmd_ap->AddRegexCommand(break_regexes[i][0], buffer); 617 if (!success) 618 break; 619 } 620 success = 621 tbreak_regex_cmd_ap->AddRegexCommand("^$", "breakpoint list --full"); 622 623 if (success) { 624 CommandObjectSP tbreak_regex_cmd_sp(tbreak_regex_cmd_ap.release()); 625 m_command_dict[tbreak_regex_cmd_sp->GetCommandName()] = 626 tbreak_regex_cmd_sp; 627 } 628 } 629 630 std::unique_ptr<CommandObjectRegexCommand> attach_regex_cmd_ap( 631 new CommandObjectRegexCommand( 632 *this, "_regexp-attach", "Attach to process by ID or name.", 633 "_regexp-attach <pid> | <process-name>", 2, 0, false)); 634 if (attach_regex_cmd_ap.get()) { 635 if (attach_regex_cmd_ap->AddRegexCommand("^([0-9]+)[[:space:]]*$", 636 "process attach --pid %1") && 637 attach_regex_cmd_ap->AddRegexCommand( 638 "^(-.*|.* -.*)$", "process attach %1") && // Any options that are 639 // specified get passed to 640 // 'process attach' 641 attach_regex_cmd_ap->AddRegexCommand("^(.+)$", 642 "process attach --name '%1'") && 643 attach_regex_cmd_ap->AddRegexCommand("^$", "process attach")) { 644 CommandObjectSP attach_regex_cmd_sp(attach_regex_cmd_ap.release()); 645 m_command_dict[attach_regex_cmd_sp->GetCommandName()] = 646 attach_regex_cmd_sp; 647 } 648 } 649 650 std::unique_ptr<CommandObjectRegexCommand> down_regex_cmd_ap( 651 new CommandObjectRegexCommand(*this, "_regexp-down", 652 "Select a newer stack frame. Defaults to " 653 "moving one frame, a numeric argument can " 654 "specify an arbitrary number.", 655 "_regexp-down [<count>]", 2, 0, false)); 656 if (down_regex_cmd_ap.get()) { 657 if (down_regex_cmd_ap->AddRegexCommand("^$", "frame select -r -1") && 658 down_regex_cmd_ap->AddRegexCommand("^([0-9]+)$", 659 "frame select -r -%1")) { 660 CommandObjectSP down_regex_cmd_sp(down_regex_cmd_ap.release()); 661 m_command_dict[down_regex_cmd_sp->GetCommandName()] = down_regex_cmd_sp; 662 } 663 } 664 665 std::unique_ptr<CommandObjectRegexCommand> up_regex_cmd_ap( 666 new CommandObjectRegexCommand( 667 *this, "_regexp-up", 668 "Select an older stack frame. Defaults to moving one " 669 "frame, a numeric argument can specify an arbitrary number.", 670 "_regexp-up [<count>]", 2, 0, false)); 671 if (up_regex_cmd_ap.get()) { 672 if (up_regex_cmd_ap->AddRegexCommand("^$", "frame select -r 1") && 673 up_regex_cmd_ap->AddRegexCommand("^([0-9]+)$", "frame select -r %1")) { 674 CommandObjectSP up_regex_cmd_sp(up_regex_cmd_ap.release()); 675 m_command_dict[up_regex_cmd_sp->GetCommandName()] = up_regex_cmd_sp; 676 } 677 } 678 679 std::unique_ptr<CommandObjectRegexCommand> display_regex_cmd_ap( 680 new CommandObjectRegexCommand( 681 *this, "_regexp-display", 682 "Evaluate an expression at every stop (see 'help target stop-hook'.)", 683 "_regexp-display expression", 2, 0, false)); 684 if (display_regex_cmd_ap.get()) { 685 if (display_regex_cmd_ap->AddRegexCommand( 686 "^(.+)$", "target stop-hook add -o \"expr -- %1\"")) { 687 CommandObjectSP display_regex_cmd_sp(display_regex_cmd_ap.release()); 688 m_command_dict[display_regex_cmd_sp->GetCommandName()] = 689 display_regex_cmd_sp; 690 } 691 } 692 693 std::unique_ptr<CommandObjectRegexCommand> undisplay_regex_cmd_ap( 694 new CommandObjectRegexCommand( 695 *this, "_regexp-undisplay", "Stop displaying expression at every " 696 "stop (specified by stop-hook index.)", 697 "_regexp-undisplay stop-hook-number", 2, 0, false)); 698 if (undisplay_regex_cmd_ap.get()) { 699 if (undisplay_regex_cmd_ap->AddRegexCommand("^([0-9]+)$", 700 "target stop-hook delete %1")) { 701 CommandObjectSP undisplay_regex_cmd_sp(undisplay_regex_cmd_ap.release()); 702 m_command_dict[undisplay_regex_cmd_sp->GetCommandName()] = 703 undisplay_regex_cmd_sp; 704 } 705 } 706 707 std::unique_ptr<CommandObjectRegexCommand> connect_gdb_remote_cmd_ap( 708 new CommandObjectRegexCommand( 709 *this, "gdb-remote", "Connect to a process via remote GDB server. " 710 "If no host is specifed, localhost is assumed.", 711 "gdb-remote [<hostname>:]<portnum>", 2, 0, false)); 712 if (connect_gdb_remote_cmd_ap.get()) { 713 if (connect_gdb_remote_cmd_ap->AddRegexCommand( 714 "^([^:]+|\\[[0-9a-fA-F:]+.*\\]):([0-9]+)$", 715 "process connect --plugin gdb-remote connect://%1:%2") && 716 connect_gdb_remote_cmd_ap->AddRegexCommand( 717 "^([[:digit:]]+)$", 718 "process connect --plugin gdb-remote connect://localhost:%1")) { 719 CommandObjectSP command_sp(connect_gdb_remote_cmd_ap.release()); 720 m_command_dict[command_sp->GetCommandName()] = command_sp; 721 } 722 } 723 724 std::unique_ptr<CommandObjectRegexCommand> connect_kdp_remote_cmd_ap( 725 new CommandObjectRegexCommand( 726 *this, "kdp-remote", "Connect to a process via remote KDP server. " 727 "If no UDP port is specified, port 41139 is " 728 "assumed.", 729 "kdp-remote <hostname>[:<portnum>]", 2, 0, false)); 730 if (connect_kdp_remote_cmd_ap.get()) { 731 if (connect_kdp_remote_cmd_ap->AddRegexCommand( 732 "^([^:]+:[[:digit:]]+)$", 733 "process connect --plugin kdp-remote udp://%1") && 734 connect_kdp_remote_cmd_ap->AddRegexCommand( 735 "^(.+)$", "process connect --plugin kdp-remote udp://%1:41139")) { 736 CommandObjectSP command_sp(connect_kdp_remote_cmd_ap.release()); 737 m_command_dict[command_sp->GetCommandName()] = command_sp; 738 } 739 } 740 741 std::unique_ptr<CommandObjectRegexCommand> bt_regex_cmd_ap( 742 new CommandObjectRegexCommand( 743 *this, "_regexp-bt", 744 "Show the current thread's call stack. Any numeric argument " 745 "displays at most that many " 746 "frames. The argument 'all' displays all threads.", 747 "bt [<digit> | all]", 2, 0, false)); 748 if (bt_regex_cmd_ap.get()) { 749 // accept but don't document "bt -c <number>" -- before bt was a regex 750 // command if you wanted to backtrace three frames you would do "bt -c 3" 751 // but the intention is to have this emulate the gdb "bt" command and so 752 // now "bt 3" is the preferred form, in line with gdb. 753 if (bt_regex_cmd_ap->AddRegexCommand("^([[:digit:]]+)$", 754 "thread backtrace -c %1") && 755 bt_regex_cmd_ap->AddRegexCommand("^-c ([[:digit:]]+)$", 756 "thread backtrace -c %1") && 757 bt_regex_cmd_ap->AddRegexCommand("^all$", "thread backtrace all") && 758 bt_regex_cmd_ap->AddRegexCommand("^$", "thread backtrace")) { 759 CommandObjectSP command_sp(bt_regex_cmd_ap.release()); 760 m_command_dict[command_sp->GetCommandName()] = command_sp; 761 } 762 } 763 764 std::unique_ptr<CommandObjectRegexCommand> list_regex_cmd_ap( 765 new CommandObjectRegexCommand( 766 *this, "_regexp-list", 767 "List relevant source code using one of several shorthand formats.", 768 "\n" 769 "_regexp-list <file>:<line> // List around specific file/line\n" 770 "_regexp-list <line> // List current file around specified " 771 "line\n" 772 "_regexp-list <function-name> // List specified function\n" 773 "_regexp-list 0x<address> // List around specified address\n" 774 "_regexp-list -[<count>] // List previous <count> lines\n" 775 "_regexp-list // List subsequent lines", 776 2, CommandCompletions::eSourceFileCompletion, false)); 777 if (list_regex_cmd_ap.get()) { 778 if (list_regex_cmd_ap->AddRegexCommand("^([0-9]+)[[:space:]]*$", 779 "source list --line %1") && 780 list_regex_cmd_ap->AddRegexCommand( 781 "^(.*[^[:space:]])[[:space:]]*:[[:space:]]*([[:digit:]]+)[[:space:]" 782 "]*$", 783 "source list --file '%1' --line %2") && 784 list_regex_cmd_ap->AddRegexCommand( 785 "^\\*?(0x[[:xdigit:]]+)[[:space:]]*$", 786 "source list --address %1") && 787 list_regex_cmd_ap->AddRegexCommand("^-[[:space:]]*$", 788 "source list --reverse") && 789 list_regex_cmd_ap->AddRegexCommand( 790 "^-([[:digit:]]+)[[:space:]]*$", 791 "source list --reverse --count %1") && 792 list_regex_cmd_ap->AddRegexCommand("^(.+)$", 793 "source list --name \"%1\"") && 794 list_regex_cmd_ap->AddRegexCommand("^$", "source list")) { 795 CommandObjectSP list_regex_cmd_sp(list_regex_cmd_ap.release()); 796 m_command_dict[list_regex_cmd_sp->GetCommandName()] = list_regex_cmd_sp; 797 } 798 } 799 800 std::unique_ptr<CommandObjectRegexCommand> env_regex_cmd_ap( 801 new CommandObjectRegexCommand( 802 *this, "_regexp-env", 803 "Shorthand for viewing and setting environment variables.", 804 "\n" 805 "_regexp-env // Show enrivonment\n" 806 "_regexp-env <name>=<value> // Set an environment variable", 807 2, 0, false)); 808 if (env_regex_cmd_ap.get()) { 809 if (env_regex_cmd_ap->AddRegexCommand("^$", 810 "settings show target.env-vars") && 811 env_regex_cmd_ap->AddRegexCommand("^([A-Za-z_][A-Za-z_0-9]*=.*)$", 812 "settings set target.env-vars %1")) { 813 CommandObjectSP env_regex_cmd_sp(env_regex_cmd_ap.release()); 814 m_command_dict[env_regex_cmd_sp->GetCommandName()] = env_regex_cmd_sp; 815 } 816 } 817 818 std::unique_ptr<CommandObjectRegexCommand> jump_regex_cmd_ap( 819 new CommandObjectRegexCommand( 820 *this, "_regexp-jump", "Set the program counter to a new address.", 821 "\n" 822 "_regexp-jump <line>\n" 823 "_regexp-jump +<line-offset> | -<line-offset>\n" 824 "_regexp-jump <file>:<line>\n" 825 "_regexp-jump *<addr>\n", 826 2, 0, false)); 827 if (jump_regex_cmd_ap.get()) { 828 if (jump_regex_cmd_ap->AddRegexCommand("^\\*(.*)$", 829 "thread jump --addr %1") && 830 jump_regex_cmd_ap->AddRegexCommand("^([0-9]+)$", 831 "thread jump --line %1") && 832 jump_regex_cmd_ap->AddRegexCommand("^([^:]+):([0-9]+)$", 833 "thread jump --file %1 --line %2") && 834 jump_regex_cmd_ap->AddRegexCommand("^([+\\-][0-9]+)$", 835 "thread jump --by %1")) { 836 CommandObjectSP jump_regex_cmd_sp(jump_regex_cmd_ap.release()); 837 m_command_dict[jump_regex_cmd_sp->GetCommandName()] = jump_regex_cmd_sp; 838 } 839 } 840 } 841 842 int CommandInterpreter::GetCommandNamesMatchingPartialString( 843 const char *cmd_str, bool include_aliases, StringList &matches, 844 StringList &descriptions) { 845 AddNamesMatchingPartialString(m_command_dict, cmd_str, matches, 846 &descriptions); 847 848 if (include_aliases) { 849 AddNamesMatchingPartialString(m_alias_dict, cmd_str, matches, 850 &descriptions); 851 } 852 853 return matches.GetSize(); 854 } 855 856 CommandObjectSP 857 CommandInterpreter::GetCommandSP(llvm::StringRef cmd_str, bool include_aliases, 858 bool exact, StringList *matches, 859 StringList *descriptions) const { 860 CommandObjectSP command_sp; 861 862 std::string cmd = cmd_str; 863 864 if (HasCommands()) { 865 auto pos = m_command_dict.find(cmd); 866 if (pos != m_command_dict.end()) 867 command_sp = pos->second; 868 } 869 870 if (include_aliases && HasAliases()) { 871 auto alias_pos = m_alias_dict.find(cmd); 872 if (alias_pos != m_alias_dict.end()) 873 command_sp = alias_pos->second; 874 } 875 876 if (HasUserCommands()) { 877 auto pos = m_user_dict.find(cmd); 878 if (pos != m_user_dict.end()) 879 command_sp = pos->second; 880 } 881 882 if (!exact && !command_sp) { 883 // We will only get into here if we didn't find any exact matches. 884 885 CommandObjectSP user_match_sp, alias_match_sp, real_match_sp; 886 887 StringList local_matches; 888 if (matches == nullptr) 889 matches = &local_matches; 890 891 unsigned int num_cmd_matches = 0; 892 unsigned int num_alias_matches = 0; 893 unsigned int num_user_matches = 0; 894 895 // Look through the command dictionaries one by one, and if we get only one 896 // match from any of them in toto, then return that, otherwise return an 897 // empty CommandObjectSP and the list of matches. 898 899 if (HasCommands()) { 900 num_cmd_matches = AddNamesMatchingPartialString(m_command_dict, cmd_str, 901 *matches, descriptions); 902 } 903 904 if (num_cmd_matches == 1) { 905 cmd.assign(matches->GetStringAtIndex(0)); 906 auto pos = m_command_dict.find(cmd); 907 if (pos != m_command_dict.end()) 908 real_match_sp = pos->second; 909 } 910 911 if (include_aliases && HasAliases()) { 912 num_alias_matches = AddNamesMatchingPartialString(m_alias_dict, cmd_str, 913 *matches, descriptions); 914 } 915 916 if (num_alias_matches == 1) { 917 cmd.assign(matches->GetStringAtIndex(num_cmd_matches)); 918 auto alias_pos = m_alias_dict.find(cmd); 919 if (alias_pos != m_alias_dict.end()) 920 alias_match_sp = alias_pos->second; 921 } 922 923 if (HasUserCommands()) { 924 num_user_matches = AddNamesMatchingPartialString(m_user_dict, cmd_str, 925 *matches, descriptions); 926 } 927 928 if (num_user_matches == 1) { 929 cmd.assign( 930 matches->GetStringAtIndex(num_cmd_matches + num_alias_matches)); 931 932 auto pos = m_user_dict.find(cmd); 933 if (pos != m_user_dict.end()) 934 user_match_sp = pos->second; 935 } 936 937 // If we got exactly one match, return that, otherwise return the match 938 // list. 939 940 if (num_user_matches + num_cmd_matches + num_alias_matches == 1) { 941 if (num_cmd_matches) 942 return real_match_sp; 943 else if (num_alias_matches) 944 return alias_match_sp; 945 else 946 return user_match_sp; 947 } 948 } else if (matches && command_sp) { 949 matches->AppendString(cmd_str); 950 if (descriptions) 951 descriptions->AppendString(command_sp->GetHelp()); 952 } 953 954 return command_sp; 955 } 956 957 bool CommandInterpreter::AddCommand(llvm::StringRef name, 958 const lldb::CommandObjectSP &cmd_sp, 959 bool can_replace) { 960 if (cmd_sp.get()) 961 lldbassert((this == &cmd_sp->GetCommandInterpreter()) && 962 "tried to add a CommandObject from a different interpreter"); 963 964 if (name.empty()) 965 return false; 966 967 std::string name_sstr(name); 968 auto name_iter = m_command_dict.find(name_sstr); 969 if (name_iter != m_command_dict.end()) { 970 if (!can_replace || !name_iter->second->IsRemovable()) 971 return false; 972 name_iter->second = cmd_sp; 973 } else { 974 m_command_dict[name_sstr] = cmd_sp; 975 } 976 return true; 977 } 978 979 bool CommandInterpreter::AddUserCommand(llvm::StringRef name, 980 const lldb::CommandObjectSP &cmd_sp, 981 bool can_replace) { 982 if (cmd_sp.get()) 983 lldbassert((this == &cmd_sp->GetCommandInterpreter()) && 984 "tried to add a CommandObject from a different interpreter"); 985 986 if (!name.empty()) { 987 // do not allow replacement of internal commands 988 if (CommandExists(name)) { 989 if (!can_replace) 990 return false; 991 if (!m_command_dict[name]->IsRemovable()) 992 return false; 993 } 994 995 if (UserCommandExists(name)) { 996 if (!can_replace) 997 return false; 998 if (!m_user_dict[name]->IsRemovable()) 999 return false; 1000 } 1001 1002 m_user_dict[name] = cmd_sp; 1003 return true; 1004 } 1005 return false; 1006 } 1007 1008 CommandObjectSP CommandInterpreter::GetCommandSPExact(llvm::StringRef cmd_str, 1009 bool include_aliases) const { 1010 Args cmd_words(cmd_str); // Break up the command string into words, in case 1011 // it's a multi-word command. 1012 CommandObjectSP ret_val; // Possibly empty return value. 1013 1014 if (cmd_str.empty()) 1015 return ret_val; 1016 1017 if (cmd_words.GetArgumentCount() == 1) 1018 return GetCommandSP(cmd_str, include_aliases, true, nullptr); 1019 else { 1020 // We have a multi-word command (seemingly), so we need to do more work. 1021 // First, get the cmd_obj_sp for the first word in the command. 1022 CommandObjectSP cmd_obj_sp = GetCommandSP(llvm::StringRef(cmd_words.GetArgumentAtIndex(0)), 1023 include_aliases, true, nullptr); 1024 if (cmd_obj_sp.get() != nullptr) { 1025 // Loop through the rest of the words in the command (everything passed 1026 // in was supposed to be part of a command name), and find the 1027 // appropriate sub-command SP for each command word.... 1028 size_t end = cmd_words.GetArgumentCount(); 1029 for (size_t j = 1; j < end; ++j) { 1030 if (cmd_obj_sp->IsMultiwordObject()) { 1031 cmd_obj_sp = 1032 cmd_obj_sp->GetSubcommandSP(cmd_words.GetArgumentAtIndex(j)); 1033 if (cmd_obj_sp.get() == nullptr) 1034 // The sub-command name was invalid. Fail and return the empty 1035 // 'ret_val'. 1036 return ret_val; 1037 } else 1038 // We have more words in the command name, but we don't have a 1039 // multiword object. Fail and return empty 'ret_val'. 1040 return ret_val; 1041 } 1042 // We successfully looped through all the command words and got valid 1043 // command objects for them. Assign the last object retrieved to 1044 // 'ret_val'. 1045 ret_val = cmd_obj_sp; 1046 } 1047 } 1048 return ret_val; 1049 } 1050 1051 CommandObject * 1052 CommandInterpreter::GetCommandObject(llvm::StringRef cmd_str, 1053 StringList *matches, 1054 StringList *descriptions) const { 1055 CommandObject *command_obj = 1056 GetCommandSP(cmd_str, false, true, matches, descriptions).get(); 1057 1058 // If we didn't find an exact match to the command string in the commands, 1059 // look in the aliases. 1060 1061 if (command_obj) 1062 return command_obj; 1063 1064 command_obj = GetCommandSP(cmd_str, true, true, matches, descriptions).get(); 1065 1066 if (command_obj) 1067 return command_obj; 1068 1069 // If there wasn't an exact match then look for an inexact one in just the 1070 // commands 1071 command_obj = GetCommandSP(cmd_str, false, false, nullptr).get(); 1072 1073 // Finally, if there wasn't an inexact match among the commands, look for an 1074 // inexact match in both the commands and aliases. 1075 1076 if (command_obj) { 1077 if (matches) 1078 matches->AppendString(command_obj->GetCommandName()); 1079 if (descriptions) 1080 descriptions->AppendString(command_obj->GetHelp()); 1081 return command_obj; 1082 } 1083 1084 return GetCommandSP(cmd_str, true, false, matches, descriptions).get(); 1085 } 1086 1087 bool CommandInterpreter::CommandExists(llvm::StringRef cmd) const { 1088 return m_command_dict.find(cmd) != m_command_dict.end(); 1089 } 1090 1091 bool CommandInterpreter::GetAliasFullName(llvm::StringRef cmd, 1092 std::string &full_name) const { 1093 bool exact_match = (m_alias_dict.find(cmd) != m_alias_dict.end()); 1094 if (exact_match) { 1095 full_name.assign(cmd); 1096 return exact_match; 1097 } else { 1098 StringList matches; 1099 size_t num_alias_matches; 1100 num_alias_matches = 1101 AddNamesMatchingPartialString(m_alias_dict, cmd, matches); 1102 if (num_alias_matches == 1) { 1103 // Make sure this isn't shadowing a command in the regular command space: 1104 StringList regular_matches; 1105 const bool include_aliases = false; 1106 const bool exact = false; 1107 CommandObjectSP cmd_obj_sp( 1108 GetCommandSP(cmd, include_aliases, exact, ®ular_matches)); 1109 if (cmd_obj_sp || regular_matches.GetSize() > 0) 1110 return false; 1111 else { 1112 full_name.assign(matches.GetStringAtIndex(0)); 1113 return true; 1114 } 1115 } else 1116 return false; 1117 } 1118 } 1119 1120 bool CommandInterpreter::AliasExists(llvm::StringRef cmd) const { 1121 return m_alias_dict.find(cmd) != m_alias_dict.end(); 1122 } 1123 1124 bool CommandInterpreter::UserCommandExists(llvm::StringRef cmd) const { 1125 return m_user_dict.find(cmd) != m_user_dict.end(); 1126 } 1127 1128 CommandAlias * 1129 CommandInterpreter::AddAlias(llvm::StringRef alias_name, 1130 lldb::CommandObjectSP &command_obj_sp, 1131 llvm::StringRef args_string) { 1132 if (command_obj_sp.get()) 1133 lldbassert((this == &command_obj_sp->GetCommandInterpreter()) && 1134 "tried to add a CommandObject from a different interpreter"); 1135 1136 std::unique_ptr<CommandAlias> command_alias_up( 1137 new CommandAlias(*this, command_obj_sp, args_string, alias_name)); 1138 1139 if (command_alias_up && command_alias_up->IsValid()) { 1140 m_alias_dict[alias_name] = CommandObjectSP(command_alias_up.get()); 1141 return command_alias_up.release(); 1142 } 1143 1144 return nullptr; 1145 } 1146 1147 bool CommandInterpreter::RemoveAlias(llvm::StringRef alias_name) { 1148 auto pos = m_alias_dict.find(alias_name); 1149 if (pos != m_alias_dict.end()) { 1150 m_alias_dict.erase(pos); 1151 return true; 1152 } 1153 return false; 1154 } 1155 1156 bool CommandInterpreter::RemoveCommand(llvm::StringRef cmd) { 1157 auto pos = m_command_dict.find(cmd); 1158 if (pos != m_command_dict.end()) { 1159 if (pos->second->IsRemovable()) { 1160 // Only regular expression objects or python commands are removable 1161 m_command_dict.erase(pos); 1162 return true; 1163 } 1164 } 1165 return false; 1166 } 1167 bool CommandInterpreter::RemoveUser(llvm::StringRef alias_name) { 1168 CommandObject::CommandMap::iterator pos = m_user_dict.find(alias_name); 1169 if (pos != m_user_dict.end()) { 1170 m_user_dict.erase(pos); 1171 return true; 1172 } 1173 return false; 1174 } 1175 1176 void CommandInterpreter::GetHelp(CommandReturnObject &result, 1177 uint32_t cmd_types) { 1178 llvm::StringRef help_prologue(GetDebugger().GetIOHandlerHelpPrologue()); 1179 if (!help_prologue.empty()) { 1180 OutputFormattedHelpText(result.GetOutputStream(), llvm::StringRef(), 1181 help_prologue); 1182 } 1183 1184 CommandObject::CommandMap::const_iterator pos; 1185 size_t max_len = FindLongestCommandWord(m_command_dict); 1186 1187 if ((cmd_types & eCommandTypesBuiltin) == eCommandTypesBuiltin) { 1188 result.AppendMessage("Debugger commands:"); 1189 result.AppendMessage(""); 1190 1191 for (pos = m_command_dict.begin(); pos != m_command_dict.end(); ++pos) { 1192 if (!(cmd_types & eCommandTypesHidden) && 1193 (pos->first.compare(0, 1, "_") == 0)) 1194 continue; 1195 1196 OutputFormattedHelpText(result.GetOutputStream(), pos->first, "--", 1197 pos->second->GetHelp(), max_len); 1198 } 1199 result.AppendMessage(""); 1200 } 1201 1202 if (!m_alias_dict.empty() && 1203 ((cmd_types & eCommandTypesAliases) == eCommandTypesAliases)) { 1204 result.AppendMessageWithFormat( 1205 "Current command abbreviations " 1206 "(type '%shelp command alias' for more info):\n", 1207 GetCommandPrefix()); 1208 result.AppendMessage(""); 1209 max_len = FindLongestCommandWord(m_alias_dict); 1210 1211 for (auto alias_pos = m_alias_dict.begin(); alias_pos != m_alias_dict.end(); 1212 ++alias_pos) { 1213 OutputFormattedHelpText(result.GetOutputStream(), alias_pos->first, "--", 1214 alias_pos->second->GetHelp(), max_len); 1215 } 1216 result.AppendMessage(""); 1217 } 1218 1219 if (!m_user_dict.empty() && 1220 ((cmd_types & eCommandTypesUserDef) == eCommandTypesUserDef)) { 1221 result.AppendMessage("Current user-defined commands:"); 1222 result.AppendMessage(""); 1223 max_len = FindLongestCommandWord(m_user_dict); 1224 for (pos = m_user_dict.begin(); pos != m_user_dict.end(); ++pos) { 1225 OutputFormattedHelpText(result.GetOutputStream(), pos->first, "--", 1226 pos->second->GetHelp(), max_len); 1227 } 1228 result.AppendMessage(""); 1229 } 1230 1231 result.AppendMessageWithFormat( 1232 "For more information on any command, type '%shelp <command-name>'.\n", 1233 GetCommandPrefix()); 1234 } 1235 1236 CommandObject *CommandInterpreter::GetCommandObjectForCommand( 1237 llvm::StringRef &command_string) { 1238 // This function finds the final, lowest-level, alias-resolved command object 1239 // whose 'Execute' function will eventually be invoked by the given command 1240 // line. 1241 1242 CommandObject *cmd_obj = nullptr; 1243 size_t start = command_string.find_first_not_of(k_white_space); 1244 size_t end = 0; 1245 bool done = false; 1246 while (!done) { 1247 if (start != std::string::npos) { 1248 // Get the next word from command_string. 1249 end = command_string.find_first_of(k_white_space, start); 1250 if (end == std::string::npos) 1251 end = command_string.size(); 1252 std::string cmd_word = command_string.substr(start, end - start); 1253 1254 if (cmd_obj == nullptr) 1255 // Since cmd_obj is NULL we are on our first time through this loop. 1256 // Check to see if cmd_word is a valid command or alias. 1257 cmd_obj = GetCommandObject(cmd_word); 1258 else if (cmd_obj->IsMultiwordObject()) { 1259 // Our current object is a multi-word object; see if the cmd_word is a 1260 // valid sub-command for our object. 1261 CommandObject *sub_cmd_obj = 1262 cmd_obj->GetSubcommandObject(cmd_word.c_str()); 1263 if (sub_cmd_obj) 1264 cmd_obj = sub_cmd_obj; 1265 else // cmd_word was not a valid sub-command word, so we are done 1266 done = true; 1267 } else 1268 // We have a cmd_obj and it is not a multi-word object, so we are done. 1269 done = true; 1270 1271 // If we didn't find a valid command object, or our command object is not 1272 // a multi-word object, or we are at the end of the command_string, then 1273 // we are done. Otherwise, find the start of the next word. 1274 1275 if (!cmd_obj || !cmd_obj->IsMultiwordObject() || 1276 end >= command_string.size()) 1277 done = true; 1278 else 1279 start = command_string.find_first_not_of(k_white_space, end); 1280 } else 1281 // Unable to find any more words. 1282 done = true; 1283 } 1284 1285 command_string = command_string.substr(end); 1286 return cmd_obj; 1287 } 1288 1289 static const char *k_valid_command_chars = 1290 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_"; 1291 static void StripLeadingSpaces(std::string &s) { 1292 if (!s.empty()) { 1293 size_t pos = s.find_first_not_of(k_white_space); 1294 if (pos == std::string::npos) 1295 s.clear(); 1296 else if (pos == 0) 1297 return; 1298 s.erase(0, pos); 1299 } 1300 } 1301 1302 static size_t FindArgumentTerminator(const std::string &s) { 1303 const size_t s_len = s.size(); 1304 size_t offset = 0; 1305 while (offset < s_len) { 1306 size_t pos = s.find("--", offset); 1307 if (pos == std::string::npos) 1308 break; 1309 if (pos > 0) { 1310 if (isspace(s[pos - 1])) { 1311 // Check if the string ends "\s--" (where \s is a space character) or 1312 // if we have "\s--\s". 1313 if ((pos + 2 >= s_len) || isspace(s[pos + 2])) { 1314 return pos; 1315 } 1316 } 1317 } 1318 offset = pos + 2; 1319 } 1320 return std::string::npos; 1321 } 1322 1323 static bool ExtractCommand(std::string &command_string, std::string &command, 1324 std::string &suffix, char "e_char) { 1325 command.clear(); 1326 suffix.clear(); 1327 StripLeadingSpaces(command_string); 1328 1329 bool result = false; 1330 quote_char = '\0'; 1331 1332 if (!command_string.empty()) { 1333 const char first_char = command_string[0]; 1334 if (first_char == '\'' || first_char == '"') { 1335 quote_char = first_char; 1336 const size_t end_quote_pos = command_string.find(quote_char, 1); 1337 if (end_quote_pos == std::string::npos) { 1338 command.swap(command_string); 1339 command_string.erase(); 1340 } else { 1341 command.assign(command_string, 1, end_quote_pos - 1); 1342 if (end_quote_pos + 1 < command_string.size()) 1343 command_string.erase(0, command_string.find_first_not_of( 1344 k_white_space, end_quote_pos + 1)); 1345 else 1346 command_string.erase(); 1347 } 1348 } else { 1349 const size_t first_space_pos = 1350 command_string.find_first_of(k_white_space); 1351 if (first_space_pos == std::string::npos) { 1352 command.swap(command_string); 1353 command_string.erase(); 1354 } else { 1355 command.assign(command_string, 0, first_space_pos); 1356 command_string.erase(0, command_string.find_first_not_of( 1357 k_white_space, first_space_pos)); 1358 } 1359 } 1360 result = true; 1361 } 1362 1363 if (!command.empty()) { 1364 // actual commands can't start with '-' or '_' 1365 if (command[0] != '-' && command[0] != '_') { 1366 size_t pos = command.find_first_not_of(k_valid_command_chars); 1367 if (pos > 0 && pos != std::string::npos) { 1368 suffix.assign(command.begin() + pos, command.end()); 1369 command.erase(pos); 1370 } 1371 } 1372 } 1373 1374 return result; 1375 } 1376 1377 CommandObject *CommandInterpreter::BuildAliasResult( 1378 llvm::StringRef alias_name, std::string &raw_input_string, 1379 std::string &alias_result, CommandReturnObject &result) { 1380 CommandObject *alias_cmd_obj = nullptr; 1381 Args cmd_args(raw_input_string); 1382 alias_cmd_obj = GetCommandObject(alias_name); 1383 StreamString result_str; 1384 1385 if (!alias_cmd_obj || !alias_cmd_obj->IsAlias()) { 1386 alias_result.clear(); 1387 return alias_cmd_obj; 1388 } 1389 std::pair<CommandObjectSP, OptionArgVectorSP> desugared = 1390 ((CommandAlias *)alias_cmd_obj)->Desugar(); 1391 OptionArgVectorSP option_arg_vector_sp = desugared.second; 1392 alias_cmd_obj = desugared.first.get(); 1393 std::string alias_name_str = alias_name; 1394 if ((cmd_args.GetArgumentCount() == 0) || 1395 (alias_name_str != cmd_args.GetArgumentAtIndex(0))) 1396 cmd_args.Unshift(alias_name_str); 1397 1398 result_str.Printf("%s", alias_cmd_obj->GetCommandName().str().c_str()); 1399 1400 if (!option_arg_vector_sp.get()) { 1401 alias_result = result_str.GetString(); 1402 return alias_cmd_obj; 1403 } 1404 OptionArgVector *option_arg_vector = option_arg_vector_sp.get(); 1405 1406 int value_type; 1407 std::string option; 1408 std::string value; 1409 for (const auto &entry : *option_arg_vector) { 1410 std::tie(option, value_type, value) = entry; 1411 if (option == "<argument>") { 1412 result_str.Printf(" %s", value.c_str()); 1413 continue; 1414 } 1415 1416 result_str.Printf(" %s", option.c_str()); 1417 if (value_type == OptionParser::eNoArgument) 1418 continue; 1419 1420 if (value_type != OptionParser::eOptionalArgument) 1421 result_str.Printf(" "); 1422 int index = GetOptionArgumentPosition(value.c_str()); 1423 if (index == 0) 1424 result_str.Printf("%s", value.c_str()); 1425 else if (static_cast<size_t>(index) >= cmd_args.GetArgumentCount()) { 1426 1427 result.AppendErrorWithFormat("Not enough arguments provided; you " 1428 "need at least %d arguments to use " 1429 "this alias.\n", 1430 index); 1431 result.SetStatus(eReturnStatusFailed); 1432 return nullptr; 1433 } else { 1434 size_t strpos = raw_input_string.find(cmd_args.GetArgumentAtIndex(index)); 1435 if (strpos != std::string::npos) 1436 raw_input_string = raw_input_string.erase( 1437 strpos, strlen(cmd_args.GetArgumentAtIndex(index))); 1438 result_str.Printf("%s", cmd_args.GetArgumentAtIndex(index)); 1439 } 1440 } 1441 1442 alias_result = result_str.GetString(); 1443 return alias_cmd_obj; 1444 } 1445 1446 Status CommandInterpreter::PreprocessCommand(std::string &command) { 1447 // The command preprocessor needs to do things to the command line before any 1448 // parsing of arguments or anything else is done. The only current stuff that 1449 // gets preprocessed is anything enclosed in backtick ('`') characters is 1450 // evaluated as an expression and the result of the expression must be a 1451 // scalar that can be substituted into the command. An example would be: 1452 // (lldb) memory read `$rsp + 20` 1453 Status error; // Status for any expressions that might not evaluate 1454 size_t start_backtick; 1455 size_t pos = 0; 1456 while ((start_backtick = command.find('`', pos)) != std::string::npos) { 1457 // Stop if an error was encountered during the previous iteration. 1458 if (error.Fail()) 1459 break; 1460 1461 if (start_backtick > 0 && command[start_backtick - 1] == '\\') { 1462 // The backtick was preceded by a '\' character, remove the slash and 1463 // don't treat the backtick as the start of an expression. 1464 command.erase(start_backtick - 1, 1); 1465 // No need to add one to start_backtick since we just deleted a char. 1466 pos = start_backtick; 1467 continue; 1468 } 1469 1470 const size_t expr_content_start = start_backtick + 1; 1471 const size_t end_backtick = command.find('`', expr_content_start); 1472 1473 if (end_backtick == std::string::npos) { 1474 // Stop if there's no end backtick. 1475 break; 1476 } 1477 1478 if (end_backtick == expr_content_start) { 1479 // Skip over empty expression. (two backticks in a row) 1480 command.erase(start_backtick, 2); 1481 continue; 1482 } 1483 1484 std::string expr_str(command, expr_content_start, 1485 end_backtick - expr_content_start); 1486 1487 ExecutionContext exe_ctx(GetExecutionContext()); 1488 Target *target = exe_ctx.GetTargetPtr(); 1489 1490 // Get a dummy target to allow for calculator mode while processing 1491 // backticks. This also helps break the infinite loop caused when target is 1492 // null. 1493 if (!target) 1494 target = m_debugger.GetDummyTarget(); 1495 1496 if (!target) 1497 continue; 1498 1499 ValueObjectSP expr_result_valobj_sp; 1500 1501 EvaluateExpressionOptions options; 1502 options.SetCoerceToId(false); 1503 options.SetUnwindOnError(true); 1504 options.SetIgnoreBreakpoints(true); 1505 options.SetKeepInMemory(false); 1506 options.SetTryAllThreads(true); 1507 options.SetTimeout(llvm::None); 1508 1509 ExpressionResults expr_result = 1510 target->EvaluateExpression(expr_str.c_str(), exe_ctx.GetFramePtr(), 1511 expr_result_valobj_sp, options); 1512 1513 if (expr_result == eExpressionCompleted) { 1514 Scalar scalar; 1515 if (expr_result_valobj_sp) 1516 expr_result_valobj_sp = 1517 expr_result_valobj_sp->GetQualifiedRepresentationIfAvailable( 1518 expr_result_valobj_sp->GetDynamicValueType(), true); 1519 if (expr_result_valobj_sp->ResolveValue(scalar)) { 1520 command.erase(start_backtick, end_backtick - start_backtick + 1); 1521 StreamString value_strm; 1522 const bool show_type = false; 1523 scalar.GetValue(&value_strm, show_type); 1524 size_t value_string_size = value_strm.GetSize(); 1525 if (value_string_size) { 1526 command.insert(start_backtick, value_strm.GetString()); 1527 pos = start_backtick + value_string_size; 1528 continue; 1529 } else { 1530 error.SetErrorStringWithFormat("expression value didn't result " 1531 "in a scalar value for the " 1532 "expression '%s'", 1533 expr_str.c_str()); 1534 break; 1535 } 1536 } else { 1537 error.SetErrorStringWithFormat("expression value didn't result " 1538 "in a scalar value for the " 1539 "expression '%s'", 1540 expr_str.c_str()); 1541 break; 1542 } 1543 1544 continue; 1545 } 1546 1547 if (expr_result_valobj_sp) 1548 error = expr_result_valobj_sp->GetError(); 1549 1550 if (error.Success()) { 1551 switch (expr_result) { 1552 case eExpressionSetupError: 1553 error.SetErrorStringWithFormat( 1554 "expression setup error for the expression '%s'", expr_str.c_str()); 1555 break; 1556 case eExpressionParseError: 1557 error.SetErrorStringWithFormat( 1558 "expression parse error for the expression '%s'", expr_str.c_str()); 1559 break; 1560 case eExpressionResultUnavailable: 1561 error.SetErrorStringWithFormat( 1562 "expression error fetching result for the expression '%s'", 1563 expr_str.c_str()); 1564 break; 1565 case eExpressionCompleted: 1566 break; 1567 case eExpressionDiscarded: 1568 error.SetErrorStringWithFormat( 1569 "expression discarded for the expression '%s'", expr_str.c_str()); 1570 break; 1571 case eExpressionInterrupted: 1572 error.SetErrorStringWithFormat( 1573 "expression interrupted for the expression '%s'", expr_str.c_str()); 1574 break; 1575 case eExpressionHitBreakpoint: 1576 error.SetErrorStringWithFormat( 1577 "expression hit breakpoint for the expression '%s'", 1578 expr_str.c_str()); 1579 break; 1580 case eExpressionTimedOut: 1581 error.SetErrorStringWithFormat( 1582 "expression timed out for the expression '%s'", expr_str.c_str()); 1583 break; 1584 case eExpressionStoppedForDebug: 1585 error.SetErrorStringWithFormat("expression stop at entry point " 1586 "for debugging for the " 1587 "expression '%s'", 1588 expr_str.c_str()); 1589 break; 1590 } 1591 } 1592 } 1593 return error; 1594 } 1595 1596 bool CommandInterpreter::HandleCommand(const char *command_line, 1597 LazyBool lazy_add_to_history, 1598 CommandReturnObject &result, 1599 ExecutionContext *override_context, 1600 bool repeat_on_empty_command, 1601 bool no_context_switching) 1602 1603 { 1604 1605 std::string command_string(command_line); 1606 std::string original_command_string(command_line); 1607 1608 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_COMMANDS)); 1609 llvm::PrettyStackTraceFormat stack_trace("HandleCommand(command = \"%s\")", 1610 command_line); 1611 1612 if (log) 1613 log->Printf("Processing command: %s", command_line); 1614 1615 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION); 1616 Timer scoped_timer(func_cat, "Handling command: %s.", command_line); 1617 1618 if (!no_context_switching) 1619 UpdateExecutionContext(override_context); 1620 1621 if (WasInterrupted()) { 1622 result.AppendError("interrupted"); 1623 result.SetStatus(eReturnStatusFailed); 1624 return false; 1625 } 1626 1627 bool add_to_history; 1628 if (lazy_add_to_history == eLazyBoolCalculate) 1629 add_to_history = (m_command_source_depth == 0); 1630 else 1631 add_to_history = (lazy_add_to_history == eLazyBoolYes); 1632 1633 bool empty_command = false; 1634 bool comment_command = false; 1635 if (command_string.empty()) 1636 empty_command = true; 1637 else { 1638 const char *k_space_characters = "\t\n\v\f\r "; 1639 1640 size_t non_space = command_string.find_first_not_of(k_space_characters); 1641 // Check for empty line or comment line (lines whose first non-space 1642 // character is the comment character for this interpreter) 1643 if (non_space == std::string::npos) 1644 empty_command = true; 1645 else if (command_string[non_space] == m_comment_char) 1646 comment_command = true; 1647 else if (command_string[non_space] == CommandHistory::g_repeat_char) { 1648 llvm::StringRef search_str(command_string); 1649 search_str = search_str.drop_front(non_space); 1650 if (auto hist_str = m_command_history.FindString(search_str)) { 1651 add_to_history = false; 1652 command_string = *hist_str; 1653 original_command_string = *hist_str; 1654 } else { 1655 result.AppendErrorWithFormat("Could not find entry: %s in history", 1656 command_string.c_str()); 1657 result.SetStatus(eReturnStatusFailed); 1658 return false; 1659 } 1660 } 1661 } 1662 1663 if (empty_command) { 1664 if (repeat_on_empty_command) { 1665 if (m_command_history.IsEmpty()) { 1666 result.AppendError("empty command"); 1667 result.SetStatus(eReturnStatusFailed); 1668 return false; 1669 } else { 1670 command_line = m_repeat_command.c_str(); 1671 command_string = command_line; 1672 original_command_string = command_line; 1673 if (m_repeat_command.empty()) { 1674 result.AppendErrorWithFormat("No auto repeat.\n"); 1675 result.SetStatus(eReturnStatusFailed); 1676 return false; 1677 } 1678 } 1679 add_to_history = false; 1680 } else { 1681 result.SetStatus(eReturnStatusSuccessFinishNoResult); 1682 return true; 1683 } 1684 } else if (comment_command) { 1685 result.SetStatus(eReturnStatusSuccessFinishNoResult); 1686 return true; 1687 } 1688 1689 Status error(PreprocessCommand(command_string)); 1690 1691 if (error.Fail()) { 1692 result.AppendError(error.AsCString()); 1693 result.SetStatus(eReturnStatusFailed); 1694 return false; 1695 } 1696 1697 // Phase 1. 1698 1699 // Before we do ANY kind of argument processing, we need to figure out what 1700 // the real/final command object is for the specified command. This gets 1701 // complicated by the fact that the user could have specified an alias, and, 1702 // in translating the alias, there may also be command options and/or even 1703 // data (including raw text strings) that need to be found and inserted into 1704 // the command line as part of the translation. So this first step is plain 1705 // look-up and replacement, resulting in: 1706 // 1. the command object whose Execute method will actually be called 1707 // 2. a revised command string, with all substitutions and replacements 1708 // taken care of 1709 // From 1 above, we can determine whether the Execute function wants raw 1710 // input or not. 1711 1712 CommandObject *cmd_obj = ResolveCommandImpl(command_string, result); 1713 1714 // Although the user may have abbreviated the command, the command_string now 1715 // has the command expanded to the full name. For example, if the input was 1716 // "br s -n main", command_string is now "breakpoint set -n main". 1717 if (log) { 1718 llvm::StringRef command_name = cmd_obj ? cmd_obj->GetCommandName() : "<not found>"; 1719 log->Printf("HandleCommand, cmd_obj : '%s'", command_name.str().c_str()); 1720 log->Printf("HandleCommand, (revised) command_string: '%s'", 1721 command_string.c_str()); 1722 const bool wants_raw_input = 1723 (cmd_obj != NULL) ? cmd_obj->WantsRawCommandString() : false; 1724 log->Printf("HandleCommand, wants_raw_input:'%s'", 1725 wants_raw_input ? "True" : "False"); 1726 } 1727 1728 // Phase 2. 1729 // Take care of things like setting up the history command & calling the 1730 // appropriate Execute method on the CommandObject, with the appropriate 1731 // arguments. 1732 1733 if (cmd_obj != nullptr) { 1734 if (add_to_history) { 1735 Args command_args(command_string); 1736 const char *repeat_command = cmd_obj->GetRepeatCommand(command_args, 0); 1737 if (repeat_command != nullptr) 1738 m_repeat_command.assign(repeat_command); 1739 else 1740 m_repeat_command.assign(original_command_string); 1741 1742 m_command_history.AppendString(original_command_string); 1743 } 1744 1745 std::string remainder; 1746 const std::size_t actual_cmd_name_len = cmd_obj->GetCommandName().size(); 1747 if (actual_cmd_name_len < command_string.length()) 1748 remainder = command_string.substr(actual_cmd_name_len); 1749 1750 // Remove any initial spaces 1751 size_t pos = remainder.find_first_not_of(k_white_space); 1752 if (pos != 0 && pos != std::string::npos) 1753 remainder.erase(0, pos); 1754 1755 if (log) 1756 log->Printf( 1757 "HandleCommand, command line after removing command name(s): '%s'", 1758 remainder.c_str()); 1759 1760 cmd_obj->Execute(remainder.c_str(), result); 1761 } 1762 1763 if (log) 1764 log->Printf("HandleCommand, command %s", 1765 (result.Succeeded() ? "succeeded" : "did not succeed")); 1766 1767 return result.Succeeded(); 1768 } 1769 1770 int CommandInterpreter::HandleCompletionMatches(CompletionRequest &request) { 1771 int num_command_matches = 0; 1772 bool look_for_subcommand = false; 1773 1774 // For any of the command completions a unique match will be a complete word. 1775 request.SetWordComplete(true); 1776 1777 if (request.GetCursorIndex() == -1) { 1778 // We got nothing on the command line, so return the list of commands 1779 bool include_aliases = true; 1780 StringList new_matches, descriptions; 1781 num_command_matches = GetCommandNamesMatchingPartialString( 1782 "", include_aliases, new_matches, descriptions); 1783 request.AddCompletions(new_matches, descriptions); 1784 } else if (request.GetCursorIndex() == 0) { 1785 // The cursor is in the first argument, so just do a lookup in the 1786 // dictionary. 1787 StringList new_matches, new_descriptions; 1788 CommandObject *cmd_obj = 1789 GetCommandObject(request.GetParsedLine().GetArgumentAtIndex(0), 1790 &new_matches, &new_descriptions); 1791 1792 if (num_command_matches == 1 && cmd_obj && cmd_obj->IsMultiwordObject() && 1793 new_matches.GetStringAtIndex(0) != nullptr && 1794 strcmp(request.GetParsedLine().GetArgumentAtIndex(0), 1795 new_matches.GetStringAtIndex(0)) == 0) { 1796 if (request.GetParsedLine().GetArgumentCount() == 1) { 1797 request.SetWordComplete(true); 1798 } else { 1799 look_for_subcommand = true; 1800 num_command_matches = 0; 1801 new_matches.DeleteStringAtIndex(0); 1802 new_descriptions.DeleteStringAtIndex(0); 1803 request.GetParsedLine().AppendArgument(llvm::StringRef()); 1804 request.SetCursorIndex(request.GetCursorIndex() + 1); 1805 request.SetCursorCharPosition(0); 1806 } 1807 } 1808 request.AddCompletions(new_matches, new_descriptions); 1809 num_command_matches = request.GetNumberOfMatches(); 1810 } 1811 1812 if (request.GetCursorIndex() > 0 || look_for_subcommand) { 1813 // We are completing further on into a commands arguments, so find the 1814 // command and tell it to complete the command. First see if there is a 1815 // matching initial command: 1816 CommandObject *command_object = 1817 GetCommandObject(request.GetParsedLine().GetArgumentAtIndex(0)); 1818 if (command_object == nullptr) { 1819 return 0; 1820 } else { 1821 request.GetParsedLine().Shift(); 1822 request.SetCursorIndex(request.GetCursorIndex() - 1); 1823 num_command_matches = command_object->HandleCompletion(request); 1824 } 1825 } 1826 1827 return num_command_matches; 1828 } 1829 1830 int CommandInterpreter::HandleCompletion( 1831 const char *current_line, const char *cursor, const char *last_char, 1832 int match_start_point, int max_return_elements, StringList &matches, 1833 StringList &descriptions) { 1834 1835 llvm::StringRef command_line(current_line, last_char - current_line); 1836 CompletionResult result; 1837 CompletionRequest request(command_line, cursor - current_line, 1838 match_start_point, max_return_elements, result); 1839 // Don't complete comments, and if the line we are completing is just the 1840 // history repeat character, substitute the appropriate history line. 1841 const char *first_arg = request.GetParsedLine().GetArgumentAtIndex(0); 1842 if (first_arg) { 1843 if (first_arg[0] == m_comment_char) 1844 return 0; 1845 else if (first_arg[0] == CommandHistory::g_repeat_char) { 1846 if (auto hist_str = m_command_history.FindString(first_arg)) { 1847 matches.InsertStringAtIndex(0, *hist_str); 1848 descriptions.InsertStringAtIndex(0, "Previous command history event"); 1849 return -2; 1850 } else 1851 return 0; 1852 } 1853 } 1854 1855 // Only max_return_elements == -1 is supported at present: 1856 lldbassert(max_return_elements == -1); 1857 1858 int num_command_matches = HandleCompletionMatches(request); 1859 result.GetMatches(matches); 1860 result.GetDescriptions(descriptions); 1861 1862 if (num_command_matches <= 0) 1863 return num_command_matches; 1864 1865 if (request.GetParsedLine().GetArgumentCount() == 0) { 1866 // If we got an empty string, insert nothing. 1867 matches.InsertStringAtIndex(0, ""); 1868 descriptions.InsertStringAtIndex(0, ""); 1869 } else { 1870 // Now figure out if there is a common substring, and if so put that in 1871 // element 0, otherwise put an empty string in element 0. 1872 std::string command_partial_str = request.GetCursorArgumentPrefix().str(); 1873 1874 std::string common_prefix; 1875 matches.LongestCommonPrefix(common_prefix); 1876 const size_t partial_name_len = command_partial_str.size(); 1877 common_prefix.erase(0, partial_name_len); 1878 1879 // If we matched a unique single command, add a space... Only do this if 1880 // the completer told us this was a complete word, however... 1881 if (num_command_matches == 1 && request.GetWordComplete()) { 1882 char quote_char = request.GetParsedLine()[request.GetCursorIndex()].quote; 1883 common_prefix = 1884 Args::EscapeLLDBCommandArgument(common_prefix, quote_char); 1885 if (quote_char != '\0') 1886 common_prefix.push_back(quote_char); 1887 common_prefix.push_back(' '); 1888 } 1889 matches.InsertStringAtIndex(0, common_prefix.c_str()); 1890 descriptions.InsertStringAtIndex(0, ""); 1891 } 1892 return num_command_matches; 1893 } 1894 1895 CommandInterpreter::~CommandInterpreter() {} 1896 1897 void CommandInterpreter::UpdatePrompt(llvm::StringRef new_prompt) { 1898 EventSP prompt_change_event_sp( 1899 new Event(eBroadcastBitResetPrompt, new EventDataBytes(new_prompt))); 1900 ; 1901 BroadcastEvent(prompt_change_event_sp); 1902 if (m_command_io_handler_sp) 1903 m_command_io_handler_sp->SetPrompt(new_prompt); 1904 } 1905 1906 bool CommandInterpreter::Confirm(llvm::StringRef message, bool default_answer) { 1907 // Check AutoConfirm first: 1908 if (m_debugger.GetAutoConfirm()) 1909 return default_answer; 1910 1911 IOHandlerConfirm *confirm = 1912 new IOHandlerConfirm(m_debugger, message, default_answer); 1913 IOHandlerSP io_handler_sp(confirm); 1914 m_debugger.RunIOHandler(io_handler_sp); 1915 return confirm->GetResponse(); 1916 } 1917 1918 const CommandAlias * 1919 CommandInterpreter::GetAlias(llvm::StringRef alias_name) const { 1920 OptionArgVectorSP ret_val; 1921 1922 auto pos = m_alias_dict.find(alias_name); 1923 if (pos != m_alias_dict.end()) 1924 return (CommandAlias *)pos->second.get(); 1925 1926 return nullptr; 1927 } 1928 1929 bool CommandInterpreter::HasCommands() const { return (!m_command_dict.empty()); } 1930 1931 bool CommandInterpreter::HasAliases() const { return (!m_alias_dict.empty()); } 1932 1933 bool CommandInterpreter::HasUserCommands() const { return (!m_user_dict.empty()); } 1934 1935 bool CommandInterpreter::HasAliasOptions() const { return HasAliases(); } 1936 1937 void CommandInterpreter::BuildAliasCommandArgs(CommandObject *alias_cmd_obj, 1938 const char *alias_name, 1939 Args &cmd_args, 1940 std::string &raw_input_string, 1941 CommandReturnObject &result) { 1942 OptionArgVectorSP option_arg_vector_sp = 1943 GetAlias(alias_name)->GetOptionArguments(); 1944 1945 bool wants_raw_input = alias_cmd_obj->WantsRawCommandString(); 1946 1947 // Make sure that the alias name is the 0th element in cmd_args 1948 std::string alias_name_str = alias_name; 1949 if (alias_name_str != cmd_args.GetArgumentAtIndex(0)) 1950 cmd_args.Unshift(alias_name_str); 1951 1952 Args new_args(alias_cmd_obj->GetCommandName()); 1953 if (new_args.GetArgumentCount() == 2) 1954 new_args.Shift(); 1955 1956 if (option_arg_vector_sp.get()) { 1957 if (wants_raw_input) { 1958 // We have a command that both has command options and takes raw input. 1959 // Make *sure* it has a " -- " in the right place in the 1960 // raw_input_string. 1961 size_t pos = raw_input_string.find(" -- "); 1962 if (pos == std::string::npos) { 1963 // None found; assume it goes at the beginning of the raw input string 1964 raw_input_string.insert(0, " -- "); 1965 } 1966 } 1967 1968 OptionArgVector *option_arg_vector = option_arg_vector_sp.get(); 1969 const size_t old_size = cmd_args.GetArgumentCount(); 1970 std::vector<bool> used(old_size + 1, false); 1971 1972 used[0] = true; 1973 1974 int value_type; 1975 std::string option; 1976 std::string value; 1977 for (const auto &option_entry : *option_arg_vector) { 1978 std::tie(option, value_type, value) = option_entry; 1979 if (option == "<argument>") { 1980 if (!wants_raw_input || (value != "--")) { 1981 // Since we inserted this above, make sure we don't insert it twice 1982 new_args.AppendArgument(value); 1983 } 1984 continue; 1985 } 1986 1987 if (value_type != OptionParser::eOptionalArgument) 1988 new_args.AppendArgument(option); 1989 1990 if (value == "<no-argument>") 1991 continue; 1992 1993 int index = GetOptionArgumentPosition(value.c_str()); 1994 if (index == 0) { 1995 // value was NOT a positional argument; must be a real value 1996 if (value_type != OptionParser::eOptionalArgument) 1997 new_args.AppendArgument(value); 1998 else { 1999 char buffer[255]; 2000 ::snprintf(buffer, sizeof(buffer), "%s%s", option.c_str(), 2001 value.c_str()); 2002 new_args.AppendArgument(llvm::StringRef(buffer)); 2003 } 2004 2005 } else if (static_cast<size_t>(index) >= cmd_args.GetArgumentCount()) { 2006 result.AppendErrorWithFormat("Not enough arguments provided; you " 2007 "need at least %d arguments to use " 2008 "this alias.\n", 2009 index); 2010 result.SetStatus(eReturnStatusFailed); 2011 return; 2012 } else { 2013 // Find and remove cmd_args.GetArgumentAtIndex(i) from raw_input_string 2014 size_t strpos = 2015 raw_input_string.find(cmd_args.GetArgumentAtIndex(index)); 2016 if (strpos != std::string::npos) { 2017 raw_input_string = raw_input_string.erase( 2018 strpos, strlen(cmd_args.GetArgumentAtIndex(index))); 2019 } 2020 2021 if (value_type != OptionParser::eOptionalArgument) 2022 new_args.AppendArgument(cmd_args.GetArgumentAtIndex(index)); 2023 else { 2024 char buffer[255]; 2025 ::snprintf(buffer, sizeof(buffer), "%s%s", option.c_str(), 2026 cmd_args.GetArgumentAtIndex(index)); 2027 new_args.AppendArgument(buffer); 2028 } 2029 used[index] = true; 2030 } 2031 } 2032 2033 for (auto entry : llvm::enumerate(cmd_args.entries())) { 2034 if (!used[entry.index()] && !wants_raw_input) 2035 new_args.AppendArgument(entry.value().ref); 2036 } 2037 2038 cmd_args.Clear(); 2039 cmd_args.SetArguments(new_args.GetArgumentCount(), 2040 new_args.GetConstArgumentVector()); 2041 } else { 2042 result.SetStatus(eReturnStatusSuccessFinishNoResult); 2043 // This alias was not created with any options; nothing further needs to be 2044 // done, unless it is a command that wants raw input, in which case we need 2045 // to clear the rest of the data from cmd_args, since its in the raw input 2046 // string. 2047 if (wants_raw_input) { 2048 cmd_args.Clear(); 2049 cmd_args.SetArguments(new_args.GetArgumentCount(), 2050 new_args.GetConstArgumentVector()); 2051 } 2052 return; 2053 } 2054 2055 result.SetStatus(eReturnStatusSuccessFinishNoResult); 2056 return; 2057 } 2058 2059 int CommandInterpreter::GetOptionArgumentPosition(const char *in_string) { 2060 int position = 0; // Any string that isn't an argument position, i.e. '%' 2061 // followed by an integer, gets a position 2062 // of zero. 2063 2064 const char *cptr = in_string; 2065 2066 // Does it start with '%' 2067 if (cptr[0] == '%') { 2068 ++cptr; 2069 2070 // Is the rest of it entirely digits? 2071 if (isdigit(cptr[0])) { 2072 const char *start = cptr; 2073 while (isdigit(cptr[0])) 2074 ++cptr; 2075 2076 // We've gotten to the end of the digits; are we at the end of the 2077 // string? 2078 if (cptr[0] == '\0') 2079 position = atoi(start); 2080 } 2081 } 2082 2083 return position; 2084 } 2085 2086 void CommandInterpreter::SourceInitFile(bool in_cwd, 2087 CommandReturnObject &result) { 2088 FileSpec init_file; 2089 if (in_cwd) { 2090 ExecutionContext exe_ctx(GetExecutionContext()); 2091 Target *target = exe_ctx.GetTargetPtr(); 2092 if (target) { 2093 // In the current working directory we don't load any program specific 2094 // .lldbinit files, we only look for a ".lldbinit" file. 2095 if (m_skip_lldbinit_files) 2096 return; 2097 2098 LoadCWDlldbinitFile should_load = 2099 target->TargetProperties::GetLoadCWDlldbinitFile(); 2100 if (should_load == eLoadCWDlldbinitWarn) { 2101 FileSpec dot_lldb(".lldbinit"); 2102 FileSystem::Instance().Resolve(dot_lldb); 2103 llvm::SmallString<64> home_dir_path; 2104 llvm::sys::path::home_directory(home_dir_path); 2105 FileSpec homedir_dot_lldb(home_dir_path.c_str()); 2106 homedir_dot_lldb.AppendPathComponent(".lldbinit"); 2107 FileSystem::Instance().Resolve(homedir_dot_lldb); 2108 if (FileSystem::Instance().Exists(dot_lldb) && 2109 dot_lldb.GetDirectory() != homedir_dot_lldb.GetDirectory()) { 2110 result.AppendErrorWithFormat( 2111 "There is a .lldbinit file in the current directory which is not " 2112 "being read.\n" 2113 "To silence this warning without sourcing in the local " 2114 ".lldbinit,\n" 2115 "add the following to the lldbinit file in your home directory:\n" 2116 " settings set target.load-cwd-lldbinit false\n" 2117 "To allow lldb to source .lldbinit files in the current working " 2118 "directory,\n" 2119 "set the value of this variable to true. Only do so if you " 2120 "understand and\n" 2121 "accept the security risk."); 2122 result.SetStatus(eReturnStatusFailed); 2123 return; 2124 } 2125 } else if (should_load == eLoadCWDlldbinitTrue) { 2126 init_file.SetFile("./.lldbinit", FileSpec::Style::native); 2127 FileSystem::Instance().Resolve(init_file); 2128 } 2129 } 2130 } else { 2131 // If we aren't looking in the current working directory we are looking in 2132 // the home directory. We will first see if there is an application 2133 // specific ".lldbinit" file whose name is "~/.lldbinit" followed by a "-" 2134 // and the name of the program. If this file doesn't exist, we fall back to 2135 // just the "~/.lldbinit" file. We also obey any requests to not load the 2136 // init files. 2137 llvm::SmallString<64> home_dir_path; 2138 llvm::sys::path::home_directory(home_dir_path); 2139 FileSpec profilePath(home_dir_path.c_str()); 2140 profilePath.AppendPathComponent(".lldbinit"); 2141 std::string init_file_path = profilePath.GetPath(); 2142 2143 if (!m_skip_app_init_files) { 2144 FileSpec program_file_spec(HostInfo::GetProgramFileSpec()); 2145 const char *program_name = program_file_spec.GetFilename().AsCString(); 2146 2147 if (program_name) { 2148 char program_init_file_name[PATH_MAX]; 2149 ::snprintf(program_init_file_name, sizeof(program_init_file_name), 2150 "%s-%s", init_file_path.c_str(), program_name); 2151 init_file.SetFile(program_init_file_name, FileSpec::Style::native); 2152 FileSystem::Instance().Resolve(init_file); 2153 if (!FileSystem::Instance().Exists(init_file)) 2154 init_file.Clear(); 2155 } 2156 } 2157 2158 if (!init_file && !m_skip_lldbinit_files) 2159 init_file.SetFile(init_file_path, FileSpec::Style::native); 2160 } 2161 2162 // If the file exists, tell HandleCommand to 'source' it; this will do the 2163 // actual broadcasting of the commands back to any appropriate listener (see 2164 // CommandObjectSource::Execute for more details). 2165 2166 if (FileSystem::Instance().Exists(init_file)) { 2167 const bool saved_batch = SetBatchCommandMode(true); 2168 CommandInterpreterRunOptions options; 2169 options.SetSilent(true); 2170 options.SetStopOnError(false); 2171 options.SetStopOnContinue(true); 2172 2173 HandleCommandsFromFile(init_file, 2174 nullptr, // Execution context 2175 options, result); 2176 SetBatchCommandMode(saved_batch); 2177 } else { 2178 // nothing to be done if the file doesn't exist 2179 result.SetStatus(eReturnStatusSuccessFinishNoResult); 2180 } 2181 } 2182 2183 const char *CommandInterpreter::GetCommandPrefix() { 2184 const char *prefix = GetDebugger().GetIOHandlerCommandPrefix(); 2185 return prefix == NULL ? "" : prefix; 2186 } 2187 2188 PlatformSP CommandInterpreter::GetPlatform(bool prefer_target_platform) { 2189 PlatformSP platform_sp; 2190 if (prefer_target_platform) { 2191 ExecutionContext exe_ctx(GetExecutionContext()); 2192 Target *target = exe_ctx.GetTargetPtr(); 2193 if (target) 2194 platform_sp = target->GetPlatform(); 2195 } 2196 2197 if (!platform_sp) 2198 platform_sp = m_debugger.GetPlatformList().GetSelectedPlatform(); 2199 return platform_sp; 2200 } 2201 2202 void CommandInterpreter::HandleCommands(const StringList &commands, 2203 ExecutionContext *override_context, 2204 CommandInterpreterRunOptions &options, 2205 CommandReturnObject &result) { 2206 size_t num_lines = commands.GetSize(); 2207 2208 // If we are going to continue past a "continue" then we need to run the 2209 // commands synchronously. Make sure you reset this value anywhere you return 2210 // from the function. 2211 2212 bool old_async_execution = m_debugger.GetAsyncExecution(); 2213 2214 // If we've been given an execution context, set it at the start, but don't 2215 // keep resetting it or we will cause series of commands that change the 2216 // context, then do an operation that relies on that context to fail. 2217 2218 if (override_context != nullptr) 2219 UpdateExecutionContext(override_context); 2220 2221 if (!options.GetStopOnContinue()) { 2222 m_debugger.SetAsyncExecution(false); 2223 } 2224 2225 for (size_t idx = 0; idx < num_lines && !WasInterrupted(); idx++) { 2226 const char *cmd = commands.GetStringAtIndex(idx); 2227 if (cmd[0] == '\0') 2228 continue; 2229 2230 if (options.GetEchoCommands()) { 2231 // TODO: Add Stream support. 2232 result.AppendMessageWithFormat("%s %s\n", 2233 m_debugger.GetPrompt().str().c_str(), cmd); 2234 } 2235 2236 CommandReturnObject tmp_result; 2237 // If override_context is not NULL, pass no_context_switching = true for 2238 // HandleCommand() since we updated our context already. 2239 2240 // We might call into a regex or alias command, in which case the 2241 // add_to_history will get lost. This m_command_source_depth dingus is the 2242 // way we turn off adding to the history in that case, so set it up here. 2243 if (!options.GetAddToHistory()) 2244 m_command_source_depth++; 2245 bool success = 2246 HandleCommand(cmd, options.m_add_to_history, tmp_result, 2247 nullptr, /* override_context */ 2248 true, /* repeat_on_empty_command */ 2249 override_context != nullptr /* no_context_switching */); 2250 if (!options.GetAddToHistory()) 2251 m_command_source_depth--; 2252 2253 if (options.GetPrintResults()) { 2254 if (tmp_result.Succeeded()) 2255 result.AppendMessage(tmp_result.GetOutputData()); 2256 } 2257 2258 if (!success || !tmp_result.Succeeded()) { 2259 llvm::StringRef error_msg = tmp_result.GetErrorData(); 2260 if (error_msg.empty()) 2261 error_msg = "<unknown error>.\n"; 2262 if (options.GetStopOnError()) { 2263 result.AppendErrorWithFormat( 2264 "Aborting reading of commands after command #%" PRIu64 2265 ": '%s' failed with %s", 2266 (uint64_t)idx, cmd, error_msg.str().c_str()); 2267 result.SetStatus(eReturnStatusFailed); 2268 m_debugger.SetAsyncExecution(old_async_execution); 2269 return; 2270 } else if (options.GetPrintResults()) { 2271 result.AppendMessageWithFormat( 2272 "Command #%" PRIu64 " '%s' failed with %s", (uint64_t)idx + 1, cmd, 2273 error_msg.str().c_str()); 2274 } 2275 } 2276 2277 if (result.GetImmediateOutputStream()) 2278 result.GetImmediateOutputStream()->Flush(); 2279 2280 if (result.GetImmediateErrorStream()) 2281 result.GetImmediateErrorStream()->Flush(); 2282 2283 // N.B. Can't depend on DidChangeProcessState, because the state coming 2284 // into the command execution could be running (for instance in Breakpoint 2285 // Commands. So we check the return value to see if it is has running in 2286 // it. 2287 if ((tmp_result.GetStatus() == eReturnStatusSuccessContinuingNoResult) || 2288 (tmp_result.GetStatus() == eReturnStatusSuccessContinuingResult)) { 2289 if (options.GetStopOnContinue()) { 2290 // If we caused the target to proceed, and we're going to stop in that 2291 // case, set the status in our real result before returning. This is 2292 // an error if the continue was not the last command in the set of 2293 // commands to be run. 2294 if (idx != num_lines - 1) 2295 result.AppendErrorWithFormat( 2296 "Aborting reading of commands after command #%" PRIu64 2297 ": '%s' continued the target.\n", 2298 (uint64_t)idx + 1, cmd); 2299 else 2300 result.AppendMessageWithFormat("Command #%" PRIu64 2301 " '%s' continued the target.\n", 2302 (uint64_t)idx + 1, cmd); 2303 2304 result.SetStatus(tmp_result.GetStatus()); 2305 m_debugger.SetAsyncExecution(old_async_execution); 2306 2307 return; 2308 } 2309 } 2310 2311 // Also check for "stop on crash here: 2312 bool should_stop = false; 2313 if (tmp_result.GetDidChangeProcessState() && options.GetStopOnCrash()) { 2314 TargetSP target_sp(m_debugger.GetTargetList().GetSelectedTarget()); 2315 if (target_sp) { 2316 ProcessSP process_sp(target_sp->GetProcessSP()); 2317 if (process_sp) { 2318 for (ThreadSP thread_sp : process_sp->GetThreadList().Threads()) { 2319 StopReason reason = thread_sp->GetStopReason(); 2320 if (reason == eStopReasonSignal || reason == eStopReasonException || 2321 reason == eStopReasonInstrumentation) { 2322 should_stop = true; 2323 break; 2324 } 2325 } 2326 } 2327 } 2328 if (should_stop) { 2329 if (idx != num_lines - 1) 2330 result.AppendErrorWithFormat( 2331 "Aborting reading of commands after command #%" PRIu64 2332 ": '%s' stopped with a signal or exception.\n", 2333 (uint64_t)idx + 1, cmd); 2334 else 2335 result.AppendMessageWithFormat( 2336 "Command #%" PRIu64 " '%s' stopped with a signal or exception.\n", 2337 (uint64_t)idx + 1, cmd); 2338 2339 result.SetStatus(tmp_result.GetStatus()); 2340 m_debugger.SetAsyncExecution(old_async_execution); 2341 2342 return; 2343 } 2344 } 2345 } 2346 2347 result.SetStatus(eReturnStatusSuccessFinishResult); 2348 m_debugger.SetAsyncExecution(old_async_execution); 2349 2350 return; 2351 } 2352 2353 // Make flags that we can pass into the IOHandler so our delegates can do the 2354 // right thing 2355 enum { 2356 eHandleCommandFlagStopOnContinue = (1u << 0), 2357 eHandleCommandFlagStopOnError = (1u << 1), 2358 eHandleCommandFlagEchoCommand = (1u << 2), 2359 eHandleCommandFlagEchoCommentCommand = (1u << 3), 2360 eHandleCommandFlagPrintResult = (1u << 4), 2361 eHandleCommandFlagStopOnCrash = (1u << 5) 2362 }; 2363 2364 void CommandInterpreter::HandleCommandsFromFile( 2365 FileSpec &cmd_file, ExecutionContext *context, 2366 CommandInterpreterRunOptions &options, CommandReturnObject &result) { 2367 if (FileSystem::Instance().Exists(cmd_file)) { 2368 StreamFileSP input_file_sp(new StreamFile()); 2369 2370 std::string cmd_file_path = cmd_file.GetPath(); 2371 Status error = FileSystem::Instance().Open(input_file_sp->GetFile(), 2372 cmd_file, File::eOpenOptionRead); 2373 if (error.Success()) { 2374 Debugger &debugger = GetDebugger(); 2375 2376 uint32_t flags = 0; 2377 2378 if (options.m_stop_on_continue == eLazyBoolCalculate) { 2379 if (m_command_source_flags.empty()) { 2380 // Stop on continue by default 2381 flags |= eHandleCommandFlagStopOnContinue; 2382 } else if (m_command_source_flags.back() & 2383 eHandleCommandFlagStopOnContinue) { 2384 flags |= eHandleCommandFlagStopOnContinue; 2385 } 2386 } else if (options.m_stop_on_continue == eLazyBoolYes) { 2387 flags |= eHandleCommandFlagStopOnContinue; 2388 } 2389 2390 if (options.m_stop_on_error == eLazyBoolCalculate) { 2391 if (m_command_source_flags.empty()) { 2392 if (GetStopCmdSourceOnError()) 2393 flags |= eHandleCommandFlagStopOnError; 2394 } else if (m_command_source_flags.back() & 2395 eHandleCommandFlagStopOnError) { 2396 flags |= eHandleCommandFlagStopOnError; 2397 } 2398 } else if (options.m_stop_on_error == eLazyBoolYes) { 2399 flags |= eHandleCommandFlagStopOnError; 2400 } 2401 2402 // stop-on-crash can only be set, if it is present in all levels of 2403 // pushed flag sets. 2404 if (options.GetStopOnCrash()) { 2405 if (m_command_source_flags.empty()) { 2406 flags |= eHandleCommandFlagStopOnCrash; 2407 } else if (m_command_source_flags.back() & 2408 eHandleCommandFlagStopOnCrash) { 2409 flags |= eHandleCommandFlagStopOnCrash; 2410 } 2411 } 2412 2413 if (options.m_echo_commands == eLazyBoolCalculate) { 2414 if (m_command_source_flags.empty()) { 2415 // Echo command by default 2416 flags |= eHandleCommandFlagEchoCommand; 2417 } else if (m_command_source_flags.back() & 2418 eHandleCommandFlagEchoCommand) { 2419 flags |= eHandleCommandFlagEchoCommand; 2420 } 2421 } else if (options.m_echo_commands == eLazyBoolYes) { 2422 flags |= eHandleCommandFlagEchoCommand; 2423 } 2424 2425 // We will only ever ask for this flag, if we echo commands in general. 2426 if (options.m_echo_comment_commands == eLazyBoolCalculate) { 2427 if (m_command_source_flags.empty()) { 2428 // Echo comments by default 2429 flags |= eHandleCommandFlagEchoCommentCommand; 2430 } else if (m_command_source_flags.back() & 2431 eHandleCommandFlagEchoCommentCommand) { 2432 flags |= eHandleCommandFlagEchoCommentCommand; 2433 } 2434 } else if (options.m_echo_comment_commands == eLazyBoolYes) { 2435 flags |= eHandleCommandFlagEchoCommentCommand; 2436 } 2437 2438 if (options.m_print_results == eLazyBoolCalculate) { 2439 if (m_command_source_flags.empty()) { 2440 // Print output by default 2441 flags |= eHandleCommandFlagPrintResult; 2442 } else if (m_command_source_flags.back() & 2443 eHandleCommandFlagPrintResult) { 2444 flags |= eHandleCommandFlagPrintResult; 2445 } 2446 } else if (options.m_print_results == eLazyBoolYes) { 2447 flags |= eHandleCommandFlagPrintResult; 2448 } 2449 2450 if (flags & eHandleCommandFlagPrintResult) { 2451 debugger.GetOutputFile()->Printf("Executing commands in '%s'.\n", 2452 cmd_file_path.c_str()); 2453 } 2454 2455 // Used for inheriting the right settings when "command source" might 2456 // have nested "command source" commands 2457 lldb::StreamFileSP empty_stream_sp; 2458 m_command_source_flags.push_back(flags); 2459 IOHandlerSP io_handler_sp(new IOHandlerEditline( 2460 debugger, IOHandler::Type::CommandInterpreter, input_file_sp, 2461 empty_stream_sp, // Pass in an empty stream so we inherit the top 2462 // input reader output stream 2463 empty_stream_sp, // Pass in an empty stream so we inherit the top 2464 // input reader error stream 2465 flags, 2466 nullptr, // Pass in NULL for "editline_name" so no history is saved, 2467 // or written 2468 debugger.GetPrompt(), llvm::StringRef(), 2469 false, // Not multi-line 2470 debugger.GetUseColor(), 0, *this)); 2471 const bool old_async_execution = debugger.GetAsyncExecution(); 2472 2473 // Set synchronous execution if we are not stopping on continue 2474 if ((flags & eHandleCommandFlagStopOnContinue) == 0) 2475 debugger.SetAsyncExecution(false); 2476 2477 m_command_source_depth++; 2478 2479 debugger.RunIOHandler(io_handler_sp); 2480 if (!m_command_source_flags.empty()) 2481 m_command_source_flags.pop_back(); 2482 m_command_source_depth--; 2483 result.SetStatus(eReturnStatusSuccessFinishNoResult); 2484 debugger.SetAsyncExecution(old_async_execution); 2485 } else { 2486 result.AppendErrorWithFormat( 2487 "error: an error occurred read file '%s': %s\n", 2488 cmd_file_path.c_str(), error.AsCString()); 2489 result.SetStatus(eReturnStatusFailed); 2490 } 2491 2492 } else { 2493 result.AppendErrorWithFormat( 2494 "Error reading commands from file %s - file not found.\n", 2495 cmd_file.GetFilename().AsCString("<Unknown>")); 2496 result.SetStatus(eReturnStatusFailed); 2497 return; 2498 } 2499 } 2500 2501 ScriptInterpreter *CommandInterpreter::GetScriptInterpreter(bool can_create) { 2502 std::lock_guard<std::recursive_mutex> locker(m_script_interpreter_mutex); 2503 if (!m_script_interpreter_sp) { 2504 if (!can_create) 2505 return nullptr; 2506 lldb::ScriptLanguage script_lang = GetDebugger().GetScriptLanguage(); 2507 m_script_interpreter_sp = 2508 PluginManager::GetScriptInterpreterForLanguage(script_lang, *this); 2509 } 2510 return m_script_interpreter_sp.get(); 2511 } 2512 2513 bool CommandInterpreter::GetSynchronous() { return m_synchronous_execution; } 2514 2515 void CommandInterpreter::SetSynchronous(bool value) { 2516 m_synchronous_execution = value; 2517 } 2518 2519 void CommandInterpreter::OutputFormattedHelpText(Stream &strm, 2520 llvm::StringRef prefix, 2521 llvm::StringRef help_text) { 2522 const uint32_t max_columns = m_debugger.GetTerminalWidth(); 2523 2524 size_t line_width_max = max_columns - prefix.size(); 2525 if (line_width_max < 16) 2526 line_width_max = help_text.size() + prefix.size(); 2527 2528 strm.IndentMore(prefix.size()); 2529 bool prefixed_yet = false; 2530 while (!help_text.empty()) { 2531 // Prefix the first line, indent subsequent lines to line up 2532 if (!prefixed_yet) { 2533 strm << prefix; 2534 prefixed_yet = true; 2535 } else 2536 strm.Indent(); 2537 2538 // Never print more than the maximum on one line. 2539 llvm::StringRef this_line = help_text.substr(0, line_width_max); 2540 2541 // Always break on an explicit newline. 2542 std::size_t first_newline = this_line.find_first_of("\n"); 2543 2544 // Don't break on space/tab unless the text is too long to fit on one line. 2545 std::size_t last_space = llvm::StringRef::npos; 2546 if (this_line.size() != help_text.size()) 2547 last_space = this_line.find_last_of(" \t"); 2548 2549 // Break at whichever condition triggered first. 2550 this_line = this_line.substr(0, std::min(first_newline, last_space)); 2551 strm.PutCString(this_line); 2552 strm.EOL(); 2553 2554 // Remove whitespace / newlines after breaking. 2555 help_text = help_text.drop_front(this_line.size()).ltrim(); 2556 } 2557 strm.IndentLess(prefix.size()); 2558 } 2559 2560 void CommandInterpreter::OutputFormattedHelpText(Stream &strm, 2561 llvm::StringRef word_text, 2562 llvm::StringRef separator, 2563 llvm::StringRef help_text, 2564 size_t max_word_len) { 2565 StreamString prefix_stream; 2566 prefix_stream.Printf(" %-*s %*s ", (int)max_word_len, word_text.data(), 2567 (int)separator.size(), separator.data()); 2568 OutputFormattedHelpText(strm, prefix_stream.GetString(), help_text); 2569 } 2570 2571 void CommandInterpreter::OutputHelpText(Stream &strm, llvm::StringRef word_text, 2572 llvm::StringRef separator, 2573 llvm::StringRef help_text, 2574 uint32_t max_word_len) { 2575 int indent_size = max_word_len + separator.size() + 2; 2576 2577 strm.IndentMore(indent_size); 2578 2579 StreamString text_strm; 2580 text_strm.Printf("%-*s ", (int)max_word_len, word_text.data()); 2581 text_strm << separator << " " << help_text; 2582 2583 const uint32_t max_columns = m_debugger.GetTerminalWidth(); 2584 2585 llvm::StringRef text = text_strm.GetString(); 2586 2587 uint32_t chars_left = max_columns; 2588 2589 auto nextWordLength = [](llvm::StringRef S) { 2590 size_t pos = S.find_first_of(' '); 2591 return pos == llvm::StringRef::npos ? S.size() : pos; 2592 }; 2593 2594 while (!text.empty()) { 2595 if (text.front() == '\n' || 2596 (text.front() == ' ' && nextWordLength(text.ltrim(' ')) > chars_left)) { 2597 strm.EOL(); 2598 strm.Indent(); 2599 chars_left = max_columns - indent_size; 2600 if (text.front() == '\n') 2601 text = text.drop_front(); 2602 else 2603 text = text.ltrim(' '); 2604 } else { 2605 strm.PutChar(text.front()); 2606 --chars_left; 2607 text = text.drop_front(); 2608 } 2609 } 2610 2611 strm.EOL(); 2612 strm.IndentLess(indent_size); 2613 } 2614 2615 void CommandInterpreter::FindCommandsForApropos( 2616 llvm::StringRef search_word, StringList &commands_found, 2617 StringList &commands_help, CommandObject::CommandMap &command_map) { 2618 CommandObject::CommandMap::const_iterator pos; 2619 2620 for (pos = command_map.begin(); pos != command_map.end(); ++pos) { 2621 llvm::StringRef command_name = pos->first; 2622 CommandObject *cmd_obj = pos->second.get(); 2623 2624 const bool search_short_help = true; 2625 const bool search_long_help = false; 2626 const bool search_syntax = false; 2627 const bool search_options = false; 2628 if (command_name.contains_lower(search_word) || 2629 cmd_obj->HelpTextContainsWord(search_word, search_short_help, 2630 search_long_help, search_syntax, 2631 search_options)) { 2632 commands_found.AppendString(cmd_obj->GetCommandName()); 2633 commands_help.AppendString(cmd_obj->GetHelp()); 2634 } 2635 2636 if (cmd_obj->IsMultiwordObject()) { 2637 CommandObjectMultiword *cmd_multiword = cmd_obj->GetAsMultiwordCommand(); 2638 FindCommandsForApropos(search_word, commands_found, commands_help, 2639 cmd_multiword->GetSubcommandDictionary()); 2640 } 2641 } 2642 } 2643 2644 void CommandInterpreter::FindCommandsForApropos(llvm::StringRef search_word, 2645 StringList &commands_found, 2646 StringList &commands_help, 2647 bool search_builtin_commands, 2648 bool search_user_commands, 2649 bool search_alias_commands) { 2650 CommandObject::CommandMap::const_iterator pos; 2651 2652 if (search_builtin_commands) 2653 FindCommandsForApropos(search_word, commands_found, commands_help, 2654 m_command_dict); 2655 2656 if (search_user_commands) 2657 FindCommandsForApropos(search_word, commands_found, commands_help, 2658 m_user_dict); 2659 2660 if (search_alias_commands) 2661 FindCommandsForApropos(search_word, commands_found, commands_help, 2662 m_alias_dict); 2663 } 2664 2665 void CommandInterpreter::UpdateExecutionContext( 2666 ExecutionContext *override_context) { 2667 if (override_context != nullptr) { 2668 m_exe_ctx_ref = *override_context; 2669 } else { 2670 const bool adopt_selected = true; 2671 m_exe_ctx_ref.SetTargetPtr(m_debugger.GetSelectedTarget().get(), 2672 adopt_selected); 2673 } 2674 } 2675 2676 size_t CommandInterpreter::GetProcessOutput() { 2677 // The process has stuff waiting for stderr; get it and write it out to the 2678 // appropriate place. 2679 char stdio_buffer[1024]; 2680 size_t len; 2681 size_t total_bytes = 0; 2682 Status error; 2683 TargetSP target_sp(m_debugger.GetTargetList().GetSelectedTarget()); 2684 if (target_sp) { 2685 ProcessSP process_sp(target_sp->GetProcessSP()); 2686 if (process_sp) { 2687 while ((len = process_sp->GetSTDOUT(stdio_buffer, sizeof(stdio_buffer), 2688 error)) > 0) { 2689 size_t bytes_written = len; 2690 m_debugger.GetOutputFile()->Write(stdio_buffer, bytes_written); 2691 total_bytes += len; 2692 } 2693 while ((len = process_sp->GetSTDERR(stdio_buffer, sizeof(stdio_buffer), 2694 error)) > 0) { 2695 size_t bytes_written = len; 2696 m_debugger.GetErrorFile()->Write(stdio_buffer, bytes_written); 2697 total_bytes += len; 2698 } 2699 } 2700 } 2701 return total_bytes; 2702 } 2703 2704 void CommandInterpreter::StartHandlingCommand() { 2705 auto idle_state = CommandHandlingState::eIdle; 2706 if (m_command_state.compare_exchange_strong( 2707 idle_state, CommandHandlingState::eInProgress)) 2708 lldbassert(m_iohandler_nesting_level == 0); 2709 else 2710 lldbassert(m_iohandler_nesting_level > 0); 2711 ++m_iohandler_nesting_level; 2712 } 2713 2714 void CommandInterpreter::FinishHandlingCommand() { 2715 lldbassert(m_iohandler_nesting_level > 0); 2716 if (--m_iohandler_nesting_level == 0) { 2717 auto prev_state = m_command_state.exchange(CommandHandlingState::eIdle); 2718 lldbassert(prev_state != CommandHandlingState::eIdle); 2719 } 2720 } 2721 2722 bool CommandInterpreter::InterruptCommand() { 2723 auto in_progress = CommandHandlingState::eInProgress; 2724 return m_command_state.compare_exchange_strong( 2725 in_progress, CommandHandlingState::eInterrupted); 2726 } 2727 2728 bool CommandInterpreter::WasInterrupted() const { 2729 bool was_interrupted = 2730 (m_command_state == CommandHandlingState::eInterrupted); 2731 lldbassert(!was_interrupted || m_iohandler_nesting_level > 0); 2732 return was_interrupted; 2733 } 2734 2735 void CommandInterpreter::PrintCommandOutput(Stream &stream, 2736 llvm::StringRef str) { 2737 // Split the output into lines and poll for interrupt requests 2738 const char *data = str.data(); 2739 size_t size = str.size(); 2740 while (size > 0 && !WasInterrupted()) { 2741 size_t chunk_size = 0; 2742 for (; chunk_size < size; ++chunk_size) { 2743 lldbassert(data[chunk_size] != '\0'); 2744 if (data[chunk_size] == '\n') { 2745 ++chunk_size; 2746 break; 2747 } 2748 } 2749 chunk_size = stream.Write(data, chunk_size); 2750 lldbassert(size >= chunk_size); 2751 data += chunk_size; 2752 size -= chunk_size; 2753 } 2754 if (size > 0) { 2755 stream.Printf("\n... Interrupted.\n"); 2756 } 2757 } 2758 2759 bool CommandInterpreter::EchoCommandNonInteractive( 2760 llvm::StringRef line, const Flags &io_handler_flags) const { 2761 if (!io_handler_flags.Test(eHandleCommandFlagEchoCommand)) 2762 return false; 2763 2764 llvm::StringRef command = line.trim(); 2765 if (command.empty()) 2766 return true; 2767 2768 if (command.front() == m_comment_char) 2769 return io_handler_flags.Test(eHandleCommandFlagEchoCommentCommand); 2770 2771 return true; 2772 } 2773 2774 void CommandInterpreter::IOHandlerInputComplete(IOHandler &io_handler, 2775 std::string &line) { 2776 // If we were interrupted, bail out... 2777 if (WasInterrupted()) 2778 return; 2779 2780 const bool is_interactive = io_handler.GetIsInteractive(); 2781 if (!is_interactive) { 2782 // When we are not interactive, don't execute blank lines. This will happen 2783 // sourcing a commands file. We don't want blank lines to repeat the 2784 // previous command and cause any errors to occur (like redefining an 2785 // alias, get an error and stop parsing the commands file). 2786 if (line.empty()) 2787 return; 2788 2789 // When using a non-interactive file handle (like when sourcing commands 2790 // from a file) we need to echo the command out so we don't just see the 2791 // command output and no command... 2792 if (EchoCommandNonInteractive(line, io_handler.GetFlags())) 2793 io_handler.GetOutputStreamFile()->Printf("%s%s\n", io_handler.GetPrompt(), 2794 line.c_str()); 2795 } 2796 2797 StartHandlingCommand(); 2798 2799 lldb_private::CommandReturnObject result; 2800 HandleCommand(line.c_str(), eLazyBoolCalculate, result); 2801 2802 // Now emit the command output text from the command we just executed 2803 if (io_handler.GetFlags().Test(eHandleCommandFlagPrintResult)) { 2804 // Display any STDOUT/STDERR _prior_ to emitting the command result text 2805 GetProcessOutput(); 2806 2807 if (!result.GetImmediateOutputStream()) { 2808 llvm::StringRef output = result.GetOutputData(); 2809 PrintCommandOutput(*io_handler.GetOutputStreamFile(), output); 2810 } 2811 2812 // Now emit the command error text from the command we just executed 2813 if (!result.GetImmediateErrorStream()) { 2814 llvm::StringRef error = result.GetErrorData(); 2815 PrintCommandOutput(*io_handler.GetErrorStreamFile(), error); 2816 } 2817 } 2818 2819 FinishHandlingCommand(); 2820 2821 switch (result.GetStatus()) { 2822 case eReturnStatusInvalid: 2823 case eReturnStatusSuccessFinishNoResult: 2824 case eReturnStatusSuccessFinishResult: 2825 case eReturnStatusStarted: 2826 break; 2827 2828 case eReturnStatusSuccessContinuingNoResult: 2829 case eReturnStatusSuccessContinuingResult: 2830 if (io_handler.GetFlags().Test(eHandleCommandFlagStopOnContinue)) 2831 io_handler.SetIsDone(true); 2832 break; 2833 2834 case eReturnStatusFailed: 2835 m_num_errors++; 2836 if (io_handler.GetFlags().Test(eHandleCommandFlagStopOnError)) 2837 io_handler.SetIsDone(true); 2838 break; 2839 2840 case eReturnStatusQuit: 2841 m_quit_requested = true; 2842 io_handler.SetIsDone(true); 2843 break; 2844 } 2845 2846 // Finally, if we're going to stop on crash, check that here: 2847 if (!m_quit_requested && result.GetDidChangeProcessState() && 2848 io_handler.GetFlags().Test(eHandleCommandFlagStopOnCrash)) { 2849 bool should_stop = false; 2850 TargetSP target_sp(m_debugger.GetTargetList().GetSelectedTarget()); 2851 if (target_sp) { 2852 ProcessSP process_sp(target_sp->GetProcessSP()); 2853 if (process_sp) { 2854 for (ThreadSP thread_sp : process_sp->GetThreadList().Threads()) { 2855 StopReason reason = thread_sp->GetStopReason(); 2856 if ((reason == eStopReasonSignal || reason == eStopReasonException || 2857 reason == eStopReasonInstrumentation) && 2858 !result.GetAbnormalStopWasExpected()) { 2859 should_stop = true; 2860 break; 2861 } 2862 } 2863 } 2864 } 2865 if (should_stop) { 2866 io_handler.SetIsDone(true); 2867 m_stopped_for_crash = true; 2868 } 2869 } 2870 } 2871 2872 bool CommandInterpreter::IOHandlerInterrupt(IOHandler &io_handler) { 2873 ExecutionContext exe_ctx(GetExecutionContext()); 2874 Process *process = exe_ctx.GetProcessPtr(); 2875 2876 if (InterruptCommand()) 2877 return true; 2878 2879 if (process) { 2880 StateType state = process->GetState(); 2881 if (StateIsRunningState(state)) { 2882 process->Halt(); 2883 return true; // Don't do any updating when we are running 2884 } 2885 } 2886 2887 ScriptInterpreter *script_interpreter = GetScriptInterpreter(false); 2888 if (script_interpreter) { 2889 if (script_interpreter->Interrupt()) 2890 return true; 2891 } 2892 return false; 2893 } 2894 2895 void CommandInterpreter::GetLLDBCommandsFromIOHandler( 2896 const char *prompt, IOHandlerDelegate &delegate, bool asynchronously, 2897 void *baton) { 2898 Debugger &debugger = GetDebugger(); 2899 IOHandlerSP io_handler_sp( 2900 new IOHandlerEditline(debugger, IOHandler::Type::CommandList, 2901 "lldb", // Name of input reader for history 2902 llvm::StringRef::withNullAsEmpty(prompt), // Prompt 2903 llvm::StringRef(), // Continuation prompt 2904 true, // Get multiple lines 2905 debugger.GetUseColor(), 2906 0, // Don't show line numbers 2907 delegate)); // IOHandlerDelegate 2908 2909 if (io_handler_sp) { 2910 io_handler_sp->SetUserData(baton); 2911 if (asynchronously) 2912 debugger.PushIOHandler(io_handler_sp); 2913 else 2914 debugger.RunIOHandler(io_handler_sp); 2915 } 2916 } 2917 2918 void CommandInterpreter::GetPythonCommandsFromIOHandler( 2919 const char *prompt, IOHandlerDelegate &delegate, bool asynchronously, 2920 void *baton) { 2921 Debugger &debugger = GetDebugger(); 2922 IOHandlerSP io_handler_sp( 2923 new IOHandlerEditline(debugger, IOHandler::Type::PythonCode, 2924 "lldb-python", // Name of input reader for history 2925 llvm::StringRef::withNullAsEmpty(prompt), // Prompt 2926 llvm::StringRef(), // Continuation prompt 2927 true, // Get multiple lines 2928 debugger.GetUseColor(), 2929 0, // Don't show line numbers 2930 delegate)); // IOHandlerDelegate 2931 2932 if (io_handler_sp) { 2933 io_handler_sp->SetUserData(baton); 2934 if (asynchronously) 2935 debugger.PushIOHandler(io_handler_sp); 2936 else 2937 debugger.RunIOHandler(io_handler_sp); 2938 } 2939 } 2940 2941 bool CommandInterpreter::IsActive() { 2942 return m_debugger.IsTopIOHandler(m_command_io_handler_sp); 2943 } 2944 2945 lldb::IOHandlerSP 2946 CommandInterpreter::GetIOHandler(bool force_create, 2947 CommandInterpreterRunOptions *options) { 2948 // Always re-create the IOHandlerEditline in case the input changed. The old 2949 // instance might have had a non-interactive input and now it does or vice 2950 // versa. 2951 if (force_create || !m_command_io_handler_sp) { 2952 // Always re-create the IOHandlerEditline in case the input changed. The 2953 // old instance might have had a non-interactive input and now it does or 2954 // vice versa. 2955 uint32_t flags = 0; 2956 2957 if (options) { 2958 if (options->m_stop_on_continue == eLazyBoolYes) 2959 flags |= eHandleCommandFlagStopOnContinue; 2960 if (options->m_stop_on_error == eLazyBoolYes) 2961 flags |= eHandleCommandFlagStopOnError; 2962 if (options->m_stop_on_crash == eLazyBoolYes) 2963 flags |= eHandleCommandFlagStopOnCrash; 2964 if (options->m_echo_commands != eLazyBoolNo) 2965 flags |= eHandleCommandFlagEchoCommand; 2966 if (options->m_echo_comment_commands != eLazyBoolNo) 2967 flags |= eHandleCommandFlagEchoCommentCommand; 2968 if (options->m_print_results != eLazyBoolNo) 2969 flags |= eHandleCommandFlagPrintResult; 2970 } else { 2971 flags = eHandleCommandFlagEchoCommand | eHandleCommandFlagPrintResult; 2972 } 2973 2974 m_command_io_handler_sp.reset(new IOHandlerEditline( 2975 m_debugger, IOHandler::Type::CommandInterpreter, 2976 m_debugger.GetInputFile(), m_debugger.GetOutputFile(), 2977 m_debugger.GetErrorFile(), flags, "lldb", m_debugger.GetPrompt(), 2978 llvm::StringRef(), // Continuation prompt 2979 false, // Don't enable multiple line input, just single line commands 2980 m_debugger.GetUseColor(), 2981 0, // Don't show line numbers 2982 *this)); 2983 } 2984 return m_command_io_handler_sp; 2985 } 2986 2987 void CommandInterpreter::RunCommandInterpreter( 2988 bool auto_handle_events, bool spawn_thread, 2989 CommandInterpreterRunOptions &options) { 2990 // Always re-create the command interpreter when we run it in case any file 2991 // handles have changed. 2992 bool force_create = true; 2993 m_debugger.PushIOHandler(GetIOHandler(force_create, &options)); 2994 m_stopped_for_crash = false; 2995 2996 if (auto_handle_events) 2997 m_debugger.StartEventHandlerThread(); 2998 2999 if (spawn_thread) { 3000 m_debugger.StartIOHandlerThread(); 3001 } else { 3002 m_debugger.ExecuteIOHandlers(); 3003 3004 if (auto_handle_events) 3005 m_debugger.StopEventHandlerThread(); 3006 } 3007 } 3008 3009 CommandObject * 3010 CommandInterpreter::ResolveCommandImpl(std::string &command_line, 3011 CommandReturnObject &result) { 3012 std::string scratch_command(command_line); // working copy so we don't modify 3013 // command_line unless we succeed 3014 CommandObject *cmd_obj = nullptr; 3015 StreamString revised_command_line; 3016 bool wants_raw_input = false; 3017 size_t actual_cmd_name_len = 0; 3018 std::string next_word; 3019 StringList matches; 3020 bool done = false; 3021 while (!done) { 3022 char quote_char = '\0'; 3023 std::string suffix; 3024 ExtractCommand(scratch_command, next_word, suffix, quote_char); 3025 if (cmd_obj == nullptr) { 3026 std::string full_name; 3027 bool is_alias = GetAliasFullName(next_word, full_name); 3028 cmd_obj = GetCommandObject(next_word, &matches); 3029 bool is_real_command = 3030 (!is_alias) || (cmd_obj != nullptr && !cmd_obj->IsAlias()); 3031 if (!is_real_command) { 3032 matches.Clear(); 3033 std::string alias_result; 3034 cmd_obj = 3035 BuildAliasResult(full_name, scratch_command, alias_result, result); 3036 revised_command_line.Printf("%s", alias_result.c_str()); 3037 if (cmd_obj) { 3038 wants_raw_input = cmd_obj->WantsRawCommandString(); 3039 actual_cmd_name_len = cmd_obj->GetCommandName().size(); 3040 } 3041 } else { 3042 if (cmd_obj) { 3043 llvm::StringRef cmd_name = cmd_obj->GetCommandName(); 3044 actual_cmd_name_len += cmd_name.size(); 3045 revised_command_line.Printf("%s", cmd_name.str().c_str()); 3046 wants_raw_input = cmd_obj->WantsRawCommandString(); 3047 } else { 3048 revised_command_line.Printf("%s", next_word.c_str()); 3049 } 3050 } 3051 } else { 3052 if (cmd_obj->IsMultiwordObject()) { 3053 CommandObject *sub_cmd_obj = 3054 cmd_obj->GetSubcommandObject(next_word.c_str()); 3055 if (sub_cmd_obj) { 3056 // The subcommand's name includes the parent command's name, so 3057 // restart rather than append to the revised_command_line. 3058 llvm::StringRef sub_cmd_name = sub_cmd_obj->GetCommandName(); 3059 actual_cmd_name_len = sub_cmd_name.size() + 1; 3060 revised_command_line.Clear(); 3061 revised_command_line.Printf("%s", sub_cmd_name.str().c_str()); 3062 cmd_obj = sub_cmd_obj; 3063 wants_raw_input = cmd_obj->WantsRawCommandString(); 3064 } else { 3065 if (quote_char) 3066 revised_command_line.Printf(" %c%s%s%c", quote_char, 3067 next_word.c_str(), suffix.c_str(), 3068 quote_char); 3069 else 3070 revised_command_line.Printf(" %s%s", next_word.c_str(), 3071 suffix.c_str()); 3072 done = true; 3073 } 3074 } else { 3075 if (quote_char) 3076 revised_command_line.Printf(" %c%s%s%c", quote_char, 3077 next_word.c_str(), suffix.c_str(), 3078 quote_char); 3079 else 3080 revised_command_line.Printf(" %s%s", next_word.c_str(), 3081 suffix.c_str()); 3082 done = true; 3083 } 3084 } 3085 3086 if (cmd_obj == nullptr) { 3087 const size_t num_matches = matches.GetSize(); 3088 if (matches.GetSize() > 1) { 3089 StreamString error_msg; 3090 error_msg.Printf("Ambiguous command '%s'. Possible matches:\n", 3091 next_word.c_str()); 3092 3093 for (uint32_t i = 0; i < num_matches; ++i) { 3094 error_msg.Printf("\t%s\n", matches.GetStringAtIndex(i)); 3095 } 3096 result.AppendRawError(error_msg.GetString()); 3097 } else { 3098 // We didn't have only one match, otherwise we wouldn't get here. 3099 lldbassert(num_matches == 0); 3100 result.AppendErrorWithFormat("'%s' is not a valid command.\n", 3101 next_word.c_str()); 3102 } 3103 result.SetStatus(eReturnStatusFailed); 3104 return nullptr; 3105 } 3106 3107 if (cmd_obj->IsMultiwordObject()) { 3108 if (!suffix.empty()) { 3109 result.AppendErrorWithFormat( 3110 "command '%s' did not recognize '%s%s%s' as valid (subcommand " 3111 "might be invalid).\n", 3112 cmd_obj->GetCommandName().str().c_str(), 3113 next_word.empty() ? "" : next_word.c_str(), 3114 next_word.empty() ? " -- " : " ", suffix.c_str()); 3115 result.SetStatus(eReturnStatusFailed); 3116 return nullptr; 3117 } 3118 } else { 3119 // If we found a normal command, we are done 3120 done = true; 3121 if (!suffix.empty()) { 3122 switch (suffix[0]) { 3123 case '/': 3124 // GDB format suffixes 3125 { 3126 Options *command_options = cmd_obj->GetOptions(); 3127 if (command_options && 3128 command_options->SupportsLongOption("gdb-format")) { 3129 std::string gdb_format_option("--gdb-format="); 3130 gdb_format_option += (suffix.c_str() + 1); 3131 3132 std::string cmd = revised_command_line.GetString(); 3133 size_t arg_terminator_idx = FindArgumentTerminator(cmd); 3134 if (arg_terminator_idx != std::string::npos) { 3135 // Insert the gdb format option before the "--" that terminates 3136 // options 3137 gdb_format_option.append(1, ' '); 3138 cmd.insert(arg_terminator_idx, gdb_format_option); 3139 revised_command_line.Clear(); 3140 revised_command_line.PutCString(cmd); 3141 } else 3142 revised_command_line.Printf(" %s", gdb_format_option.c_str()); 3143 3144 if (wants_raw_input && 3145 FindArgumentTerminator(cmd) == std::string::npos) 3146 revised_command_line.PutCString(" --"); 3147 } else { 3148 result.AppendErrorWithFormat( 3149 "the '%s' command doesn't support the --gdb-format option\n", 3150 cmd_obj->GetCommandName().str().c_str()); 3151 result.SetStatus(eReturnStatusFailed); 3152 return nullptr; 3153 } 3154 } 3155 break; 3156 3157 default: 3158 result.AppendErrorWithFormat( 3159 "unknown command shorthand suffix: '%s'\n", suffix.c_str()); 3160 result.SetStatus(eReturnStatusFailed); 3161 return nullptr; 3162 } 3163 } 3164 } 3165 if (scratch_command.empty()) 3166 done = true; 3167 } 3168 3169 if (!scratch_command.empty()) 3170 revised_command_line.Printf(" %s", scratch_command.c_str()); 3171 3172 if (cmd_obj != NULL) 3173 command_line = revised_command_line.GetString(); 3174 3175 return cmd_obj; 3176 } 3177