LLDB mainline
StackFrameList.h
Go to the documentation of this file.
1//===-- StackFrameList.h ----------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLDB_TARGET_STACKFRAMELIST_H
10#define LLDB_TARGET_STACKFRAMELIST_H
11
12#include <memory>
13#include <mutex>
14#include <shared_mutex>
15#include <vector>
16
18
19namespace lldb_private {
20
21class ScriptedThread;
22
23class StackFrameList : public std::enable_shared_from_this<StackFrameList> {
24public:
25 // Constructors and Destructors
26 StackFrameList(Thread &thread, const lldb::StackFrameListSP &prev_frames_sp,
27 bool show_inline_frames,
28 lldb::frame_list_id_t provider_id = 0);
29
30 virtual ~StackFrameList();
31
32 /// Get the number of visible frames. Frames may be created if \p can_create
33 /// is true. Synthetic (inline) frames expanded from the concrete frame #0
34 /// (aka invisible frames) are not included in this count.
35 uint32_t GetNumFrames(bool can_create = true);
36
37 /// Get the frame at index \p idx. Invisible frames cannot be indexed.
39
40 /// Get the first concrete frame with index greater than or equal to \p idx.
41 /// Unlike \ref GetFrameAtIndex, this cannot return a synthetic frame.
43
44 /// Retrieve the stack frame with the given ID \p stack_id.
46
47 /// Mark a stack frame as the currently selected frame and return its index.
49
50 /// Resets the selected frame index of this object.
52
53 /// Returns \p true if the next frame is hidden.
55
56 /// Returns \p true if the previous frame is hidden.
58
59 /// Returns the stack frame marker depending on if \p frame_sp:
60 /// @li is selected: *
61 /// @li is the first non hidden frame: ﹍
62 /// @li is the last non hidden frame: ﹉
63 ///
64 /// If the terminal does not support Unicode rendering, the hidden frame
65 /// markers are replaced with whitespaces.
66 std::string GetFrameMarker(lldb::StackFrameSP frame_sp,
67 lldb::StackFrameSP selected_frame_sp,
68 bool show_hidden_marker);
69
70 /// Get the currently selected frame index.
71 /// We should only call SelectMostRelevantFrame if (a) the user hasn't already
72 /// selected a frame, and (b) if this really is a user facing
73 /// "GetSelectedFrame". SMRF runs the frame recognizers which can do
74 /// arbitrary work that ends up being dangerous to do internally. Also,
75 /// for most internal uses we don't actually want the frame changed by the
76 /// SMRF logic. So unless this is in a command or SB API, you should
77 /// pass false here.
78 uint32_t
79 GetSelectedFrameIndex(SelectMostRelevant select_most_relevant_frame);
80
81 /// Mark a stack frame as the currently selected frame using the frame index
82 /// \p idx. Like \ref GetFrameAtIndex, invisible frames cannot be selected.
83 bool SetSelectedFrameByIndex(uint32_t idx);
84
85 /// If the current inline depth (i.e the number of invisible frames) is valid,
86 /// subtract it from \p idx. Otherwise simply return \p idx.
87 uint32_t GetVisibleStackFrameIndex(uint32_t idx) {
89 return idx - m_current_inlined_depth;
90 else
91 return idx;
92 }
93
94 /// Calculate and set the current inline depth. This may be used to update
95 /// the StackFrameList's set of inline frames when execution stops, e.g when
96 /// a breakpoint is hit.
98
99 /// If the currently selected frame comes from the currently selected thread,
100 /// point the default file and line of the thread's target to the location
101 /// specified by the frame.
103
104 /// Clear the cache of frames.
105 void Clear();
106
107 void Dump(Stream *s);
108
109 /// If \p stack_frame_ptr is contained in this StackFrameList, return its
110 /// wrapping shared pointer.
113
114 size_t GetStatus(Stream &strm, uint32_t first_frame, uint32_t num_frames,
115 bool show_frame_info, uint32_t num_frames_with_source,
116 bool show_unique = false, bool show_hidden = false,
117 bool show_hidden_marker = true,
118 bool show_selected_frame = false);
119
120 /// Returns whether we have currently fetched all the frames of a stack.
121 bool WereAllFramesFetched() const;
122
123 /// Get the thread associated with this frame list.
124 Thread &GetThread() const { return m_thread; }
125
126 /// Get the unique identifier for this frame list.
128
129protected:
130 friend class Thread;
132 friend class ScriptedThread;
133
134 /// Use this API to build a stack frame list (used for scripted threads, for
135 /// instance.) This API is not meant for StackFrameLists that have unwinders
136 /// and partake in lazy stack filling (using GetFramesUpTo). Rather if you
137 /// are building StackFrameLists with this API, you should build the entire
138 /// list before making it available for use.
139 bool SetFrameAtIndex(uint32_t idx, lldb::StackFrameSP &frame_sp);
140
141 /// Ensures that frames up to (and including) `end_idx` are realized in the
142 /// StackFrameList. `end_idx` can be larger than the actual number of frames,
143 /// in which case all the frames will be fetched. Acquires the writer end of
144 /// the list mutex.
145 /// Returns true if the function was interrupted, false otherwise.
146 /// Callers should first check (under the shared mutex) whether we need to
147 /// fetch frames or not.
148 bool GetFramesUpTo(uint32_t end_idx, InterruptionControl allow_interrupt);
149
150 // This should be called with either the reader or writer end of the list
151 // mutex held:
152 bool GetAllFramesFetched() const {
154 }
155
156 // This should be called with the writer end of the list mutex held.
158
160
162
163 uint32_t GetCurrentInlinedDepth();
164
165 void SetCurrentInlinedDepth(uint32_t new_depth);
166
167 /// Calls into the stack frame recognizers and stop info to set the most
168 /// relevant frame. This can call out to arbitrary user code so it can't
169 /// hold the StackFrameList mutex.
171
172 typedef std::vector<lldb::StackFrameSP> collection;
173 typedef collection::iterator iterator;
174 typedef collection::const_iterator const_iterator;
175
176 /// The thread this frame list describes.
178
179 /// The old stack frame list.
180 // TODO: The old stack frame list is used to fill in missing frame info
181 // heuristically when it's otherwise unavailable (say, because the unwinder
182 // fails). We should have stronger checks to make sure that this is a valid
183 // source of information.
185
186 /// A mutex for this frame list. The only public API that requires the
187 /// unique lock is Clear. All other clients take the shared lock, though
188 /// if we need more frames we may swap shared for unique to fulfill that
189 /// requirement.
190 mutable std::shared_mutex m_list_mutex;
191
192 // Setting the inlined depth should be protected against other attempts to
193 // change it, but since it doesn't mutate the list itself, we can limit the
194 // critical regions it produces by having a separate mutex.
195 mutable std::mutex m_inlined_depth_mutex;
196
197 /// A cache of frames. This may need to be updated when the program counter
198 /// changes.
200
201 /// The currently selected frame. An optional is used to record whether anyone
202 /// has set the selected frame on this stack yet. We only let recognizers
203 /// change the frame if this is the first time GetSelectedFrame is called.
204 ///
205 /// Thread-safety:
206 /// This member is not protected by a mutex.
207 /// LLDB really only should have an opinion about the selected frame index
208 /// when a process stops, before control gets handed back to the user.
209 /// After that, it's up to them to change it whenever they feel like it.
210 /// If two parts of lldb decided they wanted to be in control of the selected
211 /// frame index on stop the right way to fix it would need to be some explicit
212 /// negotiation for who gets to control this.
213 std::optional<uint32_t> m_selected_frame_idx;
214
215 /// Protect access to m_selected_frame_idx. Always acquire after m_list_mutex
216 /// to avoid lock inversion. A recursive mutex because GetSelectedFrameIndex
217 /// may indirectly call SetSelectedFrame.
218 std::recursive_mutex m_selected_frame_mutex;
219
220 /// The number of concrete frames fetched while filling the frame list. This
221 /// is only used when synthetic frames are enabled.
223
224 /// The number of synthetic function activations (invisible frames) expanded
225 /// from the concrete frame #0 activation.
226 // TODO: Use an optional instead of UINT32_MAX to denote invalid values.
228
229 /// The program counter value at the currently selected synthetic activation.
230 /// This is only valid if m_current_inlined_depth is valid.
231 // TODO: Use an optional instead of UINT32_MAX to denote invalid values.
233
234 /// Whether or not to show synthetic (inline) frames. Immutable.
236
237 /// Unique identifier for this frame list instance.
239
240 /// Returns true if fetching frames was interrupted, false otherwise.
241 virtual bool FetchFramesUpTo(uint32_t end_idx,
242 InterruptionControl allow_interrupt);
243
244private:
248 std::shared_lock<std::shared_mutex> &guard);
249
250 /// @{
251 /// These two Fetch frames APIs and SynthesizeTailCallFrames are called in
252 /// GetFramesUpTo, they are the ones that actually add frames. They must be
253 /// called with the writer end of the list mutex held.
254 ///
255 /// Not currently interruptible so returns void.
256 /// }@
257 void FetchOnlyConcreteFramesUpTo(uint32_t end_idx);
258 void SynthesizeTailCallFrames(StackFrame &next_frame);
259
261 const StackFrameList &operator=(const StackFrameList &) = delete;
262};
263
264/// A StackFrameList that wraps another StackFrameList and uses a
265/// SyntheticFrameProvider to lazily provide frames from either the provider
266/// or the underlying real stack frame list.
268public:
270 const lldb::StackFrameListSP &prev_frames_sp,
271 bool show_inline_frames,
273 uint64_t provider_id);
274
275protected:
276 /// Override FetchFramesUpTo to lazily return frames from the provider
277 /// or from the actual stack frame list.
278 bool FetchFramesUpTo(uint32_t end_idx,
279 InterruptionControl allow_interrupt) override;
280
281private:
282 /// The input stack frame list that the provider transforms.
283 /// This could be a real StackFrameList or another SyntheticStackFrameList.
285
286 /// The provider that transforms the input frames.
288};
289
290} // namespace lldb_private
291
292#endif // LLDB_TARGET_STACKFRAMELIST_H
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.
std::vector< lldb::StackFrameSP > collection
void ClearSelectedFrameIndex()
Resets the selected frame index of this object.
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.
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.
lldb::StackFrameSP GetFrameAtIndexNoLock(uint32_t idx, std::shared_lock< std::shared_mutex > &guard)
void FetchOnlyConcreteFramesUpTo(uint32_t end_idx)
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.
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.
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...
StackFrameList(const StackFrameList &)=delete
uint32_t SetSelectedFrameNoLock(lldb_private::StackFrame *frame)
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 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.
std::shared_mutex m_list_mutex
A mutex for this frame list.
collection::iterator iterator
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.
const StackFrameList & operator=(const StackFrameList &)=delete
bool WereAllFramesFetched() const
Returns whether we have currently fetched all the frames of a stack.
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.
Definition StackFrame.h:44
A stream class that can stream formatted output to a file.
Definition Stream.h:28
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)
#define UINT32_MAX
A class that represents a running process on the host machine.
std::shared_ptr< lldb_private::SyntheticFrameProvider > SyntheticFrameProviderSP
std::shared_ptr< lldb_private::StackFrame > StackFrameSP
uint64_t addr_t
Definition lldb-types.h:80
std::shared_ptr< lldb_private::StackFrameList > StackFrameListSP
uint32_t frame_list_id_t
Definition lldb-types.h:86