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