blob: 345cd7bbd1ea94524e3daf1ca98c4ba4dac8314f [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;
Jim Inghambe0cde92012-09-14 21:07:14 +0000628 bool stop_other_threads = false;
Chris Lattner24943d22010-06-08 16:52:24 +0000629
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;
Jim Inghambe0cde92012-09-14 21:07:14 +0000664 bool stop_other_threads = false;
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
Greg Clayton334d33a2012-01-30 07:41:31 +0000768 if (!frame_sp)
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000769 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000770 frame_sp = thread->GetSelectedFrame ();
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000771 if (!frame_sp)
Greg Claytonf4124de2012-02-21 00:09:25 +0000772 frame_sp = thread->GetStackFrameAtIndex (0);
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000773 }
774
775 SymbolContext frame_sc;
776 if (!frame_sp)
777 {
778 sb_error.SetErrorString("no valid frames in thread to step");
779 return sb_error;
780 }
781
782 // If we have a frame, get its line
783 frame_sc = frame_sp->GetSymbolContext (eSymbolContextCompUnit |
784 eSymbolContextFunction |
785 eSymbolContextLineEntry |
786 eSymbolContextSymbol );
787
788 if (frame_sc.comp_unit == NULL)
789 {
790 sb_error.SetErrorStringWithFormat("frame %u doesn't have debug information", frame_sp->GetFrameIndex());
791 return sb_error;
792 }
793
794 FileSpec step_file_spec;
795 if (sb_file_spec.IsValid())
796 {
797 // The file spec passed in was valid, so use it
798 step_file_spec = sb_file_spec.ref();
799 }
800 else
801 {
802 if (frame_sc.line_entry.IsValid())
803 step_file_spec = frame_sc.line_entry.file;
804 else
805 {
806 sb_error.SetErrorString("invalid file argument or no file for frame");
807 return sb_error;
808 }
809 }
810
Jim Inghamb07c62a2011-05-08 00:56:32 +0000811 // Grab the current function, then we will make sure the "until" address is
812 // within the function. We discard addresses that are out of the current
813 // function, and then if there are no addresses remaining, give an appropriate
814 // error message.
815
816 bool all_in_function = true;
817 AddressRange fun_range = frame_sc.function->GetAddressRange();
818
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000819 std::vector<addr_t> step_over_until_addrs;
Jim Inghamd82bc6d2012-05-11 23:47:32 +0000820 const bool abort_other_plans = false;
Jim Ingham52124e72012-09-14 18:57:14 +0000821 const bool stop_other_threads = false;
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000822 const bool check_inlines = true;
823 const bool exact = false;
824
825 SymbolContextList sc_list;
Jim Inghamb07c62a2011-05-08 00:56:32 +0000826 const uint32_t num_matches = frame_sc.comp_unit->ResolveSymbolContext (step_file_spec,
827 line,
828 check_inlines,
829 exact,
830 eSymbolContextLineEntry,
831 sc_list);
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000832 if (num_matches > 0)
833 {
834 SymbolContext sc;
835 for (uint32_t i=0; i<num_matches; ++i)
836 {
837 if (sc_list.GetContextAtIndex(i, sc))
838 {
Jim Inghamb07c62a2011-05-08 00:56:32 +0000839 addr_t step_addr = sc.line_entry.range.GetBaseAddress().GetLoadAddress(target);
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000840 if (step_addr != LLDB_INVALID_ADDRESS)
841 {
Jim Inghamb07c62a2011-05-08 00:56:32 +0000842 if (fun_range.ContainsLoadAddress(step_addr, target))
843 step_over_until_addrs.push_back(step_addr);
844 else
845 all_in_function = false;
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000846 }
847 }
848 }
849 }
Jim Inghamb07c62a2011-05-08 00:56:32 +0000850
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000851 if (step_over_until_addrs.empty())
852 {
Jim Inghamb07c62a2011-05-08 00:56:32 +0000853 if (all_in_function)
854 {
855 step_file_spec.GetPath (path, sizeof(path));
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000856 sb_error.SetErrorStringWithFormat("No line entries for %s:%u", path, line);
Jim Inghamb07c62a2011-05-08 00:56:32 +0000857 }
858 else
Greg Clayton9c236732011-10-26 00:56:27 +0000859 sb_error.SetErrorString ("step until target not in current function");
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000860 }
861 else
862 {
Jim Ingham88e3de22012-05-03 21:19:36 +0000863 ThreadPlan *new_plan = thread->QueueThreadPlanForStepUntil (abort_other_plans,
864 &step_over_until_addrs[0],
865 step_over_until_addrs.size(),
866 stop_other_threads,
867 frame_sp->GetFrameIndex());
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000868
Jim Ingham88e3de22012-05-03 21:19:36 +0000869 sb_error = ResumeNewPlan (exe_ctx, new_plan);
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000870 }
871 }
872 else
873 {
874 sb_error.SetErrorString("this SBThread object is invalid");
875 }
876 return sb_error;
877}
878
Jim Inghama17a81a2012-09-12 00:40:39 +0000879SBError
Jim Inghamf59388a2012-09-14 02:14:15 +0000880SBThread::ReturnFromFrame (SBFrame &frame, SBValue &return_value)
Jim Inghama17a81a2012-09-12 00:40:39 +0000881{
882 SBError sb_error;
883
884 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
885
886 Mutex::Locker api_locker;
887 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
888
889
890 if (log)
Jim Inghamf59388a2012-09-14 02:14:15 +0000891 log->Printf ("SBThread(%p)::ReturnFromFrame (frame=%d)", exe_ctx.GetThreadPtr(), frame.GetFrameID());
Jim Inghama17a81a2012-09-12 00:40:39 +0000892
893 if (exe_ctx.HasThreadScope())
894 {
895 Thread *thread = exe_ctx.GetThreadPtr();
Jim Inghamf59388a2012-09-14 02:14:15 +0000896 sb_error.SetError (thread->ReturnFromFrame(frame.GetFrameSP(), return_value.GetSP()));
Jim Inghama17a81a2012-09-12 00:40:39 +0000897 }
898
899 return sb_error;
900}
901
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000902
Greg Clayton123db402011-01-12 02:25:42 +0000903bool
904SBThread::Suspend()
905{
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000906 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytona894fe72012-04-05 16:12:35 +0000907 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000908 bool result = false;
Greg Claytonf4124de2012-02-21 00:09:25 +0000909 if (exe_ctx.HasThreadScope())
Greg Clayton123db402011-01-12 02:25:42 +0000910 {
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000911 Process::StopLocker stop_locker;
912 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
913 {
914 exe_ctx.GetThreadPtr()->SetResumeState (eStateSuspended);
915 result = true;
916 }
917 else
918 {
919 if (log)
920 log->Printf ("SBThread(%p)::Suspend() => error: process is running", exe_ctx.GetThreadPtr());
921 }
Greg Clayton123db402011-01-12 02:25:42 +0000922 }
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000923 if (log)
924 log->Printf ("SBThread(%p)::Suspend() => %i", exe_ctx.GetThreadPtr(), result);
925 return result;
Greg Clayton123db402011-01-12 02:25:42 +0000926}
927
928bool
929SBThread::Resume ()
930{
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000931 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytona894fe72012-04-05 16:12:35 +0000932 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000933 bool result = false;
Greg Claytonf4124de2012-02-21 00:09:25 +0000934 if (exe_ctx.HasThreadScope())
Greg Clayton123db402011-01-12 02:25:42 +0000935 {
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000936 Process::StopLocker stop_locker;
937 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
938 {
939 exe_ctx.GetThreadPtr()->SetResumeState (eStateRunning);
940 result = true;
941 }
942 else
943 {
944 if (log)
945 log->Printf ("SBThread(%p)::Resume() => error: process is running", exe_ctx.GetThreadPtr());
946 }
Greg Clayton123db402011-01-12 02:25:42 +0000947 }
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000948 if (log)
949 log->Printf ("SBThread(%p)::Resume() => %i", exe_ctx.GetThreadPtr(), result);
950 return result;
Greg Clayton123db402011-01-12 02:25:42 +0000951}
952
953bool
954SBThread::IsSuspended()
955{
Greg Claytona894fe72012-04-05 16:12:35 +0000956 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Claytonf4124de2012-02-21 00:09:25 +0000957 if (exe_ctx.HasThreadScope())
958 return exe_ctx.GetThreadPtr()->GetResumeState () == eStateSuspended;
Greg Clayton123db402011-01-12 02:25:42 +0000959 return false;
960}
961
Chris Lattner24943d22010-06-08 16:52:24 +0000962SBProcess
963SBThread::GetProcess ()
964{
Caroline Tice7826c882010-10-26 03:11:13 +0000965
Greg Clayton334d33a2012-01-30 07:41:31 +0000966 SBProcess sb_process;
967 ProcessSP process_sp;
Greg Claytona894fe72012-04-05 16:12:35 +0000968 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Claytonf4124de2012-02-21 00:09:25 +0000969 if (exe_ctx.HasThreadScope())
Chris Lattner24943d22010-06-08 16:52:24 +0000970 {
971 // Have to go up to the target so we can get a shared pointer to our process...
Greg Claytonf4124de2012-02-21 00:09:25 +0000972 sb_process.SetSP (exe_ctx.GetProcessSP());
Chris Lattner24943d22010-06-08 16:52:24 +0000973 }
Caroline Tice7826c882010-10-26 03:11:13 +0000974
Greg Claytone005f2c2010-11-06 01:53:30 +0000975 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000976 if (log)
977 {
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000978 SBStream frame_desc_strm;
Greg Clayton334d33a2012-01-30 07:41:31 +0000979 sb_process.GetDescription (frame_desc_strm);
Greg Claytonf4124de2012-02-21 00:09:25 +0000980 log->Printf ("SBThread(%p)::GetProcess () => SBProcess(%p): %s", exe_ctx.GetThreadPtr(),
Greg Clayton334d33a2012-01-30 07:41:31 +0000981 process_sp.get(), frame_desc_strm.GetData());
Caroline Tice7826c882010-10-26 03:11:13 +0000982 }
983
Greg Clayton334d33a2012-01-30 07:41:31 +0000984 return sb_process;
Chris Lattner24943d22010-06-08 16:52:24 +0000985}
986
987uint32_t
988SBThread::GetNumFrames ()
989{
Greg Claytone005f2c2010-11-06 01:53:30 +0000990 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000991
Caroline Tice7826c882010-10-26 03:11:13 +0000992 uint32_t num_frames = 0;
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000993 Mutex::Locker api_locker;
994 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
995
Greg Claytonf4124de2012-02-21 00:09:25 +0000996 if (exe_ctx.HasThreadScope())
Greg Claytonbdcda462010-12-20 20:49:23 +0000997 {
Greg Claytona894fe72012-04-05 16:12:35 +0000998 Process::StopLocker stop_locker;
999 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1000 {
Greg Claytona894fe72012-04-05 16:12:35 +00001001 num_frames = exe_ctx.GetThreadPtr()->GetStackFrameCount();
1002 }
Greg Clayton9f3c98e2012-04-06 02:17:47 +00001003 else
1004 {
1005 if (log)
1006 log->Printf ("SBThread(%p)::GetNumFrames() => error: process is running", exe_ctx.GetThreadPtr());
1007 }
Greg Claytonbdcda462010-12-20 20:49:23 +00001008 }
Caroline Tice7826c882010-10-26 03:11:13 +00001009
1010 if (log)
Greg Claytonf4124de2012-02-21 00:09:25 +00001011 log->Printf ("SBThread(%p)::GetNumFrames () => %u", exe_ctx.GetThreadPtr(), num_frames);
Caroline Tice7826c882010-10-26 03:11:13 +00001012
1013 return num_frames;
Chris Lattner24943d22010-06-08 16:52:24 +00001014}
1015
1016SBFrame
1017SBThread::GetFrameAtIndex (uint32_t idx)
1018{
Greg Claytone005f2c2010-11-06 01:53:30 +00001019 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +00001020
Chris Lattner24943d22010-06-08 16:52:24 +00001021 SBFrame sb_frame;
Greg Clayton334d33a2012-01-30 07:41:31 +00001022 StackFrameSP frame_sp;
Jim Inghamd0bdddf2012-08-22 21:34:33 +00001023 Mutex::Locker api_locker;
1024 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1025
Greg Claytonf4124de2012-02-21 00:09:25 +00001026 if (exe_ctx.HasThreadScope())
Greg Claytonbdcda462010-12-20 20:49:23 +00001027 {
Greg Claytona894fe72012-04-05 16:12:35 +00001028 Process::StopLocker stop_locker;
1029 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1030 {
Greg Claytona894fe72012-04-05 16:12:35 +00001031 frame_sp = exe_ctx.GetThreadPtr()->GetStackFrameAtIndex (idx);
1032 sb_frame.SetFrameSP (frame_sp);
1033 }
Greg Clayton9f3c98e2012-04-06 02:17:47 +00001034 else
1035 {
1036 if (log)
1037 log->Printf ("SBThread(%p)::GetFrameAtIndex() => error: process is running", exe_ctx.GetThreadPtr());
1038 }
Greg Claytonbdcda462010-12-20 20:49:23 +00001039 }
Caroline Tice7826c882010-10-26 03:11:13 +00001040
1041 if (log)
1042 {
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001043 SBStream frame_desc_strm;
1044 sb_frame.GetDescription (frame_desc_strm);
Greg Claytona66ba462010-10-30 04:51:46 +00001045 log->Printf ("SBThread(%p)::GetFrameAtIndex (idx=%d) => SBFrame(%p): %s",
Greg Claytonf4124de2012-02-21 00:09:25 +00001046 exe_ctx.GetThreadPtr(), idx, frame_sp.get(), frame_desc_strm.GetData());
Caroline Tice7826c882010-10-26 03:11:13 +00001047 }
1048
Chris Lattner24943d22010-06-08 16:52:24 +00001049 return sb_frame;
1050}
1051
Greg Claytonc5157ec2010-12-17 02:26:24 +00001052lldb::SBFrame
1053SBThread::GetSelectedFrame ()
1054{
1055 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1056
1057 SBFrame sb_frame;
Greg Clayton334d33a2012-01-30 07:41:31 +00001058 StackFrameSP frame_sp;
Jim Inghamd0bdddf2012-08-22 21:34:33 +00001059 Mutex::Locker api_locker;
1060 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1061
Greg Claytonf4124de2012-02-21 00:09:25 +00001062 if (exe_ctx.HasThreadScope())
Greg Claytonbdcda462010-12-20 20:49:23 +00001063 {
Greg Claytona894fe72012-04-05 16:12:35 +00001064 Process::StopLocker stop_locker;
1065 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1066 {
Greg Claytona894fe72012-04-05 16:12:35 +00001067 frame_sp = exe_ctx.GetThreadPtr()->GetSelectedFrame ();
1068 sb_frame.SetFrameSP (frame_sp);
1069 }
Greg Clayton9f3c98e2012-04-06 02:17:47 +00001070 else
1071 {
1072 if (log)
1073 log->Printf ("SBThread(%p)::GetSelectedFrame() => error: process is running", exe_ctx.GetThreadPtr());
1074 }
Greg Claytonbdcda462010-12-20 20:49:23 +00001075 }
Greg Claytonc5157ec2010-12-17 02:26:24 +00001076
1077 if (log)
1078 {
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001079 SBStream frame_desc_strm;
1080 sb_frame.GetDescription (frame_desc_strm);
Greg Claytonc5157ec2010-12-17 02:26:24 +00001081 log->Printf ("SBThread(%p)::GetSelectedFrame () => SBFrame(%p): %s",
Greg Claytonf4124de2012-02-21 00:09:25 +00001082 exe_ctx.GetThreadPtr(), frame_sp.get(), frame_desc_strm.GetData());
Greg Claytonc5157ec2010-12-17 02:26:24 +00001083 }
1084
1085 return sb_frame;
1086}
1087
1088lldb::SBFrame
1089SBThread::SetSelectedFrame (uint32_t idx)
1090{
1091 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1092
1093 SBFrame sb_frame;
Greg Clayton334d33a2012-01-30 07:41:31 +00001094 StackFrameSP frame_sp;
Jim Inghamd0bdddf2012-08-22 21:34:33 +00001095 Mutex::Locker api_locker;
1096 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1097
Greg Claytonf4124de2012-02-21 00:09:25 +00001098 if (exe_ctx.HasThreadScope())
Greg Claytonc5157ec2010-12-17 02:26:24 +00001099 {
Greg Claytona894fe72012-04-05 16:12:35 +00001100 Process::StopLocker stop_locker;
1101 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
Greg Claytonc5157ec2010-12-17 02:26:24 +00001102 {
Greg Claytona894fe72012-04-05 16:12:35 +00001103 Thread *thread = exe_ctx.GetThreadPtr();
1104 frame_sp = thread->GetStackFrameAtIndex (idx);
1105 if (frame_sp)
1106 {
1107 thread->SetSelectedFrame (frame_sp.get());
1108 sb_frame.SetFrameSP (frame_sp);
1109 }
Greg Claytonc5157ec2010-12-17 02:26:24 +00001110 }
Greg Clayton9f3c98e2012-04-06 02:17:47 +00001111 else
1112 {
1113 if (log)
1114 log->Printf ("SBThread(%p)::SetSelectedFrame() => error: process is running", exe_ctx.GetThreadPtr());
1115 }
Greg Claytonc5157ec2010-12-17 02:26:24 +00001116 }
1117
1118 if (log)
1119 {
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001120 SBStream frame_desc_strm;
1121 sb_frame.GetDescription (frame_desc_strm);
Greg Claytonc5157ec2010-12-17 02:26:24 +00001122 log->Printf ("SBThread(%p)::SetSelectedFrame (idx=%u) => SBFrame(%p): %s",
Greg Claytonf4124de2012-02-21 00:09:25 +00001123 exe_ctx.GetThreadPtr(), idx, frame_sp.get(), frame_desc_strm.GetData());
Greg Claytonc5157ec2010-12-17 02:26:24 +00001124 }
1125 return sb_frame;
1126}
1127
1128
Chris Lattner24943d22010-06-08 16:52:24 +00001129bool
1130SBThread::operator == (const SBThread &rhs) const
1131{
Greg Claytona894fe72012-04-05 16:12:35 +00001132 return m_opaque_sp->GetThreadSP().get() == rhs.m_opaque_sp->GetThreadSP().get();
Chris Lattner24943d22010-06-08 16:52:24 +00001133}
1134
1135bool
1136SBThread::operator != (const SBThread &rhs) const
1137{
Greg Claytona894fe72012-04-05 16:12:35 +00001138 return m_opaque_sp->GetThreadSP().get() != rhs.m_opaque_sp->GetThreadSP().get();
Chris Lattner24943d22010-06-08 16:52:24 +00001139}
Caroline Tice98f930f2010-09-20 05:20:02 +00001140
1141bool
Caroline Tice7826c882010-10-26 03:11:13 +00001142SBThread::GetDescription (SBStream &description) const
1143{
Greg Clayton96154be2011-11-13 06:57:31 +00001144 Stream &strm = description.ref();
1145
Greg Claytona894fe72012-04-05 16:12:35 +00001146 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Claytonf4124de2012-02-21 00:09:25 +00001147 if (exe_ctx.HasThreadScope())
Caroline Tice7826c882010-10-26 03:11:13 +00001148 {
Greg Claytonf4124de2012-02-21 00:09:25 +00001149 strm.Printf("SBThread: tid = 0x%4.4llx", exe_ctx.GetThreadPtr()->GetID());
Caroline Tice7826c882010-10-26 03:11:13 +00001150 }
1151 else
Greg Clayton96154be2011-11-13 06:57:31 +00001152 strm.PutCString ("No value");
Caroline Tice7826c882010-10-26 03:11:13 +00001153
1154 return true;
1155}