blob: 082fddd09edc977c5c479256a417793a931f8ae5 [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
Jim Ingham64a41e22012-09-08 00:26:49 +000016#include "lldb/Breakpoint/BreakpointLocation.h"
17#include "lldb/Breakpoint/Breakpoint.h"
Jim Ingham9b124c62012-09-07 23:35:54 +000018#include "lldb/Core/Log.h"
Greg Clayton1d66ef52010-08-27 18:24:16 +000019#include "lldb/Core/StreamFile.h"
Jim Inghamfdf24ef2011-09-08 22:13:49 +000020#include "lldb/Core/SourceManager.h"
Greg Clayton782b9cc2010-08-25 00:35:26 +000021#include "lldb/Symbol/Block.h"
22#include "lldb/Symbol/Function.h"
Greg Clayton4fb08152010-08-30 18:11:35 +000023#include "lldb/Symbol/Symbol.h"
Jim Inghamfdf24ef2011-09-08 22:13:49 +000024#include "lldb/Target/Process.h"
Greg Clayton782b9cc2010-08-25 00:35:26 +000025#include "lldb/Target/RegisterContext.h"
Chris Lattner24943d22010-06-08 16:52:24 +000026#include "lldb/Target/StackFrame.h"
Jim Ingham0c8fa2d2012-09-01 01:02:41 +000027#include "lldb/Target/StopInfo.h"
Jim Inghamfdf24ef2011-09-08 22:13:49 +000028#include "lldb/Target/Target.h"
Greg Clayton782b9cc2010-08-25 00:35:26 +000029#include "lldb/Target/Thread.h"
30#include "lldb/Target/Unwind.h"
Chris Lattner24943d22010-06-08 16:52:24 +000031
Greg Clayton1d66ef52010-08-27 18:24:16 +000032//#define DEBUG_STACK_FRAMES 1
33
Chris Lattner24943d22010-06-08 16:52:24 +000034using namespace lldb;
35using namespace lldb_private;
36
37//----------------------------------------------------------------------
38// StackFrameList constructor
39//----------------------------------------------------------------------
Greg Clayton5205f0b2010-09-03 17:10:42 +000040StackFrameList::StackFrameList
41(
42 Thread &thread,
43 const lldb::StackFrameListSP &prev_frames_sp,
44 bool show_inline_frames
45) :
Greg Clayton782b9cc2010-08-25 00:35:26 +000046 m_thread (thread),
Greg Clayton5205f0b2010-09-03 17:10:42 +000047 m_prev_frames_sp (prev_frames_sp),
Chris Lattner24943d22010-06-08 16:52:24 +000048 m_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton1d66ef52010-08-27 18:24:16 +000049 m_frames (),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +000050 m_selected_frame_idx (0),
Jim Inghambf97d742012-02-29 03:40:22 +000051 m_concrete_frames_fetched (0),
Jim Ingham0c8fa2d2012-09-01 01:02:41 +000052 m_current_inlined_depth (UINT32_MAX),
53 m_current_inlined_pc (LLDB_INVALID_ADDRESS),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +000054 m_show_inlined_frames (show_inline_frames)
Chris Lattner24943d22010-06-08 16:52:24 +000055{
Jim Ingham0c8fa2d2012-09-01 01:02:41 +000056 if (prev_frames_sp)
57 {
58 m_current_inlined_depth = prev_frames_sp->m_current_inlined_depth;
59 m_current_inlined_pc = prev_frames_sp->m_current_inlined_pc;
60 }
Chris Lattner24943d22010-06-08 16:52:24 +000061}
62
63//----------------------------------------------------------------------
64// Destructor
65//----------------------------------------------------------------------
66StackFrameList::~StackFrameList()
67{
68}
69
Jim Inghambf97d742012-02-29 03:40:22 +000070void
Jim Ingham0c8fa2d2012-09-01 01:02:41 +000071StackFrameList::CalculateCurrentInlinedDepth()
72{
73 uint32_t cur_inlined_depth = GetCurrentInlinedDepth();
74 if (cur_inlined_depth == UINT32_MAX)
75 {
76 ResetCurrentInlinedDepth();
77 }
78}
79
80uint32_t
81StackFrameList::GetCurrentInlinedDepth ()
82{
Jim Ingham9b124c62012-09-07 23:35:54 +000083 if (m_show_inlined_frames && m_current_inlined_pc != LLDB_INVALID_ADDRESS)
Jim Ingham0c8fa2d2012-09-01 01:02:41 +000084 {
85 lldb::addr_t cur_pc = m_thread.GetRegisterContext()->GetPC();
86 if (cur_pc != m_current_inlined_pc)
87 {
88 m_current_inlined_pc = LLDB_INVALID_ADDRESS;
89 m_current_inlined_depth = UINT32_MAX;
Jim Ingham9b124c62012-09-07 23:35:54 +000090 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
91 if (log && log->GetVerbose())
92 log->Printf ("GetCurrentInlinedDepth: invalidating current inlined depth.\n");
Jim Ingham0c8fa2d2012-09-01 01:02:41 +000093 }
94 return m_current_inlined_depth;
95 }
96 else
97 {
98 return UINT32_MAX;
99 }
100}
101
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000102void
103StackFrameList::ResetCurrentInlinedDepth ()
104{
Jim Ingham1cc0c052012-09-05 21:14:28 +0000105 if (m_show_inlined_frames)
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000106 {
107 GetFramesUpTo(0);
108 if (!m_frames[0]->IsInlined())
109 {
110 m_current_inlined_depth = UINT32_MAX;
111 m_current_inlined_pc = LLDB_INVALID_ADDRESS;
Jim Ingham9b124c62012-09-07 23:35:54 +0000112 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
113 if (log && log->GetVerbose())
114 log->Printf ("ResetCurrentInlinedDepth: Invalidating current inlined depth.\n");
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000115 }
116 else
117 {
118 // We only need to do something special about inlined blocks when we
119 // are at the beginning of an inlined function:
120 // FIXME: We probably also have to do something special if the PC is at the END
121 // of an inlined function, which coincides with the end of either its containing
122 // function or another inlined function.
123
124 lldb::addr_t curr_pc = m_thread.GetRegisterContext()->GetPC();
125 Block *block_ptr = m_frames[0]->GetFrameBlock();
126 if (block_ptr)
127 {
128 Address pc_as_address;
129 pc_as_address.SetLoadAddress(curr_pc, &(m_thread.GetProcess()->GetTarget()));
130 AddressRange containing_range;
131 if (block_ptr->GetRangeContainingAddress(pc_as_address, containing_range))
132 {
133 if (pc_as_address == containing_range.GetBaseAddress())
134 {
135 // If we got here because of a breakpoint hit, then set the inlined depth depending on where
136 // the breakpoint was set.
137 // If we got here because of a crash, then set the inlined depth to the deepest most block.
138 // Otherwise, we stopped here naturally as the result of a step, so set ourselves in the
139 // containing frame of the whole set of nested inlines, so the user can then "virtually"
140 // step into the frames one by one, or next over the whole mess.
141 // Note: We don't have to handle being somewhere in the middle of the stack here, since
142 // ResetCurrentInlinedDepth doesn't get called if there is a valid inlined depth set.
143 StopInfoSP stop_info_sp = m_thread.GetStopInfo();
144 if (stop_info_sp)
145 {
146 switch (stop_info_sp->GetStopReason())
147 {
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000148 case eStopReasonWatchpoint:
149 case eStopReasonException:
150 case eStopReasonSignal:
151 // In all these cases we want to stop in the deepest most frame.
152 m_current_inlined_pc = curr_pc;
153 m_current_inlined_depth = 0;
154 break;
Jim Ingham74d08f12012-09-07 01:11:08 +0000155 case eStopReasonBreakpoint:
156 {
157 // FIXME: Figure out what this break point is doing, and set the inline depth
158 // appropriately. Be careful to take into account breakpoints that implement
159 // step over prologue, since that should do the default calculation.
Jim Ingham64a41e22012-09-08 00:26:49 +0000160 // For now, if the breakpoints corresponding to this hit are all internal,
161 // I set the stop location to the top of the inlined stack, since that will make
162 // things like stepping over prologues work right. But if there are any non-internal
163 // breakpoints I do to the bottom of the stack, since that was the old behavior.
164 uint32_t bp_site_id = stop_info_sp->GetValue();
165 BreakpointSiteSP bp_site_sp(m_thread.GetProcess()->GetBreakpointSiteList().FindByID(bp_site_id));
166 bool all_internal = true;
167 if (bp_site_sp)
168 {
169 uint32_t num_owners = bp_site_sp->GetNumberOfOwners();
170 for (uint32_t i = 0; i < num_owners; i++)
171 {
172 Breakpoint &bp_ref = bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint();
173 if (!bp_ref.IsInternal())
174 {
175 all_internal = false;
176 }
177 }
178 }
179 if (!all_internal)
180 {
181 m_current_inlined_pc = curr_pc;
182 m_current_inlined_depth = 0;
183 break;
184 }
Jim Ingham74d08f12012-09-07 01:11:08 +0000185 }
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000186 default:
187 {
188 // Otherwise, we should set ourselves at the container of the inlining, so that the
189 // user can descend into them.
190 // So first we check whether we have more than one inlined block sharing this PC:
191 int num_inlined_functions = 0;
192
193 for (Block *container_ptr = block_ptr->GetInlinedParent();
194 container_ptr != NULL;
195 container_ptr = container_ptr->GetInlinedParent())
196 {
197 if (!container_ptr->GetRangeContainingAddress(pc_as_address, containing_range))
198 break;
199 if (pc_as_address != containing_range.GetBaseAddress())
200 break;
201
202 num_inlined_functions++;
203 }
204 m_current_inlined_pc = curr_pc;
205 m_current_inlined_depth = num_inlined_functions + 1;
Jim Ingham9b124c62012-09-07 23:35:54 +0000206 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
207 if (log && log->GetVerbose())
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000208 log->Printf ("ResetCurrentInlinedDepth: setting inlined depth: %d 0x%" PRIx64 ".\n", m_current_inlined_depth, curr_pc);
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000209
210 }
211 break;
212 }
213 }
214 }
215 }
216 }
217 }
218 }
219}
220
221bool
222StackFrameList::DecrementCurrentInlinedDepth ()
223{
224 if (m_show_inlined_frames)
225 {
226 uint32_t current_inlined_depth = GetCurrentInlinedDepth();
227 if (current_inlined_depth != UINT32_MAX)
228 {
229 if (current_inlined_depth > 0)
Jim Ingham1949b1e2012-09-06 19:24:57 +0000230 {
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000231 m_current_inlined_depth--;
Jim Ingham1949b1e2012-09-06 19:24:57 +0000232 return true;
233 }
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000234 }
235 }
236 return false;
237}
238
239void
Jim Ingham9b124c62012-09-07 23:35:54 +0000240StackFrameList::SetCurrentInlinedDepth (uint32_t new_depth)
241{
242 m_current_inlined_depth = new_depth;
243 if (new_depth == UINT32_MAX)
244 m_current_inlined_pc = LLDB_INVALID_ADDRESS;
245 else
246 m_current_inlined_pc = m_thread.GetRegisterContext()->GetPC();
247}
248
249void
Jim Inghambf97d742012-02-29 03:40:22 +0000250StackFrameList::GetFramesUpTo(uint32_t end_idx)
251{
252 // We've already gotten more frames than asked for, or we've already finished unwinding, return.
253 if (m_frames.size() > end_idx || GetAllFramesFetched())
254 return;
255
256 Unwind *unwinder = m_thread.GetUnwinder ();
257
258 if (m_show_inlined_frames)
259 {
260#if defined (DEBUG_STACK_FRAMES)
261 StreamFile s(stdout, false);
262#endif
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000263 // If we are hiding some frames from the outside world, we need to add those onto the total count of
264 // frames to fetch. However, we don't need ot do that if end_idx is 0 since in that case we always
Jim Ingham9b124c62012-09-07 23:35:54 +0000265 // get the first concrete frame and all the inlined frames below it... And of course, if end_idx is
266 // UINT32_MAX that means get all, so just do that...
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000267
268 uint32_t inlined_depth = 0;
Jim Ingham9b124c62012-09-07 23:35:54 +0000269 if (end_idx > 0 && end_idx != UINT32_MAX)
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000270 {
271 inlined_depth = GetCurrentInlinedDepth();
272 if (inlined_depth != UINT32_MAX)
273 {
274 if (end_idx > 0)
275 end_idx += inlined_depth;
276 }
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000277 }
Jim Inghambf97d742012-02-29 03:40:22 +0000278
279 StackFrameSP unwind_frame_sp;
280 do
281 {
282 uint32_t idx = m_concrete_frames_fetched++;
283 lldb::addr_t pc;
284 lldb::addr_t cfa;
285 if (idx == 0)
286 {
287 // We might have already created frame zero, only create it
288 // if we need to
289 if (m_frames.empty())
290 {
Jim Inghambf97d742012-02-29 03:40:22 +0000291 m_thread.GetRegisterContext();
Jim Inghamc92468f2012-02-29 19:58:25 +0000292 assert (m_thread.m_reg_context_sp.get());
Jim Ingham6252ea82012-03-01 02:53:40 +0000293
294 const bool success = unwinder->GetFrameInfoAtIndex(idx, cfa, pc);
295 // There shouldn't be any way not to get the frame info for frame 0.
296 // But if the unwinder can't make one, lets make one by hand with the
297 // SP as the CFA and see if that gets any further.
298 if (!success)
299 {
300 cfa = m_thread.GetRegisterContext()->GetSP();
301 pc = m_thread.GetRegisterContext()->GetPC();
302 }
303
Jim Inghambf97d742012-02-29 03:40:22 +0000304 unwind_frame_sp.reset (new StackFrame (m_thread.shared_from_this(),
305 m_frames.size(),
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000306 idx,
Jim Inghamc92468f2012-02-29 19:58:25 +0000307 m_thread.m_reg_context_sp,
Jim Ingham6252ea82012-03-01 02:53:40 +0000308 cfa,
Jim Inghamc92468f2012-02-29 19:58:25 +0000309 pc,
Jim Inghambf97d742012-02-29 03:40:22 +0000310 NULL));
311 m_frames.push_back (unwind_frame_sp);
312 }
313 else
314 {
315 unwind_frame_sp = m_frames.front();
316 cfa = unwind_frame_sp->m_id.GetCallFrameAddress();
317 }
318 }
319 else
320 {
321 const bool success = unwinder->GetFrameInfoAtIndex(idx, cfa, pc);
322 if (!success)
323 {
324 // We've gotten to the end of the stack.
325 SetAllFramesFetched();
326 break;
327 }
328 unwind_frame_sp.reset (new StackFrame (m_thread.shared_from_this(), m_frames.size(), idx, cfa, pc, NULL));
329 m_frames.push_back (unwind_frame_sp);
330 }
331
332 SymbolContext unwind_sc = unwind_frame_sp->GetSymbolContext (eSymbolContextBlock | eSymbolContextFunction);
333 Block *unwind_block = unwind_sc.block;
334 if (unwind_block)
335 {
336 Address curr_frame_address (unwind_frame_sp->GetFrameCodeAddress());
337 // Be sure to adjust the frame address to match the address
338 // that was used to lookup the symbol context above. If we are
339 // in the first concrete frame, then we lookup using the current
340 // address, else we decrement the address by one to get the correct
341 // location.
342 if (idx > 0)
343 curr_frame_address.Slide(-1);
344
345 SymbolContext next_frame_sc;
346 Address next_frame_address;
347
348 while (unwind_sc.GetParentOfInlinedScope(curr_frame_address, next_frame_sc, next_frame_address))
349 {
350 StackFrameSP frame_sp(new StackFrame (m_thread.shared_from_this(),
351 m_frames.size(),
352 idx,
353 unwind_frame_sp->GetRegisterContextSP (),
354 cfa,
355 next_frame_address,
356 &next_frame_sc));
357
358 m_frames.push_back (frame_sp);
359 unwind_sc = next_frame_sc;
360 curr_frame_address = next_frame_address;
361 }
362 }
363 } while (m_frames.size() - 1 < end_idx);
364
365 // Don't try to merge till you've calculated all the frames in this stack.
366 if (GetAllFramesFetched() && m_prev_frames_sp)
367 {
368 StackFrameList *prev_frames = m_prev_frames_sp.get();
369 StackFrameList *curr_frames = this;
Jim Ingham1949b1e2012-09-06 19:24:57 +0000370
Jim Ingham9b124c62012-09-07 23:35:54 +0000371 //curr_frames->m_current_inlined_depth = prev_frames->m_current_inlined_depth;
372 //curr_frames->m_current_inlined_pc = prev_frames->m_current_inlined_pc;
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000373 //printf ("GetFramesUpTo: Copying current inlined depth: %d 0x%" PRIx64 ".\n", curr_frames->m_current_inlined_depth, curr_frames->m_current_inlined_pc);
Jim Inghambf97d742012-02-29 03:40:22 +0000374
375#if defined (DEBUG_STACK_FRAMES)
376 s.PutCString("\nprev_frames:\n");
377 prev_frames->Dump (&s);
378 s.PutCString("\ncurr_frames:\n");
379 curr_frames->Dump (&s);
380 s.EOL();
381#endif
382 size_t curr_frame_num, prev_frame_num;
383
384 for (curr_frame_num = curr_frames->m_frames.size(), prev_frame_num = prev_frames->m_frames.size();
385 curr_frame_num > 0 && prev_frame_num > 0;
386 --curr_frame_num, --prev_frame_num)
387 {
388 const size_t curr_frame_idx = curr_frame_num-1;
389 const size_t prev_frame_idx = prev_frame_num-1;
390 StackFrameSP curr_frame_sp (curr_frames->m_frames[curr_frame_idx]);
391 StackFrameSP prev_frame_sp (prev_frames->m_frames[prev_frame_idx]);
392
393#if defined (DEBUG_STACK_FRAMES)
394 s.Printf("\n\nCurr frame #%u ", curr_frame_idx);
395 if (curr_frame_sp)
396 curr_frame_sp->Dump (&s, true, false);
397 else
398 s.PutCString("NULL");
399 s.Printf("\nPrev frame #%u ", prev_frame_idx);
400 if (prev_frame_sp)
401 prev_frame_sp->Dump (&s, true, false);
402 else
403 s.PutCString("NULL");
404#endif
405
406 StackFrame *curr_frame = curr_frame_sp.get();
407 StackFrame *prev_frame = prev_frame_sp.get();
408
409 if (curr_frame == NULL || prev_frame == NULL)
410 break;
411
412 // Check the stack ID to make sure they are equal
413 if (curr_frame->GetStackID() != prev_frame->GetStackID())
414 break;
415
416 prev_frame->UpdatePreviousFrameFromCurrentFrame (*curr_frame);
417 // Now copy the fixed up previous frame into the current frames
418 // so the pointer doesn't change
419 m_frames[curr_frame_idx] = prev_frame_sp;
420 //curr_frame->UpdateCurrentFrameFromPreviousFrame (*prev_frame);
421
422#if defined (DEBUG_STACK_FRAMES)
423 s.Printf("\n Copying previous frame to current frame");
424#endif
425 }
426 // We are done with the old stack frame list, we can release it now
427 m_prev_frames_sp.reset();
428 }
429
430#if defined (DEBUG_STACK_FRAMES)
431 s.PutCString("\n\nNew frames:\n");
432 Dump (&s);
433 s.EOL();
434#endif
435 }
436 else
437 {
438 if (end_idx < m_concrete_frames_fetched)
439 return;
440
441 uint32_t num_frames = unwinder->GetFramesUpTo(end_idx);
442 if (num_frames <= end_idx + 1)
443 {
444 //Done unwinding.
445 m_concrete_frames_fetched = UINT32_MAX;
446 }
447 m_frames.resize(num_frames);
448 }
449}
Chris Lattner24943d22010-06-08 16:52:24 +0000450
451uint32_t
Greg Clayton5205f0b2010-09-03 17:10:42 +0000452StackFrameList::GetNumFrames (bool can_create)
Chris Lattner24943d22010-06-08 16:52:24 +0000453{
454 Mutex::Locker locker (m_mutex);
Greg Clayton782b9cc2010-08-25 00:35:26 +0000455
Jim Inghambf97d742012-02-29 03:40:22 +0000456 if (can_create)
457 GetFramesUpTo (UINT32_MAX);
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000458
459 uint32_t inlined_depth = GetCurrentInlinedDepth();
460 if (inlined_depth == UINT32_MAX)
461 return m_frames.size();
462 else
463 return m_frames.size() - inlined_depth;
Greg Clayton1d66ef52010-08-27 18:24:16 +0000464}
465
466void
467StackFrameList::Dump (Stream *s)
468{
469 if (s == NULL)
470 return;
471 Mutex::Locker locker (m_mutex);
472
473 const_iterator pos, begin = m_frames.begin(), end = m_frames.end();
474 for (pos = begin; pos != end; ++pos)
Greg Clayton782b9cc2010-08-25 00:35:26 +0000475 {
Greg Clayton1d66ef52010-08-27 18:24:16 +0000476 StackFrame *frame = (*pos).get();
477 s->Printf("%p: ", frame);
478 if (frame)
Greg Clayton4fb08152010-08-30 18:11:35 +0000479 {
480 frame->GetStackID().Dump (s);
Greg Claytona830adb2010-10-04 01:05:56 +0000481 frame->DumpUsingSettingsFormat (s);
Greg Clayton4fb08152010-08-30 18:11:35 +0000482 }
Greg Clayton1d66ef52010-08-27 18:24:16 +0000483 else
Greg Clayton3e4238d2011-11-04 03:34:56 +0000484 s->Printf("frame #%u", (uint32_t)std::distance (begin, pos));
Greg Clayton1d66ef52010-08-27 18:24:16 +0000485 s->EOL();
Greg Clayton782b9cc2010-08-25 00:35:26 +0000486 }
Greg Clayton1d66ef52010-08-27 18:24:16 +0000487 s->EOL();
Chris Lattner24943d22010-06-08 16:52:24 +0000488}
489
Chris Lattner24943d22010-06-08 16:52:24 +0000490StackFrameSP
Greg Clayton782b9cc2010-08-25 00:35:26 +0000491StackFrameList::GetFrameAtIndex (uint32_t idx)
Chris Lattner24943d22010-06-08 16:52:24 +0000492{
493 StackFrameSP frame_sp;
Greg Clayton1d66ef52010-08-27 18:24:16 +0000494 Mutex::Locker locker (m_mutex);
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000495 uint32_t inlined_depth = GetCurrentInlinedDepth();
496 if (inlined_depth != UINT32_MAX)
497 idx += inlined_depth;
498
Greg Clayton1d66ef52010-08-27 18:24:16 +0000499 if (idx < m_frames.size())
500 frame_sp = m_frames[idx];
501
502 if (frame_sp)
503 return frame_sp;
Greg Claytonf40e3082010-08-26 02:28:22 +0000504
Jim Inghamc92468f2012-02-29 19:58:25 +0000505 // GetFramesUpTo will fill m_frames with as many frames as you asked for,
506 // if there are that many. If there weren't then you asked for too many
507 // frames.
Jim Inghambf97d742012-02-29 03:40:22 +0000508 GetFramesUpTo (idx);
509 if (idx < m_frames.size())
Greg Clayton1d66ef52010-08-27 18:24:16 +0000510 {
Jim Inghambf97d742012-02-29 03:40:22 +0000511 if (m_show_inlined_frames)
Greg Clayton1d66ef52010-08-27 18:24:16 +0000512 {
Jim Inghamc92468f2012-02-29 19:58:25 +0000513 // When inline frames are enabled we actually create all the frames in GetFramesUpTo.
Jim Inghambf97d742012-02-29 03:40:22 +0000514 frame_sp = m_frames[idx];
515 }
516 else
517 {
518 Unwind *unwinder = m_thread.GetUnwinder ();
519 if (unwinder)
Greg Clayton1d66ef52010-08-27 18:24:16 +0000520 {
Jim Inghambf97d742012-02-29 03:40:22 +0000521 addr_t pc, cfa;
522 if (unwinder->GetFrameInfoAtIndex(idx, cfa, pc))
Greg Clayton4fb08152010-08-30 18:11:35 +0000523 {
Jim Inghambf97d742012-02-29 03:40:22 +0000524 frame_sp.reset (new StackFrame (m_thread.shared_from_this(), idx, idx, cfa, pc, NULL));
525
526 Function *function = frame_sp->GetSymbolContext (eSymbolContextFunction).function;
527 if (function)
528 {
529 // When we aren't showing inline functions we always use
530 // the top most function block as the scope.
531 frame_sp->SetSymbolContextScope (&function->GetBlock(false));
532 }
533 else
534 {
535 // Set the symbol scope from the symbol regardless if it is NULL or valid.
536 frame_sp->SetSymbolContextScope (frame_sp->GetSymbolContext (eSymbolContextSymbol).symbol);
537 }
538 SetFrameAtIndex(idx, frame_sp);
Greg Clayton4fb08152010-08-30 18:11:35 +0000539 }
Greg Clayton1d66ef52010-08-27 18:24:16 +0000540 }
541 }
Greg Clayton782b9cc2010-08-25 00:35:26 +0000542 }
Chris Lattner24943d22010-06-08 16:52:24 +0000543 return frame_sp;
544}
545
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000546StackFrameSP
547StackFrameList::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx)
548{
549 // First try assuming the unwind index is the same as the frame index. The
550 // unwind index is always greater than or equal to the frame index, so it
551 // is a good place to start. If we have inlined frames we might have 5
552 // concrete frames (frame unwind indexes go from 0-4), but we might have 15
553 // frames after we make all the inlined frames. Most of the time the unwind
554 // frame index (or the concrete frame index) is the same as the frame index.
555 uint32_t frame_idx = unwind_idx;
556 StackFrameSP frame_sp (GetFrameAtIndex (frame_idx));
557 while (frame_sp)
558 {
559 if (frame_sp->GetFrameIndex() == unwind_idx)
560 break;
561 frame_sp = GetFrameAtIndex (++frame_idx);
562 }
563 return frame_sp;
564}
565
Jim Ingham5c4b1602011-03-31 00:15:49 +0000566StackFrameSP
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000567StackFrameList::GetFrameWithStackID (const StackID &stack_id)
Jim Ingham5c4b1602011-03-31 00:15:49 +0000568{
569 uint32_t frame_idx = 0;
570 StackFrameSP frame_sp;
571 do
572 {
573 frame_sp = GetFrameAtIndex (frame_idx);
574 if (frame_sp && frame_sp->GetStackID() == stack_id)
575 break;
576 frame_idx++;
577 }
578 while (frame_sp);
579 return frame_sp;
580}
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000581
Greg Clayton782b9cc2010-08-25 00:35:26 +0000582bool
Greg Clayton1d66ef52010-08-27 18:24:16 +0000583StackFrameList::SetFrameAtIndex (uint32_t idx, StackFrameSP &frame_sp)
Greg Clayton782b9cc2010-08-25 00:35:26 +0000584{
Greg Clayton1d66ef52010-08-27 18:24:16 +0000585 if (idx >= m_frames.size())
586 m_frames.resize(idx + 1);
Greg Clayton782b9cc2010-08-25 00:35:26 +0000587 // Make sure allocation succeeded by checking bounds again
Greg Clayton1d66ef52010-08-27 18:24:16 +0000588 if (idx < m_frames.size())
Greg Clayton782b9cc2010-08-25 00:35:26 +0000589 {
Greg Clayton1d66ef52010-08-27 18:24:16 +0000590 m_frames[idx] = frame_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000591 return true;
592 }
593 return false; // resize failed, out of memory?
594}
595
596uint32_t
Jim Inghamc8332952010-08-26 21:32:51 +0000597StackFrameList::GetSelectedFrameIndex () const
Chris Lattner24943d22010-06-08 16:52:24 +0000598{
599 Mutex::Locker locker (m_mutex);
Jim Inghamc8332952010-08-26 21:32:51 +0000600 return m_selected_frame_idx;
Chris Lattner24943d22010-06-08 16:52:24 +0000601}
602
603
604uint32_t
Jim Inghamc8332952010-08-26 21:32:51 +0000605StackFrameList::SetSelectedFrame (lldb_private::StackFrame *frame)
Chris Lattner24943d22010-06-08 16:52:24 +0000606{
607 Mutex::Locker locker (m_mutex);
Greg Clayton782b9cc2010-08-25 00:35:26 +0000608 const_iterator pos;
Greg Clayton1d66ef52010-08-27 18:24:16 +0000609 const_iterator begin = m_frames.begin();
610 const_iterator end = m_frames.end();
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000611 m_selected_frame_idx = 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000612 for (pos = begin; pos != end; ++pos)
613 {
614 if (pos->get() == frame)
615 {
Jim Inghamc8332952010-08-26 21:32:51 +0000616 m_selected_frame_idx = std::distance (begin, pos);
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000617 uint32_t inlined_depth = GetCurrentInlinedDepth();
618 if (inlined_depth != UINT32_MAX)
619 m_selected_frame_idx -= inlined_depth;
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000620 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000621 }
622 }
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000623 SetDefaultFileAndLineToSelectedFrame();
Jim Inghamc8332952010-08-26 21:32:51 +0000624 return m_selected_frame_idx;
Chris Lattner24943d22010-06-08 16:52:24 +0000625}
626
627// Mark a stack frame as the current frame using the frame index
Jim Inghambf97d742012-02-29 03:40:22 +0000628bool
Jim Inghamc8332952010-08-26 21:32:51 +0000629StackFrameList::SetSelectedFrameByIndex (uint32_t idx)
Chris Lattner24943d22010-06-08 16:52:24 +0000630{
631 Mutex::Locker locker (m_mutex);
Jim Inghambf97d742012-02-29 03:40:22 +0000632 StackFrameSP frame_sp (GetFrameAtIndex (idx));
633 if (frame_sp)
634 {
635 SetSelectedFrame(frame_sp.get());
636 return true;
637 }
638 else
639 return false;
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000640}
641
642void
643StackFrameList::SetDefaultFileAndLineToSelectedFrame()
644{
Greg Claytonf4124de2012-02-21 00:09:25 +0000645 if (m_thread.GetID() == m_thread.GetProcess()->GetThreadList().GetSelectedThread()->GetID())
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000646 {
Greg Clayton840a9922011-10-05 03:14:31 +0000647 StackFrameSP frame_sp (GetFrameAtIndex (GetSelectedFrameIndex()));
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000648 if (frame_sp)
649 {
Greg Clayton840a9922011-10-05 03:14:31 +0000650 SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextLineEntry);
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000651 if (sc.line_entry.file)
Greg Claytonf4124de2012-02-21 00:09:25 +0000652 m_thread.CalculateTarget()->GetSourceManager().SetDefaultFileAndLine (sc.line_entry.file,
Greg Clayton840a9922011-10-05 03:14:31 +0000653 sc.line_entry.line);
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000654 }
655 }
Chris Lattner24943d22010-06-08 16:52:24 +0000656}
657
658// The thread has been run, reset the number stack frames to zero so we can
659// determine how many frames we have lazily.
660void
661StackFrameList::Clear ()
662{
663 Mutex::Locker locker (m_mutex);
Greg Clayton1d66ef52010-08-27 18:24:16 +0000664 m_frames.clear();
Jim Inghambf97d742012-02-29 03:40:22 +0000665 m_concrete_frames_fetched = 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000666}
667
668void
669StackFrameList::InvalidateFrames (uint32_t start_idx)
670{
671 Mutex::Locker locker (m_mutex);
Greg Clayton782b9cc2010-08-25 00:35:26 +0000672 if (m_show_inlined_frames)
Chris Lattner24943d22010-06-08 16:52:24 +0000673 {
Greg Clayton782b9cc2010-08-25 00:35:26 +0000674 Clear();
675 }
676 else
677 {
Greg Clayton1d66ef52010-08-27 18:24:16 +0000678 const size_t num_frames = m_frames.size();
Greg Clayton782b9cc2010-08-25 00:35:26 +0000679 while (start_idx < num_frames)
680 {
Greg Clayton1d66ef52010-08-27 18:24:16 +0000681 m_frames[start_idx].reset();
Greg Clayton782b9cc2010-08-25 00:35:26 +0000682 ++start_idx;
683 }
Chris Lattner24943d22010-06-08 16:52:24 +0000684 }
685}
Greg Clayton5205f0b2010-09-03 17:10:42 +0000686
687void
688StackFrameList::Merge (std::auto_ptr<StackFrameList>& curr_ap, lldb::StackFrameListSP& prev_sp)
689{
Jim Ingham1b584eb2012-05-04 23:02:50 +0000690 Mutex::Locker curr_locker (curr_ap.get() ? &curr_ap->m_mutex : NULL);
691 Mutex::Locker prev_locker (prev_sp.get() ? &prev_sp->m_mutex : NULL);
Greg Clayton5205f0b2010-09-03 17:10:42 +0000692
693#if defined (DEBUG_STACK_FRAMES)
Johnny Chen0a77f902011-08-25 17:27:02 +0000694 StreamFile s(stdout, false);
Greg Clayton5205f0b2010-09-03 17:10:42 +0000695 s.PutCString("\n\nStackFrameList::Merge():\nPrev:\n");
696 if (prev_sp.get())
697 prev_sp->Dump (&s);
698 else
699 s.PutCString ("NULL");
700 s.PutCString("\nCurr:\n");
701 if (curr_ap.get())
702 curr_ap->Dump (&s);
703 else
704 s.PutCString ("NULL");
705 s.EOL();
706#endif
707
708 if (curr_ap.get() == NULL || curr_ap->GetNumFrames (false) == 0)
709 {
710#if defined (DEBUG_STACK_FRAMES)
711 s.PutCString("No current frames, leave previous frames alone...\n");
712#endif
713 curr_ap.release();
714 return;
715 }
716
717 if (prev_sp.get() == NULL || prev_sp->GetNumFrames (false) == 0)
718 {
719#if defined (DEBUG_STACK_FRAMES)
720 s.PutCString("No previous frames, so use current frames...\n");
721#endif
722 // We either don't have any previous frames, or since we have more than
723 // one current frames it means we have all the frames and can safely
724 // replace our previous frames.
725 prev_sp.reset (curr_ap.release());
726 return;
727 }
728
729 const uint32_t num_curr_frames = curr_ap->GetNumFrames (false);
730
731 if (num_curr_frames > 1)
732 {
733#if defined (DEBUG_STACK_FRAMES)
734 s.PutCString("We have more than one current frame, so use current frames...\n");
735#endif
736 // We have more than one current frames it means we have all the frames
737 // and can safely replace our previous frames.
738 prev_sp.reset (curr_ap.release());
739
740#if defined (DEBUG_STACK_FRAMES)
741 s.PutCString("\nMerged:\n");
742 prev_sp->Dump (&s);
743#endif
744 return;
745 }
746
747 StackFrameSP prev_frame_zero_sp(prev_sp->GetFrameAtIndex (0));
748 StackFrameSP curr_frame_zero_sp(curr_ap->GetFrameAtIndex (0));
749 StackID curr_stack_id (curr_frame_zero_sp->GetStackID());
750 StackID prev_stack_id (prev_frame_zero_sp->GetStackID());
751
Greg Clayton5205f0b2010-09-03 17:10:42 +0000752#if defined (DEBUG_STACK_FRAMES)
Johnny Chen0a77f902011-08-25 17:27:02 +0000753 const uint32_t num_prev_frames = prev_sp->GetNumFrames (false);
Greg Clayton5205f0b2010-09-03 17:10:42 +0000754 s.Printf("\n%u previous frames with one current frame\n", num_prev_frames);
755#endif
756
757 // We have only a single current frame
758 // Our previous stack frames only had a single frame as well...
759 if (curr_stack_id == prev_stack_id)
760 {
761#if defined (DEBUG_STACK_FRAMES)
762 s.Printf("\nPrevious frame #0 is same as current frame #0, merge the cached data\n");
763#endif
764
765 curr_frame_zero_sp->UpdateCurrentFrameFromPreviousFrame (*prev_frame_zero_sp);
766// prev_frame_zero_sp->UpdatePreviousFrameFromCurrentFrame (*curr_frame_zero_sp);
767// prev_sp->SetFrameAtIndex (0, prev_frame_zero_sp);
768 }
769 else if (curr_stack_id < prev_stack_id)
770 {
771#if defined (DEBUG_STACK_FRAMES)
772 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");
773#endif
774 prev_sp->m_frames.insert (prev_sp->m_frames.begin(), curr_frame_zero_sp);
775 }
776
777 curr_ap.release();
778
779#if defined (DEBUG_STACK_FRAMES)
780 s.PutCString("\nMerged:\n");
781 prev_sp->Dump (&s);
782#endif
783
784
785}
Jim Inghamccd584d2010-09-23 17:40:12 +0000786
787lldb::StackFrameSP
788StackFrameList::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr)
789{
790 const_iterator pos;
791 const_iterator begin = m_frames.begin();
792 const_iterator end = m_frames.end();
793 lldb::StackFrameSP ret_sp;
794
795 for (pos = begin; pos != end; ++pos)
796 {
797 if (pos->get() == stack_frame_ptr)
798 {
799 ret_sp = (*pos);
800 break;
801 }
802 }
803 return ret_sp;
804}
805
Greg Claytonabe0fed2011-04-18 08:33:37 +0000806size_t
807StackFrameList::GetStatus (Stream& strm,
808 uint32_t first_frame,
809 uint32_t num_frames,
810 bool show_frame_info,
Greg Claytona7d3dc72012-07-11 20:33:48 +0000811 uint32_t num_frames_with_source)
Greg Claytonabe0fed2011-04-18 08:33:37 +0000812{
813 size_t num_frames_displayed = 0;
814
815 if (num_frames == 0)
816 return 0;
817
818 StackFrameSP frame_sp;
819 uint32_t frame_idx = 0;
820 uint32_t last_frame;
821
822 // Don't let the last frame wrap around...
823 if (num_frames == UINT32_MAX)
824 last_frame = UINT32_MAX;
825 else
826 last_frame = first_frame + num_frames;
827
828 for (frame_idx = first_frame; frame_idx < last_frame; ++frame_idx)
829 {
830 frame_sp = GetFrameAtIndex(frame_idx);
831 if (frame_sp.get() == NULL)
832 break;
833
834 if (!frame_sp->GetStatus (strm,
835 show_frame_info,
Greg Claytona7d3dc72012-07-11 20:33:48 +0000836 num_frames_with_source > (first_frame - frame_idx)))
Greg Claytonabe0fed2011-04-18 08:33:37 +0000837 break;
838 ++num_frames_displayed;
839 }
840
841 strm.IndentLess();
842 return num_frames_displayed;
843}
844