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);
119 /* Dwarf FL wrappers */
121 static int __linux_kernel_find_elf(Dwfl_Module *mod,
123 const char *module_name,
125 char **file_name, Elf **elfp)
128 const char *path = kernel_get_module_path(module_name);
131 fd = open(path, O_RDONLY);
133 *file_name = strdup(path);
137 /* If failed, try to call standard method */
138 return dwfl_linux_kernel_find_elf(mod, userdata, module_name, base,
142 static char *debuginfo_path; /* Currently dummy */
144 static const Dwfl_Callbacks offline_callbacks = {
145 .find_debuginfo = dwfl_standard_find_debuginfo,
146 .debuginfo_path = &debuginfo_path,
148 .section_address = dwfl_offline_section_address,
150 /* We use this table for core files too. */
151 .find_elf = dwfl_build_id_find_elf,
154 static const Dwfl_Callbacks kernel_callbacks = {
155 .find_debuginfo = dwfl_standard_find_debuginfo,
156 .debuginfo_path = &debuginfo_path,
158 .find_elf = __linux_kernel_find_elf,
159 .section_address = dwfl_linux_kernel_module_section_address,
162 /* Get a Dwarf from offline image */
163 static Dwarf *dwfl_init_offline_dwarf(int fd, Dwfl **dwflp, Dwarf_Addr *bias)
171 *dwflp = dwfl_begin(&offline_callbacks);
175 mod = dwfl_report_offline(*dwflp, "", "", fd);
179 dbg = dwfl_module_getdwarf(mod, bias);
188 /* Get a Dwarf from live kernel image */
189 static Dwarf *dwfl_init_live_kernel_dwarf(Dwarf_Addr addr, Dwfl **dwflp,
197 *dwflp = dwfl_begin(&kernel_callbacks);
201 /* Load the kernel dwarves: Don't care the result here */
202 dwfl_linux_kernel_report_kernel(*dwflp);
203 dwfl_linux_kernel_report_modules(*dwflp);
205 dbg = dwfl_addrdwarf(*dwflp, addr, bias);
206 /* Here, check whether we could get a real dwarf */
216 /* Find the realpath of the target file. */
217 static const char *cu_find_realpath(Dwarf_Die *cu_die, const char *fname)
221 const char *src = NULL;
227 ret = dwarf_getsrcfiles(cu_die, &files, &nfiles);
231 for (i = 0; i < nfiles; i++) {
232 src = dwarf_filesrc(files, i, NULL, NULL);
233 if (strtailcmp(src, fname) == 0)
241 /* Get DW_AT_comp_dir (should be NULL with older gcc) */
242 static const char *cu_get_comp_dir(Dwarf_Die *cu_die)
244 Dwarf_Attribute attr;
245 if (dwarf_attr(cu_die, DW_AT_comp_dir, &attr) == NULL)
247 return dwarf_formstring(&attr);
250 /* Compare diename and tname */
251 static bool die_compare_name(Dwarf_Die *dw_die, const char *tname)
254 name = dwarf_diename(dw_die);
255 return name ? (strcmp(tname, name) == 0) : false;
259 static Dwarf_Die *die_get_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
261 Dwarf_Attribute attr;
263 if (dwarf_attr_integrate(vr_die, DW_AT_type, &attr) &&
264 dwarf_formref_die(&attr, die_mem))
270 /* Get a type die, but skip qualifiers */
271 static Dwarf_Die *__die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
276 vr_die = die_get_type(vr_die, die_mem);
279 tag = dwarf_tag(vr_die);
280 } while (tag == DW_TAG_const_type ||
281 tag == DW_TAG_restrict_type ||
282 tag == DW_TAG_volatile_type ||
283 tag == DW_TAG_shared_type);
288 /* Get a type die, but skip qualifiers and typedef */
289 static Dwarf_Die *die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
292 vr_die = __die_get_real_type(vr_die, die_mem);
293 } while (vr_die && dwarf_tag(vr_die) == DW_TAG_typedef);
298 static bool die_is_signed_type(Dwarf_Die *tp_die)
300 Dwarf_Attribute attr;
303 if (dwarf_attr(tp_die, DW_AT_encoding, &attr) == NULL ||
304 dwarf_formudata(&attr, &ret) != 0)
307 return (ret == DW_ATE_signed_char || ret == DW_ATE_signed ||
308 ret == DW_ATE_signed_fixed);
311 static int die_get_byte_size(Dwarf_Die *tp_die)
313 Dwarf_Attribute attr;
316 if (dwarf_attr(tp_die, DW_AT_byte_size, &attr) == NULL ||
317 dwarf_formudata(&attr, &ret) != 0)
323 /* Get data_member_location offset */
324 static int die_get_data_member_location(Dwarf_Die *mb_die, Dwarf_Word *offs)
326 Dwarf_Attribute attr;
331 if (dwarf_attr(mb_die, DW_AT_data_member_location, &attr) == NULL)
334 if (dwarf_formudata(&attr, offs) != 0) {
335 /* DW_AT_data_member_location should be DW_OP_plus_uconst */
336 ret = dwarf_getlocation(&attr, &expr, &nexpr);
337 if (ret < 0 || nexpr == 0)
340 if (expr[0].atom != DW_OP_plus_uconst || nexpr != 1) {
341 pr_debug("Unable to get offset:Unexpected OP %x (%zd)\n",
342 expr[0].atom, nexpr);
345 *offs = (Dwarf_Word)expr[0].number;
350 /* Return values for die_find callbacks */
352 DIE_FIND_CB_FOUND = 0, /* End of Search */
353 DIE_FIND_CB_CHILD = 1, /* Search only children */
354 DIE_FIND_CB_SIBLING = 2, /* Search only siblings */
355 DIE_FIND_CB_CONTINUE = 3, /* Search children and siblings */
358 /* Search a child die */
359 static Dwarf_Die *die_find_child(Dwarf_Die *rt_die,
360 int (*callback)(Dwarf_Die *, void *),
361 void *data, Dwarf_Die *die_mem)
366 ret = dwarf_child(rt_die, die_mem);
371 ret = callback(die_mem, data);
372 if (ret == DIE_FIND_CB_FOUND)
375 if ((ret & DIE_FIND_CB_CHILD) &&
376 die_find_child(die_mem, callback, data, &child_die)) {
377 memcpy(die_mem, &child_die, sizeof(Dwarf_Die));
380 } while ((ret & DIE_FIND_CB_SIBLING) &&
381 dwarf_siblingof(die_mem, die_mem) == 0);
386 struct __addr_die_search_param {
391 static int __die_search_func_cb(Dwarf_Die *fn_die, void *data)
393 struct __addr_die_search_param *ad = data;
395 if (dwarf_tag(fn_die) == DW_TAG_subprogram &&
396 dwarf_haspc(fn_die, ad->addr)) {
397 memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die));
398 return DWARF_CB_ABORT;
403 /* Search a real subprogram including this line, */
404 static Dwarf_Die *die_find_real_subprogram(Dwarf_Die *cu_die, Dwarf_Addr addr,
407 struct __addr_die_search_param ad;
409 ad.die_mem = die_mem;
410 /* dwarf_getscopes can't find subprogram. */
411 if (!dwarf_getfuncs(cu_die, __die_search_func_cb, &ad, 0))
417 /* die_find callback for inline function search */
418 static int __die_find_inline_cb(Dwarf_Die *die_mem, void *data)
420 Dwarf_Addr *addr = data;
422 if (dwarf_tag(die_mem) == DW_TAG_inlined_subroutine &&
423 dwarf_haspc(die_mem, *addr))
424 return DIE_FIND_CB_FOUND;
426 return DIE_FIND_CB_CONTINUE;
429 /* Similar to dwarf_getfuncs, but returns inlined_subroutine if exists. */
430 static Dwarf_Die *die_find_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
433 return die_find_child(sp_die, __die_find_inline_cb, &addr, die_mem);
436 struct __find_variable_param {
441 static int __die_find_variable_cb(Dwarf_Die *die_mem, void *data)
443 struct __find_variable_param *fvp = data;
446 tag = dwarf_tag(die_mem);
447 if ((tag == DW_TAG_formal_parameter ||
448 tag == DW_TAG_variable) &&
449 die_compare_name(die_mem, fvp->name))
450 return DIE_FIND_CB_FOUND;
452 if (dwarf_haspc(die_mem, fvp->addr))
453 return DIE_FIND_CB_CONTINUE;
455 return DIE_FIND_CB_SIBLING;
458 /* Find a variable called 'name' at given address */
459 static Dwarf_Die *die_find_variable_at(Dwarf_Die *sp_die, const char *name,
460 Dwarf_Addr addr, Dwarf_Die *die_mem)
462 struct __find_variable_param fvp = { .name = name, .addr = addr};
464 return die_find_child(sp_die, __die_find_variable_cb, (void *)&fvp,
468 static int __die_find_member_cb(Dwarf_Die *die_mem, void *data)
470 const char *name = data;
472 if ((dwarf_tag(die_mem) == DW_TAG_member) &&
473 die_compare_name(die_mem, name))
474 return DIE_FIND_CB_FOUND;
476 return DIE_FIND_CB_SIBLING;
479 /* Find a member called 'name' */
480 static Dwarf_Die *die_find_member(Dwarf_Die *st_die, const char *name,
483 return die_find_child(st_die, __die_find_member_cb, (void *)name,
487 /* Get the name of given variable DIE */
488 static int die_get_typename(Dwarf_Die *vr_die, char *buf, int len)
492 const char *tmp = "";
494 if (__die_get_real_type(vr_die, &type) == NULL)
497 tag = dwarf_tag(&type);
498 if (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type)
500 else if (tag == DW_TAG_subroutine_type) {
501 /* Function pointer */
502 ret = snprintf(buf, len, "(function_type)");
503 return (ret >= len) ? -E2BIG : ret;
505 if (!dwarf_diename(&type))
507 if (tag == DW_TAG_union_type)
509 else if (tag == DW_TAG_structure_type)
511 /* Write a base name */
512 ret = snprintf(buf, len, "%s%s", tmp, dwarf_diename(&type));
513 return (ret >= len) ? -E2BIG : ret;
515 ret = die_get_typename(&type, buf, len);
517 ret2 = snprintf(buf + ret, len - ret, "%s", tmp);
518 ret = (ret2 >= len - ret) ? -E2BIG : ret2 + ret;
523 /* Get the name and type of given variable DIE, stored as "type\tname" */
524 static int die_get_varname(Dwarf_Die *vr_die, char *buf, int len)
528 ret = die_get_typename(vr_die, buf, len);
530 pr_debug("Failed to get type, make it unknown.\n");
531 ret = snprintf(buf, len, "(unknown_type)");
534 ret2 = snprintf(buf + ret, len - ret, "\t%s",
535 dwarf_diename(vr_die));
536 ret = (ret2 >= len - ret) ? -E2BIG : ret2 + ret;
542 * Probe finder related functions
545 static struct probe_trace_arg_ref *alloc_trace_arg_ref(long offs)
547 struct probe_trace_arg_ref *ref;
548 ref = zalloc(sizeof(struct probe_trace_arg_ref));
555 * Convert a location into trace_arg.
556 * If tvar == NULL, this just checks variable can be converted.
558 static int convert_variable_location(Dwarf_Die *vr_die, Dwarf_Addr addr,
560 struct probe_trace_arg *tvar)
562 Dwarf_Attribute attr;
571 if (dwarf_attr(vr_die, DW_AT_external, &attr) != NULL)
574 /* TODO: handle more than 1 exprs */
575 if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL ||
576 dwarf_getlocation_addr(&attr, addr, &op, &nops, 1) <= 0 ||
578 /* TODO: Support const_value */
582 if (op->atom == DW_OP_addr) {
586 /* Static variables on memory (not stack), make @varname */
587 ret = strlen(dwarf_diename(vr_die));
588 tvar->value = zalloc(ret + 2);
589 if (tvar->value == NULL)
591 snprintf(tvar->value, ret + 2, "@%s", dwarf_diename(vr_die));
592 tvar->ref = alloc_trace_arg_ref((long)offs);
593 if (tvar->ref == NULL)
598 /* If this is based on frame buffer, set the offset */
599 if (op->atom == DW_OP_fbreg) {
607 if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) {
608 regn = op->atom - DW_OP_breg0;
611 } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) {
612 regn = op->atom - DW_OP_reg0;
613 } else if (op->atom == DW_OP_bregx) {
617 } else if (op->atom == DW_OP_regx) {
620 pr_debug("DW_OP %x is not supported.\n", op->atom);
627 regs = get_arch_regstr(regn);
629 /* This should be a bug in DWARF or this tool */
630 pr_warning("Mapping for the register number %u "
631 "missing on this architecture.\n", regn);
635 tvar->value = strdup(regs);
636 if (tvar->value == NULL)
640 tvar->ref = alloc_trace_arg_ref((long)offs);
641 if (tvar->ref == NULL)
647 static int convert_variable_type(Dwarf_Die *vr_die,
648 struct probe_trace_arg *tvar,
651 struct probe_trace_arg_ref **ref_ptr = &tvar->ref;
656 /* TODO: check all types */
657 if (cast && strcmp(cast, "string") != 0) {
658 /* Non string type is OK */
659 tvar->type = strdup(cast);
660 return (tvar->type == NULL) ? -ENOMEM : 0;
663 if (die_get_real_type(vr_die, &type) == NULL) {
664 pr_warning("Failed to get a type information of %s.\n",
665 dwarf_diename(vr_die));
669 pr_debug("%s type is %s.\n",
670 dwarf_diename(vr_die), dwarf_diename(&type));
672 if (cast && strcmp(cast, "string") == 0) { /* String type */
673 ret = dwarf_tag(&type);
674 if (ret != DW_TAG_pointer_type &&
675 ret != DW_TAG_array_type) {
676 pr_warning("Failed to cast into string: "
677 "%s(%s) is not a pointer nor array.\n",
678 dwarf_diename(vr_die), dwarf_diename(&type));
681 if (ret == DW_TAG_pointer_type) {
682 if (die_get_real_type(&type, &type) == NULL) {
683 pr_warning("Failed to get a type"
688 ref_ptr = &(*ref_ptr)->next;
689 /* Add new reference with offset +0 */
690 *ref_ptr = zalloc(sizeof(struct probe_trace_arg_ref));
691 if (*ref_ptr == NULL) {
692 pr_warning("Out of memory error\n");
696 if (!die_compare_name(&type, "char") &&
697 !die_compare_name(&type, "unsigned char")) {
698 pr_warning("Failed to cast into string: "
699 "%s is not (unsigned) char *.\n",
700 dwarf_diename(vr_die));
703 tvar->type = strdup(cast);
704 return (tvar->type == NULL) ? -ENOMEM : 0;
707 ret = die_get_byte_size(&type) * 8;
709 /* Check the bitwidth */
710 if (ret > MAX_BASIC_TYPE_BITS) {
711 pr_info("%s exceeds max-bitwidth."
712 " Cut down to %d bits.\n",
713 dwarf_diename(&type), MAX_BASIC_TYPE_BITS);
714 ret = MAX_BASIC_TYPE_BITS;
717 ret = snprintf(buf, 16, "%c%d",
718 die_is_signed_type(&type) ? 's' : 'u', ret);
719 if (ret < 0 || ret >= 16) {
722 pr_warning("Failed to convert variable type: %s\n",
726 tvar->type = strdup(buf);
727 if (tvar->type == NULL)
733 static int convert_variable_fields(Dwarf_Die *vr_die, const char *varname,
734 struct perf_probe_arg_field *field,
735 struct probe_trace_arg_ref **ref_ptr,
738 struct probe_trace_arg_ref *ref = *ref_ptr;
743 pr_debug("converting %s in %s\n", field->name, varname);
744 if (die_get_real_type(vr_die, &type) == NULL) {
745 pr_warning("Failed to get the type of %s.\n", varname);
748 pr_debug2("Var real type: (%x)\n", (unsigned)dwarf_dieoffset(&type));
749 tag = dwarf_tag(&type);
751 if (field->name[0] == '[' &&
752 (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type)) {
754 /* Save original type for next field */
755 memcpy(die_mem, &type, sizeof(*die_mem));
756 /* Get the type of this array */
757 if (die_get_real_type(&type, &type) == NULL) {
758 pr_warning("Failed to get the type of %s.\n", varname);
761 pr_debug2("Array real type: (%x)\n",
762 (unsigned)dwarf_dieoffset(&type));
763 if (tag == DW_TAG_pointer_type) {
764 ref = zalloc(sizeof(struct probe_trace_arg_ref));
768 (*ref_ptr)->next = ref;
772 ref->offset += die_get_byte_size(&type) * field->index;
774 /* Save vr_die for converting types */
775 memcpy(die_mem, vr_die, sizeof(*die_mem));
777 } else if (tag == DW_TAG_pointer_type) {
778 /* Check the pointer and dereference */
780 pr_err("Semantic error: %s must be referred by '->'\n",
784 /* Get the type pointed by this pointer */
785 if (die_get_real_type(&type, &type) == NULL) {
786 pr_warning("Failed to get the type of %s.\n", varname);
789 /* Verify it is a data structure */
790 if (dwarf_tag(&type) != DW_TAG_structure_type) {
791 pr_warning("%s is not a data structure.\n", varname);
795 ref = zalloc(sizeof(struct probe_trace_arg_ref));
799 (*ref_ptr)->next = ref;
803 /* Verify it is a data structure */
804 if (tag != DW_TAG_structure_type) {
805 pr_warning("%s is not a data structure.\n", varname);
808 if (field->name[0] == '[') {
809 pr_err("Semantic error: %s is not a pointor"
810 " nor array.\n", varname);
814 pr_err("Semantic error: %s must be referred by '.'\n",
819 pr_warning("Structure on a register is not "
825 if (die_find_member(&type, field->name, die_mem) == NULL) {
826 pr_warning("%s(tyep:%s) has no member %s.\n", varname,
827 dwarf_diename(&type), field->name);
831 /* Get the offset of the field */
832 ret = die_get_data_member_location(die_mem, &offs);
834 pr_warning("Failed to get the offset of %s.\n", field->name);
837 ref->offset += (long)offs;
840 /* Converting next field */
842 return convert_variable_fields(die_mem, field->name,
843 field->next, &ref, die_mem);
848 /* Show a variables in kprobe event format */
849 static int convert_variable(Dwarf_Die *vr_die, struct probe_finder *pf)
854 pr_debug("Converting variable %s into trace event.\n",
855 dwarf_diename(vr_die));
857 ret = convert_variable_location(vr_die, pf->addr, pf->fb_ops,
860 pr_err("Failed to find the location of %s at this address.\n"
861 " Perhaps, it has been optimized out.\n", pf->pvar->var);
862 else if (ret == -ENOTSUP)
863 pr_err("Sorry, we don't support this variable location yet.\n");
864 else if (pf->pvar->field) {
865 ret = convert_variable_fields(vr_die, pf->pvar->var,
866 pf->pvar->field, &pf->tvar->ref,
871 ret = convert_variable_type(vr_die, pf->tvar, pf->pvar->type);
872 /* *expr will be cached in libdw. Don't free it. */
876 /* Find a variable in a subprogram die */
877 static int find_variable(Dwarf_Die *sp_die, struct probe_finder *pf)
879 Dwarf_Die vr_die, *scopes;
883 if (!is_c_varname(pf->pvar->var)) {
884 /* Copy raw parameters */
885 pf->tvar->value = strdup(pf->pvar->var);
886 if (pf->tvar->value == NULL)
888 if (pf->pvar->type) {
889 pf->tvar->type = strdup(pf->pvar->type);
890 if (pf->tvar->type == NULL)
893 if (pf->pvar->name) {
894 pf->tvar->name = strdup(pf->pvar->name);
895 if (pf->tvar->name == NULL)
898 pf->tvar->name = NULL;
903 pf->tvar->name = strdup(pf->pvar->name);
905 ret = synthesize_perf_probe_arg(pf->pvar, buf, 32);
908 ptr = strchr(buf, ':'); /* Change type separator to _ */
911 pf->tvar->name = strdup(buf);
913 if (pf->tvar->name == NULL)
916 pr_debug("Searching '%s' variable in context.\n",
918 /* Search child die for local variables and parameters. */
919 if (die_find_variable_at(sp_die, pf->pvar->var, pf->addr, &vr_die))
920 ret = convert_variable(&vr_die, pf);
922 /* Search upper class */
923 nscopes = dwarf_getscopes_die(sp_die, &scopes);
924 while (nscopes-- > 1) {
925 pr_debug("Searching variables in %s\n",
926 dwarf_diename(&scopes[nscopes]));
927 /* We should check this scope, so give dummy address */
928 if (die_find_variable_at(&scopes[nscopes],
931 ret = convert_variable(&vr_die, pf);
941 pr_warning("Failed to find '%s' in this function.\n",
946 /* Convert subprogram DIE to trace point */
947 static int convert_to_trace_point(Dwarf_Die *sp_die, Dwarf_Addr paddr,
948 bool retprobe, struct probe_trace_point *tp)
953 /* Copy the name of probe point */
954 name = dwarf_diename(sp_die);
956 if (dwarf_entrypc(sp_die, &eaddr) != 0) {
957 pr_warning("Failed to get entry address of %s\n",
958 dwarf_diename(sp_die));
961 tp->symbol = strdup(name);
962 if (tp->symbol == NULL)
964 tp->offset = (unsigned long)(paddr - eaddr);
966 /* This function has no name. */
967 tp->offset = (unsigned long)paddr;
969 /* Return probe must be on the head of a subprogram */
971 if (eaddr != paddr) {
972 pr_warning("Return probe must be on the head of"
973 " a real function.\n");
982 /* Call probe_finder callback with real subprogram DIE */
983 static int call_probe_finder(Dwarf_Die *sp_die, struct probe_finder *pf)
986 Dwarf_Attribute fb_attr;
990 /* If no real subprogram, find a real one */
991 if (!sp_die || dwarf_tag(sp_die) != DW_TAG_subprogram) {
992 sp_die = die_find_real_subprogram(&pf->cu_die,
995 pr_warning("Failed to find probe point in any "
1001 /* Get the frame base attribute/ops */
1002 dwarf_attr(sp_die, DW_AT_frame_base, &fb_attr);
1003 ret = dwarf_getlocation_addr(&fb_attr, pf->addr, &pf->fb_ops, &nops, 1);
1004 if (ret <= 0 || nops == 0) {
1006 #if _ELFUTILS_PREREQ(0, 142)
1007 } else if (nops == 1 && pf->fb_ops[0].atom == DW_OP_call_frame_cfa &&
1010 if (dwarf_cfi_addrframe(pf->cfi, pf->addr, &frame) != 0 ||
1011 dwarf_frame_cfa(frame, &pf->fb_ops, &nops) != 0) {
1012 pr_warning("Failed to get call frame on 0x%jx\n",
1013 (uintmax_t)pf->addr);
1019 /* Call finder's callback handler */
1020 ret = pf->callback(sp_die, pf);
1022 /* *pf->fb_ops will be cached in libdw. Don't free it. */
1028 /* Find probe point from its line number */
1029 static int find_probe_point_by_line(struct probe_finder *pf)
1038 if (dwarf_getsrclines(&pf->cu_die, &lines, &nlines) != 0) {
1039 pr_warning("No source lines found.\n");
1043 for (i = 0; i < nlines && ret == 0; i++) {
1044 line = dwarf_onesrcline(lines, i);
1045 if (dwarf_lineno(line, &lineno) != 0 ||
1049 /* TODO: Get fileno from line, but how? */
1050 if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
1053 if (dwarf_lineaddr(line, &addr) != 0) {
1054 pr_warning("Failed to get the address of the line.\n");
1057 pr_debug("Probe line found: line[%d]:%d addr:0x%jx\n",
1058 (int)i, lineno, (uintmax_t)addr);
1061 ret = call_probe_finder(NULL, pf);
1062 /* Continuing, because target line might be inlined. */
1067 /* Find lines which match lazy pattern */
1068 static int find_lazy_match_lines(struct list_head *head,
1069 const char *fname, const char *pat)
1071 char *fbuf, *p1, *p2;
1072 int fd, line, nlines = -1;
1075 fd = open(fname, O_RDONLY);
1077 pr_warning("Failed to open %s: %s\n", fname, strerror(-fd));
1081 if (fstat(fd, &st) < 0) {
1082 pr_warning("Failed to get the size of %s: %s\n",
1083 fname, strerror(errno));
1089 fbuf = malloc(st.st_size + 2);
1092 if (read(fd, fbuf, st.st_size) < 0) {
1093 pr_warning("Failed to read %s: %s\n", fname, strerror(errno));
1097 fbuf[st.st_size] = '\n'; /* Dummy line */
1098 fbuf[st.st_size + 1] = '\0';
1102 while ((p2 = strchr(p1, '\n')) != NULL) {
1104 if (strlazymatch(p1, pat)) {
1105 line_list__add_line(head, line);
1118 /* Find probe points from lazy pattern */
1119 static int find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf)
1129 if (list_empty(&pf->lcache)) {
1130 /* Matching lazy line pattern */
1131 ret = find_lazy_match_lines(&pf->lcache, pf->fname,
1132 pf->pev->point.lazy_line);
1134 pr_debug("No matched lines found in %s.\n", pf->fname);
1140 if (dwarf_getsrclines(&pf->cu_die, &lines, &nlines) != 0) {
1141 pr_warning("No source lines found.\n");
1145 for (i = 0; i < nlines && ret >= 0; i++) {
1146 line = dwarf_onesrcline(lines, i);
1148 if (dwarf_lineno(line, &lineno) != 0 ||
1149 !line_list__has_line(&pf->lcache, lineno))
1152 /* TODO: Get fileno from line, but how? */
1153 if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
1156 if (dwarf_lineaddr(line, &addr) != 0) {
1157 pr_debug("Failed to get the address of line %d.\n",
1162 /* Address filtering 1: does sp_die include addr? */
1163 if (!dwarf_haspc(sp_die, addr))
1165 /* Address filtering 2: No child include addr? */
1166 if (die_find_inlinefunc(sp_die, addr, &die_mem))
1170 pr_debug("Probe line found: line[%d]:%d addr:0x%llx\n",
1171 (int)i, lineno, (unsigned long long)addr);
1174 ret = call_probe_finder(sp_die, pf);
1175 /* Continuing, because target line might be inlined. */
1177 /* TODO: deallocate lines, but how? */
1181 /* Callback parameter with return value */
1182 struct dwarf_callback_param {
1187 static int probe_point_inline_cb(Dwarf_Die *in_die, void *data)
1189 struct dwarf_callback_param *param = data;
1190 struct probe_finder *pf = param->data;
1191 struct perf_probe_point *pp = &pf->pev->point;
1195 param->retval = find_probe_point_lazy(in_die, pf);
1197 /* Get probe address */
1198 if (dwarf_entrypc(in_die, &addr) != 0) {
1199 pr_warning("Failed to get entry address of %s.\n",
1200 dwarf_diename(in_die));
1201 param->retval = -ENOENT;
1202 return DWARF_CB_ABORT;
1205 pf->addr += pp->offset;
1206 pr_debug("found inline addr: 0x%jx\n",
1207 (uintmax_t)pf->addr);
1209 param->retval = call_probe_finder(in_die, pf);
1210 if (param->retval < 0)
1211 return DWARF_CB_ABORT;
1217 /* Search function from function name */
1218 static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
1220 struct dwarf_callback_param *param = data;
1221 struct probe_finder *pf = param->data;
1222 struct perf_probe_point *pp = &pf->pev->point;
1224 /* Check tag and diename */
1225 if (dwarf_tag(sp_die) != DW_TAG_subprogram ||
1226 !die_compare_name(sp_die, pp->function))
1229 pf->fname = dwarf_decl_file(sp_die);
1230 if (pp->line) { /* Function relative line */
1231 dwarf_decl_line(sp_die, &pf->lno);
1232 pf->lno += pp->line;
1233 param->retval = find_probe_point_by_line(pf);
1234 } else if (!dwarf_func_inline(sp_die)) {
1237 param->retval = find_probe_point_lazy(sp_die, pf);
1239 if (dwarf_entrypc(sp_die, &pf->addr) != 0) {
1240 pr_warning("Failed to get entry address of "
1241 "%s.\n", dwarf_diename(sp_die));
1242 param->retval = -ENOENT;
1243 return DWARF_CB_ABORT;
1245 pf->addr += pp->offset;
1246 /* TODO: Check the address in this function */
1247 param->retval = call_probe_finder(sp_die, pf);
1250 struct dwarf_callback_param _param = {.data = (void *)pf,
1252 /* Inlined function: search instances */
1253 dwarf_func_inline_instances(sp_die, probe_point_inline_cb,
1255 param->retval = _param.retval;
1258 return DWARF_CB_ABORT; /* Exit; no same symbol in this CU. */
1261 static int find_probe_point_by_func(struct probe_finder *pf)
1263 struct dwarf_callback_param _param = {.data = (void *)pf,
1265 dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, &_param, 0);
1266 return _param.retval;
1269 /* Find probe points from debuginfo */
1270 static int find_probes(int fd, struct probe_finder *pf)
1272 struct perf_probe_point *pp = &pf->pev->point;
1273 Dwarf_Off off, noff;
1278 Dwarf_Addr bias; /* Currently ignored */
1281 dbg = dwfl_init_offline_dwarf(fd, &dwfl, &bias);
1283 pr_warning("No debug information found in the vmlinux - "
1284 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
1288 #if _ELFUTILS_PREREQ(0, 142)
1289 /* Get the call frame information from this dwarf */
1290 pf->cfi = dwarf_getcfi(dbg);
1294 line_list__init(&pf->lcache);
1295 /* Loop on CUs (Compilation Unit) */
1296 while (!dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL) &&
1298 /* Get the DIE(Debugging Information Entry) of this CU */
1299 diep = dwarf_offdie(dbg, off + cuhl, &pf->cu_die);
1303 /* Check if target file is included. */
1305 pf->fname = cu_find_realpath(&pf->cu_die, pp->file);
1309 if (!pp->file || pf->fname) {
1311 ret = find_probe_point_by_func(pf);
1312 else if (pp->lazy_line)
1313 ret = find_probe_point_lazy(NULL, pf);
1316 ret = find_probe_point_by_line(pf);
1321 line_list__free(&pf->lcache);
1328 /* Add a found probe point into trace event list */
1329 static int add_probe_trace_event(Dwarf_Die *sp_die, struct probe_finder *pf)
1331 struct trace_event_finder *tf =
1332 container_of(pf, struct trace_event_finder, pf);
1333 struct probe_trace_event *tev;
1336 /* Check number of tevs */
1337 if (tf->ntevs == tf->max_tevs) {
1338 pr_warning("Too many( > %d) probe point found.\n",
1342 tev = &tf->tevs[tf->ntevs++];
1344 ret = convert_to_trace_point(sp_die, pf->addr, pf->pev->point.retprobe,
1349 pr_debug("Probe point found: %s+%lu\n", tev->point.symbol,
1352 /* Find each argument */
1353 tev->nargs = pf->pev->nargs;
1354 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
1355 if (tev->args == NULL)
1357 for (i = 0; i < pf->pev->nargs; i++) {
1358 pf->pvar = &pf->pev->args[i];
1359 pf->tvar = &tev->args[i];
1360 ret = find_variable(sp_die, pf);
1368 /* Find probe_trace_events specified by perf_probe_event from debuginfo */
1369 int find_probe_trace_events(int fd, struct perf_probe_event *pev,
1370 struct probe_trace_event **tevs, int max_tevs)
1372 struct trace_event_finder tf = {
1373 .pf = {.pev = pev, .callback = add_probe_trace_event},
1374 .max_tevs = max_tevs};
1377 /* Allocate result tevs array */
1378 *tevs = zalloc(sizeof(struct probe_trace_event) * max_tevs);
1385 ret = find_probes(fd, &tf.pf);
1392 return (ret < 0) ? ret : tf.ntevs;
1395 #define MAX_VAR_LEN 64
1397 /* Collect available variables in this scope */
1398 static int collect_variables_cb(Dwarf_Die *die_mem, void *data)
1400 struct available_var_finder *af = data;
1401 struct variable_list *vl;
1402 char buf[MAX_VAR_LEN];
1405 vl = &af->vls[af->nvls - 1];
1407 tag = dwarf_tag(die_mem);
1408 if (tag == DW_TAG_formal_parameter ||
1409 tag == DW_TAG_variable) {
1410 ret = convert_variable_location(die_mem, af->pf.addr,
1411 af->pf.fb_ops, NULL);
1413 ret = die_get_varname(die_mem, buf, MAX_VAR_LEN);
1414 pr_debug2("Add new var: %s\n", buf);
1416 strlist__add(vl->vars, buf);
1420 if (af->child && dwarf_haspc(die_mem, af->pf.addr))
1421 return DIE_FIND_CB_CONTINUE;
1423 return DIE_FIND_CB_SIBLING;
1426 /* Add a found vars into available variables list */
1427 static int add_available_vars(Dwarf_Die *sp_die, struct probe_finder *pf)
1429 struct available_var_finder *af =
1430 container_of(pf, struct available_var_finder, pf);
1431 struct variable_list *vl;
1432 Dwarf_Die die_mem, *scopes = NULL;
1435 /* Check number of tevs */
1436 if (af->nvls == af->max_vls) {
1437 pr_warning("Too many( > %d) probe point found.\n", af->max_vls);
1440 vl = &af->vls[af->nvls++];
1442 ret = convert_to_trace_point(sp_die, pf->addr, pf->pev->point.retprobe,
1447 pr_debug("Probe point found: %s+%lu\n", vl->point.symbol,
1450 /* Find local variables */
1451 vl->vars = strlist__new(true, NULL);
1452 if (vl->vars == NULL)
1455 die_find_child(sp_die, collect_variables_cb, (void *)af, &die_mem);
1457 /* Find external variables */
1460 /* Don't need to search child DIE for externs. */
1462 nscopes = dwarf_getscopes_die(sp_die, &scopes);
1463 while (nscopes-- > 1)
1464 die_find_child(&scopes[nscopes], collect_variables_cb,
1465 (void *)af, &die_mem);
1470 if (strlist__empty(vl->vars)) {
1471 strlist__delete(vl->vars);
1478 /* Find available variables at given probe point */
1479 int find_available_vars_at(int fd, struct perf_probe_event *pev,
1480 struct variable_list **vls, int max_vls,
1483 struct available_var_finder af = {
1484 .pf = {.pev = pev, .callback = add_available_vars},
1485 .max_vls = max_vls, .externs = externs};
1488 /* Allocate result vls array */
1489 *vls = zalloc(sizeof(struct variable_list) * max_vls);
1496 ret = find_probes(fd, &af.pf);
1498 /* Free vlist for error */
1500 if (af.vls[af.nvls].point.symbol)
1501 free(af.vls[af.nvls].point.symbol);
1502 if (af.vls[af.nvls].vars)
1503 strlist__delete(af.vls[af.nvls].vars);
1510 return (ret < 0) ? ret : af.nvls;
1513 /* Reverse search */
1514 int find_perf_probe_point(unsigned long addr, struct perf_probe_point *ppt)
1516 Dwarf_Die cudie, spdie, indie;
1520 Dwarf_Addr laddr, eaddr, bias = 0;
1522 int lineno, ret = 0;
1525 /* Open the live linux kernel */
1526 dbg = dwfl_init_live_kernel_dwarf(addr, &dwfl, &bias);
1528 pr_warning("No debug information found in the vmlinux - "
1529 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
1534 /* Adjust address with bias */
1537 if (!dwarf_addrdie(dbg, (Dwarf_Addr)addr - bias, &cudie)) {
1538 pr_warning("Failed to find debug information for address %lx\n",
1544 /* Find a corresponding line */
1545 line = dwarf_getsrc_die(&cudie, (Dwarf_Addr)addr);
1547 if (dwarf_lineaddr(line, &laddr) == 0 &&
1548 (Dwarf_Addr)addr == laddr &&
1549 dwarf_lineno(line, &lineno) == 0) {
1550 tmp = dwarf_linesrc(line, NULL, NULL);
1553 ppt->file = strdup(tmp);
1554 if (ppt->file == NULL) {
1563 /* Find a corresponding function */
1564 if (die_find_real_subprogram(&cudie, (Dwarf_Addr)addr, &spdie)) {
1565 tmp = dwarf_diename(&spdie);
1566 if (!tmp || dwarf_entrypc(&spdie, &eaddr) != 0)
1570 if (die_find_inlinefunc(&spdie, (Dwarf_Addr)addr,
1572 /* addr in an inline function */
1573 tmp = dwarf_diename(&indie);
1576 ret = dwarf_decl_line(&indie, &lineno);
1578 if (eaddr == addr) { /* Function entry */
1582 ret = dwarf_decl_line(&spdie, &lineno);
1585 /* Make a relative line number */
1586 ppt->line -= lineno;
1590 /* We don't have a line number, let's use offset */
1591 ppt->offset = addr - (unsigned long)eaddr;
1593 ppt->function = strdup(tmp);
1594 if (ppt->function == NULL) {
1605 ret = found ? 1 : 0;
1609 /* Add a line and store the src path */
1610 static int line_range_add_line(const char *src, unsigned int lineno,
1611 struct line_range *lr)
1613 /* Copy source path */
1615 lr->path = strdup(src);
1616 if (lr->path == NULL)
1619 return line_list__add_line(&lr->line_list, lineno);
1622 /* Search function declaration lines */
1623 static int line_range_funcdecl_cb(Dwarf_Die *sp_die, void *data)
1625 struct dwarf_callback_param *param = data;
1626 struct line_finder *lf = param->data;
1630 src = dwarf_decl_file(sp_die);
1631 if (src && strtailcmp(src, lf->fname) != 0)
1634 if (dwarf_decl_line(sp_die, &lineno) != 0 ||
1635 (lf->lno_s > lineno || lf->lno_e < lineno))
1638 param->retval = line_range_add_line(src, lineno, lf->lr);
1639 if (param->retval < 0)
1640 return DWARF_CB_ABORT;
1644 static int find_line_range_func_decl_lines(struct line_finder *lf)
1646 struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
1647 dwarf_getfuncs(&lf->cu_die, line_range_funcdecl_cb, ¶m, 0);
1648 return param.retval;
1651 /* Find line range from its line number */
1652 static int find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
1658 int lineno, ret = 0;
1662 line_list__init(&lf->lr->line_list);
1663 if (dwarf_getsrclines(&lf->cu_die, &lines, &nlines) != 0) {
1664 pr_warning("No source lines found.\n");
1668 /* Search probable lines on lines list */
1669 for (i = 0; i < nlines; i++) {
1670 line = dwarf_onesrcline(lines, i);
1671 if (dwarf_lineno(line, &lineno) != 0 ||
1672 (lf->lno_s > lineno || lf->lno_e < lineno))
1676 /* Address filtering 1: does sp_die include addr? */
1677 if (dwarf_lineaddr(line, &addr) != 0 ||
1678 !dwarf_haspc(sp_die, addr))
1681 /* Address filtering 2: No child include addr? */
1682 if (die_find_inlinefunc(sp_die, addr, &die_mem))
1686 /* TODO: Get fileno from line, but how? */
1687 src = dwarf_linesrc(line, NULL, NULL);
1688 if (strtailcmp(src, lf->fname) != 0)
1691 ret = line_range_add_line(src, lineno, lf->lr);
1697 * Dwarf lines doesn't include function declarations. We have to
1698 * check functions list or given function.
1701 src = dwarf_decl_file(sp_die);
1702 if (src && dwarf_decl_line(sp_die, &lineno) == 0 &&
1703 (lf->lno_s <= lineno && lf->lno_e >= lineno))
1704 ret = line_range_add_line(src, lineno, lf->lr);
1706 ret = find_line_range_func_decl_lines(lf);
1710 if (!list_empty(&lf->lr->line_list))
1711 ret = lf->found = 1;
1713 ret = 0; /* Lines are not found */
1716 lf->lr->path = NULL;
1721 static int line_range_inline_cb(Dwarf_Die *in_die, void *data)
1723 struct dwarf_callback_param *param = data;
1725 param->retval = find_line_range_by_line(in_die, param->data);
1726 return DWARF_CB_ABORT; /* No need to find other instances */
1729 /* Search function from function name */
1730 static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
1732 struct dwarf_callback_param *param = data;
1733 struct line_finder *lf = param->data;
1734 struct line_range *lr = lf->lr;
1736 pr_debug("find (%llx) %s\n",
1737 (unsigned long long)dwarf_dieoffset(sp_die),
1738 dwarf_diename(sp_die));
1739 if (dwarf_tag(sp_die) == DW_TAG_subprogram &&
1740 die_compare_name(sp_die, lr->function)) {
1741 lf->fname = dwarf_decl_file(sp_die);
1742 dwarf_decl_line(sp_die, &lr->offset);
1743 pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
1744 lf->lno_s = lr->offset + lr->start;
1745 if (lf->lno_s < 0) /* Overflow */
1746 lf->lno_s = INT_MAX;
1747 lf->lno_e = lr->offset + lr->end;
1748 if (lf->lno_e < 0) /* Overflow */
1749 lf->lno_e = INT_MAX;
1750 pr_debug("New line range: %d to %d\n", lf->lno_s, lf->lno_e);
1751 lr->start = lf->lno_s;
1752 lr->end = lf->lno_e;
1753 if (dwarf_func_inline(sp_die)) {
1754 struct dwarf_callback_param _param;
1755 _param.data = (void *)lf;
1757 dwarf_func_inline_instances(sp_die,
1758 line_range_inline_cb,
1760 param->retval = _param.retval;
1762 param->retval = find_line_range_by_line(sp_die, lf);
1763 return DWARF_CB_ABORT;
1768 static int find_line_range_by_func(struct line_finder *lf)
1770 struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
1771 dwarf_getfuncs(&lf->cu_die, line_range_search_cb, ¶m, 0);
1772 return param.retval;
1775 int find_line_range(int fd, struct line_range *lr)
1777 struct line_finder lf = {.lr = lr, .found = 0};
1779 Dwarf_Off off = 0, noff;
1784 Dwarf_Addr bias; /* Currently ignored */
1785 const char *comp_dir;
1787 dbg = dwfl_init_offline_dwarf(fd, &dwfl, &bias);
1789 pr_warning("No debug information found in the vmlinux - "
1790 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
1794 /* Loop on CUs (Compilation Unit) */
1795 while (!lf.found && ret >= 0) {
1796 if (dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL) != 0)
1799 /* Get the DIE(Debugging Information Entry) of this CU */
1800 diep = dwarf_offdie(dbg, off + cuhl, &lf.cu_die);
1804 /* Check if target file is included. */
1806 lf.fname = cu_find_realpath(&lf.cu_die, lr->file);
1810 if (!lr->file || lf.fname) {
1812 ret = find_line_range_by_func(&lf);
1814 lf.lno_s = lr->start;
1816 ret = find_line_range_by_line(NULL, &lf);
1822 /* Store comp_dir */
1824 comp_dir = cu_get_comp_dir(&lf.cu_die);
1826 lr->comp_dir = strdup(comp_dir);
1832 pr_debug("path: %s\n", lr->path);
1834 return (ret < 0) ? ret : lf.found;