1 //===-- CommandObject.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 "lldb/Interpreter/CommandObject.h" 10 11 #include <map> 12 #include <sstream> 13 #include <string> 14 15 #include <cctype> 16 #include <cstdlib> 17 18 #include "lldb/Core/Address.h" 19 #include "lldb/Interpreter/Options.h" 20 #include "lldb/Utility/ArchSpec.h" 21 #include "llvm/ADT/ScopeExit.h" 22 23 // These are for the Sourcename completers. 24 // FIXME: Make a separate file for the completers. 25 #include "lldb/Core/FileSpecList.h" 26 #include "lldb/DataFormatters/FormatManager.h" 27 #include "lldb/Target/Process.h" 28 #include "lldb/Target/Target.h" 29 #include "lldb/Utility/FileSpec.h" 30 31 #include "lldb/Target/Language.h" 32 33 #include "lldb/Interpreter/CommandInterpreter.h" 34 #include "lldb/Interpreter/CommandOptionArgumentTable.h" 35 #include "lldb/Interpreter/CommandReturnObject.h" 36 37 using namespace lldb; 38 using namespace lldb_private; 39 40 // CommandObject 41 42 CommandObject::CommandObject(CommandInterpreter &interpreter, 43 llvm::StringRef name, llvm::StringRef help, 44 llvm::StringRef syntax, uint32_t flags) 45 : m_interpreter(interpreter), m_cmd_name(std::string(name)), 46 m_flags(flags), m_deprecated_command_override_callback(nullptr), 47 m_command_override_callback(nullptr), m_command_override_baton(nullptr) { 48 m_cmd_help_short = std::string(help); 49 m_cmd_syntax = std::string(syntax); 50 } 51 52 Debugger &CommandObject::GetDebugger() { return m_interpreter.GetDebugger(); } 53 54 llvm::StringRef CommandObject::GetHelp() { return m_cmd_help_short; } 55 56 llvm::StringRef CommandObject::GetHelpLong() { return m_cmd_help_long; } 57 58 llvm::StringRef CommandObject::GetSyntax() { 59 if (!m_cmd_syntax.empty()) 60 return m_cmd_syntax; 61 62 StreamString syntax_str; 63 syntax_str.PutCString(GetCommandName()); 64 65 if (!IsDashDashCommand() && GetOptions() != nullptr) 66 syntax_str.PutCString(" <cmd-options>"); 67 68 if (!m_arguments.empty()) { 69 syntax_str.PutCString(" "); 70 71 if (!IsDashDashCommand() && WantsRawCommandString() && GetOptions() && 72 GetOptions()->NumCommandOptions()) 73 syntax_str.PutCString("-- "); 74 GetFormattedCommandArguments(syntax_str); 75 } 76 m_cmd_syntax = std::string(syntax_str.GetString()); 77 78 return m_cmd_syntax; 79 } 80 81 llvm::StringRef CommandObject::GetCommandName() const { return m_cmd_name; } 82 83 void CommandObject::SetCommandName(llvm::StringRef name) { 84 m_cmd_name = std::string(name); 85 } 86 87 void CommandObject::SetHelp(llvm::StringRef str) { 88 m_cmd_help_short = std::string(str); 89 } 90 91 void CommandObject::SetHelpLong(llvm::StringRef str) { 92 m_cmd_help_long = std::string(str); 93 } 94 95 void CommandObject::SetSyntax(llvm::StringRef str) { 96 m_cmd_syntax = std::string(str); 97 } 98 99 Options *CommandObject::GetOptions() { 100 // By default commands don't have options unless this virtual function is 101 // overridden by base classes. 102 return nullptr; 103 } 104 105 bool CommandObject::ParseOptions(Args &args, CommandReturnObject &result) { 106 // See if the subclass has options? 107 Options *options = GetOptions(); 108 if (options != nullptr) { 109 Status error; 110 111 auto exe_ctx = GetCommandInterpreter().GetExecutionContext(); 112 options->NotifyOptionParsingStarting(&exe_ctx); 113 114 const bool require_validation = true; 115 llvm::Expected<Args> args_or = options->Parse( 116 args, &exe_ctx, GetCommandInterpreter().GetPlatform(true), 117 require_validation); 118 119 if (args_or) { 120 args = std::move(*args_or); 121 error = options->NotifyOptionParsingFinished(&exe_ctx); 122 } else 123 error = args_or.takeError(); 124 125 if (error.Success()) { 126 if (options->VerifyOptions(result)) 127 return true; 128 } else { 129 const char *error_cstr = error.AsCString(); 130 if (error_cstr) { 131 // We got an error string, lets use that 132 result.AppendError(error_cstr); 133 } else { 134 // No error string, output the usage information into result 135 options->GenerateOptionUsage( 136 result.GetErrorStream(), *this, 137 GetCommandInterpreter().GetDebugger().GetTerminalWidth()); 138 } 139 } 140 result.SetStatus(eReturnStatusFailed); 141 return false; 142 } 143 return true; 144 } 145 146 bool CommandObject::CheckRequirements(CommandReturnObject &result) { 147 // Nothing should be stored in m_exe_ctx between running commands as 148 // m_exe_ctx has shared pointers to the target, process, thread and frame and 149 // we don't want any CommandObject instances to keep any of these objects 150 // around longer than for a single command. Every command should call 151 // CommandObject::Cleanup() after it has completed. 152 assert(!m_exe_ctx.GetTargetPtr()); 153 assert(!m_exe_ctx.GetProcessPtr()); 154 assert(!m_exe_ctx.GetThreadPtr()); 155 assert(!m_exe_ctx.GetFramePtr()); 156 157 // Lock down the interpreter's execution context prior to running the command 158 // so we guarantee the selected target, process, thread and frame can't go 159 // away during the execution 160 m_exe_ctx = m_interpreter.GetExecutionContext(); 161 162 const uint32_t flags = GetFlags().Get(); 163 if (flags & (eCommandRequiresTarget | eCommandRequiresProcess | 164 eCommandRequiresThread | eCommandRequiresFrame | 165 eCommandTryTargetAPILock)) { 166 167 if ((flags & eCommandRequiresTarget) && !m_exe_ctx.HasTargetScope()) { 168 result.AppendError(GetInvalidTargetDescription()); 169 return false; 170 } 171 172 if ((flags & eCommandRequiresProcess) && !m_exe_ctx.HasProcessScope()) { 173 if (!m_exe_ctx.HasTargetScope()) 174 result.AppendError(GetInvalidTargetDescription()); 175 else 176 result.AppendError(GetInvalidProcessDescription()); 177 return false; 178 } 179 180 if ((flags & eCommandRequiresThread) && !m_exe_ctx.HasThreadScope()) { 181 if (!m_exe_ctx.HasTargetScope()) 182 result.AppendError(GetInvalidTargetDescription()); 183 else if (!m_exe_ctx.HasProcessScope()) 184 result.AppendError(GetInvalidProcessDescription()); 185 else 186 result.AppendError(GetInvalidThreadDescription()); 187 return false; 188 } 189 190 if ((flags & eCommandRequiresFrame) && !m_exe_ctx.HasFrameScope()) { 191 if (!m_exe_ctx.HasTargetScope()) 192 result.AppendError(GetInvalidTargetDescription()); 193 else if (!m_exe_ctx.HasProcessScope()) 194 result.AppendError(GetInvalidProcessDescription()); 195 else if (!m_exe_ctx.HasThreadScope()) 196 result.AppendError(GetInvalidThreadDescription()); 197 else 198 result.AppendError(GetInvalidFrameDescription()); 199 return false; 200 } 201 202 if ((flags & eCommandRequiresRegContext) && 203 (m_exe_ctx.GetRegisterContext() == nullptr)) { 204 result.AppendError(GetInvalidRegContextDescription()); 205 return false; 206 } 207 208 if (flags & eCommandTryTargetAPILock) { 209 Target *target = m_exe_ctx.GetTargetPtr(); 210 if (target) 211 m_api_locker = 212 std::unique_lock<std::recursive_mutex>(target->GetAPIMutex()); 213 } 214 } 215 216 if (GetFlags().AnySet(eCommandProcessMustBeLaunched | 217 eCommandProcessMustBePaused)) { 218 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr(); 219 if (process == nullptr) { 220 // A process that is not running is considered paused. 221 if (GetFlags().Test(eCommandProcessMustBeLaunched)) { 222 result.AppendError("Process must exist."); 223 return false; 224 } 225 } else { 226 StateType state = process->GetState(); 227 switch (state) { 228 case eStateInvalid: 229 case eStateSuspended: 230 case eStateCrashed: 231 case eStateStopped: 232 break; 233 234 case eStateConnected: 235 case eStateAttaching: 236 case eStateLaunching: 237 case eStateDetached: 238 case eStateExited: 239 case eStateUnloaded: 240 if (GetFlags().Test(eCommandProcessMustBeLaunched)) { 241 result.AppendError("Process must be launched."); 242 return false; 243 } 244 break; 245 246 case eStateRunning: 247 case eStateStepping: 248 if (GetFlags().Test(eCommandProcessMustBePaused)) { 249 result.AppendError("Process is running. Use 'process interrupt' to " 250 "pause execution."); 251 return false; 252 } 253 } 254 } 255 } 256 257 if (GetFlags().Test(eCommandProcessMustBeTraced)) { 258 Target *target = m_exe_ctx.GetTargetPtr(); 259 if (target && !target->GetTrace()) { 260 result.AppendError("Process is not being traced."); 261 return false; 262 } 263 } 264 265 return true; 266 } 267 268 void CommandObject::Cleanup() { 269 m_exe_ctx.Clear(); 270 if (m_api_locker.owns_lock()) 271 m_api_locker.unlock(); 272 } 273 274 void CommandObject::HandleCompletion(CompletionRequest &request) { 275 276 m_exe_ctx = m_interpreter.GetExecutionContext(); 277 auto reset_ctx = llvm::make_scope_exit([this]() { Cleanup(); }); 278 279 // Default implementation of WantsCompletion() is !WantsRawCommandString(). 280 // Subclasses who want raw command string but desire, for example, argument 281 // completion should override WantsCompletion() to return true, instead. 282 if (WantsRawCommandString() && !WantsCompletion()) { 283 // FIXME: Abstract telling the completion to insert the completion 284 // character. 285 return; 286 } else { 287 // Can we do anything generic with the options? 288 Options *cur_options = GetOptions(); 289 CommandReturnObject result(m_interpreter.GetDebugger().GetUseColor()); 290 OptionElementVector opt_element_vector; 291 292 if (cur_options != nullptr) { 293 opt_element_vector = cur_options->ParseForCompletion( 294 request.GetParsedLine(), request.GetCursorIndex()); 295 296 bool handled_by_options = cur_options->HandleOptionCompletion( 297 request, opt_element_vector, GetCommandInterpreter()); 298 if (handled_by_options) 299 return; 300 } 301 302 // If we got here, the last word is not an option or an option argument. 303 HandleArgumentCompletion(request, opt_element_vector); 304 } 305 } 306 307 bool CommandObject::HelpTextContainsWord(llvm::StringRef search_word, 308 bool search_short_help, 309 bool search_long_help, 310 bool search_syntax, 311 bool search_options) { 312 std::string options_usage_help; 313 314 bool found_word = false; 315 316 llvm::StringRef short_help = GetHelp(); 317 llvm::StringRef long_help = GetHelpLong(); 318 llvm::StringRef syntax_help = GetSyntax(); 319 320 if (search_short_help && short_help.contains_insensitive(search_word)) 321 found_word = true; 322 else if (search_long_help && long_help.contains_insensitive(search_word)) 323 found_word = true; 324 else if (search_syntax && syntax_help.contains_insensitive(search_word)) 325 found_word = true; 326 327 if (!found_word && search_options && GetOptions() != nullptr) { 328 StreamString usage_help; 329 GetOptions()->GenerateOptionUsage( 330 usage_help, *this, 331 GetCommandInterpreter().GetDebugger().GetTerminalWidth()); 332 if (!usage_help.Empty()) { 333 llvm::StringRef usage_text = usage_help.GetString(); 334 if (usage_text.contains_insensitive(search_word)) 335 found_word = true; 336 } 337 } 338 339 return found_word; 340 } 341 342 bool CommandObject::ParseOptionsAndNotify(Args &args, 343 CommandReturnObject &result, 344 OptionGroupOptions &group_options, 345 ExecutionContext &exe_ctx) { 346 if (!ParseOptions(args, result)) 347 return false; 348 349 Status error(group_options.NotifyOptionParsingFinished(&exe_ctx)); 350 if (error.Fail()) { 351 result.AppendError(error.AsCString()); 352 return false; 353 } 354 return true; 355 } 356 357 int CommandObject::GetNumArgumentEntries() { return m_arguments.size(); } 358 359 CommandObject::CommandArgumentEntry * 360 CommandObject::GetArgumentEntryAtIndex(int idx) { 361 if (static_cast<size_t>(idx) < m_arguments.size()) 362 return &(m_arguments[idx]); 363 364 return nullptr; 365 } 366 367 const CommandObject::ArgumentTableEntry * 368 CommandObject::FindArgumentDataByType(CommandArgumentType arg_type) { 369 for (int i = 0; i < eArgTypeLastArg; ++i) 370 if (g_argument_table[i].arg_type == arg_type) 371 return &(g_argument_table[i]); 372 373 return nullptr; 374 } 375 376 void CommandObject::GetArgumentHelp(Stream &str, CommandArgumentType arg_type, 377 CommandInterpreter &interpreter) { 378 const ArgumentTableEntry *entry = &(g_argument_table[arg_type]); 379 380 // The table is *supposed* to be kept in arg_type order, but someone *could* 381 // have messed it up... 382 383 if (entry->arg_type != arg_type) 384 entry = CommandObject::FindArgumentDataByType(arg_type); 385 386 if (!entry) 387 return; 388 389 StreamString name_str; 390 name_str.Printf("<%s>", entry->arg_name); 391 392 if (entry->help_function) { 393 llvm::StringRef help_text = entry->help_function(); 394 if (!entry->help_function.self_formatting) { 395 interpreter.OutputFormattedHelpText(str, name_str.GetString(), "--", 396 help_text, name_str.GetSize()); 397 } else { 398 interpreter.OutputHelpText(str, name_str.GetString(), "--", help_text, 399 name_str.GetSize()); 400 } 401 } else 402 interpreter.OutputFormattedHelpText(str, name_str.GetString(), "--", 403 entry->help_text, name_str.GetSize()); 404 } 405 406 const char *CommandObject::GetArgumentName(CommandArgumentType arg_type) { 407 const ArgumentTableEntry *entry = &(g_argument_table[arg_type]); 408 409 // The table is *supposed* to be kept in arg_type order, but someone *could* 410 // have messed it up... 411 412 if (entry->arg_type != arg_type) 413 entry = CommandObject::FindArgumentDataByType(arg_type); 414 415 if (entry) 416 return entry->arg_name; 417 418 return nullptr; 419 } 420 421 bool CommandObject::IsPairType(ArgumentRepetitionType arg_repeat_type) { 422 return (arg_repeat_type == eArgRepeatPairPlain) || 423 (arg_repeat_type == eArgRepeatPairOptional) || 424 (arg_repeat_type == eArgRepeatPairPlus) || 425 (arg_repeat_type == eArgRepeatPairStar) || 426 (arg_repeat_type == eArgRepeatPairRange) || 427 (arg_repeat_type == eArgRepeatPairRangeOptional); 428 } 429 430 static CommandObject::CommandArgumentEntry 431 OptSetFiltered(uint32_t opt_set_mask, 432 CommandObject::CommandArgumentEntry &cmd_arg_entry) { 433 CommandObject::CommandArgumentEntry ret_val; 434 for (unsigned i = 0; i < cmd_arg_entry.size(); ++i) 435 if (opt_set_mask & cmd_arg_entry[i].arg_opt_set_association) 436 ret_val.push_back(cmd_arg_entry[i]); 437 return ret_val; 438 } 439 440 // Default parameter value of opt_set_mask is LLDB_OPT_SET_ALL, which means 441 // take all the argument data into account. On rare cases where some argument 442 // sticks with certain option sets, this function returns the option set 443 // filtered args. 444 void CommandObject::GetFormattedCommandArguments(Stream &str, 445 uint32_t opt_set_mask) { 446 int num_args = m_arguments.size(); 447 for (int i = 0; i < num_args; ++i) { 448 if (i > 0) 449 str.Printf(" "); 450 CommandArgumentEntry arg_entry = 451 opt_set_mask == LLDB_OPT_SET_ALL 452 ? m_arguments[i] 453 : OptSetFiltered(opt_set_mask, m_arguments[i]); 454 // This argument is not associated with the current option set, so skip it. 455 if (arg_entry.empty()) 456 continue; 457 int num_alternatives = arg_entry.size(); 458 459 if ((num_alternatives == 2) && IsPairType(arg_entry[0].arg_repetition)) { 460 const char *first_name = GetArgumentName(arg_entry[0].arg_type); 461 const char *second_name = GetArgumentName(arg_entry[1].arg_type); 462 switch (arg_entry[0].arg_repetition) { 463 case eArgRepeatPairPlain: 464 str.Printf("<%s> <%s>", first_name, second_name); 465 break; 466 case eArgRepeatPairOptional: 467 str.Printf("[<%s> <%s>]", first_name, second_name); 468 break; 469 case eArgRepeatPairPlus: 470 str.Printf("<%s> <%s> [<%s> <%s> [...]]", first_name, second_name, 471 first_name, second_name); 472 break; 473 case eArgRepeatPairStar: 474 str.Printf("[<%s> <%s> [<%s> <%s> [...]]]", first_name, second_name, 475 first_name, second_name); 476 break; 477 case eArgRepeatPairRange: 478 str.Printf("<%s_1> <%s_1> ... <%s_n> <%s_n>", first_name, second_name, 479 first_name, second_name); 480 break; 481 case eArgRepeatPairRangeOptional: 482 str.Printf("[<%s_1> <%s_1> ... <%s_n> <%s_n>]", first_name, second_name, 483 first_name, second_name); 484 break; 485 // Explicitly test for all the rest of the cases, so if new types get 486 // added we will notice the missing case statement(s). 487 case eArgRepeatPlain: 488 case eArgRepeatOptional: 489 case eArgRepeatPlus: 490 case eArgRepeatStar: 491 case eArgRepeatRange: 492 // These should not be reached, as they should fail the IsPairType test 493 // above. 494 break; 495 } 496 } else { 497 StreamString names; 498 for (int j = 0; j < num_alternatives; ++j) { 499 if (j > 0) 500 names.Printf(" | "); 501 names.Printf("%s", GetArgumentName(arg_entry[j].arg_type)); 502 } 503 504 std::string name_str = std::string(names.GetString()); 505 switch (arg_entry[0].arg_repetition) { 506 case eArgRepeatPlain: 507 str.Printf("<%s>", name_str.c_str()); 508 break; 509 case eArgRepeatPlus: 510 str.Printf("<%s> [<%s> [...]]", name_str.c_str(), name_str.c_str()); 511 break; 512 case eArgRepeatStar: 513 str.Printf("[<%s> [<%s> [...]]]", name_str.c_str(), name_str.c_str()); 514 break; 515 case eArgRepeatOptional: 516 str.Printf("[<%s>]", name_str.c_str()); 517 break; 518 case eArgRepeatRange: 519 str.Printf("<%s_1> .. <%s_n>", name_str.c_str(), name_str.c_str()); 520 break; 521 // Explicitly test for all the rest of the cases, so if new types get 522 // added we will notice the missing case statement(s). 523 case eArgRepeatPairPlain: 524 case eArgRepeatPairOptional: 525 case eArgRepeatPairPlus: 526 case eArgRepeatPairStar: 527 case eArgRepeatPairRange: 528 case eArgRepeatPairRangeOptional: 529 // These should not be hit, as they should pass the IsPairType test 530 // above, and control should have gone into the other branch of the if 531 // statement. 532 break; 533 } 534 } 535 } 536 } 537 538 CommandArgumentType 539 CommandObject::LookupArgumentName(llvm::StringRef arg_name) { 540 CommandArgumentType return_type = eArgTypeLastArg; 541 542 arg_name = arg_name.ltrim('<').rtrim('>'); 543 544 for (int i = 0; i < eArgTypeLastArg; ++i) 545 if (arg_name == g_argument_table[i].arg_name) 546 return_type = g_argument_table[i].arg_type; 547 548 return return_type; 549 } 550 551 void CommandObject::FormatLongHelpText(Stream &output_strm, 552 llvm::StringRef long_help) { 553 CommandInterpreter &interpreter = GetCommandInterpreter(); 554 std::stringstream lineStream{std::string(long_help)}; 555 std::string line; 556 while (std::getline(lineStream, line)) { 557 if (line.empty()) { 558 output_strm << "\n"; 559 continue; 560 } 561 size_t result = line.find_first_not_of(" \t"); 562 if (result == std::string::npos) { 563 result = 0; 564 } 565 std::string whitespace_prefix = line.substr(0, result); 566 std::string remainder = line.substr(result); 567 interpreter.OutputFormattedHelpText(output_strm, whitespace_prefix, 568 remainder); 569 } 570 } 571 572 void CommandObject::GenerateHelpText(CommandReturnObject &result) { 573 GenerateHelpText(result.GetOutputStream()); 574 575 result.SetStatus(eReturnStatusSuccessFinishNoResult); 576 } 577 578 void CommandObject::GenerateHelpText(Stream &output_strm) { 579 CommandInterpreter &interpreter = GetCommandInterpreter(); 580 std::string help_text(GetHelp()); 581 if (WantsRawCommandString()) { 582 help_text.append(" Expects 'raw' input (see 'help raw-input'.)"); 583 } 584 interpreter.OutputFormattedHelpText(output_strm, "", help_text); 585 output_strm << "\nSyntax: " << GetSyntax() << "\n"; 586 Options *options = GetOptions(); 587 if (options != nullptr) { 588 options->GenerateOptionUsage( 589 output_strm, *this, 590 GetCommandInterpreter().GetDebugger().GetTerminalWidth()); 591 } 592 llvm::StringRef long_help = GetHelpLong(); 593 if (!long_help.empty()) { 594 FormatLongHelpText(output_strm, long_help); 595 } 596 if (!IsDashDashCommand() && options && options->NumCommandOptions() > 0) { 597 if (WantsRawCommandString() && !WantsCompletion()) { 598 // Emit the message about using ' -- ' between the end of the command 599 // options and the raw input conditionally, i.e., only if the command 600 // object does not want completion. 601 interpreter.OutputFormattedHelpText( 602 output_strm, "", "", 603 "\nImportant Note: Because this command takes 'raw' input, if you " 604 "use any command options" 605 " you must use ' -- ' between the end of the command options and the " 606 "beginning of the raw input.", 607 1); 608 } else if (GetNumArgumentEntries() > 0) { 609 // Also emit a warning about using "--" in case you are using a command 610 // that takes options and arguments. 611 interpreter.OutputFormattedHelpText( 612 output_strm, "", "", 613 "\nThis command takes options and free-form arguments. If your " 614 "arguments resemble" 615 " option specifiers (i.e., they start with a - or --), you must use " 616 "' -- ' between" 617 " the end of the command options and the beginning of the arguments.", 618 1); 619 } 620 } 621 } 622 623 void CommandObject::AddIDsArgumentData(CommandArgumentEntry &arg, 624 CommandArgumentType ID, 625 CommandArgumentType IDRange) { 626 CommandArgumentData id_arg; 627 CommandArgumentData id_range_arg; 628 629 // Create the first variant for the first (and only) argument for this 630 // command. 631 id_arg.arg_type = ID; 632 id_arg.arg_repetition = eArgRepeatOptional; 633 634 // Create the second variant for the first (and only) argument for this 635 // command. 636 id_range_arg.arg_type = IDRange; 637 id_range_arg.arg_repetition = eArgRepeatOptional; 638 639 // The first (and only) argument for this command could be either an id or an 640 // id_range. Push both variants into the entry for the first argument for 641 // this command. 642 arg.push_back(id_arg); 643 arg.push_back(id_range_arg); 644 } 645 646 const char *CommandObject::GetArgumentTypeAsCString( 647 const lldb::CommandArgumentType arg_type) { 648 assert(arg_type < eArgTypeLastArg && 649 "Invalid argument type passed to GetArgumentTypeAsCString"); 650 return g_argument_table[arg_type].arg_name; 651 } 652 653 const char *CommandObject::GetArgumentDescriptionAsCString( 654 const lldb::CommandArgumentType arg_type) { 655 assert(arg_type < eArgTypeLastArg && 656 "Invalid argument type passed to GetArgumentDescriptionAsCString"); 657 return g_argument_table[arg_type].help_text; 658 } 659 660 Target &CommandObject::GetDummyTarget() { 661 return m_interpreter.GetDebugger().GetDummyTarget(); 662 } 663 664 Target &CommandObject::GetSelectedOrDummyTarget(bool prefer_dummy) { 665 return m_interpreter.GetDebugger().GetSelectedOrDummyTarget(prefer_dummy); 666 } 667 668 Target &CommandObject::GetSelectedTarget() { 669 assert(m_flags.AnySet(eCommandRequiresTarget | eCommandProcessMustBePaused | 670 eCommandProcessMustBeLaunched | eCommandRequiresFrame | 671 eCommandRequiresThread | eCommandRequiresProcess | 672 eCommandRequiresRegContext) && 673 "GetSelectedTarget called from object that may have no target"); 674 return *m_interpreter.GetDebugger().GetSelectedTarget(); 675 } 676 677 Thread *CommandObject::GetDefaultThread() { 678 Thread *thread_to_use = m_exe_ctx.GetThreadPtr(); 679 if (thread_to_use) 680 return thread_to_use; 681 682 Process *process = m_exe_ctx.GetProcessPtr(); 683 if (!process) { 684 Target *target = m_exe_ctx.GetTargetPtr(); 685 if (!target) { 686 target = m_interpreter.GetDebugger().GetSelectedTarget().get(); 687 } 688 if (target) 689 process = target->GetProcessSP().get(); 690 } 691 692 if (process) 693 return process->GetThreadList().GetSelectedThread().get(); 694 else 695 return nullptr; 696 } 697 698 bool CommandObjectParsed::Execute(const char *args_string, 699 CommandReturnObject &result) { 700 bool handled = false; 701 Args cmd_args(args_string); 702 if (HasOverrideCallback()) { 703 Args full_args(GetCommandName()); 704 full_args.AppendArguments(cmd_args); 705 handled = 706 InvokeOverrideCallback(full_args.GetConstArgumentVector(), result); 707 } 708 if (!handled) { 709 for (auto entry : llvm::enumerate(cmd_args.entries())) { 710 if (!entry.value().ref().empty() && entry.value().ref().front() == '`') { 711 cmd_args.ReplaceArgumentAtIndex( 712 entry.index(), 713 m_interpreter.ProcessEmbeddedScriptCommands(entry.value().c_str())); 714 } 715 } 716 717 if (CheckRequirements(result)) { 718 if (ParseOptions(cmd_args, result)) { 719 // Call the command-specific version of 'Execute', passing it the 720 // already processed arguments. 721 if (cmd_args.GetArgumentCount() != 0 && m_arguments.empty()) { 722 result.AppendErrorWithFormatv("'{0}' doesn't take any arguments.", 723 GetCommandName()); 724 return false; 725 } 726 handled = DoExecute(cmd_args, result); 727 } 728 } 729 730 Cleanup(); 731 } 732 return handled; 733 } 734 735 bool CommandObjectRaw::Execute(const char *args_string, 736 CommandReturnObject &result) { 737 bool handled = false; 738 if (HasOverrideCallback()) { 739 std::string full_command(GetCommandName()); 740 full_command += ' '; 741 full_command += args_string; 742 const char *argv[2] = {nullptr, nullptr}; 743 argv[0] = full_command.c_str(); 744 handled = InvokeOverrideCallback(argv, result); 745 } 746 if (!handled) { 747 if (CheckRequirements(result)) 748 handled = DoExecute(args_string, result); 749 750 Cleanup(); 751 } 752 return handled; 753 } 754