2 * probe-finder.c : C expression to kprobe event converter
4 * Written by Masami Hiramatsu <mhiramat@redhat.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #include <sys/utsname.h>
23 #include <sys/types.h>
34 #include <dwarf-regs.h>
36 #include <linux/bitops.h>
41 #include "probe-finder.h"
43 /* Kprobe tracer basic type is up to u64 */
44 #define MAX_BASIC_TYPE_BITS 64
47 * Compare the tail of two strings.
48 * Return 0 if whole of either string is same as another's tail part.
50 static int strtailcmp(const char *s1
, const char *s2
)
54 while (--i1
>= 0 && --i2
>= 0) {
56 return s1
[i1
] - s2
[i2
];
61 /* Line number list operations */
63 /* Add a line to line number list */
64 static int line_list__add_line(struct list_head
*head
, int line
)
69 /* Reverse search, because new line will be the last one */
70 list_for_each_entry_reverse(ln
, head
, list
) {
71 if (ln
->line
< line
) {
74 } else if (ln
->line
== line
) /* Already exist */
77 /* List is empty, or the smallest entry */
80 pr_debug("line list: add a line %u\n", line
);
81 ln
= zalloc(sizeof(struct line_node
));
85 INIT_LIST_HEAD(&ln
->list
);
86 list_add(&ln
->list
, p
);
90 /* Check if the line in line number list */
91 static int line_list__has_line(struct list_head
*head
, int line
)
95 /* Reverse search, because new line will be the last one */
96 list_for_each_entry(ln
, head
, list
)
103 /* Init line number list */
104 static void line_list__init(struct list_head
*head
)
106 INIT_LIST_HEAD(head
);
109 /* Free line number list */
110 static void line_list__free(struct list_head
*head
)
112 struct line_node
*ln
;
113 while (!list_empty(head
)) {
114 ln
= list_first_entry(head
, struct line_node
, list
);
120 /* Dwarf FL wrappers */
121 static char *debuginfo_path
; /* Currently dummy */
123 static const Dwfl_Callbacks offline_callbacks
= {
124 .find_debuginfo
= dwfl_standard_find_debuginfo
,
125 .debuginfo_path
= &debuginfo_path
,
127 .section_address
= dwfl_offline_section_address
,
129 /* We use this table for core files too. */
130 .find_elf
= dwfl_build_id_find_elf
,
133 /* Get a Dwarf from offline image */
134 static Dwarf
*dwfl_init_offline_dwarf(int fd
, Dwfl
**dwflp
, Dwarf_Addr
*bias
)
142 *dwflp
= dwfl_begin(&offline_callbacks
);
146 mod
= dwfl_report_offline(*dwflp
, "", "", fd
);
150 dbg
= dwfl_module_getdwarf(mod
, bias
);
159 #if _ELFUTILS_PREREQ(0, 148)
160 /* This method is buggy if elfutils is older than 0.148 */
161 static int __linux_kernel_find_elf(Dwfl_Module
*mod
,
163 const char *module_name
,
165 char **file_name
, Elf
**elfp
)
168 const char *path
= kernel_get_module_path(module_name
);
170 pr_debug2("Use file %s for %s\n", path
, module_name
);
172 fd
= open(path
, O_RDONLY
);
174 *file_name
= strdup(path
);
178 /* If failed, try to call standard method */
179 return dwfl_linux_kernel_find_elf(mod
, userdata
, module_name
, base
,
183 static const Dwfl_Callbacks kernel_callbacks
= {
184 .find_debuginfo
= dwfl_standard_find_debuginfo
,
185 .debuginfo_path
= &debuginfo_path
,
187 .find_elf
= __linux_kernel_find_elf
,
188 .section_address
= dwfl_linux_kernel_module_section_address
,
191 /* Get a Dwarf from live kernel image */
192 static Dwarf
*dwfl_init_live_kernel_dwarf(Dwarf_Addr addr
, Dwfl
**dwflp
,
200 *dwflp
= dwfl_begin(&kernel_callbacks
);
204 /* Load the kernel dwarves: Don't care the result here */
205 dwfl_linux_kernel_report_kernel(*dwflp
);
206 dwfl_linux_kernel_report_modules(*dwflp
);
208 dbg
= dwfl_addrdwarf(*dwflp
, addr
, bias
);
209 /* Here, check whether we could get a real dwarf */
211 pr_debug("Failed to find kernel dwarf at %lx\n",
212 (unsigned long)addr
);
219 /* With older elfutils, this just support kernel module... */
220 static Dwarf
*dwfl_init_live_kernel_dwarf(Dwarf_Addr addr __used
, Dwfl
**dwflp
,
224 const char *path
= kernel_get_module_path("kernel");
227 pr_err("Failed to find vmlinux path\n");
231 pr_debug2("Use file %s for debuginfo\n", path
);
232 fd
= open(path
, O_RDONLY
);
236 return dwfl_init_offline_dwarf(fd
, dwflp
, bias
);
242 /* Find the realpath of the target file. */
243 static const char *cu_find_realpath(Dwarf_Die
*cu_die
, const char *fname
)
247 const char *src
= NULL
;
253 ret
= dwarf_getsrcfiles(cu_die
, &files
, &nfiles
);
257 for (i
= 0; i
< nfiles
; i
++) {
258 src
= dwarf_filesrc(files
, i
, NULL
, NULL
);
259 if (strtailcmp(src
, fname
) == 0)
267 /* Get DW_AT_comp_dir (should be NULL with older gcc) */
268 static const char *cu_get_comp_dir(Dwarf_Die
*cu_die
)
270 Dwarf_Attribute attr
;
271 if (dwarf_attr(cu_die
, DW_AT_comp_dir
, &attr
) == NULL
)
273 return dwarf_formstring(&attr
);
276 /* Get a line number and file name for given address */
277 static int cu_find_lineinfo(Dwarf_Die
*cudie
, unsigned long addr
,
278 const char **fname
, int *lineno
)
283 line
= dwarf_getsrc_die(cudie
, (Dwarf_Addr
)addr
);
284 if (line
&& dwarf_lineaddr(line
, &laddr
) == 0 &&
285 addr
== (unsigned long)laddr
&& dwarf_lineno(line
, lineno
) == 0) {
286 *fname
= dwarf_linesrc(line
, NULL
, NULL
);
288 /* line number is useless without filename */
292 return *lineno
?: -ENOENT
;
295 /* Compare diename and tname */
296 static bool die_compare_name(Dwarf_Die
*dw_die
, const char *tname
)
299 name
= dwarf_diename(dw_die
);
300 return name
? (strcmp(tname
, name
) == 0) : false;
303 /* Get callsite line number of inline-function instance */
304 static int die_get_call_lineno(Dwarf_Die
*in_die
)
306 Dwarf_Attribute attr
;
309 if (!dwarf_attr(in_die
, DW_AT_call_line
, &attr
))
312 dwarf_formudata(&attr
, &ret
);
317 static Dwarf_Die
*die_get_type(Dwarf_Die
*vr_die
, Dwarf_Die
*die_mem
)
319 Dwarf_Attribute attr
;
321 if (dwarf_attr_integrate(vr_die
, DW_AT_type
, &attr
) &&
322 dwarf_formref_die(&attr
, die_mem
))
328 /* Get a type die, but skip qualifiers */
329 static Dwarf_Die
*__die_get_real_type(Dwarf_Die
*vr_die
, Dwarf_Die
*die_mem
)
334 vr_die
= die_get_type(vr_die
, die_mem
);
337 tag
= dwarf_tag(vr_die
);
338 } while (tag
== DW_TAG_const_type
||
339 tag
== DW_TAG_restrict_type
||
340 tag
== DW_TAG_volatile_type
||
341 tag
== DW_TAG_shared_type
);
346 /* Get a type die, but skip qualifiers and typedef */
347 static Dwarf_Die
*die_get_real_type(Dwarf_Die
*vr_die
, Dwarf_Die
*die_mem
)
350 vr_die
= __die_get_real_type(vr_die
, die_mem
);
351 } while (vr_die
&& dwarf_tag(vr_die
) == DW_TAG_typedef
);
356 static int die_get_attr_udata(Dwarf_Die
*tp_die
, unsigned int attr_name
,
359 Dwarf_Attribute attr
;
361 if (dwarf_attr(tp_die
, attr_name
, &attr
) == NULL
||
362 dwarf_formudata(&attr
, result
) != 0)
368 static bool die_is_signed_type(Dwarf_Die
*tp_die
)
372 if (die_get_attr_udata(tp_die
, DW_AT_encoding
, &ret
))
375 return (ret
== DW_ATE_signed_char
|| ret
== DW_ATE_signed
||
376 ret
== DW_ATE_signed_fixed
);
379 static int die_get_byte_size(Dwarf_Die
*tp_die
)
383 if (die_get_attr_udata(tp_die
, DW_AT_byte_size
, &ret
))
389 static int die_get_bit_size(Dwarf_Die
*tp_die
)
393 if (die_get_attr_udata(tp_die
, DW_AT_bit_size
, &ret
))
399 static int die_get_bit_offset(Dwarf_Die
*tp_die
)
403 if (die_get_attr_udata(tp_die
, DW_AT_bit_offset
, &ret
))
409 /* Get data_member_location offset */
410 static int die_get_data_member_location(Dwarf_Die
*mb_die
, Dwarf_Word
*offs
)
412 Dwarf_Attribute attr
;
417 if (dwarf_attr(mb_die
, DW_AT_data_member_location
, &attr
) == NULL
)
420 if (dwarf_formudata(&attr
, offs
) != 0) {
421 /* DW_AT_data_member_location should be DW_OP_plus_uconst */
422 ret
= dwarf_getlocation(&attr
, &expr
, &nexpr
);
423 if (ret
< 0 || nexpr
== 0)
426 if (expr
[0].atom
!= DW_OP_plus_uconst
|| nexpr
!= 1) {
427 pr_debug("Unable to get offset:Unexpected OP %x (%zd)\n",
428 expr
[0].atom
, nexpr
);
431 *offs
= (Dwarf_Word
)expr
[0].number
;
436 /* Return values for die_find callbacks */
438 DIE_FIND_CB_FOUND
= 0, /* End of Search */
439 DIE_FIND_CB_CHILD
= 1, /* Search only children */
440 DIE_FIND_CB_SIBLING
= 2, /* Search only siblings */
441 DIE_FIND_CB_CONTINUE
= 3, /* Search children and siblings */
444 /* Search a child die */
445 static Dwarf_Die
*die_find_child(Dwarf_Die
*rt_die
,
446 int (*callback
)(Dwarf_Die
*, void *),
447 void *data
, Dwarf_Die
*die_mem
)
452 ret
= dwarf_child(rt_die
, die_mem
);
457 ret
= callback(die_mem
, data
);
458 if (ret
== DIE_FIND_CB_FOUND
)
461 if ((ret
& DIE_FIND_CB_CHILD
) &&
462 die_find_child(die_mem
, callback
, data
, &child_die
)) {
463 memcpy(die_mem
, &child_die
, sizeof(Dwarf_Die
));
466 } while ((ret
& DIE_FIND_CB_SIBLING
) &&
467 dwarf_siblingof(die_mem
, die_mem
) == 0);
472 struct __addr_die_search_param
{
477 static int __die_search_func_cb(Dwarf_Die
*fn_die
, void *data
)
479 struct __addr_die_search_param
*ad
= data
;
481 if (dwarf_tag(fn_die
) == DW_TAG_subprogram
&&
482 dwarf_haspc(fn_die
, ad
->addr
)) {
483 memcpy(ad
->die_mem
, fn_die
, sizeof(Dwarf_Die
));
484 return DWARF_CB_ABORT
;
489 /* Search a real subprogram including this line, */
490 static Dwarf_Die
*die_find_real_subprogram(Dwarf_Die
*cu_die
, Dwarf_Addr addr
,
493 struct __addr_die_search_param ad
;
495 ad
.die_mem
= die_mem
;
496 /* dwarf_getscopes can't find subprogram. */
497 if (!dwarf_getfuncs(cu_die
, __die_search_func_cb
, &ad
, 0))
503 /* die_find callback for inline function search */
504 static int __die_find_inline_cb(Dwarf_Die
*die_mem
, void *data
)
506 Dwarf_Addr
*addr
= data
;
508 if (dwarf_tag(die_mem
) == DW_TAG_inlined_subroutine
&&
509 dwarf_haspc(die_mem
, *addr
))
510 return DIE_FIND_CB_FOUND
;
512 return DIE_FIND_CB_CONTINUE
;
515 /* Similar to dwarf_getfuncs, but returns inlined_subroutine if exists. */
516 static Dwarf_Die
*die_find_inlinefunc(Dwarf_Die
*sp_die
, Dwarf_Addr addr
,
521 sp_die
= die_find_child(sp_die
, __die_find_inline_cb
, &addr
, &tmp_die
);
525 /* Inlined function could be recursive. Trace it until fail */
527 memcpy(die_mem
, sp_die
, sizeof(Dwarf_Die
));
528 sp_die
= die_find_child(sp_die
, __die_find_inline_cb
, &addr
,
535 /* Walker on lines (Note: line number will not be sorted) */
536 typedef int (* line_walk_handler_t
) (const char *fname
, int lineno
,
537 Dwarf_Addr addr
, void *data
);
539 struct __line_walk_param
{
541 line_walk_handler_t handler
;
546 static int __die_walk_funclines_cb(Dwarf_Die
*in_die
, void *data
)
548 struct __line_walk_param
*lw
= data
;
552 if (dwarf_tag(in_die
) == DW_TAG_inlined_subroutine
) {
553 lineno
= die_get_call_lineno(in_die
);
554 if (lineno
> 0 && dwarf_entrypc(in_die
, &addr
) == 0) {
555 lw
->retval
= lw
->handler(lw
->fname
, lineno
, addr
,
558 return DIE_FIND_CB_FOUND
;
561 return DIE_FIND_CB_SIBLING
;
564 /* Walk on lines of blocks included in given DIE */
565 static int __die_walk_funclines(Dwarf_Die
*sp_die
,
566 line_walk_handler_t handler
, void *data
)
568 struct __line_walk_param lw
= {
577 /* Handle function declaration line */
578 lw
.fname
= dwarf_decl_file(sp_die
);
579 if (lw
.fname
&& dwarf_decl_line(sp_die
, &lineno
) == 0 &&
580 dwarf_entrypc(sp_die
, &addr
) == 0) {
581 lw
.retval
= handler(lw
.fname
, lineno
, addr
, data
);
585 die_find_child(sp_die
, __die_walk_funclines_cb
, &lw
, &die_mem
);
590 static int __die_walk_culines_cb(Dwarf_Die
*sp_die
, void *data
)
592 struct __line_walk_param
*lw
= data
;
594 lw
->retval
= __die_walk_funclines(sp_die
, lw
->handler
, lw
->data
);
596 return DWARF_CB_ABORT
;
602 * Walk on lines inside given PDIE. If the PDIE is subprogram, walk only on
603 * the lines inside the subprogram, otherwise PDIE must be a CU DIE.
605 static int die_walk_lines(Dwarf_Die
*pdie
, line_walk_handler_t handler
,
613 Dwarf_Die die_mem
, *cu_die
;
617 if (dwarf_tag(pdie
) == DW_TAG_subprogram
)
618 cu_die
= dwarf_diecu(pdie
, &die_mem
, NULL
, NULL
);
622 pr_debug2("Failed to get CU from subprogram\n");
626 /* Get lines list in the CU */
627 if (dwarf_getsrclines(cu_die
, &lines
, &nlines
) != 0) {
628 pr_debug2("Failed to get source lines on this CU.\n");
631 pr_debug2("Get %zd lines from this CU\n", nlines
);
633 /* Walk on the lines on lines list */
634 for (i
= 0; i
< nlines
; i
++) {
635 line
= dwarf_onesrcline(lines
, i
);
637 dwarf_lineno(line
, &lineno
) != 0 ||
638 dwarf_lineaddr(line
, &addr
) != 0) {
639 pr_debug2("Failed to get line info. "
640 "Possible error in debuginfo.\n");
643 /* Filter lines based on address */
647 * The line is included in given function, and
648 * no inline block includes it.
650 if (!dwarf_haspc(pdie
, addr
) ||
651 die_find_inlinefunc(pdie
, addr
, &die_mem
))
653 /* Get source line */
654 fname
= dwarf_linesrc(line
, NULL
, NULL
);
656 ret
= handler(fname
, lineno
, addr
, data
);
662 * Dwarf lines doesn't include function declarations and inlined
663 * subroutines. We have to check functions list or given function.
666 ret
= __die_walk_funclines(pdie
, handler
, data
);
668 struct __line_walk_param param
= {
673 dwarf_getfuncs(cu_die
, __die_walk_culines_cb
, ¶m
, 0);
680 struct __find_variable_param
{
685 static int __die_find_variable_cb(Dwarf_Die
*die_mem
, void *data
)
687 struct __find_variable_param
*fvp
= data
;
690 tag
= dwarf_tag(die_mem
);
691 if ((tag
== DW_TAG_formal_parameter
||
692 tag
== DW_TAG_variable
) &&
693 die_compare_name(die_mem
, fvp
->name
))
694 return DIE_FIND_CB_FOUND
;
696 if (dwarf_haspc(die_mem
, fvp
->addr
))
697 return DIE_FIND_CB_CONTINUE
;
699 return DIE_FIND_CB_SIBLING
;
702 /* Find a variable called 'name' at given address */
703 static Dwarf_Die
*die_find_variable_at(Dwarf_Die
*sp_die
, const char *name
,
704 Dwarf_Addr addr
, Dwarf_Die
*die_mem
)
706 struct __find_variable_param fvp
= { .name
= name
, .addr
= addr
};
708 return die_find_child(sp_die
, __die_find_variable_cb
, (void *)&fvp
,
712 static int __die_find_member_cb(Dwarf_Die
*die_mem
, void *data
)
714 const char *name
= data
;
716 if ((dwarf_tag(die_mem
) == DW_TAG_member
) &&
717 die_compare_name(die_mem
, name
))
718 return DIE_FIND_CB_FOUND
;
720 return DIE_FIND_CB_SIBLING
;
723 /* Find a member called 'name' */
724 static Dwarf_Die
*die_find_member(Dwarf_Die
*st_die
, const char *name
,
727 return die_find_child(st_die
, __die_find_member_cb
, (void *)name
,
731 /* Get the name of given variable DIE */
732 static int die_get_typename(Dwarf_Die
*vr_die
, char *buf
, int len
)
736 const char *tmp
= "";
738 if (__die_get_real_type(vr_die
, &type
) == NULL
)
741 tag
= dwarf_tag(&type
);
742 if (tag
== DW_TAG_array_type
|| tag
== DW_TAG_pointer_type
)
744 else if (tag
== DW_TAG_subroutine_type
) {
745 /* Function pointer */
746 ret
= snprintf(buf
, len
, "(function_type)");
747 return (ret
>= len
) ? -E2BIG
: ret
;
749 if (!dwarf_diename(&type
))
751 if (tag
== DW_TAG_union_type
)
753 else if (tag
== DW_TAG_structure_type
)
755 /* Write a base name */
756 ret
= snprintf(buf
, len
, "%s%s", tmp
, dwarf_diename(&type
));
757 return (ret
>= len
) ? -E2BIG
: ret
;
759 ret
= die_get_typename(&type
, buf
, len
);
761 ret2
= snprintf(buf
+ ret
, len
- ret
, "%s", tmp
);
762 ret
= (ret2
>= len
- ret
) ? -E2BIG
: ret2
+ ret
;
767 /* Get the name and type of given variable DIE, stored as "type\tname" */
768 static int die_get_varname(Dwarf_Die
*vr_die
, char *buf
, int len
)
772 ret
= die_get_typename(vr_die
, buf
, len
);
774 pr_debug("Failed to get type, make it unknown.\n");
775 ret
= snprintf(buf
, len
, "(unknown_type)");
778 ret2
= snprintf(buf
+ ret
, len
- ret
, "\t%s",
779 dwarf_diename(vr_die
));
780 ret
= (ret2
>= len
- ret
) ? -E2BIG
: ret2
+ ret
;
786 * Probe finder related functions
789 static struct probe_trace_arg_ref
*alloc_trace_arg_ref(long offs
)
791 struct probe_trace_arg_ref
*ref
;
792 ref
= zalloc(sizeof(struct probe_trace_arg_ref
));
799 * Convert a location into trace_arg.
800 * If tvar == NULL, this just checks variable can be converted.
802 static int convert_variable_location(Dwarf_Die
*vr_die
, Dwarf_Addr addr
,
804 struct probe_trace_arg
*tvar
)
806 Dwarf_Attribute attr
;
815 if (dwarf_attr(vr_die
, DW_AT_external
, &attr
) != NULL
)
818 /* TODO: handle more than 1 exprs */
819 if (dwarf_attr(vr_die
, DW_AT_location
, &attr
) == NULL
||
820 dwarf_getlocation_addr(&attr
, addr
, &op
, &nops
, 1) <= 0 ||
822 /* TODO: Support const_value */
826 if (op
->atom
== DW_OP_addr
) {
830 /* Static variables on memory (not stack), make @varname */
831 ret
= strlen(dwarf_diename(vr_die
));
832 tvar
->value
= zalloc(ret
+ 2);
833 if (tvar
->value
== NULL
)
835 snprintf(tvar
->value
, ret
+ 2, "@%s", dwarf_diename(vr_die
));
836 tvar
->ref
= alloc_trace_arg_ref((long)offs
);
837 if (tvar
->ref
== NULL
)
842 /* If this is based on frame buffer, set the offset */
843 if (op
->atom
== DW_OP_fbreg
) {
851 if (op
->atom
>= DW_OP_breg0
&& op
->atom
<= DW_OP_breg31
) {
852 regn
= op
->atom
- DW_OP_breg0
;
855 } else if (op
->atom
>= DW_OP_reg0
&& op
->atom
<= DW_OP_reg31
) {
856 regn
= op
->atom
- DW_OP_reg0
;
857 } else if (op
->atom
== DW_OP_bregx
) {
861 } else if (op
->atom
== DW_OP_regx
) {
864 pr_debug("DW_OP %x is not supported.\n", op
->atom
);
871 regs
= get_arch_regstr(regn
);
873 /* This should be a bug in DWARF or this tool */
874 pr_warning("Mapping for the register number %u "
875 "missing on this architecture.\n", regn
);
879 tvar
->value
= strdup(regs
);
880 if (tvar
->value
== NULL
)
884 tvar
->ref
= alloc_trace_arg_ref((long)offs
);
885 if (tvar
->ref
== NULL
)
891 #define BYTES_TO_BITS(nb) ((nb) * BITS_PER_LONG / sizeof(long))
893 static int convert_variable_type(Dwarf_Die
*vr_die
,
894 struct probe_trace_arg
*tvar
,
897 struct probe_trace_arg_ref
**ref_ptr
= &tvar
->ref
;
902 /* TODO: check all types */
903 if (cast
&& strcmp(cast
, "string") != 0) {
904 /* Non string type is OK */
905 tvar
->type
= strdup(cast
);
906 return (tvar
->type
== NULL
) ? -ENOMEM
: 0;
909 if (die_get_bit_size(vr_die
) != 0) {
910 /* This is a bitfield */
911 ret
= snprintf(buf
, 16, "b%d@%d/%zd", die_get_bit_size(vr_die
),
912 die_get_bit_offset(vr_die
),
913 BYTES_TO_BITS(die_get_byte_size(vr_die
)));
917 if (die_get_real_type(vr_die
, &type
) == NULL
) {
918 pr_warning("Failed to get a type information of %s.\n",
919 dwarf_diename(vr_die
));
923 pr_debug("%s type is %s.\n",
924 dwarf_diename(vr_die
), dwarf_diename(&type
));
926 if (cast
&& strcmp(cast
, "string") == 0) { /* String type */
927 ret
= dwarf_tag(&type
);
928 if (ret
!= DW_TAG_pointer_type
&&
929 ret
!= DW_TAG_array_type
) {
930 pr_warning("Failed to cast into string: "
931 "%s(%s) is not a pointer nor array.\n",
932 dwarf_diename(vr_die
), dwarf_diename(&type
));
935 if (ret
== DW_TAG_pointer_type
) {
936 if (die_get_real_type(&type
, &type
) == NULL
) {
937 pr_warning("Failed to get a type"
942 ref_ptr
= &(*ref_ptr
)->next
;
943 /* Add new reference with offset +0 */
944 *ref_ptr
= zalloc(sizeof(struct probe_trace_arg_ref
));
945 if (*ref_ptr
== NULL
) {
946 pr_warning("Out of memory error\n");
950 if (!die_compare_name(&type
, "char") &&
951 !die_compare_name(&type
, "unsigned char")) {
952 pr_warning("Failed to cast into string: "
953 "%s is not (unsigned) char *.\n",
954 dwarf_diename(vr_die
));
957 tvar
->type
= strdup(cast
);
958 return (tvar
->type
== NULL
) ? -ENOMEM
: 0;
961 ret
= BYTES_TO_BITS(die_get_byte_size(&type
));
963 /* No size ... try to use default type */
966 /* Check the bitwidth */
967 if (ret
> MAX_BASIC_TYPE_BITS
) {
968 pr_info("%s exceeds max-bitwidth. Cut down to %d bits.\n",
969 dwarf_diename(&type
), MAX_BASIC_TYPE_BITS
);
970 ret
= MAX_BASIC_TYPE_BITS
;
972 ret
= snprintf(buf
, 16, "%c%d",
973 die_is_signed_type(&type
) ? 's' : 'u', ret
);
976 if (ret
< 0 || ret
>= 16) {
979 pr_warning("Failed to convert variable type: %s\n",
983 tvar
->type
= strdup(buf
);
984 if (tvar
->type
== NULL
)
989 static int convert_variable_fields(Dwarf_Die
*vr_die
, const char *varname
,
990 struct perf_probe_arg_field
*field
,
991 struct probe_trace_arg_ref
**ref_ptr
,
994 struct probe_trace_arg_ref
*ref
= *ref_ptr
;
999 pr_debug("converting %s in %s\n", field
->name
, varname
);
1000 if (die_get_real_type(vr_die
, &type
) == NULL
) {
1001 pr_warning("Failed to get the type of %s.\n", varname
);
1004 pr_debug2("Var real type: (%x)\n", (unsigned)dwarf_dieoffset(&type
));
1005 tag
= dwarf_tag(&type
);
1007 if (field
->name
[0] == '[' &&
1008 (tag
== DW_TAG_array_type
|| tag
== DW_TAG_pointer_type
)) {
1010 /* Save original type for next field */
1011 memcpy(die_mem
, &type
, sizeof(*die_mem
));
1012 /* Get the type of this array */
1013 if (die_get_real_type(&type
, &type
) == NULL
) {
1014 pr_warning("Failed to get the type of %s.\n", varname
);
1017 pr_debug2("Array real type: (%x)\n",
1018 (unsigned)dwarf_dieoffset(&type
));
1019 if (tag
== DW_TAG_pointer_type
) {
1020 ref
= zalloc(sizeof(struct probe_trace_arg_ref
));
1024 (*ref_ptr
)->next
= ref
;
1028 ref
->offset
+= die_get_byte_size(&type
) * field
->index
;
1030 /* Save vr_die for converting types */
1031 memcpy(die_mem
, vr_die
, sizeof(*die_mem
));
1033 } else if (tag
== DW_TAG_pointer_type
) {
1034 /* Check the pointer and dereference */
1036 pr_err("Semantic error: %s must be referred by '->'\n",
1040 /* Get the type pointed by this pointer */
1041 if (die_get_real_type(&type
, &type
) == NULL
) {
1042 pr_warning("Failed to get the type of %s.\n", varname
);
1045 /* Verify it is a data structure */
1046 if (dwarf_tag(&type
) != DW_TAG_structure_type
) {
1047 pr_warning("%s is not a data structure.\n", varname
);
1051 ref
= zalloc(sizeof(struct probe_trace_arg_ref
));
1055 (*ref_ptr
)->next
= ref
;
1059 /* Verify it is a data structure */
1060 if (tag
!= DW_TAG_structure_type
) {
1061 pr_warning("%s is not a data structure.\n", varname
);
1064 if (field
->name
[0] == '[') {
1065 pr_err("Semantic error: %s is not a pointor"
1066 " nor array.\n", varname
);
1070 pr_err("Semantic error: %s must be referred by '.'\n",
1075 pr_warning("Structure on a register is not "
1076 "supported yet.\n");
1081 if (die_find_member(&type
, field
->name
, die_mem
) == NULL
) {
1082 pr_warning("%s(tyep:%s) has no member %s.\n", varname
,
1083 dwarf_diename(&type
), field
->name
);
1087 /* Get the offset of the field */
1088 ret
= die_get_data_member_location(die_mem
, &offs
);
1090 pr_warning("Failed to get the offset of %s.\n", field
->name
);
1093 ref
->offset
+= (long)offs
;
1096 /* Converting next field */
1098 return convert_variable_fields(die_mem
, field
->name
,
1099 field
->next
, &ref
, die_mem
);
1104 /* Show a variables in kprobe event format */
1105 static int convert_variable(Dwarf_Die
*vr_die
, struct probe_finder
*pf
)
1110 pr_debug("Converting variable %s into trace event.\n",
1111 dwarf_diename(vr_die
));
1113 ret
= convert_variable_location(vr_die
, pf
->addr
, pf
->fb_ops
,
1116 pr_err("Failed to find the location of %s at this address.\n"
1117 " Perhaps, it has been optimized out.\n", pf
->pvar
->var
);
1118 else if (ret
== -ENOTSUP
)
1119 pr_err("Sorry, we don't support this variable location yet.\n");
1120 else if (pf
->pvar
->field
) {
1121 ret
= convert_variable_fields(vr_die
, pf
->pvar
->var
,
1122 pf
->pvar
->field
, &pf
->tvar
->ref
,
1127 ret
= convert_variable_type(vr_die
, pf
->tvar
, pf
->pvar
->type
);
1128 /* *expr will be cached in libdw. Don't free it. */
1132 /* Find a variable in a subprogram die */
1133 static int find_variable(Dwarf_Die
*sp_die
, struct probe_finder
*pf
)
1135 Dwarf_Die vr_die
, *scopes
;
1139 if (!is_c_varname(pf
->pvar
->var
)) {
1140 /* Copy raw parameters */
1141 pf
->tvar
->value
= strdup(pf
->pvar
->var
);
1142 if (pf
->tvar
->value
== NULL
)
1144 if (pf
->pvar
->type
) {
1145 pf
->tvar
->type
= strdup(pf
->pvar
->type
);
1146 if (pf
->tvar
->type
== NULL
)
1149 if (pf
->pvar
->name
) {
1150 pf
->tvar
->name
= strdup(pf
->pvar
->name
);
1151 if (pf
->tvar
->name
== NULL
)
1154 pf
->tvar
->name
= NULL
;
1159 pf
->tvar
->name
= strdup(pf
->pvar
->name
);
1161 ret
= synthesize_perf_probe_arg(pf
->pvar
, buf
, 32);
1164 ptr
= strchr(buf
, ':'); /* Change type separator to _ */
1167 pf
->tvar
->name
= strdup(buf
);
1169 if (pf
->tvar
->name
== NULL
)
1172 pr_debug("Searching '%s' variable in context.\n",
1174 /* Search child die for local variables and parameters. */
1175 if (die_find_variable_at(sp_die
, pf
->pvar
->var
, pf
->addr
, &vr_die
))
1176 ret
= convert_variable(&vr_die
, pf
);
1178 /* Search upper class */
1179 nscopes
= dwarf_getscopes_die(sp_die
, &scopes
);
1180 while (nscopes
-- > 1) {
1181 pr_debug("Searching variables in %s\n",
1182 dwarf_diename(&scopes
[nscopes
]));
1183 /* We should check this scope, so give dummy address */
1184 if (die_find_variable_at(&scopes
[nscopes
],
1187 ret
= convert_variable(&vr_die
, pf
);
1197 pr_warning("Failed to find '%s' in this function.\n",
1202 /* Convert subprogram DIE to trace point */
1203 static int convert_to_trace_point(Dwarf_Die
*sp_die
, Dwarf_Addr paddr
,
1204 bool retprobe
, struct probe_trace_point
*tp
)
1209 /* Copy the name of probe point */
1210 name
= dwarf_diename(sp_die
);
1212 if (dwarf_entrypc(sp_die
, &eaddr
) != 0) {
1213 pr_warning("Failed to get entry address of %s\n",
1214 dwarf_diename(sp_die
));
1217 tp
->symbol
= strdup(name
);
1218 if (tp
->symbol
== NULL
)
1220 tp
->offset
= (unsigned long)(paddr
- eaddr
);
1222 /* This function has no name. */
1223 tp
->offset
= (unsigned long)paddr
;
1225 /* Return probe must be on the head of a subprogram */
1227 if (eaddr
!= paddr
) {
1228 pr_warning("Return probe must be on the head of"
1229 " a real function.\n");
1232 tp
->retprobe
= true;
1238 /* Call probe_finder callback with real subprogram DIE */
1239 static int call_probe_finder(Dwarf_Die
*sp_die
, struct probe_finder
*pf
)
1242 Dwarf_Attribute fb_attr
;
1246 /* If no real subprogram, find a real one */
1247 if (!sp_die
|| dwarf_tag(sp_die
) != DW_TAG_subprogram
) {
1248 sp_die
= die_find_real_subprogram(&pf
->cu_die
,
1249 pf
->addr
, &die_mem
);
1251 pr_warning("Failed to find probe point in any "
1257 /* Get the frame base attribute/ops */
1258 dwarf_attr(sp_die
, DW_AT_frame_base
, &fb_attr
);
1259 ret
= dwarf_getlocation_addr(&fb_attr
, pf
->addr
, &pf
->fb_ops
, &nops
, 1);
1260 if (ret
<= 0 || nops
== 0) {
1262 #if _ELFUTILS_PREREQ(0, 142)
1263 } else if (nops
== 1 && pf
->fb_ops
[0].atom
== DW_OP_call_frame_cfa
&&
1266 if (dwarf_cfi_addrframe(pf
->cfi
, pf
->addr
, &frame
) != 0 ||
1267 dwarf_frame_cfa(frame
, &pf
->fb_ops
, &nops
) != 0) {
1268 pr_warning("Failed to get call frame on 0x%jx\n",
1269 (uintmax_t)pf
->addr
);
1275 /* Call finder's callback handler */
1276 ret
= pf
->callback(sp_die
, pf
);
1278 /* *pf->fb_ops will be cached in libdw. Don't free it. */
1284 static int probe_point_line_walker(const char *fname
, int lineno
,
1285 Dwarf_Addr addr
, void *data
)
1287 struct probe_finder
*pf
= data
;
1290 if (lineno
!= pf
->lno
|| strtailcmp(fname
, pf
->fname
) != 0)
1294 ret
= call_probe_finder(NULL
, pf
);
1296 /* Continue if no error, because the line will be in inline function */
1297 return ret
< 0 ? ret
: 0;
1300 /* Find probe point from its line number */
1301 static int find_probe_point_by_line(struct probe_finder
*pf
)
1303 return die_walk_lines(&pf
->cu_die
, probe_point_line_walker
, pf
);
1306 /* Find lines which match lazy pattern */
1307 static int find_lazy_match_lines(struct list_head
*head
,
1308 const char *fname
, const char *pat
)
1314 int count
= 0, linenum
= 1;
1316 fp
= fopen(fname
, "r");
1318 pr_warning("Failed to open %s: %s\n", fname
, strerror(errno
));
1322 while ((len
= getline(&line
, &line_len
, fp
)) > 0) {
1324 if (line
[len
- 1] == '\n')
1325 line
[len
- 1] = '\0';
1327 if (strlazymatch(line
, pat
)) {
1328 line_list__add_line(head
, linenum
);
1340 pr_debug("No matched lines found in %s.\n", fname
);
1344 static int probe_point_lazy_walker(const char *fname
, int lineno
,
1345 Dwarf_Addr addr
, void *data
)
1347 struct probe_finder
*pf
= data
;
1350 if (!line_list__has_line(&pf
->lcache
, lineno
) ||
1351 strtailcmp(fname
, pf
->fname
) != 0)
1354 pr_debug("Probe line found: line:%d addr:0x%llx\n",
1355 lineno
, (unsigned long long)addr
);
1357 ret
= call_probe_finder(NULL
, pf
);
1360 * Continue if no error, because the lazy pattern will match
1363 return ret
< 0 ? ret
: 0;
1366 /* Find probe points from lazy pattern */
1367 static int find_probe_point_lazy(Dwarf_Die
*sp_die
, struct probe_finder
*pf
)
1371 if (list_empty(&pf
->lcache
)) {
1372 /* Matching lazy line pattern */
1373 ret
= find_lazy_match_lines(&pf
->lcache
, pf
->fname
,
1374 pf
->pev
->point
.lazy_line
);
1379 return die_walk_lines(sp_die
, probe_point_lazy_walker
, pf
);
1382 /* Callback parameter with return value */
1383 struct dwarf_callback_param
{
1388 static int probe_point_inline_cb(Dwarf_Die
*in_die
, void *data
)
1390 struct dwarf_callback_param
*param
= data
;
1391 struct probe_finder
*pf
= param
->data
;
1392 struct perf_probe_point
*pp
= &pf
->pev
->point
;
1396 param
->retval
= find_probe_point_lazy(in_die
, pf
);
1398 /* Get probe address */
1399 if (dwarf_entrypc(in_die
, &addr
) != 0) {
1400 pr_warning("Failed to get entry address of %s.\n",
1401 dwarf_diename(in_die
));
1402 param
->retval
= -ENOENT
;
1403 return DWARF_CB_ABORT
;
1406 pf
->addr
+= pp
->offset
;
1407 pr_debug("found inline addr: 0x%jx\n",
1408 (uintmax_t)pf
->addr
);
1410 param
->retval
= call_probe_finder(in_die
, pf
);
1411 if (param
->retval
< 0)
1412 return DWARF_CB_ABORT
;
1418 /* Search function from function name */
1419 static int probe_point_search_cb(Dwarf_Die
*sp_die
, void *data
)
1421 struct dwarf_callback_param
*param
= data
;
1422 struct probe_finder
*pf
= param
->data
;
1423 struct perf_probe_point
*pp
= &pf
->pev
->point
;
1425 /* Check tag and diename */
1426 if (dwarf_tag(sp_die
) != DW_TAG_subprogram
||
1427 !die_compare_name(sp_die
, pp
->function
))
1430 /* Check declared file */
1431 if (pp
->file
&& strtailcmp(pp
->file
, dwarf_decl_file(sp_die
)))
1434 pf
->fname
= dwarf_decl_file(sp_die
);
1435 if (pp
->line
) { /* Function relative line */
1436 dwarf_decl_line(sp_die
, &pf
->lno
);
1437 pf
->lno
+= pp
->line
;
1438 param
->retval
= find_probe_point_by_line(pf
);
1439 } else if (!dwarf_func_inline(sp_die
)) {
1442 param
->retval
= find_probe_point_lazy(sp_die
, pf
);
1444 if (dwarf_entrypc(sp_die
, &pf
->addr
) != 0) {
1445 pr_warning("Failed to get entry address of "
1446 "%s.\n", dwarf_diename(sp_die
));
1447 param
->retval
= -ENOENT
;
1448 return DWARF_CB_ABORT
;
1450 pf
->addr
+= pp
->offset
;
1451 /* TODO: Check the address in this function */
1452 param
->retval
= call_probe_finder(sp_die
, pf
);
1455 struct dwarf_callback_param _param
= {.data
= (void *)pf
,
1457 /* Inlined function: search instances */
1458 dwarf_func_inline_instances(sp_die
, probe_point_inline_cb
,
1460 param
->retval
= _param
.retval
;
1463 return DWARF_CB_ABORT
; /* Exit; no same symbol in this CU. */
1466 static int find_probe_point_by_func(struct probe_finder
*pf
)
1468 struct dwarf_callback_param _param
= {.data
= (void *)pf
,
1470 dwarf_getfuncs(&pf
->cu_die
, probe_point_search_cb
, &_param
, 0);
1471 return _param
.retval
;
1474 /* Find probe points from debuginfo */
1475 static int find_probes(int fd
, struct probe_finder
*pf
)
1477 struct perf_probe_point
*pp
= &pf
->pev
->point
;
1478 Dwarf_Off off
, noff
;
1483 Dwarf_Addr bias
; /* Currently ignored */
1486 dbg
= dwfl_init_offline_dwarf(fd
, &dwfl
, &bias
);
1488 pr_warning("No debug information found in the vmlinux - "
1489 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
1490 close(fd
); /* Without dwfl_end(), fd isn't closed. */
1494 #if _ELFUTILS_PREREQ(0, 142)
1495 /* Get the call frame information from this dwarf */
1496 pf
->cfi
= dwarf_getcfi(dbg
);
1500 line_list__init(&pf
->lcache
);
1501 /* Loop on CUs (Compilation Unit) */
1502 while (!dwarf_nextcu(dbg
, off
, &noff
, &cuhl
, NULL
, NULL
, NULL
)) {
1503 /* Get the DIE(Debugging Information Entry) of this CU */
1504 diep
= dwarf_offdie(dbg
, off
+ cuhl
, &pf
->cu_die
);
1508 /* Check if target file is included. */
1510 pf
->fname
= cu_find_realpath(&pf
->cu_die
, pp
->file
);
1514 if (!pp
->file
|| pf
->fname
) {
1516 ret
= find_probe_point_by_func(pf
);
1517 else if (pp
->lazy_line
)
1518 ret
= find_probe_point_lazy(NULL
, pf
);
1521 ret
= find_probe_point_by_line(pf
);
1528 line_list__free(&pf
->lcache
);
1535 /* Add a found probe point into trace event list */
1536 static int add_probe_trace_event(Dwarf_Die
*sp_die
, struct probe_finder
*pf
)
1538 struct trace_event_finder
*tf
=
1539 container_of(pf
, struct trace_event_finder
, pf
);
1540 struct probe_trace_event
*tev
;
1543 /* Check number of tevs */
1544 if (tf
->ntevs
== tf
->max_tevs
) {
1545 pr_warning("Too many( > %d) probe point found.\n",
1549 tev
= &tf
->tevs
[tf
->ntevs
++];
1551 ret
= convert_to_trace_point(sp_die
, pf
->addr
, pf
->pev
->point
.retprobe
,
1556 pr_debug("Probe point found: %s+%lu\n", tev
->point
.symbol
,
1559 /* Find each argument */
1560 tev
->nargs
= pf
->pev
->nargs
;
1561 tev
->args
= zalloc(sizeof(struct probe_trace_arg
) * tev
->nargs
);
1562 if (tev
->args
== NULL
)
1564 for (i
= 0; i
< pf
->pev
->nargs
; i
++) {
1565 pf
->pvar
= &pf
->pev
->args
[i
];
1566 pf
->tvar
= &tev
->args
[i
];
1567 ret
= find_variable(sp_die
, pf
);
1575 /* Find probe_trace_events specified by perf_probe_event from debuginfo */
1576 int find_probe_trace_events(int fd
, struct perf_probe_event
*pev
,
1577 struct probe_trace_event
**tevs
, int max_tevs
)
1579 struct trace_event_finder tf
= {
1580 .pf
= {.pev
= pev
, .callback
= add_probe_trace_event
},
1581 .max_tevs
= max_tevs
};
1584 /* Allocate result tevs array */
1585 *tevs
= zalloc(sizeof(struct probe_trace_event
) * max_tevs
);
1592 ret
= find_probes(fd
, &tf
.pf
);
1599 return (ret
< 0) ? ret
: tf
.ntevs
;
1602 #define MAX_VAR_LEN 64
1604 /* Collect available variables in this scope */
1605 static int collect_variables_cb(Dwarf_Die
*die_mem
, void *data
)
1607 struct available_var_finder
*af
= data
;
1608 struct variable_list
*vl
;
1609 char buf
[MAX_VAR_LEN
];
1612 vl
= &af
->vls
[af
->nvls
- 1];
1614 tag
= dwarf_tag(die_mem
);
1615 if (tag
== DW_TAG_formal_parameter
||
1616 tag
== DW_TAG_variable
) {
1617 ret
= convert_variable_location(die_mem
, af
->pf
.addr
,
1618 af
->pf
.fb_ops
, NULL
);
1620 ret
= die_get_varname(die_mem
, buf
, MAX_VAR_LEN
);
1621 pr_debug2("Add new var: %s\n", buf
);
1623 strlist__add(vl
->vars
, buf
);
1627 if (af
->child
&& dwarf_haspc(die_mem
, af
->pf
.addr
))
1628 return DIE_FIND_CB_CONTINUE
;
1630 return DIE_FIND_CB_SIBLING
;
1633 /* Add a found vars into available variables list */
1634 static int add_available_vars(Dwarf_Die
*sp_die
, struct probe_finder
*pf
)
1636 struct available_var_finder
*af
=
1637 container_of(pf
, struct available_var_finder
, pf
);
1638 struct variable_list
*vl
;
1639 Dwarf_Die die_mem
, *scopes
= NULL
;
1642 /* Check number of tevs */
1643 if (af
->nvls
== af
->max_vls
) {
1644 pr_warning("Too many( > %d) probe point found.\n", af
->max_vls
);
1647 vl
= &af
->vls
[af
->nvls
++];
1649 ret
= convert_to_trace_point(sp_die
, pf
->addr
, pf
->pev
->point
.retprobe
,
1654 pr_debug("Probe point found: %s+%lu\n", vl
->point
.symbol
,
1657 /* Find local variables */
1658 vl
->vars
= strlist__new(true, NULL
);
1659 if (vl
->vars
== NULL
)
1662 die_find_child(sp_die
, collect_variables_cb
, (void *)af
, &die_mem
);
1664 /* Find external variables */
1667 /* Don't need to search child DIE for externs. */
1669 nscopes
= dwarf_getscopes_die(sp_die
, &scopes
);
1670 while (nscopes
-- > 1)
1671 die_find_child(&scopes
[nscopes
], collect_variables_cb
,
1672 (void *)af
, &die_mem
);
1677 if (strlist__empty(vl
->vars
)) {
1678 strlist__delete(vl
->vars
);
1685 /* Find available variables at given probe point */
1686 int find_available_vars_at(int fd
, struct perf_probe_event
*pev
,
1687 struct variable_list
**vls
, int max_vls
,
1690 struct available_var_finder af
= {
1691 .pf
= {.pev
= pev
, .callback
= add_available_vars
},
1692 .max_vls
= max_vls
, .externs
= externs
};
1695 /* Allocate result vls array */
1696 *vls
= zalloc(sizeof(struct variable_list
) * max_vls
);
1703 ret
= find_probes(fd
, &af
.pf
);
1705 /* Free vlist for error */
1707 if (af
.vls
[af
.nvls
].point
.symbol
)
1708 free(af
.vls
[af
.nvls
].point
.symbol
);
1709 if (af
.vls
[af
.nvls
].vars
)
1710 strlist__delete(af
.vls
[af
.nvls
].vars
);
1717 return (ret
< 0) ? ret
: af
.nvls
;
1720 /* Reverse search */
1721 int find_perf_probe_point(unsigned long addr
, struct perf_probe_point
*ppt
)
1723 Dwarf_Die cudie
, spdie
, indie
;
1726 Dwarf_Addr _addr
, baseaddr
, bias
= 0;
1727 const char *fname
= NULL
, *func
= NULL
, *tmp
;
1728 int baseline
= 0, lineno
= 0, ret
= 0;
1730 /* Open the live linux kernel */
1731 dbg
= dwfl_init_live_kernel_dwarf(addr
, &dwfl
, &bias
);
1733 pr_warning("No debug information found in the vmlinux - "
1734 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
1739 /* Adjust address with bias */
1742 if (!dwarf_addrdie(dbg
, (Dwarf_Addr
)addr
- bias
, &cudie
)) {
1743 pr_warning("Failed to find debug information for address %lx\n",
1749 /* Find a corresponding line (filename and lineno) */
1750 cu_find_lineinfo(&cudie
, addr
, &fname
, &lineno
);
1751 /* Don't care whether it failed or not */
1753 /* Find a corresponding function (name, baseline and baseaddr) */
1754 if (die_find_real_subprogram(&cudie
, (Dwarf_Addr
)addr
, &spdie
)) {
1755 /* Get function entry information */
1756 tmp
= dwarf_diename(&spdie
);
1758 dwarf_entrypc(&spdie
, &baseaddr
) != 0 ||
1759 dwarf_decl_line(&spdie
, &baseline
) != 0)
1763 if (addr
== (unsigned long)baseaddr
)
1764 /* Function entry - Relative line number is 0 */
1766 else if (die_find_inlinefunc(&spdie
, (Dwarf_Addr
)addr
,
1768 if (dwarf_entrypc(&indie
, &_addr
) == 0 &&
1771 * addr is at an inline function entry.
1772 * In this case, lineno should be the call-site
1775 lineno
= die_get_call_lineno(&indie
);
1778 * addr is in an inline function body.
1779 * Since lineno points one of the lines
1780 * of the inline function, baseline should
1781 * be the entry line of the inline function.
1783 tmp
= dwarf_diename(&indie
);
1785 dwarf_decl_line(&spdie
, &baseline
) == 0)
1792 /* Make a relative line number or an offset */
1794 ppt
->line
= lineno
- baseline
;
1796 ppt
->offset
= addr
- (unsigned long)baseaddr
;
1798 /* Duplicate strings */
1800 ppt
->function
= strdup(func
);
1801 if (ppt
->function
== NULL
) {
1807 ppt
->file
= strdup(fname
);
1808 if (ppt
->file
== NULL
) {
1809 if (ppt
->function
) {
1810 free(ppt
->function
);
1811 ppt
->function
= NULL
;
1820 if (ret
== 0 && (fname
|| func
))
1821 ret
= 1; /* Found a point */
1825 /* Add a line and store the src path */
1826 static int line_range_add_line(const char *src
, unsigned int lineno
,
1827 struct line_range
*lr
)
1829 /* Copy source path */
1831 lr
->path
= strdup(src
);
1832 if (lr
->path
== NULL
)
1835 return line_list__add_line(&lr
->line_list
, lineno
);
1838 static int line_range_walk_cb(const char *fname
, int lineno
,
1839 Dwarf_Addr addr __used
,
1842 struct line_finder
*lf
= data
;
1844 if ((strtailcmp(fname
, lf
->fname
) != 0) ||
1845 (lf
->lno_s
> lineno
|| lf
->lno_e
< lineno
))
1848 if (line_range_add_line(fname
, lineno
, lf
->lr
) < 0)
1854 /* Find line range from its line number */
1855 static int find_line_range_by_line(Dwarf_Die
*sp_die
, struct line_finder
*lf
)
1859 ret
= die_walk_lines(sp_die
?: &lf
->cu_die
, line_range_walk_cb
, lf
);
1863 if (!list_empty(&lf
->lr
->line_list
))
1864 ret
= lf
->found
= 1;
1866 ret
= 0; /* Lines are not found */
1869 lf
->lr
->path
= NULL
;
1874 static int line_range_inline_cb(Dwarf_Die
*in_die
, void *data
)
1876 struct dwarf_callback_param
*param
= data
;
1878 param
->retval
= find_line_range_by_line(in_die
, param
->data
);
1879 return DWARF_CB_ABORT
; /* No need to find other instances */
1882 /* Search function from function name */
1883 static int line_range_search_cb(Dwarf_Die
*sp_die
, void *data
)
1885 struct dwarf_callback_param
*param
= data
;
1886 struct line_finder
*lf
= param
->data
;
1887 struct line_range
*lr
= lf
->lr
;
1889 /* Check declared file */
1890 if (lr
->file
&& strtailcmp(lr
->file
, dwarf_decl_file(sp_die
)))
1893 if (dwarf_tag(sp_die
) == DW_TAG_subprogram
&&
1894 die_compare_name(sp_die
, lr
->function
)) {
1895 lf
->fname
= dwarf_decl_file(sp_die
);
1896 dwarf_decl_line(sp_die
, &lr
->offset
);
1897 pr_debug("fname: %s, lineno:%d\n", lf
->fname
, lr
->offset
);
1898 lf
->lno_s
= lr
->offset
+ lr
->start
;
1899 if (lf
->lno_s
< 0) /* Overflow */
1900 lf
->lno_s
= INT_MAX
;
1901 lf
->lno_e
= lr
->offset
+ lr
->end
;
1902 if (lf
->lno_e
< 0) /* Overflow */
1903 lf
->lno_e
= INT_MAX
;
1904 pr_debug("New line range: %d to %d\n", lf
->lno_s
, lf
->lno_e
);
1905 lr
->start
= lf
->lno_s
;
1906 lr
->end
= lf
->lno_e
;
1907 if (dwarf_func_inline(sp_die
)) {
1908 struct dwarf_callback_param _param
;
1909 _param
.data
= (void *)lf
;
1911 dwarf_func_inline_instances(sp_die
,
1912 line_range_inline_cb
,
1914 param
->retval
= _param
.retval
;
1916 param
->retval
= find_line_range_by_line(sp_die
, lf
);
1917 return DWARF_CB_ABORT
;
1922 static int find_line_range_by_func(struct line_finder
*lf
)
1924 struct dwarf_callback_param param
= {.data
= (void *)lf
, .retval
= 0};
1925 dwarf_getfuncs(&lf
->cu_die
, line_range_search_cb
, ¶m
, 0);
1926 return param
.retval
;
1929 int find_line_range(int fd
, struct line_range
*lr
)
1931 struct line_finder lf
= {.lr
= lr
, .found
= 0};
1933 Dwarf_Off off
= 0, noff
;
1938 Dwarf_Addr bias
; /* Currently ignored */
1939 const char *comp_dir
;
1941 dbg
= dwfl_init_offline_dwarf(fd
, &dwfl
, &bias
);
1943 pr_warning("No debug information found in the vmlinux - "
1944 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
1945 close(fd
); /* Without dwfl_end(), fd isn't closed. */
1949 /* Loop on CUs (Compilation Unit) */
1950 while (!lf
.found
&& ret
>= 0) {
1951 if (dwarf_nextcu(dbg
, off
, &noff
, &cuhl
, NULL
, NULL
, NULL
) != 0)
1954 /* Get the DIE(Debugging Information Entry) of this CU */
1955 diep
= dwarf_offdie(dbg
, off
+ cuhl
, &lf
.cu_die
);
1959 /* Check if target file is included. */
1961 lf
.fname
= cu_find_realpath(&lf
.cu_die
, lr
->file
);
1965 if (!lr
->file
|| lf
.fname
) {
1967 ret
= find_line_range_by_func(&lf
);
1969 lf
.lno_s
= lr
->start
;
1971 ret
= find_line_range_by_line(NULL
, &lf
);
1977 /* Store comp_dir */
1979 comp_dir
= cu_get_comp_dir(&lf
.cu_die
);
1981 lr
->comp_dir
= strdup(comp_dir
);
1987 pr_debug("path: %s\n", lr
->path
);
1989 return (ret
< 0) ? ret
: lf
.found
;