xref: /openbmc/linux/tools/perf/util/dwarf-aux.c (revision b0e9cb2802d4bf50955cca8a7d87cf94ebf750a5)
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 
99e0d153c6SMasami Hiramatsu /**
100e0d153c6SMasami Hiramatsu  * die_compare_name - Compare diename and tname
101e0d153c6SMasami Hiramatsu  * @dw_die: a DIE
102e0d153c6SMasami Hiramatsu  * @tname: a string of target name
103e0d153c6SMasami Hiramatsu  *
104e0d153c6SMasami Hiramatsu  * Compare the name of @dw_die and @tname. Return false if @dw_die has no name.
105e0d153c6SMasami Hiramatsu  */
106e0d153c6SMasami Hiramatsu bool die_compare_name(Dwarf_Die *dw_die, const char *tname)
107e0d153c6SMasami Hiramatsu {
108e0d153c6SMasami Hiramatsu 	const char *name;
109e0d153c6SMasami Hiramatsu 	name = dwarf_diename(dw_die);
110e0d153c6SMasami Hiramatsu 	return name ? (strcmp(tname, name) == 0) : false;
111e0d153c6SMasami Hiramatsu }
112e0d153c6SMasami Hiramatsu 
113e0d153c6SMasami Hiramatsu /**
114e0d153c6SMasami Hiramatsu  * die_get_call_lineno - Get callsite line number of inline-function instance
115e0d153c6SMasami Hiramatsu  * @in_die: a DIE of an inlined function instance
116e0d153c6SMasami Hiramatsu  *
117e0d153c6SMasami Hiramatsu  * Get call-site line number of @in_die. This means from where the inline
118e0d153c6SMasami Hiramatsu  * function is called.
119e0d153c6SMasami Hiramatsu  */
120e0d153c6SMasami Hiramatsu int die_get_call_lineno(Dwarf_Die *in_die)
121e0d153c6SMasami Hiramatsu {
122e0d153c6SMasami Hiramatsu 	Dwarf_Attribute attr;
123e0d153c6SMasami Hiramatsu 	Dwarf_Word ret;
124e0d153c6SMasami Hiramatsu 
125e0d153c6SMasami Hiramatsu 	if (!dwarf_attr(in_die, DW_AT_call_line, &attr))
126e0d153c6SMasami Hiramatsu 		return -ENOENT;
127e0d153c6SMasami Hiramatsu 
128e0d153c6SMasami Hiramatsu 	dwarf_formudata(&attr, &ret);
129e0d153c6SMasami Hiramatsu 	return (int)ret;
130e0d153c6SMasami Hiramatsu }
131e0d153c6SMasami Hiramatsu 
132e0d153c6SMasami Hiramatsu /**
133e0d153c6SMasami Hiramatsu  * die_get_type - Get type DIE
134e0d153c6SMasami Hiramatsu  * @vr_die: a DIE of a variable
135e0d153c6SMasami Hiramatsu  * @die_mem: where to store a type DIE
136e0d153c6SMasami Hiramatsu  *
137e0d153c6SMasami Hiramatsu  * Get a DIE of the type of given variable (@vr_die), and store
138e0d153c6SMasami Hiramatsu  * it to die_mem. Return NULL if fails to get a type DIE.
139e0d153c6SMasami Hiramatsu  */
140e0d153c6SMasami Hiramatsu Dwarf_Die *die_get_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
141e0d153c6SMasami Hiramatsu {
142e0d153c6SMasami Hiramatsu 	Dwarf_Attribute attr;
143e0d153c6SMasami Hiramatsu 
144e0d153c6SMasami Hiramatsu 	if (dwarf_attr_integrate(vr_die, DW_AT_type, &attr) &&
145e0d153c6SMasami Hiramatsu 	    dwarf_formref_die(&attr, die_mem))
146e0d153c6SMasami Hiramatsu 		return die_mem;
147e0d153c6SMasami Hiramatsu 	else
148e0d153c6SMasami Hiramatsu 		return NULL;
149e0d153c6SMasami Hiramatsu }
150e0d153c6SMasami Hiramatsu 
151e0d153c6SMasami Hiramatsu /* Get a type die, but skip qualifiers */
152e0d153c6SMasami Hiramatsu static Dwarf_Die *__die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
153e0d153c6SMasami Hiramatsu {
154e0d153c6SMasami Hiramatsu 	int tag;
155e0d153c6SMasami Hiramatsu 
156e0d153c6SMasami Hiramatsu 	do {
157e0d153c6SMasami Hiramatsu 		vr_die = die_get_type(vr_die, die_mem);
158e0d153c6SMasami Hiramatsu 		if (!vr_die)
159e0d153c6SMasami Hiramatsu 			break;
160e0d153c6SMasami Hiramatsu 		tag = dwarf_tag(vr_die);
161e0d153c6SMasami Hiramatsu 	} while (tag == DW_TAG_const_type ||
162e0d153c6SMasami Hiramatsu 		 tag == DW_TAG_restrict_type ||
163e0d153c6SMasami Hiramatsu 		 tag == DW_TAG_volatile_type ||
164e0d153c6SMasami Hiramatsu 		 tag == DW_TAG_shared_type);
165e0d153c6SMasami Hiramatsu 
166e0d153c6SMasami Hiramatsu 	return vr_die;
167e0d153c6SMasami Hiramatsu }
168e0d153c6SMasami Hiramatsu 
169e0d153c6SMasami Hiramatsu /**
170e0d153c6SMasami Hiramatsu  * die_get_real_type - Get a type die, but skip qualifiers and typedef
171e0d153c6SMasami Hiramatsu  * @vr_die: a DIE of a variable
172e0d153c6SMasami Hiramatsu  * @die_mem: where to store a type DIE
173e0d153c6SMasami Hiramatsu  *
174e0d153c6SMasami Hiramatsu  * Get a DIE of the type of given variable (@vr_die), and store
175e0d153c6SMasami Hiramatsu  * it to die_mem. Return NULL if fails to get a type DIE.
176e0d153c6SMasami Hiramatsu  * If the type is qualifiers (e.g. const) or typedef, this skips it
177e0d153c6SMasami Hiramatsu  * and tries to find real type (structure or basic types, e.g. int).
178e0d153c6SMasami Hiramatsu  */
179e0d153c6SMasami Hiramatsu Dwarf_Die *die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
180e0d153c6SMasami Hiramatsu {
181e0d153c6SMasami Hiramatsu 	do {
182e0d153c6SMasami Hiramatsu 		vr_die = __die_get_real_type(vr_die, die_mem);
183e0d153c6SMasami Hiramatsu 	} while (vr_die && dwarf_tag(vr_die) == DW_TAG_typedef);
184e0d153c6SMasami Hiramatsu 
185e0d153c6SMasami Hiramatsu 	return vr_die;
186e0d153c6SMasami Hiramatsu }
187e0d153c6SMasami Hiramatsu 
188e0d153c6SMasami Hiramatsu /* Get attribute and translate it as a udata */
189e0d153c6SMasami Hiramatsu static int die_get_attr_udata(Dwarf_Die *tp_die, unsigned int attr_name,
190e0d153c6SMasami Hiramatsu 			      Dwarf_Word *result)
191e0d153c6SMasami Hiramatsu {
192e0d153c6SMasami Hiramatsu 	Dwarf_Attribute attr;
193e0d153c6SMasami Hiramatsu 
194e0d153c6SMasami Hiramatsu 	if (dwarf_attr(tp_die, attr_name, &attr) == NULL ||
195e0d153c6SMasami Hiramatsu 	    dwarf_formudata(&attr, result) != 0)
196e0d153c6SMasami Hiramatsu 		return -ENOENT;
197e0d153c6SMasami Hiramatsu 
198e0d153c6SMasami Hiramatsu 	return 0;
199e0d153c6SMasami Hiramatsu }
200e0d153c6SMasami Hiramatsu 
201*b0e9cb28SMasami Hiramatsu /* Get attribute and translate it as a sdata */
202*b0e9cb28SMasami Hiramatsu static int die_get_attr_sdata(Dwarf_Die *tp_die, unsigned int attr_name,
203*b0e9cb28SMasami Hiramatsu 			      Dwarf_Sword *result)
204*b0e9cb28SMasami Hiramatsu {
205*b0e9cb28SMasami Hiramatsu 	Dwarf_Attribute attr;
206*b0e9cb28SMasami Hiramatsu 
207*b0e9cb28SMasami Hiramatsu 	if (dwarf_attr(tp_die, attr_name, &attr) == NULL ||
208*b0e9cb28SMasami Hiramatsu 	    dwarf_formsdata(&attr, result) != 0)
209*b0e9cb28SMasami Hiramatsu 		return -ENOENT;
210*b0e9cb28SMasami Hiramatsu 
211*b0e9cb28SMasami Hiramatsu 	return 0;
212*b0e9cb28SMasami Hiramatsu }
213*b0e9cb28SMasami Hiramatsu 
214e0d153c6SMasami Hiramatsu /**
215e0d153c6SMasami Hiramatsu  * die_is_signed_type - Check whether a type DIE is signed or not
216e0d153c6SMasami Hiramatsu  * @tp_die: a DIE of a type
217e0d153c6SMasami Hiramatsu  *
218e0d153c6SMasami Hiramatsu  * Get the encoding of @tp_die and return true if the encoding
219e0d153c6SMasami Hiramatsu  * is signed.
220e0d153c6SMasami Hiramatsu  */
221e0d153c6SMasami Hiramatsu bool die_is_signed_type(Dwarf_Die *tp_die)
222e0d153c6SMasami Hiramatsu {
223e0d153c6SMasami Hiramatsu 	Dwarf_Word ret;
224e0d153c6SMasami Hiramatsu 
225e0d153c6SMasami Hiramatsu 	if (die_get_attr_udata(tp_die, DW_AT_encoding, &ret))
226e0d153c6SMasami Hiramatsu 		return false;
227e0d153c6SMasami Hiramatsu 
228e0d153c6SMasami Hiramatsu 	return (ret == DW_ATE_signed_char || ret == DW_ATE_signed ||
229e0d153c6SMasami Hiramatsu 		ret == DW_ATE_signed_fixed);
230e0d153c6SMasami Hiramatsu }
231e0d153c6SMasami Hiramatsu 
232e0d153c6SMasami Hiramatsu /**
233e0d153c6SMasami Hiramatsu  * die_get_data_member_location - Get the data-member offset
234e0d153c6SMasami Hiramatsu  * @mb_die: a DIE of a member of a data structure
235e0d153c6SMasami Hiramatsu  * @offs: The offset of the member in the data structure
236e0d153c6SMasami Hiramatsu  *
237e0d153c6SMasami Hiramatsu  * Get the offset of @mb_die in the data structure including @mb_die, and
238e0d153c6SMasami Hiramatsu  * stores result offset to @offs. If any error occurs this returns errno.
239e0d153c6SMasami Hiramatsu  */
240e0d153c6SMasami Hiramatsu int die_get_data_member_location(Dwarf_Die *mb_die, Dwarf_Word *offs)
241e0d153c6SMasami Hiramatsu {
242e0d153c6SMasami Hiramatsu 	Dwarf_Attribute attr;
243e0d153c6SMasami Hiramatsu 	Dwarf_Op *expr;
244e0d153c6SMasami Hiramatsu 	size_t nexpr;
245e0d153c6SMasami Hiramatsu 	int ret;
246e0d153c6SMasami Hiramatsu 
247e0d153c6SMasami Hiramatsu 	if (dwarf_attr(mb_die, DW_AT_data_member_location, &attr) == NULL)
248e0d153c6SMasami Hiramatsu 		return -ENOENT;
249e0d153c6SMasami Hiramatsu 
250e0d153c6SMasami Hiramatsu 	if (dwarf_formudata(&attr, offs) != 0) {
251e0d153c6SMasami Hiramatsu 		/* DW_AT_data_member_location should be DW_OP_plus_uconst */
252e0d153c6SMasami Hiramatsu 		ret = dwarf_getlocation(&attr, &expr, &nexpr);
253e0d153c6SMasami Hiramatsu 		if (ret < 0 || nexpr == 0)
254e0d153c6SMasami Hiramatsu 			return -ENOENT;
255e0d153c6SMasami Hiramatsu 
256e0d153c6SMasami Hiramatsu 		if (expr[0].atom != DW_OP_plus_uconst || nexpr != 1) {
257e0d153c6SMasami Hiramatsu 			pr_debug("Unable to get offset:Unexpected OP %x (%zd)\n",
258e0d153c6SMasami Hiramatsu 				 expr[0].atom, nexpr);
259e0d153c6SMasami Hiramatsu 			return -ENOTSUP;
260e0d153c6SMasami Hiramatsu 		}
261e0d153c6SMasami Hiramatsu 		*offs = (Dwarf_Word)expr[0].number;
262e0d153c6SMasami Hiramatsu 	}
263e0d153c6SMasami Hiramatsu 	return 0;
264e0d153c6SMasami Hiramatsu }
265e0d153c6SMasami Hiramatsu 
266*b0e9cb28SMasami Hiramatsu /* Get the call file index number in CU DIE */
267*b0e9cb28SMasami Hiramatsu static int die_get_call_fileno(Dwarf_Die *in_die)
268*b0e9cb28SMasami Hiramatsu {
269*b0e9cb28SMasami Hiramatsu 	Dwarf_Sword idx;
270*b0e9cb28SMasami Hiramatsu 
271*b0e9cb28SMasami Hiramatsu 	if (die_get_attr_sdata(in_die, DW_AT_call_file, &idx) == 0)
272*b0e9cb28SMasami Hiramatsu 		return (int)idx;
273*b0e9cb28SMasami Hiramatsu 	else
274*b0e9cb28SMasami Hiramatsu 		return -ENOENT;
275*b0e9cb28SMasami Hiramatsu }
276*b0e9cb28SMasami Hiramatsu 
277*b0e9cb28SMasami Hiramatsu /**
278*b0e9cb28SMasami Hiramatsu  * die_get_call_file - Get callsite file name of inlined function instance
279*b0e9cb28SMasami Hiramatsu  * @in_die: a DIE of an inlined function instance
280*b0e9cb28SMasami Hiramatsu  *
281*b0e9cb28SMasami Hiramatsu  * Get call-site file name of @in_die. This means from which file the inline
282*b0e9cb28SMasami Hiramatsu  * function is called.
283*b0e9cb28SMasami Hiramatsu  */
284*b0e9cb28SMasami Hiramatsu const char *die_get_call_file(Dwarf_Die *in_die)
285*b0e9cb28SMasami Hiramatsu {
286*b0e9cb28SMasami Hiramatsu 	Dwarf_Die cu_die;
287*b0e9cb28SMasami Hiramatsu 	Dwarf_Files *files;
288*b0e9cb28SMasami Hiramatsu 	int idx;
289*b0e9cb28SMasami Hiramatsu 
290*b0e9cb28SMasami Hiramatsu 	idx = die_get_call_fileno(in_die);
291*b0e9cb28SMasami Hiramatsu 	if (idx < 0 || !dwarf_diecu(in_die, &cu_die, NULL, NULL) ||
292*b0e9cb28SMasami Hiramatsu 	    dwarf_getsrcfiles(&cu_die, &files, NULL) != 0)
293*b0e9cb28SMasami Hiramatsu 		return NULL;
294*b0e9cb28SMasami Hiramatsu 
295*b0e9cb28SMasami Hiramatsu 	return dwarf_filesrc(files, idx, NULL, NULL);
296*b0e9cb28SMasami Hiramatsu }
297*b0e9cb28SMasami Hiramatsu 
298*b0e9cb28SMasami Hiramatsu 
299e0d153c6SMasami Hiramatsu /**
300e0d153c6SMasami Hiramatsu  * die_find_child - Generic DIE search function in DIE tree
301e0d153c6SMasami Hiramatsu  * @rt_die: a root DIE
302e0d153c6SMasami Hiramatsu  * @callback: a callback function
303e0d153c6SMasami Hiramatsu  * @data: a user data passed to the callback function
304e0d153c6SMasami Hiramatsu  * @die_mem: a buffer for result DIE
305e0d153c6SMasami Hiramatsu  *
306e0d153c6SMasami Hiramatsu  * Trace DIE tree from @rt_die and call @callback for each child DIE.
307e0d153c6SMasami Hiramatsu  * If @callback returns DIE_FIND_CB_END, this stores the DIE into
308e0d153c6SMasami Hiramatsu  * @die_mem and returns it. If @callback returns DIE_FIND_CB_CONTINUE,
309e0d153c6SMasami Hiramatsu  * this continues to trace the tree. Optionally, @callback can return
310e0d153c6SMasami Hiramatsu  * DIE_FIND_CB_CHILD and DIE_FIND_CB_SIBLING, those means trace only
311e0d153c6SMasami Hiramatsu  * the children and trace only the siblings respectively.
312e0d153c6SMasami Hiramatsu  * Returns NULL if @callback can't find any appropriate DIE.
313e0d153c6SMasami Hiramatsu  */
314e0d153c6SMasami Hiramatsu Dwarf_Die *die_find_child(Dwarf_Die *rt_die,
315e0d153c6SMasami Hiramatsu 			  int (*callback)(Dwarf_Die *, void *),
316e0d153c6SMasami Hiramatsu 			  void *data, Dwarf_Die *die_mem)
317e0d153c6SMasami Hiramatsu {
318e0d153c6SMasami Hiramatsu 	Dwarf_Die child_die;
319e0d153c6SMasami Hiramatsu 	int ret;
320e0d153c6SMasami Hiramatsu 
321e0d153c6SMasami Hiramatsu 	ret = dwarf_child(rt_die, die_mem);
322e0d153c6SMasami Hiramatsu 	if (ret != 0)
323e0d153c6SMasami Hiramatsu 		return NULL;
324e0d153c6SMasami Hiramatsu 
325e0d153c6SMasami Hiramatsu 	do {
326e0d153c6SMasami Hiramatsu 		ret = callback(die_mem, data);
327e0d153c6SMasami Hiramatsu 		if (ret == DIE_FIND_CB_END)
328e0d153c6SMasami Hiramatsu 			return die_mem;
329e0d153c6SMasami Hiramatsu 
330e0d153c6SMasami Hiramatsu 		if ((ret & DIE_FIND_CB_CHILD) &&
331e0d153c6SMasami Hiramatsu 		    die_find_child(die_mem, callback, data, &child_die)) {
332e0d153c6SMasami Hiramatsu 			memcpy(die_mem, &child_die, sizeof(Dwarf_Die));
333e0d153c6SMasami Hiramatsu 			return die_mem;
334e0d153c6SMasami Hiramatsu 		}
335e0d153c6SMasami Hiramatsu 	} while ((ret & DIE_FIND_CB_SIBLING) &&
336e0d153c6SMasami Hiramatsu 		 dwarf_siblingof(die_mem, die_mem) == 0);
337e0d153c6SMasami Hiramatsu 
338e0d153c6SMasami Hiramatsu 	return NULL;
339e0d153c6SMasami Hiramatsu }
340e0d153c6SMasami Hiramatsu 
341e0d153c6SMasami Hiramatsu struct __addr_die_search_param {
342e0d153c6SMasami Hiramatsu 	Dwarf_Addr	addr;
343e0d153c6SMasami Hiramatsu 	Dwarf_Die	*die_mem;
344e0d153c6SMasami Hiramatsu };
345e0d153c6SMasami Hiramatsu 
346e0d153c6SMasami Hiramatsu /* die_find callback for non-inlined function search */
347e0d153c6SMasami Hiramatsu static int __die_search_func_cb(Dwarf_Die *fn_die, void *data)
348e0d153c6SMasami Hiramatsu {
349e0d153c6SMasami Hiramatsu 	struct __addr_die_search_param *ad = data;
350e0d153c6SMasami Hiramatsu 
351e0d153c6SMasami Hiramatsu 	if (dwarf_tag(fn_die) == DW_TAG_subprogram &&
352e0d153c6SMasami Hiramatsu 	    dwarf_haspc(fn_die, ad->addr)) {
353e0d153c6SMasami Hiramatsu 		memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die));
354e0d153c6SMasami Hiramatsu 		return DWARF_CB_ABORT;
355e0d153c6SMasami Hiramatsu 	}
356e0d153c6SMasami Hiramatsu 	return DWARF_CB_OK;
357e0d153c6SMasami Hiramatsu }
358e0d153c6SMasami Hiramatsu 
359e0d153c6SMasami Hiramatsu /**
360e0d153c6SMasami Hiramatsu  * die_find_realfunc - Search a non-inlined function at given address
361e0d153c6SMasami Hiramatsu  * @cu_die: a CU DIE which including @addr
362e0d153c6SMasami Hiramatsu  * @addr: target address
363e0d153c6SMasami Hiramatsu  * @die_mem: a buffer for result DIE
364e0d153c6SMasami Hiramatsu  *
365e0d153c6SMasami Hiramatsu  * Search a non-inlined function DIE which includes @addr. Stores the
366e0d153c6SMasami Hiramatsu  * DIE to @die_mem and returns it if found. Returns NULl if failed.
367e0d153c6SMasami Hiramatsu  */
368e0d153c6SMasami Hiramatsu Dwarf_Die *die_find_realfunc(Dwarf_Die *cu_die, Dwarf_Addr addr,
369e0d153c6SMasami Hiramatsu 				    Dwarf_Die *die_mem)
370e0d153c6SMasami Hiramatsu {
371e0d153c6SMasami Hiramatsu 	struct __addr_die_search_param ad;
372e0d153c6SMasami Hiramatsu 	ad.addr = addr;
373e0d153c6SMasami Hiramatsu 	ad.die_mem = die_mem;
374e0d153c6SMasami Hiramatsu 	/* dwarf_getscopes can't find subprogram. */
375e0d153c6SMasami Hiramatsu 	if (!dwarf_getfuncs(cu_die, __die_search_func_cb, &ad, 0))
376e0d153c6SMasami Hiramatsu 		return NULL;
377e0d153c6SMasami Hiramatsu 	else
378e0d153c6SMasami Hiramatsu 		return die_mem;
379e0d153c6SMasami Hiramatsu }
380e0d153c6SMasami Hiramatsu 
381e0d153c6SMasami Hiramatsu /* die_find callback for inline function search */
382e0d153c6SMasami Hiramatsu static int __die_find_inline_cb(Dwarf_Die *die_mem, void *data)
383e0d153c6SMasami Hiramatsu {
384e0d153c6SMasami Hiramatsu 	Dwarf_Addr *addr = data;
385e0d153c6SMasami Hiramatsu 
386e0d153c6SMasami Hiramatsu 	if (dwarf_tag(die_mem) == DW_TAG_inlined_subroutine &&
387e0d153c6SMasami Hiramatsu 	    dwarf_haspc(die_mem, *addr))
388e0d153c6SMasami Hiramatsu 		return DIE_FIND_CB_END;
389e0d153c6SMasami Hiramatsu 
390e0d153c6SMasami Hiramatsu 	return DIE_FIND_CB_CONTINUE;
391e0d153c6SMasami Hiramatsu }
392e0d153c6SMasami Hiramatsu 
393e0d153c6SMasami Hiramatsu /**
394e0d153c6SMasami Hiramatsu  * die_find_inlinefunc - Search an inlined function at given address
395e0d153c6SMasami Hiramatsu  * @cu_die: a CU DIE which including @addr
396e0d153c6SMasami Hiramatsu  * @addr: target address
397e0d153c6SMasami Hiramatsu  * @die_mem: a buffer for result DIE
398e0d153c6SMasami Hiramatsu  *
399e0d153c6SMasami Hiramatsu  * Search an inlined function DIE which includes @addr. Stores the
400e0d153c6SMasami Hiramatsu  * DIE to @die_mem and returns it if found. Returns NULl if failed.
401e0d153c6SMasami Hiramatsu  * If several inlined functions are expanded recursively, this trace
402e0d153c6SMasami Hiramatsu  * it and returns deepest one.
403e0d153c6SMasami Hiramatsu  */
404e0d153c6SMasami Hiramatsu Dwarf_Die *die_find_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
405e0d153c6SMasami Hiramatsu 			       Dwarf_Die *die_mem)
406e0d153c6SMasami Hiramatsu {
407e0d153c6SMasami Hiramatsu 	Dwarf_Die tmp_die;
408e0d153c6SMasami Hiramatsu 
409e0d153c6SMasami Hiramatsu 	sp_die = die_find_child(sp_die, __die_find_inline_cb, &addr, &tmp_die);
410e0d153c6SMasami Hiramatsu 	if (!sp_die)
411e0d153c6SMasami Hiramatsu 		return NULL;
412e0d153c6SMasami Hiramatsu 
413e0d153c6SMasami Hiramatsu 	/* Inlined function could be recursive. Trace it until fail */
414e0d153c6SMasami Hiramatsu 	while (sp_die) {
415e0d153c6SMasami Hiramatsu 		memcpy(die_mem, sp_die, sizeof(Dwarf_Die));
416e0d153c6SMasami Hiramatsu 		sp_die = die_find_child(sp_die, __die_find_inline_cb, &addr,
417e0d153c6SMasami Hiramatsu 					&tmp_die);
418e0d153c6SMasami Hiramatsu 	}
419e0d153c6SMasami Hiramatsu 
420e0d153c6SMasami Hiramatsu 	return die_mem;
421e0d153c6SMasami Hiramatsu }
422e0d153c6SMasami Hiramatsu 
423e0d153c6SMasami Hiramatsu /* Line walker internal parameters */
424e0d153c6SMasami Hiramatsu struct __line_walk_param {
425*b0e9cb28SMasami Hiramatsu 	bool recursive;
426e0d153c6SMasami Hiramatsu 	line_walk_callback_t callback;
427e0d153c6SMasami Hiramatsu 	void *data;
428e0d153c6SMasami Hiramatsu 	int retval;
429e0d153c6SMasami Hiramatsu };
430e0d153c6SMasami Hiramatsu 
431e0d153c6SMasami Hiramatsu static int __die_walk_funclines_cb(Dwarf_Die *in_die, void *data)
432e0d153c6SMasami Hiramatsu {
433e0d153c6SMasami Hiramatsu 	struct __line_walk_param *lw = data;
434*b0e9cb28SMasami Hiramatsu 	Dwarf_Addr addr = 0;
435*b0e9cb28SMasami Hiramatsu 	const char *fname;
436e0d153c6SMasami Hiramatsu 	int lineno;
437e0d153c6SMasami Hiramatsu 
438e0d153c6SMasami Hiramatsu 	if (dwarf_tag(in_die) == DW_TAG_inlined_subroutine) {
439*b0e9cb28SMasami Hiramatsu 		fname = die_get_call_file(in_die);
440e0d153c6SMasami Hiramatsu 		lineno = die_get_call_lineno(in_die);
441*b0e9cb28SMasami Hiramatsu 		if (fname && lineno > 0 && dwarf_entrypc(in_die, &addr) == 0) {
442*b0e9cb28SMasami Hiramatsu 			lw->retval = lw->callback(fname, lineno, addr, lw->data);
443e0d153c6SMasami Hiramatsu 			if (lw->retval != 0)
444e0d153c6SMasami Hiramatsu 				return DIE_FIND_CB_END;
445e0d153c6SMasami Hiramatsu 		}
446e0d153c6SMasami Hiramatsu 	}
447*b0e9cb28SMasami Hiramatsu 	if (!lw->recursive)
448*b0e9cb28SMasami Hiramatsu 		/* Don't need to search recursively */
449e0d153c6SMasami Hiramatsu 		return DIE_FIND_CB_SIBLING;
450*b0e9cb28SMasami Hiramatsu 
451*b0e9cb28SMasami Hiramatsu 	if (addr) {
452*b0e9cb28SMasami Hiramatsu 		fname = dwarf_decl_file(in_die);
453*b0e9cb28SMasami Hiramatsu 		if (fname && dwarf_decl_line(in_die, &lineno) == 0) {
454*b0e9cb28SMasami Hiramatsu 			lw->retval = lw->callback(fname, lineno, addr, lw->data);
455*b0e9cb28SMasami Hiramatsu 			if (lw->retval != 0)
456*b0e9cb28SMasami Hiramatsu 				return DIE_FIND_CB_END;
457*b0e9cb28SMasami Hiramatsu 		}
458*b0e9cb28SMasami Hiramatsu 	}
459*b0e9cb28SMasami Hiramatsu 
460*b0e9cb28SMasami Hiramatsu 	/* Continue to search nested inlined function call-sites */
461*b0e9cb28SMasami Hiramatsu 	return DIE_FIND_CB_CONTINUE;
462e0d153c6SMasami Hiramatsu }
463e0d153c6SMasami Hiramatsu 
464e0d153c6SMasami Hiramatsu /* Walk on lines of blocks included in given DIE */
465*b0e9cb28SMasami Hiramatsu static int __die_walk_funclines(Dwarf_Die *sp_die, bool recursive,
466e0d153c6SMasami Hiramatsu 				line_walk_callback_t callback, void *data)
467e0d153c6SMasami Hiramatsu {
468e0d153c6SMasami Hiramatsu 	struct __line_walk_param lw = {
469*b0e9cb28SMasami Hiramatsu 		.recursive = recursive,
470e0d153c6SMasami Hiramatsu 		.callback = callback,
471e0d153c6SMasami Hiramatsu 		.data = data,
472e0d153c6SMasami Hiramatsu 		.retval = 0,
473e0d153c6SMasami Hiramatsu 	};
474e0d153c6SMasami Hiramatsu 	Dwarf_Die die_mem;
475e0d153c6SMasami Hiramatsu 	Dwarf_Addr addr;
476*b0e9cb28SMasami Hiramatsu 	const char *fname;
477e0d153c6SMasami Hiramatsu 	int lineno;
478e0d153c6SMasami Hiramatsu 
479e0d153c6SMasami Hiramatsu 	/* Handle function declaration line */
480*b0e9cb28SMasami Hiramatsu 	fname = dwarf_decl_file(sp_die);
481*b0e9cb28SMasami Hiramatsu 	if (fname && dwarf_decl_line(sp_die, &lineno) == 0 &&
482e0d153c6SMasami Hiramatsu 	    dwarf_entrypc(sp_die, &addr) == 0) {
483*b0e9cb28SMasami Hiramatsu 		lw.retval = callback(fname, lineno, addr, data);
484e0d153c6SMasami Hiramatsu 		if (lw.retval != 0)
485e0d153c6SMasami Hiramatsu 			goto done;
486e0d153c6SMasami Hiramatsu 	}
487e0d153c6SMasami Hiramatsu 	die_find_child(sp_die, __die_walk_funclines_cb, &lw, &die_mem);
488e0d153c6SMasami Hiramatsu done:
489e0d153c6SMasami Hiramatsu 	return lw.retval;
490e0d153c6SMasami Hiramatsu }
491e0d153c6SMasami Hiramatsu 
492e0d153c6SMasami Hiramatsu static int __die_walk_culines_cb(Dwarf_Die *sp_die, void *data)
493e0d153c6SMasami Hiramatsu {
494e0d153c6SMasami Hiramatsu 	struct __line_walk_param *lw = data;
495e0d153c6SMasami Hiramatsu 
496*b0e9cb28SMasami Hiramatsu 	lw->retval = __die_walk_funclines(sp_die, true, lw->callback, lw->data);
497e0d153c6SMasami Hiramatsu 	if (lw->retval != 0)
498e0d153c6SMasami Hiramatsu 		return DWARF_CB_ABORT;
499e0d153c6SMasami Hiramatsu 
500e0d153c6SMasami Hiramatsu 	return DWARF_CB_OK;
501e0d153c6SMasami Hiramatsu }
502e0d153c6SMasami Hiramatsu 
503e0d153c6SMasami Hiramatsu /**
504e0d153c6SMasami Hiramatsu  * die_walk_lines - Walk on lines inside given DIE
505a128405cSMasami Hiramatsu  * @rt_die: a root DIE (CU, subprogram or inlined_subroutine)
506e0d153c6SMasami Hiramatsu  * @callback: callback routine
507e0d153c6SMasami Hiramatsu  * @data: user data
508e0d153c6SMasami Hiramatsu  *
509e0d153c6SMasami Hiramatsu  * Walk on all lines inside given @rt_die and call @callback on each line.
510e0d153c6SMasami Hiramatsu  * If the @rt_die is a function, walk only on the lines inside the function,
511e0d153c6SMasami Hiramatsu  * otherwise @rt_die must be a CU DIE.
512e0d153c6SMasami Hiramatsu  * Note that this walks not only dwarf line list, but also function entries
513e0d153c6SMasami Hiramatsu  * and inline call-site.
514e0d153c6SMasami Hiramatsu  */
515e0d153c6SMasami Hiramatsu int die_walk_lines(Dwarf_Die *rt_die, line_walk_callback_t callback, void *data)
516e0d153c6SMasami Hiramatsu {
517e0d153c6SMasami Hiramatsu 	Dwarf_Lines *lines;
518e0d153c6SMasami Hiramatsu 	Dwarf_Line *line;
519e0d153c6SMasami Hiramatsu 	Dwarf_Addr addr;
520e0d153c6SMasami Hiramatsu 	const char *fname;
521e0d153c6SMasami Hiramatsu 	int lineno, ret = 0;
522e0d153c6SMasami Hiramatsu 	Dwarf_Die die_mem, *cu_die;
523e0d153c6SMasami Hiramatsu 	size_t nlines, i;
524e0d153c6SMasami Hiramatsu 
525e0d153c6SMasami Hiramatsu 	/* Get the CU die */
526a128405cSMasami Hiramatsu 	if (dwarf_tag(rt_die) != DW_TAG_compile_unit)
527e0d153c6SMasami Hiramatsu 		cu_die = dwarf_diecu(rt_die, &die_mem, NULL, NULL);
528e0d153c6SMasami Hiramatsu 	else
529e0d153c6SMasami Hiramatsu 		cu_die = rt_die;
530e0d153c6SMasami Hiramatsu 	if (!cu_die) {
531a128405cSMasami Hiramatsu 		pr_debug2("Failed to get CU from given DIE.\n");
532e0d153c6SMasami Hiramatsu 		return -EINVAL;
533e0d153c6SMasami Hiramatsu 	}
534e0d153c6SMasami Hiramatsu 
535e0d153c6SMasami Hiramatsu 	/* Get lines list in the CU */
536e0d153c6SMasami Hiramatsu 	if (dwarf_getsrclines(cu_die, &lines, &nlines) != 0) {
537e0d153c6SMasami Hiramatsu 		pr_debug2("Failed to get source lines on this CU.\n");
538e0d153c6SMasami Hiramatsu 		return -ENOENT;
539e0d153c6SMasami Hiramatsu 	}
540e0d153c6SMasami Hiramatsu 	pr_debug2("Get %zd lines from this CU\n", nlines);
541e0d153c6SMasami Hiramatsu 
542e0d153c6SMasami Hiramatsu 	/* Walk on the lines on lines list */
543e0d153c6SMasami Hiramatsu 	for (i = 0; i < nlines; i++) {
544e0d153c6SMasami Hiramatsu 		line = dwarf_onesrcline(lines, i);
545e0d153c6SMasami Hiramatsu 		if (line == NULL ||
546e0d153c6SMasami Hiramatsu 		    dwarf_lineno(line, &lineno) != 0 ||
547e0d153c6SMasami Hiramatsu 		    dwarf_lineaddr(line, &addr) != 0) {
548e0d153c6SMasami Hiramatsu 			pr_debug2("Failed to get line info. "
549e0d153c6SMasami Hiramatsu 				  "Possible error in debuginfo.\n");
550e0d153c6SMasami Hiramatsu 			continue;
551e0d153c6SMasami Hiramatsu 		}
552e0d153c6SMasami Hiramatsu 		/* Filter lines based on address */
553e0d153c6SMasami Hiramatsu 		if (rt_die != cu_die)
554e0d153c6SMasami Hiramatsu 			/*
555e0d153c6SMasami Hiramatsu 			 * Address filtering
556e0d153c6SMasami Hiramatsu 			 * The line is included in given function, and
557e0d153c6SMasami Hiramatsu 			 * no inline block includes it.
558e0d153c6SMasami Hiramatsu 			 */
559e0d153c6SMasami Hiramatsu 			if (!dwarf_haspc(rt_die, addr) ||
560e0d153c6SMasami Hiramatsu 			    die_find_inlinefunc(rt_die, addr, &die_mem))
561e0d153c6SMasami Hiramatsu 				continue;
562e0d153c6SMasami Hiramatsu 		/* Get source line */
563e0d153c6SMasami Hiramatsu 		fname = dwarf_linesrc(line, NULL, NULL);
564e0d153c6SMasami Hiramatsu 
565e0d153c6SMasami Hiramatsu 		ret = callback(fname, lineno, addr, data);
566e0d153c6SMasami Hiramatsu 		if (ret != 0)
567e0d153c6SMasami Hiramatsu 			return ret;
568e0d153c6SMasami Hiramatsu 	}
569e0d153c6SMasami Hiramatsu 
570e0d153c6SMasami Hiramatsu 	/*
571e0d153c6SMasami Hiramatsu 	 * Dwarf lines doesn't include function declarations and inlined
572e0d153c6SMasami Hiramatsu 	 * subroutines. We have to check functions list or given function.
573e0d153c6SMasami Hiramatsu 	 */
574e0d153c6SMasami Hiramatsu 	if (rt_die != cu_die)
575*b0e9cb28SMasami Hiramatsu 		/*
576*b0e9cb28SMasami Hiramatsu 		 * Don't need walk functions recursively, because nested
577*b0e9cb28SMasami Hiramatsu 		 * inlined functions don't have lines of the specified DIE.
578*b0e9cb28SMasami Hiramatsu 		 */
579*b0e9cb28SMasami Hiramatsu 		ret = __die_walk_funclines(rt_die, false, callback, data);
580e0d153c6SMasami Hiramatsu 	else {
581e0d153c6SMasami Hiramatsu 		struct __line_walk_param param = {
582e0d153c6SMasami Hiramatsu 			.callback = callback,
583e0d153c6SMasami Hiramatsu 			.data = data,
584e0d153c6SMasami Hiramatsu 			.retval = 0,
585e0d153c6SMasami Hiramatsu 		};
586e0d153c6SMasami Hiramatsu 		dwarf_getfuncs(cu_die, __die_walk_culines_cb, &param, 0);
587e0d153c6SMasami Hiramatsu 		ret = param.retval;
588e0d153c6SMasami Hiramatsu 	}
589e0d153c6SMasami Hiramatsu 
590e0d153c6SMasami Hiramatsu 	return ret;
591e0d153c6SMasami Hiramatsu }
592e0d153c6SMasami Hiramatsu 
593e0d153c6SMasami Hiramatsu struct __find_variable_param {
594e0d153c6SMasami Hiramatsu 	const char *name;
595e0d153c6SMasami Hiramatsu 	Dwarf_Addr addr;
596e0d153c6SMasami Hiramatsu };
597e0d153c6SMasami Hiramatsu 
598e0d153c6SMasami Hiramatsu static int __die_find_variable_cb(Dwarf_Die *die_mem, void *data)
599e0d153c6SMasami Hiramatsu {
600e0d153c6SMasami Hiramatsu 	struct __find_variable_param *fvp = data;
601e0d153c6SMasami Hiramatsu 	int tag;
602e0d153c6SMasami Hiramatsu 
603e0d153c6SMasami Hiramatsu 	tag = dwarf_tag(die_mem);
604e0d153c6SMasami Hiramatsu 	if ((tag == DW_TAG_formal_parameter ||
605e0d153c6SMasami Hiramatsu 	     tag == DW_TAG_variable) &&
606e0d153c6SMasami Hiramatsu 	    die_compare_name(die_mem, fvp->name))
607e0d153c6SMasami Hiramatsu 		return DIE_FIND_CB_END;
608e0d153c6SMasami Hiramatsu 
609e0d153c6SMasami Hiramatsu 	if (dwarf_haspc(die_mem, fvp->addr))
610e0d153c6SMasami Hiramatsu 		return DIE_FIND_CB_CONTINUE;
611e0d153c6SMasami Hiramatsu 	else
612e0d153c6SMasami Hiramatsu 		return DIE_FIND_CB_SIBLING;
613e0d153c6SMasami Hiramatsu }
614e0d153c6SMasami Hiramatsu 
615e0d153c6SMasami Hiramatsu /**
616e0d153c6SMasami Hiramatsu  * die_find_variable_at - Find a given name variable at given address
617e0d153c6SMasami Hiramatsu  * @sp_die: a function DIE
618e0d153c6SMasami Hiramatsu  * @name: variable name
619e0d153c6SMasami Hiramatsu  * @addr: address
620e0d153c6SMasami Hiramatsu  * @die_mem: a buffer for result DIE
621e0d153c6SMasami Hiramatsu  *
622e0d153c6SMasami Hiramatsu  * Find a variable DIE called @name at @addr in @sp_die.
623e0d153c6SMasami Hiramatsu  */
624e0d153c6SMasami Hiramatsu Dwarf_Die *die_find_variable_at(Dwarf_Die *sp_die, const char *name,
625e0d153c6SMasami Hiramatsu 				Dwarf_Addr addr, Dwarf_Die *die_mem)
626e0d153c6SMasami Hiramatsu {
627e0d153c6SMasami Hiramatsu 	struct __find_variable_param fvp = { .name = name, .addr = addr};
628e0d153c6SMasami Hiramatsu 
629e0d153c6SMasami Hiramatsu 	return die_find_child(sp_die, __die_find_variable_cb, (void *)&fvp,
630e0d153c6SMasami Hiramatsu 			      die_mem);
631e0d153c6SMasami Hiramatsu }
632e0d153c6SMasami Hiramatsu 
633e0d153c6SMasami Hiramatsu static int __die_find_member_cb(Dwarf_Die *die_mem, void *data)
634e0d153c6SMasami Hiramatsu {
635e0d153c6SMasami Hiramatsu 	const char *name = data;
636e0d153c6SMasami Hiramatsu 
637e0d153c6SMasami Hiramatsu 	if ((dwarf_tag(die_mem) == DW_TAG_member) &&
638e0d153c6SMasami Hiramatsu 	    die_compare_name(die_mem, name))
639e0d153c6SMasami Hiramatsu 		return DIE_FIND_CB_END;
640e0d153c6SMasami Hiramatsu 
641e0d153c6SMasami Hiramatsu 	return DIE_FIND_CB_SIBLING;
642e0d153c6SMasami Hiramatsu }
643e0d153c6SMasami Hiramatsu 
644e0d153c6SMasami Hiramatsu /**
645e0d153c6SMasami Hiramatsu  * die_find_member - Find a given name member in a data structure
646e0d153c6SMasami Hiramatsu  * @st_die: a data structure type DIE
647e0d153c6SMasami Hiramatsu  * @name: member name
648e0d153c6SMasami Hiramatsu  * @die_mem: a buffer for result DIE
649e0d153c6SMasami Hiramatsu  *
650e0d153c6SMasami Hiramatsu  * Find a member DIE called @name in @st_die.
651e0d153c6SMasami Hiramatsu  */
652e0d153c6SMasami Hiramatsu Dwarf_Die *die_find_member(Dwarf_Die *st_die, const char *name,
653e0d153c6SMasami Hiramatsu 			   Dwarf_Die *die_mem)
654e0d153c6SMasami Hiramatsu {
655e0d153c6SMasami Hiramatsu 	return die_find_child(st_die, __die_find_member_cb, (void *)name,
656e0d153c6SMasami Hiramatsu 			      die_mem);
657e0d153c6SMasami Hiramatsu }
658e0d153c6SMasami Hiramatsu 
659e0d153c6SMasami Hiramatsu /**
660e0d153c6SMasami Hiramatsu  * die_get_typename - Get the name of given variable DIE
661e0d153c6SMasami Hiramatsu  * @vr_die: a variable DIE
662e0d153c6SMasami Hiramatsu  * @buf: a buffer for result type name
663e0d153c6SMasami Hiramatsu  * @len: a max-length of @buf
664e0d153c6SMasami Hiramatsu  *
665e0d153c6SMasami Hiramatsu  * Get the name of @vr_die and stores it to @buf. Return the actual length
666e0d153c6SMasami Hiramatsu  * of type name if succeeded. Return -E2BIG if @len is not enough long, and
667e0d153c6SMasami Hiramatsu  * Return -ENOENT if failed to find type name.
668e0d153c6SMasami Hiramatsu  * Note that the result will stores typedef name if possible, and stores
669e0d153c6SMasami Hiramatsu  * "*(function_type)" if the type is a function pointer.
670e0d153c6SMasami Hiramatsu  */
671e0d153c6SMasami Hiramatsu int die_get_typename(Dwarf_Die *vr_die, char *buf, int len)
672e0d153c6SMasami Hiramatsu {
673e0d153c6SMasami Hiramatsu 	Dwarf_Die type;
674e0d153c6SMasami Hiramatsu 	int tag, ret, ret2;
675e0d153c6SMasami Hiramatsu 	const char *tmp = "";
676e0d153c6SMasami Hiramatsu 
677e0d153c6SMasami Hiramatsu 	if (__die_get_real_type(vr_die, &type) == NULL)
678e0d153c6SMasami Hiramatsu 		return -ENOENT;
679e0d153c6SMasami Hiramatsu 
680e0d153c6SMasami Hiramatsu 	tag = dwarf_tag(&type);
681e0d153c6SMasami Hiramatsu 	if (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type)
682e0d153c6SMasami Hiramatsu 		tmp = "*";
683e0d153c6SMasami Hiramatsu 	else if (tag == DW_TAG_subroutine_type) {
684e0d153c6SMasami Hiramatsu 		/* Function pointer */
685e0d153c6SMasami Hiramatsu 		ret = snprintf(buf, len, "(function_type)");
686e0d153c6SMasami Hiramatsu 		return (ret >= len) ? -E2BIG : ret;
687e0d153c6SMasami Hiramatsu 	} else {
688e0d153c6SMasami Hiramatsu 		if (!dwarf_diename(&type))
689e0d153c6SMasami Hiramatsu 			return -ENOENT;
690e0d153c6SMasami Hiramatsu 		if (tag == DW_TAG_union_type)
691e0d153c6SMasami Hiramatsu 			tmp = "union ";
692e0d153c6SMasami Hiramatsu 		else if (tag == DW_TAG_structure_type)
693e0d153c6SMasami Hiramatsu 			tmp = "struct ";
694e0d153c6SMasami Hiramatsu 		/* Write a base name */
695e0d153c6SMasami Hiramatsu 		ret = snprintf(buf, len, "%s%s", tmp, dwarf_diename(&type));
696e0d153c6SMasami Hiramatsu 		return (ret >= len) ? -E2BIG : ret;
697e0d153c6SMasami Hiramatsu 	}
698e0d153c6SMasami Hiramatsu 	ret = die_get_typename(&type, buf, len);
699e0d153c6SMasami Hiramatsu 	if (ret > 0) {
700e0d153c6SMasami Hiramatsu 		ret2 = snprintf(buf + ret, len - ret, "%s", tmp);
701e0d153c6SMasami Hiramatsu 		ret = (ret2 >= len - ret) ? -E2BIG : ret2 + ret;
702e0d153c6SMasami Hiramatsu 	}
703e0d153c6SMasami Hiramatsu 	return ret;
704e0d153c6SMasami Hiramatsu }
705e0d153c6SMasami Hiramatsu 
706e0d153c6SMasami Hiramatsu /**
707e0d153c6SMasami Hiramatsu  * die_get_varname - Get the name and type of given variable DIE
708e0d153c6SMasami Hiramatsu  * @vr_die: a variable DIE
709e0d153c6SMasami Hiramatsu  * @buf: a buffer for type and variable name
710e0d153c6SMasami Hiramatsu  * @len: the max-length of @buf
711e0d153c6SMasami Hiramatsu  *
712e0d153c6SMasami Hiramatsu  * Get the name and type of @vr_die and stores it in @buf as "type\tname".
713e0d153c6SMasami Hiramatsu  */
714e0d153c6SMasami Hiramatsu int die_get_varname(Dwarf_Die *vr_die, char *buf, int len)
715e0d153c6SMasami Hiramatsu {
716e0d153c6SMasami Hiramatsu 	int ret, ret2;
717e0d153c6SMasami Hiramatsu 
718e0d153c6SMasami Hiramatsu 	ret = die_get_typename(vr_die, buf, len);
719e0d153c6SMasami Hiramatsu 	if (ret < 0) {
720e0d153c6SMasami Hiramatsu 		pr_debug("Failed to get type, make it unknown.\n");
721e0d153c6SMasami Hiramatsu 		ret = snprintf(buf, len, "(unknown_type)");
722e0d153c6SMasami Hiramatsu 	}
723e0d153c6SMasami Hiramatsu 	if (ret > 0) {
724e0d153c6SMasami Hiramatsu 		ret2 = snprintf(buf + ret, len - ret, "\t%s",
725e0d153c6SMasami Hiramatsu 				dwarf_diename(vr_die));
726e0d153c6SMasami Hiramatsu 		ret = (ret2 >= len - ret) ? -E2BIG : ret2 + ret;
727e0d153c6SMasami Hiramatsu 	}
728e0d153c6SMasami Hiramatsu 	return ret;
729e0d153c6SMasami Hiramatsu }
730e0d153c6SMasami Hiramatsu 
731