xref: /openbmc/linux/tools/perf/util/dwarf-aux.c (revision ab4200c17ba6fe71d2da64317aae8a8aa684624c)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2e0d153c6SMasami Hiramatsu /*
3e0d153c6SMasami Hiramatsu  * dwarf-aux.c : libdw auxiliary interfaces
4e0d153c6SMasami Hiramatsu  */
5e0d153c6SMasami Hiramatsu 
6a43783aeSArnaldo Carvalho de Melo #include <errno.h>
7fd20e811SArnaldo Carvalho de Melo #include <inttypes.h>
8e0d153c6SMasami Hiramatsu #include <stdbool.h>
9215a0d30SArnaldo Carvalho de Melo #include <stdlib.h>
10e0d153c6SMasami Hiramatsu #include "debug.h"
11e0d153c6SMasami Hiramatsu #include "dwarf-aux.h"
128520a98dSArnaldo Carvalho de Melo #include "strbuf.h"
13a067558eSArnaldo Carvalho de Melo #include "string2.h"
14e0d153c6SMasami Hiramatsu 
15e0d153c6SMasami Hiramatsu /**
16e0d153c6SMasami Hiramatsu  * cu_find_realpath - Find the realpath of the target file
17e0d153c6SMasami Hiramatsu  * @cu_die: A DIE(dwarf information entry) of CU(compilation Unit)
18e0d153c6SMasami Hiramatsu  * @fname:  The tail filename of the target file
19e0d153c6SMasami Hiramatsu  *
20e0d153c6SMasami Hiramatsu  * Find the real(long) path of @fname in @cu_die.
21e0d153c6SMasami Hiramatsu  */
22e0d153c6SMasami Hiramatsu const char *cu_find_realpath(Dwarf_Die *cu_die, const char *fname)
23e0d153c6SMasami Hiramatsu {
24e0d153c6SMasami Hiramatsu 	Dwarf_Files *files;
25e0d153c6SMasami Hiramatsu 	size_t nfiles, i;
26e0d153c6SMasami Hiramatsu 	const char *src = NULL;
27e0d153c6SMasami Hiramatsu 	int ret;
28e0d153c6SMasami Hiramatsu 
29e0d153c6SMasami Hiramatsu 	if (!fname)
30e0d153c6SMasami Hiramatsu 		return NULL;
31e0d153c6SMasami Hiramatsu 
32e0d153c6SMasami Hiramatsu 	ret = dwarf_getsrcfiles(cu_die, &files, &nfiles);
33e0d153c6SMasami Hiramatsu 	if (ret != 0)
34e0d153c6SMasami Hiramatsu 		return NULL;
35e0d153c6SMasami Hiramatsu 
36e0d153c6SMasami Hiramatsu 	for (i = 0; i < nfiles; i++) {
37e0d153c6SMasami Hiramatsu 		src = dwarf_filesrc(files, i, NULL, NULL);
38e0d153c6SMasami Hiramatsu 		if (strtailcmp(src, fname) == 0)
39e0d153c6SMasami Hiramatsu 			break;
40e0d153c6SMasami Hiramatsu 	}
41e0d153c6SMasami Hiramatsu 	if (i == nfiles)
42e0d153c6SMasami Hiramatsu 		return NULL;
43e0d153c6SMasami Hiramatsu 	return src;
44e0d153c6SMasami Hiramatsu }
45e0d153c6SMasami Hiramatsu 
46e0d153c6SMasami Hiramatsu /**
47e0d153c6SMasami Hiramatsu  * cu_get_comp_dir - Get the path of compilation directory
48e0d153c6SMasami Hiramatsu  * @cu_die: a CU DIE
49e0d153c6SMasami Hiramatsu  *
50e0d153c6SMasami Hiramatsu  * Get the path of compilation directory of given @cu_die.
51e0d153c6SMasami Hiramatsu  * Since this depends on DW_AT_comp_dir, older gcc will not
52e0d153c6SMasami Hiramatsu  * embedded it. In that case, this returns NULL.
53e0d153c6SMasami Hiramatsu  */
54e0d153c6SMasami Hiramatsu const char *cu_get_comp_dir(Dwarf_Die *cu_die)
55e0d153c6SMasami Hiramatsu {
56e0d153c6SMasami Hiramatsu 	Dwarf_Attribute attr;
57e0d153c6SMasami Hiramatsu 	if (dwarf_attr(cu_die, DW_AT_comp_dir, &attr) == NULL)
58e0d153c6SMasami Hiramatsu 		return NULL;
59e0d153c6SMasami Hiramatsu 	return dwarf_formstring(&attr);
60e0d153c6SMasami Hiramatsu }
61e0d153c6SMasami Hiramatsu 
6257f95bf5SMasami Hiramatsu /* Unlike dwarf_getsrc_die(), cu_getsrc_die() only returns statement line */
6357f95bf5SMasami Hiramatsu static Dwarf_Line *cu_getsrc_die(Dwarf_Die *cu_die, Dwarf_Addr addr)
6457f95bf5SMasami Hiramatsu {
6557f95bf5SMasami Hiramatsu 	Dwarf_Addr laddr;
6657f95bf5SMasami Hiramatsu 	Dwarf_Lines *lines;
6757f95bf5SMasami Hiramatsu 	Dwarf_Line *line;
6857f95bf5SMasami Hiramatsu 	size_t nlines, l, u, n;
6957f95bf5SMasami Hiramatsu 	bool flag;
7057f95bf5SMasami Hiramatsu 
7157f95bf5SMasami Hiramatsu 	if (dwarf_getsrclines(cu_die, &lines, &nlines) != 0 ||
7257f95bf5SMasami Hiramatsu 	    nlines == 0)
7357f95bf5SMasami Hiramatsu 		return NULL;
7457f95bf5SMasami Hiramatsu 
7557f95bf5SMasami Hiramatsu 	/* Lines are sorted by address, use binary search */
7657f95bf5SMasami Hiramatsu 	l = 0; u = nlines - 1;
7757f95bf5SMasami Hiramatsu 	while (l < u) {
7857f95bf5SMasami Hiramatsu 		n = u - (u - l) / 2;
7957f95bf5SMasami Hiramatsu 		line = dwarf_onesrcline(lines, n);
8057f95bf5SMasami Hiramatsu 		if (!line || dwarf_lineaddr(line, &laddr) != 0)
8157f95bf5SMasami Hiramatsu 			return NULL;
8257f95bf5SMasami Hiramatsu 		if (addr < laddr)
8357f95bf5SMasami Hiramatsu 			u = n - 1;
8457f95bf5SMasami Hiramatsu 		else
8557f95bf5SMasami Hiramatsu 			l = n;
8657f95bf5SMasami Hiramatsu 	}
8757f95bf5SMasami Hiramatsu 	/* Going backward to find the lowest line */
8857f95bf5SMasami Hiramatsu 	do {
8957f95bf5SMasami Hiramatsu 		line = dwarf_onesrcline(lines, --l);
9057f95bf5SMasami Hiramatsu 		if (!line || dwarf_lineaddr(line, &laddr) != 0)
9157f95bf5SMasami Hiramatsu 			return NULL;
9257f95bf5SMasami Hiramatsu 	} while (laddr == addr);
9357f95bf5SMasami Hiramatsu 	l++;
9457f95bf5SMasami Hiramatsu 	/* Going foward to find the statement line */
9557f95bf5SMasami Hiramatsu 	do {
9657f95bf5SMasami Hiramatsu 		line = dwarf_onesrcline(lines, l++);
9757f95bf5SMasami Hiramatsu 		if (!line || dwarf_lineaddr(line, &laddr) != 0 ||
9857f95bf5SMasami Hiramatsu 		    dwarf_linebeginstatement(line, &flag) != 0)
9957f95bf5SMasami Hiramatsu 			return NULL;
10057f95bf5SMasami Hiramatsu 		if (laddr > addr)
10157f95bf5SMasami Hiramatsu 			return NULL;
10257f95bf5SMasami Hiramatsu 	} while (!flag);
10357f95bf5SMasami Hiramatsu 
10457f95bf5SMasami Hiramatsu 	return line;
10557f95bf5SMasami Hiramatsu }
10657f95bf5SMasami Hiramatsu 
107e0d153c6SMasami Hiramatsu /**
108e0d153c6SMasami Hiramatsu  * cu_find_lineinfo - Get a line number and file name for given address
109e0d153c6SMasami Hiramatsu  * @cu_die: a CU DIE
110e0d153c6SMasami Hiramatsu  * @addr: An address
111e0d153c6SMasami Hiramatsu  * @fname: a pointer which returns the file name string
112e0d153c6SMasami Hiramatsu  * @lineno: a pointer which returns the line number
113e0d153c6SMasami Hiramatsu  *
114e0d153c6SMasami Hiramatsu  * Find a line number and file name for @addr in @cu_die.
115e0d153c6SMasami Hiramatsu  */
116e0d153c6SMasami Hiramatsu int cu_find_lineinfo(Dwarf_Die *cu_die, unsigned long addr,
117e0d153c6SMasami Hiramatsu 		    const char **fname, int *lineno)
118e0d153c6SMasami Hiramatsu {
119e0d153c6SMasami Hiramatsu 	Dwarf_Line *line;
12057f95bf5SMasami Hiramatsu 	Dwarf_Die die_mem;
12157f95bf5SMasami Hiramatsu 	Dwarf_Addr faddr;
122e0d153c6SMasami Hiramatsu 
12357f95bf5SMasami Hiramatsu 	if (die_find_realfunc(cu_die, (Dwarf_Addr)addr, &die_mem)
12457f95bf5SMasami Hiramatsu 	    && die_entrypc(&die_mem, &faddr) == 0 &&
12557f95bf5SMasami Hiramatsu 	    faddr == addr) {
12657f95bf5SMasami Hiramatsu 		*fname = dwarf_decl_file(&die_mem);
12757f95bf5SMasami Hiramatsu 		dwarf_decl_line(&die_mem, lineno);
12857f95bf5SMasami Hiramatsu 		goto out;
12957f95bf5SMasami Hiramatsu 	}
13057f95bf5SMasami Hiramatsu 
13157f95bf5SMasami Hiramatsu 	line = cu_getsrc_die(cu_die, (Dwarf_Addr)addr);
13257f95bf5SMasami Hiramatsu 	if (line && dwarf_lineno(line, lineno) == 0) {
133e0d153c6SMasami Hiramatsu 		*fname = dwarf_linesrc(line, NULL, NULL);
134e0d153c6SMasami Hiramatsu 		if (!*fname)
135e0d153c6SMasami Hiramatsu 			/* line number is useless without filename */
136e0d153c6SMasami Hiramatsu 			*lineno = 0;
137e0d153c6SMasami Hiramatsu 	}
138e0d153c6SMasami Hiramatsu 
13957f95bf5SMasami Hiramatsu out:
140e0d153c6SMasami Hiramatsu 	return *lineno ?: -ENOENT;
141e0d153c6SMasami Hiramatsu }
142e0d153c6SMasami Hiramatsu 
143221d0611SMasami Hiramatsu static int __die_find_inline_cb(Dwarf_Die *die_mem, void *data);
144221d0611SMasami Hiramatsu 
145221d0611SMasami Hiramatsu /**
146221d0611SMasami Hiramatsu  * cu_walk_functions_at - Walk on function DIEs at given address
147221d0611SMasami Hiramatsu  * @cu_die: A CU DIE
148221d0611SMasami Hiramatsu  * @addr: An address
149221d0611SMasami Hiramatsu  * @callback: A callback which called with found DIEs
150221d0611SMasami Hiramatsu  * @data: A user data
151221d0611SMasami Hiramatsu  *
152221d0611SMasami Hiramatsu  * Walk on function DIEs at given @addr in @cu_die. Passed DIEs
153221d0611SMasami Hiramatsu  * should be subprogram or inlined-subroutines.
154221d0611SMasami Hiramatsu  */
155221d0611SMasami Hiramatsu int cu_walk_functions_at(Dwarf_Die *cu_die, Dwarf_Addr addr,
156221d0611SMasami Hiramatsu 		    int (*callback)(Dwarf_Die *, void *), void *data)
157221d0611SMasami Hiramatsu {
158221d0611SMasami Hiramatsu 	Dwarf_Die die_mem;
159221d0611SMasami Hiramatsu 	Dwarf_Die *sc_die;
160221d0611SMasami Hiramatsu 	int ret = -ENOENT;
161221d0611SMasami Hiramatsu 
162221d0611SMasami Hiramatsu 	/* Inlined function could be recursive. Trace it until fail */
163221d0611SMasami Hiramatsu 	for (sc_die = die_find_realfunc(cu_die, addr, &die_mem);
164221d0611SMasami Hiramatsu 	     sc_die != NULL;
165221d0611SMasami Hiramatsu 	     sc_die = die_find_child(sc_die, __die_find_inline_cb, &addr,
166221d0611SMasami Hiramatsu 				     &die_mem)) {
167221d0611SMasami Hiramatsu 		ret = callback(sc_die, data);
168221d0611SMasami Hiramatsu 		if (ret)
169221d0611SMasami Hiramatsu 			break;
170221d0611SMasami Hiramatsu 	}
171221d0611SMasami Hiramatsu 
172221d0611SMasami Hiramatsu 	return ret;
173221d0611SMasami Hiramatsu 
174221d0611SMasami Hiramatsu }
175221d0611SMasami Hiramatsu 
176e0d153c6SMasami Hiramatsu /**
177d5a00296SMasami Hiramatsu  * die_get_linkage_name - Get the linkage name of the object
178d5a00296SMasami Hiramatsu  * @dw_die: A DIE of the object
179d5a00296SMasami Hiramatsu  *
180d5a00296SMasami Hiramatsu  * Get the linkage name attiribute of given @dw_die.
181d5a00296SMasami Hiramatsu  * For C++ binary, the linkage name will be the mangled symbol.
182d5a00296SMasami Hiramatsu  */
183d5a00296SMasami Hiramatsu const char *die_get_linkage_name(Dwarf_Die *dw_die)
184d5a00296SMasami Hiramatsu {
185d5a00296SMasami Hiramatsu 	Dwarf_Attribute attr;
186d5a00296SMasami Hiramatsu 
187d5a00296SMasami Hiramatsu 	if (dwarf_attr_integrate(dw_die, DW_AT_linkage_name, &attr) == NULL)
188d5a00296SMasami Hiramatsu 		return NULL;
189d5a00296SMasami Hiramatsu 	return dwarf_formstring(&attr);
190d5a00296SMasami Hiramatsu }
191d5a00296SMasami Hiramatsu 
192d5a00296SMasami Hiramatsu /**
193e0d153c6SMasami Hiramatsu  * die_compare_name - Compare diename and tname
194e0d153c6SMasami Hiramatsu  * @dw_die: a DIE
195e0d153c6SMasami Hiramatsu  * @tname: a string of target name
196e0d153c6SMasami Hiramatsu  *
197e0d153c6SMasami Hiramatsu  * Compare the name of @dw_die and @tname. Return false if @dw_die has no name.
198e0d153c6SMasami Hiramatsu  */
199e0d153c6SMasami Hiramatsu bool die_compare_name(Dwarf_Die *dw_die, const char *tname)
200e0d153c6SMasami Hiramatsu {
201e0d153c6SMasami Hiramatsu 	const char *name;
2024c859351SMasami Hiramatsu 
203e0d153c6SMasami Hiramatsu 	name = dwarf_diename(dw_die);
204e0d153c6SMasami Hiramatsu 	return name ? (strcmp(tname, name) == 0) : false;
205e0d153c6SMasami Hiramatsu }
206e0d153c6SMasami Hiramatsu 
207e0d153c6SMasami Hiramatsu /**
208d5a00296SMasami Hiramatsu  * die_match_name - Match diename/linkage name and glob
2094c859351SMasami Hiramatsu  * @dw_die: a DIE
2104c859351SMasami Hiramatsu  * @glob: a string of target glob pattern
2114c859351SMasami Hiramatsu  *
2124c859351SMasami Hiramatsu  * Glob matching the name of @dw_die and @glob. Return false if matching fail.
213d5a00296SMasami Hiramatsu  * This also match linkage name.
2144c859351SMasami Hiramatsu  */
2154c859351SMasami Hiramatsu bool die_match_name(Dwarf_Die *dw_die, const char *glob)
2164c859351SMasami Hiramatsu {
2174c859351SMasami Hiramatsu 	const char *name;
2184c859351SMasami Hiramatsu 
2194c859351SMasami Hiramatsu 	name = dwarf_diename(dw_die);
220d5a00296SMasami Hiramatsu 	if (name && strglobmatch(name, glob))
221d5a00296SMasami Hiramatsu 		return true;
222d5a00296SMasami Hiramatsu 	/* fall back to check linkage name */
223d5a00296SMasami Hiramatsu 	name = die_get_linkage_name(dw_die);
224d5a00296SMasami Hiramatsu 	if (name && strglobmatch(name, glob))
225d5a00296SMasami Hiramatsu 		return true;
226d5a00296SMasami Hiramatsu 
227d5a00296SMasami Hiramatsu 	return false;
2284c859351SMasami Hiramatsu }
2294c859351SMasami Hiramatsu 
2304c859351SMasami Hiramatsu /**
231e0d153c6SMasami Hiramatsu  * die_get_call_lineno - Get callsite line number of inline-function instance
232e0d153c6SMasami Hiramatsu  * @in_die: a DIE of an inlined function instance
233e0d153c6SMasami Hiramatsu  *
234e0d153c6SMasami Hiramatsu  * Get call-site line number of @in_die. This means from where the inline
235e0d153c6SMasami Hiramatsu  * function is called.
236e0d153c6SMasami Hiramatsu  */
237e0d153c6SMasami Hiramatsu int die_get_call_lineno(Dwarf_Die *in_die)
238e0d153c6SMasami Hiramatsu {
239e0d153c6SMasami Hiramatsu 	Dwarf_Attribute attr;
240e0d153c6SMasami Hiramatsu 	Dwarf_Word ret;
241e0d153c6SMasami Hiramatsu 
242e0d153c6SMasami Hiramatsu 	if (!dwarf_attr(in_die, DW_AT_call_line, &attr))
243e0d153c6SMasami Hiramatsu 		return -ENOENT;
244e0d153c6SMasami Hiramatsu 
245e0d153c6SMasami Hiramatsu 	dwarf_formudata(&attr, &ret);
246e0d153c6SMasami Hiramatsu 	return (int)ret;
247e0d153c6SMasami Hiramatsu }
248e0d153c6SMasami Hiramatsu 
249e0d153c6SMasami Hiramatsu /**
250e0d153c6SMasami Hiramatsu  * die_get_type - Get type DIE
251e0d153c6SMasami Hiramatsu  * @vr_die: a DIE of a variable
252e0d153c6SMasami Hiramatsu  * @die_mem: where to store a type DIE
253e0d153c6SMasami Hiramatsu  *
254e0d153c6SMasami Hiramatsu  * Get a DIE of the type of given variable (@vr_die), and store
255e0d153c6SMasami Hiramatsu  * it to die_mem. Return NULL if fails to get a type DIE.
256e0d153c6SMasami Hiramatsu  */
257e0d153c6SMasami Hiramatsu Dwarf_Die *die_get_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
258e0d153c6SMasami Hiramatsu {
259e0d153c6SMasami Hiramatsu 	Dwarf_Attribute attr;
260e0d153c6SMasami Hiramatsu 
261e0d153c6SMasami Hiramatsu 	if (dwarf_attr_integrate(vr_die, DW_AT_type, &attr) &&
262e0d153c6SMasami Hiramatsu 	    dwarf_formref_die(&attr, die_mem))
263e0d153c6SMasami Hiramatsu 		return die_mem;
264e0d153c6SMasami Hiramatsu 	else
265e0d153c6SMasami Hiramatsu 		return NULL;
266e0d153c6SMasami Hiramatsu }
267e0d153c6SMasami Hiramatsu 
268e0d153c6SMasami Hiramatsu /* Get a type die, but skip qualifiers */
269e0d153c6SMasami Hiramatsu static Dwarf_Die *__die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
270e0d153c6SMasami Hiramatsu {
271e0d153c6SMasami Hiramatsu 	int tag;
272e0d153c6SMasami Hiramatsu 
273e0d153c6SMasami Hiramatsu 	do {
274e0d153c6SMasami Hiramatsu 		vr_die = die_get_type(vr_die, die_mem);
275e0d153c6SMasami Hiramatsu 		if (!vr_die)
276e0d153c6SMasami Hiramatsu 			break;
277e0d153c6SMasami Hiramatsu 		tag = dwarf_tag(vr_die);
278e0d153c6SMasami Hiramatsu 	} while (tag == DW_TAG_const_type ||
279e0d153c6SMasami Hiramatsu 		 tag == DW_TAG_restrict_type ||
280e0d153c6SMasami Hiramatsu 		 tag == DW_TAG_volatile_type ||
281e0d153c6SMasami Hiramatsu 		 tag == DW_TAG_shared_type);
282e0d153c6SMasami Hiramatsu 
283e0d153c6SMasami Hiramatsu 	return vr_die;
284e0d153c6SMasami Hiramatsu }
285e0d153c6SMasami Hiramatsu 
286e0d153c6SMasami Hiramatsu /**
287e0d153c6SMasami Hiramatsu  * die_get_real_type - Get a type die, but skip qualifiers and typedef
288e0d153c6SMasami Hiramatsu  * @vr_die: a DIE of a variable
289e0d153c6SMasami Hiramatsu  * @die_mem: where to store a type DIE
290e0d153c6SMasami Hiramatsu  *
291e0d153c6SMasami Hiramatsu  * Get a DIE of the type of given variable (@vr_die), and store
292e0d153c6SMasami Hiramatsu  * it to die_mem. Return NULL if fails to get a type DIE.
293e0d153c6SMasami Hiramatsu  * If the type is qualifiers (e.g. const) or typedef, this skips it
294e0d153c6SMasami Hiramatsu  * and tries to find real type (structure or basic types, e.g. int).
295e0d153c6SMasami Hiramatsu  */
296e0d153c6SMasami Hiramatsu Dwarf_Die *die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
297e0d153c6SMasami Hiramatsu {
298e0d153c6SMasami Hiramatsu 	do {
299e0d153c6SMasami Hiramatsu 		vr_die = __die_get_real_type(vr_die, die_mem);
300e0d153c6SMasami Hiramatsu 	} while (vr_die && dwarf_tag(vr_die) == DW_TAG_typedef);
301e0d153c6SMasami Hiramatsu 
302e0d153c6SMasami Hiramatsu 	return vr_die;
303e0d153c6SMasami Hiramatsu }
304e0d153c6SMasami Hiramatsu 
305e0d153c6SMasami Hiramatsu /* Get attribute and translate it as a udata */
306e0d153c6SMasami Hiramatsu static int die_get_attr_udata(Dwarf_Die *tp_die, unsigned int attr_name,
307e0d153c6SMasami Hiramatsu 			      Dwarf_Word *result)
308e0d153c6SMasami Hiramatsu {
309e0d153c6SMasami Hiramatsu 	Dwarf_Attribute attr;
310e0d153c6SMasami Hiramatsu 
311e0d153c6SMasami Hiramatsu 	if (dwarf_attr(tp_die, attr_name, &attr) == NULL ||
312e0d153c6SMasami Hiramatsu 	    dwarf_formudata(&attr, result) != 0)
313e0d153c6SMasami Hiramatsu 		return -ENOENT;
314e0d153c6SMasami Hiramatsu 
315e0d153c6SMasami Hiramatsu 	return 0;
316e0d153c6SMasami Hiramatsu }
317e0d153c6SMasami Hiramatsu 
318b0e9cb28SMasami Hiramatsu /* Get attribute and translate it as a sdata */
319b0e9cb28SMasami Hiramatsu static int die_get_attr_sdata(Dwarf_Die *tp_die, unsigned int attr_name,
320b0e9cb28SMasami Hiramatsu 			      Dwarf_Sword *result)
321b0e9cb28SMasami Hiramatsu {
322b0e9cb28SMasami Hiramatsu 	Dwarf_Attribute attr;
323b0e9cb28SMasami Hiramatsu 
324b0e9cb28SMasami Hiramatsu 	if (dwarf_attr(tp_die, attr_name, &attr) == NULL ||
325b0e9cb28SMasami Hiramatsu 	    dwarf_formsdata(&attr, result) != 0)
326b0e9cb28SMasami Hiramatsu 		return -ENOENT;
327b0e9cb28SMasami Hiramatsu 
328b0e9cb28SMasami Hiramatsu 	return 0;
329b0e9cb28SMasami Hiramatsu }
330b0e9cb28SMasami Hiramatsu 
331e0d153c6SMasami Hiramatsu /**
332e0d153c6SMasami Hiramatsu  * die_is_signed_type - Check whether a type DIE is signed or not
333e0d153c6SMasami Hiramatsu  * @tp_die: a DIE of a type
334e0d153c6SMasami Hiramatsu  *
335e0d153c6SMasami Hiramatsu  * Get the encoding of @tp_die and return true if the encoding
336e0d153c6SMasami Hiramatsu  * is signed.
337e0d153c6SMasami Hiramatsu  */
338e0d153c6SMasami Hiramatsu bool die_is_signed_type(Dwarf_Die *tp_die)
339e0d153c6SMasami Hiramatsu {
340e0d153c6SMasami Hiramatsu 	Dwarf_Word ret;
341e0d153c6SMasami Hiramatsu 
342e0d153c6SMasami Hiramatsu 	if (die_get_attr_udata(tp_die, DW_AT_encoding, &ret))
343e0d153c6SMasami Hiramatsu 		return false;
344e0d153c6SMasami Hiramatsu 
345e0d153c6SMasami Hiramatsu 	return (ret == DW_ATE_signed_char || ret == DW_ATE_signed ||
346e0d153c6SMasami Hiramatsu 		ret == DW_ATE_signed_fixed);
347e0d153c6SMasami Hiramatsu }
348e0d153c6SMasami Hiramatsu 
349e0d153c6SMasami Hiramatsu /**
3500dbb1cacSMasami Hiramatsu  * die_is_func_def - Ensure that this DIE is a subprogram and definition
3510dbb1cacSMasami Hiramatsu  * @dw_die: a DIE
3520dbb1cacSMasami Hiramatsu  *
3530dbb1cacSMasami Hiramatsu  * Ensure that this DIE is a subprogram and NOT a declaration. This
3540dbb1cacSMasami Hiramatsu  * returns true if @dw_die is a function definition.
3550dbb1cacSMasami Hiramatsu  **/
3560dbb1cacSMasami Hiramatsu bool die_is_func_def(Dwarf_Die *dw_die)
3570dbb1cacSMasami Hiramatsu {
3580dbb1cacSMasami Hiramatsu 	Dwarf_Attribute attr;
3590dbb1cacSMasami Hiramatsu 
3600dbb1cacSMasami Hiramatsu 	return (dwarf_tag(dw_die) == DW_TAG_subprogram &&
3610dbb1cacSMasami Hiramatsu 		dwarf_attr(dw_die, DW_AT_declaration, &attr) == NULL);
3620dbb1cacSMasami Hiramatsu }
3630dbb1cacSMasami Hiramatsu 
3640dbb1cacSMasami Hiramatsu /**
36591e2f539SMasami Hiramatsu  * die_entrypc - Returns entry PC (the lowest address) of a DIE
36691e2f539SMasami Hiramatsu  * @dw_die: a DIE
36791e2f539SMasami Hiramatsu  * @addr: where to store entry PC
36891e2f539SMasami Hiramatsu  *
36991e2f539SMasami Hiramatsu  * Since dwarf_entrypc() does not return entry PC if the DIE has only address
37091e2f539SMasami Hiramatsu  * range, we have to use this to retrieve the lowest address from the address
37191e2f539SMasami Hiramatsu  * range attribute.
37291e2f539SMasami Hiramatsu  */
37391e2f539SMasami Hiramatsu int die_entrypc(Dwarf_Die *dw_die, Dwarf_Addr *addr)
37491e2f539SMasami Hiramatsu {
37591e2f539SMasami Hiramatsu 	Dwarf_Addr base, end;
376*ab4200c1SMasami Hiramatsu 	Dwarf_Attribute attr;
37791e2f539SMasami Hiramatsu 
37891e2f539SMasami Hiramatsu 	if (!addr)
37991e2f539SMasami Hiramatsu 		return -EINVAL;
38091e2f539SMasami Hiramatsu 
38191e2f539SMasami Hiramatsu 	if (dwarf_entrypc(dw_die, addr) == 0)
38291e2f539SMasami Hiramatsu 		return 0;
38391e2f539SMasami Hiramatsu 
384*ab4200c1SMasami Hiramatsu 	/*
385*ab4200c1SMasami Hiramatsu 	 *  Since the dwarf_ranges() will return 0 if there is no
386*ab4200c1SMasami Hiramatsu 	 * DW_AT_ranges attribute, we should check it first.
387*ab4200c1SMasami Hiramatsu 	 */
388*ab4200c1SMasami Hiramatsu 	if (!dwarf_attr(dw_die, DW_AT_ranges, &attr))
389*ab4200c1SMasami Hiramatsu 		return -ENOENT;
390*ab4200c1SMasami Hiramatsu 
39191e2f539SMasami Hiramatsu 	return dwarf_ranges(dw_die, 0, &base, addr, &end) < 0 ? -ENOENT : 0;
39291e2f539SMasami Hiramatsu }
39391e2f539SMasami Hiramatsu 
39491e2f539SMasami Hiramatsu /**
395e1ecbbc3SMasami Hiramatsu  * die_is_func_instance - Ensure that this DIE is an instance of a subprogram
396e1ecbbc3SMasami Hiramatsu  * @dw_die: a DIE
397e1ecbbc3SMasami Hiramatsu  *
398e1ecbbc3SMasami Hiramatsu  * Ensure that this DIE is an instance (which has an entry address).
399da6cb952SMasami Hiramatsu  * This returns true if @dw_die is a function instance. If not, the @dw_die
400da6cb952SMasami Hiramatsu  * must be a prototype. You can use die_walk_instances() to find actual
401da6cb952SMasami Hiramatsu  * instances.
402e1ecbbc3SMasami Hiramatsu  **/
403e1ecbbc3SMasami Hiramatsu bool die_is_func_instance(Dwarf_Die *dw_die)
404e1ecbbc3SMasami Hiramatsu {
405e1ecbbc3SMasami Hiramatsu 	Dwarf_Addr tmp;
406b77afa1fSMasami Hiramatsu 	Dwarf_Attribute attr_mem;
407da6cb952SMasami Hiramatsu 	int tag = dwarf_tag(dw_die);
408e1ecbbc3SMasami Hiramatsu 
409da6cb952SMasami Hiramatsu 	if (tag != DW_TAG_subprogram &&
410da6cb952SMasami Hiramatsu 	    tag != DW_TAG_inlined_subroutine)
411da6cb952SMasami Hiramatsu 		return false;
412da6cb952SMasami Hiramatsu 
413da6cb952SMasami Hiramatsu 	return dwarf_entrypc(dw_die, &tmp) == 0 ||
414da6cb952SMasami Hiramatsu 		dwarf_attr(dw_die, DW_AT_ranges, &attr_mem) != NULL;
415e1ecbbc3SMasami Hiramatsu }
416b77afa1fSMasami Hiramatsu 
417e1ecbbc3SMasami Hiramatsu /**
418e0d153c6SMasami Hiramatsu  * die_get_data_member_location - Get the data-member offset
419e0d153c6SMasami Hiramatsu  * @mb_die: a DIE of a member of a data structure
420e0d153c6SMasami Hiramatsu  * @offs: The offset of the member in the data structure
421e0d153c6SMasami Hiramatsu  *
422e0d153c6SMasami Hiramatsu  * Get the offset of @mb_die in the data structure including @mb_die, and
423e0d153c6SMasami Hiramatsu  * stores result offset to @offs. If any error occurs this returns errno.
424e0d153c6SMasami Hiramatsu  */
425e0d153c6SMasami Hiramatsu int die_get_data_member_location(Dwarf_Die *mb_die, Dwarf_Word *offs)
426e0d153c6SMasami Hiramatsu {
427e0d153c6SMasami Hiramatsu 	Dwarf_Attribute attr;
428e0d153c6SMasami Hiramatsu 	Dwarf_Op *expr;
429e0d153c6SMasami Hiramatsu 	size_t nexpr;
430e0d153c6SMasami Hiramatsu 	int ret;
431e0d153c6SMasami Hiramatsu 
432e0d153c6SMasami Hiramatsu 	if (dwarf_attr(mb_die, DW_AT_data_member_location, &attr) == NULL)
433e0d153c6SMasami Hiramatsu 		return -ENOENT;
434e0d153c6SMasami Hiramatsu 
435e0d153c6SMasami Hiramatsu 	if (dwarf_formudata(&attr, offs) != 0) {
436e0d153c6SMasami Hiramatsu 		/* DW_AT_data_member_location should be DW_OP_plus_uconst */
437e0d153c6SMasami Hiramatsu 		ret = dwarf_getlocation(&attr, &expr, &nexpr);
438e0d153c6SMasami Hiramatsu 		if (ret < 0 || nexpr == 0)
439e0d153c6SMasami Hiramatsu 			return -ENOENT;
440e0d153c6SMasami Hiramatsu 
441e0d153c6SMasami Hiramatsu 		if (expr[0].atom != DW_OP_plus_uconst || nexpr != 1) {
442e0d153c6SMasami Hiramatsu 			pr_debug("Unable to get offset:Unexpected OP %x (%zd)\n",
443e0d153c6SMasami Hiramatsu 				 expr[0].atom, nexpr);
444e0d153c6SMasami Hiramatsu 			return -ENOTSUP;
445e0d153c6SMasami Hiramatsu 		}
446e0d153c6SMasami Hiramatsu 		*offs = (Dwarf_Word)expr[0].number;
447e0d153c6SMasami Hiramatsu 	}
448e0d153c6SMasami Hiramatsu 	return 0;
449e0d153c6SMasami Hiramatsu }
450e0d153c6SMasami Hiramatsu 
451b0e9cb28SMasami Hiramatsu /* Get the call file index number in CU DIE */
452b0e9cb28SMasami Hiramatsu static int die_get_call_fileno(Dwarf_Die *in_die)
453b0e9cb28SMasami Hiramatsu {
454b0e9cb28SMasami Hiramatsu 	Dwarf_Sword idx;
455b0e9cb28SMasami Hiramatsu 
456b0e9cb28SMasami Hiramatsu 	if (die_get_attr_sdata(in_die, DW_AT_call_file, &idx) == 0)
457b0e9cb28SMasami Hiramatsu 		return (int)idx;
458b0e9cb28SMasami Hiramatsu 	else
459b0e9cb28SMasami Hiramatsu 		return -ENOENT;
460b0e9cb28SMasami Hiramatsu }
461b0e9cb28SMasami Hiramatsu 
4623f4460a2SMasami Hiramatsu /* Get the declared file index number in CU DIE */
4633f4460a2SMasami Hiramatsu static int die_get_decl_fileno(Dwarf_Die *pdie)
4643f4460a2SMasami Hiramatsu {
4653f4460a2SMasami Hiramatsu 	Dwarf_Sword idx;
4663f4460a2SMasami Hiramatsu 
4673f4460a2SMasami Hiramatsu 	if (die_get_attr_sdata(pdie, DW_AT_decl_file, &idx) == 0)
4683f4460a2SMasami Hiramatsu 		return (int)idx;
4693f4460a2SMasami Hiramatsu 	else
4703f4460a2SMasami Hiramatsu 		return -ENOENT;
4713f4460a2SMasami Hiramatsu }
4723f4460a2SMasami Hiramatsu 
473b0e9cb28SMasami Hiramatsu /**
474b0e9cb28SMasami Hiramatsu  * die_get_call_file - Get callsite file name of inlined function instance
475b0e9cb28SMasami Hiramatsu  * @in_die: a DIE of an inlined function instance
476b0e9cb28SMasami Hiramatsu  *
477b0e9cb28SMasami Hiramatsu  * Get call-site file name of @in_die. This means from which file the inline
478b0e9cb28SMasami Hiramatsu  * function is called.
479b0e9cb28SMasami Hiramatsu  */
480b0e9cb28SMasami Hiramatsu const char *die_get_call_file(Dwarf_Die *in_die)
481b0e9cb28SMasami Hiramatsu {
482b0e9cb28SMasami Hiramatsu 	Dwarf_Die cu_die;
483b0e9cb28SMasami Hiramatsu 	Dwarf_Files *files;
484b0e9cb28SMasami Hiramatsu 	int idx;
485b0e9cb28SMasami Hiramatsu 
486b0e9cb28SMasami Hiramatsu 	idx = die_get_call_fileno(in_die);
487b0e9cb28SMasami Hiramatsu 	if (idx < 0 || !dwarf_diecu(in_die, &cu_die, NULL, NULL) ||
488b0e9cb28SMasami Hiramatsu 	    dwarf_getsrcfiles(&cu_die, &files, NULL) != 0)
489b0e9cb28SMasami Hiramatsu 		return NULL;
490b0e9cb28SMasami Hiramatsu 
491b0e9cb28SMasami Hiramatsu 	return dwarf_filesrc(files, idx, NULL, NULL);
492b0e9cb28SMasami Hiramatsu }
493b0e9cb28SMasami Hiramatsu 
494b0e9cb28SMasami Hiramatsu 
495e0d153c6SMasami Hiramatsu /**
496e0d153c6SMasami Hiramatsu  * die_find_child - Generic DIE search function in DIE tree
497e0d153c6SMasami Hiramatsu  * @rt_die: a root DIE
498e0d153c6SMasami Hiramatsu  * @callback: a callback function
499e0d153c6SMasami Hiramatsu  * @data: a user data passed to the callback function
500e0d153c6SMasami Hiramatsu  * @die_mem: a buffer for result DIE
501e0d153c6SMasami Hiramatsu  *
502e0d153c6SMasami Hiramatsu  * Trace DIE tree from @rt_die and call @callback for each child DIE.
503e0d153c6SMasami Hiramatsu  * If @callback returns DIE_FIND_CB_END, this stores the DIE into
504e0d153c6SMasami Hiramatsu  * @die_mem and returns it. If @callback returns DIE_FIND_CB_CONTINUE,
505e0d153c6SMasami Hiramatsu  * this continues to trace the tree. Optionally, @callback can return
506e0d153c6SMasami Hiramatsu  * DIE_FIND_CB_CHILD and DIE_FIND_CB_SIBLING, those means trace only
507e0d153c6SMasami Hiramatsu  * the children and trace only the siblings respectively.
508e0d153c6SMasami Hiramatsu  * Returns NULL if @callback can't find any appropriate DIE.
509e0d153c6SMasami Hiramatsu  */
510e0d153c6SMasami Hiramatsu Dwarf_Die *die_find_child(Dwarf_Die *rt_die,
511e0d153c6SMasami Hiramatsu 			  int (*callback)(Dwarf_Die *, void *),
512e0d153c6SMasami Hiramatsu 			  void *data, Dwarf_Die *die_mem)
513e0d153c6SMasami Hiramatsu {
514e0d153c6SMasami Hiramatsu 	Dwarf_Die child_die;
515e0d153c6SMasami Hiramatsu 	int ret;
516e0d153c6SMasami Hiramatsu 
517e0d153c6SMasami Hiramatsu 	ret = dwarf_child(rt_die, die_mem);
518e0d153c6SMasami Hiramatsu 	if (ret != 0)
519e0d153c6SMasami Hiramatsu 		return NULL;
520e0d153c6SMasami Hiramatsu 
521e0d153c6SMasami Hiramatsu 	do {
522e0d153c6SMasami Hiramatsu 		ret = callback(die_mem, data);
523e0d153c6SMasami Hiramatsu 		if (ret == DIE_FIND_CB_END)
524e0d153c6SMasami Hiramatsu 			return die_mem;
525e0d153c6SMasami Hiramatsu 
526e0d153c6SMasami Hiramatsu 		if ((ret & DIE_FIND_CB_CHILD) &&
527e0d153c6SMasami Hiramatsu 		    die_find_child(die_mem, callback, data, &child_die)) {
528e0d153c6SMasami Hiramatsu 			memcpy(die_mem, &child_die, sizeof(Dwarf_Die));
529e0d153c6SMasami Hiramatsu 			return die_mem;
530e0d153c6SMasami Hiramatsu 		}
531e0d153c6SMasami Hiramatsu 	} while ((ret & DIE_FIND_CB_SIBLING) &&
532e0d153c6SMasami Hiramatsu 		 dwarf_siblingof(die_mem, die_mem) == 0);
533e0d153c6SMasami Hiramatsu 
534e0d153c6SMasami Hiramatsu 	return NULL;
535e0d153c6SMasami Hiramatsu }
536e0d153c6SMasami Hiramatsu 
537e0d153c6SMasami Hiramatsu struct __addr_die_search_param {
538e0d153c6SMasami Hiramatsu 	Dwarf_Addr	addr;
539e0d153c6SMasami Hiramatsu 	Dwarf_Die	*die_mem;
540e0d153c6SMasami Hiramatsu };
541e0d153c6SMasami Hiramatsu 
542d4c537e6SNaveen N. Rao static int __die_search_func_tail_cb(Dwarf_Die *fn_die, void *data)
543d4c537e6SNaveen N. Rao {
544d4c537e6SNaveen N. Rao 	struct __addr_die_search_param *ad = data;
545d4c537e6SNaveen N. Rao 	Dwarf_Addr addr = 0;
546d4c537e6SNaveen N. Rao 
547d4c537e6SNaveen N. Rao 	if (dwarf_tag(fn_die) == DW_TAG_subprogram &&
548d4c537e6SNaveen N. Rao 	    !dwarf_highpc(fn_die, &addr) &&
549d4c537e6SNaveen N. Rao 	    addr == ad->addr) {
550d4c537e6SNaveen N. Rao 		memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die));
551d4c537e6SNaveen N. Rao 		return DWARF_CB_ABORT;
552d4c537e6SNaveen N. Rao 	}
553d4c537e6SNaveen N. Rao 	return DWARF_CB_OK;
554d4c537e6SNaveen N. Rao }
555d4c537e6SNaveen N. Rao 
556d4c537e6SNaveen N. Rao /**
557d4c537e6SNaveen N. Rao  * die_find_tailfunc - Search for a non-inlined function with tail call at
558d4c537e6SNaveen N. Rao  * given address
559d4c537e6SNaveen N. Rao  * @cu_die: a CU DIE which including @addr
560d4c537e6SNaveen N. Rao  * @addr: target address
561d4c537e6SNaveen N. Rao  * @die_mem: a buffer for result DIE
562d4c537e6SNaveen N. Rao  *
563d4c537e6SNaveen N. Rao  * Search for a non-inlined function DIE with tail call at @addr. Stores the
564d4c537e6SNaveen N. Rao  * DIE to @die_mem and returns it if found. Returns NULL if failed.
565d4c537e6SNaveen N. Rao  */
566d4c537e6SNaveen N. Rao Dwarf_Die *die_find_tailfunc(Dwarf_Die *cu_die, Dwarf_Addr addr,
567d4c537e6SNaveen N. Rao 				    Dwarf_Die *die_mem)
568d4c537e6SNaveen N. Rao {
569d4c537e6SNaveen N. Rao 	struct __addr_die_search_param ad;
570d4c537e6SNaveen N. Rao 	ad.addr = addr;
571d4c537e6SNaveen N. Rao 	ad.die_mem = die_mem;
572d4c537e6SNaveen N. Rao 	/* dwarf_getscopes can't find subprogram. */
573d4c537e6SNaveen N. Rao 	if (!dwarf_getfuncs(cu_die, __die_search_func_tail_cb, &ad, 0))
574d4c537e6SNaveen N. Rao 		return NULL;
575d4c537e6SNaveen N. Rao 	else
576d4c537e6SNaveen N. Rao 		return die_mem;
577d4c537e6SNaveen N. Rao }
578d4c537e6SNaveen N. Rao 
579e0d153c6SMasami Hiramatsu /* die_find callback for non-inlined function search */
580e0d153c6SMasami Hiramatsu static int __die_search_func_cb(Dwarf_Die *fn_die, void *data)
581e0d153c6SMasami Hiramatsu {
582e0d153c6SMasami Hiramatsu 	struct __addr_die_search_param *ad = data;
583e0d153c6SMasami Hiramatsu 
5840dbb1cacSMasami Hiramatsu 	/*
5850dbb1cacSMasami Hiramatsu 	 * Since a declaration entry doesn't has given pc, this always returns
5860dbb1cacSMasami Hiramatsu 	 * function definition entry.
5870dbb1cacSMasami Hiramatsu 	 */
588e0d153c6SMasami Hiramatsu 	if (dwarf_tag(fn_die) == DW_TAG_subprogram &&
589e0d153c6SMasami Hiramatsu 	    dwarf_haspc(fn_die, ad->addr)) {
590e0d153c6SMasami Hiramatsu 		memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die));
591e0d153c6SMasami Hiramatsu 		return DWARF_CB_ABORT;
592e0d153c6SMasami Hiramatsu 	}
593e0d153c6SMasami Hiramatsu 	return DWARF_CB_OK;
594e0d153c6SMasami Hiramatsu }
595e0d153c6SMasami Hiramatsu 
596e0d153c6SMasami Hiramatsu /**
597e0d153c6SMasami Hiramatsu  * die_find_realfunc - Search a non-inlined function at given address
598e0d153c6SMasami Hiramatsu  * @cu_die: a CU DIE which including @addr
599e0d153c6SMasami Hiramatsu  * @addr: target address
600e0d153c6SMasami Hiramatsu  * @die_mem: a buffer for result DIE
601e0d153c6SMasami Hiramatsu  *
602e0d153c6SMasami Hiramatsu  * Search a non-inlined function DIE which includes @addr. Stores the
603e08cfd4bSMasami Hiramatsu  * DIE to @die_mem and returns it if found. Returns NULL if failed.
604e0d153c6SMasami Hiramatsu  */
605e0d153c6SMasami Hiramatsu Dwarf_Die *die_find_realfunc(Dwarf_Die *cu_die, Dwarf_Addr addr,
606e0d153c6SMasami Hiramatsu 				    Dwarf_Die *die_mem)
607e0d153c6SMasami Hiramatsu {
608e0d153c6SMasami Hiramatsu 	struct __addr_die_search_param ad;
609e0d153c6SMasami Hiramatsu 	ad.addr = addr;
610e0d153c6SMasami Hiramatsu 	ad.die_mem = die_mem;
611e0d153c6SMasami Hiramatsu 	/* dwarf_getscopes can't find subprogram. */
612e0d153c6SMasami Hiramatsu 	if (!dwarf_getfuncs(cu_die, __die_search_func_cb, &ad, 0))
613e0d153c6SMasami Hiramatsu 		return NULL;
614e0d153c6SMasami Hiramatsu 	else
615e0d153c6SMasami Hiramatsu 		return die_mem;
616e0d153c6SMasami Hiramatsu }
617e0d153c6SMasami Hiramatsu 
618e0d153c6SMasami Hiramatsu /* die_find callback for inline function search */
619e0d153c6SMasami Hiramatsu static int __die_find_inline_cb(Dwarf_Die *die_mem, void *data)
620e0d153c6SMasami Hiramatsu {
621e0d153c6SMasami Hiramatsu 	Dwarf_Addr *addr = data;
622e0d153c6SMasami Hiramatsu 
623e0d153c6SMasami Hiramatsu 	if (dwarf_tag(die_mem) == DW_TAG_inlined_subroutine &&
624e0d153c6SMasami Hiramatsu 	    dwarf_haspc(die_mem, *addr))
625e0d153c6SMasami Hiramatsu 		return DIE_FIND_CB_END;
626e0d153c6SMasami Hiramatsu 
627e0d153c6SMasami Hiramatsu 	return DIE_FIND_CB_CONTINUE;
628e0d153c6SMasami Hiramatsu }
629e0d153c6SMasami Hiramatsu 
630e0d153c6SMasami Hiramatsu /**
631e08cfd4bSMasami Hiramatsu  * die_find_top_inlinefunc - Search the top inlined function at given address
632e08cfd4bSMasami Hiramatsu  * @sp_die: a subprogram DIE which including @addr
633e0d153c6SMasami Hiramatsu  * @addr: target address
634e0d153c6SMasami Hiramatsu  * @die_mem: a buffer for result DIE
635e0d153c6SMasami Hiramatsu  *
636e0d153c6SMasami Hiramatsu  * Search an inlined function DIE which includes @addr. Stores the
637e08cfd4bSMasami Hiramatsu  * DIE to @die_mem and returns it if found. Returns NULL if failed.
638e08cfd4bSMasami Hiramatsu  * Even if several inlined functions are expanded recursively, this
639e08cfd4bSMasami Hiramatsu  * doesn't trace it down, and returns the topmost one.
640e08cfd4bSMasami Hiramatsu  */
641e08cfd4bSMasami Hiramatsu Dwarf_Die *die_find_top_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
642e08cfd4bSMasami Hiramatsu 				   Dwarf_Die *die_mem)
643e08cfd4bSMasami Hiramatsu {
644e08cfd4bSMasami Hiramatsu 	return die_find_child(sp_die, __die_find_inline_cb, &addr, die_mem);
645e08cfd4bSMasami Hiramatsu }
646e08cfd4bSMasami Hiramatsu 
647e08cfd4bSMasami Hiramatsu /**
648e08cfd4bSMasami Hiramatsu  * die_find_inlinefunc - Search an inlined function at given address
649e08cfd4bSMasami Hiramatsu  * @sp_die: a subprogram DIE which including @addr
650e08cfd4bSMasami Hiramatsu  * @addr: target address
651e08cfd4bSMasami Hiramatsu  * @die_mem: a buffer for result DIE
652e08cfd4bSMasami Hiramatsu  *
653e08cfd4bSMasami Hiramatsu  * Search an inlined function DIE which includes @addr. Stores the
654e08cfd4bSMasami Hiramatsu  * DIE to @die_mem and returns it if found. Returns NULL if failed.
655e0d153c6SMasami Hiramatsu  * If several inlined functions are expanded recursively, this trace
656e08cfd4bSMasami Hiramatsu  * it down and returns deepest one.
657e0d153c6SMasami Hiramatsu  */
658e0d153c6SMasami Hiramatsu Dwarf_Die *die_find_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
659e0d153c6SMasami Hiramatsu 			       Dwarf_Die *die_mem)
660e0d153c6SMasami Hiramatsu {
661e0d153c6SMasami Hiramatsu 	Dwarf_Die tmp_die;
662e0d153c6SMasami Hiramatsu 
663e0d153c6SMasami Hiramatsu 	sp_die = die_find_child(sp_die, __die_find_inline_cb, &addr, &tmp_die);
664e0d153c6SMasami Hiramatsu 	if (!sp_die)
665e0d153c6SMasami Hiramatsu 		return NULL;
666e0d153c6SMasami Hiramatsu 
667e0d153c6SMasami Hiramatsu 	/* Inlined function could be recursive. Trace it until fail */
668e0d153c6SMasami Hiramatsu 	while (sp_die) {
669e0d153c6SMasami Hiramatsu 		memcpy(die_mem, sp_die, sizeof(Dwarf_Die));
670e0d153c6SMasami Hiramatsu 		sp_die = die_find_child(sp_die, __die_find_inline_cb, &addr,
671e0d153c6SMasami Hiramatsu 					&tmp_die);
672e0d153c6SMasami Hiramatsu 	}
673e0d153c6SMasami Hiramatsu 
674e0d153c6SMasami Hiramatsu 	return die_mem;
675e0d153c6SMasami Hiramatsu }
676e0d153c6SMasami Hiramatsu 
677db0d2c64SMasami Hiramatsu struct __instance_walk_param {
678db0d2c64SMasami Hiramatsu 	void    *addr;
679db0d2c64SMasami Hiramatsu 	int	(*callback)(Dwarf_Die *, void *);
680db0d2c64SMasami Hiramatsu 	void    *data;
681db0d2c64SMasami Hiramatsu 	int	retval;
682db0d2c64SMasami Hiramatsu };
683db0d2c64SMasami Hiramatsu 
684db0d2c64SMasami Hiramatsu static int __die_walk_instances_cb(Dwarf_Die *inst, void *data)
685db0d2c64SMasami Hiramatsu {
686db0d2c64SMasami Hiramatsu 	struct __instance_walk_param *iwp = data;
687db0d2c64SMasami Hiramatsu 	Dwarf_Attribute attr_mem;
688db0d2c64SMasami Hiramatsu 	Dwarf_Die origin_mem;
689db0d2c64SMasami Hiramatsu 	Dwarf_Attribute *attr;
690db0d2c64SMasami Hiramatsu 	Dwarf_Die *origin;
6913f4460a2SMasami Hiramatsu 	int tmp;
692db0d2c64SMasami Hiramatsu 
693da6cb952SMasami Hiramatsu 	if (!die_is_func_instance(inst))
694da6cb952SMasami Hiramatsu 		return DIE_FIND_CB_CONTINUE;
695da6cb952SMasami Hiramatsu 
696db0d2c64SMasami Hiramatsu 	attr = dwarf_attr(inst, DW_AT_abstract_origin, &attr_mem);
697db0d2c64SMasami Hiramatsu 	if (attr == NULL)
698db0d2c64SMasami Hiramatsu 		return DIE_FIND_CB_CONTINUE;
699db0d2c64SMasami Hiramatsu 
700db0d2c64SMasami Hiramatsu 	origin = dwarf_formref_die(attr, &origin_mem);
701db0d2c64SMasami Hiramatsu 	if (origin == NULL || origin->addr != iwp->addr)
702db0d2c64SMasami Hiramatsu 		return DIE_FIND_CB_CONTINUE;
703db0d2c64SMasami Hiramatsu 
7043f4460a2SMasami Hiramatsu 	/* Ignore redundant instances */
7053f4460a2SMasami Hiramatsu 	if (dwarf_tag(inst) == DW_TAG_inlined_subroutine) {
7063f4460a2SMasami Hiramatsu 		dwarf_decl_line(origin, &tmp);
7073f4460a2SMasami Hiramatsu 		if (die_get_call_lineno(inst) == tmp) {
7083f4460a2SMasami Hiramatsu 			tmp = die_get_decl_fileno(origin);
7093f4460a2SMasami Hiramatsu 			if (die_get_call_fileno(inst) == tmp)
7103f4460a2SMasami Hiramatsu 				return DIE_FIND_CB_CONTINUE;
7113f4460a2SMasami Hiramatsu 		}
7123f4460a2SMasami Hiramatsu 	}
7133f4460a2SMasami Hiramatsu 
714db0d2c64SMasami Hiramatsu 	iwp->retval = iwp->callback(inst, iwp->data);
715db0d2c64SMasami Hiramatsu 
716db0d2c64SMasami Hiramatsu 	return (iwp->retval) ? DIE_FIND_CB_END : DIE_FIND_CB_CONTINUE;
717db0d2c64SMasami Hiramatsu }
718db0d2c64SMasami Hiramatsu 
719db0d2c64SMasami Hiramatsu /**
720db0d2c64SMasami Hiramatsu  * die_walk_instances - Walk on instances of given DIE
721db0d2c64SMasami Hiramatsu  * @or_die: an abstract original DIE
722db0d2c64SMasami Hiramatsu  * @callback: a callback function which is called with instance DIE
723db0d2c64SMasami Hiramatsu  * @data: user data
724db0d2c64SMasami Hiramatsu  *
725db0d2c64SMasami Hiramatsu  * Walk on the instances of give @in_die. @in_die must be an inlined function
726db0d2c64SMasami Hiramatsu  * declartion. This returns the return value of @callback if it returns
727db0d2c64SMasami Hiramatsu  * non-zero value, or -ENOENT if there is no instance.
728db0d2c64SMasami Hiramatsu  */
729db0d2c64SMasami Hiramatsu int die_walk_instances(Dwarf_Die *or_die, int (*callback)(Dwarf_Die *, void *),
730db0d2c64SMasami Hiramatsu 		       void *data)
731db0d2c64SMasami Hiramatsu {
732db0d2c64SMasami Hiramatsu 	Dwarf_Die cu_die;
733db0d2c64SMasami Hiramatsu 	Dwarf_Die die_mem;
734db0d2c64SMasami Hiramatsu 	struct __instance_walk_param iwp = {
735db0d2c64SMasami Hiramatsu 		.addr = or_die->addr,
736db0d2c64SMasami Hiramatsu 		.callback = callback,
737db0d2c64SMasami Hiramatsu 		.data = data,
738db0d2c64SMasami Hiramatsu 		.retval = -ENOENT,
739db0d2c64SMasami Hiramatsu 	};
740db0d2c64SMasami Hiramatsu 
741db0d2c64SMasami Hiramatsu 	if (dwarf_diecu(or_die, &cu_die, NULL, NULL) == NULL)
742db0d2c64SMasami Hiramatsu 		return -ENOENT;
743db0d2c64SMasami Hiramatsu 
744db0d2c64SMasami Hiramatsu 	die_find_child(&cu_die, __die_walk_instances_cb, &iwp, &die_mem);
745db0d2c64SMasami Hiramatsu 
746db0d2c64SMasami Hiramatsu 	return iwp.retval;
747db0d2c64SMasami Hiramatsu }
748db0d2c64SMasami Hiramatsu 
749e0d153c6SMasami Hiramatsu /* Line walker internal parameters */
750e0d153c6SMasami Hiramatsu struct __line_walk_param {
751b0e9cb28SMasami Hiramatsu 	bool recursive;
752e0d153c6SMasami Hiramatsu 	line_walk_callback_t callback;
753e0d153c6SMasami Hiramatsu 	void *data;
754e0d153c6SMasami Hiramatsu 	int retval;
755e0d153c6SMasami Hiramatsu };
756e0d153c6SMasami Hiramatsu 
757e0d153c6SMasami Hiramatsu static int __die_walk_funclines_cb(Dwarf_Die *in_die, void *data)
758e0d153c6SMasami Hiramatsu {
759e0d153c6SMasami Hiramatsu 	struct __line_walk_param *lw = data;
760b0e9cb28SMasami Hiramatsu 	Dwarf_Addr addr = 0;
761b0e9cb28SMasami Hiramatsu 	const char *fname;
762e0d153c6SMasami Hiramatsu 	int lineno;
763e0d153c6SMasami Hiramatsu 
764e0d153c6SMasami Hiramatsu 	if (dwarf_tag(in_die) == DW_TAG_inlined_subroutine) {
765b0e9cb28SMasami Hiramatsu 		fname = die_get_call_file(in_die);
766e0d153c6SMasami Hiramatsu 		lineno = die_get_call_lineno(in_die);
76718e21eb6SMasami Hiramatsu 		if (fname && lineno > 0 && die_entrypc(in_die, &addr) == 0) {
768b0e9cb28SMasami Hiramatsu 			lw->retval = lw->callback(fname, lineno, addr, lw->data);
769e0d153c6SMasami Hiramatsu 			if (lw->retval != 0)
770e0d153c6SMasami Hiramatsu 				return DIE_FIND_CB_END;
771e0d153c6SMasami Hiramatsu 		}
772b0e9cb28SMasami Hiramatsu 		if (!lw->recursive)
773e0d153c6SMasami Hiramatsu 			return DIE_FIND_CB_SIBLING;
774acb6a704SMasami Hiramatsu 	}
775b0e9cb28SMasami Hiramatsu 
776b0e9cb28SMasami Hiramatsu 	if (addr) {
777b0e9cb28SMasami Hiramatsu 		fname = dwarf_decl_file(in_die);
778b0e9cb28SMasami Hiramatsu 		if (fname && dwarf_decl_line(in_die, &lineno) == 0) {
779b0e9cb28SMasami Hiramatsu 			lw->retval = lw->callback(fname, lineno, addr, lw->data);
780b0e9cb28SMasami Hiramatsu 			if (lw->retval != 0)
781b0e9cb28SMasami Hiramatsu 				return DIE_FIND_CB_END;
782b0e9cb28SMasami Hiramatsu 		}
783b0e9cb28SMasami Hiramatsu 	}
784b0e9cb28SMasami Hiramatsu 
785b0e9cb28SMasami Hiramatsu 	/* Continue to search nested inlined function call-sites */
786b0e9cb28SMasami Hiramatsu 	return DIE_FIND_CB_CONTINUE;
787e0d153c6SMasami Hiramatsu }
788e0d153c6SMasami Hiramatsu 
789e0d153c6SMasami Hiramatsu /* Walk on lines of blocks included in given DIE */
790b0e9cb28SMasami Hiramatsu static int __die_walk_funclines(Dwarf_Die *sp_die, bool recursive,
791e0d153c6SMasami Hiramatsu 				line_walk_callback_t callback, void *data)
792e0d153c6SMasami Hiramatsu {
793e0d153c6SMasami Hiramatsu 	struct __line_walk_param lw = {
794b0e9cb28SMasami Hiramatsu 		.recursive = recursive,
795e0d153c6SMasami Hiramatsu 		.callback = callback,
796e0d153c6SMasami Hiramatsu 		.data = data,
797e0d153c6SMasami Hiramatsu 		.retval = 0,
798e0d153c6SMasami Hiramatsu 	};
799e0d153c6SMasami Hiramatsu 	Dwarf_Die die_mem;
800e0d153c6SMasami Hiramatsu 	Dwarf_Addr addr;
801b0e9cb28SMasami Hiramatsu 	const char *fname;
802e0d153c6SMasami Hiramatsu 	int lineno;
803e0d153c6SMasami Hiramatsu 
804e0d153c6SMasami Hiramatsu 	/* Handle function declaration line */
805b0e9cb28SMasami Hiramatsu 	fname = dwarf_decl_file(sp_die);
806b0e9cb28SMasami Hiramatsu 	if (fname && dwarf_decl_line(sp_die, &lineno) == 0 &&
80791e2f539SMasami Hiramatsu 	    die_entrypc(sp_die, &addr) == 0) {
808b0e9cb28SMasami Hiramatsu 		lw.retval = callback(fname, lineno, addr, data);
809e0d153c6SMasami Hiramatsu 		if (lw.retval != 0)
810e0d153c6SMasami Hiramatsu 			goto done;
811e0d153c6SMasami Hiramatsu 	}
812e0d153c6SMasami Hiramatsu 	die_find_child(sp_die, __die_walk_funclines_cb, &lw, &die_mem);
813e0d153c6SMasami Hiramatsu done:
814e0d153c6SMasami Hiramatsu 	return lw.retval;
815e0d153c6SMasami Hiramatsu }
816e0d153c6SMasami Hiramatsu 
817e0d153c6SMasami Hiramatsu static int __die_walk_culines_cb(Dwarf_Die *sp_die, void *data)
818e0d153c6SMasami Hiramatsu {
819e0d153c6SMasami Hiramatsu 	struct __line_walk_param *lw = data;
820e0d153c6SMasami Hiramatsu 
821acb6a704SMasami Hiramatsu 	/*
822acb6a704SMasami Hiramatsu 	 * Since inlined function can include another inlined function in
823acb6a704SMasami Hiramatsu 	 * the same file, we need to walk in it recursively.
824acb6a704SMasami Hiramatsu 	 */
825b0e9cb28SMasami Hiramatsu 	lw->retval = __die_walk_funclines(sp_die, true, lw->callback, lw->data);
826e0d153c6SMasami Hiramatsu 	if (lw->retval != 0)
827e0d153c6SMasami Hiramatsu 		return DWARF_CB_ABORT;
828e0d153c6SMasami Hiramatsu 
829e0d153c6SMasami Hiramatsu 	return DWARF_CB_OK;
830e0d153c6SMasami Hiramatsu }
831e0d153c6SMasami Hiramatsu 
832e0d153c6SMasami Hiramatsu /**
833e0d153c6SMasami Hiramatsu  * die_walk_lines - Walk on lines inside given DIE
834a128405cSMasami Hiramatsu  * @rt_die: a root DIE (CU, subprogram or inlined_subroutine)
835e0d153c6SMasami Hiramatsu  * @callback: callback routine
836e0d153c6SMasami Hiramatsu  * @data: user data
837e0d153c6SMasami Hiramatsu  *
838e0d153c6SMasami Hiramatsu  * Walk on all lines inside given @rt_die and call @callback on each line.
839e0d153c6SMasami Hiramatsu  * If the @rt_die is a function, walk only on the lines inside the function,
840e0d153c6SMasami Hiramatsu  * otherwise @rt_die must be a CU DIE.
841e0d153c6SMasami Hiramatsu  * Note that this walks not only dwarf line list, but also function entries
842e0d153c6SMasami Hiramatsu  * and inline call-site.
843e0d153c6SMasami Hiramatsu  */
844e0d153c6SMasami Hiramatsu int die_walk_lines(Dwarf_Die *rt_die, line_walk_callback_t callback, void *data)
845e0d153c6SMasami Hiramatsu {
846e0d153c6SMasami Hiramatsu 	Dwarf_Lines *lines;
847e0d153c6SMasami Hiramatsu 	Dwarf_Line *line;
848e0d153c6SMasami Hiramatsu 	Dwarf_Addr addr;
84986c0bf85SMasami Hiramatsu 	const char *fname, *decf = NULL, *inf = NULL;
850e0d153c6SMasami Hiramatsu 	int lineno, ret = 0;
85175186a9bSMasami Hiramatsu 	int decl = 0, inl;
852e0d153c6SMasami Hiramatsu 	Dwarf_Die die_mem, *cu_die;
853e0d153c6SMasami Hiramatsu 	size_t nlines, i;
854f4d99bdfSMasami Hiramatsu 	bool flag;
855e0d153c6SMasami Hiramatsu 
856e0d153c6SMasami Hiramatsu 	/* Get the CU die */
85775186a9bSMasami Hiramatsu 	if (dwarf_tag(rt_die) != DW_TAG_compile_unit) {
858e0d153c6SMasami Hiramatsu 		cu_die = dwarf_diecu(rt_die, &die_mem, NULL, NULL);
85975186a9bSMasami Hiramatsu 		dwarf_decl_line(rt_die, &decl);
86075186a9bSMasami Hiramatsu 		decf = dwarf_decl_file(rt_die);
86175186a9bSMasami Hiramatsu 	} else
862e0d153c6SMasami Hiramatsu 		cu_die = rt_die;
863e0d153c6SMasami Hiramatsu 	if (!cu_die) {
864a128405cSMasami Hiramatsu 		pr_debug2("Failed to get CU from given DIE.\n");
865e0d153c6SMasami Hiramatsu 		return -EINVAL;
866e0d153c6SMasami Hiramatsu 	}
867e0d153c6SMasami Hiramatsu 
868e0d153c6SMasami Hiramatsu 	/* Get lines list in the CU */
869e0d153c6SMasami Hiramatsu 	if (dwarf_getsrclines(cu_die, &lines, &nlines) != 0) {
870e0d153c6SMasami Hiramatsu 		pr_debug2("Failed to get source lines on this CU.\n");
871e0d153c6SMasami Hiramatsu 		return -ENOENT;
872e0d153c6SMasami Hiramatsu 	}
873e0d153c6SMasami Hiramatsu 	pr_debug2("Get %zd lines from this CU\n", nlines);
874e0d153c6SMasami Hiramatsu 
875e0d153c6SMasami Hiramatsu 	/* Walk on the lines on lines list */
876e0d153c6SMasami Hiramatsu 	for (i = 0; i < nlines; i++) {
877e0d153c6SMasami Hiramatsu 		line = dwarf_onesrcline(lines, i);
878e0d153c6SMasami Hiramatsu 		if (line == NULL ||
879e0d153c6SMasami Hiramatsu 		    dwarf_lineno(line, &lineno) != 0 ||
880e0d153c6SMasami Hiramatsu 		    dwarf_lineaddr(line, &addr) != 0) {
881e0d153c6SMasami Hiramatsu 			pr_debug2("Failed to get line info. "
882e0d153c6SMasami Hiramatsu 				  "Possible error in debuginfo.\n");
883e0d153c6SMasami Hiramatsu 			continue;
884e0d153c6SMasami Hiramatsu 		}
885f4d99bdfSMasami Hiramatsu 		/* Skip end-of-sequence */
886f4d99bdfSMasami Hiramatsu 		if (dwarf_lineendsequence(line, &flag) != 0 || flag)
887f4d99bdfSMasami Hiramatsu 			continue;
888f4d99bdfSMasami Hiramatsu 		/* Skip Non statement line-info */
889f4d99bdfSMasami Hiramatsu 		if (dwarf_linebeginstatement(line, &flag) != 0 || !flag)
890f4d99bdfSMasami Hiramatsu 			continue;
891e0d153c6SMasami Hiramatsu 		/* Filter lines based on address */
89286a76027SMasami Hiramatsu 		if (rt_die != cu_die) {
893e0d153c6SMasami Hiramatsu 			/*
894e0d153c6SMasami Hiramatsu 			 * Address filtering
895e0d153c6SMasami Hiramatsu 			 * The line is included in given function, and
896e0d153c6SMasami Hiramatsu 			 * no inline block includes it.
897e0d153c6SMasami Hiramatsu 			 */
89875186a9bSMasami Hiramatsu 			if (!dwarf_haspc(rt_die, addr))
899e0d153c6SMasami Hiramatsu 				continue;
90086c0bf85SMasami Hiramatsu 
90175186a9bSMasami Hiramatsu 			if (die_find_inlinefunc(rt_die, addr, &die_mem)) {
90286c0bf85SMasami Hiramatsu 				/* Call-site check */
90386c0bf85SMasami Hiramatsu 				inf = die_get_call_file(&die_mem);
90486c0bf85SMasami Hiramatsu 				if ((inf && !strcmp(inf, decf)) &&
90586c0bf85SMasami Hiramatsu 				    die_get_call_lineno(&die_mem) == lineno)
90686c0bf85SMasami Hiramatsu 					goto found;
90786c0bf85SMasami Hiramatsu 
90875186a9bSMasami Hiramatsu 				dwarf_decl_line(&die_mem, &inl);
90975186a9bSMasami Hiramatsu 				if (inl != decl ||
91075186a9bSMasami Hiramatsu 				    decf != dwarf_decl_file(&die_mem))
91175186a9bSMasami Hiramatsu 					continue;
91275186a9bSMasami Hiramatsu 			}
91386a76027SMasami Hiramatsu 		}
91486c0bf85SMasami Hiramatsu found:
915e0d153c6SMasami Hiramatsu 		/* Get source line */
916e0d153c6SMasami Hiramatsu 		fname = dwarf_linesrc(line, NULL, NULL);
917e0d153c6SMasami Hiramatsu 
918e0d153c6SMasami Hiramatsu 		ret = callback(fname, lineno, addr, data);
919e0d153c6SMasami Hiramatsu 		if (ret != 0)
920e0d153c6SMasami Hiramatsu 			return ret;
921e0d153c6SMasami Hiramatsu 	}
922e0d153c6SMasami Hiramatsu 
923e0d153c6SMasami Hiramatsu 	/*
924e0d153c6SMasami Hiramatsu 	 * Dwarf lines doesn't include function declarations and inlined
925e0d153c6SMasami Hiramatsu 	 * subroutines. We have to check functions list or given function.
926e0d153c6SMasami Hiramatsu 	 */
927e0d153c6SMasami Hiramatsu 	if (rt_die != cu_die)
928b0e9cb28SMasami Hiramatsu 		/*
929acb6a704SMasami Hiramatsu 		 * Don't need walk inlined functions recursively, because
930acb6a704SMasami Hiramatsu 		 * inner inlined functions don't have the lines of the
931acb6a704SMasami Hiramatsu 		 * specified function.
932b0e9cb28SMasami Hiramatsu 		 */
933b0e9cb28SMasami Hiramatsu 		ret = __die_walk_funclines(rt_die, false, callback, data);
934e0d153c6SMasami Hiramatsu 	else {
935e0d153c6SMasami Hiramatsu 		struct __line_walk_param param = {
936e0d153c6SMasami Hiramatsu 			.callback = callback,
937e0d153c6SMasami Hiramatsu 			.data = data,
938e0d153c6SMasami Hiramatsu 			.retval = 0,
939e0d153c6SMasami Hiramatsu 		};
940e0d153c6SMasami Hiramatsu 		dwarf_getfuncs(cu_die, __die_walk_culines_cb, &param, 0);
941e0d153c6SMasami Hiramatsu 		ret = param.retval;
942e0d153c6SMasami Hiramatsu 	}
943e0d153c6SMasami Hiramatsu 
944e0d153c6SMasami Hiramatsu 	return ret;
945e0d153c6SMasami Hiramatsu }
946e0d153c6SMasami Hiramatsu 
947e0d153c6SMasami Hiramatsu struct __find_variable_param {
948e0d153c6SMasami Hiramatsu 	const char *name;
949e0d153c6SMasami Hiramatsu 	Dwarf_Addr addr;
950e0d153c6SMasami Hiramatsu };
951e0d153c6SMasami Hiramatsu 
952e0d153c6SMasami Hiramatsu static int __die_find_variable_cb(Dwarf_Die *die_mem, void *data)
953e0d153c6SMasami Hiramatsu {
954e0d153c6SMasami Hiramatsu 	struct __find_variable_param *fvp = data;
955082f96a9SMasami Hiramatsu 	Dwarf_Attribute attr;
956e0d153c6SMasami Hiramatsu 	int tag;
957e0d153c6SMasami Hiramatsu 
958e0d153c6SMasami Hiramatsu 	tag = dwarf_tag(die_mem);
959e0d153c6SMasami Hiramatsu 	if ((tag == DW_TAG_formal_parameter ||
960e0d153c6SMasami Hiramatsu 	     tag == DW_TAG_variable) &&
961082f96a9SMasami Hiramatsu 	    die_compare_name(die_mem, fvp->name) &&
962082f96a9SMasami Hiramatsu 	/* Does the DIE have location information or external instance? */
963082f96a9SMasami Hiramatsu 	    (dwarf_attr(die_mem, DW_AT_external, &attr) ||
964082f96a9SMasami Hiramatsu 	     dwarf_attr(die_mem, DW_AT_location, &attr)))
965e0d153c6SMasami Hiramatsu 		return DIE_FIND_CB_END;
966e0d153c6SMasami Hiramatsu 	if (dwarf_haspc(die_mem, fvp->addr))
967e0d153c6SMasami Hiramatsu 		return DIE_FIND_CB_CONTINUE;
968e0d153c6SMasami Hiramatsu 	else
969e0d153c6SMasami Hiramatsu 		return DIE_FIND_CB_SIBLING;
970e0d153c6SMasami Hiramatsu }
971e0d153c6SMasami Hiramatsu 
972e0d153c6SMasami Hiramatsu /**
973e0d153c6SMasami Hiramatsu  * die_find_variable_at - Find a given name variable at given address
974e0d153c6SMasami Hiramatsu  * @sp_die: a function DIE
975e0d153c6SMasami Hiramatsu  * @name: variable name
976e0d153c6SMasami Hiramatsu  * @addr: address
977e0d153c6SMasami Hiramatsu  * @die_mem: a buffer for result DIE
978e0d153c6SMasami Hiramatsu  *
979e0d153c6SMasami Hiramatsu  * Find a variable DIE called @name at @addr in @sp_die.
980e0d153c6SMasami Hiramatsu  */
981e0d153c6SMasami Hiramatsu Dwarf_Die *die_find_variable_at(Dwarf_Die *sp_die, const char *name,
982e0d153c6SMasami Hiramatsu 				Dwarf_Addr addr, Dwarf_Die *die_mem)
983e0d153c6SMasami Hiramatsu {
984e0d153c6SMasami Hiramatsu 	struct __find_variable_param fvp = { .name = name, .addr = addr};
985e0d153c6SMasami Hiramatsu 
986e0d153c6SMasami Hiramatsu 	return die_find_child(sp_die, __die_find_variable_cb, (void *)&fvp,
987e0d153c6SMasami Hiramatsu 			      die_mem);
988e0d153c6SMasami Hiramatsu }
989e0d153c6SMasami Hiramatsu 
990e0d153c6SMasami Hiramatsu static int __die_find_member_cb(Dwarf_Die *die_mem, void *data)
991e0d153c6SMasami Hiramatsu {
992e0d153c6SMasami Hiramatsu 	const char *name = data;
993e0d153c6SMasami Hiramatsu 
994c7273835SMasami Hiramatsu 	if (dwarf_tag(die_mem) == DW_TAG_member) {
995c7273835SMasami Hiramatsu 		if (die_compare_name(die_mem, name))
996e0d153c6SMasami Hiramatsu 			return DIE_FIND_CB_END;
997c7273835SMasami Hiramatsu 		else if (!dwarf_diename(die_mem)) {	/* Unnamed structure */
998c7273835SMasami Hiramatsu 			Dwarf_Die type_die, tmp_die;
999c7273835SMasami Hiramatsu 			if (die_get_type(die_mem, &type_die) &&
1000c7273835SMasami Hiramatsu 			    die_find_member(&type_die, name, &tmp_die))
1001c7273835SMasami Hiramatsu 				return DIE_FIND_CB_END;
1002c7273835SMasami Hiramatsu 		}
1003c7273835SMasami Hiramatsu 	}
1004e0d153c6SMasami Hiramatsu 	return DIE_FIND_CB_SIBLING;
1005e0d153c6SMasami Hiramatsu }
1006e0d153c6SMasami Hiramatsu 
1007e0d153c6SMasami Hiramatsu /**
1008e0d153c6SMasami Hiramatsu  * die_find_member - Find a given name member in a data structure
1009e0d153c6SMasami Hiramatsu  * @st_die: a data structure type DIE
1010e0d153c6SMasami Hiramatsu  * @name: member name
1011e0d153c6SMasami Hiramatsu  * @die_mem: a buffer for result DIE
1012e0d153c6SMasami Hiramatsu  *
1013e0d153c6SMasami Hiramatsu  * Find a member DIE called @name in @st_die.
1014e0d153c6SMasami Hiramatsu  */
1015e0d153c6SMasami Hiramatsu Dwarf_Die *die_find_member(Dwarf_Die *st_die, const char *name,
1016e0d153c6SMasami Hiramatsu 			   Dwarf_Die *die_mem)
1017e0d153c6SMasami Hiramatsu {
1018e0d153c6SMasami Hiramatsu 	return die_find_child(st_die, __die_find_member_cb, (void *)name,
1019e0d153c6SMasami Hiramatsu 			      die_mem);
1020e0d153c6SMasami Hiramatsu }
1021e0d153c6SMasami Hiramatsu 
1022e0d153c6SMasami Hiramatsu /**
1023e0d153c6SMasami Hiramatsu  * die_get_typename - Get the name of given variable DIE
1024e0d153c6SMasami Hiramatsu  * @vr_die: a variable DIE
1025fb9596d1SHe Kuang  * @buf: a strbuf for result type name
1026e0d153c6SMasami Hiramatsu  *
1027fb9596d1SHe Kuang  * Get the name of @vr_die and stores it to @buf. Return 0 if succeeded.
1028fb9596d1SHe Kuang  * and Return -ENOENT if failed to find type name.
1029e0d153c6SMasami Hiramatsu  * Note that the result will stores typedef name if possible, and stores
1030e0d153c6SMasami Hiramatsu  * "*(function_type)" if the type is a function pointer.
1031e0d153c6SMasami Hiramatsu  */
1032fb9596d1SHe Kuang int die_get_typename(Dwarf_Die *vr_die, struct strbuf *buf)
1033e0d153c6SMasami Hiramatsu {
1034e0d153c6SMasami Hiramatsu 	Dwarf_Die type;
1035fb9596d1SHe Kuang 	int tag, ret;
1036e0d153c6SMasami Hiramatsu 	const char *tmp = "";
1037e0d153c6SMasami Hiramatsu 
1038e0d153c6SMasami Hiramatsu 	if (__die_get_real_type(vr_die, &type) == NULL)
1039e0d153c6SMasami Hiramatsu 		return -ENOENT;
1040e0d153c6SMasami Hiramatsu 
1041e0d153c6SMasami Hiramatsu 	tag = dwarf_tag(&type);
1042e0d153c6SMasami Hiramatsu 	if (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type)
1043e0d153c6SMasami Hiramatsu 		tmp = "*";
1044e0d153c6SMasami Hiramatsu 	else if (tag == DW_TAG_subroutine_type) {
1045e0d153c6SMasami Hiramatsu 		/* Function pointer */
1046bf4d5f25SMasami Hiramatsu 		return strbuf_add(buf, "(function_type)", 15);
1047e0d153c6SMasami Hiramatsu 	} else {
1048e0d153c6SMasami Hiramatsu 		if (!dwarf_diename(&type))
1049e0d153c6SMasami Hiramatsu 			return -ENOENT;
1050e0d153c6SMasami Hiramatsu 		if (tag == DW_TAG_union_type)
1051e0d153c6SMasami Hiramatsu 			tmp = "union ";
1052e0d153c6SMasami Hiramatsu 		else if (tag == DW_TAG_structure_type)
1053e0d153c6SMasami Hiramatsu 			tmp = "struct ";
1054bb2d17a0SHyeoncheol Lee 		else if (tag == DW_TAG_enumeration_type)
1055bb2d17a0SHyeoncheol Lee 			tmp = "enum ";
1056e0d153c6SMasami Hiramatsu 		/* Write a base name */
1057bf4d5f25SMasami Hiramatsu 		return strbuf_addf(buf, "%s%s", tmp, dwarf_diename(&type));
1058e0d153c6SMasami Hiramatsu 	}
1059fb9596d1SHe Kuang 	ret = die_get_typename(&type, buf);
1060bf4d5f25SMasami Hiramatsu 	return ret ? ret : strbuf_addstr(buf, tmp);
1061e0d153c6SMasami Hiramatsu }
1062e0d153c6SMasami Hiramatsu 
1063e0d153c6SMasami Hiramatsu /**
1064e0d153c6SMasami Hiramatsu  * die_get_varname - Get the name and type of given variable DIE
1065e0d153c6SMasami Hiramatsu  * @vr_die: a variable DIE
1066fb9596d1SHe Kuang  * @buf: a strbuf for type and variable name
1067e0d153c6SMasami Hiramatsu  *
1068e0d153c6SMasami Hiramatsu  * Get the name and type of @vr_die and stores it in @buf as "type\tname".
1069e0d153c6SMasami Hiramatsu  */
1070fb9596d1SHe Kuang int die_get_varname(Dwarf_Die *vr_die, struct strbuf *buf)
1071e0d153c6SMasami Hiramatsu {
1072fb9596d1SHe Kuang 	int ret;
1073e0d153c6SMasami Hiramatsu 
1074fb9596d1SHe Kuang 	ret = die_get_typename(vr_die, buf);
1075e0d153c6SMasami Hiramatsu 	if (ret < 0) {
1076e0d153c6SMasami Hiramatsu 		pr_debug("Failed to get type, make it unknown.\n");
1077bf4d5f25SMasami Hiramatsu 		ret = strbuf_add(buf, " (unknown_type)", 14);
1078e0d153c6SMasami Hiramatsu 	}
1079fb9596d1SHe Kuang 
1080bf4d5f25SMasami Hiramatsu 	return ret < 0 ? ret : strbuf_addf(buf, "\t%s", dwarf_diename(vr_die));
1081e0d153c6SMasami Hiramatsu }
1082e0d153c6SMasami Hiramatsu 
1083a36ebe4eSJin Yao #ifdef HAVE_DWARF_GETLOCATIONS_SUPPORT
1084349e8d26SHe Kuang /**
1085349e8d26SHe Kuang  * die_get_var_innermost_scope - Get innermost scope range of given variable DIE
1086349e8d26SHe Kuang  * @sp_die: a subprogram DIE
1087349e8d26SHe Kuang  * @vr_die: a variable DIE
1088349e8d26SHe Kuang  * @buf: a strbuf for variable byte offset range
1089349e8d26SHe Kuang  *
1090349e8d26SHe Kuang  * Get the innermost scope range of @vr_die and stores it in @buf as
1091349e8d26SHe Kuang  * "@<function_name+[NN-NN,NN-NN]>".
1092349e8d26SHe Kuang  */
1093349e8d26SHe Kuang static int die_get_var_innermost_scope(Dwarf_Die *sp_die, Dwarf_Die *vr_die,
1094349e8d26SHe Kuang 				struct strbuf *buf)
1095349e8d26SHe Kuang {
1096349e8d26SHe Kuang 	Dwarf_Die *scopes;
1097349e8d26SHe Kuang 	int count;
1098349e8d26SHe Kuang 	size_t offset = 0;
1099349e8d26SHe Kuang 	Dwarf_Addr base;
1100349e8d26SHe Kuang 	Dwarf_Addr start, end;
1101349e8d26SHe Kuang 	Dwarf_Addr entry;
1102349e8d26SHe Kuang 	int ret;
1103349e8d26SHe Kuang 	bool first = true;
1104349e8d26SHe Kuang 	const char *name;
1105349e8d26SHe Kuang 
1106af04dd2fSMasami Hiramatsu 	ret = die_entrypc(sp_die, &entry);
1107349e8d26SHe Kuang 	if (ret)
1108349e8d26SHe Kuang 		return ret;
1109349e8d26SHe Kuang 
1110349e8d26SHe Kuang 	name = dwarf_diename(sp_die);
1111349e8d26SHe Kuang 	if (!name)
1112349e8d26SHe Kuang 		return -ENOENT;
1113349e8d26SHe Kuang 
1114349e8d26SHe Kuang 	count = dwarf_getscopes_die(vr_die, &scopes);
1115349e8d26SHe Kuang 
1116349e8d26SHe Kuang 	/* (*SCOPES)[1] is the DIE for the scope containing that scope */
1117349e8d26SHe Kuang 	if (count <= 1) {
1118349e8d26SHe Kuang 		ret = -EINVAL;
1119349e8d26SHe Kuang 		goto out;
1120349e8d26SHe Kuang 	}
1121349e8d26SHe Kuang 
1122349e8d26SHe Kuang 	while ((offset = dwarf_ranges(&scopes[1], offset, &base,
1123349e8d26SHe Kuang 					&start, &end)) > 0) {
1124349e8d26SHe Kuang 		start -= entry;
1125349e8d26SHe Kuang 		end -= entry;
1126349e8d26SHe Kuang 
1127349e8d26SHe Kuang 		if (first) {
1128bf4d5f25SMasami Hiramatsu 			ret = strbuf_addf(buf, "@<%s+[%" PRIu64 "-%" PRIu64,
1129349e8d26SHe Kuang 					  name, start, end);
1130349e8d26SHe Kuang 			first = false;
1131349e8d26SHe Kuang 		} else {
1132bf4d5f25SMasami Hiramatsu 			ret = strbuf_addf(buf, ",%" PRIu64 "-%" PRIu64,
1133349e8d26SHe Kuang 					  start, end);
1134349e8d26SHe Kuang 		}
1135bf4d5f25SMasami Hiramatsu 		if (ret < 0)
1136bf4d5f25SMasami Hiramatsu 			goto out;
1137349e8d26SHe Kuang 	}
1138349e8d26SHe Kuang 
1139349e8d26SHe Kuang 	if (!first)
1140bf4d5f25SMasami Hiramatsu 		ret = strbuf_add(buf, "]>", 2);
1141349e8d26SHe Kuang 
1142349e8d26SHe Kuang out:
1143349e8d26SHe Kuang 	free(scopes);
1144349e8d26SHe Kuang 	return ret;
1145349e8d26SHe Kuang }
1146349e8d26SHe Kuang 
1147349e8d26SHe Kuang /**
1148349e8d26SHe Kuang  * die_get_var_range - Get byte offset range of given variable DIE
1149349e8d26SHe Kuang  * @sp_die: a subprogram DIE
1150349e8d26SHe Kuang  * @vr_die: a variable DIE
1151349e8d26SHe Kuang  * @buf: a strbuf for type and variable name and byte offset range
1152349e8d26SHe Kuang  *
1153349e8d26SHe Kuang  * Get the byte offset range of @vr_die and stores it in @buf as
1154349e8d26SHe Kuang  * "@<function_name+[NN-NN,NN-NN]>".
1155349e8d26SHe Kuang  */
1156349e8d26SHe Kuang int die_get_var_range(Dwarf_Die *sp_die, Dwarf_Die *vr_die, struct strbuf *buf)
1157349e8d26SHe Kuang {
1158349e8d26SHe Kuang 	int ret = 0;
1159349e8d26SHe Kuang 	Dwarf_Addr base;
1160349e8d26SHe Kuang 	Dwarf_Addr start, end;
1161349e8d26SHe Kuang 	Dwarf_Addr entry;
1162349e8d26SHe Kuang 	Dwarf_Op *op;
1163349e8d26SHe Kuang 	size_t nops;
1164349e8d26SHe Kuang 	size_t offset = 0;
1165349e8d26SHe Kuang 	Dwarf_Attribute attr;
1166349e8d26SHe Kuang 	bool first = true;
1167349e8d26SHe Kuang 	const char *name;
1168349e8d26SHe Kuang 
1169af04dd2fSMasami Hiramatsu 	ret = die_entrypc(sp_die, &entry);
1170349e8d26SHe Kuang 	if (ret)
1171349e8d26SHe Kuang 		return ret;
1172349e8d26SHe Kuang 
1173349e8d26SHe Kuang 	name = dwarf_diename(sp_die);
1174349e8d26SHe Kuang 	if (!name)
1175349e8d26SHe Kuang 		return -ENOENT;
1176349e8d26SHe Kuang 
1177349e8d26SHe Kuang 	if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL)
1178349e8d26SHe Kuang 		return -EINVAL;
1179349e8d26SHe Kuang 
1180bf4d5f25SMasami Hiramatsu 	while ((offset = dwarf_getlocations(&attr, offset, &base,
1181349e8d26SHe Kuang 					&start, &end, &op, &nops)) > 0) {
1182349e8d26SHe Kuang 		if (start == 0) {
1183349e8d26SHe Kuang 			/* Single Location Descriptions */
1184349e8d26SHe Kuang 			ret = die_get_var_innermost_scope(sp_die, vr_die, buf);
1185bf4d5f25SMasami Hiramatsu 			goto out;
1186349e8d26SHe Kuang 		}
1187349e8d26SHe Kuang 
1188349e8d26SHe Kuang 		/* Location Lists */
1189349e8d26SHe Kuang 		start -= entry;
1190349e8d26SHe Kuang 		end -= entry;
1191349e8d26SHe Kuang 		if (first) {
1192bf4d5f25SMasami Hiramatsu 			ret = strbuf_addf(buf, "@<%s+[%" PRIu64 "-%" PRIu64,
1193349e8d26SHe Kuang 					  name, start, end);
1194349e8d26SHe Kuang 			first = false;
1195349e8d26SHe Kuang 		} else {
1196bf4d5f25SMasami Hiramatsu 			ret = strbuf_addf(buf, ",%" PRIu64 "-%" PRIu64,
1197349e8d26SHe Kuang 					  start, end);
1198349e8d26SHe Kuang 		}
1199bf4d5f25SMasami Hiramatsu 		if (ret < 0)
1200bf4d5f25SMasami Hiramatsu 			goto out;
1201349e8d26SHe Kuang 	}
1202349e8d26SHe Kuang 
1203349e8d26SHe Kuang 	if (!first)
1204bf4d5f25SMasami Hiramatsu 		ret = strbuf_add(buf, "]>", 2);
1205bf4d5f25SMasami Hiramatsu out:
1206349e8d26SHe Kuang 	return ret;
1207349e8d26SHe Kuang }
1208bd0419e2SArnaldo Carvalho de Melo #else
1209bd0419e2SArnaldo Carvalho de Melo int die_get_var_range(Dwarf_Die *sp_die __maybe_unused,
1210bd0419e2SArnaldo Carvalho de Melo 		      Dwarf_Die *vr_die __maybe_unused,
1211bd0419e2SArnaldo Carvalho de Melo 		      struct strbuf *buf __maybe_unused)
1212bd0419e2SArnaldo Carvalho de Melo {
1213bd0419e2SArnaldo Carvalho de Melo 	return -ENOTSUP;
1214bd0419e2SArnaldo Carvalho de Melo }
1215bd0419e2SArnaldo Carvalho de Melo #endif
12166243b9dcSRavi Bangoria 
12176243b9dcSRavi Bangoria /*
12186243b9dcSRavi Bangoria  * die_has_loclist - Check if DW_AT_location of @vr_die is a location list
12196243b9dcSRavi Bangoria  * @vr_die: a variable DIE
12206243b9dcSRavi Bangoria  */
12216243b9dcSRavi Bangoria static bool die_has_loclist(Dwarf_Die *vr_die)
12226243b9dcSRavi Bangoria {
12236243b9dcSRavi Bangoria 	Dwarf_Attribute loc;
12246243b9dcSRavi Bangoria 	int tag = dwarf_tag(vr_die);
12256243b9dcSRavi Bangoria 
12266243b9dcSRavi Bangoria 	if (tag != DW_TAG_formal_parameter &&
12276243b9dcSRavi Bangoria 	    tag != DW_TAG_variable)
12286243b9dcSRavi Bangoria 		return false;
12296243b9dcSRavi Bangoria 
12306243b9dcSRavi Bangoria 	return (dwarf_attr_integrate(vr_die, DW_AT_location, &loc) &&
12316243b9dcSRavi Bangoria 		dwarf_whatform(&loc) == DW_FORM_sec_offset);
12326243b9dcSRavi Bangoria }
12336243b9dcSRavi Bangoria 
12346243b9dcSRavi Bangoria /*
12356243b9dcSRavi Bangoria  * die_is_optimized_target - Check if target program is compiled with
12366243b9dcSRavi Bangoria  * optimization
12376243b9dcSRavi Bangoria  * @cu_die: a CU DIE
12386243b9dcSRavi Bangoria  *
12396243b9dcSRavi Bangoria  * For any object in given CU whose DW_AT_location is a location list,
12406243b9dcSRavi Bangoria  * target program is compiled with optimization. This is applicable to
12416243b9dcSRavi Bangoria  * clang as well.
12426243b9dcSRavi Bangoria  */
12436243b9dcSRavi Bangoria bool die_is_optimized_target(Dwarf_Die *cu_die)
12446243b9dcSRavi Bangoria {
12456243b9dcSRavi Bangoria 	Dwarf_Die tmp_die;
12466243b9dcSRavi Bangoria 
12476243b9dcSRavi Bangoria 	if (die_has_loclist(cu_die))
12486243b9dcSRavi Bangoria 		return true;
12496243b9dcSRavi Bangoria 
12506243b9dcSRavi Bangoria 	if (!dwarf_child(cu_die, &tmp_die) &&
12516243b9dcSRavi Bangoria 	    die_is_optimized_target(&tmp_die))
12526243b9dcSRavi Bangoria 		return true;
12536243b9dcSRavi Bangoria 
12546243b9dcSRavi Bangoria 	if (!dwarf_siblingof(cu_die, &tmp_die) &&
12556243b9dcSRavi Bangoria 	    die_is_optimized_target(&tmp_die))
12566243b9dcSRavi Bangoria 		return true;
12576243b9dcSRavi Bangoria 
12586243b9dcSRavi Bangoria 	return false;
12596243b9dcSRavi Bangoria }
12606243b9dcSRavi Bangoria 
12616243b9dcSRavi Bangoria /*
12626243b9dcSRavi Bangoria  * die_search_idx - Search index of given line address
12636243b9dcSRavi Bangoria  * @lines: Line records of single CU
12646243b9dcSRavi Bangoria  * @nr_lines: Number of @lines
12656243b9dcSRavi Bangoria  * @addr: address we are looking for
12666243b9dcSRavi Bangoria  * @idx: index to be set by this function (return value)
12676243b9dcSRavi Bangoria  *
12686243b9dcSRavi Bangoria  * Search for @addr by looping over every lines of CU. If address
12696243b9dcSRavi Bangoria  * matches, set index of that line in @idx. Note that single source
12706243b9dcSRavi Bangoria  * line can have multiple line records. i.e. single source line can
12716243b9dcSRavi Bangoria  * have multiple index.
12726243b9dcSRavi Bangoria  */
12736243b9dcSRavi Bangoria static bool die_search_idx(Dwarf_Lines *lines, unsigned long nr_lines,
12746243b9dcSRavi Bangoria 			   Dwarf_Addr addr, unsigned long *idx)
12756243b9dcSRavi Bangoria {
12766243b9dcSRavi Bangoria 	unsigned long i;
12776243b9dcSRavi Bangoria 	Dwarf_Addr tmp;
12786243b9dcSRavi Bangoria 
12796243b9dcSRavi Bangoria 	for (i = 0; i < nr_lines; i++) {
12806243b9dcSRavi Bangoria 		if (dwarf_lineaddr(dwarf_onesrcline(lines, i), &tmp))
12816243b9dcSRavi Bangoria 			return false;
12826243b9dcSRavi Bangoria 
12836243b9dcSRavi Bangoria 		if (tmp == addr) {
12846243b9dcSRavi Bangoria 			*idx = i;
12856243b9dcSRavi Bangoria 			return true;
12866243b9dcSRavi Bangoria 		}
12876243b9dcSRavi Bangoria 	}
12886243b9dcSRavi Bangoria 	return false;
12896243b9dcSRavi Bangoria }
12906243b9dcSRavi Bangoria 
12916243b9dcSRavi Bangoria /*
12926243b9dcSRavi Bangoria  * die_get_postprologue_addr - Search next address after function prologue
12936243b9dcSRavi Bangoria  * @entrypc_idx: entrypc index
12946243b9dcSRavi Bangoria  * @lines: Line records of single CU
12956243b9dcSRavi Bangoria  * @nr_lines: Number of @lines
12966243b9dcSRavi Bangoria  * @hignpc: high PC address of function
12976243b9dcSRavi Bangoria  * @postprologue_addr: Next address after function prologue (return value)
12986243b9dcSRavi Bangoria  *
12996243b9dcSRavi Bangoria  * Look for prologue-end marker. If there is no explicit marker, return
13006243b9dcSRavi Bangoria  * address of next line record or next source line.
13016243b9dcSRavi Bangoria  */
13026243b9dcSRavi Bangoria static bool die_get_postprologue_addr(unsigned long entrypc_idx,
13036243b9dcSRavi Bangoria 				      Dwarf_Lines *lines,
13046243b9dcSRavi Bangoria 				      unsigned long nr_lines,
13056243b9dcSRavi Bangoria 				      Dwarf_Addr highpc,
13066243b9dcSRavi Bangoria 				      Dwarf_Addr *postprologue_addr)
13076243b9dcSRavi Bangoria {
13086243b9dcSRavi Bangoria 	unsigned long i;
13096243b9dcSRavi Bangoria 	int entrypc_lno, lno;
13106243b9dcSRavi Bangoria 	Dwarf_Line *line;
13116243b9dcSRavi Bangoria 	Dwarf_Addr addr;
13126243b9dcSRavi Bangoria 	bool p_end;
13136243b9dcSRavi Bangoria 
13146243b9dcSRavi Bangoria 	/* entrypc_lno is actual source line number */
13156243b9dcSRavi Bangoria 	line = dwarf_onesrcline(lines, entrypc_idx);
13166243b9dcSRavi Bangoria 	if (dwarf_lineno(line, &entrypc_lno))
13176243b9dcSRavi Bangoria 		return false;
13186243b9dcSRavi Bangoria 
13196243b9dcSRavi Bangoria 	for (i = entrypc_idx; i < nr_lines; i++) {
13206243b9dcSRavi Bangoria 		line = dwarf_onesrcline(lines, i);
13216243b9dcSRavi Bangoria 
13226243b9dcSRavi Bangoria 		if (dwarf_lineaddr(line, &addr) ||
13236243b9dcSRavi Bangoria 		    dwarf_lineno(line, &lno)    ||
13246243b9dcSRavi Bangoria 		    dwarf_lineprologueend(line, &p_end))
13256243b9dcSRavi Bangoria 			return false;
13266243b9dcSRavi Bangoria 
13276243b9dcSRavi Bangoria 		/* highpc is exclusive. [entrypc,highpc) */
13286243b9dcSRavi Bangoria 		if (addr >= highpc)
13296243b9dcSRavi Bangoria 			break;
13306243b9dcSRavi Bangoria 
13316243b9dcSRavi Bangoria 		/* clang supports prologue-end marker */
13326243b9dcSRavi Bangoria 		if (p_end)
13336243b9dcSRavi Bangoria 			break;
13346243b9dcSRavi Bangoria 
13356243b9dcSRavi Bangoria 		/* Actual next line in source */
13366243b9dcSRavi Bangoria 		if (lno != entrypc_lno)
13376243b9dcSRavi Bangoria 			break;
13386243b9dcSRavi Bangoria 
13396243b9dcSRavi Bangoria 		/*
13406243b9dcSRavi Bangoria 		 * Single source line can have multiple line records.
13416243b9dcSRavi Bangoria 		 * For Example,
13426243b9dcSRavi Bangoria 		 *     void foo() { printf("hello\n"); }
13436243b9dcSRavi Bangoria 		 * contains two line records. One points to declaration and
13446243b9dcSRavi Bangoria 		 * other points to printf() line. Variable 'lno' won't get
13456243b9dcSRavi Bangoria 		 * incremented in this case but 'i' will.
13466243b9dcSRavi Bangoria 		 */
13476243b9dcSRavi Bangoria 		if (i != entrypc_idx)
13486243b9dcSRavi Bangoria 			break;
13496243b9dcSRavi Bangoria 	}
13506243b9dcSRavi Bangoria 
13516243b9dcSRavi Bangoria 	dwarf_lineaddr(line, postprologue_addr);
13526243b9dcSRavi Bangoria 	if (*postprologue_addr >= highpc)
13536243b9dcSRavi Bangoria 		dwarf_lineaddr(dwarf_onesrcline(lines, i - 1),
13546243b9dcSRavi Bangoria 			       postprologue_addr);
13556243b9dcSRavi Bangoria 
13566243b9dcSRavi Bangoria 	return true;
13576243b9dcSRavi Bangoria }
13586243b9dcSRavi Bangoria 
13596243b9dcSRavi Bangoria /*
13606243b9dcSRavi Bangoria  * die_skip_prologue - Use next address after prologue as probe location
13616243b9dcSRavi Bangoria  * @sp_die: a subprogram DIE
13626243b9dcSRavi Bangoria  * @cu_die: a CU DIE
13636243b9dcSRavi Bangoria  * @entrypc: entrypc of the function
13646243b9dcSRavi Bangoria  *
13656243b9dcSRavi Bangoria  * Function prologue prepares stack and registers before executing function
13666243b9dcSRavi Bangoria  * logic. When target program is compiled without optimization, function
13676243b9dcSRavi Bangoria  * parameter information is only valid after prologue. When we probe entrypc
13686243b9dcSRavi Bangoria  * of the function, and try to record function parameter, it contains
13696243b9dcSRavi Bangoria  * garbage value.
13706243b9dcSRavi Bangoria  */
13716243b9dcSRavi Bangoria void die_skip_prologue(Dwarf_Die *sp_die, Dwarf_Die *cu_die,
13726243b9dcSRavi Bangoria 		       Dwarf_Addr *entrypc)
13736243b9dcSRavi Bangoria {
13746243b9dcSRavi Bangoria 	size_t nr_lines = 0;
13756243b9dcSRavi Bangoria 	unsigned long entrypc_idx = 0;
13766243b9dcSRavi Bangoria 	Dwarf_Lines *lines = NULL;
13776243b9dcSRavi Bangoria 	Dwarf_Addr postprologue_addr;
13786243b9dcSRavi Bangoria 	Dwarf_Addr highpc;
13796243b9dcSRavi Bangoria 
13806243b9dcSRavi Bangoria 	if (dwarf_highpc(sp_die, &highpc))
13816243b9dcSRavi Bangoria 		return;
13826243b9dcSRavi Bangoria 
13836243b9dcSRavi Bangoria 	if (dwarf_getsrclines(cu_die, &lines, &nr_lines))
13846243b9dcSRavi Bangoria 		return;
13856243b9dcSRavi Bangoria 
13866243b9dcSRavi Bangoria 	if (!die_search_idx(lines, nr_lines, *entrypc, &entrypc_idx))
13876243b9dcSRavi Bangoria 		return;
13886243b9dcSRavi Bangoria 
13896243b9dcSRavi Bangoria 	if (!die_get_postprologue_addr(entrypc_idx, lines, nr_lines,
13906243b9dcSRavi Bangoria 				       highpc, &postprologue_addr))
13916243b9dcSRavi Bangoria 		return;
13926243b9dcSRavi Bangoria 
13936243b9dcSRavi Bangoria 	*entrypc = postprologue_addr;
13946243b9dcSRavi Bangoria }
1395