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