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 void 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 = xzalloc(sizeof(struct line_node));
133 INIT_LIST_HEAD(&ln->list);
134 list_add(&ln->list, p);
137 /* Check if the line in line number list */
138 static int line_list__has_line(struct list_head *head, unsigned int line)
140 struct line_node *ln;
142 /* Reverse search, because new line will be the last one */
143 list_for_each_entry(ln, head, list)
144 if (ln->line == line)
150 /* Init line number list */
151 static void line_list__init(struct list_head *head)
153 INIT_LIST_HEAD(head);
156 /* Free line number list */
157 static void line_list__free(struct list_head *head)
159 struct line_node *ln;
160 while (!list_empty(head)) {
161 ln = list_first_entry(head, struct line_node, list);
169 /* Find the realpath of the target file. */
170 static const char *cu_find_realpath(Dwarf_Die *cu_die, const char *fname)
174 const char *src = NULL;
180 ret = dwarf_getsrcfiles(cu_die, &files, &nfiles);
184 for (i = 0; i < nfiles; i++) {
185 src = dwarf_filesrc(files, i, NULL, NULL);
186 if (strtailcmp(src, fname) == 0)
194 /* Compare diename and tname */
195 static bool die_compare_name(Dwarf_Die *dw_die, const char *tname)
198 name = dwarf_diename(dw_die);
199 DIE_IF(name == NULL);
200 return strcmp(tname, name);
203 /* Get entry pc(or low pc, 1st entry of ranges) of the die */
204 static Dwarf_Addr die_get_entrypc(Dwarf_Die *dw_die)
209 ret = dwarf_entrypc(dw_die, &epc);
214 /* Get type die, but skip qualifiers and typedef */
215 static Dwarf_Die *die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
217 Dwarf_Attribute attr;
221 if (dwarf_attr(vr_die, DW_AT_type, &attr) == NULL ||
222 dwarf_formref_die(&attr, die_mem) == NULL)
225 tag = dwarf_tag(die_mem);
227 } while (tag == DW_TAG_const_type ||
228 tag == DW_TAG_restrict_type ||
229 tag == DW_TAG_volatile_type ||
230 tag == DW_TAG_shared_type ||
231 tag == DW_TAG_typedef);
236 static bool die_is_signed_type(Dwarf_Die *tp_die)
238 Dwarf_Attribute attr;
241 if (dwarf_attr(tp_die, DW_AT_encoding, &attr) == NULL ||
242 dwarf_formudata(&attr, &ret) != 0)
245 return (ret == DW_ATE_signed_char || ret == DW_ATE_signed ||
246 ret == DW_ATE_signed_fixed);
249 static int die_get_byte_size(Dwarf_Die *tp_die)
251 Dwarf_Attribute attr;
254 if (dwarf_attr(tp_die, DW_AT_byte_size, &attr) == NULL ||
255 dwarf_formudata(&attr, &ret) != 0)
261 /* Return values for die_find callbacks */
263 DIE_FIND_CB_FOUND = 0, /* End of Search */
264 DIE_FIND_CB_CHILD = 1, /* Search only children */
265 DIE_FIND_CB_SIBLING = 2, /* Search only siblings */
266 DIE_FIND_CB_CONTINUE = 3, /* Search children and siblings */
269 /* Search a child die */
270 static Dwarf_Die *die_find_child(Dwarf_Die *rt_die,
271 int (*callback)(Dwarf_Die *, void *),
272 void *data, Dwarf_Die *die_mem)
277 ret = dwarf_child(rt_die, die_mem);
282 ret = callback(die_mem, data);
283 if (ret == DIE_FIND_CB_FOUND)
286 if ((ret & DIE_FIND_CB_CHILD) &&
287 die_find_child(die_mem, callback, data, &child_die)) {
288 memcpy(die_mem, &child_die, sizeof(Dwarf_Die));
291 } while ((ret & DIE_FIND_CB_SIBLING) &&
292 dwarf_siblingof(die_mem, die_mem) == 0);
297 struct __addr_die_search_param {
302 static int __die_search_func_cb(Dwarf_Die *fn_die, void *data)
304 struct __addr_die_search_param *ad = data;
306 if (dwarf_tag(fn_die) == DW_TAG_subprogram &&
307 dwarf_haspc(fn_die, ad->addr)) {
308 memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die));
309 return DWARF_CB_ABORT;
314 /* Search a real subprogram including this line, */
315 static Dwarf_Die *die_find_real_subprogram(Dwarf_Die *cu_die, Dwarf_Addr addr,
318 struct __addr_die_search_param ad;
320 ad.die_mem = die_mem;
321 /* dwarf_getscopes can't find subprogram. */
322 if (!dwarf_getfuncs(cu_die, __die_search_func_cb, &ad, 0))
328 /* die_find callback for inline function search */
329 static int __die_find_inline_cb(Dwarf_Die *die_mem, void *data)
331 Dwarf_Addr *addr = data;
333 if (dwarf_tag(die_mem) == DW_TAG_inlined_subroutine &&
334 dwarf_haspc(die_mem, *addr))
335 return DIE_FIND_CB_FOUND;
337 return DIE_FIND_CB_CONTINUE;
340 /* Similar to dwarf_getfuncs, but returns inlined_subroutine if exists. */
341 static Dwarf_Die *die_find_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
344 return die_find_child(sp_die, __die_find_inline_cb, &addr, die_mem);
347 static int __die_find_variable_cb(Dwarf_Die *die_mem, void *data)
349 const char *name = data;
352 tag = dwarf_tag(die_mem);
353 if ((tag == DW_TAG_formal_parameter ||
354 tag == DW_TAG_variable) &&
355 (die_compare_name(die_mem, name) == 0))
356 return DIE_FIND_CB_FOUND;
358 return DIE_FIND_CB_CONTINUE;
361 /* Find a variable called 'name' */
362 static Dwarf_Die *die_find_variable(Dwarf_Die *sp_die, const char *name,
365 return die_find_child(sp_die, __die_find_variable_cb, (void *)name,
369 static int __die_find_member_cb(Dwarf_Die *die_mem, void *data)
371 const char *name = data;
373 if ((dwarf_tag(die_mem) == DW_TAG_member) &&
374 (die_compare_name(die_mem, name) == 0))
375 return DIE_FIND_CB_FOUND;
377 return DIE_FIND_CB_SIBLING;
380 /* Find a member called 'name' */
381 static Dwarf_Die *die_find_member(Dwarf_Die *st_die, const char *name,
384 return die_find_child(st_die, __die_find_member_cb, (void *)name,
389 * Probe finder related functions
392 /* Show a location */
393 static void convert_location(Dwarf_Op *op, struct probe_finder *pf)
399 struct kprobe_trace_arg *tvar = pf->tvar;
401 /* TODO: support CFA */
402 /* If this is based on frame buffer, set the offset */
403 if (op->atom == DW_OP_fbreg) {
404 if (pf->fb_ops == NULL)
405 die("The attribute of frame base is not supported.\n");
411 if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) {
412 regn = op->atom - DW_OP_breg0;
415 } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) {
416 regn = op->atom - DW_OP_reg0;
417 } else if (op->atom == DW_OP_bregx) {
421 } else if (op->atom == DW_OP_regx) {
424 die("DW_OP %d is not supported.", op->atom);
426 regs = get_arch_regstr(regn);
428 die("%u exceeds max register number.", regn);
430 tvar->value = xstrdup(regs);
432 tvar->ref = xzalloc(sizeof(struct kprobe_trace_arg_ref));
433 tvar->ref->offset = (long)offs;
437 static void convert_variable_type(Dwarf_Die *vr_die,
438 struct kprobe_trace_arg *targ)
444 if (die_get_real_type(vr_die, &type) == NULL)
445 die("Failed to get a type information of %s.",
446 dwarf_diename(vr_die));
448 ret = die_get_byte_size(&type) * 8;
450 /* Check the bitwidth */
451 if (ret > MAX_BASIC_TYPE_BITS) {
452 pr_warning(" Warning: %s exceeds max-bitwidth."
453 " Cut down to %d bits.\n",
454 dwarf_diename(&type), MAX_BASIC_TYPE_BITS);
455 ret = MAX_BASIC_TYPE_BITS;
458 ret = snprintf(buf, 16, "%c%d",
459 die_is_signed_type(&type) ? 's' : 'u', ret);
460 if (ret < 0 || ret >= 16)
461 die("Failed to convert variable type.");
462 targ->type = xstrdup(buf);
466 static void convert_variable_fields(Dwarf_Die *vr_die, const char *varname,
467 struct perf_probe_arg_field *field,
468 struct kprobe_trace_arg_ref **ref_ptr,
471 struct kprobe_trace_arg_ref *ref = *ref_ptr;
472 Dwarf_Attribute attr;
476 pr_debug("converting %s in %s\n", field->name, varname);
477 if (die_get_real_type(vr_die, &type) == NULL)
478 die("Failed to get a type information of %s.", varname);
480 /* Check the pointer and dereference */
481 if (dwarf_tag(&type) == DW_TAG_pointer_type) {
483 die("Semantic error: %s must be referred by '->'",
485 /* Get the type pointed by this pointer */
486 if (die_get_real_type(&type, &type) == NULL)
487 die("Failed to get a type information of %s.", varname);
489 /* Verify it is a data structure */
490 if (dwarf_tag(&type) != DW_TAG_structure_type)
491 die("%s is not a data structure.", varname);
493 ref = xzalloc(sizeof(struct kprobe_trace_arg_ref));
495 (*ref_ptr)->next = ref;
499 /* Verify it is a data structure */
500 if (dwarf_tag(&type) != DW_TAG_structure_type)
501 die("%s is not a data structure.", varname);
504 die("Semantic error: %s must be referred by '.'",
507 die("Structure on a register is not supported yet.");
510 if (die_find_member(&type, field->name, die_mem) == NULL)
511 die("%s(tyep:%s) has no member %s.", varname,
512 dwarf_diename(&type), field->name);
514 /* Get the offset of the field */
515 if (dwarf_attr(die_mem, DW_AT_data_member_location, &attr) == NULL ||
516 dwarf_formudata(&attr, &offs) != 0)
517 die("Failed to get the offset of %s.", field->name);
518 ref->offset += (long)offs;
520 /* Converting next field */
522 convert_variable_fields(die_mem, field->name, field->next,
526 /* Show a variables in kprobe event format */
527 static void convert_variable(Dwarf_Die *vr_die, struct probe_finder *pf)
529 Dwarf_Attribute attr;
535 if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL)
537 /* TODO: handle more than 1 exprs */
538 ret = dwarf_getlocation_addr(&attr, pf->addr, &expr, &nexpr, 1);
539 if (ret <= 0 || nexpr == 0)
542 convert_location(expr, pf);
544 if (pf->pvar->field) {
545 convert_variable_fields(vr_die, pf->pvar->var,
546 pf->pvar->field, &pf->tvar->ref,
550 convert_variable_type(vr_die, pf->tvar);
551 /* *expr will be cached in libdw. Don't free it. */
554 /* TODO: Support const_value */
555 die("Failed to find the location of %s at this address.\n"
556 " Perhaps, it has been optimized out.", pf->pvar->var);
559 /* Find a variable in a subprogram die */
560 static void find_variable(Dwarf_Die *sp_die, struct probe_finder *pf)
565 /* TODO: Support arrays */
567 pf->tvar->name = xstrdup(pf->pvar->name);
569 synthesize_perf_probe_arg(pf->pvar, buf, 32);
570 pf->tvar->name = xstrdup(buf);
573 if (!is_c_varname(pf->pvar->var)) {
574 /* Copy raw parameters */
575 pf->tvar->value = xstrdup(pf->pvar->var);
577 pr_debug("Searching '%s' variable in context.\n",
579 /* Search child die for local variables and parameters. */
580 if (!die_find_variable(sp_die, pf->pvar->var, &vr_die))
581 die("Failed to find '%s' in this function.",
583 convert_variable(&vr_die, pf);
587 /* Show a probe point to output buffer */
588 static void convert_probe_point(Dwarf_Die *sp_die, struct probe_finder *pf)
590 struct kprobe_trace_event *tev;
595 Dwarf_Attribute fb_attr;
598 if (pf->ntevs == MAX_PROBES)
599 die("Too many( > %d) probe point found.\n", MAX_PROBES);
600 tev = &pf->tevs[pf->ntevs++];
602 /* If no real subprogram, find a real one */
603 if (!sp_die || dwarf_tag(sp_die) != DW_TAG_subprogram) {
604 sp_die = die_find_real_subprogram(&pf->cu_die,
607 die("Probe point is not found in subprograms.");
610 /* Copy the name of probe point */
611 name = dwarf_diename(sp_die);
613 dwarf_entrypc(sp_die, &eaddr);
614 tev->point.symbol = xstrdup(name);
615 tev->point.offset = (unsigned long)(pf->addr - eaddr);
617 /* This function has no name. */
618 tev->point.offset = (unsigned long)pf->addr;
620 pr_debug("Probe point found: %s+%lu\n", tev->point.symbol,
623 /* Get the frame base attribute/ops */
624 dwarf_attr(sp_die, DW_AT_frame_base, &fb_attr);
625 ret = dwarf_getlocation_addr(&fb_attr, pf->addr, &pf->fb_ops, &nops, 1);
626 if (ret <= 0 || nops == 0)
629 /* Find each argument */
630 /* TODO: use dwarf_cfi_addrframe */
631 tev->nargs = pf->pev->nargs;
632 tev->args = xzalloc(sizeof(struct kprobe_trace_arg) * tev->nargs);
633 for (i = 0; i < pf->pev->nargs; i++) {
634 pf->pvar = &pf->pev->args[i];
635 pf->tvar = &tev->args[i];
636 find_variable(sp_die, pf);
639 /* *pf->fb_ops will be cached in libdw. Don't free it. */
643 /* Find probe point from its line number */
644 static void find_probe_point_by_line(struct probe_finder *pf)
653 ret = dwarf_getsrclines(&pf->cu_die, &lines, &nlines);
656 for (i = 0; i < nlines; i++) {
657 line = dwarf_onesrcline(lines, i);
658 dwarf_lineno(line, &lineno);
659 if (lineno != pf->lno)
662 /* TODO: Get fileno from line, but how? */
663 if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
666 ret = dwarf_lineaddr(line, &addr);
668 pr_debug("Probe line found: line[%d]:%d addr:0x%jx\n",
669 (int)i, lineno, (uintmax_t)addr);
672 convert_probe_point(NULL, pf);
673 /* Continuing, because target line might be inlined. */
677 /* Find lines which match lazy pattern */
678 static int find_lazy_match_lines(struct list_head *head,
679 const char *fname, const char *pat)
681 char *fbuf, *p1, *p2;
682 int fd, line, nlines = 0;
685 fd = open(fname, O_RDONLY);
687 die("failed to open %s", fname);
688 DIE_IF(fstat(fd, &st) < 0);
689 fbuf = xmalloc(st.st_size + 2);
690 DIE_IF(read(fd, fbuf, st.st_size) < 0);
692 fbuf[st.st_size] = '\n'; /* Dummy line */
693 fbuf[st.st_size + 1] = '\0';
696 while ((p2 = strchr(p1, '\n')) != NULL) {
698 if (strlazymatch(p1, pat)) {
699 line_list__add_line(head, line);
709 /* Find probe points from lazy pattern */
710 static void find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf)
720 if (list_empty(&pf->lcache)) {
721 /* Matching lazy line pattern */
722 ret = find_lazy_match_lines(&pf->lcache, pf->fname,
723 pf->pev->point.lazy_line);
725 die("No matched lines found in %s.", pf->fname);
728 ret = dwarf_getsrclines(&pf->cu_die, &lines, &nlines);
730 for (i = 0; i < nlines; i++) {
731 line = dwarf_onesrcline(lines, i);
733 dwarf_lineno(line, &lineno);
734 if (!line_list__has_line(&pf->lcache, lineno))
737 /* TODO: Get fileno from line, but how? */
738 if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
741 ret = dwarf_lineaddr(line, &addr);
744 /* Address filtering 1: does sp_die include addr? */
745 if (!dwarf_haspc(sp_die, addr))
747 /* Address filtering 2: No child include addr? */
748 if (die_find_inlinefunc(sp_die, addr, &die_mem))
752 pr_debug("Probe line found: line[%d]:%d addr:0x%llx\n",
753 (int)i, lineno, (unsigned long long)addr);
756 convert_probe_point(sp_die, pf);
757 /* Continuing, because target line might be inlined. */
759 /* TODO: deallocate lines, but how? */
762 static int probe_point_inline_cb(Dwarf_Die *in_die, void *data)
764 struct probe_finder *pf = (struct probe_finder *)data;
765 struct perf_probe_point *pp = &pf->pev->point;
768 find_probe_point_lazy(in_die, pf);
770 /* Get probe address */
771 pf->addr = die_get_entrypc(in_die);
772 pf->addr += pp->offset;
773 pr_debug("found inline addr: 0x%jx\n",
774 (uintmax_t)pf->addr);
776 convert_probe_point(in_die, pf);
782 /* Search function from function name */
783 static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
785 struct probe_finder *pf = (struct probe_finder *)data;
786 struct perf_probe_point *pp = &pf->pev->point;
788 /* Check tag and diename */
789 if (dwarf_tag(sp_die) != DW_TAG_subprogram ||
790 die_compare_name(sp_die, pp->function) != 0)
793 pf->fname = dwarf_decl_file(sp_die);
794 if (pp->line) { /* Function relative line */
795 dwarf_decl_line(sp_die, &pf->lno);
797 find_probe_point_by_line(pf);
798 } else if (!dwarf_func_inline(sp_die)) {
801 find_probe_point_lazy(sp_die, pf);
803 pf->addr = die_get_entrypc(sp_die);
804 pf->addr += pp->offset;
805 /* TODO: Check the address in this function */
806 convert_probe_point(sp_die, pf);
809 /* Inlined function: search instances */
810 dwarf_func_inline_instances(sp_die, probe_point_inline_cb, pf);
812 return 1; /* Exit; no same symbol in this CU. */
815 static void find_probe_point_by_func(struct probe_finder *pf)
817 dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, pf, 0);
820 /* Find kprobe_trace_events specified by perf_probe_event from debuginfo */
821 int find_kprobe_trace_events(int fd, struct perf_probe_event *pev,
822 struct kprobe_trace_event **tevs)
824 struct probe_finder pf = {.pev = pev};
825 struct perf_probe_point *pp = &pev->point;
831 pf.tevs = xzalloc(sizeof(struct kprobe_trace_event) * MAX_PROBES);
835 dbg = dwarf_begin(fd, DWARF_C_READ);
840 line_list__init(&pf.lcache);
841 /* Loop on CUs (Compilation Unit) */
842 while (!dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL)) {
843 /* Get the DIE(Debugging Information Entry) of this CU */
844 diep = dwarf_offdie(dbg, off + cuhl, &pf.cu_die);
848 /* Check if target file is included. */
850 pf.fname = cu_find_realpath(&pf.cu_die, pp->file);
854 if (!pp->file || pf.fname) {
856 find_probe_point_by_func(&pf);
857 else if (pp->lazy_line)
858 find_probe_point_lazy(NULL, &pf);
861 find_probe_point_by_line(&pf);
866 line_list__free(&pf.lcache);
873 int find_perf_probe_point(int fd, unsigned long addr,
874 struct perf_probe_point *ppt)
876 Dwarf_Die cudie, spdie, indie;
879 Dwarf_Addr laddr, eaddr;
883 dbg = dwarf_begin(fd, DWARF_C_READ);
888 if (!dwarf_addrdie(dbg, (Dwarf_Addr)addr, &cudie)) {
893 /* Find a corresponding line */
894 line = dwarf_getsrc_die(&cudie, (Dwarf_Addr)addr);
896 dwarf_lineaddr(line, &laddr);
897 if ((Dwarf_Addr)addr == laddr) {
898 dwarf_lineno(line, &lineno);
901 tmp = dwarf_linesrc(line, NULL, NULL);
903 ppt->file = xstrdup(tmp);
908 /* Find a corresponding function */
909 if (die_find_real_subprogram(&cudie, (Dwarf_Addr)addr, &spdie)) {
910 tmp = dwarf_diename(&spdie);
914 dwarf_entrypc(&spdie, &eaddr);
916 /* We don't have a line number, let's use offset */
917 ppt->function = xstrdup(tmp);
918 ppt->offset = addr - (unsigned long)eaddr;
922 if (die_find_inlinefunc(&spdie, (Dwarf_Addr)addr, &indie)) {
923 /* addr in an inline function */
924 tmp = dwarf_diename(&indie);
927 dwarf_decl_line(&indie, &lineno);
929 if (eaddr == addr) /* No offset: function entry */
932 dwarf_decl_line(&spdie, &lineno);
934 ppt->function = xstrdup(tmp);
935 ppt->line -= lineno; /* Make a relative line number */
944 /* Find line range from its line number */
945 static void find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
956 line_list__init(&lf->lr->line_list);
957 ret = dwarf_getsrclines(&lf->cu_die, &lines, &nlines);
960 for (i = 0; i < nlines; i++) {
961 line = dwarf_onesrcline(lines, i);
962 ret = dwarf_lineno(line, &lineno);
964 if (lf->lno_s > lineno || lf->lno_e < lineno)
968 /* Address filtering 1: does sp_die include addr? */
969 ret = dwarf_lineaddr(line, &addr);
971 if (!dwarf_haspc(sp_die, addr))
974 /* Address filtering 2: No child include addr? */
975 if (die_find_inlinefunc(sp_die, addr, &die_mem))
979 /* TODO: Get fileno from line, but how? */
980 src = dwarf_linesrc(line, NULL, NULL);
981 if (strtailcmp(src, lf->fname) != 0)
986 lf->lr->path = xstrdup(src);
987 line_list__add_line(&lf->lr->line_list, (unsigned int)lineno);
990 if (!list_empty(&lf->lr->line_list))
998 static int line_range_inline_cb(Dwarf_Die *in_die, void *data)
1000 find_line_range_by_line(in_die, (struct line_finder *)data);
1001 return DWARF_CB_ABORT; /* No need to find other instances */
1004 /* Search function from function name */
1005 static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
1007 struct line_finder *lf = (struct line_finder *)data;
1008 struct line_range *lr = lf->lr;
1010 if (dwarf_tag(sp_die) == DW_TAG_subprogram &&
1011 die_compare_name(sp_die, lr->function) == 0) {
1012 lf->fname = dwarf_decl_file(sp_die);
1013 dwarf_decl_line(sp_die, &lr->offset);
1014 pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
1015 lf->lno_s = lr->offset + lr->start;
1017 lf->lno_e = INT_MAX;
1019 lf->lno_e = lr->offset + lr->end;
1020 lr->start = lf->lno_s;
1021 lr->end = lf->lno_e;
1022 if (dwarf_func_inline(sp_die))
1023 dwarf_func_inline_instances(sp_die,
1024 line_range_inline_cb, lf);
1026 find_line_range_by_line(sp_die, lf);
1032 static void find_line_range_by_func(struct line_finder *lf)
1034 dwarf_getfuncs(&lf->cu_die, line_range_search_cb, lf, 0);
1037 int find_line_range(int fd, struct line_range *lr)
1039 struct line_finder lf = {.lr = lr, .found = 0};
1041 Dwarf_Off off = 0, noff;
1046 dbg = dwarf_begin(fd, DWARF_C_READ);
1050 /* Loop on CUs (Compilation Unit) */
1052 ret = dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL);
1056 /* Get the DIE(Debugging Information Entry) of this CU */
1057 diep = dwarf_offdie(dbg, off + cuhl, &lf.cu_die);
1061 /* Check if target file is included. */
1063 lf.fname = cu_find_realpath(&lf.cu_die, lr->file);
1067 if (!lr->file || lf.fname) {
1069 find_line_range_by_func(&lf);
1071 lf.lno_s = lr->start;
1076 find_line_range_by_line(NULL, &lf);
1081 pr_debug("path: %lx\n", (unsigned long)lr->path);