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>
33 #include <dwarf-regs.h>
35 #include <linux/bitops.h>
41 #include "probe-finder.h"
43 /* Kprobe tracer basic type is up to u64 */
44 #define MAX_BASIC_TYPE_BITS 64
46 /* Dwarf FL wrappers */
47 static char *debuginfo_path; /* Currently dummy */
49 static const Dwfl_Callbacks offline_callbacks = {
50 .find_debuginfo = dwfl_standard_find_debuginfo,
51 .debuginfo_path = &debuginfo_path,
53 .section_address = dwfl_offline_section_address,
55 /* We use this table for core files too. */
56 .find_elf = dwfl_build_id_find_elf,
59 /* Get a Dwarf from offline image */
60 static int debuginfo__init_offline_dwarf(struct debuginfo *dbg,
65 fd = open(path, O_RDONLY);
69 dbg->dwfl = dwfl_begin(&offline_callbacks);
73 dbg->mod = dwfl_report_offline(dbg->dwfl, "", "", fd);
77 dbg->dbg = dwfl_module_getdwarf(dbg->mod, &dbg->bias);
87 memset(dbg, 0, sizeof(*dbg));
92 #if _ELFUTILS_PREREQ(0, 148)
93 /* This method is buggy if elfutils is older than 0.148 */
94 static int __linux_kernel_find_elf(Dwfl_Module *mod,
96 const char *module_name,
98 char **file_name, Elf **elfp)
101 const char *path = kernel_get_module_path(module_name);
103 pr_debug2("Use file %s for %s\n", path, module_name);
105 fd = open(path, O_RDONLY);
107 *file_name = strdup(path);
111 /* If failed, try to call standard method */
112 return dwfl_linux_kernel_find_elf(mod, userdata, module_name, base,
116 static const Dwfl_Callbacks kernel_callbacks = {
117 .find_debuginfo = dwfl_standard_find_debuginfo,
118 .debuginfo_path = &debuginfo_path,
120 .find_elf = __linux_kernel_find_elf,
121 .section_address = dwfl_linux_kernel_module_section_address,
124 /* Get a Dwarf from live kernel image */
125 static int debuginfo__init_online_kernel_dwarf(struct debuginfo *dbg,
128 dbg->dwfl = dwfl_begin(&kernel_callbacks);
132 /* Load the kernel dwarves: Don't care the result here */
133 dwfl_linux_kernel_report_kernel(dbg->dwfl);
134 dwfl_linux_kernel_report_modules(dbg->dwfl);
136 dbg->dbg = dwfl_addrdwarf(dbg->dwfl, addr, &dbg->bias);
137 /* Here, check whether we could get a real dwarf */
139 pr_debug("Failed to find kernel dwarf at %lx\n",
140 (unsigned long)addr);
142 memset(dbg, 0, sizeof(*dbg));
149 /* With older elfutils, this just support kernel module... */
150 static int debuginfo__init_online_kernel_dwarf(struct debuginfo *dbg,
151 Dwarf_Addr addr __maybe_unused)
153 const char *path = kernel_get_module_path("kernel");
156 pr_err("Failed to find vmlinux path\n");
160 pr_debug2("Use file %s for debuginfo\n", path);
161 return debuginfo__init_offline_dwarf(dbg, path);
165 struct debuginfo *debuginfo__new(const char *path)
167 struct debuginfo *dbg = zalloc(sizeof(*dbg));
171 if (debuginfo__init_offline_dwarf(dbg, path) < 0)
177 struct debuginfo *debuginfo__new_online_kernel(unsigned long addr)
179 struct debuginfo *dbg = zalloc(sizeof(*dbg));
184 if (debuginfo__init_online_kernel_dwarf(dbg, (Dwarf_Addr)addr) < 0)
190 void debuginfo__delete(struct debuginfo *dbg)
200 * Probe finder related functions
203 static struct probe_trace_arg_ref *alloc_trace_arg_ref(long offs)
205 struct probe_trace_arg_ref *ref;
206 ref = zalloc(sizeof(struct probe_trace_arg_ref));
213 * Convert a location into trace_arg.
214 * If tvar == NULL, this just checks variable can be converted.
215 * If fentry == true and vr_die is a parameter, do huristic search
216 * for the location fuzzed by function entry mcount.
218 static int convert_variable_location(Dwarf_Die *vr_die, Dwarf_Addr addr,
219 Dwarf_Op *fb_ops, Dwarf_Die *sp_die,
220 struct probe_trace_arg *tvar)
222 Dwarf_Attribute attr;
232 if (dwarf_attr(vr_die, DW_AT_external, &attr) != NULL)
235 /* TODO: handle more than 1 exprs */
236 if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL)
237 return -EINVAL; /* Broken DIE ? */
238 if (dwarf_getlocation_addr(&attr, addr, &op, &nops, 1) <= 0) {
239 ret = dwarf_entrypc(sp_die, &tmp);
240 if (ret || addr != tmp ||
241 dwarf_tag(vr_die) != DW_TAG_formal_parameter ||
242 dwarf_highpc(sp_die, &tmp))
245 * This is fuzzed by fentry mcount. We try to find the
246 * parameter location at the earliest address.
248 for (addr += 1; addr <= tmp; addr++) {
249 if (dwarf_getlocation_addr(&attr, addr, &op,
257 /* TODO: Support const_value */
260 if (op->atom == DW_OP_addr) {
264 /* Static variables on memory (not stack), make @varname */
265 ret = strlen(dwarf_diename(vr_die));
266 tvar->value = zalloc(ret + 2);
267 if (tvar->value == NULL)
269 snprintf(tvar->value, ret + 2, "@%s", dwarf_diename(vr_die));
270 tvar->ref = alloc_trace_arg_ref((long)offs);
271 if (tvar->ref == NULL)
276 /* If this is based on frame buffer, set the offset */
277 if (op->atom == DW_OP_fbreg) {
285 if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) {
286 regn = op->atom - DW_OP_breg0;
289 } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) {
290 regn = op->atom - DW_OP_reg0;
291 } else if (op->atom == DW_OP_bregx) {
295 } else if (op->atom == DW_OP_regx) {
298 pr_debug("DW_OP %x is not supported.\n", op->atom);
305 regs = get_arch_regstr(regn);
307 /* This should be a bug in DWARF or this tool */
308 pr_warning("Mapping for the register number %u "
309 "missing on this architecture.\n", regn);
313 tvar->value = strdup(regs);
314 if (tvar->value == NULL)
318 tvar->ref = alloc_trace_arg_ref((long)offs);
319 if (tvar->ref == NULL)
325 #define BYTES_TO_BITS(nb) ((nb) * BITS_PER_LONG / sizeof(long))
327 static int convert_variable_type(Dwarf_Die *vr_die,
328 struct probe_trace_arg *tvar,
331 struct probe_trace_arg_ref **ref_ptr = &tvar->ref;
334 int bsize, boffs, total;
337 /* TODO: check all types */
338 if (cast && strcmp(cast, "string") != 0) {
339 /* Non string type is OK */
340 tvar->type = strdup(cast);
341 return (tvar->type == NULL) ? -ENOMEM : 0;
344 bsize = dwarf_bitsize(vr_die);
346 /* This is a bitfield */
347 boffs = dwarf_bitoffset(vr_die);
348 total = dwarf_bytesize(vr_die);
349 if (boffs < 0 || total < 0)
351 ret = snprintf(buf, 16, "b%d@%d/%zd", bsize, boffs,
352 BYTES_TO_BITS(total));
356 if (die_get_real_type(vr_die, &type) == NULL) {
357 pr_warning("Failed to get a type information of %s.\n",
358 dwarf_diename(vr_die));
362 pr_debug("%s type is %s.\n",
363 dwarf_diename(vr_die), dwarf_diename(&type));
365 if (cast && strcmp(cast, "string") == 0) { /* String type */
366 ret = dwarf_tag(&type);
367 if (ret != DW_TAG_pointer_type &&
368 ret != DW_TAG_array_type) {
369 pr_warning("Failed to cast into string: "
370 "%s(%s) is not a pointer nor array.\n",
371 dwarf_diename(vr_die), dwarf_diename(&type));
374 if (die_get_real_type(&type, &type) == NULL) {
375 pr_warning("Failed to get a type"
379 if (ret == DW_TAG_pointer_type) {
381 ref_ptr = &(*ref_ptr)->next;
382 /* Add new reference with offset +0 */
383 *ref_ptr = zalloc(sizeof(struct probe_trace_arg_ref));
384 if (*ref_ptr == NULL) {
385 pr_warning("Out of memory error\n");
389 if (!die_compare_name(&type, "char") &&
390 !die_compare_name(&type, "unsigned char")) {
391 pr_warning("Failed to cast into string: "
392 "%s is not (unsigned) char *.\n",
393 dwarf_diename(vr_die));
396 tvar->type = strdup(cast);
397 return (tvar->type == NULL) ? -ENOMEM : 0;
400 ret = dwarf_bytesize(&type);
402 /* No size ... try to use default type */
404 ret = BYTES_TO_BITS(ret);
406 /* Check the bitwidth */
407 if (ret > MAX_BASIC_TYPE_BITS) {
408 pr_info("%s exceeds max-bitwidth. Cut down to %d bits.\n",
409 dwarf_diename(&type), MAX_BASIC_TYPE_BITS);
410 ret = MAX_BASIC_TYPE_BITS;
412 ret = snprintf(buf, 16, "%c%d",
413 die_is_signed_type(&type) ? 's' : 'u', ret);
416 if (ret < 0 || ret >= 16) {
419 pr_warning("Failed to convert variable type: %s\n",
423 tvar->type = strdup(buf);
424 if (tvar->type == NULL)
429 static int convert_variable_fields(Dwarf_Die *vr_die, const char *varname,
430 struct perf_probe_arg_field *field,
431 struct probe_trace_arg_ref **ref_ptr,
434 struct probe_trace_arg_ref *ref = *ref_ptr;
439 pr_debug("converting %s in %s\n", field->name, varname);
440 if (die_get_real_type(vr_die, &type) == NULL) {
441 pr_warning("Failed to get the type of %s.\n", varname);
444 pr_debug2("Var real type: (%x)\n", (unsigned)dwarf_dieoffset(&type));
445 tag = dwarf_tag(&type);
447 if (field->name[0] == '[' &&
448 (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type)) {
450 /* Save original type for next field */
451 memcpy(die_mem, &type, sizeof(*die_mem));
452 /* Get the type of this array */
453 if (die_get_real_type(&type, &type) == NULL) {
454 pr_warning("Failed to get the type of %s.\n", varname);
457 pr_debug2("Array real type: (%x)\n",
458 (unsigned)dwarf_dieoffset(&type));
459 if (tag == DW_TAG_pointer_type) {
460 ref = zalloc(sizeof(struct probe_trace_arg_ref));
464 (*ref_ptr)->next = ref;
468 ref->offset += dwarf_bytesize(&type) * field->index;
470 /* Save vr_die for converting types */
471 memcpy(die_mem, vr_die, sizeof(*die_mem));
473 } else if (tag == DW_TAG_pointer_type) {
474 /* Check the pointer and dereference */
476 pr_err("Semantic error: %s must be referred by '->'\n",
480 /* Get the type pointed by this pointer */
481 if (die_get_real_type(&type, &type) == NULL) {
482 pr_warning("Failed to get the type of %s.\n", varname);
485 /* Verify it is a data structure */
486 tag = dwarf_tag(&type);
487 if (tag != DW_TAG_structure_type && tag != DW_TAG_union_type) {
488 pr_warning("%s is not a data structure nor an union.\n",
493 ref = zalloc(sizeof(struct probe_trace_arg_ref));
497 (*ref_ptr)->next = ref;
501 /* Verify it is a data structure */
502 if (tag != DW_TAG_structure_type && tag != DW_TAG_union_type) {
503 pr_warning("%s is not a data structure nor an union.\n",
507 if (field->name[0] == '[') {
508 pr_err("Semantic error: %s is not a pointor"
509 " nor array.\n", varname);
513 pr_err("Semantic error: %s must be referred by '.'\n",
518 pr_warning("Structure on a register is not "
524 if (die_find_member(&type, field->name, die_mem) == NULL) {
525 pr_warning("%s(type:%s) has no member %s.\n", varname,
526 dwarf_diename(&type), field->name);
530 /* Get the offset of the field */
531 if (tag == DW_TAG_union_type) {
534 ret = die_get_data_member_location(die_mem, &offs);
536 pr_warning("Failed to get the offset of %s.\n",
541 ref->offset += (long)offs;
544 /* Converting next field */
546 return convert_variable_fields(die_mem, field->name,
547 field->next, &ref, die_mem);
552 /* Show a variables in kprobe event format */
553 static int convert_variable(Dwarf_Die *vr_die, struct probe_finder *pf)
558 pr_debug("Converting variable %s into trace event.\n",
559 dwarf_diename(vr_die));
561 ret = convert_variable_location(vr_die, pf->addr, pf->fb_ops,
562 &pf->sp_die, pf->tvar);
564 pr_err("Failed to find the location of %s at this address.\n"
565 " Perhaps, it has been optimized out.\n", pf->pvar->var);
566 else if (ret == -ENOTSUP)
567 pr_err("Sorry, we don't support this variable location yet.\n");
568 else if (pf->pvar->field) {
569 ret = convert_variable_fields(vr_die, pf->pvar->var,
570 pf->pvar->field, &pf->tvar->ref,
575 ret = convert_variable_type(vr_die, pf->tvar, pf->pvar->type);
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 if (!is_c_varname(pf->pvar->var)) {
588 /* Copy raw parameters */
589 pf->tvar->value = strdup(pf->pvar->var);
590 if (pf->tvar->value == NULL)
592 if (pf->pvar->type) {
593 pf->tvar->type = strdup(pf->pvar->type);
594 if (pf->tvar->type == NULL)
597 if (pf->pvar->name) {
598 pf->tvar->name = strdup(pf->pvar->name);
599 if (pf->tvar->name == NULL)
602 pf->tvar->name = NULL;
607 pf->tvar->name = strdup(pf->pvar->name);
609 ret = synthesize_perf_probe_arg(pf->pvar, buf, 32);
612 ptr = strchr(buf, ':'); /* Change type separator to _ */
615 pf->tvar->name = strdup(buf);
617 if (pf->tvar->name == NULL)
620 pr_debug("Searching '%s' variable in context.\n", pf->pvar->var);
621 /* Search child die for local variables and parameters. */
622 if (!die_find_variable_at(sc_die, pf->pvar->var, pf->addr, &vr_die)) {
623 /* Search again in global variables */
624 if (!die_find_variable_at(&pf->cu_die, pf->pvar->var, 0, &vr_die))
628 ret = convert_variable(&vr_die, pf);
631 pr_warning("Failed to find '%s' in this function.\n",
636 /* Convert subprogram DIE to trace point */
637 static int convert_to_trace_point(Dwarf_Die *sp_die, Dwfl_Module *mod,
638 Dwarf_Addr paddr, bool retprobe,
639 struct probe_trace_point *tp)
641 Dwarf_Addr eaddr, highaddr;
645 /* Verify the address is correct */
646 if (dwarf_entrypc(sp_die, &eaddr) != 0) {
647 pr_warning("Failed to get entry address of %s\n",
648 dwarf_diename(sp_die));
651 if (dwarf_highpc(sp_die, &highaddr) != 0) {
652 pr_warning("Failed to get end address of %s\n",
653 dwarf_diename(sp_die));
656 if (paddr > highaddr) {
657 pr_warning("Offset specified is greater than size of %s\n",
658 dwarf_diename(sp_die));
662 /* Get an appropriate symbol from symtab */
663 symbol = dwfl_module_addrsym(mod, paddr, &sym, NULL);
665 pr_warning("Failed to find symbol at 0x%lx\n",
666 (unsigned long)paddr);
669 tp->offset = (unsigned long)(paddr - sym.st_value);
670 tp->address = (unsigned long)paddr;
671 tp->symbol = strdup(symbol);
675 /* Return probe must be on the head of a subprogram */
677 if (eaddr != paddr) {
678 pr_warning("Return probe must be on the head of"
679 " a real function.\n");
688 /* Call probe_finder callback with scope DIE */
689 static int call_probe_finder(Dwarf_Die *sc_die, struct probe_finder *pf)
691 Dwarf_Attribute fb_attr;
696 pr_err("Caller must pass a scope DIE. Program error.\n");
700 /* If not a real subprogram, find a real one */
701 if (!die_is_func_def(sc_die)) {
702 if (!die_find_realfunc(&pf->cu_die, pf->addr, &pf->sp_die)) {
703 pr_warning("Failed to find probe point in any "
708 memcpy(&pf->sp_die, sc_die, sizeof(Dwarf_Die));
710 /* Get the frame base attribute/ops from subprogram */
711 dwarf_attr(&pf->sp_die, DW_AT_frame_base, &fb_attr);
712 ret = dwarf_getlocation_addr(&fb_attr, pf->addr, &pf->fb_ops, &nops, 1);
713 if (ret <= 0 || nops == 0) {
715 #if _ELFUTILS_PREREQ(0, 142)
716 } else if (nops == 1 && pf->fb_ops[0].atom == DW_OP_call_frame_cfa &&
719 if (dwarf_cfi_addrframe(pf->cfi, pf->addr, &frame) != 0 ||
720 dwarf_frame_cfa(frame, &pf->fb_ops, &nops) != 0) {
721 pr_warning("Failed to get call frame on 0x%jx\n",
722 (uintmax_t)pf->addr);
728 /* Call finder's callback handler */
729 ret = pf->callback(sc_die, pf);
731 /* *pf->fb_ops will be cached in libdw. Don't free it. */
737 struct find_scope_param {
738 const char *function;
746 static int find_best_scope_cb(Dwarf_Die *fn_die, void *data)
748 struct find_scope_param *fsp = data;
752 /* Skip if declared file name does not match */
754 file = dwarf_decl_file(fn_die);
755 if (!file || strcmp(fsp->file, file) != 0)
758 /* If the function name is given, that's what user expects */
760 if (die_compare_name(fn_die, fsp->function)) {
761 memcpy(fsp->die_mem, fn_die, sizeof(Dwarf_Die));
766 /* With the line number, find the nearest declared DIE */
767 dwarf_decl_line(fn_die, &lno);
768 if (lno < fsp->line && fsp->diff > fsp->line - lno) {
769 /* Keep a candidate and continue */
770 fsp->diff = fsp->line - lno;
771 memcpy(fsp->die_mem, fn_die, sizeof(Dwarf_Die));
778 /* Find an appropriate scope fits to given conditions */
779 static Dwarf_Die *find_best_scope(struct probe_finder *pf, Dwarf_Die *die_mem)
781 struct find_scope_param fsp = {
782 .function = pf->pev->point.function,
790 cu_walk_functions_at(&pf->cu_die, pf->addr, find_best_scope_cb, &fsp);
792 return fsp.found ? die_mem : NULL;
795 static int probe_point_line_walker(const char *fname, int lineno,
796 Dwarf_Addr addr, void *data)
798 struct probe_finder *pf = data;
799 Dwarf_Die *sc_die, die_mem;
802 if (lineno != pf->lno || strtailcmp(fname, pf->fname) != 0)
806 sc_die = find_best_scope(pf, &die_mem);
808 pr_warning("Failed to find scope of probe point.\n");
812 ret = call_probe_finder(sc_die, pf);
814 /* Continue if no error, because the line will be in inline function */
815 return ret < 0 ? ret : 0;
818 /* Find probe point from its line number */
819 static int find_probe_point_by_line(struct probe_finder *pf)
821 return die_walk_lines(&pf->cu_die, probe_point_line_walker, pf);
824 /* Find lines which match lazy pattern */
825 static int find_lazy_match_lines(struct intlist *list,
826 const char *fname, const char *pat)
832 int count = 0, linenum = 1;
834 fp = fopen(fname, "r");
836 pr_warning("Failed to open %s: %s\n", fname, strerror(errno));
840 while ((len = getline(&line, &line_len, fp)) > 0) {
842 if (line[len - 1] == '\n')
843 line[len - 1] = '\0';
845 if (strlazymatch(line, pat)) {
846 intlist__add(list, linenum);
858 pr_debug("No matched lines found in %s.\n", fname);
862 static int probe_point_lazy_walker(const char *fname, int lineno,
863 Dwarf_Addr addr, void *data)
865 struct probe_finder *pf = data;
866 Dwarf_Die *sc_die, die_mem;
869 if (!intlist__has_entry(pf->lcache, lineno) ||
870 strtailcmp(fname, pf->fname) != 0)
873 pr_debug("Probe line found: line:%d addr:0x%llx\n",
874 lineno, (unsigned long long)addr);
877 sc_die = find_best_scope(pf, &die_mem);
879 pr_warning("Failed to find scope of probe point.\n");
883 ret = call_probe_finder(sc_die, pf);
886 * Continue if no error, because the lazy pattern will match
889 return ret < 0 ? ret : 0;
892 /* Find probe points from lazy pattern */
893 static int find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf)
897 if (intlist__empty(pf->lcache)) {
898 /* Matching lazy line pattern */
899 ret = find_lazy_match_lines(pf->lcache, pf->fname,
900 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_compare_name(sp_die, pp->function))
953 /* Check declared file */
954 if (pp->file && strtailcmp(pp->file, dwarf_decl_file(sp_die)))
957 pf->fname = dwarf_decl_file(sp_die);
958 if (pp->line) { /* Function relative line */
959 dwarf_decl_line(sp_die, &pf->lno);
961 param->retval = find_probe_point_by_line(pf);
962 } else if (!dwarf_func_inline(sp_die)) {
965 param->retval = find_probe_point_lazy(sp_die, pf);
967 if (dwarf_entrypc(sp_die, &pf->addr) != 0) {
968 pr_warning("Failed to get entry address of "
969 "%s.\n", dwarf_diename(sp_die));
970 param->retval = -ENOENT;
971 return DWARF_CB_ABORT;
973 pf->addr += pp->offset;
974 /* TODO: Check the address in this function */
975 param->retval = call_probe_finder(sp_die, pf);
978 /* Inlined function: search instances */
979 param->retval = die_walk_instances(sp_die,
980 probe_point_inline_cb, (void *)pf);
982 return DWARF_CB_ABORT; /* Exit; no same symbol in this CU. */
985 static int find_probe_point_by_func(struct probe_finder *pf)
987 struct dwarf_callback_param _param = {.data = (void *)pf,
989 dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, &_param, 0);
990 return _param.retval;
993 struct pubname_callback_param {
1001 static int pubname_search_cb(Dwarf *dbg, Dwarf_Global *gl, void *data)
1003 struct pubname_callback_param *param = data;
1005 if (dwarf_offdie(dbg, gl->die_offset, param->sp_die)) {
1006 if (dwarf_tag(param->sp_die) != DW_TAG_subprogram)
1009 if (die_compare_name(param->sp_die, param->function)) {
1010 if (!dwarf_offdie(dbg, gl->cu_offset, param->cu_die))
1014 strtailcmp(param->file, dwarf_decl_file(param->sp_die)))
1018 return DWARF_CB_ABORT;
1025 /* Find probe points from debuginfo */
1026 static int debuginfo__find_probes(struct debuginfo *dbg,
1027 struct probe_finder *pf)
1029 struct perf_probe_point *pp = &pf->pev->point;
1030 Dwarf_Off off, noff;
1035 #if _ELFUTILS_PREREQ(0, 142)
1036 /* Get the call frame information from this dwarf */
1037 pf->cfi = dwarf_getcfi(dbg->dbg);
1041 pf->lcache = intlist__new(NULL);
1045 /* Fastpath: lookup by function name from .debug_pubnames section */
1047 struct pubname_callback_param pubname_param = {
1048 .function = pp->function,
1050 .cu_die = &pf->cu_die,
1051 .sp_die = &pf->sp_die,
1054 struct dwarf_callback_param probe_param = {
1058 dwarf_getpubnames(dbg->dbg, pubname_search_cb,
1060 if (pubname_param.found) {
1061 ret = probe_point_search_cb(&pf->sp_die, &probe_param);
1067 /* Loop on CUs (Compilation Unit) */
1068 while (!dwarf_nextcu(dbg->dbg, off, &noff, &cuhl, NULL, NULL, NULL)) {
1069 /* Get the DIE(Debugging Information Entry) of this CU */
1070 diep = dwarf_offdie(dbg->dbg, off + cuhl, &pf->cu_die);
1074 /* Check if target file is included. */
1076 pf->fname = cu_find_realpath(&pf->cu_die, pp->file);
1080 if (!pp->file || pf->fname) {
1082 ret = find_probe_point_by_func(pf);
1083 else if (pp->lazy_line)
1084 ret = find_probe_point_lazy(NULL, pf);
1087 ret = find_probe_point_by_line(pf);
1096 intlist__delete(pf->lcache);
1102 struct local_vars_finder {
1103 struct probe_finder *pf;
1104 struct perf_probe_arg *args;
1110 /* Collect available variables in this scope */
1111 static int copy_variables_cb(Dwarf_Die *die_mem, void *data)
1113 struct local_vars_finder *vf = data;
1114 struct probe_finder *pf = vf->pf;
1117 tag = dwarf_tag(die_mem);
1118 if (tag == DW_TAG_formal_parameter ||
1119 tag == DW_TAG_variable) {
1120 if (convert_variable_location(die_mem, vf->pf->addr,
1121 vf->pf->fb_ops, &pf->sp_die,
1123 vf->args[vf->nargs].var = (char *)dwarf_diename(die_mem);
1124 if (vf->args[vf->nargs].var == NULL) {
1126 return DIE_FIND_CB_END;
1128 pr_debug(" %s", vf->args[vf->nargs].var);
1133 if (dwarf_haspc(die_mem, vf->pf->addr))
1134 return DIE_FIND_CB_CONTINUE;
1136 return DIE_FIND_CB_SIBLING;
1139 static int expand_probe_args(Dwarf_Die *sc_die, struct probe_finder *pf,
1140 struct perf_probe_arg *args)
1145 struct local_vars_finder vf = {.pf = pf, .args = args,
1146 .max_args = MAX_PROBE_ARGS, .ret = 0};
1148 for (i = 0; i < pf->pev->nargs; i++) {
1149 /* var never be NULL */
1150 if (strcmp(pf->pev->args[i].var, "$vars") == 0) {
1151 pr_debug("Expanding $vars into:");
1153 /* Special local variables */
1154 die_find_child(sc_die, copy_variables_cb, (void *)&vf,
1156 pr_debug(" (%d)\n", vf.nargs - n);
1161 /* Copy normal argument */
1162 args[n] = pf->pev->args[i];
1169 /* Add a found probe point into trace event list */
1170 static int add_probe_trace_event(Dwarf_Die *sc_die, struct probe_finder *pf)
1172 struct trace_event_finder *tf =
1173 container_of(pf, struct trace_event_finder, pf);
1174 struct probe_trace_event *tev;
1175 struct perf_probe_arg *args;
1178 /* Check number of tevs */
1179 if (tf->ntevs == tf->max_tevs) {
1180 pr_warning("Too many( > %d) probe point found.\n",
1184 tev = &tf->tevs[tf->ntevs++];
1186 /* Trace point should be converted from subprogram DIE */
1187 ret = convert_to_trace_point(&pf->sp_die, tf->mod, pf->addr,
1188 pf->pev->point.retprobe, &tev->point);
1192 pr_debug("Probe point found: %s+%lu\n", tev->point.symbol,
1195 /* Expand special probe argument if exist */
1196 args = zalloc(sizeof(struct perf_probe_arg) * MAX_PROBE_ARGS);
1200 ret = expand_probe_args(sc_die, pf, args);
1205 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
1206 if (tev->args == NULL) {
1211 /* Find each argument */
1212 for (i = 0; i < tev->nargs; i++) {
1213 pf->pvar = &args[i];
1214 pf->tvar = &tev->args[i];
1215 /* Variable should be found from scope DIE */
1216 ret = find_variable(sc_die, pf);
1226 /* Find probe_trace_events specified by perf_probe_event from debuginfo */
1227 int debuginfo__find_trace_events(struct debuginfo *dbg,
1228 struct perf_probe_event *pev,
1229 struct probe_trace_event **tevs, int max_tevs)
1231 struct trace_event_finder tf = {
1232 .pf = {.pev = pev, .callback = add_probe_trace_event},
1233 .mod = dbg->mod, .max_tevs = max_tevs};
1236 /* Allocate result tevs array */
1237 *tevs = zalloc(sizeof(struct probe_trace_event) * max_tevs);
1244 ret = debuginfo__find_probes(dbg, &tf.pf);
1250 return (ret < 0) ? ret : tf.ntevs;
1253 #define MAX_VAR_LEN 64
1255 /* Collect available variables in this scope */
1256 static int collect_variables_cb(Dwarf_Die *die_mem, void *data)
1258 struct available_var_finder *af = data;
1259 struct variable_list *vl;
1260 char buf[MAX_VAR_LEN];
1263 vl = &af->vls[af->nvls - 1];
1265 tag = dwarf_tag(die_mem);
1266 if (tag == DW_TAG_formal_parameter ||
1267 tag == DW_TAG_variable) {
1268 ret = convert_variable_location(die_mem, af->pf.addr,
1269 af->pf.fb_ops, &af->pf.sp_die,
1272 ret = die_get_varname(die_mem, buf, MAX_VAR_LEN);
1273 pr_debug2("Add new var: %s\n", buf);
1275 strlist__add(vl->vars, buf);
1279 if (af->child && dwarf_haspc(die_mem, af->pf.addr))
1280 return DIE_FIND_CB_CONTINUE;
1282 return DIE_FIND_CB_SIBLING;
1285 /* Add a found vars into available variables list */
1286 static int add_available_vars(Dwarf_Die *sc_die, struct probe_finder *pf)
1288 struct available_var_finder *af =
1289 container_of(pf, struct available_var_finder, pf);
1290 struct variable_list *vl;
1294 /* Check number of tevs */
1295 if (af->nvls == af->max_vls) {
1296 pr_warning("Too many( > %d) probe point found.\n", af->max_vls);
1299 vl = &af->vls[af->nvls++];
1301 /* Trace point should be converted from subprogram DIE */
1302 ret = convert_to_trace_point(&pf->sp_die, af->mod, pf->addr,
1303 pf->pev->point.retprobe, &vl->point);
1307 pr_debug("Probe point found: %s+%lu\n", vl->point.symbol,
1310 /* Find local variables */
1311 vl->vars = strlist__new(true, NULL);
1312 if (vl->vars == NULL)
1315 die_find_child(sc_die, collect_variables_cb, (void *)af, &die_mem);
1317 /* Find external variables */
1320 /* Don't need to search child DIE for externs. */
1322 die_find_child(&pf->cu_die, collect_variables_cb, (void *)af, &die_mem);
1325 if (strlist__empty(vl->vars)) {
1326 strlist__delete(vl->vars);
1333 /* Find available variables at given probe point */
1334 int debuginfo__find_available_vars_at(struct debuginfo *dbg,
1335 struct perf_probe_event *pev,
1336 struct variable_list **vls,
1337 int max_vls, bool externs)
1339 struct available_var_finder af = {
1340 .pf = {.pev = pev, .callback = add_available_vars},
1342 .max_vls = max_vls, .externs = externs};
1345 /* Allocate result vls array */
1346 *vls = zalloc(sizeof(struct variable_list) * max_vls);
1353 ret = debuginfo__find_probes(dbg, &af.pf);
1355 /* Free vlist for error */
1357 zfree(&af.vls[af.nvls].point.symbol);
1358 strlist__delete(af.vls[af.nvls].vars);
1364 return (ret < 0) ? ret : af.nvls;
1367 /* Reverse search */
1368 int debuginfo__find_probe_point(struct debuginfo *dbg, unsigned long addr,
1369 struct perf_probe_point *ppt)
1371 Dwarf_Die cudie, spdie, indie;
1372 Dwarf_Addr _addr = 0, baseaddr = 0;
1373 const char *fname = NULL, *func = NULL, *basefunc = NULL, *tmp;
1374 int baseline = 0, lineno = 0, ret = 0;
1376 /* Adjust address with bias */
1380 if (!dwarf_addrdie(dbg->dbg, (Dwarf_Addr)addr - dbg->bias, &cudie)) {
1381 pr_warning("Failed to find debug information for address %lx\n",
1387 /* Find a corresponding line (filename and lineno) */
1388 cu_find_lineinfo(&cudie, addr, &fname, &lineno);
1389 /* Don't care whether it failed or not */
1391 /* Find a corresponding function (name, baseline and baseaddr) */
1392 if (die_find_realfunc(&cudie, (Dwarf_Addr)addr, &spdie)) {
1393 /* Get function entry information */
1394 func = basefunc = dwarf_diename(&spdie);
1396 dwarf_entrypc(&spdie, &baseaddr) != 0 ||
1397 dwarf_decl_line(&spdie, &baseline) != 0) {
1402 fname = dwarf_decl_file(&spdie);
1403 if (addr == (unsigned long)baseaddr) {
1404 /* Function entry - Relative line number is 0 */
1409 /* Track down the inline functions step by step */
1410 while (die_find_top_inlinefunc(&spdie, (Dwarf_Addr)addr,
1412 /* There is an inline function */
1413 if (dwarf_entrypc(&indie, &_addr) == 0 &&
1416 * addr is at an inline function entry.
1417 * In this case, lineno should be the call-site
1418 * line number. (overwrite lineinfo)
1420 lineno = die_get_call_lineno(&indie);
1421 fname = die_get_call_file(&indie);
1425 * addr is in an inline function body.
1426 * Since lineno points one of the lines
1427 * of the inline function, baseline should
1428 * be the entry line of the inline function.
1430 tmp = dwarf_diename(&indie);
1432 dwarf_decl_line(&indie, &baseline) != 0)
1438 /* Verify the lineno and baseline are in a same file */
1439 tmp = dwarf_decl_file(&spdie);
1440 if (!tmp || strcmp(tmp, fname) != 0)
1445 /* Make a relative line number or an offset */
1447 ppt->line = lineno - baseline;
1448 else if (basefunc) {
1449 ppt->offset = addr - (unsigned long)baseaddr;
1453 /* Duplicate strings */
1455 ppt->function = strdup(func);
1456 if (ppt->function == NULL) {
1462 ppt->file = strdup(fname);
1463 if (ppt->file == NULL) {
1464 zfree(&ppt->function);
1470 if (ret == 0 && (fname || func))
1471 ret = 1; /* Found a point */
1475 /* Add a line and store the src path */
1476 static int line_range_add_line(const char *src, unsigned int lineno,
1477 struct line_range *lr)
1479 /* Copy source path */
1481 lr->path = strdup(src);
1482 if (lr->path == NULL)
1485 return intlist__add(lr->line_list, lineno);
1488 static int line_range_walk_cb(const char *fname, int lineno,
1489 Dwarf_Addr addr __maybe_unused,
1492 struct line_finder *lf = data;
1494 if ((strtailcmp(fname, lf->fname) != 0) ||
1495 (lf->lno_s > lineno || lf->lno_e < lineno))
1498 if (line_range_add_line(fname, lineno, lf->lr) < 0)
1504 /* Find line range from its line number */
1505 static int find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
1509 ret = die_walk_lines(sp_die ?: &lf->cu_die, line_range_walk_cb, lf);
1513 if (!intlist__empty(lf->lr->line_list))
1514 ret = lf->found = 1;
1516 ret = 0; /* Lines are not found */
1518 zfree(&lf->lr->path);
1523 static int line_range_inline_cb(Dwarf_Die *in_die, void *data)
1525 find_line_range_by_line(in_die, data);
1528 * We have to check all instances of inlined function, because
1529 * some execution paths can be optimized out depends on the
1530 * function argument of instances
1535 /* Search function definition from function name */
1536 static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
1538 struct dwarf_callback_param *param = data;
1539 struct line_finder *lf = param->data;
1540 struct line_range *lr = lf->lr;
1542 /* Check declared file */
1543 if (lr->file && strtailcmp(lr->file, dwarf_decl_file(sp_die)))
1546 if (die_is_func_def(sp_die) &&
1547 die_compare_name(sp_die, lr->function)) {
1548 lf->fname = dwarf_decl_file(sp_die);
1549 dwarf_decl_line(sp_die, &lr->offset);
1550 pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
1551 lf->lno_s = lr->offset + lr->start;
1552 if (lf->lno_s < 0) /* Overflow */
1553 lf->lno_s = INT_MAX;
1554 lf->lno_e = lr->offset + lr->end;
1555 if (lf->lno_e < 0) /* Overflow */
1556 lf->lno_e = INT_MAX;
1557 pr_debug("New line range: %d to %d\n", lf->lno_s, lf->lno_e);
1558 lr->start = lf->lno_s;
1559 lr->end = lf->lno_e;
1560 if (dwarf_func_inline(sp_die))
1561 param->retval = die_walk_instances(sp_die,
1562 line_range_inline_cb, lf);
1564 param->retval = find_line_range_by_line(sp_die, lf);
1565 return DWARF_CB_ABORT;
1570 static int find_line_range_by_func(struct line_finder *lf)
1572 struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
1573 dwarf_getfuncs(&lf->cu_die, line_range_search_cb, ¶m, 0);
1574 return param.retval;
1577 int debuginfo__find_line_range(struct debuginfo *dbg, struct line_range *lr)
1579 struct line_finder lf = {.lr = lr, .found = 0};
1581 Dwarf_Off off = 0, noff;
1584 const char *comp_dir;
1586 /* Fastpath: lookup by function name from .debug_pubnames section */
1588 struct pubname_callback_param pubname_param = {
1589 .function = lr->function, .file = lr->file,
1590 .cu_die = &lf.cu_die, .sp_die = &lf.sp_die, .found = 0};
1591 struct dwarf_callback_param line_range_param = {
1592 .data = (void *)&lf, .retval = 0};
1594 dwarf_getpubnames(dbg->dbg, pubname_search_cb,
1596 if (pubname_param.found) {
1597 line_range_search_cb(&lf.sp_die, &line_range_param);
1603 /* Loop on CUs (Compilation Unit) */
1604 while (!lf.found && ret >= 0) {
1605 if (dwarf_nextcu(dbg->dbg, off, &noff, &cuhl,
1606 NULL, NULL, NULL) != 0)
1609 /* Get the DIE(Debugging Information Entry) of this CU */
1610 diep = dwarf_offdie(dbg->dbg, off + cuhl, &lf.cu_die);
1614 /* Check if target file is included. */
1616 lf.fname = cu_find_realpath(&lf.cu_die, lr->file);
1620 if (!lr->file || lf.fname) {
1622 ret = find_line_range_by_func(&lf);
1624 lf.lno_s = lr->start;
1626 ret = find_line_range_by_line(NULL, &lf);
1633 /* Store comp_dir */
1635 comp_dir = cu_get_comp_dir(&lf.cu_die);
1637 lr->comp_dir = strdup(comp_dir);
1643 pr_debug("path: %s\n", lr->path);
1644 return (ret < 0) ? ret : lf.found;