2 * probe-finder.c : C expression to kprobe event converter
4 * Written by Masami Hiramatsu <mhiramat@redhat.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #include <sys/utsname.h>
23 #include <sys/types.h>
34 #include <dwarf-regs.h>
40 #include "probe-finder.h"
42 /* Kprobe tracer basic type is up to u64 */
43 #define MAX_BASIC_TYPE_BITS 64
46 * Compare the tail of two strings.
47 * Return 0 if whole of either string is same as another's tail part.
49 static int strtailcmp(const char *s1, const char *s2)
53 while (--i1 >= 0 && --i2 >= 0) {
55 return s1[i1] - s2[i2];
60 /* Line number list operations */
62 /* Add a line to line number list */
63 static int line_list__add_line(struct list_head *head, int line)
68 /* Reverse search, because new line will be the last one */
69 list_for_each_entry_reverse(ln, head, list) {
70 if (ln->line < line) {
73 } else if (ln->line == line) /* Already exist */
76 /* List is empty, or the smallest entry */
79 pr_debug("line list: add a line %u\n", line);
80 ln = zalloc(sizeof(struct line_node));
84 INIT_LIST_HEAD(&ln->list);
85 list_add(&ln->list, p);
89 /* Check if the line in line number list */
90 static int line_list__has_line(struct list_head *head, int line)
94 /* Reverse search, because new line will be the last one */
95 list_for_each_entry(ln, head, list)
102 /* Init line number list */
103 static void line_list__init(struct list_head *head)
105 INIT_LIST_HEAD(head);
108 /* Free line number list */
109 static void line_list__free(struct list_head *head)
111 struct line_node *ln;
112 while (!list_empty(head)) {
113 ln = list_first_entry(head, struct line_node, list);
121 /* Find the realpath of the target file. */
122 static const char *cu_find_realpath(Dwarf_Die *cu_die, const char *fname)
126 const char *src = NULL;
132 ret = dwarf_getsrcfiles(cu_die, &files, &nfiles);
136 for (i = 0; i < nfiles; i++) {
137 src = dwarf_filesrc(files, i, NULL, NULL);
138 if (strtailcmp(src, fname) == 0)
146 /* Compare diename and tname */
147 static bool die_compare_name(Dwarf_Die *dw_die, const char *tname)
150 name = dwarf_diename(dw_die);
151 return name ? strcmp(tname, name) : -1;
154 /* Get type die, but skip qualifiers and typedef */
155 static Dwarf_Die *die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
157 Dwarf_Attribute attr;
161 if (dwarf_attr(vr_die, DW_AT_type, &attr) == NULL ||
162 dwarf_formref_die(&attr, die_mem) == NULL)
165 tag = dwarf_tag(die_mem);
167 } while (tag == DW_TAG_const_type ||
168 tag == DW_TAG_restrict_type ||
169 tag == DW_TAG_volatile_type ||
170 tag == DW_TAG_shared_type ||
171 tag == DW_TAG_typedef);
176 static bool die_is_signed_type(Dwarf_Die *tp_die)
178 Dwarf_Attribute attr;
181 if (dwarf_attr(tp_die, DW_AT_encoding, &attr) == NULL ||
182 dwarf_formudata(&attr, &ret) != 0)
185 return (ret == DW_ATE_signed_char || ret == DW_ATE_signed ||
186 ret == DW_ATE_signed_fixed);
189 static int die_get_byte_size(Dwarf_Die *tp_die)
191 Dwarf_Attribute attr;
194 if (dwarf_attr(tp_die, DW_AT_byte_size, &attr) == NULL ||
195 dwarf_formudata(&attr, &ret) != 0)
201 /* Get data_member_location offset */
202 static int die_get_data_member_location(Dwarf_Die *mb_die, Dwarf_Word *offs)
204 Dwarf_Attribute attr;
209 if (dwarf_attr(mb_die, DW_AT_data_member_location, &attr) == NULL)
212 if (dwarf_formudata(&attr, offs) != 0) {
213 /* DW_AT_data_member_location should be DW_OP_plus_uconst */
214 ret = dwarf_getlocation(&attr, &expr, &nexpr);
215 if (ret < 0 || nexpr == 0)
218 if (expr[0].atom != DW_OP_plus_uconst || nexpr != 1) {
219 pr_debug("Unable to get offset:Unexpected OP %x (%zd)\n",
220 expr[0].atom, nexpr);
223 *offs = (Dwarf_Word)expr[0].number;
228 /* Return values for die_find callbacks */
230 DIE_FIND_CB_FOUND = 0, /* End of Search */
231 DIE_FIND_CB_CHILD = 1, /* Search only children */
232 DIE_FIND_CB_SIBLING = 2, /* Search only siblings */
233 DIE_FIND_CB_CONTINUE = 3, /* Search children and siblings */
236 /* Search a child die */
237 static Dwarf_Die *die_find_child(Dwarf_Die *rt_die,
238 int (*callback)(Dwarf_Die *, void *),
239 void *data, Dwarf_Die *die_mem)
244 ret = dwarf_child(rt_die, die_mem);
249 ret = callback(die_mem, data);
250 if (ret == DIE_FIND_CB_FOUND)
253 if ((ret & DIE_FIND_CB_CHILD) &&
254 die_find_child(die_mem, callback, data, &child_die)) {
255 memcpy(die_mem, &child_die, sizeof(Dwarf_Die));
258 } while ((ret & DIE_FIND_CB_SIBLING) &&
259 dwarf_siblingof(die_mem, die_mem) == 0);
264 struct __addr_die_search_param {
269 static int __die_search_func_cb(Dwarf_Die *fn_die, void *data)
271 struct __addr_die_search_param *ad = data;
273 if (dwarf_tag(fn_die) == DW_TAG_subprogram &&
274 dwarf_haspc(fn_die, ad->addr)) {
275 memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die));
276 return DWARF_CB_ABORT;
281 /* Search a real subprogram including this line, */
282 static Dwarf_Die *die_find_real_subprogram(Dwarf_Die *cu_die, Dwarf_Addr addr,
285 struct __addr_die_search_param ad;
287 ad.die_mem = die_mem;
288 /* dwarf_getscopes can't find subprogram. */
289 if (!dwarf_getfuncs(cu_die, __die_search_func_cb, &ad, 0))
295 /* die_find callback for inline function search */
296 static int __die_find_inline_cb(Dwarf_Die *die_mem, void *data)
298 Dwarf_Addr *addr = data;
300 if (dwarf_tag(die_mem) == DW_TAG_inlined_subroutine &&
301 dwarf_haspc(die_mem, *addr))
302 return DIE_FIND_CB_FOUND;
304 return DIE_FIND_CB_CONTINUE;
307 /* Similar to dwarf_getfuncs, but returns inlined_subroutine if exists. */
308 static Dwarf_Die *die_find_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
311 return die_find_child(sp_die, __die_find_inline_cb, &addr, die_mem);
314 static int __die_find_variable_cb(Dwarf_Die *die_mem, void *data)
316 const char *name = data;
319 tag = dwarf_tag(die_mem);
320 if ((tag == DW_TAG_formal_parameter ||
321 tag == DW_TAG_variable) &&
322 (die_compare_name(die_mem, name) == 0))
323 return DIE_FIND_CB_FOUND;
325 return DIE_FIND_CB_CONTINUE;
328 /* Find a variable called 'name' */
329 static Dwarf_Die *die_find_variable(Dwarf_Die *sp_die, const char *name,
332 return die_find_child(sp_die, __die_find_variable_cb, (void *)name,
336 static int __die_find_member_cb(Dwarf_Die *die_mem, void *data)
338 const char *name = data;
340 if ((dwarf_tag(die_mem) == DW_TAG_member) &&
341 (die_compare_name(die_mem, name) == 0))
342 return DIE_FIND_CB_FOUND;
344 return DIE_FIND_CB_SIBLING;
347 /* Find a member called 'name' */
348 static Dwarf_Die *die_find_member(Dwarf_Die *st_die, const char *name,
351 return die_find_child(st_die, __die_find_member_cb, (void *)name,
356 * Probe finder related functions
359 /* Show a location */
360 static int convert_location(Dwarf_Op *op, struct probe_finder *pf)
366 struct kprobe_trace_arg *tvar = pf->tvar;
368 /* If this is based on frame buffer, set the offset */
369 if (op->atom == DW_OP_fbreg) {
370 if (pf->fb_ops == NULL) {
371 pr_warning("The attribute of frame base is not "
380 if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) {
381 regn = op->atom - DW_OP_breg0;
384 } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) {
385 regn = op->atom - DW_OP_reg0;
386 } else if (op->atom == DW_OP_bregx) {
390 } else if (op->atom == DW_OP_regx) {
393 pr_warning("DW_OP %x is not supported.\n", op->atom);
397 regs = get_arch_regstr(regn);
399 pr_warning("Mapping for DWARF register number %u missing on this architecture.", regn);
403 tvar->value = strdup(regs);
404 if (tvar->value == NULL)
408 tvar->ref = zalloc(sizeof(struct kprobe_trace_arg_ref));
409 if (tvar->ref == NULL)
411 tvar->ref->offset = (long)offs;
416 static int convert_variable_type(Dwarf_Die *vr_die,
417 struct kprobe_trace_arg *targ)
423 if (die_get_real_type(vr_die, &type) == NULL) {
424 pr_warning("Failed to get a type information of %s.\n",
425 dwarf_diename(vr_die));
429 ret = die_get_byte_size(&type) * 8;
431 /* Check the bitwidth */
432 if (ret > MAX_BASIC_TYPE_BITS) {
433 pr_info("%s exceeds max-bitwidth."
434 " Cut down to %d bits.\n",
435 dwarf_diename(&type), MAX_BASIC_TYPE_BITS);
436 ret = MAX_BASIC_TYPE_BITS;
439 ret = snprintf(buf, 16, "%c%d",
440 die_is_signed_type(&type) ? 's' : 'u', ret);
441 if (ret < 0 || ret >= 16) {
444 pr_warning("Failed to convert variable type: %s\n",
448 targ->type = strdup(buf);
449 if (targ->type == NULL)
455 static int convert_variable_fields(Dwarf_Die *vr_die, const char *varname,
456 struct perf_probe_arg_field *field,
457 struct kprobe_trace_arg_ref **ref_ptr,
460 struct kprobe_trace_arg_ref *ref = *ref_ptr;
465 pr_debug("converting %s in %s\n", field->name, varname);
466 if (die_get_real_type(vr_die, &type) == NULL) {
467 pr_warning("Failed to get the type of %s.\n", varname);
471 /* Check the pointer and dereference */
472 if (dwarf_tag(&type) == DW_TAG_pointer_type) {
474 pr_err("Semantic error: %s must be referred by '->'\n",
478 /* Get the type pointed by this pointer */
479 if (die_get_real_type(&type, &type) == NULL) {
480 pr_warning("Failed to get the type of %s.\n", varname);
483 /* Verify it is a data structure */
484 if (dwarf_tag(&type) != DW_TAG_structure_type) {
485 pr_warning("%s is not a data structure.\n", varname);
489 ref = zalloc(sizeof(struct kprobe_trace_arg_ref));
493 (*ref_ptr)->next = ref;
497 /* Verify it is a data structure */
498 if (dwarf_tag(&type) != DW_TAG_structure_type) {
499 pr_warning("%s is not a data structure.\n", varname);
503 pr_err("Semantic error: %s must be referred by '.'\n",
508 pr_warning("Structure on a register is not "
514 if (die_find_member(&type, field->name, die_mem) == NULL) {
515 pr_warning("%s(tyep:%s) has no member %s.\n", varname,
516 dwarf_diename(&type), field->name);
520 /* Get the offset of the field */
521 ret = die_get_data_member_location(die_mem, &offs);
523 pr_warning("Failed to get the offset of %s.\n", field->name);
526 ref->offset += (long)offs;
528 /* Converting next field */
530 return convert_variable_fields(die_mem, field->name,
531 field->next, &ref, die_mem);
536 /* Show a variables in kprobe event format */
537 static int convert_variable(Dwarf_Die *vr_die, struct probe_finder *pf)
539 Dwarf_Attribute attr;
545 if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL)
547 /* TODO: handle more than 1 exprs */
548 ret = dwarf_getlocation_addr(&attr, pf->addr, &expr, &nexpr, 1);
549 if (ret <= 0 || nexpr == 0)
552 ret = convert_location(expr, pf);
553 if (ret == 0 && pf->pvar->field) {
554 ret = convert_variable_fields(vr_die, pf->pvar->var,
555 pf->pvar->field, &pf->tvar->ref,
560 if (pf->pvar->type) {
561 pf->tvar->type = strdup(pf->pvar->type);
562 if (pf->tvar->type == NULL)
565 ret = convert_variable_type(vr_die, pf->tvar);
567 /* *expr will be cached in libdw. Don't free it. */
570 /* TODO: Support const_value */
571 pr_err("Failed to find the location of %s at this address.\n"
572 " Perhaps, it has been optimized out.\n", pf->pvar->var);
576 /* Find a variable in a subprogram die */
577 static int find_variable(Dwarf_Die *sp_die, struct probe_finder *pf)
583 /* TODO: Support arrays */
585 pf->tvar->name = strdup(pf->pvar->name);
587 ret = synthesize_perf_probe_arg(pf->pvar, buf, 32);
590 ptr = strchr(buf, ':'); /* Change type separator to _ */
593 pf->tvar->name = strdup(buf);
595 if (pf->tvar->name == NULL)
598 if (!is_c_varname(pf->pvar->var)) {
599 /* Copy raw parameters */
600 pf->tvar->value = strdup(pf->pvar->var);
601 if (pf->tvar->value == NULL)
607 pr_debug("Searching '%s' variable in context.\n",
609 /* Search child die for local variables and parameters. */
610 if (!die_find_variable(sp_die, pf->pvar->var, &vr_die)) {
611 pr_warning("Failed to find '%s' in this function.\n",
615 return convert_variable(&vr_die, pf);
618 /* Show a probe point to output buffer */
619 static int convert_probe_point(Dwarf_Die *sp_die, struct probe_finder *pf)
621 struct kprobe_trace_event *tev;
626 Dwarf_Attribute fb_attr;
629 if (pf->ntevs == pf->max_tevs) {
630 pr_warning("Too many( > %d) probe point found.\n",
634 tev = &pf->tevs[pf->ntevs++];
636 /* If no real subprogram, find a real one */
637 if (!sp_die || dwarf_tag(sp_die) != DW_TAG_subprogram) {
638 sp_die = die_find_real_subprogram(&pf->cu_die,
641 pr_warning("Failed to find probe point in any "
647 /* Copy the name of probe point */
648 name = dwarf_diename(sp_die);
650 if (dwarf_entrypc(sp_die, &eaddr) != 0) {
651 pr_warning("Failed to get entry pc of %s\n",
652 dwarf_diename(sp_die));
655 tev->point.symbol = strdup(name);
656 if (tev->point.symbol == NULL)
658 tev->point.offset = (unsigned long)(pf->addr - eaddr);
660 /* This function has no name. */
661 tev->point.offset = (unsigned long)pf->addr;
663 pr_debug("Probe point found: %s+%lu\n", tev->point.symbol,
666 /* Get the frame base attribute/ops */
667 dwarf_attr(sp_die, DW_AT_frame_base, &fb_attr);
668 ret = dwarf_getlocation_addr(&fb_attr, pf->addr, &pf->fb_ops, &nops, 1);
669 if (ret <= 0 || nops == 0) {
671 #if _ELFUTILS_PREREQ(0, 142)
672 } else if (nops == 1 && pf->fb_ops[0].atom == DW_OP_call_frame_cfa &&
675 if (dwarf_cfi_addrframe(pf->cfi, pf->addr, &frame) != 0 ||
676 dwarf_frame_cfa(frame, &pf->fb_ops, &nops) != 0) {
677 pr_warning("Failed to get CFA on 0x%jx\n",
678 (uintmax_t)pf->addr);
684 /* Find each argument */
685 tev->nargs = pf->pev->nargs;
686 tev->args = zalloc(sizeof(struct kprobe_trace_arg) * tev->nargs);
687 if (tev->args == NULL)
689 for (i = 0; i < pf->pev->nargs; i++) {
690 pf->pvar = &pf->pev->args[i];
691 pf->tvar = &tev->args[i];
692 ret = find_variable(sp_die, pf);
697 /* *pf->fb_ops will be cached in libdw. Don't free it. */
702 /* Find probe point from its line number */
703 static int find_probe_point_by_line(struct probe_finder *pf)
712 if (dwarf_getsrclines(&pf->cu_die, &lines, &nlines) != 0) {
713 pr_warning("No source lines found in this CU.\n");
717 for (i = 0; i < nlines && ret == 0; i++) {
718 line = dwarf_onesrcline(lines, i);
719 if (dwarf_lineno(line, &lineno) != 0 ||
723 /* TODO: Get fileno from line, but how? */
724 if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
727 if (dwarf_lineaddr(line, &addr) != 0) {
728 pr_warning("Failed to get the address of the line.\n");
731 pr_debug("Probe line found: line[%d]:%d addr:0x%jx\n",
732 (int)i, lineno, (uintmax_t)addr);
735 ret = convert_probe_point(NULL, pf);
736 /* Continuing, because target line might be inlined. */
741 /* Find lines which match lazy pattern */
742 static int find_lazy_match_lines(struct list_head *head,
743 const char *fname, const char *pat)
745 char *fbuf, *p1, *p2;
746 int fd, line, nlines = -1;
749 fd = open(fname, O_RDONLY);
751 pr_warning("Failed to open %s: %s\n", fname, strerror(-fd));
755 if (fstat(fd, &st) < 0) {
756 pr_warning("Failed to get the size of %s: %s\n",
757 fname, strerror(errno));
763 fbuf = malloc(st.st_size + 2);
766 if (read(fd, fbuf, st.st_size) < 0) {
767 pr_warning("Failed to read %s: %s\n", fname, strerror(errno));
771 fbuf[st.st_size] = '\n'; /* Dummy line */
772 fbuf[st.st_size + 1] = '\0';
776 while ((p2 = strchr(p1, '\n')) != NULL) {
778 if (strlazymatch(p1, pat)) {
779 line_list__add_line(head, line);
792 /* Find probe points from lazy pattern */
793 static int find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf)
803 if (list_empty(&pf->lcache)) {
804 /* Matching lazy line pattern */
805 ret = find_lazy_match_lines(&pf->lcache, pf->fname,
806 pf->pev->point.lazy_line);
808 pr_debug("No matched lines found in %s.\n", pf->fname);
814 if (dwarf_getsrclines(&pf->cu_die, &lines, &nlines) != 0) {
815 pr_warning("No source lines found in this CU.\n");
819 for (i = 0; i < nlines && ret >= 0; i++) {
820 line = dwarf_onesrcline(lines, i);
822 if (dwarf_lineno(line, &lineno) != 0 ||
823 !line_list__has_line(&pf->lcache, lineno))
826 /* TODO: Get fileno from line, but how? */
827 if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
830 if (dwarf_lineaddr(line, &addr) != 0) {
831 pr_debug("Failed to get the address of line %d.\n",
836 /* Address filtering 1: does sp_die include addr? */
837 if (!dwarf_haspc(sp_die, addr))
839 /* Address filtering 2: No child include addr? */
840 if (die_find_inlinefunc(sp_die, addr, &die_mem))
844 pr_debug("Probe line found: line[%d]:%d addr:0x%llx\n",
845 (int)i, lineno, (unsigned long long)addr);
848 ret = convert_probe_point(sp_die, pf);
849 /* Continuing, because target line might be inlined. */
851 /* TODO: deallocate lines, but how? */
855 /* Callback parameter with return value */
856 struct dwarf_callback_param {
861 static int probe_point_inline_cb(Dwarf_Die *in_die, void *data)
863 struct dwarf_callback_param *param = data;
864 struct probe_finder *pf = param->data;
865 struct perf_probe_point *pp = &pf->pev->point;
869 param->retval = find_probe_point_lazy(in_die, pf);
871 /* Get probe address */
872 if (dwarf_entrypc(in_die, &addr) != 0) {
873 pr_warning("Failed to get entry pc of %s.\n",
874 dwarf_diename(in_die));
875 param->retval = -ENOENT;
876 return DWARF_CB_ABORT;
879 pf->addr += pp->offset;
880 pr_debug("found inline addr: 0x%jx\n",
881 (uintmax_t)pf->addr);
883 param->retval = convert_probe_point(in_die, pf);
884 if (param->retval < 0)
885 return DWARF_CB_ABORT;
891 /* Search function from function name */
892 static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
894 struct dwarf_callback_param *param = data;
895 struct probe_finder *pf = param->data;
896 struct perf_probe_point *pp = &pf->pev->point;
898 /* Check tag and diename */
899 if (dwarf_tag(sp_die) != DW_TAG_subprogram ||
900 die_compare_name(sp_die, pp->function) != 0)
903 pf->fname = dwarf_decl_file(sp_die);
904 if (pp->line) { /* Function relative line */
905 dwarf_decl_line(sp_die, &pf->lno);
907 param->retval = find_probe_point_by_line(pf);
908 } else if (!dwarf_func_inline(sp_die)) {
911 param->retval = find_probe_point_lazy(sp_die, pf);
913 if (dwarf_entrypc(sp_die, &pf->addr) != 0) {
914 pr_warning("Failed to get entry pc of %s.\n",
915 dwarf_diename(sp_die));
916 param->retval = -ENOENT;
917 return DWARF_CB_ABORT;
919 pf->addr += pp->offset;
920 /* TODO: Check the address in this function */
921 param->retval = convert_probe_point(sp_die, pf);
924 struct dwarf_callback_param _param = {.data = (void *)pf,
926 /* Inlined function: search instances */
927 dwarf_func_inline_instances(sp_die, probe_point_inline_cb,
929 param->retval = _param.retval;
932 return DWARF_CB_ABORT; /* Exit; no same symbol in this CU. */
935 static int find_probe_point_by_func(struct probe_finder *pf)
937 struct dwarf_callback_param _param = {.data = (void *)pf,
939 dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, &_param, 0);
940 return _param.retval;
943 /* Find kprobe_trace_events specified by perf_probe_event from debuginfo */
944 int find_kprobe_trace_events(int fd, struct perf_probe_event *pev,
945 struct kprobe_trace_event **tevs, int max_tevs)
947 struct probe_finder pf = {.pev = pev, .max_tevs = max_tevs};
948 struct perf_probe_point *pp = &pev->point;
955 pf.tevs = zalloc(sizeof(struct kprobe_trace_event) * max_tevs);
961 dbg = dwarf_begin(fd, DWARF_C_READ);
963 pr_warning("No dwarf info found in the vmlinux - "
964 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
970 #if _ELFUTILS_PREREQ(0, 142)
971 /* Get the call frame information from this dwarf */
972 pf.cfi = dwarf_getcfi(dbg);
976 line_list__init(&pf.lcache);
977 /* Loop on CUs (Compilation Unit) */
978 while (!dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL) &&
980 /* Get the DIE(Debugging Information Entry) of this CU */
981 diep = dwarf_offdie(dbg, off + cuhl, &pf.cu_die);
985 /* Check if target file is included. */
987 pf.fname = cu_find_realpath(&pf.cu_die, pp->file);
991 if (!pp->file || pf.fname) {
993 ret = find_probe_point_by_func(&pf);
994 else if (pp->lazy_line)
995 ret = find_probe_point_lazy(NULL, &pf);
998 ret = find_probe_point_by_line(&pf);
1003 line_list__free(&pf.lcache);
1006 return (ret < 0) ? ret : pf.ntevs;
1009 /* Reverse search */
1010 int find_perf_probe_point(int fd, unsigned long addr,
1011 struct perf_probe_point *ppt)
1013 Dwarf_Die cudie, spdie, indie;
1016 Dwarf_Addr laddr, eaddr;
1018 int lineno, ret = 0;
1021 dbg = dwarf_begin(fd, DWARF_C_READ);
1026 if (!dwarf_addrdie(dbg, (Dwarf_Addr)addr, &cudie)) {
1031 /* Find a corresponding line */
1032 line = dwarf_getsrc_die(&cudie, (Dwarf_Addr)addr);
1034 if (dwarf_lineaddr(line, &laddr) == 0 &&
1035 (Dwarf_Addr)addr == laddr &&
1036 dwarf_lineno(line, &lineno) == 0) {
1037 tmp = dwarf_linesrc(line, NULL, NULL);
1040 ppt->file = strdup(tmp);
1041 if (ppt->file == NULL) {
1050 /* Find a corresponding function */
1051 if (die_find_real_subprogram(&cudie, (Dwarf_Addr)addr, &spdie)) {
1052 tmp = dwarf_diename(&spdie);
1053 if (!tmp || dwarf_entrypc(&spdie, &eaddr) != 0)
1057 if (die_find_inlinefunc(&spdie, (Dwarf_Addr)addr,
1059 /* addr in an inline function */
1060 tmp = dwarf_diename(&indie);
1063 ret = dwarf_decl_line(&indie, &lineno);
1065 if (eaddr == addr) { /* Function entry */
1069 ret = dwarf_decl_line(&spdie, &lineno);
1072 /* Make a relative line number */
1073 ppt->line -= lineno;
1077 /* We don't have a line number, let's use offset */
1078 ppt->offset = addr - (unsigned long)eaddr;
1080 ppt->function = strdup(tmp);
1081 if (ppt->function == NULL) {
1091 ret = found ? 1 : 0;
1095 /* Add a line and store the src path */
1096 static int line_range_add_line(const char *src, unsigned int lineno,
1097 struct line_range *lr)
1099 /* Copy real path */
1101 lr->path = strdup(src);
1102 if (lr->path == NULL)
1105 return line_list__add_line(&lr->line_list, lineno);
1108 /* Search function declaration lines */
1109 static int line_range_funcdecl_cb(Dwarf_Die *sp_die, void *data)
1111 struct dwarf_callback_param *param = data;
1112 struct line_finder *lf = param->data;
1116 src = dwarf_decl_file(sp_die);
1117 if (src && strtailcmp(src, lf->fname) != 0)
1120 if (dwarf_decl_line(sp_die, &lineno) != 0 ||
1121 (lf->lno_s > lineno || lf->lno_e < lineno))
1124 param->retval = line_range_add_line(src, lineno, lf->lr);
1125 if (param->retval < 0)
1126 return DWARF_CB_ABORT;
1130 static int find_line_range_func_decl_lines(struct line_finder *lf)
1132 struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
1133 dwarf_getfuncs(&lf->cu_die, line_range_funcdecl_cb, ¶m, 0);
1134 return param.retval;
1137 /* Find line range from its line number */
1138 static int find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
1144 int lineno, ret = 0;
1148 line_list__init(&lf->lr->line_list);
1149 if (dwarf_getsrclines(&lf->cu_die, &lines, &nlines) != 0) {
1150 pr_warning("No source lines found in this CU.\n");
1154 /* Search probable lines on lines list */
1155 for (i = 0; i < nlines; i++) {
1156 line = dwarf_onesrcline(lines, i);
1157 if (dwarf_lineno(line, &lineno) != 0 ||
1158 (lf->lno_s > lineno || lf->lno_e < lineno))
1162 /* Address filtering 1: does sp_die include addr? */
1163 if (dwarf_lineaddr(line, &addr) != 0 ||
1164 !dwarf_haspc(sp_die, addr))
1167 /* Address filtering 2: No child include addr? */
1168 if (die_find_inlinefunc(sp_die, addr, &die_mem))
1172 /* TODO: Get fileno from line, but how? */
1173 src = dwarf_linesrc(line, NULL, NULL);
1174 if (strtailcmp(src, lf->fname) != 0)
1177 ret = line_range_add_line(src, lineno, lf->lr);
1183 * Dwarf lines doesn't include function declarations. We have to
1184 * check functions list or given function.
1187 src = dwarf_decl_file(sp_die);
1188 if (src && dwarf_decl_line(sp_die, &lineno) == 0 &&
1189 (lf->lno_s <= lineno && lf->lno_e >= lineno))
1190 ret = line_range_add_line(src, lineno, lf->lr);
1192 ret = find_line_range_func_decl_lines(lf);
1196 if (!list_empty(&lf->lr->line_list))
1197 ret = lf->found = 1;
1199 ret = 0; /* Lines are not found */
1202 lf->lr->path = NULL;
1207 static int line_range_inline_cb(Dwarf_Die *in_die, void *data)
1209 struct dwarf_callback_param *param = data;
1211 param->retval = find_line_range_by_line(in_die, param->data);
1212 return DWARF_CB_ABORT; /* No need to find other instances */
1215 /* Search function from function name */
1216 static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
1218 struct dwarf_callback_param *param = data;
1219 struct line_finder *lf = param->data;
1220 struct line_range *lr = lf->lr;
1222 if (dwarf_tag(sp_die) == DW_TAG_subprogram &&
1223 die_compare_name(sp_die, lr->function) == 0) {
1224 lf->fname = dwarf_decl_file(sp_die);
1225 dwarf_decl_line(sp_die, &lr->offset);
1226 pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
1227 lf->lno_s = lr->offset + lr->start;
1228 if (lf->lno_s < 0) /* Overflow */
1229 lf->lno_s = INT_MAX;
1230 lf->lno_e = lr->offset + lr->end;
1231 if (lf->lno_e < 0) /* Overflow */
1232 lf->lno_e = INT_MAX;
1233 pr_debug("New line range: %d to %d\n", lf->lno_s, lf->lno_e);
1234 lr->start = lf->lno_s;
1235 lr->end = lf->lno_e;
1236 if (dwarf_func_inline(sp_die)) {
1237 struct dwarf_callback_param _param;
1238 _param.data = (void *)lf;
1240 dwarf_func_inline_instances(sp_die,
1241 line_range_inline_cb,
1243 param->retval = _param.retval;
1245 param->retval = find_line_range_by_line(sp_die, lf);
1246 return DWARF_CB_ABORT;
1251 static int find_line_range_by_func(struct line_finder *lf)
1253 struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
1254 dwarf_getfuncs(&lf->cu_die, line_range_search_cb, ¶m, 0);
1255 return param.retval;
1258 int find_line_range(int fd, struct line_range *lr)
1260 struct line_finder lf = {.lr = lr, .found = 0};
1262 Dwarf_Off off = 0, noff;
1267 dbg = dwarf_begin(fd, DWARF_C_READ);
1269 pr_warning("No dwarf info found in the vmlinux - "
1270 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
1274 /* Loop on CUs (Compilation Unit) */
1275 while (!lf.found && ret >= 0) {
1276 if (dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL) != 0)
1279 /* Get the DIE(Debugging Information Entry) of this CU */
1280 diep = dwarf_offdie(dbg, off + cuhl, &lf.cu_die);
1284 /* Check if target file is included. */
1286 lf.fname = cu_find_realpath(&lf.cu_die, lr->file);
1290 if (!lr->file || lf.fname) {
1292 ret = find_line_range_by_func(&lf);
1294 lf.lno_s = lr->start;
1296 ret = find_line_range_by_line(NULL, &lf);
1301 pr_debug("path: %lx\n", (unsigned long)lr->path);
1304 return (ret < 0) ? ret : lf.found;