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