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