blob: 2e5b7b704105a3539cd15e33d71d31297531335d [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
Daniel Malead891f9b2012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Eli Friedman7a62c8b2010-06-09 07:44:37 +000012#include "lldb/API/SBThread.h"
Chris Lattner24943d22010-06-08 16:52:24 +000013
14#include "lldb/API/SBSymbolContext.h"
15#include "lldb/API/SBFileSpec.h"
Caroline Tice98f930f2010-09-20 05:20:02 +000016#include "lldb/API/SBStream.h"
Greg Clayton640dc6b2010-11-18 18:52:36 +000017#include "lldb/Breakpoint/BreakpointLocation.h"
Greg Clayton63094e02010-06-23 01:19:29 +000018#include "lldb/Core/Debugger.h"
Chris Lattner24943d22010-06-08 16:52:24 +000019#include "lldb/Core/Stream.h"
20#include "lldb/Core/StreamFile.h"
Greg Clayton63094e02010-06-23 01:19:29 +000021#include "lldb/Interpreter/CommandInterpreter.h"
Chris Lattner24943d22010-06-08 16:52:24 +000022#include "lldb/Target/Thread.h"
23#include "lldb/Target/Process.h"
24#include "lldb/Symbol/SymbolContext.h"
25#include "lldb/Symbol/CompileUnit.h"
Greg Clayton643ee732010-08-04 01:40:35 +000026#include "lldb/Target/StopInfo.h"
Chris Lattner24943d22010-06-08 16:52:24 +000027#include "lldb/Target/Target.h"
28#include "lldb/Target/ThreadPlan.h"
Chris Lattner24943d22010-06-08 16:52:24 +000029#include "lldb/Target/ThreadPlanStepInstruction.h"
30#include "lldb/Target/ThreadPlanStepOut.h"
31#include "lldb/Target/ThreadPlanStepRange.h"
32#include "lldb/Target/ThreadPlanStepInRange.h"
33
34
Eli Friedman7a62c8b2010-06-09 07:44:37 +000035#include "lldb/API/SBAddress.h"
Eli Friedman7a62c8b2010-06-09 07:44:37 +000036#include "lldb/API/SBDebugger.h"
Jim Ingham94a5d0d2012-10-10 18:32:14 +000037#include "lldb/API/SBEvent.h"
Jim Ingham1586d972011-12-17 01:35:57 +000038#include "lldb/API/SBFrame.h"
Eli Friedman7a62c8b2010-06-09 07:44:37 +000039#include "lldb/API/SBProcess.h"
Jim Ingham1586d972011-12-17 01:35:57 +000040#include "lldb/API/SBValue.h"
Chris Lattner24943d22010-06-08 16:52:24 +000041
42using namespace lldb;
43using namespace lldb_private;
44
Jim Ingham94a5d0d2012-10-10 18:32:14 +000045const char *
46SBThread::GetBroadcasterClassName ()
47{
48 return Thread::GetStaticBroadcasterClass().AsCString();
49}
50
Greg Clayton49ce6822010-10-31 03:01:06 +000051//----------------------------------------------------------------------
52// Constructors
53//----------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +000054SBThread::SBThread () :
Greg Claytona894fe72012-04-05 16:12:35 +000055 m_opaque_sp (new ExecutionContextRef())
Chris Lattner24943d22010-06-08 16:52:24 +000056{
57}
58
Chris Lattner24943d22010-06-08 16:52:24 +000059SBThread::SBThread (const ThreadSP& lldb_object_sp) :
Greg Claytona894fe72012-04-05 16:12:35 +000060 m_opaque_sp (new ExecutionContextRef(lldb_object_sp))
Chris Lattner24943d22010-06-08 16:52:24 +000061{
62}
63
Greg Clayton1b284412010-10-30 18:26:59 +000064SBThread::SBThread (const SBThread &rhs) :
Greg Claytona894fe72012-04-05 16:12:35 +000065 m_opaque_sp (new ExecutionContextRef(*rhs.m_opaque_sp))
Chris Lattner24943d22010-06-08 16:52:24 +000066{
Greg Claytona894fe72012-04-05 16:12:35 +000067
Chris Lattner24943d22010-06-08 16:52:24 +000068}
69
70//----------------------------------------------------------------------
Greg Clayton49ce6822010-10-31 03:01:06 +000071// Assignment operator
72//----------------------------------------------------------------------
73
74const lldb::SBThread &
75SBThread::operator = (const SBThread &rhs)
76{
77 if (this != &rhs)
Greg Claytona894fe72012-04-05 16:12:35 +000078 *m_opaque_sp = *rhs.m_opaque_sp;
Greg Clayton49ce6822010-10-31 03:01:06 +000079 return *this;
80}
81
82//----------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +000083// Destructor
84//----------------------------------------------------------------------
85SBThread::~SBThread()
86{
87}
88
89bool
90SBThread::IsValid() const
91{
Greg Claytona894fe72012-04-05 16:12:35 +000092 return m_opaque_sp->GetThreadSP().get() != NULL;
Chris Lattner24943d22010-06-08 16:52:24 +000093}
94
Greg Clayton43490d12010-07-30 20:12:55 +000095void
96SBThread::Clear ()
97{
Greg Claytona894fe72012-04-05 16:12:35 +000098 m_opaque_sp->Clear();
Greg Clayton43490d12010-07-30 20:12:55 +000099}
100
101
Chris Lattner24943d22010-06-08 16:52:24 +0000102StopReason
103SBThread::GetStopReason()
104{
Greg Claytone005f2c2010-11-06 01:53:30 +0000105 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000106
Caroline Tice7826c882010-10-26 03:11:13 +0000107 StopReason reason = eStopReasonInvalid;
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000108 Mutex::Locker api_locker;
109 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
110
Greg Claytonf4124de2012-02-21 00:09:25 +0000111 if (exe_ctx.HasThreadScope())
Chris Lattner24943d22010-06-08 16:52:24 +0000112 {
Greg Claytona894fe72012-04-05 16:12:35 +0000113 Process::StopLocker stop_locker;
114 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
115 {
Greg Clayton3acaa922012-09-25 02:40:06 +0000116 return exe_ctx.GetThreadPtr()->GetStopReason();
Greg Claytona894fe72012-04-05 16:12:35 +0000117 }
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000118 else
119 {
120 if (log)
121 log->Printf ("SBThread(%p)::GetStopReason() => error: process is running", exe_ctx.GetThreadPtr());
122 }
Chris Lattner24943d22010-06-08 16:52:24 +0000123 }
Caroline Tice7826c882010-10-26 03:11:13 +0000124
125 if (log)
Greg Claytonf4124de2012-02-21 00:09:25 +0000126 log->Printf ("SBThread(%p)::GetStopReason () => %s", exe_ctx.GetThreadPtr(),
Caroline Tice61ba7ec2010-10-26 23:49:36 +0000127 Thread::StopReasonAsCString (reason));
Caroline Tice7826c882010-10-26 03:11:13 +0000128
129 return reason;
Chris Lattner24943d22010-06-08 16:52:24 +0000130}
131
132size_t
Greg Clayton640dc6b2010-11-18 18:52:36 +0000133SBThread::GetStopReasonDataCount ()
134{
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000135 Mutex::Locker api_locker;
136 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
137
Greg Claytonf4124de2012-02-21 00:09:25 +0000138 if (exe_ctx.HasThreadScope())
Greg Clayton640dc6b2010-11-18 18:52:36 +0000139 {
Greg Claytona894fe72012-04-05 16:12:35 +0000140 Process::StopLocker stop_locker;
141 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
Greg Clayton640dc6b2010-11-18 18:52:36 +0000142 {
Greg Claytona894fe72012-04-05 16:12:35 +0000143 StopInfoSP stop_info_sp = exe_ctx.GetThreadPtr()->GetStopInfo ();
144 if (stop_info_sp)
Greg Clayton640dc6b2010-11-18 18:52:36 +0000145 {
Greg Claytona894fe72012-04-05 16:12:35 +0000146 StopReason reason = stop_info_sp->GetStopReason();
147 switch (reason)
Greg Clayton640dc6b2010-11-18 18:52:36 +0000148 {
Greg Claytona894fe72012-04-05 16:12:35 +0000149 case eStopReasonInvalid:
150 case eStopReasonNone:
151 case eStopReasonTrace:
Greg Clayton0bce9a22012-12-05 00:16:59 +0000152 case eStopReasonExec:
Greg Claytona894fe72012-04-05 16:12:35 +0000153 case eStopReasonPlanComplete:
154 // There is no data for these stop reasons.
155 return 0;
156
157 case eStopReasonBreakpoint:
158 {
159 break_id_t site_id = stop_info_sp->GetValue();
160 lldb::BreakpointSiteSP bp_site_sp (exe_ctx.GetProcessPtr()->GetBreakpointSiteList().FindByID (site_id));
161 if (bp_site_sp)
162 return bp_site_sp->GetNumberOfOwners () * 2;
163 else
164 return 0; // Breakpoint must have cleared itself...
165 }
166 break;
167
168 case eStopReasonWatchpoint:
169 return 1;
170
171 case eStopReasonSignal:
172 return 1;
173
174 case eStopReasonException:
175 return 1;
Greg Clayton640dc6b2010-11-18 18:52:36 +0000176 }
Greg Clayton640dc6b2010-11-18 18:52:36 +0000177 }
178 }
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000179 else
180 {
181 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
182 if (log)
183 log->Printf ("SBThread(%p)::GetStopReasonDataCount() => error: process is running", exe_ctx.GetThreadPtr());
184 }
Greg Clayton640dc6b2010-11-18 18:52:36 +0000185 }
186 return 0;
187}
188
189uint64_t
190SBThread::GetStopReasonDataAtIndex (uint32_t idx)
191{
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000192 Mutex::Locker api_locker;
193 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
194
Greg Claytonf4124de2012-02-21 00:09:25 +0000195 if (exe_ctx.HasThreadScope())
Greg Clayton640dc6b2010-11-18 18:52:36 +0000196 {
Greg Claytona894fe72012-04-05 16:12:35 +0000197 Process::StopLocker stop_locker;
198 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
Greg Clayton640dc6b2010-11-18 18:52:36 +0000199 {
Greg Claytona894fe72012-04-05 16:12:35 +0000200 Thread *thread = exe_ctx.GetThreadPtr();
201 StopInfoSP stop_info_sp = thread->GetStopInfo ();
202 if (stop_info_sp)
203 {
204 StopReason reason = stop_info_sp->GetStopReason();
205 switch (reason)
Greg Clayton640dc6b2010-11-18 18:52:36 +0000206 {
Greg Claytona894fe72012-04-05 16:12:35 +0000207 case eStopReasonInvalid:
208 case eStopReasonNone:
209 case eStopReasonTrace:
Greg Clayton0bce9a22012-12-05 00:16:59 +0000210 case eStopReasonExec:
Greg Claytona894fe72012-04-05 16:12:35 +0000211 case eStopReasonPlanComplete:
212 // There is no data for these stop reasons.
213 return 0;
214
215 case eStopReasonBreakpoint:
Greg Clayton640dc6b2010-11-18 18:52:36 +0000216 {
Greg Claytona894fe72012-04-05 16:12:35 +0000217 break_id_t site_id = stop_info_sp->GetValue();
218 lldb::BreakpointSiteSP bp_site_sp (exe_ctx.GetProcessPtr()->GetBreakpointSiteList().FindByID (site_id));
219 if (bp_site_sp)
Greg Clayton640dc6b2010-11-18 18:52:36 +0000220 {
Greg Claytona894fe72012-04-05 16:12:35 +0000221 uint32_t bp_index = idx / 2;
222 BreakpointLocationSP bp_loc_sp (bp_site_sp->GetOwnerAtIndex (bp_index));
223 if (bp_loc_sp)
Greg Clayton640dc6b2010-11-18 18:52:36 +0000224 {
Greg Claytona894fe72012-04-05 16:12:35 +0000225 if (bp_index & 1)
226 {
227 // Odd idx, return the breakpoint location ID
228 return bp_loc_sp->GetID();
229 }
230 else
231 {
232 // Even idx, return the breakpoint ID
233 return bp_loc_sp->GetBreakpoint().GetID();
234 }
Greg Clayton640dc6b2010-11-18 18:52:36 +0000235 }
236 }
Greg Claytona894fe72012-04-05 16:12:35 +0000237 return LLDB_INVALID_BREAK_ID;
Greg Clayton640dc6b2010-11-18 18:52:36 +0000238 }
Greg Claytona894fe72012-04-05 16:12:35 +0000239 break;
240
241 case eStopReasonWatchpoint:
242 return stop_info_sp->GetValue();
243
244 case eStopReasonSignal:
245 return stop_info_sp->GetValue();
246
247 case eStopReasonException:
248 return stop_info_sp->GetValue();
Greg Clayton640dc6b2010-11-18 18:52:36 +0000249 }
Greg Clayton640dc6b2010-11-18 18:52:36 +0000250 }
251 }
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000252 else
253 {
254 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
255 if (log)
256 log->Printf ("SBThread(%p)::GetStopReasonDataAtIndex() => error: process is running", exe_ctx.GetThreadPtr());
257 }
Greg Clayton640dc6b2010-11-18 18:52:36 +0000258 }
259 return 0;
260}
261
262size_t
Chris Lattner24943d22010-06-08 16:52:24 +0000263SBThread::GetStopDescription (char *dst, size_t dst_len)
264{
Greg Claytone005f2c2010-11-06 01:53:30 +0000265 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000266
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000267 Mutex::Locker api_locker;
268 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
269
Greg Claytonf4124de2012-02-21 00:09:25 +0000270 if (exe_ctx.HasThreadScope())
Chris Lattner24943d22010-06-08 16:52:24 +0000271 {
Greg Claytona894fe72012-04-05 16:12:35 +0000272 Process::StopLocker stop_locker;
273 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
Chris Lattner24943d22010-06-08 16:52:24 +0000274 {
Greg Claytona894fe72012-04-05 16:12:35 +0000275
Greg Claytona894fe72012-04-05 16:12:35 +0000276 StopInfoSP stop_info_sp = exe_ctx.GetThreadPtr()->GetStopInfo ();
277 if (stop_info_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000278 {
Greg Claytona894fe72012-04-05 16:12:35 +0000279 const char *stop_desc = stop_info_sp->GetDescription();
280 if (stop_desc)
Chris Lattner24943d22010-06-08 16:52:24 +0000281 {
Caroline Tice7826c882010-10-26 03:11:13 +0000282 if (log)
Greg Claytona894fe72012-04-05 16:12:35 +0000283 log->Printf ("SBThread(%p)::GetStopDescription (dst, dst_len) => \"%s\"",
Greg Claytonf4124de2012-02-21 00:09:25 +0000284 exe_ctx.GetThreadPtr(), stop_desc);
Chris Lattner24943d22010-06-08 16:52:24 +0000285 if (dst)
Greg Claytona894fe72012-04-05 16:12:35 +0000286 return ::snprintf (dst, dst_len, "%s", stop_desc);
287 else
288 {
289 // NULL dst passed in, return the length needed to contain the description
290 return ::strlen (stop_desc) + 1; // Include the NULL byte for size
291 }
292 }
293 else
294 {
295 size_t stop_desc_len = 0;
296 switch (stop_info_sp->GetStopReason())
297 {
298 case eStopReasonTrace:
299 case eStopReasonPlanComplete:
300 {
301 static char trace_desc[] = "step";
302 stop_desc = trace_desc;
303 stop_desc_len = sizeof(trace_desc); // Include the NULL byte for size
304 }
305 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000306
Greg Claytona894fe72012-04-05 16:12:35 +0000307 case eStopReasonBreakpoint:
308 {
309 static char bp_desc[] = "breakpoint hit";
310 stop_desc = bp_desc;
311 stop_desc_len = sizeof(bp_desc); // Include the NULL byte for size
312 }
313 break;
314
315 case eStopReasonWatchpoint:
316 {
317 static char wp_desc[] = "watchpoint hit";
318 stop_desc = wp_desc;
319 stop_desc_len = sizeof(wp_desc); // Include the NULL byte for size
320 }
321 break;
322
323 case eStopReasonSignal:
324 {
325 stop_desc = exe_ctx.GetProcessPtr()->GetUnixSignals ().GetSignalAsCString (stop_info_sp->GetValue());
326 if (stop_desc == NULL || stop_desc[0] == '\0')
327 {
328 static char signal_desc[] = "signal";
329 stop_desc = signal_desc;
330 stop_desc_len = sizeof(signal_desc); // Include the NULL byte for size
331 }
332 }
333 break;
334
335 case eStopReasonException:
336 {
337 char exc_desc[] = "exception";
338 stop_desc = exc_desc;
339 stop_desc_len = sizeof(exc_desc); // Include the NULL byte for size
340 }
341 break;
342
Greg Clayton0bce9a22012-12-05 00:16:59 +0000343 case eStopReasonExec:
344 {
345 char exc_desc[] = "exec";
346 stop_desc = exc_desc;
347 stop_desc_len = sizeof(exc_desc); // Include the NULL byte for size
348 }
349 break;
350
Greg Claytona894fe72012-04-05 16:12:35 +0000351 default:
352 break;
353 }
354
355 if (stop_desc && stop_desc[0])
356 {
357 if (log)
358 log->Printf ("SBThread(%p)::GetStopDescription (dst, dst_len) => '%s'",
359 exe_ctx.GetThreadPtr(), stop_desc);
360
361 if (dst)
362 return ::snprintf (dst, dst_len, "%s", stop_desc) + 1; // Include the NULL byte
363
364 if (stop_desc_len == 0)
365 stop_desc_len = ::strlen (stop_desc) + 1; // Include the NULL byte
366
367 return stop_desc_len;
368 }
Chris Lattner24943d22010-06-08 16:52:24 +0000369 }
370 }
371 }
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000372 else
373 {
374 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
375 if (log)
376 log->Printf ("SBThread(%p)::GetStopDescription() => error: process is running", exe_ctx.GetThreadPtr());
377 }
Chris Lattner24943d22010-06-08 16:52:24 +0000378 }
379 if (dst)
380 *dst = 0;
381 return 0;
382}
383
Jim Ingham1586d972011-12-17 01:35:57 +0000384SBValue
385SBThread::GetStopReturnValue ()
386{
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000387 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Ingham1586d972011-12-17 01:35:57 +0000388 ValueObjectSP return_valobj_sp;
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000389 Mutex::Locker api_locker;
390 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
391
Greg Claytonf4124de2012-02-21 00:09:25 +0000392 if (exe_ctx.HasThreadScope())
Jim Ingham1586d972011-12-17 01:35:57 +0000393 {
Greg Claytona894fe72012-04-05 16:12:35 +0000394 Process::StopLocker stop_locker;
395 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
Jim Ingham1586d972011-12-17 01:35:57 +0000396 {
Greg Claytona894fe72012-04-05 16:12:35 +0000397 StopInfoSP stop_info_sp = exe_ctx.GetThreadPtr()->GetStopInfo ();
398 if (stop_info_sp)
399 {
400 return_valobj_sp = StopInfo::GetReturnValueObject (stop_info_sp);
401 }
Jim Ingham1586d972011-12-17 01:35:57 +0000402 }
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000403 else
404 {
405 if (log)
406 log->Printf ("SBThread(%p)::GetStopReturnValue() => error: process is running", exe_ctx.GetThreadPtr());
407 }
Jim Ingham1586d972011-12-17 01:35:57 +0000408 }
409
Jim Ingham1586d972011-12-17 01:35:57 +0000410 if (log)
Greg Claytonf4124de2012-02-21 00:09:25 +0000411 log->Printf ("SBThread(%p)::GetStopReturnValue () => %s", exe_ctx.GetThreadPtr(),
Jim Ingham1586d972011-12-17 01:35:57 +0000412 return_valobj_sp.get()
413 ? return_valobj_sp->GetValueAsCString()
414 : "<no return value>");
415
416 return SBValue (return_valobj_sp);
417}
418
Chris Lattner24943d22010-06-08 16:52:24 +0000419void
420SBThread::SetThread (const ThreadSP& lldb_object_sp)
421{
Greg Claytona894fe72012-04-05 16:12:35 +0000422 m_opaque_sp->SetThreadSP (lldb_object_sp);
Chris Lattner24943d22010-06-08 16:52:24 +0000423}
424
425
426lldb::tid_t
427SBThread::GetThreadID () const
428{
Greg Claytona894fe72012-04-05 16:12:35 +0000429 ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
Greg Clayton90c52142012-01-30 02:53:15 +0000430 if (thread_sp)
Greg Claytonf4124de2012-02-21 00:09:25 +0000431 return thread_sp->GetID();
432 return LLDB_INVALID_THREAD_ID;
Chris Lattner24943d22010-06-08 16:52:24 +0000433}
434
435uint32_t
436SBThread::GetIndexID () const
437{
Greg Claytona894fe72012-04-05 16:12:35 +0000438 ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
Greg Clayton90c52142012-01-30 02:53:15 +0000439 if (thread_sp)
440 return thread_sp->GetIndexID();
Chris Lattner24943d22010-06-08 16:52:24 +0000441 return LLDB_INVALID_INDEX32;
442}
Greg Claytonf4124de2012-02-21 00:09:25 +0000443
Chris Lattner24943d22010-06-08 16:52:24 +0000444const char *
445SBThread::GetName () const
446{
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000447 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytona66ba462010-10-30 04:51:46 +0000448 const char *name = NULL;
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000449 Mutex::Locker api_locker;
450 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
451
Greg Claytonf4124de2012-02-21 00:09:25 +0000452 if (exe_ctx.HasThreadScope())
Greg Claytonbdcda462010-12-20 20:49:23 +0000453 {
Greg Claytona894fe72012-04-05 16:12:35 +0000454 Process::StopLocker stop_locker;
455 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
456 {
Greg Claytona894fe72012-04-05 16:12:35 +0000457 name = exe_ctx.GetThreadPtr()->GetName();
458 }
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000459 else
460 {
461 if (log)
462 log->Printf ("SBThread(%p)::GetName() => error: process is running", exe_ctx.GetThreadPtr());
463 }
Greg Claytonbdcda462010-12-20 20:49:23 +0000464 }
Greg Claytona66ba462010-10-30 04:51:46 +0000465
Caroline Tice7826c882010-10-26 03:11:13 +0000466 if (log)
Greg Claytonf4124de2012-02-21 00:09:25 +0000467 log->Printf ("SBThread(%p)::GetName () => %s", exe_ctx.GetThreadPtr(), name ? name : "NULL");
Caroline Tice7826c882010-10-26 03:11:13 +0000468
Greg Claytona66ba462010-10-30 04:51:46 +0000469 return name;
Chris Lattner24943d22010-06-08 16:52:24 +0000470}
471
472const char *
473SBThread::GetQueueName () const
474{
Greg Claytona66ba462010-10-30 04:51:46 +0000475 const char *name = NULL;
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000476 Mutex::Locker api_locker;
477 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
478
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000479 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytonf4124de2012-02-21 00:09:25 +0000480 if (exe_ctx.HasThreadScope())
Greg Claytonbdcda462010-12-20 20:49:23 +0000481 {
Greg Claytona894fe72012-04-05 16:12:35 +0000482 Process::StopLocker stop_locker;
483 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
484 {
Greg Claytona894fe72012-04-05 16:12:35 +0000485 name = exe_ctx.GetThreadPtr()->GetQueueName();
486 }
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000487 else
488 {
489 if (log)
490 log->Printf ("SBThread(%p)::GetQueueName() => error: process is running", exe_ctx.GetThreadPtr());
491 }
Greg Claytonbdcda462010-12-20 20:49:23 +0000492 }
Greg Claytona66ba462010-10-30 04:51:46 +0000493
Caroline Tice7826c882010-10-26 03:11:13 +0000494 if (log)
Greg Claytonf4124de2012-02-21 00:09:25 +0000495 log->Printf ("SBThread(%p)::GetQueueName () => %s", exe_ctx.GetThreadPtr(), name ? name : "NULL");
Caroline Tice7826c882010-10-26 03:11:13 +0000496
Greg Claytona66ba462010-10-30 04:51:46 +0000497 return name;
Chris Lattner24943d22010-06-08 16:52:24 +0000498}
499
Jim Ingham88e3de22012-05-03 21:19:36 +0000500SBError
501SBThread::ResumeNewPlan (ExecutionContext &exe_ctx, ThreadPlan *new_plan)
502{
503 SBError sb_error;
504
505 Process *process = exe_ctx.GetProcessPtr();
506 if (!process)
507 {
508 sb_error.SetErrorString("No process in SBThread::ResumeNewPlan");
509 return sb_error;
510 }
511
512 Thread *thread = exe_ctx.GetThreadPtr();
513 if (!thread)
514 {
515 sb_error.SetErrorString("No thread in SBThread::ResumeNewPlan");
516 return sb_error;
517 }
518
519 // User level plans should be Master Plans so they can be interrupted, other plans executed, and
520 // then a "continue" will resume the plan.
521 if (new_plan != NULL)
522 {
523 new_plan->SetIsMasterPlan(true);
524 new_plan->SetOkayToDiscard(false);
525 }
526
527 // Why do we need to set the current thread by ID here???
528 process->GetThreadList().SetSelectedThreadByID (thread->GetID());
529 sb_error.ref() = process->Resume();
530
531 if (sb_error.Success())
532 {
533 // If we are doing synchronous mode, then wait for the
534 // process to stop yet again!
535 if (process->GetTarget().GetDebugger().GetAsyncExecution () == false)
536 process->WaitForProcessToStop (NULL);
537 }
538
539 return sb_error;
540}
Chris Lattner24943d22010-06-08 16:52:24 +0000541
542void
Chris Lattner24943d22010-06-08 16:52:24 +0000543SBThread::StepOver (lldb::RunMode stop_other_threads)
544{
Greg Claytone005f2c2010-11-06 01:53:30 +0000545 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000546
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000547 Mutex::Locker api_locker;
548 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
549
Caroline Tice7826c882010-10-26 03:11:13 +0000550
Greg Clayton90c52142012-01-30 02:53:15 +0000551 if (log)
Greg Claytonf4124de2012-02-21 00:09:25 +0000552 log->Printf ("SBThread(%p)::StepOver (stop_other_threads='%s')", exe_ctx.GetThreadPtr(),
Greg Clayton90c52142012-01-30 02:53:15 +0000553 Thread::RunModeAsCString (stop_other_threads));
554
Greg Claytonf4124de2012-02-21 00:09:25 +0000555 if (exe_ctx.HasThreadScope())
Chris Lattner24943d22010-06-08 16:52:24 +0000556 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000557 Thread *thread = exe_ctx.GetThreadPtr();
Jim Inghamd82bc6d2012-05-11 23:47:32 +0000558 bool abort_other_plans = false;
Greg Claytonf4124de2012-02-21 00:09:25 +0000559 StackFrameSP frame_sp(thread->GetStackFrameAtIndex (0));
Jim Ingham88e3de22012-05-03 21:19:36 +0000560 ThreadPlan *new_plan = NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000561
562 if (frame_sp)
563 {
564 if (frame_sp->HasDebugInformation ())
565 {
566 SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
Jim Ingham88e3de22012-05-03 21:19:36 +0000567 new_plan = thread->QueueThreadPlanForStepRange (abort_other_plans,
568 eStepTypeOver,
569 sc.line_entry.range,
570 sc,
571 stop_other_threads,
572 false);
Chris Lattner24943d22010-06-08 16:52:24 +0000573
574 }
575 else
576 {
Jim Ingham88e3de22012-05-03 21:19:36 +0000577 new_plan = thread->QueueThreadPlanForStepSingleInstruction (true,
578 abort_other_plans,
579 stop_other_threads);
Chris Lattner24943d22010-06-08 16:52:24 +0000580 }
581 }
582
Jim Ingham88e3de22012-05-03 21:19:36 +0000583 // This returns an error, we should use it!
584 ResumeNewPlan (exe_ctx, new_plan);
Chris Lattner24943d22010-06-08 16:52:24 +0000585 }
586}
587
588void
589SBThread::StepInto (lldb::RunMode stop_other_threads)
590{
Greg Claytone005f2c2010-11-06 01:53:30 +0000591 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000592
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000593 Mutex::Locker api_locker;
594 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
Greg Clayton90c52142012-01-30 02:53:15 +0000595
596 if (log)
Greg Claytonf4124de2012-02-21 00:09:25 +0000597 log->Printf ("SBThread(%p)::StepInto (stop_other_threads='%s')", exe_ctx.GetThreadPtr(),
Greg Clayton90c52142012-01-30 02:53:15 +0000598 Thread::RunModeAsCString (stop_other_threads));
Greg Claytonf4124de2012-02-21 00:09:25 +0000599 if (exe_ctx.HasThreadScope())
Chris Lattner24943d22010-06-08 16:52:24 +0000600 {
Jim Inghamd82bc6d2012-05-11 23:47:32 +0000601 bool abort_other_plans = false;
Chris Lattner24943d22010-06-08 16:52:24 +0000602
Greg Claytonf4124de2012-02-21 00:09:25 +0000603 Thread *thread = exe_ctx.GetThreadPtr();
604 StackFrameSP frame_sp(thread->GetStackFrameAtIndex (0));
Jim Ingham88e3de22012-05-03 21:19:36 +0000605 ThreadPlan *new_plan = NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000606
607 if (frame_sp && frame_sp->HasDebugInformation ())
608 {
Greg Clayton8f5fd6b2010-06-12 18:59:55 +0000609 bool avoid_code_without_debug_info = true;
Chris Lattner24943d22010-06-08 16:52:24 +0000610 SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
Jim Ingham88e3de22012-05-03 21:19:36 +0000611 new_plan = thread->QueueThreadPlanForStepRange (abort_other_plans,
612 eStepTypeInto,
613 sc.line_entry.range,
614 sc,
615 stop_other_threads,
616 avoid_code_without_debug_info);
Chris Lattner24943d22010-06-08 16:52:24 +0000617 }
618 else
619 {
Jim Ingham88e3de22012-05-03 21:19:36 +0000620 new_plan = thread->QueueThreadPlanForStepSingleInstruction (false,
621 abort_other_plans,
622 stop_other_threads);
Chris Lattner24943d22010-06-08 16:52:24 +0000623 }
Jim Ingham88e3de22012-05-03 21:19:36 +0000624
625 // This returns an error, we should use it!
626 ResumeNewPlan (exe_ctx, new_plan);
Chris Lattner24943d22010-06-08 16:52:24 +0000627 }
628}
629
630void
631SBThread::StepOut ()
632{
Greg Claytone005f2c2010-11-06 01:53:30 +0000633 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000634
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000635 Mutex::Locker api_locker;
636 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
637
Caroline Tice7826c882010-10-26 03:11:13 +0000638
Greg Clayton90c52142012-01-30 02:53:15 +0000639 if (log)
Greg Claytonf4124de2012-02-21 00:09:25 +0000640 log->Printf ("SBThread(%p)::StepOut ()", exe_ctx.GetThreadPtr());
Greg Clayton90c52142012-01-30 02:53:15 +0000641
Greg Claytonf4124de2012-02-21 00:09:25 +0000642 if (exe_ctx.HasThreadScope())
Chris Lattner24943d22010-06-08 16:52:24 +0000643 {
Jim Inghamd82bc6d2012-05-11 23:47:32 +0000644 bool abort_other_plans = false;
Jim Inghambe0cde92012-09-14 21:07:14 +0000645 bool stop_other_threads = false;
Chris Lattner24943d22010-06-08 16:52:24 +0000646
Greg Claytonf4124de2012-02-21 00:09:25 +0000647 Thread *thread = exe_ctx.GetThreadPtr();
648
Jim Ingham88e3de22012-05-03 21:19:36 +0000649 ThreadPlan *new_plan = thread->QueueThreadPlanForStepOut (abort_other_plans,
650 NULL,
651 false,
652 stop_other_threads,
653 eVoteYes,
654 eVoteNoOpinion,
655 0);
656
657 // This returns an error, we should use it!
658 ResumeNewPlan (exe_ctx, new_plan);
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000659 }
660}
Chris Lattner24943d22010-06-08 16:52:24 +0000661
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000662void
663SBThread::StepOutOfFrame (lldb::SBFrame &sb_frame)
664{
665 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
666
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000667 Mutex::Locker api_locker;
668 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
669
Greg Clayton334d33a2012-01-30 07:41:31 +0000670 StackFrameSP frame_sp (sb_frame.GetFrameSP());
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000671 if (log)
672 {
673 SBStream frame_desc_strm;
674 sb_frame.GetDescription (frame_desc_strm);
Greg Claytonf4124de2012-02-21 00:09:25 +0000675 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 +0000676 }
677
Greg Claytonf4124de2012-02-21 00:09:25 +0000678 if (exe_ctx.HasThreadScope())
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000679 {
Jim Inghamd82bc6d2012-05-11 23:47:32 +0000680 bool abort_other_plans = false;
Jim Inghambe0cde92012-09-14 21:07:14 +0000681 bool stop_other_threads = false;
Greg Claytonf4124de2012-02-21 00:09:25 +0000682 Thread *thread = exe_ctx.GetThreadPtr();
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000683
Jim Ingham88e3de22012-05-03 21:19:36 +0000684 ThreadPlan *new_plan = thread->QueueThreadPlanForStepOut (abort_other_plans,
685 NULL,
686 false,
687 stop_other_threads,
688 eVoteYes,
689 eVoteNoOpinion,
690 frame_sp->GetFrameIndex());
691
692 // This returns an error, we should use it!
693 ResumeNewPlan (exe_ctx, new_plan);
Chris Lattner24943d22010-06-08 16:52:24 +0000694 }
695}
696
697void
698SBThread::StepInstruction (bool step_over)
699{
Greg Claytone005f2c2010-11-06 01:53:30 +0000700 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000701
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000702 Mutex::Locker api_locker;
703 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
704
Greg Claytonf4124de2012-02-21 00:09:25 +0000705
Caroline Tice7826c882010-10-26 03:11:13 +0000706
Greg Clayton90c52142012-01-30 02:53:15 +0000707 if (log)
Greg Claytonf4124de2012-02-21 00:09:25 +0000708 log->Printf ("SBThread(%p)::StepInstruction (step_over=%i)", exe_ctx.GetThreadPtr(), step_over);
Greg Clayton90c52142012-01-30 02:53:15 +0000709
Greg Claytonf4124de2012-02-21 00:09:25 +0000710 if (exe_ctx.HasThreadScope())
Chris Lattner24943d22010-06-08 16:52:24 +0000711 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000712 Thread *thread = exe_ctx.GetThreadPtr();
Jim Ingham88e3de22012-05-03 21:19:36 +0000713 ThreadPlan *new_plan = thread->QueueThreadPlanForStepSingleInstruction (step_over, true, true);
714
715 // This returns an error, we should use it!
716 ResumeNewPlan (exe_ctx, new_plan);
Chris Lattner24943d22010-06-08 16:52:24 +0000717 }
718}
719
720void
721SBThread::RunToAddress (lldb::addr_t addr)
722{
Greg Claytone005f2c2010-11-06 01:53:30 +0000723 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000724
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000725 Mutex::Locker api_locker;
726 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
727
Caroline Tice7826c882010-10-26 03:11:13 +0000728
Greg Clayton90c52142012-01-30 02:53:15 +0000729 if (log)
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000730 log->Printf ("SBThread(%p)::RunToAddress (addr=0x%" PRIx64 ")", exe_ctx.GetThreadPtr(), addr);
Greg Clayton90c52142012-01-30 02:53:15 +0000731
Greg Claytonf4124de2012-02-21 00:09:25 +0000732 if (exe_ctx.HasThreadScope())
Chris Lattner24943d22010-06-08 16:52:24 +0000733 {
Jim Inghamd82bc6d2012-05-11 23:47:32 +0000734 bool abort_other_plans = false;
Chris Lattner24943d22010-06-08 16:52:24 +0000735 bool stop_other_threads = true;
736
Greg Clayton3508c382012-02-24 01:59:29 +0000737 Address target_addr (addr);
Chris Lattner24943d22010-06-08 16:52:24 +0000738
Greg Claytonf4124de2012-02-21 00:09:25 +0000739 Thread *thread = exe_ctx.GetThreadPtr();
Greg Claytonf4124de2012-02-21 00:09:25 +0000740
Jim Ingham88e3de22012-05-03 21:19:36 +0000741 ThreadPlan *new_plan = thread->QueueThreadPlanForRunToAddress (abort_other_plans, target_addr, stop_other_threads);
742
743 // This returns an error, we should use it!
744 ResumeNewPlan (exe_ctx, new_plan);
Chris Lattner24943d22010-06-08 16:52:24 +0000745 }
Chris Lattner24943d22010-06-08 16:52:24 +0000746}
747
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000748SBError
749SBThread::StepOverUntil (lldb::SBFrame &sb_frame,
750 lldb::SBFileSpec &sb_file_spec,
751 uint32_t line)
752{
753 SBError sb_error;
754 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
755 char path[PATH_MAX];
Greg Clayton90c52142012-01-30 02:53:15 +0000756
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000757 Mutex::Locker api_locker;
758 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
759
Greg Clayton334d33a2012-01-30 07:41:31 +0000760 StackFrameSP frame_sp (sb_frame.GetFrameSP());
761
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000762 if (log)
763 {
764 SBStream frame_desc_strm;
765 sb_frame.GetDescription (frame_desc_strm);
766 sb_file_spec->GetPath (path, sizeof(path));
767 log->Printf ("SBThread(%p)::StepOverUntil (frame = SBFrame(%p): %s, file+line = %s:%u)",
Greg Claytonf4124de2012-02-21 00:09:25 +0000768 exe_ctx.GetThreadPtr(),
Greg Clayton334d33a2012-01-30 07:41:31 +0000769 frame_sp.get(),
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000770 frame_desc_strm.GetData(),
771 path, line);
772 }
Greg Clayton90c52142012-01-30 02:53:15 +0000773
Greg Claytonf4124de2012-02-21 00:09:25 +0000774 if (exe_ctx.HasThreadScope())
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000775 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000776 Target *target = exe_ctx.GetTargetPtr();
Greg Claytonf4124de2012-02-21 00:09:25 +0000777 Thread *thread = exe_ctx.GetThreadPtr();
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000778
779 if (line == 0)
780 {
781 sb_error.SetErrorString("invalid line argument");
782 return sb_error;
783 }
784
Greg Clayton334d33a2012-01-30 07:41:31 +0000785 if (!frame_sp)
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000786 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000787 frame_sp = thread->GetSelectedFrame ();
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000788 if (!frame_sp)
Greg Claytonf4124de2012-02-21 00:09:25 +0000789 frame_sp = thread->GetStackFrameAtIndex (0);
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000790 }
791
792 SymbolContext frame_sc;
793 if (!frame_sp)
794 {
795 sb_error.SetErrorString("no valid frames in thread to step");
796 return sb_error;
797 }
798
799 // If we have a frame, get its line
800 frame_sc = frame_sp->GetSymbolContext (eSymbolContextCompUnit |
801 eSymbolContextFunction |
802 eSymbolContextLineEntry |
803 eSymbolContextSymbol );
804
805 if (frame_sc.comp_unit == NULL)
806 {
807 sb_error.SetErrorStringWithFormat("frame %u doesn't have debug information", frame_sp->GetFrameIndex());
808 return sb_error;
809 }
810
811 FileSpec step_file_spec;
812 if (sb_file_spec.IsValid())
813 {
814 // The file spec passed in was valid, so use it
815 step_file_spec = sb_file_spec.ref();
816 }
817 else
818 {
819 if (frame_sc.line_entry.IsValid())
820 step_file_spec = frame_sc.line_entry.file;
821 else
822 {
823 sb_error.SetErrorString("invalid file argument or no file for frame");
824 return sb_error;
825 }
826 }
827
Jim Inghamb07c62a2011-05-08 00:56:32 +0000828 // Grab the current function, then we will make sure the "until" address is
829 // within the function. We discard addresses that are out of the current
830 // function, and then if there are no addresses remaining, give an appropriate
831 // error message.
832
833 bool all_in_function = true;
834 AddressRange fun_range = frame_sc.function->GetAddressRange();
835
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000836 std::vector<addr_t> step_over_until_addrs;
Jim Inghamd82bc6d2012-05-11 23:47:32 +0000837 const bool abort_other_plans = false;
Jim Ingham52124e72012-09-14 18:57:14 +0000838 const bool stop_other_threads = false;
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000839 const bool check_inlines = true;
840 const bool exact = false;
841
842 SymbolContextList sc_list;
Jim Inghamb07c62a2011-05-08 00:56:32 +0000843 const uint32_t num_matches = frame_sc.comp_unit->ResolveSymbolContext (step_file_spec,
844 line,
845 check_inlines,
846 exact,
847 eSymbolContextLineEntry,
848 sc_list);
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000849 if (num_matches > 0)
850 {
851 SymbolContext sc;
852 for (uint32_t i=0; i<num_matches; ++i)
853 {
854 if (sc_list.GetContextAtIndex(i, sc))
855 {
Jim Inghamb07c62a2011-05-08 00:56:32 +0000856 addr_t step_addr = sc.line_entry.range.GetBaseAddress().GetLoadAddress(target);
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000857 if (step_addr != LLDB_INVALID_ADDRESS)
858 {
Jim Inghamb07c62a2011-05-08 00:56:32 +0000859 if (fun_range.ContainsLoadAddress(step_addr, target))
860 step_over_until_addrs.push_back(step_addr);
861 else
862 all_in_function = false;
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000863 }
864 }
865 }
866 }
Jim Inghamb07c62a2011-05-08 00:56:32 +0000867
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000868 if (step_over_until_addrs.empty())
869 {
Jim Inghamb07c62a2011-05-08 00:56:32 +0000870 if (all_in_function)
871 {
872 step_file_spec.GetPath (path, sizeof(path));
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000873 sb_error.SetErrorStringWithFormat("No line entries for %s:%u", path, line);
Jim Inghamb07c62a2011-05-08 00:56:32 +0000874 }
875 else
Greg Clayton9c236732011-10-26 00:56:27 +0000876 sb_error.SetErrorString ("step until target not in current function");
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000877 }
878 else
879 {
Jim Ingham88e3de22012-05-03 21:19:36 +0000880 ThreadPlan *new_plan = thread->QueueThreadPlanForStepUntil (abort_other_plans,
881 &step_over_until_addrs[0],
882 step_over_until_addrs.size(),
883 stop_other_threads,
884 frame_sp->GetFrameIndex());
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000885
Jim Ingham88e3de22012-05-03 21:19:36 +0000886 sb_error = ResumeNewPlan (exe_ctx, new_plan);
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000887 }
888 }
889 else
890 {
891 sb_error.SetErrorString("this SBThread object is invalid");
892 }
893 return sb_error;
894}
895
Jim Inghama17a81a2012-09-12 00:40:39 +0000896SBError
Jim Inghamf59388a2012-09-14 02:14:15 +0000897SBThread::ReturnFromFrame (SBFrame &frame, SBValue &return_value)
Jim Inghama17a81a2012-09-12 00:40:39 +0000898{
899 SBError sb_error;
900
901 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
902
903 Mutex::Locker api_locker;
904 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
905
906
907 if (log)
Jim Inghamf59388a2012-09-14 02:14:15 +0000908 log->Printf ("SBThread(%p)::ReturnFromFrame (frame=%d)", exe_ctx.GetThreadPtr(), frame.GetFrameID());
Jim Inghama17a81a2012-09-12 00:40:39 +0000909
910 if (exe_ctx.HasThreadScope())
911 {
912 Thread *thread = exe_ctx.GetThreadPtr();
Jim Inghamf59388a2012-09-14 02:14:15 +0000913 sb_error.SetError (thread->ReturnFromFrame(frame.GetFrameSP(), return_value.GetSP()));
Jim Inghama17a81a2012-09-12 00:40:39 +0000914 }
915
916 return sb_error;
917}
918
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000919
Greg Clayton123db402011-01-12 02:25:42 +0000920bool
921SBThread::Suspend()
922{
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000923 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytona894fe72012-04-05 16:12:35 +0000924 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000925 bool result = false;
Greg Claytonf4124de2012-02-21 00:09:25 +0000926 if (exe_ctx.HasThreadScope())
Greg Clayton123db402011-01-12 02:25:42 +0000927 {
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000928 Process::StopLocker stop_locker;
929 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
930 {
931 exe_ctx.GetThreadPtr()->SetResumeState (eStateSuspended);
932 result = true;
933 }
934 else
935 {
936 if (log)
937 log->Printf ("SBThread(%p)::Suspend() => error: process is running", exe_ctx.GetThreadPtr());
938 }
Greg Clayton123db402011-01-12 02:25:42 +0000939 }
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000940 if (log)
941 log->Printf ("SBThread(%p)::Suspend() => %i", exe_ctx.GetThreadPtr(), result);
942 return result;
Greg Clayton123db402011-01-12 02:25:42 +0000943}
944
945bool
946SBThread::Resume ()
947{
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000948 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytona894fe72012-04-05 16:12:35 +0000949 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000950 bool result = false;
Greg Claytonf4124de2012-02-21 00:09:25 +0000951 if (exe_ctx.HasThreadScope())
Greg Clayton123db402011-01-12 02:25:42 +0000952 {
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000953 Process::StopLocker stop_locker;
954 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
955 {
956 exe_ctx.GetThreadPtr()->SetResumeState (eStateRunning);
957 result = true;
958 }
959 else
960 {
961 if (log)
962 log->Printf ("SBThread(%p)::Resume() => error: process is running", exe_ctx.GetThreadPtr());
963 }
Greg Clayton123db402011-01-12 02:25:42 +0000964 }
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000965 if (log)
966 log->Printf ("SBThread(%p)::Resume() => %i", exe_ctx.GetThreadPtr(), result);
967 return result;
Greg Clayton123db402011-01-12 02:25:42 +0000968}
969
970bool
971SBThread::IsSuspended()
972{
Greg Claytona894fe72012-04-05 16:12:35 +0000973 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Claytonf4124de2012-02-21 00:09:25 +0000974 if (exe_ctx.HasThreadScope())
975 return exe_ctx.GetThreadPtr()->GetResumeState () == eStateSuspended;
Greg Clayton123db402011-01-12 02:25:42 +0000976 return false;
977}
978
Chris Lattner24943d22010-06-08 16:52:24 +0000979SBProcess
980SBThread::GetProcess ()
981{
Caroline Tice7826c882010-10-26 03:11:13 +0000982
Greg Clayton334d33a2012-01-30 07:41:31 +0000983 SBProcess sb_process;
984 ProcessSP process_sp;
Greg Claytona894fe72012-04-05 16:12:35 +0000985 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Claytonf4124de2012-02-21 00:09:25 +0000986 if (exe_ctx.HasThreadScope())
Chris Lattner24943d22010-06-08 16:52:24 +0000987 {
988 // Have to go up to the target so we can get a shared pointer to our process...
Greg Claytonf4124de2012-02-21 00:09:25 +0000989 sb_process.SetSP (exe_ctx.GetProcessSP());
Chris Lattner24943d22010-06-08 16:52:24 +0000990 }
Caroline Tice7826c882010-10-26 03:11:13 +0000991
Greg Claytone005f2c2010-11-06 01:53:30 +0000992 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000993 if (log)
994 {
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000995 SBStream frame_desc_strm;
Greg Clayton334d33a2012-01-30 07:41:31 +0000996 sb_process.GetDescription (frame_desc_strm);
Greg Claytonf4124de2012-02-21 00:09:25 +0000997 log->Printf ("SBThread(%p)::GetProcess () => SBProcess(%p): %s", exe_ctx.GetThreadPtr(),
Greg Clayton334d33a2012-01-30 07:41:31 +0000998 process_sp.get(), frame_desc_strm.GetData());
Caroline Tice7826c882010-10-26 03:11:13 +0000999 }
1000
Greg Clayton334d33a2012-01-30 07:41:31 +00001001 return sb_process;
Chris Lattner24943d22010-06-08 16:52:24 +00001002}
1003
1004uint32_t
1005SBThread::GetNumFrames ()
1006{
Greg Claytone005f2c2010-11-06 01:53:30 +00001007 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +00001008
Caroline Tice7826c882010-10-26 03:11:13 +00001009 uint32_t num_frames = 0;
Jim Inghamd0bdddf2012-08-22 21:34:33 +00001010 Mutex::Locker api_locker;
1011 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1012
Greg Claytonf4124de2012-02-21 00:09:25 +00001013 if (exe_ctx.HasThreadScope())
Greg Claytonbdcda462010-12-20 20:49:23 +00001014 {
Greg Claytona894fe72012-04-05 16:12:35 +00001015 Process::StopLocker stop_locker;
1016 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1017 {
Greg Claytona894fe72012-04-05 16:12:35 +00001018 num_frames = exe_ctx.GetThreadPtr()->GetStackFrameCount();
1019 }
Greg Clayton9f3c98e2012-04-06 02:17:47 +00001020 else
1021 {
1022 if (log)
1023 log->Printf ("SBThread(%p)::GetNumFrames() => error: process is running", exe_ctx.GetThreadPtr());
1024 }
Greg Claytonbdcda462010-12-20 20:49:23 +00001025 }
Caroline Tice7826c882010-10-26 03:11:13 +00001026
1027 if (log)
Greg Claytonf4124de2012-02-21 00:09:25 +00001028 log->Printf ("SBThread(%p)::GetNumFrames () => %u", exe_ctx.GetThreadPtr(), num_frames);
Caroline Tice7826c882010-10-26 03:11:13 +00001029
1030 return num_frames;
Chris Lattner24943d22010-06-08 16:52:24 +00001031}
1032
1033SBFrame
1034SBThread::GetFrameAtIndex (uint32_t idx)
1035{
Greg Claytone005f2c2010-11-06 01:53:30 +00001036 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +00001037
Chris Lattner24943d22010-06-08 16:52:24 +00001038 SBFrame sb_frame;
Greg Clayton334d33a2012-01-30 07:41:31 +00001039 StackFrameSP frame_sp;
Jim Inghamd0bdddf2012-08-22 21:34:33 +00001040 Mutex::Locker api_locker;
1041 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1042
Greg Claytonf4124de2012-02-21 00:09:25 +00001043 if (exe_ctx.HasThreadScope())
Greg Claytonbdcda462010-12-20 20:49:23 +00001044 {
Greg Claytona894fe72012-04-05 16:12:35 +00001045 Process::StopLocker stop_locker;
1046 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1047 {
Greg Claytona894fe72012-04-05 16:12:35 +00001048 frame_sp = exe_ctx.GetThreadPtr()->GetStackFrameAtIndex (idx);
1049 sb_frame.SetFrameSP (frame_sp);
1050 }
Greg Clayton9f3c98e2012-04-06 02:17:47 +00001051 else
1052 {
1053 if (log)
1054 log->Printf ("SBThread(%p)::GetFrameAtIndex() => error: process is running", exe_ctx.GetThreadPtr());
1055 }
Greg Claytonbdcda462010-12-20 20:49:23 +00001056 }
Caroline Tice7826c882010-10-26 03:11:13 +00001057
1058 if (log)
1059 {
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001060 SBStream frame_desc_strm;
1061 sb_frame.GetDescription (frame_desc_strm);
Greg Claytona66ba462010-10-30 04:51:46 +00001062 log->Printf ("SBThread(%p)::GetFrameAtIndex (idx=%d) => SBFrame(%p): %s",
Greg Claytonf4124de2012-02-21 00:09:25 +00001063 exe_ctx.GetThreadPtr(), idx, frame_sp.get(), frame_desc_strm.GetData());
Caroline Tice7826c882010-10-26 03:11:13 +00001064 }
1065
Chris Lattner24943d22010-06-08 16:52:24 +00001066 return sb_frame;
1067}
1068
Greg Claytonc5157ec2010-12-17 02:26:24 +00001069lldb::SBFrame
1070SBThread::GetSelectedFrame ()
1071{
1072 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1073
1074 SBFrame sb_frame;
Greg Clayton334d33a2012-01-30 07:41:31 +00001075 StackFrameSP frame_sp;
Jim Inghamd0bdddf2012-08-22 21:34:33 +00001076 Mutex::Locker api_locker;
1077 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1078
Greg Claytonf4124de2012-02-21 00:09:25 +00001079 if (exe_ctx.HasThreadScope())
Greg Claytonbdcda462010-12-20 20:49:23 +00001080 {
Greg Claytona894fe72012-04-05 16:12:35 +00001081 Process::StopLocker stop_locker;
1082 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1083 {
Greg Claytona894fe72012-04-05 16:12:35 +00001084 frame_sp = exe_ctx.GetThreadPtr()->GetSelectedFrame ();
1085 sb_frame.SetFrameSP (frame_sp);
1086 }
Greg Clayton9f3c98e2012-04-06 02:17:47 +00001087 else
1088 {
1089 if (log)
1090 log->Printf ("SBThread(%p)::GetSelectedFrame() => error: process is running", exe_ctx.GetThreadPtr());
1091 }
Greg Claytonbdcda462010-12-20 20:49:23 +00001092 }
Greg Claytonc5157ec2010-12-17 02:26:24 +00001093
1094 if (log)
1095 {
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001096 SBStream frame_desc_strm;
1097 sb_frame.GetDescription (frame_desc_strm);
Greg Claytonc5157ec2010-12-17 02:26:24 +00001098 log->Printf ("SBThread(%p)::GetSelectedFrame () => SBFrame(%p): %s",
Greg Claytonf4124de2012-02-21 00:09:25 +00001099 exe_ctx.GetThreadPtr(), frame_sp.get(), frame_desc_strm.GetData());
Greg Claytonc5157ec2010-12-17 02:26:24 +00001100 }
1101
1102 return sb_frame;
1103}
1104
1105lldb::SBFrame
1106SBThread::SetSelectedFrame (uint32_t idx)
1107{
1108 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1109
1110 SBFrame sb_frame;
Greg Clayton334d33a2012-01-30 07:41:31 +00001111 StackFrameSP frame_sp;
Jim Inghamd0bdddf2012-08-22 21:34:33 +00001112 Mutex::Locker api_locker;
1113 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1114
Greg Claytonf4124de2012-02-21 00:09:25 +00001115 if (exe_ctx.HasThreadScope())
Greg Claytonc5157ec2010-12-17 02:26:24 +00001116 {
Greg Claytona894fe72012-04-05 16:12:35 +00001117 Process::StopLocker stop_locker;
1118 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
Greg Claytonc5157ec2010-12-17 02:26:24 +00001119 {
Greg Claytona894fe72012-04-05 16:12:35 +00001120 Thread *thread = exe_ctx.GetThreadPtr();
1121 frame_sp = thread->GetStackFrameAtIndex (idx);
1122 if (frame_sp)
1123 {
1124 thread->SetSelectedFrame (frame_sp.get());
1125 sb_frame.SetFrameSP (frame_sp);
1126 }
Greg Claytonc5157ec2010-12-17 02:26:24 +00001127 }
Greg Clayton9f3c98e2012-04-06 02:17:47 +00001128 else
1129 {
1130 if (log)
1131 log->Printf ("SBThread(%p)::SetSelectedFrame() => error: process is running", exe_ctx.GetThreadPtr());
1132 }
Greg Claytonc5157ec2010-12-17 02:26:24 +00001133 }
1134
1135 if (log)
1136 {
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001137 SBStream frame_desc_strm;
1138 sb_frame.GetDescription (frame_desc_strm);
Greg Claytonc5157ec2010-12-17 02:26:24 +00001139 log->Printf ("SBThread(%p)::SetSelectedFrame (idx=%u) => SBFrame(%p): %s",
Greg Claytonf4124de2012-02-21 00:09:25 +00001140 exe_ctx.GetThreadPtr(), idx, frame_sp.get(), frame_desc_strm.GetData());
Greg Claytonc5157ec2010-12-17 02:26:24 +00001141 }
1142 return sb_frame;
1143}
1144
Jim Ingham94a5d0d2012-10-10 18:32:14 +00001145bool
1146SBThread::EventIsThreadEvent (const SBEvent &event)
1147{
1148 return Thread::ThreadEventData::GetEventDataFromEvent(event.get()) != NULL;
1149}
1150
1151SBFrame
1152SBThread::GetStackFrameFromEvent (const SBEvent &event)
1153{
1154 return Thread::ThreadEventData::GetStackFrameFromEvent (event.get());
1155
1156}
1157
1158SBThread
1159SBThread::GetThreadFromEvent (const SBEvent &event)
1160{
1161 return Thread::ThreadEventData::GetThreadFromEvent (event.get());
1162}
Greg Claytonc5157ec2010-12-17 02:26:24 +00001163
Chris Lattner24943d22010-06-08 16:52:24 +00001164bool
1165SBThread::operator == (const SBThread &rhs) const
1166{
Greg Claytona894fe72012-04-05 16:12:35 +00001167 return m_opaque_sp->GetThreadSP().get() == rhs.m_opaque_sp->GetThreadSP().get();
Chris Lattner24943d22010-06-08 16:52:24 +00001168}
1169
1170bool
1171SBThread::operator != (const SBThread &rhs) const
1172{
Greg Claytona894fe72012-04-05 16:12:35 +00001173 return m_opaque_sp->GetThreadSP().get() != rhs.m_opaque_sp->GetThreadSP().get();
Chris Lattner24943d22010-06-08 16:52:24 +00001174}
Caroline Tice98f930f2010-09-20 05:20:02 +00001175
1176bool
Jim Ingham94a5d0d2012-10-10 18:32:14 +00001177SBThread::GetStatus (SBStream &status) const
1178{
1179 Stream &strm = status.ref();
1180
1181 ExecutionContext exe_ctx (m_opaque_sp.get());
1182 if (exe_ctx.HasThreadScope())
1183 {
1184 exe_ctx.GetThreadPtr()->GetStatus(strm, 0, 1, 1);
1185 }
1186 else
1187 strm.PutCString ("No status");
1188
1189 return true;
1190}
1191
1192bool
Caroline Tice7826c882010-10-26 03:11:13 +00001193SBThread::GetDescription (SBStream &description) const
1194{
Greg Clayton96154be2011-11-13 06:57:31 +00001195 Stream &strm = description.ref();
1196
Greg Claytona894fe72012-04-05 16:12:35 +00001197 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Claytonf4124de2012-02-21 00:09:25 +00001198 if (exe_ctx.HasThreadScope())
Caroline Tice7826c882010-10-26 03:11:13 +00001199 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001200 strm.Printf("SBThread: tid = 0x%4.4" PRIx64, exe_ctx.GetThreadPtr()->GetID());
Caroline Tice7826c882010-10-26 03:11:13 +00001201 }
1202 else
Greg Clayton96154be2011-11-13 06:57:31 +00001203 strm.PutCString ("No value");
Caroline Tice7826c882010-10-26 03:11:13 +00001204
1205 return true;
1206}