blob: 801bbc3e352492a7393ffbf6c0aaa58fc2c0564c [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())
208 log->Printf ("ResetCurrentInlinedDepth: setting inlined depth: %d 0x%llx.\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 }
277 else
278 inlined_depth = 0;
279 }
Jim Inghambf97d742012-02-29 03:40:22 +0000280
281 StackFrameSP unwind_frame_sp;
282 do
283 {
284 uint32_t idx = m_concrete_frames_fetched++;
285 lldb::addr_t pc;
286 lldb::addr_t cfa;
287 if (idx == 0)
288 {
289 // We might have already created frame zero, only create it
290 // if we need to
291 if (m_frames.empty())
292 {
Jim Inghambf97d742012-02-29 03:40:22 +0000293 m_thread.GetRegisterContext();
Jim Inghamc92468f2012-02-29 19:58:25 +0000294 assert (m_thread.m_reg_context_sp.get());
Jim Ingham6252ea82012-03-01 02:53:40 +0000295
296 const bool success = unwinder->GetFrameInfoAtIndex(idx, cfa, pc);
297 // There shouldn't be any way not to get the frame info for frame 0.
298 // But if the unwinder can't make one, lets make one by hand with the
299 // SP as the CFA and see if that gets any further.
300 if (!success)
301 {
302 cfa = m_thread.GetRegisterContext()->GetSP();
303 pc = m_thread.GetRegisterContext()->GetPC();
304 }
305
Jim Inghambf97d742012-02-29 03:40:22 +0000306 unwind_frame_sp.reset (new StackFrame (m_thread.shared_from_this(),
307 m_frames.size(),
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000308 idx,
Jim Inghamc92468f2012-02-29 19:58:25 +0000309 m_thread.m_reg_context_sp,
Jim Ingham6252ea82012-03-01 02:53:40 +0000310 cfa,
Jim Inghamc92468f2012-02-29 19:58:25 +0000311 pc,
Jim Inghambf97d742012-02-29 03:40:22 +0000312 NULL));
313 m_frames.push_back (unwind_frame_sp);
314 }
315 else
316 {
317 unwind_frame_sp = m_frames.front();
318 cfa = unwind_frame_sp->m_id.GetCallFrameAddress();
319 }
320 }
321 else
322 {
323 const bool success = unwinder->GetFrameInfoAtIndex(idx, cfa, pc);
324 if (!success)
325 {
326 // We've gotten to the end of the stack.
327 SetAllFramesFetched();
328 break;
329 }
330 unwind_frame_sp.reset (new StackFrame (m_thread.shared_from_this(), m_frames.size(), idx, cfa, pc, NULL));
331 m_frames.push_back (unwind_frame_sp);
332 }
333
334 SymbolContext unwind_sc = unwind_frame_sp->GetSymbolContext (eSymbolContextBlock | eSymbolContextFunction);
335 Block *unwind_block = unwind_sc.block;
336 if (unwind_block)
337 {
338 Address curr_frame_address (unwind_frame_sp->GetFrameCodeAddress());
339 // Be sure to adjust the frame address to match the address
340 // that was used to lookup the symbol context above. If we are
341 // in the first concrete frame, then we lookup using the current
342 // address, else we decrement the address by one to get the correct
343 // location.
344 if (idx > 0)
345 curr_frame_address.Slide(-1);
346
347 SymbolContext next_frame_sc;
348 Address next_frame_address;
349
350 while (unwind_sc.GetParentOfInlinedScope(curr_frame_address, next_frame_sc, next_frame_address))
351 {
352 StackFrameSP frame_sp(new StackFrame (m_thread.shared_from_this(),
353 m_frames.size(),
354 idx,
355 unwind_frame_sp->GetRegisterContextSP (),
356 cfa,
357 next_frame_address,
358 &next_frame_sc));
359
360 m_frames.push_back (frame_sp);
361 unwind_sc = next_frame_sc;
362 curr_frame_address = next_frame_address;
363 }
364 }
365 } while (m_frames.size() - 1 < end_idx);
366
367 // Don't try to merge till you've calculated all the frames in this stack.
368 if (GetAllFramesFetched() && m_prev_frames_sp)
369 {
370 StackFrameList *prev_frames = m_prev_frames_sp.get();
371 StackFrameList *curr_frames = this;
Jim Ingham1949b1e2012-09-06 19:24:57 +0000372
Jim Ingham9b124c62012-09-07 23:35:54 +0000373 //curr_frames->m_current_inlined_depth = prev_frames->m_current_inlined_depth;
374 //curr_frames->m_current_inlined_pc = prev_frames->m_current_inlined_pc;
375 //printf ("GetFramesUpTo: Copying current inlined depth: %d 0x%llx.\n", curr_frames->m_current_inlined_depth, curr_frames->m_current_inlined_pc);
Jim Inghambf97d742012-02-29 03:40:22 +0000376
377#if defined (DEBUG_STACK_FRAMES)
378 s.PutCString("\nprev_frames:\n");
379 prev_frames->Dump (&s);
380 s.PutCString("\ncurr_frames:\n");
381 curr_frames->Dump (&s);
382 s.EOL();
383#endif
384 size_t curr_frame_num, prev_frame_num;
385
386 for (curr_frame_num = curr_frames->m_frames.size(), prev_frame_num = prev_frames->m_frames.size();
387 curr_frame_num > 0 && prev_frame_num > 0;
388 --curr_frame_num, --prev_frame_num)
389 {
390 const size_t curr_frame_idx = curr_frame_num-1;
391 const size_t prev_frame_idx = prev_frame_num-1;
392 StackFrameSP curr_frame_sp (curr_frames->m_frames[curr_frame_idx]);
393 StackFrameSP prev_frame_sp (prev_frames->m_frames[prev_frame_idx]);
394
395#if defined (DEBUG_STACK_FRAMES)
396 s.Printf("\n\nCurr frame #%u ", curr_frame_idx);
397 if (curr_frame_sp)
398 curr_frame_sp->Dump (&s, true, false);
399 else
400 s.PutCString("NULL");
401 s.Printf("\nPrev frame #%u ", prev_frame_idx);
402 if (prev_frame_sp)
403 prev_frame_sp->Dump (&s, true, false);
404 else
405 s.PutCString("NULL");
406#endif
407
408 StackFrame *curr_frame = curr_frame_sp.get();
409 StackFrame *prev_frame = prev_frame_sp.get();
410
411 if (curr_frame == NULL || prev_frame == NULL)
412 break;
413
414 // Check the stack ID to make sure they are equal
415 if (curr_frame->GetStackID() != prev_frame->GetStackID())
416 break;
417
418 prev_frame->UpdatePreviousFrameFromCurrentFrame (*curr_frame);
419 // Now copy the fixed up previous frame into the current frames
420 // so the pointer doesn't change
421 m_frames[curr_frame_idx] = prev_frame_sp;
422 //curr_frame->UpdateCurrentFrameFromPreviousFrame (*prev_frame);
423
424#if defined (DEBUG_STACK_FRAMES)
425 s.Printf("\n Copying previous frame to current frame");
426#endif
427 }
428 // We are done with the old stack frame list, we can release it now
429 m_prev_frames_sp.reset();
430 }
431
432#if defined (DEBUG_STACK_FRAMES)
433 s.PutCString("\n\nNew frames:\n");
434 Dump (&s);
435 s.EOL();
436#endif
437 }
438 else
439 {
440 if (end_idx < m_concrete_frames_fetched)
441 return;
442
443 uint32_t num_frames = unwinder->GetFramesUpTo(end_idx);
444 if (num_frames <= end_idx + 1)
445 {
446 //Done unwinding.
447 m_concrete_frames_fetched = UINT32_MAX;
448 }
449 m_frames.resize(num_frames);
450 }
451}
Chris Lattner24943d22010-06-08 16:52:24 +0000452
453uint32_t
Greg Clayton5205f0b2010-09-03 17:10:42 +0000454StackFrameList::GetNumFrames (bool can_create)
Chris Lattner24943d22010-06-08 16:52:24 +0000455{
456 Mutex::Locker locker (m_mutex);
Greg Clayton782b9cc2010-08-25 00:35:26 +0000457
Jim Inghambf97d742012-02-29 03:40:22 +0000458 if (can_create)
459 GetFramesUpTo (UINT32_MAX);
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000460
461 uint32_t inlined_depth = GetCurrentInlinedDepth();
462 if (inlined_depth == UINT32_MAX)
463 return m_frames.size();
464 else
465 return m_frames.size() - inlined_depth;
Greg Clayton1d66ef52010-08-27 18:24:16 +0000466}
467
468void
469StackFrameList::Dump (Stream *s)
470{
471 if (s == NULL)
472 return;
473 Mutex::Locker locker (m_mutex);
474
475 const_iterator pos, begin = m_frames.begin(), end = m_frames.end();
476 for (pos = begin; pos != end; ++pos)
Greg Clayton782b9cc2010-08-25 00:35:26 +0000477 {
Greg Clayton1d66ef52010-08-27 18:24:16 +0000478 StackFrame *frame = (*pos).get();
479 s->Printf("%p: ", frame);
480 if (frame)
Greg Clayton4fb08152010-08-30 18:11:35 +0000481 {
482 frame->GetStackID().Dump (s);
Greg Claytona830adb2010-10-04 01:05:56 +0000483 frame->DumpUsingSettingsFormat (s);
Greg Clayton4fb08152010-08-30 18:11:35 +0000484 }
Greg Clayton1d66ef52010-08-27 18:24:16 +0000485 else
Greg Clayton3e4238d2011-11-04 03:34:56 +0000486 s->Printf("frame #%u", (uint32_t)std::distance (begin, pos));
Greg Clayton1d66ef52010-08-27 18:24:16 +0000487 s->EOL();
Greg Clayton782b9cc2010-08-25 00:35:26 +0000488 }
Greg Clayton1d66ef52010-08-27 18:24:16 +0000489 s->EOL();
Chris Lattner24943d22010-06-08 16:52:24 +0000490}
491
Chris Lattner24943d22010-06-08 16:52:24 +0000492StackFrameSP
Greg Clayton782b9cc2010-08-25 00:35:26 +0000493StackFrameList::GetFrameAtIndex (uint32_t idx)
Chris Lattner24943d22010-06-08 16:52:24 +0000494{
495 StackFrameSP frame_sp;
Greg Clayton1d66ef52010-08-27 18:24:16 +0000496 Mutex::Locker locker (m_mutex);
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000497 uint32_t inlined_depth = GetCurrentInlinedDepth();
498 if (inlined_depth != UINT32_MAX)
499 idx += inlined_depth;
500
Greg Clayton1d66ef52010-08-27 18:24:16 +0000501 if (idx < m_frames.size())
502 frame_sp = m_frames[idx];
503
504 if (frame_sp)
505 return frame_sp;
Greg Claytonf40e3082010-08-26 02:28:22 +0000506
Jim Inghamc92468f2012-02-29 19:58:25 +0000507 // GetFramesUpTo will fill m_frames with as many frames as you asked for,
508 // if there are that many. If there weren't then you asked for too many
509 // frames.
Jim Inghambf97d742012-02-29 03:40:22 +0000510 GetFramesUpTo (idx);
511 if (idx < m_frames.size())
Greg Clayton1d66ef52010-08-27 18:24:16 +0000512 {
Jim Inghambf97d742012-02-29 03:40:22 +0000513 if (m_show_inlined_frames)
Greg Clayton1d66ef52010-08-27 18:24:16 +0000514 {
Jim Inghamc92468f2012-02-29 19:58:25 +0000515 // When inline frames are enabled we actually create all the frames in GetFramesUpTo.
Jim Inghambf97d742012-02-29 03:40:22 +0000516 frame_sp = m_frames[idx];
517 }
518 else
519 {
520 Unwind *unwinder = m_thread.GetUnwinder ();
521 if (unwinder)
Greg Clayton1d66ef52010-08-27 18:24:16 +0000522 {
Jim Inghambf97d742012-02-29 03:40:22 +0000523 addr_t pc, cfa;
524 if (unwinder->GetFrameInfoAtIndex(idx, cfa, pc))
Greg Clayton4fb08152010-08-30 18:11:35 +0000525 {
Jim Inghambf97d742012-02-29 03:40:22 +0000526 frame_sp.reset (new StackFrame (m_thread.shared_from_this(), idx, idx, cfa, pc, NULL));
527
528 Function *function = frame_sp->GetSymbolContext (eSymbolContextFunction).function;
529 if (function)
530 {
531 // When we aren't showing inline functions we always use
532 // the top most function block as the scope.
533 frame_sp->SetSymbolContextScope (&function->GetBlock(false));
534 }
535 else
536 {
537 // Set the symbol scope from the symbol regardless if it is NULL or valid.
538 frame_sp->SetSymbolContextScope (frame_sp->GetSymbolContext (eSymbolContextSymbol).symbol);
539 }
540 SetFrameAtIndex(idx, frame_sp);
Greg Clayton4fb08152010-08-30 18:11:35 +0000541 }
Greg Clayton1d66ef52010-08-27 18:24:16 +0000542 }
543 }
Greg Clayton782b9cc2010-08-25 00:35:26 +0000544 }
Chris Lattner24943d22010-06-08 16:52:24 +0000545 return frame_sp;
546}
547
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000548StackFrameSP
549StackFrameList::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx)
550{
551 // First try assuming the unwind index is the same as the frame index. The
552 // unwind index is always greater than or equal to the frame index, so it
553 // is a good place to start. If we have inlined frames we might have 5
554 // concrete frames (frame unwind indexes go from 0-4), but we might have 15
555 // frames after we make all the inlined frames. Most of the time the unwind
556 // frame index (or the concrete frame index) is the same as the frame index.
557 uint32_t frame_idx = unwind_idx;
558 StackFrameSP frame_sp (GetFrameAtIndex (frame_idx));
559 while (frame_sp)
560 {
561 if (frame_sp->GetFrameIndex() == unwind_idx)
562 break;
563 frame_sp = GetFrameAtIndex (++frame_idx);
564 }
565 return frame_sp;
566}
567
Jim Ingham5c4b1602011-03-31 00:15:49 +0000568StackFrameSP
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000569StackFrameList::GetFrameWithStackID (const StackID &stack_id)
Jim Ingham5c4b1602011-03-31 00:15:49 +0000570{
571 uint32_t frame_idx = 0;
572 StackFrameSP frame_sp;
573 do
574 {
575 frame_sp = GetFrameAtIndex (frame_idx);
576 if (frame_sp && frame_sp->GetStackID() == stack_id)
577 break;
578 frame_idx++;
579 }
580 while (frame_sp);
581 return frame_sp;
582}
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000583
Greg Clayton782b9cc2010-08-25 00:35:26 +0000584bool
Greg Clayton1d66ef52010-08-27 18:24:16 +0000585StackFrameList::SetFrameAtIndex (uint32_t idx, StackFrameSP &frame_sp)
Greg Clayton782b9cc2010-08-25 00:35:26 +0000586{
Greg Clayton1d66ef52010-08-27 18:24:16 +0000587 if (idx >= m_frames.size())
588 m_frames.resize(idx + 1);
Greg Clayton782b9cc2010-08-25 00:35:26 +0000589 // Make sure allocation succeeded by checking bounds again
Greg Clayton1d66ef52010-08-27 18:24:16 +0000590 if (idx < m_frames.size())
Greg Clayton782b9cc2010-08-25 00:35:26 +0000591 {
Greg Clayton1d66ef52010-08-27 18:24:16 +0000592 m_frames[idx] = frame_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000593 return true;
594 }
595 return false; // resize failed, out of memory?
596}
597
598uint32_t
Jim Inghamc8332952010-08-26 21:32:51 +0000599StackFrameList::GetSelectedFrameIndex () const
Chris Lattner24943d22010-06-08 16:52:24 +0000600{
601 Mutex::Locker locker (m_mutex);
Jim Inghamc8332952010-08-26 21:32:51 +0000602 return m_selected_frame_idx;
Chris Lattner24943d22010-06-08 16:52:24 +0000603}
604
605
606uint32_t
Jim Inghamc8332952010-08-26 21:32:51 +0000607StackFrameList::SetSelectedFrame (lldb_private::StackFrame *frame)
Chris Lattner24943d22010-06-08 16:52:24 +0000608{
609 Mutex::Locker locker (m_mutex);
Greg Clayton782b9cc2010-08-25 00:35:26 +0000610 const_iterator pos;
Greg Clayton1d66ef52010-08-27 18:24:16 +0000611 const_iterator begin = m_frames.begin();
612 const_iterator end = m_frames.end();
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000613 m_selected_frame_idx = 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000614 for (pos = begin; pos != end; ++pos)
615 {
616 if (pos->get() == frame)
617 {
Jim Inghamc8332952010-08-26 21:32:51 +0000618 m_selected_frame_idx = std::distance (begin, pos);
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000619 uint32_t inlined_depth = GetCurrentInlinedDepth();
620 if (inlined_depth != UINT32_MAX)
621 m_selected_frame_idx -= inlined_depth;
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000622 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000623 }
624 }
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000625 SetDefaultFileAndLineToSelectedFrame();
Jim Inghamc8332952010-08-26 21:32:51 +0000626 return m_selected_frame_idx;
Chris Lattner24943d22010-06-08 16:52:24 +0000627}
628
629// Mark a stack frame as the current frame using the frame index
Jim Inghambf97d742012-02-29 03:40:22 +0000630bool
Jim Inghamc8332952010-08-26 21:32:51 +0000631StackFrameList::SetSelectedFrameByIndex (uint32_t idx)
Chris Lattner24943d22010-06-08 16:52:24 +0000632{
633 Mutex::Locker locker (m_mutex);
Jim Inghambf97d742012-02-29 03:40:22 +0000634 StackFrameSP frame_sp (GetFrameAtIndex (idx));
635 if (frame_sp)
636 {
637 SetSelectedFrame(frame_sp.get());
638 return true;
639 }
640 else
641 return false;
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000642}
643
644void
645StackFrameList::SetDefaultFileAndLineToSelectedFrame()
646{
Greg Claytonf4124de2012-02-21 00:09:25 +0000647 if (m_thread.GetID() == m_thread.GetProcess()->GetThreadList().GetSelectedThread()->GetID())
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000648 {
Greg Clayton840a9922011-10-05 03:14:31 +0000649 StackFrameSP frame_sp (GetFrameAtIndex (GetSelectedFrameIndex()));
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000650 if (frame_sp)
651 {
Greg Clayton840a9922011-10-05 03:14:31 +0000652 SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextLineEntry);
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000653 if (sc.line_entry.file)
Greg Claytonf4124de2012-02-21 00:09:25 +0000654 m_thread.CalculateTarget()->GetSourceManager().SetDefaultFileAndLine (sc.line_entry.file,
Greg Clayton840a9922011-10-05 03:14:31 +0000655 sc.line_entry.line);
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000656 }
657 }
Chris Lattner24943d22010-06-08 16:52:24 +0000658}
659
660// The thread has been run, reset the number stack frames to zero so we can
661// determine how many frames we have lazily.
662void
663StackFrameList::Clear ()
664{
665 Mutex::Locker locker (m_mutex);
Greg Clayton1d66ef52010-08-27 18:24:16 +0000666 m_frames.clear();
Jim Inghambf97d742012-02-29 03:40:22 +0000667 m_concrete_frames_fetched = 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000668}
669
670void
671StackFrameList::InvalidateFrames (uint32_t start_idx)
672{
673 Mutex::Locker locker (m_mutex);
Greg Clayton782b9cc2010-08-25 00:35:26 +0000674 if (m_show_inlined_frames)
Chris Lattner24943d22010-06-08 16:52:24 +0000675 {
Greg Clayton782b9cc2010-08-25 00:35:26 +0000676 Clear();
677 }
678 else
679 {
Greg Clayton1d66ef52010-08-27 18:24:16 +0000680 const size_t num_frames = m_frames.size();
Greg Clayton782b9cc2010-08-25 00:35:26 +0000681 while (start_idx < num_frames)
682 {
Greg Clayton1d66ef52010-08-27 18:24:16 +0000683 m_frames[start_idx].reset();
Greg Clayton782b9cc2010-08-25 00:35:26 +0000684 ++start_idx;
685 }
Chris Lattner24943d22010-06-08 16:52:24 +0000686 }
687}
Greg Clayton5205f0b2010-09-03 17:10:42 +0000688
689void
690StackFrameList::Merge (std::auto_ptr<StackFrameList>& curr_ap, lldb::StackFrameListSP& prev_sp)
691{
Jim Ingham1b584eb2012-05-04 23:02:50 +0000692 Mutex::Locker curr_locker (curr_ap.get() ? &curr_ap->m_mutex : NULL);
693 Mutex::Locker prev_locker (prev_sp.get() ? &prev_sp->m_mutex : NULL);
Greg Clayton5205f0b2010-09-03 17:10:42 +0000694
695#if defined (DEBUG_STACK_FRAMES)
Johnny Chen0a77f902011-08-25 17:27:02 +0000696 StreamFile s(stdout, false);
Greg Clayton5205f0b2010-09-03 17:10:42 +0000697 s.PutCString("\n\nStackFrameList::Merge():\nPrev:\n");
698 if (prev_sp.get())
699 prev_sp->Dump (&s);
700 else
701 s.PutCString ("NULL");
702 s.PutCString("\nCurr:\n");
703 if (curr_ap.get())
704 curr_ap->Dump (&s);
705 else
706 s.PutCString ("NULL");
707 s.EOL();
708#endif
709
710 if (curr_ap.get() == NULL || curr_ap->GetNumFrames (false) == 0)
711 {
712#if defined (DEBUG_STACK_FRAMES)
713 s.PutCString("No current frames, leave previous frames alone...\n");
714#endif
715 curr_ap.release();
716 return;
717 }
718
719 if (prev_sp.get() == NULL || prev_sp->GetNumFrames (false) == 0)
720 {
721#if defined (DEBUG_STACK_FRAMES)
722 s.PutCString("No previous frames, so use current frames...\n");
723#endif
724 // We either don't have any previous frames, or since we have more than
725 // one current frames it means we have all the frames and can safely
726 // replace our previous frames.
727 prev_sp.reset (curr_ap.release());
728 return;
729 }
730
731 const uint32_t num_curr_frames = curr_ap->GetNumFrames (false);
732
733 if (num_curr_frames > 1)
734 {
735#if defined (DEBUG_STACK_FRAMES)
736 s.PutCString("We have more than one current frame, so use current frames...\n");
737#endif
738 // We have more than one current frames it means we have all the frames
739 // and can safely replace our previous frames.
740 prev_sp.reset (curr_ap.release());
741
742#if defined (DEBUG_STACK_FRAMES)
743 s.PutCString("\nMerged:\n");
744 prev_sp->Dump (&s);
745#endif
746 return;
747 }
748
749 StackFrameSP prev_frame_zero_sp(prev_sp->GetFrameAtIndex (0));
750 StackFrameSP curr_frame_zero_sp(curr_ap->GetFrameAtIndex (0));
751 StackID curr_stack_id (curr_frame_zero_sp->GetStackID());
752 StackID prev_stack_id (prev_frame_zero_sp->GetStackID());
753
Greg Clayton5205f0b2010-09-03 17:10:42 +0000754#if defined (DEBUG_STACK_FRAMES)
Johnny Chen0a77f902011-08-25 17:27:02 +0000755 const uint32_t num_prev_frames = prev_sp->GetNumFrames (false);
Greg Clayton5205f0b2010-09-03 17:10:42 +0000756 s.Printf("\n%u previous frames with one current frame\n", num_prev_frames);
757#endif
758
759 // We have only a single current frame
760 // Our previous stack frames only had a single frame as well...
761 if (curr_stack_id == prev_stack_id)
762 {
763#if defined (DEBUG_STACK_FRAMES)
764 s.Printf("\nPrevious frame #0 is same as current frame #0, merge the cached data\n");
765#endif
766
767 curr_frame_zero_sp->UpdateCurrentFrameFromPreviousFrame (*prev_frame_zero_sp);
768// prev_frame_zero_sp->UpdatePreviousFrameFromCurrentFrame (*curr_frame_zero_sp);
769// prev_sp->SetFrameAtIndex (0, prev_frame_zero_sp);
770 }
771 else if (curr_stack_id < prev_stack_id)
772 {
773#if defined (DEBUG_STACK_FRAMES)
774 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");
775#endif
776 prev_sp->m_frames.insert (prev_sp->m_frames.begin(), curr_frame_zero_sp);
777 }
778
779 curr_ap.release();
780
781#if defined (DEBUG_STACK_FRAMES)
782 s.PutCString("\nMerged:\n");
783 prev_sp->Dump (&s);
784#endif
785
786
787}
Jim Inghamccd584d2010-09-23 17:40:12 +0000788
789lldb::StackFrameSP
790StackFrameList::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr)
791{
792 const_iterator pos;
793 const_iterator begin = m_frames.begin();
794 const_iterator end = m_frames.end();
795 lldb::StackFrameSP ret_sp;
796
797 for (pos = begin; pos != end; ++pos)
798 {
799 if (pos->get() == stack_frame_ptr)
800 {
801 ret_sp = (*pos);
802 break;
803 }
804 }
805 return ret_sp;
806}
807
Greg Claytonabe0fed2011-04-18 08:33:37 +0000808size_t
809StackFrameList::GetStatus (Stream& strm,
810 uint32_t first_frame,
811 uint32_t num_frames,
812 bool show_frame_info,
Greg Claytona7d3dc72012-07-11 20:33:48 +0000813 uint32_t num_frames_with_source)
Greg Claytonabe0fed2011-04-18 08:33:37 +0000814{
815 size_t num_frames_displayed = 0;
816
817 if (num_frames == 0)
818 return 0;
819
820 StackFrameSP frame_sp;
821 uint32_t frame_idx = 0;
822 uint32_t last_frame;
823
824 // Don't let the last frame wrap around...
825 if (num_frames == UINT32_MAX)
826 last_frame = UINT32_MAX;
827 else
828 last_frame = first_frame + num_frames;
829
830 for (frame_idx = first_frame; frame_idx < last_frame; ++frame_idx)
831 {
832 frame_sp = GetFrameAtIndex(frame_idx);
833 if (frame_sp.get() == NULL)
834 break;
835
836 if (!frame_sp->GetStatus (strm,
837 show_frame_info,
Greg Claytona7d3dc72012-07-11 20:33:48 +0000838 num_frames_with_source > (first_frame - frame_idx)))
Greg Claytonabe0fed2011-04-18 08:33:37 +0000839 break;
840 ++num_frames_displayed;
841 }
842
843 strm.IndentLess();
844 return num_frames_displayed;
845}
846