perf probe: Use libdw callback routines
[safe/jmp/linux-2.6] / tools / perf / util / probe-finder.c
1 /*
2  * probe-finder.c : C expression to kprobe event converter
3  *
4  * Written by Masami Hiramatsu <mhiramat@redhat.com>
5  *
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.
10  *
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.
15  *
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.
19  *
20  */
21
22 #include <sys/utsname.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #include <errno.h>
27 #include <stdio.h>
28 #include <unistd.h>
29 #include <getopt.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <stdarg.h>
33 #include <ctype.h>
34
35 #include "event.h"
36 #include "debug.h"
37 #include "util.h"
38 #include "probe-finder.h"
39
40
41 /*
42  * Generic dwarf analysis helpers
43  */
44
45 #define X86_32_MAX_REGS 8
46 const char *x86_32_regs_table[X86_32_MAX_REGS] = {
47         "%ax",
48         "%cx",
49         "%dx",
50         "%bx",
51         "$stack",       /* Stack address instead of %sp */
52         "%bp",
53         "%si",
54         "%di",
55 };
56
57 #define X86_64_MAX_REGS 16
58 const char *x86_64_regs_table[X86_64_MAX_REGS] = {
59         "%ax",
60         "%dx",
61         "%cx",
62         "%bx",
63         "%si",
64         "%di",
65         "%bp",
66         "%sp",
67         "%r8",
68         "%r9",
69         "%r10",
70         "%r11",
71         "%r12",
72         "%r13",
73         "%r14",
74         "%r15",
75 };
76
77 /* TODO: switching by dwarf address size */
78 #ifdef __x86_64__
79 #define ARCH_MAX_REGS X86_64_MAX_REGS
80 #define arch_regs_table x86_64_regs_table
81 #else
82 #define ARCH_MAX_REGS X86_32_MAX_REGS
83 #define arch_regs_table x86_32_regs_table
84 #endif
85
86 /* Return architecture dependent register string (for kprobe-tracer) */
87 static const char *get_arch_regstr(unsigned int n)
88 {
89         return (n <= ARCH_MAX_REGS) ? arch_regs_table[n] : NULL;
90 }
91
92 /*
93  * Compare the tail of two strings.
94  * Return 0 if whole of either string is same as another's tail part.
95  */
96 static int strtailcmp(const char *s1, const char *s2)
97 {
98         int i1 = strlen(s1);
99         int i2 = strlen(s2);
100         while (--i1 >= 0 && --i2 >= 0) {
101                 if (s1[i1] != s2[i2])
102                         return s1[i1] - s2[i2];
103         }
104         return 0;
105 }
106
107 /* Find the fileno of the target file. */
108 static int cu_find_fileno(Dwarf_Die *cu_die, const char *fname)
109 {
110         Dwarf_Files *files;
111         size_t nfiles, i;
112         const char *src;
113         int ret;
114
115         if (!fname)
116                 return -EINVAL;
117
118         ret = dwarf_getsrcfiles(cu_die, &files, &nfiles);
119         if (ret == 0) {
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?*/
124                                 break;
125                         }
126                 }
127                 if (ret)
128                         pr_debug("found fno: %d\n", ret);
129         }
130         return ret;
131 }
132
133 struct __addr_die_search_param {
134         Dwarf_Addr      addr;
135         Dwarf_Die       *die_mem;
136 };
137
138 static int __die_search_func_cb(Dwarf_Die *fn_die, void *data)
139 {
140         struct __addr_die_search_param *ad = data;
141
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;
146         }
147         return DWARF_CB_OK;
148 }
149
150 /* Search a real subprogram including this line, */
151 static Dwarf_Die *die_get_real_subprogram(Dwarf_Die *cu_die, Dwarf_Addr addr,
152                                           Dwarf_Die *die_mem)
153 {
154         struct __addr_die_search_param ad;
155         ad.addr = addr;
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))
159                 return NULL;
160         else
161                 return die_mem;
162 }
163
164 /* Compare diename and tname */
165 static bool die_compare_name(Dwarf_Die *dw_die, const char *tname)
166 {
167         const char *name;
168         name = dwarf_diename(dw_die);
169         DIE_IF(name == NULL);
170         return strcmp(tname, name);
171 }
172
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)
175 {
176         Dwarf_Addr epc;
177         int ret;
178
179         ret = dwarf_entrypc(dw_die, &epc);
180         DIE_IF(ret == -1);
181         return epc;
182 }
183
184 /* Get a variable die */
185 static Dwarf_Die *die_find_variable(Dwarf_Die *sp_die, const char *name,
186                                     Dwarf_Die *die_mem)
187 {
188         Dwarf_Die child_die;
189         int tag;
190         int ret;
191
192         ret = dwarf_child(sp_die, die_mem);
193         if (ret != 0)
194                 return NULL;
195
196         do {
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))
201                         return die_mem;
202
203                 if (die_find_variable(die_mem, name, &child_die)) {
204                         memcpy(die_mem, &child_die, sizeof(Dwarf_Die));
205                         return die_mem;
206                 }
207         } while (dwarf_siblingof(die_mem, die_mem) == 0);
208
209         return NULL;
210 }
211
212 /*
213  * Probe finder related functions
214  */
215
216 /* Show a location */
217 static void show_location(Dwarf_Op *op, struct probe_finder *pf)
218 {
219         unsigned int regn;
220         Dwarf_Word offs = 0;
221         int deref = 0, ret;
222         const char *regs;
223
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");
229                 deref = 1;
230                 offs = op->number;
231                 op = &pf->fb_ops[0];
232         }
233
234         if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) {
235                 regn = op->atom - DW_OP_breg0;
236                 offs += op->number;
237                 deref = 1;
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) {
241                 regn = op->number;
242                 offs += op->number2;
243                 deref = 1;
244         } else if (op->atom == DW_OP_regx) {
245                 regn = op->number;
246         } else
247                 die("DW_OP %d is not supported.", op->atom);
248
249         regs = get_arch_regstr(regn);
250         if (!regs)
251                 die("%u exceeds max register number.", regn);
252
253         if (deref)
254                 ret = snprintf(pf->buf, pf->len, " %s=+%ju(%s)",
255                                pf->var, (uintmax_t)offs, regs);
256         else
257                 ret = snprintf(pf->buf, pf->len, " %s=%s", pf->var, regs);
258         DIE_IF(ret < 0);
259         DIE_IF(ret >= pf->len);
260 }
261
262 /* Show a variables in kprobe event format */
263 static void show_variable(Dwarf_Die *vr_die, struct probe_finder *pf)
264 {
265         Dwarf_Attribute attr;
266         Dwarf_Op *expr;
267         size_t nexpr;
268         int ret;
269
270         if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL)
271                 goto error;
272         /* TODO: handle more than 1 exprs */
273         ret = dwarf_getlocation_addr(&attr, (pf->addr - pf->cu_base),
274                                      &expr, &nexpr, 1);
275         if (ret <= 0 || nexpr == 0)
276                 goto error;
277
278         show_location(expr, pf);
279         /* *expr will be cached in libdw. Don't free it. */
280         return ;
281 error:
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);
285 }
286
287 /* Find a variable in a subprogram die */
288 static void find_variable(Dwarf_Die *sp_die, struct probe_finder *pf)
289 {
290         int ret;
291         Dwarf_Die vr_die;
292
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);
297                 DIE_IF(ret < 0);
298                 DIE_IF(ret >= pf->len);
299                 return ;
300         }
301
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);
306
307         show_variable(&vr_die, pf);
308 }
309
310 /* Show a probe point to output buffer */
311 static void show_probe_point(Dwarf_Die *sp_die, struct probe_finder *pf)
312 {
313         struct probe_point *pp = pf->pp;
314         Dwarf_Addr eaddr;
315         Dwarf_Die die_mem;
316         const char *name;
317         char tmp[MAX_PROBE_BUFFER];
318         int ret, i, len;
319         Dwarf_Attribute fb_attr;
320         size_t nops;
321
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,
325                                                  pf->addr, &die_mem);
326                 if (!sp_die)
327                         die("Probe point is not found in subprograms.");
328         }
329
330         /* Output name of probe point */
331         name = dwarf_diename(sp_die);
332         if (name) {
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 */
337                 if (!pp->function) {
338                         pp->function = strdup(name);
339                         pp->offset = (size_t)(pf->addr - eaddr);
340                 }
341         } else {
342                 /* This function has no name. */
343                 ret = snprintf(tmp, MAX_PROBE_BUFFER, "0x%jx",
344                                (uintmax_t)pf->addr);
345                 if (!pp->function) {
346                         /* TODO: Use _stext */
347                         pp->function = strdup("");
348                         pp->offset = (size_t)pf->addr;
349                 }
350         }
351         DIE_IF(ret < 0);
352         DIE_IF(ret >= MAX_PROBE_BUFFER);
353         len = ret;
354         pr_debug("Probe point found: %s\n", tmp);
355
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)
361                 pf->fb_ops = NULL;
362
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];
367                 pf->buf = &tmp[len];
368                 pf->len = MAX_PROBE_BUFFER - len;
369                 find_variable(sp_die, pf);
370                 len += strlen(pf->buf);
371         }
372
373         /* *pf->fb_ops will be cached in libdw. Don't free it. */
374         pf->fb_ops = NULL;
375
376         pp->probes[pp->found] = strdup(tmp);
377         pp->found++;
378 }
379
380 /* Find probe point from its line number */
381 static void find_probe_point_by_line(struct probe_finder *pf)
382 {
383         Dwarf_Lines *lines;
384         Dwarf_Line *line;
385         size_t nlines, i;
386         Dwarf_Addr addr;
387         int lineno;
388         int ret;
389
390         ret = dwarf_getsrclines(&pf->cu_die, &lines, &nlines);
391         DIE_IF(ret != 0);
392
393         for (i = 0; i < nlines; i++) {
394                 line = dwarf_onesrcline(lines, i);
395                 dwarf_lineno(line, &lineno);
396                 if (lineno != pf->lno)
397                         continue;
398
399                 /* TODO: Get fileno from line, but how? */
400                 if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
401                         continue;
402
403                 ret = dwarf_lineaddr(line, &addr);
404                 DIE_IF(ret != 0);
405                 pr_debug("Probe line found: line[%d]:%d addr:0x%jx\n",
406                          (int)i, lineno, (uintmax_t)addr);
407                 pf->addr = addr;
408
409                 show_probe_point(NULL, pf);
410                 /* Continuing, because target line might be inlined. */
411         }
412 }
413
414 static int probe_point_inline_cb(Dwarf_Die *in_die, void *data)
415 {
416         struct probe_finder *pf = (struct probe_finder *)data;
417         struct probe_point *pp = pf->pp;
418
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);
423
424         show_probe_point(in_die, pf);
425         return DWARF_CB_OK;
426 }
427
428 /* Search function from function name */
429 static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
430 {
431         struct probe_finder *pf = (struct probe_finder *)data;
432         struct probe_point *pp = pf->pp;
433
434         /* Check tag and diename */
435         if (dwarf_tag(sp_die) != DW_TAG_subprogram ||
436             die_compare_name(sp_die, pp->function) != 0)
437                 return 0;
438
439         if (pp->line) { /* Function relative line */
440                 pf->fname = dwarf_decl_file(sp_die);
441                 dwarf_decl_line(sp_die, &pf->lno);
442                 pf->lno += pp->line;
443                 find_probe_point_by_line(pf);
444         } else if (!dwarf_func_inline(sp_die)) {
445                 /* Real function */
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);
450         } else
451                 /* Inlined function: search instances */
452                 dwarf_func_inline_instances(sp_die, probe_point_inline_cb, pf);
453
454         return 1; /* Exit; no same symbol in this CU. */
455 }
456
457 static void find_probe_point_by_func(struct probe_finder *pf)
458 {
459         dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, pf, 0);
460 }
461
462 /* Find a probe point */
463 int find_probe_point(int fd, struct probe_point *pp)
464 {
465         struct probe_finder pf = {.pp = pp};
466         int ret;
467         Dwarf_Off off, noff;
468         size_t cuhl;
469         Dwarf_Die *diep;
470         Dwarf *dbg;
471         int fno = 0;
472
473         dbg = dwarf_begin(fd, DWARF_C_READ);
474         if (!dbg)
475                 return -ENOENT;
476
477         pp->found = 0;
478         off = 0;
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);
483                 if (!diep)
484                         continue;
485
486                 /* Check if target file is included. */
487                 if (pp->file)
488                         fno = cu_find_fileno(&pf.cu_die, pp->file);
489                 else
490                         fno = 0;
491
492                 if (!pp->file || fno) {
493                         /* Save CU base address (for frame_base) */
494                         ret = dwarf_lowpc(&pf.cu_die, &pf.cu_base);
495                         if (ret != 0)
496                                 pf.cu_base = 0;
497                         if (pp->function)
498                                 find_probe_point_by_func(&pf);
499                         else {
500                                 pf.lno = pp->line;
501                                 find_probe_point_by_line(&pf);
502                         }
503                 }
504                 off = noff;
505         }
506         dwarf_end(dbg);
507
508         return pp->found;
509 }
510
511
512 static void line_range_add_line(struct line_range *lr, unsigned int line)
513 {
514         struct line_node *ln;
515         struct list_head *p;
516
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) {
520                         p = &ln->list;
521                         goto found;
522                 } else if (ln->line == line)    /* Already exist */
523                         return ;
524         }
525         /* List is empty, or the smallest entry */
526         p = &lr->line_list;
527 found:
528         pr_debug("Debug: add a line %u\n", line);
529         ln = zalloc(sizeof(struct line_node));
530         DIE_IF(ln == NULL);
531         ln->line = line;
532         INIT_LIST_HEAD(&ln->list);
533         list_add(&ln->list, p);
534 }
535
536 /* Find line range from its line number */
537 static void find_line_range_by_line(struct line_finder *lf)
538 {
539         Dwarf_Lines *lines;
540         Dwarf_Line *line;
541         size_t nlines, i;
542         Dwarf_Addr addr;
543         int lineno;
544         int ret;
545         const char *src;
546
547         INIT_LIST_HEAD(&lf->lr->line_list);
548         ret = dwarf_getsrclines(&lf->cu_die, &lines, &nlines);
549         DIE_IF(ret != 0);
550
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)
555                         continue;
556
557                 /* TODO: Get fileno from line, but how? */
558                 src = dwarf_linesrc(line, NULL, NULL);
559                 if (strtailcmp(src, lf->fname) != 0)
560                         continue;
561
562                 /* Filter line in the function address range */
563                 if (lf->addr_s && lf->addr_e) {
564                         ret = dwarf_lineaddr(line, &addr);
565                         DIE_IF(ret != 0);
566                         if (lf->addr_s > addr || lf->addr_e <= addr)
567                                 continue;
568                 }
569                 /* Copy real path */
570                 if (!lf->lr->path)
571                         lf->lr->path = strdup(src);
572                 line_range_add_line(lf->lr, (unsigned int)lineno);
573         }
574         /* Update status */
575         if (!list_empty(&lf->lr->line_list))
576                 lf->found = 1;
577         else {
578                 free(lf->lr->path);
579                 lf->lr->path = NULL;
580         }
581 }
582
583 /* Search function from function name */
584 static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
585 {
586         struct line_finder *lf = (struct line_finder *)data;
587         struct line_range *lr = lf->lr;
588         int ret;
589
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);
594                 if (ret == 0)
595                         ret = dwarf_lowpc(sp_die, &lf->addr_s);
596                 if (ret != 0) {
597                         lf->addr_s = 0;
598                         lf->addr_e = 0;
599                 }
600
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;
605                 if (!lr->end)
606                         lf->lno_e = INT_MAX;
607                 else
608                         lf->lno_e = lr->offset + lr->end;
609                 lr->start = lf->lno_s;
610                 lr->end = lf->lno_e;
611                 find_line_range_by_line(lf);
612                 return 1;
613         }
614         return 0;
615 }
616
617 static void find_line_range_by_func(struct line_finder *lf)
618 {
619         dwarf_getfuncs(&lf->cu_die, line_range_search_cb, lf, 0);
620 }
621
622 int find_line_range(int fd, struct line_range *lr)
623 {
624         struct line_finder lf = {.lr = lr, .found = 0};
625         int ret;
626         Dwarf_Off off = 0, noff;
627         size_t cuhl;
628         Dwarf_Die *diep;
629         Dwarf *dbg;
630         int fno;
631
632         dbg = dwarf_begin(fd, DWARF_C_READ);
633         if (!dbg)
634                 return -ENOENT;
635
636         /* Loop on CUs (Compilation Unit) */
637         while (!lf.found) {
638                 ret = dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL);
639                 if (ret != 0)
640                         break;
641
642                 /* Get the DIE(Debugging Information Entry) of this CU */
643                 diep = dwarf_offdie(dbg, off + cuhl, &lf.cu_die);
644                 if (!diep)
645                         continue;
646
647                 /* Check if target file is included. */
648                 if (lr->file)
649                         fno = cu_find_fileno(&lf.cu_die, lr->file);
650                 else
651                         fno = 0;
652
653                 if (!lr->file || fno) {
654                         if (lr->function)
655                                 find_line_range_by_func(&lf);
656                         else {
657                                 lf.fname = lr->file;
658                                 lf.lno_s = lr->start;
659                                 if (!lr->end)
660                                         lf.lno_e = INT_MAX;
661                                 else
662                                         lf.lno_e = lr->end;
663                                 find_line_range_by_line(&lf);
664                         }
665                 }
666                 off = noff;
667         }
668         pr_debug("path: %lx\n", (unsigned long)lr->path);
669         dwarf_end(dbg);
670         return lf.found;
671 }
672