blob: eb40cae9b6bda87802c15249904324415e5337d1 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- StackFrameList.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
Greg Clayton782b9cc2010-08-25 00:35:26 +000010#include "lldb/Target/StackFrameList.h"
11
Chris Lattner24943d22010-06-08 16:52:24 +000012// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
Greg Clayton1d66ef52010-08-27 18:24:16 +000016#include "lldb/Core/StreamFile.h"
Jim Inghamfdf24ef2011-09-08 22:13:49 +000017#include "lldb/Core/SourceManager.h"
Greg Clayton782b9cc2010-08-25 00:35:26 +000018#include "lldb/Symbol/Block.h"
19#include "lldb/Symbol/Function.h"
Greg Clayton4fb08152010-08-30 18:11:35 +000020#include "lldb/Symbol/Symbol.h"
Jim Inghamfdf24ef2011-09-08 22:13:49 +000021#include "lldb/Target/Process.h"
Greg Clayton782b9cc2010-08-25 00:35:26 +000022#include "lldb/Target/RegisterContext.h"
Chris Lattner24943d22010-06-08 16:52:24 +000023#include "lldb/Target/StackFrame.h"
Jim Inghamfdf24ef2011-09-08 22:13:49 +000024#include "lldb/Target/Target.h"
Greg Clayton782b9cc2010-08-25 00:35:26 +000025#include "lldb/Target/Thread.h"
26#include "lldb/Target/Unwind.h"
Chris Lattner24943d22010-06-08 16:52:24 +000027
Greg Clayton1d66ef52010-08-27 18:24:16 +000028//#define DEBUG_STACK_FRAMES 1
29
Chris Lattner24943d22010-06-08 16:52:24 +000030using namespace lldb;
31using namespace lldb_private;
32
33//----------------------------------------------------------------------
34// StackFrameList constructor
35//----------------------------------------------------------------------
Greg Clayton5205f0b2010-09-03 17:10:42 +000036StackFrameList::StackFrameList
37(
38 Thread &thread,
39 const lldb::StackFrameListSP &prev_frames_sp,
40 bool show_inline_frames
41) :
Greg Clayton782b9cc2010-08-25 00:35:26 +000042 m_thread (thread),
Greg Clayton5205f0b2010-09-03 17:10:42 +000043 m_prev_frames_sp (prev_frames_sp),
Chris Lattner24943d22010-06-08 16:52:24 +000044 m_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton1d66ef52010-08-27 18:24:16 +000045 m_frames (),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +000046 m_selected_frame_idx (0),
47 m_show_inlined_frames (show_inline_frames)
Chris Lattner24943d22010-06-08 16:52:24 +000048{
49}
50
51//----------------------------------------------------------------------
52// Destructor
53//----------------------------------------------------------------------
54StackFrameList::~StackFrameList()
55{
56}
57
58
59uint32_t
Greg Clayton5205f0b2010-09-03 17:10:42 +000060StackFrameList::GetNumFrames (bool can_create)
Chris Lattner24943d22010-06-08 16:52:24 +000061{
62 Mutex::Locker locker (m_mutex);
Greg Clayton782b9cc2010-08-25 00:35:26 +000063
Greg Clayton5205f0b2010-09-03 17:10:42 +000064 if (can_create && m_frames.size() <= 1)
Greg Clayton782b9cc2010-08-25 00:35:26 +000065 {
Greg Clayton1d66ef52010-08-27 18:24:16 +000066 if (m_show_inlined_frames)
Greg Clayton782b9cc2010-08-25 00:35:26 +000067 {
Greg Clayton1d66ef52010-08-27 18:24:16 +000068#if defined (DEBUG_STACK_FRAMES)
Johnny Chen0a77f902011-08-25 17:27:02 +000069 StreamFile s(stdout, false);
Greg Clayton1d66ef52010-08-27 18:24:16 +000070#endif
Greg Clayton782b9cc2010-08-25 00:35:26 +000071 Unwind *unwinder = m_thread.GetUnwinder ();
Greg Clayton4fb08152010-08-30 18:11:35 +000072 addr_t pc = LLDB_INVALID_ADDRESS;
73 addr_t cfa = LLDB_INVALID_ADDRESS;
Greg Clayton1d66ef52010-08-27 18:24:16 +000074
Greg Clayton782b9cc2010-08-25 00:35:26 +000075 // If we are going to show inlined stack frames as actual frames,
76 // we need to calculate all concrete frames first, then iterate
77 // through all of them and count up how many inlined functions are
Greg Clayton1d66ef52010-08-27 18:24:16 +000078 // in each frame.
79 const uint32_t unwind_frame_count = unwinder->GetFrameCount();
Greg Clayton782b9cc2010-08-25 00:35:26 +000080
Greg Clayton1d66ef52010-08-27 18:24:16 +000081 StackFrameSP unwind_frame_sp;
82 for (uint32_t idx=0; idx<unwind_frame_count; ++idx)
Greg Clayton782b9cc2010-08-25 00:35:26 +000083 {
84 if (idx == 0)
85 {
Greg Clayton1d66ef52010-08-27 18:24:16 +000086 // We might have already created frame zero, only create it
87 // if we need to
88 if (m_frames.empty())
89 {
Greg Clayton4fb08152010-08-30 18:11:35 +000090 cfa = m_thread.m_reg_context_sp->GetSP();
Greg Clayton1d66ef52010-08-27 18:24:16 +000091 m_thread.GetRegisterContext();
92 unwind_frame_sp.reset (new StackFrame (m_frames.size(),
93 idx,
94 m_thread,
95 m_thread.m_reg_context_sp,
Greg Clayton4fb08152010-08-30 18:11:35 +000096 cfa,
Greg Clayton1d66ef52010-08-27 18:24:16 +000097 m_thread.m_reg_context_sp->GetPC(),
98 NULL));
99 m_frames.push_back (unwind_frame_sp);
100 }
101 else
102 {
103 unwind_frame_sp = m_frames.front();
Greg Clayton4fb08152010-08-30 18:11:35 +0000104 cfa = unwind_frame_sp->m_id.GetCallFrameAddress();
Greg Clayton1d66ef52010-08-27 18:24:16 +0000105 }
Greg Clayton782b9cc2010-08-25 00:35:26 +0000106 }
107 else
108 {
109 const bool success = unwinder->GetFrameInfoAtIndex(idx, cfa, pc);
110 assert (success);
Greg Clayton1d66ef52010-08-27 18:24:16 +0000111 unwind_frame_sp.reset (new StackFrame (m_frames.size(), idx, m_thread, cfa, pc, NULL));
112 m_frames.push_back (unwind_frame_sp);
Greg Clayton782b9cc2010-08-25 00:35:26 +0000113 }
Greg Clayton782b9cc2010-08-25 00:35:26 +0000114
Greg Clayton2f57db02011-10-01 00:45:15 +0000115 SymbolContext unwind_sc = unwind_frame_sp->GetSymbolContext (eSymbolContextBlock | eSymbolContextFunction);
116 Block *unwind_block = unwind_sc.block;
Greg Clayton4fb08152010-08-30 18:11:35 +0000117 if (unwind_block)
Greg Clayton782b9cc2010-08-25 00:35:26 +0000118 {
Greg Clayton2f57db02011-10-01 00:45:15 +0000119 Address curr_frame_address = unwind_frame_sp->GetFrameCodeAddress();
120 SymbolContext next_frame_sc;
121 Address next_frame_address;
122
123 while (unwind_sc.GetParentOfInlinedScope(curr_frame_address, next_frame_sc, next_frame_address))
Greg Clayton782b9cc2010-08-25 00:35:26 +0000124 {
Greg Clayton4fb08152010-08-30 18:11:35 +0000125 StackFrameSP frame_sp(new StackFrame (m_frames.size(),
126 idx,
127 m_thread,
128 unwind_frame_sp->GetRegisterContextSP (),
129 cfa,
Greg Clayton2f57db02011-10-01 00:45:15 +0000130 next_frame_address,
131 &next_frame_sc));
132
Greg Clayton4fb08152010-08-30 18:11:35 +0000133 m_frames.push_back (frame_sp);
Greg Clayton2f57db02011-10-01 00:45:15 +0000134 unwind_sc = next_frame_sc;
135 curr_frame_address = next_frame_address;
136
Greg Clayton782b9cc2010-08-25 00:35:26 +0000137 }
138 }
139 }
Greg Clayton5205f0b2010-09-03 17:10:42 +0000140
141 if (m_prev_frames_sp)
Greg Clayton1d66ef52010-08-27 18:24:16 +0000142 {
Greg Clayton5205f0b2010-09-03 17:10:42 +0000143 StackFrameList *prev_frames = m_prev_frames_sp.get();
Greg Clayton1d66ef52010-08-27 18:24:16 +0000144 StackFrameList *curr_frames = this;
145
146#if defined (DEBUG_STACK_FRAMES)
Greg Clayton870a1cd2010-08-27 21:47:54 +0000147 s.PutCString("\nprev_frames:\n");
Greg Clayton1d66ef52010-08-27 18:24:16 +0000148 prev_frames->Dump (&s);
Greg Clayton870a1cd2010-08-27 21:47:54 +0000149 s.PutCString("\ncurr_frames:\n");
Greg Clayton1d66ef52010-08-27 18:24:16 +0000150 curr_frames->Dump (&s);
151 s.EOL();
152#endif
153 size_t curr_frame_num, prev_frame_num;
154
155 for (curr_frame_num = curr_frames->m_frames.size(), prev_frame_num = prev_frames->m_frames.size();
156 curr_frame_num > 0 && prev_frame_num > 0;
157 --curr_frame_num, --prev_frame_num)
158 {
159 const size_t curr_frame_idx = curr_frame_num-1;
160 const size_t prev_frame_idx = prev_frame_num-1;
161 StackFrameSP curr_frame_sp (curr_frames->m_frames[curr_frame_idx]);
162 StackFrameSP prev_frame_sp (prev_frames->m_frames[prev_frame_idx]);
163
164#if defined (DEBUG_STACK_FRAMES)
Greg Clayton5205f0b2010-09-03 17:10:42 +0000165 s.Printf("\n\nCurr frame #%u ", curr_frame_idx);
Greg Clayton1d66ef52010-08-27 18:24:16 +0000166 if (curr_frame_sp)
Greg Clayton5205f0b2010-09-03 17:10:42 +0000167 curr_frame_sp->Dump (&s, true, false);
Greg Clayton1d66ef52010-08-27 18:24:16 +0000168 else
169 s.PutCString("NULL");
Greg Clayton5205f0b2010-09-03 17:10:42 +0000170 s.Printf("\nPrev frame #%u ", prev_frame_idx);
Greg Clayton1d66ef52010-08-27 18:24:16 +0000171 if (prev_frame_sp)
Greg Clayton5205f0b2010-09-03 17:10:42 +0000172 prev_frame_sp->Dump (&s, true, false);
Greg Clayton1d66ef52010-08-27 18:24:16 +0000173 else
174 s.PutCString("NULL");
Greg Clayton1d66ef52010-08-27 18:24:16 +0000175#endif
176
177 StackFrame *curr_frame = curr_frame_sp.get();
178 StackFrame *prev_frame = prev_frame_sp.get();
179
180 if (curr_frame == NULL || prev_frame == NULL)
181 break;
182
Greg Clayton4fb08152010-08-30 18:11:35 +0000183 // Check the stack ID to make sure they are equal
184 if (curr_frame->GetStackID() != prev_frame->GetStackID())
Greg Clayton1d66ef52010-08-27 18:24:16 +0000185 break;
186
Greg Clayton4fb08152010-08-30 18:11:35 +0000187 prev_frame->UpdatePreviousFrameFromCurrentFrame (*curr_frame);
188 // Now copy the fixed up previous frame into the current frames
189 // so the pointer doesn't change
190 m_frames[curr_frame_idx] = prev_frame_sp;
191 //curr_frame->UpdateCurrentFrameFromPreviousFrame (*prev_frame);
Greg Clayton1d66ef52010-08-27 18:24:16 +0000192
193#if defined (DEBUG_STACK_FRAMES)
Greg Clayton870a1cd2010-08-27 21:47:54 +0000194 s.Printf("\n Copying previous frame to current frame");
Greg Clayton1d66ef52010-08-27 18:24:16 +0000195#endif
196 }
197 // We are done with the old stack frame list, we can release it now
Greg Clayton5205f0b2010-09-03 17:10:42 +0000198 m_prev_frames_sp.reset();
Greg Clayton1d66ef52010-08-27 18:24:16 +0000199 }
Greg Clayton870a1cd2010-08-27 21:47:54 +0000200
201#if defined (DEBUG_STACK_FRAMES)
202 s.PutCString("\n\nNew frames:\n");
203 Dump (&s);
204 s.EOL();
205#endif
Greg Clayton782b9cc2010-08-25 00:35:26 +0000206 }
Greg Clayton1d66ef52010-08-27 18:24:16 +0000207 else
208 {
209 m_frames.resize(m_thread.GetUnwinder()->GetFrameCount());
210 }
Greg Clayton782b9cc2010-08-25 00:35:26 +0000211 }
Greg Clayton1d66ef52010-08-27 18:24:16 +0000212 return m_frames.size();
213}
214
215void
216StackFrameList::Dump (Stream *s)
217{
218 if (s == NULL)
219 return;
220 Mutex::Locker locker (m_mutex);
221
222 const_iterator pos, begin = m_frames.begin(), end = m_frames.end();
223 for (pos = begin; pos != end; ++pos)
Greg Clayton782b9cc2010-08-25 00:35:26 +0000224 {
Greg Clayton1d66ef52010-08-27 18:24:16 +0000225 StackFrame *frame = (*pos).get();
226 s->Printf("%p: ", frame);
227 if (frame)
Greg Clayton4fb08152010-08-30 18:11:35 +0000228 {
229 frame->GetStackID().Dump (s);
Greg Claytona830adb2010-10-04 01:05:56 +0000230 frame->DumpUsingSettingsFormat (s);
Greg Clayton4fb08152010-08-30 18:11:35 +0000231 }
Greg Clayton1d66ef52010-08-27 18:24:16 +0000232 else
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000233 s->Printf("frame #%ld", std::distance (begin, pos));
Greg Clayton1d66ef52010-08-27 18:24:16 +0000234 s->EOL();
Greg Clayton782b9cc2010-08-25 00:35:26 +0000235 }
Greg Clayton1d66ef52010-08-27 18:24:16 +0000236 s->EOL();
Chris Lattner24943d22010-06-08 16:52:24 +0000237}
238
Chris Lattner24943d22010-06-08 16:52:24 +0000239StackFrameSP
Greg Clayton782b9cc2010-08-25 00:35:26 +0000240StackFrameList::GetFrameAtIndex (uint32_t idx)
Chris Lattner24943d22010-06-08 16:52:24 +0000241{
242 StackFrameSP frame_sp;
Greg Clayton1d66ef52010-08-27 18:24:16 +0000243 Mutex::Locker locker (m_mutex);
244 if (idx < m_frames.size())
245 frame_sp = m_frames[idx];
246
247 if (frame_sp)
248 return frame_sp;
249
250 // Special case the first frame (idx == 0) so that we don't need to
251 // know how many stack frames there are to get it. If we need any other
252 // frames, then we do need to know if "idx" is a valid index.
253 if (idx == 0)
Chris Lattner24943d22010-06-08 16:52:24 +0000254 {
Greg Clayton1d66ef52010-08-27 18:24:16 +0000255 // If this is the first frame, we want to share the thread register
256 // context with the stack frame at index zero.
257 m_thread.GetRegisterContext();
258 assert (m_thread.m_reg_context_sp.get());
259 frame_sp.reset (new StackFrame (0,
260 0,
261 m_thread,
262 m_thread.m_reg_context_sp,
263 m_thread.m_reg_context_sp->GetSP(),
264 m_thread.m_reg_context_sp->GetPC(),
265 NULL));
Greg Claytonf40e3082010-08-26 02:28:22 +0000266
Greg Clayton1d66ef52010-08-27 18:24:16 +0000267 SetFrameAtIndex(idx, frame_sp);
268 }
269 else if (idx < GetNumFrames())
270 {
271 if (m_show_inlined_frames)
272 {
273 // When inline frames are enabled we cache up all frames in GetNumFrames()
274 frame_sp = m_frames[idx];
Greg Clayton782b9cc2010-08-25 00:35:26 +0000275 }
276 else
277 {
Greg Clayton1d66ef52010-08-27 18:24:16 +0000278 Unwind *unwinder = m_thread.GetUnwinder ();
279 if (unwinder)
280 {
281 addr_t pc, cfa;
282 if (unwinder->GetFrameInfoAtIndex(idx, cfa, pc))
283 {
284 frame_sp.reset (new StackFrame (idx, idx, m_thread, cfa, pc, NULL));
Greg Clayton4fb08152010-08-30 18:11:35 +0000285
286 Function *function = frame_sp->GetSymbolContext (eSymbolContextFunction).function;
287 if (function)
288 {
289 // When we aren't showing inline functions we always use
290 // the top most function block as the scope.
291 frame_sp->SetSymbolContextScope (&function->GetBlock(false));
292 }
293 else
294 {
295 // Set the symbol scope from the symbol regardless if it is NULL or valid.
296 frame_sp->SetSymbolContextScope (frame_sp->GetSymbolContext (eSymbolContextSymbol).symbol);
297 }
Greg Clayton1d66ef52010-08-27 18:24:16 +0000298 SetFrameAtIndex(idx, frame_sp);
299 }
300 }
Greg Clayton782b9cc2010-08-25 00:35:26 +0000301 }
Chris Lattner24943d22010-06-08 16:52:24 +0000302 }
303 return frame_sp;
304}
305
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000306StackFrameSP
307StackFrameList::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx)
308{
309 // First try assuming the unwind index is the same as the frame index. The
310 // unwind index is always greater than or equal to the frame index, so it
311 // is a good place to start. If we have inlined frames we might have 5
312 // concrete frames (frame unwind indexes go from 0-4), but we might have 15
313 // frames after we make all the inlined frames. Most of the time the unwind
314 // frame index (or the concrete frame index) is the same as the frame index.
315 uint32_t frame_idx = unwind_idx;
316 StackFrameSP frame_sp (GetFrameAtIndex (frame_idx));
317 while (frame_sp)
318 {
319 if (frame_sp->GetFrameIndex() == unwind_idx)
320 break;
321 frame_sp = GetFrameAtIndex (++frame_idx);
322 }
323 return frame_sp;
324}
325
Jim Ingham5c4b1602011-03-31 00:15:49 +0000326StackFrameSP
327StackFrameList::GetFrameWithStackID (StackID &stack_id)
328{
329 uint32_t frame_idx = 0;
330 StackFrameSP frame_sp;
331 do
332 {
333 frame_sp = GetFrameAtIndex (frame_idx);
334 if (frame_sp && frame_sp->GetStackID() == stack_id)
335 break;
336 frame_idx++;
337 }
338 while (frame_sp);
339 return frame_sp;
340}
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000341
Greg Clayton782b9cc2010-08-25 00:35:26 +0000342bool
Greg Clayton1d66ef52010-08-27 18:24:16 +0000343StackFrameList::SetFrameAtIndex (uint32_t idx, StackFrameSP &frame_sp)
Greg Clayton782b9cc2010-08-25 00:35:26 +0000344{
Greg Clayton1d66ef52010-08-27 18:24:16 +0000345 if (idx >= m_frames.size())
346 m_frames.resize(idx + 1);
Greg Clayton782b9cc2010-08-25 00:35:26 +0000347 // Make sure allocation succeeded by checking bounds again
Greg Clayton1d66ef52010-08-27 18:24:16 +0000348 if (idx < m_frames.size())
Greg Clayton782b9cc2010-08-25 00:35:26 +0000349 {
Greg Clayton1d66ef52010-08-27 18:24:16 +0000350 m_frames[idx] = frame_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000351 return true;
352 }
353 return false; // resize failed, out of memory?
354}
355
356uint32_t
Jim Inghamc8332952010-08-26 21:32:51 +0000357StackFrameList::GetSelectedFrameIndex () const
Chris Lattner24943d22010-06-08 16:52:24 +0000358{
359 Mutex::Locker locker (m_mutex);
Jim Inghamc8332952010-08-26 21:32:51 +0000360 return m_selected_frame_idx;
Chris Lattner24943d22010-06-08 16:52:24 +0000361}
362
363
364uint32_t
Jim Inghamc8332952010-08-26 21:32:51 +0000365StackFrameList::SetSelectedFrame (lldb_private::StackFrame *frame)
Chris Lattner24943d22010-06-08 16:52:24 +0000366{
367 Mutex::Locker locker (m_mutex);
Greg Clayton782b9cc2010-08-25 00:35:26 +0000368 const_iterator pos;
Greg Clayton1d66ef52010-08-27 18:24:16 +0000369 const_iterator begin = m_frames.begin();
370 const_iterator end = m_frames.end();
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000371 m_selected_frame_idx = 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000372 for (pos = begin; pos != end; ++pos)
373 {
374 if (pos->get() == frame)
375 {
Jim Inghamc8332952010-08-26 21:32:51 +0000376 m_selected_frame_idx = std::distance (begin, pos);
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000377 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000378 }
379 }
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000380 SetDefaultFileAndLineToSelectedFrame();
Jim Inghamc8332952010-08-26 21:32:51 +0000381 return m_selected_frame_idx;
Chris Lattner24943d22010-06-08 16:52:24 +0000382}
383
384// Mark a stack frame as the current frame using the frame index
385void
Jim Inghamc8332952010-08-26 21:32:51 +0000386StackFrameList::SetSelectedFrameByIndex (uint32_t idx)
Chris Lattner24943d22010-06-08 16:52:24 +0000387{
388 Mutex::Locker locker (m_mutex);
Jim Inghamc8332952010-08-26 21:32:51 +0000389 m_selected_frame_idx = idx;
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000390 SetDefaultFileAndLineToSelectedFrame();
391}
392
393void
394StackFrameList::SetDefaultFileAndLineToSelectedFrame()
395{
396 if (m_thread.GetID() == m_thread.GetProcess().GetThreadList().GetSelectedThread()->GetID())
397 {
Greg Clayton840a9922011-10-05 03:14:31 +0000398 StackFrameSP frame_sp (GetFrameAtIndex (GetSelectedFrameIndex()));
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000399 if (frame_sp)
400 {
Greg Clayton840a9922011-10-05 03:14:31 +0000401 SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextLineEntry);
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000402 if (sc.line_entry.file)
Greg Clayton840a9922011-10-05 03:14:31 +0000403 m_thread.GetProcess().GetTarget().GetSourceManager().SetDefaultFileAndLine (sc.line_entry.file,
404 sc.line_entry.line);
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000405 }
406 }
Chris Lattner24943d22010-06-08 16:52:24 +0000407}
408
409// The thread has been run, reset the number stack frames to zero so we can
410// determine how many frames we have lazily.
411void
412StackFrameList::Clear ()
413{
414 Mutex::Locker locker (m_mutex);
Greg Clayton1d66ef52010-08-27 18:24:16 +0000415 m_frames.clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000416}
417
418void
419StackFrameList::InvalidateFrames (uint32_t start_idx)
420{
421 Mutex::Locker locker (m_mutex);
Greg Clayton782b9cc2010-08-25 00:35:26 +0000422 if (m_show_inlined_frames)
Chris Lattner24943d22010-06-08 16:52:24 +0000423 {
Greg Clayton782b9cc2010-08-25 00:35:26 +0000424 Clear();
425 }
426 else
427 {
Greg Clayton1d66ef52010-08-27 18:24:16 +0000428 const size_t num_frames = m_frames.size();
Greg Clayton782b9cc2010-08-25 00:35:26 +0000429 while (start_idx < num_frames)
430 {
Greg Clayton1d66ef52010-08-27 18:24:16 +0000431 m_frames[start_idx].reset();
Greg Clayton782b9cc2010-08-25 00:35:26 +0000432 ++start_idx;
433 }
Chris Lattner24943d22010-06-08 16:52:24 +0000434 }
435}
Greg Clayton5205f0b2010-09-03 17:10:42 +0000436
437void
438StackFrameList::Merge (std::auto_ptr<StackFrameList>& curr_ap, lldb::StackFrameListSP& prev_sp)
439{
440 Mutex::Locker curr_locker (curr_ap.get() ? curr_ap->m_mutex.GetMutex() : NULL);
441 Mutex::Locker prev_locker (prev_sp.get() ? prev_sp->m_mutex.GetMutex() : NULL);
442
443#if defined (DEBUG_STACK_FRAMES)
Johnny Chen0a77f902011-08-25 17:27:02 +0000444 StreamFile s(stdout, false);
Greg Clayton5205f0b2010-09-03 17:10:42 +0000445 s.PutCString("\n\nStackFrameList::Merge():\nPrev:\n");
446 if (prev_sp.get())
447 prev_sp->Dump (&s);
448 else
449 s.PutCString ("NULL");
450 s.PutCString("\nCurr:\n");
451 if (curr_ap.get())
452 curr_ap->Dump (&s);
453 else
454 s.PutCString ("NULL");
455 s.EOL();
456#endif
457
458 if (curr_ap.get() == NULL || curr_ap->GetNumFrames (false) == 0)
459 {
460#if defined (DEBUG_STACK_FRAMES)
461 s.PutCString("No current frames, leave previous frames alone...\n");
462#endif
463 curr_ap.release();
464 return;
465 }
466
467 if (prev_sp.get() == NULL || prev_sp->GetNumFrames (false) == 0)
468 {
469#if defined (DEBUG_STACK_FRAMES)
470 s.PutCString("No previous frames, so use current frames...\n");
471#endif
472 // We either don't have any previous frames, or since we have more than
473 // one current frames it means we have all the frames and can safely
474 // replace our previous frames.
475 prev_sp.reset (curr_ap.release());
476 return;
477 }
478
479 const uint32_t num_curr_frames = curr_ap->GetNumFrames (false);
480
481 if (num_curr_frames > 1)
482 {
483#if defined (DEBUG_STACK_FRAMES)
484 s.PutCString("We have more than one current frame, so use current frames...\n");
485#endif
486 // We have more than one current frames it means we have all the frames
487 // and can safely replace our previous frames.
488 prev_sp.reset (curr_ap.release());
489
490#if defined (DEBUG_STACK_FRAMES)
491 s.PutCString("\nMerged:\n");
492 prev_sp->Dump (&s);
493#endif
494 return;
495 }
496
497 StackFrameSP prev_frame_zero_sp(prev_sp->GetFrameAtIndex (0));
498 StackFrameSP curr_frame_zero_sp(curr_ap->GetFrameAtIndex (0));
499 StackID curr_stack_id (curr_frame_zero_sp->GetStackID());
500 StackID prev_stack_id (prev_frame_zero_sp->GetStackID());
501
Greg Clayton5205f0b2010-09-03 17:10:42 +0000502#if defined (DEBUG_STACK_FRAMES)
Johnny Chen0a77f902011-08-25 17:27:02 +0000503 const uint32_t num_prev_frames = prev_sp->GetNumFrames (false);
Greg Clayton5205f0b2010-09-03 17:10:42 +0000504 s.Printf("\n%u previous frames with one current frame\n", num_prev_frames);
505#endif
506
507 // We have only a single current frame
508 // Our previous stack frames only had a single frame as well...
509 if (curr_stack_id == prev_stack_id)
510 {
511#if defined (DEBUG_STACK_FRAMES)
512 s.Printf("\nPrevious frame #0 is same as current frame #0, merge the cached data\n");
513#endif
514
515 curr_frame_zero_sp->UpdateCurrentFrameFromPreviousFrame (*prev_frame_zero_sp);
516// prev_frame_zero_sp->UpdatePreviousFrameFromCurrentFrame (*curr_frame_zero_sp);
517// prev_sp->SetFrameAtIndex (0, prev_frame_zero_sp);
518 }
519 else if (curr_stack_id < prev_stack_id)
520 {
521#if defined (DEBUG_STACK_FRAMES)
522 s.Printf("\nCurrent frame #0 has a stack ID that is less than the previous frame #0, insert current frame zero in front of previous\n");
523#endif
524 prev_sp->m_frames.insert (prev_sp->m_frames.begin(), curr_frame_zero_sp);
525 }
526
527 curr_ap.release();
528
529#if defined (DEBUG_STACK_FRAMES)
530 s.PutCString("\nMerged:\n");
531 prev_sp->Dump (&s);
532#endif
533
534
535}
Jim Inghamccd584d2010-09-23 17:40:12 +0000536
537lldb::StackFrameSP
538StackFrameList::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr)
539{
540 const_iterator pos;
541 const_iterator begin = m_frames.begin();
542 const_iterator end = m_frames.end();
543 lldb::StackFrameSP ret_sp;
544
545 for (pos = begin; pos != end; ++pos)
546 {
547 if (pos->get() == stack_frame_ptr)
548 {
549 ret_sp = (*pos);
550 break;
551 }
552 }
553 return ret_sp;
554}
555
Greg Claytonabe0fed2011-04-18 08:33:37 +0000556size_t
557StackFrameList::GetStatus (Stream& strm,
558 uint32_t first_frame,
559 uint32_t num_frames,
560 bool show_frame_info,
561 uint32_t num_frames_with_source,
562 uint32_t source_lines_before,
563 uint32_t source_lines_after)
564{
565 size_t num_frames_displayed = 0;
566
567 if (num_frames == 0)
568 return 0;
569
570 StackFrameSP frame_sp;
571 uint32_t frame_idx = 0;
572 uint32_t last_frame;
573
574 // Don't let the last frame wrap around...
575 if (num_frames == UINT32_MAX)
576 last_frame = UINT32_MAX;
577 else
578 last_frame = first_frame + num_frames;
579
580 for (frame_idx = first_frame; frame_idx < last_frame; ++frame_idx)
581 {
582 frame_sp = GetFrameAtIndex(frame_idx);
583 if (frame_sp.get() == NULL)
584 break;
585
586 if (!frame_sp->GetStatus (strm,
587 show_frame_info,
588 num_frames_with_source > first_frame - frame_idx,
589 source_lines_before,
590 source_lines_after))
591 break;
592 ++num_frames_displayed;
593 }
594
595 strm.IndentLess();
596 return num_frames_displayed;
597}
598