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 
229*eb8fa58eSVedant Kumar void StackFrameList::GetOnlyConcreteFramesUpTo(uint32_t end_idx,
230*eb8fa58eSVedant Kumar                                                Unwind *unwinder) {
231*eb8fa58eSVedant Kumar   assert(m_thread.IsValid() && "Expected valid thread");
232*eb8fa58eSVedant Kumar   assert(m_frames.size() <= end_idx && "Expected there to be frames to fill");
233*eb8fa58eSVedant Kumar 
234*eb8fa58eSVedant Kumar   if (end_idx < m_concrete_frames_fetched)
235*eb8fa58eSVedant Kumar     return;
236*eb8fa58eSVedant Kumar 
237*eb8fa58eSVedant Kumar   if (!unwinder)
238*eb8fa58eSVedant Kumar     return;
239*eb8fa58eSVedant Kumar 
240*eb8fa58eSVedant Kumar   uint32_t num_frames = unwinder->GetFramesUpTo(end_idx);
241*eb8fa58eSVedant Kumar   if (num_frames <= end_idx + 1) {
242*eb8fa58eSVedant Kumar     // Done unwinding.
243*eb8fa58eSVedant Kumar     m_concrete_frames_fetched = UINT32_MAX;
244*eb8fa58eSVedant Kumar   }
245*eb8fa58eSVedant Kumar   m_frames.resize(num_frames);
246*eb8fa58eSVedant Kumar }
247*eb8fa58eSVedant Kumar 
248b9c1b51eSKate Stone void StackFrameList::GetFramesUpTo(uint32_t end_idx) {
249*eb8fa58eSVedant Kumar   // Do not fetch frames for an invalid thread.
250d70a6e71SEugene Zelenko   if (!m_thread.IsValid())
251ebafd2f1SEnrico Granata     return;
252ebafd2f1SEnrico Granata 
253b9c1b51eSKate Stone   // We've already gotten more frames than asked for, or we've already finished
254b9c1b51eSKate Stone   // unwinding, return.
255b0c72a5fSJim Ingham   if (m_frames.size() > end_idx || GetAllFramesFetched())
256b0c72a5fSJim Ingham     return;
25712daf946SGreg Clayton 
258b0c72a5fSJim Ingham   Unwind *unwinder = m_thread.GetUnwinder();
259b0c72a5fSJim Ingham 
260*eb8fa58eSVedant Kumar   if (!m_show_inlined_frames) {
261*eb8fa58eSVedant Kumar     GetOnlyConcreteFramesUpTo(end_idx, unwinder);
262*eb8fa58eSVedant Kumar     return;
263*eb8fa58eSVedant Kumar   }
264*eb8fa58eSVedant Kumar 
2655082c5fdSGreg Clayton #if defined(DEBUG_STACK_FRAMES)
2662c595273SJohnny Chen   StreamFile s(stdout, false);
2675082c5fdSGreg Clayton #endif
26805097246SAdrian Prantl   // If we are hiding some frames from the outside world, we need to add
26905097246SAdrian Prantl   // those onto the total count of frames to fetch.  However, we don't need
27005097246SAdrian Prantl   // to do that if end_idx is 0 since in that case we always get the first
27105097246SAdrian Prantl   // concrete frame and all the inlined frames below it...  And of course, if
27205097246SAdrian Prantl   // end_idx is UINT32_MAX that means get all, so just do that...
273513c6bb8SJim Ingham 
274513c6bb8SJim Ingham   uint32_t inlined_depth = 0;
275b9c1b51eSKate Stone   if (end_idx > 0 && end_idx != UINT32_MAX) {
276513c6bb8SJim Ingham     inlined_depth = GetCurrentInlinedDepth();
277b9c1b51eSKate Stone     if (inlined_depth != UINT32_MAX) {
278513c6bb8SJim Ingham       if (end_idx > 0)
279513c6bb8SJim Ingham         end_idx += inlined_depth;
280513c6bb8SJim Ingham     }
281513c6bb8SJim Ingham   }
28212daf946SGreg Clayton 
283b57e4a1bSJason Molenda   StackFrameSP unwind_frame_sp;
284b9c1b51eSKate Stone   do {
285b0c72a5fSJim Ingham     uint32_t idx = m_concrete_frames_fetched++;
2868012cadbSGreg Clayton     lldb::addr_t pc = LLDB_INVALID_ADDRESS;
2878012cadbSGreg Clayton     lldb::addr_t cfa = LLDB_INVALID_ADDRESS;
288b9c1b51eSKate Stone     if (idx == 0) {
28905097246SAdrian Prantl       // We might have already created frame zero, only create it if we need
290*eb8fa58eSVedant Kumar       // to.
291b9c1b51eSKate Stone       if (m_frames.empty()) {
292b3ae8761SGreg Clayton         RegisterContextSP reg_ctx_sp(m_thread.GetRegisterContext());
293b3ae8761SGreg Clayton 
294b9c1b51eSKate Stone         if (reg_ctx_sp) {
295b9c1b51eSKate Stone           const bool success =
296b9c1b51eSKate Stone               unwinder && unwinder->GetFrameInfoAtIndex(idx, cfa, pc);
29705097246SAdrian Prantl           // There shouldn't be any way not to get the frame info for frame
29805097246SAdrian Prantl           // 0. But if the unwinder can't make one, lets make one by hand
299*eb8fa58eSVedant Kumar           // with the SP as the CFA and see if that gets any further.
300b9c1b51eSKate Stone           if (!success) {
301b3ae8761SGreg Clayton             cfa = reg_ctx_sp->GetSP();
302b3ae8761SGreg Clayton             pc = reg_ctx_sp->GetPC();
303376c4854SJim Ingham           }
304376c4854SJim Ingham 
305d9e416c0SGreg Clayton           unwind_frame_sp.reset(new StackFrame(m_thread.shared_from_this(),
306*eb8fa58eSVedant Kumar                                                m_frames.size(), idx, reg_ctx_sp,
307*eb8fa58eSVedant Kumar                                                cfa, pc, nullptr));
3085082c5fdSGreg Clayton           m_frames.push_back(unwind_frame_sp);
3095082c5fdSGreg Clayton         }
310b9c1b51eSKate Stone       } else {
3115082c5fdSGreg Clayton         unwind_frame_sp = m_frames.front();
312b57e4a1bSJason Molenda         cfa = unwind_frame_sp->m_id.GetCallFrameAddress();
3135082c5fdSGreg Clayton       }
314b9c1b51eSKate Stone     } else {
315b9c1b51eSKate Stone       const bool success =
316b9c1b51eSKate Stone           unwinder && unwinder->GetFrameInfoAtIndex(idx, cfa, pc);
317b9c1b51eSKate Stone       if (!success) {
318b0c72a5fSJim Ingham         // We've gotten to the end of the stack.
319b0c72a5fSJim Ingham         SetAllFramesFetched();
320b0c72a5fSJim Ingham         break;
321b0c72a5fSJim Ingham       }
32299618476SJason Molenda       const bool cfa_is_valid = true;
32399618476SJason Molenda       const bool stop_id_is_valid = false;
32499618476SJason Molenda       const bool is_history_frame = false;
325b9c1b51eSKate Stone       unwind_frame_sp.reset(new StackFrame(
326*eb8fa58eSVedant Kumar           m_thread.shared_from_this(), m_frames.size(), idx, cfa, cfa_is_valid,
327*eb8fa58eSVedant Kumar           pc, 0, stop_id_is_valid, is_history_frame, nullptr));
3285082c5fdSGreg Clayton       m_frames.push_back(unwind_frame_sp);
32912daf946SGreg Clayton     }
33012daf946SGreg Clayton 
331ff1b5c42SEd Maste     assert(unwind_frame_sp);
332b9c1b51eSKate Stone     SymbolContext unwind_sc = unwind_frame_sp->GetSymbolContext(
333b9c1b51eSKate Stone         eSymbolContextBlock | eSymbolContextFunction);
3341ed54f50SGreg Clayton     Block *unwind_block = unwind_sc.block;
335b9c1b51eSKate Stone     if (unwind_block) {
3365f4c61e2SGreg Clayton       Address curr_frame_address(unwind_frame_sp->GetFrameCodeAddress());
337911d5784STed Woodward       TargetSP target_sp = m_thread.CalculateTarget();
33805097246SAdrian Prantl       // Be sure to adjust the frame address to match the address that was
33905097246SAdrian Prantl       // used to lookup the symbol context above. If we are in the first
34005097246SAdrian Prantl       // concrete frame, then we lookup using the current address, else we
34105097246SAdrian Prantl       // decrement the address by one to get the correct location.
342b9c1b51eSKate Stone       if (idx > 0) {
343b9c1b51eSKate Stone         if (curr_frame_address.GetOffset() == 0) {
344b9c1b51eSKate Stone           // If curr_frame_address points to the first address in a section
34505097246SAdrian Prantl           // then after adjustment it will point to an other section. In that
34605097246SAdrian Prantl           // case resolve the address again to the correct section plus
34705097246SAdrian Prantl           // offset form.
348b9c1b51eSKate Stone           addr_t load_addr = curr_frame_address.GetOpcodeLoadAddress(
34904803b3eSTatyana Krasnukha               target_sp.get(), AddressClass::eCode);
350b9c1b51eSKate Stone           curr_frame_address.SetOpcodeLoadAddress(
35104803b3eSTatyana Krasnukha               load_addr - 1, target_sp.get(), AddressClass::eCode);
352b9c1b51eSKate Stone         } else {
3535f4c61e2SGreg Clayton           curr_frame_address.Slide(-1);
3540f8452baSTamas Berghammer         }
3550f8452baSTamas Berghammer       }
3565f4c61e2SGreg Clayton 
3571ed54f50SGreg Clayton       SymbolContext next_frame_sc;
3581ed54f50SGreg Clayton       Address next_frame_address;
3591ed54f50SGreg Clayton 
360b9c1b51eSKate Stone       while (unwind_sc.GetParentOfInlinedScope(
361b9c1b51eSKate Stone           curr_frame_address, next_frame_sc, next_frame_address)) {
362911d5784STed Woodward         next_frame_sc.line_entry.ApplyFileMappings(target_sp);
363b9c1b51eSKate Stone         StackFrameSP frame_sp(
364b9c1b51eSKate Stone             new StackFrame(m_thread.shared_from_this(), m_frames.size(), idx,
365b9c1b51eSKate Stone                            unwind_frame_sp->GetRegisterContextSP(), cfa,
366b9c1b51eSKate Stone                            next_frame_address, &next_frame_sc));
3675082c5fdSGreg Clayton 
3685082c5fdSGreg Clayton         m_frames.push_back(frame_sp);
3691ed54f50SGreg Clayton         unwind_sc = next_frame_sc;
3701ed54f50SGreg Clayton         curr_frame_address = next_frame_address;
371b0c72a5fSJim Ingham       }
372b0c72a5fSJim Ingham     }
373b0c72a5fSJim Ingham   } while (m_frames.size() - 1 < end_idx);
3741ed54f50SGreg Clayton 
375b0c72a5fSJim Ingham   // Don't try to merge till you've calculated all the frames in this stack.
376b9c1b51eSKate Stone   if (GetAllFramesFetched() && m_prev_frames_sp) {
3772cad65a5SGreg Clayton     StackFrameList *prev_frames = m_prev_frames_sp.get();
3785082c5fdSGreg Clayton     StackFrameList *curr_frames = this;
3795082c5fdSGreg Clayton 
3805082c5fdSGreg Clayton #if defined(DEBUG_STACK_FRAMES)
38168275d5eSGreg Clayton     s.PutCString("\nprev_frames:\n");
3825082c5fdSGreg Clayton     prev_frames->Dump(&s);
38368275d5eSGreg Clayton     s.PutCString("\ncurr_frames:\n");
3845082c5fdSGreg Clayton     curr_frames->Dump(&s);
3855082c5fdSGreg Clayton     s.EOL();
3865082c5fdSGreg Clayton #endif
3875082c5fdSGreg Clayton     size_t curr_frame_num, prev_frame_num;
3885082c5fdSGreg Clayton 
389b9c1b51eSKate Stone     for (curr_frame_num = curr_frames->m_frames.size(),
390b9c1b51eSKate Stone         prev_frame_num = prev_frames->m_frames.size();
3915082c5fdSGreg Clayton          curr_frame_num > 0 && prev_frame_num > 0;
392b9c1b51eSKate Stone          --curr_frame_num, --prev_frame_num) {
3935082c5fdSGreg Clayton       const size_t curr_frame_idx = curr_frame_num - 1;
3945082c5fdSGreg Clayton       const size_t prev_frame_idx = prev_frame_num - 1;
395b57e4a1bSJason Molenda       StackFrameSP curr_frame_sp(curr_frames->m_frames[curr_frame_idx]);
396b57e4a1bSJason Molenda       StackFrameSP prev_frame_sp(prev_frames->m_frames[prev_frame_idx]);
3975082c5fdSGreg Clayton 
3985082c5fdSGreg Clayton #if defined(DEBUG_STACK_FRAMES)
3992cad65a5SGreg Clayton       s.Printf("\n\nCurr frame #%u ", curr_frame_idx);
4005082c5fdSGreg Clayton       if (curr_frame_sp)
4012cad65a5SGreg Clayton         curr_frame_sp->Dump(&s, true, false);
4025082c5fdSGreg Clayton       else
4035082c5fdSGreg Clayton         s.PutCString("NULL");
4042cad65a5SGreg Clayton       s.Printf("\nPrev frame #%u ", prev_frame_idx);
4055082c5fdSGreg Clayton       if (prev_frame_sp)
4062cad65a5SGreg Clayton         prev_frame_sp->Dump(&s, true, false);
4075082c5fdSGreg Clayton       else
4085082c5fdSGreg Clayton         s.PutCString("NULL");
4095082c5fdSGreg Clayton #endif
4105082c5fdSGreg Clayton 
411b57e4a1bSJason Molenda       StackFrame *curr_frame = curr_frame_sp.get();
412b57e4a1bSJason Molenda       StackFrame *prev_frame = prev_frame_sp.get();
4135082c5fdSGreg Clayton 
414d70a6e71SEugene Zelenko       if (curr_frame == nullptr || prev_frame == nullptr)
4155082c5fdSGreg Clayton         break;
4165082c5fdSGreg Clayton 
417*eb8fa58eSVedant Kumar       // Check the stack ID to make sure they are equal.
41859e8fc1cSGreg Clayton       if (curr_frame->GetStackID() != prev_frame->GetStackID())
4195082c5fdSGreg Clayton         break;
4205082c5fdSGreg Clayton 
42159e8fc1cSGreg Clayton       prev_frame->UpdatePreviousFrameFromCurrentFrame(*curr_frame);
42205097246SAdrian Prantl       // Now copy the fixed up previous frame into the current frames so the
423*eb8fa58eSVedant Kumar       // pointer doesn't change.
42459e8fc1cSGreg Clayton       m_frames[curr_frame_idx] = prev_frame_sp;
4255082c5fdSGreg Clayton 
4265082c5fdSGreg Clayton #if defined(DEBUG_STACK_FRAMES)
42768275d5eSGreg Clayton       s.Printf("\n    Copying previous frame to current frame");
4285082c5fdSGreg Clayton #endif
4295082c5fdSGreg Clayton     }
430*eb8fa58eSVedant Kumar     // We are done with the old stack frame list, we can release it now.
4312cad65a5SGreg Clayton     m_prev_frames_sp.reset();
4325082c5fdSGreg Clayton   }
43368275d5eSGreg Clayton 
43468275d5eSGreg Clayton #if defined(DEBUG_STACK_FRAMES)
43568275d5eSGreg Clayton   s.PutCString("\n\nNew frames:\n");
43668275d5eSGreg Clayton   Dump(&s);
43768275d5eSGreg Clayton   s.EOL();
43868275d5eSGreg Clayton #endif
439ec6829eaSGreg Clayton }
440b0c72a5fSJim Ingham 
441b9c1b51eSKate Stone uint32_t StackFrameList::GetNumFrames(bool can_create) {
442bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
443b0c72a5fSJim Ingham 
444b0c72a5fSJim Ingham   if (can_create)
445b0c72a5fSJim Ingham     GetFramesUpTo(UINT32_MAX);
446513c6bb8SJim Ingham 
447513c6bb8SJim Ingham   uint32_t inlined_depth = GetCurrentInlinedDepth();
448513c6bb8SJim Ingham   if (inlined_depth == UINT32_MAX)
4495082c5fdSGreg Clayton     return m_frames.size();
450513c6bb8SJim Ingham   else
451513c6bb8SJim Ingham     return m_frames.size() - inlined_depth;
45230fdc8d8SChris Lattner }
45330fdc8d8SChris Lattner 
454b9c1b51eSKate Stone void StackFrameList::Dump(Stream *s) {
455d70a6e71SEugene Zelenko   if (s == nullptr)
4565082c5fdSGreg Clayton     return;
457bb19a13cSSaleem Abdulrasool 
458bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
45930fdc8d8SChris Lattner 
4605082c5fdSGreg Clayton   const_iterator pos, begin = m_frames.begin(), end = m_frames.end();
461b9c1b51eSKate Stone   for (pos = begin; pos != end; ++pos) {
462b57e4a1bSJason Molenda     StackFrame *frame = (*pos).get();
463324a1036SSaleem Abdulrasool     s->Printf("%p: ", static_cast<void *>(frame));
464b9c1b51eSKate Stone     if (frame) {
46559e8fc1cSGreg Clayton       frame->GetStackID().Dump(s);
4660603aa9dSGreg Clayton       frame->DumpUsingSettingsFormat(s);
467b9c1b51eSKate Stone     } else
468dce502edSGreg Clayton       s->Printf("frame #%u", (uint32_t)std::distance(begin, pos));
4695082c5fdSGreg Clayton     s->EOL();
47012daf946SGreg Clayton   }
4715082c5fdSGreg Clayton   s->EOL();
4725082c5fdSGreg Clayton }
47312daf946SGreg Clayton 
474b9c1b51eSKate Stone StackFrameSP StackFrameList::GetFrameAtIndex(uint32_t idx) {
475b57e4a1bSJason Molenda   StackFrameSP frame_sp;
476bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
4775cb9a184SJim Ingham   uint32_t original_idx = idx;
4785cb9a184SJim Ingham 
479513c6bb8SJim Ingham   uint32_t inlined_depth = GetCurrentInlinedDepth();
480513c6bb8SJim Ingham   if (inlined_depth != UINT32_MAX)
481513c6bb8SJim Ingham     idx += inlined_depth;
482513c6bb8SJim Ingham 
4835082c5fdSGreg Clayton   if (idx < m_frames.size())
4845082c5fdSGreg Clayton     frame_sp = m_frames[idx];
48512daf946SGreg Clayton 
4865082c5fdSGreg Clayton   if (frame_sp)
48712daf946SGreg Clayton     return frame_sp;
48812daf946SGreg Clayton 
48905097246SAdrian Prantl   // GetFramesUpTo will fill m_frames with as many frames as you asked for, if
49005097246SAdrian Prantl   // there are that many.  If there weren't then you asked for too many frames.
491b0c72a5fSJim Ingham   GetFramesUpTo(idx);
492b9c1b51eSKate Stone   if (idx < m_frames.size()) {
493b9c1b51eSKate Stone     if (m_show_inlined_frames) {
494b9c1b51eSKate Stone       // When inline frames are enabled we actually create all the frames in
495b9c1b51eSKate Stone       // GetFramesUpTo.
4965082c5fdSGreg Clayton       frame_sp = m_frames[idx];
497b9c1b51eSKate Stone     } else {
49812daf946SGreg Clayton       Unwind *unwinder = m_thread.GetUnwinder();
499b9c1b51eSKate Stone       if (unwinder) {
50012daf946SGreg Clayton         addr_t pc, cfa;
501b9c1b51eSKate Stone         if (unwinder->GetFrameInfoAtIndex(idx, cfa, pc)) {
50299618476SJason Molenda           const bool cfa_is_valid = true;
50399618476SJason Molenda           const bool stop_id_is_valid = false;
50499618476SJason Molenda           const bool is_history_frame = false;
505b9c1b51eSKate Stone           frame_sp.reset(new StackFrame(
506b9c1b51eSKate Stone               m_thread.shared_from_this(), idx, idx, cfa, cfa_is_valid, pc, 0,
507d70a6e71SEugene Zelenko               stop_id_is_valid, is_history_frame, nullptr));
50859e8fc1cSGreg Clayton 
509b9c1b51eSKate Stone           Function *function =
510b9c1b51eSKate Stone               frame_sp->GetSymbolContext(eSymbolContextFunction).function;
511b9c1b51eSKate Stone           if (function) {
51205097246SAdrian Prantl             // When we aren't showing inline functions we always use the top
51305097246SAdrian Prantl             // most function block as the scope.
51459e8fc1cSGreg Clayton             frame_sp->SetSymbolContextScope(&function->GetBlock(false));
515b9c1b51eSKate Stone           } else {
516b9c1b51eSKate Stone             // Set the symbol scope from the symbol regardless if it is nullptr
517b9c1b51eSKate Stone             // or valid.
518b9c1b51eSKate Stone             frame_sp->SetSymbolContextScope(
519b9c1b51eSKate Stone                 frame_sp->GetSymbolContext(eSymbolContextSymbol).symbol);
52059e8fc1cSGreg Clayton           }
5215082c5fdSGreg Clayton           SetFrameAtIndex(idx, frame_sp);
52212daf946SGreg Clayton         }
52312daf946SGreg Clayton       }
52412daf946SGreg Clayton     }
525b9c1b51eSKate Stone   } else if (original_idx == 0) {
526b9c1b51eSKate Stone     // There should ALWAYS be a frame at index 0.  If something went wrong with
52705097246SAdrian Prantl     // the CurrentInlinedDepth such that there weren't as many frames as we
52805097246SAdrian Prantl     // thought taking that into account, then reset the current inlined depth
5295cb9a184SJim Ingham     // and return the real zeroth frame.
530b9c1b51eSKate Stone     if (m_frames.empty()) {
531b9c1b51eSKate Stone       // Why do we have a thread with zero frames, that should not ever
532b9c1b51eSKate Stone       // happen...
533a322f36cSDavid Blaikie       assert(!m_thread.IsValid() && "A valid thread has no frames.");
534b9c1b51eSKate Stone     } else {
535d70a6e71SEugene Zelenko       ResetCurrentInlinedDepth();
536d70a6e71SEugene Zelenko       frame_sp = m_frames[original_idx];
5375cb9a184SJim Ingham     }
5385cb9a184SJim Ingham   }
5395cb9a184SJim Ingham 
54030fdc8d8SChris Lattner   return frame_sp;
54130fdc8d8SChris Lattner }
54230fdc8d8SChris Lattner 
543b57e4a1bSJason Molenda StackFrameSP
544b9c1b51eSKate Stone StackFrameList::GetFrameWithConcreteFrameIndex(uint32_t unwind_idx) {
5455ccbd294SGreg Clayton   // First try assuming the unwind index is the same as the frame index. The
54605097246SAdrian Prantl   // unwind index is always greater than or equal to the frame index, so it is
54705097246SAdrian Prantl   // a good place to start. If we have inlined frames we might have 5 concrete
54805097246SAdrian Prantl   // frames (frame unwind indexes go from 0-4), but we might have 15 frames
54905097246SAdrian Prantl   // after we make all the inlined frames. Most of the time the unwind frame
55005097246SAdrian Prantl   // index (or the concrete frame index) is the same as the frame index.
5515ccbd294SGreg Clayton   uint32_t frame_idx = unwind_idx;
552b57e4a1bSJason Molenda   StackFrameSP frame_sp(GetFrameAtIndex(frame_idx));
553b9c1b51eSKate Stone   while (frame_sp) {
5545ccbd294SGreg Clayton     if (frame_sp->GetFrameIndex() == unwind_idx)
5555ccbd294SGreg Clayton       break;
5565ccbd294SGreg Clayton     frame_sp = GetFrameAtIndex(++frame_idx);
5575ccbd294SGreg Clayton   }
5585ccbd294SGreg Clayton   return frame_sp;
5595ccbd294SGreg Clayton }
5605ccbd294SGreg Clayton 
561b9c1b51eSKate Stone static bool CompareStackID(const StackFrameSP &stack_sp,
562b9c1b51eSKate Stone                            const StackID &stack_id) {
5637bcb93d5SGreg Clayton   return stack_sp->GetStackID() < stack_id;
5647bcb93d5SGreg Clayton }
5657bcb93d5SGreg Clayton 
566b9c1b51eSKate Stone StackFrameSP StackFrameList::GetFrameWithStackID(const StackID &stack_id) {
567b57e4a1bSJason Molenda   StackFrameSP frame_sp;
5687bcb93d5SGreg Clayton 
569b9c1b51eSKate Stone   if (stack_id.IsValid()) {
570bb19a13cSSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(m_mutex);
5717bcb93d5SGreg Clayton     uint32_t frame_idx = 0;
5727bcb93d5SGreg Clayton     // Do a binary search in case the stack frame is already in our cache
5737bcb93d5SGreg Clayton     collection::const_iterator begin = m_frames.begin();
5747bcb93d5SGreg Clayton     collection::const_iterator end = m_frames.end();
575b9c1b51eSKate Stone     if (begin != end) {
576b9c1b51eSKate Stone       collection::const_iterator pos =
577b9c1b51eSKate Stone           std::lower_bound(begin, end, stack_id, CompareStackID);
578b9c1b51eSKate Stone       if (pos != end) {
5798012cadbSGreg Clayton         if ((*pos)->GetStackID() == stack_id)
5807bcb93d5SGreg Clayton           return *pos;
5818012cadbSGreg Clayton       }
5827bcb93d5SGreg Clayton 
5838012cadbSGreg Clayton       //            if (m_frames.back()->GetStackID() < stack_id)
5848012cadbSGreg Clayton       //                frame_idx = m_frames.size();
5857bcb93d5SGreg Clayton     }
586b9c1b51eSKate Stone     do {
5873a195b7eSJim Ingham       frame_sp = GetFrameAtIndex(frame_idx);
5883a195b7eSJim Ingham       if (frame_sp && frame_sp->GetStackID() == stack_id)
5893a195b7eSJim Ingham         break;
5903a195b7eSJim Ingham       frame_idx++;
591b9c1b51eSKate Stone     } while (frame_sp);
5927bcb93d5SGreg Clayton   }
5933a195b7eSJim Ingham   return frame_sp;
5943a195b7eSJim Ingham }
5955ccbd294SGreg Clayton 
596b9c1b51eSKate Stone bool StackFrameList::SetFrameAtIndex(uint32_t idx, StackFrameSP &frame_sp) {
5975082c5fdSGreg Clayton   if (idx >= m_frames.size())
5985082c5fdSGreg Clayton     m_frames.resize(idx + 1);
59912daf946SGreg Clayton   // Make sure allocation succeeded by checking bounds again
600b9c1b51eSKate Stone   if (idx < m_frames.size()) {
6015082c5fdSGreg Clayton     m_frames[idx] = frame_sp;
60230fdc8d8SChris Lattner     return true;
60330fdc8d8SChris Lattner   }
60430fdc8d8SChris Lattner   return false; // resize failed, out of memory?
60530fdc8d8SChris Lattner }
60630fdc8d8SChris Lattner 
607b9c1b51eSKate Stone uint32_t StackFrameList::GetSelectedFrameIndex() const {
608bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
6092976d00aSJim Ingham   return m_selected_frame_idx;
61030fdc8d8SChris Lattner }
61130fdc8d8SChris Lattner 
612b9c1b51eSKate Stone uint32_t StackFrameList::SetSelectedFrame(lldb_private::StackFrame *frame) {
613bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
61412daf946SGreg Clayton   const_iterator pos;
6155082c5fdSGreg Clayton   const_iterator begin = m_frames.begin();
6165082c5fdSGreg Clayton   const_iterator end = m_frames.end();
617b7f6b2faSJim Ingham   m_selected_frame_idx = 0;
618b9c1b51eSKate Stone   for (pos = begin; pos != end; ++pos) {
619b9c1b51eSKate Stone     if (pos->get() == frame) {
6202976d00aSJim Ingham       m_selected_frame_idx = std::distance(begin, pos);
621513c6bb8SJim Ingham       uint32_t inlined_depth = GetCurrentInlinedDepth();
622513c6bb8SJim Ingham       if (inlined_depth != UINT32_MAX)
623513c6bb8SJim Ingham         m_selected_frame_idx -= inlined_depth;
624b7f6b2faSJim Ingham       break;
62530fdc8d8SChris Lattner     }
62630fdc8d8SChris Lattner   }
627b7f6b2faSJim Ingham   SetDefaultFileAndLineToSelectedFrame();
6282976d00aSJim Ingham   return m_selected_frame_idx;
62930fdc8d8SChris Lattner }
63030fdc8d8SChris Lattner 
63130fdc8d8SChris Lattner // Mark a stack frame as the current frame using the frame index
632b9c1b51eSKate Stone bool StackFrameList::SetSelectedFrameByIndex(uint32_t idx) {
633bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
634b57e4a1bSJason Molenda   StackFrameSP frame_sp(GetFrameAtIndex(idx));
635b9c1b51eSKate Stone   if (frame_sp) {
636b0c72a5fSJim Ingham     SetSelectedFrame(frame_sp.get());
637b0c72a5fSJim Ingham     return true;
638b9c1b51eSKate Stone   } else
639b0c72a5fSJim Ingham     return false;
640b7f6b2faSJim Ingham }
641b7f6b2faSJim Ingham 
642b9c1b51eSKate Stone void StackFrameList::SetDefaultFileAndLineToSelectedFrame() {
643b9c1b51eSKate Stone   if (m_thread.GetID() ==
644b9c1b51eSKate Stone       m_thread.GetProcess()->GetThreadList().GetSelectedThread()->GetID()) {
645b57e4a1bSJason Molenda     StackFrameSP frame_sp(GetFrameAtIndex(GetSelectedFrameIndex()));
646b9c1b51eSKate Stone     if (frame_sp) {
647252d0edeSGreg Clayton       SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextLineEntry);
648b7f6b2faSJim Ingham       if (sc.line_entry.file)
649b9c1b51eSKate Stone         m_thread.CalculateTarget()->GetSourceManager().SetDefaultFileAndLine(
650b9c1b51eSKate Stone             sc.line_entry.file, sc.line_entry.line);
651b7f6b2faSJim Ingham     }
652b7f6b2faSJim Ingham   }
65330fdc8d8SChris Lattner }
65430fdc8d8SChris Lattner 
65530fdc8d8SChris Lattner // The thread has been run, reset the number stack frames to zero so we can
65630fdc8d8SChris Lattner // determine how many frames we have lazily.
657b9c1b51eSKate Stone void StackFrameList::Clear() {
658bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
6595082c5fdSGreg Clayton   m_frames.clear();
660b0c72a5fSJim Ingham   m_concrete_frames_fetched = 0;
66130fdc8d8SChris Lattner }
66230fdc8d8SChris Lattner 
663b9c1b51eSKate Stone void StackFrameList::InvalidateFrames(uint32_t start_idx) {
664bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
665b9c1b51eSKate Stone   if (m_show_inlined_frames) {
66612daf946SGreg Clayton     Clear();
667b9c1b51eSKate Stone   } else {
6685082c5fdSGreg Clayton     const size_t num_frames = m_frames.size();
669b9c1b51eSKate Stone     while (start_idx < num_frames) {
6705082c5fdSGreg Clayton       m_frames[start_idx].reset();
67130fdc8d8SChris Lattner       ++start_idx;
67230fdc8d8SChris Lattner     }
67330fdc8d8SChris Lattner   }
67412daf946SGreg Clayton }
6752cad65a5SGreg Clayton 
676b9c1b51eSKate Stone void StackFrameList::Merge(std::unique_ptr<StackFrameList> &curr_ap,
677b9c1b51eSKate Stone                            lldb::StackFrameListSP &prev_sp) {
678bb19a13cSSaleem Abdulrasool   std::unique_lock<std::recursive_mutex> current_lock, previous_lock;
679bb19a13cSSaleem Abdulrasool   if (curr_ap)
680bb19a13cSSaleem Abdulrasool     current_lock = std::unique_lock<std::recursive_mutex>(curr_ap->m_mutex);
681bb19a13cSSaleem Abdulrasool   if (prev_sp)
682bb19a13cSSaleem Abdulrasool     previous_lock = std::unique_lock<std::recursive_mutex>(prev_sp->m_mutex);
6832cad65a5SGreg Clayton 
6842cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES)
6852c595273SJohnny Chen   StreamFile s(stdout, false);
6862cad65a5SGreg Clayton   s.PutCString("\n\nStackFrameList::Merge():\nPrev:\n");
687d70a6e71SEugene Zelenko   if (prev_sp)
6882cad65a5SGreg Clayton     prev_sp->Dump(&s);
6892cad65a5SGreg Clayton   else
6902cad65a5SGreg Clayton     s.PutCString("NULL");
6912cad65a5SGreg Clayton   s.PutCString("\nCurr:\n");
692d70a6e71SEugene Zelenko   if (curr_ap)
6932cad65a5SGreg Clayton     curr_ap->Dump(&s);
6942cad65a5SGreg Clayton   else
6952cad65a5SGreg Clayton     s.PutCString("NULL");
6962cad65a5SGreg Clayton   s.EOL();
6972cad65a5SGreg Clayton #endif
6982cad65a5SGreg Clayton 
699b9c1b51eSKate Stone   if (!curr_ap || curr_ap->GetNumFrames(false) == 0) {
7002cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES)
7012cad65a5SGreg Clayton     s.PutCString("No current frames, leave previous frames alone...\n");
7022cad65a5SGreg Clayton #endif
7032cad65a5SGreg Clayton     curr_ap.release();
7042cad65a5SGreg Clayton     return;
7052cad65a5SGreg Clayton   }
7062cad65a5SGreg Clayton 
707b9c1b51eSKate Stone   if (!prev_sp || prev_sp->GetNumFrames(false) == 0) {
7082cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES)
7092cad65a5SGreg Clayton     s.PutCString("No previous frames, so use current frames...\n");
7102cad65a5SGreg Clayton #endif
71105097246SAdrian Prantl     // We either don't have any previous frames, or since we have more than one
71205097246SAdrian Prantl     // current frames it means we have all the frames and can safely replace
71305097246SAdrian Prantl     // our previous frames.
7142cad65a5SGreg Clayton     prev_sp.reset(curr_ap.release());
7152cad65a5SGreg Clayton     return;
7162cad65a5SGreg Clayton   }
7172cad65a5SGreg Clayton 
7182cad65a5SGreg Clayton   const uint32_t num_curr_frames = curr_ap->GetNumFrames(false);
7192cad65a5SGreg Clayton 
720b9c1b51eSKate Stone   if (num_curr_frames > 1) {
7212cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES)
722b9c1b51eSKate Stone     s.PutCString(
723b9c1b51eSKate Stone         "We have more than one current frame, so use current frames...\n");
7242cad65a5SGreg Clayton #endif
72505097246SAdrian Prantl     // We have more than one current frames it means we have all the frames and
72605097246SAdrian Prantl     // can safely replace our previous frames.
7272cad65a5SGreg Clayton     prev_sp.reset(curr_ap.release());
7282cad65a5SGreg Clayton 
7292cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES)
7302cad65a5SGreg Clayton     s.PutCString("\nMerged:\n");
7312cad65a5SGreg Clayton     prev_sp->Dump(&s);
7322cad65a5SGreg Clayton #endif
7332cad65a5SGreg Clayton     return;
7342cad65a5SGreg Clayton   }
7352cad65a5SGreg Clayton 
736b57e4a1bSJason Molenda   StackFrameSP prev_frame_zero_sp(prev_sp->GetFrameAtIndex(0));
737b57e4a1bSJason Molenda   StackFrameSP curr_frame_zero_sp(curr_ap->GetFrameAtIndex(0));
7382cad65a5SGreg Clayton   StackID curr_stack_id(curr_frame_zero_sp->GetStackID());
7392cad65a5SGreg Clayton   StackID prev_stack_id(prev_frame_zero_sp->GetStackID());
7402cad65a5SGreg Clayton 
7412cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES)
7422c595273SJohnny Chen   const uint32_t num_prev_frames = prev_sp->GetNumFrames(false);
7432cad65a5SGreg Clayton   s.Printf("\n%u previous frames with one current frame\n", num_prev_frames);
7442cad65a5SGreg Clayton #endif
7452cad65a5SGreg Clayton 
7462cad65a5SGreg Clayton   // We have only a single current frame
7472cad65a5SGreg Clayton   // Our previous stack frames only had a single frame as well...
748b9c1b51eSKate Stone   if (curr_stack_id == prev_stack_id) {
7492cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES)
750b9c1b51eSKate Stone     s.Printf("\nPrevious frame #0 is same as current frame #0, merge the "
751b9c1b51eSKate Stone              "cached data\n");
7522cad65a5SGreg Clayton #endif
7532cad65a5SGreg Clayton 
754b9c1b51eSKate Stone     curr_frame_zero_sp->UpdateCurrentFrameFromPreviousFrame(
755b9c1b51eSKate Stone         *prev_frame_zero_sp);
756b9c1b51eSKate Stone     //        prev_frame_zero_sp->UpdatePreviousFrameFromCurrentFrame
757b9c1b51eSKate Stone     //        (*curr_frame_zero_sp);
7582cad65a5SGreg Clayton     //        prev_sp->SetFrameAtIndex (0, prev_frame_zero_sp);
759b9c1b51eSKate Stone   } else if (curr_stack_id < prev_stack_id) {
7602cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES)
761b9c1b51eSKate Stone     s.Printf("\nCurrent frame #0 has a stack ID that is less than the previous "
762b9c1b51eSKate Stone              "frame #0, insert current frame zero in front of previous\n");
7632cad65a5SGreg Clayton #endif
7642cad65a5SGreg Clayton     prev_sp->m_frames.insert(prev_sp->m_frames.begin(), curr_frame_zero_sp);
7652cad65a5SGreg Clayton   }
7662cad65a5SGreg Clayton 
7672cad65a5SGreg Clayton   curr_ap.release();
7682cad65a5SGreg Clayton 
7692cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES)
7702cad65a5SGreg Clayton   s.PutCString("\nMerged:\n");
7712cad65a5SGreg Clayton   prev_sp->Dump(&s);
7722cad65a5SGreg Clayton #endif
7732cad65a5SGreg Clayton }
774e4284b71SJim Ingham 
775b57e4a1bSJason Molenda lldb::StackFrameSP
776b9c1b51eSKate Stone StackFrameList::GetStackFrameSPForStackFramePtr(StackFrame *stack_frame_ptr) {
777e4284b71SJim Ingham   const_iterator pos;
778e4284b71SJim Ingham   const_iterator begin = m_frames.begin();
779e4284b71SJim Ingham   const_iterator end = m_frames.end();
780b57e4a1bSJason Molenda   lldb::StackFrameSP ret_sp;
781e4284b71SJim Ingham 
782b9c1b51eSKate Stone   for (pos = begin; pos != end; ++pos) {
783b9c1b51eSKate Stone     if (pos->get() == stack_frame_ptr) {
784e4284b71SJim Ingham       ret_sp = (*pos);
785e4284b71SJim Ingham       break;
786e4284b71SJim Ingham     }
787e4284b71SJim Ingham   }
788e4284b71SJim Ingham   return ret_sp;
789e4284b71SJim Ingham }
790e4284b71SJim Ingham 
791b9c1b51eSKate Stone size_t StackFrameList::GetStatus(Stream &strm, uint32_t first_frame,
792b9c1b51eSKate Stone                                  uint32_t num_frames, bool show_frame_info,
7938ec10efcSJim Ingham                                  uint32_t num_frames_with_source,
7947f1c1211SPavel Labath                                  bool show_unique,
795b9c1b51eSKate Stone                                  const char *selected_frame_marker) {
7967260f620SGreg Clayton   size_t num_frames_displayed = 0;
7977260f620SGreg Clayton 
7987260f620SGreg Clayton   if (num_frames == 0)
7997260f620SGreg Clayton     return 0;
8007260f620SGreg Clayton 
801b57e4a1bSJason Molenda   StackFrameSP frame_sp;
8027260f620SGreg Clayton   uint32_t frame_idx = 0;
8037260f620SGreg Clayton   uint32_t last_frame;
8047260f620SGreg Clayton 
8057260f620SGreg Clayton   // Don't let the last frame wrap around...
8067260f620SGreg Clayton   if (num_frames == UINT32_MAX)
8077260f620SGreg Clayton     last_frame = UINT32_MAX;
8087260f620SGreg Clayton   else
8097260f620SGreg Clayton     last_frame = first_frame + num_frames;
8107260f620SGreg Clayton 
811b57e4a1bSJason Molenda   StackFrameSP selected_frame_sp = m_thread.GetSelectedFrame();
812d70a6e71SEugene Zelenko   const char *unselected_marker = nullptr;
8138ec10efcSJim Ingham   std::string buffer;
814b9c1b51eSKate Stone   if (selected_frame_marker) {
8158ec10efcSJim Ingham     size_t len = strlen(selected_frame_marker);
8168ec10efcSJim Ingham     buffer.insert(buffer.begin(), len, ' ');
8178ec10efcSJim Ingham     unselected_marker = buffer.c_str();
8188ec10efcSJim Ingham   }
819d70a6e71SEugene Zelenko   const char *marker = nullptr;
8208ec10efcSJim Ingham 
821b9c1b51eSKate Stone   for (frame_idx = first_frame; frame_idx < last_frame; ++frame_idx) {
8227260f620SGreg Clayton     frame_sp = GetFrameAtIndex(frame_idx);
823d70a6e71SEugene Zelenko     if (!frame_sp)
8247260f620SGreg Clayton       break;
8257260f620SGreg Clayton 
826b9c1b51eSKate Stone     if (selected_frame_marker != nullptr) {
8278ec10efcSJim Ingham       if (frame_sp == selected_frame_sp)
8288ec10efcSJim Ingham         marker = selected_frame_marker;
8298ec10efcSJim Ingham       else
8308ec10efcSJim Ingham         marker = unselected_marker;
8318ec10efcSJim Ingham     }
8328ec10efcSJim Ingham 
833b9c1b51eSKate Stone     if (!frame_sp->GetStatus(strm, show_frame_info,
834b9c1b51eSKate Stone                              num_frames_with_source > (first_frame - frame_idx),
8357f1c1211SPavel Labath                              show_unique, marker))
8367260f620SGreg Clayton       break;
8377260f620SGreg Clayton     ++num_frames_displayed;
8387260f620SGreg Clayton   }
8397260f620SGreg Clayton 
8407260f620SGreg Clayton   strm.IndentLess();
8417260f620SGreg Clayton   return num_frames_displayed;
8427260f620SGreg Clayton }
843