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>
40 #include "probe-finder.h"
42 /* Kprobe tracer basic type is up to u64 */
43 #define MAX_BASIC_TYPE_BITS 64
46 * Compare the tail of two strings.
47 * Return 0 if whole of either string is same as another's tail part.
49 static int strtailcmp(const char *s1
, const char *s2
)
53 while (--i1
>= 0 && --i2
>= 0) {
55 return s1
[i1
] - s2
[i2
];
60 /* Line number list operations */
62 /* Add a line to line number list */
63 static int line_list__add_line(struct list_head
*head
, int line
)
68 /* Reverse search, because new line will be the last one */
69 list_for_each_entry_reverse(ln
, head
, list
) {
70 if (ln
->line
< line
) {
73 } else if (ln
->line
== line
) /* Already exist */
76 /* List is empty, or the smallest entry */
79 pr_debug("line list: add a line %u\n", line
);
80 ln
= zalloc(sizeof(struct line_node
));
84 INIT_LIST_HEAD(&ln
->list
);
85 list_add(&ln
->list
, p
);
89 /* Check if the line in line number list */
90 static int line_list__has_line(struct list_head
*head
, int line
)
94 /* Reverse search, because new line will be the last one */
95 list_for_each_entry(ln
, head
, list
)
102 /* Init line number list */
103 static void line_list__init(struct list_head
*head
)
105 INIT_LIST_HEAD(head
);
108 /* Free line number list */
109 static void line_list__free(struct list_head
*head
)
111 struct line_node
*ln
;
112 while (!list_empty(head
)) {
113 ln
= list_first_entry(head
, struct line_node
, list
);
121 /* Find the realpath of the target file. */
122 static const char *cu_find_realpath(Dwarf_Die
*cu_die
, const char *fname
)
126 const char *src
= NULL
;
132 ret
= dwarf_getsrcfiles(cu_die
, &files
, &nfiles
);
136 for (i
= 0; i
< nfiles
; i
++) {
137 src
= dwarf_filesrc(files
, i
, NULL
, NULL
);
138 if (strtailcmp(src
, fname
) == 0)
146 /* Get DW_AT_comp_dir (should be NULL with older gcc) */
147 static const char *cu_get_comp_dir(Dwarf_Die
*cu_die
)
149 Dwarf_Attribute attr
;
150 if (dwarf_attr(cu_die
, DW_AT_comp_dir
, &attr
) == NULL
)
152 return dwarf_formstring(&attr
);
155 /* Compare diename and tname */
156 static bool die_compare_name(Dwarf_Die
*dw_die
, const char *tname
)
159 name
= dwarf_diename(dw_die
);
160 return name
? (strcmp(tname
, name
) == 0) : false;
163 /* Get type die, but skip qualifiers and typedef */
164 static Dwarf_Die
*die_get_real_type(Dwarf_Die
*vr_die
, Dwarf_Die
*die_mem
)
166 Dwarf_Attribute attr
;
170 if (dwarf_attr(vr_die
, DW_AT_type
, &attr
) == NULL
||
171 dwarf_formref_die(&attr
, die_mem
) == NULL
)
174 tag
= dwarf_tag(die_mem
);
176 } while (tag
== DW_TAG_const_type
||
177 tag
== DW_TAG_restrict_type
||
178 tag
== DW_TAG_volatile_type
||
179 tag
== DW_TAG_shared_type
||
180 tag
== DW_TAG_typedef
);
185 static bool die_is_signed_type(Dwarf_Die
*tp_die
)
187 Dwarf_Attribute attr
;
190 if (dwarf_attr(tp_die
, DW_AT_encoding
, &attr
) == NULL
||
191 dwarf_formudata(&attr
, &ret
) != 0)
194 return (ret
== DW_ATE_signed_char
|| ret
== DW_ATE_signed
||
195 ret
== DW_ATE_signed_fixed
);
198 static int die_get_byte_size(Dwarf_Die
*tp_die
)
200 Dwarf_Attribute attr
;
203 if (dwarf_attr(tp_die
, DW_AT_byte_size
, &attr
) == NULL
||
204 dwarf_formudata(&attr
, &ret
) != 0)
210 /* Get data_member_location offset */
211 static int die_get_data_member_location(Dwarf_Die
*mb_die
, Dwarf_Word
*offs
)
213 Dwarf_Attribute attr
;
218 if (dwarf_attr(mb_die
, DW_AT_data_member_location
, &attr
) == NULL
)
221 if (dwarf_formudata(&attr
, offs
) != 0) {
222 /* DW_AT_data_member_location should be DW_OP_plus_uconst */
223 ret
= dwarf_getlocation(&attr
, &expr
, &nexpr
);
224 if (ret
< 0 || nexpr
== 0)
227 if (expr
[0].atom
!= DW_OP_plus_uconst
|| nexpr
!= 1) {
228 pr_debug("Unable to get offset:Unexpected OP %x (%zd)\n",
229 expr
[0].atom
, nexpr
);
232 *offs
= (Dwarf_Word
)expr
[0].number
;
237 /* Return values for die_find callbacks */
239 DIE_FIND_CB_FOUND
= 0, /* End of Search */
240 DIE_FIND_CB_CHILD
= 1, /* Search only children */
241 DIE_FIND_CB_SIBLING
= 2, /* Search only siblings */
242 DIE_FIND_CB_CONTINUE
= 3, /* Search children and siblings */
245 /* Search a child die */
246 static Dwarf_Die
*die_find_child(Dwarf_Die
*rt_die
,
247 int (*callback
)(Dwarf_Die
*, void *),
248 void *data
, Dwarf_Die
*die_mem
)
253 ret
= dwarf_child(rt_die
, die_mem
);
258 ret
= callback(die_mem
, data
);
259 if (ret
== DIE_FIND_CB_FOUND
)
262 if ((ret
& DIE_FIND_CB_CHILD
) &&
263 die_find_child(die_mem
, callback
, data
, &child_die
)) {
264 memcpy(die_mem
, &child_die
, sizeof(Dwarf_Die
));
267 } while ((ret
& DIE_FIND_CB_SIBLING
) &&
268 dwarf_siblingof(die_mem
, die_mem
) == 0);
273 struct __addr_die_search_param
{
278 static int __die_search_func_cb(Dwarf_Die
*fn_die
, void *data
)
280 struct __addr_die_search_param
*ad
= data
;
282 if (dwarf_tag(fn_die
) == DW_TAG_subprogram
&&
283 dwarf_haspc(fn_die
, ad
->addr
)) {
284 memcpy(ad
->die_mem
, fn_die
, sizeof(Dwarf_Die
));
285 return DWARF_CB_ABORT
;
290 /* Search a real subprogram including this line, */
291 static Dwarf_Die
*die_find_real_subprogram(Dwarf_Die
*cu_die
, Dwarf_Addr addr
,
294 struct __addr_die_search_param ad
;
296 ad
.die_mem
= die_mem
;
297 /* dwarf_getscopes can't find subprogram. */
298 if (!dwarf_getfuncs(cu_die
, __die_search_func_cb
, &ad
, 0))
304 /* die_find callback for inline function search */
305 static int __die_find_inline_cb(Dwarf_Die
*die_mem
, void *data
)
307 Dwarf_Addr
*addr
= data
;
309 if (dwarf_tag(die_mem
) == DW_TAG_inlined_subroutine
&&
310 dwarf_haspc(die_mem
, *addr
))
311 return DIE_FIND_CB_FOUND
;
313 return DIE_FIND_CB_CONTINUE
;
316 /* Similar to dwarf_getfuncs, but returns inlined_subroutine if exists. */
317 static Dwarf_Die
*die_find_inlinefunc(Dwarf_Die
*sp_die
, Dwarf_Addr addr
,
320 return die_find_child(sp_die
, __die_find_inline_cb
, &addr
, die_mem
);
323 static int __die_find_variable_cb(Dwarf_Die
*die_mem
, void *data
)
325 const char *name
= data
;
328 tag
= dwarf_tag(die_mem
);
329 if ((tag
== DW_TAG_formal_parameter
||
330 tag
== DW_TAG_variable
) &&
331 die_compare_name(die_mem
, name
))
332 return DIE_FIND_CB_FOUND
;
334 return DIE_FIND_CB_CONTINUE
;
337 /* Find a variable called 'name' */
338 static Dwarf_Die
*die_find_variable(Dwarf_Die
*sp_die
, const char *name
,
341 return die_find_child(sp_die
, __die_find_variable_cb
, (void *)name
,
345 static int __die_find_member_cb(Dwarf_Die
*die_mem
, void *data
)
347 const char *name
= data
;
349 if ((dwarf_tag(die_mem
) == DW_TAG_member
) &&
350 die_compare_name(die_mem
, name
))
351 return DIE_FIND_CB_FOUND
;
353 return DIE_FIND_CB_SIBLING
;
356 /* Find a member called 'name' */
357 static Dwarf_Die
*die_find_member(Dwarf_Die
*st_die
, const char *name
,
360 return die_find_child(st_die
, __die_find_member_cb
, (void *)name
,
365 * Probe finder related functions
368 static struct probe_trace_arg_ref
*alloc_trace_arg_ref(long offs
)
370 struct probe_trace_arg_ref
*ref
;
371 ref
= zalloc(sizeof(struct probe_trace_arg_ref
));
377 /* Show a location */
378 static int convert_variable_location(Dwarf_Die
*vr_die
, struct probe_finder
*pf
)
380 Dwarf_Attribute attr
;
387 struct probe_trace_arg
*tvar
= pf
->tvar
;
390 /* TODO: handle more than 1 exprs */
391 if (dwarf_attr(vr_die
, DW_AT_location
, &attr
) == NULL
||
392 dwarf_getlocation_addr(&attr
, pf
->addr
, &op
, &nops
, 1) <= 0 ||
394 /* TODO: Support const_value */
395 pr_err("Failed to find the location of %s at this address.\n"
396 " Perhaps, it has been optimized out.\n", pf
->pvar
->var
);
400 if (op
->atom
== DW_OP_addr
) {
401 /* Static variables on memory (not stack), make @varname */
402 ret
= strlen(dwarf_diename(vr_die
));
403 tvar
->value
= zalloc(ret
+ 2);
404 if (tvar
->value
== NULL
)
406 snprintf(tvar
->value
, ret
+ 2, "@%s", dwarf_diename(vr_die
));
407 tvar
->ref
= alloc_trace_arg_ref((long)offs
);
408 if (tvar
->ref
== NULL
)
413 /* If this is based on frame buffer, set the offset */
414 if (op
->atom
== DW_OP_fbreg
) {
415 if (pf
->fb_ops
== NULL
) {
416 pr_warning("The attribute of frame base is not "
425 if (op
->atom
>= DW_OP_breg0
&& op
->atom
<= DW_OP_breg31
) {
426 regn
= op
->atom
- DW_OP_breg0
;
429 } else if (op
->atom
>= DW_OP_reg0
&& op
->atom
<= DW_OP_reg31
) {
430 regn
= op
->atom
- DW_OP_reg0
;
431 } else if (op
->atom
== DW_OP_bregx
) {
435 } else if (op
->atom
== DW_OP_regx
) {
438 pr_warning("DW_OP %x is not supported.\n", op
->atom
);
442 regs
= get_arch_regstr(regn
);
444 pr_warning("Mapping for DWARF register number %u missing on this architecture.", regn
);
448 tvar
->value
= strdup(regs
);
449 if (tvar
->value
== NULL
)
453 tvar
->ref
= alloc_trace_arg_ref((long)offs
);
454 if (tvar
->ref
== NULL
)
460 static int convert_variable_type(Dwarf_Die
*vr_die
,
461 struct probe_trace_arg
*tvar
,
464 struct probe_trace_arg_ref
**ref_ptr
= &tvar
->ref
;
469 /* TODO: check all types */
470 if (cast
&& strcmp(cast
, "string") != 0) {
471 /* Non string type is OK */
472 tvar
->type
= strdup(cast
);
473 return (tvar
->type
== NULL
) ? -ENOMEM
: 0;
476 if (die_get_real_type(vr_die
, &type
) == NULL
) {
477 pr_warning("Failed to get a type information of %s.\n",
478 dwarf_diename(vr_die
));
482 pr_debug("%s type is %s.\n",
483 dwarf_diename(vr_die
), dwarf_diename(&type
));
485 if (cast
&& strcmp(cast
, "string") == 0) { /* String type */
486 ret
= dwarf_tag(&type
);
487 if (ret
!= DW_TAG_pointer_type
&&
488 ret
!= DW_TAG_array_type
) {
489 pr_warning("Failed to cast into string: "
490 "%s(%s) is not a pointer nor array.",
491 dwarf_diename(vr_die
), dwarf_diename(&type
));
494 if (ret
== DW_TAG_pointer_type
) {
495 if (die_get_real_type(&type
, &type
) == NULL
) {
496 pr_warning("Failed to get a type information.");
500 ref_ptr
= &(*ref_ptr
)->next
;
501 /* Add new reference with offset +0 */
502 *ref_ptr
= zalloc(sizeof(struct probe_trace_arg_ref
));
503 if (*ref_ptr
== NULL
) {
504 pr_warning("Out of memory error\n");
508 if (!die_compare_name(&type
, "char") &&
509 !die_compare_name(&type
, "unsigned char")) {
510 pr_warning("Failed to cast into string: "
511 "%s is not (unsigned) char *.",
512 dwarf_diename(vr_die
));
515 tvar
->type
= strdup(cast
);
516 return (tvar
->type
== NULL
) ? -ENOMEM
: 0;
519 ret
= die_get_byte_size(&type
) * 8;
521 /* Check the bitwidth */
522 if (ret
> MAX_BASIC_TYPE_BITS
) {
523 pr_info("%s exceeds max-bitwidth."
524 " Cut down to %d bits.\n",
525 dwarf_diename(&type
), MAX_BASIC_TYPE_BITS
);
526 ret
= MAX_BASIC_TYPE_BITS
;
529 ret
= snprintf(buf
, 16, "%c%d",
530 die_is_signed_type(&type
) ? 's' : 'u', ret
);
531 if (ret
< 0 || ret
>= 16) {
534 pr_warning("Failed to convert variable type: %s\n",
538 tvar
->type
= strdup(buf
);
539 if (tvar
->type
== NULL
)
545 static int convert_variable_fields(Dwarf_Die
*vr_die
, const char *varname
,
546 struct perf_probe_arg_field
*field
,
547 struct probe_trace_arg_ref
**ref_ptr
,
550 struct probe_trace_arg_ref
*ref
= *ref_ptr
;
555 pr_debug("converting %s in %s\n", field
->name
, varname
);
556 if (die_get_real_type(vr_die
, &type
) == NULL
) {
557 pr_warning("Failed to get the type of %s.\n", varname
);
560 pr_debug2("Var real type: (%x)\n", (unsigned)dwarf_dieoffset(&type
));
561 tag
= dwarf_tag(&type
);
563 if (field
->name
[0] == '[' &&
564 (tag
== DW_TAG_array_type
|| tag
== DW_TAG_pointer_type
)) {
566 /* Save original type for next field */
567 memcpy(die_mem
, &type
, sizeof(*die_mem
));
568 /* Get the type of this array */
569 if (die_get_real_type(&type
, &type
) == NULL
) {
570 pr_warning("Failed to get the type of %s.\n", varname
);
573 pr_debug2("Array real type: (%x)\n",
574 (unsigned)dwarf_dieoffset(&type
));
575 if (tag
== DW_TAG_pointer_type
) {
576 ref
= zalloc(sizeof(struct probe_trace_arg_ref
));
580 (*ref_ptr
)->next
= ref
;
584 ref
->offset
+= die_get_byte_size(&type
) * field
->index
;
586 /* Save vr_die for converting types */
587 memcpy(die_mem
, vr_die
, sizeof(*die_mem
));
589 } else if (tag
== DW_TAG_pointer_type
) {
590 /* Check the pointer and dereference */
592 pr_err("Semantic error: %s must be referred by '->'\n",
596 /* Get the type pointed by this pointer */
597 if (die_get_real_type(&type
, &type
) == NULL
) {
598 pr_warning("Failed to get the type of %s.\n", varname
);
601 /* Verify it is a data structure */
602 if (dwarf_tag(&type
) != DW_TAG_structure_type
) {
603 pr_warning("%s is not a data structure.\n", varname
);
607 ref
= zalloc(sizeof(struct probe_trace_arg_ref
));
611 (*ref_ptr
)->next
= ref
;
615 /* Verify it is a data structure */
616 if (tag
!= DW_TAG_structure_type
) {
617 pr_warning("%s is not a data structure.\n", varname
);
620 if (field
->name
[0] == '[') {
621 pr_err("Semantic error: %s is not a pointor nor array.",
626 pr_err("Semantic error: %s must be referred by '.'\n",
631 pr_warning("Structure on a register is not "
637 if (die_find_member(&type
, field
->name
, die_mem
) == NULL
) {
638 pr_warning("%s(tyep:%s) has no member %s.\n", varname
,
639 dwarf_diename(&type
), field
->name
);
643 /* Get the offset of the field */
644 ret
= die_get_data_member_location(die_mem
, &offs
);
646 pr_warning("Failed to get the offset of %s.\n", field
->name
);
649 ref
->offset
+= (long)offs
;
652 /* Converting next field */
654 return convert_variable_fields(die_mem
, field
->name
,
655 field
->next
, &ref
, die_mem
);
660 /* Show a variables in kprobe event format */
661 static int convert_variable(Dwarf_Die
*vr_die
, struct probe_finder
*pf
)
666 pr_debug("Converting variable %s into trace event.\n",
667 dwarf_diename(vr_die
));
669 ret
= convert_variable_location(vr_die
, pf
);
670 if (ret
== 0 && pf
->pvar
->field
) {
671 ret
= convert_variable_fields(vr_die
, pf
->pvar
->var
,
672 pf
->pvar
->field
, &pf
->tvar
->ref
,
677 ret
= convert_variable_type(vr_die
, pf
->tvar
, pf
->pvar
->type
);
678 /* *expr will be cached in libdw. Don't free it. */
682 /* Find a variable in a subprogram die */
683 static int find_variable(Dwarf_Die
*sp_die
, struct probe_finder
*pf
)
685 Dwarf_Die vr_die
, *scopes
;
690 pf
->tvar
->name
= strdup(pf
->pvar
->name
);
692 ret
= synthesize_perf_probe_arg(pf
->pvar
, buf
, 32);
695 ptr
= strchr(buf
, ':'); /* Change type separator to _ */
698 pf
->tvar
->name
= strdup(buf
);
700 if (pf
->tvar
->name
== NULL
)
703 if (!is_c_varname(pf
->pvar
->var
)) {
704 /* Copy raw parameters */
705 pf
->tvar
->value
= strdup(pf
->pvar
->var
);
706 if (pf
->tvar
->value
== NULL
)
712 pr_debug("Searching '%s' variable in context.\n",
714 /* Search child die for local variables and parameters. */
715 if (die_find_variable(sp_die
, pf
->pvar
->var
, &vr_die
))
716 ret
= convert_variable(&vr_die
, pf
);
718 /* Search upper class */
719 nscopes
= dwarf_getscopes_die(sp_die
, &scopes
);
721 ret
= dwarf_getscopevar(scopes
, nscopes
, pf
->pvar
->var
,
722 0, NULL
, 0, 0, &vr_die
);
724 ret
= convert_variable(&vr_die
, pf
);
732 pr_warning("Failed to find '%s' in this function.\n",
737 /* Show a probe point to output buffer */
738 static int convert_probe_point(Dwarf_Die
*sp_die
, struct probe_finder
*pf
)
740 struct probe_trace_event
*tev
;
745 Dwarf_Attribute fb_attr
;
748 if (pf
->ntevs
== pf
->max_tevs
) {
749 pr_warning("Too many( > %d) probe point found.\n",
753 tev
= &pf
->tevs
[pf
->ntevs
++];
755 /* If no real subprogram, find a real one */
756 if (!sp_die
|| dwarf_tag(sp_die
) != DW_TAG_subprogram
) {
757 sp_die
= die_find_real_subprogram(&pf
->cu_die
,
760 pr_warning("Failed to find probe point in any "
766 /* Copy the name of probe point */
767 name
= dwarf_diename(sp_die
);
769 if (dwarf_entrypc(sp_die
, &eaddr
) != 0) {
770 pr_warning("Failed to get entry pc of %s\n",
771 dwarf_diename(sp_die
));
774 tev
->point
.symbol
= strdup(name
);
775 if (tev
->point
.symbol
== NULL
)
777 tev
->point
.offset
= (unsigned long)(pf
->addr
- eaddr
);
779 /* This function has no name. */
780 tev
->point
.offset
= (unsigned long)pf
->addr
;
782 pr_debug("Probe point found: %s+%lu\n", tev
->point
.symbol
,
785 /* Get the frame base attribute/ops */
786 dwarf_attr(sp_die
, DW_AT_frame_base
, &fb_attr
);
787 ret
= dwarf_getlocation_addr(&fb_attr
, pf
->addr
, &pf
->fb_ops
, &nops
, 1);
788 if (ret
<= 0 || nops
== 0) {
790 #if _ELFUTILS_PREREQ(0, 142)
791 } else if (nops
== 1 && pf
->fb_ops
[0].atom
== DW_OP_call_frame_cfa
&&
794 if (dwarf_cfi_addrframe(pf
->cfi
, pf
->addr
, &frame
) != 0 ||
795 dwarf_frame_cfa(frame
, &pf
->fb_ops
, &nops
) != 0) {
796 pr_warning("Failed to get CFA on 0x%jx\n",
797 (uintmax_t)pf
->addr
);
803 /* Find each argument */
804 tev
->nargs
= pf
->pev
->nargs
;
805 tev
->args
= zalloc(sizeof(struct probe_trace_arg
) * tev
->nargs
);
806 if (tev
->args
== NULL
)
808 for (i
= 0; i
< pf
->pev
->nargs
; i
++) {
809 pf
->pvar
= &pf
->pev
->args
[i
];
810 pf
->tvar
= &tev
->args
[i
];
811 ret
= find_variable(sp_die
, pf
);
816 /* *pf->fb_ops will be cached in libdw. Don't free it. */
821 /* Find probe point from its line number */
822 static int find_probe_point_by_line(struct probe_finder
*pf
)
831 if (dwarf_getsrclines(&pf
->cu_die
, &lines
, &nlines
) != 0) {
832 pr_warning("No source lines found in this CU.\n");
836 for (i
= 0; i
< nlines
&& ret
== 0; i
++) {
837 line
= dwarf_onesrcline(lines
, i
);
838 if (dwarf_lineno(line
, &lineno
) != 0 ||
842 /* TODO: Get fileno from line, but how? */
843 if (strtailcmp(dwarf_linesrc(line
, NULL
, NULL
), pf
->fname
) != 0)
846 if (dwarf_lineaddr(line
, &addr
) != 0) {
847 pr_warning("Failed to get the address of the line.\n");
850 pr_debug("Probe line found: line[%d]:%d addr:0x%jx\n",
851 (int)i
, lineno
, (uintmax_t)addr
);
854 ret
= convert_probe_point(NULL
, pf
);
855 /* Continuing, because target line might be inlined. */
860 /* Find lines which match lazy pattern */
861 static int find_lazy_match_lines(struct list_head
*head
,
862 const char *fname
, const char *pat
)
864 char *fbuf
, *p1
, *p2
;
865 int fd
, line
, nlines
= -1;
868 fd
= open(fname
, O_RDONLY
);
870 pr_warning("Failed to open %s: %s\n", fname
, strerror(-fd
));
874 if (fstat(fd
, &st
) < 0) {
875 pr_warning("Failed to get the size of %s: %s\n",
876 fname
, strerror(errno
));
882 fbuf
= malloc(st
.st_size
+ 2);
885 if (read(fd
, fbuf
, st
.st_size
) < 0) {
886 pr_warning("Failed to read %s: %s\n", fname
, strerror(errno
));
890 fbuf
[st
.st_size
] = '\n'; /* Dummy line */
891 fbuf
[st
.st_size
+ 1] = '\0';
895 while ((p2
= strchr(p1
, '\n')) != NULL
) {
897 if (strlazymatch(p1
, pat
)) {
898 line_list__add_line(head
, line
);
911 /* Find probe points from lazy pattern */
912 static int find_probe_point_lazy(Dwarf_Die
*sp_die
, struct probe_finder
*pf
)
922 if (list_empty(&pf
->lcache
)) {
923 /* Matching lazy line pattern */
924 ret
= find_lazy_match_lines(&pf
->lcache
, pf
->fname
,
925 pf
->pev
->point
.lazy_line
);
927 pr_debug("No matched lines found in %s.\n", pf
->fname
);
933 if (dwarf_getsrclines(&pf
->cu_die
, &lines
, &nlines
) != 0) {
934 pr_warning("No source lines found in this CU.\n");
938 for (i
= 0; i
< nlines
&& ret
>= 0; i
++) {
939 line
= dwarf_onesrcline(lines
, i
);
941 if (dwarf_lineno(line
, &lineno
) != 0 ||
942 !line_list__has_line(&pf
->lcache
, lineno
))
945 /* TODO: Get fileno from line, but how? */
946 if (strtailcmp(dwarf_linesrc(line
, NULL
, NULL
), pf
->fname
) != 0)
949 if (dwarf_lineaddr(line
, &addr
) != 0) {
950 pr_debug("Failed to get the address of line %d.\n",
955 /* Address filtering 1: does sp_die include addr? */
956 if (!dwarf_haspc(sp_die
, addr
))
958 /* Address filtering 2: No child include addr? */
959 if (die_find_inlinefunc(sp_die
, addr
, &die_mem
))
963 pr_debug("Probe line found: line[%d]:%d addr:0x%llx\n",
964 (int)i
, lineno
, (unsigned long long)addr
);
967 ret
= convert_probe_point(sp_die
, pf
);
968 /* Continuing, because target line might be inlined. */
970 /* TODO: deallocate lines, but how? */
974 /* Callback parameter with return value */
975 struct dwarf_callback_param
{
980 static int probe_point_inline_cb(Dwarf_Die
*in_die
, void *data
)
982 struct dwarf_callback_param
*param
= data
;
983 struct probe_finder
*pf
= param
->data
;
984 struct perf_probe_point
*pp
= &pf
->pev
->point
;
988 param
->retval
= find_probe_point_lazy(in_die
, pf
);
990 /* Get probe address */
991 if (dwarf_entrypc(in_die
, &addr
) != 0) {
992 pr_warning("Failed to get entry pc of %s.\n",
993 dwarf_diename(in_die
));
994 param
->retval
= -ENOENT
;
995 return DWARF_CB_ABORT
;
998 pf
->addr
+= pp
->offset
;
999 pr_debug("found inline addr: 0x%jx\n",
1000 (uintmax_t)pf
->addr
);
1002 param
->retval
= convert_probe_point(in_die
, pf
);
1003 if (param
->retval
< 0)
1004 return DWARF_CB_ABORT
;
1010 /* Search function from function name */
1011 static int probe_point_search_cb(Dwarf_Die
*sp_die
, void *data
)
1013 struct dwarf_callback_param
*param
= data
;
1014 struct probe_finder
*pf
= param
->data
;
1015 struct perf_probe_point
*pp
= &pf
->pev
->point
;
1017 /* Check tag and diename */
1018 if (dwarf_tag(sp_die
) != DW_TAG_subprogram
||
1019 !die_compare_name(sp_die
, pp
->function
))
1022 pf
->fname
= dwarf_decl_file(sp_die
);
1023 if (pp
->line
) { /* Function relative line */
1024 dwarf_decl_line(sp_die
, &pf
->lno
);
1025 pf
->lno
+= pp
->line
;
1026 param
->retval
= find_probe_point_by_line(pf
);
1027 } else if (!dwarf_func_inline(sp_die
)) {
1030 param
->retval
= find_probe_point_lazy(sp_die
, pf
);
1032 if (dwarf_entrypc(sp_die
, &pf
->addr
) != 0) {
1033 pr_warning("Failed to get entry pc of %s.\n",
1034 dwarf_diename(sp_die
));
1035 param
->retval
= -ENOENT
;
1036 return DWARF_CB_ABORT
;
1038 pf
->addr
+= pp
->offset
;
1039 /* TODO: Check the address in this function */
1040 param
->retval
= convert_probe_point(sp_die
, pf
);
1043 struct dwarf_callback_param _param
= {.data
= (void *)pf
,
1045 /* Inlined function: search instances */
1046 dwarf_func_inline_instances(sp_die
, probe_point_inline_cb
,
1048 param
->retval
= _param
.retval
;
1051 return DWARF_CB_ABORT
; /* Exit; no same symbol in this CU. */
1054 static int find_probe_point_by_func(struct probe_finder
*pf
)
1056 struct dwarf_callback_param _param
= {.data
= (void *)pf
,
1058 dwarf_getfuncs(&pf
->cu_die
, probe_point_search_cb
, &_param
, 0);
1059 return _param
.retval
;
1062 /* Find probe_trace_events specified by perf_probe_event from debuginfo */
1063 int find_probe_trace_events(int fd
, struct perf_probe_event
*pev
,
1064 struct probe_trace_event
**tevs
, int max_tevs
)
1066 struct probe_finder pf
= {.pev
= pev
, .max_tevs
= max_tevs
};
1067 struct perf_probe_point
*pp
= &pev
->point
;
1068 Dwarf_Off off
, noff
;
1074 pf
.tevs
= zalloc(sizeof(struct probe_trace_event
) * max_tevs
);
1075 if (pf
.tevs
== NULL
)
1080 dbg
= dwarf_begin(fd
, DWARF_C_READ
);
1082 pr_warning("No dwarf info found in the vmlinux - "
1083 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
1089 #if _ELFUTILS_PREREQ(0, 142)
1090 /* Get the call frame information from this dwarf */
1091 pf
.cfi
= dwarf_getcfi(dbg
);
1095 line_list__init(&pf
.lcache
);
1096 /* Loop on CUs (Compilation Unit) */
1097 while (!dwarf_nextcu(dbg
, off
, &noff
, &cuhl
, NULL
, NULL
, NULL
) &&
1099 /* Get the DIE(Debugging Information Entry) of this CU */
1100 diep
= dwarf_offdie(dbg
, off
+ cuhl
, &pf
.cu_die
);
1104 /* Check if target file is included. */
1106 pf
.fname
= cu_find_realpath(&pf
.cu_die
, pp
->file
);
1110 if (!pp
->file
|| pf
.fname
) {
1112 ret
= find_probe_point_by_func(&pf
);
1113 else if (pp
->lazy_line
)
1114 ret
= find_probe_point_lazy(NULL
, &pf
);
1117 ret
= find_probe_point_by_line(&pf
);
1122 line_list__free(&pf
.lcache
);
1125 return (ret
< 0) ? ret
: pf
.ntevs
;
1128 /* Reverse search */
1129 int find_perf_probe_point(int fd
, unsigned long addr
,
1130 struct perf_probe_point
*ppt
)
1132 Dwarf_Die cudie
, spdie
, indie
;
1135 Dwarf_Addr laddr
, eaddr
;
1137 int lineno
, ret
= 0;
1140 dbg
= dwarf_begin(fd
, DWARF_C_READ
);
1145 if (!dwarf_addrdie(dbg
, (Dwarf_Addr
)addr
, &cudie
)) {
1150 /* Find a corresponding line */
1151 line
= dwarf_getsrc_die(&cudie
, (Dwarf_Addr
)addr
);
1153 if (dwarf_lineaddr(line
, &laddr
) == 0 &&
1154 (Dwarf_Addr
)addr
== laddr
&&
1155 dwarf_lineno(line
, &lineno
) == 0) {
1156 tmp
= dwarf_linesrc(line
, NULL
, NULL
);
1159 ppt
->file
= strdup(tmp
);
1160 if (ppt
->file
== NULL
) {
1169 /* Find a corresponding function */
1170 if (die_find_real_subprogram(&cudie
, (Dwarf_Addr
)addr
, &spdie
)) {
1171 tmp
= dwarf_diename(&spdie
);
1172 if (!tmp
|| dwarf_entrypc(&spdie
, &eaddr
) != 0)
1176 if (die_find_inlinefunc(&spdie
, (Dwarf_Addr
)addr
,
1178 /* addr in an inline function */
1179 tmp
= dwarf_diename(&indie
);
1182 ret
= dwarf_decl_line(&indie
, &lineno
);
1184 if (eaddr
== addr
) { /* Function entry */
1188 ret
= dwarf_decl_line(&spdie
, &lineno
);
1191 /* Make a relative line number */
1192 ppt
->line
-= lineno
;
1196 /* We don't have a line number, let's use offset */
1197 ppt
->offset
= addr
- (unsigned long)eaddr
;
1199 ppt
->function
= strdup(tmp
);
1200 if (ppt
->function
== NULL
) {
1210 ret
= found
? 1 : 0;
1214 /* Add a line and store the src path */
1215 static int line_range_add_line(const char *src
, unsigned int lineno
,
1216 struct line_range
*lr
)
1218 /* Copy source path */
1220 lr
->path
= strdup(src
);
1221 if (lr
->path
== NULL
)
1224 return line_list__add_line(&lr
->line_list
, lineno
);
1227 /* Search function declaration lines */
1228 static int line_range_funcdecl_cb(Dwarf_Die
*sp_die
, void *data
)
1230 struct dwarf_callback_param
*param
= data
;
1231 struct line_finder
*lf
= param
->data
;
1235 src
= dwarf_decl_file(sp_die
);
1236 if (src
&& strtailcmp(src
, lf
->fname
) != 0)
1239 if (dwarf_decl_line(sp_die
, &lineno
) != 0 ||
1240 (lf
->lno_s
> lineno
|| lf
->lno_e
< lineno
))
1243 param
->retval
= line_range_add_line(src
, lineno
, lf
->lr
);
1244 if (param
->retval
< 0)
1245 return DWARF_CB_ABORT
;
1249 static int find_line_range_func_decl_lines(struct line_finder
*lf
)
1251 struct dwarf_callback_param param
= {.data
= (void *)lf
, .retval
= 0};
1252 dwarf_getfuncs(&lf
->cu_die
, line_range_funcdecl_cb
, ¶m
, 0);
1253 return param
.retval
;
1256 /* Find line range from its line number */
1257 static int find_line_range_by_line(Dwarf_Die
*sp_die
, struct line_finder
*lf
)
1263 int lineno
, ret
= 0;
1267 line_list__init(&lf
->lr
->line_list
);
1268 if (dwarf_getsrclines(&lf
->cu_die
, &lines
, &nlines
) != 0) {
1269 pr_warning("No source lines found in this CU.\n");
1273 /* Search probable lines on lines list */
1274 for (i
= 0; i
< nlines
; i
++) {
1275 line
= dwarf_onesrcline(lines
, i
);
1276 if (dwarf_lineno(line
, &lineno
) != 0 ||
1277 (lf
->lno_s
> lineno
|| lf
->lno_e
< lineno
))
1281 /* Address filtering 1: does sp_die include addr? */
1282 if (dwarf_lineaddr(line
, &addr
) != 0 ||
1283 !dwarf_haspc(sp_die
, addr
))
1286 /* Address filtering 2: No child include addr? */
1287 if (die_find_inlinefunc(sp_die
, addr
, &die_mem
))
1291 /* TODO: Get fileno from line, but how? */
1292 src
= dwarf_linesrc(line
, NULL
, NULL
);
1293 if (strtailcmp(src
, lf
->fname
) != 0)
1296 ret
= line_range_add_line(src
, lineno
, lf
->lr
);
1302 * Dwarf lines doesn't include function declarations. We have to
1303 * check functions list or given function.
1306 src
= dwarf_decl_file(sp_die
);
1307 if (src
&& dwarf_decl_line(sp_die
, &lineno
) == 0 &&
1308 (lf
->lno_s
<= lineno
&& lf
->lno_e
>= lineno
))
1309 ret
= line_range_add_line(src
, lineno
, lf
->lr
);
1311 ret
= find_line_range_func_decl_lines(lf
);
1315 if (!list_empty(&lf
->lr
->line_list
))
1316 ret
= lf
->found
= 1;
1318 ret
= 0; /* Lines are not found */
1321 lf
->lr
->path
= NULL
;
1326 static int line_range_inline_cb(Dwarf_Die
*in_die
, void *data
)
1328 struct dwarf_callback_param
*param
= data
;
1330 param
->retval
= find_line_range_by_line(in_die
, param
->data
);
1331 return DWARF_CB_ABORT
; /* No need to find other instances */
1334 /* Search function from function name */
1335 static int line_range_search_cb(Dwarf_Die
*sp_die
, void *data
)
1337 struct dwarf_callback_param
*param
= data
;
1338 struct line_finder
*lf
= param
->data
;
1339 struct line_range
*lr
= lf
->lr
;
1341 if (dwarf_tag(sp_die
) == DW_TAG_subprogram
&&
1342 die_compare_name(sp_die
, lr
->function
)) {
1343 lf
->fname
= dwarf_decl_file(sp_die
);
1344 dwarf_decl_line(sp_die
, &lr
->offset
);
1345 pr_debug("fname: %s, lineno:%d\n", lf
->fname
, lr
->offset
);
1346 lf
->lno_s
= lr
->offset
+ lr
->start
;
1347 if (lf
->lno_s
< 0) /* Overflow */
1348 lf
->lno_s
= INT_MAX
;
1349 lf
->lno_e
= lr
->offset
+ lr
->end
;
1350 if (lf
->lno_e
< 0) /* Overflow */
1351 lf
->lno_e
= INT_MAX
;
1352 pr_debug("New line range: %d to %d\n", lf
->lno_s
, lf
->lno_e
);
1353 lr
->start
= lf
->lno_s
;
1354 lr
->end
= lf
->lno_e
;
1355 if (dwarf_func_inline(sp_die
)) {
1356 struct dwarf_callback_param _param
;
1357 _param
.data
= (void *)lf
;
1359 dwarf_func_inline_instances(sp_die
,
1360 line_range_inline_cb
,
1362 param
->retval
= _param
.retval
;
1364 param
->retval
= find_line_range_by_line(sp_die
, lf
);
1365 return DWARF_CB_ABORT
;
1370 static int find_line_range_by_func(struct line_finder
*lf
)
1372 struct dwarf_callback_param param
= {.data
= (void *)lf
, .retval
= 0};
1373 dwarf_getfuncs(&lf
->cu_die
, line_range_search_cb
, ¶m
, 0);
1374 return param
.retval
;
1377 int find_line_range(int fd
, struct line_range
*lr
)
1379 struct line_finder lf
= {.lr
= lr
, .found
= 0};
1381 Dwarf_Off off
= 0, noff
;
1385 const char *comp_dir
;
1387 dbg
= dwarf_begin(fd
, DWARF_C_READ
);
1389 pr_warning("No dwarf info found in the vmlinux - "
1390 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
1394 /* Loop on CUs (Compilation Unit) */
1395 while (!lf
.found
&& ret
>= 0) {
1396 if (dwarf_nextcu(dbg
, off
, &noff
, &cuhl
, NULL
, NULL
, NULL
) != 0)
1399 /* Get the DIE(Debugging Information Entry) of this CU */
1400 diep
= dwarf_offdie(dbg
, off
+ cuhl
, &lf
.cu_die
);
1404 /* Check if target file is included. */
1406 lf
.fname
= cu_find_realpath(&lf
.cu_die
, lr
->file
);
1410 if (!lr
->file
|| lf
.fname
) {
1412 ret
= find_line_range_by_func(&lf
);
1414 lf
.lno_s
= lr
->start
;
1416 ret
= find_line_range_by_line(NULL
, &lf
);
1422 /* Store comp_dir */
1424 comp_dir
= cu_get_comp_dir(&lf
.cu_die
);
1426 lr
->comp_dir
= strdup(comp_dir
);
1432 pr_debug("path: %s\n", lr
->path
);
1435 return (ret
< 0) ? ret
: lf
.found
;