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