blob: 5a09dcc23a87a40872163fdff4baba5591d68153 [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 Clayton3acaa922012-09-25 02:40:06 +0000107 return exe_ctx.GetThreadPtr()->GetStopReason();
Greg Claytona894fe72012-04-05 16:12:35 +0000108 }
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000109 else
110 {
111 if (log)
112 log->Printf ("SBThread(%p)::GetStopReason() => error: process is running", exe_ctx.GetThreadPtr());
113 }
Chris Lattner24943d22010-06-08 16:52:24 +0000114 }
Caroline Tice7826c882010-10-26 03:11:13 +0000115
116 if (log)
Greg Claytonf4124de2012-02-21 00:09:25 +0000117 log->Printf ("SBThread(%p)::GetStopReason () => %s", exe_ctx.GetThreadPtr(),
Caroline Tice61ba7ec2010-10-26 23:49:36 +0000118 Thread::StopReasonAsCString (reason));
Caroline Tice7826c882010-10-26 03:11:13 +0000119
120 return reason;
Chris Lattner24943d22010-06-08 16:52:24 +0000121}
122
123size_t
Greg Clayton640dc6b2010-11-18 18:52:36 +0000124SBThread::GetStopReasonDataCount ()
125{
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000126 Mutex::Locker api_locker;
127 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
128
Greg Claytonf4124de2012-02-21 00:09:25 +0000129 if (exe_ctx.HasThreadScope())
Greg Clayton640dc6b2010-11-18 18:52:36 +0000130 {
Greg Claytona894fe72012-04-05 16:12:35 +0000131 Process::StopLocker stop_locker;
132 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
Greg Clayton640dc6b2010-11-18 18:52:36 +0000133 {
Greg Claytona894fe72012-04-05 16:12:35 +0000134 StopInfoSP stop_info_sp = exe_ctx.GetThreadPtr()->GetStopInfo ();
135 if (stop_info_sp)
Greg Clayton640dc6b2010-11-18 18:52:36 +0000136 {
Greg Claytona894fe72012-04-05 16:12:35 +0000137 StopReason reason = stop_info_sp->GetStopReason();
138 switch (reason)
Greg Clayton640dc6b2010-11-18 18:52:36 +0000139 {
Greg Claytona894fe72012-04-05 16:12:35 +0000140 case eStopReasonInvalid:
141 case eStopReasonNone:
142 case eStopReasonTrace:
143 case eStopReasonPlanComplete:
144 // There is no data for these stop reasons.
145 return 0;
146
147 case eStopReasonBreakpoint:
148 {
149 break_id_t site_id = stop_info_sp->GetValue();
150 lldb::BreakpointSiteSP bp_site_sp (exe_ctx.GetProcessPtr()->GetBreakpointSiteList().FindByID (site_id));
151 if (bp_site_sp)
152 return bp_site_sp->GetNumberOfOwners () * 2;
153 else
154 return 0; // Breakpoint must have cleared itself...
155 }
156 break;
157
158 case eStopReasonWatchpoint:
159 return 1;
160
161 case eStopReasonSignal:
162 return 1;
163
164 case eStopReasonException:
165 return 1;
Greg Clayton640dc6b2010-11-18 18:52:36 +0000166 }
Greg Clayton640dc6b2010-11-18 18:52:36 +0000167 }
168 }
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000169 else
170 {
171 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
172 if (log)
173 log->Printf ("SBThread(%p)::GetStopReasonDataCount() => error: process is running", exe_ctx.GetThreadPtr());
174 }
Greg Clayton640dc6b2010-11-18 18:52:36 +0000175 }
176 return 0;
177}
178
179uint64_t
180SBThread::GetStopReasonDataAtIndex (uint32_t idx)
181{
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000182 Mutex::Locker api_locker;
183 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
184
Greg Claytonf4124de2012-02-21 00:09:25 +0000185 if (exe_ctx.HasThreadScope())
Greg Clayton640dc6b2010-11-18 18:52:36 +0000186 {
Greg Claytona894fe72012-04-05 16:12:35 +0000187 Process::StopLocker stop_locker;
188 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
Greg Clayton640dc6b2010-11-18 18:52:36 +0000189 {
Greg Claytona894fe72012-04-05 16:12:35 +0000190 Thread *thread = exe_ctx.GetThreadPtr();
191 StopInfoSP stop_info_sp = thread->GetStopInfo ();
192 if (stop_info_sp)
193 {
194 StopReason reason = stop_info_sp->GetStopReason();
195 switch (reason)
Greg Clayton640dc6b2010-11-18 18:52:36 +0000196 {
Greg Claytona894fe72012-04-05 16:12:35 +0000197 case eStopReasonInvalid:
198 case eStopReasonNone:
199 case eStopReasonTrace:
200 case eStopReasonPlanComplete:
201 // There is no data for these stop reasons.
202 return 0;
203
204 case eStopReasonBreakpoint:
Greg Clayton640dc6b2010-11-18 18:52:36 +0000205 {
Greg Claytona894fe72012-04-05 16:12:35 +0000206 break_id_t site_id = stop_info_sp->GetValue();
207 lldb::BreakpointSiteSP bp_site_sp (exe_ctx.GetProcessPtr()->GetBreakpointSiteList().FindByID (site_id));
208 if (bp_site_sp)
Greg Clayton640dc6b2010-11-18 18:52:36 +0000209 {
Greg Claytona894fe72012-04-05 16:12:35 +0000210 uint32_t bp_index = idx / 2;
211 BreakpointLocationSP bp_loc_sp (bp_site_sp->GetOwnerAtIndex (bp_index));
212 if (bp_loc_sp)
Greg Clayton640dc6b2010-11-18 18:52:36 +0000213 {
Greg Claytona894fe72012-04-05 16:12:35 +0000214 if (bp_index & 1)
215 {
216 // Odd idx, return the breakpoint location ID
217 return bp_loc_sp->GetID();
218 }
219 else
220 {
221 // Even idx, return the breakpoint ID
222 return bp_loc_sp->GetBreakpoint().GetID();
223 }
Greg Clayton640dc6b2010-11-18 18:52:36 +0000224 }
225 }
Greg Claytona894fe72012-04-05 16:12:35 +0000226 return LLDB_INVALID_BREAK_ID;
Greg Clayton640dc6b2010-11-18 18:52:36 +0000227 }
Greg Claytona894fe72012-04-05 16:12:35 +0000228 break;
229
230 case eStopReasonWatchpoint:
231 return stop_info_sp->GetValue();
232
233 case eStopReasonSignal:
234 return stop_info_sp->GetValue();
235
236 case eStopReasonException:
237 return stop_info_sp->GetValue();
Greg Clayton640dc6b2010-11-18 18:52:36 +0000238 }
Greg Clayton640dc6b2010-11-18 18:52:36 +0000239 }
240 }
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000241 else
242 {
243 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
244 if (log)
245 log->Printf ("SBThread(%p)::GetStopReasonDataAtIndex() => error: process is running", exe_ctx.GetThreadPtr());
246 }
Greg Clayton640dc6b2010-11-18 18:52:36 +0000247 }
248 return 0;
249}
250
251size_t
Chris Lattner24943d22010-06-08 16:52:24 +0000252SBThread::GetStopDescription (char *dst, size_t dst_len)
253{
Greg Claytone005f2c2010-11-06 01:53:30 +0000254 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000255
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000256 Mutex::Locker api_locker;
257 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
258
Greg Claytonf4124de2012-02-21 00:09:25 +0000259 if (exe_ctx.HasThreadScope())
Chris Lattner24943d22010-06-08 16:52:24 +0000260 {
Greg Claytona894fe72012-04-05 16:12:35 +0000261 Process::StopLocker stop_locker;
262 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
Chris Lattner24943d22010-06-08 16:52:24 +0000263 {
Greg Claytona894fe72012-04-05 16:12:35 +0000264
Greg Claytona894fe72012-04-05 16:12:35 +0000265 StopInfoSP stop_info_sp = exe_ctx.GetThreadPtr()->GetStopInfo ();
266 if (stop_info_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000267 {
Greg Claytona894fe72012-04-05 16:12:35 +0000268 const char *stop_desc = stop_info_sp->GetDescription();
269 if (stop_desc)
Chris Lattner24943d22010-06-08 16:52:24 +0000270 {
Caroline Tice7826c882010-10-26 03:11:13 +0000271 if (log)
Greg Claytona894fe72012-04-05 16:12:35 +0000272 log->Printf ("SBThread(%p)::GetStopDescription (dst, dst_len) => \"%s\"",
Greg Claytonf4124de2012-02-21 00:09:25 +0000273 exe_ctx.GetThreadPtr(), stop_desc);
Chris Lattner24943d22010-06-08 16:52:24 +0000274 if (dst)
Greg Claytona894fe72012-04-05 16:12:35 +0000275 return ::snprintf (dst, dst_len, "%s", stop_desc);
276 else
277 {
278 // NULL dst passed in, return the length needed to contain the description
279 return ::strlen (stop_desc) + 1; // Include the NULL byte for size
280 }
281 }
282 else
283 {
284 size_t stop_desc_len = 0;
285 switch (stop_info_sp->GetStopReason())
286 {
287 case eStopReasonTrace:
288 case eStopReasonPlanComplete:
289 {
290 static char trace_desc[] = "step";
291 stop_desc = trace_desc;
292 stop_desc_len = sizeof(trace_desc); // Include the NULL byte for size
293 }
294 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000295
Greg Claytona894fe72012-04-05 16:12:35 +0000296 case eStopReasonBreakpoint:
297 {
298 static char bp_desc[] = "breakpoint hit";
299 stop_desc = bp_desc;
300 stop_desc_len = sizeof(bp_desc); // Include the NULL byte for size
301 }
302 break;
303
304 case eStopReasonWatchpoint:
305 {
306 static char wp_desc[] = "watchpoint hit";
307 stop_desc = wp_desc;
308 stop_desc_len = sizeof(wp_desc); // Include the NULL byte for size
309 }
310 break;
311
312 case eStopReasonSignal:
313 {
314 stop_desc = exe_ctx.GetProcessPtr()->GetUnixSignals ().GetSignalAsCString (stop_info_sp->GetValue());
315 if (stop_desc == NULL || stop_desc[0] == '\0')
316 {
317 static char signal_desc[] = "signal";
318 stop_desc = signal_desc;
319 stop_desc_len = sizeof(signal_desc); // Include the NULL byte for size
320 }
321 }
322 break;
323
324 case eStopReasonException:
325 {
326 char exc_desc[] = "exception";
327 stop_desc = exc_desc;
328 stop_desc_len = sizeof(exc_desc); // Include the NULL byte for size
329 }
330 break;
331
332 default:
333 break;
334 }
335
336 if (stop_desc && stop_desc[0])
337 {
338 if (log)
339 log->Printf ("SBThread(%p)::GetStopDescription (dst, dst_len) => '%s'",
340 exe_ctx.GetThreadPtr(), stop_desc);
341
342 if (dst)
343 return ::snprintf (dst, dst_len, "%s", stop_desc) + 1; // Include the NULL byte
344
345 if (stop_desc_len == 0)
346 stop_desc_len = ::strlen (stop_desc) + 1; // Include the NULL byte
347
348 return stop_desc_len;
349 }
Chris Lattner24943d22010-06-08 16:52:24 +0000350 }
351 }
352 }
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000353 else
354 {
355 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
356 if (log)
357 log->Printf ("SBThread(%p)::GetStopDescription() => error: process is running", exe_ctx.GetThreadPtr());
358 }
Chris Lattner24943d22010-06-08 16:52:24 +0000359 }
360 if (dst)
361 *dst = 0;
362 return 0;
363}
364
Jim Ingham1586d972011-12-17 01:35:57 +0000365SBValue
366SBThread::GetStopReturnValue ()
367{
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000368 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Ingham1586d972011-12-17 01:35:57 +0000369 ValueObjectSP return_valobj_sp;
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000370 Mutex::Locker api_locker;
371 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
372
Greg Claytonf4124de2012-02-21 00:09:25 +0000373 if (exe_ctx.HasThreadScope())
Jim Ingham1586d972011-12-17 01:35:57 +0000374 {
Greg Claytona894fe72012-04-05 16:12:35 +0000375 Process::StopLocker stop_locker;
376 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
Jim Ingham1586d972011-12-17 01:35:57 +0000377 {
Greg Claytona894fe72012-04-05 16:12:35 +0000378 StopInfoSP stop_info_sp = exe_ctx.GetThreadPtr()->GetStopInfo ();
379 if (stop_info_sp)
380 {
381 return_valobj_sp = StopInfo::GetReturnValueObject (stop_info_sp);
382 }
Jim Ingham1586d972011-12-17 01:35:57 +0000383 }
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000384 else
385 {
386 if (log)
387 log->Printf ("SBThread(%p)::GetStopReturnValue() => error: process is running", exe_ctx.GetThreadPtr());
388 }
Jim Ingham1586d972011-12-17 01:35:57 +0000389 }
390
Jim Ingham1586d972011-12-17 01:35:57 +0000391 if (log)
Greg Claytonf4124de2012-02-21 00:09:25 +0000392 log->Printf ("SBThread(%p)::GetStopReturnValue () => %s", exe_ctx.GetThreadPtr(),
Jim Ingham1586d972011-12-17 01:35:57 +0000393 return_valobj_sp.get()
394 ? return_valobj_sp->GetValueAsCString()
395 : "<no return value>");
396
397 return SBValue (return_valobj_sp);
398}
399
Chris Lattner24943d22010-06-08 16:52:24 +0000400void
401SBThread::SetThread (const ThreadSP& lldb_object_sp)
402{
Greg Claytona894fe72012-04-05 16:12:35 +0000403 m_opaque_sp->SetThreadSP (lldb_object_sp);
Chris Lattner24943d22010-06-08 16:52:24 +0000404}
405
406
407lldb::tid_t
408SBThread::GetThreadID () const
409{
Greg Claytona894fe72012-04-05 16:12:35 +0000410 ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
Greg Clayton90c52142012-01-30 02:53:15 +0000411 if (thread_sp)
Greg Claytonf4124de2012-02-21 00:09:25 +0000412 return thread_sp->GetID();
413 return LLDB_INVALID_THREAD_ID;
Chris Lattner24943d22010-06-08 16:52:24 +0000414}
415
416uint32_t
417SBThread::GetIndexID () const
418{
Greg Claytona894fe72012-04-05 16:12:35 +0000419 ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
Greg Clayton90c52142012-01-30 02:53:15 +0000420 if (thread_sp)
421 return thread_sp->GetIndexID();
Chris Lattner24943d22010-06-08 16:52:24 +0000422 return LLDB_INVALID_INDEX32;
423}
Greg Claytonf4124de2012-02-21 00:09:25 +0000424
Chris Lattner24943d22010-06-08 16:52:24 +0000425const char *
426SBThread::GetName () const
427{
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000428 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytona66ba462010-10-30 04:51:46 +0000429 const char *name = NULL;
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000430 Mutex::Locker api_locker;
431 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
432
Greg Claytonf4124de2012-02-21 00:09:25 +0000433 if (exe_ctx.HasThreadScope())
Greg Claytonbdcda462010-12-20 20:49:23 +0000434 {
Greg Claytona894fe72012-04-05 16:12:35 +0000435 Process::StopLocker stop_locker;
436 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
437 {
Greg Claytona894fe72012-04-05 16:12:35 +0000438 name = exe_ctx.GetThreadPtr()->GetName();
439 }
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000440 else
441 {
442 if (log)
443 log->Printf ("SBThread(%p)::GetName() => error: process is running", exe_ctx.GetThreadPtr());
444 }
Greg Claytonbdcda462010-12-20 20:49:23 +0000445 }
Greg Claytona66ba462010-10-30 04:51:46 +0000446
Caroline Tice7826c882010-10-26 03:11:13 +0000447 if (log)
Greg Claytonf4124de2012-02-21 00:09:25 +0000448 log->Printf ("SBThread(%p)::GetName () => %s", exe_ctx.GetThreadPtr(), name ? name : "NULL");
Caroline Tice7826c882010-10-26 03:11:13 +0000449
Greg Claytona66ba462010-10-30 04:51:46 +0000450 return name;
Chris Lattner24943d22010-06-08 16:52:24 +0000451}
452
453const char *
454SBThread::GetQueueName () const
455{
Greg Claytona66ba462010-10-30 04:51:46 +0000456 const char *name = NULL;
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000457 Mutex::Locker api_locker;
458 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
459
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000460 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytonf4124de2012-02-21 00:09:25 +0000461 if (exe_ctx.HasThreadScope())
Greg Claytonbdcda462010-12-20 20:49:23 +0000462 {
Greg Claytona894fe72012-04-05 16:12:35 +0000463 Process::StopLocker stop_locker;
464 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
465 {
Greg Claytona894fe72012-04-05 16:12:35 +0000466 name = exe_ctx.GetThreadPtr()->GetQueueName();
467 }
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000468 else
469 {
470 if (log)
471 log->Printf ("SBThread(%p)::GetQueueName() => error: process is running", exe_ctx.GetThreadPtr());
472 }
Greg Claytonbdcda462010-12-20 20:49:23 +0000473 }
Greg Claytona66ba462010-10-30 04:51:46 +0000474
Caroline Tice7826c882010-10-26 03:11:13 +0000475 if (log)
Greg Claytonf4124de2012-02-21 00:09:25 +0000476 log->Printf ("SBThread(%p)::GetQueueName () => %s", exe_ctx.GetThreadPtr(), name ? name : "NULL");
Caroline Tice7826c882010-10-26 03:11:13 +0000477
Greg Claytona66ba462010-10-30 04:51:46 +0000478 return name;
Chris Lattner24943d22010-06-08 16:52:24 +0000479}
480
Jim Ingham88e3de22012-05-03 21:19:36 +0000481SBError
482SBThread::ResumeNewPlan (ExecutionContext &exe_ctx, ThreadPlan *new_plan)
483{
484 SBError sb_error;
485
486 Process *process = exe_ctx.GetProcessPtr();
487 if (!process)
488 {
489 sb_error.SetErrorString("No process in SBThread::ResumeNewPlan");
490 return sb_error;
491 }
492
493 Thread *thread = exe_ctx.GetThreadPtr();
494 if (!thread)
495 {
496 sb_error.SetErrorString("No thread in SBThread::ResumeNewPlan");
497 return sb_error;
498 }
499
500 // User level plans should be Master Plans so they can be interrupted, other plans executed, and
501 // then a "continue" will resume the plan.
502 if (new_plan != NULL)
503 {
504 new_plan->SetIsMasterPlan(true);
505 new_plan->SetOkayToDiscard(false);
506 }
507
508 // Why do we need to set the current thread by ID here???
509 process->GetThreadList().SetSelectedThreadByID (thread->GetID());
510 sb_error.ref() = process->Resume();
511
512 if (sb_error.Success())
513 {
514 // If we are doing synchronous mode, then wait for the
515 // process to stop yet again!
516 if (process->GetTarget().GetDebugger().GetAsyncExecution () == false)
517 process->WaitForProcessToStop (NULL);
518 }
519
520 return sb_error;
521}
Chris Lattner24943d22010-06-08 16:52:24 +0000522
523void
Chris Lattner24943d22010-06-08 16:52:24 +0000524SBThread::StepOver (lldb::RunMode stop_other_threads)
525{
Greg Claytone005f2c2010-11-06 01:53:30 +0000526 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000527
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000528 Mutex::Locker api_locker;
529 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
530
Caroline Tice7826c882010-10-26 03:11:13 +0000531
Greg Clayton90c52142012-01-30 02:53:15 +0000532 if (log)
Greg Claytonf4124de2012-02-21 00:09:25 +0000533 log->Printf ("SBThread(%p)::StepOver (stop_other_threads='%s')", exe_ctx.GetThreadPtr(),
Greg Clayton90c52142012-01-30 02:53:15 +0000534 Thread::RunModeAsCString (stop_other_threads));
535
Greg Claytonf4124de2012-02-21 00:09:25 +0000536 if (exe_ctx.HasThreadScope())
Chris Lattner24943d22010-06-08 16:52:24 +0000537 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000538 Thread *thread = exe_ctx.GetThreadPtr();
Jim Inghamd82bc6d2012-05-11 23:47:32 +0000539 bool abort_other_plans = false;
Greg Claytonf4124de2012-02-21 00:09:25 +0000540 StackFrameSP frame_sp(thread->GetStackFrameAtIndex (0));
Jim Ingham88e3de22012-05-03 21:19:36 +0000541 ThreadPlan *new_plan = NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000542
543 if (frame_sp)
544 {
545 if (frame_sp->HasDebugInformation ())
546 {
547 SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
Jim Ingham88e3de22012-05-03 21:19:36 +0000548 new_plan = thread->QueueThreadPlanForStepRange (abort_other_plans,
549 eStepTypeOver,
550 sc.line_entry.range,
551 sc,
552 stop_other_threads,
553 false);
Chris Lattner24943d22010-06-08 16:52:24 +0000554
555 }
556 else
557 {
Jim Ingham88e3de22012-05-03 21:19:36 +0000558 new_plan = thread->QueueThreadPlanForStepSingleInstruction (true,
559 abort_other_plans,
560 stop_other_threads);
Chris Lattner24943d22010-06-08 16:52:24 +0000561 }
562 }
563
Jim Ingham88e3de22012-05-03 21:19:36 +0000564 // This returns an error, we should use it!
565 ResumeNewPlan (exe_ctx, new_plan);
Chris Lattner24943d22010-06-08 16:52:24 +0000566 }
567}
568
569void
570SBThread::StepInto (lldb::RunMode stop_other_threads)
571{
Greg Claytone005f2c2010-11-06 01:53:30 +0000572 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000573
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000574 Mutex::Locker api_locker;
575 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
Greg Clayton90c52142012-01-30 02:53:15 +0000576
577 if (log)
Greg Claytonf4124de2012-02-21 00:09:25 +0000578 log->Printf ("SBThread(%p)::StepInto (stop_other_threads='%s')", exe_ctx.GetThreadPtr(),
Greg Clayton90c52142012-01-30 02:53:15 +0000579 Thread::RunModeAsCString (stop_other_threads));
Greg Claytonf4124de2012-02-21 00:09:25 +0000580 if (exe_ctx.HasThreadScope())
Chris Lattner24943d22010-06-08 16:52:24 +0000581 {
Jim Inghamd82bc6d2012-05-11 23:47:32 +0000582 bool abort_other_plans = false;
Chris Lattner24943d22010-06-08 16:52:24 +0000583
Greg Claytonf4124de2012-02-21 00:09:25 +0000584 Thread *thread = exe_ctx.GetThreadPtr();
585 StackFrameSP frame_sp(thread->GetStackFrameAtIndex (0));
Jim Ingham88e3de22012-05-03 21:19:36 +0000586 ThreadPlan *new_plan = NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000587
588 if (frame_sp && frame_sp->HasDebugInformation ())
589 {
Greg Clayton8f5fd6b2010-06-12 18:59:55 +0000590 bool avoid_code_without_debug_info = true;
Chris Lattner24943d22010-06-08 16:52:24 +0000591 SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
Jim Ingham88e3de22012-05-03 21:19:36 +0000592 new_plan = thread->QueueThreadPlanForStepRange (abort_other_plans,
593 eStepTypeInto,
594 sc.line_entry.range,
595 sc,
596 stop_other_threads,
597 avoid_code_without_debug_info);
Chris Lattner24943d22010-06-08 16:52:24 +0000598 }
599 else
600 {
Jim Ingham88e3de22012-05-03 21:19:36 +0000601 new_plan = thread->QueueThreadPlanForStepSingleInstruction (false,
602 abort_other_plans,
603 stop_other_threads);
Chris Lattner24943d22010-06-08 16:52:24 +0000604 }
Jim Ingham88e3de22012-05-03 21:19:36 +0000605
606 // This returns an error, we should use it!
607 ResumeNewPlan (exe_ctx, new_plan);
Chris Lattner24943d22010-06-08 16:52:24 +0000608 }
609}
610
611void
612SBThread::StepOut ()
613{
Greg Claytone005f2c2010-11-06 01:53:30 +0000614 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000615
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000616 Mutex::Locker api_locker;
617 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
618
Caroline Tice7826c882010-10-26 03:11:13 +0000619
Greg Clayton90c52142012-01-30 02:53:15 +0000620 if (log)
Greg Claytonf4124de2012-02-21 00:09:25 +0000621 log->Printf ("SBThread(%p)::StepOut ()", exe_ctx.GetThreadPtr());
Greg Clayton90c52142012-01-30 02:53:15 +0000622
Greg Claytonf4124de2012-02-21 00:09:25 +0000623 if (exe_ctx.HasThreadScope())
Chris Lattner24943d22010-06-08 16:52:24 +0000624 {
Jim Inghamd82bc6d2012-05-11 23:47:32 +0000625 bool abort_other_plans = false;
Jim Inghambe0cde92012-09-14 21:07:14 +0000626 bool stop_other_threads = false;
Chris Lattner24943d22010-06-08 16:52:24 +0000627
Greg Claytonf4124de2012-02-21 00:09:25 +0000628 Thread *thread = exe_ctx.GetThreadPtr();
629
Jim Ingham88e3de22012-05-03 21:19:36 +0000630 ThreadPlan *new_plan = thread->QueueThreadPlanForStepOut (abort_other_plans,
631 NULL,
632 false,
633 stop_other_threads,
634 eVoteYes,
635 eVoteNoOpinion,
636 0);
637
638 // This returns an error, we should use it!
639 ResumeNewPlan (exe_ctx, new_plan);
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000640 }
641}
Chris Lattner24943d22010-06-08 16:52:24 +0000642
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000643void
644SBThread::StepOutOfFrame (lldb::SBFrame &sb_frame)
645{
646 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
647
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000648 Mutex::Locker api_locker;
649 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
650
Greg Clayton334d33a2012-01-30 07:41:31 +0000651 StackFrameSP frame_sp (sb_frame.GetFrameSP());
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000652 if (log)
653 {
654 SBStream frame_desc_strm;
655 sb_frame.GetDescription (frame_desc_strm);
Greg Claytonf4124de2012-02-21 00:09:25 +0000656 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 +0000657 }
658
Greg Claytonf4124de2012-02-21 00:09:25 +0000659 if (exe_ctx.HasThreadScope())
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000660 {
Jim Inghamd82bc6d2012-05-11 23:47:32 +0000661 bool abort_other_plans = false;
Jim Inghambe0cde92012-09-14 21:07:14 +0000662 bool stop_other_threads = false;
Greg Claytonf4124de2012-02-21 00:09:25 +0000663 Thread *thread = exe_ctx.GetThreadPtr();
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000664
Jim Ingham88e3de22012-05-03 21:19:36 +0000665 ThreadPlan *new_plan = thread->QueueThreadPlanForStepOut (abort_other_plans,
666 NULL,
667 false,
668 stop_other_threads,
669 eVoteYes,
670 eVoteNoOpinion,
671 frame_sp->GetFrameIndex());
672
673 // This returns an error, we should use it!
674 ResumeNewPlan (exe_ctx, new_plan);
Chris Lattner24943d22010-06-08 16:52:24 +0000675 }
676}
677
678void
679SBThread::StepInstruction (bool step_over)
680{
Greg Claytone005f2c2010-11-06 01:53:30 +0000681 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000682
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000683 Mutex::Locker api_locker;
684 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
685
Greg Claytonf4124de2012-02-21 00:09:25 +0000686
Caroline Tice7826c882010-10-26 03:11:13 +0000687
Greg Clayton90c52142012-01-30 02:53:15 +0000688 if (log)
Greg Claytonf4124de2012-02-21 00:09:25 +0000689 log->Printf ("SBThread(%p)::StepInstruction (step_over=%i)", exe_ctx.GetThreadPtr(), step_over);
Greg Clayton90c52142012-01-30 02:53:15 +0000690
Greg Claytonf4124de2012-02-21 00:09:25 +0000691 if (exe_ctx.HasThreadScope())
Chris Lattner24943d22010-06-08 16:52:24 +0000692 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000693 Thread *thread = exe_ctx.GetThreadPtr();
Jim Ingham88e3de22012-05-03 21:19:36 +0000694 ThreadPlan *new_plan = thread->QueueThreadPlanForStepSingleInstruction (step_over, true, true);
695
696 // This returns an error, we should use it!
697 ResumeNewPlan (exe_ctx, new_plan);
Chris Lattner24943d22010-06-08 16:52:24 +0000698 }
699}
700
701void
702SBThread::RunToAddress (lldb::addr_t addr)
703{
Greg Claytone005f2c2010-11-06 01:53:30 +0000704 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000705
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000706 Mutex::Locker api_locker;
707 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
708
Caroline Tice7826c882010-10-26 03:11:13 +0000709
Greg Clayton90c52142012-01-30 02:53:15 +0000710 if (log)
Greg Claytonf4124de2012-02-21 00:09:25 +0000711 log->Printf ("SBThread(%p)::RunToAddress (addr=0x%llx)", exe_ctx.GetThreadPtr(), addr);
Greg Clayton90c52142012-01-30 02:53:15 +0000712
Greg Claytonf4124de2012-02-21 00:09:25 +0000713 if (exe_ctx.HasThreadScope())
Chris Lattner24943d22010-06-08 16:52:24 +0000714 {
Jim Inghamd82bc6d2012-05-11 23:47:32 +0000715 bool abort_other_plans = false;
Chris Lattner24943d22010-06-08 16:52:24 +0000716 bool stop_other_threads = true;
717
Greg Clayton3508c382012-02-24 01:59:29 +0000718 Address target_addr (addr);
Chris Lattner24943d22010-06-08 16:52:24 +0000719
Greg Claytonf4124de2012-02-21 00:09:25 +0000720 Thread *thread = exe_ctx.GetThreadPtr();
Greg Claytonf4124de2012-02-21 00:09:25 +0000721
Jim Ingham88e3de22012-05-03 21:19:36 +0000722 ThreadPlan *new_plan = thread->QueueThreadPlanForRunToAddress (abort_other_plans, target_addr, stop_other_threads);
723
724 // This returns an error, we should use it!
725 ResumeNewPlan (exe_ctx, new_plan);
Chris Lattner24943d22010-06-08 16:52:24 +0000726 }
Chris Lattner24943d22010-06-08 16:52:24 +0000727}
728
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000729SBError
730SBThread::StepOverUntil (lldb::SBFrame &sb_frame,
731 lldb::SBFileSpec &sb_file_spec,
732 uint32_t line)
733{
734 SBError sb_error;
735 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
736 char path[PATH_MAX];
Greg Clayton90c52142012-01-30 02:53:15 +0000737
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000738 Mutex::Locker api_locker;
739 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
740
Greg Clayton334d33a2012-01-30 07:41:31 +0000741 StackFrameSP frame_sp (sb_frame.GetFrameSP());
742
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000743 if (log)
744 {
745 SBStream frame_desc_strm;
746 sb_frame.GetDescription (frame_desc_strm);
747 sb_file_spec->GetPath (path, sizeof(path));
748 log->Printf ("SBThread(%p)::StepOverUntil (frame = SBFrame(%p): %s, file+line = %s:%u)",
Greg Claytonf4124de2012-02-21 00:09:25 +0000749 exe_ctx.GetThreadPtr(),
Greg Clayton334d33a2012-01-30 07:41:31 +0000750 frame_sp.get(),
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000751 frame_desc_strm.GetData(),
752 path, line);
753 }
Greg Clayton90c52142012-01-30 02:53:15 +0000754
Greg Claytonf4124de2012-02-21 00:09:25 +0000755 if (exe_ctx.HasThreadScope())
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000756 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000757 Target *target = exe_ctx.GetTargetPtr();
Greg Claytonf4124de2012-02-21 00:09:25 +0000758 Thread *thread = exe_ctx.GetThreadPtr();
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000759
760 if (line == 0)
761 {
762 sb_error.SetErrorString("invalid line argument");
763 return sb_error;
764 }
765
Greg Clayton334d33a2012-01-30 07:41:31 +0000766 if (!frame_sp)
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000767 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000768 frame_sp = thread->GetSelectedFrame ();
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000769 if (!frame_sp)
Greg Claytonf4124de2012-02-21 00:09:25 +0000770 frame_sp = thread->GetStackFrameAtIndex (0);
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000771 }
772
773 SymbolContext frame_sc;
774 if (!frame_sp)
775 {
776 sb_error.SetErrorString("no valid frames in thread to step");
777 return sb_error;
778 }
779
780 // If we have a frame, get its line
781 frame_sc = frame_sp->GetSymbolContext (eSymbolContextCompUnit |
782 eSymbolContextFunction |
783 eSymbolContextLineEntry |
784 eSymbolContextSymbol );
785
786 if (frame_sc.comp_unit == NULL)
787 {
788 sb_error.SetErrorStringWithFormat("frame %u doesn't have debug information", frame_sp->GetFrameIndex());
789 return sb_error;
790 }
791
792 FileSpec step_file_spec;
793 if (sb_file_spec.IsValid())
794 {
795 // The file spec passed in was valid, so use it
796 step_file_spec = sb_file_spec.ref();
797 }
798 else
799 {
800 if (frame_sc.line_entry.IsValid())
801 step_file_spec = frame_sc.line_entry.file;
802 else
803 {
804 sb_error.SetErrorString("invalid file argument or no file for frame");
805 return sb_error;
806 }
807 }
808
Jim Inghamb07c62a2011-05-08 00:56:32 +0000809 // Grab the current function, then we will make sure the "until" address is
810 // within the function. We discard addresses that are out of the current
811 // function, and then if there are no addresses remaining, give an appropriate
812 // error message.
813
814 bool all_in_function = true;
815 AddressRange fun_range = frame_sc.function->GetAddressRange();
816
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000817 std::vector<addr_t> step_over_until_addrs;
Jim Inghamd82bc6d2012-05-11 23:47:32 +0000818 const bool abort_other_plans = false;
Jim Ingham52124e72012-09-14 18:57:14 +0000819 const bool stop_other_threads = false;
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000820 const bool check_inlines = true;
821 const bool exact = false;
822
823 SymbolContextList sc_list;
Jim Inghamb07c62a2011-05-08 00:56:32 +0000824 const uint32_t num_matches = frame_sc.comp_unit->ResolveSymbolContext (step_file_spec,
825 line,
826 check_inlines,
827 exact,
828 eSymbolContextLineEntry,
829 sc_list);
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000830 if (num_matches > 0)
831 {
832 SymbolContext sc;
833 for (uint32_t i=0; i<num_matches; ++i)
834 {
835 if (sc_list.GetContextAtIndex(i, sc))
836 {
Jim Inghamb07c62a2011-05-08 00:56:32 +0000837 addr_t step_addr = sc.line_entry.range.GetBaseAddress().GetLoadAddress(target);
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000838 if (step_addr != LLDB_INVALID_ADDRESS)
839 {
Jim Inghamb07c62a2011-05-08 00:56:32 +0000840 if (fun_range.ContainsLoadAddress(step_addr, target))
841 step_over_until_addrs.push_back(step_addr);
842 else
843 all_in_function = false;
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000844 }
845 }
846 }
847 }
Jim Inghamb07c62a2011-05-08 00:56:32 +0000848
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000849 if (step_over_until_addrs.empty())
850 {
Jim Inghamb07c62a2011-05-08 00:56:32 +0000851 if (all_in_function)
852 {
853 step_file_spec.GetPath (path, sizeof(path));
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000854 sb_error.SetErrorStringWithFormat("No line entries for %s:%u", path, line);
Jim Inghamb07c62a2011-05-08 00:56:32 +0000855 }
856 else
Greg Clayton9c236732011-10-26 00:56:27 +0000857 sb_error.SetErrorString ("step until target not in current function");
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000858 }
859 else
860 {
Jim Ingham88e3de22012-05-03 21:19:36 +0000861 ThreadPlan *new_plan = thread->QueueThreadPlanForStepUntil (abort_other_plans,
862 &step_over_until_addrs[0],
863 step_over_until_addrs.size(),
864 stop_other_threads,
865 frame_sp->GetFrameIndex());
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000866
Jim Ingham88e3de22012-05-03 21:19:36 +0000867 sb_error = ResumeNewPlan (exe_ctx, new_plan);
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000868 }
869 }
870 else
871 {
872 sb_error.SetErrorString("this SBThread object is invalid");
873 }
874 return sb_error;
875}
876
Jim Inghama17a81a2012-09-12 00:40:39 +0000877SBError
Jim Inghamf59388a2012-09-14 02:14:15 +0000878SBThread::ReturnFromFrame (SBFrame &frame, SBValue &return_value)
Jim Inghama17a81a2012-09-12 00:40:39 +0000879{
880 SBError sb_error;
881
882 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
883
884 Mutex::Locker api_locker;
885 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
886
887
888 if (log)
Jim Inghamf59388a2012-09-14 02:14:15 +0000889 log->Printf ("SBThread(%p)::ReturnFromFrame (frame=%d)", exe_ctx.GetThreadPtr(), frame.GetFrameID());
Jim Inghama17a81a2012-09-12 00:40:39 +0000890
891 if (exe_ctx.HasThreadScope())
892 {
893 Thread *thread = exe_ctx.GetThreadPtr();
Jim Inghamf59388a2012-09-14 02:14:15 +0000894 sb_error.SetError (thread->ReturnFromFrame(frame.GetFrameSP(), return_value.GetSP()));
Jim Inghama17a81a2012-09-12 00:40:39 +0000895 }
896
897 return sb_error;
898}
899
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000900
Greg Clayton123db402011-01-12 02:25:42 +0000901bool
902SBThread::Suspend()
903{
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000904 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytona894fe72012-04-05 16:12:35 +0000905 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000906 bool result = false;
Greg Claytonf4124de2012-02-21 00:09:25 +0000907 if (exe_ctx.HasThreadScope())
Greg Clayton123db402011-01-12 02:25:42 +0000908 {
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000909 Process::StopLocker stop_locker;
910 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
911 {
912 exe_ctx.GetThreadPtr()->SetResumeState (eStateSuspended);
913 result = true;
914 }
915 else
916 {
917 if (log)
918 log->Printf ("SBThread(%p)::Suspend() => error: process is running", exe_ctx.GetThreadPtr());
919 }
Greg Clayton123db402011-01-12 02:25:42 +0000920 }
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000921 if (log)
922 log->Printf ("SBThread(%p)::Suspend() => %i", exe_ctx.GetThreadPtr(), result);
923 return result;
Greg Clayton123db402011-01-12 02:25:42 +0000924}
925
926bool
927SBThread::Resume ()
928{
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000929 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytona894fe72012-04-05 16:12:35 +0000930 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000931 bool result = false;
Greg Claytonf4124de2012-02-21 00:09:25 +0000932 if (exe_ctx.HasThreadScope())
Greg Clayton123db402011-01-12 02:25:42 +0000933 {
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000934 Process::StopLocker stop_locker;
935 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
936 {
937 exe_ctx.GetThreadPtr()->SetResumeState (eStateRunning);
938 result = true;
939 }
940 else
941 {
942 if (log)
943 log->Printf ("SBThread(%p)::Resume() => error: process is running", exe_ctx.GetThreadPtr());
944 }
Greg Clayton123db402011-01-12 02:25:42 +0000945 }
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000946 if (log)
947 log->Printf ("SBThread(%p)::Resume() => %i", exe_ctx.GetThreadPtr(), result);
948 return result;
Greg Clayton123db402011-01-12 02:25:42 +0000949}
950
951bool
952SBThread::IsSuspended()
953{
Greg Claytona894fe72012-04-05 16:12:35 +0000954 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Claytonf4124de2012-02-21 00:09:25 +0000955 if (exe_ctx.HasThreadScope())
956 return exe_ctx.GetThreadPtr()->GetResumeState () == eStateSuspended;
Greg Clayton123db402011-01-12 02:25:42 +0000957 return false;
958}
959
Chris Lattner24943d22010-06-08 16:52:24 +0000960SBProcess
961SBThread::GetProcess ()
962{
Caroline Tice7826c882010-10-26 03:11:13 +0000963
Greg Clayton334d33a2012-01-30 07:41:31 +0000964 SBProcess sb_process;
965 ProcessSP process_sp;
Greg Claytona894fe72012-04-05 16:12:35 +0000966 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Claytonf4124de2012-02-21 00:09:25 +0000967 if (exe_ctx.HasThreadScope())
Chris Lattner24943d22010-06-08 16:52:24 +0000968 {
969 // Have to go up to the target so we can get a shared pointer to our process...
Greg Claytonf4124de2012-02-21 00:09:25 +0000970 sb_process.SetSP (exe_ctx.GetProcessSP());
Chris Lattner24943d22010-06-08 16:52:24 +0000971 }
Caroline Tice7826c882010-10-26 03:11:13 +0000972
Greg Claytone005f2c2010-11-06 01:53:30 +0000973 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000974 if (log)
975 {
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000976 SBStream frame_desc_strm;
Greg Clayton334d33a2012-01-30 07:41:31 +0000977 sb_process.GetDescription (frame_desc_strm);
Greg Claytonf4124de2012-02-21 00:09:25 +0000978 log->Printf ("SBThread(%p)::GetProcess () => SBProcess(%p): %s", exe_ctx.GetThreadPtr(),
Greg Clayton334d33a2012-01-30 07:41:31 +0000979 process_sp.get(), frame_desc_strm.GetData());
Caroline Tice7826c882010-10-26 03:11:13 +0000980 }
981
Greg Clayton334d33a2012-01-30 07:41:31 +0000982 return sb_process;
Chris Lattner24943d22010-06-08 16:52:24 +0000983}
984
985uint32_t
986SBThread::GetNumFrames ()
987{
Greg Claytone005f2c2010-11-06 01:53:30 +0000988 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000989
Caroline Tice7826c882010-10-26 03:11:13 +0000990 uint32_t num_frames = 0;
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000991 Mutex::Locker api_locker;
992 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
993
Greg Claytonf4124de2012-02-21 00:09:25 +0000994 if (exe_ctx.HasThreadScope())
Greg Claytonbdcda462010-12-20 20:49:23 +0000995 {
Greg Claytona894fe72012-04-05 16:12:35 +0000996 Process::StopLocker stop_locker;
997 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
998 {
Greg Claytona894fe72012-04-05 16:12:35 +0000999 num_frames = exe_ctx.GetThreadPtr()->GetStackFrameCount();
1000 }
Greg Clayton9f3c98e2012-04-06 02:17:47 +00001001 else
1002 {
1003 if (log)
1004 log->Printf ("SBThread(%p)::GetNumFrames() => error: process is running", exe_ctx.GetThreadPtr());
1005 }
Greg Claytonbdcda462010-12-20 20:49:23 +00001006 }
Caroline Tice7826c882010-10-26 03:11:13 +00001007
1008 if (log)
Greg Claytonf4124de2012-02-21 00:09:25 +00001009 log->Printf ("SBThread(%p)::GetNumFrames () => %u", exe_ctx.GetThreadPtr(), num_frames);
Caroline Tice7826c882010-10-26 03:11:13 +00001010
1011 return num_frames;
Chris Lattner24943d22010-06-08 16:52:24 +00001012}
1013
1014SBFrame
1015SBThread::GetFrameAtIndex (uint32_t idx)
1016{
Greg Claytone005f2c2010-11-06 01:53:30 +00001017 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +00001018
Chris Lattner24943d22010-06-08 16:52:24 +00001019 SBFrame sb_frame;
Greg Clayton334d33a2012-01-30 07:41:31 +00001020 StackFrameSP frame_sp;
Jim Inghamd0bdddf2012-08-22 21:34:33 +00001021 Mutex::Locker api_locker;
1022 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1023
Greg Claytonf4124de2012-02-21 00:09:25 +00001024 if (exe_ctx.HasThreadScope())
Greg Claytonbdcda462010-12-20 20:49:23 +00001025 {
Greg Claytona894fe72012-04-05 16:12:35 +00001026 Process::StopLocker stop_locker;
1027 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1028 {
Greg Claytona894fe72012-04-05 16:12:35 +00001029 frame_sp = exe_ctx.GetThreadPtr()->GetStackFrameAtIndex (idx);
1030 sb_frame.SetFrameSP (frame_sp);
1031 }
Greg Clayton9f3c98e2012-04-06 02:17:47 +00001032 else
1033 {
1034 if (log)
1035 log->Printf ("SBThread(%p)::GetFrameAtIndex() => error: process is running", exe_ctx.GetThreadPtr());
1036 }
Greg Claytonbdcda462010-12-20 20:49:23 +00001037 }
Caroline Tice7826c882010-10-26 03:11:13 +00001038
1039 if (log)
1040 {
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001041 SBStream frame_desc_strm;
1042 sb_frame.GetDescription (frame_desc_strm);
Greg Claytona66ba462010-10-30 04:51:46 +00001043 log->Printf ("SBThread(%p)::GetFrameAtIndex (idx=%d) => SBFrame(%p): %s",
Greg Claytonf4124de2012-02-21 00:09:25 +00001044 exe_ctx.GetThreadPtr(), idx, frame_sp.get(), frame_desc_strm.GetData());
Caroline Tice7826c882010-10-26 03:11:13 +00001045 }
1046
Chris Lattner24943d22010-06-08 16:52:24 +00001047 return sb_frame;
1048}
1049
Greg Claytonc5157ec2010-12-17 02:26:24 +00001050lldb::SBFrame
1051SBThread::GetSelectedFrame ()
1052{
1053 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1054
1055 SBFrame sb_frame;
Greg Clayton334d33a2012-01-30 07:41:31 +00001056 StackFrameSP frame_sp;
Jim Inghamd0bdddf2012-08-22 21:34:33 +00001057 Mutex::Locker api_locker;
1058 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1059
Greg Claytonf4124de2012-02-21 00:09:25 +00001060 if (exe_ctx.HasThreadScope())
Greg Claytonbdcda462010-12-20 20:49:23 +00001061 {
Greg Claytona894fe72012-04-05 16:12:35 +00001062 Process::StopLocker stop_locker;
1063 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1064 {
Greg Claytona894fe72012-04-05 16:12:35 +00001065 frame_sp = exe_ctx.GetThreadPtr()->GetSelectedFrame ();
1066 sb_frame.SetFrameSP (frame_sp);
1067 }
Greg Clayton9f3c98e2012-04-06 02:17:47 +00001068 else
1069 {
1070 if (log)
1071 log->Printf ("SBThread(%p)::GetSelectedFrame() => error: process is running", exe_ctx.GetThreadPtr());
1072 }
Greg Claytonbdcda462010-12-20 20:49:23 +00001073 }
Greg Claytonc5157ec2010-12-17 02:26:24 +00001074
1075 if (log)
1076 {
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001077 SBStream frame_desc_strm;
1078 sb_frame.GetDescription (frame_desc_strm);
Greg Claytonc5157ec2010-12-17 02:26:24 +00001079 log->Printf ("SBThread(%p)::GetSelectedFrame () => SBFrame(%p): %s",
Greg Claytonf4124de2012-02-21 00:09:25 +00001080 exe_ctx.GetThreadPtr(), frame_sp.get(), frame_desc_strm.GetData());
Greg Claytonc5157ec2010-12-17 02:26:24 +00001081 }
1082
1083 return sb_frame;
1084}
1085
1086lldb::SBFrame
1087SBThread::SetSelectedFrame (uint32_t idx)
1088{
1089 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1090
1091 SBFrame sb_frame;
Greg Clayton334d33a2012-01-30 07:41:31 +00001092 StackFrameSP frame_sp;
Jim Inghamd0bdddf2012-08-22 21:34:33 +00001093 Mutex::Locker api_locker;
1094 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1095
Greg Claytonf4124de2012-02-21 00:09:25 +00001096 if (exe_ctx.HasThreadScope())
Greg Claytonc5157ec2010-12-17 02:26:24 +00001097 {
Greg Claytona894fe72012-04-05 16:12:35 +00001098 Process::StopLocker stop_locker;
1099 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
Greg Claytonc5157ec2010-12-17 02:26:24 +00001100 {
Greg Claytona894fe72012-04-05 16:12:35 +00001101 Thread *thread = exe_ctx.GetThreadPtr();
1102 frame_sp = thread->GetStackFrameAtIndex (idx);
1103 if (frame_sp)
1104 {
1105 thread->SetSelectedFrame (frame_sp.get());
1106 sb_frame.SetFrameSP (frame_sp);
1107 }
Greg Claytonc5157ec2010-12-17 02:26:24 +00001108 }
Greg Clayton9f3c98e2012-04-06 02:17:47 +00001109 else
1110 {
1111 if (log)
1112 log->Printf ("SBThread(%p)::SetSelectedFrame() => error: process is running", exe_ctx.GetThreadPtr());
1113 }
Greg Claytonc5157ec2010-12-17 02:26:24 +00001114 }
1115
1116 if (log)
1117 {
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001118 SBStream frame_desc_strm;
1119 sb_frame.GetDescription (frame_desc_strm);
Greg Claytonc5157ec2010-12-17 02:26:24 +00001120 log->Printf ("SBThread(%p)::SetSelectedFrame (idx=%u) => SBFrame(%p): %s",
Greg Claytonf4124de2012-02-21 00:09:25 +00001121 exe_ctx.GetThreadPtr(), idx, frame_sp.get(), frame_desc_strm.GetData());
Greg Claytonc5157ec2010-12-17 02:26:24 +00001122 }
1123 return sb_frame;
1124}
1125
1126
Chris Lattner24943d22010-06-08 16:52:24 +00001127bool
1128SBThread::operator == (const SBThread &rhs) const
1129{
Greg Claytona894fe72012-04-05 16:12:35 +00001130 return m_opaque_sp->GetThreadSP().get() == rhs.m_opaque_sp->GetThreadSP().get();
Chris Lattner24943d22010-06-08 16:52:24 +00001131}
1132
1133bool
1134SBThread::operator != (const SBThread &rhs) const
1135{
Greg Claytona894fe72012-04-05 16:12:35 +00001136 return m_opaque_sp->GetThreadSP().get() != rhs.m_opaque_sp->GetThreadSP().get();
Chris Lattner24943d22010-06-08 16:52:24 +00001137}
Caroline Tice98f930f2010-09-20 05:20:02 +00001138
1139bool
Caroline Tice7826c882010-10-26 03:11:13 +00001140SBThread::GetDescription (SBStream &description) const
1141{
Greg Clayton96154be2011-11-13 06:57:31 +00001142 Stream &strm = description.ref();
1143
Greg Claytona894fe72012-04-05 16:12:35 +00001144 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Claytonf4124de2012-02-21 00:09:25 +00001145 if (exe_ctx.HasThreadScope())
Caroline Tice7826c882010-10-26 03:11:13 +00001146 {
Greg Claytonf4124de2012-02-21 00:09:25 +00001147 strm.Printf("SBThread: tid = 0x%4.4llx", exe_ctx.GetThreadPtr()->GetID());
Caroline Tice7826c882010-10-26 03:11:13 +00001148 }
1149 else
Greg Clayton96154be2011-11-13 06:57:31 +00001150 strm.PutCString ("No value");
Caroline Tice7826c882010-10-26 03:11:13 +00001151
1152 return true;
1153}