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 {
68ca5ce187SGreg Clayton     // Call clear since this takes a lock and clears the stack frame list
69ca5ce187SGreg Clayton     // in case another thread is currently using this stack frame list
70ca5ce187SGreg Clayton     Clear();
7130fdc8d8SChris Lattner }
7230fdc8d8SChris Lattner 
73b0c72a5fSJim Ingham void
74513c6bb8SJim Ingham StackFrameList::CalculateCurrentInlinedDepth()
75513c6bb8SJim Ingham {
76513c6bb8SJim Ingham     uint32_t cur_inlined_depth = GetCurrentInlinedDepth();
77513c6bb8SJim Ingham     if (cur_inlined_depth == UINT32_MAX)
78513c6bb8SJim Ingham     {
79513c6bb8SJim Ingham         ResetCurrentInlinedDepth();
80513c6bb8SJim Ingham     }
81513c6bb8SJim Ingham }
82513c6bb8SJim Ingham 
83513c6bb8SJim Ingham uint32_t
84513c6bb8SJim Ingham StackFrameList::GetCurrentInlinedDepth ()
85513c6bb8SJim Ingham {
866cd41da7SJim Ingham     if (m_show_inlined_frames && m_current_inlined_pc != LLDB_INVALID_ADDRESS)
87513c6bb8SJim Ingham     {
88513c6bb8SJim Ingham         lldb::addr_t cur_pc = m_thread.GetRegisterContext()->GetPC();
89513c6bb8SJim Ingham         if (cur_pc != m_current_inlined_pc)
90513c6bb8SJim Ingham         {
91513c6bb8SJim Ingham             m_current_inlined_pc = LLDB_INVALID_ADDRESS;
92513c6bb8SJim Ingham             m_current_inlined_depth = UINT32_MAX;
935160ce5cSGreg Clayton             Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
946cd41da7SJim Ingham             if (log && log->GetVerbose())
956cd41da7SJim Ingham                 log->Printf ("GetCurrentInlinedDepth: invalidating current inlined depth.\n");
96513c6bb8SJim Ingham         }
97513c6bb8SJim Ingham         return m_current_inlined_depth;
98513c6bb8SJim Ingham     }
99513c6bb8SJim Ingham     else
100513c6bb8SJim Ingham     {
101513c6bb8SJim Ingham         return UINT32_MAX;
102513c6bb8SJim Ingham     }
103513c6bb8SJim Ingham }
104513c6bb8SJim Ingham 
105513c6bb8SJim Ingham void
106513c6bb8SJim Ingham StackFrameList::ResetCurrentInlinedDepth ()
107513c6bb8SJim Ingham {
108e7e6ffc6SJim Ingham     if (m_show_inlined_frames)
109513c6bb8SJim Ingham     {
110513c6bb8SJim Ingham         GetFramesUpTo(0);
111*65d4d5c3SRyan Brown         if (m_frames.size() == 0)
112*65d4d5c3SRyan Brown             return;
113513c6bb8SJim Ingham         if (!m_frames[0]->IsInlined())
114513c6bb8SJim Ingham         {
115513c6bb8SJim Ingham             m_current_inlined_depth = UINT32_MAX;
116513c6bb8SJim Ingham             m_current_inlined_pc = LLDB_INVALID_ADDRESS;
1175160ce5cSGreg Clayton             Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
1186cd41da7SJim Ingham             if (log && log->GetVerbose())
1196cd41da7SJim Ingham                 log->Printf ("ResetCurrentInlinedDepth: Invalidating current inlined depth.\n");
120513c6bb8SJim Ingham         }
121513c6bb8SJim Ingham         else
122513c6bb8SJim Ingham         {
123513c6bb8SJim Ingham             // We only need to do something special about inlined blocks when we
124513c6bb8SJim Ingham             // are at the beginning of an inlined function:
125513c6bb8SJim Ingham             // FIXME: We probably also have to do something special if the PC is at the END
126513c6bb8SJim Ingham             // of an inlined function, which coincides with the end of either its containing
127513c6bb8SJim Ingham             // function or another inlined function.
128513c6bb8SJim Ingham 
129513c6bb8SJim Ingham             lldb::addr_t curr_pc = m_thread.GetRegisterContext()->GetPC();
130513c6bb8SJim Ingham             Block *block_ptr = m_frames[0]->GetFrameBlock();
131513c6bb8SJim Ingham             if (block_ptr)
132513c6bb8SJim Ingham             {
133513c6bb8SJim Ingham                 Address pc_as_address;
134513c6bb8SJim Ingham                 pc_as_address.SetLoadAddress(curr_pc, &(m_thread.GetProcess()->GetTarget()));
135513c6bb8SJim Ingham                 AddressRange containing_range;
136513c6bb8SJim Ingham                 if (block_ptr->GetRangeContainingAddress(pc_as_address, containing_range))
137513c6bb8SJim Ingham                 {
138513c6bb8SJim Ingham                     if (pc_as_address == containing_range.GetBaseAddress())
139513c6bb8SJim Ingham                     {
140513c6bb8SJim Ingham                         // If we got here because of a breakpoint hit, then set the inlined depth depending on where
141513c6bb8SJim Ingham                         // the breakpoint was set.
142513c6bb8SJim Ingham                         // If we got here because of a crash, then set the inlined depth to the deepest most block.
143513c6bb8SJim Ingham                         // Otherwise, we stopped here naturally as the result of a step, so set ourselves in the
144513c6bb8SJim Ingham                         // containing frame of the whole set of nested inlines, so the user can then "virtually"
145513c6bb8SJim Ingham                         // step into the frames one by one, or next over the whole mess.
146513c6bb8SJim Ingham                         // Note: We don't have to handle being somewhere in the middle of the stack here, since
147513c6bb8SJim Ingham                         // ResetCurrentInlinedDepth doesn't get called if there is a valid inlined depth set.
148513c6bb8SJim Ingham                         StopInfoSP stop_info_sp = m_thread.GetStopInfo();
149513c6bb8SJim Ingham                         if (stop_info_sp)
150513c6bb8SJim Ingham                         {
151513c6bb8SJim Ingham                             switch (stop_info_sp->GetStopReason())
152513c6bb8SJim Ingham                             {
153513c6bb8SJim Ingham                             case eStopReasonWatchpoint:
154513c6bb8SJim Ingham                             case eStopReasonException:
15590ba8115SGreg Clayton                             case eStopReasonExec:
156513c6bb8SJim Ingham                             case eStopReasonSignal:
157513c6bb8SJim Ingham                                 // In all these cases we want to stop in the deepest most frame.
158513c6bb8SJim Ingham                                 m_current_inlined_pc = curr_pc;
159513c6bb8SJim Ingham                                 m_current_inlined_depth = 0;
160513c6bb8SJim Ingham                                 break;
1617da851a3SJim Ingham                             case eStopReasonBreakpoint:
1627da851a3SJim Ingham                                 {
1637da851a3SJim Ingham                                     // FIXME: Figure out what this break point is doing, and set the inline depth
1647da851a3SJim Ingham                                     // appropriately.  Be careful to take into account breakpoints that implement
1657da851a3SJim Ingham                                     // step over prologue, since that should do the default calculation.
166c635500dSJim Ingham                                     // For now, if the breakpoints corresponding to this hit are all internal,
167c635500dSJim Ingham                                     // I set the stop location to the top of the inlined stack, since that will make
168c635500dSJim Ingham                                     // things like stepping over prologues work right.  But if there are any non-internal
169c635500dSJim Ingham                                     // breakpoints I do to the bottom of the stack, since that was the old behavior.
170c635500dSJim Ingham                                     uint32_t bp_site_id = stop_info_sp->GetValue();
171c635500dSJim Ingham                                     BreakpointSiteSP bp_site_sp(m_thread.GetProcess()->GetBreakpointSiteList().FindByID(bp_site_id));
172c635500dSJim Ingham                                     bool all_internal = true;
173c635500dSJim Ingham                                     if (bp_site_sp)
174c635500dSJim Ingham                                     {
175c635500dSJim Ingham                                         uint32_t num_owners = bp_site_sp->GetNumberOfOwners();
176c635500dSJim Ingham                                         for (uint32_t i = 0; i < num_owners; i++)
177c635500dSJim Ingham                                         {
178c635500dSJim Ingham                                             Breakpoint &bp_ref = bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint();
179c635500dSJim Ingham                                             if (!bp_ref.IsInternal())
180c635500dSJim Ingham                                             {
181c635500dSJim Ingham                                                 all_internal = false;
182c635500dSJim Ingham                                             }
183c635500dSJim Ingham                                         }
184c635500dSJim Ingham                                     }
185c635500dSJim Ingham                                     if (!all_internal)
186c635500dSJim Ingham                                     {
187c635500dSJim Ingham                                         m_current_inlined_pc = curr_pc;
188c635500dSJim Ingham                                         m_current_inlined_depth = 0;
189c635500dSJim Ingham                                         break;
190c635500dSJim Ingham                                     }
1917da851a3SJim Ingham                                 }
192513c6bb8SJim Ingham                             default:
193513c6bb8SJim Ingham                                 {
194513c6bb8SJim Ingham                                     // Otherwise, we should set ourselves at the container of the inlining, so that the
195513c6bb8SJim Ingham                                     // user can descend into them.
196513c6bb8SJim Ingham                                     // So first we check whether we have more than one inlined block sharing this PC:
197513c6bb8SJim Ingham                                     int num_inlined_functions = 0;
198513c6bb8SJim Ingham 
199513c6bb8SJim Ingham                                     for  (Block *container_ptr = block_ptr->GetInlinedParent();
200513c6bb8SJim Ingham                                               container_ptr != NULL;
201513c6bb8SJim Ingham                                               container_ptr = container_ptr->GetInlinedParent())
202513c6bb8SJim Ingham                                     {
203513c6bb8SJim Ingham                                         if (!container_ptr->GetRangeContainingAddress(pc_as_address, containing_range))
204513c6bb8SJim Ingham                                             break;
205513c6bb8SJim Ingham                                         if (pc_as_address != containing_range.GetBaseAddress())
206513c6bb8SJim Ingham                                             break;
207513c6bb8SJim Ingham 
208513c6bb8SJim Ingham                                         num_inlined_functions++;
209513c6bb8SJim Ingham                                     }
210513c6bb8SJim Ingham                                     m_current_inlined_pc = curr_pc;
211513c6bb8SJim Ingham                                     m_current_inlined_depth = num_inlined_functions + 1;
2125160ce5cSGreg Clayton                                     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
2136cd41da7SJim Ingham                                     if (log && log->GetVerbose())
214d01b2953SDaniel Malea                                         log->Printf ("ResetCurrentInlinedDepth: setting inlined depth: %d 0x%" PRIx64 ".\n", m_current_inlined_depth, curr_pc);
215513c6bb8SJim Ingham 
216513c6bb8SJim Ingham                                 }
217513c6bb8SJim Ingham                                 break;
218513c6bb8SJim Ingham                             }
219513c6bb8SJim Ingham                         }
220513c6bb8SJim Ingham                     }
221513c6bb8SJim Ingham                 }
222513c6bb8SJim Ingham             }
223513c6bb8SJim Ingham         }
224513c6bb8SJim Ingham     }
225513c6bb8SJim Ingham }
226513c6bb8SJim Ingham 
227513c6bb8SJim Ingham bool
228513c6bb8SJim Ingham StackFrameList::DecrementCurrentInlinedDepth ()
229513c6bb8SJim Ingham {
230513c6bb8SJim Ingham     if (m_show_inlined_frames)
231513c6bb8SJim Ingham     {
232513c6bb8SJim Ingham         uint32_t current_inlined_depth = GetCurrentInlinedDepth();
233513c6bb8SJim Ingham         if (current_inlined_depth != UINT32_MAX)
234513c6bb8SJim Ingham         {
235513c6bb8SJim Ingham             if (current_inlined_depth > 0)
2369786eeebSJim Ingham             {
237513c6bb8SJim Ingham                 m_current_inlined_depth--;
238513c6bb8SJim Ingham                 return true;
239513c6bb8SJim Ingham             }
240513c6bb8SJim Ingham         }
2419786eeebSJim Ingham     }
242513c6bb8SJim Ingham     return false;
243513c6bb8SJim Ingham }
244513c6bb8SJim Ingham 
245513c6bb8SJim Ingham void
2466cd41da7SJim Ingham StackFrameList::SetCurrentInlinedDepth (uint32_t new_depth)
2476cd41da7SJim Ingham {
2486cd41da7SJim Ingham     m_current_inlined_depth = new_depth;
2496cd41da7SJim Ingham     if (new_depth == UINT32_MAX)
2506cd41da7SJim Ingham         m_current_inlined_pc = LLDB_INVALID_ADDRESS;
2516cd41da7SJim Ingham     else
2526cd41da7SJim Ingham         m_current_inlined_pc = m_thread.GetRegisterContext()->GetPC();
2536cd41da7SJim Ingham }
2546cd41da7SJim Ingham 
2556cd41da7SJim Ingham void
256b0c72a5fSJim Ingham StackFrameList::GetFramesUpTo(uint32_t end_idx)
25730fdc8d8SChris Lattner {
258ebafd2f1SEnrico Granata     // this makes sure we do not fetch frames for an invalid thread
259ebafd2f1SEnrico Granata     if (m_thread.IsValid() == false)
260ebafd2f1SEnrico Granata         return;
261ebafd2f1SEnrico Granata 
262b0c72a5fSJim Ingham     // We've already gotten more frames than asked for, or we've already finished unwinding, return.
263b0c72a5fSJim Ingham     if (m_frames.size() > end_idx || GetAllFramesFetched())
264b0c72a5fSJim Ingham         return;
26512daf946SGreg Clayton 
266b0c72a5fSJim Ingham     Unwind *unwinder = m_thread.GetUnwinder ();
267b0c72a5fSJim Ingham 
26812daf946SGreg Clayton     if (m_show_inlined_frames)
26912daf946SGreg Clayton     {
2705082c5fdSGreg Clayton #if defined (DEBUG_STACK_FRAMES)
2712c595273SJohnny Chen         StreamFile s(stdout, false);
2725082c5fdSGreg Clayton #endif
273513c6bb8SJim Ingham         // If we are hiding some frames from the outside world, we need to add those onto the total count of
274aaa0ba31SBruce Mitchener         // frames to fetch.  However, we don't need to do that if end_idx is 0 since in that case we always
2756cd41da7SJim Ingham         // get the first concrete frame and all the inlined frames below it...  And of course, if end_idx is
2766cd41da7SJim Ingham         // UINT32_MAX that means get all, so just do that...
277513c6bb8SJim Ingham 
278513c6bb8SJim Ingham         uint32_t inlined_depth = 0;
2796cd41da7SJim Ingham         if (end_idx > 0 && end_idx != UINT32_MAX)
280513c6bb8SJim Ingham         {
281513c6bb8SJim Ingham             inlined_depth = GetCurrentInlinedDepth();
282513c6bb8SJim Ingham             if (inlined_depth != UINT32_MAX)
283513c6bb8SJim Ingham             {
284513c6bb8SJim Ingham                 if (end_idx > 0)
285513c6bb8SJim Ingham                     end_idx += inlined_depth;
286513c6bb8SJim Ingham             }
287513c6bb8SJim Ingham         }
28812daf946SGreg Clayton 
289b57e4a1bSJason Molenda         StackFrameSP unwind_frame_sp;
290b0c72a5fSJim Ingham         do
29112daf946SGreg Clayton         {
292b0c72a5fSJim Ingham             uint32_t idx = m_concrete_frames_fetched++;
2938012cadbSGreg Clayton             lldb::addr_t pc = LLDB_INVALID_ADDRESS;
2948012cadbSGreg Clayton             lldb::addr_t cfa = LLDB_INVALID_ADDRESS;
29512daf946SGreg Clayton             if (idx == 0)
29612daf946SGreg Clayton             {
2975082c5fdSGreg Clayton                 // We might have already created frame zero, only create it
2985082c5fdSGreg Clayton                 // if we need to
2995082c5fdSGreg Clayton                 if (m_frames.empty())
3005082c5fdSGreg Clayton                 {
301b3ae8761SGreg Clayton                     RegisterContextSP reg_ctx_sp (m_thread.GetRegisterContext());
302b3ae8761SGreg Clayton 
303b3ae8761SGreg Clayton                     if (reg_ctx_sp)
304b3ae8761SGreg Clayton                     {
305376c4854SJim Ingham 
306ec6829eaSGreg Clayton                         const bool success = unwinder && unwinder->GetFrameInfoAtIndex(idx, cfa, pc);
307376c4854SJim Ingham                         // There shouldn't be any way not to get the frame info for frame 0.
308376c4854SJim Ingham                         // But if the unwinder can't make one, lets make one by hand with the
309376c4854SJim Ingham                         // SP as the CFA and see if that gets any further.
310376c4854SJim Ingham                         if (!success)
311376c4854SJim Ingham                         {
312b3ae8761SGreg Clayton                             cfa = reg_ctx_sp->GetSP();
313b3ae8761SGreg Clayton                             pc = reg_ctx_sp->GetPC();
314376c4854SJim Ingham                         }
315376c4854SJim Ingham 
316d9e416c0SGreg Clayton                         unwind_frame_sp.reset (new StackFrame (m_thread.shared_from_this(),
317d9e416c0SGreg Clayton                                                                m_frames.size(),
3185082c5fdSGreg Clayton                                                                idx,
319b3ae8761SGreg Clayton                                                                reg_ctx_sp,
32059e8fc1cSGreg Clayton                                                                cfa,
3211692b901SJim Ingham                                                                pc,
32212daf946SGreg Clayton                                                                NULL));
3235082c5fdSGreg Clayton                         m_frames.push_back (unwind_frame_sp);
3245082c5fdSGreg Clayton                     }
325b3ae8761SGreg Clayton                 }
3265082c5fdSGreg Clayton                 else
3275082c5fdSGreg Clayton                 {
3285082c5fdSGreg Clayton                     unwind_frame_sp = m_frames.front();
329b57e4a1bSJason Molenda                     cfa = unwind_frame_sp->m_id.GetCallFrameAddress();
3305082c5fdSGreg Clayton                 }
33112daf946SGreg Clayton             }
33212daf946SGreg Clayton             else
33312daf946SGreg Clayton             {
334ec6829eaSGreg Clayton                 const bool success = unwinder && unwinder->GetFrameInfoAtIndex(idx, cfa, pc);
335b0c72a5fSJim Ingham                 if (!success)
336b0c72a5fSJim Ingham                 {
337b0c72a5fSJim Ingham                     // We've gotten to the end of the stack.
338b0c72a5fSJim Ingham                     SetAllFramesFetched();
339b0c72a5fSJim Ingham                     break;
340b0c72a5fSJim Ingham                 }
34199618476SJason Molenda                 const bool cfa_is_valid = true;
34299618476SJason Molenda                 const bool stop_id_is_valid = false;
34399618476SJason Molenda                 const bool is_history_frame = false;
34499618476SJason Molenda                 unwind_frame_sp.reset (new StackFrame (m_thread.shared_from_this(), m_frames.size(), idx, cfa, cfa_is_valid, pc, 0, stop_id_is_valid, is_history_frame, NULL));
3455082c5fdSGreg Clayton                 m_frames.push_back (unwind_frame_sp);
34612daf946SGreg Clayton             }
34712daf946SGreg Clayton 
348ff1b5c42SEd Maste             assert(unwind_frame_sp);
3491ed54f50SGreg Clayton             SymbolContext unwind_sc = unwind_frame_sp->GetSymbolContext (eSymbolContextBlock | eSymbolContextFunction);
3501ed54f50SGreg Clayton             Block *unwind_block = unwind_sc.block;
35159e8fc1cSGreg Clayton             if (unwind_block)
35212daf946SGreg Clayton             {
3535f4c61e2SGreg Clayton                 Address curr_frame_address (unwind_frame_sp->GetFrameCodeAddress());
3545f4c61e2SGreg Clayton                 // Be sure to adjust the frame address to match the address
3555f4c61e2SGreg Clayton                 // that was used to lookup the symbol context above. If we are
3565f4c61e2SGreg Clayton                 // in the first concrete frame, then we lookup using the current
3575f4c61e2SGreg Clayton                 // address, else we decrement the address by one to get the correct
3585f4c61e2SGreg Clayton                 // location.
3595f4c61e2SGreg Clayton                 if (idx > 0)
3600f8452baSTamas Berghammer                 {
3610f8452baSTamas Berghammer                     if (curr_frame_address.GetOffset() == 0)
3620f8452baSTamas Berghammer                     {
3630f8452baSTamas Berghammer                         // If curr_frame_address points to the first address in a section then after
3640f8452baSTamas Berghammer                         // adjustment it will point to an other section. In that case resolve the
3650f8452baSTamas Berghammer                         // address again to the correct section plus offset form.
3660f8452baSTamas Berghammer                         TargetSP target = m_thread.CalculateTarget();
36725b9f7ebSTamas Berghammer                         addr_t load_addr = curr_frame_address.GetOpcodeLoadAddress(target.get(), eAddressClassCode);
36825b9f7ebSTamas Berghammer                         curr_frame_address.SetOpcodeLoadAddress(load_addr - 1, target.get(), eAddressClassCode);
3690f8452baSTamas Berghammer                     }
3700f8452baSTamas Berghammer                     else
3710f8452baSTamas Berghammer                     {
3725f4c61e2SGreg Clayton                         curr_frame_address.Slide(-1);
3730f8452baSTamas Berghammer                     }
3740f8452baSTamas Berghammer                 }
3755f4c61e2SGreg Clayton 
3761ed54f50SGreg Clayton                 SymbolContext next_frame_sc;
3771ed54f50SGreg Clayton                 Address next_frame_address;
3781ed54f50SGreg Clayton 
3791ed54f50SGreg Clayton                 while (unwind_sc.GetParentOfInlinedScope(curr_frame_address, next_frame_sc, next_frame_address))
38059e8fc1cSGreg Clayton                 {
381b57e4a1bSJason Molenda                         StackFrameSP frame_sp(new StackFrame (m_thread.shared_from_this(),
382d9e416c0SGreg Clayton                                                               m_frames.size(),
3835082c5fdSGreg Clayton                                                               idx,
3845082c5fdSGreg Clayton                                                               unwind_frame_sp->GetRegisterContextSP (),
38559e8fc1cSGreg Clayton                                                               cfa,
3861ed54f50SGreg Clayton                                                               next_frame_address,
3871ed54f50SGreg Clayton                                                               &next_frame_sc));
3885082c5fdSGreg Clayton 
3895082c5fdSGreg Clayton                         m_frames.push_back (frame_sp);
3901ed54f50SGreg Clayton                         unwind_sc = next_frame_sc;
3911ed54f50SGreg Clayton                         curr_frame_address = next_frame_address;
392b0c72a5fSJim Ingham                 }
393b0c72a5fSJim Ingham             }
394b0c72a5fSJim Ingham         } while (m_frames.size() - 1 < end_idx);
3951ed54f50SGreg Clayton 
396b0c72a5fSJim Ingham         // Don't try to merge till you've calculated all the frames in this stack.
397b0c72a5fSJim Ingham         if (GetAllFramesFetched() && m_prev_frames_sp)
39812daf946SGreg Clayton         {
3992cad65a5SGreg Clayton             StackFrameList *prev_frames = m_prev_frames_sp.get();
4005082c5fdSGreg Clayton             StackFrameList *curr_frames = this;
4015082c5fdSGreg Clayton 
4026cd41da7SJim Ingham             //curr_frames->m_current_inlined_depth = prev_frames->m_current_inlined_depth;
4036cd41da7SJim Ingham             //curr_frames->m_current_inlined_pc = prev_frames->m_current_inlined_pc;
404d01b2953SDaniel Malea             //printf ("GetFramesUpTo: Copying current inlined depth: %d 0x%" PRIx64 ".\n", curr_frames->m_current_inlined_depth, curr_frames->m_current_inlined_pc);
4059786eeebSJim Ingham 
4065082c5fdSGreg Clayton #if defined (DEBUG_STACK_FRAMES)
40768275d5eSGreg Clayton             s.PutCString("\nprev_frames:\n");
4085082c5fdSGreg Clayton             prev_frames->Dump (&s);
40968275d5eSGreg Clayton             s.PutCString("\ncurr_frames:\n");
4105082c5fdSGreg Clayton             curr_frames->Dump (&s);
4115082c5fdSGreg Clayton             s.EOL();
4125082c5fdSGreg Clayton #endif
4135082c5fdSGreg Clayton             size_t curr_frame_num, prev_frame_num;
4145082c5fdSGreg Clayton 
4155082c5fdSGreg Clayton             for (curr_frame_num = curr_frames->m_frames.size(), prev_frame_num = prev_frames->m_frames.size();
4165082c5fdSGreg Clayton                  curr_frame_num > 0 && prev_frame_num > 0;
4175082c5fdSGreg Clayton                  --curr_frame_num, --prev_frame_num)
4185082c5fdSGreg Clayton             {
4195082c5fdSGreg Clayton                 const size_t curr_frame_idx = curr_frame_num-1;
4205082c5fdSGreg Clayton                 const size_t prev_frame_idx = prev_frame_num-1;
421b57e4a1bSJason Molenda                 StackFrameSP curr_frame_sp (curr_frames->m_frames[curr_frame_idx]);
422b57e4a1bSJason Molenda                 StackFrameSP prev_frame_sp (prev_frames->m_frames[prev_frame_idx]);
4235082c5fdSGreg Clayton 
4245082c5fdSGreg Clayton #if defined (DEBUG_STACK_FRAMES)
4252cad65a5SGreg Clayton                 s.Printf("\n\nCurr frame #%u ", curr_frame_idx);
4265082c5fdSGreg Clayton                 if (curr_frame_sp)
4272cad65a5SGreg Clayton                     curr_frame_sp->Dump (&s, true, false);
4285082c5fdSGreg Clayton                 else
4295082c5fdSGreg Clayton                     s.PutCString("NULL");
4302cad65a5SGreg Clayton                 s.Printf("\nPrev frame #%u ", prev_frame_idx);
4315082c5fdSGreg Clayton                 if (prev_frame_sp)
4322cad65a5SGreg Clayton                     prev_frame_sp->Dump (&s, true, false);
4335082c5fdSGreg Clayton                 else
4345082c5fdSGreg Clayton                     s.PutCString("NULL");
4355082c5fdSGreg Clayton #endif
4365082c5fdSGreg Clayton 
437b57e4a1bSJason Molenda                 StackFrame *curr_frame = curr_frame_sp.get();
438b57e4a1bSJason Molenda                 StackFrame *prev_frame = prev_frame_sp.get();
4395082c5fdSGreg Clayton 
4405082c5fdSGreg Clayton                 if (curr_frame == NULL || prev_frame == NULL)
4415082c5fdSGreg Clayton                     break;
4425082c5fdSGreg Clayton 
44359e8fc1cSGreg Clayton                 // Check the stack ID to make sure they are equal
44459e8fc1cSGreg Clayton                 if (curr_frame->GetStackID() != prev_frame->GetStackID())
4455082c5fdSGreg Clayton                     break;
4465082c5fdSGreg Clayton 
44759e8fc1cSGreg Clayton                 prev_frame->UpdatePreviousFrameFromCurrentFrame (*curr_frame);
44859e8fc1cSGreg Clayton                 // Now copy the fixed up previous frame into the current frames
44959e8fc1cSGreg Clayton                 // so the pointer doesn't change
45059e8fc1cSGreg Clayton                 m_frames[curr_frame_idx] = prev_frame_sp;
45159e8fc1cSGreg Clayton                 //curr_frame->UpdateCurrentFrameFromPreviousFrame (*prev_frame);
4525082c5fdSGreg Clayton 
4535082c5fdSGreg Clayton #if defined (DEBUG_STACK_FRAMES)
45468275d5eSGreg Clayton                 s.Printf("\n    Copying previous frame to current frame");
4555082c5fdSGreg Clayton #endif
4565082c5fdSGreg Clayton             }
4575082c5fdSGreg Clayton             // We are done with the old stack frame list, we can release it now
4582cad65a5SGreg Clayton             m_prev_frames_sp.reset();
4595082c5fdSGreg Clayton         }
46068275d5eSGreg Clayton 
46168275d5eSGreg Clayton #if defined (DEBUG_STACK_FRAMES)
46268275d5eSGreg Clayton             s.PutCString("\n\nNew frames:\n");
46368275d5eSGreg Clayton             Dump (&s);
46468275d5eSGreg Clayton             s.EOL();
46568275d5eSGreg Clayton #endif
46612daf946SGreg Clayton     }
46712daf946SGreg Clayton     else
46812daf946SGreg Clayton     {
469b0c72a5fSJim Ingham         if (end_idx < m_concrete_frames_fetched)
470b0c72a5fSJim Ingham             return;
471b0c72a5fSJim Ingham 
472ec6829eaSGreg Clayton         if (unwinder)
473ec6829eaSGreg Clayton         {
474b0c72a5fSJim Ingham             uint32_t num_frames = unwinder->GetFramesUpTo(end_idx);
475b0c72a5fSJim Ingham             if (num_frames <= end_idx + 1)
476b0c72a5fSJim Ingham             {
477b0c72a5fSJim Ingham                 //Done unwinding.
478b0c72a5fSJim Ingham                 m_concrete_frames_fetched = UINT32_MAX;
479b0c72a5fSJim Ingham             }
480b0c72a5fSJim Ingham             m_frames.resize(num_frames);
48112daf946SGreg Clayton         }
4825082c5fdSGreg Clayton     }
483ec6829eaSGreg Clayton }
484b0c72a5fSJim Ingham 
485b0c72a5fSJim Ingham uint32_t
486b0c72a5fSJim Ingham StackFrameList::GetNumFrames (bool can_create)
487b0c72a5fSJim Ingham {
488b0c72a5fSJim Ingham     Mutex::Locker locker (m_mutex);
489b0c72a5fSJim Ingham 
490b0c72a5fSJim Ingham     if (can_create)
491b0c72a5fSJim Ingham         GetFramesUpTo (UINT32_MAX);
492513c6bb8SJim Ingham 
493513c6bb8SJim Ingham     uint32_t inlined_depth = GetCurrentInlinedDepth();
494513c6bb8SJim Ingham     if (inlined_depth == UINT32_MAX)
4955082c5fdSGreg Clayton         return m_frames.size();
496513c6bb8SJim Ingham     else
497513c6bb8SJim Ingham         return m_frames.size() - inlined_depth;
49830fdc8d8SChris Lattner }
49930fdc8d8SChris Lattner 
5005082c5fdSGreg Clayton void
5015082c5fdSGreg Clayton StackFrameList::Dump (Stream *s)
50230fdc8d8SChris Lattner {
5035082c5fdSGreg Clayton     if (s == NULL)
5045082c5fdSGreg Clayton         return;
5055082c5fdSGreg Clayton     Mutex::Locker locker (m_mutex);
50630fdc8d8SChris Lattner 
5075082c5fdSGreg Clayton     const_iterator pos, begin = m_frames.begin(), end = m_frames.end();
5085082c5fdSGreg Clayton     for (pos = begin; pos != end; ++pos)
50912daf946SGreg Clayton     {
510b57e4a1bSJason Molenda         StackFrame *frame = (*pos).get();
511324a1036SSaleem Abdulrasool         s->Printf("%p: ", static_cast<void*>(frame));
5125082c5fdSGreg Clayton         if (frame)
51359e8fc1cSGreg Clayton         {
51459e8fc1cSGreg Clayton             frame->GetStackID().Dump (s);
5150603aa9dSGreg Clayton             frame->DumpUsingSettingsFormat (s);
51659e8fc1cSGreg Clayton         }
5175082c5fdSGreg Clayton         else
518dce502edSGreg Clayton             s->Printf("frame #%u", (uint32_t)std::distance (begin, pos));
5195082c5fdSGreg Clayton         s->EOL();
52012daf946SGreg Clayton     }
5215082c5fdSGreg Clayton     s->EOL();
5225082c5fdSGreg Clayton }
52312daf946SGreg Clayton 
524b57e4a1bSJason Molenda StackFrameSP
52512daf946SGreg Clayton StackFrameList::GetFrameAtIndex (uint32_t idx)
52630fdc8d8SChris Lattner {
527b57e4a1bSJason Molenda     StackFrameSP frame_sp;
52830fdc8d8SChris Lattner     Mutex::Locker locker (m_mutex);
5295cb9a184SJim Ingham     uint32_t original_idx = idx;
5305cb9a184SJim Ingham 
531513c6bb8SJim Ingham     uint32_t inlined_depth = GetCurrentInlinedDepth();
532513c6bb8SJim Ingham     if (inlined_depth != UINT32_MAX)
533513c6bb8SJim Ingham         idx += inlined_depth;
534513c6bb8SJim Ingham 
5355082c5fdSGreg Clayton     if (idx < m_frames.size())
5365082c5fdSGreg Clayton         frame_sp = m_frames[idx];
53712daf946SGreg Clayton 
5385082c5fdSGreg Clayton     if (frame_sp)
53912daf946SGreg Clayton         return frame_sp;
54012daf946SGreg Clayton 
5411692b901SJim Ingham     // GetFramesUpTo will fill m_frames with as many frames as you asked for,
5421692b901SJim Ingham     // if there are that many.  If there weren't then you asked for too many
5431692b901SJim Ingham     // frames.
544b0c72a5fSJim Ingham     GetFramesUpTo (idx);
545b0c72a5fSJim Ingham     if (idx < m_frames.size())
54612daf946SGreg Clayton     {
54712daf946SGreg Clayton         if (m_show_inlined_frames)
54812daf946SGreg Clayton         {
5491692b901SJim Ingham             // When inline frames are enabled we actually create all the frames in GetFramesUpTo.
5505082c5fdSGreg Clayton             frame_sp = m_frames[idx];
55112daf946SGreg Clayton         }
55212daf946SGreg Clayton         else
55312daf946SGreg Clayton         {
55412daf946SGreg Clayton             Unwind *unwinder = m_thread.GetUnwinder ();
55512daf946SGreg Clayton             if (unwinder)
55612daf946SGreg Clayton             {
55712daf946SGreg Clayton                 addr_t pc, cfa;
55812daf946SGreg Clayton                 if (unwinder->GetFrameInfoAtIndex(idx, cfa, pc))
5595082c5fdSGreg Clayton                 {
56099618476SJason Molenda                     const bool cfa_is_valid = true;
56199618476SJason Molenda                     const bool stop_id_is_valid = false;
56299618476SJason Molenda                     const bool is_history_frame = false;
56399618476SJason Molenda                     frame_sp.reset (new StackFrame (m_thread.shared_from_this(), idx, idx, cfa, cfa_is_valid, pc, 0, stop_id_is_valid, is_history_frame, NULL));
56459e8fc1cSGreg Clayton 
56559e8fc1cSGreg Clayton                     Function *function = frame_sp->GetSymbolContext (eSymbolContextFunction).function;
56659e8fc1cSGreg Clayton                     if (function)
56759e8fc1cSGreg Clayton                     {
56859e8fc1cSGreg Clayton                         // When we aren't showing inline functions we always use
56959e8fc1cSGreg Clayton                         // the top most function block as the scope.
57059e8fc1cSGreg Clayton                         frame_sp->SetSymbolContextScope (&function->GetBlock(false));
57159e8fc1cSGreg Clayton                     }
57259e8fc1cSGreg Clayton                     else
57359e8fc1cSGreg Clayton                     {
57459e8fc1cSGreg Clayton                         // Set the symbol scope from the symbol regardless if it is NULL or valid.
57559e8fc1cSGreg Clayton                         frame_sp->SetSymbolContextScope (frame_sp->GetSymbolContext (eSymbolContextSymbol).symbol);
57659e8fc1cSGreg Clayton                     }
5775082c5fdSGreg Clayton                     SetFrameAtIndex(idx, frame_sp);
57812daf946SGreg Clayton                 }
57912daf946SGreg Clayton             }
58012daf946SGreg Clayton         }
58130fdc8d8SChris Lattner     }
5825cb9a184SJim Ingham     else if (original_idx == 0)
5835cb9a184SJim Ingham     {
5845cb9a184SJim Ingham         // There should ALWAYS be a frame at index 0.  If something went wrong with the CurrentInlinedDepth such that
5855cb9a184SJim Ingham         // there weren't as many frames as we thought taking that into account, then reset the current inlined depth
5865cb9a184SJim Ingham         // and return the real zeroth frame.
5875cb9a184SJim Ingham         if (m_frames.size() > 0)
5885cb9a184SJim Ingham         {
5895cb9a184SJim Ingham             ResetCurrentInlinedDepth();
5905cb9a184SJim Ingham             frame_sp = m_frames[original_idx];
5915cb9a184SJim Ingham         }
5925cb9a184SJim Ingham         else
5935cb9a184SJim Ingham         {
5945cb9a184SJim Ingham             // Why do we have a thread with zero frames, that should not ever happen...
5955cb9a184SJim Ingham             if (m_thread.IsValid())
5965cb9a184SJim Ingham                 assert ("A valid thread has no frames.");
5975cb9a184SJim Ingham 
5985cb9a184SJim Ingham         }
5995cb9a184SJim Ingham     }
6005cb9a184SJim Ingham 
60130fdc8d8SChris Lattner     return frame_sp;
60230fdc8d8SChris Lattner }
60330fdc8d8SChris Lattner 
604b57e4a1bSJason Molenda StackFrameSP
6055ccbd294SGreg Clayton StackFrameList::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx)
6065ccbd294SGreg Clayton {
6075ccbd294SGreg Clayton     // First try assuming the unwind index is the same as the frame index. The
6085ccbd294SGreg Clayton     // unwind index is always greater than or equal to the frame index, so it
6095ccbd294SGreg Clayton     // is a good place to start. If we have inlined frames we might have 5
6105ccbd294SGreg Clayton     // concrete frames (frame unwind indexes go from 0-4), but we might have 15
6115ccbd294SGreg Clayton     // frames after we make all the inlined frames. Most of the time the unwind
6125ccbd294SGreg Clayton     // frame index (or the concrete frame index) is the same as the frame index.
6135ccbd294SGreg Clayton     uint32_t frame_idx = unwind_idx;
614b57e4a1bSJason Molenda     StackFrameSP frame_sp (GetFrameAtIndex (frame_idx));
6155ccbd294SGreg Clayton     while (frame_sp)
6165ccbd294SGreg Clayton     {
6175ccbd294SGreg Clayton         if (frame_sp->GetFrameIndex() == unwind_idx)
6185ccbd294SGreg Clayton             break;
6195ccbd294SGreg Clayton         frame_sp = GetFrameAtIndex (++frame_idx);
6205ccbd294SGreg Clayton     }
6215ccbd294SGreg Clayton     return frame_sp;
6225ccbd294SGreg Clayton }
6235ccbd294SGreg Clayton 
6247bcb93d5SGreg Clayton static bool
625b57e4a1bSJason Molenda CompareStackID (const StackFrameSP &stack_sp, const StackID &stack_id)
6267bcb93d5SGreg Clayton {
6277bcb93d5SGreg Clayton     return stack_sp->GetStackID() < stack_id;
6287bcb93d5SGreg Clayton }
6297bcb93d5SGreg Clayton 
630b57e4a1bSJason Molenda StackFrameSP
631cc4d0146SGreg Clayton StackFrameList::GetFrameWithStackID (const StackID &stack_id)
6323a195b7eSJim Ingham {
633b57e4a1bSJason Molenda     StackFrameSP frame_sp;
6347bcb93d5SGreg Clayton 
6357bcb93d5SGreg Clayton     if (stack_id.IsValid())
6367bcb93d5SGreg Clayton     {
6377bcb93d5SGreg Clayton         Mutex::Locker locker (m_mutex);
6387bcb93d5SGreg Clayton         uint32_t frame_idx = 0;
6397bcb93d5SGreg Clayton         // Do a binary search in case the stack frame is already in our cache
6407bcb93d5SGreg Clayton         collection::const_iterator begin = m_frames.begin();
6417bcb93d5SGreg Clayton         collection::const_iterator end = m_frames.end();
6427bcb93d5SGreg Clayton         if (begin != end)
6437bcb93d5SGreg Clayton         {
6447bcb93d5SGreg Clayton             collection::const_iterator pos = std::lower_bound (begin, end, stack_id, CompareStackID);
6458012cadbSGreg Clayton             if (pos != end)
6468012cadbSGreg Clayton             {
6478012cadbSGreg Clayton                 if ((*pos)->GetStackID() == stack_id)
6487bcb93d5SGreg Clayton                     return *pos;
6498012cadbSGreg Clayton             }
6507bcb93d5SGreg Clayton 
6518012cadbSGreg Clayton //            if (m_frames.back()->GetStackID() < stack_id)
6528012cadbSGreg Clayton //                frame_idx = m_frames.size();
6537bcb93d5SGreg Clayton         }
6543a195b7eSJim Ingham         do
6553a195b7eSJim Ingham         {
6563a195b7eSJim Ingham             frame_sp = GetFrameAtIndex (frame_idx);
6573a195b7eSJim Ingham             if (frame_sp && frame_sp->GetStackID() == stack_id)
6583a195b7eSJim Ingham                 break;
6593a195b7eSJim Ingham             frame_idx++;
6603a195b7eSJim Ingham         }
6613a195b7eSJim Ingham         while (frame_sp);
6627bcb93d5SGreg Clayton     }
6633a195b7eSJim Ingham     return frame_sp;
6643a195b7eSJim Ingham }
6655ccbd294SGreg Clayton 
66612daf946SGreg Clayton bool
667b57e4a1bSJason Molenda StackFrameList::SetFrameAtIndex (uint32_t idx, StackFrameSP &frame_sp)
66812daf946SGreg Clayton {
6695082c5fdSGreg Clayton     if (idx >= m_frames.size())
6705082c5fdSGreg Clayton         m_frames.resize(idx + 1);
67112daf946SGreg Clayton     // Make sure allocation succeeded by checking bounds again
6725082c5fdSGreg Clayton     if (idx < m_frames.size())
67312daf946SGreg Clayton     {
6745082c5fdSGreg Clayton         m_frames[idx] = frame_sp;
67530fdc8d8SChris Lattner         return true;
67630fdc8d8SChris Lattner     }
67730fdc8d8SChris Lattner     return false;   // resize failed, out of memory?
67830fdc8d8SChris Lattner }
67930fdc8d8SChris Lattner 
68030fdc8d8SChris Lattner uint32_t
6812976d00aSJim Ingham StackFrameList::GetSelectedFrameIndex () const
68230fdc8d8SChris Lattner {
68330fdc8d8SChris Lattner     Mutex::Locker locker (m_mutex);
6842976d00aSJim Ingham     return m_selected_frame_idx;
68530fdc8d8SChris Lattner }
68630fdc8d8SChris Lattner 
68730fdc8d8SChris Lattner 
68830fdc8d8SChris Lattner uint32_t
689b57e4a1bSJason Molenda StackFrameList::SetSelectedFrame (lldb_private::StackFrame *frame)
69030fdc8d8SChris Lattner {
69130fdc8d8SChris Lattner     Mutex::Locker locker (m_mutex);
69212daf946SGreg Clayton     const_iterator pos;
6935082c5fdSGreg Clayton     const_iterator begin = m_frames.begin();
6945082c5fdSGreg Clayton     const_iterator end = m_frames.end();
695b7f6b2faSJim Ingham     m_selected_frame_idx = 0;
69630fdc8d8SChris Lattner     for (pos = begin; pos != end; ++pos)
69730fdc8d8SChris Lattner     {
69830fdc8d8SChris Lattner         if (pos->get() == frame)
69930fdc8d8SChris Lattner         {
7002976d00aSJim Ingham             m_selected_frame_idx = std::distance (begin, pos);
701513c6bb8SJim Ingham             uint32_t inlined_depth = GetCurrentInlinedDepth();
702513c6bb8SJim Ingham             if (inlined_depth != UINT32_MAX)
703513c6bb8SJim Ingham                 m_selected_frame_idx -= inlined_depth;
704b7f6b2faSJim Ingham             break;
70530fdc8d8SChris Lattner         }
70630fdc8d8SChris Lattner     }
707b7f6b2faSJim Ingham     SetDefaultFileAndLineToSelectedFrame();
7082976d00aSJim Ingham     return m_selected_frame_idx;
70930fdc8d8SChris Lattner }
71030fdc8d8SChris Lattner 
71130fdc8d8SChris Lattner // Mark a stack frame as the current frame using the frame index
712b0c72a5fSJim Ingham bool
7132976d00aSJim Ingham StackFrameList::SetSelectedFrameByIndex (uint32_t idx)
71430fdc8d8SChris Lattner {
71530fdc8d8SChris Lattner     Mutex::Locker locker (m_mutex);
716b57e4a1bSJason Molenda     StackFrameSP frame_sp (GetFrameAtIndex (idx));
717b0c72a5fSJim Ingham     if (frame_sp)
718b0c72a5fSJim Ingham     {
719b0c72a5fSJim Ingham         SetSelectedFrame(frame_sp.get());
720b0c72a5fSJim Ingham         return true;
721b0c72a5fSJim Ingham     }
722b0c72a5fSJim Ingham     else
723b0c72a5fSJim Ingham         return false;
724b7f6b2faSJim Ingham }
725b7f6b2faSJim Ingham 
726b7f6b2faSJim Ingham void
727b7f6b2faSJim Ingham StackFrameList::SetDefaultFileAndLineToSelectedFrame()
728b7f6b2faSJim Ingham {
7291ac04c30SGreg Clayton     if (m_thread.GetID() == m_thread.GetProcess()->GetThreadList().GetSelectedThread()->GetID())
730b7f6b2faSJim Ingham     {
731b57e4a1bSJason Molenda         StackFrameSP frame_sp (GetFrameAtIndex (GetSelectedFrameIndex()));
732b7f6b2faSJim Ingham         if (frame_sp)
733b7f6b2faSJim Ingham         {
734252d0edeSGreg Clayton             SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextLineEntry);
735b7f6b2faSJim Ingham             if (sc.line_entry.file)
7361ac04c30SGreg Clayton                 m_thread.CalculateTarget()->GetSourceManager().SetDefaultFileAndLine (sc.line_entry.file,
737252d0edeSGreg Clayton                                                                                             sc.line_entry.line);
738b7f6b2faSJim Ingham         }
739b7f6b2faSJim Ingham     }
74030fdc8d8SChris Lattner }
74130fdc8d8SChris Lattner 
74230fdc8d8SChris Lattner // The thread has been run, reset the number stack frames to zero so we can
74330fdc8d8SChris Lattner // determine how many frames we have lazily.
74430fdc8d8SChris Lattner void
74530fdc8d8SChris Lattner StackFrameList::Clear ()
74630fdc8d8SChris Lattner {
74730fdc8d8SChris Lattner     Mutex::Locker locker (m_mutex);
7485082c5fdSGreg Clayton     m_frames.clear();
749b0c72a5fSJim Ingham     m_concrete_frames_fetched = 0;
75030fdc8d8SChris Lattner }
75130fdc8d8SChris Lattner 
75230fdc8d8SChris Lattner void
75330fdc8d8SChris Lattner StackFrameList::InvalidateFrames (uint32_t start_idx)
75430fdc8d8SChris Lattner {
75530fdc8d8SChris Lattner     Mutex::Locker locker (m_mutex);
75612daf946SGreg Clayton     if (m_show_inlined_frames)
75712daf946SGreg Clayton     {
75812daf946SGreg Clayton         Clear();
75912daf946SGreg Clayton     }
76012daf946SGreg Clayton     else
76112daf946SGreg Clayton     {
7625082c5fdSGreg Clayton         const size_t num_frames = m_frames.size();
76330fdc8d8SChris Lattner         while (start_idx < num_frames)
76430fdc8d8SChris Lattner         {
7655082c5fdSGreg Clayton             m_frames[start_idx].reset();
76630fdc8d8SChris Lattner             ++start_idx;
76730fdc8d8SChris Lattner         }
76830fdc8d8SChris Lattner     }
76912daf946SGreg Clayton }
7702cad65a5SGreg Clayton 
7712cad65a5SGreg Clayton void
7727b0992d9SGreg Clayton StackFrameList::Merge (std::unique_ptr<StackFrameList>& curr_ap, lldb::StackFrameListSP& prev_sp)
7732cad65a5SGreg Clayton {
77410ebffa4SJim Ingham     Mutex::Locker curr_locker (curr_ap.get() ? &curr_ap->m_mutex : NULL);
77510ebffa4SJim Ingham     Mutex::Locker prev_locker (prev_sp.get() ? &prev_sp->m_mutex : NULL);
7762cad65a5SGreg Clayton 
7772cad65a5SGreg Clayton #if defined (DEBUG_STACK_FRAMES)
7782c595273SJohnny Chen     StreamFile s(stdout, false);
7792cad65a5SGreg Clayton     s.PutCString("\n\nStackFrameList::Merge():\nPrev:\n");
7802cad65a5SGreg Clayton     if (prev_sp.get())
7812cad65a5SGreg Clayton         prev_sp->Dump (&s);
7822cad65a5SGreg Clayton     else
7832cad65a5SGreg Clayton         s.PutCString ("NULL");
7842cad65a5SGreg Clayton     s.PutCString("\nCurr:\n");
7852cad65a5SGreg Clayton     if (curr_ap.get())
7862cad65a5SGreg Clayton         curr_ap->Dump (&s);
7872cad65a5SGreg Clayton     else
7882cad65a5SGreg Clayton         s.PutCString ("NULL");
7892cad65a5SGreg Clayton     s.EOL();
7902cad65a5SGreg Clayton #endif
7912cad65a5SGreg Clayton 
7922cad65a5SGreg Clayton     if (curr_ap.get() == NULL || curr_ap->GetNumFrames (false) == 0)
7932cad65a5SGreg Clayton     {
7942cad65a5SGreg Clayton #if defined (DEBUG_STACK_FRAMES)
7952cad65a5SGreg Clayton         s.PutCString("No current frames, leave previous frames alone...\n");
7962cad65a5SGreg Clayton #endif
7972cad65a5SGreg Clayton         curr_ap.release();
7982cad65a5SGreg Clayton         return;
7992cad65a5SGreg Clayton     }
8002cad65a5SGreg Clayton 
8012cad65a5SGreg Clayton     if (prev_sp.get() == NULL || prev_sp->GetNumFrames (false) == 0)
8022cad65a5SGreg Clayton     {
8032cad65a5SGreg Clayton #if defined (DEBUG_STACK_FRAMES)
8042cad65a5SGreg Clayton         s.PutCString("No previous frames, so use current frames...\n");
8052cad65a5SGreg Clayton #endif
8062cad65a5SGreg Clayton         // We either don't have any previous frames, or since we have more than
8072cad65a5SGreg Clayton         // one current frames it means we have all the frames and can safely
8082cad65a5SGreg Clayton         // replace our previous frames.
8092cad65a5SGreg Clayton         prev_sp.reset (curr_ap.release());
8102cad65a5SGreg Clayton         return;
8112cad65a5SGreg Clayton     }
8122cad65a5SGreg Clayton 
8132cad65a5SGreg Clayton     const uint32_t num_curr_frames = curr_ap->GetNumFrames (false);
8142cad65a5SGreg Clayton 
8152cad65a5SGreg Clayton     if (num_curr_frames > 1)
8162cad65a5SGreg Clayton     {
8172cad65a5SGreg Clayton #if defined (DEBUG_STACK_FRAMES)
8182cad65a5SGreg Clayton         s.PutCString("We have more than one current frame, so use current frames...\n");
8192cad65a5SGreg Clayton #endif
8202cad65a5SGreg Clayton         // We have more than one current frames it means we have all the frames
8212cad65a5SGreg Clayton         // and can safely replace our previous frames.
8222cad65a5SGreg Clayton         prev_sp.reset (curr_ap.release());
8232cad65a5SGreg Clayton 
8242cad65a5SGreg Clayton #if defined (DEBUG_STACK_FRAMES)
8252cad65a5SGreg Clayton         s.PutCString("\nMerged:\n");
8262cad65a5SGreg Clayton         prev_sp->Dump (&s);
8272cad65a5SGreg Clayton #endif
8282cad65a5SGreg Clayton         return;
8292cad65a5SGreg Clayton     }
8302cad65a5SGreg Clayton 
831b57e4a1bSJason Molenda     StackFrameSP prev_frame_zero_sp(prev_sp->GetFrameAtIndex (0));
832b57e4a1bSJason Molenda     StackFrameSP curr_frame_zero_sp(curr_ap->GetFrameAtIndex (0));
8332cad65a5SGreg Clayton     StackID curr_stack_id (curr_frame_zero_sp->GetStackID());
8342cad65a5SGreg Clayton     StackID prev_stack_id (prev_frame_zero_sp->GetStackID());
8352cad65a5SGreg Clayton 
8362cad65a5SGreg Clayton #if defined (DEBUG_STACK_FRAMES)
8372c595273SJohnny Chen     const uint32_t num_prev_frames = prev_sp->GetNumFrames (false);
8382cad65a5SGreg Clayton     s.Printf("\n%u previous frames with one current frame\n", num_prev_frames);
8392cad65a5SGreg Clayton #endif
8402cad65a5SGreg Clayton 
8412cad65a5SGreg Clayton     // We have only a single current frame
8422cad65a5SGreg Clayton     // Our previous stack frames only had a single frame as well...
8432cad65a5SGreg Clayton     if (curr_stack_id == prev_stack_id)
8442cad65a5SGreg Clayton     {
8452cad65a5SGreg Clayton #if defined (DEBUG_STACK_FRAMES)
8462cad65a5SGreg Clayton         s.Printf("\nPrevious frame #0 is same as current frame #0, merge the cached data\n");
8472cad65a5SGreg Clayton #endif
8482cad65a5SGreg Clayton 
8492cad65a5SGreg Clayton         curr_frame_zero_sp->UpdateCurrentFrameFromPreviousFrame (*prev_frame_zero_sp);
8502cad65a5SGreg Clayton //        prev_frame_zero_sp->UpdatePreviousFrameFromCurrentFrame (*curr_frame_zero_sp);
8512cad65a5SGreg Clayton //        prev_sp->SetFrameAtIndex (0, prev_frame_zero_sp);
8522cad65a5SGreg Clayton     }
8532cad65a5SGreg Clayton     else if (curr_stack_id < prev_stack_id)
8542cad65a5SGreg Clayton     {
8552cad65a5SGreg Clayton #if defined (DEBUG_STACK_FRAMES)
8562cad65a5SGreg 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");
8572cad65a5SGreg Clayton #endif
8582cad65a5SGreg Clayton         prev_sp->m_frames.insert (prev_sp->m_frames.begin(), curr_frame_zero_sp);
8592cad65a5SGreg Clayton     }
8602cad65a5SGreg Clayton 
8612cad65a5SGreg Clayton     curr_ap.release();
8622cad65a5SGreg Clayton 
8632cad65a5SGreg Clayton #if defined (DEBUG_STACK_FRAMES)
8642cad65a5SGreg Clayton     s.PutCString("\nMerged:\n");
8652cad65a5SGreg Clayton     prev_sp->Dump (&s);
8662cad65a5SGreg Clayton #endif
8672cad65a5SGreg Clayton 
8682cad65a5SGreg Clayton 
8692cad65a5SGreg Clayton }
870e4284b71SJim Ingham 
871b57e4a1bSJason Molenda lldb::StackFrameSP
872b57e4a1bSJason Molenda StackFrameList::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr)
873e4284b71SJim Ingham {
874e4284b71SJim Ingham     const_iterator pos;
875e4284b71SJim Ingham     const_iterator begin = m_frames.begin();
876e4284b71SJim Ingham     const_iterator end = m_frames.end();
877b57e4a1bSJason Molenda     lldb::StackFrameSP ret_sp;
878e4284b71SJim Ingham 
879e4284b71SJim Ingham     for (pos = begin; pos != end; ++pos)
880e4284b71SJim Ingham     {
881e4284b71SJim Ingham         if (pos->get() == stack_frame_ptr)
882e4284b71SJim Ingham         {
883e4284b71SJim Ingham             ret_sp = (*pos);
884e4284b71SJim Ingham             break;
885e4284b71SJim Ingham         }
886e4284b71SJim Ingham     }
887e4284b71SJim Ingham     return ret_sp;
888e4284b71SJim Ingham }
889e4284b71SJim Ingham 
8907260f620SGreg Clayton size_t
8917260f620SGreg Clayton StackFrameList::GetStatus (Stream& strm,
8927260f620SGreg Clayton                            uint32_t first_frame,
8937260f620SGreg Clayton                            uint32_t num_frames,
8947260f620SGreg Clayton                            bool show_frame_info,
8958ec10efcSJim Ingham                            uint32_t num_frames_with_source,
8968ec10efcSJim Ingham                            const char *selected_frame_marker)
8977260f620SGreg Clayton {
8987260f620SGreg Clayton     size_t num_frames_displayed = 0;
8997260f620SGreg Clayton 
9007260f620SGreg Clayton     if (num_frames == 0)
9017260f620SGreg Clayton         return 0;
9027260f620SGreg Clayton 
903b57e4a1bSJason Molenda     StackFrameSP frame_sp;
9047260f620SGreg Clayton     uint32_t frame_idx = 0;
9057260f620SGreg Clayton     uint32_t last_frame;
9067260f620SGreg Clayton 
9077260f620SGreg Clayton     // Don't let the last frame wrap around...
9087260f620SGreg Clayton     if (num_frames == UINT32_MAX)
9097260f620SGreg Clayton         last_frame = UINT32_MAX;
9107260f620SGreg Clayton     else
9117260f620SGreg Clayton         last_frame = first_frame + num_frames;
9127260f620SGreg Clayton 
913b57e4a1bSJason Molenda     StackFrameSP selected_frame_sp = m_thread.GetSelectedFrame();
9148ec10efcSJim Ingham     const char *unselected_marker = NULL;
9158ec10efcSJim Ingham     std::string buffer;
9168ec10efcSJim Ingham     if (selected_frame_marker)
9178ec10efcSJim Ingham     {
9188ec10efcSJim Ingham         size_t len = strlen(selected_frame_marker);
9198ec10efcSJim Ingham         buffer.insert(buffer.begin(), len, ' ');
9208ec10efcSJim Ingham         unselected_marker = buffer.c_str();
9218ec10efcSJim Ingham     }
9228ec10efcSJim Ingham     const char *marker = NULL;
9238ec10efcSJim Ingham 
9247260f620SGreg Clayton     for (frame_idx = first_frame; frame_idx < last_frame; ++frame_idx)
9257260f620SGreg Clayton     {
9267260f620SGreg Clayton         frame_sp = GetFrameAtIndex(frame_idx);
9277260f620SGreg Clayton         if (frame_sp.get() == NULL)
9287260f620SGreg Clayton             break;
9297260f620SGreg Clayton 
9308ec10efcSJim Ingham         if (selected_frame_marker != NULL)
9318ec10efcSJim Ingham         {
9328ec10efcSJim Ingham             if (frame_sp == selected_frame_sp)
9338ec10efcSJim Ingham                 marker = selected_frame_marker;
9348ec10efcSJim Ingham             else
9358ec10efcSJim Ingham                 marker = unselected_marker;
9368ec10efcSJim Ingham         }
9378ec10efcSJim Ingham 
9387260f620SGreg Clayton         if (!frame_sp->GetStatus (strm,
9397260f620SGreg Clayton                                   show_frame_info,
9408ec10efcSJim Ingham                                   num_frames_with_source > (first_frame - frame_idx), marker))
9417260f620SGreg Clayton             break;
9427260f620SGreg Clayton         ++num_frames_displayed;
9437260f620SGreg Clayton     }
9447260f620SGreg Clayton 
9457260f620SGreg Clayton     strm.IndentLess();
9467260f620SGreg Clayton     return num_frames_displayed;
9477260f620SGreg Clayton }
9487260f620SGreg Clayton 
949