28#include "llvm/ADT/SmallPtrSet.h"
40 bool show_inline_frames)
76 "GetCurrentInlinedDepth: invalidating current inlined depth.\n");
93 auto inline_depth = stop_info_sp->GetSuggestedStackFrameIndex(inlined);
103 "ResetCurrentInlinedDepth: setting inlined "
104 "depth: %d 0x%" PRIx64
".\n",
113 "ResetCurrentInlinedDepth: Invalidating current inlined depth.\n");
121 if (current_inlined_depth > 0) {
141 std::shared_lock<std::shared_mutex> guard(
m_list_mutex);
164 LLDB_LOG(log,
"Finding frames between {0} and {1}, retn-pc={2:x}",
170 LLDB_LOG(log,
"FindInterveningFrames: found call with retn-PC = {0:x}",
171 edge->GetReturnPCAddress(begin, target));
174 LLDB_LOG(log,
"No call edge outgoing from {0} with retn-PC == {1:x}",
182 LLDB_LOG(log,
"Could not resolve callee");
185 if (first_callee == &end) {
186 LLDB_LOG(log,
"Not searching further, first callee is {0} (retn-PC: {1:x})",
197 llvm::SmallPtrSet<Function *, 2> visited_nodes = {};
198 bool ambiguous =
false;
206 : end(end), images(images), target(target), context(context) {}
210 dfs(first_edge, first_callee);
212 path = std::move(solution_path);
217 if (&callee == end) {
218 if (solution_path.empty())
219 solution_path = active_path;
229 if (!visited_nodes.insert(&callee).second) {
237 Function *next_callee = edge->GetCallee(images, context);
241 std::tie(active_path.back().address_type, active_path.back().address) =
242 edge->GetCallerAddress(callee, target);
244 dfs(*edge, *next_callee);
248 active_path.pop_back();
252 DFS(&end, images, target, exe_ctx).search(*first_edge, *first_callee, path);
281 if (!next_reg_ctx_sp)
293 LLDB_LOG(log,
"SynthesizeTailCallFrames: can't find previous function");
299 LLDB_LOG(log,
"SynthesizeTailCallFrames: can't find next function");
306 addr_t return_pc = next_reg_ctx_sp->GetPC();
307 Target &target = *target_sp.get();
315 for (
auto calleeInfo : llvm::reverse(path)) {
317 uint32_t frame_idx =
m_frames.size();
320 bool cfa_is_valid =
false;
324 const bool artificial =
true;
325 const bool behaves_like_zeroth_frame =
329 auto synth_frame = std::make_shared<StackFrame>(
330 m_thread.shared_from_this(), frame_idx, concrete_frame_idx, cfa,
332 behaves_like_zeroth_frame, &sc);
346 std::unique_lock<std::shared_mutex> guard(
m_list_mutex);
353 bool was_interrupted =
false;
370#if defined(DEBUG_STACK_FRAMES)
371 s.PutCString(
"\n\nNew frames:\n");
375 return was_interrupted;
379 assert(
m_thread.IsValid() &&
"Expected valid thread");
380 assert(
m_frames.size() <= end_idx &&
"Expected there to be frames to fill");
388 if (num_frames <= end_idx + 1) {
401 bool was_interrupted =
false;
403#if defined(DEBUG_STACK_FRAMES)
412 uint32_t inlined_depth = 0;
417 end_idx += inlined_depth;
427 bool behaves_like_zeroth_frame = (idx == 0);
436 idx, cfa,
pc, behaves_like_zeroth_frame);
441 cfa = reg_ctx_sp->GetSP();
442 pc = reg_ctx_sp->GetPC();
445 unwind_frame_sp = std::make_shared<StackFrame>(
447 cfa,
pc, behaves_like_zeroth_frame,
nullptr);
448 m_frames.push_back(unwind_frame_sp);
452 cfa = unwind_frame_sp->m_id.GetCallFrameAddressWithoutMetadata();
457 if (allow_interrupt &&
460 was_interrupted =
true;
471 const bool cfa_is_valid =
true;
472 unwind_frame_sp = std::make_shared<StackFrame>(
482 m_frames.push_back(unwind_frame_sp);
485 assert(unwind_frame_sp);
487 eSymbolContextBlock | eSymbolContextFunction);
492 unwind_frame_sp->GetFrameCodeAddressForSymbolication());
498 curr_frame_address, next_frame_sc, next_frame_address)) {
500 behaves_like_zeroth_frame =
false;
503 unwind_frame_sp->GetRegisterContextSP(), cfa, next_frame_address,
504 behaves_like_zeroth_frame, &next_frame_sc));
507 unwind_sc = next_frame_sc;
508 curr_frame_address = next_frame_address;
511 }
while (
m_frames.size() - 1 < end_idx);
518#if defined(DEBUG_STACK_FRAMES)
520 prev_frames->
Dump(&s);
522 curr_frames->
Dump(&s);
525 size_t curr_frame_num, prev_frame_num;
527 for (curr_frame_num = curr_frames->
m_frames.size(),
528 prev_frame_num = prev_frames->
m_frames.size();
529 curr_frame_num > 0 && prev_frame_num > 0;
530 --curr_frame_num, --prev_frame_num) {
531 const size_t curr_frame_idx = curr_frame_num - 1;
532 const size_t prev_frame_idx = prev_frame_num - 1;
536#if defined(DEBUG_STACK_FRAMES)
537 s.
Printf(
"\n\nCurr frame #%u ", curr_frame_idx);
539 curr_frame_sp->Dump(&s,
true,
false);
542 s.
Printf(
"\nPrev frame #%u ", prev_frame_idx);
544 prev_frame_sp->Dump(&s,
true,
false);
552 if (curr_frame ==
nullptr || prev_frame ==
nullptr)
562 m_frames[curr_frame_idx] = prev_frame_sp;
564#if defined(DEBUG_STACK_FRAMES)
565 s.
Printf(
"\n Copying previous frame to current frame");
573 return was_interrupted;
584 std::shared_lock<std::shared_mutex> guard(
m_list_mutex);
594 std::shared_lock<std::shared_mutex> guard(
m_list_mutex);
597 for (pos = begin; pos != end; ++pos) {
599 s->
Printf(
"%p: ",
static_cast<void *
>(frame));
604 s->
Printf(
"frame #%u", (uint32_t)std::distance(begin, pos));
612 uint32_t original_idx = idx;
618 std::shared_lock<std::shared_mutex> guard(
m_list_mutex);
622 idx += inlined_depth;
636 LLDB_LOG(log,
"GetFrameAtIndex was interrupted");
641 std::shared_lock<std::shared_mutex> guard(
m_list_mutex);
644 }
else if (original_idx == 0) {
652 assert(!
m_thread.IsValid() &&
"A valid thread has no frames.");
671 uint32_t frame_idx = unwind_idx;
674 if (frame_sp->GetFrameIndex() == unwind_idx)
683 return stack_sp->GetStackID() < stack_id;
690 uint32_t frame_idx = 0;
694 std::shared_lock<std::shared_mutex> guard(
m_list_mutex);
696 collection::const_iterator pos =
698 if (pos !=
m_frames.end() && (*pos)->GetStackID() == stack_id)
704 if (frame_sp && frame_sp->GetStackID() == stack_id)
713 std::unique_lock<std::shared_mutex> guard(
m_list_mutex);
728 if (
m_thread.GetProcess()->CurrentThreadPosesAsPrivateStateThread())
736 LLDB_LOG(log,
"Failed to construct Frame #0");
742 if (recognized_frame_sp) {
744 recognized_frame_sp->GetMostRelevantFrame()) {
745 LLDB_LOG(log,
"Found most relevant frame at index {0}",
746 most_relevant_frame_sp->GetFrameIndex());
751 LLDB_LOG(log,
"Frame #0 not recognized");
756 uint32_t stack_idx = 0;
757 bool found_relevant =
false;
762 bool inlined =
false;
763 std::optional<uint32_t> stack_opt =
764 stop_info_sp->GetSuggestedStackFrameIndex(inlined);
766 stack_idx = *stack_opt;
767 found_relevant =
true;
773 LLDB_LOG(log,
"Stop info suggested relevant frame {0} but it didn't exist",
775 else if (found_relevant)
776 LLDB_LOG(log,
"Setting selected frame from stop info to {0}", stack_idx);
783 LLDB_LOG(log,
"No relevant frame!");
795 if (!select_most_relevant)
804 std::shared_lock<std::shared_mutex> guard(
m_list_mutex);
805 std::lock_guard<std::recursive_mutex> selected_frame_guard(
813 for (pos = begin; pos != end; ++pos) {
814 if (pos->get() == frame) {
837 m_thread.GetProcess()->GetThreadList().GetSelectedThread()->GetID()) {
841 SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextLineEntry);
843 m_thread.CalculateTarget()->GetSourceManager().SetDefaultFileAndLine(
857 std::unique_lock<std::shared_mutex> guard(
m_list_mutex);
860 std::lock_guard<std::recursive_mutex> selected_frame_guard(
867 std::shared_lock<std::shared_mutex> guard(
m_list_mutex);
873 for (pos = begin; pos != end; ++pos) {
874 if (pos->get() == stack_frame_ptr) {
883 uint32_t num_frames,
bool show_frame_info,
884 uint32_t num_frames_with_source,
885 bool show_unique,
bool show_hidden,
886 const char *selected_frame_marker) {
887 size_t num_frames_displayed = 0;
893 uint32_t frame_idx = 0;
900 last_frame = first_frame + num_frames;
904 const char *unselected_marker =
nullptr;
906 if (selected_frame_marker) {
907 size_t len = strlen(selected_frame_marker);
908 buffer.insert(buffer.begin(), len,
' ');
909 unselected_marker = buffer.c_str();
911 const char *marker =
nullptr;
912 for (frame_idx = first_frame; frame_idx < last_frame; ++frame_idx) {
917 if (selected_frame_marker !=
nullptr) {
918 if (frame_sp == selected_frame_sp)
919 marker = selected_frame_marker;
921 marker = unselected_marker;
925 if (!show_hidden && frame_sp != selected_frame_sp && frame_sp->IsHidden())
932 dbg,
"Interrupted dumping stack for thread {0:x} with {1} shown.",
933 m_thread.GetID(), num_frames_displayed))
937 if (!frame_sp->GetStatus(strm, show_frame_info,
938 num_frames_with_source > (first_frame - frame_idx),
939 show_unique, marker))
941 ++num_frames_displayed;
945 return num_frames_displayed;
#define INTERRUPT_REQUESTED(debugger,...)
This handy define will keep you from having to generate a report for the interruption by hand.
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
#define LLDB_LOGF(log,...)
static void FindInterveningFrames(Function &begin, Function &end, ExecutionContext &exe_ctx, Target &target, addr_t return_pc, CallSequence &path, ModuleList &images, Log *log)
Find the unique path through the call graph from begin (with return PC return_pc) to end.
static bool CompareStackID(const StackFrameSP &stack_sp, const StackID &stack_id)
std::vector< CallDescriptor > CallSequence
A section + offset based address class.
A class that describes a single lexical block.
Represent a call made within a Function.
virtual Function * GetCallee(ModuleList &images, ExecutionContext &exe_ctx)=0
Get the callee's definition.
A class to manage flag bits.
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
void SetFramePtr(StackFrame *frame)
Set accessor to set only the frame shared pointer from a frame pointer.
A class that describes a function.
CallEdge * GetCallEdgeForReturnAddress(lldb::addr_t return_pc, Target &target)
Get the outgoing call edge from this function which has the given return address return_pc,...
llvm::ArrayRef< std::unique_ptr< CallEdge > > GetCallEdges()
Get the outgoing call edges from this function, sorted by their return PC addresses (in increasing or...
void CalculateSymbolContext(SymbolContext *sc) override
Reconstruct the object's symbol context into sc.
llvm::ArrayRef< std::unique_ptr< CallEdge > > GetTailCallingEdges()
Get the outgoing tail-calling edges from this function.
ConstString GetDisplayName() const
A collection class for Module objects.
lldb::addr_t m_current_inlined_pc
The program counter value at the currently selected synthetic activation.
collection m_frames
A cache of frames.
lldb::StackFrameSP GetFrameWithStackID(const StackID &stack_id)
Retrieve the stack frame with the given ID stack_id.
lldb::StackFrameSP GetFrameWithConcreteFrameIndex(uint32_t unwind_idx)
Get the first concrete frame with index greater than or equal to idx.
bool GetFramesUpTo(uint32_t end_idx, InterruptionControl allow_interrupt)
Ensures that frames up to (and including) end_idx are realized in the StackFrameList.
void ClearSelectedFrameIndex()
Resets the selected frame index of this object.
void ResetCurrentInlinedDepth()
std::recursive_mutex m_selected_frame_mutex
Protect access to m_selected_frame_idx.
void Clear()
Clear the cache of frames.
lldb::StackFrameSP GetFrameAtIndex(uint32_t idx)
Get the frame at index idx. Invisible frames cannot be indexed.
bool GetAllFramesFetched() const
std::optional< uint32_t > m_selected_frame_idx
The currently selected frame.
lldb::StackFrameListSP m_prev_frames_sp
The old stack frame list.
uint32_t m_concrete_frames_fetched
The number of concrete frames fetched while filling the frame list.
size_t GetStatus(Stream &strm, uint32_t first_frame, uint32_t num_frames, bool show_frame_info, uint32_t num_frames_with_source, bool show_unique=false, bool show_hidden=false, const char *frame_marker=nullptr)
void FetchOnlyConcreteFramesUpTo(uint32_t end_idx)
Not currently interruptible so returns void.
Thread & m_thread
The thread this frame list describes.
void SelectMostRelevantFrame()
Calls into the stack frame recognizers and stop info to set the most relevant frame.
const bool m_show_inlined_frames
Whether or not to show synthetic (inline) frames. Immutable.
std::mutex m_inlined_depth_mutex
bool SetFrameAtIndex(uint32_t idx, lldb::StackFrameSP &frame_sp)
Use this API to build a stack frame list (used for scripted threads, for instance....
uint32_t m_current_inlined_depth
The number of synthetic function activations (invisible frames) expanded from the concrete frame #0 a...
void SetDefaultFileAndLineToSelectedFrame()
If the currently selected frame comes from the currently selected thread, point the default file and ...
uint32_t GetVisibleStackFrameIndex(uint32_t idx)
If the current inline depth (i.e the number of invisible frames) is valid, subtract it from idx.
bool DecrementCurrentInlinedDepth()
uint32_t GetNumFrames(bool can_create=true)
Get the number of visible frames.
bool FetchFramesUpTo(uint32_t end_idx, InterruptionControl allow_interrupt)
These two Fetch frames APIs and SynthesizeTailCallFrames are called in GetFramesUpTo,...
collection::const_iterator const_iterator
void SynthesizeTailCallFrames(StackFrame &next_frame)
Given that next_frame will be appended to the frame list, synthesize tail call frames between the cur...
void SetAllFramesFetched()
StackFrameList(Thread &thread, const lldb::StackFrameListSP &prev_frames_sp, bool show_inline_frames)
void SetCurrentInlinedDepth(uint32_t new_depth)
uint32_t GetSelectedFrameIndex(SelectMostRelevant select_most_relevant_frame)
Get the currently selected frame index.
std::shared_mutex m_list_mutex
A mutex for this frame list.
uint32_t SetSelectedFrame(lldb_private::StackFrame *frame)
Mark a stack frame as the currently selected frame and return its index.
void CalculateCurrentInlinedDepth()
Calculate and set the current inline depth.
lldb::StackFrameSP GetStackFrameSPForStackFramePtr(StackFrame *stack_frame_ptr)
If stack_frame_ptr is contained in this StackFrameList, return its wrapping shared pointer.
bool WereAllFramesFetched() const
Returns whether we have currently fetched all the frames of a stack.
uint32_t GetCurrentInlinedDepth()
bool SetSelectedFrameByIndex(uint32_t idx)
Mark a stack frame as the currently selected frame using the frame index idx.
This base class provides an interface to stack frames.
void UpdatePreviousFrameFromCurrentFrame(StackFrame &curr_frame)
uint32_t GetConcreteFrameIndex() const
Query this frame to find what frame it is in this Thread's StackFrameList, not counting inlined frame...
void SetFrameIndex(uint32_t index)
Set this frame's synthetic frame index.
void DumpUsingSettingsFormat(Stream *strm, bool show_unique=false, const char *frame_marker=nullptr)
Print a description for this frame using the frame-format formatter settings.
lldb::RegisterContextSP GetRegisterContext()
Get the RegisterContext for this frame, if possible.
@ Regular
A regular stack frame with access to registers and local variables.
const SymbolContext & GetSymbolContext(lldb::SymbolContextItem resolve_scope)
Provide a SymbolContext for this StackFrame's current pc value.
lldb::TargetSP CalculateTarget() override
A stream class that can stream formatted output to a file.
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
size_t EOL()
Output and End of Line character to the stream.
void IndentLess(unsigned amount=2)
Decrement the current indentation level.
Defines a symbol context baton that can be handed other debug core functions.
Function * function
The Function for a given query.
bool GetParentOfInlinedScope(const Address &curr_frame_pc, SymbolContext &next_frame_sc, Address &inlined_frame_addr) const
Find the block containing the inlined block that contains this block.
Block * block
The Block for a given query.
LineEntry line_entry
The LineEntry for a given query.
bool GetFrameInfoAtIndex(uint32_t frame_idx, lldb::addr_t &cfa, lldb::addr_t &pc, bool &behaves_like_zeroth_frame)
uint32_t GetFramesUpTo(uint32_t end_idx)
#define LLDB_INVALID_ADDRESS
@ DoNoSelectMostRelevantFrame
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
std::shared_ptr< lldb_private::StackFrame > StackFrameSP
std::shared_ptr< lldb_private::RecognizedStackFrame > RecognizedStackFrameSP
std::shared_ptr< lldb_private::StopInfo > StopInfoSP
std::shared_ptr< lldb_private::Target > TargetSP
std::shared_ptr< lldb_private::RegisterContext > RegisterContextSP
std::shared_ptr< lldb_private::StackFrameList > StackFrameListSP
A sequence of calls that comprise some portion of a backtrace.
CallEdge::AddrType address_type
uint32_t line
The source line number, or LLDB_INVALID_LINE_NUMBER if there is no line number information.
const FileSpec & GetFile() const
Helper to access the file.
lldb::SupportFileSP file_sp
The source file, possibly mapped by the target.source-map setting.
void ApplyFileMappings(lldb::TargetSP target_sp)
Apply file mappings from target.source-map to the LineEntry's file.