blob: a0bfa4313535b72529f5312056d4df74c78244bd [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"
Jason Molenda705b1802014-06-13 02:37:02 +000022#include "lldb/Core/StructuredData.h"
Greg Clayton66111032010-06-23 01:19:29 +000023#include "lldb/Interpreter/CommandInterpreter.h"
Jason Molenda5dd49162013-11-06 00:04:44 +000024#include "lldb/Target/SystemRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025#include "lldb/Target/Thread.h"
26#include "lldb/Target/Process.h"
Jason Molendab9ffa982014-04-25 00:01:15 +000027#include "lldb/Target/Queue.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000028#include "lldb/Symbol/SymbolContext.h"
29#include "lldb/Symbol/CompileUnit.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000030#include "lldb/Target/StopInfo.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031#include "lldb/Target/Target.h"
32#include "lldb/Target/ThreadPlan.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033#include "lldb/Target/ThreadPlanStepInstruction.h"
34#include "lldb/Target/ThreadPlanStepOut.h"
35#include "lldb/Target/ThreadPlanStepRange.h"
36#include "lldb/Target/ThreadPlanStepInRange.h"
37
38
Eli Friedman4c5de692010-06-09 07:44:37 +000039#include "lldb/API/SBAddress.h"
Eli Friedman4c5de692010-06-09 07:44:37 +000040#include "lldb/API/SBDebugger.h"
Jim Ingham4f465cf2012-10-10 18:32:14 +000041#include "lldb/API/SBEvent.h"
Jim Ingham73ca05a2011-12-17 01:35:57 +000042#include "lldb/API/SBFrame.h"
Eli Friedman4c5de692010-06-09 07:44:37 +000043#include "lldb/API/SBProcess.h"
Jim Ingham73ca05a2011-12-17 01:35:57 +000044#include "lldb/API/SBValue.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000045
46using namespace lldb;
47using namespace lldb_private;
48
Jim Ingham4f465cf2012-10-10 18:32:14 +000049const char *
50SBThread::GetBroadcasterClassName ()
51{
52 return Thread::GetStaticBroadcasterClass().AsCString();
53}
54
Greg Claytoncfd1ace2010-10-31 03:01:06 +000055//----------------------------------------------------------------------
56// Constructors
57//----------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +000058SBThread::SBThread () :
Greg Clayton7fdf9ef2012-04-05 16:12:35 +000059 m_opaque_sp (new ExecutionContextRef())
Chris Lattner30fdc8d2010-06-08 16:52:24 +000060{
61}
62
Chris Lattner30fdc8d2010-06-08 16:52:24 +000063SBThread::SBThread (const ThreadSP& lldb_object_sp) :
Greg Clayton7fdf9ef2012-04-05 16:12:35 +000064 m_opaque_sp (new ExecutionContextRef(lldb_object_sp))
Chris Lattner30fdc8d2010-06-08 16:52:24 +000065{
66}
67
Greg Clayton92ef5732010-10-30 18:26:59 +000068SBThread::SBThread (const SBThread &rhs) :
Greg Clayton7fdf9ef2012-04-05 16:12:35 +000069 m_opaque_sp (new ExecutionContextRef(*rhs.m_opaque_sp))
Chris Lattner30fdc8d2010-06-08 16:52:24 +000070{
Greg Clayton7fdf9ef2012-04-05 16:12:35 +000071
Chris Lattner30fdc8d2010-06-08 16:52:24 +000072}
73
74//----------------------------------------------------------------------
Greg Claytoncfd1ace2010-10-31 03:01:06 +000075// Assignment operator
76//----------------------------------------------------------------------
77
78const lldb::SBThread &
79SBThread::operator = (const SBThread &rhs)
80{
81 if (this != &rhs)
Greg Clayton7fdf9ef2012-04-05 16:12:35 +000082 *m_opaque_sp = *rhs.m_opaque_sp;
Greg Claytoncfd1ace2010-10-31 03:01:06 +000083 return *this;
84}
85
86//----------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +000087// Destructor
88//----------------------------------------------------------------------
89SBThread::~SBThread()
90{
91}
92
Jason Molendab9ffa982014-04-25 00:01:15 +000093lldb::SBQueue
94SBThread::GetQueue () const
95{
96 SBQueue sb_queue;
97 QueueSP queue_sp;
98 Mutex::Locker api_locker;
99 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
100
101 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
102 if (exe_ctx.HasThreadScope())
103 {
104 Process::StopLocker stop_locker;
105 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
106 {
107 queue_sp = exe_ctx.GetThreadPtr()->GetQueue();
108 if (queue_sp)
109 {
110 sb_queue.SetQueue (queue_sp);
111 }
112 }
113 else
114 {
115 if (log)
116 log->Printf ("SBThread(%p)::GetQueueKind() => error: process is running",
117 static_cast<void*>(exe_ctx.GetThreadPtr()));
118 }
119 }
120
121 if (log)
122 log->Printf ("SBThread(%p)::GetQueueKind () => SBQueue(%p)",
123 static_cast<void*>(exe_ctx.GetThreadPtr()), static_cast<void*>(queue_sp.get()));
124
125 return sb_queue;
126}
127
128
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000129bool
130SBThread::IsValid() const
131{
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000132 return m_opaque_sp->GetThreadSP().get() != NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000133}
134
Greg Clayton48e42542010-07-30 20:12:55 +0000135void
136SBThread::Clear ()
137{
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000138 m_opaque_sp->Clear();
Greg Clayton48e42542010-07-30 20:12:55 +0000139}
140
141
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000142StopReason
143SBThread::GetStopReason()
144{
Greg Clayton5160ce52013-03-27 23:08:40 +0000145 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000146
Caroline Ticeceb6b132010-10-26 03:11:13 +0000147 StopReason reason = eStopReasonInvalid;
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000148 Mutex::Locker api_locker;
149 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
150
Greg Clayton1ac04c32012-02-21 00:09:25 +0000151 if (exe_ctx.HasThreadScope())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000152 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000153 Process::StopLocker stop_locker;
154 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
155 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000156 return exe_ctx.GetThreadPtr()->GetStopReason();
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000157 }
Greg Claytonc9858e42012-04-06 02:17:47 +0000158 else
159 {
160 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000161 log->Printf ("SBThread(%p)::GetStopReason() => error: process is running",
162 static_cast<void*>(exe_ctx.GetThreadPtr()));
Greg Claytonc9858e42012-04-06 02:17:47 +0000163 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000164 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000165
166 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000167 log->Printf ("SBThread(%p)::GetStopReason () => %s",
168 static_cast<void*>(exe_ctx.GetThreadPtr()),
Caroline Tice750cd172010-10-26 23:49:36 +0000169 Thread::StopReasonAsCString (reason));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000170
171 return reason;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000172}
173
174size_t
Greg Clayton4e78f602010-11-18 18:52:36 +0000175SBThread::GetStopReasonDataCount ()
176{
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000177 Mutex::Locker api_locker;
178 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
179
Greg Clayton1ac04c32012-02-21 00:09:25 +0000180 if (exe_ctx.HasThreadScope())
Greg Clayton4e78f602010-11-18 18:52:36 +0000181 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000182 Process::StopLocker stop_locker;
183 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
Greg Clayton4e78f602010-11-18 18:52:36 +0000184 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000185 StopInfoSP stop_info_sp = exe_ctx.GetThreadPtr()->GetStopInfo ();
186 if (stop_info_sp)
Greg Clayton4e78f602010-11-18 18:52:36 +0000187 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000188 StopReason reason = stop_info_sp->GetStopReason();
189 switch (reason)
Greg Clayton4e78f602010-11-18 18:52:36 +0000190 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000191 case eStopReasonInvalid:
192 case eStopReasonNone:
193 case eStopReasonTrace:
Greg Clayton90ba8112012-12-05 00:16:59 +0000194 case eStopReasonExec:
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000195 case eStopReasonPlanComplete:
Andrew Kaylorf85defa2012-12-20 23:08:03 +0000196 case eStopReasonThreadExiting:
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000197 // There is no data for these stop reasons.
198 return 0;
199
200 case eStopReasonBreakpoint:
201 {
202 break_id_t site_id = stop_info_sp->GetValue();
203 lldb::BreakpointSiteSP bp_site_sp (exe_ctx.GetProcessPtr()->GetBreakpointSiteList().FindByID (site_id));
204 if (bp_site_sp)
205 return bp_site_sp->GetNumberOfOwners () * 2;
206 else
207 return 0; // Breakpoint must have cleared itself...
208 }
209 break;
210
211 case eStopReasonWatchpoint:
212 return 1;
213
214 case eStopReasonSignal:
215 return 1;
216
217 case eStopReasonException:
218 return 1;
Greg Clayton4e78f602010-11-18 18:52:36 +0000219 }
Greg Clayton4e78f602010-11-18 18:52:36 +0000220 }
221 }
Greg Claytonc9858e42012-04-06 02:17:47 +0000222 else
223 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000224 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytonc9858e42012-04-06 02:17:47 +0000225 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000226 log->Printf ("SBThread(%p)::GetStopReasonDataCount() => error: process is running",
227 static_cast<void*>(exe_ctx.GetThreadPtr()));
Greg Claytonc9858e42012-04-06 02:17:47 +0000228 }
Greg Clayton4e78f602010-11-18 18:52:36 +0000229 }
230 return 0;
231}
232
233uint64_t
234SBThread::GetStopReasonDataAtIndex (uint32_t idx)
235{
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000236 Mutex::Locker api_locker;
237 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
238
Greg Clayton1ac04c32012-02-21 00:09:25 +0000239 if (exe_ctx.HasThreadScope())
Greg Clayton4e78f602010-11-18 18:52:36 +0000240 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000241 Process::StopLocker stop_locker;
242 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
Greg Clayton4e78f602010-11-18 18:52:36 +0000243 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000244 Thread *thread = exe_ctx.GetThreadPtr();
245 StopInfoSP stop_info_sp = thread->GetStopInfo ();
246 if (stop_info_sp)
247 {
248 StopReason reason = stop_info_sp->GetStopReason();
249 switch (reason)
Greg Clayton4e78f602010-11-18 18:52:36 +0000250 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000251 case eStopReasonInvalid:
252 case eStopReasonNone:
253 case eStopReasonTrace:
Greg Clayton90ba8112012-12-05 00:16:59 +0000254 case eStopReasonExec:
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000255 case eStopReasonPlanComplete:
Andrew Kaylorf85defa2012-12-20 23:08:03 +0000256 case eStopReasonThreadExiting:
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000257 // There is no data for these stop reasons.
258 return 0;
259
260 case eStopReasonBreakpoint:
Greg Clayton4e78f602010-11-18 18:52:36 +0000261 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000262 break_id_t site_id = stop_info_sp->GetValue();
263 lldb::BreakpointSiteSP bp_site_sp (exe_ctx.GetProcessPtr()->GetBreakpointSiteList().FindByID (site_id));
264 if (bp_site_sp)
Greg Clayton4e78f602010-11-18 18:52:36 +0000265 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000266 uint32_t bp_index = idx / 2;
267 BreakpointLocationSP bp_loc_sp (bp_site_sp->GetOwnerAtIndex (bp_index));
268 if (bp_loc_sp)
Greg Clayton4e78f602010-11-18 18:52:36 +0000269 {
Greg Clayton8334e142014-04-11 17:27:02 +0000270 if (idx & 1)
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000271 {
272 // Odd idx, return the breakpoint location ID
273 return bp_loc_sp->GetID();
274 }
275 else
276 {
277 // Even idx, return the breakpoint ID
278 return bp_loc_sp->GetBreakpoint().GetID();
279 }
Greg Clayton4e78f602010-11-18 18:52:36 +0000280 }
281 }
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000282 return LLDB_INVALID_BREAK_ID;
Greg Clayton4e78f602010-11-18 18:52:36 +0000283 }
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000284 break;
285
286 case eStopReasonWatchpoint:
287 return stop_info_sp->GetValue();
288
289 case eStopReasonSignal:
290 return stop_info_sp->GetValue();
291
292 case eStopReasonException:
293 return stop_info_sp->GetValue();
Greg Clayton4e78f602010-11-18 18:52:36 +0000294 }
Greg Clayton4e78f602010-11-18 18:52:36 +0000295 }
296 }
Greg Claytonc9858e42012-04-06 02:17:47 +0000297 else
298 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000299 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytonc9858e42012-04-06 02:17:47 +0000300 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000301 log->Printf ("SBThread(%p)::GetStopReasonDataAtIndex() => error: process is running",
302 static_cast<void*>(exe_ctx.GetThreadPtr()));
Greg Claytonc9858e42012-04-06 02:17:47 +0000303 }
Greg Clayton4e78f602010-11-18 18:52:36 +0000304 }
305 return 0;
306}
307
308size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000309SBThread::GetStopDescription (char *dst, size_t dst_len)
310{
Greg Clayton5160ce52013-03-27 23:08:40 +0000311 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000312
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000313 Mutex::Locker api_locker;
314 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
315
Greg Clayton1ac04c32012-02-21 00:09:25 +0000316 if (exe_ctx.HasThreadScope())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000317 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000318 Process::StopLocker stop_locker;
319 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000320 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000321
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000322 StopInfoSP stop_info_sp = exe_ctx.GetThreadPtr()->GetStopInfo ();
323 if (stop_info_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000324 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000325 const char *stop_desc = stop_info_sp->GetDescription();
326 if (stop_desc)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000327 {
Caroline Ticeceb6b132010-10-26 03:11:13 +0000328 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000329 log->Printf ("SBThread(%p)::GetStopDescription (dst, dst_len) => \"%s\"",
330 static_cast<void*>(exe_ctx.GetThreadPtr()),
331 stop_desc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000332 if (dst)
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000333 return ::snprintf (dst, dst_len, "%s", stop_desc);
334 else
335 {
336 // NULL dst passed in, return the length needed to contain the description
337 return ::strlen (stop_desc) + 1; // Include the NULL byte for size
338 }
339 }
340 else
341 {
342 size_t stop_desc_len = 0;
343 switch (stop_info_sp->GetStopReason())
344 {
345 case eStopReasonTrace:
346 case eStopReasonPlanComplete:
347 {
348 static char trace_desc[] = "step";
349 stop_desc = trace_desc;
350 stop_desc_len = sizeof(trace_desc); // Include the NULL byte for size
351 }
352 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000353
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000354 case eStopReasonBreakpoint:
355 {
356 static char bp_desc[] = "breakpoint hit";
357 stop_desc = bp_desc;
358 stop_desc_len = sizeof(bp_desc); // Include the NULL byte for size
359 }
360 break;
361
362 case eStopReasonWatchpoint:
363 {
364 static char wp_desc[] = "watchpoint hit";
365 stop_desc = wp_desc;
366 stop_desc_len = sizeof(wp_desc); // Include the NULL byte for size
367 }
368 break;
369
370 case eStopReasonSignal:
371 {
372 stop_desc = exe_ctx.GetProcessPtr()->GetUnixSignals ().GetSignalAsCString (stop_info_sp->GetValue());
373 if (stop_desc == NULL || stop_desc[0] == '\0')
374 {
375 static char signal_desc[] = "signal";
376 stop_desc = signal_desc;
377 stop_desc_len = sizeof(signal_desc); // Include the NULL byte for size
378 }
379 }
380 break;
381
382 case eStopReasonException:
383 {
384 char exc_desc[] = "exception";
385 stop_desc = exc_desc;
386 stop_desc_len = sizeof(exc_desc); // Include the NULL byte for size
387 }
388 break;
389
Greg Clayton90ba8112012-12-05 00:16:59 +0000390 case eStopReasonExec:
391 {
392 char exc_desc[] = "exec";
393 stop_desc = exc_desc;
394 stop_desc_len = sizeof(exc_desc); // Include the NULL byte for size
395 }
396 break;
397
Andrew Kaylorf85defa2012-12-20 23:08:03 +0000398 case eStopReasonThreadExiting:
399 {
400 char limbo_desc[] = "thread exiting";
401 stop_desc = limbo_desc;
402 stop_desc_len = sizeof(limbo_desc);
403 }
404 break;
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000405 default:
406 break;
407 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000408
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000409 if (stop_desc && stop_desc[0])
410 {
411 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000412 log->Printf ("SBThread(%p)::GetStopDescription (dst, dst_len) => '%s'",
413 static_cast<void*>(exe_ctx.GetThreadPtr()),
414 stop_desc);
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000415
416 if (dst)
417 return ::snprintf (dst, dst_len, "%s", stop_desc) + 1; // Include the NULL byte
418
419 if (stop_desc_len == 0)
420 stop_desc_len = ::strlen (stop_desc) + 1; // Include the NULL byte
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000421
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000422 return stop_desc_len;
423 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000424 }
425 }
426 }
Greg Claytonc9858e42012-04-06 02:17:47 +0000427 else
428 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000429 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytonc9858e42012-04-06 02:17:47 +0000430 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000431 log->Printf ("SBThread(%p)::GetStopDescription() => error: process is running",
432 static_cast<void*>(exe_ctx.GetThreadPtr()));
Greg Claytonc9858e42012-04-06 02:17:47 +0000433 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000434 }
435 if (dst)
436 *dst = 0;
437 return 0;
438}
439
Jim Ingham73ca05a2011-12-17 01:35:57 +0000440SBValue
441SBThread::GetStopReturnValue ()
442{
Greg Clayton5160ce52013-03-27 23:08:40 +0000443 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Ingham73ca05a2011-12-17 01:35:57 +0000444 ValueObjectSP return_valobj_sp;
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000445 Mutex::Locker api_locker;
446 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
447
Greg Clayton1ac04c32012-02-21 00:09:25 +0000448 if (exe_ctx.HasThreadScope())
Jim Ingham73ca05a2011-12-17 01:35:57 +0000449 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000450 Process::StopLocker stop_locker;
451 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
Jim Ingham73ca05a2011-12-17 01:35:57 +0000452 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000453 StopInfoSP stop_info_sp = exe_ctx.GetThreadPtr()->GetStopInfo ();
454 if (stop_info_sp)
455 {
456 return_valobj_sp = StopInfo::GetReturnValueObject (stop_info_sp);
457 }
Jim Ingham73ca05a2011-12-17 01:35:57 +0000458 }
Greg Claytonc9858e42012-04-06 02:17:47 +0000459 else
460 {
461 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000462 log->Printf ("SBThread(%p)::GetStopReturnValue() => error: process is running",
463 static_cast<void*>(exe_ctx.GetThreadPtr()));
Greg Claytonc9858e42012-04-06 02:17:47 +0000464 }
Jim Ingham73ca05a2011-12-17 01:35:57 +0000465 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000466
Jim Ingham73ca05a2011-12-17 01:35:57 +0000467 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000468 log->Printf ("SBThread(%p)::GetStopReturnValue () => %s",
469 static_cast<void*>(exe_ctx.GetThreadPtr()),
470 return_valobj_sp.get()
471 ? return_valobj_sp->GetValueAsCString()
472 : "<no return value>");
473
Jim Ingham73ca05a2011-12-17 01:35:57 +0000474 return SBValue (return_valobj_sp);
475}
476
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000477void
478SBThread::SetThread (const ThreadSP& lldb_object_sp)
479{
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000480 m_opaque_sp->SetThreadSP (lldb_object_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000481}
482
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000483lldb::tid_t
484SBThread::GetThreadID () const
485{
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000486 ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
Greg Clayton17a6ad02012-01-30 02:53:15 +0000487 if (thread_sp)
Greg Clayton1ac04c32012-02-21 00:09:25 +0000488 return thread_sp->GetID();
489 return LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000490}
491
492uint32_t
493SBThread::GetIndexID () const
494{
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000495 ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
Greg Clayton17a6ad02012-01-30 02:53:15 +0000496 if (thread_sp)
497 return thread_sp->GetIndexID();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000498 return LLDB_INVALID_INDEX32;
499}
Greg Clayton1ac04c32012-02-21 00:09:25 +0000500
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000501const char *
502SBThread::GetName () const
503{
Greg Clayton5160ce52013-03-27 23:08:40 +0000504 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton48381312010-10-30 04:51:46 +0000505 const char *name = NULL;
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000506 Mutex::Locker api_locker;
507 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
508
Greg Clayton1ac04c32012-02-21 00:09:25 +0000509 if (exe_ctx.HasThreadScope())
Greg Claytonaf67cec2010-12-20 20:49:23 +0000510 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000511 Process::StopLocker stop_locker;
512 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
513 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000514 name = exe_ctx.GetThreadPtr()->GetName();
515 }
Greg Claytonc9858e42012-04-06 02:17:47 +0000516 else
517 {
518 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000519 log->Printf ("SBThread(%p)::GetName() => error: process is running",
520 static_cast<void*>(exe_ctx.GetThreadPtr()));
Greg Claytonc9858e42012-04-06 02:17:47 +0000521 }
Greg Claytonaf67cec2010-12-20 20:49:23 +0000522 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000523
Caroline Ticeceb6b132010-10-26 03:11:13 +0000524 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000525 log->Printf ("SBThread(%p)::GetName () => %s",
526 static_cast<void*>(exe_ctx.GetThreadPtr()),
527 name ? name : "NULL");
Caroline Ticeceb6b132010-10-26 03:11:13 +0000528
Greg Clayton48381312010-10-30 04:51:46 +0000529 return name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000530}
531
532const char *
533SBThread::GetQueueName () const
534{
Greg Clayton48381312010-10-30 04:51:46 +0000535 const char *name = NULL;
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000536 Mutex::Locker api_locker;
537 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
538
Greg Clayton5160ce52013-03-27 23:08:40 +0000539 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton1ac04c32012-02-21 00:09:25 +0000540 if (exe_ctx.HasThreadScope())
Greg Claytonaf67cec2010-12-20 20:49:23 +0000541 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000542 Process::StopLocker stop_locker;
543 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
544 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000545 name = exe_ctx.GetThreadPtr()->GetQueueName();
546 }
Greg Claytonc9858e42012-04-06 02:17:47 +0000547 else
548 {
549 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000550 log->Printf ("SBThread(%p)::GetQueueName() => error: process is running",
551 static_cast<void*>(exe_ctx.GetThreadPtr()));
Greg Claytonc9858e42012-04-06 02:17:47 +0000552 }
Greg Claytonaf67cec2010-12-20 20:49:23 +0000553 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000554
Caroline Ticeceb6b132010-10-26 03:11:13 +0000555 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000556 log->Printf ("SBThread(%p)::GetQueueName () => %s",
557 static_cast<void*>(exe_ctx.GetThreadPtr()),
558 name ? name : "NULL");
Caroline Ticeceb6b132010-10-26 03:11:13 +0000559
Greg Clayton48381312010-10-30 04:51:46 +0000560 return name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000561}
562
Jason Molenda4fdb5862013-10-21 23:52:54 +0000563lldb::queue_id_t
564SBThread::GetQueueID () const
565{
566 queue_id_t id = LLDB_INVALID_QUEUE_ID;
567 Mutex::Locker api_locker;
568 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
569
570 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
571 if (exe_ctx.HasThreadScope())
572 {
573 Process::StopLocker stop_locker;
574 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
575 {
576 id = exe_ctx.GetThreadPtr()->GetQueueID();
577 }
578 else
579 {
580 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000581 log->Printf ("SBThread(%p)::GetQueueID() => error: process is running",
582 static_cast<void*>(exe_ctx.GetThreadPtr()));
Jason Molenda4fdb5862013-10-21 23:52:54 +0000583 }
584 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000585
Jason Molenda4fdb5862013-10-21 23:52:54 +0000586 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000587 log->Printf ("SBThread(%p)::GetQueueID () => 0x%" PRIx64,
588 static_cast<void*>(exe_ctx.GetThreadPtr()), id);
Jason Molenda4fdb5862013-10-21 23:52:54 +0000589
590 return id;
591}
592
Jason Molenda705b1802014-06-13 02:37:02 +0000593bool
594SBThread::GetInfoItemByPathAsString (const char *path, SBStream &strm)
595{
596 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
597 bool success = false;
598 Mutex::Locker api_locker;
599 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
600
601 if (exe_ctx.HasThreadScope())
602 {
603 Process::StopLocker stop_locker;
604 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
605 {
606 Thread *thread = exe_ctx.GetThreadPtr();
607 StructuredData::ObjectSP info_root_sp = thread->GetExtendedInfo();
608 if (info_root_sp)
609 {
610 StructuredData::ObjectSP node = info_root_sp->GetObjectForDotSeparatedPath (path);
611 if (node)
612 {
613 if (node->GetType() == StructuredData::Type::eTypeString)
614 {
615 strm.Printf ("%s", node->GetAsString()->GetValue().c_str());
616 success = true;
617 }
618 if (node->GetType() == StructuredData::Type::eTypeInteger)
619 {
620 strm.Printf ("0x%" PRIx64, node->GetAsInteger()->GetValue());
621 success = true;
622 }
623 if (node->GetType() == StructuredData::Type::eTypeFloat)
624 {
625 strm.Printf ("0x%f", node->GetAsFloat()->GetValue());
626 success = true;
627 }
628 if (node->GetType() == StructuredData::Type::eTypeBoolean)
629 {
630 if (node->GetAsBoolean()->GetValue() == true)
631 strm.Printf ("true");
632 else
633 strm.Printf ("false");
634 success = true;
635 }
636 if (node->GetType() == StructuredData::Type::eTypeNull)
637 {
638 strm.Printf ("null");
639 success = true;
640 }
641 }
642 }
643 }
644 else
645 {
646 if (log)
647 log->Printf ("SBThread(%p)::GetInfoItemByPathAsString() => error: process is running",
648 static_cast<void*>(exe_ctx.GetThreadPtr()));
649 }
650 }
651
652 if (log)
653 log->Printf ("SBThread(%p)::GetInfoItemByPathAsString () => %s",
654 static_cast<void*>(exe_ctx.GetThreadPtr()),
655 strm.GetData());
656
657 return success;
658}
659
660
Jim Ingham64e7ead2012-05-03 21:19:36 +0000661SBError
662SBThread::ResumeNewPlan (ExecutionContext &exe_ctx, ThreadPlan *new_plan)
663{
664 SBError sb_error;
665
666 Process *process = exe_ctx.GetProcessPtr();
667 if (!process)
668 {
669 sb_error.SetErrorString("No process in SBThread::ResumeNewPlan");
670 return sb_error;
671 }
672
673 Thread *thread = exe_ctx.GetThreadPtr();
674 if (!thread)
675 {
676 sb_error.SetErrorString("No thread in SBThread::ResumeNewPlan");
677 return sb_error;
678 }
679
680 // User level plans should be Master Plans so they can be interrupted, other plans executed, and
681 // then a "continue" will resume the plan.
682 if (new_plan != NULL)
683 {
684 new_plan->SetIsMasterPlan(true);
685 new_plan->SetOkayToDiscard(false);
686 }
687
688 // Why do we need to set the current thread by ID here???
689 process->GetThreadList().SetSelectedThreadByID (thread->GetID());
690 sb_error.ref() = process->Resume();
691
692 if (sb_error.Success())
693 {
694 // If we are doing synchronous mode, then wait for the
695 // process to stop yet again!
696 if (process->GetTarget().GetDebugger().GetAsyncExecution () == false)
697 process->WaitForProcessToStop (NULL);
698 }
699
700 return sb_error;
701}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000702
703void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000704SBThread::StepOver (lldb::RunMode stop_other_threads)
705{
Greg Clayton5160ce52013-03-27 23:08:40 +0000706 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000707
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000708 Mutex::Locker api_locker;
709 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
710
Caroline Ticeceb6b132010-10-26 03:11:13 +0000711
Greg Clayton17a6ad02012-01-30 02:53:15 +0000712 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000713 log->Printf ("SBThread(%p)::StepOver (stop_other_threads='%s')",
714 static_cast<void*>(exe_ctx.GetThreadPtr()),
Greg Clayton17a6ad02012-01-30 02:53:15 +0000715 Thread::RunModeAsCString (stop_other_threads));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000716
Greg Clayton1ac04c32012-02-21 00:09:25 +0000717 if (exe_ctx.HasThreadScope())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000718 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000719 Thread *thread = exe_ctx.GetThreadPtr();
Jim Ingham7ba6e992012-05-11 23:47:32 +0000720 bool abort_other_plans = false;
Jason Molendab57e4a12013-11-04 09:33:30 +0000721 StackFrameSP frame_sp(thread->GetStackFrameAtIndex (0));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000722
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000723 ThreadPlanSP new_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000724 if (frame_sp)
725 {
726 if (frame_sp->HasDebugInformation ())
727 {
Jim Ingham4b4b2472014-03-13 02:47:14 +0000728 const LazyBool avoid_no_debug = eLazyBoolCalculate;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000729 SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000730 new_plan_sp = thread->QueueThreadPlanForStepOverRange (abort_other_plans,
Jim Inghamc6276822012-12-12 19:58:40 +0000731 sc.line_entry.range,
732 sc,
Jim Ingham4b4b2472014-03-13 02:47:14 +0000733 stop_other_threads,
734 avoid_no_debug);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000735 }
736 else
737 {
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000738 new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction (true,
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000739 abort_other_plans,
740 stop_other_threads);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000741 }
742 }
743
Jim Ingham64e7ead2012-05-03 21:19:36 +0000744 // This returns an error, we should use it!
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000745 ResumeNewPlan (exe_ctx, new_plan_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000746 }
747}
748
749void
750SBThread::StepInto (lldb::RunMode stop_other_threads)
751{
Jim Inghamc6276822012-12-12 19:58:40 +0000752 StepInto (NULL, stop_other_threads);
753}
754
755void
756SBThread::StepInto (const char *target_name, lldb::RunMode stop_other_threads)
757{
Greg Clayton5160ce52013-03-27 23:08:40 +0000758 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000759
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000760 Mutex::Locker api_locker;
761 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
Greg Clayton17a6ad02012-01-30 02:53:15 +0000762
763 if (log)
Jim Inghamc6276822012-12-12 19:58:40 +0000764 log->Printf ("SBThread(%p)::StepInto (target_name='%s', stop_other_threads='%s')",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000765 static_cast<void*>(exe_ctx.GetThreadPtr()),
Jim Inghamc6276822012-12-12 19:58:40 +0000766 target_name? target_name: "<NULL>",
Greg Clayton17a6ad02012-01-30 02:53:15 +0000767 Thread::RunModeAsCString (stop_other_threads));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000768
Greg Clayton1ac04c32012-02-21 00:09:25 +0000769 if (exe_ctx.HasThreadScope())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000770 {
Jim Ingham7ba6e992012-05-11 23:47:32 +0000771 bool abort_other_plans = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000772
Greg Clayton1ac04c32012-02-21 00:09:25 +0000773 Thread *thread = exe_ctx.GetThreadPtr();
Jason Molendab57e4a12013-11-04 09:33:30 +0000774 StackFrameSP frame_sp(thread->GetStackFrameAtIndex (0));
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000775 ThreadPlanSP new_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000776
777 if (frame_sp && frame_sp->HasDebugInformation ())
778 {
Jim Ingham4b4b2472014-03-13 02:47:14 +0000779 const LazyBool step_out_avoids_code_without_debug_info = eLazyBoolCalculate;
780 const LazyBool step_in_avoids_code_without_debug_info = eLazyBoolCalculate;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000781 SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000782 new_plan_sp = thread->QueueThreadPlanForStepInRange (abort_other_plans,
Jim Inghamc6276822012-12-12 19:58:40 +0000783 sc.line_entry.range,
784 sc,
785 target_name,
786 stop_other_threads,
Jim Ingham4b4b2472014-03-13 02:47:14 +0000787 step_in_avoids_code_without_debug_info,
788 step_out_avoids_code_without_debug_info);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000789 }
790 else
791 {
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000792 new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction (false,
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000793 abort_other_plans,
794 stop_other_threads);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000795 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000796
Jim Ingham64e7ead2012-05-03 21:19:36 +0000797 // This returns an error, we should use it!
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000798 ResumeNewPlan (exe_ctx, new_plan_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000799 }
800}
801
802void
803SBThread::StepOut ()
804{
Greg Clayton5160ce52013-03-27 23:08:40 +0000805 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000806
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000807 Mutex::Locker api_locker;
808 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
809
Caroline Ticeceb6b132010-10-26 03:11:13 +0000810
Greg Clayton17a6ad02012-01-30 02:53:15 +0000811 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000812 log->Printf ("SBThread(%p)::StepOut ()",
813 static_cast<void*>(exe_ctx.GetThreadPtr()));
814
Greg Clayton1ac04c32012-02-21 00:09:25 +0000815 if (exe_ctx.HasThreadScope())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000816 {
Jim Ingham7ba6e992012-05-11 23:47:32 +0000817 bool abort_other_plans = false;
Jim Ingham94b09242012-09-14 21:07:14 +0000818 bool stop_other_threads = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000819
Greg Clayton1ac04c32012-02-21 00:09:25 +0000820 Thread *thread = exe_ctx.GetThreadPtr();
821
Jim Ingham4b4b2472014-03-13 02:47:14 +0000822 const LazyBool avoid_no_debug = eLazyBoolCalculate;
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000823 ThreadPlanSP new_plan_sp(thread->QueueThreadPlanForStepOut (abort_other_plans,
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000824 NULL,
825 false,
826 stop_other_threads,
827 eVoteYes,
828 eVoteNoOpinion,
829 0,
830 avoid_no_debug));
831
Jim Ingham64e7ead2012-05-03 21:19:36 +0000832 // This returns an error, we should use it!
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000833 ResumeNewPlan (exe_ctx, new_plan_sp.get());
Greg Clayton481cef22011-01-21 06:11:58 +0000834 }
835}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000836
Greg Clayton481cef22011-01-21 06:11:58 +0000837void
838SBThread::StepOutOfFrame (lldb::SBFrame &sb_frame)
839{
Greg Clayton5160ce52013-03-27 23:08:40 +0000840 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton481cef22011-01-21 06:11:58 +0000841
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000842 Mutex::Locker api_locker;
843 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
844
Jason Molendab57e4a12013-11-04 09:33:30 +0000845 StackFrameSP frame_sp (sb_frame.GetFrameSP());
Greg Clayton481cef22011-01-21 06:11:58 +0000846 if (log)
847 {
848 SBStream frame_desc_strm;
849 sb_frame.GetDescription (frame_desc_strm);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000850 log->Printf ("SBThread(%p)::StepOutOfFrame (frame = SBFrame(%p): %s)",
851 static_cast<void*>(exe_ctx.GetThreadPtr()),
852 static_cast<void*>(frame_sp.get()),
853 frame_desc_strm.GetData());
Greg Clayton481cef22011-01-21 06:11:58 +0000854 }
855
Greg Clayton1ac04c32012-02-21 00:09:25 +0000856 if (exe_ctx.HasThreadScope())
Greg Clayton481cef22011-01-21 06:11:58 +0000857 {
Jim Ingham7ba6e992012-05-11 23:47:32 +0000858 bool abort_other_plans = false;
Jim Ingham94b09242012-09-14 21:07:14 +0000859 bool stop_other_threads = false;
Greg Clayton1ac04c32012-02-21 00:09:25 +0000860 Thread *thread = exe_ctx.GetThreadPtr();
Greg Clayton481cef22011-01-21 06:11:58 +0000861
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000862 ThreadPlanSP new_plan_sp(thread->QueueThreadPlanForStepOut (abort_other_plans,
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000863 NULL,
864 false,
865 stop_other_threads,
866 eVoteYes,
Jim Ingham64e7ead2012-05-03 21:19:36 +0000867 eVoteNoOpinion,
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000868 frame_sp->GetFrameIndex()));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000869
Jim Ingham64e7ead2012-05-03 21:19:36 +0000870 // This returns an error, we should use it!
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000871 ResumeNewPlan (exe_ctx, new_plan_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000872 }
873}
874
875void
876SBThread::StepInstruction (bool step_over)
877{
Greg Clayton5160ce52013-03-27 23:08:40 +0000878 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000879
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000880 Mutex::Locker api_locker;
881 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
882
Greg Clayton1ac04c32012-02-21 00:09:25 +0000883
Caroline Ticeceb6b132010-10-26 03:11:13 +0000884
Greg Clayton17a6ad02012-01-30 02:53:15 +0000885 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000886 log->Printf ("SBThread(%p)::StepInstruction (step_over=%i)",
887 static_cast<void*>(exe_ctx.GetThreadPtr()), step_over);
888
Greg Clayton1ac04c32012-02-21 00:09:25 +0000889 if (exe_ctx.HasThreadScope())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000890 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000891 Thread *thread = exe_ctx.GetThreadPtr();
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000892 ThreadPlanSP new_plan_sp(thread->QueueThreadPlanForStepSingleInstruction (step_over, true, true));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000893
Jim Ingham64e7ead2012-05-03 21:19:36 +0000894 // This returns an error, we should use it!
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000895 ResumeNewPlan (exe_ctx, new_plan_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000896 }
897}
898
899void
900SBThread::RunToAddress (lldb::addr_t addr)
901{
Greg Clayton5160ce52013-03-27 23:08:40 +0000902 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000903
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000904 Mutex::Locker api_locker;
905 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
906
Caroline Ticeceb6b132010-10-26 03:11:13 +0000907
Greg Clayton17a6ad02012-01-30 02:53:15 +0000908 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000909 log->Printf ("SBThread(%p)::RunToAddress (addr=0x%" PRIx64 ")",
910 static_cast<void*>(exe_ctx.GetThreadPtr()), addr);
911
Greg Clayton1ac04c32012-02-21 00:09:25 +0000912 if (exe_ctx.HasThreadScope())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000913 {
Jim Ingham7ba6e992012-05-11 23:47:32 +0000914 bool abort_other_plans = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000915 bool stop_other_threads = true;
916
Greg Claytone72dfb32012-02-24 01:59:29 +0000917 Address target_addr (addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000918
Greg Clayton1ac04c32012-02-21 00:09:25 +0000919 Thread *thread = exe_ctx.GetThreadPtr();
Greg Clayton1ac04c32012-02-21 00:09:25 +0000920
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000921 ThreadPlanSP new_plan_sp(thread->QueueThreadPlanForRunToAddress (abort_other_plans, target_addr, stop_other_threads));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000922
Jim Ingham64e7ead2012-05-03 21:19:36 +0000923 // This returns an error, we should use it!
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000924 ResumeNewPlan (exe_ctx, new_plan_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000925 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000926}
927
Greg Clayton481cef22011-01-21 06:11:58 +0000928SBError
929SBThread::StepOverUntil (lldb::SBFrame &sb_frame,
930 lldb::SBFileSpec &sb_file_spec,
931 uint32_t line)
932{
933 SBError sb_error;
Greg Clayton5160ce52013-03-27 23:08:40 +0000934 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton481cef22011-01-21 06:11:58 +0000935 char path[PATH_MAX];
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000936
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000937 Mutex::Locker api_locker;
938 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
939
Jason Molendab57e4a12013-11-04 09:33:30 +0000940 StackFrameSP frame_sp (sb_frame.GetFrameSP());
Greg Claytonb9556ac2012-01-30 07:41:31 +0000941
Greg Clayton481cef22011-01-21 06:11:58 +0000942 if (log)
943 {
944 SBStream frame_desc_strm;
945 sb_frame.GetDescription (frame_desc_strm);
946 sb_file_spec->GetPath (path, sizeof(path));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000947 log->Printf ("SBThread(%p)::StepOverUntil (frame = SBFrame(%p): %s, file+line = %s:%u)",
948 static_cast<void*>(exe_ctx.GetThreadPtr()),
949 static_cast<void*>(frame_sp.get()),
950 frame_desc_strm.GetData(), path, line);
Greg Clayton481cef22011-01-21 06:11:58 +0000951 }
Greg Clayton17a6ad02012-01-30 02:53:15 +0000952
Greg Clayton1ac04c32012-02-21 00:09:25 +0000953 if (exe_ctx.HasThreadScope())
Greg Clayton481cef22011-01-21 06:11:58 +0000954 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000955 Target *target = exe_ctx.GetTargetPtr();
Greg Clayton1ac04c32012-02-21 00:09:25 +0000956 Thread *thread = exe_ctx.GetThreadPtr();
Greg Clayton481cef22011-01-21 06:11:58 +0000957
958 if (line == 0)
959 {
960 sb_error.SetErrorString("invalid line argument");
961 return sb_error;
962 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000963
Greg Claytonb9556ac2012-01-30 07:41:31 +0000964 if (!frame_sp)
Greg Clayton481cef22011-01-21 06:11:58 +0000965 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000966 frame_sp = thread->GetSelectedFrame ();
Greg Clayton481cef22011-01-21 06:11:58 +0000967 if (!frame_sp)
Greg Clayton1ac04c32012-02-21 00:09:25 +0000968 frame_sp = thread->GetStackFrameAtIndex (0);
Greg Clayton481cef22011-01-21 06:11:58 +0000969 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000970
Greg Clayton481cef22011-01-21 06:11:58 +0000971 SymbolContext frame_sc;
972 if (!frame_sp)
973 {
974 sb_error.SetErrorString("no valid frames in thread to step");
975 return sb_error;
976 }
977
978 // If we have a frame, get its line
979 frame_sc = frame_sp->GetSymbolContext (eSymbolContextCompUnit |
980 eSymbolContextFunction |
981 eSymbolContextLineEntry |
982 eSymbolContextSymbol );
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000983
Greg Clayton481cef22011-01-21 06:11:58 +0000984 if (frame_sc.comp_unit == NULL)
985 {
986 sb_error.SetErrorStringWithFormat("frame %u doesn't have debug information", frame_sp->GetFrameIndex());
987 return sb_error;
988 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000989
Greg Clayton481cef22011-01-21 06:11:58 +0000990 FileSpec step_file_spec;
991 if (sb_file_spec.IsValid())
992 {
993 // The file spec passed in was valid, so use it
994 step_file_spec = sb_file_spec.ref();
995 }
996 else
997 {
998 if (frame_sc.line_entry.IsValid())
999 step_file_spec = frame_sc.line_entry.file;
1000 else
1001 {
1002 sb_error.SetErrorString("invalid file argument or no file for frame");
1003 return sb_error;
1004 }
1005 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001006
Jim Ingham9b70ddb2011-05-08 00:56:32 +00001007 // Grab the current function, then we will make sure the "until" address is
1008 // within the function. We discard addresses that are out of the current
1009 // function, and then if there are no addresses remaining, give an appropriate
1010 // error message.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001011
Jim Ingham9b70ddb2011-05-08 00:56:32 +00001012 bool all_in_function = true;
1013 AddressRange fun_range = frame_sc.function->GetAddressRange();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001014
Greg Clayton481cef22011-01-21 06:11:58 +00001015 std::vector<addr_t> step_over_until_addrs;
Jim Ingham7ba6e992012-05-11 23:47:32 +00001016 const bool abort_other_plans = false;
Jim Inghamc02e3342012-09-14 18:57:14 +00001017 const bool stop_other_threads = false;
Greg Clayton481cef22011-01-21 06:11:58 +00001018 const bool check_inlines = true;
1019 const bool exact = false;
1020
1021 SymbolContextList sc_list;
Jim Ingham9b70ddb2011-05-08 00:56:32 +00001022 const uint32_t num_matches = frame_sc.comp_unit->ResolveSymbolContext (step_file_spec,
1023 line,
1024 check_inlines,
1025 exact,
1026 eSymbolContextLineEntry,
1027 sc_list);
Greg Clayton481cef22011-01-21 06:11:58 +00001028 if (num_matches > 0)
1029 {
1030 SymbolContext sc;
1031 for (uint32_t i=0; i<num_matches; ++i)
1032 {
1033 if (sc_list.GetContextAtIndex(i, sc))
1034 {
Jim Ingham9b70ddb2011-05-08 00:56:32 +00001035 addr_t step_addr = sc.line_entry.range.GetBaseAddress().GetLoadAddress(target);
Greg Clayton481cef22011-01-21 06:11:58 +00001036 if (step_addr != LLDB_INVALID_ADDRESS)
1037 {
Jim Ingham9b70ddb2011-05-08 00:56:32 +00001038 if (fun_range.ContainsLoadAddress(step_addr, target))
1039 step_over_until_addrs.push_back(step_addr);
1040 else
1041 all_in_function = false;
Greg Clayton481cef22011-01-21 06:11:58 +00001042 }
1043 }
1044 }
1045 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001046
Greg Clayton481cef22011-01-21 06:11:58 +00001047 if (step_over_until_addrs.empty())
1048 {
Jim Ingham9b70ddb2011-05-08 00:56:32 +00001049 if (all_in_function)
1050 {
1051 step_file_spec.GetPath (path, sizeof(path));
Jason Molendafd54b362011-09-20 21:44:10 +00001052 sb_error.SetErrorStringWithFormat("No line entries for %s:%u", path, line);
Jim Ingham9b70ddb2011-05-08 00:56:32 +00001053 }
1054 else
Greg Clayton86edbf42011-10-26 00:56:27 +00001055 sb_error.SetErrorString ("step until target not in current function");
Greg Clayton481cef22011-01-21 06:11:58 +00001056 }
1057 else
1058 {
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001059 ThreadPlanSP new_plan_sp(thread->QueueThreadPlanForStepUntil (abort_other_plans,
Jim Ingham64e7ead2012-05-03 21:19:36 +00001060 &step_over_until_addrs[0],
1061 step_over_until_addrs.size(),
1062 stop_other_threads,
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001063 frame_sp->GetFrameIndex()));
Greg Clayton481cef22011-01-21 06:11:58 +00001064
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001065 sb_error = ResumeNewPlan (exe_ctx, new_plan_sp.get());
Greg Clayton481cef22011-01-21 06:11:58 +00001066 }
1067 }
1068 else
1069 {
1070 sb_error.SetErrorString("this SBThread object is invalid");
1071 }
1072 return sb_error;
1073}
1074
Jim Ingham44137582012-09-12 00:40:39 +00001075SBError
Richard Mittonf86248d2013-09-12 02:20:34 +00001076SBThread::JumpToLine (lldb::SBFileSpec &file_spec, uint32_t line)
1077{
1078 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1079 SBError sb_error;
1080
1081 Mutex::Locker api_locker;
1082 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1083
1084 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001085 log->Printf ("SBThread(%p)::JumpToLine (file+line = %s:%u)",
1086 static_cast<void*>(exe_ctx.GetThreadPtr()),
1087 file_spec->GetPath().c_str(), line);
Richard Mittonf86248d2013-09-12 02:20:34 +00001088
1089 if (!exe_ctx.HasThreadScope())
1090 {
1091 sb_error.SetErrorString("this SBThread object is invalid");
1092 return sb_error;
1093 }
1094
1095 Thread *thread = exe_ctx.GetThreadPtr();
1096
1097 Error err = thread->JumpToLine (file_spec.get(), line, true);
1098 sb_error.SetError (err);
1099 return sb_error;
1100}
1101
1102SBError
Jim Inghamcb640dd2012-09-14 02:14:15 +00001103SBThread::ReturnFromFrame (SBFrame &frame, SBValue &return_value)
Jim Ingham44137582012-09-12 00:40:39 +00001104{
1105 SBError sb_error;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001106
Greg Clayton5160ce52013-03-27 23:08:40 +00001107 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Ingham44137582012-09-12 00:40:39 +00001108
1109 Mutex::Locker api_locker;
1110 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1111
1112
1113 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001114 log->Printf ("SBThread(%p)::ReturnFromFrame (frame=%d)",
1115 static_cast<void*>(exe_ctx.GetThreadPtr()),
1116 frame.GetFrameID());
1117
Jim Ingham44137582012-09-12 00:40:39 +00001118 if (exe_ctx.HasThreadScope())
1119 {
1120 Thread *thread = exe_ctx.GetThreadPtr();
Jim Inghamcb640dd2012-09-14 02:14:15 +00001121 sb_error.SetError (thread->ReturnFromFrame(frame.GetFrameSP(), return_value.GetSP()));
Jim Ingham44137582012-09-12 00:40:39 +00001122 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001123
Jim Ingham44137582012-09-12 00:40:39 +00001124 return sb_error;
1125}
1126
Greg Clayton481cef22011-01-21 06:11:58 +00001127
Greg Clayton722a0cd2011-01-12 02:25:42 +00001128bool
1129SBThread::Suspend()
1130{
Greg Clayton5160ce52013-03-27 23:08:40 +00001131 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001132 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Claytonc9858e42012-04-06 02:17:47 +00001133 bool result = false;
Greg Clayton1ac04c32012-02-21 00:09:25 +00001134 if (exe_ctx.HasThreadScope())
Greg Clayton722a0cd2011-01-12 02:25:42 +00001135 {
Greg Claytonc9858e42012-04-06 02:17:47 +00001136 Process::StopLocker stop_locker;
1137 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1138 {
1139 exe_ctx.GetThreadPtr()->SetResumeState (eStateSuspended);
1140 result = true;
1141 }
1142 else
1143 {
1144 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001145 log->Printf ("SBThread(%p)::Suspend() => error: process is running",
1146 static_cast<void*>(exe_ctx.GetThreadPtr()));
Greg Claytonc9858e42012-04-06 02:17:47 +00001147 }
Greg Clayton722a0cd2011-01-12 02:25:42 +00001148 }
Greg Claytonc9858e42012-04-06 02:17:47 +00001149 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001150 log->Printf ("SBThread(%p)::Suspend() => %i",
1151 static_cast<void*>(exe_ctx.GetThreadPtr()), result);
Greg Claytonc9858e42012-04-06 02:17:47 +00001152 return result;
Greg Clayton722a0cd2011-01-12 02:25:42 +00001153}
1154
1155bool
1156SBThread::Resume ()
1157{
Greg Clayton5160ce52013-03-27 23:08:40 +00001158 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001159 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Claytonc9858e42012-04-06 02:17:47 +00001160 bool result = false;
Greg Clayton1ac04c32012-02-21 00:09:25 +00001161 if (exe_ctx.HasThreadScope())
Greg Clayton722a0cd2011-01-12 02:25:42 +00001162 {
Greg Claytonc9858e42012-04-06 02:17:47 +00001163 Process::StopLocker stop_locker;
1164 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1165 {
Jim Ingham6c9ed912014-04-03 01:26:14 +00001166 const bool override_suspend = true;
1167 exe_ctx.GetThreadPtr()->SetResumeState (eStateRunning, override_suspend);
Greg Claytonc9858e42012-04-06 02:17:47 +00001168 result = true;
1169 }
1170 else
1171 {
1172 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001173 log->Printf ("SBThread(%p)::Resume() => error: process is running",
1174 static_cast<void*>(exe_ctx.GetThreadPtr()));
Greg Claytonc9858e42012-04-06 02:17:47 +00001175 }
Greg Clayton722a0cd2011-01-12 02:25:42 +00001176 }
Greg Claytonc9858e42012-04-06 02:17:47 +00001177 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001178 log->Printf ("SBThread(%p)::Resume() => %i",
1179 static_cast<void*>(exe_ctx.GetThreadPtr()), result);
Greg Claytonc9858e42012-04-06 02:17:47 +00001180 return result;
Greg Clayton722a0cd2011-01-12 02:25:42 +00001181}
1182
1183bool
1184SBThread::IsSuspended()
1185{
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001186 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Clayton1ac04c32012-02-21 00:09:25 +00001187 if (exe_ctx.HasThreadScope())
1188 return exe_ctx.GetThreadPtr()->GetResumeState () == eStateSuspended;
Greg Clayton722a0cd2011-01-12 02:25:42 +00001189 return false;
1190}
1191
Andrew Kaylora75418d2013-04-15 23:33:53 +00001192bool
1193SBThread::IsStopped()
1194{
1195 ExecutionContext exe_ctx (m_opaque_sp.get());
1196 if (exe_ctx.HasThreadScope())
1197 return StateIsStoppedState(exe_ctx.GetThreadPtr()->GetState(), true);
1198 return false;
1199}
1200
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001201SBProcess
1202SBThread::GetProcess ()
1203{
Greg Claytonb9556ac2012-01-30 07:41:31 +00001204 SBProcess sb_process;
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001205 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Clayton1ac04c32012-02-21 00:09:25 +00001206 if (exe_ctx.HasThreadScope())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001207 {
1208 // Have to go up to the target so we can get a shared pointer to our process...
Greg Clayton1ac04c32012-02-21 00:09:25 +00001209 sb_process.SetSP (exe_ctx.GetProcessSP());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001210 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001211
Greg Clayton5160ce52013-03-27 23:08:40 +00001212 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001213 if (log)
1214 {
Greg Clayton481cef22011-01-21 06:11:58 +00001215 SBStream frame_desc_strm;
Greg Claytonb9556ac2012-01-30 07:41:31 +00001216 sb_process.GetDescription (frame_desc_strm);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001217 log->Printf ("SBThread(%p)::GetProcess () => SBProcess(%p): %s",
1218 static_cast<void*>(exe_ctx.GetThreadPtr()),
1219 static_cast<void*>(sb_process.GetSP().get()),
1220 frame_desc_strm.GetData());
Caroline Ticeceb6b132010-10-26 03:11:13 +00001221 }
1222
Greg Claytonb9556ac2012-01-30 07:41:31 +00001223 return sb_process;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001224}
1225
1226uint32_t
1227SBThread::GetNumFrames ()
1228{
Greg Clayton5160ce52013-03-27 23:08:40 +00001229 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001230
Caroline Ticeceb6b132010-10-26 03:11:13 +00001231 uint32_t num_frames = 0;
Jim Ingham4fc6cb92012-08-22 21:34:33 +00001232 Mutex::Locker api_locker;
1233 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1234
Greg Clayton1ac04c32012-02-21 00:09:25 +00001235 if (exe_ctx.HasThreadScope())
Greg Claytonaf67cec2010-12-20 20:49:23 +00001236 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001237 Process::StopLocker stop_locker;
1238 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1239 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001240 num_frames = exe_ctx.GetThreadPtr()->GetStackFrameCount();
1241 }
Greg Claytonc9858e42012-04-06 02:17:47 +00001242 else
1243 {
1244 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001245 log->Printf ("SBThread(%p)::GetNumFrames() => error: process is running",
1246 static_cast<void*>(exe_ctx.GetThreadPtr()));
Greg Claytonc9858e42012-04-06 02:17:47 +00001247 }
Greg Claytonaf67cec2010-12-20 20:49:23 +00001248 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001249
1250 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001251 log->Printf ("SBThread(%p)::GetNumFrames () => %u",
1252 static_cast<void*>(exe_ctx.GetThreadPtr()), num_frames);
Caroline Ticeceb6b132010-10-26 03:11:13 +00001253
1254 return num_frames;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001255}
1256
1257SBFrame
1258SBThread::GetFrameAtIndex (uint32_t idx)
1259{
Greg Clayton5160ce52013-03-27 23:08:40 +00001260 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001261
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001262 SBFrame sb_frame;
Jason Molendab57e4a12013-11-04 09:33:30 +00001263 StackFrameSP frame_sp;
Jim Ingham4fc6cb92012-08-22 21:34:33 +00001264 Mutex::Locker api_locker;
1265 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1266
Greg Clayton1ac04c32012-02-21 00:09:25 +00001267 if (exe_ctx.HasThreadScope())
Greg Claytonaf67cec2010-12-20 20:49:23 +00001268 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001269 Process::StopLocker stop_locker;
1270 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1271 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001272 frame_sp = exe_ctx.GetThreadPtr()->GetStackFrameAtIndex (idx);
1273 sb_frame.SetFrameSP (frame_sp);
1274 }
Greg Claytonc9858e42012-04-06 02:17:47 +00001275 else
1276 {
1277 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001278 log->Printf ("SBThread(%p)::GetFrameAtIndex() => error: process is running",
1279 static_cast<void*>(exe_ctx.GetThreadPtr()));
Greg Claytonc9858e42012-04-06 02:17:47 +00001280 }
Greg Claytonaf67cec2010-12-20 20:49:23 +00001281 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001282
1283 if (log)
1284 {
Greg Clayton481cef22011-01-21 06:11:58 +00001285 SBStream frame_desc_strm;
1286 sb_frame.GetDescription (frame_desc_strm);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001287 log->Printf ("SBThread(%p)::GetFrameAtIndex (idx=%d) => SBFrame(%p): %s",
1288 static_cast<void*>(exe_ctx.GetThreadPtr()), idx,
1289 static_cast<void*>(frame_sp.get()),
1290 frame_desc_strm.GetData());
Caroline Ticeceb6b132010-10-26 03:11:13 +00001291 }
1292
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001293 return sb_frame;
1294}
1295
Greg Claytonf028a1f2010-12-17 02:26:24 +00001296lldb::SBFrame
1297SBThread::GetSelectedFrame ()
1298{
Greg Clayton5160ce52013-03-27 23:08:40 +00001299 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytonf028a1f2010-12-17 02:26:24 +00001300
1301 SBFrame sb_frame;
Jason Molendab57e4a12013-11-04 09:33:30 +00001302 StackFrameSP frame_sp;
Jim Ingham4fc6cb92012-08-22 21:34:33 +00001303 Mutex::Locker api_locker;
1304 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1305
Greg Clayton1ac04c32012-02-21 00:09:25 +00001306 if (exe_ctx.HasThreadScope())
Greg Claytonaf67cec2010-12-20 20:49:23 +00001307 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001308 Process::StopLocker stop_locker;
1309 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1310 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001311 frame_sp = exe_ctx.GetThreadPtr()->GetSelectedFrame ();
1312 sb_frame.SetFrameSP (frame_sp);
1313 }
Greg Claytonc9858e42012-04-06 02:17:47 +00001314 else
1315 {
1316 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001317 log->Printf ("SBThread(%p)::GetSelectedFrame() => error: process is running",
1318 static_cast<void*>(exe_ctx.GetThreadPtr()));
Greg Claytonc9858e42012-04-06 02:17:47 +00001319 }
Greg Claytonaf67cec2010-12-20 20:49:23 +00001320 }
Greg Claytonf028a1f2010-12-17 02:26:24 +00001321
1322 if (log)
1323 {
Greg Clayton481cef22011-01-21 06:11:58 +00001324 SBStream frame_desc_strm;
1325 sb_frame.GetDescription (frame_desc_strm);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001326 log->Printf ("SBThread(%p)::GetSelectedFrame () => SBFrame(%p): %s",
1327 static_cast<void*>(exe_ctx.GetThreadPtr()),
1328 static_cast<void*>(frame_sp.get()),
1329 frame_desc_strm.GetData());
Greg Claytonf028a1f2010-12-17 02:26:24 +00001330 }
1331
1332 return sb_frame;
1333}
1334
1335lldb::SBFrame
1336SBThread::SetSelectedFrame (uint32_t idx)
1337{
Greg Clayton5160ce52013-03-27 23:08:40 +00001338 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytonf028a1f2010-12-17 02:26:24 +00001339
1340 SBFrame sb_frame;
Jason Molendab57e4a12013-11-04 09:33:30 +00001341 StackFrameSP frame_sp;
Jim Ingham4fc6cb92012-08-22 21:34:33 +00001342 Mutex::Locker api_locker;
1343 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1344
Greg Clayton1ac04c32012-02-21 00:09:25 +00001345 if (exe_ctx.HasThreadScope())
Greg Claytonf028a1f2010-12-17 02:26:24 +00001346 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001347 Process::StopLocker stop_locker;
1348 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
Greg Claytonf028a1f2010-12-17 02:26:24 +00001349 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001350 Thread *thread = exe_ctx.GetThreadPtr();
1351 frame_sp = thread->GetStackFrameAtIndex (idx);
1352 if (frame_sp)
1353 {
1354 thread->SetSelectedFrame (frame_sp.get());
1355 sb_frame.SetFrameSP (frame_sp);
1356 }
Greg Claytonf028a1f2010-12-17 02:26:24 +00001357 }
Greg Claytonc9858e42012-04-06 02:17:47 +00001358 else
1359 {
1360 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001361 log->Printf ("SBThread(%p)::SetSelectedFrame() => error: process is running",
1362 static_cast<void*>(exe_ctx.GetThreadPtr()));
Greg Claytonc9858e42012-04-06 02:17:47 +00001363 }
Greg Claytonf028a1f2010-12-17 02:26:24 +00001364 }
1365
1366 if (log)
1367 {
Greg Clayton481cef22011-01-21 06:11:58 +00001368 SBStream frame_desc_strm;
1369 sb_frame.GetDescription (frame_desc_strm);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001370 log->Printf ("SBThread(%p)::SetSelectedFrame (idx=%u) => SBFrame(%p): %s",
1371 static_cast<void*>(exe_ctx.GetThreadPtr()), idx,
1372 static_cast<void*>(frame_sp.get()),
1373 frame_desc_strm.GetData());
Greg Claytonf028a1f2010-12-17 02:26:24 +00001374 }
1375 return sb_frame;
1376}
1377
Jim Ingham4f465cf2012-10-10 18:32:14 +00001378bool
1379SBThread::EventIsThreadEvent (const SBEvent &event)
1380{
1381 return Thread::ThreadEventData::GetEventDataFromEvent(event.get()) != NULL;
1382}
1383
1384SBFrame
1385SBThread::GetStackFrameFromEvent (const SBEvent &event)
1386{
1387 return Thread::ThreadEventData::GetStackFrameFromEvent (event.get());
1388
1389}
1390
1391SBThread
1392SBThread::GetThreadFromEvent (const SBEvent &event)
1393{
1394 return Thread::ThreadEventData::GetThreadFromEvent (event.get());
1395}
Greg Claytonf028a1f2010-12-17 02:26:24 +00001396
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001397bool
1398SBThread::operator == (const SBThread &rhs) const
1399{
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001400 return m_opaque_sp->GetThreadSP().get() == rhs.m_opaque_sp->GetThreadSP().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001401}
1402
1403bool
1404SBThread::operator != (const SBThread &rhs) const
1405{
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001406 return m_opaque_sp->GetThreadSP().get() != rhs.m_opaque_sp->GetThreadSP().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001407}
Caroline Ticedde9cff2010-09-20 05:20:02 +00001408
1409bool
Jim Ingham4f465cf2012-10-10 18:32:14 +00001410SBThread::GetStatus (SBStream &status) const
1411{
1412 Stream &strm = status.ref();
1413
1414 ExecutionContext exe_ctx (m_opaque_sp.get());
1415 if (exe_ctx.HasThreadScope())
1416 {
1417 exe_ctx.GetThreadPtr()->GetStatus(strm, 0, 1, 1);
1418 }
1419 else
1420 strm.PutCString ("No status");
1421
1422 return true;
1423}
1424
1425bool
Caroline Ticeceb6b132010-10-26 03:11:13 +00001426SBThread::GetDescription (SBStream &description) const
1427{
Greg Claytonda7bc7d2011-11-13 06:57:31 +00001428 Stream &strm = description.ref();
1429
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001430 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Clayton1ac04c32012-02-21 00:09:25 +00001431 if (exe_ctx.HasThreadScope())
Caroline Ticeceb6b132010-10-26 03:11:13 +00001432 {
Daniel Malead01b2952012-11-29 21:49:15 +00001433 strm.Printf("SBThread: tid = 0x%4.4" PRIx64, exe_ctx.GetThreadPtr()->GetID());
Caroline Ticeceb6b132010-10-26 03:11:13 +00001434 }
1435 else
Greg Claytonda7bc7d2011-11-13 06:57:31 +00001436 strm.PutCString ("No value");
Caroline Ticeceb6b132010-10-26 03:11:13 +00001437
1438 return true;
1439}
Jason Molenda5dd49162013-11-06 00:04:44 +00001440
1441SBThread
Jason Molenda008c45f2013-11-12 23:33:32 +00001442SBThread::GetExtendedBacktraceThread (const char *type)
Jason Molenda5dd49162013-11-06 00:04:44 +00001443{
1444 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1445 Mutex::Locker api_locker;
1446 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1447 SBThread sb_origin_thread;
1448
1449 if (exe_ctx.HasThreadScope())
1450 {
1451 Process::StopLocker stop_locker;
1452 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1453 {
Jason Molenda7a2f7902013-11-11 05:19:34 +00001454 ThreadSP real_thread(exe_ctx.GetThreadSP());
Jason Molenda5dd49162013-11-06 00:04:44 +00001455 if (real_thread)
1456 {
1457 ConstString type_const (type);
Jason Molenda7a2f7902013-11-11 05:19:34 +00001458 Process *process = exe_ctx.GetProcessPtr();
1459 if (process)
Jason Molenda5dd49162013-11-06 00:04:44 +00001460 {
Jason Molenda7a2f7902013-11-11 05:19:34 +00001461 SystemRuntime *runtime = process->GetSystemRuntime();
1462 if (runtime)
1463 {
Jason Molenda008c45f2013-11-12 23:33:32 +00001464 ThreadSP new_thread_sp (runtime->GetExtendedBacktraceThread (real_thread, type_const));
Jason Molendaa6e91302013-11-19 05:44:41 +00001465 if (new_thread_sp)
1466 {
1467 // Save this in the Process' ExtendedThreadList so a strong pointer retains the
1468 // object.
1469 process->GetExtendedThreadList().AddThread (new_thread_sp);
1470 sb_origin_thread.SetThread (new_thread_sp);
1471 if (log)
1472 {
1473 const char *queue_name = new_thread_sp->GetQueueName();
1474 if (queue_name == NULL)
1475 queue_name = "";
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001476 log->Printf ("SBThread(%p)::GetExtendedBacktraceThread() => new extended Thread created (%p) with queue_id 0x%" PRIx64 " queue name '%s'",
1477 static_cast<void*>(exe_ctx.GetThreadPtr()),
1478 static_cast<void*>(new_thread_sp.get()),
1479 new_thread_sp->GetQueueID(),
1480 queue_name);
Jason Molendaa6e91302013-11-19 05:44:41 +00001481 }
1482 }
Jason Molenda7a2f7902013-11-11 05:19:34 +00001483 }
Jason Molenda5dd49162013-11-06 00:04:44 +00001484 }
1485 }
1486 }
1487 else
1488 {
1489 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001490 log->Printf ("SBThread(%p)::GetExtendedBacktraceThread() => error: process is running",
1491 static_cast<void*>(exe_ctx.GetThreadPtr()));
Jason Molenda5dd49162013-11-06 00:04:44 +00001492 }
1493 }
1494
Jason Molendaac605f42014-03-08 01:34:55 +00001495 if (log && sb_origin_thread.IsValid() == false)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001496 log->Printf("SBThread(%p)::GetExtendedBacktraceThread() is not returning a Valid thread",
1497 static_cast<void*>(exe_ctx.GetThreadPtr()));
Jason Molenda5dd49162013-11-06 00:04:44 +00001498 return sb_origin_thread;
1499}
Jason Molenda8ee9cb52013-11-16 01:24:22 +00001500
1501uint32_t
1502SBThread::GetExtendedBacktraceOriginatingIndexID ()
1503{
1504 ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
1505 if (thread_sp)
1506 return thread_sp->GetExtendedBacktraceOriginatingIndexID();
1507 return LLDB_INVALID_INDEX32;
1508}
Jason Molendab4892cd2014-05-13 22:02:48 +00001509
1510bool
1511SBThread::SafeToCallFunctions ()
1512{
1513 ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
1514 if (thread_sp)
1515 return thread_sp->SafeToCallFunctions();
1516 return true;
1517}