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.
23 #include <sys/utsname.h>
24 #include <sys/types.h>
33 #include <dwarf-regs.h>
35 #include <linux/bitops.h>
43 #include "probe-finder.h"
44 #include "probe-file.h"
47 /* Kprobe tracer basic type is up to u64 */
48 #define MAX_BASIC_TYPE_BITS 64
50 /* Dwarf FL wrappers */
51 static char *debuginfo_path; /* Currently dummy */
53 static const Dwfl_Callbacks offline_callbacks = {
54 .find_debuginfo = dwfl_standard_find_debuginfo,
55 .debuginfo_path = &debuginfo_path,
57 .section_address = dwfl_offline_section_address,
59 /* We use this table for core files too. */
60 .find_elf = dwfl_build_id_find_elf,
63 /* Get a Dwarf from offline image */
64 static int debuginfo__init_offline_dwarf(struct debuginfo *dbg,
69 fd = open(path, O_RDONLY);
73 dbg->dwfl = dwfl_begin(&offline_callbacks);
77 dwfl_report_begin(dbg->dwfl);
78 dbg->mod = dwfl_report_offline(dbg->dwfl, "", "", fd);
82 dbg->dbg = dwfl_module_getdwarf(dbg->mod, &dbg->bias);
86 dwfl_report_end(dbg->dwfl, NULL, NULL);
94 memset(dbg, 0, sizeof(*dbg));
99 static struct debuginfo *__debuginfo__new(const char *path)
101 struct debuginfo *dbg = zalloc(sizeof(*dbg));
105 if (debuginfo__init_offline_dwarf(dbg, path) < 0)
108 pr_debug("Open Debuginfo file: %s\n", path);
112 enum dso_binary_type distro_dwarf_types[] = {
113 DSO_BINARY_TYPE__FEDORA_DEBUGINFO,
114 DSO_BINARY_TYPE__UBUNTU_DEBUGINFO,
115 DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO,
116 DSO_BINARY_TYPE__BUILDID_DEBUGINFO,
117 DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO,
118 DSO_BINARY_TYPE__NOT_FOUND,
121 struct debuginfo *debuginfo__new(const char *path)
123 enum dso_binary_type *type;
124 char buf[PATH_MAX], nil = '\0';
126 struct debuginfo *dinfo = NULL;
128 /* Try to open distro debuginfo files */
129 dso = dso__new(path);
133 for (type = distro_dwarf_types;
134 !dinfo && *type != DSO_BINARY_TYPE__NOT_FOUND;
136 if (dso__read_binary_type_filename(dso, *type, &nil,
139 dinfo = __debuginfo__new(buf);
144 /* if failed to open all distro debuginfo, open given binary */
145 return dinfo ? : __debuginfo__new(path);
148 void debuginfo__delete(struct debuginfo *dbg)
158 * Probe finder related functions
161 static struct probe_trace_arg_ref *alloc_trace_arg_ref(long offs)
163 struct probe_trace_arg_ref *ref;
164 ref = zalloc(sizeof(struct probe_trace_arg_ref));
171 * Convert a location into trace_arg.
172 * If tvar == NULL, this just checks variable can be converted.
173 * If fentry == true and vr_die is a parameter, do huristic search
174 * for the location fuzzed by function entry mcount.
176 static int convert_variable_location(Dwarf_Die *vr_die, Dwarf_Addr addr,
177 Dwarf_Op *fb_ops, Dwarf_Die *sp_die,
178 unsigned int machine,
179 struct probe_trace_arg *tvar)
181 Dwarf_Attribute attr;
191 if (dwarf_attr(vr_die, DW_AT_external, &attr) != NULL)
194 /* TODO: handle more than 1 exprs */
195 if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL)
196 return -EINVAL; /* Broken DIE ? */
197 if (dwarf_getlocation_addr(&attr, addr, &op, &nops, 1) <= 0) {
198 ret = dwarf_entrypc(sp_die, &tmp);
202 if (probe_conf.show_location_range &&
203 (dwarf_tag(vr_die) == DW_TAG_variable)) {
205 } else if (addr != tmp ||
206 dwarf_tag(vr_die) != DW_TAG_formal_parameter) {
210 ret = dwarf_highpc(sp_die, &tmp);
214 * This is fuzzed by fentry mcount. We try to find the
215 * parameter location at the earliest address.
217 for (addr += 1; addr <= tmp; addr++) {
218 if (dwarf_getlocation_addr(&attr, addr, &op,
226 /* TODO: Support const_value */
229 if (op->atom == DW_OP_addr) {
233 /* Static variables on memory (not stack), make @varname */
234 ret = strlen(dwarf_diename(vr_die));
235 tvar->value = zalloc(ret + 2);
236 if (tvar->value == NULL)
238 snprintf(tvar->value, ret + 2, "@%s", dwarf_diename(vr_die));
239 tvar->ref = alloc_trace_arg_ref((long)offs);
240 if (tvar->ref == NULL)
245 /* If this is based on frame buffer, set the offset */
246 if (op->atom == DW_OP_fbreg) {
254 if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) {
255 regn = op->atom - DW_OP_breg0;
258 } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) {
259 regn = op->atom - DW_OP_reg0;
260 } else if (op->atom == DW_OP_bregx) {
264 } else if (op->atom == DW_OP_regx) {
267 pr_debug("DW_OP %x is not supported.\n", op->atom);
274 regs = get_dwarf_regstr(regn, machine);
276 /* This should be a bug in DWARF or this tool */
277 pr_warning("Mapping for the register number %u "
278 "missing on this architecture.\n", regn);
282 tvar->value = strdup(regs);
283 if (tvar->value == NULL)
287 tvar->ref = alloc_trace_arg_ref((long)offs);
288 if (tvar->ref == NULL)
294 #define BYTES_TO_BITS(nb) ((nb) * BITS_PER_LONG / sizeof(long))
296 static int convert_variable_type(Dwarf_Die *vr_die,
297 struct probe_trace_arg *tvar,
300 struct probe_trace_arg_ref **ref_ptr = &tvar->ref;
303 char sbuf[STRERR_BUFSIZE];
304 int bsize, boffs, total;
308 /* TODO: check all types */
309 if (cast && strcmp(cast, "string") != 0 && strcmp(cast, "x") != 0 &&
310 strcmp(cast, "s") != 0 && strcmp(cast, "u") != 0) {
311 /* Non string type is OK */
312 /* and respect signedness/hexadecimal cast */
313 tvar->type = strdup(cast);
314 return (tvar->type == NULL) ? -ENOMEM : 0;
317 bsize = dwarf_bitsize(vr_die);
319 /* This is a bitfield */
320 boffs = dwarf_bitoffset(vr_die);
321 total = dwarf_bytesize(vr_die);
322 if (boffs < 0 || total < 0)
324 ret = snprintf(buf, 16, "b%d@%d/%zd", bsize, boffs,
325 BYTES_TO_BITS(total));
329 if (die_get_real_type(vr_die, &type) == NULL) {
330 pr_warning("Failed to get a type information of %s.\n",
331 dwarf_diename(vr_die));
335 pr_debug("%s type is %s.\n",
336 dwarf_diename(vr_die), dwarf_diename(&type));
338 if (cast && strcmp(cast, "string") == 0) { /* String type */
339 ret = dwarf_tag(&type);
340 if (ret != DW_TAG_pointer_type &&
341 ret != DW_TAG_array_type) {
342 pr_warning("Failed to cast into string: "
343 "%s(%s) is not a pointer nor array.\n",
344 dwarf_diename(vr_die), dwarf_diename(&type));
347 if (die_get_real_type(&type, &type) == NULL) {
348 pr_warning("Failed to get a type"
352 if (ret == DW_TAG_pointer_type) {
354 ref_ptr = &(*ref_ptr)->next;
355 /* Add new reference with offset +0 */
356 *ref_ptr = zalloc(sizeof(struct probe_trace_arg_ref));
357 if (*ref_ptr == NULL) {
358 pr_warning("Out of memory error\n");
362 if (!die_compare_name(&type, "char") &&
363 !die_compare_name(&type, "unsigned char")) {
364 pr_warning("Failed to cast into string: "
365 "%s is not (unsigned) char *.\n",
366 dwarf_diename(vr_die));
369 tvar->type = strdup(cast);
370 return (tvar->type == NULL) ? -ENOMEM : 0;
373 if (cast && (strcmp(cast, "u") == 0))
375 else if (cast && (strcmp(cast, "s") == 0))
377 else if (cast && (strcmp(cast, "x") == 0) &&
378 probe_type_is_available(PROBE_TYPE_X))
381 prefix = die_is_signed_type(&type) ? 's' :
382 probe_type_is_available(PROBE_TYPE_X) ? 'x' : 'u';
384 ret = dwarf_bytesize(&type);
386 /* No size ... try to use default type */
388 ret = BYTES_TO_BITS(ret);
390 /* Check the bitwidth */
391 if (ret > MAX_BASIC_TYPE_BITS) {
392 pr_info("%s exceeds max-bitwidth. Cut down to %d bits.\n",
393 dwarf_diename(&type), MAX_BASIC_TYPE_BITS);
394 ret = MAX_BASIC_TYPE_BITS;
396 ret = snprintf(buf, 16, "%c%d", prefix, ret);
399 if (ret < 0 || ret >= 16) {
402 pr_warning("Failed to convert variable type: %s\n",
403 str_error_r(-ret, sbuf, sizeof(sbuf)));
406 tvar->type = strdup(buf);
407 if (tvar->type == NULL)
412 static int convert_variable_fields(Dwarf_Die *vr_die, const char *varname,
413 struct perf_probe_arg_field *field,
414 struct probe_trace_arg_ref **ref_ptr,
417 struct probe_trace_arg_ref *ref = *ref_ptr;
422 pr_debug("converting %s in %s\n", field->name, varname);
423 if (die_get_real_type(vr_die, &type) == NULL) {
424 pr_warning("Failed to get the type of %s.\n", varname);
427 pr_debug2("Var real type: (%x)\n", (unsigned)dwarf_dieoffset(&type));
428 tag = dwarf_tag(&type);
430 if (field->name[0] == '[' &&
431 (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type)) {
433 /* Save original type for next field */
434 memcpy(die_mem, &type, sizeof(*die_mem));
435 /* Get the type of this array */
436 if (die_get_real_type(&type, &type) == NULL) {
437 pr_warning("Failed to get the type of %s.\n", varname);
440 pr_debug2("Array real type: (%x)\n",
441 (unsigned)dwarf_dieoffset(&type));
442 if (tag == DW_TAG_pointer_type) {
443 ref = zalloc(sizeof(struct probe_trace_arg_ref));
447 (*ref_ptr)->next = ref;
451 ref->offset += dwarf_bytesize(&type) * field->index;
453 /* Save vr_die for converting types */
454 memcpy(die_mem, vr_die, sizeof(*die_mem));
456 } else if (tag == DW_TAG_pointer_type) {
457 /* Check the pointer and dereference */
459 pr_err("Semantic error: %s must be referred by '->'\n",
463 /* Get the type pointed by this pointer */
464 if (die_get_real_type(&type, &type) == NULL) {
465 pr_warning("Failed to get the type of %s.\n", varname);
468 /* Verify it is a data structure */
469 tag = dwarf_tag(&type);
470 if (tag != DW_TAG_structure_type && tag != DW_TAG_union_type) {
471 pr_warning("%s is not a data structure nor a union.\n",
476 ref = zalloc(sizeof(struct probe_trace_arg_ref));
480 (*ref_ptr)->next = ref;
484 /* Verify it is a data structure */
485 if (tag != DW_TAG_structure_type && tag != DW_TAG_union_type) {
486 pr_warning("%s is not a data structure nor a union.\n",
490 if (field->name[0] == '[') {
491 pr_err("Semantic error: %s is not a pointer"
492 " nor array.\n", varname);
495 /* While prcessing unnamed field, we don't care about this */
496 if (field->ref && dwarf_diename(vr_die)) {
497 pr_err("Semantic error: %s must be referred by '.'\n",
502 pr_warning("Structure on a register is not "
508 if (die_find_member(&type, field->name, die_mem) == NULL) {
509 pr_warning("%s(type:%s) has no member %s.\n", varname,
510 dwarf_diename(&type), field->name);
514 /* Get the offset of the field */
515 if (tag == DW_TAG_union_type) {
518 ret = die_get_data_member_location(die_mem, &offs);
520 pr_warning("Failed to get the offset of %s.\n",
525 ref->offset += (long)offs;
527 /* If this member is unnamed, we need to reuse this field */
528 if (!dwarf_diename(die_mem))
529 return convert_variable_fields(die_mem, varname, field,
533 /* Converting next field */
535 return convert_variable_fields(die_mem, field->name,
536 field->next, &ref, die_mem);
541 /* Show a variables in kprobe event format */
542 static int convert_variable(Dwarf_Die *vr_die, struct probe_finder *pf)
547 pr_debug("Converting variable %s into trace event.\n",
548 dwarf_diename(vr_die));
550 ret = convert_variable_location(vr_die, pf->addr, pf->fb_ops,
551 &pf->sp_die, pf->machine, pf->tvar);
552 if (ret == -ENOENT || ret == -EINVAL) {
553 pr_err("Failed to find the location of the '%s' variable at this address.\n"
554 " Perhaps it has been optimized out.\n"
555 " Use -V with the --range option to show '%s' location range.\n",
556 pf->pvar->var, pf->pvar->var);
557 } else if (ret == -ENOTSUP)
558 pr_err("Sorry, we don't support this variable location yet.\n");
559 else if (ret == 0 && pf->pvar->field) {
560 ret = convert_variable_fields(vr_die, pf->pvar->var,
561 pf->pvar->field, &pf->tvar->ref,
566 ret = convert_variable_type(vr_die, pf->tvar, pf->pvar->type);
567 /* *expr will be cached in libdw. Don't free it. */
571 /* Find a variable in a scope DIE */
572 static int find_variable(Dwarf_Die *sc_die, struct probe_finder *pf)
578 /* Copy raw parameters */
579 if (!is_c_varname(pf->pvar->var))
580 return copy_to_probe_trace_arg(pf->tvar, pf->pvar);
583 pf->tvar->name = strdup(pf->pvar->name);
585 buf = synthesize_perf_probe_arg(pf->pvar);
588 ptr = strchr(buf, ':'); /* Change type separator to _ */
591 pf->tvar->name = buf;
593 if (pf->tvar->name == NULL)
596 pr_debug("Searching '%s' variable in context.\n", pf->pvar->var);
597 /* Search child die for local variables and parameters. */
598 if (!die_find_variable_at(sc_die, pf->pvar->var, pf->addr, &vr_die)) {
599 /* Search again in global variables */
600 if (!die_find_variable_at(&pf->cu_die, pf->pvar->var,
602 pr_warning("Failed to find '%s' in this function.\n",
608 ret = convert_variable(&vr_die, pf);
613 /* Convert subprogram DIE to trace point */
614 static int convert_to_trace_point(Dwarf_Die *sp_die, Dwfl_Module *mod,
615 Dwarf_Addr paddr, bool retprobe,
616 const char *function,
617 struct probe_trace_point *tp)
623 /* Verify the address is correct */
624 if (!dwarf_haspc(sp_die, paddr)) {
625 pr_warning("Specified offset is out of %s\n",
626 dwarf_diename(sp_die));
630 if (dwarf_entrypc(sp_die, &eaddr) == 0) {
631 /* If the DIE has entrypc, use it. */
632 symbol = dwarf_diename(sp_die);
634 /* Try to get actual symbol name and address from symtab */
635 symbol = dwfl_module_addrsym(mod, paddr, &sym, NULL);
636 eaddr = sym.st_value;
639 pr_warning("Failed to find symbol at 0x%lx\n",
640 (unsigned long)paddr);
644 tp->offset = (unsigned long)(paddr - eaddr);
645 tp->address = (unsigned long)paddr;
646 tp->symbol = strdup(symbol);
650 /* Return probe must be on the head of a subprogram */
652 if (eaddr != paddr) {
653 pr_warning("Failed to find \"%s%%return\",\n"
654 " because %s is an inlined function and"
655 " has no return point.\n", function,
665 /* Call probe_finder callback with scope DIE */
666 static int call_probe_finder(Dwarf_Die *sc_die, struct probe_finder *pf)
668 Dwarf_Attribute fb_attr;
669 Dwarf_Frame *frame = NULL;
674 pr_err("Caller must pass a scope DIE. Program error.\n");
678 /* If not a real subprogram, find a real one */
679 if (!die_is_func_def(sc_die)) {
680 if (!die_find_realfunc(&pf->cu_die, pf->addr, &pf->sp_die)) {
681 if (die_find_tailfunc(&pf->cu_die, pf->addr, &pf->sp_die)) {
682 pr_warning("Ignoring tail call from %s\n",
683 dwarf_diename(&pf->sp_die));
686 pr_warning("Failed to find probe point in any "
692 memcpy(&pf->sp_die, sc_die, sizeof(Dwarf_Die));
694 /* Get the frame base attribute/ops from subprogram */
695 dwarf_attr(&pf->sp_die, DW_AT_frame_base, &fb_attr);
696 ret = dwarf_getlocation_addr(&fb_attr, pf->addr, &pf->fb_ops, &nops, 1);
697 if (ret <= 0 || nops == 0) {
699 #if _ELFUTILS_PREREQ(0, 142)
700 } else if (nops == 1 && pf->fb_ops[0].atom == DW_OP_call_frame_cfa &&
701 (pf->cfi_eh != NULL || pf->cfi_dbg != NULL)) {
702 if ((dwarf_cfi_addrframe(pf->cfi_eh, pf->addr, &frame) != 0 &&
703 (dwarf_cfi_addrframe(pf->cfi_dbg, pf->addr, &frame) != 0)) ||
704 dwarf_frame_cfa(frame, &pf->fb_ops, &nops) != 0) {
705 pr_warning("Failed to get call frame on 0x%jx\n",
706 (uintmax_t)pf->addr);
713 /* Call finder's callback handler */
714 ret = pf->callback(sc_die, pf);
716 /* Since *pf->fb_ops can be a part of frame. we should free it here. */
723 struct find_scope_param {
724 const char *function;
732 static int find_best_scope_cb(Dwarf_Die *fn_die, void *data)
734 struct find_scope_param *fsp = data;
738 /* Skip if declared file name does not match */
740 file = dwarf_decl_file(fn_die);
741 if (!file || strcmp(fsp->file, file) != 0)
744 /* If the function name is given, that's what user expects */
746 if (die_match_name(fn_die, fsp->function)) {
747 memcpy(fsp->die_mem, fn_die, sizeof(Dwarf_Die));
752 /* With the line number, find the nearest declared DIE */
753 dwarf_decl_line(fn_die, &lno);
754 if (lno < fsp->line && fsp->diff > fsp->line - lno) {
755 /* Keep a candidate and continue */
756 fsp->diff = fsp->line - lno;
757 memcpy(fsp->die_mem, fn_die, sizeof(Dwarf_Die));
764 /* Return innermost DIE */
765 static int find_inner_scope_cb(Dwarf_Die *fn_die, void *data)
767 struct find_scope_param *fsp = data;
769 memcpy(fsp->die_mem, fn_die, sizeof(Dwarf_Die));
774 /* Find an appropriate scope fits to given conditions */
775 static Dwarf_Die *find_best_scope(struct probe_finder *pf, Dwarf_Die *die_mem)
777 struct find_scope_param fsp = {
778 .function = pf->pev->point.function,
787 ret = cu_walk_functions_at(&pf->cu_die, pf->addr, find_best_scope_cb,
789 if (!ret && !fsp.found)
790 cu_walk_functions_at(&pf->cu_die, pf->addr,
791 find_inner_scope_cb, &fsp);
793 return fsp.found ? die_mem : NULL;
796 static int probe_point_line_walker(const char *fname, int lineno,
797 Dwarf_Addr addr, void *data)
799 struct probe_finder *pf = data;
800 Dwarf_Die *sc_die, die_mem;
803 if (lineno != pf->lno || strtailcmp(fname, pf->fname) != 0)
807 sc_die = find_best_scope(pf, &die_mem);
809 pr_warning("Failed to find scope of probe point.\n");
813 ret = call_probe_finder(sc_die, pf);
815 /* Continue if no error, because the line will be in inline function */
816 return ret < 0 ? ret : 0;
819 /* Find probe point from its line number */
820 static int find_probe_point_by_line(struct probe_finder *pf)
822 return die_walk_lines(&pf->cu_die, probe_point_line_walker, pf);
825 /* Find lines which match lazy pattern */
826 static int find_lazy_match_lines(struct intlist *list,
827 const char *fname, const char *pat)
833 int count = 0, linenum = 1;
834 char sbuf[STRERR_BUFSIZE];
836 fp = fopen(fname, "r");
838 pr_warning("Failed to open %s: %s\n", fname,
839 str_error_r(errno, sbuf, sizeof(sbuf)));
843 while ((len = getline(&line, &line_len, fp)) > 0) {
845 if (line[len - 1] == '\n')
846 line[len - 1] = '\0';
848 if (strlazymatch(line, pat)) {
849 intlist__add(list, linenum);
861 pr_debug("No matched lines found in %s.\n", fname);
865 static int probe_point_lazy_walker(const char *fname, int lineno,
866 Dwarf_Addr addr, void *data)
868 struct probe_finder *pf = data;
869 Dwarf_Die *sc_die, die_mem;
872 if (!intlist__has_entry(pf->lcache, lineno) ||
873 strtailcmp(fname, pf->fname) != 0)
876 pr_debug("Probe line found: line:%d addr:0x%llx\n",
877 lineno, (unsigned long long)addr);
880 sc_die = find_best_scope(pf, &die_mem);
882 pr_warning("Failed to find scope of probe point.\n");
886 ret = call_probe_finder(sc_die, pf);
889 * Continue if no error, because the lazy pattern will match
892 return ret < 0 ? ret : 0;
895 /* Find probe points from lazy pattern */
896 static int find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf)
901 if (intlist__empty(pf->lcache)) {
902 const char *comp_dir;
904 comp_dir = cu_get_comp_dir(&pf->cu_die);
905 ret = get_real_path(pf->fname, comp_dir, &fpath);
907 pr_warning("Failed to find source file path.\n");
911 /* Matching lazy line pattern */
912 ret = find_lazy_match_lines(pf->lcache, fpath,
913 pf->pev->point.lazy_line);
919 return die_walk_lines(sp_die, probe_point_lazy_walker, pf);
922 static void skip_prologue(Dwarf_Die *sp_die, struct probe_finder *pf)
924 struct perf_probe_point *pp = &pf->pev->point;
927 if (!pf->pev->uprobes)
930 /* Compiled with optimization? */
931 if (die_is_optimized_target(&pf->cu_die))
934 /* Don't know entrypc? */
938 /* Only FUNC and FUNC@SRC are eligible. */
939 if (!pp->function || pp->line || pp->retprobe || pp->lazy_line ||
940 pp->offset || pp->abs_address)
943 /* Not interested in func parameter? */
944 if (!perf_probe_with_var(pf->pev))
947 pr_info("Target program is compiled without optimization. Skipping prologue.\n"
948 "Probe on address 0x%" PRIx64 " to force probing at the function entry.\n\n",
951 die_skip_prologue(sp_die, &pf->cu_die, &pf->addr);
954 static int probe_point_inline_cb(Dwarf_Die *in_die, void *data)
956 struct probe_finder *pf = data;
957 struct perf_probe_point *pp = &pf->pev->point;
962 ret = find_probe_point_lazy(in_die, pf);
964 /* Get probe address */
965 if (die_entrypc(in_die, &addr) != 0) {
966 pr_warning("Failed to get entry address of %s.\n",
967 dwarf_diename(in_die));
971 pr_debug("%s has no valid entry address. skipped.\n",
972 dwarf_diename(in_die));
976 pf->addr += pp->offset;
977 pr_debug("found inline addr: 0x%jx\n",
978 (uintmax_t)pf->addr);
980 ret = call_probe_finder(in_die, pf);
986 /* Callback parameter with return value for libdw */
987 struct dwarf_callback_param {
992 /* Search function from function name */
993 static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
995 struct dwarf_callback_param *param = data;
996 struct probe_finder *pf = param->data;
997 struct perf_probe_point *pp = &pf->pev->point;
999 /* Check tag and diename */
1000 if (!die_is_func_def(sp_die) ||
1001 !die_match_name(sp_die, pp->function))
1004 /* Check declared file */
1005 if (pp->file && strtailcmp(pp->file, dwarf_decl_file(sp_die)))
1008 pr_debug("Matched function: %s [%lx]\n", dwarf_diename(sp_die),
1009 (unsigned long)dwarf_dieoffset(sp_die));
1010 pf->fname = dwarf_decl_file(sp_die);
1011 if (pp->line) { /* Function relative line */
1012 dwarf_decl_line(sp_die, &pf->lno);
1013 pf->lno += pp->line;
1014 param->retval = find_probe_point_by_line(pf);
1015 } else if (die_is_func_instance(sp_die)) {
1016 /* Instances always have the entry address */
1017 die_entrypc(sp_die, &pf->addr);
1018 /* But in some case the entry address is 0 */
1019 if (pf->addr == 0) {
1020 pr_debug("%s has no entry PC. Skipped\n",
1021 dwarf_diename(sp_die));
1024 } else if (pp->lazy_line)
1025 param->retval = find_probe_point_lazy(sp_die, pf);
1027 skip_prologue(sp_die, pf);
1028 pf->addr += pp->offset;
1029 /* TODO: Check the address in this function */
1030 param->retval = call_probe_finder(sp_die, pf);
1032 } else if (!probe_conf.no_inlines) {
1033 /* Inlined function: search instances */
1034 param->retval = die_walk_instances(sp_die,
1035 probe_point_inline_cb, (void *)pf);
1036 /* This could be a non-existed inline definition */
1037 if (param->retval == -ENOENT)
1041 /* We need to find other candidates */
1042 if (strisglob(pp->function) && param->retval >= 0) {
1043 param->retval = 0; /* We have to clear the result */
1047 return DWARF_CB_ABORT; /* Exit; no same symbol in this CU. */
1050 static int find_probe_point_by_func(struct probe_finder *pf)
1052 struct dwarf_callback_param _param = {.data = (void *)pf,
1054 dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, &_param, 0);
1055 return _param.retval;
1058 struct pubname_callback_param {
1066 static int pubname_search_cb(Dwarf *dbg, Dwarf_Global *gl, void *data)
1068 struct pubname_callback_param *param = data;
1070 if (dwarf_offdie(dbg, gl->die_offset, param->sp_die)) {
1071 if (dwarf_tag(param->sp_die) != DW_TAG_subprogram)
1074 if (die_match_name(param->sp_die, param->function)) {
1075 if (!dwarf_offdie(dbg, gl->cu_offset, param->cu_die))
1079 strtailcmp(param->file, dwarf_decl_file(param->sp_die)))
1083 return DWARF_CB_ABORT;
1090 static int debuginfo__find_probe_location(struct debuginfo *dbg,
1091 struct probe_finder *pf)
1093 struct perf_probe_point *pp = &pf->pev->point;
1094 Dwarf_Off off, noff;
1100 pf->lcache = intlist__new(NULL);
1104 /* Fastpath: lookup by function name from .debug_pubnames section */
1105 if (pp->function && !strisglob(pp->function)) {
1106 struct pubname_callback_param pubname_param = {
1107 .function = pp->function,
1109 .cu_die = &pf->cu_die,
1110 .sp_die = &pf->sp_die,
1113 struct dwarf_callback_param probe_param = {
1117 dwarf_getpubnames(dbg->dbg, pubname_search_cb,
1119 if (pubname_param.found) {
1120 ret = probe_point_search_cb(&pf->sp_die, &probe_param);
1126 /* Loop on CUs (Compilation Unit) */
1127 while (!dwarf_nextcu(dbg->dbg, off, &noff, &cuhl, NULL, NULL, NULL)) {
1128 /* Get the DIE(Debugging Information Entry) of this CU */
1129 diep = dwarf_offdie(dbg->dbg, off + cuhl, &pf->cu_die);
1133 /* Check if target file is included. */
1135 pf->fname = cu_find_realpath(&pf->cu_die, pp->file);
1139 if (!pp->file || pf->fname) {
1141 ret = find_probe_point_by_func(pf);
1142 else if (pp->lazy_line)
1143 ret = find_probe_point_lazy(&pf->cu_die, pf);
1146 ret = find_probe_point_by_line(pf);
1155 intlist__delete(pf->lcache);
1161 /* Find probe points from debuginfo */
1162 static int debuginfo__find_probes(struct debuginfo *dbg,
1163 struct probe_finder *pf)
1169 if (pf->cfi_eh || pf->cfi_dbg)
1170 return debuginfo__find_probe_location(dbg, pf);
1172 /* Get the call frame information from this dwarf */
1173 elf = dwarf_getelf(dbg->dbg);
1177 if (gelf_getehdr(elf, &ehdr) == NULL)
1180 pf->machine = ehdr.e_machine;
1182 #if _ELFUTILS_PREREQ(0, 142)
1186 if (elf_section_by_name(elf, &ehdr, &shdr, ".eh_frame", NULL) &&
1187 shdr.sh_type == SHT_PROGBITS)
1188 pf->cfi_eh = dwarf_getcfi_elf(elf);
1190 pf->cfi_dbg = dwarf_getcfi(dbg->dbg);
1194 ret = debuginfo__find_probe_location(dbg, pf);
1198 struct local_vars_finder {
1199 struct probe_finder *pf;
1200 struct perf_probe_arg *args;
1207 /* Collect available variables in this scope */
1208 static int copy_variables_cb(Dwarf_Die *die_mem, void *data)
1210 struct local_vars_finder *vf = data;
1211 struct probe_finder *pf = vf->pf;
1214 tag = dwarf_tag(die_mem);
1215 if (tag == DW_TAG_formal_parameter ||
1216 (tag == DW_TAG_variable && vf->vars)) {
1217 if (convert_variable_location(die_mem, vf->pf->addr,
1218 vf->pf->fb_ops, &pf->sp_die,
1219 pf->machine, NULL) == 0) {
1220 vf->args[vf->nargs].var = (char *)dwarf_diename(die_mem);
1221 if (vf->args[vf->nargs].var == NULL) {
1223 return DIE_FIND_CB_END;
1225 pr_debug(" %s", vf->args[vf->nargs].var);
1230 if (dwarf_haspc(die_mem, vf->pf->addr))
1231 return DIE_FIND_CB_CONTINUE;
1233 return DIE_FIND_CB_SIBLING;
1236 static int expand_probe_args(Dwarf_Die *sc_die, struct probe_finder *pf,
1237 struct perf_probe_arg *args)
1242 struct local_vars_finder vf = {.pf = pf, .args = args, .vars = false,
1243 .max_args = MAX_PROBE_ARGS, .ret = 0};
1245 for (i = 0; i < pf->pev->nargs; i++) {
1246 /* var never be NULL */
1247 if (strcmp(pf->pev->args[i].var, PROBE_ARG_VARS) == 0)
1249 else if (strcmp(pf->pev->args[i].var, PROBE_ARG_PARAMS) != 0) {
1250 /* Copy normal argument */
1251 args[n] = pf->pev->args[i];
1255 pr_debug("Expanding %s into:", pf->pev->args[i].var);
1257 /* Special local variables */
1258 die_find_child(sc_die, copy_variables_cb, (void *)&vf,
1260 pr_debug(" (%d)\n", vf.nargs - n);
1268 /* Add a found probe point into trace event list */
1269 static int add_probe_trace_event(Dwarf_Die *sc_die, struct probe_finder *pf)
1271 struct trace_event_finder *tf =
1272 container_of(pf, struct trace_event_finder, pf);
1273 struct perf_probe_point *pp = &pf->pev->point;
1274 struct probe_trace_event *tev;
1275 struct perf_probe_arg *args = NULL;
1278 /* Check number of tevs */
1279 if (tf->ntevs == tf->max_tevs) {
1280 pr_warning("Too many( > %d) probe point found.\n",
1284 tev = &tf->tevs[tf->ntevs++];
1286 /* Trace point should be converted from subprogram DIE */
1287 ret = convert_to_trace_point(&pf->sp_die, tf->mod, pf->addr,
1288 pp->retprobe, pp->function, &tev->point);
1292 tev->point.realname = strdup(dwarf_diename(sc_die));
1293 if (!tev->point.realname) {
1298 pr_debug("Probe point found: %s+%lu\n", tev->point.symbol,
1301 /* Expand special probe argument if exist */
1302 args = zalloc(sizeof(struct perf_probe_arg) * MAX_PROBE_ARGS);
1308 ret = expand_probe_args(sc_die, pf, args);
1313 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
1314 if (tev->args == NULL) {
1319 /* Find each argument */
1320 for (i = 0; i < tev->nargs; i++) {
1321 pf->pvar = &args[i];
1322 pf->tvar = &tev->args[i];
1323 /* Variable should be found from scope DIE */
1324 ret = find_variable(sc_die, pf);
1331 clear_probe_trace_event(tev);
1338 /* Find probe_trace_events specified by perf_probe_event from debuginfo */
1339 int debuginfo__find_trace_events(struct debuginfo *dbg,
1340 struct perf_probe_event *pev,
1341 struct probe_trace_event **tevs)
1343 struct trace_event_finder tf = {
1344 .pf = {.pev = pev, .callback = add_probe_trace_event},
1345 .max_tevs = probe_conf.max_probes, .mod = dbg->mod};
1348 /* Allocate result tevs array */
1349 *tevs = zalloc(sizeof(struct probe_trace_event) * tf.max_tevs);
1356 ret = debuginfo__find_probes(dbg, &tf.pf);
1357 if (ret < 0 || tf.ntevs == 0) {
1358 for (i = 0; i < tf.ntevs; i++)
1359 clear_probe_trace_event(&tf.tevs[i]);
1364 return (ret < 0) ? ret : tf.ntevs;
1367 /* Collect available variables in this scope */
1368 static int collect_variables_cb(Dwarf_Die *die_mem, void *data)
1370 struct available_var_finder *af = data;
1371 struct variable_list *vl;
1372 struct strbuf buf = STRBUF_INIT;
1375 vl = &af->vls[af->nvls - 1];
1377 tag = dwarf_tag(die_mem);
1378 if (tag == DW_TAG_formal_parameter ||
1379 tag == DW_TAG_variable) {
1380 ret = convert_variable_location(die_mem, af->pf.addr,
1381 af->pf.fb_ops, &af->pf.sp_die,
1382 af->pf.machine, NULL);
1383 if (ret == 0 || ret == -ERANGE) {
1385 bool externs = !af->child;
1387 if (strbuf_init(&buf, 64) < 0)
1390 if (probe_conf.show_location_range) {
1392 ret2 = strbuf_add(&buf,
1393 ret ? "[INV]\t" : "[VAL]\t", 6);
1395 ret2 = strbuf_add(&buf, "[EXT]\t", 6);
1400 ret2 = die_get_varname(die_mem, &buf);
1402 if (!ret2 && probe_conf.show_location_range &&
1404 if (strbuf_addch(&buf, '\t') < 0)
1406 ret2 = die_get_var_range(&af->pf.sp_die,
1410 pr_debug("Add new var: %s\n", buf.buf);
1412 strlist__add(vl->vars,
1413 strbuf_detach(&buf, NULL));
1415 strbuf_release(&buf);
1419 if (af->child && dwarf_haspc(die_mem, af->pf.addr))
1420 return DIE_FIND_CB_CONTINUE;
1422 return DIE_FIND_CB_SIBLING;
1424 strbuf_release(&buf);
1425 pr_debug("Error in strbuf\n");
1426 return DIE_FIND_CB_END;
1429 static bool available_var_finder_overlap(struct available_var_finder *af)
1433 for (i = 0; i < af->nvls; i++) {
1434 if (af->pf.addr == af->vls[i].point.address)
1441 /* Add a found vars into available variables list */
1442 static int add_available_vars(Dwarf_Die *sc_die, struct probe_finder *pf)
1444 struct available_var_finder *af =
1445 container_of(pf, struct available_var_finder, pf);
1446 struct perf_probe_point *pp = &pf->pev->point;
1447 struct variable_list *vl;
1452 * For some reason (e.g. different column assigned to same address),
1453 * this callback can be called with the address which already passed.
1456 if (available_var_finder_overlap(af))
1459 /* Check number of tevs */
1460 if (af->nvls == af->max_vls) {
1461 pr_warning("Too many( > %d) probe point found.\n", af->max_vls);
1464 vl = &af->vls[af->nvls++];
1466 /* Trace point should be converted from subprogram DIE */
1467 ret = convert_to_trace_point(&pf->sp_die, af->mod, pf->addr,
1468 pp->retprobe, pp->function, &vl->point);
1472 pr_debug("Probe point found: %s+%lu\n", vl->point.symbol,
1475 /* Find local variables */
1476 vl->vars = strlist__new(NULL, NULL);
1477 if (vl->vars == NULL)
1480 die_find_child(sc_die, collect_variables_cb, (void *)af, &die_mem);
1482 /* Find external variables */
1483 if (!probe_conf.show_ext_vars)
1485 /* Don't need to search child DIE for external vars. */
1487 die_find_child(&pf->cu_die, collect_variables_cb, (void *)af, &die_mem);
1490 if (strlist__empty(vl->vars)) {
1491 strlist__delete(vl->vars);
1499 * Find available variables at given probe point
1500 * Return the number of found probe points. Return 0 if there is no
1501 * matched probe point. Return <0 if an error occurs.
1503 int debuginfo__find_available_vars_at(struct debuginfo *dbg,
1504 struct perf_probe_event *pev,
1505 struct variable_list **vls)
1507 struct available_var_finder af = {
1508 .pf = {.pev = pev, .callback = add_available_vars},
1510 .max_vls = probe_conf.max_probes};
1513 /* Allocate result vls array */
1514 *vls = zalloc(sizeof(struct variable_list) * af.max_vls);
1521 ret = debuginfo__find_probes(dbg, &af.pf);
1523 /* Free vlist for error */
1525 zfree(&af.vls[af.nvls].point.symbol);
1526 strlist__delete(af.vls[af.nvls].vars);
1532 return (ret < 0) ? ret : af.nvls;
1535 /* For the kernel module, we need a special code to get a DIE */
1536 int debuginfo__get_text_offset(struct debuginfo *dbg, Dwarf_Addr *offs,
1543 GElf_Shdr mem, *shdr;
1546 elf = dwfl_module_getelf(dbg->mod, &dbg->bias);
1550 /* Get the number of relocations */
1551 n = dwfl_module_relocations(dbg->mod);
1554 /* Search the relocation related .text section */
1555 for (i = 0; i < n; i++) {
1556 p = dwfl_module_relocation_info(dbg->mod, i, &shndx);
1557 if (strcmp(p, ".text") == 0) {
1558 /* OK, get the section header */
1559 scn = elf_getscn(elf, shndx);
1562 shdr = gelf_getshdr(scn, &mem);
1565 *offs = shdr->sh_addr;
1567 *offs -= shdr->sh_offset;
1573 /* Reverse search */
1574 int debuginfo__find_probe_point(struct debuginfo *dbg, unsigned long addr,
1575 struct perf_probe_point *ppt)
1577 Dwarf_Die cudie, spdie, indie;
1578 Dwarf_Addr _addr = 0, baseaddr = 0;
1579 const char *fname = NULL, *func = NULL, *basefunc = NULL, *tmp;
1580 int baseline = 0, lineno = 0, ret = 0;
1582 /* We always need to relocate the address for aranges */
1583 if (debuginfo__get_text_offset(dbg, &baseaddr, false) == 0)
1586 if (!dwarf_addrdie(dbg->dbg, (Dwarf_Addr)addr, &cudie)) {
1587 pr_warning("Failed to find debug information for address %lx\n",
1593 /* Find a corresponding line (filename and lineno) */
1594 cu_find_lineinfo(&cudie, addr, &fname, &lineno);
1595 /* Don't care whether it failed or not */
1597 /* Find a corresponding function (name, baseline and baseaddr) */
1598 if (die_find_realfunc(&cudie, (Dwarf_Addr)addr, &spdie)) {
1599 /* Get function entry information */
1600 func = basefunc = dwarf_diename(&spdie);
1602 die_entrypc(&spdie, &baseaddr) != 0 ||
1603 dwarf_decl_line(&spdie, &baseline) != 0) {
1608 fname = dwarf_decl_file(&spdie);
1609 if (addr == (unsigned long)baseaddr) {
1610 /* Function entry - Relative line number is 0 */
1615 /* Track down the inline functions step by step */
1616 while (die_find_top_inlinefunc(&spdie, (Dwarf_Addr)addr,
1618 /* There is an inline function */
1619 if (die_entrypc(&indie, &_addr) == 0 &&
1622 * addr is at an inline function entry.
1623 * In this case, lineno should be the call-site
1624 * line number. (overwrite lineinfo)
1626 lineno = die_get_call_lineno(&indie);
1627 fname = die_get_call_file(&indie);
1631 * addr is in an inline function body.
1632 * Since lineno points one of the lines
1633 * of the inline function, baseline should
1634 * be the entry line of the inline function.
1636 tmp = dwarf_diename(&indie);
1638 dwarf_decl_line(&indie, &baseline) != 0)
1644 /* Verify the lineno and baseline are in a same file */
1645 tmp = dwarf_decl_file(&spdie);
1646 if (!tmp || strcmp(tmp, fname) != 0)
1651 /* Make a relative line number or an offset */
1653 ppt->line = lineno - baseline;
1654 else if (basefunc) {
1655 ppt->offset = addr - (unsigned long)baseaddr;
1659 /* Duplicate strings */
1661 ppt->function = strdup(func);
1662 if (ppt->function == NULL) {
1668 ppt->file = strdup(fname);
1669 if (ppt->file == NULL) {
1670 zfree(&ppt->function);
1676 if (ret == 0 && (fname || func))
1677 ret = 1; /* Found a point */
1681 /* Add a line and store the src path */
1682 static int line_range_add_line(const char *src, unsigned int lineno,
1683 struct line_range *lr)
1685 /* Copy source path */
1687 lr->path = strdup(src);
1688 if (lr->path == NULL)
1691 return intlist__add(lr->line_list, lineno);
1694 static int line_range_walk_cb(const char *fname, int lineno,
1695 Dwarf_Addr addr __maybe_unused,
1698 struct line_finder *lf = data;
1701 if ((strtailcmp(fname, lf->fname) != 0) ||
1702 (lf->lno_s > lineno || lf->lno_e < lineno))
1705 err = line_range_add_line(fname, lineno, lf->lr);
1706 if (err < 0 && err != -EEXIST)
1712 /* Find line range from its line number */
1713 static int find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
1717 ret = die_walk_lines(sp_die ?: &lf->cu_die, line_range_walk_cb, lf);
1721 if (!intlist__empty(lf->lr->line_list))
1722 ret = lf->found = 1;
1724 ret = 0; /* Lines are not found */
1726 zfree(&lf->lr->path);
1731 static int line_range_inline_cb(Dwarf_Die *in_die, void *data)
1733 int ret = find_line_range_by_line(in_die, data);
1736 * We have to check all instances of inlined function, because
1737 * some execution paths can be optimized out depends on the
1738 * function argument of instances. However, if an error occurs,
1739 * it should be handled by the caller.
1741 return ret < 0 ? ret : 0;
1744 /* Search function definition from function name */
1745 static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
1747 struct dwarf_callback_param *param = data;
1748 struct line_finder *lf = param->data;
1749 struct line_range *lr = lf->lr;
1751 /* Check declared file */
1752 if (lr->file && strtailcmp(lr->file, dwarf_decl_file(sp_die)))
1755 if (die_is_func_def(sp_die) &&
1756 die_match_name(sp_die, lr->function)) {
1757 lf->fname = dwarf_decl_file(sp_die);
1758 dwarf_decl_line(sp_die, &lr->offset);
1759 pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
1760 lf->lno_s = lr->offset + lr->start;
1761 if (lf->lno_s < 0) /* Overflow */
1762 lf->lno_s = INT_MAX;
1763 lf->lno_e = lr->offset + lr->end;
1764 if (lf->lno_e < 0) /* Overflow */
1765 lf->lno_e = INT_MAX;
1766 pr_debug("New line range: %d to %d\n", lf->lno_s, lf->lno_e);
1767 lr->start = lf->lno_s;
1768 lr->end = lf->lno_e;
1769 if (!die_is_func_instance(sp_die))
1770 param->retval = die_walk_instances(sp_die,
1771 line_range_inline_cb, lf);
1773 param->retval = find_line_range_by_line(sp_die, lf);
1774 return DWARF_CB_ABORT;
1779 static int find_line_range_by_func(struct line_finder *lf)
1781 struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
1782 dwarf_getfuncs(&lf->cu_die, line_range_search_cb, ¶m, 0);
1783 return param.retval;
1786 int debuginfo__find_line_range(struct debuginfo *dbg, struct line_range *lr)
1788 struct line_finder lf = {.lr = lr, .found = 0};
1790 Dwarf_Off off = 0, noff;
1793 const char *comp_dir;
1795 /* Fastpath: lookup by function name from .debug_pubnames section */
1797 struct pubname_callback_param pubname_param = {
1798 .function = lr->function, .file = lr->file,
1799 .cu_die = &lf.cu_die, .sp_die = &lf.sp_die, .found = 0};
1800 struct dwarf_callback_param line_range_param = {
1801 .data = (void *)&lf, .retval = 0};
1803 dwarf_getpubnames(dbg->dbg, pubname_search_cb,
1805 if (pubname_param.found) {
1806 line_range_search_cb(&lf.sp_die, &line_range_param);
1812 /* Loop on CUs (Compilation Unit) */
1813 while (!lf.found && ret >= 0) {
1814 if (dwarf_nextcu(dbg->dbg, off, &noff, &cuhl,
1815 NULL, NULL, NULL) != 0)
1818 /* Get the DIE(Debugging Information Entry) of this CU */
1819 diep = dwarf_offdie(dbg->dbg, off + cuhl, &lf.cu_die);
1823 /* Check if target file is included. */
1825 lf.fname = cu_find_realpath(&lf.cu_die, lr->file);
1829 if (!lr->file || lf.fname) {
1831 ret = find_line_range_by_func(&lf);
1833 lf.lno_s = lr->start;
1835 ret = find_line_range_by_line(NULL, &lf);
1842 /* Store comp_dir */
1844 comp_dir = cu_get_comp_dir(&lf.cu_die);
1846 lr->comp_dir = strdup(comp_dir);
1852 pr_debug("path: %s\n", lr->path);
1853 return (ret < 0) ? ret : lf.found;
1857 * Find a src file from a DWARF tag path. Prepend optional source path prefix
1858 * and chop off leading directories that do not exist. Result is passed back as
1859 * a newly allocated path on success.
1860 * Return 0 if file was found and readable, -errno otherwise.
1862 int get_real_path(const char *raw_path, const char *comp_dir,
1865 const char *prefix = symbol_conf.source_prefix;
1868 if (raw_path[0] != '/' && comp_dir)
1869 /* If not an absolute path, try to use comp_dir */
1872 if (access(raw_path, R_OK) == 0) {
1873 *new_path = strdup(raw_path);
1874 return *new_path ? 0 : -ENOMEM;
1880 *new_path = malloc((strlen(prefix) + strlen(raw_path) + 2));
1885 sprintf(*new_path, "%s/%s", prefix, raw_path);
1887 if (access(*new_path, R_OK) == 0)
1890 if (!symbol_conf.source_prefix) {
1891 /* In case of searching comp_dir, don't retry */
1901 raw_path = strchr(++raw_path, '/');