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 /* Compare diename and tname */
277 static bool die_compare_name(Dwarf_Die
*dw_die
, const char *tname
)
280 name
= dwarf_diename(dw_die
);
281 return name
? (strcmp(tname
, name
) == 0) : false;
284 /* Get callsite line number of inline-function instance */
285 static int die_get_call_lineno(Dwarf_Die
*in_die
)
287 Dwarf_Attribute attr
;
290 if (!dwarf_attr(in_die
, DW_AT_call_line
, &attr
))
293 dwarf_formudata(&attr
, &ret
);
298 static Dwarf_Die
*die_get_type(Dwarf_Die
*vr_die
, Dwarf_Die
*die_mem
)
300 Dwarf_Attribute attr
;
302 if (dwarf_attr_integrate(vr_die
, DW_AT_type
, &attr
) &&
303 dwarf_formref_die(&attr
, die_mem
))
309 /* Get a type die, but skip qualifiers */
310 static Dwarf_Die
*__die_get_real_type(Dwarf_Die
*vr_die
, Dwarf_Die
*die_mem
)
315 vr_die
= die_get_type(vr_die
, die_mem
);
318 tag
= dwarf_tag(vr_die
);
319 } while (tag
== DW_TAG_const_type
||
320 tag
== DW_TAG_restrict_type
||
321 tag
== DW_TAG_volatile_type
||
322 tag
== DW_TAG_shared_type
);
327 /* Get a type die, but skip qualifiers and typedef */
328 static Dwarf_Die
*die_get_real_type(Dwarf_Die
*vr_die
, Dwarf_Die
*die_mem
)
331 vr_die
= __die_get_real_type(vr_die
, die_mem
);
332 } while (vr_die
&& dwarf_tag(vr_die
) == DW_TAG_typedef
);
337 static int die_get_attr_udata(Dwarf_Die
*tp_die
, unsigned int attr_name
,
340 Dwarf_Attribute attr
;
342 if (dwarf_attr(tp_die
, attr_name
, &attr
) == NULL
||
343 dwarf_formudata(&attr
, result
) != 0)
349 static bool die_is_signed_type(Dwarf_Die
*tp_die
)
353 if (die_get_attr_udata(tp_die
, DW_AT_encoding
, &ret
))
356 return (ret
== DW_ATE_signed_char
|| ret
== DW_ATE_signed
||
357 ret
== DW_ATE_signed_fixed
);
360 static int die_get_byte_size(Dwarf_Die
*tp_die
)
364 if (die_get_attr_udata(tp_die
, DW_AT_byte_size
, &ret
))
370 static int die_get_bit_size(Dwarf_Die
*tp_die
)
374 if (die_get_attr_udata(tp_die
, DW_AT_bit_size
, &ret
))
380 static int die_get_bit_offset(Dwarf_Die
*tp_die
)
384 if (die_get_attr_udata(tp_die
, DW_AT_bit_offset
, &ret
))
390 /* Get data_member_location offset */
391 static int die_get_data_member_location(Dwarf_Die
*mb_die
, Dwarf_Word
*offs
)
393 Dwarf_Attribute attr
;
398 if (dwarf_attr(mb_die
, DW_AT_data_member_location
, &attr
) == NULL
)
401 if (dwarf_formudata(&attr
, offs
) != 0) {
402 /* DW_AT_data_member_location should be DW_OP_plus_uconst */
403 ret
= dwarf_getlocation(&attr
, &expr
, &nexpr
);
404 if (ret
< 0 || nexpr
== 0)
407 if (expr
[0].atom
!= DW_OP_plus_uconst
|| nexpr
!= 1) {
408 pr_debug("Unable to get offset:Unexpected OP %x (%zd)\n",
409 expr
[0].atom
, nexpr
);
412 *offs
= (Dwarf_Word
)expr
[0].number
;
417 /* Return values for die_find callbacks */
419 DIE_FIND_CB_FOUND
= 0, /* End of Search */
420 DIE_FIND_CB_CHILD
= 1, /* Search only children */
421 DIE_FIND_CB_SIBLING
= 2, /* Search only siblings */
422 DIE_FIND_CB_CONTINUE
= 3, /* Search children and siblings */
425 /* Search a child die */
426 static Dwarf_Die
*die_find_child(Dwarf_Die
*rt_die
,
427 int (*callback
)(Dwarf_Die
*, void *),
428 void *data
, Dwarf_Die
*die_mem
)
433 ret
= dwarf_child(rt_die
, die_mem
);
438 ret
= callback(die_mem
, data
);
439 if (ret
== DIE_FIND_CB_FOUND
)
442 if ((ret
& DIE_FIND_CB_CHILD
) &&
443 die_find_child(die_mem
, callback
, data
, &child_die
)) {
444 memcpy(die_mem
, &child_die
, sizeof(Dwarf_Die
));
447 } while ((ret
& DIE_FIND_CB_SIBLING
) &&
448 dwarf_siblingof(die_mem
, die_mem
) == 0);
453 struct __addr_die_search_param
{
458 static int __die_search_func_cb(Dwarf_Die
*fn_die
, void *data
)
460 struct __addr_die_search_param
*ad
= data
;
462 if (dwarf_tag(fn_die
) == DW_TAG_subprogram
&&
463 dwarf_haspc(fn_die
, ad
->addr
)) {
464 memcpy(ad
->die_mem
, fn_die
, sizeof(Dwarf_Die
));
465 return DWARF_CB_ABORT
;
470 /* Search a real subprogram including this line, */
471 static Dwarf_Die
*die_find_real_subprogram(Dwarf_Die
*cu_die
, Dwarf_Addr addr
,
474 struct __addr_die_search_param ad
;
476 ad
.die_mem
= die_mem
;
477 /* dwarf_getscopes can't find subprogram. */
478 if (!dwarf_getfuncs(cu_die
, __die_search_func_cb
, &ad
, 0))
484 /* die_find callback for inline function search */
485 static int __die_find_inline_cb(Dwarf_Die
*die_mem
, void *data
)
487 Dwarf_Addr
*addr
= data
;
489 if (dwarf_tag(die_mem
) == DW_TAG_inlined_subroutine
&&
490 dwarf_haspc(die_mem
, *addr
))
491 return DIE_FIND_CB_FOUND
;
493 return DIE_FIND_CB_CONTINUE
;
496 /* Similar to dwarf_getfuncs, but returns inlined_subroutine if exists. */
497 static Dwarf_Die
*die_find_inlinefunc(Dwarf_Die
*sp_die
, Dwarf_Addr addr
,
500 return die_find_child(sp_die
, __die_find_inline_cb
, &addr
, die_mem
);
503 /* Walker on lines (Note: line number will not be sorted) */
504 typedef int (* line_walk_handler_t
) (const char *fname
, int lineno
,
505 Dwarf_Addr addr
, void *data
);
507 struct __line_walk_param
{
509 line_walk_handler_t handler
;
514 static int __die_walk_funclines_cb(Dwarf_Die
*in_die
, void *data
)
516 struct __line_walk_param
*lw
= data
;
520 if (dwarf_tag(in_die
) == DW_TAG_inlined_subroutine
) {
521 lineno
= die_get_call_lineno(in_die
);
522 if (lineno
> 0 && dwarf_entrypc(in_die
, &addr
) == 0) {
523 lw
->retval
= lw
->handler(lw
->fname
, lineno
, addr
,
526 return DIE_FIND_CB_FOUND
;
529 return DIE_FIND_CB_SIBLING
;
532 /* Walk on lines of blocks included in given DIE */
533 static int __die_walk_funclines(Dwarf_Die
*sp_die
,
534 line_walk_handler_t handler
, void *data
)
536 struct __line_walk_param lw
= {
545 /* Handle function declaration line */
546 lw
.fname
= dwarf_decl_file(sp_die
);
547 if (lw
.fname
&& dwarf_decl_line(sp_die
, &lineno
) == 0 &&
548 dwarf_entrypc(sp_die
, &addr
) == 0) {
549 lw
.retval
= handler(lw
.fname
, lineno
, addr
, data
);
553 die_find_child(sp_die
, __die_walk_funclines_cb
, &lw
, &die_mem
);
558 static int __die_walk_culines_cb(Dwarf_Die
*sp_die
, void *data
)
560 struct __line_walk_param
*lw
= data
;
562 lw
->retval
= __die_walk_funclines(sp_die
, lw
->handler
, lw
->data
);
564 return DWARF_CB_ABORT
;
570 * Walk on lines inside given PDIE. If the PDIE is subprogram, walk only on
571 * the lines inside the subprogram, otherwise PDIE must be a CU DIE.
573 static int die_walk_lines(Dwarf_Die
*pdie
, line_walk_handler_t handler
,
581 Dwarf_Die die_mem
, *cu_die
;
585 if (dwarf_tag(pdie
) == DW_TAG_subprogram
)
586 cu_die
= dwarf_diecu(pdie
, &die_mem
, NULL
, NULL
);
590 pr_debug2("Failed to get CU from subprogram\n");
594 /* Get lines list in the CU */
595 if (dwarf_getsrclines(cu_die
, &lines
, &nlines
) != 0) {
596 pr_debug2("Failed to get source lines on this CU.\n");
599 pr_debug2("Get %zd lines from this CU\n", nlines
);
601 /* Walk on the lines on lines list */
602 for (i
= 0; i
< nlines
; i
++) {
603 line
= dwarf_onesrcline(lines
, i
);
605 dwarf_lineno(line
, &lineno
) != 0 ||
606 dwarf_lineaddr(line
, &addr
) != 0) {
607 pr_debug2("Failed to get line info. "
608 "Possible error in debuginfo.\n");
611 /* Filter lines based on address */
615 * The line is included in given function, and
616 * no inline block includes it.
618 if (!dwarf_haspc(pdie
, addr
) ||
619 die_find_inlinefunc(pdie
, addr
, &die_mem
))
621 /* Get source line */
622 fname
= dwarf_linesrc(line
, NULL
, NULL
);
624 ret
= handler(fname
, lineno
, addr
, data
);
630 * Dwarf lines doesn't include function declarations and inlined
631 * subroutines. We have to check functions list or given function.
634 ret
= __die_walk_funclines(pdie
, handler
, data
);
636 struct __line_walk_param param
= {
641 dwarf_getfuncs(cu_die
, __die_walk_culines_cb
, ¶m
, 0);
648 struct __find_variable_param
{
653 static int __die_find_variable_cb(Dwarf_Die
*die_mem
, void *data
)
655 struct __find_variable_param
*fvp
= data
;
658 tag
= dwarf_tag(die_mem
);
659 if ((tag
== DW_TAG_formal_parameter
||
660 tag
== DW_TAG_variable
) &&
661 die_compare_name(die_mem
, fvp
->name
))
662 return DIE_FIND_CB_FOUND
;
664 if (dwarf_haspc(die_mem
, fvp
->addr
))
665 return DIE_FIND_CB_CONTINUE
;
667 return DIE_FIND_CB_SIBLING
;
670 /* Find a variable called 'name' at given address */
671 static Dwarf_Die
*die_find_variable_at(Dwarf_Die
*sp_die
, const char *name
,
672 Dwarf_Addr addr
, Dwarf_Die
*die_mem
)
674 struct __find_variable_param fvp
= { .name
= name
, .addr
= addr
};
676 return die_find_child(sp_die
, __die_find_variable_cb
, (void *)&fvp
,
680 static int __die_find_member_cb(Dwarf_Die
*die_mem
, void *data
)
682 const char *name
= data
;
684 if ((dwarf_tag(die_mem
) == DW_TAG_member
) &&
685 die_compare_name(die_mem
, name
))
686 return DIE_FIND_CB_FOUND
;
688 return DIE_FIND_CB_SIBLING
;
691 /* Find a member called 'name' */
692 static Dwarf_Die
*die_find_member(Dwarf_Die
*st_die
, const char *name
,
695 return die_find_child(st_die
, __die_find_member_cb
, (void *)name
,
699 /* Get the name of given variable DIE */
700 static int die_get_typename(Dwarf_Die
*vr_die
, char *buf
, int len
)
704 const char *tmp
= "";
706 if (__die_get_real_type(vr_die
, &type
) == NULL
)
709 tag
= dwarf_tag(&type
);
710 if (tag
== DW_TAG_array_type
|| tag
== DW_TAG_pointer_type
)
712 else if (tag
== DW_TAG_subroutine_type
) {
713 /* Function pointer */
714 ret
= snprintf(buf
, len
, "(function_type)");
715 return (ret
>= len
) ? -E2BIG
: ret
;
717 if (!dwarf_diename(&type
))
719 if (tag
== DW_TAG_union_type
)
721 else if (tag
== DW_TAG_structure_type
)
723 /* Write a base name */
724 ret
= snprintf(buf
, len
, "%s%s", tmp
, dwarf_diename(&type
));
725 return (ret
>= len
) ? -E2BIG
: ret
;
727 ret
= die_get_typename(&type
, buf
, len
);
729 ret2
= snprintf(buf
+ ret
, len
- ret
, "%s", tmp
);
730 ret
= (ret2
>= len
- ret
) ? -E2BIG
: ret2
+ ret
;
735 /* Get the name and type of given variable DIE, stored as "type\tname" */
736 static int die_get_varname(Dwarf_Die
*vr_die
, char *buf
, int len
)
740 ret
= die_get_typename(vr_die
, buf
, len
);
742 pr_debug("Failed to get type, make it unknown.\n");
743 ret
= snprintf(buf
, len
, "(unknown_type)");
746 ret2
= snprintf(buf
+ ret
, len
- ret
, "\t%s",
747 dwarf_diename(vr_die
));
748 ret
= (ret2
>= len
- ret
) ? -E2BIG
: ret2
+ ret
;
754 * Probe finder related functions
757 static struct probe_trace_arg_ref
*alloc_trace_arg_ref(long offs
)
759 struct probe_trace_arg_ref
*ref
;
760 ref
= zalloc(sizeof(struct probe_trace_arg_ref
));
767 * Convert a location into trace_arg.
768 * If tvar == NULL, this just checks variable can be converted.
770 static int convert_variable_location(Dwarf_Die
*vr_die
, Dwarf_Addr addr
,
772 struct probe_trace_arg
*tvar
)
774 Dwarf_Attribute attr
;
783 if (dwarf_attr(vr_die
, DW_AT_external
, &attr
) != NULL
)
786 /* TODO: handle more than 1 exprs */
787 if (dwarf_attr(vr_die
, DW_AT_location
, &attr
) == NULL
||
788 dwarf_getlocation_addr(&attr
, addr
, &op
, &nops
, 1) <= 0 ||
790 /* TODO: Support const_value */
794 if (op
->atom
== DW_OP_addr
) {
798 /* Static variables on memory (not stack), make @varname */
799 ret
= strlen(dwarf_diename(vr_die
));
800 tvar
->value
= zalloc(ret
+ 2);
801 if (tvar
->value
== NULL
)
803 snprintf(tvar
->value
, ret
+ 2, "@%s", dwarf_diename(vr_die
));
804 tvar
->ref
= alloc_trace_arg_ref((long)offs
);
805 if (tvar
->ref
== NULL
)
810 /* If this is based on frame buffer, set the offset */
811 if (op
->atom
== DW_OP_fbreg
) {
819 if (op
->atom
>= DW_OP_breg0
&& op
->atom
<= DW_OP_breg31
) {
820 regn
= op
->atom
- DW_OP_breg0
;
823 } else if (op
->atom
>= DW_OP_reg0
&& op
->atom
<= DW_OP_reg31
) {
824 regn
= op
->atom
- DW_OP_reg0
;
825 } else if (op
->atom
== DW_OP_bregx
) {
829 } else if (op
->atom
== DW_OP_regx
) {
832 pr_debug("DW_OP %x is not supported.\n", op
->atom
);
839 regs
= get_arch_regstr(regn
);
841 /* This should be a bug in DWARF or this tool */
842 pr_warning("Mapping for the register number %u "
843 "missing on this architecture.\n", regn
);
847 tvar
->value
= strdup(regs
);
848 if (tvar
->value
== NULL
)
852 tvar
->ref
= alloc_trace_arg_ref((long)offs
);
853 if (tvar
->ref
== NULL
)
859 #define BYTES_TO_BITS(nb) ((nb) * BITS_PER_LONG / sizeof(long))
861 static int convert_variable_type(Dwarf_Die
*vr_die
,
862 struct probe_trace_arg
*tvar
,
865 struct probe_trace_arg_ref
**ref_ptr
= &tvar
->ref
;
870 /* TODO: check all types */
871 if (cast
&& strcmp(cast
, "string") != 0) {
872 /* Non string type is OK */
873 tvar
->type
= strdup(cast
);
874 return (tvar
->type
== NULL
) ? -ENOMEM
: 0;
877 if (die_get_bit_size(vr_die
) != 0) {
878 /* This is a bitfield */
879 ret
= snprintf(buf
, 16, "b%d@%d/%zd", die_get_bit_size(vr_die
),
880 die_get_bit_offset(vr_die
),
881 BYTES_TO_BITS(die_get_byte_size(vr_die
)));
885 if (die_get_real_type(vr_die
, &type
) == NULL
) {
886 pr_warning("Failed to get a type information of %s.\n",
887 dwarf_diename(vr_die
));
891 pr_debug("%s type is %s.\n",
892 dwarf_diename(vr_die
), dwarf_diename(&type
));
894 if (cast
&& strcmp(cast
, "string") == 0) { /* String type */
895 ret
= dwarf_tag(&type
);
896 if (ret
!= DW_TAG_pointer_type
&&
897 ret
!= DW_TAG_array_type
) {
898 pr_warning("Failed to cast into string: "
899 "%s(%s) is not a pointer nor array.\n",
900 dwarf_diename(vr_die
), dwarf_diename(&type
));
903 if (ret
== DW_TAG_pointer_type
) {
904 if (die_get_real_type(&type
, &type
) == NULL
) {
905 pr_warning("Failed to get a type"
910 ref_ptr
= &(*ref_ptr
)->next
;
911 /* Add new reference with offset +0 */
912 *ref_ptr
= zalloc(sizeof(struct probe_trace_arg_ref
));
913 if (*ref_ptr
== NULL
) {
914 pr_warning("Out of memory error\n");
918 if (!die_compare_name(&type
, "char") &&
919 !die_compare_name(&type
, "unsigned char")) {
920 pr_warning("Failed to cast into string: "
921 "%s is not (unsigned) char *.\n",
922 dwarf_diename(vr_die
));
925 tvar
->type
= strdup(cast
);
926 return (tvar
->type
== NULL
) ? -ENOMEM
: 0;
929 ret
= BYTES_TO_BITS(die_get_byte_size(&type
));
931 /* No size ... try to use default type */
934 /* Check the bitwidth */
935 if (ret
> MAX_BASIC_TYPE_BITS
) {
936 pr_info("%s exceeds max-bitwidth. Cut down to %d bits.\n",
937 dwarf_diename(&type
), MAX_BASIC_TYPE_BITS
);
938 ret
= MAX_BASIC_TYPE_BITS
;
940 ret
= snprintf(buf
, 16, "%c%d",
941 die_is_signed_type(&type
) ? 's' : 'u', ret
);
944 if (ret
< 0 || ret
>= 16) {
947 pr_warning("Failed to convert variable type: %s\n",
951 tvar
->type
= strdup(buf
);
952 if (tvar
->type
== NULL
)
957 static int convert_variable_fields(Dwarf_Die
*vr_die
, const char *varname
,
958 struct perf_probe_arg_field
*field
,
959 struct probe_trace_arg_ref
**ref_ptr
,
962 struct probe_trace_arg_ref
*ref
= *ref_ptr
;
967 pr_debug("converting %s in %s\n", field
->name
, varname
);
968 if (die_get_real_type(vr_die
, &type
) == NULL
) {
969 pr_warning("Failed to get the type of %s.\n", varname
);
972 pr_debug2("Var real type: (%x)\n", (unsigned)dwarf_dieoffset(&type
));
973 tag
= dwarf_tag(&type
);
975 if (field
->name
[0] == '[' &&
976 (tag
== DW_TAG_array_type
|| tag
== DW_TAG_pointer_type
)) {
978 /* Save original type for next field */
979 memcpy(die_mem
, &type
, sizeof(*die_mem
));
980 /* Get the type of this array */
981 if (die_get_real_type(&type
, &type
) == NULL
) {
982 pr_warning("Failed to get the type of %s.\n", varname
);
985 pr_debug2("Array real type: (%x)\n",
986 (unsigned)dwarf_dieoffset(&type
));
987 if (tag
== DW_TAG_pointer_type
) {
988 ref
= zalloc(sizeof(struct probe_trace_arg_ref
));
992 (*ref_ptr
)->next
= ref
;
996 ref
->offset
+= die_get_byte_size(&type
) * field
->index
;
998 /* Save vr_die for converting types */
999 memcpy(die_mem
, vr_die
, sizeof(*die_mem
));
1001 } else if (tag
== DW_TAG_pointer_type
) {
1002 /* Check the pointer and dereference */
1004 pr_err("Semantic error: %s must be referred by '->'\n",
1008 /* Get the type pointed by this pointer */
1009 if (die_get_real_type(&type
, &type
) == NULL
) {
1010 pr_warning("Failed to get the type of %s.\n", varname
);
1013 /* Verify it is a data structure */
1014 if (dwarf_tag(&type
) != DW_TAG_structure_type
) {
1015 pr_warning("%s is not a data structure.\n", varname
);
1019 ref
= zalloc(sizeof(struct probe_trace_arg_ref
));
1023 (*ref_ptr
)->next
= ref
;
1027 /* Verify it is a data structure */
1028 if (tag
!= DW_TAG_structure_type
) {
1029 pr_warning("%s is not a data structure.\n", varname
);
1032 if (field
->name
[0] == '[') {
1033 pr_err("Semantic error: %s is not a pointor"
1034 " nor array.\n", varname
);
1038 pr_err("Semantic error: %s must be referred by '.'\n",
1043 pr_warning("Structure on a register is not "
1044 "supported yet.\n");
1049 if (die_find_member(&type
, field
->name
, die_mem
) == NULL
) {
1050 pr_warning("%s(tyep:%s) has no member %s.\n", varname
,
1051 dwarf_diename(&type
), field
->name
);
1055 /* Get the offset of the field */
1056 ret
= die_get_data_member_location(die_mem
, &offs
);
1058 pr_warning("Failed to get the offset of %s.\n", field
->name
);
1061 ref
->offset
+= (long)offs
;
1064 /* Converting next field */
1066 return convert_variable_fields(die_mem
, field
->name
,
1067 field
->next
, &ref
, die_mem
);
1072 /* Show a variables in kprobe event format */
1073 static int convert_variable(Dwarf_Die
*vr_die
, struct probe_finder
*pf
)
1078 pr_debug("Converting variable %s into trace event.\n",
1079 dwarf_diename(vr_die
));
1081 ret
= convert_variable_location(vr_die
, pf
->addr
, pf
->fb_ops
,
1084 pr_err("Failed to find the location of %s at this address.\n"
1085 " Perhaps, it has been optimized out.\n", pf
->pvar
->var
);
1086 else if (ret
== -ENOTSUP
)
1087 pr_err("Sorry, we don't support this variable location yet.\n");
1088 else if (pf
->pvar
->field
) {
1089 ret
= convert_variable_fields(vr_die
, pf
->pvar
->var
,
1090 pf
->pvar
->field
, &pf
->tvar
->ref
,
1095 ret
= convert_variable_type(vr_die
, pf
->tvar
, pf
->pvar
->type
);
1096 /* *expr will be cached in libdw. Don't free it. */
1100 /* Find a variable in a subprogram die */
1101 static int find_variable(Dwarf_Die
*sp_die
, struct probe_finder
*pf
)
1103 Dwarf_Die vr_die
, *scopes
;
1107 if (!is_c_varname(pf
->pvar
->var
)) {
1108 /* Copy raw parameters */
1109 pf
->tvar
->value
= strdup(pf
->pvar
->var
);
1110 if (pf
->tvar
->value
== NULL
)
1112 if (pf
->pvar
->type
) {
1113 pf
->tvar
->type
= strdup(pf
->pvar
->type
);
1114 if (pf
->tvar
->type
== NULL
)
1117 if (pf
->pvar
->name
) {
1118 pf
->tvar
->name
= strdup(pf
->pvar
->name
);
1119 if (pf
->tvar
->name
== NULL
)
1122 pf
->tvar
->name
= NULL
;
1127 pf
->tvar
->name
= strdup(pf
->pvar
->name
);
1129 ret
= synthesize_perf_probe_arg(pf
->pvar
, buf
, 32);
1132 ptr
= strchr(buf
, ':'); /* Change type separator to _ */
1135 pf
->tvar
->name
= strdup(buf
);
1137 if (pf
->tvar
->name
== NULL
)
1140 pr_debug("Searching '%s' variable in context.\n",
1142 /* Search child die for local variables and parameters. */
1143 if (die_find_variable_at(sp_die
, pf
->pvar
->var
, pf
->addr
, &vr_die
))
1144 ret
= convert_variable(&vr_die
, pf
);
1146 /* Search upper class */
1147 nscopes
= dwarf_getscopes_die(sp_die
, &scopes
);
1148 while (nscopes
-- > 1) {
1149 pr_debug("Searching variables in %s\n",
1150 dwarf_diename(&scopes
[nscopes
]));
1151 /* We should check this scope, so give dummy address */
1152 if (die_find_variable_at(&scopes
[nscopes
],
1155 ret
= convert_variable(&vr_die
, pf
);
1165 pr_warning("Failed to find '%s' in this function.\n",
1170 /* Convert subprogram DIE to trace point */
1171 static int convert_to_trace_point(Dwarf_Die
*sp_die
, Dwarf_Addr paddr
,
1172 bool retprobe
, struct probe_trace_point
*tp
)
1177 /* Copy the name of probe point */
1178 name
= dwarf_diename(sp_die
);
1180 if (dwarf_entrypc(sp_die
, &eaddr
) != 0) {
1181 pr_warning("Failed to get entry address of %s\n",
1182 dwarf_diename(sp_die
));
1185 tp
->symbol
= strdup(name
);
1186 if (tp
->symbol
== NULL
)
1188 tp
->offset
= (unsigned long)(paddr
- eaddr
);
1190 /* This function has no name. */
1191 tp
->offset
= (unsigned long)paddr
;
1193 /* Return probe must be on the head of a subprogram */
1195 if (eaddr
!= paddr
) {
1196 pr_warning("Return probe must be on the head of"
1197 " a real function.\n");
1200 tp
->retprobe
= true;
1206 /* Call probe_finder callback with real subprogram DIE */
1207 static int call_probe_finder(Dwarf_Die
*sp_die
, struct probe_finder
*pf
)
1210 Dwarf_Attribute fb_attr
;
1214 /* If no real subprogram, find a real one */
1215 if (!sp_die
|| dwarf_tag(sp_die
) != DW_TAG_subprogram
) {
1216 sp_die
= die_find_real_subprogram(&pf
->cu_die
,
1217 pf
->addr
, &die_mem
);
1219 pr_warning("Failed to find probe point in any "
1225 /* Get the frame base attribute/ops */
1226 dwarf_attr(sp_die
, DW_AT_frame_base
, &fb_attr
);
1227 ret
= dwarf_getlocation_addr(&fb_attr
, pf
->addr
, &pf
->fb_ops
, &nops
, 1);
1228 if (ret
<= 0 || nops
== 0) {
1230 #if _ELFUTILS_PREREQ(0, 142)
1231 } else if (nops
== 1 && pf
->fb_ops
[0].atom
== DW_OP_call_frame_cfa
&&
1234 if (dwarf_cfi_addrframe(pf
->cfi
, pf
->addr
, &frame
) != 0 ||
1235 dwarf_frame_cfa(frame
, &pf
->fb_ops
, &nops
) != 0) {
1236 pr_warning("Failed to get call frame on 0x%jx\n",
1237 (uintmax_t)pf
->addr
);
1243 /* Call finder's callback handler */
1244 ret
= pf
->callback(sp_die
, pf
);
1246 /* *pf->fb_ops will be cached in libdw. Don't free it. */
1252 static int probe_point_line_walker(const char *fname
, int lineno
,
1253 Dwarf_Addr addr
, void *data
)
1255 struct probe_finder
*pf
= data
;
1258 if (lineno
!= pf
->lno
|| strtailcmp(fname
, pf
->fname
) != 0)
1262 ret
= call_probe_finder(NULL
, pf
);
1264 /* Continue if no error, because the line will be in inline function */
1265 return ret
< 0 ? ret
: 0;
1268 /* Find probe point from its line number */
1269 static int find_probe_point_by_line(struct probe_finder
*pf
)
1271 return die_walk_lines(&pf
->cu_die
, probe_point_line_walker
, pf
);
1274 /* Find lines which match lazy pattern */
1275 static int find_lazy_match_lines(struct list_head
*head
,
1276 const char *fname
, const char *pat
)
1282 int count
= 0, linenum
= 1;
1284 fp
= fopen(fname
, "r");
1286 pr_warning("Failed to open %s: %s\n", fname
, strerror(errno
));
1290 while ((len
= getline(&line
, &line_len
, fp
)) > 0) {
1292 if (line
[len
- 1] == '\n')
1293 line
[len
- 1] = '\0';
1295 if (strlazymatch(line
, pat
)) {
1296 line_list__add_line(head
, linenum
);
1308 pr_debug("No matched lines found in %s.\n", fname
);
1312 static int probe_point_lazy_walker(const char *fname
, int lineno
,
1313 Dwarf_Addr addr
, void *data
)
1315 struct probe_finder
*pf
= data
;
1318 if (!line_list__has_line(&pf
->lcache
, lineno
) ||
1319 strtailcmp(fname
, pf
->fname
) != 0)
1322 pr_debug("Probe line found: line:%d addr:0x%llx\n",
1323 lineno
, (unsigned long long)addr
);
1325 ret
= call_probe_finder(NULL
, pf
);
1328 * Continue if no error, because the lazy pattern will match
1331 return ret
< 0 ?: 0;
1334 /* Find probe points from lazy pattern */
1335 static int find_probe_point_lazy(Dwarf_Die
*sp_die
, struct probe_finder
*pf
)
1339 if (list_empty(&pf
->lcache
)) {
1340 /* Matching lazy line pattern */
1341 ret
= find_lazy_match_lines(&pf
->lcache
, pf
->fname
,
1342 pf
->pev
->point
.lazy_line
);
1347 return die_walk_lines(sp_die
, probe_point_lazy_walker
, pf
);
1350 /* Callback parameter with return value */
1351 struct dwarf_callback_param
{
1356 static int probe_point_inline_cb(Dwarf_Die
*in_die
, void *data
)
1358 struct dwarf_callback_param
*param
= data
;
1359 struct probe_finder
*pf
= param
->data
;
1360 struct perf_probe_point
*pp
= &pf
->pev
->point
;
1364 param
->retval
= find_probe_point_lazy(in_die
, pf
);
1366 /* Get probe address */
1367 if (dwarf_entrypc(in_die
, &addr
) != 0) {
1368 pr_warning("Failed to get entry address of %s.\n",
1369 dwarf_diename(in_die
));
1370 param
->retval
= -ENOENT
;
1371 return DWARF_CB_ABORT
;
1374 pf
->addr
+= pp
->offset
;
1375 pr_debug("found inline addr: 0x%jx\n",
1376 (uintmax_t)pf
->addr
);
1378 param
->retval
= call_probe_finder(in_die
, pf
);
1379 if (param
->retval
< 0)
1380 return DWARF_CB_ABORT
;
1386 /* Search function from function name */
1387 static int probe_point_search_cb(Dwarf_Die
*sp_die
, void *data
)
1389 struct dwarf_callback_param
*param
= data
;
1390 struct probe_finder
*pf
= param
->data
;
1391 struct perf_probe_point
*pp
= &pf
->pev
->point
;
1393 /* Check tag and diename */
1394 if (dwarf_tag(sp_die
) != DW_TAG_subprogram
||
1395 !die_compare_name(sp_die
, pp
->function
))
1398 pf
->fname
= dwarf_decl_file(sp_die
);
1399 if (pp
->line
) { /* Function relative line */
1400 dwarf_decl_line(sp_die
, &pf
->lno
);
1401 pf
->lno
+= pp
->line
;
1402 param
->retval
= find_probe_point_by_line(pf
);
1403 } else if (!dwarf_func_inline(sp_die
)) {
1406 param
->retval
= find_probe_point_lazy(sp_die
, pf
);
1408 if (dwarf_entrypc(sp_die
, &pf
->addr
) != 0) {
1409 pr_warning("Failed to get entry address of "
1410 "%s.\n", dwarf_diename(sp_die
));
1411 param
->retval
= -ENOENT
;
1412 return DWARF_CB_ABORT
;
1414 pf
->addr
+= pp
->offset
;
1415 /* TODO: Check the address in this function */
1416 param
->retval
= call_probe_finder(sp_die
, pf
);
1419 struct dwarf_callback_param _param
= {.data
= (void *)pf
,
1421 /* Inlined function: search instances */
1422 dwarf_func_inline_instances(sp_die
, probe_point_inline_cb
,
1424 param
->retval
= _param
.retval
;
1427 return DWARF_CB_ABORT
; /* Exit; no same symbol in this CU. */
1430 static int find_probe_point_by_func(struct probe_finder
*pf
)
1432 struct dwarf_callback_param _param
= {.data
= (void *)pf
,
1434 dwarf_getfuncs(&pf
->cu_die
, probe_point_search_cb
, &_param
, 0);
1435 return _param
.retval
;
1438 /* Find probe points from debuginfo */
1439 static int find_probes(int fd
, struct probe_finder
*pf
)
1441 struct perf_probe_point
*pp
= &pf
->pev
->point
;
1442 Dwarf_Off off
, noff
;
1447 Dwarf_Addr bias
; /* Currently ignored */
1450 dbg
= dwfl_init_offline_dwarf(fd
, &dwfl
, &bias
);
1452 pr_warning("No debug information found in the vmlinux - "
1453 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
1457 #if _ELFUTILS_PREREQ(0, 142)
1458 /* Get the call frame information from this dwarf */
1459 pf
->cfi
= dwarf_getcfi(dbg
);
1463 line_list__init(&pf
->lcache
);
1464 /* Loop on CUs (Compilation Unit) */
1465 while (!dwarf_nextcu(dbg
, off
, &noff
, &cuhl
, NULL
, NULL
, NULL
) &&
1467 /* Get the DIE(Debugging Information Entry) of this CU */
1468 diep
= dwarf_offdie(dbg
, off
+ cuhl
, &pf
->cu_die
);
1472 /* Check if target file is included. */
1474 pf
->fname
= cu_find_realpath(&pf
->cu_die
, pp
->file
);
1478 if (!pp
->file
|| pf
->fname
) {
1480 ret
= find_probe_point_by_func(pf
);
1481 else if (pp
->lazy_line
)
1482 ret
= find_probe_point_lazy(NULL
, pf
);
1485 ret
= find_probe_point_by_line(pf
);
1487 if (ret
!= DWARF_CB_OK
)
1492 line_list__free(&pf
->lcache
);
1499 /* Add a found probe point into trace event list */
1500 static int add_probe_trace_event(Dwarf_Die
*sp_die
, struct probe_finder
*pf
)
1502 struct trace_event_finder
*tf
=
1503 container_of(pf
, struct trace_event_finder
, pf
);
1504 struct probe_trace_event
*tev
;
1507 /* Check number of tevs */
1508 if (tf
->ntevs
== tf
->max_tevs
) {
1509 pr_warning("Too many( > %d) probe point found.\n",
1513 tev
= &tf
->tevs
[tf
->ntevs
++];
1515 ret
= convert_to_trace_point(sp_die
, pf
->addr
, pf
->pev
->point
.retprobe
,
1520 pr_debug("Probe point found: %s+%lu\n", tev
->point
.symbol
,
1523 /* Find each argument */
1524 tev
->nargs
= pf
->pev
->nargs
;
1525 tev
->args
= zalloc(sizeof(struct probe_trace_arg
) * tev
->nargs
);
1526 if (tev
->args
== NULL
)
1528 for (i
= 0; i
< pf
->pev
->nargs
; i
++) {
1529 pf
->pvar
= &pf
->pev
->args
[i
];
1530 pf
->tvar
= &tev
->args
[i
];
1531 ret
= find_variable(sp_die
, pf
);
1539 /* Find probe_trace_events specified by perf_probe_event from debuginfo */
1540 int find_probe_trace_events(int fd
, struct perf_probe_event
*pev
,
1541 struct probe_trace_event
**tevs
, int max_tevs
)
1543 struct trace_event_finder tf
= {
1544 .pf
= {.pev
= pev
, .callback
= add_probe_trace_event
},
1545 .max_tevs
= max_tevs
};
1548 /* Allocate result tevs array */
1549 *tevs
= zalloc(sizeof(struct probe_trace_event
) * max_tevs
);
1556 ret
= find_probes(fd
, &tf
.pf
);
1563 return (ret
< 0) ? ret
: tf
.ntevs
;
1566 #define MAX_VAR_LEN 64
1568 /* Collect available variables in this scope */
1569 static int collect_variables_cb(Dwarf_Die
*die_mem
, void *data
)
1571 struct available_var_finder
*af
= data
;
1572 struct variable_list
*vl
;
1573 char buf
[MAX_VAR_LEN
];
1576 vl
= &af
->vls
[af
->nvls
- 1];
1578 tag
= dwarf_tag(die_mem
);
1579 if (tag
== DW_TAG_formal_parameter
||
1580 tag
== DW_TAG_variable
) {
1581 ret
= convert_variable_location(die_mem
, af
->pf
.addr
,
1582 af
->pf
.fb_ops
, NULL
);
1584 ret
= die_get_varname(die_mem
, buf
, MAX_VAR_LEN
);
1585 pr_debug2("Add new var: %s\n", buf
);
1587 strlist__add(vl
->vars
, buf
);
1591 if (af
->child
&& dwarf_haspc(die_mem
, af
->pf
.addr
))
1592 return DIE_FIND_CB_CONTINUE
;
1594 return DIE_FIND_CB_SIBLING
;
1597 /* Add a found vars into available variables list */
1598 static int add_available_vars(Dwarf_Die
*sp_die
, struct probe_finder
*pf
)
1600 struct available_var_finder
*af
=
1601 container_of(pf
, struct available_var_finder
, pf
);
1602 struct variable_list
*vl
;
1603 Dwarf_Die die_mem
, *scopes
= NULL
;
1606 /* Check number of tevs */
1607 if (af
->nvls
== af
->max_vls
) {
1608 pr_warning("Too many( > %d) probe point found.\n", af
->max_vls
);
1611 vl
= &af
->vls
[af
->nvls
++];
1613 ret
= convert_to_trace_point(sp_die
, pf
->addr
, pf
->pev
->point
.retprobe
,
1618 pr_debug("Probe point found: %s+%lu\n", vl
->point
.symbol
,
1621 /* Find local variables */
1622 vl
->vars
= strlist__new(true, NULL
);
1623 if (vl
->vars
== NULL
)
1626 die_find_child(sp_die
, collect_variables_cb
, (void *)af
, &die_mem
);
1628 /* Find external variables */
1631 /* Don't need to search child DIE for externs. */
1633 nscopes
= dwarf_getscopes_die(sp_die
, &scopes
);
1634 while (nscopes
-- > 1)
1635 die_find_child(&scopes
[nscopes
], collect_variables_cb
,
1636 (void *)af
, &die_mem
);
1641 if (strlist__empty(vl
->vars
)) {
1642 strlist__delete(vl
->vars
);
1649 /* Find available variables at given probe point */
1650 int find_available_vars_at(int fd
, struct perf_probe_event
*pev
,
1651 struct variable_list
**vls
, int max_vls
,
1654 struct available_var_finder af
= {
1655 .pf
= {.pev
= pev
, .callback
= add_available_vars
},
1656 .max_vls
= max_vls
, .externs
= externs
};
1659 /* Allocate result vls array */
1660 *vls
= zalloc(sizeof(struct variable_list
) * max_vls
);
1667 ret
= find_probes(fd
, &af
.pf
);
1669 /* Free vlist for error */
1671 if (af
.vls
[af
.nvls
].point
.symbol
)
1672 free(af
.vls
[af
.nvls
].point
.symbol
);
1673 if (af
.vls
[af
.nvls
].vars
)
1674 strlist__delete(af
.vls
[af
.nvls
].vars
);
1681 return (ret
< 0) ? ret
: af
.nvls
;
1684 /* Reverse search */
1685 int find_perf_probe_point(unsigned long addr
, struct perf_probe_point
*ppt
)
1687 Dwarf_Die cudie
, spdie
, indie
;
1691 Dwarf_Addr laddr
, eaddr
, bias
= 0;
1693 int lineno
, ret
= 0;
1696 /* Open the live linux kernel */
1697 dbg
= dwfl_init_live_kernel_dwarf(addr
, &dwfl
, &bias
);
1699 pr_warning("No debug information found in the vmlinux - "
1700 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
1705 /* Adjust address with bias */
1708 if (!dwarf_addrdie(dbg
, (Dwarf_Addr
)addr
- bias
, &cudie
)) {
1709 pr_warning("Failed to find debug information for address %lx\n",
1715 /* Find a corresponding line */
1716 line
= dwarf_getsrc_die(&cudie
, (Dwarf_Addr
)addr
);
1718 if (dwarf_lineaddr(line
, &laddr
) == 0 &&
1719 (Dwarf_Addr
)addr
== laddr
&&
1720 dwarf_lineno(line
, &lineno
) == 0) {
1721 tmp
= dwarf_linesrc(line
, NULL
, NULL
);
1724 ppt
->file
= strdup(tmp
);
1725 if (ppt
->file
== NULL
) {
1734 /* Find a corresponding function */
1735 if (die_find_real_subprogram(&cudie
, (Dwarf_Addr
)addr
, &spdie
)) {
1736 tmp
= dwarf_diename(&spdie
);
1737 if (!tmp
|| dwarf_entrypc(&spdie
, &eaddr
) != 0)
1741 if (die_find_inlinefunc(&spdie
, (Dwarf_Addr
)addr
,
1743 /* addr in an inline function */
1744 tmp
= dwarf_diename(&indie
);
1747 ret
= dwarf_decl_line(&indie
, &lineno
);
1749 if (eaddr
== addr
) { /* Function entry */
1753 ret
= dwarf_decl_line(&spdie
, &lineno
);
1756 /* Make a relative line number */
1757 ppt
->line
-= lineno
;
1761 /* We don't have a line number, let's use offset */
1762 ppt
->offset
= addr
- (unsigned long)eaddr
;
1764 ppt
->function
= strdup(tmp
);
1765 if (ppt
->function
== NULL
) {
1776 ret
= found
? 1 : 0;
1780 /* Add a line and store the src path */
1781 static int line_range_add_line(const char *src
, unsigned int lineno
,
1782 struct line_range
*lr
)
1784 /* Copy source path */
1786 lr
->path
= strdup(src
);
1787 if (lr
->path
== NULL
)
1790 return line_list__add_line(&lr
->line_list
, lineno
);
1793 static int line_range_walk_cb(const char *fname
, int lineno
,
1794 Dwarf_Addr addr __used
,
1797 struct line_finder
*lf
= data
;
1799 if ((strtailcmp(fname
, lf
->fname
) != 0) ||
1800 (lf
->lno_s
> lineno
|| lf
->lno_e
< lineno
))
1803 if (line_range_add_line(fname
, lineno
, lf
->lr
) < 0)
1809 /* Find line range from its line number */
1810 static int find_line_range_by_line(Dwarf_Die
*sp_die
, struct line_finder
*lf
)
1814 ret
= die_walk_lines(sp_die
?: &lf
->cu_die
, line_range_walk_cb
, lf
);
1818 if (!list_empty(&lf
->lr
->line_list
))
1819 ret
= lf
->found
= 1;
1821 ret
= 0; /* Lines are not found */
1824 lf
->lr
->path
= NULL
;
1829 static int line_range_inline_cb(Dwarf_Die
*in_die
, void *data
)
1831 struct dwarf_callback_param
*param
= data
;
1833 param
->retval
= find_line_range_by_line(in_die
, param
->data
);
1834 return DWARF_CB_ABORT
; /* No need to find other instances */
1837 /* Search function from function name */
1838 static int line_range_search_cb(Dwarf_Die
*sp_die
, void *data
)
1840 struct dwarf_callback_param
*param
= data
;
1841 struct line_finder
*lf
= param
->data
;
1842 struct line_range
*lr
= lf
->lr
;
1844 if (dwarf_tag(sp_die
) == DW_TAG_subprogram
&&
1845 die_compare_name(sp_die
, lr
->function
)) {
1846 lf
->fname
= dwarf_decl_file(sp_die
);
1847 dwarf_decl_line(sp_die
, &lr
->offset
);
1848 pr_debug("fname: %s, lineno:%d\n", lf
->fname
, lr
->offset
);
1849 lf
->lno_s
= lr
->offset
+ lr
->start
;
1850 if (lf
->lno_s
< 0) /* Overflow */
1851 lf
->lno_s
= INT_MAX
;
1852 lf
->lno_e
= lr
->offset
+ lr
->end
;
1853 if (lf
->lno_e
< 0) /* Overflow */
1854 lf
->lno_e
= INT_MAX
;
1855 pr_debug("New line range: %d to %d\n", lf
->lno_s
, lf
->lno_e
);
1856 lr
->start
= lf
->lno_s
;
1857 lr
->end
= lf
->lno_e
;
1858 if (dwarf_func_inline(sp_die
)) {
1859 struct dwarf_callback_param _param
;
1860 _param
.data
= (void *)lf
;
1862 dwarf_func_inline_instances(sp_die
,
1863 line_range_inline_cb
,
1865 param
->retval
= _param
.retval
;
1867 param
->retval
= find_line_range_by_line(sp_die
, lf
);
1868 return DWARF_CB_ABORT
;
1873 static int find_line_range_by_func(struct line_finder
*lf
)
1875 struct dwarf_callback_param param
= {.data
= (void *)lf
, .retval
= 0};
1876 dwarf_getfuncs(&lf
->cu_die
, line_range_search_cb
, ¶m
, 0);
1877 return param
.retval
;
1880 int find_line_range(int fd
, struct line_range
*lr
)
1882 struct line_finder lf
= {.lr
= lr
, .found
= 0};
1884 Dwarf_Off off
= 0, noff
;
1889 Dwarf_Addr bias
; /* Currently ignored */
1890 const char *comp_dir
;
1892 dbg
= dwfl_init_offline_dwarf(fd
, &dwfl
, &bias
);
1894 pr_warning("No debug information found in the vmlinux - "
1895 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
1899 /* Loop on CUs (Compilation Unit) */
1900 while (!lf
.found
&& ret
>= 0) {
1901 if (dwarf_nextcu(dbg
, off
, &noff
, &cuhl
, NULL
, NULL
, NULL
) != 0)
1904 /* Get the DIE(Debugging Information Entry) of this CU */
1905 diep
= dwarf_offdie(dbg
, off
+ cuhl
, &lf
.cu_die
);
1909 /* Check if target file is included. */
1911 lf
.fname
= cu_find_realpath(&lf
.cu_die
, lr
->file
);
1915 if (!lr
->file
|| lf
.fname
) {
1917 ret
= find_line_range_by_func(&lf
);
1919 lf
.lno_s
= lr
->start
;
1921 ret
= find_line_range_by_line(NULL
, &lf
);
1927 /* Store comp_dir */
1929 comp_dir
= cu_get_comp_dir(&lf
.cu_die
);
1931 lr
->comp_dir
= strdup(comp_dir
);
1937 pr_debug("path: %s\n", lr
->path
);
1939 return (ret
< 0) ? ret
: lf
.found
;