Remove path name from test case
[binutils-gdb.git] / gdb / frame-unwind.c
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"
30 #include "dwarf2/frame-tailcall.h"
31 #include "cli/cli-cmds.h"
32 #include "inferior.h"
33
34 struct frame_unwind_table_entry
35 {
36 const struct frame_unwind *unwinder;
37 struct frame_unwind_table_entry *next;
38 };
39
40 struct frame_unwind_table
41 {
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;
45 };
46
47 static const registry<gdbarch>::key<struct frame_unwind_table>
48 frame_unwind_data;
49
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. */
52
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)
56 {
57 *link = OBSTACK_ZALLOC (obstack, struct frame_unwind_table_entry);
58 (*link)->unwinder = unwinder;
59 return &(*link)->next;
60 }
61
62 static struct frame_unwind_table *
63 get_frame_unwind_table (struct gdbarch *gdbarch)
64 {
65 struct frame_unwind_table *table = frame_unwind_data.get (gdbarch);
66 if (table != nullptr)
67 return table;
68
69 table = new frame_unwind_table;
70
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;
74
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);
86
87 /* The insertion point for OSABI sniffers. */
88 table->osabi_head = link;
89 frame_unwind_data.set (gdbarch, table);
90
91 return table;
92 }
93
94 void
95 frame_unwind_prepend_unwinder (struct gdbarch *gdbarch,
96 const struct frame_unwind *unwinder)
97 {
98 struct frame_unwind_table *table = get_frame_unwind_table (gdbarch);
99 struct frame_unwind_table_entry *entry;
100
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;
106 }
107
108 void
109 frame_unwind_append_unwinder (struct gdbarch *gdbarch,
110 const struct frame_unwind *unwinder)
111 {
112 struct frame_unwind_table *table = get_frame_unwind_table (gdbarch);
113 struct frame_unwind_table_entry **ip;
114
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;
119 }
120
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. */
124
125 static int
126 frame_unwind_try_unwinder (frame_info_ptr this_frame, void **this_cache,
127 const struct frame_unwind *unwinder)
128 {
129 int res = 0;
130
131 unsigned int entry_generation = get_frame_cache_generation ();
132
133 frame_prepare_for_sniffer (this_frame, unwinder);
134
135 try
136 {
137 frame_debug_printf ("trying unwinder \"%s\"", unwinder->name);
138 res = unwinder->sniffer (unwinder, this_frame, this_cache);
139 }
140 catch (const gdb_exception &ex)
141 {
142 frame_debug_printf ("caught exception: %s", ex.message->c_str ());
143
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)
149 {
150 *this_cache = NULL;
151 frame_cleanup_after_sniffer (this_frame);
152 }
153
154 if (ex.error == NOT_AVAILABLE_ERROR)
155 {
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;
161 }
162 throw;
163 }
164
165 if (res)
166 {
167 frame_debug_printf ("yes");
168 return 1;
169 }
170 else
171 {
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;
177 }
178 gdb_assert_not_reached ("frame_unwind_try_unwinder");
179 }
180
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. */
184
185 void
186 frame_unwind_find_by_frame (frame_info_ptr this_frame, void **this_cache)
187 {
188 FRAME_SCOPED_DEBUG_ENTER_EXIT;
189 frame_debug_printf ("this_frame=%d", frame_relative_level (this_frame));
190
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;
195
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;
201
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;
207
208 for (entry = table->list; entry != NULL; entry = entry->next)
209 if (frame_unwind_try_unwinder (this_frame, this_cache, entry->unwinder))
210 return;
211
212 internal_error (_("frame_unwind_find_by_frame failed"));
213 }
214
215 /* A default frame sniffer which always accepts the frame. Used by
216 fallback prologue unwinders. */
217
218 int
219 default_frame_sniffer (const struct frame_unwind *self,
220 frame_info_ptr this_frame,
221 void **this_prologue_cache)
222 {
223 return 1;
224 }
225
226 /* The default frame unwinder stop_reason callback. */
227
228 enum unwind_stop_reason
229 default_frame_unwind_stop_reason (frame_info_ptr this_frame,
230 void **this_cache)
231 {
232 struct frame_id this_id = get_frame_id (this_frame);
233
234 if (this_id == outer_frame_id)
235 return UNWIND_OUTERMOST;
236 else
237 return UNWIND_NO_REASON;
238 }
239
240 /* See frame-unwind.h. */
241
242 CORE_ADDR
243 default_unwind_pc (struct gdbarch *gdbarch, frame_info_ptr next_frame)
244 {
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;
249 }
250
251 /* See frame-unwind.h. */
252
253 CORE_ADDR
254 default_unwind_sp (struct gdbarch *gdbarch, frame_info_ptr next_frame)
255 {
256 int sp_regnum = gdbarch_sp_regnum (gdbarch);
257 return frame_unwind_register_unsigned (next_frame, sp_regnum);
258 }
259
260 /* Helper functions for value-based register unwinding. These return
261 a (possibly lazy) value of the appropriate type. */
262
263 /* Return a value which indicates that FRAME did not save REGNUM. */
264
265 struct value *
266 frame_unwind_got_optimized (frame_info_ptr frame, int regnum)
267 {
268 struct gdbarch *gdbarch = frame_unwind_arch (frame);
269 struct type *type = register_type (gdbarch, regnum);
270
271 return value::allocate_optimized_out (type);
272 }
273
274 /* Return a value which indicates that FRAME copied REGNUM into
275 register NEW_REGNUM. */
276
277 struct value *
278 frame_unwind_got_register (frame_info_ptr frame,
279 int regnum, int new_regnum)
280 {
281 return value_of_register_lazy (frame, new_regnum);
282 }
283
284 /* Return a value which indicates that FRAME saved REGNUM in memory at
285 ADDR. */
286
287 struct value *
288 frame_unwind_got_memory (frame_info_ptr frame, int regnum, CORE_ADDR addr)
289 {
290 struct gdbarch *gdbarch = frame_unwind_arch (frame);
291 struct value *v = value_at_lazy (register_type (gdbarch, regnum), addr);
292
293 v->set_stack (true);
294 return v;
295 }
296
297 /* Return a value which indicates that FRAME's saved version of
298 REGNUM has a known constant (computed) value of VAL. */
299
300 struct value *
301 frame_unwind_got_constant (frame_info_ptr frame, int regnum,
302 ULONGEST val)
303 {
304 struct gdbarch *gdbarch = frame_unwind_arch (frame);
305 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
306 struct value *reg_val;
307
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;
312 }
313
314 struct value *
315 frame_unwind_got_bytes (frame_info_ptr frame, int regnum, const gdb_byte *buf)
316 {
317 struct gdbarch *gdbarch = frame_unwind_arch (frame);
318 struct value *reg_val;
319
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;
324 }
325
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. */
329
330 struct value *
331 frame_unwind_got_address (frame_info_ptr frame, int regnum,
332 CORE_ADDR addr)
333 {
334 struct gdbarch *gdbarch = frame_unwind_arch (frame);
335 struct value *reg_val;
336
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;
341 }
342
343 /* Implement "maintenance info frame-unwinders" command. */
344
345 static void
346 maintenance_info_frame_unwinders (const char *args, int from_tty)
347 {
348 gdbarch *gdbarch = current_inferior ()->arch ();
349 struct frame_unwind_table *table = get_frame_unwind_table (gdbarch);
350
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 ();
356
357 for (struct frame_unwind_table_entry *entry = table->list; entry != NULL;
358 entry = entry->next)
359 {
360 const char *name = entry->unwinder->name;
361 const char *type = frame_type_str (entry->unwinder->type);
362
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");
367 }
368 }
369
370 void _initialize_frame_unwind ();
371 void
372 _initialize_frame_unwind ()
373 {
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);
381 }