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 /* Return architecture dependent register string (for kprobe-tracer) */
88 static const char *get_arch_regstr(unsigned int n)
90 return (n <= ARCH_MAX_REGS) ? arch_regs_table[n] : NULL;
94 * Compare the tail of two strings.
95 * Return 0 if whole of either string is same as another's tail part.
97 static int strtailcmp(const char *s1, const char *s2)
101 while (--i1 >= 0 && --i2 >= 0) {
102 if (s1[i1] != s2[i2])
103 return s1[i1] - s2[i2];
108 /* Line number list operations */
110 /* Add a line to line number list */
111 static void line_list__add_line(struct list_head *head, unsigned int line)
113 struct line_node *ln;
116 /* Reverse search, because new line will be the last one */
117 list_for_each_entry_reverse(ln, head, list) {
118 if (ln->line < line) {
121 } else if (ln->line == line) /* Already exist */
124 /* List is empty, or the smallest entry */
127 pr_debug("line list: add a line %u\n", line);
128 ln = zalloc(sizeof(struct line_node));
131 INIT_LIST_HEAD(&ln->list);
132 list_add(&ln->list, p);
135 /* Check if the line in line number list */
136 static int line_list__has_line(struct list_head *head, unsigned int line)
138 struct line_node *ln;
140 /* Reverse search, because new line will be the last one */
141 list_for_each_entry(ln, head, list)
142 if (ln->line == line)
148 /* Init line number list */
149 static void line_list__init(struct list_head *head)
151 INIT_LIST_HEAD(head);
154 /* Free line number list */
155 static void line_list__free(struct list_head *head)
157 struct line_node *ln;
158 while (!list_empty(head)) {
159 ln = list_first_entry(head, struct line_node, list);
167 /* Find the realpath of the target file. */
168 static const char *cu_find_realpath(Dwarf_Die *cu_die, const char *fname)
172 const char *src = NULL;
178 ret = dwarf_getsrcfiles(cu_die, &files, &nfiles);
182 for (i = 0; i < nfiles; i++) {
183 src = dwarf_filesrc(files, i, NULL, NULL);
184 if (strtailcmp(src, fname) == 0)
190 struct __addr_die_search_param {
195 static int __die_search_func_cb(Dwarf_Die *fn_die, void *data)
197 struct __addr_die_search_param *ad = data;
199 if (dwarf_tag(fn_die) == DW_TAG_subprogram &&
200 dwarf_haspc(fn_die, ad->addr)) {
201 memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die));
202 return DWARF_CB_ABORT;
207 /* Search a real subprogram including this line, */
208 static Dwarf_Die *die_get_real_subprogram(Dwarf_Die *cu_die, Dwarf_Addr addr,
211 struct __addr_die_search_param ad;
213 ad.die_mem = die_mem;
214 /* dwarf_getscopes can't find subprogram. */
215 if (!dwarf_getfuncs(cu_die, __die_search_func_cb, &ad, 0))
221 /* Similar to dwarf_getfuncs, but returns inlined_subroutine if exists. */
222 static Dwarf_Die *die_get_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
228 ret = dwarf_child(sp_die, die_mem);
233 if (dwarf_tag(die_mem) == DW_TAG_inlined_subroutine &&
234 dwarf_haspc(die_mem, addr))
237 if (die_get_inlinefunc(die_mem, addr, &child_die)) {
238 memcpy(die_mem, &child_die, sizeof(Dwarf_Die));
241 } while (dwarf_siblingof(die_mem, die_mem) == 0);
246 /* Compare diename and tname */
247 static bool die_compare_name(Dwarf_Die *dw_die, const char *tname)
250 name = dwarf_diename(dw_die);
251 DIE_IF(name == NULL);
252 return strcmp(tname, name);
255 /* Get entry pc(or low pc, 1st entry of ranges) of the die */
256 static Dwarf_Addr die_get_entrypc(Dwarf_Die *dw_die)
261 ret = dwarf_entrypc(dw_die, &epc);
266 /* Get a variable die */
267 static Dwarf_Die *die_find_variable(Dwarf_Die *sp_die, const char *name,
274 ret = dwarf_child(sp_die, die_mem);
279 tag = dwarf_tag(die_mem);
280 if ((tag == DW_TAG_formal_parameter ||
281 tag == DW_TAG_variable) &&
282 (die_compare_name(die_mem, name) == 0))
285 if (die_find_variable(die_mem, name, &child_die)) {
286 memcpy(die_mem, &child_die, sizeof(Dwarf_Die));
289 } while (dwarf_siblingof(die_mem, die_mem) == 0);
295 * Probe finder related functions
298 /* Show a location */
299 static void show_location(Dwarf_Op *op, struct probe_finder *pf)
306 /* TODO: support CFA */
307 /* If this is based on frame buffer, set the offset */
308 if (op->atom == DW_OP_fbreg) {
309 if (pf->fb_ops == NULL)
310 die("The attribute of frame base is not supported.\n");
316 if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) {
317 regn = op->atom - DW_OP_breg0;
320 } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) {
321 regn = op->atom - DW_OP_reg0;
322 } else if (op->atom == DW_OP_bregx) {
326 } else if (op->atom == DW_OP_regx) {
329 die("DW_OP %d is not supported.", op->atom);
331 regs = get_arch_regstr(regn);
333 die("%u exceeds max register number.", regn);
336 ret = snprintf(pf->buf, pf->len, " %s=%+jd(%s)",
337 pf->var, (intmax_t)offs, regs);
339 ret = snprintf(pf->buf, pf->len, " %s=%s", pf->var, regs);
341 DIE_IF(ret >= pf->len);
344 /* Show a variables in kprobe event format */
345 static void show_variable(Dwarf_Die *vr_die, struct probe_finder *pf)
347 Dwarf_Attribute attr;
352 if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL)
354 /* TODO: handle more than 1 exprs */
355 ret = dwarf_getlocation_addr(&attr, pf->addr, &expr, &nexpr, 1);
356 if (ret <= 0 || nexpr == 0)
359 show_location(expr, pf);
360 /* *expr will be cached in libdw. Don't free it. */
363 /* TODO: Support const_value */
364 die("Failed to find the location of %s at this address.\n"
365 " Perhaps, it has been optimized out.", pf->var);
368 /* Find a variable in a subprogram die */
369 static void find_variable(Dwarf_Die *sp_die, struct probe_finder *pf)
374 /* TODO: Support struct members and arrays */
375 if (!is_c_varname(pf->var)) {
376 /* Output raw parameters */
377 ret = snprintf(pf->buf, pf->len, " %s", pf->var);
379 DIE_IF(ret >= pf->len);
383 pr_debug("Searching '%s' variable in context.\n", pf->var);
384 /* Search child die for local variables and parameters. */
385 if (!die_find_variable(sp_die, pf->var, &vr_die))
386 die("Failed to find '%s' in this function.", pf->var);
388 show_variable(&vr_die, pf);
391 /* Show a probe point to output buffer */
392 static void show_probe_point(Dwarf_Die *sp_die, struct probe_finder *pf)
394 struct probe_point *pp = pf->pp;
398 char tmp[MAX_PROBE_BUFFER];
400 Dwarf_Attribute fb_attr;
403 /* If no real subprogram, find a real one */
404 if (!sp_die || dwarf_tag(sp_die) != DW_TAG_subprogram) {
405 sp_die = die_get_real_subprogram(&pf->cu_die,
408 die("Probe point is not found in subprograms.");
411 /* Output name of probe point */
412 name = dwarf_diename(sp_die);
414 dwarf_entrypc(sp_die, &eaddr);
415 ret = snprintf(tmp, MAX_PROBE_BUFFER, "%s+%lu", name,
416 (unsigned long)(pf->addr - eaddr));
417 /* Copy the function name if possible */
419 pp->function = strdup(name);
420 pp->offset = (size_t)(pf->addr - eaddr);
423 /* This function has no name. */
424 ret = snprintf(tmp, MAX_PROBE_BUFFER, "0x%jx",
425 (uintmax_t)pf->addr);
427 /* TODO: Use _stext */
428 pp->function = strdup("");
429 pp->offset = (size_t)pf->addr;
433 DIE_IF(ret >= MAX_PROBE_BUFFER);
435 pr_debug("Probe point found: %s\n", tmp);
437 /* Get the frame base attribute/ops */
438 dwarf_attr(sp_die, DW_AT_frame_base, &fb_attr);
439 ret = dwarf_getlocation_addr(&fb_attr, pf->addr, &pf->fb_ops, &nops, 1);
440 if (ret <= 0 || nops == 0)
443 /* Find each argument */
444 /* TODO: use dwarf_cfi_addrframe */
445 for (i = 0; i < pp->nr_args; i++) {
446 pf->var = pp->args[i];
448 pf->len = MAX_PROBE_BUFFER - len;
449 find_variable(sp_die, pf);
450 len += strlen(pf->buf);
453 /* *pf->fb_ops will be cached in libdw. Don't free it. */
456 if (pp->found == MAX_PROBES)
457 die("Too many( > %d) probe point found.\n", MAX_PROBES);
459 pp->probes[pp->found] = strdup(tmp);
463 /* Find probe point from its line number */
464 static void find_probe_point_by_line(struct probe_finder *pf)
473 ret = dwarf_getsrclines(&pf->cu_die, &lines, &nlines);
476 for (i = 0; i < nlines; i++) {
477 line = dwarf_onesrcline(lines, i);
478 dwarf_lineno(line, &lineno);
479 if (lineno != pf->lno)
482 /* TODO: Get fileno from line, but how? */
483 if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
486 ret = dwarf_lineaddr(line, &addr);
488 pr_debug("Probe line found: line[%d]:%d addr:0x%jx\n",
489 (int)i, lineno, (uintmax_t)addr);
492 show_probe_point(NULL, pf);
493 /* Continuing, because target line might be inlined. */
497 /* Find lines which match lazy pattern */
498 static int find_lazy_match_lines(struct list_head *head,
499 const char *fname, const char *pat)
501 char *fbuf, *p1, *p2;
502 int fd, line, nlines = 0;
505 fd = open(fname, O_RDONLY);
507 die("failed to open %s", fname);
508 DIE_IF(fstat(fd, &st) < 0);
509 fbuf = malloc(st.st_size + 2);
510 DIE_IF(fbuf == NULL);
511 DIE_IF(read(fd, fbuf, st.st_size) < 0);
513 fbuf[st.st_size] = '\n'; /* Dummy line */
514 fbuf[st.st_size + 1] = '\0';
517 while ((p2 = strchr(p1, '\n')) != NULL) {
519 if (strlazymatch(p1, pat)) {
520 line_list__add_line(head, line);
530 /* Find probe points from lazy pattern */
531 static void find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf)
541 if (list_empty(&pf->lcache)) {
542 /* Matching lazy line pattern */
543 ret = find_lazy_match_lines(&pf->lcache, pf->fname,
546 die("No matched lines found in %s.", pf->fname);
549 ret = dwarf_getsrclines(&pf->cu_die, &lines, &nlines);
551 for (i = 0; i < nlines; i++) {
552 line = dwarf_onesrcline(lines, i);
554 dwarf_lineno(line, &lineno);
555 if (!line_list__has_line(&pf->lcache, lineno))
558 /* TODO: Get fileno from line, but how? */
559 if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
562 ret = dwarf_lineaddr(line, &addr);
565 /* Address filtering 1: does sp_die include addr? */
566 if (!dwarf_haspc(sp_die, addr))
568 /* Address filtering 2: No child include addr? */
569 if (die_get_inlinefunc(sp_die, addr, &die_mem))
573 pr_debug("Probe line found: line[%d]:%d addr:0x%llx\n",
574 (int)i, lineno, (unsigned long long)addr);
577 show_probe_point(sp_die, pf);
578 /* Continuing, because target line might be inlined. */
580 /* TODO: deallocate lines, but how? */
583 static int probe_point_inline_cb(Dwarf_Die *in_die, void *data)
585 struct probe_finder *pf = (struct probe_finder *)data;
586 struct probe_point *pp = pf->pp;
589 find_probe_point_lazy(in_die, pf);
591 /* Get probe address */
592 pf->addr = die_get_entrypc(in_die);
593 pf->addr += pp->offset;
594 pr_debug("found inline addr: 0x%jx\n",
595 (uintmax_t)pf->addr);
597 show_probe_point(in_die, pf);
603 /* Search function from function name */
604 static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
606 struct probe_finder *pf = (struct probe_finder *)data;
607 struct probe_point *pp = pf->pp;
609 /* Check tag and diename */
610 if (dwarf_tag(sp_die) != DW_TAG_subprogram ||
611 die_compare_name(sp_die, pp->function) != 0)
614 pf->fname = dwarf_decl_file(sp_die);
615 if (pp->line) { /* Function relative line */
616 dwarf_decl_line(sp_die, &pf->lno);
618 find_probe_point_by_line(pf);
619 } else if (!dwarf_func_inline(sp_die)) {
622 find_probe_point_lazy(sp_die, pf);
624 pf->addr = die_get_entrypc(sp_die);
625 pf->addr += pp->offset;
626 /* TODO: Check the address in this function */
627 show_probe_point(sp_die, pf);
630 /* Inlined function: search instances */
631 dwarf_func_inline_instances(sp_die, probe_point_inline_cb, pf);
633 return 1; /* Exit; no same symbol in this CU. */
636 static void find_probe_point_by_func(struct probe_finder *pf)
638 dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, pf, 0);
641 /* Find a probe point */
642 int find_probe_point(int fd, struct probe_point *pp)
644 struct probe_finder pf = {.pp = pp};
650 dbg = dwarf_begin(fd, DWARF_C_READ);
656 line_list__init(&pf.lcache);
657 /* Loop on CUs (Compilation Unit) */
658 while (!dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL)) {
659 /* Get the DIE(Debugging Information Entry) of this CU */
660 diep = dwarf_offdie(dbg, off + cuhl, &pf.cu_die);
664 /* Check if target file is included. */
666 pf.fname = cu_find_realpath(&pf.cu_die, pp->file);
670 if (!pp->file || pf.fname) {
672 find_probe_point_by_func(&pf);
673 else if (pp->lazy_line)
674 find_probe_point_lazy(NULL, &pf);
677 find_probe_point_by_line(&pf);
682 line_list__free(&pf.lcache);
688 /* Find line range from its line number */
689 static void find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
700 line_list__init(&lf->lr->line_list);
701 ret = dwarf_getsrclines(&lf->cu_die, &lines, &nlines);
704 for (i = 0; i < nlines; i++) {
705 line = dwarf_onesrcline(lines, i);
706 ret = dwarf_lineno(line, &lineno);
708 if (lf->lno_s > lineno || lf->lno_e < lineno)
712 /* Address filtering 1: does sp_die include addr? */
713 ret = dwarf_lineaddr(line, &addr);
715 if (!dwarf_haspc(sp_die, addr))
718 /* Address filtering 2: No child include addr? */
719 if (die_get_inlinefunc(sp_die, addr, &die_mem))
723 /* TODO: Get fileno from line, but how? */
724 src = dwarf_linesrc(line, NULL, NULL);
725 if (strtailcmp(src, lf->fname) != 0)
730 lf->lr->path = strdup(src);
731 line_list__add_line(&lf->lr->line_list, (unsigned int)lineno);
734 if (!list_empty(&lf->lr->line_list))
742 static int line_range_inline_cb(Dwarf_Die *in_die, void *data)
744 find_line_range_by_line(in_die, (struct line_finder *)data);
745 return DWARF_CB_ABORT; /* No need to find other instances */
748 /* Search function from function name */
749 static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
751 struct line_finder *lf = (struct line_finder *)data;
752 struct line_range *lr = lf->lr;
754 if (dwarf_tag(sp_die) == DW_TAG_subprogram &&
755 die_compare_name(sp_die, lr->function) == 0) {
756 lf->fname = dwarf_decl_file(sp_die);
757 dwarf_decl_line(sp_die, &lr->offset);
758 pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
759 lf->lno_s = lr->offset + lr->start;
763 lf->lno_e = lr->offset + lr->end;
764 lr->start = lf->lno_s;
766 if (dwarf_func_inline(sp_die))
767 dwarf_func_inline_instances(sp_die,
768 line_range_inline_cb, lf);
770 find_line_range_by_line(sp_die, lf);
776 static void find_line_range_by_func(struct line_finder *lf)
778 dwarf_getfuncs(&lf->cu_die, line_range_search_cb, lf, 0);
781 int find_line_range(int fd, struct line_range *lr)
783 struct line_finder lf = {.lr = lr, .found = 0};
785 Dwarf_Off off = 0, noff;
790 dbg = dwarf_begin(fd, DWARF_C_READ);
794 /* Loop on CUs (Compilation Unit) */
796 ret = dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL);
800 /* Get the DIE(Debugging Information Entry) of this CU */
801 diep = dwarf_offdie(dbg, off + cuhl, &lf.cu_die);
805 /* Check if target file is included. */
807 lf.fname = cu_find_realpath(&lf.cu_die, lr->file);
811 if (!lr->file || lf.fname) {
813 find_line_range_by_func(&lf);
815 lf.lno_s = lr->start;
820 find_line_range_by_line(NULL, &lf);
825 pr_debug("path: %lx\n", (unsigned long)lr->path);