130fdc8d8SChris Lattner //===-- StackFrameList.cpp --------------------------------------*- C++ -*-===// 230fdc8d8SChris Lattner // 330fdc8d8SChris Lattner // The LLVM Compiler Infrastructure 430fdc8d8SChris Lattner // 530fdc8d8SChris Lattner // This file is distributed under the University of Illinois Open Source 630fdc8d8SChris Lattner // License. See LICENSE.TXT for details. 730fdc8d8SChris Lattner // 830fdc8d8SChris Lattner //===----------------------------------------------------------------------===// 930fdc8d8SChris Lattner 1030fdc8d8SChris Lattner // C Includes 1130fdc8d8SChris Lattner // C++ Includes 1230fdc8d8SChris Lattner // Other libraries and framework includes 1330fdc8d8SChris Lattner // Project includes 14d70a6e71SEugene Zelenko #include "lldb/Target/StackFrameList.h" 15c635500dSJim Ingham #include "lldb/Breakpoint/Breakpoint.h" 16b9c1b51eSKate Stone #include "lldb/Breakpoint/BreakpointLocation.h" 17b7f6b2faSJim Ingham #include "lldb/Core/SourceManager.h" 18b9c1b51eSKate Stone #include "lldb/Core/StreamFile.h" 1912daf946SGreg Clayton #include "lldb/Symbol/Block.h" 2012daf946SGreg Clayton #include "lldb/Symbol/Function.h" 2159e8fc1cSGreg Clayton #include "lldb/Symbol/Symbol.h" 22b7f6b2faSJim Ingham #include "lldb/Target/Process.h" 2312daf946SGreg Clayton #include "lldb/Target/RegisterContext.h" 2430fdc8d8SChris Lattner #include "lldb/Target/StackFrame.h" 25513c6bb8SJim Ingham #include "lldb/Target/StopInfo.h" 26b7f6b2faSJim Ingham #include "lldb/Target/Target.h" 2712daf946SGreg Clayton #include "lldb/Target/Thread.h" 2812daf946SGreg Clayton #include "lldb/Target/Unwind.h" 296f9e6901SZachary Turner #include "lldb/Utility/Log.h" 3030fdc8d8SChris Lattner 315082c5fdSGreg Clayton //#define DEBUG_STACK_FRAMES 1 325082c5fdSGreg Clayton 3330fdc8d8SChris Lattner using namespace lldb; 3430fdc8d8SChris Lattner using namespace lldb_private; 3530fdc8d8SChris Lattner 3630fdc8d8SChris Lattner //---------------------------------------------------------------------- 3730fdc8d8SChris Lattner // StackFrameList constructor 3830fdc8d8SChris Lattner //---------------------------------------------------------------------- 39b9c1b51eSKate Stone StackFrameList::StackFrameList(Thread &thread, 40b9c1b51eSKate Stone const lldb::StackFrameListSP &prev_frames_sp, 41b9c1b51eSKate Stone bool show_inline_frames) 42b9c1b51eSKate Stone : m_thread(thread), m_prev_frames_sp(prev_frames_sp), m_mutex(), m_frames(), 43b9c1b51eSKate Stone m_selected_frame_idx(0), m_concrete_frames_fetched(0), 44513c6bb8SJim Ingham m_current_inlined_depth(UINT32_MAX), 45513c6bb8SJim Ingham m_current_inlined_pc(LLDB_INVALID_ADDRESS), 46b9c1b51eSKate Stone m_show_inlined_frames(show_inline_frames) { 47b9c1b51eSKate Stone if (prev_frames_sp) { 48513c6bb8SJim Ingham m_current_inlined_depth = prev_frames_sp->m_current_inlined_depth; 49513c6bb8SJim Ingham m_current_inlined_pc = prev_frames_sp->m_current_inlined_pc; 50513c6bb8SJim Ingham } 5130fdc8d8SChris Lattner } 5230fdc8d8SChris Lattner 53b9c1b51eSKate Stone StackFrameList::~StackFrameList() { 5405097246SAdrian Prantl // Call clear since this takes a lock and clears the stack frame list in case 5505097246SAdrian Prantl // another thread is currently using this stack frame list 56ca5ce187SGreg Clayton Clear(); 5730fdc8d8SChris Lattner } 5830fdc8d8SChris Lattner 59b9c1b51eSKate Stone void StackFrameList::CalculateCurrentInlinedDepth() { 60513c6bb8SJim Ingham uint32_t cur_inlined_depth = GetCurrentInlinedDepth(); 61b9c1b51eSKate Stone if (cur_inlined_depth == UINT32_MAX) { 62513c6bb8SJim Ingham ResetCurrentInlinedDepth(); 63513c6bb8SJim Ingham } 64513c6bb8SJim Ingham } 65513c6bb8SJim Ingham 66b9c1b51eSKate Stone uint32_t StackFrameList::GetCurrentInlinedDepth() { 67b9c1b51eSKate Stone if (m_show_inlined_frames && m_current_inlined_pc != LLDB_INVALID_ADDRESS) { 68513c6bb8SJim Ingham lldb::addr_t cur_pc = m_thread.GetRegisterContext()->GetPC(); 69b9c1b51eSKate Stone if (cur_pc != m_current_inlined_pc) { 70513c6bb8SJim Ingham m_current_inlined_pc = LLDB_INVALID_ADDRESS; 71513c6bb8SJim Ingham m_current_inlined_depth = UINT32_MAX; 725160ce5cSGreg Clayton Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); 736cd41da7SJim Ingham if (log && log->GetVerbose()) 74b9c1b51eSKate Stone log->Printf( 75b9c1b51eSKate Stone "GetCurrentInlinedDepth: invalidating current inlined depth.\n"); 76513c6bb8SJim Ingham } 77513c6bb8SJim Ingham return m_current_inlined_depth; 78b9c1b51eSKate Stone } else { 79513c6bb8SJim Ingham return UINT32_MAX; 80513c6bb8SJim Ingham } 81513c6bb8SJim Ingham } 82513c6bb8SJim Ingham 83b9c1b51eSKate Stone void StackFrameList::ResetCurrentInlinedDepth() { 84e7167e03SVedant Kumar if (!m_show_inlined_frames) 85e7167e03SVedant Kumar return; 86e7167e03SVedant Kumar 87bb19a13cSSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 8847db4a2cSKeno Fischer 89513c6bb8SJim Ingham GetFramesUpTo(0); 90d70a6e71SEugene Zelenko if (m_frames.empty()) 9165d4d5c3SRyan Brown return; 92b9c1b51eSKate Stone if (!m_frames[0]->IsInlined()) { 93513c6bb8SJim Ingham m_current_inlined_depth = UINT32_MAX; 94513c6bb8SJim Ingham m_current_inlined_pc = LLDB_INVALID_ADDRESS; 955160ce5cSGreg Clayton Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); 966cd41da7SJim Ingham if (log && log->GetVerbose()) 97b9c1b51eSKate Stone log->Printf( 98b9c1b51eSKate Stone "ResetCurrentInlinedDepth: Invalidating current inlined depth.\n"); 99e7167e03SVedant Kumar return; 100e7167e03SVedant Kumar } 101e7167e03SVedant Kumar 10205097246SAdrian Prantl // We only need to do something special about inlined blocks when we are 10305097246SAdrian Prantl // at the beginning of an inlined function: 104b9c1b51eSKate Stone // FIXME: We probably also have to do something special if the PC is at 105e7167e03SVedant Kumar // the END of an inlined function, which coincides with the end of either 106e7167e03SVedant Kumar // its containing function or another inlined function. 107513c6bb8SJim Ingham 108513c6bb8SJim Ingham Block *block_ptr = m_frames[0]->GetFrameBlock(); 109e7167e03SVedant Kumar if (!block_ptr) 110e7167e03SVedant Kumar return; 111e7167e03SVedant Kumar 112513c6bb8SJim Ingham Address pc_as_address; 113e7167e03SVedant Kumar lldb::addr_t curr_pc = m_thread.GetRegisterContext()->GetPC(); 114e7167e03SVedant Kumar pc_as_address.SetLoadAddress(curr_pc, &(m_thread.GetProcess()->GetTarget())); 115513c6bb8SJim Ingham AddressRange containing_range; 116e7167e03SVedant Kumar if (!block_ptr->GetRangeContainingAddress(pc_as_address, containing_range) || 117e7167e03SVedant Kumar pc_as_address != containing_range.GetBaseAddress()) 118e7167e03SVedant Kumar return; 119e7167e03SVedant Kumar 120e7167e03SVedant Kumar // If we got here because of a breakpoint hit, then set the inlined depth 121e7167e03SVedant Kumar // depending on where the breakpoint was set. If we got here because of a 122e7167e03SVedant Kumar // crash, then set the inlined depth to the deepest most block. Otherwise, 123e7167e03SVedant Kumar // we stopped here naturally as the result of a step, so set ourselves in the 124e7167e03SVedant Kumar // containing frame of the whole set of nested inlines, so the user can then 125e7167e03SVedant Kumar // "virtually" step into the frames one by one, or next over the whole mess. 126e7167e03SVedant Kumar // Note: We don't have to handle being somewhere in the middle of the stack 127e7167e03SVedant Kumar // here, since ResetCurrentInlinedDepth doesn't get called if there is a 128e7167e03SVedant Kumar // valid inlined depth set. 129513c6bb8SJim Ingham StopInfoSP stop_info_sp = m_thread.GetStopInfo(); 130e7167e03SVedant Kumar if (!stop_info_sp) 131e7167e03SVedant Kumar return; 132b9c1b51eSKate Stone switch (stop_info_sp->GetStopReason()) { 133513c6bb8SJim Ingham case eStopReasonWatchpoint: 134513c6bb8SJim Ingham case eStopReasonException: 13590ba8115SGreg Clayton case eStopReasonExec: 136513c6bb8SJim Ingham case eStopReasonSignal: 137e7167e03SVedant Kumar // In all these cases we want to stop in the deepest frame. 138513c6bb8SJim Ingham m_current_inlined_pc = curr_pc; 139513c6bb8SJim Ingham m_current_inlined_depth = 0; 140513c6bb8SJim Ingham break; 141b9c1b51eSKate Stone case eStopReasonBreakpoint: { 142e7167e03SVedant Kumar // FIXME: Figure out what this break point is doing, and set the inline 143e7167e03SVedant Kumar // depth appropriately. Be careful to take into account breakpoints that 144e7167e03SVedant Kumar // implement step over prologue, since that should do the default 145e7167e03SVedant Kumar // calculation. For now, if the breakpoints corresponding to this hit are 146e7167e03SVedant Kumar // all internal, I set the stop location to the top of the inlined stack, 147e7167e03SVedant Kumar // since that will make things like stepping over prologues work right. 148e7167e03SVedant Kumar // But if there are any non-internal breakpoints I do to the bottom of the 149e7167e03SVedant Kumar // stack, since that was the old behavior. 150c635500dSJim Ingham uint32_t bp_site_id = stop_info_sp->GetValue(); 151b9c1b51eSKate Stone BreakpointSiteSP bp_site_sp( 152e7167e03SVedant Kumar m_thread.GetProcess()->GetBreakpointSiteList().FindByID(bp_site_id)); 153c635500dSJim Ingham bool all_internal = true; 154b9c1b51eSKate Stone if (bp_site_sp) { 155c635500dSJim Ingham uint32_t num_owners = bp_site_sp->GetNumberOfOwners(); 156b9c1b51eSKate Stone for (uint32_t i = 0; i < num_owners; i++) { 157e7167e03SVedant Kumar Breakpoint &bp_ref = bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint(); 158b9c1b51eSKate Stone if (!bp_ref.IsInternal()) { 159c635500dSJim Ingham all_internal = false; 160c635500dSJim Ingham } 161c635500dSJim Ingham } 162c635500dSJim Ingham } 163b9c1b51eSKate Stone if (!all_internal) { 164c635500dSJim Ingham m_current_inlined_pc = curr_pc; 165c635500dSJim Ingham m_current_inlined_depth = 0; 166c635500dSJim Ingham break; 167c635500dSJim Ingham } 1687da851a3SJim Ingham } 169cec91ef9SGreg Clayton LLVM_FALLTHROUGH; 170b9c1b51eSKate Stone default: { 171e7167e03SVedant Kumar // Otherwise, we should set ourselves at the container of the inlining, so 172e7167e03SVedant Kumar // that the user can descend into them. So first we check whether we have 173e7167e03SVedant Kumar // more than one inlined block sharing this PC: 174513c6bb8SJim Ingham int num_inlined_functions = 0; 175513c6bb8SJim Ingham 176513c6bb8SJim Ingham for (Block *container_ptr = block_ptr->GetInlinedParent(); 177d70a6e71SEugene Zelenko container_ptr != nullptr; 178b9c1b51eSKate Stone container_ptr = container_ptr->GetInlinedParent()) { 179e7167e03SVedant Kumar if (!container_ptr->GetRangeContainingAddress(pc_as_address, 180e7167e03SVedant Kumar containing_range)) 181513c6bb8SJim Ingham break; 182513c6bb8SJim Ingham if (pc_as_address != containing_range.GetBaseAddress()) 183513c6bb8SJim Ingham break; 184513c6bb8SJim Ingham 185513c6bb8SJim Ingham num_inlined_functions++; 186513c6bb8SJim Ingham } 187513c6bb8SJim Ingham m_current_inlined_pc = curr_pc; 188513c6bb8SJim Ingham m_current_inlined_depth = num_inlined_functions + 1; 189e7167e03SVedant Kumar Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); 1906cd41da7SJim Ingham if (log && log->GetVerbose()) 191b9c1b51eSKate Stone log->Printf("ResetCurrentInlinedDepth: setting inlined " 192b9c1b51eSKate Stone "depth: %d 0x%" PRIx64 ".\n", 193b9c1b51eSKate Stone m_current_inlined_depth, curr_pc); 194513c6bb8SJim Ingham 195e7167e03SVedant Kumar break; 196513c6bb8SJim Ingham } 197513c6bb8SJim Ingham } 198513c6bb8SJim Ingham } 199513c6bb8SJim Ingham 200b9c1b51eSKate Stone bool StackFrameList::DecrementCurrentInlinedDepth() { 201b9c1b51eSKate Stone if (m_show_inlined_frames) { 202513c6bb8SJim Ingham uint32_t current_inlined_depth = GetCurrentInlinedDepth(); 203b9c1b51eSKate Stone if (current_inlined_depth != UINT32_MAX) { 204b9c1b51eSKate Stone if (current_inlined_depth > 0) { 205513c6bb8SJim Ingham m_current_inlined_depth--; 206513c6bb8SJim Ingham return true; 207513c6bb8SJim Ingham } 208513c6bb8SJim Ingham } 2099786eeebSJim Ingham } 210513c6bb8SJim Ingham return false; 211513c6bb8SJim Ingham } 212513c6bb8SJim Ingham 213b9c1b51eSKate Stone void StackFrameList::SetCurrentInlinedDepth(uint32_t new_depth) { 2146cd41da7SJim Ingham m_current_inlined_depth = new_depth; 2156cd41da7SJim Ingham if (new_depth == UINT32_MAX) 2166cd41da7SJim Ingham m_current_inlined_pc = LLDB_INVALID_ADDRESS; 2176cd41da7SJim Ingham else 2186cd41da7SJim Ingham m_current_inlined_pc = m_thread.GetRegisterContext()->GetPC(); 2196cd41da7SJim Ingham } 2206cd41da7SJim Ingham 221eb8fa58eSVedant Kumar void StackFrameList::GetOnlyConcreteFramesUpTo(uint32_t end_idx, 222eb8fa58eSVedant Kumar Unwind *unwinder) { 223eb8fa58eSVedant Kumar assert(m_thread.IsValid() && "Expected valid thread"); 224eb8fa58eSVedant Kumar assert(m_frames.size() <= end_idx && "Expected there to be frames to fill"); 225eb8fa58eSVedant Kumar 226eb8fa58eSVedant Kumar if (end_idx < m_concrete_frames_fetched) 227eb8fa58eSVedant Kumar return; 228eb8fa58eSVedant Kumar 229eb8fa58eSVedant Kumar if (!unwinder) 230eb8fa58eSVedant Kumar return; 231eb8fa58eSVedant Kumar 232eb8fa58eSVedant Kumar uint32_t num_frames = unwinder->GetFramesUpTo(end_idx); 233eb8fa58eSVedant Kumar if (num_frames <= end_idx + 1) { 234eb8fa58eSVedant Kumar // Done unwinding. 235eb8fa58eSVedant Kumar m_concrete_frames_fetched = UINT32_MAX; 236eb8fa58eSVedant Kumar } 237*33e51b14SVedant Kumar 238*33e51b14SVedant Kumar // Don't create the frames eagerly. Defer this work to GetFrameAtIndex, 239*33e51b14SVedant Kumar // which can lazily query the unwinder to create frames. 240eb8fa58eSVedant Kumar m_frames.resize(num_frames); 241eb8fa58eSVedant Kumar } 242eb8fa58eSVedant Kumar 243b9c1b51eSKate Stone void StackFrameList::GetFramesUpTo(uint32_t end_idx) { 244eb8fa58eSVedant Kumar // Do not fetch frames for an invalid thread. 245d70a6e71SEugene Zelenko if (!m_thread.IsValid()) 246ebafd2f1SEnrico Granata return; 247ebafd2f1SEnrico Granata 248b9c1b51eSKate Stone // We've already gotten more frames than asked for, or we've already finished 249b9c1b51eSKate Stone // unwinding, return. 250b0c72a5fSJim Ingham if (m_frames.size() > end_idx || GetAllFramesFetched()) 251b0c72a5fSJim Ingham return; 25212daf946SGreg Clayton 253b0c72a5fSJim Ingham Unwind *unwinder = m_thread.GetUnwinder(); 254b0c72a5fSJim Ingham 255eb8fa58eSVedant Kumar if (!m_show_inlined_frames) { 256eb8fa58eSVedant Kumar GetOnlyConcreteFramesUpTo(end_idx, unwinder); 257eb8fa58eSVedant Kumar return; 258eb8fa58eSVedant Kumar } 259eb8fa58eSVedant Kumar 2605082c5fdSGreg Clayton #if defined(DEBUG_STACK_FRAMES) 2612c595273SJohnny Chen StreamFile s(stdout, false); 2625082c5fdSGreg Clayton #endif 26305097246SAdrian Prantl // If we are hiding some frames from the outside world, we need to add 26405097246SAdrian Prantl // those onto the total count of frames to fetch. However, we don't need 26505097246SAdrian Prantl // to do that if end_idx is 0 since in that case we always get the first 26605097246SAdrian Prantl // concrete frame and all the inlined frames below it... And of course, if 26705097246SAdrian Prantl // end_idx is UINT32_MAX that means get all, so just do that... 268513c6bb8SJim Ingham 269513c6bb8SJim Ingham uint32_t inlined_depth = 0; 270b9c1b51eSKate Stone if (end_idx > 0 && end_idx != UINT32_MAX) { 271513c6bb8SJim Ingham inlined_depth = GetCurrentInlinedDepth(); 272b9c1b51eSKate Stone if (inlined_depth != UINT32_MAX) { 273513c6bb8SJim Ingham if (end_idx > 0) 274513c6bb8SJim Ingham end_idx += inlined_depth; 275513c6bb8SJim Ingham } 276513c6bb8SJim Ingham } 27712daf946SGreg Clayton 278b57e4a1bSJason Molenda StackFrameSP unwind_frame_sp; 279b9c1b51eSKate Stone do { 280b0c72a5fSJim Ingham uint32_t idx = m_concrete_frames_fetched++; 2818012cadbSGreg Clayton lldb::addr_t pc = LLDB_INVALID_ADDRESS; 2828012cadbSGreg Clayton lldb::addr_t cfa = LLDB_INVALID_ADDRESS; 283b9c1b51eSKate Stone if (idx == 0) { 28405097246SAdrian Prantl // We might have already created frame zero, only create it if we need 285eb8fa58eSVedant Kumar // to. 286b9c1b51eSKate Stone if (m_frames.empty()) { 287b3ae8761SGreg Clayton RegisterContextSP reg_ctx_sp(m_thread.GetRegisterContext()); 288b3ae8761SGreg Clayton 289b9c1b51eSKate Stone if (reg_ctx_sp) { 290b9c1b51eSKate Stone const bool success = 291b9c1b51eSKate Stone unwinder && unwinder->GetFrameInfoAtIndex(idx, cfa, pc); 29205097246SAdrian Prantl // There shouldn't be any way not to get the frame info for frame 29305097246SAdrian Prantl // 0. But if the unwinder can't make one, lets make one by hand 294eb8fa58eSVedant Kumar // with the SP as the CFA and see if that gets any further. 295b9c1b51eSKate Stone if (!success) { 296b3ae8761SGreg Clayton cfa = reg_ctx_sp->GetSP(); 297b3ae8761SGreg Clayton pc = reg_ctx_sp->GetPC(); 298376c4854SJim Ingham } 299376c4854SJim Ingham 300d9e416c0SGreg Clayton unwind_frame_sp.reset(new StackFrame(m_thread.shared_from_this(), 301eb8fa58eSVedant Kumar m_frames.size(), idx, reg_ctx_sp, 302eb8fa58eSVedant Kumar cfa, pc, nullptr)); 3035082c5fdSGreg Clayton m_frames.push_back(unwind_frame_sp); 3045082c5fdSGreg Clayton } 305b9c1b51eSKate Stone } else { 3065082c5fdSGreg Clayton unwind_frame_sp = m_frames.front(); 307b57e4a1bSJason Molenda cfa = unwind_frame_sp->m_id.GetCallFrameAddress(); 3085082c5fdSGreg Clayton } 309b9c1b51eSKate Stone } else { 310b9c1b51eSKate Stone const bool success = 311b9c1b51eSKate Stone unwinder && unwinder->GetFrameInfoAtIndex(idx, cfa, pc); 312b9c1b51eSKate Stone if (!success) { 313b0c72a5fSJim Ingham // We've gotten to the end of the stack. 314b0c72a5fSJim Ingham SetAllFramesFetched(); 315b0c72a5fSJim Ingham break; 316b0c72a5fSJim Ingham } 31799618476SJason Molenda const bool cfa_is_valid = true; 31899618476SJason Molenda const bool stop_id_is_valid = false; 31999618476SJason Molenda const bool is_history_frame = false; 320b9c1b51eSKate Stone unwind_frame_sp.reset(new StackFrame( 321eb8fa58eSVedant Kumar m_thread.shared_from_this(), m_frames.size(), idx, cfa, cfa_is_valid, 322eb8fa58eSVedant Kumar pc, 0, stop_id_is_valid, is_history_frame, nullptr)); 3235082c5fdSGreg Clayton m_frames.push_back(unwind_frame_sp); 32412daf946SGreg Clayton } 32512daf946SGreg Clayton 326ff1b5c42SEd Maste assert(unwind_frame_sp); 327b9c1b51eSKate Stone SymbolContext unwind_sc = unwind_frame_sp->GetSymbolContext( 328b9c1b51eSKate Stone eSymbolContextBlock | eSymbolContextFunction); 3291ed54f50SGreg Clayton Block *unwind_block = unwind_sc.block; 330b9c1b51eSKate Stone if (unwind_block) { 3315f4c61e2SGreg Clayton Address curr_frame_address(unwind_frame_sp->GetFrameCodeAddress()); 332911d5784STed Woodward TargetSP target_sp = m_thread.CalculateTarget(); 33305097246SAdrian Prantl // Be sure to adjust the frame address to match the address that was 33405097246SAdrian Prantl // used to lookup the symbol context above. If we are in the first 33505097246SAdrian Prantl // concrete frame, then we lookup using the current address, else we 33605097246SAdrian Prantl // decrement the address by one to get the correct location. 337b9c1b51eSKate Stone if (idx > 0) { 338b9c1b51eSKate Stone if (curr_frame_address.GetOffset() == 0) { 339b9c1b51eSKate Stone // If curr_frame_address points to the first address in a section 34005097246SAdrian Prantl // then after adjustment it will point to an other section. In that 34105097246SAdrian Prantl // case resolve the address again to the correct section plus 34205097246SAdrian Prantl // offset form. 343b9c1b51eSKate Stone addr_t load_addr = curr_frame_address.GetOpcodeLoadAddress( 34404803b3eSTatyana Krasnukha target_sp.get(), AddressClass::eCode); 345b9c1b51eSKate Stone curr_frame_address.SetOpcodeLoadAddress( 34604803b3eSTatyana Krasnukha load_addr - 1, target_sp.get(), AddressClass::eCode); 347b9c1b51eSKate Stone } else { 3485f4c61e2SGreg Clayton curr_frame_address.Slide(-1); 3490f8452baSTamas Berghammer } 3500f8452baSTamas Berghammer } 3515f4c61e2SGreg Clayton 3521ed54f50SGreg Clayton SymbolContext next_frame_sc; 3531ed54f50SGreg Clayton Address next_frame_address; 3541ed54f50SGreg Clayton 355b9c1b51eSKate Stone while (unwind_sc.GetParentOfInlinedScope( 356b9c1b51eSKate Stone curr_frame_address, next_frame_sc, next_frame_address)) { 357911d5784STed Woodward next_frame_sc.line_entry.ApplyFileMappings(target_sp); 358b9c1b51eSKate Stone StackFrameSP frame_sp( 359b9c1b51eSKate Stone new StackFrame(m_thread.shared_from_this(), m_frames.size(), idx, 360b9c1b51eSKate Stone unwind_frame_sp->GetRegisterContextSP(), cfa, 361b9c1b51eSKate Stone next_frame_address, &next_frame_sc)); 3625082c5fdSGreg Clayton 3635082c5fdSGreg Clayton m_frames.push_back(frame_sp); 3641ed54f50SGreg Clayton unwind_sc = next_frame_sc; 3651ed54f50SGreg Clayton curr_frame_address = next_frame_address; 366b0c72a5fSJim Ingham } 367b0c72a5fSJim Ingham } 368b0c72a5fSJim Ingham } while (m_frames.size() - 1 < end_idx); 3691ed54f50SGreg Clayton 370b0c72a5fSJim Ingham // Don't try to merge till you've calculated all the frames in this stack. 371b9c1b51eSKate Stone if (GetAllFramesFetched() && m_prev_frames_sp) { 3722cad65a5SGreg Clayton StackFrameList *prev_frames = m_prev_frames_sp.get(); 3735082c5fdSGreg Clayton StackFrameList *curr_frames = this; 3745082c5fdSGreg Clayton 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 412eb8fa58eSVedant Kumar // 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); 41705097246SAdrian Prantl // Now copy the fixed up previous frame into the current frames so the 418eb8fa58eSVedant Kumar // pointer doesn't change. 41959e8fc1cSGreg Clayton m_frames[curr_frame_idx] = prev_frame_sp; 4205082c5fdSGreg Clayton 4215082c5fdSGreg Clayton #if defined(DEBUG_STACK_FRAMES) 42268275d5eSGreg Clayton s.Printf("\n Copying previous frame to current frame"); 4235082c5fdSGreg Clayton #endif 4245082c5fdSGreg Clayton } 425eb8fa58eSVedant Kumar // We are done with the old stack frame list, we can release it now. 4262cad65a5SGreg Clayton m_prev_frames_sp.reset(); 4275082c5fdSGreg Clayton } 42868275d5eSGreg Clayton 42968275d5eSGreg Clayton #if defined(DEBUG_STACK_FRAMES) 43068275d5eSGreg Clayton s.PutCString("\n\nNew frames:\n"); 43168275d5eSGreg Clayton Dump(&s); 43268275d5eSGreg Clayton s.EOL(); 43368275d5eSGreg Clayton #endif 434ec6829eaSGreg Clayton } 435b0c72a5fSJim Ingham 436b9c1b51eSKate Stone uint32_t StackFrameList::GetNumFrames(bool can_create) { 437bb19a13cSSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 438b0c72a5fSJim Ingham 439b0c72a5fSJim Ingham if (can_create) 440b0c72a5fSJim Ingham GetFramesUpTo(UINT32_MAX); 441513c6bb8SJim Ingham 442b3b7b1bfSVedant Kumar return GetVisibleStackFrameIndex(m_frames.size()); 44330fdc8d8SChris Lattner } 44430fdc8d8SChris Lattner 445b9c1b51eSKate Stone void StackFrameList::Dump(Stream *s) { 446d70a6e71SEugene Zelenko if (s == nullptr) 4475082c5fdSGreg Clayton return; 448bb19a13cSSaleem Abdulrasool 449bb19a13cSSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 45030fdc8d8SChris Lattner 4515082c5fdSGreg Clayton const_iterator pos, begin = m_frames.begin(), end = m_frames.end(); 452b9c1b51eSKate Stone for (pos = begin; pos != end; ++pos) { 453b57e4a1bSJason Molenda StackFrame *frame = (*pos).get(); 454324a1036SSaleem Abdulrasool s->Printf("%p: ", static_cast<void *>(frame)); 455b9c1b51eSKate Stone if (frame) { 45659e8fc1cSGreg Clayton frame->GetStackID().Dump(s); 4570603aa9dSGreg Clayton frame->DumpUsingSettingsFormat(s); 458b9c1b51eSKate Stone } else 459dce502edSGreg Clayton s->Printf("frame #%u", (uint32_t)std::distance(begin, pos)); 4605082c5fdSGreg Clayton s->EOL(); 46112daf946SGreg Clayton } 4625082c5fdSGreg Clayton s->EOL(); 4635082c5fdSGreg Clayton } 46412daf946SGreg Clayton 465b9c1b51eSKate Stone StackFrameSP StackFrameList::GetFrameAtIndex(uint32_t idx) { 466b57e4a1bSJason Molenda StackFrameSP frame_sp; 467bb19a13cSSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 4685cb9a184SJim Ingham uint32_t original_idx = idx; 4695cb9a184SJim Ingham 470513c6bb8SJim Ingham uint32_t inlined_depth = GetCurrentInlinedDepth(); 471513c6bb8SJim Ingham if (inlined_depth != UINT32_MAX) 472513c6bb8SJim Ingham idx += inlined_depth; 473513c6bb8SJim Ingham 4745082c5fdSGreg Clayton if (idx < m_frames.size()) 4755082c5fdSGreg Clayton frame_sp = m_frames[idx]; 47612daf946SGreg Clayton 4775082c5fdSGreg Clayton if (frame_sp) 47812daf946SGreg Clayton return frame_sp; 47912daf946SGreg Clayton 48005097246SAdrian Prantl // GetFramesUpTo will fill m_frames with as many frames as you asked for, if 48105097246SAdrian Prantl // there are that many. If there weren't then you asked for too many frames. 482b0c72a5fSJim Ingham GetFramesUpTo(idx); 483b9c1b51eSKate Stone if (idx < m_frames.size()) { 484b9c1b51eSKate Stone if (m_show_inlined_frames) { 485b9c1b51eSKate Stone // When inline frames are enabled we actually create all the frames in 486b9c1b51eSKate Stone // GetFramesUpTo. 4875082c5fdSGreg Clayton frame_sp = m_frames[idx]; 488b9c1b51eSKate Stone } else { 48912daf946SGreg Clayton Unwind *unwinder = m_thread.GetUnwinder(); 490b9c1b51eSKate Stone if (unwinder) { 49112daf946SGreg Clayton addr_t pc, cfa; 492b9c1b51eSKate Stone if (unwinder->GetFrameInfoAtIndex(idx, cfa, pc)) { 49399618476SJason Molenda const bool cfa_is_valid = true; 49499618476SJason Molenda const bool stop_id_is_valid = false; 49599618476SJason Molenda const bool is_history_frame = false; 496b9c1b51eSKate Stone frame_sp.reset(new StackFrame( 497b9c1b51eSKate Stone m_thread.shared_from_this(), idx, idx, cfa, cfa_is_valid, pc, 0, 498d70a6e71SEugene Zelenko stop_id_is_valid, is_history_frame, nullptr)); 49959e8fc1cSGreg Clayton 500b9c1b51eSKate Stone Function *function = 501b9c1b51eSKate Stone frame_sp->GetSymbolContext(eSymbolContextFunction).function; 502b9c1b51eSKate Stone if (function) { 50305097246SAdrian Prantl // When we aren't showing inline functions we always use the top 50405097246SAdrian Prantl // most function block as the scope. 50559e8fc1cSGreg Clayton frame_sp->SetSymbolContextScope(&function->GetBlock(false)); 506b9c1b51eSKate Stone } else { 507b9c1b51eSKate Stone // Set the symbol scope from the symbol regardless if it is nullptr 508b9c1b51eSKate Stone // or valid. 509b9c1b51eSKate Stone frame_sp->SetSymbolContextScope( 510b9c1b51eSKate Stone frame_sp->GetSymbolContext(eSymbolContextSymbol).symbol); 51159e8fc1cSGreg Clayton } 5125082c5fdSGreg Clayton SetFrameAtIndex(idx, frame_sp); 51312daf946SGreg Clayton } 51412daf946SGreg Clayton } 51512daf946SGreg Clayton } 516b9c1b51eSKate Stone } else if (original_idx == 0) { 517b9c1b51eSKate Stone // There should ALWAYS be a frame at index 0. If something went wrong with 51805097246SAdrian Prantl // the CurrentInlinedDepth such that there weren't as many frames as we 51905097246SAdrian Prantl // thought taking that into account, then reset the current inlined depth 5205cb9a184SJim Ingham // and return the real zeroth frame. 521b9c1b51eSKate Stone if (m_frames.empty()) { 522b9c1b51eSKate Stone // Why do we have a thread with zero frames, that should not ever 523b9c1b51eSKate Stone // happen... 524a322f36cSDavid Blaikie assert(!m_thread.IsValid() && "A valid thread has no frames."); 525b9c1b51eSKate Stone } else { 526d70a6e71SEugene Zelenko ResetCurrentInlinedDepth(); 527d70a6e71SEugene Zelenko frame_sp = m_frames[original_idx]; 5285cb9a184SJim Ingham } 5295cb9a184SJim Ingham } 5305cb9a184SJim Ingham 53130fdc8d8SChris Lattner return frame_sp; 53230fdc8d8SChris Lattner } 53330fdc8d8SChris Lattner 534b57e4a1bSJason Molenda StackFrameSP 535b9c1b51eSKate Stone StackFrameList::GetFrameWithConcreteFrameIndex(uint32_t unwind_idx) { 5365ccbd294SGreg Clayton // First try assuming the unwind index is the same as the frame index. The 53705097246SAdrian Prantl // unwind index is always greater than or equal to the frame index, so it is 53805097246SAdrian Prantl // a good place to start. If we have inlined frames we might have 5 concrete 53905097246SAdrian Prantl // frames (frame unwind indexes go from 0-4), but we might have 15 frames 54005097246SAdrian Prantl // after we make all the inlined frames. Most of the time the unwind frame 54105097246SAdrian Prantl // index (or the concrete frame index) is the same as the frame index. 5425ccbd294SGreg Clayton uint32_t frame_idx = unwind_idx; 543b57e4a1bSJason Molenda StackFrameSP frame_sp(GetFrameAtIndex(frame_idx)); 544b9c1b51eSKate Stone while (frame_sp) { 5455ccbd294SGreg Clayton if (frame_sp->GetFrameIndex() == unwind_idx) 5465ccbd294SGreg Clayton break; 5475ccbd294SGreg Clayton frame_sp = GetFrameAtIndex(++frame_idx); 5485ccbd294SGreg Clayton } 5495ccbd294SGreg Clayton return frame_sp; 5505ccbd294SGreg Clayton } 5515ccbd294SGreg Clayton 552b9c1b51eSKate Stone static bool CompareStackID(const StackFrameSP &stack_sp, 553b9c1b51eSKate Stone const StackID &stack_id) { 5547bcb93d5SGreg Clayton return stack_sp->GetStackID() < stack_id; 5557bcb93d5SGreg Clayton } 5567bcb93d5SGreg Clayton 557b9c1b51eSKate Stone StackFrameSP StackFrameList::GetFrameWithStackID(const StackID &stack_id) { 558b57e4a1bSJason Molenda StackFrameSP frame_sp; 5597bcb93d5SGreg Clayton 560b9c1b51eSKate Stone if (stack_id.IsValid()) { 561bb19a13cSSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 5627bcb93d5SGreg Clayton uint32_t frame_idx = 0; 5637bcb93d5SGreg Clayton // Do a binary search in case the stack frame is already in our cache 5647bcb93d5SGreg Clayton collection::const_iterator begin = m_frames.begin(); 5657bcb93d5SGreg Clayton collection::const_iterator end = m_frames.end(); 566b9c1b51eSKate Stone if (begin != end) { 567b9c1b51eSKate Stone collection::const_iterator pos = 568b9c1b51eSKate Stone std::lower_bound(begin, end, stack_id, CompareStackID); 569b9c1b51eSKate Stone if (pos != end) { 5708012cadbSGreg Clayton if ((*pos)->GetStackID() == stack_id) 5717bcb93d5SGreg Clayton return *pos; 5728012cadbSGreg Clayton } 5737bcb93d5SGreg Clayton } 574b9c1b51eSKate Stone do { 5753a195b7eSJim Ingham frame_sp = GetFrameAtIndex(frame_idx); 5763a195b7eSJim Ingham if (frame_sp && frame_sp->GetStackID() == stack_id) 5773a195b7eSJim Ingham break; 5783a195b7eSJim Ingham frame_idx++; 579b9c1b51eSKate Stone } while (frame_sp); 5807bcb93d5SGreg Clayton } 5813a195b7eSJim Ingham return frame_sp; 5823a195b7eSJim Ingham } 5835ccbd294SGreg Clayton 584b9c1b51eSKate Stone bool StackFrameList::SetFrameAtIndex(uint32_t idx, StackFrameSP &frame_sp) { 5855082c5fdSGreg Clayton if (idx >= m_frames.size()) 5865082c5fdSGreg Clayton m_frames.resize(idx + 1); 58712daf946SGreg Clayton // Make sure allocation succeeded by checking bounds again 588b9c1b51eSKate Stone if (idx < m_frames.size()) { 5895082c5fdSGreg Clayton m_frames[idx] = frame_sp; 59030fdc8d8SChris Lattner return true; 59130fdc8d8SChris Lattner } 59230fdc8d8SChris Lattner return false; // resize failed, out of memory? 59330fdc8d8SChris Lattner } 59430fdc8d8SChris Lattner 595b9c1b51eSKate Stone uint32_t StackFrameList::GetSelectedFrameIndex() const { 596bb19a13cSSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 5972976d00aSJim Ingham return m_selected_frame_idx; 59830fdc8d8SChris Lattner } 59930fdc8d8SChris Lattner 600b9c1b51eSKate Stone uint32_t StackFrameList::SetSelectedFrame(lldb_private::StackFrame *frame) { 601bb19a13cSSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 60212daf946SGreg Clayton const_iterator pos; 6035082c5fdSGreg Clayton const_iterator begin = m_frames.begin(); 6045082c5fdSGreg Clayton const_iterator end = m_frames.end(); 605b7f6b2faSJim Ingham m_selected_frame_idx = 0; 606b9c1b51eSKate Stone for (pos = begin; pos != end; ++pos) { 607b9c1b51eSKate Stone if (pos->get() == frame) { 6082976d00aSJim Ingham m_selected_frame_idx = std::distance(begin, pos); 609513c6bb8SJim Ingham uint32_t inlined_depth = GetCurrentInlinedDepth(); 610513c6bb8SJim Ingham if (inlined_depth != UINT32_MAX) 611513c6bb8SJim Ingham m_selected_frame_idx -= inlined_depth; 612b7f6b2faSJim Ingham break; 61330fdc8d8SChris Lattner } 61430fdc8d8SChris Lattner } 615b7f6b2faSJim Ingham SetDefaultFileAndLineToSelectedFrame(); 6162976d00aSJim Ingham return m_selected_frame_idx; 61730fdc8d8SChris Lattner } 61830fdc8d8SChris Lattner 619b9c1b51eSKate Stone bool StackFrameList::SetSelectedFrameByIndex(uint32_t idx) { 620bb19a13cSSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 621b57e4a1bSJason Molenda StackFrameSP frame_sp(GetFrameAtIndex(idx)); 622b9c1b51eSKate Stone if (frame_sp) { 623b0c72a5fSJim Ingham SetSelectedFrame(frame_sp.get()); 624b0c72a5fSJim Ingham return true; 625b9c1b51eSKate Stone } else 626b0c72a5fSJim Ingham return false; 627b7f6b2faSJim Ingham } 628b7f6b2faSJim Ingham 629b9c1b51eSKate Stone void StackFrameList::SetDefaultFileAndLineToSelectedFrame() { 630b9c1b51eSKate Stone if (m_thread.GetID() == 631b9c1b51eSKate Stone m_thread.GetProcess()->GetThreadList().GetSelectedThread()->GetID()) { 632b57e4a1bSJason Molenda StackFrameSP frame_sp(GetFrameAtIndex(GetSelectedFrameIndex())); 633b9c1b51eSKate Stone if (frame_sp) { 634252d0edeSGreg Clayton SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextLineEntry); 635b7f6b2faSJim Ingham if (sc.line_entry.file) 636b9c1b51eSKate Stone m_thread.CalculateTarget()->GetSourceManager().SetDefaultFileAndLine( 637b9c1b51eSKate Stone sc.line_entry.file, sc.line_entry.line); 638b7f6b2faSJim Ingham } 639b7f6b2faSJim Ingham } 64030fdc8d8SChris Lattner } 64130fdc8d8SChris Lattner 64230fdc8d8SChris Lattner // The thread has been run, reset the number stack frames to zero so we can 64330fdc8d8SChris Lattner // determine how many frames we have lazily. 644b9c1b51eSKate Stone void StackFrameList::Clear() { 645bb19a13cSSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 6465082c5fdSGreg Clayton m_frames.clear(); 647b0c72a5fSJim Ingham m_concrete_frames_fetched = 0; 64830fdc8d8SChris Lattner } 64930fdc8d8SChris Lattner 650b9c1b51eSKate Stone void StackFrameList::Merge(std::unique_ptr<StackFrameList> &curr_ap, 651b9c1b51eSKate Stone lldb::StackFrameListSP &prev_sp) { 652bb19a13cSSaleem Abdulrasool std::unique_lock<std::recursive_mutex> current_lock, previous_lock; 653bb19a13cSSaleem Abdulrasool if (curr_ap) 654bb19a13cSSaleem Abdulrasool current_lock = std::unique_lock<std::recursive_mutex>(curr_ap->m_mutex); 655bb19a13cSSaleem Abdulrasool if (prev_sp) 656bb19a13cSSaleem Abdulrasool previous_lock = std::unique_lock<std::recursive_mutex>(prev_sp->m_mutex); 6572cad65a5SGreg Clayton 6582cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES) 6592c595273SJohnny Chen StreamFile s(stdout, false); 6602cad65a5SGreg Clayton s.PutCString("\n\nStackFrameList::Merge():\nPrev:\n"); 661d70a6e71SEugene Zelenko if (prev_sp) 6622cad65a5SGreg Clayton prev_sp->Dump(&s); 6632cad65a5SGreg Clayton else 6642cad65a5SGreg Clayton s.PutCString("NULL"); 6652cad65a5SGreg Clayton s.PutCString("\nCurr:\n"); 666d70a6e71SEugene Zelenko if (curr_ap) 6672cad65a5SGreg Clayton curr_ap->Dump(&s); 6682cad65a5SGreg Clayton else 6692cad65a5SGreg Clayton s.PutCString("NULL"); 6702cad65a5SGreg Clayton s.EOL(); 6712cad65a5SGreg Clayton #endif 6722cad65a5SGreg Clayton 673b9c1b51eSKate Stone if (!curr_ap || curr_ap->GetNumFrames(false) == 0) { 6742cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES) 6752cad65a5SGreg Clayton s.PutCString("No current frames, leave previous frames alone...\n"); 6762cad65a5SGreg Clayton #endif 6772cad65a5SGreg Clayton curr_ap.release(); 6782cad65a5SGreg Clayton return; 6792cad65a5SGreg Clayton } 6802cad65a5SGreg Clayton 681b9c1b51eSKate Stone if (!prev_sp || prev_sp->GetNumFrames(false) == 0) { 6822cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES) 6832cad65a5SGreg Clayton s.PutCString("No previous frames, so use current frames...\n"); 6842cad65a5SGreg Clayton #endif 68505097246SAdrian Prantl // We either don't have any previous frames, or since we have more than one 68605097246SAdrian Prantl // current frames it means we have all the frames and can safely replace 68705097246SAdrian Prantl // our previous frames. 6882cad65a5SGreg Clayton prev_sp.reset(curr_ap.release()); 6892cad65a5SGreg Clayton return; 6902cad65a5SGreg Clayton } 6912cad65a5SGreg Clayton 6922cad65a5SGreg Clayton const uint32_t num_curr_frames = curr_ap->GetNumFrames(false); 6932cad65a5SGreg Clayton 694b9c1b51eSKate Stone if (num_curr_frames > 1) { 6952cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES) 696b9c1b51eSKate Stone s.PutCString( 697b9c1b51eSKate Stone "We have more than one current frame, so use current frames...\n"); 6982cad65a5SGreg Clayton #endif 69905097246SAdrian Prantl // We have more than one current frames it means we have all the frames and 70005097246SAdrian Prantl // can safely replace our previous frames. 7012cad65a5SGreg Clayton prev_sp.reset(curr_ap.release()); 7022cad65a5SGreg Clayton 7032cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES) 7042cad65a5SGreg Clayton s.PutCString("\nMerged:\n"); 7052cad65a5SGreg Clayton prev_sp->Dump(&s); 7062cad65a5SGreg Clayton #endif 7072cad65a5SGreg Clayton return; 7082cad65a5SGreg Clayton } 7092cad65a5SGreg Clayton 710b57e4a1bSJason Molenda StackFrameSP prev_frame_zero_sp(prev_sp->GetFrameAtIndex(0)); 711b57e4a1bSJason Molenda StackFrameSP curr_frame_zero_sp(curr_ap->GetFrameAtIndex(0)); 7122cad65a5SGreg Clayton StackID curr_stack_id(curr_frame_zero_sp->GetStackID()); 7132cad65a5SGreg Clayton StackID prev_stack_id(prev_frame_zero_sp->GetStackID()); 7142cad65a5SGreg Clayton 7152cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES) 7162c595273SJohnny Chen const uint32_t num_prev_frames = prev_sp->GetNumFrames(false); 7172cad65a5SGreg Clayton s.Printf("\n%u previous frames with one current frame\n", num_prev_frames); 7182cad65a5SGreg Clayton #endif 7192cad65a5SGreg Clayton 7202cad65a5SGreg Clayton // We have only a single current frame 7212cad65a5SGreg Clayton // Our previous stack frames only had a single frame as well... 722b9c1b51eSKate Stone if (curr_stack_id == prev_stack_id) { 7232cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES) 724b9c1b51eSKate Stone s.Printf("\nPrevious frame #0 is same as current frame #0, merge the " 725b9c1b51eSKate Stone "cached data\n"); 7262cad65a5SGreg Clayton #endif 7272cad65a5SGreg Clayton 728b9c1b51eSKate Stone curr_frame_zero_sp->UpdateCurrentFrameFromPreviousFrame( 729b9c1b51eSKate Stone *prev_frame_zero_sp); 730b9c1b51eSKate Stone // prev_frame_zero_sp->UpdatePreviousFrameFromCurrentFrame 731b9c1b51eSKate Stone // (*curr_frame_zero_sp); 7322cad65a5SGreg Clayton // prev_sp->SetFrameAtIndex (0, prev_frame_zero_sp); 733b9c1b51eSKate Stone } else if (curr_stack_id < prev_stack_id) { 7342cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES) 735b9c1b51eSKate Stone s.Printf("\nCurrent frame #0 has a stack ID that is less than the previous " 736b9c1b51eSKate Stone "frame #0, insert current frame zero in front of previous\n"); 7372cad65a5SGreg Clayton #endif 7382cad65a5SGreg Clayton prev_sp->m_frames.insert(prev_sp->m_frames.begin(), curr_frame_zero_sp); 7392cad65a5SGreg Clayton } 7402cad65a5SGreg Clayton 7412cad65a5SGreg Clayton curr_ap.release(); 7422cad65a5SGreg Clayton 7432cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES) 7442cad65a5SGreg Clayton s.PutCString("\nMerged:\n"); 7452cad65a5SGreg Clayton prev_sp->Dump(&s); 7462cad65a5SGreg Clayton #endif 7472cad65a5SGreg Clayton } 748e4284b71SJim Ingham 749b57e4a1bSJason Molenda lldb::StackFrameSP 750b9c1b51eSKate Stone StackFrameList::GetStackFrameSPForStackFramePtr(StackFrame *stack_frame_ptr) { 751e4284b71SJim Ingham const_iterator pos; 752e4284b71SJim Ingham const_iterator begin = m_frames.begin(); 753e4284b71SJim Ingham const_iterator end = m_frames.end(); 754b57e4a1bSJason Molenda lldb::StackFrameSP ret_sp; 755e4284b71SJim Ingham 756b9c1b51eSKate Stone for (pos = begin; pos != end; ++pos) { 757b9c1b51eSKate Stone if (pos->get() == stack_frame_ptr) { 758e4284b71SJim Ingham ret_sp = (*pos); 759e4284b71SJim Ingham break; 760e4284b71SJim Ingham } 761e4284b71SJim Ingham } 762e4284b71SJim Ingham return ret_sp; 763e4284b71SJim Ingham } 764e4284b71SJim Ingham 765b9c1b51eSKate Stone size_t StackFrameList::GetStatus(Stream &strm, uint32_t first_frame, 766b9c1b51eSKate Stone uint32_t num_frames, bool show_frame_info, 7678ec10efcSJim Ingham uint32_t num_frames_with_source, 7687f1c1211SPavel Labath bool show_unique, 769b9c1b51eSKate Stone const char *selected_frame_marker) { 7707260f620SGreg Clayton size_t num_frames_displayed = 0; 7717260f620SGreg Clayton 7727260f620SGreg Clayton if (num_frames == 0) 7737260f620SGreg Clayton return 0; 7747260f620SGreg Clayton 775b57e4a1bSJason Molenda StackFrameSP frame_sp; 7767260f620SGreg Clayton uint32_t frame_idx = 0; 7777260f620SGreg Clayton uint32_t last_frame; 7787260f620SGreg Clayton 7797260f620SGreg Clayton // Don't let the last frame wrap around... 7807260f620SGreg Clayton if (num_frames == UINT32_MAX) 7817260f620SGreg Clayton last_frame = UINT32_MAX; 7827260f620SGreg Clayton else 7837260f620SGreg Clayton last_frame = first_frame + num_frames; 7847260f620SGreg Clayton 785b57e4a1bSJason Molenda StackFrameSP selected_frame_sp = m_thread.GetSelectedFrame(); 786d70a6e71SEugene Zelenko const char *unselected_marker = nullptr; 7878ec10efcSJim Ingham std::string buffer; 788b9c1b51eSKate Stone if (selected_frame_marker) { 7898ec10efcSJim Ingham size_t len = strlen(selected_frame_marker); 7908ec10efcSJim Ingham buffer.insert(buffer.begin(), len, ' '); 7918ec10efcSJim Ingham unselected_marker = buffer.c_str(); 7928ec10efcSJim Ingham } 793d70a6e71SEugene Zelenko const char *marker = nullptr; 7948ec10efcSJim Ingham 795b9c1b51eSKate Stone for (frame_idx = first_frame; frame_idx < last_frame; ++frame_idx) { 7967260f620SGreg Clayton frame_sp = GetFrameAtIndex(frame_idx); 797d70a6e71SEugene Zelenko if (!frame_sp) 7987260f620SGreg Clayton break; 7997260f620SGreg Clayton 800b9c1b51eSKate Stone if (selected_frame_marker != nullptr) { 8018ec10efcSJim Ingham if (frame_sp == selected_frame_sp) 8028ec10efcSJim Ingham marker = selected_frame_marker; 8038ec10efcSJim Ingham else 8048ec10efcSJim Ingham marker = unselected_marker; 8058ec10efcSJim Ingham } 8068ec10efcSJim Ingham 807b9c1b51eSKate Stone if (!frame_sp->GetStatus(strm, show_frame_info, 808b9c1b51eSKate Stone num_frames_with_source > (first_frame - frame_idx), 8097f1c1211SPavel Labath show_unique, marker)) 8107260f620SGreg Clayton break; 8117260f620SGreg Clayton ++num_frames_displayed; 8127260f620SGreg Clayton } 8137260f620SGreg Clayton 8147260f620SGreg Clayton strm.IndentLess(); 8157260f620SGreg Clayton return num_frames_displayed; 8167260f620SGreg Clayton } 817