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