blob: 036ce10fcdee588140f27e80815a80446ccecee3 [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:
Kuba Breckaafdf8422014-10-10 23:43:03 +0000198 case eStopReasonInstrumentation:
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000199 // There is no data for these stop reasons.
200 return 0;
201
202 case eStopReasonBreakpoint:
203 {
204 break_id_t site_id = stop_info_sp->GetValue();
205 lldb::BreakpointSiteSP bp_site_sp (exe_ctx.GetProcessPtr()->GetBreakpointSiteList().FindByID (site_id));
206 if (bp_site_sp)
207 return bp_site_sp->GetNumberOfOwners () * 2;
208 else
209 return 0; // Breakpoint must have cleared itself...
210 }
211 break;
212
213 case eStopReasonWatchpoint:
214 return 1;
215
216 case eStopReasonSignal:
217 return 1;
218
219 case eStopReasonException:
220 return 1;
Greg Clayton4e78f602010-11-18 18:52:36 +0000221 }
Greg Clayton4e78f602010-11-18 18:52:36 +0000222 }
223 }
Greg Claytonc9858e42012-04-06 02:17:47 +0000224 else
225 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000226 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytonc9858e42012-04-06 02:17:47 +0000227 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000228 log->Printf ("SBThread(%p)::GetStopReasonDataCount() => error: process is running",
229 static_cast<void*>(exe_ctx.GetThreadPtr()));
Greg Claytonc9858e42012-04-06 02:17:47 +0000230 }
Greg Clayton4e78f602010-11-18 18:52:36 +0000231 }
232 return 0;
233}
234
235uint64_t
236SBThread::GetStopReasonDataAtIndex (uint32_t idx)
237{
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000238 Mutex::Locker api_locker;
239 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
240
Greg Clayton1ac04c32012-02-21 00:09:25 +0000241 if (exe_ctx.HasThreadScope())
Greg Clayton4e78f602010-11-18 18:52:36 +0000242 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000243 Process::StopLocker stop_locker;
244 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
Greg Clayton4e78f602010-11-18 18:52:36 +0000245 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000246 Thread *thread = exe_ctx.GetThreadPtr();
247 StopInfoSP stop_info_sp = thread->GetStopInfo ();
248 if (stop_info_sp)
249 {
250 StopReason reason = stop_info_sp->GetStopReason();
251 switch (reason)
Greg Clayton4e78f602010-11-18 18:52:36 +0000252 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000253 case eStopReasonInvalid:
254 case eStopReasonNone:
255 case eStopReasonTrace:
Greg Clayton90ba8112012-12-05 00:16:59 +0000256 case eStopReasonExec:
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000257 case eStopReasonPlanComplete:
Andrew Kaylorf85defa2012-12-20 23:08:03 +0000258 case eStopReasonThreadExiting:
Kuba Breckaafdf8422014-10-10 23:43:03 +0000259 case eStopReasonInstrumentation:
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000260 // There is no data for these stop reasons.
261 return 0;
262
263 case eStopReasonBreakpoint:
Greg Clayton4e78f602010-11-18 18:52:36 +0000264 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000265 break_id_t site_id = stop_info_sp->GetValue();
266 lldb::BreakpointSiteSP bp_site_sp (exe_ctx.GetProcessPtr()->GetBreakpointSiteList().FindByID (site_id));
267 if (bp_site_sp)
Greg Clayton4e78f602010-11-18 18:52:36 +0000268 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000269 uint32_t bp_index = idx / 2;
270 BreakpointLocationSP bp_loc_sp (bp_site_sp->GetOwnerAtIndex (bp_index));
271 if (bp_loc_sp)
Greg Clayton4e78f602010-11-18 18:52:36 +0000272 {
Greg Clayton8334e142014-04-11 17:27:02 +0000273 if (idx & 1)
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000274 {
275 // Odd idx, return the breakpoint location ID
276 return bp_loc_sp->GetID();
277 }
278 else
279 {
280 // Even idx, return the breakpoint ID
281 return bp_loc_sp->GetBreakpoint().GetID();
282 }
Greg Clayton4e78f602010-11-18 18:52:36 +0000283 }
284 }
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000285 return LLDB_INVALID_BREAK_ID;
Greg Clayton4e78f602010-11-18 18:52:36 +0000286 }
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000287 break;
288
289 case eStopReasonWatchpoint:
290 return stop_info_sp->GetValue();
291
292 case eStopReasonSignal:
293 return stop_info_sp->GetValue();
294
295 case eStopReasonException:
296 return stop_info_sp->GetValue();
Greg Clayton4e78f602010-11-18 18:52:36 +0000297 }
Greg Clayton4e78f602010-11-18 18:52:36 +0000298 }
299 }
Greg Claytonc9858e42012-04-06 02:17:47 +0000300 else
301 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000302 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytonc9858e42012-04-06 02:17:47 +0000303 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000304 log->Printf ("SBThread(%p)::GetStopReasonDataAtIndex() => error: process is running",
305 static_cast<void*>(exe_ctx.GetThreadPtr()));
Greg Claytonc9858e42012-04-06 02:17:47 +0000306 }
Greg Clayton4e78f602010-11-18 18:52:36 +0000307 }
308 return 0;
309}
310
Kuba Breckaafdf8422014-10-10 23:43:03 +0000311bool
312SBThread::GetStopReasonExtendedInfoAsJSON (lldb::SBStream &stream)
313{
314 Stream &strm = stream.ref();
315
316 ExecutionContext exe_ctx (m_opaque_sp.get());
317 if (! exe_ctx.HasThreadScope())
318 return false;
319
320
321 StopInfoSP stop_info = exe_ctx.GetThreadPtr()->GetStopInfo();
322 StructuredData::ObjectSP info = stop_info->GetExtendedInfo();
323 if (! info)
324 return false;
325
326 info->Dump(strm);
327
328 return true;
329}
330
Greg Clayton4e78f602010-11-18 18:52:36 +0000331size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000332SBThread::GetStopDescription (char *dst, size_t dst_len)
333{
Greg Clayton5160ce52013-03-27 23:08:40 +0000334 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000335
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000336 Mutex::Locker api_locker;
337 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
338
Greg Clayton1ac04c32012-02-21 00:09:25 +0000339 if (exe_ctx.HasThreadScope())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000340 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000341 Process::StopLocker stop_locker;
342 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000343 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000344
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000345 StopInfoSP stop_info_sp = exe_ctx.GetThreadPtr()->GetStopInfo ();
346 if (stop_info_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000347 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000348 const char *stop_desc = stop_info_sp->GetDescription();
349 if (stop_desc)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000350 {
Caroline Ticeceb6b132010-10-26 03:11:13 +0000351 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000352 log->Printf ("SBThread(%p)::GetStopDescription (dst, dst_len) => \"%s\"",
353 static_cast<void*>(exe_ctx.GetThreadPtr()),
354 stop_desc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000355 if (dst)
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000356 return ::snprintf (dst, dst_len, "%s", stop_desc);
357 else
358 {
359 // NULL dst passed in, return the length needed to contain the description
360 return ::strlen (stop_desc) + 1; // Include the NULL byte for size
361 }
362 }
363 else
364 {
365 size_t stop_desc_len = 0;
366 switch (stop_info_sp->GetStopReason())
367 {
368 case eStopReasonTrace:
369 case eStopReasonPlanComplete:
370 {
371 static char trace_desc[] = "step";
372 stop_desc = trace_desc;
373 stop_desc_len = sizeof(trace_desc); // Include the NULL byte for size
374 }
375 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000376
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000377 case eStopReasonBreakpoint:
378 {
379 static char bp_desc[] = "breakpoint hit";
380 stop_desc = bp_desc;
381 stop_desc_len = sizeof(bp_desc); // Include the NULL byte for size
382 }
383 break;
384
385 case eStopReasonWatchpoint:
386 {
387 static char wp_desc[] = "watchpoint hit";
388 stop_desc = wp_desc;
389 stop_desc_len = sizeof(wp_desc); // Include the NULL byte for size
390 }
391 break;
392
393 case eStopReasonSignal:
394 {
395 stop_desc = exe_ctx.GetProcessPtr()->GetUnixSignals ().GetSignalAsCString (stop_info_sp->GetValue());
396 if (stop_desc == NULL || stop_desc[0] == '\0')
397 {
398 static char signal_desc[] = "signal";
399 stop_desc = signal_desc;
400 stop_desc_len = sizeof(signal_desc); // Include the NULL byte for size
401 }
402 }
403 break;
404
405 case eStopReasonException:
406 {
407 char exc_desc[] = "exception";
408 stop_desc = exc_desc;
409 stop_desc_len = sizeof(exc_desc); // Include the NULL byte for size
410 }
411 break;
412
Greg Clayton90ba8112012-12-05 00:16:59 +0000413 case eStopReasonExec:
414 {
415 char exc_desc[] = "exec";
416 stop_desc = exc_desc;
417 stop_desc_len = sizeof(exc_desc); // Include the NULL byte for size
418 }
419 break;
420
Andrew Kaylorf85defa2012-12-20 23:08:03 +0000421 case eStopReasonThreadExiting:
422 {
423 char limbo_desc[] = "thread exiting";
424 stop_desc = limbo_desc;
425 stop_desc_len = sizeof(limbo_desc);
426 }
427 break;
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000428 default:
429 break;
430 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000431
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000432 if (stop_desc && stop_desc[0])
433 {
434 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000435 log->Printf ("SBThread(%p)::GetStopDescription (dst, dst_len) => '%s'",
436 static_cast<void*>(exe_ctx.GetThreadPtr()),
437 stop_desc);
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000438
439 if (dst)
440 return ::snprintf (dst, dst_len, "%s", stop_desc) + 1; // Include the NULL byte
441
442 if (stop_desc_len == 0)
443 stop_desc_len = ::strlen (stop_desc) + 1; // Include the NULL byte
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000444
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000445 return stop_desc_len;
446 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000447 }
448 }
449 }
Greg Claytonc9858e42012-04-06 02:17:47 +0000450 else
451 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000452 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytonc9858e42012-04-06 02:17:47 +0000453 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000454 log->Printf ("SBThread(%p)::GetStopDescription() => error: process is running",
455 static_cast<void*>(exe_ctx.GetThreadPtr()));
Greg Claytonc9858e42012-04-06 02:17:47 +0000456 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000457 }
458 if (dst)
459 *dst = 0;
460 return 0;
461}
462
Jim Ingham73ca05a2011-12-17 01:35:57 +0000463SBValue
464SBThread::GetStopReturnValue ()
465{
Greg Clayton5160ce52013-03-27 23:08:40 +0000466 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Ingham73ca05a2011-12-17 01:35:57 +0000467 ValueObjectSP return_valobj_sp;
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000468 Mutex::Locker api_locker;
469 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
470
Greg Clayton1ac04c32012-02-21 00:09:25 +0000471 if (exe_ctx.HasThreadScope())
Jim Ingham73ca05a2011-12-17 01:35:57 +0000472 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000473 Process::StopLocker stop_locker;
474 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
Jim Ingham73ca05a2011-12-17 01:35:57 +0000475 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000476 StopInfoSP stop_info_sp = exe_ctx.GetThreadPtr()->GetStopInfo ();
477 if (stop_info_sp)
478 {
479 return_valobj_sp = StopInfo::GetReturnValueObject (stop_info_sp);
480 }
Jim Ingham73ca05a2011-12-17 01:35:57 +0000481 }
Greg Claytonc9858e42012-04-06 02:17:47 +0000482 else
483 {
484 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000485 log->Printf ("SBThread(%p)::GetStopReturnValue() => error: process is running",
486 static_cast<void*>(exe_ctx.GetThreadPtr()));
Greg Claytonc9858e42012-04-06 02:17:47 +0000487 }
Jim Ingham73ca05a2011-12-17 01:35:57 +0000488 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000489
Jim Ingham73ca05a2011-12-17 01:35:57 +0000490 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000491 log->Printf ("SBThread(%p)::GetStopReturnValue () => %s",
492 static_cast<void*>(exe_ctx.GetThreadPtr()),
493 return_valobj_sp.get()
494 ? return_valobj_sp->GetValueAsCString()
495 : "<no return value>");
496
Jim Ingham73ca05a2011-12-17 01:35:57 +0000497 return SBValue (return_valobj_sp);
498}
499
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000500void
501SBThread::SetThread (const ThreadSP& lldb_object_sp)
502{
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000503 m_opaque_sp->SetThreadSP (lldb_object_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000504}
505
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000506lldb::tid_t
507SBThread::GetThreadID () const
508{
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000509 ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
Greg Clayton17a6ad02012-01-30 02:53:15 +0000510 if (thread_sp)
Greg Clayton1ac04c32012-02-21 00:09:25 +0000511 return thread_sp->GetID();
512 return LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000513}
514
515uint32_t
516SBThread::GetIndexID () const
517{
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000518 ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
Greg Clayton17a6ad02012-01-30 02:53:15 +0000519 if (thread_sp)
520 return thread_sp->GetIndexID();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000521 return LLDB_INVALID_INDEX32;
522}
Greg Clayton1ac04c32012-02-21 00:09:25 +0000523
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000524const char *
525SBThread::GetName () const
526{
Greg Clayton5160ce52013-03-27 23:08:40 +0000527 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton48381312010-10-30 04:51:46 +0000528 const char *name = NULL;
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000529 Mutex::Locker api_locker;
530 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
531
Greg Clayton1ac04c32012-02-21 00:09:25 +0000532 if (exe_ctx.HasThreadScope())
Greg Claytonaf67cec2010-12-20 20:49:23 +0000533 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000534 Process::StopLocker stop_locker;
535 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
536 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000537 name = exe_ctx.GetThreadPtr()->GetName();
538 }
Greg Claytonc9858e42012-04-06 02:17:47 +0000539 else
540 {
541 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000542 log->Printf ("SBThread(%p)::GetName() => error: process is running",
543 static_cast<void*>(exe_ctx.GetThreadPtr()));
Greg Claytonc9858e42012-04-06 02:17:47 +0000544 }
Greg Claytonaf67cec2010-12-20 20:49:23 +0000545 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000546
Caroline Ticeceb6b132010-10-26 03:11:13 +0000547 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000548 log->Printf ("SBThread(%p)::GetName () => %s",
549 static_cast<void*>(exe_ctx.GetThreadPtr()),
550 name ? name : "NULL");
Caroline Ticeceb6b132010-10-26 03:11:13 +0000551
Greg Clayton48381312010-10-30 04:51:46 +0000552 return name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000553}
554
555const char *
556SBThread::GetQueueName () const
557{
Greg Clayton48381312010-10-30 04:51:46 +0000558 const char *name = NULL;
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000559 Mutex::Locker api_locker;
560 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
561
Greg Clayton5160ce52013-03-27 23:08:40 +0000562 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton1ac04c32012-02-21 00:09:25 +0000563 if (exe_ctx.HasThreadScope())
Greg Claytonaf67cec2010-12-20 20:49:23 +0000564 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000565 Process::StopLocker stop_locker;
566 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
567 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +0000568 name = exe_ctx.GetThreadPtr()->GetQueueName();
569 }
Greg Claytonc9858e42012-04-06 02:17:47 +0000570 else
571 {
572 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000573 log->Printf ("SBThread(%p)::GetQueueName() => error: process is running",
574 static_cast<void*>(exe_ctx.GetThreadPtr()));
Greg Claytonc9858e42012-04-06 02:17:47 +0000575 }
Greg Claytonaf67cec2010-12-20 20:49:23 +0000576 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000577
Caroline Ticeceb6b132010-10-26 03:11:13 +0000578 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000579 log->Printf ("SBThread(%p)::GetQueueName () => %s",
580 static_cast<void*>(exe_ctx.GetThreadPtr()),
581 name ? name : "NULL");
Caroline Ticeceb6b132010-10-26 03:11:13 +0000582
Greg Clayton48381312010-10-30 04:51:46 +0000583 return name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000584}
585
Jason Molenda4fdb5862013-10-21 23:52:54 +0000586lldb::queue_id_t
587SBThread::GetQueueID () const
588{
589 queue_id_t id = LLDB_INVALID_QUEUE_ID;
590 Mutex::Locker api_locker;
591 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
592
593 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
594 if (exe_ctx.HasThreadScope())
595 {
596 Process::StopLocker stop_locker;
597 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
598 {
599 id = exe_ctx.GetThreadPtr()->GetQueueID();
600 }
601 else
602 {
603 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000604 log->Printf ("SBThread(%p)::GetQueueID() => error: process is running",
605 static_cast<void*>(exe_ctx.GetThreadPtr()));
Jason Molenda4fdb5862013-10-21 23:52:54 +0000606 }
607 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000608
Jason Molenda4fdb5862013-10-21 23:52:54 +0000609 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000610 log->Printf ("SBThread(%p)::GetQueueID () => 0x%" PRIx64,
611 static_cast<void*>(exe_ctx.GetThreadPtr()), id);
Jason Molenda4fdb5862013-10-21 23:52:54 +0000612
613 return id;
614}
615
Jason Molenda705b1802014-06-13 02:37:02 +0000616bool
617SBThread::GetInfoItemByPathAsString (const char *path, SBStream &strm)
618{
619 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
620 bool success = false;
621 Mutex::Locker api_locker;
622 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
623
624 if (exe_ctx.HasThreadScope())
625 {
626 Process::StopLocker stop_locker;
627 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
628 {
629 Thread *thread = exe_ctx.GetThreadPtr();
630 StructuredData::ObjectSP info_root_sp = thread->GetExtendedInfo();
631 if (info_root_sp)
632 {
633 StructuredData::ObjectSP node = info_root_sp->GetObjectForDotSeparatedPath (path);
634 if (node)
635 {
636 if (node->GetType() == StructuredData::Type::eTypeString)
637 {
638 strm.Printf ("%s", node->GetAsString()->GetValue().c_str());
639 success = true;
640 }
641 if (node->GetType() == StructuredData::Type::eTypeInteger)
642 {
643 strm.Printf ("0x%" PRIx64, node->GetAsInteger()->GetValue());
644 success = true;
645 }
646 if (node->GetType() == StructuredData::Type::eTypeFloat)
647 {
648 strm.Printf ("0x%f", node->GetAsFloat()->GetValue());
649 success = true;
650 }
651 if (node->GetType() == StructuredData::Type::eTypeBoolean)
652 {
653 if (node->GetAsBoolean()->GetValue() == true)
654 strm.Printf ("true");
655 else
656 strm.Printf ("false");
657 success = true;
658 }
659 if (node->GetType() == StructuredData::Type::eTypeNull)
660 {
661 strm.Printf ("null");
662 success = true;
663 }
664 }
665 }
666 }
667 else
668 {
669 if (log)
670 log->Printf ("SBThread(%p)::GetInfoItemByPathAsString() => error: process is running",
671 static_cast<void*>(exe_ctx.GetThreadPtr()));
672 }
673 }
674
675 if (log)
676 log->Printf ("SBThread(%p)::GetInfoItemByPathAsString () => %s",
677 static_cast<void*>(exe_ctx.GetThreadPtr()),
678 strm.GetData());
679
680 return success;
681}
682
683
Jim Ingham64e7ead2012-05-03 21:19:36 +0000684SBError
685SBThread::ResumeNewPlan (ExecutionContext &exe_ctx, ThreadPlan *new_plan)
686{
687 SBError sb_error;
688
689 Process *process = exe_ctx.GetProcessPtr();
690 if (!process)
691 {
692 sb_error.SetErrorString("No process in SBThread::ResumeNewPlan");
693 return sb_error;
694 }
695
696 Thread *thread = exe_ctx.GetThreadPtr();
697 if (!thread)
698 {
699 sb_error.SetErrorString("No thread in SBThread::ResumeNewPlan");
700 return sb_error;
701 }
702
703 // User level plans should be Master Plans so they can be interrupted, other plans executed, and
704 // then a "continue" will resume the plan.
705 if (new_plan != NULL)
706 {
707 new_plan->SetIsMasterPlan(true);
708 new_plan->SetOkayToDiscard(false);
709 }
710
711 // Why do we need to set the current thread by ID here???
712 process->GetThreadList().SetSelectedThreadByID (thread->GetID());
713 sb_error.ref() = process->Resume();
714
715 if (sb_error.Success())
716 {
717 // If we are doing synchronous mode, then wait for the
718 // process to stop yet again!
719 if (process->GetTarget().GetDebugger().GetAsyncExecution () == false)
720 process->WaitForProcessToStop (NULL);
721 }
722
723 return sb_error;
724}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000725
726void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000727SBThread::StepOver (lldb::RunMode stop_other_threads)
728{
Greg Clayton5160ce52013-03-27 23:08:40 +0000729 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000730
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000731 Mutex::Locker api_locker;
732 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
733
Caroline Ticeceb6b132010-10-26 03:11:13 +0000734
Greg Clayton17a6ad02012-01-30 02:53:15 +0000735 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000736 log->Printf ("SBThread(%p)::StepOver (stop_other_threads='%s')",
737 static_cast<void*>(exe_ctx.GetThreadPtr()),
Greg Clayton17a6ad02012-01-30 02:53:15 +0000738 Thread::RunModeAsCString (stop_other_threads));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000739
Greg Clayton1ac04c32012-02-21 00:09:25 +0000740 if (exe_ctx.HasThreadScope())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000741 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000742 Thread *thread = exe_ctx.GetThreadPtr();
Jim Ingham7ba6e992012-05-11 23:47:32 +0000743 bool abort_other_plans = false;
Jason Molendab57e4a12013-11-04 09:33:30 +0000744 StackFrameSP frame_sp(thread->GetStackFrameAtIndex (0));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000745
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000746 ThreadPlanSP new_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000747 if (frame_sp)
748 {
749 if (frame_sp->HasDebugInformation ())
750 {
Jim Ingham4b4b2472014-03-13 02:47:14 +0000751 const LazyBool avoid_no_debug = eLazyBoolCalculate;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000752 SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000753 new_plan_sp = thread->QueueThreadPlanForStepOverRange (abort_other_plans,
Jim Inghamc6276822012-12-12 19:58:40 +0000754 sc.line_entry.range,
755 sc,
Jim Ingham4b4b2472014-03-13 02:47:14 +0000756 stop_other_threads,
757 avoid_no_debug);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000758 }
759 else
760 {
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000761 new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction (true,
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000762 abort_other_plans,
763 stop_other_threads);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000764 }
765 }
766
Jim Ingham64e7ead2012-05-03 21:19:36 +0000767 // This returns an error, we should use it!
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000768 ResumeNewPlan (exe_ctx, new_plan_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000769 }
770}
771
772void
773SBThread::StepInto (lldb::RunMode stop_other_threads)
774{
Jim Inghamc6276822012-12-12 19:58:40 +0000775 StepInto (NULL, stop_other_threads);
776}
777
778void
779SBThread::StepInto (const char *target_name, lldb::RunMode stop_other_threads)
780{
Greg Clayton5160ce52013-03-27 23:08:40 +0000781 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000782
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000783 Mutex::Locker api_locker;
784 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
Greg Clayton17a6ad02012-01-30 02:53:15 +0000785
786 if (log)
Jim Inghamc6276822012-12-12 19:58:40 +0000787 log->Printf ("SBThread(%p)::StepInto (target_name='%s', stop_other_threads='%s')",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000788 static_cast<void*>(exe_ctx.GetThreadPtr()),
Jim Inghamc6276822012-12-12 19:58:40 +0000789 target_name? target_name: "<NULL>",
Greg Clayton17a6ad02012-01-30 02:53:15 +0000790 Thread::RunModeAsCString (stop_other_threads));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000791
Greg Clayton1ac04c32012-02-21 00:09:25 +0000792 if (exe_ctx.HasThreadScope())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000793 {
Jim Ingham7ba6e992012-05-11 23:47:32 +0000794 bool abort_other_plans = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000795
Greg Clayton1ac04c32012-02-21 00:09:25 +0000796 Thread *thread = exe_ctx.GetThreadPtr();
Jason Molendab57e4a12013-11-04 09:33:30 +0000797 StackFrameSP frame_sp(thread->GetStackFrameAtIndex (0));
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000798 ThreadPlanSP new_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000799
800 if (frame_sp && frame_sp->HasDebugInformation ())
801 {
Jim Ingham4b4b2472014-03-13 02:47:14 +0000802 const LazyBool step_out_avoids_code_without_debug_info = eLazyBoolCalculate;
803 const LazyBool step_in_avoids_code_without_debug_info = eLazyBoolCalculate;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000804 SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000805 new_plan_sp = thread->QueueThreadPlanForStepInRange (abort_other_plans,
Jim Inghamc6276822012-12-12 19:58:40 +0000806 sc.line_entry.range,
807 sc,
808 target_name,
809 stop_other_threads,
Jim Ingham4b4b2472014-03-13 02:47:14 +0000810 step_in_avoids_code_without_debug_info,
811 step_out_avoids_code_without_debug_info);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000812 }
813 else
814 {
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000815 new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction (false,
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000816 abort_other_plans,
817 stop_other_threads);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000818 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000819
Jim Ingham64e7ead2012-05-03 21:19:36 +0000820 // This returns an error, we should use it!
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000821 ResumeNewPlan (exe_ctx, new_plan_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000822 }
823}
824
825void
826SBThread::StepOut ()
827{
Greg Clayton5160ce52013-03-27 23:08:40 +0000828 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000829
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000830 Mutex::Locker api_locker;
831 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
832
Caroline Ticeceb6b132010-10-26 03:11:13 +0000833
Greg Clayton17a6ad02012-01-30 02:53:15 +0000834 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000835 log->Printf ("SBThread(%p)::StepOut ()",
836 static_cast<void*>(exe_ctx.GetThreadPtr()));
837
Greg Clayton1ac04c32012-02-21 00:09:25 +0000838 if (exe_ctx.HasThreadScope())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000839 {
Jim Ingham7ba6e992012-05-11 23:47:32 +0000840 bool abort_other_plans = false;
Jim Ingham94b09242012-09-14 21:07:14 +0000841 bool stop_other_threads = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000842
Greg Clayton1ac04c32012-02-21 00:09:25 +0000843 Thread *thread = exe_ctx.GetThreadPtr();
844
Jim Ingham4b4b2472014-03-13 02:47:14 +0000845 const LazyBool avoid_no_debug = eLazyBoolCalculate;
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000846 ThreadPlanSP new_plan_sp(thread->QueueThreadPlanForStepOut (abort_other_plans,
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000847 NULL,
848 false,
849 stop_other_threads,
850 eVoteYes,
851 eVoteNoOpinion,
852 0,
853 avoid_no_debug));
854
Jim Ingham64e7ead2012-05-03 21:19:36 +0000855 // This returns an error, we should use it!
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000856 ResumeNewPlan (exe_ctx, new_plan_sp.get());
Greg Clayton481cef22011-01-21 06:11:58 +0000857 }
858}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000859
Greg Clayton481cef22011-01-21 06:11:58 +0000860void
861SBThread::StepOutOfFrame (lldb::SBFrame &sb_frame)
862{
Greg Clayton5160ce52013-03-27 23:08:40 +0000863 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton481cef22011-01-21 06:11:58 +0000864
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000865 Mutex::Locker api_locker;
866 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
867
Jason Molendab57e4a12013-11-04 09:33:30 +0000868 StackFrameSP frame_sp (sb_frame.GetFrameSP());
Greg Clayton481cef22011-01-21 06:11:58 +0000869 if (log)
870 {
871 SBStream frame_desc_strm;
872 sb_frame.GetDescription (frame_desc_strm);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000873 log->Printf ("SBThread(%p)::StepOutOfFrame (frame = SBFrame(%p): %s)",
874 static_cast<void*>(exe_ctx.GetThreadPtr()),
875 static_cast<void*>(frame_sp.get()),
876 frame_desc_strm.GetData());
Greg Clayton481cef22011-01-21 06:11:58 +0000877 }
878
Greg Clayton1ac04c32012-02-21 00:09:25 +0000879 if (exe_ctx.HasThreadScope())
Greg Clayton481cef22011-01-21 06:11:58 +0000880 {
Jim Ingham7ba6e992012-05-11 23:47:32 +0000881 bool abort_other_plans = false;
Jim Ingham94b09242012-09-14 21:07:14 +0000882 bool stop_other_threads = false;
Greg Clayton1ac04c32012-02-21 00:09:25 +0000883 Thread *thread = exe_ctx.GetThreadPtr();
Greg Clayton481cef22011-01-21 06:11:58 +0000884
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000885 ThreadPlanSP new_plan_sp(thread->QueueThreadPlanForStepOut (abort_other_plans,
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000886 NULL,
887 false,
888 stop_other_threads,
889 eVoteYes,
Jim Ingham64e7ead2012-05-03 21:19:36 +0000890 eVoteNoOpinion,
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000891 frame_sp->GetFrameIndex()));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000892
Jim Ingham64e7ead2012-05-03 21:19:36 +0000893 // This returns an error, we should use it!
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000894 ResumeNewPlan (exe_ctx, new_plan_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000895 }
896}
897
898void
899SBThread::StepInstruction (bool step_over)
900{
Greg Clayton5160ce52013-03-27 23:08:40 +0000901 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000902
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000903 Mutex::Locker api_locker;
904 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
905
Greg Clayton1ac04c32012-02-21 00:09:25 +0000906
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)::StepInstruction (step_over=%i)",
910 static_cast<void*>(exe_ctx.GetThreadPtr()), step_over);
911
Greg Clayton1ac04c32012-02-21 00:09:25 +0000912 if (exe_ctx.HasThreadScope())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000913 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000914 Thread *thread = exe_ctx.GetThreadPtr();
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000915 ThreadPlanSP new_plan_sp(thread->QueueThreadPlanForStepSingleInstruction (step_over, true, true));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000916
Jim Ingham64e7ead2012-05-03 21:19:36 +0000917 // This returns an error, we should use it!
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000918 ResumeNewPlan (exe_ctx, new_plan_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000919 }
920}
921
922void
923SBThread::RunToAddress (lldb::addr_t addr)
924{
Greg Clayton5160ce52013-03-27 23:08:40 +0000925 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000926
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000927 Mutex::Locker api_locker;
928 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
929
Caroline Ticeceb6b132010-10-26 03:11:13 +0000930
Greg Clayton17a6ad02012-01-30 02:53:15 +0000931 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000932 log->Printf ("SBThread(%p)::RunToAddress (addr=0x%" PRIx64 ")",
933 static_cast<void*>(exe_ctx.GetThreadPtr()), addr);
934
Greg Clayton1ac04c32012-02-21 00:09:25 +0000935 if (exe_ctx.HasThreadScope())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000936 {
Jim Ingham7ba6e992012-05-11 23:47:32 +0000937 bool abort_other_plans = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000938 bool stop_other_threads = true;
939
Greg Claytone72dfb32012-02-24 01:59:29 +0000940 Address target_addr (addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000941
Greg Clayton1ac04c32012-02-21 00:09:25 +0000942 Thread *thread = exe_ctx.GetThreadPtr();
Greg Clayton1ac04c32012-02-21 00:09:25 +0000943
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000944 ThreadPlanSP new_plan_sp(thread->QueueThreadPlanForRunToAddress (abort_other_plans,
945 target_addr,
946 stop_other_threads));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000947
Jim Ingham64e7ead2012-05-03 21:19:36 +0000948 // This returns an error, we should use it!
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000949 ResumeNewPlan (exe_ctx, new_plan_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000950 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000951}
952
Greg Clayton481cef22011-01-21 06:11:58 +0000953SBError
954SBThread::StepOverUntil (lldb::SBFrame &sb_frame,
955 lldb::SBFileSpec &sb_file_spec,
956 uint32_t line)
957{
958 SBError sb_error;
Greg Clayton5160ce52013-03-27 23:08:40 +0000959 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton481cef22011-01-21 06:11:58 +0000960 char path[PATH_MAX];
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000961
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000962 Mutex::Locker api_locker;
963 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
964
Jason Molendab57e4a12013-11-04 09:33:30 +0000965 StackFrameSP frame_sp (sb_frame.GetFrameSP());
Greg Claytonb9556ac2012-01-30 07:41:31 +0000966
Greg Clayton481cef22011-01-21 06:11:58 +0000967 if (log)
968 {
969 SBStream frame_desc_strm;
970 sb_frame.GetDescription (frame_desc_strm);
971 sb_file_spec->GetPath (path, sizeof(path));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000972 log->Printf ("SBThread(%p)::StepOverUntil (frame = SBFrame(%p): %s, file+line = %s:%u)",
973 static_cast<void*>(exe_ctx.GetThreadPtr()),
974 static_cast<void*>(frame_sp.get()),
975 frame_desc_strm.GetData(), path, line);
Greg Clayton481cef22011-01-21 06:11:58 +0000976 }
Greg Clayton17a6ad02012-01-30 02:53:15 +0000977
Greg Clayton1ac04c32012-02-21 00:09:25 +0000978 if (exe_ctx.HasThreadScope())
Greg Clayton481cef22011-01-21 06:11:58 +0000979 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000980 Target *target = exe_ctx.GetTargetPtr();
Greg Clayton1ac04c32012-02-21 00:09:25 +0000981 Thread *thread = exe_ctx.GetThreadPtr();
Greg Clayton481cef22011-01-21 06:11:58 +0000982
983 if (line == 0)
984 {
985 sb_error.SetErrorString("invalid line argument");
986 return sb_error;
987 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000988
Greg Claytonb9556ac2012-01-30 07:41:31 +0000989 if (!frame_sp)
Greg Clayton481cef22011-01-21 06:11:58 +0000990 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000991 frame_sp = thread->GetSelectedFrame ();
Greg Clayton481cef22011-01-21 06:11:58 +0000992 if (!frame_sp)
Greg Clayton1ac04c32012-02-21 00:09:25 +0000993 frame_sp = thread->GetStackFrameAtIndex (0);
Greg Clayton481cef22011-01-21 06:11:58 +0000994 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000995
Greg Clayton481cef22011-01-21 06:11:58 +0000996 SymbolContext frame_sc;
997 if (!frame_sp)
998 {
999 sb_error.SetErrorString("no valid frames in thread to step");
1000 return sb_error;
1001 }
1002
1003 // If we have a frame, get its line
1004 frame_sc = frame_sp->GetSymbolContext (eSymbolContextCompUnit |
1005 eSymbolContextFunction |
1006 eSymbolContextLineEntry |
1007 eSymbolContextSymbol );
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001008
Greg Clayton481cef22011-01-21 06:11:58 +00001009 if (frame_sc.comp_unit == NULL)
1010 {
1011 sb_error.SetErrorStringWithFormat("frame %u doesn't have debug information", frame_sp->GetFrameIndex());
1012 return sb_error;
1013 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001014
Greg Clayton481cef22011-01-21 06:11:58 +00001015 FileSpec step_file_spec;
1016 if (sb_file_spec.IsValid())
1017 {
1018 // The file spec passed in was valid, so use it
1019 step_file_spec = sb_file_spec.ref();
1020 }
1021 else
1022 {
1023 if (frame_sc.line_entry.IsValid())
1024 step_file_spec = frame_sc.line_entry.file;
1025 else
1026 {
1027 sb_error.SetErrorString("invalid file argument or no file for frame");
1028 return sb_error;
1029 }
1030 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001031
Jim Ingham9b70ddb2011-05-08 00:56:32 +00001032 // Grab the current function, then we will make sure the "until" address is
1033 // within the function. We discard addresses that are out of the current
1034 // function, and then if there are no addresses remaining, give an appropriate
1035 // error message.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001036
Jim Ingham9b70ddb2011-05-08 00:56:32 +00001037 bool all_in_function = true;
1038 AddressRange fun_range = frame_sc.function->GetAddressRange();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001039
Greg Clayton481cef22011-01-21 06:11:58 +00001040 std::vector<addr_t> step_over_until_addrs;
Jim Ingham7ba6e992012-05-11 23:47:32 +00001041 const bool abort_other_plans = false;
Jim Inghamc02e3342012-09-14 18:57:14 +00001042 const bool stop_other_threads = false;
Greg Clayton481cef22011-01-21 06:11:58 +00001043 const bool check_inlines = true;
1044 const bool exact = false;
1045
1046 SymbolContextList sc_list;
Jim Ingham9b70ddb2011-05-08 00:56:32 +00001047 const uint32_t num_matches = frame_sc.comp_unit->ResolveSymbolContext (step_file_spec,
1048 line,
1049 check_inlines,
1050 exact,
1051 eSymbolContextLineEntry,
1052 sc_list);
Greg Clayton481cef22011-01-21 06:11:58 +00001053 if (num_matches > 0)
1054 {
1055 SymbolContext sc;
1056 for (uint32_t i=0; i<num_matches; ++i)
1057 {
1058 if (sc_list.GetContextAtIndex(i, sc))
1059 {
Jim Ingham9b70ddb2011-05-08 00:56:32 +00001060 addr_t step_addr = sc.line_entry.range.GetBaseAddress().GetLoadAddress(target);
Greg Clayton481cef22011-01-21 06:11:58 +00001061 if (step_addr != LLDB_INVALID_ADDRESS)
1062 {
Jim Ingham9b70ddb2011-05-08 00:56:32 +00001063 if (fun_range.ContainsLoadAddress(step_addr, target))
1064 step_over_until_addrs.push_back(step_addr);
1065 else
1066 all_in_function = false;
Greg Clayton481cef22011-01-21 06:11:58 +00001067 }
1068 }
1069 }
1070 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001071
Greg Clayton481cef22011-01-21 06:11:58 +00001072 if (step_over_until_addrs.empty())
1073 {
Jim Ingham9b70ddb2011-05-08 00:56:32 +00001074 if (all_in_function)
1075 {
1076 step_file_spec.GetPath (path, sizeof(path));
Jason Molendafd54b362011-09-20 21:44:10 +00001077 sb_error.SetErrorStringWithFormat("No line entries for %s:%u", path, line);
Jim Ingham9b70ddb2011-05-08 00:56:32 +00001078 }
1079 else
Greg Clayton86edbf42011-10-26 00:56:27 +00001080 sb_error.SetErrorString ("step until target not in current function");
Greg Clayton481cef22011-01-21 06:11:58 +00001081 }
1082 else
1083 {
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001084 ThreadPlanSP new_plan_sp(thread->QueueThreadPlanForStepUntil (abort_other_plans,
Jim Ingham64e7ead2012-05-03 21:19:36 +00001085 &step_over_until_addrs[0],
1086 step_over_until_addrs.size(),
1087 stop_other_threads,
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001088 frame_sp->GetFrameIndex()));
Greg Clayton481cef22011-01-21 06:11:58 +00001089
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001090 sb_error = ResumeNewPlan (exe_ctx, new_plan_sp.get());
Greg Clayton481cef22011-01-21 06:11:58 +00001091 }
1092 }
1093 else
1094 {
1095 sb_error.SetErrorString("this SBThread object is invalid");
1096 }
1097 return sb_error;
1098}
1099
Jim Ingham44137582012-09-12 00:40:39 +00001100SBError
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001101SBThread::StepUsingScriptedThreadPlan (const char *script_class_name)
1102{
1103 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1104 SBError sb_error;
1105
1106 Mutex::Locker api_locker;
1107 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1108
1109 if (log)
1110 {
1111 log->Printf ("SBThread(%p)::StepUsingScriptedThreadPlan: class name: %s",
1112 static_cast<void*>(exe_ctx.GetThreadPtr()),
1113 script_class_name);
1114 }
1115
1116
1117 if (!exe_ctx.HasThreadScope())
1118 {
1119 sb_error.SetErrorString("this SBThread object is invalid");
1120 return sb_error;
1121 }
1122
1123 Thread *thread = exe_ctx.GetThreadPtr();
1124 ThreadPlanSP thread_plan_sp = thread->QueueThreadPlanForStepScripted(false, script_class_name, false);
1125
1126 if (thread_plan_sp)
1127 sb_error = ResumeNewPlan(exe_ctx, thread_plan_sp.get());
1128 else
1129 {
1130 sb_error.SetErrorStringWithFormat("Error queuing thread plan for class: %s.", script_class_name);
1131 if (log)
1132 log->Printf ("SBThread(%p)::StepUsingScriptedThreadPlan: Error queuing thread plan for class: %s",
1133 static_cast<void*>(exe_ctx.GetThreadPtr()),
1134 script_class_name);
1135 }
1136
1137 return sb_error;
1138}
1139
1140SBError
Richard Mittonf86248d2013-09-12 02:20:34 +00001141SBThread::JumpToLine (lldb::SBFileSpec &file_spec, uint32_t line)
1142{
1143 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1144 SBError sb_error;
1145
1146 Mutex::Locker api_locker;
1147 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1148
1149 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001150 log->Printf ("SBThread(%p)::JumpToLine (file+line = %s:%u)",
1151 static_cast<void*>(exe_ctx.GetThreadPtr()),
1152 file_spec->GetPath().c_str(), line);
Richard Mittonf86248d2013-09-12 02:20:34 +00001153
1154 if (!exe_ctx.HasThreadScope())
1155 {
1156 sb_error.SetErrorString("this SBThread object is invalid");
1157 return sb_error;
1158 }
1159
1160 Thread *thread = exe_ctx.GetThreadPtr();
1161
1162 Error err = thread->JumpToLine (file_spec.get(), line, true);
1163 sb_error.SetError (err);
1164 return sb_error;
1165}
1166
1167SBError
Jim Inghamcb640dd2012-09-14 02:14:15 +00001168SBThread::ReturnFromFrame (SBFrame &frame, SBValue &return_value)
Jim Ingham44137582012-09-12 00:40:39 +00001169{
1170 SBError sb_error;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001171
Greg Clayton5160ce52013-03-27 23:08:40 +00001172 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Ingham44137582012-09-12 00:40:39 +00001173
1174 Mutex::Locker api_locker;
1175 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1176
1177
1178 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001179 log->Printf ("SBThread(%p)::ReturnFromFrame (frame=%d)",
1180 static_cast<void*>(exe_ctx.GetThreadPtr()),
1181 frame.GetFrameID());
1182
Jim Ingham44137582012-09-12 00:40:39 +00001183 if (exe_ctx.HasThreadScope())
1184 {
1185 Thread *thread = exe_ctx.GetThreadPtr();
Jim Inghamcb640dd2012-09-14 02:14:15 +00001186 sb_error.SetError (thread->ReturnFromFrame(frame.GetFrameSP(), return_value.GetSP()));
Jim Ingham44137582012-09-12 00:40:39 +00001187 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001188
Jim Ingham44137582012-09-12 00:40:39 +00001189 return sb_error;
1190}
1191
Greg Clayton481cef22011-01-21 06:11:58 +00001192
Greg Clayton722a0cd2011-01-12 02:25:42 +00001193bool
1194SBThread::Suspend()
1195{
Greg Clayton5160ce52013-03-27 23:08:40 +00001196 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001197 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Claytonc9858e42012-04-06 02:17:47 +00001198 bool result = false;
Greg Clayton1ac04c32012-02-21 00:09:25 +00001199 if (exe_ctx.HasThreadScope())
Greg Clayton722a0cd2011-01-12 02:25:42 +00001200 {
Greg Claytonc9858e42012-04-06 02:17:47 +00001201 Process::StopLocker stop_locker;
1202 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1203 {
1204 exe_ctx.GetThreadPtr()->SetResumeState (eStateSuspended);
1205 result = true;
1206 }
1207 else
1208 {
1209 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001210 log->Printf ("SBThread(%p)::Suspend() => error: process is running",
1211 static_cast<void*>(exe_ctx.GetThreadPtr()));
Greg Claytonc9858e42012-04-06 02:17:47 +00001212 }
Greg Clayton722a0cd2011-01-12 02:25:42 +00001213 }
Greg Claytonc9858e42012-04-06 02:17:47 +00001214 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001215 log->Printf ("SBThread(%p)::Suspend() => %i",
1216 static_cast<void*>(exe_ctx.GetThreadPtr()), result);
Greg Claytonc9858e42012-04-06 02:17:47 +00001217 return result;
Greg Clayton722a0cd2011-01-12 02:25:42 +00001218}
1219
1220bool
1221SBThread::Resume ()
1222{
Greg Clayton5160ce52013-03-27 23:08:40 +00001223 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001224 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Claytonc9858e42012-04-06 02:17:47 +00001225 bool result = false;
Greg Clayton1ac04c32012-02-21 00:09:25 +00001226 if (exe_ctx.HasThreadScope())
Greg Clayton722a0cd2011-01-12 02:25:42 +00001227 {
Greg Claytonc9858e42012-04-06 02:17:47 +00001228 Process::StopLocker stop_locker;
1229 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1230 {
Jim Ingham6c9ed912014-04-03 01:26:14 +00001231 const bool override_suspend = true;
1232 exe_ctx.GetThreadPtr()->SetResumeState (eStateRunning, override_suspend);
Greg Claytonc9858e42012-04-06 02:17:47 +00001233 result = true;
1234 }
1235 else
1236 {
1237 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001238 log->Printf ("SBThread(%p)::Resume() => error: process is running",
1239 static_cast<void*>(exe_ctx.GetThreadPtr()));
Greg Claytonc9858e42012-04-06 02:17:47 +00001240 }
Greg Clayton722a0cd2011-01-12 02:25:42 +00001241 }
Greg Claytonc9858e42012-04-06 02:17:47 +00001242 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001243 log->Printf ("SBThread(%p)::Resume() => %i",
1244 static_cast<void*>(exe_ctx.GetThreadPtr()), result);
Greg Claytonc9858e42012-04-06 02:17:47 +00001245 return result;
Greg Clayton722a0cd2011-01-12 02:25:42 +00001246}
1247
1248bool
1249SBThread::IsSuspended()
1250{
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001251 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Clayton1ac04c32012-02-21 00:09:25 +00001252 if (exe_ctx.HasThreadScope())
1253 return exe_ctx.GetThreadPtr()->GetResumeState () == eStateSuspended;
Greg Clayton722a0cd2011-01-12 02:25:42 +00001254 return false;
1255}
1256
Andrew Kaylora75418d2013-04-15 23:33:53 +00001257bool
1258SBThread::IsStopped()
1259{
1260 ExecutionContext exe_ctx (m_opaque_sp.get());
1261 if (exe_ctx.HasThreadScope())
1262 return StateIsStoppedState(exe_ctx.GetThreadPtr()->GetState(), true);
1263 return false;
1264}
1265
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001266SBProcess
1267SBThread::GetProcess ()
1268{
Greg Claytonb9556ac2012-01-30 07:41:31 +00001269 SBProcess sb_process;
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001270 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Clayton1ac04c32012-02-21 00:09:25 +00001271 if (exe_ctx.HasThreadScope())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001272 {
1273 // Have to go up to the target so we can get a shared pointer to our process...
Greg Clayton1ac04c32012-02-21 00:09:25 +00001274 sb_process.SetSP (exe_ctx.GetProcessSP());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001275 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001276
Greg Clayton5160ce52013-03-27 23:08:40 +00001277 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001278 if (log)
1279 {
Greg Clayton481cef22011-01-21 06:11:58 +00001280 SBStream frame_desc_strm;
Greg Claytonb9556ac2012-01-30 07:41:31 +00001281 sb_process.GetDescription (frame_desc_strm);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001282 log->Printf ("SBThread(%p)::GetProcess () => SBProcess(%p): %s",
1283 static_cast<void*>(exe_ctx.GetThreadPtr()),
1284 static_cast<void*>(sb_process.GetSP().get()),
1285 frame_desc_strm.GetData());
Caroline Ticeceb6b132010-10-26 03:11:13 +00001286 }
1287
Greg Claytonb9556ac2012-01-30 07:41:31 +00001288 return sb_process;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001289}
1290
1291uint32_t
1292SBThread::GetNumFrames ()
1293{
Greg Clayton5160ce52013-03-27 23:08:40 +00001294 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001295
Caroline Ticeceb6b132010-10-26 03:11:13 +00001296 uint32_t num_frames = 0;
Jim Ingham4fc6cb92012-08-22 21:34:33 +00001297 Mutex::Locker api_locker;
1298 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1299
Greg Clayton1ac04c32012-02-21 00:09:25 +00001300 if (exe_ctx.HasThreadScope())
Greg Claytonaf67cec2010-12-20 20:49:23 +00001301 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001302 Process::StopLocker stop_locker;
1303 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1304 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001305 num_frames = exe_ctx.GetThreadPtr()->GetStackFrameCount();
1306 }
Greg Claytonc9858e42012-04-06 02:17:47 +00001307 else
1308 {
1309 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001310 log->Printf ("SBThread(%p)::GetNumFrames() => error: process is running",
1311 static_cast<void*>(exe_ctx.GetThreadPtr()));
Greg Claytonc9858e42012-04-06 02:17:47 +00001312 }
Greg Claytonaf67cec2010-12-20 20:49:23 +00001313 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001314
1315 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001316 log->Printf ("SBThread(%p)::GetNumFrames () => %u",
1317 static_cast<void*>(exe_ctx.GetThreadPtr()), num_frames);
Caroline Ticeceb6b132010-10-26 03:11:13 +00001318
1319 return num_frames;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001320}
1321
1322SBFrame
1323SBThread::GetFrameAtIndex (uint32_t idx)
1324{
Greg Clayton5160ce52013-03-27 23:08:40 +00001325 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001326
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001327 SBFrame sb_frame;
Jason Molendab57e4a12013-11-04 09:33:30 +00001328 StackFrameSP frame_sp;
Jim Ingham4fc6cb92012-08-22 21:34:33 +00001329 Mutex::Locker api_locker;
1330 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1331
Greg Clayton1ac04c32012-02-21 00:09:25 +00001332 if (exe_ctx.HasThreadScope())
Greg Claytonaf67cec2010-12-20 20:49:23 +00001333 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001334 Process::StopLocker stop_locker;
1335 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1336 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001337 frame_sp = exe_ctx.GetThreadPtr()->GetStackFrameAtIndex (idx);
1338 sb_frame.SetFrameSP (frame_sp);
1339 }
Greg Claytonc9858e42012-04-06 02:17:47 +00001340 else
1341 {
1342 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001343 log->Printf ("SBThread(%p)::GetFrameAtIndex() => error: process is running",
1344 static_cast<void*>(exe_ctx.GetThreadPtr()));
Greg Claytonc9858e42012-04-06 02:17:47 +00001345 }
Greg Claytonaf67cec2010-12-20 20:49:23 +00001346 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001347
1348 if (log)
1349 {
Greg Clayton481cef22011-01-21 06:11:58 +00001350 SBStream frame_desc_strm;
1351 sb_frame.GetDescription (frame_desc_strm);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001352 log->Printf ("SBThread(%p)::GetFrameAtIndex (idx=%d) => SBFrame(%p): %s",
1353 static_cast<void*>(exe_ctx.GetThreadPtr()), idx,
1354 static_cast<void*>(frame_sp.get()),
1355 frame_desc_strm.GetData());
Caroline Ticeceb6b132010-10-26 03:11:13 +00001356 }
1357
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001358 return sb_frame;
1359}
1360
Greg Claytonf028a1f2010-12-17 02:26:24 +00001361lldb::SBFrame
1362SBThread::GetSelectedFrame ()
1363{
Greg Clayton5160ce52013-03-27 23:08:40 +00001364 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytonf028a1f2010-12-17 02:26:24 +00001365
1366 SBFrame sb_frame;
Jason Molendab57e4a12013-11-04 09:33:30 +00001367 StackFrameSP frame_sp;
Jim Ingham4fc6cb92012-08-22 21:34:33 +00001368 Mutex::Locker api_locker;
1369 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1370
Greg Clayton1ac04c32012-02-21 00:09:25 +00001371 if (exe_ctx.HasThreadScope())
Greg Claytonaf67cec2010-12-20 20:49:23 +00001372 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001373 Process::StopLocker stop_locker;
1374 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1375 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001376 frame_sp = exe_ctx.GetThreadPtr()->GetSelectedFrame ();
1377 sb_frame.SetFrameSP (frame_sp);
1378 }
Greg Claytonc9858e42012-04-06 02:17:47 +00001379 else
1380 {
1381 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001382 log->Printf ("SBThread(%p)::GetSelectedFrame() => error: process is running",
1383 static_cast<void*>(exe_ctx.GetThreadPtr()));
Greg Claytonc9858e42012-04-06 02:17:47 +00001384 }
Greg Claytonaf67cec2010-12-20 20:49:23 +00001385 }
Greg Claytonf028a1f2010-12-17 02:26:24 +00001386
1387 if (log)
1388 {
Greg Clayton481cef22011-01-21 06:11:58 +00001389 SBStream frame_desc_strm;
1390 sb_frame.GetDescription (frame_desc_strm);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001391 log->Printf ("SBThread(%p)::GetSelectedFrame () => SBFrame(%p): %s",
1392 static_cast<void*>(exe_ctx.GetThreadPtr()),
1393 static_cast<void*>(frame_sp.get()),
1394 frame_desc_strm.GetData());
Greg Claytonf028a1f2010-12-17 02:26:24 +00001395 }
1396
1397 return sb_frame;
1398}
1399
1400lldb::SBFrame
1401SBThread::SetSelectedFrame (uint32_t idx)
1402{
Greg Clayton5160ce52013-03-27 23:08:40 +00001403 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytonf028a1f2010-12-17 02:26:24 +00001404
1405 SBFrame sb_frame;
Jason Molendab57e4a12013-11-04 09:33:30 +00001406 StackFrameSP frame_sp;
Jim Ingham4fc6cb92012-08-22 21:34:33 +00001407 Mutex::Locker api_locker;
1408 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1409
Greg Clayton1ac04c32012-02-21 00:09:25 +00001410 if (exe_ctx.HasThreadScope())
Greg Claytonf028a1f2010-12-17 02:26:24 +00001411 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001412 Process::StopLocker stop_locker;
1413 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
Greg Claytonf028a1f2010-12-17 02:26:24 +00001414 {
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001415 Thread *thread = exe_ctx.GetThreadPtr();
1416 frame_sp = thread->GetStackFrameAtIndex (idx);
1417 if (frame_sp)
1418 {
1419 thread->SetSelectedFrame (frame_sp.get());
1420 sb_frame.SetFrameSP (frame_sp);
1421 }
Greg Claytonf028a1f2010-12-17 02:26:24 +00001422 }
Greg Claytonc9858e42012-04-06 02:17:47 +00001423 else
1424 {
1425 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001426 log->Printf ("SBThread(%p)::SetSelectedFrame() => error: process is running",
1427 static_cast<void*>(exe_ctx.GetThreadPtr()));
Greg Claytonc9858e42012-04-06 02:17:47 +00001428 }
Greg Claytonf028a1f2010-12-17 02:26:24 +00001429 }
1430
1431 if (log)
1432 {
Greg Clayton481cef22011-01-21 06:11:58 +00001433 SBStream frame_desc_strm;
1434 sb_frame.GetDescription (frame_desc_strm);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001435 log->Printf ("SBThread(%p)::SetSelectedFrame (idx=%u) => SBFrame(%p): %s",
1436 static_cast<void*>(exe_ctx.GetThreadPtr()), idx,
1437 static_cast<void*>(frame_sp.get()),
1438 frame_desc_strm.GetData());
Greg Claytonf028a1f2010-12-17 02:26:24 +00001439 }
1440 return sb_frame;
1441}
1442
Jim Ingham4f465cf2012-10-10 18:32:14 +00001443bool
1444SBThread::EventIsThreadEvent (const SBEvent &event)
1445{
1446 return Thread::ThreadEventData::GetEventDataFromEvent(event.get()) != NULL;
1447}
1448
1449SBFrame
1450SBThread::GetStackFrameFromEvent (const SBEvent &event)
1451{
1452 return Thread::ThreadEventData::GetStackFrameFromEvent (event.get());
1453
1454}
1455
1456SBThread
1457SBThread::GetThreadFromEvent (const SBEvent &event)
1458{
1459 return Thread::ThreadEventData::GetThreadFromEvent (event.get());
1460}
Greg Claytonf028a1f2010-12-17 02:26:24 +00001461
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001462bool
1463SBThread::operator == (const SBThread &rhs) const
1464{
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001465 return m_opaque_sp->GetThreadSP().get() == rhs.m_opaque_sp->GetThreadSP().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001466}
1467
1468bool
1469SBThread::operator != (const SBThread &rhs) const
1470{
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001471 return m_opaque_sp->GetThreadSP().get() != rhs.m_opaque_sp->GetThreadSP().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001472}
Caroline Ticedde9cff2010-09-20 05:20:02 +00001473
1474bool
Jim Ingham4f465cf2012-10-10 18:32:14 +00001475SBThread::GetStatus (SBStream &status) const
1476{
1477 Stream &strm = status.ref();
1478
1479 ExecutionContext exe_ctx (m_opaque_sp.get());
1480 if (exe_ctx.HasThreadScope())
1481 {
1482 exe_ctx.GetThreadPtr()->GetStatus(strm, 0, 1, 1);
1483 }
1484 else
1485 strm.PutCString ("No status");
1486
1487 return true;
1488}
1489
1490bool
Caroline Ticeceb6b132010-10-26 03:11:13 +00001491SBThread::GetDescription (SBStream &description) const
1492{
Greg Claytonda7bc7d2011-11-13 06:57:31 +00001493 Stream &strm = description.ref();
1494
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001495 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Clayton1ac04c32012-02-21 00:09:25 +00001496 if (exe_ctx.HasThreadScope())
Caroline Ticeceb6b132010-10-26 03:11:13 +00001497 {
Daniel Malead01b2952012-11-29 21:49:15 +00001498 strm.Printf("SBThread: tid = 0x%4.4" PRIx64, exe_ctx.GetThreadPtr()->GetID());
Caroline Ticeceb6b132010-10-26 03:11:13 +00001499 }
1500 else
Greg Claytonda7bc7d2011-11-13 06:57:31 +00001501 strm.PutCString ("No value");
Caroline Ticeceb6b132010-10-26 03:11:13 +00001502
1503 return true;
1504}
Jason Molenda5dd49162013-11-06 00:04:44 +00001505
1506SBThread
Jason Molenda008c45f2013-11-12 23:33:32 +00001507SBThread::GetExtendedBacktraceThread (const char *type)
Jason Molenda5dd49162013-11-06 00:04:44 +00001508{
1509 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1510 Mutex::Locker api_locker;
1511 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1512 SBThread sb_origin_thread;
1513
1514 if (exe_ctx.HasThreadScope())
1515 {
1516 Process::StopLocker stop_locker;
1517 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1518 {
Jason Molenda7a2f7902013-11-11 05:19:34 +00001519 ThreadSP real_thread(exe_ctx.GetThreadSP());
Jason Molenda5dd49162013-11-06 00:04:44 +00001520 if (real_thread)
1521 {
1522 ConstString type_const (type);
Jason Molenda7a2f7902013-11-11 05:19:34 +00001523 Process *process = exe_ctx.GetProcessPtr();
1524 if (process)
Jason Molenda5dd49162013-11-06 00:04:44 +00001525 {
Jason Molenda7a2f7902013-11-11 05:19:34 +00001526 SystemRuntime *runtime = process->GetSystemRuntime();
1527 if (runtime)
1528 {
Jason Molenda008c45f2013-11-12 23:33:32 +00001529 ThreadSP new_thread_sp (runtime->GetExtendedBacktraceThread (real_thread, type_const));
Jason Molendaa6e91302013-11-19 05:44:41 +00001530 if (new_thread_sp)
1531 {
1532 // Save this in the Process' ExtendedThreadList so a strong pointer retains the
1533 // object.
1534 process->GetExtendedThreadList().AddThread (new_thread_sp);
1535 sb_origin_thread.SetThread (new_thread_sp);
1536 if (log)
1537 {
1538 const char *queue_name = new_thread_sp->GetQueueName();
1539 if (queue_name == NULL)
1540 queue_name = "";
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001541 log->Printf ("SBThread(%p)::GetExtendedBacktraceThread() => new extended Thread "
1542 "created (%p) with queue_id 0x%" PRIx64 " queue name '%s'",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001543 static_cast<void*>(exe_ctx.GetThreadPtr()),
1544 static_cast<void*>(new_thread_sp.get()),
1545 new_thread_sp->GetQueueID(),
1546 queue_name);
Jason Molendaa6e91302013-11-19 05:44:41 +00001547 }
1548 }
Jason Molenda7a2f7902013-11-11 05:19:34 +00001549 }
Jason Molenda5dd49162013-11-06 00:04:44 +00001550 }
1551 }
1552 }
1553 else
1554 {
1555 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001556 log->Printf ("SBThread(%p)::GetExtendedBacktraceThread() => error: process is running",
1557 static_cast<void*>(exe_ctx.GetThreadPtr()));
Jason Molenda5dd49162013-11-06 00:04:44 +00001558 }
1559 }
1560
Jason Molendaac605f42014-03-08 01:34:55 +00001561 if (log && sb_origin_thread.IsValid() == false)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001562 log->Printf("SBThread(%p)::GetExtendedBacktraceThread() is not returning a Valid thread",
1563 static_cast<void*>(exe_ctx.GetThreadPtr()));
Jason Molenda5dd49162013-11-06 00:04:44 +00001564 return sb_origin_thread;
1565}
Jason Molenda8ee9cb52013-11-16 01:24:22 +00001566
1567uint32_t
1568SBThread::GetExtendedBacktraceOriginatingIndexID ()
1569{
1570 ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
1571 if (thread_sp)
1572 return thread_sp->GetExtendedBacktraceOriginatingIndexID();
1573 return LLDB_INVALID_INDEX32;
1574}
Jason Molendab4892cd2014-05-13 22:02:48 +00001575
1576bool
1577SBThread::SafeToCallFunctions ()
1578{
1579 ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
1580 if (thread_sp)
1581 return thread_sp->SafeToCallFunctions();
1582 return true;
1583}
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001584
1585lldb_private::Thread *
1586SBThread::operator->()
1587{
1588 ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
1589 if (thread_sp)
1590 return thread_sp.get();
1591 else
1592 return NULL;
1593}
1594
1595lldb_private::Thread *
1596SBThread::get()
1597{
1598 ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
1599 if (thread_sp)
1600 return thread_sp.get();
1601 else
1602 return NULL;
1603}
1604