]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/frame-unwind.c
95768fa2f4e20ca9c5a872a68474d7a7ea6bb680
[thirdparty/binutils-gdb.git] / gdb / frame-unwind.c
1 /* Definitions for frame unwinder, for GDB, the GNU debugger.
2
3 Copyright (C) 2003-2025 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 "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 "cli/cli-option.h"
33 #include "inferior.h"
34
35 /* Conversion list between the enum for frame_unwind_class and
36 string. */
37 static const char * unwind_class_conversion[] =
38 {
39 "GDB",
40 "EXTENSION",
41 "DEBUGINFO",
42 "ARCH",
43 nullptr
44 };
45
46 /* Default sniffers, that must always be the first in the unwinder list,
47 no matter the architecture. */
48 static constexpr std::initializer_list<const frame_unwind *>
49 standard_unwinders =
50 {
51 &dummy_frame_unwind,
52 #if defined(DWARF_FORMAT_AVAILABLE)
53 /* The DWARF tailcall sniffer must come before the inline sniffer.
54 Otherwise, we can end up in a situation where a DWARF frame finds
55 tailcall information, but then the inline sniffer claims a frame
56 before the tailcall sniffer, resulting in confusion. This is
57 safe to do always because the tailcall sniffer can only ever be
58 activated if the newer frame was created using the DWARF
59 unwinder, and it also found tailcall information. */
60 &dwarf2_tailcall_frame_unwind,
61 #endif
62 &inline_frame_unwind,
63 };
64
65 /* If an unwinder should be prepended to the list, this is the
66 index in which it should be inserted. */
67 static constexpr int prepend_unwinder_index = standard_unwinders.size ();
68
69 static const registry<gdbarch>::key<std::vector<const frame_unwind *>>
70 frame_unwind_data;
71
72 /* Retrieve the list of frame unwinders available in GDBARCH.
73 If this list is empty, it is initialized before being returned. */
74 static std::vector<const frame_unwind *> &
75 get_frame_unwind_table (struct gdbarch *gdbarch)
76 {
77 std::vector<const frame_unwind *> *table = frame_unwind_data.get (gdbarch);
78 if (table == nullptr)
79 table = frame_unwind_data.emplace (gdbarch,
80 standard_unwinders.begin (),
81 standard_unwinders.end ());
82 return *table;
83 }
84
85 static const char *
86 frame_unwinder_class_str (frame_unwind_class uclass)
87 {
88 gdb_assert (uclass < UNWIND_CLASS_NUMBER);
89 return unwind_class_conversion[uclass];
90 }
91
92 /* Case insensitive search for a frame_unwind_class based on the given
93 string. */
94 static enum frame_unwind_class
95 str_to_frame_unwind_class (const char *class_str)
96 {
97 for (int i = 0; i < UNWIND_CLASS_NUMBER; i++)
98 {
99 if (strcasecmp (unwind_class_conversion[i], class_str) == 0)
100 return (frame_unwind_class) i;
101 }
102
103 error (_("Unknown frame unwind class: %s"), class_str);
104 }
105
106 void
107 frame_unwind_prepend_unwinder (struct gdbarch *gdbarch,
108 const struct frame_unwind *unwinder)
109 {
110 std::vector<const frame_unwind *> &table = get_frame_unwind_table (gdbarch);
111
112 table.insert (table.begin () + prepend_unwinder_index, unwinder);
113 }
114
115 void
116 frame_unwind_append_unwinder (struct gdbarch *gdbarch,
117 const struct frame_unwind *unwinder)
118 {
119 get_frame_unwind_table (gdbarch).push_back (unwinder);
120 }
121
122 /* Call SNIFFER from UNWINDER. If it succeeded set UNWINDER for
123 THIS_FRAME and return true. Otherwise the function keeps THIS_FRAME
124 unchanged and returns false. */
125
126 static bool
127 frame_unwind_try_unwinder (const frame_info_ptr &this_frame, void **this_cache,
128 const struct frame_unwind *unwinder)
129 {
130 int res = 0;
131
132 unsigned int entry_generation = get_frame_cache_generation ();
133
134 frame_prepare_for_sniffer (this_frame, unwinder);
135
136 try
137 {
138 frame_debug_printf ("trying unwinder \"%s\"", unwinder->name ());
139 res = unwinder->sniff (this_frame, this_cache);
140 }
141 catch (const gdb_exception &ex)
142 {
143 frame_debug_printf ("caught exception: %s", ex.message->c_str ());
144
145 /* Catch all exceptions, caused by either interrupt or error.
146 Reset *THIS_CACHE, unless something reinitialized the frame
147 cache meanwhile, in which case THIS_FRAME/THIS_CACHE are now
148 dangling. */
149 if (get_frame_cache_generation () == entry_generation)
150 {
151 *this_cache = NULL;
152 frame_cleanup_after_sniffer (this_frame);
153 }
154
155 if (ex.error == NOT_AVAILABLE_ERROR)
156 {
157 /* This usually means that not even the PC is available,
158 thus most unwinders aren't able to determine if they're
159 the best fit. Keep trying. Fallback prologue unwinders
160 should always accept the frame. */
161 return false;
162 }
163 throw;
164 }
165
166 if (res)
167 {
168 frame_debug_printf ("yes");
169 return true;
170 }
171 else
172 {
173 frame_debug_printf ("no");
174 /* Don't set *THIS_CACHE to NULL here, because sniffer has to do
175 so. */
176 frame_cleanup_after_sniffer (this_frame);
177 return false;
178 }
179 gdb_assert_not_reached ("frame_unwind_try_unwinder");
180 }
181
182 /* Iterate through sniffers for THIS_FRAME frame until one returns with an
183 unwinder implementation. THIS_FRAME->UNWIND must be NULL, it will get set
184 by this function. Possibly initialize THIS_CACHE. */
185
186 void
187 frame_unwind_find_by_frame (const frame_info_ptr &this_frame, void **this_cache)
188 {
189 FRAME_SCOPED_DEBUG_ENTER_EXIT;
190 frame_debug_printf ("this_frame=%d", frame_relative_level (this_frame));
191
192 /* If we see a disabled unwinder, we assume some test is being run on
193 GDB, and we don't want to internal_error at the end of this function. */
194 bool seen_disabled_unwinder = false;
195 /* Lambda to factor out the logic of checking if an unwinder is enabled,
196 testing it and otherwise recording if we saw a disable unwinder. */
197 auto test_unwinder = [&] (const struct frame_unwind *unwinder)
198 {
199 if (unwinder == nullptr)
200 return false;
201
202 if (!unwinder->enabled ())
203 {
204 seen_disabled_unwinder = true;
205 return false;
206 }
207
208 return frame_unwind_try_unwinder (this_frame,
209 this_cache,
210 unwinder);
211 };
212
213 if (test_unwinder (target_get_unwinder ()))
214 return;
215
216 if (test_unwinder (target_get_tailcall_unwinder ()))
217 return;
218
219 struct gdbarch *gdbarch = get_frame_arch (this_frame);
220 std::vector<const frame_unwind *> &table = get_frame_unwind_table (gdbarch);
221 for (const auto &unwinder : table)
222 {
223 if (test_unwinder (unwinder))
224 return;
225 }
226
227 if (seen_disabled_unwinder)
228 error (_("Required frame unwinder may have been disabled"
229 ", see 'maint info frame-unwinders'"));
230 else
231 internal_error (_("frame_unwind_find_by_frame failed"));
232 }
233
234 /* A default frame sniffer which always accepts the frame. Used by
235 fallback prologue unwinders. */
236
237 int
238 default_frame_sniffer (const struct frame_unwind *self,
239 const frame_info_ptr &this_frame,
240 void **this_prologue_cache)
241 {
242 return 1;
243 }
244
245 /* The default frame unwinder stop_reason callback. */
246
247 enum unwind_stop_reason
248 default_frame_unwind_stop_reason (const frame_info_ptr &this_frame,
249 void **this_cache)
250 {
251 struct frame_id this_id = get_frame_id (this_frame);
252
253 if (this_id == outer_frame_id)
254 return UNWIND_OUTERMOST;
255 else
256 return UNWIND_NO_REASON;
257 }
258
259 /* See frame-unwind.h. */
260
261 CORE_ADDR
262 default_unwind_pc (struct gdbarch *gdbarch, const frame_info_ptr &next_frame)
263 {
264 int pc_regnum = gdbarch_pc_regnum (gdbarch);
265 CORE_ADDR pc = frame_unwind_register_unsigned (next_frame, pc_regnum);
266 pc = gdbarch_addr_bits_remove (gdbarch, pc);
267 return pc;
268 }
269
270 /* See frame-unwind.h. */
271
272 CORE_ADDR
273 default_unwind_sp (struct gdbarch *gdbarch, const frame_info_ptr &next_frame)
274 {
275 int sp_regnum = gdbarch_sp_regnum (gdbarch);
276 return frame_unwind_register_unsigned (next_frame, sp_regnum);
277 }
278
279 /* Helper functions for value-based register unwinding. These return
280 a (possibly lazy) value of the appropriate type. */
281
282 /* Return a value which indicates that FRAME did not save REGNUM. */
283
284 struct value *
285 frame_unwind_got_optimized (const frame_info_ptr &frame, int regnum)
286 {
287 struct gdbarch *gdbarch = frame_unwind_arch (frame);
288 struct type *type = register_type (gdbarch, regnum);
289
290 return value::allocate_optimized_out (type);
291 }
292
293 /* Return a value which indicates that FRAME copied REGNUM into
294 register NEW_REGNUM. */
295
296 struct value *
297 frame_unwind_got_register (const frame_info_ptr &frame,
298 int regnum, int new_regnum)
299 {
300 return value_of_register_lazy (get_next_frame_sentinel_okay (frame),
301 new_regnum);
302 }
303
304 /* Return a value which indicates that FRAME saved REGNUM in memory at
305 ADDR. */
306
307 struct value *
308 frame_unwind_got_memory (const frame_info_ptr &frame, int regnum, CORE_ADDR addr)
309 {
310 struct gdbarch *gdbarch = frame_unwind_arch (frame);
311 struct value *v = value_at_lazy (register_type (gdbarch, regnum), addr);
312
313 v->set_stack (true);
314 return v;
315 }
316
317 /* Return a value which indicates that FRAME's saved version of
318 REGNUM has a known constant (computed) value of VAL. */
319
320 struct value *
321 frame_unwind_got_constant (const frame_info_ptr &frame, int regnum,
322 ULONGEST val)
323 {
324 struct gdbarch *gdbarch = frame_unwind_arch (frame);
325 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
326 struct value *reg_val;
327
328 reg_val = value::zero (register_type (gdbarch, regnum), not_lval);
329 store_unsigned_integer (reg_val->contents_writeable ().data (),
330 register_size (gdbarch, regnum), byte_order, val);
331 return reg_val;
332 }
333
334 struct value *
335 frame_unwind_got_bytes (const frame_info_ptr &frame, int regnum,
336 gdb::array_view<const gdb_byte> buf)
337 {
338 struct gdbarch *gdbarch = frame_unwind_arch (frame);
339 struct value *reg_val;
340
341 reg_val = value::zero (register_type (gdbarch, regnum), not_lval);
342 gdb::array_view<gdb_byte> val_contents = reg_val->contents_raw ();
343
344 /* The value's contents buffer is zeroed on allocation so if buf is
345 smaller, the remaining space will be filled with zero.
346
347 This can happen when unwinding through signal frames. For example, if
348 an AArch64 program doesn't use SVE, then the Linux kernel will only
349 save in the signal frame the first 128 bits of the vector registers,
350 which is their minimum size, even if the vector length says they're
351 bigger. */
352 gdb_assert (buf.size () <= val_contents.size ());
353
354 memcpy (val_contents.data (), buf.data (), buf.size ());
355 return reg_val;
356 }
357
358 /* Return a value which indicates that FRAME's saved version of REGNUM
359 has a known constant (computed) value of ADDR. Convert the
360 CORE_ADDR to a target address if necessary. */
361
362 struct value *
363 frame_unwind_got_address (const frame_info_ptr &frame, int regnum,
364 CORE_ADDR addr)
365 {
366 struct gdbarch *gdbarch = frame_unwind_arch (frame);
367 struct value *reg_val;
368
369 reg_val = value::zero (register_type (gdbarch, regnum), not_lval);
370 pack_long (reg_val->contents_writeable ().data (),
371 register_type (gdbarch, regnum), addr);
372 return reg_val;
373 }
374
375 /* See frame-unwind.h. */
376
377 enum unwind_stop_reason
378 frame_unwind_legacy::stop_reason (const frame_info_ptr &this_frame,
379 void **this_prologue_cache) const
380 {
381 return m_stop_reason (this_frame, this_prologue_cache);
382 }
383
384 /* See frame-unwind.h. */
385
386 void
387 frame_unwind_legacy::this_id (const frame_info_ptr &this_frame,
388 void **this_prologue_cache,
389 struct frame_id *id) const
390 {
391 return m_this_id (this_frame, this_prologue_cache, id);
392 }
393
394 /* See frame-unwind.h. */
395
396 struct value *
397 frame_unwind_legacy::prev_register (const frame_info_ptr &this_frame,
398 void **this_prologue_cache,
399 int regnum) const
400 {
401 return m_prev_register (this_frame, this_prologue_cache, regnum);
402 }
403
404 /* See frame-unwind.h. */
405
406 int
407 frame_unwind_legacy::sniff (const frame_info_ptr &this_frame,
408 void **this_prologue_cache) const
409 {
410 return m_sniffer (this, this_frame, this_prologue_cache);
411 }
412
413 /* See frame-unwind.h. */
414
415 void
416 frame_unwind_legacy::dealloc_cache (frame_info *self, void *this_cache) const
417 {
418 if (m_dealloc_cache != nullptr)
419 m_dealloc_cache (self, this_cache);
420 }
421
422 /* See frame-unwind.h. */
423
424 struct gdbarch *
425 frame_unwind_legacy::prev_arch (const frame_info_ptr &this_frame,
426 void **this_prologue_cache) const
427 {
428 if (m_prev_arch == nullptr)
429 return frame_unwind::prev_arch (this_frame, this_prologue_cache);
430 return m_prev_arch (this_frame, this_prologue_cache);
431 }
432
433 /* Implement "maintenance info frame-unwinders" command. */
434
435 static void
436 maintenance_info_frame_unwinders (const char *args, int from_tty)
437 {
438 gdbarch *gdbarch = current_inferior ()->arch ();
439 std::vector<const frame_unwind *> &table = get_frame_unwind_table (gdbarch);
440
441 ui_out *uiout = current_uiout;
442 ui_out_emit_table table_emitter (uiout, 4, -1, "FrameUnwinders");
443 uiout->table_header (27, ui_left, "name", "Name");
444 uiout->table_header (25, ui_left, "type", "Type");
445 uiout->table_header (9, ui_left, "class", "Class");
446 uiout->table_header (8, ui_left, "enabled", "Enabled");
447 uiout->table_body ();
448
449 for (const auto &unwinder : table)
450 {
451 ui_out_emit_list tuple_emitter (uiout, nullptr);
452 uiout->field_string ("name", unwinder->name ());
453 uiout->field_string ("type", frame_type_str (unwinder->type ()));
454 uiout->field_string ("class", frame_unwinder_class_str (
455 unwinder->unwinder_class ()));
456 uiout->field_string ("enabled", unwinder->enabled () ? "Y" : "N");
457 uiout->text ("\n");
458 }
459 }
460
461 /* Options for disabling frame unwinders. */
462 struct maint_frame_unwind_options
463 {
464 std::string unwinder_name;
465 const char *unwinder_class = nullptr;
466 bool all = false;
467 };
468
469 static const gdb::option::option_def maint_frame_unwind_opt_defs[] = {
470
471 gdb::option::flag_option_def<maint_frame_unwind_options> {
472 "all",
473 [] (maint_frame_unwind_options *opt) { return &opt->all; },
474 N_("Change the state of all unwinders")
475 },
476 gdb::option::string_option_def<maint_frame_unwind_options> {
477 "name",
478 [] (maint_frame_unwind_options *opt) { return &opt->unwinder_name; },
479 nullptr,
480 N_("The name of the unwinder to have its state changed")
481 },
482 gdb::option::enum_option_def<maint_frame_unwind_options> {
483 "class",
484 unwind_class_conversion,
485 [] (maint_frame_unwind_options *opt) { return &opt->unwinder_class; },
486 nullptr,
487 N_("The class of unwinders to have their states changed")
488 }
489 };
490
491 static inline gdb::option::option_def_group
492 make_frame_unwind_enable_disable_options (maint_frame_unwind_options *opts)
493 {
494 return {{maint_frame_unwind_opt_defs}, opts};
495 }
496
497 /* Helper function to both enable and disable frame unwinders.
498 If ENABLE is true, this call will be enabling unwinders,
499 otherwise the unwinders will be disabled. */
500 static void
501 enable_disable_frame_unwinders (const char *args, int from_tty, bool enable)
502 {
503 if (args == nullptr)
504 {
505 if (enable)
506 error (_("Specify which frame unwinder(s) should be enabled"));
507 else
508 error (_("Specify which frame unwinder(s) should be disabled"));
509 }
510
511 struct gdbarch* gdbarch = current_inferior ()->arch ();
512 std::vector<const frame_unwind *> unwinder_list
513 = get_frame_unwind_table (gdbarch);
514
515 maint_frame_unwind_options opts;
516 gdb::option::process_options
517 (&args, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR,
518 make_frame_unwind_enable_disable_options (&opts));
519
520 if ((opts.all && !opts.unwinder_name.empty ())
521 || (opts.all && opts.unwinder_class != nullptr)
522 || (!opts.unwinder_name.empty () && opts.unwinder_class != nullptr))
523 error (_("Options are mutually exclusive"));
524
525 /* First see if the user wants to change all unwinders. */
526 if (opts.all)
527 {
528 for (const frame_unwind *u : unwinder_list)
529 u->set_enabled (enable);
530
531 reinit_frame_cache ();
532 return;
533 }
534
535 /* If user entered a specific unwinder name, handle it here. If the
536 unwinder is already at the expected state, error out. */
537 if (!opts.unwinder_name.empty ())
538 {
539 bool did_something = false;
540 for (const frame_unwind *unwinder : unwinder_list)
541 {
542 if (strcasecmp (unwinder->name (),
543 opts.unwinder_name.c_str ()) == 0)
544 {
545 if (unwinder->enabled () == enable)
546 {
547 if (unwinder->enabled ())
548 error (_("unwinder %s is already enabled"),
549 unwinder->name ());
550 else
551 error (_("unwinder %s is already disabled"),
552 unwinder->name ());
553 }
554 unwinder->set_enabled (enable);
555
556 did_something = true;
557 break;
558 }
559 }
560 if (!did_something)
561 error (_("couldn't find unwinder named %s"),
562 opts.unwinder_name.c_str ());
563 }
564 else
565 {
566 if (opts.unwinder_class == nullptr)
567 opts.unwinder_class = args;
568 enum frame_unwind_class dclass = str_to_frame_unwind_class
569 (opts.unwinder_class);
570 for (const frame_unwind *unwinder: unwinder_list)
571 {
572 if (unwinder->unwinder_class () == dclass)
573 unwinder->set_enabled (enable);
574 }
575 }
576
577 reinit_frame_cache ();
578 }
579
580 /* Completer for the "maint frame-unwinder enable|disable" commands. */
581
582 static void
583 enable_disable_frame_unwinders_completer (struct cmd_list_element *ignore,
584 completion_tracker &tracker,
585 const char *text,
586 const char * /*word*/)
587 {
588 maint_frame_unwind_options opts;
589 const auto group = make_frame_unwind_enable_disable_options (&opts);
590
591 const char *start = text;
592 if (gdb::option::complete_options
593 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group))
594 return;
595
596 /* Only complete a class name as a stand-alone operand when no options
597 are given. */
598 if (start == text)
599 complete_on_enum (tracker, unwind_class_conversion, text, text);
600 return;
601 }
602
603 /* Implement "maint frame-unwinder disable" command. */
604 static void
605 maintenance_disable_frame_unwinders (const char *args, int from_tty)
606 {
607 enable_disable_frame_unwinders (args, from_tty, false);
608 }
609
610 /* Implement "maint frame-unwinder enable" command. */
611 static void
612 maintenance_enable_frame_unwinders (const char *args, int from_tty)
613 {
614 enable_disable_frame_unwinders (args, from_tty, true);
615 }
616
617 INIT_GDB_FILE (frame_unwind)
618 {
619 /* Add "maint info frame-unwinders". */
620 add_cmd ("frame-unwinders",
621 class_maintenance,
622 maintenance_info_frame_unwinders,
623 _("\
624 List the frame unwinders currently in effect.\n\
625 Unwinders are listed starting with the highest priority."),
626 &maintenanceinfolist);
627
628 /* Add "maint frame-unwinder disable/enable". */
629 static struct cmd_list_element *maint_frame_unwinder;
630
631 add_basic_prefix_cmd ("frame-unwinder", class_maintenance,
632 _("Commands handling frame unwinders."),
633 &maint_frame_unwinder, 0, &maintenancelist);
634
635 cmd_list_element *c
636 = add_cmd ("disable", class_maintenance, maintenance_disable_frame_unwinders,
637 _("\
638 Disable one or more frame unwinder(s).\n\
639 Usage: maint frame-unwinder disable [-all | -name NAME | [-class] CLASS]\n\
640 \n\
641 These are the meanings of the options:\n\
642 \t-all - All available unwinders will be disabled\n\
643 \t-name - NAME is the exact name of the frame unwinder to be disabled\n\
644 \t-class - CLASS is the class of unwinders to be disabled. Valid classes are:\n\
645 \t\tGDB - Unwinders added by GDB core;\n\
646 \t\tEXTENSION - Unwinders added by extension languages;\n\
647 \t\tDEBUGINFO - Unwinders that handle debug information;\n\
648 \t\tARCH - Unwinders that use architecture-specific information;\n\
649 \n\
650 UNWINDER and NAME are case insensitive."),
651 &maint_frame_unwinder);
652 set_cmd_completer_handle_brkchars (c,
653 enable_disable_frame_unwinders_completer);
654
655 c =
656 add_cmd ("enable", class_maintenance, maintenance_enable_frame_unwinders,
657 _("\
658 Enable one or more frame unwinder(s).\n\
659 Usage: maint frame-unwinder enable [-all | -name NAME | [-class] CLASS]\n\
660 \n\
661 These are the meanings of the options:\n\
662 \t-all - All available unwinders will be enabled\n\
663 \t-name - NAME is the exact name of the frame unwinder to be enabled\n\
664 \t-class - CLASS is the class of unwinders to be enabled. Valid classes are:\n\
665 \t\tGDB - Unwinders added by GDB core;\n\
666 \t\tEXTENSION - Unwinders added by extension languages;\n\
667 \t\tDEBUGINFO - Unwinders that handle debug information;\n\
668 \t\tARCH - Unwinders that use architecture-specific information;\n\
669 \n\
670 UNWINDER and NAME are case insensitive."),
671 &maint_frame_unwinder);
672 set_cmd_completer_handle_brkchars (c,
673 enable_disable_frame_unwinders_completer);
674 }