2 * dwarf-aux.c : libdw auxiliary interfaces
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.
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.
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.
23 #include "dwarf-aux.h"
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
30 * Find the real(long) path of @fname in @cu_die.
32 const char *cu_find_realpath(Dwarf_Die
*cu_die
, const char *fname
)
36 const char *src
= NULL
;
42 ret
= dwarf_getsrcfiles(cu_die
, &files
, &nfiles
);
46 for (i
= 0; i
< nfiles
; i
++) {
47 src
= dwarf_filesrc(files
, i
, NULL
, NULL
);
48 if (strtailcmp(src
, fname
) == 0)
57 * cu_get_comp_dir - Get the path of compilation directory
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.
64 const char *cu_get_comp_dir(Dwarf_Die
*cu_die
)
67 if (dwarf_attr(cu_die
, DW_AT_comp_dir
, &attr
) == NULL
)
69 return dwarf_formstring(&attr
);
73 * cu_find_lineinfo - Get a line number and file name for given address
76 * @fname: a pointer which returns the file name string
77 * @lineno: a pointer which returns the line number
79 * Find a line number and file name for @addr in @cu_die.
81 int cu_find_lineinfo(Dwarf_Die
*cu_die
, unsigned long addr
,
82 const char **fname
, int *lineno
)
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
);
92 /* line number is useless without filename */
96 return *lineno
?: -ENOENT
;
100 * die_compare_name - Compare diename and tname
102 * @tname: a string of target name
104 * Compare the name of @dw_die and @tname. Return false if @dw_die has no name.
106 bool die_compare_name(Dwarf_Die
*dw_die
, const char *tname
)
109 name
= dwarf_diename(dw_die
);
110 return name
? (strcmp(tname
, name
) == 0) : false;
114 * die_get_call_lineno - Get callsite line number of inline-function instance
115 * @in_die: a DIE of an inlined function instance
117 * Get call-site line number of @in_die. This means from where the inline
118 * function is called.
120 int die_get_call_lineno(Dwarf_Die
*in_die
)
122 Dwarf_Attribute attr
;
125 if (!dwarf_attr(in_die
, DW_AT_call_line
, &attr
))
128 dwarf_formudata(&attr
, &ret
);
133 * die_get_type - Get type DIE
134 * @vr_die: a DIE of a variable
135 * @die_mem: where to store a type DIE
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.
140 Dwarf_Die
*die_get_type(Dwarf_Die
*vr_die
, Dwarf_Die
*die_mem
)
142 Dwarf_Attribute attr
;
144 if (dwarf_attr_integrate(vr_die
, DW_AT_type
, &attr
) &&
145 dwarf_formref_die(&attr
, die_mem
))
151 /* Get a type die, but skip qualifiers */
152 static Dwarf_Die
*__die_get_real_type(Dwarf_Die
*vr_die
, Dwarf_Die
*die_mem
)
157 vr_die
= die_get_type(vr_die
, die_mem
);
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
);
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
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).
179 Dwarf_Die
*die_get_real_type(Dwarf_Die
*vr_die
, Dwarf_Die
*die_mem
)
182 vr_die
= __die_get_real_type(vr_die
, die_mem
);
183 } while (vr_die
&& dwarf_tag(vr_die
) == DW_TAG_typedef
);
188 /* Get attribute and translate it as a udata */
189 static int die_get_attr_udata(Dwarf_Die
*tp_die
, unsigned int attr_name
,
192 Dwarf_Attribute attr
;
194 if (dwarf_attr(tp_die
, attr_name
, &attr
) == NULL
||
195 dwarf_formudata(&attr
, result
) != 0)
202 * die_is_signed_type - Check whether a type DIE is signed or not
203 * @tp_die: a DIE of a type
205 * Get the encoding of @tp_die and return true if the encoding
208 bool die_is_signed_type(Dwarf_Die
*tp_die
)
212 if (die_get_attr_udata(tp_die
, DW_AT_encoding
, &ret
))
215 return (ret
== DW_ATE_signed_char
|| ret
== DW_ATE_signed
||
216 ret
== DW_ATE_signed_fixed
);
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
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.
227 int die_get_data_member_location(Dwarf_Die
*mb_die
, Dwarf_Word
*offs
)
229 Dwarf_Attribute attr
;
234 if (dwarf_attr(mb_die
, DW_AT_data_member_location
, &attr
) == NULL
)
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)
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
);
248 *offs
= (Dwarf_Word
)expr
[0].number
;
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
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.
268 Dwarf_Die
*die_find_child(Dwarf_Die
*rt_die
,
269 int (*callback
)(Dwarf_Die
*, void *),
270 void *data
, Dwarf_Die
*die_mem
)
275 ret
= dwarf_child(rt_die
, die_mem
);
280 ret
= callback(die_mem
, data
);
281 if (ret
== DIE_FIND_CB_END
)
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
));
289 } while ((ret
& DIE_FIND_CB_SIBLING
) &&
290 dwarf_siblingof(die_mem
, die_mem
) == 0);
295 struct __addr_die_search_param
{
300 /* die_find callback for non-inlined function search */
301 static int __die_search_func_cb(Dwarf_Die
*fn_die
, void *data
)
303 struct __addr_die_search_param
*ad
= data
;
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
;
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
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.
322 Dwarf_Die
*die_find_realfunc(Dwarf_Die
*cu_die
, Dwarf_Addr addr
,
325 struct __addr_die_search_param ad
;
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))
335 /* die_find callback for inline function search */
336 static int __die_find_inline_cb(Dwarf_Die
*die_mem
, void *data
)
338 Dwarf_Addr
*addr
= data
;
340 if (dwarf_tag(die_mem
) == DW_TAG_inlined_subroutine
&&
341 dwarf_haspc(die_mem
, *addr
))
342 return DIE_FIND_CB_END
;
344 return DIE_FIND_CB_CONTINUE
;
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
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.
358 Dwarf_Die
*die_find_inlinefunc(Dwarf_Die
*sp_die
, Dwarf_Addr addr
,
363 sp_die
= die_find_child(sp_die
, __die_find_inline_cb
, &addr
, &tmp_die
);
367 /* Inlined function could be recursive. Trace it until fail */
369 memcpy(die_mem
, sp_die
, sizeof(Dwarf_Die
));
370 sp_die
= die_find_child(sp_die
, __die_find_inline_cb
, &addr
,
377 /* Line walker internal parameters */
378 struct __line_walk_param
{
380 line_walk_callback_t callback
;
385 static int __die_walk_funclines_cb(Dwarf_Die
*in_die
, void *data
)
387 struct __line_walk_param
*lw
= data
;
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
,
397 return DIE_FIND_CB_END
;
400 return DIE_FIND_CB_SIBLING
;
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
)
407 struct __line_walk_param lw
= {
408 .callback
= callback
,
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
);
424 die_find_child(sp_die
, __die_walk_funclines_cb
, &lw
, &die_mem
);
429 static int __die_walk_culines_cb(Dwarf_Die
*sp_die
, void *data
)
431 struct __line_walk_param
*lw
= data
;
433 lw
->retval
= __die_walk_funclines(sp_die
, lw
->callback
, lw
->data
);
435 return DWARF_CB_ABORT
;
441 * die_walk_lines - Walk on lines inside given DIE
442 * @rt_die: a root DIE (CU or subprogram)
443 * @callback: callback routine
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.
452 int die_walk_lines(Dwarf_Die
*rt_die
, line_walk_callback_t callback
, void *data
)
459 Dwarf_Die die_mem
, *cu_die
;
463 if (dwarf_tag(rt_die
) == DW_TAG_subprogram
)
464 cu_die
= dwarf_diecu(rt_die
, &die_mem
, NULL
, NULL
);
468 pr_debug2("Failed to get CU from subprogram\n");
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");
477 pr_debug2("Get %zd lines from this CU\n", nlines
);
479 /* Walk on the lines on lines list */
480 for (i
= 0; i
< nlines
; i
++) {
481 line
= dwarf_onesrcline(lines
, i
);
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");
489 /* Filter lines based on address */
490 if (rt_die
!= cu_die
)
493 * The line is included in given function, and
494 * no inline block includes it.
496 if (!dwarf_haspc(rt_die
, addr
) ||
497 die_find_inlinefunc(rt_die
, addr
, &die_mem
))
499 /* Get source line */
500 fname
= dwarf_linesrc(line
, NULL
, NULL
);
502 ret
= callback(fname
, lineno
, addr
, data
);
508 * Dwarf lines doesn't include function declarations and inlined
509 * subroutines. We have to check functions list or given function.
511 if (rt_die
!= cu_die
)
512 ret
= __die_walk_funclines(rt_die
, callback
, data
);
514 struct __line_walk_param param
= {
515 .callback
= callback
,
519 dwarf_getfuncs(cu_die
, __die_walk_culines_cb
, ¶m
, 0);
526 struct __find_variable_param
{
531 static int __die_find_variable_cb(Dwarf_Die
*die_mem
, void *data
)
533 struct __find_variable_param
*fvp
= data
;
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
;
542 if (dwarf_haspc(die_mem
, fvp
->addr
))
543 return DIE_FIND_CB_CONTINUE
;
545 return DIE_FIND_CB_SIBLING
;
549 * die_find_variable_at - Find a given name variable at given address
550 * @sp_die: a function DIE
551 * @name: variable name
553 * @die_mem: a buffer for result DIE
555 * Find a variable DIE called @name at @addr in @sp_die.
557 Dwarf_Die
*die_find_variable_at(Dwarf_Die
*sp_die
, const char *name
,
558 Dwarf_Addr addr
, Dwarf_Die
*die_mem
)
560 struct __find_variable_param fvp
= { .name
= name
, .addr
= addr
};
562 return die_find_child(sp_die
, __die_find_variable_cb
, (void *)&fvp
,
566 static int __die_find_member_cb(Dwarf_Die
*die_mem
, void *data
)
568 const char *name
= data
;
570 if ((dwarf_tag(die_mem
) == DW_TAG_member
) &&
571 die_compare_name(die_mem
, name
))
572 return DIE_FIND_CB_END
;
574 return DIE_FIND_CB_SIBLING
;
578 * die_find_member - Find a given name member in a data structure
579 * @st_die: a data structure type DIE
581 * @die_mem: a buffer for result DIE
583 * Find a member DIE called @name in @st_die.
585 Dwarf_Die
*die_find_member(Dwarf_Die
*st_die
, const char *name
,
588 return die_find_child(st_die
, __die_find_member_cb
, (void *)name
,
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
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.
604 int die_get_typename(Dwarf_Die
*vr_die
, char *buf
, int len
)
608 const char *tmp
= "";
610 if (__die_get_real_type(vr_die
, &type
) == NULL
)
613 tag
= dwarf_tag(&type
);
614 if (tag
== DW_TAG_array_type
|| tag
== DW_TAG_pointer_type
)
616 else if (tag
== DW_TAG_subroutine_type
) {
617 /* Function pointer */
618 ret
= snprintf(buf
, len
, "(function_type)");
619 return (ret
>= len
) ? -E2BIG
: ret
;
621 if (!dwarf_diename(&type
))
623 if (tag
== DW_TAG_union_type
)
625 else if (tag
== DW_TAG_structure_type
)
627 /* Write a base name */
628 ret
= snprintf(buf
, len
, "%s%s", tmp
, dwarf_diename(&type
));
629 return (ret
>= len
) ? -E2BIG
: ret
;
631 ret
= die_get_typename(&type
, buf
, len
);
633 ret2
= snprintf(buf
+ ret
, len
- ret
, "%s", tmp
);
634 ret
= (ret2
>= len
- ret
) ? -E2BIG
: ret2
+ ret
;
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
645 * Get the name and type of @vr_die and stores it in @buf as "type\tname".
647 int die_get_varname(Dwarf_Die
*vr_die
, char *buf
, int len
)
651 ret
= die_get_typename(vr_die
, buf
, len
);
653 pr_debug("Failed to get type, make it unknown.\n");
654 ret
= snprintf(buf
, len
, "(unknown_type)");
657 ret2
= snprintf(buf
+ ret
, len
- ret
, "\t%s",
658 dwarf_diename(vr_die
));
659 ret
= (ret2
>= len
- ret
) ? -E2BIG
: ret2
+ ret
;