1 //===-- CommandObjectThread.cpp ---------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "CommandObjectThread.h" 10 11 #include "lldb/Core/SourceManager.h" 12 #include "lldb/Core/ValueObject.h" 13 #include "lldb/Host/Host.h" 14 #include "lldb/Host/OptionParser.h" 15 #include "lldb/Host/StringConvert.h" 16 #include "lldb/Interpreter/CommandInterpreter.h" 17 #include "lldb/Interpreter/CommandReturnObject.h" 18 #include "lldb/Interpreter/OptionArgParser.h" 19 #include "lldb/Interpreter/OptionGroupPythonClassWithDict.h" 20 #include "lldb/Interpreter/Options.h" 21 #include "lldb/Symbol/CompileUnit.h" 22 #include "lldb/Symbol/Function.h" 23 #include "lldb/Symbol/LineEntry.h" 24 #include "lldb/Symbol/LineTable.h" 25 #include "lldb/Target/Process.h" 26 #include "lldb/Target/RegisterContext.h" 27 #include "lldb/Target/SystemRuntime.h" 28 #include "lldb/Target/Target.h" 29 #include "lldb/Target/Thread.h" 30 #include "lldb/Target/ThreadPlan.h" 31 #include "lldb/Target/ThreadPlanStepInRange.h" 32 #include "lldb/Target/ThreadPlanStepInstruction.h" 33 #include "lldb/Target/ThreadPlanStepOut.h" 34 #include "lldb/Target/ThreadPlanStepRange.h" 35 #include "lldb/Utility/State.h" 36 #include "lldb/lldb-private.h" 37 38 using namespace lldb; 39 using namespace lldb_private; 40 41 // CommandObjectIterateOverThreads 42 43 class CommandObjectIterateOverThreads : public CommandObjectParsed { 44 45 class UniqueStack { 46 47 public: 48 UniqueStack(std::stack<lldb::addr_t> stack_frames, uint32_t thread_index_id) 49 : m_stack_frames(stack_frames) { 50 m_thread_index_ids.push_back(thread_index_id); 51 } 52 53 void AddThread(uint32_t thread_index_id) const { 54 m_thread_index_ids.push_back(thread_index_id); 55 } 56 57 const std::vector<uint32_t> &GetUniqueThreadIndexIDs() const { 58 return m_thread_index_ids; 59 } 60 61 lldb::tid_t GetRepresentativeThread() const { 62 return m_thread_index_ids.front(); 63 } 64 65 friend bool inline operator<(const UniqueStack &lhs, 66 const UniqueStack &rhs) { 67 return lhs.m_stack_frames < rhs.m_stack_frames; 68 } 69 70 protected: 71 // Mark the thread index as mutable, as we don't care about it from a const 72 // perspective, we only care about m_stack_frames so we keep our std::set 73 // sorted. 74 mutable std::vector<uint32_t> m_thread_index_ids; 75 std::stack<lldb::addr_t> m_stack_frames; 76 }; 77 78 public: 79 CommandObjectIterateOverThreads(CommandInterpreter &interpreter, 80 const char *name, const char *help, 81 const char *syntax, uint32_t flags) 82 : CommandObjectParsed(interpreter, name, help, syntax, flags) {} 83 84 ~CommandObjectIterateOverThreads() override = default; 85 86 bool DoExecute(Args &command, CommandReturnObject &result) override { 87 result.SetStatus(m_success_return); 88 89 bool all_threads = false; 90 if (command.GetArgumentCount() == 0) { 91 Thread *thread = m_exe_ctx.GetThreadPtr(); 92 if (!thread || !HandleOneThread(thread->GetID(), result)) 93 return false; 94 return result.Succeeded(); 95 } else if (command.GetArgumentCount() == 1) { 96 all_threads = ::strcmp(command.GetArgumentAtIndex(0), "all") == 0; 97 m_unique_stacks = ::strcmp(command.GetArgumentAtIndex(0), "unique") == 0; 98 } 99 100 // Use tids instead of ThreadSPs to prevent deadlocking problems which 101 // result from JIT-ing code while iterating over the (locked) ThreadSP 102 // list. 103 std::vector<lldb::tid_t> tids; 104 105 if (all_threads || m_unique_stacks) { 106 Process *process = m_exe_ctx.GetProcessPtr(); 107 108 for (ThreadSP thread_sp : process->Threads()) 109 tids.push_back(thread_sp->GetID()); 110 } else { 111 const size_t num_args = command.GetArgumentCount(); 112 Process *process = m_exe_ctx.GetProcessPtr(); 113 114 std::lock_guard<std::recursive_mutex> guard( 115 process->GetThreadList().GetMutex()); 116 117 for (size_t i = 0; i < num_args; i++) { 118 bool success; 119 120 uint32_t thread_idx = StringConvert::ToUInt32( 121 command.GetArgumentAtIndex(i), 0, 0, &success); 122 if (!success) { 123 result.AppendErrorWithFormat("invalid thread specification: \"%s\"\n", 124 command.GetArgumentAtIndex(i)); 125 result.SetStatus(eReturnStatusFailed); 126 return false; 127 } 128 129 ThreadSP thread = 130 process->GetThreadList().FindThreadByIndexID(thread_idx); 131 132 if (!thread) { 133 result.AppendErrorWithFormat("no thread with index: \"%s\"\n", 134 command.GetArgumentAtIndex(i)); 135 result.SetStatus(eReturnStatusFailed); 136 return false; 137 } 138 139 tids.push_back(thread->GetID()); 140 } 141 } 142 143 if (m_unique_stacks) { 144 // Iterate over threads, finding unique stack buckets. 145 std::set<UniqueStack> unique_stacks; 146 for (const lldb::tid_t &tid : tids) { 147 if (!BucketThread(tid, unique_stacks, result)) { 148 return false; 149 } 150 } 151 152 // Write the thread id's and unique call stacks to the output stream 153 Stream &strm = result.GetOutputStream(); 154 Process *process = m_exe_ctx.GetProcessPtr(); 155 for (const UniqueStack &stack : unique_stacks) { 156 // List the common thread ID's 157 const std::vector<uint32_t> &thread_index_ids = 158 stack.GetUniqueThreadIndexIDs(); 159 strm.Format("{0} thread(s) ", thread_index_ids.size()); 160 for (const uint32_t &thread_index_id : thread_index_ids) { 161 strm.Format("#{0} ", thread_index_id); 162 } 163 strm.EOL(); 164 165 // List the shared call stack for this set of threads 166 uint32_t representative_thread_id = stack.GetRepresentativeThread(); 167 ThreadSP thread = process->GetThreadList().FindThreadByIndexID( 168 representative_thread_id); 169 if (!HandleOneThread(thread->GetID(), result)) { 170 return false; 171 } 172 } 173 } else { 174 uint32_t idx = 0; 175 for (const lldb::tid_t &tid : tids) { 176 if (idx != 0 && m_add_return) 177 result.AppendMessage(""); 178 179 if (!HandleOneThread(tid, result)) 180 return false; 181 182 ++idx; 183 } 184 } 185 return result.Succeeded(); 186 } 187 188 protected: 189 // Override this to do whatever you need to do for one thread. 190 // 191 // If you return false, the iteration will stop, otherwise it will proceed. 192 // The result is set to m_success_return (defaults to 193 // eReturnStatusSuccessFinishResult) before the iteration, so you only need 194 // to set the return status in HandleOneThread if you want to indicate an 195 // error. If m_add_return is true, a blank line will be inserted between each 196 // of the listings (except the last one.) 197 198 virtual bool HandleOneThread(lldb::tid_t, CommandReturnObject &result) = 0; 199 200 bool BucketThread(lldb::tid_t tid, std::set<UniqueStack> &unique_stacks, 201 CommandReturnObject &result) { 202 // Grab the corresponding thread for the given thread id. 203 Process *process = m_exe_ctx.GetProcessPtr(); 204 Thread *thread = process->GetThreadList().FindThreadByID(tid).get(); 205 if (thread == nullptr) { 206 result.AppendErrorWithFormatv("Failed to process thread #{0}.\n", tid); 207 result.SetStatus(eReturnStatusFailed); 208 return false; 209 } 210 211 // Collect the each frame's address for this call-stack 212 std::stack<lldb::addr_t> stack_frames; 213 const uint32_t frame_count = thread->GetStackFrameCount(); 214 for (uint32_t frame_index = 0; frame_index < frame_count; frame_index++) { 215 const lldb::StackFrameSP frame_sp = 216 thread->GetStackFrameAtIndex(frame_index); 217 const lldb::addr_t pc = frame_sp->GetStackID().GetPC(); 218 stack_frames.push(pc); 219 } 220 221 uint32_t thread_index_id = thread->GetIndexID(); 222 UniqueStack new_unique_stack(stack_frames, thread_index_id); 223 224 // Try to match the threads stack to and existing entry. 225 std::set<UniqueStack>::iterator matching_stack = 226 unique_stacks.find(new_unique_stack); 227 if (matching_stack != unique_stacks.end()) { 228 matching_stack->AddThread(thread_index_id); 229 } else { 230 unique_stacks.insert(new_unique_stack); 231 } 232 return true; 233 } 234 235 ReturnStatus m_success_return = eReturnStatusSuccessFinishResult; 236 bool m_unique_stacks = false; 237 bool m_add_return = true; 238 }; 239 240 // CommandObjectThreadBacktrace 241 #define LLDB_OPTIONS_thread_backtrace 242 #include "CommandOptions.inc" 243 244 class CommandObjectThreadBacktrace : public CommandObjectIterateOverThreads { 245 public: 246 class CommandOptions : public Options { 247 public: 248 CommandOptions() : Options() { 249 // Keep default values of all options in one place: OptionParsingStarting 250 // () 251 OptionParsingStarting(nullptr); 252 } 253 254 ~CommandOptions() override = default; 255 256 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 257 ExecutionContext *execution_context) override { 258 Status error; 259 const int short_option = m_getopt_table[option_idx].val; 260 261 switch (short_option) { 262 case 'c': { 263 int32_t input_count = 0; 264 if (option_arg.getAsInteger(0, m_count)) { 265 m_count = UINT32_MAX; 266 error.SetErrorStringWithFormat( 267 "invalid integer value for option '%c'", short_option); 268 } else if (input_count < 0) 269 m_count = UINT32_MAX; 270 } break; 271 case 's': 272 if (option_arg.getAsInteger(0, m_start)) 273 error.SetErrorStringWithFormat( 274 "invalid integer value for option '%c'", short_option); 275 break; 276 case 'e': { 277 bool success; 278 m_extended_backtrace = 279 OptionArgParser::ToBoolean(option_arg, false, &success); 280 if (!success) 281 error.SetErrorStringWithFormat( 282 "invalid boolean value for option '%c'", short_option); 283 } break; 284 default: 285 llvm_unreachable("Unimplemented option"); 286 } 287 return error; 288 } 289 290 void OptionParsingStarting(ExecutionContext *execution_context) override { 291 m_count = UINT32_MAX; 292 m_start = 0; 293 m_extended_backtrace = false; 294 } 295 296 llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 297 return llvm::makeArrayRef(g_thread_backtrace_options); 298 } 299 300 // Instance variables to hold the values for command options. 301 uint32_t m_count; 302 uint32_t m_start; 303 bool m_extended_backtrace; 304 }; 305 306 CommandObjectThreadBacktrace(CommandInterpreter &interpreter) 307 : CommandObjectIterateOverThreads( 308 interpreter, "thread backtrace", 309 "Show thread call stacks. Defaults to the current thread, thread " 310 "indexes can be specified as arguments.\n" 311 "Use the thread-index \"all\" to see all threads.\n" 312 "Use the thread-index \"unique\" to see threads grouped by unique " 313 "call stacks.\n" 314 "Use 'settings set frame-format' to customize the printing of " 315 "frames in the backtrace and 'settings set thread-format' to " 316 "customize the thread header.", 317 nullptr, 318 eCommandRequiresProcess | eCommandRequiresThread | 319 eCommandTryTargetAPILock | eCommandProcessMustBeLaunched | 320 eCommandProcessMustBePaused), 321 m_options() {} 322 323 ~CommandObjectThreadBacktrace() override = default; 324 325 Options *GetOptions() override { return &m_options; } 326 327 protected: 328 void DoExtendedBacktrace(Thread *thread, CommandReturnObject &result) { 329 SystemRuntime *runtime = thread->GetProcess()->GetSystemRuntime(); 330 if (runtime) { 331 Stream &strm = result.GetOutputStream(); 332 const std::vector<ConstString> &types = 333 runtime->GetExtendedBacktraceTypes(); 334 for (auto type : types) { 335 ThreadSP ext_thread_sp = runtime->GetExtendedBacktraceThread( 336 thread->shared_from_this(), type); 337 if (ext_thread_sp && ext_thread_sp->IsValid()) { 338 const uint32_t num_frames_with_source = 0; 339 const bool stop_format = false; 340 if (ext_thread_sp->GetStatus(strm, m_options.m_start, 341 m_options.m_count, 342 num_frames_with_source, stop_format)) { 343 DoExtendedBacktrace(ext_thread_sp.get(), result); 344 } 345 } 346 } 347 } 348 } 349 350 bool HandleOneThread(lldb::tid_t tid, CommandReturnObject &result) override { 351 ThreadSP thread_sp = 352 m_exe_ctx.GetProcessPtr()->GetThreadList().FindThreadByID(tid); 353 if (!thread_sp) { 354 result.AppendErrorWithFormat( 355 "thread disappeared while computing backtraces: 0x%" PRIx64 "\n", 356 tid); 357 result.SetStatus(eReturnStatusFailed); 358 return false; 359 } 360 361 Thread *thread = thread_sp.get(); 362 363 Stream &strm = result.GetOutputStream(); 364 365 // Only dump stack info if we processing unique stacks. 366 const bool only_stacks = m_unique_stacks; 367 368 // Don't show source context when doing backtraces. 369 const uint32_t num_frames_with_source = 0; 370 const bool stop_format = true; 371 if (!thread->GetStatus(strm, m_options.m_start, m_options.m_count, 372 num_frames_with_source, stop_format, only_stacks)) { 373 result.AppendErrorWithFormat( 374 "error displaying backtrace for thread: \"0x%4.4x\"\n", 375 thread->GetIndexID()); 376 result.SetStatus(eReturnStatusFailed); 377 return false; 378 } 379 if (m_options.m_extended_backtrace) { 380 DoExtendedBacktrace(thread, result); 381 } 382 383 return true; 384 } 385 386 CommandOptions m_options; 387 }; 388 389 enum StepScope { eStepScopeSource, eStepScopeInstruction }; 390 391 static constexpr OptionEnumValueElement g_tri_running_mode[] = { 392 {eOnlyThisThread, "this-thread", "Run only this thread"}, 393 {eAllThreads, "all-threads", "Run all threads"}, 394 {eOnlyDuringStepping, "while-stepping", 395 "Run only this thread while stepping"}}; 396 397 static constexpr OptionEnumValues TriRunningModes() { 398 return OptionEnumValues(g_tri_running_mode); 399 } 400 401 #define LLDB_OPTIONS_thread_step_scope 402 #include "CommandOptions.inc" 403 404 class ThreadStepScopeOptionGroup : public OptionGroup { 405 public: 406 ThreadStepScopeOptionGroup() : OptionGroup() { 407 // Keep default values of all options in one place: OptionParsingStarting 408 // () 409 OptionParsingStarting(nullptr); 410 } 411 412 ~ThreadStepScopeOptionGroup() override = default; 413 414 llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 415 return llvm::makeArrayRef(g_thread_step_scope_options); 416 } 417 418 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 419 ExecutionContext *execution_context) override { 420 Status error; 421 const int short_option = 422 g_thread_step_scope_options[option_idx].short_option; 423 424 switch (short_option) { 425 case 'a': { 426 bool success; 427 bool avoid_no_debug = 428 OptionArgParser::ToBoolean(option_arg, true, &success); 429 if (!success) 430 error.SetErrorStringWithFormat("invalid boolean value for option '%c'", 431 short_option); 432 else { 433 m_step_in_avoid_no_debug = avoid_no_debug ? eLazyBoolYes : eLazyBoolNo; 434 } 435 } break; 436 437 case 'A': { 438 bool success; 439 bool avoid_no_debug = 440 OptionArgParser::ToBoolean(option_arg, true, &success); 441 if (!success) 442 error.SetErrorStringWithFormat("invalid boolean value for option '%c'", 443 short_option); 444 else { 445 m_step_out_avoid_no_debug = avoid_no_debug ? eLazyBoolYes : eLazyBoolNo; 446 } 447 } break; 448 449 case 'c': 450 if (option_arg.getAsInteger(0, m_step_count)) 451 error.SetErrorStringWithFormat("invalid step count '%s'", 452 option_arg.str().c_str()); 453 break; 454 455 case 'm': { 456 auto enum_values = GetDefinitions()[option_idx].enum_values; 457 m_run_mode = (lldb::RunMode)OptionArgParser::ToOptionEnum( 458 option_arg, enum_values, eOnlyDuringStepping, error); 459 } break; 460 461 case 'e': 462 if (option_arg == "block") { 463 m_end_line_is_block_end = true; 464 break; 465 } 466 if (option_arg.getAsInteger(0, m_end_line)) 467 error.SetErrorStringWithFormat("invalid end line number '%s'", 468 option_arg.str().c_str()); 469 break; 470 471 case 'r': 472 m_avoid_regexp.clear(); 473 m_avoid_regexp.assign(option_arg); 474 break; 475 476 case 't': 477 m_step_in_target.clear(); 478 m_step_in_target.assign(option_arg); 479 break; 480 481 default: 482 llvm_unreachable("Unimplemented option"); 483 } 484 return error; 485 } 486 487 void OptionParsingStarting(ExecutionContext *execution_context) override { 488 m_step_in_avoid_no_debug = eLazyBoolCalculate; 489 m_step_out_avoid_no_debug = eLazyBoolCalculate; 490 m_run_mode = eOnlyDuringStepping; 491 492 // Check if we are in Non-Stop mode 493 TargetSP target_sp = 494 execution_context ? execution_context->GetTargetSP() : TargetSP(); 495 if (target_sp && target_sp->GetNonStopModeEnabled()) 496 m_run_mode = eOnlyThisThread; 497 498 m_avoid_regexp.clear(); 499 m_step_in_target.clear(); 500 m_step_count = 1; 501 m_end_line = LLDB_INVALID_LINE_NUMBER; 502 m_end_line_is_block_end = false; 503 } 504 505 // Instance variables to hold the values for command options. 506 LazyBool m_step_in_avoid_no_debug; 507 LazyBool m_step_out_avoid_no_debug; 508 RunMode m_run_mode; 509 std::string m_avoid_regexp; 510 std::string m_step_in_target; 511 uint32_t m_step_count; 512 uint32_t m_end_line; 513 bool m_end_line_is_block_end; 514 }; 515 516 class CommandObjectThreadStepWithTypeAndScope : public CommandObjectParsed { 517 public: 518 CommandObjectThreadStepWithTypeAndScope(CommandInterpreter &interpreter, 519 const char *name, const char *help, 520 const char *syntax, 521 StepType step_type, 522 StepScope step_scope) 523 : CommandObjectParsed(interpreter, name, help, syntax, 524 eCommandRequiresProcess | eCommandRequiresThread | 525 eCommandTryTargetAPILock | 526 eCommandProcessMustBeLaunched | 527 eCommandProcessMustBePaused), 528 m_step_type(step_type), m_step_scope(step_scope), m_options(), 529 m_class_options("scripted step", 'C') { 530 CommandArgumentEntry arg; 531 CommandArgumentData thread_id_arg; 532 533 // Define the first (and only) variant of this arg. 534 thread_id_arg.arg_type = eArgTypeThreadID; 535 thread_id_arg.arg_repetition = eArgRepeatOptional; 536 537 // There is only one variant this argument could be; put it into the 538 // argument entry. 539 arg.push_back(thread_id_arg); 540 541 // Push the data for the first argument into the m_arguments vector. 542 m_arguments.push_back(arg); 543 544 if (step_type == eStepTypeScripted) { 545 m_all_options.Append(&m_class_options, LLDB_OPT_SET_1 | LLDB_OPT_SET_2, 546 LLDB_OPT_SET_1); 547 } 548 m_all_options.Append(&m_options); 549 m_all_options.Finalize(); 550 } 551 552 ~CommandObjectThreadStepWithTypeAndScope() override = default; 553 554 Options *GetOptions() override { return &m_all_options; } 555 556 protected: 557 bool DoExecute(Args &command, CommandReturnObject &result) override { 558 Process *process = m_exe_ctx.GetProcessPtr(); 559 bool synchronous_execution = m_interpreter.GetSynchronous(); 560 561 const uint32_t num_threads = process->GetThreadList().GetSize(); 562 Thread *thread = nullptr; 563 564 if (command.GetArgumentCount() == 0) { 565 thread = GetDefaultThread(); 566 567 if (thread == nullptr) { 568 result.AppendError("no selected thread in process"); 569 result.SetStatus(eReturnStatusFailed); 570 return false; 571 } 572 } else { 573 const char *thread_idx_cstr = command.GetArgumentAtIndex(0); 574 uint32_t step_thread_idx = 575 StringConvert::ToUInt32(thread_idx_cstr, LLDB_INVALID_INDEX32); 576 if (step_thread_idx == LLDB_INVALID_INDEX32) { 577 result.AppendErrorWithFormat("invalid thread index '%s'.\n", 578 thread_idx_cstr); 579 result.SetStatus(eReturnStatusFailed); 580 return false; 581 } 582 thread = 583 process->GetThreadList().FindThreadByIndexID(step_thread_idx).get(); 584 if (thread == nullptr) { 585 result.AppendErrorWithFormat( 586 "Thread index %u is out of range (valid values are 0 - %u).\n", 587 step_thread_idx, num_threads); 588 result.SetStatus(eReturnStatusFailed); 589 return false; 590 } 591 } 592 593 if (m_step_type == eStepTypeScripted) { 594 if (m_class_options.GetName().empty()) { 595 result.AppendErrorWithFormat("empty class name for scripted step."); 596 result.SetStatus(eReturnStatusFailed); 597 return false; 598 } else if (!GetDebugger().GetScriptInterpreter()->CheckObjectExists( 599 m_class_options.GetName().c_str())) { 600 result.AppendErrorWithFormat( 601 "class for scripted step: \"%s\" does not exist.", 602 m_class_options.GetName().c_str()); 603 result.SetStatus(eReturnStatusFailed); 604 return false; 605 } 606 } 607 608 if (m_options.m_end_line != LLDB_INVALID_LINE_NUMBER && 609 m_step_type != eStepTypeInto) { 610 result.AppendErrorWithFormat( 611 "end line option is only valid for step into"); 612 result.SetStatus(eReturnStatusFailed); 613 return false; 614 } 615 616 const bool abort_other_plans = false; 617 const lldb::RunMode stop_other_threads = m_options.m_run_mode; 618 619 // This is a bit unfortunate, but not all the commands in this command 620 // object support only while stepping, so I use the bool for them. 621 bool bool_stop_other_threads; 622 if (m_options.m_run_mode == eAllThreads) 623 bool_stop_other_threads = false; 624 else if (m_options.m_run_mode == eOnlyDuringStepping) 625 bool_stop_other_threads = 626 (m_step_type != eStepTypeOut && m_step_type != eStepTypeScripted); 627 else 628 bool_stop_other_threads = true; 629 630 ThreadPlanSP new_plan_sp; 631 Status new_plan_status; 632 633 if (m_step_type == eStepTypeInto) { 634 StackFrame *frame = thread->GetStackFrameAtIndex(0).get(); 635 assert(frame != nullptr); 636 637 if (frame->HasDebugInformation()) { 638 AddressRange range; 639 SymbolContext sc = frame->GetSymbolContext(eSymbolContextEverything); 640 if (m_options.m_end_line != LLDB_INVALID_LINE_NUMBER) { 641 Status error; 642 if (!sc.GetAddressRangeFromHereToEndLine(m_options.m_end_line, range, 643 error)) { 644 result.AppendErrorWithFormat("invalid end-line option: %s.", 645 error.AsCString()); 646 result.SetStatus(eReturnStatusFailed); 647 return false; 648 } 649 } else if (m_options.m_end_line_is_block_end) { 650 Status error; 651 Block *block = frame->GetSymbolContext(eSymbolContextBlock).block; 652 if (!block) { 653 result.AppendErrorWithFormat("Could not find the current block."); 654 result.SetStatus(eReturnStatusFailed); 655 return false; 656 } 657 658 AddressRange block_range; 659 Address pc_address = frame->GetFrameCodeAddress(); 660 block->GetRangeContainingAddress(pc_address, block_range); 661 if (!block_range.GetBaseAddress().IsValid()) { 662 result.AppendErrorWithFormat( 663 "Could not find the current block address."); 664 result.SetStatus(eReturnStatusFailed); 665 return false; 666 } 667 lldb::addr_t pc_offset_in_block = 668 pc_address.GetFileAddress() - 669 block_range.GetBaseAddress().GetFileAddress(); 670 lldb::addr_t range_length = 671 block_range.GetByteSize() - pc_offset_in_block; 672 range = AddressRange(pc_address, range_length); 673 } else { 674 range = sc.line_entry.range; 675 } 676 677 new_plan_sp = thread->QueueThreadPlanForStepInRange( 678 abort_other_plans, range, 679 frame->GetSymbolContext(eSymbolContextEverything), 680 m_options.m_step_in_target.c_str(), stop_other_threads, 681 new_plan_status, m_options.m_step_in_avoid_no_debug, 682 m_options.m_step_out_avoid_no_debug); 683 684 if (new_plan_sp && !m_options.m_avoid_regexp.empty()) { 685 ThreadPlanStepInRange *step_in_range_plan = 686 static_cast<ThreadPlanStepInRange *>(new_plan_sp.get()); 687 step_in_range_plan->SetAvoidRegexp(m_options.m_avoid_regexp.c_str()); 688 } 689 } else 690 new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction( 691 false, abort_other_plans, bool_stop_other_threads, new_plan_status); 692 } else if (m_step_type == eStepTypeOver) { 693 StackFrame *frame = thread->GetStackFrameAtIndex(0).get(); 694 695 if (frame->HasDebugInformation()) 696 new_plan_sp = thread->QueueThreadPlanForStepOverRange( 697 abort_other_plans, 698 frame->GetSymbolContext(eSymbolContextEverything).line_entry, 699 frame->GetSymbolContext(eSymbolContextEverything), 700 stop_other_threads, new_plan_status, 701 m_options.m_step_out_avoid_no_debug); 702 else 703 new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction( 704 true, abort_other_plans, bool_stop_other_threads, new_plan_status); 705 } else if (m_step_type == eStepTypeTrace) { 706 new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction( 707 false, abort_other_plans, bool_stop_other_threads, new_plan_status); 708 } else if (m_step_type == eStepTypeTraceOver) { 709 new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction( 710 true, abort_other_plans, bool_stop_other_threads, new_plan_status); 711 } else if (m_step_type == eStepTypeOut) { 712 new_plan_sp = thread->QueueThreadPlanForStepOut( 713 abort_other_plans, nullptr, false, bool_stop_other_threads, eVoteYes, 714 eVoteNoOpinion, thread->GetSelectedFrameIndex(), new_plan_status, 715 m_options.m_step_out_avoid_no_debug); 716 } else if (m_step_type == eStepTypeScripted) { 717 new_plan_sp = thread->QueueThreadPlanForStepScripted( 718 abort_other_plans, m_class_options.GetName().c_str(), 719 m_class_options.GetStructuredData(), bool_stop_other_threads, 720 new_plan_status); 721 } else { 722 result.AppendError("step type is not supported"); 723 result.SetStatus(eReturnStatusFailed); 724 return false; 725 } 726 727 // If we got a new plan, then set it to be a master plan (User level Plans 728 // should be master plans so that they can be interruptible). Then resume 729 // the process. 730 731 if (new_plan_sp) { 732 new_plan_sp->SetIsMasterPlan(true); 733 new_plan_sp->SetOkayToDiscard(false); 734 735 if (m_options.m_step_count > 1) { 736 if (!new_plan_sp->SetIterationCount(m_options.m_step_count)) { 737 result.AppendWarning( 738 "step operation does not support iteration count."); 739 } 740 } 741 742 process->GetThreadList().SetSelectedThreadByID(thread->GetID()); 743 744 const uint32_t iohandler_id = process->GetIOHandlerID(); 745 746 StreamString stream; 747 Status error; 748 if (synchronous_execution) 749 error = process->ResumeSynchronous(&stream); 750 else 751 error = process->Resume(); 752 753 if (!error.Success()) { 754 result.AppendMessage(error.AsCString()); 755 result.SetStatus(eReturnStatusFailed); 756 return false; 757 } 758 759 // There is a race condition where this thread will return up the call 760 // stack to the main command handler and show an (lldb) prompt before 761 // HandlePrivateEvent (from PrivateStateThread) has a chance to call 762 // PushProcessIOHandler(). 763 process->SyncIOHandler(iohandler_id, std::chrono::seconds(2)); 764 765 if (synchronous_execution) { 766 // If any state changed events had anything to say, add that to the 767 // result 768 if (stream.GetSize() > 0) 769 result.AppendMessage(stream.GetString()); 770 771 process->GetThreadList().SetSelectedThreadByID(thread->GetID()); 772 result.SetDidChangeProcessState(true); 773 result.SetStatus(eReturnStatusSuccessFinishNoResult); 774 } else { 775 result.SetStatus(eReturnStatusSuccessContinuingNoResult); 776 } 777 } else { 778 result.SetError(new_plan_status); 779 result.SetStatus(eReturnStatusFailed); 780 } 781 return result.Succeeded(); 782 } 783 784 protected: 785 StepType m_step_type; 786 StepScope m_step_scope; 787 ThreadStepScopeOptionGroup m_options; 788 OptionGroupPythonClassWithDict m_class_options; 789 OptionGroupOptions m_all_options; 790 }; 791 792 // CommandObjectThreadContinue 793 794 class CommandObjectThreadContinue : public CommandObjectParsed { 795 public: 796 CommandObjectThreadContinue(CommandInterpreter &interpreter) 797 : CommandObjectParsed( 798 interpreter, "thread continue", 799 "Continue execution of the current target process. One " 800 "or more threads may be specified, by default all " 801 "threads continue.", 802 nullptr, 803 eCommandRequiresThread | eCommandTryTargetAPILock | 804 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused) { 805 CommandArgumentEntry arg; 806 CommandArgumentData thread_idx_arg; 807 808 // Define the first (and only) variant of this arg. 809 thread_idx_arg.arg_type = eArgTypeThreadIndex; 810 thread_idx_arg.arg_repetition = eArgRepeatPlus; 811 812 // There is only one variant this argument could be; put it into the 813 // argument entry. 814 arg.push_back(thread_idx_arg); 815 816 // Push the data for the first argument into the m_arguments vector. 817 m_arguments.push_back(arg); 818 } 819 820 ~CommandObjectThreadContinue() override = default; 821 822 bool DoExecute(Args &command, CommandReturnObject &result) override { 823 bool synchronous_execution = m_interpreter.GetSynchronous(); 824 825 Process *process = m_exe_ctx.GetProcessPtr(); 826 if (process == nullptr) { 827 result.AppendError("no process exists. Cannot continue"); 828 result.SetStatus(eReturnStatusFailed); 829 return false; 830 } 831 832 StateType state = process->GetState(); 833 if ((state == eStateCrashed) || (state == eStateStopped) || 834 (state == eStateSuspended)) { 835 const size_t argc = command.GetArgumentCount(); 836 if (argc > 0) { 837 // These two lines appear at the beginning of both blocks in this 838 // if..else, but that is because we need to release the lock before 839 // calling process->Resume below. 840 std::lock_guard<std::recursive_mutex> guard( 841 process->GetThreadList().GetMutex()); 842 const uint32_t num_threads = process->GetThreadList().GetSize(); 843 std::vector<Thread *> resume_threads; 844 for (auto &entry : command.entries()) { 845 uint32_t thread_idx; 846 if (entry.ref().getAsInteger(0, thread_idx)) { 847 result.AppendErrorWithFormat( 848 "invalid thread index argument: \"%s\".\n", entry.c_str()); 849 result.SetStatus(eReturnStatusFailed); 850 return false; 851 } 852 Thread *thread = 853 process->GetThreadList().FindThreadByIndexID(thread_idx).get(); 854 855 if (thread) { 856 resume_threads.push_back(thread); 857 } else { 858 result.AppendErrorWithFormat("invalid thread index %u.\n", 859 thread_idx); 860 result.SetStatus(eReturnStatusFailed); 861 return false; 862 } 863 } 864 865 if (resume_threads.empty()) { 866 result.AppendError("no valid thread indexes were specified"); 867 result.SetStatus(eReturnStatusFailed); 868 return false; 869 } else { 870 if (resume_threads.size() == 1) 871 result.AppendMessageWithFormat("Resuming thread: "); 872 else 873 result.AppendMessageWithFormat("Resuming threads: "); 874 875 for (uint32_t idx = 0; idx < num_threads; ++idx) { 876 Thread *thread = 877 process->GetThreadList().GetThreadAtIndex(idx).get(); 878 std::vector<Thread *>::iterator this_thread_pos = 879 find(resume_threads.begin(), resume_threads.end(), thread); 880 881 if (this_thread_pos != resume_threads.end()) { 882 resume_threads.erase(this_thread_pos); 883 if (!resume_threads.empty()) 884 result.AppendMessageWithFormat("%u, ", thread->GetIndexID()); 885 else 886 result.AppendMessageWithFormat("%u ", thread->GetIndexID()); 887 888 const bool override_suspend = true; 889 thread->SetResumeState(eStateRunning, override_suspend); 890 } else { 891 thread->SetResumeState(eStateSuspended); 892 } 893 } 894 result.AppendMessageWithFormat("in process %" PRIu64 "\n", 895 process->GetID()); 896 } 897 } else { 898 // These two lines appear at the beginning of both blocks in this 899 // if..else, but that is because we need to release the lock before 900 // calling process->Resume below. 901 std::lock_guard<std::recursive_mutex> guard( 902 process->GetThreadList().GetMutex()); 903 const uint32_t num_threads = process->GetThreadList().GetSize(); 904 Thread *current_thread = GetDefaultThread(); 905 if (current_thread == nullptr) { 906 result.AppendError("the process doesn't have a current thread"); 907 result.SetStatus(eReturnStatusFailed); 908 return false; 909 } 910 // Set the actions that the threads should each take when resuming 911 for (uint32_t idx = 0; idx < num_threads; ++idx) { 912 Thread *thread = process->GetThreadList().GetThreadAtIndex(idx).get(); 913 if (thread == current_thread) { 914 result.AppendMessageWithFormat("Resuming thread 0x%4.4" PRIx64 915 " in process %" PRIu64 "\n", 916 thread->GetID(), process->GetID()); 917 const bool override_suspend = true; 918 thread->SetResumeState(eStateRunning, override_suspend); 919 } else { 920 thread->SetResumeState(eStateSuspended); 921 } 922 } 923 } 924 925 StreamString stream; 926 Status error; 927 if (synchronous_execution) 928 error = process->ResumeSynchronous(&stream); 929 else 930 error = process->Resume(); 931 932 // We should not be holding the thread list lock when we do this. 933 if (error.Success()) { 934 result.AppendMessageWithFormat("Process %" PRIu64 " resuming\n", 935 process->GetID()); 936 if (synchronous_execution) { 937 // If any state changed events had anything to say, add that to the 938 // result 939 if (stream.GetSize() > 0) 940 result.AppendMessage(stream.GetString()); 941 942 result.SetDidChangeProcessState(true); 943 result.SetStatus(eReturnStatusSuccessFinishNoResult); 944 } else { 945 result.SetStatus(eReturnStatusSuccessContinuingNoResult); 946 } 947 } else { 948 result.AppendErrorWithFormat("Failed to resume process: %s\n", 949 error.AsCString()); 950 result.SetStatus(eReturnStatusFailed); 951 } 952 } else { 953 result.AppendErrorWithFormat( 954 "Process cannot be continued from its current state (%s).\n", 955 StateAsCString(state)); 956 result.SetStatus(eReturnStatusFailed); 957 } 958 959 return result.Succeeded(); 960 } 961 }; 962 963 // CommandObjectThreadUntil 964 965 static constexpr OptionEnumValueElement g_duo_running_mode[] = { 966 {eOnlyThisThread, "this-thread", "Run only this thread"}, 967 {eAllThreads, "all-threads", "Run all threads"}}; 968 969 static constexpr OptionEnumValues DuoRunningModes() { 970 return OptionEnumValues(g_duo_running_mode); 971 } 972 973 #define LLDB_OPTIONS_thread_until 974 #include "CommandOptions.inc" 975 976 class CommandObjectThreadUntil : public CommandObjectParsed { 977 public: 978 class CommandOptions : public Options { 979 public: 980 uint32_t m_thread_idx; 981 uint32_t m_frame_idx; 982 983 CommandOptions() 984 : Options(), m_thread_idx(LLDB_INVALID_THREAD_ID), 985 m_frame_idx(LLDB_INVALID_FRAME_ID) { 986 // Keep default values of all options in one place: OptionParsingStarting 987 // () 988 OptionParsingStarting(nullptr); 989 } 990 991 ~CommandOptions() override = default; 992 993 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 994 ExecutionContext *execution_context) override { 995 Status error; 996 const int short_option = m_getopt_table[option_idx].val; 997 998 switch (short_option) { 999 case 'a': { 1000 lldb::addr_t tmp_addr = OptionArgParser::ToAddress( 1001 execution_context, option_arg, LLDB_INVALID_ADDRESS, &error); 1002 if (error.Success()) 1003 m_until_addrs.push_back(tmp_addr); 1004 } break; 1005 case 't': 1006 if (option_arg.getAsInteger(0, m_thread_idx)) { 1007 m_thread_idx = LLDB_INVALID_INDEX32; 1008 error.SetErrorStringWithFormat("invalid thread index '%s'", 1009 option_arg.str().c_str()); 1010 } 1011 break; 1012 case 'f': 1013 if (option_arg.getAsInteger(0, m_frame_idx)) { 1014 m_frame_idx = LLDB_INVALID_FRAME_ID; 1015 error.SetErrorStringWithFormat("invalid frame index '%s'", 1016 option_arg.str().c_str()); 1017 } 1018 break; 1019 case 'm': { 1020 auto enum_values = GetDefinitions()[option_idx].enum_values; 1021 lldb::RunMode run_mode = (lldb::RunMode)OptionArgParser::ToOptionEnum( 1022 option_arg, enum_values, eOnlyDuringStepping, error); 1023 1024 if (error.Success()) { 1025 if (run_mode == eAllThreads) 1026 m_stop_others = false; 1027 else 1028 m_stop_others = true; 1029 } 1030 } break; 1031 default: 1032 llvm_unreachable("Unimplemented option"); 1033 } 1034 return error; 1035 } 1036 1037 void OptionParsingStarting(ExecutionContext *execution_context) override { 1038 m_thread_idx = LLDB_INVALID_THREAD_ID; 1039 m_frame_idx = 0; 1040 m_stop_others = false; 1041 m_until_addrs.clear(); 1042 } 1043 1044 llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 1045 return llvm::makeArrayRef(g_thread_until_options); 1046 } 1047 1048 uint32_t m_step_thread_idx; 1049 bool m_stop_others; 1050 std::vector<lldb::addr_t> m_until_addrs; 1051 1052 // Instance variables to hold the values for command options. 1053 }; 1054 1055 CommandObjectThreadUntil(CommandInterpreter &interpreter) 1056 : CommandObjectParsed( 1057 interpreter, "thread until", 1058 "Continue until a line number or address is reached by the " 1059 "current or specified thread. Stops when returning from " 1060 "the current function as a safety measure. " 1061 "The target line number(s) are given as arguments, and if more " 1062 "than one" 1063 " is provided, stepping will stop when the first one is hit.", 1064 nullptr, 1065 eCommandRequiresThread | eCommandTryTargetAPILock | 1066 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused), 1067 m_options() { 1068 CommandArgumentEntry arg; 1069 CommandArgumentData line_num_arg; 1070 1071 // Define the first (and only) variant of this arg. 1072 line_num_arg.arg_type = eArgTypeLineNum; 1073 line_num_arg.arg_repetition = eArgRepeatPlain; 1074 1075 // There is only one variant this argument could be; put it into the 1076 // argument entry. 1077 arg.push_back(line_num_arg); 1078 1079 // Push the data for the first argument into the m_arguments vector. 1080 m_arguments.push_back(arg); 1081 } 1082 1083 ~CommandObjectThreadUntil() override = default; 1084 1085 Options *GetOptions() override { return &m_options; } 1086 1087 protected: 1088 bool DoExecute(Args &command, CommandReturnObject &result) override { 1089 bool synchronous_execution = m_interpreter.GetSynchronous(); 1090 1091 Target *target = &GetSelectedTarget(); 1092 1093 Process *process = m_exe_ctx.GetProcessPtr(); 1094 if (process == nullptr) { 1095 result.AppendError("need a valid process to step"); 1096 result.SetStatus(eReturnStatusFailed); 1097 } else { 1098 Thread *thread = nullptr; 1099 std::vector<uint32_t> line_numbers; 1100 1101 if (command.GetArgumentCount() >= 1) { 1102 size_t num_args = command.GetArgumentCount(); 1103 for (size_t i = 0; i < num_args; i++) { 1104 uint32_t line_number; 1105 line_number = StringConvert::ToUInt32(command.GetArgumentAtIndex(i), 1106 UINT32_MAX); 1107 if (line_number == UINT32_MAX) { 1108 result.AppendErrorWithFormat("invalid line number: '%s'.\n", 1109 command.GetArgumentAtIndex(i)); 1110 result.SetStatus(eReturnStatusFailed); 1111 return false; 1112 } else 1113 line_numbers.push_back(line_number); 1114 } 1115 } else if (m_options.m_until_addrs.empty()) { 1116 result.AppendErrorWithFormat("No line number or address provided:\n%s", 1117 GetSyntax().str().c_str()); 1118 result.SetStatus(eReturnStatusFailed); 1119 return false; 1120 } 1121 1122 if (m_options.m_thread_idx == LLDB_INVALID_THREAD_ID) { 1123 thread = GetDefaultThread(); 1124 } else { 1125 thread = process->GetThreadList() 1126 .FindThreadByIndexID(m_options.m_thread_idx) 1127 .get(); 1128 } 1129 1130 if (thread == nullptr) { 1131 const uint32_t num_threads = process->GetThreadList().GetSize(); 1132 result.AppendErrorWithFormat( 1133 "Thread index %u is out of range (valid values are 0 - %u).\n", 1134 m_options.m_thread_idx, num_threads); 1135 result.SetStatus(eReturnStatusFailed); 1136 return false; 1137 } 1138 1139 const bool abort_other_plans = false; 1140 1141 StackFrame *frame = 1142 thread->GetStackFrameAtIndex(m_options.m_frame_idx).get(); 1143 if (frame == nullptr) { 1144 result.AppendErrorWithFormat( 1145 "Frame index %u is out of range for thread %u.\n", 1146 m_options.m_frame_idx, m_options.m_thread_idx); 1147 result.SetStatus(eReturnStatusFailed); 1148 return false; 1149 } 1150 1151 ThreadPlanSP new_plan_sp; 1152 Status new_plan_status; 1153 1154 if (frame->HasDebugInformation()) { 1155 // Finally we got here... Translate the given line number to a bunch 1156 // of addresses: 1157 SymbolContext sc(frame->GetSymbolContext(eSymbolContextCompUnit)); 1158 LineTable *line_table = nullptr; 1159 if (sc.comp_unit) 1160 line_table = sc.comp_unit->GetLineTable(); 1161 1162 if (line_table == nullptr) { 1163 result.AppendErrorWithFormat("Failed to resolve the line table for " 1164 "frame %u of thread index %u.\n", 1165 m_options.m_frame_idx, 1166 m_options.m_thread_idx); 1167 result.SetStatus(eReturnStatusFailed); 1168 return false; 1169 } 1170 1171 LineEntry function_start; 1172 uint32_t index_ptr = 0, end_ptr; 1173 std::vector<addr_t> address_list; 1174 1175 // Find the beginning & end index of the 1176 AddressRange fun_addr_range = sc.function->GetAddressRange(); 1177 Address fun_start_addr = fun_addr_range.GetBaseAddress(); 1178 line_table->FindLineEntryByAddress(fun_start_addr, function_start, 1179 &index_ptr); 1180 1181 Address fun_end_addr(fun_start_addr.GetSection(), 1182 fun_start_addr.GetOffset() + 1183 fun_addr_range.GetByteSize()); 1184 1185 bool all_in_function = true; 1186 1187 line_table->FindLineEntryByAddress(fun_end_addr, function_start, 1188 &end_ptr); 1189 1190 for (uint32_t line_number : line_numbers) { 1191 uint32_t start_idx_ptr = index_ptr; 1192 while (start_idx_ptr <= end_ptr) { 1193 LineEntry line_entry; 1194 const bool exact = false; 1195 start_idx_ptr = sc.comp_unit->FindLineEntry( 1196 start_idx_ptr, line_number, sc.comp_unit, exact, &line_entry); 1197 if (start_idx_ptr == UINT32_MAX) 1198 break; 1199 1200 addr_t address = 1201 line_entry.range.GetBaseAddress().GetLoadAddress(target); 1202 if (address != LLDB_INVALID_ADDRESS) { 1203 if (fun_addr_range.ContainsLoadAddress(address, target)) 1204 address_list.push_back(address); 1205 else 1206 all_in_function = false; 1207 } 1208 start_idx_ptr++; 1209 } 1210 } 1211 1212 for (lldb::addr_t address : m_options.m_until_addrs) { 1213 if (fun_addr_range.ContainsLoadAddress(address, target)) 1214 address_list.push_back(address); 1215 else 1216 all_in_function = false; 1217 } 1218 1219 if (address_list.empty()) { 1220 if (all_in_function) 1221 result.AppendErrorWithFormat( 1222 "No line entries matching until target.\n"); 1223 else 1224 result.AppendErrorWithFormat( 1225 "Until target outside of the current function.\n"); 1226 1227 result.SetStatus(eReturnStatusFailed); 1228 return false; 1229 } 1230 1231 new_plan_sp = thread->QueueThreadPlanForStepUntil( 1232 abort_other_plans, &address_list.front(), address_list.size(), 1233 m_options.m_stop_others, m_options.m_frame_idx, new_plan_status); 1234 if (new_plan_sp) { 1235 // User level plans should be master plans so they can be interrupted 1236 // (e.g. by hitting a breakpoint) and other plans executed by the 1237 // user (stepping around the breakpoint) and then a "continue" will 1238 // resume the original plan. 1239 new_plan_sp->SetIsMasterPlan(true); 1240 new_plan_sp->SetOkayToDiscard(false); 1241 } else { 1242 result.SetError(new_plan_status); 1243 result.SetStatus(eReturnStatusFailed); 1244 return false; 1245 } 1246 } else { 1247 result.AppendErrorWithFormat( 1248 "Frame index %u of thread %u has no debug information.\n", 1249 m_options.m_frame_idx, m_options.m_thread_idx); 1250 result.SetStatus(eReturnStatusFailed); 1251 return false; 1252 } 1253 1254 process->GetThreadList().SetSelectedThreadByID(m_options.m_thread_idx); 1255 1256 StreamString stream; 1257 Status error; 1258 if (synchronous_execution) 1259 error = process->ResumeSynchronous(&stream); 1260 else 1261 error = process->Resume(); 1262 1263 if (error.Success()) { 1264 result.AppendMessageWithFormat("Process %" PRIu64 " resuming\n", 1265 process->GetID()); 1266 if (synchronous_execution) { 1267 // If any state changed events had anything to say, add that to the 1268 // result 1269 if (stream.GetSize() > 0) 1270 result.AppendMessage(stream.GetString()); 1271 1272 result.SetDidChangeProcessState(true); 1273 result.SetStatus(eReturnStatusSuccessFinishNoResult); 1274 } else { 1275 result.SetStatus(eReturnStatusSuccessContinuingNoResult); 1276 } 1277 } else { 1278 result.AppendErrorWithFormat("Failed to resume process: %s.\n", 1279 error.AsCString()); 1280 result.SetStatus(eReturnStatusFailed); 1281 } 1282 } 1283 return result.Succeeded(); 1284 } 1285 1286 CommandOptions m_options; 1287 }; 1288 1289 // CommandObjectThreadSelect 1290 1291 class CommandObjectThreadSelect : public CommandObjectParsed { 1292 public: 1293 CommandObjectThreadSelect(CommandInterpreter &interpreter) 1294 : CommandObjectParsed(interpreter, "thread select", 1295 "Change the currently selected thread.", nullptr, 1296 eCommandRequiresProcess | eCommandTryTargetAPILock | 1297 eCommandProcessMustBeLaunched | 1298 eCommandProcessMustBePaused) { 1299 CommandArgumentEntry arg; 1300 CommandArgumentData thread_idx_arg; 1301 1302 // Define the first (and only) variant of this arg. 1303 thread_idx_arg.arg_type = eArgTypeThreadIndex; 1304 thread_idx_arg.arg_repetition = eArgRepeatPlain; 1305 1306 // There is only one variant this argument could be; put it into the 1307 // argument entry. 1308 arg.push_back(thread_idx_arg); 1309 1310 // Push the data for the first argument into the m_arguments vector. 1311 m_arguments.push_back(arg); 1312 } 1313 1314 ~CommandObjectThreadSelect() override = default; 1315 1316 protected: 1317 bool DoExecute(Args &command, CommandReturnObject &result) override { 1318 Process *process = m_exe_ctx.GetProcessPtr(); 1319 if (process == nullptr) { 1320 result.AppendError("no process"); 1321 result.SetStatus(eReturnStatusFailed); 1322 return false; 1323 } else if (command.GetArgumentCount() != 1) { 1324 result.AppendErrorWithFormat( 1325 "'%s' takes exactly one thread index argument:\nUsage: %s\n", 1326 m_cmd_name.c_str(), m_cmd_syntax.c_str()); 1327 result.SetStatus(eReturnStatusFailed); 1328 return false; 1329 } 1330 1331 uint32_t index_id = 1332 StringConvert::ToUInt32(command.GetArgumentAtIndex(0), 0, 0); 1333 1334 Thread *new_thread = 1335 process->GetThreadList().FindThreadByIndexID(index_id).get(); 1336 if (new_thread == nullptr) { 1337 result.AppendErrorWithFormat("invalid thread #%s.\n", 1338 command.GetArgumentAtIndex(0)); 1339 result.SetStatus(eReturnStatusFailed); 1340 return false; 1341 } 1342 1343 process->GetThreadList().SetSelectedThreadByID(new_thread->GetID(), true); 1344 result.SetStatus(eReturnStatusSuccessFinishNoResult); 1345 1346 return result.Succeeded(); 1347 } 1348 }; 1349 1350 // CommandObjectThreadList 1351 1352 class CommandObjectThreadList : public CommandObjectParsed { 1353 public: 1354 CommandObjectThreadList(CommandInterpreter &interpreter) 1355 : CommandObjectParsed( 1356 interpreter, "thread list", 1357 "Show a summary of each thread in the current target process. " 1358 "Use 'settings set thread-format' to customize the individual " 1359 "thread listings.", 1360 "thread list", 1361 eCommandRequiresProcess | eCommandTryTargetAPILock | 1362 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused) {} 1363 1364 ~CommandObjectThreadList() override = default; 1365 1366 protected: 1367 bool DoExecute(Args &command, CommandReturnObject &result) override { 1368 Stream &strm = result.GetOutputStream(); 1369 result.SetStatus(eReturnStatusSuccessFinishNoResult); 1370 Process *process = m_exe_ctx.GetProcessPtr(); 1371 const bool only_threads_with_stop_reason = false; 1372 const uint32_t start_frame = 0; 1373 const uint32_t num_frames = 0; 1374 const uint32_t num_frames_with_source = 0; 1375 process->GetStatus(strm); 1376 process->GetThreadStatus(strm, only_threads_with_stop_reason, start_frame, 1377 num_frames, num_frames_with_source, false); 1378 return result.Succeeded(); 1379 } 1380 }; 1381 1382 // CommandObjectThreadInfo 1383 #define LLDB_OPTIONS_thread_info 1384 #include "CommandOptions.inc" 1385 1386 class CommandObjectThreadInfo : public CommandObjectIterateOverThreads { 1387 public: 1388 class CommandOptions : public Options { 1389 public: 1390 CommandOptions() : Options() { OptionParsingStarting(nullptr); } 1391 1392 ~CommandOptions() override = default; 1393 1394 void OptionParsingStarting(ExecutionContext *execution_context) override { 1395 m_json_thread = false; 1396 m_json_stopinfo = false; 1397 } 1398 1399 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 1400 ExecutionContext *execution_context) override { 1401 const int short_option = m_getopt_table[option_idx].val; 1402 Status error; 1403 1404 switch (short_option) { 1405 case 'j': 1406 m_json_thread = true; 1407 break; 1408 1409 case 's': 1410 m_json_stopinfo = true; 1411 break; 1412 1413 default: 1414 llvm_unreachable("Unimplemented option"); 1415 } 1416 return error; 1417 } 1418 1419 llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 1420 return llvm::makeArrayRef(g_thread_info_options); 1421 } 1422 1423 bool m_json_thread; 1424 bool m_json_stopinfo; 1425 }; 1426 1427 CommandObjectThreadInfo(CommandInterpreter &interpreter) 1428 : CommandObjectIterateOverThreads( 1429 interpreter, "thread info", 1430 "Show an extended summary of one or " 1431 "more threads. Defaults to the " 1432 "current thread.", 1433 "thread info", 1434 eCommandRequiresProcess | eCommandTryTargetAPILock | 1435 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused), 1436 m_options() { 1437 m_add_return = false; 1438 } 1439 1440 ~CommandObjectThreadInfo() override = default; 1441 1442 Options *GetOptions() override { return &m_options; } 1443 1444 bool HandleOneThread(lldb::tid_t tid, CommandReturnObject &result) override { 1445 ThreadSP thread_sp = 1446 m_exe_ctx.GetProcessPtr()->GetThreadList().FindThreadByID(tid); 1447 if (!thread_sp) { 1448 result.AppendErrorWithFormat("thread no longer exists: 0x%" PRIx64 "\n", 1449 tid); 1450 result.SetStatus(eReturnStatusFailed); 1451 return false; 1452 } 1453 1454 Thread *thread = thread_sp.get(); 1455 1456 Stream &strm = result.GetOutputStream(); 1457 if (!thread->GetDescription(strm, eDescriptionLevelFull, 1458 m_options.m_json_thread, 1459 m_options.m_json_stopinfo)) { 1460 result.AppendErrorWithFormat("error displaying info for thread: \"%d\"\n", 1461 thread->GetIndexID()); 1462 result.SetStatus(eReturnStatusFailed); 1463 return false; 1464 } 1465 return true; 1466 } 1467 1468 CommandOptions m_options; 1469 }; 1470 1471 // CommandObjectThreadException 1472 1473 class CommandObjectThreadException : public CommandObjectIterateOverThreads { 1474 public: 1475 CommandObjectThreadException(CommandInterpreter &interpreter) 1476 : CommandObjectIterateOverThreads( 1477 interpreter, "thread exception", 1478 "Display the current exception object for a thread. Defaults to " 1479 "the current thread.", 1480 "thread exception", 1481 eCommandRequiresProcess | eCommandTryTargetAPILock | 1482 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused) {} 1483 1484 ~CommandObjectThreadException() override = default; 1485 1486 bool HandleOneThread(lldb::tid_t tid, CommandReturnObject &result) override { 1487 ThreadSP thread_sp = 1488 m_exe_ctx.GetProcessPtr()->GetThreadList().FindThreadByID(tid); 1489 if (!thread_sp) { 1490 result.AppendErrorWithFormat("thread no longer exists: 0x%" PRIx64 "\n", 1491 tid); 1492 result.SetStatus(eReturnStatusFailed); 1493 return false; 1494 } 1495 1496 Stream &strm = result.GetOutputStream(); 1497 ValueObjectSP exception_object_sp = thread_sp->GetCurrentException(); 1498 if (exception_object_sp) { 1499 exception_object_sp->Dump(strm); 1500 } 1501 1502 ThreadSP exception_thread_sp = thread_sp->GetCurrentExceptionBacktrace(); 1503 if (exception_thread_sp && exception_thread_sp->IsValid()) { 1504 const uint32_t num_frames_with_source = 0; 1505 const bool stop_format = false; 1506 exception_thread_sp->GetStatus(strm, 0, UINT32_MAX, 1507 num_frames_with_source, stop_format); 1508 } 1509 1510 return true; 1511 } 1512 }; 1513 1514 // CommandObjectThreadReturn 1515 #define LLDB_OPTIONS_thread_return 1516 #include "CommandOptions.inc" 1517 1518 class CommandObjectThreadReturn : public CommandObjectRaw { 1519 public: 1520 class CommandOptions : public Options { 1521 public: 1522 CommandOptions() : Options(), m_from_expression(false) { 1523 // Keep default values of all options in one place: OptionParsingStarting 1524 // () 1525 OptionParsingStarting(nullptr); 1526 } 1527 1528 ~CommandOptions() override = default; 1529 1530 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 1531 ExecutionContext *execution_context) override { 1532 Status error; 1533 const int short_option = m_getopt_table[option_idx].val; 1534 1535 switch (short_option) { 1536 case 'x': { 1537 bool success; 1538 bool tmp_value = 1539 OptionArgParser::ToBoolean(option_arg, false, &success); 1540 if (success) 1541 m_from_expression = tmp_value; 1542 else { 1543 error.SetErrorStringWithFormat( 1544 "invalid boolean value '%s' for 'x' option", 1545 option_arg.str().c_str()); 1546 } 1547 } break; 1548 default: 1549 llvm_unreachable("Unimplemented option"); 1550 } 1551 return error; 1552 } 1553 1554 void OptionParsingStarting(ExecutionContext *execution_context) override { 1555 m_from_expression = false; 1556 } 1557 1558 llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 1559 return llvm::makeArrayRef(g_thread_return_options); 1560 } 1561 1562 bool m_from_expression; 1563 1564 // Instance variables to hold the values for command options. 1565 }; 1566 1567 CommandObjectThreadReturn(CommandInterpreter &interpreter) 1568 : CommandObjectRaw(interpreter, "thread return", 1569 "Prematurely return from a stack frame, " 1570 "short-circuiting execution of newer frames " 1571 "and optionally yielding a specified value. Defaults " 1572 "to the exiting the current stack " 1573 "frame.", 1574 "thread return", 1575 eCommandRequiresFrame | eCommandTryTargetAPILock | 1576 eCommandProcessMustBeLaunched | 1577 eCommandProcessMustBePaused), 1578 m_options() { 1579 CommandArgumentEntry arg; 1580 CommandArgumentData expression_arg; 1581 1582 // Define the first (and only) variant of this arg. 1583 expression_arg.arg_type = eArgTypeExpression; 1584 expression_arg.arg_repetition = eArgRepeatOptional; 1585 1586 // There is only one variant this argument could be; put it into the 1587 // argument entry. 1588 arg.push_back(expression_arg); 1589 1590 // Push the data for the first argument into the m_arguments vector. 1591 m_arguments.push_back(arg); 1592 } 1593 1594 ~CommandObjectThreadReturn() override = default; 1595 1596 Options *GetOptions() override { return &m_options; } 1597 1598 protected: 1599 bool DoExecute(llvm::StringRef command, 1600 CommandReturnObject &result) override { 1601 // I am going to handle this by hand, because I don't want you to have to 1602 // say: 1603 // "thread return -- -5". 1604 if (command.startswith("-x")) { 1605 if (command.size() != 2U) 1606 result.AppendWarning("Return values ignored when returning from user " 1607 "called expressions"); 1608 1609 Thread *thread = m_exe_ctx.GetThreadPtr(); 1610 Status error; 1611 error = thread->UnwindInnermostExpression(); 1612 if (!error.Success()) { 1613 result.AppendErrorWithFormat("Unwinding expression failed - %s.", 1614 error.AsCString()); 1615 result.SetStatus(eReturnStatusFailed); 1616 } else { 1617 bool success = 1618 thread->SetSelectedFrameByIndexNoisily(0, result.GetOutputStream()); 1619 if (success) { 1620 m_exe_ctx.SetFrameSP(thread->GetSelectedFrame()); 1621 result.SetStatus(eReturnStatusSuccessFinishResult); 1622 } else { 1623 result.AppendErrorWithFormat( 1624 "Could not select 0th frame after unwinding expression."); 1625 result.SetStatus(eReturnStatusFailed); 1626 } 1627 } 1628 return result.Succeeded(); 1629 } 1630 1631 ValueObjectSP return_valobj_sp; 1632 1633 StackFrameSP frame_sp = m_exe_ctx.GetFrameSP(); 1634 uint32_t frame_idx = frame_sp->GetFrameIndex(); 1635 1636 if (frame_sp->IsInlined()) { 1637 result.AppendError("Don't know how to return from inlined frames."); 1638 result.SetStatus(eReturnStatusFailed); 1639 return false; 1640 } 1641 1642 if (!command.empty()) { 1643 Target *target = m_exe_ctx.GetTargetPtr(); 1644 EvaluateExpressionOptions options; 1645 1646 options.SetUnwindOnError(true); 1647 options.SetUseDynamic(eNoDynamicValues); 1648 1649 ExpressionResults exe_results = eExpressionSetupError; 1650 exe_results = target->EvaluateExpression(command, frame_sp.get(), 1651 return_valobj_sp, options); 1652 if (exe_results != eExpressionCompleted) { 1653 if (return_valobj_sp) 1654 result.AppendErrorWithFormat( 1655 "Error evaluating result expression: %s", 1656 return_valobj_sp->GetError().AsCString()); 1657 else 1658 result.AppendErrorWithFormat( 1659 "Unknown error evaluating result expression."); 1660 result.SetStatus(eReturnStatusFailed); 1661 return false; 1662 } 1663 } 1664 1665 Status error; 1666 ThreadSP thread_sp = m_exe_ctx.GetThreadSP(); 1667 const bool broadcast = true; 1668 error = thread_sp->ReturnFromFrame(frame_sp, return_valobj_sp, broadcast); 1669 if (!error.Success()) { 1670 result.AppendErrorWithFormat( 1671 "Error returning from frame %d of thread %d: %s.", frame_idx, 1672 thread_sp->GetIndexID(), error.AsCString()); 1673 result.SetStatus(eReturnStatusFailed); 1674 return false; 1675 } 1676 1677 result.SetStatus(eReturnStatusSuccessFinishResult); 1678 return true; 1679 } 1680 1681 CommandOptions m_options; 1682 }; 1683 1684 // CommandObjectThreadJump 1685 #define LLDB_OPTIONS_thread_jump 1686 #include "CommandOptions.inc" 1687 1688 class CommandObjectThreadJump : public CommandObjectParsed { 1689 public: 1690 class CommandOptions : public Options { 1691 public: 1692 CommandOptions() : Options() { OptionParsingStarting(nullptr); } 1693 1694 ~CommandOptions() override = default; 1695 1696 void OptionParsingStarting(ExecutionContext *execution_context) override { 1697 m_filenames.Clear(); 1698 m_line_num = 0; 1699 m_line_offset = 0; 1700 m_load_addr = LLDB_INVALID_ADDRESS; 1701 m_force = false; 1702 } 1703 1704 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 1705 ExecutionContext *execution_context) override { 1706 const int short_option = m_getopt_table[option_idx].val; 1707 Status error; 1708 1709 switch (short_option) { 1710 case 'f': 1711 m_filenames.AppendIfUnique(FileSpec(option_arg)); 1712 if (m_filenames.GetSize() > 1) 1713 return Status("only one source file expected."); 1714 break; 1715 case 'l': 1716 if (option_arg.getAsInteger(0, m_line_num)) 1717 return Status("invalid line number: '%s'.", option_arg.str().c_str()); 1718 break; 1719 case 'b': 1720 if (option_arg.getAsInteger(0, m_line_offset)) 1721 return Status("invalid line offset: '%s'.", option_arg.str().c_str()); 1722 break; 1723 case 'a': 1724 m_load_addr = OptionArgParser::ToAddress(execution_context, option_arg, 1725 LLDB_INVALID_ADDRESS, &error); 1726 break; 1727 case 'r': 1728 m_force = true; 1729 break; 1730 default: 1731 llvm_unreachable("Unimplemented option"); 1732 } 1733 return error; 1734 } 1735 1736 llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 1737 return llvm::makeArrayRef(g_thread_jump_options); 1738 } 1739 1740 FileSpecList m_filenames; 1741 uint32_t m_line_num; 1742 int32_t m_line_offset; 1743 lldb::addr_t m_load_addr; 1744 bool m_force; 1745 }; 1746 1747 CommandObjectThreadJump(CommandInterpreter &interpreter) 1748 : CommandObjectParsed( 1749 interpreter, "thread jump", 1750 "Sets the program counter to a new address.", "thread jump", 1751 eCommandRequiresFrame | eCommandTryTargetAPILock | 1752 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused), 1753 m_options() {} 1754 1755 ~CommandObjectThreadJump() override = default; 1756 1757 Options *GetOptions() override { return &m_options; } 1758 1759 protected: 1760 bool DoExecute(Args &args, CommandReturnObject &result) override { 1761 RegisterContext *reg_ctx = m_exe_ctx.GetRegisterContext(); 1762 StackFrame *frame = m_exe_ctx.GetFramePtr(); 1763 Thread *thread = m_exe_ctx.GetThreadPtr(); 1764 Target *target = m_exe_ctx.GetTargetPtr(); 1765 const SymbolContext &sym_ctx = 1766 frame->GetSymbolContext(eSymbolContextLineEntry); 1767 1768 if (m_options.m_load_addr != LLDB_INVALID_ADDRESS) { 1769 // Use this address directly. 1770 Address dest = Address(m_options.m_load_addr); 1771 1772 lldb::addr_t callAddr = dest.GetCallableLoadAddress(target); 1773 if (callAddr == LLDB_INVALID_ADDRESS) { 1774 result.AppendErrorWithFormat("Invalid destination address."); 1775 result.SetStatus(eReturnStatusFailed); 1776 return false; 1777 } 1778 1779 if (!reg_ctx->SetPC(callAddr)) { 1780 result.AppendErrorWithFormat("Error changing PC value for thread %d.", 1781 thread->GetIndexID()); 1782 result.SetStatus(eReturnStatusFailed); 1783 return false; 1784 } 1785 } else { 1786 // Pick either the absolute line, or work out a relative one. 1787 int32_t line = (int32_t)m_options.m_line_num; 1788 if (line == 0) 1789 line = sym_ctx.line_entry.line + m_options.m_line_offset; 1790 1791 // Try the current file, but override if asked. 1792 FileSpec file = sym_ctx.line_entry.file; 1793 if (m_options.m_filenames.GetSize() == 1) 1794 file = m_options.m_filenames.GetFileSpecAtIndex(0); 1795 1796 if (!file) { 1797 result.AppendErrorWithFormat( 1798 "No source file available for the current location."); 1799 result.SetStatus(eReturnStatusFailed); 1800 return false; 1801 } 1802 1803 std::string warnings; 1804 Status err = thread->JumpToLine(file, line, m_options.m_force, &warnings); 1805 1806 if (err.Fail()) { 1807 result.SetError(err); 1808 return false; 1809 } 1810 1811 if (!warnings.empty()) 1812 result.AppendWarning(warnings.c_str()); 1813 } 1814 1815 result.SetStatus(eReturnStatusSuccessFinishResult); 1816 return true; 1817 } 1818 1819 CommandOptions m_options; 1820 }; 1821 1822 // Next are the subcommands of CommandObjectMultiwordThreadPlan 1823 1824 // CommandObjectThreadPlanList 1825 #define LLDB_OPTIONS_thread_plan_list 1826 #include "CommandOptions.inc" 1827 1828 class CommandObjectThreadPlanList : public CommandObjectIterateOverThreads { 1829 public: 1830 class CommandOptions : public Options { 1831 public: 1832 CommandOptions() : Options() { 1833 // Keep default values of all options in one place: OptionParsingStarting 1834 // () 1835 OptionParsingStarting(nullptr); 1836 } 1837 1838 ~CommandOptions() override = default; 1839 1840 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 1841 ExecutionContext *execution_context) override { 1842 Status error; 1843 const int short_option = m_getopt_table[option_idx].val; 1844 1845 switch (short_option) { 1846 case 'i': 1847 m_internal = true; 1848 break; 1849 case 'v': 1850 m_verbose = true; 1851 break; 1852 default: 1853 llvm_unreachable("Unimplemented option"); 1854 } 1855 return error; 1856 } 1857 1858 void OptionParsingStarting(ExecutionContext *execution_context) override { 1859 m_verbose = false; 1860 m_internal = false; 1861 } 1862 1863 llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 1864 return llvm::makeArrayRef(g_thread_plan_list_options); 1865 } 1866 1867 // Instance variables to hold the values for command options. 1868 bool m_verbose; 1869 bool m_internal; 1870 }; 1871 1872 CommandObjectThreadPlanList(CommandInterpreter &interpreter) 1873 : CommandObjectIterateOverThreads( 1874 interpreter, "thread plan list", 1875 "Show thread plans for one or more threads. If no threads are " 1876 "specified, show the " 1877 "current thread. Use the thread-index \"all\" to see all threads.", 1878 nullptr, 1879 eCommandRequiresProcess | eCommandRequiresThread | 1880 eCommandTryTargetAPILock | eCommandProcessMustBeLaunched | 1881 eCommandProcessMustBePaused), 1882 m_options() {} 1883 1884 ~CommandObjectThreadPlanList() override = default; 1885 1886 Options *GetOptions() override { return &m_options; } 1887 1888 protected: 1889 bool HandleOneThread(lldb::tid_t tid, CommandReturnObject &result) override { 1890 ThreadSP thread_sp = 1891 m_exe_ctx.GetProcessPtr()->GetThreadList().FindThreadByID(tid); 1892 if (!thread_sp) { 1893 result.AppendErrorWithFormat("thread no longer exists: 0x%" PRIx64 "\n", 1894 tid); 1895 result.SetStatus(eReturnStatusFailed); 1896 return false; 1897 } 1898 1899 Thread *thread = thread_sp.get(); 1900 1901 Stream &strm = result.GetOutputStream(); 1902 DescriptionLevel desc_level = eDescriptionLevelFull; 1903 if (m_options.m_verbose) 1904 desc_level = eDescriptionLevelVerbose; 1905 1906 thread->DumpThreadPlans(&strm, desc_level, m_options.m_internal, true); 1907 return true; 1908 } 1909 1910 CommandOptions m_options; 1911 }; 1912 1913 class CommandObjectThreadPlanDiscard : public CommandObjectParsed { 1914 public: 1915 CommandObjectThreadPlanDiscard(CommandInterpreter &interpreter) 1916 : CommandObjectParsed(interpreter, "thread plan discard", 1917 "Discards thread plans up to and including the " 1918 "specified index (see 'thread plan list'.) " 1919 "Only user visible plans can be discarded.", 1920 nullptr, 1921 eCommandRequiresProcess | eCommandRequiresThread | 1922 eCommandTryTargetAPILock | 1923 eCommandProcessMustBeLaunched | 1924 eCommandProcessMustBePaused) { 1925 CommandArgumentEntry arg; 1926 CommandArgumentData plan_index_arg; 1927 1928 // Define the first (and only) variant of this arg. 1929 plan_index_arg.arg_type = eArgTypeUnsignedInteger; 1930 plan_index_arg.arg_repetition = eArgRepeatPlain; 1931 1932 // There is only one variant this argument could be; put it into the 1933 // argument entry. 1934 arg.push_back(plan_index_arg); 1935 1936 // Push the data for the first argument into the m_arguments vector. 1937 m_arguments.push_back(arg); 1938 } 1939 1940 ~CommandObjectThreadPlanDiscard() override = default; 1941 1942 bool DoExecute(Args &args, CommandReturnObject &result) override { 1943 Thread *thread = m_exe_ctx.GetThreadPtr(); 1944 if (args.GetArgumentCount() != 1) { 1945 result.AppendErrorWithFormat("Too many arguments, expected one - the " 1946 "thread plan index - but got %zu.", 1947 args.GetArgumentCount()); 1948 result.SetStatus(eReturnStatusFailed); 1949 return false; 1950 } 1951 1952 bool success; 1953 uint32_t thread_plan_idx = 1954 StringConvert::ToUInt32(args.GetArgumentAtIndex(0), 0, 0, &success); 1955 if (!success) { 1956 result.AppendErrorWithFormat( 1957 "Invalid thread index: \"%s\" - should be unsigned int.", 1958 args.GetArgumentAtIndex(0)); 1959 result.SetStatus(eReturnStatusFailed); 1960 return false; 1961 } 1962 1963 if (thread_plan_idx == 0) { 1964 result.AppendErrorWithFormat( 1965 "You wouldn't really want me to discard the base thread plan."); 1966 result.SetStatus(eReturnStatusFailed); 1967 return false; 1968 } 1969 1970 if (thread->DiscardUserThreadPlansUpToIndex(thread_plan_idx)) { 1971 result.SetStatus(eReturnStatusSuccessFinishNoResult); 1972 return true; 1973 } else { 1974 result.AppendErrorWithFormat( 1975 "Could not find User thread plan with index %s.", 1976 args.GetArgumentAtIndex(0)); 1977 result.SetStatus(eReturnStatusFailed); 1978 return false; 1979 } 1980 } 1981 }; 1982 1983 // CommandObjectMultiwordThreadPlan 1984 1985 class CommandObjectMultiwordThreadPlan : public CommandObjectMultiword { 1986 public: 1987 CommandObjectMultiwordThreadPlan(CommandInterpreter &interpreter) 1988 : CommandObjectMultiword( 1989 interpreter, "plan", 1990 "Commands for managing thread plans that control execution.", 1991 "thread plan <subcommand> [<subcommand objects]") { 1992 LoadSubCommand( 1993 "list", CommandObjectSP(new CommandObjectThreadPlanList(interpreter))); 1994 LoadSubCommand( 1995 "discard", 1996 CommandObjectSP(new CommandObjectThreadPlanDiscard(interpreter))); 1997 } 1998 1999 ~CommandObjectMultiwordThreadPlan() override = default; 2000 }; 2001 2002 // CommandObjectMultiwordThread 2003 2004 CommandObjectMultiwordThread::CommandObjectMultiwordThread( 2005 CommandInterpreter &interpreter) 2006 : CommandObjectMultiword(interpreter, "thread", 2007 "Commands for operating on " 2008 "one or more threads in " 2009 "the current process.", 2010 "thread <subcommand> [<subcommand-options>]") { 2011 LoadSubCommand("backtrace", CommandObjectSP(new CommandObjectThreadBacktrace( 2012 interpreter))); 2013 LoadSubCommand("continue", 2014 CommandObjectSP(new CommandObjectThreadContinue(interpreter))); 2015 LoadSubCommand("list", 2016 CommandObjectSP(new CommandObjectThreadList(interpreter))); 2017 LoadSubCommand("return", 2018 CommandObjectSP(new CommandObjectThreadReturn(interpreter))); 2019 LoadSubCommand("jump", 2020 CommandObjectSP(new CommandObjectThreadJump(interpreter))); 2021 LoadSubCommand("select", 2022 CommandObjectSP(new CommandObjectThreadSelect(interpreter))); 2023 LoadSubCommand("until", 2024 CommandObjectSP(new CommandObjectThreadUntil(interpreter))); 2025 LoadSubCommand("info", 2026 CommandObjectSP(new CommandObjectThreadInfo(interpreter))); 2027 LoadSubCommand("exception", CommandObjectSP(new CommandObjectThreadException( 2028 interpreter))); 2029 LoadSubCommand("step-in", 2030 CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope( 2031 interpreter, "thread step-in", 2032 "Source level single step, stepping into calls. Defaults " 2033 "to current thread unless specified.", 2034 nullptr, eStepTypeInto, eStepScopeSource))); 2035 2036 LoadSubCommand("step-out", 2037 CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope( 2038 interpreter, "thread step-out", 2039 "Finish executing the current stack frame and stop after " 2040 "returning. Defaults to current thread unless specified.", 2041 nullptr, eStepTypeOut, eStepScopeSource))); 2042 2043 LoadSubCommand("step-over", 2044 CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope( 2045 interpreter, "thread step-over", 2046 "Source level single step, stepping over calls. Defaults " 2047 "to current thread unless specified.", 2048 nullptr, eStepTypeOver, eStepScopeSource))); 2049 2050 LoadSubCommand("step-inst", 2051 CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope( 2052 interpreter, "thread step-inst", 2053 "Instruction level single step, stepping into calls. " 2054 "Defaults to current thread unless specified.", 2055 nullptr, eStepTypeTrace, eStepScopeInstruction))); 2056 2057 LoadSubCommand("step-inst-over", 2058 CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope( 2059 interpreter, "thread step-inst-over", 2060 "Instruction level single step, stepping over calls. " 2061 "Defaults to current thread unless specified.", 2062 nullptr, eStepTypeTraceOver, eStepScopeInstruction))); 2063 2064 LoadSubCommand( 2065 "step-scripted", 2066 CommandObjectSP(new CommandObjectThreadStepWithTypeAndScope( 2067 interpreter, "thread step-scripted", 2068 "Step as instructed by the script class passed in the -C option. " 2069 "You can also specify a dictionary of key (-k) and value (-v) pairs " 2070 "that will be used to populate an SBStructuredData Dictionary, which " 2071 "will be passed to the constructor of the class implementing the " 2072 "scripted step. See the Python Reference for more details.", 2073 nullptr, eStepTypeScripted, eStepScopeSource))); 2074 2075 LoadSubCommand("plan", CommandObjectSP(new CommandObjectMultiwordThreadPlan( 2076 interpreter))); 2077 } 2078 2079 CommandObjectMultiwordThread::~CommandObjectMultiwordThread() = default; 2080