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