blob: e336c4f6039834ace922869b05a46ef616ba8487 [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 Ingham0c8fa2d2012-09-01 01:02:41 +000024#include "lldb/Target/StopInfo.h"
Jim Inghamfdf24ef2011-09-08 22:13:49 +000025#include "lldb/Target/Target.h"
Greg Clayton782b9cc2010-08-25 00:35:26 +000026#include "lldb/Target/Thread.h"
27#include "lldb/Target/Unwind.h"
Chris Lattner24943d22010-06-08 16:52:24 +000028
Greg Clayton1d66ef52010-08-27 18:24:16 +000029//#define DEBUG_STACK_FRAMES 1
30
Chris Lattner24943d22010-06-08 16:52:24 +000031using namespace lldb;
32using namespace lldb_private;
33
34//----------------------------------------------------------------------
35// StackFrameList constructor
36//----------------------------------------------------------------------
Greg Clayton5205f0b2010-09-03 17:10:42 +000037StackFrameList::StackFrameList
38(
39 Thread &thread,
40 const lldb::StackFrameListSP &prev_frames_sp,
41 bool show_inline_frames
42) :
Greg Clayton782b9cc2010-08-25 00:35:26 +000043 m_thread (thread),
Greg Clayton5205f0b2010-09-03 17:10:42 +000044 m_prev_frames_sp (prev_frames_sp),
Chris Lattner24943d22010-06-08 16:52:24 +000045 m_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton1d66ef52010-08-27 18:24:16 +000046 m_frames (),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +000047 m_selected_frame_idx (0),
Jim Inghambf97d742012-02-29 03:40:22 +000048 m_concrete_frames_fetched (0),
Jim Ingham0c8fa2d2012-09-01 01:02:41 +000049 m_current_inlined_depth (UINT32_MAX),
50 m_current_inlined_pc (LLDB_INVALID_ADDRESS),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +000051 m_show_inlined_frames (show_inline_frames)
Chris Lattner24943d22010-06-08 16:52:24 +000052{
Jim Ingham0c8fa2d2012-09-01 01:02:41 +000053 if (prev_frames_sp)
54 {
55 m_current_inlined_depth = prev_frames_sp->m_current_inlined_depth;
56 m_current_inlined_pc = prev_frames_sp->m_current_inlined_pc;
57 }
Chris Lattner24943d22010-06-08 16:52:24 +000058}
59
60//----------------------------------------------------------------------
61// Destructor
62//----------------------------------------------------------------------
63StackFrameList::~StackFrameList()
64{
65}
66
Jim Inghambf97d742012-02-29 03:40:22 +000067void
Jim Ingham0c8fa2d2012-09-01 01:02:41 +000068StackFrameList::CalculateCurrentInlinedDepth()
69{
70 uint32_t cur_inlined_depth = GetCurrentInlinedDepth();
71 if (cur_inlined_depth == UINT32_MAX)
72 {
73 ResetCurrentInlinedDepth();
74 }
75}
76
77uint32_t
78StackFrameList::GetCurrentInlinedDepth ()
79{
80 if (m_show_inlined_frames)
81 {
82 lldb::addr_t cur_pc = m_thread.GetRegisterContext()->GetPC();
83 if (cur_pc != m_current_inlined_pc)
84 {
85 m_current_inlined_pc = LLDB_INVALID_ADDRESS;
86 m_current_inlined_depth = UINT32_MAX;
87 }
88 return m_current_inlined_depth;
89 }
90 else
91 {
92 return UINT32_MAX;
93 }
94}
95
Jim Ingham0c8fa2d2012-09-01 01:02:41 +000096void
97StackFrameList::ResetCurrentInlinedDepth ()
98{
Jim Ingham1cc0c052012-09-05 21:14:28 +000099 if (m_show_inlined_frames)
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000100 {
101 GetFramesUpTo(0);
102 if (!m_frames[0]->IsInlined())
103 {
104 m_current_inlined_depth = UINT32_MAX;
105 m_current_inlined_pc = LLDB_INVALID_ADDRESS;
106 }
107 else
108 {
109 // We only need to do something special about inlined blocks when we
110 // are at the beginning of an inlined function:
111 // FIXME: We probably also have to do something special if the PC is at the END
112 // of an inlined function, which coincides with the end of either its containing
113 // function or another inlined function.
114
115 lldb::addr_t curr_pc = m_thread.GetRegisterContext()->GetPC();
116 Block *block_ptr = m_frames[0]->GetFrameBlock();
117 if (block_ptr)
118 {
119 Address pc_as_address;
120 pc_as_address.SetLoadAddress(curr_pc, &(m_thread.GetProcess()->GetTarget()));
121 AddressRange containing_range;
122 if (block_ptr->GetRangeContainingAddress(pc_as_address, containing_range))
123 {
124 if (pc_as_address == containing_range.GetBaseAddress())
125 {
126 // If we got here because of a breakpoint hit, then set the inlined depth depending on where
127 // the breakpoint was set.
128 // If we got here because of a crash, then set the inlined depth to the deepest most block.
129 // Otherwise, we stopped here naturally as the result of a step, so set ourselves in the
130 // containing frame of the whole set of nested inlines, so the user can then "virtually"
131 // step into the frames one by one, or next over the whole mess.
132 // Note: We don't have to handle being somewhere in the middle of the stack here, since
133 // ResetCurrentInlinedDepth doesn't get called if there is a valid inlined depth set.
134 StopInfoSP stop_info_sp = m_thread.GetStopInfo();
135 if (stop_info_sp)
136 {
137 switch (stop_info_sp->GetStopReason())
138 {
139 case eStopReasonBreakpoint:
140 {
141
142 }
143 break;
144 case eStopReasonWatchpoint:
145 case eStopReasonException:
146 case eStopReasonSignal:
147 // In all these cases we want to stop in the deepest most frame.
148 m_current_inlined_pc = curr_pc;
149 m_current_inlined_depth = 0;
150 break;
151 default:
152 {
153 // Otherwise, we should set ourselves at the container of the inlining, so that the
154 // user can descend into them.
155 // So first we check whether we have more than one inlined block sharing this PC:
156 int num_inlined_functions = 0;
157
158 for (Block *container_ptr = block_ptr->GetInlinedParent();
159 container_ptr != NULL;
160 container_ptr = container_ptr->GetInlinedParent())
161 {
162 if (!container_ptr->GetRangeContainingAddress(pc_as_address, containing_range))
163 break;
164 if (pc_as_address != containing_range.GetBaseAddress())
165 break;
166
167 num_inlined_functions++;
168 }
169 m_current_inlined_pc = curr_pc;
170 m_current_inlined_depth = num_inlined_functions + 1;
171
172 }
173 break;
174 }
175 }
176 }
177 }
178 }
179 }
180 }
181}
182
183bool
184StackFrameList::DecrementCurrentInlinedDepth ()
185{
186 if (m_show_inlined_frames)
187 {
188 uint32_t current_inlined_depth = GetCurrentInlinedDepth();
189 if (current_inlined_depth != UINT32_MAX)
190 {
191 if (current_inlined_depth > 0)
Jim Ingham1949b1e2012-09-06 19:24:57 +0000192 {
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000193 m_current_inlined_depth--;
Jim Ingham1949b1e2012-09-06 19:24:57 +0000194 return true;
195 }
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000196 }
197 }
198 return false;
199}
200
201void
Jim Inghambf97d742012-02-29 03:40:22 +0000202StackFrameList::GetFramesUpTo(uint32_t end_idx)
203{
204 // We've already gotten more frames than asked for, or we've already finished unwinding, return.
205 if (m_frames.size() > end_idx || GetAllFramesFetched())
206 return;
207
208 Unwind *unwinder = m_thread.GetUnwinder ();
209
210 if (m_show_inlined_frames)
211 {
212#if defined (DEBUG_STACK_FRAMES)
213 StreamFile s(stdout, false);
214#endif
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000215 // If we are hiding some frames from the outside world, we need to add those onto the total count of
216 // frames to fetch. However, we don't need ot do that if end_idx is 0 since in that case we always
217 // get the first concrete frame and all the inlined frames below it...
218
219 uint32_t inlined_depth = 0;
220 if (end_idx > 0)
221 {
222 inlined_depth = GetCurrentInlinedDepth();
223 if (inlined_depth != UINT32_MAX)
224 {
225 if (end_idx > 0)
226 end_idx += inlined_depth;
227 }
228 else
229 inlined_depth = 0;
230 }
Jim Inghambf97d742012-02-29 03:40:22 +0000231
232 StackFrameSP unwind_frame_sp;
233 do
234 {
235 uint32_t idx = m_concrete_frames_fetched++;
236 lldb::addr_t pc;
237 lldb::addr_t cfa;
238 if (idx == 0)
239 {
240 // We might have already created frame zero, only create it
241 // if we need to
242 if (m_frames.empty())
243 {
Jim Inghambf97d742012-02-29 03:40:22 +0000244 m_thread.GetRegisterContext();
Jim Inghamc92468f2012-02-29 19:58:25 +0000245 assert (m_thread.m_reg_context_sp.get());
Jim Ingham6252ea82012-03-01 02:53:40 +0000246
247 const bool success = unwinder->GetFrameInfoAtIndex(idx, cfa, pc);
248 // There shouldn't be any way not to get the frame info for frame 0.
249 // But if the unwinder can't make one, lets make one by hand with the
250 // SP as the CFA and see if that gets any further.
251 if (!success)
252 {
253 cfa = m_thread.GetRegisterContext()->GetSP();
254 pc = m_thread.GetRegisterContext()->GetPC();
255 }
256
Jim Inghambf97d742012-02-29 03:40:22 +0000257 unwind_frame_sp.reset (new StackFrame (m_thread.shared_from_this(),
258 m_frames.size(),
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000259 idx,
Jim Inghamc92468f2012-02-29 19:58:25 +0000260 m_thread.m_reg_context_sp,
Jim Ingham6252ea82012-03-01 02:53:40 +0000261 cfa,
Jim Inghamc92468f2012-02-29 19:58:25 +0000262 pc,
Jim Inghambf97d742012-02-29 03:40:22 +0000263 NULL));
264 m_frames.push_back (unwind_frame_sp);
265 }
266 else
267 {
268 unwind_frame_sp = m_frames.front();
269 cfa = unwind_frame_sp->m_id.GetCallFrameAddress();
270 }
271 }
272 else
273 {
274 const bool success = unwinder->GetFrameInfoAtIndex(idx, cfa, pc);
275 if (!success)
276 {
277 // We've gotten to the end of the stack.
278 SetAllFramesFetched();
279 break;
280 }
281 unwind_frame_sp.reset (new StackFrame (m_thread.shared_from_this(), m_frames.size(), idx, cfa, pc, NULL));
282 m_frames.push_back (unwind_frame_sp);
283 }
284
285 SymbolContext unwind_sc = unwind_frame_sp->GetSymbolContext (eSymbolContextBlock | eSymbolContextFunction);
286 Block *unwind_block = unwind_sc.block;
287 if (unwind_block)
288 {
289 Address curr_frame_address (unwind_frame_sp->GetFrameCodeAddress());
290 // Be sure to adjust the frame address to match the address
291 // that was used to lookup the symbol context above. If we are
292 // in the first concrete frame, then we lookup using the current
293 // address, else we decrement the address by one to get the correct
294 // location.
295 if (idx > 0)
296 curr_frame_address.Slide(-1);
297
298 SymbolContext next_frame_sc;
299 Address next_frame_address;
300
301 while (unwind_sc.GetParentOfInlinedScope(curr_frame_address, next_frame_sc, next_frame_address))
302 {
303 StackFrameSP frame_sp(new StackFrame (m_thread.shared_from_this(),
304 m_frames.size(),
305 idx,
306 unwind_frame_sp->GetRegisterContextSP (),
307 cfa,
308 next_frame_address,
309 &next_frame_sc));
310
311 m_frames.push_back (frame_sp);
312 unwind_sc = next_frame_sc;
313 curr_frame_address = next_frame_address;
314 }
315 }
316 } while (m_frames.size() - 1 < end_idx);
317
318 // Don't try to merge till you've calculated all the frames in this stack.
319 if (GetAllFramesFetched() && m_prev_frames_sp)
320 {
321 StackFrameList *prev_frames = m_prev_frames_sp.get();
322 StackFrameList *curr_frames = this;
Jim Ingham1949b1e2012-09-06 19:24:57 +0000323
324 curr_frames->m_current_inlined_depth = prev_frames->m_current_inlined_depth;
325 curr_frames->m_current_inlined_pc = prev_frames->m_current_inlined_pc;
Jim Inghambf97d742012-02-29 03:40:22 +0000326
327#if defined (DEBUG_STACK_FRAMES)
328 s.PutCString("\nprev_frames:\n");
329 prev_frames->Dump (&s);
330 s.PutCString("\ncurr_frames:\n");
331 curr_frames->Dump (&s);
332 s.EOL();
333#endif
334 size_t curr_frame_num, prev_frame_num;
335
336 for (curr_frame_num = curr_frames->m_frames.size(), prev_frame_num = prev_frames->m_frames.size();
337 curr_frame_num > 0 && prev_frame_num > 0;
338 --curr_frame_num, --prev_frame_num)
339 {
340 const size_t curr_frame_idx = curr_frame_num-1;
341 const size_t prev_frame_idx = prev_frame_num-1;
342 StackFrameSP curr_frame_sp (curr_frames->m_frames[curr_frame_idx]);
343 StackFrameSP prev_frame_sp (prev_frames->m_frames[prev_frame_idx]);
344
345#if defined (DEBUG_STACK_FRAMES)
346 s.Printf("\n\nCurr frame #%u ", curr_frame_idx);
347 if (curr_frame_sp)
348 curr_frame_sp->Dump (&s, true, false);
349 else
350 s.PutCString("NULL");
351 s.Printf("\nPrev frame #%u ", prev_frame_idx);
352 if (prev_frame_sp)
353 prev_frame_sp->Dump (&s, true, false);
354 else
355 s.PutCString("NULL");
356#endif
357
358 StackFrame *curr_frame = curr_frame_sp.get();
359 StackFrame *prev_frame = prev_frame_sp.get();
360
361 if (curr_frame == NULL || prev_frame == NULL)
362 break;
363
364 // Check the stack ID to make sure they are equal
365 if (curr_frame->GetStackID() != prev_frame->GetStackID())
366 break;
367
368 prev_frame->UpdatePreviousFrameFromCurrentFrame (*curr_frame);
369 // Now copy the fixed up previous frame into the current frames
370 // so the pointer doesn't change
371 m_frames[curr_frame_idx] = prev_frame_sp;
372 //curr_frame->UpdateCurrentFrameFromPreviousFrame (*prev_frame);
373
374#if defined (DEBUG_STACK_FRAMES)
375 s.Printf("\n Copying previous frame to current frame");
376#endif
377 }
378 // We are done with the old stack frame list, we can release it now
379 m_prev_frames_sp.reset();
380 }
381
382#if defined (DEBUG_STACK_FRAMES)
383 s.PutCString("\n\nNew frames:\n");
384 Dump (&s);
385 s.EOL();
386#endif
387 }
388 else
389 {
390 if (end_idx < m_concrete_frames_fetched)
391 return;
392
393 uint32_t num_frames = unwinder->GetFramesUpTo(end_idx);
394 if (num_frames <= end_idx + 1)
395 {
396 //Done unwinding.
397 m_concrete_frames_fetched = UINT32_MAX;
398 }
399 m_frames.resize(num_frames);
400 }
401}
Chris Lattner24943d22010-06-08 16:52:24 +0000402
403uint32_t
Greg Clayton5205f0b2010-09-03 17:10:42 +0000404StackFrameList::GetNumFrames (bool can_create)
Chris Lattner24943d22010-06-08 16:52:24 +0000405{
406 Mutex::Locker locker (m_mutex);
Greg Clayton782b9cc2010-08-25 00:35:26 +0000407
Jim Inghambf97d742012-02-29 03:40:22 +0000408 if (can_create)
409 GetFramesUpTo (UINT32_MAX);
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000410
411 uint32_t inlined_depth = GetCurrentInlinedDepth();
412 if (inlined_depth == UINT32_MAX)
413 return m_frames.size();
414 else
415 return m_frames.size() - inlined_depth;
Greg Clayton1d66ef52010-08-27 18:24:16 +0000416}
417
418void
419StackFrameList::Dump (Stream *s)
420{
421 if (s == NULL)
422 return;
423 Mutex::Locker locker (m_mutex);
424
425 const_iterator pos, begin = m_frames.begin(), end = m_frames.end();
426 for (pos = begin; pos != end; ++pos)
Greg Clayton782b9cc2010-08-25 00:35:26 +0000427 {
Greg Clayton1d66ef52010-08-27 18:24:16 +0000428 StackFrame *frame = (*pos).get();
429 s->Printf("%p: ", frame);
430 if (frame)
Greg Clayton4fb08152010-08-30 18:11:35 +0000431 {
432 frame->GetStackID().Dump (s);
Greg Claytona830adb2010-10-04 01:05:56 +0000433 frame->DumpUsingSettingsFormat (s);
Greg Clayton4fb08152010-08-30 18:11:35 +0000434 }
Greg Clayton1d66ef52010-08-27 18:24:16 +0000435 else
Greg Clayton3e4238d2011-11-04 03:34:56 +0000436 s->Printf("frame #%u", (uint32_t)std::distance (begin, pos));
Greg Clayton1d66ef52010-08-27 18:24:16 +0000437 s->EOL();
Greg Clayton782b9cc2010-08-25 00:35:26 +0000438 }
Greg Clayton1d66ef52010-08-27 18:24:16 +0000439 s->EOL();
Chris Lattner24943d22010-06-08 16:52:24 +0000440}
441
Chris Lattner24943d22010-06-08 16:52:24 +0000442StackFrameSP
Greg Clayton782b9cc2010-08-25 00:35:26 +0000443StackFrameList::GetFrameAtIndex (uint32_t idx)
Chris Lattner24943d22010-06-08 16:52:24 +0000444{
445 StackFrameSP frame_sp;
Greg Clayton1d66ef52010-08-27 18:24:16 +0000446 Mutex::Locker locker (m_mutex);
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000447 uint32_t inlined_depth = GetCurrentInlinedDepth();
448 if (inlined_depth != UINT32_MAX)
449 idx += inlined_depth;
450
Greg Clayton1d66ef52010-08-27 18:24:16 +0000451 if (idx < m_frames.size())
452 frame_sp = m_frames[idx];
453
454 if (frame_sp)
455 return frame_sp;
Greg Claytonf40e3082010-08-26 02:28:22 +0000456
Jim Inghamc92468f2012-02-29 19:58:25 +0000457 // GetFramesUpTo will fill m_frames with as many frames as you asked for,
458 // if there are that many. If there weren't then you asked for too many
459 // frames.
Jim Inghambf97d742012-02-29 03:40:22 +0000460 GetFramesUpTo (idx);
461 if (idx < m_frames.size())
Greg Clayton1d66ef52010-08-27 18:24:16 +0000462 {
Jim Inghambf97d742012-02-29 03:40:22 +0000463 if (m_show_inlined_frames)
Greg Clayton1d66ef52010-08-27 18:24:16 +0000464 {
Jim Inghamc92468f2012-02-29 19:58:25 +0000465 // When inline frames are enabled we actually create all the frames in GetFramesUpTo.
Jim Inghambf97d742012-02-29 03:40:22 +0000466 frame_sp = m_frames[idx];
467 }
468 else
469 {
470 Unwind *unwinder = m_thread.GetUnwinder ();
471 if (unwinder)
Greg Clayton1d66ef52010-08-27 18:24:16 +0000472 {
Jim Inghambf97d742012-02-29 03:40:22 +0000473 addr_t pc, cfa;
474 if (unwinder->GetFrameInfoAtIndex(idx, cfa, pc))
Greg Clayton4fb08152010-08-30 18:11:35 +0000475 {
Jim Inghambf97d742012-02-29 03:40:22 +0000476 frame_sp.reset (new StackFrame (m_thread.shared_from_this(), idx, idx, cfa, pc, NULL));
477
478 Function *function = frame_sp->GetSymbolContext (eSymbolContextFunction).function;
479 if (function)
480 {
481 // When we aren't showing inline functions we always use
482 // the top most function block as the scope.
483 frame_sp->SetSymbolContextScope (&function->GetBlock(false));
484 }
485 else
486 {
487 // Set the symbol scope from the symbol regardless if it is NULL or valid.
488 frame_sp->SetSymbolContextScope (frame_sp->GetSymbolContext (eSymbolContextSymbol).symbol);
489 }
490 SetFrameAtIndex(idx, frame_sp);
Greg Clayton4fb08152010-08-30 18:11:35 +0000491 }
Greg Clayton1d66ef52010-08-27 18:24:16 +0000492 }
493 }
Greg Clayton782b9cc2010-08-25 00:35:26 +0000494 }
Chris Lattner24943d22010-06-08 16:52:24 +0000495 return frame_sp;
496}
497
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000498StackFrameSP
499StackFrameList::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx)
500{
501 // First try assuming the unwind index is the same as the frame index. The
502 // unwind index is always greater than or equal to the frame index, so it
503 // is a good place to start. If we have inlined frames we might have 5
504 // concrete frames (frame unwind indexes go from 0-4), but we might have 15
505 // frames after we make all the inlined frames. Most of the time the unwind
506 // frame index (or the concrete frame index) is the same as the frame index.
507 uint32_t frame_idx = unwind_idx;
508 StackFrameSP frame_sp (GetFrameAtIndex (frame_idx));
509 while (frame_sp)
510 {
511 if (frame_sp->GetFrameIndex() == unwind_idx)
512 break;
513 frame_sp = GetFrameAtIndex (++frame_idx);
514 }
515 return frame_sp;
516}
517
Jim Ingham5c4b1602011-03-31 00:15:49 +0000518StackFrameSP
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000519StackFrameList::GetFrameWithStackID (const StackID &stack_id)
Jim Ingham5c4b1602011-03-31 00:15:49 +0000520{
521 uint32_t frame_idx = 0;
522 StackFrameSP frame_sp;
523 do
524 {
525 frame_sp = GetFrameAtIndex (frame_idx);
526 if (frame_sp && frame_sp->GetStackID() == stack_id)
527 break;
528 frame_idx++;
529 }
530 while (frame_sp);
531 return frame_sp;
532}
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000533
Greg Clayton782b9cc2010-08-25 00:35:26 +0000534bool
Greg Clayton1d66ef52010-08-27 18:24:16 +0000535StackFrameList::SetFrameAtIndex (uint32_t idx, StackFrameSP &frame_sp)
Greg Clayton782b9cc2010-08-25 00:35:26 +0000536{
Greg Clayton1d66ef52010-08-27 18:24:16 +0000537 if (idx >= m_frames.size())
538 m_frames.resize(idx + 1);
Greg Clayton782b9cc2010-08-25 00:35:26 +0000539 // Make sure allocation succeeded by checking bounds again
Greg Clayton1d66ef52010-08-27 18:24:16 +0000540 if (idx < m_frames.size())
Greg Clayton782b9cc2010-08-25 00:35:26 +0000541 {
Greg Clayton1d66ef52010-08-27 18:24:16 +0000542 m_frames[idx] = frame_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000543 return true;
544 }
545 return false; // resize failed, out of memory?
546}
547
548uint32_t
Jim Inghamc8332952010-08-26 21:32:51 +0000549StackFrameList::GetSelectedFrameIndex () const
Chris Lattner24943d22010-06-08 16:52:24 +0000550{
551 Mutex::Locker locker (m_mutex);
Jim Inghamc8332952010-08-26 21:32:51 +0000552 return m_selected_frame_idx;
Chris Lattner24943d22010-06-08 16:52:24 +0000553}
554
555
556uint32_t
Jim Inghamc8332952010-08-26 21:32:51 +0000557StackFrameList::SetSelectedFrame (lldb_private::StackFrame *frame)
Chris Lattner24943d22010-06-08 16:52:24 +0000558{
559 Mutex::Locker locker (m_mutex);
Greg Clayton782b9cc2010-08-25 00:35:26 +0000560 const_iterator pos;
Greg Clayton1d66ef52010-08-27 18:24:16 +0000561 const_iterator begin = m_frames.begin();
562 const_iterator end = m_frames.end();
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000563 m_selected_frame_idx = 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000564 for (pos = begin; pos != end; ++pos)
565 {
566 if (pos->get() == frame)
567 {
Jim Inghamc8332952010-08-26 21:32:51 +0000568 m_selected_frame_idx = std::distance (begin, pos);
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000569 uint32_t inlined_depth = GetCurrentInlinedDepth();
570 if (inlined_depth != UINT32_MAX)
571 m_selected_frame_idx -= inlined_depth;
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000572 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000573 }
574 }
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000575 SetDefaultFileAndLineToSelectedFrame();
Jim Inghamc8332952010-08-26 21:32:51 +0000576 return m_selected_frame_idx;
Chris Lattner24943d22010-06-08 16:52:24 +0000577}
578
579// Mark a stack frame as the current frame using the frame index
Jim Inghambf97d742012-02-29 03:40:22 +0000580bool
Jim Inghamc8332952010-08-26 21:32:51 +0000581StackFrameList::SetSelectedFrameByIndex (uint32_t idx)
Chris Lattner24943d22010-06-08 16:52:24 +0000582{
583 Mutex::Locker locker (m_mutex);
Jim Inghambf97d742012-02-29 03:40:22 +0000584 StackFrameSP frame_sp (GetFrameAtIndex (idx));
585 if (frame_sp)
586 {
587 SetSelectedFrame(frame_sp.get());
588 return true;
589 }
590 else
591 return false;
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000592}
593
594void
595StackFrameList::SetDefaultFileAndLineToSelectedFrame()
596{
Greg Claytonf4124de2012-02-21 00:09:25 +0000597 if (m_thread.GetID() == m_thread.GetProcess()->GetThreadList().GetSelectedThread()->GetID())
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000598 {
Greg Clayton840a9922011-10-05 03:14:31 +0000599 StackFrameSP frame_sp (GetFrameAtIndex (GetSelectedFrameIndex()));
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000600 if (frame_sp)
601 {
Greg Clayton840a9922011-10-05 03:14:31 +0000602 SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextLineEntry);
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000603 if (sc.line_entry.file)
Greg Claytonf4124de2012-02-21 00:09:25 +0000604 m_thread.CalculateTarget()->GetSourceManager().SetDefaultFileAndLine (sc.line_entry.file,
Greg Clayton840a9922011-10-05 03:14:31 +0000605 sc.line_entry.line);
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000606 }
607 }
Chris Lattner24943d22010-06-08 16:52:24 +0000608}
609
610// The thread has been run, reset the number stack frames to zero so we can
611// determine how many frames we have lazily.
612void
613StackFrameList::Clear ()
614{
615 Mutex::Locker locker (m_mutex);
Greg Clayton1d66ef52010-08-27 18:24:16 +0000616 m_frames.clear();
Jim Inghambf97d742012-02-29 03:40:22 +0000617 m_concrete_frames_fetched = 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000618}
619
620void
621StackFrameList::InvalidateFrames (uint32_t start_idx)
622{
623 Mutex::Locker locker (m_mutex);
Greg Clayton782b9cc2010-08-25 00:35:26 +0000624 if (m_show_inlined_frames)
Chris Lattner24943d22010-06-08 16:52:24 +0000625 {
Greg Clayton782b9cc2010-08-25 00:35:26 +0000626 Clear();
627 }
628 else
629 {
Greg Clayton1d66ef52010-08-27 18:24:16 +0000630 const size_t num_frames = m_frames.size();
Greg Clayton782b9cc2010-08-25 00:35:26 +0000631 while (start_idx < num_frames)
632 {
Greg Clayton1d66ef52010-08-27 18:24:16 +0000633 m_frames[start_idx].reset();
Greg Clayton782b9cc2010-08-25 00:35:26 +0000634 ++start_idx;
635 }
Chris Lattner24943d22010-06-08 16:52:24 +0000636 }
637}
Greg Clayton5205f0b2010-09-03 17:10:42 +0000638
639void
640StackFrameList::Merge (std::auto_ptr<StackFrameList>& curr_ap, lldb::StackFrameListSP& prev_sp)
641{
Jim Ingham1b584eb2012-05-04 23:02:50 +0000642 Mutex::Locker curr_locker (curr_ap.get() ? &curr_ap->m_mutex : NULL);
643 Mutex::Locker prev_locker (prev_sp.get() ? &prev_sp->m_mutex : NULL);
Greg Clayton5205f0b2010-09-03 17:10:42 +0000644
645#if defined (DEBUG_STACK_FRAMES)
Johnny Chen0a77f902011-08-25 17:27:02 +0000646 StreamFile s(stdout, false);
Greg Clayton5205f0b2010-09-03 17:10:42 +0000647 s.PutCString("\n\nStackFrameList::Merge():\nPrev:\n");
648 if (prev_sp.get())
649 prev_sp->Dump (&s);
650 else
651 s.PutCString ("NULL");
652 s.PutCString("\nCurr:\n");
653 if (curr_ap.get())
654 curr_ap->Dump (&s);
655 else
656 s.PutCString ("NULL");
657 s.EOL();
658#endif
659
660 if (curr_ap.get() == NULL || curr_ap->GetNumFrames (false) == 0)
661 {
662#if defined (DEBUG_STACK_FRAMES)
663 s.PutCString("No current frames, leave previous frames alone...\n");
664#endif
665 curr_ap.release();
666 return;
667 }
668
669 if (prev_sp.get() == NULL || prev_sp->GetNumFrames (false) == 0)
670 {
671#if defined (DEBUG_STACK_FRAMES)
672 s.PutCString("No previous frames, so use current frames...\n");
673#endif
674 // We either don't have any previous frames, or since we have more than
675 // one current frames it means we have all the frames and can safely
676 // replace our previous frames.
677 prev_sp.reset (curr_ap.release());
678 return;
679 }
680
681 const uint32_t num_curr_frames = curr_ap->GetNumFrames (false);
682
683 if (num_curr_frames > 1)
684 {
685#if defined (DEBUG_STACK_FRAMES)
686 s.PutCString("We have more than one current frame, so use current frames...\n");
687#endif
688 // We have more than one current frames it means we have all the frames
689 // and can safely replace our previous frames.
690 prev_sp.reset (curr_ap.release());
691
692#if defined (DEBUG_STACK_FRAMES)
693 s.PutCString("\nMerged:\n");
694 prev_sp->Dump (&s);
695#endif
696 return;
697 }
698
699 StackFrameSP prev_frame_zero_sp(prev_sp->GetFrameAtIndex (0));
700 StackFrameSP curr_frame_zero_sp(curr_ap->GetFrameAtIndex (0));
701 StackID curr_stack_id (curr_frame_zero_sp->GetStackID());
702 StackID prev_stack_id (prev_frame_zero_sp->GetStackID());
703
Greg Clayton5205f0b2010-09-03 17:10:42 +0000704#if defined (DEBUG_STACK_FRAMES)
Johnny Chen0a77f902011-08-25 17:27:02 +0000705 const uint32_t num_prev_frames = prev_sp->GetNumFrames (false);
Greg Clayton5205f0b2010-09-03 17:10:42 +0000706 s.Printf("\n%u previous frames with one current frame\n", num_prev_frames);
707#endif
708
709 // We have only a single current frame
710 // Our previous stack frames only had a single frame as well...
711 if (curr_stack_id == prev_stack_id)
712 {
713#if defined (DEBUG_STACK_FRAMES)
714 s.Printf("\nPrevious frame #0 is same as current frame #0, merge the cached data\n");
715#endif
716
717 curr_frame_zero_sp->UpdateCurrentFrameFromPreviousFrame (*prev_frame_zero_sp);
718// prev_frame_zero_sp->UpdatePreviousFrameFromCurrentFrame (*curr_frame_zero_sp);
719// prev_sp->SetFrameAtIndex (0, prev_frame_zero_sp);
720 }
721 else if (curr_stack_id < prev_stack_id)
722 {
723#if defined (DEBUG_STACK_FRAMES)
724 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");
725#endif
726 prev_sp->m_frames.insert (prev_sp->m_frames.begin(), curr_frame_zero_sp);
727 }
728
729 curr_ap.release();
730
731#if defined (DEBUG_STACK_FRAMES)
732 s.PutCString("\nMerged:\n");
733 prev_sp->Dump (&s);
734#endif
735
736
737}
Jim Inghamccd584d2010-09-23 17:40:12 +0000738
739lldb::StackFrameSP
740StackFrameList::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr)
741{
742 const_iterator pos;
743 const_iterator begin = m_frames.begin();
744 const_iterator end = m_frames.end();
745 lldb::StackFrameSP ret_sp;
746
747 for (pos = begin; pos != end; ++pos)
748 {
749 if (pos->get() == stack_frame_ptr)
750 {
751 ret_sp = (*pos);
752 break;
753 }
754 }
755 return ret_sp;
756}
757
Greg Claytonabe0fed2011-04-18 08:33:37 +0000758size_t
759StackFrameList::GetStatus (Stream& strm,
760 uint32_t first_frame,
761 uint32_t num_frames,
762 bool show_frame_info,
Greg Claytona7d3dc72012-07-11 20:33:48 +0000763 uint32_t num_frames_with_source)
Greg Claytonabe0fed2011-04-18 08:33:37 +0000764{
765 size_t num_frames_displayed = 0;
766
767 if (num_frames == 0)
768 return 0;
769
770 StackFrameSP frame_sp;
771 uint32_t frame_idx = 0;
772 uint32_t last_frame;
773
774 // Don't let the last frame wrap around...
775 if (num_frames == UINT32_MAX)
776 last_frame = UINT32_MAX;
777 else
778 last_frame = first_frame + num_frames;
779
780 for (frame_idx = first_frame; frame_idx < last_frame; ++frame_idx)
781 {
782 frame_sp = GetFrameAtIndex(frame_idx);
783 if (frame_sp.get() == NULL)
784 break;
785
786 if (!frame_sp->GetStatus (strm,
787 show_frame_info,
Greg Claytona7d3dc72012-07-11 20:33:48 +0000788 num_frames_with_source > (first_frame - frame_idx)))
Greg Claytonabe0fed2011-04-18 08:33:37 +0000789 break;
790 ++num_frames_displayed;
791 }
792
793 strm.IndentLess();
794 return num_frames_displayed;
795}
796