130fdc8d8SChris Lattner //===-- StackFrameList.cpp --------------------------------------*- C++ -*-===//
230fdc8d8SChris Lattner //
330fdc8d8SChris Lattner //                     The LLVM Compiler Infrastructure
430fdc8d8SChris Lattner //
530fdc8d8SChris Lattner // This file is distributed under the University of Illinois Open Source
630fdc8d8SChris Lattner // License. See LICENSE.TXT for details.
730fdc8d8SChris Lattner //
830fdc8d8SChris Lattner //===----------------------------------------------------------------------===//
930fdc8d8SChris Lattner 
1012daf946SGreg Clayton #include "lldb/Target/StackFrameList.h"
1112daf946SGreg Clayton 
1230fdc8d8SChris Lattner // C Includes
1330fdc8d8SChris Lattner // C++ Includes
1430fdc8d8SChris Lattner // Other libraries and framework includes
1530fdc8d8SChris Lattner // Project includes
16c635500dSJim Ingham #include "lldb/Breakpoint/BreakpointLocation.h"
17c635500dSJim Ingham #include "lldb/Breakpoint/Breakpoint.h"
186cd41da7SJim Ingham #include "lldb/Core/Log.h"
195082c5fdSGreg Clayton #include "lldb/Core/StreamFile.h"
20b7f6b2faSJim Ingham #include "lldb/Core/SourceManager.h"
2112daf946SGreg Clayton #include "lldb/Symbol/Block.h"
2212daf946SGreg Clayton #include "lldb/Symbol/Function.h"
2359e8fc1cSGreg Clayton #include "lldb/Symbol/Symbol.h"
24b7f6b2faSJim Ingham #include "lldb/Target/Process.h"
2512daf946SGreg Clayton #include "lldb/Target/RegisterContext.h"
2630fdc8d8SChris Lattner #include "lldb/Target/StackFrame.h"
27513c6bb8SJim Ingham #include "lldb/Target/StopInfo.h"
28b7f6b2faSJim Ingham #include "lldb/Target/Target.h"
2912daf946SGreg Clayton #include "lldb/Target/Thread.h"
3012daf946SGreg Clayton #include "lldb/Target/Unwind.h"
3130fdc8d8SChris Lattner 
325082c5fdSGreg Clayton //#define DEBUG_STACK_FRAMES 1
335082c5fdSGreg Clayton 
3430fdc8d8SChris Lattner using namespace lldb;
3530fdc8d8SChris Lattner using namespace lldb_private;
3630fdc8d8SChris Lattner 
3730fdc8d8SChris Lattner //----------------------------------------------------------------------
3830fdc8d8SChris Lattner // StackFrameList constructor
3930fdc8d8SChris Lattner //----------------------------------------------------------------------
402cad65a5SGreg Clayton StackFrameList::StackFrameList
412cad65a5SGreg Clayton (
422cad65a5SGreg Clayton     Thread &thread,
432cad65a5SGreg Clayton     const lldb::StackFrameListSP &prev_frames_sp,
442cad65a5SGreg Clayton     bool show_inline_frames
452cad65a5SGreg Clayton ) :
4612daf946SGreg Clayton     m_thread (thread),
472cad65a5SGreg Clayton     m_prev_frames_sp (prev_frames_sp),
4830fdc8d8SChris Lattner     m_mutex (Mutex::eMutexTypeRecursive),
495082c5fdSGreg Clayton     m_frames (),
5071c21d18SStephen Wilson     m_selected_frame_idx (0),
51b0c72a5fSJim Ingham     m_concrete_frames_fetched (0),
52513c6bb8SJim Ingham     m_current_inlined_depth (UINT32_MAX),
53513c6bb8SJim Ingham     m_current_inlined_pc (LLDB_INVALID_ADDRESS),
5471c21d18SStephen Wilson     m_show_inlined_frames (show_inline_frames)
5530fdc8d8SChris Lattner {
56513c6bb8SJim Ingham     if (prev_frames_sp)
57513c6bb8SJim Ingham     {
58513c6bb8SJim Ingham         m_current_inlined_depth = prev_frames_sp->m_current_inlined_depth;
59513c6bb8SJim Ingham         m_current_inlined_pc =    prev_frames_sp->m_current_inlined_pc;
60513c6bb8SJim Ingham     }
6130fdc8d8SChris Lattner }
6230fdc8d8SChris Lattner 
6330fdc8d8SChris Lattner //----------------------------------------------------------------------
6430fdc8d8SChris Lattner // Destructor
6530fdc8d8SChris Lattner //----------------------------------------------------------------------
6630fdc8d8SChris Lattner StackFrameList::~StackFrameList()
6730fdc8d8SChris Lattner {
6830fdc8d8SChris Lattner }
6930fdc8d8SChris Lattner 
70b0c72a5fSJim Ingham void
71513c6bb8SJim Ingham StackFrameList::CalculateCurrentInlinedDepth()
72513c6bb8SJim Ingham {
73513c6bb8SJim Ingham     uint32_t cur_inlined_depth = GetCurrentInlinedDepth();
74513c6bb8SJim Ingham     if (cur_inlined_depth == UINT32_MAX)
75513c6bb8SJim Ingham     {
76513c6bb8SJim Ingham         ResetCurrentInlinedDepth();
77513c6bb8SJim Ingham     }
78513c6bb8SJim Ingham }
79513c6bb8SJim Ingham 
80513c6bb8SJim Ingham uint32_t
81513c6bb8SJim Ingham StackFrameList::GetCurrentInlinedDepth ()
82513c6bb8SJim Ingham {
836cd41da7SJim Ingham     if (m_show_inlined_frames && m_current_inlined_pc != LLDB_INVALID_ADDRESS)
84513c6bb8SJim Ingham     {
85513c6bb8SJim Ingham         lldb::addr_t cur_pc = m_thread.GetRegisterContext()->GetPC();
86513c6bb8SJim Ingham         if (cur_pc != m_current_inlined_pc)
87513c6bb8SJim Ingham         {
88513c6bb8SJim Ingham             m_current_inlined_pc = LLDB_INVALID_ADDRESS;
89513c6bb8SJim Ingham             m_current_inlined_depth = UINT32_MAX;
906cd41da7SJim Ingham             LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
916cd41da7SJim Ingham             if (log && log->GetVerbose())
926cd41da7SJim Ingham                 log->Printf ("GetCurrentInlinedDepth: invalidating current inlined depth.\n");
93513c6bb8SJim Ingham         }
94513c6bb8SJim Ingham         return m_current_inlined_depth;
95513c6bb8SJim Ingham     }
96513c6bb8SJim Ingham     else
97513c6bb8SJim Ingham     {
98513c6bb8SJim Ingham         return UINT32_MAX;
99513c6bb8SJim Ingham     }
100513c6bb8SJim Ingham }
101513c6bb8SJim Ingham 
102513c6bb8SJim Ingham void
103513c6bb8SJim Ingham StackFrameList::ResetCurrentInlinedDepth ()
104513c6bb8SJim Ingham {
105e7e6ffc6SJim Ingham     if (m_show_inlined_frames)
106513c6bb8SJim Ingham     {
107513c6bb8SJim Ingham         GetFramesUpTo(0);
108513c6bb8SJim Ingham         if (!m_frames[0]->IsInlined())
109513c6bb8SJim Ingham         {
110513c6bb8SJim Ingham             m_current_inlined_depth = UINT32_MAX;
111513c6bb8SJim Ingham             m_current_inlined_pc = LLDB_INVALID_ADDRESS;
1126cd41da7SJim Ingham             LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
1136cd41da7SJim Ingham             if (log && log->GetVerbose())
1146cd41da7SJim Ingham                 log->Printf ("ResetCurrentInlinedDepth: Invalidating current inlined depth.\n");
115513c6bb8SJim Ingham         }
116513c6bb8SJim Ingham         else
117513c6bb8SJim Ingham         {
118513c6bb8SJim Ingham             // We only need to do something special about inlined blocks when we
119513c6bb8SJim Ingham             // are at the beginning of an inlined function:
120513c6bb8SJim Ingham             // FIXME: We probably also have to do something special if the PC is at the END
121513c6bb8SJim Ingham             // of an inlined function, which coincides with the end of either its containing
122513c6bb8SJim Ingham             // function or another inlined function.
123513c6bb8SJim Ingham 
124513c6bb8SJim Ingham             lldb::addr_t curr_pc = m_thread.GetRegisterContext()->GetPC();
125513c6bb8SJim Ingham             Block *block_ptr = m_frames[0]->GetFrameBlock();
126513c6bb8SJim Ingham             if (block_ptr)
127513c6bb8SJim Ingham             {
128513c6bb8SJim Ingham                 Address pc_as_address;
129513c6bb8SJim Ingham                 pc_as_address.SetLoadAddress(curr_pc, &(m_thread.GetProcess()->GetTarget()));
130513c6bb8SJim Ingham                 AddressRange containing_range;
131513c6bb8SJim Ingham                 if (block_ptr->GetRangeContainingAddress(pc_as_address, containing_range))
132513c6bb8SJim Ingham                 {
133513c6bb8SJim Ingham                     if (pc_as_address == containing_range.GetBaseAddress())
134513c6bb8SJim Ingham                     {
135513c6bb8SJim Ingham                         // If we got here because of a breakpoint hit, then set the inlined depth depending on where
136513c6bb8SJim Ingham                         // the breakpoint was set.
137513c6bb8SJim Ingham                         // If we got here because of a crash, then set the inlined depth to the deepest most block.
138513c6bb8SJim Ingham                         // Otherwise, we stopped here naturally as the result of a step, so set ourselves in the
139513c6bb8SJim Ingham                         // containing frame of the whole set of nested inlines, so the user can then "virtually"
140513c6bb8SJim Ingham                         // step into the frames one by one, or next over the whole mess.
141513c6bb8SJim Ingham                         // Note: We don't have to handle being somewhere in the middle of the stack here, since
142513c6bb8SJim Ingham                         // ResetCurrentInlinedDepth doesn't get called if there is a valid inlined depth set.
143513c6bb8SJim Ingham                         StopInfoSP stop_info_sp = m_thread.GetStopInfo();
144513c6bb8SJim Ingham                         if (stop_info_sp)
145513c6bb8SJim Ingham                         {
146513c6bb8SJim Ingham                             switch (stop_info_sp->GetStopReason())
147513c6bb8SJim Ingham                             {
148513c6bb8SJim Ingham                             case eStopReasonWatchpoint:
149513c6bb8SJim Ingham                             case eStopReasonException:
15090ba8115SGreg Clayton                             case eStopReasonExec:
151513c6bb8SJim Ingham                             case eStopReasonSignal:
152513c6bb8SJim Ingham                                 // In all these cases we want to stop in the deepest most frame.
153513c6bb8SJim Ingham                                 m_current_inlined_pc = curr_pc;
154513c6bb8SJim Ingham                                 m_current_inlined_depth = 0;
155513c6bb8SJim Ingham                                 break;
1567da851a3SJim Ingham                             case eStopReasonBreakpoint:
1577da851a3SJim Ingham                                 {
1587da851a3SJim Ingham                                     // FIXME: Figure out what this break point is doing, and set the inline depth
1597da851a3SJim Ingham                                     // appropriately.  Be careful to take into account breakpoints that implement
1607da851a3SJim Ingham                                     // step over prologue, since that should do the default calculation.
161c635500dSJim Ingham                                     // For now, if the breakpoints corresponding to this hit are all internal,
162c635500dSJim Ingham                                     // I set the stop location to the top of the inlined stack, since that will make
163c635500dSJim Ingham                                     // things like stepping over prologues work right.  But if there are any non-internal
164c635500dSJim Ingham                                     // breakpoints I do to the bottom of the stack, since that was the old behavior.
165c635500dSJim Ingham                                     uint32_t bp_site_id = stop_info_sp->GetValue();
166c635500dSJim Ingham                                     BreakpointSiteSP bp_site_sp(m_thread.GetProcess()->GetBreakpointSiteList().FindByID(bp_site_id));
167c635500dSJim Ingham                                     bool all_internal = true;
168c635500dSJim Ingham                                     if (bp_site_sp)
169c635500dSJim Ingham                                     {
170c635500dSJim Ingham                                         uint32_t num_owners = bp_site_sp->GetNumberOfOwners();
171c635500dSJim Ingham                                         for (uint32_t i = 0; i < num_owners; i++)
172c635500dSJim Ingham                                         {
173c635500dSJim Ingham                                             Breakpoint &bp_ref = bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint();
174c635500dSJim Ingham                                             if (!bp_ref.IsInternal())
175c635500dSJim Ingham                                             {
176c635500dSJim Ingham                                                 all_internal = false;
177c635500dSJim Ingham                                             }
178c635500dSJim Ingham                                         }
179c635500dSJim Ingham                                     }
180c635500dSJim Ingham                                     if (!all_internal)
181c635500dSJim Ingham                                     {
182c635500dSJim Ingham                                         m_current_inlined_pc = curr_pc;
183c635500dSJim Ingham                                         m_current_inlined_depth = 0;
184c635500dSJim Ingham                                         break;
185c635500dSJim Ingham                                     }
1867da851a3SJim Ingham                                 }
187513c6bb8SJim Ingham                             default:
188513c6bb8SJim Ingham                                 {
189513c6bb8SJim Ingham                                     // Otherwise, we should set ourselves at the container of the inlining, so that the
190513c6bb8SJim Ingham                                     // user can descend into them.
191513c6bb8SJim Ingham                                     // So first we check whether we have more than one inlined block sharing this PC:
192513c6bb8SJim Ingham                                     int num_inlined_functions = 0;
193513c6bb8SJim Ingham 
194513c6bb8SJim Ingham                                     for  (Block *container_ptr = block_ptr->GetInlinedParent();
195513c6bb8SJim Ingham                                               container_ptr != NULL;
196513c6bb8SJim Ingham                                               container_ptr = container_ptr->GetInlinedParent())
197513c6bb8SJim Ingham                                     {
198513c6bb8SJim Ingham                                         if (!container_ptr->GetRangeContainingAddress(pc_as_address, containing_range))
199513c6bb8SJim Ingham                                             break;
200513c6bb8SJim Ingham                                         if (pc_as_address != containing_range.GetBaseAddress())
201513c6bb8SJim Ingham                                             break;
202513c6bb8SJim Ingham 
203513c6bb8SJim Ingham                                         num_inlined_functions++;
204513c6bb8SJim Ingham                                     }
205513c6bb8SJim Ingham                                     m_current_inlined_pc = curr_pc;
206513c6bb8SJim Ingham                                     m_current_inlined_depth = num_inlined_functions + 1;
2076cd41da7SJim Ingham                                     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
2086cd41da7SJim Ingham                                     if (log && log->GetVerbose())
209d01b2953SDaniel Malea                                         log->Printf ("ResetCurrentInlinedDepth: setting inlined depth: %d 0x%" PRIx64 ".\n", m_current_inlined_depth, curr_pc);
210513c6bb8SJim Ingham 
211513c6bb8SJim Ingham                                 }
212513c6bb8SJim Ingham                                 break;
213513c6bb8SJim Ingham                             }
214513c6bb8SJim Ingham                         }
215513c6bb8SJim Ingham                     }
216513c6bb8SJim Ingham                 }
217513c6bb8SJim Ingham             }
218513c6bb8SJim Ingham         }
219513c6bb8SJim Ingham     }
220513c6bb8SJim Ingham }
221513c6bb8SJim Ingham 
222513c6bb8SJim Ingham bool
223513c6bb8SJim Ingham StackFrameList::DecrementCurrentInlinedDepth ()
224513c6bb8SJim Ingham {
225513c6bb8SJim Ingham     if (m_show_inlined_frames)
226513c6bb8SJim Ingham     {
227513c6bb8SJim Ingham         uint32_t current_inlined_depth = GetCurrentInlinedDepth();
228513c6bb8SJim Ingham         if (current_inlined_depth != UINT32_MAX)
229513c6bb8SJim Ingham         {
230513c6bb8SJim Ingham             if (current_inlined_depth > 0)
2319786eeebSJim Ingham             {
232513c6bb8SJim Ingham                 m_current_inlined_depth--;
233513c6bb8SJim Ingham                 return true;
234513c6bb8SJim Ingham             }
235513c6bb8SJim Ingham         }
2369786eeebSJim Ingham     }
237513c6bb8SJim Ingham     return false;
238513c6bb8SJim Ingham }
239513c6bb8SJim Ingham 
240513c6bb8SJim Ingham void
2416cd41da7SJim Ingham StackFrameList::SetCurrentInlinedDepth (uint32_t new_depth)
2426cd41da7SJim Ingham {
2436cd41da7SJim Ingham     m_current_inlined_depth = new_depth;
2446cd41da7SJim Ingham     if (new_depth == UINT32_MAX)
2456cd41da7SJim Ingham         m_current_inlined_pc = LLDB_INVALID_ADDRESS;
2466cd41da7SJim Ingham     else
2476cd41da7SJim Ingham         m_current_inlined_pc = m_thread.GetRegisterContext()->GetPC();
2486cd41da7SJim Ingham }
2496cd41da7SJim Ingham 
2506cd41da7SJim Ingham void
251b0c72a5fSJim Ingham StackFrameList::GetFramesUpTo(uint32_t end_idx)
25230fdc8d8SChris Lattner {
253*ebafd2f1SEnrico Granata     // this makes sure we do not fetch frames for an invalid thread
254*ebafd2f1SEnrico Granata     if (m_thread.IsValid() == false)
255*ebafd2f1SEnrico Granata         return;
256*ebafd2f1SEnrico Granata 
257b0c72a5fSJim Ingham     // We've already gotten more frames than asked for, or we've already finished unwinding, return.
258b0c72a5fSJim Ingham     if (m_frames.size() > end_idx || GetAllFramesFetched())
259b0c72a5fSJim Ingham         return;
26012daf946SGreg Clayton 
261b0c72a5fSJim Ingham     Unwind *unwinder = m_thread.GetUnwinder ();
262b0c72a5fSJim Ingham 
26312daf946SGreg Clayton     if (m_show_inlined_frames)
26412daf946SGreg Clayton     {
2655082c5fdSGreg Clayton #if defined (DEBUG_STACK_FRAMES)
2662c595273SJohnny Chen         StreamFile s(stdout, false);
2675082c5fdSGreg Clayton #endif
268513c6bb8SJim Ingham         // If we are hiding some frames from the outside world, we need to add those onto the total count of
269513c6bb8SJim Ingham         // frames to fetch.  However, we don't need ot do that if end_idx is 0 since in that case we always
2706cd41da7SJim Ingham         // get the first concrete frame and all the inlined frames below it...  And of course, if end_idx is
2716cd41da7SJim Ingham         // UINT32_MAX that means get all, so just do that...
272513c6bb8SJim Ingham 
273513c6bb8SJim Ingham         uint32_t inlined_depth = 0;
2746cd41da7SJim Ingham         if (end_idx > 0 && end_idx != UINT32_MAX)
275513c6bb8SJim Ingham         {
276513c6bb8SJim Ingham             inlined_depth = GetCurrentInlinedDepth();
277513c6bb8SJim Ingham             if (inlined_depth != UINT32_MAX)
278513c6bb8SJim Ingham             {
279513c6bb8SJim Ingham                 if (end_idx > 0)
280513c6bb8SJim Ingham                     end_idx += inlined_depth;
281513c6bb8SJim Ingham             }
282513c6bb8SJim Ingham         }
28312daf946SGreg Clayton 
2845082c5fdSGreg Clayton         StackFrameSP unwind_frame_sp;
285b0c72a5fSJim Ingham         do
28612daf946SGreg Clayton         {
287b0c72a5fSJim Ingham             uint32_t idx = m_concrete_frames_fetched++;
288b0c72a5fSJim Ingham             lldb::addr_t pc;
289b0c72a5fSJim Ingham             lldb::addr_t cfa;
29012daf946SGreg Clayton             if (idx == 0)
29112daf946SGreg Clayton             {
2925082c5fdSGreg Clayton                 // We might have already created frame zero, only create it
2935082c5fdSGreg Clayton                 // if we need to
2945082c5fdSGreg Clayton                 if (m_frames.empty())
2955082c5fdSGreg Clayton                 {
29612daf946SGreg Clayton                     m_thread.GetRegisterContext();
2971692b901SJim Ingham                     assert (m_thread.m_reg_context_sp.get());
298376c4854SJim Ingham 
299376c4854SJim Ingham                     const bool success = unwinder->GetFrameInfoAtIndex(idx, cfa, pc);
300376c4854SJim Ingham                     // There shouldn't be any way not to get the frame info for frame 0.
301376c4854SJim Ingham                     // But if the unwinder can't make one, lets make one by hand with the
302376c4854SJim Ingham                     // SP as the CFA and see if that gets any further.
303376c4854SJim Ingham                     if (!success)
304376c4854SJim Ingham                     {
305376c4854SJim Ingham                         cfa = m_thread.GetRegisterContext()->GetSP();
306376c4854SJim Ingham                         pc = m_thread.GetRegisterContext()->GetPC();
307376c4854SJim Ingham                     }
308376c4854SJim Ingham 
309d9e416c0SGreg Clayton                     unwind_frame_sp.reset (new StackFrame (m_thread.shared_from_this(),
310d9e416c0SGreg Clayton                                                            m_frames.size(),
3115082c5fdSGreg Clayton                                                            idx,
31212daf946SGreg Clayton                                                            m_thread.m_reg_context_sp,
31359e8fc1cSGreg Clayton                                                            cfa,
3141692b901SJim Ingham                                                            pc,
31512daf946SGreg Clayton                                                            NULL));
3165082c5fdSGreg Clayton                     m_frames.push_back (unwind_frame_sp);
3175082c5fdSGreg Clayton                 }
3185082c5fdSGreg Clayton                 else
3195082c5fdSGreg Clayton                 {
3205082c5fdSGreg Clayton                     unwind_frame_sp = m_frames.front();
32159e8fc1cSGreg Clayton                     cfa = unwind_frame_sp->m_id.GetCallFrameAddress();
3225082c5fdSGreg Clayton                 }
32312daf946SGreg Clayton             }
32412daf946SGreg Clayton             else
32512daf946SGreg Clayton             {
32612daf946SGreg Clayton                 const bool success = unwinder->GetFrameInfoAtIndex(idx, cfa, pc);
327b0c72a5fSJim Ingham                 if (!success)
328b0c72a5fSJim Ingham                 {
329b0c72a5fSJim Ingham                     // We've gotten to the end of the stack.
330b0c72a5fSJim Ingham                     SetAllFramesFetched();
331b0c72a5fSJim Ingham                     break;
332b0c72a5fSJim Ingham                 }
333d9e416c0SGreg Clayton                 unwind_frame_sp.reset (new StackFrame (m_thread.shared_from_this(), m_frames.size(), idx, cfa, pc, NULL));
3345082c5fdSGreg Clayton                 m_frames.push_back (unwind_frame_sp);
33512daf946SGreg Clayton             }
33612daf946SGreg Clayton 
3371ed54f50SGreg Clayton             SymbolContext unwind_sc = unwind_frame_sp->GetSymbolContext (eSymbolContextBlock | eSymbolContextFunction);
3381ed54f50SGreg Clayton             Block *unwind_block = unwind_sc.block;
33959e8fc1cSGreg Clayton             if (unwind_block)
34012daf946SGreg Clayton             {
3415f4c61e2SGreg Clayton                 Address curr_frame_address (unwind_frame_sp->GetFrameCodeAddress());
3425f4c61e2SGreg Clayton                 // Be sure to adjust the frame address to match the address
3435f4c61e2SGreg Clayton                 // that was used to lookup the symbol context above. If we are
3445f4c61e2SGreg Clayton                 // in the first concrete frame, then we lookup using the current
3455f4c61e2SGreg Clayton                 // address, else we decrement the address by one to get the correct
3465f4c61e2SGreg Clayton                 // location.
3475f4c61e2SGreg Clayton                 if (idx > 0)
3485f4c61e2SGreg Clayton                     curr_frame_address.Slide(-1);
3495f4c61e2SGreg Clayton 
3501ed54f50SGreg Clayton                 SymbolContext next_frame_sc;
3511ed54f50SGreg Clayton                 Address next_frame_address;
3521ed54f50SGreg Clayton 
3531ed54f50SGreg Clayton                 while (unwind_sc.GetParentOfInlinedScope(curr_frame_address, next_frame_sc, next_frame_address))
35459e8fc1cSGreg Clayton                 {
355d9e416c0SGreg Clayton                         StackFrameSP frame_sp(new StackFrame (m_thread.shared_from_this(),
356d9e416c0SGreg Clayton                                                               m_frames.size(),
3575082c5fdSGreg Clayton                                                               idx,
3585082c5fdSGreg Clayton                                                               unwind_frame_sp->GetRegisterContextSP (),
35959e8fc1cSGreg Clayton                                                               cfa,
3601ed54f50SGreg Clayton                                                               next_frame_address,
3611ed54f50SGreg Clayton                                                               &next_frame_sc));
3625082c5fdSGreg Clayton 
3635082c5fdSGreg Clayton                         m_frames.push_back (frame_sp);
3641ed54f50SGreg Clayton                         unwind_sc = next_frame_sc;
3651ed54f50SGreg Clayton                         curr_frame_address = next_frame_address;
366b0c72a5fSJim Ingham                 }
367b0c72a5fSJim Ingham             }
368b0c72a5fSJim Ingham         } while (m_frames.size() - 1 < end_idx);
3691ed54f50SGreg Clayton 
370b0c72a5fSJim Ingham         // Don't try to merge till you've calculated all the frames in this stack.
371b0c72a5fSJim Ingham         if (GetAllFramesFetched() && m_prev_frames_sp)
37212daf946SGreg Clayton         {
3732cad65a5SGreg Clayton             StackFrameList *prev_frames = m_prev_frames_sp.get();
3745082c5fdSGreg Clayton             StackFrameList *curr_frames = this;
3755082c5fdSGreg Clayton 
3766cd41da7SJim Ingham             //curr_frames->m_current_inlined_depth = prev_frames->m_current_inlined_depth;
3776cd41da7SJim Ingham             //curr_frames->m_current_inlined_pc = prev_frames->m_current_inlined_pc;
378d01b2953SDaniel Malea             //printf ("GetFramesUpTo: Copying current inlined depth: %d 0x%" PRIx64 ".\n", curr_frames->m_current_inlined_depth, curr_frames->m_current_inlined_pc);
3799786eeebSJim Ingham 
3805082c5fdSGreg Clayton #if defined (DEBUG_STACK_FRAMES)
38168275d5eSGreg Clayton             s.PutCString("\nprev_frames:\n");
3825082c5fdSGreg Clayton             prev_frames->Dump (&s);
38368275d5eSGreg Clayton             s.PutCString("\ncurr_frames:\n");
3845082c5fdSGreg Clayton             curr_frames->Dump (&s);
3855082c5fdSGreg Clayton             s.EOL();
3865082c5fdSGreg Clayton #endif
3875082c5fdSGreg Clayton             size_t curr_frame_num, prev_frame_num;
3885082c5fdSGreg Clayton 
3895082c5fdSGreg Clayton             for (curr_frame_num = curr_frames->m_frames.size(), prev_frame_num = prev_frames->m_frames.size();
3905082c5fdSGreg Clayton                  curr_frame_num > 0 && prev_frame_num > 0;
3915082c5fdSGreg Clayton                  --curr_frame_num, --prev_frame_num)
3925082c5fdSGreg Clayton             {
3935082c5fdSGreg Clayton                 const size_t curr_frame_idx = curr_frame_num-1;
3945082c5fdSGreg Clayton                 const size_t prev_frame_idx = prev_frame_num-1;
3955082c5fdSGreg Clayton                 StackFrameSP curr_frame_sp (curr_frames->m_frames[curr_frame_idx]);
3965082c5fdSGreg Clayton                 StackFrameSP prev_frame_sp (prev_frames->m_frames[prev_frame_idx]);
3975082c5fdSGreg Clayton 
3985082c5fdSGreg Clayton #if defined (DEBUG_STACK_FRAMES)
3992cad65a5SGreg Clayton                 s.Printf("\n\nCurr frame #%u ", curr_frame_idx);
4005082c5fdSGreg Clayton                 if (curr_frame_sp)
4012cad65a5SGreg Clayton                     curr_frame_sp->Dump (&s, true, false);
4025082c5fdSGreg Clayton                 else
4035082c5fdSGreg Clayton                     s.PutCString("NULL");
4042cad65a5SGreg Clayton                 s.Printf("\nPrev frame #%u ", prev_frame_idx);
4055082c5fdSGreg Clayton                 if (prev_frame_sp)
4062cad65a5SGreg Clayton                     prev_frame_sp->Dump (&s, true, false);
4075082c5fdSGreg Clayton                 else
4085082c5fdSGreg Clayton                     s.PutCString("NULL");
4095082c5fdSGreg Clayton #endif
4105082c5fdSGreg Clayton 
4115082c5fdSGreg Clayton                 StackFrame *curr_frame = curr_frame_sp.get();
4125082c5fdSGreg Clayton                 StackFrame *prev_frame = prev_frame_sp.get();
4135082c5fdSGreg Clayton 
4145082c5fdSGreg Clayton                 if (curr_frame == NULL || prev_frame == NULL)
4155082c5fdSGreg Clayton                     break;
4165082c5fdSGreg Clayton 
41759e8fc1cSGreg Clayton                 // Check the stack ID to make sure they are equal
41859e8fc1cSGreg Clayton                 if (curr_frame->GetStackID() != prev_frame->GetStackID())
4195082c5fdSGreg Clayton                     break;
4205082c5fdSGreg Clayton 
42159e8fc1cSGreg Clayton                 prev_frame->UpdatePreviousFrameFromCurrentFrame (*curr_frame);
42259e8fc1cSGreg Clayton                 // Now copy the fixed up previous frame into the current frames
42359e8fc1cSGreg Clayton                 // so the pointer doesn't change
42459e8fc1cSGreg Clayton                 m_frames[curr_frame_idx] = prev_frame_sp;
42559e8fc1cSGreg Clayton                 //curr_frame->UpdateCurrentFrameFromPreviousFrame (*prev_frame);
4265082c5fdSGreg Clayton 
4275082c5fdSGreg Clayton #if defined (DEBUG_STACK_FRAMES)
42868275d5eSGreg Clayton                 s.Printf("\n    Copying previous frame to current frame");
4295082c5fdSGreg Clayton #endif
4305082c5fdSGreg Clayton             }
4315082c5fdSGreg Clayton             // We are done with the old stack frame list, we can release it now
4322cad65a5SGreg Clayton             m_prev_frames_sp.reset();
4335082c5fdSGreg Clayton         }
43468275d5eSGreg Clayton 
43568275d5eSGreg Clayton #if defined (DEBUG_STACK_FRAMES)
43668275d5eSGreg Clayton             s.PutCString("\n\nNew frames:\n");
43768275d5eSGreg Clayton             Dump (&s);
43868275d5eSGreg Clayton             s.EOL();
43968275d5eSGreg Clayton #endif
44012daf946SGreg Clayton     }
44112daf946SGreg Clayton     else
44212daf946SGreg Clayton     {
443b0c72a5fSJim Ingham         if (end_idx < m_concrete_frames_fetched)
444b0c72a5fSJim Ingham             return;
445b0c72a5fSJim Ingham 
446b0c72a5fSJim Ingham         uint32_t num_frames = unwinder->GetFramesUpTo(end_idx);
447b0c72a5fSJim Ingham         if (num_frames <= end_idx + 1)
448b0c72a5fSJim Ingham         {
449b0c72a5fSJim Ingham             //Done unwinding.
450b0c72a5fSJim Ingham             m_concrete_frames_fetched = UINT32_MAX;
451b0c72a5fSJim Ingham         }
452b0c72a5fSJim Ingham         m_frames.resize(num_frames);
45312daf946SGreg Clayton     }
4545082c5fdSGreg Clayton }
455b0c72a5fSJim Ingham 
456b0c72a5fSJim Ingham uint32_t
457b0c72a5fSJim Ingham StackFrameList::GetNumFrames (bool can_create)
458b0c72a5fSJim Ingham {
459b0c72a5fSJim Ingham     Mutex::Locker locker (m_mutex);
460b0c72a5fSJim Ingham 
461b0c72a5fSJim Ingham     if (can_create)
462b0c72a5fSJim Ingham         GetFramesUpTo (UINT32_MAX);
463513c6bb8SJim Ingham 
464513c6bb8SJim Ingham     uint32_t inlined_depth = GetCurrentInlinedDepth();
465513c6bb8SJim Ingham     if (inlined_depth == UINT32_MAX)
4665082c5fdSGreg Clayton         return m_frames.size();
467513c6bb8SJim Ingham     else
468513c6bb8SJim Ingham         return m_frames.size() - inlined_depth;
46930fdc8d8SChris Lattner }
47030fdc8d8SChris Lattner 
4715082c5fdSGreg Clayton void
4725082c5fdSGreg Clayton StackFrameList::Dump (Stream *s)
47330fdc8d8SChris Lattner {
4745082c5fdSGreg Clayton     if (s == NULL)
4755082c5fdSGreg Clayton         return;
4765082c5fdSGreg Clayton     Mutex::Locker locker (m_mutex);
47730fdc8d8SChris Lattner 
4785082c5fdSGreg Clayton     const_iterator pos, begin = m_frames.begin(), end = m_frames.end();
4795082c5fdSGreg Clayton     for (pos = begin; pos != end; ++pos)
48012daf946SGreg Clayton     {
4815082c5fdSGreg Clayton         StackFrame *frame = (*pos).get();
4825082c5fdSGreg Clayton         s->Printf("%p: ", frame);
4835082c5fdSGreg Clayton         if (frame)
48459e8fc1cSGreg Clayton         {
48559e8fc1cSGreg Clayton             frame->GetStackID().Dump (s);
4860603aa9dSGreg Clayton             frame->DumpUsingSettingsFormat (s);
48759e8fc1cSGreg Clayton         }
4885082c5fdSGreg Clayton         else
489dce502edSGreg Clayton             s->Printf("frame #%u", (uint32_t)std::distance (begin, pos));
4905082c5fdSGreg Clayton         s->EOL();
49112daf946SGreg Clayton     }
4925082c5fdSGreg Clayton     s->EOL();
4935082c5fdSGreg Clayton }
49412daf946SGreg Clayton 
49530fdc8d8SChris Lattner StackFrameSP
49612daf946SGreg Clayton StackFrameList::GetFrameAtIndex (uint32_t idx)
49730fdc8d8SChris Lattner {
49830fdc8d8SChris Lattner     StackFrameSP frame_sp;
49930fdc8d8SChris Lattner     Mutex::Locker locker (m_mutex);
500513c6bb8SJim Ingham     uint32_t inlined_depth = GetCurrentInlinedDepth();
501513c6bb8SJim Ingham     if (inlined_depth != UINT32_MAX)
502513c6bb8SJim Ingham         idx += inlined_depth;
503513c6bb8SJim Ingham 
5045082c5fdSGreg Clayton     if (idx < m_frames.size())
5055082c5fdSGreg Clayton         frame_sp = m_frames[idx];
50612daf946SGreg Clayton 
5075082c5fdSGreg Clayton     if (frame_sp)
50812daf946SGreg Clayton         return frame_sp;
50912daf946SGreg Clayton 
5101692b901SJim Ingham         // GetFramesUpTo will fill m_frames with as many frames as you asked for,
5111692b901SJim Ingham         // if there are that many.  If there weren't then you asked for too many
5121692b901SJim Ingham         // frames.
513b0c72a5fSJim Ingham         GetFramesUpTo (idx);
514b0c72a5fSJim Ingham         if (idx < m_frames.size())
51512daf946SGreg Clayton         {
51612daf946SGreg Clayton             if (m_show_inlined_frames)
51712daf946SGreg Clayton             {
5181692b901SJim Ingham                 // When inline frames are enabled we actually create all the frames in GetFramesUpTo.
5195082c5fdSGreg Clayton                 frame_sp = m_frames[idx];
52012daf946SGreg Clayton             }
52112daf946SGreg Clayton             else
52212daf946SGreg Clayton             {
52312daf946SGreg Clayton                 Unwind *unwinder = m_thread.GetUnwinder ();
52412daf946SGreg Clayton                 if (unwinder)
52512daf946SGreg Clayton                 {
52612daf946SGreg Clayton                     addr_t pc, cfa;
52712daf946SGreg Clayton                     if (unwinder->GetFrameInfoAtIndex(idx, cfa, pc))
5285082c5fdSGreg Clayton                     {
529d9e416c0SGreg Clayton                         frame_sp.reset (new StackFrame (m_thread.shared_from_this(), idx, idx, cfa, pc, NULL));
53059e8fc1cSGreg Clayton 
53159e8fc1cSGreg Clayton                         Function *function = frame_sp->GetSymbolContext (eSymbolContextFunction).function;
53259e8fc1cSGreg Clayton                         if (function)
53359e8fc1cSGreg Clayton                         {
53459e8fc1cSGreg Clayton                             // When we aren't showing inline functions we always use
53559e8fc1cSGreg Clayton                             // the top most function block as the scope.
53659e8fc1cSGreg Clayton                             frame_sp->SetSymbolContextScope (&function->GetBlock(false));
53759e8fc1cSGreg Clayton                         }
53859e8fc1cSGreg Clayton                         else
53959e8fc1cSGreg Clayton                         {
54059e8fc1cSGreg Clayton                             // Set the symbol scope from the symbol regardless if it is NULL or valid.
54159e8fc1cSGreg Clayton                             frame_sp->SetSymbolContextScope (frame_sp->GetSymbolContext (eSymbolContextSymbol).symbol);
54259e8fc1cSGreg Clayton                         }
5435082c5fdSGreg Clayton                         SetFrameAtIndex(idx, frame_sp);
54412daf946SGreg Clayton                     }
54512daf946SGreg Clayton                 }
54612daf946SGreg Clayton             }
54730fdc8d8SChris Lattner         }
54830fdc8d8SChris Lattner     return frame_sp;
54930fdc8d8SChris Lattner }
55030fdc8d8SChris Lattner 
5515ccbd294SGreg Clayton StackFrameSP
5525ccbd294SGreg Clayton StackFrameList::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx)
5535ccbd294SGreg Clayton {
5545ccbd294SGreg Clayton     // First try assuming the unwind index is the same as the frame index. The
5555ccbd294SGreg Clayton     // unwind index is always greater than or equal to the frame index, so it
5565ccbd294SGreg Clayton     // is a good place to start. If we have inlined frames we might have 5
5575ccbd294SGreg Clayton     // concrete frames (frame unwind indexes go from 0-4), but we might have 15
5585ccbd294SGreg Clayton     // frames after we make all the inlined frames. Most of the time the unwind
5595ccbd294SGreg Clayton     // frame index (or the concrete frame index) is the same as the frame index.
5605ccbd294SGreg Clayton     uint32_t frame_idx = unwind_idx;
5615ccbd294SGreg Clayton     StackFrameSP frame_sp (GetFrameAtIndex (frame_idx));
5625ccbd294SGreg Clayton     while (frame_sp)
5635ccbd294SGreg Clayton     {
5645ccbd294SGreg Clayton         if (frame_sp->GetFrameIndex() == unwind_idx)
5655ccbd294SGreg Clayton             break;
5665ccbd294SGreg Clayton         frame_sp = GetFrameAtIndex (++frame_idx);
5675ccbd294SGreg Clayton     }
5685ccbd294SGreg Clayton     return frame_sp;
5695ccbd294SGreg Clayton }
5705ccbd294SGreg Clayton 
5713a195b7eSJim Ingham StackFrameSP
572cc4d0146SGreg Clayton StackFrameList::GetFrameWithStackID (const StackID &stack_id)
5733a195b7eSJim Ingham {
5743a195b7eSJim Ingham     uint32_t frame_idx = 0;
5753a195b7eSJim Ingham     StackFrameSP frame_sp;
5763a195b7eSJim Ingham     do
5773a195b7eSJim Ingham     {
5783a195b7eSJim Ingham         frame_sp = GetFrameAtIndex (frame_idx);
5793a195b7eSJim Ingham         if (frame_sp && frame_sp->GetStackID() == stack_id)
5803a195b7eSJim Ingham             break;
5813a195b7eSJim Ingham         frame_idx++;
5823a195b7eSJim Ingham     }
5833a195b7eSJim Ingham     while (frame_sp);
5843a195b7eSJim Ingham     return frame_sp;
5853a195b7eSJim Ingham }
5865ccbd294SGreg Clayton 
58712daf946SGreg Clayton bool
5885082c5fdSGreg Clayton StackFrameList::SetFrameAtIndex (uint32_t idx, StackFrameSP &frame_sp)
58912daf946SGreg Clayton {
5905082c5fdSGreg Clayton     if (idx >= m_frames.size())
5915082c5fdSGreg Clayton         m_frames.resize(idx + 1);
59212daf946SGreg Clayton     // Make sure allocation succeeded by checking bounds again
5935082c5fdSGreg Clayton     if (idx < m_frames.size())
59412daf946SGreg Clayton     {
5955082c5fdSGreg Clayton         m_frames[idx] = frame_sp;
59630fdc8d8SChris Lattner         return true;
59730fdc8d8SChris Lattner     }
59830fdc8d8SChris Lattner     return false;   // resize failed, out of memory?
59930fdc8d8SChris Lattner }
60030fdc8d8SChris Lattner 
60130fdc8d8SChris Lattner uint32_t
6022976d00aSJim Ingham StackFrameList::GetSelectedFrameIndex () const
60330fdc8d8SChris Lattner {
60430fdc8d8SChris Lattner     Mutex::Locker locker (m_mutex);
6052976d00aSJim Ingham     return m_selected_frame_idx;
60630fdc8d8SChris Lattner }
60730fdc8d8SChris Lattner 
60830fdc8d8SChris Lattner 
60930fdc8d8SChris Lattner uint32_t
6102976d00aSJim Ingham StackFrameList::SetSelectedFrame (lldb_private::StackFrame *frame)
61130fdc8d8SChris Lattner {
61230fdc8d8SChris Lattner     Mutex::Locker locker (m_mutex);
61312daf946SGreg Clayton     const_iterator pos;
6145082c5fdSGreg Clayton     const_iterator begin = m_frames.begin();
6155082c5fdSGreg Clayton     const_iterator end = m_frames.end();
616b7f6b2faSJim Ingham     m_selected_frame_idx = 0;
61730fdc8d8SChris Lattner     for (pos = begin; pos != end; ++pos)
61830fdc8d8SChris Lattner     {
61930fdc8d8SChris Lattner         if (pos->get() == frame)
62030fdc8d8SChris Lattner         {
6212976d00aSJim Ingham             m_selected_frame_idx = std::distance (begin, pos);
622513c6bb8SJim Ingham             uint32_t inlined_depth = GetCurrentInlinedDepth();
623513c6bb8SJim Ingham             if (inlined_depth != UINT32_MAX)
624513c6bb8SJim Ingham                 m_selected_frame_idx -= inlined_depth;
625b7f6b2faSJim Ingham             break;
62630fdc8d8SChris Lattner         }
62730fdc8d8SChris Lattner     }
628b7f6b2faSJim Ingham     SetDefaultFileAndLineToSelectedFrame();
6292976d00aSJim Ingham     return m_selected_frame_idx;
63030fdc8d8SChris Lattner }
63130fdc8d8SChris Lattner 
63230fdc8d8SChris Lattner // Mark a stack frame as the current frame using the frame index
633b0c72a5fSJim Ingham bool
6342976d00aSJim Ingham StackFrameList::SetSelectedFrameByIndex (uint32_t idx)
63530fdc8d8SChris Lattner {
63630fdc8d8SChris Lattner     Mutex::Locker locker (m_mutex);
637b0c72a5fSJim Ingham     StackFrameSP frame_sp (GetFrameAtIndex (idx));
638b0c72a5fSJim Ingham     if (frame_sp)
639b0c72a5fSJim Ingham     {
640b0c72a5fSJim Ingham         SetSelectedFrame(frame_sp.get());
641b0c72a5fSJim Ingham         return true;
642b0c72a5fSJim Ingham     }
643b0c72a5fSJim Ingham     else
644b0c72a5fSJim Ingham         return false;
645b7f6b2faSJim Ingham }
646b7f6b2faSJim Ingham 
647b7f6b2faSJim Ingham void
648b7f6b2faSJim Ingham StackFrameList::SetDefaultFileAndLineToSelectedFrame()
649b7f6b2faSJim Ingham {
6501ac04c30SGreg Clayton     if (m_thread.GetID() == m_thread.GetProcess()->GetThreadList().GetSelectedThread()->GetID())
651b7f6b2faSJim Ingham     {
652252d0edeSGreg Clayton         StackFrameSP frame_sp (GetFrameAtIndex (GetSelectedFrameIndex()));
653b7f6b2faSJim Ingham         if (frame_sp)
654b7f6b2faSJim Ingham         {
655252d0edeSGreg Clayton             SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextLineEntry);
656b7f6b2faSJim Ingham             if (sc.line_entry.file)
6571ac04c30SGreg Clayton                 m_thread.CalculateTarget()->GetSourceManager().SetDefaultFileAndLine (sc.line_entry.file,
658252d0edeSGreg Clayton                                                                                             sc.line_entry.line);
659b7f6b2faSJim Ingham         }
660b7f6b2faSJim Ingham     }
66130fdc8d8SChris Lattner }
66230fdc8d8SChris Lattner 
66330fdc8d8SChris Lattner // The thread has been run, reset the number stack frames to zero so we can
66430fdc8d8SChris Lattner // determine how many frames we have lazily.
66530fdc8d8SChris Lattner void
66630fdc8d8SChris Lattner StackFrameList::Clear ()
66730fdc8d8SChris Lattner {
66830fdc8d8SChris Lattner     Mutex::Locker locker (m_mutex);
6695082c5fdSGreg Clayton     m_frames.clear();
670b0c72a5fSJim Ingham     m_concrete_frames_fetched = 0;
67130fdc8d8SChris Lattner }
67230fdc8d8SChris Lattner 
67330fdc8d8SChris Lattner void
67430fdc8d8SChris Lattner StackFrameList::InvalidateFrames (uint32_t start_idx)
67530fdc8d8SChris Lattner {
67630fdc8d8SChris Lattner     Mutex::Locker locker (m_mutex);
67712daf946SGreg Clayton     if (m_show_inlined_frames)
67812daf946SGreg Clayton     {
67912daf946SGreg Clayton         Clear();
68012daf946SGreg Clayton     }
68112daf946SGreg Clayton     else
68212daf946SGreg Clayton     {
6835082c5fdSGreg Clayton         const size_t num_frames = m_frames.size();
68430fdc8d8SChris Lattner         while (start_idx < num_frames)
68530fdc8d8SChris Lattner         {
6865082c5fdSGreg Clayton             m_frames[start_idx].reset();
68730fdc8d8SChris Lattner             ++start_idx;
68830fdc8d8SChris Lattner         }
68930fdc8d8SChris Lattner     }
69012daf946SGreg Clayton }
6912cad65a5SGreg Clayton 
6922cad65a5SGreg Clayton void
6932cad65a5SGreg Clayton StackFrameList::Merge (std::auto_ptr<StackFrameList>& curr_ap, lldb::StackFrameListSP& prev_sp)
6942cad65a5SGreg Clayton {
69510ebffa4SJim Ingham     Mutex::Locker curr_locker (curr_ap.get() ? &curr_ap->m_mutex : NULL);
69610ebffa4SJim Ingham     Mutex::Locker prev_locker (prev_sp.get() ? &prev_sp->m_mutex : NULL);
6972cad65a5SGreg Clayton 
6982cad65a5SGreg Clayton #if defined (DEBUG_STACK_FRAMES)
6992c595273SJohnny Chen     StreamFile s(stdout, false);
7002cad65a5SGreg Clayton     s.PutCString("\n\nStackFrameList::Merge():\nPrev:\n");
7012cad65a5SGreg Clayton     if (prev_sp.get())
7022cad65a5SGreg Clayton         prev_sp->Dump (&s);
7032cad65a5SGreg Clayton     else
7042cad65a5SGreg Clayton         s.PutCString ("NULL");
7052cad65a5SGreg Clayton     s.PutCString("\nCurr:\n");
7062cad65a5SGreg Clayton     if (curr_ap.get())
7072cad65a5SGreg Clayton         curr_ap->Dump (&s);
7082cad65a5SGreg Clayton     else
7092cad65a5SGreg Clayton         s.PutCString ("NULL");
7102cad65a5SGreg Clayton     s.EOL();
7112cad65a5SGreg Clayton #endif
7122cad65a5SGreg Clayton 
7132cad65a5SGreg Clayton     if (curr_ap.get() == NULL || curr_ap->GetNumFrames (false) == 0)
7142cad65a5SGreg Clayton     {
7152cad65a5SGreg Clayton #if defined (DEBUG_STACK_FRAMES)
7162cad65a5SGreg Clayton         s.PutCString("No current frames, leave previous frames alone...\n");
7172cad65a5SGreg Clayton #endif
7182cad65a5SGreg Clayton         curr_ap.release();
7192cad65a5SGreg Clayton         return;
7202cad65a5SGreg Clayton     }
7212cad65a5SGreg Clayton 
7222cad65a5SGreg Clayton     if (prev_sp.get() == NULL || prev_sp->GetNumFrames (false) == 0)
7232cad65a5SGreg Clayton     {
7242cad65a5SGreg Clayton #if defined (DEBUG_STACK_FRAMES)
7252cad65a5SGreg Clayton         s.PutCString("No previous frames, so use current frames...\n");
7262cad65a5SGreg Clayton #endif
7272cad65a5SGreg Clayton         // We either don't have any previous frames, or since we have more than
7282cad65a5SGreg Clayton         // one current frames it means we have all the frames and can safely
7292cad65a5SGreg Clayton         // replace our previous frames.
7302cad65a5SGreg Clayton         prev_sp.reset (curr_ap.release());
7312cad65a5SGreg Clayton         return;
7322cad65a5SGreg Clayton     }
7332cad65a5SGreg Clayton 
7342cad65a5SGreg Clayton     const uint32_t num_curr_frames = curr_ap->GetNumFrames (false);
7352cad65a5SGreg Clayton 
7362cad65a5SGreg Clayton     if (num_curr_frames > 1)
7372cad65a5SGreg Clayton     {
7382cad65a5SGreg Clayton #if defined (DEBUG_STACK_FRAMES)
7392cad65a5SGreg Clayton         s.PutCString("We have more than one current frame, so use current frames...\n");
7402cad65a5SGreg Clayton #endif
7412cad65a5SGreg Clayton         // We have more than one current frames it means we have all the frames
7422cad65a5SGreg Clayton         // and can safely replace our previous frames.
7432cad65a5SGreg Clayton         prev_sp.reset (curr_ap.release());
7442cad65a5SGreg Clayton 
7452cad65a5SGreg Clayton #if defined (DEBUG_STACK_FRAMES)
7462cad65a5SGreg Clayton         s.PutCString("\nMerged:\n");
7472cad65a5SGreg Clayton         prev_sp->Dump (&s);
7482cad65a5SGreg Clayton #endif
7492cad65a5SGreg Clayton         return;
7502cad65a5SGreg Clayton     }
7512cad65a5SGreg Clayton 
7522cad65a5SGreg Clayton     StackFrameSP prev_frame_zero_sp(prev_sp->GetFrameAtIndex (0));
7532cad65a5SGreg Clayton     StackFrameSP curr_frame_zero_sp(curr_ap->GetFrameAtIndex (0));
7542cad65a5SGreg Clayton     StackID curr_stack_id (curr_frame_zero_sp->GetStackID());
7552cad65a5SGreg Clayton     StackID prev_stack_id (prev_frame_zero_sp->GetStackID());
7562cad65a5SGreg Clayton 
7572cad65a5SGreg Clayton #if defined (DEBUG_STACK_FRAMES)
7582c595273SJohnny Chen     const uint32_t num_prev_frames = prev_sp->GetNumFrames (false);
7592cad65a5SGreg Clayton     s.Printf("\n%u previous frames with one current frame\n", num_prev_frames);
7602cad65a5SGreg Clayton #endif
7612cad65a5SGreg Clayton 
7622cad65a5SGreg Clayton     // We have only a single current frame
7632cad65a5SGreg Clayton     // Our previous stack frames only had a single frame as well...
7642cad65a5SGreg Clayton     if (curr_stack_id == prev_stack_id)
7652cad65a5SGreg Clayton     {
7662cad65a5SGreg Clayton #if defined (DEBUG_STACK_FRAMES)
7672cad65a5SGreg Clayton         s.Printf("\nPrevious frame #0 is same as current frame #0, merge the cached data\n");
7682cad65a5SGreg Clayton #endif
7692cad65a5SGreg Clayton 
7702cad65a5SGreg Clayton         curr_frame_zero_sp->UpdateCurrentFrameFromPreviousFrame (*prev_frame_zero_sp);
7712cad65a5SGreg Clayton //        prev_frame_zero_sp->UpdatePreviousFrameFromCurrentFrame (*curr_frame_zero_sp);
7722cad65a5SGreg Clayton //        prev_sp->SetFrameAtIndex (0, prev_frame_zero_sp);
7732cad65a5SGreg Clayton     }
7742cad65a5SGreg Clayton     else if (curr_stack_id < prev_stack_id)
7752cad65a5SGreg Clayton     {
7762cad65a5SGreg Clayton #if defined (DEBUG_STACK_FRAMES)
7772cad65a5SGreg Clayton         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");
7782cad65a5SGreg Clayton #endif
7792cad65a5SGreg Clayton         prev_sp->m_frames.insert (prev_sp->m_frames.begin(), curr_frame_zero_sp);
7802cad65a5SGreg Clayton     }
7812cad65a5SGreg Clayton 
7822cad65a5SGreg Clayton     curr_ap.release();
7832cad65a5SGreg Clayton 
7842cad65a5SGreg Clayton #if defined (DEBUG_STACK_FRAMES)
7852cad65a5SGreg Clayton     s.PutCString("\nMerged:\n");
7862cad65a5SGreg Clayton     prev_sp->Dump (&s);
7872cad65a5SGreg Clayton #endif
7882cad65a5SGreg Clayton 
7892cad65a5SGreg Clayton 
7902cad65a5SGreg Clayton }
791e4284b71SJim Ingham 
792e4284b71SJim Ingham lldb::StackFrameSP
793e4284b71SJim Ingham StackFrameList::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr)
794e4284b71SJim Ingham {
795e4284b71SJim Ingham     const_iterator pos;
796e4284b71SJim Ingham     const_iterator begin = m_frames.begin();
797e4284b71SJim Ingham     const_iterator end = m_frames.end();
798e4284b71SJim Ingham     lldb::StackFrameSP ret_sp;
799e4284b71SJim Ingham 
800e4284b71SJim Ingham     for (pos = begin; pos != end; ++pos)
801e4284b71SJim Ingham     {
802e4284b71SJim Ingham         if (pos->get() == stack_frame_ptr)
803e4284b71SJim Ingham         {
804e4284b71SJim Ingham             ret_sp = (*pos);
805e4284b71SJim Ingham             break;
806e4284b71SJim Ingham         }
807e4284b71SJim Ingham     }
808e4284b71SJim Ingham     return ret_sp;
809e4284b71SJim Ingham }
810e4284b71SJim Ingham 
8117260f620SGreg Clayton size_t
8127260f620SGreg Clayton StackFrameList::GetStatus (Stream& strm,
8137260f620SGreg Clayton                            uint32_t first_frame,
8147260f620SGreg Clayton                            uint32_t num_frames,
8157260f620SGreg Clayton                            bool show_frame_info,
81653eb7ad2SGreg Clayton                            uint32_t num_frames_with_source)
8177260f620SGreg Clayton {
8187260f620SGreg Clayton     size_t num_frames_displayed = 0;
8197260f620SGreg Clayton 
8207260f620SGreg Clayton     if (num_frames == 0)
8217260f620SGreg Clayton         return 0;
8227260f620SGreg Clayton 
8237260f620SGreg Clayton     StackFrameSP frame_sp;
8247260f620SGreg Clayton     uint32_t frame_idx = 0;
8257260f620SGreg Clayton     uint32_t last_frame;
8267260f620SGreg Clayton 
8277260f620SGreg Clayton     // Don't let the last frame wrap around...
8287260f620SGreg Clayton     if (num_frames == UINT32_MAX)
8297260f620SGreg Clayton         last_frame = UINT32_MAX;
8307260f620SGreg Clayton     else
8317260f620SGreg Clayton         last_frame = first_frame + num_frames;
8327260f620SGreg Clayton 
8337260f620SGreg Clayton     for (frame_idx = first_frame; frame_idx < last_frame; ++frame_idx)
8347260f620SGreg Clayton     {
8357260f620SGreg Clayton         frame_sp = GetFrameAtIndex(frame_idx);
8367260f620SGreg Clayton         if (frame_sp.get() == NULL)
8377260f620SGreg Clayton             break;
8387260f620SGreg Clayton 
8397260f620SGreg Clayton         if (!frame_sp->GetStatus (strm,
8407260f620SGreg Clayton                                   show_frame_info,
84153eb7ad2SGreg Clayton                                   num_frames_with_source > (first_frame - frame_idx)))
8427260f620SGreg Clayton             break;
8437260f620SGreg Clayton         ++num_frames_displayed;
8447260f620SGreg Clayton     }
8457260f620SGreg Clayton 
8467260f620SGreg Clayton     strm.IndentLess();
8477260f620SGreg Clayton     return num_frames_displayed;
8487260f620SGreg Clayton }
8497260f620SGreg Clayton 
850