blob: 390fa0d4b6ecbb95d1b92d0e871ba11a3490c3c4 [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 Ingham2bdbfd52014-09-29 23:17:18 +000044#include "lldb/API/SBThreadPlan.h"
Jim Ingham73ca05a2011-12-17 01:35:57 +000045#include "lldb/API/SBValue.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046
47using namespace lldb;
48using namespace lldb_private;
49
Jim Ingham4f465cf2012-10-10 18:32:14 +000050const char *
51SBThread::GetBroadcasterClassName ()
52{
53 return Thread::GetStaticBroadcasterClass().AsCString();
54}
55
Greg Claytoncfd1ace2010-10-31 03:01:06 +000056//----------------------------------------------------------------------
57// Constructors
58//----------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +000059SBThread::SBThread () :
Greg Clayton7fdf9ef2012-04-05 16:12:35 +000060 m_opaque_sp (new ExecutionContextRef())
Chris Lattner30fdc8d2010-06-08 16:52:24 +000061{
62}
63
Chris Lattner30fdc8d2010-06-08 16:52:24 +000064SBThread::SBThread (const ThreadSP& lldb_object_sp) :
Greg Clayton7fdf9ef2012-04-05 16:12:35 +000065 m_opaque_sp (new ExecutionContextRef(lldb_object_sp))
Chris Lattner30fdc8d2010-06-08 16:52:24 +000066{
67}
68
Greg Clayton92ef5732010-10-30 18:26:59 +000069SBThread::SBThread (const SBThread &rhs) :
Greg Clayton7fdf9ef2012-04-05 16:12:35 +000070 m_opaque_sp (new ExecutionContextRef(*rhs.m_opaque_sp))
Chris Lattner30fdc8d2010-06-08 16:52:24 +000071{
Greg Clayton7fdf9ef2012-04-05 16:12:35 +000072
Chris Lattner30fdc8d2010-06-08 16:52:24 +000073}
74
75//----------------------------------------------------------------------
Greg Claytoncfd1ace2010-10-31 03:01:06 +000076// Assignment operator
77//----------------------------------------------------------------------
78
79const lldb::SBThread &
80SBThread::operator = (const SBThread &rhs)
81{
82 if (this != &rhs)
Greg Clayton7fdf9ef2012-04-05 16:12:35 +000083 *m_opaque_sp = *rhs.m_opaque_sp;
Greg Claytoncfd1ace2010-10-31 03:01:06 +000084 return *this;
85}
86
87//----------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +000088// Destructor
89//----------------------------------------------------------------------
90SBThread::~SBThread()
91{
92}
93
Jason Molendab9ffa982014-04-25 00:01:15 +000094lldb::SBQueue
95SBThread::GetQueue () const
96{
97 SBQueue sb_queue;
98 QueueSP queue_sp;
99 Mutex::Locker api_locker;
100 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
101
102 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
103 if (exe_ctx.HasThreadScope())
104 {
105 Process::StopLocker stop_locker;
106 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
107 {
108 queue_sp = exe_ctx.GetThreadPtr()->GetQueue();
109 if (queue_sp)
110 {
111 sb_queue.SetQueue (queue_sp);
112 }
113 }
114 else
115 {
116 if (log)
117 log->Printf ("SBThread(%p)::GetQueueKind() => error: process is running",
118 static_cast<void*>(exe_ctx.GetThreadPtr()));
119 }
120 }
121
122 if (log)
123 log->Printf ("SBThread(%p)::GetQueueKind () => SBQueue(%p)",
124 static_cast<void*>(exe_ctx.GetThreadPtr()), static_cast<void*>(queue_sp.get()));
125
126 return sb_queue;
127}
128
129
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000130bool
131SBThread::IsValid() const
132{
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000133 return m_opaque_sp->GetThreadSP().get() != NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000134}
135
Greg Clayton48e42542010-07-30 20:12:55 +0000136void
137SBThread::Clear ()
138{
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000139 m_opaque_sp->Clear();
Greg Clayton48e42542010-07-30 20:12:55 +0000140}
141
142
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000143StopReason
144SBThread::GetStopReason()
145{
Greg Clayton5160ce52013-03-27 23:08:40 +0000146 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000147
Caroline Ticeceb6b132010-10-26 03:11:13 +0000148 StopReason reason = eStopReasonInvalid;
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000149 Mutex::Locker api_locker;
150 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
151
Greg Clayton1ac04c32012-02-21 00:09:25 +0000152 if (exe_ctx.HasThreadScope())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000153 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000154 Process::StopLocker stop_locker;
155 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
156 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000157 return exe_ctx.GetThreadPtr()->GetStopReason();
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000158 }
Greg Claytonc9858e42012-04-06 02:17:47 +0000159 else
160 {
161 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000162 log->Printf ("SBThread(%p)::GetStopReason() => error: process is running",
163 static_cast<void*>(exe_ctx.GetThreadPtr()));
Greg Claytonc9858e42012-04-06 02:17:47 +0000164 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000165 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000166
167 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000168 log->Printf ("SBThread(%p)::GetStopReason () => %s",
169 static_cast<void*>(exe_ctx.GetThreadPtr()),
Caroline Tice750cd172010-10-26 23:49:36 +0000170 Thread::StopReasonAsCString (reason));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000171
172 return reason;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000173}
174
175size_t
Greg Clayton4e78f602010-11-18 18:52:36 +0000176SBThread::GetStopReasonDataCount ()
177{
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000178 Mutex::Locker api_locker;
179 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
180
Greg Clayton1ac04c32012-02-21 00:09:25 +0000181 if (exe_ctx.HasThreadScope())
Greg Clayton4e78f602010-11-18 18:52:36 +0000182 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000183 Process::StopLocker stop_locker;
184 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
Greg Clayton4e78f602010-11-18 18:52:36 +0000185 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000186 StopInfoSP stop_info_sp = exe_ctx.GetThreadPtr()->GetStopInfo ();
187 if (stop_info_sp)
Greg Clayton4e78f602010-11-18 18:52:36 +0000188 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000189 StopReason reason = stop_info_sp->GetStopReason();
190 switch (reason)
Greg Clayton4e78f602010-11-18 18:52:36 +0000191 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000192 case eStopReasonInvalid:
193 case eStopReasonNone:
194 case eStopReasonTrace:
Greg Clayton90ba8112012-12-05 00:16:59 +0000195 case eStopReasonExec:
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000196 case eStopReasonPlanComplete:
Andrew Kaylorf85defa2012-12-20 23:08:03 +0000197 case eStopReasonThreadExiting:
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000198 // There is no data for these stop reasons.
199 return 0;
200
201 case eStopReasonBreakpoint:
202 {
203 break_id_t site_id = stop_info_sp->GetValue();
204 lldb::BreakpointSiteSP bp_site_sp (exe_ctx.GetProcessPtr()->GetBreakpointSiteList().FindByID (site_id));
205 if (bp_site_sp)
206 return bp_site_sp->GetNumberOfOwners () * 2;
207 else
208 return 0; // Breakpoint must have cleared itself...
209 }
210 break;
211
212 case eStopReasonWatchpoint:
213 return 1;
214
215 case eStopReasonSignal:
216 return 1;
217
218 case eStopReasonException:
219 return 1;
Greg Clayton4e78f602010-11-18 18:52:36 +0000220 }
Greg Clayton4e78f602010-11-18 18:52:36 +0000221 }
222 }
Greg Claytonc9858e42012-04-06 02:17:47 +0000223 else
224 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000225 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytonc9858e42012-04-06 02:17:47 +0000226 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000227 log->Printf ("SBThread(%p)::GetStopReasonDataCount() => error: process is running",
228 static_cast<void*>(exe_ctx.GetThreadPtr()));
Greg Claytonc9858e42012-04-06 02:17:47 +0000229 }
Greg Clayton4e78f602010-11-18 18:52:36 +0000230 }
231 return 0;
232}
233
234uint64_t
235SBThread::GetStopReasonDataAtIndex (uint32_t idx)
236{
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000237 Mutex::Locker api_locker;
238 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
239
Greg Clayton1ac04c32012-02-21 00:09:25 +0000240 if (exe_ctx.HasThreadScope())
Greg Clayton4e78f602010-11-18 18:52:36 +0000241 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000242 Process::StopLocker stop_locker;
243 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
Greg Clayton4e78f602010-11-18 18:52:36 +0000244 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000245 Thread *thread = exe_ctx.GetThreadPtr();
246 StopInfoSP stop_info_sp = thread->GetStopInfo ();
247 if (stop_info_sp)
248 {
249 StopReason reason = stop_info_sp->GetStopReason();
250 switch (reason)
Greg Clayton4e78f602010-11-18 18:52:36 +0000251 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000252 case eStopReasonInvalid:
253 case eStopReasonNone:
254 case eStopReasonTrace:
Greg Clayton90ba8112012-12-05 00:16:59 +0000255 case eStopReasonExec:
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000256 case eStopReasonPlanComplete:
Andrew Kaylorf85defa2012-12-20 23:08:03 +0000257 case eStopReasonThreadExiting:
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000258 // There is no data for these stop reasons.
259 return 0;
260
261 case eStopReasonBreakpoint:
Greg Clayton4e78f602010-11-18 18:52:36 +0000262 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000263 break_id_t site_id = stop_info_sp->GetValue();
264 lldb::BreakpointSiteSP bp_site_sp (exe_ctx.GetProcessPtr()->GetBreakpointSiteList().FindByID (site_id));
265 if (bp_site_sp)
Greg Clayton4e78f602010-11-18 18:52:36 +0000266 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000267 uint32_t bp_index = idx / 2;
268 BreakpointLocationSP bp_loc_sp (bp_site_sp->GetOwnerAtIndex (bp_index));
269 if (bp_loc_sp)
Greg Clayton4e78f602010-11-18 18:52:36 +0000270 {
Greg Clayton8334e142014-04-11 17:27:02 +0000271 if (idx & 1)
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000272 {
273 // Odd idx, return the breakpoint location ID
274 return bp_loc_sp->GetID();
275 }
276 else
277 {
278 // Even idx, return the breakpoint ID
279 return bp_loc_sp->GetBreakpoint().GetID();
280 }
Greg Clayton4e78f602010-11-18 18:52:36 +0000281 }
282 }
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000283 return LLDB_INVALID_BREAK_ID;
Greg Clayton4e78f602010-11-18 18:52:36 +0000284 }
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000285 break;
286
287 case eStopReasonWatchpoint:
288 return stop_info_sp->GetValue();
289
290 case eStopReasonSignal:
291 return stop_info_sp->GetValue();
292
293 case eStopReasonException:
294 return stop_info_sp->GetValue();
Greg Clayton4e78f602010-11-18 18:52:36 +0000295 }
Greg Clayton4e78f602010-11-18 18:52:36 +0000296 }
297 }
Greg Claytonc9858e42012-04-06 02:17:47 +0000298 else
299 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000300 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytonc9858e42012-04-06 02:17:47 +0000301 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000302 log->Printf ("SBThread(%p)::GetStopReasonDataAtIndex() => error: process is running",
303 static_cast<void*>(exe_ctx.GetThreadPtr()));
Greg Claytonc9858e42012-04-06 02:17:47 +0000304 }
Greg Clayton4e78f602010-11-18 18:52:36 +0000305 }
306 return 0;
307}
308
309size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000310SBThread::GetStopDescription (char *dst, size_t dst_len)
311{
Greg Clayton5160ce52013-03-27 23:08:40 +0000312 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000313
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000314 Mutex::Locker api_locker;
315 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
316
Greg Clayton1ac04c32012-02-21 00:09:25 +0000317 if (exe_ctx.HasThreadScope())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000318 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000319 Process::StopLocker stop_locker;
320 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000321 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000322
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000323 StopInfoSP stop_info_sp = exe_ctx.GetThreadPtr()->GetStopInfo ();
324 if (stop_info_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000325 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000326 const char *stop_desc = stop_info_sp->GetDescription();
327 if (stop_desc)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000328 {
Caroline Ticeceb6b132010-10-26 03:11:13 +0000329 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000330 log->Printf ("SBThread(%p)::GetStopDescription (dst, dst_len) => \"%s\"",
331 static_cast<void*>(exe_ctx.GetThreadPtr()),
332 stop_desc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000333 if (dst)
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000334 return ::snprintf (dst, dst_len, "%s", stop_desc);
335 else
336 {
337 // NULL dst passed in, return the length needed to contain the description
338 return ::strlen (stop_desc) + 1; // Include the NULL byte for size
339 }
340 }
341 else
342 {
343 size_t stop_desc_len = 0;
344 switch (stop_info_sp->GetStopReason())
345 {
346 case eStopReasonTrace:
347 case eStopReasonPlanComplete:
348 {
349 static char trace_desc[] = "step";
350 stop_desc = trace_desc;
351 stop_desc_len = sizeof(trace_desc); // Include the NULL byte for size
352 }
353 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000354
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000355 case eStopReasonBreakpoint:
356 {
357 static char bp_desc[] = "breakpoint hit";
358 stop_desc = bp_desc;
359 stop_desc_len = sizeof(bp_desc); // Include the NULL byte for size
360 }
361 break;
362
363 case eStopReasonWatchpoint:
364 {
365 static char wp_desc[] = "watchpoint hit";
366 stop_desc = wp_desc;
367 stop_desc_len = sizeof(wp_desc); // Include the NULL byte for size
368 }
369 break;
370
371 case eStopReasonSignal:
372 {
373 stop_desc = exe_ctx.GetProcessPtr()->GetUnixSignals ().GetSignalAsCString (stop_info_sp->GetValue());
374 if (stop_desc == NULL || stop_desc[0] == '\0')
375 {
376 static char signal_desc[] = "signal";
377 stop_desc = signal_desc;
378 stop_desc_len = sizeof(signal_desc); // Include the NULL byte for size
379 }
380 }
381 break;
382
383 case eStopReasonException:
384 {
385 char exc_desc[] = "exception";
386 stop_desc = exc_desc;
387 stop_desc_len = sizeof(exc_desc); // Include the NULL byte for size
388 }
389 break;
390
Greg Clayton90ba8112012-12-05 00:16:59 +0000391 case eStopReasonExec:
392 {
393 char exc_desc[] = "exec";
394 stop_desc = exc_desc;
395 stop_desc_len = sizeof(exc_desc); // Include the NULL byte for size
396 }
397 break;
398
Andrew Kaylorf85defa2012-12-20 23:08:03 +0000399 case eStopReasonThreadExiting:
400 {
401 char limbo_desc[] = "thread exiting";
402 stop_desc = limbo_desc;
403 stop_desc_len = sizeof(limbo_desc);
404 }
405 break;
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000406 default:
407 break;
408 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000409
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000410 if (stop_desc && stop_desc[0])
411 {
412 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000413 log->Printf ("SBThread(%p)::GetStopDescription (dst, dst_len) => '%s'",
414 static_cast<void*>(exe_ctx.GetThreadPtr()),
415 stop_desc);
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000416
417 if (dst)
418 return ::snprintf (dst, dst_len, "%s", stop_desc) + 1; // Include the NULL byte
419
420 if (stop_desc_len == 0)
421 stop_desc_len = ::strlen (stop_desc) + 1; // Include the NULL byte
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000422
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000423 return stop_desc_len;
424 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000425 }
426 }
427 }
Greg Claytonc9858e42012-04-06 02:17:47 +0000428 else
429 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000430 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytonc9858e42012-04-06 02:17:47 +0000431 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000432 log->Printf ("SBThread(%p)::GetStopDescription() => error: process is running",
433 static_cast<void*>(exe_ctx.GetThreadPtr()));
Greg Claytonc9858e42012-04-06 02:17:47 +0000434 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000435 }
436 if (dst)
437 *dst = 0;
438 return 0;
439}
440
Jim Ingham73ca05a2011-12-17 01:35:57 +0000441SBValue
442SBThread::GetStopReturnValue ()
443{
Greg Clayton5160ce52013-03-27 23:08:40 +0000444 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Ingham73ca05a2011-12-17 01:35:57 +0000445 ValueObjectSP return_valobj_sp;
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000446 Mutex::Locker api_locker;
447 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
448
Greg Clayton1ac04c32012-02-21 00:09:25 +0000449 if (exe_ctx.HasThreadScope())
Jim Ingham73ca05a2011-12-17 01:35:57 +0000450 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000451 Process::StopLocker stop_locker;
452 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
Jim Ingham73ca05a2011-12-17 01:35:57 +0000453 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000454 StopInfoSP stop_info_sp = exe_ctx.GetThreadPtr()->GetStopInfo ();
455 if (stop_info_sp)
456 {
457 return_valobj_sp = StopInfo::GetReturnValueObject (stop_info_sp);
458 }
Jim Ingham73ca05a2011-12-17 01:35:57 +0000459 }
Greg Claytonc9858e42012-04-06 02:17:47 +0000460 else
461 {
462 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000463 log->Printf ("SBThread(%p)::GetStopReturnValue() => error: process is running",
464 static_cast<void*>(exe_ctx.GetThreadPtr()));
Greg Claytonc9858e42012-04-06 02:17:47 +0000465 }
Jim Ingham73ca05a2011-12-17 01:35:57 +0000466 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000467
Jim Ingham73ca05a2011-12-17 01:35:57 +0000468 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000469 log->Printf ("SBThread(%p)::GetStopReturnValue () => %s",
470 static_cast<void*>(exe_ctx.GetThreadPtr()),
471 return_valobj_sp.get()
472 ? return_valobj_sp->GetValueAsCString()
473 : "<no return value>");
474
Jim Ingham73ca05a2011-12-17 01:35:57 +0000475 return SBValue (return_valobj_sp);
476}
477
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000478void
479SBThread::SetThread (const ThreadSP& lldb_object_sp)
480{
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000481 m_opaque_sp->SetThreadSP (lldb_object_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000482}
483
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000484lldb::tid_t
485SBThread::GetThreadID () const
486{
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000487 ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
Greg Clayton17a6ad02012-01-30 02:53:15 +0000488 if (thread_sp)
Greg Clayton1ac04c32012-02-21 00:09:25 +0000489 return thread_sp->GetID();
490 return LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000491}
492
493uint32_t
494SBThread::GetIndexID () const
495{
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000496 ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
Greg Clayton17a6ad02012-01-30 02:53:15 +0000497 if (thread_sp)
498 return thread_sp->GetIndexID();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000499 return LLDB_INVALID_INDEX32;
500}
Greg Clayton1ac04c32012-02-21 00:09:25 +0000501
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000502const char *
503SBThread::GetName () const
504{
Greg Clayton5160ce52013-03-27 23:08:40 +0000505 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton48381312010-10-30 04:51:46 +0000506 const char *name = NULL;
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000507 Mutex::Locker api_locker;
508 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
509
Greg Clayton1ac04c32012-02-21 00:09:25 +0000510 if (exe_ctx.HasThreadScope())
Greg Claytonaf67cec2010-12-20 20:49:23 +0000511 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000512 Process::StopLocker stop_locker;
513 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
514 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000515 name = exe_ctx.GetThreadPtr()->GetName();
516 }
Greg Claytonc9858e42012-04-06 02:17:47 +0000517 else
518 {
519 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000520 log->Printf ("SBThread(%p)::GetName() => error: process is running",
521 static_cast<void*>(exe_ctx.GetThreadPtr()));
Greg Claytonc9858e42012-04-06 02:17:47 +0000522 }
Greg Claytonaf67cec2010-12-20 20:49:23 +0000523 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000524
Caroline Ticeceb6b132010-10-26 03:11:13 +0000525 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000526 log->Printf ("SBThread(%p)::GetName () => %s",
527 static_cast<void*>(exe_ctx.GetThreadPtr()),
528 name ? name : "NULL");
Caroline Ticeceb6b132010-10-26 03:11:13 +0000529
Greg Clayton48381312010-10-30 04:51:46 +0000530 return name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000531}
532
533const char *
534SBThread::GetQueueName () const
535{
Greg Clayton48381312010-10-30 04:51:46 +0000536 const char *name = NULL;
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000537 Mutex::Locker api_locker;
538 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
539
Greg Clayton5160ce52013-03-27 23:08:40 +0000540 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton1ac04c32012-02-21 00:09:25 +0000541 if (exe_ctx.HasThreadScope())
Greg Claytonaf67cec2010-12-20 20:49:23 +0000542 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000543 Process::StopLocker stop_locker;
544 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
545 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000546 name = exe_ctx.GetThreadPtr()->GetQueueName();
547 }
Greg Claytonc9858e42012-04-06 02:17:47 +0000548 else
549 {
550 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000551 log->Printf ("SBThread(%p)::GetQueueName() => error: process is running",
552 static_cast<void*>(exe_ctx.GetThreadPtr()));
Greg Claytonc9858e42012-04-06 02:17:47 +0000553 }
Greg Claytonaf67cec2010-12-20 20:49:23 +0000554 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000555
Caroline Ticeceb6b132010-10-26 03:11:13 +0000556 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000557 log->Printf ("SBThread(%p)::GetQueueName () => %s",
558 static_cast<void*>(exe_ctx.GetThreadPtr()),
559 name ? name : "NULL");
Caroline Ticeceb6b132010-10-26 03:11:13 +0000560
Greg Clayton48381312010-10-30 04:51:46 +0000561 return name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000562}
563
Jason Molenda4fdb5862013-10-21 23:52:54 +0000564lldb::queue_id_t
565SBThread::GetQueueID () const
566{
567 queue_id_t id = LLDB_INVALID_QUEUE_ID;
568 Mutex::Locker api_locker;
569 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
570
571 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
572 if (exe_ctx.HasThreadScope())
573 {
574 Process::StopLocker stop_locker;
575 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
576 {
577 id = exe_ctx.GetThreadPtr()->GetQueueID();
578 }
579 else
580 {
581 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000582 log->Printf ("SBThread(%p)::GetQueueID() => error: process is running",
583 static_cast<void*>(exe_ctx.GetThreadPtr()));
Jason Molenda4fdb5862013-10-21 23:52:54 +0000584 }
585 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000586
Jason Molenda4fdb5862013-10-21 23:52:54 +0000587 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000588 log->Printf ("SBThread(%p)::GetQueueID () => 0x%" PRIx64,
589 static_cast<void*>(exe_ctx.GetThreadPtr()), id);
Jason Molenda4fdb5862013-10-21 23:52:54 +0000590
591 return id;
592}
593
Jason Molenda705b1802014-06-13 02:37:02 +0000594bool
595SBThread::GetInfoItemByPathAsString (const char *path, SBStream &strm)
596{
597 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
598 bool success = false;
599 Mutex::Locker api_locker;
600 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
601
602 if (exe_ctx.HasThreadScope())
603 {
604 Process::StopLocker stop_locker;
605 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
606 {
607 Thread *thread = exe_ctx.GetThreadPtr();
608 StructuredData::ObjectSP info_root_sp = thread->GetExtendedInfo();
609 if (info_root_sp)
610 {
611 StructuredData::ObjectSP node = info_root_sp->GetObjectForDotSeparatedPath (path);
612 if (node)
613 {
614 if (node->GetType() == StructuredData::Type::eTypeString)
615 {
616 strm.Printf ("%s", node->GetAsString()->GetValue().c_str());
617 success = true;
618 }
619 if (node->GetType() == StructuredData::Type::eTypeInteger)
620 {
621 strm.Printf ("0x%" PRIx64, node->GetAsInteger()->GetValue());
622 success = true;
623 }
624 if (node->GetType() == StructuredData::Type::eTypeFloat)
625 {
626 strm.Printf ("0x%f", node->GetAsFloat()->GetValue());
627 success = true;
628 }
629 if (node->GetType() == StructuredData::Type::eTypeBoolean)
630 {
631 if (node->GetAsBoolean()->GetValue() == true)
632 strm.Printf ("true");
633 else
634 strm.Printf ("false");
635 success = true;
636 }
637 if (node->GetType() == StructuredData::Type::eTypeNull)
638 {
639 strm.Printf ("null");
640 success = true;
641 }
642 }
643 }
644 }
645 else
646 {
647 if (log)
648 log->Printf ("SBThread(%p)::GetInfoItemByPathAsString() => error: process is running",
649 static_cast<void*>(exe_ctx.GetThreadPtr()));
650 }
651 }
652
653 if (log)
654 log->Printf ("SBThread(%p)::GetInfoItemByPathAsString () => %s",
655 static_cast<void*>(exe_ctx.GetThreadPtr()),
656 strm.GetData());
657
658 return success;
659}
660
661
Jim Ingham64e7ead2012-05-03 21:19:36 +0000662SBError
663SBThread::ResumeNewPlan (ExecutionContext &exe_ctx, ThreadPlan *new_plan)
664{
665 SBError sb_error;
666
667 Process *process = exe_ctx.GetProcessPtr();
668 if (!process)
669 {
670 sb_error.SetErrorString("No process in SBThread::ResumeNewPlan");
671 return sb_error;
672 }
673
674 Thread *thread = exe_ctx.GetThreadPtr();
675 if (!thread)
676 {
677 sb_error.SetErrorString("No thread in SBThread::ResumeNewPlan");
678 return sb_error;
679 }
680
681 // User level plans should be Master Plans so they can be interrupted, other plans executed, and
682 // then a "continue" will resume the plan.
683 if (new_plan != NULL)
684 {
685 new_plan->SetIsMasterPlan(true);
686 new_plan->SetOkayToDiscard(false);
687 }
688
689 // Why do we need to set the current thread by ID here???
690 process->GetThreadList().SetSelectedThreadByID (thread->GetID());
691 sb_error.ref() = process->Resume();
692
693 if (sb_error.Success())
694 {
695 // If we are doing synchronous mode, then wait for the
696 // process to stop yet again!
697 if (process->GetTarget().GetDebugger().GetAsyncExecution () == false)
698 process->WaitForProcessToStop (NULL);
699 }
700
701 return sb_error;
702}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000703
704void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000705SBThread::StepOver (lldb::RunMode stop_other_threads)
706{
Greg Clayton5160ce52013-03-27 23:08:40 +0000707 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000708
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000709 Mutex::Locker api_locker;
710 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
711
Caroline Ticeceb6b132010-10-26 03:11:13 +0000712
Greg Clayton17a6ad02012-01-30 02:53:15 +0000713 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000714 log->Printf ("SBThread(%p)::StepOver (stop_other_threads='%s')",
715 static_cast<void*>(exe_ctx.GetThreadPtr()),
Greg Clayton17a6ad02012-01-30 02:53:15 +0000716 Thread::RunModeAsCString (stop_other_threads));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000717
Greg Clayton1ac04c32012-02-21 00:09:25 +0000718 if (exe_ctx.HasThreadScope())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000719 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000720 Thread *thread = exe_ctx.GetThreadPtr();
Jim Ingham7ba6e992012-05-11 23:47:32 +0000721 bool abort_other_plans = false;
Jason Molendab57e4a12013-11-04 09:33:30 +0000722 StackFrameSP frame_sp(thread->GetStackFrameAtIndex (0));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000723
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000724 ThreadPlanSP new_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000725 if (frame_sp)
726 {
727 if (frame_sp->HasDebugInformation ())
728 {
Jim Ingham4b4b2472014-03-13 02:47:14 +0000729 const LazyBool avoid_no_debug = eLazyBoolCalculate;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000730 SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000731 new_plan_sp = thread->QueueThreadPlanForStepOverRange (abort_other_plans,
Jim Inghamc6276822012-12-12 19:58:40 +0000732 sc.line_entry.range,
733 sc,
Jim Ingham4b4b2472014-03-13 02:47:14 +0000734 stop_other_threads,
735 avoid_no_debug);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000736 }
737 else
738 {
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000739 new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction (true,
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000740 abort_other_plans,
741 stop_other_threads);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000742 }
743 }
744
Jim Ingham64e7ead2012-05-03 21:19:36 +0000745 // This returns an error, we should use it!
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000746 ResumeNewPlan (exe_ctx, new_plan_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000747 }
748}
749
750void
751SBThread::StepInto (lldb::RunMode stop_other_threads)
752{
Jim Inghamc6276822012-12-12 19:58:40 +0000753 StepInto (NULL, stop_other_threads);
754}
755
756void
757SBThread::StepInto (const char *target_name, lldb::RunMode stop_other_threads)
758{
Greg Clayton5160ce52013-03-27 23:08:40 +0000759 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000760
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000761 Mutex::Locker api_locker;
762 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
Greg Clayton17a6ad02012-01-30 02:53:15 +0000763
764 if (log)
Jim Inghamc6276822012-12-12 19:58:40 +0000765 log->Printf ("SBThread(%p)::StepInto (target_name='%s', stop_other_threads='%s')",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000766 static_cast<void*>(exe_ctx.GetThreadPtr()),
Jim Inghamc6276822012-12-12 19:58:40 +0000767 target_name? target_name: "<NULL>",
Greg Clayton17a6ad02012-01-30 02:53:15 +0000768 Thread::RunModeAsCString (stop_other_threads));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000769
Greg Clayton1ac04c32012-02-21 00:09:25 +0000770 if (exe_ctx.HasThreadScope())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000771 {
Jim Ingham7ba6e992012-05-11 23:47:32 +0000772 bool abort_other_plans = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000773
Greg Clayton1ac04c32012-02-21 00:09:25 +0000774 Thread *thread = exe_ctx.GetThreadPtr();
Jason Molendab57e4a12013-11-04 09:33:30 +0000775 StackFrameSP frame_sp(thread->GetStackFrameAtIndex (0));
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000776 ThreadPlanSP new_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000777
778 if (frame_sp && frame_sp->HasDebugInformation ())
779 {
Jim Ingham4b4b2472014-03-13 02:47:14 +0000780 const LazyBool step_out_avoids_code_without_debug_info = eLazyBoolCalculate;
781 const LazyBool step_in_avoids_code_without_debug_info = eLazyBoolCalculate;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000782 SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000783 new_plan_sp = thread->QueueThreadPlanForStepInRange (abort_other_plans,
Jim Inghamc6276822012-12-12 19:58:40 +0000784 sc.line_entry.range,
785 sc,
786 target_name,
787 stop_other_threads,
Jim Ingham4b4b2472014-03-13 02:47:14 +0000788 step_in_avoids_code_without_debug_info,
789 step_out_avoids_code_without_debug_info);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000790 }
791 else
792 {
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000793 new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction (false,
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000794 abort_other_plans,
795 stop_other_threads);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000796 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000797
Jim Ingham64e7ead2012-05-03 21:19:36 +0000798 // This returns an error, we should use it!
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000799 ResumeNewPlan (exe_ctx, new_plan_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000800 }
801}
802
803void
804SBThread::StepOut ()
805{
Greg Clayton5160ce52013-03-27 23:08:40 +0000806 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000807
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000808 Mutex::Locker api_locker;
809 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
810
Caroline Ticeceb6b132010-10-26 03:11:13 +0000811
Greg Clayton17a6ad02012-01-30 02:53:15 +0000812 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000813 log->Printf ("SBThread(%p)::StepOut ()",
814 static_cast<void*>(exe_ctx.GetThreadPtr()));
815
Greg Clayton1ac04c32012-02-21 00:09:25 +0000816 if (exe_ctx.HasThreadScope())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000817 {
Jim Ingham7ba6e992012-05-11 23:47:32 +0000818 bool abort_other_plans = false;
Jim Ingham94b09242012-09-14 21:07:14 +0000819 bool stop_other_threads = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000820
Greg Clayton1ac04c32012-02-21 00:09:25 +0000821 Thread *thread = exe_ctx.GetThreadPtr();
822
Jim Ingham4b4b2472014-03-13 02:47:14 +0000823 const LazyBool avoid_no_debug = eLazyBoolCalculate;
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000824 ThreadPlanSP new_plan_sp(thread->QueueThreadPlanForStepOut (abort_other_plans,
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000825 NULL,
826 false,
827 stop_other_threads,
828 eVoteYes,
829 eVoteNoOpinion,
830 0,
831 avoid_no_debug));
832
Jim Ingham64e7ead2012-05-03 21:19:36 +0000833 // This returns an error, we should use it!
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000834 ResumeNewPlan (exe_ctx, new_plan_sp.get());
Greg Clayton481cef22011-01-21 06:11:58 +0000835 }
836}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000837
Greg Clayton481cef22011-01-21 06:11:58 +0000838void
839SBThread::StepOutOfFrame (lldb::SBFrame &sb_frame)
840{
Greg Clayton5160ce52013-03-27 23:08:40 +0000841 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton481cef22011-01-21 06:11:58 +0000842
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000843 Mutex::Locker api_locker;
844 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
845
Jason Molendab57e4a12013-11-04 09:33:30 +0000846 StackFrameSP frame_sp (sb_frame.GetFrameSP());
Greg Clayton481cef22011-01-21 06:11:58 +0000847 if (log)
848 {
849 SBStream frame_desc_strm;
850 sb_frame.GetDescription (frame_desc_strm);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000851 log->Printf ("SBThread(%p)::StepOutOfFrame (frame = SBFrame(%p): %s)",
852 static_cast<void*>(exe_ctx.GetThreadPtr()),
853 static_cast<void*>(frame_sp.get()),
854 frame_desc_strm.GetData());
Greg Clayton481cef22011-01-21 06:11:58 +0000855 }
856
Greg Clayton1ac04c32012-02-21 00:09:25 +0000857 if (exe_ctx.HasThreadScope())
Greg Clayton481cef22011-01-21 06:11:58 +0000858 {
Jim Ingham7ba6e992012-05-11 23:47:32 +0000859 bool abort_other_plans = false;
Jim Ingham94b09242012-09-14 21:07:14 +0000860 bool stop_other_threads = false;
Greg Clayton1ac04c32012-02-21 00:09:25 +0000861 Thread *thread = exe_ctx.GetThreadPtr();
Greg Clayton481cef22011-01-21 06:11:58 +0000862
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000863 ThreadPlanSP new_plan_sp(thread->QueueThreadPlanForStepOut (abort_other_plans,
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000864 NULL,
865 false,
866 stop_other_threads,
867 eVoteYes,
Jim Ingham64e7ead2012-05-03 21:19:36 +0000868 eVoteNoOpinion,
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000869 frame_sp->GetFrameIndex()));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000870
Jim Ingham64e7ead2012-05-03 21:19:36 +0000871 // This returns an error, we should use it!
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000872 ResumeNewPlan (exe_ctx, new_plan_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000873 }
874}
875
876void
877SBThread::StepInstruction (bool step_over)
878{
Greg Clayton5160ce52013-03-27 23:08:40 +0000879 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000880
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000881 Mutex::Locker api_locker;
882 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
883
Greg Clayton1ac04c32012-02-21 00:09:25 +0000884
Caroline Ticeceb6b132010-10-26 03:11:13 +0000885
Greg Clayton17a6ad02012-01-30 02:53:15 +0000886 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000887 log->Printf ("SBThread(%p)::StepInstruction (step_over=%i)",
888 static_cast<void*>(exe_ctx.GetThreadPtr()), step_over);
889
Greg Clayton1ac04c32012-02-21 00:09:25 +0000890 if (exe_ctx.HasThreadScope())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000891 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000892 Thread *thread = exe_ctx.GetThreadPtr();
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000893 ThreadPlanSP new_plan_sp(thread->QueueThreadPlanForStepSingleInstruction (step_over, true, true));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000894
Jim Ingham64e7ead2012-05-03 21:19:36 +0000895 // This returns an error, we should use it!
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000896 ResumeNewPlan (exe_ctx, new_plan_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000897 }
898}
899
900void
901SBThread::RunToAddress (lldb::addr_t addr)
902{
Greg Clayton5160ce52013-03-27 23:08:40 +0000903 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000904
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000905 Mutex::Locker api_locker;
906 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
907
Caroline Ticeceb6b132010-10-26 03:11:13 +0000908
Greg Clayton17a6ad02012-01-30 02:53:15 +0000909 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000910 log->Printf ("SBThread(%p)::RunToAddress (addr=0x%" PRIx64 ")",
911 static_cast<void*>(exe_ctx.GetThreadPtr()), addr);
912
Greg Clayton1ac04c32012-02-21 00:09:25 +0000913 if (exe_ctx.HasThreadScope())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000914 {
Jim Ingham7ba6e992012-05-11 23:47:32 +0000915 bool abort_other_plans = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000916 bool stop_other_threads = true;
917
Greg Claytone72dfb32012-02-24 01:59:29 +0000918 Address target_addr (addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000919
Greg Clayton1ac04c32012-02-21 00:09:25 +0000920 Thread *thread = exe_ctx.GetThreadPtr();
Greg Clayton1ac04c32012-02-21 00:09:25 +0000921
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000922 ThreadPlanSP new_plan_sp(thread->QueueThreadPlanForRunToAddress (abort_other_plans,
923 target_addr,
924 stop_other_threads));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000925
Jim Ingham64e7ead2012-05-03 21:19:36 +0000926 // This returns an error, we should use it!
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000927 ResumeNewPlan (exe_ctx, new_plan_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000928 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000929}
930
Greg Clayton481cef22011-01-21 06:11:58 +0000931SBError
932SBThread::StepOverUntil (lldb::SBFrame &sb_frame,
933 lldb::SBFileSpec &sb_file_spec,
934 uint32_t line)
935{
936 SBError sb_error;
Greg Clayton5160ce52013-03-27 23:08:40 +0000937 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton481cef22011-01-21 06:11:58 +0000938 char path[PATH_MAX];
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000939
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000940 Mutex::Locker api_locker;
941 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
942
Jason Molendab57e4a12013-11-04 09:33:30 +0000943 StackFrameSP frame_sp (sb_frame.GetFrameSP());
Greg Claytonb9556ac2012-01-30 07:41:31 +0000944
Greg Clayton481cef22011-01-21 06:11:58 +0000945 if (log)
946 {
947 SBStream frame_desc_strm;
948 sb_frame.GetDescription (frame_desc_strm);
949 sb_file_spec->GetPath (path, sizeof(path));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000950 log->Printf ("SBThread(%p)::StepOverUntil (frame = SBFrame(%p): %s, file+line = %s:%u)",
951 static_cast<void*>(exe_ctx.GetThreadPtr()),
952 static_cast<void*>(frame_sp.get()),
953 frame_desc_strm.GetData(), path, line);
Greg Clayton481cef22011-01-21 06:11:58 +0000954 }
Greg Clayton17a6ad02012-01-30 02:53:15 +0000955
Greg Clayton1ac04c32012-02-21 00:09:25 +0000956 if (exe_ctx.HasThreadScope())
Greg Clayton481cef22011-01-21 06:11:58 +0000957 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000958 Target *target = exe_ctx.GetTargetPtr();
Greg Clayton1ac04c32012-02-21 00:09:25 +0000959 Thread *thread = exe_ctx.GetThreadPtr();
Greg Clayton481cef22011-01-21 06:11:58 +0000960
961 if (line == 0)
962 {
963 sb_error.SetErrorString("invalid line argument");
964 return sb_error;
965 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000966
Greg Claytonb9556ac2012-01-30 07:41:31 +0000967 if (!frame_sp)
Greg Clayton481cef22011-01-21 06:11:58 +0000968 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000969 frame_sp = thread->GetSelectedFrame ();
Greg Clayton481cef22011-01-21 06:11:58 +0000970 if (!frame_sp)
Greg Clayton1ac04c32012-02-21 00:09:25 +0000971 frame_sp = thread->GetStackFrameAtIndex (0);
Greg Clayton481cef22011-01-21 06:11:58 +0000972 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000973
Greg Clayton481cef22011-01-21 06:11:58 +0000974 SymbolContext frame_sc;
975 if (!frame_sp)
976 {
977 sb_error.SetErrorString("no valid frames in thread to step");
978 return sb_error;
979 }
980
981 // If we have a frame, get its line
982 frame_sc = frame_sp->GetSymbolContext (eSymbolContextCompUnit |
983 eSymbolContextFunction |
984 eSymbolContextLineEntry |
985 eSymbolContextSymbol );
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000986
Greg Clayton481cef22011-01-21 06:11:58 +0000987 if (frame_sc.comp_unit == NULL)
988 {
989 sb_error.SetErrorStringWithFormat("frame %u doesn't have debug information", frame_sp->GetFrameIndex());
990 return sb_error;
991 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000992
Greg Clayton481cef22011-01-21 06:11:58 +0000993 FileSpec step_file_spec;
994 if (sb_file_spec.IsValid())
995 {
996 // The file spec passed in was valid, so use it
997 step_file_spec = sb_file_spec.ref();
998 }
999 else
1000 {
1001 if (frame_sc.line_entry.IsValid())
1002 step_file_spec = frame_sc.line_entry.file;
1003 else
1004 {
1005 sb_error.SetErrorString("invalid file argument or no file for frame");
1006 return sb_error;
1007 }
1008 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001009
Jim Ingham9b70ddb2011-05-08 00:56:32 +00001010 // Grab the current function, then we will make sure the "until" address is
1011 // within the function. We discard addresses that are out of the current
1012 // function, and then if there are no addresses remaining, give an appropriate
1013 // error message.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001014
Jim Ingham9b70ddb2011-05-08 00:56:32 +00001015 bool all_in_function = true;
1016 AddressRange fun_range = frame_sc.function->GetAddressRange();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001017
Greg Clayton481cef22011-01-21 06:11:58 +00001018 std::vector<addr_t> step_over_until_addrs;
Jim Ingham7ba6e992012-05-11 23:47:32 +00001019 const bool abort_other_plans = false;
Jim Inghamc02e3342012-09-14 18:57:14 +00001020 const bool stop_other_threads = false;
Greg Clayton481cef22011-01-21 06:11:58 +00001021 const bool check_inlines = true;
1022 const bool exact = false;
1023
1024 SymbolContextList sc_list;
Jim Ingham9b70ddb2011-05-08 00:56:32 +00001025 const uint32_t num_matches = frame_sc.comp_unit->ResolveSymbolContext (step_file_spec,
1026 line,
1027 check_inlines,
1028 exact,
1029 eSymbolContextLineEntry,
1030 sc_list);
Greg Clayton481cef22011-01-21 06:11:58 +00001031 if (num_matches > 0)
1032 {
1033 SymbolContext sc;
1034 for (uint32_t i=0; i<num_matches; ++i)
1035 {
1036 if (sc_list.GetContextAtIndex(i, sc))
1037 {
Jim Ingham9b70ddb2011-05-08 00:56:32 +00001038 addr_t step_addr = sc.line_entry.range.GetBaseAddress().GetLoadAddress(target);
Greg Clayton481cef22011-01-21 06:11:58 +00001039 if (step_addr != LLDB_INVALID_ADDRESS)
1040 {
Jim Ingham9b70ddb2011-05-08 00:56:32 +00001041 if (fun_range.ContainsLoadAddress(step_addr, target))
1042 step_over_until_addrs.push_back(step_addr);
1043 else
1044 all_in_function = false;
Greg Clayton481cef22011-01-21 06:11:58 +00001045 }
1046 }
1047 }
1048 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001049
Greg Clayton481cef22011-01-21 06:11:58 +00001050 if (step_over_until_addrs.empty())
1051 {
Jim Ingham9b70ddb2011-05-08 00:56:32 +00001052 if (all_in_function)
1053 {
1054 step_file_spec.GetPath (path, sizeof(path));
Jason Molendafd54b362011-09-20 21:44:10 +00001055 sb_error.SetErrorStringWithFormat("No line entries for %s:%u", path, line);
Jim Ingham9b70ddb2011-05-08 00:56:32 +00001056 }
1057 else
Greg Clayton86edbf42011-10-26 00:56:27 +00001058 sb_error.SetErrorString ("step until target not in current function");
Greg Clayton481cef22011-01-21 06:11:58 +00001059 }
1060 else
1061 {
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001062 ThreadPlanSP new_plan_sp(thread->QueueThreadPlanForStepUntil (abort_other_plans,
Jim Ingham64e7ead2012-05-03 21:19:36 +00001063 &step_over_until_addrs[0],
1064 step_over_until_addrs.size(),
1065 stop_other_threads,
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001066 frame_sp->GetFrameIndex()));
Greg Clayton481cef22011-01-21 06:11:58 +00001067
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001068 sb_error = ResumeNewPlan (exe_ctx, new_plan_sp.get());
Greg Clayton481cef22011-01-21 06:11:58 +00001069 }
1070 }
1071 else
1072 {
1073 sb_error.SetErrorString("this SBThread object is invalid");
1074 }
1075 return sb_error;
1076}
1077
Jim Ingham44137582012-09-12 00:40:39 +00001078SBError
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001079SBThread::StepUsingScriptedThreadPlan (const char *script_class_name)
1080{
1081 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1082 SBError sb_error;
1083
1084 Mutex::Locker api_locker;
1085 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1086
1087 if (log)
1088 {
1089 log->Printf ("SBThread(%p)::StepUsingScriptedThreadPlan: class name: %s",
1090 static_cast<void*>(exe_ctx.GetThreadPtr()),
1091 script_class_name);
1092 }
1093
1094
1095 if (!exe_ctx.HasThreadScope())
1096 {
1097 sb_error.SetErrorString("this SBThread object is invalid");
1098 return sb_error;
1099 }
1100
1101 Thread *thread = exe_ctx.GetThreadPtr();
1102 ThreadPlanSP thread_plan_sp = thread->QueueThreadPlanForStepScripted(false, script_class_name, false);
1103
1104 if (thread_plan_sp)
1105 sb_error = ResumeNewPlan(exe_ctx, thread_plan_sp.get());
1106 else
1107 {
1108 sb_error.SetErrorStringWithFormat("Error queuing thread plan for class: %s.", script_class_name);
1109 if (log)
1110 log->Printf ("SBThread(%p)::StepUsingScriptedThreadPlan: Error queuing thread plan for class: %s",
1111 static_cast<void*>(exe_ctx.GetThreadPtr()),
1112 script_class_name);
1113 }
1114
1115 return sb_error;
1116}
1117
1118SBError
Richard Mittonf86248d2013-09-12 02:20:34 +00001119SBThread::JumpToLine (lldb::SBFileSpec &file_spec, uint32_t line)
1120{
1121 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1122 SBError sb_error;
1123
1124 Mutex::Locker api_locker;
1125 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1126
1127 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001128 log->Printf ("SBThread(%p)::JumpToLine (file+line = %s:%u)",
1129 static_cast<void*>(exe_ctx.GetThreadPtr()),
1130 file_spec->GetPath().c_str(), line);
Richard Mittonf86248d2013-09-12 02:20:34 +00001131
1132 if (!exe_ctx.HasThreadScope())
1133 {
1134 sb_error.SetErrorString("this SBThread object is invalid");
1135 return sb_error;
1136 }
1137
1138 Thread *thread = exe_ctx.GetThreadPtr();
1139
1140 Error err = thread->JumpToLine (file_spec.get(), line, true);
1141 sb_error.SetError (err);
1142 return sb_error;
1143}
1144
1145SBError
Jim Inghamcb640dd2012-09-14 02:14:15 +00001146SBThread::ReturnFromFrame (SBFrame &frame, SBValue &return_value)
Jim Ingham44137582012-09-12 00:40:39 +00001147{
1148 SBError sb_error;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001149
Greg Clayton5160ce52013-03-27 23:08:40 +00001150 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Ingham44137582012-09-12 00:40:39 +00001151
1152 Mutex::Locker api_locker;
1153 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1154
1155
1156 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001157 log->Printf ("SBThread(%p)::ReturnFromFrame (frame=%d)",
1158 static_cast<void*>(exe_ctx.GetThreadPtr()),
1159 frame.GetFrameID());
1160
Jim Ingham44137582012-09-12 00:40:39 +00001161 if (exe_ctx.HasThreadScope())
1162 {
1163 Thread *thread = exe_ctx.GetThreadPtr();
Jim Inghamcb640dd2012-09-14 02:14:15 +00001164 sb_error.SetError (thread->ReturnFromFrame(frame.GetFrameSP(), return_value.GetSP()));
Jim Ingham44137582012-09-12 00:40:39 +00001165 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001166
Jim Ingham44137582012-09-12 00:40:39 +00001167 return sb_error;
1168}
1169
Greg Clayton481cef22011-01-21 06:11:58 +00001170
Greg Clayton722a0cd2011-01-12 02:25:42 +00001171bool
1172SBThread::Suspend()
1173{
Greg Clayton5160ce52013-03-27 23:08:40 +00001174 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001175 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Claytonc9858e42012-04-06 02:17:47 +00001176 bool result = false;
Greg Clayton1ac04c32012-02-21 00:09:25 +00001177 if (exe_ctx.HasThreadScope())
Greg Clayton722a0cd2011-01-12 02:25:42 +00001178 {
Greg Claytonc9858e42012-04-06 02:17:47 +00001179 Process::StopLocker stop_locker;
1180 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1181 {
1182 exe_ctx.GetThreadPtr()->SetResumeState (eStateSuspended);
1183 result = true;
1184 }
1185 else
1186 {
1187 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001188 log->Printf ("SBThread(%p)::Suspend() => error: process is running",
1189 static_cast<void*>(exe_ctx.GetThreadPtr()));
Greg Claytonc9858e42012-04-06 02:17:47 +00001190 }
Greg Clayton722a0cd2011-01-12 02:25:42 +00001191 }
Greg Claytonc9858e42012-04-06 02:17:47 +00001192 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001193 log->Printf ("SBThread(%p)::Suspend() => %i",
1194 static_cast<void*>(exe_ctx.GetThreadPtr()), result);
Greg Claytonc9858e42012-04-06 02:17:47 +00001195 return result;
Greg Clayton722a0cd2011-01-12 02:25:42 +00001196}
1197
1198bool
1199SBThread::Resume ()
1200{
Greg Clayton5160ce52013-03-27 23:08:40 +00001201 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001202 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Claytonc9858e42012-04-06 02:17:47 +00001203 bool result = false;
Greg Clayton1ac04c32012-02-21 00:09:25 +00001204 if (exe_ctx.HasThreadScope())
Greg Clayton722a0cd2011-01-12 02:25:42 +00001205 {
Greg Claytonc9858e42012-04-06 02:17:47 +00001206 Process::StopLocker stop_locker;
1207 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1208 {
Jim Ingham6c9ed912014-04-03 01:26:14 +00001209 const bool override_suspend = true;
1210 exe_ctx.GetThreadPtr()->SetResumeState (eStateRunning, override_suspend);
Greg Claytonc9858e42012-04-06 02:17:47 +00001211 result = true;
1212 }
1213 else
1214 {
1215 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001216 log->Printf ("SBThread(%p)::Resume() => error: process is running",
1217 static_cast<void*>(exe_ctx.GetThreadPtr()));
Greg Claytonc9858e42012-04-06 02:17:47 +00001218 }
Greg Clayton722a0cd2011-01-12 02:25:42 +00001219 }
Greg Claytonc9858e42012-04-06 02:17:47 +00001220 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001221 log->Printf ("SBThread(%p)::Resume() => %i",
1222 static_cast<void*>(exe_ctx.GetThreadPtr()), result);
Greg Claytonc9858e42012-04-06 02:17:47 +00001223 return result;
Greg Clayton722a0cd2011-01-12 02:25:42 +00001224}
1225
1226bool
1227SBThread::IsSuspended()
1228{
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001229 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Clayton1ac04c32012-02-21 00:09:25 +00001230 if (exe_ctx.HasThreadScope())
1231 return exe_ctx.GetThreadPtr()->GetResumeState () == eStateSuspended;
Greg Clayton722a0cd2011-01-12 02:25:42 +00001232 return false;
1233}
1234
Andrew Kaylora75418d2013-04-15 23:33:53 +00001235bool
1236SBThread::IsStopped()
1237{
1238 ExecutionContext exe_ctx (m_opaque_sp.get());
1239 if (exe_ctx.HasThreadScope())
1240 return StateIsStoppedState(exe_ctx.GetThreadPtr()->GetState(), true);
1241 return false;
1242}
1243
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001244SBProcess
1245SBThread::GetProcess ()
1246{
Greg Claytonb9556ac2012-01-30 07:41:31 +00001247 SBProcess sb_process;
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001248 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Clayton1ac04c32012-02-21 00:09:25 +00001249 if (exe_ctx.HasThreadScope())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001250 {
1251 // Have to go up to the target so we can get a shared pointer to our process...
Greg Clayton1ac04c32012-02-21 00:09:25 +00001252 sb_process.SetSP (exe_ctx.GetProcessSP());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001253 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001254
Greg Clayton5160ce52013-03-27 23:08:40 +00001255 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001256 if (log)
1257 {
Greg Clayton481cef22011-01-21 06:11:58 +00001258 SBStream frame_desc_strm;
Greg Claytonb9556ac2012-01-30 07:41:31 +00001259 sb_process.GetDescription (frame_desc_strm);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001260 log->Printf ("SBThread(%p)::GetProcess () => SBProcess(%p): %s",
1261 static_cast<void*>(exe_ctx.GetThreadPtr()),
1262 static_cast<void*>(sb_process.GetSP().get()),
1263 frame_desc_strm.GetData());
Caroline Ticeceb6b132010-10-26 03:11:13 +00001264 }
1265
Greg Claytonb9556ac2012-01-30 07:41:31 +00001266 return sb_process;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001267}
1268
1269uint32_t
1270SBThread::GetNumFrames ()
1271{
Greg Clayton5160ce52013-03-27 23:08:40 +00001272 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001273
Caroline Ticeceb6b132010-10-26 03:11:13 +00001274 uint32_t num_frames = 0;
Jim Ingham4fc6cb92012-08-22 21:34:33 +00001275 Mutex::Locker api_locker;
1276 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1277
Greg Clayton1ac04c32012-02-21 00:09:25 +00001278 if (exe_ctx.HasThreadScope())
Greg Claytonaf67cec2010-12-20 20:49:23 +00001279 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001280 Process::StopLocker stop_locker;
1281 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1282 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001283 num_frames = exe_ctx.GetThreadPtr()->GetStackFrameCount();
1284 }
Greg Claytonc9858e42012-04-06 02:17:47 +00001285 else
1286 {
1287 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001288 log->Printf ("SBThread(%p)::GetNumFrames() => error: process is running",
1289 static_cast<void*>(exe_ctx.GetThreadPtr()));
Greg Claytonc9858e42012-04-06 02:17:47 +00001290 }
Greg Claytonaf67cec2010-12-20 20:49:23 +00001291 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001292
1293 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001294 log->Printf ("SBThread(%p)::GetNumFrames () => %u",
1295 static_cast<void*>(exe_ctx.GetThreadPtr()), num_frames);
Caroline Ticeceb6b132010-10-26 03:11:13 +00001296
1297 return num_frames;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001298}
1299
1300SBFrame
1301SBThread::GetFrameAtIndex (uint32_t idx)
1302{
Greg Clayton5160ce52013-03-27 23:08:40 +00001303 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001304
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001305 SBFrame sb_frame;
Jason Molendab57e4a12013-11-04 09:33:30 +00001306 StackFrameSP frame_sp;
Jim Ingham4fc6cb92012-08-22 21:34:33 +00001307 Mutex::Locker api_locker;
1308 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1309
Greg Clayton1ac04c32012-02-21 00:09:25 +00001310 if (exe_ctx.HasThreadScope())
Greg Claytonaf67cec2010-12-20 20:49:23 +00001311 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001312 Process::StopLocker stop_locker;
1313 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1314 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001315 frame_sp = exe_ctx.GetThreadPtr()->GetStackFrameAtIndex (idx);
1316 sb_frame.SetFrameSP (frame_sp);
1317 }
Greg Claytonc9858e42012-04-06 02:17:47 +00001318 else
1319 {
1320 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001321 log->Printf ("SBThread(%p)::GetFrameAtIndex() => error: process is running",
1322 static_cast<void*>(exe_ctx.GetThreadPtr()));
Greg Claytonc9858e42012-04-06 02:17:47 +00001323 }
Greg Claytonaf67cec2010-12-20 20:49:23 +00001324 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001325
1326 if (log)
1327 {
Greg Clayton481cef22011-01-21 06:11:58 +00001328 SBStream frame_desc_strm;
1329 sb_frame.GetDescription (frame_desc_strm);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001330 log->Printf ("SBThread(%p)::GetFrameAtIndex (idx=%d) => SBFrame(%p): %s",
1331 static_cast<void*>(exe_ctx.GetThreadPtr()), idx,
1332 static_cast<void*>(frame_sp.get()),
1333 frame_desc_strm.GetData());
Caroline Ticeceb6b132010-10-26 03:11:13 +00001334 }
1335
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001336 return sb_frame;
1337}
1338
Greg Claytonf028a1f2010-12-17 02:26:24 +00001339lldb::SBFrame
1340SBThread::GetSelectedFrame ()
1341{
Greg Clayton5160ce52013-03-27 23:08:40 +00001342 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytonf028a1f2010-12-17 02:26:24 +00001343
1344 SBFrame sb_frame;
Jason Molendab57e4a12013-11-04 09:33:30 +00001345 StackFrameSP frame_sp;
Jim Ingham4fc6cb92012-08-22 21:34:33 +00001346 Mutex::Locker api_locker;
1347 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1348
Greg Clayton1ac04c32012-02-21 00:09:25 +00001349 if (exe_ctx.HasThreadScope())
Greg Claytonaf67cec2010-12-20 20:49:23 +00001350 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001351 Process::StopLocker stop_locker;
1352 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1353 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001354 frame_sp = exe_ctx.GetThreadPtr()->GetSelectedFrame ();
1355 sb_frame.SetFrameSP (frame_sp);
1356 }
Greg Claytonc9858e42012-04-06 02:17:47 +00001357 else
1358 {
1359 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001360 log->Printf ("SBThread(%p)::GetSelectedFrame() => error: process is running",
1361 static_cast<void*>(exe_ctx.GetThreadPtr()));
Greg Claytonc9858e42012-04-06 02:17:47 +00001362 }
Greg Claytonaf67cec2010-12-20 20:49:23 +00001363 }
Greg Claytonf028a1f2010-12-17 02:26:24 +00001364
1365 if (log)
1366 {
Greg Clayton481cef22011-01-21 06:11:58 +00001367 SBStream frame_desc_strm;
1368 sb_frame.GetDescription (frame_desc_strm);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001369 log->Printf ("SBThread(%p)::GetSelectedFrame () => SBFrame(%p): %s",
1370 static_cast<void*>(exe_ctx.GetThreadPtr()),
1371 static_cast<void*>(frame_sp.get()),
1372 frame_desc_strm.GetData());
Greg Claytonf028a1f2010-12-17 02:26:24 +00001373 }
1374
1375 return sb_frame;
1376}
1377
1378lldb::SBFrame
1379SBThread::SetSelectedFrame (uint32_t idx)
1380{
Greg Clayton5160ce52013-03-27 23:08:40 +00001381 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytonf028a1f2010-12-17 02:26:24 +00001382
1383 SBFrame sb_frame;
Jason Molendab57e4a12013-11-04 09:33:30 +00001384 StackFrameSP frame_sp;
Jim Ingham4fc6cb92012-08-22 21:34:33 +00001385 Mutex::Locker api_locker;
1386 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1387
Greg Clayton1ac04c32012-02-21 00:09:25 +00001388 if (exe_ctx.HasThreadScope())
Greg Claytonf028a1f2010-12-17 02:26:24 +00001389 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001390 Process::StopLocker stop_locker;
1391 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
Greg Claytonf028a1f2010-12-17 02:26:24 +00001392 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001393 Thread *thread = exe_ctx.GetThreadPtr();
1394 frame_sp = thread->GetStackFrameAtIndex (idx);
1395 if (frame_sp)
1396 {
1397 thread->SetSelectedFrame (frame_sp.get());
1398 sb_frame.SetFrameSP (frame_sp);
1399 }
Greg Claytonf028a1f2010-12-17 02:26:24 +00001400 }
Greg Claytonc9858e42012-04-06 02:17:47 +00001401 else
1402 {
1403 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001404 log->Printf ("SBThread(%p)::SetSelectedFrame() => error: process is running",
1405 static_cast<void*>(exe_ctx.GetThreadPtr()));
Greg Claytonc9858e42012-04-06 02:17:47 +00001406 }
Greg Claytonf028a1f2010-12-17 02:26:24 +00001407 }
1408
1409 if (log)
1410 {
Greg Clayton481cef22011-01-21 06:11:58 +00001411 SBStream frame_desc_strm;
1412 sb_frame.GetDescription (frame_desc_strm);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001413 log->Printf ("SBThread(%p)::SetSelectedFrame (idx=%u) => SBFrame(%p): %s",
1414 static_cast<void*>(exe_ctx.GetThreadPtr()), idx,
1415 static_cast<void*>(frame_sp.get()),
1416 frame_desc_strm.GetData());
Greg Claytonf028a1f2010-12-17 02:26:24 +00001417 }
1418 return sb_frame;
1419}
1420
Jim Ingham4f465cf2012-10-10 18:32:14 +00001421bool
1422SBThread::EventIsThreadEvent (const SBEvent &event)
1423{
1424 return Thread::ThreadEventData::GetEventDataFromEvent(event.get()) != NULL;
1425}
1426
1427SBFrame
1428SBThread::GetStackFrameFromEvent (const SBEvent &event)
1429{
1430 return Thread::ThreadEventData::GetStackFrameFromEvent (event.get());
1431
1432}
1433
1434SBThread
1435SBThread::GetThreadFromEvent (const SBEvent &event)
1436{
1437 return Thread::ThreadEventData::GetThreadFromEvent (event.get());
1438}
Greg Claytonf028a1f2010-12-17 02:26:24 +00001439
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001440bool
1441SBThread::operator == (const SBThread &rhs) const
1442{
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001443 return m_opaque_sp->GetThreadSP().get() == rhs.m_opaque_sp->GetThreadSP().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001444}
1445
1446bool
1447SBThread::operator != (const SBThread &rhs) const
1448{
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001449 return m_opaque_sp->GetThreadSP().get() != rhs.m_opaque_sp->GetThreadSP().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001450}
Caroline Ticedde9cff2010-09-20 05:20:02 +00001451
1452bool
Jim Ingham4f465cf2012-10-10 18:32:14 +00001453SBThread::GetStatus (SBStream &status) const
1454{
1455 Stream &strm = status.ref();
1456
1457 ExecutionContext exe_ctx (m_opaque_sp.get());
1458 if (exe_ctx.HasThreadScope())
1459 {
1460 exe_ctx.GetThreadPtr()->GetStatus(strm, 0, 1, 1);
1461 }
1462 else
1463 strm.PutCString ("No status");
1464
1465 return true;
1466}
1467
1468bool
Caroline Ticeceb6b132010-10-26 03:11:13 +00001469SBThread::GetDescription (SBStream &description) const
1470{
Greg Claytonda7bc7d2011-11-13 06:57:31 +00001471 Stream &strm = description.ref();
1472
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001473 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Clayton1ac04c32012-02-21 00:09:25 +00001474 if (exe_ctx.HasThreadScope())
Caroline Ticeceb6b132010-10-26 03:11:13 +00001475 {
Daniel Malead01b2952012-11-29 21:49:15 +00001476 strm.Printf("SBThread: tid = 0x%4.4" PRIx64, exe_ctx.GetThreadPtr()->GetID());
Caroline Ticeceb6b132010-10-26 03:11:13 +00001477 }
1478 else
Greg Claytonda7bc7d2011-11-13 06:57:31 +00001479 strm.PutCString ("No value");
Caroline Ticeceb6b132010-10-26 03:11:13 +00001480
1481 return true;
1482}
Jason Molenda5dd49162013-11-06 00:04:44 +00001483
1484SBThread
Jason Molenda008c45f2013-11-12 23:33:32 +00001485SBThread::GetExtendedBacktraceThread (const char *type)
Jason Molenda5dd49162013-11-06 00:04:44 +00001486{
1487 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1488 Mutex::Locker api_locker;
1489 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1490 SBThread sb_origin_thread;
1491
1492 if (exe_ctx.HasThreadScope())
1493 {
1494 Process::StopLocker stop_locker;
1495 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1496 {
Jason Molenda7a2f7902013-11-11 05:19:34 +00001497 ThreadSP real_thread(exe_ctx.GetThreadSP());
Jason Molenda5dd49162013-11-06 00:04:44 +00001498 if (real_thread)
1499 {
1500 ConstString type_const (type);
Jason Molenda7a2f7902013-11-11 05:19:34 +00001501 Process *process = exe_ctx.GetProcessPtr();
1502 if (process)
Jason Molenda5dd49162013-11-06 00:04:44 +00001503 {
Jason Molenda7a2f7902013-11-11 05:19:34 +00001504 SystemRuntime *runtime = process->GetSystemRuntime();
1505 if (runtime)
1506 {
Jason Molenda008c45f2013-11-12 23:33:32 +00001507 ThreadSP new_thread_sp (runtime->GetExtendedBacktraceThread (real_thread, type_const));
Jason Molendaa6e91302013-11-19 05:44:41 +00001508 if (new_thread_sp)
1509 {
1510 // Save this in the Process' ExtendedThreadList so a strong pointer retains the
1511 // object.
1512 process->GetExtendedThreadList().AddThread (new_thread_sp);
1513 sb_origin_thread.SetThread (new_thread_sp);
1514 if (log)
1515 {
1516 const char *queue_name = new_thread_sp->GetQueueName();
1517 if (queue_name == NULL)
1518 queue_name = "";
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001519 log->Printf ("SBThread(%p)::GetExtendedBacktraceThread() => new extended Thread "
1520 "created (%p) with queue_id 0x%" PRIx64 " queue name '%s'",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001521 static_cast<void*>(exe_ctx.GetThreadPtr()),
1522 static_cast<void*>(new_thread_sp.get()),
1523 new_thread_sp->GetQueueID(),
1524 queue_name);
Jason Molendaa6e91302013-11-19 05:44:41 +00001525 }
1526 }
Jason Molenda7a2f7902013-11-11 05:19:34 +00001527 }
Jason Molenda5dd49162013-11-06 00:04:44 +00001528 }
1529 }
1530 }
1531 else
1532 {
1533 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001534 log->Printf ("SBThread(%p)::GetExtendedBacktraceThread() => error: process is running",
1535 static_cast<void*>(exe_ctx.GetThreadPtr()));
Jason Molenda5dd49162013-11-06 00:04:44 +00001536 }
1537 }
1538
Jason Molendaac605f42014-03-08 01:34:55 +00001539 if (log && sb_origin_thread.IsValid() == false)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001540 log->Printf("SBThread(%p)::GetExtendedBacktraceThread() is not returning a Valid thread",
1541 static_cast<void*>(exe_ctx.GetThreadPtr()));
Jason Molenda5dd49162013-11-06 00:04:44 +00001542 return sb_origin_thread;
1543}
Jason Molenda8ee9cb52013-11-16 01:24:22 +00001544
1545uint32_t
1546SBThread::GetExtendedBacktraceOriginatingIndexID ()
1547{
1548 ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
1549 if (thread_sp)
1550 return thread_sp->GetExtendedBacktraceOriginatingIndexID();
1551 return LLDB_INVALID_INDEX32;
1552}
Jason Molendab4892cd2014-05-13 22:02:48 +00001553
1554bool
1555SBThread::SafeToCallFunctions ()
1556{
1557 ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
1558 if (thread_sp)
1559 return thread_sp->SafeToCallFunctions();
1560 return true;
1561}
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001562
1563lldb_private::Thread *
1564SBThread::operator->()
1565{
1566 ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
1567 if (thread_sp)
1568 return thread_sp.get();
1569 else
1570 return NULL;
1571}
1572
1573lldb_private::Thread *
1574SBThread::get()
1575{
1576 ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
1577 if (thread_sp)
1578 return thread_sp.get();
1579 else
1580 return NULL;
1581}
1582