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