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     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     m_actual_stop_info_sp.reset();
360     return true;
361 }
362 
363 void
364 Thread::DidResume ()
365 {
366     SetResumeSignal (LLDB_INVALID_SIGNAL_NUMBER);
367 }
368 
369 bool
370 Thread::ShouldStop (Event* event_ptr)
371 {
372     ThreadPlan *current_plan = GetCurrentPlan();
373     bool should_stop = true;
374 
375     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
376 
377     if (GetResumeState () == eStateSuspended)
378     {
379         if (log)
380             log->Printf ("Thread::%s for tid = 0x%4.4llx, should_stop = 0 (ignore since thread was suspended)",
381                          __FUNCTION__,
382                          GetID ());
383 //            log->Printf ("Thread::%s for tid = 0x%4.4llx, pc = 0x%16.16llx, should_stop = 0 (ignore since thread was suspended)",
384 //                         __FUNCTION__,
385 //                         GetID (),
386 //                         GetRegisterContext()->GetPC());
387         return false;
388     }
389 
390     if (GetTemporaryResumeState () == eStateSuspended)
391     {
392         if (log)
393             log->Printf ("Thread::%s for tid = 0x%4.4llx, should_stop = 0 (ignore since thread was suspended)",
394                          __FUNCTION__,
395                          GetID ());
396 //            log->Printf ("Thread::%s for tid = 0x%4.4llx, pc = 0x%16.16llx, should_stop = 0 (ignore since thread was suspended)",
397 //                         __FUNCTION__,
398 //                         GetID (),
399 //                         GetRegisterContext()->GetPC());
400         return false;
401     }
402 
403     if (ThreadStoppedForAReason() == false)
404     {
405         if (log)
406             log->Printf ("Thread::%s for tid = 0x%4.4llx, pc = 0x%16.16llx, should_stop = 0 (ignore since no stop reason)",
407                          __FUNCTION__,
408                          GetID (),
409                          GetRegisterContext()->GetPC());
410         return false;
411     }
412 
413     if (log)
414     {
415         log->Printf ("Thread::%s for tid = 0x%4.4llx, pc = 0x%16.16llx",
416                      __FUNCTION__,
417                      GetID (),
418                      GetRegisterContext()->GetPC());
419         log->Printf ("^^^^^^^^ Thread::ShouldStop Begin ^^^^^^^^");
420         StreamString s;
421         s.IndentMore();
422         DumpThreadPlans(&s);
423         log->Printf ("Plan stack initial state:\n%s", s.GetData());
424     }
425 
426     // The top most plan always gets to do the trace log...
427     current_plan->DoTraceLog ();
428 
429     // First query the stop info's ShouldStopSynchronous.  This handles "synchronous" stop reasons, for example the breakpoint
430     // command on internal breakpoints.  If a synchronous stop reason says we should not stop, then we don't have to
431     // do any more work on this stop.
432     StopInfoSP private_stop_info (GetPrivateStopReason());
433     if (private_stop_info && private_stop_info->ShouldStopSynchronous(event_ptr) == false)
434     {
435         if (log)
436             log->Printf ("StopInfo::ShouldStop async callback says we should not stop, returning ShouldStop of false.");
437         return false;
438     }
439 
440     // If the base plan doesn't understand why we stopped, then we have to find a plan that does.
441     // If that plan is still working, then we don't need to do any more work.  If the plan that explains
442     // the stop is done, then we should pop all the plans below it, and pop it, and then let the plans above it decide
443     // whether they still need to do more work.
444 
445     bool done_processing_current_plan = false;
446 
447     if (!current_plan->PlanExplainsStop())
448     {
449         if (current_plan->TracerExplainsStop())
450         {
451             done_processing_current_plan = true;
452             should_stop = false;
453         }
454         else
455         {
456             // If the current plan doesn't explain the stop, then find one that
457             // does and let it handle the situation.
458             ThreadPlan *plan_ptr = current_plan;
459             while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
460             {
461                 if (plan_ptr->PlanExplainsStop())
462                 {
463                     should_stop = plan_ptr->ShouldStop (event_ptr);
464 
465                     // plan_ptr explains the stop, next check whether plan_ptr is done, if so, then we should take it
466                     // and all the plans below it off the stack.
467 
468                     if (plan_ptr->MischiefManaged())
469                     {
470                         // We're going to pop the plans up to and including the plan that explains the stop.
471                         ThreadPlan *prev_plan_ptr = GetPreviousPlan (plan_ptr);
472 
473                         do
474                         {
475                             if (should_stop)
476                                 current_plan->WillStop();
477                             PopPlan();
478                         }
479                         while ((current_plan = GetCurrentPlan()) != prev_plan_ptr);
480                         // Now, if the responsible plan was not "Okay to discard" then we're done,
481                         // otherwise we forward this to the next plan in the stack below.
482                         if (plan_ptr->IsMasterPlan() && !plan_ptr->OkayToDiscard())
483                             done_processing_current_plan = true;
484                         else
485                             done_processing_current_plan = false;
486                     }
487                     else
488                         done_processing_current_plan = true;
489 
490                     break;
491                 }
492 
493             }
494         }
495     }
496 
497     if (!done_processing_current_plan)
498     {
499         bool over_ride_stop = current_plan->ShouldAutoContinue(event_ptr);
500 
501         if (log)
502             log->Printf("Plan %s explains stop, auto-continue %i.", current_plan->GetName(), over_ride_stop);
503 
504         // We're starting from the base plan, so just let it decide;
505         if (PlanIsBasePlan(current_plan))
506         {
507             should_stop = current_plan->ShouldStop (event_ptr);
508             if (log)
509                 log->Printf("Base plan says should stop: %i.", should_stop);
510         }
511         else
512         {
513             // Otherwise, don't let the base plan override what the other plans say to do, since
514             // presumably if there were other plans they would know what to do...
515             while (1)
516             {
517                 if (PlanIsBasePlan(current_plan))
518                     break;
519 
520                 should_stop = current_plan->ShouldStop(event_ptr);
521                 if (log)
522                     log->Printf("Plan %s should stop: %d.", current_plan->GetName(), should_stop);
523                 if (current_plan->MischiefManaged())
524                 {
525                     if (should_stop)
526                         current_plan->WillStop();
527 
528                     // If a Master Plan wants to stop, and wants to stick on the stack, we let it.
529                     // Otherwise, see if the plan's parent wants to stop.
530 
531                     if (should_stop && current_plan->IsMasterPlan() && !current_plan->OkayToDiscard())
532                     {
533                         PopPlan();
534                         break;
535                     }
536                     else
537                     {
538 
539                         PopPlan();
540 
541                         current_plan = GetCurrentPlan();
542                         if (current_plan == NULL)
543                         {
544                             break;
545                         }
546                     }
547                 }
548                 else
549                 {
550                     break;
551                 }
552             }
553         }
554 
555         if (over_ride_stop)
556             should_stop = false;
557 
558         // One other potential problem is that we set up a master plan, then stop in before it is complete - for instance
559         // by hitting a breakpoint during a step-over - then do some step/finish/etc operations that wind up
560         // past the end point condition of the initial plan.  We don't want to strand the original plan on the stack,
561         // This code clears stale plans off the stack.
562 
563         if (should_stop)
564         {
565             ThreadPlan *plan_ptr = GetCurrentPlan();
566             while (!PlanIsBasePlan(plan_ptr))
567             {
568                 bool stale = plan_ptr->IsPlanStale ();
569                 ThreadPlan *examined_plan = plan_ptr;
570                 plan_ptr = GetPreviousPlan (examined_plan);
571 
572                 if (stale)
573                 {
574                     if (log)
575                         log->Printf("Plan %s being discarded in cleanup, it says it is already done.", examined_plan->GetName());
576                     DiscardThreadPlansUpToPlan(examined_plan);
577                 }
578             }
579         }
580 
581     }
582 
583     if (log)
584     {
585         StreamString s;
586         s.IndentMore();
587         DumpThreadPlans(&s);
588         log->Printf ("Plan stack final state:\n%s", s.GetData());
589         log->Printf ("vvvvvvvv Thread::ShouldStop End (returning %i) vvvvvvvv", should_stop);
590     }
591     return should_stop;
592 }
593 
594 Vote
595 Thread::ShouldReportStop (Event* event_ptr)
596 {
597     StateType thread_state = GetResumeState ();
598     StateType temp_thread_state = GetTemporaryResumeState();
599 
600     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
601 
602     if (thread_state == eStateSuspended || thread_state == eStateInvalid)
603     {
604         if (log)
605             log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4llx: returning vote %i (state was suspended or invalid)\n", GetID(), eVoteNoOpinion);
606         return eVoteNoOpinion;
607     }
608 
609     if (temp_thread_state == eStateSuspended || temp_thread_state == eStateInvalid)
610     {
611         if (log)
612             log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4llx: returning vote %i (temporary state was suspended or invalid)\n", GetID(), eVoteNoOpinion);
613         return eVoteNoOpinion;
614     }
615 
616     if (!ThreadStoppedForAReason())
617     {
618         if (log)
619             log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4llx: returning vote %i (thread didn't stop for a reason.)\n", GetID(), eVoteNoOpinion);
620         return eVoteNoOpinion;
621     }
622 
623     if (m_completed_plan_stack.size() > 0)
624     {
625         // Don't use GetCompletedPlan here, since that suppresses private plans.
626         if (log)
627             log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4llx: returning vote  for complete stack's back plan\n", GetID());
628         return m_completed_plan_stack.back()->ShouldReportStop (event_ptr);
629     }
630     else
631     {
632         if (log)
633             log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4llx: returning vote  for current plan\n", GetID());
634         return GetCurrentPlan()->ShouldReportStop (event_ptr);
635     }
636 }
637 
638 Vote
639 Thread::ShouldReportRun (Event* event_ptr)
640 {
641     StateType thread_state = GetResumeState ();
642 
643     if (thread_state == eStateSuspended
644             || thread_state == eStateInvalid)
645     {
646         return eVoteNoOpinion;
647     }
648 
649     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
650     if (m_completed_plan_stack.size() > 0)
651     {
652         // Don't use GetCompletedPlan here, since that suppresses private plans.
653         if (log)
654             log->Printf ("Current Plan for thread %d (0x%4.4llx): %s being asked whether we should report run.",
655                          GetIndexID(),
656                          GetID(),
657                          m_completed_plan_stack.back()->GetName());
658 
659         return m_completed_plan_stack.back()->ShouldReportRun (event_ptr);
660     }
661     else
662     {
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                          GetCurrentPlan()->GetName());
668 
669         return GetCurrentPlan()->ShouldReportRun (event_ptr);
670      }
671 }
672 
673 bool
674 Thread::MatchesSpec (const ThreadSpec *spec)
675 {
676     if (spec == NULL)
677         return true;
678 
679     return spec->ThreadPassesBasicTests(*this);
680 }
681 
682 void
683 Thread::PushPlan (ThreadPlanSP &thread_plan_sp)
684 {
685     if (thread_plan_sp)
686     {
687         // If the thread plan doesn't already have a tracer, give it its parent's tracer:
688         if (!thread_plan_sp->GetThreadPlanTracer())
689             thread_plan_sp->SetThreadPlanTracer(m_plan_stack.back()->GetThreadPlanTracer());
690         m_plan_stack.push_back (thread_plan_sp);
691 
692         thread_plan_sp->DidPush();
693 
694         LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
695         if (log)
696         {
697             StreamString s;
698             thread_plan_sp->GetDescription (&s, lldb::eDescriptionLevelFull);
699             log->Printf("Pushing plan: \"%s\", tid = 0x%4.4llx.",
700                         s.GetData(),
701                         thread_plan_sp->GetThread().GetID());
702         }
703     }
704 }
705 
706 void
707 Thread::PopPlan ()
708 {
709     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
710 
711     if (m_plan_stack.empty())
712         return;
713     else
714     {
715         ThreadPlanSP &plan = m_plan_stack.back();
716         if (log)
717         {
718             log->Printf("Popping plan: \"%s\", tid = 0x%4.4llx.", plan->GetName(), plan->GetThread().GetID());
719         }
720         m_completed_plan_stack.push_back (plan);
721         plan->WillPop();
722         m_plan_stack.pop_back();
723     }
724 }
725 
726 void
727 Thread::DiscardPlan ()
728 {
729     if (m_plan_stack.size() > 1)
730     {
731         ThreadPlanSP &plan = m_plan_stack.back();
732         m_discarded_plan_stack.push_back (plan);
733         plan->WillPop();
734         m_plan_stack.pop_back();
735     }
736 }
737 
738 ThreadPlan *
739 Thread::GetCurrentPlan ()
740 {
741     // There will always be at least the base plan.  If somebody is mucking with a
742     // thread with an empty plan stack, we should assert right away.
743     assert (!m_plan_stack.empty());
744 
745     return m_plan_stack.back().get();
746 }
747 
748 ThreadPlanSP
749 Thread::GetCompletedPlan ()
750 {
751     ThreadPlanSP empty_plan_sp;
752     if (!m_completed_plan_stack.empty())
753     {
754         for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
755         {
756             ThreadPlanSP completed_plan_sp;
757             completed_plan_sp = m_completed_plan_stack[i];
758             if (!completed_plan_sp->GetPrivate ())
759             return completed_plan_sp;
760         }
761     }
762     return empty_plan_sp;
763 }
764 
765 ValueObjectSP
766 Thread::GetReturnValueObject ()
767 {
768     if (!m_completed_plan_stack.empty())
769     {
770         for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
771         {
772             ValueObjectSP return_valobj_sp;
773             return_valobj_sp = m_completed_plan_stack[i]->GetReturnValueObject();
774             if (return_valobj_sp)
775             return return_valobj_sp;
776         }
777     }
778     return ValueObjectSP();
779 }
780 
781 bool
782 Thread::IsThreadPlanDone (ThreadPlan *plan)
783 {
784     if (!m_completed_plan_stack.empty())
785     {
786         for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
787         {
788             if (m_completed_plan_stack[i].get() == plan)
789                 return true;
790         }
791     }
792     return false;
793 }
794 
795 bool
796 Thread::WasThreadPlanDiscarded (ThreadPlan *plan)
797 {
798     if (!m_discarded_plan_stack.empty())
799     {
800         for (int i = m_discarded_plan_stack.size() - 1; i >= 0; i--)
801         {
802             if (m_discarded_plan_stack[i].get() == plan)
803                 return true;
804         }
805     }
806     return false;
807 }
808 
809 ThreadPlan *
810 Thread::GetPreviousPlan (ThreadPlan *current_plan)
811 {
812     if (current_plan == NULL)
813         return NULL;
814 
815     int stack_size = m_completed_plan_stack.size();
816     for (int i = stack_size - 1; i > 0; i--)
817     {
818         if (current_plan == m_completed_plan_stack[i].get())
819             return m_completed_plan_stack[i-1].get();
820     }
821 
822     if (stack_size > 0 && m_completed_plan_stack[0].get() == current_plan)
823     {
824         if (m_plan_stack.size() > 0)
825             return m_plan_stack.back().get();
826         else
827             return NULL;
828     }
829 
830     stack_size = m_plan_stack.size();
831     for (int i = stack_size - 1; i > 0; i--)
832     {
833         if (current_plan == m_plan_stack[i].get())
834             return m_plan_stack[i-1].get();
835     }
836     return NULL;
837 }
838 
839 void
840 Thread::QueueThreadPlan (ThreadPlanSP &thread_plan_sp, bool abort_other_plans)
841 {
842     if (abort_other_plans)
843        DiscardThreadPlans(true);
844 
845     PushPlan (thread_plan_sp);
846 }
847 
848 
849 void
850 Thread::EnableTracer (bool value, bool single_stepping)
851 {
852     int stack_size = m_plan_stack.size();
853     for (int i = 0; i < stack_size; i++)
854     {
855         if (m_plan_stack[i]->GetThreadPlanTracer())
856         {
857             m_plan_stack[i]->GetThreadPlanTracer()->EnableTracing(value);
858             m_plan_stack[i]->GetThreadPlanTracer()->EnableSingleStep(single_stepping);
859         }
860     }
861 }
862 
863 void
864 Thread::SetTracer (lldb::ThreadPlanTracerSP &tracer_sp)
865 {
866     int stack_size = m_plan_stack.size();
867     for (int i = 0; i < stack_size; i++)
868         m_plan_stack[i]->SetThreadPlanTracer(tracer_sp);
869 }
870 
871 void
872 Thread::DiscardThreadPlansUpToPlan (lldb::ThreadPlanSP &up_to_plan_sp)
873 {
874     DiscardThreadPlansUpToPlan (up_to_plan_sp.get());
875 }
876 
877 void
878 Thread::DiscardThreadPlansUpToPlan (ThreadPlan *up_to_plan_ptr)
879 {
880     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
881     if (log)
882     {
883         log->Printf("Discarding thread plans for thread tid = 0x%4.4llx, up to %p", GetID(), up_to_plan_ptr);
884     }
885 
886     int stack_size = m_plan_stack.size();
887 
888     // If the input plan is NULL, discard all plans.  Otherwise make sure this plan is in the
889     // stack, and if so discard up to and including it.
890 
891     if (up_to_plan_ptr == NULL)
892     {
893         for (int i = stack_size - 1; i > 0; i--)
894             DiscardPlan();
895     }
896     else
897     {
898         bool found_it = false;
899         for (int i = stack_size - 1; i > 0; i--)
900         {
901             if (m_plan_stack[i].get() == up_to_plan_ptr)
902                 found_it = true;
903         }
904         if (found_it)
905         {
906             bool last_one = false;
907             for (int i = stack_size - 1; i > 0 && !last_one ; i--)
908             {
909                 if (GetCurrentPlan() == up_to_plan_ptr)
910                     last_one = true;
911                 DiscardPlan();
912             }
913         }
914     }
915     return;
916 }
917 
918 void
919 Thread::DiscardThreadPlans(bool force)
920 {
921     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
922     if (log)
923     {
924         log->Printf("Discarding thread plans for thread (tid = 0x%4.4llx, force %d)", GetID(), force);
925     }
926 
927     if (force)
928     {
929         int stack_size = m_plan_stack.size();
930         for (int i = stack_size - 1; i > 0; i--)
931         {
932             DiscardPlan();
933         }
934         return;
935     }
936 
937     while (1)
938     {
939 
940         int master_plan_idx;
941         bool discard;
942 
943         // Find the first master plan, see if it wants discarding, and if yes discard up to it.
944         for (master_plan_idx = m_plan_stack.size() - 1; master_plan_idx >= 0; master_plan_idx--)
945         {
946             if (m_plan_stack[master_plan_idx]->IsMasterPlan())
947             {
948                 discard = m_plan_stack[master_plan_idx]->OkayToDiscard();
949                 break;
950             }
951         }
952 
953         if (discard)
954         {
955             // First pop all the dependent plans:
956             for (int i = m_plan_stack.size() - 1; i > master_plan_idx; i--)
957             {
958 
959                 // FIXME: Do we need a finalize here, or is the rule that "PrepareForStop"
960                 // for the plan leaves it in a state that it is safe to pop the plan
961                 // with no more notice?
962                 DiscardPlan();
963             }
964 
965             // Now discard the master plan itself.
966             // The bottom-most plan never gets discarded.  "OkayToDiscard" for it means
967             // discard it's dependent plans, but not it...
968             if (master_plan_idx > 0)
969             {
970                 DiscardPlan();
971             }
972         }
973         else
974         {
975             // If the master plan doesn't want to get discarded, then we're done.
976             break;
977         }
978 
979     }
980 }
981 
982 bool
983 Thread::PlanIsBasePlan (ThreadPlan *plan_ptr)
984 {
985     if (plan_ptr->IsBasePlan())
986         return true;
987     else if (m_plan_stack.size() == 0)
988         return false;
989     else
990        return m_plan_stack[0].get() == plan_ptr;
991 }
992 
993 ThreadPlan *
994 Thread::QueueFundamentalPlan (bool abort_other_plans)
995 {
996     ThreadPlanSP thread_plan_sp (new ThreadPlanBase(*this));
997     QueueThreadPlan (thread_plan_sp, abort_other_plans);
998     return thread_plan_sp.get();
999 }
1000 
1001 ThreadPlan *
1002 Thread::QueueThreadPlanForStepSingleInstruction
1003 (
1004     bool step_over,
1005     bool abort_other_plans,
1006     bool stop_other_threads
1007 )
1008 {
1009     ThreadPlanSP thread_plan_sp (new ThreadPlanStepInstruction (*this, step_over, stop_other_threads, eVoteNoOpinion, eVoteNoOpinion));
1010     QueueThreadPlan (thread_plan_sp, abort_other_plans);
1011     return thread_plan_sp.get();
1012 }
1013 
1014 ThreadPlan *
1015 Thread::QueueThreadPlanForStepRange
1016 (
1017     bool abort_other_plans,
1018     StepType type,
1019     const AddressRange &range,
1020     const SymbolContext &addr_context,
1021     lldb::RunMode stop_other_threads,
1022     bool avoid_code_without_debug_info
1023 )
1024 {
1025     ThreadPlanSP thread_plan_sp;
1026     if (type == eStepTypeInto)
1027     {
1028         ThreadPlanStepInRange *plan = new ThreadPlanStepInRange (*this, range, addr_context, stop_other_threads);
1029         if (avoid_code_without_debug_info)
1030             plan->GetFlags().Set (ThreadPlanShouldStopHere::eAvoidNoDebug);
1031         else
1032             plan->GetFlags().Clear (ThreadPlanShouldStopHere::eAvoidNoDebug);
1033         thread_plan_sp.reset (plan);
1034     }
1035     else
1036         thread_plan_sp.reset (new ThreadPlanStepOverRange (*this, range, addr_context, stop_other_threads));
1037 
1038     QueueThreadPlan (thread_plan_sp, abort_other_plans);
1039     return thread_plan_sp.get();
1040 }
1041 
1042 
1043 ThreadPlan *
1044 Thread::QueueThreadPlanForStepOverBreakpointPlan (bool abort_other_plans)
1045 {
1046     ThreadPlanSP thread_plan_sp (new ThreadPlanStepOverBreakpoint (*this));
1047     QueueThreadPlan (thread_plan_sp, abort_other_plans);
1048     return thread_plan_sp.get();
1049 }
1050 
1051 ThreadPlan *
1052 Thread::QueueThreadPlanForStepOut
1053 (
1054     bool abort_other_plans,
1055     SymbolContext *addr_context,
1056     bool first_insn,
1057     bool stop_other_threads,
1058     Vote stop_vote,
1059     Vote run_vote,
1060     uint32_t frame_idx
1061 )
1062 {
1063     ThreadPlanSP thread_plan_sp (new ThreadPlanStepOut (*this,
1064                                                         addr_context,
1065                                                         first_insn,
1066                                                         stop_other_threads,
1067                                                         stop_vote,
1068                                                         run_vote,
1069                                                         frame_idx));
1070 
1071     if (thread_plan_sp->ValidatePlan(NULL))
1072     {
1073         QueueThreadPlan (thread_plan_sp, abort_other_plans);
1074         return thread_plan_sp.get();
1075     }
1076     else
1077     {
1078         return NULL;
1079     }
1080 }
1081 
1082 ThreadPlan *
1083 Thread::QueueThreadPlanForStepThrough (StackID &return_stack_id, bool abort_other_plans, bool stop_other_threads)
1084 {
1085     ThreadPlanSP thread_plan_sp(new ThreadPlanStepThrough (*this, return_stack_id, stop_other_threads));
1086     if (!thread_plan_sp || !thread_plan_sp->ValidatePlan (NULL))
1087         return NULL;
1088 
1089     QueueThreadPlan (thread_plan_sp, abort_other_plans);
1090     return thread_plan_sp.get();
1091 }
1092 
1093 ThreadPlan *
1094 Thread::QueueThreadPlanForCallFunction (bool abort_other_plans,
1095                                         Address& function,
1096                                         lldb::addr_t arg,
1097                                         bool stop_other_threads,
1098                                         bool discard_on_error)
1099 {
1100     ThreadPlanSP thread_plan_sp (new ThreadPlanCallFunction (*this, function, ClangASTType(), arg, stop_other_threads, discard_on_error));
1101     QueueThreadPlan (thread_plan_sp, abort_other_plans);
1102     return thread_plan_sp.get();
1103 }
1104 
1105 ThreadPlan *
1106 Thread::QueueThreadPlanForRunToAddress (bool abort_other_plans,
1107                                         Address &target_addr,
1108                                         bool stop_other_threads)
1109 {
1110     ThreadPlanSP thread_plan_sp (new ThreadPlanRunToAddress (*this, target_addr, stop_other_threads));
1111     QueueThreadPlan (thread_plan_sp, abort_other_plans);
1112     return thread_plan_sp.get();
1113 }
1114 
1115 ThreadPlan *
1116 Thread::QueueThreadPlanForStepUntil (bool abort_other_plans,
1117                                      lldb::addr_t *address_list,
1118                                      size_t num_addresses,
1119                                      bool stop_other_threads,
1120                                      uint32_t frame_idx)
1121 {
1122     ThreadPlanSP thread_plan_sp (new ThreadPlanStepUntil (*this, address_list, num_addresses, stop_other_threads, frame_idx));
1123     QueueThreadPlan (thread_plan_sp, abort_other_plans);
1124     return thread_plan_sp.get();
1125 
1126 }
1127 
1128 uint32_t
1129 Thread::GetIndexID () const
1130 {
1131     return m_index_id;
1132 }
1133 
1134 void
1135 Thread::DumpThreadPlans (lldb_private::Stream *s) const
1136 {
1137     uint32_t stack_size = m_plan_stack.size();
1138     int i;
1139     s->Indent();
1140     s->Printf ("Plan Stack for thread #%u: tid = 0x%4.4llx, stack_size = %d\n", GetIndexID(), GetID(), stack_size);
1141     for (i = stack_size - 1; i >= 0; i--)
1142     {
1143         s->IndentMore();
1144         s->Indent();
1145         s->Printf ("Element %d: ", i);
1146         m_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1147         s->EOL();
1148         s->IndentLess();
1149     }
1150 
1151     stack_size = m_completed_plan_stack.size();
1152     if (stack_size > 0)
1153     {
1154         s->Indent();
1155         s->Printf ("Completed Plan Stack: %d elements.\n", stack_size);
1156         for (i = stack_size - 1; i >= 0; i--)
1157         {
1158             s->IndentMore();
1159             s->Indent();
1160             s->Printf ("Element %d: ", i);
1161             m_completed_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1162             s->EOL();
1163             s->IndentLess();
1164         }
1165     }
1166 
1167     stack_size = m_discarded_plan_stack.size();
1168     if (stack_size > 0)
1169     {
1170         s->Indent();
1171         s->Printf ("Discarded Plan Stack: %d elements.\n", stack_size);
1172         for (i = stack_size - 1; i >= 0; i--)
1173         {
1174             s->IndentMore();
1175             s->Indent();
1176             s->Printf ("Element %d: ", i);
1177             m_discarded_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1178             s->EOL();
1179             s->IndentLess();
1180         }
1181     }
1182 
1183 }
1184 
1185 TargetSP
1186 Thread::CalculateTarget ()
1187 {
1188     TargetSP target_sp;
1189     ProcessSP process_sp(GetProcess());
1190     if (process_sp)
1191         target_sp = process_sp->CalculateTarget();
1192     return target_sp;
1193 
1194 }
1195 
1196 ProcessSP
1197 Thread::CalculateProcess ()
1198 {
1199     return GetProcess();
1200 }
1201 
1202 ThreadSP
1203 Thread::CalculateThread ()
1204 {
1205     return shared_from_this();
1206 }
1207 
1208 StackFrameSP
1209 Thread::CalculateStackFrame ()
1210 {
1211     return StackFrameSP();
1212 }
1213 
1214 void
1215 Thread::CalculateExecutionContext (ExecutionContext &exe_ctx)
1216 {
1217     exe_ctx.SetContext (shared_from_this());
1218 }
1219 
1220 
1221 StackFrameListSP
1222 Thread::GetStackFrameList ()
1223 {
1224     StackFrameListSP frame_list_sp;
1225     Mutex::Locker locker(m_frame_mutex);
1226     if (m_curr_frames_sp)
1227     {
1228         frame_list_sp = m_curr_frames_sp;
1229     }
1230     else
1231     {
1232         frame_list_sp.reset(new StackFrameList (*this, m_prev_frames_sp, true));
1233         m_curr_frames_sp = frame_list_sp;
1234     }
1235     return frame_list_sp;
1236 }
1237 
1238 void
1239 Thread::ClearStackFrames ()
1240 {
1241     Mutex::Locker locker(m_frame_mutex);
1242 
1243     // Only store away the old "reference" StackFrameList if we got all its frames:
1244     // FIXME: At some point we can try to splice in the frames we have fetched into
1245     // the new frame as we make it, but let's not try that now.
1246     if (m_curr_frames_sp && m_curr_frames_sp->GetAllFramesFetched())
1247         m_prev_frames_sp.swap (m_curr_frames_sp);
1248     m_curr_frames_sp.reset();
1249 }
1250 
1251 lldb::StackFrameSP
1252 Thread::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx)
1253 {
1254     return GetStackFrameList()->GetFrameWithConcreteFrameIndex (unwind_idx);
1255 }
1256 
1257 void
1258 Thread::DumpUsingSettingsFormat (Stream &strm, uint32_t frame_idx)
1259 {
1260     ExecutionContext exe_ctx (shared_from_this());
1261     Process *process = exe_ctx.GetProcessPtr();
1262     if (process == NULL)
1263         return;
1264 
1265     StackFrameSP frame_sp;
1266     SymbolContext frame_sc;
1267     if (frame_idx != LLDB_INVALID_INDEX32)
1268     {
1269         frame_sp = GetStackFrameAtIndex (frame_idx);
1270         if (frame_sp)
1271         {
1272             exe_ctx.SetFrameSP(frame_sp);
1273             frame_sc = frame_sp->GetSymbolContext(eSymbolContextEverything);
1274         }
1275     }
1276 
1277     const char *thread_format = exe_ctx.GetTargetRef().GetDebugger().GetThreadFormat();
1278     assert (thread_format);
1279     const char *end = NULL;
1280     Debugger::FormatPrompt (thread_format,
1281                             frame_sp ? &frame_sc : NULL,
1282                             &exe_ctx,
1283                             NULL,
1284                             strm,
1285                             &end);
1286 }
1287 
1288 void
1289 Thread::SettingsInitialize ()
1290 {
1291 }
1292 
1293 void
1294 Thread::SettingsTerminate ()
1295 {
1296 }
1297 
1298 lldb::StackFrameSP
1299 Thread::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr)
1300 {
1301     return GetStackFrameList()->GetStackFrameSPForStackFramePtr (stack_frame_ptr);
1302 }
1303 
1304 const char *
1305 Thread::StopReasonAsCString (lldb::StopReason reason)
1306 {
1307     switch (reason)
1308     {
1309     case eStopReasonInvalid:      return "invalid";
1310     case eStopReasonNone:         return "none";
1311     case eStopReasonTrace:        return "trace";
1312     case eStopReasonBreakpoint:   return "breakpoint";
1313     case eStopReasonWatchpoint:   return "watchpoint";
1314     case eStopReasonSignal:       return "signal";
1315     case eStopReasonException:    return "exception";
1316     case eStopReasonPlanComplete: return "plan complete";
1317     }
1318 
1319 
1320     static char unknown_state_string[64];
1321     snprintf(unknown_state_string, sizeof (unknown_state_string), "StopReason = %i", reason);
1322     return unknown_state_string;
1323 }
1324 
1325 const char *
1326 Thread::RunModeAsCString (lldb::RunMode mode)
1327 {
1328     switch (mode)
1329     {
1330     case eOnlyThisThread:     return "only this thread";
1331     case eAllThreads:         return "all threads";
1332     case eOnlyDuringStepping: return "only during stepping";
1333     }
1334 
1335     static char unknown_state_string[64];
1336     snprintf(unknown_state_string, sizeof (unknown_state_string), "RunMode = %i", mode);
1337     return unknown_state_string;
1338 }
1339 
1340 size_t
1341 Thread::GetStatus (Stream &strm, uint32_t start_frame, uint32_t num_frames, uint32_t num_frames_with_source)
1342 {
1343     ExecutionContext exe_ctx (shared_from_this());
1344     Target *target = exe_ctx.GetTargetPtr();
1345     Process *process = exe_ctx.GetProcessPtr();
1346     size_t num_frames_shown = 0;
1347     strm.Indent();
1348     bool is_selected = false;
1349     if (process)
1350     {
1351         if (process->GetThreadList().GetSelectedThread().get() == this)
1352             is_selected = true;
1353     }
1354     strm.Printf("%c ", is_selected ? '*' : ' ');
1355     if (target && target->GetDebugger().GetUseExternalEditor())
1356     {
1357         StackFrameSP frame_sp = GetStackFrameAtIndex(start_frame);
1358         if (frame_sp)
1359         {
1360             SymbolContext frame_sc(frame_sp->GetSymbolContext (eSymbolContextLineEntry));
1361             if (frame_sc.line_entry.line != 0 && frame_sc.line_entry.file)
1362             {
1363                 Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line);
1364             }
1365         }
1366     }
1367 
1368     DumpUsingSettingsFormat (strm, start_frame);
1369 
1370     if (num_frames > 0)
1371     {
1372         strm.IndentMore();
1373 
1374         const bool show_frame_info = true;
1375         strm.IndentMore ();
1376         num_frames_shown = GetStackFrameList ()->GetStatus (strm,
1377                                                             start_frame,
1378                                                             num_frames,
1379                                                             show_frame_info,
1380                                                             num_frames_with_source);
1381         strm.IndentLess();
1382         strm.IndentLess();
1383     }
1384     return num_frames_shown;
1385 }
1386 
1387 size_t
1388 Thread::GetStackFrameStatus (Stream& strm,
1389                              uint32_t first_frame,
1390                              uint32_t num_frames,
1391                              bool show_frame_info,
1392                              uint32_t num_frames_with_source)
1393 {
1394     return GetStackFrameList()->GetStatus (strm,
1395                                            first_frame,
1396                                            num_frames,
1397                                            show_frame_info,
1398                                            num_frames_with_source);
1399 }
1400 
1401 bool
1402 Thread::SaveFrameZeroState (RegisterCheckpoint &checkpoint)
1403 {
1404     lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
1405     if (frame_sp)
1406     {
1407         checkpoint.SetStackID(frame_sp->GetStackID());
1408         return frame_sp->GetRegisterContext()->ReadAllRegisterValues (checkpoint.GetData());
1409     }
1410     return false;
1411 }
1412 
1413 bool
1414 Thread::RestoreSaveFrameZero (const RegisterCheckpoint &checkpoint)
1415 {
1416     lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
1417     if (frame_sp)
1418     {
1419         bool ret = frame_sp->GetRegisterContext()->WriteAllRegisterValues (checkpoint.GetData());
1420 
1421         // Clear out all stack frames as our world just changed.
1422         ClearStackFrames();
1423         frame_sp->GetRegisterContext()->InvalidateIfNeeded(true);
1424 
1425         return ret;
1426     }
1427     return false;
1428 }
1429 
1430 Unwind *
1431 Thread::GetUnwinder ()
1432 {
1433     if (m_unwinder_ap.get() == NULL)
1434     {
1435         const ArchSpec target_arch (CalculateTarget()->GetArchitecture ());
1436         const llvm::Triple::ArchType machine = target_arch.GetMachine();
1437         switch (machine)
1438         {
1439             case llvm::Triple::x86_64:
1440             case llvm::Triple::x86:
1441             case llvm::Triple::arm:
1442             case llvm::Triple::thumb:
1443                 m_unwinder_ap.reset (new UnwindLLDB (*this));
1444                 break;
1445 
1446             default:
1447                 if (target_arch.GetTriple().getVendor() == llvm::Triple::Apple)
1448                     m_unwinder_ap.reset (new UnwindMacOSXFrameBackchain (*this));
1449                 break;
1450         }
1451     }
1452     return m_unwinder_ap.get();
1453 }
1454 
1455 
1456 void
1457 Thread::Flush ()
1458 {
1459     ClearStackFrames ();
1460     m_reg_context_sp.reset();
1461 }
1462