xref: /openbmc/linux/tools/perf/util/dwarf-aux.c (revision acb6a7047ac2146b723fef69ee1ab6b7143546bf)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2e0d153c6SMasami Hiramatsu /*
3e0d153c6SMasami Hiramatsu  * dwarf-aux.c : libdw auxiliary interfaces
4e0d153c6SMasami Hiramatsu  */
5e0d153c6SMasami Hiramatsu 
6a43783aeSArnaldo Carvalho de Melo #include <errno.h>
7fd20e811SArnaldo Carvalho de Melo #include <inttypes.h>
8e0d153c6SMasami Hiramatsu #include <stdbool.h>
9215a0d30SArnaldo Carvalho de Melo #include <stdlib.h>
10e0d153c6SMasami Hiramatsu #include "debug.h"
11e0d153c6SMasami Hiramatsu #include "dwarf-aux.h"
128520a98dSArnaldo Carvalho de Melo #include "strbuf.h"
13a067558eSArnaldo Carvalho de Melo #include "string2.h"
14e0d153c6SMasami Hiramatsu 
15e0d153c6SMasami Hiramatsu /**
16e0d153c6SMasami Hiramatsu  * cu_find_realpath - Find the realpath of the target file
17e0d153c6SMasami Hiramatsu  * @cu_die: A DIE(dwarf information entry) of CU(compilation Unit)
18e0d153c6SMasami Hiramatsu  * @fname:  The tail filename of the target file
19e0d153c6SMasami Hiramatsu  *
20e0d153c6SMasami Hiramatsu  * Find the real(long) path of @fname in @cu_die.
21e0d153c6SMasami Hiramatsu  */
22e0d153c6SMasami Hiramatsu const char *cu_find_realpath(Dwarf_Die *cu_die, const char *fname)
23e0d153c6SMasami Hiramatsu {
24e0d153c6SMasami Hiramatsu 	Dwarf_Files *files;
25e0d153c6SMasami Hiramatsu 	size_t nfiles, i;
26e0d153c6SMasami Hiramatsu 	const char *src = NULL;
27e0d153c6SMasami Hiramatsu 	int ret;
28e0d153c6SMasami Hiramatsu 
29e0d153c6SMasami Hiramatsu 	if (!fname)
30e0d153c6SMasami Hiramatsu 		return NULL;
31e0d153c6SMasami Hiramatsu 
32e0d153c6SMasami Hiramatsu 	ret = dwarf_getsrcfiles(cu_die, &files, &nfiles);
33e0d153c6SMasami Hiramatsu 	if (ret != 0)
34e0d153c6SMasami Hiramatsu 		return NULL;
35e0d153c6SMasami Hiramatsu 
36e0d153c6SMasami Hiramatsu 	for (i = 0; i < nfiles; i++) {
37e0d153c6SMasami Hiramatsu 		src = dwarf_filesrc(files, i, NULL, NULL);
38e0d153c6SMasami Hiramatsu 		if (strtailcmp(src, fname) == 0)
39e0d153c6SMasami Hiramatsu 			break;
40e0d153c6SMasami Hiramatsu 	}
41e0d153c6SMasami Hiramatsu 	if (i == nfiles)
42e0d153c6SMasami Hiramatsu 		return NULL;
43e0d153c6SMasami Hiramatsu 	return src;
44e0d153c6SMasami Hiramatsu }
45e0d153c6SMasami Hiramatsu 
46e0d153c6SMasami Hiramatsu /**
47e0d153c6SMasami Hiramatsu  * cu_get_comp_dir - Get the path of compilation directory
48e0d153c6SMasami Hiramatsu  * @cu_die: a CU DIE
49e0d153c6SMasami Hiramatsu  *
50e0d153c6SMasami Hiramatsu  * Get the path of compilation directory of given @cu_die.
51e0d153c6SMasami Hiramatsu  * Since this depends on DW_AT_comp_dir, older gcc will not
52e0d153c6SMasami Hiramatsu  * embedded it. In that case, this returns NULL.
53e0d153c6SMasami Hiramatsu  */
54e0d153c6SMasami Hiramatsu const char *cu_get_comp_dir(Dwarf_Die *cu_die)
55e0d153c6SMasami Hiramatsu {
56e0d153c6SMasami Hiramatsu 	Dwarf_Attribute attr;
57e0d153c6SMasami Hiramatsu 	if (dwarf_attr(cu_die, DW_AT_comp_dir, &attr) == NULL)
58e0d153c6SMasami Hiramatsu 		return NULL;
59e0d153c6SMasami Hiramatsu 	return dwarf_formstring(&attr);
60e0d153c6SMasami Hiramatsu }
61e0d153c6SMasami Hiramatsu 
62e0d153c6SMasami Hiramatsu /**
63e0d153c6SMasami Hiramatsu  * cu_find_lineinfo - Get a line number and file name for given address
64e0d153c6SMasami Hiramatsu  * @cu_die: a CU DIE
65e0d153c6SMasami Hiramatsu  * @addr: An address
66e0d153c6SMasami Hiramatsu  * @fname: a pointer which returns the file name string
67e0d153c6SMasami Hiramatsu  * @lineno: a pointer which returns the line number
68e0d153c6SMasami Hiramatsu  *
69e0d153c6SMasami Hiramatsu  * Find a line number and file name for @addr in @cu_die.
70e0d153c6SMasami Hiramatsu  */
71e0d153c6SMasami Hiramatsu int cu_find_lineinfo(Dwarf_Die *cu_die, unsigned long addr,
72e0d153c6SMasami Hiramatsu 		    const char **fname, int *lineno)
73e0d153c6SMasami Hiramatsu {
74e0d153c6SMasami Hiramatsu 	Dwarf_Line *line;
75e0d153c6SMasami Hiramatsu 	Dwarf_Addr laddr;
76e0d153c6SMasami Hiramatsu 
77e0d153c6SMasami Hiramatsu 	line = dwarf_getsrc_die(cu_die, (Dwarf_Addr)addr);
78e0d153c6SMasami Hiramatsu 	if (line && dwarf_lineaddr(line, &laddr) == 0 &&
79e0d153c6SMasami Hiramatsu 	    addr == (unsigned long)laddr && dwarf_lineno(line, lineno) == 0) {
80e0d153c6SMasami Hiramatsu 		*fname = dwarf_linesrc(line, NULL, NULL);
81e0d153c6SMasami Hiramatsu 		if (!*fname)
82e0d153c6SMasami Hiramatsu 			/* line number is useless without filename */
83e0d153c6SMasami Hiramatsu 			*lineno = 0;
84e0d153c6SMasami Hiramatsu 	}
85e0d153c6SMasami Hiramatsu 
86e0d153c6SMasami Hiramatsu 	return *lineno ?: -ENOENT;
87e0d153c6SMasami Hiramatsu }
88e0d153c6SMasami Hiramatsu 
89221d0611SMasami Hiramatsu static int __die_find_inline_cb(Dwarf_Die *die_mem, void *data);
90221d0611SMasami Hiramatsu 
91221d0611SMasami Hiramatsu /**
92221d0611SMasami Hiramatsu  * cu_walk_functions_at - Walk on function DIEs at given address
93221d0611SMasami Hiramatsu  * @cu_die: A CU DIE
94221d0611SMasami Hiramatsu  * @addr: An address
95221d0611SMasami Hiramatsu  * @callback: A callback which called with found DIEs
96221d0611SMasami Hiramatsu  * @data: A user data
97221d0611SMasami Hiramatsu  *
98221d0611SMasami Hiramatsu  * Walk on function DIEs at given @addr in @cu_die. Passed DIEs
99221d0611SMasami Hiramatsu  * should be subprogram or inlined-subroutines.
100221d0611SMasami Hiramatsu  */
101221d0611SMasami Hiramatsu int cu_walk_functions_at(Dwarf_Die *cu_die, Dwarf_Addr addr,
102221d0611SMasami Hiramatsu 		    int (*callback)(Dwarf_Die *, void *), void *data)
103221d0611SMasami Hiramatsu {
104221d0611SMasami Hiramatsu 	Dwarf_Die die_mem;
105221d0611SMasami Hiramatsu 	Dwarf_Die *sc_die;
106221d0611SMasami Hiramatsu 	int ret = -ENOENT;
107221d0611SMasami Hiramatsu 
108221d0611SMasami Hiramatsu 	/* Inlined function could be recursive. Trace it until fail */
109221d0611SMasami Hiramatsu 	for (sc_die = die_find_realfunc(cu_die, addr, &die_mem);
110221d0611SMasami Hiramatsu 	     sc_die != NULL;
111221d0611SMasami Hiramatsu 	     sc_die = die_find_child(sc_die, __die_find_inline_cb, &addr,
112221d0611SMasami Hiramatsu 				     &die_mem)) {
113221d0611SMasami Hiramatsu 		ret = callback(sc_die, data);
114221d0611SMasami Hiramatsu 		if (ret)
115221d0611SMasami Hiramatsu 			break;
116221d0611SMasami Hiramatsu 	}
117221d0611SMasami Hiramatsu 
118221d0611SMasami Hiramatsu 	return ret;
119221d0611SMasami Hiramatsu 
120221d0611SMasami Hiramatsu }
121221d0611SMasami Hiramatsu 
122e0d153c6SMasami Hiramatsu /**
123d5a00296SMasami Hiramatsu  * die_get_linkage_name - Get the linkage name of the object
124d5a00296SMasami Hiramatsu  * @dw_die: A DIE of the object
125d5a00296SMasami Hiramatsu  *
126d5a00296SMasami Hiramatsu  * Get the linkage name attiribute of given @dw_die.
127d5a00296SMasami Hiramatsu  * For C++ binary, the linkage name will be the mangled symbol.
128d5a00296SMasami Hiramatsu  */
129d5a00296SMasami Hiramatsu const char *die_get_linkage_name(Dwarf_Die *dw_die)
130d5a00296SMasami Hiramatsu {
131d5a00296SMasami Hiramatsu 	Dwarf_Attribute attr;
132d5a00296SMasami Hiramatsu 
133d5a00296SMasami Hiramatsu 	if (dwarf_attr_integrate(dw_die, DW_AT_linkage_name, &attr) == NULL)
134d5a00296SMasami Hiramatsu 		return NULL;
135d5a00296SMasami Hiramatsu 	return dwarf_formstring(&attr);
136d5a00296SMasami Hiramatsu }
137d5a00296SMasami Hiramatsu 
138d5a00296SMasami Hiramatsu /**
139e0d153c6SMasami Hiramatsu  * die_compare_name - Compare diename and tname
140e0d153c6SMasami Hiramatsu  * @dw_die: a DIE
141e0d153c6SMasami Hiramatsu  * @tname: a string of target name
142e0d153c6SMasami Hiramatsu  *
143e0d153c6SMasami Hiramatsu  * Compare the name of @dw_die and @tname. Return false if @dw_die has no name.
144e0d153c6SMasami Hiramatsu  */
145e0d153c6SMasami Hiramatsu bool die_compare_name(Dwarf_Die *dw_die, const char *tname)
146e0d153c6SMasami Hiramatsu {
147e0d153c6SMasami Hiramatsu 	const char *name;
1484c859351SMasami Hiramatsu 
149e0d153c6SMasami Hiramatsu 	name = dwarf_diename(dw_die);
150e0d153c6SMasami Hiramatsu 	return name ? (strcmp(tname, name) == 0) : false;
151e0d153c6SMasami Hiramatsu }
152e0d153c6SMasami Hiramatsu 
153e0d153c6SMasami Hiramatsu /**
154d5a00296SMasami Hiramatsu  * die_match_name - Match diename/linkage name and glob
1554c859351SMasami Hiramatsu  * @dw_die: a DIE
1564c859351SMasami Hiramatsu  * @glob: a string of target glob pattern
1574c859351SMasami Hiramatsu  *
1584c859351SMasami Hiramatsu  * Glob matching the name of @dw_die and @glob. Return false if matching fail.
159d5a00296SMasami Hiramatsu  * This also match linkage name.
1604c859351SMasami Hiramatsu  */
1614c859351SMasami Hiramatsu bool die_match_name(Dwarf_Die *dw_die, const char *glob)
1624c859351SMasami Hiramatsu {
1634c859351SMasami Hiramatsu 	const char *name;
1644c859351SMasami Hiramatsu 
1654c859351SMasami Hiramatsu 	name = dwarf_diename(dw_die);
166d5a00296SMasami Hiramatsu 	if (name && strglobmatch(name, glob))
167d5a00296SMasami Hiramatsu 		return true;
168d5a00296SMasami Hiramatsu 	/* fall back to check linkage name */
169d5a00296SMasami Hiramatsu 	name = die_get_linkage_name(dw_die);
170d5a00296SMasami Hiramatsu 	if (name && strglobmatch(name, glob))
171d5a00296SMasami Hiramatsu 		return true;
172d5a00296SMasami Hiramatsu 
173d5a00296SMasami Hiramatsu 	return false;
1744c859351SMasami Hiramatsu }
1754c859351SMasami Hiramatsu 
1764c859351SMasami Hiramatsu /**
177e0d153c6SMasami Hiramatsu  * die_get_call_lineno - Get callsite line number of inline-function instance
178e0d153c6SMasami Hiramatsu  * @in_die: a DIE of an inlined function instance
179e0d153c6SMasami Hiramatsu  *
180e0d153c6SMasami Hiramatsu  * Get call-site line number of @in_die. This means from where the inline
181e0d153c6SMasami Hiramatsu  * function is called.
182e0d153c6SMasami Hiramatsu  */
183e0d153c6SMasami Hiramatsu int die_get_call_lineno(Dwarf_Die *in_die)
184e0d153c6SMasami Hiramatsu {
185e0d153c6SMasami Hiramatsu 	Dwarf_Attribute attr;
186e0d153c6SMasami Hiramatsu 	Dwarf_Word ret;
187e0d153c6SMasami Hiramatsu 
188e0d153c6SMasami Hiramatsu 	if (!dwarf_attr(in_die, DW_AT_call_line, &attr))
189e0d153c6SMasami Hiramatsu 		return -ENOENT;
190e0d153c6SMasami Hiramatsu 
191e0d153c6SMasami Hiramatsu 	dwarf_formudata(&attr, &ret);
192e0d153c6SMasami Hiramatsu 	return (int)ret;
193e0d153c6SMasami Hiramatsu }
194e0d153c6SMasami Hiramatsu 
195e0d153c6SMasami Hiramatsu /**
196e0d153c6SMasami Hiramatsu  * die_get_type - Get type DIE
197e0d153c6SMasami Hiramatsu  * @vr_die: a DIE of a variable
198e0d153c6SMasami Hiramatsu  * @die_mem: where to store a type DIE
199e0d153c6SMasami Hiramatsu  *
200e0d153c6SMasami Hiramatsu  * Get a DIE of the type of given variable (@vr_die), and store
201e0d153c6SMasami Hiramatsu  * it to die_mem. Return NULL if fails to get a type DIE.
202e0d153c6SMasami Hiramatsu  */
203e0d153c6SMasami Hiramatsu Dwarf_Die *die_get_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
204e0d153c6SMasami Hiramatsu {
205e0d153c6SMasami Hiramatsu 	Dwarf_Attribute attr;
206e0d153c6SMasami Hiramatsu 
207e0d153c6SMasami Hiramatsu 	if (dwarf_attr_integrate(vr_die, DW_AT_type, &attr) &&
208e0d153c6SMasami Hiramatsu 	    dwarf_formref_die(&attr, die_mem))
209e0d153c6SMasami Hiramatsu 		return die_mem;
210e0d153c6SMasami Hiramatsu 	else
211e0d153c6SMasami Hiramatsu 		return NULL;
212e0d153c6SMasami Hiramatsu }
213e0d153c6SMasami Hiramatsu 
214e0d153c6SMasami Hiramatsu /* Get a type die, but skip qualifiers */
215e0d153c6SMasami Hiramatsu static Dwarf_Die *__die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
216e0d153c6SMasami Hiramatsu {
217e0d153c6SMasami Hiramatsu 	int tag;
218e0d153c6SMasami Hiramatsu 
219e0d153c6SMasami Hiramatsu 	do {
220e0d153c6SMasami Hiramatsu 		vr_die = die_get_type(vr_die, die_mem);
221e0d153c6SMasami Hiramatsu 		if (!vr_die)
222e0d153c6SMasami Hiramatsu 			break;
223e0d153c6SMasami Hiramatsu 		tag = dwarf_tag(vr_die);
224e0d153c6SMasami Hiramatsu 	} while (tag == DW_TAG_const_type ||
225e0d153c6SMasami Hiramatsu 		 tag == DW_TAG_restrict_type ||
226e0d153c6SMasami Hiramatsu 		 tag == DW_TAG_volatile_type ||
227e0d153c6SMasami Hiramatsu 		 tag == DW_TAG_shared_type);
228e0d153c6SMasami Hiramatsu 
229e0d153c6SMasami Hiramatsu 	return vr_die;
230e0d153c6SMasami Hiramatsu }
231e0d153c6SMasami Hiramatsu 
232e0d153c6SMasami Hiramatsu /**
233e0d153c6SMasami Hiramatsu  * die_get_real_type - Get a type die, but skip qualifiers and typedef
234e0d153c6SMasami Hiramatsu  * @vr_die: a DIE of a variable
235e0d153c6SMasami Hiramatsu  * @die_mem: where to store a type DIE
236e0d153c6SMasami Hiramatsu  *
237e0d153c6SMasami Hiramatsu  * Get a DIE of the type of given variable (@vr_die), and store
238e0d153c6SMasami Hiramatsu  * it to die_mem. Return NULL if fails to get a type DIE.
239e0d153c6SMasami Hiramatsu  * If the type is qualifiers (e.g. const) or typedef, this skips it
240e0d153c6SMasami Hiramatsu  * and tries to find real type (structure or basic types, e.g. int).
241e0d153c6SMasami Hiramatsu  */
242e0d153c6SMasami Hiramatsu Dwarf_Die *die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
243e0d153c6SMasami Hiramatsu {
244e0d153c6SMasami Hiramatsu 	do {
245e0d153c6SMasami Hiramatsu 		vr_die = __die_get_real_type(vr_die, die_mem);
246e0d153c6SMasami Hiramatsu 	} while (vr_die && dwarf_tag(vr_die) == DW_TAG_typedef);
247e0d153c6SMasami Hiramatsu 
248e0d153c6SMasami Hiramatsu 	return vr_die;
249e0d153c6SMasami Hiramatsu }
250e0d153c6SMasami Hiramatsu 
251e0d153c6SMasami Hiramatsu /* Get attribute and translate it as a udata */
252e0d153c6SMasami Hiramatsu static int die_get_attr_udata(Dwarf_Die *tp_die, unsigned int attr_name,
253e0d153c6SMasami Hiramatsu 			      Dwarf_Word *result)
254e0d153c6SMasami Hiramatsu {
255e0d153c6SMasami Hiramatsu 	Dwarf_Attribute attr;
256e0d153c6SMasami Hiramatsu 
257e0d153c6SMasami Hiramatsu 	if (dwarf_attr(tp_die, attr_name, &attr) == NULL ||
258e0d153c6SMasami Hiramatsu 	    dwarf_formudata(&attr, result) != 0)
259e0d153c6SMasami Hiramatsu 		return -ENOENT;
260e0d153c6SMasami Hiramatsu 
261e0d153c6SMasami Hiramatsu 	return 0;
262e0d153c6SMasami Hiramatsu }
263e0d153c6SMasami Hiramatsu 
264b0e9cb28SMasami Hiramatsu /* Get attribute and translate it as a sdata */
265b0e9cb28SMasami Hiramatsu static int die_get_attr_sdata(Dwarf_Die *tp_die, unsigned int attr_name,
266b0e9cb28SMasami Hiramatsu 			      Dwarf_Sword *result)
267b0e9cb28SMasami Hiramatsu {
268b0e9cb28SMasami Hiramatsu 	Dwarf_Attribute attr;
269b0e9cb28SMasami Hiramatsu 
270b0e9cb28SMasami Hiramatsu 	if (dwarf_attr(tp_die, attr_name, &attr) == NULL ||
271b0e9cb28SMasami Hiramatsu 	    dwarf_formsdata(&attr, result) != 0)
272b0e9cb28SMasami Hiramatsu 		return -ENOENT;
273b0e9cb28SMasami Hiramatsu 
274b0e9cb28SMasami Hiramatsu 	return 0;
275b0e9cb28SMasami Hiramatsu }
276b0e9cb28SMasami Hiramatsu 
277e0d153c6SMasami Hiramatsu /**
278e0d153c6SMasami Hiramatsu  * die_is_signed_type - Check whether a type DIE is signed or not
279e0d153c6SMasami Hiramatsu  * @tp_die: a DIE of a type
280e0d153c6SMasami Hiramatsu  *
281e0d153c6SMasami Hiramatsu  * Get the encoding of @tp_die and return true if the encoding
282e0d153c6SMasami Hiramatsu  * is signed.
283e0d153c6SMasami Hiramatsu  */
284e0d153c6SMasami Hiramatsu bool die_is_signed_type(Dwarf_Die *tp_die)
285e0d153c6SMasami Hiramatsu {
286e0d153c6SMasami Hiramatsu 	Dwarf_Word ret;
287e0d153c6SMasami Hiramatsu 
288e0d153c6SMasami Hiramatsu 	if (die_get_attr_udata(tp_die, DW_AT_encoding, &ret))
289e0d153c6SMasami Hiramatsu 		return false;
290e0d153c6SMasami Hiramatsu 
291e0d153c6SMasami Hiramatsu 	return (ret == DW_ATE_signed_char || ret == DW_ATE_signed ||
292e0d153c6SMasami Hiramatsu 		ret == DW_ATE_signed_fixed);
293e0d153c6SMasami Hiramatsu }
294e0d153c6SMasami Hiramatsu 
295e0d153c6SMasami Hiramatsu /**
2960dbb1cacSMasami Hiramatsu  * die_is_func_def - Ensure that this DIE is a subprogram and definition
2970dbb1cacSMasami Hiramatsu  * @dw_die: a DIE
2980dbb1cacSMasami Hiramatsu  *
2990dbb1cacSMasami Hiramatsu  * Ensure that this DIE is a subprogram and NOT a declaration. This
3000dbb1cacSMasami Hiramatsu  * returns true if @dw_die is a function definition.
3010dbb1cacSMasami Hiramatsu  **/
3020dbb1cacSMasami Hiramatsu bool die_is_func_def(Dwarf_Die *dw_die)
3030dbb1cacSMasami Hiramatsu {
3040dbb1cacSMasami Hiramatsu 	Dwarf_Attribute attr;
3050dbb1cacSMasami Hiramatsu 
3060dbb1cacSMasami Hiramatsu 	return (dwarf_tag(dw_die) == DW_TAG_subprogram &&
3070dbb1cacSMasami Hiramatsu 		dwarf_attr(dw_die, DW_AT_declaration, &attr) == NULL);
3080dbb1cacSMasami Hiramatsu }
3090dbb1cacSMasami Hiramatsu 
3100dbb1cacSMasami Hiramatsu /**
311e1ecbbc3SMasami Hiramatsu  * die_is_func_instance - Ensure that this DIE is an instance of a subprogram
312e1ecbbc3SMasami Hiramatsu  * @dw_die: a DIE
313e1ecbbc3SMasami Hiramatsu  *
314e1ecbbc3SMasami Hiramatsu  * Ensure that this DIE is an instance (which has an entry address).
315e1ecbbc3SMasami Hiramatsu  * This returns true if @dw_die is a function instance. If not, you need to
316e1ecbbc3SMasami Hiramatsu  * call die_walk_instances() to find actual instances.
317e1ecbbc3SMasami Hiramatsu  **/
318e1ecbbc3SMasami Hiramatsu bool die_is_func_instance(Dwarf_Die *dw_die)
319e1ecbbc3SMasami Hiramatsu {
320e1ecbbc3SMasami Hiramatsu 	Dwarf_Addr tmp;
321b77afa1fSMasami Hiramatsu 	Dwarf_Attribute attr_mem;
322e1ecbbc3SMasami Hiramatsu 
323e1ecbbc3SMasami Hiramatsu 	/* Actually gcc optimizes non-inline as like as inlined */
324b77afa1fSMasami Hiramatsu 	return !dwarf_func_inline(dw_die) &&
325b77afa1fSMasami Hiramatsu 	       (dwarf_entrypc(dw_die, &tmp) == 0 ||
326b77afa1fSMasami Hiramatsu 		dwarf_attr(dw_die, DW_AT_ranges, &attr_mem) != NULL);
327e1ecbbc3SMasami Hiramatsu }
328b77afa1fSMasami Hiramatsu 
329e1ecbbc3SMasami Hiramatsu /**
330e0d153c6SMasami Hiramatsu  * die_get_data_member_location - Get the data-member offset
331e0d153c6SMasami Hiramatsu  * @mb_die: a DIE of a member of a data structure
332e0d153c6SMasami Hiramatsu  * @offs: The offset of the member in the data structure
333e0d153c6SMasami Hiramatsu  *
334e0d153c6SMasami Hiramatsu  * Get the offset of @mb_die in the data structure including @mb_die, and
335e0d153c6SMasami Hiramatsu  * stores result offset to @offs. If any error occurs this returns errno.
336e0d153c6SMasami Hiramatsu  */
337e0d153c6SMasami Hiramatsu int die_get_data_member_location(Dwarf_Die *mb_die, Dwarf_Word *offs)
338e0d153c6SMasami Hiramatsu {
339e0d153c6SMasami Hiramatsu 	Dwarf_Attribute attr;
340e0d153c6SMasami Hiramatsu 	Dwarf_Op *expr;
341e0d153c6SMasami Hiramatsu 	size_t nexpr;
342e0d153c6SMasami Hiramatsu 	int ret;
343e0d153c6SMasami Hiramatsu 
344e0d153c6SMasami Hiramatsu 	if (dwarf_attr(mb_die, DW_AT_data_member_location, &attr) == NULL)
345e0d153c6SMasami Hiramatsu 		return -ENOENT;
346e0d153c6SMasami Hiramatsu 
347e0d153c6SMasami Hiramatsu 	if (dwarf_formudata(&attr, offs) != 0) {
348e0d153c6SMasami Hiramatsu 		/* DW_AT_data_member_location should be DW_OP_plus_uconst */
349e0d153c6SMasami Hiramatsu 		ret = dwarf_getlocation(&attr, &expr, &nexpr);
350e0d153c6SMasami Hiramatsu 		if (ret < 0 || nexpr == 0)
351e0d153c6SMasami Hiramatsu 			return -ENOENT;
352e0d153c6SMasami Hiramatsu 
353e0d153c6SMasami Hiramatsu 		if (expr[0].atom != DW_OP_plus_uconst || nexpr != 1) {
354e0d153c6SMasami Hiramatsu 			pr_debug("Unable to get offset:Unexpected OP %x (%zd)\n",
355e0d153c6SMasami Hiramatsu 				 expr[0].atom, nexpr);
356e0d153c6SMasami Hiramatsu 			return -ENOTSUP;
357e0d153c6SMasami Hiramatsu 		}
358e0d153c6SMasami Hiramatsu 		*offs = (Dwarf_Word)expr[0].number;
359e0d153c6SMasami Hiramatsu 	}
360e0d153c6SMasami Hiramatsu 	return 0;
361e0d153c6SMasami Hiramatsu }
362e0d153c6SMasami Hiramatsu 
363b0e9cb28SMasami Hiramatsu /* Get the call file index number in CU DIE */
364b0e9cb28SMasami Hiramatsu static int die_get_call_fileno(Dwarf_Die *in_die)
365b0e9cb28SMasami Hiramatsu {
366b0e9cb28SMasami Hiramatsu 	Dwarf_Sword idx;
367b0e9cb28SMasami Hiramatsu 
368b0e9cb28SMasami Hiramatsu 	if (die_get_attr_sdata(in_die, DW_AT_call_file, &idx) == 0)
369b0e9cb28SMasami Hiramatsu 		return (int)idx;
370b0e9cb28SMasami Hiramatsu 	else
371b0e9cb28SMasami Hiramatsu 		return -ENOENT;
372b0e9cb28SMasami Hiramatsu }
373b0e9cb28SMasami Hiramatsu 
3743f4460a2SMasami Hiramatsu /* Get the declared file index number in CU DIE */
3753f4460a2SMasami Hiramatsu static int die_get_decl_fileno(Dwarf_Die *pdie)
3763f4460a2SMasami Hiramatsu {
3773f4460a2SMasami Hiramatsu 	Dwarf_Sword idx;
3783f4460a2SMasami Hiramatsu 
3793f4460a2SMasami Hiramatsu 	if (die_get_attr_sdata(pdie, DW_AT_decl_file, &idx) == 0)
3803f4460a2SMasami Hiramatsu 		return (int)idx;
3813f4460a2SMasami Hiramatsu 	else
3823f4460a2SMasami Hiramatsu 		return -ENOENT;
3833f4460a2SMasami Hiramatsu }
3843f4460a2SMasami Hiramatsu 
385b0e9cb28SMasami Hiramatsu /**
386b0e9cb28SMasami Hiramatsu  * die_get_call_file - Get callsite file name of inlined function instance
387b0e9cb28SMasami Hiramatsu  * @in_die: a DIE of an inlined function instance
388b0e9cb28SMasami Hiramatsu  *
389b0e9cb28SMasami Hiramatsu  * Get call-site file name of @in_die. This means from which file the inline
390b0e9cb28SMasami Hiramatsu  * function is called.
391b0e9cb28SMasami Hiramatsu  */
392b0e9cb28SMasami Hiramatsu const char *die_get_call_file(Dwarf_Die *in_die)
393b0e9cb28SMasami Hiramatsu {
394b0e9cb28SMasami Hiramatsu 	Dwarf_Die cu_die;
395b0e9cb28SMasami Hiramatsu 	Dwarf_Files *files;
396b0e9cb28SMasami Hiramatsu 	int idx;
397b0e9cb28SMasami Hiramatsu 
398b0e9cb28SMasami Hiramatsu 	idx = die_get_call_fileno(in_die);
399b0e9cb28SMasami Hiramatsu 	if (idx < 0 || !dwarf_diecu(in_die, &cu_die, NULL, NULL) ||
400b0e9cb28SMasami Hiramatsu 	    dwarf_getsrcfiles(&cu_die, &files, NULL) != 0)
401b0e9cb28SMasami Hiramatsu 		return NULL;
402b0e9cb28SMasami Hiramatsu 
403b0e9cb28SMasami Hiramatsu 	return dwarf_filesrc(files, idx, NULL, NULL);
404b0e9cb28SMasami Hiramatsu }
405b0e9cb28SMasami Hiramatsu 
406b0e9cb28SMasami Hiramatsu 
407e0d153c6SMasami Hiramatsu /**
408e0d153c6SMasami Hiramatsu  * die_find_child - Generic DIE search function in DIE tree
409e0d153c6SMasami Hiramatsu  * @rt_die: a root DIE
410e0d153c6SMasami Hiramatsu  * @callback: a callback function
411e0d153c6SMasami Hiramatsu  * @data: a user data passed to the callback function
412e0d153c6SMasami Hiramatsu  * @die_mem: a buffer for result DIE
413e0d153c6SMasami Hiramatsu  *
414e0d153c6SMasami Hiramatsu  * Trace DIE tree from @rt_die and call @callback for each child DIE.
415e0d153c6SMasami Hiramatsu  * If @callback returns DIE_FIND_CB_END, this stores the DIE into
416e0d153c6SMasami Hiramatsu  * @die_mem and returns it. If @callback returns DIE_FIND_CB_CONTINUE,
417e0d153c6SMasami Hiramatsu  * this continues to trace the tree. Optionally, @callback can return
418e0d153c6SMasami Hiramatsu  * DIE_FIND_CB_CHILD and DIE_FIND_CB_SIBLING, those means trace only
419e0d153c6SMasami Hiramatsu  * the children and trace only the siblings respectively.
420e0d153c6SMasami Hiramatsu  * Returns NULL if @callback can't find any appropriate DIE.
421e0d153c6SMasami Hiramatsu  */
422e0d153c6SMasami Hiramatsu Dwarf_Die *die_find_child(Dwarf_Die *rt_die,
423e0d153c6SMasami Hiramatsu 			  int (*callback)(Dwarf_Die *, void *),
424e0d153c6SMasami Hiramatsu 			  void *data, Dwarf_Die *die_mem)
425e0d153c6SMasami Hiramatsu {
426e0d153c6SMasami Hiramatsu 	Dwarf_Die child_die;
427e0d153c6SMasami Hiramatsu 	int ret;
428e0d153c6SMasami Hiramatsu 
429e0d153c6SMasami Hiramatsu 	ret = dwarf_child(rt_die, die_mem);
430e0d153c6SMasami Hiramatsu 	if (ret != 0)
431e0d153c6SMasami Hiramatsu 		return NULL;
432e0d153c6SMasami Hiramatsu 
433e0d153c6SMasami Hiramatsu 	do {
434e0d153c6SMasami Hiramatsu 		ret = callback(die_mem, data);
435e0d153c6SMasami Hiramatsu 		if (ret == DIE_FIND_CB_END)
436e0d153c6SMasami Hiramatsu 			return die_mem;
437e0d153c6SMasami Hiramatsu 
438e0d153c6SMasami Hiramatsu 		if ((ret & DIE_FIND_CB_CHILD) &&
439e0d153c6SMasami Hiramatsu 		    die_find_child(die_mem, callback, data, &child_die)) {
440e0d153c6SMasami Hiramatsu 			memcpy(die_mem, &child_die, sizeof(Dwarf_Die));
441e0d153c6SMasami Hiramatsu 			return die_mem;
442e0d153c6SMasami Hiramatsu 		}
443e0d153c6SMasami Hiramatsu 	} while ((ret & DIE_FIND_CB_SIBLING) &&
444e0d153c6SMasami Hiramatsu 		 dwarf_siblingof(die_mem, die_mem) == 0);
445e0d153c6SMasami Hiramatsu 
446e0d153c6SMasami Hiramatsu 	return NULL;
447e0d153c6SMasami Hiramatsu }
448e0d153c6SMasami Hiramatsu 
449e0d153c6SMasami Hiramatsu struct __addr_die_search_param {
450e0d153c6SMasami Hiramatsu 	Dwarf_Addr	addr;
451e0d153c6SMasami Hiramatsu 	Dwarf_Die	*die_mem;
452e0d153c6SMasami Hiramatsu };
453e0d153c6SMasami Hiramatsu 
454d4c537e6SNaveen N. Rao static int __die_search_func_tail_cb(Dwarf_Die *fn_die, void *data)
455d4c537e6SNaveen N. Rao {
456d4c537e6SNaveen N. Rao 	struct __addr_die_search_param *ad = data;
457d4c537e6SNaveen N. Rao 	Dwarf_Addr addr = 0;
458d4c537e6SNaveen N. Rao 
459d4c537e6SNaveen N. Rao 	if (dwarf_tag(fn_die) == DW_TAG_subprogram &&
460d4c537e6SNaveen N. Rao 	    !dwarf_highpc(fn_die, &addr) &&
461d4c537e6SNaveen N. Rao 	    addr == ad->addr) {
462d4c537e6SNaveen N. Rao 		memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die));
463d4c537e6SNaveen N. Rao 		return DWARF_CB_ABORT;
464d4c537e6SNaveen N. Rao 	}
465d4c537e6SNaveen N. Rao 	return DWARF_CB_OK;
466d4c537e6SNaveen N. Rao }
467d4c537e6SNaveen N. Rao 
468d4c537e6SNaveen N. Rao /**
469d4c537e6SNaveen N. Rao  * die_find_tailfunc - Search for a non-inlined function with tail call at
470d4c537e6SNaveen N. Rao  * given address
471d4c537e6SNaveen N. Rao  * @cu_die: a CU DIE which including @addr
472d4c537e6SNaveen N. Rao  * @addr: target address
473d4c537e6SNaveen N. Rao  * @die_mem: a buffer for result DIE
474d4c537e6SNaveen N. Rao  *
475d4c537e6SNaveen N. Rao  * Search for a non-inlined function DIE with tail call at @addr. Stores the
476d4c537e6SNaveen N. Rao  * DIE to @die_mem and returns it if found. Returns NULL if failed.
477d4c537e6SNaveen N. Rao  */
478d4c537e6SNaveen N. Rao Dwarf_Die *die_find_tailfunc(Dwarf_Die *cu_die, Dwarf_Addr addr,
479d4c537e6SNaveen N. Rao 				    Dwarf_Die *die_mem)
480d4c537e6SNaveen N. Rao {
481d4c537e6SNaveen N. Rao 	struct __addr_die_search_param ad;
482d4c537e6SNaveen N. Rao 	ad.addr = addr;
483d4c537e6SNaveen N. Rao 	ad.die_mem = die_mem;
484d4c537e6SNaveen N. Rao 	/* dwarf_getscopes can't find subprogram. */
485d4c537e6SNaveen N. Rao 	if (!dwarf_getfuncs(cu_die, __die_search_func_tail_cb, &ad, 0))
486d4c537e6SNaveen N. Rao 		return NULL;
487d4c537e6SNaveen N. Rao 	else
488d4c537e6SNaveen N. Rao 		return die_mem;
489d4c537e6SNaveen N. Rao }
490d4c537e6SNaveen N. Rao 
491e0d153c6SMasami Hiramatsu /* die_find callback for non-inlined function search */
492e0d153c6SMasami Hiramatsu static int __die_search_func_cb(Dwarf_Die *fn_die, void *data)
493e0d153c6SMasami Hiramatsu {
494e0d153c6SMasami Hiramatsu 	struct __addr_die_search_param *ad = data;
495e0d153c6SMasami Hiramatsu 
4960dbb1cacSMasami Hiramatsu 	/*
4970dbb1cacSMasami Hiramatsu 	 * Since a declaration entry doesn't has given pc, this always returns
4980dbb1cacSMasami Hiramatsu 	 * function definition entry.
4990dbb1cacSMasami Hiramatsu 	 */
500e0d153c6SMasami Hiramatsu 	if (dwarf_tag(fn_die) == DW_TAG_subprogram &&
501e0d153c6SMasami Hiramatsu 	    dwarf_haspc(fn_die, ad->addr)) {
502e0d153c6SMasami Hiramatsu 		memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die));
503e0d153c6SMasami Hiramatsu 		return DWARF_CB_ABORT;
504e0d153c6SMasami Hiramatsu 	}
505e0d153c6SMasami Hiramatsu 	return DWARF_CB_OK;
506e0d153c6SMasami Hiramatsu }
507e0d153c6SMasami Hiramatsu 
508e0d153c6SMasami Hiramatsu /**
509e0d153c6SMasami Hiramatsu  * die_find_realfunc - Search a non-inlined function at given address
510e0d153c6SMasami Hiramatsu  * @cu_die: a CU DIE which including @addr
511e0d153c6SMasami Hiramatsu  * @addr: target address
512e0d153c6SMasami Hiramatsu  * @die_mem: a buffer for result DIE
513e0d153c6SMasami Hiramatsu  *
514e0d153c6SMasami Hiramatsu  * Search a non-inlined function DIE which includes @addr. Stores the
515e08cfd4bSMasami Hiramatsu  * DIE to @die_mem and returns it if found. Returns NULL if failed.
516e0d153c6SMasami Hiramatsu  */
517e0d153c6SMasami Hiramatsu Dwarf_Die *die_find_realfunc(Dwarf_Die *cu_die, Dwarf_Addr addr,
518e0d153c6SMasami Hiramatsu 				    Dwarf_Die *die_mem)
519e0d153c6SMasami Hiramatsu {
520e0d153c6SMasami Hiramatsu 	struct __addr_die_search_param ad;
521e0d153c6SMasami Hiramatsu 	ad.addr = addr;
522e0d153c6SMasami Hiramatsu 	ad.die_mem = die_mem;
523e0d153c6SMasami Hiramatsu 	/* dwarf_getscopes can't find subprogram. */
524e0d153c6SMasami Hiramatsu 	if (!dwarf_getfuncs(cu_die, __die_search_func_cb, &ad, 0))
525e0d153c6SMasami Hiramatsu 		return NULL;
526e0d153c6SMasami Hiramatsu 	else
527e0d153c6SMasami Hiramatsu 		return die_mem;
528e0d153c6SMasami Hiramatsu }
529e0d153c6SMasami Hiramatsu 
530e0d153c6SMasami Hiramatsu /* die_find callback for inline function search */
531e0d153c6SMasami Hiramatsu static int __die_find_inline_cb(Dwarf_Die *die_mem, void *data)
532e0d153c6SMasami Hiramatsu {
533e0d153c6SMasami Hiramatsu 	Dwarf_Addr *addr = data;
534e0d153c6SMasami Hiramatsu 
535e0d153c6SMasami Hiramatsu 	if (dwarf_tag(die_mem) == DW_TAG_inlined_subroutine &&
536e0d153c6SMasami Hiramatsu 	    dwarf_haspc(die_mem, *addr))
537e0d153c6SMasami Hiramatsu 		return DIE_FIND_CB_END;
538e0d153c6SMasami Hiramatsu 
539e0d153c6SMasami Hiramatsu 	return DIE_FIND_CB_CONTINUE;
540e0d153c6SMasami Hiramatsu }
541e0d153c6SMasami Hiramatsu 
542e0d153c6SMasami Hiramatsu /**
543e08cfd4bSMasami Hiramatsu  * die_find_top_inlinefunc - Search the top inlined function at given address
544e08cfd4bSMasami Hiramatsu  * @sp_die: a subprogram DIE which including @addr
545e0d153c6SMasami Hiramatsu  * @addr: target address
546e0d153c6SMasami Hiramatsu  * @die_mem: a buffer for result DIE
547e0d153c6SMasami Hiramatsu  *
548e0d153c6SMasami Hiramatsu  * Search an inlined function DIE which includes @addr. Stores the
549e08cfd4bSMasami Hiramatsu  * DIE to @die_mem and returns it if found. Returns NULL if failed.
550e08cfd4bSMasami Hiramatsu  * Even if several inlined functions are expanded recursively, this
551e08cfd4bSMasami Hiramatsu  * doesn't trace it down, and returns the topmost one.
552e08cfd4bSMasami Hiramatsu  */
553e08cfd4bSMasami Hiramatsu Dwarf_Die *die_find_top_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
554e08cfd4bSMasami Hiramatsu 				   Dwarf_Die *die_mem)
555e08cfd4bSMasami Hiramatsu {
556e08cfd4bSMasami Hiramatsu 	return die_find_child(sp_die, __die_find_inline_cb, &addr, die_mem);
557e08cfd4bSMasami Hiramatsu }
558e08cfd4bSMasami Hiramatsu 
559e08cfd4bSMasami Hiramatsu /**
560e08cfd4bSMasami Hiramatsu  * die_find_inlinefunc - Search an inlined function at given address
561e08cfd4bSMasami Hiramatsu  * @sp_die: a subprogram DIE which including @addr
562e08cfd4bSMasami Hiramatsu  * @addr: target address
563e08cfd4bSMasami Hiramatsu  * @die_mem: a buffer for result DIE
564e08cfd4bSMasami Hiramatsu  *
565e08cfd4bSMasami Hiramatsu  * Search an inlined function DIE which includes @addr. Stores the
566e08cfd4bSMasami Hiramatsu  * DIE to @die_mem and returns it if found. Returns NULL if failed.
567e0d153c6SMasami Hiramatsu  * If several inlined functions are expanded recursively, this trace
568e08cfd4bSMasami Hiramatsu  * it down and returns deepest one.
569e0d153c6SMasami Hiramatsu  */
570e0d153c6SMasami Hiramatsu Dwarf_Die *die_find_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
571e0d153c6SMasami Hiramatsu 			       Dwarf_Die *die_mem)
572e0d153c6SMasami Hiramatsu {
573e0d153c6SMasami Hiramatsu 	Dwarf_Die tmp_die;
574e0d153c6SMasami Hiramatsu 
575e0d153c6SMasami Hiramatsu 	sp_die = die_find_child(sp_die, __die_find_inline_cb, &addr, &tmp_die);
576e0d153c6SMasami Hiramatsu 	if (!sp_die)
577e0d153c6SMasami Hiramatsu 		return NULL;
578e0d153c6SMasami Hiramatsu 
579e0d153c6SMasami Hiramatsu 	/* Inlined function could be recursive. Trace it until fail */
580e0d153c6SMasami Hiramatsu 	while (sp_die) {
581e0d153c6SMasami Hiramatsu 		memcpy(die_mem, sp_die, sizeof(Dwarf_Die));
582e0d153c6SMasami Hiramatsu 		sp_die = die_find_child(sp_die, __die_find_inline_cb, &addr,
583e0d153c6SMasami Hiramatsu 					&tmp_die);
584e0d153c6SMasami Hiramatsu 	}
585e0d153c6SMasami Hiramatsu 
586e0d153c6SMasami Hiramatsu 	return die_mem;
587e0d153c6SMasami Hiramatsu }
588e0d153c6SMasami Hiramatsu 
589db0d2c64SMasami Hiramatsu struct __instance_walk_param {
590db0d2c64SMasami Hiramatsu 	void    *addr;
591db0d2c64SMasami Hiramatsu 	int	(*callback)(Dwarf_Die *, void *);
592db0d2c64SMasami Hiramatsu 	void    *data;
593db0d2c64SMasami Hiramatsu 	int	retval;
594db0d2c64SMasami Hiramatsu };
595db0d2c64SMasami Hiramatsu 
596db0d2c64SMasami Hiramatsu static int __die_walk_instances_cb(Dwarf_Die *inst, void *data)
597db0d2c64SMasami Hiramatsu {
598db0d2c64SMasami Hiramatsu 	struct __instance_walk_param *iwp = data;
599db0d2c64SMasami Hiramatsu 	Dwarf_Attribute attr_mem;
600db0d2c64SMasami Hiramatsu 	Dwarf_Die origin_mem;
601db0d2c64SMasami Hiramatsu 	Dwarf_Attribute *attr;
602db0d2c64SMasami Hiramatsu 	Dwarf_Die *origin;
6033f4460a2SMasami Hiramatsu 	int tmp;
604db0d2c64SMasami Hiramatsu 
605db0d2c64SMasami Hiramatsu 	attr = dwarf_attr(inst, DW_AT_abstract_origin, &attr_mem);
606db0d2c64SMasami Hiramatsu 	if (attr == NULL)
607db0d2c64SMasami Hiramatsu 		return DIE_FIND_CB_CONTINUE;
608db0d2c64SMasami Hiramatsu 
609db0d2c64SMasami Hiramatsu 	origin = dwarf_formref_die(attr, &origin_mem);
610db0d2c64SMasami Hiramatsu 	if (origin == NULL || origin->addr != iwp->addr)
611db0d2c64SMasami Hiramatsu 		return DIE_FIND_CB_CONTINUE;
612db0d2c64SMasami Hiramatsu 
6133f4460a2SMasami Hiramatsu 	/* Ignore redundant instances */
6143f4460a2SMasami Hiramatsu 	if (dwarf_tag(inst) == DW_TAG_inlined_subroutine) {
6153f4460a2SMasami Hiramatsu 		dwarf_decl_line(origin, &tmp);
6163f4460a2SMasami Hiramatsu 		if (die_get_call_lineno(inst) == tmp) {
6173f4460a2SMasami Hiramatsu 			tmp = die_get_decl_fileno(origin);
6183f4460a2SMasami Hiramatsu 			if (die_get_call_fileno(inst) == tmp)
6193f4460a2SMasami Hiramatsu 				return DIE_FIND_CB_CONTINUE;
6203f4460a2SMasami Hiramatsu 		}
6213f4460a2SMasami Hiramatsu 	}
6223f4460a2SMasami Hiramatsu 
623db0d2c64SMasami Hiramatsu 	iwp->retval = iwp->callback(inst, iwp->data);
624db0d2c64SMasami Hiramatsu 
625db0d2c64SMasami Hiramatsu 	return (iwp->retval) ? DIE_FIND_CB_END : DIE_FIND_CB_CONTINUE;
626db0d2c64SMasami Hiramatsu }
627db0d2c64SMasami Hiramatsu 
628db0d2c64SMasami Hiramatsu /**
629db0d2c64SMasami Hiramatsu  * die_walk_instances - Walk on instances of given DIE
630db0d2c64SMasami Hiramatsu  * @or_die: an abstract original DIE
631db0d2c64SMasami Hiramatsu  * @callback: a callback function which is called with instance DIE
632db0d2c64SMasami Hiramatsu  * @data: user data
633db0d2c64SMasami Hiramatsu  *
634db0d2c64SMasami Hiramatsu  * Walk on the instances of give @in_die. @in_die must be an inlined function
635db0d2c64SMasami Hiramatsu  * declartion. This returns the return value of @callback if it returns
636db0d2c64SMasami Hiramatsu  * non-zero value, or -ENOENT if there is no instance.
637db0d2c64SMasami Hiramatsu  */
638db0d2c64SMasami Hiramatsu int die_walk_instances(Dwarf_Die *or_die, int (*callback)(Dwarf_Die *, void *),
639db0d2c64SMasami Hiramatsu 		       void *data)
640db0d2c64SMasami Hiramatsu {
641db0d2c64SMasami Hiramatsu 	Dwarf_Die cu_die;
642db0d2c64SMasami Hiramatsu 	Dwarf_Die die_mem;
643db0d2c64SMasami Hiramatsu 	struct __instance_walk_param iwp = {
644db0d2c64SMasami Hiramatsu 		.addr = or_die->addr,
645db0d2c64SMasami Hiramatsu 		.callback = callback,
646db0d2c64SMasami Hiramatsu 		.data = data,
647db0d2c64SMasami Hiramatsu 		.retval = -ENOENT,
648db0d2c64SMasami Hiramatsu 	};
649db0d2c64SMasami Hiramatsu 
650db0d2c64SMasami Hiramatsu 	if (dwarf_diecu(or_die, &cu_die, NULL, NULL) == NULL)
651db0d2c64SMasami Hiramatsu 		return -ENOENT;
652db0d2c64SMasami Hiramatsu 
653db0d2c64SMasami Hiramatsu 	die_find_child(&cu_die, __die_walk_instances_cb, &iwp, &die_mem);
654db0d2c64SMasami Hiramatsu 
655db0d2c64SMasami Hiramatsu 	return iwp.retval;
656db0d2c64SMasami Hiramatsu }
657db0d2c64SMasami Hiramatsu 
658e0d153c6SMasami Hiramatsu /* Line walker internal parameters */
659e0d153c6SMasami Hiramatsu struct __line_walk_param {
660b0e9cb28SMasami Hiramatsu 	bool recursive;
661e0d153c6SMasami Hiramatsu 	line_walk_callback_t callback;
662e0d153c6SMasami Hiramatsu 	void *data;
663e0d153c6SMasami Hiramatsu 	int retval;
664e0d153c6SMasami Hiramatsu };
665e0d153c6SMasami Hiramatsu 
666e0d153c6SMasami Hiramatsu static int __die_walk_funclines_cb(Dwarf_Die *in_die, void *data)
667e0d153c6SMasami Hiramatsu {
668e0d153c6SMasami Hiramatsu 	struct __line_walk_param *lw = data;
669b0e9cb28SMasami Hiramatsu 	Dwarf_Addr addr = 0;
670b0e9cb28SMasami Hiramatsu 	const char *fname;
671e0d153c6SMasami Hiramatsu 	int lineno;
672e0d153c6SMasami Hiramatsu 
673e0d153c6SMasami Hiramatsu 	if (dwarf_tag(in_die) == DW_TAG_inlined_subroutine) {
674b0e9cb28SMasami Hiramatsu 		fname = die_get_call_file(in_die);
675e0d153c6SMasami Hiramatsu 		lineno = die_get_call_lineno(in_die);
676b0e9cb28SMasami Hiramatsu 		if (fname && lineno > 0 && dwarf_entrypc(in_die, &addr) == 0) {
677b0e9cb28SMasami Hiramatsu 			lw->retval = lw->callback(fname, lineno, addr, lw->data);
678e0d153c6SMasami Hiramatsu 			if (lw->retval != 0)
679e0d153c6SMasami Hiramatsu 				return DIE_FIND_CB_END;
680e0d153c6SMasami Hiramatsu 		}
681b0e9cb28SMasami Hiramatsu 		if (!lw->recursive)
682e0d153c6SMasami Hiramatsu 			return DIE_FIND_CB_SIBLING;
683*acb6a704SMasami Hiramatsu 	}
684b0e9cb28SMasami Hiramatsu 
685b0e9cb28SMasami Hiramatsu 	if (addr) {
686b0e9cb28SMasami Hiramatsu 		fname = dwarf_decl_file(in_die);
687b0e9cb28SMasami Hiramatsu 		if (fname && dwarf_decl_line(in_die, &lineno) == 0) {
688b0e9cb28SMasami Hiramatsu 			lw->retval = lw->callback(fname, lineno, addr, lw->data);
689b0e9cb28SMasami Hiramatsu 			if (lw->retval != 0)
690b0e9cb28SMasami Hiramatsu 				return DIE_FIND_CB_END;
691b0e9cb28SMasami Hiramatsu 		}
692b0e9cb28SMasami Hiramatsu 	}
693b0e9cb28SMasami Hiramatsu 
694b0e9cb28SMasami Hiramatsu 	/* Continue to search nested inlined function call-sites */
695b0e9cb28SMasami Hiramatsu 	return DIE_FIND_CB_CONTINUE;
696e0d153c6SMasami Hiramatsu }
697e0d153c6SMasami Hiramatsu 
698e0d153c6SMasami Hiramatsu /* Walk on lines of blocks included in given DIE */
699b0e9cb28SMasami Hiramatsu static int __die_walk_funclines(Dwarf_Die *sp_die, bool recursive,
700e0d153c6SMasami Hiramatsu 				line_walk_callback_t callback, void *data)
701e0d153c6SMasami Hiramatsu {
702e0d153c6SMasami Hiramatsu 	struct __line_walk_param lw = {
703b0e9cb28SMasami Hiramatsu 		.recursive = recursive,
704e0d153c6SMasami Hiramatsu 		.callback = callback,
705e0d153c6SMasami Hiramatsu 		.data = data,
706e0d153c6SMasami Hiramatsu 		.retval = 0,
707e0d153c6SMasami Hiramatsu 	};
708e0d153c6SMasami Hiramatsu 	Dwarf_Die die_mem;
709e0d153c6SMasami Hiramatsu 	Dwarf_Addr addr;
710b0e9cb28SMasami Hiramatsu 	const char *fname;
711e0d153c6SMasami Hiramatsu 	int lineno;
712e0d153c6SMasami Hiramatsu 
713e0d153c6SMasami Hiramatsu 	/* Handle function declaration line */
714b0e9cb28SMasami Hiramatsu 	fname = dwarf_decl_file(sp_die);
715b0e9cb28SMasami Hiramatsu 	if (fname && dwarf_decl_line(sp_die, &lineno) == 0 &&
716e0d153c6SMasami Hiramatsu 	    dwarf_entrypc(sp_die, &addr) == 0) {
717b0e9cb28SMasami Hiramatsu 		lw.retval = callback(fname, lineno, addr, data);
718e0d153c6SMasami Hiramatsu 		if (lw.retval != 0)
719e0d153c6SMasami Hiramatsu 			goto done;
720e0d153c6SMasami Hiramatsu 	}
721e0d153c6SMasami Hiramatsu 	die_find_child(sp_die, __die_walk_funclines_cb, &lw, &die_mem);
722e0d153c6SMasami Hiramatsu done:
723e0d153c6SMasami Hiramatsu 	return lw.retval;
724e0d153c6SMasami Hiramatsu }
725e0d153c6SMasami Hiramatsu 
726e0d153c6SMasami Hiramatsu static int __die_walk_culines_cb(Dwarf_Die *sp_die, void *data)
727e0d153c6SMasami Hiramatsu {
728e0d153c6SMasami Hiramatsu 	struct __line_walk_param *lw = data;
729e0d153c6SMasami Hiramatsu 
730*acb6a704SMasami Hiramatsu 	/*
731*acb6a704SMasami Hiramatsu 	 * Since inlined function can include another inlined function in
732*acb6a704SMasami Hiramatsu 	 * the same file, we need to walk in it recursively.
733*acb6a704SMasami Hiramatsu 	 */
734b0e9cb28SMasami Hiramatsu 	lw->retval = __die_walk_funclines(sp_die, true, lw->callback, lw->data);
735e0d153c6SMasami Hiramatsu 	if (lw->retval != 0)
736e0d153c6SMasami Hiramatsu 		return DWARF_CB_ABORT;
737e0d153c6SMasami Hiramatsu 
738e0d153c6SMasami Hiramatsu 	return DWARF_CB_OK;
739e0d153c6SMasami Hiramatsu }
740e0d153c6SMasami Hiramatsu 
741e0d153c6SMasami Hiramatsu /**
742e0d153c6SMasami Hiramatsu  * die_walk_lines - Walk on lines inside given DIE
743a128405cSMasami Hiramatsu  * @rt_die: a root DIE (CU, subprogram or inlined_subroutine)
744e0d153c6SMasami Hiramatsu  * @callback: callback routine
745e0d153c6SMasami Hiramatsu  * @data: user data
746e0d153c6SMasami Hiramatsu  *
747e0d153c6SMasami Hiramatsu  * Walk on all lines inside given @rt_die and call @callback on each line.
748e0d153c6SMasami Hiramatsu  * If the @rt_die is a function, walk only on the lines inside the function,
749e0d153c6SMasami Hiramatsu  * otherwise @rt_die must be a CU DIE.
750e0d153c6SMasami Hiramatsu  * Note that this walks not only dwarf line list, but also function entries
751e0d153c6SMasami Hiramatsu  * and inline call-site.
752e0d153c6SMasami Hiramatsu  */
753e0d153c6SMasami Hiramatsu int die_walk_lines(Dwarf_Die *rt_die, line_walk_callback_t callback, void *data)
754e0d153c6SMasami Hiramatsu {
755e0d153c6SMasami Hiramatsu 	Dwarf_Lines *lines;
756e0d153c6SMasami Hiramatsu 	Dwarf_Line *line;
757e0d153c6SMasami Hiramatsu 	Dwarf_Addr addr;
75875186a9bSMasami Hiramatsu 	const char *fname, *decf = NULL;
759e0d153c6SMasami Hiramatsu 	int lineno, ret = 0;
76075186a9bSMasami Hiramatsu 	int decl = 0, inl;
761e0d153c6SMasami Hiramatsu 	Dwarf_Die die_mem, *cu_die;
762e0d153c6SMasami Hiramatsu 	size_t nlines, i;
763e0d153c6SMasami Hiramatsu 
764e0d153c6SMasami Hiramatsu 	/* Get the CU die */
76575186a9bSMasami Hiramatsu 	if (dwarf_tag(rt_die) != DW_TAG_compile_unit) {
766e0d153c6SMasami Hiramatsu 		cu_die = dwarf_diecu(rt_die, &die_mem, NULL, NULL);
76775186a9bSMasami Hiramatsu 		dwarf_decl_line(rt_die, &decl);
76875186a9bSMasami Hiramatsu 		decf = dwarf_decl_file(rt_die);
76975186a9bSMasami Hiramatsu 	} else
770e0d153c6SMasami Hiramatsu 		cu_die = rt_die;
771e0d153c6SMasami Hiramatsu 	if (!cu_die) {
772a128405cSMasami Hiramatsu 		pr_debug2("Failed to get CU from given DIE.\n");
773e0d153c6SMasami Hiramatsu 		return -EINVAL;
774e0d153c6SMasami Hiramatsu 	}
775e0d153c6SMasami Hiramatsu 
776e0d153c6SMasami Hiramatsu 	/* Get lines list in the CU */
777e0d153c6SMasami Hiramatsu 	if (dwarf_getsrclines(cu_die, &lines, &nlines) != 0) {
778e0d153c6SMasami Hiramatsu 		pr_debug2("Failed to get source lines on this CU.\n");
779e0d153c6SMasami Hiramatsu 		return -ENOENT;
780e0d153c6SMasami Hiramatsu 	}
781e0d153c6SMasami Hiramatsu 	pr_debug2("Get %zd lines from this CU\n", nlines);
782e0d153c6SMasami Hiramatsu 
783e0d153c6SMasami Hiramatsu 	/* Walk on the lines on lines list */
784e0d153c6SMasami Hiramatsu 	for (i = 0; i < nlines; i++) {
785e0d153c6SMasami Hiramatsu 		line = dwarf_onesrcline(lines, i);
786e0d153c6SMasami Hiramatsu 		if (line == NULL ||
787e0d153c6SMasami Hiramatsu 		    dwarf_lineno(line, &lineno) != 0 ||
788e0d153c6SMasami Hiramatsu 		    dwarf_lineaddr(line, &addr) != 0) {
789e0d153c6SMasami Hiramatsu 			pr_debug2("Failed to get line info. "
790e0d153c6SMasami Hiramatsu 				  "Possible error in debuginfo.\n");
791e0d153c6SMasami Hiramatsu 			continue;
792e0d153c6SMasami Hiramatsu 		}
793e0d153c6SMasami Hiramatsu 		/* Filter lines based on address */
79486a76027SMasami Hiramatsu 		if (rt_die != cu_die) {
795e0d153c6SMasami Hiramatsu 			/*
796e0d153c6SMasami Hiramatsu 			 * Address filtering
797e0d153c6SMasami Hiramatsu 			 * The line is included in given function, and
798e0d153c6SMasami Hiramatsu 			 * no inline block includes it.
799e0d153c6SMasami Hiramatsu 			 */
80075186a9bSMasami Hiramatsu 			if (!dwarf_haspc(rt_die, addr))
801e0d153c6SMasami Hiramatsu 				continue;
80275186a9bSMasami Hiramatsu 			if (die_find_inlinefunc(rt_die, addr, &die_mem)) {
80375186a9bSMasami Hiramatsu 				dwarf_decl_line(&die_mem, &inl);
80475186a9bSMasami Hiramatsu 				if (inl != decl ||
80575186a9bSMasami Hiramatsu 				    decf != dwarf_decl_file(&die_mem))
80675186a9bSMasami Hiramatsu 					continue;
80775186a9bSMasami Hiramatsu 			}
80886a76027SMasami Hiramatsu 		}
809e0d153c6SMasami Hiramatsu 		/* Get source line */
810e0d153c6SMasami Hiramatsu 		fname = dwarf_linesrc(line, NULL, NULL);
811e0d153c6SMasami Hiramatsu 
812e0d153c6SMasami Hiramatsu 		ret = callback(fname, lineno, addr, data);
813e0d153c6SMasami Hiramatsu 		if (ret != 0)
814e0d153c6SMasami Hiramatsu 			return ret;
815e0d153c6SMasami Hiramatsu 	}
816e0d153c6SMasami Hiramatsu 
817e0d153c6SMasami Hiramatsu 	/*
818e0d153c6SMasami Hiramatsu 	 * Dwarf lines doesn't include function declarations and inlined
819e0d153c6SMasami Hiramatsu 	 * subroutines. We have to check functions list or given function.
820e0d153c6SMasami Hiramatsu 	 */
821e0d153c6SMasami Hiramatsu 	if (rt_die != cu_die)
822b0e9cb28SMasami Hiramatsu 		/*
823*acb6a704SMasami Hiramatsu 		 * Don't need walk inlined functions recursively, because
824*acb6a704SMasami Hiramatsu 		 * inner inlined functions don't have the lines of the
825*acb6a704SMasami Hiramatsu 		 * specified function.
826b0e9cb28SMasami Hiramatsu 		 */
827b0e9cb28SMasami Hiramatsu 		ret = __die_walk_funclines(rt_die, false, callback, data);
828e0d153c6SMasami Hiramatsu 	else {
829e0d153c6SMasami Hiramatsu 		struct __line_walk_param param = {
830e0d153c6SMasami Hiramatsu 			.callback = callback,
831e0d153c6SMasami Hiramatsu 			.data = data,
832e0d153c6SMasami Hiramatsu 			.retval = 0,
833e0d153c6SMasami Hiramatsu 		};
834e0d153c6SMasami Hiramatsu 		dwarf_getfuncs(cu_die, __die_walk_culines_cb, &param, 0);
835e0d153c6SMasami Hiramatsu 		ret = param.retval;
836e0d153c6SMasami Hiramatsu 	}
837e0d153c6SMasami Hiramatsu 
838e0d153c6SMasami Hiramatsu 	return ret;
839e0d153c6SMasami Hiramatsu }
840e0d153c6SMasami Hiramatsu 
841e0d153c6SMasami Hiramatsu struct __find_variable_param {
842e0d153c6SMasami Hiramatsu 	const char *name;
843e0d153c6SMasami Hiramatsu 	Dwarf_Addr addr;
844e0d153c6SMasami Hiramatsu };
845e0d153c6SMasami Hiramatsu 
846e0d153c6SMasami Hiramatsu static int __die_find_variable_cb(Dwarf_Die *die_mem, void *data)
847e0d153c6SMasami Hiramatsu {
848e0d153c6SMasami Hiramatsu 	struct __find_variable_param *fvp = data;
849082f96a9SMasami Hiramatsu 	Dwarf_Attribute attr;
850e0d153c6SMasami Hiramatsu 	int tag;
851e0d153c6SMasami Hiramatsu 
852e0d153c6SMasami Hiramatsu 	tag = dwarf_tag(die_mem);
853e0d153c6SMasami Hiramatsu 	if ((tag == DW_TAG_formal_parameter ||
854e0d153c6SMasami Hiramatsu 	     tag == DW_TAG_variable) &&
855082f96a9SMasami Hiramatsu 	    die_compare_name(die_mem, fvp->name) &&
856082f96a9SMasami Hiramatsu 	/* Does the DIE have location information or external instance? */
857082f96a9SMasami Hiramatsu 	    (dwarf_attr(die_mem, DW_AT_external, &attr) ||
858082f96a9SMasami Hiramatsu 	     dwarf_attr(die_mem, DW_AT_location, &attr)))
859e0d153c6SMasami Hiramatsu 		return DIE_FIND_CB_END;
860e0d153c6SMasami Hiramatsu 	if (dwarf_haspc(die_mem, fvp->addr))
861e0d153c6SMasami Hiramatsu 		return DIE_FIND_CB_CONTINUE;
862e0d153c6SMasami Hiramatsu 	else
863e0d153c6SMasami Hiramatsu 		return DIE_FIND_CB_SIBLING;
864e0d153c6SMasami Hiramatsu }
865e0d153c6SMasami Hiramatsu 
866e0d153c6SMasami Hiramatsu /**
867e0d153c6SMasami Hiramatsu  * die_find_variable_at - Find a given name variable at given address
868e0d153c6SMasami Hiramatsu  * @sp_die: a function DIE
869e0d153c6SMasami Hiramatsu  * @name: variable name
870e0d153c6SMasami Hiramatsu  * @addr: address
871e0d153c6SMasami Hiramatsu  * @die_mem: a buffer for result DIE
872e0d153c6SMasami Hiramatsu  *
873e0d153c6SMasami Hiramatsu  * Find a variable DIE called @name at @addr in @sp_die.
874e0d153c6SMasami Hiramatsu  */
875e0d153c6SMasami Hiramatsu Dwarf_Die *die_find_variable_at(Dwarf_Die *sp_die, const char *name,
876e0d153c6SMasami Hiramatsu 				Dwarf_Addr addr, Dwarf_Die *die_mem)
877e0d153c6SMasami Hiramatsu {
878e0d153c6SMasami Hiramatsu 	struct __find_variable_param fvp = { .name = name, .addr = addr};
879e0d153c6SMasami Hiramatsu 
880e0d153c6SMasami Hiramatsu 	return die_find_child(sp_die, __die_find_variable_cb, (void *)&fvp,
881e0d153c6SMasami Hiramatsu 			      die_mem);
882e0d153c6SMasami Hiramatsu }
883e0d153c6SMasami Hiramatsu 
884e0d153c6SMasami Hiramatsu static int __die_find_member_cb(Dwarf_Die *die_mem, void *data)
885e0d153c6SMasami Hiramatsu {
886e0d153c6SMasami Hiramatsu 	const char *name = data;
887e0d153c6SMasami Hiramatsu 
888c7273835SMasami Hiramatsu 	if (dwarf_tag(die_mem) == DW_TAG_member) {
889c7273835SMasami Hiramatsu 		if (die_compare_name(die_mem, name))
890e0d153c6SMasami Hiramatsu 			return DIE_FIND_CB_END;
891c7273835SMasami Hiramatsu 		else if (!dwarf_diename(die_mem)) {	/* Unnamed structure */
892c7273835SMasami Hiramatsu 			Dwarf_Die type_die, tmp_die;
893c7273835SMasami Hiramatsu 			if (die_get_type(die_mem, &type_die) &&
894c7273835SMasami Hiramatsu 			    die_find_member(&type_die, name, &tmp_die))
895c7273835SMasami Hiramatsu 				return DIE_FIND_CB_END;
896c7273835SMasami Hiramatsu 		}
897c7273835SMasami Hiramatsu 	}
898e0d153c6SMasami Hiramatsu 	return DIE_FIND_CB_SIBLING;
899e0d153c6SMasami Hiramatsu }
900e0d153c6SMasami Hiramatsu 
901e0d153c6SMasami Hiramatsu /**
902e0d153c6SMasami Hiramatsu  * die_find_member - Find a given name member in a data structure
903e0d153c6SMasami Hiramatsu  * @st_die: a data structure type DIE
904e0d153c6SMasami Hiramatsu  * @name: member name
905e0d153c6SMasami Hiramatsu  * @die_mem: a buffer for result DIE
906e0d153c6SMasami Hiramatsu  *
907e0d153c6SMasami Hiramatsu  * Find a member DIE called @name in @st_die.
908e0d153c6SMasami Hiramatsu  */
909e0d153c6SMasami Hiramatsu Dwarf_Die *die_find_member(Dwarf_Die *st_die, const char *name,
910e0d153c6SMasami Hiramatsu 			   Dwarf_Die *die_mem)
911e0d153c6SMasami Hiramatsu {
912e0d153c6SMasami Hiramatsu 	return die_find_child(st_die, __die_find_member_cb, (void *)name,
913e0d153c6SMasami Hiramatsu 			      die_mem);
914e0d153c6SMasami Hiramatsu }
915e0d153c6SMasami Hiramatsu 
916e0d153c6SMasami Hiramatsu /**
917e0d153c6SMasami Hiramatsu  * die_get_typename - Get the name of given variable DIE
918e0d153c6SMasami Hiramatsu  * @vr_die: a variable DIE
919fb9596d1SHe Kuang  * @buf: a strbuf for result type name
920e0d153c6SMasami Hiramatsu  *
921fb9596d1SHe Kuang  * Get the name of @vr_die and stores it to @buf. Return 0 if succeeded.
922fb9596d1SHe Kuang  * and Return -ENOENT if failed to find type name.
923e0d153c6SMasami Hiramatsu  * Note that the result will stores typedef name if possible, and stores
924e0d153c6SMasami Hiramatsu  * "*(function_type)" if the type is a function pointer.
925e0d153c6SMasami Hiramatsu  */
926fb9596d1SHe Kuang int die_get_typename(Dwarf_Die *vr_die, struct strbuf *buf)
927e0d153c6SMasami Hiramatsu {
928e0d153c6SMasami Hiramatsu 	Dwarf_Die type;
929fb9596d1SHe Kuang 	int tag, ret;
930e0d153c6SMasami Hiramatsu 	const char *tmp = "";
931e0d153c6SMasami Hiramatsu 
932e0d153c6SMasami Hiramatsu 	if (__die_get_real_type(vr_die, &type) == NULL)
933e0d153c6SMasami Hiramatsu 		return -ENOENT;
934e0d153c6SMasami Hiramatsu 
935e0d153c6SMasami Hiramatsu 	tag = dwarf_tag(&type);
936e0d153c6SMasami Hiramatsu 	if (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type)
937e0d153c6SMasami Hiramatsu 		tmp = "*";
938e0d153c6SMasami Hiramatsu 	else if (tag == DW_TAG_subroutine_type) {
939e0d153c6SMasami Hiramatsu 		/* Function pointer */
940bf4d5f25SMasami Hiramatsu 		return strbuf_add(buf, "(function_type)", 15);
941e0d153c6SMasami Hiramatsu 	} else {
942e0d153c6SMasami Hiramatsu 		if (!dwarf_diename(&type))
943e0d153c6SMasami Hiramatsu 			return -ENOENT;
944e0d153c6SMasami Hiramatsu 		if (tag == DW_TAG_union_type)
945e0d153c6SMasami Hiramatsu 			tmp = "union ";
946e0d153c6SMasami Hiramatsu 		else if (tag == DW_TAG_structure_type)
947e0d153c6SMasami Hiramatsu 			tmp = "struct ";
948bb2d17a0SHyeoncheol Lee 		else if (tag == DW_TAG_enumeration_type)
949bb2d17a0SHyeoncheol Lee 			tmp = "enum ";
950e0d153c6SMasami Hiramatsu 		/* Write a base name */
951bf4d5f25SMasami Hiramatsu 		return strbuf_addf(buf, "%s%s", tmp, dwarf_diename(&type));
952e0d153c6SMasami Hiramatsu 	}
953fb9596d1SHe Kuang 	ret = die_get_typename(&type, buf);
954bf4d5f25SMasami Hiramatsu 	return ret ? ret : strbuf_addstr(buf, tmp);
955e0d153c6SMasami Hiramatsu }
956e0d153c6SMasami Hiramatsu 
957e0d153c6SMasami Hiramatsu /**
958e0d153c6SMasami Hiramatsu  * die_get_varname - Get the name and type of given variable DIE
959e0d153c6SMasami Hiramatsu  * @vr_die: a variable DIE
960fb9596d1SHe Kuang  * @buf: a strbuf for type and variable name
961e0d153c6SMasami Hiramatsu  *
962e0d153c6SMasami Hiramatsu  * Get the name and type of @vr_die and stores it in @buf as "type\tname".
963e0d153c6SMasami Hiramatsu  */
964fb9596d1SHe Kuang int die_get_varname(Dwarf_Die *vr_die, struct strbuf *buf)
965e0d153c6SMasami Hiramatsu {
966fb9596d1SHe Kuang 	int ret;
967e0d153c6SMasami Hiramatsu 
968fb9596d1SHe Kuang 	ret = die_get_typename(vr_die, buf);
969e0d153c6SMasami Hiramatsu 	if (ret < 0) {
970e0d153c6SMasami Hiramatsu 		pr_debug("Failed to get type, make it unknown.\n");
971bf4d5f25SMasami Hiramatsu 		ret = strbuf_add(buf, " (unknown_type)", 14);
972e0d153c6SMasami Hiramatsu 	}
973fb9596d1SHe Kuang 
974bf4d5f25SMasami Hiramatsu 	return ret < 0 ? ret : strbuf_addf(buf, "\t%s", dwarf_diename(vr_die));
975e0d153c6SMasami Hiramatsu }
976e0d153c6SMasami Hiramatsu 
977a36ebe4eSJin Yao #ifdef HAVE_DWARF_GETLOCATIONS_SUPPORT
978349e8d26SHe Kuang /**
979349e8d26SHe Kuang  * die_get_var_innermost_scope - Get innermost scope range of given variable DIE
980349e8d26SHe Kuang  * @sp_die: a subprogram DIE
981349e8d26SHe Kuang  * @vr_die: a variable DIE
982349e8d26SHe Kuang  * @buf: a strbuf for variable byte offset range
983349e8d26SHe Kuang  *
984349e8d26SHe Kuang  * Get the innermost scope range of @vr_die and stores it in @buf as
985349e8d26SHe Kuang  * "@<function_name+[NN-NN,NN-NN]>".
986349e8d26SHe Kuang  */
987349e8d26SHe Kuang static int die_get_var_innermost_scope(Dwarf_Die *sp_die, Dwarf_Die *vr_die,
988349e8d26SHe Kuang 				struct strbuf *buf)
989349e8d26SHe Kuang {
990349e8d26SHe Kuang 	Dwarf_Die *scopes;
991349e8d26SHe Kuang 	int count;
992349e8d26SHe Kuang 	size_t offset = 0;
993349e8d26SHe Kuang 	Dwarf_Addr base;
994349e8d26SHe Kuang 	Dwarf_Addr start, end;
995349e8d26SHe Kuang 	Dwarf_Addr entry;
996349e8d26SHe Kuang 	int ret;
997349e8d26SHe Kuang 	bool first = true;
998349e8d26SHe Kuang 	const char *name;
999349e8d26SHe Kuang 
1000349e8d26SHe Kuang 	ret = dwarf_entrypc(sp_die, &entry);
1001349e8d26SHe Kuang 	if (ret)
1002349e8d26SHe Kuang 		return ret;
1003349e8d26SHe Kuang 
1004349e8d26SHe Kuang 	name = dwarf_diename(sp_die);
1005349e8d26SHe Kuang 	if (!name)
1006349e8d26SHe Kuang 		return -ENOENT;
1007349e8d26SHe Kuang 
1008349e8d26SHe Kuang 	count = dwarf_getscopes_die(vr_die, &scopes);
1009349e8d26SHe Kuang 
1010349e8d26SHe Kuang 	/* (*SCOPES)[1] is the DIE for the scope containing that scope */
1011349e8d26SHe Kuang 	if (count <= 1) {
1012349e8d26SHe Kuang 		ret = -EINVAL;
1013349e8d26SHe Kuang 		goto out;
1014349e8d26SHe Kuang 	}
1015349e8d26SHe Kuang 
1016349e8d26SHe Kuang 	while ((offset = dwarf_ranges(&scopes[1], offset, &base,
1017349e8d26SHe Kuang 					&start, &end)) > 0) {
1018349e8d26SHe Kuang 		start -= entry;
1019349e8d26SHe Kuang 		end -= entry;
1020349e8d26SHe Kuang 
1021349e8d26SHe Kuang 		if (first) {
1022bf4d5f25SMasami Hiramatsu 			ret = strbuf_addf(buf, "@<%s+[%" PRIu64 "-%" PRIu64,
1023349e8d26SHe Kuang 					  name, start, end);
1024349e8d26SHe Kuang 			first = false;
1025349e8d26SHe Kuang 		} else {
1026bf4d5f25SMasami Hiramatsu 			ret = strbuf_addf(buf, ",%" PRIu64 "-%" PRIu64,
1027349e8d26SHe Kuang 					  start, end);
1028349e8d26SHe Kuang 		}
1029bf4d5f25SMasami Hiramatsu 		if (ret < 0)
1030bf4d5f25SMasami Hiramatsu 			goto out;
1031349e8d26SHe Kuang 	}
1032349e8d26SHe Kuang 
1033349e8d26SHe Kuang 	if (!first)
1034bf4d5f25SMasami Hiramatsu 		ret = strbuf_add(buf, "]>", 2);
1035349e8d26SHe Kuang 
1036349e8d26SHe Kuang out:
1037349e8d26SHe Kuang 	free(scopes);
1038349e8d26SHe Kuang 	return ret;
1039349e8d26SHe Kuang }
1040349e8d26SHe Kuang 
1041349e8d26SHe Kuang /**
1042349e8d26SHe Kuang  * die_get_var_range - Get byte offset range of given variable DIE
1043349e8d26SHe Kuang  * @sp_die: a subprogram DIE
1044349e8d26SHe Kuang  * @vr_die: a variable DIE
1045349e8d26SHe Kuang  * @buf: a strbuf for type and variable name and byte offset range
1046349e8d26SHe Kuang  *
1047349e8d26SHe Kuang  * Get the byte offset range of @vr_die and stores it in @buf as
1048349e8d26SHe Kuang  * "@<function_name+[NN-NN,NN-NN]>".
1049349e8d26SHe Kuang  */
1050349e8d26SHe Kuang int die_get_var_range(Dwarf_Die *sp_die, Dwarf_Die *vr_die, struct strbuf *buf)
1051349e8d26SHe Kuang {
1052349e8d26SHe Kuang 	int ret = 0;
1053349e8d26SHe Kuang 	Dwarf_Addr base;
1054349e8d26SHe Kuang 	Dwarf_Addr start, end;
1055349e8d26SHe Kuang 	Dwarf_Addr entry;
1056349e8d26SHe Kuang 	Dwarf_Op *op;
1057349e8d26SHe Kuang 	size_t nops;
1058349e8d26SHe Kuang 	size_t offset = 0;
1059349e8d26SHe Kuang 	Dwarf_Attribute attr;
1060349e8d26SHe Kuang 	bool first = true;
1061349e8d26SHe Kuang 	const char *name;
1062349e8d26SHe Kuang 
1063349e8d26SHe Kuang 	ret = dwarf_entrypc(sp_die, &entry);
1064349e8d26SHe Kuang 	if (ret)
1065349e8d26SHe Kuang 		return ret;
1066349e8d26SHe Kuang 
1067349e8d26SHe Kuang 	name = dwarf_diename(sp_die);
1068349e8d26SHe Kuang 	if (!name)
1069349e8d26SHe Kuang 		return -ENOENT;
1070349e8d26SHe Kuang 
1071349e8d26SHe Kuang 	if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL)
1072349e8d26SHe Kuang 		return -EINVAL;
1073349e8d26SHe Kuang 
1074bf4d5f25SMasami Hiramatsu 	while ((offset = dwarf_getlocations(&attr, offset, &base,
1075349e8d26SHe Kuang 					&start, &end, &op, &nops)) > 0) {
1076349e8d26SHe Kuang 		if (start == 0) {
1077349e8d26SHe Kuang 			/* Single Location Descriptions */
1078349e8d26SHe Kuang 			ret = die_get_var_innermost_scope(sp_die, vr_die, buf);
1079bf4d5f25SMasami Hiramatsu 			goto out;
1080349e8d26SHe Kuang 		}
1081349e8d26SHe Kuang 
1082349e8d26SHe Kuang 		/* Location Lists */
1083349e8d26SHe Kuang 		start -= entry;
1084349e8d26SHe Kuang 		end -= entry;
1085349e8d26SHe Kuang 		if (first) {
1086bf4d5f25SMasami Hiramatsu 			ret = strbuf_addf(buf, "@<%s+[%" PRIu64 "-%" PRIu64,
1087349e8d26SHe Kuang 					  name, start, end);
1088349e8d26SHe Kuang 			first = false;
1089349e8d26SHe Kuang 		} else {
1090bf4d5f25SMasami Hiramatsu 			ret = strbuf_addf(buf, ",%" PRIu64 "-%" PRIu64,
1091349e8d26SHe Kuang 					  start, end);
1092349e8d26SHe Kuang 		}
1093bf4d5f25SMasami Hiramatsu 		if (ret < 0)
1094bf4d5f25SMasami Hiramatsu 			goto out;
1095349e8d26SHe Kuang 	}
1096349e8d26SHe Kuang 
1097349e8d26SHe Kuang 	if (!first)
1098bf4d5f25SMasami Hiramatsu 		ret = strbuf_add(buf, "]>", 2);
1099bf4d5f25SMasami Hiramatsu out:
1100349e8d26SHe Kuang 	return ret;
1101349e8d26SHe Kuang }
1102bd0419e2SArnaldo Carvalho de Melo #else
1103bd0419e2SArnaldo Carvalho de Melo int die_get_var_range(Dwarf_Die *sp_die __maybe_unused,
1104bd0419e2SArnaldo Carvalho de Melo 		      Dwarf_Die *vr_die __maybe_unused,
1105bd0419e2SArnaldo Carvalho de Melo 		      struct strbuf *buf __maybe_unused)
1106bd0419e2SArnaldo Carvalho de Melo {
1107bd0419e2SArnaldo Carvalho de Melo 	return -ENOTSUP;
1108bd0419e2SArnaldo Carvalho de Melo }
1109bd0419e2SArnaldo Carvalho de Melo #endif
11106243b9dcSRavi Bangoria 
11116243b9dcSRavi Bangoria /*
11126243b9dcSRavi Bangoria  * die_has_loclist - Check if DW_AT_location of @vr_die is a location list
11136243b9dcSRavi Bangoria  * @vr_die: a variable DIE
11146243b9dcSRavi Bangoria  */
11156243b9dcSRavi Bangoria static bool die_has_loclist(Dwarf_Die *vr_die)
11166243b9dcSRavi Bangoria {
11176243b9dcSRavi Bangoria 	Dwarf_Attribute loc;
11186243b9dcSRavi Bangoria 	int tag = dwarf_tag(vr_die);
11196243b9dcSRavi Bangoria 
11206243b9dcSRavi Bangoria 	if (tag != DW_TAG_formal_parameter &&
11216243b9dcSRavi Bangoria 	    tag != DW_TAG_variable)
11226243b9dcSRavi Bangoria 		return false;
11236243b9dcSRavi Bangoria 
11246243b9dcSRavi Bangoria 	return (dwarf_attr_integrate(vr_die, DW_AT_location, &loc) &&
11256243b9dcSRavi Bangoria 		dwarf_whatform(&loc) == DW_FORM_sec_offset);
11266243b9dcSRavi Bangoria }
11276243b9dcSRavi Bangoria 
11286243b9dcSRavi Bangoria /*
11296243b9dcSRavi Bangoria  * die_is_optimized_target - Check if target program is compiled with
11306243b9dcSRavi Bangoria  * optimization
11316243b9dcSRavi Bangoria  * @cu_die: a CU DIE
11326243b9dcSRavi Bangoria  *
11336243b9dcSRavi Bangoria  * For any object in given CU whose DW_AT_location is a location list,
11346243b9dcSRavi Bangoria  * target program is compiled with optimization. This is applicable to
11356243b9dcSRavi Bangoria  * clang as well.
11366243b9dcSRavi Bangoria  */
11376243b9dcSRavi Bangoria bool die_is_optimized_target(Dwarf_Die *cu_die)
11386243b9dcSRavi Bangoria {
11396243b9dcSRavi Bangoria 	Dwarf_Die tmp_die;
11406243b9dcSRavi Bangoria 
11416243b9dcSRavi Bangoria 	if (die_has_loclist(cu_die))
11426243b9dcSRavi Bangoria 		return true;
11436243b9dcSRavi Bangoria 
11446243b9dcSRavi Bangoria 	if (!dwarf_child(cu_die, &tmp_die) &&
11456243b9dcSRavi Bangoria 	    die_is_optimized_target(&tmp_die))
11466243b9dcSRavi Bangoria 		return true;
11476243b9dcSRavi Bangoria 
11486243b9dcSRavi Bangoria 	if (!dwarf_siblingof(cu_die, &tmp_die) &&
11496243b9dcSRavi Bangoria 	    die_is_optimized_target(&tmp_die))
11506243b9dcSRavi Bangoria 		return true;
11516243b9dcSRavi Bangoria 
11526243b9dcSRavi Bangoria 	return false;
11536243b9dcSRavi Bangoria }
11546243b9dcSRavi Bangoria 
11556243b9dcSRavi Bangoria /*
11566243b9dcSRavi Bangoria  * die_search_idx - Search index of given line address
11576243b9dcSRavi Bangoria  * @lines: Line records of single CU
11586243b9dcSRavi Bangoria  * @nr_lines: Number of @lines
11596243b9dcSRavi Bangoria  * @addr: address we are looking for
11606243b9dcSRavi Bangoria  * @idx: index to be set by this function (return value)
11616243b9dcSRavi Bangoria  *
11626243b9dcSRavi Bangoria  * Search for @addr by looping over every lines of CU. If address
11636243b9dcSRavi Bangoria  * matches, set index of that line in @idx. Note that single source
11646243b9dcSRavi Bangoria  * line can have multiple line records. i.e. single source line can
11656243b9dcSRavi Bangoria  * have multiple index.
11666243b9dcSRavi Bangoria  */
11676243b9dcSRavi Bangoria static bool die_search_idx(Dwarf_Lines *lines, unsigned long nr_lines,
11686243b9dcSRavi Bangoria 			   Dwarf_Addr addr, unsigned long *idx)
11696243b9dcSRavi Bangoria {
11706243b9dcSRavi Bangoria 	unsigned long i;
11716243b9dcSRavi Bangoria 	Dwarf_Addr tmp;
11726243b9dcSRavi Bangoria 
11736243b9dcSRavi Bangoria 	for (i = 0; i < nr_lines; i++) {
11746243b9dcSRavi Bangoria 		if (dwarf_lineaddr(dwarf_onesrcline(lines, i), &tmp))
11756243b9dcSRavi Bangoria 			return false;
11766243b9dcSRavi Bangoria 
11776243b9dcSRavi Bangoria 		if (tmp == addr) {
11786243b9dcSRavi Bangoria 			*idx = i;
11796243b9dcSRavi Bangoria 			return true;
11806243b9dcSRavi Bangoria 		}
11816243b9dcSRavi Bangoria 	}
11826243b9dcSRavi Bangoria 	return false;
11836243b9dcSRavi Bangoria }
11846243b9dcSRavi Bangoria 
11856243b9dcSRavi Bangoria /*
11866243b9dcSRavi Bangoria  * die_get_postprologue_addr - Search next address after function prologue
11876243b9dcSRavi Bangoria  * @entrypc_idx: entrypc index
11886243b9dcSRavi Bangoria  * @lines: Line records of single CU
11896243b9dcSRavi Bangoria  * @nr_lines: Number of @lines
11906243b9dcSRavi Bangoria  * @hignpc: high PC address of function
11916243b9dcSRavi Bangoria  * @postprologue_addr: Next address after function prologue (return value)
11926243b9dcSRavi Bangoria  *
11936243b9dcSRavi Bangoria  * Look for prologue-end marker. If there is no explicit marker, return
11946243b9dcSRavi Bangoria  * address of next line record or next source line.
11956243b9dcSRavi Bangoria  */
11966243b9dcSRavi Bangoria static bool die_get_postprologue_addr(unsigned long entrypc_idx,
11976243b9dcSRavi Bangoria 				      Dwarf_Lines *lines,
11986243b9dcSRavi Bangoria 				      unsigned long nr_lines,
11996243b9dcSRavi Bangoria 				      Dwarf_Addr highpc,
12006243b9dcSRavi Bangoria 				      Dwarf_Addr *postprologue_addr)
12016243b9dcSRavi Bangoria {
12026243b9dcSRavi Bangoria 	unsigned long i;
12036243b9dcSRavi Bangoria 	int entrypc_lno, lno;
12046243b9dcSRavi Bangoria 	Dwarf_Line *line;
12056243b9dcSRavi Bangoria 	Dwarf_Addr addr;
12066243b9dcSRavi Bangoria 	bool p_end;
12076243b9dcSRavi Bangoria 
12086243b9dcSRavi Bangoria 	/* entrypc_lno is actual source line number */
12096243b9dcSRavi Bangoria 	line = dwarf_onesrcline(lines, entrypc_idx);
12106243b9dcSRavi Bangoria 	if (dwarf_lineno(line, &entrypc_lno))
12116243b9dcSRavi Bangoria 		return false;
12126243b9dcSRavi Bangoria 
12136243b9dcSRavi Bangoria 	for (i = entrypc_idx; i < nr_lines; i++) {
12146243b9dcSRavi Bangoria 		line = dwarf_onesrcline(lines, i);
12156243b9dcSRavi Bangoria 
12166243b9dcSRavi Bangoria 		if (dwarf_lineaddr(line, &addr) ||
12176243b9dcSRavi Bangoria 		    dwarf_lineno(line, &lno)    ||
12186243b9dcSRavi Bangoria 		    dwarf_lineprologueend(line, &p_end))
12196243b9dcSRavi Bangoria 			return false;
12206243b9dcSRavi Bangoria 
12216243b9dcSRavi Bangoria 		/* highpc is exclusive. [entrypc,highpc) */
12226243b9dcSRavi Bangoria 		if (addr >= highpc)
12236243b9dcSRavi Bangoria 			break;
12246243b9dcSRavi Bangoria 
12256243b9dcSRavi Bangoria 		/* clang supports prologue-end marker */
12266243b9dcSRavi Bangoria 		if (p_end)
12276243b9dcSRavi Bangoria 			break;
12286243b9dcSRavi Bangoria 
12296243b9dcSRavi Bangoria 		/* Actual next line in source */
12306243b9dcSRavi Bangoria 		if (lno != entrypc_lno)
12316243b9dcSRavi Bangoria 			break;
12326243b9dcSRavi Bangoria 
12336243b9dcSRavi Bangoria 		/*
12346243b9dcSRavi Bangoria 		 * Single source line can have multiple line records.
12356243b9dcSRavi Bangoria 		 * For Example,
12366243b9dcSRavi Bangoria 		 *     void foo() { printf("hello\n"); }
12376243b9dcSRavi Bangoria 		 * contains two line records. One points to declaration and
12386243b9dcSRavi Bangoria 		 * other points to printf() line. Variable 'lno' won't get
12396243b9dcSRavi Bangoria 		 * incremented in this case but 'i' will.
12406243b9dcSRavi Bangoria 		 */
12416243b9dcSRavi Bangoria 		if (i != entrypc_idx)
12426243b9dcSRavi Bangoria 			break;
12436243b9dcSRavi Bangoria 	}
12446243b9dcSRavi Bangoria 
12456243b9dcSRavi Bangoria 	dwarf_lineaddr(line, postprologue_addr);
12466243b9dcSRavi Bangoria 	if (*postprologue_addr >= highpc)
12476243b9dcSRavi Bangoria 		dwarf_lineaddr(dwarf_onesrcline(lines, i - 1),
12486243b9dcSRavi Bangoria 			       postprologue_addr);
12496243b9dcSRavi Bangoria 
12506243b9dcSRavi Bangoria 	return true;
12516243b9dcSRavi Bangoria }
12526243b9dcSRavi Bangoria 
12536243b9dcSRavi Bangoria /*
12546243b9dcSRavi Bangoria  * die_skip_prologue - Use next address after prologue as probe location
12556243b9dcSRavi Bangoria  * @sp_die: a subprogram DIE
12566243b9dcSRavi Bangoria  * @cu_die: a CU DIE
12576243b9dcSRavi Bangoria  * @entrypc: entrypc of the function
12586243b9dcSRavi Bangoria  *
12596243b9dcSRavi Bangoria  * Function prologue prepares stack and registers before executing function
12606243b9dcSRavi Bangoria  * logic. When target program is compiled without optimization, function
12616243b9dcSRavi Bangoria  * parameter information is only valid after prologue. When we probe entrypc
12626243b9dcSRavi Bangoria  * of the function, and try to record function parameter, it contains
12636243b9dcSRavi Bangoria  * garbage value.
12646243b9dcSRavi Bangoria  */
12656243b9dcSRavi Bangoria void die_skip_prologue(Dwarf_Die *sp_die, Dwarf_Die *cu_die,
12666243b9dcSRavi Bangoria 		       Dwarf_Addr *entrypc)
12676243b9dcSRavi Bangoria {
12686243b9dcSRavi Bangoria 	size_t nr_lines = 0;
12696243b9dcSRavi Bangoria 	unsigned long entrypc_idx = 0;
12706243b9dcSRavi Bangoria 	Dwarf_Lines *lines = NULL;
12716243b9dcSRavi Bangoria 	Dwarf_Addr postprologue_addr;
12726243b9dcSRavi Bangoria 	Dwarf_Addr highpc;
12736243b9dcSRavi Bangoria 
12746243b9dcSRavi Bangoria 	if (dwarf_highpc(sp_die, &highpc))
12756243b9dcSRavi Bangoria 		return;
12766243b9dcSRavi Bangoria 
12776243b9dcSRavi Bangoria 	if (dwarf_getsrclines(cu_die, &lines, &nr_lines))
12786243b9dcSRavi Bangoria 		return;
12796243b9dcSRavi Bangoria 
12806243b9dcSRavi Bangoria 	if (!die_search_idx(lines, nr_lines, *entrypc, &entrypc_idx))
12816243b9dcSRavi Bangoria 		return;
12826243b9dcSRavi Bangoria 
12836243b9dcSRavi Bangoria 	if (!die_get_postprologue_addr(entrypc_idx, lines, nr_lines,
12846243b9dcSRavi Bangoria 				       highpc, &postprologue_addr))
12856243b9dcSRavi Bangoria 		return;
12866243b9dcSRavi Bangoria 
12876243b9dcSRavi Bangoria 	*entrypc = postprologue_addr;
12886243b9dcSRavi Bangoria }
1289