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
165082c5fdSGreg Clayton #include "lldb/Core/StreamFile.h"
17b7f6b2faSJim Ingham #include "lldb/Core/SourceManager.h"
1812daf946SGreg Clayton #include "lldb/Symbol/Block.h"
1912daf946SGreg Clayton #include "lldb/Symbol/Function.h"
2059e8fc1cSGreg Clayton #include "lldb/Symbol/Symbol.h"
21b7f6b2faSJim Ingham #include "lldb/Target/Process.h"
2212daf946SGreg Clayton #include "lldb/Target/RegisterContext.h"
2330fdc8d8SChris Lattner #include "lldb/Target/StackFrame.h"
24*513c6bb8SJim Ingham #include "lldb/Target/StopInfo.h"
25b7f6b2faSJim Ingham #include "lldb/Target/Target.h"
2612daf946SGreg Clayton #include "lldb/Target/Thread.h"
2712daf946SGreg Clayton #include "lldb/Target/Unwind.h"
2830fdc8d8SChris Lattner 
295082c5fdSGreg Clayton //#define DEBUG_STACK_FRAMES 1
305082c5fdSGreg Clayton 
3130fdc8d8SChris Lattner using namespace lldb;
3230fdc8d8SChris Lattner using namespace lldb_private;
3330fdc8d8SChris Lattner 
3430fdc8d8SChris Lattner //----------------------------------------------------------------------
3530fdc8d8SChris Lattner // StackFrameList constructor
3630fdc8d8SChris Lattner //----------------------------------------------------------------------
372cad65a5SGreg Clayton StackFrameList::StackFrameList
382cad65a5SGreg Clayton (
392cad65a5SGreg Clayton     Thread &thread,
402cad65a5SGreg Clayton     const lldb::StackFrameListSP &prev_frames_sp,
412cad65a5SGreg Clayton     bool show_inline_frames
422cad65a5SGreg Clayton ) :
4312daf946SGreg Clayton     m_thread (thread),
442cad65a5SGreg Clayton     m_prev_frames_sp (prev_frames_sp),
4530fdc8d8SChris Lattner     m_mutex (Mutex::eMutexTypeRecursive),
465082c5fdSGreg Clayton     m_frames (),
4771c21d18SStephen Wilson     m_selected_frame_idx (0),
48b0c72a5fSJim Ingham     m_concrete_frames_fetched (0),
49*513c6bb8SJim Ingham     m_current_inlined_depth (UINT32_MAX),
50*513c6bb8SJim Ingham     m_current_inlined_pc (LLDB_INVALID_ADDRESS),
5171c21d18SStephen Wilson     m_show_inlined_frames (show_inline_frames)
5230fdc8d8SChris Lattner {
53*513c6bb8SJim Ingham     if (prev_frames_sp)
54*513c6bb8SJim Ingham     {
55*513c6bb8SJim Ingham         m_current_inlined_depth = prev_frames_sp->m_current_inlined_depth;
56*513c6bb8SJim Ingham         m_current_inlined_pc =    prev_frames_sp->m_current_inlined_pc;
57*513c6bb8SJim Ingham     }
5830fdc8d8SChris Lattner }
5930fdc8d8SChris Lattner 
6030fdc8d8SChris Lattner //----------------------------------------------------------------------
6130fdc8d8SChris Lattner // Destructor
6230fdc8d8SChris Lattner //----------------------------------------------------------------------
6330fdc8d8SChris Lattner StackFrameList::~StackFrameList()
6430fdc8d8SChris Lattner {
6530fdc8d8SChris Lattner }
6630fdc8d8SChris Lattner 
67b0c72a5fSJim Ingham void
68*513c6bb8SJim Ingham StackFrameList::CalculateCurrentInlinedDepth()
69*513c6bb8SJim Ingham {
70*513c6bb8SJim Ingham     uint32_t cur_inlined_depth = GetCurrentInlinedDepth();
71*513c6bb8SJim Ingham     if (cur_inlined_depth == UINT32_MAX)
72*513c6bb8SJim Ingham     {
73*513c6bb8SJim Ingham         ResetCurrentInlinedDepth();
74*513c6bb8SJim Ingham     }
75*513c6bb8SJim Ingham }
76*513c6bb8SJim Ingham 
77*513c6bb8SJim Ingham uint32_t
78*513c6bb8SJim Ingham StackFrameList::GetCurrentInlinedDepth ()
79*513c6bb8SJim Ingham {
80*513c6bb8SJim Ingham     if (m_show_inlined_frames)
81*513c6bb8SJim Ingham     {
82*513c6bb8SJim Ingham         lldb::addr_t cur_pc = m_thread.GetRegisterContext()->GetPC();
83*513c6bb8SJim Ingham         if (cur_pc != m_current_inlined_pc)
84*513c6bb8SJim Ingham         {
85*513c6bb8SJim Ingham             m_current_inlined_pc = LLDB_INVALID_ADDRESS;
86*513c6bb8SJim Ingham             m_current_inlined_depth = UINT32_MAX;
87*513c6bb8SJim Ingham         }
88*513c6bb8SJim Ingham         return m_current_inlined_depth;
89*513c6bb8SJim Ingham     }
90*513c6bb8SJim Ingham     else
91*513c6bb8SJim Ingham     {
92*513c6bb8SJim Ingham         return UINT32_MAX;
93*513c6bb8SJim Ingham     }
94*513c6bb8SJim Ingham }
95*513c6bb8SJim Ingham 
96*513c6bb8SJim Ingham static const bool LLDB_FANCY_INLINED_STEPPING = false;
97*513c6bb8SJim Ingham 
98*513c6bb8SJim Ingham void
99*513c6bb8SJim Ingham StackFrameList::ResetCurrentInlinedDepth ()
100*513c6bb8SJim Ingham {
101*513c6bb8SJim Ingham     if (LLDB_FANCY_INLINED_STEPPING && m_show_inlined_frames)
102*513c6bb8SJim Ingham     {
103*513c6bb8SJim Ingham         GetFramesUpTo(0);
104*513c6bb8SJim Ingham         if (!m_frames[0]->IsInlined())
105*513c6bb8SJim Ingham         {
106*513c6bb8SJim Ingham             m_current_inlined_depth = UINT32_MAX;
107*513c6bb8SJim Ingham             m_current_inlined_pc = LLDB_INVALID_ADDRESS;
108*513c6bb8SJim Ingham         }
109*513c6bb8SJim Ingham         else
110*513c6bb8SJim Ingham         {
111*513c6bb8SJim Ingham             // We only need to do something special about inlined blocks when we
112*513c6bb8SJim Ingham             // are at the beginning of an inlined function:
113*513c6bb8SJim Ingham             // FIXME: We probably also have to do something special if the PC is at the END
114*513c6bb8SJim Ingham             // of an inlined function, which coincides with the end of either its containing
115*513c6bb8SJim Ingham             // function or another inlined function.
116*513c6bb8SJim Ingham 
117*513c6bb8SJim Ingham             lldb::addr_t curr_pc = m_thread.GetRegisterContext()->GetPC();
118*513c6bb8SJim Ingham             Block *block_ptr = m_frames[0]->GetFrameBlock();
119*513c6bb8SJim Ingham             if (block_ptr)
120*513c6bb8SJim Ingham             {
121*513c6bb8SJim Ingham                 Address pc_as_address;
122*513c6bb8SJim Ingham                 pc_as_address.SetLoadAddress(curr_pc, &(m_thread.GetProcess()->GetTarget()));
123*513c6bb8SJim Ingham                 AddressRange containing_range;
124*513c6bb8SJim Ingham                 if (block_ptr->GetRangeContainingAddress(pc_as_address, containing_range))
125*513c6bb8SJim Ingham                 {
126*513c6bb8SJim Ingham                     if (pc_as_address == containing_range.GetBaseAddress())
127*513c6bb8SJim Ingham                     {
128*513c6bb8SJim Ingham                         // If we got here because of a breakpoint hit, then set the inlined depth depending on where
129*513c6bb8SJim Ingham                         // the breakpoint was set.
130*513c6bb8SJim Ingham                         // If we got here because of a crash, then set the inlined depth to the deepest most block.
131*513c6bb8SJim Ingham                         // Otherwise, we stopped here naturally as the result of a step, so set ourselves in the
132*513c6bb8SJim Ingham                         // containing frame of the whole set of nested inlines, so the user can then "virtually"
133*513c6bb8SJim Ingham                         // step into the frames one by one, or next over the whole mess.
134*513c6bb8SJim Ingham                         // Note: We don't have to handle being somewhere in the middle of the stack here, since
135*513c6bb8SJim Ingham                         // ResetCurrentInlinedDepth doesn't get called if there is a valid inlined depth set.
136*513c6bb8SJim Ingham                         StopInfoSP stop_info_sp = m_thread.GetStopInfo();
137*513c6bb8SJim Ingham                         if (stop_info_sp)
138*513c6bb8SJim Ingham                         {
139*513c6bb8SJim Ingham                             switch (stop_info_sp->GetStopReason())
140*513c6bb8SJim Ingham                             {
141*513c6bb8SJim Ingham                             case eStopReasonBreakpoint:
142*513c6bb8SJim Ingham                                 {
143*513c6bb8SJim Ingham 
144*513c6bb8SJim Ingham                                 }
145*513c6bb8SJim Ingham                                 break;
146*513c6bb8SJim Ingham                             case eStopReasonWatchpoint:
147*513c6bb8SJim Ingham                             case eStopReasonException:
148*513c6bb8SJim Ingham                             case eStopReasonSignal:
149*513c6bb8SJim Ingham                                 // In all these cases we want to stop in the deepest most frame.
150*513c6bb8SJim Ingham                                 m_current_inlined_pc = curr_pc;
151*513c6bb8SJim Ingham                                 m_current_inlined_depth = 0;
152*513c6bb8SJim Ingham                                 break;
153*513c6bb8SJim Ingham                             default:
154*513c6bb8SJim Ingham                                 {
155*513c6bb8SJim Ingham                                     // Otherwise, we should set ourselves at the container of the inlining, so that the
156*513c6bb8SJim Ingham                                     // user can descend into them.
157*513c6bb8SJim Ingham                                     // So first we check whether we have more than one inlined block sharing this PC:
158*513c6bb8SJim Ingham                                     int num_inlined_functions = 0;
159*513c6bb8SJim Ingham 
160*513c6bb8SJim Ingham                                     for  (Block *container_ptr = block_ptr->GetInlinedParent();
161*513c6bb8SJim Ingham                                               container_ptr != NULL;
162*513c6bb8SJim Ingham                                               container_ptr = container_ptr->GetInlinedParent())
163*513c6bb8SJim Ingham                                     {
164*513c6bb8SJim Ingham                                         if (!container_ptr->GetRangeContainingAddress(pc_as_address, containing_range))
165*513c6bb8SJim Ingham                                             break;
166*513c6bb8SJim Ingham                                         if (pc_as_address != containing_range.GetBaseAddress())
167*513c6bb8SJim Ingham                                             break;
168*513c6bb8SJim Ingham 
169*513c6bb8SJim Ingham                                         num_inlined_functions++;
170*513c6bb8SJim Ingham                                     }
171*513c6bb8SJim Ingham                                     m_current_inlined_pc = curr_pc;
172*513c6bb8SJim Ingham                                     m_current_inlined_depth = num_inlined_functions + 1;
173*513c6bb8SJim Ingham 
174*513c6bb8SJim Ingham                                 }
175*513c6bb8SJim Ingham                                 break;
176*513c6bb8SJim Ingham                             }
177*513c6bb8SJim Ingham                         }
178*513c6bb8SJim Ingham                     }
179*513c6bb8SJim Ingham                 }
180*513c6bb8SJim Ingham             }
181*513c6bb8SJim Ingham         }
182*513c6bb8SJim Ingham     }
183*513c6bb8SJim Ingham }
184*513c6bb8SJim Ingham 
185*513c6bb8SJim Ingham bool
186*513c6bb8SJim Ingham StackFrameList::DecrementCurrentInlinedDepth ()
187*513c6bb8SJim Ingham {
188*513c6bb8SJim Ingham     if (m_show_inlined_frames)
189*513c6bb8SJim Ingham     {
190*513c6bb8SJim Ingham         uint32_t current_inlined_depth = GetCurrentInlinedDepth();
191*513c6bb8SJim Ingham         if (current_inlined_depth != UINT32_MAX)
192*513c6bb8SJim Ingham         {
193*513c6bb8SJim Ingham             if (current_inlined_depth > 0)
194*513c6bb8SJim Ingham                 m_current_inlined_depth--;
195*513c6bb8SJim Ingham             return true;
196*513c6bb8SJim Ingham         }
197*513c6bb8SJim Ingham     }
198*513c6bb8SJim Ingham     return false;
199*513c6bb8SJim Ingham }
200*513c6bb8SJim Ingham 
201*513c6bb8SJim Ingham void
202b0c72a5fSJim Ingham StackFrameList::GetFramesUpTo(uint32_t end_idx)
20330fdc8d8SChris Lattner {
204b0c72a5fSJim Ingham     // We've already gotten more frames than asked for, or we've already finished unwinding, return.
205b0c72a5fSJim Ingham     if (m_frames.size() > end_idx || GetAllFramesFetched())
206b0c72a5fSJim Ingham         return;
20712daf946SGreg Clayton 
208b0c72a5fSJim Ingham     Unwind *unwinder = m_thread.GetUnwinder ();
209b0c72a5fSJim Ingham 
21012daf946SGreg Clayton     if (m_show_inlined_frames)
21112daf946SGreg Clayton     {
2125082c5fdSGreg Clayton #if defined (DEBUG_STACK_FRAMES)
2132c595273SJohnny Chen         StreamFile s(stdout, false);
2145082c5fdSGreg Clayton #endif
215*513c6bb8SJim Ingham         // If we are hiding some frames from the outside world, we need to add those onto the total count of
216*513c6bb8SJim Ingham         // frames to fetch.  However, we don't need ot do that if end_idx is 0 since in that case we always
217*513c6bb8SJim Ingham         // get the first concrete frame and all the inlined frames below it...
218*513c6bb8SJim Ingham 
219*513c6bb8SJim Ingham         uint32_t inlined_depth = 0;
220*513c6bb8SJim Ingham         if (end_idx > 0)
221*513c6bb8SJim Ingham         {
222*513c6bb8SJim Ingham             inlined_depth = GetCurrentInlinedDepth();
223*513c6bb8SJim Ingham             if (inlined_depth != UINT32_MAX)
224*513c6bb8SJim Ingham             {
225*513c6bb8SJim Ingham                 if (end_idx > 0)
226*513c6bb8SJim Ingham                     end_idx += inlined_depth;
227*513c6bb8SJim Ingham             }
228*513c6bb8SJim Ingham             else
229*513c6bb8SJim Ingham                 inlined_depth = 0;
230*513c6bb8SJim Ingham         }
23112daf946SGreg Clayton 
2325082c5fdSGreg Clayton         StackFrameSP unwind_frame_sp;
233b0c72a5fSJim Ingham         do
23412daf946SGreg Clayton         {
235b0c72a5fSJim Ingham             uint32_t idx = m_concrete_frames_fetched++;
236b0c72a5fSJim Ingham             lldb::addr_t pc;
237b0c72a5fSJim Ingham             lldb::addr_t cfa;
23812daf946SGreg Clayton             if (idx == 0)
23912daf946SGreg Clayton             {
2405082c5fdSGreg Clayton                 // We might have already created frame zero, only create it
2415082c5fdSGreg Clayton                 // if we need to
2425082c5fdSGreg Clayton                 if (m_frames.empty())
2435082c5fdSGreg Clayton                 {
24412daf946SGreg Clayton                     m_thread.GetRegisterContext();
2451692b901SJim Ingham                     assert (m_thread.m_reg_context_sp.get());
246376c4854SJim Ingham 
247376c4854SJim Ingham                     const bool success = unwinder->GetFrameInfoAtIndex(idx, cfa, pc);
248376c4854SJim Ingham                     // There shouldn't be any way not to get the frame info for frame 0.
249376c4854SJim Ingham                     // But if the unwinder can't make one, lets make one by hand with the
250376c4854SJim Ingham                     // SP as the CFA and see if that gets any further.
251376c4854SJim Ingham                     if (!success)
252376c4854SJim Ingham                     {
253376c4854SJim Ingham                         cfa = m_thread.GetRegisterContext()->GetSP();
254376c4854SJim Ingham                         pc = m_thread.GetRegisterContext()->GetPC();
255376c4854SJim Ingham                     }
256376c4854SJim Ingham 
257d9e416c0SGreg Clayton                     unwind_frame_sp.reset (new StackFrame (m_thread.shared_from_this(),
258d9e416c0SGreg Clayton                                                            m_frames.size(),
2595082c5fdSGreg Clayton                                                            idx,
26012daf946SGreg Clayton                                                            m_thread.m_reg_context_sp,
26159e8fc1cSGreg Clayton                                                            cfa,
2621692b901SJim Ingham                                                            pc,
26312daf946SGreg Clayton                                                            NULL));
2645082c5fdSGreg Clayton                     m_frames.push_back (unwind_frame_sp);
2655082c5fdSGreg Clayton                 }
2665082c5fdSGreg Clayton                 else
2675082c5fdSGreg Clayton                 {
2685082c5fdSGreg Clayton                     unwind_frame_sp = m_frames.front();
26959e8fc1cSGreg Clayton                     cfa = unwind_frame_sp->m_id.GetCallFrameAddress();
2705082c5fdSGreg Clayton                 }
27112daf946SGreg Clayton             }
27212daf946SGreg Clayton             else
27312daf946SGreg Clayton             {
27412daf946SGreg Clayton                 const bool success = unwinder->GetFrameInfoAtIndex(idx, cfa, pc);
275b0c72a5fSJim Ingham                 if (!success)
276b0c72a5fSJim Ingham                 {
277b0c72a5fSJim Ingham                     // We've gotten to the end of the stack.
278b0c72a5fSJim Ingham                     SetAllFramesFetched();
279b0c72a5fSJim Ingham                     break;
280b0c72a5fSJim Ingham                 }
281d9e416c0SGreg Clayton                 unwind_frame_sp.reset (new StackFrame (m_thread.shared_from_this(), m_frames.size(), idx, cfa, pc, NULL));
2825082c5fdSGreg Clayton                 m_frames.push_back (unwind_frame_sp);
28312daf946SGreg Clayton             }
28412daf946SGreg Clayton 
2851ed54f50SGreg Clayton             SymbolContext unwind_sc = unwind_frame_sp->GetSymbolContext (eSymbolContextBlock | eSymbolContextFunction);
2861ed54f50SGreg Clayton             Block *unwind_block = unwind_sc.block;
28759e8fc1cSGreg Clayton             if (unwind_block)
28812daf946SGreg Clayton             {
2895f4c61e2SGreg Clayton                 Address curr_frame_address (unwind_frame_sp->GetFrameCodeAddress());
2905f4c61e2SGreg Clayton                 // Be sure to adjust the frame address to match the address
2915f4c61e2SGreg Clayton                 // that was used to lookup the symbol context above. If we are
2925f4c61e2SGreg Clayton                 // in the first concrete frame, then we lookup using the current
2935f4c61e2SGreg Clayton                 // address, else we decrement the address by one to get the correct
2945f4c61e2SGreg Clayton                 // location.
2955f4c61e2SGreg Clayton                 if (idx > 0)
2965f4c61e2SGreg Clayton                     curr_frame_address.Slide(-1);
2975f4c61e2SGreg Clayton 
2981ed54f50SGreg Clayton                 SymbolContext next_frame_sc;
2991ed54f50SGreg Clayton                 Address next_frame_address;
3001ed54f50SGreg Clayton 
3011ed54f50SGreg Clayton                 while (unwind_sc.GetParentOfInlinedScope(curr_frame_address, next_frame_sc, next_frame_address))
30259e8fc1cSGreg Clayton                 {
303d9e416c0SGreg Clayton                         StackFrameSP frame_sp(new StackFrame (m_thread.shared_from_this(),
304d9e416c0SGreg Clayton                                                               m_frames.size(),
3055082c5fdSGreg Clayton                                                               idx,
3065082c5fdSGreg Clayton                                                               unwind_frame_sp->GetRegisterContextSP (),
30759e8fc1cSGreg Clayton                                                               cfa,
3081ed54f50SGreg Clayton                                                               next_frame_address,
3091ed54f50SGreg Clayton                                                               &next_frame_sc));
3105082c5fdSGreg Clayton 
3115082c5fdSGreg Clayton                         m_frames.push_back (frame_sp);
3121ed54f50SGreg Clayton                         unwind_sc = next_frame_sc;
3131ed54f50SGreg Clayton                         curr_frame_address = next_frame_address;
314b0c72a5fSJim Ingham                 }
315b0c72a5fSJim Ingham             }
316b0c72a5fSJim Ingham         } while (m_frames.size() - 1 < end_idx);
3171ed54f50SGreg Clayton 
318b0c72a5fSJim Ingham         // Don't try to merge till you've calculated all the frames in this stack.
319b0c72a5fSJim Ingham         if (GetAllFramesFetched() && m_prev_frames_sp)
32012daf946SGreg Clayton         {
3212cad65a5SGreg Clayton             StackFrameList *prev_frames = m_prev_frames_sp.get();
3225082c5fdSGreg Clayton             StackFrameList *curr_frames = this;
3235082c5fdSGreg Clayton 
3245082c5fdSGreg Clayton #if defined (DEBUG_STACK_FRAMES)
32568275d5eSGreg Clayton             s.PutCString("\nprev_frames:\n");
3265082c5fdSGreg Clayton             prev_frames->Dump (&s);
32768275d5eSGreg Clayton             s.PutCString("\ncurr_frames:\n");
3285082c5fdSGreg Clayton             curr_frames->Dump (&s);
3295082c5fdSGreg Clayton             s.EOL();
3305082c5fdSGreg Clayton #endif
3315082c5fdSGreg Clayton             size_t curr_frame_num, prev_frame_num;
3325082c5fdSGreg Clayton 
3335082c5fdSGreg Clayton             for (curr_frame_num = curr_frames->m_frames.size(), prev_frame_num = prev_frames->m_frames.size();
3345082c5fdSGreg Clayton                  curr_frame_num > 0 && prev_frame_num > 0;
3355082c5fdSGreg Clayton                  --curr_frame_num, --prev_frame_num)
3365082c5fdSGreg Clayton             {
3375082c5fdSGreg Clayton                 const size_t curr_frame_idx = curr_frame_num-1;
3385082c5fdSGreg Clayton                 const size_t prev_frame_idx = prev_frame_num-1;
3395082c5fdSGreg Clayton                 StackFrameSP curr_frame_sp (curr_frames->m_frames[curr_frame_idx]);
3405082c5fdSGreg Clayton                 StackFrameSP prev_frame_sp (prev_frames->m_frames[prev_frame_idx]);
3415082c5fdSGreg Clayton 
3425082c5fdSGreg Clayton #if defined (DEBUG_STACK_FRAMES)
3432cad65a5SGreg Clayton                 s.Printf("\n\nCurr frame #%u ", curr_frame_idx);
3445082c5fdSGreg Clayton                 if (curr_frame_sp)
3452cad65a5SGreg Clayton                     curr_frame_sp->Dump (&s, true, false);
3465082c5fdSGreg Clayton                 else
3475082c5fdSGreg Clayton                     s.PutCString("NULL");
3482cad65a5SGreg Clayton                 s.Printf("\nPrev frame #%u ", prev_frame_idx);
3495082c5fdSGreg Clayton                 if (prev_frame_sp)
3502cad65a5SGreg Clayton                     prev_frame_sp->Dump (&s, true, false);
3515082c5fdSGreg Clayton                 else
3525082c5fdSGreg Clayton                     s.PutCString("NULL");
3535082c5fdSGreg Clayton #endif
3545082c5fdSGreg Clayton 
3555082c5fdSGreg Clayton                 StackFrame *curr_frame = curr_frame_sp.get();
3565082c5fdSGreg Clayton                 StackFrame *prev_frame = prev_frame_sp.get();
3575082c5fdSGreg Clayton 
3585082c5fdSGreg Clayton                 if (curr_frame == NULL || prev_frame == NULL)
3595082c5fdSGreg Clayton                     break;
3605082c5fdSGreg Clayton 
36159e8fc1cSGreg Clayton                 // Check the stack ID to make sure they are equal
36259e8fc1cSGreg Clayton                 if (curr_frame->GetStackID() != prev_frame->GetStackID())
3635082c5fdSGreg Clayton                     break;
3645082c5fdSGreg Clayton 
36559e8fc1cSGreg Clayton                 prev_frame->UpdatePreviousFrameFromCurrentFrame (*curr_frame);
36659e8fc1cSGreg Clayton                 // Now copy the fixed up previous frame into the current frames
36759e8fc1cSGreg Clayton                 // so the pointer doesn't change
36859e8fc1cSGreg Clayton                 m_frames[curr_frame_idx] = prev_frame_sp;
36959e8fc1cSGreg Clayton                 //curr_frame->UpdateCurrentFrameFromPreviousFrame (*prev_frame);
3705082c5fdSGreg Clayton 
3715082c5fdSGreg Clayton #if defined (DEBUG_STACK_FRAMES)
37268275d5eSGreg Clayton                 s.Printf("\n    Copying previous frame to current frame");
3735082c5fdSGreg Clayton #endif
3745082c5fdSGreg Clayton             }
3755082c5fdSGreg Clayton             // We are done with the old stack frame list, we can release it now
3762cad65a5SGreg Clayton             m_prev_frames_sp.reset();
3775082c5fdSGreg Clayton         }
37868275d5eSGreg Clayton 
37968275d5eSGreg Clayton #if defined (DEBUG_STACK_FRAMES)
38068275d5eSGreg Clayton             s.PutCString("\n\nNew frames:\n");
38168275d5eSGreg Clayton             Dump (&s);
38268275d5eSGreg Clayton             s.EOL();
38368275d5eSGreg Clayton #endif
38412daf946SGreg Clayton     }
38512daf946SGreg Clayton     else
38612daf946SGreg Clayton     {
387b0c72a5fSJim Ingham         if (end_idx < m_concrete_frames_fetched)
388b0c72a5fSJim Ingham             return;
389b0c72a5fSJim Ingham 
390b0c72a5fSJim Ingham         uint32_t num_frames = unwinder->GetFramesUpTo(end_idx);
391b0c72a5fSJim Ingham         if (num_frames <= end_idx + 1)
392b0c72a5fSJim Ingham         {
393b0c72a5fSJim Ingham             //Done unwinding.
394b0c72a5fSJim Ingham             m_concrete_frames_fetched = UINT32_MAX;
395b0c72a5fSJim Ingham         }
396b0c72a5fSJim Ingham         m_frames.resize(num_frames);
39712daf946SGreg Clayton     }
3985082c5fdSGreg Clayton }
399b0c72a5fSJim Ingham 
400b0c72a5fSJim Ingham uint32_t
401b0c72a5fSJim Ingham StackFrameList::GetNumFrames (bool can_create)
402b0c72a5fSJim Ingham {
403b0c72a5fSJim Ingham     Mutex::Locker locker (m_mutex);
404b0c72a5fSJim Ingham 
405b0c72a5fSJim Ingham     if (can_create)
406b0c72a5fSJim Ingham         GetFramesUpTo (UINT32_MAX);
407*513c6bb8SJim Ingham 
408*513c6bb8SJim Ingham     uint32_t inlined_depth = GetCurrentInlinedDepth();
409*513c6bb8SJim Ingham     if (inlined_depth == UINT32_MAX)
4105082c5fdSGreg Clayton         return m_frames.size();
411*513c6bb8SJim Ingham     else
412*513c6bb8SJim Ingham         return m_frames.size() - inlined_depth;
41330fdc8d8SChris Lattner }
41430fdc8d8SChris Lattner 
4155082c5fdSGreg Clayton void
4165082c5fdSGreg Clayton StackFrameList::Dump (Stream *s)
41730fdc8d8SChris Lattner {
4185082c5fdSGreg Clayton     if (s == NULL)
4195082c5fdSGreg Clayton         return;
4205082c5fdSGreg Clayton     Mutex::Locker locker (m_mutex);
42130fdc8d8SChris Lattner 
4225082c5fdSGreg Clayton     const_iterator pos, begin = m_frames.begin(), end = m_frames.end();
4235082c5fdSGreg Clayton     for (pos = begin; pos != end; ++pos)
42412daf946SGreg Clayton     {
4255082c5fdSGreg Clayton         StackFrame *frame = (*pos).get();
4265082c5fdSGreg Clayton         s->Printf("%p: ", frame);
4275082c5fdSGreg Clayton         if (frame)
42859e8fc1cSGreg Clayton         {
42959e8fc1cSGreg Clayton             frame->GetStackID().Dump (s);
4300603aa9dSGreg Clayton             frame->DumpUsingSettingsFormat (s);
43159e8fc1cSGreg Clayton         }
4325082c5fdSGreg Clayton         else
433dce502edSGreg Clayton             s->Printf("frame #%u", (uint32_t)std::distance (begin, pos));
4345082c5fdSGreg Clayton         s->EOL();
43512daf946SGreg Clayton     }
4365082c5fdSGreg Clayton     s->EOL();
4375082c5fdSGreg Clayton }
43812daf946SGreg Clayton 
43930fdc8d8SChris Lattner StackFrameSP
44012daf946SGreg Clayton StackFrameList::GetFrameAtIndex (uint32_t idx)
44130fdc8d8SChris Lattner {
44230fdc8d8SChris Lattner     StackFrameSP frame_sp;
44330fdc8d8SChris Lattner     Mutex::Locker locker (m_mutex);
444*513c6bb8SJim Ingham     uint32_t inlined_depth = GetCurrentInlinedDepth();
445*513c6bb8SJim Ingham     if (inlined_depth != UINT32_MAX)
446*513c6bb8SJim Ingham         idx += inlined_depth;
447*513c6bb8SJim Ingham 
4485082c5fdSGreg Clayton     if (idx < m_frames.size())
4495082c5fdSGreg Clayton         frame_sp = m_frames[idx];
45012daf946SGreg Clayton 
4515082c5fdSGreg Clayton     if (frame_sp)
45212daf946SGreg Clayton         return frame_sp;
45312daf946SGreg Clayton 
4541692b901SJim Ingham         // GetFramesUpTo will fill m_frames with as many frames as you asked for,
4551692b901SJim Ingham         // if there are that many.  If there weren't then you asked for too many
4561692b901SJim Ingham         // frames.
457b0c72a5fSJim Ingham         GetFramesUpTo (idx);
458b0c72a5fSJim Ingham         if (idx < m_frames.size())
45912daf946SGreg Clayton         {
46012daf946SGreg Clayton             if (m_show_inlined_frames)
46112daf946SGreg Clayton             {
4621692b901SJim Ingham                 // When inline frames are enabled we actually create all the frames in GetFramesUpTo.
4635082c5fdSGreg Clayton                 frame_sp = m_frames[idx];
46412daf946SGreg Clayton             }
46512daf946SGreg Clayton             else
46612daf946SGreg Clayton             {
46712daf946SGreg Clayton                 Unwind *unwinder = m_thread.GetUnwinder ();
46812daf946SGreg Clayton                 if (unwinder)
46912daf946SGreg Clayton                 {
47012daf946SGreg Clayton                     addr_t pc, cfa;
47112daf946SGreg Clayton                     if (unwinder->GetFrameInfoAtIndex(idx, cfa, pc))
4725082c5fdSGreg Clayton                     {
473d9e416c0SGreg Clayton                         frame_sp.reset (new StackFrame (m_thread.shared_from_this(), idx, idx, cfa, pc, NULL));
47459e8fc1cSGreg Clayton 
47559e8fc1cSGreg Clayton                         Function *function = frame_sp->GetSymbolContext (eSymbolContextFunction).function;
47659e8fc1cSGreg Clayton                         if (function)
47759e8fc1cSGreg Clayton                         {
47859e8fc1cSGreg Clayton                             // When we aren't showing inline functions we always use
47959e8fc1cSGreg Clayton                             // the top most function block as the scope.
48059e8fc1cSGreg Clayton                             frame_sp->SetSymbolContextScope (&function->GetBlock(false));
48159e8fc1cSGreg Clayton                         }
48259e8fc1cSGreg Clayton                         else
48359e8fc1cSGreg Clayton                         {
48459e8fc1cSGreg Clayton                             // Set the symbol scope from the symbol regardless if it is NULL or valid.
48559e8fc1cSGreg Clayton                             frame_sp->SetSymbolContextScope (frame_sp->GetSymbolContext (eSymbolContextSymbol).symbol);
48659e8fc1cSGreg Clayton                         }
4875082c5fdSGreg Clayton                         SetFrameAtIndex(idx, frame_sp);
48812daf946SGreg Clayton                     }
48912daf946SGreg Clayton                 }
49012daf946SGreg Clayton             }
49130fdc8d8SChris Lattner         }
49230fdc8d8SChris Lattner     return frame_sp;
49330fdc8d8SChris Lattner }
49430fdc8d8SChris Lattner 
4955ccbd294SGreg Clayton StackFrameSP
4965ccbd294SGreg Clayton StackFrameList::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx)
4975ccbd294SGreg Clayton {
4985ccbd294SGreg Clayton     // First try assuming the unwind index is the same as the frame index. The
4995ccbd294SGreg Clayton     // unwind index is always greater than or equal to the frame index, so it
5005ccbd294SGreg Clayton     // is a good place to start. If we have inlined frames we might have 5
5015ccbd294SGreg Clayton     // concrete frames (frame unwind indexes go from 0-4), but we might have 15
5025ccbd294SGreg Clayton     // frames after we make all the inlined frames. Most of the time the unwind
5035ccbd294SGreg Clayton     // frame index (or the concrete frame index) is the same as the frame index.
5045ccbd294SGreg Clayton     uint32_t frame_idx = unwind_idx;
5055ccbd294SGreg Clayton     StackFrameSP frame_sp (GetFrameAtIndex (frame_idx));
5065ccbd294SGreg Clayton     while (frame_sp)
5075ccbd294SGreg Clayton     {
5085ccbd294SGreg Clayton         if (frame_sp->GetFrameIndex() == unwind_idx)
5095ccbd294SGreg Clayton             break;
5105ccbd294SGreg Clayton         frame_sp = GetFrameAtIndex (++frame_idx);
5115ccbd294SGreg Clayton     }
5125ccbd294SGreg Clayton     return frame_sp;
5135ccbd294SGreg Clayton }
5145ccbd294SGreg Clayton 
5153a195b7eSJim Ingham StackFrameSP
516cc4d0146SGreg Clayton StackFrameList::GetFrameWithStackID (const StackID &stack_id)
5173a195b7eSJim Ingham {
5183a195b7eSJim Ingham     uint32_t frame_idx = 0;
5193a195b7eSJim Ingham     StackFrameSP frame_sp;
5203a195b7eSJim Ingham     do
5213a195b7eSJim Ingham     {
5223a195b7eSJim Ingham         frame_sp = GetFrameAtIndex (frame_idx);
5233a195b7eSJim Ingham         if (frame_sp && frame_sp->GetStackID() == stack_id)
5243a195b7eSJim Ingham             break;
5253a195b7eSJim Ingham         frame_idx++;
5263a195b7eSJim Ingham     }
5273a195b7eSJim Ingham     while (frame_sp);
5283a195b7eSJim Ingham     return frame_sp;
5293a195b7eSJim Ingham }
5305ccbd294SGreg Clayton 
53112daf946SGreg Clayton bool
5325082c5fdSGreg Clayton StackFrameList::SetFrameAtIndex (uint32_t idx, StackFrameSP &frame_sp)
53312daf946SGreg Clayton {
5345082c5fdSGreg Clayton     if (idx >= m_frames.size())
5355082c5fdSGreg Clayton         m_frames.resize(idx + 1);
53612daf946SGreg Clayton     // Make sure allocation succeeded by checking bounds again
5375082c5fdSGreg Clayton     if (idx < m_frames.size())
53812daf946SGreg Clayton     {
5395082c5fdSGreg Clayton         m_frames[idx] = frame_sp;
54030fdc8d8SChris Lattner         return true;
54130fdc8d8SChris Lattner     }
54230fdc8d8SChris Lattner     return false;   // resize failed, out of memory?
54330fdc8d8SChris Lattner }
54430fdc8d8SChris Lattner 
54530fdc8d8SChris Lattner uint32_t
5462976d00aSJim Ingham StackFrameList::GetSelectedFrameIndex () const
54730fdc8d8SChris Lattner {
54830fdc8d8SChris Lattner     Mutex::Locker locker (m_mutex);
5492976d00aSJim Ingham     return m_selected_frame_idx;
55030fdc8d8SChris Lattner }
55130fdc8d8SChris Lattner 
55230fdc8d8SChris Lattner 
55330fdc8d8SChris Lattner uint32_t
5542976d00aSJim Ingham StackFrameList::SetSelectedFrame (lldb_private::StackFrame *frame)
55530fdc8d8SChris Lattner {
55630fdc8d8SChris Lattner     Mutex::Locker locker (m_mutex);
55712daf946SGreg Clayton     const_iterator pos;
5585082c5fdSGreg Clayton     const_iterator begin = m_frames.begin();
5595082c5fdSGreg Clayton     const_iterator end = m_frames.end();
560b7f6b2faSJim Ingham     m_selected_frame_idx = 0;
56130fdc8d8SChris Lattner     for (pos = begin; pos != end; ++pos)
56230fdc8d8SChris Lattner     {
56330fdc8d8SChris Lattner         if (pos->get() == frame)
56430fdc8d8SChris Lattner         {
5652976d00aSJim Ingham             m_selected_frame_idx = std::distance (begin, pos);
566*513c6bb8SJim Ingham             uint32_t inlined_depth = GetCurrentInlinedDepth();
567*513c6bb8SJim Ingham             if (inlined_depth != UINT32_MAX)
568*513c6bb8SJim Ingham                 m_selected_frame_idx -= inlined_depth;
569b7f6b2faSJim Ingham             break;
57030fdc8d8SChris Lattner         }
57130fdc8d8SChris Lattner     }
572b7f6b2faSJim Ingham     SetDefaultFileAndLineToSelectedFrame();
5732976d00aSJim Ingham     return m_selected_frame_idx;
57430fdc8d8SChris Lattner }
57530fdc8d8SChris Lattner 
57630fdc8d8SChris Lattner // Mark a stack frame as the current frame using the frame index
577b0c72a5fSJim Ingham bool
5782976d00aSJim Ingham StackFrameList::SetSelectedFrameByIndex (uint32_t idx)
57930fdc8d8SChris Lattner {
58030fdc8d8SChris Lattner     Mutex::Locker locker (m_mutex);
581b0c72a5fSJim Ingham     StackFrameSP frame_sp (GetFrameAtIndex (idx));
582b0c72a5fSJim Ingham     if (frame_sp)
583b0c72a5fSJim Ingham     {
584b0c72a5fSJim Ingham         SetSelectedFrame(frame_sp.get());
585b0c72a5fSJim Ingham         return true;
586b0c72a5fSJim Ingham     }
587b0c72a5fSJim Ingham     else
588b0c72a5fSJim Ingham         return false;
589b7f6b2faSJim Ingham }
590b7f6b2faSJim Ingham 
591b7f6b2faSJim Ingham void
592b7f6b2faSJim Ingham StackFrameList::SetDefaultFileAndLineToSelectedFrame()
593b7f6b2faSJim Ingham {
5941ac04c30SGreg Clayton     if (m_thread.GetID() == m_thread.GetProcess()->GetThreadList().GetSelectedThread()->GetID())
595b7f6b2faSJim Ingham     {
596252d0edeSGreg Clayton         StackFrameSP frame_sp (GetFrameAtIndex (GetSelectedFrameIndex()));
597b7f6b2faSJim Ingham         if (frame_sp)
598b7f6b2faSJim Ingham         {
599252d0edeSGreg Clayton             SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextLineEntry);
600b7f6b2faSJim Ingham             if (sc.line_entry.file)
6011ac04c30SGreg Clayton                 m_thread.CalculateTarget()->GetSourceManager().SetDefaultFileAndLine (sc.line_entry.file,
602252d0edeSGreg Clayton                                                                                             sc.line_entry.line);
603b7f6b2faSJim Ingham         }
604b7f6b2faSJim Ingham     }
60530fdc8d8SChris Lattner }
60630fdc8d8SChris Lattner 
60730fdc8d8SChris Lattner // The thread has been run, reset the number stack frames to zero so we can
60830fdc8d8SChris Lattner // determine how many frames we have lazily.
60930fdc8d8SChris Lattner void
61030fdc8d8SChris Lattner StackFrameList::Clear ()
61130fdc8d8SChris Lattner {
61230fdc8d8SChris Lattner     Mutex::Locker locker (m_mutex);
6135082c5fdSGreg Clayton     m_frames.clear();
614b0c72a5fSJim Ingham     m_concrete_frames_fetched = 0;
61530fdc8d8SChris Lattner }
61630fdc8d8SChris Lattner 
61730fdc8d8SChris Lattner void
61830fdc8d8SChris Lattner StackFrameList::InvalidateFrames (uint32_t start_idx)
61930fdc8d8SChris Lattner {
62030fdc8d8SChris Lattner     Mutex::Locker locker (m_mutex);
62112daf946SGreg Clayton     if (m_show_inlined_frames)
62212daf946SGreg Clayton     {
62312daf946SGreg Clayton         Clear();
62412daf946SGreg Clayton     }
62512daf946SGreg Clayton     else
62612daf946SGreg Clayton     {
6275082c5fdSGreg Clayton         const size_t num_frames = m_frames.size();
62830fdc8d8SChris Lattner         while (start_idx < num_frames)
62930fdc8d8SChris Lattner         {
6305082c5fdSGreg Clayton             m_frames[start_idx].reset();
63130fdc8d8SChris Lattner             ++start_idx;
63230fdc8d8SChris Lattner         }
63330fdc8d8SChris Lattner     }
63412daf946SGreg Clayton }
6352cad65a5SGreg Clayton 
6362cad65a5SGreg Clayton void
6372cad65a5SGreg Clayton StackFrameList::Merge (std::auto_ptr<StackFrameList>& curr_ap, lldb::StackFrameListSP& prev_sp)
6382cad65a5SGreg Clayton {
63910ebffa4SJim Ingham     Mutex::Locker curr_locker (curr_ap.get() ? &curr_ap->m_mutex : NULL);
64010ebffa4SJim Ingham     Mutex::Locker prev_locker (prev_sp.get() ? &prev_sp->m_mutex : NULL);
6412cad65a5SGreg Clayton 
6422cad65a5SGreg Clayton #if defined (DEBUG_STACK_FRAMES)
6432c595273SJohnny Chen     StreamFile s(stdout, false);
6442cad65a5SGreg Clayton     s.PutCString("\n\nStackFrameList::Merge():\nPrev:\n");
6452cad65a5SGreg Clayton     if (prev_sp.get())
6462cad65a5SGreg Clayton         prev_sp->Dump (&s);
6472cad65a5SGreg Clayton     else
6482cad65a5SGreg Clayton         s.PutCString ("NULL");
6492cad65a5SGreg Clayton     s.PutCString("\nCurr:\n");
6502cad65a5SGreg Clayton     if (curr_ap.get())
6512cad65a5SGreg Clayton         curr_ap->Dump (&s);
6522cad65a5SGreg Clayton     else
6532cad65a5SGreg Clayton         s.PutCString ("NULL");
6542cad65a5SGreg Clayton     s.EOL();
6552cad65a5SGreg Clayton #endif
6562cad65a5SGreg Clayton 
6572cad65a5SGreg Clayton     if (curr_ap.get() == NULL || curr_ap->GetNumFrames (false) == 0)
6582cad65a5SGreg Clayton     {
6592cad65a5SGreg Clayton #if defined (DEBUG_STACK_FRAMES)
6602cad65a5SGreg Clayton         s.PutCString("No current frames, leave previous frames alone...\n");
6612cad65a5SGreg Clayton #endif
6622cad65a5SGreg Clayton         curr_ap.release();
6632cad65a5SGreg Clayton         return;
6642cad65a5SGreg Clayton     }
6652cad65a5SGreg Clayton 
6662cad65a5SGreg Clayton     if (prev_sp.get() == NULL || prev_sp->GetNumFrames (false) == 0)
6672cad65a5SGreg Clayton     {
6682cad65a5SGreg Clayton #if defined (DEBUG_STACK_FRAMES)
6692cad65a5SGreg Clayton         s.PutCString("No previous frames, so use current frames...\n");
6702cad65a5SGreg Clayton #endif
6712cad65a5SGreg Clayton         // We either don't have any previous frames, or since we have more than
6722cad65a5SGreg Clayton         // one current frames it means we have all the frames and can safely
6732cad65a5SGreg Clayton         // replace our previous frames.
6742cad65a5SGreg Clayton         prev_sp.reset (curr_ap.release());
6752cad65a5SGreg Clayton         return;
6762cad65a5SGreg Clayton     }
6772cad65a5SGreg Clayton 
6782cad65a5SGreg Clayton     const uint32_t num_curr_frames = curr_ap->GetNumFrames (false);
6792cad65a5SGreg Clayton 
6802cad65a5SGreg Clayton     if (num_curr_frames > 1)
6812cad65a5SGreg Clayton     {
6822cad65a5SGreg Clayton #if defined (DEBUG_STACK_FRAMES)
6832cad65a5SGreg Clayton         s.PutCString("We have more than one current frame, so use current frames...\n");
6842cad65a5SGreg Clayton #endif
6852cad65a5SGreg Clayton         // We have more than one current frames it means we have all the frames
6862cad65a5SGreg Clayton         // and can safely replace our previous frames.
6872cad65a5SGreg Clayton         prev_sp.reset (curr_ap.release());
6882cad65a5SGreg Clayton 
6892cad65a5SGreg Clayton #if defined (DEBUG_STACK_FRAMES)
6902cad65a5SGreg Clayton         s.PutCString("\nMerged:\n");
6912cad65a5SGreg Clayton         prev_sp->Dump (&s);
6922cad65a5SGreg Clayton #endif
6932cad65a5SGreg Clayton         return;
6942cad65a5SGreg Clayton     }
6952cad65a5SGreg Clayton 
6962cad65a5SGreg Clayton     StackFrameSP prev_frame_zero_sp(prev_sp->GetFrameAtIndex (0));
6972cad65a5SGreg Clayton     StackFrameSP curr_frame_zero_sp(curr_ap->GetFrameAtIndex (0));
6982cad65a5SGreg Clayton     StackID curr_stack_id (curr_frame_zero_sp->GetStackID());
6992cad65a5SGreg Clayton     StackID prev_stack_id (prev_frame_zero_sp->GetStackID());
7002cad65a5SGreg Clayton 
7012cad65a5SGreg Clayton #if defined (DEBUG_STACK_FRAMES)
7022c595273SJohnny Chen     const uint32_t num_prev_frames = prev_sp->GetNumFrames (false);
7032cad65a5SGreg Clayton     s.Printf("\n%u previous frames with one current frame\n", num_prev_frames);
7042cad65a5SGreg Clayton #endif
7052cad65a5SGreg Clayton 
7062cad65a5SGreg Clayton     // We have only a single current frame
7072cad65a5SGreg Clayton     // Our previous stack frames only had a single frame as well...
7082cad65a5SGreg Clayton     if (curr_stack_id == prev_stack_id)
7092cad65a5SGreg Clayton     {
7102cad65a5SGreg Clayton #if defined (DEBUG_STACK_FRAMES)
7112cad65a5SGreg Clayton         s.Printf("\nPrevious frame #0 is same as current frame #0, merge the cached data\n");
7122cad65a5SGreg Clayton #endif
7132cad65a5SGreg Clayton 
7142cad65a5SGreg Clayton         curr_frame_zero_sp->UpdateCurrentFrameFromPreviousFrame (*prev_frame_zero_sp);
7152cad65a5SGreg Clayton //        prev_frame_zero_sp->UpdatePreviousFrameFromCurrentFrame (*curr_frame_zero_sp);
7162cad65a5SGreg Clayton //        prev_sp->SetFrameAtIndex (0, prev_frame_zero_sp);
7172cad65a5SGreg Clayton     }
7182cad65a5SGreg Clayton     else if (curr_stack_id < prev_stack_id)
7192cad65a5SGreg Clayton     {
7202cad65a5SGreg Clayton #if defined (DEBUG_STACK_FRAMES)
7212cad65a5SGreg 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");
7222cad65a5SGreg Clayton #endif
7232cad65a5SGreg Clayton         prev_sp->m_frames.insert (prev_sp->m_frames.begin(), curr_frame_zero_sp);
7242cad65a5SGreg Clayton     }
7252cad65a5SGreg Clayton 
7262cad65a5SGreg Clayton     curr_ap.release();
7272cad65a5SGreg Clayton 
7282cad65a5SGreg Clayton #if defined (DEBUG_STACK_FRAMES)
7292cad65a5SGreg Clayton     s.PutCString("\nMerged:\n");
7302cad65a5SGreg Clayton     prev_sp->Dump (&s);
7312cad65a5SGreg Clayton #endif
7322cad65a5SGreg Clayton 
7332cad65a5SGreg Clayton 
7342cad65a5SGreg Clayton }
735e4284b71SJim Ingham 
736e4284b71SJim Ingham lldb::StackFrameSP
737e4284b71SJim Ingham StackFrameList::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr)
738e4284b71SJim Ingham {
739e4284b71SJim Ingham     const_iterator pos;
740e4284b71SJim Ingham     const_iterator begin = m_frames.begin();
741e4284b71SJim Ingham     const_iterator end = m_frames.end();
742e4284b71SJim Ingham     lldb::StackFrameSP ret_sp;
743e4284b71SJim Ingham 
744e4284b71SJim Ingham     for (pos = begin; pos != end; ++pos)
745e4284b71SJim Ingham     {
746e4284b71SJim Ingham         if (pos->get() == stack_frame_ptr)
747e4284b71SJim Ingham         {
748e4284b71SJim Ingham             ret_sp = (*pos);
749e4284b71SJim Ingham             break;
750e4284b71SJim Ingham         }
751e4284b71SJim Ingham     }
752e4284b71SJim Ingham     return ret_sp;
753e4284b71SJim Ingham }
754e4284b71SJim Ingham 
7557260f620SGreg Clayton size_t
7567260f620SGreg Clayton StackFrameList::GetStatus (Stream& strm,
7577260f620SGreg Clayton                            uint32_t first_frame,
7587260f620SGreg Clayton                            uint32_t num_frames,
7597260f620SGreg Clayton                            bool show_frame_info,
76053eb7ad2SGreg Clayton                            uint32_t num_frames_with_source)
7617260f620SGreg Clayton {
7627260f620SGreg Clayton     size_t num_frames_displayed = 0;
7637260f620SGreg Clayton 
7647260f620SGreg Clayton     if (num_frames == 0)
7657260f620SGreg Clayton         return 0;
7667260f620SGreg Clayton 
7677260f620SGreg Clayton     StackFrameSP frame_sp;
7687260f620SGreg Clayton     uint32_t frame_idx = 0;
7697260f620SGreg Clayton     uint32_t last_frame;
7707260f620SGreg Clayton 
7717260f620SGreg Clayton     // Don't let the last frame wrap around...
7727260f620SGreg Clayton     if (num_frames == UINT32_MAX)
7737260f620SGreg Clayton         last_frame = UINT32_MAX;
7747260f620SGreg Clayton     else
7757260f620SGreg Clayton         last_frame = first_frame + num_frames;
7767260f620SGreg Clayton 
7777260f620SGreg Clayton     for (frame_idx = first_frame; frame_idx < last_frame; ++frame_idx)
7787260f620SGreg Clayton     {
7797260f620SGreg Clayton         frame_sp = GetFrameAtIndex(frame_idx);
7807260f620SGreg Clayton         if (frame_sp.get() == NULL)
7817260f620SGreg Clayton             break;
7827260f620SGreg Clayton 
7837260f620SGreg Clayton         if (!frame_sp->GetStatus (strm,
7847260f620SGreg Clayton                                   show_frame_info,
78553eb7ad2SGreg Clayton                                   num_frames_with_source > (first_frame - frame_idx)))
7867260f620SGreg Clayton             break;
7877260f620SGreg Clayton         ++num_frames_displayed;
7887260f620SGreg Clayton     }
7897260f620SGreg Clayton 
7907260f620SGreg Clayton     strm.IndentLess();
7917260f620SGreg Clayton     return num_frames_displayed;
7927260f620SGreg Clayton }
7937260f620SGreg Clayton 
794