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"
29*6f9e6901SZachary 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() {
54ca5ce187SGreg Clayton   // Call clear since this takes a lock and clears the stack frame list
55ca5ce187SGreg Clayton   // in case 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 {
98513c6bb8SJim Ingham       // We only need to do something special about inlined blocks when we
99513c6bb8SJim Ingham       // are 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
103b9c1b51eSKate Stone       // containing
104513c6bb8SJim Ingham       // function or another inlined function.
105513c6bb8SJim Ingham 
106513c6bb8SJim Ingham       lldb::addr_t curr_pc = m_thread.GetRegisterContext()->GetPC();
107513c6bb8SJim Ingham       Block *block_ptr = m_frames[0]->GetFrameBlock();
108b9c1b51eSKate Stone       if (block_ptr) {
109513c6bb8SJim Ingham         Address pc_as_address;
110b9c1b51eSKate Stone         pc_as_address.SetLoadAddress(curr_pc,
111b9c1b51eSKate Stone                                      &(m_thread.GetProcess()->GetTarget()));
112513c6bb8SJim Ingham         AddressRange containing_range;
113b9c1b51eSKate Stone         if (block_ptr->GetRangeContainingAddress(pc_as_address,
114b9c1b51eSKate Stone                                                  containing_range)) {
115b9c1b51eSKate Stone           if (pc_as_address == containing_range.GetBaseAddress()) {
116b9c1b51eSKate Stone             // If we got here because of a breakpoint hit, then set the inlined
117b9c1b51eSKate Stone             // depth depending on where
118513c6bb8SJim Ingham             // the breakpoint was set.
119b9c1b51eSKate Stone             // If we got here because of a crash, then set the inlined depth to
120b9c1b51eSKate Stone             // the deepest most block.
121b9c1b51eSKate Stone             // Otherwise, we stopped here naturally as the result of a step, so
122b9c1b51eSKate Stone             // set ourselves in the
123b9c1b51eSKate Stone             // containing frame of the whole set of nested inlines, so the user
124b9c1b51eSKate Stone             // can then "virtually"
125513c6bb8SJim Ingham             // step into the frames one by one, or next over the whole mess.
126b9c1b51eSKate Stone             // Note: We don't have to handle being somewhere in the middle of
127b9c1b51eSKate Stone             // the stack here, since
128b9c1b51eSKate Stone             // ResetCurrentInlinedDepth doesn't get called if there is a valid
129b9c1b51eSKate Stone             // inlined depth set.
130513c6bb8SJim Ingham             StopInfoSP stop_info_sp = m_thread.GetStopInfo();
131b9c1b51eSKate Stone             if (stop_info_sp) {
132b9c1b51eSKate Stone               switch (stop_info_sp->GetStopReason()) {
133513c6bb8SJim Ingham               case eStopReasonWatchpoint:
134513c6bb8SJim Ingham               case eStopReasonException:
13590ba8115SGreg Clayton               case eStopReasonExec:
136513c6bb8SJim Ingham               case eStopReasonSignal:
137513c6bb8SJim Ingham                 // In all these cases we want to stop in the deepest most frame.
138513c6bb8SJim Ingham                 m_current_inlined_pc = curr_pc;
139513c6bb8SJim Ingham                 m_current_inlined_depth = 0;
140513c6bb8SJim Ingham                 break;
141b9c1b51eSKate Stone               case eStopReasonBreakpoint: {
142b9c1b51eSKate Stone                 // FIXME: Figure out what this break point is doing, and set the
143b9c1b51eSKate Stone                 // inline depth
144b9c1b51eSKate Stone                 // appropriately.  Be careful to take into account breakpoints
145b9c1b51eSKate Stone                 // that implement
146b9c1b51eSKate Stone                 // step over prologue, since that should do the default
147b9c1b51eSKate Stone                 // calculation.
148b9c1b51eSKate Stone                 // For now, if the breakpoints corresponding to this hit are all
149b9c1b51eSKate Stone                 // internal,
150b9c1b51eSKate Stone                 // I set the stop location to the top of the inlined stack,
151b9c1b51eSKate Stone                 // since that will make
152b9c1b51eSKate Stone                 // things like stepping over prologues work right.  But if there
153b9c1b51eSKate Stone                 // are any non-internal
154b9c1b51eSKate Stone                 // breakpoints I do to the bottom of the stack, since that was
155b9c1b51eSKate Stone                 // the old behavior.
156c635500dSJim Ingham                 uint32_t bp_site_id = stop_info_sp->GetValue();
157b9c1b51eSKate Stone                 BreakpointSiteSP bp_site_sp(
158b9c1b51eSKate Stone                     m_thread.GetProcess()->GetBreakpointSiteList().FindByID(
159b9c1b51eSKate Stone                         bp_site_id));
160c635500dSJim Ingham                 bool all_internal = true;
161b9c1b51eSKate Stone                 if (bp_site_sp) {
162c635500dSJim Ingham                   uint32_t num_owners = bp_site_sp->GetNumberOfOwners();
163b9c1b51eSKate Stone                   for (uint32_t i = 0; i < num_owners; i++) {
164b9c1b51eSKate Stone                     Breakpoint &bp_ref =
165b9c1b51eSKate Stone                         bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint();
166b9c1b51eSKate Stone                     if (!bp_ref.IsInternal()) {
167c635500dSJim Ingham                       all_internal = false;
168c635500dSJim Ingham                     }
169c635500dSJim Ingham                   }
170c635500dSJim Ingham                 }
171b9c1b51eSKate Stone                 if (!all_internal) {
172c635500dSJim Ingham                   m_current_inlined_pc = curr_pc;
173c635500dSJim Ingham                   m_current_inlined_depth = 0;
174c635500dSJim Ingham                   break;
175c635500dSJim Ingham                 }
1767da851a3SJim Ingham               }
177cec91ef9SGreg Clayton                 LLVM_FALLTHROUGH;
178b9c1b51eSKate Stone               default: {
179b9c1b51eSKate Stone                 // Otherwise, we should set ourselves at the container of the
180b9c1b51eSKate Stone                 // inlining, so that the
181513c6bb8SJim Ingham                 // user can descend into them.
182b9c1b51eSKate Stone                 // So first we check whether we have more than one inlined block
183b9c1b51eSKate Stone                 // sharing this PC:
184513c6bb8SJim Ingham                 int num_inlined_functions = 0;
185513c6bb8SJim Ingham 
186513c6bb8SJim Ingham                 for (Block *container_ptr = block_ptr->GetInlinedParent();
187d70a6e71SEugene Zelenko                      container_ptr != nullptr;
188b9c1b51eSKate Stone                      container_ptr = container_ptr->GetInlinedParent()) {
189b9c1b51eSKate Stone                   if (!container_ptr->GetRangeContainingAddress(
190b9c1b51eSKate Stone                           pc_as_address, containing_range))
191513c6bb8SJim Ingham                     break;
192513c6bb8SJim Ingham                   if (pc_as_address != containing_range.GetBaseAddress())
193513c6bb8SJim Ingham                     break;
194513c6bb8SJim Ingham 
195513c6bb8SJim Ingham                   num_inlined_functions++;
196513c6bb8SJim Ingham                 }
197513c6bb8SJim Ingham                 m_current_inlined_pc = curr_pc;
198513c6bb8SJim Ingham                 m_current_inlined_depth = num_inlined_functions + 1;
199b9c1b51eSKate Stone                 Log *log(
200b9c1b51eSKate Stone                     lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
2016cd41da7SJim Ingham                 if (log && log->GetVerbose())
202b9c1b51eSKate Stone                   log->Printf("ResetCurrentInlinedDepth: setting inlined "
203b9c1b51eSKate Stone                               "depth: %d 0x%" PRIx64 ".\n",
204b9c1b51eSKate Stone                               m_current_inlined_depth, curr_pc);
205513c6bb8SJim Ingham 
206b9c1b51eSKate Stone               } break;
207513c6bb8SJim Ingham               }
208513c6bb8SJim Ingham             }
209513c6bb8SJim Ingham           }
210513c6bb8SJim Ingham         }
211513c6bb8SJim Ingham       }
212513c6bb8SJim Ingham     }
213513c6bb8SJim Ingham   }
214513c6bb8SJim Ingham }
215513c6bb8SJim Ingham 
216b9c1b51eSKate Stone bool StackFrameList::DecrementCurrentInlinedDepth() {
217b9c1b51eSKate Stone   if (m_show_inlined_frames) {
218513c6bb8SJim Ingham     uint32_t current_inlined_depth = GetCurrentInlinedDepth();
219b9c1b51eSKate Stone     if (current_inlined_depth != UINT32_MAX) {
220b9c1b51eSKate Stone       if (current_inlined_depth > 0) {
221513c6bb8SJim Ingham         m_current_inlined_depth--;
222513c6bb8SJim Ingham         return true;
223513c6bb8SJim Ingham       }
224513c6bb8SJim Ingham     }
2259786eeebSJim Ingham   }
226513c6bb8SJim Ingham   return false;
227513c6bb8SJim Ingham }
228513c6bb8SJim Ingham 
229b9c1b51eSKate Stone void StackFrameList::SetCurrentInlinedDepth(uint32_t new_depth) {
2306cd41da7SJim Ingham   m_current_inlined_depth = new_depth;
2316cd41da7SJim Ingham   if (new_depth == UINT32_MAX)
2326cd41da7SJim Ingham     m_current_inlined_pc = LLDB_INVALID_ADDRESS;
2336cd41da7SJim Ingham   else
2346cd41da7SJim Ingham     m_current_inlined_pc = m_thread.GetRegisterContext()->GetPC();
2356cd41da7SJim Ingham }
2366cd41da7SJim Ingham 
237b9c1b51eSKate Stone void StackFrameList::GetFramesUpTo(uint32_t end_idx) {
238ebafd2f1SEnrico Granata   // this makes sure we do not fetch frames for an invalid thread
239d70a6e71SEugene Zelenko   if (!m_thread.IsValid())
240ebafd2f1SEnrico Granata     return;
241ebafd2f1SEnrico Granata 
242b9c1b51eSKate Stone   // We've already gotten more frames than asked for, or we've already finished
243b9c1b51eSKate Stone   // unwinding, return.
244b0c72a5fSJim Ingham   if (m_frames.size() > end_idx || GetAllFramesFetched())
245b0c72a5fSJim Ingham     return;
24612daf946SGreg Clayton 
247b0c72a5fSJim Ingham   Unwind *unwinder = m_thread.GetUnwinder();
248b0c72a5fSJim Ingham 
249b9c1b51eSKate Stone   if (m_show_inlined_frames) {
2505082c5fdSGreg Clayton #if defined(DEBUG_STACK_FRAMES)
2512c595273SJohnny Chen     StreamFile s(stdout, false);
2525082c5fdSGreg Clayton #endif
253b9c1b51eSKate Stone     // If we are hiding some frames from the outside world, we need to add those
254b9c1b51eSKate Stone     // onto the total count of
255b9c1b51eSKate Stone     // frames to fetch.  However, we don't need to do that if end_idx is 0 since
256b9c1b51eSKate Stone     // in that case we always
257b9c1b51eSKate Stone     // get the first concrete frame and all the inlined frames below it...  And
258b9c1b51eSKate Stone     // of course, if end_idx is
2596cd41da7SJim Ingham     // UINT32_MAX that means get all, so just do that...
260513c6bb8SJim Ingham 
261513c6bb8SJim Ingham     uint32_t inlined_depth = 0;
262b9c1b51eSKate Stone     if (end_idx > 0 && end_idx != UINT32_MAX) {
263513c6bb8SJim Ingham       inlined_depth = GetCurrentInlinedDepth();
264b9c1b51eSKate Stone       if (inlined_depth != UINT32_MAX) {
265513c6bb8SJim Ingham         if (end_idx > 0)
266513c6bb8SJim Ingham           end_idx += inlined_depth;
267513c6bb8SJim Ingham       }
268513c6bb8SJim Ingham     }
26912daf946SGreg Clayton 
270b57e4a1bSJason Molenda     StackFrameSP unwind_frame_sp;
271b9c1b51eSKate Stone     do {
272b0c72a5fSJim Ingham       uint32_t idx = m_concrete_frames_fetched++;
2738012cadbSGreg Clayton       lldb::addr_t pc = LLDB_INVALID_ADDRESS;
2748012cadbSGreg Clayton       lldb::addr_t cfa = LLDB_INVALID_ADDRESS;
275b9c1b51eSKate Stone       if (idx == 0) {
2765082c5fdSGreg Clayton         // We might have already created frame zero, only create it
2775082c5fdSGreg Clayton         // if we need to
278b9c1b51eSKate Stone         if (m_frames.empty()) {
279b3ae8761SGreg Clayton           RegisterContextSP reg_ctx_sp(m_thread.GetRegisterContext());
280b3ae8761SGreg Clayton 
281b9c1b51eSKate Stone           if (reg_ctx_sp) {
282b9c1b51eSKate Stone             const bool success =
283b9c1b51eSKate Stone                 unwinder && unwinder->GetFrameInfoAtIndex(idx, cfa, pc);
284376c4854SJim Ingham             // There shouldn't be any way not to get the frame info for frame 0.
285b9c1b51eSKate Stone             // But if the unwinder can't make one, lets make one by hand with
286b9c1b51eSKate Stone             // the
287376c4854SJim Ingham             // SP as the CFA and see if that gets any further.
288b9c1b51eSKate Stone             if (!success) {
289b3ae8761SGreg Clayton               cfa = reg_ctx_sp->GetSP();
290b3ae8761SGreg Clayton               pc = reg_ctx_sp->GetPC();
291376c4854SJim Ingham             }
292376c4854SJim Ingham 
293d9e416c0SGreg Clayton             unwind_frame_sp.reset(new StackFrame(m_thread.shared_from_this(),
294b9c1b51eSKate Stone                                                  m_frames.size(), idx,
295b9c1b51eSKate Stone                                                  reg_ctx_sp, cfa, pc, nullptr));
2965082c5fdSGreg Clayton             m_frames.push_back(unwind_frame_sp);
2975082c5fdSGreg Clayton           }
298b9c1b51eSKate Stone         } else {
2995082c5fdSGreg Clayton           unwind_frame_sp = m_frames.front();
300b57e4a1bSJason Molenda           cfa = unwind_frame_sp->m_id.GetCallFrameAddress();
3015082c5fdSGreg Clayton         }
302b9c1b51eSKate Stone       } else {
303b9c1b51eSKate Stone         const bool success =
304b9c1b51eSKate Stone             unwinder && unwinder->GetFrameInfoAtIndex(idx, cfa, pc);
305b9c1b51eSKate Stone         if (!success) {
306b0c72a5fSJim Ingham           // We've gotten to the end of the stack.
307b0c72a5fSJim Ingham           SetAllFramesFetched();
308b0c72a5fSJim Ingham           break;
309b0c72a5fSJim Ingham         }
31099618476SJason Molenda         const bool cfa_is_valid = true;
31199618476SJason Molenda         const bool stop_id_is_valid = false;
31299618476SJason Molenda         const bool is_history_frame = false;
313b9c1b51eSKate Stone         unwind_frame_sp.reset(new StackFrame(
314b9c1b51eSKate Stone             m_thread.shared_from_this(), m_frames.size(), idx, cfa,
315b9c1b51eSKate Stone             cfa_is_valid, pc, 0, stop_id_is_valid, is_history_frame, nullptr));
3165082c5fdSGreg Clayton         m_frames.push_back(unwind_frame_sp);
31712daf946SGreg Clayton       }
31812daf946SGreg Clayton 
319ff1b5c42SEd Maste       assert(unwind_frame_sp);
320b9c1b51eSKate Stone       SymbolContext unwind_sc = unwind_frame_sp->GetSymbolContext(
321b9c1b51eSKate Stone           eSymbolContextBlock | eSymbolContextFunction);
3221ed54f50SGreg Clayton       Block *unwind_block = unwind_sc.block;
323b9c1b51eSKate Stone       if (unwind_block) {
3245f4c61e2SGreg Clayton         Address curr_frame_address(unwind_frame_sp->GetFrameCodeAddress());
325911d5784STed Woodward         TargetSP target_sp = m_thread.CalculateTarget();
3265f4c61e2SGreg Clayton         // Be sure to adjust the frame address to match the address
3275f4c61e2SGreg Clayton         // that was used to lookup the symbol context above. If we are
3285f4c61e2SGreg Clayton         // in the first concrete frame, then we lookup using the current
3295f4c61e2SGreg Clayton         // address, else we decrement the address by one to get the correct
3305f4c61e2SGreg Clayton         // location.
331b9c1b51eSKate Stone         if (idx > 0) {
332b9c1b51eSKate Stone           if (curr_frame_address.GetOffset() == 0) {
333b9c1b51eSKate Stone             // If curr_frame_address points to the first address in a section
334b9c1b51eSKate Stone             // then after
335b9c1b51eSKate Stone             // adjustment it will point to an other section. In that case
336b9c1b51eSKate Stone             // resolve the
3370f8452baSTamas Berghammer             // address again to the correct section plus offset form.
338b9c1b51eSKate Stone             addr_t load_addr = curr_frame_address.GetOpcodeLoadAddress(
339b9c1b51eSKate Stone                 target_sp.get(), eAddressClassCode);
340b9c1b51eSKate Stone             curr_frame_address.SetOpcodeLoadAddress(
341b9c1b51eSKate Stone                 load_addr - 1, target_sp.get(), eAddressClassCode);
342b9c1b51eSKate Stone           } else {
3435f4c61e2SGreg Clayton             curr_frame_address.Slide(-1);
3440f8452baSTamas Berghammer           }
3450f8452baSTamas Berghammer         }
3465f4c61e2SGreg Clayton 
3471ed54f50SGreg Clayton         SymbolContext next_frame_sc;
3481ed54f50SGreg Clayton         Address next_frame_address;
3491ed54f50SGreg Clayton 
350b9c1b51eSKate Stone         while (unwind_sc.GetParentOfInlinedScope(
351b9c1b51eSKate Stone             curr_frame_address, next_frame_sc, next_frame_address)) {
352911d5784STed Woodward           next_frame_sc.line_entry.ApplyFileMappings(target_sp);
353b9c1b51eSKate Stone           StackFrameSP frame_sp(
354b9c1b51eSKate Stone               new StackFrame(m_thread.shared_from_this(), m_frames.size(), idx,
355b9c1b51eSKate Stone                              unwind_frame_sp->GetRegisterContextSP(), cfa,
356b9c1b51eSKate Stone                              next_frame_address, &next_frame_sc));
3575082c5fdSGreg Clayton 
3585082c5fdSGreg Clayton           m_frames.push_back(frame_sp);
3591ed54f50SGreg Clayton           unwind_sc = next_frame_sc;
3601ed54f50SGreg Clayton           curr_frame_address = next_frame_address;
361b0c72a5fSJim Ingham         }
362b0c72a5fSJim Ingham       }
363b0c72a5fSJim Ingham     } while (m_frames.size() - 1 < end_idx);
3641ed54f50SGreg Clayton 
365b0c72a5fSJim Ingham     // Don't try to merge till you've calculated all the frames in this stack.
366b9c1b51eSKate Stone     if (GetAllFramesFetched() && m_prev_frames_sp) {
3672cad65a5SGreg Clayton       StackFrameList *prev_frames = m_prev_frames_sp.get();
3685082c5fdSGreg Clayton       StackFrameList *curr_frames = this;
3695082c5fdSGreg Clayton 
3706cd41da7SJim Ingham // curr_frames->m_current_inlined_depth = prev_frames->m_current_inlined_depth;
3716cd41da7SJim Ingham // curr_frames->m_current_inlined_pc = prev_frames->m_current_inlined_pc;
372b9c1b51eSKate Stone // printf ("GetFramesUpTo: Copying current inlined depth: %d 0x%" PRIx64 ".\n",
373b9c1b51eSKate Stone // curr_frames->m_current_inlined_depth, curr_frames->m_current_inlined_pc);
3749786eeebSJim Ingham 
3755082c5fdSGreg Clayton #if defined(DEBUG_STACK_FRAMES)
37668275d5eSGreg Clayton       s.PutCString("\nprev_frames:\n");
3775082c5fdSGreg Clayton       prev_frames->Dump(&s);
37868275d5eSGreg Clayton       s.PutCString("\ncurr_frames:\n");
3795082c5fdSGreg Clayton       curr_frames->Dump(&s);
3805082c5fdSGreg Clayton       s.EOL();
3815082c5fdSGreg Clayton #endif
3825082c5fdSGreg Clayton       size_t curr_frame_num, prev_frame_num;
3835082c5fdSGreg Clayton 
384b9c1b51eSKate Stone       for (curr_frame_num = curr_frames->m_frames.size(),
385b9c1b51eSKate Stone           prev_frame_num = prev_frames->m_frames.size();
3865082c5fdSGreg Clayton            curr_frame_num > 0 && prev_frame_num > 0;
387b9c1b51eSKate Stone            --curr_frame_num, --prev_frame_num) {
3885082c5fdSGreg Clayton         const size_t curr_frame_idx = curr_frame_num - 1;
3895082c5fdSGreg Clayton         const size_t prev_frame_idx = prev_frame_num - 1;
390b57e4a1bSJason Molenda         StackFrameSP curr_frame_sp(curr_frames->m_frames[curr_frame_idx]);
391b57e4a1bSJason Molenda         StackFrameSP prev_frame_sp(prev_frames->m_frames[prev_frame_idx]);
3925082c5fdSGreg Clayton 
3935082c5fdSGreg Clayton #if defined(DEBUG_STACK_FRAMES)
3942cad65a5SGreg Clayton         s.Printf("\n\nCurr frame #%u ", curr_frame_idx);
3955082c5fdSGreg Clayton         if (curr_frame_sp)
3962cad65a5SGreg Clayton           curr_frame_sp->Dump(&s, true, false);
3975082c5fdSGreg Clayton         else
3985082c5fdSGreg Clayton           s.PutCString("NULL");
3992cad65a5SGreg Clayton         s.Printf("\nPrev frame #%u ", prev_frame_idx);
4005082c5fdSGreg Clayton         if (prev_frame_sp)
4012cad65a5SGreg Clayton           prev_frame_sp->Dump(&s, true, false);
4025082c5fdSGreg Clayton         else
4035082c5fdSGreg Clayton           s.PutCString("NULL");
4045082c5fdSGreg Clayton #endif
4055082c5fdSGreg Clayton 
406b57e4a1bSJason Molenda         StackFrame *curr_frame = curr_frame_sp.get();
407b57e4a1bSJason Molenda         StackFrame *prev_frame = prev_frame_sp.get();
4085082c5fdSGreg Clayton 
409d70a6e71SEugene Zelenko         if (curr_frame == nullptr || prev_frame == nullptr)
4105082c5fdSGreg Clayton           break;
4115082c5fdSGreg Clayton 
41259e8fc1cSGreg Clayton         // Check the stack ID to make sure they are equal
41359e8fc1cSGreg Clayton         if (curr_frame->GetStackID() != prev_frame->GetStackID())
4145082c5fdSGreg Clayton           break;
4155082c5fdSGreg Clayton 
41659e8fc1cSGreg Clayton         prev_frame->UpdatePreviousFrameFromCurrentFrame(*curr_frame);
41759e8fc1cSGreg Clayton         // Now copy the fixed up previous frame into the current frames
41859e8fc1cSGreg Clayton         // so the pointer doesn't change
41959e8fc1cSGreg Clayton         m_frames[curr_frame_idx] = prev_frame_sp;
42059e8fc1cSGreg Clayton // curr_frame->UpdateCurrentFrameFromPreviousFrame (*prev_frame);
4215082c5fdSGreg Clayton 
4225082c5fdSGreg Clayton #if defined(DEBUG_STACK_FRAMES)
42368275d5eSGreg Clayton         s.Printf("\n    Copying previous frame to current frame");
4245082c5fdSGreg Clayton #endif
4255082c5fdSGreg Clayton       }
4265082c5fdSGreg Clayton       // We are done with the old stack frame list, we can release it now
4272cad65a5SGreg Clayton       m_prev_frames_sp.reset();
4285082c5fdSGreg Clayton     }
42968275d5eSGreg Clayton 
43068275d5eSGreg Clayton #if defined(DEBUG_STACK_FRAMES)
43168275d5eSGreg Clayton     s.PutCString("\n\nNew frames:\n");
43268275d5eSGreg Clayton     Dump(&s);
43368275d5eSGreg Clayton     s.EOL();
43468275d5eSGreg Clayton #endif
435b9c1b51eSKate Stone   } else {
436b0c72a5fSJim Ingham     if (end_idx < m_concrete_frames_fetched)
437b0c72a5fSJim Ingham       return;
438b0c72a5fSJim Ingham 
439b9c1b51eSKate Stone     if (unwinder) {
440b0c72a5fSJim Ingham       uint32_t num_frames = unwinder->GetFramesUpTo(end_idx);
441b9c1b51eSKate Stone       if (num_frames <= end_idx + 1) {
442b0c72a5fSJim Ingham         // Done unwinding.
443b0c72a5fSJim Ingham         m_concrete_frames_fetched = UINT32_MAX;
444b0c72a5fSJim Ingham       }
445b0c72a5fSJim Ingham       m_frames.resize(num_frames);
44612daf946SGreg Clayton     }
4475082c5fdSGreg Clayton   }
448ec6829eaSGreg Clayton }
449b0c72a5fSJim Ingham 
450b9c1b51eSKate Stone uint32_t StackFrameList::GetNumFrames(bool can_create) {
451bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
452b0c72a5fSJim Ingham 
453b0c72a5fSJim Ingham   if (can_create)
454b0c72a5fSJim Ingham     GetFramesUpTo(UINT32_MAX);
455513c6bb8SJim Ingham 
456513c6bb8SJim Ingham   uint32_t inlined_depth = GetCurrentInlinedDepth();
457513c6bb8SJim Ingham   if (inlined_depth == UINT32_MAX)
4585082c5fdSGreg Clayton     return m_frames.size();
459513c6bb8SJim Ingham   else
460513c6bb8SJim Ingham     return m_frames.size() - inlined_depth;
46130fdc8d8SChris Lattner }
46230fdc8d8SChris Lattner 
463b9c1b51eSKate Stone void StackFrameList::Dump(Stream *s) {
464d70a6e71SEugene Zelenko   if (s == nullptr)
4655082c5fdSGreg Clayton     return;
466bb19a13cSSaleem Abdulrasool 
467bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
46830fdc8d8SChris Lattner 
4695082c5fdSGreg Clayton   const_iterator pos, begin = m_frames.begin(), end = m_frames.end();
470b9c1b51eSKate Stone   for (pos = begin; pos != end; ++pos) {
471b57e4a1bSJason Molenda     StackFrame *frame = (*pos).get();
472324a1036SSaleem Abdulrasool     s->Printf("%p: ", static_cast<void *>(frame));
473b9c1b51eSKate Stone     if (frame) {
47459e8fc1cSGreg Clayton       frame->GetStackID().Dump(s);
4750603aa9dSGreg Clayton       frame->DumpUsingSettingsFormat(s);
476b9c1b51eSKate Stone     } else
477dce502edSGreg Clayton       s->Printf("frame #%u", (uint32_t)std::distance(begin, pos));
4785082c5fdSGreg Clayton     s->EOL();
47912daf946SGreg Clayton   }
4805082c5fdSGreg Clayton   s->EOL();
4815082c5fdSGreg Clayton }
48212daf946SGreg Clayton 
483b9c1b51eSKate Stone StackFrameSP StackFrameList::GetFrameAtIndex(uint32_t idx) {
484b57e4a1bSJason Molenda   StackFrameSP frame_sp;
485bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
4865cb9a184SJim Ingham   uint32_t original_idx = idx;
4875cb9a184SJim Ingham 
488513c6bb8SJim Ingham   uint32_t inlined_depth = GetCurrentInlinedDepth();
489513c6bb8SJim Ingham   if (inlined_depth != UINT32_MAX)
490513c6bb8SJim Ingham     idx += inlined_depth;
491513c6bb8SJim Ingham 
4925082c5fdSGreg Clayton   if (idx < m_frames.size())
4935082c5fdSGreg Clayton     frame_sp = m_frames[idx];
49412daf946SGreg Clayton 
4955082c5fdSGreg Clayton   if (frame_sp)
49612daf946SGreg Clayton     return frame_sp;
49712daf946SGreg Clayton 
4981692b901SJim Ingham   // GetFramesUpTo will fill m_frames with as many frames as you asked for,
4991692b901SJim Ingham   // if there are that many.  If there weren't then you asked for too many
5001692b901SJim Ingham   // frames.
501b0c72a5fSJim Ingham   GetFramesUpTo(idx);
502b9c1b51eSKate Stone   if (idx < m_frames.size()) {
503b9c1b51eSKate Stone     if (m_show_inlined_frames) {
504b9c1b51eSKate Stone       // When inline frames are enabled we actually create all the frames in
505b9c1b51eSKate Stone       // GetFramesUpTo.
5065082c5fdSGreg Clayton       frame_sp = m_frames[idx];
507b9c1b51eSKate Stone     } else {
50812daf946SGreg Clayton       Unwind *unwinder = m_thread.GetUnwinder();
509b9c1b51eSKate Stone       if (unwinder) {
51012daf946SGreg Clayton         addr_t pc, cfa;
511b9c1b51eSKate Stone         if (unwinder->GetFrameInfoAtIndex(idx, cfa, pc)) {
51299618476SJason Molenda           const bool cfa_is_valid = true;
51399618476SJason Molenda           const bool stop_id_is_valid = false;
51499618476SJason Molenda           const bool is_history_frame = false;
515b9c1b51eSKate Stone           frame_sp.reset(new StackFrame(
516b9c1b51eSKate Stone               m_thread.shared_from_this(), idx, idx, cfa, cfa_is_valid, pc, 0,
517d70a6e71SEugene Zelenko               stop_id_is_valid, is_history_frame, nullptr));
51859e8fc1cSGreg Clayton 
519b9c1b51eSKate Stone           Function *function =
520b9c1b51eSKate Stone               frame_sp->GetSymbolContext(eSymbolContextFunction).function;
521b9c1b51eSKate Stone           if (function) {
52259e8fc1cSGreg Clayton             // When we aren't showing inline functions we always use
52359e8fc1cSGreg Clayton             // the top most function block as the scope.
52459e8fc1cSGreg Clayton             frame_sp->SetSymbolContextScope(&function->GetBlock(false));
525b9c1b51eSKate Stone           } else {
526b9c1b51eSKate Stone             // Set the symbol scope from the symbol regardless if it is nullptr
527b9c1b51eSKate Stone             // or valid.
528b9c1b51eSKate Stone             frame_sp->SetSymbolContextScope(
529b9c1b51eSKate Stone                 frame_sp->GetSymbolContext(eSymbolContextSymbol).symbol);
53059e8fc1cSGreg Clayton           }
5315082c5fdSGreg Clayton           SetFrameAtIndex(idx, frame_sp);
53212daf946SGreg Clayton         }
53312daf946SGreg Clayton       }
53412daf946SGreg Clayton     }
535b9c1b51eSKate Stone   } else if (original_idx == 0) {
536b9c1b51eSKate Stone     // There should ALWAYS be a frame at index 0.  If something went wrong with
537b9c1b51eSKate Stone     // the CurrentInlinedDepth such that
538b9c1b51eSKate Stone     // there weren't as many frames as we thought taking that into account, then
539b9c1b51eSKate Stone     // reset the current inlined depth
5405cb9a184SJim Ingham     // and return the real zeroth frame.
541b9c1b51eSKate Stone     if (m_frames.empty()) {
542b9c1b51eSKate Stone       // Why do we have a thread with zero frames, that should not ever
543b9c1b51eSKate Stone       // happen...
544a322f36cSDavid Blaikie       assert(!m_thread.IsValid() && "A valid thread has no frames.");
545b9c1b51eSKate Stone     } else {
546d70a6e71SEugene Zelenko       ResetCurrentInlinedDepth();
547d70a6e71SEugene Zelenko       frame_sp = m_frames[original_idx];
5485cb9a184SJim Ingham     }
5495cb9a184SJim Ingham   }
5505cb9a184SJim Ingham 
55130fdc8d8SChris Lattner   return frame_sp;
55230fdc8d8SChris Lattner }
55330fdc8d8SChris Lattner 
554b57e4a1bSJason Molenda StackFrameSP
555b9c1b51eSKate Stone StackFrameList::GetFrameWithConcreteFrameIndex(uint32_t unwind_idx) {
5565ccbd294SGreg Clayton   // First try assuming the unwind index is the same as the frame index. The
5575ccbd294SGreg Clayton   // unwind index is always greater than or equal to the frame index, so it
5585ccbd294SGreg Clayton   // is a good place to start. If we have inlined frames we might have 5
5595ccbd294SGreg Clayton   // concrete frames (frame unwind indexes go from 0-4), but we might have 15
5605ccbd294SGreg Clayton   // frames after we make all the inlined frames. Most of the time the unwind
5615ccbd294SGreg Clayton   // frame index (or the concrete frame index) is the same as the frame index.
5625ccbd294SGreg Clayton   uint32_t frame_idx = unwind_idx;
563b57e4a1bSJason Molenda   StackFrameSP frame_sp(GetFrameAtIndex(frame_idx));
564b9c1b51eSKate Stone   while (frame_sp) {
5655ccbd294SGreg Clayton     if (frame_sp->GetFrameIndex() == unwind_idx)
5665ccbd294SGreg Clayton       break;
5675ccbd294SGreg Clayton     frame_sp = GetFrameAtIndex(++frame_idx);
5685ccbd294SGreg Clayton   }
5695ccbd294SGreg Clayton   return frame_sp;
5705ccbd294SGreg Clayton }
5715ccbd294SGreg Clayton 
572b9c1b51eSKate Stone static bool CompareStackID(const StackFrameSP &stack_sp,
573b9c1b51eSKate Stone                            const StackID &stack_id) {
5747bcb93d5SGreg Clayton   return stack_sp->GetStackID() < stack_id;
5757bcb93d5SGreg Clayton }
5767bcb93d5SGreg Clayton 
577b9c1b51eSKate Stone StackFrameSP StackFrameList::GetFrameWithStackID(const StackID &stack_id) {
578b57e4a1bSJason Molenda   StackFrameSP frame_sp;
5797bcb93d5SGreg Clayton 
580b9c1b51eSKate Stone   if (stack_id.IsValid()) {
581bb19a13cSSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(m_mutex);
5827bcb93d5SGreg Clayton     uint32_t frame_idx = 0;
5837bcb93d5SGreg Clayton     // Do a binary search in case the stack frame is already in our cache
5847bcb93d5SGreg Clayton     collection::const_iterator begin = m_frames.begin();
5857bcb93d5SGreg Clayton     collection::const_iterator end = m_frames.end();
586b9c1b51eSKate Stone     if (begin != end) {
587b9c1b51eSKate Stone       collection::const_iterator pos =
588b9c1b51eSKate Stone           std::lower_bound(begin, end, stack_id, CompareStackID);
589b9c1b51eSKate Stone       if (pos != end) {
5908012cadbSGreg Clayton         if ((*pos)->GetStackID() == stack_id)
5917bcb93d5SGreg Clayton           return *pos;
5928012cadbSGreg Clayton       }
5937bcb93d5SGreg Clayton 
5948012cadbSGreg Clayton       //            if (m_frames.back()->GetStackID() < stack_id)
5958012cadbSGreg Clayton       //                frame_idx = m_frames.size();
5967bcb93d5SGreg Clayton     }
597b9c1b51eSKate Stone     do {
5983a195b7eSJim Ingham       frame_sp = GetFrameAtIndex(frame_idx);
5993a195b7eSJim Ingham       if (frame_sp && frame_sp->GetStackID() == stack_id)
6003a195b7eSJim Ingham         break;
6013a195b7eSJim Ingham       frame_idx++;
602b9c1b51eSKate Stone     } while (frame_sp);
6037bcb93d5SGreg Clayton   }
6043a195b7eSJim Ingham   return frame_sp;
6053a195b7eSJim Ingham }
6065ccbd294SGreg Clayton 
607b9c1b51eSKate Stone bool StackFrameList::SetFrameAtIndex(uint32_t idx, StackFrameSP &frame_sp) {
6085082c5fdSGreg Clayton   if (idx >= m_frames.size())
6095082c5fdSGreg Clayton     m_frames.resize(idx + 1);
61012daf946SGreg Clayton   // Make sure allocation succeeded by checking bounds again
611b9c1b51eSKate Stone   if (idx < m_frames.size()) {
6125082c5fdSGreg Clayton     m_frames[idx] = frame_sp;
61330fdc8d8SChris Lattner     return true;
61430fdc8d8SChris Lattner   }
61530fdc8d8SChris Lattner   return false; // resize failed, out of memory?
61630fdc8d8SChris Lattner }
61730fdc8d8SChris Lattner 
618b9c1b51eSKate Stone uint32_t StackFrameList::GetSelectedFrameIndex() const {
619bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
6202976d00aSJim Ingham   return m_selected_frame_idx;
62130fdc8d8SChris Lattner }
62230fdc8d8SChris Lattner 
623b9c1b51eSKate Stone uint32_t StackFrameList::SetSelectedFrame(lldb_private::StackFrame *frame) {
624bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
62512daf946SGreg Clayton   const_iterator pos;
6265082c5fdSGreg Clayton   const_iterator begin = m_frames.begin();
6275082c5fdSGreg Clayton   const_iterator end = m_frames.end();
628b7f6b2faSJim Ingham   m_selected_frame_idx = 0;
629b9c1b51eSKate Stone   for (pos = begin; pos != end; ++pos) {
630b9c1b51eSKate Stone     if (pos->get() == frame) {
6312976d00aSJim Ingham       m_selected_frame_idx = std::distance(begin, pos);
632513c6bb8SJim Ingham       uint32_t inlined_depth = GetCurrentInlinedDepth();
633513c6bb8SJim Ingham       if (inlined_depth != UINT32_MAX)
634513c6bb8SJim Ingham         m_selected_frame_idx -= inlined_depth;
635b7f6b2faSJim Ingham       break;
63630fdc8d8SChris Lattner     }
63730fdc8d8SChris Lattner   }
638b7f6b2faSJim Ingham   SetDefaultFileAndLineToSelectedFrame();
6392976d00aSJim Ingham   return m_selected_frame_idx;
64030fdc8d8SChris Lattner }
64130fdc8d8SChris Lattner 
64230fdc8d8SChris Lattner // Mark a stack frame as the current frame using the frame index
643b9c1b51eSKate Stone bool StackFrameList::SetSelectedFrameByIndex(uint32_t idx) {
644bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
645b57e4a1bSJason Molenda   StackFrameSP frame_sp(GetFrameAtIndex(idx));
646b9c1b51eSKate Stone   if (frame_sp) {
647b0c72a5fSJim Ingham     SetSelectedFrame(frame_sp.get());
648b0c72a5fSJim Ingham     return true;
649b9c1b51eSKate Stone   } else
650b0c72a5fSJim Ingham     return false;
651b7f6b2faSJim Ingham }
652b7f6b2faSJim Ingham 
653b9c1b51eSKate Stone void StackFrameList::SetDefaultFileAndLineToSelectedFrame() {
654b9c1b51eSKate Stone   if (m_thread.GetID() ==
655b9c1b51eSKate Stone       m_thread.GetProcess()->GetThreadList().GetSelectedThread()->GetID()) {
656b57e4a1bSJason Molenda     StackFrameSP frame_sp(GetFrameAtIndex(GetSelectedFrameIndex()));
657b9c1b51eSKate Stone     if (frame_sp) {
658252d0edeSGreg Clayton       SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextLineEntry);
659b7f6b2faSJim Ingham       if (sc.line_entry.file)
660b9c1b51eSKate Stone         m_thread.CalculateTarget()->GetSourceManager().SetDefaultFileAndLine(
661b9c1b51eSKate Stone             sc.line_entry.file, sc.line_entry.line);
662b7f6b2faSJim Ingham     }
663b7f6b2faSJim Ingham   }
66430fdc8d8SChris Lattner }
66530fdc8d8SChris Lattner 
66630fdc8d8SChris Lattner // The thread has been run, reset the number stack frames to zero so we can
66730fdc8d8SChris Lattner // determine how many frames we have lazily.
668b9c1b51eSKate Stone void StackFrameList::Clear() {
669bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
6705082c5fdSGreg Clayton   m_frames.clear();
671b0c72a5fSJim Ingham   m_concrete_frames_fetched = 0;
67230fdc8d8SChris Lattner }
67330fdc8d8SChris Lattner 
674b9c1b51eSKate Stone void StackFrameList::InvalidateFrames(uint32_t start_idx) {
675bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
676b9c1b51eSKate Stone   if (m_show_inlined_frames) {
67712daf946SGreg Clayton     Clear();
678b9c1b51eSKate Stone   } else {
6795082c5fdSGreg Clayton     const size_t num_frames = m_frames.size();
680b9c1b51eSKate Stone     while (start_idx < num_frames) {
6815082c5fdSGreg Clayton       m_frames[start_idx].reset();
68230fdc8d8SChris Lattner       ++start_idx;
68330fdc8d8SChris Lattner     }
68430fdc8d8SChris Lattner   }
68512daf946SGreg Clayton }
6862cad65a5SGreg Clayton 
687b9c1b51eSKate Stone void StackFrameList::Merge(std::unique_ptr<StackFrameList> &curr_ap,
688b9c1b51eSKate Stone                            lldb::StackFrameListSP &prev_sp) {
689bb19a13cSSaleem Abdulrasool   std::unique_lock<std::recursive_mutex> current_lock, previous_lock;
690bb19a13cSSaleem Abdulrasool   if (curr_ap)
691bb19a13cSSaleem Abdulrasool     current_lock = std::unique_lock<std::recursive_mutex>(curr_ap->m_mutex);
692bb19a13cSSaleem Abdulrasool   if (prev_sp)
693bb19a13cSSaleem Abdulrasool     previous_lock = std::unique_lock<std::recursive_mutex>(prev_sp->m_mutex);
6942cad65a5SGreg Clayton 
6952cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES)
6962c595273SJohnny Chen   StreamFile s(stdout, false);
6972cad65a5SGreg Clayton   s.PutCString("\n\nStackFrameList::Merge():\nPrev:\n");
698d70a6e71SEugene Zelenko   if (prev_sp)
6992cad65a5SGreg Clayton     prev_sp->Dump(&s);
7002cad65a5SGreg Clayton   else
7012cad65a5SGreg Clayton     s.PutCString("NULL");
7022cad65a5SGreg Clayton   s.PutCString("\nCurr:\n");
703d70a6e71SEugene Zelenko   if (curr_ap)
7042cad65a5SGreg Clayton     curr_ap->Dump(&s);
7052cad65a5SGreg Clayton   else
7062cad65a5SGreg Clayton     s.PutCString("NULL");
7072cad65a5SGreg Clayton   s.EOL();
7082cad65a5SGreg Clayton #endif
7092cad65a5SGreg Clayton 
710b9c1b51eSKate Stone   if (!curr_ap || curr_ap->GetNumFrames(false) == 0) {
7112cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES)
7122cad65a5SGreg Clayton     s.PutCString("No current frames, leave previous frames alone...\n");
7132cad65a5SGreg Clayton #endif
7142cad65a5SGreg Clayton     curr_ap.release();
7152cad65a5SGreg Clayton     return;
7162cad65a5SGreg Clayton   }
7172cad65a5SGreg Clayton 
718b9c1b51eSKate Stone   if (!prev_sp || prev_sp->GetNumFrames(false) == 0) {
7192cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES)
7202cad65a5SGreg Clayton     s.PutCString("No previous frames, so use current frames...\n");
7212cad65a5SGreg Clayton #endif
7222cad65a5SGreg Clayton     // We either don't have any previous frames, or since we have more than
7232cad65a5SGreg Clayton     // one current frames it means we have all the frames and can safely
7242cad65a5SGreg Clayton     // replace our previous frames.
7252cad65a5SGreg Clayton     prev_sp.reset(curr_ap.release());
7262cad65a5SGreg Clayton     return;
7272cad65a5SGreg Clayton   }
7282cad65a5SGreg Clayton 
7292cad65a5SGreg Clayton   const uint32_t num_curr_frames = curr_ap->GetNumFrames(false);
7302cad65a5SGreg Clayton 
731b9c1b51eSKate Stone   if (num_curr_frames > 1) {
7322cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES)
733b9c1b51eSKate Stone     s.PutCString(
734b9c1b51eSKate Stone         "We have more than one current frame, so use current frames...\n");
7352cad65a5SGreg Clayton #endif
7362cad65a5SGreg Clayton     // We have more than one current frames it means we have all the frames
7372cad65a5SGreg Clayton     // and can safely replace our previous frames.
7382cad65a5SGreg Clayton     prev_sp.reset(curr_ap.release());
7392cad65a5SGreg Clayton 
7402cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES)
7412cad65a5SGreg Clayton     s.PutCString("\nMerged:\n");
7422cad65a5SGreg Clayton     prev_sp->Dump(&s);
7432cad65a5SGreg Clayton #endif
7442cad65a5SGreg Clayton     return;
7452cad65a5SGreg Clayton   }
7462cad65a5SGreg Clayton 
747b57e4a1bSJason Molenda   StackFrameSP prev_frame_zero_sp(prev_sp->GetFrameAtIndex(0));
748b57e4a1bSJason Molenda   StackFrameSP curr_frame_zero_sp(curr_ap->GetFrameAtIndex(0));
7492cad65a5SGreg Clayton   StackID curr_stack_id(curr_frame_zero_sp->GetStackID());
7502cad65a5SGreg Clayton   StackID prev_stack_id(prev_frame_zero_sp->GetStackID());
7512cad65a5SGreg Clayton 
7522cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES)
7532c595273SJohnny Chen   const uint32_t num_prev_frames = prev_sp->GetNumFrames(false);
7542cad65a5SGreg Clayton   s.Printf("\n%u previous frames with one current frame\n", num_prev_frames);
7552cad65a5SGreg Clayton #endif
7562cad65a5SGreg Clayton 
7572cad65a5SGreg Clayton   // We have only a single current frame
7582cad65a5SGreg Clayton   // Our previous stack frames only had a single frame as well...
759b9c1b51eSKate Stone   if (curr_stack_id == prev_stack_id) {
7602cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES)
761b9c1b51eSKate Stone     s.Printf("\nPrevious frame #0 is same as current frame #0, merge the "
762b9c1b51eSKate Stone              "cached data\n");
7632cad65a5SGreg Clayton #endif
7642cad65a5SGreg Clayton 
765b9c1b51eSKate Stone     curr_frame_zero_sp->UpdateCurrentFrameFromPreviousFrame(
766b9c1b51eSKate Stone         *prev_frame_zero_sp);
767b9c1b51eSKate Stone     //        prev_frame_zero_sp->UpdatePreviousFrameFromCurrentFrame
768b9c1b51eSKate Stone     //        (*curr_frame_zero_sp);
7692cad65a5SGreg Clayton     //        prev_sp->SetFrameAtIndex (0, prev_frame_zero_sp);
770b9c1b51eSKate Stone   } else if (curr_stack_id < prev_stack_id) {
7712cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES)
772b9c1b51eSKate Stone     s.Printf("\nCurrent frame #0 has a stack ID that is less than the previous "
773b9c1b51eSKate Stone              "frame #0, insert current frame zero in front of previous\n");
7742cad65a5SGreg Clayton #endif
7752cad65a5SGreg Clayton     prev_sp->m_frames.insert(prev_sp->m_frames.begin(), curr_frame_zero_sp);
7762cad65a5SGreg Clayton   }
7772cad65a5SGreg Clayton 
7782cad65a5SGreg Clayton   curr_ap.release();
7792cad65a5SGreg Clayton 
7802cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES)
7812cad65a5SGreg Clayton   s.PutCString("\nMerged:\n");
7822cad65a5SGreg Clayton   prev_sp->Dump(&s);
7832cad65a5SGreg Clayton #endif
7842cad65a5SGreg Clayton }
785e4284b71SJim Ingham 
786b57e4a1bSJason Molenda lldb::StackFrameSP
787b9c1b51eSKate Stone StackFrameList::GetStackFrameSPForStackFramePtr(StackFrame *stack_frame_ptr) {
788e4284b71SJim Ingham   const_iterator pos;
789e4284b71SJim Ingham   const_iterator begin = m_frames.begin();
790e4284b71SJim Ingham   const_iterator end = m_frames.end();
791b57e4a1bSJason Molenda   lldb::StackFrameSP ret_sp;
792e4284b71SJim Ingham 
793b9c1b51eSKate Stone   for (pos = begin; pos != end; ++pos) {
794b9c1b51eSKate Stone     if (pos->get() == stack_frame_ptr) {
795e4284b71SJim Ingham       ret_sp = (*pos);
796e4284b71SJim Ingham       break;
797e4284b71SJim Ingham     }
798e4284b71SJim Ingham   }
799e4284b71SJim Ingham   return ret_sp;
800e4284b71SJim Ingham }
801e4284b71SJim Ingham 
802b9c1b51eSKate Stone size_t StackFrameList::GetStatus(Stream &strm, uint32_t first_frame,
803b9c1b51eSKate Stone                                  uint32_t num_frames, bool show_frame_info,
8048ec10efcSJim Ingham                                  uint32_t num_frames_with_source,
805b9c1b51eSKate Stone                                  const char *selected_frame_marker) {
8067260f620SGreg Clayton   size_t num_frames_displayed = 0;
8077260f620SGreg Clayton 
8087260f620SGreg Clayton   if (num_frames == 0)
8097260f620SGreg Clayton     return 0;
8107260f620SGreg Clayton 
811b57e4a1bSJason Molenda   StackFrameSP frame_sp;
8127260f620SGreg Clayton   uint32_t frame_idx = 0;
8137260f620SGreg Clayton   uint32_t last_frame;
8147260f620SGreg Clayton 
8157260f620SGreg Clayton   // Don't let the last frame wrap around...
8167260f620SGreg Clayton   if (num_frames == UINT32_MAX)
8177260f620SGreg Clayton     last_frame = UINT32_MAX;
8187260f620SGreg Clayton   else
8197260f620SGreg Clayton     last_frame = first_frame + num_frames;
8207260f620SGreg Clayton 
821b57e4a1bSJason Molenda   StackFrameSP selected_frame_sp = m_thread.GetSelectedFrame();
822d70a6e71SEugene Zelenko   const char *unselected_marker = nullptr;
8238ec10efcSJim Ingham   std::string buffer;
824b9c1b51eSKate Stone   if (selected_frame_marker) {
8258ec10efcSJim Ingham     size_t len = strlen(selected_frame_marker);
8268ec10efcSJim Ingham     buffer.insert(buffer.begin(), len, ' ');
8278ec10efcSJim Ingham     unselected_marker = buffer.c_str();
8288ec10efcSJim Ingham   }
829d70a6e71SEugene Zelenko   const char *marker = nullptr;
8308ec10efcSJim Ingham 
831b9c1b51eSKate Stone   for (frame_idx = first_frame; frame_idx < last_frame; ++frame_idx) {
8327260f620SGreg Clayton     frame_sp = GetFrameAtIndex(frame_idx);
833d70a6e71SEugene Zelenko     if (!frame_sp)
8347260f620SGreg Clayton       break;
8357260f620SGreg Clayton 
836b9c1b51eSKate Stone     if (selected_frame_marker != nullptr) {
8378ec10efcSJim Ingham       if (frame_sp == selected_frame_sp)
8388ec10efcSJim Ingham         marker = selected_frame_marker;
8398ec10efcSJim Ingham       else
8408ec10efcSJim Ingham         marker = unselected_marker;
8418ec10efcSJim Ingham     }
8428ec10efcSJim Ingham 
843b9c1b51eSKate Stone     if (!frame_sp->GetStatus(strm, show_frame_info,
844b9c1b51eSKate Stone                              num_frames_with_source > (first_frame - frame_idx),
845b9c1b51eSKate Stone                              marker))
8467260f620SGreg Clayton       break;
8477260f620SGreg Clayton     ++num_frames_displayed;
8487260f620SGreg Clayton   }
8497260f620SGreg Clayton 
8507260f620SGreg Clayton   strm.IndentLess();
8517260f620SGreg Clayton   return num_frames_displayed;
8527260f620SGreg Clayton }
853