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" 16*b9c1b51eSKate Stone #include "lldb/Breakpoint/BreakpointLocation.h" 176cd41da7SJim Ingham #include "lldb/Core/Log.h" 18b7f6b2faSJim Ingham #include "lldb/Core/SourceManager.h" 19*b9c1b51eSKate Stone #include "lldb/Core/StreamFile.h" 2012daf946SGreg Clayton #include "lldb/Symbol/Block.h" 2112daf946SGreg Clayton #include "lldb/Symbol/Function.h" 2259e8fc1cSGreg Clayton #include "lldb/Symbol/Symbol.h" 23b7f6b2faSJim Ingham #include "lldb/Target/Process.h" 2412daf946SGreg Clayton #include "lldb/Target/RegisterContext.h" 2530fdc8d8SChris Lattner #include "lldb/Target/StackFrame.h" 26513c6bb8SJim Ingham #include "lldb/Target/StopInfo.h" 27b7f6b2faSJim Ingham #include "lldb/Target/Target.h" 2812daf946SGreg Clayton #include "lldb/Target/Thread.h" 2912daf946SGreg Clayton #include "lldb/Target/Unwind.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 //---------------------------------------------------------------------- 39*b9c1b51eSKate Stone StackFrameList::StackFrameList(Thread &thread, 40*b9c1b51eSKate Stone const lldb::StackFrameListSP &prev_frames_sp, 41*b9c1b51eSKate Stone bool show_inline_frames) 42*b9c1b51eSKate Stone : m_thread(thread), m_prev_frames_sp(prev_frames_sp), m_mutex(), m_frames(), 43*b9c1b51eSKate 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), 46*b9c1b51eSKate Stone m_show_inlined_frames(show_inline_frames) { 47*b9c1b51eSKate 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 53*b9c1b51eSKate 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 59*b9c1b51eSKate Stone void StackFrameList::CalculateCurrentInlinedDepth() { 60513c6bb8SJim Ingham uint32_t cur_inlined_depth = GetCurrentInlinedDepth(); 61*b9c1b51eSKate Stone if (cur_inlined_depth == UINT32_MAX) { 62513c6bb8SJim Ingham ResetCurrentInlinedDepth(); 63513c6bb8SJim Ingham } 64513c6bb8SJim Ingham } 65513c6bb8SJim Ingham 66*b9c1b51eSKate Stone uint32_t StackFrameList::GetCurrentInlinedDepth() { 67*b9c1b51eSKate Stone if (m_show_inlined_frames && m_current_inlined_pc != LLDB_INVALID_ADDRESS) { 68513c6bb8SJim Ingham lldb::addr_t cur_pc = m_thread.GetRegisterContext()->GetPC(); 69*b9c1b51eSKate 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()) 74*b9c1b51eSKate Stone log->Printf( 75*b9c1b51eSKate Stone "GetCurrentInlinedDepth: invalidating current inlined depth.\n"); 76513c6bb8SJim Ingham } 77513c6bb8SJim Ingham return m_current_inlined_depth; 78*b9c1b51eSKate Stone } else { 79513c6bb8SJim Ingham return UINT32_MAX; 80513c6bb8SJim Ingham } 81513c6bb8SJim Ingham } 82513c6bb8SJim Ingham 83*b9c1b51eSKate Stone void StackFrameList::ResetCurrentInlinedDepth() { 84bb19a13cSSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 8547db4a2cSKeno Fischer 86*b9c1b51eSKate Stone if (m_show_inlined_frames) { 87513c6bb8SJim Ingham GetFramesUpTo(0); 88d70a6e71SEugene Zelenko if (m_frames.empty()) 8965d4d5c3SRyan Brown return; 90*b9c1b51eSKate 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()) 95*b9c1b51eSKate Stone log->Printf( 96*b9c1b51eSKate Stone "ResetCurrentInlinedDepth: Invalidating current inlined depth.\n"); 97*b9c1b51eSKate 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: 100*b9c1b51eSKate Stone // FIXME: We probably also have to do something special if the PC is at 101*b9c1b51eSKate Stone // the END 102*b9c1b51eSKate Stone // of an inlined function, which coincides with the end of either its 103*b9c1b51eSKate 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(); 108*b9c1b51eSKate Stone if (block_ptr) { 109513c6bb8SJim Ingham Address pc_as_address; 110*b9c1b51eSKate Stone pc_as_address.SetLoadAddress(curr_pc, 111*b9c1b51eSKate Stone &(m_thread.GetProcess()->GetTarget())); 112513c6bb8SJim Ingham AddressRange containing_range; 113*b9c1b51eSKate Stone if (block_ptr->GetRangeContainingAddress(pc_as_address, 114*b9c1b51eSKate Stone containing_range)) { 115*b9c1b51eSKate Stone if (pc_as_address == containing_range.GetBaseAddress()) { 116*b9c1b51eSKate Stone // If we got here because of a breakpoint hit, then set the inlined 117*b9c1b51eSKate Stone // depth depending on where 118513c6bb8SJim Ingham // the breakpoint was set. 119*b9c1b51eSKate Stone // If we got here because of a crash, then set the inlined depth to 120*b9c1b51eSKate Stone // the deepest most block. 121*b9c1b51eSKate Stone // Otherwise, we stopped here naturally as the result of a step, so 122*b9c1b51eSKate Stone // set ourselves in the 123*b9c1b51eSKate Stone // containing frame of the whole set of nested inlines, so the user 124*b9c1b51eSKate Stone // can then "virtually" 125513c6bb8SJim Ingham // step into the frames one by one, or next over the whole mess. 126*b9c1b51eSKate Stone // Note: We don't have to handle being somewhere in the middle of 127*b9c1b51eSKate Stone // the stack here, since 128*b9c1b51eSKate Stone // ResetCurrentInlinedDepth doesn't get called if there is a valid 129*b9c1b51eSKate Stone // inlined depth set. 130513c6bb8SJim Ingham StopInfoSP stop_info_sp = m_thread.GetStopInfo(); 131*b9c1b51eSKate Stone if (stop_info_sp) { 132*b9c1b51eSKate 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; 141*b9c1b51eSKate Stone case eStopReasonBreakpoint: { 142*b9c1b51eSKate Stone // FIXME: Figure out what this break point is doing, and set the 143*b9c1b51eSKate Stone // inline depth 144*b9c1b51eSKate Stone // appropriately. Be careful to take into account breakpoints 145*b9c1b51eSKate Stone // that implement 146*b9c1b51eSKate Stone // step over prologue, since that should do the default 147*b9c1b51eSKate Stone // calculation. 148*b9c1b51eSKate Stone // For now, if the breakpoints corresponding to this hit are all 149*b9c1b51eSKate Stone // internal, 150*b9c1b51eSKate Stone // I set the stop location to the top of the inlined stack, 151*b9c1b51eSKate Stone // since that will make 152*b9c1b51eSKate Stone // things like stepping over prologues work right. But if there 153*b9c1b51eSKate Stone // are any non-internal 154*b9c1b51eSKate Stone // breakpoints I do to the bottom of the stack, since that was 155*b9c1b51eSKate Stone // the old behavior. 156c635500dSJim Ingham uint32_t bp_site_id = stop_info_sp->GetValue(); 157*b9c1b51eSKate Stone BreakpointSiteSP bp_site_sp( 158*b9c1b51eSKate Stone m_thread.GetProcess()->GetBreakpointSiteList().FindByID( 159*b9c1b51eSKate Stone bp_site_id)); 160c635500dSJim Ingham bool all_internal = true; 161*b9c1b51eSKate Stone if (bp_site_sp) { 162c635500dSJim Ingham uint32_t num_owners = bp_site_sp->GetNumberOfOwners(); 163*b9c1b51eSKate Stone for (uint32_t i = 0; i < num_owners; i++) { 164*b9c1b51eSKate Stone Breakpoint &bp_ref = 165*b9c1b51eSKate Stone bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint(); 166*b9c1b51eSKate Stone if (!bp_ref.IsInternal()) { 167c635500dSJim Ingham all_internal = false; 168c635500dSJim Ingham } 169c635500dSJim Ingham } 170c635500dSJim Ingham } 171*b9c1b51eSKate 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; 178*b9c1b51eSKate Stone default: { 179*b9c1b51eSKate Stone // Otherwise, we should set ourselves at the container of the 180*b9c1b51eSKate Stone // inlining, so that the 181513c6bb8SJim Ingham // user can descend into them. 182*b9c1b51eSKate Stone // So first we check whether we have more than one inlined block 183*b9c1b51eSKate 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; 188*b9c1b51eSKate Stone container_ptr = container_ptr->GetInlinedParent()) { 189*b9c1b51eSKate Stone if (!container_ptr->GetRangeContainingAddress( 190*b9c1b51eSKate 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; 199*b9c1b51eSKate Stone Log *log( 200*b9c1b51eSKate Stone lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); 2016cd41da7SJim Ingham if (log && log->GetVerbose()) 202*b9c1b51eSKate Stone log->Printf("ResetCurrentInlinedDepth: setting inlined " 203*b9c1b51eSKate Stone "depth: %d 0x%" PRIx64 ".\n", 204*b9c1b51eSKate Stone m_current_inlined_depth, curr_pc); 205513c6bb8SJim Ingham 206*b9c1b51eSKate Stone } break; 207513c6bb8SJim Ingham } 208513c6bb8SJim Ingham } 209513c6bb8SJim Ingham } 210513c6bb8SJim Ingham } 211513c6bb8SJim Ingham } 212513c6bb8SJim Ingham } 213513c6bb8SJim Ingham } 214513c6bb8SJim Ingham } 215513c6bb8SJim Ingham 216*b9c1b51eSKate Stone bool StackFrameList::DecrementCurrentInlinedDepth() { 217*b9c1b51eSKate Stone if (m_show_inlined_frames) { 218513c6bb8SJim Ingham uint32_t current_inlined_depth = GetCurrentInlinedDepth(); 219*b9c1b51eSKate Stone if (current_inlined_depth != UINT32_MAX) { 220*b9c1b51eSKate 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 229*b9c1b51eSKate 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 237*b9c1b51eSKate 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 242*b9c1b51eSKate Stone // We've already gotten more frames than asked for, or we've already finished 243*b9c1b51eSKate 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 249*b9c1b51eSKate Stone if (m_show_inlined_frames) { 2505082c5fdSGreg Clayton #if defined(DEBUG_STACK_FRAMES) 2512c595273SJohnny Chen StreamFile s(stdout, false); 2525082c5fdSGreg Clayton #endif 253*b9c1b51eSKate Stone // If we are hiding some frames from the outside world, we need to add those 254*b9c1b51eSKate Stone // onto the total count of 255*b9c1b51eSKate Stone // frames to fetch. However, we don't need to do that if end_idx is 0 since 256*b9c1b51eSKate Stone // in that case we always 257*b9c1b51eSKate Stone // get the first concrete frame and all the inlined frames below it... And 258*b9c1b51eSKate 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; 262*b9c1b51eSKate Stone if (end_idx > 0 && end_idx != UINT32_MAX) { 263513c6bb8SJim Ingham inlined_depth = GetCurrentInlinedDepth(); 264*b9c1b51eSKate 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; 271*b9c1b51eSKate 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; 275*b9c1b51eSKate Stone if (idx == 0) { 2765082c5fdSGreg Clayton // We might have already created frame zero, only create it 2775082c5fdSGreg Clayton // if we need to 278*b9c1b51eSKate Stone if (m_frames.empty()) { 279b3ae8761SGreg Clayton RegisterContextSP reg_ctx_sp(m_thread.GetRegisterContext()); 280b3ae8761SGreg Clayton 281*b9c1b51eSKate Stone if (reg_ctx_sp) { 282*b9c1b51eSKate Stone const bool success = 283*b9c1b51eSKate Stone unwinder && unwinder->GetFrameInfoAtIndex(idx, cfa, pc); 284376c4854SJim Ingham // There shouldn't be any way not to get the frame info for frame 0. 285*b9c1b51eSKate Stone // But if the unwinder can't make one, lets make one by hand with 286*b9c1b51eSKate Stone // the 287376c4854SJim Ingham // SP as the CFA and see if that gets any further. 288*b9c1b51eSKate 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(), 294*b9c1b51eSKate Stone m_frames.size(), idx, 295*b9c1b51eSKate Stone reg_ctx_sp, cfa, pc, nullptr)); 2965082c5fdSGreg Clayton m_frames.push_back(unwind_frame_sp); 2975082c5fdSGreg Clayton } 298*b9c1b51eSKate Stone } else { 2995082c5fdSGreg Clayton unwind_frame_sp = m_frames.front(); 300b57e4a1bSJason Molenda cfa = unwind_frame_sp->m_id.GetCallFrameAddress(); 3015082c5fdSGreg Clayton } 302*b9c1b51eSKate Stone } else { 303*b9c1b51eSKate Stone const bool success = 304*b9c1b51eSKate Stone unwinder && unwinder->GetFrameInfoAtIndex(idx, cfa, pc); 305*b9c1b51eSKate 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; 313*b9c1b51eSKate Stone unwind_frame_sp.reset(new StackFrame( 314*b9c1b51eSKate Stone m_thread.shared_from_this(), m_frames.size(), idx, cfa, 315*b9c1b51eSKate 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); 320*b9c1b51eSKate Stone SymbolContext unwind_sc = unwind_frame_sp->GetSymbolContext( 321*b9c1b51eSKate Stone eSymbolContextBlock | eSymbolContextFunction); 3221ed54f50SGreg Clayton Block *unwind_block = unwind_sc.block; 323*b9c1b51eSKate 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. 331*b9c1b51eSKate Stone if (idx > 0) { 332*b9c1b51eSKate Stone if (curr_frame_address.GetOffset() == 0) { 333*b9c1b51eSKate Stone // If curr_frame_address points to the first address in a section 334*b9c1b51eSKate Stone // then after 335*b9c1b51eSKate Stone // adjustment it will point to an other section. In that case 336*b9c1b51eSKate Stone // resolve the 3370f8452baSTamas Berghammer // address again to the correct section plus offset form. 338*b9c1b51eSKate Stone addr_t load_addr = curr_frame_address.GetOpcodeLoadAddress( 339*b9c1b51eSKate Stone target_sp.get(), eAddressClassCode); 340*b9c1b51eSKate Stone curr_frame_address.SetOpcodeLoadAddress( 341*b9c1b51eSKate Stone load_addr - 1, target_sp.get(), eAddressClassCode); 342*b9c1b51eSKate 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 350*b9c1b51eSKate Stone while (unwind_sc.GetParentOfInlinedScope( 351*b9c1b51eSKate Stone curr_frame_address, next_frame_sc, next_frame_address)) { 352911d5784STed Woodward next_frame_sc.line_entry.ApplyFileMappings(target_sp); 353*b9c1b51eSKate Stone StackFrameSP frame_sp( 354*b9c1b51eSKate Stone new StackFrame(m_thread.shared_from_this(), m_frames.size(), idx, 355*b9c1b51eSKate Stone unwind_frame_sp->GetRegisterContextSP(), cfa, 356*b9c1b51eSKate 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. 366*b9c1b51eSKate 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; 372*b9c1b51eSKate Stone // printf ("GetFramesUpTo: Copying current inlined depth: %d 0x%" PRIx64 ".\n", 373*b9c1b51eSKate 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 384*b9c1b51eSKate Stone for (curr_frame_num = curr_frames->m_frames.size(), 385*b9c1b51eSKate Stone prev_frame_num = prev_frames->m_frames.size(); 3865082c5fdSGreg Clayton curr_frame_num > 0 && prev_frame_num > 0; 387*b9c1b51eSKate 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 435*b9c1b51eSKate Stone } else { 436b0c72a5fSJim Ingham if (end_idx < m_concrete_frames_fetched) 437b0c72a5fSJim Ingham return; 438b0c72a5fSJim Ingham 439*b9c1b51eSKate Stone if (unwinder) { 440b0c72a5fSJim Ingham uint32_t num_frames = unwinder->GetFramesUpTo(end_idx); 441*b9c1b51eSKate 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 450*b9c1b51eSKate 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 463*b9c1b51eSKate 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(); 470*b9c1b51eSKate Stone for (pos = begin; pos != end; ++pos) { 471b57e4a1bSJason Molenda StackFrame *frame = (*pos).get(); 472324a1036SSaleem Abdulrasool s->Printf("%p: ", static_cast<void *>(frame)); 473*b9c1b51eSKate Stone if (frame) { 47459e8fc1cSGreg Clayton frame->GetStackID().Dump(s); 4750603aa9dSGreg Clayton frame->DumpUsingSettingsFormat(s); 476*b9c1b51eSKate 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 483*b9c1b51eSKate 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); 502*b9c1b51eSKate Stone if (idx < m_frames.size()) { 503*b9c1b51eSKate Stone if (m_show_inlined_frames) { 504*b9c1b51eSKate Stone // When inline frames are enabled we actually create all the frames in 505*b9c1b51eSKate Stone // GetFramesUpTo. 5065082c5fdSGreg Clayton frame_sp = m_frames[idx]; 507*b9c1b51eSKate Stone } else { 50812daf946SGreg Clayton Unwind *unwinder = m_thread.GetUnwinder(); 509*b9c1b51eSKate Stone if (unwinder) { 51012daf946SGreg Clayton addr_t pc, cfa; 511*b9c1b51eSKate 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; 515*b9c1b51eSKate Stone frame_sp.reset(new StackFrame( 516*b9c1b51eSKate 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 519*b9c1b51eSKate Stone Function *function = 520*b9c1b51eSKate Stone frame_sp->GetSymbolContext(eSymbolContextFunction).function; 521*b9c1b51eSKate 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)); 525*b9c1b51eSKate Stone } else { 526*b9c1b51eSKate Stone // Set the symbol scope from the symbol regardless if it is nullptr 527*b9c1b51eSKate Stone // or valid. 528*b9c1b51eSKate Stone frame_sp->SetSymbolContextScope( 529*b9c1b51eSKate Stone frame_sp->GetSymbolContext(eSymbolContextSymbol).symbol); 53059e8fc1cSGreg Clayton } 5315082c5fdSGreg Clayton SetFrameAtIndex(idx, frame_sp); 53212daf946SGreg Clayton } 53312daf946SGreg Clayton } 53412daf946SGreg Clayton } 535*b9c1b51eSKate Stone } else if (original_idx == 0) { 536*b9c1b51eSKate Stone // There should ALWAYS be a frame at index 0. If something went wrong with 537*b9c1b51eSKate Stone // the CurrentInlinedDepth such that 538*b9c1b51eSKate Stone // there weren't as many frames as we thought taking that into account, then 539*b9c1b51eSKate Stone // reset the current inlined depth 5405cb9a184SJim Ingham // and return the real zeroth frame. 541*b9c1b51eSKate Stone if (m_frames.empty()) { 542*b9c1b51eSKate Stone // Why do we have a thread with zero frames, that should not ever 543*b9c1b51eSKate Stone // happen... 5445cb9a184SJim Ingham if (m_thread.IsValid()) 5455cb9a184SJim Ingham assert("A valid thread has no frames."); 546*b9c1b51eSKate Stone } else { 547d70a6e71SEugene Zelenko ResetCurrentInlinedDepth(); 548d70a6e71SEugene Zelenko frame_sp = m_frames[original_idx]; 5495cb9a184SJim Ingham } 5505cb9a184SJim Ingham } 5515cb9a184SJim Ingham 55230fdc8d8SChris Lattner return frame_sp; 55330fdc8d8SChris Lattner } 55430fdc8d8SChris Lattner 555b57e4a1bSJason Molenda StackFrameSP 556*b9c1b51eSKate Stone StackFrameList::GetFrameWithConcreteFrameIndex(uint32_t unwind_idx) { 5575ccbd294SGreg Clayton // First try assuming the unwind index is the same as the frame index. The 5585ccbd294SGreg Clayton // unwind index is always greater than or equal to the frame index, so it 5595ccbd294SGreg Clayton // is a good place to start. If we have inlined frames we might have 5 5605ccbd294SGreg Clayton // concrete frames (frame unwind indexes go from 0-4), but we might have 15 5615ccbd294SGreg Clayton // frames after we make all the inlined frames. Most of the time the unwind 5625ccbd294SGreg Clayton // frame index (or the concrete frame index) is the same as the frame index. 5635ccbd294SGreg Clayton uint32_t frame_idx = unwind_idx; 564b57e4a1bSJason Molenda StackFrameSP frame_sp(GetFrameAtIndex(frame_idx)); 565*b9c1b51eSKate Stone while (frame_sp) { 5665ccbd294SGreg Clayton if (frame_sp->GetFrameIndex() == unwind_idx) 5675ccbd294SGreg Clayton break; 5685ccbd294SGreg Clayton frame_sp = GetFrameAtIndex(++frame_idx); 5695ccbd294SGreg Clayton } 5705ccbd294SGreg Clayton return frame_sp; 5715ccbd294SGreg Clayton } 5725ccbd294SGreg Clayton 573*b9c1b51eSKate Stone static bool CompareStackID(const StackFrameSP &stack_sp, 574*b9c1b51eSKate Stone const StackID &stack_id) { 5757bcb93d5SGreg Clayton return stack_sp->GetStackID() < stack_id; 5767bcb93d5SGreg Clayton } 5777bcb93d5SGreg Clayton 578*b9c1b51eSKate Stone StackFrameSP StackFrameList::GetFrameWithStackID(const StackID &stack_id) { 579b57e4a1bSJason Molenda StackFrameSP frame_sp; 5807bcb93d5SGreg Clayton 581*b9c1b51eSKate Stone if (stack_id.IsValid()) { 582bb19a13cSSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 5837bcb93d5SGreg Clayton uint32_t frame_idx = 0; 5847bcb93d5SGreg Clayton // Do a binary search in case the stack frame is already in our cache 5857bcb93d5SGreg Clayton collection::const_iterator begin = m_frames.begin(); 5867bcb93d5SGreg Clayton collection::const_iterator end = m_frames.end(); 587*b9c1b51eSKate Stone if (begin != end) { 588*b9c1b51eSKate Stone collection::const_iterator pos = 589*b9c1b51eSKate Stone std::lower_bound(begin, end, stack_id, CompareStackID); 590*b9c1b51eSKate Stone if (pos != end) { 5918012cadbSGreg Clayton if ((*pos)->GetStackID() == stack_id) 5927bcb93d5SGreg Clayton return *pos; 5938012cadbSGreg Clayton } 5947bcb93d5SGreg Clayton 5958012cadbSGreg Clayton // if (m_frames.back()->GetStackID() < stack_id) 5968012cadbSGreg Clayton // frame_idx = m_frames.size(); 5977bcb93d5SGreg Clayton } 598*b9c1b51eSKate Stone do { 5993a195b7eSJim Ingham frame_sp = GetFrameAtIndex(frame_idx); 6003a195b7eSJim Ingham if (frame_sp && frame_sp->GetStackID() == stack_id) 6013a195b7eSJim Ingham break; 6023a195b7eSJim Ingham frame_idx++; 603*b9c1b51eSKate Stone } while (frame_sp); 6047bcb93d5SGreg Clayton } 6053a195b7eSJim Ingham return frame_sp; 6063a195b7eSJim Ingham } 6075ccbd294SGreg Clayton 608*b9c1b51eSKate Stone bool StackFrameList::SetFrameAtIndex(uint32_t idx, StackFrameSP &frame_sp) { 6095082c5fdSGreg Clayton if (idx >= m_frames.size()) 6105082c5fdSGreg Clayton m_frames.resize(idx + 1); 61112daf946SGreg Clayton // Make sure allocation succeeded by checking bounds again 612*b9c1b51eSKate Stone if (idx < m_frames.size()) { 6135082c5fdSGreg Clayton m_frames[idx] = frame_sp; 61430fdc8d8SChris Lattner return true; 61530fdc8d8SChris Lattner } 61630fdc8d8SChris Lattner return false; // resize failed, out of memory? 61730fdc8d8SChris Lattner } 61830fdc8d8SChris Lattner 619*b9c1b51eSKate Stone uint32_t StackFrameList::GetSelectedFrameIndex() const { 620bb19a13cSSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 6212976d00aSJim Ingham return m_selected_frame_idx; 62230fdc8d8SChris Lattner } 62330fdc8d8SChris Lattner 624*b9c1b51eSKate Stone uint32_t StackFrameList::SetSelectedFrame(lldb_private::StackFrame *frame) { 625bb19a13cSSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 62612daf946SGreg Clayton const_iterator pos; 6275082c5fdSGreg Clayton const_iterator begin = m_frames.begin(); 6285082c5fdSGreg Clayton const_iterator end = m_frames.end(); 629b7f6b2faSJim Ingham m_selected_frame_idx = 0; 630*b9c1b51eSKate Stone for (pos = begin; pos != end; ++pos) { 631*b9c1b51eSKate Stone if (pos->get() == frame) { 6322976d00aSJim Ingham m_selected_frame_idx = std::distance(begin, pos); 633513c6bb8SJim Ingham uint32_t inlined_depth = GetCurrentInlinedDepth(); 634513c6bb8SJim Ingham if (inlined_depth != UINT32_MAX) 635513c6bb8SJim Ingham m_selected_frame_idx -= inlined_depth; 636b7f6b2faSJim Ingham break; 63730fdc8d8SChris Lattner } 63830fdc8d8SChris Lattner } 639b7f6b2faSJim Ingham SetDefaultFileAndLineToSelectedFrame(); 6402976d00aSJim Ingham return m_selected_frame_idx; 64130fdc8d8SChris Lattner } 64230fdc8d8SChris Lattner 64330fdc8d8SChris Lattner // Mark a stack frame as the current frame using the frame index 644*b9c1b51eSKate Stone bool StackFrameList::SetSelectedFrameByIndex(uint32_t idx) { 645bb19a13cSSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 646b57e4a1bSJason Molenda StackFrameSP frame_sp(GetFrameAtIndex(idx)); 647*b9c1b51eSKate Stone if (frame_sp) { 648b0c72a5fSJim Ingham SetSelectedFrame(frame_sp.get()); 649b0c72a5fSJim Ingham return true; 650*b9c1b51eSKate Stone } else 651b0c72a5fSJim Ingham return false; 652b7f6b2faSJim Ingham } 653b7f6b2faSJim Ingham 654*b9c1b51eSKate Stone void StackFrameList::SetDefaultFileAndLineToSelectedFrame() { 655*b9c1b51eSKate Stone if (m_thread.GetID() == 656*b9c1b51eSKate Stone m_thread.GetProcess()->GetThreadList().GetSelectedThread()->GetID()) { 657b57e4a1bSJason Molenda StackFrameSP frame_sp(GetFrameAtIndex(GetSelectedFrameIndex())); 658*b9c1b51eSKate Stone if (frame_sp) { 659252d0edeSGreg Clayton SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextLineEntry); 660b7f6b2faSJim Ingham if (sc.line_entry.file) 661*b9c1b51eSKate Stone m_thread.CalculateTarget()->GetSourceManager().SetDefaultFileAndLine( 662*b9c1b51eSKate Stone sc.line_entry.file, sc.line_entry.line); 663b7f6b2faSJim Ingham } 664b7f6b2faSJim Ingham } 66530fdc8d8SChris Lattner } 66630fdc8d8SChris Lattner 66730fdc8d8SChris Lattner // The thread has been run, reset the number stack frames to zero so we can 66830fdc8d8SChris Lattner // determine how many frames we have lazily. 669*b9c1b51eSKate Stone void StackFrameList::Clear() { 670bb19a13cSSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 6715082c5fdSGreg Clayton m_frames.clear(); 672b0c72a5fSJim Ingham m_concrete_frames_fetched = 0; 67330fdc8d8SChris Lattner } 67430fdc8d8SChris Lattner 675*b9c1b51eSKate Stone void StackFrameList::InvalidateFrames(uint32_t start_idx) { 676bb19a13cSSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 677*b9c1b51eSKate Stone if (m_show_inlined_frames) { 67812daf946SGreg Clayton Clear(); 679*b9c1b51eSKate Stone } else { 6805082c5fdSGreg Clayton const size_t num_frames = m_frames.size(); 681*b9c1b51eSKate Stone while (start_idx < num_frames) { 6825082c5fdSGreg Clayton m_frames[start_idx].reset(); 68330fdc8d8SChris Lattner ++start_idx; 68430fdc8d8SChris Lattner } 68530fdc8d8SChris Lattner } 68612daf946SGreg Clayton } 6872cad65a5SGreg Clayton 688*b9c1b51eSKate Stone void StackFrameList::Merge(std::unique_ptr<StackFrameList> &curr_ap, 689*b9c1b51eSKate Stone lldb::StackFrameListSP &prev_sp) { 690bb19a13cSSaleem Abdulrasool std::unique_lock<std::recursive_mutex> current_lock, previous_lock; 691bb19a13cSSaleem Abdulrasool if (curr_ap) 692bb19a13cSSaleem Abdulrasool current_lock = std::unique_lock<std::recursive_mutex>(curr_ap->m_mutex); 693bb19a13cSSaleem Abdulrasool if (prev_sp) 694bb19a13cSSaleem Abdulrasool previous_lock = std::unique_lock<std::recursive_mutex>(prev_sp->m_mutex); 6952cad65a5SGreg Clayton 6962cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES) 6972c595273SJohnny Chen StreamFile s(stdout, false); 6982cad65a5SGreg Clayton s.PutCString("\n\nStackFrameList::Merge():\nPrev:\n"); 699d70a6e71SEugene Zelenko if (prev_sp) 7002cad65a5SGreg Clayton prev_sp->Dump(&s); 7012cad65a5SGreg Clayton else 7022cad65a5SGreg Clayton s.PutCString("NULL"); 7032cad65a5SGreg Clayton s.PutCString("\nCurr:\n"); 704d70a6e71SEugene Zelenko if (curr_ap) 7052cad65a5SGreg Clayton curr_ap->Dump(&s); 7062cad65a5SGreg Clayton else 7072cad65a5SGreg Clayton s.PutCString("NULL"); 7082cad65a5SGreg Clayton s.EOL(); 7092cad65a5SGreg Clayton #endif 7102cad65a5SGreg Clayton 711*b9c1b51eSKate Stone if (!curr_ap || curr_ap->GetNumFrames(false) == 0) { 7122cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES) 7132cad65a5SGreg Clayton s.PutCString("No current frames, leave previous frames alone...\n"); 7142cad65a5SGreg Clayton #endif 7152cad65a5SGreg Clayton curr_ap.release(); 7162cad65a5SGreg Clayton return; 7172cad65a5SGreg Clayton } 7182cad65a5SGreg Clayton 719*b9c1b51eSKate Stone if (!prev_sp || prev_sp->GetNumFrames(false) == 0) { 7202cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES) 7212cad65a5SGreg Clayton s.PutCString("No previous frames, so use current frames...\n"); 7222cad65a5SGreg Clayton #endif 7232cad65a5SGreg Clayton // We either don't have any previous frames, or since we have more than 7242cad65a5SGreg Clayton // one current frames it means we have all the frames and can safely 7252cad65a5SGreg Clayton // replace our previous frames. 7262cad65a5SGreg Clayton prev_sp.reset(curr_ap.release()); 7272cad65a5SGreg Clayton return; 7282cad65a5SGreg Clayton } 7292cad65a5SGreg Clayton 7302cad65a5SGreg Clayton const uint32_t num_curr_frames = curr_ap->GetNumFrames(false); 7312cad65a5SGreg Clayton 732*b9c1b51eSKate Stone if (num_curr_frames > 1) { 7332cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES) 734*b9c1b51eSKate Stone s.PutCString( 735*b9c1b51eSKate Stone "We have more than one current frame, so use current frames...\n"); 7362cad65a5SGreg Clayton #endif 7372cad65a5SGreg Clayton // We have more than one current frames it means we have all the frames 7382cad65a5SGreg Clayton // and can safely replace our previous frames. 7392cad65a5SGreg Clayton prev_sp.reset(curr_ap.release()); 7402cad65a5SGreg Clayton 7412cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES) 7422cad65a5SGreg Clayton s.PutCString("\nMerged:\n"); 7432cad65a5SGreg Clayton prev_sp->Dump(&s); 7442cad65a5SGreg Clayton #endif 7452cad65a5SGreg Clayton return; 7462cad65a5SGreg Clayton } 7472cad65a5SGreg Clayton 748b57e4a1bSJason Molenda StackFrameSP prev_frame_zero_sp(prev_sp->GetFrameAtIndex(0)); 749b57e4a1bSJason Molenda StackFrameSP curr_frame_zero_sp(curr_ap->GetFrameAtIndex(0)); 7502cad65a5SGreg Clayton StackID curr_stack_id(curr_frame_zero_sp->GetStackID()); 7512cad65a5SGreg Clayton StackID prev_stack_id(prev_frame_zero_sp->GetStackID()); 7522cad65a5SGreg Clayton 7532cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES) 7542c595273SJohnny Chen const uint32_t num_prev_frames = prev_sp->GetNumFrames(false); 7552cad65a5SGreg Clayton s.Printf("\n%u previous frames with one current frame\n", num_prev_frames); 7562cad65a5SGreg Clayton #endif 7572cad65a5SGreg Clayton 7582cad65a5SGreg Clayton // We have only a single current frame 7592cad65a5SGreg Clayton // Our previous stack frames only had a single frame as well... 760*b9c1b51eSKate Stone if (curr_stack_id == prev_stack_id) { 7612cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES) 762*b9c1b51eSKate Stone s.Printf("\nPrevious frame #0 is same as current frame #0, merge the " 763*b9c1b51eSKate Stone "cached data\n"); 7642cad65a5SGreg Clayton #endif 7652cad65a5SGreg Clayton 766*b9c1b51eSKate Stone curr_frame_zero_sp->UpdateCurrentFrameFromPreviousFrame( 767*b9c1b51eSKate Stone *prev_frame_zero_sp); 768*b9c1b51eSKate Stone // prev_frame_zero_sp->UpdatePreviousFrameFromCurrentFrame 769*b9c1b51eSKate Stone // (*curr_frame_zero_sp); 7702cad65a5SGreg Clayton // prev_sp->SetFrameAtIndex (0, prev_frame_zero_sp); 771*b9c1b51eSKate Stone } else if (curr_stack_id < prev_stack_id) { 7722cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES) 773*b9c1b51eSKate Stone s.Printf("\nCurrent frame #0 has a stack ID that is less than the previous " 774*b9c1b51eSKate Stone "frame #0, insert current frame zero in front of previous\n"); 7752cad65a5SGreg Clayton #endif 7762cad65a5SGreg Clayton prev_sp->m_frames.insert(prev_sp->m_frames.begin(), curr_frame_zero_sp); 7772cad65a5SGreg Clayton } 7782cad65a5SGreg Clayton 7792cad65a5SGreg Clayton curr_ap.release(); 7802cad65a5SGreg Clayton 7812cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES) 7822cad65a5SGreg Clayton s.PutCString("\nMerged:\n"); 7832cad65a5SGreg Clayton prev_sp->Dump(&s); 7842cad65a5SGreg Clayton #endif 7852cad65a5SGreg Clayton } 786e4284b71SJim Ingham 787b57e4a1bSJason Molenda lldb::StackFrameSP 788*b9c1b51eSKate Stone StackFrameList::GetStackFrameSPForStackFramePtr(StackFrame *stack_frame_ptr) { 789e4284b71SJim Ingham const_iterator pos; 790e4284b71SJim Ingham const_iterator begin = m_frames.begin(); 791e4284b71SJim Ingham const_iterator end = m_frames.end(); 792b57e4a1bSJason Molenda lldb::StackFrameSP ret_sp; 793e4284b71SJim Ingham 794*b9c1b51eSKate Stone for (pos = begin; pos != end; ++pos) { 795*b9c1b51eSKate Stone if (pos->get() == stack_frame_ptr) { 796e4284b71SJim Ingham ret_sp = (*pos); 797e4284b71SJim Ingham break; 798e4284b71SJim Ingham } 799e4284b71SJim Ingham } 800e4284b71SJim Ingham return ret_sp; 801e4284b71SJim Ingham } 802e4284b71SJim Ingham 803*b9c1b51eSKate Stone size_t StackFrameList::GetStatus(Stream &strm, uint32_t first_frame, 804*b9c1b51eSKate Stone uint32_t num_frames, bool show_frame_info, 8058ec10efcSJim Ingham uint32_t num_frames_with_source, 806*b9c1b51eSKate Stone const char *selected_frame_marker) { 8077260f620SGreg Clayton size_t num_frames_displayed = 0; 8087260f620SGreg Clayton 8097260f620SGreg Clayton if (num_frames == 0) 8107260f620SGreg Clayton return 0; 8117260f620SGreg Clayton 812b57e4a1bSJason Molenda StackFrameSP frame_sp; 8137260f620SGreg Clayton uint32_t frame_idx = 0; 8147260f620SGreg Clayton uint32_t last_frame; 8157260f620SGreg Clayton 8167260f620SGreg Clayton // Don't let the last frame wrap around... 8177260f620SGreg Clayton if (num_frames == UINT32_MAX) 8187260f620SGreg Clayton last_frame = UINT32_MAX; 8197260f620SGreg Clayton else 8207260f620SGreg Clayton last_frame = first_frame + num_frames; 8217260f620SGreg Clayton 822b57e4a1bSJason Molenda StackFrameSP selected_frame_sp = m_thread.GetSelectedFrame(); 823d70a6e71SEugene Zelenko const char *unselected_marker = nullptr; 8248ec10efcSJim Ingham std::string buffer; 825*b9c1b51eSKate Stone if (selected_frame_marker) { 8268ec10efcSJim Ingham size_t len = strlen(selected_frame_marker); 8278ec10efcSJim Ingham buffer.insert(buffer.begin(), len, ' '); 8288ec10efcSJim Ingham unselected_marker = buffer.c_str(); 8298ec10efcSJim Ingham } 830d70a6e71SEugene Zelenko const char *marker = nullptr; 8318ec10efcSJim Ingham 832*b9c1b51eSKate Stone for (frame_idx = first_frame; frame_idx < last_frame; ++frame_idx) { 8337260f620SGreg Clayton frame_sp = GetFrameAtIndex(frame_idx); 834d70a6e71SEugene Zelenko if (!frame_sp) 8357260f620SGreg Clayton break; 8367260f620SGreg Clayton 837*b9c1b51eSKate Stone if (selected_frame_marker != nullptr) { 8388ec10efcSJim Ingham if (frame_sp == selected_frame_sp) 8398ec10efcSJim Ingham marker = selected_frame_marker; 8408ec10efcSJim Ingham else 8418ec10efcSJim Ingham marker = unselected_marker; 8428ec10efcSJim Ingham } 8438ec10efcSJim Ingham 844*b9c1b51eSKate Stone if (!frame_sp->GetStatus(strm, show_frame_info, 845*b9c1b51eSKate Stone num_frames_with_source > (first_frame - frame_idx), 846*b9c1b51eSKate Stone marker)) 8477260f620SGreg Clayton break; 8487260f620SGreg Clayton ++num_frames_displayed; 8497260f620SGreg Clayton } 8507260f620SGreg Clayton 8517260f620SGreg Clayton strm.IndentLess(); 8527260f620SGreg Clayton return num_frames_displayed; 8537260f620SGreg Clayton } 854