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"
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 /* The DWARF tailcall sniffer must come before the inline sniffer.
53 Otherwise, we can end up in a situation where a DWARF frame finds
54 tailcall information, but then the inline sniffer claims a frame
55 before the tailcall sniffer, resulting in confusion. This is
56 safe to do always because the tailcall sniffer can only ever be
57 activated if the newer frame was created using the DWARF
58 unwinder, and it also found tailcall information. */
59 &dwarf2_tailcall_frame_unwind
,
63 /* If an unwinder should be prepended to the list, this is the
64 index in which it should be inserted. */
65 static constexpr int prepend_unwinder_index
= standard_unwinders
.size ();
67 static const registry
<gdbarch
>::key
<std::vector
<const frame_unwind
*>>
70 /* Retrieve the list of frame unwinders available in GDBARCH.
71 If this list is empty, it is initialized before being returned. */
72 static std::vector
<const frame_unwind
*> &
73 get_frame_unwind_table (struct gdbarch
*gdbarch
)
75 std::vector
<const frame_unwind
*> *table
= frame_unwind_data
.get (gdbarch
);
77 table
= frame_unwind_data
.emplace (gdbarch
,
78 standard_unwinders
.begin (),
79 standard_unwinders
.end ());
84 frame_unwinder_class_str (frame_unwind_class uclass
)
86 gdb_assert (uclass
< UNWIND_CLASS_NUMBER
);
87 return unwind_class_conversion
[uclass
];
90 /* Case insensitive search for a frame_unwind_class based on the given
92 static enum frame_unwind_class
93 str_to_frame_unwind_class (const char *class_str
)
95 for (int i
= 0; i
< UNWIND_CLASS_NUMBER
; i
++)
97 if (strcasecmp (unwind_class_conversion
[i
], class_str
) == 0)
98 return (frame_unwind_class
) i
;
101 error (_("Unknown frame unwind class: %s"), class_str
);
105 frame_unwind_prepend_unwinder (struct gdbarch
*gdbarch
,
106 const struct frame_unwind
*unwinder
)
108 std::vector
<const frame_unwind
*> &table
= get_frame_unwind_table (gdbarch
);
110 table
.insert (table
.begin () + prepend_unwinder_index
, unwinder
);
114 frame_unwind_append_unwinder (struct gdbarch
*gdbarch
,
115 const struct frame_unwind
*unwinder
)
117 get_frame_unwind_table (gdbarch
).push_back (unwinder
);
120 /* Call SNIFFER from UNWINDER. If it succeeded set UNWINDER for
121 THIS_FRAME and return true. Otherwise the function keeps THIS_FRAME
122 unchanged and returns false. */
125 frame_unwind_try_unwinder (const frame_info_ptr
&this_frame
, void **this_cache
,
126 const struct frame_unwind
*unwinder
)
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
->sniff (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
147 if (get_frame_cache_generation () == entry_generation
)
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. */
166 frame_debug_printf ("yes");
171 frame_debug_printf ("no");
172 /* Don't set *THIS_CACHE to NULL here, because sniffer has to do
174 frame_cleanup_after_sniffer (this_frame
);
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. */
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 /* If we see a disabled unwinder, we assume some test is being run on
191 GDB, and we don't want to internal_error at the end of this function. */
192 bool seen_disabled_unwinder
= false;
193 /* Lambda to factor out the logic of checking if an unwinder is enabled,
194 testing it and otherwise recording if we saw a disable unwinder. */
195 auto test_unwinder
= [&] (const struct frame_unwind
*unwinder
)
197 if (unwinder
== nullptr)
200 if (!unwinder
->enabled ())
202 seen_disabled_unwinder
= true;
206 return frame_unwind_try_unwinder (this_frame
,
211 if (test_unwinder (target_get_unwinder ()))
214 if (test_unwinder (target_get_tailcall_unwinder ()))
217 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
218 std::vector
<const frame_unwind
*> &table
= get_frame_unwind_table (gdbarch
);
219 for (const auto &unwinder
: table
)
221 if (test_unwinder (unwinder
))
225 if (seen_disabled_unwinder
)
226 error (_("Required frame unwinder may have been disabled"
227 ", see 'maint info frame-unwinders'"));
229 internal_error (_("frame_unwind_find_by_frame failed"));
232 /* A default frame sniffer which always accepts the frame. Used by
233 fallback prologue unwinders. */
236 default_frame_sniffer (const struct frame_unwind
*self
,
237 const frame_info_ptr
&this_frame
,
238 void **this_prologue_cache
)
243 /* The default frame unwinder stop_reason callback. */
245 enum unwind_stop_reason
246 default_frame_unwind_stop_reason (const frame_info_ptr
&this_frame
,
249 struct frame_id this_id
= get_frame_id (this_frame
);
251 if (this_id
== outer_frame_id
)
252 return UNWIND_OUTERMOST
;
254 return UNWIND_NO_REASON
;
257 /* See frame-unwind.h. */
260 default_unwind_pc (struct gdbarch
*gdbarch
, const frame_info_ptr
&next_frame
)
262 int pc_regnum
= gdbarch_pc_regnum (gdbarch
);
263 CORE_ADDR pc
= frame_unwind_register_unsigned (next_frame
, pc_regnum
);
264 pc
= gdbarch_addr_bits_remove (gdbarch
, pc
);
268 /* See frame-unwind.h. */
271 default_unwind_sp (struct gdbarch
*gdbarch
, const frame_info_ptr
&next_frame
)
273 int sp_regnum
= gdbarch_sp_regnum (gdbarch
);
274 return frame_unwind_register_unsigned (next_frame
, sp_regnum
);
277 /* Helper functions for value-based register unwinding. These return
278 a (possibly lazy) value of the appropriate type. */
280 /* Return a value which indicates that FRAME did not save REGNUM. */
283 frame_unwind_got_optimized (const frame_info_ptr
&frame
, int regnum
)
285 struct gdbarch
*gdbarch
= frame_unwind_arch (frame
);
286 struct type
*type
= register_type (gdbarch
, regnum
);
288 return value::allocate_optimized_out (type
);
291 /* Return a value which indicates that FRAME copied REGNUM into
292 register NEW_REGNUM. */
295 frame_unwind_got_register (const frame_info_ptr
&frame
,
296 int regnum
, int new_regnum
)
298 return value_of_register_lazy (get_next_frame_sentinel_okay (frame
),
302 /* Return a value which indicates that FRAME saved REGNUM in memory at
306 frame_unwind_got_memory (const frame_info_ptr
&frame
, int regnum
, CORE_ADDR addr
)
308 struct gdbarch
*gdbarch
= frame_unwind_arch (frame
);
309 struct value
*v
= value_at_lazy (register_type (gdbarch
, regnum
), addr
);
315 /* Return a value which indicates that FRAME's saved version of
316 REGNUM has a known constant (computed) value of VAL. */
319 frame_unwind_got_constant (const frame_info_ptr
&frame
, int regnum
,
322 struct gdbarch
*gdbarch
= frame_unwind_arch (frame
);
323 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
324 struct value
*reg_val
;
326 reg_val
= value::zero (register_type (gdbarch
, regnum
), not_lval
);
327 store_unsigned_integer (reg_val
->contents_writeable ().data (),
328 register_size (gdbarch
, regnum
), byte_order
, val
);
333 frame_unwind_got_bytes (const frame_info_ptr
&frame
, int regnum
,
334 gdb::array_view
<const gdb_byte
> buf
)
336 struct gdbarch
*gdbarch
= frame_unwind_arch (frame
);
337 struct value
*reg_val
;
339 reg_val
= value::zero (register_type (gdbarch
, regnum
), not_lval
);
340 gdb::array_view
<gdb_byte
> val_contents
= reg_val
->contents_raw ();
342 /* The value's contents buffer is zeroed on allocation so if buf is
343 smaller, the remaining space will be filled with zero.
345 This can happen when unwinding through signal frames. For example, if
346 an AArch64 program doesn't use SVE, then the Linux kernel will only
347 save in the signal frame the first 128 bits of the vector registers,
348 which is their minimum size, even if the vector length says they're
350 gdb_assert (buf
.size () <= val_contents
.size ());
352 memcpy (val_contents
.data (), buf
.data (), buf
.size ());
356 /* Return a value which indicates that FRAME's saved version of REGNUM
357 has a known constant (computed) value of ADDR. Convert the
358 CORE_ADDR to a target address if necessary. */
361 frame_unwind_got_address (const frame_info_ptr
&frame
, int regnum
,
364 struct gdbarch
*gdbarch
= frame_unwind_arch (frame
);
365 struct value
*reg_val
;
367 reg_val
= value::zero (register_type (gdbarch
, regnum
), not_lval
);
368 pack_long (reg_val
->contents_writeable ().data (),
369 register_type (gdbarch
, regnum
), addr
);
373 /* See frame-unwind.h. */
375 enum unwind_stop_reason
376 frame_unwind_legacy::stop_reason (const frame_info_ptr
&this_frame
,
377 void **this_prologue_cache
) const
379 return m_stop_reason (this_frame
, this_prologue_cache
);
382 /* See frame-unwind.h. */
385 frame_unwind_legacy::this_id (const frame_info_ptr
&this_frame
,
386 void **this_prologue_cache
,
387 struct frame_id
*id
) const
389 return m_this_id (this_frame
, this_prologue_cache
, id
);
392 /* See frame-unwind.h. */
395 frame_unwind_legacy::prev_register (const frame_info_ptr
&this_frame
,
396 void **this_prologue_cache
,
399 return m_prev_register (this_frame
, this_prologue_cache
, regnum
);
402 /* See frame-unwind.h. */
405 frame_unwind_legacy::sniff (const frame_info_ptr
&this_frame
,
406 void **this_prologue_cache
) const
408 return m_sniffer (this, this_frame
, this_prologue_cache
);
411 /* See frame-unwind.h. */
414 frame_unwind_legacy::dealloc_cache (frame_info
*self
, void *this_cache
) const
416 if (m_dealloc_cache
!= nullptr)
417 m_dealloc_cache (self
, this_cache
);
420 /* See frame-unwind.h. */
423 frame_unwind_legacy::prev_arch (const frame_info_ptr
&this_frame
,
424 void **this_prologue_cache
) const
426 if (m_prev_arch
== nullptr)
427 return frame_unwind::prev_arch (this_frame
, this_prologue_cache
);
428 return m_prev_arch (this_frame
, this_prologue_cache
);
431 /* Implement "maintenance info frame-unwinders" command. */
434 maintenance_info_frame_unwinders (const char *args
, int from_tty
)
436 gdbarch
*gdbarch
= current_inferior ()->arch ();
437 std::vector
<const frame_unwind
*> &table
= get_frame_unwind_table (gdbarch
);
439 ui_out
*uiout
= current_uiout
;
440 ui_out_emit_table
table_emitter (uiout
, 4, -1, "FrameUnwinders");
441 uiout
->table_header (27, ui_left
, "name", "Name");
442 uiout
->table_header (25, ui_left
, "type", "Type");
443 uiout
->table_header (9, ui_left
, "class", "Class");
444 uiout
->table_header (8, ui_left
, "enabled", "Enabled");
445 uiout
->table_body ();
447 for (const auto &unwinder
: table
)
449 ui_out_emit_list
tuple_emitter (uiout
, nullptr);
450 uiout
->field_string ("name", unwinder
->name ());
451 uiout
->field_string ("type", frame_type_str (unwinder
->type ()));
452 uiout
->field_string ("class", frame_unwinder_class_str (
453 unwinder
->unwinder_class ()));
454 uiout
->field_string ("enabled", unwinder
->enabled () ? "Y" : "N");
459 /* Options for disabling frame unwinders. */
460 struct maint_frame_unwind_options
462 std::string unwinder_name
;
463 const char *unwinder_class
= nullptr;
467 static const gdb::option::option_def maint_frame_unwind_opt_defs
[] = {
469 gdb::option::flag_option_def
<maint_frame_unwind_options
> {
471 [] (maint_frame_unwind_options
*opt
) { return &opt
->all
; },
472 N_("Change the state of all unwinders")
474 gdb::option::string_option_def
<maint_frame_unwind_options
> {
476 [] (maint_frame_unwind_options
*opt
) { return &opt
->unwinder_name
; },
478 N_("The name of the unwinder to have its state changed")
480 gdb::option::enum_option_def
<maint_frame_unwind_options
> {
482 unwind_class_conversion
,
483 [] (maint_frame_unwind_options
*opt
) { return &opt
->unwinder_class
; },
485 N_("The class of unwinders to have their states changed")
489 static inline gdb::option::option_def_group
490 make_frame_unwind_enable_disable_options (maint_frame_unwind_options
*opts
)
492 return {{maint_frame_unwind_opt_defs
}, opts
};
495 /* Helper function to both enable and disable frame unwinders.
496 If ENABLE is true, this call will be enabling unwinders,
497 otherwise the unwinders will be disabled. */
499 enable_disable_frame_unwinders (const char *args
, int from_tty
, bool enable
)
504 error (_("Specify which frame unwinder(s) should be enabled"));
506 error (_("Specify which frame unwinder(s) should be disabled"));
509 struct gdbarch
* gdbarch
= current_inferior ()->arch ();
510 std::vector
<const frame_unwind
*> unwinder_list
511 = get_frame_unwind_table (gdbarch
);
513 maint_frame_unwind_options opts
;
514 gdb::option::process_options
515 (&args
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR
,
516 make_frame_unwind_enable_disable_options (&opts
));
518 if ((opts
.all
&& !opts
.unwinder_name
.empty ())
519 || (opts
.all
&& opts
.unwinder_class
!= nullptr)
520 || (!opts
.unwinder_name
.empty () && opts
.unwinder_class
!= nullptr))
521 error (_("Options are mutually exclusive"));
523 /* First see if the user wants to change all unwinders. */
526 for (const frame_unwind
*u
: unwinder_list
)
527 u
->set_enabled (enable
);
529 reinit_frame_cache ();
533 /* If user entered a specific unwinder name, handle it here. If the
534 unwinder is already at the expected state, error out. */
535 if (!opts
.unwinder_name
.empty ())
537 bool did_something
= false;
538 for (const frame_unwind
*unwinder
: unwinder_list
)
540 if (strcasecmp (unwinder
->name (),
541 opts
.unwinder_name
.c_str ()) == 0)
543 if (unwinder
->enabled () == enable
)
545 if (unwinder
->enabled ())
546 error (_("unwinder %s is already enabled"),
549 error (_("unwinder %s is already disabled"),
552 unwinder
->set_enabled (enable
);
554 did_something
= true;
559 error (_("couldn't find unwinder named %s"),
560 opts
.unwinder_name
.c_str ());
564 if (opts
.unwinder_class
== nullptr)
565 opts
.unwinder_class
= args
;
566 enum frame_unwind_class dclass
= str_to_frame_unwind_class
567 (opts
.unwinder_class
);
568 for (const frame_unwind
*unwinder
: unwinder_list
)
570 if (unwinder
->unwinder_class () == dclass
)
571 unwinder
->set_enabled (enable
);
575 reinit_frame_cache ();
578 /* Completer for the "maint frame-unwinder enable|disable" commands. */
581 enable_disable_frame_unwinders_completer (struct cmd_list_element
*ignore
,
582 completion_tracker
&tracker
,
584 const char * /*word*/)
586 maint_frame_unwind_options opts
;
587 const auto group
= make_frame_unwind_enable_disable_options (&opts
);
589 const char *start
= text
;
590 if (gdb::option::complete_options
591 (tracker
, &text
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, group
))
594 /* Only complete a class name as a stand-alone operand when no options
597 complete_on_enum (tracker
, unwind_class_conversion
, text
, text
);
601 /* Implement "maint frame-unwinder disable" command. */
603 maintenance_disable_frame_unwinders (const char *args
, int from_tty
)
605 enable_disable_frame_unwinders (args
, from_tty
, false);
608 /* Implement "maint frame-unwinder enable" command. */
610 maintenance_enable_frame_unwinders (const char *args
, int from_tty
)
612 enable_disable_frame_unwinders (args
, from_tty
, true);
615 void _initialize_frame_unwind ();
617 _initialize_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
);