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