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/ScopeExit.h" 80 #include "llvm/ADT/SmallString.h" 81 #include "llvm/Support/FormatAdapters.h" 82 #include "llvm/Support/Path.h" 83 #include "llvm/Support/PrettyStackTrace.h" 84 #include "llvm/Support/ScopedPrinter.h" 85 86 using namespace lldb; 87 using namespace lldb_private; 88 89 static const char *k_white_space = " \t\v"; 90 91 static constexpr const char *InitFileWarning = 92 "There is a .lldbinit file in the current directory which is not being " 93 "read.\n" 94 "To silence this warning without sourcing in the local .lldbinit,\n" 95 "add the following to the lldbinit file in your home directory:\n" 96 " settings set target.load-cwd-lldbinit false\n" 97 "To allow lldb to source .lldbinit files in the current working " 98 "directory,\n" 99 "set the value of this variable to true. Only do so if you understand " 100 "and\n" 101 "accept the security risk."; 102 103 #define LLDB_PROPERTIES_interpreter 104 #include "InterpreterProperties.inc" 105 106 enum { 107 #define LLDB_PROPERTIES_interpreter 108 #include "InterpreterPropertiesEnum.inc" 109 }; 110 111 ConstString &CommandInterpreter::GetStaticBroadcasterClass() { 112 static ConstString class_name("lldb.commandInterpreter"); 113 return class_name; 114 } 115 116 CommandInterpreter::CommandInterpreter(Debugger &debugger, 117 bool synchronous_execution) 118 : Broadcaster(debugger.GetBroadcasterManager(), 119 CommandInterpreter::GetStaticBroadcasterClass().AsCString()), 120 Properties(OptionValuePropertiesSP( 121 new OptionValueProperties(ConstString("interpreter")))), 122 IOHandlerDelegate(IOHandlerDelegate::Completion::LLDBCommand), 123 m_debugger(debugger), m_synchronous_execution(true), 124 m_skip_lldbinit_files(false), m_skip_app_init_files(false), 125 m_comment_char('#'), m_batch_command_mode(false), 126 m_truncation_warning(eNoTruncation), m_command_source_depth(0) { 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(__APPLE__) 420 #if defined(TARGET_OS_IPHONE) 421 AddAlias("r", cmd_obj_sp, "--"); 422 AddAlias("run", cmd_obj_sp, "--"); 423 #else 424 AddAlias("r", cmd_obj_sp, "--shell-expand-args true --"); 425 AddAlias("run", cmd_obj_sp, "--shell-expand-args true --"); 426 #endif 427 #else 428 StreamString defaultshell; 429 defaultshell.Printf("--shell=%s --", 430 HostInfo::GetDefaultShell().GetPath().c_str()); 431 AddAlias("r", cmd_obj_sp, defaultshell.GetString()); 432 AddAlias("run", cmd_obj_sp, defaultshell.GetString()); 433 #endif 434 } 435 436 cmd_obj_sp = GetCommandSPExact("target symbols add"); 437 if (cmd_obj_sp) { 438 AddAlias("add-dsym", cmd_obj_sp); 439 } 440 441 cmd_obj_sp = GetCommandSPExact("breakpoint set"); 442 if (cmd_obj_sp) { 443 AddAlias("rbreak", cmd_obj_sp, "--func-regex %1"); 444 } 445 446 cmd_obj_sp = GetCommandSPExact("frame variable"); 447 if (cmd_obj_sp) { 448 AddAlias("v", cmd_obj_sp); 449 AddAlias("var", cmd_obj_sp); 450 AddAlias("vo", cmd_obj_sp, "--object-description"); 451 } 452 453 cmd_obj_sp = GetCommandSPExact("register"); 454 if (cmd_obj_sp) { 455 AddAlias("re", cmd_obj_sp); 456 } 457 458 cmd_obj_sp = GetCommandSPExact("session history"); 459 if (cmd_obj_sp) { 460 AddAlias("history", cmd_obj_sp); 461 } 462 } 463 464 void CommandInterpreter::Clear() { 465 m_command_io_handler_sp.reset(); 466 } 467 468 const char *CommandInterpreter::ProcessEmbeddedScriptCommands(const char *arg) { 469 // This function has not yet been implemented. 470 471 // Look for any embedded script command 472 // If found, 473 // get interpreter object from the command dictionary, 474 // call execute_one_command on it, 475 // get the results as a string, 476 // substitute that string for current stuff. 477 478 return arg; 479 } 480 481 #define REGISTER_COMMAND_OBJECT(NAME, CLASS) \ 482 m_command_dict[NAME] = std::make_shared<CLASS>(*this); 483 484 void CommandInterpreter::LoadCommandDictionary() { 485 LLDB_SCOPED_TIMER(); 486 487 REGISTER_COMMAND_OBJECT("apropos", CommandObjectApropos); 488 REGISTER_COMMAND_OBJECT("breakpoint", CommandObjectMultiwordBreakpoint); 489 REGISTER_COMMAND_OBJECT("command", CommandObjectMultiwordCommands); 490 REGISTER_COMMAND_OBJECT("disassemble", CommandObjectDisassemble); 491 REGISTER_COMMAND_OBJECT("expression", CommandObjectExpression); 492 REGISTER_COMMAND_OBJECT("frame", CommandObjectMultiwordFrame); 493 REGISTER_COMMAND_OBJECT("gui", CommandObjectGUI); 494 REGISTER_COMMAND_OBJECT("help", CommandObjectHelp); 495 REGISTER_COMMAND_OBJECT("log", CommandObjectLog); 496 REGISTER_COMMAND_OBJECT("memory", CommandObjectMemory); 497 REGISTER_COMMAND_OBJECT("platform", CommandObjectPlatform); 498 REGISTER_COMMAND_OBJECT("plugin", CommandObjectPlugin); 499 REGISTER_COMMAND_OBJECT("process", CommandObjectMultiwordProcess); 500 REGISTER_COMMAND_OBJECT("quit", CommandObjectQuit); 501 REGISTER_COMMAND_OBJECT("register", CommandObjectRegister); 502 REGISTER_COMMAND_OBJECT("reproducer", CommandObjectReproducer); 503 REGISTER_COMMAND_OBJECT("script", CommandObjectScript); 504 REGISTER_COMMAND_OBJECT("settings", CommandObjectMultiwordSettings); 505 REGISTER_COMMAND_OBJECT("session", CommandObjectSession); 506 REGISTER_COMMAND_OBJECT("source", CommandObjectMultiwordSource); 507 REGISTER_COMMAND_OBJECT("statistics", CommandObjectStats); 508 REGISTER_COMMAND_OBJECT("target", CommandObjectMultiwordTarget); 509 REGISTER_COMMAND_OBJECT("thread", CommandObjectMultiwordThread); 510 REGISTER_COMMAND_OBJECT("trace", CommandObjectTrace); 511 REGISTER_COMMAND_OBJECT("type", CommandObjectType); 512 REGISTER_COMMAND_OBJECT("version", CommandObjectVersion); 513 REGISTER_COMMAND_OBJECT("watchpoint", CommandObjectMultiwordWatchpoint); 514 REGISTER_COMMAND_OBJECT("language", CommandObjectLanguage); 515 516 // clang-format off 517 const char *break_regexes[][2] = { 518 {"^(.*[^[:space:]])[[:space:]]*:[[:space:]]*([[:digit:]]+)[[:space:]]*:[[:space:]]*([[:digit:]]+)[[:space:]]*$", 519 "breakpoint set --file '%1' --line %2 --column %3"}, 520 {"^(.*[^[:space:]])[[:space:]]*:[[:space:]]*([[:digit:]]+)[[:space:]]*$", 521 "breakpoint set --file '%1' --line %2"}, 522 {"^/([^/]+)/$", "breakpoint set --source-pattern-regexp '%1'"}, 523 {"^([[:digit:]]+)[[:space:]]*$", "breakpoint set --line %1"}, 524 {"^\\*?(0x[[:xdigit:]]+)[[:space:]]*$", "breakpoint set --address %1"}, 525 {"^[\"']?([-+]?\\[.*\\])[\"']?[[:space:]]*$", 526 "breakpoint set --name '%1'"}, 527 {"^(-.*)$", "breakpoint set %1"}, 528 {"^(.*[^[:space:]])`(.*[^[:space:]])[[:space:]]*$", 529 "breakpoint set --name '%2' --shlib '%1'"}, 530 {"^\\&(.*[^[:space:]])[[:space:]]*$", 531 "breakpoint set --name '%1' --skip-prologue=0"}, 532 {"^[\"']?(.*[^[:space:]\"'])[\"']?[[:space:]]*$", 533 "breakpoint set --name '%1'"}}; 534 // clang-format on 535 536 size_t num_regexes = llvm::array_lengthof(break_regexes); 537 538 std::unique_ptr<CommandObjectRegexCommand> break_regex_cmd_up( 539 new CommandObjectRegexCommand( 540 *this, "_regexp-break", 541 "Set a breakpoint using one of several shorthand formats.", 542 "\n" 543 "_regexp-break <filename>:<linenum>:<colnum>\n" 544 " main.c:12:21 // Break at line 12 and column " 545 "21 of main.c\n\n" 546 "_regexp-break <filename>:<linenum>\n" 547 " main.c:12 // Break at line 12 of " 548 "main.c\n\n" 549 "_regexp-break <linenum>\n" 550 " 12 // Break at line 12 of current " 551 "file\n\n" 552 "_regexp-break 0x<address>\n" 553 " 0x1234000 // Break at address " 554 "0x1234000\n\n" 555 "_regexp-break <name>\n" 556 " main // Break in 'main' after the " 557 "prologue\n\n" 558 "_regexp-break &<name>\n" 559 " &main // Break at first instruction " 560 "in 'main'\n\n" 561 "_regexp-break <module>`<name>\n" 562 " libc.so`malloc // Break in 'malloc' from " 563 "'libc.so'\n\n" 564 "_regexp-break /<source-regex>/\n" 565 " /break here/ // Break on source lines in " 566 "current file\n" 567 " // containing text 'break " 568 "here'.\n", 569 3, 570 CommandCompletions::eSymbolCompletion | 571 CommandCompletions::eSourceFileCompletion, 572 false)); 573 574 if (break_regex_cmd_up) { 575 bool success = true; 576 for (size_t i = 0; i < num_regexes; i++) { 577 success = break_regex_cmd_up->AddRegexCommand(break_regexes[i][0], 578 break_regexes[i][1]); 579 if (!success) 580 break; 581 } 582 success = 583 break_regex_cmd_up->AddRegexCommand("^$", "breakpoint list --full"); 584 585 if (success) { 586 CommandObjectSP break_regex_cmd_sp(break_regex_cmd_up.release()); 587 m_command_dict[std::string(break_regex_cmd_sp->GetCommandName())] = 588 break_regex_cmd_sp; 589 } 590 } 591 592 std::unique_ptr<CommandObjectRegexCommand> tbreak_regex_cmd_up( 593 new CommandObjectRegexCommand( 594 *this, "_regexp-tbreak", 595 "Set a one-shot breakpoint using one of several shorthand formats.", 596 "\n" 597 "_regexp-break <filename>:<linenum>:<colnum>\n" 598 " main.c:12:21 // Break at line 12 and column " 599 "21 of main.c\n\n" 600 "_regexp-break <filename>:<linenum>\n" 601 " main.c:12 // Break at line 12 of " 602 "main.c\n\n" 603 "_regexp-break <linenum>\n" 604 " 12 // Break at line 12 of current " 605 "file\n\n" 606 "_regexp-break 0x<address>\n" 607 " 0x1234000 // Break at address " 608 "0x1234000\n\n" 609 "_regexp-break <name>\n" 610 " main // Break in 'main' after the " 611 "prologue\n\n" 612 "_regexp-break &<name>\n" 613 " &main // Break at first instruction " 614 "in 'main'\n\n" 615 "_regexp-break <module>`<name>\n" 616 " libc.so`malloc // Break in 'malloc' from " 617 "'libc.so'\n\n" 618 "_regexp-break /<source-regex>/\n" 619 " /break here/ // Break on source lines in " 620 "current file\n" 621 " // containing text 'break " 622 "here'.\n", 623 2, 624 CommandCompletions::eSymbolCompletion | 625 CommandCompletions::eSourceFileCompletion, 626 false)); 627 628 if (tbreak_regex_cmd_up) { 629 bool success = true; 630 for (size_t i = 0; i < num_regexes; i++) { 631 std::string command = break_regexes[i][1]; 632 command += " -o 1"; 633 success = 634 tbreak_regex_cmd_up->AddRegexCommand(break_regexes[i][0], command); 635 if (!success) 636 break; 637 } 638 success = 639 tbreak_regex_cmd_up->AddRegexCommand("^$", "breakpoint list --full"); 640 641 if (success) { 642 CommandObjectSP tbreak_regex_cmd_sp(tbreak_regex_cmd_up.release()); 643 m_command_dict[std::string(tbreak_regex_cmd_sp->GetCommandName())] = 644 tbreak_regex_cmd_sp; 645 } 646 } 647 648 std::unique_ptr<CommandObjectRegexCommand> attach_regex_cmd_up( 649 new CommandObjectRegexCommand( 650 *this, "_regexp-attach", "Attach to process by ID or name.", 651 "_regexp-attach <pid> | <process-name>", 2, 0, false)); 652 if (attach_regex_cmd_up) { 653 if (attach_regex_cmd_up->AddRegexCommand("^([0-9]+)[[:space:]]*$", 654 "process attach --pid %1") && 655 attach_regex_cmd_up->AddRegexCommand( 656 "^(-.*|.* -.*)$", "process attach %1") && // Any options that are 657 // specified get passed to 658 // 'process attach' 659 attach_regex_cmd_up->AddRegexCommand("^(.+)$", 660 "process attach --name '%1'") && 661 attach_regex_cmd_up->AddRegexCommand("^$", "process attach")) { 662 CommandObjectSP attach_regex_cmd_sp(attach_regex_cmd_up.release()); 663 m_command_dict[std::string(attach_regex_cmd_sp->GetCommandName())] = 664 attach_regex_cmd_sp; 665 } 666 } 667 668 std::unique_ptr<CommandObjectRegexCommand> down_regex_cmd_up( 669 new CommandObjectRegexCommand(*this, "_regexp-down", 670 "Select a newer stack frame. Defaults to " 671 "moving one frame, a numeric argument can " 672 "specify an arbitrary number.", 673 "_regexp-down [<count>]", 2, 0, false)); 674 if (down_regex_cmd_up) { 675 if (down_regex_cmd_up->AddRegexCommand("^$", "frame select -r -1") && 676 down_regex_cmd_up->AddRegexCommand("^([0-9]+)$", 677 "frame select -r -%1")) { 678 CommandObjectSP down_regex_cmd_sp(down_regex_cmd_up.release()); 679 m_command_dict[std::string(down_regex_cmd_sp->GetCommandName())] = 680 down_regex_cmd_sp; 681 } 682 } 683 684 std::unique_ptr<CommandObjectRegexCommand> up_regex_cmd_up( 685 new CommandObjectRegexCommand( 686 *this, "_regexp-up", 687 "Select an older stack frame. Defaults to moving one " 688 "frame, a numeric argument can specify an arbitrary number.", 689 "_regexp-up [<count>]", 2, 0, false)); 690 if (up_regex_cmd_up) { 691 if (up_regex_cmd_up->AddRegexCommand("^$", "frame select -r 1") && 692 up_regex_cmd_up->AddRegexCommand("^([0-9]+)$", "frame select -r %1")) { 693 CommandObjectSP up_regex_cmd_sp(up_regex_cmd_up.release()); 694 m_command_dict[std::string(up_regex_cmd_sp->GetCommandName())] = 695 up_regex_cmd_sp; 696 } 697 } 698 699 std::unique_ptr<CommandObjectRegexCommand> display_regex_cmd_up( 700 new CommandObjectRegexCommand( 701 *this, "_regexp-display", 702 "Evaluate an expression at every stop (see 'help target stop-hook'.)", 703 "_regexp-display expression", 2, 0, false)); 704 if (display_regex_cmd_up) { 705 if (display_regex_cmd_up->AddRegexCommand( 706 "^(.+)$", "target stop-hook add -o \"expr -- %1\"")) { 707 CommandObjectSP display_regex_cmd_sp(display_regex_cmd_up.release()); 708 m_command_dict[std::string(display_regex_cmd_sp->GetCommandName())] = 709 display_regex_cmd_sp; 710 } 711 } 712 713 std::unique_ptr<CommandObjectRegexCommand> undisplay_regex_cmd_up( 714 new CommandObjectRegexCommand(*this, "_regexp-undisplay", 715 "Stop displaying expression at every " 716 "stop (specified by stop-hook index.)", 717 "_regexp-undisplay stop-hook-number", 2, 0, 718 false)); 719 if (undisplay_regex_cmd_up) { 720 if (undisplay_regex_cmd_up->AddRegexCommand("^([0-9]+)$", 721 "target stop-hook delete %1")) { 722 CommandObjectSP undisplay_regex_cmd_sp(undisplay_regex_cmd_up.release()); 723 m_command_dict[std::string(undisplay_regex_cmd_sp->GetCommandName())] = 724 undisplay_regex_cmd_sp; 725 } 726 } 727 728 std::unique_ptr<CommandObjectRegexCommand> connect_gdb_remote_cmd_up( 729 new CommandObjectRegexCommand( 730 *this, "gdb-remote", 731 "Connect to a process via remote GDB server. " 732 "If no host is specifed, localhost is assumed.", 733 "gdb-remote [<hostname>:]<portnum>", 2, 0, false)); 734 if (connect_gdb_remote_cmd_up) { 735 if (connect_gdb_remote_cmd_up->AddRegexCommand( 736 "^([^:]+|\\[[0-9a-fA-F:]+.*\\]):([0-9]+)$", 737 "process connect --plugin gdb-remote connect://%1:%2") && 738 connect_gdb_remote_cmd_up->AddRegexCommand( 739 "^([[:digit:]]+)$", 740 "process connect --plugin gdb-remote connect://localhost:%1")) { 741 CommandObjectSP command_sp(connect_gdb_remote_cmd_up.release()); 742 m_command_dict[std::string(command_sp->GetCommandName())] = command_sp; 743 } 744 } 745 746 std::unique_ptr<CommandObjectRegexCommand> connect_kdp_remote_cmd_up( 747 new CommandObjectRegexCommand( 748 *this, "kdp-remote", 749 "Connect to a process via remote KDP server. " 750 "If no UDP port is specified, port 41139 is " 751 "assumed.", 752 "kdp-remote <hostname>[:<portnum>]", 2, 0, false)); 753 if (connect_kdp_remote_cmd_up) { 754 if (connect_kdp_remote_cmd_up->AddRegexCommand( 755 "^([^:]+:[[:digit:]]+)$", 756 "process connect --plugin kdp-remote udp://%1") && 757 connect_kdp_remote_cmd_up->AddRegexCommand( 758 "^(.+)$", "process connect --plugin kdp-remote udp://%1:41139")) { 759 CommandObjectSP command_sp(connect_kdp_remote_cmd_up.release()); 760 m_command_dict[std::string(command_sp->GetCommandName())] = command_sp; 761 } 762 } 763 764 std::unique_ptr<CommandObjectRegexCommand> bt_regex_cmd_up( 765 new CommandObjectRegexCommand( 766 *this, "_regexp-bt", 767 "Show the current thread's call stack. Any numeric argument " 768 "displays at most that many " 769 "frames. The argument 'all' displays all threads. Use 'settings" 770 " set frame-format' to customize the printing of individual frames " 771 "and 'settings set thread-format' to customize the thread header.", 772 "bt [<digit> | all]", 2, 0, false)); 773 if (bt_regex_cmd_up) { 774 // accept but don't document "bt -c <number>" -- before bt was a regex 775 // command if you wanted to backtrace three frames you would do "bt -c 3" 776 // but the intention is to have this emulate the gdb "bt" command and so 777 // now "bt 3" is the preferred form, in line with gdb. 778 if (bt_regex_cmd_up->AddRegexCommand("^([[:digit:]]+)[[:space:]]*$", 779 "thread backtrace -c %1") && 780 bt_regex_cmd_up->AddRegexCommand("^-c ([[:digit:]]+)[[:space:]]*$", 781 "thread backtrace -c %1") && 782 bt_regex_cmd_up->AddRegexCommand("^all[[:space:]]*$", "thread backtrace all") && 783 bt_regex_cmd_up->AddRegexCommand("^[[:space:]]*$", "thread backtrace")) { 784 CommandObjectSP command_sp(bt_regex_cmd_up.release()); 785 m_command_dict[std::string(command_sp->GetCommandName())] = command_sp; 786 } 787 } 788 789 std::unique_ptr<CommandObjectRegexCommand> list_regex_cmd_up( 790 new CommandObjectRegexCommand( 791 *this, "_regexp-list", 792 "List relevant source code using one of several shorthand formats.", 793 "\n" 794 "_regexp-list <file>:<line> // List around specific file/line\n" 795 "_regexp-list <line> // List current file around specified " 796 "line\n" 797 "_regexp-list <function-name> // List specified function\n" 798 "_regexp-list 0x<address> // List around specified address\n" 799 "_regexp-list -[<count>] // List previous <count> lines\n" 800 "_regexp-list // List subsequent lines", 801 2, CommandCompletions::eSourceFileCompletion, false)); 802 if (list_regex_cmd_up) { 803 if (list_regex_cmd_up->AddRegexCommand("^([0-9]+)[[:space:]]*$", 804 "source list --line %1") && 805 list_regex_cmd_up->AddRegexCommand( 806 "^(.*[^[:space:]])[[:space:]]*:[[:space:]]*([[:digit:]]+)[[:space:]" 807 "]*$", 808 "source list --file '%1' --line %2") && 809 list_regex_cmd_up->AddRegexCommand( 810 "^\\*?(0x[[:xdigit:]]+)[[:space:]]*$", 811 "source list --address %1") && 812 list_regex_cmd_up->AddRegexCommand("^-[[:space:]]*$", 813 "source list --reverse") && 814 list_regex_cmd_up->AddRegexCommand( 815 "^-([[:digit:]]+)[[:space:]]*$", 816 "source list --reverse --count %1") && 817 list_regex_cmd_up->AddRegexCommand("^(.+)$", 818 "source list --name \"%1\"") && 819 list_regex_cmd_up->AddRegexCommand("^$", "source list")) { 820 CommandObjectSP list_regex_cmd_sp(list_regex_cmd_up.release()); 821 m_command_dict[std::string(list_regex_cmd_sp->GetCommandName())] = 822 list_regex_cmd_sp; 823 } 824 } 825 826 std::unique_ptr<CommandObjectRegexCommand> env_regex_cmd_up( 827 new CommandObjectRegexCommand( 828 *this, "_regexp-env", 829 "Shorthand for viewing and setting environment variables.", 830 "\n" 831 "_regexp-env // Show environment\n" 832 "_regexp-env <name>=<value> // Set an environment variable", 833 2, 0, false)); 834 if (env_regex_cmd_up) { 835 if (env_regex_cmd_up->AddRegexCommand("^$", 836 "settings show target.env-vars") && 837 env_regex_cmd_up->AddRegexCommand("^([A-Za-z_][A-Za-z_0-9]*=.*)$", 838 "settings set target.env-vars %1")) { 839 CommandObjectSP env_regex_cmd_sp(env_regex_cmd_up.release()); 840 m_command_dict[std::string(env_regex_cmd_sp->GetCommandName())] = 841 env_regex_cmd_sp; 842 } 843 } 844 845 std::unique_ptr<CommandObjectRegexCommand> jump_regex_cmd_up( 846 new CommandObjectRegexCommand( 847 *this, "_regexp-jump", "Set the program counter to a new address.", 848 "\n" 849 "_regexp-jump <line>\n" 850 "_regexp-jump +<line-offset> | -<line-offset>\n" 851 "_regexp-jump <file>:<line>\n" 852 "_regexp-jump *<addr>\n", 853 2, 0, false)); 854 if (jump_regex_cmd_up) { 855 if (jump_regex_cmd_up->AddRegexCommand("^\\*(.*)$", 856 "thread jump --addr %1") && 857 jump_regex_cmd_up->AddRegexCommand("^([0-9]+)$", 858 "thread jump --line %1") && 859 jump_regex_cmd_up->AddRegexCommand("^([^:]+):([0-9]+)$", 860 "thread jump --file %1 --line %2") && 861 jump_regex_cmd_up->AddRegexCommand("^([+\\-][0-9]+)$", 862 "thread jump --by %1")) { 863 CommandObjectSP jump_regex_cmd_sp(jump_regex_cmd_up.release()); 864 m_command_dict[std::string(jump_regex_cmd_sp->GetCommandName())] = 865 jump_regex_cmd_sp; 866 } 867 } 868 } 869 870 int CommandInterpreter::GetCommandNamesMatchingPartialString( 871 const char *cmd_str, bool include_aliases, StringList &matches, 872 StringList &descriptions) { 873 AddNamesMatchingPartialString(m_command_dict, cmd_str, matches, 874 &descriptions); 875 876 if (include_aliases) { 877 AddNamesMatchingPartialString(m_alias_dict, cmd_str, matches, 878 &descriptions); 879 } 880 881 return matches.GetSize(); 882 } 883 884 CommandObjectSP 885 CommandInterpreter::GetCommandSP(llvm::StringRef cmd_str, bool include_aliases, 886 bool exact, StringList *matches, 887 StringList *descriptions) const { 888 CommandObjectSP command_sp; 889 890 std::string cmd = std::string(cmd_str); 891 892 if (HasCommands()) { 893 auto pos = m_command_dict.find(cmd); 894 if (pos != m_command_dict.end()) 895 command_sp = pos->second; 896 } 897 898 if (include_aliases && HasAliases()) { 899 auto alias_pos = m_alias_dict.find(cmd); 900 if (alias_pos != m_alias_dict.end()) 901 command_sp = alias_pos->second; 902 } 903 904 if (HasUserCommands()) { 905 auto pos = m_user_dict.find(cmd); 906 if (pos != m_user_dict.end()) 907 command_sp = pos->second; 908 } 909 910 if (!exact && !command_sp) { 911 // We will only get into here if we didn't find any exact matches. 912 913 CommandObjectSP user_match_sp, alias_match_sp, real_match_sp; 914 915 StringList local_matches; 916 if (matches == nullptr) 917 matches = &local_matches; 918 919 unsigned int num_cmd_matches = 0; 920 unsigned int num_alias_matches = 0; 921 unsigned int num_user_matches = 0; 922 923 // Look through the command dictionaries one by one, and if we get only one 924 // match from any of them in toto, then return that, otherwise return an 925 // empty CommandObjectSP and the list of matches. 926 927 if (HasCommands()) { 928 num_cmd_matches = AddNamesMatchingPartialString(m_command_dict, cmd_str, 929 *matches, descriptions); 930 } 931 932 if (num_cmd_matches == 1) { 933 cmd.assign(matches->GetStringAtIndex(0)); 934 auto pos = m_command_dict.find(cmd); 935 if (pos != m_command_dict.end()) 936 real_match_sp = pos->second; 937 } 938 939 if (include_aliases && HasAliases()) { 940 num_alias_matches = AddNamesMatchingPartialString(m_alias_dict, cmd_str, 941 *matches, descriptions); 942 } 943 944 if (num_alias_matches == 1) { 945 cmd.assign(matches->GetStringAtIndex(num_cmd_matches)); 946 auto alias_pos = m_alias_dict.find(cmd); 947 if (alias_pos != m_alias_dict.end()) 948 alias_match_sp = alias_pos->second; 949 } 950 951 if (HasUserCommands()) { 952 num_user_matches = AddNamesMatchingPartialString(m_user_dict, cmd_str, 953 *matches, descriptions); 954 } 955 956 if (num_user_matches == 1) { 957 cmd.assign( 958 matches->GetStringAtIndex(num_cmd_matches + num_alias_matches)); 959 960 auto pos = m_user_dict.find(cmd); 961 if (pos != m_user_dict.end()) 962 user_match_sp = pos->second; 963 } 964 965 // If we got exactly one match, return that, otherwise return the match 966 // list. 967 968 if (num_user_matches + num_cmd_matches + num_alias_matches == 1) { 969 if (num_cmd_matches) 970 return real_match_sp; 971 else if (num_alias_matches) 972 return alias_match_sp; 973 else 974 return user_match_sp; 975 } 976 } else if (matches && command_sp) { 977 matches->AppendString(cmd_str); 978 if (descriptions) 979 descriptions->AppendString(command_sp->GetHelp()); 980 } 981 982 return command_sp; 983 } 984 985 bool CommandInterpreter::AddCommand(llvm::StringRef name, 986 const lldb::CommandObjectSP &cmd_sp, 987 bool can_replace) { 988 if (cmd_sp.get()) 989 lldbassert((this == &cmd_sp->GetCommandInterpreter()) && 990 "tried to add a CommandObject from a different interpreter"); 991 992 if (name.empty()) 993 return false; 994 995 std::string name_sstr(name); 996 auto name_iter = m_command_dict.find(name_sstr); 997 if (name_iter != m_command_dict.end()) { 998 if (!can_replace || !name_iter->second->IsRemovable()) 999 return false; 1000 name_iter->second = cmd_sp; 1001 } else { 1002 m_command_dict[name_sstr] = cmd_sp; 1003 } 1004 return true; 1005 } 1006 1007 bool CommandInterpreter::AddUserCommand(llvm::StringRef name, 1008 const lldb::CommandObjectSP &cmd_sp, 1009 bool can_replace) { 1010 if (cmd_sp.get()) 1011 lldbassert((this == &cmd_sp->GetCommandInterpreter()) && 1012 "tried to add a CommandObject from a different interpreter"); 1013 1014 if (!name.empty()) { 1015 // do not allow replacement of internal commands 1016 if (CommandExists(name)) { 1017 if (!can_replace) 1018 return false; 1019 if (!m_command_dict[std::string(name)]->IsRemovable()) 1020 return false; 1021 } 1022 1023 if (UserCommandExists(name)) { 1024 if (!can_replace) 1025 return false; 1026 if (!m_user_dict[std::string(name)]->IsRemovable()) 1027 return false; 1028 } 1029 1030 m_user_dict[std::string(name)] = cmd_sp; 1031 return true; 1032 } 1033 return false; 1034 } 1035 1036 CommandObjectSP 1037 CommandInterpreter::GetCommandSPExact(llvm::StringRef cmd_str, 1038 bool include_aliases) const { 1039 // Break up the command string into words, in case it's a multi-word command. 1040 Args cmd_words(cmd_str); 1041 1042 if (cmd_str.empty()) 1043 return {}; 1044 1045 if (cmd_words.GetArgumentCount() == 1) 1046 return GetCommandSP(cmd_str, include_aliases, true); 1047 1048 // We have a multi-word command (seemingly), so we need to do more work. 1049 // First, get the cmd_obj_sp for the first word in the command. 1050 CommandObjectSP cmd_obj_sp = 1051 GetCommandSP(cmd_words.GetArgumentAtIndex(0), include_aliases, true); 1052 if (!cmd_obj_sp) 1053 return {}; 1054 1055 // Loop through the rest of the words in the command (everything passed in 1056 // was supposed to be part of a command name), and find the appropriate 1057 // sub-command SP for each command word.... 1058 size_t end = cmd_words.GetArgumentCount(); 1059 for (size_t i = 1; i < end; ++i) { 1060 if (!cmd_obj_sp->IsMultiwordObject()) { 1061 // We have more words in the command name, but we don't have a 1062 // multiword object. Fail and return. 1063 return {}; 1064 } 1065 1066 cmd_obj_sp = cmd_obj_sp->GetSubcommandSP(cmd_words.GetArgumentAtIndex(i)); 1067 if (!cmd_obj_sp) { 1068 // The sub-command name was invalid. Fail and return. 1069 return {}; 1070 } 1071 } 1072 1073 // We successfully looped through all the command words and got valid 1074 // command objects for them. 1075 return cmd_obj_sp; 1076 } 1077 1078 CommandObject * 1079 CommandInterpreter::GetCommandObject(llvm::StringRef cmd_str, 1080 StringList *matches, 1081 StringList *descriptions) const { 1082 CommandObject *command_obj = 1083 GetCommandSP(cmd_str, false, true, matches, descriptions).get(); 1084 1085 // If we didn't find an exact match to the command string in the commands, 1086 // look in the aliases. 1087 1088 if (command_obj) 1089 return command_obj; 1090 1091 command_obj = GetCommandSP(cmd_str, true, true, matches, descriptions).get(); 1092 1093 if (command_obj) 1094 return command_obj; 1095 1096 // If there wasn't an exact match then look for an inexact one in just the 1097 // commands 1098 command_obj = GetCommandSP(cmd_str, false, false, nullptr).get(); 1099 1100 // Finally, if there wasn't an inexact match among the commands, look for an 1101 // inexact match in both the commands and aliases. 1102 1103 if (command_obj) { 1104 if (matches) 1105 matches->AppendString(command_obj->GetCommandName()); 1106 if (descriptions) 1107 descriptions->AppendString(command_obj->GetHelp()); 1108 return command_obj; 1109 } 1110 1111 return GetCommandSP(cmd_str, true, false, matches, descriptions).get(); 1112 } 1113 1114 bool CommandInterpreter::CommandExists(llvm::StringRef cmd) const { 1115 return m_command_dict.find(std::string(cmd)) != m_command_dict.end(); 1116 } 1117 1118 bool CommandInterpreter::GetAliasFullName(llvm::StringRef cmd, 1119 std::string &full_name) const { 1120 bool exact_match = 1121 (m_alias_dict.find(std::string(cmd)) != m_alias_dict.end()); 1122 if (exact_match) { 1123 full_name.assign(std::string(cmd)); 1124 return exact_match; 1125 } else { 1126 StringList matches; 1127 size_t num_alias_matches; 1128 num_alias_matches = 1129 AddNamesMatchingPartialString(m_alias_dict, cmd, matches); 1130 if (num_alias_matches == 1) { 1131 // Make sure this isn't shadowing a command in the regular command space: 1132 StringList regular_matches; 1133 const bool include_aliases = false; 1134 const bool exact = false; 1135 CommandObjectSP cmd_obj_sp( 1136 GetCommandSP(cmd, include_aliases, exact, ®ular_matches)); 1137 if (cmd_obj_sp || regular_matches.GetSize() > 0) 1138 return false; 1139 else { 1140 full_name.assign(matches.GetStringAtIndex(0)); 1141 return true; 1142 } 1143 } else 1144 return false; 1145 } 1146 } 1147 1148 bool CommandInterpreter::AliasExists(llvm::StringRef cmd) const { 1149 return m_alias_dict.find(std::string(cmd)) != m_alias_dict.end(); 1150 } 1151 1152 bool CommandInterpreter::UserCommandExists(llvm::StringRef cmd) const { 1153 return m_user_dict.find(std::string(cmd)) != m_user_dict.end(); 1154 } 1155 1156 CommandAlias * 1157 CommandInterpreter::AddAlias(llvm::StringRef alias_name, 1158 lldb::CommandObjectSP &command_obj_sp, 1159 llvm::StringRef args_string) { 1160 if (command_obj_sp.get()) 1161 lldbassert((this == &command_obj_sp->GetCommandInterpreter()) && 1162 "tried to add a CommandObject from a different interpreter"); 1163 1164 std::unique_ptr<CommandAlias> command_alias_up( 1165 new CommandAlias(*this, command_obj_sp, args_string, alias_name)); 1166 1167 if (command_alias_up && command_alias_up->IsValid()) { 1168 m_alias_dict[std::string(alias_name)] = 1169 CommandObjectSP(command_alias_up.get()); 1170 return command_alias_up.release(); 1171 } 1172 1173 return nullptr; 1174 } 1175 1176 bool CommandInterpreter::RemoveAlias(llvm::StringRef alias_name) { 1177 auto pos = m_alias_dict.find(std::string(alias_name)); 1178 if (pos != m_alias_dict.end()) { 1179 m_alias_dict.erase(pos); 1180 return true; 1181 } 1182 return false; 1183 } 1184 1185 bool CommandInterpreter::RemoveCommand(llvm::StringRef cmd) { 1186 auto pos = m_command_dict.find(std::string(cmd)); 1187 if (pos != m_command_dict.end()) { 1188 if (pos->second->IsRemovable()) { 1189 // Only regular expression objects or python commands are removable 1190 m_command_dict.erase(pos); 1191 return true; 1192 } 1193 } 1194 return false; 1195 } 1196 bool CommandInterpreter::RemoveUser(llvm::StringRef alias_name) { 1197 CommandObject::CommandMap::iterator pos = 1198 m_user_dict.find(std::string(alias_name)); 1199 if (pos != m_user_dict.end()) { 1200 m_user_dict.erase(pos); 1201 return true; 1202 } 1203 return false; 1204 } 1205 1206 void CommandInterpreter::GetHelp(CommandReturnObject &result, 1207 uint32_t cmd_types) { 1208 llvm::StringRef help_prologue(GetDebugger().GetIOHandlerHelpPrologue()); 1209 if (!help_prologue.empty()) { 1210 OutputFormattedHelpText(result.GetOutputStream(), llvm::StringRef(), 1211 help_prologue); 1212 } 1213 1214 CommandObject::CommandMap::const_iterator pos; 1215 size_t max_len = FindLongestCommandWord(m_command_dict); 1216 1217 if ((cmd_types & eCommandTypesBuiltin) == eCommandTypesBuiltin) { 1218 result.AppendMessage("Debugger commands:"); 1219 result.AppendMessage(""); 1220 1221 for (pos = m_command_dict.begin(); pos != m_command_dict.end(); ++pos) { 1222 if (!(cmd_types & eCommandTypesHidden) && 1223 (pos->first.compare(0, 1, "_") == 0)) 1224 continue; 1225 1226 OutputFormattedHelpText(result.GetOutputStream(), pos->first, "--", 1227 pos->second->GetHelp(), max_len); 1228 } 1229 result.AppendMessage(""); 1230 } 1231 1232 if (!m_alias_dict.empty() && 1233 ((cmd_types & eCommandTypesAliases) == eCommandTypesAliases)) { 1234 result.AppendMessageWithFormat( 1235 "Current command abbreviations " 1236 "(type '%shelp command alias' for more info):\n", 1237 GetCommandPrefix()); 1238 result.AppendMessage(""); 1239 max_len = FindLongestCommandWord(m_alias_dict); 1240 1241 for (auto alias_pos = m_alias_dict.begin(); alias_pos != m_alias_dict.end(); 1242 ++alias_pos) { 1243 OutputFormattedHelpText(result.GetOutputStream(), alias_pos->first, "--", 1244 alias_pos->second->GetHelp(), max_len); 1245 } 1246 result.AppendMessage(""); 1247 } 1248 1249 if (!m_user_dict.empty() && 1250 ((cmd_types & eCommandTypesUserDef) == eCommandTypesUserDef)) { 1251 result.AppendMessage("Current user-defined commands:"); 1252 result.AppendMessage(""); 1253 max_len = FindLongestCommandWord(m_user_dict); 1254 for (pos = m_user_dict.begin(); pos != m_user_dict.end(); ++pos) { 1255 OutputFormattedHelpText(result.GetOutputStream(), pos->first, "--", 1256 pos->second->GetHelp(), max_len); 1257 } 1258 result.AppendMessage(""); 1259 } 1260 1261 result.AppendMessageWithFormat( 1262 "For more information on any command, type '%shelp <command-name>'.\n", 1263 GetCommandPrefix()); 1264 } 1265 1266 CommandObject *CommandInterpreter::GetCommandObjectForCommand( 1267 llvm::StringRef &command_string) { 1268 // This function finds the final, lowest-level, alias-resolved command object 1269 // whose 'Execute' function will eventually be invoked by the given command 1270 // line. 1271 1272 CommandObject *cmd_obj = nullptr; 1273 size_t start = command_string.find_first_not_of(k_white_space); 1274 size_t end = 0; 1275 bool done = false; 1276 while (!done) { 1277 if (start != std::string::npos) { 1278 // Get the next word from command_string. 1279 end = command_string.find_first_of(k_white_space, start); 1280 if (end == std::string::npos) 1281 end = command_string.size(); 1282 std::string cmd_word = 1283 std::string(command_string.substr(start, end - start)); 1284 1285 if (cmd_obj == nullptr) 1286 // Since cmd_obj is NULL we are on our first time through this loop. 1287 // Check to see if cmd_word is a valid command or alias. 1288 cmd_obj = GetCommandObject(cmd_word); 1289 else if (cmd_obj->IsMultiwordObject()) { 1290 // Our current object is a multi-word object; see if the cmd_word is a 1291 // valid sub-command for our object. 1292 CommandObject *sub_cmd_obj = 1293 cmd_obj->GetSubcommandObject(cmd_word.c_str()); 1294 if (sub_cmd_obj) 1295 cmd_obj = sub_cmd_obj; 1296 else // cmd_word was not a valid sub-command word, so we are done 1297 done = true; 1298 } else 1299 // We have a cmd_obj and it is not a multi-word object, so we are done. 1300 done = true; 1301 1302 // If we didn't find a valid command object, or our command object is not 1303 // a multi-word object, or we are at the end of the command_string, then 1304 // we are done. Otherwise, find the start of the next word. 1305 1306 if (!cmd_obj || !cmd_obj->IsMultiwordObject() || 1307 end >= command_string.size()) 1308 done = true; 1309 else 1310 start = command_string.find_first_not_of(k_white_space, end); 1311 } else 1312 // Unable to find any more words. 1313 done = true; 1314 } 1315 1316 command_string = command_string.substr(end); 1317 return cmd_obj; 1318 } 1319 1320 static const char *k_valid_command_chars = 1321 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_"; 1322 static void StripLeadingSpaces(std::string &s) { 1323 if (!s.empty()) { 1324 size_t pos = s.find_first_not_of(k_white_space); 1325 if (pos == std::string::npos) 1326 s.clear(); 1327 else if (pos == 0) 1328 return; 1329 s.erase(0, pos); 1330 } 1331 } 1332 1333 static size_t FindArgumentTerminator(const std::string &s) { 1334 const size_t s_len = s.size(); 1335 size_t offset = 0; 1336 while (offset < s_len) { 1337 size_t pos = s.find("--", offset); 1338 if (pos == std::string::npos) 1339 break; 1340 if (pos > 0) { 1341 if (llvm::isSpace(s[pos - 1])) { 1342 // Check if the string ends "\s--" (where \s is a space character) or 1343 // if we have "\s--\s". 1344 if ((pos + 2 >= s_len) || llvm::isSpace(s[pos + 2])) { 1345 return pos; 1346 } 1347 } 1348 } 1349 offset = pos + 2; 1350 } 1351 return std::string::npos; 1352 } 1353 1354 static bool ExtractCommand(std::string &command_string, std::string &command, 1355 std::string &suffix, char "e_char) { 1356 command.clear(); 1357 suffix.clear(); 1358 StripLeadingSpaces(command_string); 1359 1360 bool result = false; 1361 quote_char = '\0'; 1362 1363 if (!command_string.empty()) { 1364 const char first_char = command_string[0]; 1365 if (first_char == '\'' || first_char == '"') { 1366 quote_char = first_char; 1367 const size_t end_quote_pos = command_string.find(quote_char, 1); 1368 if (end_quote_pos == std::string::npos) { 1369 command.swap(command_string); 1370 command_string.erase(); 1371 } else { 1372 command.assign(command_string, 1, end_quote_pos - 1); 1373 if (end_quote_pos + 1 < command_string.size()) 1374 command_string.erase(0, command_string.find_first_not_of( 1375 k_white_space, end_quote_pos + 1)); 1376 else 1377 command_string.erase(); 1378 } 1379 } else { 1380 const size_t first_space_pos = 1381 command_string.find_first_of(k_white_space); 1382 if (first_space_pos == std::string::npos) { 1383 command.swap(command_string); 1384 command_string.erase(); 1385 } else { 1386 command.assign(command_string, 0, first_space_pos); 1387 command_string.erase(0, command_string.find_first_not_of( 1388 k_white_space, first_space_pos)); 1389 } 1390 } 1391 result = true; 1392 } 1393 1394 if (!command.empty()) { 1395 // actual commands can't start with '-' or '_' 1396 if (command[0] != '-' && command[0] != '_') { 1397 size_t pos = command.find_first_not_of(k_valid_command_chars); 1398 if (pos > 0 && pos != std::string::npos) { 1399 suffix.assign(command.begin() + pos, command.end()); 1400 command.erase(pos); 1401 } 1402 } 1403 } 1404 1405 return result; 1406 } 1407 1408 CommandObject *CommandInterpreter::BuildAliasResult( 1409 llvm::StringRef alias_name, std::string &raw_input_string, 1410 std::string &alias_result, CommandReturnObject &result) { 1411 CommandObject *alias_cmd_obj = nullptr; 1412 Args cmd_args(raw_input_string); 1413 alias_cmd_obj = GetCommandObject(alias_name); 1414 StreamString result_str; 1415 1416 if (!alias_cmd_obj || !alias_cmd_obj->IsAlias()) { 1417 alias_result.clear(); 1418 return alias_cmd_obj; 1419 } 1420 std::pair<CommandObjectSP, OptionArgVectorSP> desugared = 1421 ((CommandAlias *)alias_cmd_obj)->Desugar(); 1422 OptionArgVectorSP option_arg_vector_sp = desugared.second; 1423 alias_cmd_obj = desugared.first.get(); 1424 std::string alias_name_str = std::string(alias_name); 1425 if ((cmd_args.GetArgumentCount() == 0) || 1426 (alias_name_str != cmd_args.GetArgumentAtIndex(0))) 1427 cmd_args.Unshift(alias_name_str); 1428 1429 result_str.Printf("%s", alias_cmd_obj->GetCommandName().str().c_str()); 1430 1431 if (!option_arg_vector_sp.get()) { 1432 alias_result = std::string(result_str.GetString()); 1433 return alias_cmd_obj; 1434 } 1435 OptionArgVector *option_arg_vector = option_arg_vector_sp.get(); 1436 1437 int value_type; 1438 std::string option; 1439 std::string value; 1440 for (const auto &entry : *option_arg_vector) { 1441 std::tie(option, value_type, value) = entry; 1442 if (option == "<argument>") { 1443 result_str.Printf(" %s", value.c_str()); 1444 continue; 1445 } 1446 1447 result_str.Printf(" %s", option.c_str()); 1448 if (value_type == OptionParser::eNoArgument) 1449 continue; 1450 1451 if (value_type != OptionParser::eOptionalArgument) 1452 result_str.Printf(" "); 1453 int index = GetOptionArgumentPosition(value.c_str()); 1454 if (index == 0) 1455 result_str.Printf("%s", value.c_str()); 1456 else if (static_cast<size_t>(index) >= cmd_args.GetArgumentCount()) { 1457 1458 result.AppendErrorWithFormat("Not enough arguments provided; you " 1459 "need at least %d arguments to use " 1460 "this alias.\n", 1461 index); 1462 result.SetStatus(eReturnStatusFailed); 1463 return nullptr; 1464 } else { 1465 size_t strpos = raw_input_string.find(cmd_args.GetArgumentAtIndex(index)); 1466 if (strpos != std::string::npos) 1467 raw_input_string = raw_input_string.erase( 1468 strpos, strlen(cmd_args.GetArgumentAtIndex(index))); 1469 result_str.Printf("%s", cmd_args.GetArgumentAtIndex(index)); 1470 } 1471 } 1472 1473 alias_result = std::string(result_str.GetString()); 1474 return alias_cmd_obj; 1475 } 1476 1477 Status CommandInterpreter::PreprocessCommand(std::string &command) { 1478 // The command preprocessor needs to do things to the command line before any 1479 // parsing of arguments or anything else is done. The only current stuff that 1480 // gets preprocessed is anything enclosed in backtick ('`') characters is 1481 // evaluated as an expression and the result of the expression must be a 1482 // scalar that can be substituted into the command. An example would be: 1483 // (lldb) memory read `$rsp + 20` 1484 Status error; // Status for any expressions that might not evaluate 1485 size_t start_backtick; 1486 size_t pos = 0; 1487 while ((start_backtick = command.find('`', pos)) != std::string::npos) { 1488 // Stop if an error was encountered during the previous iteration. 1489 if (error.Fail()) 1490 break; 1491 1492 if (start_backtick > 0 && command[start_backtick - 1] == '\\') { 1493 // The backtick was preceded by a '\' character, remove the slash and 1494 // don't treat the backtick as the start of an expression. 1495 command.erase(start_backtick - 1, 1); 1496 // No need to add one to start_backtick since we just deleted a char. 1497 pos = start_backtick; 1498 continue; 1499 } 1500 1501 const size_t expr_content_start = start_backtick + 1; 1502 const size_t end_backtick = command.find('`', expr_content_start); 1503 1504 if (end_backtick == std::string::npos) { 1505 // Stop if there's no end backtick. 1506 break; 1507 } 1508 1509 if (end_backtick == expr_content_start) { 1510 // Skip over empty expression. (two backticks in a row) 1511 command.erase(start_backtick, 2); 1512 continue; 1513 } 1514 1515 std::string expr_str(command, expr_content_start, 1516 end_backtick - expr_content_start); 1517 1518 ExecutionContext exe_ctx(GetExecutionContext()); 1519 1520 // Get a dummy target to allow for calculator mode while processing 1521 // backticks. This also helps break the infinite loop caused when target is 1522 // null. 1523 Target *exe_target = exe_ctx.GetTargetPtr(); 1524 Target &target = exe_target ? *exe_target : m_debugger.GetDummyTarget(); 1525 1526 ValueObjectSP expr_result_valobj_sp; 1527 1528 EvaluateExpressionOptions options; 1529 options.SetCoerceToId(false); 1530 options.SetUnwindOnError(true); 1531 options.SetIgnoreBreakpoints(true); 1532 options.SetKeepInMemory(false); 1533 options.SetTryAllThreads(true); 1534 options.SetTimeout(llvm::None); 1535 1536 ExpressionResults expr_result = 1537 target.EvaluateExpression(expr_str.c_str(), exe_ctx.GetFramePtr(), 1538 expr_result_valobj_sp, options); 1539 1540 if (expr_result == eExpressionCompleted) { 1541 Scalar scalar; 1542 if (expr_result_valobj_sp) 1543 expr_result_valobj_sp = 1544 expr_result_valobj_sp->GetQualifiedRepresentationIfAvailable( 1545 expr_result_valobj_sp->GetDynamicValueType(), true); 1546 if (expr_result_valobj_sp->ResolveValue(scalar)) { 1547 command.erase(start_backtick, end_backtick - start_backtick + 1); 1548 StreamString value_strm; 1549 const bool show_type = false; 1550 scalar.GetValue(&value_strm, show_type); 1551 size_t value_string_size = value_strm.GetSize(); 1552 if (value_string_size) { 1553 command.insert(start_backtick, std::string(value_strm.GetString())); 1554 pos = start_backtick + value_string_size; 1555 continue; 1556 } else { 1557 error.SetErrorStringWithFormat("expression value didn't result " 1558 "in a scalar value for the " 1559 "expression '%s'", 1560 expr_str.c_str()); 1561 break; 1562 } 1563 } else { 1564 error.SetErrorStringWithFormat("expression value didn't result " 1565 "in a scalar value for the " 1566 "expression '%s'", 1567 expr_str.c_str()); 1568 break; 1569 } 1570 1571 continue; 1572 } 1573 1574 if (expr_result_valobj_sp) 1575 error = expr_result_valobj_sp->GetError(); 1576 1577 if (error.Success()) { 1578 switch (expr_result) { 1579 case eExpressionSetupError: 1580 error.SetErrorStringWithFormat( 1581 "expression setup error for the expression '%s'", expr_str.c_str()); 1582 break; 1583 case eExpressionParseError: 1584 error.SetErrorStringWithFormat( 1585 "expression parse error for the expression '%s'", expr_str.c_str()); 1586 break; 1587 case eExpressionResultUnavailable: 1588 error.SetErrorStringWithFormat( 1589 "expression error fetching result for the expression '%s'", 1590 expr_str.c_str()); 1591 break; 1592 case eExpressionCompleted: 1593 break; 1594 case eExpressionDiscarded: 1595 error.SetErrorStringWithFormat( 1596 "expression discarded for the expression '%s'", expr_str.c_str()); 1597 break; 1598 case eExpressionInterrupted: 1599 error.SetErrorStringWithFormat( 1600 "expression interrupted for the expression '%s'", expr_str.c_str()); 1601 break; 1602 case eExpressionHitBreakpoint: 1603 error.SetErrorStringWithFormat( 1604 "expression hit breakpoint for the expression '%s'", 1605 expr_str.c_str()); 1606 break; 1607 case eExpressionTimedOut: 1608 error.SetErrorStringWithFormat( 1609 "expression timed out for the expression '%s'", expr_str.c_str()); 1610 break; 1611 case eExpressionStoppedForDebug: 1612 error.SetErrorStringWithFormat("expression stop at entry point " 1613 "for debugging for the " 1614 "expression '%s'", 1615 expr_str.c_str()); 1616 break; 1617 case eExpressionThreadVanished: 1618 error.SetErrorStringWithFormat( 1619 "expression thread vanished for the expression '%s'", 1620 expr_str.c_str()); 1621 break; 1622 } 1623 } 1624 } 1625 return error; 1626 } 1627 1628 bool CommandInterpreter::HandleCommand(const char *command_line, 1629 LazyBool lazy_add_to_history, 1630 const ExecutionContext &override_context, 1631 CommandReturnObject &result) { 1632 1633 OverrideExecutionContext(override_context); 1634 bool status = HandleCommand(command_line, lazy_add_to_history, result); 1635 RestoreExecutionContext(); 1636 return status; 1637 } 1638 1639 bool CommandInterpreter::HandleCommand(const char *command_line, 1640 LazyBool lazy_add_to_history, 1641 CommandReturnObject &result) { 1642 1643 std::string command_string(command_line); 1644 std::string original_command_string(command_line); 1645 1646 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_COMMANDS)); 1647 llvm::PrettyStackTraceFormat stack_trace("HandleCommand(command = \"%s\")", 1648 command_line); 1649 1650 LLDB_LOGF(log, "Processing command: %s", command_line); 1651 LLDB_SCOPED_TIMERF("Processing command: %s.", command_line); 1652 1653 if (WasInterrupted()) { 1654 result.AppendError("interrupted"); 1655 result.SetStatus(eReturnStatusFailed); 1656 return false; 1657 } 1658 1659 bool add_to_history; 1660 if (lazy_add_to_history == eLazyBoolCalculate) 1661 add_to_history = (m_command_source_depth == 0); 1662 else 1663 add_to_history = (lazy_add_to_history == eLazyBoolYes); 1664 1665 m_transcript_stream << "(lldb) " << command_line << '\n'; 1666 1667 bool empty_command = false; 1668 bool comment_command = false; 1669 if (command_string.empty()) 1670 empty_command = true; 1671 else { 1672 const char *k_space_characters = "\t\n\v\f\r "; 1673 1674 size_t non_space = command_string.find_first_not_of(k_space_characters); 1675 // Check for empty line or comment line (lines whose first non-space 1676 // character is the comment character for this interpreter) 1677 if (non_space == std::string::npos) 1678 empty_command = true; 1679 else if (command_string[non_space] == m_comment_char) 1680 comment_command = true; 1681 else if (command_string[non_space] == CommandHistory::g_repeat_char) { 1682 llvm::StringRef search_str(command_string); 1683 search_str = search_str.drop_front(non_space); 1684 if (auto hist_str = m_command_history.FindString(search_str)) { 1685 add_to_history = false; 1686 command_string = std::string(*hist_str); 1687 original_command_string = std::string(*hist_str); 1688 } else { 1689 result.AppendErrorWithFormat("Could not find entry: %s in history", 1690 command_string.c_str()); 1691 result.SetStatus(eReturnStatusFailed); 1692 return false; 1693 } 1694 } 1695 } 1696 1697 if (empty_command) { 1698 if (m_command_history.IsEmpty()) { 1699 result.AppendError("empty command"); 1700 result.SetStatus(eReturnStatusFailed); 1701 return false; 1702 } 1703 1704 command_line = m_repeat_command.c_str(); 1705 command_string = command_line; 1706 original_command_string = command_line; 1707 if (m_repeat_command.empty()) { 1708 result.AppendError("No auto repeat."); 1709 result.SetStatus(eReturnStatusFailed); 1710 return false; 1711 } 1712 1713 add_to_history = false; 1714 } else if (comment_command) { 1715 result.SetStatus(eReturnStatusSuccessFinishNoResult); 1716 return true; 1717 } 1718 1719 Status error(PreprocessCommand(command_string)); 1720 1721 if (error.Fail()) { 1722 result.AppendError(error.AsCString()); 1723 result.SetStatus(eReturnStatusFailed); 1724 return false; 1725 } 1726 1727 // Phase 1. 1728 1729 // Before we do ANY kind of argument processing, we need to figure out what 1730 // the real/final command object is for the specified command. This gets 1731 // complicated by the fact that the user could have specified an alias, and, 1732 // in translating the alias, there may also be command options and/or even 1733 // data (including raw text strings) that need to be found and inserted into 1734 // the command line as part of the translation. So this first step is plain 1735 // look-up and replacement, resulting in: 1736 // 1. the command object whose Execute method will actually be called 1737 // 2. a revised command string, with all substitutions and replacements 1738 // taken care of 1739 // From 1 above, we can determine whether the Execute function wants raw 1740 // input or not. 1741 1742 CommandObject *cmd_obj = ResolveCommandImpl(command_string, result); 1743 1744 // Although the user may have abbreviated the command, the command_string now 1745 // has the command expanded to the full name. For example, if the input was 1746 // "br s -n main", command_string is now "breakpoint set -n main". 1747 if (log) { 1748 llvm::StringRef command_name = cmd_obj ? cmd_obj->GetCommandName() : "<not found>"; 1749 LLDB_LOGF(log, "HandleCommand, cmd_obj : '%s'", command_name.str().c_str()); 1750 LLDB_LOGF(log, "HandleCommand, (revised) command_string: '%s'", 1751 command_string.c_str()); 1752 const bool wants_raw_input = 1753 (cmd_obj != nullptr) ? cmd_obj->WantsRawCommandString() : false; 1754 LLDB_LOGF(log, "HandleCommand, wants_raw_input:'%s'", 1755 wants_raw_input ? "True" : "False"); 1756 } 1757 1758 // Phase 2. 1759 // Take care of things like setting up the history command & calling the 1760 // appropriate Execute method on the CommandObject, with the appropriate 1761 // arguments. 1762 1763 if (cmd_obj != nullptr) { 1764 if (add_to_history) { 1765 Args command_args(command_string); 1766 const char *repeat_command = cmd_obj->GetRepeatCommand(command_args, 0); 1767 if (repeat_command != nullptr) 1768 m_repeat_command.assign(repeat_command); 1769 else 1770 m_repeat_command.assign(original_command_string); 1771 1772 m_command_history.AppendString(original_command_string); 1773 } 1774 1775 std::string remainder; 1776 const std::size_t actual_cmd_name_len = cmd_obj->GetCommandName().size(); 1777 if (actual_cmd_name_len < command_string.length()) 1778 remainder = command_string.substr(actual_cmd_name_len); 1779 1780 // Remove any initial spaces 1781 size_t pos = remainder.find_first_not_of(k_white_space); 1782 if (pos != 0 && pos != std::string::npos) 1783 remainder.erase(0, pos); 1784 1785 LLDB_LOGF( 1786 log, "HandleCommand, command line after removing command name(s): '%s'", 1787 remainder.c_str()); 1788 1789 cmd_obj->Execute(remainder.c_str(), result); 1790 } 1791 1792 LLDB_LOGF(log, "HandleCommand, command %s", 1793 (result.Succeeded() ? "succeeded" : "did not succeed")); 1794 1795 m_transcript_stream << result.GetOutputData(); 1796 m_transcript_stream << result.GetErrorData(); 1797 1798 return result.Succeeded(); 1799 } 1800 1801 void CommandInterpreter::HandleCompletionMatches(CompletionRequest &request) { 1802 bool look_for_subcommand = false; 1803 1804 // For any of the command completions a unique match will be a complete word. 1805 1806 if (request.GetParsedLine().GetArgumentCount() == 0) { 1807 // We got nothing on the command line, so return the list of commands 1808 bool include_aliases = true; 1809 StringList new_matches, descriptions; 1810 GetCommandNamesMatchingPartialString("", include_aliases, new_matches, 1811 descriptions); 1812 request.AddCompletions(new_matches, descriptions); 1813 } else if (request.GetCursorIndex() == 0) { 1814 // The cursor is in the first argument, so just do a lookup in the 1815 // dictionary. 1816 StringList new_matches, new_descriptions; 1817 CommandObject *cmd_obj = 1818 GetCommandObject(request.GetParsedLine().GetArgumentAtIndex(0), 1819 &new_matches, &new_descriptions); 1820 1821 if (new_matches.GetSize() && cmd_obj && cmd_obj->IsMultiwordObject() && 1822 new_matches.GetStringAtIndex(0) != nullptr && 1823 strcmp(request.GetParsedLine().GetArgumentAtIndex(0), 1824 new_matches.GetStringAtIndex(0)) == 0) { 1825 if (request.GetParsedLine().GetArgumentCount() != 1) { 1826 look_for_subcommand = true; 1827 new_matches.DeleteStringAtIndex(0); 1828 new_descriptions.DeleteStringAtIndex(0); 1829 request.AppendEmptyArgument(); 1830 } 1831 } 1832 request.AddCompletions(new_matches, new_descriptions); 1833 } 1834 1835 if (request.GetCursorIndex() > 0 || look_for_subcommand) { 1836 // We are completing further on into a commands arguments, so find the 1837 // command and tell it to complete the command. First see if there is a 1838 // matching initial command: 1839 CommandObject *command_object = 1840 GetCommandObject(request.GetParsedLine().GetArgumentAtIndex(0)); 1841 if (command_object) { 1842 request.ShiftArguments(); 1843 command_object->HandleCompletion(request); 1844 } 1845 } 1846 } 1847 1848 void CommandInterpreter::HandleCompletion(CompletionRequest &request) { 1849 1850 // Don't complete comments, and if the line we are completing is just the 1851 // history repeat character, substitute the appropriate history line. 1852 llvm::StringRef first_arg = request.GetParsedLine().GetArgumentAtIndex(0); 1853 1854 if (!first_arg.empty()) { 1855 if (first_arg.front() == m_comment_char) 1856 return; 1857 if (first_arg.front() == CommandHistory::g_repeat_char) { 1858 if (auto hist_str = m_command_history.FindString(first_arg)) 1859 request.AddCompletion(*hist_str, "Previous command history event", 1860 CompletionMode::RewriteLine); 1861 return; 1862 } 1863 } 1864 1865 HandleCompletionMatches(request); 1866 } 1867 1868 llvm::Optional<std::string> 1869 CommandInterpreter::GetAutoSuggestionForCommand(llvm::StringRef line) { 1870 if (line.empty()) 1871 return llvm::None; 1872 const size_t s = m_command_history.GetSize(); 1873 for (int i = s - 1; i >= 0; --i) { 1874 llvm::StringRef entry = m_command_history.GetStringAtIndex(i); 1875 if (entry.consume_front(line)) 1876 return entry.str(); 1877 } 1878 return llvm::None; 1879 } 1880 1881 void CommandInterpreter::UpdatePrompt(llvm::StringRef new_prompt) { 1882 EventSP prompt_change_event_sp( 1883 new Event(eBroadcastBitResetPrompt, new EventDataBytes(new_prompt))); 1884 ; 1885 BroadcastEvent(prompt_change_event_sp); 1886 if (m_command_io_handler_sp) 1887 m_command_io_handler_sp->SetPrompt(new_prompt); 1888 } 1889 1890 bool CommandInterpreter::Confirm(llvm::StringRef message, bool default_answer) { 1891 // Check AutoConfirm first: 1892 if (m_debugger.GetAutoConfirm()) 1893 return default_answer; 1894 1895 IOHandlerConfirm *confirm = 1896 new IOHandlerConfirm(m_debugger, message, default_answer); 1897 IOHandlerSP io_handler_sp(confirm); 1898 m_debugger.RunIOHandlerSync(io_handler_sp); 1899 return confirm->GetResponse(); 1900 } 1901 1902 const CommandAlias * 1903 CommandInterpreter::GetAlias(llvm::StringRef alias_name) const { 1904 OptionArgVectorSP ret_val; 1905 1906 auto pos = m_alias_dict.find(std::string(alias_name)); 1907 if (pos != m_alias_dict.end()) 1908 return (CommandAlias *)pos->second.get(); 1909 1910 return nullptr; 1911 } 1912 1913 bool CommandInterpreter::HasCommands() const { return (!m_command_dict.empty()); } 1914 1915 bool CommandInterpreter::HasAliases() const { return (!m_alias_dict.empty()); } 1916 1917 bool CommandInterpreter::HasUserCommands() const { return (!m_user_dict.empty()); } 1918 1919 bool CommandInterpreter::HasAliasOptions() const { return HasAliases(); } 1920 1921 void CommandInterpreter::BuildAliasCommandArgs(CommandObject *alias_cmd_obj, 1922 const char *alias_name, 1923 Args &cmd_args, 1924 std::string &raw_input_string, 1925 CommandReturnObject &result) { 1926 OptionArgVectorSP option_arg_vector_sp = 1927 GetAlias(alias_name)->GetOptionArguments(); 1928 1929 bool wants_raw_input = alias_cmd_obj->WantsRawCommandString(); 1930 1931 // Make sure that the alias name is the 0th element in cmd_args 1932 std::string alias_name_str = alias_name; 1933 if (alias_name_str != cmd_args.GetArgumentAtIndex(0)) 1934 cmd_args.Unshift(alias_name_str); 1935 1936 Args new_args(alias_cmd_obj->GetCommandName()); 1937 if (new_args.GetArgumentCount() == 2) 1938 new_args.Shift(); 1939 1940 if (option_arg_vector_sp.get()) { 1941 if (wants_raw_input) { 1942 // We have a command that both has command options and takes raw input. 1943 // Make *sure* it has a " -- " in the right place in the 1944 // raw_input_string. 1945 size_t pos = raw_input_string.find(" -- "); 1946 if (pos == std::string::npos) { 1947 // None found; assume it goes at the beginning of the raw input string 1948 raw_input_string.insert(0, " -- "); 1949 } 1950 } 1951 1952 OptionArgVector *option_arg_vector = option_arg_vector_sp.get(); 1953 const size_t old_size = cmd_args.GetArgumentCount(); 1954 std::vector<bool> used(old_size + 1, false); 1955 1956 used[0] = true; 1957 1958 int value_type; 1959 std::string option; 1960 std::string value; 1961 for (const auto &option_entry : *option_arg_vector) { 1962 std::tie(option, value_type, value) = option_entry; 1963 if (option == "<argument>") { 1964 if (!wants_raw_input || (value != "--")) { 1965 // Since we inserted this above, make sure we don't insert it twice 1966 new_args.AppendArgument(value); 1967 } 1968 continue; 1969 } 1970 1971 if (value_type != OptionParser::eOptionalArgument) 1972 new_args.AppendArgument(option); 1973 1974 if (value == "<no-argument>") 1975 continue; 1976 1977 int index = GetOptionArgumentPosition(value.c_str()); 1978 if (index == 0) { 1979 // value was NOT a positional argument; must be a real value 1980 if (value_type != OptionParser::eOptionalArgument) 1981 new_args.AppendArgument(value); 1982 else { 1983 new_args.AppendArgument(option + value); 1984 } 1985 1986 } else if (static_cast<size_t>(index) >= cmd_args.GetArgumentCount()) { 1987 result.AppendErrorWithFormat("Not enough arguments provided; you " 1988 "need at least %d arguments to use " 1989 "this alias.\n", 1990 index); 1991 result.SetStatus(eReturnStatusFailed); 1992 return; 1993 } else { 1994 // Find and remove cmd_args.GetArgumentAtIndex(i) from raw_input_string 1995 size_t strpos = 1996 raw_input_string.find(cmd_args.GetArgumentAtIndex(index)); 1997 if (strpos != std::string::npos) { 1998 raw_input_string = raw_input_string.erase( 1999 strpos, strlen(cmd_args.GetArgumentAtIndex(index))); 2000 } 2001 2002 if (value_type != OptionParser::eOptionalArgument) 2003 new_args.AppendArgument(cmd_args.GetArgumentAtIndex(index)); 2004 else { 2005 new_args.AppendArgument(option + cmd_args.GetArgumentAtIndex(index)); 2006 } 2007 used[index] = true; 2008 } 2009 } 2010 2011 for (auto entry : llvm::enumerate(cmd_args.entries())) { 2012 if (!used[entry.index()] && !wants_raw_input) 2013 new_args.AppendArgument(entry.value().ref()); 2014 } 2015 2016 cmd_args.Clear(); 2017 cmd_args.SetArguments(new_args.GetArgumentCount(), 2018 new_args.GetConstArgumentVector()); 2019 } else { 2020 result.SetStatus(eReturnStatusSuccessFinishNoResult); 2021 // This alias was not created with any options; nothing further needs to be 2022 // done, unless it is a command that wants raw input, in which case we need 2023 // to clear the rest of the data from cmd_args, since its in the raw input 2024 // string. 2025 if (wants_raw_input) { 2026 cmd_args.Clear(); 2027 cmd_args.SetArguments(new_args.GetArgumentCount(), 2028 new_args.GetConstArgumentVector()); 2029 } 2030 return; 2031 } 2032 2033 result.SetStatus(eReturnStatusSuccessFinishNoResult); 2034 return; 2035 } 2036 2037 int CommandInterpreter::GetOptionArgumentPosition(const char *in_string) { 2038 int position = 0; // Any string that isn't an argument position, i.e. '%' 2039 // followed by an integer, gets a position 2040 // of zero. 2041 2042 const char *cptr = in_string; 2043 2044 // Does it start with '%' 2045 if (cptr[0] == '%') { 2046 ++cptr; 2047 2048 // Is the rest of it entirely digits? 2049 if (isdigit(cptr[0])) { 2050 const char *start = cptr; 2051 while (isdigit(cptr[0])) 2052 ++cptr; 2053 2054 // We've gotten to the end of the digits; are we at the end of the 2055 // string? 2056 if (cptr[0] == '\0') 2057 position = atoi(start); 2058 } 2059 } 2060 2061 return position; 2062 } 2063 2064 static void GetHomeInitFile(llvm::SmallVectorImpl<char> &init_file, 2065 llvm::StringRef suffix = {}) { 2066 std::string init_file_name = ".lldbinit"; 2067 if (!suffix.empty()) { 2068 init_file_name.append("-"); 2069 init_file_name.append(suffix.str()); 2070 } 2071 2072 FileSystem::Instance().GetHomeDirectory(init_file); 2073 llvm::sys::path::append(init_file, init_file_name); 2074 2075 FileSystem::Instance().Resolve(init_file); 2076 } 2077 2078 static void GetHomeREPLInitFile(llvm::SmallVectorImpl<char> &init_file) { 2079 LanguageSet repl_languages = Language::GetLanguagesSupportingREPLs(); 2080 LanguageType language = eLanguageTypeUnknown; 2081 if (auto main_repl_language = repl_languages.GetSingularLanguage()) 2082 language = *main_repl_language; 2083 else 2084 return; 2085 2086 std::string init_file_name = 2087 (llvm::Twine(".lldbinit-") + 2088 llvm::Twine(Language::GetNameForLanguageType(language)) + 2089 llvm::Twine("-repl")) 2090 .str(); 2091 FileSystem::Instance().GetHomeDirectory(init_file); 2092 llvm::sys::path::append(init_file, init_file_name); 2093 FileSystem::Instance().Resolve(init_file); 2094 } 2095 2096 static void GetCwdInitFile(llvm::SmallVectorImpl<char> &init_file) { 2097 llvm::StringRef s = ".lldbinit"; 2098 init_file.assign(s.begin(), s.end()); 2099 FileSystem::Instance().Resolve(init_file); 2100 } 2101 2102 static LoadCWDlldbinitFile ShouldLoadCwdInitFile() { 2103 lldb::TargetPropertiesSP properties = Target::GetGlobalProperties(); 2104 if (!properties) 2105 return eLoadCWDlldbinitFalse; 2106 return properties->GetLoadCWDlldbinitFile(); 2107 } 2108 2109 void CommandInterpreter::SourceInitFile(FileSpec file, 2110 CommandReturnObject &result) { 2111 assert(!m_skip_lldbinit_files); 2112 2113 if (!FileSystem::Instance().Exists(file)) { 2114 result.SetStatus(eReturnStatusSuccessFinishNoResult); 2115 return; 2116 } 2117 2118 // Use HandleCommand to 'source' the given file; this will do the actual 2119 // broadcasting of the commands back to any appropriate listener (see 2120 // CommandObjectSource::Execute for more details). 2121 const bool saved_batch = SetBatchCommandMode(true); 2122 CommandInterpreterRunOptions options; 2123 options.SetSilent(true); 2124 options.SetPrintErrors(true); 2125 options.SetStopOnError(false); 2126 options.SetStopOnContinue(true); 2127 HandleCommandsFromFile(file, options, result); 2128 SetBatchCommandMode(saved_batch); 2129 } 2130 2131 void CommandInterpreter::SourceInitFileCwd(CommandReturnObject &result) { 2132 if (m_skip_lldbinit_files) { 2133 result.SetStatus(eReturnStatusSuccessFinishNoResult); 2134 return; 2135 } 2136 2137 llvm::SmallString<128> init_file; 2138 GetCwdInitFile(init_file); 2139 if (!FileSystem::Instance().Exists(init_file)) { 2140 result.SetStatus(eReturnStatusSuccessFinishNoResult); 2141 return; 2142 } 2143 2144 LoadCWDlldbinitFile should_load = ShouldLoadCwdInitFile(); 2145 2146 switch (should_load) { 2147 case eLoadCWDlldbinitFalse: 2148 result.SetStatus(eReturnStatusSuccessFinishNoResult); 2149 break; 2150 case eLoadCWDlldbinitTrue: 2151 SourceInitFile(FileSpec(init_file.str()), result); 2152 break; 2153 case eLoadCWDlldbinitWarn: { 2154 llvm::SmallString<128> home_init_file; 2155 GetHomeInitFile(home_init_file); 2156 if (llvm::sys::path::parent_path(init_file) == 2157 llvm::sys::path::parent_path(home_init_file)) { 2158 result.SetStatus(eReturnStatusSuccessFinishNoResult); 2159 } else { 2160 result.AppendError(InitFileWarning); 2161 result.SetStatus(eReturnStatusFailed); 2162 } 2163 } 2164 } 2165 } 2166 2167 /// We will first see if there is an application specific ".lldbinit" file 2168 /// whose name is "~/.lldbinit" followed by a "-" and the name of the program. 2169 /// If this file doesn't exist, we fall back to the REPL init file or the 2170 /// default home init file in "~/.lldbinit". 2171 void CommandInterpreter::SourceInitFileHome(CommandReturnObject &result, 2172 bool is_repl) { 2173 if (m_skip_lldbinit_files) { 2174 result.SetStatus(eReturnStatusSuccessFinishNoResult); 2175 return; 2176 } 2177 2178 llvm::SmallString<128> init_file; 2179 2180 if (is_repl) 2181 GetHomeREPLInitFile(init_file); 2182 2183 if (init_file.empty()) 2184 GetHomeInitFile(init_file); 2185 2186 if (!m_skip_app_init_files) { 2187 llvm::StringRef program_name = 2188 HostInfo::GetProgramFileSpec().GetFilename().GetStringRef(); 2189 llvm::SmallString<128> program_init_file; 2190 GetHomeInitFile(program_init_file, program_name); 2191 if (FileSystem::Instance().Exists(program_init_file)) 2192 init_file = program_init_file; 2193 } 2194 2195 SourceInitFile(FileSpec(init_file.str()), result); 2196 } 2197 2198 const char *CommandInterpreter::GetCommandPrefix() { 2199 const char *prefix = GetDebugger().GetIOHandlerCommandPrefix(); 2200 return prefix == nullptr ? "" : prefix; 2201 } 2202 2203 PlatformSP CommandInterpreter::GetPlatform(bool prefer_target_platform) { 2204 PlatformSP platform_sp; 2205 if (prefer_target_platform) { 2206 ExecutionContext exe_ctx(GetExecutionContext()); 2207 Target *target = exe_ctx.GetTargetPtr(); 2208 if (target) 2209 platform_sp = target->GetPlatform(); 2210 } 2211 2212 if (!platform_sp) 2213 platform_sp = m_debugger.GetPlatformList().GetSelectedPlatform(); 2214 return platform_sp; 2215 } 2216 2217 bool CommandInterpreter::DidProcessStopAbnormally() const { 2218 auto exe_ctx = GetExecutionContext(); 2219 TargetSP target_sp = exe_ctx.GetTargetSP(); 2220 if (!target_sp) 2221 return false; 2222 2223 ProcessSP process_sp(target_sp->GetProcessSP()); 2224 if (!process_sp) 2225 return false; 2226 2227 if (eStateStopped != process_sp->GetState()) 2228 return false; 2229 2230 for (const auto &thread_sp : process_sp->GetThreadList().Threads()) { 2231 StopInfoSP stop_info = thread_sp->GetStopInfo(); 2232 if (!stop_info) 2233 return false; 2234 2235 const StopReason reason = stop_info->GetStopReason(); 2236 if (reason == eStopReasonException || reason == eStopReasonInstrumentation) 2237 return true; 2238 2239 if (reason == eStopReasonSignal) { 2240 const auto stop_signal = static_cast<int32_t>(stop_info->GetValue()); 2241 UnixSignalsSP signals_sp = process_sp->GetUnixSignals(); 2242 if (!signals_sp || !signals_sp->SignalIsValid(stop_signal)) 2243 // The signal is unknown, treat it as abnormal. 2244 return true; 2245 2246 const auto sigint_num = signals_sp->GetSignalNumberFromName("SIGINT"); 2247 const auto sigstop_num = signals_sp->GetSignalNumberFromName("SIGSTOP"); 2248 if ((stop_signal != sigint_num) && (stop_signal != sigstop_num)) 2249 // The signal very likely implies a crash. 2250 return true; 2251 } 2252 } 2253 2254 return false; 2255 } 2256 2257 void 2258 CommandInterpreter::HandleCommands(const StringList &commands, 2259 const ExecutionContext &override_context, 2260 const CommandInterpreterRunOptions &options, 2261 CommandReturnObject &result) { 2262 2263 OverrideExecutionContext(override_context); 2264 HandleCommands(commands, options, result); 2265 RestoreExecutionContext(); 2266 } 2267 2268 void CommandInterpreter::HandleCommands(const StringList &commands, 2269 const CommandInterpreterRunOptions &options, 2270 CommandReturnObject &result) { 2271 size_t num_lines = commands.GetSize(); 2272 2273 // If we are going to continue past a "continue" then we need to run the 2274 // commands synchronously. Make sure you reset this value anywhere you return 2275 // from the function. 2276 2277 bool old_async_execution = m_debugger.GetAsyncExecution(); 2278 2279 if (!options.GetStopOnContinue()) { 2280 m_debugger.SetAsyncExecution(false); 2281 } 2282 2283 for (size_t idx = 0; idx < num_lines && !WasInterrupted(); idx++) { 2284 const char *cmd = commands.GetStringAtIndex(idx); 2285 if (cmd[0] == '\0') 2286 continue; 2287 2288 if (options.GetEchoCommands()) { 2289 // TODO: Add Stream support. 2290 result.AppendMessageWithFormat("%s %s\n", 2291 m_debugger.GetPrompt().str().c_str(), cmd); 2292 } 2293 2294 CommandReturnObject tmp_result(m_debugger.GetUseColor()); 2295 tmp_result.SetInteractive(result.GetInteractive()); 2296 2297 // We might call into a regex or alias command, in which case the 2298 // add_to_history will get lost. This m_command_source_depth dingus is the 2299 // way we turn off adding to the history in that case, so set it up here. 2300 if (!options.GetAddToHistory()) 2301 m_command_source_depth++; 2302 bool success = HandleCommand(cmd, options.m_add_to_history, tmp_result); 2303 if (!options.GetAddToHistory()) 2304 m_command_source_depth--; 2305 2306 if (options.GetPrintResults()) { 2307 if (tmp_result.Succeeded()) 2308 result.AppendMessage(tmp_result.GetOutputData()); 2309 } 2310 2311 if (!success || !tmp_result.Succeeded()) { 2312 llvm::StringRef error_msg = tmp_result.GetErrorData(); 2313 if (error_msg.empty()) 2314 error_msg = "<unknown error>.\n"; 2315 if (options.GetStopOnError()) { 2316 result.AppendErrorWithFormat( 2317 "Aborting reading of commands after command #%" PRIu64 2318 ": '%s' failed with %s", 2319 (uint64_t)idx, cmd, error_msg.str().c_str()); 2320 result.SetStatus(eReturnStatusFailed); 2321 m_debugger.SetAsyncExecution(old_async_execution); 2322 return; 2323 } else if (options.GetPrintResults()) { 2324 result.AppendMessageWithFormat( 2325 "Command #%" PRIu64 " '%s' failed with %s", (uint64_t)idx + 1, cmd, 2326 error_msg.str().c_str()); 2327 } 2328 } 2329 2330 if (result.GetImmediateOutputStream()) 2331 result.GetImmediateOutputStream()->Flush(); 2332 2333 if (result.GetImmediateErrorStream()) 2334 result.GetImmediateErrorStream()->Flush(); 2335 2336 // N.B. Can't depend on DidChangeProcessState, because the state coming 2337 // into the command execution could be running (for instance in Breakpoint 2338 // Commands. So we check the return value to see if it is has running in 2339 // it. 2340 if ((tmp_result.GetStatus() == eReturnStatusSuccessContinuingNoResult) || 2341 (tmp_result.GetStatus() == eReturnStatusSuccessContinuingResult)) { 2342 if (options.GetStopOnContinue()) { 2343 // If we caused the target to proceed, and we're going to stop in that 2344 // case, set the status in our real result before returning. This is 2345 // an error if the continue was not the last command in the set of 2346 // commands to be run. 2347 if (idx != num_lines - 1) 2348 result.AppendErrorWithFormat( 2349 "Aborting reading of commands after command #%" PRIu64 2350 ": '%s' continued the target.\n", 2351 (uint64_t)idx + 1, cmd); 2352 else 2353 result.AppendMessageWithFormat("Command #%" PRIu64 2354 " '%s' continued the target.\n", 2355 (uint64_t)idx + 1, cmd); 2356 2357 result.SetStatus(tmp_result.GetStatus()); 2358 m_debugger.SetAsyncExecution(old_async_execution); 2359 2360 return; 2361 } 2362 } 2363 2364 // Also check for "stop on crash here: 2365 if (tmp_result.GetDidChangeProcessState() && options.GetStopOnCrash() && 2366 DidProcessStopAbnormally()) { 2367 if (idx != num_lines - 1) 2368 result.AppendErrorWithFormat( 2369 "Aborting reading of commands after command #%" PRIu64 2370 ": '%s' stopped with a signal or exception.\n", 2371 (uint64_t)idx + 1, cmd); 2372 else 2373 result.AppendMessageWithFormat( 2374 "Command #%" PRIu64 " '%s' stopped with a signal or exception.\n", 2375 (uint64_t)idx + 1, cmd); 2376 2377 result.SetStatus(tmp_result.GetStatus()); 2378 m_debugger.SetAsyncExecution(old_async_execution); 2379 2380 return; 2381 } 2382 } 2383 2384 result.SetStatus(eReturnStatusSuccessFinishResult); 2385 m_debugger.SetAsyncExecution(old_async_execution); 2386 2387 return; 2388 } 2389 2390 // Make flags that we can pass into the IOHandler so our delegates can do the 2391 // right thing 2392 enum { 2393 eHandleCommandFlagStopOnContinue = (1u << 0), 2394 eHandleCommandFlagStopOnError = (1u << 1), 2395 eHandleCommandFlagEchoCommand = (1u << 2), 2396 eHandleCommandFlagEchoCommentCommand = (1u << 3), 2397 eHandleCommandFlagPrintResult = (1u << 4), 2398 eHandleCommandFlagPrintErrors = (1u << 5), 2399 eHandleCommandFlagStopOnCrash = (1u << 6) 2400 }; 2401 2402 void CommandInterpreter::HandleCommandsFromFile( 2403 FileSpec &cmd_file, const ExecutionContext &context, 2404 const CommandInterpreterRunOptions &options, CommandReturnObject &result) { 2405 OverrideExecutionContext(context); 2406 HandleCommandsFromFile(cmd_file, options, result); 2407 RestoreExecutionContext(); 2408 } 2409 2410 void CommandInterpreter::HandleCommandsFromFile(FileSpec &cmd_file, 2411 const CommandInterpreterRunOptions &options, CommandReturnObject &result) { 2412 if (!FileSystem::Instance().Exists(cmd_file)) { 2413 result.AppendErrorWithFormat( 2414 "Error reading commands from file %s - file not found.\n", 2415 cmd_file.GetFilename().AsCString("<Unknown>")); 2416 result.SetStatus(eReturnStatusFailed); 2417 return; 2418 } 2419 2420 std::string cmd_file_path = cmd_file.GetPath(); 2421 auto input_file_up = 2422 FileSystem::Instance().Open(cmd_file, File::eOpenOptionRead); 2423 if (!input_file_up) { 2424 std::string error = llvm::toString(input_file_up.takeError()); 2425 result.AppendErrorWithFormatv( 2426 "error: an error occurred read file '{0}': {1}\n", cmd_file_path, 2427 llvm::fmt_consume(input_file_up.takeError())); 2428 result.SetStatus(eReturnStatusFailed); 2429 return; 2430 } 2431 FileSP input_file_sp = FileSP(std::move(input_file_up.get())); 2432 2433 Debugger &debugger = GetDebugger(); 2434 2435 uint32_t flags = 0; 2436 2437 if (options.m_stop_on_continue == eLazyBoolCalculate) { 2438 if (m_command_source_flags.empty()) { 2439 // Stop on continue by default 2440 flags |= eHandleCommandFlagStopOnContinue; 2441 } else if (m_command_source_flags.back() & 2442 eHandleCommandFlagStopOnContinue) { 2443 flags |= eHandleCommandFlagStopOnContinue; 2444 } 2445 } else if (options.m_stop_on_continue == eLazyBoolYes) { 2446 flags |= eHandleCommandFlagStopOnContinue; 2447 } 2448 2449 if (options.m_stop_on_error == eLazyBoolCalculate) { 2450 if (m_command_source_flags.empty()) { 2451 if (GetStopCmdSourceOnError()) 2452 flags |= eHandleCommandFlagStopOnError; 2453 } else if (m_command_source_flags.back() & eHandleCommandFlagStopOnError) { 2454 flags |= eHandleCommandFlagStopOnError; 2455 } 2456 } else if (options.m_stop_on_error == eLazyBoolYes) { 2457 flags |= eHandleCommandFlagStopOnError; 2458 } 2459 2460 // stop-on-crash can only be set, if it is present in all levels of 2461 // pushed flag sets. 2462 if (options.GetStopOnCrash()) { 2463 if (m_command_source_flags.empty()) { 2464 flags |= eHandleCommandFlagStopOnCrash; 2465 } else if (m_command_source_flags.back() & eHandleCommandFlagStopOnCrash) { 2466 flags |= eHandleCommandFlagStopOnCrash; 2467 } 2468 } 2469 2470 if (options.m_echo_commands == eLazyBoolCalculate) { 2471 if (m_command_source_flags.empty()) { 2472 // Echo command by default 2473 flags |= eHandleCommandFlagEchoCommand; 2474 } else if (m_command_source_flags.back() & eHandleCommandFlagEchoCommand) { 2475 flags |= eHandleCommandFlagEchoCommand; 2476 } 2477 } else if (options.m_echo_commands == eLazyBoolYes) { 2478 flags |= eHandleCommandFlagEchoCommand; 2479 } 2480 2481 // We will only ever ask for this flag, if we echo commands in general. 2482 if (options.m_echo_comment_commands == eLazyBoolCalculate) { 2483 if (m_command_source_flags.empty()) { 2484 // Echo comments by default 2485 flags |= eHandleCommandFlagEchoCommentCommand; 2486 } else if (m_command_source_flags.back() & 2487 eHandleCommandFlagEchoCommentCommand) { 2488 flags |= eHandleCommandFlagEchoCommentCommand; 2489 } 2490 } else if (options.m_echo_comment_commands == eLazyBoolYes) { 2491 flags |= eHandleCommandFlagEchoCommentCommand; 2492 } 2493 2494 if (options.m_print_results == eLazyBoolCalculate) { 2495 if (m_command_source_flags.empty()) { 2496 // Print output by default 2497 flags |= eHandleCommandFlagPrintResult; 2498 } else if (m_command_source_flags.back() & eHandleCommandFlagPrintResult) { 2499 flags |= eHandleCommandFlagPrintResult; 2500 } 2501 } else if (options.m_print_results == eLazyBoolYes) { 2502 flags |= eHandleCommandFlagPrintResult; 2503 } 2504 2505 if (options.m_print_errors == eLazyBoolCalculate) { 2506 if (m_command_source_flags.empty()) { 2507 // Print output by default 2508 flags |= eHandleCommandFlagPrintErrors; 2509 } else if (m_command_source_flags.back() & eHandleCommandFlagPrintErrors) { 2510 flags |= eHandleCommandFlagPrintErrors; 2511 } 2512 } else if (options.m_print_errors == eLazyBoolYes) { 2513 flags |= eHandleCommandFlagPrintErrors; 2514 } 2515 2516 if (flags & eHandleCommandFlagPrintResult) { 2517 debugger.GetOutputFile().Printf("Executing commands in '%s'.\n", 2518 cmd_file_path.c_str()); 2519 } 2520 2521 // Used for inheriting the right settings when "command source" might 2522 // have nested "command source" commands 2523 lldb::StreamFileSP empty_stream_sp; 2524 m_command_source_flags.push_back(flags); 2525 IOHandlerSP io_handler_sp(new IOHandlerEditline( 2526 debugger, IOHandler::Type::CommandInterpreter, input_file_sp, 2527 empty_stream_sp, // Pass in an empty stream so we inherit the top 2528 // input reader output stream 2529 empty_stream_sp, // Pass in an empty stream so we inherit the top 2530 // input reader error stream 2531 flags, 2532 nullptr, // Pass in NULL for "editline_name" so no history is saved, 2533 // or written 2534 debugger.GetPrompt(), llvm::StringRef(), 2535 false, // Not multi-line 2536 debugger.GetUseColor(), 0, *this, nullptr)); 2537 const bool old_async_execution = debugger.GetAsyncExecution(); 2538 2539 // Set synchronous execution if we are not stopping on continue 2540 if ((flags & eHandleCommandFlagStopOnContinue) == 0) 2541 debugger.SetAsyncExecution(false); 2542 2543 m_command_source_depth++; 2544 m_command_source_dirs.push_back(cmd_file.CopyByRemovingLastPathComponent()); 2545 2546 debugger.RunIOHandlerSync(io_handler_sp); 2547 if (!m_command_source_flags.empty()) 2548 m_command_source_flags.pop_back(); 2549 2550 m_command_source_dirs.pop_back(); 2551 m_command_source_depth--; 2552 2553 result.SetStatus(eReturnStatusSuccessFinishNoResult); 2554 debugger.SetAsyncExecution(old_async_execution); 2555 } 2556 2557 bool CommandInterpreter::GetSynchronous() { return m_synchronous_execution; } 2558 2559 void CommandInterpreter::SetSynchronous(bool value) { 2560 // Asynchronous mode is not supported during reproducer replay. 2561 if (repro::Reproducer::Instance().GetLoader()) 2562 return; 2563 m_synchronous_execution = value; 2564 } 2565 2566 void CommandInterpreter::OutputFormattedHelpText(Stream &strm, 2567 llvm::StringRef prefix, 2568 llvm::StringRef help_text) { 2569 const uint32_t max_columns = m_debugger.GetTerminalWidth(); 2570 2571 size_t line_width_max = max_columns - prefix.size(); 2572 if (line_width_max < 16) 2573 line_width_max = help_text.size() + prefix.size(); 2574 2575 strm.IndentMore(prefix.size()); 2576 bool prefixed_yet = false; 2577 while (!help_text.empty()) { 2578 // Prefix the first line, indent subsequent lines to line up 2579 if (!prefixed_yet) { 2580 strm << prefix; 2581 prefixed_yet = true; 2582 } else 2583 strm.Indent(); 2584 2585 // Never print more than the maximum on one line. 2586 llvm::StringRef this_line = help_text.substr(0, line_width_max); 2587 2588 // Always break on an explicit newline. 2589 std::size_t first_newline = this_line.find_first_of("\n"); 2590 2591 // Don't break on space/tab unless the text is too long to fit on one line. 2592 std::size_t last_space = llvm::StringRef::npos; 2593 if (this_line.size() != help_text.size()) 2594 last_space = this_line.find_last_of(" \t"); 2595 2596 // Break at whichever condition triggered first. 2597 this_line = this_line.substr(0, std::min(first_newline, last_space)); 2598 strm.PutCString(this_line); 2599 strm.EOL(); 2600 2601 // Remove whitespace / newlines after breaking. 2602 help_text = help_text.drop_front(this_line.size()).ltrim(); 2603 } 2604 strm.IndentLess(prefix.size()); 2605 } 2606 2607 void CommandInterpreter::OutputFormattedHelpText(Stream &strm, 2608 llvm::StringRef word_text, 2609 llvm::StringRef separator, 2610 llvm::StringRef help_text, 2611 size_t max_word_len) { 2612 StreamString prefix_stream; 2613 prefix_stream.Printf(" %-*s %*s ", (int)max_word_len, word_text.data(), 2614 (int)separator.size(), separator.data()); 2615 OutputFormattedHelpText(strm, prefix_stream.GetString(), help_text); 2616 } 2617 2618 void CommandInterpreter::OutputHelpText(Stream &strm, llvm::StringRef word_text, 2619 llvm::StringRef separator, 2620 llvm::StringRef help_text, 2621 uint32_t max_word_len) { 2622 int indent_size = max_word_len + separator.size() + 2; 2623 2624 strm.IndentMore(indent_size); 2625 2626 StreamString text_strm; 2627 text_strm.Printf("%-*s ", (int)max_word_len, word_text.data()); 2628 text_strm << separator << " " << help_text; 2629 2630 const uint32_t max_columns = m_debugger.GetTerminalWidth(); 2631 2632 llvm::StringRef text = text_strm.GetString(); 2633 2634 uint32_t chars_left = max_columns; 2635 2636 auto nextWordLength = [](llvm::StringRef S) { 2637 size_t pos = S.find(' '); 2638 return pos == llvm::StringRef::npos ? S.size() : pos; 2639 }; 2640 2641 while (!text.empty()) { 2642 if (text.front() == '\n' || 2643 (text.front() == ' ' && nextWordLength(text.ltrim(' ')) > chars_left)) { 2644 strm.EOL(); 2645 strm.Indent(); 2646 chars_left = max_columns - indent_size; 2647 if (text.front() == '\n') 2648 text = text.drop_front(); 2649 else 2650 text = text.ltrim(' '); 2651 } else { 2652 strm.PutChar(text.front()); 2653 --chars_left; 2654 text = text.drop_front(); 2655 } 2656 } 2657 2658 strm.EOL(); 2659 strm.IndentLess(indent_size); 2660 } 2661 2662 void CommandInterpreter::FindCommandsForApropos( 2663 llvm::StringRef search_word, StringList &commands_found, 2664 StringList &commands_help, CommandObject::CommandMap &command_map) { 2665 CommandObject::CommandMap::const_iterator pos; 2666 2667 for (pos = command_map.begin(); pos != command_map.end(); ++pos) { 2668 llvm::StringRef command_name = pos->first; 2669 CommandObject *cmd_obj = pos->second.get(); 2670 2671 const bool search_short_help = true; 2672 const bool search_long_help = false; 2673 const bool search_syntax = false; 2674 const bool search_options = false; 2675 if (command_name.contains_lower(search_word) || 2676 cmd_obj->HelpTextContainsWord(search_word, search_short_help, 2677 search_long_help, search_syntax, 2678 search_options)) { 2679 commands_found.AppendString(cmd_obj->GetCommandName()); 2680 commands_help.AppendString(cmd_obj->GetHelp()); 2681 } 2682 2683 if (cmd_obj->IsMultiwordObject()) { 2684 CommandObjectMultiword *cmd_multiword = cmd_obj->GetAsMultiwordCommand(); 2685 FindCommandsForApropos(search_word, commands_found, commands_help, 2686 cmd_multiword->GetSubcommandDictionary()); 2687 } 2688 } 2689 } 2690 2691 void CommandInterpreter::FindCommandsForApropos(llvm::StringRef search_word, 2692 StringList &commands_found, 2693 StringList &commands_help, 2694 bool search_builtin_commands, 2695 bool search_user_commands, 2696 bool search_alias_commands) { 2697 CommandObject::CommandMap::const_iterator pos; 2698 2699 if (search_builtin_commands) 2700 FindCommandsForApropos(search_word, commands_found, commands_help, 2701 m_command_dict); 2702 2703 if (search_user_commands) 2704 FindCommandsForApropos(search_word, commands_found, commands_help, 2705 m_user_dict); 2706 2707 if (search_alias_commands) 2708 FindCommandsForApropos(search_word, commands_found, commands_help, 2709 m_alias_dict); 2710 } 2711 2712 ExecutionContext CommandInterpreter::GetExecutionContext() const { 2713 return !m_overriden_exe_contexts.empty() 2714 ? m_overriden_exe_contexts.top() 2715 : m_debugger.GetSelectedExecutionContext(); 2716 } 2717 2718 void CommandInterpreter::OverrideExecutionContext( 2719 const ExecutionContext &override_context) { 2720 m_overriden_exe_contexts.push(override_context); 2721 } 2722 2723 void CommandInterpreter::RestoreExecutionContext() { 2724 if (!m_overriden_exe_contexts.empty()) 2725 m_overriden_exe_contexts.pop(); 2726 } 2727 2728 void CommandInterpreter::GetProcessOutput() { 2729 if (ProcessSP process_sp = GetExecutionContext().GetProcessSP()) 2730 m_debugger.FlushProcessOutput(*process_sp, /*flush_stdout*/ true, 2731 /*flush_stderr*/ true); 2732 } 2733 2734 void CommandInterpreter::StartHandlingCommand() { 2735 auto idle_state = CommandHandlingState::eIdle; 2736 if (m_command_state.compare_exchange_strong( 2737 idle_state, CommandHandlingState::eInProgress)) 2738 lldbassert(m_iohandler_nesting_level == 0); 2739 else 2740 lldbassert(m_iohandler_nesting_level > 0); 2741 ++m_iohandler_nesting_level; 2742 } 2743 2744 void CommandInterpreter::FinishHandlingCommand() { 2745 lldbassert(m_iohandler_nesting_level > 0); 2746 if (--m_iohandler_nesting_level == 0) { 2747 auto prev_state = m_command_state.exchange(CommandHandlingState::eIdle); 2748 lldbassert(prev_state != CommandHandlingState::eIdle); 2749 } 2750 } 2751 2752 bool CommandInterpreter::InterruptCommand() { 2753 auto in_progress = CommandHandlingState::eInProgress; 2754 return m_command_state.compare_exchange_strong( 2755 in_progress, CommandHandlingState::eInterrupted); 2756 } 2757 2758 bool CommandInterpreter::WasInterrupted() const { 2759 bool was_interrupted = 2760 (m_command_state == CommandHandlingState::eInterrupted); 2761 lldbassert(!was_interrupted || m_iohandler_nesting_level > 0); 2762 return was_interrupted; 2763 } 2764 2765 void CommandInterpreter::PrintCommandOutput(Stream &stream, 2766 llvm::StringRef str) { 2767 // Split the output into lines and poll for interrupt requests 2768 const char *data = str.data(); 2769 size_t size = str.size(); 2770 while (size > 0 && !WasInterrupted()) { 2771 size_t chunk_size = 0; 2772 for (; chunk_size < size; ++chunk_size) { 2773 lldbassert(data[chunk_size] != '\0'); 2774 if (data[chunk_size] == '\n') { 2775 ++chunk_size; 2776 break; 2777 } 2778 } 2779 chunk_size = stream.Write(data, chunk_size); 2780 lldbassert(size >= chunk_size); 2781 data += chunk_size; 2782 size -= chunk_size; 2783 } 2784 if (size > 0) { 2785 stream.Printf("\n... Interrupted.\n"); 2786 } 2787 } 2788 2789 bool CommandInterpreter::EchoCommandNonInteractive( 2790 llvm::StringRef line, const Flags &io_handler_flags) const { 2791 if (!io_handler_flags.Test(eHandleCommandFlagEchoCommand)) 2792 return false; 2793 2794 llvm::StringRef command = line.trim(); 2795 if (command.empty()) 2796 return true; 2797 2798 if (command.front() == m_comment_char) 2799 return io_handler_flags.Test(eHandleCommandFlagEchoCommentCommand); 2800 2801 return true; 2802 } 2803 2804 void CommandInterpreter::IOHandlerInputComplete(IOHandler &io_handler, 2805 std::string &line) { 2806 // If we were interrupted, bail out... 2807 if (WasInterrupted()) 2808 return; 2809 2810 const bool is_interactive = io_handler.GetIsInteractive(); 2811 if (!is_interactive) { 2812 // When we are not interactive, don't execute blank lines. This will happen 2813 // sourcing a commands file. We don't want blank lines to repeat the 2814 // previous command and cause any errors to occur (like redefining an 2815 // alias, get an error and stop parsing the commands file). 2816 if (line.empty()) 2817 return; 2818 2819 // When using a non-interactive file handle (like when sourcing commands 2820 // from a file) we need to echo the command out so we don't just see the 2821 // command output and no command... 2822 if (EchoCommandNonInteractive(line, io_handler.GetFlags())) 2823 io_handler.GetOutputStreamFileSP()->Printf( 2824 "%s%s\n", io_handler.GetPrompt(), line.c_str()); 2825 } 2826 2827 StartHandlingCommand(); 2828 2829 OverrideExecutionContext(m_debugger.GetSelectedExecutionContext()); 2830 auto finalize = llvm::make_scope_exit([this]() { 2831 RestoreExecutionContext(); 2832 }); 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