xref: /openbmc/linux/tools/perf/util/dwarf-aux.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
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  */
cu_find_realpath(Dwarf_Die * cu_die,const char * fname)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  */
cu_get_comp_dir(Dwarf_Die * cu_die)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 */
cu_getsrc_die(Dwarf_Die * cu_die,Dwarf_Addr addr)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++;
944d39c89fSIngo Molnar 	/* Going forward 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  */
cu_find_lineinfo(Dwarf_Die * cu_die,Dwarf_Addr addr,const char ** fname,int * lineno)11622a66551SYang Jihong int cu_find_lineinfo(Dwarf_Die *cu_die, Dwarf_Addr 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 
12322a66551SYang Jihong 	if (die_find_realfunc(cu_die, addr, &die_mem)
12457f95bf5SMasami Hiramatsu 	    && die_entrypc(&die_mem, &faddr) == 0 &&
12557f95bf5SMasami Hiramatsu 	    faddr == addr) {
126dc9a5d2cSMasami Hiramatsu (Google) 		*fname = die_get_decl_file(&die_mem);
12757f95bf5SMasami Hiramatsu 		dwarf_decl_line(&die_mem, lineno);
12857f95bf5SMasami Hiramatsu 		goto out;
12957f95bf5SMasami Hiramatsu 	}
13057f95bf5SMasami Hiramatsu 
13122a66551SYang Jihong 	line = cu_getsrc_die(cu_die, 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:
14098e4c68dSMasami Hiramatsu (Google) 	return (*lineno && *fname) ? *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  */
cu_walk_functions_at(Dwarf_Die * cu_die,Dwarf_Addr addr,int (* callback)(Dwarf_Die *,void *),void * data)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  *
1804d39c89fSIngo Molnar  * Get the linkage name attribute of given @dw_die.
181d5a00296SMasami Hiramatsu  * For C++ binary, the linkage name will be the mangled symbol.
182d5a00296SMasami Hiramatsu  */
die_get_linkage_name(Dwarf_Die * dw_die)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  */
die_compare_name(Dwarf_Die * dw_die,const char * tname)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  */
die_match_name(Dwarf_Die * dw_die,const char * glob)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  */
die_get_call_lineno(Dwarf_Die * in_die)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  */
die_get_type(Dwarf_Die * vr_die,Dwarf_Die * die_mem)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 */
__die_get_real_type(Dwarf_Die * vr_die,Dwarf_Die * die_mem)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  */
die_get_real_type(Dwarf_Die * vr_die,Dwarf_Die * die_mem)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 */
die_get_attr_udata(Dwarf_Die * tp_die,unsigned int attr_name,Dwarf_Word * result)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 
311f828929aSMasami Hiramatsu (Google) 	if (dwarf_attr_integrate(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 
318e0d153c6SMasami Hiramatsu /**
319e0d153c6SMasami Hiramatsu  * die_is_signed_type - Check whether a type DIE is signed or not
320e0d153c6SMasami Hiramatsu  * @tp_die: a DIE of a type
321e0d153c6SMasami Hiramatsu  *
322e0d153c6SMasami Hiramatsu  * Get the encoding of @tp_die and return true if the encoding
323e0d153c6SMasami Hiramatsu  * is signed.
324e0d153c6SMasami Hiramatsu  */
die_is_signed_type(Dwarf_Die * tp_die)325e0d153c6SMasami Hiramatsu bool die_is_signed_type(Dwarf_Die *tp_die)
326e0d153c6SMasami Hiramatsu {
327e0d153c6SMasami Hiramatsu 	Dwarf_Word ret;
328e0d153c6SMasami Hiramatsu 
329e0d153c6SMasami Hiramatsu 	if (die_get_attr_udata(tp_die, DW_AT_encoding, &ret))
330e0d153c6SMasami Hiramatsu 		return false;
331e0d153c6SMasami Hiramatsu 
332e0d153c6SMasami Hiramatsu 	return (ret == DW_ATE_signed_char || ret == DW_ATE_signed ||
333e0d153c6SMasami Hiramatsu 		ret == DW_ATE_signed_fixed);
334e0d153c6SMasami Hiramatsu }
335e0d153c6SMasami Hiramatsu 
336e0d153c6SMasami Hiramatsu /**
3370dbb1cacSMasami Hiramatsu  * die_is_func_def - Ensure that this DIE is a subprogram and definition
3380dbb1cacSMasami Hiramatsu  * @dw_die: a DIE
3390dbb1cacSMasami Hiramatsu  *
3400dbb1cacSMasami Hiramatsu  * Ensure that this DIE is a subprogram and NOT a declaration. This
3410dbb1cacSMasami Hiramatsu  * returns true if @dw_die is a function definition.
3420dbb1cacSMasami Hiramatsu  **/
die_is_func_def(Dwarf_Die * dw_die)3430dbb1cacSMasami Hiramatsu bool die_is_func_def(Dwarf_Die *dw_die)
3440dbb1cacSMasami Hiramatsu {
3450dbb1cacSMasami Hiramatsu 	Dwarf_Attribute attr;
346a9ffd048SMasami Hiramatsu 	Dwarf_Addr addr = 0;
3470dbb1cacSMasami Hiramatsu 
348a9ffd048SMasami Hiramatsu 	if (dwarf_tag(dw_die) != DW_TAG_subprogram)
349a9ffd048SMasami Hiramatsu 		return false;
350a9ffd048SMasami Hiramatsu 
351a9ffd048SMasami Hiramatsu 	if (dwarf_attr(dw_die, DW_AT_declaration, &attr))
352a9ffd048SMasami Hiramatsu 		return false;
353a9ffd048SMasami Hiramatsu 
354a9ffd048SMasami Hiramatsu 	/*
355a9ffd048SMasami Hiramatsu 	 * DW_AT_declaration can be lost from function declaration
356a9ffd048SMasami Hiramatsu 	 * by gcc's bug #97060.
357a9ffd048SMasami Hiramatsu 	 * So we need to check this subprogram DIE has DW_AT_inline
358a9ffd048SMasami Hiramatsu 	 * or an entry address.
359a9ffd048SMasami Hiramatsu 	 */
360a9ffd048SMasami Hiramatsu 	if (!dwarf_attr(dw_die, DW_AT_inline, &attr) &&
361a9ffd048SMasami Hiramatsu 	    die_entrypc(dw_die, &addr) < 0)
362a9ffd048SMasami Hiramatsu 		return false;
363a9ffd048SMasami Hiramatsu 
364a9ffd048SMasami Hiramatsu 	return true;
3650dbb1cacSMasami Hiramatsu }
3660dbb1cacSMasami Hiramatsu 
3670dbb1cacSMasami Hiramatsu /**
36891e2f539SMasami Hiramatsu  * die_entrypc - Returns entry PC (the lowest address) of a DIE
36991e2f539SMasami Hiramatsu  * @dw_die: a DIE
37091e2f539SMasami Hiramatsu  * @addr: where to store entry PC
37191e2f539SMasami Hiramatsu  *
37291e2f539SMasami Hiramatsu  * Since dwarf_entrypc() does not return entry PC if the DIE has only address
37391e2f539SMasami Hiramatsu  * range, we have to use this to retrieve the lowest address from the address
37491e2f539SMasami Hiramatsu  * range attribute.
37591e2f539SMasami Hiramatsu  */
die_entrypc(Dwarf_Die * dw_die,Dwarf_Addr * addr)37691e2f539SMasami Hiramatsu int die_entrypc(Dwarf_Die *dw_die, Dwarf_Addr *addr)
37791e2f539SMasami Hiramatsu {
37891e2f539SMasami Hiramatsu 	Dwarf_Addr base, end;
379ab4200c1SMasami Hiramatsu 	Dwarf_Attribute attr;
38091e2f539SMasami Hiramatsu 
38191e2f539SMasami Hiramatsu 	if (!addr)
38291e2f539SMasami Hiramatsu 		return -EINVAL;
38391e2f539SMasami Hiramatsu 
38491e2f539SMasami Hiramatsu 	if (dwarf_entrypc(dw_die, addr) == 0)
38591e2f539SMasami Hiramatsu 		return 0;
38691e2f539SMasami Hiramatsu 
387ab4200c1SMasami Hiramatsu 	/*
388ab4200c1SMasami Hiramatsu 	 *  Since the dwarf_ranges() will return 0 if there is no
389ab4200c1SMasami Hiramatsu 	 * DW_AT_ranges attribute, we should check it first.
390ab4200c1SMasami Hiramatsu 	 */
391ab4200c1SMasami Hiramatsu 	if (!dwarf_attr(dw_die, DW_AT_ranges, &attr))
392ab4200c1SMasami Hiramatsu 		return -ENOENT;
393ab4200c1SMasami Hiramatsu 
39491e2f539SMasami Hiramatsu 	return dwarf_ranges(dw_die, 0, &base, addr, &end) < 0 ? -ENOENT : 0;
39591e2f539SMasami Hiramatsu }
39691e2f539SMasami Hiramatsu 
39791e2f539SMasami Hiramatsu /**
398e1ecbbc3SMasami Hiramatsu  * die_is_func_instance - Ensure that this DIE is an instance of a subprogram
399e1ecbbc3SMasami Hiramatsu  * @dw_die: a DIE
400e1ecbbc3SMasami Hiramatsu  *
401e1ecbbc3SMasami Hiramatsu  * Ensure that this DIE is an instance (which has an entry address).
402da6cb952SMasami Hiramatsu  * This returns true if @dw_die is a function instance. If not, the @dw_die
403da6cb952SMasami Hiramatsu  * must be a prototype. You can use die_walk_instances() to find actual
404da6cb952SMasami Hiramatsu  * instances.
405e1ecbbc3SMasami Hiramatsu  **/
die_is_func_instance(Dwarf_Die * dw_die)406e1ecbbc3SMasami Hiramatsu bool die_is_func_instance(Dwarf_Die *dw_die)
407e1ecbbc3SMasami Hiramatsu {
408e1ecbbc3SMasami Hiramatsu 	Dwarf_Addr tmp;
409b77afa1fSMasami Hiramatsu 	Dwarf_Attribute attr_mem;
410da6cb952SMasami Hiramatsu 	int tag = dwarf_tag(dw_die);
411e1ecbbc3SMasami Hiramatsu 
412da6cb952SMasami Hiramatsu 	if (tag != DW_TAG_subprogram &&
413da6cb952SMasami Hiramatsu 	    tag != DW_TAG_inlined_subroutine)
414da6cb952SMasami Hiramatsu 		return false;
415da6cb952SMasami Hiramatsu 
416da6cb952SMasami Hiramatsu 	return dwarf_entrypc(dw_die, &tmp) == 0 ||
417da6cb952SMasami Hiramatsu 		dwarf_attr(dw_die, DW_AT_ranges, &attr_mem) != NULL;
418e1ecbbc3SMasami Hiramatsu }
419b77afa1fSMasami Hiramatsu 
420e1ecbbc3SMasami Hiramatsu /**
421e0d153c6SMasami Hiramatsu  * die_get_data_member_location - Get the data-member offset
422e0d153c6SMasami Hiramatsu  * @mb_die: a DIE of a member of a data structure
423e0d153c6SMasami Hiramatsu  * @offs: The offset of the member in the data structure
424e0d153c6SMasami Hiramatsu  *
425e0d153c6SMasami Hiramatsu  * Get the offset of @mb_die in the data structure including @mb_die, and
426e0d153c6SMasami Hiramatsu  * stores result offset to @offs. If any error occurs this returns errno.
427e0d153c6SMasami Hiramatsu  */
die_get_data_member_location(Dwarf_Die * mb_die,Dwarf_Word * offs)428e0d153c6SMasami Hiramatsu int die_get_data_member_location(Dwarf_Die *mb_die, Dwarf_Word *offs)
429e0d153c6SMasami Hiramatsu {
430e0d153c6SMasami Hiramatsu 	Dwarf_Attribute attr;
431e0d153c6SMasami Hiramatsu 	Dwarf_Op *expr;
432e0d153c6SMasami Hiramatsu 	size_t nexpr;
433e0d153c6SMasami Hiramatsu 	int ret;
434e0d153c6SMasami Hiramatsu 
435e0d153c6SMasami Hiramatsu 	if (dwarf_attr(mb_die, DW_AT_data_member_location, &attr) == NULL)
436e0d153c6SMasami Hiramatsu 		return -ENOENT;
437e0d153c6SMasami Hiramatsu 
438e0d153c6SMasami Hiramatsu 	if (dwarf_formudata(&attr, offs) != 0) {
439e0d153c6SMasami Hiramatsu 		/* DW_AT_data_member_location should be DW_OP_plus_uconst */
440e0d153c6SMasami Hiramatsu 		ret = dwarf_getlocation(&attr, &expr, &nexpr);
441e0d153c6SMasami Hiramatsu 		if (ret < 0 || nexpr == 0)
442e0d153c6SMasami Hiramatsu 			return -ENOENT;
443e0d153c6SMasami Hiramatsu 
444e0d153c6SMasami Hiramatsu 		if (expr[0].atom != DW_OP_plus_uconst || nexpr != 1) {
445e0d153c6SMasami Hiramatsu 			pr_debug("Unable to get offset:Unexpected OP %x (%zd)\n",
446e0d153c6SMasami Hiramatsu 				 expr[0].atom, nexpr);
447e0d153c6SMasami Hiramatsu 			return -ENOTSUP;
448e0d153c6SMasami Hiramatsu 		}
449e0d153c6SMasami Hiramatsu 		*offs = (Dwarf_Word)expr[0].number;
450e0d153c6SMasami Hiramatsu 	}
451e0d153c6SMasami Hiramatsu 	return 0;
452e0d153c6SMasami Hiramatsu }
453e0d153c6SMasami Hiramatsu 
454b0e9cb28SMasami Hiramatsu /* Get the call file index number in CU DIE */
die_get_call_fileno(Dwarf_Die * in_die)455b0e9cb28SMasami Hiramatsu static int die_get_call_fileno(Dwarf_Die *in_die)
456b0e9cb28SMasami Hiramatsu {
457a9dfc46cSMasami Hiramatsu (Google) 	Dwarf_Word idx;
458b0e9cb28SMasami Hiramatsu 
459a9dfc46cSMasami Hiramatsu (Google) 	if (die_get_attr_udata(in_die, DW_AT_call_file, &idx) == 0)
460b0e9cb28SMasami Hiramatsu 		return (int)idx;
461b0e9cb28SMasami Hiramatsu 	else
462b0e9cb28SMasami Hiramatsu 		return -ENOENT;
463b0e9cb28SMasami Hiramatsu }
464b0e9cb28SMasami Hiramatsu 
4653f4460a2SMasami Hiramatsu /* Get the declared file index number in CU DIE */
die_get_decl_fileno(Dwarf_Die * pdie)4663f4460a2SMasami Hiramatsu static int die_get_decl_fileno(Dwarf_Die *pdie)
4673f4460a2SMasami Hiramatsu {
468a9dfc46cSMasami Hiramatsu (Google) 	Dwarf_Word idx;
4693f4460a2SMasami Hiramatsu 
470a9dfc46cSMasami Hiramatsu (Google) 	if (die_get_attr_udata(pdie, DW_AT_decl_file, &idx) == 0)
4713f4460a2SMasami Hiramatsu 		return (int)idx;
4723f4460a2SMasami Hiramatsu 	else
4733f4460a2SMasami Hiramatsu 		return -ENOENT;
4743f4460a2SMasami Hiramatsu }
4753f4460a2SMasami Hiramatsu 
476dc9a5d2cSMasami Hiramatsu (Google) /* Return the file name by index */
die_get_file_name(Dwarf_Die * dw_die,int idx)477dc9a5d2cSMasami Hiramatsu (Google) static const char *die_get_file_name(Dwarf_Die *dw_die, int idx)
478dc9a5d2cSMasami Hiramatsu (Google) {
479dc9a5d2cSMasami Hiramatsu (Google) 	Dwarf_Die cu_die;
480dc9a5d2cSMasami Hiramatsu (Google) 	Dwarf_Files *files;
481*c66e1c68SGeorg Müller 	Dwarf_Attribute attr_mem;
482dc9a5d2cSMasami Hiramatsu (Google) 
483*c66e1c68SGeorg Müller 	if (idx < 0 || !dwarf_attr_integrate(dw_die, DW_AT_decl_file, &attr_mem) ||
484*c66e1c68SGeorg Müller 	    !dwarf_cu_die(attr_mem.cu, &cu_die, NULL, NULL, NULL, NULL, NULL, NULL) ||
485dc9a5d2cSMasami Hiramatsu (Google) 	    dwarf_getsrcfiles(&cu_die, &files, NULL) != 0)
486dc9a5d2cSMasami Hiramatsu (Google) 		return NULL;
487dc9a5d2cSMasami Hiramatsu (Google) 
488dc9a5d2cSMasami Hiramatsu (Google) 	return dwarf_filesrc(files, idx, NULL, NULL);
489dc9a5d2cSMasami Hiramatsu (Google) }
490dc9a5d2cSMasami Hiramatsu (Google) 
491b0e9cb28SMasami Hiramatsu /**
492b0e9cb28SMasami Hiramatsu  * die_get_call_file - Get callsite file name of inlined function instance
493b0e9cb28SMasami Hiramatsu  * @in_die: a DIE of an inlined function instance
494b0e9cb28SMasami Hiramatsu  *
495b0e9cb28SMasami Hiramatsu  * Get call-site file name of @in_die. This means from which file the inline
496b0e9cb28SMasami Hiramatsu  * function is called.
497b0e9cb28SMasami Hiramatsu  */
die_get_call_file(Dwarf_Die * in_die)498b0e9cb28SMasami Hiramatsu const char *die_get_call_file(Dwarf_Die *in_die)
499b0e9cb28SMasami Hiramatsu {
500dc9a5d2cSMasami Hiramatsu (Google) 	return die_get_file_name(in_die, die_get_call_fileno(in_die));
501b0e9cb28SMasami Hiramatsu }
502b0e9cb28SMasami Hiramatsu 
503dc9a5d2cSMasami Hiramatsu (Google) /**
504dc9a5d2cSMasami Hiramatsu (Google)  * die_get_decl_file - Find the declared file name of this DIE
505dc9a5d2cSMasami Hiramatsu (Google)  * @dw_die: a DIE for something declared.
506dc9a5d2cSMasami Hiramatsu (Google)  *
507dc9a5d2cSMasami Hiramatsu (Google)  * Get declared file name of @dw_die.
508dc9a5d2cSMasami Hiramatsu (Google)  * NOTE: Since some version of clang DWARF5 implementation incorrectly uses
509dc9a5d2cSMasami Hiramatsu (Google)  * file index 0 for DW_AT_decl_file, die_get_decl_file() will return NULL for
510dc9a5d2cSMasami Hiramatsu (Google)  * such cases. Use this function instead.
511dc9a5d2cSMasami Hiramatsu (Google)  */
die_get_decl_file(Dwarf_Die * dw_die)512dc9a5d2cSMasami Hiramatsu (Google) const char *die_get_decl_file(Dwarf_Die *dw_die)
513dc9a5d2cSMasami Hiramatsu (Google) {
514dc9a5d2cSMasami Hiramatsu (Google) 	return die_get_file_name(dw_die, die_get_decl_fileno(dw_die));
515dc9a5d2cSMasami Hiramatsu (Google) }
516b0e9cb28SMasami Hiramatsu 
517e0d153c6SMasami Hiramatsu /**
518e0d153c6SMasami Hiramatsu  * die_find_child - Generic DIE search function in DIE tree
519e0d153c6SMasami Hiramatsu  * @rt_die: a root DIE
520e0d153c6SMasami Hiramatsu  * @callback: a callback function
521e0d153c6SMasami Hiramatsu  * @data: a user data passed to the callback function
522e0d153c6SMasami Hiramatsu  * @die_mem: a buffer for result DIE
523e0d153c6SMasami Hiramatsu  *
524e0d153c6SMasami Hiramatsu  * Trace DIE tree from @rt_die and call @callback for each child DIE.
525e0d153c6SMasami Hiramatsu  * If @callback returns DIE_FIND_CB_END, this stores the DIE into
526e0d153c6SMasami Hiramatsu  * @die_mem and returns it. If @callback returns DIE_FIND_CB_CONTINUE,
527e0d153c6SMasami Hiramatsu  * this continues to trace the tree. Optionally, @callback can return
528e0d153c6SMasami Hiramatsu  * DIE_FIND_CB_CHILD and DIE_FIND_CB_SIBLING, those means trace only
529e0d153c6SMasami Hiramatsu  * the children and trace only the siblings respectively.
530e0d153c6SMasami Hiramatsu  * Returns NULL if @callback can't find any appropriate DIE.
531e0d153c6SMasami Hiramatsu  */
die_find_child(Dwarf_Die * rt_die,int (* callback)(Dwarf_Die *,void *),void * data,Dwarf_Die * die_mem)532e0d153c6SMasami Hiramatsu Dwarf_Die *die_find_child(Dwarf_Die *rt_die,
533e0d153c6SMasami Hiramatsu 			  int (*callback)(Dwarf_Die *, void *),
534e0d153c6SMasami Hiramatsu 			  void *data, Dwarf_Die *die_mem)
535e0d153c6SMasami Hiramatsu {
536e0d153c6SMasami Hiramatsu 	Dwarf_Die child_die;
537e0d153c6SMasami Hiramatsu 	int ret;
538e0d153c6SMasami Hiramatsu 
539e0d153c6SMasami Hiramatsu 	ret = dwarf_child(rt_die, die_mem);
540e0d153c6SMasami Hiramatsu 	if (ret != 0)
541e0d153c6SMasami Hiramatsu 		return NULL;
542e0d153c6SMasami Hiramatsu 
543e0d153c6SMasami Hiramatsu 	do {
544e0d153c6SMasami Hiramatsu 		ret = callback(die_mem, data);
545e0d153c6SMasami Hiramatsu 		if (ret == DIE_FIND_CB_END)
546e0d153c6SMasami Hiramatsu 			return die_mem;
547e0d153c6SMasami Hiramatsu 
548e0d153c6SMasami Hiramatsu 		if ((ret & DIE_FIND_CB_CHILD) &&
549e0d153c6SMasami Hiramatsu 		    die_find_child(die_mem, callback, data, &child_die)) {
550e0d153c6SMasami Hiramatsu 			memcpy(die_mem, &child_die, sizeof(Dwarf_Die));
551e0d153c6SMasami Hiramatsu 			return die_mem;
552e0d153c6SMasami Hiramatsu 		}
553e0d153c6SMasami Hiramatsu 	} while ((ret & DIE_FIND_CB_SIBLING) &&
554e0d153c6SMasami Hiramatsu 		 dwarf_siblingof(die_mem, die_mem) == 0);
555e0d153c6SMasami Hiramatsu 
556e0d153c6SMasami Hiramatsu 	return NULL;
557e0d153c6SMasami Hiramatsu }
558e0d153c6SMasami Hiramatsu 
559e0d153c6SMasami Hiramatsu struct __addr_die_search_param {
560e0d153c6SMasami Hiramatsu 	Dwarf_Addr	addr;
561e0d153c6SMasami Hiramatsu 	Dwarf_Die	*die_mem;
562e0d153c6SMasami Hiramatsu };
563e0d153c6SMasami Hiramatsu 
__die_search_func_tail_cb(Dwarf_Die * fn_die,void * data)564d4c537e6SNaveen N. Rao static int __die_search_func_tail_cb(Dwarf_Die *fn_die, void *data)
565d4c537e6SNaveen N. Rao {
566d4c537e6SNaveen N. Rao 	struct __addr_die_search_param *ad = data;
567d4c537e6SNaveen N. Rao 	Dwarf_Addr addr = 0;
568d4c537e6SNaveen N. Rao 
569d4c537e6SNaveen N. Rao 	if (dwarf_tag(fn_die) == DW_TAG_subprogram &&
570d4c537e6SNaveen N. Rao 	    !dwarf_highpc(fn_die, &addr) &&
571d4c537e6SNaveen N. Rao 	    addr == ad->addr) {
572d4c537e6SNaveen N. Rao 		memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die));
573d4c537e6SNaveen N. Rao 		return DWARF_CB_ABORT;
574d4c537e6SNaveen N. Rao 	}
575d4c537e6SNaveen N. Rao 	return DWARF_CB_OK;
576d4c537e6SNaveen N. Rao }
577d4c537e6SNaveen N. Rao 
578d4c537e6SNaveen N. Rao /**
579d4c537e6SNaveen N. Rao  * die_find_tailfunc - Search for a non-inlined function with tail call at
580d4c537e6SNaveen N. Rao  * given address
581d4c537e6SNaveen N. Rao  * @cu_die: a CU DIE which including @addr
582d4c537e6SNaveen N. Rao  * @addr: target address
583d4c537e6SNaveen N. Rao  * @die_mem: a buffer for result DIE
584d4c537e6SNaveen N. Rao  *
585d4c537e6SNaveen N. Rao  * Search for a non-inlined function DIE with tail call at @addr. Stores the
586d4c537e6SNaveen N. Rao  * DIE to @die_mem and returns it if found. Returns NULL if failed.
587d4c537e6SNaveen N. Rao  */
die_find_tailfunc(Dwarf_Die * cu_die,Dwarf_Addr addr,Dwarf_Die * die_mem)588d4c537e6SNaveen N. Rao Dwarf_Die *die_find_tailfunc(Dwarf_Die *cu_die, Dwarf_Addr addr,
589d4c537e6SNaveen N. Rao 				    Dwarf_Die *die_mem)
590d4c537e6SNaveen N. Rao {
591d4c537e6SNaveen N. Rao 	struct __addr_die_search_param ad;
592d4c537e6SNaveen N. Rao 	ad.addr = addr;
593d4c537e6SNaveen N. Rao 	ad.die_mem = die_mem;
594d4c537e6SNaveen N. Rao 	/* dwarf_getscopes can't find subprogram. */
595d4c537e6SNaveen N. Rao 	if (!dwarf_getfuncs(cu_die, __die_search_func_tail_cb, &ad, 0))
596d4c537e6SNaveen N. Rao 		return NULL;
597d4c537e6SNaveen N. Rao 	else
598d4c537e6SNaveen N. Rao 		return die_mem;
599d4c537e6SNaveen N. Rao }
600d4c537e6SNaveen N. Rao 
601e0d153c6SMasami Hiramatsu /* die_find callback for non-inlined function search */
__die_search_func_cb(Dwarf_Die * fn_die,void * data)602e0d153c6SMasami Hiramatsu static int __die_search_func_cb(Dwarf_Die *fn_die, void *data)
603e0d153c6SMasami Hiramatsu {
604e0d153c6SMasami Hiramatsu 	struct __addr_die_search_param *ad = data;
605e0d153c6SMasami Hiramatsu 
6060dbb1cacSMasami Hiramatsu 	/*
6070dbb1cacSMasami Hiramatsu 	 * Since a declaration entry doesn't has given pc, this always returns
6080dbb1cacSMasami Hiramatsu 	 * function definition entry.
6090dbb1cacSMasami Hiramatsu 	 */
610e0d153c6SMasami Hiramatsu 	if (dwarf_tag(fn_die) == DW_TAG_subprogram &&
611e0d153c6SMasami Hiramatsu 	    dwarf_haspc(fn_die, ad->addr)) {
612e0d153c6SMasami Hiramatsu 		memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die));
613e0d153c6SMasami Hiramatsu 		return DWARF_CB_ABORT;
614e0d153c6SMasami Hiramatsu 	}
615e0d153c6SMasami Hiramatsu 	return DWARF_CB_OK;
616e0d153c6SMasami Hiramatsu }
617e0d153c6SMasami Hiramatsu 
618e0d153c6SMasami Hiramatsu /**
619e0d153c6SMasami Hiramatsu  * die_find_realfunc - Search a non-inlined function at given address
620e0d153c6SMasami Hiramatsu  * @cu_die: a CU DIE which including @addr
621e0d153c6SMasami Hiramatsu  * @addr: target address
622e0d153c6SMasami Hiramatsu  * @die_mem: a buffer for result DIE
623e0d153c6SMasami Hiramatsu  *
624e0d153c6SMasami Hiramatsu  * Search a non-inlined function DIE which includes @addr. Stores the
625e08cfd4bSMasami Hiramatsu  * DIE to @die_mem and returns it if found. Returns NULL if failed.
626e0d153c6SMasami Hiramatsu  */
die_find_realfunc(Dwarf_Die * cu_die,Dwarf_Addr addr,Dwarf_Die * die_mem)627e0d153c6SMasami Hiramatsu Dwarf_Die *die_find_realfunc(Dwarf_Die *cu_die, Dwarf_Addr addr,
628e0d153c6SMasami Hiramatsu 				    Dwarf_Die *die_mem)
629e0d153c6SMasami Hiramatsu {
630e0d153c6SMasami Hiramatsu 	struct __addr_die_search_param ad;
631e0d153c6SMasami Hiramatsu 	ad.addr = addr;
632e0d153c6SMasami Hiramatsu 	ad.die_mem = die_mem;
633e0d153c6SMasami Hiramatsu 	/* dwarf_getscopes can't find subprogram. */
634e0d153c6SMasami Hiramatsu 	if (!dwarf_getfuncs(cu_die, __die_search_func_cb, &ad, 0))
635e0d153c6SMasami Hiramatsu 		return NULL;
636e0d153c6SMasami Hiramatsu 	else
637e0d153c6SMasami Hiramatsu 		return die_mem;
638e0d153c6SMasami Hiramatsu }
639e0d153c6SMasami Hiramatsu 
640e0d153c6SMasami Hiramatsu /* die_find callback for inline function search */
__die_find_inline_cb(Dwarf_Die * die_mem,void * data)641e0d153c6SMasami Hiramatsu static int __die_find_inline_cb(Dwarf_Die *die_mem, void *data)
642e0d153c6SMasami Hiramatsu {
643e0d153c6SMasami Hiramatsu 	Dwarf_Addr *addr = data;
644e0d153c6SMasami Hiramatsu 
645e0d153c6SMasami Hiramatsu 	if (dwarf_tag(die_mem) == DW_TAG_inlined_subroutine &&
646e0d153c6SMasami Hiramatsu 	    dwarf_haspc(die_mem, *addr))
647e0d153c6SMasami Hiramatsu 		return DIE_FIND_CB_END;
648e0d153c6SMasami Hiramatsu 
649e0d153c6SMasami Hiramatsu 	return DIE_FIND_CB_CONTINUE;
650e0d153c6SMasami Hiramatsu }
651e0d153c6SMasami Hiramatsu 
652e0d153c6SMasami Hiramatsu /**
653e08cfd4bSMasami Hiramatsu  * die_find_top_inlinefunc - Search the top inlined function at given address
654e08cfd4bSMasami Hiramatsu  * @sp_die: a subprogram DIE which including @addr
655e0d153c6SMasami Hiramatsu  * @addr: target address
656e0d153c6SMasami Hiramatsu  * @die_mem: a buffer for result DIE
657e0d153c6SMasami Hiramatsu  *
658e0d153c6SMasami Hiramatsu  * Search an inlined function DIE which includes @addr. Stores the
659e08cfd4bSMasami Hiramatsu  * DIE to @die_mem and returns it if found. Returns NULL if failed.
660e08cfd4bSMasami Hiramatsu  * Even if several inlined functions are expanded recursively, this
661e08cfd4bSMasami Hiramatsu  * doesn't trace it down, and returns the topmost one.
662e08cfd4bSMasami Hiramatsu  */
die_find_top_inlinefunc(Dwarf_Die * sp_die,Dwarf_Addr addr,Dwarf_Die * die_mem)663e08cfd4bSMasami Hiramatsu Dwarf_Die *die_find_top_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
664e08cfd4bSMasami Hiramatsu 				   Dwarf_Die *die_mem)
665e08cfd4bSMasami Hiramatsu {
666e08cfd4bSMasami Hiramatsu 	return die_find_child(sp_die, __die_find_inline_cb, &addr, die_mem);
667e08cfd4bSMasami Hiramatsu }
668e08cfd4bSMasami Hiramatsu 
669e08cfd4bSMasami Hiramatsu /**
670e08cfd4bSMasami Hiramatsu  * die_find_inlinefunc - Search an inlined function at given address
671e08cfd4bSMasami Hiramatsu  * @sp_die: a subprogram DIE which including @addr
672e08cfd4bSMasami Hiramatsu  * @addr: target address
673e08cfd4bSMasami Hiramatsu  * @die_mem: a buffer for result DIE
674e08cfd4bSMasami Hiramatsu  *
675e08cfd4bSMasami Hiramatsu  * Search an inlined function DIE which includes @addr. Stores the
676e08cfd4bSMasami Hiramatsu  * DIE to @die_mem and returns it if found. Returns NULL if failed.
677e0d153c6SMasami Hiramatsu  * If several inlined functions are expanded recursively, this trace
678e08cfd4bSMasami Hiramatsu  * it down and returns deepest one.
679e0d153c6SMasami Hiramatsu  */
die_find_inlinefunc(Dwarf_Die * sp_die,Dwarf_Addr addr,Dwarf_Die * die_mem)680e0d153c6SMasami Hiramatsu Dwarf_Die *die_find_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
681e0d153c6SMasami Hiramatsu 			       Dwarf_Die *die_mem)
682e0d153c6SMasami Hiramatsu {
683e0d153c6SMasami Hiramatsu 	Dwarf_Die tmp_die;
684e0d153c6SMasami Hiramatsu 
685e0d153c6SMasami Hiramatsu 	sp_die = die_find_child(sp_die, __die_find_inline_cb, &addr, &tmp_die);
686e0d153c6SMasami Hiramatsu 	if (!sp_die)
687e0d153c6SMasami Hiramatsu 		return NULL;
688e0d153c6SMasami Hiramatsu 
689e0d153c6SMasami Hiramatsu 	/* Inlined function could be recursive. Trace it until fail */
690e0d153c6SMasami Hiramatsu 	while (sp_die) {
691e0d153c6SMasami Hiramatsu 		memcpy(die_mem, sp_die, sizeof(Dwarf_Die));
692e0d153c6SMasami Hiramatsu 		sp_die = die_find_child(sp_die, __die_find_inline_cb, &addr,
693e0d153c6SMasami Hiramatsu 					&tmp_die);
694e0d153c6SMasami Hiramatsu 	}
695e0d153c6SMasami Hiramatsu 
696e0d153c6SMasami Hiramatsu 	return die_mem;
697e0d153c6SMasami Hiramatsu }
698e0d153c6SMasami Hiramatsu 
699db0d2c64SMasami Hiramatsu struct __instance_walk_param {
700db0d2c64SMasami Hiramatsu 	void    *addr;
701db0d2c64SMasami Hiramatsu 	int	(*callback)(Dwarf_Die *, void *);
702db0d2c64SMasami Hiramatsu 	void    *data;
703db0d2c64SMasami Hiramatsu 	int	retval;
704db0d2c64SMasami Hiramatsu };
705db0d2c64SMasami Hiramatsu 
__die_walk_instances_cb(Dwarf_Die * inst,void * data)706db0d2c64SMasami Hiramatsu static int __die_walk_instances_cb(Dwarf_Die *inst, void *data)
707db0d2c64SMasami Hiramatsu {
708db0d2c64SMasami Hiramatsu 	struct __instance_walk_param *iwp = data;
709db0d2c64SMasami Hiramatsu 	Dwarf_Attribute attr_mem;
710db0d2c64SMasami Hiramatsu 	Dwarf_Die origin_mem;
711db0d2c64SMasami Hiramatsu 	Dwarf_Attribute *attr;
712db0d2c64SMasami Hiramatsu 	Dwarf_Die *origin;
7133f4460a2SMasami Hiramatsu 	int tmp;
714db0d2c64SMasami Hiramatsu 
715da6cb952SMasami Hiramatsu 	if (!die_is_func_instance(inst))
716da6cb952SMasami Hiramatsu 		return DIE_FIND_CB_CONTINUE;
717da6cb952SMasami Hiramatsu 
718db0d2c64SMasami Hiramatsu 	attr = dwarf_attr(inst, DW_AT_abstract_origin, &attr_mem);
719db0d2c64SMasami Hiramatsu 	if (attr == NULL)
720db0d2c64SMasami Hiramatsu 		return DIE_FIND_CB_CONTINUE;
721db0d2c64SMasami Hiramatsu 
722db0d2c64SMasami Hiramatsu 	origin = dwarf_formref_die(attr, &origin_mem);
723db0d2c64SMasami Hiramatsu 	if (origin == NULL || origin->addr != iwp->addr)
724db0d2c64SMasami Hiramatsu 		return DIE_FIND_CB_CONTINUE;
725db0d2c64SMasami Hiramatsu 
7263f4460a2SMasami Hiramatsu 	/* Ignore redundant instances */
7273f4460a2SMasami Hiramatsu 	if (dwarf_tag(inst) == DW_TAG_inlined_subroutine) {
7283f4460a2SMasami Hiramatsu 		dwarf_decl_line(origin, &tmp);
7293f4460a2SMasami Hiramatsu 		if (die_get_call_lineno(inst) == tmp) {
7303f4460a2SMasami Hiramatsu 			tmp = die_get_decl_fileno(origin);
7313f4460a2SMasami Hiramatsu 			if (die_get_call_fileno(inst) == tmp)
7323f4460a2SMasami Hiramatsu 				return DIE_FIND_CB_CONTINUE;
7333f4460a2SMasami Hiramatsu 		}
7343f4460a2SMasami Hiramatsu 	}
7353f4460a2SMasami Hiramatsu 
736db0d2c64SMasami Hiramatsu 	iwp->retval = iwp->callback(inst, iwp->data);
737db0d2c64SMasami Hiramatsu 
738db0d2c64SMasami Hiramatsu 	return (iwp->retval) ? DIE_FIND_CB_END : DIE_FIND_CB_CONTINUE;
739db0d2c64SMasami Hiramatsu }
740db0d2c64SMasami Hiramatsu 
741db0d2c64SMasami Hiramatsu /**
742db0d2c64SMasami Hiramatsu  * die_walk_instances - Walk on instances of given DIE
743db0d2c64SMasami Hiramatsu  * @or_die: an abstract original DIE
744db0d2c64SMasami Hiramatsu  * @callback: a callback function which is called with instance DIE
745db0d2c64SMasami Hiramatsu  * @data: user data
746db0d2c64SMasami Hiramatsu  *
747db0d2c64SMasami Hiramatsu  * Walk on the instances of give @in_die. @in_die must be an inlined function
7484d39c89fSIngo Molnar  * declaration. This returns the return value of @callback if it returns
749db0d2c64SMasami Hiramatsu  * non-zero value, or -ENOENT if there is no instance.
750db0d2c64SMasami Hiramatsu  */
die_walk_instances(Dwarf_Die * or_die,int (* callback)(Dwarf_Die *,void *),void * data)751db0d2c64SMasami Hiramatsu int die_walk_instances(Dwarf_Die *or_die, int (*callback)(Dwarf_Die *, void *),
752db0d2c64SMasami Hiramatsu 		       void *data)
753db0d2c64SMasami Hiramatsu {
754db0d2c64SMasami Hiramatsu 	Dwarf_Die cu_die;
755db0d2c64SMasami Hiramatsu 	Dwarf_Die die_mem;
756db0d2c64SMasami Hiramatsu 	struct __instance_walk_param iwp = {
757db0d2c64SMasami Hiramatsu 		.addr = or_die->addr,
758db0d2c64SMasami Hiramatsu 		.callback = callback,
759db0d2c64SMasami Hiramatsu 		.data = data,
760db0d2c64SMasami Hiramatsu 		.retval = -ENOENT,
761db0d2c64SMasami Hiramatsu 	};
762db0d2c64SMasami Hiramatsu 
763db0d2c64SMasami Hiramatsu 	if (dwarf_diecu(or_die, &cu_die, NULL, NULL) == NULL)
764db0d2c64SMasami Hiramatsu 		return -ENOENT;
765db0d2c64SMasami Hiramatsu 
766db0d2c64SMasami Hiramatsu 	die_find_child(&cu_die, __die_walk_instances_cb, &iwp, &die_mem);
767db0d2c64SMasami Hiramatsu 
768db0d2c64SMasami Hiramatsu 	return iwp.retval;
769db0d2c64SMasami Hiramatsu }
770db0d2c64SMasami Hiramatsu 
771e0d153c6SMasami Hiramatsu /* Line walker internal parameters */
772e0d153c6SMasami Hiramatsu struct __line_walk_param {
773b0e9cb28SMasami Hiramatsu 	bool recursive;
774e0d153c6SMasami Hiramatsu 	line_walk_callback_t callback;
775e0d153c6SMasami Hiramatsu 	void *data;
776e0d153c6SMasami Hiramatsu 	int retval;
777e0d153c6SMasami Hiramatsu };
778e0d153c6SMasami Hiramatsu 
__die_walk_funclines_cb(Dwarf_Die * in_die,void * data)779e0d153c6SMasami Hiramatsu static int __die_walk_funclines_cb(Dwarf_Die *in_die, void *data)
780e0d153c6SMasami Hiramatsu {
781e0d153c6SMasami Hiramatsu 	struct __line_walk_param *lw = data;
782b0e9cb28SMasami Hiramatsu 	Dwarf_Addr addr = 0;
783b0e9cb28SMasami Hiramatsu 	const char *fname;
784e0d153c6SMasami Hiramatsu 	int lineno;
785e0d153c6SMasami Hiramatsu 
786e0d153c6SMasami Hiramatsu 	if (dwarf_tag(in_die) == DW_TAG_inlined_subroutine) {
787b0e9cb28SMasami Hiramatsu 		fname = die_get_call_file(in_die);
788e0d153c6SMasami Hiramatsu 		lineno = die_get_call_lineno(in_die);
78918e21eb6SMasami Hiramatsu 		if (fname && lineno > 0 && die_entrypc(in_die, &addr) == 0) {
790b0e9cb28SMasami Hiramatsu 			lw->retval = lw->callback(fname, lineno, addr, lw->data);
791e0d153c6SMasami Hiramatsu 			if (lw->retval != 0)
792e0d153c6SMasami Hiramatsu 				return DIE_FIND_CB_END;
793e0d153c6SMasami Hiramatsu 		}
794b0e9cb28SMasami Hiramatsu 		if (!lw->recursive)
795e0d153c6SMasami Hiramatsu 			return DIE_FIND_CB_SIBLING;
796acb6a704SMasami Hiramatsu 	}
797b0e9cb28SMasami Hiramatsu 
798b0e9cb28SMasami Hiramatsu 	if (addr) {
799dc9a5d2cSMasami Hiramatsu (Google) 		fname = die_get_decl_file(in_die);
800b0e9cb28SMasami Hiramatsu 		if (fname && dwarf_decl_line(in_die, &lineno) == 0) {
801b0e9cb28SMasami Hiramatsu 			lw->retval = lw->callback(fname, lineno, addr, lw->data);
802b0e9cb28SMasami Hiramatsu 			if (lw->retval != 0)
803b0e9cb28SMasami Hiramatsu 				return DIE_FIND_CB_END;
804b0e9cb28SMasami Hiramatsu 		}
805b0e9cb28SMasami Hiramatsu 	}
806b0e9cb28SMasami Hiramatsu 
807b0e9cb28SMasami Hiramatsu 	/* Continue to search nested inlined function call-sites */
808b0e9cb28SMasami Hiramatsu 	return DIE_FIND_CB_CONTINUE;
809e0d153c6SMasami Hiramatsu }
810e0d153c6SMasami Hiramatsu 
811e0d153c6SMasami Hiramatsu /* Walk on lines of blocks included in given DIE */
__die_walk_funclines(Dwarf_Die * sp_die,bool recursive,line_walk_callback_t callback,void * data)812b0e9cb28SMasami Hiramatsu static int __die_walk_funclines(Dwarf_Die *sp_die, bool recursive,
813e0d153c6SMasami Hiramatsu 				line_walk_callback_t callback, void *data)
814e0d153c6SMasami Hiramatsu {
815e0d153c6SMasami Hiramatsu 	struct __line_walk_param lw = {
816b0e9cb28SMasami Hiramatsu 		.recursive = recursive,
817e0d153c6SMasami Hiramatsu 		.callback = callback,
818e0d153c6SMasami Hiramatsu 		.data = data,
819e0d153c6SMasami Hiramatsu 		.retval = 0,
820e0d153c6SMasami Hiramatsu 	};
821e0d153c6SMasami Hiramatsu 	Dwarf_Die die_mem;
822e0d153c6SMasami Hiramatsu 	Dwarf_Addr addr;
823b0e9cb28SMasami Hiramatsu 	const char *fname;
824e0d153c6SMasami Hiramatsu 	int lineno;
825e0d153c6SMasami Hiramatsu 
826e0d153c6SMasami Hiramatsu 	/* Handle function declaration line */
827dc9a5d2cSMasami Hiramatsu (Google) 	fname = die_get_decl_file(sp_die);
828b0e9cb28SMasami Hiramatsu 	if (fname && dwarf_decl_line(sp_die, &lineno) == 0 &&
82991e2f539SMasami Hiramatsu 	    die_entrypc(sp_die, &addr) == 0) {
830b0e9cb28SMasami Hiramatsu 		lw.retval = callback(fname, lineno, addr, data);
831e0d153c6SMasami Hiramatsu 		if (lw.retval != 0)
832e0d153c6SMasami Hiramatsu 			goto done;
833e0d153c6SMasami Hiramatsu 	}
834e0d153c6SMasami Hiramatsu 	die_find_child(sp_die, __die_walk_funclines_cb, &lw, &die_mem);
835e0d153c6SMasami Hiramatsu done:
836e0d153c6SMasami Hiramatsu 	return lw.retval;
837e0d153c6SMasami Hiramatsu }
838e0d153c6SMasami Hiramatsu 
__die_walk_culines_cb(Dwarf_Die * sp_die,void * data)839e0d153c6SMasami Hiramatsu static int __die_walk_culines_cb(Dwarf_Die *sp_die, void *data)
840e0d153c6SMasami Hiramatsu {
841e0d153c6SMasami Hiramatsu 	struct __line_walk_param *lw = data;
842e0d153c6SMasami Hiramatsu 
843acb6a704SMasami Hiramatsu 	/*
844acb6a704SMasami Hiramatsu 	 * Since inlined function can include another inlined function in
845acb6a704SMasami Hiramatsu 	 * the same file, we need to walk in it recursively.
846acb6a704SMasami Hiramatsu 	 */
847b0e9cb28SMasami Hiramatsu 	lw->retval = __die_walk_funclines(sp_die, true, lw->callback, lw->data);
848e0d153c6SMasami Hiramatsu 	if (lw->retval != 0)
849e0d153c6SMasami Hiramatsu 		return DWARF_CB_ABORT;
850e0d153c6SMasami Hiramatsu 
851e0d153c6SMasami Hiramatsu 	return DWARF_CB_OK;
852e0d153c6SMasami Hiramatsu }
853e0d153c6SMasami Hiramatsu 
854e0d153c6SMasami Hiramatsu /**
855e0d153c6SMasami Hiramatsu  * die_walk_lines - Walk on lines inside given DIE
856a128405cSMasami Hiramatsu  * @rt_die: a root DIE (CU, subprogram or inlined_subroutine)
857e0d153c6SMasami Hiramatsu  * @callback: callback routine
858e0d153c6SMasami Hiramatsu  * @data: user data
859e0d153c6SMasami Hiramatsu  *
860e0d153c6SMasami Hiramatsu  * Walk on all lines inside given @rt_die and call @callback on each line.
861e0d153c6SMasami Hiramatsu  * If the @rt_die is a function, walk only on the lines inside the function,
862e0d153c6SMasami Hiramatsu  * otherwise @rt_die must be a CU DIE.
863e0d153c6SMasami Hiramatsu  * Note that this walks not only dwarf line list, but also function entries
864e0d153c6SMasami Hiramatsu  * and inline call-site.
865e0d153c6SMasami Hiramatsu  */
die_walk_lines(Dwarf_Die * rt_die,line_walk_callback_t callback,void * data)866e0d153c6SMasami Hiramatsu int die_walk_lines(Dwarf_Die *rt_die, line_walk_callback_t callback, void *data)
867e0d153c6SMasami Hiramatsu {
868e0d153c6SMasami Hiramatsu 	Dwarf_Lines *lines;
869e0d153c6SMasami Hiramatsu 	Dwarf_Line *line;
870e0d153c6SMasami Hiramatsu 	Dwarf_Addr addr;
87186c0bf85SMasami Hiramatsu 	const char *fname, *decf = NULL, *inf = NULL;
872e0d153c6SMasami Hiramatsu 	int lineno, ret = 0;
87375186a9bSMasami Hiramatsu 	int decl = 0, inl;
874e0d153c6SMasami Hiramatsu 	Dwarf_Die die_mem, *cu_die;
875e0d153c6SMasami Hiramatsu 	size_t nlines, i;
876f4d99bdfSMasami Hiramatsu 	bool flag;
877e0d153c6SMasami Hiramatsu 
878e0d153c6SMasami Hiramatsu 	/* Get the CU die */
87975186a9bSMasami Hiramatsu 	if (dwarf_tag(rt_die) != DW_TAG_compile_unit) {
880e0d153c6SMasami Hiramatsu 		cu_die = dwarf_diecu(rt_die, &die_mem, NULL, NULL);
88175186a9bSMasami Hiramatsu 		dwarf_decl_line(rt_die, &decl);
882dc9a5d2cSMasami Hiramatsu (Google) 		decf = die_get_decl_file(rt_die);
88398e4c68dSMasami Hiramatsu (Google) 		if (!decf) {
88498e4c68dSMasami Hiramatsu (Google) 			pr_debug2("Failed to get the declared file name of %s\n",
88598e4c68dSMasami Hiramatsu (Google) 				  dwarf_diename(rt_die));
88698e4c68dSMasami Hiramatsu (Google) 			return -EINVAL;
88798e4c68dSMasami Hiramatsu (Google) 		}
88875186a9bSMasami Hiramatsu 	} else
889e0d153c6SMasami Hiramatsu 		cu_die = rt_die;
890e0d153c6SMasami Hiramatsu 	if (!cu_die) {
891a128405cSMasami Hiramatsu 		pr_debug2("Failed to get CU from given DIE.\n");
892e0d153c6SMasami Hiramatsu 		return -EINVAL;
893e0d153c6SMasami Hiramatsu 	}
894e0d153c6SMasami Hiramatsu 
895e0d153c6SMasami Hiramatsu 	/* Get lines list in the CU */
896e0d153c6SMasami Hiramatsu 	if (dwarf_getsrclines(cu_die, &lines, &nlines) != 0) {
897e0d153c6SMasami Hiramatsu 		pr_debug2("Failed to get source lines on this CU.\n");
898e0d153c6SMasami Hiramatsu 		return -ENOENT;
899e0d153c6SMasami Hiramatsu 	}
900e0d153c6SMasami Hiramatsu 	pr_debug2("Get %zd lines from this CU\n", nlines);
901e0d153c6SMasami Hiramatsu 
902e0d153c6SMasami Hiramatsu 	/* Walk on the lines on lines list */
903e0d153c6SMasami Hiramatsu 	for (i = 0; i < nlines; i++) {
904e0d153c6SMasami Hiramatsu 		line = dwarf_onesrcline(lines, i);
905e0d153c6SMasami Hiramatsu 		if (line == NULL ||
906e0d153c6SMasami Hiramatsu 		    dwarf_lineno(line, &lineno) != 0 ||
907e0d153c6SMasami Hiramatsu 		    dwarf_lineaddr(line, &addr) != 0) {
908e0d153c6SMasami Hiramatsu 			pr_debug2("Failed to get line info. "
909e0d153c6SMasami Hiramatsu 				  "Possible error in debuginfo.\n");
910e0d153c6SMasami Hiramatsu 			continue;
911e0d153c6SMasami Hiramatsu 		}
912f4d99bdfSMasami Hiramatsu 		/* Skip end-of-sequence */
913f4d99bdfSMasami Hiramatsu 		if (dwarf_lineendsequence(line, &flag) != 0 || flag)
914f4d99bdfSMasami Hiramatsu 			continue;
915f4d99bdfSMasami Hiramatsu 		/* Skip Non statement line-info */
916f4d99bdfSMasami Hiramatsu 		if (dwarf_linebeginstatement(line, &flag) != 0 || !flag)
917f4d99bdfSMasami Hiramatsu 			continue;
918e0d153c6SMasami Hiramatsu 		/* Filter lines based on address */
91986a76027SMasami Hiramatsu 		if (rt_die != cu_die) {
920e0d153c6SMasami Hiramatsu 			/*
921e0d153c6SMasami Hiramatsu 			 * Address filtering
922e0d153c6SMasami Hiramatsu 			 * The line is included in given function, and
923e0d153c6SMasami Hiramatsu 			 * no inline block includes it.
924e0d153c6SMasami Hiramatsu 			 */
92575186a9bSMasami Hiramatsu 			if (!dwarf_haspc(rt_die, addr))
926e0d153c6SMasami Hiramatsu 				continue;
92786c0bf85SMasami Hiramatsu 
92875186a9bSMasami Hiramatsu 			if (die_find_inlinefunc(rt_die, addr, &die_mem)) {
92986c0bf85SMasami Hiramatsu 				/* Call-site check */
93086c0bf85SMasami Hiramatsu 				inf = die_get_call_file(&die_mem);
93186c0bf85SMasami Hiramatsu 				if ((inf && !strcmp(inf, decf)) &&
93286c0bf85SMasami Hiramatsu 				    die_get_call_lineno(&die_mem) == lineno)
93386c0bf85SMasami Hiramatsu 					goto found;
93486c0bf85SMasami Hiramatsu 
93575186a9bSMasami Hiramatsu 				dwarf_decl_line(&die_mem, &inl);
93675186a9bSMasami Hiramatsu 				if (inl != decl ||
937dc9a5d2cSMasami Hiramatsu (Google) 				    decf != die_get_decl_file(&die_mem))
93875186a9bSMasami Hiramatsu 					continue;
93975186a9bSMasami Hiramatsu 			}
94086a76027SMasami Hiramatsu 		}
94186c0bf85SMasami Hiramatsu found:
942e0d153c6SMasami Hiramatsu 		/* Get source line */
943e0d153c6SMasami Hiramatsu 		fname = dwarf_linesrc(line, NULL, NULL);
944e0d153c6SMasami Hiramatsu 
945e0d153c6SMasami Hiramatsu 		ret = callback(fname, lineno, addr, data);
946e0d153c6SMasami Hiramatsu 		if (ret != 0)
947e0d153c6SMasami Hiramatsu 			return ret;
948e0d153c6SMasami Hiramatsu 	}
949e0d153c6SMasami Hiramatsu 
950e0d153c6SMasami Hiramatsu 	/*
951e0d153c6SMasami Hiramatsu 	 * Dwarf lines doesn't include function declarations and inlined
952e0d153c6SMasami Hiramatsu 	 * subroutines. We have to check functions list or given function.
953e0d153c6SMasami Hiramatsu 	 */
954e0d153c6SMasami Hiramatsu 	if (rt_die != cu_die)
955b0e9cb28SMasami Hiramatsu 		/*
956acb6a704SMasami Hiramatsu 		 * Don't need walk inlined functions recursively, because
957acb6a704SMasami Hiramatsu 		 * inner inlined functions don't have the lines of the
958acb6a704SMasami Hiramatsu 		 * specified function.
959b0e9cb28SMasami Hiramatsu 		 */
960b0e9cb28SMasami Hiramatsu 		ret = __die_walk_funclines(rt_die, false, callback, data);
961e0d153c6SMasami Hiramatsu 	else {
962e0d153c6SMasami Hiramatsu 		struct __line_walk_param param = {
963e0d153c6SMasami Hiramatsu 			.callback = callback,
964e0d153c6SMasami Hiramatsu 			.data = data,
965e0d153c6SMasami Hiramatsu 			.retval = 0,
966e0d153c6SMasami Hiramatsu 		};
967e0d153c6SMasami Hiramatsu 		dwarf_getfuncs(cu_die, __die_walk_culines_cb, &param, 0);
968e0d153c6SMasami Hiramatsu 		ret = param.retval;
969e0d153c6SMasami Hiramatsu 	}
970e0d153c6SMasami Hiramatsu 
971e0d153c6SMasami Hiramatsu 	return ret;
972e0d153c6SMasami Hiramatsu }
973e0d153c6SMasami Hiramatsu 
974e0d153c6SMasami Hiramatsu struct __find_variable_param {
975e0d153c6SMasami Hiramatsu 	const char *name;
976e0d153c6SMasami Hiramatsu 	Dwarf_Addr addr;
977e0d153c6SMasami Hiramatsu };
978e0d153c6SMasami Hiramatsu 
__die_find_variable_cb(Dwarf_Die * die_mem,void * data)979e0d153c6SMasami Hiramatsu static int __die_find_variable_cb(Dwarf_Die *die_mem, void *data)
980e0d153c6SMasami Hiramatsu {
981e0d153c6SMasami Hiramatsu 	struct __find_variable_param *fvp = data;
982082f96a9SMasami Hiramatsu 	Dwarf_Attribute attr;
983e0d153c6SMasami Hiramatsu 	int tag;
984e0d153c6SMasami Hiramatsu 
985e0d153c6SMasami Hiramatsu 	tag = dwarf_tag(die_mem);
986e0d153c6SMasami Hiramatsu 	if ((tag == DW_TAG_formal_parameter ||
987e0d153c6SMasami Hiramatsu 	     tag == DW_TAG_variable) &&
988082f96a9SMasami Hiramatsu 	    die_compare_name(die_mem, fvp->name) &&
9893cb17cceSLi Huafei 	/*
9903cb17cceSLi Huafei 	 * Does the DIE have location information or const value
9913cb17cceSLi Huafei 	 * or external instance?
9923cb17cceSLi Huafei 	 */
993082f96a9SMasami Hiramatsu 	    (dwarf_attr(die_mem, DW_AT_external, &attr) ||
9943cb17cceSLi Huafei 	     dwarf_attr(die_mem, DW_AT_location, &attr) ||
9953cb17cceSLi Huafei 	     dwarf_attr(die_mem, DW_AT_const_value, &attr)))
996e0d153c6SMasami Hiramatsu 		return DIE_FIND_CB_END;
997e0d153c6SMasami Hiramatsu 	if (dwarf_haspc(die_mem, fvp->addr))
998e0d153c6SMasami Hiramatsu 		return DIE_FIND_CB_CONTINUE;
999e0d153c6SMasami Hiramatsu 	else
1000e0d153c6SMasami Hiramatsu 		return DIE_FIND_CB_SIBLING;
1001e0d153c6SMasami Hiramatsu }
1002e0d153c6SMasami Hiramatsu 
1003e0d153c6SMasami Hiramatsu /**
1004e0d153c6SMasami Hiramatsu  * die_find_variable_at - Find a given name variable at given address
1005e0d153c6SMasami Hiramatsu  * @sp_die: a function DIE
1006e0d153c6SMasami Hiramatsu  * @name: variable name
1007e0d153c6SMasami Hiramatsu  * @addr: address
1008e0d153c6SMasami Hiramatsu  * @die_mem: a buffer for result DIE
1009e0d153c6SMasami Hiramatsu  *
1010e0d153c6SMasami Hiramatsu  * Find a variable DIE called @name at @addr in @sp_die.
1011e0d153c6SMasami Hiramatsu  */
die_find_variable_at(Dwarf_Die * sp_die,const char * name,Dwarf_Addr addr,Dwarf_Die * die_mem)1012e0d153c6SMasami Hiramatsu Dwarf_Die *die_find_variable_at(Dwarf_Die *sp_die, const char *name,
1013e0d153c6SMasami Hiramatsu 				Dwarf_Addr addr, Dwarf_Die *die_mem)
1014e0d153c6SMasami Hiramatsu {
1015e0d153c6SMasami Hiramatsu 	struct __find_variable_param fvp = { .name = name, .addr = addr};
1016e0d153c6SMasami Hiramatsu 
1017e0d153c6SMasami Hiramatsu 	return die_find_child(sp_die, __die_find_variable_cb, (void *)&fvp,
1018e0d153c6SMasami Hiramatsu 			      die_mem);
1019e0d153c6SMasami Hiramatsu }
1020e0d153c6SMasami Hiramatsu 
__die_find_member_cb(Dwarf_Die * die_mem,void * data)1021e0d153c6SMasami Hiramatsu static int __die_find_member_cb(Dwarf_Die *die_mem, void *data)
1022e0d153c6SMasami Hiramatsu {
1023e0d153c6SMasami Hiramatsu 	const char *name = data;
1024e0d153c6SMasami Hiramatsu 
1025c7273835SMasami Hiramatsu 	if (dwarf_tag(die_mem) == DW_TAG_member) {
1026c7273835SMasami Hiramatsu 		if (die_compare_name(die_mem, name))
1027e0d153c6SMasami Hiramatsu 			return DIE_FIND_CB_END;
1028c7273835SMasami Hiramatsu 		else if (!dwarf_diename(die_mem)) {	/* Unnamed structure */
1029c7273835SMasami Hiramatsu 			Dwarf_Die type_die, tmp_die;
1030c7273835SMasami Hiramatsu 			if (die_get_type(die_mem, &type_die) &&
1031c7273835SMasami Hiramatsu 			    die_find_member(&type_die, name, &tmp_die))
1032c7273835SMasami Hiramatsu 				return DIE_FIND_CB_END;
1033c7273835SMasami Hiramatsu 		}
1034c7273835SMasami Hiramatsu 	}
1035e0d153c6SMasami Hiramatsu 	return DIE_FIND_CB_SIBLING;
1036e0d153c6SMasami Hiramatsu }
1037e0d153c6SMasami Hiramatsu 
1038e0d153c6SMasami Hiramatsu /**
1039e0d153c6SMasami Hiramatsu  * die_find_member - Find a given name member in a data structure
1040e0d153c6SMasami Hiramatsu  * @st_die: a data structure type DIE
1041e0d153c6SMasami Hiramatsu  * @name: member name
1042e0d153c6SMasami Hiramatsu  * @die_mem: a buffer for result DIE
1043e0d153c6SMasami Hiramatsu  *
1044e0d153c6SMasami Hiramatsu  * Find a member DIE called @name in @st_die.
1045e0d153c6SMasami Hiramatsu  */
die_find_member(Dwarf_Die * st_die,const char * name,Dwarf_Die * die_mem)1046e0d153c6SMasami Hiramatsu Dwarf_Die *die_find_member(Dwarf_Die *st_die, const char *name,
1047e0d153c6SMasami Hiramatsu 			   Dwarf_Die *die_mem)
1048e0d153c6SMasami Hiramatsu {
1049e0d153c6SMasami Hiramatsu 	return die_find_child(st_die, __die_find_member_cb, (void *)name,
1050e0d153c6SMasami Hiramatsu 			      die_mem);
1051e0d153c6SMasami Hiramatsu }
1052e0d153c6SMasami Hiramatsu 
1053e0d153c6SMasami Hiramatsu /**
1054e0d153c6SMasami Hiramatsu  * die_get_typename - Get the name of given variable DIE
1055e0d153c6SMasami Hiramatsu  * @vr_die: a variable DIE
1056fb9596d1SHe Kuang  * @buf: a strbuf for result type name
1057e0d153c6SMasami Hiramatsu  *
1058fb9596d1SHe Kuang  * Get the name of @vr_die and stores it to @buf. Return 0 if succeeded.
1059fb9596d1SHe Kuang  * and Return -ENOENT if failed to find type name.
1060e0d153c6SMasami Hiramatsu  * Note that the result will stores typedef name if possible, and stores
1061e0d153c6SMasami Hiramatsu  * "*(function_type)" if the type is a function pointer.
1062e0d153c6SMasami Hiramatsu  */
die_get_typename(Dwarf_Die * vr_die,struct strbuf * buf)1063fb9596d1SHe Kuang int die_get_typename(Dwarf_Die *vr_die, struct strbuf *buf)
1064e0d153c6SMasami Hiramatsu {
1065e0d153c6SMasami Hiramatsu 	Dwarf_Die type;
1066fb9596d1SHe Kuang 	int tag, ret;
1067e0d153c6SMasami Hiramatsu 	const char *tmp = "";
1068e0d153c6SMasami Hiramatsu 
1069e0d153c6SMasami Hiramatsu 	if (__die_get_real_type(vr_die, &type) == NULL)
1070e0d153c6SMasami Hiramatsu 		return -ENOENT;
1071e0d153c6SMasami Hiramatsu 
1072e0d153c6SMasami Hiramatsu 	tag = dwarf_tag(&type);
1073e0d153c6SMasami Hiramatsu 	if (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type)
1074e0d153c6SMasami Hiramatsu 		tmp = "*";
1075e0d153c6SMasami Hiramatsu 	else if (tag == DW_TAG_subroutine_type) {
1076e0d153c6SMasami Hiramatsu 		/* Function pointer */
1077bf4d5f25SMasami Hiramatsu 		return strbuf_add(buf, "(function_type)", 15);
1078e0d153c6SMasami Hiramatsu 	} else {
10797f911905SNamhyung Kim 		const char *name = dwarf_diename(&type);
10807f911905SNamhyung Kim 
1081e0d153c6SMasami Hiramatsu 		if (tag == DW_TAG_union_type)
1082e0d153c6SMasami Hiramatsu 			tmp = "union ";
1083e0d153c6SMasami Hiramatsu 		else if (tag == DW_TAG_structure_type)
1084e0d153c6SMasami Hiramatsu 			tmp = "struct ";
1085bb2d17a0SHyeoncheol Lee 		else if (tag == DW_TAG_enumeration_type)
1086bb2d17a0SHyeoncheol Lee 			tmp = "enum ";
10877f911905SNamhyung Kim 		else if (name == NULL)
10887f911905SNamhyung Kim 			return -ENOENT;
1089e0d153c6SMasami Hiramatsu 		/* Write a base name */
10907f911905SNamhyung Kim 		return strbuf_addf(buf, "%s%s", tmp, name ?: "");
1091e0d153c6SMasami Hiramatsu 	}
1092fb9596d1SHe Kuang 	ret = die_get_typename(&type, buf);
1093bf4d5f25SMasami Hiramatsu 	return ret ? ret : strbuf_addstr(buf, tmp);
1094e0d153c6SMasami Hiramatsu }
1095e0d153c6SMasami Hiramatsu 
1096e0d153c6SMasami Hiramatsu /**
1097e0d153c6SMasami Hiramatsu  * die_get_varname - Get the name and type of given variable DIE
1098e0d153c6SMasami Hiramatsu  * @vr_die: a variable DIE
1099fb9596d1SHe Kuang  * @buf: a strbuf for type and variable name
1100e0d153c6SMasami Hiramatsu  *
1101e0d153c6SMasami Hiramatsu  * Get the name and type of @vr_die and stores it in @buf as "type\tname".
1102e0d153c6SMasami Hiramatsu  */
die_get_varname(Dwarf_Die * vr_die,struct strbuf * buf)1103fb9596d1SHe Kuang int die_get_varname(Dwarf_Die *vr_die, struct strbuf *buf)
1104e0d153c6SMasami Hiramatsu {
1105fb9596d1SHe Kuang 	int ret;
1106e0d153c6SMasami Hiramatsu 
1107fb9596d1SHe Kuang 	ret = die_get_typename(vr_die, buf);
1108e0d153c6SMasami Hiramatsu 	if (ret < 0) {
1109e0d153c6SMasami Hiramatsu 		pr_debug("Failed to get type, make it unknown.\n");
1110bf4d5f25SMasami Hiramatsu 		ret = strbuf_add(buf, "(unknown_type)", 14);
1111e0d153c6SMasami Hiramatsu 	}
1112fb9596d1SHe Kuang 
1113bf4d5f25SMasami Hiramatsu 	return ret < 0 ? ret : strbuf_addf(buf, "\t%s", dwarf_diename(vr_die));
1114e0d153c6SMasami Hiramatsu }
1115e0d153c6SMasami Hiramatsu 
1116a36ebe4eSJin Yao #ifdef HAVE_DWARF_GETLOCATIONS_SUPPORT
1117349e8d26SHe Kuang /**
1118349e8d26SHe Kuang  * die_get_var_innermost_scope - Get innermost scope range of given variable DIE
1119349e8d26SHe Kuang  * @sp_die: a subprogram DIE
1120349e8d26SHe Kuang  * @vr_die: a variable DIE
1121349e8d26SHe Kuang  * @buf: a strbuf for variable byte offset range
1122349e8d26SHe Kuang  *
1123349e8d26SHe Kuang  * Get the innermost scope range of @vr_die and stores it in @buf as
1124349e8d26SHe Kuang  * "@<function_name+[NN-NN,NN-NN]>".
1125349e8d26SHe Kuang  */
die_get_var_innermost_scope(Dwarf_Die * sp_die,Dwarf_Die * vr_die,struct strbuf * buf)1126349e8d26SHe Kuang static int die_get_var_innermost_scope(Dwarf_Die *sp_die, Dwarf_Die *vr_die,
1127349e8d26SHe Kuang 				struct strbuf *buf)
1128349e8d26SHe Kuang {
1129349e8d26SHe Kuang 	Dwarf_Die *scopes;
1130349e8d26SHe Kuang 	int count;
1131349e8d26SHe Kuang 	size_t offset = 0;
1132349e8d26SHe Kuang 	Dwarf_Addr base;
1133349e8d26SHe Kuang 	Dwarf_Addr start, end;
1134349e8d26SHe Kuang 	Dwarf_Addr entry;
1135349e8d26SHe Kuang 	int ret;
1136349e8d26SHe Kuang 	bool first = true;
1137349e8d26SHe Kuang 	const char *name;
1138349e8d26SHe Kuang 
1139af04dd2fSMasami Hiramatsu 	ret = die_entrypc(sp_die, &entry);
1140349e8d26SHe Kuang 	if (ret)
1141349e8d26SHe Kuang 		return ret;
1142349e8d26SHe Kuang 
1143349e8d26SHe Kuang 	name = dwarf_diename(sp_die);
1144349e8d26SHe Kuang 	if (!name)
1145349e8d26SHe Kuang 		return -ENOENT;
1146349e8d26SHe Kuang 
1147349e8d26SHe Kuang 	count = dwarf_getscopes_die(vr_die, &scopes);
1148349e8d26SHe Kuang 
1149349e8d26SHe Kuang 	/* (*SCOPES)[1] is the DIE for the scope containing that scope */
1150349e8d26SHe Kuang 	if (count <= 1) {
1151349e8d26SHe Kuang 		ret = -EINVAL;
1152349e8d26SHe Kuang 		goto out;
1153349e8d26SHe Kuang 	}
1154349e8d26SHe Kuang 
1155349e8d26SHe Kuang 	while ((offset = dwarf_ranges(&scopes[1], offset, &base,
1156349e8d26SHe Kuang 					&start, &end)) > 0) {
1157349e8d26SHe Kuang 		start -= entry;
1158349e8d26SHe Kuang 		end -= entry;
1159349e8d26SHe Kuang 
1160349e8d26SHe Kuang 		if (first) {
1161bf4d5f25SMasami Hiramatsu 			ret = strbuf_addf(buf, "@<%s+[%" PRIu64 "-%" PRIu64,
1162349e8d26SHe Kuang 					  name, start, end);
1163349e8d26SHe Kuang 			first = false;
1164349e8d26SHe Kuang 		} else {
1165bf4d5f25SMasami Hiramatsu 			ret = strbuf_addf(buf, ",%" PRIu64 "-%" PRIu64,
1166349e8d26SHe Kuang 					  start, end);
1167349e8d26SHe Kuang 		}
1168bf4d5f25SMasami Hiramatsu 		if (ret < 0)
1169bf4d5f25SMasami Hiramatsu 			goto out;
1170349e8d26SHe Kuang 	}
1171349e8d26SHe Kuang 
1172349e8d26SHe Kuang 	if (!first)
1173bf4d5f25SMasami Hiramatsu 		ret = strbuf_add(buf, "]>", 2);
1174349e8d26SHe Kuang 
1175349e8d26SHe Kuang out:
1176349e8d26SHe Kuang 	free(scopes);
1177349e8d26SHe Kuang 	return ret;
1178349e8d26SHe Kuang }
1179349e8d26SHe Kuang 
1180349e8d26SHe Kuang /**
1181349e8d26SHe Kuang  * die_get_var_range - Get byte offset range of given variable DIE
1182349e8d26SHe Kuang  * @sp_die: a subprogram DIE
1183349e8d26SHe Kuang  * @vr_die: a variable DIE
1184349e8d26SHe Kuang  * @buf: a strbuf for type and variable name and byte offset range
1185349e8d26SHe Kuang  *
1186349e8d26SHe Kuang  * Get the byte offset range of @vr_die and stores it in @buf as
1187349e8d26SHe Kuang  * "@<function_name+[NN-NN,NN-NN]>".
1188349e8d26SHe Kuang  */
die_get_var_range(Dwarf_Die * sp_die,Dwarf_Die * vr_die,struct strbuf * buf)1189349e8d26SHe Kuang int die_get_var_range(Dwarf_Die *sp_die, Dwarf_Die *vr_die, struct strbuf *buf)
1190349e8d26SHe Kuang {
1191349e8d26SHe Kuang 	int ret = 0;
1192349e8d26SHe Kuang 	Dwarf_Addr base;
1193349e8d26SHe Kuang 	Dwarf_Addr start, end;
1194349e8d26SHe Kuang 	Dwarf_Addr entry;
1195349e8d26SHe Kuang 	Dwarf_Op *op;
1196349e8d26SHe Kuang 	size_t nops;
1197349e8d26SHe Kuang 	size_t offset = 0;
1198349e8d26SHe Kuang 	Dwarf_Attribute attr;
1199349e8d26SHe Kuang 	bool first = true;
1200349e8d26SHe Kuang 	const char *name;
1201349e8d26SHe Kuang 
1202af04dd2fSMasami Hiramatsu 	ret = die_entrypc(sp_die, &entry);
1203349e8d26SHe Kuang 	if (ret)
1204349e8d26SHe Kuang 		return ret;
1205349e8d26SHe Kuang 
1206349e8d26SHe Kuang 	name = dwarf_diename(sp_die);
1207349e8d26SHe Kuang 	if (!name)
1208349e8d26SHe Kuang 		return -ENOENT;
1209349e8d26SHe Kuang 
1210349e8d26SHe Kuang 	if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL)
1211349e8d26SHe Kuang 		return -EINVAL;
1212349e8d26SHe Kuang 
1213bf4d5f25SMasami Hiramatsu 	while ((offset = dwarf_getlocations(&attr, offset, &base,
1214349e8d26SHe Kuang 					&start, &end, &op, &nops)) > 0) {
1215349e8d26SHe Kuang 		if (start == 0) {
1216349e8d26SHe Kuang 			/* Single Location Descriptions */
1217349e8d26SHe Kuang 			ret = die_get_var_innermost_scope(sp_die, vr_die, buf);
1218bf4d5f25SMasami Hiramatsu 			goto out;
1219349e8d26SHe Kuang 		}
1220349e8d26SHe Kuang 
1221349e8d26SHe Kuang 		/* Location Lists */
1222349e8d26SHe Kuang 		start -= entry;
1223349e8d26SHe Kuang 		end -= entry;
1224349e8d26SHe Kuang 		if (first) {
1225bf4d5f25SMasami Hiramatsu 			ret = strbuf_addf(buf, "@<%s+[%" PRIu64 "-%" PRIu64,
1226349e8d26SHe Kuang 					  name, start, end);
1227349e8d26SHe Kuang 			first = false;
1228349e8d26SHe Kuang 		} else {
1229bf4d5f25SMasami Hiramatsu 			ret = strbuf_addf(buf, ",%" PRIu64 "-%" PRIu64,
1230349e8d26SHe Kuang 					  start, end);
1231349e8d26SHe Kuang 		}
1232bf4d5f25SMasami Hiramatsu 		if (ret < 0)
1233bf4d5f25SMasami Hiramatsu 			goto out;
1234349e8d26SHe Kuang 	}
1235349e8d26SHe Kuang 
1236349e8d26SHe Kuang 	if (!first)
1237bf4d5f25SMasami Hiramatsu 		ret = strbuf_add(buf, "]>", 2);
1238bf4d5f25SMasami Hiramatsu out:
1239349e8d26SHe Kuang 	return ret;
1240349e8d26SHe Kuang }
1241bd0419e2SArnaldo Carvalho de Melo #else
die_get_var_range(Dwarf_Die * sp_die __maybe_unused,Dwarf_Die * vr_die __maybe_unused,struct strbuf * buf __maybe_unused)1242bd0419e2SArnaldo Carvalho de Melo int die_get_var_range(Dwarf_Die *sp_die __maybe_unused,
1243bd0419e2SArnaldo Carvalho de Melo 		      Dwarf_Die *vr_die __maybe_unused,
1244bd0419e2SArnaldo Carvalho de Melo 		      struct strbuf *buf __maybe_unused)
1245bd0419e2SArnaldo Carvalho de Melo {
1246bd0419e2SArnaldo Carvalho de Melo 	return -ENOTSUP;
1247bd0419e2SArnaldo Carvalho de Melo }
1248bd0419e2SArnaldo Carvalho de Melo #endif
12496243b9dcSRavi Bangoria 
12506243b9dcSRavi Bangoria /*
12516243b9dcSRavi Bangoria  * die_has_loclist - Check if DW_AT_location of @vr_die is a location list
12526243b9dcSRavi Bangoria  * @vr_die: a variable DIE
12536243b9dcSRavi Bangoria  */
die_has_loclist(Dwarf_Die * vr_die)12546243b9dcSRavi Bangoria static bool die_has_loclist(Dwarf_Die *vr_die)
12556243b9dcSRavi Bangoria {
12566243b9dcSRavi Bangoria 	Dwarf_Attribute loc;
12576243b9dcSRavi Bangoria 	int tag = dwarf_tag(vr_die);
12586243b9dcSRavi Bangoria 
12596243b9dcSRavi Bangoria 	if (tag != DW_TAG_formal_parameter &&
12606243b9dcSRavi Bangoria 	    tag != DW_TAG_variable)
12616243b9dcSRavi Bangoria 		return false;
12626243b9dcSRavi Bangoria 
12636243b9dcSRavi Bangoria 	return (dwarf_attr_integrate(vr_die, DW_AT_location, &loc) &&
12646243b9dcSRavi Bangoria 		dwarf_whatform(&loc) == DW_FORM_sec_offset);
12656243b9dcSRavi Bangoria }
12666243b9dcSRavi Bangoria 
12676243b9dcSRavi Bangoria /*
12686243b9dcSRavi Bangoria  * die_is_optimized_target - Check if target program is compiled with
12696243b9dcSRavi Bangoria  * optimization
12706243b9dcSRavi Bangoria  * @cu_die: a CU DIE
12716243b9dcSRavi Bangoria  *
12726243b9dcSRavi Bangoria  * For any object in given CU whose DW_AT_location is a location list,
12736243b9dcSRavi Bangoria  * target program is compiled with optimization. This is applicable to
12746243b9dcSRavi Bangoria  * clang as well.
12756243b9dcSRavi Bangoria  */
die_is_optimized_target(Dwarf_Die * cu_die)12766243b9dcSRavi Bangoria bool die_is_optimized_target(Dwarf_Die *cu_die)
12776243b9dcSRavi Bangoria {
12786243b9dcSRavi Bangoria 	Dwarf_Die tmp_die;
12796243b9dcSRavi Bangoria 
12806243b9dcSRavi Bangoria 	if (die_has_loclist(cu_die))
12816243b9dcSRavi Bangoria 		return true;
12826243b9dcSRavi Bangoria 
12836243b9dcSRavi Bangoria 	if (!dwarf_child(cu_die, &tmp_die) &&
12846243b9dcSRavi Bangoria 	    die_is_optimized_target(&tmp_die))
12856243b9dcSRavi Bangoria 		return true;
12866243b9dcSRavi Bangoria 
12876243b9dcSRavi Bangoria 	if (!dwarf_siblingof(cu_die, &tmp_die) &&
12886243b9dcSRavi Bangoria 	    die_is_optimized_target(&tmp_die))
12896243b9dcSRavi Bangoria 		return true;
12906243b9dcSRavi Bangoria 
12916243b9dcSRavi Bangoria 	return false;
12926243b9dcSRavi Bangoria }
12936243b9dcSRavi Bangoria 
12946243b9dcSRavi Bangoria /*
12956243b9dcSRavi Bangoria  * die_search_idx - Search index of given line address
12966243b9dcSRavi Bangoria  * @lines: Line records of single CU
12976243b9dcSRavi Bangoria  * @nr_lines: Number of @lines
12986243b9dcSRavi Bangoria  * @addr: address we are looking for
12996243b9dcSRavi Bangoria  * @idx: index to be set by this function (return value)
13006243b9dcSRavi Bangoria  *
13016243b9dcSRavi Bangoria  * Search for @addr by looping over every lines of CU. If address
13026243b9dcSRavi Bangoria  * matches, set index of that line in @idx. Note that single source
13036243b9dcSRavi Bangoria  * line can have multiple line records. i.e. single source line can
13046243b9dcSRavi Bangoria  * have multiple index.
13056243b9dcSRavi Bangoria  */
die_search_idx(Dwarf_Lines * lines,unsigned long nr_lines,Dwarf_Addr addr,unsigned long * idx)13066243b9dcSRavi Bangoria static bool die_search_idx(Dwarf_Lines *lines, unsigned long nr_lines,
13076243b9dcSRavi Bangoria 			   Dwarf_Addr addr, unsigned long *idx)
13086243b9dcSRavi Bangoria {
13096243b9dcSRavi Bangoria 	unsigned long i;
13106243b9dcSRavi Bangoria 	Dwarf_Addr tmp;
13116243b9dcSRavi Bangoria 
13126243b9dcSRavi Bangoria 	for (i = 0; i < nr_lines; i++) {
13136243b9dcSRavi Bangoria 		if (dwarf_lineaddr(dwarf_onesrcline(lines, i), &tmp))
13146243b9dcSRavi Bangoria 			return false;
13156243b9dcSRavi Bangoria 
13166243b9dcSRavi Bangoria 		if (tmp == addr) {
13176243b9dcSRavi Bangoria 			*idx = i;
13186243b9dcSRavi Bangoria 			return true;
13196243b9dcSRavi Bangoria 		}
13206243b9dcSRavi Bangoria 	}
13216243b9dcSRavi Bangoria 	return false;
13226243b9dcSRavi Bangoria }
13236243b9dcSRavi Bangoria 
13246243b9dcSRavi Bangoria /*
13256243b9dcSRavi Bangoria  * die_get_postprologue_addr - Search next address after function prologue
13266243b9dcSRavi Bangoria  * @entrypc_idx: entrypc index
13276243b9dcSRavi Bangoria  * @lines: Line records of single CU
13286243b9dcSRavi Bangoria  * @nr_lines: Number of @lines
13296243b9dcSRavi Bangoria  * @hignpc: high PC address of function
13306243b9dcSRavi Bangoria  * @postprologue_addr: Next address after function prologue (return value)
13316243b9dcSRavi Bangoria  *
13326243b9dcSRavi Bangoria  * Look for prologue-end marker. If there is no explicit marker, return
13336243b9dcSRavi Bangoria  * address of next line record or next source line.
13346243b9dcSRavi Bangoria  */
die_get_postprologue_addr(unsigned long entrypc_idx,Dwarf_Lines * lines,unsigned long nr_lines,Dwarf_Addr highpc,Dwarf_Addr * postprologue_addr)13356243b9dcSRavi Bangoria static bool die_get_postprologue_addr(unsigned long entrypc_idx,
13366243b9dcSRavi Bangoria 				      Dwarf_Lines *lines,
13376243b9dcSRavi Bangoria 				      unsigned long nr_lines,
13386243b9dcSRavi Bangoria 				      Dwarf_Addr highpc,
13396243b9dcSRavi Bangoria 				      Dwarf_Addr *postprologue_addr)
13406243b9dcSRavi Bangoria {
13416243b9dcSRavi Bangoria 	unsigned long i;
13426243b9dcSRavi Bangoria 	int entrypc_lno, lno;
13436243b9dcSRavi Bangoria 	Dwarf_Line *line;
13446243b9dcSRavi Bangoria 	Dwarf_Addr addr;
13456243b9dcSRavi Bangoria 	bool p_end;
13466243b9dcSRavi Bangoria 
13476243b9dcSRavi Bangoria 	/* entrypc_lno is actual source line number */
13486243b9dcSRavi Bangoria 	line = dwarf_onesrcline(lines, entrypc_idx);
13496243b9dcSRavi Bangoria 	if (dwarf_lineno(line, &entrypc_lno))
13506243b9dcSRavi Bangoria 		return false;
13516243b9dcSRavi Bangoria 
13526243b9dcSRavi Bangoria 	for (i = entrypc_idx; i < nr_lines; i++) {
13536243b9dcSRavi Bangoria 		line = dwarf_onesrcline(lines, i);
13546243b9dcSRavi Bangoria 
13556243b9dcSRavi Bangoria 		if (dwarf_lineaddr(line, &addr) ||
13566243b9dcSRavi Bangoria 		    dwarf_lineno(line, &lno)    ||
13576243b9dcSRavi Bangoria 		    dwarf_lineprologueend(line, &p_end))
13586243b9dcSRavi Bangoria 			return false;
13596243b9dcSRavi Bangoria 
13606243b9dcSRavi Bangoria 		/* highpc is exclusive. [entrypc,highpc) */
13616243b9dcSRavi Bangoria 		if (addr >= highpc)
13626243b9dcSRavi Bangoria 			break;
13636243b9dcSRavi Bangoria 
13646243b9dcSRavi Bangoria 		/* clang supports prologue-end marker */
13656243b9dcSRavi Bangoria 		if (p_end)
13666243b9dcSRavi Bangoria 			break;
13676243b9dcSRavi Bangoria 
13686243b9dcSRavi Bangoria 		/* Actual next line in source */
13696243b9dcSRavi Bangoria 		if (lno != entrypc_lno)
13706243b9dcSRavi Bangoria 			break;
13716243b9dcSRavi Bangoria 
13726243b9dcSRavi Bangoria 		/*
13736243b9dcSRavi Bangoria 		 * Single source line can have multiple line records.
13746243b9dcSRavi Bangoria 		 * For Example,
13756243b9dcSRavi Bangoria 		 *     void foo() { printf("hello\n"); }
13766243b9dcSRavi Bangoria 		 * contains two line records. One points to declaration and
13776243b9dcSRavi Bangoria 		 * other points to printf() line. Variable 'lno' won't get
13786243b9dcSRavi Bangoria 		 * incremented in this case but 'i' will.
13796243b9dcSRavi Bangoria 		 */
13806243b9dcSRavi Bangoria 		if (i != entrypc_idx)
13816243b9dcSRavi Bangoria 			break;
13826243b9dcSRavi Bangoria 	}
13836243b9dcSRavi Bangoria 
13846243b9dcSRavi Bangoria 	dwarf_lineaddr(line, postprologue_addr);
13856243b9dcSRavi Bangoria 	if (*postprologue_addr >= highpc)
13866243b9dcSRavi Bangoria 		dwarf_lineaddr(dwarf_onesrcline(lines, i - 1),
13876243b9dcSRavi Bangoria 			       postprologue_addr);
13886243b9dcSRavi Bangoria 
13896243b9dcSRavi Bangoria 	return true;
13906243b9dcSRavi Bangoria }
13916243b9dcSRavi Bangoria 
13926243b9dcSRavi Bangoria /*
13936243b9dcSRavi Bangoria  * die_skip_prologue - Use next address after prologue as probe location
13946243b9dcSRavi Bangoria  * @sp_die: a subprogram DIE
13956243b9dcSRavi Bangoria  * @cu_die: a CU DIE
13966243b9dcSRavi Bangoria  * @entrypc: entrypc of the function
13976243b9dcSRavi Bangoria  *
13986243b9dcSRavi Bangoria  * Function prologue prepares stack and registers before executing function
13996243b9dcSRavi Bangoria  * logic. When target program is compiled without optimization, function
14006243b9dcSRavi Bangoria  * parameter information is only valid after prologue. When we probe entrypc
14016243b9dcSRavi Bangoria  * of the function, and try to record function parameter, it contains
14026243b9dcSRavi Bangoria  * garbage value.
14036243b9dcSRavi Bangoria  */
die_skip_prologue(Dwarf_Die * sp_die,Dwarf_Die * cu_die,Dwarf_Addr * entrypc)14046243b9dcSRavi Bangoria void die_skip_prologue(Dwarf_Die *sp_die, Dwarf_Die *cu_die,
14056243b9dcSRavi Bangoria 		       Dwarf_Addr *entrypc)
14066243b9dcSRavi Bangoria {
14076243b9dcSRavi Bangoria 	size_t nr_lines = 0;
14086243b9dcSRavi Bangoria 	unsigned long entrypc_idx = 0;
14096243b9dcSRavi Bangoria 	Dwarf_Lines *lines = NULL;
14106243b9dcSRavi Bangoria 	Dwarf_Addr postprologue_addr;
14116243b9dcSRavi Bangoria 	Dwarf_Addr highpc;
14126243b9dcSRavi Bangoria 
14136243b9dcSRavi Bangoria 	if (dwarf_highpc(sp_die, &highpc))
14146243b9dcSRavi Bangoria 		return;
14156243b9dcSRavi Bangoria 
14166243b9dcSRavi Bangoria 	if (dwarf_getsrclines(cu_die, &lines, &nr_lines))
14176243b9dcSRavi Bangoria 		return;
14186243b9dcSRavi Bangoria 
14196243b9dcSRavi Bangoria 	if (!die_search_idx(lines, nr_lines, *entrypc, &entrypc_idx))
14206243b9dcSRavi Bangoria 		return;
14216243b9dcSRavi Bangoria 
14226243b9dcSRavi Bangoria 	if (!die_get_postprologue_addr(entrypc_idx, lines, nr_lines,
14236243b9dcSRavi Bangoria 				       highpc, &postprologue_addr))
14246243b9dcSRavi Bangoria 		return;
14256243b9dcSRavi Bangoria 
14266243b9dcSRavi Bangoria 	*entrypc = postprologue_addr;
14276243b9dcSRavi Bangoria }
1428