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 <vector>
15
17#include "llvm/Support/RWMutex.h"
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 visible frame whose concrete frame index is \p unwind_idx,
41 /// or nullptr if there is none. Unlike \ref GetFrameAtIndex, this cannot
42 /// return the artificial frame of a tail call.
44
45 /// Retrieve the stack frame with the given ID \p stack_id.
47
48 /// Mark a stack frame as the currently selected frame and return its index.
50
51 /// Resets the selected frame index of this object.
53
54 /// Returns \p true if the next frame is hidden.
56
57 /// Returns \p true if the previous frame is hidden.
59
60 /// Returns the stack frame marker depending on if \p frame_sp:
61 /// @li is selected: *
62 /// @li is the first non hidden frame: ﹍
63 /// @li is the last non hidden frame: ﹉
64 ///
65 /// If the terminal does not support Unicode rendering, the hidden frame
66 /// markers are replaced with whitespaces.
67 std::string GetFrameMarker(lldb::StackFrameSP frame_sp,
68 lldb::StackFrameSP selected_frame_sp,
69 bool show_hidden_marker);
70
71 /// Get the currently selected frame index.
72 /// We should only call SelectMostRelevantFrame if (a) the user hasn't already
73 /// selected a frame, and (b) if this really is a user facing
74 /// "GetSelectedFrame". SMRF runs the frame recognizers which can do
75 /// arbitrary work that ends up being dangerous to do internally. Also,
76 /// for most internal uses we don't actually want the frame changed by the
77 /// SMRF logic. So unless this is in a command or SB API, you should
78 /// pass false here.
79 uint32_t
80 GetSelectedFrameIndex(SelectMostRelevant select_most_relevant_frame);
81
82 /// Mark a stack frame as the currently selected frame using the frame index
83 /// \p idx. Like \ref GetFrameAtIndex, invisible frames cannot be selected.
84 bool SetSelectedFrameByIndex(uint32_t idx);
85
86 /// If the current inline depth (i.e the number of invisible frames) is valid,
87 /// subtract it from \p idx. Otherwise simply return \p idx.
88 uint32_t GetVisibleStackFrameIndex(uint32_t idx) {
90 return idx - m_current_inlined_depth;
91 else
92 return idx;
93 }
94
95 /// Calculate and set the current inline depth. This may be used to update
96 /// the StackFrameList's set of inline frames when execution stops, e.g when
97 /// a breakpoint is hit.
99
100 /// If the currently selected frame comes from the currently selected thread,
101 /// point the default file and line of the thread's target to the location
102 /// specified by the frame.
104
105 /// Clear the cache of frames.
106 void Clear();
107
108 void Dump(Stream *s);
109
110 /// If \p stack_frame_ptr is contained in this StackFrameList, return its
111 /// wrapping shared pointer.
114
115 size_t GetStatus(Stream &strm, uint32_t first_frame, uint32_t num_frames,
116 bool show_frame_info, uint32_t num_frames_with_source,
117 bool show_unique = false, bool show_hidden = false,
118 bool show_hidden_marker = true,
119 bool show_selected_frame = false);
120
121 /// Returns whether we have currently fetched all the frames of a stack.
122 bool WereAllFramesFetched() const;
123
124 /// Get the thread associated with this frame list.
125 Thread &GetThread() const { return m_thread; }
126
127 /// Get the unique identifier for this frame list.
129
130protected:
131 friend class Thread;
133 friend class ScriptedThread;
134
135 /// Use this API to build a stack frame list (used for scripted threads, for
136 /// instance.) This API is not meant for StackFrameLists that have unwinders
137 /// and partake in lazy stack filling (using GetFramesUpTo). Rather if you
138 /// are building StackFrameLists with this API, you should build the entire
139 /// list before making it available for use.
140 bool SetFrameAtIndex(uint32_t idx, lldb::StackFrameSP &frame_sp);
141
142 /// Ensures that frames up to (and including) `end_idx` are realized in the
143 /// StackFrameList. `end_idx` can be larger than the actual number of frames,
144 /// in which case all the frames will be fetched. Acquires the writer end of
145 /// the list mutex.
146 /// Returns true if the function was interrupted, false otherwise.
147 /// Callers should first check (under the shared mutex) whether we need to
148 /// fetch frames or not.
149 bool GetFramesUpTo(uint32_t end_idx, InterruptionControl allow_interrupt);
150
151 // This should be called with either the reader or writer end of the list
152 // mutex held:
153 bool GetAllFramesFetched() const {
155 }
156
157 // This should be called with the writer end of the list mutex held.
159
161
163
164 uint32_t GetCurrentInlinedDepth();
165
166 void SetCurrentInlinedDepth(uint32_t new_depth);
167
168 /// Calls into the stack frame recognizers and stop info to set the most
169 /// relevant frame. This can call out to arbitrary user code so it can't
170 /// hold the StackFrameList mutex.
172
173 typedef std::vector<lldb::StackFrameSP> collection;
174 typedef collection::iterator iterator;
175 typedef collection::const_iterator const_iterator;
176
177 /// The thread this frame list describes.
179
180 /// The old stack frame list.
181 // TODO: The old stack frame list is used to fill in missing frame info
182 // heuristically when it's otherwise unavailable (say, because the unwinder
183 // fails). We should have stronger checks to make sure that this is a valid
184 // source of information.
186
187 /// A mutex for this frame list. The only public API that requires the
188 /// unique lock is Clear. All other clients take the shared lock, though
189 /// if we need more frames we may swap shared for unique to fulfill that
190 /// requirement.
191 mutable llvm::sys::RWMutex m_list_mutex;
192
193 // Setting the inlined depth should be protected against other attempts to
194 // change it, but since it doesn't mutate the list itself, we can limit the
195 // critical regions it produces by having a separate mutex.
196 mutable std::mutex m_inlined_depth_mutex;
197
198 /// A cache of frames. This may need to be updated when the program counter
199 /// changes.
201
202 /// The currently selected frame. An optional is used to record whether anyone
203 /// has set the selected frame on this stack yet. We only let recognizers
204 /// change the frame if this is the first time GetSelectedFrame is called.
205 ///
206 /// Thread-safety:
207 /// This member is not protected by a mutex.
208 /// LLDB really only should have an opinion about the selected frame index
209 /// when a process stops, before control gets handed back to the user.
210 /// After that, it's up to them to change it whenever they feel like it.
211 /// If two parts of lldb decided they wanted to be in control of the selected
212 /// frame index on stop the right way to fix it would need to be some explicit
213 /// negotiation for who gets to control this.
214 std::optional<uint32_t> m_selected_frame_idx;
215
216 /// Protect access to m_selected_frame_idx. Always acquire after m_list_mutex
217 /// to avoid lock inversion. A recursive mutex because GetSelectedFrameIndex
218 /// may indirectly call SetSelectedFrame.
219 std::recursive_mutex m_selected_frame_mutex;
220
221 /// The number of concrete frames fetched while filling the frame list. This
222 /// is only used when synthetic frames are enabled.
224
225 /// The number of synthetic function activations (invisible frames) expanded
226 /// from the concrete frame #0 activation.
227 // TODO: Use an optional instead of UINT32_MAX to denote invalid values.
229
230 /// The program counter value at the currently selected synthetic activation.
231 /// This is only valid if m_current_inlined_depth is valid.
232 // TODO: Use an optional instead of UINT32_MAX to denote invalid values.
234
235 /// Whether or not to show synthetic (inline) frames. Immutable.
237
238 /// Unique identifier for this frame list instance.
240
241 /// Returns true if fetching frames was interrupted, false otherwise.
242 virtual bool FetchFramesUpTo(uint32_t end_idx,
243 InterruptionControl allow_interrupt);
244
245private:
248 llvm::sys::ScopedReader &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
260 /// Synthesize inline frames for \p frame_sp by walking the inlined
261 /// scope chain via GetParentOfInlinedScope and appending frames to the
262 /// list. Returns the number of inline frames created.
264 lldb::addr_t cfa);
265
267 const StackFrameList &operator=(const StackFrameList &) = delete;
268};
269
270/// A StackFrameList that wraps another StackFrameList and uses a
271/// SyntheticFrameProvider to lazily provide frames from either the provider
272/// or the underlying real stack frame list.
274public:
276 const lldb::StackFrameListSP &prev_frames_sp,
277 bool show_inline_frames,
279 uint64_t provider_id);
280
281protected:
282 /// Override FetchFramesUpTo to lazily return frames from the provider
283 /// or from the actual stack frame list.
284 bool FetchFramesUpTo(uint32_t end_idx,
285 InterruptionControl allow_interrupt) override;
286
287private:
288 /// The input stack frame list that the provider transforms.
289 /// This could be a real StackFrameList or another SyntheticStackFrameList.
291
292 /// The provider that transforms the input frames.
294};
295
296} // namespace lldb_private
297
298#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 visible frame whose concrete frame index is unwind_idx, or nullptr if there is none.
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.
lldb::StackFrameSP GetFrameAtIndexNoLock(uint32_t idx, llvm::sys::ScopedReader &guard)
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.
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.
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.
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.
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:87