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