• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * dwarf-aux.c : libdw auxiliary interfaces
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  */
19 
20 #include <errno.h>
21 #include <inttypes.h>
22 #include <stdbool.h>
23 #include "util.h"
24 #include "debug.h"
25 #include "dwarf-aux.h"
26 #include "string2.h"
27 
28 /**
29  * cu_find_realpath - Find the realpath of the target file
30  * @cu_die: A DIE(dwarf information entry) of CU(compilation Unit)
31  * @fname:  The tail filename of the target file
32  *
33  * Find the real(long) path of @fname in @cu_die.
34  */
cu_find_realpath(Dwarf_Die * cu_die,const char * fname)35 const char *cu_find_realpath(Dwarf_Die *cu_die, const char *fname)
36 {
37 	Dwarf_Files *files;
38 	size_t nfiles, i;
39 	const char *src = NULL;
40 	int ret;
41 
42 	if (!fname)
43 		return NULL;
44 
45 	ret = dwarf_getsrcfiles(cu_die, &files, &nfiles);
46 	if (ret != 0)
47 		return NULL;
48 
49 	for (i = 0; i < nfiles; i++) {
50 		src = dwarf_filesrc(files, i, NULL, NULL);
51 		if (strtailcmp(src, fname) == 0)
52 			break;
53 	}
54 	if (i == nfiles)
55 		return NULL;
56 	return src;
57 }
58 
59 /**
60  * cu_get_comp_dir - Get the path of compilation directory
61  * @cu_die: a CU DIE
62  *
63  * Get the path of compilation directory of given @cu_die.
64  * Since this depends on DW_AT_comp_dir, older gcc will not
65  * embedded it. In that case, this returns NULL.
66  */
cu_get_comp_dir(Dwarf_Die * cu_die)67 const char *cu_get_comp_dir(Dwarf_Die *cu_die)
68 {
69 	Dwarf_Attribute attr;
70 	if (dwarf_attr(cu_die, DW_AT_comp_dir, &attr) == NULL)
71 		return NULL;
72 	return dwarf_formstring(&attr);
73 }
74 
75 /**
76  * cu_find_lineinfo - Get a line number and file name for given address
77  * @cu_die: a CU DIE
78  * @addr: An address
79  * @fname: a pointer which returns the file name string
80  * @lineno: a pointer which returns the line number
81  *
82  * Find a line number and file name for @addr in @cu_die.
83  */
cu_find_lineinfo(Dwarf_Die * cu_die,unsigned long addr,const char ** fname,int * lineno)84 int cu_find_lineinfo(Dwarf_Die *cu_die, unsigned long addr,
85 		    const char **fname, int *lineno)
86 {
87 	Dwarf_Line *line;
88 	Dwarf_Addr laddr;
89 
90 	line = dwarf_getsrc_die(cu_die, (Dwarf_Addr)addr);
91 	if (line && dwarf_lineaddr(line, &laddr) == 0 &&
92 	    addr == (unsigned long)laddr && dwarf_lineno(line, lineno) == 0) {
93 		*fname = dwarf_linesrc(line, NULL, NULL);
94 		if (!*fname)
95 			/* line number is useless without filename */
96 			*lineno = 0;
97 	}
98 
99 	return *lineno ?: -ENOENT;
100 }
101 
102 static int __die_find_inline_cb(Dwarf_Die *die_mem, void *data);
103 
104 /**
105  * cu_walk_functions_at - Walk on function DIEs at given address
106  * @cu_die: A CU DIE
107  * @addr: An address
108  * @callback: A callback which called with found DIEs
109  * @data: A user data
110  *
111  * Walk on function DIEs at given @addr in @cu_die. Passed DIEs
112  * should be subprogram or inlined-subroutines.
113  */
cu_walk_functions_at(Dwarf_Die * cu_die,Dwarf_Addr addr,int (* callback)(Dwarf_Die *,void *),void * data)114 int cu_walk_functions_at(Dwarf_Die *cu_die, Dwarf_Addr addr,
115 		    int (*callback)(Dwarf_Die *, void *), void *data)
116 {
117 	Dwarf_Die die_mem;
118 	Dwarf_Die *sc_die;
119 	int ret = -ENOENT;
120 
121 	/* Inlined function could be recursive. Trace it until fail */
122 	for (sc_die = die_find_realfunc(cu_die, addr, &die_mem);
123 	     sc_die != NULL;
124 	     sc_die = die_find_child(sc_die, __die_find_inline_cb, &addr,
125 				     &die_mem)) {
126 		ret = callback(sc_die, data);
127 		if (ret)
128 			break;
129 	}
130 
131 	return ret;
132 
133 }
134 
135 /**
136  * die_get_linkage_name - Get the linkage name of the object
137  * @dw_die: A DIE of the object
138  *
139  * Get the linkage name attiribute of given @dw_die.
140  * For C++ binary, the linkage name will be the mangled symbol.
141  */
die_get_linkage_name(Dwarf_Die * dw_die)142 const char *die_get_linkage_name(Dwarf_Die *dw_die)
143 {
144 	Dwarf_Attribute attr;
145 
146 	if (dwarf_attr_integrate(dw_die, DW_AT_linkage_name, &attr) == NULL)
147 		return NULL;
148 	return dwarf_formstring(&attr);
149 }
150 
151 /**
152  * die_compare_name - Compare diename and tname
153  * @dw_die: a DIE
154  * @tname: a string of target name
155  *
156  * Compare the name of @dw_die and @tname. Return false if @dw_die has no name.
157  */
die_compare_name(Dwarf_Die * dw_die,const char * tname)158 bool die_compare_name(Dwarf_Die *dw_die, const char *tname)
159 {
160 	const char *name;
161 
162 	name = dwarf_diename(dw_die);
163 	return name ? (strcmp(tname, name) == 0) : false;
164 }
165 
166 /**
167  * die_match_name - Match diename/linkage name and glob
168  * @dw_die: a DIE
169  * @glob: a string of target glob pattern
170  *
171  * Glob matching the name of @dw_die and @glob. Return false if matching fail.
172  * This also match linkage name.
173  */
die_match_name(Dwarf_Die * dw_die,const char * glob)174 bool die_match_name(Dwarf_Die *dw_die, const char *glob)
175 {
176 	const char *name;
177 
178 	name = dwarf_diename(dw_die);
179 	if (name && strglobmatch(name, glob))
180 		return true;
181 	/* fall back to check linkage name */
182 	name = die_get_linkage_name(dw_die);
183 	if (name && strglobmatch(name, glob))
184 		return true;
185 
186 	return false;
187 }
188 
189 /**
190  * die_get_call_lineno - Get callsite line number of inline-function instance
191  * @in_die: a DIE of an inlined function instance
192  *
193  * Get call-site line number of @in_die. This means from where the inline
194  * function is called.
195  */
die_get_call_lineno(Dwarf_Die * in_die)196 int die_get_call_lineno(Dwarf_Die *in_die)
197 {
198 	Dwarf_Attribute attr;
199 	Dwarf_Word ret;
200 
201 	if (!dwarf_attr(in_die, DW_AT_call_line, &attr))
202 		return -ENOENT;
203 
204 	dwarf_formudata(&attr, &ret);
205 	return (int)ret;
206 }
207 
208 /**
209  * die_get_type - Get type DIE
210  * @vr_die: a DIE of a variable
211  * @die_mem: where to store a type DIE
212  *
213  * Get a DIE of the type of given variable (@vr_die), and store
214  * it to die_mem. Return NULL if fails to get a type DIE.
215  */
die_get_type(Dwarf_Die * vr_die,Dwarf_Die * die_mem)216 Dwarf_Die *die_get_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
217 {
218 	Dwarf_Attribute attr;
219 
220 	if (dwarf_attr_integrate(vr_die, DW_AT_type, &attr) &&
221 	    dwarf_formref_die(&attr, die_mem))
222 		return die_mem;
223 	else
224 		return NULL;
225 }
226 
227 /* Get a type die, but skip qualifiers */
__die_get_real_type(Dwarf_Die * vr_die,Dwarf_Die * die_mem)228 static Dwarf_Die *__die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
229 {
230 	int tag;
231 
232 	do {
233 		vr_die = die_get_type(vr_die, die_mem);
234 		if (!vr_die)
235 			break;
236 		tag = dwarf_tag(vr_die);
237 	} while (tag == DW_TAG_const_type ||
238 		 tag == DW_TAG_restrict_type ||
239 		 tag == DW_TAG_volatile_type ||
240 		 tag == DW_TAG_shared_type);
241 
242 	return vr_die;
243 }
244 
245 /**
246  * die_get_real_type - Get a type die, but skip qualifiers and typedef
247  * @vr_die: a DIE of a variable
248  * @die_mem: where to store a type DIE
249  *
250  * Get a DIE of the type of given variable (@vr_die), and store
251  * it to die_mem. Return NULL if fails to get a type DIE.
252  * If the type is qualifiers (e.g. const) or typedef, this skips it
253  * and tries to find real type (structure or basic types, e.g. int).
254  */
die_get_real_type(Dwarf_Die * vr_die,Dwarf_Die * die_mem)255 Dwarf_Die *die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
256 {
257 	do {
258 		vr_die = __die_get_real_type(vr_die, die_mem);
259 	} while (vr_die && dwarf_tag(vr_die) == DW_TAG_typedef);
260 
261 	return vr_die;
262 }
263 
264 /* Get attribute and translate it as a udata */
die_get_attr_udata(Dwarf_Die * tp_die,unsigned int attr_name,Dwarf_Word * result)265 static int die_get_attr_udata(Dwarf_Die *tp_die, unsigned int attr_name,
266 			      Dwarf_Word *result)
267 {
268 	Dwarf_Attribute attr;
269 
270 	if (dwarf_attr(tp_die, attr_name, &attr) == NULL ||
271 	    dwarf_formudata(&attr, result) != 0)
272 		return -ENOENT;
273 
274 	return 0;
275 }
276 
277 /* Get attribute and translate it as a sdata */
die_get_attr_sdata(Dwarf_Die * tp_die,unsigned int attr_name,Dwarf_Sword * result)278 static int die_get_attr_sdata(Dwarf_Die *tp_die, unsigned int attr_name,
279 			      Dwarf_Sword *result)
280 {
281 	Dwarf_Attribute attr;
282 
283 	if (dwarf_attr(tp_die, attr_name, &attr) == NULL ||
284 	    dwarf_formsdata(&attr, result) != 0)
285 		return -ENOENT;
286 
287 	return 0;
288 }
289 
290 /**
291  * die_is_signed_type - Check whether a type DIE is signed or not
292  * @tp_die: a DIE of a type
293  *
294  * Get the encoding of @tp_die and return true if the encoding
295  * is signed.
296  */
die_is_signed_type(Dwarf_Die * tp_die)297 bool die_is_signed_type(Dwarf_Die *tp_die)
298 {
299 	Dwarf_Word ret;
300 
301 	if (die_get_attr_udata(tp_die, DW_AT_encoding, &ret))
302 		return false;
303 
304 	return (ret == DW_ATE_signed_char || ret == DW_ATE_signed ||
305 		ret == DW_ATE_signed_fixed);
306 }
307 
308 /**
309  * die_is_func_def - Ensure that this DIE is a subprogram and definition
310  * @dw_die: a DIE
311  *
312  * Ensure that this DIE is a subprogram and NOT a declaration. This
313  * returns true if @dw_die is a function definition.
314  **/
die_is_func_def(Dwarf_Die * dw_die)315 bool die_is_func_def(Dwarf_Die *dw_die)
316 {
317 	Dwarf_Attribute attr;
318 
319 	return (dwarf_tag(dw_die) == DW_TAG_subprogram &&
320 		dwarf_attr(dw_die, DW_AT_declaration, &attr) == NULL);
321 }
322 
323 /**
324  * die_entrypc - Returns entry PC (the lowest address) of a DIE
325  * @dw_die: a DIE
326  * @addr: where to store entry PC
327  *
328  * Since dwarf_entrypc() does not return entry PC if the DIE has only address
329  * range, we have to use this to retrieve the lowest address from the address
330  * range attribute.
331  */
die_entrypc(Dwarf_Die * dw_die,Dwarf_Addr * addr)332 int die_entrypc(Dwarf_Die *dw_die, Dwarf_Addr *addr)
333 {
334 	Dwarf_Addr base, end;
335 
336 	if (!addr)
337 		return -EINVAL;
338 
339 	if (dwarf_entrypc(dw_die, addr) == 0)
340 		return 0;
341 
342 	return dwarf_ranges(dw_die, 0, &base, addr, &end) < 0 ? -ENOENT : 0;
343 }
344 
345 /**
346  * die_is_func_instance - Ensure that this DIE is an instance of a subprogram
347  * @dw_die: a DIE
348  *
349  * Ensure that this DIE is an instance (which has an entry address).
350  * This returns true if @dw_die is a function instance. If not, the @dw_die
351  * must be a prototype. You can use die_walk_instances() to find actual
352  * instances.
353  **/
die_is_func_instance(Dwarf_Die * dw_die)354 bool die_is_func_instance(Dwarf_Die *dw_die)
355 {
356 	Dwarf_Addr tmp;
357 	Dwarf_Attribute attr_mem;
358 	int tag = dwarf_tag(dw_die);
359 
360 	if (tag != DW_TAG_subprogram &&
361 	    tag != DW_TAG_inlined_subroutine)
362 		return false;
363 
364 	return dwarf_entrypc(dw_die, &tmp) == 0 ||
365 		dwarf_attr(dw_die, DW_AT_ranges, &attr_mem) != NULL;
366 }
367 
368 /**
369  * die_get_data_member_location - Get the data-member offset
370  * @mb_die: a DIE of a member of a data structure
371  * @offs: The offset of the member in the data structure
372  *
373  * Get the offset of @mb_die in the data structure including @mb_die, and
374  * stores result offset to @offs. If any error occurs this returns errno.
375  */
die_get_data_member_location(Dwarf_Die * mb_die,Dwarf_Word * offs)376 int die_get_data_member_location(Dwarf_Die *mb_die, Dwarf_Word *offs)
377 {
378 	Dwarf_Attribute attr;
379 	Dwarf_Op *expr;
380 	size_t nexpr;
381 	int ret;
382 
383 	if (dwarf_attr(mb_die, DW_AT_data_member_location, &attr) == NULL)
384 		return -ENOENT;
385 
386 	if (dwarf_formudata(&attr, offs) != 0) {
387 		/* DW_AT_data_member_location should be DW_OP_plus_uconst */
388 		ret = dwarf_getlocation(&attr, &expr, &nexpr);
389 		if (ret < 0 || nexpr == 0)
390 			return -ENOENT;
391 
392 		if (expr[0].atom != DW_OP_plus_uconst || nexpr != 1) {
393 			pr_debug("Unable to get offset:Unexpected OP %x (%zd)\n",
394 				 expr[0].atom, nexpr);
395 			return -ENOTSUP;
396 		}
397 		*offs = (Dwarf_Word)expr[0].number;
398 	}
399 	return 0;
400 }
401 
402 /* Get the call file index number in CU DIE */
die_get_call_fileno(Dwarf_Die * in_die)403 static int die_get_call_fileno(Dwarf_Die *in_die)
404 {
405 	Dwarf_Sword idx;
406 
407 	if (die_get_attr_sdata(in_die, DW_AT_call_file, &idx) == 0)
408 		return (int)idx;
409 	else
410 		return -ENOENT;
411 }
412 
413 /* Get the declared file index number in CU DIE */
die_get_decl_fileno(Dwarf_Die * pdie)414 static int die_get_decl_fileno(Dwarf_Die *pdie)
415 {
416 	Dwarf_Sword idx;
417 
418 	if (die_get_attr_sdata(pdie, DW_AT_decl_file, &idx) == 0)
419 		return (int)idx;
420 	else
421 		return -ENOENT;
422 }
423 
424 /**
425  * die_get_call_file - Get callsite file name of inlined function instance
426  * @in_die: a DIE of an inlined function instance
427  *
428  * Get call-site file name of @in_die. This means from which file the inline
429  * function is called.
430  */
die_get_call_file(Dwarf_Die * in_die)431 const char *die_get_call_file(Dwarf_Die *in_die)
432 {
433 	Dwarf_Die cu_die;
434 	Dwarf_Files *files;
435 	int idx;
436 
437 	idx = die_get_call_fileno(in_die);
438 	if (idx < 0 || !dwarf_diecu(in_die, &cu_die, NULL, NULL) ||
439 	    dwarf_getsrcfiles(&cu_die, &files, NULL) != 0)
440 		return NULL;
441 
442 	return dwarf_filesrc(files, idx, NULL, NULL);
443 }
444 
445 
446 /**
447  * die_find_child - Generic DIE search function in DIE tree
448  * @rt_die: a root DIE
449  * @callback: a callback function
450  * @data: a user data passed to the callback function
451  * @die_mem: a buffer for result DIE
452  *
453  * Trace DIE tree from @rt_die and call @callback for each child DIE.
454  * If @callback returns DIE_FIND_CB_END, this stores the DIE into
455  * @die_mem and returns it. If @callback returns DIE_FIND_CB_CONTINUE,
456  * this continues to trace the tree. Optionally, @callback can return
457  * DIE_FIND_CB_CHILD and DIE_FIND_CB_SIBLING, those means trace only
458  * the children and trace only the siblings respectively.
459  * Returns NULL if @callback can't find any appropriate DIE.
460  */
die_find_child(Dwarf_Die * rt_die,int (* callback)(Dwarf_Die *,void *),void * data,Dwarf_Die * die_mem)461 Dwarf_Die *die_find_child(Dwarf_Die *rt_die,
462 			  int (*callback)(Dwarf_Die *, void *),
463 			  void *data, Dwarf_Die *die_mem)
464 {
465 	Dwarf_Die child_die;
466 	int ret;
467 
468 	ret = dwarf_child(rt_die, die_mem);
469 	if (ret != 0)
470 		return NULL;
471 
472 	do {
473 		ret = callback(die_mem, data);
474 		if (ret == DIE_FIND_CB_END)
475 			return die_mem;
476 
477 		if ((ret & DIE_FIND_CB_CHILD) &&
478 		    die_find_child(die_mem, callback, data, &child_die)) {
479 			memcpy(die_mem, &child_die, sizeof(Dwarf_Die));
480 			return die_mem;
481 		}
482 	} while ((ret & DIE_FIND_CB_SIBLING) &&
483 		 dwarf_siblingof(die_mem, die_mem) == 0);
484 
485 	return NULL;
486 }
487 
488 struct __addr_die_search_param {
489 	Dwarf_Addr	addr;
490 	Dwarf_Die	*die_mem;
491 };
492 
__die_search_func_tail_cb(Dwarf_Die * fn_die,void * data)493 static int __die_search_func_tail_cb(Dwarf_Die *fn_die, void *data)
494 {
495 	struct __addr_die_search_param *ad = data;
496 	Dwarf_Addr addr = 0;
497 
498 	if (dwarf_tag(fn_die) == DW_TAG_subprogram &&
499 	    !dwarf_highpc(fn_die, &addr) &&
500 	    addr == ad->addr) {
501 		memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die));
502 		return DWARF_CB_ABORT;
503 	}
504 	return DWARF_CB_OK;
505 }
506 
507 /**
508  * die_find_tailfunc - Search for a non-inlined function with tail call at
509  * given address
510  * @cu_die: a CU DIE which including @addr
511  * @addr: target address
512  * @die_mem: a buffer for result DIE
513  *
514  * Search for a non-inlined function DIE with tail call at @addr. Stores the
515  * DIE to @die_mem and returns it if found. Returns NULL if failed.
516  */
die_find_tailfunc(Dwarf_Die * cu_die,Dwarf_Addr addr,Dwarf_Die * die_mem)517 Dwarf_Die *die_find_tailfunc(Dwarf_Die *cu_die, Dwarf_Addr addr,
518 				    Dwarf_Die *die_mem)
519 {
520 	struct __addr_die_search_param ad;
521 	ad.addr = addr;
522 	ad.die_mem = die_mem;
523 	/* dwarf_getscopes can't find subprogram. */
524 	if (!dwarf_getfuncs(cu_die, __die_search_func_tail_cb, &ad, 0))
525 		return NULL;
526 	else
527 		return die_mem;
528 }
529 
530 /* die_find callback for non-inlined function search */
__die_search_func_cb(Dwarf_Die * fn_die,void * data)531 static int __die_search_func_cb(Dwarf_Die *fn_die, void *data)
532 {
533 	struct __addr_die_search_param *ad = data;
534 
535 	/*
536 	 * Since a declaration entry doesn't has given pc, this always returns
537 	 * function definition entry.
538 	 */
539 	if (dwarf_tag(fn_die) == DW_TAG_subprogram &&
540 	    dwarf_haspc(fn_die, ad->addr)) {
541 		memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die));
542 		return DWARF_CB_ABORT;
543 	}
544 	return DWARF_CB_OK;
545 }
546 
547 /**
548  * die_find_realfunc - Search a non-inlined function at given address
549  * @cu_die: a CU DIE which including @addr
550  * @addr: target address
551  * @die_mem: a buffer for result DIE
552  *
553  * Search a non-inlined function DIE which includes @addr. Stores the
554  * DIE to @die_mem and returns it if found. Returns NULL if failed.
555  */
die_find_realfunc(Dwarf_Die * cu_die,Dwarf_Addr addr,Dwarf_Die * die_mem)556 Dwarf_Die *die_find_realfunc(Dwarf_Die *cu_die, Dwarf_Addr addr,
557 				    Dwarf_Die *die_mem)
558 {
559 	struct __addr_die_search_param ad;
560 	ad.addr = addr;
561 	ad.die_mem = die_mem;
562 	/* dwarf_getscopes can't find subprogram. */
563 	if (!dwarf_getfuncs(cu_die, __die_search_func_cb, &ad, 0))
564 		return NULL;
565 	else
566 		return die_mem;
567 }
568 
569 /* die_find callback for inline function search */
__die_find_inline_cb(Dwarf_Die * die_mem,void * data)570 static int __die_find_inline_cb(Dwarf_Die *die_mem, void *data)
571 {
572 	Dwarf_Addr *addr = data;
573 
574 	if (dwarf_tag(die_mem) == DW_TAG_inlined_subroutine &&
575 	    dwarf_haspc(die_mem, *addr))
576 		return DIE_FIND_CB_END;
577 
578 	return DIE_FIND_CB_CONTINUE;
579 }
580 
581 /**
582  * die_find_top_inlinefunc - Search the top inlined function at given address
583  * @sp_die: a subprogram DIE which including @addr
584  * @addr: target address
585  * @die_mem: a buffer for result DIE
586  *
587  * Search an inlined function DIE which includes @addr. Stores the
588  * DIE to @die_mem and returns it if found. Returns NULL if failed.
589  * Even if several inlined functions are expanded recursively, this
590  * doesn't trace it down, and returns the topmost one.
591  */
die_find_top_inlinefunc(Dwarf_Die * sp_die,Dwarf_Addr addr,Dwarf_Die * die_mem)592 Dwarf_Die *die_find_top_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
593 				   Dwarf_Die *die_mem)
594 {
595 	return die_find_child(sp_die, __die_find_inline_cb, &addr, die_mem);
596 }
597 
598 /**
599  * die_find_inlinefunc - Search an inlined function at given address
600  * @sp_die: a subprogram DIE which including @addr
601  * @addr: target address
602  * @die_mem: a buffer for result DIE
603  *
604  * Search an inlined function DIE which includes @addr. Stores the
605  * DIE to @die_mem and returns it if found. Returns NULL if failed.
606  * If several inlined functions are expanded recursively, this trace
607  * it down and returns deepest one.
608  */
die_find_inlinefunc(Dwarf_Die * sp_die,Dwarf_Addr addr,Dwarf_Die * die_mem)609 Dwarf_Die *die_find_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
610 			       Dwarf_Die *die_mem)
611 {
612 	Dwarf_Die tmp_die;
613 
614 	sp_die = die_find_child(sp_die, __die_find_inline_cb, &addr, &tmp_die);
615 	if (!sp_die)
616 		return NULL;
617 
618 	/* Inlined function could be recursive. Trace it until fail */
619 	while (sp_die) {
620 		memcpy(die_mem, sp_die, sizeof(Dwarf_Die));
621 		sp_die = die_find_child(sp_die, __die_find_inline_cb, &addr,
622 					&tmp_die);
623 	}
624 
625 	return die_mem;
626 }
627 
628 struct __instance_walk_param {
629 	void    *addr;
630 	int	(*callback)(Dwarf_Die *, void *);
631 	void    *data;
632 	int	retval;
633 };
634 
__die_walk_instances_cb(Dwarf_Die * inst,void * data)635 static int __die_walk_instances_cb(Dwarf_Die *inst, void *data)
636 {
637 	struct __instance_walk_param *iwp = data;
638 	Dwarf_Attribute attr_mem;
639 	Dwarf_Die origin_mem;
640 	Dwarf_Attribute *attr;
641 	Dwarf_Die *origin;
642 	int tmp;
643 
644 	if (!die_is_func_instance(inst))
645 		return DIE_FIND_CB_CONTINUE;
646 
647 	attr = dwarf_attr(inst, DW_AT_abstract_origin, &attr_mem);
648 	if (attr == NULL)
649 		return DIE_FIND_CB_CONTINUE;
650 
651 	origin = dwarf_formref_die(attr, &origin_mem);
652 	if (origin == NULL || origin->addr != iwp->addr)
653 		return DIE_FIND_CB_CONTINUE;
654 
655 	/* Ignore redundant instances */
656 	if (dwarf_tag(inst) == DW_TAG_inlined_subroutine) {
657 		dwarf_decl_line(origin, &tmp);
658 		if (die_get_call_lineno(inst) == tmp) {
659 			tmp = die_get_decl_fileno(origin);
660 			if (die_get_call_fileno(inst) == tmp)
661 				return DIE_FIND_CB_CONTINUE;
662 		}
663 	}
664 
665 	iwp->retval = iwp->callback(inst, iwp->data);
666 
667 	return (iwp->retval) ? DIE_FIND_CB_END : DIE_FIND_CB_CONTINUE;
668 }
669 
670 /**
671  * die_walk_instances - Walk on instances of given DIE
672  * @or_die: an abstract original DIE
673  * @callback: a callback function which is called with instance DIE
674  * @data: user data
675  *
676  * Walk on the instances of give @in_die. @in_die must be an inlined function
677  * declartion. This returns the return value of @callback if it returns
678  * non-zero value, or -ENOENT if there is no instance.
679  */
die_walk_instances(Dwarf_Die * or_die,int (* callback)(Dwarf_Die *,void *),void * data)680 int die_walk_instances(Dwarf_Die *or_die, int (*callback)(Dwarf_Die *, void *),
681 		       void *data)
682 {
683 	Dwarf_Die cu_die;
684 	Dwarf_Die die_mem;
685 	struct __instance_walk_param iwp = {
686 		.addr = or_die->addr,
687 		.callback = callback,
688 		.data = data,
689 		.retval = -ENOENT,
690 	};
691 
692 	if (dwarf_diecu(or_die, &cu_die, NULL, NULL) == NULL)
693 		return -ENOENT;
694 
695 	die_find_child(&cu_die, __die_walk_instances_cb, &iwp, &die_mem);
696 
697 	return iwp.retval;
698 }
699 
700 /* Line walker internal parameters */
701 struct __line_walk_param {
702 	bool recursive;
703 	line_walk_callback_t callback;
704 	void *data;
705 	int retval;
706 };
707 
__die_walk_funclines_cb(Dwarf_Die * in_die,void * data)708 static int __die_walk_funclines_cb(Dwarf_Die *in_die, void *data)
709 {
710 	struct __line_walk_param *lw = data;
711 	Dwarf_Addr addr = 0;
712 	const char *fname;
713 	int lineno;
714 
715 	if (dwarf_tag(in_die) == DW_TAG_inlined_subroutine) {
716 		fname = die_get_call_file(in_die);
717 		lineno = die_get_call_lineno(in_die);
718 		if (fname && lineno > 0 && die_entrypc(in_die, &addr) == 0) {
719 			lw->retval = lw->callback(fname, lineno, addr, lw->data);
720 			if (lw->retval != 0)
721 				return DIE_FIND_CB_END;
722 		}
723 		if (!lw->recursive)
724 			return DIE_FIND_CB_SIBLING;
725 	}
726 
727 	if (addr) {
728 		fname = dwarf_decl_file(in_die);
729 		if (fname && dwarf_decl_line(in_die, &lineno) == 0) {
730 			lw->retval = lw->callback(fname, lineno, addr, lw->data);
731 			if (lw->retval != 0)
732 				return DIE_FIND_CB_END;
733 		}
734 	}
735 
736 	/* Continue to search nested inlined function call-sites */
737 	return DIE_FIND_CB_CONTINUE;
738 }
739 
740 /* 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)741 static int __die_walk_funclines(Dwarf_Die *sp_die, bool recursive,
742 				line_walk_callback_t callback, void *data)
743 {
744 	struct __line_walk_param lw = {
745 		.recursive = recursive,
746 		.callback = callback,
747 		.data = data,
748 		.retval = 0,
749 	};
750 	Dwarf_Die die_mem;
751 	Dwarf_Addr addr;
752 	const char *fname;
753 	int lineno;
754 
755 	/* Handle function declaration line */
756 	fname = dwarf_decl_file(sp_die);
757 	if (fname && dwarf_decl_line(sp_die, &lineno) == 0 &&
758 	    die_entrypc(sp_die, &addr) == 0) {
759 		lw.retval = callback(fname, lineno, addr, data);
760 		if (lw.retval != 0)
761 			goto done;
762 	}
763 	die_find_child(sp_die, __die_walk_funclines_cb, &lw, &die_mem);
764 done:
765 	return lw.retval;
766 }
767 
__die_walk_culines_cb(Dwarf_Die * sp_die,void * data)768 static int __die_walk_culines_cb(Dwarf_Die *sp_die, void *data)
769 {
770 	struct __line_walk_param *lw = data;
771 
772 	/*
773 	 * Since inlined function can include another inlined function in
774 	 * the same file, we need to walk in it recursively.
775 	 */
776 	lw->retval = __die_walk_funclines(sp_die, true, lw->callback, lw->data);
777 	if (lw->retval != 0)
778 		return DWARF_CB_ABORT;
779 
780 	return DWARF_CB_OK;
781 }
782 
783 /**
784  * die_walk_lines - Walk on lines inside given DIE
785  * @rt_die: a root DIE (CU, subprogram or inlined_subroutine)
786  * @callback: callback routine
787  * @data: user data
788  *
789  * Walk on all lines inside given @rt_die and call @callback on each line.
790  * If the @rt_die is a function, walk only on the lines inside the function,
791  * otherwise @rt_die must be a CU DIE.
792  * Note that this walks not only dwarf line list, but also function entries
793  * and inline call-site.
794  */
die_walk_lines(Dwarf_Die * rt_die,line_walk_callback_t callback,void * data)795 int die_walk_lines(Dwarf_Die *rt_die, line_walk_callback_t callback, void *data)
796 {
797 	Dwarf_Lines *lines;
798 	Dwarf_Line *line;
799 	Dwarf_Addr addr;
800 	const char *fname, *decf = NULL, *inf = NULL;
801 	int lineno, ret = 0;
802 	int decl = 0, inl;
803 	Dwarf_Die die_mem, *cu_die;
804 	size_t nlines, i;
805 	bool flag;
806 
807 	/* Get the CU die */
808 	if (dwarf_tag(rt_die) != DW_TAG_compile_unit) {
809 		cu_die = dwarf_diecu(rt_die, &die_mem, NULL, NULL);
810 		dwarf_decl_line(rt_die, &decl);
811 		decf = dwarf_decl_file(rt_die);
812 	} else
813 		cu_die = rt_die;
814 	if (!cu_die) {
815 		pr_debug2("Failed to get CU from given DIE.\n");
816 		return -EINVAL;
817 	}
818 
819 	/* Get lines list in the CU */
820 	if (dwarf_getsrclines(cu_die, &lines, &nlines) != 0) {
821 		pr_debug2("Failed to get source lines on this CU.\n");
822 		return -ENOENT;
823 	}
824 	pr_debug2("Get %zd lines from this CU\n", nlines);
825 
826 	/* Walk on the lines on lines list */
827 	for (i = 0; i < nlines; i++) {
828 		line = dwarf_onesrcline(lines, i);
829 		if (line == NULL ||
830 		    dwarf_lineno(line, &lineno) != 0 ||
831 		    dwarf_lineaddr(line, &addr) != 0) {
832 			pr_debug2("Failed to get line info. "
833 				  "Possible error in debuginfo.\n");
834 			continue;
835 		}
836 		/* Skip end-of-sequence */
837 		if (dwarf_lineendsequence(line, &flag) != 0 || flag)
838 			continue;
839 		/* Skip Non statement line-info */
840 		if (dwarf_linebeginstatement(line, &flag) != 0 || !flag)
841 			continue;
842 		/* Filter lines based on address */
843 		if (rt_die != cu_die) {
844 			/*
845 			 * Address filtering
846 			 * The line is included in given function, and
847 			 * no inline block includes it.
848 			 */
849 			if (!dwarf_haspc(rt_die, addr))
850 				continue;
851 
852 			if (die_find_inlinefunc(rt_die, addr, &die_mem)) {
853 				/* Call-site check */
854 				inf = die_get_call_file(&die_mem);
855 				if ((inf && !strcmp(inf, decf)) &&
856 				    die_get_call_lineno(&die_mem) == lineno)
857 					goto found;
858 
859 				dwarf_decl_line(&die_mem, &inl);
860 				if (inl != decl ||
861 				    decf != dwarf_decl_file(&die_mem))
862 					continue;
863 			}
864 		}
865 found:
866 		/* Get source line */
867 		fname = dwarf_linesrc(line, NULL, NULL);
868 
869 		ret = callback(fname, lineno, addr, data);
870 		if (ret != 0)
871 			return ret;
872 	}
873 
874 	/*
875 	 * Dwarf lines doesn't include function declarations and inlined
876 	 * subroutines. We have to check functions list or given function.
877 	 */
878 	if (rt_die != cu_die)
879 		/*
880 		 * Don't need walk inlined functions recursively, because
881 		 * inner inlined functions don't have the lines of the
882 		 * specified function.
883 		 */
884 		ret = __die_walk_funclines(rt_die, false, callback, data);
885 	else {
886 		struct __line_walk_param param = {
887 			.callback = callback,
888 			.data = data,
889 			.retval = 0,
890 		};
891 		dwarf_getfuncs(cu_die, __die_walk_culines_cb, &param, 0);
892 		ret = param.retval;
893 	}
894 
895 	return ret;
896 }
897 
898 struct __find_variable_param {
899 	const char *name;
900 	Dwarf_Addr addr;
901 };
902 
__die_find_variable_cb(Dwarf_Die * die_mem,void * data)903 static int __die_find_variable_cb(Dwarf_Die *die_mem, void *data)
904 {
905 	struct __find_variable_param *fvp = data;
906 	Dwarf_Attribute attr;
907 	int tag;
908 
909 	tag = dwarf_tag(die_mem);
910 	if ((tag == DW_TAG_formal_parameter ||
911 	     tag == DW_TAG_variable) &&
912 	    die_compare_name(die_mem, fvp->name) &&
913 	/* Does the DIE have location information or external instance? */
914 	    (dwarf_attr(die_mem, DW_AT_external, &attr) ||
915 	     dwarf_attr(die_mem, DW_AT_location, &attr)))
916 		return DIE_FIND_CB_END;
917 	if (dwarf_haspc(die_mem, fvp->addr))
918 		return DIE_FIND_CB_CONTINUE;
919 	else
920 		return DIE_FIND_CB_SIBLING;
921 }
922 
923 /**
924  * die_find_variable_at - Find a given name variable at given address
925  * @sp_die: a function DIE
926  * @name: variable name
927  * @addr: address
928  * @die_mem: a buffer for result DIE
929  *
930  * Find a variable DIE called @name at @addr in @sp_die.
931  */
die_find_variable_at(Dwarf_Die * sp_die,const char * name,Dwarf_Addr addr,Dwarf_Die * die_mem)932 Dwarf_Die *die_find_variable_at(Dwarf_Die *sp_die, const char *name,
933 				Dwarf_Addr addr, Dwarf_Die *die_mem)
934 {
935 	struct __find_variable_param fvp = { .name = name, .addr = addr};
936 
937 	return die_find_child(sp_die, __die_find_variable_cb, (void *)&fvp,
938 			      die_mem);
939 }
940 
__die_find_member_cb(Dwarf_Die * die_mem,void * data)941 static int __die_find_member_cb(Dwarf_Die *die_mem, void *data)
942 {
943 	const char *name = data;
944 
945 	if (dwarf_tag(die_mem) == DW_TAG_member) {
946 		if (die_compare_name(die_mem, name))
947 			return DIE_FIND_CB_END;
948 		else if (!dwarf_diename(die_mem)) {	/* Unnamed structure */
949 			Dwarf_Die type_die, tmp_die;
950 			if (die_get_type(die_mem, &type_die) &&
951 			    die_find_member(&type_die, name, &tmp_die))
952 				return DIE_FIND_CB_END;
953 		}
954 	}
955 	return DIE_FIND_CB_SIBLING;
956 }
957 
958 /**
959  * die_find_member - Find a given name member in a data structure
960  * @st_die: a data structure type DIE
961  * @name: member name
962  * @die_mem: a buffer for result DIE
963  *
964  * Find a member DIE called @name in @st_die.
965  */
die_find_member(Dwarf_Die * st_die,const char * name,Dwarf_Die * die_mem)966 Dwarf_Die *die_find_member(Dwarf_Die *st_die, const char *name,
967 			   Dwarf_Die *die_mem)
968 {
969 	return die_find_child(st_die, __die_find_member_cb, (void *)name,
970 			      die_mem);
971 }
972 
973 /**
974  * die_get_typename - Get the name of given variable DIE
975  * @vr_die: a variable DIE
976  * @buf: a strbuf for result type name
977  *
978  * Get the name of @vr_die and stores it to @buf. Return 0 if succeeded.
979  * and Return -ENOENT if failed to find type name.
980  * Note that the result will stores typedef name if possible, and stores
981  * "*(function_type)" if the type is a function pointer.
982  */
die_get_typename(Dwarf_Die * vr_die,struct strbuf * buf)983 int die_get_typename(Dwarf_Die *vr_die, struct strbuf *buf)
984 {
985 	Dwarf_Die type;
986 	int tag, ret;
987 	const char *tmp = "";
988 
989 	if (__die_get_real_type(vr_die, &type) == NULL)
990 		return -ENOENT;
991 
992 	tag = dwarf_tag(&type);
993 	if (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type)
994 		tmp = "*";
995 	else if (tag == DW_TAG_subroutine_type) {
996 		/* Function pointer */
997 		return strbuf_add(buf, "(function_type)", 15);
998 	} else {
999 		if (!dwarf_diename(&type))
1000 			return -ENOENT;
1001 		if (tag == DW_TAG_union_type)
1002 			tmp = "union ";
1003 		else if (tag == DW_TAG_structure_type)
1004 			tmp = "struct ";
1005 		else if (tag == DW_TAG_enumeration_type)
1006 			tmp = "enum ";
1007 		/* Write a base name */
1008 		return strbuf_addf(buf, "%s%s", tmp, dwarf_diename(&type));
1009 	}
1010 	ret = die_get_typename(&type, buf);
1011 	return ret ? ret : strbuf_addstr(buf, tmp);
1012 }
1013 
1014 /**
1015  * die_get_varname - Get the name and type of given variable DIE
1016  * @vr_die: a variable DIE
1017  * @buf: a strbuf for type and variable name
1018  *
1019  * Get the name and type of @vr_die and stores it in @buf as "type\tname".
1020  */
die_get_varname(Dwarf_Die * vr_die,struct strbuf * buf)1021 int die_get_varname(Dwarf_Die *vr_die, struct strbuf *buf)
1022 {
1023 	int ret;
1024 
1025 	ret = die_get_typename(vr_die, buf);
1026 	if (ret < 0) {
1027 		pr_debug("Failed to get type, make it unknown.\n");
1028 		ret = strbuf_add(buf, " (unknown_type)", 14);
1029 	}
1030 
1031 	return ret < 0 ? ret : strbuf_addf(buf, "\t%s", dwarf_diename(vr_die));
1032 }
1033 
1034 #ifdef HAVE_DWARF_GETLOCATIONS_SUPPORT
1035 /**
1036  * die_get_var_innermost_scope - Get innermost scope range of given variable DIE
1037  * @sp_die: a subprogram DIE
1038  * @vr_die: a variable DIE
1039  * @buf: a strbuf for variable byte offset range
1040  *
1041  * Get the innermost scope range of @vr_die and stores it in @buf as
1042  * "@<function_name+[NN-NN,NN-NN]>".
1043  */
die_get_var_innermost_scope(Dwarf_Die * sp_die,Dwarf_Die * vr_die,struct strbuf * buf)1044 static int die_get_var_innermost_scope(Dwarf_Die *sp_die, Dwarf_Die *vr_die,
1045 				struct strbuf *buf)
1046 {
1047 	Dwarf_Die *scopes;
1048 	int count;
1049 	size_t offset = 0;
1050 	Dwarf_Addr base;
1051 	Dwarf_Addr start, end;
1052 	Dwarf_Addr entry;
1053 	int ret;
1054 	bool first = true;
1055 	const char *name;
1056 
1057 	ret = die_entrypc(sp_die, &entry);
1058 	if (ret)
1059 		return ret;
1060 
1061 	name = dwarf_diename(sp_die);
1062 	if (!name)
1063 		return -ENOENT;
1064 
1065 	count = dwarf_getscopes_die(vr_die, &scopes);
1066 
1067 	/* (*SCOPES)[1] is the DIE for the scope containing that scope */
1068 	if (count <= 1) {
1069 		ret = -EINVAL;
1070 		goto out;
1071 	}
1072 
1073 	while ((offset = dwarf_ranges(&scopes[1], offset, &base,
1074 					&start, &end)) > 0) {
1075 		start -= entry;
1076 		end -= entry;
1077 
1078 		if (first) {
1079 			ret = strbuf_addf(buf, "@<%s+[%" PRIu64 "-%" PRIu64,
1080 					  name, start, end);
1081 			first = false;
1082 		} else {
1083 			ret = strbuf_addf(buf, ",%" PRIu64 "-%" PRIu64,
1084 					  start, end);
1085 		}
1086 		if (ret < 0)
1087 			goto out;
1088 	}
1089 
1090 	if (!first)
1091 		ret = strbuf_add(buf, "]>", 2);
1092 
1093 out:
1094 	free(scopes);
1095 	return ret;
1096 }
1097 
1098 /**
1099  * die_get_var_range - Get byte offset range of given variable DIE
1100  * @sp_die: a subprogram DIE
1101  * @vr_die: a variable DIE
1102  * @buf: a strbuf for type and variable name and byte offset range
1103  *
1104  * Get the byte offset range of @vr_die and stores it in @buf as
1105  * "@<function_name+[NN-NN,NN-NN]>".
1106  */
die_get_var_range(Dwarf_Die * sp_die,Dwarf_Die * vr_die,struct strbuf * buf)1107 int die_get_var_range(Dwarf_Die *sp_die, Dwarf_Die *vr_die, struct strbuf *buf)
1108 {
1109 	int ret = 0;
1110 	Dwarf_Addr base;
1111 	Dwarf_Addr start, end;
1112 	Dwarf_Addr entry;
1113 	Dwarf_Op *op;
1114 	size_t nops;
1115 	size_t offset = 0;
1116 	Dwarf_Attribute attr;
1117 	bool first = true;
1118 	const char *name;
1119 
1120 	ret = die_entrypc(sp_die, &entry);
1121 	if (ret)
1122 		return ret;
1123 
1124 	name = dwarf_diename(sp_die);
1125 	if (!name)
1126 		return -ENOENT;
1127 
1128 	if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL)
1129 		return -EINVAL;
1130 
1131 	while ((offset = dwarf_getlocations(&attr, offset, &base,
1132 					&start, &end, &op, &nops)) > 0) {
1133 		if (start == 0) {
1134 			/* Single Location Descriptions */
1135 			ret = die_get_var_innermost_scope(sp_die, vr_die, buf);
1136 			goto out;
1137 		}
1138 
1139 		/* Location Lists */
1140 		start -= entry;
1141 		end -= entry;
1142 		if (first) {
1143 			ret = strbuf_addf(buf, "@<%s+[%" PRIu64 "-%" PRIu64,
1144 					  name, start, end);
1145 			first = false;
1146 		} else {
1147 			ret = strbuf_addf(buf, ",%" PRIu64 "-%" PRIu64,
1148 					  start, end);
1149 		}
1150 		if (ret < 0)
1151 			goto out;
1152 	}
1153 
1154 	if (!first)
1155 		ret = strbuf_add(buf, "]>", 2);
1156 out:
1157 	return ret;
1158 }
1159 #else
die_get_var_range(Dwarf_Die * sp_die __maybe_unused,Dwarf_Die * vr_die __maybe_unused,struct strbuf * buf __maybe_unused)1160 int die_get_var_range(Dwarf_Die *sp_die __maybe_unused,
1161 		      Dwarf_Die *vr_die __maybe_unused,
1162 		      struct strbuf *buf __maybe_unused)
1163 {
1164 	return -ENOTSUP;
1165 }
1166 #endif
1167 
1168 /*
1169  * die_has_loclist - Check if DW_AT_location of @vr_die is a location list
1170  * @vr_die: a variable DIE
1171  */
die_has_loclist(Dwarf_Die * vr_die)1172 static bool die_has_loclist(Dwarf_Die *vr_die)
1173 {
1174 	Dwarf_Attribute loc;
1175 	int tag = dwarf_tag(vr_die);
1176 
1177 	if (tag != DW_TAG_formal_parameter &&
1178 	    tag != DW_TAG_variable)
1179 		return false;
1180 
1181 	return (dwarf_attr_integrate(vr_die, DW_AT_location, &loc) &&
1182 		dwarf_whatform(&loc) == DW_FORM_sec_offset);
1183 }
1184 
1185 /*
1186  * die_is_optimized_target - Check if target program is compiled with
1187  * optimization
1188  * @cu_die: a CU DIE
1189  *
1190  * For any object in given CU whose DW_AT_location is a location list,
1191  * target program is compiled with optimization. This is applicable to
1192  * clang as well.
1193  */
die_is_optimized_target(Dwarf_Die * cu_die)1194 bool die_is_optimized_target(Dwarf_Die *cu_die)
1195 {
1196 	Dwarf_Die tmp_die;
1197 
1198 	if (die_has_loclist(cu_die))
1199 		return true;
1200 
1201 	if (!dwarf_child(cu_die, &tmp_die) &&
1202 	    die_is_optimized_target(&tmp_die))
1203 		return true;
1204 
1205 	if (!dwarf_siblingof(cu_die, &tmp_die) &&
1206 	    die_is_optimized_target(&tmp_die))
1207 		return true;
1208 
1209 	return false;
1210 }
1211 
1212 /*
1213  * die_search_idx - Search index of given line address
1214  * @lines: Line records of single CU
1215  * @nr_lines: Number of @lines
1216  * @addr: address we are looking for
1217  * @idx: index to be set by this function (return value)
1218  *
1219  * Search for @addr by looping over every lines of CU. If address
1220  * matches, set index of that line in @idx. Note that single source
1221  * line can have multiple line records. i.e. single source line can
1222  * have multiple index.
1223  */
die_search_idx(Dwarf_Lines * lines,unsigned long nr_lines,Dwarf_Addr addr,unsigned long * idx)1224 static bool die_search_idx(Dwarf_Lines *lines, unsigned long nr_lines,
1225 			   Dwarf_Addr addr, unsigned long *idx)
1226 {
1227 	unsigned long i;
1228 	Dwarf_Addr tmp;
1229 
1230 	for (i = 0; i < nr_lines; i++) {
1231 		if (dwarf_lineaddr(dwarf_onesrcline(lines, i), &tmp))
1232 			return false;
1233 
1234 		if (tmp == addr) {
1235 			*idx = i;
1236 			return true;
1237 		}
1238 	}
1239 	return false;
1240 }
1241 
1242 /*
1243  * die_get_postprologue_addr - Search next address after function prologue
1244  * @entrypc_idx: entrypc index
1245  * @lines: Line records of single CU
1246  * @nr_lines: Number of @lines
1247  * @hignpc: high PC address of function
1248  * @postprologue_addr: Next address after function prologue (return value)
1249  *
1250  * Look for prologue-end marker. If there is no explicit marker, return
1251  * address of next line record or next source line.
1252  */
die_get_postprologue_addr(unsigned long entrypc_idx,Dwarf_Lines * lines,unsigned long nr_lines,Dwarf_Addr highpc,Dwarf_Addr * postprologue_addr)1253 static bool die_get_postprologue_addr(unsigned long entrypc_idx,
1254 				      Dwarf_Lines *lines,
1255 				      unsigned long nr_lines,
1256 				      Dwarf_Addr highpc,
1257 				      Dwarf_Addr *postprologue_addr)
1258 {
1259 	unsigned long i;
1260 	int entrypc_lno, lno;
1261 	Dwarf_Line *line;
1262 	Dwarf_Addr addr;
1263 	bool p_end;
1264 
1265 	/* entrypc_lno is actual source line number */
1266 	line = dwarf_onesrcline(lines, entrypc_idx);
1267 	if (dwarf_lineno(line, &entrypc_lno))
1268 		return false;
1269 
1270 	for (i = entrypc_idx; i < nr_lines; i++) {
1271 		line = dwarf_onesrcline(lines, i);
1272 
1273 		if (dwarf_lineaddr(line, &addr) ||
1274 		    dwarf_lineno(line, &lno)    ||
1275 		    dwarf_lineprologueend(line, &p_end))
1276 			return false;
1277 
1278 		/* highpc is exclusive. [entrypc,highpc) */
1279 		if (addr >= highpc)
1280 			break;
1281 
1282 		/* clang supports prologue-end marker */
1283 		if (p_end)
1284 			break;
1285 
1286 		/* Actual next line in source */
1287 		if (lno != entrypc_lno)
1288 			break;
1289 
1290 		/*
1291 		 * Single source line can have multiple line records.
1292 		 * For Example,
1293 		 *     void foo() { printf("hello\n"); }
1294 		 * contains two line records. One points to declaration and
1295 		 * other points to printf() line. Variable 'lno' won't get
1296 		 * incremented in this case but 'i' will.
1297 		 */
1298 		if (i != entrypc_idx)
1299 			break;
1300 	}
1301 
1302 	dwarf_lineaddr(line, postprologue_addr);
1303 	if (*postprologue_addr >= highpc)
1304 		dwarf_lineaddr(dwarf_onesrcline(lines, i - 1),
1305 			       postprologue_addr);
1306 
1307 	return true;
1308 }
1309 
1310 /*
1311  * die_skip_prologue - Use next address after prologue as probe location
1312  * @sp_die: a subprogram DIE
1313  * @cu_die: a CU DIE
1314  * @entrypc: entrypc of the function
1315  *
1316  * Function prologue prepares stack and registers before executing function
1317  * logic. When target program is compiled without optimization, function
1318  * parameter information is only valid after prologue. When we probe entrypc
1319  * of the function, and try to record function parameter, it contains
1320  * garbage value.
1321  */
die_skip_prologue(Dwarf_Die * sp_die,Dwarf_Die * cu_die,Dwarf_Addr * entrypc)1322 void die_skip_prologue(Dwarf_Die *sp_die, Dwarf_Die *cu_die,
1323 		       Dwarf_Addr *entrypc)
1324 {
1325 	size_t nr_lines = 0;
1326 	unsigned long entrypc_idx = 0;
1327 	Dwarf_Lines *lines = NULL;
1328 	Dwarf_Addr postprologue_addr;
1329 	Dwarf_Addr highpc;
1330 
1331 	if (dwarf_highpc(sp_die, &highpc))
1332 		return;
1333 
1334 	if (dwarf_getsrclines(cu_die, &lines, &nr_lines))
1335 		return;
1336 
1337 	if (!die_search_idx(lines, nr_lines, *entrypc, &entrypc_idx))
1338 		return;
1339 
1340 	if (!die_get_postprologue_addr(entrypc_idx, lines, nr_lines,
1341 				       highpc, &postprologue_addr))
1342 		return;
1343 
1344 	*entrypc = postprologue_addr;
1345 }
1346