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