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>
39 #include "probe-finder.h"
43 * Generic dwarf analysis helpers
46 #define X86_32_MAX_REGS 8
47 const char *x86_32_regs_table[X86_32_MAX_REGS] = {
52 "$stack", /* Stack address instead of %sp */
58 #define X86_64_MAX_REGS 16
59 const char *x86_64_regs_table[X86_64_MAX_REGS] = {
78 /* TODO: switching by dwarf address size */
80 #define ARCH_MAX_REGS X86_64_MAX_REGS
81 #define arch_regs_table x86_64_regs_table
83 #define ARCH_MAX_REGS X86_32_MAX_REGS
84 #define arch_regs_table x86_32_regs_table
87 /* Kprobe tracer basic type is up to u64 */
88 #define MAX_BASIC_TYPE_BITS 64
90 /* Return architecture dependent register string (for kprobe-tracer) */
91 static const char *get_arch_regstr(unsigned int n)
93 return (n <= ARCH_MAX_REGS) ? arch_regs_table[n] : NULL;
97 * Compare the tail of two strings.
98 * Return 0 if whole of either string is same as another's tail part.
100 static int strtailcmp(const char *s1, const char *s2)
104 while (--i1 >= 0 && --i2 >= 0) {
105 if (s1[i1] != s2[i2])
106 return s1[i1] - s2[i2];
111 /* Line number list operations */
113 /* Add a line to line number list */
114 static int line_list__add_line(struct list_head *head, unsigned int line)
116 struct line_node *ln;
119 /* Reverse search, because new line will be the last one */
120 list_for_each_entry_reverse(ln, head, list) {
121 if (ln->line < line) {
124 } else if (ln->line == line) /* Already exist */
127 /* List is empty, or the smallest entry */
130 pr_debug("line list: add a line %u\n", line);
131 ln = zalloc(sizeof(struct line_node));
135 INIT_LIST_HEAD(&ln->list);
136 list_add(&ln->list, p);
140 /* Check if the line in line number list */
141 static int line_list__has_line(struct list_head *head, unsigned int line)
143 struct line_node *ln;
145 /* Reverse search, because new line will be the last one */
146 list_for_each_entry(ln, head, list)
147 if (ln->line == line)
153 /* Init line number list */
154 static void line_list__init(struct list_head *head)
156 INIT_LIST_HEAD(head);
159 /* Free line number list */
160 static void line_list__free(struct list_head *head)
162 struct line_node *ln;
163 while (!list_empty(head)) {
164 ln = list_first_entry(head, struct line_node, list);
172 /* Find the realpath of the target file. */
173 static const char *cu_find_realpath(Dwarf_Die *cu_die, const char *fname)
177 const char *src = NULL;
183 ret = dwarf_getsrcfiles(cu_die, &files, &nfiles);
187 for (i = 0; i < nfiles; i++) {
188 src = dwarf_filesrc(files, i, NULL, NULL);
189 if (strtailcmp(src, fname) == 0)
197 /* Compare diename and tname */
198 static bool die_compare_name(Dwarf_Die *dw_die, const char *tname)
201 name = dwarf_diename(dw_die);
202 return name ? strcmp(tname, name) : -1;
205 /* Get type die, but skip qualifiers and typedef */
206 static Dwarf_Die *die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
208 Dwarf_Attribute attr;
212 if (dwarf_attr(vr_die, DW_AT_type, &attr) == NULL ||
213 dwarf_formref_die(&attr, die_mem) == NULL)
216 tag = dwarf_tag(die_mem);
218 } while (tag == DW_TAG_const_type ||
219 tag == DW_TAG_restrict_type ||
220 tag == DW_TAG_volatile_type ||
221 tag == DW_TAG_shared_type ||
222 tag == DW_TAG_typedef);
227 static bool die_is_signed_type(Dwarf_Die *tp_die)
229 Dwarf_Attribute attr;
232 if (dwarf_attr(tp_die, DW_AT_encoding, &attr) == NULL ||
233 dwarf_formudata(&attr, &ret) != 0)
236 return (ret == DW_ATE_signed_char || ret == DW_ATE_signed ||
237 ret == DW_ATE_signed_fixed);
240 static int die_get_byte_size(Dwarf_Die *tp_die)
242 Dwarf_Attribute attr;
245 if (dwarf_attr(tp_die, DW_AT_byte_size, &attr) == NULL ||
246 dwarf_formudata(&attr, &ret) != 0)
252 /* Return values for die_find callbacks */
254 DIE_FIND_CB_FOUND = 0, /* End of Search */
255 DIE_FIND_CB_CHILD = 1, /* Search only children */
256 DIE_FIND_CB_SIBLING = 2, /* Search only siblings */
257 DIE_FIND_CB_CONTINUE = 3, /* Search children and siblings */
260 /* Search a child die */
261 static Dwarf_Die *die_find_child(Dwarf_Die *rt_die,
262 int (*callback)(Dwarf_Die *, void *),
263 void *data, Dwarf_Die *die_mem)
268 ret = dwarf_child(rt_die, die_mem);
273 ret = callback(die_mem, data);
274 if (ret == DIE_FIND_CB_FOUND)
277 if ((ret & DIE_FIND_CB_CHILD) &&
278 die_find_child(die_mem, callback, data, &child_die)) {
279 memcpy(die_mem, &child_die, sizeof(Dwarf_Die));
282 } while ((ret & DIE_FIND_CB_SIBLING) &&
283 dwarf_siblingof(die_mem, die_mem) == 0);
288 struct __addr_die_search_param {
293 static int __die_search_func_cb(Dwarf_Die *fn_die, void *data)
295 struct __addr_die_search_param *ad = data;
297 if (dwarf_tag(fn_die) == DW_TAG_subprogram &&
298 dwarf_haspc(fn_die, ad->addr)) {
299 memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die));
300 return DWARF_CB_ABORT;
305 /* Search a real subprogram including this line, */
306 static Dwarf_Die *die_find_real_subprogram(Dwarf_Die *cu_die, Dwarf_Addr addr,
309 struct __addr_die_search_param ad;
311 ad.die_mem = die_mem;
312 /* dwarf_getscopes can't find subprogram. */
313 if (!dwarf_getfuncs(cu_die, __die_search_func_cb, &ad, 0))
319 /* die_find callback for inline function search */
320 static int __die_find_inline_cb(Dwarf_Die *die_mem, void *data)
322 Dwarf_Addr *addr = data;
324 if (dwarf_tag(die_mem) == DW_TAG_inlined_subroutine &&
325 dwarf_haspc(die_mem, *addr))
326 return DIE_FIND_CB_FOUND;
328 return DIE_FIND_CB_CONTINUE;
331 /* Similar to dwarf_getfuncs, but returns inlined_subroutine if exists. */
332 static Dwarf_Die *die_find_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
335 return die_find_child(sp_die, __die_find_inline_cb, &addr, die_mem);
338 static int __die_find_variable_cb(Dwarf_Die *die_mem, void *data)
340 const char *name = data;
343 tag = dwarf_tag(die_mem);
344 if ((tag == DW_TAG_formal_parameter ||
345 tag == DW_TAG_variable) &&
346 (die_compare_name(die_mem, name) == 0))
347 return DIE_FIND_CB_FOUND;
349 return DIE_FIND_CB_CONTINUE;
352 /* Find a variable called 'name' */
353 static Dwarf_Die *die_find_variable(Dwarf_Die *sp_die, const char *name,
356 return die_find_child(sp_die, __die_find_variable_cb, (void *)name,
360 static int __die_find_member_cb(Dwarf_Die *die_mem, void *data)
362 const char *name = data;
364 if ((dwarf_tag(die_mem) == DW_TAG_member) &&
365 (die_compare_name(die_mem, name) == 0))
366 return DIE_FIND_CB_FOUND;
368 return DIE_FIND_CB_SIBLING;
371 /* Find a member called 'name' */
372 static Dwarf_Die *die_find_member(Dwarf_Die *st_die, const char *name,
375 return die_find_child(st_die, __die_find_member_cb, (void *)name,
380 * Probe finder related functions
383 /* Show a location */
384 static int convert_location(Dwarf_Op *op, struct probe_finder *pf)
390 struct kprobe_trace_arg *tvar = pf->tvar;
392 /* If this is based on frame buffer, set the offset */
393 if (op->atom == DW_OP_fbreg) {
394 if (pf->fb_ops == NULL) {
395 pr_warning("The attribute of frame base is not "
404 if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) {
405 regn = op->atom - DW_OP_breg0;
408 } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) {
409 regn = op->atom - DW_OP_reg0;
410 } else if (op->atom == DW_OP_bregx) {
414 } else if (op->atom == DW_OP_regx) {
417 pr_warning("DW_OP %x is not supported.\n", op->atom);
421 regs = get_arch_regstr(regn);
423 pr_warning("%u exceeds max register number.\n", regn);
427 tvar->value = strdup(regs);
428 if (tvar->value == NULL)
432 tvar->ref = zalloc(sizeof(struct kprobe_trace_arg_ref));
433 if (tvar->ref == NULL)
435 tvar->ref->offset = (long)offs;
440 static int convert_variable_type(Dwarf_Die *vr_die,
441 struct kprobe_trace_arg *targ)
447 if (die_get_real_type(vr_die, &type) == NULL) {
448 pr_warning("Failed to get a type information of %s.\n",
449 dwarf_diename(vr_die));
453 ret = die_get_byte_size(&type) * 8;
455 /* Check the bitwidth */
456 if (ret > MAX_BASIC_TYPE_BITS) {
457 pr_info("%s exceeds max-bitwidth."
458 " Cut down to %d bits.\n",
459 dwarf_diename(&type), MAX_BASIC_TYPE_BITS);
460 ret = MAX_BASIC_TYPE_BITS;
463 ret = snprintf(buf, 16, "%c%d",
464 die_is_signed_type(&type) ? 's' : 'u', ret);
465 if (ret < 0 || ret >= 16) {
468 pr_warning("Failed to convert variable type: %s\n",
472 targ->type = strdup(buf);
473 if (targ->type == NULL)
479 static int convert_variable_fields(Dwarf_Die *vr_die, const char *varname,
480 struct perf_probe_arg_field *field,
481 struct kprobe_trace_arg_ref **ref_ptr,
484 struct kprobe_trace_arg_ref *ref = *ref_ptr;
485 Dwarf_Attribute attr;
489 pr_debug("converting %s in %s\n", field->name, varname);
490 if (die_get_real_type(vr_die, &type) == NULL) {
491 pr_warning("Failed to get the type of %s.\n", varname);
495 /* Check the pointer and dereference */
496 if (dwarf_tag(&type) == DW_TAG_pointer_type) {
498 pr_err("Semantic error: %s must be referred by '->'\n",
502 /* Get the type pointed by this pointer */
503 if (die_get_real_type(&type, &type) == NULL) {
504 pr_warning("Failed to get the type of %s.\n", varname);
507 /* Verify it is a data structure */
508 if (dwarf_tag(&type) != DW_TAG_structure_type) {
509 pr_warning("%s is not a data structure.\n", varname);
513 ref = zalloc(sizeof(struct kprobe_trace_arg_ref));
517 (*ref_ptr)->next = ref;
521 /* Verify it is a data structure */
522 if (dwarf_tag(&type) != DW_TAG_structure_type) {
523 pr_warning("%s is not a data structure.\n", varname);
527 pr_err("Semantic error: %s must be referred by '.'\n",
532 pr_warning("Structure on a register is not "
538 if (die_find_member(&type, field->name, die_mem) == NULL) {
539 pr_warning("%s(tyep:%s) has no member %s.\n", varname,
540 dwarf_diename(&type), field->name);
544 /* Get the offset of the field */
545 if (dwarf_attr(die_mem, DW_AT_data_member_location, &attr) == NULL ||
546 dwarf_formudata(&attr, &offs) != 0) {
547 pr_warning("Failed to get the offset of %s.\n", field->name);
550 ref->offset += (long)offs;
552 /* Converting next field */
554 return convert_variable_fields(die_mem, field->name,
555 field->next, &ref, die_mem);
560 /* Show a variables in kprobe event format */
561 static int convert_variable(Dwarf_Die *vr_die, struct probe_finder *pf)
563 Dwarf_Attribute attr;
569 if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL)
571 /* TODO: handle more than 1 exprs */
572 ret = dwarf_getlocation_addr(&attr, pf->addr, &expr, &nexpr, 1);
573 if (ret <= 0 || nexpr == 0)
576 ret = convert_location(expr, pf);
577 if (ret == 0 && pf->pvar->field) {
578 ret = convert_variable_fields(vr_die, pf->pvar->var,
579 pf->pvar->field, &pf->tvar->ref,
584 if (pf->pvar->type) {
585 pf->tvar->type = strdup(pf->pvar->type);
586 if (pf->tvar->type == NULL)
589 ret = convert_variable_type(vr_die, pf->tvar);
591 /* *expr will be cached in libdw. Don't free it. */
594 /* TODO: Support const_value */
595 pr_err("Failed to find the location of %s at this address.\n"
596 " Perhaps, it has been optimized out.\n", pf->pvar->var);
600 /* Find a variable in a subprogram die */
601 static int find_variable(Dwarf_Die *sp_die, struct probe_finder *pf)
607 /* TODO: Support arrays */
609 pf->tvar->name = strdup(pf->pvar->name);
611 ret = synthesize_perf_probe_arg(pf->pvar, buf, 32);
614 ptr = strchr(buf, ':'); /* Change type separator to _ */
617 pf->tvar->name = strdup(buf);
619 if (pf->tvar->name == NULL)
622 if (!is_c_varname(pf->pvar->var)) {
623 /* Copy raw parameters */
624 pf->tvar->value = strdup(pf->pvar->var);
625 if (pf->tvar->value == NULL)
631 pr_debug("Searching '%s' variable in context.\n",
633 /* Search child die for local variables and parameters. */
634 if (!die_find_variable(sp_die, pf->pvar->var, &vr_die)) {
635 pr_warning("Failed to find '%s' in this function.\n",
639 return convert_variable(&vr_die, pf);
642 /* Show a probe point to output buffer */
643 static int convert_probe_point(Dwarf_Die *sp_die, struct probe_finder *pf)
645 struct kprobe_trace_event *tev;
650 Dwarf_Attribute fb_attr;
653 if (pf->ntevs == MAX_PROBES) {
654 pr_warning("Too many( > %d) probe point found.\n", MAX_PROBES);
657 tev = &pf->tevs[pf->ntevs++];
659 /* If no real subprogram, find a real one */
660 if (!sp_die || dwarf_tag(sp_die) != DW_TAG_subprogram) {
661 sp_die = die_find_real_subprogram(&pf->cu_die,
664 pr_warning("Failed to find probe point in any "
670 /* Copy the name of probe point */
671 name = dwarf_diename(sp_die);
673 if (dwarf_entrypc(sp_die, &eaddr) != 0) {
674 pr_warning("Failed to get entry pc of %s\n",
675 dwarf_diename(sp_die));
678 tev->point.symbol = strdup(name);
679 if (tev->point.symbol == NULL)
681 tev->point.offset = (unsigned long)(pf->addr - eaddr);
683 /* This function has no name. */
684 tev->point.offset = (unsigned long)pf->addr;
686 pr_debug("Probe point found: %s+%lu\n", tev->point.symbol,
689 /* Get the frame base attribute/ops */
690 dwarf_attr(sp_die, DW_AT_frame_base, &fb_attr);
691 ret = dwarf_getlocation_addr(&fb_attr, pf->addr, &pf->fb_ops, &nops, 1);
692 if (ret <= 0 || nops == 0) {
694 } else if (nops == 1 && pf->fb_ops[0].atom == DW_OP_call_frame_cfa &&
697 if (dwarf_cfi_addrframe(pf->cfi, pf->addr, &frame) != 0 ||
698 dwarf_frame_cfa(frame, &pf->fb_ops, &nops) != 0) {
699 pr_warning("Failed to get CFA on 0x%jx\n",
700 (uintmax_t)pf->addr);
705 /* Find each argument */
706 tev->nargs = pf->pev->nargs;
707 tev->args = zalloc(sizeof(struct kprobe_trace_arg) * tev->nargs);
708 if (tev->args == NULL)
710 for (i = 0; i < pf->pev->nargs; i++) {
711 pf->pvar = &pf->pev->args[i];
712 pf->tvar = &tev->args[i];
713 ret = find_variable(sp_die, pf);
718 /* *pf->fb_ops will be cached in libdw. Don't free it. */
723 /* Find probe point from its line number */
724 static int find_probe_point_by_line(struct probe_finder *pf)
733 if (dwarf_getsrclines(&pf->cu_die, &lines, &nlines) != 0) {
734 pr_warning("No source lines found in this CU.\n");
738 for (i = 0; i < nlines && ret == 0; i++) {
739 line = dwarf_onesrcline(lines, i);
740 if (dwarf_lineno(line, &lineno) != 0 ||
744 /* TODO: Get fileno from line, but how? */
745 if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
748 if (dwarf_lineaddr(line, &addr) != 0) {
749 pr_warning("Failed to get the address of the line.\n");
752 pr_debug("Probe line found: line[%d]:%d addr:0x%jx\n",
753 (int)i, lineno, (uintmax_t)addr);
756 ret = convert_probe_point(NULL, pf);
757 /* Continuing, because target line might be inlined. */
762 /* Find lines which match lazy pattern */
763 static int find_lazy_match_lines(struct list_head *head,
764 const char *fname, const char *pat)
766 char *fbuf, *p1, *p2;
767 int fd, ret, line, nlines = 0;
770 fd = open(fname, O_RDONLY);
772 pr_warning("Failed to open %s: %s\n", fname, strerror(-fd));
776 ret = fstat(fd, &st);
778 pr_warning("Failed to get the size of %s: %s\n",
779 fname, strerror(errno));
782 fbuf = xmalloc(st.st_size + 2);
783 ret = read(fd, fbuf, st.st_size);
785 pr_warning("Failed to read %s: %s\n", fname, strerror(errno));
789 fbuf[st.st_size] = '\n'; /* Dummy line */
790 fbuf[st.st_size + 1] = '\0';
793 while ((p2 = strchr(p1, '\n')) != NULL) {
795 if (strlazymatch(p1, pat)) {
796 line_list__add_line(head, line);
806 /* Find probe points from lazy pattern */
807 static int find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf)
817 if (list_empty(&pf->lcache)) {
818 /* Matching lazy line pattern */
819 ret = find_lazy_match_lines(&pf->lcache, pf->fname,
820 pf->pev->point.lazy_line);
822 pr_debug("No matched lines found in %s.\n", pf->fname);
828 if (dwarf_getsrclines(&pf->cu_die, &lines, &nlines) != 0) {
829 pr_warning("No source lines found in this CU.\n");
833 for (i = 0; i < nlines && ret >= 0; i++) {
834 line = dwarf_onesrcline(lines, i);
836 if (dwarf_lineno(line, &lineno) != 0 ||
837 !line_list__has_line(&pf->lcache, lineno))
840 /* TODO: Get fileno from line, but how? */
841 if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
844 if (dwarf_lineaddr(line, &addr) != 0) {
845 pr_debug("Failed to get the address of line %d.\n",
850 /* Address filtering 1: does sp_die include addr? */
851 if (!dwarf_haspc(sp_die, addr))
853 /* Address filtering 2: No child include addr? */
854 if (die_find_inlinefunc(sp_die, addr, &die_mem))
858 pr_debug("Probe line found: line[%d]:%d addr:0x%llx\n",
859 (int)i, lineno, (unsigned long long)addr);
862 ret = convert_probe_point(sp_die, pf);
863 /* Continuing, because target line might be inlined. */
865 /* TODO: deallocate lines, but how? */
869 /* Callback parameter with return value */
870 struct dwarf_callback_param {
875 static int probe_point_inline_cb(Dwarf_Die *in_die, void *data)
877 struct dwarf_callback_param *param = data;
878 struct probe_finder *pf = param->data;
879 struct perf_probe_point *pp = &pf->pev->point;
883 param->retval = find_probe_point_lazy(in_die, pf);
885 /* Get probe address */
886 if (dwarf_entrypc(in_die, &addr) != 0) {
887 pr_warning("Failed to get entry pc of %s.\n",
888 dwarf_diename(in_die));
889 param->retval = -ENOENT;
890 return DWARF_CB_ABORT;
893 pf->addr += pp->offset;
894 pr_debug("found inline addr: 0x%jx\n",
895 (uintmax_t)pf->addr);
897 param->retval = convert_probe_point(in_die, pf);
903 /* Search function from function name */
904 static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
906 struct dwarf_callback_param *param = data;
907 struct probe_finder *pf = param->data;
908 struct perf_probe_point *pp = &pf->pev->point;
910 /* Check tag and diename */
911 if (dwarf_tag(sp_die) != DW_TAG_subprogram ||
912 die_compare_name(sp_die, pp->function) != 0)
915 pf->fname = dwarf_decl_file(sp_die);
916 if (pp->line) { /* Function relative line */
917 dwarf_decl_line(sp_die, &pf->lno);
919 param->retval = find_probe_point_by_line(pf);
920 } else if (!dwarf_func_inline(sp_die)) {
923 param->retval = find_probe_point_lazy(sp_die, pf);
925 if (dwarf_entrypc(sp_die, &pf->addr) != 0) {
926 pr_warning("Failed to get entry pc of %s.\n",
927 dwarf_diename(sp_die));
928 param->retval = -ENOENT;
929 return DWARF_CB_ABORT;
931 pf->addr += pp->offset;
932 /* TODO: Check the address in this function */
933 param->retval = convert_probe_point(sp_die, pf);
936 struct dwarf_callback_param _param = {.data = (void *)pf,
938 /* Inlined function: search instances */
939 dwarf_func_inline_instances(sp_die, probe_point_inline_cb,
941 param->retval = _param.retval;
944 return DWARF_CB_ABORT; /* Exit; no same symbol in this CU. */
947 static int find_probe_point_by_func(struct probe_finder *pf)
949 struct dwarf_callback_param _param = {.data = (void *)pf,
951 dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, &_param, 0);
952 return _param.retval;
955 /* Find kprobe_trace_events specified by perf_probe_event from debuginfo */
956 int find_kprobe_trace_events(int fd, struct perf_probe_event *pev,
957 struct kprobe_trace_event **tevs)
959 struct probe_finder pf = {.pev = pev};
960 struct perf_probe_point *pp = &pev->point;
967 pf.tevs = zalloc(sizeof(struct kprobe_trace_event) * MAX_PROBES);
973 dbg = dwarf_begin(fd, DWARF_C_READ);
975 pr_warning("No dwarf info found in the vmlinux - "
976 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
980 /* Get the call frame information from this dwarf */
981 pf.cfi = dwarf_getcfi(dbg);
984 line_list__init(&pf.lcache);
985 /* Loop on CUs (Compilation Unit) */
986 while (!dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL) &&
988 /* Get the DIE(Debugging Information Entry) of this CU */
989 diep = dwarf_offdie(dbg, off + cuhl, &pf.cu_die);
993 /* Check if target file is included. */
995 pf.fname = cu_find_realpath(&pf.cu_die, pp->file);
999 if (!pp->file || pf.fname) {
1001 ret = find_probe_point_by_func(&pf);
1002 else if (pp->lazy_line)
1003 ret = find_probe_point_lazy(NULL, &pf);
1006 ret = find_probe_point_by_line(&pf);
1011 line_list__free(&pf.lcache);
1014 return (ret < 0) ? ret : pf.ntevs;
1017 /* Reverse search */
1018 int find_perf_probe_point(int fd, unsigned long addr,
1019 struct perf_probe_point *ppt)
1021 Dwarf_Die cudie, spdie, indie;
1024 Dwarf_Addr laddr, eaddr;
1026 int lineno, ret = 0;
1029 dbg = dwarf_begin(fd, DWARF_C_READ);
1034 if (!dwarf_addrdie(dbg, (Dwarf_Addr)addr, &cudie)) {
1039 /* Find a corresponding line */
1040 line = dwarf_getsrc_die(&cudie, (Dwarf_Addr)addr);
1042 if (dwarf_lineaddr(line, &laddr) == 0 &&
1043 (Dwarf_Addr)addr == laddr &&
1044 dwarf_lineno(line, &lineno) == 0) {
1045 tmp = dwarf_linesrc(line, NULL, NULL);
1048 ppt->file = strdup(tmp);
1049 if (ppt->file == NULL) {
1058 /* Find a corresponding function */
1059 if (die_find_real_subprogram(&cudie, (Dwarf_Addr)addr, &spdie)) {
1060 tmp = dwarf_diename(&spdie);
1061 if (!tmp || dwarf_entrypc(&spdie, &eaddr) != 0)
1065 if (die_find_inlinefunc(&spdie, (Dwarf_Addr)addr,
1067 /* addr in an inline function */
1068 tmp = dwarf_diename(&indie);
1071 ret = dwarf_decl_line(&indie, &lineno);
1073 if (eaddr == addr) { /* Function entry */
1077 ret = dwarf_decl_line(&spdie, &lineno);
1080 /* Make a relative line number */
1081 ppt->line -= lineno;
1085 /* We don't have a line number, let's use offset */
1086 ppt->offset = addr - (unsigned long)eaddr;
1088 ppt->function = strdup(tmp);
1089 if (ppt->function == NULL) {
1099 ret = found ? 1 : 0;
1104 /* Find line range from its line number */
1105 static int find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
1115 line_list__init(&lf->lr->line_list);
1116 if (dwarf_getsrclines(&lf->cu_die, &lines, &nlines) != 0) {
1117 pr_warning("No source lines found in this CU.\n");
1121 for (i = 0; i < nlines; i++) {
1122 line = dwarf_onesrcline(lines, i);
1123 if (dwarf_lineno(line, &lineno) != 0 ||
1124 (lf->lno_s > lineno || lf->lno_e < lineno))
1128 /* Address filtering 1: does sp_die include addr? */
1129 if (dwarf_lineaddr(line, &addr) != 0 ||
1130 !dwarf_haspc(sp_die, addr))
1133 /* Address filtering 2: No child include addr? */
1134 if (die_find_inlinefunc(sp_die, addr, &die_mem))
1138 /* TODO: Get fileno from line, but how? */
1139 src = dwarf_linesrc(line, NULL, NULL);
1140 if (strtailcmp(src, lf->fname) != 0)
1143 /* Copy real path */
1144 if (!lf->lr->path) {
1145 lf->lr->path = strdup(src);
1146 if (lf->lr->path == NULL)
1149 line_list__add_line(&lf->lr->line_list, (unsigned int)lineno);
1152 if (!list_empty(&lf->lr->line_list))
1156 lf->lr->path = NULL;
1161 static int line_range_inline_cb(Dwarf_Die *in_die, void *data)
1163 struct dwarf_callback_param *param = data;
1165 param->retval = find_line_range_by_line(in_die, param->data);
1166 return DWARF_CB_ABORT; /* No need to find other instances */
1169 /* Search function from function name */
1170 static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
1172 struct dwarf_callback_param *param = data;
1173 struct line_finder *lf = param->data;
1174 struct line_range *lr = lf->lr;
1176 if (dwarf_tag(sp_die) == DW_TAG_subprogram &&
1177 die_compare_name(sp_die, lr->function) == 0) {
1178 lf->fname = dwarf_decl_file(sp_die);
1179 dwarf_decl_line(sp_die, &lr->offset);
1180 pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
1181 lf->lno_s = lr->offset + lr->start;
1183 lf->lno_e = INT_MAX;
1185 lf->lno_e = lr->offset + lr->end;
1186 lr->start = lf->lno_s;
1187 lr->end = lf->lno_e;
1188 if (dwarf_func_inline(sp_die)) {
1189 struct dwarf_callback_param _param;
1190 _param.data = (void *)lf;
1192 dwarf_func_inline_instances(sp_die,
1193 line_range_inline_cb,
1195 param->retval = _param.retval;
1197 param->retval = find_line_range_by_line(sp_die, lf);
1198 return DWARF_CB_ABORT;
1203 static int find_line_range_by_func(struct line_finder *lf)
1205 struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
1206 dwarf_getfuncs(&lf->cu_die, line_range_search_cb, ¶m, 0);
1207 return param.retval;
1210 int find_line_range(int fd, struct line_range *lr)
1212 struct line_finder lf = {.lr = lr, .found = 0};
1214 Dwarf_Off off = 0, noff;
1219 dbg = dwarf_begin(fd, DWARF_C_READ);
1221 pr_warning("No dwarf info found in the vmlinux - "
1222 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
1226 /* Loop on CUs (Compilation Unit) */
1227 while (!lf.found && ret >= 0) {
1228 if (dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL) != 0)
1231 /* Get the DIE(Debugging Information Entry) of this CU */
1232 diep = dwarf_offdie(dbg, off + cuhl, &lf.cu_die);
1236 /* Check if target file is included. */
1238 lf.fname = cu_find_realpath(&lf.cu_die, lr->file);
1242 if (!lr->file || lf.fname) {
1244 ret = find_line_range_by_func(&lf);
1246 lf.lno_s = lr->start;
1251 ret = find_line_range_by_line(NULL, &lf);
1256 pr_debug("path: %lx\n", (unsigned long)lr->path);
1259 return (ret < 0) ? ret : lf.found;