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() {
84*e7167e03SVedant Kumar   if (!m_show_inlined_frames)
85*e7167e03SVedant Kumar     return;
86*e7167e03SVedant 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");
99*e7167e03SVedant Kumar     return;
100*e7167e03SVedant Kumar   }
101*e7167e03SVedant 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
105*e7167e03SVedant Kumar   // the END of an inlined function, which coincides with the end of either
106*e7167e03SVedant Kumar   // its containing function or another inlined function.
107513c6bb8SJim Ingham 
108513c6bb8SJim Ingham   Block *block_ptr = m_frames[0]->GetFrameBlock();
109*e7167e03SVedant Kumar   if (!block_ptr)
110*e7167e03SVedant Kumar     return;
111*e7167e03SVedant Kumar 
112513c6bb8SJim Ingham   Address pc_as_address;
113*e7167e03SVedant Kumar   lldb::addr_t curr_pc = m_thread.GetRegisterContext()->GetPC();
114*e7167e03SVedant Kumar   pc_as_address.SetLoadAddress(curr_pc, &(m_thread.GetProcess()->GetTarget()));
115513c6bb8SJim Ingham   AddressRange containing_range;
116*e7167e03SVedant Kumar   if (!block_ptr->GetRangeContainingAddress(pc_as_address, containing_range) ||
117*e7167e03SVedant Kumar       pc_as_address != containing_range.GetBaseAddress())
118*e7167e03SVedant Kumar     return;
119*e7167e03SVedant Kumar 
120*e7167e03SVedant Kumar   // If we got here because of a breakpoint hit, then set the inlined depth
121*e7167e03SVedant Kumar   // depending on where the breakpoint was set. If we got here because of a
122*e7167e03SVedant Kumar   // crash, then set the inlined depth to the deepest most block.  Otherwise,
123*e7167e03SVedant Kumar   // we stopped here naturally as the result of a step, so set ourselves in the
124*e7167e03SVedant Kumar   // containing frame of the whole set of nested inlines, so the user can then
125*e7167e03SVedant Kumar   // "virtually" step into the frames one by one, or next over the whole mess.
126*e7167e03SVedant Kumar   // Note: We don't have to handle being somewhere in the middle of the stack
127*e7167e03SVedant Kumar   // here, since ResetCurrentInlinedDepth doesn't get called if there is a
128*e7167e03SVedant Kumar   // valid inlined depth set.
129513c6bb8SJim Ingham   StopInfoSP stop_info_sp = m_thread.GetStopInfo();
130*e7167e03SVedant Kumar   if (!stop_info_sp)
131*e7167e03SVedant 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:
137*e7167e03SVedant 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: {
142*e7167e03SVedant Kumar     // FIXME: Figure out what this break point is doing, and set the inline
143*e7167e03SVedant Kumar     // depth appropriately.  Be careful to take into account breakpoints that
144*e7167e03SVedant Kumar     // implement step over prologue, since that should do the default
145*e7167e03SVedant Kumar     // calculation. For now, if the breakpoints corresponding to this hit are
146*e7167e03SVedant Kumar     // all internal, I set the stop location to the top of the inlined stack,
147*e7167e03SVedant Kumar     // since that will make things like stepping over prologues work right.
148*e7167e03SVedant Kumar     // But if there are any non-internal breakpoints I do to the bottom of the
149*e7167e03SVedant 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(
152*e7167e03SVedant 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++) {
157*e7167e03SVedant 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: {
171*e7167e03SVedant Kumar     // Otherwise, we should set ourselves at the container of the inlining, so
172*e7167e03SVedant Kumar     // that the user can descend into them. So first we check whether we have
173*e7167e03SVedant 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()) {
179*e7167e03SVedant Kumar       if (!container_ptr->GetRangeContainingAddress(pc_as_address,
180*e7167e03SVedant 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;
189*e7167e03SVedant 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 
195*e7167e03SVedant 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 
439513c6bb8SJim Ingham   uint32_t inlined_depth = GetCurrentInlinedDepth();
440513c6bb8SJim Ingham   if (inlined_depth == UINT32_MAX)
4415082c5fdSGreg Clayton     return m_frames.size();
442513c6bb8SJim Ingham   else
443513c6bb8SJim Ingham     return m_frames.size() - inlined_depth;
44430fdc8d8SChris Lattner }
44530fdc8d8SChris Lattner 
446b9c1b51eSKate Stone void StackFrameList::Dump(Stream *s) {
447d70a6e71SEugene Zelenko   if (s == nullptr)
4485082c5fdSGreg Clayton     return;
449bb19a13cSSaleem Abdulrasool 
450bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
45130fdc8d8SChris Lattner 
4525082c5fdSGreg Clayton   const_iterator pos, begin = m_frames.begin(), end = m_frames.end();
453b9c1b51eSKate Stone   for (pos = begin; pos != end; ++pos) {
454b57e4a1bSJason Molenda     StackFrame *frame = (*pos).get();
455324a1036SSaleem Abdulrasool     s->Printf("%p: ", static_cast<void *>(frame));
456b9c1b51eSKate Stone     if (frame) {
45759e8fc1cSGreg Clayton       frame->GetStackID().Dump(s);
4580603aa9dSGreg Clayton       frame->DumpUsingSettingsFormat(s);
459b9c1b51eSKate Stone     } else
460dce502edSGreg Clayton       s->Printf("frame #%u", (uint32_t)std::distance(begin, pos));
4615082c5fdSGreg Clayton     s->EOL();
46212daf946SGreg Clayton   }
4635082c5fdSGreg Clayton   s->EOL();
4645082c5fdSGreg Clayton }
46512daf946SGreg Clayton 
466b9c1b51eSKate Stone StackFrameSP StackFrameList::GetFrameAtIndex(uint32_t idx) {
467b57e4a1bSJason Molenda   StackFrameSP frame_sp;
468bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
4695cb9a184SJim Ingham   uint32_t original_idx = idx;
4705cb9a184SJim Ingham 
471513c6bb8SJim Ingham   uint32_t inlined_depth = GetCurrentInlinedDepth();
472513c6bb8SJim Ingham   if (inlined_depth != UINT32_MAX)
473513c6bb8SJim Ingham     idx += inlined_depth;
474513c6bb8SJim Ingham 
4755082c5fdSGreg Clayton   if (idx < m_frames.size())
4765082c5fdSGreg Clayton     frame_sp = m_frames[idx];
47712daf946SGreg Clayton 
4785082c5fdSGreg Clayton   if (frame_sp)
47912daf946SGreg Clayton     return frame_sp;
48012daf946SGreg Clayton 
48105097246SAdrian Prantl   // GetFramesUpTo will fill m_frames with as many frames as you asked for, if
48205097246SAdrian Prantl   // there are that many.  If there weren't then you asked for too many frames.
483b0c72a5fSJim Ingham   GetFramesUpTo(idx);
484b9c1b51eSKate Stone   if (idx < m_frames.size()) {
485b9c1b51eSKate Stone     if (m_show_inlined_frames) {
486b9c1b51eSKate Stone       // When inline frames are enabled we actually create all the frames in
487b9c1b51eSKate Stone       // GetFramesUpTo.
4885082c5fdSGreg Clayton       frame_sp = m_frames[idx];
489b9c1b51eSKate Stone     } else {
49012daf946SGreg Clayton       Unwind *unwinder = m_thread.GetUnwinder();
491b9c1b51eSKate Stone       if (unwinder) {
49212daf946SGreg Clayton         addr_t pc, cfa;
493b9c1b51eSKate Stone         if (unwinder->GetFrameInfoAtIndex(idx, cfa, pc)) {
49499618476SJason Molenda           const bool cfa_is_valid = true;
49599618476SJason Molenda           const bool stop_id_is_valid = false;
49699618476SJason Molenda           const bool is_history_frame = false;
497b9c1b51eSKate Stone           frame_sp.reset(new StackFrame(
498b9c1b51eSKate Stone               m_thread.shared_from_this(), idx, idx, cfa, cfa_is_valid, pc, 0,
499d70a6e71SEugene Zelenko               stop_id_is_valid, is_history_frame, nullptr));
50059e8fc1cSGreg Clayton 
501b9c1b51eSKate Stone           Function *function =
502b9c1b51eSKate Stone               frame_sp->GetSymbolContext(eSymbolContextFunction).function;
503b9c1b51eSKate Stone           if (function) {
50405097246SAdrian Prantl             // When we aren't showing inline functions we always use the top
50505097246SAdrian Prantl             // most function block as the scope.
50659e8fc1cSGreg Clayton             frame_sp->SetSymbolContextScope(&function->GetBlock(false));
507b9c1b51eSKate Stone           } else {
508b9c1b51eSKate Stone             // Set the symbol scope from the symbol regardless if it is nullptr
509b9c1b51eSKate Stone             // or valid.
510b9c1b51eSKate Stone             frame_sp->SetSymbolContextScope(
511b9c1b51eSKate Stone                 frame_sp->GetSymbolContext(eSymbolContextSymbol).symbol);
51259e8fc1cSGreg Clayton           }
5135082c5fdSGreg Clayton           SetFrameAtIndex(idx, frame_sp);
51412daf946SGreg Clayton         }
51512daf946SGreg Clayton       }
51612daf946SGreg Clayton     }
517b9c1b51eSKate Stone   } else if (original_idx == 0) {
518b9c1b51eSKate Stone     // There should ALWAYS be a frame at index 0.  If something went wrong with
51905097246SAdrian Prantl     // the CurrentInlinedDepth such that there weren't as many frames as we
52005097246SAdrian Prantl     // thought taking that into account, then reset the current inlined depth
5215cb9a184SJim Ingham     // and return the real zeroth frame.
522b9c1b51eSKate Stone     if (m_frames.empty()) {
523b9c1b51eSKate Stone       // Why do we have a thread with zero frames, that should not ever
524b9c1b51eSKate Stone       // happen...
525a322f36cSDavid Blaikie       assert(!m_thread.IsValid() && "A valid thread has no frames.");
526b9c1b51eSKate Stone     } else {
527d70a6e71SEugene Zelenko       ResetCurrentInlinedDepth();
528d70a6e71SEugene Zelenko       frame_sp = m_frames[original_idx];
5295cb9a184SJim Ingham     }
5305cb9a184SJim Ingham   }
5315cb9a184SJim Ingham 
53230fdc8d8SChris Lattner   return frame_sp;
53330fdc8d8SChris Lattner }
53430fdc8d8SChris Lattner 
535b57e4a1bSJason Molenda StackFrameSP
536b9c1b51eSKate Stone StackFrameList::GetFrameWithConcreteFrameIndex(uint32_t unwind_idx) {
5375ccbd294SGreg Clayton   // First try assuming the unwind index is the same as the frame index. The
53805097246SAdrian Prantl   // unwind index is always greater than or equal to the frame index, so it is
53905097246SAdrian Prantl   // a good place to start. If we have inlined frames we might have 5 concrete
54005097246SAdrian Prantl   // frames (frame unwind indexes go from 0-4), but we might have 15 frames
54105097246SAdrian Prantl   // after we make all the inlined frames. Most of the time the unwind frame
54205097246SAdrian Prantl   // index (or the concrete frame index) is the same as the frame index.
5435ccbd294SGreg Clayton   uint32_t frame_idx = unwind_idx;
544b57e4a1bSJason Molenda   StackFrameSP frame_sp(GetFrameAtIndex(frame_idx));
545b9c1b51eSKate Stone   while (frame_sp) {
5465ccbd294SGreg Clayton     if (frame_sp->GetFrameIndex() == unwind_idx)
5475ccbd294SGreg Clayton       break;
5485ccbd294SGreg Clayton     frame_sp = GetFrameAtIndex(++frame_idx);
5495ccbd294SGreg Clayton   }
5505ccbd294SGreg Clayton   return frame_sp;
5515ccbd294SGreg Clayton }
5525ccbd294SGreg Clayton 
553b9c1b51eSKate Stone static bool CompareStackID(const StackFrameSP &stack_sp,
554b9c1b51eSKate Stone                            const StackID &stack_id) {
5557bcb93d5SGreg Clayton   return stack_sp->GetStackID() < stack_id;
5567bcb93d5SGreg Clayton }
5577bcb93d5SGreg Clayton 
558b9c1b51eSKate Stone StackFrameSP StackFrameList::GetFrameWithStackID(const StackID &stack_id) {
559b57e4a1bSJason Molenda   StackFrameSP frame_sp;
5607bcb93d5SGreg Clayton 
561b9c1b51eSKate Stone   if (stack_id.IsValid()) {
562bb19a13cSSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(m_mutex);
5637bcb93d5SGreg Clayton     uint32_t frame_idx = 0;
5647bcb93d5SGreg Clayton     // Do a binary search in case the stack frame is already in our cache
5657bcb93d5SGreg Clayton     collection::const_iterator begin = m_frames.begin();
5667bcb93d5SGreg Clayton     collection::const_iterator end = m_frames.end();
567b9c1b51eSKate Stone     if (begin != end) {
568b9c1b51eSKate Stone       collection::const_iterator pos =
569b9c1b51eSKate Stone           std::lower_bound(begin, end, stack_id, CompareStackID);
570b9c1b51eSKate Stone       if (pos != end) {
5718012cadbSGreg Clayton         if ((*pos)->GetStackID() == stack_id)
5727bcb93d5SGreg Clayton           return *pos;
5738012cadbSGreg Clayton       }
5747bcb93d5SGreg Clayton 
5758012cadbSGreg Clayton       //            if (m_frames.back()->GetStackID() < stack_id)
5768012cadbSGreg Clayton       //                frame_idx = m_frames.size();
5777bcb93d5SGreg Clayton     }
578b9c1b51eSKate Stone     do {
5793a195b7eSJim Ingham       frame_sp = GetFrameAtIndex(frame_idx);
5803a195b7eSJim Ingham       if (frame_sp && frame_sp->GetStackID() == stack_id)
5813a195b7eSJim Ingham         break;
5823a195b7eSJim Ingham       frame_idx++;
583b9c1b51eSKate Stone     } while (frame_sp);
5847bcb93d5SGreg Clayton   }
5853a195b7eSJim Ingham   return frame_sp;
5863a195b7eSJim Ingham }
5875ccbd294SGreg Clayton 
588b9c1b51eSKate Stone bool StackFrameList::SetFrameAtIndex(uint32_t idx, StackFrameSP &frame_sp) {
5895082c5fdSGreg Clayton   if (idx >= m_frames.size())
5905082c5fdSGreg Clayton     m_frames.resize(idx + 1);
59112daf946SGreg Clayton   // Make sure allocation succeeded by checking bounds again
592b9c1b51eSKate Stone   if (idx < m_frames.size()) {
5935082c5fdSGreg Clayton     m_frames[idx] = frame_sp;
59430fdc8d8SChris Lattner     return true;
59530fdc8d8SChris Lattner   }
59630fdc8d8SChris Lattner   return false; // resize failed, out of memory?
59730fdc8d8SChris Lattner }
59830fdc8d8SChris Lattner 
599b9c1b51eSKate Stone uint32_t StackFrameList::GetSelectedFrameIndex() const {
600bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
6012976d00aSJim Ingham   return m_selected_frame_idx;
60230fdc8d8SChris Lattner }
60330fdc8d8SChris Lattner 
604b9c1b51eSKate Stone uint32_t StackFrameList::SetSelectedFrame(lldb_private::StackFrame *frame) {
605bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
60612daf946SGreg Clayton   const_iterator pos;
6075082c5fdSGreg Clayton   const_iterator begin = m_frames.begin();
6085082c5fdSGreg Clayton   const_iterator end = m_frames.end();
609b7f6b2faSJim Ingham   m_selected_frame_idx = 0;
610b9c1b51eSKate Stone   for (pos = begin; pos != end; ++pos) {
611b9c1b51eSKate Stone     if (pos->get() == frame) {
6122976d00aSJim Ingham       m_selected_frame_idx = std::distance(begin, pos);
613513c6bb8SJim Ingham       uint32_t inlined_depth = GetCurrentInlinedDepth();
614513c6bb8SJim Ingham       if (inlined_depth != UINT32_MAX)
615513c6bb8SJim Ingham         m_selected_frame_idx -= inlined_depth;
616b7f6b2faSJim Ingham       break;
61730fdc8d8SChris Lattner     }
61830fdc8d8SChris Lattner   }
619b7f6b2faSJim Ingham   SetDefaultFileAndLineToSelectedFrame();
6202976d00aSJim Ingham   return m_selected_frame_idx;
62130fdc8d8SChris Lattner }
62230fdc8d8SChris Lattner 
62330fdc8d8SChris Lattner // Mark a stack frame as the current frame using the frame index
624b9c1b51eSKate Stone bool StackFrameList::SetSelectedFrameByIndex(uint32_t idx) {
625bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
626b57e4a1bSJason Molenda   StackFrameSP frame_sp(GetFrameAtIndex(idx));
627b9c1b51eSKate Stone   if (frame_sp) {
628b0c72a5fSJim Ingham     SetSelectedFrame(frame_sp.get());
629b0c72a5fSJim Ingham     return true;
630b9c1b51eSKate Stone   } else
631b0c72a5fSJim Ingham     return false;
632b7f6b2faSJim Ingham }
633b7f6b2faSJim Ingham 
634b9c1b51eSKate Stone void StackFrameList::SetDefaultFileAndLineToSelectedFrame() {
635b9c1b51eSKate Stone   if (m_thread.GetID() ==
636b9c1b51eSKate Stone       m_thread.GetProcess()->GetThreadList().GetSelectedThread()->GetID()) {
637b57e4a1bSJason Molenda     StackFrameSP frame_sp(GetFrameAtIndex(GetSelectedFrameIndex()));
638b9c1b51eSKate Stone     if (frame_sp) {
639252d0edeSGreg Clayton       SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextLineEntry);
640b7f6b2faSJim Ingham       if (sc.line_entry.file)
641b9c1b51eSKate Stone         m_thread.CalculateTarget()->GetSourceManager().SetDefaultFileAndLine(
642b9c1b51eSKate Stone             sc.line_entry.file, sc.line_entry.line);
643b7f6b2faSJim Ingham     }
644b7f6b2faSJim Ingham   }
64530fdc8d8SChris Lattner }
64630fdc8d8SChris Lattner 
64730fdc8d8SChris Lattner // The thread has been run, reset the number stack frames to zero so we can
64830fdc8d8SChris Lattner // determine how many frames we have lazily.
649b9c1b51eSKate Stone void StackFrameList::Clear() {
650bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
6515082c5fdSGreg Clayton   m_frames.clear();
652b0c72a5fSJim Ingham   m_concrete_frames_fetched = 0;
65330fdc8d8SChris Lattner }
65430fdc8d8SChris Lattner 
655b9c1b51eSKate Stone void StackFrameList::InvalidateFrames(uint32_t start_idx) {
656bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
657b9c1b51eSKate Stone   if (m_show_inlined_frames) {
65812daf946SGreg Clayton     Clear();
659b9c1b51eSKate Stone   } else {
6605082c5fdSGreg Clayton     const size_t num_frames = m_frames.size();
661b9c1b51eSKate Stone     while (start_idx < num_frames) {
6625082c5fdSGreg Clayton       m_frames[start_idx].reset();
66330fdc8d8SChris Lattner       ++start_idx;
66430fdc8d8SChris Lattner     }
66530fdc8d8SChris Lattner   }
66612daf946SGreg Clayton }
6672cad65a5SGreg Clayton 
668b9c1b51eSKate Stone void StackFrameList::Merge(std::unique_ptr<StackFrameList> &curr_ap,
669b9c1b51eSKate Stone                            lldb::StackFrameListSP &prev_sp) {
670bb19a13cSSaleem Abdulrasool   std::unique_lock<std::recursive_mutex> current_lock, previous_lock;
671bb19a13cSSaleem Abdulrasool   if (curr_ap)
672bb19a13cSSaleem Abdulrasool     current_lock = std::unique_lock<std::recursive_mutex>(curr_ap->m_mutex);
673bb19a13cSSaleem Abdulrasool   if (prev_sp)
674bb19a13cSSaleem Abdulrasool     previous_lock = std::unique_lock<std::recursive_mutex>(prev_sp->m_mutex);
6752cad65a5SGreg Clayton 
6762cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES)
6772c595273SJohnny Chen   StreamFile s(stdout, false);
6782cad65a5SGreg Clayton   s.PutCString("\n\nStackFrameList::Merge():\nPrev:\n");
679d70a6e71SEugene Zelenko   if (prev_sp)
6802cad65a5SGreg Clayton     prev_sp->Dump(&s);
6812cad65a5SGreg Clayton   else
6822cad65a5SGreg Clayton     s.PutCString("NULL");
6832cad65a5SGreg Clayton   s.PutCString("\nCurr:\n");
684d70a6e71SEugene Zelenko   if (curr_ap)
6852cad65a5SGreg Clayton     curr_ap->Dump(&s);
6862cad65a5SGreg Clayton   else
6872cad65a5SGreg Clayton     s.PutCString("NULL");
6882cad65a5SGreg Clayton   s.EOL();
6892cad65a5SGreg Clayton #endif
6902cad65a5SGreg Clayton 
691b9c1b51eSKate Stone   if (!curr_ap || curr_ap->GetNumFrames(false) == 0) {
6922cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES)
6932cad65a5SGreg Clayton     s.PutCString("No current frames, leave previous frames alone...\n");
6942cad65a5SGreg Clayton #endif
6952cad65a5SGreg Clayton     curr_ap.release();
6962cad65a5SGreg Clayton     return;
6972cad65a5SGreg Clayton   }
6982cad65a5SGreg Clayton 
699b9c1b51eSKate Stone   if (!prev_sp || prev_sp->GetNumFrames(false) == 0) {
7002cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES)
7012cad65a5SGreg Clayton     s.PutCString("No previous frames, so use current frames...\n");
7022cad65a5SGreg Clayton #endif
70305097246SAdrian Prantl     // We either don't have any previous frames, or since we have more than one
70405097246SAdrian Prantl     // current frames it means we have all the frames and can safely replace
70505097246SAdrian Prantl     // our previous frames.
7062cad65a5SGreg Clayton     prev_sp.reset(curr_ap.release());
7072cad65a5SGreg Clayton     return;
7082cad65a5SGreg Clayton   }
7092cad65a5SGreg Clayton 
7102cad65a5SGreg Clayton   const uint32_t num_curr_frames = curr_ap->GetNumFrames(false);
7112cad65a5SGreg Clayton 
712b9c1b51eSKate Stone   if (num_curr_frames > 1) {
7132cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES)
714b9c1b51eSKate Stone     s.PutCString(
715b9c1b51eSKate Stone         "We have more than one current frame, so use current frames...\n");
7162cad65a5SGreg Clayton #endif
71705097246SAdrian Prantl     // We have more than one current frames it means we have all the frames and
71805097246SAdrian Prantl     // can safely replace our previous frames.
7192cad65a5SGreg Clayton     prev_sp.reset(curr_ap.release());
7202cad65a5SGreg Clayton 
7212cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES)
7222cad65a5SGreg Clayton     s.PutCString("\nMerged:\n");
7232cad65a5SGreg Clayton     prev_sp->Dump(&s);
7242cad65a5SGreg Clayton #endif
7252cad65a5SGreg Clayton     return;
7262cad65a5SGreg Clayton   }
7272cad65a5SGreg Clayton 
728b57e4a1bSJason Molenda   StackFrameSP prev_frame_zero_sp(prev_sp->GetFrameAtIndex(0));
729b57e4a1bSJason Molenda   StackFrameSP curr_frame_zero_sp(curr_ap->GetFrameAtIndex(0));
7302cad65a5SGreg Clayton   StackID curr_stack_id(curr_frame_zero_sp->GetStackID());
7312cad65a5SGreg Clayton   StackID prev_stack_id(prev_frame_zero_sp->GetStackID());
7322cad65a5SGreg Clayton 
7332cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES)
7342c595273SJohnny Chen   const uint32_t num_prev_frames = prev_sp->GetNumFrames(false);
7352cad65a5SGreg Clayton   s.Printf("\n%u previous frames with one current frame\n", num_prev_frames);
7362cad65a5SGreg Clayton #endif
7372cad65a5SGreg Clayton 
7382cad65a5SGreg Clayton   // We have only a single current frame
7392cad65a5SGreg Clayton   // Our previous stack frames only had a single frame as well...
740b9c1b51eSKate Stone   if (curr_stack_id == prev_stack_id) {
7412cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES)
742b9c1b51eSKate Stone     s.Printf("\nPrevious frame #0 is same as current frame #0, merge the "
743b9c1b51eSKate Stone              "cached data\n");
7442cad65a5SGreg Clayton #endif
7452cad65a5SGreg Clayton 
746b9c1b51eSKate Stone     curr_frame_zero_sp->UpdateCurrentFrameFromPreviousFrame(
747b9c1b51eSKate Stone         *prev_frame_zero_sp);
748b9c1b51eSKate Stone     //        prev_frame_zero_sp->UpdatePreviousFrameFromCurrentFrame
749b9c1b51eSKate Stone     //        (*curr_frame_zero_sp);
7502cad65a5SGreg Clayton     //        prev_sp->SetFrameAtIndex (0, prev_frame_zero_sp);
751b9c1b51eSKate Stone   } else if (curr_stack_id < prev_stack_id) {
7522cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES)
753b9c1b51eSKate Stone     s.Printf("\nCurrent frame #0 has a stack ID that is less than the previous "
754b9c1b51eSKate Stone              "frame #0, insert current frame zero in front of previous\n");
7552cad65a5SGreg Clayton #endif
7562cad65a5SGreg Clayton     prev_sp->m_frames.insert(prev_sp->m_frames.begin(), curr_frame_zero_sp);
7572cad65a5SGreg Clayton   }
7582cad65a5SGreg Clayton 
7592cad65a5SGreg Clayton   curr_ap.release();
7602cad65a5SGreg Clayton 
7612cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES)
7622cad65a5SGreg Clayton   s.PutCString("\nMerged:\n");
7632cad65a5SGreg Clayton   prev_sp->Dump(&s);
7642cad65a5SGreg Clayton #endif
7652cad65a5SGreg Clayton }
766e4284b71SJim Ingham 
767b57e4a1bSJason Molenda lldb::StackFrameSP
768b9c1b51eSKate Stone StackFrameList::GetStackFrameSPForStackFramePtr(StackFrame *stack_frame_ptr) {
769e4284b71SJim Ingham   const_iterator pos;
770e4284b71SJim Ingham   const_iterator begin = m_frames.begin();
771e4284b71SJim Ingham   const_iterator end = m_frames.end();
772b57e4a1bSJason Molenda   lldb::StackFrameSP ret_sp;
773e4284b71SJim Ingham 
774b9c1b51eSKate Stone   for (pos = begin; pos != end; ++pos) {
775b9c1b51eSKate Stone     if (pos->get() == stack_frame_ptr) {
776e4284b71SJim Ingham       ret_sp = (*pos);
777e4284b71SJim Ingham       break;
778e4284b71SJim Ingham     }
779e4284b71SJim Ingham   }
780e4284b71SJim Ingham   return ret_sp;
781e4284b71SJim Ingham }
782e4284b71SJim Ingham 
783b9c1b51eSKate Stone size_t StackFrameList::GetStatus(Stream &strm, uint32_t first_frame,
784b9c1b51eSKate Stone                                  uint32_t num_frames, bool show_frame_info,
7858ec10efcSJim Ingham                                  uint32_t num_frames_with_source,
7867f1c1211SPavel Labath                                  bool show_unique,
787b9c1b51eSKate Stone                                  const char *selected_frame_marker) {
7887260f620SGreg Clayton   size_t num_frames_displayed = 0;
7897260f620SGreg Clayton 
7907260f620SGreg Clayton   if (num_frames == 0)
7917260f620SGreg Clayton     return 0;
7927260f620SGreg Clayton 
793b57e4a1bSJason Molenda   StackFrameSP frame_sp;
7947260f620SGreg Clayton   uint32_t frame_idx = 0;
7957260f620SGreg Clayton   uint32_t last_frame;
7967260f620SGreg Clayton 
7977260f620SGreg Clayton   // Don't let the last frame wrap around...
7987260f620SGreg Clayton   if (num_frames == UINT32_MAX)
7997260f620SGreg Clayton     last_frame = UINT32_MAX;
8007260f620SGreg Clayton   else
8017260f620SGreg Clayton     last_frame = first_frame + num_frames;
8027260f620SGreg Clayton 
803b57e4a1bSJason Molenda   StackFrameSP selected_frame_sp = m_thread.GetSelectedFrame();
804d70a6e71SEugene Zelenko   const char *unselected_marker = nullptr;
8058ec10efcSJim Ingham   std::string buffer;
806b9c1b51eSKate Stone   if (selected_frame_marker) {
8078ec10efcSJim Ingham     size_t len = strlen(selected_frame_marker);
8088ec10efcSJim Ingham     buffer.insert(buffer.begin(), len, ' ');
8098ec10efcSJim Ingham     unselected_marker = buffer.c_str();
8108ec10efcSJim Ingham   }
811d70a6e71SEugene Zelenko   const char *marker = nullptr;
8128ec10efcSJim Ingham 
813b9c1b51eSKate Stone   for (frame_idx = first_frame; frame_idx < last_frame; ++frame_idx) {
8147260f620SGreg Clayton     frame_sp = GetFrameAtIndex(frame_idx);
815d70a6e71SEugene Zelenko     if (!frame_sp)
8167260f620SGreg Clayton       break;
8177260f620SGreg Clayton 
818b9c1b51eSKate Stone     if (selected_frame_marker != nullptr) {
8198ec10efcSJim Ingham       if (frame_sp == selected_frame_sp)
8208ec10efcSJim Ingham         marker = selected_frame_marker;
8218ec10efcSJim Ingham       else
8228ec10efcSJim Ingham         marker = unselected_marker;
8238ec10efcSJim Ingham     }
8248ec10efcSJim Ingham 
825b9c1b51eSKate Stone     if (!frame_sp->GetStatus(strm, show_frame_info,
826b9c1b51eSKate Stone                              num_frames_with_source > (first_frame - frame_idx),
8277f1c1211SPavel Labath                              show_unique, marker))
8287260f620SGreg Clayton       break;
8297260f620SGreg Clayton     ++num_frames_displayed;
8307260f620SGreg Clayton   }
8317260f620SGreg Clayton 
8327260f620SGreg Clayton   strm.IndentLess();
8337260f620SGreg Clayton   return num_frames_displayed;
8347260f620SGreg Clayton }
835