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