130fdc8d8SChris Lattner //===-- StackFrameList.cpp --------------------------------------*- C++ -*-===// 230fdc8d8SChris Lattner // 3*2946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*2946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 5*2946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 630fdc8d8SChris Lattner // 730fdc8d8SChris Lattner //===----------------------------------------------------------------------===// 830fdc8d8SChris Lattner 9d70a6e71SEugene Zelenko #include "lldb/Target/StackFrameList.h" 10c635500dSJim Ingham #include "lldb/Breakpoint/Breakpoint.h" 11b9c1b51eSKate Stone #include "lldb/Breakpoint/BreakpointLocation.h" 12b7f6b2faSJim Ingham #include "lldb/Core/SourceManager.h" 13b9c1b51eSKate Stone #include "lldb/Core/StreamFile.h" 1412daf946SGreg Clayton #include "lldb/Symbol/Block.h" 1512daf946SGreg Clayton #include "lldb/Symbol/Function.h" 1659e8fc1cSGreg Clayton #include "lldb/Symbol/Symbol.h" 17b7f6b2faSJim Ingham #include "lldb/Target/Process.h" 1812daf946SGreg Clayton #include "lldb/Target/RegisterContext.h" 1930fdc8d8SChris Lattner #include "lldb/Target/StackFrame.h" 20513c6bb8SJim Ingham #include "lldb/Target/StopInfo.h" 21b7f6b2faSJim Ingham #include "lldb/Target/Target.h" 2212daf946SGreg Clayton #include "lldb/Target/Thread.h" 2312daf946SGreg Clayton #include "lldb/Target/Unwind.h" 246f9e6901SZachary Turner #include "lldb/Utility/Log.h" 254b36f791SVedant Kumar #include "llvm/ADT/SmallPtrSet.h" 2630fdc8d8SChris Lattner 275082c5fdSGreg Clayton //#define DEBUG_STACK_FRAMES 1 285082c5fdSGreg Clayton 2930fdc8d8SChris Lattner using namespace lldb; 3030fdc8d8SChris Lattner using namespace lldb_private; 3130fdc8d8SChris Lattner 3230fdc8d8SChris Lattner //---------------------------------------------------------------------- 3330fdc8d8SChris Lattner // StackFrameList constructor 3430fdc8d8SChris Lattner //---------------------------------------------------------------------- 35b9c1b51eSKate Stone StackFrameList::StackFrameList(Thread &thread, 36b9c1b51eSKate Stone const lldb::StackFrameListSP &prev_frames_sp, 37b9c1b51eSKate Stone bool show_inline_frames) 38b9c1b51eSKate Stone : m_thread(thread), m_prev_frames_sp(prev_frames_sp), m_mutex(), m_frames(), 39b9c1b51eSKate Stone m_selected_frame_idx(0), m_concrete_frames_fetched(0), 40513c6bb8SJim Ingham m_current_inlined_depth(UINT32_MAX), 41513c6bb8SJim Ingham m_current_inlined_pc(LLDB_INVALID_ADDRESS), 42b9c1b51eSKate Stone m_show_inlined_frames(show_inline_frames) { 43b9c1b51eSKate Stone if (prev_frames_sp) { 44513c6bb8SJim Ingham m_current_inlined_depth = prev_frames_sp->m_current_inlined_depth; 45513c6bb8SJim Ingham m_current_inlined_pc = prev_frames_sp->m_current_inlined_pc; 46513c6bb8SJim Ingham } 4730fdc8d8SChris Lattner } 4830fdc8d8SChris Lattner 49b9c1b51eSKate Stone StackFrameList::~StackFrameList() { 5005097246SAdrian Prantl // Call clear since this takes a lock and clears the stack frame list in case 5105097246SAdrian Prantl // another thread is currently using this stack frame list 52ca5ce187SGreg Clayton Clear(); 5330fdc8d8SChris Lattner } 5430fdc8d8SChris Lattner 55b9c1b51eSKate Stone void StackFrameList::CalculateCurrentInlinedDepth() { 56513c6bb8SJim Ingham uint32_t cur_inlined_depth = GetCurrentInlinedDepth(); 57b9c1b51eSKate Stone if (cur_inlined_depth == UINT32_MAX) { 58513c6bb8SJim Ingham ResetCurrentInlinedDepth(); 59513c6bb8SJim Ingham } 60513c6bb8SJim Ingham } 61513c6bb8SJim Ingham 62b9c1b51eSKate Stone uint32_t StackFrameList::GetCurrentInlinedDepth() { 63b9c1b51eSKate Stone if (m_show_inlined_frames && m_current_inlined_pc != LLDB_INVALID_ADDRESS) { 64513c6bb8SJim Ingham lldb::addr_t cur_pc = m_thread.GetRegisterContext()->GetPC(); 65b9c1b51eSKate Stone if (cur_pc != m_current_inlined_pc) { 66513c6bb8SJim Ingham m_current_inlined_pc = LLDB_INVALID_ADDRESS; 67513c6bb8SJim Ingham m_current_inlined_depth = UINT32_MAX; 685160ce5cSGreg Clayton Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); 696cd41da7SJim Ingham if (log && log->GetVerbose()) 70b9c1b51eSKate Stone log->Printf( 71b9c1b51eSKate Stone "GetCurrentInlinedDepth: invalidating current inlined depth.\n"); 72513c6bb8SJim Ingham } 73513c6bb8SJim Ingham return m_current_inlined_depth; 74b9c1b51eSKate Stone } else { 75513c6bb8SJim Ingham return UINT32_MAX; 76513c6bb8SJim Ingham } 77513c6bb8SJim Ingham } 78513c6bb8SJim Ingham 79b9c1b51eSKate Stone void StackFrameList::ResetCurrentInlinedDepth() { 80e7167e03SVedant Kumar if (!m_show_inlined_frames) 81e7167e03SVedant Kumar return; 82e7167e03SVedant Kumar 83bb19a13cSSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 8447db4a2cSKeno Fischer 85513c6bb8SJim Ingham GetFramesUpTo(0); 86d70a6e71SEugene Zelenko if (m_frames.empty()) 8765d4d5c3SRyan Brown return; 88b9c1b51eSKate Stone if (!m_frames[0]->IsInlined()) { 89513c6bb8SJim Ingham m_current_inlined_depth = UINT32_MAX; 90513c6bb8SJim Ingham m_current_inlined_pc = LLDB_INVALID_ADDRESS; 915160ce5cSGreg Clayton Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); 926cd41da7SJim Ingham if (log && log->GetVerbose()) 93b9c1b51eSKate Stone log->Printf( 94b9c1b51eSKate Stone "ResetCurrentInlinedDepth: Invalidating current inlined depth.\n"); 95e7167e03SVedant Kumar return; 96e7167e03SVedant Kumar } 97e7167e03SVedant Kumar 9805097246SAdrian Prantl // We only need to do something special about inlined blocks when we are 9905097246SAdrian Prantl // at the beginning of an inlined function: 100b9c1b51eSKate Stone // FIXME: We probably also have to do something special if the PC is at 101e7167e03SVedant Kumar // the END of an inlined function, which coincides with the end of either 102e7167e03SVedant Kumar // its containing function or another inlined function. 103513c6bb8SJim Ingham 104513c6bb8SJim Ingham Block *block_ptr = m_frames[0]->GetFrameBlock(); 105e7167e03SVedant Kumar if (!block_ptr) 106e7167e03SVedant Kumar return; 107e7167e03SVedant Kumar 108513c6bb8SJim Ingham Address pc_as_address; 109e7167e03SVedant Kumar lldb::addr_t curr_pc = m_thread.GetRegisterContext()->GetPC(); 110e7167e03SVedant Kumar pc_as_address.SetLoadAddress(curr_pc, &(m_thread.GetProcess()->GetTarget())); 111513c6bb8SJim Ingham AddressRange containing_range; 112e7167e03SVedant Kumar if (!block_ptr->GetRangeContainingAddress(pc_as_address, containing_range) || 113e7167e03SVedant Kumar pc_as_address != containing_range.GetBaseAddress()) 114e7167e03SVedant Kumar return; 115e7167e03SVedant Kumar 116e7167e03SVedant Kumar // If we got here because of a breakpoint hit, then set the inlined depth 117e7167e03SVedant Kumar // depending on where the breakpoint was set. If we got here because of a 118e7167e03SVedant Kumar // crash, then set the inlined depth to the deepest most block. Otherwise, 119e7167e03SVedant Kumar // we stopped here naturally as the result of a step, so set ourselves in the 120e7167e03SVedant Kumar // containing frame of the whole set of nested inlines, so the user can then 121e7167e03SVedant Kumar // "virtually" step into the frames one by one, or next over the whole mess. 122e7167e03SVedant Kumar // Note: We don't have to handle being somewhere in the middle of the stack 123e7167e03SVedant Kumar // here, since ResetCurrentInlinedDepth doesn't get called if there is a 124e7167e03SVedant Kumar // valid inlined depth set. 125513c6bb8SJim Ingham StopInfoSP stop_info_sp = m_thread.GetStopInfo(); 126e7167e03SVedant Kumar if (!stop_info_sp) 127e7167e03SVedant Kumar return; 128b9c1b51eSKate Stone switch (stop_info_sp->GetStopReason()) { 129513c6bb8SJim Ingham case eStopReasonWatchpoint: 130513c6bb8SJim Ingham case eStopReasonException: 13190ba8115SGreg Clayton case eStopReasonExec: 132513c6bb8SJim Ingham case eStopReasonSignal: 133e7167e03SVedant Kumar // In all these cases we want to stop in the deepest frame. 134513c6bb8SJim Ingham m_current_inlined_pc = curr_pc; 135513c6bb8SJim Ingham m_current_inlined_depth = 0; 136513c6bb8SJim Ingham break; 137b9c1b51eSKate Stone case eStopReasonBreakpoint: { 138e7167e03SVedant Kumar // FIXME: Figure out what this break point is doing, and set the inline 139e7167e03SVedant Kumar // depth appropriately. Be careful to take into account breakpoints that 140e7167e03SVedant Kumar // implement step over prologue, since that should do the default 141e7167e03SVedant Kumar // calculation. For now, if the breakpoints corresponding to this hit are 142e7167e03SVedant Kumar // all internal, I set the stop location to the top of the inlined stack, 143e7167e03SVedant Kumar // since that will make things like stepping over prologues work right. 144e7167e03SVedant Kumar // But if there are any non-internal breakpoints I do to the bottom of the 145e7167e03SVedant Kumar // stack, since that was the old behavior. 146c635500dSJim Ingham uint32_t bp_site_id = stop_info_sp->GetValue(); 147b9c1b51eSKate Stone BreakpointSiteSP bp_site_sp( 148e7167e03SVedant Kumar m_thread.GetProcess()->GetBreakpointSiteList().FindByID(bp_site_id)); 149c635500dSJim Ingham bool all_internal = true; 150b9c1b51eSKate Stone if (bp_site_sp) { 151c635500dSJim Ingham uint32_t num_owners = bp_site_sp->GetNumberOfOwners(); 152b9c1b51eSKate Stone for (uint32_t i = 0; i < num_owners; i++) { 153e7167e03SVedant Kumar Breakpoint &bp_ref = bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint(); 154b9c1b51eSKate Stone if (!bp_ref.IsInternal()) { 155c635500dSJim Ingham all_internal = false; 156c635500dSJim Ingham } 157c635500dSJim Ingham } 158c635500dSJim Ingham } 159b9c1b51eSKate Stone if (!all_internal) { 160c635500dSJim Ingham m_current_inlined_pc = curr_pc; 161c635500dSJim Ingham m_current_inlined_depth = 0; 162c635500dSJim Ingham break; 163c635500dSJim Ingham } 1647da851a3SJim Ingham } 165cec91ef9SGreg Clayton LLVM_FALLTHROUGH; 166b9c1b51eSKate Stone default: { 167e7167e03SVedant Kumar // Otherwise, we should set ourselves at the container of the inlining, so 168e7167e03SVedant Kumar // that the user can descend into them. So first we check whether we have 169e7167e03SVedant Kumar // more than one inlined block sharing this PC: 170513c6bb8SJim Ingham int num_inlined_functions = 0; 171513c6bb8SJim Ingham 172513c6bb8SJim Ingham for (Block *container_ptr = block_ptr->GetInlinedParent(); 173d70a6e71SEugene Zelenko container_ptr != nullptr; 174b9c1b51eSKate Stone container_ptr = container_ptr->GetInlinedParent()) { 175e7167e03SVedant Kumar if (!container_ptr->GetRangeContainingAddress(pc_as_address, 176e7167e03SVedant Kumar containing_range)) 177513c6bb8SJim Ingham break; 178513c6bb8SJim Ingham if (pc_as_address != containing_range.GetBaseAddress()) 179513c6bb8SJim Ingham break; 180513c6bb8SJim Ingham 181513c6bb8SJim Ingham num_inlined_functions++; 182513c6bb8SJim Ingham } 183513c6bb8SJim Ingham m_current_inlined_pc = curr_pc; 184513c6bb8SJim Ingham m_current_inlined_depth = num_inlined_functions + 1; 185e7167e03SVedant Kumar Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); 1866cd41da7SJim Ingham if (log && log->GetVerbose()) 187b9c1b51eSKate Stone log->Printf("ResetCurrentInlinedDepth: setting inlined " 188b9c1b51eSKate Stone "depth: %d 0x%" PRIx64 ".\n", 189b9c1b51eSKate Stone m_current_inlined_depth, curr_pc); 190513c6bb8SJim Ingham 191e7167e03SVedant Kumar break; 192513c6bb8SJim Ingham } 193513c6bb8SJim Ingham } 194513c6bb8SJim Ingham } 195513c6bb8SJim Ingham 196b9c1b51eSKate Stone bool StackFrameList::DecrementCurrentInlinedDepth() { 197b9c1b51eSKate Stone if (m_show_inlined_frames) { 198513c6bb8SJim Ingham uint32_t current_inlined_depth = GetCurrentInlinedDepth(); 199b9c1b51eSKate Stone if (current_inlined_depth != UINT32_MAX) { 200b9c1b51eSKate Stone if (current_inlined_depth > 0) { 201513c6bb8SJim Ingham m_current_inlined_depth--; 202513c6bb8SJim Ingham return true; 203513c6bb8SJim Ingham } 204513c6bb8SJim Ingham } 2059786eeebSJim Ingham } 206513c6bb8SJim Ingham return false; 207513c6bb8SJim Ingham } 208513c6bb8SJim Ingham 209b9c1b51eSKate Stone void StackFrameList::SetCurrentInlinedDepth(uint32_t new_depth) { 2106cd41da7SJim Ingham m_current_inlined_depth = new_depth; 2116cd41da7SJim Ingham if (new_depth == UINT32_MAX) 2126cd41da7SJim Ingham m_current_inlined_pc = LLDB_INVALID_ADDRESS; 2136cd41da7SJim Ingham else 2146cd41da7SJim Ingham m_current_inlined_pc = m_thread.GetRegisterContext()->GetPC(); 2156cd41da7SJim Ingham } 2166cd41da7SJim Ingham 217eb8fa58eSVedant Kumar void StackFrameList::GetOnlyConcreteFramesUpTo(uint32_t end_idx, 218eb8fa58eSVedant Kumar Unwind *unwinder) { 219eb8fa58eSVedant Kumar assert(m_thread.IsValid() && "Expected valid thread"); 220eb8fa58eSVedant Kumar assert(m_frames.size() <= end_idx && "Expected there to be frames to fill"); 221eb8fa58eSVedant Kumar 222eb8fa58eSVedant Kumar if (end_idx < m_concrete_frames_fetched) 223eb8fa58eSVedant Kumar return; 224eb8fa58eSVedant Kumar 225eb8fa58eSVedant Kumar if (!unwinder) 226eb8fa58eSVedant Kumar return; 227eb8fa58eSVedant Kumar 228eb8fa58eSVedant Kumar uint32_t num_frames = unwinder->GetFramesUpTo(end_idx); 229eb8fa58eSVedant Kumar if (num_frames <= end_idx + 1) { 230eb8fa58eSVedant Kumar // Done unwinding. 231eb8fa58eSVedant Kumar m_concrete_frames_fetched = UINT32_MAX; 232eb8fa58eSVedant Kumar } 23333e51b14SVedant Kumar 23433e51b14SVedant Kumar // Don't create the frames eagerly. Defer this work to GetFrameAtIndex, 23533e51b14SVedant Kumar // which can lazily query the unwinder to create frames. 236eb8fa58eSVedant Kumar m_frames.resize(num_frames); 237eb8fa58eSVedant Kumar } 238eb8fa58eSVedant Kumar 2394b36f791SVedant Kumar /// Find the unique path through the call graph from \p begin (with return PC 2404b36f791SVedant Kumar /// \p return_pc) to \p end. On success this path is stored into \p path, and 2414b36f791SVedant Kumar /// on failure \p path is unchanged. 2424b36f791SVedant Kumar static void FindInterveningFrames(Function &begin, Function &end, 2434b36f791SVedant Kumar Target &target, addr_t return_pc, 2444b36f791SVedant Kumar std::vector<Function *> &path, 2454b36f791SVedant Kumar ModuleList &images, Log *log) { 2464b36f791SVedant Kumar LLDB_LOG(log, "Finding frames between {0} and {1}, retn-pc={2:x}", 2474b36f791SVedant Kumar begin.GetDisplayName(), end.GetDisplayName(), return_pc); 2484b36f791SVedant Kumar 2494b36f791SVedant Kumar // Find a non-tail calling edge with the correct return PC. 2504b36f791SVedant Kumar auto first_level_edges = begin.GetCallEdges(); 2514b36f791SVedant Kumar if (log) 2524b36f791SVedant Kumar for (const CallEdge &edge : first_level_edges) 2534b36f791SVedant Kumar LLDB_LOG(log, "FindInterveningFrames: found call with retn-PC = {0:x}", 2544b36f791SVedant Kumar edge.GetReturnPCAddress(begin, target)); 2554b36f791SVedant Kumar auto first_edge_it = std::lower_bound( 2564b36f791SVedant Kumar first_level_edges.begin(), first_level_edges.end(), return_pc, 2574b36f791SVedant Kumar [&](const CallEdge &edge, addr_t target_pc) { 2584b36f791SVedant Kumar return edge.GetReturnPCAddress(begin, target) < target_pc; 2594b36f791SVedant Kumar }); 2604b36f791SVedant Kumar if (first_edge_it == first_level_edges.end() || 2614b36f791SVedant Kumar first_edge_it->GetReturnPCAddress(begin, target) != return_pc) { 2624b36f791SVedant Kumar LLDB_LOG(log, "No call edge outgoing from {0} with retn-PC == {1:x}", 2634b36f791SVedant Kumar begin.GetDisplayName(), return_pc); 2644b36f791SVedant Kumar return; 2654b36f791SVedant Kumar } 2664b36f791SVedant Kumar CallEdge &first_edge = const_cast<CallEdge &>(*first_edge_it); 2674b36f791SVedant Kumar 2684b36f791SVedant Kumar // The first callee may not be resolved, or there may be nothing to fill in. 2694b36f791SVedant Kumar Function *first_callee = first_edge.GetCallee(images); 2704b36f791SVedant Kumar if (!first_callee) { 2714b36f791SVedant Kumar LLDB_LOG(log, "Could not resolve callee"); 2724b36f791SVedant Kumar return; 2734b36f791SVedant Kumar } 2744b36f791SVedant Kumar if (first_callee == &end) { 2754b36f791SVedant Kumar LLDB_LOG(log, "Not searching further, first callee is {0} (retn-PC: {1:x})", 2764b36f791SVedant Kumar end.GetDisplayName(), return_pc); 2774b36f791SVedant Kumar return; 2784b36f791SVedant Kumar } 2794b36f791SVedant Kumar 2804b36f791SVedant Kumar // Run DFS on the tail-calling edges out of the first callee to find \p end. 2814b36f791SVedant Kumar // Fully explore the set of functions reachable from the first edge via tail 2824b36f791SVedant Kumar // calls in order to detect ambiguous executions. 2834b36f791SVedant Kumar struct DFS { 2844b36f791SVedant Kumar std::vector<Function *> active_path = {}; 2854b36f791SVedant Kumar std::vector<Function *> solution_path = {}; 2864b36f791SVedant Kumar llvm::SmallPtrSet<Function *, 2> visited_nodes = {}; 2874b36f791SVedant Kumar bool ambiguous = false; 2884b36f791SVedant Kumar Function *end; 2894b36f791SVedant Kumar ModuleList &images; 2904b36f791SVedant Kumar 2914b36f791SVedant Kumar DFS(Function *end, ModuleList &images) : end(end), images(images) {} 2924b36f791SVedant Kumar 2934b36f791SVedant Kumar void search(Function *first_callee, std::vector<Function *> &path) { 2944b36f791SVedant Kumar dfs(first_callee); 2954b36f791SVedant Kumar if (!ambiguous) 2964b36f791SVedant Kumar path = std::move(solution_path); 2974b36f791SVedant Kumar } 2984b36f791SVedant Kumar 2994b36f791SVedant Kumar void dfs(Function *callee) { 3004b36f791SVedant Kumar // Found a path to the target function. 3014b36f791SVedant Kumar if (callee == end) { 3024b36f791SVedant Kumar if (solution_path.empty()) 3034b36f791SVedant Kumar solution_path = active_path; 3044b36f791SVedant Kumar else 3054b36f791SVedant Kumar ambiguous = true; 3064b36f791SVedant Kumar return; 3074b36f791SVedant Kumar } 3084b36f791SVedant Kumar 3094b36f791SVedant Kumar // Terminate the search if tail recursion is found, or more generally if 3104b36f791SVedant Kumar // there's more than one way to reach a target. This errs on the side of 3114b36f791SVedant Kumar // caution: it conservatively stops searching when some solutions are 3124b36f791SVedant Kumar // still possible to save time in the average case. 3134b36f791SVedant Kumar if (!visited_nodes.insert(callee).second) { 3144b36f791SVedant Kumar ambiguous = true; 3154b36f791SVedant Kumar return; 3164b36f791SVedant Kumar } 3174b36f791SVedant Kumar 3184b36f791SVedant Kumar // Search the calls made from this callee. 3194b36f791SVedant Kumar active_path.push_back(callee); 3204b36f791SVedant Kumar for (CallEdge &edge : callee->GetTailCallingEdges()) { 3214b36f791SVedant Kumar Function *next_callee = edge.GetCallee(images); 3224b36f791SVedant Kumar if (!next_callee) 3234b36f791SVedant Kumar continue; 3244b36f791SVedant Kumar 3254b36f791SVedant Kumar dfs(next_callee); 3264b36f791SVedant Kumar if (ambiguous) 3274b36f791SVedant Kumar return; 3284b36f791SVedant Kumar } 3294b36f791SVedant Kumar active_path.pop_back(); 3304b36f791SVedant Kumar } 3314b36f791SVedant Kumar }; 3324b36f791SVedant Kumar 3334b36f791SVedant Kumar DFS(&end, images).search(first_callee, path); 3344b36f791SVedant Kumar } 3354b36f791SVedant Kumar 3364b36f791SVedant Kumar /// Given that \p next_frame will be appended to the frame list, synthesize 3374b36f791SVedant Kumar /// tail call frames between the current end of the list and \p next_frame. 3384b36f791SVedant Kumar /// If any frames are added, adjust the frame index of \p next_frame. 3394b36f791SVedant Kumar /// 3404b36f791SVedant Kumar /// -------------- 3414b36f791SVedant Kumar /// | ... | <- Completed frames. 3424b36f791SVedant Kumar /// -------------- 3434b36f791SVedant Kumar /// | prev_frame | 3444b36f791SVedant Kumar /// -------------- 3454b36f791SVedant Kumar /// | ... | <- Artificial frames inserted here. 3464b36f791SVedant Kumar /// -------------- 3474b36f791SVedant Kumar /// | next_frame | 3484b36f791SVedant Kumar /// -------------- 3494b36f791SVedant Kumar /// | ... | <- Not-yet-visited frames. 3504b36f791SVedant Kumar /// -------------- 3514b36f791SVedant Kumar void StackFrameList::SynthesizeTailCallFrames(StackFrame &next_frame) { 3524b36f791SVedant Kumar TargetSP target_sp = next_frame.CalculateTarget(); 3534b36f791SVedant Kumar if (!target_sp) 3544b36f791SVedant Kumar return; 3554b36f791SVedant Kumar 3564b36f791SVedant Kumar lldb::RegisterContextSP next_reg_ctx_sp = next_frame.GetRegisterContext(); 3574b36f791SVedant Kumar if (!next_reg_ctx_sp) 3584b36f791SVedant Kumar return; 3594b36f791SVedant Kumar 3604b36f791SVedant Kumar Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); 3614b36f791SVedant Kumar 3624b36f791SVedant Kumar assert(!m_frames.empty() && "Cannot synthesize frames in an empty stack"); 3634b36f791SVedant Kumar StackFrame &prev_frame = *m_frames.back().get(); 3644b36f791SVedant Kumar 3654b36f791SVedant Kumar // Find the functions prev_frame and next_frame are stopped in. The function 3664b36f791SVedant Kumar // objects are needed to search the lazy call graph for intervening frames. 3674b36f791SVedant Kumar Function *prev_func = 3684b36f791SVedant Kumar prev_frame.GetSymbolContext(eSymbolContextFunction).function; 3694b36f791SVedant Kumar if (!prev_func) { 3704b36f791SVedant Kumar LLDB_LOG(log, "SynthesizeTailCallFrames: can't find previous function"); 3714b36f791SVedant Kumar return; 3724b36f791SVedant Kumar } 3734b36f791SVedant Kumar Function *next_func = 3744b36f791SVedant Kumar next_frame.GetSymbolContext(eSymbolContextFunction).function; 3754b36f791SVedant Kumar if (!next_func) { 3764b36f791SVedant Kumar LLDB_LOG(log, "SynthesizeTailCallFrames: can't find next function"); 3774b36f791SVedant Kumar return; 3784b36f791SVedant Kumar } 3794b36f791SVedant Kumar 3804b36f791SVedant Kumar // Try to find the unique sequence of (tail) calls which led from next_frame 3814b36f791SVedant Kumar // to prev_frame. 3824b36f791SVedant Kumar std::vector<Function *> path; 3834b36f791SVedant Kumar addr_t return_pc = next_reg_ctx_sp->GetPC(); 3844b36f791SVedant Kumar Target &target = *target_sp.get(); 3854b36f791SVedant Kumar ModuleList &images = next_frame.CalculateTarget()->GetImages(); 3864b36f791SVedant Kumar FindInterveningFrames(*next_func, *prev_func, target, return_pc, path, images, 3874b36f791SVedant Kumar log); 3884b36f791SVedant Kumar 3894b36f791SVedant Kumar // Push synthetic tail call frames. 3904b36f791SVedant Kumar for (Function *callee : llvm::reverse(path)) { 3914b36f791SVedant Kumar uint32_t frame_idx = m_frames.size(); 3924b36f791SVedant Kumar uint32_t concrete_frame_idx = next_frame.GetConcreteFrameIndex(); 3934b36f791SVedant Kumar addr_t cfa = LLDB_INVALID_ADDRESS; 3944b36f791SVedant Kumar bool cfa_is_valid = false; 3954b36f791SVedant Kumar addr_t pc = 3964b36f791SVedant Kumar callee->GetAddressRange().GetBaseAddress().GetLoadAddress(&target); 3974b36f791SVedant Kumar SymbolContext sc; 3984b36f791SVedant Kumar callee->CalculateSymbolContext(&sc); 3994b36f791SVedant Kumar auto synth_frame = std::make_shared<StackFrame>( 4004b36f791SVedant Kumar m_thread.shared_from_this(), frame_idx, concrete_frame_idx, cfa, 4014b36f791SVedant Kumar cfa_is_valid, pc, StackFrame::Kind::Artificial, &sc); 4024b36f791SVedant Kumar m_frames.push_back(synth_frame); 4034b36f791SVedant Kumar LLDB_LOG(log, "Pushed frame {0}", callee->GetDisplayName()); 4044b36f791SVedant Kumar } 4054b36f791SVedant Kumar 4064b36f791SVedant Kumar // If any frames were created, adjust next_frame's index. 4074b36f791SVedant Kumar if (!path.empty()) 4084b36f791SVedant Kumar next_frame.SetFrameIndex(m_frames.size()); 4094b36f791SVedant Kumar } 4104b36f791SVedant Kumar 411b9c1b51eSKate Stone void StackFrameList::GetFramesUpTo(uint32_t end_idx) { 412eb8fa58eSVedant Kumar // Do not fetch frames for an invalid thread. 413d70a6e71SEugene Zelenko if (!m_thread.IsValid()) 414ebafd2f1SEnrico Granata return; 415ebafd2f1SEnrico Granata 416b9c1b51eSKate Stone // We've already gotten more frames than asked for, or we've already finished 417b9c1b51eSKate Stone // unwinding, return. 418b0c72a5fSJim Ingham if (m_frames.size() > end_idx || GetAllFramesFetched()) 419b0c72a5fSJim Ingham return; 42012daf946SGreg Clayton 421b0c72a5fSJim Ingham Unwind *unwinder = m_thread.GetUnwinder(); 422b0c72a5fSJim Ingham 423eb8fa58eSVedant Kumar if (!m_show_inlined_frames) { 424eb8fa58eSVedant Kumar GetOnlyConcreteFramesUpTo(end_idx, unwinder); 425eb8fa58eSVedant Kumar return; 426eb8fa58eSVedant Kumar } 427eb8fa58eSVedant Kumar 4285082c5fdSGreg Clayton #if defined(DEBUG_STACK_FRAMES) 4292c595273SJohnny Chen StreamFile s(stdout, false); 4305082c5fdSGreg Clayton #endif 43105097246SAdrian Prantl // If we are hiding some frames from the outside world, we need to add 43205097246SAdrian Prantl // those onto the total count of frames to fetch. However, we don't need 43305097246SAdrian Prantl // to do that if end_idx is 0 since in that case we always get the first 43405097246SAdrian Prantl // concrete frame and all the inlined frames below it... And of course, if 43505097246SAdrian Prantl // end_idx is UINT32_MAX that means get all, so just do that... 436513c6bb8SJim Ingham 437513c6bb8SJim Ingham uint32_t inlined_depth = 0; 438b9c1b51eSKate Stone if (end_idx > 0 && end_idx != UINT32_MAX) { 439513c6bb8SJim Ingham inlined_depth = GetCurrentInlinedDepth(); 440b9c1b51eSKate Stone if (inlined_depth != UINT32_MAX) { 441513c6bb8SJim Ingham if (end_idx > 0) 442513c6bb8SJim Ingham end_idx += inlined_depth; 443513c6bb8SJim Ingham } 444513c6bb8SJim Ingham } 44512daf946SGreg Clayton 446b57e4a1bSJason Molenda StackFrameSP unwind_frame_sp; 447b9c1b51eSKate Stone do { 448b0c72a5fSJim Ingham uint32_t idx = m_concrete_frames_fetched++; 4498012cadbSGreg Clayton lldb::addr_t pc = LLDB_INVALID_ADDRESS; 4508012cadbSGreg Clayton lldb::addr_t cfa = LLDB_INVALID_ADDRESS; 451b9c1b51eSKate Stone if (idx == 0) { 45205097246SAdrian Prantl // We might have already created frame zero, only create it if we need 453eb8fa58eSVedant Kumar // to. 454b9c1b51eSKate Stone if (m_frames.empty()) { 455b3ae8761SGreg Clayton RegisterContextSP reg_ctx_sp(m_thread.GetRegisterContext()); 456b3ae8761SGreg Clayton 457b9c1b51eSKate Stone if (reg_ctx_sp) { 458b9c1b51eSKate Stone const bool success = 459b9c1b51eSKate Stone unwinder && unwinder->GetFrameInfoAtIndex(idx, cfa, pc); 46005097246SAdrian Prantl // There shouldn't be any way not to get the frame info for frame 46105097246SAdrian Prantl // 0. But if the unwinder can't make one, lets make one by hand 462eb8fa58eSVedant Kumar // with the SP as the CFA and see if that gets any further. 463b9c1b51eSKate Stone if (!success) { 464b3ae8761SGreg Clayton cfa = reg_ctx_sp->GetSP(); 465b3ae8761SGreg Clayton pc = reg_ctx_sp->GetPC(); 466376c4854SJim Ingham } 467376c4854SJim Ingham 468d9e416c0SGreg Clayton unwind_frame_sp.reset(new StackFrame(m_thread.shared_from_this(), 469eb8fa58eSVedant Kumar m_frames.size(), idx, reg_ctx_sp, 470eb8fa58eSVedant Kumar cfa, pc, nullptr)); 4715082c5fdSGreg Clayton m_frames.push_back(unwind_frame_sp); 4725082c5fdSGreg Clayton } 473b9c1b51eSKate Stone } else { 4745082c5fdSGreg Clayton unwind_frame_sp = m_frames.front(); 475b57e4a1bSJason Molenda cfa = unwind_frame_sp->m_id.GetCallFrameAddress(); 4765082c5fdSGreg Clayton } 477b9c1b51eSKate Stone } else { 478b9c1b51eSKate Stone const bool success = 479b9c1b51eSKate Stone unwinder && unwinder->GetFrameInfoAtIndex(idx, cfa, pc); 480b9c1b51eSKate Stone if (!success) { 481b0c72a5fSJim Ingham // We've gotten to the end of the stack. 482b0c72a5fSJim Ingham SetAllFramesFetched(); 483b0c72a5fSJim Ingham break; 484b0c72a5fSJim Ingham } 48599618476SJason Molenda const bool cfa_is_valid = true; 4864b36f791SVedant Kumar unwind_frame_sp.reset( 4874b36f791SVedant Kumar new StackFrame(m_thread.shared_from_this(), m_frames.size(), idx, cfa, 4884b36f791SVedant Kumar cfa_is_valid, pc, StackFrame::Kind::Regular, nullptr)); 4894b36f791SVedant Kumar 4904b36f791SVedant Kumar // Create synthetic tail call frames between the previous frame and the 4914b36f791SVedant Kumar // newly-found frame. The new frame's index may change after this call, 4924b36f791SVedant Kumar // although its concrete index will stay the same. 4934b36f791SVedant Kumar SynthesizeTailCallFrames(*unwind_frame_sp.get()); 4944b36f791SVedant Kumar 4955082c5fdSGreg Clayton m_frames.push_back(unwind_frame_sp); 49612daf946SGreg Clayton } 49712daf946SGreg Clayton 498ff1b5c42SEd Maste assert(unwind_frame_sp); 499b9c1b51eSKate Stone SymbolContext unwind_sc = unwind_frame_sp->GetSymbolContext( 500b9c1b51eSKate Stone eSymbolContextBlock | eSymbolContextFunction); 5011ed54f50SGreg Clayton Block *unwind_block = unwind_sc.block; 502b9c1b51eSKate Stone if (unwind_block) { 5035f4c61e2SGreg Clayton Address curr_frame_address(unwind_frame_sp->GetFrameCodeAddress()); 504911d5784STed Woodward TargetSP target_sp = m_thread.CalculateTarget(); 50505097246SAdrian Prantl // Be sure to adjust the frame address to match the address that was 50605097246SAdrian Prantl // used to lookup the symbol context above. If we are in the first 50705097246SAdrian Prantl // concrete frame, then we lookup using the current address, else we 50805097246SAdrian Prantl // decrement the address by one to get the correct location. 509b9c1b51eSKate Stone if (idx > 0) { 510b9c1b51eSKate Stone if (curr_frame_address.GetOffset() == 0) { 511b9c1b51eSKate Stone // If curr_frame_address points to the first address in a section 51205097246SAdrian Prantl // then after adjustment it will point to an other section. In that 51305097246SAdrian Prantl // case resolve the address again to the correct section plus 51405097246SAdrian Prantl // offset form. 515b9c1b51eSKate Stone addr_t load_addr = curr_frame_address.GetOpcodeLoadAddress( 51604803b3eSTatyana Krasnukha target_sp.get(), AddressClass::eCode); 517b9c1b51eSKate Stone curr_frame_address.SetOpcodeLoadAddress( 51804803b3eSTatyana Krasnukha load_addr - 1, target_sp.get(), AddressClass::eCode); 519b9c1b51eSKate Stone } else { 5205f4c61e2SGreg Clayton curr_frame_address.Slide(-1); 5210f8452baSTamas Berghammer } 5220f8452baSTamas Berghammer } 5235f4c61e2SGreg Clayton 5241ed54f50SGreg Clayton SymbolContext next_frame_sc; 5251ed54f50SGreg Clayton Address next_frame_address; 5261ed54f50SGreg Clayton 527b9c1b51eSKate Stone while (unwind_sc.GetParentOfInlinedScope( 528b9c1b51eSKate Stone curr_frame_address, next_frame_sc, next_frame_address)) { 529911d5784STed Woodward next_frame_sc.line_entry.ApplyFileMappings(target_sp); 530b9c1b51eSKate Stone StackFrameSP frame_sp( 531b9c1b51eSKate Stone new StackFrame(m_thread.shared_from_this(), m_frames.size(), idx, 532b9c1b51eSKate Stone unwind_frame_sp->GetRegisterContextSP(), cfa, 533b9c1b51eSKate Stone next_frame_address, &next_frame_sc)); 5345082c5fdSGreg Clayton 5355082c5fdSGreg Clayton m_frames.push_back(frame_sp); 5361ed54f50SGreg Clayton unwind_sc = next_frame_sc; 5371ed54f50SGreg Clayton curr_frame_address = next_frame_address; 538b0c72a5fSJim Ingham } 539b0c72a5fSJim Ingham } 540b0c72a5fSJim Ingham } while (m_frames.size() - 1 < end_idx); 5411ed54f50SGreg Clayton 542b0c72a5fSJim Ingham // Don't try to merge till you've calculated all the frames in this stack. 543b9c1b51eSKate Stone if (GetAllFramesFetched() && m_prev_frames_sp) { 5442cad65a5SGreg Clayton StackFrameList *prev_frames = m_prev_frames_sp.get(); 5455082c5fdSGreg Clayton StackFrameList *curr_frames = this; 5465082c5fdSGreg Clayton 5475082c5fdSGreg Clayton #if defined(DEBUG_STACK_FRAMES) 54868275d5eSGreg Clayton s.PutCString("\nprev_frames:\n"); 5495082c5fdSGreg Clayton prev_frames->Dump(&s); 55068275d5eSGreg Clayton s.PutCString("\ncurr_frames:\n"); 5515082c5fdSGreg Clayton curr_frames->Dump(&s); 5525082c5fdSGreg Clayton s.EOL(); 5535082c5fdSGreg Clayton #endif 5545082c5fdSGreg Clayton size_t curr_frame_num, prev_frame_num; 5555082c5fdSGreg Clayton 556b9c1b51eSKate Stone for (curr_frame_num = curr_frames->m_frames.size(), 557b9c1b51eSKate Stone prev_frame_num = prev_frames->m_frames.size(); 5585082c5fdSGreg Clayton curr_frame_num > 0 && prev_frame_num > 0; 559b9c1b51eSKate Stone --curr_frame_num, --prev_frame_num) { 5605082c5fdSGreg Clayton const size_t curr_frame_idx = curr_frame_num - 1; 5615082c5fdSGreg Clayton const size_t prev_frame_idx = prev_frame_num - 1; 562b57e4a1bSJason Molenda StackFrameSP curr_frame_sp(curr_frames->m_frames[curr_frame_idx]); 563b57e4a1bSJason Molenda StackFrameSP prev_frame_sp(prev_frames->m_frames[prev_frame_idx]); 5645082c5fdSGreg Clayton 5655082c5fdSGreg Clayton #if defined(DEBUG_STACK_FRAMES) 5662cad65a5SGreg Clayton s.Printf("\n\nCurr frame #%u ", curr_frame_idx); 5675082c5fdSGreg Clayton if (curr_frame_sp) 5682cad65a5SGreg Clayton curr_frame_sp->Dump(&s, true, false); 5695082c5fdSGreg Clayton else 5705082c5fdSGreg Clayton s.PutCString("NULL"); 5712cad65a5SGreg Clayton s.Printf("\nPrev frame #%u ", prev_frame_idx); 5725082c5fdSGreg Clayton if (prev_frame_sp) 5732cad65a5SGreg Clayton prev_frame_sp->Dump(&s, true, false); 5745082c5fdSGreg Clayton else 5755082c5fdSGreg Clayton s.PutCString("NULL"); 5765082c5fdSGreg Clayton #endif 5775082c5fdSGreg Clayton 578b57e4a1bSJason Molenda StackFrame *curr_frame = curr_frame_sp.get(); 579b57e4a1bSJason Molenda StackFrame *prev_frame = prev_frame_sp.get(); 5805082c5fdSGreg Clayton 581d70a6e71SEugene Zelenko if (curr_frame == nullptr || prev_frame == nullptr) 5825082c5fdSGreg Clayton break; 5835082c5fdSGreg Clayton 584eb8fa58eSVedant Kumar // Check the stack ID to make sure they are equal. 58559e8fc1cSGreg Clayton if (curr_frame->GetStackID() != prev_frame->GetStackID()) 5865082c5fdSGreg Clayton break; 5875082c5fdSGreg Clayton 58859e8fc1cSGreg Clayton prev_frame->UpdatePreviousFrameFromCurrentFrame(*curr_frame); 58905097246SAdrian Prantl // Now copy the fixed up previous frame into the current frames so the 590eb8fa58eSVedant Kumar // pointer doesn't change. 59159e8fc1cSGreg Clayton m_frames[curr_frame_idx] = prev_frame_sp; 5925082c5fdSGreg Clayton 5935082c5fdSGreg Clayton #if defined(DEBUG_STACK_FRAMES) 59468275d5eSGreg Clayton s.Printf("\n Copying previous frame to current frame"); 5955082c5fdSGreg Clayton #endif 5965082c5fdSGreg Clayton } 597eb8fa58eSVedant Kumar // We are done with the old stack frame list, we can release it now. 5982cad65a5SGreg Clayton m_prev_frames_sp.reset(); 5995082c5fdSGreg Clayton } 60068275d5eSGreg Clayton 60168275d5eSGreg Clayton #if defined(DEBUG_STACK_FRAMES) 60268275d5eSGreg Clayton s.PutCString("\n\nNew frames:\n"); 60368275d5eSGreg Clayton Dump(&s); 60468275d5eSGreg Clayton s.EOL(); 60568275d5eSGreg Clayton #endif 606ec6829eaSGreg Clayton } 607b0c72a5fSJim Ingham 608b9c1b51eSKate Stone uint32_t StackFrameList::GetNumFrames(bool can_create) { 609bb19a13cSSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 610b0c72a5fSJim Ingham 611b0c72a5fSJim Ingham if (can_create) 612b0c72a5fSJim Ingham GetFramesUpTo(UINT32_MAX); 613513c6bb8SJim Ingham 614b3b7b1bfSVedant Kumar return GetVisibleStackFrameIndex(m_frames.size()); 61530fdc8d8SChris Lattner } 61630fdc8d8SChris Lattner 617b9c1b51eSKate Stone void StackFrameList::Dump(Stream *s) { 618d70a6e71SEugene Zelenko if (s == nullptr) 6195082c5fdSGreg Clayton return; 620bb19a13cSSaleem Abdulrasool 621bb19a13cSSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 62230fdc8d8SChris Lattner 6235082c5fdSGreg Clayton const_iterator pos, begin = m_frames.begin(), end = m_frames.end(); 624b9c1b51eSKate Stone for (pos = begin; pos != end; ++pos) { 625b57e4a1bSJason Molenda StackFrame *frame = (*pos).get(); 626324a1036SSaleem Abdulrasool s->Printf("%p: ", static_cast<void *>(frame)); 627b9c1b51eSKate Stone if (frame) { 62859e8fc1cSGreg Clayton frame->GetStackID().Dump(s); 6290603aa9dSGreg Clayton frame->DumpUsingSettingsFormat(s); 630b9c1b51eSKate Stone } else 631dce502edSGreg Clayton s->Printf("frame #%u", (uint32_t)std::distance(begin, pos)); 6325082c5fdSGreg Clayton s->EOL(); 63312daf946SGreg Clayton } 6345082c5fdSGreg Clayton s->EOL(); 6355082c5fdSGreg Clayton } 63612daf946SGreg Clayton 637b9c1b51eSKate Stone StackFrameSP StackFrameList::GetFrameAtIndex(uint32_t idx) { 638b57e4a1bSJason Molenda StackFrameSP frame_sp; 639bb19a13cSSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 6405cb9a184SJim Ingham uint32_t original_idx = idx; 6415cb9a184SJim Ingham 642513c6bb8SJim Ingham uint32_t inlined_depth = GetCurrentInlinedDepth(); 643513c6bb8SJim Ingham if (inlined_depth != UINT32_MAX) 644513c6bb8SJim Ingham idx += inlined_depth; 645513c6bb8SJim Ingham 6465082c5fdSGreg Clayton if (idx < m_frames.size()) 6475082c5fdSGreg Clayton frame_sp = m_frames[idx]; 64812daf946SGreg Clayton 6495082c5fdSGreg Clayton if (frame_sp) 65012daf946SGreg Clayton return frame_sp; 65112daf946SGreg Clayton 65205097246SAdrian Prantl // GetFramesUpTo will fill m_frames with as many frames as you asked for, if 65305097246SAdrian Prantl // there are that many. If there weren't then you asked for too many frames. 654b0c72a5fSJim Ingham GetFramesUpTo(idx); 655b9c1b51eSKate Stone if (idx < m_frames.size()) { 656b9c1b51eSKate Stone if (m_show_inlined_frames) { 657b9c1b51eSKate Stone // When inline frames are enabled we actually create all the frames in 658b9c1b51eSKate Stone // GetFramesUpTo. 6595082c5fdSGreg Clayton frame_sp = m_frames[idx]; 660b9c1b51eSKate Stone } else { 66112daf946SGreg Clayton Unwind *unwinder = m_thread.GetUnwinder(); 662b9c1b51eSKate Stone if (unwinder) { 66312daf946SGreg Clayton addr_t pc, cfa; 664b9c1b51eSKate Stone if (unwinder->GetFrameInfoAtIndex(idx, cfa, pc)) { 66599618476SJason Molenda const bool cfa_is_valid = true; 6664b36f791SVedant Kumar frame_sp.reset(new StackFrame(m_thread.shared_from_this(), idx, idx, 6674b36f791SVedant Kumar cfa, cfa_is_valid, pc, 6684b36f791SVedant Kumar StackFrame::Kind::Regular, nullptr)); 66959e8fc1cSGreg Clayton 670b9c1b51eSKate Stone Function *function = 671b9c1b51eSKate Stone frame_sp->GetSymbolContext(eSymbolContextFunction).function; 672b9c1b51eSKate Stone if (function) { 67305097246SAdrian Prantl // When we aren't showing inline functions we always use the top 67405097246SAdrian Prantl // most function block as the scope. 67559e8fc1cSGreg Clayton frame_sp->SetSymbolContextScope(&function->GetBlock(false)); 676b9c1b51eSKate Stone } else { 677b9c1b51eSKate Stone // Set the symbol scope from the symbol regardless if it is nullptr 678b9c1b51eSKate Stone // or valid. 679b9c1b51eSKate Stone frame_sp->SetSymbolContextScope( 680b9c1b51eSKate Stone frame_sp->GetSymbolContext(eSymbolContextSymbol).symbol); 68159e8fc1cSGreg Clayton } 6825082c5fdSGreg Clayton SetFrameAtIndex(idx, frame_sp); 68312daf946SGreg Clayton } 68412daf946SGreg Clayton } 68512daf946SGreg Clayton } 686b9c1b51eSKate Stone } else if (original_idx == 0) { 687b9c1b51eSKate Stone // There should ALWAYS be a frame at index 0. If something went wrong with 68805097246SAdrian Prantl // the CurrentInlinedDepth such that there weren't as many frames as we 68905097246SAdrian Prantl // thought taking that into account, then reset the current inlined depth 6905cb9a184SJim Ingham // and return the real zeroth frame. 691b9c1b51eSKate Stone if (m_frames.empty()) { 692b9c1b51eSKate Stone // Why do we have a thread with zero frames, that should not ever 693b9c1b51eSKate Stone // happen... 694a322f36cSDavid Blaikie assert(!m_thread.IsValid() && "A valid thread has no frames."); 695b9c1b51eSKate Stone } else { 696d70a6e71SEugene Zelenko ResetCurrentInlinedDepth(); 697d70a6e71SEugene Zelenko frame_sp = m_frames[original_idx]; 6985cb9a184SJim Ingham } 6995cb9a184SJim Ingham } 7005cb9a184SJim Ingham 70130fdc8d8SChris Lattner return frame_sp; 70230fdc8d8SChris Lattner } 70330fdc8d8SChris Lattner 704b57e4a1bSJason Molenda StackFrameSP 705b9c1b51eSKate Stone StackFrameList::GetFrameWithConcreteFrameIndex(uint32_t unwind_idx) { 7065ccbd294SGreg Clayton // First try assuming the unwind index is the same as the frame index. The 70705097246SAdrian Prantl // unwind index is always greater than or equal to the frame index, so it is 70805097246SAdrian Prantl // a good place to start. If we have inlined frames we might have 5 concrete 70905097246SAdrian Prantl // frames (frame unwind indexes go from 0-4), but we might have 15 frames 71005097246SAdrian Prantl // after we make all the inlined frames. Most of the time the unwind frame 71105097246SAdrian Prantl // index (or the concrete frame index) is the same as the frame index. 7125ccbd294SGreg Clayton uint32_t frame_idx = unwind_idx; 713b57e4a1bSJason Molenda StackFrameSP frame_sp(GetFrameAtIndex(frame_idx)); 714b9c1b51eSKate Stone while (frame_sp) { 7155ccbd294SGreg Clayton if (frame_sp->GetFrameIndex() == unwind_idx) 7165ccbd294SGreg Clayton break; 7175ccbd294SGreg Clayton frame_sp = GetFrameAtIndex(++frame_idx); 7185ccbd294SGreg Clayton } 7195ccbd294SGreg Clayton return frame_sp; 7205ccbd294SGreg Clayton } 7215ccbd294SGreg Clayton 722b9c1b51eSKate Stone static bool CompareStackID(const StackFrameSP &stack_sp, 723b9c1b51eSKate Stone const StackID &stack_id) { 7247bcb93d5SGreg Clayton return stack_sp->GetStackID() < stack_id; 7257bcb93d5SGreg Clayton } 7267bcb93d5SGreg Clayton 727b9c1b51eSKate Stone StackFrameSP StackFrameList::GetFrameWithStackID(const StackID &stack_id) { 728b57e4a1bSJason Molenda StackFrameSP frame_sp; 7297bcb93d5SGreg Clayton 730b9c1b51eSKate Stone if (stack_id.IsValid()) { 731bb19a13cSSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 7327bcb93d5SGreg Clayton uint32_t frame_idx = 0; 7337bcb93d5SGreg Clayton // Do a binary search in case the stack frame is already in our cache 7347bcb93d5SGreg Clayton collection::const_iterator begin = m_frames.begin(); 7357bcb93d5SGreg Clayton collection::const_iterator end = m_frames.end(); 736b9c1b51eSKate Stone if (begin != end) { 737b9c1b51eSKate Stone collection::const_iterator pos = 738b9c1b51eSKate Stone std::lower_bound(begin, end, stack_id, CompareStackID); 739b9c1b51eSKate Stone if (pos != end) { 7408012cadbSGreg Clayton if ((*pos)->GetStackID() == stack_id) 7417bcb93d5SGreg Clayton return *pos; 7428012cadbSGreg Clayton } 7437bcb93d5SGreg Clayton } 744b9c1b51eSKate Stone do { 7453a195b7eSJim Ingham frame_sp = GetFrameAtIndex(frame_idx); 7463a195b7eSJim Ingham if (frame_sp && frame_sp->GetStackID() == stack_id) 7473a195b7eSJim Ingham break; 7483a195b7eSJim Ingham frame_idx++; 749b9c1b51eSKate Stone } while (frame_sp); 7507bcb93d5SGreg Clayton } 7513a195b7eSJim Ingham return frame_sp; 7523a195b7eSJim Ingham } 7535ccbd294SGreg Clayton 754b9c1b51eSKate Stone bool StackFrameList::SetFrameAtIndex(uint32_t idx, StackFrameSP &frame_sp) { 7555082c5fdSGreg Clayton if (idx >= m_frames.size()) 7565082c5fdSGreg Clayton m_frames.resize(idx + 1); 75712daf946SGreg Clayton // Make sure allocation succeeded by checking bounds again 758b9c1b51eSKate Stone if (idx < m_frames.size()) { 7595082c5fdSGreg Clayton m_frames[idx] = frame_sp; 76030fdc8d8SChris Lattner return true; 76130fdc8d8SChris Lattner } 76230fdc8d8SChris Lattner return false; // resize failed, out of memory? 76330fdc8d8SChris Lattner } 76430fdc8d8SChris Lattner 765b9c1b51eSKate Stone uint32_t StackFrameList::GetSelectedFrameIndex() const { 766bb19a13cSSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 7672976d00aSJim Ingham return m_selected_frame_idx; 76830fdc8d8SChris Lattner } 76930fdc8d8SChris Lattner 770b9c1b51eSKate Stone uint32_t StackFrameList::SetSelectedFrame(lldb_private::StackFrame *frame) { 771bb19a13cSSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 77212daf946SGreg Clayton const_iterator pos; 7735082c5fdSGreg Clayton const_iterator begin = m_frames.begin(); 7745082c5fdSGreg Clayton const_iterator end = m_frames.end(); 775b7f6b2faSJim Ingham m_selected_frame_idx = 0; 776b9c1b51eSKate Stone for (pos = begin; pos != end; ++pos) { 777b9c1b51eSKate Stone if (pos->get() == frame) { 7782976d00aSJim Ingham m_selected_frame_idx = std::distance(begin, pos); 779513c6bb8SJim Ingham uint32_t inlined_depth = GetCurrentInlinedDepth(); 780513c6bb8SJim Ingham if (inlined_depth != UINT32_MAX) 781513c6bb8SJim Ingham m_selected_frame_idx -= inlined_depth; 782b7f6b2faSJim Ingham break; 78330fdc8d8SChris Lattner } 78430fdc8d8SChris Lattner } 785b7f6b2faSJim Ingham SetDefaultFileAndLineToSelectedFrame(); 7862976d00aSJim Ingham return m_selected_frame_idx; 78730fdc8d8SChris Lattner } 78830fdc8d8SChris Lattner 789b9c1b51eSKate Stone bool StackFrameList::SetSelectedFrameByIndex(uint32_t idx) { 790bb19a13cSSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 791b57e4a1bSJason Molenda StackFrameSP frame_sp(GetFrameAtIndex(idx)); 792b9c1b51eSKate Stone if (frame_sp) { 793b0c72a5fSJim Ingham SetSelectedFrame(frame_sp.get()); 794b0c72a5fSJim Ingham return true; 795b9c1b51eSKate Stone } else 796b0c72a5fSJim Ingham return false; 797b7f6b2faSJim Ingham } 798b7f6b2faSJim Ingham 799b9c1b51eSKate Stone void StackFrameList::SetDefaultFileAndLineToSelectedFrame() { 800b9c1b51eSKate Stone if (m_thread.GetID() == 801b9c1b51eSKate Stone m_thread.GetProcess()->GetThreadList().GetSelectedThread()->GetID()) { 802b57e4a1bSJason Molenda StackFrameSP frame_sp(GetFrameAtIndex(GetSelectedFrameIndex())); 803b9c1b51eSKate Stone if (frame_sp) { 804252d0edeSGreg Clayton SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextLineEntry); 805b7f6b2faSJim Ingham if (sc.line_entry.file) 806b9c1b51eSKate Stone m_thread.CalculateTarget()->GetSourceManager().SetDefaultFileAndLine( 807b9c1b51eSKate Stone sc.line_entry.file, sc.line_entry.line); 808b7f6b2faSJim Ingham } 809b7f6b2faSJim Ingham } 81030fdc8d8SChris Lattner } 81130fdc8d8SChris Lattner 81230fdc8d8SChris Lattner // The thread has been run, reset the number stack frames to zero so we can 81330fdc8d8SChris Lattner // determine how many frames we have lazily. 814b9c1b51eSKate Stone void StackFrameList::Clear() { 815bb19a13cSSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 8165082c5fdSGreg Clayton m_frames.clear(); 817b0c72a5fSJim Ingham m_concrete_frames_fetched = 0; 81830fdc8d8SChris Lattner } 81930fdc8d8SChris Lattner 820b9c1b51eSKate Stone void StackFrameList::Merge(std::unique_ptr<StackFrameList> &curr_ap, 821b9c1b51eSKate Stone lldb::StackFrameListSP &prev_sp) { 822bb19a13cSSaleem Abdulrasool std::unique_lock<std::recursive_mutex> current_lock, previous_lock; 823bb19a13cSSaleem Abdulrasool if (curr_ap) 824bb19a13cSSaleem Abdulrasool current_lock = std::unique_lock<std::recursive_mutex>(curr_ap->m_mutex); 825bb19a13cSSaleem Abdulrasool if (prev_sp) 826bb19a13cSSaleem Abdulrasool previous_lock = std::unique_lock<std::recursive_mutex>(prev_sp->m_mutex); 8272cad65a5SGreg Clayton 8282cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES) 8292c595273SJohnny Chen StreamFile s(stdout, false); 8302cad65a5SGreg Clayton s.PutCString("\n\nStackFrameList::Merge():\nPrev:\n"); 831d70a6e71SEugene Zelenko if (prev_sp) 8322cad65a5SGreg Clayton prev_sp->Dump(&s); 8332cad65a5SGreg Clayton else 8342cad65a5SGreg Clayton s.PutCString("NULL"); 8352cad65a5SGreg Clayton s.PutCString("\nCurr:\n"); 836d70a6e71SEugene Zelenko if (curr_ap) 8372cad65a5SGreg Clayton curr_ap->Dump(&s); 8382cad65a5SGreg Clayton else 8392cad65a5SGreg Clayton s.PutCString("NULL"); 8402cad65a5SGreg Clayton s.EOL(); 8412cad65a5SGreg Clayton #endif 8422cad65a5SGreg Clayton 843b9c1b51eSKate Stone if (!curr_ap || curr_ap->GetNumFrames(false) == 0) { 8442cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES) 8452cad65a5SGreg Clayton s.PutCString("No current frames, leave previous frames alone...\n"); 8462cad65a5SGreg Clayton #endif 8472cad65a5SGreg Clayton curr_ap.release(); 8482cad65a5SGreg Clayton return; 8492cad65a5SGreg Clayton } 8502cad65a5SGreg Clayton 851b9c1b51eSKate Stone if (!prev_sp || prev_sp->GetNumFrames(false) == 0) { 8522cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES) 8532cad65a5SGreg Clayton s.PutCString("No previous frames, so use current frames...\n"); 8542cad65a5SGreg Clayton #endif 85505097246SAdrian Prantl // We either don't have any previous frames, or since we have more than one 85605097246SAdrian Prantl // current frames it means we have all the frames and can safely replace 85705097246SAdrian Prantl // our previous frames. 8582cad65a5SGreg Clayton prev_sp.reset(curr_ap.release()); 8592cad65a5SGreg Clayton return; 8602cad65a5SGreg Clayton } 8612cad65a5SGreg Clayton 8622cad65a5SGreg Clayton const uint32_t num_curr_frames = curr_ap->GetNumFrames(false); 8632cad65a5SGreg Clayton 864b9c1b51eSKate Stone if (num_curr_frames > 1) { 8652cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES) 866b9c1b51eSKate Stone s.PutCString( 867b9c1b51eSKate Stone "We have more than one current frame, so use current frames...\n"); 8682cad65a5SGreg Clayton #endif 86905097246SAdrian Prantl // We have more than one current frames it means we have all the frames and 87005097246SAdrian Prantl // can safely replace our previous frames. 8712cad65a5SGreg Clayton prev_sp.reset(curr_ap.release()); 8722cad65a5SGreg Clayton 8732cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES) 8742cad65a5SGreg Clayton s.PutCString("\nMerged:\n"); 8752cad65a5SGreg Clayton prev_sp->Dump(&s); 8762cad65a5SGreg Clayton #endif 8772cad65a5SGreg Clayton return; 8782cad65a5SGreg Clayton } 8792cad65a5SGreg Clayton 880b57e4a1bSJason Molenda StackFrameSP prev_frame_zero_sp(prev_sp->GetFrameAtIndex(0)); 881b57e4a1bSJason Molenda StackFrameSP curr_frame_zero_sp(curr_ap->GetFrameAtIndex(0)); 8822cad65a5SGreg Clayton StackID curr_stack_id(curr_frame_zero_sp->GetStackID()); 8832cad65a5SGreg Clayton StackID prev_stack_id(prev_frame_zero_sp->GetStackID()); 8842cad65a5SGreg Clayton 8852cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES) 8862c595273SJohnny Chen const uint32_t num_prev_frames = prev_sp->GetNumFrames(false); 8872cad65a5SGreg Clayton s.Printf("\n%u previous frames with one current frame\n", num_prev_frames); 8882cad65a5SGreg Clayton #endif 8892cad65a5SGreg Clayton 8902cad65a5SGreg Clayton // We have only a single current frame 8912cad65a5SGreg Clayton // Our previous stack frames only had a single frame as well... 892b9c1b51eSKate Stone if (curr_stack_id == prev_stack_id) { 8932cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES) 894b9c1b51eSKate Stone s.Printf("\nPrevious frame #0 is same as current frame #0, merge the " 895b9c1b51eSKate Stone "cached data\n"); 8962cad65a5SGreg Clayton #endif 8972cad65a5SGreg Clayton 898b9c1b51eSKate Stone curr_frame_zero_sp->UpdateCurrentFrameFromPreviousFrame( 899b9c1b51eSKate Stone *prev_frame_zero_sp); 900b9c1b51eSKate Stone // prev_frame_zero_sp->UpdatePreviousFrameFromCurrentFrame 901b9c1b51eSKate Stone // (*curr_frame_zero_sp); 9022cad65a5SGreg Clayton // prev_sp->SetFrameAtIndex (0, prev_frame_zero_sp); 903b9c1b51eSKate Stone } else if (curr_stack_id < prev_stack_id) { 9042cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES) 905b9c1b51eSKate Stone s.Printf("\nCurrent frame #0 has a stack ID that is less than the previous " 906b9c1b51eSKate Stone "frame #0, insert current frame zero in front of previous\n"); 9072cad65a5SGreg Clayton #endif 9082cad65a5SGreg Clayton prev_sp->m_frames.insert(prev_sp->m_frames.begin(), curr_frame_zero_sp); 9092cad65a5SGreg Clayton } 9102cad65a5SGreg Clayton 9112cad65a5SGreg Clayton curr_ap.release(); 9122cad65a5SGreg Clayton 9132cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES) 9142cad65a5SGreg Clayton s.PutCString("\nMerged:\n"); 9152cad65a5SGreg Clayton prev_sp->Dump(&s); 9162cad65a5SGreg Clayton #endif 9172cad65a5SGreg Clayton } 918e4284b71SJim Ingham 919b57e4a1bSJason Molenda lldb::StackFrameSP 920b9c1b51eSKate Stone StackFrameList::GetStackFrameSPForStackFramePtr(StackFrame *stack_frame_ptr) { 921e4284b71SJim Ingham const_iterator pos; 922e4284b71SJim Ingham const_iterator begin = m_frames.begin(); 923e4284b71SJim Ingham const_iterator end = m_frames.end(); 924b57e4a1bSJason Molenda lldb::StackFrameSP ret_sp; 925e4284b71SJim Ingham 926b9c1b51eSKate Stone for (pos = begin; pos != end; ++pos) { 927b9c1b51eSKate Stone if (pos->get() == stack_frame_ptr) { 928e4284b71SJim Ingham ret_sp = (*pos); 929e4284b71SJim Ingham break; 930e4284b71SJim Ingham } 931e4284b71SJim Ingham } 932e4284b71SJim Ingham return ret_sp; 933e4284b71SJim Ingham } 934e4284b71SJim Ingham 935b9c1b51eSKate Stone size_t StackFrameList::GetStatus(Stream &strm, uint32_t first_frame, 936b9c1b51eSKate Stone uint32_t num_frames, bool show_frame_info, 9378ec10efcSJim Ingham uint32_t num_frames_with_source, 9387f1c1211SPavel Labath bool show_unique, 939b9c1b51eSKate Stone const char *selected_frame_marker) { 9407260f620SGreg Clayton size_t num_frames_displayed = 0; 9417260f620SGreg Clayton 9427260f620SGreg Clayton if (num_frames == 0) 9437260f620SGreg Clayton return 0; 9447260f620SGreg Clayton 945b57e4a1bSJason Molenda StackFrameSP frame_sp; 9467260f620SGreg Clayton uint32_t frame_idx = 0; 9477260f620SGreg Clayton uint32_t last_frame; 9487260f620SGreg Clayton 9497260f620SGreg Clayton // Don't let the last frame wrap around... 9507260f620SGreg Clayton if (num_frames == UINT32_MAX) 9517260f620SGreg Clayton last_frame = UINT32_MAX; 9527260f620SGreg Clayton else 9537260f620SGreg Clayton last_frame = first_frame + num_frames; 9547260f620SGreg Clayton 955b57e4a1bSJason Molenda StackFrameSP selected_frame_sp = m_thread.GetSelectedFrame(); 956d70a6e71SEugene Zelenko const char *unselected_marker = nullptr; 9578ec10efcSJim Ingham std::string buffer; 958b9c1b51eSKate Stone if (selected_frame_marker) { 9598ec10efcSJim Ingham size_t len = strlen(selected_frame_marker); 9608ec10efcSJim Ingham buffer.insert(buffer.begin(), len, ' '); 9618ec10efcSJim Ingham unselected_marker = buffer.c_str(); 9628ec10efcSJim Ingham } 963d70a6e71SEugene Zelenko const char *marker = nullptr; 9648ec10efcSJim Ingham 965b9c1b51eSKate Stone for (frame_idx = first_frame; frame_idx < last_frame; ++frame_idx) { 9667260f620SGreg Clayton frame_sp = GetFrameAtIndex(frame_idx); 967d70a6e71SEugene Zelenko if (!frame_sp) 9687260f620SGreg Clayton break; 9697260f620SGreg Clayton 970b9c1b51eSKate Stone if (selected_frame_marker != nullptr) { 9718ec10efcSJim Ingham if (frame_sp == selected_frame_sp) 9728ec10efcSJim Ingham marker = selected_frame_marker; 9738ec10efcSJim Ingham else 9748ec10efcSJim Ingham marker = unselected_marker; 9758ec10efcSJim Ingham } 9768ec10efcSJim Ingham 977b9c1b51eSKate Stone if (!frame_sp->GetStatus(strm, show_frame_info, 978b9c1b51eSKate Stone num_frames_with_source > (first_frame - frame_idx), 9797f1c1211SPavel Labath show_unique, marker)) 9807260f620SGreg Clayton break; 9817260f620SGreg Clayton ++num_frames_displayed; 9827260f620SGreg Clayton } 9837260f620SGreg Clayton 9847260f620SGreg Clayton strm.IndentLess(); 9857260f620SGreg Clayton return num_frames_displayed; 9867260f620SGreg Clayton } 987