blob: 74df380e357103b35e3ea4f13c7279a043bc3418 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- SBThread.cpp --------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Eli Friedman7a62c8b2010-06-09 07:44:37 +000010#include "lldb/API/SBThread.h"
Chris Lattner24943d22010-06-08 16:52:24 +000011
12#include "lldb/API/SBSymbolContext.h"
13#include "lldb/API/SBFileSpec.h"
Caroline Tice98f930f2010-09-20 05:20:02 +000014#include "lldb/API/SBStream.h"
Greg Clayton640dc6b2010-11-18 18:52:36 +000015#include "lldb/Breakpoint/BreakpointLocation.h"
Greg Clayton63094e02010-06-23 01:19:29 +000016#include "lldb/Core/Debugger.h"
Chris Lattner24943d22010-06-08 16:52:24 +000017#include "lldb/Core/Stream.h"
18#include "lldb/Core/StreamFile.h"
Greg Clayton63094e02010-06-23 01:19:29 +000019#include "lldb/Interpreter/CommandInterpreter.h"
Chris Lattner24943d22010-06-08 16:52:24 +000020#include "lldb/Target/Thread.h"
21#include "lldb/Target/Process.h"
22#include "lldb/Symbol/SymbolContext.h"
23#include "lldb/Symbol/CompileUnit.h"
Greg Clayton643ee732010-08-04 01:40:35 +000024#include "lldb/Target/StopInfo.h"
Chris Lattner24943d22010-06-08 16:52:24 +000025#include "lldb/Target/Target.h"
26#include "lldb/Target/ThreadPlan.h"
Chris Lattner24943d22010-06-08 16:52:24 +000027#include "lldb/Target/ThreadPlanStepInstruction.h"
28#include "lldb/Target/ThreadPlanStepOut.h"
29#include "lldb/Target/ThreadPlanStepRange.h"
30#include "lldb/Target/ThreadPlanStepInRange.h"
31
32
Eli Friedman7a62c8b2010-06-09 07:44:37 +000033#include "lldb/API/SBAddress.h"
Eli Friedman7a62c8b2010-06-09 07:44:37 +000034#include "lldb/API/SBDebugger.h"
Jim Ingham1586d972011-12-17 01:35:57 +000035#include "lldb/API/SBFrame.h"
Eli Friedman7a62c8b2010-06-09 07:44:37 +000036#include "lldb/API/SBProcess.h"
Jim Ingham1586d972011-12-17 01:35:57 +000037#include "lldb/API/SBValue.h"
Chris Lattner24943d22010-06-08 16:52:24 +000038
39using namespace lldb;
40using namespace lldb_private;
41
Greg Clayton49ce6822010-10-31 03:01:06 +000042//----------------------------------------------------------------------
43// Constructors
44//----------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +000045SBThread::SBThread () :
Greg Claytona894fe72012-04-05 16:12:35 +000046 m_opaque_sp (new ExecutionContextRef())
Chris Lattner24943d22010-06-08 16:52:24 +000047{
48}
49
Chris Lattner24943d22010-06-08 16:52:24 +000050SBThread::SBThread (const ThreadSP& lldb_object_sp) :
Greg Claytona894fe72012-04-05 16:12:35 +000051 m_opaque_sp (new ExecutionContextRef(lldb_object_sp))
Chris Lattner24943d22010-06-08 16:52:24 +000052{
53}
54
Greg Clayton1b284412010-10-30 18:26:59 +000055SBThread::SBThread (const SBThread &rhs) :
Greg Claytona894fe72012-04-05 16:12:35 +000056 m_opaque_sp (new ExecutionContextRef(*rhs.m_opaque_sp))
Chris Lattner24943d22010-06-08 16:52:24 +000057{
Greg Claytona894fe72012-04-05 16:12:35 +000058
Chris Lattner24943d22010-06-08 16:52:24 +000059}
60
61//----------------------------------------------------------------------
Greg Clayton49ce6822010-10-31 03:01:06 +000062// Assignment operator
63//----------------------------------------------------------------------
64
65const lldb::SBThread &
66SBThread::operator = (const SBThread &rhs)
67{
68 if (this != &rhs)
Greg Claytona894fe72012-04-05 16:12:35 +000069 *m_opaque_sp = *rhs.m_opaque_sp;
Greg Clayton49ce6822010-10-31 03:01:06 +000070 return *this;
71}
72
73//----------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +000074// Destructor
75//----------------------------------------------------------------------
76SBThread::~SBThread()
77{
78}
79
80bool
81SBThread::IsValid() const
82{
Greg Claytona894fe72012-04-05 16:12:35 +000083 return m_opaque_sp->GetThreadSP().get() != NULL;
Chris Lattner24943d22010-06-08 16:52:24 +000084}
85
Greg Clayton43490d12010-07-30 20:12:55 +000086void
87SBThread::Clear ()
88{
Greg Claytona894fe72012-04-05 16:12:35 +000089 m_opaque_sp->Clear();
Greg Clayton43490d12010-07-30 20:12:55 +000090}
91
92
Chris Lattner24943d22010-06-08 16:52:24 +000093StopReason
94SBThread::GetStopReason()
95{
Greg Claytone005f2c2010-11-06 01:53:30 +000096 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +000097
Caroline Tice7826c882010-10-26 03:11:13 +000098 StopReason reason = eStopReasonInvalid;
Jim Inghamd0bdddf2012-08-22 21:34:33 +000099 Mutex::Locker api_locker;
100 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
101
Greg Claytonf4124de2012-02-21 00:09:25 +0000102 if (exe_ctx.HasThreadScope())
Chris Lattner24943d22010-06-08 16:52:24 +0000103 {
Greg Claytona894fe72012-04-05 16:12:35 +0000104 Process::StopLocker stop_locker;
105 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
106 {
Greg Claytona894fe72012-04-05 16:12:35 +0000107 StopInfoSP stop_info_sp = exe_ctx.GetThreadPtr()->GetStopInfo ();
108 if (stop_info_sp)
109 reason = stop_info_sp->GetStopReason();
110 }
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000111 else
112 {
113 if (log)
114 log->Printf ("SBThread(%p)::GetStopReason() => error: process is running", exe_ctx.GetThreadPtr());
115 }
Chris Lattner24943d22010-06-08 16:52:24 +0000116 }
Caroline Tice7826c882010-10-26 03:11:13 +0000117
118 if (log)
Greg Claytonf4124de2012-02-21 00:09:25 +0000119 log->Printf ("SBThread(%p)::GetStopReason () => %s", exe_ctx.GetThreadPtr(),
Caroline Tice61ba7ec2010-10-26 23:49:36 +0000120 Thread::StopReasonAsCString (reason));
Caroline Tice7826c882010-10-26 03:11:13 +0000121
122 return reason;
Chris Lattner24943d22010-06-08 16:52:24 +0000123}
124
125size_t
Greg Clayton640dc6b2010-11-18 18:52:36 +0000126SBThread::GetStopReasonDataCount ()
127{
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000128 Mutex::Locker api_locker;
129 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
130
Greg Claytonf4124de2012-02-21 00:09:25 +0000131 if (exe_ctx.HasThreadScope())
Greg Clayton640dc6b2010-11-18 18:52:36 +0000132 {
Greg Claytona894fe72012-04-05 16:12:35 +0000133 Process::StopLocker stop_locker;
134 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
Greg Clayton640dc6b2010-11-18 18:52:36 +0000135 {
Greg Claytona894fe72012-04-05 16:12:35 +0000136 StopInfoSP stop_info_sp = exe_ctx.GetThreadPtr()->GetStopInfo ();
137 if (stop_info_sp)
Greg Clayton640dc6b2010-11-18 18:52:36 +0000138 {
Greg Claytona894fe72012-04-05 16:12:35 +0000139 StopReason reason = stop_info_sp->GetStopReason();
140 switch (reason)
Greg Clayton640dc6b2010-11-18 18:52:36 +0000141 {
Greg Claytona894fe72012-04-05 16:12:35 +0000142 case eStopReasonInvalid:
143 case eStopReasonNone:
144 case eStopReasonTrace:
145 case eStopReasonPlanComplete:
146 // There is no data for these stop reasons.
147 return 0;
148
149 case eStopReasonBreakpoint:
150 {
151 break_id_t site_id = stop_info_sp->GetValue();
152 lldb::BreakpointSiteSP bp_site_sp (exe_ctx.GetProcessPtr()->GetBreakpointSiteList().FindByID (site_id));
153 if (bp_site_sp)
154 return bp_site_sp->GetNumberOfOwners () * 2;
155 else
156 return 0; // Breakpoint must have cleared itself...
157 }
158 break;
159
160 case eStopReasonWatchpoint:
161 return 1;
162
163 case eStopReasonSignal:
164 return 1;
165
166 case eStopReasonException:
167 return 1;
Greg Clayton640dc6b2010-11-18 18:52:36 +0000168 }
Greg Clayton640dc6b2010-11-18 18:52:36 +0000169 }
170 }
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000171 else
172 {
173 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
174 if (log)
175 log->Printf ("SBThread(%p)::GetStopReasonDataCount() => error: process is running", exe_ctx.GetThreadPtr());
176 }
Greg Clayton640dc6b2010-11-18 18:52:36 +0000177 }
178 return 0;
179}
180
181uint64_t
182SBThread::GetStopReasonDataAtIndex (uint32_t idx)
183{
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000184 Mutex::Locker api_locker;
185 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
186
Greg Claytonf4124de2012-02-21 00:09:25 +0000187 if (exe_ctx.HasThreadScope())
Greg Clayton640dc6b2010-11-18 18:52:36 +0000188 {
Greg Claytona894fe72012-04-05 16:12:35 +0000189 Process::StopLocker stop_locker;
190 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
Greg Clayton640dc6b2010-11-18 18:52:36 +0000191 {
Greg Claytona894fe72012-04-05 16:12:35 +0000192 Thread *thread = exe_ctx.GetThreadPtr();
193 StopInfoSP stop_info_sp = thread->GetStopInfo ();
194 if (stop_info_sp)
195 {
196 StopReason reason = stop_info_sp->GetStopReason();
197 switch (reason)
Greg Clayton640dc6b2010-11-18 18:52:36 +0000198 {
Greg Claytona894fe72012-04-05 16:12:35 +0000199 case eStopReasonInvalid:
200 case eStopReasonNone:
201 case eStopReasonTrace:
202 case eStopReasonPlanComplete:
203 // There is no data for these stop reasons.
204 return 0;
205
206 case eStopReasonBreakpoint:
Greg Clayton640dc6b2010-11-18 18:52:36 +0000207 {
Greg Claytona894fe72012-04-05 16:12:35 +0000208 break_id_t site_id = stop_info_sp->GetValue();
209 lldb::BreakpointSiteSP bp_site_sp (exe_ctx.GetProcessPtr()->GetBreakpointSiteList().FindByID (site_id));
210 if (bp_site_sp)
Greg Clayton640dc6b2010-11-18 18:52:36 +0000211 {
Greg Claytona894fe72012-04-05 16:12:35 +0000212 uint32_t bp_index = idx / 2;
213 BreakpointLocationSP bp_loc_sp (bp_site_sp->GetOwnerAtIndex (bp_index));
214 if (bp_loc_sp)
Greg Clayton640dc6b2010-11-18 18:52:36 +0000215 {
Greg Claytona894fe72012-04-05 16:12:35 +0000216 if (bp_index & 1)
217 {
218 // Odd idx, return the breakpoint location ID
219 return bp_loc_sp->GetID();
220 }
221 else
222 {
223 // Even idx, return the breakpoint ID
224 return bp_loc_sp->GetBreakpoint().GetID();
225 }
Greg Clayton640dc6b2010-11-18 18:52:36 +0000226 }
227 }
Greg Claytona894fe72012-04-05 16:12:35 +0000228 return LLDB_INVALID_BREAK_ID;
Greg Clayton640dc6b2010-11-18 18:52:36 +0000229 }
Greg Claytona894fe72012-04-05 16:12:35 +0000230 break;
231
232 case eStopReasonWatchpoint:
233 return stop_info_sp->GetValue();
234
235 case eStopReasonSignal:
236 return stop_info_sp->GetValue();
237
238 case eStopReasonException:
239 return stop_info_sp->GetValue();
Greg Clayton640dc6b2010-11-18 18:52:36 +0000240 }
Greg Clayton640dc6b2010-11-18 18:52:36 +0000241 }
242 }
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000243 else
244 {
245 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
246 if (log)
247 log->Printf ("SBThread(%p)::GetStopReasonDataAtIndex() => error: process is running", exe_ctx.GetThreadPtr());
248 }
Greg Clayton640dc6b2010-11-18 18:52:36 +0000249 }
250 return 0;
251}
252
253size_t
Chris Lattner24943d22010-06-08 16:52:24 +0000254SBThread::GetStopDescription (char *dst, size_t dst_len)
255{
Greg Claytone005f2c2010-11-06 01:53:30 +0000256 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000257
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000258 Mutex::Locker api_locker;
259 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
260
Greg Claytonf4124de2012-02-21 00:09:25 +0000261 if (exe_ctx.HasThreadScope())
Chris Lattner24943d22010-06-08 16:52:24 +0000262 {
Greg Claytona894fe72012-04-05 16:12:35 +0000263 Process::StopLocker stop_locker;
264 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
Chris Lattner24943d22010-06-08 16:52:24 +0000265 {
Greg Claytona894fe72012-04-05 16:12:35 +0000266
Greg Claytona894fe72012-04-05 16:12:35 +0000267 StopInfoSP stop_info_sp = exe_ctx.GetThreadPtr()->GetStopInfo ();
268 if (stop_info_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000269 {
Greg Claytona894fe72012-04-05 16:12:35 +0000270 const char *stop_desc = stop_info_sp->GetDescription();
271 if (stop_desc)
Chris Lattner24943d22010-06-08 16:52:24 +0000272 {
Caroline Tice7826c882010-10-26 03:11:13 +0000273 if (log)
Greg Claytona894fe72012-04-05 16:12:35 +0000274 log->Printf ("SBThread(%p)::GetStopDescription (dst, dst_len) => \"%s\"",
Greg Claytonf4124de2012-02-21 00:09:25 +0000275 exe_ctx.GetThreadPtr(), stop_desc);
Chris Lattner24943d22010-06-08 16:52:24 +0000276 if (dst)
Greg Claytona894fe72012-04-05 16:12:35 +0000277 return ::snprintf (dst, dst_len, "%s", stop_desc);
278 else
279 {
280 // NULL dst passed in, return the length needed to contain the description
281 return ::strlen (stop_desc) + 1; // Include the NULL byte for size
282 }
283 }
284 else
285 {
286 size_t stop_desc_len = 0;
287 switch (stop_info_sp->GetStopReason())
288 {
289 case eStopReasonTrace:
290 case eStopReasonPlanComplete:
291 {
292 static char trace_desc[] = "step";
293 stop_desc = trace_desc;
294 stop_desc_len = sizeof(trace_desc); // Include the NULL byte for size
295 }
296 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000297
Greg Claytona894fe72012-04-05 16:12:35 +0000298 case eStopReasonBreakpoint:
299 {
300 static char bp_desc[] = "breakpoint hit";
301 stop_desc = bp_desc;
302 stop_desc_len = sizeof(bp_desc); // Include the NULL byte for size
303 }
304 break;
305
306 case eStopReasonWatchpoint:
307 {
308 static char wp_desc[] = "watchpoint hit";
309 stop_desc = wp_desc;
310 stop_desc_len = sizeof(wp_desc); // Include the NULL byte for size
311 }
312 break;
313
314 case eStopReasonSignal:
315 {
316 stop_desc = exe_ctx.GetProcessPtr()->GetUnixSignals ().GetSignalAsCString (stop_info_sp->GetValue());
317 if (stop_desc == NULL || stop_desc[0] == '\0')
318 {
319 static char signal_desc[] = "signal";
320 stop_desc = signal_desc;
321 stop_desc_len = sizeof(signal_desc); // Include the NULL byte for size
322 }
323 }
324 break;
325
326 case eStopReasonException:
327 {
328 char exc_desc[] = "exception";
329 stop_desc = exc_desc;
330 stop_desc_len = sizeof(exc_desc); // Include the NULL byte for size
331 }
332 break;
333
334 default:
335 break;
336 }
337
338 if (stop_desc && stop_desc[0])
339 {
340 if (log)
341 log->Printf ("SBThread(%p)::GetStopDescription (dst, dst_len) => '%s'",
342 exe_ctx.GetThreadPtr(), stop_desc);
343
344 if (dst)
345 return ::snprintf (dst, dst_len, "%s", stop_desc) + 1; // Include the NULL byte
346
347 if (stop_desc_len == 0)
348 stop_desc_len = ::strlen (stop_desc) + 1; // Include the NULL byte
349
350 return stop_desc_len;
351 }
Chris Lattner24943d22010-06-08 16:52:24 +0000352 }
353 }
354 }
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000355 else
356 {
357 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
358 if (log)
359 log->Printf ("SBThread(%p)::GetStopDescription() => error: process is running", exe_ctx.GetThreadPtr());
360 }
Chris Lattner24943d22010-06-08 16:52:24 +0000361 }
362 if (dst)
363 *dst = 0;
364 return 0;
365}
366
Jim Ingham1586d972011-12-17 01:35:57 +0000367SBValue
368SBThread::GetStopReturnValue ()
369{
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000370 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Ingham1586d972011-12-17 01:35:57 +0000371 ValueObjectSP return_valobj_sp;
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000372 Mutex::Locker api_locker;
373 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
374
Greg Claytonf4124de2012-02-21 00:09:25 +0000375 if (exe_ctx.HasThreadScope())
Jim Ingham1586d972011-12-17 01:35:57 +0000376 {
Greg Claytona894fe72012-04-05 16:12:35 +0000377 Process::StopLocker stop_locker;
378 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
Jim Ingham1586d972011-12-17 01:35:57 +0000379 {
Greg Claytona894fe72012-04-05 16:12:35 +0000380 StopInfoSP stop_info_sp = exe_ctx.GetThreadPtr()->GetStopInfo ();
381 if (stop_info_sp)
382 {
383 return_valobj_sp = StopInfo::GetReturnValueObject (stop_info_sp);
384 }
Jim Ingham1586d972011-12-17 01:35:57 +0000385 }
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000386 else
387 {
388 if (log)
389 log->Printf ("SBThread(%p)::GetStopReturnValue() => error: process is running", exe_ctx.GetThreadPtr());
390 }
Jim Ingham1586d972011-12-17 01:35:57 +0000391 }
392
Jim Ingham1586d972011-12-17 01:35:57 +0000393 if (log)
Greg Claytonf4124de2012-02-21 00:09:25 +0000394 log->Printf ("SBThread(%p)::GetStopReturnValue () => %s", exe_ctx.GetThreadPtr(),
Jim Ingham1586d972011-12-17 01:35:57 +0000395 return_valobj_sp.get()
396 ? return_valobj_sp->GetValueAsCString()
397 : "<no return value>");
398
399 return SBValue (return_valobj_sp);
400}
401
Chris Lattner24943d22010-06-08 16:52:24 +0000402void
403SBThread::SetThread (const ThreadSP& lldb_object_sp)
404{
Greg Claytona894fe72012-04-05 16:12:35 +0000405 m_opaque_sp->SetThreadSP (lldb_object_sp);
Chris Lattner24943d22010-06-08 16:52:24 +0000406}
407
408
409lldb::tid_t
410SBThread::GetThreadID () const
411{
Greg Claytona894fe72012-04-05 16:12:35 +0000412 ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
Greg Clayton90c52142012-01-30 02:53:15 +0000413 if (thread_sp)
Greg Claytonf4124de2012-02-21 00:09:25 +0000414 return thread_sp->GetID();
415 return LLDB_INVALID_THREAD_ID;
Chris Lattner24943d22010-06-08 16:52:24 +0000416}
417
418uint32_t
419SBThread::GetIndexID () const
420{
Greg Claytona894fe72012-04-05 16:12:35 +0000421 ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
Greg Clayton90c52142012-01-30 02:53:15 +0000422 if (thread_sp)
423 return thread_sp->GetIndexID();
Chris Lattner24943d22010-06-08 16:52:24 +0000424 return LLDB_INVALID_INDEX32;
425}
Greg Claytonf4124de2012-02-21 00:09:25 +0000426
Chris Lattner24943d22010-06-08 16:52:24 +0000427const char *
428SBThread::GetName () const
429{
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000430 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytona66ba462010-10-30 04:51:46 +0000431 const char *name = NULL;
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000432 Mutex::Locker api_locker;
433 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
434
Greg Claytonf4124de2012-02-21 00:09:25 +0000435 if (exe_ctx.HasThreadScope())
Greg Claytonbdcda462010-12-20 20:49:23 +0000436 {
Greg Claytona894fe72012-04-05 16:12:35 +0000437 Process::StopLocker stop_locker;
438 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
439 {
Greg Claytona894fe72012-04-05 16:12:35 +0000440 name = exe_ctx.GetThreadPtr()->GetName();
441 }
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000442 else
443 {
444 if (log)
445 log->Printf ("SBThread(%p)::GetName() => error: process is running", exe_ctx.GetThreadPtr());
446 }
Greg Claytonbdcda462010-12-20 20:49:23 +0000447 }
Greg Claytona66ba462010-10-30 04:51:46 +0000448
Caroline Tice7826c882010-10-26 03:11:13 +0000449 if (log)
Greg Claytonf4124de2012-02-21 00:09:25 +0000450 log->Printf ("SBThread(%p)::GetName () => %s", exe_ctx.GetThreadPtr(), name ? name : "NULL");
Caroline Tice7826c882010-10-26 03:11:13 +0000451
Greg Claytona66ba462010-10-30 04:51:46 +0000452 return name;
Chris Lattner24943d22010-06-08 16:52:24 +0000453}
454
455const char *
456SBThread::GetQueueName () const
457{
Greg Claytona66ba462010-10-30 04:51:46 +0000458 const char *name = NULL;
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000459 Mutex::Locker api_locker;
460 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
461
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000462 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytonf4124de2012-02-21 00:09:25 +0000463 if (exe_ctx.HasThreadScope())
Greg Claytonbdcda462010-12-20 20:49:23 +0000464 {
Greg Claytona894fe72012-04-05 16:12:35 +0000465 Process::StopLocker stop_locker;
466 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
467 {
Greg Claytona894fe72012-04-05 16:12:35 +0000468 name = exe_ctx.GetThreadPtr()->GetQueueName();
469 }
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000470 else
471 {
472 if (log)
473 log->Printf ("SBThread(%p)::GetQueueName() => error: process is running", exe_ctx.GetThreadPtr());
474 }
Greg Claytonbdcda462010-12-20 20:49:23 +0000475 }
Greg Claytona66ba462010-10-30 04:51:46 +0000476
Caroline Tice7826c882010-10-26 03:11:13 +0000477 if (log)
Greg Claytonf4124de2012-02-21 00:09:25 +0000478 log->Printf ("SBThread(%p)::GetQueueName () => %s", exe_ctx.GetThreadPtr(), name ? name : "NULL");
Caroline Tice7826c882010-10-26 03:11:13 +0000479
Greg Claytona66ba462010-10-30 04:51:46 +0000480 return name;
Chris Lattner24943d22010-06-08 16:52:24 +0000481}
482
Jim Ingham88e3de22012-05-03 21:19:36 +0000483SBError
484SBThread::ResumeNewPlan (ExecutionContext &exe_ctx, ThreadPlan *new_plan)
485{
486 SBError sb_error;
487
488 Process *process = exe_ctx.GetProcessPtr();
489 if (!process)
490 {
491 sb_error.SetErrorString("No process in SBThread::ResumeNewPlan");
492 return sb_error;
493 }
494
495 Thread *thread = exe_ctx.GetThreadPtr();
496 if (!thread)
497 {
498 sb_error.SetErrorString("No thread in SBThread::ResumeNewPlan");
499 return sb_error;
500 }
501
502 // User level plans should be Master Plans so they can be interrupted, other plans executed, and
503 // then a "continue" will resume the plan.
504 if (new_plan != NULL)
505 {
506 new_plan->SetIsMasterPlan(true);
507 new_plan->SetOkayToDiscard(false);
508 }
509
510 // Why do we need to set the current thread by ID here???
511 process->GetThreadList().SetSelectedThreadByID (thread->GetID());
512 sb_error.ref() = process->Resume();
513
514 if (sb_error.Success())
515 {
516 // If we are doing synchronous mode, then wait for the
517 // process to stop yet again!
518 if (process->GetTarget().GetDebugger().GetAsyncExecution () == false)
519 process->WaitForProcessToStop (NULL);
520 }
521
522 return sb_error;
523}
Chris Lattner24943d22010-06-08 16:52:24 +0000524
525void
Chris Lattner24943d22010-06-08 16:52:24 +0000526SBThread::StepOver (lldb::RunMode stop_other_threads)
527{
Greg Claytone005f2c2010-11-06 01:53:30 +0000528 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000529
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000530 Mutex::Locker api_locker;
531 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
532
Caroline Tice7826c882010-10-26 03:11:13 +0000533
Greg Clayton90c52142012-01-30 02:53:15 +0000534 if (log)
Greg Claytonf4124de2012-02-21 00:09:25 +0000535 log->Printf ("SBThread(%p)::StepOver (stop_other_threads='%s')", exe_ctx.GetThreadPtr(),
Greg Clayton90c52142012-01-30 02:53:15 +0000536 Thread::RunModeAsCString (stop_other_threads));
537
Greg Claytonf4124de2012-02-21 00:09:25 +0000538 if (exe_ctx.HasThreadScope())
Chris Lattner24943d22010-06-08 16:52:24 +0000539 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000540 Thread *thread = exe_ctx.GetThreadPtr();
Jim Inghamd82bc6d2012-05-11 23:47:32 +0000541 bool abort_other_plans = false;
Greg Claytonf4124de2012-02-21 00:09:25 +0000542 StackFrameSP frame_sp(thread->GetStackFrameAtIndex (0));
Jim Ingham88e3de22012-05-03 21:19:36 +0000543 ThreadPlan *new_plan = NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000544
545 if (frame_sp)
546 {
547 if (frame_sp->HasDebugInformation ())
548 {
549 SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
Jim Ingham88e3de22012-05-03 21:19:36 +0000550 new_plan = thread->QueueThreadPlanForStepRange (abort_other_plans,
551 eStepTypeOver,
552 sc.line_entry.range,
553 sc,
554 stop_other_threads,
555 false);
Chris Lattner24943d22010-06-08 16:52:24 +0000556
557 }
558 else
559 {
Jim Ingham88e3de22012-05-03 21:19:36 +0000560 new_plan = thread->QueueThreadPlanForStepSingleInstruction (true,
561 abort_other_plans,
562 stop_other_threads);
Chris Lattner24943d22010-06-08 16:52:24 +0000563 }
564 }
565
Jim Ingham88e3de22012-05-03 21:19:36 +0000566 // This returns an error, we should use it!
567 ResumeNewPlan (exe_ctx, new_plan);
Chris Lattner24943d22010-06-08 16:52:24 +0000568 }
569}
570
571void
572SBThread::StepInto (lldb::RunMode stop_other_threads)
573{
Greg Claytone005f2c2010-11-06 01:53:30 +0000574 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000575
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000576 Mutex::Locker api_locker;
577 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
Greg Clayton90c52142012-01-30 02:53:15 +0000578
579 if (log)
Greg Claytonf4124de2012-02-21 00:09:25 +0000580 log->Printf ("SBThread(%p)::StepInto (stop_other_threads='%s')", exe_ctx.GetThreadPtr(),
Greg Clayton90c52142012-01-30 02:53:15 +0000581 Thread::RunModeAsCString (stop_other_threads));
Greg Claytonf4124de2012-02-21 00:09:25 +0000582 if (exe_ctx.HasThreadScope())
Chris Lattner24943d22010-06-08 16:52:24 +0000583 {
Jim Inghamd82bc6d2012-05-11 23:47:32 +0000584 bool abort_other_plans = false;
Chris Lattner24943d22010-06-08 16:52:24 +0000585
Greg Claytonf4124de2012-02-21 00:09:25 +0000586 Thread *thread = exe_ctx.GetThreadPtr();
587 StackFrameSP frame_sp(thread->GetStackFrameAtIndex (0));
Jim Ingham88e3de22012-05-03 21:19:36 +0000588 ThreadPlan *new_plan = NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000589
590 if (frame_sp && frame_sp->HasDebugInformation ())
591 {
Greg Clayton8f5fd6b2010-06-12 18:59:55 +0000592 bool avoid_code_without_debug_info = true;
Chris Lattner24943d22010-06-08 16:52:24 +0000593 SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
Jim Ingham88e3de22012-05-03 21:19:36 +0000594 new_plan = thread->QueueThreadPlanForStepRange (abort_other_plans,
595 eStepTypeInto,
596 sc.line_entry.range,
597 sc,
598 stop_other_threads,
599 avoid_code_without_debug_info);
Chris Lattner24943d22010-06-08 16:52:24 +0000600 }
601 else
602 {
Jim Ingham88e3de22012-05-03 21:19:36 +0000603 new_plan = thread->QueueThreadPlanForStepSingleInstruction (false,
604 abort_other_plans,
605 stop_other_threads);
Chris Lattner24943d22010-06-08 16:52:24 +0000606 }
Jim Ingham88e3de22012-05-03 21:19:36 +0000607
608 // This returns an error, we should use it!
609 ResumeNewPlan (exe_ctx, new_plan);
Chris Lattner24943d22010-06-08 16:52:24 +0000610 }
611}
612
613void
614SBThread::StepOut ()
615{
Greg Claytone005f2c2010-11-06 01:53:30 +0000616 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000617
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000618 Mutex::Locker api_locker;
619 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
620
Caroline Tice7826c882010-10-26 03:11:13 +0000621
Greg Clayton90c52142012-01-30 02:53:15 +0000622 if (log)
Greg Claytonf4124de2012-02-21 00:09:25 +0000623 log->Printf ("SBThread(%p)::StepOut ()", exe_ctx.GetThreadPtr());
Greg Clayton90c52142012-01-30 02:53:15 +0000624
Greg Claytonf4124de2012-02-21 00:09:25 +0000625 if (exe_ctx.HasThreadScope())
Chris Lattner24943d22010-06-08 16:52:24 +0000626 {
Jim Inghamd82bc6d2012-05-11 23:47:32 +0000627 bool abort_other_plans = false;
Chris Lattner24943d22010-06-08 16:52:24 +0000628 bool stop_other_threads = true;
629
Greg Claytonf4124de2012-02-21 00:09:25 +0000630 Thread *thread = exe_ctx.GetThreadPtr();
631
Jim Ingham88e3de22012-05-03 21:19:36 +0000632 ThreadPlan *new_plan = thread->QueueThreadPlanForStepOut (abort_other_plans,
633 NULL,
634 false,
635 stop_other_threads,
636 eVoteYes,
637 eVoteNoOpinion,
638 0);
639
640 // This returns an error, we should use it!
641 ResumeNewPlan (exe_ctx, new_plan);
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000642 }
643}
Chris Lattner24943d22010-06-08 16:52:24 +0000644
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000645void
646SBThread::StepOutOfFrame (lldb::SBFrame &sb_frame)
647{
648 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
649
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000650 Mutex::Locker api_locker;
651 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
652
Greg Clayton334d33a2012-01-30 07:41:31 +0000653 StackFrameSP frame_sp (sb_frame.GetFrameSP());
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000654 if (log)
655 {
656 SBStream frame_desc_strm;
657 sb_frame.GetDescription (frame_desc_strm);
Greg Claytonf4124de2012-02-21 00:09:25 +0000658 log->Printf ("SBThread(%p)::StepOutOfFrame (frame = SBFrame(%p): %s)", exe_ctx.GetThreadPtr(), frame_sp.get(), frame_desc_strm.GetData());
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000659 }
660
Greg Claytonf4124de2012-02-21 00:09:25 +0000661 if (exe_ctx.HasThreadScope())
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000662 {
Jim Inghamd82bc6d2012-05-11 23:47:32 +0000663 bool abort_other_plans = false;
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000664 bool stop_other_threads = true;
Greg Claytonf4124de2012-02-21 00:09:25 +0000665 Thread *thread = exe_ctx.GetThreadPtr();
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000666
Jim Ingham88e3de22012-05-03 21:19:36 +0000667 ThreadPlan *new_plan = thread->QueueThreadPlanForStepOut (abort_other_plans,
668 NULL,
669 false,
670 stop_other_threads,
671 eVoteYes,
672 eVoteNoOpinion,
673 frame_sp->GetFrameIndex());
674
675 // This returns an error, we should use it!
676 ResumeNewPlan (exe_ctx, new_plan);
Chris Lattner24943d22010-06-08 16:52:24 +0000677 }
678}
679
680void
681SBThread::StepInstruction (bool step_over)
682{
Greg Claytone005f2c2010-11-06 01:53:30 +0000683 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000684
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000685 Mutex::Locker api_locker;
686 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
687
Greg Claytonf4124de2012-02-21 00:09:25 +0000688
Caroline Tice7826c882010-10-26 03:11:13 +0000689
Greg Clayton90c52142012-01-30 02:53:15 +0000690 if (log)
Greg Claytonf4124de2012-02-21 00:09:25 +0000691 log->Printf ("SBThread(%p)::StepInstruction (step_over=%i)", exe_ctx.GetThreadPtr(), step_over);
Greg Clayton90c52142012-01-30 02:53:15 +0000692
Greg Claytonf4124de2012-02-21 00:09:25 +0000693 if (exe_ctx.HasThreadScope())
Chris Lattner24943d22010-06-08 16:52:24 +0000694 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000695 Thread *thread = exe_ctx.GetThreadPtr();
Jim Ingham88e3de22012-05-03 21:19:36 +0000696 ThreadPlan *new_plan = thread->QueueThreadPlanForStepSingleInstruction (step_over, true, true);
697
698 // This returns an error, we should use it!
699 ResumeNewPlan (exe_ctx, new_plan);
Chris Lattner24943d22010-06-08 16:52:24 +0000700 }
701}
702
703void
704SBThread::RunToAddress (lldb::addr_t addr)
705{
Greg Claytone005f2c2010-11-06 01:53:30 +0000706 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000707
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000708 Mutex::Locker api_locker;
709 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
710
Caroline Tice7826c882010-10-26 03:11:13 +0000711
Greg Clayton90c52142012-01-30 02:53:15 +0000712 if (log)
Greg Claytonf4124de2012-02-21 00:09:25 +0000713 log->Printf ("SBThread(%p)::RunToAddress (addr=0x%llx)", exe_ctx.GetThreadPtr(), addr);
Greg Clayton90c52142012-01-30 02:53:15 +0000714
Greg Claytonf4124de2012-02-21 00:09:25 +0000715 if (exe_ctx.HasThreadScope())
Chris Lattner24943d22010-06-08 16:52:24 +0000716 {
Jim Inghamd82bc6d2012-05-11 23:47:32 +0000717 bool abort_other_plans = false;
Chris Lattner24943d22010-06-08 16:52:24 +0000718 bool stop_other_threads = true;
719
Greg Clayton3508c382012-02-24 01:59:29 +0000720 Address target_addr (addr);
Chris Lattner24943d22010-06-08 16:52:24 +0000721
Greg Claytonf4124de2012-02-21 00:09:25 +0000722 Thread *thread = exe_ctx.GetThreadPtr();
Greg Claytonf4124de2012-02-21 00:09:25 +0000723
Jim Ingham88e3de22012-05-03 21:19:36 +0000724 ThreadPlan *new_plan = thread->QueueThreadPlanForRunToAddress (abort_other_plans, target_addr, stop_other_threads);
725
726 // This returns an error, we should use it!
727 ResumeNewPlan (exe_ctx, new_plan);
Chris Lattner24943d22010-06-08 16:52:24 +0000728 }
Chris Lattner24943d22010-06-08 16:52:24 +0000729}
730
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000731SBError
732SBThread::StepOverUntil (lldb::SBFrame &sb_frame,
733 lldb::SBFileSpec &sb_file_spec,
734 uint32_t line)
735{
736 SBError sb_error;
737 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
738 char path[PATH_MAX];
Greg Clayton90c52142012-01-30 02:53:15 +0000739
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000740 Mutex::Locker api_locker;
741 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
742
Greg Clayton334d33a2012-01-30 07:41:31 +0000743 StackFrameSP frame_sp (sb_frame.GetFrameSP());
744
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000745 if (log)
746 {
747 SBStream frame_desc_strm;
748 sb_frame.GetDescription (frame_desc_strm);
749 sb_file_spec->GetPath (path, sizeof(path));
750 log->Printf ("SBThread(%p)::StepOverUntil (frame = SBFrame(%p): %s, file+line = %s:%u)",
Greg Claytonf4124de2012-02-21 00:09:25 +0000751 exe_ctx.GetThreadPtr(),
Greg Clayton334d33a2012-01-30 07:41:31 +0000752 frame_sp.get(),
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000753 frame_desc_strm.GetData(),
754 path, line);
755 }
Greg Clayton90c52142012-01-30 02:53:15 +0000756
Greg Claytonf4124de2012-02-21 00:09:25 +0000757 if (exe_ctx.HasThreadScope())
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000758 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000759 Target *target = exe_ctx.GetTargetPtr();
Greg Claytonf4124de2012-02-21 00:09:25 +0000760 Thread *thread = exe_ctx.GetThreadPtr();
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000761
762 if (line == 0)
763 {
764 sb_error.SetErrorString("invalid line argument");
765 return sb_error;
766 }
767
768 StackFrameSP frame_sp;
Greg Clayton334d33a2012-01-30 07:41:31 +0000769 if (!frame_sp)
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000770 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000771 frame_sp = thread->GetSelectedFrame ();
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000772 if (!frame_sp)
Greg Claytonf4124de2012-02-21 00:09:25 +0000773 frame_sp = thread->GetStackFrameAtIndex (0);
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000774 }
775
776 SymbolContext frame_sc;
777 if (!frame_sp)
778 {
779 sb_error.SetErrorString("no valid frames in thread to step");
780 return sb_error;
781 }
782
783 // If we have a frame, get its line
784 frame_sc = frame_sp->GetSymbolContext (eSymbolContextCompUnit |
785 eSymbolContextFunction |
786 eSymbolContextLineEntry |
787 eSymbolContextSymbol );
788
789 if (frame_sc.comp_unit == NULL)
790 {
791 sb_error.SetErrorStringWithFormat("frame %u doesn't have debug information", frame_sp->GetFrameIndex());
792 return sb_error;
793 }
794
795 FileSpec step_file_spec;
796 if (sb_file_spec.IsValid())
797 {
798 // The file spec passed in was valid, so use it
799 step_file_spec = sb_file_spec.ref();
800 }
801 else
802 {
803 if (frame_sc.line_entry.IsValid())
804 step_file_spec = frame_sc.line_entry.file;
805 else
806 {
807 sb_error.SetErrorString("invalid file argument or no file for frame");
808 return sb_error;
809 }
810 }
811
Jim Inghamb07c62a2011-05-08 00:56:32 +0000812 // Grab the current function, then we will make sure the "until" address is
813 // within the function. We discard addresses that are out of the current
814 // function, and then if there are no addresses remaining, give an appropriate
815 // error message.
816
817 bool all_in_function = true;
818 AddressRange fun_range = frame_sc.function->GetAddressRange();
819
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000820 std::vector<addr_t> step_over_until_addrs;
Jim Inghamd82bc6d2012-05-11 23:47:32 +0000821 const bool abort_other_plans = false;
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000822 const bool stop_other_threads = true;
823 const bool check_inlines = true;
824 const bool exact = false;
825
826 SymbolContextList sc_list;
Jim Inghamb07c62a2011-05-08 00:56:32 +0000827 const uint32_t num_matches = frame_sc.comp_unit->ResolveSymbolContext (step_file_spec,
828 line,
829 check_inlines,
830 exact,
831 eSymbolContextLineEntry,
832 sc_list);
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000833 if (num_matches > 0)
834 {
835 SymbolContext sc;
836 for (uint32_t i=0; i<num_matches; ++i)
837 {
838 if (sc_list.GetContextAtIndex(i, sc))
839 {
Jim Inghamb07c62a2011-05-08 00:56:32 +0000840 addr_t step_addr = sc.line_entry.range.GetBaseAddress().GetLoadAddress(target);
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000841 if (step_addr != LLDB_INVALID_ADDRESS)
842 {
Jim Inghamb07c62a2011-05-08 00:56:32 +0000843 if (fun_range.ContainsLoadAddress(step_addr, target))
844 step_over_until_addrs.push_back(step_addr);
845 else
846 all_in_function = false;
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000847 }
848 }
849 }
850 }
Jim Inghamb07c62a2011-05-08 00:56:32 +0000851
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000852 if (step_over_until_addrs.empty())
853 {
Jim Inghamb07c62a2011-05-08 00:56:32 +0000854 if (all_in_function)
855 {
856 step_file_spec.GetPath (path, sizeof(path));
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000857 sb_error.SetErrorStringWithFormat("No line entries for %s:%u", path, line);
Jim Inghamb07c62a2011-05-08 00:56:32 +0000858 }
859 else
Greg Clayton9c236732011-10-26 00:56:27 +0000860 sb_error.SetErrorString ("step until target not in current function");
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000861 }
862 else
863 {
Jim Ingham88e3de22012-05-03 21:19:36 +0000864 ThreadPlan *new_plan = thread->QueueThreadPlanForStepUntil (abort_other_plans,
865 &step_over_until_addrs[0],
866 step_over_until_addrs.size(),
867 stop_other_threads,
868 frame_sp->GetFrameIndex());
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000869
Jim Ingham88e3de22012-05-03 21:19:36 +0000870 sb_error = ResumeNewPlan (exe_ctx, new_plan);
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000871 }
872 }
873 else
874 {
875 sb_error.SetErrorString("this SBThread object is invalid");
876 }
877 return sb_error;
878}
879
Jim Inghama17a81a2012-09-12 00:40:39 +0000880SBError
881SBThread::ReturnToFrame (SBFrame &frame, SBValue &return_value)
882{
883 SBError sb_error;
884
885 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
886
887 Mutex::Locker api_locker;
888 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
889
890
891 if (log)
892 log->Printf ("SBThread(%p)::ReturnToFrame (frame=%d)", exe_ctx.GetThreadPtr(), frame.GetFrameID());
893
894 if (exe_ctx.HasThreadScope())
895 {
896 Thread *thread = exe_ctx.GetThreadPtr();
897 sb_error.SetError (thread->ReturnToFrame(frame.GetFrameSP(), return_value.GetSP()));
898 }
899
900 return sb_error;
901}
902
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000903
Greg Clayton123db402011-01-12 02:25:42 +0000904bool
905SBThread::Suspend()
906{
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000907 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytona894fe72012-04-05 16:12:35 +0000908 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000909 bool result = false;
Greg Claytonf4124de2012-02-21 00:09:25 +0000910 if (exe_ctx.HasThreadScope())
Greg Clayton123db402011-01-12 02:25:42 +0000911 {
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000912 Process::StopLocker stop_locker;
913 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
914 {
915 exe_ctx.GetThreadPtr()->SetResumeState (eStateSuspended);
916 result = true;
917 }
918 else
919 {
920 if (log)
921 log->Printf ("SBThread(%p)::Suspend() => error: process is running", exe_ctx.GetThreadPtr());
922 }
Greg Clayton123db402011-01-12 02:25:42 +0000923 }
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000924 if (log)
925 log->Printf ("SBThread(%p)::Suspend() => %i", exe_ctx.GetThreadPtr(), result);
926 return result;
Greg Clayton123db402011-01-12 02:25:42 +0000927}
928
929bool
930SBThread::Resume ()
931{
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000932 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytona894fe72012-04-05 16:12:35 +0000933 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000934 bool result = false;
Greg Claytonf4124de2012-02-21 00:09:25 +0000935 if (exe_ctx.HasThreadScope())
Greg Clayton123db402011-01-12 02:25:42 +0000936 {
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000937 Process::StopLocker stop_locker;
938 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
939 {
940 exe_ctx.GetThreadPtr()->SetResumeState (eStateRunning);
941 result = true;
942 }
943 else
944 {
945 if (log)
946 log->Printf ("SBThread(%p)::Resume() => error: process is running", exe_ctx.GetThreadPtr());
947 }
Greg Clayton123db402011-01-12 02:25:42 +0000948 }
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000949 if (log)
950 log->Printf ("SBThread(%p)::Resume() => %i", exe_ctx.GetThreadPtr(), result);
951 return result;
Greg Clayton123db402011-01-12 02:25:42 +0000952}
953
954bool
955SBThread::IsSuspended()
956{
Greg Claytona894fe72012-04-05 16:12:35 +0000957 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Claytonf4124de2012-02-21 00:09:25 +0000958 if (exe_ctx.HasThreadScope())
959 return exe_ctx.GetThreadPtr()->GetResumeState () == eStateSuspended;
Greg Clayton123db402011-01-12 02:25:42 +0000960 return false;
961}
962
Chris Lattner24943d22010-06-08 16:52:24 +0000963SBProcess
964SBThread::GetProcess ()
965{
Caroline Tice7826c882010-10-26 03:11:13 +0000966
Greg Clayton334d33a2012-01-30 07:41:31 +0000967 SBProcess sb_process;
968 ProcessSP process_sp;
Greg Claytona894fe72012-04-05 16:12:35 +0000969 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Claytonf4124de2012-02-21 00:09:25 +0000970 if (exe_ctx.HasThreadScope())
Chris Lattner24943d22010-06-08 16:52:24 +0000971 {
972 // Have to go up to the target so we can get a shared pointer to our process...
Greg Claytonf4124de2012-02-21 00:09:25 +0000973 sb_process.SetSP (exe_ctx.GetProcessSP());
Chris Lattner24943d22010-06-08 16:52:24 +0000974 }
Caroline Tice7826c882010-10-26 03:11:13 +0000975
Greg Claytone005f2c2010-11-06 01:53:30 +0000976 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000977 if (log)
978 {
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000979 SBStream frame_desc_strm;
Greg Clayton334d33a2012-01-30 07:41:31 +0000980 sb_process.GetDescription (frame_desc_strm);
Greg Claytonf4124de2012-02-21 00:09:25 +0000981 log->Printf ("SBThread(%p)::GetProcess () => SBProcess(%p): %s", exe_ctx.GetThreadPtr(),
Greg Clayton334d33a2012-01-30 07:41:31 +0000982 process_sp.get(), frame_desc_strm.GetData());
Caroline Tice7826c882010-10-26 03:11:13 +0000983 }
984
Greg Clayton334d33a2012-01-30 07:41:31 +0000985 return sb_process;
Chris Lattner24943d22010-06-08 16:52:24 +0000986}
987
988uint32_t
989SBThread::GetNumFrames ()
990{
Greg Claytone005f2c2010-11-06 01:53:30 +0000991 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000992
Caroline Tice7826c882010-10-26 03:11:13 +0000993 uint32_t num_frames = 0;
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000994 Mutex::Locker api_locker;
995 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
996
Greg Claytonf4124de2012-02-21 00:09:25 +0000997 if (exe_ctx.HasThreadScope())
Greg Claytonbdcda462010-12-20 20:49:23 +0000998 {
Greg Claytona894fe72012-04-05 16:12:35 +0000999 Process::StopLocker stop_locker;
1000 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1001 {
Greg Claytona894fe72012-04-05 16:12:35 +00001002 num_frames = exe_ctx.GetThreadPtr()->GetStackFrameCount();
1003 }
Greg Clayton9f3c98e2012-04-06 02:17:47 +00001004 else
1005 {
1006 if (log)
1007 log->Printf ("SBThread(%p)::GetNumFrames() => error: process is running", exe_ctx.GetThreadPtr());
1008 }
Greg Claytonbdcda462010-12-20 20:49:23 +00001009 }
Caroline Tice7826c882010-10-26 03:11:13 +00001010
1011 if (log)
Greg Claytonf4124de2012-02-21 00:09:25 +00001012 log->Printf ("SBThread(%p)::GetNumFrames () => %u", exe_ctx.GetThreadPtr(), num_frames);
Caroline Tice7826c882010-10-26 03:11:13 +00001013
1014 return num_frames;
Chris Lattner24943d22010-06-08 16:52:24 +00001015}
1016
1017SBFrame
1018SBThread::GetFrameAtIndex (uint32_t idx)
1019{
Greg Claytone005f2c2010-11-06 01:53:30 +00001020 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +00001021
Chris Lattner24943d22010-06-08 16:52:24 +00001022 SBFrame sb_frame;
Greg Clayton334d33a2012-01-30 07:41:31 +00001023 StackFrameSP frame_sp;
Jim Inghamd0bdddf2012-08-22 21:34:33 +00001024 Mutex::Locker api_locker;
1025 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1026
Greg Claytonf4124de2012-02-21 00:09:25 +00001027 if (exe_ctx.HasThreadScope())
Greg Claytonbdcda462010-12-20 20:49:23 +00001028 {
Greg Claytona894fe72012-04-05 16:12:35 +00001029 Process::StopLocker stop_locker;
1030 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1031 {
Greg Claytona894fe72012-04-05 16:12:35 +00001032 frame_sp = exe_ctx.GetThreadPtr()->GetStackFrameAtIndex (idx);
1033 sb_frame.SetFrameSP (frame_sp);
1034 }
Greg Clayton9f3c98e2012-04-06 02:17:47 +00001035 else
1036 {
1037 if (log)
1038 log->Printf ("SBThread(%p)::GetFrameAtIndex() => error: process is running", exe_ctx.GetThreadPtr());
1039 }
Greg Claytonbdcda462010-12-20 20:49:23 +00001040 }
Caroline Tice7826c882010-10-26 03:11:13 +00001041
1042 if (log)
1043 {
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001044 SBStream frame_desc_strm;
1045 sb_frame.GetDescription (frame_desc_strm);
Greg Claytona66ba462010-10-30 04:51:46 +00001046 log->Printf ("SBThread(%p)::GetFrameAtIndex (idx=%d) => SBFrame(%p): %s",
Greg Claytonf4124de2012-02-21 00:09:25 +00001047 exe_ctx.GetThreadPtr(), idx, frame_sp.get(), frame_desc_strm.GetData());
Caroline Tice7826c882010-10-26 03:11:13 +00001048 }
1049
Chris Lattner24943d22010-06-08 16:52:24 +00001050 return sb_frame;
1051}
1052
Greg Claytonc5157ec2010-12-17 02:26:24 +00001053lldb::SBFrame
1054SBThread::GetSelectedFrame ()
1055{
1056 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1057
1058 SBFrame sb_frame;
Greg Clayton334d33a2012-01-30 07:41:31 +00001059 StackFrameSP frame_sp;
Jim Inghamd0bdddf2012-08-22 21:34:33 +00001060 Mutex::Locker api_locker;
1061 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1062
Greg Claytonf4124de2012-02-21 00:09:25 +00001063 if (exe_ctx.HasThreadScope())
Greg Claytonbdcda462010-12-20 20:49:23 +00001064 {
Greg Claytona894fe72012-04-05 16:12:35 +00001065 Process::StopLocker stop_locker;
1066 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1067 {
Greg Claytona894fe72012-04-05 16:12:35 +00001068 frame_sp = exe_ctx.GetThreadPtr()->GetSelectedFrame ();
1069 sb_frame.SetFrameSP (frame_sp);
1070 }
Greg Clayton9f3c98e2012-04-06 02:17:47 +00001071 else
1072 {
1073 if (log)
1074 log->Printf ("SBThread(%p)::GetSelectedFrame() => error: process is running", exe_ctx.GetThreadPtr());
1075 }
Greg Claytonbdcda462010-12-20 20:49:23 +00001076 }
Greg Claytonc5157ec2010-12-17 02:26:24 +00001077
1078 if (log)
1079 {
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001080 SBStream frame_desc_strm;
1081 sb_frame.GetDescription (frame_desc_strm);
Greg Claytonc5157ec2010-12-17 02:26:24 +00001082 log->Printf ("SBThread(%p)::GetSelectedFrame () => SBFrame(%p): %s",
Greg Claytonf4124de2012-02-21 00:09:25 +00001083 exe_ctx.GetThreadPtr(), frame_sp.get(), frame_desc_strm.GetData());
Greg Claytonc5157ec2010-12-17 02:26:24 +00001084 }
1085
1086 return sb_frame;
1087}
1088
1089lldb::SBFrame
1090SBThread::SetSelectedFrame (uint32_t idx)
1091{
1092 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1093
1094 SBFrame sb_frame;
Greg Clayton334d33a2012-01-30 07:41:31 +00001095 StackFrameSP frame_sp;
Jim Inghamd0bdddf2012-08-22 21:34:33 +00001096 Mutex::Locker api_locker;
1097 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1098
Greg Claytonf4124de2012-02-21 00:09:25 +00001099 if (exe_ctx.HasThreadScope())
Greg Claytonc5157ec2010-12-17 02:26:24 +00001100 {
Greg Claytona894fe72012-04-05 16:12:35 +00001101 Process::StopLocker stop_locker;
1102 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
Greg Claytonc5157ec2010-12-17 02:26:24 +00001103 {
Greg Claytona894fe72012-04-05 16:12:35 +00001104 Thread *thread = exe_ctx.GetThreadPtr();
1105 frame_sp = thread->GetStackFrameAtIndex (idx);
1106 if (frame_sp)
1107 {
1108 thread->SetSelectedFrame (frame_sp.get());
1109 sb_frame.SetFrameSP (frame_sp);
1110 }
Greg Claytonc5157ec2010-12-17 02:26:24 +00001111 }
Greg Clayton9f3c98e2012-04-06 02:17:47 +00001112 else
1113 {
1114 if (log)
1115 log->Printf ("SBThread(%p)::SetSelectedFrame() => error: process is running", exe_ctx.GetThreadPtr());
1116 }
Greg Claytonc5157ec2010-12-17 02:26:24 +00001117 }
1118
1119 if (log)
1120 {
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001121 SBStream frame_desc_strm;
1122 sb_frame.GetDescription (frame_desc_strm);
Greg Claytonc5157ec2010-12-17 02:26:24 +00001123 log->Printf ("SBThread(%p)::SetSelectedFrame (idx=%u) => SBFrame(%p): %s",
Greg Claytonf4124de2012-02-21 00:09:25 +00001124 exe_ctx.GetThreadPtr(), idx, frame_sp.get(), frame_desc_strm.GetData());
Greg Claytonc5157ec2010-12-17 02:26:24 +00001125 }
1126 return sb_frame;
1127}
1128
1129
Chris Lattner24943d22010-06-08 16:52:24 +00001130bool
1131SBThread::operator == (const SBThread &rhs) const
1132{
Greg Claytona894fe72012-04-05 16:12:35 +00001133 return m_opaque_sp->GetThreadSP().get() == rhs.m_opaque_sp->GetThreadSP().get();
Chris Lattner24943d22010-06-08 16:52:24 +00001134}
1135
1136bool
1137SBThread::operator != (const SBThread &rhs) const
1138{
Greg Claytona894fe72012-04-05 16:12:35 +00001139 return m_opaque_sp->GetThreadSP().get() != rhs.m_opaque_sp->GetThreadSP().get();
Chris Lattner24943d22010-06-08 16:52:24 +00001140}
Caroline Tice98f930f2010-09-20 05:20:02 +00001141
1142bool
Caroline Tice7826c882010-10-26 03:11:13 +00001143SBThread::GetDescription (SBStream &description) const
1144{
Greg Clayton96154be2011-11-13 06:57:31 +00001145 Stream &strm = description.ref();
1146
Greg Claytona894fe72012-04-05 16:12:35 +00001147 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Claytonf4124de2012-02-21 00:09:25 +00001148 if (exe_ctx.HasThreadScope())
Caroline Tice7826c882010-10-26 03:11:13 +00001149 {
Greg Claytonf4124de2012-02-21 00:09:25 +00001150 strm.Printf("SBThread: tid = 0x%4.4llx", exe_ctx.GetThreadPtr()->GetID());
Caroline Tice7826c882010-10-26 03:11:13 +00001151 }
1152 else
Greg Clayton96154be2011-11-13 06:57:31 +00001153 strm.PutCString ("No value");
Caroline Tice7826c882010-10-26 03:11:13 +00001154
1155 return true;
1156}