130fdc8d8SChris Lattner //===-- StackFrameList.cpp --------------------------------------*- C++ -*-===// 230fdc8d8SChris Lattner // 330fdc8d8SChris Lattner // The LLVM Compiler Infrastructure 430fdc8d8SChris Lattner // 530fdc8d8SChris Lattner // This file is distributed under the University of Illinois Open Source 630fdc8d8SChris Lattner // License. See LICENSE.TXT for details. 730fdc8d8SChris Lattner // 830fdc8d8SChris Lattner //===----------------------------------------------------------------------===// 930fdc8d8SChris Lattner 1030fdc8d8SChris Lattner // C Includes 1130fdc8d8SChris Lattner // C++ Includes 1230fdc8d8SChris Lattner // Other libraries and framework includes 1330fdc8d8SChris Lattner // Project includes 14d70a6e71SEugene Zelenko #include "lldb/Target/StackFrameList.h" 15c635500dSJim Ingham #include "lldb/Breakpoint/Breakpoint.h" 16b9c1b51eSKate Stone #include "lldb/Breakpoint/BreakpointLocation.h" 17b7f6b2faSJim Ingham #include "lldb/Core/SourceManager.h" 18b9c1b51eSKate Stone #include "lldb/Core/StreamFile.h" 1912daf946SGreg Clayton #include "lldb/Symbol/Block.h" 2012daf946SGreg Clayton #include "lldb/Symbol/Function.h" 2159e8fc1cSGreg Clayton #include "lldb/Symbol/Symbol.h" 22b7f6b2faSJim Ingham #include "lldb/Target/Process.h" 2312daf946SGreg Clayton #include "lldb/Target/RegisterContext.h" 2430fdc8d8SChris Lattner #include "lldb/Target/StackFrame.h" 25513c6bb8SJim Ingham #include "lldb/Target/StopInfo.h" 26b7f6b2faSJim Ingham #include "lldb/Target/Target.h" 2712daf946SGreg Clayton #include "lldb/Target/Thread.h" 2812daf946SGreg Clayton #include "lldb/Target/Unwind.h" 296f9e6901SZachary Turner #include "lldb/Utility/Log.h" 30*4b36f791SVedant Kumar #include "llvm/ADT/SmallPtrSet.h" 3130fdc8d8SChris Lattner 325082c5fdSGreg Clayton //#define DEBUG_STACK_FRAMES 1 335082c5fdSGreg Clayton 3430fdc8d8SChris Lattner using namespace lldb; 3530fdc8d8SChris Lattner using namespace lldb_private; 3630fdc8d8SChris Lattner 3730fdc8d8SChris Lattner //---------------------------------------------------------------------- 3830fdc8d8SChris Lattner // StackFrameList constructor 3930fdc8d8SChris Lattner //---------------------------------------------------------------------- 40b9c1b51eSKate Stone StackFrameList::StackFrameList(Thread &thread, 41b9c1b51eSKate Stone const lldb::StackFrameListSP &prev_frames_sp, 42b9c1b51eSKate Stone bool show_inline_frames) 43b9c1b51eSKate Stone : m_thread(thread), m_prev_frames_sp(prev_frames_sp), m_mutex(), m_frames(), 44b9c1b51eSKate Stone m_selected_frame_idx(0), m_concrete_frames_fetched(0), 45513c6bb8SJim Ingham m_current_inlined_depth(UINT32_MAX), 46513c6bb8SJim Ingham m_current_inlined_pc(LLDB_INVALID_ADDRESS), 47b9c1b51eSKate Stone m_show_inlined_frames(show_inline_frames) { 48b9c1b51eSKate Stone if (prev_frames_sp) { 49513c6bb8SJim Ingham m_current_inlined_depth = prev_frames_sp->m_current_inlined_depth; 50513c6bb8SJim Ingham m_current_inlined_pc = prev_frames_sp->m_current_inlined_pc; 51513c6bb8SJim Ingham } 5230fdc8d8SChris Lattner } 5330fdc8d8SChris Lattner 54b9c1b51eSKate Stone StackFrameList::~StackFrameList() { 5505097246SAdrian Prantl // Call clear since this takes a lock and clears the stack frame list in case 5605097246SAdrian Prantl // another thread is currently using this stack frame list 57ca5ce187SGreg Clayton Clear(); 5830fdc8d8SChris Lattner } 5930fdc8d8SChris Lattner 60b9c1b51eSKate Stone void StackFrameList::CalculateCurrentInlinedDepth() { 61513c6bb8SJim Ingham uint32_t cur_inlined_depth = GetCurrentInlinedDepth(); 62b9c1b51eSKate Stone if (cur_inlined_depth == UINT32_MAX) { 63513c6bb8SJim Ingham ResetCurrentInlinedDepth(); 64513c6bb8SJim Ingham } 65513c6bb8SJim Ingham } 66513c6bb8SJim Ingham 67b9c1b51eSKate Stone uint32_t StackFrameList::GetCurrentInlinedDepth() { 68b9c1b51eSKate Stone if (m_show_inlined_frames && m_current_inlined_pc != LLDB_INVALID_ADDRESS) { 69513c6bb8SJim Ingham lldb::addr_t cur_pc = m_thread.GetRegisterContext()->GetPC(); 70b9c1b51eSKate Stone if (cur_pc != m_current_inlined_pc) { 71513c6bb8SJim Ingham m_current_inlined_pc = LLDB_INVALID_ADDRESS; 72513c6bb8SJim Ingham m_current_inlined_depth = UINT32_MAX; 735160ce5cSGreg Clayton Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); 746cd41da7SJim Ingham if (log && log->GetVerbose()) 75b9c1b51eSKate Stone log->Printf( 76b9c1b51eSKate Stone "GetCurrentInlinedDepth: invalidating current inlined depth.\n"); 77513c6bb8SJim Ingham } 78513c6bb8SJim Ingham return m_current_inlined_depth; 79b9c1b51eSKate Stone } else { 80513c6bb8SJim Ingham return UINT32_MAX; 81513c6bb8SJim Ingham } 82513c6bb8SJim Ingham } 83513c6bb8SJim Ingham 84b9c1b51eSKate Stone void StackFrameList::ResetCurrentInlinedDepth() { 85e7167e03SVedant Kumar if (!m_show_inlined_frames) 86e7167e03SVedant Kumar return; 87e7167e03SVedant Kumar 88bb19a13cSSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 8947db4a2cSKeno Fischer 90513c6bb8SJim Ingham GetFramesUpTo(0); 91d70a6e71SEugene Zelenko if (m_frames.empty()) 9265d4d5c3SRyan Brown return; 93b9c1b51eSKate Stone if (!m_frames[0]->IsInlined()) { 94513c6bb8SJim Ingham m_current_inlined_depth = UINT32_MAX; 95513c6bb8SJim Ingham m_current_inlined_pc = LLDB_INVALID_ADDRESS; 965160ce5cSGreg Clayton Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); 976cd41da7SJim Ingham if (log && log->GetVerbose()) 98b9c1b51eSKate Stone log->Printf( 99b9c1b51eSKate Stone "ResetCurrentInlinedDepth: Invalidating current inlined depth.\n"); 100e7167e03SVedant Kumar return; 101e7167e03SVedant Kumar } 102e7167e03SVedant Kumar 10305097246SAdrian Prantl // We only need to do something special about inlined blocks when we are 10405097246SAdrian Prantl // at the beginning of an inlined function: 105b9c1b51eSKate Stone // FIXME: We probably also have to do something special if the PC is at 106e7167e03SVedant Kumar // the END of an inlined function, which coincides with the end of either 107e7167e03SVedant Kumar // its containing function or another inlined function. 108513c6bb8SJim Ingham 109513c6bb8SJim Ingham Block *block_ptr = m_frames[0]->GetFrameBlock(); 110e7167e03SVedant Kumar if (!block_ptr) 111e7167e03SVedant Kumar return; 112e7167e03SVedant Kumar 113513c6bb8SJim Ingham Address pc_as_address; 114e7167e03SVedant Kumar lldb::addr_t curr_pc = m_thread.GetRegisterContext()->GetPC(); 115e7167e03SVedant Kumar pc_as_address.SetLoadAddress(curr_pc, &(m_thread.GetProcess()->GetTarget())); 116513c6bb8SJim Ingham AddressRange containing_range; 117e7167e03SVedant Kumar if (!block_ptr->GetRangeContainingAddress(pc_as_address, containing_range) || 118e7167e03SVedant Kumar pc_as_address != containing_range.GetBaseAddress()) 119e7167e03SVedant Kumar return; 120e7167e03SVedant Kumar 121e7167e03SVedant Kumar // If we got here because of a breakpoint hit, then set the inlined depth 122e7167e03SVedant Kumar // depending on where the breakpoint was set. If we got here because of a 123e7167e03SVedant Kumar // crash, then set the inlined depth to the deepest most block. Otherwise, 124e7167e03SVedant Kumar // we stopped here naturally as the result of a step, so set ourselves in the 125e7167e03SVedant Kumar // containing frame of the whole set of nested inlines, so the user can then 126e7167e03SVedant Kumar // "virtually" step into the frames one by one, or next over the whole mess. 127e7167e03SVedant Kumar // Note: We don't have to handle being somewhere in the middle of the stack 128e7167e03SVedant Kumar // here, since ResetCurrentInlinedDepth doesn't get called if there is a 129e7167e03SVedant Kumar // valid inlined depth set. 130513c6bb8SJim Ingham StopInfoSP stop_info_sp = m_thread.GetStopInfo(); 131e7167e03SVedant Kumar if (!stop_info_sp) 132e7167e03SVedant Kumar return; 133b9c1b51eSKate Stone switch (stop_info_sp->GetStopReason()) { 134513c6bb8SJim Ingham case eStopReasonWatchpoint: 135513c6bb8SJim Ingham case eStopReasonException: 13690ba8115SGreg Clayton case eStopReasonExec: 137513c6bb8SJim Ingham case eStopReasonSignal: 138e7167e03SVedant Kumar // In all these cases we want to stop in the deepest frame. 139513c6bb8SJim Ingham m_current_inlined_pc = curr_pc; 140513c6bb8SJim Ingham m_current_inlined_depth = 0; 141513c6bb8SJim Ingham break; 142b9c1b51eSKate Stone case eStopReasonBreakpoint: { 143e7167e03SVedant Kumar // FIXME: Figure out what this break point is doing, and set the inline 144e7167e03SVedant Kumar // depth appropriately. Be careful to take into account breakpoints that 145e7167e03SVedant Kumar // implement step over prologue, since that should do the default 146e7167e03SVedant Kumar // calculation. For now, if the breakpoints corresponding to this hit are 147e7167e03SVedant Kumar // all internal, I set the stop location to the top of the inlined stack, 148e7167e03SVedant Kumar // since that will make things like stepping over prologues work right. 149e7167e03SVedant Kumar // But if there are any non-internal breakpoints I do to the bottom of the 150e7167e03SVedant Kumar // stack, since that was the old behavior. 151c635500dSJim Ingham uint32_t bp_site_id = stop_info_sp->GetValue(); 152b9c1b51eSKate Stone BreakpointSiteSP bp_site_sp( 153e7167e03SVedant Kumar m_thread.GetProcess()->GetBreakpointSiteList().FindByID(bp_site_id)); 154c635500dSJim Ingham bool all_internal = true; 155b9c1b51eSKate Stone if (bp_site_sp) { 156c635500dSJim Ingham uint32_t num_owners = bp_site_sp->GetNumberOfOwners(); 157b9c1b51eSKate Stone for (uint32_t i = 0; i < num_owners; i++) { 158e7167e03SVedant Kumar Breakpoint &bp_ref = bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint(); 159b9c1b51eSKate Stone if (!bp_ref.IsInternal()) { 160c635500dSJim Ingham all_internal = false; 161c635500dSJim Ingham } 162c635500dSJim Ingham } 163c635500dSJim Ingham } 164b9c1b51eSKate Stone if (!all_internal) { 165c635500dSJim Ingham m_current_inlined_pc = curr_pc; 166c635500dSJim Ingham m_current_inlined_depth = 0; 167c635500dSJim Ingham break; 168c635500dSJim Ingham } 1697da851a3SJim Ingham } 170cec91ef9SGreg Clayton LLVM_FALLTHROUGH; 171b9c1b51eSKate Stone default: { 172e7167e03SVedant Kumar // Otherwise, we should set ourselves at the container of the inlining, so 173e7167e03SVedant Kumar // that the user can descend into them. So first we check whether we have 174e7167e03SVedant Kumar // more than one inlined block sharing this PC: 175513c6bb8SJim Ingham int num_inlined_functions = 0; 176513c6bb8SJim Ingham 177513c6bb8SJim Ingham for (Block *container_ptr = block_ptr->GetInlinedParent(); 178d70a6e71SEugene Zelenko container_ptr != nullptr; 179b9c1b51eSKate Stone container_ptr = container_ptr->GetInlinedParent()) { 180e7167e03SVedant Kumar if (!container_ptr->GetRangeContainingAddress(pc_as_address, 181e7167e03SVedant Kumar containing_range)) 182513c6bb8SJim Ingham break; 183513c6bb8SJim Ingham if (pc_as_address != containing_range.GetBaseAddress()) 184513c6bb8SJim Ingham break; 185513c6bb8SJim Ingham 186513c6bb8SJim Ingham num_inlined_functions++; 187513c6bb8SJim Ingham } 188513c6bb8SJim Ingham m_current_inlined_pc = curr_pc; 189513c6bb8SJim Ingham m_current_inlined_depth = num_inlined_functions + 1; 190e7167e03SVedant Kumar Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); 1916cd41da7SJim Ingham if (log && log->GetVerbose()) 192b9c1b51eSKate Stone log->Printf("ResetCurrentInlinedDepth: setting inlined " 193b9c1b51eSKate Stone "depth: %d 0x%" PRIx64 ".\n", 194b9c1b51eSKate Stone m_current_inlined_depth, curr_pc); 195513c6bb8SJim Ingham 196e7167e03SVedant Kumar break; 197513c6bb8SJim Ingham } 198513c6bb8SJim Ingham } 199513c6bb8SJim Ingham } 200513c6bb8SJim Ingham 201b9c1b51eSKate Stone bool StackFrameList::DecrementCurrentInlinedDepth() { 202b9c1b51eSKate Stone if (m_show_inlined_frames) { 203513c6bb8SJim Ingham uint32_t current_inlined_depth = GetCurrentInlinedDepth(); 204b9c1b51eSKate Stone if (current_inlined_depth != UINT32_MAX) { 205b9c1b51eSKate Stone if (current_inlined_depth > 0) { 206513c6bb8SJim Ingham m_current_inlined_depth--; 207513c6bb8SJim Ingham return true; 208513c6bb8SJim Ingham } 209513c6bb8SJim Ingham } 2109786eeebSJim Ingham } 211513c6bb8SJim Ingham return false; 212513c6bb8SJim Ingham } 213513c6bb8SJim Ingham 214b9c1b51eSKate Stone void StackFrameList::SetCurrentInlinedDepth(uint32_t new_depth) { 2156cd41da7SJim Ingham m_current_inlined_depth = new_depth; 2166cd41da7SJim Ingham if (new_depth == UINT32_MAX) 2176cd41da7SJim Ingham m_current_inlined_pc = LLDB_INVALID_ADDRESS; 2186cd41da7SJim Ingham else 2196cd41da7SJim Ingham m_current_inlined_pc = m_thread.GetRegisterContext()->GetPC(); 2206cd41da7SJim Ingham } 2216cd41da7SJim Ingham 222eb8fa58eSVedant Kumar void StackFrameList::GetOnlyConcreteFramesUpTo(uint32_t end_idx, 223eb8fa58eSVedant Kumar Unwind *unwinder) { 224eb8fa58eSVedant Kumar assert(m_thread.IsValid() && "Expected valid thread"); 225eb8fa58eSVedant Kumar assert(m_frames.size() <= end_idx && "Expected there to be frames to fill"); 226eb8fa58eSVedant Kumar 227eb8fa58eSVedant Kumar if (end_idx < m_concrete_frames_fetched) 228eb8fa58eSVedant Kumar return; 229eb8fa58eSVedant Kumar 230eb8fa58eSVedant Kumar if (!unwinder) 231eb8fa58eSVedant Kumar return; 232eb8fa58eSVedant Kumar 233eb8fa58eSVedant Kumar uint32_t num_frames = unwinder->GetFramesUpTo(end_idx); 234eb8fa58eSVedant Kumar if (num_frames <= end_idx + 1) { 235eb8fa58eSVedant Kumar // Done unwinding. 236eb8fa58eSVedant Kumar m_concrete_frames_fetched = UINT32_MAX; 237eb8fa58eSVedant Kumar } 23833e51b14SVedant Kumar 23933e51b14SVedant Kumar // Don't create the frames eagerly. Defer this work to GetFrameAtIndex, 24033e51b14SVedant Kumar // which can lazily query the unwinder to create frames. 241eb8fa58eSVedant Kumar m_frames.resize(num_frames); 242eb8fa58eSVedant Kumar } 243eb8fa58eSVedant Kumar 244*4b36f791SVedant Kumar /// Find the unique path through the call graph from \p begin (with return PC 245*4b36f791SVedant Kumar /// \p return_pc) to \p end. On success this path is stored into \p path, and 246*4b36f791SVedant Kumar /// on failure \p path is unchanged. 247*4b36f791SVedant Kumar static void FindInterveningFrames(Function &begin, Function &end, 248*4b36f791SVedant Kumar Target &target, addr_t return_pc, 249*4b36f791SVedant Kumar std::vector<Function *> &path, 250*4b36f791SVedant Kumar ModuleList &images, Log *log) { 251*4b36f791SVedant Kumar LLDB_LOG(log, "Finding frames between {0} and {1}, retn-pc={2:x}", 252*4b36f791SVedant Kumar begin.GetDisplayName(), end.GetDisplayName(), return_pc); 253*4b36f791SVedant Kumar 254*4b36f791SVedant Kumar // Find a non-tail calling edge with the correct return PC. 255*4b36f791SVedant Kumar auto first_level_edges = begin.GetCallEdges(); 256*4b36f791SVedant Kumar if (log) 257*4b36f791SVedant Kumar for (const CallEdge &edge : first_level_edges) 258*4b36f791SVedant Kumar LLDB_LOG(log, "FindInterveningFrames: found call with retn-PC = {0:x}", 259*4b36f791SVedant Kumar edge.GetReturnPCAddress(begin, target)); 260*4b36f791SVedant Kumar auto first_edge_it = std::lower_bound( 261*4b36f791SVedant Kumar first_level_edges.begin(), first_level_edges.end(), return_pc, 262*4b36f791SVedant Kumar [&](const CallEdge &edge, addr_t target_pc) { 263*4b36f791SVedant Kumar return edge.GetReturnPCAddress(begin, target) < target_pc; 264*4b36f791SVedant Kumar }); 265*4b36f791SVedant Kumar if (first_edge_it == first_level_edges.end() || 266*4b36f791SVedant Kumar first_edge_it->GetReturnPCAddress(begin, target) != return_pc) { 267*4b36f791SVedant Kumar LLDB_LOG(log, "No call edge outgoing from {0} with retn-PC == {1:x}", 268*4b36f791SVedant Kumar begin.GetDisplayName(), return_pc); 269*4b36f791SVedant Kumar return; 270*4b36f791SVedant Kumar } 271*4b36f791SVedant Kumar CallEdge &first_edge = const_cast<CallEdge &>(*first_edge_it); 272*4b36f791SVedant Kumar 273*4b36f791SVedant Kumar // The first callee may not be resolved, or there may be nothing to fill in. 274*4b36f791SVedant Kumar Function *first_callee = first_edge.GetCallee(images); 275*4b36f791SVedant Kumar if (!first_callee) { 276*4b36f791SVedant Kumar LLDB_LOG(log, "Could not resolve callee"); 277*4b36f791SVedant Kumar return; 278*4b36f791SVedant Kumar } 279*4b36f791SVedant Kumar if (first_callee == &end) { 280*4b36f791SVedant Kumar LLDB_LOG(log, "Not searching further, first callee is {0} (retn-PC: {1:x})", 281*4b36f791SVedant Kumar end.GetDisplayName(), return_pc); 282*4b36f791SVedant Kumar return; 283*4b36f791SVedant Kumar } 284*4b36f791SVedant Kumar 285*4b36f791SVedant Kumar // Run DFS on the tail-calling edges out of the first callee to find \p end. 286*4b36f791SVedant Kumar // Fully explore the set of functions reachable from the first edge via tail 287*4b36f791SVedant Kumar // calls in order to detect ambiguous executions. 288*4b36f791SVedant Kumar struct DFS { 289*4b36f791SVedant Kumar std::vector<Function *> active_path = {}; 290*4b36f791SVedant Kumar std::vector<Function *> solution_path = {}; 291*4b36f791SVedant Kumar llvm::SmallPtrSet<Function *, 2> visited_nodes = {}; 292*4b36f791SVedant Kumar bool ambiguous = false; 293*4b36f791SVedant Kumar Function *end; 294*4b36f791SVedant Kumar ModuleList &images; 295*4b36f791SVedant Kumar 296*4b36f791SVedant Kumar DFS(Function *end, ModuleList &images) : end(end), images(images) {} 297*4b36f791SVedant Kumar 298*4b36f791SVedant Kumar void search(Function *first_callee, std::vector<Function *> &path) { 299*4b36f791SVedant Kumar dfs(first_callee); 300*4b36f791SVedant Kumar if (!ambiguous) 301*4b36f791SVedant Kumar path = std::move(solution_path); 302*4b36f791SVedant Kumar } 303*4b36f791SVedant Kumar 304*4b36f791SVedant Kumar void dfs(Function *callee) { 305*4b36f791SVedant Kumar // Found a path to the target function. 306*4b36f791SVedant Kumar if (callee == end) { 307*4b36f791SVedant Kumar if (solution_path.empty()) 308*4b36f791SVedant Kumar solution_path = active_path; 309*4b36f791SVedant Kumar else 310*4b36f791SVedant Kumar ambiguous = true; 311*4b36f791SVedant Kumar return; 312*4b36f791SVedant Kumar } 313*4b36f791SVedant Kumar 314*4b36f791SVedant Kumar // Terminate the search if tail recursion is found, or more generally if 315*4b36f791SVedant Kumar // there's more than one way to reach a target. This errs on the side of 316*4b36f791SVedant Kumar // caution: it conservatively stops searching when some solutions are 317*4b36f791SVedant Kumar // still possible to save time in the average case. 318*4b36f791SVedant Kumar if (!visited_nodes.insert(callee).second) { 319*4b36f791SVedant Kumar ambiguous = true; 320*4b36f791SVedant Kumar return; 321*4b36f791SVedant Kumar } 322*4b36f791SVedant Kumar 323*4b36f791SVedant Kumar // Search the calls made from this callee. 324*4b36f791SVedant Kumar active_path.push_back(callee); 325*4b36f791SVedant Kumar for (CallEdge &edge : callee->GetTailCallingEdges()) { 326*4b36f791SVedant Kumar Function *next_callee = edge.GetCallee(images); 327*4b36f791SVedant Kumar if (!next_callee) 328*4b36f791SVedant Kumar continue; 329*4b36f791SVedant Kumar 330*4b36f791SVedant Kumar dfs(next_callee); 331*4b36f791SVedant Kumar if (ambiguous) 332*4b36f791SVedant Kumar return; 333*4b36f791SVedant Kumar } 334*4b36f791SVedant Kumar active_path.pop_back(); 335*4b36f791SVedant Kumar } 336*4b36f791SVedant Kumar }; 337*4b36f791SVedant Kumar 338*4b36f791SVedant Kumar DFS(&end, images).search(first_callee, path); 339*4b36f791SVedant Kumar } 340*4b36f791SVedant Kumar 341*4b36f791SVedant Kumar /// Given that \p next_frame will be appended to the frame list, synthesize 342*4b36f791SVedant Kumar /// tail call frames between the current end of the list and \p next_frame. 343*4b36f791SVedant Kumar /// If any frames are added, adjust the frame index of \p next_frame. 344*4b36f791SVedant Kumar /// 345*4b36f791SVedant Kumar /// -------------- 346*4b36f791SVedant Kumar /// | ... | <- Completed frames. 347*4b36f791SVedant Kumar /// -------------- 348*4b36f791SVedant Kumar /// | prev_frame | 349*4b36f791SVedant Kumar /// -------------- 350*4b36f791SVedant Kumar /// | ... | <- Artificial frames inserted here. 351*4b36f791SVedant Kumar /// -------------- 352*4b36f791SVedant Kumar /// | next_frame | 353*4b36f791SVedant Kumar /// -------------- 354*4b36f791SVedant Kumar /// | ... | <- Not-yet-visited frames. 355*4b36f791SVedant Kumar /// -------------- 356*4b36f791SVedant Kumar void StackFrameList::SynthesizeTailCallFrames(StackFrame &next_frame) { 357*4b36f791SVedant Kumar TargetSP target_sp = next_frame.CalculateTarget(); 358*4b36f791SVedant Kumar if (!target_sp) 359*4b36f791SVedant Kumar return; 360*4b36f791SVedant Kumar 361*4b36f791SVedant Kumar lldb::RegisterContextSP next_reg_ctx_sp = next_frame.GetRegisterContext(); 362*4b36f791SVedant Kumar if (!next_reg_ctx_sp) 363*4b36f791SVedant Kumar return; 364*4b36f791SVedant Kumar 365*4b36f791SVedant Kumar Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); 366*4b36f791SVedant Kumar 367*4b36f791SVedant Kumar assert(!m_frames.empty() && "Cannot synthesize frames in an empty stack"); 368*4b36f791SVedant Kumar StackFrame &prev_frame = *m_frames.back().get(); 369*4b36f791SVedant Kumar 370*4b36f791SVedant Kumar // Find the functions prev_frame and next_frame are stopped in. The function 371*4b36f791SVedant Kumar // objects are needed to search the lazy call graph for intervening frames. 372*4b36f791SVedant Kumar Function *prev_func = 373*4b36f791SVedant Kumar prev_frame.GetSymbolContext(eSymbolContextFunction).function; 374*4b36f791SVedant Kumar if (!prev_func) { 375*4b36f791SVedant Kumar LLDB_LOG(log, "SynthesizeTailCallFrames: can't find previous function"); 376*4b36f791SVedant Kumar return; 377*4b36f791SVedant Kumar } 378*4b36f791SVedant Kumar Function *next_func = 379*4b36f791SVedant Kumar next_frame.GetSymbolContext(eSymbolContextFunction).function; 380*4b36f791SVedant Kumar if (!next_func) { 381*4b36f791SVedant Kumar LLDB_LOG(log, "SynthesizeTailCallFrames: can't find next function"); 382*4b36f791SVedant Kumar return; 383*4b36f791SVedant Kumar } 384*4b36f791SVedant Kumar 385*4b36f791SVedant Kumar // Try to find the unique sequence of (tail) calls which led from next_frame 386*4b36f791SVedant Kumar // to prev_frame. 387*4b36f791SVedant Kumar std::vector<Function *> path; 388*4b36f791SVedant Kumar addr_t return_pc = next_reg_ctx_sp->GetPC(); 389*4b36f791SVedant Kumar Target &target = *target_sp.get(); 390*4b36f791SVedant Kumar ModuleList &images = next_frame.CalculateTarget()->GetImages(); 391*4b36f791SVedant Kumar FindInterveningFrames(*next_func, *prev_func, target, return_pc, path, images, 392*4b36f791SVedant Kumar log); 393*4b36f791SVedant Kumar 394*4b36f791SVedant Kumar // Push synthetic tail call frames. 395*4b36f791SVedant Kumar for (Function *callee : llvm::reverse(path)) { 396*4b36f791SVedant Kumar uint32_t frame_idx = m_frames.size(); 397*4b36f791SVedant Kumar uint32_t concrete_frame_idx = next_frame.GetConcreteFrameIndex(); 398*4b36f791SVedant Kumar addr_t cfa = LLDB_INVALID_ADDRESS; 399*4b36f791SVedant Kumar bool cfa_is_valid = false; 400*4b36f791SVedant Kumar addr_t pc = 401*4b36f791SVedant Kumar callee->GetAddressRange().GetBaseAddress().GetLoadAddress(&target); 402*4b36f791SVedant Kumar SymbolContext sc; 403*4b36f791SVedant Kumar callee->CalculateSymbolContext(&sc); 404*4b36f791SVedant Kumar auto synth_frame = std::make_shared<StackFrame>( 405*4b36f791SVedant Kumar m_thread.shared_from_this(), frame_idx, concrete_frame_idx, cfa, 406*4b36f791SVedant Kumar cfa_is_valid, pc, StackFrame::Kind::Artificial, &sc); 407*4b36f791SVedant Kumar m_frames.push_back(synth_frame); 408*4b36f791SVedant Kumar LLDB_LOG(log, "Pushed frame {0}", callee->GetDisplayName()); 409*4b36f791SVedant Kumar } 410*4b36f791SVedant Kumar 411*4b36f791SVedant Kumar // If any frames were created, adjust next_frame's index. 412*4b36f791SVedant Kumar if (!path.empty()) 413*4b36f791SVedant Kumar next_frame.SetFrameIndex(m_frames.size()); 414*4b36f791SVedant Kumar } 415*4b36f791SVedant Kumar 416b9c1b51eSKate Stone void StackFrameList::GetFramesUpTo(uint32_t end_idx) { 417eb8fa58eSVedant Kumar // Do not fetch frames for an invalid thread. 418d70a6e71SEugene Zelenko if (!m_thread.IsValid()) 419ebafd2f1SEnrico Granata return; 420ebafd2f1SEnrico Granata 421b9c1b51eSKate Stone // We've already gotten more frames than asked for, or we've already finished 422b9c1b51eSKate Stone // unwinding, return. 423b0c72a5fSJim Ingham if (m_frames.size() > end_idx || GetAllFramesFetched()) 424b0c72a5fSJim Ingham return; 42512daf946SGreg Clayton 426b0c72a5fSJim Ingham Unwind *unwinder = m_thread.GetUnwinder(); 427b0c72a5fSJim Ingham 428eb8fa58eSVedant Kumar if (!m_show_inlined_frames) { 429eb8fa58eSVedant Kumar GetOnlyConcreteFramesUpTo(end_idx, unwinder); 430eb8fa58eSVedant Kumar return; 431eb8fa58eSVedant Kumar } 432eb8fa58eSVedant Kumar 4335082c5fdSGreg Clayton #if defined(DEBUG_STACK_FRAMES) 4342c595273SJohnny Chen StreamFile s(stdout, false); 4355082c5fdSGreg Clayton #endif 43605097246SAdrian Prantl // If we are hiding some frames from the outside world, we need to add 43705097246SAdrian Prantl // those onto the total count of frames to fetch. However, we don't need 43805097246SAdrian Prantl // to do that if end_idx is 0 since in that case we always get the first 43905097246SAdrian Prantl // concrete frame and all the inlined frames below it... And of course, if 44005097246SAdrian Prantl // end_idx is UINT32_MAX that means get all, so just do that... 441513c6bb8SJim Ingham 442513c6bb8SJim Ingham uint32_t inlined_depth = 0; 443b9c1b51eSKate Stone if (end_idx > 0 && end_idx != UINT32_MAX) { 444513c6bb8SJim Ingham inlined_depth = GetCurrentInlinedDepth(); 445b9c1b51eSKate Stone if (inlined_depth != UINT32_MAX) { 446513c6bb8SJim Ingham if (end_idx > 0) 447513c6bb8SJim Ingham end_idx += inlined_depth; 448513c6bb8SJim Ingham } 449513c6bb8SJim Ingham } 45012daf946SGreg Clayton 451b57e4a1bSJason Molenda StackFrameSP unwind_frame_sp; 452b9c1b51eSKate Stone do { 453b0c72a5fSJim Ingham uint32_t idx = m_concrete_frames_fetched++; 4548012cadbSGreg Clayton lldb::addr_t pc = LLDB_INVALID_ADDRESS; 4558012cadbSGreg Clayton lldb::addr_t cfa = LLDB_INVALID_ADDRESS; 456b9c1b51eSKate Stone if (idx == 0) { 45705097246SAdrian Prantl // We might have already created frame zero, only create it if we need 458eb8fa58eSVedant Kumar // to. 459b9c1b51eSKate Stone if (m_frames.empty()) { 460b3ae8761SGreg Clayton RegisterContextSP reg_ctx_sp(m_thread.GetRegisterContext()); 461b3ae8761SGreg Clayton 462b9c1b51eSKate Stone if (reg_ctx_sp) { 463b9c1b51eSKate Stone const bool success = 464b9c1b51eSKate Stone unwinder && unwinder->GetFrameInfoAtIndex(idx, cfa, pc); 46505097246SAdrian Prantl // There shouldn't be any way not to get the frame info for frame 46605097246SAdrian Prantl // 0. But if the unwinder can't make one, lets make one by hand 467eb8fa58eSVedant Kumar // with the SP as the CFA and see if that gets any further. 468b9c1b51eSKate Stone if (!success) { 469b3ae8761SGreg Clayton cfa = reg_ctx_sp->GetSP(); 470b3ae8761SGreg Clayton pc = reg_ctx_sp->GetPC(); 471376c4854SJim Ingham } 472376c4854SJim Ingham 473d9e416c0SGreg Clayton unwind_frame_sp.reset(new StackFrame(m_thread.shared_from_this(), 474eb8fa58eSVedant Kumar m_frames.size(), idx, reg_ctx_sp, 475eb8fa58eSVedant Kumar cfa, pc, nullptr)); 4765082c5fdSGreg Clayton m_frames.push_back(unwind_frame_sp); 4775082c5fdSGreg Clayton } 478b9c1b51eSKate Stone } else { 4795082c5fdSGreg Clayton unwind_frame_sp = m_frames.front(); 480b57e4a1bSJason Molenda cfa = unwind_frame_sp->m_id.GetCallFrameAddress(); 4815082c5fdSGreg Clayton } 482b9c1b51eSKate Stone } else { 483b9c1b51eSKate Stone const bool success = 484b9c1b51eSKate Stone unwinder && unwinder->GetFrameInfoAtIndex(idx, cfa, pc); 485b9c1b51eSKate Stone if (!success) { 486b0c72a5fSJim Ingham // We've gotten to the end of the stack. 487b0c72a5fSJim Ingham SetAllFramesFetched(); 488b0c72a5fSJim Ingham break; 489b0c72a5fSJim Ingham } 49099618476SJason Molenda const bool cfa_is_valid = true; 491*4b36f791SVedant Kumar unwind_frame_sp.reset( 492*4b36f791SVedant Kumar new StackFrame(m_thread.shared_from_this(), m_frames.size(), idx, cfa, 493*4b36f791SVedant Kumar cfa_is_valid, pc, StackFrame::Kind::Regular, nullptr)); 494*4b36f791SVedant Kumar 495*4b36f791SVedant Kumar // Create synthetic tail call frames between the previous frame and the 496*4b36f791SVedant Kumar // newly-found frame. The new frame's index may change after this call, 497*4b36f791SVedant Kumar // although its concrete index will stay the same. 498*4b36f791SVedant Kumar SynthesizeTailCallFrames(*unwind_frame_sp.get()); 499*4b36f791SVedant Kumar 5005082c5fdSGreg Clayton m_frames.push_back(unwind_frame_sp); 50112daf946SGreg Clayton } 50212daf946SGreg Clayton 503ff1b5c42SEd Maste assert(unwind_frame_sp); 504b9c1b51eSKate Stone SymbolContext unwind_sc = unwind_frame_sp->GetSymbolContext( 505b9c1b51eSKate Stone eSymbolContextBlock | eSymbolContextFunction); 5061ed54f50SGreg Clayton Block *unwind_block = unwind_sc.block; 507b9c1b51eSKate Stone if (unwind_block) { 5085f4c61e2SGreg Clayton Address curr_frame_address(unwind_frame_sp->GetFrameCodeAddress()); 509911d5784STed Woodward TargetSP target_sp = m_thread.CalculateTarget(); 51005097246SAdrian Prantl // Be sure to adjust the frame address to match the address that was 51105097246SAdrian Prantl // used to lookup the symbol context above. If we are in the first 51205097246SAdrian Prantl // concrete frame, then we lookup using the current address, else we 51305097246SAdrian Prantl // decrement the address by one to get the correct location. 514b9c1b51eSKate Stone if (idx > 0) { 515b9c1b51eSKate Stone if (curr_frame_address.GetOffset() == 0) { 516b9c1b51eSKate Stone // If curr_frame_address points to the first address in a section 51705097246SAdrian Prantl // then after adjustment it will point to an other section. In that 51805097246SAdrian Prantl // case resolve the address again to the correct section plus 51905097246SAdrian Prantl // offset form. 520b9c1b51eSKate Stone addr_t load_addr = curr_frame_address.GetOpcodeLoadAddress( 52104803b3eSTatyana Krasnukha target_sp.get(), AddressClass::eCode); 522b9c1b51eSKate Stone curr_frame_address.SetOpcodeLoadAddress( 52304803b3eSTatyana Krasnukha load_addr - 1, target_sp.get(), AddressClass::eCode); 524b9c1b51eSKate Stone } else { 5255f4c61e2SGreg Clayton curr_frame_address.Slide(-1); 5260f8452baSTamas Berghammer } 5270f8452baSTamas Berghammer } 5285f4c61e2SGreg Clayton 5291ed54f50SGreg Clayton SymbolContext next_frame_sc; 5301ed54f50SGreg Clayton Address next_frame_address; 5311ed54f50SGreg Clayton 532b9c1b51eSKate Stone while (unwind_sc.GetParentOfInlinedScope( 533b9c1b51eSKate Stone curr_frame_address, next_frame_sc, next_frame_address)) { 534911d5784STed Woodward next_frame_sc.line_entry.ApplyFileMappings(target_sp); 535b9c1b51eSKate Stone StackFrameSP frame_sp( 536b9c1b51eSKate Stone new StackFrame(m_thread.shared_from_this(), m_frames.size(), idx, 537b9c1b51eSKate Stone unwind_frame_sp->GetRegisterContextSP(), cfa, 538b9c1b51eSKate Stone next_frame_address, &next_frame_sc)); 5395082c5fdSGreg Clayton 5405082c5fdSGreg Clayton m_frames.push_back(frame_sp); 5411ed54f50SGreg Clayton unwind_sc = next_frame_sc; 5421ed54f50SGreg Clayton curr_frame_address = next_frame_address; 543b0c72a5fSJim Ingham } 544b0c72a5fSJim Ingham } 545b0c72a5fSJim Ingham } while (m_frames.size() - 1 < end_idx); 5461ed54f50SGreg Clayton 547b0c72a5fSJim Ingham // Don't try to merge till you've calculated all the frames in this stack. 548b9c1b51eSKate Stone if (GetAllFramesFetched() && m_prev_frames_sp) { 5492cad65a5SGreg Clayton StackFrameList *prev_frames = m_prev_frames_sp.get(); 5505082c5fdSGreg Clayton StackFrameList *curr_frames = this; 5515082c5fdSGreg Clayton 5525082c5fdSGreg Clayton #if defined(DEBUG_STACK_FRAMES) 55368275d5eSGreg Clayton s.PutCString("\nprev_frames:\n"); 5545082c5fdSGreg Clayton prev_frames->Dump(&s); 55568275d5eSGreg Clayton s.PutCString("\ncurr_frames:\n"); 5565082c5fdSGreg Clayton curr_frames->Dump(&s); 5575082c5fdSGreg Clayton s.EOL(); 5585082c5fdSGreg Clayton #endif 5595082c5fdSGreg Clayton size_t curr_frame_num, prev_frame_num; 5605082c5fdSGreg Clayton 561b9c1b51eSKate Stone for (curr_frame_num = curr_frames->m_frames.size(), 562b9c1b51eSKate Stone prev_frame_num = prev_frames->m_frames.size(); 5635082c5fdSGreg Clayton curr_frame_num > 0 && prev_frame_num > 0; 564b9c1b51eSKate Stone --curr_frame_num, --prev_frame_num) { 5655082c5fdSGreg Clayton const size_t curr_frame_idx = curr_frame_num - 1; 5665082c5fdSGreg Clayton const size_t prev_frame_idx = prev_frame_num - 1; 567b57e4a1bSJason Molenda StackFrameSP curr_frame_sp(curr_frames->m_frames[curr_frame_idx]); 568b57e4a1bSJason Molenda StackFrameSP prev_frame_sp(prev_frames->m_frames[prev_frame_idx]); 5695082c5fdSGreg Clayton 5705082c5fdSGreg Clayton #if defined(DEBUG_STACK_FRAMES) 5712cad65a5SGreg Clayton s.Printf("\n\nCurr frame #%u ", curr_frame_idx); 5725082c5fdSGreg Clayton if (curr_frame_sp) 5732cad65a5SGreg Clayton curr_frame_sp->Dump(&s, true, false); 5745082c5fdSGreg Clayton else 5755082c5fdSGreg Clayton s.PutCString("NULL"); 5762cad65a5SGreg Clayton s.Printf("\nPrev frame #%u ", prev_frame_idx); 5775082c5fdSGreg Clayton if (prev_frame_sp) 5782cad65a5SGreg Clayton prev_frame_sp->Dump(&s, true, false); 5795082c5fdSGreg Clayton else 5805082c5fdSGreg Clayton s.PutCString("NULL"); 5815082c5fdSGreg Clayton #endif 5825082c5fdSGreg Clayton 583b57e4a1bSJason Molenda StackFrame *curr_frame = curr_frame_sp.get(); 584b57e4a1bSJason Molenda StackFrame *prev_frame = prev_frame_sp.get(); 5855082c5fdSGreg Clayton 586d70a6e71SEugene Zelenko if (curr_frame == nullptr || prev_frame == nullptr) 5875082c5fdSGreg Clayton break; 5885082c5fdSGreg Clayton 589eb8fa58eSVedant Kumar // Check the stack ID to make sure they are equal. 59059e8fc1cSGreg Clayton if (curr_frame->GetStackID() != prev_frame->GetStackID()) 5915082c5fdSGreg Clayton break; 5925082c5fdSGreg Clayton 59359e8fc1cSGreg Clayton prev_frame->UpdatePreviousFrameFromCurrentFrame(*curr_frame); 59405097246SAdrian Prantl // Now copy the fixed up previous frame into the current frames so the 595eb8fa58eSVedant Kumar // pointer doesn't change. 59659e8fc1cSGreg Clayton m_frames[curr_frame_idx] = prev_frame_sp; 5975082c5fdSGreg Clayton 5985082c5fdSGreg Clayton #if defined(DEBUG_STACK_FRAMES) 59968275d5eSGreg Clayton s.Printf("\n Copying previous frame to current frame"); 6005082c5fdSGreg Clayton #endif 6015082c5fdSGreg Clayton } 602eb8fa58eSVedant Kumar // We are done with the old stack frame list, we can release it now. 6032cad65a5SGreg Clayton m_prev_frames_sp.reset(); 6045082c5fdSGreg Clayton } 60568275d5eSGreg Clayton 60668275d5eSGreg Clayton #if defined(DEBUG_STACK_FRAMES) 60768275d5eSGreg Clayton s.PutCString("\n\nNew frames:\n"); 60868275d5eSGreg Clayton Dump(&s); 60968275d5eSGreg Clayton s.EOL(); 61068275d5eSGreg Clayton #endif 611ec6829eaSGreg Clayton } 612b0c72a5fSJim Ingham 613b9c1b51eSKate Stone uint32_t StackFrameList::GetNumFrames(bool can_create) { 614bb19a13cSSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 615b0c72a5fSJim Ingham 616b0c72a5fSJim Ingham if (can_create) 617b0c72a5fSJim Ingham GetFramesUpTo(UINT32_MAX); 618513c6bb8SJim Ingham 619b3b7b1bfSVedant Kumar return GetVisibleStackFrameIndex(m_frames.size()); 62030fdc8d8SChris Lattner } 62130fdc8d8SChris Lattner 622b9c1b51eSKate Stone void StackFrameList::Dump(Stream *s) { 623d70a6e71SEugene Zelenko if (s == nullptr) 6245082c5fdSGreg Clayton return; 625bb19a13cSSaleem Abdulrasool 626bb19a13cSSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 62730fdc8d8SChris Lattner 6285082c5fdSGreg Clayton const_iterator pos, begin = m_frames.begin(), end = m_frames.end(); 629b9c1b51eSKate Stone for (pos = begin; pos != end; ++pos) { 630b57e4a1bSJason Molenda StackFrame *frame = (*pos).get(); 631324a1036SSaleem Abdulrasool s->Printf("%p: ", static_cast<void *>(frame)); 632b9c1b51eSKate Stone if (frame) { 63359e8fc1cSGreg Clayton frame->GetStackID().Dump(s); 6340603aa9dSGreg Clayton frame->DumpUsingSettingsFormat(s); 635b9c1b51eSKate Stone } else 636dce502edSGreg Clayton s->Printf("frame #%u", (uint32_t)std::distance(begin, pos)); 6375082c5fdSGreg Clayton s->EOL(); 63812daf946SGreg Clayton } 6395082c5fdSGreg Clayton s->EOL(); 6405082c5fdSGreg Clayton } 64112daf946SGreg Clayton 642b9c1b51eSKate Stone StackFrameSP StackFrameList::GetFrameAtIndex(uint32_t idx) { 643b57e4a1bSJason Molenda StackFrameSP frame_sp; 644bb19a13cSSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 6455cb9a184SJim Ingham uint32_t original_idx = idx; 6465cb9a184SJim Ingham 647513c6bb8SJim Ingham uint32_t inlined_depth = GetCurrentInlinedDepth(); 648513c6bb8SJim Ingham if (inlined_depth != UINT32_MAX) 649513c6bb8SJim Ingham idx += inlined_depth; 650513c6bb8SJim Ingham 6515082c5fdSGreg Clayton if (idx < m_frames.size()) 6525082c5fdSGreg Clayton frame_sp = m_frames[idx]; 65312daf946SGreg Clayton 6545082c5fdSGreg Clayton if (frame_sp) 65512daf946SGreg Clayton return frame_sp; 65612daf946SGreg Clayton 65705097246SAdrian Prantl // GetFramesUpTo will fill m_frames with as many frames as you asked for, if 65805097246SAdrian Prantl // there are that many. If there weren't then you asked for too many frames. 659b0c72a5fSJim Ingham GetFramesUpTo(idx); 660b9c1b51eSKate Stone if (idx < m_frames.size()) { 661b9c1b51eSKate Stone if (m_show_inlined_frames) { 662b9c1b51eSKate Stone // When inline frames are enabled we actually create all the frames in 663b9c1b51eSKate Stone // GetFramesUpTo. 6645082c5fdSGreg Clayton frame_sp = m_frames[idx]; 665b9c1b51eSKate Stone } else { 66612daf946SGreg Clayton Unwind *unwinder = m_thread.GetUnwinder(); 667b9c1b51eSKate Stone if (unwinder) { 66812daf946SGreg Clayton addr_t pc, cfa; 669b9c1b51eSKate Stone if (unwinder->GetFrameInfoAtIndex(idx, cfa, pc)) { 67099618476SJason Molenda const bool cfa_is_valid = true; 671*4b36f791SVedant Kumar frame_sp.reset(new StackFrame(m_thread.shared_from_this(), idx, idx, 672*4b36f791SVedant Kumar cfa, cfa_is_valid, pc, 673*4b36f791SVedant Kumar StackFrame::Kind::Regular, nullptr)); 67459e8fc1cSGreg Clayton 675b9c1b51eSKate Stone Function *function = 676b9c1b51eSKate Stone frame_sp->GetSymbolContext(eSymbolContextFunction).function; 677b9c1b51eSKate Stone if (function) { 67805097246SAdrian Prantl // When we aren't showing inline functions we always use the top 67905097246SAdrian Prantl // most function block as the scope. 68059e8fc1cSGreg Clayton frame_sp->SetSymbolContextScope(&function->GetBlock(false)); 681b9c1b51eSKate Stone } else { 682b9c1b51eSKate Stone // Set the symbol scope from the symbol regardless if it is nullptr 683b9c1b51eSKate Stone // or valid. 684b9c1b51eSKate Stone frame_sp->SetSymbolContextScope( 685b9c1b51eSKate Stone frame_sp->GetSymbolContext(eSymbolContextSymbol).symbol); 68659e8fc1cSGreg Clayton } 6875082c5fdSGreg Clayton SetFrameAtIndex(idx, frame_sp); 68812daf946SGreg Clayton } 68912daf946SGreg Clayton } 69012daf946SGreg Clayton } 691b9c1b51eSKate Stone } else if (original_idx == 0) { 692b9c1b51eSKate Stone // There should ALWAYS be a frame at index 0. If something went wrong with 69305097246SAdrian Prantl // the CurrentInlinedDepth such that there weren't as many frames as we 69405097246SAdrian Prantl // thought taking that into account, then reset the current inlined depth 6955cb9a184SJim Ingham // and return the real zeroth frame. 696b9c1b51eSKate Stone if (m_frames.empty()) { 697b9c1b51eSKate Stone // Why do we have a thread with zero frames, that should not ever 698b9c1b51eSKate Stone // happen... 699a322f36cSDavid Blaikie assert(!m_thread.IsValid() && "A valid thread has no frames."); 700b9c1b51eSKate Stone } else { 701d70a6e71SEugene Zelenko ResetCurrentInlinedDepth(); 702d70a6e71SEugene Zelenko frame_sp = m_frames[original_idx]; 7035cb9a184SJim Ingham } 7045cb9a184SJim Ingham } 7055cb9a184SJim Ingham 70630fdc8d8SChris Lattner return frame_sp; 70730fdc8d8SChris Lattner } 70830fdc8d8SChris Lattner 709b57e4a1bSJason Molenda StackFrameSP 710b9c1b51eSKate Stone StackFrameList::GetFrameWithConcreteFrameIndex(uint32_t unwind_idx) { 7115ccbd294SGreg Clayton // First try assuming the unwind index is the same as the frame index. The 71205097246SAdrian Prantl // unwind index is always greater than or equal to the frame index, so it is 71305097246SAdrian Prantl // a good place to start. If we have inlined frames we might have 5 concrete 71405097246SAdrian Prantl // frames (frame unwind indexes go from 0-4), but we might have 15 frames 71505097246SAdrian Prantl // after we make all the inlined frames. Most of the time the unwind frame 71605097246SAdrian Prantl // index (or the concrete frame index) is the same as the frame index. 7175ccbd294SGreg Clayton uint32_t frame_idx = unwind_idx; 718b57e4a1bSJason Molenda StackFrameSP frame_sp(GetFrameAtIndex(frame_idx)); 719b9c1b51eSKate Stone while (frame_sp) { 7205ccbd294SGreg Clayton if (frame_sp->GetFrameIndex() == unwind_idx) 7215ccbd294SGreg Clayton break; 7225ccbd294SGreg Clayton frame_sp = GetFrameAtIndex(++frame_idx); 7235ccbd294SGreg Clayton } 7245ccbd294SGreg Clayton return frame_sp; 7255ccbd294SGreg Clayton } 7265ccbd294SGreg Clayton 727b9c1b51eSKate Stone static bool CompareStackID(const StackFrameSP &stack_sp, 728b9c1b51eSKate Stone const StackID &stack_id) { 7297bcb93d5SGreg Clayton return stack_sp->GetStackID() < stack_id; 7307bcb93d5SGreg Clayton } 7317bcb93d5SGreg Clayton 732b9c1b51eSKate Stone StackFrameSP StackFrameList::GetFrameWithStackID(const StackID &stack_id) { 733b57e4a1bSJason Molenda StackFrameSP frame_sp; 7347bcb93d5SGreg Clayton 735b9c1b51eSKate Stone if (stack_id.IsValid()) { 736bb19a13cSSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 7377bcb93d5SGreg Clayton uint32_t frame_idx = 0; 7387bcb93d5SGreg Clayton // Do a binary search in case the stack frame is already in our cache 7397bcb93d5SGreg Clayton collection::const_iterator begin = m_frames.begin(); 7407bcb93d5SGreg Clayton collection::const_iterator end = m_frames.end(); 741b9c1b51eSKate Stone if (begin != end) { 742b9c1b51eSKate Stone collection::const_iterator pos = 743b9c1b51eSKate Stone std::lower_bound(begin, end, stack_id, CompareStackID); 744b9c1b51eSKate Stone if (pos != end) { 7458012cadbSGreg Clayton if ((*pos)->GetStackID() == stack_id) 7467bcb93d5SGreg Clayton return *pos; 7478012cadbSGreg Clayton } 7487bcb93d5SGreg Clayton } 749b9c1b51eSKate Stone do { 7503a195b7eSJim Ingham frame_sp = GetFrameAtIndex(frame_idx); 7513a195b7eSJim Ingham if (frame_sp && frame_sp->GetStackID() == stack_id) 7523a195b7eSJim Ingham break; 7533a195b7eSJim Ingham frame_idx++; 754b9c1b51eSKate Stone } while (frame_sp); 7557bcb93d5SGreg Clayton } 7563a195b7eSJim Ingham return frame_sp; 7573a195b7eSJim Ingham } 7585ccbd294SGreg Clayton 759b9c1b51eSKate Stone bool StackFrameList::SetFrameAtIndex(uint32_t idx, StackFrameSP &frame_sp) { 7605082c5fdSGreg Clayton if (idx >= m_frames.size()) 7615082c5fdSGreg Clayton m_frames.resize(idx + 1); 76212daf946SGreg Clayton // Make sure allocation succeeded by checking bounds again 763b9c1b51eSKate Stone if (idx < m_frames.size()) { 7645082c5fdSGreg Clayton m_frames[idx] = frame_sp; 76530fdc8d8SChris Lattner return true; 76630fdc8d8SChris Lattner } 76730fdc8d8SChris Lattner return false; // resize failed, out of memory? 76830fdc8d8SChris Lattner } 76930fdc8d8SChris Lattner 770b9c1b51eSKate Stone uint32_t StackFrameList::GetSelectedFrameIndex() const { 771bb19a13cSSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 7722976d00aSJim Ingham return m_selected_frame_idx; 77330fdc8d8SChris Lattner } 77430fdc8d8SChris Lattner 775b9c1b51eSKate Stone uint32_t StackFrameList::SetSelectedFrame(lldb_private::StackFrame *frame) { 776bb19a13cSSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 77712daf946SGreg Clayton const_iterator pos; 7785082c5fdSGreg Clayton const_iterator begin = m_frames.begin(); 7795082c5fdSGreg Clayton const_iterator end = m_frames.end(); 780b7f6b2faSJim Ingham m_selected_frame_idx = 0; 781b9c1b51eSKate Stone for (pos = begin; pos != end; ++pos) { 782b9c1b51eSKate Stone if (pos->get() == frame) { 7832976d00aSJim Ingham m_selected_frame_idx = std::distance(begin, pos); 784513c6bb8SJim Ingham uint32_t inlined_depth = GetCurrentInlinedDepth(); 785513c6bb8SJim Ingham if (inlined_depth != UINT32_MAX) 786513c6bb8SJim Ingham m_selected_frame_idx -= inlined_depth; 787b7f6b2faSJim Ingham break; 78830fdc8d8SChris Lattner } 78930fdc8d8SChris Lattner } 790b7f6b2faSJim Ingham SetDefaultFileAndLineToSelectedFrame(); 7912976d00aSJim Ingham return m_selected_frame_idx; 79230fdc8d8SChris Lattner } 79330fdc8d8SChris Lattner 794b9c1b51eSKate Stone bool StackFrameList::SetSelectedFrameByIndex(uint32_t idx) { 795bb19a13cSSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 796b57e4a1bSJason Molenda StackFrameSP frame_sp(GetFrameAtIndex(idx)); 797b9c1b51eSKate Stone if (frame_sp) { 798b0c72a5fSJim Ingham SetSelectedFrame(frame_sp.get()); 799b0c72a5fSJim Ingham return true; 800b9c1b51eSKate Stone } else 801b0c72a5fSJim Ingham return false; 802b7f6b2faSJim Ingham } 803b7f6b2faSJim Ingham 804b9c1b51eSKate Stone void StackFrameList::SetDefaultFileAndLineToSelectedFrame() { 805b9c1b51eSKate Stone if (m_thread.GetID() == 806b9c1b51eSKate Stone m_thread.GetProcess()->GetThreadList().GetSelectedThread()->GetID()) { 807b57e4a1bSJason Molenda StackFrameSP frame_sp(GetFrameAtIndex(GetSelectedFrameIndex())); 808b9c1b51eSKate Stone if (frame_sp) { 809252d0edeSGreg Clayton SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextLineEntry); 810b7f6b2faSJim Ingham if (sc.line_entry.file) 811b9c1b51eSKate Stone m_thread.CalculateTarget()->GetSourceManager().SetDefaultFileAndLine( 812b9c1b51eSKate Stone sc.line_entry.file, sc.line_entry.line); 813b7f6b2faSJim Ingham } 814b7f6b2faSJim Ingham } 81530fdc8d8SChris Lattner } 81630fdc8d8SChris Lattner 81730fdc8d8SChris Lattner // The thread has been run, reset the number stack frames to zero so we can 81830fdc8d8SChris Lattner // determine how many frames we have lazily. 819b9c1b51eSKate Stone void StackFrameList::Clear() { 820bb19a13cSSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_mutex); 8215082c5fdSGreg Clayton m_frames.clear(); 822b0c72a5fSJim Ingham m_concrete_frames_fetched = 0; 82330fdc8d8SChris Lattner } 82430fdc8d8SChris Lattner 825b9c1b51eSKate Stone void StackFrameList::Merge(std::unique_ptr<StackFrameList> &curr_ap, 826b9c1b51eSKate Stone lldb::StackFrameListSP &prev_sp) { 827bb19a13cSSaleem Abdulrasool std::unique_lock<std::recursive_mutex> current_lock, previous_lock; 828bb19a13cSSaleem Abdulrasool if (curr_ap) 829bb19a13cSSaleem Abdulrasool current_lock = std::unique_lock<std::recursive_mutex>(curr_ap->m_mutex); 830bb19a13cSSaleem Abdulrasool if (prev_sp) 831bb19a13cSSaleem Abdulrasool previous_lock = std::unique_lock<std::recursive_mutex>(prev_sp->m_mutex); 8322cad65a5SGreg Clayton 8332cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES) 8342c595273SJohnny Chen StreamFile s(stdout, false); 8352cad65a5SGreg Clayton s.PutCString("\n\nStackFrameList::Merge():\nPrev:\n"); 836d70a6e71SEugene Zelenko if (prev_sp) 8372cad65a5SGreg Clayton prev_sp->Dump(&s); 8382cad65a5SGreg Clayton else 8392cad65a5SGreg Clayton s.PutCString("NULL"); 8402cad65a5SGreg Clayton s.PutCString("\nCurr:\n"); 841d70a6e71SEugene Zelenko if (curr_ap) 8422cad65a5SGreg Clayton curr_ap->Dump(&s); 8432cad65a5SGreg Clayton else 8442cad65a5SGreg Clayton s.PutCString("NULL"); 8452cad65a5SGreg Clayton s.EOL(); 8462cad65a5SGreg Clayton #endif 8472cad65a5SGreg Clayton 848b9c1b51eSKate Stone if (!curr_ap || curr_ap->GetNumFrames(false) == 0) { 8492cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES) 8502cad65a5SGreg Clayton s.PutCString("No current frames, leave previous frames alone...\n"); 8512cad65a5SGreg Clayton #endif 8522cad65a5SGreg Clayton curr_ap.release(); 8532cad65a5SGreg Clayton return; 8542cad65a5SGreg Clayton } 8552cad65a5SGreg Clayton 856b9c1b51eSKate Stone if (!prev_sp || prev_sp->GetNumFrames(false) == 0) { 8572cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES) 8582cad65a5SGreg Clayton s.PutCString("No previous frames, so use current frames...\n"); 8592cad65a5SGreg Clayton #endif 86005097246SAdrian Prantl // We either don't have any previous frames, or since we have more than one 86105097246SAdrian Prantl // current frames it means we have all the frames and can safely replace 86205097246SAdrian Prantl // our previous frames. 8632cad65a5SGreg Clayton prev_sp.reset(curr_ap.release()); 8642cad65a5SGreg Clayton return; 8652cad65a5SGreg Clayton } 8662cad65a5SGreg Clayton 8672cad65a5SGreg Clayton const uint32_t num_curr_frames = curr_ap->GetNumFrames(false); 8682cad65a5SGreg Clayton 869b9c1b51eSKate Stone if (num_curr_frames > 1) { 8702cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES) 871b9c1b51eSKate Stone s.PutCString( 872b9c1b51eSKate Stone "We have more than one current frame, so use current frames...\n"); 8732cad65a5SGreg Clayton #endif 87405097246SAdrian Prantl // We have more than one current frames it means we have all the frames and 87505097246SAdrian Prantl // can safely replace our previous frames. 8762cad65a5SGreg Clayton prev_sp.reset(curr_ap.release()); 8772cad65a5SGreg Clayton 8782cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES) 8792cad65a5SGreg Clayton s.PutCString("\nMerged:\n"); 8802cad65a5SGreg Clayton prev_sp->Dump(&s); 8812cad65a5SGreg Clayton #endif 8822cad65a5SGreg Clayton return; 8832cad65a5SGreg Clayton } 8842cad65a5SGreg Clayton 885b57e4a1bSJason Molenda StackFrameSP prev_frame_zero_sp(prev_sp->GetFrameAtIndex(0)); 886b57e4a1bSJason Molenda StackFrameSP curr_frame_zero_sp(curr_ap->GetFrameAtIndex(0)); 8872cad65a5SGreg Clayton StackID curr_stack_id(curr_frame_zero_sp->GetStackID()); 8882cad65a5SGreg Clayton StackID prev_stack_id(prev_frame_zero_sp->GetStackID()); 8892cad65a5SGreg Clayton 8902cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES) 8912c595273SJohnny Chen const uint32_t num_prev_frames = prev_sp->GetNumFrames(false); 8922cad65a5SGreg Clayton s.Printf("\n%u previous frames with one current frame\n", num_prev_frames); 8932cad65a5SGreg Clayton #endif 8942cad65a5SGreg Clayton 8952cad65a5SGreg Clayton // We have only a single current frame 8962cad65a5SGreg Clayton // Our previous stack frames only had a single frame as well... 897b9c1b51eSKate Stone if (curr_stack_id == prev_stack_id) { 8982cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES) 899b9c1b51eSKate Stone s.Printf("\nPrevious frame #0 is same as current frame #0, merge the " 900b9c1b51eSKate Stone "cached data\n"); 9012cad65a5SGreg Clayton #endif 9022cad65a5SGreg Clayton 903b9c1b51eSKate Stone curr_frame_zero_sp->UpdateCurrentFrameFromPreviousFrame( 904b9c1b51eSKate Stone *prev_frame_zero_sp); 905b9c1b51eSKate Stone // prev_frame_zero_sp->UpdatePreviousFrameFromCurrentFrame 906b9c1b51eSKate Stone // (*curr_frame_zero_sp); 9072cad65a5SGreg Clayton // prev_sp->SetFrameAtIndex (0, prev_frame_zero_sp); 908b9c1b51eSKate Stone } else if (curr_stack_id < prev_stack_id) { 9092cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES) 910b9c1b51eSKate Stone s.Printf("\nCurrent frame #0 has a stack ID that is less than the previous " 911b9c1b51eSKate Stone "frame #0, insert current frame zero in front of previous\n"); 9122cad65a5SGreg Clayton #endif 9132cad65a5SGreg Clayton prev_sp->m_frames.insert(prev_sp->m_frames.begin(), curr_frame_zero_sp); 9142cad65a5SGreg Clayton } 9152cad65a5SGreg Clayton 9162cad65a5SGreg Clayton curr_ap.release(); 9172cad65a5SGreg Clayton 9182cad65a5SGreg Clayton #if defined(DEBUG_STACK_FRAMES) 9192cad65a5SGreg Clayton s.PutCString("\nMerged:\n"); 9202cad65a5SGreg Clayton prev_sp->Dump(&s); 9212cad65a5SGreg Clayton #endif 9222cad65a5SGreg Clayton } 923e4284b71SJim Ingham 924b57e4a1bSJason Molenda lldb::StackFrameSP 925b9c1b51eSKate Stone StackFrameList::GetStackFrameSPForStackFramePtr(StackFrame *stack_frame_ptr) { 926e4284b71SJim Ingham const_iterator pos; 927e4284b71SJim Ingham const_iterator begin = m_frames.begin(); 928e4284b71SJim Ingham const_iterator end = m_frames.end(); 929b57e4a1bSJason Molenda lldb::StackFrameSP ret_sp; 930e4284b71SJim Ingham 931b9c1b51eSKate Stone for (pos = begin; pos != end; ++pos) { 932b9c1b51eSKate Stone if (pos->get() == stack_frame_ptr) { 933e4284b71SJim Ingham ret_sp = (*pos); 934e4284b71SJim Ingham break; 935e4284b71SJim Ingham } 936e4284b71SJim Ingham } 937e4284b71SJim Ingham return ret_sp; 938e4284b71SJim Ingham } 939e4284b71SJim Ingham 940b9c1b51eSKate Stone size_t StackFrameList::GetStatus(Stream &strm, uint32_t first_frame, 941b9c1b51eSKate Stone uint32_t num_frames, bool show_frame_info, 9428ec10efcSJim Ingham uint32_t num_frames_with_source, 9437f1c1211SPavel Labath bool show_unique, 944b9c1b51eSKate Stone const char *selected_frame_marker) { 9457260f620SGreg Clayton size_t num_frames_displayed = 0; 9467260f620SGreg Clayton 9477260f620SGreg Clayton if (num_frames == 0) 9487260f620SGreg Clayton return 0; 9497260f620SGreg Clayton 950b57e4a1bSJason Molenda StackFrameSP frame_sp; 9517260f620SGreg Clayton uint32_t frame_idx = 0; 9527260f620SGreg Clayton uint32_t last_frame; 9537260f620SGreg Clayton 9547260f620SGreg Clayton // Don't let the last frame wrap around... 9557260f620SGreg Clayton if (num_frames == UINT32_MAX) 9567260f620SGreg Clayton last_frame = UINT32_MAX; 9577260f620SGreg Clayton else 9587260f620SGreg Clayton last_frame = first_frame + num_frames; 9597260f620SGreg Clayton 960b57e4a1bSJason Molenda StackFrameSP selected_frame_sp = m_thread.GetSelectedFrame(); 961d70a6e71SEugene Zelenko const char *unselected_marker = nullptr; 9628ec10efcSJim Ingham std::string buffer; 963b9c1b51eSKate Stone if (selected_frame_marker) { 9648ec10efcSJim Ingham size_t len = strlen(selected_frame_marker); 9658ec10efcSJim Ingham buffer.insert(buffer.begin(), len, ' '); 9668ec10efcSJim Ingham unselected_marker = buffer.c_str(); 9678ec10efcSJim Ingham } 968d70a6e71SEugene Zelenko const char *marker = nullptr; 9698ec10efcSJim Ingham 970b9c1b51eSKate Stone for (frame_idx = first_frame; frame_idx < last_frame; ++frame_idx) { 9717260f620SGreg Clayton frame_sp = GetFrameAtIndex(frame_idx); 972d70a6e71SEugene Zelenko if (!frame_sp) 9737260f620SGreg Clayton break; 9747260f620SGreg Clayton 975b9c1b51eSKate Stone if (selected_frame_marker != nullptr) { 9768ec10efcSJim Ingham if (frame_sp == selected_frame_sp) 9778ec10efcSJim Ingham marker = selected_frame_marker; 9788ec10efcSJim Ingham else 9798ec10efcSJim Ingham marker = unselected_marker; 9808ec10efcSJim Ingham } 9818ec10efcSJim Ingham 982b9c1b51eSKate Stone if (!frame_sp->GetStatus(strm, show_frame_info, 983b9c1b51eSKate Stone num_frames_with_source > (first_frame - frame_idx), 9847f1c1211SPavel Labath show_unique, marker)) 9857260f620SGreg Clayton break; 9867260f620SGreg Clayton ++num_frames_displayed; 9877260f620SGreg Clayton } 9887260f620SGreg Clayton 9897260f620SGreg Clayton strm.IndentLess(); 9907260f620SGreg Clayton return num_frames_displayed; 9917260f620SGreg Clayton } 992