Fix null pointer dereference in process_debug_info()
[binutils-gdb.git] / gdb / frame-unwind.c
bloba80421a9c5a86212489e38feec7aa421ff8649dd
1 /* Definitions for 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 #include "frame.h"
21 #include "frame-unwind.h"
22 #include "dummy-frame.h"
23 #include "inline-frame.h"
24 #include "value.h"
25 #include "regcache.h"
26 #include "gdbsupport/gdb_obstack.h"
27 #include "target.h"
28 #include "gdbarch.h"
29 #include "dwarf2/frame-tailcall.h"
30 #include "cli/cli-cmds.h"
31 #include "inferior.h"
33 struct frame_unwind_table_entry
35 const struct frame_unwind *unwinder;
36 struct frame_unwind_table_entry *next;
39 struct frame_unwind_table
41 struct frame_unwind_table_entry *list = nullptr;
42 /* The head of the OSABI part of the search list. */
43 struct frame_unwind_table_entry **osabi_head = nullptr;
46 static const registry<gdbarch>::key<struct frame_unwind_table>
47 frame_unwind_data;
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. */
52 static struct frame_unwind_table_entry **
53 add_unwinder (struct obstack *obstack, const struct frame_unwind *unwinder,
54 struct frame_unwind_table_entry **link)
56 *link = OBSTACK_ZALLOC (obstack, struct frame_unwind_table_entry);
57 (*link)->unwinder = unwinder;
58 return &(*link)->next;
61 static struct frame_unwind_table *
62 get_frame_unwind_table (struct gdbarch *gdbarch)
64 struct frame_unwind_table *table = frame_unwind_data.get (gdbarch);
65 if (table != nullptr)
66 return table;
68 table = new frame_unwind_table;
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;
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);
86 /* The insertion point for OSABI sniffers. */
87 table->osabi_head = link;
88 frame_unwind_data.set (gdbarch, table);
90 return table;
93 void
94 frame_unwind_prepend_unwinder (struct gdbarch *gdbarch,
95 const struct frame_unwind *unwinder)
97 struct frame_unwind_table *table = get_frame_unwind_table (gdbarch);
98 struct frame_unwind_table_entry *entry;
100 /* Insert the new entry at the start of the list. */
101 entry = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct frame_unwind_table_entry);
102 entry->unwinder = unwinder;
103 entry->next = (*table->osabi_head);
104 (*table->osabi_head) = entry;
107 void
108 frame_unwind_append_unwinder (struct gdbarch *gdbarch,
109 const struct frame_unwind *unwinder)
111 struct frame_unwind_table *table = get_frame_unwind_table (gdbarch);
112 struct frame_unwind_table_entry **ip;
114 /* Find the end of the list and insert the new entry there. */
115 for (ip = table->osabi_head; (*ip) != NULL; ip = &(*ip)->next);
116 (*ip) = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct frame_unwind_table_entry);
117 (*ip)->unwinder = unwinder;
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. */
124 static int
125 frame_unwind_try_unwinder (const frame_info_ptr &this_frame, void **this_cache,
126 const struct frame_unwind *unwinder)
128 int res = 0;
130 unsigned int entry_generation = get_frame_cache_generation ();
132 frame_prepare_for_sniffer (this_frame, unwinder);
136 frame_debug_printf ("trying unwinder \"%s\"", unwinder->name);
137 res = unwinder->sniffer (unwinder, this_frame, this_cache);
139 catch (const gdb_exception &ex)
141 frame_debug_printf ("caught exception: %s", ex.message->c_str ());
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)
149 *this_cache = NULL;
150 frame_cleanup_after_sniffer (this_frame);
153 if (ex.error == NOT_AVAILABLE_ERROR)
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;
161 throw;
164 if (res)
166 frame_debug_printf ("yes");
167 return 1;
169 else
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;
177 gdb_assert_not_reached ("frame_unwind_try_unwinder");
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. */
184 void
185 frame_unwind_find_by_frame (const frame_info_ptr &this_frame, void **this_cache)
187 FRAME_SCOPED_DEBUG_ENTER_EXIT;
188 frame_debug_printf ("this_frame=%d", frame_relative_level (this_frame));
190 struct gdbarch *gdbarch = get_frame_arch (this_frame);
191 struct frame_unwind_table *table = get_frame_unwind_table (gdbarch);
192 struct frame_unwind_table_entry *entry;
193 const struct frame_unwind *unwinder_from_target;
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;
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;
207 for (entry = table->list; entry != NULL; entry = entry->next)
208 if (frame_unwind_try_unwinder (this_frame, this_cache, entry->unwinder))
209 return;
211 internal_error (_("frame_unwind_find_by_frame failed"));
214 /* A default frame sniffer which always accepts the frame. Used by
215 fallback prologue unwinders. */
218 default_frame_sniffer (const struct frame_unwind *self,
219 const frame_info_ptr &this_frame,
220 void **this_prologue_cache)
222 return 1;
225 /* The default frame unwinder stop_reason callback. */
227 enum unwind_stop_reason
228 default_frame_unwind_stop_reason (const frame_info_ptr &this_frame,
229 void **this_cache)
231 struct frame_id this_id = get_frame_id (this_frame);
233 if (this_id == outer_frame_id)
234 return UNWIND_OUTERMOST;
235 else
236 return UNWIND_NO_REASON;
239 /* See frame-unwind.h. */
241 CORE_ADDR
242 default_unwind_pc (struct gdbarch *gdbarch, const frame_info_ptr &next_frame)
244 int pc_regnum = gdbarch_pc_regnum (gdbarch);
245 CORE_ADDR pc = frame_unwind_register_unsigned (next_frame, pc_regnum);
246 pc = gdbarch_addr_bits_remove (gdbarch, pc);
247 return pc;
250 /* See frame-unwind.h. */
252 CORE_ADDR
253 default_unwind_sp (struct gdbarch *gdbarch, const frame_info_ptr &next_frame)
255 int sp_regnum = gdbarch_sp_regnum (gdbarch);
256 return frame_unwind_register_unsigned (next_frame, sp_regnum);
259 /* Helper functions for value-based register unwinding. These return
260 a (possibly lazy) value of the appropriate type. */
262 /* Return a value which indicates that FRAME did not save REGNUM. */
264 struct value *
265 frame_unwind_got_optimized (const frame_info_ptr &frame, int regnum)
267 struct gdbarch *gdbarch = frame_unwind_arch (frame);
268 struct type *type = register_type (gdbarch, regnum);
270 return value::allocate_optimized_out (type);
273 /* Return a value which indicates that FRAME copied REGNUM into
274 register NEW_REGNUM. */
276 struct value *
277 frame_unwind_got_register (const frame_info_ptr &frame,
278 int regnum, int new_regnum)
280 return value_of_register_lazy (get_next_frame_sentinel_okay (frame),
281 new_regnum);
284 /* Return a value which indicates that FRAME saved REGNUM in memory at
285 ADDR. */
287 struct value *
288 frame_unwind_got_memory (const frame_info_ptr &frame, int regnum, CORE_ADDR addr)
290 struct gdbarch *gdbarch = frame_unwind_arch (frame);
291 struct value *v = value_at_lazy (register_type (gdbarch, regnum), addr);
293 v->set_stack (true);
294 return v;
297 /* Return a value which indicates that FRAME's saved version of
298 REGNUM has a known constant (computed) value of VAL. */
300 struct value *
301 frame_unwind_got_constant (const frame_info_ptr &frame, int regnum,
302 ULONGEST val)
304 struct gdbarch *gdbarch = frame_unwind_arch (frame);
305 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
306 struct value *reg_val;
308 reg_val = value::zero (register_type (gdbarch, regnum), not_lval);
309 store_unsigned_integer (reg_val->contents_writeable ().data (),
310 register_size (gdbarch, regnum), byte_order, val);
311 return reg_val;
314 struct value *
315 frame_unwind_got_bytes (const frame_info_ptr &frame, int regnum, const gdb_byte *buf)
317 struct gdbarch *gdbarch = frame_unwind_arch (frame);
318 struct value *reg_val;
320 reg_val = value::zero (register_type (gdbarch, regnum), not_lval);
321 memcpy (reg_val->contents_raw ().data (), buf,
322 register_size (gdbarch, regnum));
323 return reg_val;
326 /* Return a value which indicates that FRAME's saved version of REGNUM
327 has a known constant (computed) value of ADDR. Convert the
328 CORE_ADDR to a target address if necessary. */
330 struct value *
331 frame_unwind_got_address (const frame_info_ptr &frame, int regnum,
332 CORE_ADDR addr)
334 struct gdbarch *gdbarch = frame_unwind_arch (frame);
335 struct value *reg_val;
337 reg_val = value::zero (register_type (gdbarch, regnum), not_lval);
338 pack_long (reg_val->contents_writeable ().data (),
339 register_type (gdbarch, regnum), addr);
340 return reg_val;
343 /* Implement "maintenance info frame-unwinders" command. */
345 static void
346 maintenance_info_frame_unwinders (const char *args, int from_tty)
348 gdbarch *gdbarch = current_inferior ()->arch ();
349 struct frame_unwind_table *table = get_frame_unwind_table (gdbarch);
351 ui_out *uiout = current_uiout;
352 ui_out_emit_table table_emitter (uiout, 2, -1, "FrameUnwinders");
353 uiout->table_header (27, ui_left, "name", "Name");
354 uiout->table_header (25, ui_left, "type", "Type");
355 uiout->table_body ();
357 for (struct frame_unwind_table_entry *entry = table->list; entry != NULL;
358 entry = entry->next)
360 const char *name = entry->unwinder->name;
361 const char *type = frame_type_str (entry->unwinder->type);
363 ui_out_emit_list tuple_emitter (uiout, nullptr);
364 uiout->field_string ("name", name);
365 uiout->field_string ("type", type);
366 uiout->text ("\n");
370 void _initialize_frame_unwind ();
371 void
372 _initialize_frame_unwind ()
374 /* Add "maint info frame-unwinders". */
375 add_cmd ("frame-unwinders",
376 class_maintenance,
377 maintenance_info_frame_unwinders,
378 _("List the frame unwinders currently in effect, "
379 "starting with the highest priority."),
380 &maintenanceinfolist);