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() {
84bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
8547db4a2cSKeno Fischer 
86b9c1b51eSKate Stone   if (m_show_inlined_frames) {
87513c6bb8SJim Ingham     GetFramesUpTo(0);
88d70a6e71SEugene Zelenko     if (m_frames.empty())
8965d4d5c3SRyan Brown       return;
90b9c1b51eSKate Stone     if (!m_frames[0]->IsInlined()) {
91513c6bb8SJim Ingham       m_current_inlined_depth = UINT32_MAX;
92513c6bb8SJim Ingham       m_current_inlined_pc = LLDB_INVALID_ADDRESS;
935160ce5cSGreg Clayton       Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
946cd41da7SJim Ingham       if (log && log->GetVerbose())
95b9c1b51eSKate Stone         log->Printf(
96b9c1b51eSKate Stone             "ResetCurrentInlinedDepth: Invalidating current inlined depth.\n");
97b9c1b51eSKate Stone     } else {
9805097246SAdrian Prantl       // We only need to do something special about inlined blocks when we are
9905097246SAdrian Prantl       // at the beginning of an inlined function:
100b9c1b51eSKate Stone       // FIXME: We probably also have to do something special if the PC is at
101b9c1b51eSKate Stone       // the END
102b9c1b51eSKate Stone       // of an inlined function, which coincides with the end of either its
10305097246SAdrian Prantl       // containing function or another inlined function.
104513c6bb8SJim Ingham 
105513c6bb8SJim Ingham       lldb::addr_t curr_pc = m_thread.GetRegisterContext()->GetPC();
106513c6bb8SJim Ingham       Block *block_ptr = m_frames[0]->GetFrameBlock();
107b9c1b51eSKate Stone       if (block_ptr) {
108513c6bb8SJim Ingham         Address pc_as_address;
109b9c1b51eSKate Stone         pc_as_address.SetLoadAddress(curr_pc,
110b9c1b51eSKate Stone                                      &(m_thread.GetProcess()->GetTarget()));
111513c6bb8SJim Ingham         AddressRange containing_range;
112b9c1b51eSKate Stone         if (block_ptr->GetRangeContainingAddress(pc_as_address,
113b9c1b51eSKate Stone                                                  containing_range)) {
114b9c1b51eSKate Stone           if (pc_as_address == containing_range.GetBaseAddress()) {
115b9c1b51eSKate Stone             // If we got here because of a breakpoint hit, then set the inlined
11605097246SAdrian Prantl             // depth depending on where the breakpoint was set. If we got here
11705097246SAdrian Prantl             // because of a crash, then set the inlined depth to the deepest
11805097246SAdrian Prantl             // most block. Otherwise, we stopped here naturally as the result
11905097246SAdrian Prantl             // of a step, so set ourselves in the containing frame of the whole
12005097246SAdrian Prantl             // set of nested inlines, so the user can then "virtually" step
12105097246SAdrian Prantl             // into the frames one by one, or next over the whole mess. Note:
12205097246SAdrian Prantl             // We don't have to handle being somewhere in the middle of the
12305097246SAdrian Prantl             // stack here, since ResetCurrentInlinedDepth doesn't get called if
12405097246SAdrian Prantl             // there is a valid inlined depth set.
125513c6bb8SJim Ingham             StopInfoSP stop_info_sp = m_thread.GetStopInfo();
126b9c1b51eSKate Stone             if (stop_info_sp) {
127b9c1b51eSKate Stone               switch (stop_info_sp->GetStopReason()) {
128513c6bb8SJim Ingham               case eStopReasonWatchpoint:
129513c6bb8SJim Ingham               case eStopReasonException:
13090ba8115SGreg Clayton               case eStopReasonExec:
131513c6bb8SJim Ingham               case eStopReasonSignal:
13205097246SAdrian Prantl                 // In all these cases we want to stop in the deepest most
13305097246SAdrian Prantl                 // frame.
134513c6bb8SJim Ingham                 m_current_inlined_pc = curr_pc;
135513c6bb8SJim Ingham                 m_current_inlined_depth = 0;
136513c6bb8SJim Ingham                 break;
137b9c1b51eSKate Stone               case eStopReasonBreakpoint: {
138b9c1b51eSKate Stone                 // FIXME: Figure out what this break point is doing, and set the
139b9c1b51eSKate Stone                 // inline depth
140b9c1b51eSKate Stone                 // appropriately.  Be careful to take into account breakpoints
14105097246SAdrian Prantl                 // that implement step over prologue, since that should do the
14205097246SAdrian Prantl                 // default calculation. For now, if the breakpoints
14305097246SAdrian Prantl                 // corresponding to this hit are all internal,
144b9c1b51eSKate Stone                 // I set the stop location to the top of the inlined stack,
145b9c1b51eSKate Stone                 // since that will make
14605097246SAdrian Prantl                 // things like stepping over prologues work right.  But if
14705097246SAdrian Prantl                 // there are any non-internal breakpoints I do to the bottom of
14805097246SAdrian Prantl                 // the stack, since that was the old behavior.
149c635500dSJim Ingham                 uint32_t bp_site_id = stop_info_sp->GetValue();
150b9c1b51eSKate Stone                 BreakpointSiteSP bp_site_sp(
151b9c1b51eSKate Stone                     m_thread.GetProcess()->GetBreakpointSiteList().FindByID(
152b9c1b51eSKate Stone                         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++) {
157b9c1b51eSKate Stone                     Breakpoint &bp_ref =
158b9c1b51eSKate Stone                         bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint();
159b9c1b51eSKate Stone                     if (!bp_ref.IsInternal()) {
160c635500dSJim Ingham                       all_internal = false;
161c635500dSJim Ingham                     }
162c635500dSJim Ingham                   }
163c635500dSJim Ingham                 }
164b9c1b51eSKate Stone                 if (!all_internal) {
165c635500dSJim Ingham                   m_current_inlined_pc = curr_pc;
166c635500dSJim Ingham                   m_current_inlined_depth = 0;
167c635500dSJim Ingham                   break;
168c635500dSJim Ingham                 }
1697da851a3SJim Ingham               }
170cec91ef9SGreg Clayton                 LLVM_FALLTHROUGH;
171b9c1b51eSKate Stone               default: {
172b9c1b51eSKate Stone                 // Otherwise, we should set ourselves at the container of the
17305097246SAdrian Prantl                 // inlining, so that the user can descend into them. So first
17405097246SAdrian Prantl                 // we check whether we have more than one inlined block sharing
17505097246SAdrian Prantl                 // this PC:
176513c6bb8SJim Ingham                 int num_inlined_functions = 0;
177513c6bb8SJim Ingham 
178513c6bb8SJim Ingham                 for (Block *container_ptr = block_ptr->GetInlinedParent();
179d70a6e71SEugene Zelenko                      container_ptr != nullptr;
180b9c1b51eSKate Stone                      container_ptr = container_ptr->GetInlinedParent()) {
181b9c1b51eSKate Stone                   if (!container_ptr->GetRangeContainingAddress(
182b9c1b51eSKate Stone                           pc_as_address, containing_range))
183513c6bb8SJim Ingham                     break;
184513c6bb8SJim Ingham                   if (pc_as_address != containing_range.GetBaseAddress())
185513c6bb8SJim Ingham                     break;
186513c6bb8SJim Ingham 
187513c6bb8SJim Ingham                   num_inlined_functions++;
188513c6bb8SJim Ingham                 }
189513c6bb8SJim Ingham                 m_current_inlined_pc = curr_pc;
190513c6bb8SJim Ingham                 m_current_inlined_depth = num_inlined_functions + 1;
191b9c1b51eSKate Stone                 Log *log(
192b9c1b51eSKate Stone                     lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
1936cd41da7SJim Ingham                 if (log && log->GetVerbose())
194b9c1b51eSKate Stone                   log->Printf("ResetCurrentInlinedDepth: setting inlined "
195b9c1b51eSKate Stone                               "depth: %d 0x%" PRIx64 ".\n",
196b9c1b51eSKate Stone                               m_current_inlined_depth, curr_pc);
197513c6bb8SJim Ingham 
198b9c1b51eSKate Stone               } break;
199513c6bb8SJim Ingham               }
200513c6bb8SJim Ingham             }
201513c6bb8SJim Ingham           }
202513c6bb8SJim Ingham         }
203513c6bb8SJim Ingham       }
204513c6bb8SJim Ingham     }
205513c6bb8SJim Ingham   }
206513c6bb8SJim Ingham }
207513c6bb8SJim Ingham 
208b9c1b51eSKate Stone bool StackFrameList::DecrementCurrentInlinedDepth() {
209b9c1b51eSKate Stone   if (m_show_inlined_frames) {
210513c6bb8SJim Ingham     uint32_t current_inlined_depth = GetCurrentInlinedDepth();
211b9c1b51eSKate Stone     if (current_inlined_depth != UINT32_MAX) {
212b9c1b51eSKate Stone       if (current_inlined_depth > 0) {
213513c6bb8SJim Ingham         m_current_inlined_depth--;
214513c6bb8SJim Ingham         return true;
215513c6bb8SJim Ingham       }
216513c6bb8SJim Ingham     }
2179786eeebSJim Ingham   }
218513c6bb8SJim Ingham   return false;
219513c6bb8SJim Ingham }
220513c6bb8SJim Ingham 
221b9c1b51eSKate Stone void StackFrameList::SetCurrentInlinedDepth(uint32_t new_depth) {
2226cd41da7SJim Ingham   m_current_inlined_depth = new_depth;
2236cd41da7SJim Ingham   if (new_depth == UINT32_MAX)
2246cd41da7SJim Ingham     m_current_inlined_pc = LLDB_INVALID_ADDRESS;
2256cd41da7SJim Ingham   else
2266cd41da7SJim Ingham     m_current_inlined_pc = m_thread.GetRegisterContext()->GetPC();
2276cd41da7SJim Ingham }
2286cd41da7SJim Ingham 
229b9c1b51eSKate Stone void StackFrameList::GetFramesUpTo(uint32_t end_idx) {
230ebafd2f1SEnrico Granata   // this makes sure we do not fetch frames for an invalid thread
231d70a6e71SEugene Zelenko   if (!m_thread.IsValid())
232ebafd2f1SEnrico Granata     return;
233ebafd2f1SEnrico Granata 
234b9c1b51eSKate Stone   // We've already gotten more frames than asked for, or we've already finished
235b9c1b51eSKate Stone   // unwinding, return.
236b0c72a5fSJim Ingham   if (m_frames.size() > end_idx || GetAllFramesFetched())
237b0c72a5fSJim Ingham     return;
23812daf946SGreg Clayton 
239b0c72a5fSJim Ingham   Unwind *unwinder = m_thread.GetUnwinder();
240b0c72a5fSJim Ingham 
241b9c1b51eSKate Stone   if (m_show_inlined_frames) {
2425082c5fdSGreg Clayton #if defined(DEBUG_STACK_FRAMES)
2432c595273SJohnny Chen     StreamFile s(stdout, false);
2445082c5fdSGreg Clayton #endif
24505097246SAdrian Prantl     // If we are hiding some frames from the outside world, we need to add
24605097246SAdrian Prantl     // those onto the total count of frames to fetch.  However, we don't need
24705097246SAdrian Prantl     // to do that if end_idx is 0 since in that case we always get the first
24805097246SAdrian Prantl     // concrete frame and all the inlined frames below it...  And of course, if
24905097246SAdrian Prantl     // end_idx is UINT32_MAX that means get all, so just do that...
250513c6bb8SJim Ingham 
251513c6bb8SJim Ingham     uint32_t inlined_depth = 0;
252b9c1b51eSKate Stone     if (end_idx > 0 && end_idx != UINT32_MAX) {
253513c6bb8SJim Ingham       inlined_depth = GetCurrentInlinedDepth();
254b9c1b51eSKate Stone       if (inlined_depth != UINT32_MAX) {
255513c6bb8SJim Ingham         if (end_idx > 0)
256513c6bb8SJim Ingham           end_idx += inlined_depth;
257513c6bb8SJim Ingham       }
258513c6bb8SJim Ingham     }
25912daf946SGreg Clayton 
260b57e4a1bSJason Molenda     StackFrameSP unwind_frame_sp;
261b9c1b51eSKate Stone     do {
262b0c72a5fSJim Ingham       uint32_t idx = m_concrete_frames_fetched++;
2638012cadbSGreg Clayton       lldb::addr_t pc = LLDB_INVALID_ADDRESS;
2648012cadbSGreg Clayton       lldb::addr_t cfa = LLDB_INVALID_ADDRESS;
265b9c1b51eSKate Stone       if (idx == 0) {
26605097246SAdrian Prantl         // We might have already created frame zero, only create it if we need
26705097246SAdrian Prantl         // to
268b9c1b51eSKate Stone         if (m_frames.empty()) {
269b3ae8761SGreg Clayton           RegisterContextSP reg_ctx_sp(m_thread.GetRegisterContext());
270b3ae8761SGreg Clayton 
271b9c1b51eSKate Stone           if (reg_ctx_sp) {
272b9c1b51eSKate Stone             const bool success =
273b9c1b51eSKate Stone                 unwinder && unwinder->GetFrameInfoAtIndex(idx, cfa, pc);
27405097246SAdrian Prantl             // There shouldn't be any way not to get the frame info for frame
27505097246SAdrian Prantl             // 0. But if the unwinder can't make one, lets make one by hand
27605097246SAdrian Prantl             // with the
277376c4854SJim Ingham             // SP as the CFA and see if that gets any further.
278b9c1b51eSKate Stone             if (!success) {
279b3ae8761SGreg Clayton               cfa = reg_ctx_sp->GetSP();
280b3ae8761SGreg Clayton               pc = reg_ctx_sp->GetPC();
281376c4854SJim Ingham             }
282376c4854SJim Ingham 
283d9e416c0SGreg Clayton             unwind_frame_sp.reset(new StackFrame(m_thread.shared_from_this(),
284b9c1b51eSKate Stone                                                  m_frames.size(), idx,
285b9c1b51eSKate Stone                                                  reg_ctx_sp, cfa, pc, nullptr));
2865082c5fdSGreg Clayton             m_frames.push_back(unwind_frame_sp);
2875082c5fdSGreg Clayton           }
288b9c1b51eSKate Stone         } else {
2895082c5fdSGreg Clayton           unwind_frame_sp = m_frames.front();
290b57e4a1bSJason Molenda           cfa = unwind_frame_sp->m_id.GetCallFrameAddress();
2915082c5fdSGreg Clayton         }
292b9c1b51eSKate Stone       } else {
293b9c1b51eSKate Stone         const bool success =
294b9c1b51eSKate Stone             unwinder && unwinder->GetFrameInfoAtIndex(idx, cfa, pc);
295b9c1b51eSKate Stone         if (!success) {
296b0c72a5fSJim Ingham           // We've gotten to the end of the stack.
297b0c72a5fSJim Ingham           SetAllFramesFetched();
298b0c72a5fSJim Ingham           break;
299b0c72a5fSJim Ingham         }
30099618476SJason Molenda         const bool cfa_is_valid = true;
30199618476SJason Molenda         const bool stop_id_is_valid = false;
30299618476SJason Molenda         const bool is_history_frame = false;
303b9c1b51eSKate Stone         unwind_frame_sp.reset(new StackFrame(
304b9c1b51eSKate Stone             m_thread.shared_from_this(), m_frames.size(), idx, cfa,
305b9c1b51eSKate Stone             cfa_is_valid, pc, 0, stop_id_is_valid, is_history_frame, nullptr));
3065082c5fdSGreg Clayton         m_frames.push_back(unwind_frame_sp);
30712daf946SGreg Clayton       }
30812daf946SGreg Clayton 
309ff1b5c42SEd Maste       assert(unwind_frame_sp);
310b9c1b51eSKate Stone       SymbolContext unwind_sc = unwind_frame_sp->GetSymbolContext(
311b9c1b51eSKate Stone           eSymbolContextBlock | eSymbolContextFunction);
3121ed54f50SGreg Clayton       Block *unwind_block = unwind_sc.block;
313b9c1b51eSKate Stone       if (unwind_block) {
3145f4c61e2SGreg Clayton         Address curr_frame_address(unwind_frame_sp->GetFrameCodeAddress());
315911d5784STed Woodward         TargetSP target_sp = m_thread.CalculateTarget();
31605097246SAdrian Prantl         // Be sure to adjust the frame address to match the address that was
31705097246SAdrian Prantl         // used to lookup the symbol context above. If we are in the first
31805097246SAdrian Prantl         // concrete frame, then we lookup using the current address, else we
31905097246SAdrian Prantl         // decrement the address by one to get the correct location.
320b9c1b51eSKate Stone         if (idx > 0) {
321b9c1b51eSKate Stone           if (curr_frame_address.GetOffset() == 0) {
322b9c1b51eSKate Stone             // If curr_frame_address points to the first address in a section
32305097246SAdrian Prantl             // then after adjustment it will point to an other section. In that
32405097246SAdrian Prantl             // case resolve the address again to the correct section plus
32505097246SAdrian Prantl             // offset form.
326b9c1b51eSKate Stone             addr_t load_addr = curr_frame_address.GetOpcodeLoadAddress(
327*04803b3eSTatyana Krasnukha                 target_sp.get(), AddressClass::eCode);
328b9c1b51eSKate Stone             curr_frame_address.SetOpcodeLoadAddress(
329*04803b3eSTatyana Krasnukha                 load_addr - 1, target_sp.get(), AddressClass::eCode);
330b9c1b51eSKate Stone           } else {
3315f4c61e2SGreg Clayton             curr_frame_address.Slide(-1);
3320f8452baSTamas Berghammer           }
3330f8452baSTamas Berghammer         }
3345f4c61e2SGreg Clayton 
3351ed54f50SGreg Clayton         SymbolContext next_frame_sc;
3361ed54f50SGreg Clayton         Address next_frame_address;
3371ed54f50SGreg Clayton 
338b9c1b51eSKate Stone         while (unwind_sc.GetParentOfInlinedScope(
339b9c1b51eSKate Stone             curr_frame_address, next_frame_sc, next_frame_address)) {
340911d5784STed Woodward           next_frame_sc.line_entry.ApplyFileMappings(target_sp);
341b9c1b51eSKate Stone           StackFrameSP frame_sp(
342b9c1b51eSKate Stone               new StackFrame(m_thread.shared_from_this(), m_frames.size(), idx,
343b9c1b51eSKate Stone                              unwind_frame_sp->GetRegisterContextSP(), cfa,
344b9c1b51eSKate Stone                              next_frame_address, &next_frame_sc));
3455082c5fdSGreg Clayton 
3465082c5fdSGreg Clayton           m_frames.push_back(frame_sp);
3471ed54f50SGreg Clayton           unwind_sc = next_frame_sc;
3481ed54f50SGreg Clayton           curr_frame_address = next_frame_address;
349b0c72a5fSJim Ingham         }
350b0c72a5fSJim Ingham       }
351b0c72a5fSJim Ingham     } while (m_frames.size() - 1 < end_idx);
3521ed54f50SGreg Clayton 
353b0c72a5fSJim Ingham     // Don't try to merge till you've calculated all the frames in this stack.
354b9c1b51eSKate Stone     if (GetAllFramesFetched() && m_prev_frames_sp) {
3552cad65a5SGreg Clayton       StackFrameList *prev_frames = m_prev_frames_sp.get();
3565082c5fdSGreg Clayton       StackFrameList *curr_frames = this;
3575082c5fdSGreg Clayton 
3586cd41da7SJim Ingham // curr_frames->m_current_inlined_depth = prev_frames->m_current_inlined_depth;
3596cd41da7SJim Ingham // curr_frames->m_current_inlined_pc = prev_frames->m_current_inlined_pc;
360b9c1b51eSKate Stone // printf ("GetFramesUpTo: Copying current inlined depth: %d 0x%" PRIx64 ".\n",
361b9c1b51eSKate Stone // curr_frames->m_current_inlined_depth, curr_frames->m_current_inlined_pc);
3629786eeebSJim Ingham 
3635082c5fdSGreg Clayton #if defined(DEBUG_STACK_FRAMES)
36468275d5eSGreg Clayton       s.PutCString("\nprev_frames:\n");
3655082c5fdSGreg Clayton       prev_frames->Dump(&s);
36668275d5eSGreg Clayton       s.PutCString("\ncurr_frames:\n");
3675082c5fdSGreg Clayton       curr_frames->Dump(&s);
3685082c5fdSGreg Clayton       s.EOL();
3695082c5fdSGreg Clayton #endif
3705082c5fdSGreg Clayton       size_t curr_frame_num, prev_frame_num;
3715082c5fdSGreg Clayton 
372b9c1b51eSKate Stone       for (curr_frame_num = curr_frames->m_frames.size(),
373b9c1b51eSKate Stone           prev_frame_num = prev_frames->m_frames.size();
3745082c5fdSGreg Clayton            curr_frame_num > 0 && prev_frame_num > 0;
375b9c1b51eSKate Stone            --curr_frame_num, --prev_frame_num) {
3765082c5fdSGreg Clayton         const size_t curr_frame_idx = curr_frame_num - 1;
3775082c5fdSGreg Clayton         const size_t prev_frame_idx = prev_frame_num - 1;
378b57e4a1bSJason Molenda         StackFrameSP curr_frame_sp(curr_frames->m_frames[curr_frame_idx]);
379b57e4a1bSJason Molenda         StackFrameSP prev_frame_sp(prev_frames->m_frames[prev_frame_idx]);
3805082c5fdSGreg Clayton 
3815082c5fdSGreg Clayton #if defined(DEBUG_STACK_FRAMES)
3822cad65a5SGreg Clayton         s.Printf("\n\nCurr frame #%u ", curr_frame_idx);
3835082c5fdSGreg Clayton         if (curr_frame_sp)
3842cad65a5SGreg Clayton           curr_frame_sp->Dump(&s, true, false);
3855082c5fdSGreg Clayton         else
3865082c5fdSGreg Clayton           s.PutCString("NULL");
3872cad65a5SGreg Clayton         s.Printf("\nPrev frame #%u ", prev_frame_idx);
3885082c5fdSGreg Clayton         if (prev_frame_sp)
3892cad65a5SGreg Clayton           prev_frame_sp->Dump(&s, true, false);
3905082c5fdSGreg Clayton         else
3915082c5fdSGreg Clayton           s.PutCString("NULL");
3925082c5fdSGreg Clayton #endif
3935082c5fdSGreg Clayton 
394b57e4a1bSJason Molenda         StackFrame *curr_frame = curr_frame_sp.get();
395b57e4a1bSJason Molenda         StackFrame *prev_frame = prev_frame_sp.get();
3965082c5fdSGreg Clayton 
397d70a6e71SEugene Zelenko         if (curr_frame == nullptr || prev_frame == nullptr)
3985082c5fdSGreg Clayton           break;
3995082c5fdSGreg Clayton 
40059e8fc1cSGreg Clayton         // Check the stack ID to make sure they are equal
40159e8fc1cSGreg Clayton         if (curr_frame->GetStackID() != prev_frame->GetStackID())
4025082c5fdSGreg Clayton           break;
4035082c5fdSGreg Clayton 
40459e8fc1cSGreg Clayton         prev_frame->UpdatePreviousFrameFromCurrentFrame(*curr_frame);
40505097246SAdrian Prantl         // Now copy the fixed up previous frame into the current frames so the
40605097246SAdrian Prantl         // pointer doesn't change
40759e8fc1cSGreg Clayton         m_frames[curr_frame_idx] = prev_frame_sp;
40859e8fc1cSGreg Clayton // curr_frame->UpdateCurrentFrameFromPreviousFrame (*prev_frame);
4095082c5fdSGreg Clayton 
4105082c5fdSGreg Clayton #if defined(DEBUG_STACK_FRAMES)
41168275d5eSGreg Clayton         s.Printf("\n    Copying previous frame to current frame");
4125082c5fdSGreg Clayton #endif
4135082c5fdSGreg Clayton       }
4145082c5fdSGreg Clayton       // We are done with the old stack frame list, we can release it now
4152cad65a5SGreg Clayton       m_prev_frames_sp.reset();
4165082c5fdSGreg Clayton     }
41768275d5eSGreg Clayton 
41868275d5eSGreg Clayton #if defined(DEBUG_STACK_FRAMES)
41968275d5eSGreg Clayton     s.PutCString("\n\nNew frames:\n");
42068275d5eSGreg Clayton     Dump(&s);
42168275d5eSGreg Clayton     s.EOL();
42268275d5eSGreg Clayton #endif
423b9c1b51eSKate Stone   } else {
424b0c72a5fSJim Ingham     if (end_idx < m_concrete_frames_fetched)
425b0c72a5fSJim Ingham       return;
426b0c72a5fSJim Ingham 
427b9c1b51eSKate Stone     if (unwinder) {
428b0c72a5fSJim Ingham       uint32_t num_frames = unwinder->GetFramesUpTo(end_idx);
429b9c1b51eSKate Stone       if (num_frames <= end_idx + 1) {
430b0c72a5fSJim Ingham         // Done unwinding.
431b0c72a5fSJim Ingham         m_concrete_frames_fetched = UINT32_MAX;
432b0c72a5fSJim Ingham       }
433b0c72a5fSJim Ingham       m_frames.resize(num_frames);
43412daf946SGreg Clayton     }
4355082c5fdSGreg Clayton   }
436ec6829eaSGreg Clayton }
437b0c72a5fSJim Ingham 
438b9c1b51eSKate Stone uint32_t StackFrameList::GetNumFrames(bool can_create) {
439bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
440b0c72a5fSJim Ingham 
441b0c72a5fSJim Ingham   if (can_create)
442b0c72a5fSJim Ingham     GetFramesUpTo(UINT32_MAX);
443513c6bb8SJim Ingham 
444513c6bb8SJim Ingham   uint32_t inlined_depth = GetCurrentInlinedDepth();
445513c6bb8SJim Ingham   if (inlined_depth == UINT32_MAX)
4465082c5fdSGreg Clayton     return m_frames.size();
447513c6bb8SJim Ingham   else
448513c6bb8SJim Ingham     return m_frames.size() - inlined_depth;
44930fdc8d8SChris Lattner }
45030fdc8d8SChris Lattner 
451b9c1b51eSKate Stone void StackFrameList::Dump(Stream *s) {
452d70a6e71SEugene Zelenko   if (s == nullptr)
4535082c5fdSGreg Clayton     return;
454bb19a13cSSaleem Abdulrasool 
455bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
45630fdc8d8SChris Lattner 
4575082c5fdSGreg Clayton   const_iterator pos, begin = m_frames.begin(), end = m_frames.end();
458b9c1b51eSKate Stone   for (pos = begin; pos != end; ++pos) {
459b57e4a1bSJason Molenda     StackFrame *frame = (*pos).get();
460324a1036SSaleem Abdulrasool     s->Printf("%p: ", static_cast<void *>(frame));
461b9c1b51eSKate Stone     if (frame) {
46259e8fc1cSGreg Clayton       frame->GetStackID().Dump(s);
4630603aa9dSGreg Clayton       frame->DumpUsingSettingsFormat(s);
464b9c1b51eSKate Stone     } else
465dce502edSGreg Clayton       s->Printf("frame #%u", (uint32_t)std::distance(begin, pos));
4665082c5fdSGreg Clayton     s->EOL();
46712daf946SGreg Clayton   }
4685082c5fdSGreg Clayton   s->EOL();
4695082c5fdSGreg Clayton }
47012daf946SGreg Clayton 
471b9c1b51eSKate Stone StackFrameSP StackFrameList::GetFrameAtIndex(uint32_t idx) {
472b57e4a1bSJason Molenda   StackFrameSP frame_sp;
473bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
4745cb9a184SJim Ingham   uint32_t original_idx = idx;
4755cb9a184SJim Ingham 
476513c6bb8SJim Ingham   uint32_t inlined_depth = GetCurrentInlinedDepth();
477513c6bb8SJim Ingham   if (inlined_depth != UINT32_MAX)
478513c6bb8SJim Ingham     idx += inlined_depth;
479513c6bb8SJim Ingham 
4805082c5fdSGreg Clayton   if (idx < m_frames.size())
4815082c5fdSGreg Clayton     frame_sp = m_frames[idx];
48212daf946SGreg Clayton 
4835082c5fdSGreg Clayton   if (frame_sp)
48412daf946SGreg Clayton     return frame_sp;
48512daf946SGreg Clayton 
48605097246SAdrian Prantl   // GetFramesUpTo will fill m_frames with as many frames as you asked for, if
48705097246SAdrian Prantl   // there are that many.  If there weren't then you asked for too many frames.
488b0c72a5fSJim Ingham   GetFramesUpTo(idx);
489b9c1b51eSKate Stone   if (idx < m_frames.size()) {
490b9c1b51eSKate Stone     if (m_show_inlined_frames) {
491b9c1b51eSKate Stone       // When inline frames are enabled we actually create all the frames in
492b9c1b51eSKate Stone       // GetFramesUpTo.
4935082c5fdSGreg Clayton       frame_sp = m_frames[idx];
494b9c1b51eSKate Stone     } else {
49512daf946SGreg Clayton       Unwind *unwinder = m_thread.GetUnwinder();
496b9c1b51eSKate Stone       if (unwinder) {
49712daf946SGreg Clayton         addr_t pc, cfa;
498b9c1b51eSKate Stone         if (unwinder->GetFrameInfoAtIndex(idx, cfa, pc)) {
49999618476SJason Molenda           const bool cfa_is_valid = true;
50099618476SJason Molenda           const bool stop_id_is_valid = false;
50199618476SJason Molenda           const bool is_history_frame = false;
502b9c1b51eSKate Stone           frame_sp.reset(new StackFrame(
503b9c1b51eSKate Stone               m_thread.shared_from_this(), idx, idx, cfa, cfa_is_valid, pc, 0,
504d70a6e71SEugene Zelenko               stop_id_is_valid, is_history_frame, nullptr));
50559e8fc1cSGreg Clayton 
506b9c1b51eSKate Stone           Function *function =
507b9c1b51eSKate Stone               frame_sp->GetSymbolContext(eSymbolContextFunction).function;
508b9c1b51eSKate Stone           if (function) {
50905097246SAdrian Prantl             // When we aren't showing inline functions we always use the top
51005097246SAdrian Prantl             // most function block as the scope.
51159e8fc1cSGreg Clayton             frame_sp->SetSymbolContextScope(&function->GetBlock(false));
512b9c1b51eSKate Stone           } else {
513b9c1b51eSKate Stone             // Set the symbol scope from the symbol regardless if it is nullptr
514b9c1b51eSKate Stone             // or valid.
515b9c1b51eSKate Stone             frame_sp->SetSymbolContextScope(
516b9c1b51eSKate Stone                 frame_sp->GetSymbolContext(eSymbolContextSymbol).symbol);
51759e8fc1cSGreg Clayton           }
5185082c5fdSGreg Clayton           SetFrameAtIndex(idx, frame_sp);
51912daf946SGreg Clayton         }
52012daf946SGreg Clayton       }
52112daf946SGreg Clayton     }
522b9c1b51eSKate Stone   } else if (original_idx == 0) {
523b9c1b51eSKate Stone     // There should ALWAYS be a frame at index 0.  If something went wrong with
52405097246SAdrian Prantl     // the CurrentInlinedDepth such that there weren't as many frames as we
52505097246SAdrian Prantl     // thought taking that into account, then reset the current inlined depth
5265cb9a184SJim Ingham     // and return the real zeroth frame.
527b9c1b51eSKate Stone     if (m_frames.empty()) {
528b9c1b51eSKate Stone       // Why do we have a thread with zero frames, that should not ever
529b9c1b51eSKate Stone       // happen...
530a322f36cSDavid Blaikie       assert(!m_thread.IsValid() && "A valid thread has no frames.");
531b9c1b51eSKate Stone     } else {
532d70a6e71SEugene Zelenko       ResetCurrentInlinedDepth();
533d70a6e71SEugene Zelenko       frame_sp = m_frames[original_idx];
5345cb9a184SJim Ingham     }
5355cb9a184SJim Ingham   }
5365cb9a184SJim Ingham 
53730fdc8d8SChris Lattner   return frame_sp;
53830fdc8d8SChris Lattner }
53930fdc8d8SChris Lattner 
540b57e4a1bSJason Molenda StackFrameSP
541b9c1b51eSKate Stone StackFrameList::GetFrameWithConcreteFrameIndex(uint32_t unwind_idx) {
5425ccbd294SGreg Clayton   // First try assuming the unwind index is the same as the frame index. The
54305097246SAdrian Prantl   // unwind index is always greater than or equal to the frame index, so it is
54405097246SAdrian Prantl   // a good place to start. If we have inlined frames we might have 5 concrete
54505097246SAdrian Prantl   // frames (frame unwind indexes go from 0-4), but we might have 15 frames
54605097246SAdrian Prantl   // after we make all the inlined frames. Most of the time the unwind frame
54705097246SAdrian Prantl   // index (or the concrete frame index) is the same as the frame index.
5485ccbd294SGreg Clayton   uint32_t frame_idx = unwind_idx;
549b57e4a1bSJason Molenda   StackFrameSP frame_sp(GetFrameAtIndex(frame_idx));
550b9c1b51eSKate Stone   while (frame_sp) {
5515ccbd294SGreg Clayton     if (frame_sp->GetFrameIndex() == unwind_idx)
5525ccbd294SGreg Clayton       break;
5535ccbd294SGreg Clayton     frame_sp = GetFrameAtIndex(++frame_idx);
5545ccbd294SGreg Clayton   }
5555ccbd294SGreg Clayton   return frame_sp;
5565ccbd294SGreg Clayton }
5575ccbd294SGreg Clayton 
558b9c1b51eSKate Stone static bool CompareStackID(const StackFrameSP &stack_sp,
559b9c1b51eSKate Stone                            const StackID &stack_id) {
5607bcb93d5SGreg Clayton   return stack_sp->GetStackID() < stack_id;
5617bcb93d5SGreg Clayton }
5627bcb93d5SGreg Clayton 
563b9c1b51eSKate Stone StackFrameSP StackFrameList::GetFrameWithStackID(const StackID &stack_id) {
564b57e4a1bSJason Molenda   StackFrameSP frame_sp;
5657bcb93d5SGreg Clayton 
566b9c1b51eSKate Stone   if (stack_id.IsValid()) {
567bb19a13cSSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(m_mutex);
5687bcb93d5SGreg Clayton     uint32_t frame_idx = 0;
5697bcb93d5SGreg Clayton     // Do a binary search in case the stack frame is already in our cache
5707bcb93d5SGreg Clayton     collection::const_iterator begin = m_frames.begin();
5717bcb93d5SGreg Clayton     collection::const_iterator end = m_frames.end();
572b9c1b51eSKate Stone     if (begin != end) {
573b9c1b51eSKate Stone       collection::const_iterator pos =
574b9c1b51eSKate Stone           std::lower_bound(begin, end, stack_id, CompareStackID);
575b9c1b51eSKate Stone       if (pos != end) {
5768012cadbSGreg Clayton         if ((*pos)->GetStackID() == stack_id)
5777bcb93d5SGreg Clayton           return *pos;
5788012cadbSGreg Clayton       }
5797bcb93d5SGreg Clayton 
5808012cadbSGreg Clayton       //            if (m_frames.back()->GetStackID() < stack_id)
5818012cadbSGreg Clayton       //                frame_idx = m_frames.size();
5827bcb93d5SGreg Clayton     }
583b9c1b51eSKate Stone     do {
5843a195b7eSJim Ingham       frame_sp = GetFrameAtIndex(frame_idx);
5853a195b7eSJim Ingham       if (frame_sp && frame_sp->GetStackID() == stack_id)
5863a195b7eSJim Ingham         break;
5873a195b7eSJim Ingham       frame_idx++;
588b9c1b51eSKate Stone     } while (frame_sp);
5897bcb93d5SGreg Clayton   }
5903a195b7eSJim Ingham   return frame_sp;
5913a195b7eSJim Ingham }
5925ccbd294SGreg Clayton 
593b9c1b51eSKate Stone bool StackFrameList::SetFrameAtIndex(uint32_t idx, StackFrameSP &frame_sp) {
5945082c5fdSGreg Clayton   if (idx >= m_frames.size())
5955082c5fdSGreg Clayton     m_frames.resize(idx + 1);
59612daf946SGreg Clayton   // Make sure allocation succeeded by checking bounds again
597b9c1b51eSKate Stone   if (idx < m_frames.size()) {
5985082c5fdSGreg Clayton     m_frames[idx] = frame_sp;
59930fdc8d8SChris Lattner     return true;
60030fdc8d8SChris Lattner   }
60130fdc8d8SChris Lattner   return false; // resize failed, out of memory?
60230fdc8d8SChris Lattner }
60330fdc8d8SChris Lattner 
604b9c1b51eSKate Stone uint32_t StackFrameList::GetSelectedFrameIndex() const {
605bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
6062976d00aSJim Ingham   return m_selected_frame_idx;
60730fdc8d8SChris Lattner }
60830fdc8d8SChris Lattner 
609b9c1b51eSKate Stone uint32_t StackFrameList::SetSelectedFrame(lldb_private::StackFrame *frame) {
610bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
61112daf946SGreg Clayton   const_iterator pos;
6125082c5fdSGreg Clayton   const_iterator begin = m_frames.begin();
6135082c5fdSGreg Clayton   const_iterator end = m_frames.end();
614b7f6b2faSJim Ingham   m_selected_frame_idx = 0;
615b9c1b51eSKate Stone   for (pos = begin; pos != end; ++pos) {
616b9c1b51eSKate Stone     if (pos->get() == frame) {
6172976d00aSJim Ingham       m_selected_frame_idx = std::distance(begin, pos);
618513c6bb8SJim Ingham       uint32_t inlined_depth = GetCurrentInlinedDepth();
619513c6bb8SJim Ingham       if (inlined_depth != UINT32_MAX)
620513c6bb8SJim Ingham         m_selected_frame_idx -= inlined_depth;
621b7f6b2faSJim Ingham       break;
62230fdc8d8SChris Lattner     }
62330fdc8d8SChris Lattner   }
624b7f6b2faSJim Ingham   SetDefaultFileAndLineToSelectedFrame();
6252976d00aSJim Ingham   return m_selected_frame_idx;
62630fdc8d8SChris Lattner }
62730fdc8d8SChris Lattner 
62830fdc8d8SChris Lattner // Mark a stack frame as the current frame using the frame index
629b9c1b51eSKate Stone bool StackFrameList::SetSelectedFrameByIndex(uint32_t idx) {
630bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
631b57e4a1bSJason Molenda   StackFrameSP frame_sp(GetFrameAtIndex(idx));
632b9c1b51eSKate Stone   if (frame_sp) {
633b0c72a5fSJim Ingham     SetSelectedFrame(frame_sp.get());
634b0c72a5fSJim Ingham     return true;
635b9c1b51eSKate Stone   } else
636b0c72a5fSJim Ingham     return false;
637b7f6b2faSJim Ingham }
638b7f6b2faSJim Ingham 
639b9c1b51eSKate Stone void StackFrameList::SetDefaultFileAndLineToSelectedFrame() {
640b9c1b51eSKate Stone   if (m_thread.GetID() ==
641b9c1b51eSKate Stone       m_thread.GetProcess()->GetThreadList().GetSelectedThread()->GetID()) {
642b57e4a1bSJason Molenda     StackFrameSP frame_sp(GetFrameAtIndex(GetSelectedFrameIndex()));
643b9c1b51eSKate Stone     if (frame_sp) {
644252d0edeSGreg Clayton       SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextLineEntry);
645b7f6b2faSJim Ingham       if (sc.line_entry.file)
646b9c1b51eSKate Stone         m_thread.CalculateTarget()->GetSourceManager().SetDefaultFileAndLine(
647b9c1b51eSKate Stone             sc.line_entry.file, sc.line_entry.line);
648b7f6b2faSJim Ingham     }
649b7f6b2faSJim Ingham   }
65030fdc8d8SChris Lattner }
65130fdc8d8SChris Lattner 
65230fdc8d8SChris Lattner // The thread has been run, reset the number stack frames to zero so we can
65330fdc8d8SChris Lattner // determine how many frames we have lazily.
654b9c1b51eSKate Stone void StackFrameList::Clear() {
655bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
6565082c5fdSGreg Clayton   m_frames.clear();
657b0c72a5fSJim Ingham   m_concrete_frames_fetched = 0;
65830fdc8d8SChris Lattner }
65930fdc8d8SChris Lattner 
660b9c1b51eSKate Stone void StackFrameList::InvalidateFrames(uint32_t start_idx) {
661bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
662b9c1b51eSKate Stone   if (m_show_inlined_frames) {
66312daf946SGreg Clayton     Clear();
664b9c1b51eSKate Stone   } else {
6655082c5fdSGreg Clayton     const size_t num_frames = m_frames.size();
666b9c1b51eSKate Stone     while (start_idx < num_frames) {
6675082c5fdSGreg Clayton       m_frames[start_idx].reset();
66830fdc8d8SChris Lattner       ++start_idx;
66930fdc8d8SChris Lattner     }
67030fdc8d8SChris Lattner   }
67112daf946SGreg Clayton }
6722cad65a5SGreg Clayton 
673b9c1b51eSKate Stone void StackFrameList::Merge(std::unique_ptr<StackFrameList> &curr_ap,
674b9c1b51eSKate Stone                            lldb::StackFrameListSP &prev_sp) {
675bb19a13cSSaleem Abdulrasool   std::unique_lock<std::recursive_mutex> current_lock, previous_lock;
676bb19a13cSSaleem Abdulrasool   if (curr_ap)
677bb19a13cSSaleem Abdulrasool     current_lock = std::unique_lock<std::recursive_mutex>(curr_ap->m_mutex);
678bb19a13cSSaleem Abdulrasool   if (prev_sp)
679bb19a13cSSaleem Abdulrasool     previous_lock = std::unique_lock<std::recursive_mutex>(prev_sp->m_mutex);
6802cad65a5SGreg Clayton 
6812cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES)
6822c595273SJohnny Chen   StreamFile s(stdout, false);
6832cad65a5SGreg Clayton   s.PutCString("\n\nStackFrameList::Merge():\nPrev:\n");
684d70a6e71SEugene Zelenko   if (prev_sp)
6852cad65a5SGreg Clayton     prev_sp->Dump(&s);
6862cad65a5SGreg Clayton   else
6872cad65a5SGreg Clayton     s.PutCString("NULL");
6882cad65a5SGreg Clayton   s.PutCString("\nCurr:\n");
689d70a6e71SEugene Zelenko   if (curr_ap)
6902cad65a5SGreg Clayton     curr_ap->Dump(&s);
6912cad65a5SGreg Clayton   else
6922cad65a5SGreg Clayton     s.PutCString("NULL");
6932cad65a5SGreg Clayton   s.EOL();
6942cad65a5SGreg Clayton #endif
6952cad65a5SGreg Clayton 
696b9c1b51eSKate Stone   if (!curr_ap || curr_ap->GetNumFrames(false) == 0) {
6972cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES)
6982cad65a5SGreg Clayton     s.PutCString("No current frames, leave previous frames alone...\n");
6992cad65a5SGreg Clayton #endif
7002cad65a5SGreg Clayton     curr_ap.release();
7012cad65a5SGreg Clayton     return;
7022cad65a5SGreg Clayton   }
7032cad65a5SGreg Clayton 
704b9c1b51eSKate Stone   if (!prev_sp || prev_sp->GetNumFrames(false) == 0) {
7052cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES)
7062cad65a5SGreg Clayton     s.PutCString("No previous frames, so use current frames...\n");
7072cad65a5SGreg Clayton #endif
70805097246SAdrian Prantl     // We either don't have any previous frames, or since we have more than one
70905097246SAdrian Prantl     // current frames it means we have all the frames and can safely replace
71005097246SAdrian Prantl     // our previous frames.
7112cad65a5SGreg Clayton     prev_sp.reset(curr_ap.release());
7122cad65a5SGreg Clayton     return;
7132cad65a5SGreg Clayton   }
7142cad65a5SGreg Clayton 
7152cad65a5SGreg Clayton   const uint32_t num_curr_frames = curr_ap->GetNumFrames(false);
7162cad65a5SGreg Clayton 
717b9c1b51eSKate Stone   if (num_curr_frames > 1) {
7182cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES)
719b9c1b51eSKate Stone     s.PutCString(
720b9c1b51eSKate Stone         "We have more than one current frame, so use current frames...\n");
7212cad65a5SGreg Clayton #endif
72205097246SAdrian Prantl     // We have more than one current frames it means we have all the frames and
72305097246SAdrian Prantl     // can safely replace our previous frames.
7242cad65a5SGreg Clayton     prev_sp.reset(curr_ap.release());
7252cad65a5SGreg Clayton 
7262cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES)
7272cad65a5SGreg Clayton     s.PutCString("\nMerged:\n");
7282cad65a5SGreg Clayton     prev_sp->Dump(&s);
7292cad65a5SGreg Clayton #endif
7302cad65a5SGreg Clayton     return;
7312cad65a5SGreg Clayton   }
7322cad65a5SGreg Clayton 
733b57e4a1bSJason Molenda   StackFrameSP prev_frame_zero_sp(prev_sp->GetFrameAtIndex(0));
734b57e4a1bSJason Molenda   StackFrameSP curr_frame_zero_sp(curr_ap->GetFrameAtIndex(0));
7352cad65a5SGreg Clayton   StackID curr_stack_id(curr_frame_zero_sp->GetStackID());
7362cad65a5SGreg Clayton   StackID prev_stack_id(prev_frame_zero_sp->GetStackID());
7372cad65a5SGreg Clayton 
7382cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES)
7392c595273SJohnny Chen   const uint32_t num_prev_frames = prev_sp->GetNumFrames(false);
7402cad65a5SGreg Clayton   s.Printf("\n%u previous frames with one current frame\n", num_prev_frames);
7412cad65a5SGreg Clayton #endif
7422cad65a5SGreg Clayton 
7432cad65a5SGreg Clayton   // We have only a single current frame
7442cad65a5SGreg Clayton   // Our previous stack frames only had a single frame as well...
745b9c1b51eSKate Stone   if (curr_stack_id == prev_stack_id) {
7462cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES)
747b9c1b51eSKate Stone     s.Printf("\nPrevious frame #0 is same as current frame #0, merge the "
748b9c1b51eSKate Stone              "cached data\n");
7492cad65a5SGreg Clayton #endif
7502cad65a5SGreg Clayton 
751b9c1b51eSKate Stone     curr_frame_zero_sp->UpdateCurrentFrameFromPreviousFrame(
752b9c1b51eSKate Stone         *prev_frame_zero_sp);
753b9c1b51eSKate Stone     //        prev_frame_zero_sp->UpdatePreviousFrameFromCurrentFrame
754b9c1b51eSKate Stone     //        (*curr_frame_zero_sp);
7552cad65a5SGreg Clayton     //        prev_sp->SetFrameAtIndex (0, prev_frame_zero_sp);
756b9c1b51eSKate Stone   } else if (curr_stack_id < prev_stack_id) {
7572cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES)
758b9c1b51eSKate Stone     s.Printf("\nCurrent frame #0 has a stack ID that is less than the previous "
759b9c1b51eSKate Stone              "frame #0, insert current frame zero in front of previous\n");
7602cad65a5SGreg Clayton #endif
7612cad65a5SGreg Clayton     prev_sp->m_frames.insert(prev_sp->m_frames.begin(), curr_frame_zero_sp);
7622cad65a5SGreg Clayton   }
7632cad65a5SGreg Clayton 
7642cad65a5SGreg Clayton   curr_ap.release();
7652cad65a5SGreg Clayton 
7662cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES)
7672cad65a5SGreg Clayton   s.PutCString("\nMerged:\n");
7682cad65a5SGreg Clayton   prev_sp->Dump(&s);
7692cad65a5SGreg Clayton #endif
7702cad65a5SGreg Clayton }
771e4284b71SJim Ingham 
772b57e4a1bSJason Molenda lldb::StackFrameSP
773b9c1b51eSKate Stone StackFrameList::GetStackFrameSPForStackFramePtr(StackFrame *stack_frame_ptr) {
774e4284b71SJim Ingham   const_iterator pos;
775e4284b71SJim Ingham   const_iterator begin = m_frames.begin();
776e4284b71SJim Ingham   const_iterator end = m_frames.end();
777b57e4a1bSJason Molenda   lldb::StackFrameSP ret_sp;
778e4284b71SJim Ingham 
779b9c1b51eSKate Stone   for (pos = begin; pos != end; ++pos) {
780b9c1b51eSKate Stone     if (pos->get() == stack_frame_ptr) {
781e4284b71SJim Ingham       ret_sp = (*pos);
782e4284b71SJim Ingham       break;
783e4284b71SJim Ingham     }
784e4284b71SJim Ingham   }
785e4284b71SJim Ingham   return ret_sp;
786e4284b71SJim Ingham }
787e4284b71SJim Ingham 
788b9c1b51eSKate Stone size_t StackFrameList::GetStatus(Stream &strm, uint32_t first_frame,
789b9c1b51eSKate Stone                                  uint32_t num_frames, bool show_frame_info,
7908ec10efcSJim Ingham                                  uint32_t num_frames_with_source,
7917f1c1211SPavel Labath                                  bool show_unique,
792b9c1b51eSKate Stone                                  const char *selected_frame_marker) {
7937260f620SGreg Clayton   size_t num_frames_displayed = 0;
7947260f620SGreg Clayton 
7957260f620SGreg Clayton   if (num_frames == 0)
7967260f620SGreg Clayton     return 0;
7977260f620SGreg Clayton 
798b57e4a1bSJason Molenda   StackFrameSP frame_sp;
7997260f620SGreg Clayton   uint32_t frame_idx = 0;
8007260f620SGreg Clayton   uint32_t last_frame;
8017260f620SGreg Clayton 
8027260f620SGreg Clayton   // Don't let the last frame wrap around...
8037260f620SGreg Clayton   if (num_frames == UINT32_MAX)
8047260f620SGreg Clayton     last_frame = UINT32_MAX;
8057260f620SGreg Clayton   else
8067260f620SGreg Clayton     last_frame = first_frame + num_frames;
8077260f620SGreg Clayton 
808b57e4a1bSJason Molenda   StackFrameSP selected_frame_sp = m_thread.GetSelectedFrame();
809d70a6e71SEugene Zelenko   const char *unselected_marker = nullptr;
8108ec10efcSJim Ingham   std::string buffer;
811b9c1b51eSKate Stone   if (selected_frame_marker) {
8128ec10efcSJim Ingham     size_t len = strlen(selected_frame_marker);
8138ec10efcSJim Ingham     buffer.insert(buffer.begin(), len, ' ');
8148ec10efcSJim Ingham     unselected_marker = buffer.c_str();
8158ec10efcSJim Ingham   }
816d70a6e71SEugene Zelenko   const char *marker = nullptr;
8178ec10efcSJim Ingham 
818b9c1b51eSKate Stone   for (frame_idx = first_frame; frame_idx < last_frame; ++frame_idx) {
8197260f620SGreg Clayton     frame_sp = GetFrameAtIndex(frame_idx);
820d70a6e71SEugene Zelenko     if (!frame_sp)
8217260f620SGreg Clayton       break;
8227260f620SGreg Clayton 
823b9c1b51eSKate Stone     if (selected_frame_marker != nullptr) {
8248ec10efcSJim Ingham       if (frame_sp == selected_frame_sp)
8258ec10efcSJim Ingham         marker = selected_frame_marker;
8268ec10efcSJim Ingham       else
8278ec10efcSJim Ingham         marker = unselected_marker;
8288ec10efcSJim Ingham     }
8298ec10efcSJim Ingham 
830b9c1b51eSKate Stone     if (!frame_sp->GetStatus(strm, show_frame_info,
831b9c1b51eSKate Stone                              num_frames_with_source > (first_frame - frame_idx),
8327f1c1211SPavel Labath                              show_unique, marker))
8337260f620SGreg Clayton       break;
8347260f620SGreg Clayton     ++num_frames_displayed;
8357260f620SGreg Clayton   }
8367260f620SGreg Clayton 
8377260f620SGreg Clayton   strm.IndentLess();
8387260f620SGreg Clayton   return num_frames_displayed;
8397260f620SGreg Clayton }
840