1 //===-- SBThread.cpp --------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "lldb/lldb-python.h"
11 
12 #include "lldb/API/SBThread.h"
13 
14 #include "lldb/API/SBSymbolContext.h"
15 #include "lldb/API/SBFileSpec.h"
16 #include "lldb/API/SBStream.h"
17 #include "lldb/Breakpoint/BreakpointLocation.h"
18 #include "lldb/Core/Debugger.h"
19 #include "lldb/Core/State.h"
20 #include "lldb/Core/Stream.h"
21 #include "lldb/Core/StreamFile.h"
22 #include "lldb/Core/StructuredData.h"
23 #include "lldb/Interpreter/CommandInterpreter.h"
24 #include "lldb/Target/SystemRuntime.h"
25 #include "lldb/Target/Thread.h"
26 #include "lldb/Target/Process.h"
27 #include "lldb/Target/Queue.h"
28 #include "lldb/Symbol/SymbolContext.h"
29 #include "lldb/Symbol/CompileUnit.h"
30 #include "lldb/Target/StopInfo.h"
31 #include "lldb/Target/Target.h"
32 #include "lldb/Target/ThreadPlan.h"
33 #include "lldb/Target/ThreadPlanStepInstruction.h"
34 #include "lldb/Target/ThreadPlanStepOut.h"
35 #include "lldb/Target/ThreadPlanStepRange.h"
36 #include "lldb/Target/ThreadPlanStepInRange.h"
37 
38 
39 #include "lldb/API/SBAddress.h"
40 #include "lldb/API/SBDebugger.h"
41 #include "lldb/API/SBEvent.h"
42 #include "lldb/API/SBFrame.h"
43 #include "lldb/API/SBProcess.h"
44 #include "lldb/API/SBThreadPlan.h"
45 #include "lldb/API/SBValue.h"
46 
47 using namespace lldb;
48 using namespace lldb_private;
49 
50 const char *
51 SBThread::GetBroadcasterClassName ()
52 {
53     return Thread::GetStaticBroadcasterClass().AsCString();
54 }
55 
56 //----------------------------------------------------------------------
57 // Constructors
58 //----------------------------------------------------------------------
59 SBThread::SBThread () :
60     m_opaque_sp (new ExecutionContextRef())
61 {
62 }
63 
64 SBThread::SBThread (const ThreadSP& lldb_object_sp) :
65     m_opaque_sp (new ExecutionContextRef(lldb_object_sp))
66 {
67 }
68 
69 SBThread::SBThread (const SBThread &rhs) :
70     m_opaque_sp (new ExecutionContextRef(*rhs.m_opaque_sp))
71 {
72 
73 }
74 
75 //----------------------------------------------------------------------
76 // Assignment operator
77 //----------------------------------------------------------------------
78 
79 const lldb::SBThread &
80 SBThread::operator = (const SBThread &rhs)
81 {
82     if (this != &rhs)
83         *m_opaque_sp = *rhs.m_opaque_sp;
84     return *this;
85 }
86 
87 //----------------------------------------------------------------------
88 // Destructor
89 //----------------------------------------------------------------------
90 SBThread::~SBThread()
91 {
92 }
93 
94 lldb::SBQueue
95 SBThread::GetQueue () const
96 {
97     SBQueue sb_queue;
98     QueueSP queue_sp;
99     Mutex::Locker api_locker;
100     ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
101 
102     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
103     if (exe_ctx.HasThreadScope())
104     {
105         Process::StopLocker stop_locker;
106         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
107         {
108             queue_sp = exe_ctx.GetThreadPtr()->GetQueue();
109             if (queue_sp)
110             {
111                 sb_queue.SetQueue (queue_sp);
112             }
113         }
114         else
115         {
116             if (log)
117                 log->Printf ("SBThread(%p)::GetQueueKind() => error: process is running",
118                              static_cast<void*>(exe_ctx.GetThreadPtr()));
119         }
120     }
121 
122     if (log)
123         log->Printf ("SBThread(%p)::GetQueueKind () => SBQueue(%p)",
124                      static_cast<void*>(exe_ctx.GetThreadPtr()), static_cast<void*>(queue_sp.get()));
125 
126     return sb_queue;
127 }
128 
129 
130 bool
131 SBThread::IsValid() const
132 {
133     return m_opaque_sp->GetThreadSP().get() != NULL;
134 }
135 
136 void
137 SBThread::Clear ()
138 {
139     m_opaque_sp->Clear();
140 }
141 
142 
143 StopReason
144 SBThread::GetStopReason()
145 {
146     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
147 
148     StopReason reason = eStopReasonInvalid;
149     Mutex::Locker api_locker;
150     ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
151 
152     if (exe_ctx.HasThreadScope())
153     {
154         Process::StopLocker stop_locker;
155         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
156         {
157             return exe_ctx.GetThreadPtr()->GetStopReason();
158         }
159         else
160         {
161             if (log)
162                 log->Printf ("SBThread(%p)::GetStopReason() => error: process is running",
163                              static_cast<void*>(exe_ctx.GetThreadPtr()));
164         }
165     }
166 
167     if (log)
168         log->Printf ("SBThread(%p)::GetStopReason () => %s",
169                      static_cast<void*>(exe_ctx.GetThreadPtr()),
170                      Thread::StopReasonAsCString (reason));
171 
172     return reason;
173 }
174 
175 size_t
176 SBThread::GetStopReasonDataCount ()
177 {
178     Mutex::Locker api_locker;
179     ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
180 
181     if (exe_ctx.HasThreadScope())
182     {
183         Process::StopLocker stop_locker;
184         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
185         {
186             StopInfoSP stop_info_sp = exe_ctx.GetThreadPtr()->GetStopInfo ();
187             if (stop_info_sp)
188             {
189                 StopReason reason = stop_info_sp->GetStopReason();
190                 switch (reason)
191                 {
192                 case eStopReasonInvalid:
193                 case eStopReasonNone:
194                 case eStopReasonTrace:
195                 case eStopReasonExec:
196                 case eStopReasonPlanComplete:
197                 case eStopReasonThreadExiting:
198                     // There is no data for these stop reasons.
199                     return 0;
200 
201                 case eStopReasonBreakpoint:
202                     {
203                         break_id_t site_id = stop_info_sp->GetValue();
204                         lldb::BreakpointSiteSP bp_site_sp (exe_ctx.GetProcessPtr()->GetBreakpointSiteList().FindByID (site_id));
205                         if (bp_site_sp)
206                             return bp_site_sp->GetNumberOfOwners () * 2;
207                         else
208                             return 0; // Breakpoint must have cleared itself...
209                     }
210                     break;
211 
212                 case eStopReasonWatchpoint:
213                     return 1;
214 
215                 case eStopReasonSignal:
216                     return 1;
217 
218                 case eStopReasonException:
219                     return 1;
220                 }
221             }
222         }
223         else
224         {
225             Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
226             if (log)
227                 log->Printf ("SBThread(%p)::GetStopReasonDataCount() => error: process is running",
228                              static_cast<void*>(exe_ctx.GetThreadPtr()));
229         }
230     }
231     return 0;
232 }
233 
234 uint64_t
235 SBThread::GetStopReasonDataAtIndex (uint32_t idx)
236 {
237     Mutex::Locker api_locker;
238     ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
239 
240     if (exe_ctx.HasThreadScope())
241     {
242         Process::StopLocker stop_locker;
243         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
244         {
245             Thread *thread = exe_ctx.GetThreadPtr();
246             StopInfoSP stop_info_sp = thread->GetStopInfo ();
247             if (stop_info_sp)
248             {
249                 StopReason reason = stop_info_sp->GetStopReason();
250                 switch (reason)
251                 {
252                 case eStopReasonInvalid:
253                 case eStopReasonNone:
254                 case eStopReasonTrace:
255                 case eStopReasonExec:
256                 case eStopReasonPlanComplete:
257                 case eStopReasonThreadExiting:
258                     // There is no data for these stop reasons.
259                     return 0;
260 
261                 case eStopReasonBreakpoint:
262                     {
263                         break_id_t site_id = stop_info_sp->GetValue();
264                         lldb::BreakpointSiteSP bp_site_sp (exe_ctx.GetProcessPtr()->GetBreakpointSiteList().FindByID (site_id));
265                         if (bp_site_sp)
266                         {
267                             uint32_t bp_index = idx / 2;
268                             BreakpointLocationSP bp_loc_sp (bp_site_sp->GetOwnerAtIndex (bp_index));
269                             if (bp_loc_sp)
270                             {
271                                 if (idx & 1)
272                                 {
273                                     // Odd idx, return the breakpoint location ID
274                                     return bp_loc_sp->GetID();
275                                 }
276                                 else
277                                 {
278                                     // Even idx, return the breakpoint ID
279                                     return bp_loc_sp->GetBreakpoint().GetID();
280                                 }
281                             }
282                         }
283                         return LLDB_INVALID_BREAK_ID;
284                     }
285                     break;
286 
287                 case eStopReasonWatchpoint:
288                     return stop_info_sp->GetValue();
289 
290                 case eStopReasonSignal:
291                     return stop_info_sp->GetValue();
292 
293                 case eStopReasonException:
294                     return stop_info_sp->GetValue();
295                 }
296             }
297         }
298         else
299         {
300             Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
301             if (log)
302                 log->Printf ("SBThread(%p)::GetStopReasonDataAtIndex() => error: process is running",
303                              static_cast<void*>(exe_ctx.GetThreadPtr()));
304         }
305     }
306     return 0;
307 }
308 
309 size_t
310 SBThread::GetStopDescription (char *dst, size_t dst_len)
311 {
312     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
313 
314     Mutex::Locker api_locker;
315     ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
316 
317     if (exe_ctx.HasThreadScope())
318     {
319         Process::StopLocker stop_locker;
320         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
321         {
322 
323             StopInfoSP stop_info_sp = exe_ctx.GetThreadPtr()->GetStopInfo ();
324             if (stop_info_sp)
325             {
326                 const char *stop_desc = stop_info_sp->GetDescription();
327                 if (stop_desc)
328                 {
329                     if (log)
330                         log->Printf ("SBThread(%p)::GetStopDescription (dst, dst_len) => \"%s\"",
331                                      static_cast<void*>(exe_ctx.GetThreadPtr()),
332                                      stop_desc);
333                     if (dst)
334                         return ::snprintf (dst, dst_len, "%s", stop_desc);
335                     else
336                     {
337                         // NULL dst passed in, return the length needed to contain the description
338                         return ::strlen (stop_desc) + 1; // Include the NULL byte for size
339                     }
340                 }
341                 else
342                 {
343                     size_t stop_desc_len = 0;
344                     switch (stop_info_sp->GetStopReason())
345                     {
346                     case eStopReasonTrace:
347                     case eStopReasonPlanComplete:
348                         {
349                             static char trace_desc[] = "step";
350                             stop_desc = trace_desc;
351                             stop_desc_len = sizeof(trace_desc); // Include the NULL byte for size
352                         }
353                         break;
354 
355                     case eStopReasonBreakpoint:
356                         {
357                             static char bp_desc[] = "breakpoint hit";
358                             stop_desc = bp_desc;
359                             stop_desc_len = sizeof(bp_desc); // Include the NULL byte for size
360                         }
361                         break;
362 
363                     case eStopReasonWatchpoint:
364                         {
365                             static char wp_desc[] = "watchpoint hit";
366                             stop_desc = wp_desc;
367                             stop_desc_len = sizeof(wp_desc); // Include the NULL byte for size
368                         }
369                         break;
370 
371                     case eStopReasonSignal:
372                         {
373                             stop_desc = exe_ctx.GetProcessPtr()->GetUnixSignals ().GetSignalAsCString (stop_info_sp->GetValue());
374                             if (stop_desc == NULL || stop_desc[0] == '\0')
375                             {
376                                 static char signal_desc[] = "signal";
377                                 stop_desc = signal_desc;
378                                 stop_desc_len = sizeof(signal_desc); // Include the NULL byte for size
379                             }
380                         }
381                         break;
382 
383                     case eStopReasonException:
384                         {
385                             char exc_desc[] = "exception";
386                             stop_desc = exc_desc;
387                             stop_desc_len = sizeof(exc_desc); // Include the NULL byte for size
388                         }
389                         break;
390 
391                     case eStopReasonExec:
392                         {
393                             char exc_desc[] = "exec";
394                             stop_desc = exc_desc;
395                             stop_desc_len = sizeof(exc_desc); // Include the NULL byte for size
396                         }
397                         break;
398 
399                     case eStopReasonThreadExiting:
400                         {
401                             char limbo_desc[] = "thread exiting";
402                             stop_desc = limbo_desc;
403                             stop_desc_len = sizeof(limbo_desc);
404                         }
405                         break;
406                     default:
407                         break;
408                     }
409 
410                     if (stop_desc && stop_desc[0])
411                     {
412                         if (log)
413                             log->Printf ("SBThread(%p)::GetStopDescription (dst, dst_len) => '%s'",
414                                          static_cast<void*>(exe_ctx.GetThreadPtr()),
415                                          stop_desc);
416 
417                         if (dst)
418                             return ::snprintf (dst, dst_len, "%s", stop_desc) + 1; // Include the NULL byte
419 
420                         if (stop_desc_len == 0)
421                             stop_desc_len = ::strlen (stop_desc) + 1; // Include the NULL byte
422 
423                         return stop_desc_len;
424                     }
425                 }
426             }
427         }
428         else
429         {
430             Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
431             if (log)
432                 log->Printf ("SBThread(%p)::GetStopDescription() => error: process is running",
433                              static_cast<void*>(exe_ctx.GetThreadPtr()));
434         }
435     }
436     if (dst)
437         *dst = 0;
438     return 0;
439 }
440 
441 SBValue
442 SBThread::GetStopReturnValue ()
443 {
444     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
445     ValueObjectSP return_valobj_sp;
446     Mutex::Locker api_locker;
447     ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
448 
449     if (exe_ctx.HasThreadScope())
450     {
451         Process::StopLocker stop_locker;
452         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
453         {
454             StopInfoSP stop_info_sp = exe_ctx.GetThreadPtr()->GetStopInfo ();
455             if (stop_info_sp)
456             {
457                 return_valobj_sp = StopInfo::GetReturnValueObject (stop_info_sp);
458             }
459         }
460         else
461         {
462             if (log)
463                 log->Printf ("SBThread(%p)::GetStopReturnValue() => error: process is running",
464                              static_cast<void*>(exe_ctx.GetThreadPtr()));
465         }
466     }
467 
468     if (log)
469         log->Printf ("SBThread(%p)::GetStopReturnValue () => %s",
470                      static_cast<void*>(exe_ctx.GetThreadPtr()),
471                      return_valobj_sp.get()
472                         ? return_valobj_sp->GetValueAsCString()
473                         : "<no return value>");
474 
475     return SBValue (return_valobj_sp);
476 }
477 
478 void
479 SBThread::SetThread (const ThreadSP& lldb_object_sp)
480 {
481     m_opaque_sp->SetThreadSP (lldb_object_sp);
482 }
483 
484 lldb::tid_t
485 SBThread::GetThreadID () const
486 {
487     ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
488     if (thread_sp)
489         return thread_sp->GetID();
490     return LLDB_INVALID_THREAD_ID;
491 }
492 
493 uint32_t
494 SBThread::GetIndexID () const
495 {
496     ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
497     if (thread_sp)
498         return thread_sp->GetIndexID();
499     return LLDB_INVALID_INDEX32;
500 }
501 
502 const char *
503 SBThread::GetName () const
504 {
505     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
506     const char *name = NULL;
507     Mutex::Locker api_locker;
508     ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
509 
510     if (exe_ctx.HasThreadScope())
511     {
512         Process::StopLocker stop_locker;
513         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
514         {
515             name = exe_ctx.GetThreadPtr()->GetName();
516         }
517         else
518         {
519             if (log)
520                 log->Printf ("SBThread(%p)::GetName() => error: process is running",
521                              static_cast<void*>(exe_ctx.GetThreadPtr()));
522         }
523     }
524 
525     if (log)
526         log->Printf ("SBThread(%p)::GetName () => %s",
527                      static_cast<void*>(exe_ctx.GetThreadPtr()),
528                      name ? name : "NULL");
529 
530     return name;
531 }
532 
533 const char *
534 SBThread::GetQueueName () const
535 {
536     const char *name = NULL;
537     Mutex::Locker api_locker;
538     ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
539 
540     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
541     if (exe_ctx.HasThreadScope())
542     {
543         Process::StopLocker stop_locker;
544         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
545         {
546             name = exe_ctx.GetThreadPtr()->GetQueueName();
547         }
548         else
549         {
550             if (log)
551                 log->Printf ("SBThread(%p)::GetQueueName() => error: process is running",
552                              static_cast<void*>(exe_ctx.GetThreadPtr()));
553         }
554     }
555 
556     if (log)
557         log->Printf ("SBThread(%p)::GetQueueName () => %s",
558                      static_cast<void*>(exe_ctx.GetThreadPtr()),
559                      name ? name : "NULL");
560 
561     return name;
562 }
563 
564 lldb::queue_id_t
565 SBThread::GetQueueID () const
566 {
567     queue_id_t id = LLDB_INVALID_QUEUE_ID;
568     Mutex::Locker api_locker;
569     ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
570 
571     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
572     if (exe_ctx.HasThreadScope())
573     {
574         Process::StopLocker stop_locker;
575         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
576         {
577             id = exe_ctx.GetThreadPtr()->GetQueueID();
578         }
579         else
580         {
581             if (log)
582                 log->Printf ("SBThread(%p)::GetQueueID() => error: process is running",
583                              static_cast<void*>(exe_ctx.GetThreadPtr()));
584         }
585     }
586 
587     if (log)
588         log->Printf ("SBThread(%p)::GetQueueID () => 0x%" PRIx64,
589                      static_cast<void*>(exe_ctx.GetThreadPtr()), id);
590 
591     return id;
592 }
593 
594 bool
595 SBThread::GetInfoItemByPathAsString (const char *path, SBStream &strm)
596 {
597     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
598     bool success = false;
599     Mutex::Locker api_locker;
600     ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
601 
602     if (exe_ctx.HasThreadScope())
603     {
604         Process::StopLocker stop_locker;
605         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
606         {
607             Thread *thread = exe_ctx.GetThreadPtr();
608             StructuredData::ObjectSP info_root_sp = thread->GetExtendedInfo();
609             if (info_root_sp)
610             {
611                 StructuredData::ObjectSP node = info_root_sp->GetObjectForDotSeparatedPath (path);
612                 if (node)
613                 {
614                     if (node->GetType() == StructuredData::Type::eTypeString)
615                     {
616                         strm.Printf ("%s", node->GetAsString()->GetValue().c_str());
617                         success = true;
618                     }
619                     if (node->GetType() == StructuredData::Type::eTypeInteger)
620                     {
621                         strm.Printf ("0x%" PRIx64, node->GetAsInteger()->GetValue());
622                         success = true;
623                     }
624                     if (node->GetType() == StructuredData::Type::eTypeFloat)
625                     {
626                         strm.Printf ("0x%f", node->GetAsFloat()->GetValue());
627                         success = true;
628                     }
629                     if (node->GetType() == StructuredData::Type::eTypeBoolean)
630                     {
631                         if (node->GetAsBoolean()->GetValue() == true)
632                             strm.Printf ("true");
633                         else
634                             strm.Printf ("false");
635                         success = true;
636                     }
637                     if (node->GetType() == StructuredData::Type::eTypeNull)
638                     {
639                         strm.Printf ("null");
640                         success = true;
641                     }
642                 }
643             }
644         }
645         else
646         {
647             if (log)
648                 log->Printf ("SBThread(%p)::GetInfoItemByPathAsString() => error: process is running",
649                              static_cast<void*>(exe_ctx.GetThreadPtr()));
650         }
651     }
652 
653     if (log)
654         log->Printf ("SBThread(%p)::GetInfoItemByPathAsString () => %s",
655                      static_cast<void*>(exe_ctx.GetThreadPtr()),
656                      strm.GetData());
657 
658     return success;
659 }
660 
661 
662 SBError
663 SBThread::ResumeNewPlan (ExecutionContext &exe_ctx, ThreadPlan *new_plan)
664 {
665     SBError sb_error;
666 
667     Process *process = exe_ctx.GetProcessPtr();
668     if (!process)
669     {
670         sb_error.SetErrorString("No process in SBThread::ResumeNewPlan");
671         return sb_error;
672     }
673 
674     Thread *thread = exe_ctx.GetThreadPtr();
675     if (!thread)
676     {
677         sb_error.SetErrorString("No thread in SBThread::ResumeNewPlan");
678         return sb_error;
679     }
680 
681     // User level plans should be Master Plans so they can be interrupted, other plans executed, and
682     // then a "continue" will resume the plan.
683     if (new_plan != NULL)
684     {
685         new_plan->SetIsMasterPlan(true);
686         new_plan->SetOkayToDiscard(false);
687     }
688 
689     // Why do we need to set the current thread by ID here???
690     process->GetThreadList().SetSelectedThreadByID (thread->GetID());
691     sb_error.ref() = process->Resume();
692 
693     if (sb_error.Success())
694     {
695         // If we are doing synchronous mode, then wait for the
696         // process to stop yet again!
697         if (process->GetTarget().GetDebugger().GetAsyncExecution () == false)
698             process->WaitForProcessToStop (NULL);
699     }
700 
701     return sb_error;
702 }
703 
704 void
705 SBThread::StepOver (lldb::RunMode stop_other_threads)
706 {
707     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
708 
709     Mutex::Locker api_locker;
710     ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
711 
712 
713     if (log)
714         log->Printf ("SBThread(%p)::StepOver (stop_other_threads='%s')",
715                      static_cast<void*>(exe_ctx.GetThreadPtr()),
716                      Thread::RunModeAsCString (stop_other_threads));
717 
718     if (exe_ctx.HasThreadScope())
719     {
720         Thread *thread = exe_ctx.GetThreadPtr();
721         bool abort_other_plans = false;
722         StackFrameSP frame_sp(thread->GetStackFrameAtIndex (0));
723 
724         ThreadPlanSP new_plan_sp;
725         if (frame_sp)
726         {
727             if (frame_sp->HasDebugInformation ())
728             {
729                 const LazyBool avoid_no_debug = eLazyBoolCalculate;
730                 SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
731                 new_plan_sp = thread->QueueThreadPlanForStepOverRange (abort_other_plans,
732                                                                     sc.line_entry.range,
733                                                                     sc,
734                                                                     stop_other_threads,
735                                                                     avoid_no_debug);
736             }
737             else
738             {
739                 new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction (true,
740                                                                                abort_other_plans,
741                                                                                stop_other_threads);
742             }
743         }
744 
745         // This returns an error, we should use it!
746         ResumeNewPlan (exe_ctx, new_plan_sp.get());
747     }
748 }
749 
750 void
751 SBThread::StepInto (lldb::RunMode stop_other_threads)
752 {
753     StepInto (NULL, stop_other_threads);
754 }
755 
756 void
757 SBThread::StepInto (const char *target_name, lldb::RunMode stop_other_threads)
758 {
759     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
760 
761     Mutex::Locker api_locker;
762     ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
763 
764     if (log)
765         log->Printf ("SBThread(%p)::StepInto (target_name='%s', stop_other_threads='%s')",
766                      static_cast<void*>(exe_ctx.GetThreadPtr()),
767                      target_name? target_name: "<NULL>",
768                      Thread::RunModeAsCString (stop_other_threads));
769 
770     if (exe_ctx.HasThreadScope())
771     {
772         bool abort_other_plans = false;
773 
774         Thread *thread = exe_ctx.GetThreadPtr();
775         StackFrameSP frame_sp(thread->GetStackFrameAtIndex (0));
776         ThreadPlanSP new_plan_sp;
777 
778         if (frame_sp && frame_sp->HasDebugInformation ())
779         {
780             const LazyBool step_out_avoids_code_without_debug_info = eLazyBoolCalculate;
781             const LazyBool step_in_avoids_code_without_debug_info = eLazyBoolCalculate;
782             SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
783             new_plan_sp = thread->QueueThreadPlanForStepInRange (abort_other_plans,
784                                                               sc.line_entry.range,
785                                                               sc,
786                                                               target_name,
787                                                               stop_other_threads,
788                                                               step_in_avoids_code_without_debug_info,
789                                                               step_out_avoids_code_without_debug_info);
790         }
791         else
792         {
793             new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction (false,
794                                                                            abort_other_plans,
795                                                                            stop_other_threads);
796         }
797 
798         // This returns an error, we should use it!
799         ResumeNewPlan (exe_ctx, new_plan_sp.get());
800     }
801 }
802 
803 void
804 SBThread::StepOut ()
805 {
806     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
807 
808     Mutex::Locker api_locker;
809     ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
810 
811 
812     if (log)
813         log->Printf ("SBThread(%p)::StepOut ()",
814                      static_cast<void*>(exe_ctx.GetThreadPtr()));
815 
816     if (exe_ctx.HasThreadScope())
817     {
818         bool abort_other_plans = false;
819         bool stop_other_threads = false;
820 
821         Thread *thread = exe_ctx.GetThreadPtr();
822 
823         const LazyBool avoid_no_debug = eLazyBoolCalculate;
824         ThreadPlanSP new_plan_sp(thread->QueueThreadPlanForStepOut (abort_other_plans,
825                                                                     NULL,
826                                                                     false,
827                                                                     stop_other_threads,
828                                                                     eVoteYes,
829                                                                     eVoteNoOpinion,
830                                                                     0,
831                                                                     avoid_no_debug));
832 
833         // This returns an error, we should use it!
834         ResumeNewPlan (exe_ctx, new_plan_sp.get());
835     }
836 }
837 
838 void
839 SBThread::StepOutOfFrame (lldb::SBFrame &sb_frame)
840 {
841     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
842 
843     Mutex::Locker api_locker;
844     ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
845 
846     StackFrameSP frame_sp (sb_frame.GetFrameSP());
847     if (log)
848     {
849         SBStream frame_desc_strm;
850         sb_frame.GetDescription (frame_desc_strm);
851         log->Printf ("SBThread(%p)::StepOutOfFrame (frame = SBFrame(%p): %s)",
852                      static_cast<void*>(exe_ctx.GetThreadPtr()),
853                      static_cast<void*>(frame_sp.get()),
854                      frame_desc_strm.GetData());
855     }
856 
857     if (exe_ctx.HasThreadScope())
858     {
859         bool abort_other_plans = false;
860         bool stop_other_threads = false;
861         Thread *thread = exe_ctx.GetThreadPtr();
862 
863         ThreadPlanSP new_plan_sp(thread->QueueThreadPlanForStepOut (abort_other_plans,
864                                                                     NULL,
865                                                                     false,
866                                                                     stop_other_threads,
867                                                                     eVoteYes,
868                                                                     eVoteNoOpinion,
869                                                                     frame_sp->GetFrameIndex()));
870 
871         // This returns an error, we should use it!
872         ResumeNewPlan (exe_ctx, new_plan_sp.get());
873     }
874 }
875 
876 void
877 SBThread::StepInstruction (bool step_over)
878 {
879     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
880 
881     Mutex::Locker api_locker;
882     ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
883 
884 
885 
886     if (log)
887         log->Printf ("SBThread(%p)::StepInstruction (step_over=%i)",
888                      static_cast<void*>(exe_ctx.GetThreadPtr()), step_over);
889 
890     if (exe_ctx.HasThreadScope())
891     {
892         Thread *thread = exe_ctx.GetThreadPtr();
893         ThreadPlanSP new_plan_sp(thread->QueueThreadPlanForStepSingleInstruction (step_over, true, true));
894 
895         // This returns an error, we should use it!
896         ResumeNewPlan (exe_ctx, new_plan_sp.get());
897     }
898 }
899 
900 void
901 SBThread::RunToAddress (lldb::addr_t addr)
902 {
903     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
904 
905     Mutex::Locker api_locker;
906     ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
907 
908 
909     if (log)
910         log->Printf ("SBThread(%p)::RunToAddress (addr=0x%" PRIx64 ")",
911                      static_cast<void*>(exe_ctx.GetThreadPtr()), addr);
912 
913     if (exe_ctx.HasThreadScope())
914     {
915         bool abort_other_plans = false;
916         bool stop_other_threads = true;
917 
918         Address target_addr (addr);
919 
920         Thread *thread = exe_ctx.GetThreadPtr();
921 
922         ThreadPlanSP new_plan_sp(thread->QueueThreadPlanForRunToAddress (abort_other_plans,
923                                                                          target_addr,
924                                                                          stop_other_threads));
925 
926         // This returns an error, we should use it!
927         ResumeNewPlan (exe_ctx, new_plan_sp.get());
928     }
929 }
930 
931 SBError
932 SBThread::StepOverUntil (lldb::SBFrame &sb_frame,
933                          lldb::SBFileSpec &sb_file_spec,
934                          uint32_t line)
935 {
936     SBError sb_error;
937     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
938     char path[PATH_MAX];
939 
940     Mutex::Locker api_locker;
941     ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
942 
943     StackFrameSP frame_sp (sb_frame.GetFrameSP());
944 
945     if (log)
946     {
947         SBStream frame_desc_strm;
948         sb_frame.GetDescription (frame_desc_strm);
949         sb_file_spec->GetPath (path, sizeof(path));
950         log->Printf ("SBThread(%p)::StepOverUntil (frame = SBFrame(%p): %s, file+line = %s:%u)",
951                      static_cast<void*>(exe_ctx.GetThreadPtr()),
952                      static_cast<void*>(frame_sp.get()),
953                      frame_desc_strm.GetData(), path, line);
954     }
955 
956     if (exe_ctx.HasThreadScope())
957     {
958         Target *target = exe_ctx.GetTargetPtr();
959         Thread *thread = exe_ctx.GetThreadPtr();
960 
961         if (line == 0)
962         {
963             sb_error.SetErrorString("invalid line argument");
964             return sb_error;
965         }
966 
967         if (!frame_sp)
968         {
969             frame_sp = thread->GetSelectedFrame ();
970             if (!frame_sp)
971                 frame_sp = thread->GetStackFrameAtIndex (0);
972         }
973 
974         SymbolContext frame_sc;
975         if (!frame_sp)
976         {
977             sb_error.SetErrorString("no valid frames in thread to step");
978             return sb_error;
979         }
980 
981         // If we have a frame, get its line
982         frame_sc = frame_sp->GetSymbolContext (eSymbolContextCompUnit  |
983                                                eSymbolContextFunction  |
984                                                eSymbolContextLineEntry |
985                                                eSymbolContextSymbol    );
986 
987         if (frame_sc.comp_unit == NULL)
988         {
989             sb_error.SetErrorStringWithFormat("frame %u doesn't have debug information", frame_sp->GetFrameIndex());
990             return sb_error;
991         }
992 
993         FileSpec step_file_spec;
994         if (sb_file_spec.IsValid())
995         {
996             // The file spec passed in was valid, so use it
997             step_file_spec = sb_file_spec.ref();
998         }
999         else
1000         {
1001             if (frame_sc.line_entry.IsValid())
1002                 step_file_spec = frame_sc.line_entry.file;
1003             else
1004             {
1005                 sb_error.SetErrorString("invalid file argument or no file for frame");
1006                 return sb_error;
1007             }
1008         }
1009 
1010         // Grab the current function, then we will make sure the "until" address is
1011         // within the function.  We discard addresses that are out of the current
1012         // function, and then if there are no addresses remaining, give an appropriate
1013         // error message.
1014 
1015         bool all_in_function = true;
1016         AddressRange fun_range = frame_sc.function->GetAddressRange();
1017 
1018         std::vector<addr_t> step_over_until_addrs;
1019         const bool abort_other_plans = false;
1020         const bool stop_other_threads = false;
1021         const bool check_inlines = true;
1022         const bool exact = false;
1023 
1024         SymbolContextList sc_list;
1025         const uint32_t num_matches = frame_sc.comp_unit->ResolveSymbolContext (step_file_spec,
1026                                                                                line,
1027                                                                                check_inlines,
1028                                                                                exact,
1029                                                                                eSymbolContextLineEntry,
1030                                                                                sc_list);
1031         if (num_matches > 0)
1032         {
1033             SymbolContext sc;
1034             for (uint32_t i=0; i<num_matches; ++i)
1035             {
1036                 if (sc_list.GetContextAtIndex(i, sc))
1037                 {
1038                     addr_t step_addr = sc.line_entry.range.GetBaseAddress().GetLoadAddress(target);
1039                     if (step_addr != LLDB_INVALID_ADDRESS)
1040                     {
1041                         if (fun_range.ContainsLoadAddress(step_addr, target))
1042                             step_over_until_addrs.push_back(step_addr);
1043                         else
1044                             all_in_function = false;
1045                     }
1046                 }
1047             }
1048         }
1049 
1050         if (step_over_until_addrs.empty())
1051         {
1052             if (all_in_function)
1053             {
1054                 step_file_spec.GetPath (path, sizeof(path));
1055                 sb_error.SetErrorStringWithFormat("No line entries for %s:%u", path, line);
1056             }
1057             else
1058                 sb_error.SetErrorString ("step until target not in current function");
1059         }
1060         else
1061         {
1062             ThreadPlanSP new_plan_sp(thread->QueueThreadPlanForStepUntil (abort_other_plans,
1063                                                                         &step_over_until_addrs[0],
1064                                                                         step_over_until_addrs.size(),
1065                                                                         stop_other_threads,
1066                                                                         frame_sp->GetFrameIndex()));
1067 
1068             sb_error = ResumeNewPlan (exe_ctx, new_plan_sp.get());
1069         }
1070     }
1071     else
1072     {
1073         sb_error.SetErrorString("this SBThread object is invalid");
1074     }
1075     return sb_error;
1076 }
1077 
1078 SBError
1079 SBThread::StepUsingScriptedThreadPlan (const char *script_class_name)
1080 {
1081     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1082     SBError sb_error;
1083 
1084     Mutex::Locker api_locker;
1085     ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1086 
1087     if (log)
1088     {
1089         log->Printf ("SBThread(%p)::StepUsingScriptedThreadPlan: class name: %s",
1090                      static_cast<void*>(exe_ctx.GetThreadPtr()),
1091                      script_class_name);
1092     }
1093 
1094 
1095     if (!exe_ctx.HasThreadScope())
1096     {
1097         sb_error.SetErrorString("this SBThread object is invalid");
1098         return sb_error;
1099     }
1100 
1101     Thread *thread = exe_ctx.GetThreadPtr();
1102     ThreadPlanSP thread_plan_sp = thread->QueueThreadPlanForStepScripted(false, script_class_name, false);
1103 
1104     if (thread_plan_sp)
1105         sb_error = ResumeNewPlan(exe_ctx, thread_plan_sp.get());
1106     else
1107     {
1108         sb_error.SetErrorStringWithFormat("Error queuing thread plan for class: %s.", script_class_name);
1109         if (log)
1110         log->Printf ("SBThread(%p)::StepUsingScriptedThreadPlan: Error queuing thread plan for class: %s",
1111                      static_cast<void*>(exe_ctx.GetThreadPtr()),
1112                      script_class_name);
1113     }
1114 
1115     return sb_error;
1116 }
1117 
1118 SBError
1119 SBThread::JumpToLine (lldb::SBFileSpec &file_spec, uint32_t line)
1120 {
1121     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1122     SBError sb_error;
1123 
1124     Mutex::Locker api_locker;
1125     ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1126 
1127     if (log)
1128         log->Printf ("SBThread(%p)::JumpToLine (file+line = %s:%u)",
1129                      static_cast<void*>(exe_ctx.GetThreadPtr()),
1130                      file_spec->GetPath().c_str(), line);
1131 
1132     if (!exe_ctx.HasThreadScope())
1133     {
1134         sb_error.SetErrorString("this SBThread object is invalid");
1135         return sb_error;
1136     }
1137 
1138     Thread *thread = exe_ctx.GetThreadPtr();
1139 
1140     Error err = thread->JumpToLine (file_spec.get(), line, true);
1141     sb_error.SetError (err);
1142     return sb_error;
1143 }
1144 
1145 SBError
1146 SBThread::ReturnFromFrame (SBFrame &frame, SBValue &return_value)
1147 {
1148     SBError sb_error;
1149 
1150     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1151 
1152     Mutex::Locker api_locker;
1153     ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1154 
1155 
1156     if (log)
1157         log->Printf ("SBThread(%p)::ReturnFromFrame (frame=%d)",
1158                      static_cast<void*>(exe_ctx.GetThreadPtr()),
1159                      frame.GetFrameID());
1160 
1161     if (exe_ctx.HasThreadScope())
1162     {
1163         Thread *thread = exe_ctx.GetThreadPtr();
1164         sb_error.SetError (thread->ReturnFromFrame(frame.GetFrameSP(), return_value.GetSP()));
1165     }
1166 
1167     return sb_error;
1168 }
1169 
1170 
1171 bool
1172 SBThread::Suspend()
1173 {
1174     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1175     ExecutionContext exe_ctx (m_opaque_sp.get());
1176     bool result = false;
1177     if (exe_ctx.HasThreadScope())
1178     {
1179         Process::StopLocker stop_locker;
1180         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1181         {
1182             exe_ctx.GetThreadPtr()->SetResumeState (eStateSuspended);
1183             result = true;
1184         }
1185         else
1186         {
1187             if (log)
1188                 log->Printf ("SBThread(%p)::Suspend() => error: process is running",
1189                              static_cast<void*>(exe_ctx.GetThreadPtr()));
1190         }
1191     }
1192     if (log)
1193         log->Printf ("SBThread(%p)::Suspend() => %i",
1194                      static_cast<void*>(exe_ctx.GetThreadPtr()), result);
1195     return result;
1196 }
1197 
1198 bool
1199 SBThread::Resume ()
1200 {
1201     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1202     ExecutionContext exe_ctx (m_opaque_sp.get());
1203     bool result = false;
1204     if (exe_ctx.HasThreadScope())
1205     {
1206         Process::StopLocker stop_locker;
1207         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1208         {
1209             const bool override_suspend = true;
1210             exe_ctx.GetThreadPtr()->SetResumeState (eStateRunning, override_suspend);
1211             result = true;
1212         }
1213         else
1214         {
1215             if (log)
1216                 log->Printf ("SBThread(%p)::Resume() => error: process is running",
1217                              static_cast<void*>(exe_ctx.GetThreadPtr()));
1218         }
1219     }
1220     if (log)
1221         log->Printf ("SBThread(%p)::Resume() => %i",
1222                      static_cast<void*>(exe_ctx.GetThreadPtr()), result);
1223     return result;
1224 }
1225 
1226 bool
1227 SBThread::IsSuspended()
1228 {
1229     ExecutionContext exe_ctx (m_opaque_sp.get());
1230     if (exe_ctx.HasThreadScope())
1231         return exe_ctx.GetThreadPtr()->GetResumeState () == eStateSuspended;
1232     return false;
1233 }
1234 
1235 bool
1236 SBThread::IsStopped()
1237 {
1238     ExecutionContext exe_ctx (m_opaque_sp.get());
1239     if (exe_ctx.HasThreadScope())
1240         return StateIsStoppedState(exe_ctx.GetThreadPtr()->GetState(), true);
1241     return false;
1242 }
1243 
1244 SBProcess
1245 SBThread::GetProcess ()
1246 {
1247     SBProcess sb_process;
1248     ExecutionContext exe_ctx (m_opaque_sp.get());
1249     if (exe_ctx.HasThreadScope())
1250     {
1251         // Have to go up to the target so we can get a shared pointer to our process...
1252         sb_process.SetSP (exe_ctx.GetProcessSP());
1253     }
1254 
1255     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1256     if (log)
1257     {
1258         SBStream frame_desc_strm;
1259         sb_process.GetDescription (frame_desc_strm);
1260         log->Printf ("SBThread(%p)::GetProcess () => SBProcess(%p): %s",
1261                      static_cast<void*>(exe_ctx.GetThreadPtr()),
1262                      static_cast<void*>(sb_process.GetSP().get()),
1263                      frame_desc_strm.GetData());
1264     }
1265 
1266     return sb_process;
1267 }
1268 
1269 uint32_t
1270 SBThread::GetNumFrames ()
1271 {
1272     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1273 
1274     uint32_t num_frames = 0;
1275     Mutex::Locker api_locker;
1276     ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1277 
1278     if (exe_ctx.HasThreadScope())
1279     {
1280         Process::StopLocker stop_locker;
1281         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1282         {
1283             num_frames = exe_ctx.GetThreadPtr()->GetStackFrameCount();
1284         }
1285         else
1286         {
1287             if (log)
1288                 log->Printf ("SBThread(%p)::GetNumFrames() => error: process is running",
1289                              static_cast<void*>(exe_ctx.GetThreadPtr()));
1290         }
1291     }
1292 
1293     if (log)
1294         log->Printf ("SBThread(%p)::GetNumFrames () => %u",
1295                      static_cast<void*>(exe_ctx.GetThreadPtr()), num_frames);
1296 
1297     return num_frames;
1298 }
1299 
1300 SBFrame
1301 SBThread::GetFrameAtIndex (uint32_t idx)
1302 {
1303     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1304 
1305     SBFrame sb_frame;
1306     StackFrameSP frame_sp;
1307     Mutex::Locker api_locker;
1308     ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1309 
1310     if (exe_ctx.HasThreadScope())
1311     {
1312         Process::StopLocker stop_locker;
1313         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1314         {
1315             frame_sp = exe_ctx.GetThreadPtr()->GetStackFrameAtIndex (idx);
1316             sb_frame.SetFrameSP (frame_sp);
1317         }
1318         else
1319         {
1320             if (log)
1321                 log->Printf ("SBThread(%p)::GetFrameAtIndex() => error: process is running",
1322                              static_cast<void*>(exe_ctx.GetThreadPtr()));
1323         }
1324     }
1325 
1326     if (log)
1327     {
1328         SBStream frame_desc_strm;
1329         sb_frame.GetDescription (frame_desc_strm);
1330         log->Printf ("SBThread(%p)::GetFrameAtIndex (idx=%d) => SBFrame(%p): %s",
1331                      static_cast<void*>(exe_ctx.GetThreadPtr()), idx,
1332                      static_cast<void*>(frame_sp.get()),
1333                      frame_desc_strm.GetData());
1334     }
1335 
1336     return sb_frame;
1337 }
1338 
1339 lldb::SBFrame
1340 SBThread::GetSelectedFrame ()
1341 {
1342     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1343 
1344     SBFrame sb_frame;
1345     StackFrameSP frame_sp;
1346     Mutex::Locker api_locker;
1347     ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1348 
1349     if (exe_ctx.HasThreadScope())
1350     {
1351         Process::StopLocker stop_locker;
1352         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1353         {
1354             frame_sp = exe_ctx.GetThreadPtr()->GetSelectedFrame ();
1355             sb_frame.SetFrameSP (frame_sp);
1356         }
1357         else
1358         {
1359             if (log)
1360                 log->Printf ("SBThread(%p)::GetSelectedFrame() => error: process is running",
1361                              static_cast<void*>(exe_ctx.GetThreadPtr()));
1362         }
1363     }
1364 
1365     if (log)
1366     {
1367         SBStream frame_desc_strm;
1368         sb_frame.GetDescription (frame_desc_strm);
1369         log->Printf ("SBThread(%p)::GetSelectedFrame () => SBFrame(%p): %s",
1370                      static_cast<void*>(exe_ctx.GetThreadPtr()),
1371                      static_cast<void*>(frame_sp.get()),
1372                      frame_desc_strm.GetData());
1373     }
1374 
1375     return sb_frame;
1376 }
1377 
1378 lldb::SBFrame
1379 SBThread::SetSelectedFrame (uint32_t idx)
1380 {
1381     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1382 
1383     SBFrame sb_frame;
1384     StackFrameSP frame_sp;
1385     Mutex::Locker api_locker;
1386     ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1387 
1388     if (exe_ctx.HasThreadScope())
1389     {
1390         Process::StopLocker stop_locker;
1391         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1392         {
1393             Thread *thread = exe_ctx.GetThreadPtr();
1394             frame_sp = thread->GetStackFrameAtIndex (idx);
1395             if (frame_sp)
1396             {
1397                 thread->SetSelectedFrame (frame_sp.get());
1398                 sb_frame.SetFrameSP (frame_sp);
1399             }
1400         }
1401         else
1402         {
1403             if (log)
1404                 log->Printf ("SBThread(%p)::SetSelectedFrame() => error: process is running",
1405                              static_cast<void*>(exe_ctx.GetThreadPtr()));
1406         }
1407     }
1408 
1409     if (log)
1410     {
1411         SBStream frame_desc_strm;
1412         sb_frame.GetDescription (frame_desc_strm);
1413         log->Printf ("SBThread(%p)::SetSelectedFrame (idx=%u) => SBFrame(%p): %s",
1414                      static_cast<void*>(exe_ctx.GetThreadPtr()), idx,
1415                      static_cast<void*>(frame_sp.get()),
1416                      frame_desc_strm.GetData());
1417     }
1418     return sb_frame;
1419 }
1420 
1421 bool
1422 SBThread::EventIsThreadEvent (const SBEvent &event)
1423 {
1424     return Thread::ThreadEventData::GetEventDataFromEvent(event.get()) != NULL;
1425 }
1426 
1427 SBFrame
1428 SBThread::GetStackFrameFromEvent (const SBEvent &event)
1429 {
1430     return Thread::ThreadEventData::GetStackFrameFromEvent (event.get());
1431 
1432 }
1433 
1434 SBThread
1435 SBThread::GetThreadFromEvent (const SBEvent &event)
1436 {
1437     return Thread::ThreadEventData::GetThreadFromEvent (event.get());
1438 }
1439 
1440 bool
1441 SBThread::operator == (const SBThread &rhs) const
1442 {
1443     return m_opaque_sp->GetThreadSP().get() == rhs.m_opaque_sp->GetThreadSP().get();
1444 }
1445 
1446 bool
1447 SBThread::operator != (const SBThread &rhs) const
1448 {
1449     return m_opaque_sp->GetThreadSP().get() != rhs.m_opaque_sp->GetThreadSP().get();
1450 }
1451 
1452 bool
1453 SBThread::GetStatus (SBStream &status) const
1454 {
1455     Stream &strm = status.ref();
1456 
1457     ExecutionContext exe_ctx (m_opaque_sp.get());
1458     if (exe_ctx.HasThreadScope())
1459     {
1460         exe_ctx.GetThreadPtr()->GetStatus(strm, 0, 1, 1);
1461     }
1462     else
1463         strm.PutCString ("No status");
1464 
1465     return true;
1466 }
1467 
1468 bool
1469 SBThread::GetDescription (SBStream &description) const
1470 {
1471     Stream &strm = description.ref();
1472 
1473     ExecutionContext exe_ctx (m_opaque_sp.get());
1474     if (exe_ctx.HasThreadScope())
1475     {
1476         strm.Printf("SBThread: tid = 0x%4.4" PRIx64, exe_ctx.GetThreadPtr()->GetID());
1477     }
1478     else
1479         strm.PutCString ("No value");
1480 
1481     return true;
1482 }
1483 
1484 SBThread
1485 SBThread::GetExtendedBacktraceThread (const char *type)
1486 {
1487     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1488     Mutex::Locker api_locker;
1489     ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1490     SBThread sb_origin_thread;
1491 
1492     if (exe_ctx.HasThreadScope())
1493     {
1494         Process::StopLocker stop_locker;
1495         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1496         {
1497             ThreadSP real_thread(exe_ctx.GetThreadSP());
1498             if (real_thread)
1499             {
1500                 ConstString type_const (type);
1501                 Process *process = exe_ctx.GetProcessPtr();
1502                 if (process)
1503                 {
1504                     SystemRuntime *runtime = process->GetSystemRuntime();
1505                     if (runtime)
1506                     {
1507                         ThreadSP new_thread_sp (runtime->GetExtendedBacktraceThread (real_thread, type_const));
1508                         if (new_thread_sp)
1509                         {
1510                             // Save this in the Process' ExtendedThreadList so a strong pointer retains the
1511                             // object.
1512                             process->GetExtendedThreadList().AddThread (new_thread_sp);
1513                             sb_origin_thread.SetThread (new_thread_sp);
1514                             if (log)
1515                             {
1516                                 const char *queue_name = new_thread_sp->GetQueueName();
1517                                 if (queue_name == NULL)
1518                                     queue_name = "";
1519                                 log->Printf ("SBThread(%p)::GetExtendedBacktraceThread() => new extended Thread "
1520                                              "created (%p) with queue_id 0x%" PRIx64 " queue name '%s'",
1521                                              static_cast<void*>(exe_ctx.GetThreadPtr()),
1522                                              static_cast<void*>(new_thread_sp.get()),
1523                                              new_thread_sp->GetQueueID(),
1524                                              queue_name);
1525                             }
1526                         }
1527                     }
1528                 }
1529             }
1530         }
1531         else
1532         {
1533             if (log)
1534                 log->Printf ("SBThread(%p)::GetExtendedBacktraceThread() => error: process is running",
1535                              static_cast<void*>(exe_ctx.GetThreadPtr()));
1536         }
1537     }
1538 
1539     if (log && sb_origin_thread.IsValid() == false)
1540         log->Printf("SBThread(%p)::GetExtendedBacktraceThread() is not returning a Valid thread",
1541                     static_cast<void*>(exe_ctx.GetThreadPtr()));
1542     return sb_origin_thread;
1543 }
1544 
1545 uint32_t
1546 SBThread::GetExtendedBacktraceOriginatingIndexID ()
1547 {
1548     ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
1549     if (thread_sp)
1550         return thread_sp->GetExtendedBacktraceOriginatingIndexID();
1551     return LLDB_INVALID_INDEX32;
1552 }
1553 
1554 bool
1555 SBThread::SafeToCallFunctions ()
1556 {
1557     ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
1558     if (thread_sp)
1559         return thread_sp->SafeToCallFunctions();
1560     return true;
1561 }
1562 
1563 lldb_private::Thread *
1564 SBThread::operator->()
1565 {
1566     ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
1567     if (thread_sp)
1568         return thread_sp.get();
1569     else
1570         return NULL;
1571 }
1572 
1573 lldb_private::Thread *
1574 SBThread::get()
1575 {
1576     ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
1577     if (thread_sp)
1578         return thread_sp.get();
1579     else
1580         return NULL;
1581 }
1582 
1583