perf probe: Remove redundant dwarf functions
[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 <linux/bitops.h>
37 #include "event.h"
38 #include "debug.h"
39 #include "util.h"
40 #include "symbol.h"
41 #include "probe-finder.h"
42
43 /* Kprobe tracer basic type is up to u64 */
44 #define MAX_BASIC_TYPE_BITS     64
45
46 /* Line number list operations */
47
48 /* Add a line to line number list */
49 static int line_list__add_line(struct list_head *head, int line)
50 {
51         struct line_node *ln;
52         struct list_head *p;
53
54         /* Reverse search, because new line will be the last one */
55         list_for_each_entry_reverse(ln, head, list) {
56                 if (ln->line < line) {
57                         p = &ln->list;
58                         goto found;
59                 } else if (ln->line == line)    /* Already exist */
60                         return 1;
61         }
62         /* List is empty, or the smallest entry */
63         p = head;
64 found:
65         pr_debug("line list: add a line %u\n", line);
66         ln = zalloc(sizeof(struct line_node));
67         if (ln == NULL)
68                 return -ENOMEM;
69         ln->line = line;
70         INIT_LIST_HEAD(&ln->list);
71         list_add(&ln->list, p);
72         return 0;
73 }
74
75 /* Check if the line in line number list */
76 static int line_list__has_line(struct list_head *head, int line)
77 {
78         struct line_node *ln;
79
80         /* Reverse search, because new line will be the last one */
81         list_for_each_entry(ln, head, list)
82                 if (ln->line == line)
83                         return 1;
84
85         return 0;
86 }
87
88 /* Init line number list */
89 static void line_list__init(struct list_head *head)
90 {
91         INIT_LIST_HEAD(head);
92 }
93
94 /* Free line number list */
95 static void line_list__free(struct list_head *head)
96 {
97         struct line_node *ln;
98         while (!list_empty(head)) {
99                 ln = list_first_entry(head, struct line_node, list);
100                 list_del(&ln->list);
101                 free(ln);
102         }
103 }
104
105 /* Dwarf FL wrappers */
106 static char *debuginfo_path;    /* Currently dummy */
107
108 static const Dwfl_Callbacks offline_callbacks = {
109         .find_debuginfo = dwfl_standard_find_debuginfo,
110         .debuginfo_path = &debuginfo_path,
111
112         .section_address = dwfl_offline_section_address,
113
114         /* We use this table for core files too.  */
115         .find_elf = dwfl_build_id_find_elf,
116 };
117
118 /* Get a Dwarf from offline image */
119 static Dwarf *dwfl_init_offline_dwarf(int fd, Dwfl **dwflp, Dwarf_Addr *bias)
120 {
121         Dwfl_Module *mod;
122         Dwarf *dbg = NULL;
123
124         if (!dwflp)
125                 return NULL;
126
127         *dwflp = dwfl_begin(&offline_callbacks);
128         if (!*dwflp)
129                 return NULL;
130
131         mod = dwfl_report_offline(*dwflp, "", "", fd);
132         if (!mod)
133                 goto error;
134
135         dbg = dwfl_module_getdwarf(mod, bias);
136         if (!dbg) {
137 error:
138                 dwfl_end(*dwflp);
139                 *dwflp = NULL;
140         }
141         return dbg;
142 }
143
144 #if _ELFUTILS_PREREQ(0, 148)
145 /* This method is buggy if elfutils is older than 0.148 */
146 static int __linux_kernel_find_elf(Dwfl_Module *mod,
147                                    void **userdata,
148                                    const char *module_name,
149                                    Dwarf_Addr base,
150                                    char **file_name, Elf **elfp)
151 {
152         int fd;
153         const char *path = kernel_get_module_path(module_name);
154
155         pr_debug2("Use file %s for %s\n", path, module_name);
156         if (path) {
157                 fd = open(path, O_RDONLY);
158                 if (fd >= 0) {
159                         *file_name = strdup(path);
160                         return fd;
161                 }
162         }
163         /* If failed, try to call standard method */
164         return dwfl_linux_kernel_find_elf(mod, userdata, module_name, base,
165                                           file_name, elfp);
166 }
167
168 static const Dwfl_Callbacks kernel_callbacks = {
169         .find_debuginfo = dwfl_standard_find_debuginfo,
170         .debuginfo_path = &debuginfo_path,
171
172         .find_elf = __linux_kernel_find_elf,
173         .section_address = dwfl_linux_kernel_module_section_address,
174 };
175
176 /* Get a Dwarf from live kernel image */
177 static Dwarf *dwfl_init_live_kernel_dwarf(Dwarf_Addr addr, Dwfl **dwflp,
178                                           Dwarf_Addr *bias)
179 {
180         Dwarf *dbg;
181
182         if (!dwflp)
183                 return NULL;
184
185         *dwflp = dwfl_begin(&kernel_callbacks);
186         if (!*dwflp)
187                 return NULL;
188
189         /* Load the kernel dwarves: Don't care the result here */
190         dwfl_linux_kernel_report_kernel(*dwflp);
191         dwfl_linux_kernel_report_modules(*dwflp);
192
193         dbg = dwfl_addrdwarf(*dwflp, addr, bias);
194         /* Here, check whether we could get a real dwarf */
195         if (!dbg) {
196                 pr_debug("Failed to find kernel dwarf at %lx\n",
197                          (unsigned long)addr);
198                 dwfl_end(*dwflp);
199                 *dwflp = NULL;
200         }
201         return dbg;
202 }
203 #else
204 /* With older elfutils, this just support kernel module... */
205 static Dwarf *dwfl_init_live_kernel_dwarf(Dwarf_Addr addr __used, Dwfl **dwflp,
206                                           Dwarf_Addr *bias)
207 {
208         int fd;
209         const char *path = kernel_get_module_path("kernel");
210
211         if (!path) {
212                 pr_err("Failed to find vmlinux path\n");
213                 return NULL;
214         }
215
216         pr_debug2("Use file %s for debuginfo\n", path);
217         fd = open(path, O_RDONLY);
218         if (fd < 0)
219                 return NULL;
220
221         return dwfl_init_offline_dwarf(fd, dwflp, bias);
222 }
223 #endif
224
225 /* Dwarf wrappers */
226
227 /* Find the realpath of the target file. */
228 static const char *cu_find_realpath(Dwarf_Die *cu_die, const char *fname)
229 {
230         Dwarf_Files *files;
231         size_t nfiles, i;
232         const char *src = NULL;
233         int ret;
234
235         if (!fname)
236                 return NULL;
237
238         ret = dwarf_getsrcfiles(cu_die, &files, &nfiles);
239         if (ret != 0)
240                 return NULL;
241
242         for (i = 0; i < nfiles; i++) {
243                 src = dwarf_filesrc(files, i, NULL, NULL);
244                 if (strtailcmp(src, fname) == 0)
245                         break;
246         }
247         if (i == nfiles)
248                 return NULL;
249         return src;
250 }
251
252 /* Get DW_AT_comp_dir (should be NULL with older gcc) */
253 static const char *cu_get_comp_dir(Dwarf_Die *cu_die)
254 {
255         Dwarf_Attribute attr;
256         if (dwarf_attr(cu_die, DW_AT_comp_dir, &attr) == NULL)
257                 return NULL;
258         return dwarf_formstring(&attr);
259 }
260
261 /* Get a line number and file name for given address */
262 static int cu_find_lineinfo(Dwarf_Die *cudie, unsigned long addr,
263                             const char **fname, int *lineno)
264 {
265         Dwarf_Line *line;
266         Dwarf_Addr laddr;
267
268         line = dwarf_getsrc_die(cudie, (Dwarf_Addr)addr);
269         if (line && dwarf_lineaddr(line, &laddr) == 0 &&
270             addr == (unsigned long)laddr && dwarf_lineno(line, lineno) == 0) {
271                 *fname = dwarf_linesrc(line, NULL, NULL);
272                 if (!*fname)
273                         /* line number is useless without filename */
274                         *lineno = 0;
275         }
276
277         return *lineno ?: -ENOENT;
278 }
279
280 /* Compare diename and tname */
281 static bool die_compare_name(Dwarf_Die *dw_die, const char *tname)
282 {
283         const char *name;
284         name = dwarf_diename(dw_die);
285         return name ? (strcmp(tname, name) == 0) : false;
286 }
287
288 /* Get callsite line number of inline-function instance */
289 static int die_get_call_lineno(Dwarf_Die *in_die)
290 {
291         Dwarf_Attribute attr;
292         Dwarf_Word ret;
293
294         if (!dwarf_attr(in_die, DW_AT_call_line, &attr))
295                 return -ENOENT;
296
297         dwarf_formudata(&attr, &ret);
298         return (int)ret;
299 }
300
301 /* Get type die */
302 static Dwarf_Die *die_get_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
303 {
304         Dwarf_Attribute attr;
305
306         if (dwarf_attr_integrate(vr_die, DW_AT_type, &attr) &&
307             dwarf_formref_die(&attr, die_mem))
308                 return die_mem;
309         else
310                 return NULL;
311 }
312
313 /* Get a type die, but skip qualifiers */
314 static Dwarf_Die *__die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
315 {
316         int tag;
317
318         do {
319                 vr_die = die_get_type(vr_die, die_mem);
320                 if (!vr_die)
321                         break;
322                 tag = dwarf_tag(vr_die);
323         } while (tag == DW_TAG_const_type ||
324                  tag == DW_TAG_restrict_type ||
325                  tag == DW_TAG_volatile_type ||
326                  tag == DW_TAG_shared_type);
327
328         return vr_die;
329 }
330
331 /* Get a type die, but skip qualifiers and typedef */
332 static Dwarf_Die *die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
333 {
334         do {
335                 vr_die = __die_get_real_type(vr_die, die_mem);
336         } while (vr_die && dwarf_tag(vr_die) == DW_TAG_typedef);
337
338         return vr_die;
339 }
340
341 static int die_get_attr_udata(Dwarf_Die *tp_die, unsigned int attr_name,
342                               Dwarf_Word *result)
343 {
344         Dwarf_Attribute attr;
345
346         if (dwarf_attr(tp_die, attr_name, &attr) == NULL ||
347             dwarf_formudata(&attr, result) != 0)
348                 return -ENOENT;
349
350         return 0;
351 }
352
353 static bool die_is_signed_type(Dwarf_Die *tp_die)
354 {
355         Dwarf_Word ret;
356
357         if (die_get_attr_udata(tp_die, DW_AT_encoding, &ret))
358                 return false;
359
360         return (ret == DW_ATE_signed_char || ret == DW_ATE_signed ||
361                 ret == DW_ATE_signed_fixed);
362 }
363
364 /* Get data_member_location offset */
365 static int die_get_data_member_location(Dwarf_Die *mb_die, Dwarf_Word *offs)
366 {
367         Dwarf_Attribute attr;
368         Dwarf_Op *expr;
369         size_t nexpr;
370         int ret;
371
372         if (dwarf_attr(mb_die, DW_AT_data_member_location, &attr) == NULL)
373                 return -ENOENT;
374
375         if (dwarf_formudata(&attr, offs) != 0) {
376                 /* DW_AT_data_member_location should be DW_OP_plus_uconst */
377                 ret = dwarf_getlocation(&attr, &expr, &nexpr);
378                 if (ret < 0 || nexpr == 0)
379                         return -ENOENT;
380
381                 if (expr[0].atom != DW_OP_plus_uconst || nexpr != 1) {
382                         pr_debug("Unable to get offset:Unexpected OP %x (%zd)\n",
383                                  expr[0].atom, nexpr);
384                         return -ENOTSUP;
385                 }
386                 *offs = (Dwarf_Word)expr[0].number;
387         }
388         return 0;
389 }
390
391 /* Return values for die_find callbacks */
392 enum {
393         DIE_FIND_CB_END = 0,            /* End of Search */
394         DIE_FIND_CB_CHILD = 1,          /* Search only children */
395         DIE_FIND_CB_SIBLING = 2,        /* Search only siblings */
396         DIE_FIND_CB_CONTINUE = 3,       /* Search children and siblings */
397 };
398
399 /* Search a child die */
400 static Dwarf_Die *die_find_child(Dwarf_Die *rt_die,
401                                  int (*callback)(Dwarf_Die *, void *),
402                                  void *data, Dwarf_Die *die_mem)
403 {
404         Dwarf_Die child_die;
405         int ret;
406
407         ret = dwarf_child(rt_die, die_mem);
408         if (ret != 0)
409                 return NULL;
410
411         do {
412                 ret = callback(die_mem, data);
413                 if (ret == DIE_FIND_CB_END)
414                         return die_mem;
415
416                 if ((ret & DIE_FIND_CB_CHILD) &&
417                     die_find_child(die_mem, callback, data, &child_die)) {
418                         memcpy(die_mem, &child_die, sizeof(Dwarf_Die));
419                         return die_mem;
420                 }
421         } while ((ret & DIE_FIND_CB_SIBLING) &&
422                  dwarf_siblingof(die_mem, die_mem) == 0);
423
424         return NULL;
425 }
426
427 struct __addr_die_search_param {
428         Dwarf_Addr      addr;
429         Dwarf_Die       *die_mem;
430 };
431
432 static int __die_search_func_cb(Dwarf_Die *fn_die, void *data)
433 {
434         struct __addr_die_search_param *ad = data;
435
436         if (dwarf_tag(fn_die) == DW_TAG_subprogram &&
437             dwarf_haspc(fn_die, ad->addr)) {
438                 memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die));
439                 return DWARF_CB_ABORT;
440         }
441         return DWARF_CB_OK;
442 }
443
444 /* Search a real subprogram including this line, */
445 static Dwarf_Die *die_find_real_subprogram(Dwarf_Die *cu_die, Dwarf_Addr addr,
446                                            Dwarf_Die *die_mem)
447 {
448         struct __addr_die_search_param ad;
449         ad.addr = addr;
450         ad.die_mem = die_mem;
451         /* dwarf_getscopes can't find subprogram. */
452         if (!dwarf_getfuncs(cu_die, __die_search_func_cb, &ad, 0))
453                 return NULL;
454         else
455                 return die_mem;
456 }
457
458 /* die_find callback for inline function search */
459 static int __die_find_inline_cb(Dwarf_Die *die_mem, void *data)
460 {
461         Dwarf_Addr *addr = data;
462
463         if (dwarf_tag(die_mem) == DW_TAG_inlined_subroutine &&
464             dwarf_haspc(die_mem, *addr))
465                 return DIE_FIND_CB_END;
466
467         return DIE_FIND_CB_CONTINUE;
468 }
469
470 /* Similar to dwarf_getfuncs, but returns inlined_subroutine if exists. */
471 static Dwarf_Die *die_find_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
472                                       Dwarf_Die *die_mem)
473 {
474         Dwarf_Die tmp_die;
475
476         sp_die = die_find_child(sp_die, __die_find_inline_cb, &addr, &tmp_die);
477         if (!sp_die)
478                 return NULL;
479
480         /* Inlined function could be recursive. Trace it until fail */
481         while (sp_die) {
482                 memcpy(die_mem, sp_die, sizeof(Dwarf_Die));
483                 sp_die = die_find_child(sp_die, __die_find_inline_cb, &addr,
484                                         &tmp_die);
485         }
486
487         return die_mem;
488 }
489
490 /* Walker on lines (Note: line number will not be sorted) */
491 typedef int (* line_walk_handler_t) (const char *fname, int lineno,
492                                      Dwarf_Addr addr, void *data);
493
494 struct __line_walk_param {
495         const char *fname;
496         line_walk_handler_t handler;
497         void *data;
498         int retval;
499 };
500
501 static int __die_walk_funclines_cb(Dwarf_Die *in_die, void *data)
502 {
503         struct __line_walk_param *lw = data;
504         Dwarf_Addr addr;
505         int lineno;
506
507         if (dwarf_tag(in_die) == DW_TAG_inlined_subroutine) {
508                 lineno = die_get_call_lineno(in_die);
509                 if (lineno > 0 && dwarf_entrypc(in_die, &addr) == 0) {
510                         lw->retval = lw->handler(lw->fname, lineno, addr,
511                                                  lw->data);
512                         if (lw->retval != 0)
513                                 return DIE_FIND_CB_END;
514                 }
515         }
516         return DIE_FIND_CB_SIBLING;
517 }
518
519 /* Walk on lines of blocks included in given DIE */
520 static int __die_walk_funclines(Dwarf_Die *sp_die,
521                                 line_walk_handler_t handler, void *data)
522 {
523         struct __line_walk_param lw = {
524                 .handler = handler,
525                 .data = data,
526                 .retval = 0,
527         };
528         Dwarf_Die die_mem;
529         Dwarf_Addr addr;
530         int lineno;
531
532         /* Handle function declaration line */
533         lw.fname = dwarf_decl_file(sp_die);
534         if (lw.fname && dwarf_decl_line(sp_die, &lineno) == 0 &&
535             dwarf_entrypc(sp_die, &addr) == 0) {
536                 lw.retval = handler(lw.fname, lineno, addr, data);
537                 if (lw.retval != 0)
538                         goto done;
539         }
540         die_find_child(sp_die, __die_walk_funclines_cb, &lw, &die_mem);
541 done:
542         return lw.retval;
543 }
544
545 static int __die_walk_culines_cb(Dwarf_Die *sp_die, void *data)
546 {
547         struct __line_walk_param *lw = data;
548
549         lw->retval = __die_walk_funclines(sp_die, lw->handler, lw->data);
550         if (lw->retval != 0)
551                 return DWARF_CB_ABORT;
552
553         return DWARF_CB_OK;
554 }
555
556 /*
557  * Walk on lines inside given PDIE. If the PDIE is subprogram, walk only on
558  * the lines inside the subprogram, otherwise PDIE must be a CU DIE.
559  */
560 static int die_walk_lines(Dwarf_Die *pdie, line_walk_handler_t handler,
561                           void *data)
562 {
563         Dwarf_Lines *lines;
564         Dwarf_Line *line;
565         Dwarf_Addr addr;
566         const char *fname;
567         int lineno, ret = 0;
568         Dwarf_Die die_mem, *cu_die;
569         size_t nlines, i;
570
571         /* Get the CU die */
572         if (dwarf_tag(pdie) == DW_TAG_subprogram)
573                 cu_die = dwarf_diecu(pdie, &die_mem, NULL, NULL);
574         else
575                 cu_die = pdie;
576         if (!cu_die) {
577                 pr_debug2("Failed to get CU from subprogram\n");
578                 return -EINVAL;
579         }
580
581         /* Get lines list in the CU */
582         if (dwarf_getsrclines(cu_die, &lines, &nlines) != 0) {
583                 pr_debug2("Failed to get source lines on this CU.\n");
584                 return -ENOENT;
585         }
586         pr_debug2("Get %zd lines from this CU\n", nlines);
587
588         /* Walk on the lines on lines list */
589         for (i = 0; i < nlines; i++) {
590                 line = dwarf_onesrcline(lines, i);
591                 if (line == NULL ||
592                     dwarf_lineno(line, &lineno) != 0 ||
593                     dwarf_lineaddr(line, &addr) != 0) {
594                         pr_debug2("Failed to get line info. "
595                                   "Possible error in debuginfo.\n");
596                         continue;
597                 }
598                 /* Filter lines based on address */
599                 if (pdie != cu_die)
600                         /*
601                          * Address filtering
602                          * The line is included in given function, and
603                          * no inline block includes it.
604                          */
605                         if (!dwarf_haspc(pdie, addr) ||
606                             die_find_inlinefunc(pdie, addr, &die_mem))
607                                 continue;
608                 /* Get source line */
609                 fname = dwarf_linesrc(line, NULL, NULL);
610
611                 ret = handler(fname, lineno, addr, data);
612                 if (ret != 0)
613                         return ret;
614         }
615
616         /*
617          * Dwarf lines doesn't include function declarations and inlined
618          * subroutines. We have to check functions list or given function.
619          */
620         if (pdie != cu_die)
621                 ret = __die_walk_funclines(pdie, handler, data);
622         else {
623                 struct __line_walk_param param = {
624                         .handler = handler,
625                         .data = data,
626                         .retval = 0,
627                 };
628                 dwarf_getfuncs(cu_die, __die_walk_culines_cb, &param, 0);
629                 ret = param.retval;
630         }
631
632         return ret;
633 }
634
635 struct __find_variable_param {
636         const char *name;
637         Dwarf_Addr addr;
638 };
639
640 static int __die_find_variable_cb(Dwarf_Die *die_mem, void *data)
641 {
642         struct __find_variable_param *fvp = data;
643         int tag;
644
645         tag = dwarf_tag(die_mem);
646         if ((tag == DW_TAG_formal_parameter ||
647              tag == DW_TAG_variable) &&
648             die_compare_name(die_mem, fvp->name))
649                 return DIE_FIND_CB_END;
650
651         if (dwarf_haspc(die_mem, fvp->addr))
652                 return DIE_FIND_CB_CONTINUE;
653         else
654                 return DIE_FIND_CB_SIBLING;
655 }
656
657 /* Find a variable called 'name' at given address */
658 static Dwarf_Die *die_find_variable_at(Dwarf_Die *sp_die, const char *name,
659                                        Dwarf_Addr addr, Dwarf_Die *die_mem)
660 {
661         struct __find_variable_param fvp = { .name = name, .addr = addr};
662
663         return die_find_child(sp_die, __die_find_variable_cb, (void *)&fvp,
664                               die_mem);
665 }
666
667 static int __die_find_member_cb(Dwarf_Die *die_mem, void *data)
668 {
669         const char *name = data;
670
671         if ((dwarf_tag(die_mem) == DW_TAG_member) &&
672             die_compare_name(die_mem, name))
673                 return DIE_FIND_CB_END;
674
675         return DIE_FIND_CB_SIBLING;
676 }
677
678 /* Find a member called 'name' */
679 static Dwarf_Die *die_find_member(Dwarf_Die *st_die, const char *name,
680                                   Dwarf_Die *die_mem)
681 {
682         return die_find_child(st_die, __die_find_member_cb, (void *)name,
683                               die_mem);
684 }
685
686 /* Get the name of given variable DIE */
687 static int die_get_typename(Dwarf_Die *vr_die, char *buf, int len)
688 {
689         Dwarf_Die type;
690         int tag, ret, ret2;
691         const char *tmp = "";
692
693         if (__die_get_real_type(vr_die, &type) == NULL)
694                 return -ENOENT;
695
696         tag = dwarf_tag(&type);
697         if (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type)
698                 tmp = "*";
699         else if (tag == DW_TAG_subroutine_type) {
700                 /* Function pointer */
701                 ret = snprintf(buf, len, "(function_type)");
702                 return (ret >= len) ? -E2BIG : ret;
703         } else {
704                 if (!dwarf_diename(&type))
705                         return -ENOENT;
706                 if (tag == DW_TAG_union_type)
707                         tmp = "union ";
708                 else if (tag == DW_TAG_structure_type)
709                         tmp = "struct ";
710                 /* Write a base name */
711                 ret = snprintf(buf, len, "%s%s", tmp, dwarf_diename(&type));
712                 return (ret >= len) ? -E2BIG : ret;
713         }
714         ret = die_get_typename(&type, buf, len);
715         if (ret > 0) {
716                 ret2 = snprintf(buf + ret, len - ret, "%s", tmp);
717                 ret = (ret2 >= len - ret) ? -E2BIG : ret2 + ret;
718         }
719         return ret;
720 }
721
722 /* Get the name and type of given variable DIE, stored as "type\tname" */
723 static int die_get_varname(Dwarf_Die *vr_die, char *buf, int len)
724 {
725         int ret, ret2;
726
727         ret = die_get_typename(vr_die, buf, len);
728         if (ret < 0) {
729                 pr_debug("Failed to get type, make it unknown.\n");
730                 ret = snprintf(buf, len, "(unknown_type)");
731         }
732         if (ret > 0) {
733                 ret2 = snprintf(buf + ret, len - ret, "\t%s",
734                                 dwarf_diename(vr_die));
735                 ret = (ret2 >= len - ret) ? -E2BIG : ret2 + ret;
736         }
737         return ret;
738 }
739
740 /*
741  * Probe finder related functions
742  */
743
744 static struct probe_trace_arg_ref *alloc_trace_arg_ref(long offs)
745 {
746         struct probe_trace_arg_ref *ref;
747         ref = zalloc(sizeof(struct probe_trace_arg_ref));
748         if (ref != NULL)
749                 ref->offset = offs;
750         return ref;
751 }
752
753 /*
754  * Convert a location into trace_arg.
755  * If tvar == NULL, this just checks variable can be converted.
756  */
757 static int convert_variable_location(Dwarf_Die *vr_die, Dwarf_Addr addr,
758                                      Dwarf_Op *fb_ops,
759                                      struct probe_trace_arg *tvar)
760 {
761         Dwarf_Attribute attr;
762         Dwarf_Op *op;
763         size_t nops;
764         unsigned int regn;
765         Dwarf_Word offs = 0;
766         bool ref = false;
767         const char *regs;
768         int ret;
769
770         if (dwarf_attr(vr_die, DW_AT_external, &attr) != NULL)
771                 goto static_var;
772
773         /* TODO: handle more than 1 exprs */
774         if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL ||
775             dwarf_getlocation_addr(&attr, addr, &op, &nops, 1) <= 0 ||
776             nops == 0) {
777                 /* TODO: Support const_value */
778                 return -ENOENT;
779         }
780
781         if (op->atom == DW_OP_addr) {
782 static_var:
783                 if (!tvar)
784                         return 0;
785                 /* Static variables on memory (not stack), make @varname */
786                 ret = strlen(dwarf_diename(vr_die));
787                 tvar->value = zalloc(ret + 2);
788                 if (tvar->value == NULL)
789                         return -ENOMEM;
790                 snprintf(tvar->value, ret + 2, "@%s", dwarf_diename(vr_die));
791                 tvar->ref = alloc_trace_arg_ref((long)offs);
792                 if (tvar->ref == NULL)
793                         return -ENOMEM;
794                 return 0;
795         }
796
797         /* If this is based on frame buffer, set the offset */
798         if (op->atom == DW_OP_fbreg) {
799                 if (fb_ops == NULL)
800                         return -ENOTSUP;
801                 ref = true;
802                 offs = op->number;
803                 op = &fb_ops[0];
804         }
805
806         if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) {
807                 regn = op->atom - DW_OP_breg0;
808                 offs += op->number;
809                 ref = true;
810         } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) {
811                 regn = op->atom - DW_OP_reg0;
812         } else if (op->atom == DW_OP_bregx) {
813                 regn = op->number;
814                 offs += op->number2;
815                 ref = true;
816         } else if (op->atom == DW_OP_regx) {
817                 regn = op->number;
818         } else {
819                 pr_debug("DW_OP %x is not supported.\n", op->atom);
820                 return -ENOTSUP;
821         }
822
823         if (!tvar)
824                 return 0;
825
826         regs = get_arch_regstr(regn);
827         if (!regs) {
828                 /* This should be a bug in DWARF or this tool */
829                 pr_warning("Mapping for the register number %u "
830                            "missing on this architecture.\n", regn);
831                 return -ERANGE;
832         }
833
834         tvar->value = strdup(regs);
835         if (tvar->value == NULL)
836                 return -ENOMEM;
837
838         if (ref) {
839                 tvar->ref = alloc_trace_arg_ref((long)offs);
840                 if (tvar->ref == NULL)
841                         return -ENOMEM;
842         }
843         return 0;
844 }
845
846 #define BYTES_TO_BITS(nb)       ((nb) * BITS_PER_LONG / sizeof(long))
847
848 static int convert_variable_type(Dwarf_Die *vr_die,
849                                  struct probe_trace_arg *tvar,
850                                  const char *cast)
851 {
852         struct probe_trace_arg_ref **ref_ptr = &tvar->ref;
853         Dwarf_Die type;
854         char buf[16];
855         int bsize, boffs, total;
856         int ret;
857
858         /* TODO: check all types */
859         if (cast && strcmp(cast, "string") != 0) {
860                 /* Non string type is OK */
861                 tvar->type = strdup(cast);
862                 return (tvar->type == NULL) ? -ENOMEM : 0;
863         }
864
865         bsize = dwarf_bitsize(vr_die);
866         if (bsize > 0) {
867                 /* This is a bitfield */
868                 boffs = dwarf_bitoffset(vr_die);
869                 total = dwarf_bytesize(vr_die);
870                 if (boffs < 0 || total < 0)
871                         return -ENOENT;
872                 ret = snprintf(buf, 16, "b%d@%d/%zd", bsize, boffs,
873                                 BYTES_TO_BITS(total));
874                 goto formatted;
875         }
876
877         if (die_get_real_type(vr_die, &type) == NULL) {
878                 pr_warning("Failed to get a type information of %s.\n",
879                            dwarf_diename(vr_die));
880                 return -ENOENT;
881         }
882
883         pr_debug("%s type is %s.\n",
884                  dwarf_diename(vr_die), dwarf_diename(&type));
885
886         if (cast && strcmp(cast, "string") == 0) {      /* String type */
887                 ret = dwarf_tag(&type);
888                 if (ret != DW_TAG_pointer_type &&
889                     ret != DW_TAG_array_type) {
890                         pr_warning("Failed to cast into string: "
891                                    "%s(%s) is not a pointer nor array.\n",
892                                    dwarf_diename(vr_die), dwarf_diename(&type));
893                         return -EINVAL;
894                 }
895                 if (ret == DW_TAG_pointer_type) {
896                         if (die_get_real_type(&type, &type) == NULL) {
897                                 pr_warning("Failed to get a type"
898                                            " information.\n");
899                                 return -ENOENT;
900                         }
901                         while (*ref_ptr)
902                                 ref_ptr = &(*ref_ptr)->next;
903                         /* Add new reference with offset +0 */
904                         *ref_ptr = zalloc(sizeof(struct probe_trace_arg_ref));
905                         if (*ref_ptr == NULL) {
906                                 pr_warning("Out of memory error\n");
907                                 return -ENOMEM;
908                         }
909                 }
910                 if (!die_compare_name(&type, "char") &&
911                     !die_compare_name(&type, "unsigned char")) {
912                         pr_warning("Failed to cast into string: "
913                                    "%s is not (unsigned) char *.\n",
914                                    dwarf_diename(vr_die));
915                         return -EINVAL;
916                 }
917                 tvar->type = strdup(cast);
918                 return (tvar->type == NULL) ? -ENOMEM : 0;
919         }
920
921         ret = dwarf_bytesize(&type);
922         if (ret <= 0)
923                 /* No size ... try to use default type */
924                 return 0;
925         ret = BYTES_TO_BITS(ret);
926
927         /* Check the bitwidth */
928         if (ret > MAX_BASIC_TYPE_BITS) {
929                 pr_info("%s exceeds max-bitwidth. Cut down to %d bits.\n",
930                         dwarf_diename(&type), MAX_BASIC_TYPE_BITS);
931                 ret = MAX_BASIC_TYPE_BITS;
932         }
933         ret = snprintf(buf, 16, "%c%d",
934                        die_is_signed_type(&type) ? 's' : 'u', ret);
935
936 formatted:
937         if (ret < 0 || ret >= 16) {
938                 if (ret >= 16)
939                         ret = -E2BIG;
940                 pr_warning("Failed to convert variable type: %s\n",
941                            strerror(-ret));
942                 return ret;
943         }
944         tvar->type = strdup(buf);
945         if (tvar->type == NULL)
946                 return -ENOMEM;
947         return 0;
948 }
949
950 static int convert_variable_fields(Dwarf_Die *vr_die, const char *varname,
951                                     struct perf_probe_arg_field *field,
952                                     struct probe_trace_arg_ref **ref_ptr,
953                                     Dwarf_Die *die_mem)
954 {
955         struct probe_trace_arg_ref *ref = *ref_ptr;
956         Dwarf_Die type;
957         Dwarf_Word offs;
958         int ret, tag;
959
960         pr_debug("converting %s in %s\n", field->name, varname);
961         if (die_get_real_type(vr_die, &type) == NULL) {
962                 pr_warning("Failed to get the type of %s.\n", varname);
963                 return -ENOENT;
964         }
965         pr_debug2("Var real type: (%x)\n", (unsigned)dwarf_dieoffset(&type));
966         tag = dwarf_tag(&type);
967
968         if (field->name[0] == '[' &&
969             (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type)) {
970                 if (field->next)
971                         /* Save original type for next field */
972                         memcpy(die_mem, &type, sizeof(*die_mem));
973                 /* Get the type of this array */
974                 if (die_get_real_type(&type, &type) == NULL) {
975                         pr_warning("Failed to get the type of %s.\n", varname);
976                         return -ENOENT;
977                 }
978                 pr_debug2("Array real type: (%x)\n",
979                          (unsigned)dwarf_dieoffset(&type));
980                 if (tag == DW_TAG_pointer_type) {
981                         ref = zalloc(sizeof(struct probe_trace_arg_ref));
982                         if (ref == NULL)
983                                 return -ENOMEM;
984                         if (*ref_ptr)
985                                 (*ref_ptr)->next = ref;
986                         else
987                                 *ref_ptr = ref;
988                 }
989                 ref->offset += dwarf_bytesize(&type) * field->index;
990                 if (!field->next)
991                         /* Save vr_die for converting types */
992                         memcpy(die_mem, vr_die, sizeof(*die_mem));
993                 goto next;
994         } else if (tag == DW_TAG_pointer_type) {
995                 /* Check the pointer and dereference */
996                 if (!field->ref) {
997                         pr_err("Semantic error: %s must be referred by '->'\n",
998                                field->name);
999                         return -EINVAL;
1000                 }
1001                 /* Get the type pointed by this pointer */
1002                 if (die_get_real_type(&type, &type) == NULL) {
1003                         pr_warning("Failed to get the type of %s.\n", varname);
1004                         return -ENOENT;
1005                 }
1006                 /* Verify it is a data structure  */
1007                 if (dwarf_tag(&type) != DW_TAG_structure_type) {
1008                         pr_warning("%s is not a data structure.\n", varname);
1009                         return -EINVAL;
1010                 }
1011
1012                 ref = zalloc(sizeof(struct probe_trace_arg_ref));
1013                 if (ref == NULL)
1014                         return -ENOMEM;
1015                 if (*ref_ptr)
1016                         (*ref_ptr)->next = ref;
1017                 else
1018                         *ref_ptr = ref;
1019         } else {
1020                 /* Verify it is a data structure  */
1021                 if (tag != DW_TAG_structure_type) {
1022                         pr_warning("%s is not a data structure.\n", varname);
1023                         return -EINVAL;
1024                 }
1025                 if (field->name[0] == '[') {
1026                         pr_err("Semantic error: %s is not a pointor"
1027                                " nor array.\n", varname);
1028                         return -EINVAL;
1029                 }
1030                 if (field->ref) {
1031                         pr_err("Semantic error: %s must be referred by '.'\n",
1032                                field->name);
1033                         return -EINVAL;
1034                 }
1035                 if (!ref) {
1036                         pr_warning("Structure on a register is not "
1037                                    "supported yet.\n");
1038                         return -ENOTSUP;
1039                 }
1040         }
1041
1042         if (die_find_member(&type, field->name, die_mem) == NULL) {
1043                 pr_warning("%s(tyep:%s) has no member %s.\n", varname,
1044                            dwarf_diename(&type), field->name);
1045                 return -EINVAL;
1046         }
1047
1048         /* Get the offset of the field */
1049         ret = die_get_data_member_location(die_mem, &offs);
1050         if (ret < 0) {
1051                 pr_warning("Failed to get the offset of %s.\n", field->name);
1052                 return ret;
1053         }
1054         ref->offset += (long)offs;
1055
1056 next:
1057         /* Converting next field */
1058         if (field->next)
1059                 return convert_variable_fields(die_mem, field->name,
1060                                         field->next, &ref, die_mem);
1061         else
1062                 return 0;
1063 }
1064
1065 /* Show a variables in kprobe event format */
1066 static int convert_variable(Dwarf_Die *vr_die, struct probe_finder *pf)
1067 {
1068         Dwarf_Die die_mem;
1069         int ret;
1070
1071         pr_debug("Converting variable %s into trace event.\n",
1072                  dwarf_diename(vr_die));
1073
1074         ret = convert_variable_location(vr_die, pf->addr, pf->fb_ops,
1075                                         pf->tvar);
1076         if (ret == -ENOENT)
1077                 pr_err("Failed to find the location of %s at this address.\n"
1078                        " Perhaps, it has been optimized out.\n", pf->pvar->var);
1079         else if (ret == -ENOTSUP)
1080                 pr_err("Sorry, we don't support this variable location yet.\n");
1081         else if (pf->pvar->field) {
1082                 ret = convert_variable_fields(vr_die, pf->pvar->var,
1083                                               pf->pvar->field, &pf->tvar->ref,
1084                                               &die_mem);
1085                 vr_die = &die_mem;
1086         }
1087         if (ret == 0)
1088                 ret = convert_variable_type(vr_die, pf->tvar, pf->pvar->type);
1089         /* *expr will be cached in libdw. Don't free it. */
1090         return ret;
1091 }
1092
1093 /* Find a variable in a subprogram die */
1094 static int find_variable(Dwarf_Die *sp_die, struct probe_finder *pf)
1095 {
1096         Dwarf_Die vr_die, *scopes;
1097         char buf[32], *ptr;
1098         int ret, nscopes;
1099
1100         if (!is_c_varname(pf->pvar->var)) {
1101                 /* Copy raw parameters */
1102                 pf->tvar->value = strdup(pf->pvar->var);
1103                 if (pf->tvar->value == NULL)
1104                         return -ENOMEM;
1105                 if (pf->pvar->type) {
1106                         pf->tvar->type = strdup(pf->pvar->type);
1107                         if (pf->tvar->type == NULL)
1108                                 return -ENOMEM;
1109                 }
1110                 if (pf->pvar->name) {
1111                         pf->tvar->name = strdup(pf->pvar->name);
1112                         if (pf->tvar->name == NULL)
1113                                 return -ENOMEM;
1114                 } else
1115                         pf->tvar->name = NULL;
1116                 return 0;
1117         }
1118
1119         if (pf->pvar->name)
1120                 pf->tvar->name = strdup(pf->pvar->name);
1121         else {
1122                 ret = synthesize_perf_probe_arg(pf->pvar, buf, 32);
1123                 if (ret < 0)
1124                         return ret;
1125                 ptr = strchr(buf, ':'); /* Change type separator to _ */
1126                 if (ptr)
1127                         *ptr = '_';
1128                 pf->tvar->name = strdup(buf);
1129         }
1130         if (pf->tvar->name == NULL)
1131                 return -ENOMEM;
1132
1133         pr_debug("Searching '%s' variable in context.\n",
1134                  pf->pvar->var);
1135         /* Search child die for local variables and parameters. */
1136         if (die_find_variable_at(sp_die, pf->pvar->var, pf->addr, &vr_die))
1137                 ret = convert_variable(&vr_die, pf);
1138         else {
1139                 /* Search upper class */
1140                 nscopes = dwarf_getscopes_die(sp_die, &scopes);
1141                 while (nscopes-- > 1) {
1142                         pr_debug("Searching variables in %s\n",
1143                                  dwarf_diename(&scopes[nscopes]));
1144                         /* We should check this scope, so give dummy address */
1145                         if (die_find_variable_at(&scopes[nscopes],
1146                                                  pf->pvar->var, 0,
1147                                                  &vr_die)) {
1148                                 ret = convert_variable(&vr_die, pf);
1149                                 goto found;
1150                         }
1151                 }
1152                 if (scopes)
1153                         free(scopes);
1154                 ret = -ENOENT;
1155         }
1156 found:
1157         if (ret < 0)
1158                 pr_warning("Failed to find '%s' in this function.\n",
1159                            pf->pvar->var);
1160         return ret;
1161 }
1162
1163 /* Convert subprogram DIE to trace point */
1164 static int convert_to_trace_point(Dwarf_Die *sp_die, Dwarf_Addr paddr,
1165                                   bool retprobe, struct probe_trace_point *tp)
1166 {
1167         Dwarf_Addr eaddr;
1168         const char *name;
1169
1170         /* Copy the name of probe point */
1171         name = dwarf_diename(sp_die);
1172         if (name) {
1173                 if (dwarf_entrypc(sp_die, &eaddr) != 0) {
1174                         pr_warning("Failed to get entry address of %s\n",
1175                                    dwarf_diename(sp_die));
1176                         return -ENOENT;
1177                 }
1178                 tp->symbol = strdup(name);
1179                 if (tp->symbol == NULL)
1180                         return -ENOMEM;
1181                 tp->offset = (unsigned long)(paddr - eaddr);
1182         } else
1183                 /* This function has no name. */
1184                 tp->offset = (unsigned long)paddr;
1185
1186         /* Return probe must be on the head of a subprogram */
1187         if (retprobe) {
1188                 if (eaddr != paddr) {
1189                         pr_warning("Return probe must be on the head of"
1190                                    " a real function.\n");
1191                         return -EINVAL;
1192                 }
1193                 tp->retprobe = true;
1194         }
1195
1196         return 0;
1197 }
1198
1199 /* Call probe_finder callback with real subprogram DIE */
1200 static int call_probe_finder(Dwarf_Die *sp_die, struct probe_finder *pf)
1201 {
1202         Dwarf_Die die_mem;
1203         Dwarf_Attribute fb_attr;
1204         size_t nops;
1205         int ret;
1206
1207         /* If no real subprogram, find a real one */
1208         if (!sp_die || dwarf_tag(sp_die) != DW_TAG_subprogram) {
1209                 sp_die = die_find_real_subprogram(&pf->cu_die,
1210                                                   pf->addr, &die_mem);
1211                 if (!sp_die) {
1212                         pr_warning("Failed to find probe point in any "
1213                                    "functions.\n");
1214                         return -ENOENT;
1215                 }
1216         }
1217
1218         /* Get the frame base attribute/ops */
1219         dwarf_attr(sp_die, DW_AT_frame_base, &fb_attr);
1220         ret = dwarf_getlocation_addr(&fb_attr, pf->addr, &pf->fb_ops, &nops, 1);
1221         if (ret <= 0 || nops == 0) {
1222                 pf->fb_ops = NULL;
1223 #if _ELFUTILS_PREREQ(0, 142)
1224         } else if (nops == 1 && pf->fb_ops[0].atom == DW_OP_call_frame_cfa &&
1225                    pf->cfi != NULL) {
1226                 Dwarf_Frame *frame;
1227                 if (dwarf_cfi_addrframe(pf->cfi, pf->addr, &frame) != 0 ||
1228                     dwarf_frame_cfa(frame, &pf->fb_ops, &nops) != 0) {
1229                         pr_warning("Failed to get call frame on 0x%jx\n",
1230                                    (uintmax_t)pf->addr);
1231                         return -ENOENT;
1232                 }
1233 #endif
1234         }
1235
1236         /* Call finder's callback handler */
1237         ret = pf->callback(sp_die, pf);
1238
1239         /* *pf->fb_ops will be cached in libdw. Don't free it. */
1240         pf->fb_ops = NULL;
1241
1242         return ret;
1243 }
1244
1245 static int probe_point_line_walker(const char *fname, int lineno,
1246                                    Dwarf_Addr addr, void *data)
1247 {
1248         struct probe_finder *pf = data;
1249         int ret;
1250
1251         if (lineno != pf->lno || strtailcmp(fname, pf->fname) != 0)
1252                 return 0;
1253
1254         pf->addr = addr;
1255         ret = call_probe_finder(NULL, pf);
1256
1257         /* Continue if no error, because the line will be in inline function */
1258         return ret < 0 ? ret : 0;
1259 }
1260
1261 /* Find probe point from its line number */
1262 static int find_probe_point_by_line(struct probe_finder *pf)
1263 {
1264         return die_walk_lines(&pf->cu_die, probe_point_line_walker, pf);
1265 }
1266
1267 /* Find lines which match lazy pattern */
1268 static int find_lazy_match_lines(struct list_head *head,
1269                                  const char *fname, const char *pat)
1270 {
1271         FILE *fp;
1272         char *line = NULL;
1273         size_t line_len;
1274         ssize_t len;
1275         int count = 0, linenum = 1;
1276
1277         fp = fopen(fname, "r");
1278         if (!fp) {
1279                 pr_warning("Failed to open %s: %s\n", fname, strerror(errno));
1280                 return -errno;
1281         }
1282
1283         while ((len = getline(&line, &line_len, fp)) > 0) {
1284
1285                 if (line[len - 1] == '\n')
1286                         line[len - 1] = '\0';
1287
1288                 if (strlazymatch(line, pat)) {
1289                         line_list__add_line(head, linenum);
1290                         count++;
1291                 }
1292                 linenum++;
1293         }
1294
1295         if (ferror(fp))
1296                 count = -errno;
1297         free(line);
1298         fclose(fp);
1299
1300         if (count == 0)
1301                 pr_debug("No matched lines found in %s.\n", fname);
1302         return count;
1303 }
1304
1305 static int probe_point_lazy_walker(const char *fname, int lineno,
1306                                    Dwarf_Addr addr, void *data)
1307 {
1308         struct probe_finder *pf = data;
1309         int ret;
1310
1311         if (!line_list__has_line(&pf->lcache, lineno) ||
1312             strtailcmp(fname, pf->fname) != 0)
1313                 return 0;
1314
1315         pr_debug("Probe line found: line:%d addr:0x%llx\n",
1316                  lineno, (unsigned long long)addr);
1317         pf->addr = addr;
1318         ret = call_probe_finder(NULL, pf);
1319
1320         /*
1321          * Continue if no error, because the lazy pattern will match
1322          * to other lines
1323          */
1324         return ret < 0 ? ret : 0;
1325 }
1326
1327 /* Find probe points from lazy pattern  */
1328 static int find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf)
1329 {
1330         int ret = 0;
1331
1332         if (list_empty(&pf->lcache)) {
1333                 /* Matching lazy line pattern */
1334                 ret = find_lazy_match_lines(&pf->lcache, pf->fname,
1335                                             pf->pev->point.lazy_line);
1336                 if (ret <= 0)
1337                         return ret;
1338         }
1339
1340         return die_walk_lines(sp_die, probe_point_lazy_walker, pf);
1341 }
1342
1343 /* Callback parameter with return value */
1344 struct dwarf_callback_param {
1345         void *data;
1346         int retval;
1347 };
1348
1349 static int probe_point_inline_cb(Dwarf_Die *in_die, void *data)
1350 {
1351         struct dwarf_callback_param *param = data;
1352         struct probe_finder *pf = param->data;
1353         struct perf_probe_point *pp = &pf->pev->point;
1354         Dwarf_Addr addr;
1355
1356         if (pp->lazy_line)
1357                 param->retval = find_probe_point_lazy(in_die, pf);
1358         else {
1359                 /* Get probe address */
1360                 if (dwarf_entrypc(in_die, &addr) != 0) {
1361                         pr_warning("Failed to get entry address of %s.\n",
1362                                    dwarf_diename(in_die));
1363                         param->retval = -ENOENT;
1364                         return DWARF_CB_ABORT;
1365                 }
1366                 pf->addr = addr;
1367                 pf->addr += pp->offset;
1368                 pr_debug("found inline addr: 0x%jx\n",
1369                          (uintmax_t)pf->addr);
1370
1371                 param->retval = call_probe_finder(in_die, pf);
1372                 if (param->retval < 0)
1373                         return DWARF_CB_ABORT;
1374         }
1375
1376         return DWARF_CB_OK;
1377 }
1378
1379 /* Search function from function name */
1380 static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
1381 {
1382         struct dwarf_callback_param *param = data;
1383         struct probe_finder *pf = param->data;
1384         struct perf_probe_point *pp = &pf->pev->point;
1385
1386         /* Check tag and diename */
1387         if (dwarf_tag(sp_die) != DW_TAG_subprogram ||
1388             !die_compare_name(sp_die, pp->function))
1389                 return DWARF_CB_OK;
1390
1391         /* Check declared file */
1392         if (pp->file && strtailcmp(pp->file, dwarf_decl_file(sp_die)))
1393                 return DWARF_CB_OK;
1394
1395         pf->fname = dwarf_decl_file(sp_die);
1396         if (pp->line) { /* Function relative line */
1397                 dwarf_decl_line(sp_die, &pf->lno);
1398                 pf->lno += pp->line;
1399                 param->retval = find_probe_point_by_line(pf);
1400         } else if (!dwarf_func_inline(sp_die)) {
1401                 /* Real function */
1402                 if (pp->lazy_line)
1403                         param->retval = find_probe_point_lazy(sp_die, pf);
1404                 else {
1405                         if (dwarf_entrypc(sp_die, &pf->addr) != 0) {
1406                                 pr_warning("Failed to get entry address of "
1407                                            "%s.\n", dwarf_diename(sp_die));
1408                                 param->retval = -ENOENT;
1409                                 return DWARF_CB_ABORT;
1410                         }
1411                         pf->addr += pp->offset;
1412                         /* TODO: Check the address in this function */
1413                         param->retval = call_probe_finder(sp_die, pf);
1414                 }
1415         } else {
1416                 struct dwarf_callback_param _param = {.data = (void *)pf,
1417                                                       .retval = 0};
1418                 /* Inlined function: search instances */
1419                 dwarf_func_inline_instances(sp_die, probe_point_inline_cb,
1420                                             &_param);
1421                 param->retval = _param.retval;
1422         }
1423
1424         return DWARF_CB_ABORT; /* Exit; no same symbol in this CU. */
1425 }
1426
1427 static int find_probe_point_by_func(struct probe_finder *pf)
1428 {
1429         struct dwarf_callback_param _param = {.data = (void *)pf,
1430                                               .retval = 0};
1431         dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, &_param, 0);
1432         return _param.retval;
1433 }
1434
1435 struct pubname_callback_param {
1436         char *function;
1437         char *file;
1438         Dwarf_Die *cu_die;
1439         Dwarf_Die *sp_die;
1440         int found;
1441 };
1442
1443 static int pubname_search_cb(Dwarf *dbg, Dwarf_Global *gl, void *data)
1444 {
1445         struct pubname_callback_param *param = data;
1446
1447         if (dwarf_offdie(dbg, gl->die_offset, param->sp_die)) {
1448                 if (dwarf_tag(param->sp_die) != DW_TAG_subprogram)
1449                         return DWARF_CB_OK;
1450
1451                 if (die_compare_name(param->sp_die, param->function)) {
1452                         if (!dwarf_offdie(dbg, gl->cu_offset, param->cu_die))
1453                                 return DWARF_CB_OK;
1454
1455                         if (param->file &&
1456                             strtailcmp(param->file, dwarf_decl_file(param->sp_die)))
1457                                 return DWARF_CB_OK;
1458
1459                         param->found = 1;
1460                         return DWARF_CB_ABORT;
1461                 }
1462         }
1463
1464         return DWARF_CB_OK;
1465 }
1466
1467 /* Find probe points from debuginfo */
1468 static int find_probes(int fd, struct probe_finder *pf)
1469 {
1470         struct perf_probe_point *pp = &pf->pev->point;
1471         Dwarf_Off off, noff;
1472         size_t cuhl;
1473         Dwarf_Die *diep;
1474         Dwarf *dbg = NULL;
1475         Dwfl *dwfl;
1476         Dwarf_Addr bias;        /* Currently ignored */
1477         int ret = 0;
1478
1479         dbg = dwfl_init_offline_dwarf(fd, &dwfl, &bias);
1480         if (!dbg) {
1481                 pr_warning("No debug information found in the vmlinux - "
1482                         "please rebuild with CONFIG_DEBUG_INFO=y.\n");
1483                 close(fd);      /* Without dwfl_end(), fd isn't closed. */
1484                 return -EBADF;
1485         }
1486
1487 #if _ELFUTILS_PREREQ(0, 142)
1488         /* Get the call frame information from this dwarf */
1489         pf->cfi = dwarf_getcfi(dbg);
1490 #endif
1491
1492         off = 0;
1493         line_list__init(&pf->lcache);
1494
1495         /* Fastpath: lookup by function name from .debug_pubnames section */
1496         if (pp->function) {
1497                 struct pubname_callback_param pubname_param = {
1498                         .function = pp->function,
1499                         .file     = pp->file,
1500                         .cu_die   = &pf->cu_die,
1501                         .sp_die   = &pf->sp_die,
1502                         .found    = 0,
1503                 };
1504                 struct dwarf_callback_param probe_param = {
1505                         .data = pf,
1506                 };
1507
1508                 dwarf_getpubnames(dbg, pubname_search_cb, &pubname_param, 0);
1509                 if (pubname_param.found) {
1510                         ret = probe_point_search_cb(&pf->sp_die, &probe_param);
1511                         if (ret)
1512                                 goto found;
1513                 }
1514         }
1515
1516         /* Loop on CUs (Compilation Unit) */
1517         while (!dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL)) {
1518                 /* Get the DIE(Debugging Information Entry) of this CU */
1519                 diep = dwarf_offdie(dbg, off + cuhl, &pf->cu_die);
1520                 if (!diep)
1521                         continue;
1522
1523                 /* Check if target file is included. */
1524                 if (pp->file)
1525                         pf->fname = cu_find_realpath(&pf->cu_die, pp->file);
1526                 else
1527                         pf->fname = NULL;
1528
1529                 if (!pp->file || pf->fname) {
1530                         if (pp->function)
1531                                 ret = find_probe_point_by_func(pf);
1532                         else if (pp->lazy_line)
1533                                 ret = find_probe_point_lazy(NULL, pf);
1534                         else {
1535                                 pf->lno = pp->line;
1536                                 ret = find_probe_point_by_line(pf);
1537                         }
1538                         if (ret < 0)
1539                                 break;
1540                 }
1541                 off = noff;
1542         }
1543
1544 found:
1545         line_list__free(&pf->lcache);
1546         if (dwfl)
1547                 dwfl_end(dwfl);
1548
1549         return ret;
1550 }
1551
1552 /* Add a found probe point into trace event list */
1553 static int add_probe_trace_event(Dwarf_Die *sp_die, struct probe_finder *pf)
1554 {
1555         struct trace_event_finder *tf =
1556                         container_of(pf, struct trace_event_finder, pf);
1557         struct probe_trace_event *tev;
1558         int ret, i;
1559
1560         /* Check number of tevs */
1561         if (tf->ntevs == tf->max_tevs) {
1562                 pr_warning("Too many( > %d) probe point found.\n",
1563                            tf->max_tevs);
1564                 return -ERANGE;
1565         }
1566         tev = &tf->tevs[tf->ntevs++];
1567
1568         ret = convert_to_trace_point(sp_die, pf->addr, pf->pev->point.retprobe,
1569                                      &tev->point);
1570         if (ret < 0)
1571                 return ret;
1572
1573         pr_debug("Probe point found: %s+%lu\n", tev->point.symbol,
1574                  tev->point.offset);
1575
1576         /* Find each argument */
1577         tev->nargs = pf->pev->nargs;
1578         tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
1579         if (tev->args == NULL)
1580                 return -ENOMEM;
1581         for (i = 0; i < pf->pev->nargs; i++) {
1582                 pf->pvar = &pf->pev->args[i];
1583                 pf->tvar = &tev->args[i];
1584                 ret = find_variable(sp_die, pf);
1585                 if (ret != 0)
1586                         return ret;
1587         }
1588
1589         return 0;
1590 }
1591
1592 /* Find probe_trace_events specified by perf_probe_event from debuginfo */
1593 int find_probe_trace_events(int fd, struct perf_probe_event *pev,
1594                             struct probe_trace_event **tevs, int max_tevs)
1595 {
1596         struct trace_event_finder tf = {
1597                         .pf = {.pev = pev, .callback = add_probe_trace_event},
1598                         .max_tevs = max_tevs};
1599         int ret;
1600
1601         /* Allocate result tevs array */
1602         *tevs = zalloc(sizeof(struct probe_trace_event) * max_tevs);
1603         if (*tevs == NULL)
1604                 return -ENOMEM;
1605
1606         tf.tevs = *tevs;
1607         tf.ntevs = 0;
1608
1609         ret = find_probes(fd, &tf.pf);
1610         if (ret < 0) {
1611                 free(*tevs);
1612                 *tevs = NULL;
1613                 return ret;
1614         }
1615
1616         return (ret < 0) ? ret : tf.ntevs;
1617 }
1618
1619 #define MAX_VAR_LEN 64
1620
1621 /* Collect available variables in this scope */
1622 static int collect_variables_cb(Dwarf_Die *die_mem, void *data)
1623 {
1624         struct available_var_finder *af = data;
1625         struct variable_list *vl;
1626         char buf[MAX_VAR_LEN];
1627         int tag, ret;
1628
1629         vl = &af->vls[af->nvls - 1];
1630
1631         tag = dwarf_tag(die_mem);
1632         if (tag == DW_TAG_formal_parameter ||
1633             tag == DW_TAG_variable) {
1634                 ret = convert_variable_location(die_mem, af->pf.addr,
1635                                                 af->pf.fb_ops, NULL);
1636                 if (ret == 0) {
1637                         ret = die_get_varname(die_mem, buf, MAX_VAR_LEN);
1638                         pr_debug2("Add new var: %s\n", buf);
1639                         if (ret > 0)
1640                                 strlist__add(vl->vars, buf);
1641                 }
1642         }
1643
1644         if (af->child && dwarf_haspc(die_mem, af->pf.addr))
1645                 return DIE_FIND_CB_CONTINUE;
1646         else
1647                 return DIE_FIND_CB_SIBLING;
1648 }
1649
1650 /* Add a found vars into available variables list */
1651 static int add_available_vars(Dwarf_Die *sp_die, struct probe_finder *pf)
1652 {
1653         struct available_var_finder *af =
1654                         container_of(pf, struct available_var_finder, pf);
1655         struct variable_list *vl;
1656         Dwarf_Die die_mem, *scopes = NULL;
1657         int ret, nscopes;
1658
1659         /* Check number of tevs */
1660         if (af->nvls == af->max_vls) {
1661                 pr_warning("Too many( > %d) probe point found.\n", af->max_vls);
1662                 return -ERANGE;
1663         }
1664         vl = &af->vls[af->nvls++];
1665
1666         ret = convert_to_trace_point(sp_die, pf->addr, pf->pev->point.retprobe,
1667                                      &vl->point);
1668         if (ret < 0)
1669                 return ret;
1670
1671         pr_debug("Probe point found: %s+%lu\n", vl->point.symbol,
1672                  vl->point.offset);
1673
1674         /* Find local variables */
1675         vl->vars = strlist__new(true, NULL);
1676         if (vl->vars == NULL)
1677                 return -ENOMEM;
1678         af->child = true;
1679         die_find_child(sp_die, collect_variables_cb, (void *)af, &die_mem);
1680
1681         /* Find external variables */
1682         if (!af->externs)
1683                 goto out;
1684         /* Don't need to search child DIE for externs. */
1685         af->child = false;
1686         nscopes = dwarf_getscopes_die(sp_die, &scopes);
1687         while (nscopes-- > 1)
1688                 die_find_child(&scopes[nscopes], collect_variables_cb,
1689                                (void *)af, &die_mem);
1690         if (scopes)
1691                 free(scopes);
1692
1693 out:
1694         if (strlist__empty(vl->vars)) {
1695                 strlist__delete(vl->vars);
1696                 vl->vars = NULL;
1697         }
1698
1699         return ret;
1700 }
1701
1702 /* Find available variables at given probe point */
1703 int find_available_vars_at(int fd, struct perf_probe_event *pev,
1704                            struct variable_list **vls, int max_vls,
1705                            bool externs)
1706 {
1707         struct available_var_finder af = {
1708                         .pf = {.pev = pev, .callback = add_available_vars},
1709                         .max_vls = max_vls, .externs = externs};
1710         int ret;
1711
1712         /* Allocate result vls array */
1713         *vls = zalloc(sizeof(struct variable_list) * max_vls);
1714         if (*vls == NULL)
1715                 return -ENOMEM;
1716
1717         af.vls = *vls;
1718         af.nvls = 0;
1719
1720         ret = find_probes(fd, &af.pf);
1721         if (ret < 0) {
1722                 /* Free vlist for error */
1723                 while (af.nvls--) {
1724                         if (af.vls[af.nvls].point.symbol)
1725                                 free(af.vls[af.nvls].point.symbol);
1726                         if (af.vls[af.nvls].vars)
1727                                 strlist__delete(af.vls[af.nvls].vars);
1728                 }
1729                 free(af.vls);
1730                 *vls = NULL;
1731                 return ret;
1732         }
1733
1734         return (ret < 0) ? ret : af.nvls;
1735 }
1736
1737 /* Reverse search */
1738 int find_perf_probe_point(unsigned long addr, struct perf_probe_point *ppt)
1739 {
1740         Dwarf_Die cudie, spdie, indie;
1741         Dwarf *dbg = NULL;
1742         Dwfl *dwfl = NULL;
1743         Dwarf_Addr _addr, baseaddr, bias = 0;
1744         const char *fname = NULL, *func = NULL, *tmp;
1745         int baseline = 0, lineno = 0, ret = 0;
1746
1747         /* Open the live linux kernel */
1748         dbg = dwfl_init_live_kernel_dwarf(addr, &dwfl, &bias);
1749         if (!dbg) {
1750                 pr_warning("No debug information found in the vmlinux - "
1751                         "please rebuild with CONFIG_DEBUG_INFO=y.\n");
1752                 ret = -EINVAL;
1753                 goto end;
1754         }
1755
1756         /* Adjust address with bias */
1757         addr += bias;
1758         /* Find cu die */
1759         if (!dwarf_addrdie(dbg, (Dwarf_Addr)addr - bias, &cudie)) {
1760                 pr_warning("Failed to find debug information for address %lx\n",
1761                            addr);
1762                 ret = -EINVAL;
1763                 goto end;
1764         }
1765
1766         /* Find a corresponding line (filename and lineno) */
1767         cu_find_lineinfo(&cudie, addr, &fname, &lineno);
1768         /* Don't care whether it failed or not */
1769
1770         /* Find a corresponding function (name, baseline and baseaddr) */
1771         if (die_find_real_subprogram(&cudie, (Dwarf_Addr)addr, &spdie)) {
1772                 /* Get function entry information */
1773                 tmp = dwarf_diename(&spdie);
1774                 if (!tmp ||
1775                     dwarf_entrypc(&spdie, &baseaddr) != 0 ||
1776                     dwarf_decl_line(&spdie, &baseline) != 0)
1777                         goto post;
1778                 func = tmp;
1779
1780                 if (addr == (unsigned long)baseaddr)
1781                         /* Function entry - Relative line number is 0 */
1782                         lineno = baseline;
1783                 else if (die_find_inlinefunc(&spdie, (Dwarf_Addr)addr,
1784                                              &indie)) {
1785                         if (dwarf_entrypc(&indie, &_addr) == 0 &&
1786                             _addr == addr)
1787                                 /*
1788                                  * addr is at an inline function entry.
1789                                  * In this case, lineno should be the call-site
1790                                  * line number.
1791                                  */
1792                                 lineno = die_get_call_lineno(&indie);
1793                         else {
1794                                 /*
1795                                  * addr is in an inline function body.
1796                                  * Since lineno points one of the lines
1797                                  * of the inline function, baseline should
1798                                  * be the entry line of the inline function.
1799                                  */
1800                                 tmp = dwarf_diename(&indie);
1801                                 if (tmp &&
1802                                     dwarf_decl_line(&spdie, &baseline) == 0)
1803                                         func = tmp;
1804                         }
1805                 }
1806         }
1807
1808 post:
1809         /* Make a relative line number or an offset */
1810         if (lineno)
1811                 ppt->line = lineno - baseline;
1812         else if (func)
1813                 ppt->offset = addr - (unsigned long)baseaddr;
1814
1815         /* Duplicate strings */
1816         if (func) {
1817                 ppt->function = strdup(func);
1818                 if (ppt->function == NULL) {
1819                         ret = -ENOMEM;
1820                         goto end;
1821                 }
1822         }
1823         if (fname) {
1824                 ppt->file = strdup(fname);
1825                 if (ppt->file == NULL) {
1826                         if (ppt->function) {
1827                                 free(ppt->function);
1828                                 ppt->function = NULL;
1829                         }
1830                         ret = -ENOMEM;
1831                         goto end;
1832                 }
1833         }
1834 end:
1835         if (dwfl)
1836                 dwfl_end(dwfl);
1837         if (ret == 0 && (fname || func))
1838                 ret = 1;        /* Found a point */
1839         return ret;
1840 }
1841
1842 /* Add a line and store the src path */
1843 static int line_range_add_line(const char *src, unsigned int lineno,
1844                                struct line_range *lr)
1845 {
1846         /* Copy source path */
1847         if (!lr->path) {
1848                 lr->path = strdup(src);
1849                 if (lr->path == NULL)
1850                         return -ENOMEM;
1851         }
1852         return line_list__add_line(&lr->line_list, lineno);
1853 }
1854
1855 static int line_range_walk_cb(const char *fname, int lineno,
1856                               Dwarf_Addr addr __used,
1857                               void *data)
1858 {
1859         struct line_finder *lf = data;
1860
1861         if ((strtailcmp(fname, lf->fname) != 0) ||
1862             (lf->lno_s > lineno || lf->lno_e < lineno))
1863                 return 0;
1864
1865         if (line_range_add_line(fname, lineno, lf->lr) < 0)
1866                 return -EINVAL;
1867
1868         return 0;
1869 }
1870
1871 /* Find line range from its line number */
1872 static int find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
1873 {
1874         int ret;
1875
1876         ret = die_walk_lines(sp_die ?: &lf->cu_die, line_range_walk_cb, lf);
1877
1878         /* Update status */
1879         if (ret >= 0)
1880                 if (!list_empty(&lf->lr->line_list))
1881                         ret = lf->found = 1;
1882                 else
1883                         ret = 0;        /* Lines are not found */
1884         else {
1885                 free(lf->lr->path);
1886                 lf->lr->path = NULL;
1887         }
1888         return ret;
1889 }
1890
1891 static int line_range_inline_cb(Dwarf_Die *in_die, void *data)
1892 {
1893         struct dwarf_callback_param *param = data;
1894
1895         param->retval = find_line_range_by_line(in_die, param->data);
1896         return DWARF_CB_ABORT;  /* No need to find other instances */
1897 }
1898
1899 /* Search function from function name */
1900 static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
1901 {
1902         struct dwarf_callback_param *param = data;
1903         struct line_finder *lf = param->data;
1904         struct line_range *lr = lf->lr;
1905
1906         /* Check declared file */
1907         if (lr->file && strtailcmp(lr->file, dwarf_decl_file(sp_die)))
1908                 return DWARF_CB_OK;
1909
1910         if (dwarf_tag(sp_die) == DW_TAG_subprogram &&
1911             die_compare_name(sp_die, lr->function)) {
1912                 lf->fname = dwarf_decl_file(sp_die);
1913                 dwarf_decl_line(sp_die, &lr->offset);
1914                 pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
1915                 lf->lno_s = lr->offset + lr->start;
1916                 if (lf->lno_s < 0)      /* Overflow */
1917                         lf->lno_s = INT_MAX;
1918                 lf->lno_e = lr->offset + lr->end;
1919                 if (lf->lno_e < 0)      /* Overflow */
1920                         lf->lno_e = INT_MAX;
1921                 pr_debug("New line range: %d to %d\n", lf->lno_s, lf->lno_e);
1922                 lr->start = lf->lno_s;
1923                 lr->end = lf->lno_e;
1924                 if (dwarf_func_inline(sp_die)) {
1925                         struct dwarf_callback_param _param;
1926                         _param.data = (void *)lf;
1927                         _param.retval = 0;
1928                         dwarf_func_inline_instances(sp_die,
1929                                                     line_range_inline_cb,
1930                                                     &_param);
1931                         param->retval = _param.retval;
1932                 } else
1933                         param->retval = find_line_range_by_line(sp_die, lf);
1934                 return DWARF_CB_ABORT;
1935         }
1936         return DWARF_CB_OK;
1937 }
1938
1939 static int find_line_range_by_func(struct line_finder *lf)
1940 {
1941         struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
1942         dwarf_getfuncs(&lf->cu_die, line_range_search_cb, &param, 0);
1943         return param.retval;
1944 }
1945
1946 int find_line_range(int fd, struct line_range *lr)
1947 {
1948         struct line_finder lf = {.lr = lr, .found = 0};
1949         int ret = 0;
1950         Dwarf_Off off = 0, noff;
1951         size_t cuhl;
1952         Dwarf_Die *diep;
1953         Dwarf *dbg = NULL;
1954         Dwfl *dwfl;
1955         Dwarf_Addr bias;        /* Currently ignored */
1956         const char *comp_dir;
1957
1958         dbg = dwfl_init_offline_dwarf(fd, &dwfl, &bias);
1959         if (!dbg) {
1960                 pr_warning("No debug information found in the vmlinux - "
1961                         "please rebuild with CONFIG_DEBUG_INFO=y.\n");
1962                 close(fd);      /* Without dwfl_end(), fd isn't closed. */
1963                 return -EBADF;
1964         }
1965
1966         /* Fastpath: lookup by function name from .debug_pubnames section */
1967         if (lr->function) {
1968                 struct pubname_callback_param pubname_param = {
1969                         .function = lr->function, .file = lr->file,
1970                         .cu_die = &lf.cu_die, .sp_die = &lf.sp_die, .found = 0};
1971                 struct dwarf_callback_param line_range_param = {
1972                         .data = (void *)&lf, .retval = 0};
1973
1974                 dwarf_getpubnames(dbg, pubname_search_cb, &pubname_param, 0);
1975                 if (pubname_param.found) {
1976                         line_range_search_cb(&lf.sp_die, &line_range_param);
1977                         if (lf.found)
1978                                 goto found;
1979                 }
1980         }
1981
1982         /* Loop on CUs (Compilation Unit) */
1983         while (!lf.found && ret >= 0) {
1984                 if (dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL) != 0)
1985                         break;
1986
1987                 /* Get the DIE(Debugging Information Entry) of this CU */
1988                 diep = dwarf_offdie(dbg, off + cuhl, &lf.cu_die);
1989                 if (!diep)
1990                         continue;
1991
1992                 /* Check if target file is included. */
1993                 if (lr->file)
1994                         lf.fname = cu_find_realpath(&lf.cu_die, lr->file);
1995                 else
1996                         lf.fname = 0;
1997
1998                 if (!lr->file || lf.fname) {
1999                         if (lr->function)
2000                                 ret = find_line_range_by_func(&lf);
2001                         else {
2002                                 lf.lno_s = lr->start;
2003                                 lf.lno_e = lr->end;
2004                                 ret = find_line_range_by_line(NULL, &lf);
2005                         }
2006                 }
2007                 off = noff;
2008         }
2009
2010 found:
2011         /* Store comp_dir */
2012         if (lf.found) {
2013                 comp_dir = cu_get_comp_dir(&lf.cu_die);
2014                 if (comp_dir) {
2015                         lr->comp_dir = strdup(comp_dir);
2016                         if (!lr->comp_dir)
2017                                 ret = -ENOMEM;
2018                 }
2019         }
2020
2021         pr_debug("path: %s\n", lr->path);
2022         dwfl_end(dwfl);
2023         return (ret < 0) ? ret : lf.found;
2024 }
2025