blob: 510f7420a494b5149ad370a2b90946b358744ff6 [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 Ingham1586d972011-12-17 01:35:57 +000035#include "lldb/API/SBFrame.h"
Eli Friedman7a62c8b2010-06-09 07:44:37 +000036#include "lldb/API/SBProcess.h"
Jim Ingham1586d972011-12-17 01:35:57 +000037#include "lldb/API/SBValue.h"
Chris Lattner24943d22010-06-08 16:52:24 +000038
39using namespace lldb;
40using namespace lldb_private;
41
Greg Clayton49ce6822010-10-31 03:01:06 +000042//----------------------------------------------------------------------
43// Constructors
44//----------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +000045SBThread::SBThread () :
Greg Claytona894fe72012-04-05 16:12:35 +000046 m_opaque_sp (new ExecutionContextRef())
Chris Lattner24943d22010-06-08 16:52:24 +000047{
48}
49
Chris Lattner24943d22010-06-08 16:52:24 +000050SBThread::SBThread (const ThreadSP& lldb_object_sp) :
Greg Claytona894fe72012-04-05 16:12:35 +000051 m_opaque_sp (new ExecutionContextRef(lldb_object_sp))
Chris Lattner24943d22010-06-08 16:52:24 +000052{
53}
54
Greg Clayton1b284412010-10-30 18:26:59 +000055SBThread::SBThread (const SBThread &rhs) :
Greg Claytona894fe72012-04-05 16:12:35 +000056 m_opaque_sp (new ExecutionContextRef(*rhs.m_opaque_sp))
Chris Lattner24943d22010-06-08 16:52:24 +000057{
Greg Claytona894fe72012-04-05 16:12:35 +000058
Chris Lattner24943d22010-06-08 16:52:24 +000059}
60
61//----------------------------------------------------------------------
Greg Clayton49ce6822010-10-31 03:01:06 +000062// Assignment operator
63//----------------------------------------------------------------------
64
65const lldb::SBThread &
66SBThread::operator = (const SBThread &rhs)
67{
68 if (this != &rhs)
Greg Claytona894fe72012-04-05 16:12:35 +000069 *m_opaque_sp = *rhs.m_opaque_sp;
Greg Clayton49ce6822010-10-31 03:01:06 +000070 return *this;
71}
72
73//----------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +000074// Destructor
75//----------------------------------------------------------------------
76SBThread::~SBThread()
77{
78}
79
80bool
81SBThread::IsValid() const
82{
Greg Claytona894fe72012-04-05 16:12:35 +000083 return m_opaque_sp->GetThreadSP().get() != NULL;
Chris Lattner24943d22010-06-08 16:52:24 +000084}
85
Greg Clayton43490d12010-07-30 20:12:55 +000086void
87SBThread::Clear ()
88{
Greg Claytona894fe72012-04-05 16:12:35 +000089 m_opaque_sp->Clear();
Greg Clayton43490d12010-07-30 20:12:55 +000090}
91
92
Chris Lattner24943d22010-06-08 16:52:24 +000093StopReason
94SBThread::GetStopReason()
95{
Greg Claytone005f2c2010-11-06 01:53:30 +000096 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +000097
Caroline Tice7826c882010-10-26 03:11:13 +000098 StopReason reason = eStopReasonInvalid;
Greg Claytona894fe72012-04-05 16:12:35 +000099 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Claytonf4124de2012-02-21 00:09:25 +0000100 if (exe_ctx.HasThreadScope())
Chris Lattner24943d22010-06-08 16:52:24 +0000101 {
Greg Claytona894fe72012-04-05 16:12:35 +0000102 Process::StopLocker stop_locker;
103 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
104 {
105 Mutex::Locker api_locker (exe_ctx.GetTargetPtr()->GetAPIMutex());
106 StopInfoSP stop_info_sp = exe_ctx.GetThreadPtr()->GetStopInfo ();
107 if (stop_info_sp)
108 reason = stop_info_sp->GetStopReason();
109 }
Chris Lattner24943d22010-06-08 16:52:24 +0000110 }
Caroline Tice7826c882010-10-26 03:11:13 +0000111
112 if (log)
Greg Claytonf4124de2012-02-21 00:09:25 +0000113 log->Printf ("SBThread(%p)::GetStopReason () => %s", exe_ctx.GetThreadPtr(),
Caroline Tice61ba7ec2010-10-26 23:49:36 +0000114 Thread::StopReasonAsCString (reason));
Caroline Tice7826c882010-10-26 03:11:13 +0000115
116 return reason;
Chris Lattner24943d22010-06-08 16:52:24 +0000117}
118
119size_t
Greg Clayton640dc6b2010-11-18 18:52:36 +0000120SBThread::GetStopReasonDataCount ()
121{
Greg Claytona894fe72012-04-05 16:12:35 +0000122 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Claytonf4124de2012-02-21 00:09:25 +0000123 if (exe_ctx.HasThreadScope())
Greg Clayton640dc6b2010-11-18 18:52:36 +0000124 {
Greg Claytona894fe72012-04-05 16:12:35 +0000125 Process::StopLocker stop_locker;
126 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
Greg Clayton640dc6b2010-11-18 18:52:36 +0000127 {
Greg Claytona894fe72012-04-05 16:12:35 +0000128 Mutex::Locker api_locker (exe_ctx.GetTargetPtr()->GetAPIMutex());
129 StopInfoSP stop_info_sp = exe_ctx.GetThreadPtr()->GetStopInfo ();
130 if (stop_info_sp)
Greg Clayton640dc6b2010-11-18 18:52:36 +0000131 {
Greg Claytona894fe72012-04-05 16:12:35 +0000132 StopReason reason = stop_info_sp->GetStopReason();
133 switch (reason)
Greg Clayton640dc6b2010-11-18 18:52:36 +0000134 {
Greg Claytona894fe72012-04-05 16:12:35 +0000135 case eStopReasonInvalid:
136 case eStopReasonNone:
137 case eStopReasonTrace:
138 case eStopReasonPlanComplete:
139 // There is no data for these stop reasons.
140 return 0;
141
142 case eStopReasonBreakpoint:
143 {
144 break_id_t site_id = stop_info_sp->GetValue();
145 lldb::BreakpointSiteSP bp_site_sp (exe_ctx.GetProcessPtr()->GetBreakpointSiteList().FindByID (site_id));
146 if (bp_site_sp)
147 return bp_site_sp->GetNumberOfOwners () * 2;
148 else
149 return 0; // Breakpoint must have cleared itself...
150 }
151 break;
152
153 case eStopReasonWatchpoint:
154 return 1;
155
156 case eStopReasonSignal:
157 return 1;
158
159 case eStopReasonException:
160 return 1;
Greg Clayton640dc6b2010-11-18 18:52:36 +0000161 }
Greg Clayton640dc6b2010-11-18 18:52:36 +0000162 }
163 }
164 }
165 return 0;
166}
167
168uint64_t
169SBThread::GetStopReasonDataAtIndex (uint32_t idx)
170{
Greg Claytona894fe72012-04-05 16:12:35 +0000171 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Claytonf4124de2012-02-21 00:09:25 +0000172 if (exe_ctx.HasThreadScope())
Greg Clayton640dc6b2010-11-18 18:52:36 +0000173 {
Greg Claytona894fe72012-04-05 16:12:35 +0000174 Process::StopLocker stop_locker;
175 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
Greg Clayton640dc6b2010-11-18 18:52:36 +0000176 {
Greg Clayton640dc6b2010-11-18 18:52:36 +0000177
Greg Claytona894fe72012-04-05 16:12:35 +0000178 Mutex::Locker api_locker (exe_ctx.GetTargetPtr()->GetAPIMutex());
179 Thread *thread = exe_ctx.GetThreadPtr();
180 StopInfoSP stop_info_sp = thread->GetStopInfo ();
181 if (stop_info_sp)
182 {
183 StopReason reason = stop_info_sp->GetStopReason();
184 switch (reason)
Greg Clayton640dc6b2010-11-18 18:52:36 +0000185 {
Greg Claytona894fe72012-04-05 16:12:35 +0000186 case eStopReasonInvalid:
187 case eStopReasonNone:
188 case eStopReasonTrace:
189 case eStopReasonPlanComplete:
190 // There is no data for these stop reasons.
191 return 0;
192
193 case eStopReasonBreakpoint:
Greg Clayton640dc6b2010-11-18 18:52:36 +0000194 {
Greg Claytona894fe72012-04-05 16:12:35 +0000195 break_id_t site_id = stop_info_sp->GetValue();
196 lldb::BreakpointSiteSP bp_site_sp (exe_ctx.GetProcessPtr()->GetBreakpointSiteList().FindByID (site_id));
197 if (bp_site_sp)
Greg Clayton640dc6b2010-11-18 18:52:36 +0000198 {
Greg Claytona894fe72012-04-05 16:12:35 +0000199 uint32_t bp_index = idx / 2;
200 BreakpointLocationSP bp_loc_sp (bp_site_sp->GetOwnerAtIndex (bp_index));
201 if (bp_loc_sp)
Greg Clayton640dc6b2010-11-18 18:52:36 +0000202 {
Greg Claytona894fe72012-04-05 16:12:35 +0000203 if (bp_index & 1)
204 {
205 // Odd idx, return the breakpoint location ID
206 return bp_loc_sp->GetID();
207 }
208 else
209 {
210 // Even idx, return the breakpoint ID
211 return bp_loc_sp->GetBreakpoint().GetID();
212 }
Greg Clayton640dc6b2010-11-18 18:52:36 +0000213 }
214 }
Greg Claytona894fe72012-04-05 16:12:35 +0000215 return LLDB_INVALID_BREAK_ID;
Greg Clayton640dc6b2010-11-18 18:52:36 +0000216 }
Greg Claytona894fe72012-04-05 16:12:35 +0000217 break;
218
219 case eStopReasonWatchpoint:
220 return stop_info_sp->GetValue();
221
222 case eStopReasonSignal:
223 return stop_info_sp->GetValue();
224
225 case eStopReasonException:
226 return stop_info_sp->GetValue();
Greg Clayton640dc6b2010-11-18 18:52:36 +0000227 }
Greg Clayton640dc6b2010-11-18 18:52:36 +0000228 }
229 }
230 }
231 return 0;
232}
233
234size_t
Chris Lattner24943d22010-06-08 16:52:24 +0000235SBThread::GetStopDescription (char *dst, size_t dst_len)
236{
Greg Claytone005f2c2010-11-06 01:53:30 +0000237 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000238
Greg Claytona894fe72012-04-05 16:12:35 +0000239 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Claytonf4124de2012-02-21 00:09:25 +0000240 if (exe_ctx.HasThreadScope())
Chris Lattner24943d22010-06-08 16:52:24 +0000241 {
Greg Claytona894fe72012-04-05 16:12:35 +0000242 Process::StopLocker stop_locker;
243 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
Chris Lattner24943d22010-06-08 16:52:24 +0000244 {
Greg Claytona894fe72012-04-05 16:12:35 +0000245
246 Mutex::Locker api_locker (exe_ctx.GetTargetPtr()->GetAPIMutex());
247 StopInfoSP stop_info_sp = exe_ctx.GetThreadPtr()->GetStopInfo ();
248 if (stop_info_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000249 {
Greg Claytona894fe72012-04-05 16:12:35 +0000250 const char *stop_desc = stop_info_sp->GetDescription();
251 if (stop_desc)
Chris Lattner24943d22010-06-08 16:52:24 +0000252 {
Caroline Tice7826c882010-10-26 03:11:13 +0000253 if (log)
Greg Claytona894fe72012-04-05 16:12:35 +0000254 log->Printf ("SBThread(%p)::GetStopDescription (dst, dst_len) => \"%s\"",
Greg Claytonf4124de2012-02-21 00:09:25 +0000255 exe_ctx.GetThreadPtr(), stop_desc);
Chris Lattner24943d22010-06-08 16:52:24 +0000256 if (dst)
Greg Claytona894fe72012-04-05 16:12:35 +0000257 return ::snprintf (dst, dst_len, "%s", stop_desc);
258 else
259 {
260 // NULL dst passed in, return the length needed to contain the description
261 return ::strlen (stop_desc) + 1; // Include the NULL byte for size
262 }
263 }
264 else
265 {
266 size_t stop_desc_len = 0;
267 switch (stop_info_sp->GetStopReason())
268 {
269 case eStopReasonTrace:
270 case eStopReasonPlanComplete:
271 {
272 static char trace_desc[] = "step";
273 stop_desc = trace_desc;
274 stop_desc_len = sizeof(trace_desc); // Include the NULL byte for size
275 }
276 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000277
Greg Claytona894fe72012-04-05 16:12:35 +0000278 case eStopReasonBreakpoint:
279 {
280 static char bp_desc[] = "breakpoint hit";
281 stop_desc = bp_desc;
282 stop_desc_len = sizeof(bp_desc); // Include the NULL byte for size
283 }
284 break;
285
286 case eStopReasonWatchpoint:
287 {
288 static char wp_desc[] = "watchpoint hit";
289 stop_desc = wp_desc;
290 stop_desc_len = sizeof(wp_desc); // Include the NULL byte for size
291 }
292 break;
293
294 case eStopReasonSignal:
295 {
296 stop_desc = exe_ctx.GetProcessPtr()->GetUnixSignals ().GetSignalAsCString (stop_info_sp->GetValue());
297 if (stop_desc == NULL || stop_desc[0] == '\0')
298 {
299 static char signal_desc[] = "signal";
300 stop_desc = signal_desc;
301 stop_desc_len = sizeof(signal_desc); // Include the NULL byte for size
302 }
303 }
304 break;
305
306 case eStopReasonException:
307 {
308 char exc_desc[] = "exception";
309 stop_desc = exc_desc;
310 stop_desc_len = sizeof(exc_desc); // Include the NULL byte for size
311 }
312 break;
313
314 default:
315 break;
316 }
317
318 if (stop_desc && stop_desc[0])
319 {
320 if (log)
321 log->Printf ("SBThread(%p)::GetStopDescription (dst, dst_len) => '%s'",
322 exe_ctx.GetThreadPtr(), stop_desc);
323
324 if (dst)
325 return ::snprintf (dst, dst_len, "%s", stop_desc) + 1; // Include the NULL byte
326
327 if (stop_desc_len == 0)
328 stop_desc_len = ::strlen (stop_desc) + 1; // Include the NULL byte
329
330 return stop_desc_len;
331 }
Chris Lattner24943d22010-06-08 16:52:24 +0000332 }
333 }
334 }
335 }
336 if (dst)
337 *dst = 0;
338 return 0;
339}
340
Jim Ingham1586d972011-12-17 01:35:57 +0000341SBValue
342SBThread::GetStopReturnValue ()
343{
344 ValueObjectSP return_valobj_sp;
Greg Claytona894fe72012-04-05 16:12:35 +0000345 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Claytonf4124de2012-02-21 00:09:25 +0000346 if (exe_ctx.HasThreadScope())
Jim Ingham1586d972011-12-17 01:35:57 +0000347 {
Greg Claytona894fe72012-04-05 16:12:35 +0000348 Process::StopLocker stop_locker;
349 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
Jim Ingham1586d972011-12-17 01:35:57 +0000350 {
Greg Claytona894fe72012-04-05 16:12:35 +0000351 Mutex::Locker api_locker (exe_ctx.GetTargetPtr()->GetAPIMutex());
352 StopInfoSP stop_info_sp = exe_ctx.GetThreadPtr()->GetStopInfo ();
353 if (stop_info_sp)
354 {
355 return_valobj_sp = StopInfo::GetReturnValueObject (stop_info_sp);
356 }
Jim Ingham1586d972011-12-17 01:35:57 +0000357 }
358 }
359
360 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
361 if (log)
Greg Claytonf4124de2012-02-21 00:09:25 +0000362 log->Printf ("SBThread(%p)::GetStopReturnValue () => %s", exe_ctx.GetThreadPtr(),
Jim Ingham1586d972011-12-17 01:35:57 +0000363 return_valobj_sp.get()
364 ? return_valobj_sp->GetValueAsCString()
365 : "<no return value>");
366
367 return SBValue (return_valobj_sp);
368}
369
Chris Lattner24943d22010-06-08 16:52:24 +0000370void
371SBThread::SetThread (const ThreadSP& lldb_object_sp)
372{
Greg Claytona894fe72012-04-05 16:12:35 +0000373 m_opaque_sp->SetThreadSP (lldb_object_sp);
Chris Lattner24943d22010-06-08 16:52:24 +0000374}
375
376
377lldb::tid_t
378SBThread::GetThreadID () const
379{
Greg Claytona894fe72012-04-05 16:12:35 +0000380 ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
Greg Clayton90c52142012-01-30 02:53:15 +0000381 if (thread_sp)
Greg Claytonf4124de2012-02-21 00:09:25 +0000382 return thread_sp->GetID();
383 return LLDB_INVALID_THREAD_ID;
Chris Lattner24943d22010-06-08 16:52:24 +0000384}
385
386uint32_t
387SBThread::GetIndexID () const
388{
Greg Claytona894fe72012-04-05 16:12:35 +0000389 ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
Greg Clayton90c52142012-01-30 02:53:15 +0000390 if (thread_sp)
391 return thread_sp->GetIndexID();
Chris Lattner24943d22010-06-08 16:52:24 +0000392 return LLDB_INVALID_INDEX32;
393}
Greg Claytonf4124de2012-02-21 00:09:25 +0000394
Chris Lattner24943d22010-06-08 16:52:24 +0000395const char *
396SBThread::GetName () const
397{
Greg Claytona66ba462010-10-30 04:51:46 +0000398 const char *name = NULL;
Greg Claytona894fe72012-04-05 16:12:35 +0000399 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Claytonf4124de2012-02-21 00:09:25 +0000400 if (exe_ctx.HasThreadScope())
Greg Claytonbdcda462010-12-20 20:49:23 +0000401 {
Greg Claytona894fe72012-04-05 16:12:35 +0000402 Process::StopLocker stop_locker;
403 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
404 {
405 Mutex::Locker api_locker (exe_ctx.GetTargetPtr()->GetAPIMutex());
406 name = exe_ctx.GetThreadPtr()->GetName();
407 }
Greg Claytonbdcda462010-12-20 20:49:23 +0000408 }
Greg Claytona66ba462010-10-30 04:51:46 +0000409
Greg Claytone005f2c2010-11-06 01:53:30 +0000410 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000411 if (log)
Greg Claytonf4124de2012-02-21 00:09:25 +0000412 log->Printf ("SBThread(%p)::GetName () => %s", exe_ctx.GetThreadPtr(), name ? name : "NULL");
Caroline Tice7826c882010-10-26 03:11:13 +0000413
Greg Claytona66ba462010-10-30 04:51:46 +0000414 return name;
Chris Lattner24943d22010-06-08 16:52:24 +0000415}
416
417const char *
418SBThread::GetQueueName () const
419{
Greg Claytona66ba462010-10-30 04:51:46 +0000420 const char *name = NULL;
Greg Claytona894fe72012-04-05 16:12:35 +0000421 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Claytonf4124de2012-02-21 00:09:25 +0000422 if (exe_ctx.HasThreadScope())
Greg Claytonbdcda462010-12-20 20:49:23 +0000423 {
Greg Claytona894fe72012-04-05 16:12:35 +0000424 Process::StopLocker stop_locker;
425 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
426 {
427 Mutex::Locker api_locker (exe_ctx.GetTargetPtr()->GetAPIMutex());
428 name = exe_ctx.GetThreadPtr()->GetQueueName();
429 }
Greg Claytonbdcda462010-12-20 20:49:23 +0000430 }
Greg Claytona66ba462010-10-30 04:51:46 +0000431
Greg Claytone005f2c2010-11-06 01:53:30 +0000432 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000433 if (log)
Greg Claytonf4124de2012-02-21 00:09:25 +0000434 log->Printf ("SBThread(%p)::GetQueueName () => %s", exe_ctx.GetThreadPtr(), name ? name : "NULL");
Caroline Tice7826c882010-10-26 03:11:13 +0000435
Greg Claytona66ba462010-10-30 04:51:46 +0000436 return name;
Chris Lattner24943d22010-06-08 16:52:24 +0000437}
438
439
440void
Chris Lattner24943d22010-06-08 16:52:24 +0000441SBThread::StepOver (lldb::RunMode stop_other_threads)
442{
Greg Claytone005f2c2010-11-06 01:53:30 +0000443 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000444
Greg Claytona894fe72012-04-05 16:12:35 +0000445 ExecutionContext exe_ctx (m_opaque_sp.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000446
Greg Clayton90c52142012-01-30 02:53:15 +0000447 if (log)
Greg Claytonf4124de2012-02-21 00:09:25 +0000448 log->Printf ("SBThread(%p)::StepOver (stop_other_threads='%s')", exe_ctx.GetThreadPtr(),
Greg Clayton90c52142012-01-30 02:53:15 +0000449 Thread::RunModeAsCString (stop_other_threads));
450
Greg Claytonf4124de2012-02-21 00:09:25 +0000451 if (exe_ctx.HasThreadScope())
Chris Lattner24943d22010-06-08 16:52:24 +0000452 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000453 Mutex::Locker api_locker (exe_ctx.GetTargetPtr()->GetAPIMutex());
454 Thread *thread = exe_ctx.GetThreadPtr();
Chris Lattner24943d22010-06-08 16:52:24 +0000455 bool abort_other_plans = true;
Greg Claytonf4124de2012-02-21 00:09:25 +0000456 StackFrameSP frame_sp(thread->GetStackFrameAtIndex (0));
Chris Lattner24943d22010-06-08 16:52:24 +0000457
458 if (frame_sp)
459 {
460 if (frame_sp->HasDebugInformation ())
461 {
462 SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
Greg Claytonf4124de2012-02-21 00:09:25 +0000463 thread->QueueThreadPlanForStepRange (abort_other_plans,
Greg Clayton9af596d2012-03-28 17:12:37 +0000464 eStepTypeOver,
465 sc.line_entry.range,
466 sc,
467 stop_other_threads,
468 false);
Chris Lattner24943d22010-06-08 16:52:24 +0000469
470 }
471 else
472 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000473 thread->QueueThreadPlanForStepSingleInstruction (true,
Greg Clayton9af596d2012-03-28 17:12:37 +0000474 abort_other_plans,
475 stop_other_threads);
Chris Lattner24943d22010-06-08 16:52:24 +0000476 }
477 }
478
Greg Claytonf4124de2012-02-21 00:09:25 +0000479 Process *process = exe_ctx.GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +0000480 // Why do we need to set the current thread by ID here???
Greg Claytonf4124de2012-02-21 00:09:25 +0000481 process->GetThreadList().SetSelectedThreadByID (thread->GetID());
482 Error error (process->Resume());
Greg Clayton1a3083a2010-10-06 03:53:16 +0000483 if (error.Success())
484 {
485 // If we are doing synchronous mode, then wait for the
486 // process to stop yet again!
Greg Claytonf4124de2012-02-21 00:09:25 +0000487 if (process->GetTarget().GetDebugger().GetAsyncExecution () == false)
488 process->WaitForProcessToStop (NULL);
Greg Clayton1a3083a2010-10-06 03:53:16 +0000489 }
Chris Lattner24943d22010-06-08 16:52:24 +0000490 }
491}
492
493void
494SBThread::StepInto (lldb::RunMode stop_other_threads)
495{
Greg Claytone005f2c2010-11-06 01:53:30 +0000496 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000497
Greg Claytona894fe72012-04-05 16:12:35 +0000498 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Clayton90c52142012-01-30 02:53:15 +0000499
500 if (log)
Greg Claytonf4124de2012-02-21 00:09:25 +0000501 log->Printf ("SBThread(%p)::StepInto (stop_other_threads='%s')", exe_ctx.GetThreadPtr(),
Greg Clayton90c52142012-01-30 02:53:15 +0000502 Thread::RunModeAsCString (stop_other_threads));
Greg Claytonf4124de2012-02-21 00:09:25 +0000503 if (exe_ctx.HasThreadScope())
Chris Lattner24943d22010-06-08 16:52:24 +0000504 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000505 Mutex::Locker api_locker (exe_ctx.GetTargetPtr()->GetAPIMutex());
Chris Lattner24943d22010-06-08 16:52:24 +0000506 bool abort_other_plans = true;
507
Greg Claytonf4124de2012-02-21 00:09:25 +0000508 Thread *thread = exe_ctx.GetThreadPtr();
509 StackFrameSP frame_sp(thread->GetStackFrameAtIndex (0));
Chris Lattner24943d22010-06-08 16:52:24 +0000510
511 if (frame_sp && frame_sp->HasDebugInformation ())
512 {
Greg Clayton8f5fd6b2010-06-12 18:59:55 +0000513 bool avoid_code_without_debug_info = true;
Chris Lattner24943d22010-06-08 16:52:24 +0000514 SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
Greg Claytonf4124de2012-02-21 00:09:25 +0000515 thread->QueueThreadPlanForStepRange (abort_other_plans,
516 eStepTypeInto,
517 sc.line_entry.range,
518 sc,
519 stop_other_threads,
520 avoid_code_without_debug_info);
Chris Lattner24943d22010-06-08 16:52:24 +0000521 }
522 else
523 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000524 thread->QueueThreadPlanForStepSingleInstruction (false,
525 abort_other_plans,
526 stop_other_threads);
Chris Lattner24943d22010-06-08 16:52:24 +0000527 }
528
Greg Claytonf4124de2012-02-21 00:09:25 +0000529 Process *process = exe_ctx.GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +0000530 // Why do we need to set the current thread by ID here???
Greg Claytonf4124de2012-02-21 00:09:25 +0000531 process->GetThreadList().SetSelectedThreadByID (thread->GetID());
532 Error error (process->Resume());
Greg Clayton1a3083a2010-10-06 03:53:16 +0000533 if (error.Success())
534 {
535 // If we are doing synchronous mode, then wait for the
536 // process to stop yet again!
Greg Claytonf4124de2012-02-21 00:09:25 +0000537 if (process->GetTarget().GetDebugger().GetAsyncExecution () == false)
538 process->WaitForProcessToStop (NULL);
Greg Clayton1a3083a2010-10-06 03:53:16 +0000539 }
Chris Lattner24943d22010-06-08 16:52:24 +0000540 }
541}
542
543void
544SBThread::StepOut ()
545{
Greg Claytone005f2c2010-11-06 01:53:30 +0000546 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000547
Greg Claytona894fe72012-04-05 16:12:35 +0000548 ExecutionContext exe_ctx (m_opaque_sp.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000549
Greg Clayton90c52142012-01-30 02:53:15 +0000550 if (log)
Greg Claytonf4124de2012-02-21 00:09:25 +0000551 log->Printf ("SBThread(%p)::StepOut ()", exe_ctx.GetThreadPtr());
Greg Clayton90c52142012-01-30 02:53:15 +0000552
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 Mutex::Locker api_locker (exe_ctx.GetTargetPtr()->GetAPIMutex());
Chris Lattner24943d22010-06-08 16:52:24 +0000556 bool abort_other_plans = true;
557 bool stop_other_threads = true;
558
Greg Claytonf4124de2012-02-21 00:09:25 +0000559 Thread *thread = exe_ctx.GetThreadPtr();
560
561 thread->QueueThreadPlanForStepOut (abort_other_plans,
Greg Clayton90c52142012-01-30 02:53:15 +0000562 NULL,
563 false,
564 stop_other_threads,
565 eVoteYes,
566 eVoteNoOpinion,
567 0);
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000568
Greg Claytonf4124de2012-02-21 00:09:25 +0000569 Process *process = exe_ctx.GetProcessPtr();
570 process->GetThreadList().SetSelectedThreadByID (thread->GetID());
571 Error error (process->Resume());
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000572 if (error.Success())
573 {
574 // If we are doing synchronous mode, then wait for the
575 // process to stop yet again!
Greg Claytonf4124de2012-02-21 00:09:25 +0000576 if (process->GetTarget().GetDebugger().GetAsyncExecution () == false)
577 process->WaitForProcessToStop (NULL);
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000578 }
579 }
580}
Chris Lattner24943d22010-06-08 16:52:24 +0000581
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000582void
583SBThread::StepOutOfFrame (lldb::SBFrame &sb_frame)
584{
585 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
586
Greg Claytona894fe72012-04-05 16:12:35 +0000587 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Clayton334d33a2012-01-30 07:41:31 +0000588 StackFrameSP frame_sp (sb_frame.GetFrameSP());
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000589 if (log)
590 {
591 SBStream frame_desc_strm;
592 sb_frame.GetDescription (frame_desc_strm);
Greg Claytonf4124de2012-02-21 00:09:25 +0000593 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 +0000594 }
595
Greg Claytonf4124de2012-02-21 00:09:25 +0000596 if (exe_ctx.HasThreadScope())
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000597 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000598 Mutex::Locker api_locker (exe_ctx.GetTargetPtr()->GetAPIMutex());
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000599 bool abort_other_plans = true;
600 bool stop_other_threads = true;
Greg Claytonf4124de2012-02-21 00:09:25 +0000601 Thread *thread = exe_ctx.GetThreadPtr();
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000602
Greg Claytonf4124de2012-02-21 00:09:25 +0000603 thread->QueueThreadPlanForStepOut (abort_other_plans,
Greg Clayton90c52142012-01-30 02:53:15 +0000604 NULL,
605 false,
606 stop_other_threads,
607 eVoteYes,
608 eVoteNoOpinion,
Greg Clayton334d33a2012-01-30 07:41:31 +0000609 frame_sp->GetFrameIndex());
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000610
Greg Claytonf4124de2012-02-21 00:09:25 +0000611 Process *process = exe_ctx.GetProcessPtr();
612 process->GetThreadList().SetSelectedThreadByID (thread->GetID());
613 Error error (process->Resume());
Greg Clayton1a3083a2010-10-06 03:53:16 +0000614 if (error.Success())
615 {
616 // If we are doing synchronous mode, then wait for the
617 // process to stop yet again!
Greg Claytonf4124de2012-02-21 00:09:25 +0000618 if (process->GetTarget().GetDebugger().GetAsyncExecution () == false)
619 process->WaitForProcessToStop (NULL);
Greg Clayton1a3083a2010-10-06 03:53:16 +0000620 }
Chris Lattner24943d22010-06-08 16:52:24 +0000621 }
622}
623
624void
625SBThread::StepInstruction (bool step_over)
626{
Greg Claytone005f2c2010-11-06 01:53:30 +0000627 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000628
Greg Claytona894fe72012-04-05 16:12:35 +0000629 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Claytonf4124de2012-02-21 00:09:25 +0000630
Caroline Tice7826c882010-10-26 03:11:13 +0000631
Greg Clayton90c52142012-01-30 02:53:15 +0000632 if (log)
Greg Claytonf4124de2012-02-21 00:09:25 +0000633 log->Printf ("SBThread(%p)::StepInstruction (step_over=%i)", exe_ctx.GetThreadPtr(), step_over);
Greg Clayton90c52142012-01-30 02:53:15 +0000634
Greg Claytonf4124de2012-02-21 00:09:25 +0000635 if (exe_ctx.HasThreadScope())
Chris Lattner24943d22010-06-08 16:52:24 +0000636 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000637 Mutex::Locker api_locker (exe_ctx.GetTargetPtr()->GetAPIMutex());
638 Thread *thread = exe_ctx.GetThreadPtr();
639 Process *process = exe_ctx.GetProcessPtr();
640 thread->QueueThreadPlanForStepSingleInstruction (step_over, true, true);
641 process->GetThreadList().SetSelectedThreadByID (thread->GetID());
642 Error error (process->Resume());
Greg Clayton1a3083a2010-10-06 03:53:16 +0000643 if (error.Success())
644 {
645 // If we are doing synchronous mode, then wait for the
646 // process to stop yet again!
Greg Claytonf4124de2012-02-21 00:09:25 +0000647 if (process->GetTarget().GetDebugger().GetAsyncExecution () == false)
648 process->WaitForProcessToStop (NULL);
Greg Clayton1a3083a2010-10-06 03:53:16 +0000649 }
Chris Lattner24943d22010-06-08 16:52:24 +0000650 }
651}
652
653void
654SBThread::RunToAddress (lldb::addr_t addr)
655{
Greg Claytone005f2c2010-11-06 01:53:30 +0000656 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000657
Greg Claytona894fe72012-04-05 16:12:35 +0000658 ExecutionContext exe_ctx (m_opaque_sp.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000659
Greg Clayton90c52142012-01-30 02:53:15 +0000660 if (log)
Greg Claytonf4124de2012-02-21 00:09:25 +0000661 log->Printf ("SBThread(%p)::RunToAddress (addr=0x%llx)", exe_ctx.GetThreadPtr(), addr);
Greg Clayton90c52142012-01-30 02:53:15 +0000662
Greg Claytonf4124de2012-02-21 00:09:25 +0000663 if (exe_ctx.HasThreadScope())
Chris Lattner24943d22010-06-08 16:52:24 +0000664 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000665 Mutex::Locker api_locker (exe_ctx.GetTargetPtr()->GetAPIMutex());
Chris Lattner24943d22010-06-08 16:52:24 +0000666 bool abort_other_plans = true;
667 bool stop_other_threads = true;
668
Greg Clayton3508c382012-02-24 01:59:29 +0000669 Address target_addr (addr);
Chris Lattner24943d22010-06-08 16:52:24 +0000670
Greg Claytonf4124de2012-02-21 00:09:25 +0000671 Thread *thread = exe_ctx.GetThreadPtr();
672 Process *process = exe_ctx.GetProcessPtr();
673
674 thread->QueueThreadPlanForRunToAddress (abort_other_plans, target_addr, stop_other_threads);
675 process->GetThreadList().SetSelectedThreadByID (thread->GetID());
676 Error error (process->Resume());
Greg Clayton1a3083a2010-10-06 03:53:16 +0000677 if (error.Success())
678 {
679 // If we are doing synchronous mode, then wait for the
680 // process to stop yet again!
Greg Claytonf4124de2012-02-21 00:09:25 +0000681 if (process->GetTarget().GetDebugger().GetAsyncExecution () == false)
682 process->WaitForProcessToStop (NULL);
Greg Clayton1a3083a2010-10-06 03:53:16 +0000683 }
Chris Lattner24943d22010-06-08 16:52:24 +0000684 }
Chris Lattner24943d22010-06-08 16:52:24 +0000685}
686
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000687SBError
688SBThread::StepOverUntil (lldb::SBFrame &sb_frame,
689 lldb::SBFileSpec &sb_file_spec,
690 uint32_t line)
691{
692 SBError sb_error;
693 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
694 char path[PATH_MAX];
Greg Clayton90c52142012-01-30 02:53:15 +0000695
Greg Claytona894fe72012-04-05 16:12:35 +0000696 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Clayton334d33a2012-01-30 07:41:31 +0000697 StackFrameSP frame_sp (sb_frame.GetFrameSP());
698
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000699 if (log)
700 {
701 SBStream frame_desc_strm;
702 sb_frame.GetDescription (frame_desc_strm);
703 sb_file_spec->GetPath (path, sizeof(path));
704 log->Printf ("SBThread(%p)::StepOverUntil (frame = SBFrame(%p): %s, file+line = %s:%u)",
Greg Claytonf4124de2012-02-21 00:09:25 +0000705 exe_ctx.GetThreadPtr(),
Greg Clayton334d33a2012-01-30 07:41:31 +0000706 frame_sp.get(),
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000707 frame_desc_strm.GetData(),
708 path, line);
709 }
Greg Clayton90c52142012-01-30 02:53:15 +0000710
Greg Claytonf4124de2012-02-21 00:09:25 +0000711 if (exe_ctx.HasThreadScope())
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000712 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000713 Target *target = exe_ctx.GetTargetPtr();
714 Mutex::Locker api_locker (target->GetAPIMutex());
715 Thread *thread = exe_ctx.GetThreadPtr();
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000716
717 if (line == 0)
718 {
719 sb_error.SetErrorString("invalid line argument");
720 return sb_error;
721 }
722
723 StackFrameSP frame_sp;
Greg Clayton334d33a2012-01-30 07:41:31 +0000724 if (!frame_sp)
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000725 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000726 frame_sp = thread->GetSelectedFrame ();
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000727 if (!frame_sp)
Greg Claytonf4124de2012-02-21 00:09:25 +0000728 frame_sp = thread->GetStackFrameAtIndex (0);
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000729 }
730
731 SymbolContext frame_sc;
732 if (!frame_sp)
733 {
734 sb_error.SetErrorString("no valid frames in thread to step");
735 return sb_error;
736 }
737
738 // If we have a frame, get its line
739 frame_sc = frame_sp->GetSymbolContext (eSymbolContextCompUnit |
740 eSymbolContextFunction |
741 eSymbolContextLineEntry |
742 eSymbolContextSymbol );
743
744 if (frame_sc.comp_unit == NULL)
745 {
746 sb_error.SetErrorStringWithFormat("frame %u doesn't have debug information", frame_sp->GetFrameIndex());
747 return sb_error;
748 }
749
750 FileSpec step_file_spec;
751 if (sb_file_spec.IsValid())
752 {
753 // The file spec passed in was valid, so use it
754 step_file_spec = sb_file_spec.ref();
755 }
756 else
757 {
758 if (frame_sc.line_entry.IsValid())
759 step_file_spec = frame_sc.line_entry.file;
760 else
761 {
762 sb_error.SetErrorString("invalid file argument or no file for frame");
763 return sb_error;
764 }
765 }
766
Jim Inghamb07c62a2011-05-08 00:56:32 +0000767 // Grab the current function, then we will make sure the "until" address is
768 // within the function. We discard addresses that are out of the current
769 // function, and then if there are no addresses remaining, give an appropriate
770 // error message.
771
772 bool all_in_function = true;
773 AddressRange fun_range = frame_sc.function->GetAddressRange();
774
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000775 std::vector<addr_t> step_over_until_addrs;
776 const bool abort_other_plans = true;
777 const bool stop_other_threads = true;
778 const bool check_inlines = true;
779 const bool exact = false;
780
781 SymbolContextList sc_list;
Jim Inghamb07c62a2011-05-08 00:56:32 +0000782 const uint32_t num_matches = frame_sc.comp_unit->ResolveSymbolContext (step_file_spec,
783 line,
784 check_inlines,
785 exact,
786 eSymbolContextLineEntry,
787 sc_list);
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000788 if (num_matches > 0)
789 {
790 SymbolContext sc;
791 for (uint32_t i=0; i<num_matches; ++i)
792 {
793 if (sc_list.GetContextAtIndex(i, sc))
794 {
Jim Inghamb07c62a2011-05-08 00:56:32 +0000795 addr_t step_addr = sc.line_entry.range.GetBaseAddress().GetLoadAddress(target);
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000796 if (step_addr != LLDB_INVALID_ADDRESS)
797 {
Jim Inghamb07c62a2011-05-08 00:56:32 +0000798 if (fun_range.ContainsLoadAddress(step_addr, target))
799 step_over_until_addrs.push_back(step_addr);
800 else
801 all_in_function = false;
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000802 }
803 }
804 }
805 }
Jim Inghamb07c62a2011-05-08 00:56:32 +0000806
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000807 if (step_over_until_addrs.empty())
808 {
Jim Inghamb07c62a2011-05-08 00:56:32 +0000809 if (all_in_function)
810 {
811 step_file_spec.GetPath (path, sizeof(path));
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000812 sb_error.SetErrorStringWithFormat("No line entries for %s:%u", path, line);
Jim Inghamb07c62a2011-05-08 00:56:32 +0000813 }
814 else
Greg Clayton9c236732011-10-26 00:56:27 +0000815 sb_error.SetErrorString ("step until target not in current function");
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000816 }
817 else
818 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000819 thread->QueueThreadPlanForStepUntil (abort_other_plans,
820 &step_over_until_addrs[0],
821 step_over_until_addrs.size(),
822 stop_other_threads,
823 frame_sp->GetFrameIndex());
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000824
Greg Claytonf4124de2012-02-21 00:09:25 +0000825 Process *process = exe_ctx.GetProcessPtr();
826
827 process->GetThreadList().SetSelectedThreadByID (thread->GetID());
828 sb_error.ref() = process->Resume();
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000829 if (sb_error->Success())
830 {
831 // If we are doing synchronous mode, then wait for the
832 // process to stop yet again!
Greg Claytonf4124de2012-02-21 00:09:25 +0000833 if (process->GetTarget().GetDebugger().GetAsyncExecution () == false)
834 process->WaitForProcessToStop (NULL);
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000835 }
836 }
837 }
838 else
839 {
840 sb_error.SetErrorString("this SBThread object is invalid");
841 }
842 return sb_error;
843}
844
845
Greg Clayton123db402011-01-12 02:25:42 +0000846bool
847SBThread::Suspend()
848{
Greg Claytona894fe72012-04-05 16:12:35 +0000849 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Claytonf4124de2012-02-21 00:09:25 +0000850 if (exe_ctx.HasThreadScope())
Greg Clayton123db402011-01-12 02:25:42 +0000851 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000852 exe_ctx.GetThreadPtr()->SetResumeState (eStateSuspended);
Greg Clayton123db402011-01-12 02:25:42 +0000853 return true;
854 }
855 return false;
856}
857
858bool
859SBThread::Resume ()
860{
Greg Claytona894fe72012-04-05 16:12:35 +0000861 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Claytonf4124de2012-02-21 00:09:25 +0000862 if (exe_ctx.HasThreadScope())
Greg Clayton123db402011-01-12 02:25:42 +0000863 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000864 exe_ctx.GetThreadPtr()->SetResumeState (eStateRunning);
Greg Clayton123db402011-01-12 02:25:42 +0000865 return true;
866 }
867 return false;
868}
869
870bool
871SBThread::IsSuspended()
872{
Greg Claytona894fe72012-04-05 16:12:35 +0000873 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Claytonf4124de2012-02-21 00:09:25 +0000874 if (exe_ctx.HasThreadScope())
875 return exe_ctx.GetThreadPtr()->GetResumeState () == eStateSuspended;
Greg Clayton123db402011-01-12 02:25:42 +0000876 return false;
877}
878
Chris Lattner24943d22010-06-08 16:52:24 +0000879SBProcess
880SBThread::GetProcess ()
881{
Caroline Tice7826c882010-10-26 03:11:13 +0000882
Greg Clayton334d33a2012-01-30 07:41:31 +0000883 SBProcess sb_process;
884 ProcessSP process_sp;
Greg Claytona894fe72012-04-05 16:12:35 +0000885 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Claytonf4124de2012-02-21 00:09:25 +0000886 if (exe_ctx.HasThreadScope())
Chris Lattner24943d22010-06-08 16:52:24 +0000887 {
888 // Have to go up to the target so we can get a shared pointer to our process...
Greg Claytonf4124de2012-02-21 00:09:25 +0000889 sb_process.SetSP (exe_ctx.GetProcessSP());
Chris Lattner24943d22010-06-08 16:52:24 +0000890 }
Caroline Tice7826c882010-10-26 03:11:13 +0000891
Greg Claytone005f2c2010-11-06 01:53:30 +0000892 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000893 if (log)
894 {
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000895 SBStream frame_desc_strm;
Greg Clayton334d33a2012-01-30 07:41:31 +0000896 sb_process.GetDescription (frame_desc_strm);
Greg Claytonf4124de2012-02-21 00:09:25 +0000897 log->Printf ("SBThread(%p)::GetProcess () => SBProcess(%p): %s", exe_ctx.GetThreadPtr(),
Greg Clayton334d33a2012-01-30 07:41:31 +0000898 process_sp.get(), frame_desc_strm.GetData());
Caroline Tice7826c882010-10-26 03:11:13 +0000899 }
900
Greg Clayton334d33a2012-01-30 07:41:31 +0000901 return sb_process;
Chris Lattner24943d22010-06-08 16:52:24 +0000902}
903
904uint32_t
905SBThread::GetNumFrames ()
906{
Greg Claytone005f2c2010-11-06 01:53:30 +0000907 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000908
Caroline Tice7826c882010-10-26 03:11:13 +0000909 uint32_t num_frames = 0;
Greg Claytona894fe72012-04-05 16:12:35 +0000910 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Claytonf4124de2012-02-21 00:09:25 +0000911 if (exe_ctx.HasThreadScope())
Greg Claytonbdcda462010-12-20 20:49:23 +0000912 {
Greg Claytona894fe72012-04-05 16:12:35 +0000913 Process::StopLocker stop_locker;
914 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
915 {
916 Mutex::Locker api_locker (exe_ctx.GetTargetPtr()->GetAPIMutex());
917 num_frames = exe_ctx.GetThreadPtr()->GetStackFrameCount();
918 }
Greg Claytonbdcda462010-12-20 20:49:23 +0000919 }
Caroline Tice7826c882010-10-26 03:11:13 +0000920
921 if (log)
Greg Claytonf4124de2012-02-21 00:09:25 +0000922 log->Printf ("SBThread(%p)::GetNumFrames () => %u", exe_ctx.GetThreadPtr(), num_frames);
Caroline Tice7826c882010-10-26 03:11:13 +0000923
924 return num_frames;
Chris Lattner24943d22010-06-08 16:52:24 +0000925}
926
927SBFrame
928SBThread::GetFrameAtIndex (uint32_t idx)
929{
Greg Claytone005f2c2010-11-06 01:53:30 +0000930 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000931
Chris Lattner24943d22010-06-08 16:52:24 +0000932 SBFrame sb_frame;
Greg Clayton334d33a2012-01-30 07:41:31 +0000933 StackFrameSP frame_sp;
Greg Claytona894fe72012-04-05 16:12:35 +0000934 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Claytonf4124de2012-02-21 00:09:25 +0000935 if (exe_ctx.HasThreadScope())
Greg Claytonbdcda462010-12-20 20:49:23 +0000936 {
Greg Claytona894fe72012-04-05 16:12:35 +0000937 Process::StopLocker stop_locker;
938 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
939 {
940 Mutex::Locker api_locker (exe_ctx.GetTargetPtr()->GetAPIMutex());
941 frame_sp = exe_ctx.GetThreadPtr()->GetStackFrameAtIndex (idx);
942 sb_frame.SetFrameSP (frame_sp);
943 }
Greg Claytonbdcda462010-12-20 20:49:23 +0000944 }
Caroline Tice7826c882010-10-26 03:11:13 +0000945
946 if (log)
947 {
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000948 SBStream frame_desc_strm;
949 sb_frame.GetDescription (frame_desc_strm);
Greg Claytona66ba462010-10-30 04:51:46 +0000950 log->Printf ("SBThread(%p)::GetFrameAtIndex (idx=%d) => SBFrame(%p): %s",
Greg Claytonf4124de2012-02-21 00:09:25 +0000951 exe_ctx.GetThreadPtr(), idx, frame_sp.get(), frame_desc_strm.GetData());
Caroline Tice7826c882010-10-26 03:11:13 +0000952 }
953
Chris Lattner24943d22010-06-08 16:52:24 +0000954 return sb_frame;
955}
956
Greg Claytonc5157ec2010-12-17 02:26:24 +0000957lldb::SBFrame
958SBThread::GetSelectedFrame ()
959{
960 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
961
962 SBFrame sb_frame;
Greg Clayton334d33a2012-01-30 07:41:31 +0000963 StackFrameSP frame_sp;
Greg Claytona894fe72012-04-05 16:12:35 +0000964 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Claytonf4124de2012-02-21 00:09:25 +0000965 if (exe_ctx.HasThreadScope())
Greg Claytonbdcda462010-12-20 20:49:23 +0000966 {
Greg Claytona894fe72012-04-05 16:12:35 +0000967 Process::StopLocker stop_locker;
968 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
969 {
970 Mutex::Locker api_locker (exe_ctx.GetTargetPtr()->GetAPIMutex());
971 frame_sp = exe_ctx.GetThreadPtr()->GetSelectedFrame ();
972 sb_frame.SetFrameSP (frame_sp);
973 }
Greg Claytonbdcda462010-12-20 20:49:23 +0000974 }
Greg Claytonc5157ec2010-12-17 02:26:24 +0000975
976 if (log)
977 {
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000978 SBStream frame_desc_strm;
979 sb_frame.GetDescription (frame_desc_strm);
Greg Claytonc5157ec2010-12-17 02:26:24 +0000980 log->Printf ("SBThread(%p)::GetSelectedFrame () => SBFrame(%p): %s",
Greg Claytonf4124de2012-02-21 00:09:25 +0000981 exe_ctx.GetThreadPtr(), frame_sp.get(), frame_desc_strm.GetData());
Greg Claytonc5157ec2010-12-17 02:26:24 +0000982 }
983
984 return sb_frame;
985}
986
987lldb::SBFrame
988SBThread::SetSelectedFrame (uint32_t idx)
989{
990 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
991
992 SBFrame sb_frame;
Greg Clayton334d33a2012-01-30 07:41:31 +0000993 StackFrameSP frame_sp;
Greg Claytona894fe72012-04-05 16:12:35 +0000994 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Claytonf4124de2012-02-21 00:09:25 +0000995 if (exe_ctx.HasThreadScope())
Greg Claytonc5157ec2010-12-17 02:26:24 +0000996 {
Greg Claytona894fe72012-04-05 16:12:35 +0000997 Process::StopLocker stop_locker;
998 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
Greg Claytonc5157ec2010-12-17 02:26:24 +0000999 {
Greg Claytona894fe72012-04-05 16:12:35 +00001000 Mutex::Locker api_locker (exe_ctx.GetTargetPtr()->GetAPIMutex());
1001 Thread *thread = exe_ctx.GetThreadPtr();
1002 frame_sp = thread->GetStackFrameAtIndex (idx);
1003 if (frame_sp)
1004 {
1005 thread->SetSelectedFrame (frame_sp.get());
1006 sb_frame.SetFrameSP (frame_sp);
1007 }
Greg Claytonc5157ec2010-12-17 02:26:24 +00001008 }
1009 }
1010
1011 if (log)
1012 {
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001013 SBStream frame_desc_strm;
1014 sb_frame.GetDescription (frame_desc_strm);
Greg Claytonc5157ec2010-12-17 02:26:24 +00001015 log->Printf ("SBThread(%p)::SetSelectedFrame (idx=%u) => SBFrame(%p): %s",
Greg Claytonf4124de2012-02-21 00:09:25 +00001016 exe_ctx.GetThreadPtr(), idx, frame_sp.get(), frame_desc_strm.GetData());
Greg Claytonc5157ec2010-12-17 02:26:24 +00001017 }
1018 return sb_frame;
1019}
1020
1021
Chris Lattner24943d22010-06-08 16:52:24 +00001022bool
1023SBThread::operator == (const SBThread &rhs) const
1024{
Greg Claytona894fe72012-04-05 16:12:35 +00001025 return m_opaque_sp->GetThreadSP().get() == rhs.m_opaque_sp->GetThreadSP().get();
Chris Lattner24943d22010-06-08 16:52:24 +00001026}
1027
1028bool
1029SBThread::operator != (const SBThread &rhs) const
1030{
Greg Claytona894fe72012-04-05 16:12:35 +00001031 return m_opaque_sp->GetThreadSP().get() != rhs.m_opaque_sp->GetThreadSP().get();
Chris Lattner24943d22010-06-08 16:52:24 +00001032}
Caroline Tice98f930f2010-09-20 05:20:02 +00001033
1034bool
Caroline Tice7826c882010-10-26 03:11:13 +00001035SBThread::GetDescription (SBStream &description) const
1036{
Greg Clayton96154be2011-11-13 06:57:31 +00001037 Stream &strm = description.ref();
1038
Greg Claytona894fe72012-04-05 16:12:35 +00001039 ExecutionContext exe_ctx (m_opaque_sp.get());
Greg Claytonf4124de2012-02-21 00:09:25 +00001040 if (exe_ctx.HasThreadScope())
Caroline Tice7826c882010-10-26 03:11:13 +00001041 {
Greg Claytonf4124de2012-02-21 00:09:25 +00001042 strm.Printf("SBThread: tid = 0x%4.4llx", exe_ctx.GetThreadPtr()->GetID());
Caroline Tice7826c882010-10-26 03:11:13 +00001043 }
1044 else
Greg Clayton96154be2011-11-13 06:57:31 +00001045 strm.PutCString ("No value");
Caroline Tice7826c882010-10-26 03:11:13 +00001046
1047 return true;
1048}