^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: GPL-2.0-or-later */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #ifndef _DWARF_AUX_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #define _DWARF_AUX_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * dwarf-aux.h : libdw auxiliary interfaces
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <dwarf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <elfutils/libdw.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <elfutils/libdwfl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <elfutils/version.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) struct strbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) /* Find the realpath of the target file */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) const char *cu_find_realpath(Dwarf_Die *cu_die, const char *fname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) /* Get DW_AT_comp_dir (should be NULL with older gcc) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) const char *cu_get_comp_dir(Dwarf_Die *cu_die);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) /* Get a line number and file name for given address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) int cu_find_lineinfo(Dwarf_Die *cudie, unsigned long addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) const char **fname, int *lineno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) /* Walk on funcitons at given address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) int cu_walk_functions_at(Dwarf_Die *cu_die, Dwarf_Addr addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) int (*callback)(Dwarf_Die *, void *), void *data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) /* Get DW_AT_linkage_name (should be NULL for C binary) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) const char *die_get_linkage_name(Dwarf_Die *dw_die);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) /* Get the lowest PC in DIE (including range list) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) int die_entrypc(Dwarf_Die *dw_die, Dwarf_Addr *addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) /* Ensure that this DIE is a subprogram and definition (not declaration) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) bool die_is_func_def(Dwarf_Die *dw_die);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) /* Ensure that this DIE is an instance of a subprogram */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) bool die_is_func_instance(Dwarf_Die *dw_die);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) /* Compare diename and tname */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) bool die_compare_name(Dwarf_Die *dw_die, const char *tname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) /* Matching diename with glob pattern */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) bool die_match_name(Dwarf_Die *dw_die, const char *glob);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) /* Get callsite line number of inline-function instance */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) int die_get_call_lineno(Dwarf_Die *in_die);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) /* Get callsite file name of inlined function instance */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) const char *die_get_call_file(Dwarf_Die *in_die);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) /* Get type die */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) Dwarf_Die *die_get_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) /* Get a type die, but skip qualifiers and typedef */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) Dwarf_Die *die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) /* Check whether the DIE is signed or not */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) bool die_is_signed_type(Dwarf_Die *tp_die);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) /* Get data_member_location offset */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) int die_get_data_member_location(Dwarf_Die *mb_die, Dwarf_Word *offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) /* Return values for die_find_child() callbacks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) DIE_FIND_CB_END = 0, /* End of Search */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) DIE_FIND_CB_CHILD = 1, /* Search only children */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) DIE_FIND_CB_SIBLING = 2, /* Search only siblings */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) DIE_FIND_CB_CONTINUE = 3, /* Search children and siblings */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) /* Search child DIEs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) Dwarf_Die *die_find_child(Dwarf_Die *rt_die,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) int (*callback)(Dwarf_Die *, void *),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) void *data, Dwarf_Die *die_mem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) /* Search a non-inlined function including given address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) Dwarf_Die *die_find_realfunc(Dwarf_Die *cu_die, Dwarf_Addr addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) Dwarf_Die *die_mem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) /* Search a non-inlined function with tail call at given address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) Dwarf_Die *die_find_tailfunc(Dwarf_Die *cu_die, Dwarf_Addr addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) Dwarf_Die *die_mem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) /* Search the top inlined function including given address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) Dwarf_Die *die_find_top_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) Dwarf_Die *die_mem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) /* Search the deepest inlined function including given address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) Dwarf_Die *die_find_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) Dwarf_Die *die_mem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) /* Walk on the instances of given DIE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) int die_walk_instances(Dwarf_Die *in_die,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) int (*callback)(Dwarf_Die *, void *), void *data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) /* Walker on lines (Note: line number will not be sorted) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) typedef int (* line_walk_callback_t) (const char *fname, int lineno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) Dwarf_Addr addr, void *data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) * Walk on lines inside given DIE. If the DIE is a subprogram, walk only on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) * the lines inside the subprogram, otherwise the DIE must be a CU DIE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) int die_walk_lines(Dwarf_Die *rt_die, line_walk_callback_t callback, void *data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) /* Find a variable called 'name' at given address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) Dwarf_Die *die_find_variable_at(Dwarf_Die *sp_die, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) Dwarf_Addr addr, Dwarf_Die *die_mem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) /* Find a member called 'name' */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) Dwarf_Die *die_find_member(Dwarf_Die *st_die, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) Dwarf_Die *die_mem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) /* Get the name of given variable DIE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) int die_get_typename(Dwarf_Die *vr_die, struct strbuf *buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) /* Get the name and type of given variable DIE, stored as "type\tname" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) int die_get_varname(Dwarf_Die *vr_die, struct strbuf *buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) int die_get_var_range(Dwarf_Die *sp_die, Dwarf_Die *vr_die, struct strbuf *buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) /* Check if target program is compiled with optimization */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) bool die_is_optimized_target(Dwarf_Die *cu_die);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) /* Use next address after prologue as probe location */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) void die_skip_prologue(Dwarf_Die *sp_die, Dwarf_Die *cu_die,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) Dwarf_Addr *entrypc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) #endif