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 264 // We're starting from the base plan, so just let it decide; 265 if (PlanIsBasePlan(current_plan)) 266 { 267 should_stop = current_plan->ShouldStop (event_ptr); 268 if (log) 269 log->Printf("Base plan says should stop: %i.", should_stop); 270 } 271 else 272 { 273 // Otherwise, don't let the base plan override what the other plans say to do, since 274 // presumably if there were other plans they would know what to do... 275 while (1) 276 { 277 if (PlanIsBasePlan(current_plan)) 278 break; 279 280 should_stop = current_plan->ShouldStop(event_ptr); 281 if (log) 282 log->Printf("Plan %s should stop: %d.", current_plan->GetName(), should_stop); 283 if (current_plan->MischiefManaged()) 284 { 285 if (should_stop) 286 current_plan->WillStop(); 287 288 // If a Master Plan wants to stop, and wants to stick on the stack, we let it. 289 // Otherwise, see if the plan's parent wants to stop. 290 291 if (should_stop && current_plan->IsMasterPlan() && !current_plan->OkayToDiscard()) 292 { 293 PopPlan(); 294 break; 295 } 296 else 297 { 298 299 PopPlan(); 300 301 current_plan = GetCurrentPlan(); 302 if (current_plan == NULL) 303 { 304 break; 305 } 306 } 307 308 } 309 else 310 { 311 break; 312 } 313 } 314 } 315 if (over_ride_stop) 316 should_stop = false; 317 } 318 else if (current_plan->TracerExplainsStop()) 319 { 320 return false; 321 } 322 else 323 { 324 // If the current plan doesn't explain the stop, then, find one that 325 // does and let it handle the situation. 326 ThreadPlan *plan_ptr = current_plan; 327 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL) 328 { 329 if (plan_ptr->PlanExplainsStop()) 330 { 331 should_stop = plan_ptr->ShouldStop (event_ptr); 332 break; 333 } 334 335 } 336 } 337 338 return should_stop; 339 } 340 341 Vote 342 Thread::ShouldReportStop (Event* event_ptr) 343 { 344 StateType thread_state = GetResumeState (); 345 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); 346 347 if (thread_state == eStateSuspended || thread_state == eStateInvalid) 348 { 349 if (log) 350 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4x: returning vote %i (state was suspended or invalid)\n", GetID(), eVoteNoOpinion); 351 return eVoteNoOpinion; 352 } 353 354 if (m_completed_plan_stack.size() > 0) 355 { 356 // Don't use GetCompletedPlan here, since that suppresses private plans. 357 if (log) 358 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4x: returning vote for complete stack's back plan\n", GetID()); 359 return m_completed_plan_stack.back()->ShouldReportStop (event_ptr); 360 } 361 else 362 { 363 if (log) 364 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4x: returning vote for current plan\n", GetID()); 365 return GetCurrentPlan()->ShouldReportStop (event_ptr); 366 } 367 } 368 369 Vote 370 Thread::ShouldReportRun (Event* event_ptr) 371 { 372 StateType thread_state = GetResumeState (); 373 374 if (thread_state == eStateSuspended 375 || thread_state == eStateInvalid) 376 { 377 return eVoteNoOpinion; 378 } 379 380 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); 381 if (m_completed_plan_stack.size() > 0) 382 { 383 // Don't use GetCompletedPlan here, since that suppresses private plans. 384 if (log) 385 log->Printf ("Current Plan for thread %d (0x%4.4x): %s being asked whether we should report run.", 386 GetIndexID(), 387 GetID(), 388 m_completed_plan_stack.back()->GetName()); 389 390 return m_completed_plan_stack.back()->ShouldReportRun (event_ptr); 391 } 392 else 393 { 394 if (log) 395 log->Printf ("Current Plan for thread %d (0x%4.4x): %s being asked whether we should report run.", 396 GetIndexID(), 397 GetID(), 398 GetCurrentPlan()->GetName()); 399 400 return GetCurrentPlan()->ShouldReportRun (event_ptr); 401 } 402 } 403 404 bool 405 Thread::MatchesSpec (const ThreadSpec *spec) 406 { 407 if (spec == NULL) 408 return true; 409 410 return spec->ThreadPassesBasicTests(this); 411 } 412 413 void 414 Thread::PushPlan (ThreadPlanSP &thread_plan_sp) 415 { 416 if (thread_plan_sp) 417 { 418 // If the thread plan doesn't already have a tracer, give it its parent's tracer: 419 if (!thread_plan_sp->GetThreadPlanTracer()) 420 thread_plan_sp->SetThreadPlanTracer(m_plan_stack.back()->GetThreadPlanTracer()); 421 m_plan_stack.push_back (thread_plan_sp); 422 423 thread_plan_sp->DidPush(); 424 425 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); 426 if (log) 427 { 428 StreamString s; 429 thread_plan_sp->GetDescription (&s, lldb::eDescriptionLevelFull); 430 log->Printf("Pushing plan: \"%s\", tid = 0x%4.4x.", 431 s.GetData(), 432 thread_plan_sp->GetThread().GetID()); 433 } 434 } 435 } 436 437 void 438 Thread::PopPlan () 439 { 440 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); 441 442 if (m_plan_stack.empty()) 443 return; 444 else 445 { 446 ThreadPlanSP &plan = m_plan_stack.back(); 447 if (log) 448 { 449 log->Printf("Popping plan: \"%s\", tid = 0x%4.4x.", plan->GetName(), plan->GetThread().GetID()); 450 } 451 m_completed_plan_stack.push_back (plan); 452 plan->WillPop(); 453 m_plan_stack.pop_back(); 454 } 455 } 456 457 void 458 Thread::DiscardPlan () 459 { 460 if (m_plan_stack.size() > 1) 461 { 462 ThreadPlanSP &plan = m_plan_stack.back(); 463 m_discarded_plan_stack.push_back (plan); 464 plan->WillPop(); 465 m_plan_stack.pop_back(); 466 } 467 } 468 469 ThreadPlan * 470 Thread::GetCurrentPlan () 471 { 472 if (m_plan_stack.empty()) 473 return NULL; 474 else 475 return m_plan_stack.back().get(); 476 } 477 478 ThreadPlanSP 479 Thread::GetCompletedPlan () 480 { 481 ThreadPlanSP empty_plan_sp; 482 if (!m_completed_plan_stack.empty()) 483 { 484 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--) 485 { 486 ThreadPlanSP completed_plan_sp; 487 completed_plan_sp = m_completed_plan_stack[i]; 488 if (!completed_plan_sp->GetPrivate ()) 489 return completed_plan_sp; 490 } 491 } 492 return empty_plan_sp; 493 } 494 495 bool 496 Thread::IsThreadPlanDone (ThreadPlan *plan) 497 { 498 if (!m_completed_plan_stack.empty()) 499 { 500 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--) 501 { 502 if (m_completed_plan_stack[i].get() == plan) 503 return true; 504 } 505 } 506 return false; 507 } 508 509 bool 510 Thread::WasThreadPlanDiscarded (ThreadPlan *plan) 511 { 512 if (!m_discarded_plan_stack.empty()) 513 { 514 for (int i = m_discarded_plan_stack.size() - 1; i >= 0; i--) 515 { 516 if (m_discarded_plan_stack[i].get() == plan) 517 return true; 518 } 519 } 520 return false; 521 } 522 523 ThreadPlan * 524 Thread::GetPreviousPlan (ThreadPlan *current_plan) 525 { 526 if (current_plan == NULL) 527 return NULL; 528 529 int stack_size = m_completed_plan_stack.size(); 530 for (int i = stack_size - 1; i > 0; i--) 531 { 532 if (current_plan == m_completed_plan_stack[i].get()) 533 return m_completed_plan_stack[i-1].get(); 534 } 535 536 if (stack_size > 0 && m_completed_plan_stack[0].get() == current_plan) 537 { 538 if (m_plan_stack.size() > 0) 539 return m_plan_stack.back().get(); 540 else 541 return NULL; 542 } 543 544 stack_size = m_plan_stack.size(); 545 for (int i = stack_size - 1; i > 0; i--) 546 { 547 if (current_plan == m_plan_stack[i].get()) 548 return m_plan_stack[i-1].get(); 549 } 550 return NULL; 551 } 552 553 void 554 Thread::QueueThreadPlan (ThreadPlanSP &thread_plan_sp, bool abort_other_plans) 555 { 556 if (abort_other_plans) 557 DiscardThreadPlans(true); 558 559 PushPlan (thread_plan_sp); 560 } 561 562 563 void 564 Thread::EnableTracer (bool value, bool single_stepping) 565 { 566 int stack_size = m_plan_stack.size(); 567 for (int i = 0; i < stack_size; i++) 568 { 569 if (m_plan_stack[i]->GetThreadPlanTracer()) 570 { 571 m_plan_stack[i]->GetThreadPlanTracer()->EnableTracing(value); 572 m_plan_stack[i]->GetThreadPlanTracer()->EnableSingleStep(single_stepping); 573 } 574 } 575 } 576 577 void 578 Thread::SetTracer (lldb::ThreadPlanTracerSP &tracer_sp) 579 { 580 int stack_size = m_plan_stack.size(); 581 for (int i = 0; i < stack_size; i++) 582 m_plan_stack[i]->SetThreadPlanTracer(tracer_sp); 583 } 584 585 void 586 Thread::DiscardThreadPlansUpToPlan (lldb::ThreadPlanSP &up_to_plan_sp) 587 { 588 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); 589 if (log) 590 { 591 log->Printf("Discarding thread plans for thread tid = 0x%4.4x, up to %p", GetID(), up_to_plan_sp.get()); 592 } 593 594 int stack_size = m_plan_stack.size(); 595 596 // If the input plan is NULL, discard all plans. Otherwise make sure this plan is in the 597 // stack, and if so discard up to and including it. 598 599 if (up_to_plan_sp.get() == NULL) 600 { 601 for (int i = stack_size - 1; i > 0; i--) 602 DiscardPlan(); 603 } 604 else 605 { 606 bool found_it = false; 607 for (int i = stack_size - 1; i > 0; i--) 608 { 609 if (m_plan_stack[i] == up_to_plan_sp) 610 found_it = true; 611 } 612 if (found_it) 613 { 614 bool last_one = false; 615 for (int i = stack_size - 1; i > 0 && !last_one ; i--) 616 { 617 if (GetCurrentPlan() == up_to_plan_sp.get()) 618 last_one = true; 619 DiscardPlan(); 620 } 621 } 622 } 623 return; 624 } 625 626 void 627 Thread::DiscardThreadPlans(bool force) 628 { 629 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); 630 if (log) 631 { 632 log->Printf("Discarding thread plans for thread (tid = 0x%4.4x, force %d)", GetID(), force); 633 } 634 635 if (force) 636 { 637 int stack_size = m_plan_stack.size(); 638 for (int i = stack_size - 1; i > 0; i--) 639 { 640 DiscardPlan(); 641 } 642 return; 643 } 644 645 while (1) 646 { 647 648 int master_plan_idx; 649 bool discard; 650 651 // Find the first master plan, see if it wants discarding, and if yes discard up to it. 652 for (master_plan_idx = m_plan_stack.size() - 1; master_plan_idx >= 0; master_plan_idx--) 653 { 654 if (m_plan_stack[master_plan_idx]->IsMasterPlan()) 655 { 656 discard = m_plan_stack[master_plan_idx]->OkayToDiscard(); 657 break; 658 } 659 } 660 661 if (discard) 662 { 663 // First pop all the dependent plans: 664 for (int i = m_plan_stack.size() - 1; i > master_plan_idx; i--) 665 { 666 667 // FIXME: Do we need a finalize here, or is the rule that "PrepareForStop" 668 // for the plan leaves it in a state that it is safe to pop the plan 669 // with no more notice? 670 DiscardPlan(); 671 } 672 673 // Now discard the master plan itself. 674 // The bottom-most plan never gets discarded. "OkayToDiscard" for it means 675 // discard it's dependent plans, but not it... 676 if (master_plan_idx > 0) 677 { 678 DiscardPlan(); 679 } 680 } 681 else 682 { 683 // If the master plan doesn't want to get discarded, then we're done. 684 break; 685 } 686 687 } 688 } 689 690 ThreadPlan * 691 Thread::QueueFundamentalPlan (bool abort_other_plans) 692 { 693 ThreadPlanSP thread_plan_sp (new ThreadPlanBase(*this)); 694 QueueThreadPlan (thread_plan_sp, abort_other_plans); 695 return thread_plan_sp.get(); 696 } 697 698 ThreadPlan * 699 Thread::QueueThreadPlanForStepSingleInstruction 700 ( 701 bool step_over, 702 bool abort_other_plans, 703 bool stop_other_threads 704 ) 705 { 706 ThreadPlanSP thread_plan_sp (new ThreadPlanStepInstruction (*this, step_over, stop_other_threads, eVoteNoOpinion, eVoteNoOpinion)); 707 QueueThreadPlan (thread_plan_sp, abort_other_plans); 708 return thread_plan_sp.get(); 709 } 710 711 ThreadPlan * 712 Thread::QueueThreadPlanForStepRange 713 ( 714 bool abort_other_plans, 715 StepType type, 716 const AddressRange &range, 717 const SymbolContext &addr_context, 718 lldb::RunMode stop_other_threads, 719 bool avoid_code_without_debug_info 720 ) 721 { 722 ThreadPlanSP thread_plan_sp; 723 if (type == eStepTypeInto) 724 { 725 ThreadPlanStepInRange *plan = new ThreadPlanStepInRange (*this, range, addr_context, stop_other_threads); 726 if (avoid_code_without_debug_info) 727 plan->GetFlags().Set (ThreadPlanShouldStopHere::eAvoidNoDebug); 728 else 729 plan->GetFlags().Clear (ThreadPlanShouldStopHere::eAvoidNoDebug); 730 thread_plan_sp.reset (plan); 731 } 732 else 733 thread_plan_sp.reset (new ThreadPlanStepOverRange (*this, range, addr_context, stop_other_threads)); 734 735 QueueThreadPlan (thread_plan_sp, abort_other_plans); 736 return thread_plan_sp.get(); 737 } 738 739 740 ThreadPlan * 741 Thread::QueueThreadPlanForStepOverBreakpointPlan (bool abort_other_plans) 742 { 743 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOverBreakpoint (*this)); 744 QueueThreadPlan (thread_plan_sp, abort_other_plans); 745 return thread_plan_sp.get(); 746 } 747 748 ThreadPlan * 749 Thread::QueueThreadPlanForStepOut 750 ( 751 bool abort_other_plans, 752 SymbolContext *addr_context, 753 bool first_insn, 754 bool stop_other_threads, 755 Vote stop_vote, 756 Vote run_vote, 757 uint32_t frame_idx 758 ) 759 { 760 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOut (*this, 761 addr_context, 762 first_insn, 763 stop_other_threads, 764 stop_vote, 765 run_vote, 766 frame_idx)); 767 QueueThreadPlan (thread_plan_sp, abort_other_plans); 768 return thread_plan_sp.get(); 769 } 770 771 ThreadPlan * 772 Thread::QueueThreadPlanForStepThrough (bool abort_other_plans, bool stop_other_threads) 773 { 774 // Try the dynamic loader first: 775 ThreadPlanSP thread_plan_sp(GetProcess().GetDynamicLoader()->GetStepThroughTrampolinePlan (*this, stop_other_threads)); 776 // If that didn't come up with anything, try the ObjC runtime plugin: 777 if (thread_plan_sp.get() == NULL) 778 { 779 ObjCLanguageRuntime *objc_runtime = GetProcess().GetObjCLanguageRuntime(); 780 if (objc_runtime) 781 thread_plan_sp = objc_runtime->GetStepThroughTrampolinePlan (*this, stop_other_threads); 782 } 783 784 if (thread_plan_sp.get() == NULL) 785 { 786 thread_plan_sp.reset(new ThreadPlanStepThrough (*this, stop_other_threads)); 787 if (thread_plan_sp && !thread_plan_sp->ValidatePlan (NULL)) 788 return NULL; 789 } 790 QueueThreadPlan (thread_plan_sp, abort_other_plans); 791 return thread_plan_sp.get(); 792 } 793 794 ThreadPlan * 795 Thread::QueueThreadPlanForCallFunction (bool abort_other_plans, 796 Address& function, 797 lldb::addr_t arg, 798 bool stop_other_threads, 799 bool discard_on_error) 800 { 801 ThreadPlanSP thread_plan_sp (new ThreadPlanCallFunction (*this, function, arg, stop_other_threads, discard_on_error)); 802 QueueThreadPlan (thread_plan_sp, abort_other_plans); 803 return thread_plan_sp.get(); 804 } 805 806 ThreadPlan * 807 Thread::QueueThreadPlanForRunToAddress (bool abort_other_plans, 808 Address &target_addr, 809 bool stop_other_threads) 810 { 811 ThreadPlanSP thread_plan_sp (new ThreadPlanRunToAddress (*this, target_addr, stop_other_threads)); 812 QueueThreadPlan (thread_plan_sp, abort_other_plans); 813 return thread_plan_sp.get(); 814 } 815 816 ThreadPlan * 817 Thread::QueueThreadPlanForStepUntil (bool abort_other_plans, 818 lldb::addr_t *address_list, 819 size_t num_addresses, 820 bool stop_other_threads, 821 uint32_t frame_idx) 822 { 823 ThreadPlanSP thread_plan_sp (new ThreadPlanStepUntil (*this, address_list, num_addresses, stop_other_threads, frame_idx)); 824 QueueThreadPlan (thread_plan_sp, abort_other_plans); 825 return thread_plan_sp.get(); 826 827 } 828 829 uint32_t 830 Thread::GetIndexID () const 831 { 832 return m_index_id; 833 } 834 835 void 836 Thread::DumpThreadPlans (lldb_private::Stream *s) const 837 { 838 uint32_t stack_size = m_plan_stack.size(); 839 int i; 840 s->Printf ("Plan Stack for thread #%u: tid = 0x%4.4x, stack_size = %d\n", GetIndexID(), GetID(), stack_size); 841 for (i = stack_size - 1; i >= 0; i--) 842 { 843 s->Printf ("Element %d: ", i); 844 s->IndentMore(); 845 m_plan_stack[i]->GetDescription (s, eDescriptionLevelFull); 846 s->IndentLess(); 847 s->EOL(); 848 } 849 850 stack_size = m_completed_plan_stack.size(); 851 s->Printf ("Completed Plan Stack: %d elements.\n", stack_size); 852 for (i = stack_size - 1; i >= 0; i--) 853 { 854 s->Printf ("Element %d: ", i); 855 s->IndentMore(); 856 m_completed_plan_stack[i]->GetDescription (s, eDescriptionLevelFull); 857 s->IndentLess(); 858 s->EOL(); 859 } 860 861 stack_size = m_discarded_plan_stack.size(); 862 s->Printf ("Discarded Plan Stack: %d elements.\n", stack_size); 863 for (i = stack_size - 1; i >= 0; i--) 864 { 865 s->Printf ("Element %d: ", i); 866 s->IndentMore(); 867 m_discarded_plan_stack[i]->GetDescription (s, eDescriptionLevelFull); 868 s->IndentLess(); 869 s->EOL(); 870 } 871 872 } 873 874 Target * 875 Thread::CalculateTarget () 876 { 877 return m_process.CalculateTarget(); 878 } 879 880 Process * 881 Thread::CalculateProcess () 882 { 883 return &m_process; 884 } 885 886 Thread * 887 Thread::CalculateThread () 888 { 889 return this; 890 } 891 892 StackFrame * 893 Thread::CalculateStackFrame () 894 { 895 return NULL; 896 } 897 898 void 899 Thread::CalculateExecutionContext (ExecutionContext &exe_ctx) 900 { 901 m_process.CalculateExecutionContext (exe_ctx); 902 exe_ctx.thread = this; 903 exe_ctx.frame = NULL; 904 } 905 906 907 StackFrameList & 908 Thread::GetStackFrameList () 909 { 910 if (m_curr_frames_ap.get() == NULL) 911 m_curr_frames_ap.reset (new StackFrameList (*this, m_prev_frames_sp, true)); 912 return *m_curr_frames_ap; 913 } 914 915 916 917 uint32_t 918 Thread::GetStackFrameCount() 919 { 920 return GetStackFrameList().GetNumFrames(); 921 } 922 923 924 void 925 Thread::ClearStackFrames () 926 { 927 if (m_curr_frames_ap.get() && m_curr_frames_ap->GetNumFrames (false) > 1) 928 m_prev_frames_sp.reset (m_curr_frames_ap.release()); 929 else 930 m_curr_frames_ap.release(); 931 932 // StackFrameList::Merge (m_curr_frames_ap, m_prev_frames_sp); 933 // assert (m_curr_frames_ap.get() == NULL); 934 } 935 936 lldb::StackFrameSP 937 Thread::GetStackFrameAtIndex (uint32_t idx) 938 { 939 return GetStackFrameList().GetFrameAtIndex(idx); 940 } 941 942 uint32_t 943 Thread::GetSelectedFrameIndex () 944 { 945 return GetStackFrameList().GetSelectedFrameIndex(); 946 } 947 948 lldb::StackFrameSP 949 Thread::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx) 950 { 951 return GetStackFrameList().GetFrameWithConcreteFrameIndex (unwind_idx); 952 } 953 954 lldb::StackFrameSP 955 Thread::GetFrameWithStackID(StackID &stack_id) 956 { 957 return GetStackFrameList().GetFrameWithStackID (stack_id); 958 } 959 960 961 lldb::StackFrameSP 962 Thread::GetSelectedFrame () 963 { 964 return GetStackFrameAtIndex (GetStackFrameList().GetSelectedFrameIndex()); 965 } 966 967 uint32_t 968 Thread::SetSelectedFrame (lldb_private::StackFrame *frame) 969 { 970 return GetStackFrameList().SetSelectedFrame(frame); 971 } 972 973 void 974 Thread::SetSelectedFrameByIndex (uint32_t idx) 975 { 976 GetStackFrameList().SetSelectedFrameByIndex(idx); 977 } 978 979 void 980 Thread::DumpUsingSettingsFormat (Stream &strm, uint32_t frame_idx) 981 { 982 ExecutionContext exe_ctx; 983 SymbolContext frame_sc; 984 CalculateExecutionContext (exe_ctx); 985 986 if (frame_idx != LLDB_INVALID_INDEX32) 987 { 988 StackFrameSP frame_sp(GetStackFrameAtIndex (frame_idx)); 989 if (frame_sp) 990 { 991 exe_ctx.frame = frame_sp.get(); 992 frame_sc = exe_ctx.frame->GetSymbolContext(eSymbolContextEverything); 993 } 994 } 995 996 const char *thread_format = GetProcess().GetTarget().GetDebugger().GetThreadFormat(); 997 assert (thread_format); 998 const char *end = NULL; 999 Debugger::FormatPrompt (thread_format, 1000 exe_ctx.frame ? &frame_sc : NULL, 1001 &exe_ctx, 1002 NULL, 1003 strm, 1004 &end); 1005 } 1006 1007 lldb::ThreadSP 1008 Thread::GetSP () 1009 { 1010 return m_process.GetThreadList().GetThreadSPForThreadPtr(this); 1011 } 1012 1013 1014 void 1015 Thread::SettingsInitialize () 1016 { 1017 UserSettingsControllerSP &usc = GetSettingsController(); 1018 usc.reset (new SettingsController); 1019 UserSettingsController::InitializeSettingsController (usc, 1020 SettingsController::global_settings_table, 1021 SettingsController::instance_settings_table); 1022 1023 // Now call SettingsInitialize() on each 'child' setting of Thread. 1024 // Currently there are none. 1025 } 1026 1027 void 1028 Thread::SettingsTerminate () 1029 { 1030 // Must call SettingsTerminate() on each 'child' setting of Thread before terminating Thread settings. 1031 // Currently there are none. 1032 1033 // Now terminate Thread Settings. 1034 1035 UserSettingsControllerSP &usc = GetSettingsController(); 1036 UserSettingsController::FinalizeSettingsController (usc); 1037 usc.reset(); 1038 } 1039 1040 UserSettingsControllerSP & 1041 Thread::GetSettingsController () 1042 { 1043 static UserSettingsControllerSP g_settings_controller; 1044 return g_settings_controller; 1045 } 1046 1047 void 1048 Thread::UpdateInstanceName () 1049 { 1050 StreamString sstr; 1051 const char *name = GetName(); 1052 1053 if (name && name[0] != '\0') 1054 sstr.Printf ("%s", name); 1055 else if ((GetIndexID() != 0) || (GetID() != 0)) 1056 sstr.Printf ("0x%4.4x", GetIndexID(), GetID()); 1057 1058 if (sstr.GetSize() > 0) 1059 Thread::GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(), sstr.GetData()); 1060 } 1061 1062 lldb::StackFrameSP 1063 Thread::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr) 1064 { 1065 return GetStackFrameList().GetStackFrameSPForStackFramePtr (stack_frame_ptr); 1066 } 1067 1068 const char * 1069 Thread::StopReasonAsCString (lldb::StopReason reason) 1070 { 1071 switch (reason) 1072 { 1073 case eStopReasonInvalid: return "invalid"; 1074 case eStopReasonNone: return "none"; 1075 case eStopReasonTrace: return "trace"; 1076 case eStopReasonBreakpoint: return "breakpoint"; 1077 case eStopReasonWatchpoint: return "watchpoint"; 1078 case eStopReasonSignal: return "signal"; 1079 case eStopReasonException: return "exception"; 1080 case eStopReasonPlanComplete: return "plan complete"; 1081 } 1082 1083 1084 static char unknown_state_string[64]; 1085 snprintf(unknown_state_string, sizeof (unknown_state_string), "StopReason = %i", reason); 1086 return unknown_state_string; 1087 } 1088 1089 const char * 1090 Thread::RunModeAsCString (lldb::RunMode mode) 1091 { 1092 switch (mode) 1093 { 1094 case eOnlyThisThread: return "only this thread"; 1095 case eAllThreads: return "all threads"; 1096 case eOnlyDuringStepping: return "only during stepping"; 1097 } 1098 1099 static char unknown_state_string[64]; 1100 snprintf(unknown_state_string, sizeof (unknown_state_string), "RunMode = %i", mode); 1101 return unknown_state_string; 1102 } 1103 1104 size_t 1105 Thread::GetStatus (Stream &strm, uint32_t start_frame, uint32_t num_frames, uint32_t num_frames_with_source) 1106 { 1107 size_t num_frames_shown = 0; 1108 strm.Indent(); 1109 strm.Printf("%c ", GetProcess().GetThreadList().GetSelectedThread().get() == this ? '*' : ' '); 1110 1111 StackFrameSP frame_sp = GetStackFrameAtIndex(start_frame); 1112 SymbolContext frame_sc(frame_sp->GetSymbolContext (eSymbolContextLineEntry)); 1113 if (frame_sc.line_entry.line != 0 && 1114 frame_sc.line_entry.file && 1115 GetProcess().GetTarget().GetDebugger().GetUseExternalEditor()) 1116 { 1117 Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line); 1118 } 1119 1120 DumpUsingSettingsFormat (strm, start_frame); 1121 1122 if (num_frames > 0) 1123 { 1124 strm.IndentMore(); 1125 1126 const bool show_frame_info = true; 1127 const uint32_t source_lines_before = 3; 1128 const uint32_t source_lines_after = 3; 1129 num_frames_shown = GetStackFrameList ().GetStatus (strm, 1130 start_frame, 1131 num_frames, 1132 show_frame_info, 1133 num_frames_with_source, 1134 source_lines_before, 1135 source_lines_after); 1136 strm.IndentLess(); 1137 } 1138 return num_frames_shown; 1139 } 1140 1141 size_t 1142 Thread::GetStackFrameStatus (Stream& strm, 1143 uint32_t first_frame, 1144 uint32_t num_frames, 1145 bool show_frame_info, 1146 uint32_t num_frames_with_source, 1147 uint32_t source_lines_before, 1148 uint32_t source_lines_after) 1149 { 1150 return GetStackFrameList().GetStatus (strm, 1151 first_frame, 1152 num_frames, 1153 show_frame_info, 1154 num_frames_with_source, 1155 source_lines_before, 1156 source_lines_after); 1157 } 1158 1159 1160 1161 #pragma mark "Thread::SettingsController" 1162 //-------------------------------------------------------------- 1163 // class Thread::SettingsController 1164 //-------------------------------------------------------------- 1165 1166 Thread::SettingsController::SettingsController () : 1167 UserSettingsController ("thread", Process::GetSettingsController()) 1168 { 1169 m_default_settings.reset (new ThreadInstanceSettings (*this, false, 1170 InstanceSettings::GetDefaultName().AsCString())); 1171 } 1172 1173 Thread::SettingsController::~SettingsController () 1174 { 1175 } 1176 1177 lldb::InstanceSettingsSP 1178 Thread::SettingsController::CreateInstanceSettings (const char *instance_name) 1179 { 1180 ThreadInstanceSettings *new_settings = new ThreadInstanceSettings (*GetSettingsController(), 1181 false, 1182 instance_name); 1183 lldb::InstanceSettingsSP new_settings_sp (new_settings); 1184 return new_settings_sp; 1185 } 1186 1187 #pragma mark "ThreadInstanceSettings" 1188 //-------------------------------------------------------------- 1189 // class ThreadInstanceSettings 1190 //-------------------------------------------------------------- 1191 1192 ThreadInstanceSettings::ThreadInstanceSettings (UserSettingsController &owner, bool live_instance, const char *name) : 1193 InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance), 1194 m_avoid_regexp_ap (), 1195 m_trace_enabled (false) 1196 { 1197 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called 1198 // until the vtables for ThreadInstanceSettings are properly set up, i.e. AFTER all the initializers. 1199 // For this reason it has to be called here, rather than in the initializer or in the parent constructor. 1200 // This is true for CreateInstanceName() too. 1201 1202 if (GetInstanceName() == InstanceSettings::InvalidName()) 1203 { 1204 ChangeInstanceName (std::string (CreateInstanceName().AsCString())); 1205 m_owner.RegisterInstanceSettings (this); 1206 } 1207 1208 if (live_instance) 1209 { 1210 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name); 1211 CopyInstanceSettings (pending_settings,false); 1212 //m_owner.RemovePendingSettings (m_instance_name); 1213 } 1214 } 1215 1216 ThreadInstanceSettings::ThreadInstanceSettings (const ThreadInstanceSettings &rhs) : 1217 InstanceSettings (*Thread::GetSettingsController(), CreateInstanceName().AsCString()), 1218 m_avoid_regexp_ap (), 1219 m_trace_enabled (rhs.m_trace_enabled) 1220 { 1221 if (m_instance_name != InstanceSettings::GetDefaultName()) 1222 { 1223 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name); 1224 CopyInstanceSettings (pending_settings,false); 1225 m_owner.RemovePendingSettings (m_instance_name); 1226 } 1227 if (rhs.m_avoid_regexp_ap.get() != NULL) 1228 m_avoid_regexp_ap.reset(new RegularExpression(rhs.m_avoid_regexp_ap->GetText())); 1229 } 1230 1231 ThreadInstanceSettings::~ThreadInstanceSettings () 1232 { 1233 } 1234 1235 ThreadInstanceSettings& 1236 ThreadInstanceSettings::operator= (const ThreadInstanceSettings &rhs) 1237 { 1238 if (this != &rhs) 1239 { 1240 if (rhs.m_avoid_regexp_ap.get() != NULL) 1241 m_avoid_regexp_ap.reset(new RegularExpression(rhs.m_avoid_regexp_ap->GetText())); 1242 else 1243 m_avoid_regexp_ap.reset(NULL); 1244 } 1245 m_trace_enabled = rhs.m_trace_enabled; 1246 return *this; 1247 } 1248 1249 1250 void 1251 ThreadInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name, 1252 const char *index_value, 1253 const char *value, 1254 const ConstString &instance_name, 1255 const SettingEntry &entry, 1256 VarSetOperationType op, 1257 Error &err, 1258 bool pending) 1259 { 1260 if (var_name == StepAvoidRegexpVarName()) 1261 { 1262 std::string regexp_text; 1263 if (m_avoid_regexp_ap.get() != NULL) 1264 regexp_text.append (m_avoid_regexp_ap->GetText()); 1265 UserSettingsController::UpdateStringVariable (op, regexp_text, value, err); 1266 if (regexp_text.empty()) 1267 m_avoid_regexp_ap.reset(); 1268 else 1269 { 1270 m_avoid_regexp_ap.reset(new RegularExpression(regexp_text.c_str())); 1271 1272 } 1273 } 1274 else if (var_name == GetTraceThreadVarName()) 1275 { 1276 bool success; 1277 bool result = Args::StringToBoolean(value, false, &success); 1278 1279 if (success) 1280 { 1281 m_trace_enabled = result; 1282 if (!pending) 1283 { 1284 Thread *myself = static_cast<Thread *> (this); 1285 myself->EnableTracer(m_trace_enabled, true); 1286 } 1287 } 1288 else 1289 { 1290 err.SetErrorStringWithFormat ("Bad value \"%s\" for trace-thread, should be Boolean.", value); 1291 } 1292 1293 } 1294 } 1295 1296 void 1297 ThreadInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings, 1298 bool pending) 1299 { 1300 if (new_settings.get() == NULL) 1301 return; 1302 1303 ThreadInstanceSettings *new_process_settings = (ThreadInstanceSettings *) new_settings.get(); 1304 if (new_process_settings->GetSymbolsToAvoidRegexp() != NULL) 1305 m_avoid_regexp_ap.reset (new RegularExpression (new_process_settings->GetSymbolsToAvoidRegexp()->GetText())); 1306 else 1307 m_avoid_regexp_ap.reset (); 1308 } 1309 1310 bool 1311 ThreadInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry, 1312 const ConstString &var_name, 1313 StringList &value, 1314 Error *err) 1315 { 1316 if (var_name == StepAvoidRegexpVarName()) 1317 { 1318 if (m_avoid_regexp_ap.get() != NULL) 1319 { 1320 std::string regexp_text("\""); 1321 regexp_text.append(m_avoid_regexp_ap->GetText()); 1322 regexp_text.append ("\""); 1323 value.AppendString (regexp_text.c_str()); 1324 } 1325 1326 } 1327 else if (var_name == GetTraceThreadVarName()) 1328 { 1329 value.AppendString(m_trace_enabled ? "true" : "false"); 1330 } 1331 else 1332 { 1333 if (err) 1334 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString()); 1335 return false; 1336 } 1337 return true; 1338 } 1339 1340 const ConstString 1341 ThreadInstanceSettings::CreateInstanceName () 1342 { 1343 static int instance_count = 1; 1344 StreamString sstr; 1345 1346 sstr.Printf ("thread_%d", instance_count); 1347 ++instance_count; 1348 1349 const ConstString ret_val (sstr.GetData()); 1350 return ret_val; 1351 } 1352 1353 const ConstString & 1354 ThreadInstanceSettings::StepAvoidRegexpVarName () 1355 { 1356 static ConstString step_avoid_var_name ("step-avoid-regexp"); 1357 1358 return step_avoid_var_name; 1359 } 1360 1361 const ConstString & 1362 ThreadInstanceSettings::GetTraceThreadVarName () 1363 { 1364 static ConstString trace_thread_var_name ("trace-thread"); 1365 1366 return trace_thread_var_name; 1367 } 1368 1369 //-------------------------------------------------- 1370 // SettingsController Variable Tables 1371 //-------------------------------------------------- 1372 1373 SettingEntry 1374 Thread::SettingsController::global_settings_table[] = 1375 { 1376 //{ "var-name", var-type , "default", enum-table, init'd, hidden, "help-text"}, 1377 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL } 1378 }; 1379 1380 1381 SettingEntry 1382 Thread::SettingsController::instance_settings_table[] = 1383 { 1384 //{ "var-name", var-type, "default", enum-table, init'd, hidden, "help-text"}, 1385 { "step-avoid-regexp", eSetVarTypeString, "", NULL, false, false, "A regular expression defining functions step-in won't stop in." }, 1386 { "trace-thread", eSetVarTypeBoolean, "false", NULL, false, false, "If true, this thread will single-step and log execution." }, 1387 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL } 1388 }; 1389