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/Symbol/Function.h" 19 #include "lldb/Target/DynamicLoader.h" 20 #include "lldb/Target/ExecutionContext.h" 21 #include "lldb/Target/ObjCLanguageRuntime.h" 22 #include "lldb/Target/Process.h" 23 #include "lldb/Target/RegisterContext.h" 24 #include "lldb/Target/StopInfo.h" 25 #include "lldb/Target/Target.h" 26 #include "lldb/Target/Thread.h" 27 #include "lldb/Target/ThreadPlan.h" 28 #include "lldb/Target/ThreadPlanCallFunction.h" 29 #include "lldb/Target/ThreadPlanBase.h" 30 #include "lldb/Target/ThreadPlanStepInstruction.h" 31 #include "lldb/Target/ThreadPlanStepOut.h" 32 #include "lldb/Target/ThreadPlanStepOverBreakpoint.h" 33 #include "lldb/Target/ThreadPlanStepThrough.h" 34 #include "lldb/Target/ThreadPlanStepInRange.h" 35 #include "lldb/Target/ThreadPlanStepOverRange.h" 36 #include "lldb/Target/ThreadPlanRunToAddress.h" 37 #include "lldb/Target/ThreadPlanStepUntil.h" 38 #include "lldb/Target/ThreadSpec.h" 39 #include "lldb/Target/Unwind.h" 40 #include "Plugins/Process/Utility/UnwindLLDB.h" 41 #include "UnwindMacOSXFrameBackchain.h" 42 43 44 using namespace lldb; 45 using namespace lldb_private; 46 47 48 const ThreadPropertiesSP & 49 Thread::GetGlobalProperties() 50 { 51 static ThreadPropertiesSP g_settings_sp; 52 if (!g_settings_sp) 53 g_settings_sp.reset (new ThreadProperties (true)); 54 return g_settings_sp; 55 } 56 57 static PropertyDefinition 58 g_properties[] = 59 { 60 { "step-avoid-regexp", OptionValue::eTypeRegex , true , REG_EXTENDED, "^std::", NULL, "A regular expression defining functions step-in won't stop in." }, 61 { "trace-thread", OptionValue::eTypeBoolean, false, false, NULL, NULL, "If true, this thread will single-step and log execution." }, 62 { NULL , OptionValue::eTypeInvalid, false, 0 , NULL, NULL, NULL } 63 }; 64 65 enum { 66 ePropertyStepAvoidRegex, 67 ePropertyEnableThreadTrace 68 }; 69 70 71 class ThreadOptionValueProperties : public OptionValueProperties 72 { 73 public: 74 ThreadOptionValueProperties (const ConstString &name) : 75 OptionValueProperties (name) 76 { 77 } 78 79 // This constructor is used when creating ThreadOptionValueProperties when it 80 // is part of a new lldb_private::Thread instance. It will copy all current 81 // global property values as needed 82 ThreadOptionValueProperties (ThreadProperties *global_properties) : 83 OptionValueProperties(*global_properties->GetValueProperties()) 84 { 85 } 86 87 virtual const Property * 88 GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const 89 { 90 // When gettings the value for a key from the thread options, we will always 91 // try and grab the setting from the current thread if there is one. Else we just 92 // use the one from this instance. 93 if (exe_ctx) 94 { 95 Thread *thread = exe_ctx->GetThreadPtr(); 96 if (thread) 97 { 98 ThreadOptionValueProperties *instance_properties = static_cast<ThreadOptionValueProperties *>(thread->GetValueProperties().get()); 99 if (this != instance_properties) 100 return instance_properties->ProtectedGetPropertyAtIndex (idx); 101 } 102 } 103 return ProtectedGetPropertyAtIndex (idx); 104 } 105 }; 106 107 108 109 ThreadProperties::ThreadProperties (bool is_global) : 110 Properties () 111 { 112 if (is_global) 113 { 114 m_collection_sp.reset (new ThreadOptionValueProperties(ConstString("thread"))); 115 m_collection_sp->Initialize(g_properties); 116 } 117 else 118 m_collection_sp.reset (new ThreadOptionValueProperties(Thread::GetGlobalProperties().get())); 119 } 120 121 ThreadProperties::~ThreadProperties() 122 { 123 } 124 125 const RegularExpression * 126 ThreadProperties::GetSymbolsToAvoidRegexp() 127 { 128 const uint32_t idx = ePropertyStepAvoidRegex; 129 return m_collection_sp->GetPropertyAtIndexAsOptionValueRegex (NULL, idx); 130 } 131 132 bool 133 ThreadProperties::GetTraceEnabledState() const 134 { 135 const uint32_t idx = ePropertyEnableThreadTrace; 136 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0); 137 } 138 139 //------------------------------------------------------------------ 140 // Thread Event Data 141 //------------------------------------------------------------------ 142 143 144 const ConstString & 145 Thread::ThreadEventData::GetFlavorString () 146 { 147 static ConstString g_flavor ("Thread::ThreadEventData"); 148 return g_flavor; 149 } 150 151 Thread::ThreadEventData::ThreadEventData (const lldb::ThreadSP thread_sp) : 152 m_thread_sp (thread_sp), 153 m_stack_id () 154 { 155 } 156 157 Thread::ThreadEventData::ThreadEventData (const lldb::ThreadSP thread_sp, const StackID &stack_id) : 158 m_thread_sp (thread_sp), 159 m_stack_id (stack_id) 160 { 161 } 162 163 Thread::ThreadEventData::ThreadEventData () : 164 m_thread_sp (), 165 m_stack_id () 166 { 167 } 168 169 Thread::ThreadEventData::~ThreadEventData () 170 { 171 } 172 173 void 174 Thread::ThreadEventData::Dump (Stream *s) const 175 { 176 177 } 178 179 const Thread::ThreadEventData * 180 Thread::ThreadEventData::GetEventDataFromEvent (const Event *event_ptr) 181 { 182 if (event_ptr) 183 { 184 const EventData *event_data = event_ptr->GetData(); 185 if (event_data && event_data->GetFlavor() == ThreadEventData::GetFlavorString()) 186 return static_cast <const ThreadEventData *> (event_ptr->GetData()); 187 } 188 return NULL; 189 } 190 191 ThreadSP 192 Thread::ThreadEventData::GetThreadFromEvent (const Event *event_ptr) 193 { 194 ThreadSP thread_sp; 195 const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr); 196 if (event_data) 197 thread_sp = event_data->GetThread(); 198 return thread_sp; 199 } 200 201 StackID 202 Thread::ThreadEventData::GetStackIDFromEvent (const Event *event_ptr) 203 { 204 StackID stack_id; 205 const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr); 206 if (event_data) 207 stack_id = event_data->GetStackID(); 208 return stack_id; 209 } 210 211 StackFrameSP 212 Thread::ThreadEventData::GetStackFrameFromEvent (const Event *event_ptr) 213 { 214 const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr); 215 StackFrameSP frame_sp; 216 if (event_data) 217 { 218 ThreadSP thread_sp = event_data->GetThread(); 219 if (thread_sp) 220 { 221 frame_sp = thread_sp->GetStackFrameList()->GetFrameWithStackID (event_data->GetStackID()); 222 } 223 } 224 return frame_sp; 225 } 226 227 //------------------------------------------------------------------ 228 // Thread class 229 //------------------------------------------------------------------ 230 231 ConstString & 232 Thread::GetStaticBroadcasterClass () 233 { 234 static ConstString class_name ("lldb.thread"); 235 return class_name; 236 } 237 238 Thread::Thread (Process &process, lldb::tid_t tid) : 239 ThreadProperties (false), 240 UserID (tid), 241 Broadcaster(&process.GetTarget().GetDebugger(), Thread::GetStaticBroadcasterClass().AsCString()), 242 m_process_wp (process.shared_from_this()), 243 m_actual_stop_info_sp (), 244 m_index_id (process.GetNextThreadIndexID ()), 245 m_reg_context_sp (), 246 m_state (eStateUnloaded), 247 m_state_mutex (Mutex::eMutexTypeRecursive), 248 m_plan_stack (), 249 m_completed_plan_stack(), 250 m_frame_mutex (Mutex::eMutexTypeRecursive), 251 m_curr_frames_sp (), 252 m_prev_frames_sp (), 253 m_resume_signal (LLDB_INVALID_SIGNAL_NUMBER), 254 m_resume_state (eStateRunning), 255 m_temporary_resume_state (eStateRunning), 256 m_unwinder_ap (), 257 m_destroy_called (false), 258 m_thread_stop_reason_stop_id (0) 259 260 { 261 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); 262 if (log) 263 log->Printf ("%p Thread::Thread(tid = 0x%4.4llx)", this, GetID()); 264 265 CheckInWithManager(); 266 QueueFundamentalPlan(true); 267 } 268 269 270 Thread::~Thread() 271 { 272 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); 273 if (log) 274 log->Printf ("%p Thread::~Thread(tid = 0x%4.4llx)", this, GetID()); 275 /// If you hit this assert, it means your derived class forgot to call DoDestroy in its destructor. 276 assert (m_destroy_called); 277 } 278 279 void 280 Thread::DestroyThread () 281 { 282 m_plan_stack.clear(); 283 m_discarded_plan_stack.clear(); 284 m_completed_plan_stack.clear(); 285 m_actual_stop_info_sp.reset(); 286 m_destroy_called = true; 287 } 288 289 void 290 Thread::BroadcastSelectedFrameChange(StackID &new_frame_id) 291 { 292 if (EventTypeHasListeners(eBroadcastBitSelectedFrameChanged)) 293 BroadcastEvent(eBroadcastBitSelectedFrameChanged, new ThreadEventData (this->shared_from_this(), new_frame_id)); 294 } 295 296 uint32_t 297 Thread::SetSelectedFrame (lldb_private::StackFrame *frame, bool broadcast) 298 { 299 uint32_t ret_value = GetStackFrameList()->SetSelectedFrame(frame); 300 if (broadcast) 301 BroadcastSelectedFrameChange(frame->GetStackID()); 302 return ret_value; 303 } 304 305 bool 306 Thread::SetSelectedFrameByIndex (uint32_t frame_idx, bool broadcast) 307 { 308 StackFrameSP frame_sp(GetStackFrameList()->GetFrameAtIndex (frame_idx)); 309 if (frame_sp) 310 { 311 GetStackFrameList()->SetSelectedFrame(frame_sp.get()); 312 if (broadcast) 313 BroadcastSelectedFrameChange(frame_sp->GetStackID()); 314 return true; 315 } 316 else 317 return false; 318 } 319 320 321 lldb::StopInfoSP 322 Thread::GetStopInfo () 323 { 324 ThreadPlanSP plan_sp (GetCompletedPlan()); 325 if (plan_sp && plan_sp->PlanSucceeded()) 326 return StopInfo::CreateStopReasonWithPlan (plan_sp, GetReturnValueObject()); 327 else 328 { 329 ProcessSP process_sp (GetProcess()); 330 if (process_sp 331 && m_actual_stop_info_sp 332 && m_actual_stop_info_sp->IsValid() 333 && m_thread_stop_reason_stop_id == process_sp->GetStopID()) 334 return m_actual_stop_info_sp; 335 else 336 return GetPrivateStopReason (); 337 } 338 } 339 340 lldb::StopReason 341 Thread::GetStopReason() 342 { 343 lldb::StopInfoSP stop_info_sp (GetStopInfo ()); 344 if (stop_info_sp) 345 return stop_info_sp->GetStopReason(); 346 return eStopReasonNone; 347 } 348 349 350 351 void 352 Thread::SetStopInfo (const lldb::StopInfoSP &stop_info_sp) 353 { 354 m_actual_stop_info_sp = stop_info_sp; 355 if (m_actual_stop_info_sp) 356 m_actual_stop_info_sp->MakeStopInfoValid(); 357 ProcessSP process_sp (GetProcess()); 358 if (process_sp) 359 m_thread_stop_reason_stop_id = process_sp->GetStopID(); 360 else 361 m_thread_stop_reason_stop_id = UINT32_MAX; 362 } 363 364 void 365 Thread::SetStopInfoToNothing() 366 { 367 // Note, we can't just NULL out the private reason, or the native thread implementation will try to 368 // go calculate it again. For now, just set it to a Unix Signal with an invalid signal number. 369 SetStopInfo (StopInfo::CreateStopReasonWithSignal (*this, LLDB_INVALID_SIGNAL_NUMBER)); 370 } 371 372 bool 373 Thread::ThreadStoppedForAReason (void) 374 { 375 return (bool) GetPrivateStopReason (); 376 } 377 378 bool 379 Thread::CheckpointThreadState (ThreadStateCheckpoint &saved_state) 380 { 381 if (!SaveFrameZeroState(saved_state.register_backup)) 382 return false; 383 384 saved_state.stop_info_sp = GetStopInfo(); 385 ProcessSP process_sp (GetProcess()); 386 if (process_sp) 387 saved_state.orig_stop_id = process_sp->GetStopID(); 388 saved_state.current_inlined_depth = GetCurrentInlinedDepth(); 389 390 return true; 391 } 392 393 bool 394 Thread::RestoreThreadStateFromCheckpoint (ThreadStateCheckpoint &saved_state) 395 { 396 RestoreSaveFrameZero(saved_state.register_backup); 397 if (saved_state.stop_info_sp) 398 saved_state.stop_info_sp->MakeStopInfoValid(); 399 SetStopInfo(saved_state.stop_info_sp); 400 GetStackFrameList()->SetCurrentInlinedDepth (saved_state.current_inlined_depth); 401 return true; 402 } 403 404 StateType 405 Thread::GetState() const 406 { 407 // If any other threads access this we will need a mutex for it 408 Mutex::Locker locker(m_state_mutex); 409 return m_state; 410 } 411 412 void 413 Thread::SetState(StateType state) 414 { 415 Mutex::Locker locker(m_state_mutex); 416 m_state = state; 417 } 418 419 void 420 Thread::WillStop() 421 { 422 ThreadPlan *current_plan = GetCurrentPlan(); 423 424 // FIXME: I may decide to disallow threads with no plans. In which 425 // case this should go to an assert. 426 427 if (!current_plan) 428 return; 429 430 current_plan->WillStop(); 431 } 432 433 void 434 Thread::SetupForResume () 435 { 436 if (GetResumeState() != eStateSuspended) 437 { 438 439 // If we're at a breakpoint push the step-over breakpoint plan. Do this before 440 // telling the current plan it will resume, since we might change what the current 441 // plan is. 442 443 StopReason stop_reason = lldb::eStopReasonInvalid; 444 StopInfoSP stop_info_sp = GetStopInfo(); 445 if (stop_info_sp.get()) 446 stop_reason = stop_info_sp->GetStopReason(); 447 if (stop_reason == lldb::eStopReasonBreakpoint) 448 { 449 // Note, don't assume there's a ThreadPlanStepOverBreakpoint, the target may not require anything 450 // special to step over a breakpoint. 451 452 ThreadPlan *cur_plan = GetCurrentPlan(); 453 454 if (cur_plan->GetKind() != ThreadPlan::eKindStepOverBreakpoint) 455 { 456 ThreadPlanStepOverBreakpoint *step_bp_plan = new ThreadPlanStepOverBreakpoint (*this); 457 if (step_bp_plan) 458 { 459 ThreadPlanSP step_bp_plan_sp; 460 step_bp_plan->SetPrivate (true); 461 462 if (GetCurrentPlan()->RunState() != eStateStepping) 463 { 464 step_bp_plan->SetAutoContinue(true); 465 } 466 step_bp_plan_sp.reset (step_bp_plan); 467 QueueThreadPlan (step_bp_plan_sp, false); 468 } 469 } 470 } 471 } 472 } 473 474 bool 475 Thread::WillResume (StateType resume_state) 476 { 477 // At this point clear the completed plan stack. 478 m_completed_plan_stack.clear(); 479 m_discarded_plan_stack.clear(); 480 481 m_temporary_resume_state = resume_state; 482 483 // This is a little dubious, but we are trying to limit how often we actually fetch stop info from 484 // the target, 'cause that slows down single stepping. So assume that if we got to the point where 485 // we're about to resume, and we haven't yet had to fetch the stop reason, then it doesn't need to know 486 // about the fact that we are resuming... 487 const uint32_t process_stop_id = GetProcess()->GetStopID(); 488 if (m_thread_stop_reason_stop_id == process_stop_id && 489 (m_actual_stop_info_sp && m_actual_stop_info_sp->IsValid())) 490 { 491 StopInfo *stop_info = GetPrivateStopReason().get(); 492 if (stop_info) 493 stop_info->WillResume (resume_state); 494 } 495 496 // Tell all the plans that we are about to resume in case they need to clear any state. 497 // We distinguish between the plan on the top of the stack and the lower 498 // plans in case a plan needs to do any special business before it runs. 499 500 ThreadPlan *plan_ptr = GetCurrentPlan(); 501 bool need_to_resume = plan_ptr->WillResume(resume_state, true); 502 503 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL) 504 { 505 plan_ptr->WillResume (resume_state, false); 506 } 507 508 // If the WillResume for the plan says we are faking a resume, then it will have set an appropriate stop info. 509 // In that case, don't reset it here. 510 511 if (need_to_resume && resume_state != eStateSuspended) 512 { 513 m_actual_stop_info_sp.reset(); 514 } 515 516 return need_to_resume; 517 } 518 519 void 520 Thread::DidResume () 521 { 522 SetResumeSignal (LLDB_INVALID_SIGNAL_NUMBER); 523 } 524 525 bool 526 Thread::ShouldStop (Event* event_ptr) 527 { 528 ThreadPlan *current_plan = GetCurrentPlan(); 529 bool should_stop = true; 530 531 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); 532 533 if (GetResumeState () == eStateSuspended) 534 { 535 if (log) 536 log->Printf ("Thread::%s for tid = 0x%4.4llx, should_stop = 0 (ignore since thread was suspended)", 537 __FUNCTION__, 538 GetID ()); 539 // log->Printf ("Thread::%s for tid = 0x%4.4llx, pc = 0x%16.16llx, should_stop = 0 (ignore since thread was suspended)", 540 // __FUNCTION__, 541 // GetID (), 542 // GetRegisterContext()->GetPC()); 543 return false; 544 } 545 546 if (GetTemporaryResumeState () == eStateSuspended) 547 { 548 if (log) 549 log->Printf ("Thread::%s for tid = 0x%4.4llx, should_stop = 0 (ignore since thread was suspended)", 550 __FUNCTION__, 551 GetID ()); 552 // log->Printf ("Thread::%s for tid = 0x%4.4llx, pc = 0x%16.16llx, should_stop = 0 (ignore since thread was suspended)", 553 // __FUNCTION__, 554 // GetID (), 555 // GetRegisterContext()->GetPC()); 556 return false; 557 } 558 559 if (ThreadStoppedForAReason() == false) 560 { 561 if (log) 562 log->Printf ("Thread::%s for tid = 0x%4.4llx, pc = 0x%16.16llx, should_stop = 0 (ignore since no stop reason)", 563 __FUNCTION__, 564 GetID (), 565 GetRegisterContext()->GetPC()); 566 return false; 567 } 568 569 if (log) 570 { 571 log->Printf ("Thread::%s for tid = 0x%4.4llx, pc = 0x%16.16llx", 572 __FUNCTION__, 573 GetID (), 574 GetRegisterContext()->GetPC()); 575 log->Printf ("^^^^^^^^ Thread::ShouldStop Begin ^^^^^^^^"); 576 StreamString s; 577 s.IndentMore(); 578 DumpThreadPlans(&s); 579 log->Printf ("Plan stack initial state:\n%s", s.GetData()); 580 } 581 582 // The top most plan always gets to do the trace log... 583 current_plan->DoTraceLog (); 584 585 // First query the stop info's ShouldStopSynchronous. This handles "synchronous" stop reasons, for example the breakpoint 586 // command on internal breakpoints. If a synchronous stop reason says we should not stop, then we don't have to 587 // do any more work on this stop. 588 StopInfoSP private_stop_info (GetPrivateStopReason()); 589 if (private_stop_info && private_stop_info->ShouldStopSynchronous(event_ptr) == false) 590 { 591 if (log) 592 log->Printf ("StopInfo::ShouldStop async callback says we should not stop, returning ShouldStop of false."); 593 return false; 594 } 595 596 // If we've already been restarted, don't query the plans since the state they would examine is not current. 597 if (Process::ProcessEventData::GetRestartedFromEvent(event_ptr)) 598 return false; 599 600 // Before the plans see the state of the world, calculate the current inlined depth. 601 GetStackFrameList()->CalculateCurrentInlinedDepth(); 602 603 // If the base plan doesn't understand why we stopped, then we have to find a plan that does. 604 // If that plan is still working, then we don't need to do any more work. If the plan that explains 605 // the stop is done, then we should pop all the plans below it, and pop it, and then let the plans above it decide 606 // whether they still need to do more work. 607 608 bool done_processing_current_plan = false; 609 610 if (!current_plan->PlanExplainsStop()) 611 { 612 if (current_plan->TracerExplainsStop()) 613 { 614 done_processing_current_plan = true; 615 should_stop = false; 616 } 617 else 618 { 619 // If the current plan doesn't explain the stop, then find one that 620 // does and let it handle the situation. 621 ThreadPlan *plan_ptr = current_plan; 622 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL) 623 { 624 if (plan_ptr->PlanExplainsStop()) 625 { 626 should_stop = plan_ptr->ShouldStop (event_ptr); 627 628 // plan_ptr explains the stop, next check whether plan_ptr is done, if so, then we should take it 629 // and all the plans below it off the stack. 630 631 if (plan_ptr->MischiefManaged()) 632 { 633 // We're going to pop the plans up to and including the plan that explains the stop. 634 ThreadPlan *prev_plan_ptr = GetPreviousPlan (plan_ptr); 635 636 do 637 { 638 if (should_stop) 639 current_plan->WillStop(); 640 PopPlan(); 641 } 642 while ((current_plan = GetCurrentPlan()) != prev_plan_ptr); 643 // Now, if the responsible plan was not "Okay to discard" then we're done, 644 // otherwise we forward this to the next plan in the stack below. 645 if (plan_ptr->IsMasterPlan() && !plan_ptr->OkayToDiscard()) 646 done_processing_current_plan = true; 647 else 648 done_processing_current_plan = false; 649 } 650 else 651 done_processing_current_plan = true; 652 653 break; 654 } 655 656 } 657 } 658 } 659 660 if (!done_processing_current_plan) 661 { 662 bool over_ride_stop = current_plan->ShouldAutoContinue(event_ptr); 663 664 if (log) 665 log->Printf("Plan %s explains stop, auto-continue %i.", current_plan->GetName(), over_ride_stop); 666 667 // We're starting from the base plan, so just let it decide; 668 if (PlanIsBasePlan(current_plan)) 669 { 670 should_stop = current_plan->ShouldStop (event_ptr); 671 if (log) 672 log->Printf("Base plan says should stop: %i.", should_stop); 673 } 674 else 675 { 676 // Otherwise, don't let the base plan override what the other plans say to do, since 677 // presumably if there were other plans they would know what to do... 678 while (1) 679 { 680 if (PlanIsBasePlan(current_plan)) 681 break; 682 683 should_stop = current_plan->ShouldStop(event_ptr); 684 if (log) 685 log->Printf("Plan %s should stop: %d.", current_plan->GetName(), should_stop); 686 if (current_plan->MischiefManaged()) 687 { 688 if (should_stop) 689 current_plan->WillStop(); 690 691 // If a Master Plan wants to stop, and wants to stick on the stack, we let it. 692 // Otherwise, see if the plan's parent wants to stop. 693 694 if (should_stop && current_plan->IsMasterPlan() && !current_plan->OkayToDiscard()) 695 { 696 PopPlan(); 697 break; 698 } 699 else 700 { 701 702 PopPlan(); 703 704 current_plan = GetCurrentPlan(); 705 if (current_plan == NULL) 706 { 707 break; 708 } 709 } 710 } 711 else 712 { 713 break; 714 } 715 } 716 } 717 718 if (over_ride_stop) 719 should_stop = false; 720 721 // One other potential problem is that we set up a master plan, then stop in before it is complete - for instance 722 // by hitting a breakpoint during a step-over - then do some step/finish/etc operations that wind up 723 // past the end point condition of the initial plan. We don't want to strand the original plan on the stack, 724 // This code clears stale plans off the stack. 725 726 if (should_stop) 727 { 728 ThreadPlan *plan_ptr = GetCurrentPlan(); 729 while (!PlanIsBasePlan(plan_ptr)) 730 { 731 bool stale = plan_ptr->IsPlanStale (); 732 ThreadPlan *examined_plan = plan_ptr; 733 plan_ptr = GetPreviousPlan (examined_plan); 734 735 if (stale) 736 { 737 if (log) 738 log->Printf("Plan %s being discarded in cleanup, it says it is already done.", examined_plan->GetName()); 739 DiscardThreadPlansUpToPlan(examined_plan); 740 } 741 } 742 } 743 744 } 745 746 if (log) 747 { 748 StreamString s; 749 s.IndentMore(); 750 DumpThreadPlans(&s); 751 log->Printf ("Plan stack final state:\n%s", s.GetData()); 752 log->Printf ("vvvvvvvv Thread::ShouldStop End (returning %i) vvvvvvvv", should_stop); 753 } 754 return should_stop; 755 } 756 757 Vote 758 Thread::ShouldReportStop (Event* event_ptr) 759 { 760 StateType thread_state = GetResumeState (); 761 StateType temp_thread_state = GetTemporaryResumeState(); 762 763 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); 764 765 if (thread_state == eStateSuspended || thread_state == eStateInvalid) 766 { 767 if (log) 768 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4llx: returning vote %i (state was suspended or invalid)\n", GetID(), eVoteNoOpinion); 769 return eVoteNoOpinion; 770 } 771 772 if (temp_thread_state == eStateSuspended || temp_thread_state == eStateInvalid) 773 { 774 if (log) 775 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4llx: returning vote %i (temporary state was suspended or invalid)\n", GetID(), eVoteNoOpinion); 776 return eVoteNoOpinion; 777 } 778 779 if (!ThreadStoppedForAReason()) 780 { 781 if (log) 782 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4llx: returning vote %i (thread didn't stop for a reason.)\n", GetID(), eVoteNoOpinion); 783 return eVoteNoOpinion; 784 } 785 786 if (m_completed_plan_stack.size() > 0) 787 { 788 // Don't use GetCompletedPlan here, since that suppresses private plans. 789 if (log) 790 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4llx: returning vote for complete stack's back plan\n", GetID()); 791 return m_completed_plan_stack.back()->ShouldReportStop (event_ptr); 792 } 793 else 794 { 795 if (log) 796 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4llx: returning vote for current plan\n", GetID()); 797 return GetCurrentPlan()->ShouldReportStop (event_ptr); 798 } 799 } 800 801 Vote 802 Thread::ShouldReportRun (Event* event_ptr) 803 { 804 StateType thread_state = GetResumeState (); 805 806 if (thread_state == eStateSuspended 807 || thread_state == eStateInvalid) 808 { 809 return eVoteNoOpinion; 810 } 811 812 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); 813 if (m_completed_plan_stack.size() > 0) 814 { 815 // Don't use GetCompletedPlan here, since that suppresses private plans. 816 if (log) 817 log->Printf ("Current Plan for thread %d (0x%4.4llx): %s being asked whether we should report run.", 818 GetIndexID(), 819 GetID(), 820 m_completed_plan_stack.back()->GetName()); 821 822 return m_completed_plan_stack.back()->ShouldReportRun (event_ptr); 823 } 824 else 825 { 826 if (log) 827 log->Printf ("Current Plan for thread %d (0x%4.4llx): %s being asked whether we should report run.", 828 GetIndexID(), 829 GetID(), 830 GetCurrentPlan()->GetName()); 831 832 return GetCurrentPlan()->ShouldReportRun (event_ptr); 833 } 834 } 835 836 bool 837 Thread::MatchesSpec (const ThreadSpec *spec) 838 { 839 if (spec == NULL) 840 return true; 841 842 return spec->ThreadPassesBasicTests(*this); 843 } 844 845 void 846 Thread::PushPlan (ThreadPlanSP &thread_plan_sp) 847 { 848 if (thread_plan_sp) 849 { 850 // If the thread plan doesn't already have a tracer, give it its parent's tracer: 851 if (!thread_plan_sp->GetThreadPlanTracer()) 852 thread_plan_sp->SetThreadPlanTracer(m_plan_stack.back()->GetThreadPlanTracer()); 853 m_plan_stack.push_back (thread_plan_sp); 854 855 thread_plan_sp->DidPush(); 856 857 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); 858 if (log) 859 { 860 StreamString s; 861 thread_plan_sp->GetDescription (&s, lldb::eDescriptionLevelFull); 862 log->Printf("Pushing plan: \"%s\", tid = 0x%4.4llx.", 863 s.GetData(), 864 thread_plan_sp->GetThread().GetID()); 865 } 866 } 867 } 868 869 void 870 Thread::PopPlan () 871 { 872 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); 873 874 if (m_plan_stack.size() <= 1) 875 return; 876 else 877 { 878 ThreadPlanSP &plan = m_plan_stack.back(); 879 if (log) 880 { 881 log->Printf("Popping plan: \"%s\", tid = 0x%4.4llx.", plan->GetName(), plan->GetThread().GetID()); 882 } 883 m_completed_plan_stack.push_back (plan); 884 plan->WillPop(); 885 m_plan_stack.pop_back(); 886 } 887 } 888 889 void 890 Thread::DiscardPlan () 891 { 892 if (m_plan_stack.size() > 1) 893 { 894 ThreadPlanSP &plan = m_plan_stack.back(); 895 m_discarded_plan_stack.push_back (plan); 896 plan->WillPop(); 897 m_plan_stack.pop_back(); 898 } 899 } 900 901 ThreadPlan * 902 Thread::GetCurrentPlan () 903 { 904 // There will always be at least the base plan. If somebody is mucking with a 905 // thread with an empty plan stack, we should assert right away. 906 assert (!m_plan_stack.empty()); 907 908 return m_plan_stack.back().get(); 909 } 910 911 ThreadPlanSP 912 Thread::GetCompletedPlan () 913 { 914 ThreadPlanSP empty_plan_sp; 915 if (!m_completed_plan_stack.empty()) 916 { 917 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--) 918 { 919 ThreadPlanSP completed_plan_sp; 920 completed_plan_sp = m_completed_plan_stack[i]; 921 if (!completed_plan_sp->GetPrivate ()) 922 return completed_plan_sp; 923 } 924 } 925 return empty_plan_sp; 926 } 927 928 ValueObjectSP 929 Thread::GetReturnValueObject () 930 { 931 if (!m_completed_plan_stack.empty()) 932 { 933 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--) 934 { 935 ValueObjectSP return_valobj_sp; 936 return_valobj_sp = m_completed_plan_stack[i]->GetReturnValueObject(); 937 if (return_valobj_sp) 938 return return_valobj_sp; 939 } 940 } 941 return ValueObjectSP(); 942 } 943 944 bool 945 Thread::IsThreadPlanDone (ThreadPlan *plan) 946 { 947 if (!m_completed_plan_stack.empty()) 948 { 949 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--) 950 { 951 if (m_completed_plan_stack[i].get() == plan) 952 return true; 953 } 954 } 955 return false; 956 } 957 958 bool 959 Thread::WasThreadPlanDiscarded (ThreadPlan *plan) 960 { 961 if (!m_discarded_plan_stack.empty()) 962 { 963 for (int i = m_discarded_plan_stack.size() - 1; i >= 0; i--) 964 { 965 if (m_discarded_plan_stack[i].get() == plan) 966 return true; 967 } 968 } 969 return false; 970 } 971 972 ThreadPlan * 973 Thread::GetPreviousPlan (ThreadPlan *current_plan) 974 { 975 if (current_plan == NULL) 976 return NULL; 977 978 int stack_size = m_completed_plan_stack.size(); 979 for (int i = stack_size - 1; i > 0; i--) 980 { 981 if (current_plan == m_completed_plan_stack[i].get()) 982 return m_completed_plan_stack[i-1].get(); 983 } 984 985 if (stack_size > 0 && m_completed_plan_stack[0].get() == current_plan) 986 { 987 if (m_plan_stack.size() > 0) 988 return m_plan_stack.back().get(); 989 else 990 return NULL; 991 } 992 993 stack_size = m_plan_stack.size(); 994 for (int i = stack_size - 1; i > 0; i--) 995 { 996 if (current_plan == m_plan_stack[i].get()) 997 return m_plan_stack[i-1].get(); 998 } 999 return NULL; 1000 } 1001 1002 void 1003 Thread::QueueThreadPlan (ThreadPlanSP &thread_plan_sp, bool abort_other_plans) 1004 { 1005 if (abort_other_plans) 1006 DiscardThreadPlans(true); 1007 1008 PushPlan (thread_plan_sp); 1009 } 1010 1011 1012 void 1013 Thread::EnableTracer (bool value, bool single_stepping) 1014 { 1015 int stack_size = m_plan_stack.size(); 1016 for (int i = 0; i < stack_size; i++) 1017 { 1018 if (m_plan_stack[i]->GetThreadPlanTracer()) 1019 { 1020 m_plan_stack[i]->GetThreadPlanTracer()->EnableTracing(value); 1021 m_plan_stack[i]->GetThreadPlanTracer()->EnableSingleStep(single_stepping); 1022 } 1023 } 1024 } 1025 1026 void 1027 Thread::SetTracer (lldb::ThreadPlanTracerSP &tracer_sp) 1028 { 1029 int stack_size = m_plan_stack.size(); 1030 for (int i = 0; i < stack_size; i++) 1031 m_plan_stack[i]->SetThreadPlanTracer(tracer_sp); 1032 } 1033 1034 void 1035 Thread::DiscardThreadPlansUpToPlan (lldb::ThreadPlanSP &up_to_plan_sp) 1036 { 1037 DiscardThreadPlansUpToPlan (up_to_plan_sp.get()); 1038 } 1039 1040 void 1041 Thread::DiscardThreadPlansUpToPlan (ThreadPlan *up_to_plan_ptr) 1042 { 1043 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); 1044 if (log) 1045 { 1046 log->Printf("Discarding thread plans for thread tid = 0x%4.4llx, up to %p", GetID(), up_to_plan_ptr); 1047 } 1048 1049 int stack_size = m_plan_stack.size(); 1050 1051 // If the input plan is NULL, discard all plans. Otherwise make sure this plan is in the 1052 // stack, and if so discard up to and including it. 1053 1054 if (up_to_plan_ptr == NULL) 1055 { 1056 for (int i = stack_size - 1; i > 0; i--) 1057 DiscardPlan(); 1058 } 1059 else 1060 { 1061 bool found_it = false; 1062 for (int i = stack_size - 1; i > 0; i--) 1063 { 1064 if (m_plan_stack[i].get() == up_to_plan_ptr) 1065 found_it = true; 1066 } 1067 if (found_it) 1068 { 1069 bool last_one = false; 1070 for (int i = stack_size - 1; i > 0 && !last_one ; i--) 1071 { 1072 if (GetCurrentPlan() == up_to_plan_ptr) 1073 last_one = true; 1074 DiscardPlan(); 1075 } 1076 } 1077 } 1078 return; 1079 } 1080 1081 void 1082 Thread::DiscardThreadPlans(bool force) 1083 { 1084 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); 1085 if (log) 1086 { 1087 log->Printf("Discarding thread plans for thread (tid = 0x%4.4llx, force %d)", GetID(), force); 1088 } 1089 1090 if (force) 1091 { 1092 int stack_size = m_plan_stack.size(); 1093 for (int i = stack_size - 1; i > 0; i--) 1094 { 1095 DiscardPlan(); 1096 } 1097 return; 1098 } 1099 1100 while (1) 1101 { 1102 1103 int master_plan_idx; 1104 bool discard = true; 1105 1106 // Find the first master plan, see if it wants discarding, and if yes discard up to it. 1107 for (master_plan_idx = m_plan_stack.size() - 1; master_plan_idx >= 0; master_plan_idx--) 1108 { 1109 if (m_plan_stack[master_plan_idx]->IsMasterPlan()) 1110 { 1111 discard = m_plan_stack[master_plan_idx]->OkayToDiscard(); 1112 break; 1113 } 1114 } 1115 1116 if (discard) 1117 { 1118 // First pop all the dependent plans: 1119 for (int i = m_plan_stack.size() - 1; i > master_plan_idx; i--) 1120 { 1121 1122 // FIXME: Do we need a finalize here, or is the rule that "PrepareForStop" 1123 // for the plan leaves it in a state that it is safe to pop the plan 1124 // with no more notice? 1125 DiscardPlan(); 1126 } 1127 1128 // Now discard the master plan itself. 1129 // The bottom-most plan never gets discarded. "OkayToDiscard" for it means 1130 // discard it's dependent plans, but not it... 1131 if (master_plan_idx > 0) 1132 { 1133 DiscardPlan(); 1134 } 1135 } 1136 else 1137 { 1138 // If the master plan doesn't want to get discarded, then we're done. 1139 break; 1140 } 1141 1142 } 1143 } 1144 1145 bool 1146 Thread::PlanIsBasePlan (ThreadPlan *plan_ptr) 1147 { 1148 if (plan_ptr->IsBasePlan()) 1149 return true; 1150 else if (m_plan_stack.size() == 0) 1151 return false; 1152 else 1153 return m_plan_stack[0].get() == plan_ptr; 1154 } 1155 1156 ThreadPlan * 1157 Thread::QueueFundamentalPlan (bool abort_other_plans) 1158 { 1159 ThreadPlanSP thread_plan_sp (new ThreadPlanBase(*this)); 1160 QueueThreadPlan (thread_plan_sp, abort_other_plans); 1161 return thread_plan_sp.get(); 1162 } 1163 1164 ThreadPlan * 1165 Thread::QueueThreadPlanForStepSingleInstruction 1166 ( 1167 bool step_over, 1168 bool abort_other_plans, 1169 bool stop_other_threads 1170 ) 1171 { 1172 ThreadPlanSP thread_plan_sp (new ThreadPlanStepInstruction (*this, step_over, stop_other_threads, eVoteNoOpinion, eVoteNoOpinion)); 1173 QueueThreadPlan (thread_plan_sp, abort_other_plans); 1174 return thread_plan_sp.get(); 1175 } 1176 1177 ThreadPlan * 1178 Thread::QueueThreadPlanForStepRange 1179 ( 1180 bool abort_other_plans, 1181 StepType type, 1182 const AddressRange &range, 1183 const SymbolContext &addr_context, 1184 lldb::RunMode stop_other_threads, 1185 bool avoid_code_without_debug_info 1186 ) 1187 { 1188 ThreadPlanSP thread_plan_sp; 1189 if (type == eStepTypeInto) 1190 { 1191 ThreadPlanStepInRange *plan = new ThreadPlanStepInRange (*this, range, addr_context, stop_other_threads); 1192 if (avoid_code_without_debug_info) 1193 plan->GetFlags().Set (ThreadPlanShouldStopHere::eAvoidNoDebug); 1194 else 1195 plan->GetFlags().Clear (ThreadPlanShouldStopHere::eAvoidNoDebug); 1196 thread_plan_sp.reset (plan); 1197 } 1198 else 1199 thread_plan_sp.reset (new ThreadPlanStepOverRange (*this, range, addr_context, stop_other_threads)); 1200 1201 QueueThreadPlan (thread_plan_sp, abort_other_plans); 1202 return thread_plan_sp.get(); 1203 } 1204 1205 1206 ThreadPlan * 1207 Thread::QueueThreadPlanForStepOverBreakpointPlan (bool abort_other_plans) 1208 { 1209 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOverBreakpoint (*this)); 1210 QueueThreadPlan (thread_plan_sp, abort_other_plans); 1211 return thread_plan_sp.get(); 1212 } 1213 1214 ThreadPlan * 1215 Thread::QueueThreadPlanForStepOut 1216 ( 1217 bool abort_other_plans, 1218 SymbolContext *addr_context, 1219 bool first_insn, 1220 bool stop_other_threads, 1221 Vote stop_vote, 1222 Vote run_vote, 1223 uint32_t frame_idx 1224 ) 1225 { 1226 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOut (*this, 1227 addr_context, 1228 first_insn, 1229 stop_other_threads, 1230 stop_vote, 1231 run_vote, 1232 frame_idx)); 1233 1234 if (thread_plan_sp->ValidatePlan(NULL)) 1235 { 1236 QueueThreadPlan (thread_plan_sp, abort_other_plans); 1237 return thread_plan_sp.get(); 1238 } 1239 else 1240 { 1241 return NULL; 1242 } 1243 } 1244 1245 ThreadPlan * 1246 Thread::QueueThreadPlanForStepThrough (StackID &return_stack_id, bool abort_other_plans, bool stop_other_threads) 1247 { 1248 ThreadPlanSP thread_plan_sp(new ThreadPlanStepThrough (*this, return_stack_id, stop_other_threads)); 1249 if (!thread_plan_sp || !thread_plan_sp->ValidatePlan (NULL)) 1250 return NULL; 1251 1252 QueueThreadPlan (thread_plan_sp, abort_other_plans); 1253 return thread_plan_sp.get(); 1254 } 1255 1256 ThreadPlan * 1257 Thread::QueueThreadPlanForCallFunction (bool abort_other_plans, 1258 Address& function, 1259 lldb::addr_t arg, 1260 bool stop_other_threads, 1261 bool discard_on_error) 1262 { 1263 ThreadPlanSP thread_plan_sp (new ThreadPlanCallFunction (*this, function, ClangASTType(), arg, stop_other_threads, discard_on_error)); 1264 QueueThreadPlan (thread_plan_sp, abort_other_plans); 1265 return thread_plan_sp.get(); 1266 } 1267 1268 ThreadPlan * 1269 Thread::QueueThreadPlanForRunToAddress (bool abort_other_plans, 1270 Address &target_addr, 1271 bool stop_other_threads) 1272 { 1273 ThreadPlanSP thread_plan_sp (new ThreadPlanRunToAddress (*this, target_addr, stop_other_threads)); 1274 QueueThreadPlan (thread_plan_sp, abort_other_plans); 1275 return thread_plan_sp.get(); 1276 } 1277 1278 ThreadPlan * 1279 Thread::QueueThreadPlanForStepUntil (bool abort_other_plans, 1280 lldb::addr_t *address_list, 1281 size_t num_addresses, 1282 bool stop_other_threads, 1283 uint32_t frame_idx) 1284 { 1285 ThreadPlanSP thread_plan_sp (new ThreadPlanStepUntil (*this, address_list, num_addresses, stop_other_threads, frame_idx)); 1286 QueueThreadPlan (thread_plan_sp, abort_other_plans); 1287 return thread_plan_sp.get(); 1288 1289 } 1290 1291 uint32_t 1292 Thread::GetIndexID () const 1293 { 1294 return m_index_id; 1295 } 1296 1297 void 1298 Thread::DumpThreadPlans (lldb_private::Stream *s) const 1299 { 1300 uint32_t stack_size = m_plan_stack.size(); 1301 int i; 1302 s->Indent(); 1303 s->Printf ("Plan Stack for thread #%u: tid = 0x%4.4llx, stack_size = %d\n", GetIndexID(), GetID(), stack_size); 1304 for (i = stack_size - 1; i >= 0; i--) 1305 { 1306 s->IndentMore(); 1307 s->Indent(); 1308 s->Printf ("Element %d: ", i); 1309 m_plan_stack[i]->GetDescription (s, eDescriptionLevelFull); 1310 s->EOL(); 1311 s->IndentLess(); 1312 } 1313 1314 stack_size = m_completed_plan_stack.size(); 1315 if (stack_size > 0) 1316 { 1317 s->Indent(); 1318 s->Printf ("Completed Plan Stack: %d elements.\n", stack_size); 1319 for (i = stack_size - 1; i >= 0; i--) 1320 { 1321 s->IndentMore(); 1322 s->Indent(); 1323 s->Printf ("Element %d: ", i); 1324 m_completed_plan_stack[i]->GetDescription (s, eDescriptionLevelFull); 1325 s->EOL(); 1326 s->IndentLess(); 1327 } 1328 } 1329 1330 stack_size = m_discarded_plan_stack.size(); 1331 if (stack_size > 0) 1332 { 1333 s->Indent(); 1334 s->Printf ("Discarded Plan Stack: %d elements.\n", stack_size); 1335 for (i = stack_size - 1; i >= 0; i--) 1336 { 1337 s->IndentMore(); 1338 s->Indent(); 1339 s->Printf ("Element %d: ", i); 1340 m_discarded_plan_stack[i]->GetDescription (s, eDescriptionLevelFull); 1341 s->EOL(); 1342 s->IndentLess(); 1343 } 1344 } 1345 1346 } 1347 1348 TargetSP 1349 Thread::CalculateTarget () 1350 { 1351 TargetSP target_sp; 1352 ProcessSP process_sp(GetProcess()); 1353 if (process_sp) 1354 target_sp = process_sp->CalculateTarget(); 1355 return target_sp; 1356 1357 } 1358 1359 ProcessSP 1360 Thread::CalculateProcess () 1361 { 1362 return GetProcess(); 1363 } 1364 1365 ThreadSP 1366 Thread::CalculateThread () 1367 { 1368 return shared_from_this(); 1369 } 1370 1371 StackFrameSP 1372 Thread::CalculateStackFrame () 1373 { 1374 return StackFrameSP(); 1375 } 1376 1377 void 1378 Thread::CalculateExecutionContext (ExecutionContext &exe_ctx) 1379 { 1380 exe_ctx.SetContext (shared_from_this()); 1381 } 1382 1383 1384 StackFrameListSP 1385 Thread::GetStackFrameList () 1386 { 1387 StackFrameListSP frame_list_sp; 1388 Mutex::Locker locker(m_frame_mutex); 1389 if (m_curr_frames_sp) 1390 { 1391 frame_list_sp = m_curr_frames_sp; 1392 } 1393 else 1394 { 1395 frame_list_sp.reset(new StackFrameList (*this, m_prev_frames_sp, true)); 1396 m_curr_frames_sp = frame_list_sp; 1397 } 1398 return frame_list_sp; 1399 } 1400 1401 void 1402 Thread::ClearStackFrames () 1403 { 1404 Mutex::Locker locker(m_frame_mutex); 1405 1406 // Only store away the old "reference" StackFrameList if we got all its frames: 1407 // FIXME: At some point we can try to splice in the frames we have fetched into 1408 // the new frame as we make it, but let's not try that now. 1409 if (m_curr_frames_sp && m_curr_frames_sp->GetAllFramesFetched()) 1410 m_prev_frames_sp.swap (m_curr_frames_sp); 1411 m_curr_frames_sp.reset(); 1412 } 1413 1414 lldb::StackFrameSP 1415 Thread::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx) 1416 { 1417 return GetStackFrameList()->GetFrameWithConcreteFrameIndex (unwind_idx); 1418 } 1419 1420 1421 Error 1422 Thread::ReturnFromFrameWithIndex (uint32_t frame_idx, lldb::ValueObjectSP return_value_sp, bool broadcast) 1423 { 1424 StackFrameSP frame_sp = GetStackFrameAtIndex (frame_idx); 1425 Error return_error; 1426 1427 if (!frame_sp) 1428 { 1429 return_error.SetErrorStringWithFormat("Could not find frame with index %d in thread 0x%llx.", frame_idx, GetID()); 1430 } 1431 1432 return ReturnFromFrame(frame_sp, return_value_sp, broadcast); 1433 } 1434 1435 Error 1436 Thread::ReturnFromFrame (lldb::StackFrameSP frame_sp, lldb::ValueObjectSP return_value_sp, bool broadcast) 1437 { 1438 Error return_error; 1439 1440 if (!frame_sp) 1441 { 1442 return_error.SetErrorString("Can't return to a null frame."); 1443 return return_error; 1444 } 1445 1446 Thread *thread = frame_sp->GetThread().get(); 1447 uint32_t older_frame_idx = frame_sp->GetFrameIndex() + 1; 1448 StackFrameSP older_frame_sp = thread->GetStackFrameAtIndex(older_frame_idx); 1449 1450 if (return_value_sp) 1451 { 1452 lldb::ABISP abi = thread->GetProcess()->GetABI(); 1453 if (!abi) 1454 { 1455 return_error.SetErrorString("Could not find ABI to set return value."); 1456 return return_error; 1457 } 1458 SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextFunction); 1459 1460 // FIXME: ValueObject::Cast doesn't currently work correctly, at least not for scalars. 1461 // Turn that back on when that works. 1462 if (0 && sc.function != NULL) 1463 { 1464 Type *function_type = sc.function->GetType(); 1465 if (function_type) 1466 { 1467 clang_type_t return_type = sc.function->GetReturnClangType(); 1468 if (return_type) 1469 { 1470 ClangASTType ast_type (function_type->GetClangAST(), return_type); 1471 StreamString s; 1472 ast_type.DumpTypeDescription(&s); 1473 ValueObjectSP cast_value_sp = return_value_sp->Cast(ast_type); 1474 if (cast_value_sp) 1475 { 1476 cast_value_sp->SetFormat(eFormatHex); 1477 return_value_sp = cast_value_sp; 1478 } 1479 } 1480 } 1481 } 1482 1483 return_error = abi->SetReturnValueObject(older_frame_sp, return_value_sp); 1484 if (!return_error.Success()) 1485 return return_error; 1486 } 1487 1488 // Now write the return registers for the chosen frame: 1489 // Note, we can't use ReadAllRegisterValues->WriteAllRegisterValues, since the read & write 1490 // cook their data 1491 bool copy_success = thread->GetStackFrameAtIndex(0)->GetRegisterContext()->CopyFromRegisterContext(older_frame_sp->GetRegisterContext()); 1492 if (copy_success) 1493 { 1494 thread->DiscardThreadPlans(true); 1495 thread->ClearStackFrames(); 1496 if (broadcast && EventTypeHasListeners(eBroadcastBitStackChanged)) 1497 BroadcastEvent(eBroadcastBitStackChanged, new ThreadEventData (this->shared_from_this())); 1498 return return_error; 1499 } 1500 else 1501 { 1502 return_error.SetErrorString("Could not reset register values."); 1503 return return_error; 1504 } 1505 } 1506 1507 void 1508 Thread::DumpUsingSettingsFormat (Stream &strm, uint32_t frame_idx) 1509 { 1510 ExecutionContext exe_ctx (shared_from_this()); 1511 Process *process = exe_ctx.GetProcessPtr(); 1512 if (process == NULL) 1513 return; 1514 1515 StackFrameSP frame_sp; 1516 SymbolContext frame_sc; 1517 if (frame_idx != LLDB_INVALID_INDEX32) 1518 { 1519 frame_sp = GetStackFrameAtIndex (frame_idx); 1520 if (frame_sp) 1521 { 1522 exe_ctx.SetFrameSP(frame_sp); 1523 frame_sc = frame_sp->GetSymbolContext(eSymbolContextEverything); 1524 } 1525 } 1526 1527 const char *thread_format = exe_ctx.GetTargetRef().GetDebugger().GetThreadFormat(); 1528 assert (thread_format); 1529 const char *end = NULL; 1530 Debugger::FormatPrompt (thread_format, 1531 frame_sp ? &frame_sc : NULL, 1532 &exe_ctx, 1533 NULL, 1534 strm, 1535 &end); 1536 } 1537 1538 void 1539 Thread::SettingsInitialize () 1540 { 1541 } 1542 1543 void 1544 Thread::SettingsTerminate () 1545 { 1546 } 1547 1548 lldb::StackFrameSP 1549 Thread::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr) 1550 { 1551 return GetStackFrameList()->GetStackFrameSPForStackFramePtr (stack_frame_ptr); 1552 } 1553 1554 const char * 1555 Thread::StopReasonAsCString (lldb::StopReason reason) 1556 { 1557 switch (reason) 1558 { 1559 case eStopReasonInvalid: return "invalid"; 1560 case eStopReasonNone: return "none"; 1561 case eStopReasonTrace: return "trace"; 1562 case eStopReasonBreakpoint: return "breakpoint"; 1563 case eStopReasonWatchpoint: return "watchpoint"; 1564 case eStopReasonSignal: return "signal"; 1565 case eStopReasonException: return "exception"; 1566 case eStopReasonPlanComplete: return "plan complete"; 1567 } 1568 1569 1570 static char unknown_state_string[64]; 1571 snprintf(unknown_state_string, sizeof (unknown_state_string), "StopReason = %i", reason); 1572 return unknown_state_string; 1573 } 1574 1575 const char * 1576 Thread::RunModeAsCString (lldb::RunMode mode) 1577 { 1578 switch (mode) 1579 { 1580 case eOnlyThisThread: return "only this thread"; 1581 case eAllThreads: return "all threads"; 1582 case eOnlyDuringStepping: return "only during stepping"; 1583 } 1584 1585 static char unknown_state_string[64]; 1586 snprintf(unknown_state_string, sizeof (unknown_state_string), "RunMode = %i", mode); 1587 return unknown_state_string; 1588 } 1589 1590 size_t 1591 Thread::GetStatus (Stream &strm, uint32_t start_frame, uint32_t num_frames, uint32_t num_frames_with_source) 1592 { 1593 ExecutionContext exe_ctx (shared_from_this()); 1594 Target *target = exe_ctx.GetTargetPtr(); 1595 Process *process = exe_ctx.GetProcessPtr(); 1596 size_t num_frames_shown = 0; 1597 strm.Indent(); 1598 bool is_selected = false; 1599 if (process) 1600 { 1601 if (process->GetThreadList().GetSelectedThread().get() == this) 1602 is_selected = true; 1603 } 1604 strm.Printf("%c ", is_selected ? '*' : ' '); 1605 if (target && target->GetDebugger().GetUseExternalEditor()) 1606 { 1607 StackFrameSP frame_sp = GetStackFrameAtIndex(start_frame); 1608 if (frame_sp) 1609 { 1610 SymbolContext frame_sc(frame_sp->GetSymbolContext (eSymbolContextLineEntry)); 1611 if (frame_sc.line_entry.line != 0 && frame_sc.line_entry.file) 1612 { 1613 Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line); 1614 } 1615 } 1616 } 1617 1618 DumpUsingSettingsFormat (strm, start_frame); 1619 1620 if (num_frames > 0) 1621 { 1622 strm.IndentMore(); 1623 1624 const bool show_frame_info = true; 1625 strm.IndentMore (); 1626 num_frames_shown = GetStackFrameList ()->GetStatus (strm, 1627 start_frame, 1628 num_frames, 1629 show_frame_info, 1630 num_frames_with_source); 1631 strm.IndentLess(); 1632 strm.IndentLess(); 1633 } 1634 return num_frames_shown; 1635 } 1636 1637 size_t 1638 Thread::GetStackFrameStatus (Stream& strm, 1639 uint32_t first_frame, 1640 uint32_t num_frames, 1641 bool show_frame_info, 1642 uint32_t num_frames_with_source) 1643 { 1644 return GetStackFrameList()->GetStatus (strm, 1645 first_frame, 1646 num_frames, 1647 show_frame_info, 1648 num_frames_with_source); 1649 } 1650 1651 bool 1652 Thread::SaveFrameZeroState (RegisterCheckpoint &checkpoint) 1653 { 1654 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0)); 1655 if (frame_sp) 1656 { 1657 checkpoint.SetStackID(frame_sp->GetStackID()); 1658 return frame_sp->GetRegisterContext()->ReadAllRegisterValues (checkpoint.GetData()); 1659 } 1660 return false; 1661 } 1662 1663 bool 1664 Thread::RestoreSaveFrameZero (const RegisterCheckpoint &checkpoint) 1665 { 1666 return ResetFrameZeroRegisters (checkpoint.GetData()); 1667 } 1668 1669 bool 1670 Thread::ResetFrameZeroRegisters (lldb::DataBufferSP register_data_sp) 1671 { 1672 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0)); 1673 if (frame_sp) 1674 { 1675 bool ret = frame_sp->GetRegisterContext()->WriteAllRegisterValues (register_data_sp); 1676 1677 // Clear out all stack frames as our world just changed. 1678 ClearStackFrames(); 1679 frame_sp->GetRegisterContext()->InvalidateIfNeeded(true); 1680 1681 return ret; 1682 } 1683 return false; 1684 } 1685 1686 Unwind * 1687 Thread::GetUnwinder () 1688 { 1689 if (m_unwinder_ap.get() == NULL) 1690 { 1691 const ArchSpec target_arch (CalculateTarget()->GetArchitecture ()); 1692 const llvm::Triple::ArchType machine = target_arch.GetMachine(); 1693 switch (machine) 1694 { 1695 case llvm::Triple::x86_64: 1696 case llvm::Triple::x86: 1697 case llvm::Triple::arm: 1698 case llvm::Triple::thumb: 1699 m_unwinder_ap.reset (new UnwindLLDB (*this)); 1700 break; 1701 1702 default: 1703 if (target_arch.GetTriple().getVendor() == llvm::Triple::Apple) 1704 m_unwinder_ap.reset (new UnwindMacOSXFrameBackchain (*this)); 1705 break; 1706 } 1707 } 1708 return m_unwinder_ap.get(); 1709 } 1710 1711 1712 void 1713 Thread::Flush () 1714 { 1715 ClearStackFrames (); 1716 m_reg_context_sp.reset(); 1717 } 1718 1719 const bool 1720 Thread::IsStillAtLastBreakpointHit () 1721 { 1722 // If we are currently stopped at a breakpoint, always return that stopinfo and don't reset it. 1723 // This allows threads to maintain their breakpoint stopinfo, such as when thread-stepping in 1724 // multithreaded programs. 1725 if (m_actual_stop_info_sp) { 1726 StopReason stop_reason = m_actual_stop_info_sp->GetStopReason(); 1727 if (stop_reason == lldb::eStopReasonBreakpoint) { 1728 uint64_t value = m_actual_stop_info_sp->GetValue(); 1729 lldb::addr_t pc = GetRegisterContext()->GetPC(); 1730 BreakpointSiteSP bp_site_sp = GetProcess()->GetBreakpointSiteList().FindByAddress(pc); 1731 if (bp_site_sp && value == bp_site_sp->GetID()) 1732 return true; 1733 } 1734 } 1735 return false; 1736 } 1737