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>
32 #include <dwarf-regs.h>
34 #include <linux/bitops.h>
41 #include "probe-finder.h"
42 #include "probe-file.h"
44 /* Kprobe tracer basic type is up to u64 */
45 #define MAX_BASIC_TYPE_BITS 64
47 /* Dwarf FL wrappers */
48 static char *debuginfo_path; /* Currently dummy */
50 static const Dwfl_Callbacks offline_callbacks = {
51 .find_debuginfo = dwfl_standard_find_debuginfo,
52 .debuginfo_path = &debuginfo_path,
54 .section_address = dwfl_offline_section_address,
56 /* We use this table for core files too. */
57 .find_elf = dwfl_build_id_find_elf,
60 /* Get a Dwarf from offline image */
61 static int debuginfo__init_offline_dwarf(struct debuginfo *dbg,
66 fd = open(path, O_RDONLY);
70 dbg->dwfl = dwfl_begin(&offline_callbacks);
74 dwfl_report_begin(dbg->dwfl);
75 dbg->mod = dwfl_report_offline(dbg->dwfl, "", "", fd);
79 dbg->dbg = dwfl_module_getdwarf(dbg->mod, &dbg->bias);
83 dwfl_report_end(dbg->dwfl, NULL, NULL);
91 memset(dbg, 0, sizeof(*dbg));
96 static struct debuginfo *__debuginfo__new(const char *path)
98 struct debuginfo *dbg = zalloc(sizeof(*dbg));
102 if (debuginfo__init_offline_dwarf(dbg, path) < 0)
105 pr_debug("Open Debuginfo file: %s\n", path);
109 enum dso_binary_type distro_dwarf_types[] = {
110 DSO_BINARY_TYPE__FEDORA_DEBUGINFO,
111 DSO_BINARY_TYPE__UBUNTU_DEBUGINFO,
112 DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO,
113 DSO_BINARY_TYPE__BUILDID_DEBUGINFO,
114 DSO_BINARY_TYPE__NOT_FOUND,
117 struct debuginfo *debuginfo__new(const char *path)
119 enum dso_binary_type *type;
120 char buf[PATH_MAX], nil = '\0';
122 struct debuginfo *dinfo = NULL;
124 /* Try to open distro debuginfo files */
125 dso = dso__new(path);
129 for (type = distro_dwarf_types;
130 !dinfo && *type != DSO_BINARY_TYPE__NOT_FOUND;
132 if (dso__read_binary_type_filename(dso, *type, &nil,
135 dinfo = __debuginfo__new(buf);
140 /* if failed to open all distro debuginfo, open given binary */
141 return dinfo ? : __debuginfo__new(path);
144 void debuginfo__delete(struct debuginfo *dbg)
154 * Probe finder related functions
157 static struct probe_trace_arg_ref *alloc_trace_arg_ref(long offs)
159 struct probe_trace_arg_ref *ref;
160 ref = zalloc(sizeof(struct probe_trace_arg_ref));
167 * Convert a location into trace_arg.
168 * If tvar == NULL, this just checks variable can be converted.
169 * If fentry == true and vr_die is a parameter, do huristic search
170 * for the location fuzzed by function entry mcount.
172 static int convert_variable_location(Dwarf_Die *vr_die, Dwarf_Addr addr,
173 Dwarf_Op *fb_ops, Dwarf_Die *sp_die,
174 struct probe_trace_arg *tvar)
176 Dwarf_Attribute attr;
186 if (dwarf_attr(vr_die, DW_AT_external, &attr) != NULL)
189 /* TODO: handle more than 1 exprs */
190 if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL)
191 return -EINVAL; /* Broken DIE ? */
192 if (dwarf_getlocation_addr(&attr, addr, &op, &nops, 1) <= 0) {
193 ret = dwarf_entrypc(sp_die, &tmp);
197 if (probe_conf.show_location_range &&
198 (dwarf_tag(vr_die) == DW_TAG_variable)) {
200 } else if (addr != tmp ||
201 dwarf_tag(vr_die) != DW_TAG_formal_parameter) {
205 ret = dwarf_highpc(sp_die, &tmp);
209 * This is fuzzed by fentry mcount. We try to find the
210 * parameter location at the earliest address.
212 for (addr += 1; addr <= tmp; addr++) {
213 if (dwarf_getlocation_addr(&attr, addr, &op,
221 /* TODO: Support const_value */
224 if (op->atom == DW_OP_addr) {
228 /* Static variables on memory (not stack), make @varname */
229 ret = strlen(dwarf_diename(vr_die));
230 tvar->value = zalloc(ret + 2);
231 if (tvar->value == NULL)
233 snprintf(tvar->value, ret + 2, "@%s", dwarf_diename(vr_die));
234 tvar->ref = alloc_trace_arg_ref((long)offs);
235 if (tvar->ref == NULL)
240 /* If this is based on frame buffer, set the offset */
241 if (op->atom == DW_OP_fbreg) {
249 if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) {
250 regn = op->atom - DW_OP_breg0;
253 } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) {
254 regn = op->atom - DW_OP_reg0;
255 } else if (op->atom == DW_OP_bregx) {
259 } else if (op->atom == DW_OP_regx) {
262 pr_debug("DW_OP %x is not supported.\n", op->atom);
269 regs = get_arch_regstr(regn);
271 /* This should be a bug in DWARF or this tool */
272 pr_warning("Mapping for the register number %u "
273 "missing on this architecture.\n", regn);
277 tvar->value = strdup(regs);
278 if (tvar->value == NULL)
282 tvar->ref = alloc_trace_arg_ref((long)offs);
283 if (tvar->ref == NULL)
289 #define BYTES_TO_BITS(nb) ((nb) * BITS_PER_LONG / sizeof(long))
291 static int convert_variable_type(Dwarf_Die *vr_die,
292 struct probe_trace_arg *tvar,
295 struct probe_trace_arg_ref **ref_ptr = &tvar->ref;
298 char sbuf[STRERR_BUFSIZE];
299 int bsize, boffs, total;
303 /* TODO: check all types */
304 if (cast && strcmp(cast, "string") != 0 && strcmp(cast, "x") != 0 &&
305 strcmp(cast, "s") != 0 && strcmp(cast, "u") != 0) {
306 /* Non string type is OK */
307 /* and respect signedness/hexadecimal cast */
308 tvar->type = strdup(cast);
309 return (tvar->type == NULL) ? -ENOMEM : 0;
312 bsize = dwarf_bitsize(vr_die);
314 /* This is a bitfield */
315 boffs = dwarf_bitoffset(vr_die);
316 total = dwarf_bytesize(vr_die);
317 if (boffs < 0 || total < 0)
319 ret = snprintf(buf, 16, "b%d@%d/%zd", bsize, boffs,
320 BYTES_TO_BITS(total));
324 if (die_get_real_type(vr_die, &type) == NULL) {
325 pr_warning("Failed to get a type information of %s.\n",
326 dwarf_diename(vr_die));
330 pr_debug("%s type is %s.\n",
331 dwarf_diename(vr_die), dwarf_diename(&type));
333 if (cast && strcmp(cast, "string") == 0) { /* String type */
334 ret = dwarf_tag(&type);
335 if (ret != DW_TAG_pointer_type &&
336 ret != DW_TAG_array_type) {
337 pr_warning("Failed to cast into string: "
338 "%s(%s) is not a pointer nor array.\n",
339 dwarf_diename(vr_die), dwarf_diename(&type));
342 if (die_get_real_type(&type, &type) == NULL) {
343 pr_warning("Failed to get a type"
347 if (ret == DW_TAG_pointer_type) {
349 ref_ptr = &(*ref_ptr)->next;
350 /* Add new reference with offset +0 */
351 *ref_ptr = zalloc(sizeof(struct probe_trace_arg_ref));
352 if (*ref_ptr == NULL) {
353 pr_warning("Out of memory error\n");
357 if (!die_compare_name(&type, "char") &&
358 !die_compare_name(&type, "unsigned char")) {
359 pr_warning("Failed to cast into string: "
360 "%s is not (unsigned) char *.\n",
361 dwarf_diename(vr_die));
364 tvar->type = strdup(cast);
365 return (tvar->type == NULL) ? -ENOMEM : 0;
368 if (cast && (strcmp(cast, "u") == 0))
370 else if (cast && (strcmp(cast, "s") == 0))
372 else if (cast && (strcmp(cast, "x") == 0) &&
373 probe_type_is_available(PROBE_TYPE_X))
376 prefix = die_is_signed_type(&type) ? 's' : 'u';
378 ret = dwarf_bytesize(&type);
380 /* No size ... try to use default type */
382 ret = BYTES_TO_BITS(ret);
384 /* Check the bitwidth */
385 if (ret > MAX_BASIC_TYPE_BITS) {
386 pr_info("%s exceeds max-bitwidth. Cut down to %d bits.\n",
387 dwarf_diename(&type), MAX_BASIC_TYPE_BITS);
388 ret = MAX_BASIC_TYPE_BITS;
390 ret = snprintf(buf, 16, "%c%d", prefix, ret);
393 if (ret < 0 || ret >= 16) {
396 pr_warning("Failed to convert variable type: %s\n",
397 str_error_r(-ret, sbuf, sizeof(sbuf)));
400 tvar->type = strdup(buf);
401 if (tvar->type == NULL)
406 static int convert_variable_fields(Dwarf_Die *vr_die, const char *varname,
407 struct perf_probe_arg_field *field,
408 struct probe_trace_arg_ref **ref_ptr,
411 struct probe_trace_arg_ref *ref = *ref_ptr;
416 pr_debug("converting %s in %s\n", field->name, varname);
417 if (die_get_real_type(vr_die, &type) == NULL) {
418 pr_warning("Failed to get the type of %s.\n", varname);
421 pr_debug2("Var real type: (%x)\n", (unsigned)dwarf_dieoffset(&type));
422 tag = dwarf_tag(&type);
424 if (field->name[0] == '[' &&
425 (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type)) {
427 /* Save original type for next field */
428 memcpy(die_mem, &type, sizeof(*die_mem));
429 /* Get the type of this array */
430 if (die_get_real_type(&type, &type) == NULL) {
431 pr_warning("Failed to get the type of %s.\n", varname);
434 pr_debug2("Array real type: (%x)\n",
435 (unsigned)dwarf_dieoffset(&type));
436 if (tag == DW_TAG_pointer_type) {
437 ref = zalloc(sizeof(struct probe_trace_arg_ref));
441 (*ref_ptr)->next = ref;
445 ref->offset += dwarf_bytesize(&type) * field->index;
447 /* Save vr_die for converting types */
448 memcpy(die_mem, vr_die, sizeof(*die_mem));
450 } else if (tag == DW_TAG_pointer_type) {
451 /* Check the pointer and dereference */
453 pr_err("Semantic error: %s must be referred by '->'\n",
457 /* Get the type pointed by this pointer */
458 if (die_get_real_type(&type, &type) == NULL) {
459 pr_warning("Failed to get the type of %s.\n", varname);
462 /* Verify it is a data structure */
463 tag = dwarf_tag(&type);
464 if (tag != DW_TAG_structure_type && tag != DW_TAG_union_type) {
465 pr_warning("%s is not a data structure nor an union.\n",
470 ref = zalloc(sizeof(struct probe_trace_arg_ref));
474 (*ref_ptr)->next = ref;
478 /* Verify it is a data structure */
479 if (tag != DW_TAG_structure_type && tag != DW_TAG_union_type) {
480 pr_warning("%s is not a data structure nor an union.\n",
484 if (field->name[0] == '[') {
485 pr_err("Semantic error: %s is not a pointer"
486 " nor array.\n", varname);
489 /* While prcessing unnamed field, we don't care about this */
490 if (field->ref && dwarf_diename(vr_die)) {
491 pr_err("Semantic error: %s must be referred by '.'\n",
496 pr_warning("Structure on a register is not "
502 if (die_find_member(&type, field->name, die_mem) == NULL) {
503 pr_warning("%s(type:%s) has no member %s.\n", varname,
504 dwarf_diename(&type), field->name);
508 /* Get the offset of the field */
509 if (tag == DW_TAG_union_type) {
512 ret = die_get_data_member_location(die_mem, &offs);
514 pr_warning("Failed to get the offset of %s.\n",
519 ref->offset += (long)offs;
521 /* If this member is unnamed, we need to reuse this field */
522 if (!dwarf_diename(die_mem))
523 return convert_variable_fields(die_mem, varname, field,
527 /* Converting next field */
529 return convert_variable_fields(die_mem, field->name,
530 field->next, &ref, die_mem);
535 /* Show a variables in kprobe event format */
536 static int convert_variable(Dwarf_Die *vr_die, struct probe_finder *pf)
541 pr_debug("Converting variable %s into trace event.\n",
542 dwarf_diename(vr_die));
544 ret = convert_variable_location(vr_die, pf->addr, pf->fb_ops,
545 &pf->sp_die, pf->tvar);
546 if (ret == -ENOENT || ret == -EINVAL) {
547 pr_err("Failed to find the location of the '%s' variable at this address.\n"
548 " Perhaps it has been optimized out.\n"
549 " Use -V with the --range option to show '%s' location range.\n",
550 pf->pvar->var, pf->pvar->var);
551 } else if (ret == -ENOTSUP)
552 pr_err("Sorry, we don't support this variable location yet.\n");
553 else if (ret == 0 && pf->pvar->field) {
554 ret = convert_variable_fields(vr_die, pf->pvar->var,
555 pf->pvar->field, &pf->tvar->ref,
560 ret = convert_variable_type(vr_die, pf->tvar, pf->pvar->type);
561 /* *expr will be cached in libdw. Don't free it. */
565 /* Find a variable in a scope DIE */
566 static int find_variable(Dwarf_Die *sc_die, struct probe_finder *pf)
572 /* Copy raw parameters */
573 if (!is_c_varname(pf->pvar->var))
574 return copy_to_probe_trace_arg(pf->tvar, pf->pvar);
577 pf->tvar->name = strdup(pf->pvar->name);
579 buf = synthesize_perf_probe_arg(pf->pvar);
582 ptr = strchr(buf, ':'); /* Change type separator to _ */
585 pf->tvar->name = buf;
587 if (pf->tvar->name == NULL)
590 pr_debug("Searching '%s' variable in context.\n", pf->pvar->var);
591 /* Search child die for local variables and parameters. */
592 if (!die_find_variable_at(sc_die, pf->pvar->var, pf->addr, &vr_die)) {
593 /* Search again in global variables */
594 if (!die_find_variable_at(&pf->cu_die, pf->pvar->var,
596 pr_warning("Failed to find '%s' in this function.\n",
602 ret = convert_variable(&vr_die, pf);
607 /* Convert subprogram DIE to trace point */
608 static int convert_to_trace_point(Dwarf_Die *sp_die, Dwfl_Module *mod,
609 Dwarf_Addr paddr, bool retprobe,
610 const char *function,
611 struct probe_trace_point *tp)
613 Dwarf_Addr eaddr, highaddr;
617 /* Verify the address is correct */
618 if (dwarf_entrypc(sp_die, &eaddr) != 0) {
619 pr_warning("Failed to get entry address of %s\n",
620 dwarf_diename(sp_die));
623 if (dwarf_highpc(sp_die, &highaddr) != 0) {
624 pr_warning("Failed to get end address of %s\n",
625 dwarf_diename(sp_die));
628 if (paddr > highaddr) {
629 pr_warning("Offset specified is greater than size of %s\n",
630 dwarf_diename(sp_die));
634 symbol = dwarf_diename(sp_die);
636 /* Try to get the symbol name from symtab */
637 symbol = dwfl_module_addrsym(mod, paddr, &sym, NULL);
639 pr_warning("Failed to find symbol at 0x%lx\n",
640 (unsigned long)paddr);
643 eaddr = sym.st_value;
645 tp->offset = (unsigned long)(paddr - eaddr);
646 tp->address = (unsigned long)paddr;
647 tp->symbol = strdup(symbol);
651 /* Return probe must be on the head of a subprogram */
653 if (eaddr != paddr) {
654 pr_warning("Failed to find \"%s%%return\",\n"
655 " because %s is an inlined function and"
656 " has no return point.\n", function,
666 /* Call probe_finder callback with scope DIE */
667 static int call_probe_finder(Dwarf_Die *sc_die, struct probe_finder *pf)
669 Dwarf_Attribute fb_attr;
670 Dwarf_Frame *frame = NULL;
675 pr_err("Caller must pass a scope DIE. Program error.\n");
679 /* If not a real subprogram, find a real one */
680 if (!die_is_func_def(sc_die)) {
681 if (!die_find_realfunc(&pf->cu_die, pf->addr, &pf->sp_die)) {
682 if (die_find_tailfunc(&pf->cu_die, pf->addr, &pf->sp_die)) {
683 pr_warning("Ignoring tail call from %s\n",
684 dwarf_diename(&pf->sp_die));
687 pr_warning("Failed to find probe point in any "
693 memcpy(&pf->sp_die, sc_die, sizeof(Dwarf_Die));
695 /* Get the frame base attribute/ops from subprogram */
696 dwarf_attr(&pf->sp_die, DW_AT_frame_base, &fb_attr);
697 ret = dwarf_getlocation_addr(&fb_attr, pf->addr, &pf->fb_ops, &nops, 1);
698 if (ret <= 0 || nops == 0) {
700 #if _ELFUTILS_PREREQ(0, 142)
701 } else if (nops == 1 && pf->fb_ops[0].atom == DW_OP_call_frame_cfa &&
702 (pf->cfi_eh != NULL || pf->cfi_dbg != NULL)) {
703 if ((dwarf_cfi_addrframe(pf->cfi_eh, pf->addr, &frame) != 0 &&
704 (dwarf_cfi_addrframe(pf->cfi_dbg, pf->addr, &frame) != 0)) ||
705 dwarf_frame_cfa(frame, &pf->fb_ops, &nops) != 0) {
706 pr_warning("Failed to get call frame on 0x%jx\n",
707 (uintmax_t)pf->addr);
714 /* Call finder's callback handler */
715 ret = pf->callback(sc_die, pf);
717 /* Since *pf->fb_ops can be a part of frame. we should free it here. */
724 struct find_scope_param {
725 const char *function;
733 static int find_best_scope_cb(Dwarf_Die *fn_die, void *data)
735 struct find_scope_param *fsp = data;
739 /* Skip if declared file name does not match */
741 file = dwarf_decl_file(fn_die);
742 if (!file || strcmp(fsp->file, file) != 0)
745 /* If the function name is given, that's what user expects */
747 if (die_match_name(fn_die, fsp->function)) {
748 memcpy(fsp->die_mem, fn_die, sizeof(Dwarf_Die));
753 /* With the line number, find the nearest declared DIE */
754 dwarf_decl_line(fn_die, &lno);
755 if (lno < fsp->line && fsp->diff > fsp->line - lno) {
756 /* Keep a candidate and continue */
757 fsp->diff = fsp->line - lno;
758 memcpy(fsp->die_mem, fn_die, sizeof(Dwarf_Die));
765 /* Find an appropriate scope fits to given conditions */
766 static Dwarf_Die *find_best_scope(struct probe_finder *pf, Dwarf_Die *die_mem)
768 struct find_scope_param fsp = {
769 .function = pf->pev->point.function,
777 cu_walk_functions_at(&pf->cu_die, pf->addr, find_best_scope_cb, &fsp);
779 return fsp.found ? die_mem : NULL;
782 static int probe_point_line_walker(const char *fname, int lineno,
783 Dwarf_Addr addr, void *data)
785 struct probe_finder *pf = data;
786 Dwarf_Die *sc_die, die_mem;
789 if (lineno != pf->lno || strtailcmp(fname, pf->fname) != 0)
793 sc_die = find_best_scope(pf, &die_mem);
795 pr_warning("Failed to find scope of probe point.\n");
799 ret = call_probe_finder(sc_die, pf);
801 /* Continue if no error, because the line will be in inline function */
802 return ret < 0 ? ret : 0;
805 /* Find probe point from its line number */
806 static int find_probe_point_by_line(struct probe_finder *pf)
808 return die_walk_lines(&pf->cu_die, probe_point_line_walker, pf);
811 /* Find lines which match lazy pattern */
812 static int find_lazy_match_lines(struct intlist *list,
813 const char *fname, const char *pat)
819 int count = 0, linenum = 1;
820 char sbuf[STRERR_BUFSIZE];
822 fp = fopen(fname, "r");
824 pr_warning("Failed to open %s: %s\n", fname,
825 str_error_r(errno, sbuf, sizeof(sbuf)));
829 while ((len = getline(&line, &line_len, fp)) > 0) {
831 if (line[len - 1] == '\n')
832 line[len - 1] = '\0';
834 if (strlazymatch(line, pat)) {
835 intlist__add(list, linenum);
847 pr_debug("No matched lines found in %s.\n", fname);
851 static int probe_point_lazy_walker(const char *fname, int lineno,
852 Dwarf_Addr addr, void *data)
854 struct probe_finder *pf = data;
855 Dwarf_Die *sc_die, die_mem;
858 if (!intlist__has_entry(pf->lcache, lineno) ||
859 strtailcmp(fname, pf->fname) != 0)
862 pr_debug("Probe line found: line:%d addr:0x%llx\n",
863 lineno, (unsigned long long)addr);
866 sc_die = find_best_scope(pf, &die_mem);
868 pr_warning("Failed to find scope of probe point.\n");
872 ret = call_probe_finder(sc_die, pf);
875 * Continue if no error, because the lazy pattern will match
878 return ret < 0 ? ret : 0;
881 /* Find probe points from lazy pattern */
882 static int find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf)
887 if (intlist__empty(pf->lcache)) {
888 const char *comp_dir;
890 comp_dir = cu_get_comp_dir(&pf->cu_die);
891 ret = get_real_path(pf->fname, comp_dir, &fpath);
893 pr_warning("Failed to find source file path.\n");
897 /* Matching lazy line pattern */
898 ret = find_lazy_match_lines(pf->lcache, fpath,
899 pf->pev->point.lazy_line);
905 return die_walk_lines(sp_die, probe_point_lazy_walker, pf);
908 static int probe_point_inline_cb(Dwarf_Die *in_die, void *data)
910 struct probe_finder *pf = data;
911 struct perf_probe_point *pp = &pf->pev->point;
916 ret = find_probe_point_lazy(in_die, pf);
918 /* Get probe address */
919 if (dwarf_entrypc(in_die, &addr) != 0) {
920 pr_warning("Failed to get entry address of %s.\n",
921 dwarf_diename(in_die));
925 pf->addr += pp->offset;
926 pr_debug("found inline addr: 0x%jx\n",
927 (uintmax_t)pf->addr);
929 ret = call_probe_finder(in_die, pf);
935 /* Callback parameter with return value for libdw */
936 struct dwarf_callback_param {
941 /* Search function from function name */
942 static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
944 struct dwarf_callback_param *param = data;
945 struct probe_finder *pf = param->data;
946 struct perf_probe_point *pp = &pf->pev->point;
948 /* Check tag and diename */
949 if (!die_is_func_def(sp_die) ||
950 !die_match_name(sp_die, pp->function))
953 /* Check declared file */
954 if (pp->file && strtailcmp(pp->file, dwarf_decl_file(sp_die)))
957 pr_debug("Matched function: %s\n", dwarf_diename(sp_die));
958 pf->fname = dwarf_decl_file(sp_die);
959 if (pp->line) { /* Function relative line */
960 dwarf_decl_line(sp_die, &pf->lno);
962 param->retval = find_probe_point_by_line(pf);
963 } else if (die_is_func_instance(sp_die)) {
964 /* Instances always have the entry address */
965 dwarf_entrypc(sp_die, &pf->addr);
968 param->retval = find_probe_point_lazy(sp_die, pf);
970 pf->addr += pp->offset;
971 /* TODO: Check the address in this function */
972 param->retval = call_probe_finder(sp_die, pf);
974 } else if (!probe_conf.no_inlines) {
975 /* Inlined function: search instances */
976 param->retval = die_walk_instances(sp_die,
977 probe_point_inline_cb, (void *)pf);
978 /* This could be a non-existed inline definition */
979 if (param->retval == -ENOENT && strisglob(pp->function))
983 /* We need to find other candidates */
984 if (strisglob(pp->function) && param->retval >= 0) {
985 param->retval = 0; /* We have to clear the result */
989 return DWARF_CB_ABORT; /* Exit; no same symbol in this CU. */
992 static int find_probe_point_by_func(struct probe_finder *pf)
994 struct dwarf_callback_param _param = {.data = (void *)pf,
996 dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, &_param, 0);
997 return _param.retval;
1000 struct pubname_callback_param {
1008 static int pubname_search_cb(Dwarf *dbg, Dwarf_Global *gl, void *data)
1010 struct pubname_callback_param *param = data;
1012 if (dwarf_offdie(dbg, gl->die_offset, param->sp_die)) {
1013 if (dwarf_tag(param->sp_die) != DW_TAG_subprogram)
1016 if (die_match_name(param->sp_die, param->function)) {
1017 if (!dwarf_offdie(dbg, gl->cu_offset, param->cu_die))
1021 strtailcmp(param->file, dwarf_decl_file(param->sp_die)))
1025 return DWARF_CB_ABORT;
1032 static int debuginfo__find_probe_location(struct debuginfo *dbg,
1033 struct probe_finder *pf)
1035 struct perf_probe_point *pp = &pf->pev->point;
1036 Dwarf_Off off, noff;
1042 pf->lcache = intlist__new(NULL);
1046 /* Fastpath: lookup by function name from .debug_pubnames section */
1047 if (pp->function && !strisglob(pp->function)) {
1048 struct pubname_callback_param pubname_param = {
1049 .function = pp->function,
1051 .cu_die = &pf->cu_die,
1052 .sp_die = &pf->sp_die,
1055 struct dwarf_callback_param probe_param = {
1059 dwarf_getpubnames(dbg->dbg, pubname_search_cb,
1061 if (pubname_param.found) {
1062 ret = probe_point_search_cb(&pf->sp_die, &probe_param);
1068 /* Loop on CUs (Compilation Unit) */
1069 while (!dwarf_nextcu(dbg->dbg, off, &noff, &cuhl, NULL, NULL, NULL)) {
1070 /* Get the DIE(Debugging Information Entry) of this CU */
1071 diep = dwarf_offdie(dbg->dbg, off + cuhl, &pf->cu_die);
1075 /* Check if target file is included. */
1077 pf->fname = cu_find_realpath(&pf->cu_die, pp->file);
1081 if (!pp->file || pf->fname) {
1083 ret = find_probe_point_by_func(pf);
1084 else if (pp->lazy_line)
1085 ret = find_probe_point_lazy(&pf->cu_die, pf);
1088 ret = find_probe_point_by_line(pf);
1097 intlist__delete(pf->lcache);
1103 /* Find probe points from debuginfo */
1104 static int debuginfo__find_probes(struct debuginfo *dbg,
1105 struct probe_finder *pf)
1109 #if _ELFUTILS_PREREQ(0, 142)
1114 if (pf->cfi_eh || pf->cfi_dbg)
1115 return debuginfo__find_probe_location(dbg, pf);
1117 /* Get the call frame information from this dwarf */
1118 elf = dwarf_getelf(dbg->dbg);
1122 if (gelf_getehdr(elf, &ehdr) == NULL)
1125 if (elf_section_by_name(elf, &ehdr, &shdr, ".eh_frame", NULL) &&
1126 shdr.sh_type == SHT_PROGBITS)
1127 pf->cfi_eh = dwarf_getcfi_elf(elf);
1129 pf->cfi_dbg = dwarf_getcfi(dbg->dbg);
1132 ret = debuginfo__find_probe_location(dbg, pf);
1136 struct local_vars_finder {
1137 struct probe_finder *pf;
1138 struct perf_probe_arg *args;
1145 /* Collect available variables in this scope */
1146 static int copy_variables_cb(Dwarf_Die *die_mem, void *data)
1148 struct local_vars_finder *vf = data;
1149 struct probe_finder *pf = vf->pf;
1152 tag = dwarf_tag(die_mem);
1153 if (tag == DW_TAG_formal_parameter ||
1154 (tag == DW_TAG_variable && vf->vars)) {
1155 if (convert_variable_location(die_mem, vf->pf->addr,
1156 vf->pf->fb_ops, &pf->sp_die,
1158 vf->args[vf->nargs].var = (char *)dwarf_diename(die_mem);
1159 if (vf->args[vf->nargs].var == NULL) {
1161 return DIE_FIND_CB_END;
1163 pr_debug(" %s", vf->args[vf->nargs].var);
1168 if (dwarf_haspc(die_mem, vf->pf->addr))
1169 return DIE_FIND_CB_CONTINUE;
1171 return DIE_FIND_CB_SIBLING;
1174 static int expand_probe_args(Dwarf_Die *sc_die, struct probe_finder *pf,
1175 struct perf_probe_arg *args)
1180 struct local_vars_finder vf = {.pf = pf, .args = args, .vars = false,
1181 .max_args = MAX_PROBE_ARGS, .ret = 0};
1183 for (i = 0; i < pf->pev->nargs; i++) {
1184 /* var never be NULL */
1185 if (strcmp(pf->pev->args[i].var, PROBE_ARG_VARS) == 0)
1187 else if (strcmp(pf->pev->args[i].var, PROBE_ARG_PARAMS) != 0) {
1188 /* Copy normal argument */
1189 args[n] = pf->pev->args[i];
1193 pr_debug("Expanding %s into:", pf->pev->args[i].var);
1195 /* Special local variables */
1196 die_find_child(sc_die, copy_variables_cb, (void *)&vf,
1198 pr_debug(" (%d)\n", vf.nargs - n);
1206 /* Add a found probe point into trace event list */
1207 static int add_probe_trace_event(Dwarf_Die *sc_die, struct probe_finder *pf)
1209 struct trace_event_finder *tf =
1210 container_of(pf, struct trace_event_finder, pf);
1211 struct perf_probe_point *pp = &pf->pev->point;
1212 struct probe_trace_event *tev;
1213 struct perf_probe_arg *args = NULL;
1216 /* Check number of tevs */
1217 if (tf->ntevs == tf->max_tevs) {
1218 pr_warning("Too many( > %d) probe point found.\n",
1222 tev = &tf->tevs[tf->ntevs++];
1224 /* Trace point should be converted from subprogram DIE */
1225 ret = convert_to_trace_point(&pf->sp_die, tf->mod, pf->addr,
1226 pp->retprobe, pp->function, &tev->point);
1230 tev->point.realname = strdup(dwarf_diename(sc_die));
1231 if (!tev->point.realname) {
1236 pr_debug("Probe point found: %s+%lu\n", tev->point.symbol,
1239 /* Expand special probe argument if exist */
1240 args = zalloc(sizeof(struct perf_probe_arg) * MAX_PROBE_ARGS);
1246 ret = expand_probe_args(sc_die, pf, args);
1251 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
1252 if (tev->args == NULL) {
1257 /* Find each argument */
1258 for (i = 0; i < tev->nargs; i++) {
1259 pf->pvar = &args[i];
1260 pf->tvar = &tev->args[i];
1261 /* Variable should be found from scope DIE */
1262 ret = find_variable(sc_die, pf);
1269 clear_probe_trace_event(tev);
1276 /* Find probe_trace_events specified by perf_probe_event from debuginfo */
1277 int debuginfo__find_trace_events(struct debuginfo *dbg,
1278 struct perf_probe_event *pev,
1279 struct probe_trace_event **tevs)
1281 struct trace_event_finder tf = {
1282 .pf = {.pev = pev, .callback = add_probe_trace_event},
1283 .max_tevs = probe_conf.max_probes, .mod = dbg->mod};
1286 /* Allocate result tevs array */
1287 *tevs = zalloc(sizeof(struct probe_trace_event) * tf.max_tevs);
1294 ret = debuginfo__find_probes(dbg, &tf.pf);
1296 for (i = 0; i < tf.ntevs; i++)
1297 clear_probe_trace_event(&tf.tevs[i]);
1302 return (ret < 0) ? ret : tf.ntevs;
1305 /* Collect available variables in this scope */
1306 static int collect_variables_cb(Dwarf_Die *die_mem, void *data)
1308 struct available_var_finder *af = data;
1309 struct variable_list *vl;
1310 struct strbuf buf = STRBUF_INIT;
1313 vl = &af->vls[af->nvls - 1];
1315 tag = dwarf_tag(die_mem);
1316 if (tag == DW_TAG_formal_parameter ||
1317 tag == DW_TAG_variable) {
1318 ret = convert_variable_location(die_mem, af->pf.addr,
1319 af->pf.fb_ops, &af->pf.sp_die,
1321 if (ret == 0 || ret == -ERANGE) {
1323 bool externs = !af->child;
1325 if (strbuf_init(&buf, 64) < 0)
1328 if (probe_conf.show_location_range) {
1330 ret2 = strbuf_add(&buf,
1331 ret ? "[INV]\t" : "[VAL]\t", 6);
1333 ret2 = strbuf_add(&buf, "[EXT]\t", 6);
1338 ret2 = die_get_varname(die_mem, &buf);
1340 if (!ret2 && probe_conf.show_location_range &&
1342 if (strbuf_addch(&buf, '\t') < 0)
1344 ret2 = die_get_var_range(&af->pf.sp_die,
1348 pr_debug("Add new var: %s\n", buf.buf);
1350 strlist__add(vl->vars,
1351 strbuf_detach(&buf, NULL));
1353 strbuf_release(&buf);
1357 if (af->child && dwarf_haspc(die_mem, af->pf.addr))
1358 return DIE_FIND_CB_CONTINUE;
1360 return DIE_FIND_CB_SIBLING;
1362 strbuf_release(&buf);
1363 pr_debug("Error in strbuf\n");
1364 return DIE_FIND_CB_END;
1367 /* Add a found vars into available variables list */
1368 static int add_available_vars(Dwarf_Die *sc_die, struct probe_finder *pf)
1370 struct available_var_finder *af =
1371 container_of(pf, struct available_var_finder, pf);
1372 struct perf_probe_point *pp = &pf->pev->point;
1373 struct variable_list *vl;
1377 /* Check number of tevs */
1378 if (af->nvls == af->max_vls) {
1379 pr_warning("Too many( > %d) probe point found.\n", af->max_vls);
1382 vl = &af->vls[af->nvls++];
1384 /* Trace point should be converted from subprogram DIE */
1385 ret = convert_to_trace_point(&pf->sp_die, af->mod, pf->addr,
1386 pp->retprobe, pp->function, &vl->point);
1390 pr_debug("Probe point found: %s+%lu\n", vl->point.symbol,
1393 /* Find local variables */
1394 vl->vars = strlist__new(NULL, NULL);
1395 if (vl->vars == NULL)
1398 die_find_child(sc_die, collect_variables_cb, (void *)af, &die_mem);
1400 /* Find external variables */
1401 if (!probe_conf.show_ext_vars)
1403 /* Don't need to search child DIE for external vars. */
1405 die_find_child(&pf->cu_die, collect_variables_cb, (void *)af, &die_mem);
1408 if (strlist__empty(vl->vars)) {
1409 strlist__delete(vl->vars);
1417 * Find available variables at given probe point
1418 * Return the number of found probe points. Return 0 if there is no
1419 * matched probe point. Return <0 if an error occurs.
1421 int debuginfo__find_available_vars_at(struct debuginfo *dbg,
1422 struct perf_probe_event *pev,
1423 struct variable_list **vls)
1425 struct available_var_finder af = {
1426 .pf = {.pev = pev, .callback = add_available_vars},
1428 .max_vls = probe_conf.max_probes};
1431 /* Allocate result vls array */
1432 *vls = zalloc(sizeof(struct variable_list) * af.max_vls);
1439 ret = debuginfo__find_probes(dbg, &af.pf);
1441 /* Free vlist for error */
1443 zfree(&af.vls[af.nvls].point.symbol);
1444 strlist__delete(af.vls[af.nvls].vars);
1450 return (ret < 0) ? ret : af.nvls;
1453 /* For the kernel module, we need a special code to get a DIE */
1454 static int debuginfo__get_text_offset(struct debuginfo *dbg, Dwarf_Addr *offs)
1460 GElf_Shdr mem, *shdr;
1463 elf = dwfl_module_getelf(dbg->mod, &dbg->bias);
1467 /* Get the number of relocations */
1468 n = dwfl_module_relocations(dbg->mod);
1471 /* Search the relocation related .text section */
1472 for (i = 0; i < n; i++) {
1473 p = dwfl_module_relocation_info(dbg->mod, i, &shndx);
1474 if (strcmp(p, ".text") == 0) {
1475 /* OK, get the section header */
1476 scn = elf_getscn(elf, shndx);
1479 shdr = gelf_getshdr(scn, &mem);
1482 *offs = shdr->sh_addr;
1488 /* Reverse search */
1489 int debuginfo__find_probe_point(struct debuginfo *dbg, unsigned long addr,
1490 struct perf_probe_point *ppt)
1492 Dwarf_Die cudie, spdie, indie;
1493 Dwarf_Addr _addr = 0, baseaddr = 0;
1494 const char *fname = NULL, *func = NULL, *basefunc = NULL, *tmp;
1495 int baseline = 0, lineno = 0, ret = 0;
1500 if (!dwarf_addrdie(dbg->dbg, (Dwarf_Addr)addr, &cudie)) {
1501 if (!reloc && debuginfo__get_text_offset(dbg, &baseaddr) == 0) {
1506 pr_warning("Failed to find debug information for address %lx\n",
1512 /* Find a corresponding line (filename and lineno) */
1513 cu_find_lineinfo(&cudie, addr, &fname, &lineno);
1514 /* Don't care whether it failed or not */
1516 /* Find a corresponding function (name, baseline and baseaddr) */
1517 if (die_find_realfunc(&cudie, (Dwarf_Addr)addr, &spdie)) {
1518 /* Get function entry information */
1519 func = basefunc = dwarf_diename(&spdie);
1521 dwarf_entrypc(&spdie, &baseaddr) != 0 ||
1522 dwarf_decl_line(&spdie, &baseline) != 0) {
1527 fname = dwarf_decl_file(&spdie);
1528 if (addr == (unsigned long)baseaddr) {
1529 /* Function entry - Relative line number is 0 */
1534 /* Track down the inline functions step by step */
1535 while (die_find_top_inlinefunc(&spdie, (Dwarf_Addr)addr,
1537 /* There is an inline function */
1538 if (dwarf_entrypc(&indie, &_addr) == 0 &&
1541 * addr is at an inline function entry.
1542 * In this case, lineno should be the call-site
1543 * line number. (overwrite lineinfo)
1545 lineno = die_get_call_lineno(&indie);
1546 fname = die_get_call_file(&indie);
1550 * addr is in an inline function body.
1551 * Since lineno points one of the lines
1552 * of the inline function, baseline should
1553 * be the entry line of the inline function.
1555 tmp = dwarf_diename(&indie);
1557 dwarf_decl_line(&indie, &baseline) != 0)
1563 /* Verify the lineno and baseline are in a same file */
1564 tmp = dwarf_decl_file(&spdie);
1565 if (!tmp || strcmp(tmp, fname) != 0)
1570 /* Make a relative line number or an offset */
1572 ppt->line = lineno - baseline;
1573 else if (basefunc) {
1574 ppt->offset = addr - (unsigned long)baseaddr;
1578 /* Duplicate strings */
1580 ppt->function = strdup(func);
1581 if (ppt->function == NULL) {
1587 ppt->file = strdup(fname);
1588 if (ppt->file == NULL) {
1589 zfree(&ppt->function);
1595 if (ret == 0 && (fname || func))
1596 ret = 1; /* Found a point */
1600 /* Add a line and store the src path */
1601 static int line_range_add_line(const char *src, unsigned int lineno,
1602 struct line_range *lr)
1604 /* Copy source path */
1606 lr->path = strdup(src);
1607 if (lr->path == NULL)
1610 return intlist__add(lr->line_list, lineno);
1613 static int line_range_walk_cb(const char *fname, int lineno,
1614 Dwarf_Addr addr __maybe_unused,
1617 struct line_finder *lf = data;
1620 if ((strtailcmp(fname, lf->fname) != 0) ||
1621 (lf->lno_s > lineno || lf->lno_e < lineno))
1624 err = line_range_add_line(fname, lineno, lf->lr);
1625 if (err < 0 && err != -EEXIST)
1631 /* Find line range from its line number */
1632 static int find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
1636 ret = die_walk_lines(sp_die ?: &lf->cu_die, line_range_walk_cb, lf);
1640 if (!intlist__empty(lf->lr->line_list))
1641 ret = lf->found = 1;
1643 ret = 0; /* Lines are not found */
1645 zfree(&lf->lr->path);
1650 static int line_range_inline_cb(Dwarf_Die *in_die, void *data)
1652 int ret = find_line_range_by_line(in_die, data);
1655 * We have to check all instances of inlined function, because
1656 * some execution paths can be optimized out depends on the
1657 * function argument of instances. However, if an error occurs,
1658 * it should be handled by the caller.
1660 return ret < 0 ? ret : 0;
1663 /* Search function definition from function name */
1664 static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
1666 struct dwarf_callback_param *param = data;
1667 struct line_finder *lf = param->data;
1668 struct line_range *lr = lf->lr;
1670 /* Check declared file */
1671 if (lr->file && strtailcmp(lr->file, dwarf_decl_file(sp_die)))
1674 if (die_is_func_def(sp_die) &&
1675 die_match_name(sp_die, lr->function)) {
1676 lf->fname = dwarf_decl_file(sp_die);
1677 dwarf_decl_line(sp_die, &lr->offset);
1678 pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
1679 lf->lno_s = lr->offset + lr->start;
1680 if (lf->lno_s < 0) /* Overflow */
1681 lf->lno_s = INT_MAX;
1682 lf->lno_e = lr->offset + lr->end;
1683 if (lf->lno_e < 0) /* Overflow */
1684 lf->lno_e = INT_MAX;
1685 pr_debug("New line range: %d to %d\n", lf->lno_s, lf->lno_e);
1686 lr->start = lf->lno_s;
1687 lr->end = lf->lno_e;
1688 if (!die_is_func_instance(sp_die))
1689 param->retval = die_walk_instances(sp_die,
1690 line_range_inline_cb, lf);
1692 param->retval = find_line_range_by_line(sp_die, lf);
1693 return DWARF_CB_ABORT;
1698 static int find_line_range_by_func(struct line_finder *lf)
1700 struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
1701 dwarf_getfuncs(&lf->cu_die, line_range_search_cb, ¶m, 0);
1702 return param.retval;
1705 int debuginfo__find_line_range(struct debuginfo *dbg, struct line_range *lr)
1707 struct line_finder lf = {.lr = lr, .found = 0};
1709 Dwarf_Off off = 0, noff;
1712 const char *comp_dir;
1714 /* Fastpath: lookup by function name from .debug_pubnames section */
1716 struct pubname_callback_param pubname_param = {
1717 .function = lr->function, .file = lr->file,
1718 .cu_die = &lf.cu_die, .sp_die = &lf.sp_die, .found = 0};
1719 struct dwarf_callback_param line_range_param = {
1720 .data = (void *)&lf, .retval = 0};
1722 dwarf_getpubnames(dbg->dbg, pubname_search_cb,
1724 if (pubname_param.found) {
1725 line_range_search_cb(&lf.sp_die, &line_range_param);
1731 /* Loop on CUs (Compilation Unit) */
1732 while (!lf.found && ret >= 0) {
1733 if (dwarf_nextcu(dbg->dbg, off, &noff, &cuhl,
1734 NULL, NULL, NULL) != 0)
1737 /* Get the DIE(Debugging Information Entry) of this CU */
1738 diep = dwarf_offdie(dbg->dbg, off + cuhl, &lf.cu_die);
1742 /* Check if target file is included. */
1744 lf.fname = cu_find_realpath(&lf.cu_die, lr->file);
1748 if (!lr->file || lf.fname) {
1750 ret = find_line_range_by_func(&lf);
1752 lf.lno_s = lr->start;
1754 ret = find_line_range_by_line(NULL, &lf);
1761 /* Store comp_dir */
1763 comp_dir = cu_get_comp_dir(&lf.cu_die);
1765 lr->comp_dir = strdup(comp_dir);
1771 pr_debug("path: %s\n", lr->path);
1772 return (ret < 0) ? ret : lf.found;
1776 * Find a src file from a DWARF tag path. Prepend optional source path prefix
1777 * and chop off leading directories that do not exist. Result is passed back as
1778 * a newly allocated path on success.
1779 * Return 0 if file was found and readable, -errno otherwise.
1781 int get_real_path(const char *raw_path, const char *comp_dir,
1784 const char *prefix = symbol_conf.source_prefix;
1787 if (raw_path[0] != '/' && comp_dir)
1788 /* If not an absolute path, try to use comp_dir */
1791 if (access(raw_path, R_OK) == 0) {
1792 *new_path = strdup(raw_path);
1793 return *new_path ? 0 : -ENOMEM;
1799 *new_path = malloc((strlen(prefix) + strlen(raw_path) + 2));
1804 sprintf(*new_path, "%s/%s", prefix, raw_path);
1806 if (access(*new_path, R_OK) == 0)
1809 if (!symbol_conf.source_prefix) {
1810 /* In case of searching comp_dir, don't retry */
1820 raw_path = strchr(++raw_path, '/');