xref: /linux/tools/perf/util/dwarf-aux.h (revision b2128290c29902315e632ea59e0504d6bc9e9b42)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 #ifndef _DWARF_AUX_H
3 #define _DWARF_AUX_H
4 /*
5  * dwarf-aux.h : libdw auxiliary interfaces
6  */
7 
8 #include <dwarf.h>
9 #include <elfutils/libdw.h>
10 #include <elfutils/libdwfl.h>
11 #include <elfutils/version.h>
12 
13 static inline const char *die_name(Dwarf_Die *die)
14 {
15 	return dwarf_diename(die) ?: "<unknown>";
16 }
17 
18 struct strbuf;
19 
20 /* Find the realpath of the target file */
21 const char *cu_find_realpath(Dwarf_Die *cu_die, const char *fname);
22 
23 /* Get DW_AT_comp_dir (should be NULL with older gcc) */
24 const char *cu_get_comp_dir(Dwarf_Die *cu_die);
25 
26 /* Get a line number and file name for given address */
27 int cu_find_lineinfo(Dwarf_Die *cudie, Dwarf_Addr addr,
28 		     const char **fname, int *lineno);
29 
30 /* Walk on functions at given address */
31 int cu_walk_functions_at(Dwarf_Die *cu_die, Dwarf_Addr addr,
32 			 int (*callback)(Dwarf_Die *, void *), void *data);
33 
34 /* Get DW_AT_linkage_name (should be NULL for C binary) */
35 const char *die_get_linkage_name(Dwarf_Die *dw_die);
36 
37 /* Get the lowest PC in DIE (including range list) */
38 int die_entrypc(Dwarf_Die *dw_die, Dwarf_Addr *addr);
39 
40 /* Ensure that this DIE is a subprogram and definition (not declaration) */
41 bool die_is_func_def(Dwarf_Die *dw_die);
42 
43 /* Ensure that this DIE is an instance of a subprogram */
44 bool die_is_func_instance(Dwarf_Die *dw_die);
45 
46 /* Compare diename and tname */
47 bool die_compare_name(Dwarf_Die *dw_die, const char *tname);
48 
49 /* Matching diename with glob pattern */
50 bool die_match_name(Dwarf_Die *dw_die, const char *glob);
51 
52 /* Get callsite line number of inline-function instance */
53 int die_get_call_lineno(Dwarf_Die *in_die);
54 
55 /* Get callsite file name of inlined function instance */
56 const char *die_get_call_file(Dwarf_Die *in_die);
57 
58 /* Get declared file name of a DIE */
59 const char *die_get_decl_file(Dwarf_Die *dw_die);
60 
61 /* Get type die */
62 Dwarf_Die *die_get_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem);
63 
64 /* Get a type die, but skip qualifiers */
65 Dwarf_Die *__die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem);
66 /* Get a type die, but skip qualifiers and typedef */
67 Dwarf_Die *die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem);
68 /* Get a pointer/array type, following typedefs/qualifiers */
69 Dwarf_Die *die_get_pointer_type(Dwarf_Die *type_die, Dwarf_Die *die_mem);
70 
71 /* Check whether the DIE is signed or not */
72 bool die_is_signed_type(Dwarf_Die *tp_die);
73 
74 /* Get data_member_location offset */
75 int die_get_data_member_location(Dwarf_Die *mb_die, Dwarf_Word *offs);
76 
77 /* Return values for die_find_child() callbacks */
78 enum {
79 	DIE_FIND_CB_END = 0,		/* End of Search */
80 	DIE_FIND_CB_CHILD = 1,		/* Search only children */
81 	DIE_FIND_CB_SIBLING = 2,	/* Search only siblings */
82 	DIE_FIND_CB_CONTINUE = 3,	/* Search children and siblings */
83 };
84 
85 /* Search child DIEs */
86 Dwarf_Die *die_find_child(Dwarf_Die *rt_die,
87 			 int (*callback)(Dwarf_Die *, void *),
88 			 void *data, Dwarf_Die *die_mem);
89 
90 /* Search a non-inlined function including given address */
91 Dwarf_Die *die_find_realfunc(Dwarf_Die *cu_die, Dwarf_Addr addr,
92 			     Dwarf_Die *die_mem);
93 
94 /* Search a non-inlined function with tail call at given address */
95 Dwarf_Die *die_find_tailfunc(Dwarf_Die *cu_die, Dwarf_Addr addr,
96 				    Dwarf_Die *die_mem);
97 
98 /* Search the top inlined function including given address */
99 Dwarf_Die *die_find_top_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
100 				   Dwarf_Die *die_mem);
101 
102 /* Search the deepest inlined function including given address */
103 Dwarf_Die *die_find_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
104 			       Dwarf_Die *die_mem);
105 
106 /* Search a non-inlined function by name and returns its return type */
107 Dwarf_Die *die_find_func_rettype(Dwarf_Die *sp_die, const char *name,
108 				 Dwarf_Die *die_mem);
109 
110 /* Walk on the instances of given DIE */
111 int die_walk_instances(Dwarf_Die *in_die,
112 		       int (*callback)(Dwarf_Die *, void *), void *data);
113 
114 /* Walker on lines (Note: line number will not be sorted) */
115 typedef int (* line_walk_callback_t) (const char *fname, int lineno,
116 				      Dwarf_Addr addr, void *data);
117 
118 /*
119  * Walk on lines inside given DIE. If the DIE is a subprogram, walk only on
120  * the lines inside the subprogram, otherwise the DIE must be a CU DIE.
121  */
122 int die_walk_lines(Dwarf_Die *rt_die, line_walk_callback_t callback, void *data);
123 
124 /* Find a variable called 'name' at given address */
125 Dwarf_Die *die_find_variable_at(Dwarf_Die *sp_die, const char *name,
126 				Dwarf_Addr addr, Dwarf_Die *die_mem);
127 
128 /* Find a member called 'name' */
129 Dwarf_Die *die_find_member(Dwarf_Die *st_die, const char *name,
130 			   Dwarf_Die *die_mem);
131 
132 /* Get the name of given type DIE */
133 int die_get_typename_from_type(Dwarf_Die *type_die, struct strbuf *buf);
134 
135 /* Get the name of given variable DIE */
136 int die_get_typename(Dwarf_Die *vr_die, struct strbuf *buf);
137 
138 /* Get the name and type of given variable DIE, stored as "type\tname" */
139 int die_get_varname(Dwarf_Die *vr_die, struct strbuf *buf);
140 
141 /* Check if target program is compiled with optimization */
142 bool die_is_optimized_target(Dwarf_Die *cu_die);
143 
144 /* Use next address after prologue as probe location */
145 void die_skip_prologue(Dwarf_Die *sp_die, Dwarf_Die *cu_die,
146 		       Dwarf_Addr *entrypc);
147 
148 /* Get the list of including scopes */
149 int die_get_scopes(Dwarf_Die *cu_die, Dwarf_Addr pc, Dwarf_Die **scopes);
150 
151 /* Variable type information */
152 struct die_var_type {
153 	struct die_var_type *next;
154 	u64 die_off;
155 	u64 addr;
156 	u64 end;        /* end address of location range */
157 	int reg;
158 	int offset;
159 	/* Whether the register holds a address to the type */
160 	bool is_reg_var_addr;
161 	bool has_range; /* whether end is valid */
162 };
163 
164 /* Return type info of a member at offset */
165 Dwarf_Die *die_get_member_type(Dwarf_Die *type_die, int offset, Dwarf_Die *die_mem);
166 
167 /* Return type info where the pointer and offset point to */
168 Dwarf_Die *die_deref_ptr_type(Dwarf_Die *ptr_die, int offset, Dwarf_Die *die_mem);
169 
170 /* Get byte offset range of given variable DIE */
171 int die_get_var_range(Dwarf_Die *sp_die, Dwarf_Die *vr_die, struct strbuf *buf);
172 
173 /* Find a variable saved in the 'reg' at given address */
174 Dwarf_Die *die_find_variable_by_reg(Dwarf_Die *sc_die, Dwarf_Addr pc, int reg,
175 				    Dwarf_Die *type_die, int *poffset, bool is_fbreg,
176 				    Dwarf_Die *die_mem);
177 
178 /* Find a (global) variable located in the 'addr' */
179 Dwarf_Die *die_find_variable_by_addr(Dwarf_Die *sc_die, Dwarf_Addr addr,
180 				     Dwarf_Die *die_mem, Dwarf_Die *type_die,
181 				     int *offset);
182 
183 /* Save all variables and parameters in this scope */
184 void die_collect_vars(Dwarf_Die *sc_die, struct die_var_type **var_types);
185 
186 /* Save all global variables in this CU */
187 void die_collect_global_vars(Dwarf_Die *cu_die, struct die_var_type **var_types);
188 
189 /* Get the frame base information from CFA */
190 int die_get_cfa(Dwarf *dwarf, u64 pc, int *preg, int *poffset);
191 
192 #endif /* _DWARF_AUX_H */
193