blob: 76f74f1533ccc6678bae49bf73432ce7272e22d7 [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 Claytond426d632011-10-07 01:52:19 +0000119 Address curr_frame_address (unwind_frame_sp->GetFrameCodeAddress());
120 // Be sure to adjust the frame address to match the address
121 // that was used to lookup the symbol context above. If we are
122 // in the first concrete frame, then we lookup using the current
123 // address, else we decrement the address by one to get the correct
124 // location.
125 if (idx > 0)
126 curr_frame_address.Slide(-1);
127
Greg Clayton2f57db02011-10-01 00:45:15 +0000128 SymbolContext next_frame_sc;
129 Address next_frame_address;
130
131 while (unwind_sc.GetParentOfInlinedScope(curr_frame_address, next_frame_sc, next_frame_address))
Greg Clayton782b9cc2010-08-25 00:35:26 +0000132 {
Greg Clayton4fb08152010-08-30 18:11:35 +0000133 StackFrameSP frame_sp(new StackFrame (m_frames.size(),
134 idx,
135 m_thread,
136 unwind_frame_sp->GetRegisterContextSP (),
137 cfa,
Greg Clayton2f57db02011-10-01 00:45:15 +0000138 next_frame_address,
139 &next_frame_sc));
140
Greg Clayton4fb08152010-08-30 18:11:35 +0000141 m_frames.push_back (frame_sp);
Greg Clayton2f57db02011-10-01 00:45:15 +0000142 unwind_sc = next_frame_sc;
143 curr_frame_address = next_frame_address;
144
Greg Clayton782b9cc2010-08-25 00:35:26 +0000145 }
146 }
147 }
Greg Clayton5205f0b2010-09-03 17:10:42 +0000148
149 if (m_prev_frames_sp)
Greg Clayton1d66ef52010-08-27 18:24:16 +0000150 {
Greg Clayton5205f0b2010-09-03 17:10:42 +0000151 StackFrameList *prev_frames = m_prev_frames_sp.get();
Greg Clayton1d66ef52010-08-27 18:24:16 +0000152 StackFrameList *curr_frames = this;
153
154#if defined (DEBUG_STACK_FRAMES)
Greg Clayton870a1cd2010-08-27 21:47:54 +0000155 s.PutCString("\nprev_frames:\n");
Greg Clayton1d66ef52010-08-27 18:24:16 +0000156 prev_frames->Dump (&s);
Greg Clayton870a1cd2010-08-27 21:47:54 +0000157 s.PutCString("\ncurr_frames:\n");
Greg Clayton1d66ef52010-08-27 18:24:16 +0000158 curr_frames->Dump (&s);
159 s.EOL();
160#endif
161 size_t curr_frame_num, prev_frame_num;
162
163 for (curr_frame_num = curr_frames->m_frames.size(), prev_frame_num = prev_frames->m_frames.size();
164 curr_frame_num > 0 && prev_frame_num > 0;
165 --curr_frame_num, --prev_frame_num)
166 {
167 const size_t curr_frame_idx = curr_frame_num-1;
168 const size_t prev_frame_idx = prev_frame_num-1;
169 StackFrameSP curr_frame_sp (curr_frames->m_frames[curr_frame_idx]);
170 StackFrameSP prev_frame_sp (prev_frames->m_frames[prev_frame_idx]);
171
172#if defined (DEBUG_STACK_FRAMES)
Greg Clayton5205f0b2010-09-03 17:10:42 +0000173 s.Printf("\n\nCurr frame #%u ", curr_frame_idx);
Greg Clayton1d66ef52010-08-27 18:24:16 +0000174 if (curr_frame_sp)
Greg Clayton5205f0b2010-09-03 17:10:42 +0000175 curr_frame_sp->Dump (&s, true, false);
Greg Clayton1d66ef52010-08-27 18:24:16 +0000176 else
177 s.PutCString("NULL");
Greg Clayton5205f0b2010-09-03 17:10:42 +0000178 s.Printf("\nPrev frame #%u ", prev_frame_idx);
Greg Clayton1d66ef52010-08-27 18:24:16 +0000179 if (prev_frame_sp)
Greg Clayton5205f0b2010-09-03 17:10:42 +0000180 prev_frame_sp->Dump (&s, true, false);
Greg Clayton1d66ef52010-08-27 18:24:16 +0000181 else
182 s.PutCString("NULL");
Greg Clayton1d66ef52010-08-27 18:24:16 +0000183#endif
184
185 StackFrame *curr_frame = curr_frame_sp.get();
186 StackFrame *prev_frame = prev_frame_sp.get();
187
188 if (curr_frame == NULL || prev_frame == NULL)
189 break;
190
Greg Clayton4fb08152010-08-30 18:11:35 +0000191 // Check the stack ID to make sure they are equal
192 if (curr_frame->GetStackID() != prev_frame->GetStackID())
Greg Clayton1d66ef52010-08-27 18:24:16 +0000193 break;
194
Greg Clayton4fb08152010-08-30 18:11:35 +0000195 prev_frame->UpdatePreviousFrameFromCurrentFrame (*curr_frame);
196 // Now copy the fixed up previous frame into the current frames
197 // so the pointer doesn't change
198 m_frames[curr_frame_idx] = prev_frame_sp;
199 //curr_frame->UpdateCurrentFrameFromPreviousFrame (*prev_frame);
Greg Clayton1d66ef52010-08-27 18:24:16 +0000200
201#if defined (DEBUG_STACK_FRAMES)
Greg Clayton870a1cd2010-08-27 21:47:54 +0000202 s.Printf("\n Copying previous frame to current frame");
Greg Clayton1d66ef52010-08-27 18:24:16 +0000203#endif
204 }
205 // We are done with the old stack frame list, we can release it now
Greg Clayton5205f0b2010-09-03 17:10:42 +0000206 m_prev_frames_sp.reset();
Greg Clayton1d66ef52010-08-27 18:24:16 +0000207 }
Greg Clayton870a1cd2010-08-27 21:47:54 +0000208
209#if defined (DEBUG_STACK_FRAMES)
210 s.PutCString("\n\nNew frames:\n");
211 Dump (&s);
212 s.EOL();
213#endif
Greg Clayton782b9cc2010-08-25 00:35:26 +0000214 }
Greg Clayton1d66ef52010-08-27 18:24:16 +0000215 else
216 {
217 m_frames.resize(m_thread.GetUnwinder()->GetFrameCount());
218 }
Greg Clayton782b9cc2010-08-25 00:35:26 +0000219 }
Greg Clayton1d66ef52010-08-27 18:24:16 +0000220 return m_frames.size();
221}
222
223void
224StackFrameList::Dump (Stream *s)
225{
226 if (s == NULL)
227 return;
228 Mutex::Locker locker (m_mutex);
229
230 const_iterator pos, begin = m_frames.begin(), end = m_frames.end();
231 for (pos = begin; pos != end; ++pos)
Greg Clayton782b9cc2010-08-25 00:35:26 +0000232 {
Greg Clayton1d66ef52010-08-27 18:24:16 +0000233 StackFrame *frame = (*pos).get();
234 s->Printf("%p: ", frame);
235 if (frame)
Greg Clayton4fb08152010-08-30 18:11:35 +0000236 {
237 frame->GetStackID().Dump (s);
Greg Claytona830adb2010-10-04 01:05:56 +0000238 frame->DumpUsingSettingsFormat (s);
Greg Clayton4fb08152010-08-30 18:11:35 +0000239 }
Greg Clayton1d66ef52010-08-27 18:24:16 +0000240 else
Greg Clayton3e4238d2011-11-04 03:34:56 +0000241 s->Printf("frame #%u", (uint32_t)std::distance (begin, pos));
Greg Clayton1d66ef52010-08-27 18:24:16 +0000242 s->EOL();
Greg Clayton782b9cc2010-08-25 00:35:26 +0000243 }
Greg Clayton1d66ef52010-08-27 18:24:16 +0000244 s->EOL();
Chris Lattner24943d22010-06-08 16:52:24 +0000245}
246
Chris Lattner24943d22010-06-08 16:52:24 +0000247StackFrameSP
Greg Clayton782b9cc2010-08-25 00:35:26 +0000248StackFrameList::GetFrameAtIndex (uint32_t idx)
Chris Lattner24943d22010-06-08 16:52:24 +0000249{
250 StackFrameSP frame_sp;
Greg Clayton1d66ef52010-08-27 18:24:16 +0000251 Mutex::Locker locker (m_mutex);
252 if (idx < m_frames.size())
253 frame_sp = m_frames[idx];
254
255 if (frame_sp)
256 return frame_sp;
257
258 // Special case the first frame (idx == 0) so that we don't need to
259 // know how many stack frames there are to get it. If we need any other
260 // frames, then we do need to know if "idx" is a valid index.
261 if (idx == 0)
Chris Lattner24943d22010-06-08 16:52:24 +0000262 {
Greg Clayton1d66ef52010-08-27 18:24:16 +0000263 // If this is the first frame, we want to share the thread register
264 // context with the stack frame at index zero.
265 m_thread.GetRegisterContext();
266 assert (m_thread.m_reg_context_sp.get());
267 frame_sp.reset (new StackFrame (0,
268 0,
269 m_thread,
270 m_thread.m_reg_context_sp,
271 m_thread.m_reg_context_sp->GetSP(),
272 m_thread.m_reg_context_sp->GetPC(),
273 NULL));
Greg Claytonf40e3082010-08-26 02:28:22 +0000274
Greg Clayton1d66ef52010-08-27 18:24:16 +0000275 SetFrameAtIndex(idx, frame_sp);
276 }
277 else if (idx < GetNumFrames())
278 {
279 if (m_show_inlined_frames)
280 {
281 // When inline frames are enabled we cache up all frames in GetNumFrames()
282 frame_sp = m_frames[idx];
Greg Clayton782b9cc2010-08-25 00:35:26 +0000283 }
284 else
285 {
Greg Clayton1d66ef52010-08-27 18:24:16 +0000286 Unwind *unwinder = m_thread.GetUnwinder ();
287 if (unwinder)
288 {
289 addr_t pc, cfa;
290 if (unwinder->GetFrameInfoAtIndex(idx, cfa, pc))
291 {
292 frame_sp.reset (new StackFrame (idx, idx, m_thread, cfa, pc, NULL));
Greg Clayton4fb08152010-08-30 18:11:35 +0000293
294 Function *function = frame_sp->GetSymbolContext (eSymbolContextFunction).function;
295 if (function)
296 {
297 // When we aren't showing inline functions we always use
298 // the top most function block as the scope.
299 frame_sp->SetSymbolContextScope (&function->GetBlock(false));
300 }
301 else
302 {
303 // Set the symbol scope from the symbol regardless if it is NULL or valid.
304 frame_sp->SetSymbolContextScope (frame_sp->GetSymbolContext (eSymbolContextSymbol).symbol);
305 }
Greg Clayton1d66ef52010-08-27 18:24:16 +0000306 SetFrameAtIndex(idx, frame_sp);
307 }
308 }
Greg Clayton782b9cc2010-08-25 00:35:26 +0000309 }
Chris Lattner24943d22010-06-08 16:52:24 +0000310 }
311 return frame_sp;
312}
313
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000314StackFrameSP
315StackFrameList::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx)
316{
317 // First try assuming the unwind index is the same as the frame index. The
318 // unwind index is always greater than or equal to the frame index, so it
319 // is a good place to start. If we have inlined frames we might have 5
320 // concrete frames (frame unwind indexes go from 0-4), but we might have 15
321 // frames after we make all the inlined frames. Most of the time the unwind
322 // frame index (or the concrete frame index) is the same as the frame index.
323 uint32_t frame_idx = unwind_idx;
324 StackFrameSP frame_sp (GetFrameAtIndex (frame_idx));
325 while (frame_sp)
326 {
327 if (frame_sp->GetFrameIndex() == unwind_idx)
328 break;
329 frame_sp = GetFrameAtIndex (++frame_idx);
330 }
331 return frame_sp;
332}
333
Jim Ingham5c4b1602011-03-31 00:15:49 +0000334StackFrameSP
335StackFrameList::GetFrameWithStackID (StackID &stack_id)
336{
337 uint32_t frame_idx = 0;
338 StackFrameSP frame_sp;
339 do
340 {
341 frame_sp = GetFrameAtIndex (frame_idx);
342 if (frame_sp && frame_sp->GetStackID() == stack_id)
343 break;
344 frame_idx++;
345 }
346 while (frame_sp);
347 return frame_sp;
348}
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000349
Greg Clayton782b9cc2010-08-25 00:35:26 +0000350bool
Greg Clayton1d66ef52010-08-27 18:24:16 +0000351StackFrameList::SetFrameAtIndex (uint32_t idx, StackFrameSP &frame_sp)
Greg Clayton782b9cc2010-08-25 00:35:26 +0000352{
Greg Clayton1d66ef52010-08-27 18:24:16 +0000353 if (idx >= m_frames.size())
354 m_frames.resize(idx + 1);
Greg Clayton782b9cc2010-08-25 00:35:26 +0000355 // Make sure allocation succeeded by checking bounds again
Greg Clayton1d66ef52010-08-27 18:24:16 +0000356 if (idx < m_frames.size())
Greg Clayton782b9cc2010-08-25 00:35:26 +0000357 {
Greg Clayton1d66ef52010-08-27 18:24:16 +0000358 m_frames[idx] = frame_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000359 return true;
360 }
361 return false; // resize failed, out of memory?
362}
363
364uint32_t
Jim Inghamc8332952010-08-26 21:32:51 +0000365StackFrameList::GetSelectedFrameIndex () const
Chris Lattner24943d22010-06-08 16:52:24 +0000366{
367 Mutex::Locker locker (m_mutex);
Jim Inghamc8332952010-08-26 21:32:51 +0000368 return m_selected_frame_idx;
Chris Lattner24943d22010-06-08 16:52:24 +0000369}
370
371
372uint32_t
Jim Inghamc8332952010-08-26 21:32:51 +0000373StackFrameList::SetSelectedFrame (lldb_private::StackFrame *frame)
Chris Lattner24943d22010-06-08 16:52:24 +0000374{
375 Mutex::Locker locker (m_mutex);
Greg Clayton782b9cc2010-08-25 00:35:26 +0000376 const_iterator pos;
Greg Clayton1d66ef52010-08-27 18:24:16 +0000377 const_iterator begin = m_frames.begin();
378 const_iterator end = m_frames.end();
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000379 m_selected_frame_idx = 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000380 for (pos = begin; pos != end; ++pos)
381 {
382 if (pos->get() == frame)
383 {
Jim Inghamc8332952010-08-26 21:32:51 +0000384 m_selected_frame_idx = std::distance (begin, pos);
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000385 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000386 }
387 }
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000388 SetDefaultFileAndLineToSelectedFrame();
Jim Inghamc8332952010-08-26 21:32:51 +0000389 return m_selected_frame_idx;
Chris Lattner24943d22010-06-08 16:52:24 +0000390}
391
392// Mark a stack frame as the current frame using the frame index
393void
Jim Inghamc8332952010-08-26 21:32:51 +0000394StackFrameList::SetSelectedFrameByIndex (uint32_t idx)
Chris Lattner24943d22010-06-08 16:52:24 +0000395{
396 Mutex::Locker locker (m_mutex);
Jim Inghamc8332952010-08-26 21:32:51 +0000397 m_selected_frame_idx = idx;
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000398 SetDefaultFileAndLineToSelectedFrame();
399}
400
401void
402StackFrameList::SetDefaultFileAndLineToSelectedFrame()
403{
404 if (m_thread.GetID() == m_thread.GetProcess().GetThreadList().GetSelectedThread()->GetID())
405 {
Greg Clayton840a9922011-10-05 03:14:31 +0000406 StackFrameSP frame_sp (GetFrameAtIndex (GetSelectedFrameIndex()));
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000407 if (frame_sp)
408 {
Greg Clayton840a9922011-10-05 03:14:31 +0000409 SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextLineEntry);
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000410 if (sc.line_entry.file)
Greg Clayton840a9922011-10-05 03:14:31 +0000411 m_thread.GetProcess().GetTarget().GetSourceManager().SetDefaultFileAndLine (sc.line_entry.file,
412 sc.line_entry.line);
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000413 }
414 }
Chris Lattner24943d22010-06-08 16:52:24 +0000415}
416
417// The thread has been run, reset the number stack frames to zero so we can
418// determine how many frames we have lazily.
419void
420StackFrameList::Clear ()
421{
422 Mutex::Locker locker (m_mutex);
Greg Clayton1d66ef52010-08-27 18:24:16 +0000423 m_frames.clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000424}
425
426void
427StackFrameList::InvalidateFrames (uint32_t start_idx)
428{
429 Mutex::Locker locker (m_mutex);
Greg Clayton782b9cc2010-08-25 00:35:26 +0000430 if (m_show_inlined_frames)
Chris Lattner24943d22010-06-08 16:52:24 +0000431 {
Greg Clayton782b9cc2010-08-25 00:35:26 +0000432 Clear();
433 }
434 else
435 {
Greg Clayton1d66ef52010-08-27 18:24:16 +0000436 const size_t num_frames = m_frames.size();
Greg Clayton782b9cc2010-08-25 00:35:26 +0000437 while (start_idx < num_frames)
438 {
Greg Clayton1d66ef52010-08-27 18:24:16 +0000439 m_frames[start_idx].reset();
Greg Clayton782b9cc2010-08-25 00:35:26 +0000440 ++start_idx;
441 }
Chris Lattner24943d22010-06-08 16:52:24 +0000442 }
443}
Greg Clayton5205f0b2010-09-03 17:10:42 +0000444
445void
446StackFrameList::Merge (std::auto_ptr<StackFrameList>& curr_ap, lldb::StackFrameListSP& prev_sp)
447{
448 Mutex::Locker curr_locker (curr_ap.get() ? curr_ap->m_mutex.GetMutex() : NULL);
449 Mutex::Locker prev_locker (prev_sp.get() ? prev_sp->m_mutex.GetMutex() : NULL);
450
451#if defined (DEBUG_STACK_FRAMES)
Johnny Chen0a77f902011-08-25 17:27:02 +0000452 StreamFile s(stdout, false);
Greg Clayton5205f0b2010-09-03 17:10:42 +0000453 s.PutCString("\n\nStackFrameList::Merge():\nPrev:\n");
454 if (prev_sp.get())
455 prev_sp->Dump (&s);
456 else
457 s.PutCString ("NULL");
458 s.PutCString("\nCurr:\n");
459 if (curr_ap.get())
460 curr_ap->Dump (&s);
461 else
462 s.PutCString ("NULL");
463 s.EOL();
464#endif
465
466 if (curr_ap.get() == NULL || curr_ap->GetNumFrames (false) == 0)
467 {
468#if defined (DEBUG_STACK_FRAMES)
469 s.PutCString("No current frames, leave previous frames alone...\n");
470#endif
471 curr_ap.release();
472 return;
473 }
474
475 if (prev_sp.get() == NULL || prev_sp->GetNumFrames (false) == 0)
476 {
477#if defined (DEBUG_STACK_FRAMES)
478 s.PutCString("No previous frames, so use current frames...\n");
479#endif
480 // We either don't have any previous frames, or since we have more than
481 // one current frames it means we have all the frames and can safely
482 // replace our previous frames.
483 prev_sp.reset (curr_ap.release());
484 return;
485 }
486
487 const uint32_t num_curr_frames = curr_ap->GetNumFrames (false);
488
489 if (num_curr_frames > 1)
490 {
491#if defined (DEBUG_STACK_FRAMES)
492 s.PutCString("We have more than one current frame, so use current frames...\n");
493#endif
494 // We have more than one current frames it means we have all the frames
495 // and can safely replace our previous frames.
496 prev_sp.reset (curr_ap.release());
497
498#if defined (DEBUG_STACK_FRAMES)
499 s.PutCString("\nMerged:\n");
500 prev_sp->Dump (&s);
501#endif
502 return;
503 }
504
505 StackFrameSP prev_frame_zero_sp(prev_sp->GetFrameAtIndex (0));
506 StackFrameSP curr_frame_zero_sp(curr_ap->GetFrameAtIndex (0));
507 StackID curr_stack_id (curr_frame_zero_sp->GetStackID());
508 StackID prev_stack_id (prev_frame_zero_sp->GetStackID());
509
Greg Clayton5205f0b2010-09-03 17:10:42 +0000510#if defined (DEBUG_STACK_FRAMES)
Johnny Chen0a77f902011-08-25 17:27:02 +0000511 const uint32_t num_prev_frames = prev_sp->GetNumFrames (false);
Greg Clayton5205f0b2010-09-03 17:10:42 +0000512 s.Printf("\n%u previous frames with one current frame\n", num_prev_frames);
513#endif
514
515 // We have only a single current frame
516 // Our previous stack frames only had a single frame as well...
517 if (curr_stack_id == prev_stack_id)
518 {
519#if defined (DEBUG_STACK_FRAMES)
520 s.Printf("\nPrevious frame #0 is same as current frame #0, merge the cached data\n");
521#endif
522
523 curr_frame_zero_sp->UpdateCurrentFrameFromPreviousFrame (*prev_frame_zero_sp);
524// prev_frame_zero_sp->UpdatePreviousFrameFromCurrentFrame (*curr_frame_zero_sp);
525// prev_sp->SetFrameAtIndex (0, prev_frame_zero_sp);
526 }
527 else if (curr_stack_id < prev_stack_id)
528 {
529#if defined (DEBUG_STACK_FRAMES)
530 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");
531#endif
532 prev_sp->m_frames.insert (prev_sp->m_frames.begin(), curr_frame_zero_sp);
533 }
534
535 curr_ap.release();
536
537#if defined (DEBUG_STACK_FRAMES)
538 s.PutCString("\nMerged:\n");
539 prev_sp->Dump (&s);
540#endif
541
542
543}
Jim Inghamccd584d2010-09-23 17:40:12 +0000544
545lldb::StackFrameSP
546StackFrameList::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr)
547{
548 const_iterator pos;
549 const_iterator begin = m_frames.begin();
550 const_iterator end = m_frames.end();
551 lldb::StackFrameSP ret_sp;
552
553 for (pos = begin; pos != end; ++pos)
554 {
555 if (pos->get() == stack_frame_ptr)
556 {
557 ret_sp = (*pos);
558 break;
559 }
560 }
561 return ret_sp;
562}
563
Greg Claytonabe0fed2011-04-18 08:33:37 +0000564size_t
565StackFrameList::GetStatus (Stream& strm,
566 uint32_t first_frame,
567 uint32_t num_frames,
568 bool show_frame_info,
569 uint32_t num_frames_with_source,
570 uint32_t source_lines_before,
571 uint32_t source_lines_after)
572{
573 size_t num_frames_displayed = 0;
574
575 if (num_frames == 0)
576 return 0;
577
578 StackFrameSP frame_sp;
579 uint32_t frame_idx = 0;
580 uint32_t last_frame;
581
582 // Don't let the last frame wrap around...
583 if (num_frames == UINT32_MAX)
584 last_frame = UINT32_MAX;
585 else
586 last_frame = first_frame + num_frames;
587
588 for (frame_idx = first_frame; frame_idx < last_frame; ++frame_idx)
589 {
590 frame_sp = GetFrameAtIndex(frame_idx);
591 if (frame_sp.get() == NULL)
592 break;
593
594 if (!frame_sp->GetStatus (strm,
595 show_frame_info,
596 num_frames_with_source > first_frame - frame_idx,
597 source_lines_before,
598 source_lines_after))
599 break;
600 ++num_frames_displayed;
601 }
602
603 strm.IndentLess();
604 return num_frames_displayed;
605}
606