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