1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * probe-finder.c : C expression to kprobe event converter
5 * Written by Masami Hiramatsu <mhiramat@redhat.com>
9 #include <sys/utsname.h>
10 #include <sys/types.h>
19 #include <dwarf-regs.h>
21 #include <linux/bitops.h>
22 #include <linux/zalloc.h>
30 #include "probe-finder.h"
31 #include "probe-file.h"
34 /* Kprobe tracer basic type is up to u64 */
35 #define MAX_BASIC_TYPE_BITS 64
37 /* Dwarf FL wrappers */
38 static char *debuginfo_path; /* Currently dummy */
40 static const Dwfl_Callbacks offline_callbacks = {
41 .find_debuginfo = dwfl_standard_find_debuginfo,
42 .debuginfo_path = &debuginfo_path,
44 .section_address = dwfl_offline_section_address,
46 /* We use this table for core files too. */
47 .find_elf = dwfl_build_id_find_elf,
50 /* Get a Dwarf from offline image */
51 static int debuginfo__init_offline_dwarf(struct debuginfo *dbg,
56 fd = open(path, O_RDONLY);
60 dbg->dwfl = dwfl_begin(&offline_callbacks);
64 dwfl_report_begin(dbg->dwfl);
65 dbg->mod = dwfl_report_offline(dbg->dwfl, "", "", fd);
69 dbg->dbg = dwfl_module_getdwarf(dbg->mod, &dbg->bias);
73 dwfl_report_end(dbg->dwfl, NULL, NULL);
81 memset(dbg, 0, sizeof(*dbg));
86 static struct debuginfo *__debuginfo__new(const char *path)
88 struct debuginfo *dbg = zalloc(sizeof(*dbg));
92 if (debuginfo__init_offline_dwarf(dbg, path) < 0)
95 pr_debug("Open Debuginfo file: %s\n", path);
99 enum dso_binary_type distro_dwarf_types[] = {
100 DSO_BINARY_TYPE__FEDORA_DEBUGINFO,
101 DSO_BINARY_TYPE__UBUNTU_DEBUGINFO,
102 DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO,
103 DSO_BINARY_TYPE__BUILDID_DEBUGINFO,
104 DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO,
105 DSO_BINARY_TYPE__NOT_FOUND,
108 struct debuginfo *debuginfo__new(const char *path)
110 enum dso_binary_type *type;
111 char buf[PATH_MAX], nil = '\0';
113 struct debuginfo *dinfo = NULL;
115 /* Try to open distro debuginfo files */
116 dso = dso__new(path);
120 for (type = distro_dwarf_types;
121 !dinfo && *type != DSO_BINARY_TYPE__NOT_FOUND;
123 if (dso__read_binary_type_filename(dso, *type, &nil,
126 dinfo = __debuginfo__new(buf);
131 /* if failed to open all distro debuginfo, open given binary */
132 return dinfo ? : __debuginfo__new(path);
135 void debuginfo__delete(struct debuginfo *dbg)
145 * Probe finder related functions
148 static struct probe_trace_arg_ref *alloc_trace_arg_ref(long offs)
150 struct probe_trace_arg_ref *ref;
151 ref = zalloc(sizeof(struct probe_trace_arg_ref));
158 * Convert a location into trace_arg.
159 * If tvar == NULL, this just checks variable can be converted.
160 * If fentry == true and vr_die is a parameter, do huristic search
161 * for the location fuzzed by function entry mcount.
163 static int convert_variable_location(Dwarf_Die *vr_die, Dwarf_Addr addr,
164 Dwarf_Op *fb_ops, Dwarf_Die *sp_die,
165 unsigned int machine,
166 struct probe_trace_arg *tvar)
168 Dwarf_Attribute attr;
178 if (dwarf_attr(vr_die, DW_AT_external, &attr) != NULL)
182 if (dwarf_attr(vr_die, DW_AT_const_value, &attr) &&
183 immediate_value_is_supported()) {
186 dwarf_formsdata(&attr, &snum);
187 ret = asprintf(&tvar->value, "\\%ld", (long)snum);
189 return ret < 0 ? -ENOMEM : 0;
192 /* TODO: handle more than 1 exprs */
193 if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL)
194 return -EINVAL; /* Broken DIE ? */
195 if (dwarf_getlocation_addr(&attr, addr, &op, &nops, 1) <= 0) {
196 ret = dwarf_entrypc(sp_die, &tmp);
200 if (probe_conf.show_location_range &&
201 (dwarf_tag(vr_die) == DW_TAG_variable)) {
203 } else if (addr != tmp ||
204 dwarf_tag(vr_die) != DW_TAG_formal_parameter) {
208 ret = dwarf_highpc(sp_die, &tmp);
212 * This is fuzzed by fentry mcount. We try to find the
213 * parameter location at the earliest address.
215 for (addr += 1; addr <= tmp; addr++) {
216 if (dwarf_getlocation_addr(&attr, addr, &op,
224 /* TODO: Support const_value */
227 if (op->atom == DW_OP_addr) {
231 /* Static variables on memory (not stack), make @varname */
232 ret = strlen(dwarf_diename(vr_die));
233 tvar->value = zalloc(ret + 2);
234 if (tvar->value == NULL)
236 snprintf(tvar->value, ret + 2, "@%s", dwarf_diename(vr_die));
237 tvar->ref = alloc_trace_arg_ref((long)offs);
238 if (tvar->ref == NULL)
243 /* If this is based on frame buffer, set the offset */
244 if (op->atom == DW_OP_fbreg) {
252 if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) {
253 regn = op->atom - DW_OP_breg0;
256 } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) {
257 regn = op->atom - DW_OP_reg0;
258 } else if (op->atom == DW_OP_bregx) {
262 } else if (op->atom == DW_OP_regx) {
265 pr_debug("DW_OP %x is not supported.\n", op->atom);
272 regs = get_dwarf_regstr(regn, machine);
274 /* This should be a bug in DWARF or this tool */
275 pr_warning("Mapping for the register number %u "
276 "missing on this architecture.\n", regn);
280 tvar->value = strdup(regs);
281 if (tvar->value == NULL)
285 tvar->ref = alloc_trace_arg_ref((long)offs);
286 if (tvar->ref == NULL)
292 #define BYTES_TO_BITS(nb) ((nb) * BITS_PER_LONG / sizeof(long))
294 static int convert_variable_type(Dwarf_Die *vr_die,
295 struct probe_trace_arg *tvar,
296 const char *cast, bool user_access)
298 struct probe_trace_arg_ref **ref_ptr = &tvar->ref;
301 char sbuf[STRERR_BUFSIZE];
302 int bsize, boffs, total;
306 /* TODO: check all types */
307 if (cast && strcmp(cast, "string") != 0 && strcmp(cast, "ustring") &&
308 strcmp(cast, "x") != 0 &&
309 strcmp(cast, "s") != 0 && strcmp(cast, "u") != 0) {
310 /* Non string type is OK */
311 /* and respect signedness/hexadecimal cast */
312 tvar->type = strdup(cast);
313 return (tvar->type == NULL) ? -ENOMEM : 0;
316 bsize = dwarf_bitsize(vr_die);
318 /* This is a bitfield */
319 boffs = dwarf_bitoffset(vr_die);
320 total = dwarf_bytesize(vr_die);
321 if (boffs < 0 || total < 0)
323 ret = snprintf(buf, 16, "b%d@%d/%zd", bsize, boffs,
324 BYTES_TO_BITS(total));
328 if (die_get_real_type(vr_die, &type) == NULL) {
329 pr_warning("Failed to get a type information of %s.\n",
330 dwarf_diename(vr_die));
334 pr_debug("%s type is %s.\n",
335 dwarf_diename(vr_die), dwarf_diename(&type));
337 if (cast && (!strcmp(cast, "string") || !strcmp(cast, "ustring"))) {
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");
361 (*ref_ptr)->user_access = user_access;
363 if (!die_compare_name(&type, "char") &&
364 !die_compare_name(&type, "unsigned char")) {
365 pr_warning("Failed to cast into string: "
366 "%s is not (unsigned) char *.\n",
367 dwarf_diename(vr_die));
370 tvar->type = strdup(cast);
371 return (tvar->type == NULL) ? -ENOMEM : 0;
374 if (cast && (strcmp(cast, "u") == 0))
376 else if (cast && (strcmp(cast, "s") == 0))
378 else if (cast && (strcmp(cast, "x") == 0) &&
379 probe_type_is_available(PROBE_TYPE_X))
382 prefix = die_is_signed_type(&type) ? 's' :
383 probe_type_is_available(PROBE_TYPE_X) ? 'x' : 'u';
385 ret = dwarf_bytesize(&type);
387 /* No size ... try to use default type */
389 ret = BYTES_TO_BITS(ret);
391 /* Check the bitwidth */
392 if (ret > MAX_BASIC_TYPE_BITS) {
393 pr_info("%s exceeds max-bitwidth. Cut down to %d bits.\n",
394 dwarf_diename(&type), MAX_BASIC_TYPE_BITS);
395 ret = MAX_BASIC_TYPE_BITS;
397 ret = snprintf(buf, 16, "%c%d", prefix, ret);
400 if (ret < 0 || ret >= 16) {
403 pr_warning("Failed to convert variable type: %s\n",
404 str_error_r(-ret, sbuf, sizeof(sbuf)));
407 tvar->type = strdup(buf);
408 if (tvar->type == NULL)
413 static int convert_variable_fields(Dwarf_Die *vr_die, const char *varname,
414 struct perf_probe_arg_field *field,
415 struct probe_trace_arg_ref **ref_ptr,
416 Dwarf_Die *die_mem, bool user_access)
418 struct probe_trace_arg_ref *ref = *ref_ptr;
423 pr_debug("converting %s in %s\n", field->name, varname);
424 if (die_get_real_type(vr_die, &type) == NULL) {
425 pr_warning("Failed to get the type of %s.\n", varname);
428 pr_debug2("Var real type: %s (%x)\n", dwarf_diename(&type),
429 (unsigned)dwarf_dieoffset(&type));
430 tag = dwarf_tag(&type);
432 if (field->name[0] == '[' &&
433 (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type)) {
434 /* Save original type for next field or type */
435 memcpy(die_mem, &type, sizeof(*die_mem));
436 /* Get the type of this array */
437 if (die_get_real_type(&type, &type) == NULL) {
438 pr_warning("Failed to get the type of %s.\n", varname);
441 pr_debug2("Array real type: %s (%x)\n", dwarf_diename(&type),
442 (unsigned)dwarf_dieoffset(&type));
443 if (tag == DW_TAG_pointer_type) {
444 ref = zalloc(sizeof(struct probe_trace_arg_ref));
448 (*ref_ptr)->next = ref;
452 ref->offset += dwarf_bytesize(&type) * field->index;
453 ref->user_access = user_access;
455 } else if (tag == DW_TAG_pointer_type) {
456 /* Check the pointer and dereference */
458 pr_err("Semantic error: %s must be referred by '->'\n",
462 /* Get the type pointed by this pointer */
463 if (die_get_real_type(&type, &type) == NULL) {
464 pr_warning("Failed to get the type of %s.\n", varname);
467 /* Verify it is a data structure */
468 tag = dwarf_tag(&type);
469 if (tag != DW_TAG_structure_type && tag != DW_TAG_union_type) {
470 pr_warning("%s is not a data structure nor a union.\n",
475 ref = zalloc(sizeof(struct probe_trace_arg_ref));
479 (*ref_ptr)->next = ref;
483 /* Verify it is a data structure */
484 if (tag != DW_TAG_structure_type && tag != DW_TAG_union_type) {
485 pr_warning("%s is not a data structure nor a union.\n",
489 if (field->name[0] == '[') {
490 pr_err("Semantic error: %s is not a pointer"
491 " nor array.\n", varname);
494 /* While prcessing unnamed field, we don't care about this */
495 if (field->ref && dwarf_diename(vr_die)) {
496 pr_err("Semantic error: %s must be referred by '.'\n",
501 pr_warning("Structure on a register is not "
507 if (die_find_member(&type, field->name, die_mem) == NULL) {
508 pr_warning("%s(type:%s) has no member %s.\n", varname,
509 dwarf_diename(&type), field->name);
513 /* Get the offset of the field */
514 if (tag == DW_TAG_union_type) {
517 ret = die_get_data_member_location(die_mem, &offs);
519 pr_warning("Failed to get the offset of %s.\n",
524 ref->offset += (long)offs;
525 ref->user_access = user_access;
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,
530 &ref, die_mem, user_access);
533 /* Converting next field */
535 return convert_variable_fields(die_mem, field->name,
536 field->next, &ref, die_mem, user_access);
541 static void print_var_not_found(const char *varname)
543 pr_err("Failed to find the location of the '%s' variable at this address.\n"
544 " Perhaps it has been optimized out.\n"
545 " Use -V with the --range option to show '%s' location range.\n",
549 /* Show a variables in kprobe event format */
550 static int convert_variable(Dwarf_Die *vr_die, struct probe_finder *pf)
555 pr_debug("Converting variable %s into trace event.\n",
556 dwarf_diename(vr_die));
558 ret = convert_variable_location(vr_die, pf->addr, pf->fb_ops,
559 &pf->sp_die, pf->machine, pf->tvar);
560 if (ret == -ENOENT && pf->skip_empty_arg)
561 /* This can be found in other place. skip it */
563 if (ret == -ENOENT || ret == -EINVAL) {
564 print_var_not_found(pf->pvar->var);
565 } else if (ret == -ENOTSUP)
566 pr_err("Sorry, we don't support this variable location yet.\n");
567 else if (ret == 0 && pf->pvar->field) {
568 ret = convert_variable_fields(vr_die, pf->pvar->var,
569 pf->pvar->field, &pf->tvar->ref,
570 &die_mem, pf->pvar->user_access);
574 ret = convert_variable_type(vr_die, pf->tvar, pf->pvar->type,
575 pf->pvar->user_access);
576 /* *expr will be cached in libdw. Don't free it. */
580 /* Find a variable in a scope DIE */
581 static int find_variable(Dwarf_Die *sc_die, struct probe_finder *pf)
587 /* Copy raw parameters */
588 if (!is_c_varname(pf->pvar->var))
589 return copy_to_probe_trace_arg(pf->tvar, pf->pvar);
592 pf->tvar->name = strdup(pf->pvar->name);
594 buf = synthesize_perf_probe_arg(pf->pvar);
597 ptr = strchr(buf, ':'); /* Change type separator to _ */
600 pf->tvar->name = buf;
602 if (pf->tvar->name == NULL)
605 pr_debug("Searching '%s' variable in context.\n", pf->pvar->var);
606 /* Search child die for local variables and parameters. */
607 if (!die_find_variable_at(sc_die, pf->pvar->var, pf->addr, &vr_die)) {
608 /* Search again in global variables */
609 if (!die_find_variable_at(&pf->cu_die, pf->pvar->var,
611 if (pf->skip_empty_arg)
613 pr_warning("Failed to find '%s' in this function.\n",
619 ret = convert_variable(&vr_die, pf);
624 /* Convert subprogram DIE to trace point */
625 static int convert_to_trace_point(Dwarf_Die *sp_die, Dwfl_Module *mod,
626 Dwarf_Addr paddr, bool retprobe,
627 const char *function,
628 struct probe_trace_point *tp)
634 /* Verify the address is correct */
635 if (!dwarf_haspc(sp_die, paddr)) {
636 pr_warning("Specified offset is out of %s\n",
637 dwarf_diename(sp_die));
641 if (dwarf_entrypc(sp_die, &eaddr) == 0) {
642 /* If the DIE has entrypc, use it. */
643 symbol = dwarf_diename(sp_die);
645 /* Try to get actual symbol name and address from symtab */
646 symbol = dwfl_module_addrsym(mod, paddr, &sym, NULL);
647 eaddr = sym.st_value;
650 pr_warning("Failed to find symbol at 0x%lx\n",
651 (unsigned long)paddr);
655 tp->offset = (unsigned long)(paddr - eaddr);
656 tp->address = (unsigned long)paddr;
657 tp->symbol = strdup(symbol);
661 /* Return probe must be on the head of a subprogram */
663 if (eaddr != paddr) {
664 pr_warning("Failed to find \"%s%%return\",\n"
665 " because %s is an inlined function and"
666 " has no return point.\n", function,
676 /* Call probe_finder callback with scope DIE */
677 static int call_probe_finder(Dwarf_Die *sc_die, struct probe_finder *pf)
679 Dwarf_Attribute fb_attr;
680 Dwarf_Frame *frame = NULL;
685 pr_err("Caller must pass a scope DIE. Program error.\n");
689 /* If not a real subprogram, find a real one */
690 if (!die_is_func_def(sc_die)) {
691 if (!die_find_realfunc(&pf->cu_die, pf->addr, &pf->sp_die)) {
692 if (die_find_tailfunc(&pf->cu_die, pf->addr, &pf->sp_die)) {
693 pr_warning("Ignoring tail call from %s\n",
694 dwarf_diename(&pf->sp_die));
697 pr_warning("Failed to find probe point in any "
703 memcpy(&pf->sp_die, sc_die, sizeof(Dwarf_Die));
705 /* Get the frame base attribute/ops from subprogram */
706 dwarf_attr(&pf->sp_die, DW_AT_frame_base, &fb_attr);
707 ret = dwarf_getlocation_addr(&fb_attr, pf->addr, &pf->fb_ops, &nops, 1);
708 if (ret <= 0 || nops == 0) {
710 #if _ELFUTILS_PREREQ(0, 142)
711 } else if (nops == 1 && pf->fb_ops[0].atom == DW_OP_call_frame_cfa &&
712 (pf->cfi_eh != NULL || pf->cfi_dbg != NULL)) {
713 if ((dwarf_cfi_addrframe(pf->cfi_eh, pf->addr, &frame) != 0 &&
714 (dwarf_cfi_addrframe(pf->cfi_dbg, pf->addr, &frame) != 0)) ||
715 dwarf_frame_cfa(frame, &pf->fb_ops, &nops) != 0) {
716 pr_warning("Failed to get call frame on 0x%jx\n",
717 (uintmax_t)pf->addr);
724 /* Call finder's callback handler */
725 ret = pf->callback(sc_die, pf);
727 /* Since *pf->fb_ops can be a part of frame. we should free it here. */
734 struct find_scope_param {
735 const char *function;
743 static int find_best_scope_cb(Dwarf_Die *fn_die, void *data)
745 struct find_scope_param *fsp = data;
749 /* Skip if declared file name does not match */
751 file = dwarf_decl_file(fn_die);
752 if (!file || strcmp(fsp->file, file) != 0)
755 /* If the function name is given, that's what user expects */
757 if (die_match_name(fn_die, fsp->function)) {
758 memcpy(fsp->die_mem, fn_die, sizeof(Dwarf_Die));
763 /* With the line number, find the nearest declared DIE */
764 dwarf_decl_line(fn_die, &lno);
765 if (lno < fsp->line && fsp->diff > fsp->line - lno) {
766 /* Keep a candidate and continue */
767 fsp->diff = fsp->line - lno;
768 memcpy(fsp->die_mem, fn_die, sizeof(Dwarf_Die));
775 /* Return innermost DIE */
776 static int find_inner_scope_cb(Dwarf_Die *fn_die, void *data)
778 struct find_scope_param *fsp = data;
780 memcpy(fsp->die_mem, fn_die, sizeof(Dwarf_Die));
785 /* Find an appropriate scope fits to given conditions */
786 static Dwarf_Die *find_best_scope(struct probe_finder *pf, Dwarf_Die *die_mem)
788 struct find_scope_param fsp = {
789 .function = pf->pev->point.function,
798 ret = cu_walk_functions_at(&pf->cu_die, pf->addr, find_best_scope_cb,
800 if (!ret && !fsp.found)
801 cu_walk_functions_at(&pf->cu_die, pf->addr,
802 find_inner_scope_cb, &fsp);
804 return fsp.found ? die_mem : NULL;
807 static int verify_representive_line(struct probe_finder *pf, const char *fname,
808 int lineno, Dwarf_Addr addr)
810 const char *__fname, *__func = NULL;
814 /* Verify line number and address by reverse search */
815 if (cu_find_lineinfo(&pf->cu_die, addr, &__fname, &__lineno) < 0)
818 pr_debug2("Reversed line: %s:%d\n", __fname, __lineno);
819 if (strcmp(fname, __fname) || lineno == __lineno)
822 pr_warning("This line is sharing the address with other lines.\n");
824 if (pf->pev->point.function) {
825 /* Find best match function name and lines */
827 if (find_best_scope(pf, &die_mem)
828 && die_match_name(&die_mem, pf->pev->point.function)
829 && dwarf_decl_line(&die_mem, &lineno) == 0) {
830 __func = dwarf_diename(&die_mem);
834 pr_warning("Please try to probe at %s:%d instead.\n",
835 __func ? : __fname, __lineno);
840 static int probe_point_line_walker(const char *fname, int lineno,
841 Dwarf_Addr addr, void *data)
843 struct probe_finder *pf = data;
844 Dwarf_Die *sc_die, die_mem;
847 if (lineno != pf->lno || strtailcmp(fname, pf->fname) != 0)
850 if (verify_representive_line(pf, fname, lineno, addr))
854 sc_die = find_best_scope(pf, &die_mem);
856 pr_warning("Failed to find scope of probe point.\n");
860 ret = call_probe_finder(sc_die, pf);
862 /* Continue if no error, because the line will be in inline function */
863 return ret < 0 ? ret : 0;
866 /* Find probe point from its line number */
867 static int find_probe_point_by_line(struct probe_finder *pf)
869 return die_walk_lines(&pf->cu_die, probe_point_line_walker, pf);
872 /* Find lines which match lazy pattern */
873 static int find_lazy_match_lines(struct intlist *list,
874 const char *fname, const char *pat)
880 int count = 0, linenum = 1;
881 char sbuf[STRERR_BUFSIZE];
883 fp = fopen(fname, "r");
885 pr_warning("Failed to open %s: %s\n", fname,
886 str_error_r(errno, sbuf, sizeof(sbuf)));
890 while ((len = getline(&line, &line_len, fp)) > 0) {
892 if (line[len - 1] == '\n')
893 line[len - 1] = '\0';
895 if (strlazymatch(line, pat)) {
896 intlist__add(list, linenum);
908 pr_debug("No matched lines found in %s.\n", fname);
912 static int probe_point_lazy_walker(const char *fname, int lineno,
913 Dwarf_Addr addr, void *data)
915 struct probe_finder *pf = data;
916 Dwarf_Die *sc_die, die_mem;
919 if (!intlist__has_entry(pf->lcache, lineno) ||
920 strtailcmp(fname, pf->fname) != 0)
923 pr_debug("Probe line found: line:%d addr:0x%llx\n",
924 lineno, (unsigned long long)addr);
927 sc_die = find_best_scope(pf, &die_mem);
929 pr_warning("Failed to find scope of probe point.\n");
933 ret = call_probe_finder(sc_die, pf);
936 * Continue if no error, because the lazy pattern will match
939 return ret < 0 ? ret : 0;
942 /* Find probe points from lazy pattern */
943 static int find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf)
948 if (intlist__empty(pf->lcache)) {
949 const char *comp_dir;
951 comp_dir = cu_get_comp_dir(&pf->cu_die);
952 ret = get_real_path(pf->fname, comp_dir, &fpath);
954 pr_warning("Failed to find source file path.\n");
958 /* Matching lazy line pattern */
959 ret = find_lazy_match_lines(pf->lcache, fpath,
960 pf->pev->point.lazy_line);
966 return die_walk_lines(sp_die, probe_point_lazy_walker, pf);
969 static void skip_prologue(Dwarf_Die *sp_die, struct probe_finder *pf)
971 struct perf_probe_point *pp = &pf->pev->point;
974 if (!pf->pev->uprobes)
977 /* Compiled with optimization? */
978 if (die_is_optimized_target(&pf->cu_die))
981 /* Don't know entrypc? */
985 /* Only FUNC and FUNC@SRC are eligible. */
986 if (!pp->function || pp->line || pp->retprobe || pp->lazy_line ||
987 pp->offset || pp->abs_address)
990 /* Not interested in func parameter? */
991 if (!perf_probe_with_var(pf->pev))
994 pr_info("Target program is compiled without optimization. Skipping prologue.\n"
995 "Probe on address 0x%" PRIx64 " to force probing at the function entry.\n\n",
998 die_skip_prologue(sp_die, &pf->cu_die, &pf->addr);
1001 static int probe_point_inline_cb(Dwarf_Die *in_die, void *data)
1003 struct probe_finder *pf = data;
1004 struct perf_probe_point *pp = &pf->pev->point;
1009 ret = find_probe_point_lazy(in_die, pf);
1011 /* Get probe address */
1012 if (die_entrypc(in_die, &addr) != 0) {
1013 pr_warning("Failed to get entry address of %s.\n",
1014 dwarf_diename(in_die));
1018 pr_debug("%s has no valid entry address. skipped.\n",
1019 dwarf_diename(in_die));
1023 pf->addr += pp->offset;
1024 pr_debug("found inline addr: 0x%jx\n",
1025 (uintmax_t)pf->addr);
1027 ret = call_probe_finder(in_die, pf);
1033 /* Callback parameter with return value for libdw */
1034 struct dwarf_callback_param {
1039 /* Search function from function name */
1040 static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
1042 struct dwarf_callback_param *param = data;
1043 struct probe_finder *pf = param->data;
1044 struct perf_probe_point *pp = &pf->pev->point;
1046 /* Check tag and diename */
1047 if (!die_is_func_def(sp_die) ||
1048 !die_match_name(sp_die, pp->function))
1051 /* Check declared file */
1052 if (pp->file && strtailcmp(pp->file, dwarf_decl_file(sp_die)))
1055 pr_debug("Matched function: %s [%lx]\n", dwarf_diename(sp_die),
1056 (unsigned long)dwarf_dieoffset(sp_die));
1057 pf->fname = dwarf_decl_file(sp_die);
1058 if (pp->line) { /* Function relative line */
1059 dwarf_decl_line(sp_die, &pf->lno);
1060 pf->lno += pp->line;
1061 param->retval = find_probe_point_by_line(pf);
1062 } else if (die_is_func_instance(sp_die)) {
1063 /* Instances always have the entry address */
1064 die_entrypc(sp_die, &pf->addr);
1065 /* But in some case the entry address is 0 */
1066 if (pf->addr == 0) {
1067 pr_debug("%s has no entry PC. Skipped\n",
1068 dwarf_diename(sp_die));
1071 } else if (pp->lazy_line)
1072 param->retval = find_probe_point_lazy(sp_die, pf);
1074 skip_prologue(sp_die, pf);
1075 pf->addr += pp->offset;
1076 /* TODO: Check the address in this function */
1077 param->retval = call_probe_finder(sp_die, pf);
1079 } else if (!probe_conf.no_inlines) {
1080 /* Inlined function: search instances */
1081 param->retval = die_walk_instances(sp_die,
1082 probe_point_inline_cb, (void *)pf);
1083 /* This could be a non-existed inline definition */
1084 if (param->retval == -ENOENT)
1088 /* We need to find other candidates */
1089 if (strisglob(pp->function) && param->retval >= 0) {
1090 param->retval = 0; /* We have to clear the result */
1094 return DWARF_CB_ABORT; /* Exit; no same symbol in this CU. */
1097 static int find_probe_point_by_func(struct probe_finder *pf)
1099 struct dwarf_callback_param _param = {.data = (void *)pf,
1101 dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, &_param, 0);
1102 return _param.retval;
1105 struct pubname_callback_param {
1113 static int pubname_search_cb(Dwarf *dbg, Dwarf_Global *gl, void *data)
1115 struct pubname_callback_param *param = data;
1117 if (dwarf_offdie(dbg, gl->die_offset, param->sp_die)) {
1118 if (dwarf_tag(param->sp_die) != DW_TAG_subprogram)
1121 if (die_match_name(param->sp_die, param->function)) {
1122 if (!dwarf_offdie(dbg, gl->cu_offset, param->cu_die))
1126 strtailcmp(param->file, dwarf_decl_file(param->sp_die)))
1130 return DWARF_CB_ABORT;
1137 static int debuginfo__find_probe_location(struct debuginfo *dbg,
1138 struct probe_finder *pf)
1140 struct perf_probe_point *pp = &pf->pev->point;
1141 Dwarf_Off off, noff;
1147 pf->lcache = intlist__new(NULL);
1151 /* Fastpath: lookup by function name from .debug_pubnames section */
1152 if (pp->function && !strisglob(pp->function)) {
1153 struct pubname_callback_param pubname_param = {
1154 .function = pp->function,
1156 .cu_die = &pf->cu_die,
1157 .sp_die = &pf->sp_die,
1160 struct dwarf_callback_param probe_param = {
1164 dwarf_getpubnames(dbg->dbg, pubname_search_cb,
1166 if (pubname_param.found) {
1167 ret = probe_point_search_cb(&pf->sp_die, &probe_param);
1173 /* Loop on CUs (Compilation Unit) */
1174 while (!dwarf_nextcu(dbg->dbg, off, &noff, &cuhl, NULL, NULL, NULL)) {
1175 /* Get the DIE(Debugging Information Entry) of this CU */
1176 diep = dwarf_offdie(dbg->dbg, off + cuhl, &pf->cu_die);
1180 /* Check if target file is included. */
1182 pf->fname = cu_find_realpath(&pf->cu_die, pp->file);
1186 if (!pp->file || pf->fname) {
1188 ret = find_probe_point_by_func(pf);
1189 else if (pp->lazy_line)
1190 ret = find_probe_point_lazy(&pf->cu_die, pf);
1193 ret = find_probe_point_by_line(pf);
1202 intlist__delete(pf->lcache);
1208 /* Find probe points from debuginfo */
1209 static int debuginfo__find_probes(struct debuginfo *dbg,
1210 struct probe_finder *pf)
1216 if (pf->cfi_eh || pf->cfi_dbg)
1217 return debuginfo__find_probe_location(dbg, pf);
1219 /* Get the call frame information from this dwarf */
1220 elf = dwarf_getelf(dbg->dbg);
1224 if (gelf_getehdr(elf, &ehdr) == NULL)
1227 pf->machine = ehdr.e_machine;
1229 #if _ELFUTILS_PREREQ(0, 142)
1233 if (elf_section_by_name(elf, &ehdr, &shdr, ".eh_frame", NULL) &&
1234 shdr.sh_type == SHT_PROGBITS)
1235 pf->cfi_eh = dwarf_getcfi_elf(elf);
1237 pf->cfi_dbg = dwarf_getcfi(dbg->dbg);
1241 ret = debuginfo__find_probe_location(dbg, pf);
1245 struct local_vars_finder {
1246 struct probe_finder *pf;
1247 struct perf_probe_arg *args;
1254 /* Collect available variables in this scope */
1255 static int copy_variables_cb(Dwarf_Die *die_mem, void *data)
1257 struct local_vars_finder *vf = data;
1258 struct probe_finder *pf = vf->pf;
1261 tag = dwarf_tag(die_mem);
1262 if (tag == DW_TAG_formal_parameter ||
1263 (tag == DW_TAG_variable && vf->vars)) {
1264 if (convert_variable_location(die_mem, vf->pf->addr,
1265 vf->pf->fb_ops, &pf->sp_die,
1266 pf->machine, NULL) == 0) {
1267 vf->args[vf->nargs].var = (char *)dwarf_diename(die_mem);
1268 if (vf->args[vf->nargs].var == NULL) {
1270 return DIE_FIND_CB_END;
1272 pr_debug(" %s", vf->args[vf->nargs].var);
1277 if (dwarf_haspc(die_mem, vf->pf->addr))
1278 return DIE_FIND_CB_CONTINUE;
1280 return DIE_FIND_CB_SIBLING;
1283 static int expand_probe_args(Dwarf_Die *sc_die, struct probe_finder *pf,
1284 struct perf_probe_arg *args)
1289 struct local_vars_finder vf = {.pf = pf, .args = args, .vars = false,
1290 .max_args = MAX_PROBE_ARGS, .ret = 0};
1292 for (i = 0; i < pf->pev->nargs; i++) {
1293 /* var never be NULL */
1294 if (strcmp(pf->pev->args[i].var, PROBE_ARG_VARS) == 0)
1296 else if (strcmp(pf->pev->args[i].var, PROBE_ARG_PARAMS) != 0) {
1297 /* Copy normal argument */
1298 args[n] = pf->pev->args[i];
1302 pr_debug("Expanding %s into:", pf->pev->args[i].var);
1304 /* Special local variables */
1305 die_find_child(sc_die, copy_variables_cb, (void *)&vf,
1307 pr_debug(" (%d)\n", vf.nargs - n);
1315 static bool trace_event_finder_overlap(struct trace_event_finder *tf)
1319 for (i = 0; i < tf->ntevs; i++) {
1320 if (tf->pf.addr == tf->tevs[i].point.address)
1326 /* Add a found probe point into trace event list */
1327 static int add_probe_trace_event(Dwarf_Die *sc_die, struct probe_finder *pf)
1329 struct trace_event_finder *tf =
1330 container_of(pf, struct trace_event_finder, pf);
1331 struct perf_probe_point *pp = &pf->pev->point;
1332 struct probe_trace_event *tev;
1333 struct perf_probe_arg *args = NULL;
1337 * For some reason (e.g. different column assigned to same address)
1338 * This callback can be called with the address which already passed.
1341 if (trace_event_finder_overlap(tf))
1344 /* Check number of tevs */
1345 if (tf->ntevs == tf->max_tevs) {
1346 pr_warning("Too many( > %d) probe point found.\n",
1350 tev = &tf->tevs[tf->ntevs++];
1352 /* Trace point should be converted from subprogram DIE */
1353 ret = convert_to_trace_point(&pf->sp_die, tf->mod, pf->addr,
1354 pp->retprobe, pp->function, &tev->point);
1358 tev->point.realname = strdup(dwarf_diename(sc_die));
1359 if (!tev->point.realname) {
1364 pr_debug("Probe point found: %s+%lu\n", tev->point.symbol,
1367 /* Expand special probe argument if exist */
1368 args = zalloc(sizeof(struct perf_probe_arg) * MAX_PROBE_ARGS);
1374 ret = expand_probe_args(sc_die, pf, args);
1379 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
1380 if (tev->args == NULL) {
1385 /* Find each argument */
1386 for (i = 0; i < tev->nargs; i++) {
1387 pf->pvar = &args[i];
1388 pf->tvar = &tev->args[i];
1389 /* Variable should be found from scope DIE */
1390 ret = find_variable(sc_die, pf);
1397 clear_probe_trace_event(tev);
1404 static int fill_empty_trace_arg(struct perf_probe_event *pev,
1405 struct probe_trace_event *tevs, int ntevs)
1414 for (i = 0; i < pev->nargs; i++) {
1416 for (j = 0; j < ntevs; j++) {
1417 if (tevs[j].args[i].value) {
1418 type = tevs[j].args[i].type;
1423 print_var_not_found(pev->args[i].var);
1426 for (j = 0; j < ntevs; j++) {
1427 valp = &tevs[j].args[i].value;
1431 ret = asprintf(valp, "\\%lx", probe_conf.magic_num);
1434 /* Note that type can be NULL */
1436 tevs[j].args[i].type = strdup(type);
1437 if (!tevs[j].args[i].type)
1445 /* Find probe_trace_events specified by perf_probe_event from debuginfo */
1446 int debuginfo__find_trace_events(struct debuginfo *dbg,
1447 struct perf_probe_event *pev,
1448 struct probe_trace_event **tevs)
1450 struct trace_event_finder tf = {
1451 .pf = {.pev = pev, .callback = add_probe_trace_event},
1452 .max_tevs = probe_conf.max_probes, .mod = dbg->mod};
1455 /* Allocate result tevs array */
1456 *tevs = zalloc(sizeof(struct probe_trace_event) * tf.max_tevs);
1463 if (pev->nargs != 0 && immediate_value_is_supported())
1464 tf.pf.skip_empty_arg = true;
1466 ret = debuginfo__find_probes(dbg, &tf.pf);
1467 if (ret >= 0 && tf.pf.skip_empty_arg)
1468 ret = fill_empty_trace_arg(pev, tf.tevs, tf.ntevs);
1470 if (ret < 0 || tf.ntevs == 0) {
1471 for (i = 0; i < tf.ntevs; i++)
1472 clear_probe_trace_event(&tf.tevs[i]);
1477 return (ret < 0) ? ret : tf.ntevs;
1480 /* Collect available variables in this scope */
1481 static int collect_variables_cb(Dwarf_Die *die_mem, void *data)
1483 struct available_var_finder *af = data;
1484 struct variable_list *vl;
1485 struct strbuf buf = STRBUF_INIT;
1488 vl = &af->vls[af->nvls - 1];
1490 tag = dwarf_tag(die_mem);
1491 if (tag == DW_TAG_formal_parameter ||
1492 tag == DW_TAG_variable) {
1493 ret = convert_variable_location(die_mem, af->pf.addr,
1494 af->pf.fb_ops, &af->pf.sp_die,
1495 af->pf.machine, NULL);
1496 if (ret == 0 || ret == -ERANGE) {
1498 bool externs = !af->child;
1500 if (strbuf_init(&buf, 64) < 0)
1503 if (probe_conf.show_location_range) {
1505 ret2 = strbuf_add(&buf,
1506 ret ? "[INV]\t" : "[VAL]\t", 6);
1508 ret2 = strbuf_add(&buf, "[EXT]\t", 6);
1513 ret2 = die_get_varname(die_mem, &buf);
1515 if (!ret2 && probe_conf.show_location_range &&
1517 if (strbuf_addch(&buf, '\t') < 0)
1519 ret2 = die_get_var_range(&af->pf.sp_die,
1523 pr_debug("Add new var: %s\n", buf.buf);
1525 strlist__add(vl->vars,
1526 strbuf_detach(&buf, NULL));
1528 strbuf_release(&buf);
1532 if (af->child && dwarf_haspc(die_mem, af->pf.addr))
1533 return DIE_FIND_CB_CONTINUE;
1535 return DIE_FIND_CB_SIBLING;
1537 strbuf_release(&buf);
1538 pr_debug("Error in strbuf\n");
1539 return DIE_FIND_CB_END;
1542 static bool available_var_finder_overlap(struct available_var_finder *af)
1546 for (i = 0; i < af->nvls; i++) {
1547 if (af->pf.addr == af->vls[i].point.address)
1554 /* Add a found vars into available variables list */
1555 static int add_available_vars(Dwarf_Die *sc_die, struct probe_finder *pf)
1557 struct available_var_finder *af =
1558 container_of(pf, struct available_var_finder, pf);
1559 struct perf_probe_point *pp = &pf->pev->point;
1560 struct variable_list *vl;
1565 * For some reason (e.g. different column assigned to same address),
1566 * this callback can be called with the address which already passed.
1569 if (available_var_finder_overlap(af))
1572 /* Check number of tevs */
1573 if (af->nvls == af->max_vls) {
1574 pr_warning("Too many( > %d) probe point found.\n", af->max_vls);
1577 vl = &af->vls[af->nvls++];
1579 /* Trace point should be converted from subprogram DIE */
1580 ret = convert_to_trace_point(&pf->sp_die, af->mod, pf->addr,
1581 pp->retprobe, pp->function, &vl->point);
1585 pr_debug("Probe point found: %s+%lu\n", vl->point.symbol,
1588 /* Find local variables */
1589 vl->vars = strlist__new(NULL, NULL);
1590 if (vl->vars == NULL)
1593 die_find_child(sc_die, collect_variables_cb, (void *)af, &die_mem);
1595 /* Find external variables */
1596 if (!probe_conf.show_ext_vars)
1598 /* Don't need to search child DIE for external vars. */
1600 die_find_child(&pf->cu_die, collect_variables_cb, (void *)af, &die_mem);
1603 if (strlist__empty(vl->vars)) {
1604 strlist__delete(vl->vars);
1612 * Find available variables at given probe point
1613 * Return the number of found probe points. Return 0 if there is no
1614 * matched probe point. Return <0 if an error occurs.
1616 int debuginfo__find_available_vars_at(struct debuginfo *dbg,
1617 struct perf_probe_event *pev,
1618 struct variable_list **vls)
1620 struct available_var_finder af = {
1621 .pf = {.pev = pev, .callback = add_available_vars},
1623 .max_vls = probe_conf.max_probes};
1626 /* Allocate result vls array */
1627 *vls = zalloc(sizeof(struct variable_list) * af.max_vls);
1634 ret = debuginfo__find_probes(dbg, &af.pf);
1636 /* Free vlist for error */
1638 zfree(&af.vls[af.nvls].point.symbol);
1639 strlist__delete(af.vls[af.nvls].vars);
1645 return (ret < 0) ? ret : af.nvls;
1648 /* For the kernel module, we need a special code to get a DIE */
1649 int debuginfo__get_text_offset(struct debuginfo *dbg, Dwarf_Addr *offs,
1656 GElf_Shdr mem, *shdr;
1659 elf = dwfl_module_getelf(dbg->mod, &dbg->bias);
1663 /* Get the number of relocations */
1664 n = dwfl_module_relocations(dbg->mod);
1667 /* Search the relocation related .text section */
1668 for (i = 0; i < n; i++) {
1669 p = dwfl_module_relocation_info(dbg->mod, i, &shndx);
1670 if (strcmp(p, ".text") == 0) {
1671 /* OK, get the section header */
1672 scn = elf_getscn(elf, shndx);
1675 shdr = gelf_getshdr(scn, &mem);
1678 *offs = shdr->sh_addr;
1680 *offs -= shdr->sh_offset;
1686 /* Reverse search */
1687 int debuginfo__find_probe_point(struct debuginfo *dbg, unsigned long addr,
1688 struct perf_probe_point *ppt)
1690 Dwarf_Die cudie, spdie, indie;
1691 Dwarf_Addr _addr = 0, baseaddr = 0;
1692 const char *fname = NULL, *func = NULL, *basefunc = NULL, *tmp;
1693 int baseline = 0, lineno = 0, ret = 0;
1695 /* We always need to relocate the address for aranges */
1696 if (debuginfo__get_text_offset(dbg, &baseaddr, false) == 0)
1699 if (!dwarf_addrdie(dbg->dbg, (Dwarf_Addr)addr, &cudie)) {
1700 pr_warning("Failed to find debug information for address %lx\n",
1706 /* Find a corresponding line (filename and lineno) */
1707 cu_find_lineinfo(&cudie, addr, &fname, &lineno);
1708 /* Don't care whether it failed or not */
1710 /* Find a corresponding function (name, baseline and baseaddr) */
1711 if (die_find_realfunc(&cudie, (Dwarf_Addr)addr, &spdie)) {
1712 /* Get function entry information */
1713 func = basefunc = dwarf_diename(&spdie);
1715 die_entrypc(&spdie, &baseaddr) != 0 ||
1716 dwarf_decl_line(&spdie, &baseline) != 0) {
1721 fname = dwarf_decl_file(&spdie);
1722 if (addr == (unsigned long)baseaddr) {
1723 /* Function entry - Relative line number is 0 */
1728 /* Track down the inline functions step by step */
1729 while (die_find_top_inlinefunc(&spdie, (Dwarf_Addr)addr,
1731 /* There is an inline function */
1732 if (die_entrypc(&indie, &_addr) == 0 &&
1735 * addr is at an inline function entry.
1736 * In this case, lineno should be the call-site
1737 * line number. (overwrite lineinfo)
1739 lineno = die_get_call_lineno(&indie);
1740 fname = die_get_call_file(&indie);
1744 * addr is in an inline function body.
1745 * Since lineno points one of the lines
1746 * of the inline function, baseline should
1747 * be the entry line of the inline function.
1749 tmp = dwarf_diename(&indie);
1751 dwarf_decl_line(&indie, &baseline) != 0)
1757 /* Verify the lineno and baseline are in a same file */
1758 tmp = dwarf_decl_file(&spdie);
1759 if (!tmp || strcmp(tmp, fname) != 0)
1764 /* Make a relative line number or an offset */
1766 ppt->line = lineno - baseline;
1767 else if (basefunc) {
1768 ppt->offset = addr - (unsigned long)baseaddr;
1772 /* Duplicate strings */
1774 ppt->function = strdup(func);
1775 if (ppt->function == NULL) {
1781 ppt->file = strdup(fname);
1782 if (ppt->file == NULL) {
1783 zfree(&ppt->function);
1789 if (ret == 0 && (fname || func))
1790 ret = 1; /* Found a point */
1794 /* Add a line and store the src path */
1795 static int line_range_add_line(const char *src, unsigned int lineno,
1796 struct line_range *lr)
1798 /* Copy source path */
1800 lr->path = strdup(src);
1801 if (lr->path == NULL)
1804 return intlist__add(lr->line_list, lineno);
1807 static int line_range_walk_cb(const char *fname, int lineno,
1808 Dwarf_Addr addr __maybe_unused,
1811 struct line_finder *lf = data;
1812 const char *__fname;
1816 if ((strtailcmp(fname, lf->fname) != 0) ||
1817 (lf->lno_s > lineno || lf->lno_e < lineno))
1820 /* Make sure this line can be reversable */
1821 if (cu_find_lineinfo(&lf->cu_die, addr, &__fname, &__lineno) > 0
1822 && (lineno != __lineno || strcmp(fname, __fname)))
1825 err = line_range_add_line(fname, lineno, lf->lr);
1826 if (err < 0 && err != -EEXIST)
1832 /* Find line range from its line number */
1833 static int find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
1837 ret = die_walk_lines(sp_die ?: &lf->cu_die, line_range_walk_cb, lf);
1841 if (!intlist__empty(lf->lr->line_list))
1842 ret = lf->found = 1;
1844 ret = 0; /* Lines are not found */
1846 zfree(&lf->lr->path);
1851 static int line_range_inline_cb(Dwarf_Die *in_die, void *data)
1853 int ret = find_line_range_by_line(in_die, data);
1856 * We have to check all instances of inlined function, because
1857 * some execution paths can be optimized out depends on the
1858 * function argument of instances. However, if an error occurs,
1859 * it should be handled by the caller.
1861 return ret < 0 ? ret : 0;
1864 /* Search function definition from function name */
1865 static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
1867 struct dwarf_callback_param *param = data;
1868 struct line_finder *lf = param->data;
1869 struct line_range *lr = lf->lr;
1871 /* Check declared file */
1872 if (lr->file && strtailcmp(lr->file, dwarf_decl_file(sp_die)))
1875 if (die_is_func_def(sp_die) &&
1876 die_match_name(sp_die, lr->function)) {
1877 lf->fname = dwarf_decl_file(sp_die);
1878 dwarf_decl_line(sp_die, &lr->offset);
1879 pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
1880 lf->lno_s = lr->offset + lr->start;
1881 if (lf->lno_s < 0) /* Overflow */
1882 lf->lno_s = INT_MAX;
1883 lf->lno_e = lr->offset + lr->end;
1884 if (lf->lno_e < 0) /* Overflow */
1885 lf->lno_e = INT_MAX;
1886 pr_debug("New line range: %d to %d\n", lf->lno_s, lf->lno_e);
1887 lr->start = lf->lno_s;
1888 lr->end = lf->lno_e;
1889 if (!die_is_func_instance(sp_die))
1890 param->retval = die_walk_instances(sp_die,
1891 line_range_inline_cb, lf);
1893 param->retval = find_line_range_by_line(sp_die, lf);
1894 return DWARF_CB_ABORT;
1899 static int find_line_range_by_func(struct line_finder *lf)
1901 struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
1902 dwarf_getfuncs(&lf->cu_die, line_range_search_cb, ¶m, 0);
1903 return param.retval;
1906 int debuginfo__find_line_range(struct debuginfo *dbg, struct line_range *lr)
1908 struct line_finder lf = {.lr = lr, .found = 0};
1910 Dwarf_Off off = 0, noff;
1913 const char *comp_dir;
1915 /* Fastpath: lookup by function name from .debug_pubnames section */
1917 struct pubname_callback_param pubname_param = {
1918 .function = lr->function, .file = lr->file,
1919 .cu_die = &lf.cu_die, .sp_die = &lf.sp_die, .found = 0};
1920 struct dwarf_callback_param line_range_param = {
1921 .data = (void *)&lf, .retval = 0};
1923 dwarf_getpubnames(dbg->dbg, pubname_search_cb,
1925 if (pubname_param.found) {
1926 line_range_search_cb(&lf.sp_die, &line_range_param);
1932 /* Loop on CUs (Compilation Unit) */
1933 while (!lf.found && ret >= 0) {
1934 if (dwarf_nextcu(dbg->dbg, off, &noff, &cuhl,
1935 NULL, NULL, NULL) != 0)
1938 /* Get the DIE(Debugging Information Entry) of this CU */
1939 diep = dwarf_offdie(dbg->dbg, off + cuhl, &lf.cu_die);
1943 /* Check if target file is included. */
1945 lf.fname = cu_find_realpath(&lf.cu_die, lr->file);
1949 if (!lr->file || lf.fname) {
1951 ret = find_line_range_by_func(&lf);
1953 lf.lno_s = lr->start;
1955 ret = find_line_range_by_line(NULL, &lf);
1962 /* Store comp_dir */
1964 comp_dir = cu_get_comp_dir(&lf.cu_die);
1966 lr->comp_dir = strdup(comp_dir);
1972 pr_debug("path: %s\n", lr->path);
1973 return (ret < 0) ? ret : lf.found;
1977 * Find a src file from a DWARF tag path. Prepend optional source path prefix
1978 * and chop off leading directories that do not exist. Result is passed back as
1979 * a newly allocated path on success.
1980 * Return 0 if file was found and readable, -errno otherwise.
1982 int get_real_path(const char *raw_path, const char *comp_dir,
1985 const char *prefix = symbol_conf.source_prefix;
1988 if (raw_path[0] != '/' && comp_dir)
1989 /* If not an absolute path, try to use comp_dir */
1992 if (access(raw_path, R_OK) == 0) {
1993 *new_path = strdup(raw_path);
1994 return *new_path ? 0 : -ENOMEM;
2000 *new_path = malloc((strlen(prefix) + strlen(raw_path) + 2));
2005 sprintf(*new_path, "%s/%s", prefix, raw_path);
2007 if (access(*new_path, R_OK) == 0)
2010 if (!symbol_conf.source_prefix) {
2011 /* In case of searching comp_dir, don't retry */
2021 raw_path = strchr(++raw_path, '/');