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