blob: e083939e333f0fd883c863bb11a5afbe78f54462 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- SBThread.cpp --------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Eli Friedman7a62c8b2010-06-09 07:44:37 +000010#include "lldb/API/SBThread.h"
Chris Lattner24943d22010-06-08 16:52:24 +000011
12#include "lldb/API/SBSymbolContext.h"
13#include "lldb/API/SBFileSpec.h"
Caroline Tice98f930f2010-09-20 05:20:02 +000014#include "lldb/API/SBStream.h"
Greg Clayton640dc6b2010-11-18 18:52:36 +000015#include "lldb/Breakpoint/BreakpointLocation.h"
Greg Clayton63094e02010-06-23 01:19:29 +000016#include "lldb/Core/Debugger.h"
Chris Lattner24943d22010-06-08 16:52:24 +000017#include "lldb/Core/Stream.h"
18#include "lldb/Core/StreamFile.h"
Greg Clayton63094e02010-06-23 01:19:29 +000019#include "lldb/Interpreter/CommandInterpreter.h"
Chris Lattner24943d22010-06-08 16:52:24 +000020#include "lldb/Target/Thread.h"
21#include "lldb/Target/Process.h"
22#include "lldb/Symbol/SymbolContext.h"
23#include "lldb/Symbol/CompileUnit.h"
Greg Clayton643ee732010-08-04 01:40:35 +000024#include "lldb/Target/StopInfo.h"
Chris Lattner24943d22010-06-08 16:52:24 +000025#include "lldb/Target/Target.h"
26#include "lldb/Target/ThreadPlan.h"
Chris Lattner24943d22010-06-08 16:52:24 +000027#include "lldb/Target/ThreadPlanStepInstruction.h"
28#include "lldb/Target/ThreadPlanStepOut.h"
29#include "lldb/Target/ThreadPlanStepRange.h"
30#include "lldb/Target/ThreadPlanStepInRange.h"
31
32
Eli Friedman7a62c8b2010-06-09 07:44:37 +000033#include "lldb/API/SBAddress.h"
Eli Friedman7a62c8b2010-06-09 07:44:37 +000034#include "lldb/API/SBDebugger.h"
Jim Ingham94a5d0d2012-10-10 18:32:14 +000035#include "lldb/API/SBEvent.h"
Jim Ingham1586d972011-12-17 01:35:57 +000036#include "lldb/API/SBFrame.h"
Eli Friedman7a62c8b2010-06-09 07:44:37 +000037#include "lldb/API/SBProcess.h"
Jim Ingham1586d972011-12-17 01:35:57 +000038#include "lldb/API/SBValue.h"
Chris Lattner24943d22010-06-08 16:52:24 +000039
40using namespace lldb;
41using namespace lldb_private;
42
Jim Ingham94a5d0d2012-10-10 18:32:14 +000043const char *
44SBThread::GetBroadcasterClassName ()
45{
46 return Thread::GetStaticBroadcasterClass().AsCString();
47}
48
Greg Clayton49ce6822010-10-31 03:01:06 +000049//----------------------------------------------------------------------
50// Constructors
51//----------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +000052SBThread::SBThread () :
Greg Claytona894fe72012-04-05 16:12:35 +000053 m_opaque_sp (new ExecutionContextRef())
Chris Lattner24943d22010-06-08 16:52:24 +000054{
55}
56
Chris Lattner24943d22010-06-08 16:52:24 +000057SBThread::SBThread (const ThreadSP& lldb_object_sp) :
Greg Claytona894fe72012-04-05 16:12:35 +000058 m_opaque_sp (new ExecutionContextRef(lldb_object_sp))
Chris Lattner24943d22010-06-08 16:52:24 +000059{
60}
61
Greg Clayton1b284412010-10-30 18:26:59 +000062SBThread::SBThread (const SBThread &rhs) :
Greg Claytona894fe72012-04-05 16:12:35 +000063 m_opaque_sp (new ExecutionContextRef(*rhs.m_opaque_sp))
Chris Lattner24943d22010-06-08 16:52:24 +000064{
Greg Claytona894fe72012-04-05 16:12:35 +000065
Chris Lattner24943d22010-06-08 16:52:24 +000066}
67
68//----------------------------------------------------------------------
Greg Clayton49ce6822010-10-31 03:01:06 +000069// Assignment operator
70//----------------------------------------------------------------------
71
72const lldb::SBThread &
73SBThread::operator = (const SBThread &rhs)
74{
75 if (this != &rhs)
Greg Claytona894fe72012-04-05 16:12:35 +000076 *m_opaque_sp = *rhs.m_opaque_sp;
Greg Clayton49ce6822010-10-31 03:01:06 +000077 return *this;
78}
79
80//----------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +000081// Destructor
82//----------------------------------------------------------------------
83SBThread::~SBThread()
84{
85}
86
87bool
88SBThread::IsValid() const
89{
Greg Claytona894fe72012-04-05 16:12:35 +000090 return m_opaque_sp->GetThreadSP().get() != NULL;
Chris Lattner24943d22010-06-08 16:52:24 +000091}
92
Greg Clayton43490d12010-07-30 20:12:55 +000093void
94SBThread::Clear ()
95{
Greg Claytona894fe72012-04-05 16:12:35 +000096 m_opaque_sp->Clear();
Greg Clayton43490d12010-07-30 20:12:55 +000097}
98
99
Chris Lattner24943d22010-06-08 16:52:24 +0000100StopReason
101SBThread::GetStopReason()
102{
Greg Claytone005f2c2010-11-06 01:53:30 +0000103 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000104
Caroline Tice7826c882010-10-26 03:11:13 +0000105 StopReason reason = eStopReasonInvalid;
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000106 Mutex::Locker api_locker;
107 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
108
Greg Claytonf4124de2012-02-21 00:09:25 +0000109 if (exe_ctx.HasThreadScope())
Chris Lattner24943d22010-06-08 16:52:24 +0000110 {
Greg Claytona894fe72012-04-05 16:12:35 +0000111 Process::StopLocker stop_locker;
112 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
113 {
Greg Clayton3acaa922012-09-25 02:40:06 +0000114 return exe_ctx.GetThreadPtr()->GetStopReason();
Greg Claytona894fe72012-04-05 16:12:35 +0000115 }
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000116 else
117 {
118 if (log)
119 log->Printf ("SBThread(%p)::GetStopReason() => error: process is running", exe_ctx.GetThreadPtr());
120 }
Chris Lattner24943d22010-06-08 16:52:24 +0000121 }
Caroline Tice7826c882010-10-26 03:11:13 +0000122
123 if (log)
Greg Claytonf4124de2012-02-21 00:09:25 +0000124 log->Printf ("SBThread(%p)::GetStopReason () => %s", exe_ctx.GetThreadPtr(),
Caroline Tice61ba7ec2010-10-26 23:49:36 +0000125 Thread::StopReasonAsCString (reason));
Caroline Tice7826c882010-10-26 03:11:13 +0000126
127 return reason;
Chris Lattner24943d22010-06-08 16:52:24 +0000128}
129
130size_t
Greg Clayton640dc6b2010-11-18 18:52:36 +0000131SBThread::GetStopReasonDataCount ()
132{
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000133 Mutex::Locker api_locker;
134 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
135
Greg Claytonf4124de2012-02-21 00:09:25 +0000136 if (exe_ctx.HasThreadScope())
Greg Clayton640dc6b2010-11-18 18:52:36 +0000137 {
Greg Claytona894fe72012-04-05 16:12:35 +0000138 Process::StopLocker stop_locker;
139 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
Greg Clayton640dc6b2010-11-18 18:52:36 +0000140 {
Greg Claytona894fe72012-04-05 16:12:35 +0000141 StopInfoSP stop_info_sp = exe_ctx.GetThreadPtr()->GetStopInfo ();
142 if (stop_info_sp)
Greg Clayton640dc6b2010-11-18 18:52:36 +0000143 {
Greg Claytona894fe72012-04-05 16:12:35 +0000144 StopReason reason = stop_info_sp->GetStopReason();
145 switch (reason)
Greg Clayton640dc6b2010-11-18 18:52:36 +0000146 {
Greg Claytona894fe72012-04-05 16:12:35 +0000147 case eStopReasonInvalid:
148 case eStopReasonNone:
149 case eStopReasonTrace:
Greg Clayton0bce9a22012-12-05 00:16:59 +0000150 case eStopReasonExec:
Greg Claytona894fe72012-04-05 16:12:35 +0000151 case eStopReasonPlanComplete:
152 // There is no data for these stop reasons.
153 return 0;
154
155 case eStopReasonBreakpoint:
156 {
157 break_id_t site_id = stop_info_sp->GetValue();
158 lldb::BreakpointSiteSP bp_site_sp (exe_ctx.GetProcessPtr()->GetBreakpointSiteList().FindByID (site_id));
159 if (bp_site_sp)
160 return bp_site_sp->GetNumberOfOwners () * 2;
161 else
162 return 0; // Breakpoint must have cleared itself...
163 }
164 break;
165
166 case eStopReasonWatchpoint:
167 return 1;
168
169 case eStopReasonSignal:
170 return 1;
171
172 case eStopReasonException:
173 return 1;
Greg Clayton640dc6b2010-11-18 18:52:36 +0000174 }
Greg Clayton640dc6b2010-11-18 18:52:36 +0000175 }
176 }
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000177 else
178 {
179 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
180 if (log)
181 log->Printf ("SBThread(%p)::GetStopReasonDataCount() => error: process is running", exe_ctx.GetThreadPtr());
182 }
Greg Clayton640dc6b2010-11-18 18:52:36 +0000183 }
184 return 0;
185}
186
187uint64_t
188SBThread::GetStopReasonDataAtIndex (uint32_t idx)
189{
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000190 Mutex::Locker api_locker;
191 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
192
Greg Claytonf4124de2012-02-21 00:09:25 +0000193 if (exe_ctx.HasThreadScope())
Greg Clayton640dc6b2010-11-18 18:52:36 +0000194 {
Greg Claytona894fe72012-04-05 16:12:35 +0000195 Process::StopLocker stop_locker;
196 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
Greg Clayton640dc6b2010-11-18 18:52:36 +0000197 {
Greg Claytona894fe72012-04-05 16:12:35 +0000198 Thread *thread = exe_ctx.GetThreadPtr();
199 StopInfoSP stop_info_sp = thread->GetStopInfo ();
200 if (stop_info_sp)
201 {
202 StopReason reason = stop_info_sp->GetStopReason();
203 switch (reason)
Greg Clayton640dc6b2010-11-18 18:52:36 +0000204 {
Greg Claytona894fe72012-04-05 16:12:35 +0000205 case eStopReasonInvalid:
206 case eStopReasonNone:
207 case eStopReasonTrace:
Greg Clayton0bce9a22012-12-05 00:16:59 +0000208 case eStopReasonExec:
Greg Claytona894fe72012-04-05 16:12:35 +0000209 case eStopReasonPlanComplete:
210 // There is no data for these stop reasons.
211 return 0;
212
213 case eStopReasonBreakpoint:
Greg Clayton640dc6b2010-11-18 18:52:36 +0000214 {
Greg Claytona894fe72012-04-05 16:12:35 +0000215 break_id_t site_id = stop_info_sp->GetValue();
216 lldb::BreakpointSiteSP bp_site_sp (exe_ctx.GetProcessPtr()->GetBreakpointSiteList().FindByID (site_id));
217 if (bp_site_sp)
Greg Clayton640dc6b2010-11-18 18:52:36 +0000218 {
Greg Claytona894fe72012-04-05 16:12:35 +0000219 uint32_t bp_index = idx / 2;
220 BreakpointLocationSP bp_loc_sp (bp_site_sp->GetOwnerAtIndex (bp_index));
221 if (bp_loc_sp)
Greg Clayton640dc6b2010-11-18 18:52:36 +0000222 {
Greg Claytona894fe72012-04-05 16:12:35 +0000223 if (bp_index & 1)
224 {
225 // Odd idx, return the breakpoint location ID
226 return bp_loc_sp->GetID();
227 }
228 else
229 {
230 // Even idx, return the breakpoint ID
231 return bp_loc_sp->GetBreakpoint().GetID();
232 }
Greg Clayton640dc6b2010-11-18 18:52:36 +0000233 }
234 }
Greg Claytona894fe72012-04-05 16:12:35 +0000235 return LLDB_INVALID_BREAK_ID;
Greg Clayton640dc6b2010-11-18 18:52:36 +0000236 }
Greg Claytona894fe72012-04-05 16:12:35 +0000237 break;
238
239 case eStopReasonWatchpoint:
240 return stop_info_sp->GetValue();
241
242 case eStopReasonSignal:
243 return stop_info_sp->GetValue();
244
245 case eStopReasonException:
246 return stop_info_sp->GetValue();
Greg Clayton640dc6b2010-11-18 18:52:36 +0000247 }
Greg Clayton640dc6b2010-11-18 18:52:36 +0000248 }
249 }
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000250 else
251 {
252 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
253 if (log)
254 log->Printf ("SBThread(%p)::GetStopReasonDataAtIndex() => error: process is running", exe_ctx.GetThreadPtr());
255 }
Greg Clayton640dc6b2010-11-18 18:52:36 +0000256 }
257 return 0;
258}
259
260size_t
Chris Lattner24943d22010-06-08 16:52:24 +0000261SBThread::GetStopDescription (char *dst, size_t dst_len)
262{
Greg Claytone005f2c2010-11-06 01:53:30 +0000263 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000264
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000265 Mutex::Locker api_locker;
266 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
267
Greg Claytonf4124de2012-02-21 00:09:25 +0000268 if (exe_ctx.HasThreadScope())
Chris Lattner24943d22010-06-08 16:52:24 +0000269 {
Greg Claytona894fe72012-04-05 16:12:35 +0000270 Process::StopLocker stop_locker;
271 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
Chris Lattner24943d22010-06-08 16:52:24 +0000272 {
Greg Claytona894fe72012-04-05 16:12:35 +0000273
Greg Claytona894fe72012-04-05 16:12:35 +0000274 StopInfoSP stop_info_sp = exe_ctx.GetThreadPtr()->GetStopInfo ();
275 if (stop_info_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000276 {
Greg Claytona894fe72012-04-05 16:12:35 +0000277 const char *stop_desc = stop_info_sp->GetDescription();
278 if (stop_desc)
Chris Lattner24943d22010-06-08 16:52:24 +0000279 {
Caroline Tice7826c882010-10-26 03:11:13 +0000280 if (log)
Greg Claytona894fe72012-04-05 16:12:35 +0000281 log->Printf ("SBThread(%p)::GetStopDescription (dst, dst_len) => \"%s\"",
Greg Claytonf4124de2012-02-21 00:09:25 +0000282 exe_ctx.GetThreadPtr(), stop_desc);
Chris Lattner24943d22010-06-08 16:52:24 +0000283 if (dst)
Greg Claytona894fe72012-04-05 16:12:35 +0000284 return ::snprintf (dst, dst_len, "%s", stop_desc);
285 else
286 {
287 // NULL dst passed in, return the length needed to contain the description
288 return ::strlen (stop_desc) + 1; // Include the NULL byte for size
289 }
290 }
291 else
292 {
293 size_t stop_desc_len = 0;
294 switch (stop_info_sp->GetStopReason())
295 {
296 case eStopReasonTrace:
297 case eStopReasonPlanComplete:
298 {
299 static char trace_desc[] = "step";
300 stop_desc = trace_desc;
301 stop_desc_len = sizeof(trace_desc); // Include the NULL byte for size
302 }
303 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000304
Greg Claytona894fe72012-04-05 16:12:35 +0000305 case eStopReasonBreakpoint:
306 {
307 static char bp_desc[] = "breakpoint hit";
308 stop_desc = bp_desc;
309 stop_desc_len = sizeof(bp_desc); // Include the NULL byte for size
310 }
311 break;
312
313 case eStopReasonWatchpoint:
314 {
315 static char wp_desc[] = "watchpoint hit";
316 stop_desc = wp_desc;
317 stop_desc_len = sizeof(wp_desc); // Include the NULL byte for size
318 }
319 break;
320
321 case eStopReasonSignal:
322 {
323 stop_desc = exe_ctx.GetProcessPtr()->GetUnixSignals ().GetSignalAsCString (stop_info_sp->GetValue());
324 if (stop_desc == NULL || stop_desc[0] == '\0')
325 {
326 static char signal_desc[] = "signal";
327 stop_desc = signal_desc;
328 stop_desc_len = sizeof(signal_desc); // Include the NULL byte for size
329 }
330 }
331 break;
332
333 case eStopReasonException:
334 {
335 char exc_desc[] = "exception";
336 stop_desc = exc_desc;
337 stop_desc_len = sizeof(exc_desc); // Include the NULL byte for size
338 }
339 break;
340
Greg Clayton0bce9a22012-12-05 00:16:59 +0000341 case eStopReasonExec:
342 {
343 char exc_desc[] = "exec";
344 stop_desc = exc_desc;
345 stop_desc_len = sizeof(exc_desc); // Include the NULL byte for size
346 }
347 break;
348
Greg Claytona894fe72012-04-05 16:12:35 +0000349 default:
350 break;
351 }
352
353 if (stop_desc && stop_desc[0])
354 {
355 if (log)
356 log->Printf ("SBThread(%p)::GetStopDescription (dst, dst_len) => '%s'",
357 exe_ctx.GetThreadPtr(), stop_desc);
358
359 if (dst)
360 return ::snprintf (dst, dst_len, "%s", stop_desc) + 1; // Include the NULL byte
361
362 if (stop_desc_len == 0)
363 stop_desc_len = ::strlen (stop_desc) + 1; // Include the NULL byte
364
365 return stop_desc_len;
366 }
Chris Lattner24943d22010-06-08 16:52:24 +0000367 }
368 }
369 }
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000370 else
371 {
372 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
373 if (log)
374 log->Printf ("SBThread(%p)::GetStopDescription() => error: process is running", exe_ctx.GetThreadPtr());
375 }
Chris Lattner24943d22010-06-08 16:52:24 +0000376 }
377 if (dst)
378 *dst = 0;
379 return 0;
380}
381
Jim Ingham1586d972011-12-17 01:35:57 +0000382SBValue
383SBThread::GetStopReturnValue ()
384{
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000385 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Ingham1586d972011-12-17 01:35:57 +0000386 ValueObjectSP return_valobj_sp;
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000387 Mutex::Locker api_locker;
388 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
389
Greg Claytonf4124de2012-02-21 00:09:25 +0000390 if (exe_ctx.HasThreadScope())
Jim Ingham1586d972011-12-17 01:35:57 +0000391 {
Greg Claytona894fe72012-04-05 16:12:35 +0000392 Process::StopLocker stop_locker;
393 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
Jim Ingham1586d972011-12-17 01:35:57 +0000394 {
Greg Claytona894fe72012-04-05 16:12:35 +0000395 StopInfoSP stop_info_sp = exe_ctx.GetThreadPtr()->GetStopInfo ();
396 if (stop_info_sp)
397 {
398 return_valobj_sp = StopInfo::GetReturnValueObject (stop_info_sp);
399 }
Jim Ingham1586d972011-12-17 01:35:57 +0000400 }
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000401 else
402 {
403 if (log)
404 log->Printf ("SBThread(%p)::GetStopReturnValue() => error: process is running", exe_ctx.GetThreadPtr());
405 }
Jim Ingham1586d972011-12-17 01:35:57 +0000406 }
407
Jim Ingham1586d972011-12-17 01:35:57 +0000408 if (log)
Greg Claytonf4124de2012-02-21 00:09:25 +0000409 log->Printf ("SBThread(%p)::GetStopReturnValue () => %s", exe_ctx.GetThreadPtr(),
Jim Ingham1586d972011-12-17 01:35:57 +0000410 return_valobj_sp.get()
411 ? return_valobj_sp->GetValueAsCString()
412 : "<no return value>");
413
414 return SBValue (return_valobj_sp);
415}
416
Chris Lattner24943d22010-06-08 16:52:24 +0000417void
418SBThread::SetThread (const ThreadSP& lldb_object_sp)
419{
Greg Claytona894fe72012-04-05 16:12:35 +0000420 m_opaque_sp->SetThreadSP (lldb_object_sp);
Chris Lattner24943d22010-06-08 16:52:24 +0000421}
422
423
424lldb::tid_t
425SBThread::GetThreadID () const
426{
Greg Claytona894fe72012-04-05 16:12:35 +0000427 ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
Greg Clayton90c52142012-01-30 02:53:15 +0000428 if (thread_sp)
Greg Claytonf4124de2012-02-21 00:09:25 +0000429 return thread_sp->GetID();
430 return LLDB_INVALID_THREAD_ID;
Chris Lattner24943d22010-06-08 16:52:24 +0000431}
432
433uint32_t
434SBThread::GetIndexID () const
435{
Greg Claytona894fe72012-04-05 16:12:35 +0000436 ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
Greg Clayton90c52142012-01-30 02:53:15 +0000437 if (thread_sp)
438 return thread_sp->GetIndexID();
Chris Lattner24943d22010-06-08 16:52:24 +0000439 return LLDB_INVALID_INDEX32;
440}
Greg Claytonf4124de2012-02-21 00:09:25 +0000441
Chris Lattner24943d22010-06-08 16:52:24 +0000442const char *
443SBThread::GetName () const
444{
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000445 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytona66ba462010-10-30 04:51:46 +0000446 const char *name = NULL;
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000447 Mutex::Locker api_locker;
448 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
449
Greg Claytonf4124de2012-02-21 00:09:25 +0000450 if (exe_ctx.HasThreadScope())
Greg Claytonbdcda462010-12-20 20:49:23 +0000451 {
Greg Claytona894fe72012-04-05 16:12:35 +0000452 Process::StopLocker stop_locker;
453 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
454 {
Greg Claytona894fe72012-04-05 16:12:35 +0000455 name = exe_ctx.GetThreadPtr()->GetName();
456 }
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000457 else
458 {
459 if (log)
460 log->Printf ("SBThread(%p)::GetName() => error: process is running", exe_ctx.GetThreadPtr());
461 }
Greg Claytonbdcda462010-12-20 20:49:23 +0000462 }
Greg Claytona66ba462010-10-30 04:51:46 +0000463
Caroline Tice7826c882010-10-26 03:11:13 +0000464 if (log)
Greg Claytonf4124de2012-02-21 00:09:25 +0000465 log->Printf ("SBThread(%p)::GetName () => %s", exe_ctx.GetThreadPtr(), name ? name : "NULL");
Caroline Tice7826c882010-10-26 03:11:13 +0000466
Greg Claytona66ba462010-10-30 04:51:46 +0000467 return name;
Chris Lattner24943d22010-06-08 16:52:24 +0000468}
469
470const char *
471SBThread::GetQueueName () const
472{
Greg Claytona66ba462010-10-30 04:51:46 +0000473 const char *name = NULL;
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000474 Mutex::Locker api_locker;
475 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
476
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000477 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytonf4124de2012-02-21 00:09:25 +0000478 if (exe_ctx.HasThreadScope())
Greg Claytonbdcda462010-12-20 20:49:23 +0000479 {
Greg Claytona894fe72012-04-05 16:12:35 +0000480 Process::StopLocker stop_locker;
481 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
482 {
Greg Claytona894fe72012-04-05 16:12:35 +0000483 name = exe_ctx.GetThreadPtr()->GetQueueName();
484 }
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000485 else
486 {
487 if (log)
488 log->Printf ("SBThread(%p)::GetQueueName() => error: process is running", exe_ctx.GetThreadPtr());
489 }
Greg Claytonbdcda462010-12-20 20:49:23 +0000490 }
Greg Claytona66ba462010-10-30 04:51:46 +0000491
Caroline Tice7826c882010-10-26 03:11:13 +0000492 if (log)
Greg Claytonf4124de2012-02-21 00:09:25 +0000493 log->Printf ("SBThread(%p)::GetQueueName () => %s", exe_ctx.GetThreadPtr(), name ? name : "NULL");
Caroline Tice7826c882010-10-26 03:11:13 +0000494
Greg Claytona66ba462010-10-30 04:51:46 +0000495 return name;
Chris Lattner24943d22010-06-08 16:52:24 +0000496}
497
Jim Ingham88e3de22012-05-03 21:19:36 +0000498SBError
499SBThread::ResumeNewPlan (ExecutionContext &exe_ctx, ThreadPlan *new_plan)
500{
501 SBError sb_error;
502
503 Process *process = exe_ctx.GetProcessPtr();
504 if (!process)
505 {
506 sb_error.SetErrorString("No process in SBThread::ResumeNewPlan");
507 return sb_error;
508 }
509
510 Thread *thread = exe_ctx.GetThreadPtr();
511 if (!thread)
512 {
513 sb_error.SetErrorString("No thread in SBThread::ResumeNewPlan");
514 return sb_error;
515 }
516
517 // User level plans should be Master Plans so they can be interrupted, other plans executed, and
518 // then a "continue" will resume the plan.
519 if (new_plan != NULL)
520 {
521 new_plan->SetIsMasterPlan(true);
522 new_plan->SetOkayToDiscard(false);
523 }
524
525 // Why do we need to set the current thread by ID here???
526 process->GetThreadList().SetSelectedThreadByID (thread->GetID());
527 sb_error.ref() = process->Resume();
528
529 if (sb_error.Success())
530 {
531 // If we are doing synchronous mode, then wait for the
532 // process to stop yet again!
533 if (process->GetTarget().GetDebugger().GetAsyncExecution () == false)
534 process->WaitForProcessToStop (NULL);
535 }
536
537 return sb_error;
538}
Chris Lattner24943d22010-06-08 16:52:24 +0000539
540void
Chris Lattner24943d22010-06-08 16:52:24 +0000541SBThread::StepOver (lldb::RunMode stop_other_threads)
542{
Greg Claytone005f2c2010-11-06 01:53:30 +0000543 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000544
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000545 Mutex::Locker api_locker;
546 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
547
Caroline Tice7826c882010-10-26 03:11:13 +0000548
Greg Clayton90c52142012-01-30 02:53:15 +0000549 if (log)
Greg Claytonf4124de2012-02-21 00:09:25 +0000550 log->Printf ("SBThread(%p)::StepOver (stop_other_threads='%s')", exe_ctx.GetThreadPtr(),
Greg Clayton90c52142012-01-30 02:53:15 +0000551 Thread::RunModeAsCString (stop_other_threads));
552
Greg Claytonf4124de2012-02-21 00:09:25 +0000553 if (exe_ctx.HasThreadScope())
Chris Lattner24943d22010-06-08 16:52:24 +0000554 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000555 Thread *thread = exe_ctx.GetThreadPtr();
Jim Inghamd82bc6d2012-05-11 23:47:32 +0000556 bool abort_other_plans = false;
Greg Claytonf4124de2012-02-21 00:09:25 +0000557 StackFrameSP frame_sp(thread->GetStackFrameAtIndex (0));
Jim Ingham88e3de22012-05-03 21:19:36 +0000558 ThreadPlan *new_plan = NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000559
560 if (frame_sp)
561 {
562 if (frame_sp->HasDebugInformation ())
563 {
564 SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
Jim Ingham88e3de22012-05-03 21:19:36 +0000565 new_plan = thread->QueueThreadPlanForStepRange (abort_other_plans,
566 eStepTypeOver,
567 sc.line_entry.range,
568 sc,
569 stop_other_threads,
570 false);
Chris Lattner24943d22010-06-08 16:52:24 +0000571
572 }
573 else
574 {
Jim Ingham88e3de22012-05-03 21:19:36 +0000575 new_plan = thread->QueueThreadPlanForStepSingleInstruction (true,
576 abort_other_plans,
577 stop_other_threads);
Chris Lattner24943d22010-06-08 16:52:24 +0000578 }
579 }
580
Jim Ingham88e3de22012-05-03 21:19:36 +0000581 // This returns an error, we should use it!
582 ResumeNewPlan (exe_ctx, new_plan);
Chris Lattner24943d22010-06-08 16:52:24 +0000583 }
584}
585
586void
587SBThread::StepInto (lldb::RunMode stop_other_threads)
588{
Greg Claytone005f2c2010-11-06 01:53:30 +0000589 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000590
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000591 Mutex::Locker api_locker;
592 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
Greg Clayton90c52142012-01-30 02:53:15 +0000593
594 if (log)
Greg Claytonf4124de2012-02-21 00:09:25 +0000595 log->Printf ("SBThread(%p)::StepInto (stop_other_threads='%s')", exe_ctx.GetThreadPtr(),
Greg Clayton90c52142012-01-30 02:53:15 +0000596 Thread::RunModeAsCString (stop_other_threads));
Greg Claytonf4124de2012-02-21 00:09:25 +0000597 if (exe_ctx.HasThreadScope())
Chris Lattner24943d22010-06-08 16:52:24 +0000598 {
Jim Inghamd82bc6d2012-05-11 23:47:32 +0000599 bool abort_other_plans = false;
Chris Lattner24943d22010-06-08 16:52:24 +0000600
Greg Claytonf4124de2012-02-21 00:09:25 +0000601 Thread *thread = exe_ctx.GetThreadPtr();
602 StackFrameSP frame_sp(thread->GetStackFrameAtIndex (0));
Jim Ingham88e3de22012-05-03 21:19:36 +0000603 ThreadPlan *new_plan = NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000604
605 if (frame_sp && frame_sp->HasDebugInformation ())
606 {
Greg Clayton8f5fd6b2010-06-12 18:59:55 +0000607 bool avoid_code_without_debug_info = true;
Chris Lattner24943d22010-06-08 16:52:24 +0000608 SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
Jim Ingham88e3de22012-05-03 21:19:36 +0000609 new_plan = thread->QueueThreadPlanForStepRange (abort_other_plans,
610 eStepTypeInto,
611 sc.line_entry.range,
612 sc,
613 stop_other_threads,
614 avoid_code_without_debug_info);
Chris Lattner24943d22010-06-08 16:52:24 +0000615 }
616 else
617 {
Jim Ingham88e3de22012-05-03 21:19:36 +0000618 new_plan = thread->QueueThreadPlanForStepSingleInstruction (false,
619 abort_other_plans,
620 stop_other_threads);
Chris Lattner24943d22010-06-08 16:52:24 +0000621 }
Jim Ingham88e3de22012-05-03 21:19:36 +0000622
623 // This returns an error, we should use it!
624 ResumeNewPlan (exe_ctx, new_plan);
Chris Lattner24943d22010-06-08 16:52:24 +0000625 }
626}
627
628void
629SBThread::StepOut ()
630{
Greg Claytone005f2c2010-11-06 01:53:30 +0000631 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000632
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000633 Mutex::Locker api_locker;
634 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
635
Caroline Tice7826c882010-10-26 03:11:13 +0000636
Greg Clayton90c52142012-01-30 02:53:15 +0000637 if (log)
Greg Claytonf4124de2012-02-21 00:09:25 +0000638 log->Printf ("SBThread(%p)::StepOut ()", exe_ctx.GetThreadPtr());
Greg Clayton90c52142012-01-30 02:53:15 +0000639
Greg Claytonf4124de2012-02-21 00:09:25 +0000640 if (exe_ctx.HasThreadScope())
Chris Lattner24943d22010-06-08 16:52:24 +0000641 {
Jim Inghamd82bc6d2012-05-11 23:47:32 +0000642 bool abort_other_plans = false;
Jim Inghambe0cde92012-09-14 21:07:14 +0000643 bool stop_other_threads = false;
Chris Lattner24943d22010-06-08 16:52:24 +0000644
Greg Claytonf4124de2012-02-21 00:09:25 +0000645 Thread *thread = exe_ctx.GetThreadPtr();
646
Jim Ingham88e3de22012-05-03 21:19:36 +0000647 ThreadPlan *new_plan = thread->QueueThreadPlanForStepOut (abort_other_plans,
648 NULL,
649 false,
650 stop_other_threads,
651 eVoteYes,
652 eVoteNoOpinion,
653 0);
654
655 // This returns an error, we should use it!
656 ResumeNewPlan (exe_ctx, new_plan);
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000657 }
658}
Chris Lattner24943d22010-06-08 16:52:24 +0000659
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000660void
661SBThread::StepOutOfFrame (lldb::SBFrame &sb_frame)
662{
663 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
664
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000665 Mutex::Locker api_locker;
666 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
667
Greg Clayton334d33a2012-01-30 07:41:31 +0000668 StackFrameSP frame_sp (sb_frame.GetFrameSP());
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000669 if (log)
670 {
671 SBStream frame_desc_strm;
672 sb_frame.GetDescription (frame_desc_strm);
Greg Claytonf4124de2012-02-21 00:09:25 +0000673 log->Printf ("SBThread(%p)::StepOutOfFrame (frame = SBFrame(%p): %s)", exe_ctx.GetThreadPtr(), frame_sp.get(), frame_desc_strm.GetData());
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000674 }
675
Greg Claytonf4124de2012-02-21 00:09:25 +0000676 if (exe_ctx.HasThreadScope())
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000677 {
Jim Inghamd82bc6d2012-05-11 23:47:32 +0000678 bool abort_other_plans = false;
Jim Inghambe0cde92012-09-14 21:07:14 +0000679 bool stop_other_threads = false;
Greg Claytonf4124de2012-02-21 00:09:25 +0000680 Thread *thread = exe_ctx.GetThreadPtr();
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000681
Jim Ingham88e3de22012-05-03 21:19:36 +0000682 ThreadPlan *new_plan = thread->QueueThreadPlanForStepOut (abort_other_plans,
683 NULL,
684 false,
685 stop_other_threads,
686 eVoteYes,
687 eVoteNoOpinion,
688 frame_sp->GetFrameIndex());
689
690 // This returns an error, we should use it!
691 ResumeNewPlan (exe_ctx, new_plan);
Chris Lattner24943d22010-06-08 16:52:24 +0000692 }
693}
694
695void
696SBThread::StepInstruction (bool step_over)
697{
Greg Claytone005f2c2010-11-06 01:53:30 +0000698 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000699
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000700 Mutex::Locker api_locker;
701 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
702
Greg Claytonf4124de2012-02-21 00:09:25 +0000703
Caroline Tice7826c882010-10-26 03:11:13 +0000704
Greg Clayton90c52142012-01-30 02:53:15 +0000705 if (log)
Greg Claytonf4124de2012-02-21 00:09:25 +0000706 log->Printf ("SBThread(%p)::StepInstruction (step_over=%i)", exe_ctx.GetThreadPtr(), step_over);
Greg Clayton90c52142012-01-30 02:53:15 +0000707
Greg Claytonf4124de2012-02-21 00:09:25 +0000708 if (exe_ctx.HasThreadScope())
Chris Lattner24943d22010-06-08 16:52:24 +0000709 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000710 Thread *thread = exe_ctx.GetThreadPtr();
Jim Ingham88e3de22012-05-03 21:19:36 +0000711 ThreadPlan *new_plan = thread->QueueThreadPlanForStepSingleInstruction (step_over, true, true);
712
713 // This returns an error, we should use it!
714 ResumeNewPlan (exe_ctx, new_plan);
Chris Lattner24943d22010-06-08 16:52:24 +0000715 }
716}
717
718void
719SBThread::RunToAddress (lldb::addr_t addr)
720{
Greg Claytone005f2c2010-11-06 01:53:30 +0000721 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000722
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000723 Mutex::Locker api_locker;
724 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
725
Caroline Tice7826c882010-10-26 03:11:13 +0000726
Greg Clayton90c52142012-01-30 02:53:15 +0000727 if (log)
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000728 log->Printf ("SBThread(%p)::RunToAddress (addr=0x%" PRIx64 ")", exe_ctx.GetThreadPtr(), addr);
Greg Clayton90c52142012-01-30 02:53:15 +0000729
Greg Claytonf4124de2012-02-21 00:09:25 +0000730 if (exe_ctx.HasThreadScope())
Chris Lattner24943d22010-06-08 16:52:24 +0000731 {
Jim Inghamd82bc6d2012-05-11 23:47:32 +0000732 bool abort_other_plans = false;
Chris Lattner24943d22010-06-08 16:52:24 +0000733 bool stop_other_threads = true;
734
Greg Clayton3508c382012-02-24 01:59:29 +0000735 Address target_addr (addr);
Chris Lattner24943d22010-06-08 16:52:24 +0000736
Greg Claytonf4124de2012-02-21 00:09:25 +0000737 Thread *thread = exe_ctx.GetThreadPtr();
Greg Claytonf4124de2012-02-21 00:09:25 +0000738
Jim Ingham88e3de22012-05-03 21:19:36 +0000739 ThreadPlan *new_plan = thread->QueueThreadPlanForRunToAddress (abort_other_plans, target_addr, stop_other_threads);
740
741 // This returns an error, we should use it!
742 ResumeNewPlan (exe_ctx, new_plan);
Chris Lattner24943d22010-06-08 16:52:24 +0000743 }
Chris Lattner24943d22010-06-08 16:52:24 +0000744}
745
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000746SBError
747SBThread::StepOverUntil (lldb::SBFrame &sb_frame,
748 lldb::SBFileSpec &sb_file_spec,
749 uint32_t line)
750{
751 SBError sb_error;
752 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
753 char path[PATH_MAX];
Greg Clayton90c52142012-01-30 02:53:15 +0000754
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000755 Mutex::Locker api_locker;
756 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
757
Greg Clayton334d33a2012-01-30 07:41:31 +0000758 StackFrameSP frame_sp (sb_frame.GetFrameSP());
759
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000760 if (log)
761 {
762 SBStream frame_desc_strm;
763 sb_frame.GetDescription (frame_desc_strm);
764 sb_file_spec->GetPath (path, sizeof(path));
765 log->Printf ("SBThread(%p)::StepOverUntil (frame = SBFrame(%p): %s, file+line = %s:%u)",
Greg Claytonf4124de2012-02-21 00:09:25 +0000766 exe_ctx.GetThreadPtr(),
Greg Clayton334d33a2012-01-30 07:41:31 +0000767 frame_sp.get(),
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000768 frame_desc_strm.GetData(),
769 path, line);
770 }
Greg Clayton90c52142012-01-30 02:53:15 +0000771
Greg Claytonf4124de2012-02-21 00:09:25 +0000772 if (exe_ctx.HasThreadScope())
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000773 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000774 Target *target = exe_ctx.GetTargetPtr();
Greg Claytonf4124de2012-02-21 00:09:25 +0000775 Thread *thread = exe_ctx.GetThreadPtr();
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000776
777 if (line == 0)
778 {
779 sb_error.SetErrorString("invalid line argument");
780 return sb_error;
781 }
782
Greg Clayton334d33a2012-01-30 07:41:31 +0000783 if (!frame_sp)
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000784 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000785 frame_sp = thread->GetSelectedFrame ();
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000786 if (!frame_sp)
Greg Claytonf4124de2012-02-21 00:09:25 +0000787 frame_sp = thread->GetStackFrameAtIndex (0);
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000788 }
789
790 SymbolContext frame_sc;
791 if (!frame_sp)
792 {
793 sb_error.SetErrorString("no valid frames in thread to step");
794 return sb_error;
795 }
796
797 // If we have a frame, get its line
798 frame_sc = frame_sp->GetSymbolContext (eSymbolContextCompUnit |
799 eSymbolContextFunction |
800 eSymbolContextLineEntry |
801 eSymbolContextSymbol );
802
803 if (frame_sc.comp_unit == NULL)
804 {
805 sb_error.SetErrorStringWithFormat("frame %u doesn't have debug information", frame_sp->GetFrameIndex());
806 return sb_error;
807 }
808
809 FileSpec step_file_spec;
810 if (sb_file_spec.IsValid())
811 {
812 // The file spec passed in was valid, so use it
813 step_file_spec = sb_file_spec.ref();
814 }
815 else
816 {
817 if (frame_sc.line_entry.IsValid())
818 step_file_spec = frame_sc.line_entry.file;
819 else
820 {
821 sb_error.SetErrorString("invalid file argument or no file for frame");
822 return sb_error;
823 }
824 }
825
Jim Inghamb07c62a2011-05-08 00:56:32 +0000826 // Grab the current function, then we will make sure the "until" address is
827 // within the function. We discard addresses that are out of the current
828 // function, and then if there are no addresses remaining, give an appropriate
829 // error message.
830
831 bool all_in_function = true;
832 AddressRange fun_range = frame_sc.function->GetAddressRange();
833
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000834 std::vector<addr_t> step_over_until_addrs;
Jim Inghamd82bc6d2012-05-11 23:47:32 +0000835 const bool abort_other_plans = false;
Jim Ingham52124e72012-09-14 18:57:14 +0000836 const bool stop_other_threads = false;
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000837 const bool check_inlines = true;
838 const bool exact = false;
839
840 SymbolContextList sc_list;
Jim Inghamb07c62a2011-05-08 00:56:32 +0000841 const uint32_t num_matches = frame_sc.comp_unit->ResolveSymbolContext (step_file_spec,
842 line,
843 check_inlines,
844 exact,
845 eSymbolContextLineEntry,
846 sc_list);
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000847 if (num_matches > 0)
848 {
849 SymbolContext sc;
850 for (uint32_t i=0; i<num_matches; ++i)
851 {
852 if (sc_list.GetContextAtIndex(i, sc))
853 {
Jim Inghamb07c62a2011-05-08 00:56:32 +0000854 addr_t step_addr = sc.line_entry.range.GetBaseAddress().GetLoadAddress(target);
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000855 if (step_addr != LLDB_INVALID_ADDRESS)
856 {
Jim Inghamb07c62a2011-05-08 00:56:32 +0000857 if (fun_range.ContainsLoadAddress(step_addr, target))
858 step_over_until_addrs.push_back(step_addr);
859 else
860 all_in_function = false;
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000861 }
862 }
863 }
864 }
Jim Inghamb07c62a2011-05-08 00:56:32 +0000865
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000866 if (step_over_until_addrs.empty())
867 {
Jim Inghamb07c62a2011-05-08 00:56:32 +0000868 if (all_in_function)
869 {
870 step_file_spec.GetPath (path, sizeof(path));
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000871 sb_error.SetErrorStringWithFormat("No line entries for %s:%u", path, line);
Jim Inghamb07c62a2011-05-08 00:56:32 +0000872 }
873 else
Greg Clayton9c236732011-10-26 00:56:27 +0000874 sb_error.SetErrorString ("step until target not in current function");
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000875 }
876 else
877 {
Jim Ingham88e3de22012-05-03 21:19:36 +0000878 ThreadPlan *new_plan = thread->QueueThreadPlanForStepUntil (abort_other_plans,
879 &step_over_until_addrs[0],
880 step_over_until_addrs.size(),
881 stop_other_threads,
882 frame_sp->GetFrameIndex());
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000883
Jim Ingham88e3de22012-05-03 21:19:36 +0000884 sb_error = ResumeNewPlan (exe_ctx, new_plan);
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000885 }
886 }
887 else
888 {
889 sb_error.SetErrorString("this SBThread object is invalid");
890 }
891 return sb_error;
892}
893
Jim Inghama17a81a2012-09-12 00:40:39 +0000894SBError
Jim Inghamf59388a2012-09-14 02:14:15 +0000895SBThread::ReturnFromFrame (SBFrame &frame, SBValue &return_value)
Jim Inghama17a81a2012-09-12 00:40:39 +0000896{
897 SBError sb_error;
898
899 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
900
901 Mutex::Locker api_locker;
902 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
903
904
905 if (log)
Jim Inghamf59388a2012-09-14 02:14:15 +0000906 log->Printf ("SBThread(%p)::ReturnFromFrame (frame=%d)", exe_ctx.GetThreadPtr(), frame.GetFrameID());
Jim Inghama17a81a2012-09-12 00:40:39 +0000907
908 if (exe_ctx.HasThreadScope())
909 {
910 Thread *thread = exe_ctx.GetThreadPtr();
Jim Inghamf59388a2012-09-14 02:14:15 +0000911 sb_error.SetError (thread->ReturnFromFrame(frame.GetFrameSP(), return_value.GetSP()));
Jim Inghama17a81a2012-09-12 00:40:39 +0000912 }
913
914 return sb_error;
915}
916
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000917
Greg Clayton123db402011-01-12 02:25:42 +0000918bool
919SBThread::Suspend()
920{
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000921 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytona894fe72012-04-05 16:12:35 +0000922 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000923 bool result = false;
Greg Claytonf4124de2012-02-21 00:09:25 +0000924 if (exe_ctx.HasThreadScope())
Greg Clayton123db402011-01-12 02:25:42 +0000925 {
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000926 Process::StopLocker stop_locker;
927 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
928 {
929 exe_ctx.GetThreadPtr()->SetResumeState (eStateSuspended);
930 result = true;
931 }
932 else
933 {
934 if (log)
935 log->Printf ("SBThread(%p)::Suspend() => error: process is running", exe_ctx.GetThreadPtr());
936 }
Greg Clayton123db402011-01-12 02:25:42 +0000937 }
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000938 if (log)
939 log->Printf ("SBThread(%p)::Suspend() => %i", exe_ctx.GetThreadPtr(), result);
940 return result;
Greg Clayton123db402011-01-12 02:25:42 +0000941}
942
943bool
944SBThread::Resume ()
945{
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000946 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytona894fe72012-04-05 16:12:35 +0000947 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000948 bool result = false;
Greg Claytonf4124de2012-02-21 00:09:25 +0000949 if (exe_ctx.HasThreadScope())
Greg Clayton123db402011-01-12 02:25:42 +0000950 {
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000951 Process::StopLocker stop_locker;
952 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
953 {
954 exe_ctx.GetThreadPtr()->SetResumeState (eStateRunning);
955 result = true;
956 }
957 else
958 {
959 if (log)
960 log->Printf ("SBThread(%p)::Resume() => error: process is running", exe_ctx.GetThreadPtr());
961 }
Greg Clayton123db402011-01-12 02:25:42 +0000962 }
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000963 if (log)
964 log->Printf ("SBThread(%p)::Resume() => %i", exe_ctx.GetThreadPtr(), result);
965 return result;
Greg Clayton123db402011-01-12 02:25:42 +0000966}
967
968bool
969SBThread::IsSuspended()
970{
Greg Claytona894fe72012-04-05 16:12:35 +0000971 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Claytonf4124de2012-02-21 00:09:25 +0000972 if (exe_ctx.HasThreadScope())
973 return exe_ctx.GetThreadPtr()->GetResumeState () == eStateSuspended;
Greg Clayton123db402011-01-12 02:25:42 +0000974 return false;
975}
976
Chris Lattner24943d22010-06-08 16:52:24 +0000977SBProcess
978SBThread::GetProcess ()
979{
Caroline Tice7826c882010-10-26 03:11:13 +0000980
Greg Clayton334d33a2012-01-30 07:41:31 +0000981 SBProcess sb_process;
982 ProcessSP process_sp;
Greg Claytona894fe72012-04-05 16:12:35 +0000983 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Claytonf4124de2012-02-21 00:09:25 +0000984 if (exe_ctx.HasThreadScope())
Chris Lattner24943d22010-06-08 16:52:24 +0000985 {
986 // Have to go up to the target so we can get a shared pointer to our process...
Greg Claytonf4124de2012-02-21 00:09:25 +0000987 sb_process.SetSP (exe_ctx.GetProcessSP());
Chris Lattner24943d22010-06-08 16:52:24 +0000988 }
Caroline Tice7826c882010-10-26 03:11:13 +0000989
Greg Claytone005f2c2010-11-06 01:53:30 +0000990 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000991 if (log)
992 {
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000993 SBStream frame_desc_strm;
Greg Clayton334d33a2012-01-30 07:41:31 +0000994 sb_process.GetDescription (frame_desc_strm);
Greg Claytonf4124de2012-02-21 00:09:25 +0000995 log->Printf ("SBThread(%p)::GetProcess () => SBProcess(%p): %s", exe_ctx.GetThreadPtr(),
Greg Clayton334d33a2012-01-30 07:41:31 +0000996 process_sp.get(), frame_desc_strm.GetData());
Caroline Tice7826c882010-10-26 03:11:13 +0000997 }
998
Greg Clayton334d33a2012-01-30 07:41:31 +0000999 return sb_process;
Chris Lattner24943d22010-06-08 16:52:24 +00001000}
1001
1002uint32_t
1003SBThread::GetNumFrames ()
1004{
Greg Claytone005f2c2010-11-06 01:53:30 +00001005 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +00001006
Caroline Tice7826c882010-10-26 03:11:13 +00001007 uint32_t num_frames = 0;
Jim Inghamd0bdddf2012-08-22 21:34:33 +00001008 Mutex::Locker api_locker;
1009 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1010
Greg Claytonf4124de2012-02-21 00:09:25 +00001011 if (exe_ctx.HasThreadScope())
Greg Claytonbdcda462010-12-20 20:49:23 +00001012 {
Greg Claytona894fe72012-04-05 16:12:35 +00001013 Process::StopLocker stop_locker;
1014 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1015 {
Greg Claytona894fe72012-04-05 16:12:35 +00001016 num_frames = exe_ctx.GetThreadPtr()->GetStackFrameCount();
1017 }
Greg Clayton9f3c98e2012-04-06 02:17:47 +00001018 else
1019 {
1020 if (log)
1021 log->Printf ("SBThread(%p)::GetNumFrames() => error: process is running", exe_ctx.GetThreadPtr());
1022 }
Greg Claytonbdcda462010-12-20 20:49:23 +00001023 }
Caroline Tice7826c882010-10-26 03:11:13 +00001024
1025 if (log)
Greg Claytonf4124de2012-02-21 00:09:25 +00001026 log->Printf ("SBThread(%p)::GetNumFrames () => %u", exe_ctx.GetThreadPtr(), num_frames);
Caroline Tice7826c882010-10-26 03:11:13 +00001027
1028 return num_frames;
Chris Lattner24943d22010-06-08 16:52:24 +00001029}
1030
1031SBFrame
1032SBThread::GetFrameAtIndex (uint32_t idx)
1033{
Greg Claytone005f2c2010-11-06 01:53:30 +00001034 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +00001035
Chris Lattner24943d22010-06-08 16:52:24 +00001036 SBFrame sb_frame;
Greg Clayton334d33a2012-01-30 07:41:31 +00001037 StackFrameSP frame_sp;
Jim Inghamd0bdddf2012-08-22 21:34:33 +00001038 Mutex::Locker api_locker;
1039 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1040
Greg Claytonf4124de2012-02-21 00:09:25 +00001041 if (exe_ctx.HasThreadScope())
Greg Claytonbdcda462010-12-20 20:49:23 +00001042 {
Greg Claytona894fe72012-04-05 16:12:35 +00001043 Process::StopLocker stop_locker;
1044 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1045 {
Greg Claytona894fe72012-04-05 16:12:35 +00001046 frame_sp = exe_ctx.GetThreadPtr()->GetStackFrameAtIndex (idx);
1047 sb_frame.SetFrameSP (frame_sp);
1048 }
Greg Clayton9f3c98e2012-04-06 02:17:47 +00001049 else
1050 {
1051 if (log)
1052 log->Printf ("SBThread(%p)::GetFrameAtIndex() => error: process is running", exe_ctx.GetThreadPtr());
1053 }
Greg Claytonbdcda462010-12-20 20:49:23 +00001054 }
Caroline Tice7826c882010-10-26 03:11:13 +00001055
1056 if (log)
1057 {
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001058 SBStream frame_desc_strm;
1059 sb_frame.GetDescription (frame_desc_strm);
Greg Claytona66ba462010-10-30 04:51:46 +00001060 log->Printf ("SBThread(%p)::GetFrameAtIndex (idx=%d) => SBFrame(%p): %s",
Greg Claytonf4124de2012-02-21 00:09:25 +00001061 exe_ctx.GetThreadPtr(), idx, frame_sp.get(), frame_desc_strm.GetData());
Caroline Tice7826c882010-10-26 03:11:13 +00001062 }
1063
Chris Lattner24943d22010-06-08 16:52:24 +00001064 return sb_frame;
1065}
1066
Greg Claytonc5157ec2010-12-17 02:26:24 +00001067lldb::SBFrame
1068SBThread::GetSelectedFrame ()
1069{
1070 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1071
1072 SBFrame sb_frame;
Greg Clayton334d33a2012-01-30 07:41:31 +00001073 StackFrameSP frame_sp;
Jim Inghamd0bdddf2012-08-22 21:34:33 +00001074 Mutex::Locker api_locker;
1075 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1076
Greg Claytonf4124de2012-02-21 00:09:25 +00001077 if (exe_ctx.HasThreadScope())
Greg Claytonbdcda462010-12-20 20:49:23 +00001078 {
Greg Claytona894fe72012-04-05 16:12:35 +00001079 Process::StopLocker stop_locker;
1080 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1081 {
Greg Claytona894fe72012-04-05 16:12:35 +00001082 frame_sp = exe_ctx.GetThreadPtr()->GetSelectedFrame ();
1083 sb_frame.SetFrameSP (frame_sp);
1084 }
Greg Clayton9f3c98e2012-04-06 02:17:47 +00001085 else
1086 {
1087 if (log)
1088 log->Printf ("SBThread(%p)::GetSelectedFrame() => error: process is running", exe_ctx.GetThreadPtr());
1089 }
Greg Claytonbdcda462010-12-20 20:49:23 +00001090 }
Greg Claytonc5157ec2010-12-17 02:26:24 +00001091
1092 if (log)
1093 {
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001094 SBStream frame_desc_strm;
1095 sb_frame.GetDescription (frame_desc_strm);
Greg Claytonc5157ec2010-12-17 02:26:24 +00001096 log->Printf ("SBThread(%p)::GetSelectedFrame () => SBFrame(%p): %s",
Greg Claytonf4124de2012-02-21 00:09:25 +00001097 exe_ctx.GetThreadPtr(), frame_sp.get(), frame_desc_strm.GetData());
Greg Claytonc5157ec2010-12-17 02:26:24 +00001098 }
1099
1100 return sb_frame;
1101}
1102
1103lldb::SBFrame
1104SBThread::SetSelectedFrame (uint32_t idx)
1105{
1106 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1107
1108 SBFrame sb_frame;
Greg Clayton334d33a2012-01-30 07:41:31 +00001109 StackFrameSP frame_sp;
Jim Inghamd0bdddf2012-08-22 21:34:33 +00001110 Mutex::Locker api_locker;
1111 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1112
Greg Claytonf4124de2012-02-21 00:09:25 +00001113 if (exe_ctx.HasThreadScope())
Greg Claytonc5157ec2010-12-17 02:26:24 +00001114 {
Greg Claytona894fe72012-04-05 16:12:35 +00001115 Process::StopLocker stop_locker;
1116 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
Greg Claytonc5157ec2010-12-17 02:26:24 +00001117 {
Greg Claytona894fe72012-04-05 16:12:35 +00001118 Thread *thread = exe_ctx.GetThreadPtr();
1119 frame_sp = thread->GetStackFrameAtIndex (idx);
1120 if (frame_sp)
1121 {
1122 thread->SetSelectedFrame (frame_sp.get());
1123 sb_frame.SetFrameSP (frame_sp);
1124 }
Greg Claytonc5157ec2010-12-17 02:26:24 +00001125 }
Greg Clayton9f3c98e2012-04-06 02:17:47 +00001126 else
1127 {
1128 if (log)
1129 log->Printf ("SBThread(%p)::SetSelectedFrame() => error: process is running", exe_ctx.GetThreadPtr());
1130 }
Greg Claytonc5157ec2010-12-17 02:26:24 +00001131 }
1132
1133 if (log)
1134 {
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001135 SBStream frame_desc_strm;
1136 sb_frame.GetDescription (frame_desc_strm);
Greg Claytonc5157ec2010-12-17 02:26:24 +00001137 log->Printf ("SBThread(%p)::SetSelectedFrame (idx=%u) => SBFrame(%p): %s",
Greg Claytonf4124de2012-02-21 00:09:25 +00001138 exe_ctx.GetThreadPtr(), idx, frame_sp.get(), frame_desc_strm.GetData());
Greg Claytonc5157ec2010-12-17 02:26:24 +00001139 }
1140 return sb_frame;
1141}
1142
Jim Ingham94a5d0d2012-10-10 18:32:14 +00001143bool
1144SBThread::EventIsThreadEvent (const SBEvent &event)
1145{
1146 return Thread::ThreadEventData::GetEventDataFromEvent(event.get()) != NULL;
1147}
1148
1149SBFrame
1150SBThread::GetStackFrameFromEvent (const SBEvent &event)
1151{
1152 return Thread::ThreadEventData::GetStackFrameFromEvent (event.get());
1153
1154}
1155
1156SBThread
1157SBThread::GetThreadFromEvent (const SBEvent &event)
1158{
1159 return Thread::ThreadEventData::GetThreadFromEvent (event.get());
1160}
Greg Claytonc5157ec2010-12-17 02:26:24 +00001161
Chris Lattner24943d22010-06-08 16:52:24 +00001162bool
1163SBThread::operator == (const SBThread &rhs) const
1164{
Greg Claytona894fe72012-04-05 16:12:35 +00001165 return m_opaque_sp->GetThreadSP().get() == rhs.m_opaque_sp->GetThreadSP().get();
Chris Lattner24943d22010-06-08 16:52:24 +00001166}
1167
1168bool
1169SBThread::operator != (const SBThread &rhs) const
1170{
Greg Claytona894fe72012-04-05 16:12:35 +00001171 return m_opaque_sp->GetThreadSP().get() != rhs.m_opaque_sp->GetThreadSP().get();
Chris Lattner24943d22010-06-08 16:52:24 +00001172}
Caroline Tice98f930f2010-09-20 05:20:02 +00001173
1174bool
Jim Ingham94a5d0d2012-10-10 18:32:14 +00001175SBThread::GetStatus (SBStream &status) const
1176{
1177 Stream &strm = status.ref();
1178
1179 ExecutionContext exe_ctx (m_opaque_sp.get());
1180 if (exe_ctx.HasThreadScope())
1181 {
1182 exe_ctx.GetThreadPtr()->GetStatus(strm, 0, 1, 1);
1183 }
1184 else
1185 strm.PutCString ("No status");
1186
1187 return true;
1188}
1189
1190bool
Caroline Tice7826c882010-10-26 03:11:13 +00001191SBThread::GetDescription (SBStream &description) const
1192{
Greg Clayton96154be2011-11-13 06:57:31 +00001193 Stream &strm = description.ref();
1194
Greg Claytona894fe72012-04-05 16:12:35 +00001195 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Claytonf4124de2012-02-21 00:09:25 +00001196 if (exe_ctx.HasThreadScope())
Caroline Tice7826c882010-10-26 03:11:13 +00001197 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001198 strm.Printf("SBThread: tid = 0x%4.4" PRIx64, exe_ctx.GetThreadPtr()->GetID());
Caroline Tice7826c882010-10-26 03:11:13 +00001199 }
1200 else
Greg Clayton96154be2011-11-13 06:57:31 +00001201 strm.PutCString ("No value");
Caroline Tice7826c882010-10-26 03:11:13 +00001202
1203 return true;
1204}