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