blob: 24e5484c9cf969472312e50ea5b12825c9249e6d [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"
Greg Clayton63094e02010-06-23 01:19:29 +000014#include "lldb/Core/Debugger.h"
Chris Lattner24943d22010-06-08 16:52:24 +000015#include "lldb/Core/Stream.h"
16#include "lldb/Core/StreamFile.h"
Greg Clayton63094e02010-06-23 01:19:29 +000017#include "lldb/Interpreter/CommandInterpreter.h"
Chris Lattner24943d22010-06-08 16:52:24 +000018#include "lldb/Target/Thread.h"
19#include "lldb/Target/Process.h"
20#include "lldb/Symbol/SymbolContext.h"
21#include "lldb/Symbol/CompileUnit.h"
22#include "lldb/Target/Target.h"
23#include "lldb/Target/ThreadPlan.h"
Chris Lattner24943d22010-06-08 16:52:24 +000024#include "lldb/Target/ThreadPlanStepInstruction.h"
25#include "lldb/Target/ThreadPlanStepOut.h"
26#include "lldb/Target/ThreadPlanStepRange.h"
27#include "lldb/Target/ThreadPlanStepInRange.h"
28
29
Eli Friedman7a62c8b2010-06-09 07:44:37 +000030#include "lldb/API/SBAddress.h"
31#include "lldb/API/SBFrame.h"
32#include "lldb/API/SBSourceManager.h"
33#include "lldb/API/SBDebugger.h"
34#include "lldb/API/SBProcess.h"
Chris Lattner24943d22010-06-08 16:52:24 +000035
36using namespace lldb;
37using namespace lldb_private;
38
39SBThread::SBThread () :
Greg Clayton63094e02010-06-23 01:19:29 +000040 m_opaque_sp ()
Chris Lattner24943d22010-06-08 16:52:24 +000041{
42}
43
44//----------------------------------------------------------------------
45// Thread constructor
46//----------------------------------------------------------------------
47SBThread::SBThread (const ThreadSP& lldb_object_sp) :
Greg Clayton63094e02010-06-23 01:19:29 +000048 m_opaque_sp (lldb_object_sp)
Chris Lattner24943d22010-06-08 16:52:24 +000049{
50}
51
52SBThread::SBThread (const SBThread &rhs)
53{
Greg Clayton63094e02010-06-23 01:19:29 +000054 m_opaque_sp = rhs.m_opaque_sp;
Chris Lattner24943d22010-06-08 16:52:24 +000055}
56
57//----------------------------------------------------------------------
58// Destructor
59//----------------------------------------------------------------------
60SBThread::~SBThread()
61{
62}
63
64bool
65SBThread::IsValid() const
66{
Greg Clayton63094e02010-06-23 01:19:29 +000067 return m_opaque_sp != NULL;
Chris Lattner24943d22010-06-08 16:52:24 +000068}
69
70StopReason
71SBThread::GetStopReason()
72{
Greg Clayton63094e02010-06-23 01:19:29 +000073 if (m_opaque_sp)
Chris Lattner24943d22010-06-08 16:52:24 +000074 {
75 lldb_private::Thread::StopInfo thread_stop_info;
Greg Clayton63094e02010-06-23 01:19:29 +000076 if (m_opaque_sp->GetStopInfo(&thread_stop_info))
Chris Lattner24943d22010-06-08 16:52:24 +000077 return thread_stop_info.GetStopReason();
78 }
79 return eStopReasonInvalid;
80}
81
82size_t
83SBThread::GetStopDescription (char *dst, size_t dst_len)
84{
Greg Clayton63094e02010-06-23 01:19:29 +000085 if (m_opaque_sp)
Chris Lattner24943d22010-06-08 16:52:24 +000086 {
87 lldb_private::Thread::StopInfo thread_stop_info;
Greg Clayton63094e02010-06-23 01:19:29 +000088 if (m_opaque_sp->GetStopInfo(&thread_stop_info))
Chris Lattner24943d22010-06-08 16:52:24 +000089 {
90 const char *stop_desc = thread_stop_info.GetStopDescription();
91 if (stop_desc)
92 {
93 if (dst)
94 return ::snprintf (dst, dst_len, "%s", stop_desc);
95 else
96 {
97 // NULL dst passed in, return the length needed to contain the description
98 return ::strlen (stop_desc) + 1; // Include the NULL byte for size
99 }
100 }
101 else
102 {
103 const char *stop_desc = NULL;
104 size_t stop_desc_len = 0;
105 switch (thread_stop_info.GetStopReason())
106 {
107 case eStopReasonTrace:
108 case eStopReasonPlanComplete:
109 {
110 static char trace_desc[] = "step";
111 stop_desc = trace_desc;
112 stop_desc_len = sizeof(trace_desc); // Include the NULL byte for size
113 }
114 break;
115
116 case eStopReasonBreakpoint:
117 {
118 static char bp_desc[] = "breakpoint hit";
119 stop_desc = bp_desc;
120 stop_desc_len = sizeof(bp_desc); // Include the NULL byte for size
121 }
122 break;
123
124 case eStopReasonWatchpoint:
125 {
126 static char wp_desc[] = "watchpoint hit";
127 stop_desc = wp_desc;
128 stop_desc_len = sizeof(wp_desc); // Include the NULL byte for size
129 }
130 break;
131
132 case eStopReasonSignal:
133 {
Greg Clayton63094e02010-06-23 01:19:29 +0000134 stop_desc = m_opaque_sp->GetProcess().GetUnixSignals ().GetSignalAsCString (thread_stop_info.GetSignal());
Chris Lattner24943d22010-06-08 16:52:24 +0000135 if (stop_desc == NULL || stop_desc[0] == '\0')
136 {
137 static char signal_desc[] = "signal";
138 stop_desc = signal_desc;
139 stop_desc_len = sizeof(signal_desc); // Include the NULL byte for size
140 }
141 }
142 break;
143
144 case eStopReasonException:
145 {
146 char exc_desc[] = "exception";
147 stop_desc = exc_desc;
148 stop_desc_len = sizeof(exc_desc); // Include the NULL byte for size
149 }
150 break;
151 }
152
153 if (stop_desc && stop_desc[0])
154 {
155 if (dst)
156 return ::snprintf (dst, dst_len, "%s", stop_desc) + 1; // Include the NULL byte
157
158 if (stop_desc_len == 0)
159 stop_desc_len = ::strlen (stop_desc) + 1; // Include the NULL byte
160
161 return stop_desc_len;
162 }
163 }
164 }
165 }
166 if (dst)
167 *dst = 0;
168 return 0;
169}
170
171void
172SBThread::SetThread (const ThreadSP& lldb_object_sp)
173{
Greg Clayton63094e02010-06-23 01:19:29 +0000174 m_opaque_sp = lldb_object_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000175}
176
177
178lldb::tid_t
179SBThread::GetThreadID () const
180{
Greg Clayton63094e02010-06-23 01:19:29 +0000181 if (m_opaque_sp)
182 return m_opaque_sp->GetID();
Chris Lattner24943d22010-06-08 16:52:24 +0000183 else
184 return LLDB_INVALID_THREAD_ID;
185}
186
187uint32_t
188SBThread::GetIndexID () const
189{
Greg Clayton63094e02010-06-23 01:19:29 +0000190 if (m_opaque_sp)
191 return m_opaque_sp->GetIndexID();
Chris Lattner24943d22010-06-08 16:52:24 +0000192 return LLDB_INVALID_INDEX32;
193}
194const char *
195SBThread::GetName () const
196{
Greg Clayton63094e02010-06-23 01:19:29 +0000197 if (m_opaque_sp)
198 return m_opaque_sp->GetName();
Chris Lattner24943d22010-06-08 16:52:24 +0000199 return NULL;
200}
201
202const char *
203SBThread::GetQueueName () const
204{
Greg Clayton63094e02010-06-23 01:19:29 +0000205 if (m_opaque_sp)
206 return m_opaque_sp->GetQueueName();
Chris Lattner24943d22010-06-08 16:52:24 +0000207 return NULL;
208}
209
210
211void
212SBThread::DisplayFramesForCurrentContext (FILE *out,
213 FILE *err,
214 uint32_t first_frame,
215 uint32_t num_frames,
216 bool show_frame_info,
217 uint32_t num_frames_with_source,
218 uint32_t source_lines_before,
219 uint32_t source_lines_after)
220{
221 if ((out == NULL) || (err == NULL))
222 return;
223
Greg Clayton63094e02010-06-23 01:19:29 +0000224 if (m_opaque_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000225 {
Greg Clayton63094e02010-06-23 01:19:29 +0000226 uint32_t num_stack_frames = m_opaque_sp->GetStackFrameCount ();
Chris Lattner24943d22010-06-08 16:52:24 +0000227 StackFrameSP frame_sp;
228 int frame_idx = 0;
229
230 for (frame_idx = first_frame; frame_idx < first_frame + num_frames; ++frame_idx)
231 {
232 if (frame_idx >= num_stack_frames)
233 break;
234
Greg Clayton63094e02010-06-23 01:19:29 +0000235 frame_sp = m_opaque_sp->GetStackFrameAtIndex (frame_idx);
Chris Lattner24943d22010-06-08 16:52:24 +0000236 if (!frame_sp)
237 break;
238
239 SBFrame sb_frame (frame_sp);
240 if (DisplaySingleFrameForCurrentContext (out,
241 err,
242 sb_frame,
243 show_frame_info,
244 num_frames_with_source > first_frame - frame_idx,
245 source_lines_before,
246 source_lines_after) == false)
247 break;
248 }
249 }
250}
251
252bool
253SBThread::DisplaySingleFrameForCurrentContext (FILE *out,
254 FILE *err,
255 SBFrame &frame,
256 bool show_frame_info,
257 bool show_source,
258 uint32_t source_lines_after,
259 uint32_t source_lines_before)
260{
261 bool success = false;
Greg Clayton63094e02010-06-23 01:19:29 +0000262
263 if ((out == NULL) || (err == NULL))
Chris Lattner24943d22010-06-08 16:52:24 +0000264 return false;
Greg Clayton63094e02010-06-23 01:19:29 +0000265
266 if (m_opaque_sp && frame.IsValid())
Chris Lattner24943d22010-06-08 16:52:24 +0000267 {
Chris Lattner24943d22010-06-08 16:52:24 +0000268 StreamFile str (out);
Greg Clayton63094e02010-06-23 01:19:29 +0000269
Chris Lattner24943d22010-06-08 16:52:24 +0000270 SBSymbolContext sc(frame.GetSymbolContext(eSymbolContextEverything));
Greg Clayton63094e02010-06-23 01:19:29 +0000271
Chris Lattner24943d22010-06-08 16:52:24 +0000272 if (show_frame_info && sc.IsValid())
273 {
274 user_id_t frame_idx = (user_id_t) frame.GetFrameID();
275 lldb::addr_t pc = frame.GetPC();
276 ::fprintf (out,
277 " frame #%u: tid = 0x%4.4x, pc = 0x%llx ",
278 frame_idx,
279 GetThreadID(),
280 pc);
Greg Clayton63094e02010-06-23 01:19:29 +0000281 sc->DumpStopContext (&str, &m_opaque_sp->GetProcess(), *frame.GetPCAddress());
Chris Lattner24943d22010-06-08 16:52:24 +0000282 fprintf (out, "\n");
283 success = true;
284 }
Greg Clayton63094e02010-06-23 01:19:29 +0000285
Chris Lattner24943d22010-06-08 16:52:24 +0000286 SBCompileUnit comp_unit(sc.GetCompileUnit());
287 if (show_source && comp_unit.IsValid())
288 {
Greg Clayton63094e02010-06-23 01:19:29 +0000289 success = false;
Chris Lattner24943d22010-06-08 16:52:24 +0000290 SBLineEntry line_entry;
291 if (line_entry.IsValid())
292 {
Greg Clayton63094e02010-06-23 01:19:29 +0000293 SourceManager& source_manager = m_opaque_sp->GetProcess().GetTarget().GetDebugger().GetSourceManager();
294 SBFileSpec line_entry_file_spec (line_entry.GetFileSpec());
295
Chris Lattner24943d22010-06-08 16:52:24 +0000296 if (line_entry_file_spec.IsValid())
297 {
Greg Clayton63094e02010-06-23 01:19:29 +0000298 source_manager.DisplaySourceLinesWithLineNumbers (line_entry_file_spec.ref(),
Chris Lattner24943d22010-06-08 16:52:24 +0000299 line_entry.GetLine(),
300 source_lines_after,
301 source_lines_before, "->",
Greg Clayton63094e02010-06-23 01:19:29 +0000302 &str);
Chris Lattner24943d22010-06-08 16:52:24 +0000303 success = true;
304 }
305 }
306 }
307 }
308 return success;
309}
310
311void
312SBThread::StepOver (lldb::RunMode stop_other_threads)
313{
Greg Clayton63094e02010-06-23 01:19:29 +0000314 if (m_opaque_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000315 {
316 bool abort_other_plans = true;
Greg Clayton63094e02010-06-23 01:19:29 +0000317 StackFrameSP frame_sp(m_opaque_sp->GetStackFrameAtIndex (0));
Chris Lattner24943d22010-06-08 16:52:24 +0000318
319 if (frame_sp)
320 {
321 if (frame_sp->HasDebugInformation ())
322 {
323 SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
Greg Clayton63094e02010-06-23 01:19:29 +0000324 m_opaque_sp->QueueThreadPlanForStepRange (abort_other_plans,
Chris Lattner24943d22010-06-08 16:52:24 +0000325 eStepTypeOver,
326 sc.line_entry.range,
327 sc,
Greg Clayton8f5fd6b2010-06-12 18:59:55 +0000328 stop_other_threads,
329 false);
Chris Lattner24943d22010-06-08 16:52:24 +0000330
331 }
332 else
333 {
Greg Clayton63094e02010-06-23 01:19:29 +0000334 m_opaque_sp->QueueThreadPlanForStepSingleInstruction (true,
Chris Lattner24943d22010-06-08 16:52:24 +0000335 abort_other_plans,
336 stop_other_threads);
337 }
338 }
339
Greg Clayton63094e02010-06-23 01:19:29 +0000340 Process &process = m_opaque_sp->GetProcess();
Chris Lattner24943d22010-06-08 16:52:24 +0000341 // Why do we need to set the current thread by ID here???
Greg Clayton63094e02010-06-23 01:19:29 +0000342 process.GetThreadList().SetCurrentThreadByID (m_opaque_sp->GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000343 process.Resume();
344 }
345}
346
347void
348SBThread::StepInto (lldb::RunMode stop_other_threads)
349{
Greg Clayton63094e02010-06-23 01:19:29 +0000350 if (m_opaque_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000351 {
352 bool abort_other_plans = true;
353
Greg Clayton63094e02010-06-23 01:19:29 +0000354 StackFrameSP frame_sp(m_opaque_sp->GetStackFrameAtIndex (0));
Chris Lattner24943d22010-06-08 16:52:24 +0000355
356 if (frame_sp && frame_sp->HasDebugInformation ())
357 {
Greg Clayton8f5fd6b2010-06-12 18:59:55 +0000358 bool avoid_code_without_debug_info = true;
Chris Lattner24943d22010-06-08 16:52:24 +0000359 SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
Greg Clayton63094e02010-06-23 01:19:29 +0000360 m_opaque_sp->QueueThreadPlanForStepRange (abort_other_plans,
Greg Clayton8f5fd6b2010-06-12 18:59:55 +0000361 eStepTypeInto,
362 sc.line_entry.range,
363 sc,
364 stop_other_threads,
365 avoid_code_without_debug_info);
Chris Lattner24943d22010-06-08 16:52:24 +0000366 }
367 else
368 {
Greg Clayton63094e02010-06-23 01:19:29 +0000369 m_opaque_sp->QueueThreadPlanForStepSingleInstruction (false,
Chris Lattner24943d22010-06-08 16:52:24 +0000370 abort_other_plans,
371 stop_other_threads);
372 }
373
Greg Clayton63094e02010-06-23 01:19:29 +0000374 Process &process = m_opaque_sp->GetProcess();
Chris Lattner24943d22010-06-08 16:52:24 +0000375 // Why do we need to set the current thread by ID here???
Greg Clayton63094e02010-06-23 01:19:29 +0000376 process.GetThreadList().SetCurrentThreadByID (m_opaque_sp->GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000377 process.Resume();
378
379 }
380}
381
382void
383SBThread::StepOut ()
384{
Greg Clayton63094e02010-06-23 01:19:29 +0000385 if (m_opaque_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000386 {
387 bool abort_other_plans = true;
388 bool stop_other_threads = true;
389
Greg Clayton63094e02010-06-23 01:19:29 +0000390 m_opaque_sp->QueueThreadPlanForStepOut (abort_other_plans, NULL, false, stop_other_threads, eVoteYes, eVoteNoOpinion);
Chris Lattner24943d22010-06-08 16:52:24 +0000391
Greg Clayton63094e02010-06-23 01:19:29 +0000392 Process &process = m_opaque_sp->GetProcess();
393 process.GetThreadList().SetCurrentThreadByID (m_opaque_sp->GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000394 process.Resume();
395 }
396}
397
398void
399SBThread::StepInstruction (bool step_over)
400{
Greg Clayton63094e02010-06-23 01:19:29 +0000401 if (m_opaque_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000402 {
Greg Clayton63094e02010-06-23 01:19:29 +0000403 m_opaque_sp->QueueThreadPlanForStepSingleInstruction (step_over, true, true);
404 Process &process = m_opaque_sp->GetProcess();
405 process.GetThreadList().SetCurrentThreadByID (m_opaque_sp->GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000406 process.Resume();
407 }
408}
409
410void
411SBThread::RunToAddress (lldb::addr_t addr)
412{
Greg Clayton63094e02010-06-23 01:19:29 +0000413 if (m_opaque_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000414 {
415 bool abort_other_plans = true;
416 bool stop_other_threads = true;
417
418 Address target_addr (NULL, addr);
419
Greg Clayton63094e02010-06-23 01:19:29 +0000420 m_opaque_sp->QueueThreadPlanForRunToAddress (abort_other_plans, target_addr, stop_other_threads);
421 Process &process = m_opaque_sp->GetProcess();
422 process.GetThreadList().SetCurrentThreadByID (m_opaque_sp->GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000423 process.Resume();
424 }
425
426}
427
Chris Lattner24943d22010-06-08 16:52:24 +0000428SBProcess
429SBThread::GetProcess ()
430{
431 SBProcess process;
Greg Clayton63094e02010-06-23 01:19:29 +0000432 if (m_opaque_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000433 {
434 // Have to go up to the target so we can get a shared pointer to our process...
Greg Clayton63094e02010-06-23 01:19:29 +0000435 process.SetProcess(m_opaque_sp->GetProcess().GetTarget().GetProcessSP());
Chris Lattner24943d22010-06-08 16:52:24 +0000436 }
437 return process;
438}
439
440uint32_t
441SBThread::GetNumFrames ()
442{
Greg Clayton63094e02010-06-23 01:19:29 +0000443 if (m_opaque_sp)
444 return m_opaque_sp->GetStackFrameCount();
Chris Lattner24943d22010-06-08 16:52:24 +0000445 return 0;
446}
447
448SBFrame
449SBThread::GetFrameAtIndex (uint32_t idx)
450{
451 SBFrame sb_frame;
Greg Clayton63094e02010-06-23 01:19:29 +0000452 if (m_opaque_sp)
453 sb_frame.SetFrame (m_opaque_sp->GetStackFrameAtIndex (idx));
Chris Lattner24943d22010-06-08 16:52:24 +0000454 return sb_frame;
455}
456
457const lldb::SBThread &
458SBThread::operator = (const lldb::SBThread &rhs)
459{
Greg Clayton63094e02010-06-23 01:19:29 +0000460 m_opaque_sp = rhs.m_opaque_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000461 return *this;
462}
463
464bool
465SBThread::operator == (const SBThread &rhs) const
466{
Greg Clayton63094e02010-06-23 01:19:29 +0000467 return m_opaque_sp.get() == rhs.m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000468}
469
470bool
471SBThread::operator != (const SBThread &rhs) const
472{
Greg Clayton63094e02010-06-23 01:19:29 +0000473 return m_opaque_sp.get() != rhs.m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000474}
475
476lldb_private::Thread *
477SBThread::GetLLDBObjectPtr ()
478{
Greg Clayton63094e02010-06-23 01:19:29 +0000479 return m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000480}
481
482const lldb_private::Thread *
483SBThread::operator->() const
484{
Greg Clayton63094e02010-06-23 01:19:29 +0000485 return m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000486}
487
488const lldb_private::Thread &
489SBThread::operator*() const
490{
Greg Clayton63094e02010-06-23 01:19:29 +0000491 return *m_opaque_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000492}
493
494lldb_private::Thread *
495SBThread::operator->()
496{
Greg Clayton63094e02010-06-23 01:19:29 +0000497 return m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000498}
499
500lldb_private::Thread &
501SBThread::operator*()
502{
Greg Clayton63094e02010-06-23 01:19:29 +0000503 return *m_opaque_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000504}