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