1 //===-- Thread.cpp ----------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "lldb/lldb-private-log.h"
11 #include "lldb/Breakpoint/BreakpointLocation.h"
12 #include "lldb/Core/Debugger.h"
13 #include "lldb/Core/Log.h"
14 #include "lldb/Core/Stream.h"
15 #include "lldb/Core/StreamString.h"
16 #include "lldb/Core/RegularExpression.h"
17 #include "lldb/Host/Host.h"
18 #include "lldb/Target/DynamicLoader.h"
19 #include "lldb/Target/ExecutionContext.h"
20 #include "lldb/Target/ObjCLanguageRuntime.h"
21 #include "lldb/Target/Process.h"
22 #include "lldb/Target/RegisterContext.h"
23 #include "lldb/Target/StopInfo.h"
24 #include "lldb/Target/Target.h"
25 #include "lldb/Target/Thread.h"
26 #include "lldb/Target/ThreadPlan.h"
27 #include "lldb/Target/ThreadPlanCallFunction.h"
28 #include "lldb/Target/ThreadPlanBase.h"
29 #include "lldb/Target/ThreadPlanStepInstruction.h"
30 #include "lldb/Target/ThreadPlanStepOut.h"
31 #include "lldb/Target/ThreadPlanStepOverBreakpoint.h"
32 #include "lldb/Target/ThreadPlanStepThrough.h"
33 #include "lldb/Target/ThreadPlanStepInRange.h"
34 #include "lldb/Target/ThreadPlanStepOverRange.h"
35 #include "lldb/Target/ThreadPlanRunToAddress.h"
36 #include "lldb/Target/ThreadPlanStepUntil.h"
37 #include "lldb/Target/ThreadSpec.h"
38 #include "lldb/Target/Unwind.h"
39 #include "Plugins/Process/Utility/UnwindLLDB.h"
40 #include "UnwindMacOSXFrameBackchain.h"
41 
42 
43 using namespace lldb;
44 using namespace lldb_private;
45 
46 
47 const ThreadPropertiesSP &
48 Thread::GetGlobalProperties()
49 {
50     static ThreadPropertiesSP g_settings_sp;
51     if (!g_settings_sp)
52         g_settings_sp.reset (new ThreadProperties (true));
53     return g_settings_sp;
54 }
55 
56 static PropertyDefinition
57 g_properties[] =
58 {
59     { "step-avoid-regexp",  OptionValue::eTypeRegex  , true , REG_EXTENDED, "^std::", NULL, "A regular expression defining functions step-in won't stop in." },
60     { "trace-thread",       OptionValue::eTypeBoolean, false, false, NULL, NULL, "If true, this thread will single-step and log execution." },
61     {  NULL               , OptionValue::eTypeInvalid, false, 0    , NULL, NULL, NULL  }
62 };
63 
64 enum {
65     ePropertyStepAvoidRegex,
66     ePropertyEnableThreadTrace
67 };
68 
69 
70 class ThreadOptionValueProperties : public OptionValueProperties
71 {
72 public:
73     ThreadOptionValueProperties (const ConstString &name) :
74         OptionValueProperties (name)
75     {
76     }
77 
78     // This constructor is used when creating ThreadOptionValueProperties when it
79     // is part of a new lldb_private::Thread instance. It will copy all current
80     // global property values as needed
81     ThreadOptionValueProperties (ThreadProperties *global_properties) :
82         OptionValueProperties(*global_properties->GetValueProperties())
83     {
84     }
85 
86     virtual const Property *
87     GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
88     {
89         // When gettings the value for a key from the thread options, we will always
90         // try and grab the setting from the current thread if there is one. Else we just
91         // use the one from this instance.
92         if (exe_ctx)
93         {
94             Thread *thread = exe_ctx->GetThreadPtr();
95             if (thread)
96             {
97                 ThreadOptionValueProperties *instance_properties = static_cast<ThreadOptionValueProperties *>(thread->GetValueProperties().get());
98                 if (this != instance_properties)
99                     return instance_properties->ProtectedGetPropertyAtIndex (idx);
100             }
101         }
102         return ProtectedGetPropertyAtIndex (idx);
103     }
104 };
105 
106 
107 
108 ThreadProperties::ThreadProperties (bool is_global) :
109     Properties ()
110 {
111     if (is_global)
112     {
113         m_collection_sp.reset (new ThreadOptionValueProperties(ConstString("thread")));
114         m_collection_sp->Initialize(g_properties);
115     }
116     else
117         m_collection_sp.reset (new ThreadOptionValueProperties(Thread::GetGlobalProperties().get()));
118 }
119 
120 ThreadProperties::~ThreadProperties()
121 {
122 }
123 
124 const RegularExpression *
125 ThreadProperties::GetSymbolsToAvoidRegexp()
126 {
127     const uint32_t idx = ePropertyStepAvoidRegex;
128     return m_collection_sp->GetPropertyAtIndexAsOptionValueRegex (NULL, idx);
129 }
130 
131 bool
132 ThreadProperties::GetTraceEnabledState() const
133 {
134     const uint32_t idx = ePropertyEnableThreadTrace;
135     return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
136 }
137 
138 
139 Thread::Thread (const ProcessSP &process_sp, lldb::tid_t tid) :
140     ThreadProperties (false),
141     UserID (tid),
142     m_process_wp (process_sp),
143     m_actual_stop_info_sp (),
144     m_index_id (process_sp->GetNextThreadIndexID ()),
145     m_reg_context_sp (),
146     m_state (eStateUnloaded),
147     m_state_mutex (Mutex::eMutexTypeRecursive),
148     m_plan_stack (),
149     m_completed_plan_stack(),
150     m_frame_mutex (Mutex::eMutexTypeRecursive),
151     m_curr_frames_sp (),
152     m_prev_frames_sp (),
153     m_resume_signal (LLDB_INVALID_SIGNAL_NUMBER),
154     m_resume_state (eStateRunning),
155     m_temporary_resume_state (eStateRunning),
156     m_unwinder_ap (),
157     m_destroy_called (false),
158     m_thread_stop_reason_stop_id (0)
159 
160 {
161     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
162     if (log)
163         log->Printf ("%p Thread::Thread(tid = 0x%4.4llx)", this, GetID());
164 
165     QueueFundamentalPlan(true);
166 }
167 
168 
169 Thread::~Thread()
170 {
171     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
172     if (log)
173         log->Printf ("%p Thread::~Thread(tid = 0x%4.4llx)", this, GetID());
174     /// If you hit this assert, it means your derived class forgot to call DoDestroy in its destructor.
175     assert (m_destroy_called);
176 }
177 
178 void
179 Thread::DestroyThread ()
180 {
181     m_plan_stack.clear();
182     m_discarded_plan_stack.clear();
183     m_completed_plan_stack.clear();
184     m_actual_stop_info_sp.reset();
185     m_destroy_called = true;
186 }
187 
188 lldb::StopInfoSP
189 Thread::GetStopInfo ()
190 {
191     ThreadPlanSP plan_sp (GetCompletedPlan());
192     if (plan_sp && plan_sp->PlanSucceeded())
193         return StopInfo::CreateStopReasonWithPlan (plan_sp, GetReturnValueObject());
194     else
195     {
196         ProcessSP process_sp (GetProcess());
197         if (process_sp
198             && m_actual_stop_info_sp
199             && m_actual_stop_info_sp->IsValid()
200             && m_thread_stop_reason_stop_id == process_sp->GetStopID())
201             return m_actual_stop_info_sp;
202         else
203             return GetPrivateStopReason ();
204     }
205 }
206 
207 void
208 Thread::SetStopInfo (const lldb::StopInfoSP &stop_info_sp)
209 {
210     m_actual_stop_info_sp = stop_info_sp;
211     if (m_actual_stop_info_sp)
212         m_actual_stop_info_sp->MakeStopInfoValid();
213     ProcessSP process_sp (GetProcess());
214     if (process_sp)
215         m_thread_stop_reason_stop_id = process_sp->GetStopID();
216     else
217         m_thread_stop_reason_stop_id = UINT32_MAX;
218 }
219 
220 void
221 Thread::SetStopInfoToNothing()
222 {
223     // Note, we can't just NULL out the private reason, or the native thread implementation will try to
224     // go calculate it again.  For now, just set it to a Unix Signal with an invalid signal number.
225     SetStopInfo (StopInfo::CreateStopReasonWithSignal (*this,  LLDB_INVALID_SIGNAL_NUMBER));
226 }
227 
228 bool
229 Thread::ThreadStoppedForAReason (void)
230 {
231     return (bool) GetPrivateStopReason ();
232 }
233 
234 bool
235 Thread::CheckpointThreadState (ThreadStateCheckpoint &saved_state)
236 {
237     if (!SaveFrameZeroState(saved_state.register_backup))
238         return false;
239 
240     saved_state.stop_info_sp = GetStopInfo();
241     ProcessSP process_sp (GetProcess());
242     if (process_sp)
243         saved_state.orig_stop_id = process_sp->GetStopID();
244     return true;
245 }
246 
247 bool
248 Thread::RestoreThreadStateFromCheckpoint (ThreadStateCheckpoint &saved_state)
249 {
250     RestoreSaveFrameZero(saved_state.register_backup);
251     if (saved_state.stop_info_sp)
252         saved_state.stop_info_sp->MakeStopInfoValid();
253     SetStopInfo(saved_state.stop_info_sp);
254     return true;
255 }
256 
257 StateType
258 Thread::GetState() const
259 {
260     // If any other threads access this we will need a mutex for it
261     Mutex::Locker locker(m_state_mutex);
262     return m_state;
263 }
264 
265 void
266 Thread::SetState(StateType state)
267 {
268     Mutex::Locker locker(m_state_mutex);
269     m_state = state;
270 }
271 
272 void
273 Thread::WillStop()
274 {
275     ThreadPlan *current_plan = GetCurrentPlan();
276 
277     // FIXME: I may decide to disallow threads with no plans.  In which
278     // case this should go to an assert.
279 
280     if (!current_plan)
281         return;
282 
283     current_plan->WillStop();
284 }
285 
286 void
287 Thread::SetupForResume ()
288 {
289     if (GetResumeState() != eStateSuspended)
290     {
291 
292         // If we're at a breakpoint push the step-over breakpoint plan.  Do this before
293         // telling the current plan it will resume, since we might change what the current
294         // plan is.
295 
296         lldb::addr_t pc = GetRegisterContext()->GetPC();
297         BreakpointSiteSP bp_site_sp = GetProcess()->GetBreakpointSiteList().FindByAddress(pc);
298         if (bp_site_sp && bp_site_sp->IsEnabled())
299         {
300             // Note, don't assume there's a ThreadPlanStepOverBreakpoint, the target may not require anything
301             // special to step over a breakpoint.
302 
303             ThreadPlan *cur_plan = GetCurrentPlan();
304 
305             if (cur_plan->GetKind() != ThreadPlan::eKindStepOverBreakpoint)
306             {
307                 ThreadPlanStepOverBreakpoint *step_bp_plan = new ThreadPlanStepOverBreakpoint (*this);
308                 if (step_bp_plan)
309                 {
310                     ThreadPlanSP step_bp_plan_sp;
311                     step_bp_plan->SetPrivate (true);
312 
313                     if (GetCurrentPlan()->RunState() != eStateStepping)
314                     {
315                         step_bp_plan->SetAutoContinue(true);
316                     }
317                     step_bp_plan_sp.reset (step_bp_plan);
318                     QueueThreadPlan (step_bp_plan_sp, false);
319                 }
320             }
321         }
322     }
323 }
324 
325 bool
326 Thread::WillResume (StateType resume_state)
327 {
328     // At this point clear the completed plan stack.
329     m_completed_plan_stack.clear();
330     m_discarded_plan_stack.clear();
331 
332     SetTemporaryResumeState(resume_state);
333 
334     // This is a little dubious, but we are trying to limit how often we actually fetch stop info from
335     // the target, 'cause that slows down single stepping.  So assume that if we got to the point where
336     // we're about to resume, and we haven't yet had to fetch the stop reason, then it doesn't need to know
337     // about the fact that we are resuming...
338         const uint32_t process_stop_id = GetProcess()->GetStopID();
339     if (m_thread_stop_reason_stop_id == process_stop_id &&
340         (m_actual_stop_info_sp && m_actual_stop_info_sp->IsValid()))
341     {
342         StopInfo *stop_info = GetPrivateStopReason().get();
343         if (stop_info)
344             stop_info->WillResume (resume_state);
345     }
346 
347     // Tell all the plans that we are about to resume in case they need to clear any state.
348     // We distinguish between the plan on the top of the stack and the lower
349     // plans in case a plan needs to do any special business before it runs.
350 
351     ThreadPlan *plan_ptr = GetCurrentPlan();
352     bool need_to_resume = plan_ptr->WillResume(resume_state, true);
353 
354     while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
355     {
356         plan_ptr->WillResume (resume_state, false);
357     }
358 
359     // If the WillResume for the plan says we are faking a resume, then it will have set an appropriate stop info.
360     // In that case, don't reset it here.
361 
362     if (need_to_resume)
363     {
364         m_actual_stop_info_sp.reset();
365     }
366 
367     return need_to_resume;
368 }
369 
370 void
371 Thread::DidResume ()
372 {
373     SetResumeSignal (LLDB_INVALID_SIGNAL_NUMBER);
374 }
375 
376 bool
377 Thread::ShouldStop (Event* event_ptr)
378 {
379     ThreadPlan *current_plan = GetCurrentPlan();
380     bool should_stop = true;
381 
382     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
383 
384     if (GetResumeState () == eStateSuspended)
385     {
386         if (log)
387             log->Printf ("Thread::%s for tid = 0x%4.4llx, should_stop = 0 (ignore since thread was suspended)",
388                          __FUNCTION__,
389                          GetID ());
390 //            log->Printf ("Thread::%s for tid = 0x%4.4llx, pc = 0x%16.16llx, should_stop = 0 (ignore since thread was suspended)",
391 //                         __FUNCTION__,
392 //                         GetID (),
393 //                         GetRegisterContext()->GetPC());
394         return false;
395     }
396 
397     if (GetTemporaryResumeState () == eStateSuspended)
398     {
399         if (log)
400             log->Printf ("Thread::%s for tid = 0x%4.4llx, should_stop = 0 (ignore since thread was suspended)",
401                          __FUNCTION__,
402                          GetID ());
403 //            log->Printf ("Thread::%s for tid = 0x%4.4llx, pc = 0x%16.16llx, should_stop = 0 (ignore since thread was suspended)",
404 //                         __FUNCTION__,
405 //                         GetID (),
406 //                         GetRegisterContext()->GetPC());
407         return false;
408     }
409 
410     if (ThreadStoppedForAReason() == false)
411     {
412         if (log)
413             log->Printf ("Thread::%s for tid = 0x%4.4llx, pc = 0x%16.16llx, should_stop = 0 (ignore since no stop reason)",
414                          __FUNCTION__,
415                          GetID (),
416                          GetRegisterContext()->GetPC());
417         return false;
418     }
419 
420     // Adjust the stack frame's current inlined depth if it is needed.
421     GetStackFrameList()->CalculateCurrentInlinedDepth();
422 
423     if (log)
424     {
425         log->Printf ("Thread::%s for tid = 0x%4.4llx, pc = 0x%16.16llx",
426                      __FUNCTION__,
427                      GetID (),
428                      GetRegisterContext()->GetPC());
429         log->Printf ("^^^^^^^^ Thread::ShouldStop Begin ^^^^^^^^");
430         StreamString s;
431         s.IndentMore();
432         DumpThreadPlans(&s);
433         log->Printf ("Plan stack initial state:\n%s", s.GetData());
434     }
435 
436     // The top most plan always gets to do the trace log...
437     current_plan->DoTraceLog ();
438 
439     // First query the stop info's ShouldStopSynchronous.  This handles "synchronous" stop reasons, for example the breakpoint
440     // command on internal breakpoints.  If a synchronous stop reason says we should not stop, then we don't have to
441     // do any more work on this stop.
442     StopInfoSP private_stop_info (GetPrivateStopReason());
443     if (private_stop_info && private_stop_info->ShouldStopSynchronous(event_ptr) == false)
444     {
445         if (log)
446             log->Printf ("StopInfo::ShouldStop async callback says we should not stop, returning ShouldStop of false.");
447         return false;
448     }
449 
450     // If the base plan doesn't understand why we stopped, then we have to find a plan that does.
451     // If that plan is still working, then we don't need to do any more work.  If the plan that explains
452     // the stop is done, then we should pop all the plans below it, and pop it, and then let the plans above it decide
453     // whether they still need to do more work.
454 
455     bool done_processing_current_plan = false;
456 
457     if (!current_plan->PlanExplainsStop())
458     {
459         if (current_plan->TracerExplainsStop())
460         {
461             done_processing_current_plan = true;
462             should_stop = false;
463         }
464         else
465         {
466             // If the current plan doesn't explain the stop, then find one that
467             // does and let it handle the situation.
468             ThreadPlan *plan_ptr = current_plan;
469             while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
470             {
471                 if (plan_ptr->PlanExplainsStop())
472                 {
473                     should_stop = plan_ptr->ShouldStop (event_ptr);
474 
475                     // plan_ptr explains the stop, next check whether plan_ptr is done, if so, then we should take it
476                     // and all the plans below it off the stack.
477 
478                     if (plan_ptr->MischiefManaged())
479                     {
480                         // We're going to pop the plans up to and including the plan that explains the stop.
481                         ThreadPlan *prev_plan_ptr = GetPreviousPlan (plan_ptr);
482 
483                         do
484                         {
485                             if (should_stop)
486                                 current_plan->WillStop();
487                             PopPlan();
488                         }
489                         while ((current_plan = GetCurrentPlan()) != prev_plan_ptr);
490                         // Now, if the responsible plan was not "Okay to discard" then we're done,
491                         // otherwise we forward this to the next plan in the stack below.
492                         if (plan_ptr->IsMasterPlan() && !plan_ptr->OkayToDiscard())
493                             done_processing_current_plan = true;
494                         else
495                             done_processing_current_plan = false;
496                     }
497                     else
498                         done_processing_current_plan = true;
499 
500                     break;
501                 }
502 
503             }
504         }
505     }
506 
507     if (!done_processing_current_plan)
508     {
509         bool over_ride_stop = current_plan->ShouldAutoContinue(event_ptr);
510 
511         if (log)
512             log->Printf("Plan %s explains stop, auto-continue %i.", current_plan->GetName(), over_ride_stop);
513 
514         // We're starting from the base plan, so just let it decide;
515         if (PlanIsBasePlan(current_plan))
516         {
517             should_stop = current_plan->ShouldStop (event_ptr);
518             if (log)
519                 log->Printf("Base plan says should stop: %i.", should_stop);
520         }
521         else
522         {
523             // Otherwise, don't let the base plan override what the other plans say to do, since
524             // presumably if there were other plans they would know what to do...
525             while (1)
526             {
527                 if (PlanIsBasePlan(current_plan))
528                     break;
529 
530                 should_stop = current_plan->ShouldStop(event_ptr);
531                 if (log)
532                     log->Printf("Plan %s should stop: %d.", current_plan->GetName(), should_stop);
533                 if (current_plan->MischiefManaged())
534                 {
535                     if (should_stop)
536                         current_plan->WillStop();
537 
538                     // If a Master Plan wants to stop, and wants to stick on the stack, we let it.
539                     // Otherwise, see if the plan's parent wants to stop.
540 
541                     if (should_stop && current_plan->IsMasterPlan() && !current_plan->OkayToDiscard())
542                     {
543                         PopPlan();
544                         break;
545                     }
546                     else
547                     {
548 
549                         PopPlan();
550 
551                         current_plan = GetCurrentPlan();
552                         if (current_plan == NULL)
553                         {
554                             break;
555                         }
556                     }
557                 }
558                 else
559                 {
560                     break;
561                 }
562             }
563         }
564 
565         if (over_ride_stop)
566             should_stop = false;
567 
568         // One other potential problem is that we set up a master plan, then stop in before it is complete - for instance
569         // by hitting a breakpoint during a step-over - then do some step/finish/etc operations that wind up
570         // past the end point condition of the initial plan.  We don't want to strand the original plan on the stack,
571         // This code clears stale plans off the stack.
572 
573         if (should_stop)
574         {
575             ThreadPlan *plan_ptr = GetCurrentPlan();
576             while (!PlanIsBasePlan(plan_ptr))
577             {
578                 bool stale = plan_ptr->IsPlanStale ();
579                 ThreadPlan *examined_plan = plan_ptr;
580                 plan_ptr = GetPreviousPlan (examined_plan);
581 
582                 if (stale)
583                 {
584                     if (log)
585                         log->Printf("Plan %s being discarded in cleanup, it says it is already done.", examined_plan->GetName());
586                     DiscardThreadPlansUpToPlan(examined_plan);
587                 }
588             }
589         }
590 
591     }
592 
593     if (log)
594     {
595         StreamString s;
596         s.IndentMore();
597         DumpThreadPlans(&s);
598         log->Printf ("Plan stack final state:\n%s", s.GetData());
599         log->Printf ("vvvvvvvv Thread::ShouldStop End (returning %i) vvvvvvvv", should_stop);
600     }
601     return should_stop;
602 }
603 
604 Vote
605 Thread::ShouldReportStop (Event* event_ptr)
606 {
607     StateType thread_state = GetResumeState ();
608     StateType temp_thread_state = GetTemporaryResumeState();
609 
610     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
611 
612     if (thread_state == eStateSuspended || thread_state == eStateInvalid)
613     {
614         if (log)
615             log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4llx: returning vote %i (state was suspended or invalid)\n", GetID(), eVoteNoOpinion);
616         return eVoteNoOpinion;
617     }
618 
619     if (temp_thread_state == eStateSuspended || temp_thread_state == eStateInvalid)
620     {
621         if (log)
622             log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4llx: returning vote %i (temporary state was suspended or invalid)\n", GetID(), eVoteNoOpinion);
623         return eVoteNoOpinion;
624     }
625 
626     if (!ThreadStoppedForAReason())
627     {
628         if (log)
629             log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4llx: returning vote %i (thread didn't stop for a reason.)\n", GetID(), eVoteNoOpinion);
630         return eVoteNoOpinion;
631     }
632 
633     if (m_completed_plan_stack.size() > 0)
634     {
635         // Don't use GetCompletedPlan here, since that suppresses private plans.
636         if (log)
637             log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4llx: returning vote  for complete stack's back plan\n", GetID());
638         return m_completed_plan_stack.back()->ShouldReportStop (event_ptr);
639     }
640     else
641     {
642         if (log)
643             log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4llx: returning vote  for current plan\n", GetID());
644         return GetCurrentPlan()->ShouldReportStop (event_ptr);
645     }
646 }
647 
648 Vote
649 Thread::ShouldReportRun (Event* event_ptr)
650 {
651     StateType thread_state = GetResumeState ();
652 
653     if (thread_state == eStateSuspended
654             || thread_state == eStateInvalid)
655     {
656         return eVoteNoOpinion;
657     }
658 
659     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
660     if (m_completed_plan_stack.size() > 0)
661     {
662         // Don't use GetCompletedPlan here, since that suppresses private plans.
663         if (log)
664             log->Printf ("Current Plan for thread %d (0x%4.4llx): %s being asked whether we should report run.",
665                          GetIndexID(),
666                          GetID(),
667                          m_completed_plan_stack.back()->GetName());
668 
669         return m_completed_plan_stack.back()->ShouldReportRun (event_ptr);
670     }
671     else
672     {
673         if (log)
674             log->Printf ("Current Plan for thread %d (0x%4.4llx): %s being asked whether we should report run.",
675                          GetIndexID(),
676                          GetID(),
677                          GetCurrentPlan()->GetName());
678 
679         return GetCurrentPlan()->ShouldReportRun (event_ptr);
680      }
681 }
682 
683 bool
684 Thread::MatchesSpec (const ThreadSpec *spec)
685 {
686     if (spec == NULL)
687         return true;
688 
689     return spec->ThreadPassesBasicTests(*this);
690 }
691 
692 void
693 Thread::PushPlan (ThreadPlanSP &thread_plan_sp)
694 {
695     if (thread_plan_sp)
696     {
697         // If the thread plan doesn't already have a tracer, give it its parent's tracer:
698         if (!thread_plan_sp->GetThreadPlanTracer())
699             thread_plan_sp->SetThreadPlanTracer(m_plan_stack.back()->GetThreadPlanTracer());
700         m_plan_stack.push_back (thread_plan_sp);
701 
702         thread_plan_sp->DidPush();
703 
704         LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
705         if (log)
706         {
707             StreamString s;
708             thread_plan_sp->GetDescription (&s, lldb::eDescriptionLevelFull);
709             log->Printf("Pushing plan: \"%s\", tid = 0x%4.4llx.",
710                         s.GetData(),
711                         thread_plan_sp->GetThread().GetID());
712         }
713     }
714 }
715 
716 void
717 Thread::PopPlan ()
718 {
719     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
720 
721     if (m_plan_stack.size() <= 1)
722         return;
723     else
724     {
725         ThreadPlanSP &plan = m_plan_stack.back();
726         if (log)
727         {
728             log->Printf("Popping plan: \"%s\", tid = 0x%4.4llx.", plan->GetName(), plan->GetThread().GetID());
729         }
730         m_completed_plan_stack.push_back (plan);
731         plan->WillPop();
732         m_plan_stack.pop_back();
733     }
734 }
735 
736 void
737 Thread::DiscardPlan ()
738 {
739     if (m_plan_stack.size() > 1)
740     {
741         ThreadPlanSP &plan = m_plan_stack.back();
742         m_discarded_plan_stack.push_back (plan);
743         plan->WillPop();
744         m_plan_stack.pop_back();
745     }
746 }
747 
748 ThreadPlan *
749 Thread::GetCurrentPlan ()
750 {
751     // There will always be at least the base plan.  If somebody is mucking with a
752     // thread with an empty plan stack, we should assert right away.
753     assert (!m_plan_stack.empty());
754 
755     return m_plan_stack.back().get();
756 }
757 
758 ThreadPlanSP
759 Thread::GetCompletedPlan ()
760 {
761     ThreadPlanSP empty_plan_sp;
762     if (!m_completed_plan_stack.empty())
763     {
764         for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
765         {
766             ThreadPlanSP completed_plan_sp;
767             completed_plan_sp = m_completed_plan_stack[i];
768             if (!completed_plan_sp->GetPrivate ())
769             return completed_plan_sp;
770         }
771     }
772     return empty_plan_sp;
773 }
774 
775 ValueObjectSP
776 Thread::GetReturnValueObject ()
777 {
778     if (!m_completed_plan_stack.empty())
779     {
780         for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
781         {
782             ValueObjectSP return_valobj_sp;
783             return_valobj_sp = m_completed_plan_stack[i]->GetReturnValueObject();
784             if (return_valobj_sp)
785             return return_valobj_sp;
786         }
787     }
788     return ValueObjectSP();
789 }
790 
791 bool
792 Thread::IsThreadPlanDone (ThreadPlan *plan)
793 {
794     if (!m_completed_plan_stack.empty())
795     {
796         for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
797         {
798             if (m_completed_plan_stack[i].get() == plan)
799                 return true;
800         }
801     }
802     return false;
803 }
804 
805 bool
806 Thread::WasThreadPlanDiscarded (ThreadPlan *plan)
807 {
808     if (!m_discarded_plan_stack.empty())
809     {
810         for (int i = m_discarded_plan_stack.size() - 1; i >= 0; i--)
811         {
812             if (m_discarded_plan_stack[i].get() == plan)
813                 return true;
814         }
815     }
816     return false;
817 }
818 
819 ThreadPlan *
820 Thread::GetPreviousPlan (ThreadPlan *current_plan)
821 {
822     if (current_plan == NULL)
823         return NULL;
824 
825     int stack_size = m_completed_plan_stack.size();
826     for (int i = stack_size - 1; i > 0; i--)
827     {
828         if (current_plan == m_completed_plan_stack[i].get())
829             return m_completed_plan_stack[i-1].get();
830     }
831 
832     if (stack_size > 0 && m_completed_plan_stack[0].get() == current_plan)
833     {
834         if (m_plan_stack.size() > 0)
835             return m_plan_stack.back().get();
836         else
837             return NULL;
838     }
839 
840     stack_size = m_plan_stack.size();
841     for (int i = stack_size - 1; i > 0; i--)
842     {
843         if (current_plan == m_plan_stack[i].get())
844             return m_plan_stack[i-1].get();
845     }
846     return NULL;
847 }
848 
849 void
850 Thread::QueueThreadPlan (ThreadPlanSP &thread_plan_sp, bool abort_other_plans)
851 {
852     if (abort_other_plans)
853        DiscardThreadPlans(true);
854 
855     PushPlan (thread_plan_sp);
856 }
857 
858 
859 void
860 Thread::EnableTracer (bool value, bool single_stepping)
861 {
862     int stack_size = m_plan_stack.size();
863     for (int i = 0; i < stack_size; i++)
864     {
865         if (m_plan_stack[i]->GetThreadPlanTracer())
866         {
867             m_plan_stack[i]->GetThreadPlanTracer()->EnableTracing(value);
868             m_plan_stack[i]->GetThreadPlanTracer()->EnableSingleStep(single_stepping);
869         }
870     }
871 }
872 
873 void
874 Thread::SetTracer (lldb::ThreadPlanTracerSP &tracer_sp)
875 {
876     int stack_size = m_plan_stack.size();
877     for (int i = 0; i < stack_size; i++)
878         m_plan_stack[i]->SetThreadPlanTracer(tracer_sp);
879 }
880 
881 void
882 Thread::DiscardThreadPlansUpToPlan (lldb::ThreadPlanSP &up_to_plan_sp)
883 {
884     DiscardThreadPlansUpToPlan (up_to_plan_sp.get());
885 }
886 
887 void
888 Thread::DiscardThreadPlansUpToPlan (ThreadPlan *up_to_plan_ptr)
889 {
890     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
891     if (log)
892     {
893         log->Printf("Discarding thread plans for thread tid = 0x%4.4llx, up to %p", GetID(), up_to_plan_ptr);
894     }
895 
896     int stack_size = m_plan_stack.size();
897 
898     // If the input plan is NULL, discard all plans.  Otherwise make sure this plan is in the
899     // stack, and if so discard up to and including it.
900 
901     if (up_to_plan_ptr == NULL)
902     {
903         for (int i = stack_size - 1; i > 0; i--)
904             DiscardPlan();
905     }
906     else
907     {
908         bool found_it = false;
909         for (int i = stack_size - 1; i > 0; i--)
910         {
911             if (m_plan_stack[i].get() == up_to_plan_ptr)
912                 found_it = true;
913         }
914         if (found_it)
915         {
916             bool last_one = false;
917             for (int i = stack_size - 1; i > 0 && !last_one ; i--)
918             {
919                 if (GetCurrentPlan() == up_to_plan_ptr)
920                     last_one = true;
921                 DiscardPlan();
922             }
923         }
924     }
925     return;
926 }
927 
928 void
929 Thread::DiscardThreadPlans(bool force)
930 {
931     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
932     if (log)
933     {
934         log->Printf("Discarding thread plans for thread (tid = 0x%4.4llx, force %d)", GetID(), force);
935     }
936 
937     if (force)
938     {
939         int stack_size = m_plan_stack.size();
940         for (int i = stack_size - 1; i > 0; i--)
941         {
942             DiscardPlan();
943         }
944         return;
945     }
946 
947     while (1)
948     {
949 
950         int master_plan_idx;
951         bool discard;
952 
953         // Find the first master plan, see if it wants discarding, and if yes discard up to it.
954         for (master_plan_idx = m_plan_stack.size() - 1; master_plan_idx >= 0; master_plan_idx--)
955         {
956             if (m_plan_stack[master_plan_idx]->IsMasterPlan())
957             {
958                 discard = m_plan_stack[master_plan_idx]->OkayToDiscard();
959                 break;
960             }
961         }
962 
963         if (discard)
964         {
965             // First pop all the dependent plans:
966             for (int i = m_plan_stack.size() - 1; i > master_plan_idx; i--)
967             {
968 
969                 // FIXME: Do we need a finalize here, or is the rule that "PrepareForStop"
970                 // for the plan leaves it in a state that it is safe to pop the plan
971                 // with no more notice?
972                 DiscardPlan();
973             }
974 
975             // Now discard the master plan itself.
976             // The bottom-most plan never gets discarded.  "OkayToDiscard" for it means
977             // discard it's dependent plans, but not it...
978             if (master_plan_idx > 0)
979             {
980                 DiscardPlan();
981             }
982         }
983         else
984         {
985             // If the master plan doesn't want to get discarded, then we're done.
986             break;
987         }
988 
989     }
990 }
991 
992 bool
993 Thread::PlanIsBasePlan (ThreadPlan *plan_ptr)
994 {
995     if (plan_ptr->IsBasePlan())
996         return true;
997     else if (m_plan_stack.size() == 0)
998         return false;
999     else
1000        return m_plan_stack[0].get() == plan_ptr;
1001 }
1002 
1003 ThreadPlan *
1004 Thread::QueueFundamentalPlan (bool abort_other_plans)
1005 {
1006     ThreadPlanSP thread_plan_sp (new ThreadPlanBase(*this));
1007     QueueThreadPlan (thread_plan_sp, abort_other_plans);
1008     return thread_plan_sp.get();
1009 }
1010 
1011 ThreadPlan *
1012 Thread::QueueThreadPlanForStepSingleInstruction
1013 (
1014     bool step_over,
1015     bool abort_other_plans,
1016     bool stop_other_threads
1017 )
1018 {
1019     ThreadPlanSP thread_plan_sp (new ThreadPlanStepInstruction (*this, step_over, stop_other_threads, eVoteNoOpinion, eVoteNoOpinion));
1020     QueueThreadPlan (thread_plan_sp, abort_other_plans);
1021     return thread_plan_sp.get();
1022 }
1023 
1024 ThreadPlan *
1025 Thread::QueueThreadPlanForStepRange
1026 (
1027     bool abort_other_plans,
1028     StepType type,
1029     const AddressRange &range,
1030     const SymbolContext &addr_context,
1031     lldb::RunMode stop_other_threads,
1032     bool avoid_code_without_debug_info
1033 )
1034 {
1035     ThreadPlanSP thread_plan_sp;
1036     if (type == eStepTypeInto)
1037     {
1038         ThreadPlanStepInRange *plan = new ThreadPlanStepInRange (*this, range, addr_context, stop_other_threads);
1039         if (avoid_code_without_debug_info)
1040             plan->GetFlags().Set (ThreadPlanShouldStopHere::eAvoidNoDebug);
1041         else
1042             plan->GetFlags().Clear (ThreadPlanShouldStopHere::eAvoidNoDebug);
1043         thread_plan_sp.reset (plan);
1044     }
1045     else
1046         thread_plan_sp.reset (new ThreadPlanStepOverRange (*this, range, addr_context, stop_other_threads));
1047 
1048     QueueThreadPlan (thread_plan_sp, abort_other_plans);
1049     return thread_plan_sp.get();
1050 }
1051 
1052 
1053 ThreadPlan *
1054 Thread::QueueThreadPlanForStepOverBreakpointPlan (bool abort_other_plans)
1055 {
1056     ThreadPlanSP thread_plan_sp (new ThreadPlanStepOverBreakpoint (*this));
1057     QueueThreadPlan (thread_plan_sp, abort_other_plans);
1058     return thread_plan_sp.get();
1059 }
1060 
1061 ThreadPlan *
1062 Thread::QueueThreadPlanForStepOut
1063 (
1064     bool abort_other_plans,
1065     SymbolContext *addr_context,
1066     bool first_insn,
1067     bool stop_other_threads,
1068     Vote stop_vote,
1069     Vote run_vote,
1070     uint32_t frame_idx
1071 )
1072 {
1073     ThreadPlanSP thread_plan_sp (new ThreadPlanStepOut (*this,
1074                                                         addr_context,
1075                                                         first_insn,
1076                                                         stop_other_threads,
1077                                                         stop_vote,
1078                                                         run_vote,
1079                                                         frame_idx));
1080 
1081     if (thread_plan_sp->ValidatePlan(NULL))
1082     {
1083         QueueThreadPlan (thread_plan_sp, abort_other_plans);
1084         return thread_plan_sp.get();
1085     }
1086     else
1087     {
1088         return NULL;
1089     }
1090 }
1091 
1092 ThreadPlan *
1093 Thread::QueueThreadPlanForStepThrough (StackID &return_stack_id, bool abort_other_plans, bool stop_other_threads)
1094 {
1095     ThreadPlanSP thread_plan_sp(new ThreadPlanStepThrough (*this, return_stack_id, stop_other_threads));
1096     if (!thread_plan_sp || !thread_plan_sp->ValidatePlan (NULL))
1097         return NULL;
1098 
1099     QueueThreadPlan (thread_plan_sp, abort_other_plans);
1100     return thread_plan_sp.get();
1101 }
1102 
1103 ThreadPlan *
1104 Thread::QueueThreadPlanForCallFunction (bool abort_other_plans,
1105                                         Address& function,
1106                                         lldb::addr_t arg,
1107                                         bool stop_other_threads,
1108                                         bool discard_on_error)
1109 {
1110     ThreadPlanSP thread_plan_sp (new ThreadPlanCallFunction (*this, function, ClangASTType(), arg, stop_other_threads, discard_on_error));
1111     QueueThreadPlan (thread_plan_sp, abort_other_plans);
1112     return thread_plan_sp.get();
1113 }
1114 
1115 ThreadPlan *
1116 Thread::QueueThreadPlanForRunToAddress (bool abort_other_plans,
1117                                         Address &target_addr,
1118                                         bool stop_other_threads)
1119 {
1120     ThreadPlanSP thread_plan_sp (new ThreadPlanRunToAddress (*this, target_addr, stop_other_threads));
1121     QueueThreadPlan (thread_plan_sp, abort_other_plans);
1122     return thread_plan_sp.get();
1123 }
1124 
1125 ThreadPlan *
1126 Thread::QueueThreadPlanForStepUntil (bool abort_other_plans,
1127                                      lldb::addr_t *address_list,
1128                                      size_t num_addresses,
1129                                      bool stop_other_threads,
1130                                      uint32_t frame_idx)
1131 {
1132     ThreadPlanSP thread_plan_sp (new ThreadPlanStepUntil (*this, address_list, num_addresses, stop_other_threads, frame_idx));
1133     QueueThreadPlan (thread_plan_sp, abort_other_plans);
1134     return thread_plan_sp.get();
1135 
1136 }
1137 
1138 uint32_t
1139 Thread::GetIndexID () const
1140 {
1141     return m_index_id;
1142 }
1143 
1144 void
1145 Thread::DumpThreadPlans (lldb_private::Stream *s) const
1146 {
1147     uint32_t stack_size = m_plan_stack.size();
1148     int i;
1149     s->Indent();
1150     s->Printf ("Plan Stack for thread #%u: tid = 0x%4.4llx, stack_size = %d\n", GetIndexID(), GetID(), stack_size);
1151     for (i = stack_size - 1; i >= 0; i--)
1152     {
1153         s->IndentMore();
1154         s->Indent();
1155         s->Printf ("Element %d: ", i);
1156         m_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1157         s->EOL();
1158         s->IndentLess();
1159     }
1160 
1161     stack_size = m_completed_plan_stack.size();
1162     if (stack_size > 0)
1163     {
1164         s->Indent();
1165         s->Printf ("Completed Plan Stack: %d elements.\n", stack_size);
1166         for (i = stack_size - 1; i >= 0; i--)
1167         {
1168             s->IndentMore();
1169             s->Indent();
1170             s->Printf ("Element %d: ", i);
1171             m_completed_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1172             s->EOL();
1173             s->IndentLess();
1174         }
1175     }
1176 
1177     stack_size = m_discarded_plan_stack.size();
1178     if (stack_size > 0)
1179     {
1180         s->Indent();
1181         s->Printf ("Discarded Plan Stack: %d elements.\n", stack_size);
1182         for (i = stack_size - 1; i >= 0; i--)
1183         {
1184             s->IndentMore();
1185             s->Indent();
1186             s->Printf ("Element %d: ", i);
1187             m_discarded_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1188             s->EOL();
1189             s->IndentLess();
1190         }
1191     }
1192 
1193 }
1194 
1195 TargetSP
1196 Thread::CalculateTarget ()
1197 {
1198     TargetSP target_sp;
1199     ProcessSP process_sp(GetProcess());
1200     if (process_sp)
1201         target_sp = process_sp->CalculateTarget();
1202     return target_sp;
1203 
1204 }
1205 
1206 ProcessSP
1207 Thread::CalculateProcess ()
1208 {
1209     return GetProcess();
1210 }
1211 
1212 ThreadSP
1213 Thread::CalculateThread ()
1214 {
1215     return shared_from_this();
1216 }
1217 
1218 StackFrameSP
1219 Thread::CalculateStackFrame ()
1220 {
1221     return StackFrameSP();
1222 }
1223 
1224 void
1225 Thread::CalculateExecutionContext (ExecutionContext &exe_ctx)
1226 {
1227     exe_ctx.SetContext (shared_from_this());
1228 }
1229 
1230 
1231 StackFrameListSP
1232 Thread::GetStackFrameList ()
1233 {
1234     StackFrameListSP frame_list_sp;
1235     Mutex::Locker locker(m_frame_mutex);
1236     if (m_curr_frames_sp)
1237     {
1238         frame_list_sp = m_curr_frames_sp;
1239     }
1240     else
1241     {
1242         frame_list_sp.reset(new StackFrameList (*this, m_prev_frames_sp, true));
1243         m_curr_frames_sp = frame_list_sp;
1244     }
1245     return frame_list_sp;
1246 }
1247 
1248 void
1249 Thread::ClearStackFrames ()
1250 {
1251     Mutex::Locker locker(m_frame_mutex);
1252 
1253     // Only store away the old "reference" StackFrameList if we got all its frames:
1254     // FIXME: At some point we can try to splice in the frames we have fetched into
1255     // the new frame as we make it, but let's not try that now.
1256     if (m_curr_frames_sp && m_curr_frames_sp->GetAllFramesFetched())
1257         m_prev_frames_sp.swap (m_curr_frames_sp);
1258     m_curr_frames_sp.reset();
1259 }
1260 
1261 lldb::StackFrameSP
1262 Thread::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx)
1263 {
1264     return GetStackFrameList()->GetFrameWithConcreteFrameIndex (unwind_idx);
1265 }
1266 
1267 void
1268 Thread::DumpUsingSettingsFormat (Stream &strm, uint32_t frame_idx)
1269 {
1270     ExecutionContext exe_ctx (shared_from_this());
1271     Process *process = exe_ctx.GetProcessPtr();
1272     if (process == NULL)
1273         return;
1274 
1275     StackFrameSP frame_sp;
1276     SymbolContext frame_sc;
1277     if (frame_idx != LLDB_INVALID_INDEX32)
1278     {
1279         frame_sp = GetStackFrameAtIndex (frame_idx);
1280         if (frame_sp)
1281         {
1282             exe_ctx.SetFrameSP(frame_sp);
1283             frame_sc = frame_sp->GetSymbolContext(eSymbolContextEverything);
1284         }
1285     }
1286 
1287     const char *thread_format = exe_ctx.GetTargetRef().GetDebugger().GetThreadFormat();
1288     assert (thread_format);
1289     const char *end = NULL;
1290     Debugger::FormatPrompt (thread_format,
1291                             frame_sp ? &frame_sc : NULL,
1292                             &exe_ctx,
1293                             NULL,
1294                             strm,
1295                             &end);
1296 }
1297 
1298 void
1299 Thread::SettingsInitialize ()
1300 {
1301 }
1302 
1303 void
1304 Thread::SettingsTerminate ()
1305 {
1306 }
1307 
1308 lldb::StackFrameSP
1309 Thread::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr)
1310 {
1311     return GetStackFrameList()->GetStackFrameSPForStackFramePtr (stack_frame_ptr);
1312 }
1313 
1314 const char *
1315 Thread::StopReasonAsCString (lldb::StopReason reason)
1316 {
1317     switch (reason)
1318     {
1319     case eStopReasonInvalid:      return "invalid";
1320     case eStopReasonNone:         return "none";
1321     case eStopReasonTrace:        return "trace";
1322     case eStopReasonBreakpoint:   return "breakpoint";
1323     case eStopReasonWatchpoint:   return "watchpoint";
1324     case eStopReasonSignal:       return "signal";
1325     case eStopReasonException:    return "exception";
1326     case eStopReasonPlanComplete: return "plan complete";
1327     }
1328 
1329 
1330     static char unknown_state_string[64];
1331     snprintf(unknown_state_string, sizeof (unknown_state_string), "StopReason = %i", reason);
1332     return unknown_state_string;
1333 }
1334 
1335 const char *
1336 Thread::RunModeAsCString (lldb::RunMode mode)
1337 {
1338     switch (mode)
1339     {
1340     case eOnlyThisThread:     return "only this thread";
1341     case eAllThreads:         return "all threads";
1342     case eOnlyDuringStepping: return "only during stepping";
1343     }
1344 
1345     static char unknown_state_string[64];
1346     snprintf(unknown_state_string, sizeof (unknown_state_string), "RunMode = %i", mode);
1347     return unknown_state_string;
1348 }
1349 
1350 size_t
1351 Thread::GetStatus (Stream &strm, uint32_t start_frame, uint32_t num_frames, uint32_t num_frames_with_source)
1352 {
1353     ExecutionContext exe_ctx (shared_from_this());
1354     Target *target = exe_ctx.GetTargetPtr();
1355     Process *process = exe_ctx.GetProcessPtr();
1356     size_t num_frames_shown = 0;
1357     strm.Indent();
1358     bool is_selected = false;
1359     if (process)
1360     {
1361         if (process->GetThreadList().GetSelectedThread().get() == this)
1362             is_selected = true;
1363     }
1364     strm.Printf("%c ", is_selected ? '*' : ' ');
1365     if (target && target->GetDebugger().GetUseExternalEditor())
1366     {
1367         StackFrameSP frame_sp = GetStackFrameAtIndex(start_frame);
1368         if (frame_sp)
1369         {
1370             SymbolContext frame_sc(frame_sp->GetSymbolContext (eSymbolContextLineEntry));
1371             if (frame_sc.line_entry.line != 0 && frame_sc.line_entry.file)
1372             {
1373                 Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line);
1374             }
1375         }
1376     }
1377 
1378     DumpUsingSettingsFormat (strm, start_frame);
1379 
1380     if (num_frames > 0)
1381     {
1382         strm.IndentMore();
1383 
1384         const bool show_frame_info = true;
1385         strm.IndentMore ();
1386         num_frames_shown = GetStackFrameList ()->GetStatus (strm,
1387                                                             start_frame,
1388                                                             num_frames,
1389                                                             show_frame_info,
1390                                                             num_frames_with_source);
1391         strm.IndentLess();
1392         strm.IndentLess();
1393     }
1394     return num_frames_shown;
1395 }
1396 
1397 size_t
1398 Thread::GetStackFrameStatus (Stream& strm,
1399                              uint32_t first_frame,
1400                              uint32_t num_frames,
1401                              bool show_frame_info,
1402                              uint32_t num_frames_with_source)
1403 {
1404     return GetStackFrameList()->GetStatus (strm,
1405                                            first_frame,
1406                                            num_frames,
1407                                            show_frame_info,
1408                                            num_frames_with_source);
1409 }
1410 
1411 bool
1412 Thread::SaveFrameZeroState (RegisterCheckpoint &checkpoint)
1413 {
1414     lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
1415     if (frame_sp)
1416     {
1417         checkpoint.SetStackID(frame_sp->GetStackID());
1418         return frame_sp->GetRegisterContext()->ReadAllRegisterValues (checkpoint.GetData());
1419     }
1420     return false;
1421 }
1422 
1423 bool
1424 Thread::RestoreSaveFrameZero (const RegisterCheckpoint &checkpoint)
1425 {
1426     lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
1427     if (frame_sp)
1428     {
1429         bool ret = frame_sp->GetRegisterContext()->WriteAllRegisterValues (checkpoint.GetData());
1430 
1431         // Clear out all stack frames as our world just changed.
1432         ClearStackFrames();
1433         frame_sp->GetRegisterContext()->InvalidateIfNeeded(true);
1434 
1435         return ret;
1436     }
1437     return false;
1438 }
1439 
1440 Unwind *
1441 Thread::GetUnwinder ()
1442 {
1443     if (m_unwinder_ap.get() == NULL)
1444     {
1445         const ArchSpec target_arch (CalculateTarget()->GetArchitecture ());
1446         const llvm::Triple::ArchType machine = target_arch.GetMachine();
1447         switch (machine)
1448         {
1449             case llvm::Triple::x86_64:
1450             case llvm::Triple::x86:
1451             case llvm::Triple::arm:
1452             case llvm::Triple::thumb:
1453                 m_unwinder_ap.reset (new UnwindLLDB (*this));
1454                 break;
1455 
1456             default:
1457                 if (target_arch.GetTriple().getVendor() == llvm::Triple::Apple)
1458                     m_unwinder_ap.reset (new UnwindMacOSXFrameBackchain (*this));
1459                 break;
1460         }
1461     }
1462     return m_unwinder_ap.get();
1463 }
1464 
1465 
1466 void
1467 Thread::Flush ()
1468 {
1469     ClearStackFrames ();
1470     m_reg_context_sp.reset();
1471 }
1472