1 /* Definitions for a frame unwinder, for GDB, the GNU debugger.
3 Copyright (C) 2003-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #ifndef GDB_FRAME_UNWIND_H
21 #define GDB_FRAME_UNWIND_H
33 /* The following unwind functions assume a chain of frames forming the
34 sequence: (outer) prev <-> this <-> next (inner). All the
35 functions are called with this frame's `struct frame_info' and
38 THIS frame's register values can be obtained by unwinding NEXT
39 frame's registers (a recursive operation).
41 THIS frame's prologue cache can be used to cache information such
42 as where this frame's prologue stores the previous frame's
45 /* Given THIS frame, take a whiff of its registers (namely
46 the PC and attributes) and if SELF is the applicable unwinder,
47 return non-zero. Possibly also initialize THIS_PROLOGUE_CACHE; but
48 only if returning 1. Initializing THIS_PROLOGUE_CACHE in other
49 cases (0 return) is invalid. In case of exception, the caller has
50 to set *THIS_PROLOGUE_CACHE to NULL. */
52 typedef int (frame_sniffer_ftype
) (const struct frame_unwind
*self
,
53 const frame_info_ptr
&this_frame
,
54 void **this_prologue_cache
);
56 typedef unwind_stop_reason (frame_unwind_stop_reason_ftype
)
57 (const frame_info_ptr
&this_frame
, void **this_prologue_cache
);
59 /* A default frame sniffer which always accepts the frame. Used by
60 fallback prologue unwinders. */
62 int default_frame_sniffer (const struct frame_unwind
*self
,
63 const frame_info_ptr
&this_frame
,
64 void **this_prologue_cache
);
66 /* A default stop_reason callback which always claims the frame is
69 enum unwind_stop_reason
70 default_frame_unwind_stop_reason (const frame_info_ptr
&this_frame
,
73 /* A default unwind_pc callback that simply unwinds the register identified
74 by GDBARCH_PC_REGNUM. */
76 extern CORE_ADDR
default_unwind_pc (struct gdbarch
*gdbarch
,
77 const frame_info_ptr
&next_frame
);
79 /* A default unwind_sp callback that simply unwinds the register identified
80 by GDBARCH_SP_REGNUM. */
82 extern CORE_ADDR
default_unwind_sp (struct gdbarch
*gdbarch
,
83 const frame_info_ptr
&next_frame
);
85 /* Assuming the frame chain: (outer) prev <-> this <-> next (inner);
86 use THIS frame, and through it the NEXT frame's register unwind
87 method, to determine the frame ID of THIS frame.
89 A frame ID provides an invariant that can be used to re-identify an
90 instance of a frame. It is a combination of the frame's `base' and
91 the frame's function's code address.
93 Traditionally, THIS frame's ID was determined by examining THIS
94 frame's function's prologue, and identifying the register/offset
95 used as THIS frame's base.
97 Example: An examination of THIS frame's prologue reveals that, on
98 entry, it saves the PC(+12), SP(+8), and R1(+4) registers
99 (decrementing the SP by 12). Consequently, the frame ID's base can
100 be determined by adding 12 to the THIS frame's stack-pointer, and
101 the value of THIS frame's SP can be obtained by unwinding the NEXT
104 THIS_PROLOGUE_CACHE can be used to share any prolog analysis data
105 with the other unwind methods. Memory for that cache should be
106 allocated using FRAME_OBSTACK_ZALLOC(). */
108 typedef void (frame_this_id_ftype
) (const frame_info_ptr
&this_frame
,
109 void **this_prologue_cache
,
110 struct frame_id
*this_id
);
112 /* Assuming the frame chain: (outer) prev <-> this <-> next (inner);
113 use THIS frame, and implicitly the NEXT frame's register unwind
114 method, to unwind THIS frame's registers (returning the value of
115 the specified register REGNUM in the previous frame).
117 Traditionally, THIS frame's registers were unwound by examining
118 THIS frame's function's prologue and identifying which registers
119 that prolog code saved on the stack.
121 Example: An examination of THIS frame's prologue reveals that, on
122 entry, it saves the PC(+12), SP(+8), and R1(+4) registers
123 (decrementing the SP by 12). Consequently, the value of the PC
124 register in the previous frame is found in memory at SP+12, and
125 THIS frame's SP can be obtained by unwinding the NEXT frame's SP.
127 This function takes THIS_FRAME as an argument. It can find the
128 values of registers in THIS frame by calling get_frame_register
129 (THIS_FRAME), and reinvoke itself to find other registers in the
130 PREVIOUS frame by calling frame_unwind_register (THIS_FRAME).
132 The result is a GDB value object describing the register value. It
133 may be a lazy reference to memory, a lazy reference to the value of
134 a register in THIS frame, or a non-lvalue.
136 If the previous frame's register was not saved by THIS_FRAME and is
137 therefore undefined, return a wholly optimized-out not_lval value.
139 THIS_PROLOGUE_CACHE can be used to share any prolog analysis data
140 with the other unwind methods. Memory for that cache should be
141 allocated using FRAME_OBSTACK_ZALLOC(). */
143 typedef value
*(frame_prev_register_ftype
) (const frame_info_ptr
&this_frame
,
144 void **this_prologue_cache
,
147 /* Deallocate extra memory associated with the frame cache if any. */
149 typedef void (frame_dealloc_cache_ftype
) (frame_info
*self
,
152 /* Assuming the frame chain: (outer) prev <-> this <-> next (inner);
153 use THIS frame, and implicitly the NEXT frame's register unwind
154 method, return PREV frame's architecture. */
156 typedef gdbarch
*(frame_prev_arch_ftype
) (const frame_info_ptr
&this_frame
,
157 void **this_prologue_cache
);
159 /* Unwinders are classified by what part of GDB code created it. */
160 enum frame_unwind_class
162 /* This is mostly handled by core GDB code. */
164 /* This unwinder was added by one of GDB's extension languages. */
165 FRAME_UNWIND_EXTENSION
,
166 /* The unwinder was created and mostly handles debug information. */
167 FRAME_UNWIND_DEBUGINFO
,
168 /* The unwinder was created and handles target dependent things. */
170 /* Meta enum value, to ensure we're always sent a valid unwinder class. */
177 frame_unwind (const char *name
, frame_type type
, frame_unwind_class uclass
,
178 const frame_data
*data
)
179 : m_name (name
), m_type (type
), m_unwinder_class (uclass
),
183 const char *name () const
186 frame_type
type () const
189 frame_unwind_class
unwinder_class () const
190 { return m_unwinder_class
; }
192 const frame_data
*unwind_data () const
193 { return m_unwind_data
; }
195 bool enabled () const
196 { return m_enabled
; }
198 void set_enabled (bool state
) const
199 { m_enabled
= state
; }
201 /* Default stop_reason implementation. It reports NO_REASON, unless the
202 frame is the outermost. */
204 virtual unwind_stop_reason
stop_reason (const frame_info_ptr
&this_frame
,
205 void **this_prologue_cache
) const
207 return default_frame_unwind_stop_reason (this_frame
, this_prologue_cache
);
210 /* Default frame sniffer. Will always return that the unwinder
211 is able to unwind the frame. */
213 virtual int sniff (const frame_info_ptr
&this_frame
,
214 void **this_prologue_cache
) const
217 /* Calculate the ID of the given frame using this unwinder. */
219 virtual void this_id (const frame_info_ptr
&this_frame
,
220 void **this_prologue_cache
,
221 struct frame_id
*id
) const = 0;
223 /* Get the value of a register in a previous frame. */
225 virtual struct value
*prev_register (const frame_info_ptr
&this_frame
,
226 void **this_prologue_cache
,
227 int regnum
) const = 0;
229 /* Properly deallocate the cache. */
231 virtual void dealloc_cache (frame_info
*self
, void *this_cache
) const
234 /* Get the previous architecture. */
236 virtual gdbarch
*prev_arch (const frame_info_ptr
&this_frame
,
237 void **this_prologue_cache
) const
238 { return get_frame_arch (this_frame
); }
242 /* Name of the unwinder. Used to uniquely identify unwinders. */
245 /* The frame's type. Should this instead be a collection of
246 predicates that test the frame for various attributes? */
249 /* What kind of unwinder is this. It generally follows from where
250 the unwinder was added or where it looks for information to do the
252 frame_unwind_class m_unwinder_class
;
254 /* Additional data used by the trampoline and python frame unwinders. */
255 const frame_data
*m_unwind_data
;
257 /* Whether this unwinder can be used when sniffing. */
258 mutable bool m_enabled
= true;
261 /* This is a legacy version of the frame unwinder. The original struct
262 used function pointers for callbacks, this updated version has a
263 method that just passes the parameters along to the callback
265 Do not use this class for new unwinders. Instead, see other classes
266 that inherit from frame_unwind, such as the python unwinder. */
267 struct frame_unwind_legacy
: public frame_unwind
269 frame_unwind_legacy (const char *name
, frame_type type
,
270 frame_unwind_class uclass
,
271 frame_unwind_stop_reason_ftype
*stop_reason_func
,
272 frame_this_id_ftype
*this_id_func
,
273 frame_prev_register_ftype
*prev_register_func
,
274 const struct frame_data
*data
,
275 frame_sniffer_ftype
*sniffer_func
,
276 frame_dealloc_cache_ftype
*dealloc_cache_func
= nullptr,
277 frame_prev_arch_ftype
*prev_arch_func
= nullptr)
278 : frame_unwind (name
, type
, uclass
, data
), m_stop_reason (stop_reason_func
),
279 m_this_id (this_id_func
), m_prev_register (prev_register_func
),
280 m_sniffer (sniffer_func
), m_dealloc_cache (dealloc_cache_func
),
281 m_prev_arch (prev_arch_func
)
284 /* This method just passes the parameters to the callback pointer. */
286 enum unwind_stop_reason
stop_reason (const frame_info_ptr
&this_frame
,
287 void **this_prologue_cache
) const override
;
289 /* This method just passes the parameters to the callback pointer. */
291 void this_id (const frame_info_ptr
&this_frame
, void **this_prologue_cache
,
292 struct frame_id
*id
) const override
;
294 /* This method just passes the parameters to the callback pointer. */
296 struct value
*prev_register (const frame_info_ptr
&this_frame
,
297 void **this_prologue_cache
,
298 int regnum
) const override
;
300 /* This method just passes the parameters to the callback pointer. */
302 int sniff (const frame_info_ptr
&this_frame
,
303 void **this_prologue_cache
) const override
;
305 /* This method just passes the parameters to the callback pointer.
306 It is safe to call this method if no callback is installed, it
307 just turns into a noop. */
309 void dealloc_cache (frame_info
*self
, void *this_cache
) const override
;
311 /* This method just passes the parameters to the callback pointer. */
313 struct gdbarch
*prev_arch (const frame_info_ptr
&this_frame
,
314 void **this_prologue_cache
) const override
;
318 frame_unwind_stop_reason_ftype
*m_stop_reason
;
319 frame_this_id_ftype
*m_this_id
;
320 frame_prev_register_ftype
*m_prev_register
;
321 frame_sniffer_ftype
*m_sniffer
;
322 frame_dealloc_cache_ftype
*m_dealloc_cache
;
323 frame_prev_arch_ftype
*m_prev_arch
;
326 /* Register a frame unwinder, _prepending_ it to the front of the
327 search list (so it is sniffed before previously registered
328 unwinders). By using a prepend, later calls can install unwinders
329 that override earlier calls. This allows, for instance, an OSABI
330 to install a more specific sigtramp unwinder that overrides the
331 traditional brute-force unwinder. */
332 extern void frame_unwind_prepend_unwinder (struct gdbarch
*,
333 const struct frame_unwind
*);
335 /* Add a frame sniffer to the list. The predicates are polled in the
336 order that they are appended. The initial list contains the dummy
339 extern void frame_unwind_append_unwinder (struct gdbarch
*gdbarch
,
340 const struct frame_unwind
*unwinder
);
342 /* Iterate through sniffers for THIS_FRAME frame until one returns with an
343 unwinder implementation. THIS_FRAME->UNWIND must be NULL, it will get set
344 by this function. Possibly initialize THIS_CACHE. */
346 extern void frame_unwind_find_by_frame (const frame_info_ptr
&this_frame
,
349 /* Helper functions for value-based register unwinding. These return
350 a (possibly lazy) value of the appropriate type. */
352 /* Return a value which indicates that FRAME did not save REGNUM. */
354 value
*frame_unwind_got_optimized (const frame_info_ptr
&frame
, int regnum
);
356 /* Return a value which indicates that FRAME copied REGNUM into
357 register NEW_REGNUM. */
359 value
*frame_unwind_got_register (const frame_info_ptr
&frame
, int regnum
,
362 /* Return a value which indicates that FRAME saved REGNUM in memory at
365 value
*frame_unwind_got_memory (const frame_info_ptr
&frame
, int regnum
,
368 /* Return a value which indicates that FRAME's saved version of
369 REGNUM has a known constant (computed) value of VAL. */
371 value
*frame_unwind_got_constant (const frame_info_ptr
&frame
, int regnum
,
374 /* Return a value which indicates that FRAME's saved version of
375 REGNUM has a known constant (computed) value which is stored
378 value
*frame_unwind_got_bytes (const frame_info_ptr
&frame
, int regnum
,
379 gdb::array_view
<const gdb_byte
> buf
);
381 /* Return a value which indicates that FRAME's saved version of REGNUM
382 has a known constant (computed) value of ADDR. Convert the
383 CORE_ADDR to a target address if necessary. */
385 value
*frame_unwind_got_address (const frame_info_ptr
&frame
, int regnum
,
388 #endif /* GDB_FRAME_UNWIND_H */