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