perf probe: Fix to use symtab only if no debuginfo
[linux-2.6.git] / 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 #include <dwarf-regs.h>
35
36 #include "string.h"
37 #include "event.h"
38 #include "debug.h"
39 #include "util.h"
40 #include "probe-finder.h"
41
42 /* Kprobe tracer basic type is up to u64 */
43 #define MAX_BASIC_TYPE_BITS     64
44
45 /*
46  * Compare the tail of two strings.
47  * Return 0 if whole of either string is same as another's tail part.
48  */
49 static int strtailcmp(const char *s1, const char *s2)
50 {
51         int i1 = strlen(s1);
52         int i2 = strlen(s2);
53         while (--i1 >= 0 && --i2 >= 0) {
54                 if (s1[i1] != s2[i2])
55                         return s1[i1] - s2[i2];
56         }
57         return 0;
58 }
59
60 /* Line number list operations */
61
62 /* Add a line to line number list */
63 static int line_list__add_line(struct list_head *head, int line)
64 {
65         struct line_node *ln;
66         struct list_head *p;
67
68         /* Reverse search, because new line will be the last one */
69         list_for_each_entry_reverse(ln, head, list) {
70                 if (ln->line < line) {
71                         p = &ln->list;
72                         goto found;
73                 } else if (ln->line == line)    /* Already exist */
74                         return 1;
75         }
76         /* List is empty, or the smallest entry */
77         p = head;
78 found:
79         pr_debug("line list: add a line %u\n", line);
80         ln = zalloc(sizeof(struct line_node));
81         if (ln == NULL)
82                 return -ENOMEM;
83         ln->line = line;
84         INIT_LIST_HEAD(&ln->list);
85         list_add(&ln->list, p);
86         return 0;
87 }
88
89 /* Check if the line in line number list */
90 static int line_list__has_line(struct list_head *head, int line)
91 {
92         struct line_node *ln;
93
94         /* Reverse search, because new line will be the last one */
95         list_for_each_entry(ln, head, list)
96                 if (ln->line == line)
97                         return 1;
98
99         return 0;
100 }
101
102 /* Init line number list */
103 static void line_list__init(struct list_head *head)
104 {
105         INIT_LIST_HEAD(head);
106 }
107
108 /* Free line number list */
109 static void line_list__free(struct list_head *head)
110 {
111         struct line_node *ln;
112         while (!list_empty(head)) {
113                 ln = list_first_entry(head, struct line_node, list);
114                 list_del(&ln->list);
115                 free(ln);
116         }
117 }
118
119 /* Dwarf wrappers */
120
121 /* Find the realpath of the target file. */
122 static const char *cu_find_realpath(Dwarf_Die *cu_die, const char *fname)
123 {
124         Dwarf_Files *files;
125         size_t nfiles, i;
126         const char *src = NULL;
127         int ret;
128
129         if (!fname)
130                 return NULL;
131
132         ret = dwarf_getsrcfiles(cu_die, &files, &nfiles);
133         if (ret != 0)
134                 return NULL;
135
136         for (i = 0; i < nfiles; i++) {
137                 src = dwarf_filesrc(files, i, NULL, NULL);
138                 if (strtailcmp(src, fname) == 0)
139                         break;
140         }
141         if (i == nfiles)
142                 return NULL;
143         return src;
144 }
145
146 /* Compare diename and tname */
147 static bool die_compare_name(Dwarf_Die *dw_die, const char *tname)
148 {
149         const char *name;
150         name = dwarf_diename(dw_die);
151         return name ? strcmp(tname, name) : -1;
152 }
153
154 /* Get type die, but skip qualifiers and typedef */
155 static Dwarf_Die *die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
156 {
157         Dwarf_Attribute attr;
158         int tag;
159
160         do {
161                 if (dwarf_attr(vr_die, DW_AT_type, &attr) == NULL ||
162                     dwarf_formref_die(&attr, die_mem) == NULL)
163                         return NULL;
164
165                 tag = dwarf_tag(die_mem);
166                 vr_die = die_mem;
167         } while (tag == DW_TAG_const_type ||
168                  tag == DW_TAG_restrict_type ||
169                  tag == DW_TAG_volatile_type ||
170                  tag == DW_TAG_shared_type ||
171                  tag == DW_TAG_typedef);
172
173         return die_mem;
174 }
175
176 static bool die_is_signed_type(Dwarf_Die *tp_die)
177 {
178         Dwarf_Attribute attr;
179         Dwarf_Word ret;
180
181         if (dwarf_attr(tp_die, DW_AT_encoding, &attr) == NULL ||
182             dwarf_formudata(&attr, &ret) != 0)
183                 return false;
184
185         return (ret == DW_ATE_signed_char || ret == DW_ATE_signed ||
186                 ret == DW_ATE_signed_fixed);
187 }
188
189 static int die_get_byte_size(Dwarf_Die *tp_die)
190 {
191         Dwarf_Attribute attr;
192         Dwarf_Word ret;
193
194         if (dwarf_attr(tp_die, DW_AT_byte_size, &attr) == NULL ||
195             dwarf_formudata(&attr, &ret) != 0)
196                 return 0;
197
198         return (int)ret;
199 }
200
201 /* Get data_member_location offset */
202 static int die_get_data_member_location(Dwarf_Die *mb_die, Dwarf_Word *offs)
203 {
204         Dwarf_Attribute attr;
205         Dwarf_Op *expr;
206         size_t nexpr;
207         int ret;
208
209         if (dwarf_attr(mb_die, DW_AT_data_member_location, &attr) == NULL)
210                 return -ENOENT;
211
212         if (dwarf_formudata(&attr, offs) != 0) {
213                 /* DW_AT_data_member_location should be DW_OP_plus_uconst */
214                 ret = dwarf_getlocation(&attr, &expr, &nexpr);
215                 if (ret < 0 || nexpr == 0)
216                         return -ENOENT;
217
218                 if (expr[0].atom != DW_OP_plus_uconst || nexpr != 1) {
219                         pr_debug("Unable to get offset:Unexpected OP %x (%zd)\n",
220                                  expr[0].atom, nexpr);
221                         return -ENOTSUP;
222                 }
223                 *offs = (Dwarf_Word)expr[0].number;
224         }
225         return 0;
226 }
227
228 /* Return values for die_find callbacks */
229 enum {
230         DIE_FIND_CB_FOUND = 0,          /* End of Search */
231         DIE_FIND_CB_CHILD = 1,          /* Search only children */
232         DIE_FIND_CB_SIBLING = 2,        /* Search only siblings */
233         DIE_FIND_CB_CONTINUE = 3,       /* Search children and siblings */
234 };
235
236 /* Search a child die */
237 static Dwarf_Die *die_find_child(Dwarf_Die *rt_die,
238                                  int (*callback)(Dwarf_Die *, void *),
239                                  void *data, Dwarf_Die *die_mem)
240 {
241         Dwarf_Die child_die;
242         int ret;
243
244         ret = dwarf_child(rt_die, die_mem);
245         if (ret != 0)
246                 return NULL;
247
248         do {
249                 ret = callback(die_mem, data);
250                 if (ret == DIE_FIND_CB_FOUND)
251                         return die_mem;
252
253                 if ((ret & DIE_FIND_CB_CHILD) &&
254                     die_find_child(die_mem, callback, data, &child_die)) {
255                         memcpy(die_mem, &child_die, sizeof(Dwarf_Die));
256                         return die_mem;
257                 }
258         } while ((ret & DIE_FIND_CB_SIBLING) &&
259                  dwarf_siblingof(die_mem, die_mem) == 0);
260
261         return NULL;
262 }
263
264 struct __addr_die_search_param {
265         Dwarf_Addr      addr;
266         Dwarf_Die       *die_mem;
267 };
268
269 static int __die_search_func_cb(Dwarf_Die *fn_die, void *data)
270 {
271         struct __addr_die_search_param *ad = data;
272
273         if (dwarf_tag(fn_die) == DW_TAG_subprogram &&
274             dwarf_haspc(fn_die, ad->addr)) {
275                 memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die));
276                 return DWARF_CB_ABORT;
277         }
278         return DWARF_CB_OK;
279 }
280
281 /* Search a real subprogram including this line, */
282 static Dwarf_Die *die_find_real_subprogram(Dwarf_Die *cu_die, Dwarf_Addr addr,
283                                            Dwarf_Die *die_mem)
284 {
285         struct __addr_die_search_param ad;
286         ad.addr = addr;
287         ad.die_mem = die_mem;
288         /* dwarf_getscopes can't find subprogram. */
289         if (!dwarf_getfuncs(cu_die, __die_search_func_cb, &ad, 0))
290                 return NULL;
291         else
292                 return die_mem;
293 }
294
295 /* die_find callback for inline function search */
296 static int __die_find_inline_cb(Dwarf_Die *die_mem, void *data)
297 {
298         Dwarf_Addr *addr = data;
299
300         if (dwarf_tag(die_mem) == DW_TAG_inlined_subroutine &&
301             dwarf_haspc(die_mem, *addr))
302                 return DIE_FIND_CB_FOUND;
303
304         return DIE_FIND_CB_CONTINUE;
305 }
306
307 /* Similar to dwarf_getfuncs, but returns inlined_subroutine if exists. */
308 static Dwarf_Die *die_find_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
309                                       Dwarf_Die *die_mem)
310 {
311         return die_find_child(sp_die, __die_find_inline_cb, &addr, die_mem);
312 }
313
314 static int __die_find_variable_cb(Dwarf_Die *die_mem, void *data)
315 {
316         const char *name = data;
317         int tag;
318
319         tag = dwarf_tag(die_mem);
320         if ((tag == DW_TAG_formal_parameter ||
321              tag == DW_TAG_variable) &&
322             (die_compare_name(die_mem, name) == 0))
323                 return DIE_FIND_CB_FOUND;
324
325         return DIE_FIND_CB_CONTINUE;
326 }
327
328 /* Find a variable called 'name' */
329 static Dwarf_Die *die_find_variable(Dwarf_Die *sp_die, const char *name,
330                                     Dwarf_Die *die_mem)
331 {
332         return die_find_child(sp_die, __die_find_variable_cb, (void *)name,
333                               die_mem);
334 }
335
336 static int __die_find_member_cb(Dwarf_Die *die_mem, void *data)
337 {
338         const char *name = data;
339
340         if ((dwarf_tag(die_mem) == DW_TAG_member) &&
341             (die_compare_name(die_mem, name) == 0))
342                 return DIE_FIND_CB_FOUND;
343
344         return DIE_FIND_CB_SIBLING;
345 }
346
347 /* Find a member called 'name' */
348 static Dwarf_Die *die_find_member(Dwarf_Die *st_die, const char *name,
349                                   Dwarf_Die *die_mem)
350 {
351         return die_find_child(st_die, __die_find_member_cb, (void *)name,
352                               die_mem);
353 }
354
355 /*
356  * Probe finder related functions
357  */
358
359 /* Show a location */
360 static int convert_location(Dwarf_Op *op, struct probe_finder *pf)
361 {
362         unsigned int regn;
363         Dwarf_Word offs = 0;
364         bool ref = false;
365         const char *regs;
366         struct kprobe_trace_arg *tvar = pf->tvar;
367
368         /* If this is based on frame buffer, set the offset */
369         if (op->atom == DW_OP_fbreg) {
370                 if (pf->fb_ops == NULL) {
371                         pr_warning("The attribute of frame base is not "
372                                    "supported.\n");
373                         return -ENOTSUP;
374                 }
375                 ref = true;
376                 offs = op->number;
377                 op = &pf->fb_ops[0];
378         }
379
380         if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) {
381                 regn = op->atom - DW_OP_breg0;
382                 offs += op->number;
383                 ref = true;
384         } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) {
385                 regn = op->atom - DW_OP_reg0;
386         } else if (op->atom == DW_OP_bregx) {
387                 regn = op->number;
388                 offs += op->number2;
389                 ref = true;
390         } else if (op->atom == DW_OP_regx) {
391                 regn = op->number;
392         } else {
393                 pr_warning("DW_OP %x is not supported.\n", op->atom);
394                 return -ENOTSUP;
395         }
396
397         regs = get_arch_regstr(regn);
398         if (!regs) {
399                 pr_warning("Mapping for DWARF register number %u missing on this architecture.", regn);
400                 return -ERANGE;
401         }
402
403         tvar->value = strdup(regs);
404         if (tvar->value == NULL)
405                 return -ENOMEM;
406
407         if (ref) {
408                 tvar->ref = zalloc(sizeof(struct kprobe_trace_arg_ref));
409                 if (tvar->ref == NULL)
410                         return -ENOMEM;
411                 tvar->ref->offset = (long)offs;
412         }
413         return 0;
414 }
415
416 static int convert_variable_type(Dwarf_Die *vr_die,
417                                  struct kprobe_trace_arg *targ)
418 {
419         Dwarf_Die type;
420         char buf[16];
421         int ret;
422
423         if (die_get_real_type(vr_die, &type) == NULL) {
424                 pr_warning("Failed to get a type information of %s.\n",
425                            dwarf_diename(vr_die));
426                 return -ENOENT;
427         }
428
429         ret = die_get_byte_size(&type) * 8;
430         if (ret) {
431                 /* Check the bitwidth */
432                 if (ret > MAX_BASIC_TYPE_BITS) {
433                         pr_info("%s exceeds max-bitwidth."
434                                 " Cut down to %d bits.\n",
435                                 dwarf_diename(&type), MAX_BASIC_TYPE_BITS);
436                         ret = MAX_BASIC_TYPE_BITS;
437                 }
438
439                 ret = snprintf(buf, 16, "%c%d",
440                                die_is_signed_type(&type) ? 's' : 'u', ret);
441                 if (ret < 0 || ret >= 16) {
442                         if (ret >= 16)
443                                 ret = -E2BIG;
444                         pr_warning("Failed to convert variable type: %s\n",
445                                    strerror(-ret));
446                         return ret;
447                 }
448                 targ->type = strdup(buf);
449                 if (targ->type == NULL)
450                         return -ENOMEM;
451         }
452         return 0;
453 }
454
455 static int convert_variable_fields(Dwarf_Die *vr_die, const char *varname,
456                                     struct perf_probe_arg_field *field,
457                                     struct kprobe_trace_arg_ref **ref_ptr,
458                                     Dwarf_Die *die_mem)
459 {
460         struct kprobe_trace_arg_ref *ref = *ref_ptr;
461         Dwarf_Die type;
462         Dwarf_Word offs;
463         int ret;
464
465         pr_debug("converting %s in %s\n", field->name, varname);
466         if (die_get_real_type(vr_die, &type) == NULL) {
467                 pr_warning("Failed to get the type of %s.\n", varname);
468                 return -ENOENT;
469         }
470
471         /* Check the pointer and dereference */
472         if (dwarf_tag(&type) == DW_TAG_pointer_type) {
473                 if (!field->ref) {
474                         pr_err("Semantic error: %s must be referred by '->'\n",
475                                field->name);
476                         return -EINVAL;
477                 }
478                 /* Get the type pointed by this pointer */
479                 if (die_get_real_type(&type, &type) == NULL) {
480                         pr_warning("Failed to get the type of %s.\n", varname);
481                         return -ENOENT;
482                 }
483                 /* Verify it is a data structure  */
484                 if (dwarf_tag(&type) != DW_TAG_structure_type) {
485                         pr_warning("%s is not a data structure.\n", varname);
486                         return -EINVAL;
487                 }
488
489                 ref = zalloc(sizeof(struct kprobe_trace_arg_ref));
490                 if (ref == NULL)
491                         return -ENOMEM;
492                 if (*ref_ptr)
493                         (*ref_ptr)->next = ref;
494                 else
495                         *ref_ptr = ref;
496         } else {
497                 /* Verify it is a data structure  */
498                 if (dwarf_tag(&type) != DW_TAG_structure_type) {
499                         pr_warning("%s is not a data structure.\n", varname);
500                         return -EINVAL;
501                 }
502                 if (field->ref) {
503                         pr_err("Semantic error: %s must be referred by '.'\n",
504                                field->name);
505                         return -EINVAL;
506                 }
507                 if (!ref) {
508                         pr_warning("Structure on a register is not "
509                                    "supported yet.\n");
510                         return -ENOTSUP;
511                 }
512         }
513
514         if (die_find_member(&type, field->name, die_mem) == NULL) {
515                 pr_warning("%s(tyep:%s) has no member %s.\n", varname,
516                            dwarf_diename(&type), field->name);
517                 return -EINVAL;
518         }
519
520         /* Get the offset of the field */
521         ret = die_get_data_member_location(die_mem, &offs);
522         if (ret < 0) {
523                 pr_warning("Failed to get the offset of %s.\n", field->name);
524                 return ret;
525         }
526         ref->offset += (long)offs;
527
528         /* Converting next field */
529         if (field->next)
530                 return convert_variable_fields(die_mem, field->name,
531                                         field->next, &ref, die_mem);
532         else
533                 return 0;
534 }
535
536 /* Show a variables in kprobe event format */
537 static int convert_variable(Dwarf_Die *vr_die, struct probe_finder *pf)
538 {
539         Dwarf_Attribute attr;
540         Dwarf_Die die_mem;
541         Dwarf_Op *expr;
542         size_t nexpr;
543         int ret;
544
545         if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL)
546                 goto error;
547         /* TODO: handle more than 1 exprs */
548         ret = dwarf_getlocation_addr(&attr, pf->addr, &expr, &nexpr, 1);
549         if (ret <= 0 || nexpr == 0)
550                 goto error;
551
552         ret = convert_location(expr, pf);
553         if (ret == 0 && pf->pvar->field) {
554                 ret = convert_variable_fields(vr_die, pf->pvar->var,
555                                               pf->pvar->field, &pf->tvar->ref,
556                                               &die_mem);
557                 vr_die = &die_mem;
558         }
559         if (ret == 0) {
560                 if (pf->pvar->type) {
561                         pf->tvar->type = strdup(pf->pvar->type);
562                         if (pf->tvar->type == NULL)
563                                 ret = -ENOMEM;
564                 } else
565                         ret = convert_variable_type(vr_die, pf->tvar);
566         }
567         /* *expr will be cached in libdw. Don't free it. */
568         return ret;
569 error:
570         /* TODO: Support const_value */
571         pr_err("Failed to find the location of %s at this address.\n"
572                " Perhaps, it has been optimized out.\n", pf->pvar->var);
573         return -ENOENT;
574 }
575
576 /* Find a variable in a subprogram die */
577 static int find_variable(Dwarf_Die *sp_die, struct probe_finder *pf)
578 {
579         Dwarf_Die vr_die;
580         char buf[32], *ptr;
581         int ret;
582
583         /* TODO: Support arrays */
584         if (pf->pvar->name)
585                 pf->tvar->name = strdup(pf->pvar->name);
586         else {
587                 ret = synthesize_perf_probe_arg(pf->pvar, buf, 32);
588                 if (ret < 0)
589                         return ret;
590                 ptr = strchr(buf, ':'); /* Change type separator to _ */
591                 if (ptr)
592                         *ptr = '_';
593                 pf->tvar->name = strdup(buf);
594         }
595         if (pf->tvar->name == NULL)
596                 return -ENOMEM;
597
598         if (!is_c_varname(pf->pvar->var)) {
599                 /* Copy raw parameters */
600                 pf->tvar->value = strdup(pf->pvar->var);
601                 if (pf->tvar->value == NULL)
602                         return -ENOMEM;
603                 else
604                         return 0;
605         }
606
607         pr_debug("Searching '%s' variable in context.\n",
608                  pf->pvar->var);
609         /* Search child die for local variables and parameters. */
610         if (!die_find_variable(sp_die, pf->pvar->var, &vr_die)) {
611                 pr_warning("Failed to find '%s' in this function.\n",
612                            pf->pvar->var);
613                 return -ENOENT;
614         }
615         return convert_variable(&vr_die, pf);
616 }
617
618 /* Show a probe point to output buffer */
619 static int convert_probe_point(Dwarf_Die *sp_die, struct probe_finder *pf)
620 {
621         struct kprobe_trace_event *tev;
622         Dwarf_Addr eaddr;
623         Dwarf_Die die_mem;
624         const char *name;
625         int ret, i;
626         Dwarf_Attribute fb_attr;
627         size_t nops;
628
629         if (pf->ntevs == MAX_PROBES) {
630                 pr_warning("Too many( > %d) probe point found.\n", MAX_PROBES);
631                 return -ERANGE;
632         }
633         tev = &pf->tevs[pf->ntevs++];
634
635         /* If no real subprogram, find a real one */
636         if (!sp_die || dwarf_tag(sp_die) != DW_TAG_subprogram) {
637                 sp_die = die_find_real_subprogram(&pf->cu_die,
638                                                  pf->addr, &die_mem);
639                 if (!sp_die) {
640                         pr_warning("Failed to find probe point in any "
641                                    "functions.\n");
642                         return -ENOENT;
643                 }
644         }
645
646         /* Copy the name of probe point */
647         name = dwarf_diename(sp_die);
648         if (name) {
649                 if (dwarf_entrypc(sp_die, &eaddr) != 0) {
650                         pr_warning("Failed to get entry pc of %s\n",
651                                    dwarf_diename(sp_die));
652                         return -ENOENT;
653                 }
654                 tev->point.symbol = strdup(name);
655                 if (tev->point.symbol == NULL)
656                         return -ENOMEM;
657                 tev->point.offset = (unsigned long)(pf->addr - eaddr);
658         } else
659                 /* This function has no name. */
660                 tev->point.offset = (unsigned long)pf->addr;
661
662         pr_debug("Probe point found: %s+%lu\n", tev->point.symbol,
663                  tev->point.offset);
664
665         /* Get the frame base attribute/ops */
666         dwarf_attr(sp_die, DW_AT_frame_base, &fb_attr);
667         ret = dwarf_getlocation_addr(&fb_attr, pf->addr, &pf->fb_ops, &nops, 1);
668         if (ret <= 0 || nops == 0) {
669                 pf->fb_ops = NULL;
670         } else if (nops == 1 && pf->fb_ops[0].atom == DW_OP_call_frame_cfa &&
671                    pf->cfi != NULL) {
672                 Dwarf_Frame *frame;
673                 if (dwarf_cfi_addrframe(pf->cfi, pf->addr, &frame) != 0 ||
674                     dwarf_frame_cfa(frame, &pf->fb_ops, &nops) != 0) {
675                         pr_warning("Failed to get CFA on 0x%jx\n",
676                                    (uintmax_t)pf->addr);
677                         return -ENOENT;
678                 }
679         }
680
681         /* Find each argument */
682         tev->nargs = pf->pev->nargs;
683         tev->args = zalloc(sizeof(struct kprobe_trace_arg) * tev->nargs);
684         if (tev->args == NULL)
685                 return -ENOMEM;
686         for (i = 0; i < pf->pev->nargs; i++) {
687                 pf->pvar = &pf->pev->args[i];
688                 pf->tvar = &tev->args[i];
689                 ret = find_variable(sp_die, pf);
690                 if (ret != 0)
691                         return ret;
692         }
693
694         /* *pf->fb_ops will be cached in libdw. Don't free it. */
695         pf->fb_ops = NULL;
696         return 0;
697 }
698
699 /* Find probe point from its line number */
700 static int find_probe_point_by_line(struct probe_finder *pf)
701 {
702         Dwarf_Lines *lines;
703         Dwarf_Line *line;
704         size_t nlines, i;
705         Dwarf_Addr addr;
706         int lineno;
707         int ret = 0;
708
709         if (dwarf_getsrclines(&pf->cu_die, &lines, &nlines) != 0) {
710                 pr_warning("No source lines found in this CU.\n");
711                 return -ENOENT;
712         }
713
714         for (i = 0; i < nlines && ret == 0; i++) {
715                 line = dwarf_onesrcline(lines, i);
716                 if (dwarf_lineno(line, &lineno) != 0 ||
717                     lineno != pf->lno)
718                         continue;
719
720                 /* TODO: Get fileno from line, but how? */
721                 if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
722                         continue;
723
724                 if (dwarf_lineaddr(line, &addr) != 0) {
725                         pr_warning("Failed to get the address of the line.\n");
726                         return -ENOENT;
727                 }
728                 pr_debug("Probe line found: line[%d]:%d addr:0x%jx\n",
729                          (int)i, lineno, (uintmax_t)addr);
730                 pf->addr = addr;
731
732                 ret = convert_probe_point(NULL, pf);
733                 /* Continuing, because target line might be inlined. */
734         }
735         return ret;
736 }
737
738 /* Find lines which match lazy pattern */
739 static int find_lazy_match_lines(struct list_head *head,
740                                  const char *fname, const char *pat)
741 {
742         char *fbuf, *p1, *p2;
743         int fd, ret, line, nlines = 0;
744         struct stat st;
745
746         fd = open(fname, O_RDONLY);
747         if (fd < 0) {
748                 pr_warning("Failed to open %s: %s\n", fname, strerror(-fd));
749                 return fd;
750         }
751
752         ret = fstat(fd, &st);
753         if (ret < 0) {
754                 pr_warning("Failed to get the size of %s: %s\n",
755                            fname, strerror(errno));
756                 return ret;
757         }
758         fbuf = xmalloc(st.st_size + 2);
759         ret = read(fd, fbuf, st.st_size);
760         if (ret < 0) {
761                 pr_warning("Failed to read %s: %s\n", fname, strerror(errno));
762                 return ret;
763         }
764         close(fd);
765         fbuf[st.st_size] = '\n';        /* Dummy line */
766         fbuf[st.st_size + 1] = '\0';
767         p1 = fbuf;
768         line = 1;
769         while ((p2 = strchr(p1, '\n')) != NULL) {
770                 *p2 = '\0';
771                 if (strlazymatch(p1, pat)) {
772                         line_list__add_line(head, line);
773                         nlines++;
774                 }
775                 line++;
776                 p1 = p2 + 1;
777         }
778         free(fbuf);
779         return nlines;
780 }
781
782 /* Find probe points from lazy pattern  */
783 static int find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf)
784 {
785         Dwarf_Lines *lines;
786         Dwarf_Line *line;
787         size_t nlines, i;
788         Dwarf_Addr addr;
789         Dwarf_Die die_mem;
790         int lineno;
791         int ret = 0;
792
793         if (list_empty(&pf->lcache)) {
794                 /* Matching lazy line pattern */
795                 ret = find_lazy_match_lines(&pf->lcache, pf->fname,
796                                             pf->pev->point.lazy_line);
797                 if (ret == 0) {
798                         pr_debug("No matched lines found in %s.\n", pf->fname);
799                         return 0;
800                 } else if (ret < 0)
801                         return ret;
802         }
803
804         if (dwarf_getsrclines(&pf->cu_die, &lines, &nlines) != 0) {
805                 pr_warning("No source lines found in this CU.\n");
806                 return -ENOENT;
807         }
808
809         for (i = 0; i < nlines && ret >= 0; i++) {
810                 line = dwarf_onesrcline(lines, i);
811
812                 if (dwarf_lineno(line, &lineno) != 0 ||
813                     !line_list__has_line(&pf->lcache, lineno))
814                         continue;
815
816                 /* TODO: Get fileno from line, but how? */
817                 if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
818                         continue;
819
820                 if (dwarf_lineaddr(line, &addr) != 0) {
821                         pr_debug("Failed to get the address of line %d.\n",
822                                  lineno);
823                         continue;
824                 }
825                 if (sp_die) {
826                         /* Address filtering 1: does sp_die include addr? */
827                         if (!dwarf_haspc(sp_die, addr))
828                                 continue;
829                         /* Address filtering 2: No child include addr? */
830                         if (die_find_inlinefunc(sp_die, addr, &die_mem))
831                                 continue;
832                 }
833
834                 pr_debug("Probe line found: line[%d]:%d addr:0x%llx\n",
835                          (int)i, lineno, (unsigned long long)addr);
836                 pf->addr = addr;
837
838                 ret = convert_probe_point(sp_die, pf);
839                 /* Continuing, because target line might be inlined. */
840         }
841         /* TODO: deallocate lines, but how? */
842         return ret;
843 }
844
845 /* Callback parameter with return value */
846 struct dwarf_callback_param {
847         void *data;
848         int retval;
849 };
850
851 static int probe_point_inline_cb(Dwarf_Die *in_die, void *data)
852 {
853         struct dwarf_callback_param *param = data;
854         struct probe_finder *pf = param->data;
855         struct perf_probe_point *pp = &pf->pev->point;
856         Dwarf_Addr addr;
857
858         if (pp->lazy_line)
859                 param->retval = find_probe_point_lazy(in_die, pf);
860         else {
861                 /* Get probe address */
862                 if (dwarf_entrypc(in_die, &addr) != 0) {
863                         pr_warning("Failed to get entry pc of %s.\n",
864                                    dwarf_diename(in_die));
865                         param->retval = -ENOENT;
866                         return DWARF_CB_ABORT;
867                 }
868                 pf->addr = addr;
869                 pf->addr += pp->offset;
870                 pr_debug("found inline addr: 0x%jx\n",
871                          (uintmax_t)pf->addr);
872
873                 param->retval = convert_probe_point(in_die, pf);
874         }
875
876         return DWARF_CB_OK;
877 }
878
879 /* Search function from function name */
880 static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
881 {
882         struct dwarf_callback_param *param = data;
883         struct probe_finder *pf = param->data;
884         struct perf_probe_point *pp = &pf->pev->point;
885
886         /* Check tag and diename */
887         if (dwarf_tag(sp_die) != DW_TAG_subprogram ||
888             die_compare_name(sp_die, pp->function) != 0)
889                 return DWARF_CB_OK;
890
891         pf->fname = dwarf_decl_file(sp_die);
892         if (pp->line) { /* Function relative line */
893                 dwarf_decl_line(sp_die, &pf->lno);
894                 pf->lno += pp->line;
895                 param->retval = find_probe_point_by_line(pf);
896         } else if (!dwarf_func_inline(sp_die)) {
897                 /* Real function */
898                 if (pp->lazy_line)
899                         param->retval = find_probe_point_lazy(sp_die, pf);
900                 else {
901                         if (dwarf_entrypc(sp_die, &pf->addr) != 0) {
902                                 pr_warning("Failed to get entry pc of %s.\n",
903                                            dwarf_diename(sp_die));
904                                 param->retval = -ENOENT;
905                                 return DWARF_CB_ABORT;
906                         }
907                         pf->addr += pp->offset;
908                         /* TODO: Check the address in this function */
909                         param->retval = convert_probe_point(sp_die, pf);
910                 }
911         } else {
912                 struct dwarf_callback_param _param = {.data = (void *)pf,
913                                                       .retval = 0};
914                 /* Inlined function: search instances */
915                 dwarf_func_inline_instances(sp_die, probe_point_inline_cb,
916                                             &_param);
917                 param->retval = _param.retval;
918         }
919
920         return DWARF_CB_ABORT; /* Exit; no same symbol in this CU. */
921 }
922
923 static int find_probe_point_by_func(struct probe_finder *pf)
924 {
925         struct dwarf_callback_param _param = {.data = (void *)pf,
926                                               .retval = 0};
927         dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, &_param, 0);
928         return _param.retval;
929 }
930
931 /* Find kprobe_trace_events specified by perf_probe_event from debuginfo */
932 int find_kprobe_trace_events(int fd, struct perf_probe_event *pev,
933                              struct kprobe_trace_event **tevs)
934 {
935         struct probe_finder pf = {.pev = pev};
936         struct perf_probe_point *pp = &pev->point;
937         Dwarf_Off off, noff;
938         size_t cuhl;
939         Dwarf_Die *diep;
940         Dwarf *dbg;
941         int ret = 0;
942
943         pf.tevs = zalloc(sizeof(struct kprobe_trace_event) * MAX_PROBES);
944         if (pf.tevs == NULL)
945                 return -ENOMEM;
946         *tevs = pf.tevs;
947         pf.ntevs = 0;
948
949         dbg = dwarf_begin(fd, DWARF_C_READ);
950         if (!dbg) {
951                 pr_warning("No dwarf info found in the vmlinux - "
952                         "please rebuild with CONFIG_DEBUG_INFO=y.\n");
953                 return -EBADF;
954         }
955
956         /* Get the call frame information from this dwarf */
957         pf.cfi = dwarf_getcfi(dbg);
958
959         off = 0;
960         line_list__init(&pf.lcache);
961         /* Loop on CUs (Compilation Unit) */
962         while (!dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL) &&
963                ret >= 0) {
964                 /* Get the DIE(Debugging Information Entry) of this CU */
965                 diep = dwarf_offdie(dbg, off + cuhl, &pf.cu_die);
966                 if (!diep)
967                         continue;
968
969                 /* Check if target file is included. */
970                 if (pp->file)
971                         pf.fname = cu_find_realpath(&pf.cu_die, pp->file);
972                 else
973                         pf.fname = NULL;
974
975                 if (!pp->file || pf.fname) {
976                         if (pp->function)
977                                 ret = find_probe_point_by_func(&pf);
978                         else if (pp->lazy_line)
979                                 ret = find_probe_point_lazy(NULL, &pf);
980                         else {
981                                 pf.lno = pp->line;
982                                 ret = find_probe_point_by_line(&pf);
983                         }
984                 }
985                 off = noff;
986         }
987         line_list__free(&pf.lcache);
988         dwarf_end(dbg);
989
990         return (ret < 0) ? ret : pf.ntevs;
991 }
992
993 /* Reverse search */
994 int find_perf_probe_point(int fd, unsigned long addr,
995                           struct perf_probe_point *ppt)
996 {
997         Dwarf_Die cudie, spdie, indie;
998         Dwarf *dbg;
999         Dwarf_Line *line;
1000         Dwarf_Addr laddr, eaddr;
1001         const char *tmp;
1002         int lineno, ret = 0;
1003         bool found = false;
1004
1005         dbg = dwarf_begin(fd, DWARF_C_READ);
1006         if (!dbg)
1007                 return -EBADF;
1008
1009         /* Find cu die */
1010         if (!dwarf_addrdie(dbg, (Dwarf_Addr)addr, &cudie)) {
1011                 ret = -EINVAL;
1012                 goto end;
1013         }
1014
1015         /* Find a corresponding line */
1016         line = dwarf_getsrc_die(&cudie, (Dwarf_Addr)addr);
1017         if (line) {
1018                 if (dwarf_lineaddr(line, &laddr) == 0 &&
1019                     (Dwarf_Addr)addr == laddr &&
1020                     dwarf_lineno(line, &lineno) == 0) {
1021                         tmp = dwarf_linesrc(line, NULL, NULL);
1022                         if (tmp) {
1023                                 ppt->line = lineno;
1024                                 ppt->file = strdup(tmp);
1025                                 if (ppt->file == NULL) {
1026                                         ret = -ENOMEM;
1027                                         goto end;
1028                                 }
1029                                 found = true;
1030                         }
1031                 }
1032         }
1033
1034         /* Find a corresponding function */
1035         if (die_find_real_subprogram(&cudie, (Dwarf_Addr)addr, &spdie)) {
1036                 tmp = dwarf_diename(&spdie);
1037                 if (!tmp || dwarf_entrypc(&spdie, &eaddr) != 0)
1038                         goto end;
1039
1040                 if (ppt->line) {
1041                         if (die_find_inlinefunc(&spdie, (Dwarf_Addr)addr,
1042                                                 &indie)) {
1043                                 /* addr in an inline function */
1044                                 tmp = dwarf_diename(&indie);
1045                                 if (!tmp)
1046                                         goto end;
1047                                 ret = dwarf_decl_line(&indie, &lineno);
1048                         } else {
1049                                 if (eaddr == addr) {    /* Function entry */
1050                                         lineno = ppt->line;
1051                                         ret = 0;
1052                                 } else
1053                                         ret = dwarf_decl_line(&spdie, &lineno);
1054                         }
1055                         if (ret == 0) {
1056                                 /* Make a relative line number */
1057                                 ppt->line -= lineno;
1058                                 goto found;
1059                         }
1060                 }
1061                 /* We don't have a line number, let's use offset */
1062                 ppt->offset = addr - (unsigned long)eaddr;
1063 found:
1064                 ppt->function = strdup(tmp);
1065                 if (ppt->function == NULL) {
1066                         ret = -ENOMEM;
1067                         goto end;
1068                 }
1069                 found = true;
1070         }
1071
1072 end:
1073         dwarf_end(dbg);
1074         if (ret >= 0)
1075                 ret = found ? 1 : 0;
1076         return ret;
1077 }
1078
1079 /* Add a line and store the src path */
1080 static int line_range_add_line(const char *src, unsigned int lineno,
1081                                struct line_range *lr)
1082 {
1083         /* Copy real path */
1084         if (!lr->path) {
1085                 lr->path = strdup(src);
1086                 if (lr->path == NULL)
1087                         return -ENOMEM;
1088         }
1089         return line_list__add_line(&lr->line_list, lineno);
1090 }
1091
1092 /* Search function declaration lines */
1093 static int line_range_funcdecl_cb(Dwarf_Die *sp_die, void *data)
1094 {
1095         struct dwarf_callback_param *param = data;
1096         struct line_finder *lf = param->data;
1097         const char *src;
1098         int lineno;
1099
1100         src = dwarf_decl_file(sp_die);
1101         if (src && strtailcmp(src, lf->fname) != 0)
1102                 return DWARF_CB_OK;
1103
1104         if (dwarf_decl_line(sp_die, &lineno) != 0 ||
1105             (lf->lno_s > lineno || lf->lno_e < lineno))
1106                 return DWARF_CB_OK;
1107
1108         param->retval = line_range_add_line(src, lineno, lf->lr);
1109         return DWARF_CB_OK;
1110 }
1111
1112 static int find_line_range_func_decl_lines(struct line_finder *lf)
1113 {
1114         struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
1115         dwarf_getfuncs(&lf->cu_die, line_range_funcdecl_cb, &param, 0);
1116         return param.retval;
1117 }
1118
1119 /* Find line range from its line number */
1120 static int find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
1121 {
1122         Dwarf_Lines *lines;
1123         Dwarf_Line *line;
1124         size_t nlines, i;
1125         Dwarf_Addr addr;
1126         int lineno, ret = 0;
1127         const char *src;
1128         Dwarf_Die die_mem;
1129
1130         line_list__init(&lf->lr->line_list);
1131         if (dwarf_getsrclines(&lf->cu_die, &lines, &nlines) != 0) {
1132                 pr_warning("No source lines found in this CU.\n");
1133                 return -ENOENT;
1134         }
1135
1136         /* Search probable lines on lines list */
1137         for (i = 0; i < nlines; i++) {
1138                 line = dwarf_onesrcline(lines, i);
1139                 if (dwarf_lineno(line, &lineno) != 0 ||
1140                     (lf->lno_s > lineno || lf->lno_e < lineno))
1141                         continue;
1142
1143                 if (sp_die) {
1144                         /* Address filtering 1: does sp_die include addr? */
1145                         if (dwarf_lineaddr(line, &addr) != 0 ||
1146                             !dwarf_haspc(sp_die, addr))
1147                                 continue;
1148
1149                         /* Address filtering 2: No child include addr? */
1150                         if (die_find_inlinefunc(sp_die, addr, &die_mem))
1151                                 continue;
1152                 }
1153
1154                 /* TODO: Get fileno from line, but how? */
1155                 src = dwarf_linesrc(line, NULL, NULL);
1156                 if (strtailcmp(src, lf->fname) != 0)
1157                         continue;
1158
1159                 ret = line_range_add_line(src, lineno, lf->lr);
1160                 if (ret < 0)
1161                         return ret;
1162         }
1163
1164         /*
1165          * Dwarf lines doesn't include function declarations. We have to
1166          * check functions list or given function.
1167          */
1168         if (sp_die) {
1169                 src = dwarf_decl_file(sp_die);
1170                 if (src && dwarf_decl_line(sp_die, &lineno) == 0 &&
1171                     (lf->lno_s <= lineno && lf->lno_e >= lineno))
1172                         ret = line_range_add_line(src, lineno, lf->lr);
1173         } else
1174                 ret = find_line_range_func_decl_lines(lf);
1175
1176         /* Update status */
1177         if (ret >= 0)
1178                 if (!list_empty(&lf->lr->line_list))
1179                         ret = lf->found = 1;
1180                 else
1181                         ret = 0;        /* Lines are not found */
1182         else {
1183                 free(lf->lr->path);
1184                 lf->lr->path = NULL;
1185         }
1186         return ret;
1187 }
1188
1189 static int line_range_inline_cb(Dwarf_Die *in_die, void *data)
1190 {
1191         struct dwarf_callback_param *param = data;
1192
1193         param->retval = find_line_range_by_line(in_die, param->data);
1194         return DWARF_CB_ABORT;  /* No need to find other instances */
1195 }
1196
1197 /* Search function from function name */
1198 static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
1199 {
1200         struct dwarf_callback_param *param = data;
1201         struct line_finder *lf = param->data;
1202         struct line_range *lr = lf->lr;
1203
1204         if (dwarf_tag(sp_die) == DW_TAG_subprogram &&
1205             die_compare_name(sp_die, lr->function) == 0) {
1206                 lf->fname = dwarf_decl_file(sp_die);
1207                 dwarf_decl_line(sp_die, &lr->offset);
1208                 pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
1209                 lf->lno_s = lr->offset + lr->start;
1210                 if (lf->lno_s < 0)      /* Overflow */
1211                         lf->lno_s = INT_MAX;
1212                 lf->lno_e = lr->offset + lr->end;
1213                 if (lf->lno_e < 0)      /* Overflow */
1214                         lf->lno_e = INT_MAX;
1215                 pr_debug("New line range: %d to %d\n", lf->lno_s, lf->lno_e);
1216                 lr->start = lf->lno_s;
1217                 lr->end = lf->lno_e;
1218                 if (dwarf_func_inline(sp_die)) {
1219                         struct dwarf_callback_param _param;
1220                         _param.data = (void *)lf;
1221                         _param.retval = 0;
1222                         dwarf_func_inline_instances(sp_die,
1223                                                     line_range_inline_cb,
1224                                                     &_param);
1225                         param->retval = _param.retval;
1226                 } else
1227                         param->retval = find_line_range_by_line(sp_die, lf);
1228                 return DWARF_CB_ABORT;
1229         }
1230         return DWARF_CB_OK;
1231 }
1232
1233 static int find_line_range_by_func(struct line_finder *lf)
1234 {
1235         struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
1236         dwarf_getfuncs(&lf->cu_die, line_range_search_cb, &param, 0);
1237         return param.retval;
1238 }
1239
1240 int find_line_range(int fd, struct line_range *lr)
1241 {
1242         struct line_finder lf = {.lr = lr, .found = 0};
1243         int ret = 0;
1244         Dwarf_Off off = 0, noff;
1245         size_t cuhl;
1246         Dwarf_Die *diep;
1247         Dwarf *dbg;
1248
1249         dbg = dwarf_begin(fd, DWARF_C_READ);
1250         if (!dbg) {
1251                 pr_warning("No dwarf info found in the vmlinux - "
1252                         "please rebuild with CONFIG_DEBUG_INFO=y.\n");
1253                 return -EBADF;
1254         }
1255
1256         /* Loop on CUs (Compilation Unit) */
1257         while (!lf.found && ret >= 0) {
1258                 if (dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL) != 0)
1259                         break;
1260
1261                 /* Get the DIE(Debugging Information Entry) of this CU */
1262                 diep = dwarf_offdie(dbg, off + cuhl, &lf.cu_die);
1263                 if (!diep)
1264                         continue;
1265
1266                 /* Check if target file is included. */
1267                 if (lr->file)
1268                         lf.fname = cu_find_realpath(&lf.cu_die, lr->file);
1269                 else
1270                         lf.fname = 0;
1271
1272                 if (!lr->file || lf.fname) {
1273                         if (lr->function)
1274                                 ret = find_line_range_by_func(&lf);
1275                         else {
1276                                 lf.lno_s = lr->start;
1277                                 lf.lno_e = lr->end;
1278                                 ret = find_line_range_by_line(NULL, &lf);
1279                         }
1280                 }
1281                 off = noff;
1282         }
1283         pr_debug("path: %lx\n", (unsigned long)lr->path);
1284         dwarf_end(dbg);
1285
1286         return (ret < 0) ? ret : lf.found;
1287 }
1288