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/Breakpoint.h"
16b9c1b51eSKate Stone #include "lldb/Breakpoint/BreakpointLocation.h"
17b7f6b2faSJim Ingham #include "lldb/Core/SourceManager.h"
18b9c1b51eSKate Stone #include "lldb/Core/StreamFile.h"
1912daf946SGreg Clayton #include "lldb/Symbol/Block.h"
2012daf946SGreg Clayton #include "lldb/Symbol/Function.h"
2159e8fc1cSGreg Clayton #include "lldb/Symbol/Symbol.h"
22b7f6b2faSJim Ingham #include "lldb/Target/Process.h"
2312daf946SGreg Clayton #include "lldb/Target/RegisterContext.h"
2430fdc8d8SChris Lattner #include "lldb/Target/StackFrame.h"
25513c6bb8SJim Ingham #include "lldb/Target/StopInfo.h"
26b7f6b2faSJim Ingham #include "lldb/Target/Target.h"
2712daf946SGreg Clayton #include "lldb/Target/Thread.h"
2812daf946SGreg Clayton #include "lldb/Target/Unwind.h"
296f9e6901SZachary Turner #include "lldb/Utility/Log.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 //----------------------------------------------------------------------
39b9c1b51eSKate Stone StackFrameList::StackFrameList(Thread &thread,
40b9c1b51eSKate Stone                                const lldb::StackFrameListSP &prev_frames_sp,
41b9c1b51eSKate Stone                                bool show_inline_frames)
42b9c1b51eSKate Stone     : m_thread(thread), m_prev_frames_sp(prev_frames_sp), m_mutex(), m_frames(),
43b9c1b51eSKate Stone       m_selected_frame_idx(0), m_concrete_frames_fetched(0),
44513c6bb8SJim Ingham       m_current_inlined_depth(UINT32_MAX),
45513c6bb8SJim Ingham       m_current_inlined_pc(LLDB_INVALID_ADDRESS),
46b9c1b51eSKate Stone       m_show_inlined_frames(show_inline_frames) {
47b9c1b51eSKate Stone   if (prev_frames_sp) {
48513c6bb8SJim Ingham     m_current_inlined_depth = prev_frames_sp->m_current_inlined_depth;
49513c6bb8SJim Ingham     m_current_inlined_pc = prev_frames_sp->m_current_inlined_pc;
50513c6bb8SJim Ingham   }
5130fdc8d8SChris Lattner }
5230fdc8d8SChris Lattner 
53b9c1b51eSKate Stone StackFrameList::~StackFrameList() {
5405097246SAdrian Prantl   // Call clear since this takes a lock and clears the stack frame list in case
5505097246SAdrian Prantl   // another thread is currently using this stack frame list
56ca5ce187SGreg Clayton   Clear();
5730fdc8d8SChris Lattner }
5830fdc8d8SChris Lattner 
59b9c1b51eSKate Stone void StackFrameList::CalculateCurrentInlinedDepth() {
60513c6bb8SJim Ingham   uint32_t cur_inlined_depth = GetCurrentInlinedDepth();
61b9c1b51eSKate Stone   if (cur_inlined_depth == UINT32_MAX) {
62513c6bb8SJim Ingham     ResetCurrentInlinedDepth();
63513c6bb8SJim Ingham   }
64513c6bb8SJim Ingham }
65513c6bb8SJim Ingham 
66b9c1b51eSKate Stone uint32_t StackFrameList::GetCurrentInlinedDepth() {
67b9c1b51eSKate Stone   if (m_show_inlined_frames && m_current_inlined_pc != LLDB_INVALID_ADDRESS) {
68513c6bb8SJim Ingham     lldb::addr_t cur_pc = m_thread.GetRegisterContext()->GetPC();
69b9c1b51eSKate Stone     if (cur_pc != m_current_inlined_pc) {
70513c6bb8SJim Ingham       m_current_inlined_pc = LLDB_INVALID_ADDRESS;
71513c6bb8SJim Ingham       m_current_inlined_depth = UINT32_MAX;
725160ce5cSGreg Clayton       Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
736cd41da7SJim Ingham       if (log && log->GetVerbose())
74b9c1b51eSKate Stone         log->Printf(
75b9c1b51eSKate Stone             "GetCurrentInlinedDepth: invalidating current inlined depth.\n");
76513c6bb8SJim Ingham     }
77513c6bb8SJim Ingham     return m_current_inlined_depth;
78b9c1b51eSKate Stone   } else {
79513c6bb8SJim Ingham     return UINT32_MAX;
80513c6bb8SJim Ingham   }
81513c6bb8SJim Ingham }
82513c6bb8SJim Ingham 
83b9c1b51eSKate Stone void StackFrameList::ResetCurrentInlinedDepth() {
84e7167e03SVedant Kumar   if (!m_show_inlined_frames)
85e7167e03SVedant Kumar     return;
86e7167e03SVedant Kumar 
87bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
8847db4a2cSKeno Fischer 
89513c6bb8SJim Ingham   GetFramesUpTo(0);
90d70a6e71SEugene Zelenko   if (m_frames.empty())
9165d4d5c3SRyan Brown     return;
92b9c1b51eSKate Stone   if (!m_frames[0]->IsInlined()) {
93513c6bb8SJim Ingham     m_current_inlined_depth = UINT32_MAX;
94513c6bb8SJim Ingham     m_current_inlined_pc = LLDB_INVALID_ADDRESS;
955160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
966cd41da7SJim Ingham     if (log && log->GetVerbose())
97b9c1b51eSKate Stone       log->Printf(
98b9c1b51eSKate Stone           "ResetCurrentInlinedDepth: Invalidating current inlined depth.\n");
99e7167e03SVedant Kumar     return;
100e7167e03SVedant Kumar   }
101e7167e03SVedant Kumar 
10205097246SAdrian Prantl   // We only need to do something special about inlined blocks when we are
10305097246SAdrian Prantl   // at the beginning of an inlined function:
104b9c1b51eSKate Stone   // FIXME: We probably also have to do something special if the PC is at
105e7167e03SVedant Kumar   // the END of an inlined function, which coincides with the end of either
106e7167e03SVedant Kumar   // its containing function or another inlined function.
107513c6bb8SJim Ingham 
108513c6bb8SJim Ingham   Block *block_ptr = m_frames[0]->GetFrameBlock();
109e7167e03SVedant Kumar   if (!block_ptr)
110e7167e03SVedant Kumar     return;
111e7167e03SVedant Kumar 
112513c6bb8SJim Ingham   Address pc_as_address;
113e7167e03SVedant Kumar   lldb::addr_t curr_pc = m_thread.GetRegisterContext()->GetPC();
114e7167e03SVedant Kumar   pc_as_address.SetLoadAddress(curr_pc, &(m_thread.GetProcess()->GetTarget()));
115513c6bb8SJim Ingham   AddressRange containing_range;
116e7167e03SVedant Kumar   if (!block_ptr->GetRangeContainingAddress(pc_as_address, containing_range) ||
117e7167e03SVedant Kumar       pc_as_address != containing_range.GetBaseAddress())
118e7167e03SVedant Kumar     return;
119e7167e03SVedant Kumar 
120e7167e03SVedant Kumar   // If we got here because of a breakpoint hit, then set the inlined depth
121e7167e03SVedant Kumar   // depending on where the breakpoint was set. If we got here because of a
122e7167e03SVedant Kumar   // crash, then set the inlined depth to the deepest most block.  Otherwise,
123e7167e03SVedant Kumar   // we stopped here naturally as the result of a step, so set ourselves in the
124e7167e03SVedant Kumar   // containing frame of the whole set of nested inlines, so the user can then
125e7167e03SVedant Kumar   // "virtually" step into the frames one by one, or next over the whole mess.
126e7167e03SVedant Kumar   // Note: We don't have to handle being somewhere in the middle of the stack
127e7167e03SVedant Kumar   // here, since ResetCurrentInlinedDepth doesn't get called if there is a
128e7167e03SVedant Kumar   // valid inlined depth set.
129513c6bb8SJim Ingham   StopInfoSP stop_info_sp = m_thread.GetStopInfo();
130e7167e03SVedant Kumar   if (!stop_info_sp)
131e7167e03SVedant Kumar     return;
132b9c1b51eSKate Stone   switch (stop_info_sp->GetStopReason()) {
133513c6bb8SJim Ingham   case eStopReasonWatchpoint:
134513c6bb8SJim Ingham   case eStopReasonException:
13590ba8115SGreg Clayton   case eStopReasonExec:
136513c6bb8SJim Ingham   case eStopReasonSignal:
137e7167e03SVedant Kumar     // In all these cases we want to stop in the deepest frame.
138513c6bb8SJim Ingham     m_current_inlined_pc = curr_pc;
139513c6bb8SJim Ingham     m_current_inlined_depth = 0;
140513c6bb8SJim Ingham     break;
141b9c1b51eSKate Stone   case eStopReasonBreakpoint: {
142e7167e03SVedant Kumar     // FIXME: Figure out what this break point is doing, and set the inline
143e7167e03SVedant Kumar     // depth appropriately.  Be careful to take into account breakpoints that
144e7167e03SVedant Kumar     // implement step over prologue, since that should do the default
145e7167e03SVedant Kumar     // calculation. For now, if the breakpoints corresponding to this hit are
146e7167e03SVedant Kumar     // all internal, I set the stop location to the top of the inlined stack,
147e7167e03SVedant Kumar     // since that will make things like stepping over prologues work right.
148e7167e03SVedant Kumar     // But if there are any non-internal breakpoints I do to the bottom of the
149e7167e03SVedant Kumar     // stack, since that was the old behavior.
150c635500dSJim Ingham     uint32_t bp_site_id = stop_info_sp->GetValue();
151b9c1b51eSKate Stone     BreakpointSiteSP bp_site_sp(
152e7167e03SVedant Kumar         m_thread.GetProcess()->GetBreakpointSiteList().FindByID(bp_site_id));
153c635500dSJim Ingham     bool all_internal = true;
154b9c1b51eSKate Stone     if (bp_site_sp) {
155c635500dSJim Ingham       uint32_t num_owners = bp_site_sp->GetNumberOfOwners();
156b9c1b51eSKate Stone       for (uint32_t i = 0; i < num_owners; i++) {
157e7167e03SVedant Kumar         Breakpoint &bp_ref = bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint();
158b9c1b51eSKate Stone         if (!bp_ref.IsInternal()) {
159c635500dSJim Ingham           all_internal = false;
160c635500dSJim Ingham         }
161c635500dSJim Ingham       }
162c635500dSJim Ingham     }
163b9c1b51eSKate Stone     if (!all_internal) {
164c635500dSJim Ingham       m_current_inlined_pc = curr_pc;
165c635500dSJim Ingham       m_current_inlined_depth = 0;
166c635500dSJim Ingham       break;
167c635500dSJim Ingham     }
1687da851a3SJim Ingham   }
169cec91ef9SGreg Clayton     LLVM_FALLTHROUGH;
170b9c1b51eSKate Stone   default: {
171e7167e03SVedant Kumar     // Otherwise, we should set ourselves at the container of the inlining, so
172e7167e03SVedant Kumar     // that the user can descend into them. So first we check whether we have
173e7167e03SVedant Kumar     // more than one inlined block sharing this PC:
174513c6bb8SJim Ingham     int num_inlined_functions = 0;
175513c6bb8SJim Ingham 
176513c6bb8SJim Ingham     for (Block *container_ptr = block_ptr->GetInlinedParent();
177d70a6e71SEugene Zelenko          container_ptr != nullptr;
178b9c1b51eSKate Stone          container_ptr = container_ptr->GetInlinedParent()) {
179e7167e03SVedant Kumar       if (!container_ptr->GetRangeContainingAddress(pc_as_address,
180e7167e03SVedant Kumar                                                     containing_range))
181513c6bb8SJim Ingham         break;
182513c6bb8SJim Ingham       if (pc_as_address != containing_range.GetBaseAddress())
183513c6bb8SJim Ingham         break;
184513c6bb8SJim Ingham 
185513c6bb8SJim Ingham       num_inlined_functions++;
186513c6bb8SJim Ingham     }
187513c6bb8SJim Ingham     m_current_inlined_pc = curr_pc;
188513c6bb8SJim Ingham     m_current_inlined_depth = num_inlined_functions + 1;
189e7167e03SVedant Kumar     Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
1906cd41da7SJim Ingham     if (log && log->GetVerbose())
191b9c1b51eSKate Stone       log->Printf("ResetCurrentInlinedDepth: setting inlined "
192b9c1b51eSKate Stone                   "depth: %d 0x%" PRIx64 ".\n",
193b9c1b51eSKate Stone                   m_current_inlined_depth, curr_pc);
194513c6bb8SJim Ingham 
195e7167e03SVedant Kumar     break;
196513c6bb8SJim Ingham   }
197513c6bb8SJim Ingham   }
198513c6bb8SJim Ingham }
199513c6bb8SJim Ingham 
200b9c1b51eSKate Stone bool StackFrameList::DecrementCurrentInlinedDepth() {
201b9c1b51eSKate Stone   if (m_show_inlined_frames) {
202513c6bb8SJim Ingham     uint32_t current_inlined_depth = GetCurrentInlinedDepth();
203b9c1b51eSKate Stone     if (current_inlined_depth != UINT32_MAX) {
204b9c1b51eSKate Stone       if (current_inlined_depth > 0) {
205513c6bb8SJim Ingham         m_current_inlined_depth--;
206513c6bb8SJim Ingham         return true;
207513c6bb8SJim Ingham       }
208513c6bb8SJim Ingham     }
2099786eeebSJim Ingham   }
210513c6bb8SJim Ingham   return false;
211513c6bb8SJim Ingham }
212513c6bb8SJim Ingham 
213b9c1b51eSKate Stone void StackFrameList::SetCurrentInlinedDepth(uint32_t new_depth) {
2146cd41da7SJim Ingham   m_current_inlined_depth = new_depth;
2156cd41da7SJim Ingham   if (new_depth == UINT32_MAX)
2166cd41da7SJim Ingham     m_current_inlined_pc = LLDB_INVALID_ADDRESS;
2176cd41da7SJim Ingham   else
2186cd41da7SJim Ingham     m_current_inlined_pc = m_thread.GetRegisterContext()->GetPC();
2196cd41da7SJim Ingham }
2206cd41da7SJim Ingham 
221eb8fa58eSVedant Kumar void StackFrameList::GetOnlyConcreteFramesUpTo(uint32_t end_idx,
222eb8fa58eSVedant Kumar                                                Unwind *unwinder) {
223eb8fa58eSVedant Kumar   assert(m_thread.IsValid() && "Expected valid thread");
224eb8fa58eSVedant Kumar   assert(m_frames.size() <= end_idx && "Expected there to be frames to fill");
225eb8fa58eSVedant Kumar 
226eb8fa58eSVedant Kumar   if (end_idx < m_concrete_frames_fetched)
227eb8fa58eSVedant Kumar     return;
228eb8fa58eSVedant Kumar 
229eb8fa58eSVedant Kumar   if (!unwinder)
230eb8fa58eSVedant Kumar     return;
231eb8fa58eSVedant Kumar 
232eb8fa58eSVedant Kumar   uint32_t num_frames = unwinder->GetFramesUpTo(end_idx);
233eb8fa58eSVedant Kumar   if (num_frames <= end_idx + 1) {
234eb8fa58eSVedant Kumar     // Done unwinding.
235eb8fa58eSVedant Kumar     m_concrete_frames_fetched = UINT32_MAX;
236eb8fa58eSVedant Kumar   }
237eb8fa58eSVedant Kumar   m_frames.resize(num_frames);
238eb8fa58eSVedant Kumar }
239eb8fa58eSVedant Kumar 
240b9c1b51eSKate Stone void StackFrameList::GetFramesUpTo(uint32_t end_idx) {
241eb8fa58eSVedant Kumar   // Do not fetch frames for an invalid thread.
242d70a6e71SEugene Zelenko   if (!m_thread.IsValid())
243ebafd2f1SEnrico Granata     return;
244ebafd2f1SEnrico Granata 
245b9c1b51eSKate Stone   // We've already gotten more frames than asked for, or we've already finished
246b9c1b51eSKate Stone   // unwinding, return.
247b0c72a5fSJim Ingham   if (m_frames.size() > end_idx || GetAllFramesFetched())
248b0c72a5fSJim Ingham     return;
24912daf946SGreg Clayton 
250b0c72a5fSJim Ingham   Unwind *unwinder = m_thread.GetUnwinder();
251b0c72a5fSJim Ingham 
252eb8fa58eSVedant Kumar   if (!m_show_inlined_frames) {
253eb8fa58eSVedant Kumar     GetOnlyConcreteFramesUpTo(end_idx, unwinder);
254eb8fa58eSVedant Kumar     return;
255eb8fa58eSVedant Kumar   }
256eb8fa58eSVedant Kumar 
2575082c5fdSGreg Clayton #if defined(DEBUG_STACK_FRAMES)
2582c595273SJohnny Chen   StreamFile s(stdout, false);
2595082c5fdSGreg Clayton #endif
26005097246SAdrian Prantl   // If we are hiding some frames from the outside world, we need to add
26105097246SAdrian Prantl   // those onto the total count of frames to fetch.  However, we don't need
26205097246SAdrian Prantl   // to do that if end_idx is 0 since in that case we always get the first
26305097246SAdrian Prantl   // concrete frame and all the inlined frames below it...  And of course, if
26405097246SAdrian Prantl   // end_idx is UINT32_MAX that means get all, so just do that...
265513c6bb8SJim Ingham 
266513c6bb8SJim Ingham   uint32_t inlined_depth = 0;
267b9c1b51eSKate Stone   if (end_idx > 0 && end_idx != UINT32_MAX) {
268513c6bb8SJim Ingham     inlined_depth = GetCurrentInlinedDepth();
269b9c1b51eSKate Stone     if (inlined_depth != UINT32_MAX) {
270513c6bb8SJim Ingham       if (end_idx > 0)
271513c6bb8SJim Ingham         end_idx += inlined_depth;
272513c6bb8SJim Ingham     }
273513c6bb8SJim Ingham   }
27412daf946SGreg Clayton 
275b57e4a1bSJason Molenda   StackFrameSP unwind_frame_sp;
276b9c1b51eSKate Stone   do {
277b0c72a5fSJim Ingham     uint32_t idx = m_concrete_frames_fetched++;
2788012cadbSGreg Clayton     lldb::addr_t pc = LLDB_INVALID_ADDRESS;
2798012cadbSGreg Clayton     lldb::addr_t cfa = LLDB_INVALID_ADDRESS;
280b9c1b51eSKate Stone     if (idx == 0) {
28105097246SAdrian Prantl       // We might have already created frame zero, only create it if we need
282eb8fa58eSVedant Kumar       // to.
283b9c1b51eSKate Stone       if (m_frames.empty()) {
284b3ae8761SGreg Clayton         RegisterContextSP reg_ctx_sp(m_thread.GetRegisterContext());
285b3ae8761SGreg Clayton 
286b9c1b51eSKate Stone         if (reg_ctx_sp) {
287b9c1b51eSKate Stone           const bool success =
288b9c1b51eSKate Stone               unwinder && unwinder->GetFrameInfoAtIndex(idx, cfa, pc);
28905097246SAdrian Prantl           // There shouldn't be any way not to get the frame info for frame
29005097246SAdrian Prantl           // 0. But if the unwinder can't make one, lets make one by hand
291eb8fa58eSVedant Kumar           // with the SP as the CFA and see if that gets any further.
292b9c1b51eSKate Stone           if (!success) {
293b3ae8761SGreg Clayton             cfa = reg_ctx_sp->GetSP();
294b3ae8761SGreg Clayton             pc = reg_ctx_sp->GetPC();
295376c4854SJim Ingham           }
296376c4854SJim Ingham 
297d9e416c0SGreg Clayton           unwind_frame_sp.reset(new StackFrame(m_thread.shared_from_this(),
298eb8fa58eSVedant Kumar                                                m_frames.size(), idx, reg_ctx_sp,
299eb8fa58eSVedant Kumar                                                cfa, pc, nullptr));
3005082c5fdSGreg Clayton           m_frames.push_back(unwind_frame_sp);
3015082c5fdSGreg Clayton         }
302b9c1b51eSKate Stone       } else {
3035082c5fdSGreg Clayton         unwind_frame_sp = m_frames.front();
304b57e4a1bSJason Molenda         cfa = unwind_frame_sp->m_id.GetCallFrameAddress();
3055082c5fdSGreg Clayton       }
306b9c1b51eSKate Stone     } else {
307b9c1b51eSKate Stone       const bool success =
308b9c1b51eSKate Stone           unwinder && unwinder->GetFrameInfoAtIndex(idx, cfa, pc);
309b9c1b51eSKate Stone       if (!success) {
310b0c72a5fSJim Ingham         // We've gotten to the end of the stack.
311b0c72a5fSJim Ingham         SetAllFramesFetched();
312b0c72a5fSJim Ingham         break;
313b0c72a5fSJim Ingham       }
31499618476SJason Molenda       const bool cfa_is_valid = true;
31599618476SJason Molenda       const bool stop_id_is_valid = false;
31699618476SJason Molenda       const bool is_history_frame = false;
317b9c1b51eSKate Stone       unwind_frame_sp.reset(new StackFrame(
318eb8fa58eSVedant Kumar           m_thread.shared_from_this(), m_frames.size(), idx, cfa, cfa_is_valid,
319eb8fa58eSVedant Kumar           pc, 0, stop_id_is_valid, is_history_frame, nullptr));
3205082c5fdSGreg Clayton       m_frames.push_back(unwind_frame_sp);
32112daf946SGreg Clayton     }
32212daf946SGreg Clayton 
323ff1b5c42SEd Maste     assert(unwind_frame_sp);
324b9c1b51eSKate Stone     SymbolContext unwind_sc = unwind_frame_sp->GetSymbolContext(
325b9c1b51eSKate Stone         eSymbolContextBlock | eSymbolContextFunction);
3261ed54f50SGreg Clayton     Block *unwind_block = unwind_sc.block;
327b9c1b51eSKate Stone     if (unwind_block) {
3285f4c61e2SGreg Clayton       Address curr_frame_address(unwind_frame_sp->GetFrameCodeAddress());
329911d5784STed Woodward       TargetSP target_sp = m_thread.CalculateTarget();
33005097246SAdrian Prantl       // Be sure to adjust the frame address to match the address that was
33105097246SAdrian Prantl       // used to lookup the symbol context above. If we are in the first
33205097246SAdrian Prantl       // concrete frame, then we lookup using the current address, else we
33305097246SAdrian Prantl       // decrement the address by one to get the correct location.
334b9c1b51eSKate Stone       if (idx > 0) {
335b9c1b51eSKate Stone         if (curr_frame_address.GetOffset() == 0) {
336b9c1b51eSKate Stone           // If curr_frame_address points to the first address in a section
33705097246SAdrian Prantl           // then after adjustment it will point to an other section. In that
33805097246SAdrian Prantl           // case resolve the address again to the correct section plus
33905097246SAdrian Prantl           // offset form.
340b9c1b51eSKate Stone           addr_t load_addr = curr_frame_address.GetOpcodeLoadAddress(
34104803b3eSTatyana Krasnukha               target_sp.get(), AddressClass::eCode);
342b9c1b51eSKate Stone           curr_frame_address.SetOpcodeLoadAddress(
34304803b3eSTatyana Krasnukha               load_addr - 1, target_sp.get(), AddressClass::eCode);
344b9c1b51eSKate Stone         } else {
3455f4c61e2SGreg Clayton           curr_frame_address.Slide(-1);
3460f8452baSTamas Berghammer         }
3470f8452baSTamas Berghammer       }
3485f4c61e2SGreg Clayton 
3491ed54f50SGreg Clayton       SymbolContext next_frame_sc;
3501ed54f50SGreg Clayton       Address next_frame_address;
3511ed54f50SGreg Clayton 
352b9c1b51eSKate Stone       while (unwind_sc.GetParentOfInlinedScope(
353b9c1b51eSKate Stone           curr_frame_address, next_frame_sc, next_frame_address)) {
354911d5784STed Woodward         next_frame_sc.line_entry.ApplyFileMappings(target_sp);
355b9c1b51eSKate Stone         StackFrameSP frame_sp(
356b9c1b51eSKate Stone             new StackFrame(m_thread.shared_from_this(), m_frames.size(), idx,
357b9c1b51eSKate Stone                            unwind_frame_sp->GetRegisterContextSP(), cfa,
358b9c1b51eSKate Stone                            next_frame_address, &next_frame_sc));
3595082c5fdSGreg Clayton 
3605082c5fdSGreg Clayton         m_frames.push_back(frame_sp);
3611ed54f50SGreg Clayton         unwind_sc = next_frame_sc;
3621ed54f50SGreg Clayton         curr_frame_address = next_frame_address;
363b0c72a5fSJim Ingham       }
364b0c72a5fSJim Ingham     }
365b0c72a5fSJim Ingham   } while (m_frames.size() - 1 < end_idx);
3661ed54f50SGreg Clayton 
367b0c72a5fSJim Ingham   // Don't try to merge till you've calculated all the frames in this stack.
368b9c1b51eSKate Stone   if (GetAllFramesFetched() && m_prev_frames_sp) {
3692cad65a5SGreg Clayton     StackFrameList *prev_frames = m_prev_frames_sp.get();
3705082c5fdSGreg Clayton     StackFrameList *curr_frames = this;
3715082c5fdSGreg Clayton 
3725082c5fdSGreg Clayton #if defined(DEBUG_STACK_FRAMES)
37368275d5eSGreg Clayton     s.PutCString("\nprev_frames:\n");
3745082c5fdSGreg Clayton     prev_frames->Dump(&s);
37568275d5eSGreg Clayton     s.PutCString("\ncurr_frames:\n");
3765082c5fdSGreg Clayton     curr_frames->Dump(&s);
3775082c5fdSGreg Clayton     s.EOL();
3785082c5fdSGreg Clayton #endif
3795082c5fdSGreg Clayton     size_t curr_frame_num, prev_frame_num;
3805082c5fdSGreg Clayton 
381b9c1b51eSKate Stone     for (curr_frame_num = curr_frames->m_frames.size(),
382b9c1b51eSKate Stone         prev_frame_num = prev_frames->m_frames.size();
3835082c5fdSGreg Clayton          curr_frame_num > 0 && prev_frame_num > 0;
384b9c1b51eSKate Stone          --curr_frame_num, --prev_frame_num) {
3855082c5fdSGreg Clayton       const size_t curr_frame_idx = curr_frame_num - 1;
3865082c5fdSGreg Clayton       const size_t prev_frame_idx = prev_frame_num - 1;
387b57e4a1bSJason Molenda       StackFrameSP curr_frame_sp(curr_frames->m_frames[curr_frame_idx]);
388b57e4a1bSJason Molenda       StackFrameSP prev_frame_sp(prev_frames->m_frames[prev_frame_idx]);
3895082c5fdSGreg Clayton 
3905082c5fdSGreg Clayton #if defined(DEBUG_STACK_FRAMES)
3912cad65a5SGreg Clayton       s.Printf("\n\nCurr frame #%u ", curr_frame_idx);
3925082c5fdSGreg Clayton       if (curr_frame_sp)
3932cad65a5SGreg Clayton         curr_frame_sp->Dump(&s, true, false);
3945082c5fdSGreg Clayton       else
3955082c5fdSGreg Clayton         s.PutCString("NULL");
3962cad65a5SGreg Clayton       s.Printf("\nPrev frame #%u ", prev_frame_idx);
3975082c5fdSGreg Clayton       if (prev_frame_sp)
3982cad65a5SGreg Clayton         prev_frame_sp->Dump(&s, true, false);
3995082c5fdSGreg Clayton       else
4005082c5fdSGreg Clayton         s.PutCString("NULL");
4015082c5fdSGreg Clayton #endif
4025082c5fdSGreg Clayton 
403b57e4a1bSJason Molenda       StackFrame *curr_frame = curr_frame_sp.get();
404b57e4a1bSJason Molenda       StackFrame *prev_frame = prev_frame_sp.get();
4055082c5fdSGreg Clayton 
406d70a6e71SEugene Zelenko       if (curr_frame == nullptr || prev_frame == nullptr)
4075082c5fdSGreg Clayton         break;
4085082c5fdSGreg Clayton 
409eb8fa58eSVedant Kumar       // Check the stack ID to make sure they are equal.
41059e8fc1cSGreg Clayton       if (curr_frame->GetStackID() != prev_frame->GetStackID())
4115082c5fdSGreg Clayton         break;
4125082c5fdSGreg Clayton 
41359e8fc1cSGreg Clayton       prev_frame->UpdatePreviousFrameFromCurrentFrame(*curr_frame);
41405097246SAdrian Prantl       // Now copy the fixed up previous frame into the current frames so the
415eb8fa58eSVedant Kumar       // pointer doesn't change.
41659e8fc1cSGreg Clayton       m_frames[curr_frame_idx] = prev_frame_sp;
4175082c5fdSGreg Clayton 
4185082c5fdSGreg Clayton #if defined(DEBUG_STACK_FRAMES)
41968275d5eSGreg Clayton       s.Printf("\n    Copying previous frame to current frame");
4205082c5fdSGreg Clayton #endif
4215082c5fdSGreg Clayton     }
422eb8fa58eSVedant Kumar     // We are done with the old stack frame list, we can release it now.
4232cad65a5SGreg Clayton     m_prev_frames_sp.reset();
4245082c5fdSGreg Clayton   }
42568275d5eSGreg Clayton 
42668275d5eSGreg Clayton #if defined(DEBUG_STACK_FRAMES)
42768275d5eSGreg Clayton   s.PutCString("\n\nNew frames:\n");
42868275d5eSGreg Clayton   Dump(&s);
42968275d5eSGreg Clayton   s.EOL();
43068275d5eSGreg Clayton #endif
431ec6829eaSGreg Clayton }
432b0c72a5fSJim Ingham 
433b9c1b51eSKate Stone uint32_t StackFrameList::GetNumFrames(bool can_create) {
434bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
435b0c72a5fSJim Ingham 
436b0c72a5fSJim Ingham   if (can_create)
437b0c72a5fSJim Ingham     GetFramesUpTo(UINT32_MAX);
438513c6bb8SJim Ingham 
439*b3b7b1bfSVedant Kumar   return GetVisibleStackFrameIndex(m_frames.size());
44030fdc8d8SChris Lattner }
44130fdc8d8SChris Lattner 
442b9c1b51eSKate Stone void StackFrameList::Dump(Stream *s) {
443d70a6e71SEugene Zelenko   if (s == nullptr)
4445082c5fdSGreg Clayton     return;
445bb19a13cSSaleem Abdulrasool 
446bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
44730fdc8d8SChris Lattner 
4485082c5fdSGreg Clayton   const_iterator pos, begin = m_frames.begin(), end = m_frames.end();
449b9c1b51eSKate Stone   for (pos = begin; pos != end; ++pos) {
450b57e4a1bSJason Molenda     StackFrame *frame = (*pos).get();
451324a1036SSaleem Abdulrasool     s->Printf("%p: ", static_cast<void *>(frame));
452b9c1b51eSKate Stone     if (frame) {
45359e8fc1cSGreg Clayton       frame->GetStackID().Dump(s);
4540603aa9dSGreg Clayton       frame->DumpUsingSettingsFormat(s);
455b9c1b51eSKate Stone     } else
456dce502edSGreg Clayton       s->Printf("frame #%u", (uint32_t)std::distance(begin, pos));
4575082c5fdSGreg Clayton     s->EOL();
45812daf946SGreg Clayton   }
4595082c5fdSGreg Clayton   s->EOL();
4605082c5fdSGreg Clayton }
46112daf946SGreg Clayton 
462b9c1b51eSKate Stone StackFrameSP StackFrameList::GetFrameAtIndex(uint32_t idx) {
463b57e4a1bSJason Molenda   StackFrameSP frame_sp;
464bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
4655cb9a184SJim Ingham   uint32_t original_idx = idx;
4665cb9a184SJim Ingham 
467513c6bb8SJim Ingham   uint32_t inlined_depth = GetCurrentInlinedDepth();
468513c6bb8SJim Ingham   if (inlined_depth != UINT32_MAX)
469513c6bb8SJim Ingham     idx += inlined_depth;
470513c6bb8SJim Ingham 
4715082c5fdSGreg Clayton   if (idx < m_frames.size())
4725082c5fdSGreg Clayton     frame_sp = m_frames[idx];
47312daf946SGreg Clayton 
4745082c5fdSGreg Clayton   if (frame_sp)
47512daf946SGreg Clayton     return frame_sp;
47612daf946SGreg Clayton 
47705097246SAdrian Prantl   // GetFramesUpTo will fill m_frames with as many frames as you asked for, if
47805097246SAdrian Prantl   // there are that many.  If there weren't then you asked for too many frames.
479b0c72a5fSJim Ingham   GetFramesUpTo(idx);
480b9c1b51eSKate Stone   if (idx < m_frames.size()) {
481b9c1b51eSKate Stone     if (m_show_inlined_frames) {
482b9c1b51eSKate Stone       // When inline frames are enabled we actually create all the frames in
483b9c1b51eSKate Stone       // GetFramesUpTo.
4845082c5fdSGreg Clayton       frame_sp = m_frames[idx];
485b9c1b51eSKate Stone     } else {
48612daf946SGreg Clayton       Unwind *unwinder = m_thread.GetUnwinder();
487b9c1b51eSKate Stone       if (unwinder) {
48812daf946SGreg Clayton         addr_t pc, cfa;
489b9c1b51eSKate Stone         if (unwinder->GetFrameInfoAtIndex(idx, cfa, pc)) {
49099618476SJason Molenda           const bool cfa_is_valid = true;
49199618476SJason Molenda           const bool stop_id_is_valid = false;
49299618476SJason Molenda           const bool is_history_frame = false;
493b9c1b51eSKate Stone           frame_sp.reset(new StackFrame(
494b9c1b51eSKate Stone               m_thread.shared_from_this(), idx, idx, cfa, cfa_is_valid, pc, 0,
495d70a6e71SEugene Zelenko               stop_id_is_valid, is_history_frame, nullptr));
49659e8fc1cSGreg Clayton 
497b9c1b51eSKate Stone           Function *function =
498b9c1b51eSKate Stone               frame_sp->GetSymbolContext(eSymbolContextFunction).function;
499b9c1b51eSKate Stone           if (function) {
50005097246SAdrian Prantl             // When we aren't showing inline functions we always use the top
50105097246SAdrian Prantl             // most function block as the scope.
50259e8fc1cSGreg Clayton             frame_sp->SetSymbolContextScope(&function->GetBlock(false));
503b9c1b51eSKate Stone           } else {
504b9c1b51eSKate Stone             // Set the symbol scope from the symbol regardless if it is nullptr
505b9c1b51eSKate Stone             // or valid.
506b9c1b51eSKate Stone             frame_sp->SetSymbolContextScope(
507b9c1b51eSKate Stone                 frame_sp->GetSymbolContext(eSymbolContextSymbol).symbol);
50859e8fc1cSGreg Clayton           }
5095082c5fdSGreg Clayton           SetFrameAtIndex(idx, frame_sp);
51012daf946SGreg Clayton         }
51112daf946SGreg Clayton       }
51212daf946SGreg Clayton     }
513b9c1b51eSKate Stone   } else if (original_idx == 0) {
514b9c1b51eSKate Stone     // There should ALWAYS be a frame at index 0.  If something went wrong with
51505097246SAdrian Prantl     // the CurrentInlinedDepth such that there weren't as many frames as we
51605097246SAdrian Prantl     // thought taking that into account, then reset the current inlined depth
5175cb9a184SJim Ingham     // and return the real zeroth frame.
518b9c1b51eSKate Stone     if (m_frames.empty()) {
519b9c1b51eSKate Stone       // Why do we have a thread with zero frames, that should not ever
520b9c1b51eSKate Stone       // happen...
521a322f36cSDavid Blaikie       assert(!m_thread.IsValid() && "A valid thread has no frames.");
522b9c1b51eSKate Stone     } else {
523d70a6e71SEugene Zelenko       ResetCurrentInlinedDepth();
524d70a6e71SEugene Zelenko       frame_sp = m_frames[original_idx];
5255cb9a184SJim Ingham     }
5265cb9a184SJim Ingham   }
5275cb9a184SJim Ingham 
52830fdc8d8SChris Lattner   return frame_sp;
52930fdc8d8SChris Lattner }
53030fdc8d8SChris Lattner 
531b57e4a1bSJason Molenda StackFrameSP
532b9c1b51eSKate Stone StackFrameList::GetFrameWithConcreteFrameIndex(uint32_t unwind_idx) {
5335ccbd294SGreg Clayton   // First try assuming the unwind index is the same as the frame index. The
53405097246SAdrian Prantl   // unwind index is always greater than or equal to the frame index, so it is
53505097246SAdrian Prantl   // a good place to start. If we have inlined frames we might have 5 concrete
53605097246SAdrian Prantl   // frames (frame unwind indexes go from 0-4), but we might have 15 frames
53705097246SAdrian Prantl   // after we make all the inlined frames. Most of the time the unwind frame
53805097246SAdrian Prantl   // index (or the concrete frame index) is the same as the frame index.
5395ccbd294SGreg Clayton   uint32_t frame_idx = unwind_idx;
540b57e4a1bSJason Molenda   StackFrameSP frame_sp(GetFrameAtIndex(frame_idx));
541b9c1b51eSKate Stone   while (frame_sp) {
5425ccbd294SGreg Clayton     if (frame_sp->GetFrameIndex() == unwind_idx)
5435ccbd294SGreg Clayton       break;
5445ccbd294SGreg Clayton     frame_sp = GetFrameAtIndex(++frame_idx);
5455ccbd294SGreg Clayton   }
5465ccbd294SGreg Clayton   return frame_sp;
5475ccbd294SGreg Clayton }
5485ccbd294SGreg Clayton 
549b9c1b51eSKate Stone static bool CompareStackID(const StackFrameSP &stack_sp,
550b9c1b51eSKate Stone                            const StackID &stack_id) {
5517bcb93d5SGreg Clayton   return stack_sp->GetStackID() < stack_id;
5527bcb93d5SGreg Clayton }
5537bcb93d5SGreg Clayton 
554b9c1b51eSKate Stone StackFrameSP StackFrameList::GetFrameWithStackID(const StackID &stack_id) {
555b57e4a1bSJason Molenda   StackFrameSP frame_sp;
5567bcb93d5SGreg Clayton 
557b9c1b51eSKate Stone   if (stack_id.IsValid()) {
558bb19a13cSSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(m_mutex);
5597bcb93d5SGreg Clayton     uint32_t frame_idx = 0;
5607bcb93d5SGreg Clayton     // Do a binary search in case the stack frame is already in our cache
5617bcb93d5SGreg Clayton     collection::const_iterator begin = m_frames.begin();
5627bcb93d5SGreg Clayton     collection::const_iterator end = m_frames.end();
563b9c1b51eSKate Stone     if (begin != end) {
564b9c1b51eSKate Stone       collection::const_iterator pos =
565b9c1b51eSKate Stone           std::lower_bound(begin, end, stack_id, CompareStackID);
566b9c1b51eSKate Stone       if (pos != end) {
5678012cadbSGreg Clayton         if ((*pos)->GetStackID() == stack_id)
5687bcb93d5SGreg Clayton           return *pos;
5698012cadbSGreg Clayton       }
5707bcb93d5SGreg Clayton 
5718012cadbSGreg Clayton       //            if (m_frames.back()->GetStackID() < stack_id)
5728012cadbSGreg Clayton       //                frame_idx = m_frames.size();
5737bcb93d5SGreg Clayton     }
574b9c1b51eSKate Stone     do {
5753a195b7eSJim Ingham       frame_sp = GetFrameAtIndex(frame_idx);
5763a195b7eSJim Ingham       if (frame_sp && frame_sp->GetStackID() == stack_id)
5773a195b7eSJim Ingham         break;
5783a195b7eSJim Ingham       frame_idx++;
579b9c1b51eSKate Stone     } while (frame_sp);
5807bcb93d5SGreg Clayton   }
5813a195b7eSJim Ingham   return frame_sp;
5823a195b7eSJim Ingham }
5835ccbd294SGreg Clayton 
584b9c1b51eSKate Stone bool StackFrameList::SetFrameAtIndex(uint32_t idx, StackFrameSP &frame_sp) {
5855082c5fdSGreg Clayton   if (idx >= m_frames.size())
5865082c5fdSGreg Clayton     m_frames.resize(idx + 1);
58712daf946SGreg Clayton   // Make sure allocation succeeded by checking bounds again
588b9c1b51eSKate Stone   if (idx < m_frames.size()) {
5895082c5fdSGreg Clayton     m_frames[idx] = frame_sp;
59030fdc8d8SChris Lattner     return true;
59130fdc8d8SChris Lattner   }
59230fdc8d8SChris Lattner   return false; // resize failed, out of memory?
59330fdc8d8SChris Lattner }
59430fdc8d8SChris Lattner 
595b9c1b51eSKate Stone uint32_t StackFrameList::GetSelectedFrameIndex() const {
596bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
5972976d00aSJim Ingham   return m_selected_frame_idx;
59830fdc8d8SChris Lattner }
59930fdc8d8SChris Lattner 
600b9c1b51eSKate Stone uint32_t StackFrameList::SetSelectedFrame(lldb_private::StackFrame *frame) {
601bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
60212daf946SGreg Clayton   const_iterator pos;
6035082c5fdSGreg Clayton   const_iterator begin = m_frames.begin();
6045082c5fdSGreg Clayton   const_iterator end = m_frames.end();
605b7f6b2faSJim Ingham   m_selected_frame_idx = 0;
606b9c1b51eSKate Stone   for (pos = begin; pos != end; ++pos) {
607b9c1b51eSKate Stone     if (pos->get() == frame) {
6082976d00aSJim Ingham       m_selected_frame_idx = std::distance(begin, pos);
609513c6bb8SJim Ingham       uint32_t inlined_depth = GetCurrentInlinedDepth();
610513c6bb8SJim Ingham       if (inlined_depth != UINT32_MAX)
611513c6bb8SJim Ingham         m_selected_frame_idx -= inlined_depth;
612b7f6b2faSJim Ingham       break;
61330fdc8d8SChris Lattner     }
61430fdc8d8SChris Lattner   }
615b7f6b2faSJim Ingham   SetDefaultFileAndLineToSelectedFrame();
6162976d00aSJim Ingham   return m_selected_frame_idx;
61730fdc8d8SChris Lattner }
61830fdc8d8SChris Lattner 
619b9c1b51eSKate Stone bool StackFrameList::SetSelectedFrameByIndex(uint32_t idx) {
620bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
621b57e4a1bSJason Molenda   StackFrameSP frame_sp(GetFrameAtIndex(idx));
622b9c1b51eSKate Stone   if (frame_sp) {
623b0c72a5fSJim Ingham     SetSelectedFrame(frame_sp.get());
624b0c72a5fSJim Ingham     return true;
625b9c1b51eSKate Stone   } else
626b0c72a5fSJim Ingham     return false;
627b7f6b2faSJim Ingham }
628b7f6b2faSJim Ingham 
629b9c1b51eSKate Stone void StackFrameList::SetDefaultFileAndLineToSelectedFrame() {
630b9c1b51eSKate Stone   if (m_thread.GetID() ==
631b9c1b51eSKate Stone       m_thread.GetProcess()->GetThreadList().GetSelectedThread()->GetID()) {
632b57e4a1bSJason Molenda     StackFrameSP frame_sp(GetFrameAtIndex(GetSelectedFrameIndex()));
633b9c1b51eSKate Stone     if (frame_sp) {
634252d0edeSGreg Clayton       SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextLineEntry);
635b7f6b2faSJim Ingham       if (sc.line_entry.file)
636b9c1b51eSKate Stone         m_thread.CalculateTarget()->GetSourceManager().SetDefaultFileAndLine(
637b9c1b51eSKate Stone             sc.line_entry.file, sc.line_entry.line);
638b7f6b2faSJim Ingham     }
639b7f6b2faSJim Ingham   }
64030fdc8d8SChris Lattner }
64130fdc8d8SChris Lattner 
64230fdc8d8SChris Lattner // The thread has been run, reset the number stack frames to zero so we can
64330fdc8d8SChris Lattner // determine how many frames we have lazily.
644b9c1b51eSKate Stone void StackFrameList::Clear() {
645bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
6465082c5fdSGreg Clayton   m_frames.clear();
647b0c72a5fSJim Ingham   m_concrete_frames_fetched = 0;
64830fdc8d8SChris Lattner }
64930fdc8d8SChris Lattner 
650b9c1b51eSKate Stone void StackFrameList::Merge(std::unique_ptr<StackFrameList> &curr_ap,
651b9c1b51eSKate Stone                            lldb::StackFrameListSP &prev_sp) {
652bb19a13cSSaleem Abdulrasool   std::unique_lock<std::recursive_mutex> current_lock, previous_lock;
653bb19a13cSSaleem Abdulrasool   if (curr_ap)
654bb19a13cSSaleem Abdulrasool     current_lock = std::unique_lock<std::recursive_mutex>(curr_ap->m_mutex);
655bb19a13cSSaleem Abdulrasool   if (prev_sp)
656bb19a13cSSaleem Abdulrasool     previous_lock = std::unique_lock<std::recursive_mutex>(prev_sp->m_mutex);
6572cad65a5SGreg Clayton 
6582cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES)
6592c595273SJohnny Chen   StreamFile s(stdout, false);
6602cad65a5SGreg Clayton   s.PutCString("\n\nStackFrameList::Merge():\nPrev:\n");
661d70a6e71SEugene Zelenko   if (prev_sp)
6622cad65a5SGreg Clayton     prev_sp->Dump(&s);
6632cad65a5SGreg Clayton   else
6642cad65a5SGreg Clayton     s.PutCString("NULL");
6652cad65a5SGreg Clayton   s.PutCString("\nCurr:\n");
666d70a6e71SEugene Zelenko   if (curr_ap)
6672cad65a5SGreg Clayton     curr_ap->Dump(&s);
6682cad65a5SGreg Clayton   else
6692cad65a5SGreg Clayton     s.PutCString("NULL");
6702cad65a5SGreg Clayton   s.EOL();
6712cad65a5SGreg Clayton #endif
6722cad65a5SGreg Clayton 
673b9c1b51eSKate Stone   if (!curr_ap || curr_ap->GetNumFrames(false) == 0) {
6742cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES)
6752cad65a5SGreg Clayton     s.PutCString("No current frames, leave previous frames alone...\n");
6762cad65a5SGreg Clayton #endif
6772cad65a5SGreg Clayton     curr_ap.release();
6782cad65a5SGreg Clayton     return;
6792cad65a5SGreg Clayton   }
6802cad65a5SGreg Clayton 
681b9c1b51eSKate Stone   if (!prev_sp || prev_sp->GetNumFrames(false) == 0) {
6822cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES)
6832cad65a5SGreg Clayton     s.PutCString("No previous frames, so use current frames...\n");
6842cad65a5SGreg Clayton #endif
68505097246SAdrian Prantl     // We either don't have any previous frames, or since we have more than one
68605097246SAdrian Prantl     // current frames it means we have all the frames and can safely replace
68705097246SAdrian Prantl     // our previous frames.
6882cad65a5SGreg Clayton     prev_sp.reset(curr_ap.release());
6892cad65a5SGreg Clayton     return;
6902cad65a5SGreg Clayton   }
6912cad65a5SGreg Clayton 
6922cad65a5SGreg Clayton   const uint32_t num_curr_frames = curr_ap->GetNumFrames(false);
6932cad65a5SGreg Clayton 
694b9c1b51eSKate Stone   if (num_curr_frames > 1) {
6952cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES)
696b9c1b51eSKate Stone     s.PutCString(
697b9c1b51eSKate Stone         "We have more than one current frame, so use current frames...\n");
6982cad65a5SGreg Clayton #endif
69905097246SAdrian Prantl     // We have more than one current frames it means we have all the frames and
70005097246SAdrian Prantl     // can safely replace our previous frames.
7012cad65a5SGreg Clayton     prev_sp.reset(curr_ap.release());
7022cad65a5SGreg Clayton 
7032cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES)
7042cad65a5SGreg Clayton     s.PutCString("\nMerged:\n");
7052cad65a5SGreg Clayton     prev_sp->Dump(&s);
7062cad65a5SGreg Clayton #endif
7072cad65a5SGreg Clayton     return;
7082cad65a5SGreg Clayton   }
7092cad65a5SGreg Clayton 
710b57e4a1bSJason Molenda   StackFrameSP prev_frame_zero_sp(prev_sp->GetFrameAtIndex(0));
711b57e4a1bSJason Molenda   StackFrameSP curr_frame_zero_sp(curr_ap->GetFrameAtIndex(0));
7122cad65a5SGreg Clayton   StackID curr_stack_id(curr_frame_zero_sp->GetStackID());
7132cad65a5SGreg Clayton   StackID prev_stack_id(prev_frame_zero_sp->GetStackID());
7142cad65a5SGreg Clayton 
7152cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES)
7162c595273SJohnny Chen   const uint32_t num_prev_frames = prev_sp->GetNumFrames(false);
7172cad65a5SGreg Clayton   s.Printf("\n%u previous frames with one current frame\n", num_prev_frames);
7182cad65a5SGreg Clayton #endif
7192cad65a5SGreg Clayton 
7202cad65a5SGreg Clayton   // We have only a single current frame
7212cad65a5SGreg Clayton   // Our previous stack frames only had a single frame as well...
722b9c1b51eSKate Stone   if (curr_stack_id == prev_stack_id) {
7232cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES)
724b9c1b51eSKate Stone     s.Printf("\nPrevious frame #0 is same as current frame #0, merge the "
725b9c1b51eSKate Stone              "cached data\n");
7262cad65a5SGreg Clayton #endif
7272cad65a5SGreg Clayton 
728b9c1b51eSKate Stone     curr_frame_zero_sp->UpdateCurrentFrameFromPreviousFrame(
729b9c1b51eSKate Stone         *prev_frame_zero_sp);
730b9c1b51eSKate Stone     //        prev_frame_zero_sp->UpdatePreviousFrameFromCurrentFrame
731b9c1b51eSKate Stone     //        (*curr_frame_zero_sp);
7322cad65a5SGreg Clayton     //        prev_sp->SetFrameAtIndex (0, prev_frame_zero_sp);
733b9c1b51eSKate Stone   } else if (curr_stack_id < prev_stack_id) {
7342cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES)
735b9c1b51eSKate Stone     s.Printf("\nCurrent frame #0 has a stack ID that is less than the previous "
736b9c1b51eSKate Stone              "frame #0, insert current frame zero in front of previous\n");
7372cad65a5SGreg Clayton #endif
7382cad65a5SGreg Clayton     prev_sp->m_frames.insert(prev_sp->m_frames.begin(), curr_frame_zero_sp);
7392cad65a5SGreg Clayton   }
7402cad65a5SGreg Clayton 
7412cad65a5SGreg Clayton   curr_ap.release();
7422cad65a5SGreg Clayton 
7432cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES)
7442cad65a5SGreg Clayton   s.PutCString("\nMerged:\n");
7452cad65a5SGreg Clayton   prev_sp->Dump(&s);
7462cad65a5SGreg Clayton #endif
7472cad65a5SGreg Clayton }
748e4284b71SJim Ingham 
749b57e4a1bSJason Molenda lldb::StackFrameSP
750b9c1b51eSKate Stone StackFrameList::GetStackFrameSPForStackFramePtr(StackFrame *stack_frame_ptr) {
751e4284b71SJim Ingham   const_iterator pos;
752e4284b71SJim Ingham   const_iterator begin = m_frames.begin();
753e4284b71SJim Ingham   const_iterator end = m_frames.end();
754b57e4a1bSJason Molenda   lldb::StackFrameSP ret_sp;
755e4284b71SJim Ingham 
756b9c1b51eSKate Stone   for (pos = begin; pos != end; ++pos) {
757b9c1b51eSKate Stone     if (pos->get() == stack_frame_ptr) {
758e4284b71SJim Ingham       ret_sp = (*pos);
759e4284b71SJim Ingham       break;
760e4284b71SJim Ingham     }
761e4284b71SJim Ingham   }
762e4284b71SJim Ingham   return ret_sp;
763e4284b71SJim Ingham }
764e4284b71SJim Ingham 
765b9c1b51eSKate Stone size_t StackFrameList::GetStatus(Stream &strm, uint32_t first_frame,
766b9c1b51eSKate Stone                                  uint32_t num_frames, bool show_frame_info,
7678ec10efcSJim Ingham                                  uint32_t num_frames_with_source,
7687f1c1211SPavel Labath                                  bool show_unique,
769b9c1b51eSKate Stone                                  const char *selected_frame_marker) {
7707260f620SGreg Clayton   size_t num_frames_displayed = 0;
7717260f620SGreg Clayton 
7727260f620SGreg Clayton   if (num_frames == 0)
7737260f620SGreg Clayton     return 0;
7747260f620SGreg Clayton 
775b57e4a1bSJason Molenda   StackFrameSP frame_sp;
7767260f620SGreg Clayton   uint32_t frame_idx = 0;
7777260f620SGreg Clayton   uint32_t last_frame;
7787260f620SGreg Clayton 
7797260f620SGreg Clayton   // Don't let the last frame wrap around...
7807260f620SGreg Clayton   if (num_frames == UINT32_MAX)
7817260f620SGreg Clayton     last_frame = UINT32_MAX;
7827260f620SGreg Clayton   else
7837260f620SGreg Clayton     last_frame = first_frame + num_frames;
7847260f620SGreg Clayton 
785b57e4a1bSJason Molenda   StackFrameSP selected_frame_sp = m_thread.GetSelectedFrame();
786d70a6e71SEugene Zelenko   const char *unselected_marker = nullptr;
7878ec10efcSJim Ingham   std::string buffer;
788b9c1b51eSKate Stone   if (selected_frame_marker) {
7898ec10efcSJim Ingham     size_t len = strlen(selected_frame_marker);
7908ec10efcSJim Ingham     buffer.insert(buffer.begin(), len, ' ');
7918ec10efcSJim Ingham     unselected_marker = buffer.c_str();
7928ec10efcSJim Ingham   }
793d70a6e71SEugene Zelenko   const char *marker = nullptr;
7948ec10efcSJim Ingham 
795b9c1b51eSKate Stone   for (frame_idx = first_frame; frame_idx < last_frame; ++frame_idx) {
7967260f620SGreg Clayton     frame_sp = GetFrameAtIndex(frame_idx);
797d70a6e71SEugene Zelenko     if (!frame_sp)
7987260f620SGreg Clayton       break;
7997260f620SGreg Clayton 
800b9c1b51eSKate Stone     if (selected_frame_marker != nullptr) {
8018ec10efcSJim Ingham       if (frame_sp == selected_frame_sp)
8028ec10efcSJim Ingham         marker = selected_frame_marker;
8038ec10efcSJim Ingham       else
8048ec10efcSJim Ingham         marker = unselected_marker;
8058ec10efcSJim Ingham     }
8068ec10efcSJim Ingham 
807b9c1b51eSKate Stone     if (!frame_sp->GetStatus(strm, show_frame_info,
808b9c1b51eSKate Stone                              num_frames_with_source > (first_frame - frame_idx),
8097f1c1211SPavel Labath                              show_unique, marker))
8107260f620SGreg Clayton       break;
8117260f620SGreg Clayton     ++num_frames_displayed;
8127260f620SGreg Clayton   }
8137260f620SGreg Clayton 
8147260f620SGreg Clayton   strm.IndentLess();
8157260f620SGreg Clayton   return num_frames_displayed;
8167260f620SGreg Clayton }
817