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 /* Compare diename and tname */
165 static bool die_compare_name(Dwarf_Die *dw_die, const char *tname)
168 name = dwarf_diename(dw_die);
169 DIE_IF(name == NULL);
170 return strcmp(tname, name);
173 /* Get entry pc(or low pc, 1st entry of ranges) of the die */
174 static Dwarf_Addr die_get_entrypc(Dwarf_Die *dw_die)
179 ret = dwarf_entrypc(dw_die, &epc);
184 /* Get a variable die */
185 static Dwarf_Die *die_find_variable(Dwarf_Die *sp_die, const char *name,
192 ret = dwarf_child(sp_die, die_mem);
197 tag = dwarf_tag(die_mem);
198 if ((tag == DW_TAG_formal_parameter ||
199 tag == DW_TAG_variable) &&
200 (die_compare_name(die_mem, name) == 0))
203 if (die_find_variable(die_mem, name, &child_die)) {
204 memcpy(die_mem, &child_die, sizeof(Dwarf_Die));
207 } while (dwarf_siblingof(die_mem, die_mem) == 0);
213 * Probe finder related functions
216 /* Show a location */
217 static void show_location(Dwarf_Op *op, struct probe_finder *pf)
224 /* TODO: support CFA */
225 /* If this is based on frame buffer, set the offset */
226 if (op->atom == DW_OP_fbreg) {
227 if (pf->fb_ops == NULL)
228 die("The attribute of frame base is not supported.\n");
234 if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) {
235 regn = op->atom - DW_OP_breg0;
238 } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) {
239 regn = op->atom - DW_OP_reg0;
240 } else if (op->atom == DW_OP_bregx) {
244 } else if (op->atom == DW_OP_regx) {
247 die("DW_OP %d is not supported.", op->atom);
249 regs = get_arch_regstr(regn);
251 die("%u exceeds max register number.", regn);
254 ret = snprintf(pf->buf, pf->len, " %s=+%ju(%s)",
255 pf->var, (uintmax_t)offs, regs);
257 ret = snprintf(pf->buf, pf->len, " %s=%s", pf->var, regs);
259 DIE_IF(ret >= pf->len);
262 /* Show a variables in kprobe event format */
263 static void show_variable(Dwarf_Die *vr_die, struct probe_finder *pf)
265 Dwarf_Attribute attr;
270 if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL)
272 /* TODO: handle more than 1 exprs */
273 ret = dwarf_getlocation_addr(&attr, (pf->addr - pf->cu_base),
275 if (ret <= 0 || nexpr == 0)
278 show_location(expr, pf);
279 /* *expr will be cached in libdw. Don't free it. */
282 /* TODO: Support const_value */
283 die("Failed to find the location of %s at this address.\n"
284 " Perhaps, it has been optimized out.", pf->var);
287 /* Find a variable in a subprogram die */
288 static void find_variable(Dwarf_Die *sp_die, struct probe_finder *pf)
293 /* TODO: Support struct members and arrays */
294 if (!is_c_varname(pf->var)) {
295 /* Output raw parameters */
296 ret = snprintf(pf->buf, pf->len, " %s", pf->var);
298 DIE_IF(ret >= pf->len);
302 pr_debug("Searching '%s' variable in context.\n", pf->var);
303 /* Search child die for local variables and parameters. */
304 if (!die_find_variable(sp_die, pf->var, &vr_die))
305 die("Failed to find '%s' in this function.", pf->var);
307 show_variable(&vr_die, pf);
310 /* Show a probe point to output buffer */
311 static void show_probe_point(Dwarf_Die *sp_die, struct probe_finder *pf)
313 struct probe_point *pp = pf->pp;
317 char tmp[MAX_PROBE_BUFFER];
319 Dwarf_Attribute fb_attr;
322 /* If no real subprogram, find a real one */
323 if (!sp_die || dwarf_tag(sp_die) != DW_TAG_subprogram) {
324 sp_die = die_get_real_subprogram(&pf->cu_die,
327 die("Probe point is not found in subprograms.");
330 /* Output name of probe point */
331 name = dwarf_diename(sp_die);
333 dwarf_entrypc(sp_die, &eaddr);
334 ret = snprintf(tmp, MAX_PROBE_BUFFER, "%s+%lu", name,
335 (unsigned long)(pf->addr - eaddr));
336 /* Copy the function name if possible */
338 pp->function = strdup(name);
339 pp->offset = (size_t)(pf->addr - eaddr);
342 /* This function has no name. */
343 ret = snprintf(tmp, MAX_PROBE_BUFFER, "0x%jx",
344 (uintmax_t)pf->addr);
346 /* TODO: Use _stext */
347 pp->function = strdup("");
348 pp->offset = (size_t)pf->addr;
352 DIE_IF(ret >= MAX_PROBE_BUFFER);
354 pr_debug("Probe point found: %s\n", tmp);
356 /* Get the frame base attribute/ops */
357 dwarf_attr(sp_die, DW_AT_frame_base, &fb_attr);
358 ret = dwarf_getlocation_addr(&fb_attr, (pf->addr - pf->cu_base),
359 &pf->fb_ops, &nops, 1);
360 if (ret <= 0 || nops == 0)
363 /* Find each argument */
364 /* TODO: use dwarf_cfi_addrframe */
365 for (i = 0; i < pp->nr_args; i++) {
366 pf->var = pp->args[i];
368 pf->len = MAX_PROBE_BUFFER - len;
369 find_variable(sp_die, pf);
370 len += strlen(pf->buf);
373 /* *pf->fb_ops will be cached in libdw. Don't free it. */
376 pp->probes[pp->found] = strdup(tmp);
380 /* Find probe point from its line number */
381 static void find_probe_point_by_line(struct probe_finder *pf)
390 ret = dwarf_getsrclines(&pf->cu_die, &lines, &nlines);
393 for (i = 0; i < nlines; i++) {
394 line = dwarf_onesrcline(lines, i);
395 dwarf_lineno(line, &lineno);
396 if (lineno != pf->lno)
399 /* TODO: Get fileno from line, but how? */
400 if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
403 ret = dwarf_lineaddr(line, &addr);
405 pr_debug("Probe line found: line[%d]:%d addr:0x%jx\n",
406 (int)i, lineno, (uintmax_t)addr);
409 show_probe_point(NULL, pf);
410 /* Continuing, because target line might be inlined. */
414 static int probe_point_inline_cb(Dwarf_Die *in_die, void *data)
416 struct probe_finder *pf = (struct probe_finder *)data;
417 struct probe_point *pp = pf->pp;
419 /* Get probe address */
420 pf->addr = die_get_entrypc(in_die);
421 pf->addr += pp->offset;
422 pr_debug("found inline addr: 0x%jx\n", (uintmax_t)pf->addr);
424 show_probe_point(in_die, pf);
428 /* Search function from function name */
429 static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
431 struct probe_finder *pf = (struct probe_finder *)data;
432 struct probe_point *pp = pf->pp;
434 /* Check tag and diename */
435 if (dwarf_tag(sp_die) != DW_TAG_subprogram ||
436 die_compare_name(sp_die, pp->function) != 0)
439 if (pp->line) { /* Function relative line */
440 pf->fname = dwarf_decl_file(sp_die);
441 dwarf_decl_line(sp_die, &pf->lno);
443 find_probe_point_by_line(pf);
444 } else if (!dwarf_func_inline(sp_die)) {
446 pf->addr = die_get_entrypc(sp_die);
447 pf->addr += pp->offset;
448 /* TODO: Check the address in this function */
449 show_probe_point(sp_die, pf);
451 /* Inlined function: search instances */
452 dwarf_func_inline_instances(sp_die, probe_point_inline_cb, pf);
454 return 1; /* Exit; no same symbol in this CU. */
457 static void find_probe_point_by_func(struct probe_finder *pf)
459 dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, pf, 0);
462 /* Find a probe point */
463 int find_probe_point(int fd, struct probe_point *pp)
465 struct probe_finder pf = {.pp = pp};
473 dbg = dwarf_begin(fd, DWARF_C_READ);
479 /* Loop on CUs (Compilation Unit) */
480 while (!dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL)) {
481 /* Get the DIE(Debugging Information Entry) of this CU */
482 diep = dwarf_offdie(dbg, off + cuhl, &pf.cu_die);
486 /* Check if target file is included. */
488 fno = cu_find_fileno(&pf.cu_die, pp->file);
492 if (!pp->file || fno) {
493 /* Save CU base address (for frame_base) */
494 ret = dwarf_lowpc(&pf.cu_die, &pf.cu_base);
498 find_probe_point_by_func(&pf);
501 find_probe_point_by_line(&pf);
512 static void line_range_add_line(struct line_range *lr, unsigned int line)
514 struct line_node *ln;
517 /* Reverse search, because new line will be the last one */
518 list_for_each_entry_reverse(ln, &lr->line_list, list) {
519 if (ln->line < line) {
522 } else if (ln->line == line) /* Already exist */
525 /* List is empty, or the smallest entry */
528 pr_debug("Debug: add a line %u\n", line);
529 ln = zalloc(sizeof(struct line_node));
532 INIT_LIST_HEAD(&ln->list);
533 list_add(&ln->list, p);
536 /* Find line range from its line number */
537 static void find_line_range_by_line(struct line_finder *lf)
547 INIT_LIST_HEAD(&lf->lr->line_list);
548 ret = dwarf_getsrclines(&lf->cu_die, &lines, &nlines);
551 for (i = 0; i < nlines; i++) {
552 line = dwarf_onesrcline(lines, i);
553 dwarf_lineno(line, &lineno);
554 if (lf->lno_s > lineno || lf->lno_e < lineno)
557 /* TODO: Get fileno from line, but how? */
558 src = dwarf_linesrc(line, NULL, NULL);
559 if (strtailcmp(src, lf->fname) != 0)
562 /* Filter line in the function address range */
563 if (lf->addr_s && lf->addr_e) {
564 ret = dwarf_lineaddr(line, &addr);
566 if (lf->addr_s > addr || lf->addr_e <= addr)
571 lf->lr->path = strdup(src);
572 line_range_add_line(lf->lr, (unsigned int)lineno);
575 if (!list_empty(&lf->lr->line_list))
583 /* Search function from function name */
584 static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
586 struct line_finder *lf = (struct line_finder *)data;
587 struct line_range *lr = lf->lr;
590 if (dwarf_tag(sp_die) == DW_TAG_subprogram &&
591 die_compare_name(sp_die, lr->function) == 0) {
592 /* Get the address range of this function */
593 ret = dwarf_highpc(sp_die, &lf->addr_e);
595 ret = dwarf_lowpc(sp_die, &lf->addr_s);
601 lf->fname = dwarf_decl_file(sp_die);
602 dwarf_decl_line(sp_die, &lr->offset);
603 pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
604 lf->lno_s = lr->offset + lr->start;
608 lf->lno_e = lr->offset + lr->end;
609 lr->start = lf->lno_s;
611 find_line_range_by_line(lf);
617 static void find_line_range_by_func(struct line_finder *lf)
619 dwarf_getfuncs(&lf->cu_die, line_range_search_cb, lf, 0);
622 int find_line_range(int fd, struct line_range *lr)
624 struct line_finder lf = {.lr = lr, .found = 0};
626 Dwarf_Off off = 0, noff;
632 dbg = dwarf_begin(fd, DWARF_C_READ);
636 /* Loop on CUs (Compilation Unit) */
638 ret = dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL);
642 /* Get the DIE(Debugging Information Entry) of this CU */
643 diep = dwarf_offdie(dbg, off + cuhl, &lf.cu_die);
647 /* Check if target file is included. */
649 fno = cu_find_fileno(&lf.cu_die, lr->file);
653 if (!lr->file || fno) {
655 find_line_range_by_func(&lf);
658 lf.lno_s = lr->start;
663 find_line_range_by_line(&lf);
668 pr_debug("path: %lx\n", (unsigned long)lr->path);