1 //===-- Thread.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 "lldb/lldb-private-log.h" 11 #include "lldb/Breakpoint/BreakpointLocation.h" 12 #include "lldb/Core/Debugger.h" 13 #include "lldb/Core/Log.h" 14 #include "lldb/Core/Stream.h" 15 #include "lldb/Core/StreamString.h" 16 #include "lldb/Core/RegularExpression.h" 17 #include "lldb/Target/DynamicLoader.h" 18 #include "lldb/Target/ExecutionContext.h" 19 #include "lldb/Target/ObjCLanguageRuntime.h" 20 #include "lldb/Target/Process.h" 21 #include "lldb/Target/RegisterContext.h" 22 #include "lldb/Target/StopInfo.h" 23 #include "lldb/Target/Target.h" 24 #include "lldb/Target/Thread.h" 25 #include "lldb/Target/ThreadPlan.h" 26 #include "lldb/Target/ThreadPlanCallFunction.h" 27 #include "lldb/Target/ThreadPlanBase.h" 28 #include "lldb/Target/ThreadPlanStepInstruction.h" 29 #include "lldb/Target/ThreadPlanStepOut.h" 30 #include "lldb/Target/ThreadPlanStepOverBreakpoint.h" 31 #include "lldb/Target/ThreadPlanStepThrough.h" 32 #include "lldb/Target/ThreadPlanStepInRange.h" 33 #include "lldb/Target/ThreadPlanStepOverRange.h" 34 #include "lldb/Target/ThreadPlanRunToAddress.h" 35 #include "lldb/Target/ThreadPlanStepUntil.h" 36 #include "lldb/Target/ThreadSpec.h" 37 #include "lldb/Target/Unwind.h" 38 39 using namespace lldb; 40 using namespace lldb_private; 41 42 Thread::Thread (Process &process, lldb::tid_t tid) : 43 UserID (tid), 44 ThreadInstanceSettings (*GetSettingsController()), 45 m_process (process), 46 m_actual_stop_info_sp (), 47 m_index_id (process.GetNextThreadIndexID ()), 48 m_reg_context_sp (), 49 m_state (eStateUnloaded), 50 m_state_mutex (Mutex::eMutexTypeRecursive), 51 m_plan_stack (), 52 m_completed_plan_stack(), 53 m_curr_frames_ap (), 54 m_resume_signal (LLDB_INVALID_SIGNAL_NUMBER), 55 m_resume_state (eStateRunning), 56 m_unwinder_ap (), 57 m_destroy_called (false), 58 m_thread_stop_reason_stop_id (0) 59 60 { 61 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); 62 if (log) 63 log->Printf ("%p Thread::Thread(tid = 0x%4.4x)", this, GetID()); 64 65 QueueFundamentalPlan(true); 66 UpdateInstanceName(); 67 } 68 69 70 Thread::~Thread() 71 { 72 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); 73 if (log) 74 log->Printf ("%p Thread::~Thread(tid = 0x%4.4x)", this, GetID()); 75 /// If you hit this assert, it means your derived class forgot to call DoDestroy in its destructor. 76 assert (m_destroy_called); 77 } 78 79 void 80 Thread::DestroyThread () 81 { 82 m_plan_stack.clear(); 83 m_discarded_plan_stack.clear(); 84 m_completed_plan_stack.clear(); 85 m_destroy_called = true; 86 } 87 88 lldb::StopInfoSP 89 Thread::GetStopInfo () 90 { 91 ThreadPlanSP plan_sp (GetCompletedPlan()); 92 if (plan_sp) 93 return StopInfo::CreateStopReasonWithPlan (plan_sp); 94 else 95 return GetPrivateStopReason (); 96 } 97 98 void 99 Thread::SetStopInfo (const lldb::StopInfoSP &stop_info_sp) 100 { 101 m_actual_stop_info_sp = stop_info_sp; 102 m_thread_stop_reason_stop_id = GetProcess().GetStopID(); 103 } 104 105 void 106 Thread::SetStopInfoToNothing() 107 { 108 // Note, we can't just NULL out the private reason, or the native thread implementation will try to 109 // go calculate it again. For now, just set it to a Unix Signal with an invalid signal number. 110 SetStopInfo (StopInfo::CreateStopReasonWithSignal (*this, LLDB_INVALID_SIGNAL_NUMBER)); 111 } 112 113 bool 114 Thread::ThreadStoppedForAReason (void) 115 { 116 return GetPrivateStopReason () != NULL; 117 } 118 119 bool 120 Thread::CheckpointThreadState (ThreadStateCheckpoint &saved_state) 121 { 122 if (!SaveFrameZeroState(saved_state.register_backup)) 123 return false; 124 125 saved_state.stop_info_sp = GetStopInfo(); 126 saved_state.orig_stop_id = GetProcess().GetStopID(); 127 128 return true; 129 } 130 131 bool 132 Thread::RestoreThreadStateFromCheckpoint (ThreadStateCheckpoint &saved_state) 133 { 134 RestoreSaveFrameZero(saved_state.register_backup); 135 if (saved_state.stop_info_sp) 136 saved_state.stop_info_sp->MakeStopInfoValid(); 137 SetStopInfo(saved_state.stop_info_sp); 138 return true; 139 } 140 141 StateType 142 Thread::GetState() const 143 { 144 // If any other threads access this we will need a mutex for it 145 Mutex::Locker locker(m_state_mutex); 146 return m_state; 147 } 148 149 void 150 Thread::SetState(StateType state) 151 { 152 Mutex::Locker locker(m_state_mutex); 153 m_state = state; 154 } 155 156 void 157 Thread::WillStop() 158 { 159 ThreadPlan *current_plan = GetCurrentPlan(); 160 161 // FIXME: I may decide to disallow threads with no plans. In which 162 // case this should go to an assert. 163 164 if (!current_plan) 165 return; 166 167 current_plan->WillStop(); 168 } 169 170 void 171 Thread::SetupForResume () 172 { 173 if (GetResumeState() != eStateSuspended) 174 { 175 176 // If we're at a breakpoint push the step-over breakpoint plan. Do this before 177 // telling the current plan it will resume, since we might change what the current 178 // plan is. 179 180 lldb::addr_t pc = GetRegisterContext()->GetPC(); 181 BreakpointSiteSP bp_site_sp = GetProcess().GetBreakpointSiteList().FindByAddress(pc); 182 if (bp_site_sp && bp_site_sp->IsEnabled()) 183 { 184 // Note, don't assume there's a ThreadPlanStepOverBreakpoint, the target may not require anything 185 // special to step over a breakpoint. 186 187 ThreadPlan *cur_plan = GetCurrentPlan(); 188 189 if (cur_plan->GetKind() != ThreadPlan::eKindStepOverBreakpoint) 190 { 191 ThreadPlanStepOverBreakpoint *step_bp_plan = new ThreadPlanStepOverBreakpoint (*this); 192 if (step_bp_plan) 193 { 194 ThreadPlanSP step_bp_plan_sp; 195 step_bp_plan->SetPrivate (true); 196 197 if (GetCurrentPlan()->RunState() != eStateStepping) 198 { 199 step_bp_plan->SetAutoContinue(true); 200 } 201 step_bp_plan_sp.reset (step_bp_plan); 202 QueueThreadPlan (step_bp_plan_sp, false); 203 } 204 } 205 } 206 } 207 } 208 209 bool 210 Thread::WillResume (StateType resume_state) 211 { 212 // At this point clear the completed plan stack. 213 m_completed_plan_stack.clear(); 214 m_discarded_plan_stack.clear(); 215 216 StopInfo *stop_info = GetPrivateStopReason().get(); 217 if (stop_info) 218 stop_info->WillResume (resume_state); 219 220 // Tell all the plans that we are about to resume in case they need to clear any state. 221 // We distinguish between the plan on the top of the stack and the lower 222 // plans in case a plan needs to do any special business before it runs. 223 224 ThreadPlan *plan_ptr = GetCurrentPlan(); 225 plan_ptr->WillResume(resume_state, true); 226 227 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL) 228 { 229 plan_ptr->WillResume (resume_state, false); 230 } 231 232 m_actual_stop_info_sp.reset(); 233 return true; 234 } 235 236 void 237 Thread::DidResume () 238 { 239 SetResumeSignal (LLDB_INVALID_SIGNAL_NUMBER); 240 } 241 242 bool 243 Thread::ShouldStop (Event* event_ptr) 244 { 245 ThreadPlan *current_plan = GetCurrentPlan(); 246 bool should_stop = true; 247 248 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); 249 if (log) 250 { 251 StreamString s; 252 DumpThreadPlans(&s); 253 log->PutCString (s.GetData()); 254 } 255 256 // The top most plan always gets to do the trace log... 257 current_plan->DoTraceLog (); 258 259 if (current_plan->PlanExplainsStop()) 260 { 261 bool over_ride_stop = current_plan->ShouldAutoContinue(event_ptr); 262 263 // We're starting from the base plan, so just let it decide; 264 if (PlanIsBasePlan(current_plan)) 265 { 266 should_stop = current_plan->ShouldStop (event_ptr); 267 if (log) 268 log->Printf("Base plan says should stop: %d.", current_plan->GetName(), should_stop); 269 } 270 else 271 { 272 // Otherwise, don't let the base plan override what the other plans say to do, since 273 // presumably if there were other plans they would know what to do... 274 while (1) 275 { 276 if (PlanIsBasePlan(current_plan)) 277 break; 278 279 should_stop = current_plan->ShouldStop(event_ptr); 280 if (log) 281 log->Printf("Plan %s should stop: %d.", current_plan->GetName(), should_stop); 282 if (current_plan->MischiefManaged()) 283 { 284 if (should_stop) 285 current_plan->WillStop(); 286 287 // If a Master Plan wants to stop, and wants to stick on the stack, we let it. 288 // Otherwise, see if the plan's parent wants to stop. 289 290 if (should_stop && current_plan->IsMasterPlan() && !current_plan->OkayToDiscard()) 291 { 292 PopPlan(); 293 break; 294 } 295 else 296 { 297 298 PopPlan(); 299 300 current_plan = GetCurrentPlan(); 301 if (current_plan == NULL) 302 { 303 break; 304 } 305 } 306 307 } 308 else 309 { 310 break; 311 } 312 } 313 } 314 if (over_ride_stop) 315 should_stop = false; 316 } 317 else if (current_plan->TracerExplainsStop()) 318 { 319 return false; 320 } 321 else 322 { 323 // If the current plan doesn't explain the stop, then, find one that 324 // does and let it handle the situation. 325 ThreadPlan *plan_ptr = current_plan; 326 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL) 327 { 328 if (plan_ptr->PlanExplainsStop()) 329 { 330 should_stop = plan_ptr->ShouldStop (event_ptr); 331 break; 332 } 333 334 } 335 } 336 337 return should_stop; 338 } 339 340 Vote 341 Thread::ShouldReportStop (Event* event_ptr) 342 { 343 StateType thread_state = GetResumeState (); 344 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); 345 346 if (thread_state == eStateSuspended || thread_state == eStateInvalid) 347 { 348 if (log) 349 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4x: returning vote %i (state was suspended or invalid)\n", GetID(), eVoteNoOpinion); 350 return eVoteNoOpinion; 351 } 352 353 if (m_completed_plan_stack.size() > 0) 354 { 355 // Don't use GetCompletedPlan here, since that suppresses private plans. 356 if (log) 357 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4x: returning vote for complete stack's back plan\n", GetID()); 358 return m_completed_plan_stack.back()->ShouldReportStop (event_ptr); 359 } 360 else 361 { 362 if (log) 363 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4x: returning vote for current plan\n", GetID()); 364 return GetCurrentPlan()->ShouldReportStop (event_ptr); 365 } 366 } 367 368 Vote 369 Thread::ShouldReportRun (Event* event_ptr) 370 { 371 StateType thread_state = GetResumeState (); 372 373 if (thread_state == eStateSuspended 374 || thread_state == eStateInvalid) 375 { 376 return eVoteNoOpinion; 377 } 378 379 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); 380 if (m_completed_plan_stack.size() > 0) 381 { 382 // Don't use GetCompletedPlan here, since that suppresses private plans. 383 if (log) 384 log->Printf ("Current Plan for thread %d (0x%4.4x): %s being asked whether we should report run.", 385 GetIndexID(), 386 GetID(), 387 m_completed_plan_stack.back()->GetName()); 388 389 return m_completed_plan_stack.back()->ShouldReportRun (event_ptr); 390 } 391 else 392 { 393 if (log) 394 log->Printf ("Current Plan for thread %d (0x%4.4x): %s being asked whether we should report run.", 395 GetIndexID(), 396 GetID(), 397 GetCurrentPlan()->GetName()); 398 399 return GetCurrentPlan()->ShouldReportRun (event_ptr); 400 } 401 } 402 403 bool 404 Thread::MatchesSpec (const ThreadSpec *spec) 405 { 406 if (spec == NULL) 407 return true; 408 409 return spec->ThreadPassesBasicTests(this); 410 } 411 412 void 413 Thread::PushPlan (ThreadPlanSP &thread_plan_sp) 414 { 415 if (thread_plan_sp) 416 { 417 // If the thread plan doesn't already have a tracer, give it its parent's tracer: 418 if (!thread_plan_sp->GetThreadPlanTracer()) 419 thread_plan_sp->SetThreadPlanTracer(m_plan_stack.back()->GetThreadPlanTracer()); 420 m_plan_stack.push_back (thread_plan_sp); 421 422 thread_plan_sp->DidPush(); 423 424 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); 425 if (log) 426 { 427 StreamString s; 428 thread_plan_sp->GetDescription (&s, lldb::eDescriptionLevelFull); 429 log->Printf("Pushing plan: \"%s\", tid = 0x%4.4x.", 430 s.GetData(), 431 thread_plan_sp->GetThread().GetID()); 432 } 433 } 434 } 435 436 void 437 Thread::PopPlan () 438 { 439 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); 440 441 if (m_plan_stack.empty()) 442 return; 443 else 444 { 445 ThreadPlanSP &plan = m_plan_stack.back(); 446 if (log) 447 { 448 log->Printf("Popping plan: \"%s\", tid = 0x%4.4x.", plan->GetName(), plan->GetThread().GetID()); 449 } 450 m_completed_plan_stack.push_back (plan); 451 plan->WillPop(); 452 m_plan_stack.pop_back(); 453 } 454 } 455 456 void 457 Thread::DiscardPlan () 458 { 459 if (m_plan_stack.size() > 1) 460 { 461 ThreadPlanSP &plan = m_plan_stack.back(); 462 m_discarded_plan_stack.push_back (plan); 463 plan->WillPop(); 464 m_plan_stack.pop_back(); 465 } 466 } 467 468 ThreadPlan * 469 Thread::GetCurrentPlan () 470 { 471 if (m_plan_stack.empty()) 472 return NULL; 473 else 474 return m_plan_stack.back().get(); 475 } 476 477 ThreadPlanSP 478 Thread::GetCompletedPlan () 479 { 480 ThreadPlanSP empty_plan_sp; 481 if (!m_completed_plan_stack.empty()) 482 { 483 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--) 484 { 485 ThreadPlanSP completed_plan_sp; 486 completed_plan_sp = m_completed_plan_stack[i]; 487 if (!completed_plan_sp->GetPrivate ()) 488 return completed_plan_sp; 489 } 490 } 491 return empty_plan_sp; 492 } 493 494 bool 495 Thread::IsThreadPlanDone (ThreadPlan *plan) 496 { 497 if (!m_completed_plan_stack.empty()) 498 { 499 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--) 500 { 501 if (m_completed_plan_stack[i].get() == plan) 502 return true; 503 } 504 } 505 return false; 506 } 507 508 bool 509 Thread::WasThreadPlanDiscarded (ThreadPlan *plan) 510 { 511 if (!m_discarded_plan_stack.empty()) 512 { 513 for (int i = m_discarded_plan_stack.size() - 1; i >= 0; i--) 514 { 515 if (m_discarded_plan_stack[i].get() == plan) 516 return true; 517 } 518 } 519 return false; 520 } 521 522 ThreadPlan * 523 Thread::GetPreviousPlan (ThreadPlan *current_plan) 524 { 525 if (current_plan == NULL) 526 return NULL; 527 528 int stack_size = m_completed_plan_stack.size(); 529 for (int i = stack_size - 1; i > 0; i--) 530 { 531 if (current_plan == m_completed_plan_stack[i].get()) 532 return m_completed_plan_stack[i-1].get(); 533 } 534 535 if (stack_size > 0 && m_completed_plan_stack[0].get() == current_plan) 536 { 537 if (m_plan_stack.size() > 0) 538 return m_plan_stack.back().get(); 539 else 540 return NULL; 541 } 542 543 stack_size = m_plan_stack.size(); 544 for (int i = stack_size - 1; i > 0; i--) 545 { 546 if (current_plan == m_plan_stack[i].get()) 547 return m_plan_stack[i-1].get(); 548 } 549 return NULL; 550 } 551 552 void 553 Thread::QueueThreadPlan (ThreadPlanSP &thread_plan_sp, bool abort_other_plans) 554 { 555 if (abort_other_plans) 556 DiscardThreadPlans(true); 557 558 PushPlan (thread_plan_sp); 559 } 560 561 562 void 563 Thread::EnableTracer (bool value, bool single_stepping) 564 { 565 int stack_size = m_plan_stack.size(); 566 for (int i = 0; i < stack_size; i++) 567 { 568 if (m_plan_stack[i]->GetThreadPlanTracer()) 569 { 570 m_plan_stack[i]->GetThreadPlanTracer()->EnableTracing(value); 571 m_plan_stack[i]->GetThreadPlanTracer()->EnableSingleStep(single_stepping); 572 } 573 } 574 } 575 576 void 577 Thread::SetTracer (lldb::ThreadPlanTracerSP &tracer_sp) 578 { 579 int stack_size = m_plan_stack.size(); 580 for (int i = 0; i < stack_size; i++) 581 m_plan_stack[i]->SetThreadPlanTracer(tracer_sp); 582 } 583 584 void 585 Thread::DiscardThreadPlansUpToPlan (lldb::ThreadPlanSP &up_to_plan_sp) 586 { 587 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); 588 if (log) 589 { 590 log->Printf("Discarding thread plans for thread tid = 0x%4.4x, up to %p", GetID(), up_to_plan_sp.get()); 591 } 592 593 int stack_size = m_plan_stack.size(); 594 595 // If the input plan is NULL, discard all plans. Otherwise make sure this plan is in the 596 // stack, and if so discard up to and including it. 597 598 if (up_to_plan_sp.get() == NULL) 599 { 600 for (int i = stack_size - 1; i > 0; i--) 601 DiscardPlan(); 602 } 603 else 604 { 605 bool found_it = false; 606 for (int i = stack_size - 1; i > 0; i--) 607 { 608 if (m_plan_stack[i] == up_to_plan_sp) 609 found_it = true; 610 } 611 if (found_it) 612 { 613 bool last_one = false; 614 for (int i = stack_size - 1; i > 0 && !last_one ; i--) 615 { 616 if (GetCurrentPlan() == up_to_plan_sp.get()) 617 last_one = true; 618 DiscardPlan(); 619 } 620 } 621 } 622 return; 623 } 624 625 void 626 Thread::DiscardThreadPlans(bool force) 627 { 628 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); 629 if (log) 630 { 631 log->Printf("Discarding thread plans for thread (tid = 0x%4.4x, force %d)", GetID(), force); 632 } 633 634 if (force) 635 { 636 int stack_size = m_plan_stack.size(); 637 for (int i = stack_size - 1; i > 0; i--) 638 { 639 DiscardPlan(); 640 } 641 return; 642 } 643 644 while (1) 645 { 646 647 int master_plan_idx; 648 bool discard; 649 650 // Find the first master plan, see if it wants discarding, and if yes discard up to it. 651 for (master_plan_idx = m_plan_stack.size() - 1; master_plan_idx >= 0; master_plan_idx--) 652 { 653 if (m_plan_stack[master_plan_idx]->IsMasterPlan()) 654 { 655 discard = m_plan_stack[master_plan_idx]->OkayToDiscard(); 656 break; 657 } 658 } 659 660 if (discard) 661 { 662 // First pop all the dependent plans: 663 for (int i = m_plan_stack.size() - 1; i > master_plan_idx; i--) 664 { 665 666 // FIXME: Do we need a finalize here, or is the rule that "PrepareForStop" 667 // for the plan leaves it in a state that it is safe to pop the plan 668 // with no more notice? 669 DiscardPlan(); 670 } 671 672 // Now discard the master plan itself. 673 // The bottom-most plan never gets discarded. "OkayToDiscard" for it means 674 // discard it's dependent plans, but not it... 675 if (master_plan_idx > 0) 676 { 677 DiscardPlan(); 678 } 679 } 680 else 681 { 682 // If the master plan doesn't want to get discarded, then we're done. 683 break; 684 } 685 686 } 687 } 688 689 ThreadPlan * 690 Thread::QueueFundamentalPlan (bool abort_other_plans) 691 { 692 ThreadPlanSP thread_plan_sp (new ThreadPlanBase(*this)); 693 QueueThreadPlan (thread_plan_sp, abort_other_plans); 694 return thread_plan_sp.get(); 695 } 696 697 ThreadPlan * 698 Thread::QueueThreadPlanForStepSingleInstruction 699 ( 700 bool step_over, 701 bool abort_other_plans, 702 bool stop_other_threads 703 ) 704 { 705 ThreadPlanSP thread_plan_sp (new ThreadPlanStepInstruction (*this, step_over, stop_other_threads, eVoteNoOpinion, eVoteNoOpinion)); 706 QueueThreadPlan (thread_plan_sp, abort_other_plans); 707 return thread_plan_sp.get(); 708 } 709 710 ThreadPlan * 711 Thread::QueueThreadPlanForStepRange 712 ( 713 bool abort_other_plans, 714 StepType type, 715 const AddressRange &range, 716 const SymbolContext &addr_context, 717 lldb::RunMode stop_other_threads, 718 bool avoid_code_without_debug_info 719 ) 720 { 721 ThreadPlanSP thread_plan_sp; 722 if (type == eStepTypeInto) 723 { 724 ThreadPlanStepInRange *plan = new ThreadPlanStepInRange (*this, range, addr_context, stop_other_threads); 725 if (avoid_code_without_debug_info) 726 plan->GetFlags().Set (ThreadPlanShouldStopHere::eAvoidNoDebug); 727 else 728 plan->GetFlags().Clear (ThreadPlanShouldStopHere::eAvoidNoDebug); 729 thread_plan_sp.reset (plan); 730 } 731 else 732 thread_plan_sp.reset (new ThreadPlanStepOverRange (*this, range, addr_context, stop_other_threads)); 733 734 QueueThreadPlan (thread_plan_sp, abort_other_plans); 735 return thread_plan_sp.get(); 736 } 737 738 739 ThreadPlan * 740 Thread::QueueThreadPlanForStepOverBreakpointPlan (bool abort_other_plans) 741 { 742 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOverBreakpoint (*this)); 743 QueueThreadPlan (thread_plan_sp, abort_other_plans); 744 return thread_plan_sp.get(); 745 } 746 747 ThreadPlan * 748 Thread::QueueThreadPlanForStepOut 749 ( 750 bool abort_other_plans, 751 SymbolContext *addr_context, 752 bool first_insn, 753 bool stop_other_threads, 754 Vote stop_vote, 755 Vote run_vote, 756 uint32_t frame_idx 757 ) 758 { 759 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOut (*this, 760 addr_context, 761 first_insn, 762 stop_other_threads, 763 stop_vote, 764 run_vote, 765 frame_idx)); 766 QueueThreadPlan (thread_plan_sp, abort_other_plans); 767 return thread_plan_sp.get(); 768 } 769 770 ThreadPlan * 771 Thread::QueueThreadPlanForStepThrough (bool abort_other_plans, bool stop_other_threads) 772 { 773 // Try the dynamic loader first: 774 ThreadPlanSP thread_plan_sp(GetProcess().GetDynamicLoader()->GetStepThroughTrampolinePlan (*this, stop_other_threads)); 775 // If that didn't come up with anything, try the ObjC runtime plugin: 776 if (thread_plan_sp.get() == NULL) 777 { 778 ObjCLanguageRuntime *objc_runtime = GetProcess().GetObjCLanguageRuntime(); 779 if (objc_runtime) 780 thread_plan_sp = objc_runtime->GetStepThroughTrampolinePlan (*this, stop_other_threads); 781 } 782 783 if (thread_plan_sp.get() == NULL) 784 { 785 thread_plan_sp.reset(new ThreadPlanStepThrough (*this, stop_other_threads)); 786 if (thread_plan_sp && !thread_plan_sp->ValidatePlan (NULL)) 787 return NULL; 788 } 789 QueueThreadPlan (thread_plan_sp, abort_other_plans); 790 return thread_plan_sp.get(); 791 } 792 793 ThreadPlan * 794 Thread::QueueThreadPlanForCallFunction (bool abort_other_plans, 795 Address& function, 796 lldb::addr_t arg, 797 bool stop_other_threads, 798 bool discard_on_error) 799 { 800 ThreadPlanSP thread_plan_sp (new ThreadPlanCallFunction (*this, function, arg, stop_other_threads, discard_on_error)); 801 QueueThreadPlan (thread_plan_sp, abort_other_plans); 802 return thread_plan_sp.get(); 803 } 804 805 ThreadPlan * 806 Thread::QueueThreadPlanForRunToAddress (bool abort_other_plans, 807 Address &target_addr, 808 bool stop_other_threads) 809 { 810 ThreadPlanSP thread_plan_sp (new ThreadPlanRunToAddress (*this, target_addr, stop_other_threads)); 811 QueueThreadPlan (thread_plan_sp, abort_other_plans); 812 return thread_plan_sp.get(); 813 } 814 815 ThreadPlan * 816 Thread::QueueThreadPlanForStepUntil (bool abort_other_plans, 817 lldb::addr_t *address_list, 818 size_t num_addresses, 819 bool stop_other_threads, 820 uint32_t frame_idx) 821 { 822 ThreadPlanSP thread_plan_sp (new ThreadPlanStepUntil (*this, address_list, num_addresses, stop_other_threads, frame_idx)); 823 QueueThreadPlan (thread_plan_sp, abort_other_plans); 824 return thread_plan_sp.get(); 825 826 } 827 828 uint32_t 829 Thread::GetIndexID () const 830 { 831 return m_index_id; 832 } 833 834 void 835 Thread::DumpThreadPlans (lldb_private::Stream *s) const 836 { 837 uint32_t stack_size = m_plan_stack.size(); 838 int i; 839 s->Printf ("Plan Stack for thread #%u: tid = 0x%4.4x, stack_size = %d\n", GetIndexID(), GetID(), stack_size); 840 for (i = stack_size - 1; i >= 0; i--) 841 { 842 s->Printf ("Element %d: ", i); 843 s->IndentMore(); 844 m_plan_stack[i]->GetDescription (s, eDescriptionLevelFull); 845 s->IndentLess(); 846 s->EOL(); 847 } 848 849 stack_size = m_completed_plan_stack.size(); 850 s->Printf ("Completed Plan Stack: %d elements.\n", stack_size); 851 for (i = stack_size - 1; i >= 0; i--) 852 { 853 s->Printf ("Element %d: ", i); 854 s->IndentMore(); 855 m_completed_plan_stack[i]->GetDescription (s, eDescriptionLevelFull); 856 s->IndentLess(); 857 s->EOL(); 858 } 859 860 stack_size = m_discarded_plan_stack.size(); 861 s->Printf ("Discarded Plan Stack: %d elements.\n", stack_size); 862 for (i = stack_size - 1; i >= 0; i--) 863 { 864 s->Printf ("Element %d: ", i); 865 s->IndentMore(); 866 m_discarded_plan_stack[i]->GetDescription (s, eDescriptionLevelFull); 867 s->IndentLess(); 868 s->EOL(); 869 } 870 871 } 872 873 Target * 874 Thread::CalculateTarget () 875 { 876 return m_process.CalculateTarget(); 877 } 878 879 Process * 880 Thread::CalculateProcess () 881 { 882 return &m_process; 883 } 884 885 Thread * 886 Thread::CalculateThread () 887 { 888 return this; 889 } 890 891 StackFrame * 892 Thread::CalculateStackFrame () 893 { 894 return NULL; 895 } 896 897 void 898 Thread::CalculateExecutionContext (ExecutionContext &exe_ctx) 899 { 900 m_process.CalculateExecutionContext (exe_ctx); 901 exe_ctx.thread = this; 902 exe_ctx.frame = NULL; 903 } 904 905 906 StackFrameList & 907 Thread::GetStackFrameList () 908 { 909 if (m_curr_frames_ap.get() == NULL) 910 m_curr_frames_ap.reset (new StackFrameList (*this, m_prev_frames_sp, true)); 911 return *m_curr_frames_ap; 912 } 913 914 915 916 uint32_t 917 Thread::GetStackFrameCount() 918 { 919 return GetStackFrameList().GetNumFrames(); 920 } 921 922 923 void 924 Thread::ClearStackFrames () 925 { 926 if (m_curr_frames_ap.get() && m_curr_frames_ap->GetNumFrames (false) > 1) 927 m_prev_frames_sp.reset (m_curr_frames_ap.release()); 928 else 929 m_curr_frames_ap.release(); 930 931 // StackFrameList::Merge (m_curr_frames_ap, m_prev_frames_sp); 932 // assert (m_curr_frames_ap.get() == NULL); 933 } 934 935 lldb::StackFrameSP 936 Thread::GetStackFrameAtIndex (uint32_t idx) 937 { 938 return GetStackFrameList().GetFrameAtIndex(idx); 939 } 940 941 uint32_t 942 Thread::GetSelectedFrameIndex () 943 { 944 return GetStackFrameList().GetSelectedFrameIndex(); 945 } 946 947 lldb::StackFrameSP 948 Thread::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx) 949 { 950 return GetStackFrameList().GetFrameWithConcreteFrameIndex (unwind_idx); 951 } 952 953 lldb::StackFrameSP 954 Thread::GetFrameWithStackID(StackID &stack_id) 955 { 956 return GetStackFrameList().GetFrameWithStackID (stack_id); 957 } 958 959 960 lldb::StackFrameSP 961 Thread::GetSelectedFrame () 962 { 963 return GetStackFrameAtIndex (GetStackFrameList().GetSelectedFrameIndex()); 964 } 965 966 uint32_t 967 Thread::SetSelectedFrame (lldb_private::StackFrame *frame) 968 { 969 return GetStackFrameList().SetSelectedFrame(frame); 970 } 971 972 void 973 Thread::SetSelectedFrameByIndex (uint32_t idx) 974 { 975 GetStackFrameList().SetSelectedFrameByIndex(idx); 976 } 977 978 void 979 Thread::DumpUsingSettingsFormat (Stream &strm, uint32_t frame_idx) 980 { 981 ExecutionContext exe_ctx; 982 SymbolContext frame_sc; 983 CalculateExecutionContext (exe_ctx); 984 985 if (frame_idx != LLDB_INVALID_INDEX32) 986 { 987 StackFrameSP frame_sp(GetStackFrameAtIndex (frame_idx)); 988 if (frame_sp) 989 { 990 exe_ctx.frame = frame_sp.get(); 991 frame_sc = exe_ctx.frame->GetSymbolContext(eSymbolContextEverything); 992 } 993 } 994 995 const char *thread_format = GetProcess().GetTarget().GetDebugger().GetThreadFormat(); 996 assert (thread_format); 997 const char *end = NULL; 998 Debugger::FormatPrompt (thread_format, 999 exe_ctx.frame ? &frame_sc : NULL, 1000 &exe_ctx, 1001 NULL, 1002 strm, 1003 &end); 1004 } 1005 1006 lldb::ThreadSP 1007 Thread::GetSP () 1008 { 1009 return m_process.GetThreadList().GetThreadSPForThreadPtr(this); 1010 } 1011 1012 1013 void 1014 Thread::SettingsInitialize () 1015 { 1016 UserSettingsControllerSP &usc = GetSettingsController(); 1017 usc.reset (new SettingsController); 1018 UserSettingsController::InitializeSettingsController (usc, 1019 SettingsController::global_settings_table, 1020 SettingsController::instance_settings_table); 1021 1022 // Now call SettingsInitialize() on each 'child' setting of Thread. 1023 // Currently there are none. 1024 } 1025 1026 void 1027 Thread::SettingsTerminate () 1028 { 1029 // Must call SettingsTerminate() on each 'child' setting of Thread before terminating Thread settings. 1030 // Currently there are none. 1031 1032 // Now terminate Thread Settings. 1033 1034 UserSettingsControllerSP &usc = GetSettingsController(); 1035 UserSettingsController::FinalizeSettingsController (usc); 1036 usc.reset(); 1037 } 1038 1039 UserSettingsControllerSP & 1040 Thread::GetSettingsController () 1041 { 1042 static UserSettingsControllerSP g_settings_controller; 1043 return g_settings_controller; 1044 } 1045 1046 void 1047 Thread::UpdateInstanceName () 1048 { 1049 StreamString sstr; 1050 const char *name = GetName(); 1051 1052 if (name && name[0] != '\0') 1053 sstr.Printf ("%s", name); 1054 else if ((GetIndexID() != 0) || (GetID() != 0)) 1055 sstr.Printf ("0x%4.4x", GetIndexID(), GetID()); 1056 1057 if (sstr.GetSize() > 0) 1058 Thread::GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(), sstr.GetData()); 1059 } 1060 1061 lldb::StackFrameSP 1062 Thread::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr) 1063 { 1064 return GetStackFrameList().GetStackFrameSPForStackFramePtr (stack_frame_ptr); 1065 } 1066 1067 const char * 1068 Thread::StopReasonAsCString (lldb::StopReason reason) 1069 { 1070 switch (reason) 1071 { 1072 case eStopReasonInvalid: return "invalid"; 1073 case eStopReasonNone: return "none"; 1074 case eStopReasonTrace: return "trace"; 1075 case eStopReasonBreakpoint: return "breakpoint"; 1076 case eStopReasonWatchpoint: return "watchpoint"; 1077 case eStopReasonSignal: return "signal"; 1078 case eStopReasonException: return "exception"; 1079 case eStopReasonPlanComplete: return "plan complete"; 1080 } 1081 1082 1083 static char unknown_state_string[64]; 1084 snprintf(unknown_state_string, sizeof (unknown_state_string), "StopReason = %i", reason); 1085 return unknown_state_string; 1086 } 1087 1088 const char * 1089 Thread::RunModeAsCString (lldb::RunMode mode) 1090 { 1091 switch (mode) 1092 { 1093 case eOnlyThisThread: return "only this thread"; 1094 case eAllThreads: return "all threads"; 1095 case eOnlyDuringStepping: return "only during stepping"; 1096 } 1097 1098 static char unknown_state_string[64]; 1099 snprintf(unknown_state_string, sizeof (unknown_state_string), "RunMode = %i", mode); 1100 return unknown_state_string; 1101 } 1102 1103 #pragma mark "Thread::SettingsController" 1104 //-------------------------------------------------------------- 1105 // class Thread::SettingsController 1106 //-------------------------------------------------------------- 1107 1108 Thread::SettingsController::SettingsController () : 1109 UserSettingsController ("thread", Process::GetSettingsController()) 1110 { 1111 m_default_settings.reset (new ThreadInstanceSettings (*this, false, 1112 InstanceSettings::GetDefaultName().AsCString())); 1113 } 1114 1115 Thread::SettingsController::~SettingsController () 1116 { 1117 } 1118 1119 lldb::InstanceSettingsSP 1120 Thread::SettingsController::CreateInstanceSettings (const char *instance_name) 1121 { 1122 ThreadInstanceSettings *new_settings = new ThreadInstanceSettings (*GetSettingsController(), 1123 false, 1124 instance_name); 1125 lldb::InstanceSettingsSP new_settings_sp (new_settings); 1126 return new_settings_sp; 1127 } 1128 1129 #pragma mark "ThreadInstanceSettings" 1130 //-------------------------------------------------------------- 1131 // class ThreadInstanceSettings 1132 //-------------------------------------------------------------- 1133 1134 ThreadInstanceSettings::ThreadInstanceSettings (UserSettingsController &owner, bool live_instance, const char *name) : 1135 InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance), 1136 m_avoid_regexp_ap (), 1137 m_trace_enabled (false) 1138 { 1139 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called 1140 // until the vtables for ThreadInstanceSettings are properly set up, i.e. AFTER all the initializers. 1141 // For this reason it has to be called here, rather than in the initializer or in the parent constructor. 1142 // This is true for CreateInstanceName() too. 1143 1144 if (GetInstanceName() == InstanceSettings::InvalidName()) 1145 { 1146 ChangeInstanceName (std::string (CreateInstanceName().AsCString())); 1147 m_owner.RegisterInstanceSettings (this); 1148 } 1149 1150 if (live_instance) 1151 { 1152 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name); 1153 CopyInstanceSettings (pending_settings,false); 1154 //m_owner.RemovePendingSettings (m_instance_name); 1155 } 1156 } 1157 1158 ThreadInstanceSettings::ThreadInstanceSettings (const ThreadInstanceSettings &rhs) : 1159 InstanceSettings (*Thread::GetSettingsController(), CreateInstanceName().AsCString()), 1160 m_avoid_regexp_ap (), 1161 m_trace_enabled (rhs.m_trace_enabled) 1162 { 1163 if (m_instance_name != InstanceSettings::GetDefaultName()) 1164 { 1165 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name); 1166 CopyInstanceSettings (pending_settings,false); 1167 m_owner.RemovePendingSettings (m_instance_name); 1168 } 1169 if (rhs.m_avoid_regexp_ap.get() != NULL) 1170 m_avoid_regexp_ap.reset(new RegularExpression(rhs.m_avoid_regexp_ap->GetText())); 1171 } 1172 1173 ThreadInstanceSettings::~ThreadInstanceSettings () 1174 { 1175 } 1176 1177 ThreadInstanceSettings& 1178 ThreadInstanceSettings::operator= (const ThreadInstanceSettings &rhs) 1179 { 1180 if (this != &rhs) 1181 { 1182 if (rhs.m_avoid_regexp_ap.get() != NULL) 1183 m_avoid_regexp_ap.reset(new RegularExpression(rhs.m_avoid_regexp_ap->GetText())); 1184 else 1185 m_avoid_regexp_ap.reset(NULL); 1186 } 1187 m_trace_enabled = rhs.m_trace_enabled; 1188 return *this; 1189 } 1190 1191 1192 void 1193 ThreadInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name, 1194 const char *index_value, 1195 const char *value, 1196 const ConstString &instance_name, 1197 const SettingEntry &entry, 1198 VarSetOperationType op, 1199 Error &err, 1200 bool pending) 1201 { 1202 if (var_name == StepAvoidRegexpVarName()) 1203 { 1204 std::string regexp_text; 1205 if (m_avoid_regexp_ap.get() != NULL) 1206 regexp_text.append (m_avoid_regexp_ap->GetText()); 1207 UserSettingsController::UpdateStringVariable (op, regexp_text, value, err); 1208 if (regexp_text.empty()) 1209 m_avoid_regexp_ap.reset(); 1210 else 1211 { 1212 m_avoid_regexp_ap.reset(new RegularExpression(regexp_text.c_str())); 1213 1214 } 1215 } 1216 else if (var_name == GetTraceThreadVarName()) 1217 { 1218 bool success; 1219 bool result = Args::StringToBoolean(value, false, &success); 1220 1221 if (success) 1222 { 1223 m_trace_enabled = result; 1224 if (!pending) 1225 { 1226 Thread *myself = static_cast<Thread *> (this); 1227 myself->EnableTracer(m_trace_enabled, true); 1228 } 1229 } 1230 else 1231 { 1232 err.SetErrorStringWithFormat ("Bad value \"%s\" for trace-thread, should be Boolean.", value); 1233 } 1234 1235 } 1236 } 1237 1238 void 1239 ThreadInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings, 1240 bool pending) 1241 { 1242 if (new_settings.get() == NULL) 1243 return; 1244 1245 ThreadInstanceSettings *new_process_settings = (ThreadInstanceSettings *) new_settings.get(); 1246 if (new_process_settings->GetSymbolsToAvoidRegexp() != NULL) 1247 m_avoid_regexp_ap.reset (new RegularExpression (new_process_settings->GetSymbolsToAvoidRegexp()->GetText())); 1248 else 1249 m_avoid_regexp_ap.reset (); 1250 } 1251 1252 bool 1253 ThreadInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry, 1254 const ConstString &var_name, 1255 StringList &value, 1256 Error *err) 1257 { 1258 if (var_name == StepAvoidRegexpVarName()) 1259 { 1260 if (m_avoid_regexp_ap.get() != NULL) 1261 { 1262 std::string regexp_text("\""); 1263 regexp_text.append(m_avoid_regexp_ap->GetText()); 1264 regexp_text.append ("\""); 1265 value.AppendString (regexp_text.c_str()); 1266 } 1267 1268 } 1269 else if (var_name == GetTraceThreadVarName()) 1270 { 1271 value.AppendString(m_trace_enabled ? "true" : "false"); 1272 } 1273 else 1274 { 1275 if (err) 1276 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString()); 1277 return false; 1278 } 1279 return true; 1280 } 1281 1282 const ConstString 1283 ThreadInstanceSettings::CreateInstanceName () 1284 { 1285 static int instance_count = 1; 1286 StreamString sstr; 1287 1288 sstr.Printf ("thread_%d", instance_count); 1289 ++instance_count; 1290 1291 const ConstString ret_val (sstr.GetData()); 1292 return ret_val; 1293 } 1294 1295 const ConstString & 1296 ThreadInstanceSettings::StepAvoidRegexpVarName () 1297 { 1298 static ConstString step_avoid_var_name ("step-avoid-regexp"); 1299 1300 return step_avoid_var_name; 1301 } 1302 1303 const ConstString & 1304 ThreadInstanceSettings::GetTraceThreadVarName () 1305 { 1306 static ConstString trace_thread_var_name ("trace-thread"); 1307 1308 return trace_thread_var_name; 1309 } 1310 1311 //-------------------------------------------------- 1312 // SettingsController Variable Tables 1313 //-------------------------------------------------- 1314 1315 SettingEntry 1316 Thread::SettingsController::global_settings_table[] = 1317 { 1318 //{ "var-name", var-type , "default", enum-table, init'd, hidden, "help-text"}, 1319 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL } 1320 }; 1321 1322 1323 SettingEntry 1324 Thread::SettingsController::instance_settings_table[] = 1325 { 1326 //{ "var-name", var-type, "default", enum-table, init'd, hidden, "help-text"}, 1327 { "step-avoid-regexp", eSetVarTypeString, "", NULL, false, false, "A regular expression defining functions step-in won't stop in." }, 1328 { "trace-thread", eSetVarTypeBoolean, "false", NULL, false, false, "If true, this thread will single-step and log execution." }, 1329 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL } 1330 }; 1331