1 //===-- Thread.cpp ----------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/lldb-python.h"
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/Interpreter/OptionValueFileSpecList.h"
22 #include "lldb/Symbol/Function.h"
23 #include "lldb/Target/DynamicLoader.h"
24 #include "lldb/Target/ExecutionContext.h"
25 #include "lldb/Target/ObjCLanguageRuntime.h"
26 #include "lldb/Target/Process.h"
27 #include "lldb/Target/RegisterContext.h"
28 #include "lldb/Target/StopInfo.h"
29 #include "lldb/Target/Target.h"
30 #include "lldb/Target/Thread.h"
31 #include "lldb/Target/ThreadPlan.h"
32 #include "lldb/Target/ThreadPlanCallFunction.h"
33 #include "lldb/Target/ThreadPlanBase.h"
34 #include "lldb/Target/ThreadPlanStepInstruction.h"
35 #include "lldb/Target/ThreadPlanStepOut.h"
36 #include "lldb/Target/ThreadPlanStepOverBreakpoint.h"
37 #include "lldb/Target/ThreadPlanStepThrough.h"
38 #include "lldb/Target/ThreadPlanStepInRange.h"
39 #include "lldb/Target/ThreadPlanStepOverRange.h"
40 #include "lldb/Target/ThreadPlanRunToAddress.h"
41 #include "lldb/Target/ThreadPlanStepUntil.h"
42 #include "lldb/Target/ThreadSpec.h"
43 #include "lldb/Target/Unwind.h"
44 #include "Plugins/Process/Utility/UnwindLLDB.h"
45 #include "UnwindMacOSXFrameBackchain.h"
49 using namespace lldb_private;
52 const ThreadPropertiesSP &
53 Thread::GetGlobalProperties()
55 static ThreadPropertiesSP g_settings_sp;
57 g_settings_sp.reset (new ThreadProperties (true));
61 static PropertyDefinition
64 { "step-avoid-regexp", OptionValue::eTypeRegex , true , REG_EXTENDED, "^std::", NULL, "A regular expression defining functions step-in won't stop in." },
65 { "step-avoid-libraries", OptionValue::eTypeFileSpecList , true , REG_EXTENDED, NULL, NULL, "A list of libraries that source stepping won't stop in." },
66 { "trace-thread", OptionValue::eTypeBoolean, false, false, NULL, NULL, "If true, this thread will single-step and log execution." },
67 { NULL , OptionValue::eTypeInvalid, false, 0 , NULL, NULL, NULL }
71 ePropertyStepAvoidRegex,
72 ePropertyStepAvoidLibraries,
73 ePropertyEnableThreadTrace
77 class ThreadOptionValueProperties : public OptionValueProperties
80 ThreadOptionValueProperties (const ConstString &name) :
81 OptionValueProperties (name)
85 // This constructor is used when creating ThreadOptionValueProperties when it
86 // is part of a new lldb_private::Thread instance. It will copy all current
87 // global property values as needed
88 ThreadOptionValueProperties (ThreadProperties *global_properties) :
89 OptionValueProperties(*global_properties->GetValueProperties())
93 virtual const Property *
94 GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
96 // When gettings the value for a key from the thread options, we will always
97 // try and grab the setting from the current thread if there is one. Else we just
98 // use the one from this instance.
101 Thread *thread = exe_ctx->GetThreadPtr();
104 ThreadOptionValueProperties *instance_properties = static_cast<ThreadOptionValueProperties *>(thread->GetValueProperties().get());
105 if (this != instance_properties)
106 return instance_properties->ProtectedGetPropertyAtIndex (idx);
109 return ProtectedGetPropertyAtIndex (idx);
115 ThreadProperties::ThreadProperties (bool is_global) :
120 m_collection_sp.reset (new ThreadOptionValueProperties(ConstString("thread")));
121 m_collection_sp->Initialize(g_properties);
124 m_collection_sp.reset (new ThreadOptionValueProperties(Thread::GetGlobalProperties().get()));
127 ThreadProperties::~ThreadProperties()
131 const RegularExpression *
132 ThreadProperties::GetSymbolsToAvoidRegexp()
134 const uint32_t idx = ePropertyStepAvoidRegex;
135 return m_collection_sp->GetPropertyAtIndexAsOptionValueRegex (NULL, idx);
139 ThreadProperties::GetLibrariesToAvoid() const
141 const uint32_t idx = ePropertyStepAvoidLibraries;
142 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
143 assert(option_value);
144 return option_value->GetCurrentValue();
148 ThreadProperties::GetTraceEnabledState() const
150 const uint32_t idx = ePropertyEnableThreadTrace;
151 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
154 //------------------------------------------------------------------
156 //------------------------------------------------------------------
160 Thread::ThreadEventData::GetFlavorString ()
162 static ConstString g_flavor ("Thread::ThreadEventData");
166 Thread::ThreadEventData::ThreadEventData (const lldb::ThreadSP thread_sp) :
167 m_thread_sp (thread_sp),
172 Thread::ThreadEventData::ThreadEventData (const lldb::ThreadSP thread_sp, const StackID &stack_id) :
173 m_thread_sp (thread_sp),
174 m_stack_id (stack_id)
178 Thread::ThreadEventData::ThreadEventData () :
184 Thread::ThreadEventData::~ThreadEventData ()
189 Thread::ThreadEventData::Dump (Stream *s) const
194 const Thread::ThreadEventData *
195 Thread::ThreadEventData::GetEventDataFromEvent (const Event *event_ptr)
199 const EventData *event_data = event_ptr->GetData();
200 if (event_data && event_data->GetFlavor() == ThreadEventData::GetFlavorString())
201 return static_cast <const ThreadEventData *> (event_ptr->GetData());
207 Thread::ThreadEventData::GetThreadFromEvent (const Event *event_ptr)
210 const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr);
212 thread_sp = event_data->GetThread();
217 Thread::ThreadEventData::GetStackIDFromEvent (const Event *event_ptr)
220 const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr);
222 stack_id = event_data->GetStackID();
227 Thread::ThreadEventData::GetStackFrameFromEvent (const Event *event_ptr)
229 const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr);
230 StackFrameSP frame_sp;
233 ThreadSP thread_sp = event_data->GetThread();
236 frame_sp = thread_sp->GetStackFrameList()->GetFrameWithStackID (event_data->GetStackID());
242 //------------------------------------------------------------------
244 //------------------------------------------------------------------
247 Thread::GetStaticBroadcasterClass ()
249 static ConstString class_name ("lldb.thread");
253 Thread::Thread (Process &process, lldb::tid_t tid) :
254 ThreadProperties (false),
256 Broadcaster(&process.GetTarget().GetDebugger(), Thread::GetStaticBroadcasterClass().AsCString()),
257 m_process_wp (process.shared_from_this()),
259 m_stop_info_stop_id (0),
260 m_index_id (process.GetNextThreadIndexID(tid)),
262 m_state (eStateUnloaded),
263 m_state_mutex (Mutex::eMutexTypeRecursive),
265 m_completed_plan_stack(),
266 m_frame_mutex (Mutex::eMutexTypeRecursive),
269 m_resume_signal (LLDB_INVALID_SIGNAL_NUMBER),
270 m_resume_state (eStateRunning),
271 m_temporary_resume_state (eStateRunning),
273 m_destroy_called (false),
274 m_override_should_notify (eLazyBoolCalculate)
276 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
278 log->Printf ("%p Thread::Thread(tid = 0x%4.4" PRIx64 ")", this, GetID());
280 CheckInWithManager();
281 QueueFundamentalPlan(true);
287 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
289 log->Printf ("%p Thread::~Thread(tid = 0x%4.4" PRIx64 ")", this, GetID());
290 /// If you hit this assert, it means your derived class forgot to call DoDestroy in its destructor.
291 assert (m_destroy_called);
295 Thread::DestroyThread ()
297 // Tell any plans on the plan stacks that the thread is being destroyed since
298 // any plans that have a thread go away in the middle of might need
299 // to do cleanup, or in some cases NOT do cleanup...
300 for (auto plan : m_plan_stack)
301 plan->ThreadDestroyed();
303 for (auto plan : m_discarded_plan_stack)
304 plan->ThreadDestroyed();
306 for (auto plan : m_completed_plan_stack)
307 plan->ThreadDestroyed();
309 m_destroy_called = true;
310 m_plan_stack.clear();
311 m_discarded_plan_stack.clear();
312 m_completed_plan_stack.clear();
314 // Push a ThreadPlanNull on the plan stack. That way we can continue assuming that the
315 // plan stack is never empty, but if somebody errantly asks questions of a destroyed thread
316 // without checking first whether it is destroyed, they won't crash.
317 ThreadPlanSP null_plan_sp(new ThreadPlanNull (*this));
318 m_plan_stack.push_back (null_plan_sp);
320 m_stop_info_sp.reset();
321 m_reg_context_sp.reset();
322 m_unwinder_ap.reset();
323 Mutex::Locker locker(m_frame_mutex);
324 m_curr_frames_sp.reset();
325 m_prev_frames_sp.reset();
329 Thread::BroadcastSelectedFrameChange(StackID &new_frame_id)
331 if (EventTypeHasListeners(eBroadcastBitSelectedFrameChanged))
332 BroadcastEvent(eBroadcastBitSelectedFrameChanged, new ThreadEventData (this->shared_from_this(), new_frame_id));
336 Thread::SetSelectedFrame (lldb_private::StackFrame *frame, bool broadcast)
338 uint32_t ret_value = GetStackFrameList()->SetSelectedFrame(frame);
340 BroadcastSelectedFrameChange(frame->GetStackID());
345 Thread::SetSelectedFrameByIndex (uint32_t frame_idx, bool broadcast)
347 StackFrameSP frame_sp(GetStackFrameList()->GetFrameAtIndex (frame_idx));
350 GetStackFrameList()->SetSelectedFrame(frame_sp.get());
352 BroadcastSelectedFrameChange(frame_sp->GetStackID());
360 Thread::SetSelectedFrameByIndexNoisily (uint32_t frame_idx, Stream &output_stream)
362 const bool broadcast = true;
363 bool success = SetSelectedFrameByIndex (frame_idx, broadcast);
366 StackFrameSP frame_sp = GetSelectedFrame();
369 bool already_shown = false;
370 SymbolContext frame_sc(frame_sp->GetSymbolContext(eSymbolContextLineEntry));
371 if (GetProcess()->GetTarget().GetDebugger().GetUseExternalEditor() && frame_sc.line_entry.file && frame_sc.line_entry.line != 0)
373 already_shown = Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line);
376 bool show_frame_info = true;
377 bool show_source = !already_shown;
378 return frame_sp->GetStatus (output_stream, show_frame_info, show_source);
388 Thread::GetStopInfo ()
390 if (m_destroy_called)
391 return m_stop_info_sp;
393 ThreadPlanSP plan_sp (GetCompletedPlan());
394 ProcessSP process_sp (GetProcess());
395 const uint32_t stop_id = process_sp ? process_sp->GetStopID() : UINT32_MAX;
396 if (plan_sp && plan_sp->PlanSucceeded())
398 return StopInfo::CreateStopReasonWithPlan (plan_sp, GetReturnValueObject());
402 if ((m_stop_info_stop_id == stop_id) || // Stop info is valid, just return what we have (even if empty)
403 (m_stop_info_sp && m_stop_info_sp->IsValid())) // Stop info is valid, just return what we have
405 return m_stop_info_sp;
409 GetPrivateStopInfo ();
410 return m_stop_info_sp;
416 Thread::GetPrivateStopInfo ()
418 if (m_destroy_called)
419 return m_stop_info_sp;
421 ProcessSP process_sp (GetProcess());
424 const uint32_t process_stop_id = process_sp->GetStopID();
425 if (m_stop_info_stop_id != process_stop_id)
429 if (m_stop_info_sp->IsValid()
430 || IsStillAtLastBreakpointHit()
431 || GetCurrentPlan()->IsVirtualStep())
432 SetStopInfo (m_stop_info_sp);
434 m_stop_info_sp.reset();
439 if (CalculateStopInfo() == false)
440 SetStopInfo (StopInfoSP());
444 return m_stop_info_sp;
449 Thread::GetStopReason()
451 lldb::StopInfoSP stop_info_sp (GetStopInfo ());
453 return stop_info_sp->GetStopReason();
454 return eStopReasonNone;
460 Thread::SetStopInfo (const lldb::StopInfoSP &stop_info_sp)
462 m_stop_info_sp = stop_info_sp;
465 m_stop_info_sp->MakeStopInfoValid();
466 // If we are overriding the ShouldReportStop, do that here:
467 if (m_override_should_notify != eLazyBoolCalculate)
468 m_stop_info_sp->OverrideShouldNotify (m_override_should_notify == eLazyBoolYes);
471 ProcessSP process_sp (GetProcess());
473 m_stop_info_stop_id = process_sp->GetStopID();
475 m_stop_info_stop_id = UINT32_MAX;
476 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
478 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);
482 Thread::SetShouldReportStop (Vote vote)
484 if (vote == eVoteNoOpinion)
488 m_override_should_notify = (vote == eVoteYes ? eLazyBoolYes : eLazyBoolNo);
490 m_stop_info_sp->OverrideShouldNotify (m_override_should_notify == eLazyBoolYes);
495 Thread::SetStopInfoToNothing()
497 // Note, we can't just NULL out the private reason, or the native thread implementation will try to
498 // go calculate it again. For now, just set it to a Unix Signal with an invalid signal number.
499 SetStopInfo (StopInfo::CreateStopReasonWithSignal (*this, LLDB_INVALID_SIGNAL_NUMBER));
503 Thread::ThreadStoppedForAReason (void)
505 return (bool) GetPrivateStopInfo ();
509 Thread::CheckpointThreadState (ThreadStateCheckpoint &saved_state)
511 saved_state.register_backup_sp.reset();
512 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
515 lldb::RegisterCheckpointSP reg_checkpoint_sp(new RegisterCheckpoint(RegisterCheckpoint::Reason::eExpression));
516 if (reg_checkpoint_sp)
518 lldb::RegisterContextSP reg_ctx_sp (frame_sp->GetRegisterContext());
519 if (reg_ctx_sp && reg_ctx_sp->ReadAllRegisterValues (*reg_checkpoint_sp))
520 saved_state.register_backup_sp = reg_checkpoint_sp;
523 if (!saved_state.register_backup_sp)
526 saved_state.stop_info_sp = GetStopInfo();
527 ProcessSP process_sp (GetProcess());
529 saved_state.orig_stop_id = process_sp->GetStopID();
530 saved_state.current_inlined_depth = GetCurrentInlinedDepth();
536 Thread::RestoreRegisterStateFromCheckpoint (ThreadStateCheckpoint &saved_state)
538 if (saved_state.register_backup_sp)
540 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
543 lldb::RegisterContextSP reg_ctx_sp (frame_sp->GetRegisterContext());
546 bool ret = reg_ctx_sp->WriteAllRegisterValues (*saved_state.register_backup_sp);
548 // Clear out all stack frames as our world just changed.
550 reg_ctx_sp->InvalidateIfNeeded(true);
551 if (m_unwinder_ap.get())
552 m_unwinder_ap->Clear();
561 Thread::RestoreThreadStateFromCheckpoint (ThreadStateCheckpoint &saved_state)
563 if (saved_state.stop_info_sp)
564 saved_state.stop_info_sp->MakeStopInfoValid();
565 SetStopInfo(saved_state.stop_info_sp);
566 GetStackFrameList()->SetCurrentInlinedDepth (saved_state.current_inlined_depth);
571 Thread::GetState() const
573 // If any other threads access this we will need a mutex for it
574 Mutex::Locker locker(m_state_mutex);
579 Thread::SetState(StateType state)
581 Mutex::Locker locker(m_state_mutex);
588 ThreadPlan *current_plan = GetCurrentPlan();
590 // FIXME: I may decide to disallow threads with no plans. In which
591 // case this should go to an assert.
596 current_plan->WillStop();
600 Thread::SetupForResume ()
602 if (GetResumeState() != eStateSuspended)
605 // If we're at a breakpoint push the step-over breakpoint plan. Do this before
606 // telling the current plan it will resume, since we might change what the current
609 // StopReason stop_reason = lldb::eStopReasonInvalid;
610 // StopInfoSP stop_info_sp = GetStopInfo();
611 // if (stop_info_sp.get())
612 // stop_reason = stop_info_sp->GetStopReason();
613 // if (stop_reason == lldb::eStopReasonBreakpoint)
614 lldb::RegisterContextSP reg_ctx_sp (GetRegisterContext());
617 BreakpointSiteSP bp_site_sp = GetProcess()->GetBreakpointSiteList().FindByAddress(reg_ctx_sp->GetPC());
620 // Note, don't assume there's a ThreadPlanStepOverBreakpoint, the target may not require anything
621 // special to step over a breakpoint.
623 ThreadPlan *cur_plan = GetCurrentPlan();
625 if (cur_plan->GetKind() != ThreadPlan::eKindStepOverBreakpoint)
627 ThreadPlanStepOverBreakpoint *step_bp_plan = new ThreadPlanStepOverBreakpoint (*this);
630 ThreadPlanSP step_bp_plan_sp;
631 step_bp_plan->SetPrivate (true);
633 if (GetCurrentPlan()->RunState() != eStateStepping)
635 step_bp_plan->SetAutoContinue(true);
637 step_bp_plan_sp.reset (step_bp_plan);
638 QueueThreadPlan (step_bp_plan_sp, false);
647 Thread::ShouldResume (StateType resume_state)
649 // At this point clear the completed plan stack.
650 m_completed_plan_stack.clear();
651 m_discarded_plan_stack.clear();
652 m_override_should_notify = eLazyBoolCalculate;
654 m_temporary_resume_state = resume_state;
656 lldb::ThreadSP backing_thread_sp (GetBackingThread ());
657 if (backing_thread_sp)
658 backing_thread_sp->m_temporary_resume_state = resume_state;
660 // Make sure m_stop_info_sp is valid
661 GetPrivateStopInfo();
663 // This is a little dubious, but we are trying to limit how often we actually fetch stop info from
664 // the target, 'cause that slows down single stepping. So assume that if we got to the point where
665 // we're about to resume, and we haven't yet had to fetch the stop reason, then it doesn't need to know
666 // about the fact that we are resuming...
667 const uint32_t process_stop_id = GetProcess()->GetStopID();
668 if (m_stop_info_stop_id == process_stop_id &&
669 (m_stop_info_sp && m_stop_info_sp->IsValid()))
671 StopInfo *stop_info = GetPrivateStopInfo().get();
673 stop_info->WillResume (resume_state);
676 // Tell all the plans that we are about to resume in case they need to clear any state.
677 // We distinguish between the plan on the top of the stack and the lower
678 // plans in case a plan needs to do any special business before it runs.
680 bool need_to_resume = false;
681 ThreadPlan *plan_ptr = GetCurrentPlan();
684 need_to_resume = plan_ptr->WillResume(resume_state, true);
686 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
688 plan_ptr->WillResume (resume_state, false);
691 // If the WillResume for the plan says we are faking a resume, then it will have set an appropriate stop info.
692 // In that case, don't reset it here.
694 if (need_to_resume && resume_state != eStateSuspended)
696 m_stop_info_sp.reset();
703 // Let Thread subclasses do any special work they need to prior to resuming
704 WillResume (resume_state);
707 return need_to_resume;
713 SetResumeSignal (LLDB_INVALID_SIGNAL_NUMBER);
719 SetState (eStateStopped);
723 Thread::ShouldStop (Event* event_ptr)
725 ThreadPlan *current_plan = GetCurrentPlan();
727 bool should_stop = true;
729 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
731 if (GetResumeState () == eStateSuspended)
734 log->Printf ("Thread::%s for tid = 0x%4.4" PRIx64 " 0x%4.4" PRIx64 ", should_stop = 0 (ignore since thread was suspended)",
741 if (GetTemporaryResumeState () == eStateSuspended)
744 log->Printf ("Thread::%s for tid = 0x%4.4" PRIx64 " 0x%4.4" PRIx64 ", should_stop = 0 (ignore since thread was suspended)",
751 // Based on the current thread plan and process stop info, check if this
752 // thread caused the process to stop. NOTE: this must take place before
753 // the plan is moved from the current plan stack to the completed plan
755 if (ThreadStoppedForAReason() == false)
758 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)",
762 GetRegisterContext() ? GetRegisterContext()->GetPC() : LLDB_INVALID_ADDRESS);
768 log->Printf ("Thread::%s(%p) for tid = 0x%4.4" PRIx64 " 0x%4.4" PRIx64 ", pc = 0x%16.16" PRIx64,
773 GetRegisterContext() ? GetRegisterContext()->GetPC() : LLDB_INVALID_ADDRESS);
774 log->Printf ("^^^^^^^^ Thread::ShouldStop Begin ^^^^^^^^");
778 log->Printf ("Plan stack initial state:\n%s", s.GetData());
781 // The top most plan always gets to do the trace log...
782 current_plan->DoTraceLog ();
784 // First query the stop info's ShouldStopSynchronous. This handles "synchronous" stop reasons, for example the breakpoint
785 // command on internal breakpoints. If a synchronous stop reason says we should not stop, then we don't have to
786 // do any more work on this stop.
787 StopInfoSP private_stop_info (GetPrivateStopInfo());
788 if (private_stop_info && private_stop_info->ShouldStopSynchronous(event_ptr) == false)
791 log->Printf ("StopInfo::ShouldStop async callback says we should not stop, returning ShouldStop of false.");
795 // If we've already been restarted, don't query the plans since the state they would examine is not current.
796 if (Process::ProcessEventData::GetRestartedFromEvent(event_ptr))
799 // Before the plans see the state of the world, calculate the current inlined depth.
800 GetStackFrameList()->CalculateCurrentInlinedDepth();
802 // If the base plan doesn't understand why we stopped, then we have to find a plan that does.
803 // If that plan is still working, then we don't need to do any more work. If the plan that explains
804 // the stop is done, then we should pop all the plans below it, and pop it, and then let the plans above it decide
805 // whether they still need to do more work.
807 bool done_processing_current_plan = false;
809 if (!current_plan->PlanExplainsStop(event_ptr))
811 if (current_plan->TracerExplainsStop())
813 done_processing_current_plan = true;
818 // If the current plan doesn't explain the stop, then find one that
819 // does and let it handle the situation.
820 ThreadPlan *plan_ptr = current_plan;
821 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
823 if (plan_ptr->PlanExplainsStop(event_ptr))
825 should_stop = plan_ptr->ShouldStop (event_ptr);
827 // plan_ptr explains the stop, next check whether plan_ptr is done, if so, then we should take it
828 // and all the plans below it off the stack.
830 if (plan_ptr->MischiefManaged())
832 // We're going to pop the plans up to and including the plan that explains the stop.
833 ThreadPlan *prev_plan_ptr = GetPreviousPlan (plan_ptr);
838 current_plan->WillStop();
841 while ((current_plan = GetCurrentPlan()) != prev_plan_ptr);
842 // Now, if the responsible plan was not "Okay to discard" then we're done,
843 // otherwise we forward this to the next plan in the stack below.
844 if (plan_ptr->IsMasterPlan() && !plan_ptr->OkayToDiscard())
845 done_processing_current_plan = true;
847 done_processing_current_plan = false;
850 done_processing_current_plan = true;
859 if (!done_processing_current_plan)
861 bool over_ride_stop = current_plan->ShouldAutoContinue(event_ptr);
864 log->Printf("Plan %s explains stop, auto-continue %i.", current_plan->GetName(), over_ride_stop);
866 // We're starting from the base plan, so just let it decide;
867 if (PlanIsBasePlan(current_plan))
869 should_stop = current_plan->ShouldStop (event_ptr);
871 log->Printf("Base plan says should stop: %i.", should_stop);
875 // Otherwise, don't let the base plan override what the other plans say to do, since
876 // presumably if there were other plans they would know what to do...
879 if (PlanIsBasePlan(current_plan))
882 should_stop = current_plan->ShouldStop(event_ptr);
884 log->Printf("Plan %s should stop: %d.", current_plan->GetName(), should_stop);
885 if (current_plan->MischiefManaged())
888 current_plan->WillStop();
890 // If a Master Plan wants to stop, and wants to stick on the stack, we let it.
891 // Otherwise, see if the plan's parent wants to stop.
893 if (should_stop && current_plan->IsMasterPlan() && !current_plan->OkayToDiscard())
903 current_plan = GetCurrentPlan();
904 if (current_plan == NULL)
920 // One other potential problem is that we set up a master plan, then stop in before it is complete - for instance
921 // by hitting a breakpoint during a step-over - then do some step/finish/etc operations that wind up
922 // past the end point condition of the initial plan. We don't want to strand the original plan on the stack,
923 // This code clears stale plans off the stack.
927 ThreadPlan *plan_ptr = GetCurrentPlan();
928 while (!PlanIsBasePlan(plan_ptr))
930 bool stale = plan_ptr->IsPlanStale ();
931 ThreadPlan *examined_plan = plan_ptr;
932 plan_ptr = GetPreviousPlan (examined_plan);
937 log->Printf("Plan %s being discarded in cleanup, it says it is already done.", examined_plan->GetName());
938 DiscardThreadPlansUpToPlan(examined_plan);
950 log->Printf ("Plan stack final state:\n%s", s.GetData());
951 log->Printf ("vvvvvvvv Thread::ShouldStop End (returning %i) vvvvvvvv", should_stop);
957 Thread::ShouldReportStop (Event* event_ptr)
959 StateType thread_state = GetResumeState ();
960 StateType temp_thread_state = GetTemporaryResumeState();
962 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
964 if (thread_state == eStateSuspended || thread_state == eStateInvalid)
967 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4" PRIx64 ": returning vote %i (state was suspended or invalid)", GetID(), eVoteNoOpinion);
968 return eVoteNoOpinion;
971 if (temp_thread_state == eStateSuspended || temp_thread_state == eStateInvalid)
974 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4" PRIx64 ": returning vote %i (temporary state was suspended or invalid)", GetID(), eVoteNoOpinion);
975 return eVoteNoOpinion;
978 if (!ThreadStoppedForAReason())
981 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4" PRIx64 ": returning vote %i (thread didn't stop for a reason.)", GetID(), eVoteNoOpinion);
982 return eVoteNoOpinion;
985 if (m_completed_plan_stack.size() > 0)
987 // Don't use GetCompletedPlan here, since that suppresses private plans.
989 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4" PRIx64 ": returning vote for complete stack's back plan", GetID());
990 return m_completed_plan_stack.back()->ShouldReportStop (event_ptr);
994 Vote thread_vote = eVoteNoOpinion;
995 ThreadPlan *plan_ptr = GetCurrentPlan();
998 if (plan_ptr->PlanExplainsStop(event_ptr))
1000 thread_vote = plan_ptr->ShouldReportStop(event_ptr);
1003 if (PlanIsBasePlan(plan_ptr))
1006 plan_ptr = GetPreviousPlan(plan_ptr);
1009 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4" PRIx64 ": returning vote %i for current plan", GetID(), thread_vote);
1016 Thread::ShouldReportRun (Event* event_ptr)
1018 StateType thread_state = GetResumeState ();
1020 if (thread_state == eStateSuspended
1021 || thread_state == eStateInvalid)
1023 return eVoteNoOpinion;
1026 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
1027 if (m_completed_plan_stack.size() > 0)
1029 // Don't use GetCompletedPlan here, since that suppresses private plans.
1031 log->Printf ("Current Plan for thread %d(%p) (0x%4.4" PRIx64 ", %s): %s being asked whether we should report run.",
1035 StateAsCString(GetTemporaryResumeState()),
1036 m_completed_plan_stack.back()->GetName());
1038 return m_completed_plan_stack.back()->ShouldReportRun (event_ptr);
1043 log->Printf ("Current Plan for thread %d(%p) (0x%4.4" PRIx64 ", %s): %s being asked whether we should report run.",
1047 StateAsCString(GetTemporaryResumeState()),
1048 GetCurrentPlan()->GetName());
1050 return GetCurrentPlan()->ShouldReportRun (event_ptr);
1055 Thread::MatchesSpec (const ThreadSpec *spec)
1060 return spec->ThreadPassesBasicTests(*this);
1064 Thread::PushPlan (ThreadPlanSP &thread_plan_sp)
1068 // If the thread plan doesn't already have a tracer, give it its parent's tracer:
1069 if (!thread_plan_sp->GetThreadPlanTracer())
1070 thread_plan_sp->SetThreadPlanTracer(m_plan_stack.back()->GetThreadPlanTracer());
1071 m_plan_stack.push_back (thread_plan_sp);
1073 thread_plan_sp->DidPush();
1075 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
1079 thread_plan_sp->GetDescription (&s, lldb::eDescriptionLevelFull);
1080 log->Printf("Thread::PushPlan(0x%p): \"%s\", tid = 0x%4.4" PRIx64 ".",
1083 thread_plan_sp->GetThread().GetID());
1091 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
1093 if (m_plan_stack.size() <= 1)
1097 ThreadPlanSP &plan = m_plan_stack.back();
1100 log->Printf("Popping plan: \"%s\", tid = 0x%4.4" PRIx64 ".", plan->GetName(), plan->GetThread().GetID());
1102 m_completed_plan_stack.push_back (plan);
1104 m_plan_stack.pop_back();
1109 Thread::DiscardPlan ()
1111 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
1112 if (m_plan_stack.size() > 1)
1114 ThreadPlanSP &plan = m_plan_stack.back();
1116 log->Printf("Discarding plan: \"%s\", tid = 0x%4.4" PRIx64 ".", plan->GetName(), plan->GetThread().GetID());
1118 m_discarded_plan_stack.push_back (plan);
1120 m_plan_stack.pop_back();
1125 Thread::GetCurrentPlan ()
1127 // There will always be at least the base plan. If somebody is mucking with a
1128 // thread with an empty plan stack, we should assert right away.
1129 if (m_plan_stack.empty())
1131 return m_plan_stack.back().get();
1135 Thread::GetCompletedPlan ()
1137 ThreadPlanSP empty_plan_sp;
1138 if (!m_completed_plan_stack.empty())
1140 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1142 ThreadPlanSP completed_plan_sp;
1143 completed_plan_sp = m_completed_plan_stack[i];
1144 if (!completed_plan_sp->GetPrivate ())
1145 return completed_plan_sp;
1148 return empty_plan_sp;
1152 Thread::GetReturnValueObject ()
1154 if (!m_completed_plan_stack.empty())
1156 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1158 ValueObjectSP return_valobj_sp;
1159 return_valobj_sp = m_completed_plan_stack[i]->GetReturnValueObject();
1160 if (return_valobj_sp)
1161 return return_valobj_sp;
1164 return ValueObjectSP();
1168 Thread::IsThreadPlanDone (ThreadPlan *plan)
1170 if (!m_completed_plan_stack.empty())
1172 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1174 if (m_completed_plan_stack[i].get() == plan)
1182 Thread::WasThreadPlanDiscarded (ThreadPlan *plan)
1184 if (!m_discarded_plan_stack.empty())
1186 for (int i = m_discarded_plan_stack.size() - 1; i >= 0; i--)
1188 if (m_discarded_plan_stack[i].get() == plan)
1196 Thread::GetPreviousPlan (ThreadPlan *current_plan)
1198 if (current_plan == NULL)
1201 int stack_size = m_completed_plan_stack.size();
1202 for (int i = stack_size - 1; i > 0; i--)
1204 if (current_plan == m_completed_plan_stack[i].get())
1205 return m_completed_plan_stack[i-1].get();
1208 if (stack_size > 0 && m_completed_plan_stack[0].get() == current_plan)
1210 if (m_plan_stack.size() > 0)
1211 return m_plan_stack.back().get();
1216 stack_size = m_plan_stack.size();
1217 for (int i = stack_size - 1; i > 0; i--)
1219 if (current_plan == m_plan_stack[i].get())
1220 return m_plan_stack[i-1].get();
1226 Thread::QueueThreadPlan (ThreadPlanSP &thread_plan_sp, bool abort_other_plans)
1228 if (abort_other_plans)
1229 DiscardThreadPlans(true);
1231 PushPlan (thread_plan_sp);
1236 Thread::EnableTracer (bool value, bool single_stepping)
1238 int stack_size = m_plan_stack.size();
1239 for (int i = 0; i < stack_size; i++)
1241 if (m_plan_stack[i]->GetThreadPlanTracer())
1243 m_plan_stack[i]->GetThreadPlanTracer()->EnableTracing(value);
1244 m_plan_stack[i]->GetThreadPlanTracer()->EnableSingleStep(single_stepping);
1250 Thread::SetTracer (lldb::ThreadPlanTracerSP &tracer_sp)
1252 int stack_size = m_plan_stack.size();
1253 for (int i = 0; i < stack_size; i++)
1254 m_plan_stack[i]->SetThreadPlanTracer(tracer_sp);
1258 Thread::DiscardThreadPlansUpToPlan (lldb::ThreadPlanSP &up_to_plan_sp)
1260 DiscardThreadPlansUpToPlan (up_to_plan_sp.get());
1264 Thread::DiscardThreadPlansUpToPlan (ThreadPlan *up_to_plan_ptr)
1266 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
1269 log->Printf("Discarding thread plans for thread tid = 0x%4.4" PRIx64 ", up to %p", GetID(), up_to_plan_ptr);
1272 int stack_size = m_plan_stack.size();
1274 // If the input plan is NULL, discard all plans. Otherwise make sure this plan is in the
1275 // stack, and if so discard up to and including it.
1277 if (up_to_plan_ptr == NULL)
1279 for (int i = stack_size - 1; i > 0; i--)
1284 bool found_it = false;
1285 for (int i = stack_size - 1; i > 0; i--)
1287 if (m_plan_stack[i].get() == up_to_plan_ptr)
1292 bool last_one = false;
1293 for (int i = stack_size - 1; i > 0 && !last_one ; i--)
1295 if (GetCurrentPlan() == up_to_plan_ptr)
1305 Thread::DiscardThreadPlans(bool force)
1307 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
1310 log->Printf("Discarding thread plans for thread (tid = 0x%4.4" PRIx64 ", force %d)", GetID(), force);
1315 int stack_size = m_plan_stack.size();
1316 for (int i = stack_size - 1; i > 0; i--)
1326 int master_plan_idx;
1327 bool discard = true;
1329 // Find the first master plan, see if it wants discarding, and if yes discard up to it.
1330 for (master_plan_idx = m_plan_stack.size() - 1; master_plan_idx >= 0; master_plan_idx--)
1332 if (m_plan_stack[master_plan_idx]->IsMasterPlan())
1334 discard = m_plan_stack[master_plan_idx]->OkayToDiscard();
1341 // First pop all the dependent plans:
1342 for (int i = m_plan_stack.size() - 1; i > master_plan_idx; i--)
1345 // FIXME: Do we need a finalize here, or is the rule that "PrepareForStop"
1346 // for the plan leaves it in a state that it is safe to pop the plan
1347 // with no more notice?
1351 // Now discard the master plan itself.
1352 // The bottom-most plan never gets discarded. "OkayToDiscard" for it means
1353 // discard it's dependent plans, but not it...
1354 if (master_plan_idx > 0)
1361 // If the master plan doesn't want to get discarded, then we're done.
1369 Thread::PlanIsBasePlan (ThreadPlan *plan_ptr)
1371 if (plan_ptr->IsBasePlan())
1373 else if (m_plan_stack.size() == 0)
1376 return m_plan_stack[0].get() == plan_ptr;
1380 Thread::UnwindInnermostExpression()
1383 int stack_size = m_plan_stack.size();
1385 // If the input plan is NULL, discard all plans. Otherwise make sure this plan is in the
1386 // stack, and if so discard up to and including it.
1388 for (int i = stack_size - 1; i > 0; i--)
1390 if (m_plan_stack[i]->GetKind() == ThreadPlan::eKindCallFunction)
1392 DiscardThreadPlansUpToPlan(m_plan_stack[i].get());
1396 error.SetErrorString("No expressions currently active on this thread");
1402 Thread::QueueFundamentalPlan (bool abort_other_plans)
1404 ThreadPlanSP thread_plan_sp (new ThreadPlanBase(*this));
1405 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1406 return thread_plan_sp;
1410 Thread::QueueThreadPlanForStepSingleInstruction
1413 bool abort_other_plans,
1414 bool stop_other_threads
1417 ThreadPlanSP thread_plan_sp (new ThreadPlanStepInstruction (*this, step_over, stop_other_threads, eVoteNoOpinion, eVoteNoOpinion));
1418 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1419 return thread_plan_sp;
1423 Thread::QueueThreadPlanForStepOverRange
1425 bool abort_other_plans,
1426 const AddressRange &range,
1427 const SymbolContext &addr_context,
1428 lldb::RunMode stop_other_threads
1431 ThreadPlanSP thread_plan_sp;
1432 thread_plan_sp.reset (new ThreadPlanStepOverRange (*this, range, addr_context, stop_other_threads));
1434 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1435 return thread_plan_sp;
1439 Thread::QueueThreadPlanForStepInRange
1441 bool abort_other_plans,
1442 const AddressRange &range,
1443 const SymbolContext &addr_context,
1444 const char *step_in_target,
1445 lldb::RunMode stop_other_threads,
1446 bool avoid_code_without_debug_info
1449 ThreadPlanSP thread_plan_sp;
1450 ThreadPlanStepInRange *plan = new ThreadPlanStepInRange (*this, range, addr_context, stop_other_threads);
1451 if (avoid_code_without_debug_info)
1452 plan->GetFlags().Set (ThreadPlanShouldStopHere::eAvoidNoDebug);
1454 plan->GetFlags().Clear (ThreadPlanShouldStopHere::eAvoidNoDebug);
1456 plan->SetStepInTarget(step_in_target);
1457 thread_plan_sp.reset (plan);
1459 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1460 return thread_plan_sp;
1465 Thread::QueueThreadPlanForStepOut
1467 bool abort_other_plans,
1468 SymbolContext *addr_context,
1470 bool stop_other_threads,
1476 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOut (*this,
1484 if (thread_plan_sp->ValidatePlan(NULL))
1486 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1487 return thread_plan_sp;
1491 return ThreadPlanSP();
1496 Thread::QueueThreadPlanForStepThrough (StackID &return_stack_id, bool abort_other_plans, bool stop_other_threads)
1498 ThreadPlanSP thread_plan_sp(new ThreadPlanStepThrough (*this, return_stack_id, stop_other_threads));
1499 if (!thread_plan_sp || !thread_plan_sp->ValidatePlan (NULL))
1500 return ThreadPlanSP();
1502 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1503 return thread_plan_sp;
1507 Thread::QueueThreadPlanForRunToAddress (bool abort_other_plans,
1508 Address &target_addr,
1509 bool stop_other_threads)
1511 ThreadPlanSP thread_plan_sp (new ThreadPlanRunToAddress (*this, target_addr, stop_other_threads));
1512 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1513 return thread_plan_sp;
1517 Thread::QueueThreadPlanForStepUntil (bool abort_other_plans,
1518 lldb::addr_t *address_list,
1519 size_t num_addresses,
1520 bool stop_other_threads,
1523 ThreadPlanSP thread_plan_sp (new ThreadPlanStepUntil (*this, address_list, num_addresses, stop_other_threads, frame_idx));
1524 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1525 return thread_plan_sp;
1530 Thread::GetIndexID () const
1536 Thread::DumpThreadPlans (lldb_private::Stream *s) const
1538 uint32_t stack_size = m_plan_stack.size();
1541 s->Printf ("Plan Stack for thread #%u: tid = 0x%4.4" PRIx64 ", stack_size = %d\n", GetIndexID(), GetID(), stack_size);
1542 for (i = stack_size - 1; i >= 0; i--)
1546 s->Printf ("Element %d: ", i);
1547 m_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1552 stack_size = m_completed_plan_stack.size();
1556 s->Printf ("Completed Plan Stack: %d elements.\n", stack_size);
1557 for (i = stack_size - 1; i >= 0; i--)
1561 s->Printf ("Element %d: ", i);
1562 m_completed_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1568 stack_size = m_discarded_plan_stack.size();
1572 s->Printf ("Discarded Plan Stack: %d elements.\n", stack_size);
1573 for (i = stack_size - 1; i >= 0; i--)
1577 s->Printf ("Element %d: ", i);
1578 m_discarded_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1587 Thread::CalculateTarget ()
1590 ProcessSP process_sp(GetProcess());
1592 target_sp = process_sp->CalculateTarget();
1598 Thread::CalculateProcess ()
1600 return GetProcess();
1604 Thread::CalculateThread ()
1606 return shared_from_this();
1610 Thread::CalculateStackFrame ()
1612 return StackFrameSP();
1616 Thread::CalculateExecutionContext (ExecutionContext &exe_ctx)
1618 exe_ctx.SetContext (shared_from_this());
1623 Thread::GetStackFrameList ()
1625 StackFrameListSP frame_list_sp;
1626 Mutex::Locker locker(m_frame_mutex);
1627 if (m_curr_frames_sp)
1629 frame_list_sp = m_curr_frames_sp;
1633 frame_list_sp.reset(new StackFrameList (*this, m_prev_frames_sp, true));
1634 m_curr_frames_sp = frame_list_sp;
1636 return frame_list_sp;
1640 Thread::ClearStackFrames ()
1642 Mutex::Locker locker(m_frame_mutex);
1644 Unwind *unwinder = GetUnwinder ();
1648 // Only store away the old "reference" StackFrameList if we got all its frames:
1649 // FIXME: At some point we can try to splice in the frames we have fetched into
1650 // the new frame as we make it, but let's not try that now.
1651 if (m_curr_frames_sp && m_curr_frames_sp->GetAllFramesFetched())
1652 m_prev_frames_sp.swap (m_curr_frames_sp);
1653 m_curr_frames_sp.reset();
1657 Thread::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx)
1659 return GetStackFrameList()->GetFrameWithConcreteFrameIndex (unwind_idx);
1664 Thread::ReturnFromFrameWithIndex (uint32_t frame_idx, lldb::ValueObjectSP return_value_sp, bool broadcast)
1666 StackFrameSP frame_sp = GetStackFrameAtIndex (frame_idx);
1671 return_error.SetErrorStringWithFormat("Could not find frame with index %d in thread 0x%" PRIx64 ".", frame_idx, GetID());
1674 return ReturnFromFrame(frame_sp, return_value_sp, broadcast);
1678 Thread::ReturnFromFrame (lldb::StackFrameSP frame_sp, lldb::ValueObjectSP return_value_sp, bool broadcast)
1684 return_error.SetErrorString("Can't return to a null frame.");
1685 return return_error;
1688 Thread *thread = frame_sp->GetThread().get();
1689 uint32_t older_frame_idx = frame_sp->GetFrameIndex() + 1;
1690 StackFrameSP older_frame_sp = thread->GetStackFrameAtIndex(older_frame_idx);
1691 if (!older_frame_sp)
1693 return_error.SetErrorString("No older frame to return to.");
1694 return return_error;
1697 if (return_value_sp)
1699 lldb::ABISP abi = thread->GetProcess()->GetABI();
1702 return_error.SetErrorString("Could not find ABI to set return value.");
1703 return return_error;
1705 SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextFunction);
1707 // FIXME: ValueObject::Cast doesn't currently work correctly, at least not for scalars.
1708 // Turn that back on when that works.
1709 if (0 && sc.function != NULL)
1711 Type *function_type = sc.function->GetType();
1714 ClangASTType return_type = sc.function->GetClangType().GetFunctionReturnType();
1718 return_type.DumpTypeDescription(&s);
1719 ValueObjectSP cast_value_sp = return_value_sp->Cast(return_type);
1722 cast_value_sp->SetFormat(eFormatHex);
1723 return_value_sp = cast_value_sp;
1729 return_error = abi->SetReturnValueObject(older_frame_sp, return_value_sp);
1730 if (!return_error.Success())
1731 return return_error;
1734 // Now write the return registers for the chosen frame:
1735 // Note, we can't use ReadAllRegisterValues->WriteAllRegisterValues, since the read & write
1738 StackFrameSP youngest_frame_sp = thread->GetStackFrameAtIndex(0);
1739 if (youngest_frame_sp)
1741 lldb::RegisterContextSP reg_ctx_sp (youngest_frame_sp->GetRegisterContext());
1744 bool copy_success = reg_ctx_sp->CopyFromRegisterContext(older_frame_sp->GetRegisterContext());
1747 thread->DiscardThreadPlans(true);
1748 thread->ClearStackFrames();
1749 if (broadcast && EventTypeHasListeners(eBroadcastBitStackChanged))
1750 BroadcastEvent(eBroadcastBitStackChanged, new ThreadEventData (this->shared_from_this()));
1754 return_error.SetErrorString("Could not reset register values.");
1759 return_error.SetErrorString("Frame has no register context.");
1764 return_error.SetErrorString("Returned past top frame.");
1766 return return_error;
1769 static void DumpAddressList (Stream &s, const std::vector<Address> &list, ExecutionContextScope *exe_scope)
1771 for (size_t n=0;n<list.size();n++)
1774 list[n].Dump (&s, exe_scope, Address::DumpStyleResolvedDescription, Address::DumpStyleSectionNameOffset);
1780 Thread::JumpToLine (const FileSpec &file, uint32_t line, bool can_leave_function, std::string *warnings)
1782 ExecutionContext exe_ctx (GetStackFrameAtIndex(0));
1783 Target *target = exe_ctx.GetTargetPtr();
1784 TargetSP target_sp = exe_ctx.GetTargetSP();
1785 RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
1786 StackFrame *frame = exe_ctx.GetFramePtr();
1787 const SymbolContext &sc = frame->GetSymbolContext(eSymbolContextFunction);
1789 // Find candidate locations.
1790 std::vector<Address> candidates, within_function, outside_function;
1791 target->GetImages().FindAddressesForLine (target_sp, file, line, sc.function, within_function, outside_function);
1793 // If possible, we try and stay within the current function.
1794 // Within a function, we accept multiple locations (optimized code may do this,
1795 // there's no solution here so we do the best we can).
1796 // However if we're trying to leave the function, we don't know how to pick the
1797 // right location, so if there's more than one then we bail.
1798 if (!within_function.empty())
1799 candidates = within_function;
1800 else if (outside_function.size() == 1 && can_leave_function)
1801 candidates = outside_function;
1803 // Check if we got anything.
1804 if (candidates.empty())
1806 if (outside_function.empty())
1808 return Error("Cannot locate an address for %s:%i.",
1809 file.GetFilename().AsCString(), line);
1811 else if (outside_function.size() == 1)
1813 return Error("%s:%i is outside the current function.",
1814 file.GetFilename().AsCString(), line);
1819 DumpAddressList(sstr, outside_function, target);
1820 return Error("%s:%i has multiple candidate locations:\n%s",
1821 file.GetFilename().AsCString(), line, sstr.GetString().c_str());
1825 // Accept the first location, warn about any others.
1826 Address dest = candidates[0];
1827 if (warnings && candidates.size() > 1)
1830 sstr.Printf("%s:%i appears multiple times in this function, selecting the first location:\n",
1831 file.GetFilename().AsCString(), line);
1832 DumpAddressList(sstr, candidates, target);
1833 *warnings = sstr.GetString();
1836 if (!reg_ctx->SetPC (dest))
1837 return Error("Cannot change PC to target address.");
1843 Thread::DumpUsingSettingsFormat (Stream &strm, uint32_t frame_idx)
1845 ExecutionContext exe_ctx (shared_from_this());
1846 Process *process = exe_ctx.GetProcessPtr();
1847 if (process == NULL)
1850 StackFrameSP frame_sp;
1851 SymbolContext frame_sc;
1852 if (frame_idx != LLDB_INVALID_INDEX32)
1854 frame_sp = GetStackFrameAtIndex (frame_idx);
1857 exe_ctx.SetFrameSP(frame_sp);
1858 frame_sc = frame_sp->GetSymbolContext(eSymbolContextEverything);
1862 const char *thread_format = exe_ctx.GetTargetRef().GetDebugger().GetThreadFormat();
1863 assert (thread_format);
1864 Debugger::FormatPrompt (thread_format,
1865 frame_sp ? &frame_sc : NULL,
1872 Thread::SettingsInitialize ()
1877 Thread::SettingsTerminate ()
1882 Thread::GetThreadPointer ()
1884 return LLDB_INVALID_ADDRESS;
1888 Thread::GetThreadLocalData (const ModuleSP module)
1890 // The default implementation is to ask the dynamic loader for it.
1891 // This can be overridden for specific platforms.
1892 DynamicLoader *loader = GetProcess()->GetDynamicLoader();
1894 return loader->GetThreadLocalData (module, shared_from_this());
1896 return LLDB_INVALID_ADDRESS;
1900 Thread::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr)
1902 return GetStackFrameList()->GetStackFrameSPForStackFramePtr (stack_frame_ptr);
1906 Thread::StopReasonAsCString (lldb::StopReason reason)
1910 case eStopReasonInvalid: return "invalid";
1911 case eStopReasonNone: return "none";
1912 case eStopReasonTrace: return "trace";
1913 case eStopReasonBreakpoint: return "breakpoint";
1914 case eStopReasonWatchpoint: return "watchpoint";
1915 case eStopReasonSignal: return "signal";
1916 case eStopReasonException: return "exception";
1917 case eStopReasonExec: return "exec";
1918 case eStopReasonPlanComplete: return "plan complete";
1919 case eStopReasonThreadExiting: return "thread exiting";
1923 static char unknown_state_string[64];
1924 snprintf(unknown_state_string, sizeof (unknown_state_string), "StopReason = %i", reason);
1925 return unknown_state_string;
1929 Thread::RunModeAsCString (lldb::RunMode mode)
1933 case eOnlyThisThread: return "only this thread";
1934 case eAllThreads: return "all threads";
1935 case eOnlyDuringStepping: return "only during stepping";
1938 static char unknown_state_string[64];
1939 snprintf(unknown_state_string, sizeof (unknown_state_string), "RunMode = %i", mode);
1940 return unknown_state_string;
1944 Thread::GetStatus (Stream &strm, uint32_t start_frame, uint32_t num_frames, uint32_t num_frames_with_source)
1946 ExecutionContext exe_ctx (shared_from_this());
1947 Target *target = exe_ctx.GetTargetPtr();
1948 Process *process = exe_ctx.GetProcessPtr();
1949 size_t num_frames_shown = 0;
1951 bool is_selected = false;
1954 if (process->GetThreadList().GetSelectedThread().get() == this)
1957 strm.Printf("%c ", is_selected ? '*' : ' ');
1958 if (target && target->GetDebugger().GetUseExternalEditor())
1960 StackFrameSP frame_sp = GetStackFrameAtIndex(start_frame);
1963 SymbolContext frame_sc(frame_sp->GetSymbolContext (eSymbolContextLineEntry));
1964 if (frame_sc.line_entry.line != 0 && frame_sc.line_entry.file)
1966 Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line);
1971 DumpUsingSettingsFormat (strm, start_frame);
1977 const bool show_frame_info = true;
1979 const char *selected_frame_marker = NULL;
1980 if (num_frames == 1 || (GetID() != GetProcess()->GetThreadList().GetSelectedThread()->GetID()))
1983 selected_frame_marker = "* ";
1985 num_frames_shown = GetStackFrameList ()->GetStatus (strm,
1989 num_frames_with_source,
1990 selected_frame_marker);
1991 if (num_frames == 1)
1995 return num_frames_shown;
1999 Thread::GetStackFrameStatus (Stream& strm,
2000 uint32_t first_frame,
2001 uint32_t num_frames,
2002 bool show_frame_info,
2003 uint32_t num_frames_with_source)
2005 return GetStackFrameList()->GetStatus (strm,
2009 num_frames_with_source);
2013 Thread::GetUnwinder ()
2015 if (m_unwinder_ap.get() == NULL)
2017 const ArchSpec target_arch (CalculateTarget()->GetArchitecture ());
2018 const llvm::Triple::ArchType machine = target_arch.GetMachine();
2021 case llvm::Triple::x86_64:
2022 case llvm::Triple::x86:
2023 case llvm::Triple::arm:
2024 case llvm::Triple::thumb:
2025 case llvm::Triple::mips64:
2026 case llvm::Triple::hexagon:
2027 m_unwinder_ap.reset (new UnwindLLDB (*this));
2031 if (target_arch.GetTriple().getVendor() == llvm::Triple::Apple)
2032 m_unwinder_ap.reset (new UnwindMacOSXFrameBackchain (*this));
2036 return m_unwinder_ap.get();
2043 ClearStackFrames ();
2044 m_reg_context_sp.reset();
2048 Thread::IsStillAtLastBreakpointHit ()
2050 // If we are currently stopped at a breakpoint, always return that stopinfo and don't reset it.
2051 // This allows threads to maintain their breakpoint stopinfo, such as when thread-stepping in
2052 // multithreaded programs.
2053 if (m_stop_info_sp) {
2054 StopReason stop_reason = m_stop_info_sp->GetStopReason();
2055 if (stop_reason == lldb::eStopReasonBreakpoint) {
2056 uint64_t value = m_stop_info_sp->GetValue();
2057 lldb::RegisterContextSP reg_ctx_sp (GetRegisterContext());
2060 lldb::addr_t pc = reg_ctx_sp->GetPC();
2061 BreakpointSiteSP bp_site_sp = GetProcess()->GetBreakpointSiteList().FindByAddress(pc);
2062 if (bp_site_sp && value == bp_site_sp->GetID())
2072 Thread::StepIn (bool source_step,
2073 bool avoid_code_without_debug_info)
2077 Process *process = GetProcess().get();
2078 if (StateIsStoppedState (process->GetState(), true))
2080 StackFrameSP frame_sp = GetStackFrameAtIndex (0);
2081 ThreadPlanSP new_plan_sp;
2082 const lldb::RunMode run_mode = eOnlyThisThread;
2083 const bool abort_other_plans = false;
2085 if (source_step && frame_sp && frame_sp->HasDebugInformation ())
2087 SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
2088 new_plan_sp = QueueThreadPlanForStepInRange (abort_other_plans,
2089 sc.line_entry.range,
2093 avoid_code_without_debug_info);
2097 new_plan_sp = QueueThreadPlanForStepSingleInstruction (false,
2102 new_plan_sp->SetIsMasterPlan(true);
2103 new_plan_sp->SetOkayToDiscard(false);
2105 // Why do we need to set the current thread by ID here???
2106 process->GetThreadList().SetSelectedThreadByID (GetID());
2107 error = process->Resume();
2111 error.SetErrorString("process not stopped");
2117 Thread::StepOver (bool source_step)
2121 Process *process = GetProcess().get();
2122 if (StateIsStoppedState (process->GetState(), true))
2124 StackFrameSP frame_sp = GetStackFrameAtIndex (0);
2125 ThreadPlanSP new_plan_sp;
2127 const lldb::RunMode run_mode = eOnlyThisThread;
2128 const bool abort_other_plans = false;
2130 if (source_step && frame_sp && frame_sp->HasDebugInformation ())
2132 SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
2133 new_plan_sp = QueueThreadPlanForStepOverRange (abort_other_plans,
2134 sc.line_entry.range,
2140 new_plan_sp = QueueThreadPlanForStepSingleInstruction (true,
2145 new_plan_sp->SetIsMasterPlan(true);
2146 new_plan_sp->SetOkayToDiscard(false);
2148 // Why do we need to set the current thread by ID here???
2149 process->GetThreadList().SetSelectedThreadByID (GetID());
2150 error = process->Resume();
2154 error.SetErrorString("process not stopped");
2163 Process *process = GetProcess().get();
2164 if (StateIsStoppedState (process->GetState(), true))
2166 const bool first_instruction = false;
2167 const bool stop_other_threads = false;
2168 const bool abort_other_plans = false;
2170 ThreadPlanSP new_plan_sp(QueueThreadPlanForStepOut (abort_other_plans,
2178 new_plan_sp->SetIsMasterPlan(true);
2179 new_plan_sp->SetOkayToDiscard(false);
2181 // Why do we need to set the current thread by ID here???
2182 process->GetThreadList().SetSelectedThreadByID (GetID());
2183 error = process->Resume();
2187 error.SetErrorString("process not stopped");