blob: 39aa1bc1e906da56cad30ababf719e1b14b9ae97 [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{
Greg Clayton226484d2013-03-28 18:41:44 +000068 // Call clear since this takes a lock and clears the stack frame list
69 // in case another thread is currently using this stack frame list
70 Clear();
Chris Lattner24943d22010-06-08 16:52:24 +000071}
72
Jim Inghambf97d742012-02-29 03:40:22 +000073void
Jim Ingham0c8fa2d2012-09-01 01:02:41 +000074StackFrameList::CalculateCurrentInlinedDepth()
75{
76 uint32_t cur_inlined_depth = GetCurrentInlinedDepth();
77 if (cur_inlined_depth == UINT32_MAX)
78 {
79 ResetCurrentInlinedDepth();
80 }
81}
82
83uint32_t
84StackFrameList::GetCurrentInlinedDepth ()
85{
Jim Ingham9b124c62012-09-07 23:35:54 +000086 if (m_show_inlined_frames && m_current_inlined_pc != LLDB_INVALID_ADDRESS)
Jim Ingham0c8fa2d2012-09-01 01:02:41 +000087 {
88 lldb::addr_t cur_pc = m_thread.GetRegisterContext()->GetPC();
89 if (cur_pc != m_current_inlined_pc)
90 {
91 m_current_inlined_pc = LLDB_INVALID_ADDRESS;
92 m_current_inlined_depth = UINT32_MAX;
Greg Clayton952e9dc2013-03-27 23:08:40 +000093 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham9b124c62012-09-07 23:35:54 +000094 if (log && log->GetVerbose())
95 log->Printf ("GetCurrentInlinedDepth: invalidating current inlined depth.\n");
Jim Ingham0c8fa2d2012-09-01 01:02:41 +000096 }
97 return m_current_inlined_depth;
98 }
99 else
100 {
101 return UINT32_MAX;
102 }
103}
104
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000105void
106StackFrameList::ResetCurrentInlinedDepth ()
107{
Jim Ingham1cc0c052012-09-05 21:14:28 +0000108 if (m_show_inlined_frames)
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000109 {
110 GetFramesUpTo(0);
111 if (!m_frames[0]->IsInlined())
112 {
113 m_current_inlined_depth = UINT32_MAX;
114 m_current_inlined_pc = LLDB_INVALID_ADDRESS;
Greg Clayton952e9dc2013-03-27 23:08:40 +0000115 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham9b124c62012-09-07 23:35:54 +0000116 if (log && log->GetVerbose())
117 log->Printf ("ResetCurrentInlinedDepth: Invalidating current inlined depth.\n");
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000118 }
119 else
120 {
121 // We only need to do something special about inlined blocks when we
122 // are at the beginning of an inlined function:
123 // FIXME: We probably also have to do something special if the PC is at the END
124 // of an inlined function, which coincides with the end of either its containing
125 // function or another inlined function.
126
127 lldb::addr_t curr_pc = m_thread.GetRegisterContext()->GetPC();
128 Block *block_ptr = m_frames[0]->GetFrameBlock();
129 if (block_ptr)
130 {
131 Address pc_as_address;
132 pc_as_address.SetLoadAddress(curr_pc, &(m_thread.GetProcess()->GetTarget()));
133 AddressRange containing_range;
134 if (block_ptr->GetRangeContainingAddress(pc_as_address, containing_range))
135 {
136 if (pc_as_address == containing_range.GetBaseAddress())
137 {
138 // If we got here because of a breakpoint hit, then set the inlined depth depending on where
139 // the breakpoint was set.
140 // If we got here because of a crash, then set the inlined depth to the deepest most block.
141 // Otherwise, we stopped here naturally as the result of a step, so set ourselves in the
142 // containing frame of the whole set of nested inlines, so the user can then "virtually"
143 // step into the frames one by one, or next over the whole mess.
144 // Note: We don't have to handle being somewhere in the middle of the stack here, since
145 // ResetCurrentInlinedDepth doesn't get called if there is a valid inlined depth set.
146 StopInfoSP stop_info_sp = m_thread.GetStopInfo();
147 if (stop_info_sp)
148 {
149 switch (stop_info_sp->GetStopReason())
150 {
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000151 case eStopReasonWatchpoint:
152 case eStopReasonException:
Greg Clayton0bce9a22012-12-05 00:16:59 +0000153 case eStopReasonExec:
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000154 case eStopReasonSignal:
155 // In all these cases we want to stop in the deepest most frame.
156 m_current_inlined_pc = curr_pc;
157 m_current_inlined_depth = 0;
158 break;
Jim Ingham74d08f12012-09-07 01:11:08 +0000159 case eStopReasonBreakpoint:
160 {
161 // FIXME: Figure out what this break point is doing, and set the inline depth
162 // appropriately. Be careful to take into account breakpoints that implement
163 // step over prologue, since that should do the default calculation.
Jim Ingham64a41e22012-09-08 00:26:49 +0000164 // For now, if the breakpoints corresponding to this hit are all internal,
165 // I set the stop location to the top of the inlined stack, since that will make
166 // things like stepping over prologues work right. But if there are any non-internal
167 // breakpoints I do to the bottom of the stack, since that was the old behavior.
168 uint32_t bp_site_id = stop_info_sp->GetValue();
169 BreakpointSiteSP bp_site_sp(m_thread.GetProcess()->GetBreakpointSiteList().FindByID(bp_site_id));
170 bool all_internal = true;
171 if (bp_site_sp)
172 {
173 uint32_t num_owners = bp_site_sp->GetNumberOfOwners();
174 for (uint32_t i = 0; i < num_owners; i++)
175 {
176 Breakpoint &bp_ref = bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint();
177 if (!bp_ref.IsInternal())
178 {
179 all_internal = false;
180 }
181 }
182 }
183 if (!all_internal)
184 {
185 m_current_inlined_pc = curr_pc;
186 m_current_inlined_depth = 0;
187 break;
188 }
Jim Ingham74d08f12012-09-07 01:11:08 +0000189 }
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000190 default:
191 {
192 // Otherwise, we should set ourselves at the container of the inlining, so that the
193 // user can descend into them.
194 // So first we check whether we have more than one inlined block sharing this PC:
195 int num_inlined_functions = 0;
196
197 for (Block *container_ptr = block_ptr->GetInlinedParent();
198 container_ptr != NULL;
199 container_ptr = container_ptr->GetInlinedParent())
200 {
201 if (!container_ptr->GetRangeContainingAddress(pc_as_address, containing_range))
202 break;
203 if (pc_as_address != containing_range.GetBaseAddress())
204 break;
205
206 num_inlined_functions++;
207 }
208 m_current_inlined_pc = curr_pc;
209 m_current_inlined_depth = num_inlined_functions + 1;
Greg Clayton952e9dc2013-03-27 23:08:40 +0000210 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham9b124c62012-09-07 23:35:54 +0000211 if (log && log->GetVerbose())
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000212 log->Printf ("ResetCurrentInlinedDepth: setting inlined depth: %d 0x%" PRIx64 ".\n", m_current_inlined_depth, curr_pc);
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000213
214 }
215 break;
216 }
217 }
218 }
219 }
220 }
221 }
222 }
223}
224
225bool
226StackFrameList::DecrementCurrentInlinedDepth ()
227{
228 if (m_show_inlined_frames)
229 {
230 uint32_t current_inlined_depth = GetCurrentInlinedDepth();
231 if (current_inlined_depth != UINT32_MAX)
232 {
233 if (current_inlined_depth > 0)
Jim Ingham1949b1e2012-09-06 19:24:57 +0000234 {
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000235 m_current_inlined_depth--;
Jim Ingham1949b1e2012-09-06 19:24:57 +0000236 return true;
237 }
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000238 }
239 }
240 return false;
241}
242
243void
Jim Ingham9b124c62012-09-07 23:35:54 +0000244StackFrameList::SetCurrentInlinedDepth (uint32_t new_depth)
245{
246 m_current_inlined_depth = new_depth;
247 if (new_depth == UINT32_MAX)
248 m_current_inlined_pc = LLDB_INVALID_ADDRESS;
249 else
250 m_current_inlined_pc = m_thread.GetRegisterContext()->GetPC();
251}
252
253void
Jim Inghambf97d742012-02-29 03:40:22 +0000254StackFrameList::GetFramesUpTo(uint32_t end_idx)
255{
Enrico Granata41b5bfe2013-03-15 17:25:04 +0000256 // this makes sure we do not fetch frames for an invalid thread
257 if (m_thread.IsValid() == false)
258 return;
259
Jim Inghambf97d742012-02-29 03:40:22 +0000260 // We've already gotten more frames than asked for, or we've already finished unwinding, return.
261 if (m_frames.size() > end_idx || GetAllFramesFetched())
262 return;
263
264 Unwind *unwinder = m_thread.GetUnwinder ();
265
266 if (m_show_inlined_frames)
267 {
268#if defined (DEBUG_STACK_FRAMES)
269 StreamFile s(stdout, false);
270#endif
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000271 // If we are hiding some frames from the outside world, we need to add those onto the total count of
272 // 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 +0000273 // get the first concrete frame and all the inlined frames below it... And of course, if end_idx is
274 // UINT32_MAX that means get all, so just do that...
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000275
276 uint32_t inlined_depth = 0;
Jim Ingham9b124c62012-09-07 23:35:54 +0000277 if (end_idx > 0 && end_idx != UINT32_MAX)
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000278 {
279 inlined_depth = GetCurrentInlinedDepth();
280 if (inlined_depth != UINT32_MAX)
281 {
282 if (end_idx > 0)
283 end_idx += inlined_depth;
284 }
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000285 }
Jim Inghambf97d742012-02-29 03:40:22 +0000286
287 StackFrameSP unwind_frame_sp;
288 do
289 {
290 uint32_t idx = m_concrete_frames_fetched++;
291 lldb::addr_t pc;
292 lldb::addr_t cfa;
293 if (idx == 0)
294 {
295 // We might have already created frame zero, only create it
296 // if we need to
297 if (m_frames.empty())
298 {
Jim Inghambf97d742012-02-29 03:40:22 +0000299 m_thread.GetRegisterContext();
Jim Inghamc92468f2012-02-29 19:58:25 +0000300 assert (m_thread.m_reg_context_sp.get());
Jim Ingham6252ea82012-03-01 02:53:40 +0000301
302 const bool success = unwinder->GetFrameInfoAtIndex(idx, cfa, pc);
303 // There shouldn't be any way not to get the frame info for frame 0.
304 // But if the unwinder can't make one, lets make one by hand with the
305 // SP as the CFA and see if that gets any further.
306 if (!success)
307 {
308 cfa = m_thread.GetRegisterContext()->GetSP();
309 pc = m_thread.GetRegisterContext()->GetPC();
310 }
311
Jim Inghambf97d742012-02-29 03:40:22 +0000312 unwind_frame_sp.reset (new StackFrame (m_thread.shared_from_this(),
313 m_frames.size(),
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000314 idx,
Jim Inghamc92468f2012-02-29 19:58:25 +0000315 m_thread.m_reg_context_sp,
Jim Ingham6252ea82012-03-01 02:53:40 +0000316 cfa,
Jim Inghamc92468f2012-02-29 19:58:25 +0000317 pc,
Jim Inghambf97d742012-02-29 03:40:22 +0000318 NULL));
319 m_frames.push_back (unwind_frame_sp);
320 }
321 else
322 {
323 unwind_frame_sp = m_frames.front();
324 cfa = unwind_frame_sp->m_id.GetCallFrameAddress();
325 }
326 }
327 else
328 {
329 const bool success = unwinder->GetFrameInfoAtIndex(idx, cfa, pc);
330 if (!success)
331 {
332 // We've gotten to the end of the stack.
333 SetAllFramesFetched();
334 break;
335 }
336 unwind_frame_sp.reset (new StackFrame (m_thread.shared_from_this(), m_frames.size(), idx, cfa, pc, NULL));
337 m_frames.push_back (unwind_frame_sp);
338 }
339
340 SymbolContext unwind_sc = unwind_frame_sp->GetSymbolContext (eSymbolContextBlock | eSymbolContextFunction);
341 Block *unwind_block = unwind_sc.block;
342 if (unwind_block)
343 {
344 Address curr_frame_address (unwind_frame_sp->GetFrameCodeAddress());
345 // Be sure to adjust the frame address to match the address
346 // that was used to lookup the symbol context above. If we are
347 // in the first concrete frame, then we lookup using the current
348 // address, else we decrement the address by one to get the correct
349 // location.
350 if (idx > 0)
351 curr_frame_address.Slide(-1);
352
353 SymbolContext next_frame_sc;
354 Address next_frame_address;
355
356 while (unwind_sc.GetParentOfInlinedScope(curr_frame_address, next_frame_sc, next_frame_address))
357 {
358 StackFrameSP frame_sp(new StackFrame (m_thread.shared_from_this(),
359 m_frames.size(),
360 idx,
361 unwind_frame_sp->GetRegisterContextSP (),
362 cfa,
363 next_frame_address,
364 &next_frame_sc));
365
366 m_frames.push_back (frame_sp);
367 unwind_sc = next_frame_sc;
368 curr_frame_address = next_frame_address;
369 }
370 }
371 } while (m_frames.size() - 1 < end_idx);
372
373 // Don't try to merge till you've calculated all the frames in this stack.
374 if (GetAllFramesFetched() && m_prev_frames_sp)
375 {
376 StackFrameList *prev_frames = m_prev_frames_sp.get();
377 StackFrameList *curr_frames = this;
Jim Ingham1949b1e2012-09-06 19:24:57 +0000378
Jim Ingham9b124c62012-09-07 23:35:54 +0000379 //curr_frames->m_current_inlined_depth = prev_frames->m_current_inlined_depth;
380 //curr_frames->m_current_inlined_pc = prev_frames->m_current_inlined_pc;
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000381 //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 +0000382
383#if defined (DEBUG_STACK_FRAMES)
384 s.PutCString("\nprev_frames:\n");
385 prev_frames->Dump (&s);
386 s.PutCString("\ncurr_frames:\n");
387 curr_frames->Dump (&s);
388 s.EOL();
389#endif
390 size_t curr_frame_num, prev_frame_num;
391
392 for (curr_frame_num = curr_frames->m_frames.size(), prev_frame_num = prev_frames->m_frames.size();
393 curr_frame_num > 0 && prev_frame_num > 0;
394 --curr_frame_num, --prev_frame_num)
395 {
396 const size_t curr_frame_idx = curr_frame_num-1;
397 const size_t prev_frame_idx = prev_frame_num-1;
398 StackFrameSP curr_frame_sp (curr_frames->m_frames[curr_frame_idx]);
399 StackFrameSP prev_frame_sp (prev_frames->m_frames[prev_frame_idx]);
400
401#if defined (DEBUG_STACK_FRAMES)
402 s.Printf("\n\nCurr frame #%u ", curr_frame_idx);
403 if (curr_frame_sp)
404 curr_frame_sp->Dump (&s, true, false);
405 else
406 s.PutCString("NULL");
407 s.Printf("\nPrev frame #%u ", prev_frame_idx);
408 if (prev_frame_sp)
409 prev_frame_sp->Dump (&s, true, false);
410 else
411 s.PutCString("NULL");
412#endif
413
414 StackFrame *curr_frame = curr_frame_sp.get();
415 StackFrame *prev_frame = prev_frame_sp.get();
416
417 if (curr_frame == NULL || prev_frame == NULL)
418 break;
419
420 // Check the stack ID to make sure they are equal
421 if (curr_frame->GetStackID() != prev_frame->GetStackID())
422 break;
423
424 prev_frame->UpdatePreviousFrameFromCurrentFrame (*curr_frame);
425 // Now copy the fixed up previous frame into the current frames
426 // so the pointer doesn't change
427 m_frames[curr_frame_idx] = prev_frame_sp;
428 //curr_frame->UpdateCurrentFrameFromPreviousFrame (*prev_frame);
429
430#if defined (DEBUG_STACK_FRAMES)
431 s.Printf("\n Copying previous frame to current frame");
432#endif
433 }
434 // We are done with the old stack frame list, we can release it now
435 m_prev_frames_sp.reset();
436 }
437
438#if defined (DEBUG_STACK_FRAMES)
439 s.PutCString("\n\nNew frames:\n");
440 Dump (&s);
441 s.EOL();
442#endif
443 }
444 else
445 {
446 if (end_idx < m_concrete_frames_fetched)
447 return;
448
449 uint32_t num_frames = unwinder->GetFramesUpTo(end_idx);
450 if (num_frames <= end_idx + 1)
451 {
452 //Done unwinding.
453 m_concrete_frames_fetched = UINT32_MAX;
454 }
455 m_frames.resize(num_frames);
456 }
457}
Chris Lattner24943d22010-06-08 16:52:24 +0000458
459uint32_t
Greg Clayton5205f0b2010-09-03 17:10:42 +0000460StackFrameList::GetNumFrames (bool can_create)
Chris Lattner24943d22010-06-08 16:52:24 +0000461{
462 Mutex::Locker locker (m_mutex);
Greg Clayton782b9cc2010-08-25 00:35:26 +0000463
Jim Inghambf97d742012-02-29 03:40:22 +0000464 if (can_create)
465 GetFramesUpTo (UINT32_MAX);
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000466
467 uint32_t inlined_depth = GetCurrentInlinedDepth();
468 if (inlined_depth == UINT32_MAX)
469 return m_frames.size();
470 else
471 return m_frames.size() - inlined_depth;
Greg Clayton1d66ef52010-08-27 18:24:16 +0000472}
473
474void
475StackFrameList::Dump (Stream *s)
476{
477 if (s == NULL)
478 return;
479 Mutex::Locker locker (m_mutex);
480
481 const_iterator pos, begin = m_frames.begin(), end = m_frames.end();
482 for (pos = begin; pos != end; ++pos)
Greg Clayton782b9cc2010-08-25 00:35:26 +0000483 {
Greg Clayton1d66ef52010-08-27 18:24:16 +0000484 StackFrame *frame = (*pos).get();
485 s->Printf("%p: ", frame);
486 if (frame)
Greg Clayton4fb08152010-08-30 18:11:35 +0000487 {
488 frame->GetStackID().Dump (s);
Greg Claytona830adb2010-10-04 01:05:56 +0000489 frame->DumpUsingSettingsFormat (s);
Greg Clayton4fb08152010-08-30 18:11:35 +0000490 }
Greg Clayton1d66ef52010-08-27 18:24:16 +0000491 else
Greg Clayton3e4238d2011-11-04 03:34:56 +0000492 s->Printf("frame #%u", (uint32_t)std::distance (begin, pos));
Greg Clayton1d66ef52010-08-27 18:24:16 +0000493 s->EOL();
Greg Clayton782b9cc2010-08-25 00:35:26 +0000494 }
Greg Clayton1d66ef52010-08-27 18:24:16 +0000495 s->EOL();
Chris Lattner24943d22010-06-08 16:52:24 +0000496}
497
Chris Lattner24943d22010-06-08 16:52:24 +0000498StackFrameSP
Greg Clayton782b9cc2010-08-25 00:35:26 +0000499StackFrameList::GetFrameAtIndex (uint32_t idx)
Chris Lattner24943d22010-06-08 16:52:24 +0000500{
501 StackFrameSP frame_sp;
Greg Clayton1d66ef52010-08-27 18:24:16 +0000502 Mutex::Locker locker (m_mutex);
Jim Ingham4dd7f532013-03-28 00:13:30 +0000503 uint32_t original_idx = idx;
504
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000505 uint32_t inlined_depth = GetCurrentInlinedDepth();
506 if (inlined_depth != UINT32_MAX)
507 idx += inlined_depth;
508
Greg Clayton1d66ef52010-08-27 18:24:16 +0000509 if (idx < m_frames.size())
510 frame_sp = m_frames[idx];
511
512 if (frame_sp)
513 return frame_sp;
Greg Claytonf40e3082010-08-26 02:28:22 +0000514
Jim Ingham4dd7f532013-03-28 00:13:30 +0000515 // GetFramesUpTo will fill m_frames with as many frames as you asked for,
516 // if there are that many. If there weren't then you asked for too many
517 // frames.
518 GetFramesUpTo (idx);
519 if (idx < m_frames.size())
520 {
521 if (m_show_inlined_frames)
Greg Clayton1d66ef52010-08-27 18:24:16 +0000522 {
Jim Ingham4dd7f532013-03-28 00:13:30 +0000523 // When inline frames are enabled we actually create all the frames in GetFramesUpTo.
524 frame_sp = m_frames[idx];
525 }
526 else
527 {
528 Unwind *unwinder = m_thread.GetUnwinder ();
529 if (unwinder)
Greg Clayton1d66ef52010-08-27 18:24:16 +0000530 {
Jim Ingham4dd7f532013-03-28 00:13:30 +0000531 addr_t pc, cfa;
532 if (unwinder->GetFrameInfoAtIndex(idx, cfa, pc))
Greg Clayton1d66ef52010-08-27 18:24:16 +0000533 {
Jim Ingham4dd7f532013-03-28 00:13:30 +0000534 frame_sp.reset (new StackFrame (m_thread.shared_from_this(), idx, idx, cfa, pc, NULL));
535
536 Function *function = frame_sp->GetSymbolContext (eSymbolContextFunction).function;
537 if (function)
Greg Clayton4fb08152010-08-30 18:11:35 +0000538 {
Jim Ingham4dd7f532013-03-28 00:13:30 +0000539 // When we aren't showing inline functions we always use
540 // the top most function block as the scope.
541 frame_sp->SetSymbolContextScope (&function->GetBlock(false));
Greg Clayton4fb08152010-08-30 18:11:35 +0000542 }
Jim Ingham4dd7f532013-03-28 00:13:30 +0000543 else
544 {
545 // Set the symbol scope from the symbol regardless if it is NULL or valid.
546 frame_sp->SetSymbolContextScope (frame_sp->GetSymbolContext (eSymbolContextSymbol).symbol);
547 }
548 SetFrameAtIndex(idx, frame_sp);
Greg Clayton1d66ef52010-08-27 18:24:16 +0000549 }
550 }
Greg Clayton782b9cc2010-08-25 00:35:26 +0000551 }
Jim Ingham4dd7f532013-03-28 00:13:30 +0000552 }
553 else if (original_idx == 0)
554 {
555 // There should ALWAYS be a frame at index 0. If something went wrong with the CurrentInlinedDepth such that
556 // there weren't as many frames as we thought taking that into account, then reset the current inlined depth
557 // and return the real zeroth frame.
558 if (m_frames.size() > 0)
559 {
560 ResetCurrentInlinedDepth();
561 frame_sp = m_frames[original_idx];
562 }
563 else
564 {
565 // Why do we have a thread with zero frames, that should not ever happen...
566 if (m_thread.IsValid())
567 assert ("A valid thread has no frames.");
568
569 }
570 }
571
Chris Lattner24943d22010-06-08 16:52:24 +0000572 return frame_sp;
573}
574
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000575StackFrameSP
576StackFrameList::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx)
577{
578 // First try assuming the unwind index is the same as the frame index. The
579 // unwind index is always greater than or equal to the frame index, so it
580 // is a good place to start. If we have inlined frames we might have 5
581 // concrete frames (frame unwind indexes go from 0-4), but we might have 15
582 // frames after we make all the inlined frames. Most of the time the unwind
583 // frame index (or the concrete frame index) is the same as the frame index.
584 uint32_t frame_idx = unwind_idx;
585 StackFrameSP frame_sp (GetFrameAtIndex (frame_idx));
586 while (frame_sp)
587 {
588 if (frame_sp->GetFrameIndex() == unwind_idx)
589 break;
590 frame_sp = GetFrameAtIndex (++frame_idx);
591 }
592 return frame_sp;
593}
594
Jim Ingham5c4b1602011-03-31 00:15:49 +0000595StackFrameSP
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000596StackFrameList::GetFrameWithStackID (const StackID &stack_id)
Jim Ingham5c4b1602011-03-31 00:15:49 +0000597{
598 uint32_t frame_idx = 0;
599 StackFrameSP frame_sp;
600 do
601 {
602 frame_sp = GetFrameAtIndex (frame_idx);
603 if (frame_sp && frame_sp->GetStackID() == stack_id)
604 break;
605 frame_idx++;
606 }
607 while (frame_sp);
608 return frame_sp;
609}
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000610
Greg Clayton782b9cc2010-08-25 00:35:26 +0000611bool
Greg Clayton1d66ef52010-08-27 18:24:16 +0000612StackFrameList::SetFrameAtIndex (uint32_t idx, StackFrameSP &frame_sp)
Greg Clayton782b9cc2010-08-25 00:35:26 +0000613{
Greg Clayton1d66ef52010-08-27 18:24:16 +0000614 if (idx >= m_frames.size())
615 m_frames.resize(idx + 1);
Greg Clayton782b9cc2010-08-25 00:35:26 +0000616 // Make sure allocation succeeded by checking bounds again
Greg Clayton1d66ef52010-08-27 18:24:16 +0000617 if (idx < m_frames.size())
Greg Clayton782b9cc2010-08-25 00:35:26 +0000618 {
Greg Clayton1d66ef52010-08-27 18:24:16 +0000619 m_frames[idx] = frame_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000620 return true;
621 }
622 return false; // resize failed, out of memory?
623}
624
625uint32_t
Jim Inghamc8332952010-08-26 21:32:51 +0000626StackFrameList::GetSelectedFrameIndex () const
Chris Lattner24943d22010-06-08 16:52:24 +0000627{
628 Mutex::Locker locker (m_mutex);
Jim Inghamc8332952010-08-26 21:32:51 +0000629 return m_selected_frame_idx;
Chris Lattner24943d22010-06-08 16:52:24 +0000630}
631
632
633uint32_t
Jim Inghamc8332952010-08-26 21:32:51 +0000634StackFrameList::SetSelectedFrame (lldb_private::StackFrame *frame)
Chris Lattner24943d22010-06-08 16:52:24 +0000635{
636 Mutex::Locker locker (m_mutex);
Greg Clayton782b9cc2010-08-25 00:35:26 +0000637 const_iterator pos;
Greg Clayton1d66ef52010-08-27 18:24:16 +0000638 const_iterator begin = m_frames.begin();
639 const_iterator end = m_frames.end();
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000640 m_selected_frame_idx = 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000641 for (pos = begin; pos != end; ++pos)
642 {
643 if (pos->get() == frame)
644 {
Jim Inghamc8332952010-08-26 21:32:51 +0000645 m_selected_frame_idx = std::distance (begin, pos);
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000646 uint32_t inlined_depth = GetCurrentInlinedDepth();
647 if (inlined_depth != UINT32_MAX)
648 m_selected_frame_idx -= inlined_depth;
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000649 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000650 }
651 }
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000652 SetDefaultFileAndLineToSelectedFrame();
Jim Inghamc8332952010-08-26 21:32:51 +0000653 return m_selected_frame_idx;
Chris Lattner24943d22010-06-08 16:52:24 +0000654}
655
656// Mark a stack frame as the current frame using the frame index
Jim Inghambf97d742012-02-29 03:40:22 +0000657bool
Jim Inghamc8332952010-08-26 21:32:51 +0000658StackFrameList::SetSelectedFrameByIndex (uint32_t idx)
Chris Lattner24943d22010-06-08 16:52:24 +0000659{
660 Mutex::Locker locker (m_mutex);
Jim Inghambf97d742012-02-29 03:40:22 +0000661 StackFrameSP frame_sp (GetFrameAtIndex (idx));
662 if (frame_sp)
663 {
664 SetSelectedFrame(frame_sp.get());
665 return true;
666 }
667 else
668 return false;
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000669}
670
671void
672StackFrameList::SetDefaultFileAndLineToSelectedFrame()
673{
Greg Claytonf4124de2012-02-21 00:09:25 +0000674 if (m_thread.GetID() == m_thread.GetProcess()->GetThreadList().GetSelectedThread()->GetID())
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000675 {
Greg Clayton840a9922011-10-05 03:14:31 +0000676 StackFrameSP frame_sp (GetFrameAtIndex (GetSelectedFrameIndex()));
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000677 if (frame_sp)
678 {
Greg Clayton840a9922011-10-05 03:14:31 +0000679 SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextLineEntry);
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000680 if (sc.line_entry.file)
Greg Claytonf4124de2012-02-21 00:09:25 +0000681 m_thread.CalculateTarget()->GetSourceManager().SetDefaultFileAndLine (sc.line_entry.file,
Greg Clayton840a9922011-10-05 03:14:31 +0000682 sc.line_entry.line);
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000683 }
684 }
Chris Lattner24943d22010-06-08 16:52:24 +0000685}
686
687// The thread has been run, reset the number stack frames to zero so we can
688// determine how many frames we have lazily.
689void
690StackFrameList::Clear ()
691{
692 Mutex::Locker locker (m_mutex);
Greg Clayton1d66ef52010-08-27 18:24:16 +0000693 m_frames.clear();
Jim Inghambf97d742012-02-29 03:40:22 +0000694 m_concrete_frames_fetched = 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000695}
696
697void
698StackFrameList::InvalidateFrames (uint32_t start_idx)
699{
700 Mutex::Locker locker (m_mutex);
Greg Clayton782b9cc2010-08-25 00:35:26 +0000701 if (m_show_inlined_frames)
Chris Lattner24943d22010-06-08 16:52:24 +0000702 {
Greg Clayton782b9cc2010-08-25 00:35:26 +0000703 Clear();
704 }
705 else
706 {
Greg Clayton1d66ef52010-08-27 18:24:16 +0000707 const size_t num_frames = m_frames.size();
Greg Clayton782b9cc2010-08-25 00:35:26 +0000708 while (start_idx < num_frames)
709 {
Greg Clayton1d66ef52010-08-27 18:24:16 +0000710 m_frames[start_idx].reset();
Greg Clayton782b9cc2010-08-25 00:35:26 +0000711 ++start_idx;
712 }
Chris Lattner24943d22010-06-08 16:52:24 +0000713 }
714}
Greg Clayton5205f0b2010-09-03 17:10:42 +0000715
716void
717StackFrameList::Merge (std::auto_ptr<StackFrameList>& curr_ap, lldb::StackFrameListSP& prev_sp)
718{
Jim Ingham1b584eb2012-05-04 23:02:50 +0000719 Mutex::Locker curr_locker (curr_ap.get() ? &curr_ap->m_mutex : NULL);
720 Mutex::Locker prev_locker (prev_sp.get() ? &prev_sp->m_mutex : NULL);
Greg Clayton5205f0b2010-09-03 17:10:42 +0000721
722#if defined (DEBUG_STACK_FRAMES)
Johnny Chen0a77f902011-08-25 17:27:02 +0000723 StreamFile s(stdout, false);
Greg Clayton5205f0b2010-09-03 17:10:42 +0000724 s.PutCString("\n\nStackFrameList::Merge():\nPrev:\n");
725 if (prev_sp.get())
726 prev_sp->Dump (&s);
727 else
728 s.PutCString ("NULL");
729 s.PutCString("\nCurr:\n");
730 if (curr_ap.get())
731 curr_ap->Dump (&s);
732 else
733 s.PutCString ("NULL");
734 s.EOL();
735#endif
736
737 if (curr_ap.get() == NULL || curr_ap->GetNumFrames (false) == 0)
738 {
739#if defined (DEBUG_STACK_FRAMES)
740 s.PutCString("No current frames, leave previous frames alone...\n");
741#endif
742 curr_ap.release();
743 return;
744 }
745
746 if (prev_sp.get() == NULL || prev_sp->GetNumFrames (false) == 0)
747 {
748#if defined (DEBUG_STACK_FRAMES)
749 s.PutCString("No previous frames, so use current frames...\n");
750#endif
751 // We either don't have any previous frames, or since we have more than
752 // one current frames it means we have all the frames and can safely
753 // replace our previous frames.
754 prev_sp.reset (curr_ap.release());
755 return;
756 }
757
758 const uint32_t num_curr_frames = curr_ap->GetNumFrames (false);
759
760 if (num_curr_frames > 1)
761 {
762#if defined (DEBUG_STACK_FRAMES)
763 s.PutCString("We have more than one current frame, so use current frames...\n");
764#endif
765 // We have more than one current frames it means we have all the frames
766 // and can safely replace our previous frames.
767 prev_sp.reset (curr_ap.release());
768
769#if defined (DEBUG_STACK_FRAMES)
770 s.PutCString("\nMerged:\n");
771 prev_sp->Dump (&s);
772#endif
773 return;
774 }
775
776 StackFrameSP prev_frame_zero_sp(prev_sp->GetFrameAtIndex (0));
777 StackFrameSP curr_frame_zero_sp(curr_ap->GetFrameAtIndex (0));
778 StackID curr_stack_id (curr_frame_zero_sp->GetStackID());
779 StackID prev_stack_id (prev_frame_zero_sp->GetStackID());
780
Greg Clayton5205f0b2010-09-03 17:10:42 +0000781#if defined (DEBUG_STACK_FRAMES)
Johnny Chen0a77f902011-08-25 17:27:02 +0000782 const uint32_t num_prev_frames = prev_sp->GetNumFrames (false);
Greg Clayton5205f0b2010-09-03 17:10:42 +0000783 s.Printf("\n%u previous frames with one current frame\n", num_prev_frames);
784#endif
785
786 // We have only a single current frame
787 // Our previous stack frames only had a single frame as well...
788 if (curr_stack_id == prev_stack_id)
789 {
790#if defined (DEBUG_STACK_FRAMES)
791 s.Printf("\nPrevious frame #0 is same as current frame #0, merge the cached data\n");
792#endif
793
794 curr_frame_zero_sp->UpdateCurrentFrameFromPreviousFrame (*prev_frame_zero_sp);
795// prev_frame_zero_sp->UpdatePreviousFrameFromCurrentFrame (*curr_frame_zero_sp);
796// prev_sp->SetFrameAtIndex (0, prev_frame_zero_sp);
797 }
798 else if (curr_stack_id < prev_stack_id)
799 {
800#if defined (DEBUG_STACK_FRAMES)
801 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");
802#endif
803 prev_sp->m_frames.insert (prev_sp->m_frames.begin(), curr_frame_zero_sp);
804 }
805
806 curr_ap.release();
807
808#if defined (DEBUG_STACK_FRAMES)
809 s.PutCString("\nMerged:\n");
810 prev_sp->Dump (&s);
811#endif
812
813
814}
Jim Inghamccd584d2010-09-23 17:40:12 +0000815
816lldb::StackFrameSP
817StackFrameList::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr)
818{
819 const_iterator pos;
820 const_iterator begin = m_frames.begin();
821 const_iterator end = m_frames.end();
822 lldb::StackFrameSP ret_sp;
823
824 for (pos = begin; pos != end; ++pos)
825 {
826 if (pos->get() == stack_frame_ptr)
827 {
828 ret_sp = (*pos);
829 break;
830 }
831 }
832 return ret_sp;
833}
834
Greg Claytonabe0fed2011-04-18 08:33:37 +0000835size_t
836StackFrameList::GetStatus (Stream& strm,
837 uint32_t first_frame,
838 uint32_t num_frames,
839 bool show_frame_info,
Greg Claytona7d3dc72012-07-11 20:33:48 +0000840 uint32_t num_frames_with_source)
Greg Claytonabe0fed2011-04-18 08:33:37 +0000841{
842 size_t num_frames_displayed = 0;
843
844 if (num_frames == 0)
845 return 0;
846
847 StackFrameSP frame_sp;
848 uint32_t frame_idx = 0;
849 uint32_t last_frame;
850
851 // Don't let the last frame wrap around...
852 if (num_frames == UINT32_MAX)
853 last_frame = UINT32_MAX;
854 else
855 last_frame = first_frame + num_frames;
856
857 for (frame_idx = first_frame; frame_idx < last_frame; ++frame_idx)
858 {
859 frame_sp = GetFrameAtIndex(frame_idx);
860 if (frame_sp.get() == NULL)
861 break;
862
863 if (!frame_sp->GetStatus (strm,
864 show_frame_info,
Greg Claytona7d3dc72012-07-11 20:33:48 +0000865 num_frames_with_source > (first_frame - frame_idx)))
Greg Claytonabe0fed2011-04-18 08:33:37 +0000866 break;
867 ++num_frames_displayed;
868 }
869
870 strm.IndentLess();
871 return num_frames_displayed;
872}
873