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>
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
);
122 /* Find the realpath of the target file. */
123 static const char *cu_find_realpath(Dwarf_Die
*cu_die
, const char *fname
)
127 const char *src
= NULL
;
133 ret
= dwarf_getsrcfiles(cu_die
, &files
, &nfiles
);
137 for (i
= 0; i
< nfiles
; i
++) {
138 src
= dwarf_filesrc(files
, i
, NULL
, NULL
);
139 if (strtailcmp(src
, fname
) == 0)
147 /* Get DW_AT_comp_dir (should be NULL with older gcc) */
148 static const char *cu_get_comp_dir(Dwarf_Die
*cu_die
)
150 Dwarf_Attribute attr
;
151 if (dwarf_attr(cu_die
, DW_AT_comp_dir
, &attr
) == NULL
)
153 return dwarf_formstring(&attr
);
156 /* Compare diename and tname */
157 static bool die_compare_name(Dwarf_Die
*dw_die
, const char *tname
)
160 name
= dwarf_diename(dw_die
);
161 return name
? (strcmp(tname
, name
) == 0) : false;
164 /* Get type die, but skip qualifiers and typedef */
165 static Dwarf_Die
*die_get_real_type(Dwarf_Die
*vr_die
, Dwarf_Die
*die_mem
)
167 Dwarf_Attribute attr
;
171 if (dwarf_attr(vr_die
, DW_AT_type
, &attr
) == NULL
||
172 dwarf_formref_die(&attr
, die_mem
) == NULL
)
175 tag
= dwarf_tag(die_mem
);
177 } while (tag
== DW_TAG_const_type
||
178 tag
== DW_TAG_restrict_type
||
179 tag
== DW_TAG_volatile_type
||
180 tag
== DW_TAG_shared_type
||
181 tag
== DW_TAG_typedef
);
186 static bool die_is_signed_type(Dwarf_Die
*tp_die
)
188 Dwarf_Attribute attr
;
191 if (dwarf_attr(tp_die
, DW_AT_encoding
, &attr
) == NULL
||
192 dwarf_formudata(&attr
, &ret
) != 0)
195 return (ret
== DW_ATE_signed_char
|| ret
== DW_ATE_signed
||
196 ret
== DW_ATE_signed_fixed
);
199 static int die_get_byte_size(Dwarf_Die
*tp_die
)
201 Dwarf_Attribute attr
;
204 if (dwarf_attr(tp_die
, DW_AT_byte_size
, &attr
) == NULL
||
205 dwarf_formudata(&attr
, &ret
) != 0)
211 /* Get data_member_location offset */
212 static int die_get_data_member_location(Dwarf_Die
*mb_die
, Dwarf_Word
*offs
)
214 Dwarf_Attribute attr
;
219 if (dwarf_attr(mb_die
, DW_AT_data_member_location
, &attr
) == NULL
)
222 if (dwarf_formudata(&attr
, offs
) != 0) {
223 /* DW_AT_data_member_location should be DW_OP_plus_uconst */
224 ret
= dwarf_getlocation(&attr
, &expr
, &nexpr
);
225 if (ret
< 0 || nexpr
== 0)
228 if (expr
[0].atom
!= DW_OP_plus_uconst
|| nexpr
!= 1) {
229 pr_debug("Unable to get offset:Unexpected OP %x (%zd)\n",
230 expr
[0].atom
, nexpr
);
233 *offs
= (Dwarf_Word
)expr
[0].number
;
238 /* Return values for die_find callbacks */
240 DIE_FIND_CB_FOUND
= 0, /* End of Search */
241 DIE_FIND_CB_CHILD
= 1, /* Search only children */
242 DIE_FIND_CB_SIBLING
= 2, /* Search only siblings */
243 DIE_FIND_CB_CONTINUE
= 3, /* Search children and siblings */
246 /* Search a child die */
247 static Dwarf_Die
*die_find_child(Dwarf_Die
*rt_die
,
248 int (*callback
)(Dwarf_Die
*, void *),
249 void *data
, Dwarf_Die
*die_mem
)
254 ret
= dwarf_child(rt_die
, die_mem
);
259 ret
= callback(die_mem
, data
);
260 if (ret
== DIE_FIND_CB_FOUND
)
263 if ((ret
& DIE_FIND_CB_CHILD
) &&
264 die_find_child(die_mem
, callback
, data
, &child_die
)) {
265 memcpy(die_mem
, &child_die
, sizeof(Dwarf_Die
));
268 } while ((ret
& DIE_FIND_CB_SIBLING
) &&
269 dwarf_siblingof(die_mem
, die_mem
) == 0);
274 struct __addr_die_search_param
{
279 static int __die_search_func_cb(Dwarf_Die
*fn_die
, void *data
)
281 struct __addr_die_search_param
*ad
= data
;
283 if (dwarf_tag(fn_die
) == DW_TAG_subprogram
&&
284 dwarf_haspc(fn_die
, ad
->addr
)) {
285 memcpy(ad
->die_mem
, fn_die
, sizeof(Dwarf_Die
));
286 return DWARF_CB_ABORT
;
291 /* Search a real subprogram including this line, */
292 static Dwarf_Die
*die_find_real_subprogram(Dwarf_Die
*cu_die
, Dwarf_Addr addr
,
295 struct __addr_die_search_param ad
;
297 ad
.die_mem
= die_mem
;
298 /* dwarf_getscopes can't find subprogram. */
299 if (!dwarf_getfuncs(cu_die
, __die_search_func_cb
, &ad
, 0))
305 /* die_find callback for inline function search */
306 static int __die_find_inline_cb(Dwarf_Die
*die_mem
, void *data
)
308 Dwarf_Addr
*addr
= data
;
310 if (dwarf_tag(die_mem
) == DW_TAG_inlined_subroutine
&&
311 dwarf_haspc(die_mem
, *addr
))
312 return DIE_FIND_CB_FOUND
;
314 return DIE_FIND_CB_CONTINUE
;
317 /* Similar to dwarf_getfuncs, but returns inlined_subroutine if exists. */
318 static Dwarf_Die
*die_find_inlinefunc(Dwarf_Die
*sp_die
, Dwarf_Addr addr
,
321 return die_find_child(sp_die
, __die_find_inline_cb
, &addr
, die_mem
);
324 static int __die_find_variable_cb(Dwarf_Die
*die_mem
, void *data
)
326 const char *name
= data
;
329 tag
= dwarf_tag(die_mem
);
330 if ((tag
== DW_TAG_formal_parameter
||
331 tag
== DW_TAG_variable
) &&
332 die_compare_name(die_mem
, name
))
333 return DIE_FIND_CB_FOUND
;
335 return DIE_FIND_CB_CONTINUE
;
338 /* Find a variable called 'name' */
339 static Dwarf_Die
*die_find_variable(Dwarf_Die
*sp_die
, const char *name
,
342 return die_find_child(sp_die
, __die_find_variable_cb
, (void *)name
,
346 static int __die_find_member_cb(Dwarf_Die
*die_mem
, void *data
)
348 const char *name
= data
;
350 if ((dwarf_tag(die_mem
) == DW_TAG_member
) &&
351 die_compare_name(die_mem
, name
))
352 return DIE_FIND_CB_FOUND
;
354 return DIE_FIND_CB_SIBLING
;
357 /* Find a member called 'name' */
358 static Dwarf_Die
*die_find_member(Dwarf_Die
*st_die
, const char *name
,
361 return die_find_child(st_die
, __die_find_member_cb
, (void *)name
,
366 * Probe finder related functions
369 static struct kprobe_trace_arg_ref
*alloc_trace_arg_ref(long offs
)
371 struct kprobe_trace_arg_ref
*ref
;
372 ref
= zalloc(sizeof(struct kprobe_trace_arg_ref
));
378 /* Show a location */
379 static int convert_variable_location(Dwarf_Die
*vr_die
, struct probe_finder
*pf
)
381 Dwarf_Attribute attr
;
388 struct kprobe_trace_arg
*tvar
= pf
->tvar
;
391 /* TODO: handle more than 1 exprs */
392 if (dwarf_attr(vr_die
, DW_AT_location
, &attr
) == NULL
||
393 dwarf_getlocation_addr(&attr
, pf
->addr
, &op
, &nops
, 1) <= 0 ||
395 /* TODO: Support const_value */
396 pr_err("Failed to find the location of %s at this address.\n"
397 " Perhaps, it has been optimized out.\n", pf
->pvar
->var
);
401 if (op
->atom
== DW_OP_addr
) {
402 /* Static variables on memory (not stack), make @varname */
403 ret
= strlen(dwarf_diename(vr_die
));
404 tvar
->value
= zalloc(ret
+ 2);
405 if (tvar
->value
== NULL
)
407 snprintf(tvar
->value
, ret
+ 2, "@%s", dwarf_diename(vr_die
));
408 tvar
->ref
= alloc_trace_arg_ref((long)offs
);
409 if (tvar
->ref
== NULL
)
414 /* If this is based on frame buffer, set the offset */
415 if (op
->atom
== DW_OP_fbreg
) {
416 if (pf
->fb_ops
== NULL
) {
417 pr_warning("The attribute of frame base is not "
426 if (op
->atom
>= DW_OP_breg0
&& op
->atom
<= DW_OP_breg31
) {
427 regn
= op
->atom
- DW_OP_breg0
;
430 } else if (op
->atom
>= DW_OP_reg0
&& op
->atom
<= DW_OP_reg31
) {
431 regn
= op
->atom
- DW_OP_reg0
;
432 } else if (op
->atom
== DW_OP_bregx
) {
436 } else if (op
->atom
== DW_OP_regx
) {
439 pr_warning("DW_OP %x is not supported.\n", op
->atom
);
443 regs
= get_arch_regstr(regn
);
445 pr_warning("Mapping for DWARF register number %u missing on this architecture.", regn
);
449 tvar
->value
= strdup(regs
);
450 if (tvar
->value
== NULL
)
454 tvar
->ref
= alloc_trace_arg_ref((long)offs
);
455 if (tvar
->ref
== NULL
)
461 static int convert_variable_type(Dwarf_Die
*vr_die
,
462 struct kprobe_trace_arg
*tvar
,
465 struct kprobe_trace_arg_ref
**ref_ptr
= &tvar
->ref
;
470 /* TODO: check all types */
471 if (cast
&& strcmp(cast
, "string") != 0) {
472 /* Non string type is OK */
473 tvar
->type
= strdup(cast
);
474 return (tvar
->type
== NULL
) ? -ENOMEM
: 0;
477 if (die_get_real_type(vr_die
, &type
) == NULL
) {
478 pr_warning("Failed to get a type information of %s.\n",
479 dwarf_diename(vr_die
));
483 pr_debug("%s type is %s.\n",
484 dwarf_diename(vr_die
), dwarf_diename(&type
));
486 if (cast
&& strcmp(cast
, "string") == 0) { /* String type */
487 ret
= dwarf_tag(&type
);
488 if (ret
!= DW_TAG_pointer_type
&&
489 ret
!= DW_TAG_array_type
) {
490 pr_warning("Failed to cast into string: "
491 "%s(%s) is not a pointer nor array.",
492 dwarf_diename(vr_die
), dwarf_diename(&type
));
495 if (ret
== DW_TAG_pointer_type
) {
496 if (die_get_real_type(&type
, &type
) == NULL
) {
497 pr_warning("Failed to get a type information.");
501 ref_ptr
= &(*ref_ptr
)->next
;
502 /* Add new reference with offset +0 */
503 *ref_ptr
= zalloc(sizeof(struct kprobe_trace_arg_ref
));
504 if (*ref_ptr
== NULL
) {
505 pr_warning("Out of memory error\n");
509 if (!die_compare_name(&type
, "char") &&
510 !die_compare_name(&type
, "unsigned char")) {
511 pr_warning("Failed to cast into string: "
512 "%s is not (unsigned) char *.",
513 dwarf_diename(vr_die
));
516 tvar
->type
= strdup(cast
);
517 return (tvar
->type
== NULL
) ? -ENOMEM
: 0;
520 ret
= die_get_byte_size(&type
) * 8;
522 /* Check the bitwidth */
523 if (ret
> MAX_BASIC_TYPE_BITS
) {
524 pr_info("%s exceeds max-bitwidth."
525 " Cut down to %d bits.\n",
526 dwarf_diename(&type
), MAX_BASIC_TYPE_BITS
);
527 ret
= MAX_BASIC_TYPE_BITS
;
530 ret
= snprintf(buf
, 16, "%c%d",
531 die_is_signed_type(&type
) ? 's' : 'u', ret
);
532 if (ret
< 0 || ret
>= 16) {
535 pr_warning("Failed to convert variable type: %s\n",
539 tvar
->type
= strdup(buf
);
540 if (tvar
->type
== NULL
)
546 static int convert_variable_fields(Dwarf_Die
*vr_die
, const char *varname
,
547 struct perf_probe_arg_field
*field
,
548 struct kprobe_trace_arg_ref
**ref_ptr
,
551 struct kprobe_trace_arg_ref
*ref
= *ref_ptr
;
556 pr_debug("converting %s in %s\n", field
->name
, varname
);
557 if (die_get_real_type(vr_die
, &type
) == NULL
) {
558 pr_warning("Failed to get the type of %s.\n", varname
);
561 pr_debug2("Var real type: (%x)\n", (unsigned)dwarf_dieoffset(&type
));
562 tag
= dwarf_tag(&type
);
564 if (field
->name
[0] == '[' &&
565 (tag
== DW_TAG_array_type
|| tag
== DW_TAG_pointer_type
)) {
567 /* Save original type for next field */
568 memcpy(die_mem
, &type
, sizeof(*die_mem
));
569 /* Get the type of this array */
570 if (die_get_real_type(&type
, &type
) == NULL
) {
571 pr_warning("Failed to get the type of %s.\n", varname
);
574 pr_debug2("Array real type: (%x)\n",
575 (unsigned)dwarf_dieoffset(&type
));
576 if (tag
== DW_TAG_pointer_type
) {
577 ref
= zalloc(sizeof(struct kprobe_trace_arg_ref
));
581 (*ref_ptr
)->next
= ref
;
585 ref
->offset
+= die_get_byte_size(&type
) * field
->index
;
587 /* Save vr_die for converting types */
588 memcpy(die_mem
, vr_die
, sizeof(*die_mem
));
590 } else if (tag
== DW_TAG_pointer_type
) {
591 /* Check the pointer and dereference */
593 pr_err("Semantic error: %s must be referred by '->'\n",
597 /* Get the type pointed by this pointer */
598 if (die_get_real_type(&type
, &type
) == NULL
) {
599 pr_warning("Failed to get the type of %s.\n", varname
);
602 /* Verify it is a data structure */
603 if (dwarf_tag(&type
) != DW_TAG_structure_type
) {
604 pr_warning("%s is not a data structure.\n", varname
);
608 ref
= zalloc(sizeof(struct kprobe_trace_arg_ref
));
612 (*ref_ptr
)->next
= ref
;
616 /* Verify it is a data structure */
617 if (tag
!= DW_TAG_structure_type
) {
618 pr_warning("%s is not a data structure.\n", varname
);
621 if (field
->name
[0] == '[') {
622 pr_err("Semantic error: %s is not a pointor nor array.",
627 pr_err("Semantic error: %s must be referred by '.'\n",
632 pr_warning("Structure on a register is not "
638 if (die_find_member(&type
, field
->name
, die_mem
) == NULL
) {
639 pr_warning("%s(tyep:%s) has no member %s.\n", varname
,
640 dwarf_diename(&type
), field
->name
);
644 /* Get the offset of the field */
645 ret
= die_get_data_member_location(die_mem
, &offs
);
647 pr_warning("Failed to get the offset of %s.\n", field
->name
);
650 ref
->offset
+= (long)offs
;
653 /* Converting next field */
655 return convert_variable_fields(die_mem
, field
->name
,
656 field
->next
, &ref
, die_mem
);
661 /* Show a variables in kprobe event format */
662 static int convert_variable(Dwarf_Die
*vr_die
, struct probe_finder
*pf
)
667 pr_debug("Converting variable %s into trace event.\n",
668 dwarf_diename(vr_die
));
670 ret
= convert_variable_location(vr_die
, pf
);
671 if (ret
== 0 && pf
->pvar
->field
) {
672 ret
= convert_variable_fields(vr_die
, pf
->pvar
->var
,
673 pf
->pvar
->field
, &pf
->tvar
->ref
,
678 ret
= convert_variable_type(vr_die
, pf
->tvar
, pf
->pvar
->type
);
679 /* *expr will be cached in libdw. Don't free it. */
683 /* Find a variable in a subprogram die */
684 static int find_variable(Dwarf_Die
*sp_die
, struct probe_finder
*pf
)
686 Dwarf_Die vr_die
, *scopes
;
691 pf
->tvar
->name
= strdup(pf
->pvar
->name
);
693 ret
= synthesize_perf_probe_arg(pf
->pvar
, buf
, 32);
696 ptr
= strchr(buf
, ':'); /* Change type separator to _ */
699 pf
->tvar
->name
= strdup(buf
);
701 if (pf
->tvar
->name
== NULL
)
704 if (!is_c_varname(pf
->pvar
->var
)) {
705 /* Copy raw parameters */
706 pf
->tvar
->value
= strdup(pf
->pvar
->var
);
707 if (pf
->tvar
->value
== NULL
)
713 pr_debug("Searching '%s' variable in context.\n",
715 /* Search child die for local variables and parameters. */
716 if (die_find_variable(sp_die
, pf
->pvar
->var
, &vr_die
))
717 ret
= convert_variable(&vr_die
, pf
);
719 /* Search upper class */
720 nscopes
= dwarf_getscopes_die(sp_die
, &scopes
);
722 ret
= dwarf_getscopevar(scopes
, nscopes
, pf
->pvar
->var
,
723 0, NULL
, 0, 0, &vr_die
);
725 ret
= convert_variable(&vr_die
, pf
);
733 pr_warning("Failed to find '%s' in this function.\n",
738 /* Show a probe point to output buffer */
739 static int convert_probe_point(Dwarf_Die
*sp_die
, struct probe_finder
*pf
)
741 struct kprobe_trace_event
*tev
;
746 Dwarf_Attribute fb_attr
;
749 if (pf
->ntevs
== pf
->max_tevs
) {
750 pr_warning("Too many( > %d) probe point found.\n",
754 tev
= &pf
->tevs
[pf
->ntevs
++];
756 /* If no real subprogram, find a real one */
757 if (!sp_die
|| dwarf_tag(sp_die
) != DW_TAG_subprogram
) {
758 sp_die
= die_find_real_subprogram(&pf
->cu_die
,
761 pr_warning("Failed to find probe point in any "
767 /* Copy the name of probe point */
768 name
= dwarf_diename(sp_die
);
770 if (dwarf_entrypc(sp_die
, &eaddr
) != 0) {
771 pr_warning("Failed to get entry pc of %s\n",
772 dwarf_diename(sp_die
));
775 tev
->point
.symbol
= strdup(name
);
776 if (tev
->point
.symbol
== NULL
)
778 tev
->point
.offset
= (unsigned long)(pf
->addr
- eaddr
);
780 /* This function has no name. */
781 tev
->point
.offset
= (unsigned long)pf
->addr
;
783 pr_debug("Probe point found: %s+%lu\n", tev
->point
.symbol
,
786 /* Get the frame base attribute/ops */
787 dwarf_attr(sp_die
, DW_AT_frame_base
, &fb_attr
);
788 ret
= dwarf_getlocation_addr(&fb_attr
, pf
->addr
, &pf
->fb_ops
, &nops
, 1);
789 if (ret
<= 0 || nops
== 0) {
791 #if _ELFUTILS_PREREQ(0, 142)
792 } else if (nops
== 1 && pf
->fb_ops
[0].atom
== DW_OP_call_frame_cfa
&&
795 if (dwarf_cfi_addrframe(pf
->cfi
, pf
->addr
, &frame
) != 0 ||
796 dwarf_frame_cfa(frame
, &pf
->fb_ops
, &nops
) != 0) {
797 pr_warning("Failed to get CFA on 0x%jx\n",
798 (uintmax_t)pf
->addr
);
804 /* Find each argument */
805 tev
->nargs
= pf
->pev
->nargs
;
806 tev
->args
= zalloc(sizeof(struct kprobe_trace_arg
) * tev
->nargs
);
807 if (tev
->args
== NULL
)
809 for (i
= 0; i
< pf
->pev
->nargs
; i
++) {
810 pf
->pvar
= &pf
->pev
->args
[i
];
811 pf
->tvar
= &tev
->args
[i
];
812 ret
= find_variable(sp_die
, pf
);
817 /* *pf->fb_ops will be cached in libdw. Don't free it. */
822 /* Find probe point from its line number */
823 static int find_probe_point_by_line(struct probe_finder
*pf
)
832 if (dwarf_getsrclines(&pf
->cu_die
, &lines
, &nlines
) != 0) {
833 pr_warning("No source lines found in this CU.\n");
837 for (i
= 0; i
< nlines
&& ret
== 0; i
++) {
838 line
= dwarf_onesrcline(lines
, i
);
839 if (dwarf_lineno(line
, &lineno
) != 0 ||
843 /* TODO: Get fileno from line, but how? */
844 if (strtailcmp(dwarf_linesrc(line
, NULL
, NULL
), pf
->fname
) != 0)
847 if (dwarf_lineaddr(line
, &addr
) != 0) {
848 pr_warning("Failed to get the address of the line.\n");
851 pr_debug("Probe line found: line[%d]:%d addr:0x%jx\n",
852 (int)i
, lineno
, (uintmax_t)addr
);
855 ret
= convert_probe_point(NULL
, pf
);
856 /* Continuing, because target line might be inlined. */
861 /* Find lines which match lazy pattern */
862 static int find_lazy_match_lines(struct list_head
*head
,
863 const char *fname
, const char *pat
)
865 char *fbuf
, *p1
, *p2
;
866 int fd
, line
, nlines
= -1;
869 fd
= open(fname
, O_RDONLY
);
871 pr_warning("Failed to open %s: %s\n", fname
, strerror(-fd
));
875 if (fstat(fd
, &st
) < 0) {
876 pr_warning("Failed to get the size of %s: %s\n",
877 fname
, strerror(errno
));
883 fbuf
= malloc(st
.st_size
+ 2);
886 if (read(fd
, fbuf
, st
.st_size
) < 0) {
887 pr_warning("Failed to read %s: %s\n", fname
, strerror(errno
));
891 fbuf
[st
.st_size
] = '\n'; /* Dummy line */
892 fbuf
[st
.st_size
+ 1] = '\0';
896 while ((p2
= strchr(p1
, '\n')) != NULL
) {
898 if (strlazymatch(p1
, pat
)) {
899 line_list__add_line(head
, line
);
912 /* Find probe points from lazy pattern */
913 static int find_probe_point_lazy(Dwarf_Die
*sp_die
, struct probe_finder
*pf
)
923 if (list_empty(&pf
->lcache
)) {
924 /* Matching lazy line pattern */
925 ret
= find_lazy_match_lines(&pf
->lcache
, pf
->fname
,
926 pf
->pev
->point
.lazy_line
);
928 pr_debug("No matched lines found in %s.\n", pf
->fname
);
934 if (dwarf_getsrclines(&pf
->cu_die
, &lines
, &nlines
) != 0) {
935 pr_warning("No source lines found in this CU.\n");
939 for (i
= 0; i
< nlines
&& ret
>= 0; i
++) {
940 line
= dwarf_onesrcline(lines
, i
);
942 if (dwarf_lineno(line
, &lineno
) != 0 ||
943 !line_list__has_line(&pf
->lcache
, lineno
))
946 /* TODO: Get fileno from line, but how? */
947 if (strtailcmp(dwarf_linesrc(line
, NULL
, NULL
), pf
->fname
) != 0)
950 if (dwarf_lineaddr(line
, &addr
) != 0) {
951 pr_debug("Failed to get the address of line %d.\n",
956 /* Address filtering 1: does sp_die include addr? */
957 if (!dwarf_haspc(sp_die
, addr
))
959 /* Address filtering 2: No child include addr? */
960 if (die_find_inlinefunc(sp_die
, addr
, &die_mem
))
964 pr_debug("Probe line found: line[%d]:%d addr:0x%llx\n",
965 (int)i
, lineno
, (unsigned long long)addr
);
968 ret
= convert_probe_point(sp_die
, pf
);
969 /* Continuing, because target line might be inlined. */
971 /* TODO: deallocate lines, but how? */
975 /* Callback parameter with return value */
976 struct dwarf_callback_param
{
981 static int probe_point_inline_cb(Dwarf_Die
*in_die
, void *data
)
983 struct dwarf_callback_param
*param
= data
;
984 struct probe_finder
*pf
= param
->data
;
985 struct perf_probe_point
*pp
= &pf
->pev
->point
;
989 param
->retval
= find_probe_point_lazy(in_die
, pf
);
991 /* Get probe address */
992 if (dwarf_entrypc(in_die
, &addr
) != 0) {
993 pr_warning("Failed to get entry pc of %s.\n",
994 dwarf_diename(in_die
));
995 param
->retval
= -ENOENT
;
996 return DWARF_CB_ABORT
;
999 pf
->addr
+= pp
->offset
;
1000 pr_debug("found inline addr: 0x%jx\n",
1001 (uintmax_t)pf
->addr
);
1003 param
->retval
= convert_probe_point(in_die
, pf
);
1004 if (param
->retval
< 0)
1005 return DWARF_CB_ABORT
;
1011 /* Search function from function name */
1012 static int probe_point_search_cb(Dwarf_Die
*sp_die
, void *data
)
1014 struct dwarf_callback_param
*param
= data
;
1015 struct probe_finder
*pf
= param
->data
;
1016 struct perf_probe_point
*pp
= &pf
->pev
->point
;
1018 /* Check tag and diename */
1019 if (dwarf_tag(sp_die
) != DW_TAG_subprogram
||
1020 !die_compare_name(sp_die
, pp
->function
))
1023 pf
->fname
= dwarf_decl_file(sp_die
);
1024 if (pp
->line
) { /* Function relative line */
1025 dwarf_decl_line(sp_die
, &pf
->lno
);
1026 pf
->lno
+= pp
->line
;
1027 param
->retval
= find_probe_point_by_line(pf
);
1028 } else if (!dwarf_func_inline(sp_die
)) {
1031 param
->retval
= find_probe_point_lazy(sp_die
, pf
);
1033 if (dwarf_entrypc(sp_die
, &pf
->addr
) != 0) {
1034 pr_warning("Failed to get entry pc of %s.\n",
1035 dwarf_diename(sp_die
));
1036 param
->retval
= -ENOENT
;
1037 return DWARF_CB_ABORT
;
1039 pf
->addr
+= pp
->offset
;
1040 /* TODO: Check the address in this function */
1041 param
->retval
= convert_probe_point(sp_die
, pf
);
1044 struct dwarf_callback_param _param
= {.data
= (void *)pf
,
1046 /* Inlined function: search instances */
1047 dwarf_func_inline_instances(sp_die
, probe_point_inline_cb
,
1049 param
->retval
= _param
.retval
;
1052 return DWARF_CB_ABORT
; /* Exit; no same symbol in this CU. */
1055 static int find_probe_point_by_func(struct probe_finder
*pf
)
1057 struct dwarf_callback_param _param
= {.data
= (void *)pf
,
1059 dwarf_getfuncs(&pf
->cu_die
, probe_point_search_cb
, &_param
, 0);
1060 return _param
.retval
;
1063 /* Find kprobe_trace_events specified by perf_probe_event from debuginfo */
1064 int find_kprobe_trace_events(int fd
, struct perf_probe_event
*pev
,
1065 struct kprobe_trace_event
**tevs
, int max_tevs
)
1067 struct probe_finder pf
= {.pev
= pev
, .max_tevs
= max_tevs
};
1068 struct perf_probe_point
*pp
= &pev
->point
;
1069 Dwarf_Off off
, noff
;
1075 pf
.tevs
= zalloc(sizeof(struct kprobe_trace_event
) * max_tevs
);
1076 if (pf
.tevs
== NULL
)
1081 dbg
= dwarf_begin(fd
, DWARF_C_READ
);
1083 pr_warning("No dwarf info found in the vmlinux - "
1084 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
1090 #if _ELFUTILS_PREREQ(0, 142)
1091 /* Get the call frame information from this dwarf */
1092 pf
.cfi
= dwarf_getcfi(dbg
);
1096 line_list__init(&pf
.lcache
);
1097 /* Loop on CUs (Compilation Unit) */
1098 while (!dwarf_nextcu(dbg
, off
, &noff
, &cuhl
, NULL
, NULL
, NULL
) &&
1100 /* Get the DIE(Debugging Information Entry) of this CU */
1101 diep
= dwarf_offdie(dbg
, off
+ cuhl
, &pf
.cu_die
);
1105 /* Check if target file is included. */
1107 pf
.fname
= cu_find_realpath(&pf
.cu_die
, pp
->file
);
1111 if (!pp
->file
|| pf
.fname
) {
1113 ret
= find_probe_point_by_func(&pf
);
1114 else if (pp
->lazy_line
)
1115 ret
= find_probe_point_lazy(NULL
, &pf
);
1118 ret
= find_probe_point_by_line(&pf
);
1123 line_list__free(&pf
.lcache
);
1126 return (ret
< 0) ? ret
: pf
.ntevs
;
1129 /* Reverse search */
1130 int find_perf_probe_point(int fd
, unsigned long addr
,
1131 struct perf_probe_point
*ppt
)
1133 Dwarf_Die cudie
, spdie
, indie
;
1136 Dwarf_Addr laddr
, eaddr
;
1138 int lineno
, ret
= 0;
1141 dbg
= dwarf_begin(fd
, DWARF_C_READ
);
1146 if (!dwarf_addrdie(dbg
, (Dwarf_Addr
)addr
, &cudie
)) {
1151 /* Find a corresponding line */
1152 line
= dwarf_getsrc_die(&cudie
, (Dwarf_Addr
)addr
);
1154 if (dwarf_lineaddr(line
, &laddr
) == 0 &&
1155 (Dwarf_Addr
)addr
== laddr
&&
1156 dwarf_lineno(line
, &lineno
) == 0) {
1157 tmp
= dwarf_linesrc(line
, NULL
, NULL
);
1160 ppt
->file
= strdup(tmp
);
1161 if (ppt
->file
== NULL
) {
1170 /* Find a corresponding function */
1171 if (die_find_real_subprogram(&cudie
, (Dwarf_Addr
)addr
, &spdie
)) {
1172 tmp
= dwarf_diename(&spdie
);
1173 if (!tmp
|| dwarf_entrypc(&spdie
, &eaddr
) != 0)
1177 if (die_find_inlinefunc(&spdie
, (Dwarf_Addr
)addr
,
1179 /* addr in an inline function */
1180 tmp
= dwarf_diename(&indie
);
1183 ret
= dwarf_decl_line(&indie
, &lineno
);
1185 if (eaddr
== addr
) { /* Function entry */
1189 ret
= dwarf_decl_line(&spdie
, &lineno
);
1192 /* Make a relative line number */
1193 ppt
->line
-= lineno
;
1197 /* We don't have a line number, let's use offset */
1198 ppt
->offset
= addr
- (unsigned long)eaddr
;
1200 ppt
->function
= strdup(tmp
);
1201 if (ppt
->function
== NULL
) {
1211 ret
= found
? 1 : 0;
1215 /* Add a line and store the src path */
1216 static int line_range_add_line(const char *src
, unsigned int lineno
,
1217 struct line_range
*lr
)
1219 /* Copy source path */
1221 lr
->path
= strdup(src
);
1222 if (lr
->path
== NULL
)
1225 return line_list__add_line(&lr
->line_list
, lineno
);
1228 /* Search function declaration lines */
1229 static int line_range_funcdecl_cb(Dwarf_Die
*sp_die
, void *data
)
1231 struct dwarf_callback_param
*param
= data
;
1232 struct line_finder
*lf
= param
->data
;
1236 src
= dwarf_decl_file(sp_die
);
1237 if (src
&& strtailcmp(src
, lf
->fname
) != 0)
1240 if (dwarf_decl_line(sp_die
, &lineno
) != 0 ||
1241 (lf
->lno_s
> lineno
|| lf
->lno_e
< lineno
))
1244 param
->retval
= line_range_add_line(src
, lineno
, lf
->lr
);
1245 if (param
->retval
< 0)
1246 return DWARF_CB_ABORT
;
1250 static int find_line_range_func_decl_lines(struct line_finder
*lf
)
1252 struct dwarf_callback_param param
= {.data
= (void *)lf
, .retval
= 0};
1253 dwarf_getfuncs(&lf
->cu_die
, line_range_funcdecl_cb
, ¶m
, 0);
1254 return param
.retval
;
1257 /* Find line range from its line number */
1258 static int find_line_range_by_line(Dwarf_Die
*sp_die
, struct line_finder
*lf
)
1264 int lineno
, ret
= 0;
1268 line_list__init(&lf
->lr
->line_list
);
1269 if (dwarf_getsrclines(&lf
->cu_die
, &lines
, &nlines
) != 0) {
1270 pr_warning("No source lines found in this CU.\n");
1274 /* Search probable lines on lines list */
1275 for (i
= 0; i
< nlines
; i
++) {
1276 line
= dwarf_onesrcline(lines
, i
);
1277 if (dwarf_lineno(line
, &lineno
) != 0 ||
1278 (lf
->lno_s
> lineno
|| lf
->lno_e
< lineno
))
1282 /* Address filtering 1: does sp_die include addr? */
1283 if (dwarf_lineaddr(line
, &addr
) != 0 ||
1284 !dwarf_haspc(sp_die
, addr
))
1287 /* Address filtering 2: No child include addr? */
1288 if (die_find_inlinefunc(sp_die
, addr
, &die_mem
))
1292 /* TODO: Get fileno from line, but how? */
1293 src
= dwarf_linesrc(line
, NULL
, NULL
);
1294 if (strtailcmp(src
, lf
->fname
) != 0)
1297 ret
= line_range_add_line(src
, lineno
, lf
->lr
);
1303 * Dwarf lines doesn't include function declarations. We have to
1304 * check functions list or given function.
1307 src
= dwarf_decl_file(sp_die
);
1308 if (src
&& dwarf_decl_line(sp_die
, &lineno
) == 0 &&
1309 (lf
->lno_s
<= lineno
&& lf
->lno_e
>= lineno
))
1310 ret
= line_range_add_line(src
, lineno
, lf
->lr
);
1312 ret
= find_line_range_func_decl_lines(lf
);
1316 if (!list_empty(&lf
->lr
->line_list
))
1317 ret
= lf
->found
= 1;
1319 ret
= 0; /* Lines are not found */
1322 lf
->lr
->path
= NULL
;
1327 static int line_range_inline_cb(Dwarf_Die
*in_die
, void *data
)
1329 struct dwarf_callback_param
*param
= data
;
1331 param
->retval
= find_line_range_by_line(in_die
, param
->data
);
1332 return DWARF_CB_ABORT
; /* No need to find other instances */
1335 /* Search function from function name */
1336 static int line_range_search_cb(Dwarf_Die
*sp_die
, void *data
)
1338 struct dwarf_callback_param
*param
= data
;
1339 struct line_finder
*lf
= param
->data
;
1340 struct line_range
*lr
= lf
->lr
;
1342 if (dwarf_tag(sp_die
) == DW_TAG_subprogram
&&
1343 die_compare_name(sp_die
, lr
->function
)) {
1344 lf
->fname
= dwarf_decl_file(sp_die
);
1345 dwarf_decl_line(sp_die
, &lr
->offset
);
1346 pr_debug("fname: %s, lineno:%d\n", lf
->fname
, lr
->offset
);
1347 lf
->lno_s
= lr
->offset
+ lr
->start
;
1348 if (lf
->lno_s
< 0) /* Overflow */
1349 lf
->lno_s
= INT_MAX
;
1350 lf
->lno_e
= lr
->offset
+ lr
->end
;
1351 if (lf
->lno_e
< 0) /* Overflow */
1352 lf
->lno_e
= INT_MAX
;
1353 pr_debug("New line range: %d to %d\n", lf
->lno_s
, lf
->lno_e
);
1354 lr
->start
= lf
->lno_s
;
1355 lr
->end
= lf
->lno_e
;
1356 if (dwarf_func_inline(sp_die
)) {
1357 struct dwarf_callback_param _param
;
1358 _param
.data
= (void *)lf
;
1360 dwarf_func_inline_instances(sp_die
,
1361 line_range_inline_cb
,
1363 param
->retval
= _param
.retval
;
1365 param
->retval
= find_line_range_by_line(sp_die
, lf
);
1366 return DWARF_CB_ABORT
;
1371 static int find_line_range_by_func(struct line_finder
*lf
)
1373 struct dwarf_callback_param param
= {.data
= (void *)lf
, .retval
= 0};
1374 dwarf_getfuncs(&lf
->cu_die
, line_range_search_cb
, ¶m
, 0);
1375 return param
.retval
;
1378 int find_line_range(int fd
, struct line_range
*lr
)
1380 struct line_finder lf
= {.lr
= lr
, .found
= 0};
1382 Dwarf_Off off
= 0, noff
;
1386 const char *comp_dir
;
1388 dbg
= dwarf_begin(fd
, DWARF_C_READ
);
1390 pr_warning("No dwarf info found in the vmlinux - "
1391 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
1395 /* Loop on CUs (Compilation Unit) */
1396 while (!lf
.found
&& ret
>= 0) {
1397 if (dwarf_nextcu(dbg
, off
, &noff
, &cuhl
, NULL
, NULL
, NULL
) != 0)
1400 /* Get the DIE(Debugging Information Entry) of this CU */
1401 diep
= dwarf_offdie(dbg
, off
+ cuhl
, &lf
.cu_die
);
1405 /* Check if target file is included. */
1407 lf
.fname
= cu_find_realpath(&lf
.cu_die
, lr
->file
);
1411 if (!lr
->file
|| lf
.fname
) {
1413 ret
= find_line_range_by_func(&lf
);
1415 lf
.lno_s
= lr
->start
;
1417 ret
= find_line_range_by_line(NULL
, &lf
);
1423 /* Store comp_dir */
1425 comp_dir
= cu_get_comp_dir(&lf
.cu_die
);
1427 lr
->comp_dir
= strdup(comp_dir
);
1433 pr_debug("path: %s\n", lr
->path
);
1436 return (ret
< 0) ? ret
: lf
.found
;