1 /* Definitions for frame unwinder, for GDB, the GNU debugger.
3 Copyright (C) 2003-2025 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"
22 #include "frame-unwind.h"
23 #include "dummy-frame.h"
24 #include "inline-frame.h"
27 #include "gdbsupport/gdb_obstack.h"
30 #include "dwarf2/frame-tailcall.h"
31 #include "cli/cli-cmds.h"
32 #include "cli/cli-option.h"
35 /* Conversion list between the enum for frame_unwind_class and
37 static const char * unwind_class_conversion
[] =
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
*>
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
,
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 ();
69 static const registry
<gdbarch
>::key
<std::vector
<const frame_unwind
*>>
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
)
77 std::vector
<const frame_unwind
*> *table
= frame_unwind_data
.get (gdbarch
);
79 table
= frame_unwind_data
.emplace (gdbarch
,
80 standard_unwinders
.begin (),
81 standard_unwinders
.end ());
86 frame_unwinder_class_str (frame_unwind_class uclass
)
88 gdb_assert (uclass
< UNWIND_CLASS_NUMBER
);
89 return unwind_class_conversion
[uclass
];
92 /* Case insensitive search for a frame_unwind_class based on the given
94 static enum frame_unwind_class
95 str_to_frame_unwind_class (const char *class_str
)
97 for (int i
= 0; i
< UNWIND_CLASS_NUMBER
; i
++)
99 if (strcasecmp (unwind_class_conversion
[i
], class_str
) == 0)
100 return (frame_unwind_class
) i
;
103 error (_("Unknown frame unwind class: %s"), class_str
);
107 frame_unwind_prepend_unwinder (struct gdbarch
*gdbarch
,
108 const struct frame_unwind
*unwinder
)
110 std::vector
<const frame_unwind
*> &table
= get_frame_unwind_table (gdbarch
);
112 table
.insert (table
.begin () + prepend_unwinder_index
, unwinder
);
116 frame_unwind_append_unwinder (struct gdbarch
*gdbarch
,
117 const struct frame_unwind
*unwinder
)
119 get_frame_unwind_table (gdbarch
).push_back (unwinder
);
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. */
127 frame_unwind_try_unwinder (const frame_info_ptr
&this_frame
, void **this_cache
,
128 const struct frame_unwind
*unwinder
)
132 unsigned int entry_generation
= get_frame_cache_generation ();
134 frame_prepare_for_sniffer (this_frame
, unwinder
);
138 frame_debug_printf ("trying unwinder \"%s\"", unwinder
->name ());
139 res
= unwinder
->sniff (this_frame
, this_cache
);
141 catch (const gdb_exception
&ex
)
143 frame_debug_printf ("caught exception: %s", ex
.message
->c_str ());
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
149 if (get_frame_cache_generation () == entry_generation
)
152 frame_cleanup_after_sniffer (this_frame
);
155 if (ex
.error
== NOT_AVAILABLE_ERROR
)
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. */
168 frame_debug_printf ("yes");
173 frame_debug_printf ("no");
174 /* Don't set *THIS_CACHE to NULL here, because sniffer has to do
176 frame_cleanup_after_sniffer (this_frame
);
179 gdb_assert_not_reached ("frame_unwind_try_unwinder");
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. */
187 frame_unwind_find_by_frame (const frame_info_ptr
&this_frame
, void **this_cache
)
189 FRAME_SCOPED_DEBUG_ENTER_EXIT
;
190 frame_debug_printf ("this_frame=%d", frame_relative_level (this_frame
));
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
)
199 if (unwinder
== nullptr)
202 if (!unwinder
->enabled ())
204 seen_disabled_unwinder
= true;
208 return frame_unwind_try_unwinder (this_frame
,
213 if (test_unwinder (target_get_unwinder ()))
216 if (test_unwinder (target_get_tailcall_unwinder ()))
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
)
223 if (test_unwinder (unwinder
))
227 if (seen_disabled_unwinder
)
228 error (_("Required frame unwinder may have been disabled"
229 ", see 'maint info frame-unwinders'"));
231 internal_error (_("frame_unwind_find_by_frame failed"));
234 /* A default frame sniffer which always accepts the frame. Used by
235 fallback prologue unwinders. */
238 default_frame_sniffer (const struct frame_unwind
*self
,
239 const frame_info_ptr
&this_frame
,
240 void **this_prologue_cache
)
245 /* The default frame unwinder stop_reason callback. */
247 enum unwind_stop_reason
248 default_frame_unwind_stop_reason (const frame_info_ptr
&this_frame
,
251 struct frame_id this_id
= get_frame_id (this_frame
);
253 if (this_id
== outer_frame_id
)
254 return UNWIND_OUTERMOST
;
256 return UNWIND_NO_REASON
;
259 /* See frame-unwind.h. */
262 default_unwind_pc (struct gdbarch
*gdbarch
, const frame_info_ptr
&next_frame
)
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
);
270 /* See frame-unwind.h. */
273 default_unwind_sp (struct gdbarch
*gdbarch
, const frame_info_ptr
&next_frame
)
275 int sp_regnum
= gdbarch_sp_regnum (gdbarch
);
276 return frame_unwind_register_unsigned (next_frame
, sp_regnum
);
279 /* Helper functions for value-based register unwinding. These return
280 a (possibly lazy) value of the appropriate type. */
282 /* Return a value which indicates that FRAME did not save REGNUM. */
285 frame_unwind_got_optimized (const frame_info_ptr
&frame
, int regnum
)
287 struct gdbarch
*gdbarch
= frame_unwind_arch (frame
);
288 struct type
*type
= register_type (gdbarch
, regnum
);
290 return value::allocate_optimized_out (type
);
293 /* Return a value which indicates that FRAME copied REGNUM into
294 register NEW_REGNUM. */
297 frame_unwind_got_register (const frame_info_ptr
&frame
,
298 int regnum
, int new_regnum
)
300 return value_of_register_lazy (get_next_frame_sentinel_okay (frame
),
304 /* Return a value which indicates that FRAME saved REGNUM in memory at
308 frame_unwind_got_memory (const frame_info_ptr
&frame
, int regnum
, CORE_ADDR addr
)
310 struct gdbarch
*gdbarch
= frame_unwind_arch (frame
);
311 struct value
*v
= value_at_lazy (register_type (gdbarch
, regnum
), addr
);
317 /* Return a value which indicates that FRAME's saved version of
318 REGNUM has a known constant (computed) value of VAL. */
321 frame_unwind_got_constant (const frame_info_ptr
&frame
, int regnum
,
324 struct gdbarch
*gdbarch
= frame_unwind_arch (frame
);
325 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
326 struct value
*reg_val
;
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
);
335 frame_unwind_got_bytes (const frame_info_ptr
&frame
, int regnum
,
336 gdb::array_view
<const gdb_byte
> buf
)
338 struct gdbarch
*gdbarch
= frame_unwind_arch (frame
);
339 struct value
*reg_val
;
341 reg_val
= value::zero (register_type (gdbarch
, regnum
), not_lval
);
342 gdb::array_view
<gdb_byte
> val_contents
= reg_val
->contents_raw ();
344 /* The value's contents buffer is zeroed on allocation so if buf is
345 smaller, the remaining space will be filled with zero.
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
352 gdb_assert (buf
.size () <= val_contents
.size ());
354 memcpy (val_contents
.data (), buf
.data (), buf
.size ());
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. */
363 frame_unwind_got_address (const frame_info_ptr
&frame
, int regnum
,
366 struct gdbarch
*gdbarch
= frame_unwind_arch (frame
);
367 struct value
*reg_val
;
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
);
375 /* See frame-unwind.h. */
377 enum unwind_stop_reason
378 frame_unwind_legacy::stop_reason (const frame_info_ptr
&this_frame
,
379 void **this_prologue_cache
) const
381 return m_stop_reason (this_frame
, this_prologue_cache
);
384 /* See frame-unwind.h. */
387 frame_unwind_legacy::this_id (const frame_info_ptr
&this_frame
,
388 void **this_prologue_cache
,
389 struct frame_id
*id
) const
391 return m_this_id (this_frame
, this_prologue_cache
, id
);
394 /* See frame-unwind.h. */
397 frame_unwind_legacy::prev_register (const frame_info_ptr
&this_frame
,
398 void **this_prologue_cache
,
401 return m_prev_register (this_frame
, this_prologue_cache
, regnum
);
404 /* See frame-unwind.h. */
407 frame_unwind_legacy::sniff (const frame_info_ptr
&this_frame
,
408 void **this_prologue_cache
) const
410 return m_sniffer (this, this_frame
, this_prologue_cache
);
413 /* See frame-unwind.h. */
416 frame_unwind_legacy::dealloc_cache (frame_info
*self
, void *this_cache
) const
418 if (m_dealloc_cache
!= nullptr)
419 m_dealloc_cache (self
, this_cache
);
422 /* See frame-unwind.h. */
425 frame_unwind_legacy::prev_arch (const frame_info_ptr
&this_frame
,
426 void **this_prologue_cache
) const
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
);
433 /* Implement "maintenance info frame-unwinders" command. */
436 maintenance_info_frame_unwinders (const char *args
, int from_tty
)
438 gdbarch
*gdbarch
= current_inferior ()->arch ();
439 std::vector
<const frame_unwind
*> &table
= get_frame_unwind_table (gdbarch
);
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 ();
449 for (const auto &unwinder
: table
)
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");
461 /* Options for disabling frame unwinders. */
462 struct maint_frame_unwind_options
464 std::string unwinder_name
;
465 const char *unwinder_class
= nullptr;
469 static const gdb::option::option_def maint_frame_unwind_opt_defs
[] = {
471 gdb::option::flag_option_def
<maint_frame_unwind_options
> {
473 [] (maint_frame_unwind_options
*opt
) { return &opt
->all
; },
474 N_("Change the state of all unwinders")
476 gdb::option::string_option_def
<maint_frame_unwind_options
> {
478 [] (maint_frame_unwind_options
*opt
) { return &opt
->unwinder_name
; },
480 N_("The name of the unwinder to have its state changed")
482 gdb::option::enum_option_def
<maint_frame_unwind_options
> {
484 unwind_class_conversion
,
485 [] (maint_frame_unwind_options
*opt
) { return &opt
->unwinder_class
; },
487 N_("The class of unwinders to have their states changed")
491 static inline gdb::option::option_def_group
492 make_frame_unwind_enable_disable_options (maint_frame_unwind_options
*opts
)
494 return {{maint_frame_unwind_opt_defs
}, opts
};
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. */
501 enable_disable_frame_unwinders (const char *args
, int from_tty
, bool enable
)
506 error (_("Specify which frame unwinder(s) should be enabled"));
508 error (_("Specify which frame unwinder(s) should be disabled"));
511 struct gdbarch
* gdbarch
= current_inferior ()->arch ();
512 std::vector
<const frame_unwind
*> unwinder_list
513 = get_frame_unwind_table (gdbarch
);
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
));
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"));
525 /* First see if the user wants to change all unwinders. */
528 for (const frame_unwind
*u
: unwinder_list
)
529 u
->set_enabled (enable
);
531 reinit_frame_cache ();
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 ())
539 bool did_something
= false;
540 for (const frame_unwind
*unwinder
: unwinder_list
)
542 if (strcasecmp (unwinder
->name (),
543 opts
.unwinder_name
.c_str ()) == 0)
545 if (unwinder
->enabled () == enable
)
547 if (unwinder
->enabled ())
548 error (_("unwinder %s is already enabled"),
551 error (_("unwinder %s is already disabled"),
554 unwinder
->set_enabled (enable
);
556 did_something
= true;
561 error (_("couldn't find unwinder named %s"),
562 opts
.unwinder_name
.c_str ());
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
)
572 if (unwinder
->unwinder_class () == dclass
)
573 unwinder
->set_enabled (enable
);
577 reinit_frame_cache ();
580 /* Completer for the "maint frame-unwinder enable|disable" commands. */
583 enable_disable_frame_unwinders_completer (struct cmd_list_element
*ignore
,
584 completion_tracker
&tracker
,
586 const char * /*word*/)
588 maint_frame_unwind_options opts
;
589 const auto group
= make_frame_unwind_enable_disable_options (&opts
);
591 const char *start
= text
;
592 if (gdb::option::complete_options
593 (tracker
, &text
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, group
))
596 /* Only complete a class name as a stand-alone operand when no options
599 complete_on_enum (tracker
, unwind_class_conversion
, text
, text
);
603 /* Implement "maint frame-unwinder disable" command. */
605 maintenance_disable_frame_unwinders (const char *args
, int from_tty
)
607 enable_disable_frame_unwinders (args
, from_tty
, false);
610 /* Implement "maint frame-unwinder enable" command. */
612 maintenance_enable_frame_unwinders (const char *args
, int from_tty
)
614 enable_disable_frame_unwinders (args
, from_tty
, true);
617 INIT_GDB_FILE (frame_unwind
)
619 /* Add "maint info frame-unwinders". */
620 add_cmd ("frame-unwinders",
622 maintenance_info_frame_unwinders
,
624 List the frame unwinders currently in effect.\n\
625 Unwinders are listed starting with the highest priority."),
626 &maintenanceinfolist
);
628 /* Add "maint frame-unwinder disable/enable". */
629 static struct cmd_list_element
*maint_frame_unwinder
;
631 add_basic_prefix_cmd ("frame-unwinder", class_maintenance
,
632 _("Commands handling frame unwinders."),
633 &maint_frame_unwinder
, 0, &maintenancelist
);
636 = add_cmd ("disable", class_maintenance
, maintenance_disable_frame_unwinders
,
638 Disable one or more frame unwinder(s).\n\
639 Usage: maint frame-unwinder disable [-all | -name NAME | [-class] CLASS]\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\
650 UNWINDER and NAME are case insensitive."),
651 &maint_frame_unwinder
);
652 set_cmd_completer_handle_brkchars (c
,
653 enable_disable_frame_unwinders_completer
);
656 add_cmd ("enable", class_maintenance
, maintenance_enable_frame_unwinders
,
658 Enable one or more frame unwinder(s).\n\
659 Usage: maint frame-unwinder enable [-all | -name NAME | [-class] CLASS]\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\
670 UNWINDER and NAME are case insensitive."),
671 &maint_frame_unwinder
);
672 set_cmd_completer_handle_brkchars (c
,
673 enable_disable_frame_unwinders_completer
);