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 
1030fdc8d8SChris Lattner // C Includes
1130fdc8d8SChris Lattner // C++ Includes
1230fdc8d8SChris Lattner // Other libraries and framework includes
1330fdc8d8SChris Lattner // Project includes
14d70a6e71SEugene Zelenko #include "lldb/Target/StackFrameList.h"
15c635500dSJim Ingham #include "lldb/Breakpoint/BreakpointLocation.h"
16c635500dSJim Ingham #include "lldb/Breakpoint/Breakpoint.h"
176cd41da7SJim Ingham #include "lldb/Core/Log.h"
185082c5fdSGreg Clayton #include "lldb/Core/StreamFile.h"
19b7f6b2faSJim Ingham #include "lldb/Core/SourceManager.h"
2012daf946SGreg Clayton #include "lldb/Symbol/Block.h"
2112daf946SGreg Clayton #include "lldb/Symbol/Function.h"
2259e8fc1cSGreg Clayton #include "lldb/Symbol/Symbol.h"
23b7f6b2faSJim Ingham #include "lldb/Target/Process.h"
2412daf946SGreg Clayton #include "lldb/Target/RegisterContext.h"
2530fdc8d8SChris Lattner #include "lldb/Target/StackFrame.h"
26513c6bb8SJim Ingham #include "lldb/Target/StopInfo.h"
27b7f6b2faSJim Ingham #include "lldb/Target/Target.h"
2812daf946SGreg Clayton #include "lldb/Target/Thread.h"
2912daf946SGreg Clayton #include "lldb/Target/Unwind.h"
3030fdc8d8SChris Lattner 
315082c5fdSGreg Clayton //#define DEBUG_STACK_FRAMES 1
325082c5fdSGreg Clayton 
3330fdc8d8SChris Lattner using namespace lldb;
3430fdc8d8SChris Lattner using namespace lldb_private;
3530fdc8d8SChris Lattner 
3630fdc8d8SChris Lattner //----------------------------------------------------------------------
3730fdc8d8SChris Lattner // StackFrameList constructor
3830fdc8d8SChris Lattner //----------------------------------------------------------------------
39*bb19a13cSSaleem Abdulrasool StackFrameList::StackFrameList(Thread &thread, const lldb::StackFrameListSP &prev_frames_sp, bool show_inline_frames)
40*bb19a13cSSaleem Abdulrasool     : m_thread(thread),
412cad65a5SGreg Clayton       m_prev_frames_sp(prev_frames_sp),
42*bb19a13cSSaleem Abdulrasool       m_mutex(),
435082c5fdSGreg Clayton       m_frames(),
4471c21d18SStephen Wilson       m_selected_frame_idx(0),
45b0c72a5fSJim Ingham       m_concrete_frames_fetched(0),
46513c6bb8SJim Ingham       m_current_inlined_depth(UINT32_MAX),
47513c6bb8SJim Ingham       m_current_inlined_pc(LLDB_INVALID_ADDRESS),
4871c21d18SStephen Wilson       m_show_inlined_frames(show_inline_frames)
4930fdc8d8SChris Lattner {
50513c6bb8SJim Ingham     if (prev_frames_sp)
51513c6bb8SJim Ingham     {
52513c6bb8SJim Ingham         m_current_inlined_depth = prev_frames_sp->m_current_inlined_depth;
53513c6bb8SJim Ingham         m_current_inlined_pc = prev_frames_sp->m_current_inlined_pc;
54513c6bb8SJim Ingham     }
5530fdc8d8SChris Lattner }
5630fdc8d8SChris Lattner 
5730fdc8d8SChris Lattner StackFrameList::~StackFrameList()
5830fdc8d8SChris Lattner {
59ca5ce187SGreg Clayton     // Call clear since this takes a lock and clears the stack frame list
60ca5ce187SGreg Clayton     // in case another thread is currently using this stack frame list
61ca5ce187SGreg Clayton     Clear();
6230fdc8d8SChris Lattner }
6330fdc8d8SChris Lattner 
64b0c72a5fSJim Ingham void
65513c6bb8SJim Ingham StackFrameList::CalculateCurrentInlinedDepth()
66513c6bb8SJim Ingham {
67513c6bb8SJim Ingham     uint32_t cur_inlined_depth = GetCurrentInlinedDepth();
68513c6bb8SJim Ingham     if (cur_inlined_depth == UINT32_MAX)
69513c6bb8SJim Ingham     {
70513c6bb8SJim Ingham         ResetCurrentInlinedDepth();
71513c6bb8SJim Ingham     }
72513c6bb8SJim Ingham }
73513c6bb8SJim Ingham 
74513c6bb8SJim Ingham uint32_t
75513c6bb8SJim Ingham StackFrameList::GetCurrentInlinedDepth ()
76513c6bb8SJim Ingham {
776cd41da7SJim Ingham     if (m_show_inlined_frames && m_current_inlined_pc != LLDB_INVALID_ADDRESS)
78513c6bb8SJim Ingham     {
79513c6bb8SJim Ingham         lldb::addr_t cur_pc = m_thread.GetRegisterContext()->GetPC();
80513c6bb8SJim Ingham         if (cur_pc != m_current_inlined_pc)
81513c6bb8SJim Ingham         {
82513c6bb8SJim Ingham             m_current_inlined_pc = LLDB_INVALID_ADDRESS;
83513c6bb8SJim Ingham             m_current_inlined_depth = UINT32_MAX;
845160ce5cSGreg Clayton             Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
856cd41da7SJim Ingham             if (log && log->GetVerbose())
866cd41da7SJim Ingham                 log->Printf ("GetCurrentInlinedDepth: invalidating current inlined depth.\n");
87513c6bb8SJim Ingham         }
88513c6bb8SJim Ingham         return m_current_inlined_depth;
89513c6bb8SJim Ingham     }
90513c6bb8SJim Ingham     else
91513c6bb8SJim Ingham     {
92513c6bb8SJim Ingham         return UINT32_MAX;
93513c6bb8SJim Ingham     }
94513c6bb8SJim Ingham }
95513c6bb8SJim Ingham 
96513c6bb8SJim Ingham void
97513c6bb8SJim Ingham StackFrameList::ResetCurrentInlinedDepth ()
98513c6bb8SJim Ingham {
99*bb19a13cSSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(m_mutex);
10047db4a2cSKeno Fischer 
101e7e6ffc6SJim Ingham     if (m_show_inlined_frames)
102513c6bb8SJim Ingham     {
103513c6bb8SJim Ingham         GetFramesUpTo(0);
104d70a6e71SEugene Zelenko         if (m_frames.empty())
10565d4d5c3SRyan Brown             return;
106513c6bb8SJim Ingham         if (!m_frames[0]->IsInlined())
107513c6bb8SJim Ingham         {
108513c6bb8SJim Ingham             m_current_inlined_depth = UINT32_MAX;
109513c6bb8SJim Ingham             m_current_inlined_pc = LLDB_INVALID_ADDRESS;
1105160ce5cSGreg Clayton             Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
1116cd41da7SJim Ingham             if (log && log->GetVerbose())
1126cd41da7SJim Ingham                 log->Printf ("ResetCurrentInlinedDepth: Invalidating current inlined depth.\n");
113513c6bb8SJim Ingham         }
114513c6bb8SJim Ingham         else
115513c6bb8SJim Ingham         {
116513c6bb8SJim Ingham             // We only need to do something special about inlined blocks when we
117513c6bb8SJim Ingham             // are at the beginning of an inlined function:
118513c6bb8SJim Ingham             // FIXME: We probably also have to do something special if the PC is at the END
119513c6bb8SJim Ingham             // of an inlined function, which coincides with the end of either its containing
120513c6bb8SJim Ingham             // function or another inlined function.
121513c6bb8SJim Ingham 
122513c6bb8SJim Ingham             lldb::addr_t curr_pc = m_thread.GetRegisterContext()->GetPC();
123513c6bb8SJim Ingham             Block *block_ptr = m_frames[0]->GetFrameBlock();
124513c6bb8SJim Ingham             if (block_ptr)
125513c6bb8SJim Ingham             {
126513c6bb8SJim Ingham                 Address pc_as_address;
127513c6bb8SJim Ingham                 pc_as_address.SetLoadAddress(curr_pc, &(m_thread.GetProcess()->GetTarget()));
128513c6bb8SJim Ingham                 AddressRange containing_range;
129513c6bb8SJim Ingham                 if (block_ptr->GetRangeContainingAddress(pc_as_address, containing_range))
130513c6bb8SJim Ingham                 {
131513c6bb8SJim Ingham                     if (pc_as_address == containing_range.GetBaseAddress())
132513c6bb8SJim Ingham                     {
133513c6bb8SJim Ingham                         // If we got here because of a breakpoint hit, then set the inlined depth depending on where
134513c6bb8SJim Ingham                         // the breakpoint was set.
135513c6bb8SJim Ingham                         // If we got here because of a crash, then set the inlined depth to the deepest most block.
136513c6bb8SJim Ingham                         // Otherwise, we stopped here naturally as the result of a step, so set ourselves in the
137513c6bb8SJim Ingham                         // containing frame of the whole set of nested inlines, so the user can then "virtually"
138513c6bb8SJim Ingham                         // step into the frames one by one, or next over the whole mess.
139513c6bb8SJim Ingham                         // Note: We don't have to handle being somewhere in the middle of the stack here, since
140513c6bb8SJim Ingham                         // ResetCurrentInlinedDepth doesn't get called if there is a valid inlined depth set.
141513c6bb8SJim Ingham                         StopInfoSP stop_info_sp = m_thread.GetStopInfo();
142513c6bb8SJim Ingham                         if (stop_info_sp)
143513c6bb8SJim Ingham                         {
144513c6bb8SJim Ingham                             switch (stop_info_sp->GetStopReason())
145513c6bb8SJim Ingham                             {
146513c6bb8SJim Ingham                             case eStopReasonWatchpoint:
147513c6bb8SJim Ingham                             case eStopReasonException:
14890ba8115SGreg Clayton                             case eStopReasonExec:
149513c6bb8SJim Ingham                             case eStopReasonSignal:
150513c6bb8SJim Ingham                                 // In all these cases we want to stop in the deepest most frame.
151513c6bb8SJim Ingham                                 m_current_inlined_pc = curr_pc;
152513c6bb8SJim Ingham                                 m_current_inlined_depth = 0;
153513c6bb8SJim Ingham                                 break;
1547da851a3SJim Ingham                             case eStopReasonBreakpoint:
1557da851a3SJim Ingham                                 {
1567da851a3SJim Ingham                                     // FIXME: Figure out what this break point is doing, and set the inline depth
1577da851a3SJim Ingham                                     // appropriately.  Be careful to take into account breakpoints that implement
1587da851a3SJim Ingham                                     // step over prologue, since that should do the default calculation.
159c635500dSJim Ingham                                     // For now, if the breakpoints corresponding to this hit are all internal,
160c635500dSJim Ingham                                     // I set the stop location to the top of the inlined stack, since that will make
161c635500dSJim Ingham                                     // things like stepping over prologues work right.  But if there are any non-internal
162c635500dSJim Ingham                                     // breakpoints I do to the bottom of the stack, since that was the old behavior.
163c635500dSJim Ingham                                     uint32_t bp_site_id = stop_info_sp->GetValue();
164c635500dSJim Ingham                                     BreakpointSiteSP bp_site_sp(m_thread.GetProcess()->GetBreakpointSiteList().FindByID(bp_site_id));
165c635500dSJim Ingham                                     bool all_internal = true;
166c635500dSJim Ingham                                     if (bp_site_sp)
167c635500dSJim Ingham                                     {
168c635500dSJim Ingham                                         uint32_t num_owners = bp_site_sp->GetNumberOfOwners();
169c635500dSJim Ingham                                         for (uint32_t i = 0; i < num_owners; i++)
170c635500dSJim Ingham                                         {
171c635500dSJim Ingham                                             Breakpoint &bp_ref = bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint();
172c635500dSJim Ingham                                             if (!bp_ref.IsInternal())
173c635500dSJim Ingham                                             {
174c635500dSJim Ingham                                                 all_internal = false;
175c635500dSJim Ingham                                             }
176c635500dSJim Ingham                                         }
177c635500dSJim Ingham                                     }
178c635500dSJim Ingham                                     if (!all_internal)
179c635500dSJim Ingham                                     {
180c635500dSJim Ingham                                         m_current_inlined_pc = curr_pc;
181c635500dSJim Ingham                                         m_current_inlined_depth = 0;
182c635500dSJim Ingham                                         break;
183c635500dSJim Ingham                                     }
1847da851a3SJim Ingham                                 }
185cec91ef9SGreg Clayton                                 LLVM_FALLTHROUGH;
186513c6bb8SJim Ingham                             default:
187513c6bb8SJim Ingham                                 {
188513c6bb8SJim Ingham                                     // Otherwise, we should set ourselves at the container of the inlining, so that the
189513c6bb8SJim Ingham                                     // user can descend into them.
190513c6bb8SJim Ingham                                     // So first we check whether we have more than one inlined block sharing this PC:
191513c6bb8SJim Ingham                                     int num_inlined_functions = 0;
192513c6bb8SJim Ingham 
193513c6bb8SJim Ingham                                     for  (Block *container_ptr = block_ptr->GetInlinedParent();
194d70a6e71SEugene Zelenko                                               container_ptr != nullptr;
195513c6bb8SJim Ingham                                               container_ptr = container_ptr->GetInlinedParent())
196513c6bb8SJim Ingham                                     {
197513c6bb8SJim Ingham                                         if (!container_ptr->GetRangeContainingAddress(pc_as_address, containing_range))
198513c6bb8SJim Ingham                                             break;
199513c6bb8SJim Ingham                                         if (pc_as_address != containing_range.GetBaseAddress())
200513c6bb8SJim Ingham                                             break;
201513c6bb8SJim Ingham 
202513c6bb8SJim Ingham                                         num_inlined_functions++;
203513c6bb8SJim Ingham                                     }
204513c6bb8SJim Ingham                                     m_current_inlined_pc = curr_pc;
205513c6bb8SJim Ingham                                     m_current_inlined_depth = num_inlined_functions + 1;
2065160ce5cSGreg Clayton                                     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
2076cd41da7SJim Ingham                                     if (log && log->GetVerbose())
208d01b2953SDaniel Malea                                         log->Printf ("ResetCurrentInlinedDepth: setting inlined depth: %d 0x%" PRIx64 ".\n", m_current_inlined_depth, curr_pc);
209513c6bb8SJim Ingham 
210513c6bb8SJim Ingham                                 }
211513c6bb8SJim Ingham                                 break;
212513c6bb8SJim Ingham                             }
213513c6bb8SJim Ingham                         }
214513c6bb8SJim Ingham                     }
215513c6bb8SJim Ingham                 }
216513c6bb8SJim Ingham             }
217513c6bb8SJim Ingham         }
218513c6bb8SJim Ingham     }
219513c6bb8SJim Ingham }
220513c6bb8SJim Ingham 
221513c6bb8SJim Ingham bool
222513c6bb8SJim Ingham StackFrameList::DecrementCurrentInlinedDepth ()
223513c6bb8SJim Ingham {
224513c6bb8SJim Ingham     if (m_show_inlined_frames)
225513c6bb8SJim Ingham     {
226513c6bb8SJim Ingham         uint32_t current_inlined_depth = GetCurrentInlinedDepth();
227513c6bb8SJim Ingham         if (current_inlined_depth != UINT32_MAX)
228513c6bb8SJim Ingham         {
229513c6bb8SJim Ingham             if (current_inlined_depth > 0)
2309786eeebSJim Ingham             {
231513c6bb8SJim Ingham                 m_current_inlined_depth--;
232513c6bb8SJim Ingham                 return true;
233513c6bb8SJim Ingham             }
234513c6bb8SJim Ingham         }
2359786eeebSJim Ingham     }
236513c6bb8SJim Ingham     return false;
237513c6bb8SJim Ingham }
238513c6bb8SJim Ingham 
239513c6bb8SJim Ingham void
2406cd41da7SJim Ingham StackFrameList::SetCurrentInlinedDepth (uint32_t new_depth)
2416cd41da7SJim Ingham {
2426cd41da7SJim Ingham     m_current_inlined_depth = new_depth;
2436cd41da7SJim Ingham     if (new_depth == UINT32_MAX)
2446cd41da7SJim Ingham         m_current_inlined_pc = LLDB_INVALID_ADDRESS;
2456cd41da7SJim Ingham     else
2466cd41da7SJim Ingham         m_current_inlined_pc = m_thread.GetRegisterContext()->GetPC();
2476cd41da7SJim Ingham }
2486cd41da7SJim Ingham 
2496cd41da7SJim Ingham void
250b0c72a5fSJim Ingham StackFrameList::GetFramesUpTo(uint32_t end_idx)
25130fdc8d8SChris Lattner {
252ebafd2f1SEnrico Granata     // this makes sure we do not fetch frames for an invalid thread
253d70a6e71SEugene Zelenko     if (!m_thread.IsValid())
254ebafd2f1SEnrico Granata         return;
255ebafd2f1SEnrico Granata 
256b0c72a5fSJim Ingham     // We've already gotten more frames than asked for, or we've already finished unwinding, return.
257b0c72a5fSJim Ingham     if (m_frames.size() > end_idx || GetAllFramesFetched())
258b0c72a5fSJim Ingham         return;
25912daf946SGreg Clayton 
260b0c72a5fSJim Ingham     Unwind *unwinder = m_thread.GetUnwinder ();
261b0c72a5fSJim Ingham 
26212daf946SGreg Clayton     if (m_show_inlined_frames)
26312daf946SGreg Clayton     {
2645082c5fdSGreg Clayton #if defined (DEBUG_STACK_FRAMES)
2652c595273SJohnny Chen         StreamFile s(stdout, false);
2665082c5fdSGreg Clayton #endif
267513c6bb8SJim Ingham         // If we are hiding some frames from the outside world, we need to add those onto the total count of
268aaa0ba31SBruce Mitchener         // frames to fetch.  However, we don't need to do that if end_idx is 0 since in that case we always
2696cd41da7SJim Ingham         // get the first concrete frame and all the inlined frames below it...  And of course, if end_idx is
2706cd41da7SJim Ingham         // UINT32_MAX that means get all, so just do that...
271513c6bb8SJim Ingham 
272513c6bb8SJim Ingham         uint32_t inlined_depth = 0;
2736cd41da7SJim Ingham         if (end_idx > 0 && end_idx != UINT32_MAX)
274513c6bb8SJim Ingham         {
275513c6bb8SJim Ingham             inlined_depth = GetCurrentInlinedDepth();
276513c6bb8SJim Ingham             if (inlined_depth != UINT32_MAX)
277513c6bb8SJim Ingham             {
278513c6bb8SJim Ingham                 if (end_idx > 0)
279513c6bb8SJim Ingham                     end_idx += inlined_depth;
280513c6bb8SJim Ingham             }
281513c6bb8SJim Ingham         }
28212daf946SGreg Clayton 
283b57e4a1bSJason Molenda         StackFrameSP unwind_frame_sp;
284b0c72a5fSJim Ingham         do
28512daf946SGreg Clayton         {
286b0c72a5fSJim Ingham             uint32_t idx = m_concrete_frames_fetched++;
2878012cadbSGreg Clayton             lldb::addr_t pc = LLDB_INVALID_ADDRESS;
2888012cadbSGreg Clayton             lldb::addr_t cfa = LLDB_INVALID_ADDRESS;
28912daf946SGreg Clayton             if (idx == 0)
29012daf946SGreg Clayton             {
2915082c5fdSGreg Clayton                 // We might have already created frame zero, only create it
2925082c5fdSGreg Clayton                 // if we need to
2935082c5fdSGreg Clayton                 if (m_frames.empty())
2945082c5fdSGreg Clayton                 {
295b3ae8761SGreg Clayton                     RegisterContextSP reg_ctx_sp (m_thread.GetRegisterContext());
296b3ae8761SGreg Clayton 
297b3ae8761SGreg Clayton                     if (reg_ctx_sp)
298b3ae8761SGreg Clayton                     {
299ec6829eaSGreg Clayton                         const bool success = unwinder && unwinder->GetFrameInfoAtIndex(idx, cfa, pc);
300376c4854SJim Ingham                         // There shouldn't be any way not to get the frame info for frame 0.
301376c4854SJim Ingham                         // But if the unwinder can't make one, lets make one by hand with the
302376c4854SJim Ingham                         // SP as the CFA and see if that gets any further.
303376c4854SJim Ingham                         if (!success)
304376c4854SJim Ingham                         {
305b3ae8761SGreg Clayton                             cfa = reg_ctx_sp->GetSP();
306b3ae8761SGreg Clayton                             pc = reg_ctx_sp->GetPC();
307376c4854SJim Ingham                         }
308376c4854SJim Ingham 
309d9e416c0SGreg Clayton                         unwind_frame_sp.reset(new StackFrame(m_thread.shared_from_this(),
310d9e416c0SGreg Clayton                                                              m_frames.size(),
3115082c5fdSGreg Clayton                                                              idx,
312b3ae8761SGreg Clayton                                                              reg_ctx_sp,
31359e8fc1cSGreg Clayton                                                              cfa,
3141692b901SJim Ingham                                                              pc,
315d70a6e71SEugene Zelenko                                                              nullptr));
3165082c5fdSGreg Clayton                         m_frames.push_back (unwind_frame_sp);
3175082c5fdSGreg Clayton                     }
318b3ae8761SGreg Clayton                 }
3195082c5fdSGreg Clayton                 else
3205082c5fdSGreg Clayton                 {
3215082c5fdSGreg Clayton                     unwind_frame_sp = m_frames.front();
322b57e4a1bSJason Molenda                     cfa = unwind_frame_sp->m_id.GetCallFrameAddress();
3235082c5fdSGreg Clayton                 }
32412daf946SGreg Clayton             }
32512daf946SGreg Clayton             else
32612daf946SGreg Clayton             {
327ec6829eaSGreg Clayton                 const bool success = unwinder && unwinder->GetFrameInfoAtIndex(idx, cfa, pc);
328b0c72a5fSJim Ingham                 if (!success)
329b0c72a5fSJim Ingham                 {
330b0c72a5fSJim Ingham                     // We've gotten to the end of the stack.
331b0c72a5fSJim Ingham                     SetAllFramesFetched();
332b0c72a5fSJim Ingham                     break;
333b0c72a5fSJim Ingham                 }
33499618476SJason Molenda                 const bool cfa_is_valid = true;
33599618476SJason Molenda                 const bool stop_id_is_valid = false;
33699618476SJason Molenda                 const bool is_history_frame = false;
337d70a6e71SEugene Zelenko                 unwind_frame_sp.reset(new StackFrame(m_thread.shared_from_this(), m_frames.size(), idx, cfa, cfa_is_valid, pc,
338d70a6e71SEugene Zelenko                                                      0, stop_id_is_valid, is_history_frame, nullptr));
3395082c5fdSGreg Clayton                 m_frames.push_back (unwind_frame_sp);
34012daf946SGreg Clayton             }
34112daf946SGreg Clayton 
342ff1b5c42SEd Maste             assert(unwind_frame_sp);
3431ed54f50SGreg Clayton             SymbolContext unwind_sc = unwind_frame_sp->GetSymbolContext (eSymbolContextBlock | eSymbolContextFunction);
3441ed54f50SGreg Clayton             Block *unwind_block = unwind_sc.block;
34559e8fc1cSGreg Clayton             if (unwind_block)
34612daf946SGreg Clayton             {
3475f4c61e2SGreg Clayton                 Address curr_frame_address (unwind_frame_sp->GetFrameCodeAddress());
348911d5784STed Woodward                 TargetSP target_sp = m_thread.CalculateTarget();
3495f4c61e2SGreg Clayton                 // Be sure to adjust the frame address to match the address
3505f4c61e2SGreg Clayton                 // that was used to lookup the symbol context above. If we are
3515f4c61e2SGreg Clayton                 // in the first concrete frame, then we lookup using the current
3525f4c61e2SGreg Clayton                 // address, else we decrement the address by one to get the correct
3535f4c61e2SGreg Clayton                 // location.
3545f4c61e2SGreg Clayton                 if (idx > 0)
3550f8452baSTamas Berghammer                 {
3560f8452baSTamas Berghammer                     if (curr_frame_address.GetOffset() == 0)
3570f8452baSTamas Berghammer                     {
3580f8452baSTamas Berghammer                         // If curr_frame_address points to the first address in a section then after
3590f8452baSTamas Berghammer                         // adjustment it will point to an other section. In that case resolve the
3600f8452baSTamas Berghammer                         // address again to the correct section plus offset form.
361911d5784STed Woodward                         addr_t load_addr = curr_frame_address.GetOpcodeLoadAddress(target_sp.get(), eAddressClassCode);
362911d5784STed Woodward                         curr_frame_address.SetOpcodeLoadAddress(load_addr - 1, target_sp.get(), eAddressClassCode);
3630f8452baSTamas Berghammer                     }
3640f8452baSTamas Berghammer                     else
3650f8452baSTamas Berghammer                     {
3665f4c61e2SGreg Clayton                         curr_frame_address.Slide(-1);
3670f8452baSTamas Berghammer                     }
3680f8452baSTamas Berghammer                 }
3695f4c61e2SGreg Clayton 
3701ed54f50SGreg Clayton                 SymbolContext next_frame_sc;
3711ed54f50SGreg Clayton                 Address next_frame_address;
3721ed54f50SGreg Clayton 
3731ed54f50SGreg Clayton                 while (unwind_sc.GetParentOfInlinedScope(curr_frame_address, next_frame_sc, next_frame_address))
37459e8fc1cSGreg Clayton                 {
375911d5784STed Woodward                     next_frame_sc.line_entry.ApplyFileMappings(target_sp);
376b57e4a1bSJason Molenda                     StackFrameSP frame_sp(new StackFrame(m_thread.shared_from_this(),
377d9e416c0SGreg Clayton                                                          m_frames.size(),
3785082c5fdSGreg Clayton                                                          idx,
3795082c5fdSGreg Clayton                                                          unwind_frame_sp->GetRegisterContextSP (),
38059e8fc1cSGreg Clayton                                                          cfa,
3811ed54f50SGreg Clayton                                                          next_frame_address,
3821ed54f50SGreg Clayton                                                          &next_frame_sc));
3835082c5fdSGreg Clayton 
3845082c5fdSGreg Clayton                     m_frames.push_back (frame_sp);
3851ed54f50SGreg Clayton                     unwind_sc = next_frame_sc;
3861ed54f50SGreg Clayton                     curr_frame_address = next_frame_address;
387b0c72a5fSJim Ingham                 }
388b0c72a5fSJim Ingham             }
389b0c72a5fSJim Ingham         } while (m_frames.size() - 1 < end_idx);
3901ed54f50SGreg Clayton 
391b0c72a5fSJim Ingham         // Don't try to merge till you've calculated all the frames in this stack.
392b0c72a5fSJim Ingham         if (GetAllFramesFetched() && m_prev_frames_sp)
39312daf946SGreg Clayton         {
3942cad65a5SGreg Clayton             StackFrameList *prev_frames = m_prev_frames_sp.get();
3955082c5fdSGreg Clayton             StackFrameList *curr_frames = this;
3965082c5fdSGreg Clayton 
3976cd41da7SJim Ingham             //curr_frames->m_current_inlined_depth = prev_frames->m_current_inlined_depth;
3986cd41da7SJim Ingham             //curr_frames->m_current_inlined_pc = prev_frames->m_current_inlined_pc;
399d01b2953SDaniel Malea             //printf ("GetFramesUpTo: Copying current inlined depth: %d 0x%" PRIx64 ".\n", curr_frames->m_current_inlined_depth, curr_frames->m_current_inlined_pc);
4009786eeebSJim Ingham 
4015082c5fdSGreg Clayton #if defined (DEBUG_STACK_FRAMES)
40268275d5eSGreg Clayton             s.PutCString("\nprev_frames:\n");
4035082c5fdSGreg Clayton             prev_frames->Dump (&s);
40468275d5eSGreg Clayton             s.PutCString("\ncurr_frames:\n");
4055082c5fdSGreg Clayton             curr_frames->Dump (&s);
4065082c5fdSGreg Clayton             s.EOL();
4075082c5fdSGreg Clayton #endif
4085082c5fdSGreg Clayton             size_t curr_frame_num, prev_frame_num;
4095082c5fdSGreg Clayton 
4105082c5fdSGreg Clayton             for (curr_frame_num = curr_frames->m_frames.size(), prev_frame_num = prev_frames->m_frames.size();
4115082c5fdSGreg Clayton                  curr_frame_num > 0 && prev_frame_num > 0;
4125082c5fdSGreg Clayton                  --curr_frame_num, --prev_frame_num)
4135082c5fdSGreg Clayton             {
4145082c5fdSGreg Clayton                 const size_t curr_frame_idx = curr_frame_num-1;
4155082c5fdSGreg Clayton                 const size_t prev_frame_idx = prev_frame_num-1;
416b57e4a1bSJason Molenda                 StackFrameSP curr_frame_sp (curr_frames->m_frames[curr_frame_idx]);
417b57e4a1bSJason Molenda                 StackFrameSP prev_frame_sp (prev_frames->m_frames[prev_frame_idx]);
4185082c5fdSGreg Clayton 
4195082c5fdSGreg Clayton #if defined (DEBUG_STACK_FRAMES)
4202cad65a5SGreg Clayton                 s.Printf("\n\nCurr frame #%u ", curr_frame_idx);
4215082c5fdSGreg Clayton                 if (curr_frame_sp)
4222cad65a5SGreg Clayton                     curr_frame_sp->Dump (&s, true, false);
4235082c5fdSGreg Clayton                 else
4245082c5fdSGreg Clayton                     s.PutCString("NULL");
4252cad65a5SGreg Clayton                 s.Printf("\nPrev frame #%u ", prev_frame_idx);
4265082c5fdSGreg Clayton                 if (prev_frame_sp)
4272cad65a5SGreg Clayton                     prev_frame_sp->Dump (&s, true, false);
4285082c5fdSGreg Clayton                 else
4295082c5fdSGreg Clayton                     s.PutCString("NULL");
4305082c5fdSGreg Clayton #endif
4315082c5fdSGreg Clayton 
432b57e4a1bSJason Molenda                 StackFrame *curr_frame = curr_frame_sp.get();
433b57e4a1bSJason Molenda                 StackFrame *prev_frame = prev_frame_sp.get();
4345082c5fdSGreg Clayton 
435d70a6e71SEugene Zelenko                 if (curr_frame == nullptr || prev_frame == nullptr)
4365082c5fdSGreg Clayton                     break;
4375082c5fdSGreg Clayton 
43859e8fc1cSGreg Clayton                 // Check the stack ID to make sure they are equal
43959e8fc1cSGreg Clayton                 if (curr_frame->GetStackID() != prev_frame->GetStackID())
4405082c5fdSGreg Clayton                     break;
4415082c5fdSGreg Clayton 
44259e8fc1cSGreg Clayton                 prev_frame->UpdatePreviousFrameFromCurrentFrame (*curr_frame);
44359e8fc1cSGreg Clayton                 // Now copy the fixed up previous frame into the current frames
44459e8fc1cSGreg Clayton                 // so the pointer doesn't change
44559e8fc1cSGreg Clayton                 m_frames[curr_frame_idx] = prev_frame_sp;
44659e8fc1cSGreg Clayton                 //curr_frame->UpdateCurrentFrameFromPreviousFrame (*prev_frame);
4475082c5fdSGreg Clayton 
4485082c5fdSGreg Clayton #if defined (DEBUG_STACK_FRAMES)
44968275d5eSGreg Clayton                 s.Printf("\n    Copying previous frame to current frame");
4505082c5fdSGreg Clayton #endif
4515082c5fdSGreg Clayton             }
4525082c5fdSGreg Clayton             // We are done with the old stack frame list, we can release it now
4532cad65a5SGreg Clayton             m_prev_frames_sp.reset();
4545082c5fdSGreg Clayton         }
45568275d5eSGreg Clayton 
45668275d5eSGreg Clayton #if defined (DEBUG_STACK_FRAMES)
45768275d5eSGreg Clayton         s.PutCString("\n\nNew frames:\n");
45868275d5eSGreg Clayton         Dump (&s);
45968275d5eSGreg Clayton         s.EOL();
46068275d5eSGreg Clayton #endif
46112daf946SGreg Clayton     }
46212daf946SGreg Clayton     else
46312daf946SGreg Clayton     {
464b0c72a5fSJim Ingham         if (end_idx < m_concrete_frames_fetched)
465b0c72a5fSJim Ingham             return;
466b0c72a5fSJim Ingham 
467ec6829eaSGreg Clayton         if (unwinder)
468ec6829eaSGreg Clayton         {
469b0c72a5fSJim Ingham             uint32_t num_frames = unwinder->GetFramesUpTo(end_idx);
470b0c72a5fSJim Ingham             if (num_frames <= end_idx + 1)
471b0c72a5fSJim Ingham             {
472b0c72a5fSJim Ingham                 //Done unwinding.
473b0c72a5fSJim Ingham                 m_concrete_frames_fetched = UINT32_MAX;
474b0c72a5fSJim Ingham             }
475b0c72a5fSJim Ingham             m_frames.resize(num_frames);
47612daf946SGreg Clayton         }
4775082c5fdSGreg Clayton     }
478ec6829eaSGreg Clayton }
479b0c72a5fSJim Ingham 
480b0c72a5fSJim Ingham uint32_t
481b0c72a5fSJim Ingham StackFrameList::GetNumFrames (bool can_create)
482b0c72a5fSJim Ingham {
483*bb19a13cSSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(m_mutex);
484b0c72a5fSJim Ingham 
485b0c72a5fSJim Ingham     if (can_create)
486b0c72a5fSJim Ingham         GetFramesUpTo (UINT32_MAX);
487513c6bb8SJim Ingham 
488513c6bb8SJim Ingham     uint32_t inlined_depth = GetCurrentInlinedDepth();
489513c6bb8SJim Ingham     if (inlined_depth == UINT32_MAX)
4905082c5fdSGreg Clayton         return m_frames.size();
491513c6bb8SJim Ingham     else
492513c6bb8SJim Ingham         return m_frames.size() - inlined_depth;
49330fdc8d8SChris Lattner }
49430fdc8d8SChris Lattner 
4955082c5fdSGreg Clayton void
4965082c5fdSGreg Clayton StackFrameList::Dump (Stream *s)
49730fdc8d8SChris Lattner {
498d70a6e71SEugene Zelenko     if (s == nullptr)
4995082c5fdSGreg Clayton         return;
500*bb19a13cSSaleem Abdulrasool 
501*bb19a13cSSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(m_mutex);
50230fdc8d8SChris Lattner 
5035082c5fdSGreg Clayton     const_iterator pos, begin = m_frames.begin(), end = m_frames.end();
5045082c5fdSGreg Clayton     for (pos = begin; pos != end; ++pos)
50512daf946SGreg Clayton     {
506b57e4a1bSJason Molenda         StackFrame *frame = (*pos).get();
507324a1036SSaleem Abdulrasool         s->Printf("%p: ", static_cast<void*>(frame));
5085082c5fdSGreg Clayton         if (frame)
50959e8fc1cSGreg Clayton         {
51059e8fc1cSGreg Clayton             frame->GetStackID().Dump (s);
5110603aa9dSGreg Clayton             frame->DumpUsingSettingsFormat (s);
51259e8fc1cSGreg Clayton         }
5135082c5fdSGreg Clayton         else
514dce502edSGreg Clayton             s->Printf("frame #%u", (uint32_t)std::distance (begin, pos));
5155082c5fdSGreg Clayton         s->EOL();
51612daf946SGreg Clayton     }
5175082c5fdSGreg Clayton     s->EOL();
5185082c5fdSGreg Clayton }
51912daf946SGreg Clayton 
520b57e4a1bSJason Molenda StackFrameSP
52112daf946SGreg Clayton StackFrameList::GetFrameAtIndex (uint32_t idx)
52230fdc8d8SChris Lattner {
523b57e4a1bSJason Molenda     StackFrameSP frame_sp;
524*bb19a13cSSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(m_mutex);
5255cb9a184SJim Ingham     uint32_t original_idx = idx;
5265cb9a184SJim Ingham 
527513c6bb8SJim Ingham     uint32_t inlined_depth = GetCurrentInlinedDepth();
528513c6bb8SJim Ingham     if (inlined_depth != UINT32_MAX)
529513c6bb8SJim Ingham         idx += inlined_depth;
530513c6bb8SJim Ingham 
5315082c5fdSGreg Clayton     if (idx < m_frames.size())
5325082c5fdSGreg Clayton         frame_sp = m_frames[idx];
53312daf946SGreg Clayton 
5345082c5fdSGreg Clayton     if (frame_sp)
53512daf946SGreg Clayton         return frame_sp;
53612daf946SGreg Clayton 
5371692b901SJim Ingham     // GetFramesUpTo will fill m_frames with as many frames as you asked for,
5381692b901SJim Ingham     // if there are that many.  If there weren't then you asked for too many
5391692b901SJim Ingham     // frames.
540b0c72a5fSJim Ingham     GetFramesUpTo (idx);
541b0c72a5fSJim Ingham     if (idx < m_frames.size())
54212daf946SGreg Clayton     {
54312daf946SGreg Clayton         if (m_show_inlined_frames)
54412daf946SGreg Clayton         {
5451692b901SJim Ingham             // When inline frames are enabled we actually create all the frames in GetFramesUpTo.
5465082c5fdSGreg Clayton             frame_sp = m_frames[idx];
54712daf946SGreg Clayton         }
54812daf946SGreg Clayton         else
54912daf946SGreg Clayton         {
55012daf946SGreg Clayton             Unwind *unwinder = m_thread.GetUnwinder ();
55112daf946SGreg Clayton             if (unwinder)
55212daf946SGreg Clayton             {
55312daf946SGreg Clayton                 addr_t pc, cfa;
55412daf946SGreg Clayton                 if (unwinder->GetFrameInfoAtIndex(idx, cfa, pc))
5555082c5fdSGreg Clayton                 {
55699618476SJason Molenda                     const bool cfa_is_valid = true;
55799618476SJason Molenda                     const bool stop_id_is_valid = false;
55899618476SJason Molenda                     const bool is_history_frame = false;
559d70a6e71SEugene Zelenko                     frame_sp.reset(new StackFrame(m_thread.shared_from_this(), idx, idx, cfa, cfa_is_valid, pc, 0,
560d70a6e71SEugene Zelenko                                                   stop_id_is_valid, is_history_frame, nullptr));
56159e8fc1cSGreg Clayton 
56259e8fc1cSGreg Clayton                     Function *function = frame_sp->GetSymbolContext (eSymbolContextFunction).function;
56359e8fc1cSGreg Clayton                     if (function)
56459e8fc1cSGreg Clayton                     {
56559e8fc1cSGreg Clayton                         // When we aren't showing inline functions we always use
56659e8fc1cSGreg Clayton                         // the top most function block as the scope.
56759e8fc1cSGreg Clayton                         frame_sp->SetSymbolContextScope (&function->GetBlock(false));
56859e8fc1cSGreg Clayton                     }
56959e8fc1cSGreg Clayton                     else
57059e8fc1cSGreg Clayton                     {
571d70a6e71SEugene Zelenko                         // Set the symbol scope from the symbol regardless if it is nullptr or valid.
57259e8fc1cSGreg Clayton                         frame_sp->SetSymbolContextScope (frame_sp->GetSymbolContext (eSymbolContextSymbol).symbol);
57359e8fc1cSGreg Clayton                     }
5745082c5fdSGreg Clayton                     SetFrameAtIndex(idx, frame_sp);
57512daf946SGreg Clayton                 }
57612daf946SGreg Clayton             }
57712daf946SGreg Clayton         }
57830fdc8d8SChris Lattner     }
5795cb9a184SJim Ingham     else if (original_idx == 0)
5805cb9a184SJim Ingham     {
5815cb9a184SJim Ingham         // There should ALWAYS be a frame at index 0.  If something went wrong with the CurrentInlinedDepth such that
5825cb9a184SJim Ingham         // there weren't as many frames as we thought taking that into account, then reset the current inlined depth
5835cb9a184SJim Ingham         // and return the real zeroth frame.
584d70a6e71SEugene Zelenko         if (m_frames.empty())
5855cb9a184SJim Ingham         {
5865cb9a184SJim Ingham             // Why do we have a thread with zero frames, that should not ever happen...
5875cb9a184SJim Ingham             if (m_thread.IsValid())
5885cb9a184SJim Ingham                 assert ("A valid thread has no frames.");
589d70a6e71SEugene Zelenko         }
590d70a6e71SEugene Zelenko         else
591d70a6e71SEugene Zelenko         {
592d70a6e71SEugene Zelenko             ResetCurrentInlinedDepth();
593d70a6e71SEugene Zelenko             frame_sp = m_frames[original_idx];
5945cb9a184SJim Ingham         }
5955cb9a184SJim Ingham     }
5965cb9a184SJim Ingham 
59730fdc8d8SChris Lattner     return frame_sp;
59830fdc8d8SChris Lattner }
59930fdc8d8SChris Lattner 
600b57e4a1bSJason Molenda StackFrameSP
6015ccbd294SGreg Clayton StackFrameList::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx)
6025ccbd294SGreg Clayton {
6035ccbd294SGreg Clayton     // First try assuming the unwind index is the same as the frame index. The
6045ccbd294SGreg Clayton     // unwind index is always greater than or equal to the frame index, so it
6055ccbd294SGreg Clayton     // is a good place to start. If we have inlined frames we might have 5
6065ccbd294SGreg Clayton     // concrete frames (frame unwind indexes go from 0-4), but we might have 15
6075ccbd294SGreg Clayton     // frames after we make all the inlined frames. Most of the time the unwind
6085ccbd294SGreg Clayton     // frame index (or the concrete frame index) is the same as the frame index.
6095ccbd294SGreg Clayton     uint32_t frame_idx = unwind_idx;
610b57e4a1bSJason Molenda     StackFrameSP frame_sp (GetFrameAtIndex (frame_idx));
6115ccbd294SGreg Clayton     while (frame_sp)
6125ccbd294SGreg Clayton     {
6135ccbd294SGreg Clayton         if (frame_sp->GetFrameIndex() == unwind_idx)
6145ccbd294SGreg Clayton             break;
6155ccbd294SGreg Clayton         frame_sp = GetFrameAtIndex (++frame_idx);
6165ccbd294SGreg Clayton     }
6175ccbd294SGreg Clayton     return frame_sp;
6185ccbd294SGreg Clayton }
6195ccbd294SGreg Clayton 
6207bcb93d5SGreg Clayton static bool
621b57e4a1bSJason Molenda CompareStackID (const StackFrameSP &stack_sp, const StackID &stack_id)
6227bcb93d5SGreg Clayton {
6237bcb93d5SGreg Clayton     return stack_sp->GetStackID() < stack_id;
6247bcb93d5SGreg Clayton }
6257bcb93d5SGreg Clayton 
626b57e4a1bSJason Molenda StackFrameSP
627cc4d0146SGreg Clayton StackFrameList::GetFrameWithStackID (const StackID &stack_id)
6283a195b7eSJim Ingham {
629b57e4a1bSJason Molenda     StackFrameSP frame_sp;
6307bcb93d5SGreg Clayton 
6317bcb93d5SGreg Clayton     if (stack_id.IsValid())
6327bcb93d5SGreg Clayton     {
633*bb19a13cSSaleem Abdulrasool         std::lock_guard<std::recursive_mutex> guard(m_mutex);
6347bcb93d5SGreg Clayton         uint32_t frame_idx = 0;
6357bcb93d5SGreg Clayton         // Do a binary search in case the stack frame is already in our cache
6367bcb93d5SGreg Clayton         collection::const_iterator begin = m_frames.begin();
6377bcb93d5SGreg Clayton         collection::const_iterator end = m_frames.end();
6387bcb93d5SGreg Clayton         if (begin != end)
6397bcb93d5SGreg Clayton         {
6407bcb93d5SGreg Clayton             collection::const_iterator pos = std::lower_bound (begin, end, stack_id, CompareStackID);
6418012cadbSGreg Clayton             if (pos != end)
6428012cadbSGreg Clayton             {
6438012cadbSGreg Clayton                 if ((*pos)->GetStackID() == stack_id)
6447bcb93d5SGreg Clayton                     return *pos;
6458012cadbSGreg Clayton             }
6467bcb93d5SGreg Clayton 
6478012cadbSGreg Clayton //            if (m_frames.back()->GetStackID() < stack_id)
6488012cadbSGreg Clayton //                frame_idx = m_frames.size();
6497bcb93d5SGreg Clayton         }
6503a195b7eSJim Ingham         do
6513a195b7eSJim Ingham         {
6523a195b7eSJim Ingham             frame_sp = GetFrameAtIndex (frame_idx);
6533a195b7eSJim Ingham             if (frame_sp && frame_sp->GetStackID() == stack_id)
6543a195b7eSJim Ingham                 break;
6553a195b7eSJim Ingham             frame_idx++;
6563a195b7eSJim Ingham         }
6573a195b7eSJim Ingham         while (frame_sp);
6587bcb93d5SGreg Clayton     }
6593a195b7eSJim Ingham     return frame_sp;
6603a195b7eSJim Ingham }
6615ccbd294SGreg Clayton 
66212daf946SGreg Clayton bool
663b57e4a1bSJason Molenda StackFrameList::SetFrameAtIndex (uint32_t idx, StackFrameSP &frame_sp)
66412daf946SGreg Clayton {
6655082c5fdSGreg Clayton     if (idx >= m_frames.size())
6665082c5fdSGreg Clayton         m_frames.resize(idx + 1);
66712daf946SGreg Clayton     // Make sure allocation succeeded by checking bounds again
6685082c5fdSGreg Clayton     if (idx < m_frames.size())
66912daf946SGreg Clayton     {
6705082c5fdSGreg Clayton         m_frames[idx] = frame_sp;
67130fdc8d8SChris Lattner         return true;
67230fdc8d8SChris Lattner     }
67330fdc8d8SChris Lattner     return false;   // resize failed, out of memory?
67430fdc8d8SChris Lattner }
67530fdc8d8SChris Lattner 
67630fdc8d8SChris Lattner uint32_t
6772976d00aSJim Ingham StackFrameList::GetSelectedFrameIndex () const
67830fdc8d8SChris Lattner {
679*bb19a13cSSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(m_mutex);
6802976d00aSJim Ingham     return m_selected_frame_idx;
68130fdc8d8SChris Lattner }
68230fdc8d8SChris Lattner 
68330fdc8d8SChris Lattner uint32_t
684b57e4a1bSJason Molenda StackFrameList::SetSelectedFrame (lldb_private::StackFrame *frame)
68530fdc8d8SChris Lattner {
686*bb19a13cSSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(m_mutex);
68712daf946SGreg Clayton     const_iterator pos;
6885082c5fdSGreg Clayton     const_iterator begin = m_frames.begin();
6895082c5fdSGreg Clayton     const_iterator end = m_frames.end();
690b7f6b2faSJim Ingham     m_selected_frame_idx = 0;
69130fdc8d8SChris Lattner     for (pos = begin; pos != end; ++pos)
69230fdc8d8SChris Lattner     {
69330fdc8d8SChris Lattner         if (pos->get() == frame)
69430fdc8d8SChris Lattner         {
6952976d00aSJim Ingham             m_selected_frame_idx = std::distance (begin, pos);
696513c6bb8SJim Ingham             uint32_t inlined_depth = GetCurrentInlinedDepth();
697513c6bb8SJim Ingham             if (inlined_depth != UINT32_MAX)
698513c6bb8SJim Ingham                 m_selected_frame_idx -= inlined_depth;
699b7f6b2faSJim Ingham             break;
70030fdc8d8SChris Lattner         }
70130fdc8d8SChris Lattner     }
702b7f6b2faSJim Ingham     SetDefaultFileAndLineToSelectedFrame();
7032976d00aSJim Ingham     return m_selected_frame_idx;
70430fdc8d8SChris Lattner }
70530fdc8d8SChris Lattner 
70630fdc8d8SChris Lattner // Mark a stack frame as the current frame using the frame index
707b0c72a5fSJim Ingham bool
7082976d00aSJim Ingham StackFrameList::SetSelectedFrameByIndex (uint32_t idx)
70930fdc8d8SChris Lattner {
710*bb19a13cSSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(m_mutex);
711b57e4a1bSJason Molenda     StackFrameSP frame_sp (GetFrameAtIndex (idx));
712b0c72a5fSJim Ingham     if (frame_sp)
713b0c72a5fSJim Ingham     {
714b0c72a5fSJim Ingham         SetSelectedFrame(frame_sp.get());
715b0c72a5fSJim Ingham         return true;
716b0c72a5fSJim Ingham     }
717b0c72a5fSJim Ingham     else
718b0c72a5fSJim Ingham         return false;
719b7f6b2faSJim Ingham }
720b7f6b2faSJim Ingham 
721b7f6b2faSJim Ingham void
722b7f6b2faSJim Ingham StackFrameList::SetDefaultFileAndLineToSelectedFrame()
723b7f6b2faSJim Ingham {
7241ac04c30SGreg Clayton     if (m_thread.GetID() == m_thread.GetProcess()->GetThreadList().GetSelectedThread()->GetID())
725b7f6b2faSJim Ingham     {
726b57e4a1bSJason Molenda         StackFrameSP frame_sp (GetFrameAtIndex (GetSelectedFrameIndex()));
727b7f6b2faSJim Ingham         if (frame_sp)
728b7f6b2faSJim Ingham         {
729252d0edeSGreg Clayton             SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextLineEntry);
730b7f6b2faSJim Ingham             if (sc.line_entry.file)
7311ac04c30SGreg Clayton                 m_thread.CalculateTarget()->GetSourceManager().SetDefaultFileAndLine (sc.line_entry.file,
732252d0edeSGreg Clayton                                                                                             sc.line_entry.line);
733b7f6b2faSJim Ingham         }
734b7f6b2faSJim Ingham     }
73530fdc8d8SChris Lattner }
73630fdc8d8SChris Lattner 
73730fdc8d8SChris Lattner // The thread has been run, reset the number stack frames to zero so we can
73830fdc8d8SChris Lattner // determine how many frames we have lazily.
73930fdc8d8SChris Lattner void
74030fdc8d8SChris Lattner StackFrameList::Clear ()
74130fdc8d8SChris Lattner {
742*bb19a13cSSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(m_mutex);
7435082c5fdSGreg Clayton     m_frames.clear();
744b0c72a5fSJim Ingham     m_concrete_frames_fetched = 0;
74530fdc8d8SChris Lattner }
74630fdc8d8SChris Lattner 
74730fdc8d8SChris Lattner void
74830fdc8d8SChris Lattner StackFrameList::InvalidateFrames (uint32_t start_idx)
74930fdc8d8SChris Lattner {
750*bb19a13cSSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(m_mutex);
75112daf946SGreg Clayton     if (m_show_inlined_frames)
75212daf946SGreg Clayton     {
75312daf946SGreg Clayton         Clear();
75412daf946SGreg Clayton     }
75512daf946SGreg Clayton     else
75612daf946SGreg Clayton     {
7575082c5fdSGreg Clayton         const size_t num_frames = m_frames.size();
75830fdc8d8SChris Lattner         while (start_idx < num_frames)
75930fdc8d8SChris Lattner         {
7605082c5fdSGreg Clayton             m_frames[start_idx].reset();
76130fdc8d8SChris Lattner             ++start_idx;
76230fdc8d8SChris Lattner         }
76330fdc8d8SChris Lattner     }
76412daf946SGreg Clayton }
7652cad65a5SGreg Clayton 
7662cad65a5SGreg Clayton void
7677b0992d9SGreg Clayton StackFrameList::Merge (std::unique_ptr<StackFrameList>& curr_ap, lldb::StackFrameListSP& prev_sp)
7682cad65a5SGreg Clayton {
769*bb19a13cSSaleem Abdulrasool     std::unique_lock<std::recursive_mutex> current_lock, previous_lock;
770*bb19a13cSSaleem Abdulrasool     if (curr_ap)
771*bb19a13cSSaleem Abdulrasool         current_lock = std::unique_lock<std::recursive_mutex>(curr_ap->m_mutex);
772*bb19a13cSSaleem Abdulrasool     if (prev_sp)
773*bb19a13cSSaleem Abdulrasool         previous_lock = std::unique_lock<std::recursive_mutex>(prev_sp->m_mutex);
7742cad65a5SGreg Clayton 
7752cad65a5SGreg Clayton #if defined (DEBUG_STACK_FRAMES)
7762c595273SJohnny Chen     StreamFile s(stdout, false);
7772cad65a5SGreg Clayton     s.PutCString("\n\nStackFrameList::Merge():\nPrev:\n");
778d70a6e71SEugene Zelenko     if (prev_sp)
7792cad65a5SGreg Clayton         prev_sp->Dump (&s);
7802cad65a5SGreg Clayton     else
7812cad65a5SGreg Clayton         s.PutCString ("NULL");
7822cad65a5SGreg Clayton     s.PutCString("\nCurr:\n");
783d70a6e71SEugene Zelenko     if (curr_ap)
7842cad65a5SGreg Clayton         curr_ap->Dump (&s);
7852cad65a5SGreg Clayton     else
7862cad65a5SGreg Clayton         s.PutCString ("NULL");
7872cad65a5SGreg Clayton     s.EOL();
7882cad65a5SGreg Clayton #endif
7892cad65a5SGreg Clayton 
790d70a6e71SEugene Zelenko     if (!curr_ap || curr_ap->GetNumFrames(false) == 0)
7912cad65a5SGreg Clayton     {
7922cad65a5SGreg Clayton #if defined (DEBUG_STACK_FRAMES)
7932cad65a5SGreg Clayton         s.PutCString("No current frames, leave previous frames alone...\n");
7942cad65a5SGreg Clayton #endif
7952cad65a5SGreg Clayton         curr_ap.release();
7962cad65a5SGreg Clayton         return;
7972cad65a5SGreg Clayton     }
7982cad65a5SGreg Clayton 
799d70a6e71SEugene Zelenko     if (!prev_sp || prev_sp->GetNumFrames(false) == 0)
8002cad65a5SGreg Clayton     {
8012cad65a5SGreg Clayton #if defined (DEBUG_STACK_FRAMES)
8022cad65a5SGreg Clayton         s.PutCString("No previous frames, so use current frames...\n");
8032cad65a5SGreg Clayton #endif
8042cad65a5SGreg Clayton         // We either don't have any previous frames, or since we have more than
8052cad65a5SGreg Clayton         // one current frames it means we have all the frames and can safely
8062cad65a5SGreg Clayton         // replace our previous frames.
8072cad65a5SGreg Clayton         prev_sp.reset (curr_ap.release());
8082cad65a5SGreg Clayton         return;
8092cad65a5SGreg Clayton     }
8102cad65a5SGreg Clayton 
8112cad65a5SGreg Clayton     const uint32_t num_curr_frames = curr_ap->GetNumFrames (false);
8122cad65a5SGreg Clayton 
8132cad65a5SGreg Clayton     if (num_curr_frames > 1)
8142cad65a5SGreg Clayton     {
8152cad65a5SGreg Clayton #if defined (DEBUG_STACK_FRAMES)
8162cad65a5SGreg Clayton         s.PutCString("We have more than one current frame, so use current frames...\n");
8172cad65a5SGreg Clayton #endif
8182cad65a5SGreg Clayton         // We have more than one current frames it means we have all the frames
8192cad65a5SGreg Clayton         // and can safely replace our previous frames.
8202cad65a5SGreg Clayton         prev_sp.reset (curr_ap.release());
8212cad65a5SGreg Clayton 
8222cad65a5SGreg Clayton #if defined (DEBUG_STACK_FRAMES)
8232cad65a5SGreg Clayton         s.PutCString("\nMerged:\n");
8242cad65a5SGreg Clayton         prev_sp->Dump (&s);
8252cad65a5SGreg Clayton #endif
8262cad65a5SGreg Clayton         return;
8272cad65a5SGreg Clayton     }
8282cad65a5SGreg Clayton 
829b57e4a1bSJason Molenda     StackFrameSP prev_frame_zero_sp(prev_sp->GetFrameAtIndex (0));
830b57e4a1bSJason Molenda     StackFrameSP curr_frame_zero_sp(curr_ap->GetFrameAtIndex (0));
8312cad65a5SGreg Clayton     StackID curr_stack_id (curr_frame_zero_sp->GetStackID());
8322cad65a5SGreg Clayton     StackID prev_stack_id (prev_frame_zero_sp->GetStackID());
8332cad65a5SGreg Clayton 
8342cad65a5SGreg Clayton #if defined (DEBUG_STACK_FRAMES)
8352c595273SJohnny Chen     const uint32_t num_prev_frames = prev_sp->GetNumFrames (false);
8362cad65a5SGreg Clayton     s.Printf("\n%u previous frames with one current frame\n", num_prev_frames);
8372cad65a5SGreg Clayton #endif
8382cad65a5SGreg Clayton 
8392cad65a5SGreg Clayton     // We have only a single current frame
8402cad65a5SGreg Clayton     // Our previous stack frames only had a single frame as well...
8412cad65a5SGreg Clayton     if (curr_stack_id == prev_stack_id)
8422cad65a5SGreg Clayton     {
8432cad65a5SGreg Clayton #if defined (DEBUG_STACK_FRAMES)
8442cad65a5SGreg Clayton         s.Printf("\nPrevious frame #0 is same as current frame #0, merge the cached data\n");
8452cad65a5SGreg Clayton #endif
8462cad65a5SGreg Clayton 
8472cad65a5SGreg Clayton         curr_frame_zero_sp->UpdateCurrentFrameFromPreviousFrame (*prev_frame_zero_sp);
8482cad65a5SGreg Clayton //        prev_frame_zero_sp->UpdatePreviousFrameFromCurrentFrame (*curr_frame_zero_sp);
8492cad65a5SGreg Clayton //        prev_sp->SetFrameAtIndex (0, prev_frame_zero_sp);
8502cad65a5SGreg Clayton     }
8512cad65a5SGreg Clayton     else if (curr_stack_id < prev_stack_id)
8522cad65a5SGreg Clayton     {
8532cad65a5SGreg Clayton #if defined (DEBUG_STACK_FRAMES)
8542cad65a5SGreg 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");
8552cad65a5SGreg Clayton #endif
8562cad65a5SGreg Clayton         prev_sp->m_frames.insert (prev_sp->m_frames.begin(), curr_frame_zero_sp);
8572cad65a5SGreg Clayton     }
8582cad65a5SGreg Clayton 
8592cad65a5SGreg Clayton     curr_ap.release();
8602cad65a5SGreg Clayton 
8612cad65a5SGreg Clayton #if defined (DEBUG_STACK_FRAMES)
8622cad65a5SGreg Clayton     s.PutCString("\nMerged:\n");
8632cad65a5SGreg Clayton     prev_sp->Dump (&s);
8642cad65a5SGreg Clayton #endif
8652cad65a5SGreg Clayton }
866e4284b71SJim Ingham 
867b57e4a1bSJason Molenda lldb::StackFrameSP
868b57e4a1bSJason Molenda StackFrameList::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr)
869e4284b71SJim Ingham {
870e4284b71SJim Ingham     const_iterator pos;
871e4284b71SJim Ingham     const_iterator begin = m_frames.begin();
872e4284b71SJim Ingham     const_iterator end = m_frames.end();
873b57e4a1bSJason Molenda     lldb::StackFrameSP ret_sp;
874e4284b71SJim Ingham 
875e4284b71SJim Ingham     for (pos = begin; pos != end; ++pos)
876e4284b71SJim Ingham     {
877e4284b71SJim Ingham         if (pos->get() == stack_frame_ptr)
878e4284b71SJim Ingham         {
879e4284b71SJim Ingham             ret_sp = (*pos);
880e4284b71SJim Ingham             break;
881e4284b71SJim Ingham         }
882e4284b71SJim Ingham     }
883e4284b71SJim Ingham     return ret_sp;
884e4284b71SJim Ingham }
885e4284b71SJim Ingham 
8867260f620SGreg Clayton size_t
8877260f620SGreg Clayton StackFrameList::GetStatus (Stream& strm,
8887260f620SGreg Clayton                            uint32_t first_frame,
8897260f620SGreg Clayton                            uint32_t num_frames,
8907260f620SGreg Clayton                            bool show_frame_info,
8918ec10efcSJim Ingham                            uint32_t num_frames_with_source,
8928ec10efcSJim Ingham                            const char *selected_frame_marker)
8937260f620SGreg Clayton {
8947260f620SGreg Clayton     size_t num_frames_displayed = 0;
8957260f620SGreg Clayton 
8967260f620SGreg Clayton     if (num_frames == 0)
8977260f620SGreg Clayton         return 0;
8987260f620SGreg Clayton 
899b57e4a1bSJason Molenda     StackFrameSP frame_sp;
9007260f620SGreg Clayton     uint32_t frame_idx = 0;
9017260f620SGreg Clayton     uint32_t last_frame;
9027260f620SGreg Clayton 
9037260f620SGreg Clayton     // Don't let the last frame wrap around...
9047260f620SGreg Clayton     if (num_frames == UINT32_MAX)
9057260f620SGreg Clayton         last_frame = UINT32_MAX;
9067260f620SGreg Clayton     else
9077260f620SGreg Clayton         last_frame = first_frame + num_frames;
9087260f620SGreg Clayton 
909b57e4a1bSJason Molenda     StackFrameSP selected_frame_sp = m_thread.GetSelectedFrame();
910d70a6e71SEugene Zelenko     const char *unselected_marker = nullptr;
9118ec10efcSJim Ingham     std::string buffer;
9128ec10efcSJim Ingham     if (selected_frame_marker)
9138ec10efcSJim Ingham     {
9148ec10efcSJim Ingham         size_t len = strlen(selected_frame_marker);
9158ec10efcSJim Ingham         buffer.insert(buffer.begin(), len, ' ');
9168ec10efcSJim Ingham         unselected_marker = buffer.c_str();
9178ec10efcSJim Ingham     }
918d70a6e71SEugene Zelenko     const char *marker = nullptr;
9198ec10efcSJim Ingham 
9207260f620SGreg Clayton     for (frame_idx = first_frame; frame_idx < last_frame; ++frame_idx)
9217260f620SGreg Clayton     {
9227260f620SGreg Clayton         frame_sp = GetFrameAtIndex(frame_idx);
923d70a6e71SEugene Zelenko         if (!frame_sp)
9247260f620SGreg Clayton             break;
9257260f620SGreg Clayton 
926d70a6e71SEugene Zelenko         if (selected_frame_marker != nullptr)
9278ec10efcSJim Ingham         {
9288ec10efcSJim Ingham             if (frame_sp == selected_frame_sp)
9298ec10efcSJim Ingham                 marker = selected_frame_marker;
9308ec10efcSJim Ingham             else
9318ec10efcSJim Ingham                 marker = unselected_marker;
9328ec10efcSJim Ingham         }
9338ec10efcSJim Ingham 
9347260f620SGreg Clayton         if (!frame_sp->GetStatus (strm,
9357260f620SGreg Clayton                                   show_frame_info,
9368ec10efcSJim Ingham                                   num_frames_with_source > (first_frame - frame_idx), marker))
9377260f620SGreg Clayton             break;
9387260f620SGreg Clayton         ++num_frames_displayed;
9397260f620SGreg Clayton     }
9407260f620SGreg Clayton 
9417260f620SGreg Clayton     strm.IndentLess();
9427260f620SGreg Clayton     return num_frames_displayed;
9437260f620SGreg Clayton }
944