xref: /openbmc/linux/tools/perf/util/dwarf-aux.c (revision bf4d5f25c90bf2eca8671f2fc4e3d15919cd7f9c)
1e0d153c6SMasami Hiramatsu /*
2e0d153c6SMasami Hiramatsu  * dwarf-aux.c : libdw auxiliary interfaces
3e0d153c6SMasami Hiramatsu  *
4e0d153c6SMasami Hiramatsu  * This program is free software; you can redistribute it and/or modify
5e0d153c6SMasami Hiramatsu  * it under the terms of the GNU General Public License as published by
6e0d153c6SMasami Hiramatsu  * the Free Software Foundation; either version 2 of the License, or
7e0d153c6SMasami Hiramatsu  * (at your option) any later version.
8e0d153c6SMasami Hiramatsu  *
9e0d153c6SMasami Hiramatsu  * This program is distributed in the hope that it will be useful,
10e0d153c6SMasami Hiramatsu  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11e0d153c6SMasami Hiramatsu  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12e0d153c6SMasami Hiramatsu  * GNU General Public License for more details.
13e0d153c6SMasami Hiramatsu  *
14e0d153c6SMasami Hiramatsu  * You should have received a copy of the GNU General Public License
15e0d153c6SMasami Hiramatsu  * along with this program; if not, write to the Free Software
16e0d153c6SMasami Hiramatsu  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17e0d153c6SMasami Hiramatsu  *
18e0d153c6SMasami Hiramatsu  */
19e0d153c6SMasami Hiramatsu 
20e0d153c6SMasami Hiramatsu #include <stdbool.h>
21e0d153c6SMasami Hiramatsu #include "util.h"
22e0d153c6SMasami Hiramatsu #include "debug.h"
23e0d153c6SMasami Hiramatsu #include "dwarf-aux.h"
24e0d153c6SMasami Hiramatsu 
25e0d153c6SMasami Hiramatsu /**
26e0d153c6SMasami Hiramatsu  * cu_find_realpath - Find the realpath of the target file
27e0d153c6SMasami Hiramatsu  * @cu_die: A DIE(dwarf information entry) of CU(compilation Unit)
28e0d153c6SMasami Hiramatsu  * @fname:  The tail filename of the target file
29e0d153c6SMasami Hiramatsu  *
30e0d153c6SMasami Hiramatsu  * Find the real(long) path of @fname in @cu_die.
31e0d153c6SMasami Hiramatsu  */
32e0d153c6SMasami Hiramatsu const char *cu_find_realpath(Dwarf_Die *cu_die, const char *fname)
33e0d153c6SMasami Hiramatsu {
34e0d153c6SMasami Hiramatsu 	Dwarf_Files *files;
35e0d153c6SMasami Hiramatsu 	size_t nfiles, i;
36e0d153c6SMasami Hiramatsu 	const char *src = NULL;
37e0d153c6SMasami Hiramatsu 	int ret;
38e0d153c6SMasami Hiramatsu 
39e0d153c6SMasami Hiramatsu 	if (!fname)
40e0d153c6SMasami Hiramatsu 		return NULL;
41e0d153c6SMasami Hiramatsu 
42e0d153c6SMasami Hiramatsu 	ret = dwarf_getsrcfiles(cu_die, &files, &nfiles);
43e0d153c6SMasami Hiramatsu 	if (ret != 0)
44e0d153c6SMasami Hiramatsu 		return NULL;
45e0d153c6SMasami Hiramatsu 
46e0d153c6SMasami Hiramatsu 	for (i = 0; i < nfiles; i++) {
47e0d153c6SMasami Hiramatsu 		src = dwarf_filesrc(files, i, NULL, NULL);
48e0d153c6SMasami Hiramatsu 		if (strtailcmp(src, fname) == 0)
49e0d153c6SMasami Hiramatsu 			break;
50e0d153c6SMasami Hiramatsu 	}
51e0d153c6SMasami Hiramatsu 	if (i == nfiles)
52e0d153c6SMasami Hiramatsu 		return NULL;
53e0d153c6SMasami Hiramatsu 	return src;
54e0d153c6SMasami Hiramatsu }
55e0d153c6SMasami Hiramatsu 
56e0d153c6SMasami Hiramatsu /**
57e0d153c6SMasami Hiramatsu  * cu_get_comp_dir - Get the path of compilation directory
58e0d153c6SMasami Hiramatsu  * @cu_die: a CU DIE
59e0d153c6SMasami Hiramatsu  *
60e0d153c6SMasami Hiramatsu  * Get the path of compilation directory of given @cu_die.
61e0d153c6SMasami Hiramatsu  * Since this depends on DW_AT_comp_dir, older gcc will not
62e0d153c6SMasami Hiramatsu  * embedded it. In that case, this returns NULL.
63e0d153c6SMasami Hiramatsu  */
64e0d153c6SMasami Hiramatsu const char *cu_get_comp_dir(Dwarf_Die *cu_die)
65e0d153c6SMasami Hiramatsu {
66e0d153c6SMasami Hiramatsu 	Dwarf_Attribute attr;
67e0d153c6SMasami Hiramatsu 	if (dwarf_attr(cu_die, DW_AT_comp_dir, &attr) == NULL)
68e0d153c6SMasami Hiramatsu 		return NULL;
69e0d153c6SMasami Hiramatsu 	return dwarf_formstring(&attr);
70e0d153c6SMasami Hiramatsu }
71e0d153c6SMasami Hiramatsu 
72e0d153c6SMasami Hiramatsu /**
73e0d153c6SMasami Hiramatsu  * cu_find_lineinfo - Get a line number and file name for given address
74e0d153c6SMasami Hiramatsu  * @cu_die: a CU DIE
75e0d153c6SMasami Hiramatsu  * @addr: An address
76e0d153c6SMasami Hiramatsu  * @fname: a pointer which returns the file name string
77e0d153c6SMasami Hiramatsu  * @lineno: a pointer which returns the line number
78e0d153c6SMasami Hiramatsu  *
79e0d153c6SMasami Hiramatsu  * Find a line number and file name for @addr in @cu_die.
80e0d153c6SMasami Hiramatsu  */
81e0d153c6SMasami Hiramatsu int cu_find_lineinfo(Dwarf_Die *cu_die, unsigned long addr,
82e0d153c6SMasami Hiramatsu 		    const char **fname, int *lineno)
83e0d153c6SMasami Hiramatsu {
84e0d153c6SMasami Hiramatsu 	Dwarf_Line *line;
85e0d153c6SMasami Hiramatsu 	Dwarf_Addr laddr;
86e0d153c6SMasami Hiramatsu 
87e0d153c6SMasami Hiramatsu 	line = dwarf_getsrc_die(cu_die, (Dwarf_Addr)addr);
88e0d153c6SMasami Hiramatsu 	if (line && dwarf_lineaddr(line, &laddr) == 0 &&
89e0d153c6SMasami Hiramatsu 	    addr == (unsigned long)laddr && dwarf_lineno(line, lineno) == 0) {
90e0d153c6SMasami Hiramatsu 		*fname = dwarf_linesrc(line, NULL, NULL);
91e0d153c6SMasami Hiramatsu 		if (!*fname)
92e0d153c6SMasami Hiramatsu 			/* line number is useless without filename */
93e0d153c6SMasami Hiramatsu 			*lineno = 0;
94e0d153c6SMasami Hiramatsu 	}
95e0d153c6SMasami Hiramatsu 
96e0d153c6SMasami Hiramatsu 	return *lineno ?: -ENOENT;
97e0d153c6SMasami Hiramatsu }
98e0d153c6SMasami Hiramatsu 
99221d0611SMasami Hiramatsu static int __die_find_inline_cb(Dwarf_Die *die_mem, void *data);
100221d0611SMasami Hiramatsu 
101221d0611SMasami Hiramatsu /**
102221d0611SMasami Hiramatsu  * cu_walk_functions_at - Walk on function DIEs at given address
103221d0611SMasami Hiramatsu  * @cu_die: A CU DIE
104221d0611SMasami Hiramatsu  * @addr: An address
105221d0611SMasami Hiramatsu  * @callback: A callback which called with found DIEs
106221d0611SMasami Hiramatsu  * @data: A user data
107221d0611SMasami Hiramatsu  *
108221d0611SMasami Hiramatsu  * Walk on function DIEs at given @addr in @cu_die. Passed DIEs
109221d0611SMasami Hiramatsu  * should be subprogram or inlined-subroutines.
110221d0611SMasami Hiramatsu  */
111221d0611SMasami Hiramatsu int cu_walk_functions_at(Dwarf_Die *cu_die, Dwarf_Addr addr,
112221d0611SMasami Hiramatsu 		    int (*callback)(Dwarf_Die *, void *), void *data)
113221d0611SMasami Hiramatsu {
114221d0611SMasami Hiramatsu 	Dwarf_Die die_mem;
115221d0611SMasami Hiramatsu 	Dwarf_Die *sc_die;
116221d0611SMasami Hiramatsu 	int ret = -ENOENT;
117221d0611SMasami Hiramatsu 
118221d0611SMasami Hiramatsu 	/* Inlined function could be recursive. Trace it until fail */
119221d0611SMasami Hiramatsu 	for (sc_die = die_find_realfunc(cu_die, addr, &die_mem);
120221d0611SMasami Hiramatsu 	     sc_die != NULL;
121221d0611SMasami Hiramatsu 	     sc_die = die_find_child(sc_die, __die_find_inline_cb, &addr,
122221d0611SMasami Hiramatsu 				     &die_mem)) {
123221d0611SMasami Hiramatsu 		ret = callback(sc_die, data);
124221d0611SMasami Hiramatsu 		if (ret)
125221d0611SMasami Hiramatsu 			break;
126221d0611SMasami Hiramatsu 	}
127221d0611SMasami Hiramatsu 
128221d0611SMasami Hiramatsu 	return ret;
129221d0611SMasami Hiramatsu 
130221d0611SMasami Hiramatsu }
131221d0611SMasami Hiramatsu 
132e0d153c6SMasami Hiramatsu /**
133e0d153c6SMasami Hiramatsu  * die_compare_name - Compare diename and tname
134e0d153c6SMasami Hiramatsu  * @dw_die: a DIE
135e0d153c6SMasami Hiramatsu  * @tname: a string of target name
136e0d153c6SMasami Hiramatsu  *
137e0d153c6SMasami Hiramatsu  * Compare the name of @dw_die and @tname. Return false if @dw_die has no name.
138e0d153c6SMasami Hiramatsu  */
139e0d153c6SMasami Hiramatsu bool die_compare_name(Dwarf_Die *dw_die, const char *tname)
140e0d153c6SMasami Hiramatsu {
141e0d153c6SMasami Hiramatsu 	const char *name;
1424c859351SMasami Hiramatsu 
143e0d153c6SMasami Hiramatsu 	name = dwarf_diename(dw_die);
144e0d153c6SMasami Hiramatsu 	return name ? (strcmp(tname, name) == 0) : false;
145e0d153c6SMasami Hiramatsu }
146e0d153c6SMasami Hiramatsu 
147e0d153c6SMasami Hiramatsu /**
1484c859351SMasami Hiramatsu  * die_match_name - Match diename and glob
1494c859351SMasami Hiramatsu  * @dw_die: a DIE
1504c859351SMasami Hiramatsu  * @glob: a string of target glob pattern
1514c859351SMasami Hiramatsu  *
1524c859351SMasami Hiramatsu  * Glob matching the name of @dw_die and @glob. Return false if matching fail.
1534c859351SMasami Hiramatsu  */
1544c859351SMasami Hiramatsu bool die_match_name(Dwarf_Die *dw_die, const char *glob)
1554c859351SMasami Hiramatsu {
1564c859351SMasami Hiramatsu 	const char *name;
1574c859351SMasami Hiramatsu 
1584c859351SMasami Hiramatsu 	name = dwarf_diename(dw_die);
1594c859351SMasami Hiramatsu 	return name ? strglobmatch(name, glob) : false;
1604c859351SMasami Hiramatsu }
1614c859351SMasami Hiramatsu 
1624c859351SMasami Hiramatsu /**
163e0d153c6SMasami Hiramatsu  * die_get_call_lineno - Get callsite line number of inline-function instance
164e0d153c6SMasami Hiramatsu  * @in_die: a DIE of an inlined function instance
165e0d153c6SMasami Hiramatsu  *
166e0d153c6SMasami Hiramatsu  * Get call-site line number of @in_die. This means from where the inline
167e0d153c6SMasami Hiramatsu  * function is called.
168e0d153c6SMasami Hiramatsu  */
169e0d153c6SMasami Hiramatsu int die_get_call_lineno(Dwarf_Die *in_die)
170e0d153c6SMasami Hiramatsu {
171e0d153c6SMasami Hiramatsu 	Dwarf_Attribute attr;
172e0d153c6SMasami Hiramatsu 	Dwarf_Word ret;
173e0d153c6SMasami Hiramatsu 
174e0d153c6SMasami Hiramatsu 	if (!dwarf_attr(in_die, DW_AT_call_line, &attr))
175e0d153c6SMasami Hiramatsu 		return -ENOENT;
176e0d153c6SMasami Hiramatsu 
177e0d153c6SMasami Hiramatsu 	dwarf_formudata(&attr, &ret);
178e0d153c6SMasami Hiramatsu 	return (int)ret;
179e0d153c6SMasami Hiramatsu }
180e0d153c6SMasami Hiramatsu 
181e0d153c6SMasami Hiramatsu /**
182e0d153c6SMasami Hiramatsu  * die_get_type - Get type DIE
183e0d153c6SMasami Hiramatsu  * @vr_die: a DIE of a variable
184e0d153c6SMasami Hiramatsu  * @die_mem: where to store a type DIE
185e0d153c6SMasami Hiramatsu  *
186e0d153c6SMasami Hiramatsu  * Get a DIE of the type of given variable (@vr_die), and store
187e0d153c6SMasami Hiramatsu  * it to die_mem. Return NULL if fails to get a type DIE.
188e0d153c6SMasami Hiramatsu  */
189e0d153c6SMasami Hiramatsu Dwarf_Die *die_get_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
190e0d153c6SMasami Hiramatsu {
191e0d153c6SMasami Hiramatsu 	Dwarf_Attribute attr;
192e0d153c6SMasami Hiramatsu 
193e0d153c6SMasami Hiramatsu 	if (dwarf_attr_integrate(vr_die, DW_AT_type, &attr) &&
194e0d153c6SMasami Hiramatsu 	    dwarf_formref_die(&attr, die_mem))
195e0d153c6SMasami Hiramatsu 		return die_mem;
196e0d153c6SMasami Hiramatsu 	else
197e0d153c6SMasami Hiramatsu 		return NULL;
198e0d153c6SMasami Hiramatsu }
199e0d153c6SMasami Hiramatsu 
200e0d153c6SMasami Hiramatsu /* Get a type die, but skip qualifiers */
201e0d153c6SMasami Hiramatsu static Dwarf_Die *__die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
202e0d153c6SMasami Hiramatsu {
203e0d153c6SMasami Hiramatsu 	int tag;
204e0d153c6SMasami Hiramatsu 
205e0d153c6SMasami Hiramatsu 	do {
206e0d153c6SMasami Hiramatsu 		vr_die = die_get_type(vr_die, die_mem);
207e0d153c6SMasami Hiramatsu 		if (!vr_die)
208e0d153c6SMasami Hiramatsu 			break;
209e0d153c6SMasami Hiramatsu 		tag = dwarf_tag(vr_die);
210e0d153c6SMasami Hiramatsu 	} while (tag == DW_TAG_const_type ||
211e0d153c6SMasami Hiramatsu 		 tag == DW_TAG_restrict_type ||
212e0d153c6SMasami Hiramatsu 		 tag == DW_TAG_volatile_type ||
213e0d153c6SMasami Hiramatsu 		 tag == DW_TAG_shared_type);
214e0d153c6SMasami Hiramatsu 
215e0d153c6SMasami Hiramatsu 	return vr_die;
216e0d153c6SMasami Hiramatsu }
217e0d153c6SMasami Hiramatsu 
218e0d153c6SMasami Hiramatsu /**
219e0d153c6SMasami Hiramatsu  * die_get_real_type - Get a type die, but skip qualifiers and typedef
220e0d153c6SMasami Hiramatsu  * @vr_die: a DIE of a variable
221e0d153c6SMasami Hiramatsu  * @die_mem: where to store a type DIE
222e0d153c6SMasami Hiramatsu  *
223e0d153c6SMasami Hiramatsu  * Get a DIE of the type of given variable (@vr_die), and store
224e0d153c6SMasami Hiramatsu  * it to die_mem. Return NULL if fails to get a type DIE.
225e0d153c6SMasami Hiramatsu  * If the type is qualifiers (e.g. const) or typedef, this skips it
226e0d153c6SMasami Hiramatsu  * and tries to find real type (structure or basic types, e.g. int).
227e0d153c6SMasami Hiramatsu  */
228e0d153c6SMasami Hiramatsu Dwarf_Die *die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
229e0d153c6SMasami Hiramatsu {
230e0d153c6SMasami Hiramatsu 	do {
231e0d153c6SMasami Hiramatsu 		vr_die = __die_get_real_type(vr_die, die_mem);
232e0d153c6SMasami Hiramatsu 	} while (vr_die && dwarf_tag(vr_die) == DW_TAG_typedef);
233e0d153c6SMasami Hiramatsu 
234e0d153c6SMasami Hiramatsu 	return vr_die;
235e0d153c6SMasami Hiramatsu }
236e0d153c6SMasami Hiramatsu 
237e0d153c6SMasami Hiramatsu /* Get attribute and translate it as a udata */
238e0d153c6SMasami Hiramatsu static int die_get_attr_udata(Dwarf_Die *tp_die, unsigned int attr_name,
239e0d153c6SMasami Hiramatsu 			      Dwarf_Word *result)
240e0d153c6SMasami Hiramatsu {
241e0d153c6SMasami Hiramatsu 	Dwarf_Attribute attr;
242e0d153c6SMasami Hiramatsu 
243e0d153c6SMasami Hiramatsu 	if (dwarf_attr(tp_die, attr_name, &attr) == NULL ||
244e0d153c6SMasami Hiramatsu 	    dwarf_formudata(&attr, result) != 0)
245e0d153c6SMasami Hiramatsu 		return -ENOENT;
246e0d153c6SMasami Hiramatsu 
247e0d153c6SMasami Hiramatsu 	return 0;
248e0d153c6SMasami Hiramatsu }
249e0d153c6SMasami Hiramatsu 
250b0e9cb28SMasami Hiramatsu /* Get attribute and translate it as a sdata */
251b0e9cb28SMasami Hiramatsu static int die_get_attr_sdata(Dwarf_Die *tp_die, unsigned int attr_name,
252b0e9cb28SMasami Hiramatsu 			      Dwarf_Sword *result)
253b0e9cb28SMasami Hiramatsu {
254b0e9cb28SMasami Hiramatsu 	Dwarf_Attribute attr;
255b0e9cb28SMasami Hiramatsu 
256b0e9cb28SMasami Hiramatsu 	if (dwarf_attr(tp_die, attr_name, &attr) == NULL ||
257b0e9cb28SMasami Hiramatsu 	    dwarf_formsdata(&attr, result) != 0)
258b0e9cb28SMasami Hiramatsu 		return -ENOENT;
259b0e9cb28SMasami Hiramatsu 
260b0e9cb28SMasami Hiramatsu 	return 0;
261b0e9cb28SMasami Hiramatsu }
262b0e9cb28SMasami Hiramatsu 
263e0d153c6SMasami Hiramatsu /**
264e0d153c6SMasami Hiramatsu  * die_is_signed_type - Check whether a type DIE is signed or not
265e0d153c6SMasami Hiramatsu  * @tp_die: a DIE of a type
266e0d153c6SMasami Hiramatsu  *
267e0d153c6SMasami Hiramatsu  * Get the encoding of @tp_die and return true if the encoding
268e0d153c6SMasami Hiramatsu  * is signed.
269e0d153c6SMasami Hiramatsu  */
270e0d153c6SMasami Hiramatsu bool die_is_signed_type(Dwarf_Die *tp_die)
271e0d153c6SMasami Hiramatsu {
272e0d153c6SMasami Hiramatsu 	Dwarf_Word ret;
273e0d153c6SMasami Hiramatsu 
274e0d153c6SMasami Hiramatsu 	if (die_get_attr_udata(tp_die, DW_AT_encoding, &ret))
275e0d153c6SMasami Hiramatsu 		return false;
276e0d153c6SMasami Hiramatsu 
277e0d153c6SMasami Hiramatsu 	return (ret == DW_ATE_signed_char || ret == DW_ATE_signed ||
278e0d153c6SMasami Hiramatsu 		ret == DW_ATE_signed_fixed);
279e0d153c6SMasami Hiramatsu }
280e0d153c6SMasami Hiramatsu 
281e0d153c6SMasami Hiramatsu /**
2820dbb1cacSMasami Hiramatsu  * die_is_func_def - Ensure that this DIE is a subprogram and definition
2830dbb1cacSMasami Hiramatsu  * @dw_die: a DIE
2840dbb1cacSMasami Hiramatsu  *
2850dbb1cacSMasami Hiramatsu  * Ensure that this DIE is a subprogram and NOT a declaration. This
2860dbb1cacSMasami Hiramatsu  * returns true if @dw_die is a function definition.
2870dbb1cacSMasami Hiramatsu  **/
2880dbb1cacSMasami Hiramatsu bool die_is_func_def(Dwarf_Die *dw_die)
2890dbb1cacSMasami Hiramatsu {
2900dbb1cacSMasami Hiramatsu 	Dwarf_Attribute attr;
2910dbb1cacSMasami Hiramatsu 
2920dbb1cacSMasami Hiramatsu 	return (dwarf_tag(dw_die) == DW_TAG_subprogram &&
2930dbb1cacSMasami Hiramatsu 		dwarf_attr(dw_die, DW_AT_declaration, &attr) == NULL);
2940dbb1cacSMasami Hiramatsu }
2950dbb1cacSMasami Hiramatsu 
2960dbb1cacSMasami Hiramatsu /**
297e1ecbbc3SMasami Hiramatsu  * die_is_func_instance - Ensure that this DIE is an instance of a subprogram
298e1ecbbc3SMasami Hiramatsu  * @dw_die: a DIE
299e1ecbbc3SMasami Hiramatsu  *
300e1ecbbc3SMasami Hiramatsu  * Ensure that this DIE is an instance (which has an entry address).
301e1ecbbc3SMasami Hiramatsu  * This returns true if @dw_die is a function instance. If not, you need to
302e1ecbbc3SMasami Hiramatsu  * call die_walk_instances() to find actual instances.
303e1ecbbc3SMasami Hiramatsu  **/
304e1ecbbc3SMasami Hiramatsu bool die_is_func_instance(Dwarf_Die *dw_die)
305e1ecbbc3SMasami Hiramatsu {
306e1ecbbc3SMasami Hiramatsu 	Dwarf_Addr tmp;
307e1ecbbc3SMasami Hiramatsu 
308e1ecbbc3SMasami Hiramatsu 	/* Actually gcc optimizes non-inline as like as inlined */
309e1ecbbc3SMasami Hiramatsu 	return !dwarf_func_inline(dw_die) && dwarf_entrypc(dw_die, &tmp) == 0;
310e1ecbbc3SMasami Hiramatsu }
311e1ecbbc3SMasami Hiramatsu /**
312e0d153c6SMasami Hiramatsu  * die_get_data_member_location - Get the data-member offset
313e0d153c6SMasami Hiramatsu  * @mb_die: a DIE of a member of a data structure
314e0d153c6SMasami Hiramatsu  * @offs: The offset of the member in the data structure
315e0d153c6SMasami Hiramatsu  *
316e0d153c6SMasami Hiramatsu  * Get the offset of @mb_die in the data structure including @mb_die, and
317e0d153c6SMasami Hiramatsu  * stores result offset to @offs. If any error occurs this returns errno.
318e0d153c6SMasami Hiramatsu  */
319e0d153c6SMasami Hiramatsu int die_get_data_member_location(Dwarf_Die *mb_die, Dwarf_Word *offs)
320e0d153c6SMasami Hiramatsu {
321e0d153c6SMasami Hiramatsu 	Dwarf_Attribute attr;
322e0d153c6SMasami Hiramatsu 	Dwarf_Op *expr;
323e0d153c6SMasami Hiramatsu 	size_t nexpr;
324e0d153c6SMasami Hiramatsu 	int ret;
325e0d153c6SMasami Hiramatsu 
326e0d153c6SMasami Hiramatsu 	if (dwarf_attr(mb_die, DW_AT_data_member_location, &attr) == NULL)
327e0d153c6SMasami Hiramatsu 		return -ENOENT;
328e0d153c6SMasami Hiramatsu 
329e0d153c6SMasami Hiramatsu 	if (dwarf_formudata(&attr, offs) != 0) {
330e0d153c6SMasami Hiramatsu 		/* DW_AT_data_member_location should be DW_OP_plus_uconst */
331e0d153c6SMasami Hiramatsu 		ret = dwarf_getlocation(&attr, &expr, &nexpr);
332e0d153c6SMasami Hiramatsu 		if (ret < 0 || nexpr == 0)
333e0d153c6SMasami Hiramatsu 			return -ENOENT;
334e0d153c6SMasami Hiramatsu 
335e0d153c6SMasami Hiramatsu 		if (expr[0].atom != DW_OP_plus_uconst || nexpr != 1) {
336e0d153c6SMasami Hiramatsu 			pr_debug("Unable to get offset:Unexpected OP %x (%zd)\n",
337e0d153c6SMasami Hiramatsu 				 expr[0].atom, nexpr);
338e0d153c6SMasami Hiramatsu 			return -ENOTSUP;
339e0d153c6SMasami Hiramatsu 		}
340e0d153c6SMasami Hiramatsu 		*offs = (Dwarf_Word)expr[0].number;
341e0d153c6SMasami Hiramatsu 	}
342e0d153c6SMasami Hiramatsu 	return 0;
343e0d153c6SMasami Hiramatsu }
344e0d153c6SMasami Hiramatsu 
345b0e9cb28SMasami Hiramatsu /* Get the call file index number in CU DIE */
346b0e9cb28SMasami Hiramatsu static int die_get_call_fileno(Dwarf_Die *in_die)
347b0e9cb28SMasami Hiramatsu {
348b0e9cb28SMasami Hiramatsu 	Dwarf_Sword idx;
349b0e9cb28SMasami Hiramatsu 
350b0e9cb28SMasami Hiramatsu 	if (die_get_attr_sdata(in_die, DW_AT_call_file, &idx) == 0)
351b0e9cb28SMasami Hiramatsu 		return (int)idx;
352b0e9cb28SMasami Hiramatsu 	else
353b0e9cb28SMasami Hiramatsu 		return -ENOENT;
354b0e9cb28SMasami Hiramatsu }
355b0e9cb28SMasami Hiramatsu 
3563f4460a2SMasami Hiramatsu /* Get the declared file index number in CU DIE */
3573f4460a2SMasami Hiramatsu static int die_get_decl_fileno(Dwarf_Die *pdie)
3583f4460a2SMasami Hiramatsu {
3593f4460a2SMasami Hiramatsu 	Dwarf_Sword idx;
3603f4460a2SMasami Hiramatsu 
3613f4460a2SMasami Hiramatsu 	if (die_get_attr_sdata(pdie, DW_AT_decl_file, &idx) == 0)
3623f4460a2SMasami Hiramatsu 		return (int)idx;
3633f4460a2SMasami Hiramatsu 	else
3643f4460a2SMasami Hiramatsu 		return -ENOENT;
3653f4460a2SMasami Hiramatsu }
3663f4460a2SMasami Hiramatsu 
367b0e9cb28SMasami Hiramatsu /**
368b0e9cb28SMasami Hiramatsu  * die_get_call_file - Get callsite file name of inlined function instance
369b0e9cb28SMasami Hiramatsu  * @in_die: a DIE of an inlined function instance
370b0e9cb28SMasami Hiramatsu  *
371b0e9cb28SMasami Hiramatsu  * Get call-site file name of @in_die. This means from which file the inline
372b0e9cb28SMasami Hiramatsu  * function is called.
373b0e9cb28SMasami Hiramatsu  */
374b0e9cb28SMasami Hiramatsu const char *die_get_call_file(Dwarf_Die *in_die)
375b0e9cb28SMasami Hiramatsu {
376b0e9cb28SMasami Hiramatsu 	Dwarf_Die cu_die;
377b0e9cb28SMasami Hiramatsu 	Dwarf_Files *files;
378b0e9cb28SMasami Hiramatsu 	int idx;
379b0e9cb28SMasami Hiramatsu 
380b0e9cb28SMasami Hiramatsu 	idx = die_get_call_fileno(in_die);
381b0e9cb28SMasami Hiramatsu 	if (idx < 0 || !dwarf_diecu(in_die, &cu_die, NULL, NULL) ||
382b0e9cb28SMasami Hiramatsu 	    dwarf_getsrcfiles(&cu_die, &files, NULL) != 0)
383b0e9cb28SMasami Hiramatsu 		return NULL;
384b0e9cb28SMasami Hiramatsu 
385b0e9cb28SMasami Hiramatsu 	return dwarf_filesrc(files, idx, NULL, NULL);
386b0e9cb28SMasami Hiramatsu }
387b0e9cb28SMasami Hiramatsu 
388b0e9cb28SMasami Hiramatsu 
389e0d153c6SMasami Hiramatsu /**
390e0d153c6SMasami Hiramatsu  * die_find_child - Generic DIE search function in DIE tree
391e0d153c6SMasami Hiramatsu  * @rt_die: a root DIE
392e0d153c6SMasami Hiramatsu  * @callback: a callback function
393e0d153c6SMasami Hiramatsu  * @data: a user data passed to the callback function
394e0d153c6SMasami Hiramatsu  * @die_mem: a buffer for result DIE
395e0d153c6SMasami Hiramatsu  *
396e0d153c6SMasami Hiramatsu  * Trace DIE tree from @rt_die and call @callback for each child DIE.
397e0d153c6SMasami Hiramatsu  * If @callback returns DIE_FIND_CB_END, this stores the DIE into
398e0d153c6SMasami Hiramatsu  * @die_mem and returns it. If @callback returns DIE_FIND_CB_CONTINUE,
399e0d153c6SMasami Hiramatsu  * this continues to trace the tree. Optionally, @callback can return
400e0d153c6SMasami Hiramatsu  * DIE_FIND_CB_CHILD and DIE_FIND_CB_SIBLING, those means trace only
401e0d153c6SMasami Hiramatsu  * the children and trace only the siblings respectively.
402e0d153c6SMasami Hiramatsu  * Returns NULL if @callback can't find any appropriate DIE.
403e0d153c6SMasami Hiramatsu  */
404e0d153c6SMasami Hiramatsu Dwarf_Die *die_find_child(Dwarf_Die *rt_die,
405e0d153c6SMasami Hiramatsu 			  int (*callback)(Dwarf_Die *, void *),
406e0d153c6SMasami Hiramatsu 			  void *data, Dwarf_Die *die_mem)
407e0d153c6SMasami Hiramatsu {
408e0d153c6SMasami Hiramatsu 	Dwarf_Die child_die;
409e0d153c6SMasami Hiramatsu 	int ret;
410e0d153c6SMasami Hiramatsu 
411e0d153c6SMasami Hiramatsu 	ret = dwarf_child(rt_die, die_mem);
412e0d153c6SMasami Hiramatsu 	if (ret != 0)
413e0d153c6SMasami Hiramatsu 		return NULL;
414e0d153c6SMasami Hiramatsu 
415e0d153c6SMasami Hiramatsu 	do {
416e0d153c6SMasami Hiramatsu 		ret = callback(die_mem, data);
417e0d153c6SMasami Hiramatsu 		if (ret == DIE_FIND_CB_END)
418e0d153c6SMasami Hiramatsu 			return die_mem;
419e0d153c6SMasami Hiramatsu 
420e0d153c6SMasami Hiramatsu 		if ((ret & DIE_FIND_CB_CHILD) &&
421e0d153c6SMasami Hiramatsu 		    die_find_child(die_mem, callback, data, &child_die)) {
422e0d153c6SMasami Hiramatsu 			memcpy(die_mem, &child_die, sizeof(Dwarf_Die));
423e0d153c6SMasami Hiramatsu 			return die_mem;
424e0d153c6SMasami Hiramatsu 		}
425e0d153c6SMasami Hiramatsu 	} while ((ret & DIE_FIND_CB_SIBLING) &&
426e0d153c6SMasami Hiramatsu 		 dwarf_siblingof(die_mem, die_mem) == 0);
427e0d153c6SMasami Hiramatsu 
428e0d153c6SMasami Hiramatsu 	return NULL;
429e0d153c6SMasami Hiramatsu }
430e0d153c6SMasami Hiramatsu 
431e0d153c6SMasami Hiramatsu struct __addr_die_search_param {
432e0d153c6SMasami Hiramatsu 	Dwarf_Addr	addr;
433e0d153c6SMasami Hiramatsu 	Dwarf_Die	*die_mem;
434e0d153c6SMasami Hiramatsu };
435e0d153c6SMasami Hiramatsu 
436d4c537e6SNaveen N. Rao static int __die_search_func_tail_cb(Dwarf_Die *fn_die, void *data)
437d4c537e6SNaveen N. Rao {
438d4c537e6SNaveen N. Rao 	struct __addr_die_search_param *ad = data;
439d4c537e6SNaveen N. Rao 	Dwarf_Addr addr = 0;
440d4c537e6SNaveen N. Rao 
441d4c537e6SNaveen N. Rao 	if (dwarf_tag(fn_die) == DW_TAG_subprogram &&
442d4c537e6SNaveen N. Rao 	    !dwarf_highpc(fn_die, &addr) &&
443d4c537e6SNaveen N. Rao 	    addr == ad->addr) {
444d4c537e6SNaveen N. Rao 		memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die));
445d4c537e6SNaveen N. Rao 		return DWARF_CB_ABORT;
446d4c537e6SNaveen N. Rao 	}
447d4c537e6SNaveen N. Rao 	return DWARF_CB_OK;
448d4c537e6SNaveen N. Rao }
449d4c537e6SNaveen N. Rao 
450d4c537e6SNaveen N. Rao /**
451d4c537e6SNaveen N. Rao  * die_find_tailfunc - Search for a non-inlined function with tail call at
452d4c537e6SNaveen N. Rao  * given address
453d4c537e6SNaveen N. Rao  * @cu_die: a CU DIE which including @addr
454d4c537e6SNaveen N. Rao  * @addr: target address
455d4c537e6SNaveen N. Rao  * @die_mem: a buffer for result DIE
456d4c537e6SNaveen N. Rao  *
457d4c537e6SNaveen N. Rao  * Search for a non-inlined function DIE with tail call at @addr. Stores the
458d4c537e6SNaveen N. Rao  * DIE to @die_mem and returns it if found. Returns NULL if failed.
459d4c537e6SNaveen N. Rao  */
460d4c537e6SNaveen N. Rao Dwarf_Die *die_find_tailfunc(Dwarf_Die *cu_die, Dwarf_Addr addr,
461d4c537e6SNaveen N. Rao 				    Dwarf_Die *die_mem)
462d4c537e6SNaveen N. Rao {
463d4c537e6SNaveen N. Rao 	struct __addr_die_search_param ad;
464d4c537e6SNaveen N. Rao 	ad.addr = addr;
465d4c537e6SNaveen N. Rao 	ad.die_mem = die_mem;
466d4c537e6SNaveen N. Rao 	/* dwarf_getscopes can't find subprogram. */
467d4c537e6SNaveen N. Rao 	if (!dwarf_getfuncs(cu_die, __die_search_func_tail_cb, &ad, 0))
468d4c537e6SNaveen N. Rao 		return NULL;
469d4c537e6SNaveen N. Rao 	else
470d4c537e6SNaveen N. Rao 		return die_mem;
471d4c537e6SNaveen N. Rao }
472d4c537e6SNaveen N. Rao 
473e0d153c6SMasami Hiramatsu /* die_find callback for non-inlined function search */
474e0d153c6SMasami Hiramatsu static int __die_search_func_cb(Dwarf_Die *fn_die, void *data)
475e0d153c6SMasami Hiramatsu {
476e0d153c6SMasami Hiramatsu 	struct __addr_die_search_param *ad = data;
477e0d153c6SMasami Hiramatsu 
4780dbb1cacSMasami Hiramatsu 	/*
4790dbb1cacSMasami Hiramatsu 	 * Since a declaration entry doesn't has given pc, this always returns
4800dbb1cacSMasami Hiramatsu 	 * function definition entry.
4810dbb1cacSMasami Hiramatsu 	 */
482e0d153c6SMasami Hiramatsu 	if (dwarf_tag(fn_die) == DW_TAG_subprogram &&
483e0d153c6SMasami Hiramatsu 	    dwarf_haspc(fn_die, ad->addr)) {
484e0d153c6SMasami Hiramatsu 		memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die));
485e0d153c6SMasami Hiramatsu 		return DWARF_CB_ABORT;
486e0d153c6SMasami Hiramatsu 	}
487e0d153c6SMasami Hiramatsu 	return DWARF_CB_OK;
488e0d153c6SMasami Hiramatsu }
489e0d153c6SMasami Hiramatsu 
490e0d153c6SMasami Hiramatsu /**
491e0d153c6SMasami Hiramatsu  * die_find_realfunc - Search a non-inlined function at given address
492e0d153c6SMasami Hiramatsu  * @cu_die: a CU DIE which including @addr
493e0d153c6SMasami Hiramatsu  * @addr: target address
494e0d153c6SMasami Hiramatsu  * @die_mem: a buffer for result DIE
495e0d153c6SMasami Hiramatsu  *
496e0d153c6SMasami Hiramatsu  * Search a non-inlined function DIE which includes @addr. Stores the
497e08cfd4bSMasami Hiramatsu  * DIE to @die_mem and returns it if found. Returns NULL if failed.
498e0d153c6SMasami Hiramatsu  */
499e0d153c6SMasami Hiramatsu Dwarf_Die *die_find_realfunc(Dwarf_Die *cu_die, Dwarf_Addr addr,
500e0d153c6SMasami Hiramatsu 				    Dwarf_Die *die_mem)
501e0d153c6SMasami Hiramatsu {
502e0d153c6SMasami Hiramatsu 	struct __addr_die_search_param ad;
503e0d153c6SMasami Hiramatsu 	ad.addr = addr;
504e0d153c6SMasami Hiramatsu 	ad.die_mem = die_mem;
505e0d153c6SMasami Hiramatsu 	/* dwarf_getscopes can't find subprogram. */
506e0d153c6SMasami Hiramatsu 	if (!dwarf_getfuncs(cu_die, __die_search_func_cb, &ad, 0))
507e0d153c6SMasami Hiramatsu 		return NULL;
508e0d153c6SMasami Hiramatsu 	else
509e0d153c6SMasami Hiramatsu 		return die_mem;
510e0d153c6SMasami Hiramatsu }
511e0d153c6SMasami Hiramatsu 
512e0d153c6SMasami Hiramatsu /* die_find callback for inline function search */
513e0d153c6SMasami Hiramatsu static int __die_find_inline_cb(Dwarf_Die *die_mem, void *data)
514e0d153c6SMasami Hiramatsu {
515e0d153c6SMasami Hiramatsu 	Dwarf_Addr *addr = data;
516e0d153c6SMasami Hiramatsu 
517e0d153c6SMasami Hiramatsu 	if (dwarf_tag(die_mem) == DW_TAG_inlined_subroutine &&
518e0d153c6SMasami Hiramatsu 	    dwarf_haspc(die_mem, *addr))
519e0d153c6SMasami Hiramatsu 		return DIE_FIND_CB_END;
520e0d153c6SMasami Hiramatsu 
521e0d153c6SMasami Hiramatsu 	return DIE_FIND_CB_CONTINUE;
522e0d153c6SMasami Hiramatsu }
523e0d153c6SMasami Hiramatsu 
524e0d153c6SMasami Hiramatsu /**
525e08cfd4bSMasami Hiramatsu  * die_find_top_inlinefunc - Search the top inlined function at given address
526e08cfd4bSMasami Hiramatsu  * @sp_die: a subprogram DIE which including @addr
527e0d153c6SMasami Hiramatsu  * @addr: target address
528e0d153c6SMasami Hiramatsu  * @die_mem: a buffer for result DIE
529e0d153c6SMasami Hiramatsu  *
530e0d153c6SMasami Hiramatsu  * Search an inlined function DIE which includes @addr. Stores the
531e08cfd4bSMasami Hiramatsu  * DIE to @die_mem and returns it if found. Returns NULL if failed.
532e08cfd4bSMasami Hiramatsu  * Even if several inlined functions are expanded recursively, this
533e08cfd4bSMasami Hiramatsu  * doesn't trace it down, and returns the topmost one.
534e08cfd4bSMasami Hiramatsu  */
535e08cfd4bSMasami Hiramatsu Dwarf_Die *die_find_top_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
536e08cfd4bSMasami Hiramatsu 				   Dwarf_Die *die_mem)
537e08cfd4bSMasami Hiramatsu {
538e08cfd4bSMasami Hiramatsu 	return die_find_child(sp_die, __die_find_inline_cb, &addr, die_mem);
539e08cfd4bSMasami Hiramatsu }
540e08cfd4bSMasami Hiramatsu 
541e08cfd4bSMasami Hiramatsu /**
542e08cfd4bSMasami Hiramatsu  * die_find_inlinefunc - Search an inlined function at given address
543e08cfd4bSMasami Hiramatsu  * @sp_die: a subprogram DIE which including @addr
544e08cfd4bSMasami Hiramatsu  * @addr: target address
545e08cfd4bSMasami Hiramatsu  * @die_mem: a buffer for result DIE
546e08cfd4bSMasami Hiramatsu  *
547e08cfd4bSMasami Hiramatsu  * Search an inlined function DIE which includes @addr. Stores the
548e08cfd4bSMasami Hiramatsu  * DIE to @die_mem and returns it if found. Returns NULL if failed.
549e0d153c6SMasami Hiramatsu  * If several inlined functions are expanded recursively, this trace
550e08cfd4bSMasami Hiramatsu  * it down and returns deepest one.
551e0d153c6SMasami Hiramatsu  */
552e0d153c6SMasami Hiramatsu Dwarf_Die *die_find_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
553e0d153c6SMasami Hiramatsu 			       Dwarf_Die *die_mem)
554e0d153c6SMasami Hiramatsu {
555e0d153c6SMasami Hiramatsu 	Dwarf_Die tmp_die;
556e0d153c6SMasami Hiramatsu 
557e0d153c6SMasami Hiramatsu 	sp_die = die_find_child(sp_die, __die_find_inline_cb, &addr, &tmp_die);
558e0d153c6SMasami Hiramatsu 	if (!sp_die)
559e0d153c6SMasami Hiramatsu 		return NULL;
560e0d153c6SMasami Hiramatsu 
561e0d153c6SMasami Hiramatsu 	/* Inlined function could be recursive. Trace it until fail */
562e0d153c6SMasami Hiramatsu 	while (sp_die) {
563e0d153c6SMasami Hiramatsu 		memcpy(die_mem, sp_die, sizeof(Dwarf_Die));
564e0d153c6SMasami Hiramatsu 		sp_die = die_find_child(sp_die, __die_find_inline_cb, &addr,
565e0d153c6SMasami Hiramatsu 					&tmp_die);
566e0d153c6SMasami Hiramatsu 	}
567e0d153c6SMasami Hiramatsu 
568e0d153c6SMasami Hiramatsu 	return die_mem;
569e0d153c6SMasami Hiramatsu }
570e0d153c6SMasami Hiramatsu 
571db0d2c64SMasami Hiramatsu struct __instance_walk_param {
572db0d2c64SMasami Hiramatsu 	void    *addr;
573db0d2c64SMasami Hiramatsu 	int	(*callback)(Dwarf_Die *, void *);
574db0d2c64SMasami Hiramatsu 	void    *data;
575db0d2c64SMasami Hiramatsu 	int	retval;
576db0d2c64SMasami Hiramatsu };
577db0d2c64SMasami Hiramatsu 
578db0d2c64SMasami Hiramatsu static int __die_walk_instances_cb(Dwarf_Die *inst, void *data)
579db0d2c64SMasami Hiramatsu {
580db0d2c64SMasami Hiramatsu 	struct __instance_walk_param *iwp = data;
581db0d2c64SMasami Hiramatsu 	Dwarf_Attribute attr_mem;
582db0d2c64SMasami Hiramatsu 	Dwarf_Die origin_mem;
583db0d2c64SMasami Hiramatsu 	Dwarf_Attribute *attr;
584db0d2c64SMasami Hiramatsu 	Dwarf_Die *origin;
5853f4460a2SMasami Hiramatsu 	int tmp;
586db0d2c64SMasami Hiramatsu 
587db0d2c64SMasami Hiramatsu 	attr = dwarf_attr(inst, DW_AT_abstract_origin, &attr_mem);
588db0d2c64SMasami Hiramatsu 	if (attr == NULL)
589db0d2c64SMasami Hiramatsu 		return DIE_FIND_CB_CONTINUE;
590db0d2c64SMasami Hiramatsu 
591db0d2c64SMasami Hiramatsu 	origin = dwarf_formref_die(attr, &origin_mem);
592db0d2c64SMasami Hiramatsu 	if (origin == NULL || origin->addr != iwp->addr)
593db0d2c64SMasami Hiramatsu 		return DIE_FIND_CB_CONTINUE;
594db0d2c64SMasami Hiramatsu 
5953f4460a2SMasami Hiramatsu 	/* Ignore redundant instances */
5963f4460a2SMasami Hiramatsu 	if (dwarf_tag(inst) == DW_TAG_inlined_subroutine) {
5973f4460a2SMasami Hiramatsu 		dwarf_decl_line(origin, &tmp);
5983f4460a2SMasami Hiramatsu 		if (die_get_call_lineno(inst) == tmp) {
5993f4460a2SMasami Hiramatsu 			tmp = die_get_decl_fileno(origin);
6003f4460a2SMasami Hiramatsu 			if (die_get_call_fileno(inst) == tmp)
6013f4460a2SMasami Hiramatsu 				return DIE_FIND_CB_CONTINUE;
6023f4460a2SMasami Hiramatsu 		}
6033f4460a2SMasami Hiramatsu 	}
6043f4460a2SMasami Hiramatsu 
605db0d2c64SMasami Hiramatsu 	iwp->retval = iwp->callback(inst, iwp->data);
606db0d2c64SMasami Hiramatsu 
607db0d2c64SMasami Hiramatsu 	return (iwp->retval) ? DIE_FIND_CB_END : DIE_FIND_CB_CONTINUE;
608db0d2c64SMasami Hiramatsu }
609db0d2c64SMasami Hiramatsu 
610db0d2c64SMasami Hiramatsu /**
611db0d2c64SMasami Hiramatsu  * die_walk_instances - Walk on instances of given DIE
612db0d2c64SMasami Hiramatsu  * @or_die: an abstract original DIE
613db0d2c64SMasami Hiramatsu  * @callback: a callback function which is called with instance DIE
614db0d2c64SMasami Hiramatsu  * @data: user data
615db0d2c64SMasami Hiramatsu  *
616db0d2c64SMasami Hiramatsu  * Walk on the instances of give @in_die. @in_die must be an inlined function
617db0d2c64SMasami Hiramatsu  * declartion. This returns the return value of @callback if it returns
618db0d2c64SMasami Hiramatsu  * non-zero value, or -ENOENT if there is no instance.
619db0d2c64SMasami Hiramatsu  */
620db0d2c64SMasami Hiramatsu int die_walk_instances(Dwarf_Die *or_die, int (*callback)(Dwarf_Die *, void *),
621db0d2c64SMasami Hiramatsu 		       void *data)
622db0d2c64SMasami Hiramatsu {
623db0d2c64SMasami Hiramatsu 	Dwarf_Die cu_die;
624db0d2c64SMasami Hiramatsu 	Dwarf_Die die_mem;
625db0d2c64SMasami Hiramatsu 	struct __instance_walk_param iwp = {
626db0d2c64SMasami Hiramatsu 		.addr = or_die->addr,
627db0d2c64SMasami Hiramatsu 		.callback = callback,
628db0d2c64SMasami Hiramatsu 		.data = data,
629db0d2c64SMasami Hiramatsu 		.retval = -ENOENT,
630db0d2c64SMasami Hiramatsu 	};
631db0d2c64SMasami Hiramatsu 
632db0d2c64SMasami Hiramatsu 	if (dwarf_diecu(or_die, &cu_die, NULL, NULL) == NULL)
633db0d2c64SMasami Hiramatsu 		return -ENOENT;
634db0d2c64SMasami Hiramatsu 
635db0d2c64SMasami Hiramatsu 	die_find_child(&cu_die, __die_walk_instances_cb, &iwp, &die_mem);
636db0d2c64SMasami Hiramatsu 
637db0d2c64SMasami Hiramatsu 	return iwp.retval;
638db0d2c64SMasami Hiramatsu }
639db0d2c64SMasami Hiramatsu 
640e0d153c6SMasami Hiramatsu /* Line walker internal parameters */
641e0d153c6SMasami Hiramatsu struct __line_walk_param {
642b0e9cb28SMasami Hiramatsu 	bool recursive;
643e0d153c6SMasami Hiramatsu 	line_walk_callback_t callback;
644e0d153c6SMasami Hiramatsu 	void *data;
645e0d153c6SMasami Hiramatsu 	int retval;
646e0d153c6SMasami Hiramatsu };
647e0d153c6SMasami Hiramatsu 
648e0d153c6SMasami Hiramatsu static int __die_walk_funclines_cb(Dwarf_Die *in_die, void *data)
649e0d153c6SMasami Hiramatsu {
650e0d153c6SMasami Hiramatsu 	struct __line_walk_param *lw = data;
651b0e9cb28SMasami Hiramatsu 	Dwarf_Addr addr = 0;
652b0e9cb28SMasami Hiramatsu 	const char *fname;
653e0d153c6SMasami Hiramatsu 	int lineno;
654e0d153c6SMasami Hiramatsu 
655e0d153c6SMasami Hiramatsu 	if (dwarf_tag(in_die) == DW_TAG_inlined_subroutine) {
656b0e9cb28SMasami Hiramatsu 		fname = die_get_call_file(in_die);
657e0d153c6SMasami Hiramatsu 		lineno = die_get_call_lineno(in_die);
658b0e9cb28SMasami Hiramatsu 		if (fname && lineno > 0 && dwarf_entrypc(in_die, &addr) == 0) {
659b0e9cb28SMasami Hiramatsu 			lw->retval = lw->callback(fname, lineno, addr, lw->data);
660e0d153c6SMasami Hiramatsu 			if (lw->retval != 0)
661e0d153c6SMasami Hiramatsu 				return DIE_FIND_CB_END;
662e0d153c6SMasami Hiramatsu 		}
663e0d153c6SMasami Hiramatsu 	}
664b0e9cb28SMasami Hiramatsu 	if (!lw->recursive)
665b0e9cb28SMasami Hiramatsu 		/* Don't need to search recursively */
666e0d153c6SMasami Hiramatsu 		return DIE_FIND_CB_SIBLING;
667b0e9cb28SMasami Hiramatsu 
668b0e9cb28SMasami Hiramatsu 	if (addr) {
669b0e9cb28SMasami Hiramatsu 		fname = dwarf_decl_file(in_die);
670b0e9cb28SMasami Hiramatsu 		if (fname && dwarf_decl_line(in_die, &lineno) == 0) {
671b0e9cb28SMasami Hiramatsu 			lw->retval = lw->callback(fname, lineno, addr, lw->data);
672b0e9cb28SMasami Hiramatsu 			if (lw->retval != 0)
673b0e9cb28SMasami Hiramatsu 				return DIE_FIND_CB_END;
674b0e9cb28SMasami Hiramatsu 		}
675b0e9cb28SMasami Hiramatsu 	}
676b0e9cb28SMasami Hiramatsu 
677b0e9cb28SMasami Hiramatsu 	/* Continue to search nested inlined function call-sites */
678b0e9cb28SMasami Hiramatsu 	return DIE_FIND_CB_CONTINUE;
679e0d153c6SMasami Hiramatsu }
680e0d153c6SMasami Hiramatsu 
681e0d153c6SMasami Hiramatsu /* Walk on lines of blocks included in given DIE */
682b0e9cb28SMasami Hiramatsu static int __die_walk_funclines(Dwarf_Die *sp_die, bool recursive,
683e0d153c6SMasami Hiramatsu 				line_walk_callback_t callback, void *data)
684e0d153c6SMasami Hiramatsu {
685e0d153c6SMasami Hiramatsu 	struct __line_walk_param lw = {
686b0e9cb28SMasami Hiramatsu 		.recursive = recursive,
687e0d153c6SMasami Hiramatsu 		.callback = callback,
688e0d153c6SMasami Hiramatsu 		.data = data,
689e0d153c6SMasami Hiramatsu 		.retval = 0,
690e0d153c6SMasami Hiramatsu 	};
691e0d153c6SMasami Hiramatsu 	Dwarf_Die die_mem;
692e0d153c6SMasami Hiramatsu 	Dwarf_Addr addr;
693b0e9cb28SMasami Hiramatsu 	const char *fname;
694e0d153c6SMasami Hiramatsu 	int lineno;
695e0d153c6SMasami Hiramatsu 
696e0d153c6SMasami Hiramatsu 	/* Handle function declaration line */
697b0e9cb28SMasami Hiramatsu 	fname = dwarf_decl_file(sp_die);
698b0e9cb28SMasami Hiramatsu 	if (fname && dwarf_decl_line(sp_die, &lineno) == 0 &&
699e0d153c6SMasami Hiramatsu 	    dwarf_entrypc(sp_die, &addr) == 0) {
700b0e9cb28SMasami Hiramatsu 		lw.retval = callback(fname, lineno, addr, data);
701e0d153c6SMasami Hiramatsu 		if (lw.retval != 0)
702e0d153c6SMasami Hiramatsu 			goto done;
703e0d153c6SMasami Hiramatsu 	}
704e0d153c6SMasami Hiramatsu 	die_find_child(sp_die, __die_walk_funclines_cb, &lw, &die_mem);
705e0d153c6SMasami Hiramatsu done:
706e0d153c6SMasami Hiramatsu 	return lw.retval;
707e0d153c6SMasami Hiramatsu }
708e0d153c6SMasami Hiramatsu 
709e0d153c6SMasami Hiramatsu static int __die_walk_culines_cb(Dwarf_Die *sp_die, void *data)
710e0d153c6SMasami Hiramatsu {
711e0d153c6SMasami Hiramatsu 	struct __line_walk_param *lw = data;
712e0d153c6SMasami Hiramatsu 
713b0e9cb28SMasami Hiramatsu 	lw->retval = __die_walk_funclines(sp_die, true, lw->callback, lw->data);
714e0d153c6SMasami Hiramatsu 	if (lw->retval != 0)
715e0d153c6SMasami Hiramatsu 		return DWARF_CB_ABORT;
716e0d153c6SMasami Hiramatsu 
717e0d153c6SMasami Hiramatsu 	return DWARF_CB_OK;
718e0d153c6SMasami Hiramatsu }
719e0d153c6SMasami Hiramatsu 
720e0d153c6SMasami Hiramatsu /**
721e0d153c6SMasami Hiramatsu  * die_walk_lines - Walk on lines inside given DIE
722a128405cSMasami Hiramatsu  * @rt_die: a root DIE (CU, subprogram or inlined_subroutine)
723e0d153c6SMasami Hiramatsu  * @callback: callback routine
724e0d153c6SMasami Hiramatsu  * @data: user data
725e0d153c6SMasami Hiramatsu  *
726e0d153c6SMasami Hiramatsu  * Walk on all lines inside given @rt_die and call @callback on each line.
727e0d153c6SMasami Hiramatsu  * If the @rt_die is a function, walk only on the lines inside the function,
728e0d153c6SMasami Hiramatsu  * otherwise @rt_die must be a CU DIE.
729e0d153c6SMasami Hiramatsu  * Note that this walks not only dwarf line list, but also function entries
730e0d153c6SMasami Hiramatsu  * and inline call-site.
731e0d153c6SMasami Hiramatsu  */
732e0d153c6SMasami Hiramatsu int die_walk_lines(Dwarf_Die *rt_die, line_walk_callback_t callback, void *data)
733e0d153c6SMasami Hiramatsu {
734e0d153c6SMasami Hiramatsu 	Dwarf_Lines *lines;
735e0d153c6SMasami Hiramatsu 	Dwarf_Line *line;
736e0d153c6SMasami Hiramatsu 	Dwarf_Addr addr;
73775186a9bSMasami Hiramatsu 	const char *fname, *decf = NULL;
738e0d153c6SMasami Hiramatsu 	int lineno, ret = 0;
73975186a9bSMasami Hiramatsu 	int decl = 0, inl;
740e0d153c6SMasami Hiramatsu 	Dwarf_Die die_mem, *cu_die;
741e0d153c6SMasami Hiramatsu 	size_t nlines, i;
742e0d153c6SMasami Hiramatsu 
743e0d153c6SMasami Hiramatsu 	/* Get the CU die */
74475186a9bSMasami Hiramatsu 	if (dwarf_tag(rt_die) != DW_TAG_compile_unit) {
745e0d153c6SMasami Hiramatsu 		cu_die = dwarf_diecu(rt_die, &die_mem, NULL, NULL);
74675186a9bSMasami Hiramatsu 		dwarf_decl_line(rt_die, &decl);
74775186a9bSMasami Hiramatsu 		decf = dwarf_decl_file(rt_die);
74875186a9bSMasami Hiramatsu 	} else
749e0d153c6SMasami Hiramatsu 		cu_die = rt_die;
750e0d153c6SMasami Hiramatsu 	if (!cu_die) {
751a128405cSMasami Hiramatsu 		pr_debug2("Failed to get CU from given DIE.\n");
752e0d153c6SMasami Hiramatsu 		return -EINVAL;
753e0d153c6SMasami Hiramatsu 	}
754e0d153c6SMasami Hiramatsu 
755e0d153c6SMasami Hiramatsu 	/* Get lines list in the CU */
756e0d153c6SMasami Hiramatsu 	if (dwarf_getsrclines(cu_die, &lines, &nlines) != 0) {
757e0d153c6SMasami Hiramatsu 		pr_debug2("Failed to get source lines on this CU.\n");
758e0d153c6SMasami Hiramatsu 		return -ENOENT;
759e0d153c6SMasami Hiramatsu 	}
760e0d153c6SMasami Hiramatsu 	pr_debug2("Get %zd lines from this CU\n", nlines);
761e0d153c6SMasami Hiramatsu 
762e0d153c6SMasami Hiramatsu 	/* Walk on the lines on lines list */
763e0d153c6SMasami Hiramatsu 	for (i = 0; i < nlines; i++) {
764e0d153c6SMasami Hiramatsu 		line = dwarf_onesrcline(lines, i);
765e0d153c6SMasami Hiramatsu 		if (line == NULL ||
766e0d153c6SMasami Hiramatsu 		    dwarf_lineno(line, &lineno) != 0 ||
767e0d153c6SMasami Hiramatsu 		    dwarf_lineaddr(line, &addr) != 0) {
768e0d153c6SMasami Hiramatsu 			pr_debug2("Failed to get line info. "
769e0d153c6SMasami Hiramatsu 				  "Possible error in debuginfo.\n");
770e0d153c6SMasami Hiramatsu 			continue;
771e0d153c6SMasami Hiramatsu 		}
772e0d153c6SMasami Hiramatsu 		/* Filter lines based on address */
77386a76027SMasami Hiramatsu 		if (rt_die != cu_die) {
774e0d153c6SMasami Hiramatsu 			/*
775e0d153c6SMasami Hiramatsu 			 * Address filtering
776e0d153c6SMasami Hiramatsu 			 * The line is included in given function, and
777e0d153c6SMasami Hiramatsu 			 * no inline block includes it.
778e0d153c6SMasami Hiramatsu 			 */
77975186a9bSMasami Hiramatsu 			if (!dwarf_haspc(rt_die, addr))
780e0d153c6SMasami Hiramatsu 				continue;
78175186a9bSMasami Hiramatsu 			if (die_find_inlinefunc(rt_die, addr, &die_mem)) {
78275186a9bSMasami Hiramatsu 				dwarf_decl_line(&die_mem, &inl);
78375186a9bSMasami Hiramatsu 				if (inl != decl ||
78475186a9bSMasami Hiramatsu 				    decf != dwarf_decl_file(&die_mem))
78575186a9bSMasami Hiramatsu 					continue;
78675186a9bSMasami Hiramatsu 			}
78786a76027SMasami Hiramatsu 		}
788e0d153c6SMasami Hiramatsu 		/* Get source line */
789e0d153c6SMasami Hiramatsu 		fname = dwarf_linesrc(line, NULL, NULL);
790e0d153c6SMasami Hiramatsu 
791e0d153c6SMasami Hiramatsu 		ret = callback(fname, lineno, addr, data);
792e0d153c6SMasami Hiramatsu 		if (ret != 0)
793e0d153c6SMasami Hiramatsu 			return ret;
794e0d153c6SMasami Hiramatsu 	}
795e0d153c6SMasami Hiramatsu 
796e0d153c6SMasami Hiramatsu 	/*
797e0d153c6SMasami Hiramatsu 	 * Dwarf lines doesn't include function declarations and inlined
798e0d153c6SMasami Hiramatsu 	 * subroutines. We have to check functions list or given function.
799e0d153c6SMasami Hiramatsu 	 */
800e0d153c6SMasami Hiramatsu 	if (rt_die != cu_die)
801b0e9cb28SMasami Hiramatsu 		/*
802b0e9cb28SMasami Hiramatsu 		 * Don't need walk functions recursively, because nested
803b0e9cb28SMasami Hiramatsu 		 * inlined functions don't have lines of the specified DIE.
804b0e9cb28SMasami Hiramatsu 		 */
805b0e9cb28SMasami Hiramatsu 		ret = __die_walk_funclines(rt_die, false, callback, data);
806e0d153c6SMasami Hiramatsu 	else {
807e0d153c6SMasami Hiramatsu 		struct __line_walk_param param = {
808e0d153c6SMasami Hiramatsu 			.callback = callback,
809e0d153c6SMasami Hiramatsu 			.data = data,
810e0d153c6SMasami Hiramatsu 			.retval = 0,
811e0d153c6SMasami Hiramatsu 		};
812e0d153c6SMasami Hiramatsu 		dwarf_getfuncs(cu_die, __die_walk_culines_cb, &param, 0);
813e0d153c6SMasami Hiramatsu 		ret = param.retval;
814e0d153c6SMasami Hiramatsu 	}
815e0d153c6SMasami Hiramatsu 
816e0d153c6SMasami Hiramatsu 	return ret;
817e0d153c6SMasami Hiramatsu }
818e0d153c6SMasami Hiramatsu 
819e0d153c6SMasami Hiramatsu struct __find_variable_param {
820e0d153c6SMasami Hiramatsu 	const char *name;
821e0d153c6SMasami Hiramatsu 	Dwarf_Addr addr;
822e0d153c6SMasami Hiramatsu };
823e0d153c6SMasami Hiramatsu 
824e0d153c6SMasami Hiramatsu static int __die_find_variable_cb(Dwarf_Die *die_mem, void *data)
825e0d153c6SMasami Hiramatsu {
826e0d153c6SMasami Hiramatsu 	struct __find_variable_param *fvp = data;
827082f96a9SMasami Hiramatsu 	Dwarf_Attribute attr;
828e0d153c6SMasami Hiramatsu 	int tag;
829e0d153c6SMasami Hiramatsu 
830e0d153c6SMasami Hiramatsu 	tag = dwarf_tag(die_mem);
831e0d153c6SMasami Hiramatsu 	if ((tag == DW_TAG_formal_parameter ||
832e0d153c6SMasami Hiramatsu 	     tag == DW_TAG_variable) &&
833082f96a9SMasami Hiramatsu 	    die_compare_name(die_mem, fvp->name) &&
834082f96a9SMasami Hiramatsu 	/* Does the DIE have location information or external instance? */
835082f96a9SMasami Hiramatsu 	    (dwarf_attr(die_mem, DW_AT_external, &attr) ||
836082f96a9SMasami Hiramatsu 	     dwarf_attr(die_mem, DW_AT_location, &attr)))
837e0d153c6SMasami Hiramatsu 		return DIE_FIND_CB_END;
838e0d153c6SMasami Hiramatsu 	if (dwarf_haspc(die_mem, fvp->addr))
839e0d153c6SMasami Hiramatsu 		return DIE_FIND_CB_CONTINUE;
840e0d153c6SMasami Hiramatsu 	else
841e0d153c6SMasami Hiramatsu 		return DIE_FIND_CB_SIBLING;
842e0d153c6SMasami Hiramatsu }
843e0d153c6SMasami Hiramatsu 
844e0d153c6SMasami Hiramatsu /**
845e0d153c6SMasami Hiramatsu  * die_find_variable_at - Find a given name variable at given address
846e0d153c6SMasami Hiramatsu  * @sp_die: a function DIE
847e0d153c6SMasami Hiramatsu  * @name: variable name
848e0d153c6SMasami Hiramatsu  * @addr: address
849e0d153c6SMasami Hiramatsu  * @die_mem: a buffer for result DIE
850e0d153c6SMasami Hiramatsu  *
851e0d153c6SMasami Hiramatsu  * Find a variable DIE called @name at @addr in @sp_die.
852e0d153c6SMasami Hiramatsu  */
853e0d153c6SMasami Hiramatsu Dwarf_Die *die_find_variable_at(Dwarf_Die *sp_die, const char *name,
854e0d153c6SMasami Hiramatsu 				Dwarf_Addr addr, Dwarf_Die *die_mem)
855e0d153c6SMasami Hiramatsu {
856e0d153c6SMasami Hiramatsu 	struct __find_variable_param fvp = { .name = name, .addr = addr};
857e0d153c6SMasami Hiramatsu 
858e0d153c6SMasami Hiramatsu 	return die_find_child(sp_die, __die_find_variable_cb, (void *)&fvp,
859e0d153c6SMasami Hiramatsu 			      die_mem);
860e0d153c6SMasami Hiramatsu }
861e0d153c6SMasami Hiramatsu 
862e0d153c6SMasami Hiramatsu static int __die_find_member_cb(Dwarf_Die *die_mem, void *data)
863e0d153c6SMasami Hiramatsu {
864e0d153c6SMasami Hiramatsu 	const char *name = data;
865e0d153c6SMasami Hiramatsu 
866c7273835SMasami Hiramatsu 	if (dwarf_tag(die_mem) == DW_TAG_member) {
867c7273835SMasami Hiramatsu 		if (die_compare_name(die_mem, name))
868e0d153c6SMasami Hiramatsu 			return DIE_FIND_CB_END;
869c7273835SMasami Hiramatsu 		else if (!dwarf_diename(die_mem)) {	/* Unnamed structure */
870c7273835SMasami Hiramatsu 			Dwarf_Die type_die, tmp_die;
871c7273835SMasami Hiramatsu 			if (die_get_type(die_mem, &type_die) &&
872c7273835SMasami Hiramatsu 			    die_find_member(&type_die, name, &tmp_die))
873c7273835SMasami Hiramatsu 				return DIE_FIND_CB_END;
874c7273835SMasami Hiramatsu 		}
875c7273835SMasami Hiramatsu 	}
876e0d153c6SMasami Hiramatsu 	return DIE_FIND_CB_SIBLING;
877e0d153c6SMasami Hiramatsu }
878e0d153c6SMasami Hiramatsu 
879e0d153c6SMasami Hiramatsu /**
880e0d153c6SMasami Hiramatsu  * die_find_member - Find a given name member in a data structure
881e0d153c6SMasami Hiramatsu  * @st_die: a data structure type DIE
882e0d153c6SMasami Hiramatsu  * @name: member name
883e0d153c6SMasami Hiramatsu  * @die_mem: a buffer for result DIE
884e0d153c6SMasami Hiramatsu  *
885e0d153c6SMasami Hiramatsu  * Find a member DIE called @name in @st_die.
886e0d153c6SMasami Hiramatsu  */
887e0d153c6SMasami Hiramatsu Dwarf_Die *die_find_member(Dwarf_Die *st_die, const char *name,
888e0d153c6SMasami Hiramatsu 			   Dwarf_Die *die_mem)
889e0d153c6SMasami Hiramatsu {
890e0d153c6SMasami Hiramatsu 	return die_find_child(st_die, __die_find_member_cb, (void *)name,
891e0d153c6SMasami Hiramatsu 			      die_mem);
892e0d153c6SMasami Hiramatsu }
893e0d153c6SMasami Hiramatsu 
894e0d153c6SMasami Hiramatsu /**
895e0d153c6SMasami Hiramatsu  * die_get_typename - Get the name of given variable DIE
896e0d153c6SMasami Hiramatsu  * @vr_die: a variable DIE
897fb9596d1SHe Kuang  * @buf: a strbuf for result type name
898e0d153c6SMasami Hiramatsu  *
899fb9596d1SHe Kuang  * Get the name of @vr_die and stores it to @buf. Return 0 if succeeded.
900fb9596d1SHe Kuang  * and Return -ENOENT if failed to find type name.
901e0d153c6SMasami Hiramatsu  * Note that the result will stores typedef name if possible, and stores
902e0d153c6SMasami Hiramatsu  * "*(function_type)" if the type is a function pointer.
903e0d153c6SMasami Hiramatsu  */
904fb9596d1SHe Kuang int die_get_typename(Dwarf_Die *vr_die, struct strbuf *buf)
905e0d153c6SMasami Hiramatsu {
906e0d153c6SMasami Hiramatsu 	Dwarf_Die type;
907fb9596d1SHe Kuang 	int tag, ret;
908e0d153c6SMasami Hiramatsu 	const char *tmp = "";
909e0d153c6SMasami Hiramatsu 
910e0d153c6SMasami Hiramatsu 	if (__die_get_real_type(vr_die, &type) == NULL)
911e0d153c6SMasami Hiramatsu 		return -ENOENT;
912e0d153c6SMasami Hiramatsu 
913e0d153c6SMasami Hiramatsu 	tag = dwarf_tag(&type);
914e0d153c6SMasami Hiramatsu 	if (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type)
915e0d153c6SMasami Hiramatsu 		tmp = "*";
916e0d153c6SMasami Hiramatsu 	else if (tag == DW_TAG_subroutine_type) {
917e0d153c6SMasami Hiramatsu 		/* Function pointer */
918*bf4d5f25SMasami Hiramatsu 		return strbuf_add(buf, "(function_type)", 15);
919e0d153c6SMasami Hiramatsu 	} else {
920e0d153c6SMasami Hiramatsu 		if (!dwarf_diename(&type))
921e0d153c6SMasami Hiramatsu 			return -ENOENT;
922e0d153c6SMasami Hiramatsu 		if (tag == DW_TAG_union_type)
923e0d153c6SMasami Hiramatsu 			tmp = "union ";
924e0d153c6SMasami Hiramatsu 		else if (tag == DW_TAG_structure_type)
925e0d153c6SMasami Hiramatsu 			tmp = "struct ";
926bb2d17a0SHyeoncheol Lee 		else if (tag == DW_TAG_enumeration_type)
927bb2d17a0SHyeoncheol Lee 			tmp = "enum ";
928e0d153c6SMasami Hiramatsu 		/* Write a base name */
929*bf4d5f25SMasami Hiramatsu 		return strbuf_addf(buf, "%s%s", tmp, dwarf_diename(&type));
930e0d153c6SMasami Hiramatsu 	}
931fb9596d1SHe Kuang 	ret = die_get_typename(&type, buf);
932*bf4d5f25SMasami Hiramatsu 	return ret ? ret : strbuf_addstr(buf, tmp);
933e0d153c6SMasami Hiramatsu }
934e0d153c6SMasami Hiramatsu 
935e0d153c6SMasami Hiramatsu /**
936e0d153c6SMasami Hiramatsu  * die_get_varname - Get the name and type of given variable DIE
937e0d153c6SMasami Hiramatsu  * @vr_die: a variable DIE
938fb9596d1SHe Kuang  * @buf: a strbuf for type and variable name
939e0d153c6SMasami Hiramatsu  *
940e0d153c6SMasami Hiramatsu  * Get the name and type of @vr_die and stores it in @buf as "type\tname".
941e0d153c6SMasami Hiramatsu  */
942fb9596d1SHe Kuang int die_get_varname(Dwarf_Die *vr_die, struct strbuf *buf)
943e0d153c6SMasami Hiramatsu {
944fb9596d1SHe Kuang 	int ret;
945e0d153c6SMasami Hiramatsu 
946fb9596d1SHe Kuang 	ret = die_get_typename(vr_die, buf);
947e0d153c6SMasami Hiramatsu 	if (ret < 0) {
948e0d153c6SMasami Hiramatsu 		pr_debug("Failed to get type, make it unknown.\n");
949*bf4d5f25SMasami Hiramatsu 		ret = strbuf_add(buf, " (unknown_type)", 14);
950e0d153c6SMasami Hiramatsu 	}
951fb9596d1SHe Kuang 
952*bf4d5f25SMasami Hiramatsu 	return ret < 0 ? ret : strbuf_addf(buf, "\t%s", dwarf_diename(vr_die));
953e0d153c6SMasami Hiramatsu }
954e0d153c6SMasami Hiramatsu 
955bd0419e2SArnaldo Carvalho de Melo #ifdef HAVE_DWARF_GETLOCATIONS
956349e8d26SHe Kuang /**
957349e8d26SHe Kuang  * die_get_var_innermost_scope - Get innermost scope range of given variable DIE
958349e8d26SHe Kuang  * @sp_die: a subprogram DIE
959349e8d26SHe Kuang  * @vr_die: a variable DIE
960349e8d26SHe Kuang  * @buf: a strbuf for variable byte offset range
961349e8d26SHe Kuang  *
962349e8d26SHe Kuang  * Get the innermost scope range of @vr_die and stores it in @buf as
963349e8d26SHe Kuang  * "@<function_name+[NN-NN,NN-NN]>".
964349e8d26SHe Kuang  */
965349e8d26SHe Kuang static int die_get_var_innermost_scope(Dwarf_Die *sp_die, Dwarf_Die *vr_die,
966349e8d26SHe Kuang 				struct strbuf *buf)
967349e8d26SHe Kuang {
968349e8d26SHe Kuang 	Dwarf_Die *scopes;
969349e8d26SHe Kuang 	int count;
970349e8d26SHe Kuang 	size_t offset = 0;
971349e8d26SHe Kuang 	Dwarf_Addr base;
972349e8d26SHe Kuang 	Dwarf_Addr start, end;
973349e8d26SHe Kuang 	Dwarf_Addr entry;
974349e8d26SHe Kuang 	int ret;
975349e8d26SHe Kuang 	bool first = true;
976349e8d26SHe Kuang 	const char *name;
977349e8d26SHe Kuang 
978349e8d26SHe Kuang 	ret = dwarf_entrypc(sp_die, &entry);
979349e8d26SHe Kuang 	if (ret)
980349e8d26SHe Kuang 		return ret;
981349e8d26SHe Kuang 
982349e8d26SHe Kuang 	name = dwarf_diename(sp_die);
983349e8d26SHe Kuang 	if (!name)
984349e8d26SHe Kuang 		return -ENOENT;
985349e8d26SHe Kuang 
986349e8d26SHe Kuang 	count = dwarf_getscopes_die(vr_die, &scopes);
987349e8d26SHe Kuang 
988349e8d26SHe Kuang 	/* (*SCOPES)[1] is the DIE for the scope containing that scope */
989349e8d26SHe Kuang 	if (count <= 1) {
990349e8d26SHe Kuang 		ret = -EINVAL;
991349e8d26SHe Kuang 		goto out;
992349e8d26SHe Kuang 	}
993349e8d26SHe Kuang 
994349e8d26SHe Kuang 	while ((offset = dwarf_ranges(&scopes[1], offset, &base,
995349e8d26SHe Kuang 					&start, &end)) > 0) {
996349e8d26SHe Kuang 		start -= entry;
997349e8d26SHe Kuang 		end -= entry;
998349e8d26SHe Kuang 
999349e8d26SHe Kuang 		if (first) {
1000*bf4d5f25SMasami Hiramatsu 			ret = strbuf_addf(buf, "@<%s+[%" PRIu64 "-%" PRIu64,
1001349e8d26SHe Kuang 					  name, start, end);
1002349e8d26SHe Kuang 			first = false;
1003349e8d26SHe Kuang 		} else {
1004*bf4d5f25SMasami Hiramatsu 			ret = strbuf_addf(buf, ",%" PRIu64 "-%" PRIu64,
1005349e8d26SHe Kuang 					  start, end);
1006349e8d26SHe Kuang 		}
1007*bf4d5f25SMasami Hiramatsu 		if (ret < 0)
1008*bf4d5f25SMasami Hiramatsu 			goto out;
1009349e8d26SHe Kuang 	}
1010349e8d26SHe Kuang 
1011349e8d26SHe Kuang 	if (!first)
1012*bf4d5f25SMasami Hiramatsu 		ret = strbuf_add(buf, "]>", 2);
1013349e8d26SHe Kuang 
1014349e8d26SHe Kuang out:
1015349e8d26SHe Kuang 	free(scopes);
1016349e8d26SHe Kuang 	return ret;
1017349e8d26SHe Kuang }
1018349e8d26SHe Kuang 
1019349e8d26SHe Kuang /**
1020349e8d26SHe Kuang  * die_get_var_range - Get byte offset range of given variable DIE
1021349e8d26SHe Kuang  * @sp_die: a subprogram DIE
1022349e8d26SHe Kuang  * @vr_die: a variable DIE
1023349e8d26SHe Kuang  * @buf: a strbuf for type and variable name and byte offset range
1024349e8d26SHe Kuang  *
1025349e8d26SHe Kuang  * Get the byte offset range of @vr_die and stores it in @buf as
1026349e8d26SHe Kuang  * "@<function_name+[NN-NN,NN-NN]>".
1027349e8d26SHe Kuang  */
1028349e8d26SHe Kuang int die_get_var_range(Dwarf_Die *sp_die, Dwarf_Die *vr_die, struct strbuf *buf)
1029349e8d26SHe Kuang {
1030349e8d26SHe Kuang 	int ret = 0;
1031349e8d26SHe Kuang 	Dwarf_Addr base;
1032349e8d26SHe Kuang 	Dwarf_Addr start, end;
1033349e8d26SHe Kuang 	Dwarf_Addr entry;
1034349e8d26SHe Kuang 	Dwarf_Op *op;
1035349e8d26SHe Kuang 	size_t nops;
1036349e8d26SHe Kuang 	size_t offset = 0;
1037349e8d26SHe Kuang 	Dwarf_Attribute attr;
1038349e8d26SHe Kuang 	bool first = true;
1039349e8d26SHe Kuang 	const char *name;
1040349e8d26SHe Kuang 
1041349e8d26SHe Kuang 	ret = dwarf_entrypc(sp_die, &entry);
1042349e8d26SHe Kuang 	if (ret)
1043349e8d26SHe Kuang 		return ret;
1044349e8d26SHe Kuang 
1045349e8d26SHe Kuang 	name = dwarf_diename(sp_die);
1046349e8d26SHe Kuang 	if (!name)
1047349e8d26SHe Kuang 		return -ENOENT;
1048349e8d26SHe Kuang 
1049349e8d26SHe Kuang 	if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL)
1050349e8d26SHe Kuang 		return -EINVAL;
1051349e8d26SHe Kuang 
1052*bf4d5f25SMasami Hiramatsu 	while ((offset = dwarf_getlocations(&attr, offset, &base,
1053349e8d26SHe Kuang 					&start, &end, &op, &nops)) > 0) {
1054349e8d26SHe Kuang 		if (start == 0) {
1055349e8d26SHe Kuang 			/* Single Location Descriptions */
1056349e8d26SHe Kuang 			ret = die_get_var_innermost_scope(sp_die, vr_die, buf);
1057*bf4d5f25SMasami Hiramatsu 			goto out;
1058349e8d26SHe Kuang 		}
1059349e8d26SHe Kuang 
1060349e8d26SHe Kuang 		/* Location Lists */
1061349e8d26SHe Kuang 		start -= entry;
1062349e8d26SHe Kuang 		end -= entry;
1063349e8d26SHe Kuang 		if (first) {
1064*bf4d5f25SMasami Hiramatsu 			ret = strbuf_addf(buf, "@<%s+[%" PRIu64 "-%" PRIu64,
1065349e8d26SHe Kuang 					  name, start, end);
1066349e8d26SHe Kuang 			first = false;
1067349e8d26SHe Kuang 		} else {
1068*bf4d5f25SMasami Hiramatsu 			ret = strbuf_addf(buf, ",%" PRIu64 "-%" PRIu64,
1069349e8d26SHe Kuang 					  start, end);
1070349e8d26SHe Kuang 		}
1071*bf4d5f25SMasami Hiramatsu 		if (ret < 0)
1072*bf4d5f25SMasami Hiramatsu 			goto out;
1073349e8d26SHe Kuang 	}
1074349e8d26SHe Kuang 
1075349e8d26SHe Kuang 	if (!first)
1076*bf4d5f25SMasami Hiramatsu 		ret = strbuf_add(buf, "]>", 2);
1077*bf4d5f25SMasami Hiramatsu out:
1078349e8d26SHe Kuang 	return ret;
1079349e8d26SHe Kuang }
1080bd0419e2SArnaldo Carvalho de Melo #else
1081bd0419e2SArnaldo Carvalho de Melo int die_get_var_range(Dwarf_Die *sp_die __maybe_unused,
1082bd0419e2SArnaldo Carvalho de Melo 		      Dwarf_Die *vr_die __maybe_unused,
1083bd0419e2SArnaldo Carvalho de Melo 		      struct strbuf *buf __maybe_unused)
1084bd0419e2SArnaldo Carvalho de Melo {
1085bd0419e2SArnaldo Carvalho de Melo 	return -ENOTSUP;
1086bd0419e2SArnaldo Carvalho de Melo }
1087bd0419e2SArnaldo Carvalho de Melo #endif
1088