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