blob: 7c1bbfe9c920e8a89c5816f3fa2820ff3d100552 [file] [log] [blame]
Chris Lattner30fdc8d2010-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 Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Eli Friedman4c5de692010-06-09 07:44:37 +000012#include "lldb/API/SBThread.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000013
14#include "lldb/API/SBSymbolContext.h"
15#include "lldb/API/SBFileSpec.h"
Caroline Ticedde9cff2010-09-20 05:20:02 +000016#include "lldb/API/SBStream.h"
Greg Clayton4e78f602010-11-18 18:52:36 +000017#include "lldb/Breakpoint/BreakpointLocation.h"
Greg Clayton66111032010-06-23 01:19:29 +000018#include "lldb/Core/Debugger.h"
Andrew Kaylora75418d2013-04-15 23:33:53 +000019#include "lldb/Core/State.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020#include "lldb/Core/Stream.h"
21#include "lldb/Core/StreamFile.h"
Greg Clayton66111032010-06-23 01:19:29 +000022#include "lldb/Interpreter/CommandInterpreter.h"
Jason Molenda5dd49162013-11-06 00:04:44 +000023#include "lldb/Target/SystemRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000024#include "lldb/Target/Thread.h"
25#include "lldb/Target/Process.h"
26#include "lldb/Symbol/SymbolContext.h"
27#include "lldb/Symbol/CompileUnit.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000028#include "lldb/Target/StopInfo.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029#include "lldb/Target/Target.h"
30#include "lldb/Target/ThreadPlan.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031#include "lldb/Target/ThreadPlanStepInstruction.h"
32#include "lldb/Target/ThreadPlanStepOut.h"
33#include "lldb/Target/ThreadPlanStepRange.h"
34#include "lldb/Target/ThreadPlanStepInRange.h"
35
36
Eli Friedman4c5de692010-06-09 07:44:37 +000037#include "lldb/API/SBAddress.h"
Eli Friedman4c5de692010-06-09 07:44:37 +000038#include "lldb/API/SBDebugger.h"
Jim Ingham4f465cf2012-10-10 18:32:14 +000039#include "lldb/API/SBEvent.h"
Jim Ingham73ca05a2011-12-17 01:35:57 +000040#include "lldb/API/SBFrame.h"
Eli Friedman4c5de692010-06-09 07:44:37 +000041#include "lldb/API/SBProcess.h"
Jim Ingham73ca05a2011-12-17 01:35:57 +000042#include "lldb/API/SBValue.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043
44using namespace lldb;
45using namespace lldb_private;
46
Jim Ingham4f465cf2012-10-10 18:32:14 +000047const char *
48SBThread::GetBroadcasterClassName ()
49{
50 return Thread::GetStaticBroadcasterClass().AsCString();
51}
52
Greg Claytoncfd1ace2010-10-31 03:01:06 +000053//----------------------------------------------------------------------
54// Constructors
55//----------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +000056SBThread::SBThread () :
Greg Clayton7fdf9ef2012-04-05 16:12:35 +000057 m_opaque_sp (new ExecutionContextRef())
Chris Lattner30fdc8d2010-06-08 16:52:24 +000058{
59}
60
Chris Lattner30fdc8d2010-06-08 16:52:24 +000061SBThread::SBThread (const ThreadSP& lldb_object_sp) :
Greg Clayton7fdf9ef2012-04-05 16:12:35 +000062 m_opaque_sp (new ExecutionContextRef(lldb_object_sp))
Chris Lattner30fdc8d2010-06-08 16:52:24 +000063{
64}
65
Greg Clayton92ef5732010-10-30 18:26:59 +000066SBThread::SBThread (const SBThread &rhs) :
Greg Clayton7fdf9ef2012-04-05 16:12:35 +000067 m_opaque_sp (new ExecutionContextRef(*rhs.m_opaque_sp))
Chris Lattner30fdc8d2010-06-08 16:52:24 +000068{
Greg Clayton7fdf9ef2012-04-05 16:12:35 +000069
Chris Lattner30fdc8d2010-06-08 16:52:24 +000070}
71
72//----------------------------------------------------------------------
Greg Claytoncfd1ace2010-10-31 03:01:06 +000073// Assignment operator
74//----------------------------------------------------------------------
75
76const lldb::SBThread &
77SBThread::operator = (const SBThread &rhs)
78{
79 if (this != &rhs)
Greg Clayton7fdf9ef2012-04-05 16:12:35 +000080 *m_opaque_sp = *rhs.m_opaque_sp;
Greg Claytoncfd1ace2010-10-31 03:01:06 +000081 return *this;
82}
83
84//----------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +000085// Destructor
86//----------------------------------------------------------------------
87SBThread::~SBThread()
88{
89}
90
91bool
92SBThread::IsValid() const
93{
Greg Clayton7fdf9ef2012-04-05 16:12:35 +000094 return m_opaque_sp->GetThreadSP().get() != NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000095}
96
Greg Clayton48e42542010-07-30 20:12:55 +000097void
98SBThread::Clear ()
99{
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000100 m_opaque_sp->Clear();
Greg Clayton48e42542010-07-30 20:12:55 +0000101}
102
103
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000104StopReason
105SBThread::GetStopReason()
106{
Greg Clayton5160ce52013-03-27 23:08:40 +0000107 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000108
Caroline Ticeceb6b132010-10-26 03:11:13 +0000109 StopReason reason = eStopReasonInvalid;
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000110 Mutex::Locker api_locker;
111 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
112
Greg Clayton1ac04c32012-02-21 00:09:25 +0000113 if (exe_ctx.HasThreadScope())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000114 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000115 Process::StopLocker stop_locker;
116 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
117 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000118 return exe_ctx.GetThreadPtr()->GetStopReason();
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000119 }
Greg Claytonc9858e42012-04-06 02:17:47 +0000120 else
121 {
122 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000123 log->Printf ("SBThread(%p)::GetStopReason() => error: process is running",
124 static_cast<void*>(exe_ctx.GetThreadPtr()));
Greg Claytonc9858e42012-04-06 02:17:47 +0000125 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000126 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000127
128 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000129 log->Printf ("SBThread(%p)::GetStopReason () => %s",
130 static_cast<void*>(exe_ctx.GetThreadPtr()),
Caroline Tice750cd172010-10-26 23:49:36 +0000131 Thread::StopReasonAsCString (reason));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000132
133 return reason;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000134}
135
136size_t
Greg Clayton4e78f602010-11-18 18:52:36 +0000137SBThread::GetStopReasonDataCount ()
138{
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000139 Mutex::Locker api_locker;
140 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
141
Greg Clayton1ac04c32012-02-21 00:09:25 +0000142 if (exe_ctx.HasThreadScope())
Greg Clayton4e78f602010-11-18 18:52:36 +0000143 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000144 Process::StopLocker stop_locker;
145 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
Greg Clayton4e78f602010-11-18 18:52:36 +0000146 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000147 StopInfoSP stop_info_sp = exe_ctx.GetThreadPtr()->GetStopInfo ();
148 if (stop_info_sp)
Greg Clayton4e78f602010-11-18 18:52:36 +0000149 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000150 StopReason reason = stop_info_sp->GetStopReason();
151 switch (reason)
Greg Clayton4e78f602010-11-18 18:52:36 +0000152 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000153 case eStopReasonInvalid:
154 case eStopReasonNone:
155 case eStopReasonTrace:
Greg Clayton90ba8112012-12-05 00:16:59 +0000156 case eStopReasonExec:
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000157 case eStopReasonPlanComplete:
Andrew Kaylorf85defa2012-12-20 23:08:03 +0000158 case eStopReasonThreadExiting:
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000159 // There is no data for these stop reasons.
160 return 0;
161
162 case eStopReasonBreakpoint:
163 {
164 break_id_t site_id = stop_info_sp->GetValue();
165 lldb::BreakpointSiteSP bp_site_sp (exe_ctx.GetProcessPtr()->GetBreakpointSiteList().FindByID (site_id));
166 if (bp_site_sp)
167 return bp_site_sp->GetNumberOfOwners () * 2;
168 else
169 return 0; // Breakpoint must have cleared itself...
170 }
171 break;
172
173 case eStopReasonWatchpoint:
174 return 1;
175
176 case eStopReasonSignal:
177 return 1;
178
179 case eStopReasonException:
180 return 1;
Greg Clayton4e78f602010-11-18 18:52:36 +0000181 }
Greg Clayton4e78f602010-11-18 18:52:36 +0000182 }
183 }
Greg Claytonc9858e42012-04-06 02:17:47 +0000184 else
185 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000186 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytonc9858e42012-04-06 02:17:47 +0000187 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000188 log->Printf ("SBThread(%p)::GetStopReasonDataCount() => error: process is running",
189 static_cast<void*>(exe_ctx.GetThreadPtr()));
Greg Claytonc9858e42012-04-06 02:17:47 +0000190 }
Greg Clayton4e78f602010-11-18 18:52:36 +0000191 }
192 return 0;
193}
194
195uint64_t
196SBThread::GetStopReasonDataAtIndex (uint32_t idx)
197{
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000198 Mutex::Locker api_locker;
199 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
200
Greg Clayton1ac04c32012-02-21 00:09:25 +0000201 if (exe_ctx.HasThreadScope())
Greg Clayton4e78f602010-11-18 18:52:36 +0000202 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000203 Process::StopLocker stop_locker;
204 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
Greg Clayton4e78f602010-11-18 18:52:36 +0000205 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000206 Thread *thread = exe_ctx.GetThreadPtr();
207 StopInfoSP stop_info_sp = thread->GetStopInfo ();
208 if (stop_info_sp)
209 {
210 StopReason reason = stop_info_sp->GetStopReason();
211 switch (reason)
Greg Clayton4e78f602010-11-18 18:52:36 +0000212 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000213 case eStopReasonInvalid:
214 case eStopReasonNone:
215 case eStopReasonTrace:
Greg Clayton90ba8112012-12-05 00:16:59 +0000216 case eStopReasonExec:
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000217 case eStopReasonPlanComplete:
Andrew Kaylorf85defa2012-12-20 23:08:03 +0000218 case eStopReasonThreadExiting:
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000219 // There is no data for these stop reasons.
220 return 0;
221
222 case eStopReasonBreakpoint:
Greg Clayton4e78f602010-11-18 18:52:36 +0000223 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000224 break_id_t site_id = stop_info_sp->GetValue();
225 lldb::BreakpointSiteSP bp_site_sp (exe_ctx.GetProcessPtr()->GetBreakpointSiteList().FindByID (site_id));
226 if (bp_site_sp)
Greg Clayton4e78f602010-11-18 18:52:36 +0000227 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000228 uint32_t bp_index = idx / 2;
229 BreakpointLocationSP bp_loc_sp (bp_site_sp->GetOwnerAtIndex (bp_index));
230 if (bp_loc_sp)
Greg Clayton4e78f602010-11-18 18:52:36 +0000231 {
Greg Clayton8334e142014-04-11 17:27:02 +0000232 if (idx & 1)
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000233 {
234 // Odd idx, return the breakpoint location ID
235 return bp_loc_sp->GetID();
236 }
237 else
238 {
239 // Even idx, return the breakpoint ID
240 return bp_loc_sp->GetBreakpoint().GetID();
241 }
Greg Clayton4e78f602010-11-18 18:52:36 +0000242 }
243 }
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000244 return LLDB_INVALID_BREAK_ID;
Greg Clayton4e78f602010-11-18 18:52:36 +0000245 }
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000246 break;
247
248 case eStopReasonWatchpoint:
249 return stop_info_sp->GetValue();
250
251 case eStopReasonSignal:
252 return stop_info_sp->GetValue();
253
254 case eStopReasonException:
255 return stop_info_sp->GetValue();
Greg Clayton4e78f602010-11-18 18:52:36 +0000256 }
Greg Clayton4e78f602010-11-18 18:52:36 +0000257 }
258 }
Greg Claytonc9858e42012-04-06 02:17:47 +0000259 else
260 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000261 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytonc9858e42012-04-06 02:17:47 +0000262 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000263 log->Printf ("SBThread(%p)::GetStopReasonDataAtIndex() => error: process is running",
264 static_cast<void*>(exe_ctx.GetThreadPtr()));
Greg Claytonc9858e42012-04-06 02:17:47 +0000265 }
Greg Clayton4e78f602010-11-18 18:52:36 +0000266 }
267 return 0;
268}
269
270size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000271SBThread::GetStopDescription (char *dst, size_t dst_len)
272{
Greg Clayton5160ce52013-03-27 23:08:40 +0000273 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000274
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000275 Mutex::Locker api_locker;
276 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
277
Greg Clayton1ac04c32012-02-21 00:09:25 +0000278 if (exe_ctx.HasThreadScope())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000279 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000280 Process::StopLocker stop_locker;
281 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000282 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000283
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000284 StopInfoSP stop_info_sp = exe_ctx.GetThreadPtr()->GetStopInfo ();
285 if (stop_info_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000286 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000287 const char *stop_desc = stop_info_sp->GetDescription();
288 if (stop_desc)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000289 {
Caroline Ticeceb6b132010-10-26 03:11:13 +0000290 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000291 log->Printf ("SBThread(%p)::GetStopDescription (dst, dst_len) => \"%s\"",
292 static_cast<void*>(exe_ctx.GetThreadPtr()),
293 stop_desc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000294 if (dst)
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000295 return ::snprintf (dst, dst_len, "%s", stop_desc);
296 else
297 {
298 // NULL dst passed in, return the length needed to contain the description
299 return ::strlen (stop_desc) + 1; // Include the NULL byte for size
300 }
301 }
302 else
303 {
304 size_t stop_desc_len = 0;
305 switch (stop_info_sp->GetStopReason())
306 {
307 case eStopReasonTrace:
308 case eStopReasonPlanComplete:
309 {
310 static char trace_desc[] = "step";
311 stop_desc = trace_desc;
312 stop_desc_len = sizeof(trace_desc); // Include the NULL byte for size
313 }
314 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000315
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000316 case eStopReasonBreakpoint:
317 {
318 static char bp_desc[] = "breakpoint hit";
319 stop_desc = bp_desc;
320 stop_desc_len = sizeof(bp_desc); // Include the NULL byte for size
321 }
322 break;
323
324 case eStopReasonWatchpoint:
325 {
326 static char wp_desc[] = "watchpoint hit";
327 stop_desc = wp_desc;
328 stop_desc_len = sizeof(wp_desc); // Include the NULL byte for size
329 }
330 break;
331
332 case eStopReasonSignal:
333 {
334 stop_desc = exe_ctx.GetProcessPtr()->GetUnixSignals ().GetSignalAsCString (stop_info_sp->GetValue());
335 if (stop_desc == NULL || stop_desc[0] == '\0')
336 {
337 static char signal_desc[] = "signal";
338 stop_desc = signal_desc;
339 stop_desc_len = sizeof(signal_desc); // Include the NULL byte for size
340 }
341 }
342 break;
343
344 case eStopReasonException:
345 {
346 char exc_desc[] = "exception";
347 stop_desc = exc_desc;
348 stop_desc_len = sizeof(exc_desc); // Include the NULL byte for size
349 }
350 break;
351
Greg Clayton90ba8112012-12-05 00:16:59 +0000352 case eStopReasonExec:
353 {
354 char exc_desc[] = "exec";
355 stop_desc = exc_desc;
356 stop_desc_len = sizeof(exc_desc); // Include the NULL byte for size
357 }
358 break;
359
Andrew Kaylorf85defa2012-12-20 23:08:03 +0000360 case eStopReasonThreadExiting:
361 {
362 char limbo_desc[] = "thread exiting";
363 stop_desc = limbo_desc;
364 stop_desc_len = sizeof(limbo_desc);
365 }
366 break;
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000367 default:
368 break;
369 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000370
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000371 if (stop_desc && stop_desc[0])
372 {
373 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000374 log->Printf ("SBThread(%p)::GetStopDescription (dst, dst_len) => '%s'",
375 static_cast<void*>(exe_ctx.GetThreadPtr()),
376 stop_desc);
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000377
378 if (dst)
379 return ::snprintf (dst, dst_len, "%s", stop_desc) + 1; // Include the NULL byte
380
381 if (stop_desc_len == 0)
382 stop_desc_len = ::strlen (stop_desc) + 1; // Include the NULL byte
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000383
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000384 return stop_desc_len;
385 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000386 }
387 }
388 }
Greg Claytonc9858e42012-04-06 02:17:47 +0000389 else
390 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000391 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytonc9858e42012-04-06 02:17:47 +0000392 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000393 log->Printf ("SBThread(%p)::GetStopDescription() => error: process is running",
394 static_cast<void*>(exe_ctx.GetThreadPtr()));
Greg Claytonc9858e42012-04-06 02:17:47 +0000395 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000396 }
397 if (dst)
398 *dst = 0;
399 return 0;
400}
401
Jim Ingham73ca05a2011-12-17 01:35:57 +0000402SBValue
403SBThread::GetStopReturnValue ()
404{
Greg Clayton5160ce52013-03-27 23:08:40 +0000405 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Ingham73ca05a2011-12-17 01:35:57 +0000406 ValueObjectSP return_valobj_sp;
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000407 Mutex::Locker api_locker;
408 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
409
Greg Clayton1ac04c32012-02-21 00:09:25 +0000410 if (exe_ctx.HasThreadScope())
Jim Ingham73ca05a2011-12-17 01:35:57 +0000411 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000412 Process::StopLocker stop_locker;
413 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
Jim Ingham73ca05a2011-12-17 01:35:57 +0000414 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000415 StopInfoSP stop_info_sp = exe_ctx.GetThreadPtr()->GetStopInfo ();
416 if (stop_info_sp)
417 {
418 return_valobj_sp = StopInfo::GetReturnValueObject (stop_info_sp);
419 }
Jim Ingham73ca05a2011-12-17 01:35:57 +0000420 }
Greg Claytonc9858e42012-04-06 02:17:47 +0000421 else
422 {
423 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000424 log->Printf ("SBThread(%p)::GetStopReturnValue() => error: process is running",
425 static_cast<void*>(exe_ctx.GetThreadPtr()));
Greg Claytonc9858e42012-04-06 02:17:47 +0000426 }
Jim Ingham73ca05a2011-12-17 01:35:57 +0000427 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000428
Jim Ingham73ca05a2011-12-17 01:35:57 +0000429 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000430 log->Printf ("SBThread(%p)::GetStopReturnValue () => %s",
431 static_cast<void*>(exe_ctx.GetThreadPtr()),
432 return_valobj_sp.get()
433 ? return_valobj_sp->GetValueAsCString()
434 : "<no return value>");
435
Jim Ingham73ca05a2011-12-17 01:35:57 +0000436 return SBValue (return_valobj_sp);
437}
438
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000439void
440SBThread::SetThread (const ThreadSP& lldb_object_sp)
441{
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000442 m_opaque_sp->SetThreadSP (lldb_object_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000443}
444
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000445lldb::tid_t
446SBThread::GetThreadID () const
447{
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000448 ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
Greg Clayton17a6ad02012-01-30 02:53:15 +0000449 if (thread_sp)
Greg Clayton1ac04c32012-02-21 00:09:25 +0000450 return thread_sp->GetID();
451 return LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000452}
453
454uint32_t
455SBThread::GetIndexID () const
456{
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000457 ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
Greg Clayton17a6ad02012-01-30 02:53:15 +0000458 if (thread_sp)
459 return thread_sp->GetIndexID();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000460 return LLDB_INVALID_INDEX32;
461}
Greg Clayton1ac04c32012-02-21 00:09:25 +0000462
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000463const char *
464SBThread::GetName () const
465{
Greg Clayton5160ce52013-03-27 23:08:40 +0000466 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton48381312010-10-30 04:51:46 +0000467 const char *name = NULL;
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000468 Mutex::Locker api_locker;
469 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
470
Greg Clayton1ac04c32012-02-21 00:09:25 +0000471 if (exe_ctx.HasThreadScope())
Greg Claytonaf67cec2010-12-20 20:49:23 +0000472 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000473 Process::StopLocker stop_locker;
474 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
475 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000476 name = exe_ctx.GetThreadPtr()->GetName();
477 }
Greg Claytonc9858e42012-04-06 02:17:47 +0000478 else
479 {
480 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000481 log->Printf ("SBThread(%p)::GetName() => error: process is running",
482 static_cast<void*>(exe_ctx.GetThreadPtr()));
Greg Claytonc9858e42012-04-06 02:17:47 +0000483 }
Greg Claytonaf67cec2010-12-20 20:49:23 +0000484 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000485
Caroline Ticeceb6b132010-10-26 03:11:13 +0000486 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000487 log->Printf ("SBThread(%p)::GetName () => %s",
488 static_cast<void*>(exe_ctx.GetThreadPtr()),
489 name ? name : "NULL");
Caroline Ticeceb6b132010-10-26 03:11:13 +0000490
Greg Clayton48381312010-10-30 04:51:46 +0000491 return name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000492}
493
494const char *
495SBThread::GetQueueName () const
496{
Greg Clayton48381312010-10-30 04:51:46 +0000497 const char *name = NULL;
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000498 Mutex::Locker api_locker;
499 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
500
Greg Clayton5160ce52013-03-27 23:08:40 +0000501 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton1ac04c32012-02-21 00:09:25 +0000502 if (exe_ctx.HasThreadScope())
Greg Claytonaf67cec2010-12-20 20:49:23 +0000503 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000504 Process::StopLocker stop_locker;
505 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
506 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000507 name = exe_ctx.GetThreadPtr()->GetQueueName();
508 }
Greg Claytonc9858e42012-04-06 02:17:47 +0000509 else
510 {
511 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000512 log->Printf ("SBThread(%p)::GetQueueName() => error: process is running",
513 static_cast<void*>(exe_ctx.GetThreadPtr()));
Greg Claytonc9858e42012-04-06 02:17:47 +0000514 }
Greg Claytonaf67cec2010-12-20 20:49:23 +0000515 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000516
Caroline Ticeceb6b132010-10-26 03:11:13 +0000517 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000518 log->Printf ("SBThread(%p)::GetQueueName () => %s",
519 static_cast<void*>(exe_ctx.GetThreadPtr()),
520 name ? name : "NULL");
Caroline Ticeceb6b132010-10-26 03:11:13 +0000521
Greg Clayton48381312010-10-30 04:51:46 +0000522 return name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000523}
524
Jason Molenda4fdb5862013-10-21 23:52:54 +0000525lldb::queue_id_t
526SBThread::GetQueueID () const
527{
528 queue_id_t id = LLDB_INVALID_QUEUE_ID;
529 Mutex::Locker api_locker;
530 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
531
532 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
533 if (exe_ctx.HasThreadScope())
534 {
535 Process::StopLocker stop_locker;
536 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
537 {
538 id = exe_ctx.GetThreadPtr()->GetQueueID();
539 }
540 else
541 {
542 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000543 log->Printf ("SBThread(%p)::GetQueueID() => error: process is running",
544 static_cast<void*>(exe_ctx.GetThreadPtr()));
Jason Molenda4fdb5862013-10-21 23:52:54 +0000545 }
546 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000547
Jason Molenda4fdb5862013-10-21 23:52:54 +0000548 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000549 log->Printf ("SBThread(%p)::GetQueueID () => 0x%" PRIx64,
550 static_cast<void*>(exe_ctx.GetThreadPtr()), id);
Jason Molenda4fdb5862013-10-21 23:52:54 +0000551
552 return id;
553}
554
Jim Ingham64e7ead2012-05-03 21:19:36 +0000555SBError
556SBThread::ResumeNewPlan (ExecutionContext &exe_ctx, ThreadPlan *new_plan)
557{
558 SBError sb_error;
559
560 Process *process = exe_ctx.GetProcessPtr();
561 if (!process)
562 {
563 sb_error.SetErrorString("No process in SBThread::ResumeNewPlan");
564 return sb_error;
565 }
566
567 Thread *thread = exe_ctx.GetThreadPtr();
568 if (!thread)
569 {
570 sb_error.SetErrorString("No thread in SBThread::ResumeNewPlan");
571 return sb_error;
572 }
573
574 // User level plans should be Master Plans so they can be interrupted, other plans executed, and
575 // then a "continue" will resume the plan.
576 if (new_plan != NULL)
577 {
578 new_plan->SetIsMasterPlan(true);
579 new_plan->SetOkayToDiscard(false);
580 }
581
582 // Why do we need to set the current thread by ID here???
583 process->GetThreadList().SetSelectedThreadByID (thread->GetID());
584 sb_error.ref() = process->Resume();
585
586 if (sb_error.Success())
587 {
588 // If we are doing synchronous mode, then wait for the
589 // process to stop yet again!
590 if (process->GetTarget().GetDebugger().GetAsyncExecution () == false)
591 process->WaitForProcessToStop (NULL);
592 }
593
594 return sb_error;
595}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000596
597void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000598SBThread::StepOver (lldb::RunMode stop_other_threads)
599{
Greg Clayton5160ce52013-03-27 23:08:40 +0000600 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000601
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000602 Mutex::Locker api_locker;
603 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
604
Caroline Ticeceb6b132010-10-26 03:11:13 +0000605
Greg Clayton17a6ad02012-01-30 02:53:15 +0000606 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000607 log->Printf ("SBThread(%p)::StepOver (stop_other_threads='%s')",
608 static_cast<void*>(exe_ctx.GetThreadPtr()),
Greg Clayton17a6ad02012-01-30 02:53:15 +0000609 Thread::RunModeAsCString (stop_other_threads));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000610
Greg Clayton1ac04c32012-02-21 00:09:25 +0000611 if (exe_ctx.HasThreadScope())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000612 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000613 Thread *thread = exe_ctx.GetThreadPtr();
Jim Ingham7ba6e992012-05-11 23:47:32 +0000614 bool abort_other_plans = false;
Jason Molendab57e4a12013-11-04 09:33:30 +0000615 StackFrameSP frame_sp(thread->GetStackFrameAtIndex (0));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000616
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000617 ThreadPlanSP new_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000618 if (frame_sp)
619 {
620 if (frame_sp->HasDebugInformation ())
621 {
Jim Ingham4b4b2472014-03-13 02:47:14 +0000622 const LazyBool avoid_no_debug = eLazyBoolCalculate;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000623 SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000624 new_plan_sp = thread->QueueThreadPlanForStepOverRange (abort_other_plans,
Jim Inghamc6276822012-12-12 19:58:40 +0000625 sc.line_entry.range,
626 sc,
Jim Ingham4b4b2472014-03-13 02:47:14 +0000627 stop_other_threads,
628 avoid_no_debug);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000629 }
630 else
631 {
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000632 new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction (true,
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000633 abort_other_plans,
634 stop_other_threads);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000635 }
636 }
637
Jim Ingham64e7ead2012-05-03 21:19:36 +0000638 // This returns an error, we should use it!
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000639 ResumeNewPlan (exe_ctx, new_plan_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000640 }
641}
642
643void
644SBThread::StepInto (lldb::RunMode stop_other_threads)
645{
Jim Inghamc6276822012-12-12 19:58:40 +0000646 StepInto (NULL, stop_other_threads);
647}
648
649void
650SBThread::StepInto (const char *target_name, lldb::RunMode stop_other_threads)
651{
Greg Clayton5160ce52013-03-27 23:08:40 +0000652 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000653
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000654 Mutex::Locker api_locker;
655 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
Greg Clayton17a6ad02012-01-30 02:53:15 +0000656
657 if (log)
Jim Inghamc6276822012-12-12 19:58:40 +0000658 log->Printf ("SBThread(%p)::StepInto (target_name='%s', stop_other_threads='%s')",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000659 static_cast<void*>(exe_ctx.GetThreadPtr()),
Jim Inghamc6276822012-12-12 19:58:40 +0000660 target_name? target_name: "<NULL>",
Greg Clayton17a6ad02012-01-30 02:53:15 +0000661 Thread::RunModeAsCString (stop_other_threads));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000662
Greg Clayton1ac04c32012-02-21 00:09:25 +0000663 if (exe_ctx.HasThreadScope())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000664 {
Jim Ingham7ba6e992012-05-11 23:47:32 +0000665 bool abort_other_plans = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000666
Greg Clayton1ac04c32012-02-21 00:09:25 +0000667 Thread *thread = exe_ctx.GetThreadPtr();
Jason Molendab57e4a12013-11-04 09:33:30 +0000668 StackFrameSP frame_sp(thread->GetStackFrameAtIndex (0));
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000669 ThreadPlanSP new_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000670
671 if (frame_sp && frame_sp->HasDebugInformation ())
672 {
Jim Ingham4b4b2472014-03-13 02:47:14 +0000673 const LazyBool step_out_avoids_code_without_debug_info = eLazyBoolCalculate;
674 const LazyBool step_in_avoids_code_without_debug_info = eLazyBoolCalculate;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000675 SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000676 new_plan_sp = thread->QueueThreadPlanForStepInRange (abort_other_plans,
Jim Inghamc6276822012-12-12 19:58:40 +0000677 sc.line_entry.range,
678 sc,
679 target_name,
680 stop_other_threads,
Jim Ingham4b4b2472014-03-13 02:47:14 +0000681 step_in_avoids_code_without_debug_info,
682 step_out_avoids_code_without_debug_info);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000683 }
684 else
685 {
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000686 new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction (false,
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000687 abort_other_plans,
688 stop_other_threads);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000689 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000690
Jim Ingham64e7ead2012-05-03 21:19:36 +0000691 // This returns an error, we should use it!
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000692 ResumeNewPlan (exe_ctx, new_plan_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000693 }
694}
695
696void
697SBThread::StepOut ()
698{
Greg Clayton5160ce52013-03-27 23:08:40 +0000699 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000700
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000701 Mutex::Locker api_locker;
702 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
703
Caroline Ticeceb6b132010-10-26 03:11:13 +0000704
Greg Clayton17a6ad02012-01-30 02:53:15 +0000705 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000706 log->Printf ("SBThread(%p)::StepOut ()",
707 static_cast<void*>(exe_ctx.GetThreadPtr()));
708
Greg Clayton1ac04c32012-02-21 00:09:25 +0000709 if (exe_ctx.HasThreadScope())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000710 {
Jim Ingham7ba6e992012-05-11 23:47:32 +0000711 bool abort_other_plans = false;
Jim Ingham94b09242012-09-14 21:07:14 +0000712 bool stop_other_threads = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000713
Greg Clayton1ac04c32012-02-21 00:09:25 +0000714 Thread *thread = exe_ctx.GetThreadPtr();
715
Jim Ingham4b4b2472014-03-13 02:47:14 +0000716 const LazyBool avoid_no_debug = eLazyBoolCalculate;
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000717 ThreadPlanSP new_plan_sp(thread->QueueThreadPlanForStepOut (abort_other_plans,
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000718 NULL,
719 false,
720 stop_other_threads,
721 eVoteYes,
722 eVoteNoOpinion,
723 0,
724 avoid_no_debug));
725
Jim Ingham64e7ead2012-05-03 21:19:36 +0000726 // This returns an error, we should use it!
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000727 ResumeNewPlan (exe_ctx, new_plan_sp.get());
Greg Clayton481cef22011-01-21 06:11:58 +0000728 }
729}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000730
Greg Clayton481cef22011-01-21 06:11:58 +0000731void
732SBThread::StepOutOfFrame (lldb::SBFrame &sb_frame)
733{
Greg Clayton5160ce52013-03-27 23:08:40 +0000734 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton481cef22011-01-21 06:11:58 +0000735
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000736 Mutex::Locker api_locker;
737 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
738
Jason Molendab57e4a12013-11-04 09:33:30 +0000739 StackFrameSP frame_sp (sb_frame.GetFrameSP());
Greg Clayton481cef22011-01-21 06:11:58 +0000740 if (log)
741 {
742 SBStream frame_desc_strm;
743 sb_frame.GetDescription (frame_desc_strm);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000744 log->Printf ("SBThread(%p)::StepOutOfFrame (frame = SBFrame(%p): %s)",
745 static_cast<void*>(exe_ctx.GetThreadPtr()),
746 static_cast<void*>(frame_sp.get()),
747 frame_desc_strm.GetData());
Greg Clayton481cef22011-01-21 06:11:58 +0000748 }
749
Greg Clayton1ac04c32012-02-21 00:09:25 +0000750 if (exe_ctx.HasThreadScope())
Greg Clayton481cef22011-01-21 06:11:58 +0000751 {
Jim Ingham7ba6e992012-05-11 23:47:32 +0000752 bool abort_other_plans = false;
Jim Ingham94b09242012-09-14 21:07:14 +0000753 bool stop_other_threads = false;
Greg Clayton1ac04c32012-02-21 00:09:25 +0000754 Thread *thread = exe_ctx.GetThreadPtr();
Greg Clayton481cef22011-01-21 06:11:58 +0000755
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000756 ThreadPlanSP new_plan_sp(thread->QueueThreadPlanForStepOut (abort_other_plans,
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000757 NULL,
758 false,
759 stop_other_threads,
760 eVoteYes,
Jim Ingham64e7ead2012-05-03 21:19:36 +0000761 eVoteNoOpinion,
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000762 frame_sp->GetFrameIndex()));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000763
Jim Ingham64e7ead2012-05-03 21:19:36 +0000764 // This returns an error, we should use it!
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000765 ResumeNewPlan (exe_ctx, new_plan_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000766 }
767}
768
769void
770SBThread::StepInstruction (bool step_over)
771{
Greg Clayton5160ce52013-03-27 23:08:40 +0000772 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000773
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000774 Mutex::Locker api_locker;
775 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
776
Greg Clayton1ac04c32012-02-21 00:09:25 +0000777
Caroline Ticeceb6b132010-10-26 03:11:13 +0000778
Greg Clayton17a6ad02012-01-30 02:53:15 +0000779 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000780 log->Printf ("SBThread(%p)::StepInstruction (step_over=%i)",
781 static_cast<void*>(exe_ctx.GetThreadPtr()), step_over);
782
Greg Clayton1ac04c32012-02-21 00:09:25 +0000783 if (exe_ctx.HasThreadScope())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000784 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000785 Thread *thread = exe_ctx.GetThreadPtr();
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000786 ThreadPlanSP new_plan_sp(thread->QueueThreadPlanForStepSingleInstruction (step_over, true, true));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000787
Jim Ingham64e7ead2012-05-03 21:19:36 +0000788 // This returns an error, we should use it!
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000789 ResumeNewPlan (exe_ctx, new_plan_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000790 }
791}
792
793void
794SBThread::RunToAddress (lldb::addr_t addr)
795{
Greg Clayton5160ce52013-03-27 23:08:40 +0000796 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000797
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000798 Mutex::Locker api_locker;
799 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
800
Caroline Ticeceb6b132010-10-26 03:11:13 +0000801
Greg Clayton17a6ad02012-01-30 02:53:15 +0000802 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000803 log->Printf ("SBThread(%p)::RunToAddress (addr=0x%" PRIx64 ")",
804 static_cast<void*>(exe_ctx.GetThreadPtr()), addr);
805
Greg Clayton1ac04c32012-02-21 00:09:25 +0000806 if (exe_ctx.HasThreadScope())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000807 {
Jim Ingham7ba6e992012-05-11 23:47:32 +0000808 bool abort_other_plans = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000809 bool stop_other_threads = true;
810
Greg Claytone72dfb32012-02-24 01:59:29 +0000811 Address target_addr (addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000812
Greg Clayton1ac04c32012-02-21 00:09:25 +0000813 Thread *thread = exe_ctx.GetThreadPtr();
Greg Clayton1ac04c32012-02-21 00:09:25 +0000814
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000815 ThreadPlanSP new_plan_sp(thread->QueueThreadPlanForRunToAddress (abort_other_plans, target_addr, stop_other_threads));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000816
Jim Ingham64e7ead2012-05-03 21:19:36 +0000817 // This returns an error, we should use it!
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000818 ResumeNewPlan (exe_ctx, new_plan_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000819 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000820}
821
Greg Clayton481cef22011-01-21 06:11:58 +0000822SBError
823SBThread::StepOverUntil (lldb::SBFrame &sb_frame,
824 lldb::SBFileSpec &sb_file_spec,
825 uint32_t line)
826{
827 SBError sb_error;
Greg Clayton5160ce52013-03-27 23:08:40 +0000828 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton481cef22011-01-21 06:11:58 +0000829 char path[PATH_MAX];
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000830
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000831 Mutex::Locker api_locker;
832 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
833
Jason Molendab57e4a12013-11-04 09:33:30 +0000834 StackFrameSP frame_sp (sb_frame.GetFrameSP());
Greg Claytonb9556ac2012-01-30 07:41:31 +0000835
Greg Clayton481cef22011-01-21 06:11:58 +0000836 if (log)
837 {
838 SBStream frame_desc_strm;
839 sb_frame.GetDescription (frame_desc_strm);
840 sb_file_spec->GetPath (path, sizeof(path));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000841 log->Printf ("SBThread(%p)::StepOverUntil (frame = SBFrame(%p): %s, file+line = %s:%u)",
842 static_cast<void*>(exe_ctx.GetThreadPtr()),
843 static_cast<void*>(frame_sp.get()),
844 frame_desc_strm.GetData(), path, line);
Greg Clayton481cef22011-01-21 06:11:58 +0000845 }
Greg Clayton17a6ad02012-01-30 02:53:15 +0000846
Greg Clayton1ac04c32012-02-21 00:09:25 +0000847 if (exe_ctx.HasThreadScope())
Greg Clayton481cef22011-01-21 06:11:58 +0000848 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000849 Target *target = exe_ctx.GetTargetPtr();
Greg Clayton1ac04c32012-02-21 00:09:25 +0000850 Thread *thread = exe_ctx.GetThreadPtr();
Greg Clayton481cef22011-01-21 06:11:58 +0000851
852 if (line == 0)
853 {
854 sb_error.SetErrorString("invalid line argument");
855 return sb_error;
856 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000857
Greg Claytonb9556ac2012-01-30 07:41:31 +0000858 if (!frame_sp)
Greg Clayton481cef22011-01-21 06:11:58 +0000859 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000860 frame_sp = thread->GetSelectedFrame ();
Greg Clayton481cef22011-01-21 06:11:58 +0000861 if (!frame_sp)
Greg Clayton1ac04c32012-02-21 00:09:25 +0000862 frame_sp = thread->GetStackFrameAtIndex (0);
Greg Clayton481cef22011-01-21 06:11:58 +0000863 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000864
Greg Clayton481cef22011-01-21 06:11:58 +0000865 SymbolContext frame_sc;
866 if (!frame_sp)
867 {
868 sb_error.SetErrorString("no valid frames in thread to step");
869 return sb_error;
870 }
871
872 // If we have a frame, get its line
873 frame_sc = frame_sp->GetSymbolContext (eSymbolContextCompUnit |
874 eSymbolContextFunction |
875 eSymbolContextLineEntry |
876 eSymbolContextSymbol );
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000877
Greg Clayton481cef22011-01-21 06:11:58 +0000878 if (frame_sc.comp_unit == NULL)
879 {
880 sb_error.SetErrorStringWithFormat("frame %u doesn't have debug information", frame_sp->GetFrameIndex());
881 return sb_error;
882 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000883
Greg Clayton481cef22011-01-21 06:11:58 +0000884 FileSpec step_file_spec;
885 if (sb_file_spec.IsValid())
886 {
887 // The file spec passed in was valid, so use it
888 step_file_spec = sb_file_spec.ref();
889 }
890 else
891 {
892 if (frame_sc.line_entry.IsValid())
893 step_file_spec = frame_sc.line_entry.file;
894 else
895 {
896 sb_error.SetErrorString("invalid file argument or no file for frame");
897 return sb_error;
898 }
899 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000900
Jim Ingham9b70ddb2011-05-08 00:56:32 +0000901 // Grab the current function, then we will make sure the "until" address is
902 // within the function. We discard addresses that are out of the current
903 // function, and then if there are no addresses remaining, give an appropriate
904 // error message.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000905
Jim Ingham9b70ddb2011-05-08 00:56:32 +0000906 bool all_in_function = true;
907 AddressRange fun_range = frame_sc.function->GetAddressRange();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000908
Greg Clayton481cef22011-01-21 06:11:58 +0000909 std::vector<addr_t> step_over_until_addrs;
Jim Ingham7ba6e992012-05-11 23:47:32 +0000910 const bool abort_other_plans = false;
Jim Inghamc02e3342012-09-14 18:57:14 +0000911 const bool stop_other_threads = false;
Greg Clayton481cef22011-01-21 06:11:58 +0000912 const bool check_inlines = true;
913 const bool exact = false;
914
915 SymbolContextList sc_list;
Jim Ingham9b70ddb2011-05-08 00:56:32 +0000916 const uint32_t num_matches = frame_sc.comp_unit->ResolveSymbolContext (step_file_spec,
917 line,
918 check_inlines,
919 exact,
920 eSymbolContextLineEntry,
921 sc_list);
Greg Clayton481cef22011-01-21 06:11:58 +0000922 if (num_matches > 0)
923 {
924 SymbolContext sc;
925 for (uint32_t i=0; i<num_matches; ++i)
926 {
927 if (sc_list.GetContextAtIndex(i, sc))
928 {
Jim Ingham9b70ddb2011-05-08 00:56:32 +0000929 addr_t step_addr = sc.line_entry.range.GetBaseAddress().GetLoadAddress(target);
Greg Clayton481cef22011-01-21 06:11:58 +0000930 if (step_addr != LLDB_INVALID_ADDRESS)
931 {
Jim Ingham9b70ddb2011-05-08 00:56:32 +0000932 if (fun_range.ContainsLoadAddress(step_addr, target))
933 step_over_until_addrs.push_back(step_addr);
934 else
935 all_in_function = false;
Greg Clayton481cef22011-01-21 06:11:58 +0000936 }
937 }
938 }
939 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000940
Greg Clayton481cef22011-01-21 06:11:58 +0000941 if (step_over_until_addrs.empty())
942 {
Jim Ingham9b70ddb2011-05-08 00:56:32 +0000943 if (all_in_function)
944 {
945 step_file_spec.GetPath (path, sizeof(path));
Jason Molendafd54b362011-09-20 21:44:10 +0000946 sb_error.SetErrorStringWithFormat("No line entries for %s:%u", path, line);
Jim Ingham9b70ddb2011-05-08 00:56:32 +0000947 }
948 else
Greg Clayton86edbf42011-10-26 00:56:27 +0000949 sb_error.SetErrorString ("step until target not in current function");
Greg Clayton481cef22011-01-21 06:11:58 +0000950 }
951 else
952 {
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000953 ThreadPlanSP new_plan_sp(thread->QueueThreadPlanForStepUntil (abort_other_plans,
Jim Ingham64e7ead2012-05-03 21:19:36 +0000954 &step_over_until_addrs[0],
955 step_over_until_addrs.size(),
956 stop_other_threads,
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000957 frame_sp->GetFrameIndex()));
Greg Clayton481cef22011-01-21 06:11:58 +0000958
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000959 sb_error = ResumeNewPlan (exe_ctx, new_plan_sp.get());
Greg Clayton481cef22011-01-21 06:11:58 +0000960 }
961 }
962 else
963 {
964 sb_error.SetErrorString("this SBThread object is invalid");
965 }
966 return sb_error;
967}
968
Jim Ingham44137582012-09-12 00:40:39 +0000969SBError
Richard Mittonf86248d2013-09-12 02:20:34 +0000970SBThread::JumpToLine (lldb::SBFileSpec &file_spec, uint32_t line)
971{
972 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
973 SBError sb_error;
974
975 Mutex::Locker api_locker;
976 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
977
978 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000979 log->Printf ("SBThread(%p)::JumpToLine (file+line = %s:%u)",
980 static_cast<void*>(exe_ctx.GetThreadPtr()),
981 file_spec->GetPath().c_str(), line);
Richard Mittonf86248d2013-09-12 02:20:34 +0000982
983 if (!exe_ctx.HasThreadScope())
984 {
985 sb_error.SetErrorString("this SBThread object is invalid");
986 return sb_error;
987 }
988
989 Thread *thread = exe_ctx.GetThreadPtr();
990
991 Error err = thread->JumpToLine (file_spec.get(), line, true);
992 sb_error.SetError (err);
993 return sb_error;
994}
995
996SBError
Jim Inghamcb640dd2012-09-14 02:14:15 +0000997SBThread::ReturnFromFrame (SBFrame &frame, SBValue &return_value)
Jim Ingham44137582012-09-12 00:40:39 +0000998{
999 SBError sb_error;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001000
Greg Clayton5160ce52013-03-27 23:08:40 +00001001 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Ingham44137582012-09-12 00:40:39 +00001002
1003 Mutex::Locker api_locker;
1004 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1005
1006
1007 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001008 log->Printf ("SBThread(%p)::ReturnFromFrame (frame=%d)",
1009 static_cast<void*>(exe_ctx.GetThreadPtr()),
1010 frame.GetFrameID());
1011
Jim Ingham44137582012-09-12 00:40:39 +00001012 if (exe_ctx.HasThreadScope())
1013 {
1014 Thread *thread = exe_ctx.GetThreadPtr();
Jim Inghamcb640dd2012-09-14 02:14:15 +00001015 sb_error.SetError (thread->ReturnFromFrame(frame.GetFrameSP(), return_value.GetSP()));
Jim Ingham44137582012-09-12 00:40:39 +00001016 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001017
Jim Ingham44137582012-09-12 00:40:39 +00001018 return sb_error;
1019}
1020
Greg Clayton481cef22011-01-21 06:11:58 +00001021
Greg Clayton722a0cd2011-01-12 02:25:42 +00001022bool
1023SBThread::Suspend()
1024{
Greg Clayton5160ce52013-03-27 23:08:40 +00001025 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001026 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Claytonc9858e42012-04-06 02:17:47 +00001027 bool result = false;
Greg Clayton1ac04c32012-02-21 00:09:25 +00001028 if (exe_ctx.HasThreadScope())
Greg Clayton722a0cd2011-01-12 02:25:42 +00001029 {
Greg Claytonc9858e42012-04-06 02:17:47 +00001030 Process::StopLocker stop_locker;
1031 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1032 {
1033 exe_ctx.GetThreadPtr()->SetResumeState (eStateSuspended);
1034 result = true;
1035 }
1036 else
1037 {
1038 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001039 log->Printf ("SBThread(%p)::Suspend() => error: process is running",
1040 static_cast<void*>(exe_ctx.GetThreadPtr()));
Greg Claytonc9858e42012-04-06 02:17:47 +00001041 }
Greg Clayton722a0cd2011-01-12 02:25:42 +00001042 }
Greg Claytonc9858e42012-04-06 02:17:47 +00001043 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001044 log->Printf ("SBThread(%p)::Suspend() => %i",
1045 static_cast<void*>(exe_ctx.GetThreadPtr()), result);
Greg Claytonc9858e42012-04-06 02:17:47 +00001046 return result;
Greg Clayton722a0cd2011-01-12 02:25:42 +00001047}
1048
1049bool
1050SBThread::Resume ()
1051{
Greg Clayton5160ce52013-03-27 23:08:40 +00001052 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001053 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Claytonc9858e42012-04-06 02:17:47 +00001054 bool result = false;
Greg Clayton1ac04c32012-02-21 00:09:25 +00001055 if (exe_ctx.HasThreadScope())
Greg Clayton722a0cd2011-01-12 02:25:42 +00001056 {
Greg Claytonc9858e42012-04-06 02:17:47 +00001057 Process::StopLocker stop_locker;
1058 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1059 {
Jim Ingham6c9ed912014-04-03 01:26:14 +00001060 const bool override_suspend = true;
1061 exe_ctx.GetThreadPtr()->SetResumeState (eStateRunning, override_suspend);
Greg Claytonc9858e42012-04-06 02:17:47 +00001062 result = true;
1063 }
1064 else
1065 {
1066 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001067 log->Printf ("SBThread(%p)::Resume() => error: process is running",
1068 static_cast<void*>(exe_ctx.GetThreadPtr()));
Greg Claytonc9858e42012-04-06 02:17:47 +00001069 }
Greg Clayton722a0cd2011-01-12 02:25:42 +00001070 }
Greg Claytonc9858e42012-04-06 02:17:47 +00001071 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001072 log->Printf ("SBThread(%p)::Resume() => %i",
1073 static_cast<void*>(exe_ctx.GetThreadPtr()), result);
Greg Claytonc9858e42012-04-06 02:17:47 +00001074 return result;
Greg Clayton722a0cd2011-01-12 02:25:42 +00001075}
1076
1077bool
1078SBThread::IsSuspended()
1079{
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001080 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Clayton1ac04c32012-02-21 00:09:25 +00001081 if (exe_ctx.HasThreadScope())
1082 return exe_ctx.GetThreadPtr()->GetResumeState () == eStateSuspended;
Greg Clayton722a0cd2011-01-12 02:25:42 +00001083 return false;
1084}
1085
Andrew Kaylora75418d2013-04-15 23:33:53 +00001086bool
1087SBThread::IsStopped()
1088{
1089 ExecutionContext exe_ctx (m_opaque_sp.get());
1090 if (exe_ctx.HasThreadScope())
1091 return StateIsStoppedState(exe_ctx.GetThreadPtr()->GetState(), true);
1092 return false;
1093}
1094
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001095SBProcess
1096SBThread::GetProcess ()
1097{
Greg Claytonb9556ac2012-01-30 07:41:31 +00001098 SBProcess sb_process;
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001099 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Clayton1ac04c32012-02-21 00:09:25 +00001100 if (exe_ctx.HasThreadScope())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001101 {
1102 // Have to go up to the target so we can get a shared pointer to our process...
Greg Clayton1ac04c32012-02-21 00:09:25 +00001103 sb_process.SetSP (exe_ctx.GetProcessSP());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001104 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001105
Greg Clayton5160ce52013-03-27 23:08:40 +00001106 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001107 if (log)
1108 {
Greg Clayton481cef22011-01-21 06:11:58 +00001109 SBStream frame_desc_strm;
Greg Claytonb9556ac2012-01-30 07:41:31 +00001110 sb_process.GetDescription (frame_desc_strm);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001111 log->Printf ("SBThread(%p)::GetProcess () => SBProcess(%p): %s",
1112 static_cast<void*>(exe_ctx.GetThreadPtr()),
1113 static_cast<void*>(sb_process.GetSP().get()),
1114 frame_desc_strm.GetData());
Caroline Ticeceb6b132010-10-26 03:11:13 +00001115 }
1116
Greg Claytonb9556ac2012-01-30 07:41:31 +00001117 return sb_process;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001118}
1119
1120uint32_t
1121SBThread::GetNumFrames ()
1122{
Greg Clayton5160ce52013-03-27 23:08:40 +00001123 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001124
Caroline Ticeceb6b132010-10-26 03:11:13 +00001125 uint32_t num_frames = 0;
Jim Ingham4fc6cb92012-08-22 21:34:33 +00001126 Mutex::Locker api_locker;
1127 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1128
Greg Clayton1ac04c32012-02-21 00:09:25 +00001129 if (exe_ctx.HasThreadScope())
Greg Claytonaf67cec2010-12-20 20:49:23 +00001130 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001131 Process::StopLocker stop_locker;
1132 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1133 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001134 num_frames = exe_ctx.GetThreadPtr()->GetStackFrameCount();
1135 }
Greg Claytonc9858e42012-04-06 02:17:47 +00001136 else
1137 {
1138 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001139 log->Printf ("SBThread(%p)::GetNumFrames() => error: process is running",
1140 static_cast<void*>(exe_ctx.GetThreadPtr()));
Greg Claytonc9858e42012-04-06 02:17:47 +00001141 }
Greg Claytonaf67cec2010-12-20 20:49:23 +00001142 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001143
1144 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001145 log->Printf ("SBThread(%p)::GetNumFrames () => %u",
1146 static_cast<void*>(exe_ctx.GetThreadPtr()), num_frames);
Caroline Ticeceb6b132010-10-26 03:11:13 +00001147
1148 return num_frames;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001149}
1150
1151SBFrame
1152SBThread::GetFrameAtIndex (uint32_t idx)
1153{
Greg Clayton5160ce52013-03-27 23:08:40 +00001154 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001155
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001156 SBFrame sb_frame;
Jason Molendab57e4a12013-11-04 09:33:30 +00001157 StackFrameSP frame_sp;
Jim Ingham4fc6cb92012-08-22 21:34:33 +00001158 Mutex::Locker api_locker;
1159 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1160
Greg Clayton1ac04c32012-02-21 00:09:25 +00001161 if (exe_ctx.HasThreadScope())
Greg Claytonaf67cec2010-12-20 20:49:23 +00001162 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001163 Process::StopLocker stop_locker;
1164 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1165 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001166 frame_sp = exe_ctx.GetThreadPtr()->GetStackFrameAtIndex (idx);
1167 sb_frame.SetFrameSP (frame_sp);
1168 }
Greg Claytonc9858e42012-04-06 02:17:47 +00001169 else
1170 {
1171 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001172 log->Printf ("SBThread(%p)::GetFrameAtIndex() => error: process is running",
1173 static_cast<void*>(exe_ctx.GetThreadPtr()));
Greg Claytonc9858e42012-04-06 02:17:47 +00001174 }
Greg Claytonaf67cec2010-12-20 20:49:23 +00001175 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001176
1177 if (log)
1178 {
Greg Clayton481cef22011-01-21 06:11:58 +00001179 SBStream frame_desc_strm;
1180 sb_frame.GetDescription (frame_desc_strm);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001181 log->Printf ("SBThread(%p)::GetFrameAtIndex (idx=%d) => SBFrame(%p): %s",
1182 static_cast<void*>(exe_ctx.GetThreadPtr()), idx,
1183 static_cast<void*>(frame_sp.get()),
1184 frame_desc_strm.GetData());
Caroline Ticeceb6b132010-10-26 03:11:13 +00001185 }
1186
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001187 return sb_frame;
1188}
1189
Greg Claytonf028a1f2010-12-17 02:26:24 +00001190lldb::SBFrame
1191SBThread::GetSelectedFrame ()
1192{
Greg Clayton5160ce52013-03-27 23:08:40 +00001193 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytonf028a1f2010-12-17 02:26:24 +00001194
1195 SBFrame sb_frame;
Jason Molendab57e4a12013-11-04 09:33:30 +00001196 StackFrameSP frame_sp;
Jim Ingham4fc6cb92012-08-22 21:34:33 +00001197 Mutex::Locker api_locker;
1198 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1199
Greg Clayton1ac04c32012-02-21 00:09:25 +00001200 if (exe_ctx.HasThreadScope())
Greg Claytonaf67cec2010-12-20 20:49:23 +00001201 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001202 Process::StopLocker stop_locker;
1203 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1204 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001205 frame_sp = exe_ctx.GetThreadPtr()->GetSelectedFrame ();
1206 sb_frame.SetFrameSP (frame_sp);
1207 }
Greg Claytonc9858e42012-04-06 02:17:47 +00001208 else
1209 {
1210 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001211 log->Printf ("SBThread(%p)::GetSelectedFrame() => error: process is running",
1212 static_cast<void*>(exe_ctx.GetThreadPtr()));
Greg Claytonc9858e42012-04-06 02:17:47 +00001213 }
Greg Claytonaf67cec2010-12-20 20:49:23 +00001214 }
Greg Claytonf028a1f2010-12-17 02:26:24 +00001215
1216 if (log)
1217 {
Greg Clayton481cef22011-01-21 06:11:58 +00001218 SBStream frame_desc_strm;
1219 sb_frame.GetDescription (frame_desc_strm);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001220 log->Printf ("SBThread(%p)::GetSelectedFrame () => SBFrame(%p): %s",
1221 static_cast<void*>(exe_ctx.GetThreadPtr()),
1222 static_cast<void*>(frame_sp.get()),
1223 frame_desc_strm.GetData());
Greg Claytonf028a1f2010-12-17 02:26:24 +00001224 }
1225
1226 return sb_frame;
1227}
1228
1229lldb::SBFrame
1230SBThread::SetSelectedFrame (uint32_t idx)
1231{
Greg Clayton5160ce52013-03-27 23:08:40 +00001232 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytonf028a1f2010-12-17 02:26:24 +00001233
1234 SBFrame sb_frame;
Jason Molendab57e4a12013-11-04 09:33:30 +00001235 StackFrameSP frame_sp;
Jim Ingham4fc6cb92012-08-22 21:34:33 +00001236 Mutex::Locker api_locker;
1237 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1238
Greg Clayton1ac04c32012-02-21 00:09:25 +00001239 if (exe_ctx.HasThreadScope())
Greg Claytonf028a1f2010-12-17 02:26:24 +00001240 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001241 Process::StopLocker stop_locker;
1242 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
Greg Claytonf028a1f2010-12-17 02:26:24 +00001243 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001244 Thread *thread = exe_ctx.GetThreadPtr();
1245 frame_sp = thread->GetStackFrameAtIndex (idx);
1246 if (frame_sp)
1247 {
1248 thread->SetSelectedFrame (frame_sp.get());
1249 sb_frame.SetFrameSP (frame_sp);
1250 }
Greg Claytonf028a1f2010-12-17 02:26:24 +00001251 }
Greg Claytonc9858e42012-04-06 02:17:47 +00001252 else
1253 {
1254 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001255 log->Printf ("SBThread(%p)::SetSelectedFrame() => error: process is running",
1256 static_cast<void*>(exe_ctx.GetThreadPtr()));
Greg Claytonc9858e42012-04-06 02:17:47 +00001257 }
Greg Claytonf028a1f2010-12-17 02:26:24 +00001258 }
1259
1260 if (log)
1261 {
Greg Clayton481cef22011-01-21 06:11:58 +00001262 SBStream frame_desc_strm;
1263 sb_frame.GetDescription (frame_desc_strm);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001264 log->Printf ("SBThread(%p)::SetSelectedFrame (idx=%u) => SBFrame(%p): %s",
1265 static_cast<void*>(exe_ctx.GetThreadPtr()), idx,
1266 static_cast<void*>(frame_sp.get()),
1267 frame_desc_strm.GetData());
Greg Claytonf028a1f2010-12-17 02:26:24 +00001268 }
1269 return sb_frame;
1270}
1271
Jim Ingham4f465cf2012-10-10 18:32:14 +00001272bool
1273SBThread::EventIsThreadEvent (const SBEvent &event)
1274{
1275 return Thread::ThreadEventData::GetEventDataFromEvent(event.get()) != NULL;
1276}
1277
1278SBFrame
1279SBThread::GetStackFrameFromEvent (const SBEvent &event)
1280{
1281 return Thread::ThreadEventData::GetStackFrameFromEvent (event.get());
1282
1283}
1284
1285SBThread
1286SBThread::GetThreadFromEvent (const SBEvent &event)
1287{
1288 return Thread::ThreadEventData::GetThreadFromEvent (event.get());
1289}
Greg Claytonf028a1f2010-12-17 02:26:24 +00001290
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001291bool
1292SBThread::operator == (const SBThread &rhs) const
1293{
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001294 return m_opaque_sp->GetThreadSP().get() == rhs.m_opaque_sp->GetThreadSP().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001295}
1296
1297bool
1298SBThread::operator != (const SBThread &rhs) const
1299{
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001300 return m_opaque_sp->GetThreadSP().get() != rhs.m_opaque_sp->GetThreadSP().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001301}
Caroline Ticedde9cff2010-09-20 05:20:02 +00001302
1303bool
Jim Ingham4f465cf2012-10-10 18:32:14 +00001304SBThread::GetStatus (SBStream &status) const
1305{
1306 Stream &strm = status.ref();
1307
1308 ExecutionContext exe_ctx (m_opaque_sp.get());
1309 if (exe_ctx.HasThreadScope())
1310 {
1311 exe_ctx.GetThreadPtr()->GetStatus(strm, 0, 1, 1);
1312 }
1313 else
1314 strm.PutCString ("No status");
1315
1316 return true;
1317}
1318
1319bool
Caroline Ticeceb6b132010-10-26 03:11:13 +00001320SBThread::GetDescription (SBStream &description) const
1321{
Greg Claytonda7bc7d2011-11-13 06:57:31 +00001322 Stream &strm = description.ref();
1323
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001324 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Clayton1ac04c32012-02-21 00:09:25 +00001325 if (exe_ctx.HasThreadScope())
Caroline Ticeceb6b132010-10-26 03:11:13 +00001326 {
Daniel Malead01b2952012-11-29 21:49:15 +00001327 strm.Printf("SBThread: tid = 0x%4.4" PRIx64, exe_ctx.GetThreadPtr()->GetID());
Caroline Ticeceb6b132010-10-26 03:11:13 +00001328 }
1329 else
Greg Claytonda7bc7d2011-11-13 06:57:31 +00001330 strm.PutCString ("No value");
Caroline Ticeceb6b132010-10-26 03:11:13 +00001331
1332 return true;
1333}
Jason Molenda5dd49162013-11-06 00:04:44 +00001334
1335SBThread
Jason Molenda008c45f2013-11-12 23:33:32 +00001336SBThread::GetExtendedBacktraceThread (const char *type)
Jason Molenda5dd49162013-11-06 00:04:44 +00001337{
1338 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1339 Mutex::Locker api_locker;
1340 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1341 SBThread sb_origin_thread;
1342
1343 if (exe_ctx.HasThreadScope())
1344 {
1345 Process::StopLocker stop_locker;
1346 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1347 {
Jason Molenda7a2f7902013-11-11 05:19:34 +00001348 ThreadSP real_thread(exe_ctx.GetThreadSP());
Jason Molenda5dd49162013-11-06 00:04:44 +00001349 if (real_thread)
1350 {
1351 ConstString type_const (type);
Jason Molenda7a2f7902013-11-11 05:19:34 +00001352 Process *process = exe_ctx.GetProcessPtr();
1353 if (process)
Jason Molenda5dd49162013-11-06 00:04:44 +00001354 {
Jason Molenda7a2f7902013-11-11 05:19:34 +00001355 SystemRuntime *runtime = process->GetSystemRuntime();
1356 if (runtime)
1357 {
Jason Molenda008c45f2013-11-12 23:33:32 +00001358 ThreadSP new_thread_sp (runtime->GetExtendedBacktraceThread (real_thread, type_const));
Jason Molendaa6e91302013-11-19 05:44:41 +00001359 if (new_thread_sp)
1360 {
1361 // Save this in the Process' ExtendedThreadList so a strong pointer retains the
1362 // object.
1363 process->GetExtendedThreadList().AddThread (new_thread_sp);
1364 sb_origin_thread.SetThread (new_thread_sp);
1365 if (log)
1366 {
1367 const char *queue_name = new_thread_sp->GetQueueName();
1368 if (queue_name == NULL)
1369 queue_name = "";
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001370 log->Printf ("SBThread(%p)::GetExtendedBacktraceThread() => new extended Thread created (%p) with queue_id 0x%" PRIx64 " queue name '%s'",
1371 static_cast<void*>(exe_ctx.GetThreadPtr()),
1372 static_cast<void*>(new_thread_sp.get()),
1373 new_thread_sp->GetQueueID(),
1374 queue_name);
Jason Molendaa6e91302013-11-19 05:44:41 +00001375 }
1376 }
Jason Molenda7a2f7902013-11-11 05:19:34 +00001377 }
Jason Molenda5dd49162013-11-06 00:04:44 +00001378 }
1379 }
1380 }
1381 else
1382 {
1383 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001384 log->Printf ("SBThread(%p)::GetExtendedBacktraceThread() => error: process is running",
1385 static_cast<void*>(exe_ctx.GetThreadPtr()));
Jason Molenda5dd49162013-11-06 00:04:44 +00001386 }
1387 }
1388
Jason Molendaac605f42014-03-08 01:34:55 +00001389 if (log && sb_origin_thread.IsValid() == false)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001390 log->Printf("SBThread(%p)::GetExtendedBacktraceThread() is not returning a Valid thread",
1391 static_cast<void*>(exe_ctx.GetThreadPtr()));
Jason Molenda5dd49162013-11-06 00:04:44 +00001392 return sb_origin_thread;
1393}
Jason Molenda8ee9cb52013-11-16 01:24:22 +00001394
1395uint32_t
1396SBThread::GetExtendedBacktraceOriginatingIndexID ()
1397{
1398 ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
1399 if (thread_sp)
1400 return thread_sp->GetExtendedBacktraceOriginatingIndexID();
1401 return LLDB_INVALID_INDEX32;
1402}