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>
38 #include "probe-finder.h"
41 /* Dwarf_Die Linkage to parent Die */
43 struct die_link *parent; /* Parent die */
44 Dwarf_Die die; /* Current die */
49 * Generic dwarf analysis helpers
52 #define X86_32_MAX_REGS 8
53 const char *x86_32_regs_table[X86_32_MAX_REGS] = {
58 "$stack", /* Stack address instead of %sp */
64 #define X86_64_MAX_REGS 16
65 const char *x86_64_regs_table[X86_64_MAX_REGS] = {
84 /* TODO: switching by dwarf address size */
86 #define ARCH_MAX_REGS X86_64_MAX_REGS
87 #define arch_regs_table x86_64_regs_table
89 #define ARCH_MAX_REGS X86_32_MAX_REGS
90 #define arch_regs_table x86_32_regs_table
93 /* Return architecture dependent register string (for kprobe-tracer) */
94 static const char *get_arch_regstr(unsigned int n)
96 return (n <= ARCH_MAX_REGS) ? arch_regs_table[n] : NULL;
100 * Compare the tail of two strings.
101 * Return 0 if whole of either string is same as another's tail part.
103 static int strtailcmp(const char *s1, const char *s2)
107 while (--i1 >= 0 && --i2 >= 0) {
108 if (s1[i1] != s2[i2])
109 return s1[i1] - s2[i2];
114 /* Find the fileno of the target file. */
115 static int cu_find_fileno(Dwarf_Die *cu_die, const char *fname)
125 ret = dwarf_getsrcfiles(cu_die, &files, &nfiles);
127 for (i = 0; i < nfiles; i++) {
128 src = dwarf_filesrc(files, i, NULL, NULL);
129 if (strtailcmp(src, fname) == 0) {
130 ret = (int)i; /*???: +1 or not?*/
135 pr_debug("found fno: %d\n", ret);
140 struct __addr_die_search_param {
145 static int __die_search_func_cb(Dwarf_Die *fn_die, void *data)
147 struct __addr_die_search_param *ad = data;
149 if (dwarf_tag(fn_die) == DW_TAG_subprogram &&
150 dwarf_haspc(fn_die, ad->addr)) {
151 memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die));
152 return DWARF_CB_ABORT;
157 /* Search a real subprogram including this line, */
158 static Dwarf_Die *die_get_real_subprogram(Dwarf_Die *cu_die, Dwarf_Addr addr,
161 struct __addr_die_search_param ad;
163 ad.die_mem = die_mem;
164 /* dwarf_getscopes can't find subprogram. */
165 if (!dwarf_getfuncs(cu_die, __die_search_func_cb, &ad, 0))
171 /* Compare diename and tname */
172 static bool die_compare_name(Dwarf_Die *dw_die, const char *tname)
175 name = dwarf_diename(dw_die);
176 DIE_IF(name == NULL);
177 return strcmp(tname, name);
180 /* Check the address is in the subprogram(function). */
181 static bool die_within_subprogram(Dwarf_Die *sp_die, Dwarf_Addr addr,
187 ret = dwarf_haspc(sp_die, addr);
192 ret = dwarf_entrypc(sp_die, &epc);
200 /* Get entry pc(or low pc, 1st entry of ranges) of the die */
201 static Dwarf_Addr die_get_entrypc(Dwarf_Die *dw_die)
206 ret = dwarf_entrypc(dw_die, &epc);
211 /* Check if the abstract origin's address or not */
212 static bool die_compare_abstract_origin(Dwarf_Die *in_die, void *origin_addr)
214 Dwarf_Attribute attr;
217 if (!dwarf_attr(in_die, DW_AT_abstract_origin, &attr))
219 if (!dwarf_formref_die(&attr, &origin))
222 return origin.addr == origin_addr;
226 * Search a Die from Die tree.
227 * Note: cur_link->die should be deallocated in this function.
229 static int __search_die_tree(struct die_link *cur_link,
230 int (*die_cb)(struct die_link *, void *),
233 struct die_link new_link;
239 /* Check current die */
240 while (!(ret = die_cb(cur_link, data))) {
241 /* Check child die */
242 ret = dwarf_child(&cur_link->die, &new_link.die);
244 new_link.parent = cur_link;
245 ret = __search_die_tree(&new_link, die_cb, data);
250 /* Move to next sibling */
251 ret = dwarf_siblingof(&cur_link->die, &cur_link->die);
258 /* Search a die in its children's die tree */
259 static int search_die_from_children(Dwarf_Die *parent_die,
260 int (*die_cb)(struct die_link *, void *),
263 struct die_link new_link;
266 new_link.parent = NULL;
267 ret = dwarf_child(parent_die, &new_link.die);
269 return __search_die_tree(&new_link, die_cb, data);
276 * Probe finder related functions
279 /* Show a location */
280 static void show_location(Dwarf_Op *op, struct probe_finder *pf)
287 /* TODO: support CFA */
288 /* If this is based on frame buffer, set the offset */
289 if (op->atom == DW_OP_fbreg) {
290 if (pf->fb_ops == NULL)
291 die("The attribute of frame base is not supported.\n");
297 if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) {
298 regn = op->atom - DW_OP_breg0;
301 } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) {
302 regn = op->atom - DW_OP_reg0;
303 } else if (op->atom == DW_OP_bregx) {
307 } else if (op->atom == DW_OP_regx) {
310 die("DW_OP %d is not supported.", op->atom);
312 regs = get_arch_regstr(regn);
314 die("%u exceeds max register number.", regn);
317 ret = snprintf(pf->buf, pf->len, " %s=+%ju(%s)",
318 pf->var, (uintmax_t)offs, regs);
320 ret = snprintf(pf->buf, pf->len, " %s=%s", pf->var, regs);
322 DIE_IF(ret >= pf->len);
325 /* Show a variables in kprobe event format */
326 static void show_variable(Dwarf_Die *vr_die, struct probe_finder *pf)
328 Dwarf_Attribute attr;
333 if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL)
335 /* TODO: handle more than 1 exprs */
336 ret = dwarf_getlocation_addr(&attr, (pf->addr - pf->cu_base),
338 if (ret <= 0 || nexpr == 0)
341 show_location(expr, pf);
342 /* *expr will be cached in libdw. Don't free it. */
345 /* TODO: Support const_value */
346 die("Failed to find the location of %s at this address.\n"
347 " Perhaps, it has been optimized out.", pf->var);
350 static int variable_search_cb(struct die_link *dlink, void *data)
352 struct probe_finder *pf = (struct probe_finder *)data;
355 tag = dwarf_tag(&dlink->die);
357 if ((tag == DW_TAG_formal_parameter ||
358 tag == DW_TAG_variable) &&
359 (die_compare_name(&dlink->die, pf->var) == 0)) {
360 show_variable(&dlink->die, pf);
363 /* TODO: Support struct members and arrays */
367 /* Find a variable in a subprogram die */
368 static void find_variable(Dwarf_Die *sp_die, struct probe_finder *pf)
372 if (!is_c_varname(pf->var)) {
373 /* Output raw parameters */
374 ret = snprintf(pf->buf, pf->len, " %s", pf->var);
376 DIE_IF(ret >= pf->len);
380 pr_debug("Searching '%s' variable in context.\n", pf->var);
381 /* Search child die for local variables and parameters. */
382 ret = search_die_from_children(sp_die, variable_search_cb, pf);
384 die("Failed to find '%s' in this function.", pf->var);
387 /* Show a probe point to output buffer */
388 static void show_probe_point(Dwarf_Die *sp_die, size_t offs,
389 struct probe_finder *pf)
391 struct probe_point *pp = pf->pp;
393 char tmp[MAX_PROBE_BUFFER];
395 Dwarf_Attribute fb_attr;
398 /* Output name of probe point */
399 name = dwarf_diename(sp_die);
401 ret = snprintf(tmp, MAX_PROBE_BUFFER, "%s+%u", name,
403 /* Copy the function name if possible */
405 pp->function = strdup(name);
409 /* This function has no name. */
410 ret = snprintf(tmp, MAX_PROBE_BUFFER, "0x%jx",
411 (uintmax_t)pf->addr);
413 /* TODO: Use _stext */
414 pp->function = strdup("");
415 pp->offset = (size_t)pf->addr;
419 DIE_IF(ret >= MAX_PROBE_BUFFER);
421 pr_debug("Probe point found: %s\n", tmp);
423 /* Get the frame base attribute/ops */
424 dwarf_attr(sp_die, DW_AT_frame_base, &fb_attr);
425 ret = dwarf_getlocation_addr(&fb_attr, (pf->addr - pf->cu_base),
426 &pf->fb_ops, &nops, 1);
427 if (ret <= 0 || nops == 0)
430 /* Find each argument */
431 /* TODO: use dwarf_cfi_addrframe */
432 for (i = 0; i < pp->nr_args; i++) {
433 pf->var = pp->args[i];
435 pf->len = MAX_PROBE_BUFFER - len;
436 find_variable(sp_die, pf);
437 len += strlen(pf->buf);
440 /* *pf->fb_ops will be cached in libdw. Don't free it. */
443 pp->probes[pp->found] = strdup(tmp);
447 /* Find probe point from its line number */
448 static void find_probe_point_by_line(struct probe_finder *pf)
453 Dwarf_Addr addr, epc;
456 Dwarf_Die *sp_die, die_mem;
458 ret = dwarf_getsrclines(&pf->cu_die, &lines, &nlines);
461 for (i = 0; i < nlines; i++) {
462 line = dwarf_onesrcline(lines, i);
463 dwarf_lineno(line, &lineno);
464 if (lineno != pf->lno)
467 /* TODO: Get fileno from line, but how? */
468 if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
471 ret = dwarf_lineaddr(line, &addr);
473 pr_debug("Probe line found: line[%d]:%d addr:0x%jx\n",
474 (int)i, lineno, (uintmax_t)addr);
477 sp_die = die_get_real_subprogram(&pf->cu_die, addr, &die_mem);
479 die("Probe point is not found in subprograms.");
480 dwarf_entrypc(sp_die, &epc);
481 show_probe_point(sp_die, (size_t)(addr - epc), pf);
482 /* Continuing, because target line might be inlined. */
487 /* Search function from function name */
488 static int probe_point_search_cb(struct die_link *dlink, void *data)
490 struct probe_finder *pf = (struct probe_finder *)data;
491 struct probe_point *pp = pf->pp;
497 tag = dwarf_tag(&dlink->die);
498 if (tag == DW_TAG_subprogram) {
499 if (die_compare_name(&dlink->die, pp->function) == 0) {
500 if (pp->line) { /* Function relative line */
501 pf->fname = dwarf_decl_file(&dlink->die);
502 dwarf_decl_line(&dlink->die, &pf->lno);
504 find_probe_point_by_line(pf);
507 if (dwarf_func_inline(&dlink->die)) {
508 /* Inlined function, save it. */
509 pf->origin = dlink->die.addr;
510 return 0; /* Continue to search */
512 /* Get probe address */
513 pf->addr = die_get_entrypc(&dlink->die);
514 pf->addr += pp->offset;
515 /* TODO: Check the address in this function */
516 show_probe_point(&dlink->die, pp->offset, pf);
517 return 1; /* Exit; no same symbol in this CU. */
519 } else if (tag == DW_TAG_inlined_subroutine && pf->origin) {
520 if (die_compare_abstract_origin(&dlink->die, pf->origin)) {
521 /* Get probe address */
522 pf->addr = die_get_entrypc(&dlink->die);
523 pf->addr += pp->offset;
524 pr_debug("found inline addr: 0x%jx\n",
525 (uintmax_t)pf->addr);
526 /* Inlined function. Get a real subprogram */
527 for (lk = dlink->parent; lk != NULL; lk = lk->parent) {
528 tag = dwarf_tag(&lk->die);
529 if (tag == DW_TAG_subprogram &&
530 !dwarf_func_inline(&lk->die))
533 die("Failed to find real subprogram.");
535 /* Get offset from subprogram */
536 ret = die_within_subprogram(&lk->die, pf->addr, &offs);
538 show_probe_point(&lk->die, offs, pf);
539 /* Continue to search */
545 static void find_probe_point_by_func(struct probe_finder *pf)
547 search_die_from_children(&pf->cu_die, probe_point_search_cb, pf);
550 /* Find a probe point */
551 int find_probe_point(int fd, struct probe_point *pp)
553 struct probe_finder pf = {.pp = pp};
561 dbg = dwarf_begin(fd, DWARF_C_READ);
567 /* Loop on CUs (Compilation Unit) */
568 while (!dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL)) {
569 /* Get the DIE(Debugging Information Entry) of this CU */
570 diep = dwarf_offdie(dbg, off + cuhl, &pf.cu_die);
574 /* Check if target file is included. */
576 fno = cu_find_fileno(&pf.cu_die, pp->file);
580 if (!pp->file || fno) {
581 /* Save CU base address (for frame_base) */
582 ret = dwarf_lowpc(&pf.cu_die, &pf.cu_base);
586 find_probe_point_by_func(&pf);
589 find_probe_point_by_line(&pf);
600 static void line_range_add_line(struct line_range *lr, unsigned int line)
602 struct line_node *ln;
605 /* Reverse search, because new line will be the last one */
606 list_for_each_entry_reverse(ln, &lr->line_list, list) {
607 if (ln->line < line) {
610 } else if (ln->line == line) /* Already exist */
613 /* List is empty, or the smallest entry */
616 pr_debug("Debug: add a line %u\n", line);
617 ln = zalloc(sizeof(struct line_node));
620 INIT_LIST_HEAD(&ln->list);
621 list_add(&ln->list, p);
624 /* Find line range from its line number */
625 static void find_line_range_by_line(struct line_finder *lf)
635 INIT_LIST_HEAD(&lf->lr->line_list);
636 ret = dwarf_getsrclines(&lf->cu_die, &lines, &nlines);
639 for (i = 0; i < nlines; i++) {
640 line = dwarf_onesrcline(lines, i);
641 dwarf_lineno(line, &lineno);
642 if (lf->lno_s > lineno || lf->lno_e < lineno)
645 /* TODO: Get fileno from line, but how? */
646 src = dwarf_linesrc(line, NULL, NULL);
647 if (strtailcmp(src, lf->fname) != 0)
650 /* Filter line in the function address range */
651 if (lf->addr_s && lf->addr_e) {
652 ret = dwarf_lineaddr(line, &addr);
654 if (lf->addr_s > addr || lf->addr_e <= addr)
659 lf->lr->path = strdup(src);
660 line_range_add_line(lf->lr, (unsigned int)lineno);
663 if (!list_empty(&lf->lr->line_list))
671 /* Search function from function name */
672 static int line_range_search_cb(struct die_link *dlink, void *data)
674 struct line_finder *lf = (struct line_finder *)data;
675 struct line_range *lr = lf->lr;
679 tag = dwarf_tag(&dlink->die);
680 if (tag == DW_TAG_subprogram &&
681 die_compare_name(&dlink->die, lr->function) == 0) {
682 /* Get the address range of this function */
683 ret = dwarf_highpc(&dlink->die, &lf->addr_e);
685 ret = dwarf_lowpc(&dlink->die, &lf->addr_s);
691 lf->fname = dwarf_decl_file(&dlink->die);
692 dwarf_decl_line(&dlink->die, &lr->offset);
693 pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
694 lf->lno_s = lr->offset + lr->start;
698 lf->lno_e = lr->offset + lr->end;
699 lr->start = lf->lno_s;
701 find_line_range_by_line(lf);
707 static void find_line_range_by_func(struct line_finder *lf)
709 search_die_from_children(&lf->cu_die, line_range_search_cb, lf);
712 int find_line_range(int fd, struct line_range *lr)
714 struct line_finder lf = {.lr = lr, .found = 0};
716 Dwarf_Off off = 0, noff;
722 dbg = dwarf_begin(fd, DWARF_C_READ);
726 /* Loop on CUs (Compilation Unit) */
728 ret = dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL);
732 /* Get the DIE(Debugging Information Entry) of this CU */
733 diep = dwarf_offdie(dbg, off + cuhl, &lf.cu_die);
737 /* Check if target file is included. */
739 fno = cu_find_fileno(&lf.cu_die, lr->file);
743 if (!lr->file || fno) {
745 find_line_range_by_func(&lf);
748 lf.lno_s = lr->start;
753 find_line_range_by_line(&lf);
758 pr_debug("path: %lx\n", (unsigned long)lr->path);