GDB (xrefs)
Loading...
Searching...
No Matches
frame-unwind.c
Go to the documentation of this file.
1/* Definitions for frame unwinder, for GDB, the GNU debugger.
2
3 Copyright (C) 2003-2023 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20#include "defs.h"
21#include "frame.h"
22#include "frame-unwind.h"
23#include "dummy-frame.h"
24#include "inline-frame.h"
25#include "value.h"
26#include "regcache.h"
27#include "gdbsupport/gdb_obstack.h"
28#include "target.h"
29#include "gdbarch.h"
31#include "cli/cli-cmds.h"
32
38
40{
41 struct frame_unwind_table_entry *list = nullptr;
42 /* The head of the OSABI part of the search list. */
44};
45
48
49/* A helper function to add an unwinder to a list. LINK says where to
50 install the new unwinder. The new link is returned. */
51
52static struct frame_unwind_table_entry **
53add_unwinder (struct obstack *obstack, const struct frame_unwind *unwinder,
54 struct frame_unwind_table_entry **link)
55{
56 *link = OBSTACK_ZALLOC (obstack, struct frame_unwind_table_entry);
57 (*link)->unwinder = unwinder;
58 return &(*link)->next;
59}
60
61static struct frame_unwind_table *
63{
65 if (table != nullptr)
66 return table;
67
68 table = new frame_unwind_table;
69
70 /* Start the table out with a few default sniffers. OSABI code
71 can't override this. */
72 struct frame_unwind_table_entry **link = &table->list;
73
74 struct obstack *obstack = gdbarch_obstack (gdbarch);
75 link = add_unwinder (obstack, &dummy_frame_unwind, link);
76 /* The DWARF tailcall sniffer must come before the inline sniffer.
77 Otherwise, we can end up in a situation where a DWARF frame finds
78 tailcall information, but then the inline sniffer claims a frame
79 before the tailcall sniffer, resulting in confusion. This is
80 safe to do always because the tailcall sniffer can only ever be
81 activated if the newer frame was created using the DWARF
82 unwinder, and it also found tailcall information. */
83 link = add_unwinder (obstack, &dwarf2_tailcall_frame_unwind, link);
84 link = add_unwinder (obstack, &inline_frame_unwind, link);
85
86 /* The insertion point for OSABI sniffers. */
87 table->osabi_head = link;
89
90 return table;
91}
92
93void
95 const struct frame_unwind *unwinder)
96{
98 struct frame_unwind_table_entry *entry;
99
100 /* Insert the new entry at the start of the list. */
102 entry->unwinder = unwinder;
103 entry->next = (*table->osabi_head);
104 (*table->osabi_head) = entry;
105}
106
107void
109 const struct frame_unwind *unwinder)
110{
112 struct frame_unwind_table_entry **ip;
113
114 /* Find the end of the list and insert the new entry there. */
115 for (ip = table->osabi_head; (*ip) != NULL; ip = &(*ip)->next);
117 (*ip)->unwinder = unwinder;
118}
119
120/* Call SNIFFER from UNWINDER. If it succeeded set UNWINDER for
121 THIS_FRAME and return 1. Otherwise the function keeps THIS_FRAME
122 unchanged and returns 0. */
123
124static int
125frame_unwind_try_unwinder (frame_info_ptr this_frame, void **this_cache,
126 const struct frame_unwind *unwinder)
127{
128 int res = 0;
129
130 unsigned int entry_generation = get_frame_cache_generation ();
131
133
134 try
135 {
136 frame_debug_printf ("trying unwinder \"%s\"", unwinder->name);
137 res = unwinder->sniffer (unwinder, this_frame, this_cache);
138 }
139 catch (const gdb_exception &ex)
140 {
141 frame_debug_printf ("caught exception: %s", ex.message->c_str ());
142
143 /* Catch all exceptions, caused by either interrupt or error.
144 Reset *THIS_CACHE, unless something reinitialized the frame
145 cache meanwhile, in which case THIS_FRAME/THIS_CACHE are now
146 dangling. */
147 if (get_frame_cache_generation () == entry_generation)
148 {
149 *this_cache = NULL;
150 frame_cleanup_after_sniffer (this_frame);
151 }
152
153 if (ex.error == NOT_AVAILABLE_ERROR)
154 {
155 /* This usually means that not even the PC is available,
156 thus most unwinders aren't able to determine if they're
157 the best fit. Keep trying. Fallback prologue unwinders
158 should always accept the frame. */
159 return 0;
160 }
161 throw;
162 }
163
164 if (res)
165 {
166 frame_debug_printf ("yes");
167 return 1;
168 }
169 else
170 {
171 frame_debug_printf ("no");
172 /* Don't set *THIS_CACHE to NULL here, because sniffer has to do
173 so. */
174 frame_cleanup_after_sniffer (this_frame);
175 return 0;
176 }
177 gdb_assert_not_reached ("frame_unwind_try_unwinder");
178}
179
180/* Iterate through sniffers for THIS_FRAME frame until one returns with an
181 unwinder implementation. THIS_FRAME->UNWIND must be NULL, it will get set
182 by this function. Possibly initialize THIS_CACHE. */
183
184void
185frame_unwind_find_by_frame (frame_info_ptr this_frame, void **this_cache)
186{
188 frame_debug_printf ("this_frame=%d", frame_relative_level (this_frame));
189
190 struct gdbarch *gdbarch = get_frame_arch (this_frame);
192 struct frame_unwind_table_entry *entry;
193 const struct frame_unwind *unwinder_from_target;
194
195 unwinder_from_target = target_get_unwinder ();
196 if (unwinder_from_target != NULL
197 && frame_unwind_try_unwinder (this_frame, this_cache,
198 unwinder_from_target))
199 return;
200
201 unwinder_from_target = target_get_tailcall_unwinder ();
202 if (unwinder_from_target != NULL
203 && frame_unwind_try_unwinder (this_frame, this_cache,
204 unwinder_from_target))
205 return;
206
207 for (entry = table->list; entry != NULL; entry = entry->next)
208 if (frame_unwind_try_unwinder (this_frame, this_cache, entry->unwinder))
209 return;
210
211 internal_error (_("frame_unwind_find_by_frame failed"));
212}
213
214/* A default frame sniffer which always accepts the frame. Used by
215 fallback prologue unwinders. */
216
217int
219 frame_info_ptr this_frame,
220 void **this_prologue_cache)
221{
222 return 1;
223}
224
225/* The default frame unwinder stop_reason callback. */
226
229 void **this_cache)
230{
231 struct frame_id this_id = get_frame_id (this_frame);
232
233 if (this_id == outer_frame_id)
234 return UNWIND_OUTERMOST;
235 else
236 return UNWIND_NO_REASON;
237}
238
239/* See frame-unwind.h. */
240
241CORE_ADDR
243{
245 CORE_ADDR pc = frame_unwind_register_unsigned (next_frame, pc_regnum);
247 return pc;
248}
249
250/* See frame-unwind.h. */
251
252CORE_ADDR
258
259/* Helper functions for value-based register unwinding. These return
260 a (possibly lazy) value of the appropriate type. */
261
262/* Return a value which indicates that FRAME did not save REGNUM. */
263
264struct value *
272
273/* Return a value which indicates that FRAME copied REGNUM into
274 register NEW_REGNUM. */
275
276struct value *
278 int regnum, int new_regnum)
279{
280 return value_of_register_lazy (frame, new_regnum);
281}
282
283/* Return a value which indicates that FRAME saved REGNUM in memory at
284 ADDR. */
285
286struct value *
288{
289 struct gdbarch *gdbarch = frame_unwind_arch (frame);
290 struct value *v = value_at_lazy (register_type (gdbarch, regnum), addr);
291
292 v->set_stack (true);
293 return v;
294}
295
296/* Return a value which indicates that FRAME's saved version of
297 REGNUM has a known constant (computed) value of VAL. */
298
299struct value *
301 ULONGEST val)
302{
303 struct gdbarch *gdbarch = frame_unwind_arch (frame);
304 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
305 struct value *reg_val;
306
308 store_unsigned_integer (reg_val->contents_writeable ().data (),
309 register_size (gdbarch, regnum), byte_order, val);
310 return reg_val;
311}
312
313struct value *
314frame_unwind_got_bytes (frame_info_ptr frame, int regnum, const gdb_byte *buf)
315{
316 struct gdbarch *gdbarch = frame_unwind_arch (frame);
317 struct value *reg_val;
318
320 memcpy (reg_val->contents_raw ().data (), buf,
322 return reg_val;
323}
324
325/* Return a value which indicates that FRAME's saved version of REGNUM
326 has a known constant (computed) value of ADDR. Convert the
327 CORE_ADDR to a target address if necessary. */
328
329struct value *
331 CORE_ADDR addr)
332{
333 struct gdbarch *gdbarch = frame_unwind_arch (frame);
334 struct value *reg_val;
335
337 pack_long (reg_val->contents_writeable ().data (),
338 register_type (gdbarch, regnum), addr);
339 return reg_val;
340}
341
342/* Implement "maintenance info frame-unwinders" command. */
343
344static void
345maintenance_info_frame_unwinders (const char *args, int from_tty)
346{
347 struct gdbarch *gdbarch = target_gdbarch ();
349
350 ui_out *uiout = current_uiout;
351 ui_out_emit_table table_emitter (uiout, 2, -1, "FrameUnwinders");
352 uiout->table_header (27, ui_left, "name", "Name");
353 uiout->table_header (25, ui_left, "type", "Type");
354 uiout->table_body ();
355
356 for (struct frame_unwind_table_entry *entry = table->list; entry != NULL;
357 entry = entry->next)
358 {
359 const char *name = entry->unwinder->name;
360 const char *type = frame_type_str (entry->unwinder->type);
361
362 ui_out_emit_list tuple_emitter (uiout, nullptr);
363 uiout->field_string ("name", name);
364 uiout->field_string ("type", type);
365 uiout->text ("\n");
366 }
367}
368
370void
372{
373 /* Add "maint info frame-unwinders". */
374 add_cmd ("frame-unwinders",
377 _("List the frame unwinders currently in effect, "
378 "starting with the highest priority."),
380}
int regnum
const char *const name
struct gdbarch * target_gdbarch(void)
obstack * gdbarch_obstack(gdbarch *arch)
void set(unsigned key, void *datum)
Definition registry.h:204
void * get(unsigned key)
Definition registry.h:211
void field_string(const char *fldname, const char *string, const ui_file_style &style=ui_file_style())
Definition ui-out.c:511
void text(const char *string)
Definition ui-out.c:566
void table_header(int width, ui_align align, const std::string &col_name, const std::string &col_hdr)
Definition ui-out.c:363
void table_body()
Definition ui-out.c:376
struct cmd_list_element * maintenanceinfolist
Definition cli-cmds.c:147
struct cmd_list_element * add_cmd(const char *name, enum command_class theclass, const char *doc, struct cmd_list_element **list)
Definition cli-decode.c:233
@ class_maintenance
Definition command.h:65
static void store_unsigned_integer(gdb_byte *addr, int len, enum bfd_endian byte_order, ULONGEST val)
Definition defs.h:515
@ not_lval
Definition defs.h:361
const struct frame_unwind dummy_frame_unwind
struct value * value_of_register_lazy(frame_info_ptr frame, int regnum)
Definition findvar.c:273
const struct frame_id outer_frame_id
Definition frame.c:689
const struct frame_unwind dwarf2_tailcall_frame_unwind
int default_frame_sniffer(const struct frame_unwind *self, frame_info_ptr this_frame, void **this_prologue_cache)
struct value * frame_unwind_got_memory(frame_info_ptr frame, int regnum, CORE_ADDR addr)
CORE_ADDR default_unwind_pc(struct gdbarch *gdbarch, frame_info_ptr next_frame)
static int frame_unwind_try_unwinder(frame_info_ptr this_frame, void **this_cache, const struct frame_unwind *unwinder)
struct value * frame_unwind_got_optimized(frame_info_ptr frame, int regnum)
void frame_unwind_find_by_frame(frame_info_ptr this_frame, void **this_cache)
struct value * frame_unwind_got_register(frame_info_ptr frame, int regnum, int new_regnum)
static void maintenance_info_frame_unwinders(const char *args, int from_tty)
void frame_unwind_prepend_unwinder(struct gdbarch *gdbarch, const struct frame_unwind *unwinder)
struct value * frame_unwind_got_address(frame_info_ptr frame, int regnum, CORE_ADDR addr)
void _initialize_frame_unwind()
enum unwind_stop_reason default_frame_unwind_stop_reason(frame_info_ptr this_frame, void **this_cache)
static struct frame_unwind_table_entry ** add_unwinder(struct obstack *obstack, const struct frame_unwind *unwinder, struct frame_unwind_table_entry **link)
struct value * frame_unwind_got_bytes(frame_info_ptr frame, int regnum, const gdb_byte *buf)
static const registry< gdbarch >::key< struct frame_unwind_table > frame_unwind_data
static struct frame_unwind_table * get_frame_unwind_table(struct gdbarch *gdbarch)
struct value * frame_unwind_got_constant(frame_info_ptr frame, int regnum, ULONGEST val)
CORE_ADDR default_unwind_sp(struct gdbarch *gdbarch, frame_info_ptr next_frame)
void frame_unwind_append_unwinder(struct gdbarch *gdbarch, const struct frame_unwind *unwinder)
int frame_relative_level(frame_info_ptr fi)
Definition frame.c:2946
void frame_cleanup_after_sniffer(frame_info_ptr frame)
Definition frame.c:3228
ULONGEST frame_unwind_register_unsigned(frame_info_ptr next_frame, int regnum)
Definition frame.c:1371
const char * frame_type_str(frame_type type)
Definition frame.c:447
struct gdbarch * frame_unwind_arch(frame_info_ptr next_frame)
Definition frame.c:3033
struct gdbarch * get_frame_arch(frame_info_ptr this_frame)
Definition frame.c:3027
void frame_prepare_for_sniffer(frame_info_ptr frame, const struct frame_unwind *unwind)
Definition frame.c:3258
unsigned int get_frame_cache_generation()
Definition frame.c:66
struct frame_id get_frame_id(frame_info_ptr fi)
Definition frame.c:631
#define frame_debug_printf(fmt,...)
Definition frame.h:120
unwind_stop_reason
Definition frame.h:653
#define FRAME_SCOPED_DEBUG_ENTER_EXIT
Definition frame.h:125
@ sp_regnum
Definition frv-tdep.h:35
@ pc_regnum
Definition frv-tdep.h:46
int gdbarch_pc_regnum(struct gdbarch *gdbarch)
Definition gdbarch.c:2054
enum bfd_endian gdbarch_byte_order(struct gdbarch *gdbarch)
Definition gdbarch.c:1396
int gdbarch_sp_regnum(struct gdbarch *gdbarch)
Definition gdbarch.c:2037
CORE_ADDR gdbarch_addr_bits_remove(struct gdbarch *gdbarch, CORE_ADDR addr)
Definition gdbarch.c:3152
#define GDBARCH_OBSTACK_ZALLOC(GDBARCH, TYPE)
Definition gdbarch.h:327
const struct frame_unwind inline_frame_unwind
int register_size(struct gdbarch *gdbarch, int regnum)
Definition regcache.c:170
struct type * register_type(struct gdbarch *gdbarch, int regnum)
Definition regcache.c:158
struct frame_unwind_table_entry * next
const struct frame_unwind * unwinder
struct frame_unwind_table_entry * list
struct frame_unwind_table_entry ** osabi_head
frame_sniffer_ftype * sniffer
const char * name
Definition value.h:130
static struct value * zero(struct type *type, enum lval_type lv)
Definition value.c:3426
static struct value * allocate_optimized_out(struct type *type)
Definition value.c:997
gdb::array_view< gdb_byte > contents_writeable()
Definition value.c:1271
void set_stack(bool val)
Definition value.h:320
gdb::array_view< gdb_byte > contents_raw()
Definition value.c:1009
const struct frame_unwind * target_get_tailcall_unwinder(void)
Definition target.c:4238
const struct frame_unwind * target_get_unwinder(void)
Definition target.c:4230
@ ui_left
Definition ui-out.h:45
#define current_uiout
Definition ui-out.h:40
struct value * value_at_lazy(struct type *type, CORE_ADDR addr, frame_info_ptr frame)
Definition valops.c:1036
void pack_long(gdb_byte *buf, struct type *type, LONGEST num)
Definition value.c:3327