130fdc8d8SChris Lattner //===-- SBThread.cpp --------------------------------------------*- C++ -*-===//
230fdc8d8SChris Lattner //
330fdc8d8SChris Lattner //                     The LLVM Compiler Infrastructure
430fdc8d8SChris Lattner //
530fdc8d8SChris Lattner // This file is distributed under the University of Illinois Open Source
630fdc8d8SChris Lattner // License. See LICENSE.TXT for details.
730fdc8d8SChris Lattner //
830fdc8d8SChris Lattner //===----------------------------------------------------------------------===//
930fdc8d8SChris Lattner 
104c5de699SEli Friedman #include "lldb/API/SBThread.h"
1130fdc8d8SChris Lattner 
1230fdc8d8SChris Lattner #include "lldb/API/SBSymbolContext.h"
1330fdc8d8SChris Lattner #include "lldb/API/SBFileSpec.h"
14dde9cff3SCaroline Tice #include "lldb/API/SBStream.h"
154e78f606SGreg Clayton #include "lldb/Breakpoint/BreakpointLocation.h"
166611103cSGreg Clayton #include "lldb/Core/Debugger.h"
17a75418dbSAndrew Kaylor #include "lldb/Core/State.h"
1830fdc8d8SChris Lattner #include "lldb/Core/Stream.h"
1930fdc8d8SChris Lattner #include "lldb/Core/StreamFile.h"
20705b1809SJason Molenda #include "lldb/Core/StructuredData.h"
21a78bd7ffSZachary Turner #include "lldb/Core/ValueObject.h"
226611103cSGreg Clayton #include "lldb/Interpreter/CommandInterpreter.h"
2393749ab3SZachary Turner #include "lldb/Symbol/SymbolContext.h"
2493749ab3SZachary Turner #include "lldb/Symbol/CompileUnit.h"
255dd4916fSJason Molenda #include "lldb/Target/SystemRuntime.h"
2630fdc8d8SChris Lattner #include "lldb/Target/Thread.h"
2730fdc8d8SChris Lattner #include "lldb/Target/Process.h"
28b9ffa98cSJason Molenda #include "lldb/Target/Queue.h"
2993749ab3SZachary Turner #include "lldb/Target/UnixSignals.h"
30f4b47e15SGreg Clayton #include "lldb/Target/StopInfo.h"
3130fdc8d8SChris Lattner #include "lldb/Target/Target.h"
3230fdc8d8SChris Lattner #include "lldb/Target/ThreadPlan.h"
3330fdc8d8SChris Lattner #include "lldb/Target/ThreadPlanStepInstruction.h"
3430fdc8d8SChris Lattner #include "lldb/Target/ThreadPlanStepOut.h"
3530fdc8d8SChris Lattner #include "lldb/Target/ThreadPlanStepRange.h"
3630fdc8d8SChris Lattner #include "lldb/Target/ThreadPlanStepInRange.h"
3730fdc8d8SChris Lattner 
384c5de699SEli Friedman #include "lldb/API/SBAddress.h"
394c5de699SEli Friedman #include "lldb/API/SBDebugger.h"
404f465cffSJim Ingham #include "lldb/API/SBEvent.h"
4173ca05a2SJim Ingham #include "lldb/API/SBFrame.h"
424c5de699SEli Friedman #include "lldb/API/SBProcess.h"
436a831436SKuba Brecka #include "lldb/API/SBThreadCollection.h"
442bdbfd50SJim Ingham #include "lldb/API/SBThreadPlan.h"
4573ca05a2SJim Ingham #include "lldb/API/SBValue.h"
4630fdc8d8SChris Lattner 
4730fdc8d8SChris Lattner using namespace lldb;
4830fdc8d8SChris Lattner using namespace lldb_private;
4930fdc8d8SChris Lattner 
504f465cffSJim Ingham const char *
514f465cffSJim Ingham SBThread::GetBroadcasterClassName ()
524f465cffSJim Ingham {
534f465cffSJim Ingham     return Thread::GetStaticBroadcasterClass().AsCString();
544f465cffSJim Ingham }
554f465cffSJim Ingham 
56cfd1acedSGreg Clayton //----------------------------------------------------------------------
57cfd1acedSGreg Clayton // Constructors
58cfd1acedSGreg Clayton //----------------------------------------------------------------------
5930fdc8d8SChris Lattner SBThread::SBThread () :
607fdf9ef1SGreg Clayton     m_opaque_sp (new ExecutionContextRef())
6130fdc8d8SChris Lattner {
6230fdc8d8SChris Lattner }
6330fdc8d8SChris Lattner 
6430fdc8d8SChris Lattner SBThread::SBThread (const ThreadSP& lldb_object_sp) :
657fdf9ef1SGreg Clayton     m_opaque_sp (new ExecutionContextRef(lldb_object_sp))
6630fdc8d8SChris Lattner {
6730fdc8d8SChris Lattner }
6830fdc8d8SChris Lattner 
6992ef5735SGreg Clayton SBThread::SBThread (const SBThread &rhs) :
707fdf9ef1SGreg Clayton     m_opaque_sp (new ExecutionContextRef(*rhs.m_opaque_sp))
7130fdc8d8SChris Lattner {
727fdf9ef1SGreg Clayton 
7330fdc8d8SChris Lattner }
7430fdc8d8SChris Lattner 
7530fdc8d8SChris Lattner //----------------------------------------------------------------------
76cfd1acedSGreg Clayton // Assignment operator
77cfd1acedSGreg Clayton //----------------------------------------------------------------------
78cfd1acedSGreg Clayton 
79cfd1acedSGreg Clayton const lldb::SBThread &
80cfd1acedSGreg Clayton SBThread::operator = (const SBThread &rhs)
81cfd1acedSGreg Clayton {
82cfd1acedSGreg Clayton     if (this != &rhs)
837fdf9ef1SGreg Clayton         *m_opaque_sp = *rhs.m_opaque_sp;
84cfd1acedSGreg Clayton     return *this;
85cfd1acedSGreg Clayton }
86cfd1acedSGreg Clayton 
87cfd1acedSGreg Clayton //----------------------------------------------------------------------
8830fdc8d8SChris Lattner // Destructor
8930fdc8d8SChris Lattner //----------------------------------------------------------------------
9030fdc8d8SChris Lattner SBThread::~SBThread()
9130fdc8d8SChris Lattner {
9230fdc8d8SChris Lattner }
9330fdc8d8SChris Lattner 
94b9ffa98cSJason Molenda lldb::SBQueue
95b9ffa98cSJason Molenda SBThread::GetQueue () const
96b9ffa98cSJason Molenda {
97b9ffa98cSJason Molenda     SBQueue sb_queue;
98b9ffa98cSJason Molenda     QueueSP queue_sp;
99*bb19a13cSSaleem Abdulrasool     std::unique_lock<std::recursive_mutex> lock;
100*bb19a13cSSaleem Abdulrasool     ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
101b9ffa98cSJason Molenda 
102b9ffa98cSJason Molenda     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
103b9ffa98cSJason Molenda     if (exe_ctx.HasThreadScope())
104b9ffa98cSJason Molenda     {
105b9ffa98cSJason Molenda         Process::StopLocker stop_locker;
106b9ffa98cSJason Molenda         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
107b9ffa98cSJason Molenda         {
108b9ffa98cSJason Molenda             queue_sp = exe_ctx.GetThreadPtr()->GetQueue();
109b9ffa98cSJason Molenda             if (queue_sp)
110b9ffa98cSJason Molenda             {
111b9ffa98cSJason Molenda                 sb_queue.SetQueue (queue_sp);
112b9ffa98cSJason Molenda             }
113b9ffa98cSJason Molenda         }
114b9ffa98cSJason Molenda         else
115b9ffa98cSJason Molenda         {
116b9ffa98cSJason Molenda             if (log)
117358cf1eaSGreg Clayton                 log->Printf ("SBThread(%p)::GetQueue() => error: process is running",
118b9ffa98cSJason Molenda                              static_cast<void*>(exe_ctx.GetThreadPtr()));
119b9ffa98cSJason Molenda         }
120b9ffa98cSJason Molenda     }
121b9ffa98cSJason Molenda 
122b9ffa98cSJason Molenda     if (log)
123358cf1eaSGreg Clayton         log->Printf ("SBThread(%p)::GetQueue () => SBQueue(%p)",
124b9ffa98cSJason Molenda                      static_cast<void*>(exe_ctx.GetThreadPtr()), static_cast<void*>(queue_sp.get()));
125b9ffa98cSJason Molenda 
126b9ffa98cSJason Molenda     return sb_queue;
127b9ffa98cSJason Molenda }
128b9ffa98cSJason Molenda 
129b9ffa98cSJason Molenda 
13030fdc8d8SChris Lattner bool
13130fdc8d8SChris Lattner SBThread::IsValid() const
13230fdc8d8SChris Lattner {
133*bb19a13cSSaleem Abdulrasool     std::unique_lock<std::recursive_mutex> lock;
134*bb19a13cSSaleem Abdulrasool     ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1357fa7dc36SJim Ingham 
1367fa7dc36SJim Ingham     Target *target = exe_ctx.GetTargetPtr();
1377fa7dc36SJim Ingham     Process *process = exe_ctx.GetProcessPtr();
1387fa7dc36SJim Ingham     if (target && process)
1397fa7dc36SJim Ingham     {
1407fa7dc36SJim Ingham         Process::StopLocker stop_locker;
1417fa7dc36SJim Ingham         if (stop_locker.TryLock(&process->GetRunLock()))
1427fdf9ef1SGreg Clayton         return m_opaque_sp->GetThreadSP().get() != NULL;
14330fdc8d8SChris Lattner     }
1447fa7dc36SJim Ingham     // Without a valid target & process, this thread can't be valid.
1457fa7dc36SJim Ingham     return false;
1467fa7dc36SJim Ingham }
14730fdc8d8SChris Lattner 
14848e42549SGreg Clayton void
14948e42549SGreg Clayton SBThread::Clear ()
15048e42549SGreg Clayton {
1517fdf9ef1SGreg Clayton     m_opaque_sp->Clear();
15248e42549SGreg Clayton }
15348e42549SGreg Clayton 
15448e42549SGreg Clayton 
15530fdc8d8SChris Lattner StopReason
15630fdc8d8SChris Lattner SBThread::GetStopReason()
15730fdc8d8SChris Lattner {
1585160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
159ceb6b139SCaroline Tice 
160ceb6b139SCaroline Tice     StopReason reason = eStopReasonInvalid;
161*bb19a13cSSaleem Abdulrasool     std::unique_lock<std::recursive_mutex> lock;
162*bb19a13cSSaleem Abdulrasool     ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1634fc6cb9cSJim Ingham 
1641ac04c30SGreg Clayton     if (exe_ctx.HasThreadScope())
16530fdc8d8SChris Lattner     {
1667fdf9ef1SGreg Clayton         Process::StopLocker stop_locker;
1677fdf9ef1SGreg Clayton         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1687fdf9ef1SGreg Clayton         {
16997d5cf05SGreg Clayton             return exe_ctx.GetThreadPtr()->GetStopReason();
17030fdc8d8SChris Lattner         }
171c9858e4dSGreg Clayton         else
172c9858e4dSGreg Clayton         {
173c9858e4dSGreg Clayton             if (log)
174324a1036SSaleem Abdulrasool                 log->Printf ("SBThread(%p)::GetStopReason() => error: process is running",
175324a1036SSaleem Abdulrasool                              static_cast<void*>(exe_ctx.GetThreadPtr()));
176c9858e4dSGreg Clayton         }
1777fdf9ef1SGreg Clayton     }
178ceb6b139SCaroline Tice 
179ceb6b139SCaroline Tice     if (log)
180324a1036SSaleem Abdulrasool         log->Printf ("SBThread(%p)::GetStopReason () => %s",
181324a1036SSaleem Abdulrasool                      static_cast<void*>(exe_ctx.GetThreadPtr()),
182750cd175SCaroline Tice                      Thread::StopReasonAsCString (reason));
183ceb6b139SCaroline Tice 
184ceb6b139SCaroline Tice     return reason;
18530fdc8d8SChris Lattner }
18630fdc8d8SChris Lattner 
18730fdc8d8SChris Lattner size_t
1884e78f606SGreg Clayton SBThread::GetStopReasonDataCount ()
1894e78f606SGreg Clayton {
190*bb19a13cSSaleem Abdulrasool     std::unique_lock<std::recursive_mutex> lock;
191*bb19a13cSSaleem Abdulrasool     ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1924fc6cb9cSJim Ingham 
1931ac04c30SGreg Clayton     if (exe_ctx.HasThreadScope())
1944e78f606SGreg Clayton     {
1957fdf9ef1SGreg Clayton         Process::StopLocker stop_locker;
1967fdf9ef1SGreg Clayton         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1977fdf9ef1SGreg Clayton         {
1981ac04c30SGreg Clayton             StopInfoSP stop_info_sp = exe_ctx.GetThreadPtr()->GetStopInfo ();
1994e78f606SGreg Clayton             if (stop_info_sp)
2004e78f606SGreg Clayton             {
2014e78f606SGreg Clayton                 StopReason reason = stop_info_sp->GetStopReason();
2024e78f606SGreg Clayton                 switch (reason)
2034e78f606SGreg Clayton                 {
2044e78f606SGreg Clayton                 case eStopReasonInvalid:
2054e78f606SGreg Clayton                 case eStopReasonNone:
2064e78f606SGreg Clayton                 case eStopReasonTrace:
20790ba8115SGreg Clayton                 case eStopReasonExec:
2084e78f606SGreg Clayton                 case eStopReasonPlanComplete:
209f85defaeSAndrew Kaylor                 case eStopReasonThreadExiting:
210afdf842bSKuba Brecka                 case eStopReasonInstrumentation:
2114e78f606SGreg Clayton                     // There is no data for these stop reasons.
2124e78f606SGreg Clayton                     return 0;
2134e78f606SGreg Clayton 
2144e78f606SGreg Clayton                 case eStopReasonBreakpoint:
2154e78f606SGreg Clayton                     {
2164e78f606SGreg Clayton                         break_id_t site_id = stop_info_sp->GetValue();
2171ac04c30SGreg Clayton                         lldb::BreakpointSiteSP bp_site_sp (exe_ctx.GetProcessPtr()->GetBreakpointSiteList().FindByID (site_id));
2184e78f606SGreg Clayton                         if (bp_site_sp)
2194e78f606SGreg Clayton                             return bp_site_sp->GetNumberOfOwners () * 2;
2204e78f606SGreg Clayton                         else
2214e78f606SGreg Clayton                             return 0; // Breakpoint must have cleared itself...
2224e78f606SGreg Clayton                     }
2234e78f606SGreg Clayton                     break;
2244e78f606SGreg Clayton 
2254e78f606SGreg Clayton                 case eStopReasonWatchpoint:
226290fa41bSJohnny Chen                     return 1;
2274e78f606SGreg Clayton 
2284e78f606SGreg Clayton                 case eStopReasonSignal:
2294e78f606SGreg Clayton                     return 1;
2304e78f606SGreg Clayton 
2314e78f606SGreg Clayton                 case eStopReasonException:
2324e78f606SGreg Clayton                     return 1;
2334e78f606SGreg Clayton                 }
2344e78f606SGreg Clayton             }
2354e78f606SGreg Clayton         }
236c9858e4dSGreg Clayton         else
237c9858e4dSGreg Clayton         {
2385160ce5cSGreg Clayton             Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
239c9858e4dSGreg Clayton             if (log)
240324a1036SSaleem Abdulrasool                 log->Printf ("SBThread(%p)::GetStopReasonDataCount() => error: process is running",
241324a1036SSaleem Abdulrasool                              static_cast<void*>(exe_ctx.GetThreadPtr()));
242c9858e4dSGreg Clayton         }
2437fdf9ef1SGreg Clayton     }
2444e78f606SGreg Clayton     return 0;
2454e78f606SGreg Clayton }
2464e78f606SGreg Clayton 
2474e78f606SGreg Clayton uint64_t
2484e78f606SGreg Clayton SBThread::GetStopReasonDataAtIndex (uint32_t idx)
2494e78f606SGreg Clayton {
250*bb19a13cSSaleem Abdulrasool     std::unique_lock<std::recursive_mutex> lock;
251*bb19a13cSSaleem Abdulrasool     ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
2524fc6cb9cSJim Ingham 
2531ac04c30SGreg Clayton     if (exe_ctx.HasThreadScope())
2544e78f606SGreg Clayton     {
2557fdf9ef1SGreg Clayton         Process::StopLocker stop_locker;
2567fdf9ef1SGreg Clayton         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
2577fdf9ef1SGreg Clayton         {
2581ac04c30SGreg Clayton             Thread *thread = exe_ctx.GetThreadPtr();
2591ac04c30SGreg Clayton             StopInfoSP stop_info_sp = thread->GetStopInfo ();
2604e78f606SGreg Clayton             if (stop_info_sp)
2614e78f606SGreg Clayton             {
2624e78f606SGreg Clayton                 StopReason reason = stop_info_sp->GetStopReason();
2634e78f606SGreg Clayton                 switch (reason)
2644e78f606SGreg Clayton                 {
2654e78f606SGreg Clayton                 case eStopReasonInvalid:
2664e78f606SGreg Clayton                 case eStopReasonNone:
2674e78f606SGreg Clayton                 case eStopReasonTrace:
26890ba8115SGreg Clayton                 case eStopReasonExec:
2694e78f606SGreg Clayton                 case eStopReasonPlanComplete:
270f85defaeSAndrew Kaylor                 case eStopReasonThreadExiting:
271afdf842bSKuba Brecka                 case eStopReasonInstrumentation:
2724e78f606SGreg Clayton                     // There is no data for these stop reasons.
2734e78f606SGreg Clayton                     return 0;
2744e78f606SGreg Clayton 
2754e78f606SGreg Clayton                 case eStopReasonBreakpoint:
2764e78f606SGreg Clayton                     {
2774e78f606SGreg Clayton                         break_id_t site_id = stop_info_sp->GetValue();
2781ac04c30SGreg Clayton                         lldb::BreakpointSiteSP bp_site_sp (exe_ctx.GetProcessPtr()->GetBreakpointSiteList().FindByID (site_id));
2794e78f606SGreg Clayton                         if (bp_site_sp)
2804e78f606SGreg Clayton                         {
2814e78f606SGreg Clayton                             uint32_t bp_index = idx / 2;
2824e78f606SGreg Clayton                             BreakpointLocationSP bp_loc_sp (bp_site_sp->GetOwnerAtIndex (bp_index));
2834e78f606SGreg Clayton                             if (bp_loc_sp)
2844e78f606SGreg Clayton                             {
2858334e14eSGreg Clayton                                 if (idx & 1)
2864e78f606SGreg Clayton                                 {
2874e78f606SGreg Clayton                                     // Odd idx, return the breakpoint location ID
2884e78f606SGreg Clayton                                     return bp_loc_sp->GetID();
2894e78f606SGreg Clayton                                 }
2904e78f606SGreg Clayton                                 else
2914e78f606SGreg Clayton                                 {
2924e78f606SGreg Clayton                                     // Even idx, return the breakpoint ID
2934e78f606SGreg Clayton                                     return bp_loc_sp->GetBreakpoint().GetID();
2944e78f606SGreg Clayton                                 }
2954e78f606SGreg Clayton                             }
2964e78f606SGreg Clayton                         }
2974e78f606SGreg Clayton                         return LLDB_INVALID_BREAK_ID;
2984e78f606SGreg Clayton                     }
2994e78f606SGreg Clayton                     break;
3004e78f606SGreg Clayton 
3014e78f606SGreg Clayton                 case eStopReasonWatchpoint:
302290fa41bSJohnny Chen                     return stop_info_sp->GetValue();
3034e78f606SGreg Clayton 
3044e78f606SGreg Clayton                 case eStopReasonSignal:
3054e78f606SGreg Clayton                     return stop_info_sp->GetValue();
3064e78f606SGreg Clayton 
3074e78f606SGreg Clayton                 case eStopReasonException:
3084e78f606SGreg Clayton                     return stop_info_sp->GetValue();
3094e78f606SGreg Clayton                 }
3104e78f606SGreg Clayton             }
3114e78f606SGreg Clayton         }
312c9858e4dSGreg Clayton         else
313c9858e4dSGreg Clayton         {
3145160ce5cSGreg Clayton             Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
315c9858e4dSGreg Clayton             if (log)
316324a1036SSaleem Abdulrasool                 log->Printf ("SBThread(%p)::GetStopReasonDataAtIndex() => error: process is running",
317324a1036SSaleem Abdulrasool                              static_cast<void*>(exe_ctx.GetThreadPtr()));
318c9858e4dSGreg Clayton         }
3197fdf9ef1SGreg Clayton     }
3204e78f606SGreg Clayton     return 0;
3214e78f606SGreg Clayton }
3224e78f606SGreg Clayton 
323afdf842bSKuba Brecka bool
324afdf842bSKuba Brecka SBThread::GetStopReasonExtendedInfoAsJSON (lldb::SBStream &stream)
325afdf842bSKuba Brecka {
326afdf842bSKuba Brecka     Stream &strm = stream.ref();
327afdf842bSKuba Brecka 
328afdf842bSKuba Brecka     ExecutionContext exe_ctx (m_opaque_sp.get());
329afdf842bSKuba Brecka     if (! exe_ctx.HasThreadScope())
330afdf842bSKuba Brecka         return false;
331afdf842bSKuba Brecka 
332afdf842bSKuba Brecka 
333afdf842bSKuba Brecka     StopInfoSP stop_info = exe_ctx.GetThreadPtr()->GetStopInfo();
334afdf842bSKuba Brecka     StructuredData::ObjectSP info = stop_info->GetExtendedInfo();
335afdf842bSKuba Brecka     if (! info)
336afdf842bSKuba Brecka         return false;
337afdf842bSKuba Brecka 
338afdf842bSKuba Brecka     info->Dump(strm);
339afdf842bSKuba Brecka 
340afdf842bSKuba Brecka     return true;
341afdf842bSKuba Brecka }
342afdf842bSKuba Brecka 
3436a831436SKuba Brecka SBThreadCollection
3446a831436SKuba Brecka SBThread::GetStopReasonExtendedBacktraces (InstrumentationRuntimeType type)
3456a831436SKuba Brecka {
3466a831436SKuba Brecka     ThreadCollectionSP threads;
3476a831436SKuba Brecka     threads.reset(new ThreadCollection());
3486a831436SKuba Brecka 
3496a831436SKuba Brecka     // We currently only support ThreadSanitizer.
3506a831436SKuba Brecka     if (type != eInstrumentationRuntimeTypeThreadSanitizer)
3516a831436SKuba Brecka         return threads;
3526a831436SKuba Brecka 
3536a831436SKuba Brecka     ExecutionContext exe_ctx (m_opaque_sp.get());
3546a831436SKuba Brecka     if (! exe_ctx.HasThreadScope())
3551aad8fb7SKuba Brecka         return threads;
3566a831436SKuba Brecka 
3576a831436SKuba Brecka     ProcessSP process_sp = exe_ctx.GetProcessSP();
3586a831436SKuba Brecka 
3596a831436SKuba Brecka     StopInfoSP stop_info = exe_ctx.GetThreadPtr()->GetStopInfo();
3606a831436SKuba Brecka     StructuredData::ObjectSP info = stop_info->GetExtendedInfo();
3616a831436SKuba Brecka     if (! info)
3626a831436SKuba Brecka         return threads;
3636a831436SKuba Brecka 
3641aad8fb7SKuba Brecka     return process_sp->GetInstrumentationRuntime(type)->GetBacktracesFromExtendedStopInfo(info);
3656a831436SKuba Brecka }
3666a831436SKuba Brecka 
3674e78f606SGreg Clayton size_t
36830fdc8d8SChris Lattner SBThread::GetStopDescription (char *dst, size_t dst_len)
36930fdc8d8SChris Lattner {
3705160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
371ceb6b139SCaroline Tice 
372*bb19a13cSSaleem Abdulrasool     std::unique_lock<std::recursive_mutex> lock;
373*bb19a13cSSaleem Abdulrasool     ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
3744fc6cb9cSJim Ingham 
3751ac04c30SGreg Clayton     if (exe_ctx.HasThreadScope())
37630fdc8d8SChris Lattner     {
3777fdf9ef1SGreg Clayton         Process::StopLocker stop_locker;
3787fdf9ef1SGreg Clayton         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
3797fdf9ef1SGreg Clayton         {
3807fdf9ef1SGreg Clayton 
3811ac04c30SGreg Clayton             StopInfoSP stop_info_sp = exe_ctx.GetThreadPtr()->GetStopInfo ();
382b15bfc75SJim Ingham             if (stop_info_sp)
38330fdc8d8SChris Lattner             {
384b15bfc75SJim Ingham                 const char *stop_desc = stop_info_sp->GetDescription();
38530fdc8d8SChris Lattner                 if (stop_desc)
38630fdc8d8SChris Lattner                 {
387ceb6b139SCaroline Tice                     if (log)
3884838131bSGreg Clayton                         log->Printf ("SBThread(%p)::GetStopDescription (dst, dst_len) => \"%s\"",
389324a1036SSaleem Abdulrasool                                      static_cast<void*>(exe_ctx.GetThreadPtr()),
390324a1036SSaleem Abdulrasool                                      stop_desc);
39130fdc8d8SChris Lattner                     if (dst)
39230fdc8d8SChris Lattner                         return ::snprintf (dst, dst_len, "%s", stop_desc);
39330fdc8d8SChris Lattner                     else
39430fdc8d8SChris Lattner                     {
39530fdc8d8SChris Lattner                         // NULL dst passed in, return the length needed to contain the description
39630fdc8d8SChris Lattner                         return ::strlen (stop_desc) + 1; // Include the NULL byte for size
39730fdc8d8SChris Lattner                     }
39830fdc8d8SChris Lattner                 }
39930fdc8d8SChris Lattner                 else
40030fdc8d8SChris Lattner                 {
40130fdc8d8SChris Lattner                     size_t stop_desc_len = 0;
402b15bfc75SJim Ingham                     switch (stop_info_sp->GetStopReason())
40330fdc8d8SChris Lattner                     {
40430fdc8d8SChris Lattner                     case eStopReasonTrace:
40530fdc8d8SChris Lattner                     case eStopReasonPlanComplete:
40630fdc8d8SChris Lattner                         {
40730fdc8d8SChris Lattner                             static char trace_desc[] = "step";
40830fdc8d8SChris Lattner                             stop_desc = trace_desc;
40930fdc8d8SChris Lattner                             stop_desc_len = sizeof(trace_desc); // Include the NULL byte for size
41030fdc8d8SChris Lattner                         }
41130fdc8d8SChris Lattner                         break;
41230fdc8d8SChris Lattner 
41330fdc8d8SChris Lattner                     case eStopReasonBreakpoint:
41430fdc8d8SChris Lattner                         {
41530fdc8d8SChris Lattner                             static char bp_desc[] = "breakpoint hit";
41630fdc8d8SChris Lattner                             stop_desc = bp_desc;
41730fdc8d8SChris Lattner                             stop_desc_len = sizeof(bp_desc); // Include the NULL byte for size
41830fdc8d8SChris Lattner                         }
41930fdc8d8SChris Lattner                         break;
42030fdc8d8SChris Lattner 
42130fdc8d8SChris Lattner                     case eStopReasonWatchpoint:
42230fdc8d8SChris Lattner                         {
42330fdc8d8SChris Lattner                             static char wp_desc[] = "watchpoint hit";
42430fdc8d8SChris Lattner                             stop_desc = wp_desc;
42530fdc8d8SChris Lattner                             stop_desc_len = sizeof(wp_desc); // Include the NULL byte for size
42630fdc8d8SChris Lattner                         }
42730fdc8d8SChris Lattner                         break;
42830fdc8d8SChris Lattner 
42930fdc8d8SChris Lattner                     case eStopReasonSignal:
43030fdc8d8SChris Lattner                         {
43198d0a4b3SChaoren Lin                             stop_desc = exe_ctx.GetProcessPtr()->GetUnixSignals()->GetSignalAsCString(stop_info_sp->GetValue());
43230fdc8d8SChris Lattner                             if (stop_desc == NULL || stop_desc[0] == '\0')
43330fdc8d8SChris Lattner                             {
43430fdc8d8SChris Lattner                                 static char signal_desc[] = "signal";
43530fdc8d8SChris Lattner                                 stop_desc = signal_desc;
43630fdc8d8SChris Lattner                                 stop_desc_len = sizeof(signal_desc); // Include the NULL byte for size
43730fdc8d8SChris Lattner                             }
43830fdc8d8SChris Lattner                         }
43930fdc8d8SChris Lattner                         break;
44030fdc8d8SChris Lattner 
44130fdc8d8SChris Lattner                     case eStopReasonException:
44230fdc8d8SChris Lattner                         {
44330fdc8d8SChris Lattner                             char exc_desc[] = "exception";
44430fdc8d8SChris Lattner                             stop_desc = exc_desc;
44530fdc8d8SChris Lattner                             stop_desc_len = sizeof(exc_desc); // Include the NULL byte for size
44630fdc8d8SChris Lattner                         }
44730fdc8d8SChris Lattner                         break;
448c982c768SGreg Clayton 
44990ba8115SGreg Clayton                     case eStopReasonExec:
45090ba8115SGreg Clayton                         {
45190ba8115SGreg Clayton                             char exc_desc[] = "exec";
45290ba8115SGreg Clayton                             stop_desc = exc_desc;
45390ba8115SGreg Clayton                             stop_desc_len = sizeof(exc_desc); // Include the NULL byte for size
45490ba8115SGreg Clayton                         }
45590ba8115SGreg Clayton                         break;
45690ba8115SGreg Clayton 
457f85defaeSAndrew Kaylor                     case eStopReasonThreadExiting:
458f85defaeSAndrew Kaylor                         {
459f85defaeSAndrew Kaylor                             char limbo_desc[] = "thread exiting";
460f85defaeSAndrew Kaylor                             stop_desc = limbo_desc;
461f85defaeSAndrew Kaylor                             stop_desc_len = sizeof(limbo_desc);
462f85defaeSAndrew Kaylor                         }
463f85defaeSAndrew Kaylor                         break;
464c982c768SGreg Clayton                     default:
465c982c768SGreg Clayton                         break;
46630fdc8d8SChris Lattner                     }
46730fdc8d8SChris Lattner 
46830fdc8d8SChris Lattner                     if (stop_desc && stop_desc[0])
46930fdc8d8SChris Lattner                     {
470ceb6b139SCaroline Tice                         if (log)
47193aa84e8SGreg Clayton                             log->Printf ("SBThread(%p)::GetStopDescription (dst, dst_len) => '%s'",
472324a1036SSaleem Abdulrasool                                          static_cast<void*>(exe_ctx.GetThreadPtr()),
473324a1036SSaleem Abdulrasool                                          stop_desc);
474ceb6b139SCaroline Tice 
47530fdc8d8SChris Lattner                         if (dst)
47630fdc8d8SChris Lattner                             return ::snprintf (dst, dst_len, "%s", stop_desc) + 1; // Include the NULL byte
47730fdc8d8SChris Lattner 
47830fdc8d8SChris Lattner                         if (stop_desc_len == 0)
47930fdc8d8SChris Lattner                             stop_desc_len = ::strlen (stop_desc) + 1; // Include the NULL byte
48030fdc8d8SChris Lattner 
48130fdc8d8SChris Lattner                         return stop_desc_len;
48230fdc8d8SChris Lattner                     }
48330fdc8d8SChris Lattner                 }
48430fdc8d8SChris Lattner             }
48530fdc8d8SChris Lattner         }
486c9858e4dSGreg Clayton         else
487c9858e4dSGreg Clayton         {
4885160ce5cSGreg Clayton             Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
489c9858e4dSGreg Clayton             if (log)
490324a1036SSaleem Abdulrasool                 log->Printf ("SBThread(%p)::GetStopDescription() => error: process is running",
491324a1036SSaleem Abdulrasool                              static_cast<void*>(exe_ctx.GetThreadPtr()));
492c9858e4dSGreg Clayton         }
4937fdf9ef1SGreg Clayton     }
49430fdc8d8SChris Lattner     if (dst)
49530fdc8d8SChris Lattner         *dst = 0;
49630fdc8d8SChris Lattner     return 0;
49730fdc8d8SChris Lattner }
49830fdc8d8SChris Lattner 
49973ca05a2SJim Ingham SBValue
50073ca05a2SJim Ingham SBThread::GetStopReturnValue ()
50173ca05a2SJim Ingham {
5025160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
50373ca05a2SJim Ingham     ValueObjectSP return_valobj_sp;
504*bb19a13cSSaleem Abdulrasool     std::unique_lock<std::recursive_mutex> lock;
505*bb19a13cSSaleem Abdulrasool     ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
5064fc6cb9cSJim Ingham 
5071ac04c30SGreg Clayton     if (exe_ctx.HasThreadScope())
50873ca05a2SJim Ingham     {
5097fdf9ef1SGreg Clayton         Process::StopLocker stop_locker;
5107fdf9ef1SGreg Clayton         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
5117fdf9ef1SGreg Clayton         {
5121ac04c30SGreg Clayton             StopInfoSP stop_info_sp = exe_ctx.GetThreadPtr()->GetStopInfo ();
51373ca05a2SJim Ingham             if (stop_info_sp)
51473ca05a2SJim Ingham             {
51573ca05a2SJim Ingham                 return_valobj_sp = StopInfo::GetReturnValueObject (stop_info_sp);
51673ca05a2SJim Ingham             }
51773ca05a2SJim Ingham         }
518c9858e4dSGreg Clayton         else
519c9858e4dSGreg Clayton         {
520c9858e4dSGreg Clayton             if (log)
521324a1036SSaleem Abdulrasool                 log->Printf ("SBThread(%p)::GetStopReturnValue() => error: process is running",
522324a1036SSaleem Abdulrasool                              static_cast<void*>(exe_ctx.GetThreadPtr()));
523c9858e4dSGreg Clayton         }
5247fdf9ef1SGreg Clayton     }
52573ca05a2SJim Ingham 
52673ca05a2SJim Ingham     if (log)
527324a1036SSaleem Abdulrasool         log->Printf ("SBThread(%p)::GetStopReturnValue () => %s",
528324a1036SSaleem Abdulrasool                      static_cast<void*>(exe_ctx.GetThreadPtr()),
52973ca05a2SJim Ingham                      return_valobj_sp.get()
53073ca05a2SJim Ingham                         ? return_valobj_sp->GetValueAsCString()
53173ca05a2SJim Ingham                         : "<no return value>");
53273ca05a2SJim Ingham 
53373ca05a2SJim Ingham     return SBValue (return_valobj_sp);
53473ca05a2SJim Ingham }
53573ca05a2SJim Ingham 
53630fdc8d8SChris Lattner void
53730fdc8d8SChris Lattner SBThread::SetThread (const ThreadSP& lldb_object_sp)
53830fdc8d8SChris Lattner {
5397fdf9ef1SGreg Clayton     m_opaque_sp->SetThreadSP (lldb_object_sp);
54030fdc8d8SChris Lattner }
54130fdc8d8SChris Lattner 
54230fdc8d8SChris Lattner lldb::tid_t
54330fdc8d8SChris Lattner SBThread::GetThreadID () const
54430fdc8d8SChris Lattner {
5457fdf9ef1SGreg Clayton     ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
54617a6ad05SGreg Clayton     if (thread_sp)
5471ac04c30SGreg Clayton         return thread_sp->GetID();
5481ac04c30SGreg Clayton     return LLDB_INVALID_THREAD_ID;
54930fdc8d8SChris Lattner }
55030fdc8d8SChris Lattner 
55130fdc8d8SChris Lattner uint32_t
55230fdc8d8SChris Lattner SBThread::GetIndexID () const
55330fdc8d8SChris Lattner {
5547fdf9ef1SGreg Clayton     ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
55517a6ad05SGreg Clayton     if (thread_sp)
55617a6ad05SGreg Clayton         return thread_sp->GetIndexID();
55730fdc8d8SChris Lattner     return LLDB_INVALID_INDEX32;
55830fdc8d8SChris Lattner }
5591ac04c30SGreg Clayton 
56030fdc8d8SChris Lattner const char *
56130fdc8d8SChris Lattner SBThread::GetName () const
56230fdc8d8SChris Lattner {
5635160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
5644838131bSGreg Clayton     const char *name = NULL;
565*bb19a13cSSaleem Abdulrasool     std::unique_lock<std::recursive_mutex> lock;
566*bb19a13cSSaleem Abdulrasool     ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
5674fc6cb9cSJim Ingham 
5681ac04c30SGreg Clayton     if (exe_ctx.HasThreadScope())
569af67cecdSGreg Clayton     {
5707fdf9ef1SGreg Clayton         Process::StopLocker stop_locker;
5717fdf9ef1SGreg Clayton         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
5727fdf9ef1SGreg Clayton         {
5731ac04c30SGreg Clayton             name = exe_ctx.GetThreadPtr()->GetName();
574af67cecdSGreg Clayton         }
575c9858e4dSGreg Clayton         else
576c9858e4dSGreg Clayton         {
577c9858e4dSGreg Clayton             if (log)
578324a1036SSaleem Abdulrasool                 log->Printf ("SBThread(%p)::GetName() => error: process is running",
579324a1036SSaleem Abdulrasool                              static_cast<void*>(exe_ctx.GetThreadPtr()));
580c9858e4dSGreg Clayton         }
5817fdf9ef1SGreg Clayton     }
582ceb6b139SCaroline Tice 
583ceb6b139SCaroline Tice     if (log)
584324a1036SSaleem Abdulrasool         log->Printf ("SBThread(%p)::GetName () => %s",
585324a1036SSaleem Abdulrasool                      static_cast<void*>(exe_ctx.GetThreadPtr()),
586324a1036SSaleem Abdulrasool                      name ? name : "NULL");
587ceb6b139SCaroline Tice 
5884838131bSGreg Clayton     return name;
58930fdc8d8SChris Lattner }
59030fdc8d8SChris Lattner 
59130fdc8d8SChris Lattner const char *
59230fdc8d8SChris Lattner SBThread::GetQueueName () const
59330fdc8d8SChris Lattner {
5944838131bSGreg Clayton     const char *name = NULL;
595*bb19a13cSSaleem Abdulrasool     std::unique_lock<std::recursive_mutex> lock;
596*bb19a13cSSaleem Abdulrasool     ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
5974fc6cb9cSJim Ingham 
5985160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
5991ac04c30SGreg Clayton     if (exe_ctx.HasThreadScope())
600af67cecdSGreg Clayton     {
6017fdf9ef1SGreg Clayton         Process::StopLocker stop_locker;
6027fdf9ef1SGreg Clayton         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
6037fdf9ef1SGreg Clayton         {
6041ac04c30SGreg Clayton             name = exe_ctx.GetThreadPtr()->GetQueueName();
605af67cecdSGreg Clayton         }
606c9858e4dSGreg Clayton         else
607c9858e4dSGreg Clayton         {
608c9858e4dSGreg Clayton             if (log)
609324a1036SSaleem Abdulrasool                 log->Printf ("SBThread(%p)::GetQueueName() => error: process is running",
610324a1036SSaleem Abdulrasool                              static_cast<void*>(exe_ctx.GetThreadPtr()));
611c9858e4dSGreg Clayton         }
6127fdf9ef1SGreg Clayton     }
613ceb6b139SCaroline Tice 
614ceb6b139SCaroline Tice     if (log)
615324a1036SSaleem Abdulrasool         log->Printf ("SBThread(%p)::GetQueueName () => %s",
616324a1036SSaleem Abdulrasool                      static_cast<void*>(exe_ctx.GetThreadPtr()),
617324a1036SSaleem Abdulrasool                      name ? name : "NULL");
618ceb6b139SCaroline Tice 
6194838131bSGreg Clayton     return name;
62030fdc8d8SChris Lattner }
62130fdc8d8SChris Lattner 
6224fdb5863SJason Molenda lldb::queue_id_t
6234fdb5863SJason Molenda SBThread::GetQueueID () const
6244fdb5863SJason Molenda {
6254fdb5863SJason Molenda     queue_id_t id = LLDB_INVALID_QUEUE_ID;
626*bb19a13cSSaleem Abdulrasool     std::unique_lock<std::recursive_mutex> lock;
627*bb19a13cSSaleem Abdulrasool     ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
6284fdb5863SJason Molenda 
6294fdb5863SJason Molenda     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
6304fdb5863SJason Molenda     if (exe_ctx.HasThreadScope())
6314fdb5863SJason Molenda     {
6324fdb5863SJason Molenda         Process::StopLocker stop_locker;
6334fdb5863SJason Molenda         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
6344fdb5863SJason Molenda         {
6354fdb5863SJason Molenda             id = exe_ctx.GetThreadPtr()->GetQueueID();
6364fdb5863SJason Molenda         }
6374fdb5863SJason Molenda         else
6384fdb5863SJason Molenda         {
6394fdb5863SJason Molenda             if (log)
640324a1036SSaleem Abdulrasool                 log->Printf ("SBThread(%p)::GetQueueID() => error: process is running",
641324a1036SSaleem Abdulrasool                              static_cast<void*>(exe_ctx.GetThreadPtr()));
6424fdb5863SJason Molenda         }
6434fdb5863SJason Molenda     }
6444fdb5863SJason Molenda 
6454fdb5863SJason Molenda     if (log)
646324a1036SSaleem Abdulrasool         log->Printf ("SBThread(%p)::GetQueueID () => 0x%" PRIx64,
647324a1036SSaleem Abdulrasool                      static_cast<void*>(exe_ctx.GetThreadPtr()), id);
6484fdb5863SJason Molenda 
6494fdb5863SJason Molenda     return id;
6504fdb5863SJason Molenda }
6514fdb5863SJason Molenda 
652705b1809SJason Molenda bool
653705b1809SJason Molenda SBThread::GetInfoItemByPathAsString (const char *path, SBStream &strm)
654705b1809SJason Molenda {
655705b1809SJason Molenda     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
656705b1809SJason Molenda     bool success = false;
657*bb19a13cSSaleem Abdulrasool     std::unique_lock<std::recursive_mutex> lock;
658*bb19a13cSSaleem Abdulrasool     ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
659705b1809SJason Molenda 
660705b1809SJason Molenda     if (exe_ctx.HasThreadScope())
661705b1809SJason Molenda     {
662705b1809SJason Molenda         Process::StopLocker stop_locker;
663705b1809SJason Molenda         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
664705b1809SJason Molenda         {
665705b1809SJason Molenda             Thread *thread = exe_ctx.GetThreadPtr();
666705b1809SJason Molenda             StructuredData::ObjectSP info_root_sp = thread->GetExtendedInfo();
667705b1809SJason Molenda             if (info_root_sp)
668705b1809SJason Molenda             {
669705b1809SJason Molenda                 StructuredData::ObjectSP node = info_root_sp->GetObjectForDotSeparatedPath (path);
670705b1809SJason Molenda                 if (node)
671705b1809SJason Molenda                 {
672705b1809SJason Molenda                     if (node->GetType() == StructuredData::Type::eTypeString)
673705b1809SJason Molenda                     {
674705b1809SJason Molenda                         strm.Printf ("%s", node->GetAsString()->GetValue().c_str());
675705b1809SJason Molenda                         success = true;
676705b1809SJason Molenda                     }
677705b1809SJason Molenda                     if (node->GetType() == StructuredData::Type::eTypeInteger)
678705b1809SJason Molenda                     {
679705b1809SJason Molenda                         strm.Printf ("0x%" PRIx64, node->GetAsInteger()->GetValue());
680705b1809SJason Molenda                         success = true;
681705b1809SJason Molenda                     }
682705b1809SJason Molenda                     if (node->GetType() == StructuredData::Type::eTypeFloat)
683705b1809SJason Molenda                     {
684705b1809SJason Molenda                         strm.Printf ("0x%f", node->GetAsFloat()->GetValue());
685705b1809SJason Molenda                         success = true;
686705b1809SJason Molenda                     }
687705b1809SJason Molenda                     if (node->GetType() == StructuredData::Type::eTypeBoolean)
688705b1809SJason Molenda                     {
689705b1809SJason Molenda                         if (node->GetAsBoolean()->GetValue() == true)
690705b1809SJason Molenda                             strm.Printf ("true");
691705b1809SJason Molenda                         else
692705b1809SJason Molenda                             strm.Printf ("false");
693705b1809SJason Molenda                         success = true;
694705b1809SJason Molenda                     }
695705b1809SJason Molenda                     if (node->GetType() == StructuredData::Type::eTypeNull)
696705b1809SJason Molenda                     {
697705b1809SJason Molenda                         strm.Printf ("null");
698705b1809SJason Molenda                         success = true;
699705b1809SJason Molenda                     }
700705b1809SJason Molenda                 }
701705b1809SJason Molenda             }
702705b1809SJason Molenda         }
703705b1809SJason Molenda         else
704705b1809SJason Molenda         {
705705b1809SJason Molenda             if (log)
706705b1809SJason Molenda                 log->Printf ("SBThread(%p)::GetInfoItemByPathAsString() => error: process is running",
707705b1809SJason Molenda                              static_cast<void*>(exe_ctx.GetThreadPtr()));
708705b1809SJason Molenda         }
709705b1809SJason Molenda     }
710705b1809SJason Molenda 
711705b1809SJason Molenda     if (log)
712705b1809SJason Molenda         log->Printf ("SBThread(%p)::GetInfoItemByPathAsString () => %s",
713705b1809SJason Molenda                      static_cast<void*>(exe_ctx.GetThreadPtr()),
714705b1809SJason Molenda                      strm.GetData());
715705b1809SJason Molenda 
716705b1809SJason Molenda     return success;
717705b1809SJason Molenda }
718705b1809SJason Molenda 
719705b1809SJason Molenda 
72064e7ead1SJim Ingham SBError
72164e7ead1SJim Ingham SBThread::ResumeNewPlan (ExecutionContext &exe_ctx, ThreadPlan *new_plan)
72264e7ead1SJim Ingham {
72364e7ead1SJim Ingham     SBError sb_error;
72464e7ead1SJim Ingham 
72564e7ead1SJim Ingham     Process *process = exe_ctx.GetProcessPtr();
72664e7ead1SJim Ingham     if (!process)
72764e7ead1SJim Ingham     {
72864e7ead1SJim Ingham         sb_error.SetErrorString("No process in SBThread::ResumeNewPlan");
72964e7ead1SJim Ingham         return sb_error;
73064e7ead1SJim Ingham     }
73164e7ead1SJim Ingham 
73264e7ead1SJim Ingham     Thread *thread = exe_ctx.GetThreadPtr();
73364e7ead1SJim Ingham     if (!thread)
73464e7ead1SJim Ingham     {
73564e7ead1SJim Ingham         sb_error.SetErrorString("No thread in SBThread::ResumeNewPlan");
73664e7ead1SJim Ingham         return sb_error;
73764e7ead1SJim Ingham     }
73864e7ead1SJim Ingham 
73964e7ead1SJim Ingham     // User level plans should be Master Plans so they can be interrupted, other plans executed, and
74064e7ead1SJim Ingham     // then a "continue" will resume the plan.
74164e7ead1SJim Ingham     if (new_plan != NULL)
74264e7ead1SJim Ingham     {
74364e7ead1SJim Ingham         new_plan->SetIsMasterPlan(true);
74464e7ead1SJim Ingham         new_plan->SetOkayToDiscard(false);
74564e7ead1SJim Ingham     }
74664e7ead1SJim Ingham 
74764e7ead1SJim Ingham     // Why do we need to set the current thread by ID here???
74864e7ead1SJim Ingham     process->GetThreadList().SetSelectedThreadByID (thread->GetID());
74964e7ead1SJim Ingham 
750dc6224e0SGreg Clayton     if (process->GetTarget().GetDebugger().GetAsyncExecution ())
751dc6224e0SGreg Clayton         sb_error.ref() = process->Resume ();
752dc6224e0SGreg Clayton     else
753dc6224e0SGreg Clayton         sb_error.ref() = process->ResumeSynchronous (NULL);
75464e7ead1SJim Ingham 
75564e7ead1SJim Ingham     return sb_error;
75664e7ead1SJim Ingham }
75730fdc8d8SChris Lattner 
75830fdc8d8SChris Lattner void
75930fdc8d8SChris Lattner SBThread::StepOver (lldb::RunMode stop_other_threads)
76030fdc8d8SChris Lattner {
7615160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
762ceb6b139SCaroline Tice 
763*bb19a13cSSaleem Abdulrasool     std::unique_lock<std::recursive_mutex> lock;
764*bb19a13cSSaleem Abdulrasool     ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
76517a6ad05SGreg Clayton 
766ceb6b139SCaroline Tice     if (log)
767324a1036SSaleem Abdulrasool         log->Printf ("SBThread(%p)::StepOver (stop_other_threads='%s')",
768324a1036SSaleem Abdulrasool                      static_cast<void*>(exe_ctx.GetThreadPtr()),
769ceb6b139SCaroline Tice                      Thread::RunModeAsCString (stop_other_threads));
770ceb6b139SCaroline Tice 
7711ac04c30SGreg Clayton     if (exe_ctx.HasThreadScope())
77230fdc8d8SChris Lattner     {
7731ac04c30SGreg Clayton         Thread *thread = exe_ctx.GetThreadPtr();
7747ba6e991SJim Ingham         bool abort_other_plans = false;
775b57e4a1bSJason Molenda         StackFrameSP frame_sp(thread->GetStackFrameAtIndex (0));
77630fdc8d8SChris Lattner 
7774d56e9c1SJim Ingham         ThreadPlanSP new_plan_sp;
77830fdc8d8SChris Lattner         if (frame_sp)
77930fdc8d8SChris Lattner         {
78030fdc8d8SChris Lattner             if (frame_sp->HasDebugInformation ())
78130fdc8d8SChris Lattner             {
7824b4b2478SJim Ingham                 const LazyBool avoid_no_debug = eLazyBoolCalculate;
78330fdc8d8SChris Lattner                 SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
7844d56e9c1SJim Ingham                 new_plan_sp = thread->QueueThreadPlanForStepOverRange (abort_other_plans,
78525d5b10bSJason Molenda                                                                     sc.line_entry,
78630fdc8d8SChris Lattner                                                                     sc,
7874b4b2478SJim Ingham                                                                     stop_other_threads,
7884b4b2478SJim Ingham                                                                     avoid_no_debug);
78930fdc8d8SChris Lattner             }
79030fdc8d8SChris Lattner             else
79130fdc8d8SChris Lattner             {
7924d56e9c1SJim Ingham                 new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction (true,
79330fdc8d8SChris Lattner                                                                                abort_other_plans,
79430fdc8d8SChris Lattner                                                                                stop_other_threads);
79530fdc8d8SChris Lattner             }
79630fdc8d8SChris Lattner         }
79730fdc8d8SChris Lattner 
79864e7ead1SJim Ingham         // This returns an error, we should use it!
7994d56e9c1SJim Ingham         ResumeNewPlan (exe_ctx, new_plan_sp.get());
80030fdc8d8SChris Lattner     }
80130fdc8d8SChris Lattner }
80230fdc8d8SChris Lattner 
80330fdc8d8SChris Lattner void
80430fdc8d8SChris Lattner SBThread::StepInto (lldb::RunMode stop_other_threads)
80530fdc8d8SChris Lattner {
806c627682eSJim Ingham     StepInto (NULL, stop_other_threads);
807c627682eSJim Ingham }
808c627682eSJim Ingham 
809c627682eSJim Ingham void
810c627682eSJim Ingham SBThread::StepInto (const char *target_name, lldb::RunMode stop_other_threads)
811c627682eSJim Ingham {
812cbf6f9b2SJim Ingham     SBError error;
813cbf6f9b2SJim Ingham     StepInto(target_name, LLDB_INVALID_LINE_NUMBER, error, stop_other_threads);
814cbf6f9b2SJim Ingham }
815cbf6f9b2SJim Ingham 
816cbf6f9b2SJim Ingham void
817cbf6f9b2SJim Ingham SBThread::StepInto (const char *target_name, uint32_t end_line, SBError &error, lldb::RunMode stop_other_threads)
818cbf6f9b2SJim Ingham {
8195160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
820ceb6b139SCaroline Tice 
821*bb19a13cSSaleem Abdulrasool     std::unique_lock<std::recursive_mutex> lock;
822*bb19a13cSSaleem Abdulrasool     ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
82317a6ad05SGreg Clayton 
82417a6ad05SGreg Clayton     if (log)
825c627682eSJim Ingham         log->Printf ("SBThread(%p)::StepInto (target_name='%s', stop_other_threads='%s')",
826324a1036SSaleem Abdulrasool                      static_cast<void*>(exe_ctx.GetThreadPtr()),
827c627682eSJim Ingham                      target_name? target_name: "<NULL>",
82817a6ad05SGreg Clayton                      Thread::RunModeAsCString (stop_other_threads));
829c627682eSJim Ingham 
8301ac04c30SGreg Clayton     if (exe_ctx.HasThreadScope())
83130fdc8d8SChris Lattner     {
8327ba6e991SJim Ingham         bool abort_other_plans = false;
83330fdc8d8SChris Lattner 
8341ac04c30SGreg Clayton         Thread *thread = exe_ctx.GetThreadPtr();
835b57e4a1bSJason Molenda         StackFrameSP frame_sp(thread->GetStackFrameAtIndex (0));
8364d56e9c1SJim Ingham         ThreadPlanSP new_plan_sp;
83730fdc8d8SChris Lattner 
83830fdc8d8SChris Lattner         if (frame_sp && frame_sp->HasDebugInformation ())
83930fdc8d8SChris Lattner         {
840cbf6f9b2SJim Ingham             SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
841cbf6f9b2SJim Ingham             AddressRange range;
842cbf6f9b2SJim Ingham             if (end_line == LLDB_INVALID_LINE_NUMBER)
843cbf6f9b2SJim Ingham                 range = sc.line_entry.range;
844cbf6f9b2SJim Ingham             else
845cbf6f9b2SJim Ingham             {
846cbf6f9b2SJim Ingham                 if (!sc.GetAddressRangeFromHereToEndLine(end_line, range, error.ref()))
847cbf6f9b2SJim Ingham                     return;
848cbf6f9b2SJim Ingham             }
849cbf6f9b2SJim Ingham 
8504b4b2478SJim Ingham             const LazyBool step_out_avoids_code_without_debug_info = eLazyBoolCalculate;
8514b4b2478SJim Ingham             const LazyBool step_in_avoids_code_without_debug_info = eLazyBoolCalculate;
8524d56e9c1SJim Ingham             new_plan_sp = thread->QueueThreadPlanForStepInRange (abort_other_plans,
853cbf6f9b2SJim Ingham                                                               range,
85430fdc8d8SChris Lattner                                                               sc,
855c627682eSJim Ingham                                                               target_name,
856474966a4SGreg Clayton                                                               stop_other_threads,
8574b4b2478SJim Ingham                                                               step_in_avoids_code_without_debug_info,
8584b4b2478SJim Ingham                                                               step_out_avoids_code_without_debug_info);
85930fdc8d8SChris Lattner         }
86030fdc8d8SChris Lattner         else
86130fdc8d8SChris Lattner         {
8624d56e9c1SJim Ingham             new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction (false,
86330fdc8d8SChris Lattner                                                                            abort_other_plans,
86430fdc8d8SChris Lattner                                                                            stop_other_threads);
86530fdc8d8SChris Lattner         }
86630fdc8d8SChris Lattner 
867cbf6f9b2SJim Ingham         error = ResumeNewPlan (exe_ctx, new_plan_sp.get());
86830fdc8d8SChris Lattner     }
86930fdc8d8SChris Lattner }
87030fdc8d8SChris Lattner 
87130fdc8d8SChris Lattner void
87230fdc8d8SChris Lattner SBThread::StepOut ()
87330fdc8d8SChris Lattner {
8745160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
875ceb6b139SCaroline Tice 
876*bb19a13cSSaleem Abdulrasool     std::unique_lock<std::recursive_mutex> lock;
877*bb19a13cSSaleem Abdulrasool     ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
8784fc6cb9cSJim Ingham 
87917a6ad05SGreg Clayton     if (log)
880324a1036SSaleem Abdulrasool         log->Printf ("SBThread(%p)::StepOut ()",
881324a1036SSaleem Abdulrasool                      static_cast<void*>(exe_ctx.GetThreadPtr()));
88217a6ad05SGreg Clayton 
8831ac04c30SGreg Clayton     if (exe_ctx.HasThreadScope())
88430fdc8d8SChris Lattner     {
8857ba6e991SJim Ingham         bool abort_other_plans = false;
88694b09246SJim Ingham         bool stop_other_threads = false;
88730fdc8d8SChris Lattner 
8881ac04c30SGreg Clayton         Thread *thread = exe_ctx.GetThreadPtr();
8891ac04c30SGreg Clayton 
8904b4b2478SJim Ingham         const LazyBool avoid_no_debug = eLazyBoolCalculate;
8914d56e9c1SJim Ingham         ThreadPlanSP new_plan_sp(thread->QueueThreadPlanForStepOut (abort_other_plans,
892481cef25SGreg Clayton                                                                     NULL,
893481cef25SGreg Clayton                                                                     false,
894481cef25SGreg Clayton                                                                     stop_other_threads,
895481cef25SGreg Clayton                                                                     eVoteYes,
896481cef25SGreg Clayton                                                                     eVoteNoOpinion,
8974b4b2478SJim Ingham                                                                     0,
8984b4b2478SJim Ingham                                                                     avoid_no_debug));
899481cef25SGreg Clayton 
90064e7ead1SJim Ingham         // This returns an error, we should use it!
9014d56e9c1SJim Ingham         ResumeNewPlan (exe_ctx, new_plan_sp.get());
902481cef25SGreg Clayton     }
903481cef25SGreg Clayton }
904481cef25SGreg Clayton 
905481cef25SGreg Clayton void
906481cef25SGreg Clayton SBThread::StepOutOfFrame (lldb::SBFrame &sb_frame)
907481cef25SGreg Clayton {
9085160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
909481cef25SGreg Clayton 
910*bb19a13cSSaleem Abdulrasool     std::unique_lock<std::recursive_mutex> lock;
911*bb19a13cSSaleem Abdulrasool     ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
9124fc6cb9cSJim Ingham 
913989a7558SJim Ingham     if (!sb_frame.IsValid())
914989a7558SJim Ingham     {
915989a7558SJim Ingham         if (log)
916989a7558SJim Ingham             log->Printf("SBThread(%p)::StepOutOfFrame passed an invalid frame, returning.",
917989a7558SJim Ingham                         static_cast<void*>(exe_ctx.GetThreadPtr()));
918989a7558SJim Ingham         return;
919989a7558SJim Ingham     }
920989a7558SJim Ingham 
921b57e4a1bSJason Molenda     StackFrameSP frame_sp (sb_frame.GetFrameSP());
922481cef25SGreg Clayton     if (log)
923481cef25SGreg Clayton     {
924481cef25SGreg Clayton         SBStream frame_desc_strm;
925481cef25SGreg Clayton         sb_frame.GetDescription (frame_desc_strm);
926324a1036SSaleem Abdulrasool         log->Printf ("SBThread(%p)::StepOutOfFrame (frame = SBFrame(%p): %s)",
927324a1036SSaleem Abdulrasool                      static_cast<void*>(exe_ctx.GetThreadPtr()),
928324a1036SSaleem Abdulrasool                      static_cast<void*>(frame_sp.get()),
929324a1036SSaleem Abdulrasool                      frame_desc_strm.GetData());
930481cef25SGreg Clayton     }
931481cef25SGreg Clayton 
9321ac04c30SGreg Clayton     if (exe_ctx.HasThreadScope())
933481cef25SGreg Clayton     {
9347ba6e991SJim Ingham         bool abort_other_plans = false;
93594b09246SJim Ingham         bool stop_other_threads = false;
9361ac04c30SGreg Clayton         Thread *thread = exe_ctx.GetThreadPtr();
937989a7558SJim Ingham         if (sb_frame.GetThread().GetThreadID() != thread->GetID())
938989a7558SJim Ingham         {
9391ef6e4c8SBruce Mitchener             log->Printf("SBThread(%p)::StepOutOfFrame passed a frame from another thread (0x%" PRIx64 " vrs. 0x%" PRIx64 ", returning.",
940989a7558SJim Ingham                         static_cast<void*>(exe_ctx.GetThreadPtr()),
941989a7558SJim Ingham                         sb_frame.GetThread().GetThreadID(),
942989a7558SJim Ingham                         thread->GetID());
943989a7558SJim Ingham         }
944481cef25SGreg Clayton 
9454d56e9c1SJim Ingham         ThreadPlanSP new_plan_sp(thread->QueueThreadPlanForStepOut (abort_other_plans,
946481cef25SGreg Clayton                                                                     NULL,
947481cef25SGreg Clayton                                                                     false,
948481cef25SGreg Clayton                                                                     stop_other_threads,
949481cef25SGreg Clayton                                                                     eVoteYes,
950481cef25SGreg Clayton                                                                     eVoteNoOpinion,
9514d56e9c1SJim Ingham                                                                     frame_sp->GetFrameIndex()));
95230fdc8d8SChris Lattner 
95364e7ead1SJim Ingham         // This returns an error, we should use it!
9544d56e9c1SJim Ingham         ResumeNewPlan (exe_ctx, new_plan_sp.get());
95530fdc8d8SChris Lattner     }
95630fdc8d8SChris Lattner }
95730fdc8d8SChris Lattner 
95830fdc8d8SChris Lattner void
95930fdc8d8SChris Lattner SBThread::StepInstruction (bool step_over)
96030fdc8d8SChris Lattner {
9615160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
962ceb6b139SCaroline Tice 
963*bb19a13cSSaleem Abdulrasool     std::unique_lock<std::recursive_mutex> lock;
964*bb19a13cSSaleem Abdulrasool     ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
965ceb6b139SCaroline Tice 
96617a6ad05SGreg Clayton     if (log)
967324a1036SSaleem Abdulrasool         log->Printf ("SBThread(%p)::StepInstruction (step_over=%i)",
968324a1036SSaleem Abdulrasool                      static_cast<void*>(exe_ctx.GetThreadPtr()), step_over);
96917a6ad05SGreg Clayton 
9701ac04c30SGreg Clayton     if (exe_ctx.HasThreadScope())
97130fdc8d8SChris Lattner     {
9721ac04c30SGreg Clayton         Thread *thread = exe_ctx.GetThreadPtr();
9734d56e9c1SJim Ingham         ThreadPlanSP new_plan_sp(thread->QueueThreadPlanForStepSingleInstruction (step_over, true, true));
97464e7ead1SJim Ingham 
97564e7ead1SJim Ingham         // This returns an error, we should use it!
9764d56e9c1SJim Ingham         ResumeNewPlan (exe_ctx, new_plan_sp.get());
97730fdc8d8SChris Lattner     }
97830fdc8d8SChris Lattner }
97930fdc8d8SChris Lattner 
98030fdc8d8SChris Lattner void
98130fdc8d8SChris Lattner SBThread::RunToAddress (lldb::addr_t addr)
98230fdc8d8SChris Lattner {
9835160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
984ceb6b139SCaroline Tice 
985*bb19a13cSSaleem Abdulrasool     std::unique_lock<std::recursive_mutex> lock;
986*bb19a13cSSaleem Abdulrasool     ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
987ceb6b139SCaroline Tice 
98817a6ad05SGreg Clayton     if (log)
989324a1036SSaleem Abdulrasool         log->Printf ("SBThread(%p)::RunToAddress (addr=0x%" PRIx64 ")",
990324a1036SSaleem Abdulrasool                      static_cast<void*>(exe_ctx.GetThreadPtr()), addr);
99117a6ad05SGreg Clayton 
9921ac04c30SGreg Clayton     if (exe_ctx.HasThreadScope())
99330fdc8d8SChris Lattner     {
9947ba6e991SJim Ingham         bool abort_other_plans = false;
99530fdc8d8SChris Lattner         bool stop_other_threads = true;
99630fdc8d8SChris Lattner 
997e72dfb32SGreg Clayton         Address target_addr (addr);
99830fdc8d8SChris Lattner 
9991ac04c30SGreg Clayton         Thread *thread = exe_ctx.GetThreadPtr();
10001ac04c30SGreg Clayton 
10012bdbfd50SJim Ingham         ThreadPlanSP new_plan_sp(thread->QueueThreadPlanForRunToAddress (abort_other_plans,
10022bdbfd50SJim Ingham                                                                          target_addr,
10032bdbfd50SJim Ingham                                                                          stop_other_threads));
100464e7ead1SJim Ingham 
100564e7ead1SJim Ingham         // This returns an error, we should use it!
10064d56e9c1SJim Ingham         ResumeNewPlan (exe_ctx, new_plan_sp.get());
100730fdc8d8SChris Lattner     }
100830fdc8d8SChris Lattner }
100930fdc8d8SChris Lattner 
1010481cef25SGreg Clayton SBError
1011481cef25SGreg Clayton SBThread::StepOverUntil (lldb::SBFrame &sb_frame,
1012481cef25SGreg Clayton                          lldb::SBFileSpec &sb_file_spec,
1013481cef25SGreg Clayton                          uint32_t line)
1014481cef25SGreg Clayton {
1015481cef25SGreg Clayton     SBError sb_error;
10165160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1017481cef25SGreg Clayton     char path[PATH_MAX];
1018481cef25SGreg Clayton 
1019*bb19a13cSSaleem Abdulrasool     std::unique_lock<std::recursive_mutex> lock;
1020*bb19a13cSSaleem Abdulrasool     ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
10214fc6cb9cSJim Ingham 
1022b57e4a1bSJason Molenda     StackFrameSP frame_sp (sb_frame.GetFrameSP());
102317a6ad05SGreg Clayton 
1024481cef25SGreg Clayton     if (log)
1025481cef25SGreg Clayton     {
1026481cef25SGreg Clayton         SBStream frame_desc_strm;
1027481cef25SGreg Clayton         sb_frame.GetDescription (frame_desc_strm);
1028481cef25SGreg Clayton         sb_file_spec->GetPath (path, sizeof(path));
1029481cef25SGreg Clayton         log->Printf ("SBThread(%p)::StepOverUntil (frame = SBFrame(%p): %s, file+line = %s:%u)",
1030324a1036SSaleem Abdulrasool                      static_cast<void*>(exe_ctx.GetThreadPtr()),
1031324a1036SSaleem Abdulrasool                      static_cast<void*>(frame_sp.get()),
1032324a1036SSaleem Abdulrasool                      frame_desc_strm.GetData(), path, line);
1033481cef25SGreg Clayton     }
1034481cef25SGreg Clayton 
10351ac04c30SGreg Clayton     if (exe_ctx.HasThreadScope())
1036481cef25SGreg Clayton     {
10371ac04c30SGreg Clayton         Target *target = exe_ctx.GetTargetPtr();
10381ac04c30SGreg Clayton         Thread *thread = exe_ctx.GetThreadPtr();
1039481cef25SGreg Clayton 
1040481cef25SGreg Clayton         if (line == 0)
1041481cef25SGreg Clayton         {
1042481cef25SGreg Clayton             sb_error.SetErrorString("invalid line argument");
1043481cef25SGreg Clayton             return sb_error;
1044481cef25SGreg Clayton         }
1045481cef25SGreg Clayton 
1046b9556accSGreg Clayton         if (!frame_sp)
1047481cef25SGreg Clayton         {
10481ac04c30SGreg Clayton             frame_sp = thread->GetSelectedFrame ();
1049481cef25SGreg Clayton             if (!frame_sp)
10501ac04c30SGreg Clayton                 frame_sp = thread->GetStackFrameAtIndex (0);
1051481cef25SGreg Clayton         }
1052481cef25SGreg Clayton 
1053481cef25SGreg Clayton         SymbolContext frame_sc;
1054481cef25SGreg Clayton         if (!frame_sp)
1055481cef25SGreg Clayton         {
1056481cef25SGreg Clayton             sb_error.SetErrorString("no valid frames in thread to step");
1057481cef25SGreg Clayton             return sb_error;
1058481cef25SGreg Clayton         }
1059481cef25SGreg Clayton 
1060481cef25SGreg Clayton         // If we have a frame, get its line
1061481cef25SGreg Clayton         frame_sc = frame_sp->GetSymbolContext (eSymbolContextCompUnit  |
1062481cef25SGreg Clayton                                                eSymbolContextFunction  |
1063481cef25SGreg Clayton                                                eSymbolContextLineEntry |
1064481cef25SGreg Clayton                                                eSymbolContextSymbol    );
1065481cef25SGreg Clayton 
1066481cef25SGreg Clayton         if (frame_sc.comp_unit == NULL)
1067481cef25SGreg Clayton         {
1068481cef25SGreg Clayton             sb_error.SetErrorStringWithFormat("frame %u doesn't have debug information", frame_sp->GetFrameIndex());
1069481cef25SGreg Clayton             return sb_error;
1070481cef25SGreg Clayton         }
1071481cef25SGreg Clayton 
1072481cef25SGreg Clayton         FileSpec step_file_spec;
1073481cef25SGreg Clayton         if (sb_file_spec.IsValid())
1074481cef25SGreg Clayton         {
1075481cef25SGreg Clayton             // The file spec passed in was valid, so use it
1076481cef25SGreg Clayton             step_file_spec = sb_file_spec.ref();
1077481cef25SGreg Clayton         }
1078481cef25SGreg Clayton         else
1079481cef25SGreg Clayton         {
1080481cef25SGreg Clayton             if (frame_sc.line_entry.IsValid())
1081481cef25SGreg Clayton                 step_file_spec = frame_sc.line_entry.file;
1082481cef25SGreg Clayton             else
1083481cef25SGreg Clayton             {
1084481cef25SGreg Clayton                 sb_error.SetErrorString("invalid file argument or no file for frame");
1085481cef25SGreg Clayton                 return sb_error;
1086481cef25SGreg Clayton             }
1087481cef25SGreg Clayton         }
1088481cef25SGreg Clayton 
10899b70ddb3SJim Ingham         // Grab the current function, then we will make sure the "until" address is
10909b70ddb3SJim Ingham         // within the function.  We discard addresses that are out of the current
10919b70ddb3SJim Ingham         // function, and then if there are no addresses remaining, give an appropriate
10929b70ddb3SJim Ingham         // error message.
10939b70ddb3SJim Ingham 
10949b70ddb3SJim Ingham         bool all_in_function = true;
10959b70ddb3SJim Ingham         AddressRange fun_range = frame_sc.function->GetAddressRange();
10969b70ddb3SJim Ingham 
1097481cef25SGreg Clayton         std::vector<addr_t> step_over_until_addrs;
10987ba6e991SJim Ingham         const bool abort_other_plans = false;
1099c02e3344SJim Ingham         const bool stop_other_threads = false;
1100481cef25SGreg Clayton         const bool check_inlines = true;
1101481cef25SGreg Clayton         const bool exact = false;
1102481cef25SGreg Clayton 
1103481cef25SGreg Clayton         SymbolContextList sc_list;
11049b70ddb3SJim Ingham         const uint32_t num_matches = frame_sc.comp_unit->ResolveSymbolContext (step_file_spec,
11059b70ddb3SJim Ingham                                                                                line,
11069b70ddb3SJim Ingham                                                                                check_inlines,
11079b70ddb3SJim Ingham                                                                                exact,
11089b70ddb3SJim Ingham                                                                                eSymbolContextLineEntry,
11099b70ddb3SJim Ingham                                                                                sc_list);
1110481cef25SGreg Clayton         if (num_matches > 0)
1111481cef25SGreg Clayton         {
1112481cef25SGreg Clayton             SymbolContext sc;
1113481cef25SGreg Clayton             for (uint32_t i=0; i<num_matches; ++i)
1114481cef25SGreg Clayton             {
1115481cef25SGreg Clayton                 if (sc_list.GetContextAtIndex(i, sc))
1116481cef25SGreg Clayton                 {
11179b70ddb3SJim Ingham                     addr_t step_addr = sc.line_entry.range.GetBaseAddress().GetLoadAddress(target);
1118481cef25SGreg Clayton                     if (step_addr != LLDB_INVALID_ADDRESS)
1119481cef25SGreg Clayton                     {
11209b70ddb3SJim Ingham                         if (fun_range.ContainsLoadAddress(step_addr, target))
1121481cef25SGreg Clayton                             step_over_until_addrs.push_back(step_addr);
11229b70ddb3SJim Ingham                         else
11239b70ddb3SJim Ingham                             all_in_function = false;
1124481cef25SGreg Clayton                     }
1125481cef25SGreg Clayton                 }
1126481cef25SGreg Clayton             }
1127481cef25SGreg Clayton         }
1128481cef25SGreg Clayton 
1129481cef25SGreg Clayton         if (step_over_until_addrs.empty())
1130481cef25SGreg Clayton         {
11319b70ddb3SJim Ingham             if (all_in_function)
11329b70ddb3SJim Ingham             {
1133481cef25SGreg Clayton                 step_file_spec.GetPath (path, sizeof(path));
1134fd54b368SJason Molenda                 sb_error.SetErrorStringWithFormat("No line entries for %s:%u", path, line);
1135481cef25SGreg Clayton             }
1136481cef25SGreg Clayton             else
113786edbf41SGreg Clayton                 sb_error.SetErrorString ("step until target not in current function");
11389b70ddb3SJim Ingham         }
11399b70ddb3SJim Ingham         else
1140481cef25SGreg Clayton         {
11414d56e9c1SJim Ingham             ThreadPlanSP new_plan_sp(thread->QueueThreadPlanForStepUntil (abort_other_plans,
1142481cef25SGreg Clayton                                                                         &step_over_until_addrs[0],
1143481cef25SGreg Clayton                                                                         step_over_until_addrs.size(),
1144481cef25SGreg Clayton                                                                         stop_other_threads,
11454d56e9c1SJim Ingham                                                                         frame_sp->GetFrameIndex()));
1146481cef25SGreg Clayton 
11474d56e9c1SJim Ingham             sb_error = ResumeNewPlan (exe_ctx, new_plan_sp.get());
1148481cef25SGreg Clayton         }
1149481cef25SGreg Clayton     }
1150481cef25SGreg Clayton     else
1151481cef25SGreg Clayton     {
1152481cef25SGreg Clayton         sb_error.SetErrorString("this SBThread object is invalid");
1153481cef25SGreg Clayton     }
1154481cef25SGreg Clayton     return sb_error;
1155481cef25SGreg Clayton }
1156481cef25SGreg Clayton 
11574413758cSJim Ingham SBError
11582bdbfd50SJim Ingham SBThread::StepUsingScriptedThreadPlan (const char *script_class_name)
11592bdbfd50SJim Ingham {
11602bdbfd50SJim Ingham     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
11612bdbfd50SJim Ingham     SBError sb_error;
11622bdbfd50SJim Ingham 
1163*bb19a13cSSaleem Abdulrasool     std::unique_lock<std::recursive_mutex> lock;
1164*bb19a13cSSaleem Abdulrasool     ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
11652bdbfd50SJim Ingham 
11662bdbfd50SJim Ingham     if (log)
11672bdbfd50SJim Ingham     {
11682bdbfd50SJim Ingham         log->Printf ("SBThread(%p)::StepUsingScriptedThreadPlan: class name: %s",
11692bdbfd50SJim Ingham                      static_cast<void*>(exe_ctx.GetThreadPtr()),
11702bdbfd50SJim Ingham                      script_class_name);
11712bdbfd50SJim Ingham     }
11722bdbfd50SJim Ingham 
11732bdbfd50SJim Ingham 
11742bdbfd50SJim Ingham     if (!exe_ctx.HasThreadScope())
11752bdbfd50SJim Ingham     {
11762bdbfd50SJim Ingham         sb_error.SetErrorString("this SBThread object is invalid");
11772bdbfd50SJim Ingham         return sb_error;
11782bdbfd50SJim Ingham     }
11792bdbfd50SJim Ingham 
11802bdbfd50SJim Ingham     Thread *thread = exe_ctx.GetThreadPtr();
11812bdbfd50SJim Ingham     ThreadPlanSP thread_plan_sp = thread->QueueThreadPlanForStepScripted(false, script_class_name, false);
11822bdbfd50SJim Ingham 
11832bdbfd50SJim Ingham     if (thread_plan_sp)
11842bdbfd50SJim Ingham         sb_error = ResumeNewPlan(exe_ctx, thread_plan_sp.get());
11852bdbfd50SJim Ingham     else
11862bdbfd50SJim Ingham     {
11872bdbfd50SJim Ingham         sb_error.SetErrorStringWithFormat("Error queuing thread plan for class: %s.", script_class_name);
11882bdbfd50SJim Ingham         if (log)
11892bdbfd50SJim Ingham         log->Printf ("SBThread(%p)::StepUsingScriptedThreadPlan: Error queuing thread plan for class: %s",
11902bdbfd50SJim Ingham                      static_cast<void*>(exe_ctx.GetThreadPtr()),
11912bdbfd50SJim Ingham                      script_class_name);
11922bdbfd50SJim Ingham     }
11932bdbfd50SJim Ingham 
11942bdbfd50SJim Ingham     return sb_error;
11952bdbfd50SJim Ingham }
11962bdbfd50SJim Ingham 
11972bdbfd50SJim Ingham SBError
1198f86248d9SRichard Mitton SBThread::JumpToLine (lldb::SBFileSpec &file_spec, uint32_t line)
1199f86248d9SRichard Mitton {
1200f86248d9SRichard Mitton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1201f86248d9SRichard Mitton     SBError sb_error;
1202f86248d9SRichard Mitton 
1203*bb19a13cSSaleem Abdulrasool     std::unique_lock<std::recursive_mutex> lock;
1204*bb19a13cSSaleem Abdulrasool     ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1205f86248d9SRichard Mitton 
1206f86248d9SRichard Mitton     if (log)
1207324a1036SSaleem Abdulrasool         log->Printf ("SBThread(%p)::JumpToLine (file+line = %s:%u)",
1208324a1036SSaleem Abdulrasool                      static_cast<void*>(exe_ctx.GetThreadPtr()),
1209324a1036SSaleem Abdulrasool                      file_spec->GetPath().c_str(), line);
1210f86248d9SRichard Mitton 
1211f86248d9SRichard Mitton     if (!exe_ctx.HasThreadScope())
1212f86248d9SRichard Mitton     {
1213f86248d9SRichard Mitton         sb_error.SetErrorString("this SBThread object is invalid");
1214f86248d9SRichard Mitton         return sb_error;
1215f86248d9SRichard Mitton     }
1216f86248d9SRichard Mitton 
1217f86248d9SRichard Mitton     Thread *thread = exe_ctx.GetThreadPtr();
1218f86248d9SRichard Mitton 
1219f86248d9SRichard Mitton     Error err = thread->JumpToLine (file_spec.get(), line, true);
1220f86248d9SRichard Mitton     sb_error.SetError (err);
1221f86248d9SRichard Mitton     return sb_error;
1222f86248d9SRichard Mitton }
1223f86248d9SRichard Mitton 
1224f86248d9SRichard Mitton SBError
1225cb640dd8SJim Ingham SBThread::ReturnFromFrame (SBFrame &frame, SBValue &return_value)
12264413758cSJim Ingham {
12274413758cSJim Ingham     SBError sb_error;
12284413758cSJim Ingham 
12295160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
12304413758cSJim Ingham 
1231*bb19a13cSSaleem Abdulrasool     std::unique_lock<std::recursive_mutex> lock;
1232*bb19a13cSSaleem Abdulrasool     ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
12334413758cSJim Ingham 
12344413758cSJim Ingham     if (log)
1235324a1036SSaleem Abdulrasool         log->Printf ("SBThread(%p)::ReturnFromFrame (frame=%d)",
1236324a1036SSaleem Abdulrasool                      static_cast<void*>(exe_ctx.GetThreadPtr()),
1237324a1036SSaleem Abdulrasool                      frame.GetFrameID());
12384413758cSJim Ingham 
12394413758cSJim Ingham     if (exe_ctx.HasThreadScope())
12404413758cSJim Ingham     {
12414413758cSJim Ingham         Thread *thread = exe_ctx.GetThreadPtr();
1242cb640dd8SJim Ingham         sb_error.SetError (thread->ReturnFromFrame(frame.GetFrameSP(), return_value.GetSP()));
12434413758cSJim Ingham     }
12444413758cSJim Ingham 
12454413758cSJim Ingham     return sb_error;
12464413758cSJim Ingham }
12474413758cSJim Ingham 
1248481cef25SGreg Clayton 
1249722a0cdcSGreg Clayton bool
1250722a0cdcSGreg Clayton SBThread::Suspend()
1251722a0cdcSGreg Clayton {
12525160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
12537fdf9ef1SGreg Clayton     ExecutionContext exe_ctx (m_opaque_sp.get());
1254c9858e4dSGreg Clayton     bool result = false;
12551ac04c30SGreg Clayton     if (exe_ctx.HasThreadScope())
1256722a0cdcSGreg Clayton     {
1257c9858e4dSGreg Clayton         Process::StopLocker stop_locker;
1258c9858e4dSGreg Clayton         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1259c9858e4dSGreg Clayton         {
12601ac04c30SGreg Clayton             exe_ctx.GetThreadPtr()->SetResumeState (eStateSuspended);
1261c9858e4dSGreg Clayton             result = true;
1262722a0cdcSGreg Clayton         }
1263c9858e4dSGreg Clayton         else
1264c9858e4dSGreg Clayton         {
1265c9858e4dSGreg Clayton             if (log)
1266324a1036SSaleem Abdulrasool                 log->Printf ("SBThread(%p)::Suspend() => error: process is running",
1267324a1036SSaleem Abdulrasool                              static_cast<void*>(exe_ctx.GetThreadPtr()));
1268c9858e4dSGreg Clayton         }
1269c9858e4dSGreg Clayton     }
1270c9858e4dSGreg Clayton     if (log)
1271324a1036SSaleem Abdulrasool         log->Printf ("SBThread(%p)::Suspend() => %i",
1272324a1036SSaleem Abdulrasool                      static_cast<void*>(exe_ctx.GetThreadPtr()), result);
1273c9858e4dSGreg Clayton     return result;
1274722a0cdcSGreg Clayton }
1275722a0cdcSGreg Clayton 
1276722a0cdcSGreg Clayton bool
1277722a0cdcSGreg Clayton SBThread::Resume ()
1278722a0cdcSGreg Clayton {
12795160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
12807fdf9ef1SGreg Clayton     ExecutionContext exe_ctx (m_opaque_sp.get());
1281c9858e4dSGreg Clayton     bool result = false;
12821ac04c30SGreg Clayton     if (exe_ctx.HasThreadScope())
1283722a0cdcSGreg Clayton     {
1284c9858e4dSGreg Clayton         Process::StopLocker stop_locker;
1285c9858e4dSGreg Clayton         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1286c9858e4dSGreg Clayton         {
12876c9ed91cSJim Ingham             const bool override_suspend = true;
12886c9ed91cSJim Ingham             exe_ctx.GetThreadPtr()->SetResumeState (eStateRunning, override_suspend);
1289c9858e4dSGreg Clayton             result = true;
1290722a0cdcSGreg Clayton         }
1291c9858e4dSGreg Clayton         else
1292c9858e4dSGreg Clayton         {
1293c9858e4dSGreg Clayton             if (log)
1294324a1036SSaleem Abdulrasool                 log->Printf ("SBThread(%p)::Resume() => error: process is running",
1295324a1036SSaleem Abdulrasool                              static_cast<void*>(exe_ctx.GetThreadPtr()));
1296c9858e4dSGreg Clayton         }
1297c9858e4dSGreg Clayton     }
1298c9858e4dSGreg Clayton     if (log)
1299324a1036SSaleem Abdulrasool         log->Printf ("SBThread(%p)::Resume() => %i",
1300324a1036SSaleem Abdulrasool                      static_cast<void*>(exe_ctx.GetThreadPtr()), result);
1301c9858e4dSGreg Clayton     return result;
1302722a0cdcSGreg Clayton }
1303722a0cdcSGreg Clayton 
1304722a0cdcSGreg Clayton bool
1305722a0cdcSGreg Clayton SBThread::IsSuspended()
1306722a0cdcSGreg Clayton {
13077fdf9ef1SGreg Clayton     ExecutionContext exe_ctx (m_opaque_sp.get());
13081ac04c30SGreg Clayton     if (exe_ctx.HasThreadScope())
13091ac04c30SGreg Clayton         return exe_ctx.GetThreadPtr()->GetResumeState () == eStateSuspended;
1310722a0cdcSGreg Clayton     return false;
1311722a0cdcSGreg Clayton }
1312722a0cdcSGreg Clayton 
1313a75418dbSAndrew Kaylor bool
1314a75418dbSAndrew Kaylor SBThread::IsStopped()
1315a75418dbSAndrew Kaylor {
1316a75418dbSAndrew Kaylor     ExecutionContext exe_ctx (m_opaque_sp.get());
1317a75418dbSAndrew Kaylor     if (exe_ctx.HasThreadScope())
1318a75418dbSAndrew Kaylor         return StateIsStoppedState(exe_ctx.GetThreadPtr()->GetState(), true);
1319a75418dbSAndrew Kaylor     return false;
1320a75418dbSAndrew Kaylor }
1321a75418dbSAndrew Kaylor 
132230fdc8d8SChris Lattner SBProcess
132330fdc8d8SChris Lattner SBThread::GetProcess ()
132430fdc8d8SChris Lattner {
1325b9556accSGreg Clayton     SBProcess sb_process;
13267fdf9ef1SGreg Clayton     ExecutionContext exe_ctx (m_opaque_sp.get());
13271ac04c30SGreg Clayton     if (exe_ctx.HasThreadScope())
132830fdc8d8SChris Lattner     {
132930fdc8d8SChris Lattner         // Have to go up to the target so we can get a shared pointer to our process...
13301ac04c30SGreg Clayton         sb_process.SetSP (exe_ctx.GetProcessSP());
133130fdc8d8SChris Lattner     }
1332ceb6b139SCaroline Tice 
13335160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1334ceb6b139SCaroline Tice     if (log)
1335ceb6b139SCaroline Tice     {
1336481cef25SGreg Clayton         SBStream frame_desc_strm;
1337b9556accSGreg Clayton         sb_process.GetDescription (frame_desc_strm);
1338324a1036SSaleem Abdulrasool         log->Printf ("SBThread(%p)::GetProcess () => SBProcess(%p): %s",
1339324a1036SSaleem Abdulrasool                      static_cast<void*>(exe_ctx.GetThreadPtr()),
1340324a1036SSaleem Abdulrasool                      static_cast<void*>(sb_process.GetSP().get()),
1341324a1036SSaleem Abdulrasool                      frame_desc_strm.GetData());
1342ceb6b139SCaroline Tice     }
1343ceb6b139SCaroline Tice 
1344b9556accSGreg Clayton     return sb_process;
134530fdc8d8SChris Lattner }
134630fdc8d8SChris Lattner 
134730fdc8d8SChris Lattner uint32_t
134830fdc8d8SChris Lattner SBThread::GetNumFrames ()
134930fdc8d8SChris Lattner {
13505160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1351ceb6b139SCaroline Tice 
1352ceb6b139SCaroline Tice     uint32_t num_frames = 0;
1353*bb19a13cSSaleem Abdulrasool     std::unique_lock<std::recursive_mutex> lock;
1354*bb19a13cSSaleem Abdulrasool     ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
13554fc6cb9cSJim Ingham 
13561ac04c30SGreg Clayton     if (exe_ctx.HasThreadScope())
1357af67cecdSGreg Clayton     {
13587fdf9ef1SGreg Clayton         Process::StopLocker stop_locker;
13597fdf9ef1SGreg Clayton         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
13607fdf9ef1SGreg Clayton         {
13611ac04c30SGreg Clayton             num_frames = exe_ctx.GetThreadPtr()->GetStackFrameCount();
1362af67cecdSGreg Clayton         }
1363c9858e4dSGreg Clayton         else
1364c9858e4dSGreg Clayton         {
1365c9858e4dSGreg Clayton             if (log)
1366324a1036SSaleem Abdulrasool                 log->Printf ("SBThread(%p)::GetNumFrames() => error: process is running",
1367324a1036SSaleem Abdulrasool                              static_cast<void*>(exe_ctx.GetThreadPtr()));
1368c9858e4dSGreg Clayton         }
13697fdf9ef1SGreg Clayton     }
1370ceb6b139SCaroline Tice 
1371ceb6b139SCaroline Tice     if (log)
1372324a1036SSaleem Abdulrasool         log->Printf ("SBThread(%p)::GetNumFrames () => %u",
1373324a1036SSaleem Abdulrasool                      static_cast<void*>(exe_ctx.GetThreadPtr()), num_frames);
1374ceb6b139SCaroline Tice 
1375ceb6b139SCaroline Tice     return num_frames;
137630fdc8d8SChris Lattner }
137730fdc8d8SChris Lattner 
137830fdc8d8SChris Lattner SBFrame
137930fdc8d8SChris Lattner SBThread::GetFrameAtIndex (uint32_t idx)
138030fdc8d8SChris Lattner {
13815160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1382ceb6b139SCaroline Tice 
138330fdc8d8SChris Lattner     SBFrame sb_frame;
1384b57e4a1bSJason Molenda     StackFrameSP frame_sp;
1385*bb19a13cSSaleem Abdulrasool     std::unique_lock<std::recursive_mutex> lock;
1386*bb19a13cSSaleem Abdulrasool     ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
13874fc6cb9cSJim Ingham 
13881ac04c30SGreg Clayton     if (exe_ctx.HasThreadScope())
1389af67cecdSGreg Clayton     {
13907fdf9ef1SGreg Clayton         Process::StopLocker stop_locker;
13917fdf9ef1SGreg Clayton         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
13927fdf9ef1SGreg Clayton         {
13931ac04c30SGreg Clayton             frame_sp = exe_ctx.GetThreadPtr()->GetStackFrameAtIndex (idx);
1394b9556accSGreg Clayton             sb_frame.SetFrameSP (frame_sp);
1395af67cecdSGreg Clayton         }
1396c9858e4dSGreg Clayton         else
1397c9858e4dSGreg Clayton         {
1398c9858e4dSGreg Clayton             if (log)
1399324a1036SSaleem Abdulrasool                 log->Printf ("SBThread(%p)::GetFrameAtIndex() => error: process is running",
1400324a1036SSaleem Abdulrasool                              static_cast<void*>(exe_ctx.GetThreadPtr()));
1401c9858e4dSGreg Clayton         }
14027fdf9ef1SGreg Clayton     }
1403ceb6b139SCaroline Tice 
1404ceb6b139SCaroline Tice     if (log)
1405ceb6b139SCaroline Tice     {
1406481cef25SGreg Clayton         SBStream frame_desc_strm;
1407481cef25SGreg Clayton         sb_frame.GetDescription (frame_desc_strm);
14084838131bSGreg Clayton         log->Printf ("SBThread(%p)::GetFrameAtIndex (idx=%d) => SBFrame(%p): %s",
1409324a1036SSaleem Abdulrasool                      static_cast<void*>(exe_ctx.GetThreadPtr()), idx,
1410324a1036SSaleem Abdulrasool                      static_cast<void*>(frame_sp.get()),
1411324a1036SSaleem Abdulrasool                      frame_desc_strm.GetData());
1412ceb6b139SCaroline Tice     }
1413ceb6b139SCaroline Tice 
141430fdc8d8SChris Lattner     return sb_frame;
141530fdc8d8SChris Lattner }
141630fdc8d8SChris Lattner 
1417f028a1fbSGreg Clayton lldb::SBFrame
1418f028a1fbSGreg Clayton SBThread::GetSelectedFrame ()
1419f028a1fbSGreg Clayton {
14205160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1421f028a1fbSGreg Clayton 
1422f028a1fbSGreg Clayton     SBFrame sb_frame;
1423b57e4a1bSJason Molenda     StackFrameSP frame_sp;
1424*bb19a13cSSaleem Abdulrasool     std::unique_lock<std::recursive_mutex> lock;
1425*bb19a13cSSaleem Abdulrasool     ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
14264fc6cb9cSJim Ingham 
14271ac04c30SGreg Clayton     if (exe_ctx.HasThreadScope())
1428af67cecdSGreg Clayton     {
14297fdf9ef1SGreg Clayton         Process::StopLocker stop_locker;
14307fdf9ef1SGreg Clayton         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
14317fdf9ef1SGreg Clayton         {
14321ac04c30SGreg Clayton             frame_sp = exe_ctx.GetThreadPtr()->GetSelectedFrame ();
1433b9556accSGreg Clayton             sb_frame.SetFrameSP (frame_sp);
1434af67cecdSGreg Clayton         }
1435c9858e4dSGreg Clayton         else
1436c9858e4dSGreg Clayton         {
1437c9858e4dSGreg Clayton             if (log)
1438324a1036SSaleem Abdulrasool                 log->Printf ("SBThread(%p)::GetSelectedFrame() => error: process is running",
1439324a1036SSaleem Abdulrasool                              static_cast<void*>(exe_ctx.GetThreadPtr()));
1440c9858e4dSGreg Clayton         }
14417fdf9ef1SGreg Clayton     }
1442f028a1fbSGreg Clayton 
1443f028a1fbSGreg Clayton     if (log)
1444f028a1fbSGreg Clayton     {
1445481cef25SGreg Clayton         SBStream frame_desc_strm;
1446481cef25SGreg Clayton         sb_frame.GetDescription (frame_desc_strm);
1447f028a1fbSGreg Clayton         log->Printf ("SBThread(%p)::GetSelectedFrame () => SBFrame(%p): %s",
1448324a1036SSaleem Abdulrasool                      static_cast<void*>(exe_ctx.GetThreadPtr()),
1449324a1036SSaleem Abdulrasool                      static_cast<void*>(frame_sp.get()),
1450324a1036SSaleem Abdulrasool                      frame_desc_strm.GetData());
1451f028a1fbSGreg Clayton     }
1452f028a1fbSGreg Clayton 
1453f028a1fbSGreg Clayton     return sb_frame;
1454f028a1fbSGreg Clayton }
1455f028a1fbSGreg Clayton 
1456f028a1fbSGreg Clayton lldb::SBFrame
1457f028a1fbSGreg Clayton SBThread::SetSelectedFrame (uint32_t idx)
1458f028a1fbSGreg Clayton {
14595160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1460f028a1fbSGreg Clayton 
1461f028a1fbSGreg Clayton     SBFrame sb_frame;
1462b57e4a1bSJason Molenda     StackFrameSP frame_sp;
1463*bb19a13cSSaleem Abdulrasool     std::unique_lock<std::recursive_mutex> lock;
1464*bb19a13cSSaleem Abdulrasool     ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
14654fc6cb9cSJim Ingham 
14661ac04c30SGreg Clayton     if (exe_ctx.HasThreadScope())
1467f028a1fbSGreg Clayton     {
14687fdf9ef1SGreg Clayton         Process::StopLocker stop_locker;
14697fdf9ef1SGreg Clayton         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
14707fdf9ef1SGreg Clayton         {
14711ac04c30SGreg Clayton             Thread *thread = exe_ctx.GetThreadPtr();
14721ac04c30SGreg Clayton             frame_sp = thread->GetStackFrameAtIndex (idx);
1473f028a1fbSGreg Clayton             if (frame_sp)
1474f028a1fbSGreg Clayton             {
14751ac04c30SGreg Clayton                 thread->SetSelectedFrame (frame_sp.get());
1476b9556accSGreg Clayton                 sb_frame.SetFrameSP (frame_sp);
1477f028a1fbSGreg Clayton             }
1478f028a1fbSGreg Clayton         }
1479c9858e4dSGreg Clayton         else
1480c9858e4dSGreg Clayton         {
1481c9858e4dSGreg Clayton             if (log)
1482324a1036SSaleem Abdulrasool                 log->Printf ("SBThread(%p)::SetSelectedFrame() => error: process is running",
1483324a1036SSaleem Abdulrasool                              static_cast<void*>(exe_ctx.GetThreadPtr()));
1484c9858e4dSGreg Clayton         }
14857fdf9ef1SGreg Clayton     }
1486f028a1fbSGreg Clayton 
1487f028a1fbSGreg Clayton     if (log)
1488f028a1fbSGreg Clayton     {
1489481cef25SGreg Clayton         SBStream frame_desc_strm;
1490481cef25SGreg Clayton         sb_frame.GetDescription (frame_desc_strm);
1491f028a1fbSGreg Clayton         log->Printf ("SBThread(%p)::SetSelectedFrame (idx=%u) => SBFrame(%p): %s",
1492324a1036SSaleem Abdulrasool                      static_cast<void*>(exe_ctx.GetThreadPtr()), idx,
1493324a1036SSaleem Abdulrasool                      static_cast<void*>(frame_sp.get()),
1494324a1036SSaleem Abdulrasool                      frame_desc_strm.GetData());
1495f028a1fbSGreg Clayton     }
1496f028a1fbSGreg Clayton     return sb_frame;
1497f028a1fbSGreg Clayton }
1498f028a1fbSGreg Clayton 
14994f465cffSJim Ingham bool
15004f465cffSJim Ingham SBThread::EventIsThreadEvent (const SBEvent &event)
15014f465cffSJim Ingham {
15024f465cffSJim Ingham     return Thread::ThreadEventData::GetEventDataFromEvent(event.get()) != NULL;
15034f465cffSJim Ingham }
15044f465cffSJim Ingham 
15054f465cffSJim Ingham SBFrame
15064f465cffSJim Ingham SBThread::GetStackFrameFromEvent (const SBEvent &event)
15074f465cffSJim Ingham {
15084f465cffSJim Ingham     return Thread::ThreadEventData::GetStackFrameFromEvent (event.get());
15094f465cffSJim Ingham 
15104f465cffSJim Ingham }
15114f465cffSJim Ingham 
15124f465cffSJim Ingham SBThread
15134f465cffSJim Ingham SBThread::GetThreadFromEvent (const SBEvent &event)
15144f465cffSJim Ingham {
15154f465cffSJim Ingham     return Thread::ThreadEventData::GetThreadFromEvent (event.get());
15164f465cffSJim Ingham }
1517f028a1fbSGreg Clayton 
151830fdc8d8SChris Lattner bool
151930fdc8d8SChris Lattner SBThread::operator == (const SBThread &rhs) const
152030fdc8d8SChris Lattner {
15217fdf9ef1SGreg Clayton     return m_opaque_sp->GetThreadSP().get() == rhs.m_opaque_sp->GetThreadSP().get();
152230fdc8d8SChris Lattner }
152330fdc8d8SChris Lattner 
152430fdc8d8SChris Lattner bool
152530fdc8d8SChris Lattner SBThread::operator != (const SBThread &rhs) const
152630fdc8d8SChris Lattner {
15277fdf9ef1SGreg Clayton     return m_opaque_sp->GetThreadSP().get() != rhs.m_opaque_sp->GetThreadSP().get();
152830fdc8d8SChris Lattner }
1529dde9cff3SCaroline Tice 
1530dde9cff3SCaroline Tice bool
15314f465cffSJim Ingham SBThread::GetStatus (SBStream &status) const
15324f465cffSJim Ingham {
15334f465cffSJim Ingham     Stream &strm = status.ref();
15344f465cffSJim Ingham 
15354f465cffSJim Ingham     ExecutionContext exe_ctx (m_opaque_sp.get());
15364f465cffSJim Ingham     if (exe_ctx.HasThreadScope())
15374f465cffSJim Ingham     {
15384f465cffSJim Ingham         exe_ctx.GetThreadPtr()->GetStatus(strm, 0, 1, 1);
15394f465cffSJim Ingham     }
15404f465cffSJim Ingham     else
15414f465cffSJim Ingham         strm.PutCString ("No status");
15424f465cffSJim Ingham 
15434f465cffSJim Ingham     return true;
15444f465cffSJim Ingham }
15454f465cffSJim Ingham 
15464f465cffSJim Ingham bool
1547ceb6b139SCaroline Tice SBThread::GetDescription (SBStream &description) const
1548ceb6b139SCaroline Tice {
1549da7bc7d0SGreg Clayton     Stream &strm = description.ref();
1550da7bc7d0SGreg Clayton 
15517fdf9ef1SGreg Clayton     ExecutionContext exe_ctx (m_opaque_sp.get());
15521ac04c30SGreg Clayton     if (exe_ctx.HasThreadScope())
1553ceb6b139SCaroline Tice     {
1554603985fcSJim Ingham         exe_ctx.GetThreadPtr()->DumpUsingSettingsFormat(strm, LLDB_INVALID_THREAD_ID);
1555603985fcSJim Ingham         //strm.Printf("SBThread: tid = 0x%4.4" PRIx64, exe_ctx.GetThreadPtr()->GetID());
1556ceb6b139SCaroline Tice     }
1557ceb6b139SCaroline Tice     else
1558da7bc7d0SGreg Clayton         strm.PutCString ("No value");
1559ceb6b139SCaroline Tice 
1560ceb6b139SCaroline Tice     return true;
1561ceb6b139SCaroline Tice }
15625dd4916fSJason Molenda 
15635dd4916fSJason Molenda SBThread
1564008c45f1SJason Molenda SBThread::GetExtendedBacktraceThread (const char *type)
15655dd4916fSJason Molenda {
15665dd4916fSJason Molenda     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1567*bb19a13cSSaleem Abdulrasool     std::unique_lock<std::recursive_mutex> lock;
1568*bb19a13cSSaleem Abdulrasool     ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
15695dd4916fSJason Molenda     SBThread sb_origin_thread;
15705dd4916fSJason Molenda 
15715dd4916fSJason Molenda     if (exe_ctx.HasThreadScope())
15725dd4916fSJason Molenda     {
15735dd4916fSJason Molenda         Process::StopLocker stop_locker;
15745dd4916fSJason Molenda         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
15755dd4916fSJason Molenda         {
15767a2f7904SJason Molenda             ThreadSP real_thread(exe_ctx.GetThreadSP());
15775dd4916fSJason Molenda             if (real_thread)
15785dd4916fSJason Molenda             {
15795dd4916fSJason Molenda                 ConstString type_const (type);
15807a2f7904SJason Molenda                 Process *process = exe_ctx.GetProcessPtr();
15817a2f7904SJason Molenda                 if (process)
15827a2f7904SJason Molenda                 {
15837a2f7904SJason Molenda                     SystemRuntime *runtime = process->GetSystemRuntime();
15845dd4916fSJason Molenda                     if (runtime)
15855dd4916fSJason Molenda                     {
1586008c45f1SJason Molenda                         ThreadSP new_thread_sp (runtime->GetExtendedBacktraceThread (real_thread, type_const));
1587a6e9130dSJason Molenda                         if (new_thread_sp)
1588a6e9130dSJason Molenda                         {
15897a2f7904SJason Molenda                             // Save this in the Process' ExtendedThreadList so a strong pointer retains the
15907a2f7904SJason Molenda                             // object.
15917a2f7904SJason Molenda                             process->GetExtendedThreadList().AddThread (new_thread_sp);
15927a2f7904SJason Molenda                             sb_origin_thread.SetThread (new_thread_sp);
1593a6e9130dSJason Molenda                             if (log)
1594a6e9130dSJason Molenda                             {
1595a6e9130dSJason Molenda                                 const char *queue_name = new_thread_sp->GetQueueName();
1596a6e9130dSJason Molenda                                 if (queue_name == NULL)
1597a6e9130dSJason Molenda                                     queue_name = "";
15982bdbfd50SJim Ingham                                 log->Printf ("SBThread(%p)::GetExtendedBacktraceThread() => new extended Thread "
15992bdbfd50SJim Ingham                                              "created (%p) with queue_id 0x%" PRIx64 " queue name '%s'",
1600324a1036SSaleem Abdulrasool                                              static_cast<void*>(exe_ctx.GetThreadPtr()),
1601324a1036SSaleem Abdulrasool                                              static_cast<void*>(new_thread_sp.get()),
1602324a1036SSaleem Abdulrasool                                              new_thread_sp->GetQueueID(),
1603324a1036SSaleem Abdulrasool                                              queue_name);
1604a6e9130dSJason Molenda                             }
1605a6e9130dSJason Molenda                         }
16067a2f7904SJason Molenda                     }
16075dd4916fSJason Molenda                 }
16085dd4916fSJason Molenda             }
16095dd4916fSJason Molenda         }
16105dd4916fSJason Molenda         else
16115dd4916fSJason Molenda         {
16125dd4916fSJason Molenda             if (log)
1613324a1036SSaleem Abdulrasool                 log->Printf ("SBThread(%p)::GetExtendedBacktraceThread() => error: process is running",
1614324a1036SSaleem Abdulrasool                              static_cast<void*>(exe_ctx.GetThreadPtr()));
16155dd4916fSJason Molenda         }
16165dd4916fSJason Molenda     }
16175dd4916fSJason Molenda 
1618ac605f4aSJason Molenda     if (log && sb_origin_thread.IsValid() == false)
1619324a1036SSaleem Abdulrasool         log->Printf("SBThread(%p)::GetExtendedBacktraceThread() is not returning a Valid thread",
1620324a1036SSaleem Abdulrasool                     static_cast<void*>(exe_ctx.GetThreadPtr()));
16215dd4916fSJason Molenda     return sb_origin_thread;
16225dd4916fSJason Molenda }
16238ee9cb58SJason Molenda 
16248ee9cb58SJason Molenda uint32_t
16258ee9cb58SJason Molenda SBThread::GetExtendedBacktraceOriginatingIndexID ()
16268ee9cb58SJason Molenda {
16278ee9cb58SJason Molenda     ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
16288ee9cb58SJason Molenda     if (thread_sp)
16298ee9cb58SJason Molenda         return thread_sp->GetExtendedBacktraceOriginatingIndexID();
16308ee9cb58SJason Molenda     return LLDB_INVALID_INDEX32;
16318ee9cb58SJason Molenda }
1632b4892cd2SJason Molenda 
1633b4892cd2SJason Molenda bool
1634b4892cd2SJason Molenda SBThread::SafeToCallFunctions ()
1635b4892cd2SJason Molenda {
1636b4892cd2SJason Molenda     ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
1637b4892cd2SJason Molenda     if (thread_sp)
1638b4892cd2SJason Molenda         return thread_sp->SafeToCallFunctions();
1639b4892cd2SJason Molenda     return true;
1640b4892cd2SJason Molenda }
16412bdbfd50SJim Ingham 
16422bdbfd50SJim Ingham lldb_private::Thread *
16432bdbfd50SJim Ingham SBThread::operator->()
16442bdbfd50SJim Ingham {
16452bdbfd50SJim Ingham     ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
16462bdbfd50SJim Ingham     if (thread_sp)
16472bdbfd50SJim Ingham         return thread_sp.get();
16482bdbfd50SJim Ingham     else
16492bdbfd50SJim Ingham         return NULL;
16502bdbfd50SJim Ingham }
16512bdbfd50SJim Ingham 
16522bdbfd50SJim Ingham lldb_private::Thread *
16532bdbfd50SJim Ingham SBThread::get()
16542bdbfd50SJim Ingham {
16552bdbfd50SJim Ingham     ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
16562bdbfd50SJim Ingham     if (thread_sp)
16572bdbfd50SJim Ingham         return thread_sp.get();
16582bdbfd50SJim Ingham     else
16592bdbfd50SJim Ingham         return NULL;
16602bdbfd50SJim Ingham }
16612bdbfd50SJim Ingham 
1662