testsuite, threads: fix LD_LIBRARY_PATH in 'tls-sepdebug.exp'
[binutils-gdb.git] / gdb / frame-unwind.c
blobe5f108d3257371a4df74b4281eeb79c5eafebde8
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 "extract-store-integer.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"
30 #include "dwarf2/frame-tailcall.h"
31 #include "cli/cli-cmds.h"
32 #include "inferior.h"
34 struct frame_unwind_table_entry
36 const struct frame_unwind *unwinder;
37 struct frame_unwind_table_entry *next;
40 struct frame_unwind_table
42 struct frame_unwind_table_entry *list = nullptr;
43 /* The head of the OSABI part of the search list. */
44 struct frame_unwind_table_entry **osabi_head = nullptr;
47 static const registry<gdbarch>::key<struct frame_unwind_table>
48 frame_unwind_data;
50 /* A helper function to add an unwinder to a list. LINK says where to
51 install the new unwinder. The new link is returned. */
53 static struct frame_unwind_table_entry **
54 add_unwinder (struct obstack *obstack, const struct frame_unwind *unwinder,
55 struct frame_unwind_table_entry **link)
57 *link = OBSTACK_ZALLOC (obstack, struct frame_unwind_table_entry);
58 (*link)->unwinder = unwinder;
59 return &(*link)->next;
62 static struct frame_unwind_table *
63 get_frame_unwind_table (struct gdbarch *gdbarch)
65 struct frame_unwind_table *table = frame_unwind_data.get (gdbarch);
66 if (table != nullptr)
67 return table;
69 table = new frame_unwind_table;
71 /* Start the table out with a few default sniffers. OSABI code
72 can't override this. */
73 struct frame_unwind_table_entry **link = &table->list;
75 struct obstack *obstack = gdbarch_obstack (gdbarch);
76 link = add_unwinder (obstack, &dummy_frame_unwind, link);
77 /* The DWARF tailcall sniffer must come before the inline sniffer.
78 Otherwise, we can end up in a situation where a DWARF frame finds
79 tailcall information, but then the inline sniffer claims a frame
80 before the tailcall sniffer, resulting in confusion. This is
81 safe to do always because the tailcall sniffer can only ever be
82 activated if the newer frame was created using the DWARF
83 unwinder, and it also found tailcall information. */
84 link = add_unwinder (obstack, &dwarf2_tailcall_frame_unwind, link);
85 link = add_unwinder (obstack, &inline_frame_unwind, link);
87 /* The insertion point for OSABI sniffers. */
88 table->osabi_head = link;
89 frame_unwind_data.set (gdbarch, table);
91 return table;
94 void
95 frame_unwind_prepend_unwinder (struct gdbarch *gdbarch,
96 const struct frame_unwind *unwinder)
98 struct frame_unwind_table *table = get_frame_unwind_table (gdbarch);
99 struct frame_unwind_table_entry *entry;
101 /* Insert the new entry at the start of the list. */
102 entry = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct frame_unwind_table_entry);
103 entry->unwinder = unwinder;
104 entry->next = (*table->osabi_head);
105 (*table->osabi_head) = entry;
108 void
109 frame_unwind_append_unwinder (struct gdbarch *gdbarch,
110 const struct frame_unwind *unwinder)
112 struct frame_unwind_table *table = get_frame_unwind_table (gdbarch);
113 struct frame_unwind_table_entry **ip;
115 /* Find the end of the list and insert the new entry there. */
116 for (ip = table->osabi_head; (*ip) != NULL; ip = &(*ip)->next);
117 (*ip) = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct frame_unwind_table_entry);
118 (*ip)->unwinder = unwinder;
121 /* Call SNIFFER from UNWINDER. If it succeeded set UNWINDER for
122 THIS_FRAME and return 1. Otherwise the function keeps THIS_FRAME
123 unchanged and returns 0. */
125 static int
126 frame_unwind_try_unwinder (const frame_info_ptr &this_frame, void **this_cache,
127 const struct frame_unwind *unwinder)
129 int res = 0;
131 unsigned int entry_generation = get_frame_cache_generation ();
133 frame_prepare_for_sniffer (this_frame, unwinder);
137 frame_debug_printf ("trying unwinder \"%s\"", unwinder->name);
138 res = unwinder->sniffer (unwinder, this_frame, this_cache);
140 catch (const gdb_exception &ex)
142 frame_debug_printf ("caught exception: %s", ex.message->c_str ());
144 /* Catch all exceptions, caused by either interrupt or error.
145 Reset *THIS_CACHE, unless something reinitialized the frame
146 cache meanwhile, in which case THIS_FRAME/THIS_CACHE are now
147 dangling. */
148 if (get_frame_cache_generation () == entry_generation)
150 *this_cache = NULL;
151 frame_cleanup_after_sniffer (this_frame);
154 if (ex.error == NOT_AVAILABLE_ERROR)
156 /* This usually means that not even the PC is available,
157 thus most unwinders aren't able to determine if they're
158 the best fit. Keep trying. Fallback prologue unwinders
159 should always accept the frame. */
160 return 0;
162 throw;
165 if (res)
167 frame_debug_printf ("yes");
168 return 1;
170 else
172 frame_debug_printf ("no");
173 /* Don't set *THIS_CACHE to NULL here, because sniffer has to do
174 so. */
175 frame_cleanup_after_sniffer (this_frame);
176 return 0;
178 gdb_assert_not_reached ("frame_unwind_try_unwinder");
181 /* Iterate through sniffers for THIS_FRAME frame until one returns with an
182 unwinder implementation. THIS_FRAME->UNWIND must be NULL, it will get set
183 by this function. Possibly initialize THIS_CACHE. */
185 void
186 frame_unwind_find_by_frame (const frame_info_ptr &this_frame, void **this_cache)
188 FRAME_SCOPED_DEBUG_ENTER_EXIT;
189 frame_debug_printf ("this_frame=%d", frame_relative_level (this_frame));
191 struct gdbarch *gdbarch = get_frame_arch (this_frame);
192 struct frame_unwind_table *table = get_frame_unwind_table (gdbarch);
193 struct frame_unwind_table_entry *entry;
194 const struct frame_unwind *unwinder_from_target;
196 unwinder_from_target = target_get_unwinder ();
197 if (unwinder_from_target != NULL
198 && frame_unwind_try_unwinder (this_frame, this_cache,
199 unwinder_from_target))
200 return;
202 unwinder_from_target = target_get_tailcall_unwinder ();
203 if (unwinder_from_target != NULL
204 && frame_unwind_try_unwinder (this_frame, this_cache,
205 unwinder_from_target))
206 return;
208 for (entry = table->list; entry != NULL; entry = entry->next)
209 if (frame_unwind_try_unwinder (this_frame, this_cache, entry->unwinder))
210 return;
212 internal_error (_("frame_unwind_find_by_frame failed"));
215 /* A default frame sniffer which always accepts the frame. Used by
216 fallback prologue unwinders. */
219 default_frame_sniffer (const struct frame_unwind *self,
220 const frame_info_ptr &this_frame,
221 void **this_prologue_cache)
223 return 1;
226 /* The default frame unwinder stop_reason callback. */
228 enum unwind_stop_reason
229 default_frame_unwind_stop_reason (const frame_info_ptr &this_frame,
230 void **this_cache)
232 struct frame_id this_id = get_frame_id (this_frame);
234 if (this_id == outer_frame_id)
235 return UNWIND_OUTERMOST;
236 else
237 return UNWIND_NO_REASON;
240 /* See frame-unwind.h. */
242 CORE_ADDR
243 default_unwind_pc (struct gdbarch *gdbarch, const frame_info_ptr &next_frame)
245 int pc_regnum = gdbarch_pc_regnum (gdbarch);
246 CORE_ADDR pc = frame_unwind_register_unsigned (next_frame, pc_regnum);
247 pc = gdbarch_addr_bits_remove (gdbarch, pc);
248 return pc;
251 /* See frame-unwind.h. */
253 CORE_ADDR
254 default_unwind_sp (struct gdbarch *gdbarch, const frame_info_ptr &next_frame)
256 int sp_regnum = gdbarch_sp_regnum (gdbarch);
257 return frame_unwind_register_unsigned (next_frame, sp_regnum);
260 /* Helper functions for value-based register unwinding. These return
261 a (possibly lazy) value of the appropriate type. */
263 /* Return a value which indicates that FRAME did not save REGNUM. */
265 struct value *
266 frame_unwind_got_optimized (const frame_info_ptr &frame, int regnum)
268 struct gdbarch *gdbarch = frame_unwind_arch (frame);
269 struct type *type = register_type (gdbarch, regnum);
271 return value::allocate_optimized_out (type);
274 /* Return a value which indicates that FRAME copied REGNUM into
275 register NEW_REGNUM. */
277 struct value *
278 frame_unwind_got_register (const frame_info_ptr &frame,
279 int regnum, int new_regnum)
281 return value_of_register_lazy (get_next_frame_sentinel_okay (frame),
282 new_regnum);
285 /* Return a value which indicates that FRAME saved REGNUM in memory at
286 ADDR. */
288 struct value *
289 frame_unwind_got_memory (const frame_info_ptr &frame, int regnum, CORE_ADDR addr)
291 struct gdbarch *gdbarch = frame_unwind_arch (frame);
292 struct value *v = value_at_lazy (register_type (gdbarch, regnum), addr);
294 v->set_stack (true);
295 return v;
298 /* Return a value which indicates that FRAME's saved version of
299 REGNUM has a known constant (computed) value of VAL. */
301 struct value *
302 frame_unwind_got_constant (const frame_info_ptr &frame, int regnum,
303 ULONGEST val)
305 struct gdbarch *gdbarch = frame_unwind_arch (frame);
306 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
307 struct value *reg_val;
309 reg_val = value::zero (register_type (gdbarch, regnum), not_lval);
310 store_unsigned_integer (reg_val->contents_writeable ().data (),
311 register_size (gdbarch, regnum), byte_order, val);
312 return reg_val;
315 struct value *
316 frame_unwind_got_bytes (const frame_info_ptr &frame, int regnum, const gdb_byte *buf)
318 struct gdbarch *gdbarch = frame_unwind_arch (frame);
319 struct value *reg_val;
321 reg_val = value::zero (register_type (gdbarch, regnum), not_lval);
322 memcpy (reg_val->contents_raw ().data (), buf,
323 register_size (gdbarch, regnum));
324 return reg_val;
327 /* Return a value which indicates that FRAME's saved version of REGNUM
328 has a known constant (computed) value of ADDR. Convert the
329 CORE_ADDR to a target address if necessary. */
331 struct value *
332 frame_unwind_got_address (const frame_info_ptr &frame, int regnum,
333 CORE_ADDR addr)
335 struct gdbarch *gdbarch = frame_unwind_arch (frame);
336 struct value *reg_val;
338 reg_val = value::zero (register_type (gdbarch, regnum), not_lval);
339 pack_long (reg_val->contents_writeable ().data (),
340 register_type (gdbarch, regnum), addr);
341 return reg_val;
344 /* Implement "maintenance info frame-unwinders" command. */
346 static void
347 maintenance_info_frame_unwinders (const char *args, int from_tty)
349 gdbarch *gdbarch = current_inferior ()->arch ();
350 struct frame_unwind_table *table = get_frame_unwind_table (gdbarch);
352 ui_out *uiout = current_uiout;
353 ui_out_emit_table table_emitter (uiout, 2, -1, "FrameUnwinders");
354 uiout->table_header (27, ui_left, "name", "Name");
355 uiout->table_header (25, ui_left, "type", "Type");
356 uiout->table_body ();
358 for (struct frame_unwind_table_entry *entry = table->list; entry != NULL;
359 entry = entry->next)
361 const char *name = entry->unwinder->name;
362 const char *type = frame_type_str (entry->unwinder->type);
364 ui_out_emit_list tuple_emitter (uiout, nullptr);
365 uiout->field_string ("name", name);
366 uiout->field_string ("type", type);
367 uiout->text ("\n");
371 void _initialize_frame_unwind ();
372 void
373 _initialize_frame_unwind ()
375 /* Add "maint info frame-unwinders". */
376 add_cmd ("frame-unwinders",
377 class_maintenance,
378 maintenance_info_frame_unwinders,
379 _("List the frame unwinders currently in effect, "
380 "starting with the highest priority."),
381 &maintenanceinfolist);