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