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