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