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"
42 * Generic dwarf analysis helpers
45 #define X86_32_MAX_REGS 8
46 const char *x86_32_regs_table[X86_32_MAX_REGS] = {
51 "$stack", /* Stack address instead of %sp */
57 #define X86_64_MAX_REGS 16
58 const char *x86_64_regs_table[X86_64_MAX_REGS] = {
77 /* TODO: switching by dwarf address size */
79 #define ARCH_MAX_REGS X86_64_MAX_REGS
80 #define arch_regs_table x86_64_regs_table
82 #define ARCH_MAX_REGS X86_32_MAX_REGS
83 #define arch_regs_table x86_32_regs_table
86 /* Return architecture dependent register string (for kprobe-tracer) */
87 static const char *get_arch_regstr(unsigned int n)
89 return (n <= ARCH_MAX_REGS) ? arch_regs_table[n] : NULL;
93 * Compare the tail of two strings.
94 * Return 0 if whole of either string is same as another's tail part.
96 static int strtailcmp(const char *s1, const char *s2)
100 while (--i1 >= 0 && --i2 >= 0) {
101 if (s1[i1] != s2[i2])
102 return s1[i1] - s2[i2];
107 /* Find the fileno of the target file. */
108 static int cu_find_fileno(Dwarf_Die *cu_die, const char *fname)
118 ret = dwarf_getsrcfiles(cu_die, &files, &nfiles);
120 for (i = 0; i < nfiles; i++) {
121 src = dwarf_filesrc(files, i, NULL, NULL);
122 if (strtailcmp(src, fname) == 0) {
123 ret = (int)i; /*???: +1 or not?*/
128 pr_debug("found fno: %d\n", ret);
133 struct __addr_die_search_param {
138 static int __die_search_func_cb(Dwarf_Die *fn_die, void *data)
140 struct __addr_die_search_param *ad = data;
142 if (dwarf_tag(fn_die) == DW_TAG_subprogram &&
143 dwarf_haspc(fn_die, ad->addr)) {
144 memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die));
145 return DWARF_CB_ABORT;
150 /* Search a real subprogram including this line, */
151 static Dwarf_Die *die_get_real_subprogram(Dwarf_Die *cu_die, Dwarf_Addr addr,
154 struct __addr_die_search_param ad;
156 ad.die_mem = die_mem;
157 /* dwarf_getscopes can't find subprogram. */
158 if (!dwarf_getfuncs(cu_die, __die_search_func_cb, &ad, 0))
164 /* Similar to dwarf_getfuncs, but returns inlined_subroutine if exists. */
165 static Dwarf_Die *die_get_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
171 ret = dwarf_child(sp_die, die_mem);
176 if (dwarf_tag(die_mem) == DW_TAG_inlined_subroutine &&
177 dwarf_haspc(die_mem, addr))
180 if (die_get_inlinefunc(die_mem, addr, &child_die)) {
181 memcpy(die_mem, &child_die, sizeof(Dwarf_Die));
184 } while (dwarf_siblingof(die_mem, die_mem) == 0);
189 /* Compare diename and tname */
190 static bool die_compare_name(Dwarf_Die *dw_die, const char *tname)
193 name = dwarf_diename(dw_die);
194 DIE_IF(name == NULL);
195 return strcmp(tname, name);
198 /* Get entry pc(or low pc, 1st entry of ranges) of the die */
199 static Dwarf_Addr die_get_entrypc(Dwarf_Die *dw_die)
204 ret = dwarf_entrypc(dw_die, &epc);
209 /* Get a variable die */
210 static Dwarf_Die *die_find_variable(Dwarf_Die *sp_die, const char *name,
217 ret = dwarf_child(sp_die, die_mem);
222 tag = dwarf_tag(die_mem);
223 if ((tag == DW_TAG_formal_parameter ||
224 tag == DW_TAG_variable) &&
225 (die_compare_name(die_mem, name) == 0))
228 if (die_find_variable(die_mem, name, &child_die)) {
229 memcpy(die_mem, &child_die, sizeof(Dwarf_Die));
232 } while (dwarf_siblingof(die_mem, die_mem) == 0);
238 * Probe finder related functions
241 /* Show a location */
242 static void show_location(Dwarf_Op *op, struct probe_finder *pf)
249 /* TODO: support CFA */
250 /* If this is based on frame buffer, set the offset */
251 if (op->atom == DW_OP_fbreg) {
252 if (pf->fb_ops == NULL)
253 die("The attribute of frame base is not supported.\n");
259 if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) {
260 regn = op->atom - DW_OP_breg0;
263 } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) {
264 regn = op->atom - DW_OP_reg0;
265 } else if (op->atom == DW_OP_bregx) {
269 } else if (op->atom == DW_OP_regx) {
272 die("DW_OP %d is not supported.", op->atom);
274 regs = get_arch_regstr(regn);
276 die("%u exceeds max register number.", regn);
279 ret = snprintf(pf->buf, pf->len, " %s=+%ju(%s)",
280 pf->var, (uintmax_t)offs, regs);
282 ret = snprintf(pf->buf, pf->len, " %s=%s", pf->var, regs);
284 DIE_IF(ret >= pf->len);
287 /* Show a variables in kprobe event format */
288 static void show_variable(Dwarf_Die *vr_die, struct probe_finder *pf)
290 Dwarf_Attribute attr;
295 if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL)
297 /* TODO: handle more than 1 exprs */
298 ret = dwarf_getlocation_addr(&attr, (pf->addr - pf->cu_base),
300 if (ret <= 0 || nexpr == 0)
303 show_location(expr, pf);
304 /* *expr will be cached in libdw. Don't free it. */
307 /* TODO: Support const_value */
308 die("Failed to find the location of %s at this address.\n"
309 " Perhaps, it has been optimized out.", pf->var);
312 /* Find a variable in a subprogram die */
313 static void find_variable(Dwarf_Die *sp_die, struct probe_finder *pf)
318 /* TODO: Support struct members and arrays */
319 if (!is_c_varname(pf->var)) {
320 /* Output raw parameters */
321 ret = snprintf(pf->buf, pf->len, " %s", pf->var);
323 DIE_IF(ret >= pf->len);
327 pr_debug("Searching '%s' variable in context.\n", pf->var);
328 /* Search child die for local variables and parameters. */
329 if (!die_find_variable(sp_die, pf->var, &vr_die))
330 die("Failed to find '%s' in this function.", pf->var);
332 show_variable(&vr_die, pf);
335 /* Show a probe point to output buffer */
336 static void show_probe_point(Dwarf_Die *sp_die, struct probe_finder *pf)
338 struct probe_point *pp = pf->pp;
342 char tmp[MAX_PROBE_BUFFER];
344 Dwarf_Attribute fb_attr;
347 /* If no real subprogram, find a real one */
348 if (!sp_die || dwarf_tag(sp_die) != DW_TAG_subprogram) {
349 sp_die = die_get_real_subprogram(&pf->cu_die,
352 die("Probe point is not found in subprograms.");
355 /* Output name of probe point */
356 name = dwarf_diename(sp_die);
358 dwarf_entrypc(sp_die, &eaddr);
359 ret = snprintf(tmp, MAX_PROBE_BUFFER, "%s+%lu", name,
360 (unsigned long)(pf->addr - eaddr));
361 /* Copy the function name if possible */
363 pp->function = strdup(name);
364 pp->offset = (size_t)(pf->addr - eaddr);
367 /* This function has no name. */
368 ret = snprintf(tmp, MAX_PROBE_BUFFER, "0x%jx",
369 (uintmax_t)pf->addr);
371 /* TODO: Use _stext */
372 pp->function = strdup("");
373 pp->offset = (size_t)pf->addr;
377 DIE_IF(ret >= MAX_PROBE_BUFFER);
379 pr_debug("Probe point found: %s\n", tmp);
381 /* Get the frame base attribute/ops */
382 dwarf_attr(sp_die, DW_AT_frame_base, &fb_attr);
383 ret = dwarf_getlocation_addr(&fb_attr, (pf->addr - pf->cu_base),
384 &pf->fb_ops, &nops, 1);
385 if (ret <= 0 || nops == 0)
388 /* Find each argument */
389 /* TODO: use dwarf_cfi_addrframe */
390 for (i = 0; i < pp->nr_args; i++) {
391 pf->var = pp->args[i];
393 pf->len = MAX_PROBE_BUFFER - len;
394 find_variable(sp_die, pf);
395 len += strlen(pf->buf);
398 /* *pf->fb_ops will be cached in libdw. Don't free it. */
401 pp->probes[pp->found] = strdup(tmp);
405 /* Find probe point from its line number */
406 static void find_probe_point_by_line(struct probe_finder *pf)
415 ret = dwarf_getsrclines(&pf->cu_die, &lines, &nlines);
418 for (i = 0; i < nlines; i++) {
419 line = dwarf_onesrcline(lines, i);
420 dwarf_lineno(line, &lineno);
421 if (lineno != pf->lno)
424 /* TODO: Get fileno from line, but how? */
425 if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
428 ret = dwarf_lineaddr(line, &addr);
430 pr_debug("Probe line found: line[%d]:%d addr:0x%jx\n",
431 (int)i, lineno, (uintmax_t)addr);
434 show_probe_point(NULL, pf);
435 /* Continuing, because target line might be inlined. */
439 static int probe_point_inline_cb(Dwarf_Die *in_die, void *data)
441 struct probe_finder *pf = (struct probe_finder *)data;
442 struct probe_point *pp = pf->pp;
444 /* Get probe address */
445 pf->addr = die_get_entrypc(in_die);
446 pf->addr += pp->offset;
447 pr_debug("found inline addr: 0x%jx\n", (uintmax_t)pf->addr);
449 show_probe_point(in_die, pf);
453 /* Search function from function name */
454 static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
456 struct probe_finder *pf = (struct probe_finder *)data;
457 struct probe_point *pp = pf->pp;
459 /* Check tag and diename */
460 if (dwarf_tag(sp_die) != DW_TAG_subprogram ||
461 die_compare_name(sp_die, pp->function) != 0)
464 if (pp->line) { /* Function relative line */
465 pf->fname = dwarf_decl_file(sp_die);
466 dwarf_decl_line(sp_die, &pf->lno);
468 find_probe_point_by_line(pf);
469 } else if (!dwarf_func_inline(sp_die)) {
471 pf->addr = die_get_entrypc(sp_die);
472 pf->addr += pp->offset;
473 /* TODO: Check the address in this function */
474 show_probe_point(sp_die, pf);
476 /* Inlined function: search instances */
477 dwarf_func_inline_instances(sp_die, probe_point_inline_cb, pf);
479 return 1; /* Exit; no same symbol in this CU. */
482 static void find_probe_point_by_func(struct probe_finder *pf)
484 dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, pf, 0);
487 /* Find a probe point */
488 int find_probe_point(int fd, struct probe_point *pp)
490 struct probe_finder pf = {.pp = pp};
498 dbg = dwarf_begin(fd, DWARF_C_READ);
504 /* Loop on CUs (Compilation Unit) */
505 while (!dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL)) {
506 /* Get the DIE(Debugging Information Entry) of this CU */
507 diep = dwarf_offdie(dbg, off + cuhl, &pf.cu_die);
511 /* Check if target file is included. */
513 fno = cu_find_fileno(&pf.cu_die, pp->file);
517 if (!pp->file || fno) {
518 /* Save CU base address (for frame_base) */
519 ret = dwarf_lowpc(&pf.cu_die, &pf.cu_base);
523 find_probe_point_by_func(&pf);
526 find_probe_point_by_line(&pf);
537 static void line_range_add_line(struct line_range *lr, unsigned int line)
539 struct line_node *ln;
542 /* Reverse search, because new line will be the last one */
543 list_for_each_entry_reverse(ln, &lr->line_list, list) {
544 if (ln->line < line) {
547 } else if (ln->line == line) /* Already exist */
550 /* List is empty, or the smallest entry */
553 pr_debug("Debug: add a line %u\n", line);
554 ln = zalloc(sizeof(struct line_node));
557 INIT_LIST_HEAD(&ln->list);
558 list_add(&ln->list, p);
561 /* Find line range from its line number */
562 static void find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
573 INIT_LIST_HEAD(&lf->lr->line_list);
574 ret = dwarf_getsrclines(&lf->cu_die, &lines, &nlines);
577 for (i = 0; i < nlines; i++) {
578 line = dwarf_onesrcline(lines, i);
579 ret = dwarf_lineno(line, &lineno);
581 if (lf->lno_s > lineno || lf->lno_e < lineno)
585 /* Address filtering 1: does sp_die include addr? */
586 ret = dwarf_lineaddr(line, &addr);
588 if (!dwarf_haspc(sp_die, addr))
591 /* Address filtering 2: No child include addr? */
592 if (die_get_inlinefunc(sp_die, addr, &die_mem))
596 /* TODO: Get fileno from line, but how? */
597 src = dwarf_linesrc(line, NULL, NULL);
598 if (strtailcmp(src, lf->fname) != 0)
603 lf->lr->path = strdup(src);
604 line_range_add_line(lf->lr, (unsigned int)lineno);
607 if (!list_empty(&lf->lr->line_list))
615 static int line_range_inline_cb(Dwarf_Die *in_die, void *data)
617 find_line_range_by_line(in_die, (struct line_finder *)data);
618 return DWARF_CB_ABORT; /* No need to find other instances */
621 /* Search function from function name */
622 static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
624 struct line_finder *lf = (struct line_finder *)data;
625 struct line_range *lr = lf->lr;
627 if (dwarf_tag(sp_die) == DW_TAG_subprogram &&
628 die_compare_name(sp_die, lr->function) == 0) {
629 lf->fname = dwarf_decl_file(sp_die);
630 dwarf_decl_line(sp_die, &lr->offset);
631 pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
632 lf->lno_s = lr->offset + lr->start;
636 lf->lno_e = lr->offset + lr->end;
637 lr->start = lf->lno_s;
639 if (dwarf_func_inline(sp_die))
640 dwarf_func_inline_instances(sp_die,
641 line_range_inline_cb, lf);
643 find_line_range_by_line(sp_die, lf);
649 static void find_line_range_by_func(struct line_finder *lf)
651 dwarf_getfuncs(&lf->cu_die, line_range_search_cb, lf, 0);
654 int find_line_range(int fd, struct line_range *lr)
656 struct line_finder lf = {.lr = lr, .found = 0};
658 Dwarf_Off off = 0, noff;
664 dbg = dwarf_begin(fd, DWARF_C_READ);
668 /* Loop on CUs (Compilation Unit) */
670 ret = dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL);
674 /* Get the DIE(Debugging Information Entry) of this CU */
675 diep = dwarf_offdie(dbg, off + cuhl, &lf.cu_die);
679 /* Check if target file is included. */
681 fno = cu_find_fileno(&lf.cu_die, lr->file);
685 if (!lr->file || fno) {
687 find_line_range_by_func(&lf);
690 lf.lno_s = lr->start;
695 find_line_range_by_line(NULL, &lf);
700 pr_debug("path: %lx\n", (unsigned long)lr->path);