perf probe: Check function address range strictly in line finder
[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 /* Similar to dwarf_getfuncs, but returns inlined_subroutine if exists. */
165 static Dwarf_Die *die_get_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
166                                      Dwarf_Die *die_mem)
167 {
168         Dwarf_Die child_die;
169         int ret;
170
171         ret = dwarf_child(sp_die, die_mem);
172         if (ret != 0)
173                 return NULL;
174
175         do {
176                 if (dwarf_tag(die_mem) == DW_TAG_inlined_subroutine &&
177                     dwarf_haspc(die_mem, addr))
178                         return die_mem;
179
180                 if (die_get_inlinefunc(die_mem, addr, &child_die)) {
181                         memcpy(die_mem, &child_die, sizeof(Dwarf_Die));
182                         return die_mem;
183                 }
184         } while (dwarf_siblingof(die_mem, die_mem) == 0);
185
186         return NULL;
187 }
188
189 /* Compare diename and tname */
190 static bool die_compare_name(Dwarf_Die *dw_die, const char *tname)
191 {
192         const char *name;
193         name = dwarf_diename(dw_die);
194         DIE_IF(name == NULL);
195         return strcmp(tname, name);
196 }
197
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)
200 {
201         Dwarf_Addr epc;
202         int ret;
203
204         ret = dwarf_entrypc(dw_die, &epc);
205         DIE_IF(ret == -1);
206         return epc;
207 }
208
209 /* Get a variable die */
210 static Dwarf_Die *die_find_variable(Dwarf_Die *sp_die, const char *name,
211                                     Dwarf_Die *die_mem)
212 {
213         Dwarf_Die child_die;
214         int tag;
215         int ret;
216
217         ret = dwarf_child(sp_die, die_mem);
218         if (ret != 0)
219                 return NULL;
220
221         do {
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))
226                         return die_mem;
227
228                 if (die_find_variable(die_mem, name, &child_die)) {
229                         memcpy(die_mem, &child_die, sizeof(Dwarf_Die));
230                         return die_mem;
231                 }
232         } while (dwarf_siblingof(die_mem, die_mem) == 0);
233
234         return NULL;
235 }
236
237 /*
238  * Probe finder related functions
239  */
240
241 /* Show a location */
242 static void show_location(Dwarf_Op *op, struct probe_finder *pf)
243 {
244         unsigned int regn;
245         Dwarf_Word offs = 0;
246         int deref = 0, ret;
247         const char *regs;
248
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");
254                 deref = 1;
255                 offs = op->number;
256                 op = &pf->fb_ops[0];
257         }
258
259         if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) {
260                 regn = op->atom - DW_OP_breg0;
261                 offs += op->number;
262                 deref = 1;
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) {
266                 regn = op->number;
267                 offs += op->number2;
268                 deref = 1;
269         } else if (op->atom == DW_OP_regx) {
270                 regn = op->number;
271         } else
272                 die("DW_OP %d is not supported.", op->atom);
273
274         regs = get_arch_regstr(regn);
275         if (!regs)
276                 die("%u exceeds max register number.", regn);
277
278         if (deref)
279                 ret = snprintf(pf->buf, pf->len, " %s=+%ju(%s)",
280                                pf->var, (uintmax_t)offs, regs);
281         else
282                 ret = snprintf(pf->buf, pf->len, " %s=%s", pf->var, regs);
283         DIE_IF(ret < 0);
284         DIE_IF(ret >= pf->len);
285 }
286
287 /* Show a variables in kprobe event format */
288 static void show_variable(Dwarf_Die *vr_die, struct probe_finder *pf)
289 {
290         Dwarf_Attribute attr;
291         Dwarf_Op *expr;
292         size_t nexpr;
293         int ret;
294
295         if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL)
296                 goto error;
297         /* TODO: handle more than 1 exprs */
298         ret = dwarf_getlocation_addr(&attr, (pf->addr - pf->cu_base),
299                                      &expr, &nexpr, 1);
300         if (ret <= 0 || nexpr == 0)
301                 goto error;
302
303         show_location(expr, pf);
304         /* *expr will be cached in libdw. Don't free it. */
305         return ;
306 error:
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);
310 }
311
312 /* Find a variable in a subprogram die */
313 static void find_variable(Dwarf_Die *sp_die, struct probe_finder *pf)
314 {
315         int ret;
316         Dwarf_Die vr_die;
317
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);
322                 DIE_IF(ret < 0);
323                 DIE_IF(ret >= pf->len);
324                 return ;
325         }
326
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);
331
332         show_variable(&vr_die, pf);
333 }
334
335 /* Show a probe point to output buffer */
336 static void show_probe_point(Dwarf_Die *sp_die, struct probe_finder *pf)
337 {
338         struct probe_point *pp = pf->pp;
339         Dwarf_Addr eaddr;
340         Dwarf_Die die_mem;
341         const char *name;
342         char tmp[MAX_PROBE_BUFFER];
343         int ret, i, len;
344         Dwarf_Attribute fb_attr;
345         size_t nops;
346
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,
350                                                  pf->addr, &die_mem);
351                 if (!sp_die)
352                         die("Probe point is not found in subprograms.");
353         }
354
355         /* Output name of probe point */
356         name = dwarf_diename(sp_die);
357         if (name) {
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 */
362                 if (!pp->function) {
363                         pp->function = strdup(name);
364                         pp->offset = (size_t)(pf->addr - eaddr);
365                 }
366         } else {
367                 /* This function has no name. */
368                 ret = snprintf(tmp, MAX_PROBE_BUFFER, "0x%jx",
369                                (uintmax_t)pf->addr);
370                 if (!pp->function) {
371                         /* TODO: Use _stext */
372                         pp->function = strdup("");
373                         pp->offset = (size_t)pf->addr;
374                 }
375         }
376         DIE_IF(ret < 0);
377         DIE_IF(ret >= MAX_PROBE_BUFFER);
378         len = ret;
379         pr_debug("Probe point found: %s\n", tmp);
380
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)
386                 pf->fb_ops = NULL;
387
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];
392                 pf->buf = &tmp[len];
393                 pf->len = MAX_PROBE_BUFFER - len;
394                 find_variable(sp_die, pf);
395                 len += strlen(pf->buf);
396         }
397
398         /* *pf->fb_ops will be cached in libdw. Don't free it. */
399         pf->fb_ops = NULL;
400
401         pp->probes[pp->found] = strdup(tmp);
402         pp->found++;
403 }
404
405 /* Find probe point from its line number */
406 static void find_probe_point_by_line(struct probe_finder *pf)
407 {
408         Dwarf_Lines *lines;
409         Dwarf_Line *line;
410         size_t nlines, i;
411         Dwarf_Addr addr;
412         int lineno;
413         int ret;
414
415         ret = dwarf_getsrclines(&pf->cu_die, &lines, &nlines);
416         DIE_IF(ret != 0);
417
418         for (i = 0; i < nlines; i++) {
419                 line = dwarf_onesrcline(lines, i);
420                 dwarf_lineno(line, &lineno);
421                 if (lineno != pf->lno)
422                         continue;
423
424                 /* TODO: Get fileno from line, but how? */
425                 if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
426                         continue;
427
428                 ret = dwarf_lineaddr(line, &addr);
429                 DIE_IF(ret != 0);
430                 pr_debug("Probe line found: line[%d]:%d addr:0x%jx\n",
431                          (int)i, lineno, (uintmax_t)addr);
432                 pf->addr = addr;
433
434                 show_probe_point(NULL, pf);
435                 /* Continuing, because target line might be inlined. */
436         }
437 }
438
439 static int probe_point_inline_cb(Dwarf_Die *in_die, void *data)
440 {
441         struct probe_finder *pf = (struct probe_finder *)data;
442         struct probe_point *pp = pf->pp;
443
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);
448
449         show_probe_point(in_die, pf);
450         return DWARF_CB_OK;
451 }
452
453 /* Search function from function name */
454 static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
455 {
456         struct probe_finder *pf = (struct probe_finder *)data;
457         struct probe_point *pp = pf->pp;
458
459         /* Check tag and diename */
460         if (dwarf_tag(sp_die) != DW_TAG_subprogram ||
461             die_compare_name(sp_die, pp->function) != 0)
462                 return 0;
463
464         if (pp->line) { /* Function relative line */
465                 pf->fname = dwarf_decl_file(sp_die);
466                 dwarf_decl_line(sp_die, &pf->lno);
467                 pf->lno += pp->line;
468                 find_probe_point_by_line(pf);
469         } else if (!dwarf_func_inline(sp_die)) {
470                 /* Real function */
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);
475         } else
476                 /* Inlined function: search instances */
477                 dwarf_func_inline_instances(sp_die, probe_point_inline_cb, pf);
478
479         return 1; /* Exit; no same symbol in this CU. */
480 }
481
482 static void find_probe_point_by_func(struct probe_finder *pf)
483 {
484         dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, pf, 0);
485 }
486
487 /* Find a probe point */
488 int find_probe_point(int fd, struct probe_point *pp)
489 {
490         struct probe_finder pf = {.pp = pp};
491         int ret;
492         Dwarf_Off off, noff;
493         size_t cuhl;
494         Dwarf_Die *diep;
495         Dwarf *dbg;
496         int fno = 0;
497
498         dbg = dwarf_begin(fd, DWARF_C_READ);
499         if (!dbg)
500                 return -ENOENT;
501
502         pp->found = 0;
503         off = 0;
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);
508                 if (!diep)
509                         continue;
510
511                 /* Check if target file is included. */
512                 if (pp->file)
513                         fno = cu_find_fileno(&pf.cu_die, pp->file);
514                 else
515                         fno = 0;
516
517                 if (!pp->file || fno) {
518                         /* Save CU base address (for frame_base) */
519                         ret = dwarf_lowpc(&pf.cu_die, &pf.cu_base);
520                         if (ret != 0)
521                                 pf.cu_base = 0;
522                         if (pp->function)
523                                 find_probe_point_by_func(&pf);
524                         else {
525                                 pf.lno = pp->line;
526                                 find_probe_point_by_line(&pf);
527                         }
528                 }
529                 off = noff;
530         }
531         dwarf_end(dbg);
532
533         return pp->found;
534 }
535
536
537 static void line_range_add_line(struct line_range *lr, unsigned int line)
538 {
539         struct line_node *ln;
540         struct list_head *p;
541
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) {
545                         p = &ln->list;
546                         goto found;
547                 } else if (ln->line == line)    /* Already exist */
548                         return ;
549         }
550         /* List is empty, or the smallest entry */
551         p = &lr->line_list;
552 found:
553         pr_debug("Debug: add a line %u\n", line);
554         ln = zalloc(sizeof(struct line_node));
555         DIE_IF(ln == NULL);
556         ln->line = line;
557         INIT_LIST_HEAD(&ln->list);
558         list_add(&ln->list, p);
559 }
560
561 /* Find line range from its line number */
562 static void find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
563 {
564         Dwarf_Lines *lines;
565         Dwarf_Line *line;
566         size_t nlines, i;
567         Dwarf_Addr addr;
568         int lineno;
569         int ret;
570         const char *src;
571         Dwarf_Die die_mem;
572
573         INIT_LIST_HEAD(&lf->lr->line_list);
574         ret = dwarf_getsrclines(&lf->cu_die, &lines, &nlines);
575         DIE_IF(ret != 0);
576
577         for (i = 0; i < nlines; i++) {
578                 line = dwarf_onesrcline(lines, i);
579                 ret = dwarf_lineno(line, &lineno);
580                 DIE_IF(ret != 0);
581                 if (lf->lno_s > lineno || lf->lno_e < lineno)
582                         continue;
583
584                 if (sp_die) {
585                         /* Address filtering 1: does sp_die include addr? */
586                         ret = dwarf_lineaddr(line, &addr);
587                         DIE_IF(ret != 0);
588                         if (!dwarf_haspc(sp_die, addr))
589                                 continue;
590
591                         /* Address filtering 2: No child include addr? */
592                         if (die_get_inlinefunc(sp_die, addr, &die_mem))
593                                 continue;
594                 }
595
596                 /* TODO: Get fileno from line, but how? */
597                 src = dwarf_linesrc(line, NULL, NULL);
598                 if (strtailcmp(src, lf->fname) != 0)
599                         continue;
600
601                 /* Copy real path */
602                 if (!lf->lr->path)
603                         lf->lr->path = strdup(src);
604                 line_range_add_line(lf->lr, (unsigned int)lineno);
605         }
606         /* Update status */
607         if (!list_empty(&lf->lr->line_list))
608                 lf->found = 1;
609         else {
610                 free(lf->lr->path);
611                 lf->lr->path = NULL;
612         }
613 }
614
615 static int line_range_inline_cb(Dwarf_Die *in_die, void *data)
616 {
617         find_line_range_by_line(in_die, (struct line_finder *)data);
618         return DWARF_CB_ABORT;  /* No need to find other instances */
619 }
620
621 /* Search function from function name */
622 static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
623 {
624         struct line_finder *lf = (struct line_finder *)data;
625         struct line_range *lr = lf->lr;
626
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;
633                 if (!lr->end)
634                         lf->lno_e = INT_MAX;
635                 else
636                         lf->lno_e = lr->offset + lr->end;
637                 lr->start = lf->lno_s;
638                 lr->end = lf->lno_e;
639                 if (dwarf_func_inline(sp_die))
640                         dwarf_func_inline_instances(sp_die,
641                                                     line_range_inline_cb, lf);
642                 else
643                         find_line_range_by_line(sp_die, lf);
644                 return 1;
645         }
646         return 0;
647 }
648
649 static void find_line_range_by_func(struct line_finder *lf)
650 {
651         dwarf_getfuncs(&lf->cu_die, line_range_search_cb, lf, 0);
652 }
653
654 int find_line_range(int fd, struct line_range *lr)
655 {
656         struct line_finder lf = {.lr = lr, .found = 0};
657         int ret;
658         Dwarf_Off off = 0, noff;
659         size_t cuhl;
660         Dwarf_Die *diep;
661         Dwarf *dbg;
662         int fno;
663
664         dbg = dwarf_begin(fd, DWARF_C_READ);
665         if (!dbg)
666                 return -ENOENT;
667
668         /* Loop on CUs (Compilation Unit) */
669         while (!lf.found) {
670                 ret = dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL);
671                 if (ret != 0)
672                         break;
673
674                 /* Get the DIE(Debugging Information Entry) of this CU */
675                 diep = dwarf_offdie(dbg, off + cuhl, &lf.cu_die);
676                 if (!diep)
677                         continue;
678
679                 /* Check if target file is included. */
680                 if (lr->file)
681                         fno = cu_find_fileno(&lf.cu_die, lr->file);
682                 else
683                         fno = 0;
684
685                 if (!lr->file || fno) {
686                         if (lr->function)
687                                 find_line_range_by_func(&lf);
688                         else {
689                                 lf.fname = lr->file;
690                                 lf.lno_s = lr->start;
691                                 if (!lr->end)
692                                         lf.lno_e = INT_MAX;
693                                 else
694                                         lf.lno_e = lr->end;
695                                 find_line_range_by_line(NULL, &lf);
696                         }
697                 }
698                 off = noff;
699         }
700         pr_debug("path: %lx\n", (unsigned long)lr->path);
701         dwarf_end(dbg);
702         return lf.found;
703 }
704