1cfca06d7SDimitry Andric //===-- StackFrameList.cpp ------------------------------------------------===//
2f034231aSEd Maste //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f034231aSEd Maste //
7f034231aSEd Maste //===----------------------------------------------------------------------===//
8f034231aSEd Maste
9f3fbd1c0SDimitry Andric #include "lldb/Target/StackFrameList.h"
10f034231aSEd Maste #include "lldb/Breakpoint/Breakpoint.h"
1114f1b3e8SDimitry Andric #include "lldb/Breakpoint/BreakpointLocation.h"
127fa27ce4SDimitry Andric #include "lldb/Core/Debugger.h"
13f034231aSEd Maste #include "lldb/Core/SourceManager.h"
14b1c73532SDimitry Andric #include "lldb/Host/StreamFile.h"
15f034231aSEd Maste #include "lldb/Symbol/Block.h"
16f034231aSEd Maste #include "lldb/Symbol/Function.h"
17f034231aSEd Maste #include "lldb/Symbol/Symbol.h"
18f034231aSEd Maste #include "lldb/Target/Process.h"
19f034231aSEd Maste #include "lldb/Target/RegisterContext.h"
20f034231aSEd Maste #include "lldb/Target/StackFrame.h"
217fa27ce4SDimitry Andric #include "lldb/Target/StackFrameRecognizer.h"
22f034231aSEd Maste #include "lldb/Target/StopInfo.h"
23f034231aSEd Maste #include "lldb/Target/Target.h"
24f034231aSEd Maste #include "lldb/Target/Thread.h"
25f034231aSEd Maste #include "lldb/Target/Unwind.h"
26145449b1SDimitry Andric #include "lldb/Utility/LLDBLog.h"
2774a628f7SDimitry Andric #include "lldb/Utility/Log.h"
2894994d37SDimitry Andric #include "llvm/ADT/SmallPtrSet.h"
29f034231aSEd Maste
305f29bb8aSDimitry Andric #include <memory>
315f29bb8aSDimitry Andric
32f034231aSEd Maste //#define DEBUG_STACK_FRAMES 1
33f034231aSEd Maste
34f034231aSEd Maste using namespace lldb;
35f034231aSEd Maste using namespace lldb_private;
36f034231aSEd Maste
37f034231aSEd Maste // StackFrameList constructor
StackFrameList(Thread & thread,const lldb::StackFrameListSP & prev_frames_sp,bool show_inline_frames)3814f1b3e8SDimitry Andric StackFrameList::StackFrameList(Thread &thread,
3914f1b3e8SDimitry Andric const lldb::StackFrameListSP &prev_frames_sp,
4014f1b3e8SDimitry Andric bool show_inline_frames)
4114f1b3e8SDimitry Andric : m_thread(thread), m_prev_frames_sp(prev_frames_sp), m_mutex(), m_frames(),
427fa27ce4SDimitry Andric m_selected_frame_idx(), m_concrete_frames_fetched(0),
43f034231aSEd Maste m_current_inlined_depth(UINT32_MAX),
44f034231aSEd Maste m_current_inlined_pc(LLDB_INVALID_ADDRESS),
4514f1b3e8SDimitry Andric m_show_inlined_frames(show_inline_frames) {
4614f1b3e8SDimitry Andric if (prev_frames_sp) {
47f034231aSEd Maste m_current_inlined_depth = prev_frames_sp->m_current_inlined_depth;
48f034231aSEd Maste m_current_inlined_pc = prev_frames_sp->m_current_inlined_pc;
49f034231aSEd Maste }
50f034231aSEd Maste }
51f034231aSEd Maste
~StackFrameList()5214f1b3e8SDimitry Andric StackFrameList::~StackFrameList() {
53f73363f1SDimitry Andric // Call clear since this takes a lock and clears the stack frame list in case
54f73363f1SDimitry Andric // another thread is currently using this stack frame list
55f034231aSEd Maste Clear();
56f034231aSEd Maste }
57f034231aSEd Maste
CalculateCurrentInlinedDepth()5814f1b3e8SDimitry Andric void StackFrameList::CalculateCurrentInlinedDepth() {
59f034231aSEd Maste uint32_t cur_inlined_depth = GetCurrentInlinedDepth();
6014f1b3e8SDimitry Andric if (cur_inlined_depth == UINT32_MAX) {
61f034231aSEd Maste ResetCurrentInlinedDepth();
62f034231aSEd Maste }
63f034231aSEd Maste }
64f034231aSEd Maste
GetCurrentInlinedDepth()6514f1b3e8SDimitry Andric uint32_t StackFrameList::GetCurrentInlinedDepth() {
6614f1b3e8SDimitry Andric if (m_show_inlined_frames && m_current_inlined_pc != LLDB_INVALID_ADDRESS) {
67f034231aSEd Maste lldb::addr_t cur_pc = m_thread.GetRegisterContext()->GetPC();
6814f1b3e8SDimitry Andric if (cur_pc != m_current_inlined_pc) {
69f034231aSEd Maste m_current_inlined_pc = LLDB_INVALID_ADDRESS;
70f034231aSEd Maste m_current_inlined_depth = UINT32_MAX;
71145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Step);
72f034231aSEd Maste if (log && log->GetVerbose())
73ead24645SDimitry Andric LLDB_LOGF(
74ead24645SDimitry Andric log,
7514f1b3e8SDimitry Andric "GetCurrentInlinedDepth: invalidating current inlined depth.\n");
76f034231aSEd Maste }
77f034231aSEd Maste return m_current_inlined_depth;
7814f1b3e8SDimitry Andric } else {
79f034231aSEd Maste return UINT32_MAX;
80f034231aSEd Maste }
81f034231aSEd Maste }
82f034231aSEd Maste
ResetCurrentInlinedDepth()8314f1b3e8SDimitry Andric void StackFrameList::ResetCurrentInlinedDepth() {
8494994d37SDimitry Andric if (!m_show_inlined_frames)
8594994d37SDimitry Andric return;
8694994d37SDimitry Andric
87f3fbd1c0SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_mutex);
88e81d9d49SDimitry Andric
897fa27ce4SDimitry Andric GetFramesUpTo(0, DoNotAllowInterruption);
90f3fbd1c0SDimitry Andric if (m_frames.empty())
91e81d9d49SDimitry Andric return;
9214f1b3e8SDimitry Andric if (!m_frames[0]->IsInlined()) {
93f034231aSEd Maste m_current_inlined_depth = UINT32_MAX;
94f034231aSEd Maste m_current_inlined_pc = LLDB_INVALID_ADDRESS;
95145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Step);
96f034231aSEd Maste if (log && log->GetVerbose())
97ead24645SDimitry Andric LLDB_LOGF(
98ead24645SDimitry Andric log,
9914f1b3e8SDimitry Andric "ResetCurrentInlinedDepth: Invalidating current inlined depth.\n");
10094994d37SDimitry Andric return;
10194994d37SDimitry Andric }
10294994d37SDimitry Andric
103f73363f1SDimitry Andric // We only need to do something special about inlined blocks when we are
104f73363f1SDimitry Andric // at the beginning of an inlined function:
10514f1b3e8SDimitry Andric // FIXME: We probably also have to do something special if the PC is at
10694994d37SDimitry Andric // the END of an inlined function, which coincides with the end of either
10794994d37SDimitry Andric // its containing function or another inlined function.
108f034231aSEd Maste
109f034231aSEd Maste Block *block_ptr = m_frames[0]->GetFrameBlock();
11094994d37SDimitry Andric if (!block_ptr)
11194994d37SDimitry Andric return;
11294994d37SDimitry Andric
113f034231aSEd Maste Address pc_as_address;
11494994d37SDimitry Andric lldb::addr_t curr_pc = m_thread.GetRegisterContext()->GetPC();
11594994d37SDimitry Andric pc_as_address.SetLoadAddress(curr_pc, &(m_thread.GetProcess()->GetTarget()));
116f034231aSEd Maste AddressRange containing_range;
11794994d37SDimitry Andric if (!block_ptr->GetRangeContainingAddress(pc_as_address, containing_range) ||
11894994d37SDimitry Andric pc_as_address != containing_range.GetBaseAddress())
11994994d37SDimitry Andric return;
12094994d37SDimitry Andric
12194994d37SDimitry Andric // If we got here because of a breakpoint hit, then set the inlined depth
12294994d37SDimitry Andric // depending on where the breakpoint was set. If we got here because of a
12394994d37SDimitry Andric // crash, then set the inlined depth to the deepest most block. Otherwise,
12494994d37SDimitry Andric // we stopped here naturally as the result of a step, so set ourselves in the
12594994d37SDimitry Andric // containing frame of the whole set of nested inlines, so the user can then
12694994d37SDimitry Andric // "virtually" step into the frames one by one, or next over the whole mess.
12794994d37SDimitry Andric // Note: We don't have to handle being somewhere in the middle of the stack
12894994d37SDimitry Andric // here, since ResetCurrentInlinedDepth doesn't get called if there is a
12994994d37SDimitry Andric // valid inlined depth set.
130f034231aSEd Maste StopInfoSP stop_info_sp = m_thread.GetStopInfo();
13194994d37SDimitry Andric if (!stop_info_sp)
13294994d37SDimitry Andric return;
13314f1b3e8SDimitry Andric switch (stop_info_sp->GetStopReason()) {
134f034231aSEd Maste case eStopReasonWatchpoint:
135f034231aSEd Maste case eStopReasonException:
136f034231aSEd Maste case eStopReasonExec:
137344a3780SDimitry Andric case eStopReasonFork:
138344a3780SDimitry Andric case eStopReasonVFork:
139344a3780SDimitry Andric case eStopReasonVForkDone:
140f034231aSEd Maste case eStopReasonSignal:
14194994d37SDimitry Andric // In all these cases we want to stop in the deepest frame.
142f034231aSEd Maste m_current_inlined_pc = curr_pc;
143f034231aSEd Maste m_current_inlined_depth = 0;
144f034231aSEd Maste break;
14514f1b3e8SDimitry Andric case eStopReasonBreakpoint: {
14694994d37SDimitry Andric // FIXME: Figure out what this break point is doing, and set the inline
14794994d37SDimitry Andric // depth appropriately. Be careful to take into account breakpoints that
14894994d37SDimitry Andric // implement step over prologue, since that should do the default
14994994d37SDimitry Andric // calculation. For now, if the breakpoints corresponding to this hit are
15094994d37SDimitry Andric // all internal, I set the stop location to the top of the inlined stack,
15194994d37SDimitry Andric // since that will make things like stepping over prologues work right.
15294994d37SDimitry Andric // But if there are any non-internal breakpoints I do to the bottom of the
15394994d37SDimitry Andric // stack, since that was the old behavior.
154f034231aSEd Maste uint32_t bp_site_id = stop_info_sp->GetValue();
15514f1b3e8SDimitry Andric BreakpointSiteSP bp_site_sp(
15694994d37SDimitry Andric m_thread.GetProcess()->GetBreakpointSiteList().FindByID(bp_site_id));
157f034231aSEd Maste bool all_internal = true;
15814f1b3e8SDimitry Andric if (bp_site_sp) {
159b1c73532SDimitry Andric uint32_t num_owners = bp_site_sp->GetNumberOfConstituents();
16014f1b3e8SDimitry Andric for (uint32_t i = 0; i < num_owners; i++) {
161b1c73532SDimitry Andric Breakpoint &bp_ref =
162b1c73532SDimitry Andric bp_site_sp->GetConstituentAtIndex(i)->GetBreakpoint();
16314f1b3e8SDimitry Andric if (!bp_ref.IsInternal()) {
164f034231aSEd Maste all_internal = false;
165f034231aSEd Maste }
166f034231aSEd Maste }
167f034231aSEd Maste }
16814f1b3e8SDimitry Andric if (!all_internal) {
169f034231aSEd Maste m_current_inlined_pc = curr_pc;
170f034231aSEd Maste m_current_inlined_depth = 0;
171f034231aSEd Maste break;
172f034231aSEd Maste }
173f034231aSEd Maste }
174e3b55780SDimitry Andric [[fallthrough]];
17514f1b3e8SDimitry Andric default: {
17694994d37SDimitry Andric // Otherwise, we should set ourselves at the container of the inlining, so
17794994d37SDimitry Andric // that the user can descend into them. So first we check whether we have
17894994d37SDimitry Andric // more than one inlined block sharing this PC:
179f034231aSEd Maste int num_inlined_functions = 0;
180f034231aSEd Maste
181f034231aSEd Maste for (Block *container_ptr = block_ptr->GetInlinedParent();
182f3fbd1c0SDimitry Andric container_ptr != nullptr;
18314f1b3e8SDimitry Andric container_ptr = container_ptr->GetInlinedParent()) {
18494994d37SDimitry Andric if (!container_ptr->GetRangeContainingAddress(pc_as_address,
18594994d37SDimitry Andric containing_range))
186f034231aSEd Maste break;
187f034231aSEd Maste if (pc_as_address != containing_range.GetBaseAddress())
188f034231aSEd Maste break;
189f034231aSEd Maste
190f034231aSEd Maste num_inlined_functions++;
191f034231aSEd Maste }
192f034231aSEd Maste m_current_inlined_pc = curr_pc;
193f034231aSEd Maste m_current_inlined_depth = num_inlined_functions + 1;
194145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Step);
195f034231aSEd Maste if (log && log->GetVerbose())
196ead24645SDimitry Andric LLDB_LOGF(log,
197ead24645SDimitry Andric "ResetCurrentInlinedDepth: setting inlined "
19814f1b3e8SDimitry Andric "depth: %d 0x%" PRIx64 ".\n",
19914f1b3e8SDimitry Andric m_current_inlined_depth, curr_pc);
200f034231aSEd Maste
20194994d37SDimitry Andric break;
202f034231aSEd Maste }
203f034231aSEd Maste }
204f034231aSEd Maste }
205f034231aSEd Maste
DecrementCurrentInlinedDepth()20614f1b3e8SDimitry Andric bool StackFrameList::DecrementCurrentInlinedDepth() {
20714f1b3e8SDimitry Andric if (m_show_inlined_frames) {
208f034231aSEd Maste uint32_t current_inlined_depth = GetCurrentInlinedDepth();
20914f1b3e8SDimitry Andric if (current_inlined_depth != UINT32_MAX) {
21014f1b3e8SDimitry Andric if (current_inlined_depth > 0) {
211f034231aSEd Maste m_current_inlined_depth--;
212f034231aSEd Maste return true;
213f034231aSEd Maste }
214f034231aSEd Maste }
215f034231aSEd Maste }
216f034231aSEd Maste return false;
217f034231aSEd Maste }
218f034231aSEd Maste
SetCurrentInlinedDepth(uint32_t new_depth)21914f1b3e8SDimitry Andric void StackFrameList::SetCurrentInlinedDepth(uint32_t new_depth) {
220f034231aSEd Maste m_current_inlined_depth = new_depth;
221f034231aSEd Maste if (new_depth == UINT32_MAX)
222f034231aSEd Maste m_current_inlined_pc = LLDB_INVALID_ADDRESS;
223f034231aSEd Maste else
224f034231aSEd Maste m_current_inlined_pc = m_thread.GetRegisterContext()->GetPC();
225f034231aSEd Maste }
226f034231aSEd Maste
GetOnlyConcreteFramesUpTo(uint32_t end_idx,Unwind & unwinder)22794994d37SDimitry Andric void StackFrameList::GetOnlyConcreteFramesUpTo(uint32_t end_idx,
228cfca06d7SDimitry Andric Unwind &unwinder) {
22994994d37SDimitry Andric assert(m_thread.IsValid() && "Expected valid thread");
23094994d37SDimitry Andric assert(m_frames.size() <= end_idx && "Expected there to be frames to fill");
23194994d37SDimitry Andric
23294994d37SDimitry Andric if (end_idx < m_concrete_frames_fetched)
23394994d37SDimitry Andric return;
23494994d37SDimitry Andric
235cfca06d7SDimitry Andric uint32_t num_frames = unwinder.GetFramesUpTo(end_idx);
23694994d37SDimitry Andric if (num_frames <= end_idx + 1) {
23794994d37SDimitry Andric // Done unwinding.
23894994d37SDimitry Andric m_concrete_frames_fetched = UINT32_MAX;
23994994d37SDimitry Andric }
24094994d37SDimitry Andric
24194994d37SDimitry Andric // Don't create the frames eagerly. Defer this work to GetFrameAtIndex,
24294994d37SDimitry Andric // which can lazily query the unwinder to create frames.
24394994d37SDimitry Andric m_frames.resize(num_frames);
24494994d37SDimitry Andric }
24594994d37SDimitry Andric
246cfca06d7SDimitry Andric /// A sequence of calls that comprise some portion of a backtrace. Each frame
247cfca06d7SDimitry Andric /// is represented as a pair of a callee (Function *) and an address within the
248cfca06d7SDimitry Andric /// callee.
249cfca06d7SDimitry Andric struct CallDescriptor {
250cfca06d7SDimitry Andric Function *func;
251cfca06d7SDimitry Andric CallEdge::AddrType address_type = CallEdge::AddrType::Call;
252cfca06d7SDimitry Andric addr_t address = LLDB_INVALID_ADDRESS;
253cfca06d7SDimitry Andric };
254cfca06d7SDimitry Andric using CallSequence = std::vector<CallDescriptor>;
255cfca06d7SDimitry Andric
25694994d37SDimitry Andric /// Find the unique path through the call graph from \p begin (with return PC
25794994d37SDimitry Andric /// \p return_pc) to \p end. On success this path is stored into \p path, and
25894994d37SDimitry Andric /// on failure \p path is unchanged.
FindInterveningFrames(Function & begin,Function & end,ExecutionContext & exe_ctx,Target & target,addr_t return_pc,CallSequence & path,ModuleList & images,Log * log)25994994d37SDimitry Andric static void FindInterveningFrames(Function &begin, Function &end,
260706b4fc4SDimitry Andric ExecutionContext &exe_ctx, Target &target,
261cfca06d7SDimitry Andric addr_t return_pc, CallSequence &path,
26294994d37SDimitry Andric ModuleList &images, Log *log) {
26394994d37SDimitry Andric LLDB_LOG(log, "Finding frames between {0} and {1}, retn-pc={2:x}",
26494994d37SDimitry Andric begin.GetDisplayName(), end.GetDisplayName(), return_pc);
26594994d37SDimitry Andric
26694994d37SDimitry Andric // Find a non-tail calling edge with the correct return PC.
26794994d37SDimitry Andric if (log)
268706b4fc4SDimitry Andric for (const auto &edge : begin.GetCallEdges())
26994994d37SDimitry Andric LLDB_LOG(log, "FindInterveningFrames: found call with retn-PC = {0:x}",
270706b4fc4SDimitry Andric edge->GetReturnPCAddress(begin, target));
271ead24645SDimitry Andric CallEdge *first_edge = begin.GetCallEdgeForReturnAddress(return_pc, target);
272ead24645SDimitry Andric if (!first_edge) {
27394994d37SDimitry Andric LLDB_LOG(log, "No call edge outgoing from {0} with retn-PC == {1:x}",
27494994d37SDimitry Andric begin.GetDisplayName(), return_pc);
27594994d37SDimitry Andric return;
27694994d37SDimitry Andric }
27794994d37SDimitry Andric
27894994d37SDimitry Andric // The first callee may not be resolved, or there may be nothing to fill in.
279706b4fc4SDimitry Andric Function *first_callee = first_edge->GetCallee(images, exe_ctx);
28094994d37SDimitry Andric if (!first_callee) {
28194994d37SDimitry Andric LLDB_LOG(log, "Could not resolve callee");
28294994d37SDimitry Andric return;
28394994d37SDimitry Andric }
28494994d37SDimitry Andric if (first_callee == &end) {
28594994d37SDimitry Andric LLDB_LOG(log, "Not searching further, first callee is {0} (retn-PC: {1:x})",
28694994d37SDimitry Andric end.GetDisplayName(), return_pc);
28794994d37SDimitry Andric return;
28894994d37SDimitry Andric }
28994994d37SDimitry Andric
29094994d37SDimitry Andric // Run DFS on the tail-calling edges out of the first callee to find \p end.
29194994d37SDimitry Andric // Fully explore the set of functions reachable from the first edge via tail
29294994d37SDimitry Andric // calls in order to detect ambiguous executions.
29394994d37SDimitry Andric struct DFS {
294cfca06d7SDimitry Andric CallSequence active_path = {};
295cfca06d7SDimitry Andric CallSequence solution_path = {};
29694994d37SDimitry Andric llvm::SmallPtrSet<Function *, 2> visited_nodes = {};
29794994d37SDimitry Andric bool ambiguous = false;
29894994d37SDimitry Andric Function *end;
29994994d37SDimitry Andric ModuleList &images;
300cfca06d7SDimitry Andric Target ⌖
301706b4fc4SDimitry Andric ExecutionContext &context;
30294994d37SDimitry Andric
303cfca06d7SDimitry Andric DFS(Function *end, ModuleList &images, Target &target,
304cfca06d7SDimitry Andric ExecutionContext &context)
305cfca06d7SDimitry Andric : end(end), images(images), target(target), context(context) {}
30694994d37SDimitry Andric
307cfca06d7SDimitry Andric void search(CallEdge &first_edge, Function &first_callee,
308cfca06d7SDimitry Andric CallSequence &path) {
309cfca06d7SDimitry Andric dfs(first_edge, first_callee);
31094994d37SDimitry Andric if (!ambiguous)
31194994d37SDimitry Andric path = std::move(solution_path);
31294994d37SDimitry Andric }
31394994d37SDimitry Andric
314cfca06d7SDimitry Andric void dfs(CallEdge ¤t_edge, Function &callee) {
31594994d37SDimitry Andric // Found a path to the target function.
316ead24645SDimitry Andric if (&callee == end) {
31794994d37SDimitry Andric if (solution_path.empty())
31894994d37SDimitry Andric solution_path = active_path;
31994994d37SDimitry Andric else
32094994d37SDimitry Andric ambiguous = true;
32194994d37SDimitry Andric return;
32294994d37SDimitry Andric }
32394994d37SDimitry Andric
32494994d37SDimitry Andric // Terminate the search if tail recursion is found, or more generally if
32594994d37SDimitry Andric // there's more than one way to reach a target. This errs on the side of
32694994d37SDimitry Andric // caution: it conservatively stops searching when some solutions are
32794994d37SDimitry Andric // still possible to save time in the average case.
328ead24645SDimitry Andric if (!visited_nodes.insert(&callee).second) {
32994994d37SDimitry Andric ambiguous = true;
33094994d37SDimitry Andric return;
33194994d37SDimitry Andric }
33294994d37SDimitry Andric
33394994d37SDimitry Andric // Search the calls made from this callee.
334cfca06d7SDimitry Andric active_path.push_back(CallDescriptor{&callee});
335706b4fc4SDimitry Andric for (const auto &edge : callee.GetTailCallingEdges()) {
336706b4fc4SDimitry Andric Function *next_callee = edge->GetCallee(images, context);
33794994d37SDimitry Andric if (!next_callee)
33894994d37SDimitry Andric continue;
33994994d37SDimitry Andric
340cfca06d7SDimitry Andric std::tie(active_path.back().address_type, active_path.back().address) =
341cfca06d7SDimitry Andric edge->GetCallerAddress(callee, target);
342cfca06d7SDimitry Andric
343cfca06d7SDimitry Andric dfs(*edge, *next_callee);
34494994d37SDimitry Andric if (ambiguous)
34594994d37SDimitry Andric return;
34694994d37SDimitry Andric }
34794994d37SDimitry Andric active_path.pop_back();
34894994d37SDimitry Andric }
34994994d37SDimitry Andric };
35094994d37SDimitry Andric
351cfca06d7SDimitry Andric DFS(&end, images, target, exe_ctx).search(*first_edge, *first_callee, path);
35294994d37SDimitry Andric }
35394994d37SDimitry Andric
35494994d37SDimitry Andric /// Given that \p next_frame will be appended to the frame list, synthesize
35594994d37SDimitry Andric /// tail call frames between the current end of the list and \p next_frame.
35694994d37SDimitry Andric /// If any frames are added, adjust the frame index of \p next_frame.
35794994d37SDimitry Andric ///
35894994d37SDimitry Andric /// --------------
35994994d37SDimitry Andric /// | ... | <- Completed frames.
36094994d37SDimitry Andric /// --------------
36194994d37SDimitry Andric /// | prev_frame |
36294994d37SDimitry Andric /// --------------
36394994d37SDimitry Andric /// | ... | <- Artificial frames inserted here.
36494994d37SDimitry Andric /// --------------
36594994d37SDimitry Andric /// | next_frame |
36694994d37SDimitry Andric /// --------------
36794994d37SDimitry Andric /// | ... | <- Not-yet-visited frames.
36894994d37SDimitry Andric /// --------------
SynthesizeTailCallFrames(StackFrame & next_frame)36994994d37SDimitry Andric void StackFrameList::SynthesizeTailCallFrames(StackFrame &next_frame) {
370cfca06d7SDimitry Andric // Cannot synthesize tail call frames when the stack is empty (there is no
371cfca06d7SDimitry Andric // "previous" frame).
372cfca06d7SDimitry Andric if (m_frames.empty())
373cfca06d7SDimitry Andric return;
374cfca06d7SDimitry Andric
37594994d37SDimitry Andric TargetSP target_sp = next_frame.CalculateTarget();
37694994d37SDimitry Andric if (!target_sp)
37794994d37SDimitry Andric return;
37894994d37SDimitry Andric
37994994d37SDimitry Andric lldb::RegisterContextSP next_reg_ctx_sp = next_frame.GetRegisterContext();
38094994d37SDimitry Andric if (!next_reg_ctx_sp)
38194994d37SDimitry Andric return;
38294994d37SDimitry Andric
383145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Step);
38494994d37SDimitry Andric
38594994d37SDimitry Andric StackFrame &prev_frame = *m_frames.back().get();
38694994d37SDimitry Andric
38794994d37SDimitry Andric // Find the functions prev_frame and next_frame are stopped in. The function
38894994d37SDimitry Andric // objects are needed to search the lazy call graph for intervening frames.
38994994d37SDimitry Andric Function *prev_func =
39094994d37SDimitry Andric prev_frame.GetSymbolContext(eSymbolContextFunction).function;
39194994d37SDimitry Andric if (!prev_func) {
39294994d37SDimitry Andric LLDB_LOG(log, "SynthesizeTailCallFrames: can't find previous function");
39394994d37SDimitry Andric return;
39494994d37SDimitry Andric }
39594994d37SDimitry Andric Function *next_func =
39694994d37SDimitry Andric next_frame.GetSymbolContext(eSymbolContextFunction).function;
39794994d37SDimitry Andric if (!next_func) {
39894994d37SDimitry Andric LLDB_LOG(log, "SynthesizeTailCallFrames: can't find next function");
39994994d37SDimitry Andric return;
40094994d37SDimitry Andric }
40194994d37SDimitry Andric
40294994d37SDimitry Andric // Try to find the unique sequence of (tail) calls which led from next_frame
40394994d37SDimitry Andric // to prev_frame.
404cfca06d7SDimitry Andric CallSequence path;
40594994d37SDimitry Andric addr_t return_pc = next_reg_ctx_sp->GetPC();
40694994d37SDimitry Andric Target &target = *target_sp.get();
40794994d37SDimitry Andric ModuleList &images = next_frame.CalculateTarget()->GetImages();
408706b4fc4SDimitry Andric ExecutionContext exe_ctx(target_sp, /*get_process=*/true);
409706b4fc4SDimitry Andric exe_ctx.SetFramePtr(&next_frame);
410706b4fc4SDimitry Andric FindInterveningFrames(*next_func, *prev_func, exe_ctx, target, return_pc,
411706b4fc4SDimitry Andric path, images, log);
41294994d37SDimitry Andric
41394994d37SDimitry Andric // Push synthetic tail call frames.
414cfca06d7SDimitry Andric for (auto calleeInfo : llvm::reverse(path)) {
415cfca06d7SDimitry Andric Function *callee = calleeInfo.func;
41694994d37SDimitry Andric uint32_t frame_idx = m_frames.size();
41794994d37SDimitry Andric uint32_t concrete_frame_idx = next_frame.GetConcreteFrameIndex();
41894994d37SDimitry Andric addr_t cfa = LLDB_INVALID_ADDRESS;
41994994d37SDimitry Andric bool cfa_is_valid = false;
420cfca06d7SDimitry Andric addr_t pc = calleeInfo.address;
421cfca06d7SDimitry Andric // If the callee address refers to the call instruction, we do not want to
422cfca06d7SDimitry Andric // subtract 1 from this value.
423cfca06d7SDimitry Andric const bool behaves_like_zeroth_frame =
424cfca06d7SDimitry Andric calleeInfo.address_type == CallEdge::AddrType::Call;
42594994d37SDimitry Andric SymbolContext sc;
42694994d37SDimitry Andric callee->CalculateSymbolContext(&sc);
42794994d37SDimitry Andric auto synth_frame = std::make_shared<StackFrame>(
42894994d37SDimitry Andric m_thread.shared_from_this(), frame_idx, concrete_frame_idx, cfa,
429ead24645SDimitry Andric cfa_is_valid, pc, StackFrame::Kind::Artificial,
430ead24645SDimitry Andric behaves_like_zeroth_frame, &sc);
43194994d37SDimitry Andric m_frames.push_back(synth_frame);
432cfca06d7SDimitry Andric LLDB_LOG(log, "Pushed frame {0} at {1:x}", callee->GetDisplayName(), pc);
43394994d37SDimitry Andric }
43494994d37SDimitry Andric
43594994d37SDimitry Andric // If any frames were created, adjust next_frame's index.
43694994d37SDimitry Andric if (!path.empty())
43794994d37SDimitry Andric next_frame.SetFrameIndex(m_frames.size());
43894994d37SDimitry Andric }
43994994d37SDimitry Andric
GetFramesUpTo(uint32_t end_idx,InterruptionControl allow_interrupt)4407fa27ce4SDimitry Andric bool StackFrameList::GetFramesUpTo(uint32_t end_idx,
4417fa27ce4SDimitry Andric InterruptionControl allow_interrupt) {
44294994d37SDimitry Andric // Do not fetch frames for an invalid thread.
4437fa27ce4SDimitry Andric bool was_interrupted = false;
444f3fbd1c0SDimitry Andric if (!m_thread.IsValid())
4457fa27ce4SDimitry Andric return false;
446f034231aSEd Maste
44714f1b3e8SDimitry Andric // We've already gotten more frames than asked for, or we've already finished
44814f1b3e8SDimitry Andric // unwinding, return.
449f034231aSEd Maste if (m_frames.size() > end_idx || GetAllFramesFetched())
4507fa27ce4SDimitry Andric return false;
451f034231aSEd Maste
452cfca06d7SDimitry Andric Unwind &unwinder = m_thread.GetUnwinder();
453f034231aSEd Maste
45494994d37SDimitry Andric if (!m_show_inlined_frames) {
45594994d37SDimitry Andric GetOnlyConcreteFramesUpTo(end_idx, unwinder);
4567fa27ce4SDimitry Andric return false;
45794994d37SDimitry Andric }
45894994d37SDimitry Andric
459f034231aSEd Maste #if defined(DEBUG_STACK_FRAMES)
460f034231aSEd Maste StreamFile s(stdout, false);
461f034231aSEd Maste #endif
462f73363f1SDimitry Andric // If we are hiding some frames from the outside world, we need to add
463f73363f1SDimitry Andric // those onto the total count of frames to fetch. However, we don't need
464f73363f1SDimitry Andric // to do that if end_idx is 0 since in that case we always get the first
465f73363f1SDimitry Andric // concrete frame and all the inlined frames below it... And of course, if
466f73363f1SDimitry Andric // end_idx is UINT32_MAX that means get all, so just do that...
467f034231aSEd Maste
468f034231aSEd Maste uint32_t inlined_depth = 0;
46914f1b3e8SDimitry Andric if (end_idx > 0 && end_idx != UINT32_MAX) {
470f034231aSEd Maste inlined_depth = GetCurrentInlinedDepth();
47114f1b3e8SDimitry Andric if (inlined_depth != UINT32_MAX) {
472f034231aSEd Maste if (end_idx > 0)
473f034231aSEd Maste end_idx += inlined_depth;
474f034231aSEd Maste }
475f034231aSEd Maste }
476f034231aSEd Maste
477f034231aSEd Maste StackFrameSP unwind_frame_sp;
4787fa27ce4SDimitry Andric Debugger &dbg = m_thread.GetProcess()->GetTarget().GetDebugger();
47914f1b3e8SDimitry Andric do {
480f034231aSEd Maste uint32_t idx = m_concrete_frames_fetched++;
481205afe67SEd Maste lldb::addr_t pc = LLDB_INVALID_ADDRESS;
482205afe67SEd Maste lldb::addr_t cfa = LLDB_INVALID_ADDRESS;
483ead24645SDimitry Andric bool behaves_like_zeroth_frame = (idx == 0);
48414f1b3e8SDimitry Andric if (idx == 0) {
485f73363f1SDimitry Andric // We might have already created frame zero, only create it if we need
48694994d37SDimitry Andric // to.
48714f1b3e8SDimitry Andric if (m_frames.empty()) {
488f034231aSEd Maste RegisterContextSP reg_ctx_sp(m_thread.GetRegisterContext());
489f034231aSEd Maste
49014f1b3e8SDimitry Andric if (reg_ctx_sp) {
491cfca06d7SDimitry Andric const bool success = unwinder.GetFrameInfoAtIndex(
492ead24645SDimitry Andric idx, cfa, pc, behaves_like_zeroth_frame);
493f73363f1SDimitry Andric // There shouldn't be any way not to get the frame info for frame
494f73363f1SDimitry Andric // 0. But if the unwinder can't make one, lets make one by hand
49594994d37SDimitry Andric // with the SP as the CFA and see if that gets any further.
49614f1b3e8SDimitry Andric if (!success) {
497f034231aSEd Maste cfa = reg_ctx_sp->GetSP();
498f034231aSEd Maste pc = reg_ctx_sp->GetPC();
499f034231aSEd Maste }
500f034231aSEd Maste
5015f29bb8aSDimitry Andric unwind_frame_sp = std::make_shared<StackFrame>(
5025f29bb8aSDimitry Andric m_thread.shared_from_this(), m_frames.size(), idx, reg_ctx_sp,
503ead24645SDimitry Andric cfa, pc, behaves_like_zeroth_frame, nullptr);
504f034231aSEd Maste m_frames.push_back(unwind_frame_sp);
505f034231aSEd Maste }
50614f1b3e8SDimitry Andric } else {
507f034231aSEd Maste unwind_frame_sp = m_frames.front();
508f034231aSEd Maste cfa = unwind_frame_sp->m_id.GetCallFrameAddress();
509f034231aSEd Maste }
51014f1b3e8SDimitry Andric } else {
5117fa27ce4SDimitry Andric // Check for interruption when building the frames.
5127fa27ce4SDimitry Andric // Do the check in idx > 0 so that we'll always create a 0th frame.
5137fa27ce4SDimitry Andric if (allow_interrupt
5147fa27ce4SDimitry Andric && INTERRUPT_REQUESTED(dbg, "Interrupted having fetched {0} frames",
5157fa27ce4SDimitry Andric m_frames.size())) {
5167fa27ce4SDimitry Andric was_interrupted = true;
5177fa27ce4SDimitry Andric break;
5187fa27ce4SDimitry Andric }
5197fa27ce4SDimitry Andric
520cfca06d7SDimitry Andric const bool success =
521cfca06d7SDimitry Andric unwinder.GetFrameInfoAtIndex(idx, cfa, pc, behaves_like_zeroth_frame);
52214f1b3e8SDimitry Andric if (!success) {
523f034231aSEd Maste // We've gotten to the end of the stack.
524f034231aSEd Maste SetAllFramesFetched();
525f034231aSEd Maste break;
526f034231aSEd Maste }
527f21a844fSEd Maste const bool cfa_is_valid = true;
5285f29bb8aSDimitry Andric unwind_frame_sp = std::make_shared<StackFrame>(
5295f29bb8aSDimitry Andric m_thread.shared_from_this(), m_frames.size(), idx, cfa, cfa_is_valid,
530ead24645SDimitry Andric pc, StackFrame::Kind::Regular, behaves_like_zeroth_frame, nullptr);
53194994d37SDimitry Andric
53294994d37SDimitry Andric // Create synthetic tail call frames between the previous frame and the
53394994d37SDimitry Andric // newly-found frame. The new frame's index may change after this call,
53494994d37SDimitry Andric // although its concrete index will stay the same.
53594994d37SDimitry Andric SynthesizeTailCallFrames(*unwind_frame_sp.get());
53694994d37SDimitry Andric
537f034231aSEd Maste m_frames.push_back(unwind_frame_sp);
538f034231aSEd Maste }
539f034231aSEd Maste
5405e95aa85SEd Maste assert(unwind_frame_sp);
54114f1b3e8SDimitry Andric SymbolContext unwind_sc = unwind_frame_sp->GetSymbolContext(
54214f1b3e8SDimitry Andric eSymbolContextBlock | eSymbolContextFunction);
543f034231aSEd Maste Block *unwind_block = unwind_sc.block;
544f3fbd1c0SDimitry Andric TargetSP target_sp = m_thread.CalculateTarget();
545344a3780SDimitry Andric if (unwind_block) {
546344a3780SDimitry Andric Address curr_frame_address(
547344a3780SDimitry Andric unwind_frame_sp->GetFrameCodeAddressForSymbolication());
548f034231aSEd Maste
549f034231aSEd Maste SymbolContext next_frame_sc;
550f034231aSEd Maste Address next_frame_address;
551f034231aSEd Maste
55214f1b3e8SDimitry Andric while (unwind_sc.GetParentOfInlinedScope(
55314f1b3e8SDimitry Andric curr_frame_address, next_frame_sc, next_frame_address)) {
554f3fbd1c0SDimitry Andric next_frame_sc.line_entry.ApplyFileMappings(target_sp);
555ead24645SDimitry Andric behaves_like_zeroth_frame = false;
556ead24645SDimitry Andric StackFrameSP frame_sp(new StackFrame(
557ead24645SDimitry Andric m_thread.shared_from_this(), m_frames.size(), idx,
558ead24645SDimitry Andric unwind_frame_sp->GetRegisterContextSP(), cfa, next_frame_address,
559ead24645SDimitry Andric behaves_like_zeroth_frame, &next_frame_sc));
560f034231aSEd Maste
561f034231aSEd Maste m_frames.push_back(frame_sp);
562f034231aSEd Maste unwind_sc = next_frame_sc;
563f034231aSEd Maste curr_frame_address = next_frame_address;
564f034231aSEd Maste }
565f034231aSEd Maste }
566f034231aSEd Maste } while (m_frames.size() - 1 < end_idx);
567f034231aSEd Maste
568f034231aSEd Maste // Don't try to merge till you've calculated all the frames in this stack.
56914f1b3e8SDimitry Andric if (GetAllFramesFetched() && m_prev_frames_sp) {
570f034231aSEd Maste StackFrameList *prev_frames = m_prev_frames_sp.get();
571f034231aSEd Maste StackFrameList *curr_frames = this;
572f034231aSEd Maste
573f034231aSEd Maste #if defined(DEBUG_STACK_FRAMES)
574f034231aSEd Maste s.PutCString("\nprev_frames:\n");
575f034231aSEd Maste prev_frames->Dump(&s);
576f034231aSEd Maste s.PutCString("\ncurr_frames:\n");
577f034231aSEd Maste curr_frames->Dump(&s);
578f034231aSEd Maste s.EOL();
579f034231aSEd Maste #endif
580f034231aSEd Maste size_t curr_frame_num, prev_frame_num;
581f034231aSEd Maste
58214f1b3e8SDimitry Andric for (curr_frame_num = curr_frames->m_frames.size(),
58314f1b3e8SDimitry Andric prev_frame_num = prev_frames->m_frames.size();
584f034231aSEd Maste curr_frame_num > 0 && prev_frame_num > 0;
58514f1b3e8SDimitry Andric --curr_frame_num, --prev_frame_num) {
586f034231aSEd Maste const size_t curr_frame_idx = curr_frame_num - 1;
587f034231aSEd Maste const size_t prev_frame_idx = prev_frame_num - 1;
588f034231aSEd Maste StackFrameSP curr_frame_sp(curr_frames->m_frames[curr_frame_idx]);
589f034231aSEd Maste StackFrameSP prev_frame_sp(prev_frames->m_frames[prev_frame_idx]);
590f034231aSEd Maste
591f034231aSEd Maste #if defined(DEBUG_STACK_FRAMES)
592f034231aSEd Maste s.Printf("\n\nCurr frame #%u ", curr_frame_idx);
593f034231aSEd Maste if (curr_frame_sp)
594f034231aSEd Maste curr_frame_sp->Dump(&s, true, false);
595f034231aSEd Maste else
596f034231aSEd Maste s.PutCString("NULL");
597f034231aSEd Maste s.Printf("\nPrev frame #%u ", prev_frame_idx);
598f034231aSEd Maste if (prev_frame_sp)
599f034231aSEd Maste prev_frame_sp->Dump(&s, true, false);
600f034231aSEd Maste else
601f034231aSEd Maste s.PutCString("NULL");
602f034231aSEd Maste #endif
603f034231aSEd Maste
604f034231aSEd Maste StackFrame *curr_frame = curr_frame_sp.get();
605f034231aSEd Maste StackFrame *prev_frame = prev_frame_sp.get();
606f034231aSEd Maste
607f3fbd1c0SDimitry Andric if (curr_frame == nullptr || prev_frame == nullptr)
608f034231aSEd Maste break;
609f034231aSEd Maste
61094994d37SDimitry Andric // Check the stack ID to make sure they are equal.
611f034231aSEd Maste if (curr_frame->GetStackID() != prev_frame->GetStackID())
612f034231aSEd Maste break;
613f034231aSEd Maste
614f034231aSEd Maste prev_frame->UpdatePreviousFrameFromCurrentFrame(*curr_frame);
615f73363f1SDimitry Andric // Now copy the fixed up previous frame into the current frames so the
61694994d37SDimitry Andric // pointer doesn't change.
617f034231aSEd Maste m_frames[curr_frame_idx] = prev_frame_sp;
618f034231aSEd Maste
619f034231aSEd Maste #if defined(DEBUG_STACK_FRAMES)
620f034231aSEd Maste s.Printf("\n Copying previous frame to current frame");
621f034231aSEd Maste #endif
622f034231aSEd Maste }
62394994d37SDimitry Andric // We are done with the old stack frame list, we can release it now.
624f034231aSEd Maste m_prev_frames_sp.reset();
625f034231aSEd Maste }
626f034231aSEd Maste
627f034231aSEd Maste #if defined(DEBUG_STACK_FRAMES)
628f034231aSEd Maste s.PutCString("\n\nNew frames:\n");
629f034231aSEd Maste Dump(&s);
630f034231aSEd Maste s.EOL();
631f034231aSEd Maste #endif
6327fa27ce4SDimitry Andric // Don't report interrupted if we happen to have gotten all the frames:
6337fa27ce4SDimitry Andric if (!GetAllFramesFetched())
6347fa27ce4SDimitry Andric return was_interrupted;
6357fa27ce4SDimitry Andric return false;
63686758c71SEd Maste }
637f034231aSEd Maste
GetNumFrames(bool can_create)63814f1b3e8SDimitry Andric uint32_t StackFrameList::GetNumFrames(bool can_create) {
639f3fbd1c0SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_mutex);
640f034231aSEd Maste
6417fa27ce4SDimitry Andric if (can_create) {
6427fa27ce4SDimitry Andric // Don't allow interrupt or we might not return the correct count
6437fa27ce4SDimitry Andric GetFramesUpTo(UINT32_MAX, DoNotAllowInterruption);
6447fa27ce4SDimitry Andric }
64594994d37SDimitry Andric return GetVisibleStackFrameIndex(m_frames.size());
646f034231aSEd Maste }
647f034231aSEd Maste
Dump(Stream * s)64814f1b3e8SDimitry Andric void StackFrameList::Dump(Stream *s) {
649f3fbd1c0SDimitry Andric if (s == nullptr)
650f034231aSEd Maste return;
651f3fbd1c0SDimitry Andric
652f3fbd1c0SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_mutex);
653f034231aSEd Maste
654f034231aSEd Maste const_iterator pos, begin = m_frames.begin(), end = m_frames.end();
65514f1b3e8SDimitry Andric for (pos = begin; pos != end; ++pos) {
656f034231aSEd Maste StackFrame *frame = (*pos).get();
6570cac4ca3SEd Maste s->Printf("%p: ", static_cast<void *>(frame));
65814f1b3e8SDimitry Andric if (frame) {
659f034231aSEd Maste frame->GetStackID().Dump(s);
660f034231aSEd Maste frame->DumpUsingSettingsFormat(s);
66114f1b3e8SDimitry Andric } else
662f034231aSEd Maste s->Printf("frame #%u", (uint32_t)std::distance(begin, pos));
663f034231aSEd Maste s->EOL();
664f034231aSEd Maste }
665f034231aSEd Maste s->EOL();
666f034231aSEd Maste }
667f034231aSEd Maste
GetFrameAtIndex(uint32_t idx)66814f1b3e8SDimitry Andric StackFrameSP StackFrameList::GetFrameAtIndex(uint32_t idx) {
669f034231aSEd Maste StackFrameSP frame_sp;
670f3fbd1c0SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_mutex);
671f034231aSEd Maste uint32_t original_idx = idx;
672f034231aSEd Maste
673f034231aSEd Maste uint32_t inlined_depth = GetCurrentInlinedDepth();
674f034231aSEd Maste if (inlined_depth != UINT32_MAX)
675f034231aSEd Maste idx += inlined_depth;
676f034231aSEd Maste
677f034231aSEd Maste if (idx < m_frames.size())
678f034231aSEd Maste frame_sp = m_frames[idx];
679f034231aSEd Maste
680f034231aSEd Maste if (frame_sp)
681f034231aSEd Maste return frame_sp;
682f034231aSEd Maste
683f73363f1SDimitry Andric // GetFramesUpTo will fill m_frames with as many frames as you asked for, if
684f73363f1SDimitry Andric // there are that many. If there weren't then you asked for too many frames.
6857fa27ce4SDimitry Andric // GetFramesUpTo returns true if interrupted:
6867fa27ce4SDimitry Andric if (GetFramesUpTo(idx)) {
6877fa27ce4SDimitry Andric Log *log = GetLog(LLDBLog::Thread);
6887fa27ce4SDimitry Andric LLDB_LOG(log, "GetFrameAtIndex was interrupted");
6897fa27ce4SDimitry Andric return {};
6907fa27ce4SDimitry Andric }
6917fa27ce4SDimitry Andric
69214f1b3e8SDimitry Andric if (idx < m_frames.size()) {
69314f1b3e8SDimitry Andric if (m_show_inlined_frames) {
69414f1b3e8SDimitry Andric // When inline frames are enabled we actually create all the frames in
69514f1b3e8SDimitry Andric // GetFramesUpTo.
696f034231aSEd Maste frame_sp = m_frames[idx];
69714f1b3e8SDimitry Andric } else {
698f034231aSEd Maste addr_t pc, cfa;
699ead24645SDimitry Andric bool behaves_like_zeroth_frame = (idx == 0);
700cfca06d7SDimitry Andric if (m_thread.GetUnwinder().GetFrameInfoAtIndex(
701cfca06d7SDimitry Andric idx, cfa, pc, behaves_like_zeroth_frame)) {
702f21a844fSEd Maste const bool cfa_is_valid = true;
7035f29bb8aSDimitry Andric frame_sp = std::make_shared<StackFrame>(
7045f29bb8aSDimitry Andric m_thread.shared_from_this(), idx, idx, cfa, cfa_is_valid, pc,
705ead24645SDimitry Andric StackFrame::Kind::Regular, behaves_like_zeroth_frame, nullptr);
706f034231aSEd Maste
70714f1b3e8SDimitry Andric Function *function =
70814f1b3e8SDimitry Andric frame_sp->GetSymbolContext(eSymbolContextFunction).function;
70914f1b3e8SDimitry Andric if (function) {
710f73363f1SDimitry Andric // When we aren't showing inline functions we always use the top
711f73363f1SDimitry Andric // most function block as the scope.
712f034231aSEd Maste frame_sp->SetSymbolContextScope(&function->GetBlock(false));
71314f1b3e8SDimitry Andric } else {
71414f1b3e8SDimitry Andric // Set the symbol scope from the symbol regardless if it is nullptr
71514f1b3e8SDimitry Andric // or valid.
71614f1b3e8SDimitry Andric frame_sp->SetSymbolContextScope(
71714f1b3e8SDimitry Andric frame_sp->GetSymbolContext(eSymbolContextSymbol).symbol);
718f034231aSEd Maste }
719f034231aSEd Maste SetFrameAtIndex(idx, frame_sp);
720f034231aSEd Maste }
721f034231aSEd Maste }
72214f1b3e8SDimitry Andric } else if (original_idx == 0) {
72314f1b3e8SDimitry Andric // There should ALWAYS be a frame at index 0. If something went wrong with
724f73363f1SDimitry Andric // the CurrentInlinedDepth such that there weren't as many frames as we
725f73363f1SDimitry Andric // thought taking that into account, then reset the current inlined depth
726f034231aSEd Maste // and return the real zeroth frame.
72714f1b3e8SDimitry Andric if (m_frames.empty()) {
72814f1b3e8SDimitry Andric // Why do we have a thread with zero frames, that should not ever
72914f1b3e8SDimitry Andric // happen...
730a4092fcbSDimitry Andric assert(!m_thread.IsValid() && "A valid thread has no frames.");
73114f1b3e8SDimitry Andric } else {
732f3fbd1c0SDimitry Andric ResetCurrentInlinedDepth();
733f3fbd1c0SDimitry Andric frame_sp = m_frames[original_idx];
734f034231aSEd Maste }
735f034231aSEd Maste }
736f034231aSEd Maste
737f034231aSEd Maste return frame_sp;
738f034231aSEd Maste }
739f034231aSEd Maste
740f034231aSEd Maste StackFrameSP
GetFrameWithConcreteFrameIndex(uint32_t unwind_idx)74114f1b3e8SDimitry Andric StackFrameList::GetFrameWithConcreteFrameIndex(uint32_t unwind_idx) {
742f034231aSEd Maste // First try assuming the unwind index is the same as the frame index. The
743f73363f1SDimitry Andric // unwind index is always greater than or equal to the frame index, so it is
744f73363f1SDimitry Andric // a good place to start. If we have inlined frames we might have 5 concrete
745f73363f1SDimitry Andric // frames (frame unwind indexes go from 0-4), but we might have 15 frames
746f73363f1SDimitry Andric // after we make all the inlined frames. Most of the time the unwind frame
747f73363f1SDimitry Andric // index (or the concrete frame index) is the same as the frame index.
748f034231aSEd Maste uint32_t frame_idx = unwind_idx;
749f034231aSEd Maste StackFrameSP frame_sp(GetFrameAtIndex(frame_idx));
75014f1b3e8SDimitry Andric while (frame_sp) {
751f034231aSEd Maste if (frame_sp->GetFrameIndex() == unwind_idx)
752f034231aSEd Maste break;
753f034231aSEd Maste frame_sp = GetFrameAtIndex(++frame_idx);
754f034231aSEd Maste }
755f034231aSEd Maste return frame_sp;
756f034231aSEd Maste }
757f034231aSEd Maste
CompareStackID(const StackFrameSP & stack_sp,const StackID & stack_id)75814f1b3e8SDimitry Andric static bool CompareStackID(const StackFrameSP &stack_sp,
75914f1b3e8SDimitry Andric const StackID &stack_id) {
760f034231aSEd Maste return stack_sp->GetStackID() < stack_id;
761f034231aSEd Maste }
762f034231aSEd Maste
GetFrameWithStackID(const StackID & stack_id)76314f1b3e8SDimitry Andric StackFrameSP StackFrameList::GetFrameWithStackID(const StackID &stack_id) {
764f034231aSEd Maste StackFrameSP frame_sp;
765f034231aSEd Maste
76614f1b3e8SDimitry Andric if (stack_id.IsValid()) {
767f3fbd1c0SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_mutex);
768f034231aSEd Maste uint32_t frame_idx = 0;
769f034231aSEd Maste // Do a binary search in case the stack frame is already in our cache
770f034231aSEd Maste collection::const_iterator begin = m_frames.begin();
771f034231aSEd Maste collection::const_iterator end = m_frames.end();
77214f1b3e8SDimitry Andric if (begin != end) {
77314f1b3e8SDimitry Andric collection::const_iterator pos =
77414f1b3e8SDimitry Andric std::lower_bound(begin, end, stack_id, CompareStackID);
77514f1b3e8SDimitry Andric if (pos != end) {
776205afe67SEd Maste if ((*pos)->GetStackID() == stack_id)
777f034231aSEd Maste return *pos;
778205afe67SEd Maste }
779f034231aSEd Maste }
78014f1b3e8SDimitry Andric do {
781f034231aSEd Maste frame_sp = GetFrameAtIndex(frame_idx);
782f034231aSEd Maste if (frame_sp && frame_sp->GetStackID() == stack_id)
783f034231aSEd Maste break;
784f034231aSEd Maste frame_idx++;
78514f1b3e8SDimitry Andric } while (frame_sp);
786f034231aSEd Maste }
787f034231aSEd Maste return frame_sp;
788f034231aSEd Maste }
789f034231aSEd Maste
SetFrameAtIndex(uint32_t idx,StackFrameSP & frame_sp)79014f1b3e8SDimitry Andric bool StackFrameList::SetFrameAtIndex(uint32_t idx, StackFrameSP &frame_sp) {
791f034231aSEd Maste if (idx >= m_frames.size())
792f034231aSEd Maste m_frames.resize(idx + 1);
793f034231aSEd Maste // Make sure allocation succeeded by checking bounds again
79414f1b3e8SDimitry Andric if (idx < m_frames.size()) {
795f034231aSEd Maste m_frames[idx] = frame_sp;
796f034231aSEd Maste return true;
797f034231aSEd Maste }
798f034231aSEd Maste return false; // resize failed, out of memory?
799f034231aSEd Maste }
800f034231aSEd Maste
SelectMostRelevantFrame()8017fa27ce4SDimitry Andric void StackFrameList::SelectMostRelevantFrame() {
8027fa27ce4SDimitry Andric // Don't call into the frame recognizers on the private state thread as
8037fa27ce4SDimitry Andric // they can cause code to run in the target, and that can cause deadlocks
8047fa27ce4SDimitry Andric // when fetching stop events for the expression.
8057fa27ce4SDimitry Andric if (m_thread.GetProcess()->CurrentThreadIsPrivateStateThread())
8067fa27ce4SDimitry Andric return;
8077fa27ce4SDimitry Andric
8087fa27ce4SDimitry Andric Log *log = GetLog(LLDBLog::Thread);
8097fa27ce4SDimitry Andric
8107fa27ce4SDimitry Andric // Only the top frame should be recognized.
8117fa27ce4SDimitry Andric StackFrameSP frame_sp = GetFrameAtIndex(0);
8127fa27ce4SDimitry Andric if (!frame_sp) {
8137fa27ce4SDimitry Andric LLDB_LOG(log, "Failed to construct Frame #0");
8147fa27ce4SDimitry Andric return;
8157fa27ce4SDimitry Andric }
8167fa27ce4SDimitry Andric
8177fa27ce4SDimitry Andric RecognizedStackFrameSP recognized_frame_sp = frame_sp->GetRecognizedFrame();
8187fa27ce4SDimitry Andric
8197fa27ce4SDimitry Andric if (!recognized_frame_sp) {
8207fa27ce4SDimitry Andric LLDB_LOG(log, "Frame #0 not recognized");
8217fa27ce4SDimitry Andric return;
8227fa27ce4SDimitry Andric }
8237fa27ce4SDimitry Andric
8247fa27ce4SDimitry Andric if (StackFrameSP most_relevant_frame_sp =
8257fa27ce4SDimitry Andric recognized_frame_sp->GetMostRelevantFrame()) {
8267fa27ce4SDimitry Andric LLDB_LOG(log, "Found most relevant frame at index {0}",
8277fa27ce4SDimitry Andric most_relevant_frame_sp->GetFrameIndex());
8287fa27ce4SDimitry Andric SetSelectedFrame(most_relevant_frame_sp.get());
8297fa27ce4SDimitry Andric } else {
8307fa27ce4SDimitry Andric LLDB_LOG(log, "No relevant frame!");
8317fa27ce4SDimitry Andric }
8327fa27ce4SDimitry Andric }
8337fa27ce4SDimitry Andric
GetSelectedFrameIndex(SelectMostRelevant select_most_relevant)8347fa27ce4SDimitry Andric uint32_t StackFrameList::GetSelectedFrameIndex(
8357fa27ce4SDimitry Andric SelectMostRelevant select_most_relevant) {
836f3fbd1c0SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_mutex);
8377fa27ce4SDimitry Andric if (!m_selected_frame_idx && select_most_relevant)
8387fa27ce4SDimitry Andric SelectMostRelevantFrame();
8397fa27ce4SDimitry Andric if (!m_selected_frame_idx) {
8407fa27ce4SDimitry Andric // If we aren't selecting the most relevant frame, and the selected frame
8417fa27ce4SDimitry Andric // isn't set, then don't force a selection here, just return 0.
8427fa27ce4SDimitry Andric if (!select_most_relevant)
8437fa27ce4SDimitry Andric return 0;
8447fa27ce4SDimitry Andric m_selected_frame_idx = 0;
8457fa27ce4SDimitry Andric }
8467fa27ce4SDimitry Andric return *m_selected_frame_idx;
847f034231aSEd Maste }
848f034231aSEd Maste
SetSelectedFrame(lldb_private::StackFrame * frame)84914f1b3e8SDimitry Andric uint32_t StackFrameList::SetSelectedFrame(lldb_private::StackFrame *frame) {
850f3fbd1c0SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_mutex);
851f034231aSEd Maste const_iterator pos;
852f034231aSEd Maste const_iterator begin = m_frames.begin();
853f034231aSEd Maste const_iterator end = m_frames.end();
854f034231aSEd Maste m_selected_frame_idx = 0;
8557fa27ce4SDimitry Andric
85614f1b3e8SDimitry Andric for (pos = begin; pos != end; ++pos) {
85714f1b3e8SDimitry Andric if (pos->get() == frame) {
858f034231aSEd Maste m_selected_frame_idx = std::distance(begin, pos);
859f034231aSEd Maste uint32_t inlined_depth = GetCurrentInlinedDepth();
860f034231aSEd Maste if (inlined_depth != UINT32_MAX)
8617fa27ce4SDimitry Andric m_selected_frame_idx = *m_selected_frame_idx - inlined_depth;
862f034231aSEd Maste break;
863f034231aSEd Maste }
864f034231aSEd Maste }
8657fa27ce4SDimitry Andric
866f034231aSEd Maste SetDefaultFileAndLineToSelectedFrame();
8677fa27ce4SDimitry Andric return *m_selected_frame_idx;
868f034231aSEd Maste }
869f034231aSEd Maste
SetSelectedFrameByIndex(uint32_t idx)87014f1b3e8SDimitry Andric bool StackFrameList::SetSelectedFrameByIndex(uint32_t idx) {
871f3fbd1c0SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_mutex);
872f034231aSEd Maste StackFrameSP frame_sp(GetFrameAtIndex(idx));
87314f1b3e8SDimitry Andric if (frame_sp) {
874f034231aSEd Maste SetSelectedFrame(frame_sp.get());
875f034231aSEd Maste return true;
87614f1b3e8SDimitry Andric } else
877f034231aSEd Maste return false;
878f034231aSEd Maste }
879f034231aSEd Maste
SetDefaultFileAndLineToSelectedFrame()88014f1b3e8SDimitry Andric void StackFrameList::SetDefaultFileAndLineToSelectedFrame() {
88114f1b3e8SDimitry Andric if (m_thread.GetID() ==
88214f1b3e8SDimitry Andric m_thread.GetProcess()->GetThreadList().GetSelectedThread()->GetID()) {
8837fa27ce4SDimitry Andric StackFrameSP frame_sp(
8847fa27ce4SDimitry Andric GetFrameAtIndex(GetSelectedFrameIndex(DoNoSelectMostRelevantFrame)));
88514f1b3e8SDimitry Andric if (frame_sp) {
886f034231aSEd Maste SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextLineEntry);
887ac9a064cSDimitry Andric if (sc.line_entry.GetFile())
88814f1b3e8SDimitry Andric m_thread.CalculateTarget()->GetSourceManager().SetDefaultFileAndLine(
889ac9a064cSDimitry Andric sc.line_entry.GetFile(), sc.line_entry.line);
890f034231aSEd Maste }
891f034231aSEd Maste }
892f034231aSEd Maste }
893f034231aSEd Maste
894f034231aSEd Maste // The thread has been run, reset the number stack frames to zero so we can
895f034231aSEd Maste // determine how many frames we have lazily.
8967fa27ce4SDimitry Andric // Note, we don't actually re-use StackFrameLists, we always make a new
8977fa27ce4SDimitry Andric // StackFrameList every time we stop, and then copy frame information frame
8987fa27ce4SDimitry Andric // by frame from the old to the new StackFrameList. So the comment above,
8997fa27ce4SDimitry Andric // does not describe how StackFrameLists are currently used.
9007fa27ce4SDimitry Andric // Clear is currently only used to clear the list in the destructor.
Clear()90114f1b3e8SDimitry Andric void StackFrameList::Clear() {
902f3fbd1c0SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_mutex);
903f034231aSEd Maste m_frames.clear();
904f034231aSEd Maste m_concrete_frames_fetched = 0;
9057fa27ce4SDimitry Andric m_selected_frame_idx.reset();
906f034231aSEd Maste }
907f034231aSEd Maste
908f034231aSEd Maste lldb::StackFrameSP
GetStackFrameSPForStackFramePtr(StackFrame * stack_frame_ptr)90914f1b3e8SDimitry Andric StackFrameList::GetStackFrameSPForStackFramePtr(StackFrame *stack_frame_ptr) {
910f034231aSEd Maste const_iterator pos;
911f034231aSEd Maste const_iterator begin = m_frames.begin();
912f034231aSEd Maste const_iterator end = m_frames.end();
913f034231aSEd Maste lldb::StackFrameSP ret_sp;
914f034231aSEd Maste
91514f1b3e8SDimitry Andric for (pos = begin; pos != end; ++pos) {
91614f1b3e8SDimitry Andric if (pos->get() == stack_frame_ptr) {
917f034231aSEd Maste ret_sp = (*pos);
918f034231aSEd Maste break;
919f034231aSEd Maste }
920f034231aSEd Maste }
921f034231aSEd Maste return ret_sp;
922f034231aSEd Maste }
923f034231aSEd Maste
GetStatus(Stream & strm,uint32_t first_frame,uint32_t num_frames,bool show_frame_info,uint32_t num_frames_with_source,bool show_unique,const char * selected_frame_marker)92414f1b3e8SDimitry Andric size_t StackFrameList::GetStatus(Stream &strm, uint32_t first_frame,
92514f1b3e8SDimitry Andric uint32_t num_frames, bool show_frame_info,
926f21a844fSEd Maste uint32_t num_frames_with_source,
9274befb1f9SDimitry Andric bool show_unique,
92814f1b3e8SDimitry Andric const char *selected_frame_marker) {
929f034231aSEd Maste size_t num_frames_displayed = 0;
930f034231aSEd Maste
931f034231aSEd Maste if (num_frames == 0)
932f034231aSEd Maste return 0;
933f034231aSEd Maste
934f034231aSEd Maste StackFrameSP frame_sp;
935f034231aSEd Maste uint32_t frame_idx = 0;
936f034231aSEd Maste uint32_t last_frame;
937f034231aSEd Maste
938f034231aSEd Maste // Don't let the last frame wrap around...
939f034231aSEd Maste if (num_frames == UINT32_MAX)
940f034231aSEd Maste last_frame = UINT32_MAX;
941f034231aSEd Maste else
942f034231aSEd Maste last_frame = first_frame + num_frames;
943f034231aSEd Maste
9447fa27ce4SDimitry Andric StackFrameSP selected_frame_sp =
9457fa27ce4SDimitry Andric m_thread.GetSelectedFrame(DoNoSelectMostRelevantFrame);
946f3fbd1c0SDimitry Andric const char *unselected_marker = nullptr;
947f21a844fSEd Maste std::string buffer;
94814f1b3e8SDimitry Andric if (selected_frame_marker) {
949f21a844fSEd Maste size_t len = strlen(selected_frame_marker);
950f21a844fSEd Maste buffer.insert(buffer.begin(), len, ' ');
951f21a844fSEd Maste unselected_marker = buffer.c_str();
952f21a844fSEd Maste }
953f3fbd1c0SDimitry Andric const char *marker = nullptr;
954f21a844fSEd Maste
95514f1b3e8SDimitry Andric for (frame_idx = first_frame; frame_idx < last_frame; ++frame_idx) {
956f034231aSEd Maste frame_sp = GetFrameAtIndex(frame_idx);
957f3fbd1c0SDimitry Andric if (!frame_sp)
958f034231aSEd Maste break;
959f034231aSEd Maste
96014f1b3e8SDimitry Andric if (selected_frame_marker != nullptr) {
961f21a844fSEd Maste if (frame_sp == selected_frame_sp)
962f21a844fSEd Maste marker = selected_frame_marker;
963f21a844fSEd Maste else
964f21a844fSEd Maste marker = unselected_marker;
965f21a844fSEd Maste }
9667fa27ce4SDimitry Andric // Check for interruption here. If we're fetching arguments, this loop
9677fa27ce4SDimitry Andric // can go slowly:
9687fa27ce4SDimitry Andric Debugger &dbg = m_thread.GetProcess()->GetTarget().GetDebugger();
969ac9a064cSDimitry Andric if (INTERRUPT_REQUESTED(
970ac9a064cSDimitry Andric dbg, "Interrupted dumping stack for thread {0:x} with {1} shown.",
9717fa27ce4SDimitry Andric m_thread.GetID(), num_frames_displayed))
9727fa27ce4SDimitry Andric break;
9737fa27ce4SDimitry Andric
974f21a844fSEd Maste
97514f1b3e8SDimitry Andric if (!frame_sp->GetStatus(strm, show_frame_info,
97614f1b3e8SDimitry Andric num_frames_with_source > (first_frame - frame_idx),
9774befb1f9SDimitry Andric show_unique, marker))
978f034231aSEd Maste break;
979f034231aSEd Maste ++num_frames_displayed;
980f034231aSEd Maste }
981f034231aSEd Maste
982f034231aSEd Maste strm.IndentLess();
983f034231aSEd Maste return num_frames_displayed;
984f034231aSEd Maste }
985