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