31#include "llvm/ADT/ScopeExit.h"
32#include "llvm/ADT/SmallPtrSet.h"
33#include "llvm/Support/ConvertUTF.h"
45 bool show_inline_frames,
68 :
StackFrameList(thread, prev_frames_sp, show_inline_frames, provider_id),
82 size_t num_synthetic_frames = 0;
84 if (f && f->IsSynthetic())
85 num_synthetic_frames++;
89 for (uint32_t idx =
m_frames.size(); idx <= end_idx; idx++) {
90 if (allow_interrupt &&
91 m_thread.GetProcess()->GetTarget().GetDebugger().InterruptRequested())
102 auto clear_active_frames =
103 llvm::scope_exit([&]() {
m_thread.PopProviderFrameList(); });
104 auto frame_or_err =
m_provider->GetFrameAtIndex(idx);
109 "Frame provider reached end at index {1}: {0}", idx);
116 if (frame_sp->IsSynthetic() && !frame_sp->GetStackID().IsValid())
117 frame_sp->GetStackID().SetCFA(num_synthetic_frames++,
148 "GetCurrentInlinedDepth: invalidating current inlined depth.\n");
165 auto inline_depth = stop_info_sp->GetSuggestedStackFrameIndex(inlined);
174 "ResetCurrentInlinedDepth: setting inlined "
175 "depth: %d 0x%" PRIx64
".\n",
182 log,
"ResetCurrentInlinedDepth: Invalidating current inlined depth.\n");
190 if (current_inlined_depth > 0) {
239 "FindInterveningFrames: found call with retn-PC = {0:x}",
240 edge->GetReturnPCAddress(begin, target));
244 "No call edge outgoing from {0} with retn-PC == {1:x}",
255 if (first_callee.
function == &end) {
257 log,
"Not searching further, first callee is {0} (retn-PC: {1:x})",
268 llvm::SmallPtrSet<Function *, 2> visited_nodes = {};
269 bool ambiguous =
false;
277 : end(end), images(images), target(target), context(context) {}
281 dfs(first_edge, first_callee);
283 path = std::move(solution_path);
289 if (solution_path.empty())
290 solution_path = active_path;
300 if (!visited_nodes.insert(callee.
function).second) {
308 SymbolContext next_callee = edge->GetCallee(images, context);
312 std::tie(active_path.back().address_type, active_path.back().address) =
313 edge->GetCallerAddress(*callee.
function, target);
315 dfs(*edge, next_callee);
319 active_path.pop_back();
323 DFS(&end, images, target, exe_ctx).search(*first_edge, first_callee, path);
352 if (!next_reg_ctx_sp)
364 LLDB_LOG(log,
"SynthesizeTailCallFrames: can't find previous function");
370 LLDB_LOG(log,
"SynthesizeTailCallFrames: can't find next function");
377 addr_t return_pc = next_reg_ctx_sp->GetPC();
378 Target &target = *target_sp.get();
386 for (
const auto &calleeInfo : llvm::reverse(path)) {
387 Function *callee = calleeInfo.callee.function;
388 uint32_t frame_idx =
m_frames.size();
391 bool cfa_is_valid =
false;
395 const bool artificial =
true;
396 const bool behaves_like_zeroth_frame =
400 auto synth_frame = std::make_shared<StackFrame>(
401 m_thread.shared_from_this(), frame_idx, concrete_frame_idx, cfa,
403 behaves_like_zeroth_frame, &sc);
417 frame_sp->GetSymbolContext(eSymbolContextBlock | eSymbolContextFunction);
418 if (!unwind_sc.
block)
422 uint32_t concrete_frame_idx = frame_sp->GetConcreteFrameIndex();
423 Address curr_frame_address(frame_sp->GetFrameCodeAddressForSymbolication());
427 uint32_t num_inlined_frames = 0;
429 const bool behaves_like_zeroth_frame = frame_sp->m_behaves_like_zeroth_frame;
432 next_frame_address)) {
434 StackFrameSP inline_frame_sp = std::make_shared<StackFrame>(
436 frame_sp->GetRegisterContextSP(), cfa, next_frame_address,
437 behaves_like_zeroth_frame, &next_frame_sc);
440 m_frames.push_back(inline_frame_sp);
441 unwind_sc = next_frame_sc;
442 curr_frame_address = next_frame_address;
443 ++num_inlined_frames;
446 return num_inlined_frames;
460 bool was_interrupted =
false;
477#if defined(DEBUG_STACK_FRAMES)
478 s.PutCString(
"\n\nNew frames:\n");
482 return was_interrupted;
486 assert(
m_thread.IsValid() &&
"Expected valid thread");
487 assert(
m_frames.size() <= end_idx &&
"Expected there to be frames to fill");
495 if (num_frames <= end_idx + 1) {
508 bool was_interrupted =
false;
510#if defined(DEBUG_STACK_FRAMES)
519 uint32_t inlined_depth = 0;
524 end_idx += inlined_depth;
534 bool behaves_like_zeroth_frame = (idx == 0);
543 idx, cfa,
pc, behaves_like_zeroth_frame);
548 cfa = reg_ctx_sp->GetSP();
549 pc = reg_ctx_sp->GetPC();
552 unwind_frame_sp = std::make_shared<StackFrame>(
554 cfa,
pc, behaves_like_zeroth_frame,
nullptr);
556 m_frames.push_back(unwind_frame_sp);
560 cfa = unwind_frame_sp->m_id.GetCallFrameAddressWithoutMetadata();
565 if (allow_interrupt &&
568 was_interrupted =
true;
579 const bool cfa_is_valid =
true;
580 unwind_frame_sp = std::make_shared<StackFrame>(
591 m_frames.push_back(unwind_frame_sp);
594 assert(unwind_frame_sp);
596 }
while (
m_frames.size() - 1 < end_idx);
603#if defined(DEBUG_STACK_FRAMES)
605 prev_frames->
Dump(&s);
607 curr_frames->
Dump(&s);
610 size_t curr_frame_num, prev_frame_num;
612 for (curr_frame_num = curr_frames->
m_frames.size(),
613 prev_frame_num = prev_frames->
m_frames.size();
614 curr_frame_num > 0 && prev_frame_num > 0;
615 --curr_frame_num, --prev_frame_num) {
616 const size_t curr_frame_idx = curr_frame_num - 1;
617 const size_t prev_frame_idx = prev_frame_num - 1;
621#if defined(DEBUG_STACK_FRAMES)
622 s.
Printf(
"\n\nCurr frame #%u ", curr_frame_idx);
624 curr_frame_sp->Dump(&s,
true,
false);
627 s.
Printf(
"\nPrev frame #%u ", prev_frame_idx);
629 prev_frame_sp->Dump(&s,
true,
false);
637 if (curr_frame ==
nullptr || prev_frame ==
nullptr)
649 if (llvm::isa<BorrowedStackFrame>(prev_frame))
656 m_frames[curr_frame_idx] = prev_frame_sp;
658#if defined(DEBUG_STACK_FRAMES)
659 s.
PutCString(
"\n Copying previous frame to current frame");
667 return was_interrupted;
691 for (pos = begin; pos != end; ++pos) {
693 s->
Printf(
"%p: ",
static_cast<void *
>(frame));
698 s->
Printf(
"frame #%u", (uint32_t)std::distance(begin, pos));
706 uint32_t original_idx = idx;
716 idx += inlined_depth;
730 LLDB_LOG(log,
"GetFrameAtIndex was interrupted");
738 }
else if (original_idx == 0) {
746 assert(!
m_thread.IsValid() &&
"A valid thread has no frames.");
765 uint32_t frame_idx = unwind_idx;
768 if (frame_sp->GetFrameIndex() == unwind_idx)
777 return stack_sp->GetStackID().IsYoungerThan(stack_id);
784 uint32_t frame_idx = 0;
790 collection::const_iterator pos =
792 if (pos !=
m_frames.end() && (*pos)->GetStackID() == stack_id)
798 if (frame_sp && frame_sp->GetStackID() == stack_id)
832 LLDB_LOG(log,
"Failed to construct Frame #0");
838 if (recognized_frame_sp) {
840 recognized_frame_sp->GetMostRelevantFrame()) {
841 LLDB_LOG(log,
"Found most relevant frame at index {0}",
842 most_relevant_frame_sp->GetFrameIndex());
847 LLDB_LOG(log,
"Frame #0 not recognized");
852 uint32_t stack_idx = 0;
853 bool found_relevant =
false;
858 bool inlined =
false;
859 std::optional<uint32_t> stack_opt =
860 stop_info_sp->GetSuggestedStackFrameIndex(inlined);
862 stack_idx = *stack_opt;
863 found_relevant =
true;
869 LLDB_LOG(log,
"Stop info suggested relevant frame {0} but it didn't exist",
871 else if (found_relevant)
872 LLDB_LOG(log,
"Setting selected frame from stop info to {0}", stack_idx);
879 LLDB_LOG(log,
"No relevant frame!");
891 if (!select_most_relevant)
901 std::lock_guard<std::recursive_mutex> selected_frame_guard(
909 for (pos = begin; pos != end; ++pos) {
910 if (pos->get() == frame) {
933 m_thread.GetProcess()->GetThreadList().GetSelectedThread()->GetID()) {
937 SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextLineEntry);
939 m_thread.CalculateTarget()->GetSourceManager().SetDefaultFileAndLine(
956 std::lock_guard<std::recursive_mutex> selected_frame_guard(
969 for (pos = begin; pos != end; ++pos) {
970 if (pos->get() == stack_frame_ptr) {
983 return frame_sp->IsHidden();
993 return frame_sp->IsHidden();
998 bool show_hidden_marker) {
1000 if (frame_sp == selected_frame_sp)
1001 return show_unicode_marker ?
" * " :
"* ";
1002 if (!show_unicode_marker)
1005 return reinterpret_cast<const char *
>(u8
"﹉ ");
1007 return reinterpret_cast<const char *
>(u8
"﹍ ");
1012 uint32_t num_frames,
bool show_frame_info,
1013 uint32_t num_frames_with_source,
1014 bool show_unique,
bool show_hidden,
1015 bool show_hidden_marker,
1016 bool show_selected_frame) {
1017 size_t num_frames_displayed = 0;
1019 if (num_frames == 0)
1023 uint32_t frame_idx = 0;
1024 uint32_t last_frame;
1030 last_frame = first_frame + num_frames;
1035 for (frame_idx = first_frame; frame_idx < last_frame; ++frame_idx) {
1040 if (show_selected_frame)
1041 marker =
GetFrameMarker(frame_sp, selected_frame_sp, show_hidden_marker);
1044 show_hidden_marker);
1047 if (!show_hidden && frame_sp != selected_frame_sp && frame_sp->IsHidden())
1054 dbg,
"Interrupted dumping stack for thread {0:x} with {1} shown.",
1055 m_thread.GetID(), num_frames_displayed))
1058 if (!frame_sp->GetStatus(strm, show_frame_info,
1059 num_frames_with_source > (first_frame - frame_idx),
1060 show_unique, marker))
1062 ++num_frames_displayed;
1066 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_VERBOSE(log,...)
#define LLDB_LOG_ERROR(log, error,...)
#define LLDB_LOG_VERBOSE(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.
Represent a call made within a Function.
virtual SymbolContext GetCallee(ModuleList &images, ExecutionContext &exe_ctx)=0
Get the callee's definition, resolved against images.
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.
static PolicyStack & Get()
lldb::frame_list_id_t m_identifier
Unique identifier for this frame list instance.
lldb::frame_list_id_t GetIdentifier() const
Get the unique identifier for this frame list.
StackFrameList(Thread &thread, const lldb::StackFrameListSP &prev_frames_sp, bool show_inline_frames, lldb::frame_list_id_t provider_id=0)
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()
uint32_t SynthesizeInlineFrames(lldb::StackFrameSP frame_sp, lldb::addr_t cfa)
Synthesize inline frames for frame_sp by walking the inlined scope chain via GetParentOfInlinedScope ...
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.
virtual ~StackFrameList()
void FetchOnlyConcreteFramesUpTo(uint32_t end_idx)
Thread & m_thread
The thread this frame list describes.
llvm::sys::RWMutex m_list_mutex
A mutex for this frame list.
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 IsNextFrameHidden(lldb_private::StackFrame &frame)
Returns true if the next frame is hidden.
bool DecrementCurrentInlinedDepth()
uint32_t GetNumFrames(bool can_create=true)
Get the number of visible frames.
virtual bool FetchFramesUpTo(uint32_t end_idx, InterruptionControl allow_interrupt)
Returns true if fetching frames was interrupted, false otherwise.
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...
std::string GetFrameMarker(lldb::StackFrameSP frame_sp, lldb::StackFrameSP selected_frame_sp, bool show_hidden_marker)
Returns the stack frame marker depending on if frame_sp:
void SetAllFramesFetched()
void SetCurrentInlinedDepth(uint32_t new_depth)
Thread & GetThread() const
Get the thread associated with this frame list.
uint32_t GetSelectedFrameIndex(SelectMostRelevant select_most_relevant_frame)
Get the currently selected frame index.
uint32_t SetSelectedFrame(lldb_private::StackFrame *frame)
Mark a stack frame as the currently selected frame and return its index.
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, bool show_hidden_marker=true, bool show_selected_frame=false)
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 IsPreviousFrameHidden(lldb_private::StackFrame &frame)
Returns true if the previous frame is hidden.
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)
void SetFrameIndex(uint32_t index)
Set this frame's frame index.
virtual uint32_t GetConcreteFrameIndex()
Query this frame to find what frame it is in this Thread's StackFrameList, not counting inlined frame...
virtual lldb::RegisterContextSP GetRegisterContext()
Get the RegisterContext for this frame, if possible.
@ Regular
A regular stack frame with access to registers and local variables.
virtual StackID & GetStackID()
virtual const SymbolContext & GetSymbolContext(lldb::SymbolContextItem resolve_scope)
Provide a SymbolContext for this StackFrame's current pc value.
virtual uint32_t GetFrameIndex() const
Query this frame to find what frame it is in this Thread's StackFrameList.
virtual void DumpUsingSettingsFormat(Stream *strm, bool show_unique=false, const llvm::StringRef frame_marker="")
Print a description for this frame using the frame-format formatter settings.
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.
lldb::SyntheticFrameProviderSP m_provider
The provider that transforms the input frames.
bool FetchFramesUpTo(uint32_t end_idx, InterruptionControl allow_interrupt) override
Override FetchFramesUpTo to lazily return frames from the provider or from the actual stack frame lis...
lldb::StackFrameListSP m_input_frames
The input stack frame list that the provider transforms.
SyntheticStackFrameList(Thread &thread, lldb::StackFrameListSP input_frames, const lldb::StackFrameListSP &prev_frames_sp, bool show_inline_frames, lldb::SyntheticFrameProviderSP provider_sp, uint64_t provider_id)
static bool SupportsUnicode()
Returns whether or not the current terminal supports Unicode rendering.
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::SyntheticFrameProvider > SyntheticFrameProviderSP
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.
SupportFileNSP 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.
bool can_run_frame_recognizers
Describes what view of the process a thread should see and what operations it is allowed to perform.
Capabilities capabilities