perf probe: Fix to walk all inline instances
[linux-2.6.git] / tools / perf / util / probe-finder.c
index 37dcdb6..d6d5768 100644 (file)
@@ -33,7 +33,7 @@
 #include <ctype.h>
 #include <dwarf-regs.h>
 
-#include "string.h"
+#include <linux/bitops.h>
 #include "event.h"
 #include "debug.h"
 #include "util.h"
 /* Kprobe tracer basic type is up to u64 */
 #define MAX_BASIC_TYPE_BITS    64
 
-/*
- * Compare the tail of two strings.
- * Return 0 if whole of either string is same as another's tail part.
- */
-static int strtailcmp(const char *s1, const char *s2)
-{
-       int i1 = strlen(s1);
-       int i2 = strlen(s2);
-       while (--i1 >= 0 && --i2 >= 0) {
-               if (s1[i1] != s2[i2])
-                       return s1[i1] - s2[i2];
-       }
-       return 0;
-}
-
 /* Line number list operations */
 
 /* Add a line to line number list */
@@ -117,266 +102,183 @@ static void line_list__free(struct list_head *head)
        }
 }
 
-/* Dwarf wrappers */
-
-/* Find the realpath of the target file. */
-static const char *cu_find_realpath(Dwarf_Die *cu_die, const char *fname)
-{
-       Dwarf_Files *files;
-       size_t nfiles, i;
-       const char *src = NULL;
-       int ret;
-
-       if (!fname)
-               return NULL;
-
-       ret = dwarf_getsrcfiles(cu_die, &files, &nfiles);
-       if (ret != 0)
-               return NULL;
+/* Dwarf FL wrappers */
+static char *debuginfo_path;   /* Currently dummy */
 
-       for (i = 0; i < nfiles; i++) {
-               src = dwarf_filesrc(files, i, NULL, NULL);
-               if (strtailcmp(src, fname) == 0)
-                       break;
-       }
-       if (i == nfiles)
-               return NULL;
-       return src;
-}
+static const Dwfl_Callbacks offline_callbacks = {
+       .find_debuginfo = dwfl_standard_find_debuginfo,
+       .debuginfo_path = &debuginfo_path,
 
-/* Get DW_AT_comp_dir (should be NULL with older gcc) */
-static const char *cu_get_comp_dir(Dwarf_Die *cu_die)
-{
-       Dwarf_Attribute attr;
-       if (dwarf_attr(cu_die, DW_AT_comp_dir, &attr) == NULL)
-               return NULL;
-       return dwarf_formstring(&attr);
-}
+       .section_address = dwfl_offline_section_address,
 
-/* Compare diename and tname */
-static bool die_compare_name(Dwarf_Die *dw_die, const char *tname)
-{
-       const char *name;
-       name = dwarf_diename(dw_die);
-       return name ? strcmp(tname, name) : -1;
-}
+       /* We use this table for core files too.  */
+       .find_elf = dwfl_build_id_find_elf,
+};
 
-/* Get type die, but skip qualifiers and typedef */
-static Dwarf_Die *die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
+/* Get a Dwarf from offline image */
+static int debuginfo__init_offline_dwarf(struct debuginfo *self,
+                                        const char *path)
 {
-       Dwarf_Attribute attr;
-       int tag;
-
-       do {
-               if (dwarf_attr(vr_die, DW_AT_type, &attr) == NULL ||
-                   dwarf_formref_die(&attr, die_mem) == NULL)
-                       return NULL;
-
-               tag = dwarf_tag(die_mem);
-               vr_die = die_mem;
-       } while (tag == DW_TAG_const_type ||
-                tag == DW_TAG_restrict_type ||
-                tag == DW_TAG_volatile_type ||
-                tag == DW_TAG_shared_type ||
-                tag == DW_TAG_typedef);
-
-       return die_mem;
-}
+       Dwfl_Module *mod;
+       int fd;
 
-static bool die_is_signed_type(Dwarf_Die *tp_die)
-{
-       Dwarf_Attribute attr;
-       Dwarf_Word ret;
+       fd = open(path, O_RDONLY);
+       if (fd < 0)
+               return fd;
 
-       if (dwarf_attr(tp_die, DW_AT_encoding, &attr) == NULL ||
-           dwarf_formudata(&attr, &ret) != 0)
-               return false;
+       self->dwfl = dwfl_begin(&offline_callbacks);
+       if (!self->dwfl)
+               goto error;
 
-       return (ret == DW_ATE_signed_char || ret == DW_ATE_signed ||
-               ret == DW_ATE_signed_fixed);
-}
+       mod = dwfl_report_offline(self->dwfl, "", "", fd);
+       if (!mod)
+               goto error;
 
-static int die_get_byte_size(Dwarf_Die *tp_die)
-{
-       Dwarf_Attribute attr;
-       Dwarf_Word ret;
+       self->dbg = dwfl_module_getdwarf(mod, &self->bias);
+       if (!self->dbg)
+               goto error;
 
-       if (dwarf_attr(tp_die, DW_AT_byte_size, &attr) == NULL ||
-           dwarf_formudata(&attr, &ret) != 0)
-               return 0;
+       return 0;
+error:
+       if (self->dwfl)
+               dwfl_end(self->dwfl);
+       else
+               close(fd);
+       memset(self, 0, sizeof(*self));
 
-       return (int)ret;
+       return -ENOENT;
 }
 
-/* Get data_member_location offset */
-static int die_get_data_member_location(Dwarf_Die *mb_die, Dwarf_Word *offs)
+#if _ELFUTILS_PREREQ(0, 148)
+/* This method is buggy if elfutils is older than 0.148 */
+static int __linux_kernel_find_elf(Dwfl_Module *mod,
+                                  void **userdata,
+                                  const char *module_name,
+                                  Dwarf_Addr base,
+                                  char **file_name, Elf **elfp)
 {
-       Dwarf_Attribute attr;
-       Dwarf_Op *expr;
-       size_t nexpr;
-       int ret;
-
-       if (dwarf_attr(mb_die, DW_AT_data_member_location, &attr) == NULL)
-               return -ENOENT;
-
-       if (dwarf_formudata(&attr, offs) != 0) {
-               /* DW_AT_data_member_location should be DW_OP_plus_uconst */
-               ret = dwarf_getlocation(&attr, &expr, &nexpr);
-               if (ret < 0 || nexpr == 0)
-                       return -ENOENT;
-
-               if (expr[0].atom != DW_OP_plus_uconst || nexpr != 1) {
-                       pr_debug("Unable to get offset:Unexpected OP %x (%zd)\n",
-                                expr[0].atom, nexpr);
-                       return -ENOTSUP;
+       int fd;
+       const char *path = kernel_get_module_path(module_name);
+
+       pr_debug2("Use file %s for %s\n", path, module_name);
+       if (path) {
+               fd = open(path, O_RDONLY);
+               if (fd >= 0) {
+                       *file_name = strdup(path);
+                       return fd;
                }
-               *offs = (Dwarf_Word)expr[0].number;
        }
-       return 0;
+       /* If failed, try to call standard method */
+       return dwfl_linux_kernel_find_elf(mod, userdata, module_name, base,
+                                         file_name, elfp);
 }
 
-/* Return values for die_find callbacks */
-enum {
-       DIE_FIND_CB_FOUND = 0,          /* End of Search */
-       DIE_FIND_CB_CHILD = 1,          /* Search only children */
-       DIE_FIND_CB_SIBLING = 2,        /* Search only siblings */
-       DIE_FIND_CB_CONTINUE = 3,       /* Search children and siblings */
-};
-
-/* Search a child die */
-static Dwarf_Die *die_find_child(Dwarf_Die *rt_die,
-                                int (*callback)(Dwarf_Die *, void *),
-                                void *data, Dwarf_Die *die_mem)
-{
-       Dwarf_Die child_die;
-       int ret;
-
-       ret = dwarf_child(rt_die, die_mem);
-       if (ret != 0)
-               return NULL;
-
-       do {
-               ret = callback(die_mem, data);
-               if (ret == DIE_FIND_CB_FOUND)
-                       return die_mem;
+static const Dwfl_Callbacks kernel_callbacks = {
+       .find_debuginfo = dwfl_standard_find_debuginfo,
+       .debuginfo_path = &debuginfo_path,
 
-               if ((ret & DIE_FIND_CB_CHILD) &&
-                   die_find_child(die_mem, callback, data, &child_die)) {
-                       memcpy(die_mem, &child_die, sizeof(Dwarf_Die));
-                       return die_mem;
-               }
-       } while ((ret & DIE_FIND_CB_SIBLING) &&
-                dwarf_siblingof(die_mem, die_mem) == 0);
-
-       return NULL;
-}
-
-struct __addr_die_search_param {
-       Dwarf_Addr      addr;
-       Dwarf_Die       *die_mem;
+       .find_elf = __linux_kernel_find_elf,
+       .section_address = dwfl_linux_kernel_module_section_address,
 };
 
-static int __die_search_func_cb(Dwarf_Die *fn_die, void *data)
+/* Get a Dwarf from live kernel image */
+static int debuginfo__init_online_kernel_dwarf(struct debuginfo *self,
+                                              Dwarf_Addr addr)
 {
-       struct __addr_die_search_param *ad = data;
+       self->dwfl = dwfl_begin(&kernel_callbacks);
+       if (!self->dwfl)
+               return -EINVAL;
 
-       if (dwarf_tag(fn_die) == DW_TAG_subprogram &&
-           dwarf_haspc(fn_die, ad->addr)) {
-               memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die));
-               return DWARF_CB_ABORT;
+       /* Load the kernel dwarves: Don't care the result here */
+       dwfl_linux_kernel_report_kernel(self->dwfl);
+       dwfl_linux_kernel_report_modules(self->dwfl);
+
+       self->dbg = dwfl_addrdwarf(self->dwfl, addr, &self->bias);
+       /* Here, check whether we could get a real dwarf */
+       if (!self->dbg) {
+               pr_debug("Failed to find kernel dwarf at %lx\n",
+                        (unsigned long)addr);
+               dwfl_end(self->dwfl);
+               memset(self, 0, sizeof(*self));
+               return -ENOENT;
        }
-       return DWARF_CB_OK;
-}
 
-/* Search a real subprogram including this line, */
-static Dwarf_Die *die_find_real_subprogram(Dwarf_Die *cu_die, Dwarf_Addr addr,
-                                          Dwarf_Die *die_mem)
-{
-       struct __addr_die_search_param ad;
-       ad.addr = addr;
-       ad.die_mem = die_mem;
-       /* dwarf_getscopes can't find subprogram. */
-       if (!dwarf_getfuncs(cu_die, __die_search_func_cb, &ad, 0))
-               return NULL;
-       else
-               return die_mem;
+       return 0;
 }
-
-/* die_find callback for inline function search */
-static int __die_find_inline_cb(Dwarf_Die *die_mem, void *data)
+#else
+/* With older elfutils, this just support kernel module... */
+static int debuginfo__init_online_kernel_dwarf(struct debuginfo *self,
+                                              Dwarf_Addr addr __used)
 {
-       Dwarf_Addr *addr = data;
-
-       if (dwarf_tag(die_mem) == DW_TAG_inlined_subroutine &&
-           dwarf_haspc(die_mem, *addr))
-               return DIE_FIND_CB_FOUND;
+       const char *path = kernel_get_module_path("kernel");
 
-       return DIE_FIND_CB_CONTINUE;
-}
+       if (!path) {
+               pr_err("Failed to find vmlinux path\n");
+               return -ENOENT;
+       }
 
-/* Similar to dwarf_getfuncs, but returns inlined_subroutine if exists. */
-static Dwarf_Die *die_find_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
-                                     Dwarf_Die *die_mem)
-{
-       return die_find_child(sp_die, __die_find_inline_cb, &addr, die_mem);
+       pr_debug2("Use file %s for debuginfo\n", path);
+       return debuginfo__init_offline_dwarf(self, path);
 }
+#endif
 
-static int __die_find_variable_cb(Dwarf_Die *die_mem, void *data)
+struct debuginfo *debuginfo__new(const char *path)
 {
-       const char *name = data;
-       int tag;
-
-       tag = dwarf_tag(die_mem);
-       if ((tag == DW_TAG_formal_parameter ||
-            tag == DW_TAG_variable) &&
-           (die_compare_name(die_mem, name) == 0))
-               return DIE_FIND_CB_FOUND;
+       struct debuginfo *self = zalloc(sizeof(struct debuginfo));
+       if (!self)
+               return NULL;
 
-       return DIE_FIND_CB_CONTINUE;
-}
+       if (debuginfo__init_offline_dwarf(self, path) < 0) {
+               free(self);
+               self = NULL;
+       }
 
-/* Find a variable called 'name' */
-static Dwarf_Die *die_find_variable(Dwarf_Die *sp_die, const char *name,
-                                   Dwarf_Die *die_mem)
-{
-       return die_find_child(sp_die, __die_find_variable_cb, (void *)name,
-                             die_mem);
+       return self;
 }
 
-static int __die_find_member_cb(Dwarf_Die *die_mem, void *data)
+struct debuginfo *debuginfo__new_online_kernel(unsigned long addr)
 {
-       const char *name = data;
+       struct debuginfo *self = zalloc(sizeof(struct debuginfo));
+       if (!self)
+               return NULL;
 
-       if ((dwarf_tag(die_mem) == DW_TAG_member) &&
-           (die_compare_name(die_mem, name) == 0))
-               return DIE_FIND_CB_FOUND;
+       if (debuginfo__init_online_kernel_dwarf(self, (Dwarf_Addr)addr) < 0) {
+               free(self);
+               self = NULL;
+       }
 
-       return DIE_FIND_CB_SIBLING;
+       return self;
 }
 
-/* Find a member called 'name' */
-static Dwarf_Die *die_find_member(Dwarf_Die *st_die, const char *name,
-                                 Dwarf_Die *die_mem)
+void debuginfo__delete(struct debuginfo *self)
 {
-       return die_find_child(st_die, __die_find_member_cb, (void *)name,
-                             die_mem);
+       if (self) {
+               if (self->dwfl)
+                       dwfl_end(self->dwfl);
+               free(self);
+       }
 }
 
 /*
  * Probe finder related functions
  */
 
-static struct kprobe_trace_arg_ref *alloc_trace_arg_ref(long offs)
+static struct probe_trace_arg_ref *alloc_trace_arg_ref(long offs)
 {
-       struct kprobe_trace_arg_ref *ref;
-       ref = zalloc(sizeof(struct kprobe_trace_arg_ref));
+       struct probe_trace_arg_ref *ref;
+       ref = zalloc(sizeof(struct probe_trace_arg_ref));
        if (ref != NULL)
                ref->offset = offs;
        return ref;
 }
 
-/* Show a location */
-static int convert_variable_location(Dwarf_Die *vr_die, struct probe_finder *pf)
+/*
+ * Convert a location into trace_arg.
+ * If tvar == NULL, this just checks variable can be converted.
+ */
+static int convert_variable_location(Dwarf_Die *vr_die, Dwarf_Addr addr,
+                                    Dwarf_Op *fb_ops,
+                                    struct probe_trace_arg *tvar)
 {
        Dwarf_Attribute attr;
        Dwarf_Op *op;
@@ -385,20 +287,23 @@ static int convert_variable_location(Dwarf_Die *vr_die, struct probe_finder *pf)
        Dwarf_Word offs = 0;
        bool ref = false;
        const char *regs;
-       struct kprobe_trace_arg *tvar = pf->tvar;
        int ret;
 
+       if (dwarf_attr(vr_die, DW_AT_external, &attr) != NULL)
+               goto static_var;
+
        /* TODO: handle more than 1 exprs */
        if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL ||
-           dwarf_getlocation_addr(&attr, pf->addr, &op, &nops, 1) <= 0 ||
+           dwarf_getlocation_addr(&attr, addr, &op, &nops, 1) <= 0 ||
            nops == 0) {
                /* TODO: Support const_value */
-               pr_err("Failed to find the location of %s at this address.\n"
-                      " Perhaps, it has been optimized out.\n", pf->pvar->var);
                return -ENOENT;
        }
 
        if (op->atom == DW_OP_addr) {
+static_var:
+               if (!tvar)
+                       return 0;
                /* Static variables on memory (not stack), make @varname */
                ret = strlen(dwarf_diename(vr_die));
                tvar->value = zalloc(ret + 2);
@@ -413,14 +318,11 @@ static int convert_variable_location(Dwarf_Die *vr_die, struct probe_finder *pf)
 
        /* If this is based on frame buffer, set the offset */
        if (op->atom == DW_OP_fbreg) {
-               if (pf->fb_ops == NULL) {
-                       pr_warning("The attribute of frame base is not "
-                                  "supported.\n");
+               if (fb_ops == NULL)
                        return -ENOTSUP;
-               }
                ref = true;
                offs = op->number;
-               op = &pf->fb_ops[0];
+               op = &fb_ops[0];
        }
 
        if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) {
@@ -436,13 +338,18 @@ static int convert_variable_location(Dwarf_Die *vr_die, struct probe_finder *pf)
        } else if (op->atom == DW_OP_regx) {
                regn = op->number;
        } else {
-               pr_warning("DW_OP %x is not supported.\n", op->atom);
+               pr_debug("DW_OP %x is not supported.\n", op->atom);
                return -ENOTSUP;
        }
 
+       if (!tvar)
+               return 0;
+
        regs = get_arch_regstr(regn);
        if (!regs) {
-               pr_warning("Mapping for DWARF register number %u missing on this architecture.", regn);
+               /* This should be a bug in DWARF or this tool */
+               pr_warning("Mapping for the register number %u "
+                          "missing on this architecture.\n", regn);
                return -ERANGE;
        }
 
@@ -458,13 +365,16 @@ static int convert_variable_location(Dwarf_Die *vr_die, struct probe_finder *pf)
        return 0;
 }
 
+#define BYTES_TO_BITS(nb)      ((nb) * BITS_PER_LONG / sizeof(long))
+
 static int convert_variable_type(Dwarf_Die *vr_die,
-                                struct kprobe_trace_arg *tvar,
+                                struct probe_trace_arg *tvar,
                                 const char *cast)
 {
-       struct kprobe_trace_arg_ref **ref_ptr = &tvar->ref;
+       struct probe_trace_arg_ref **ref_ptr = &tvar->ref;
        Dwarf_Die type;
        char buf[16];
+       int bsize, boffs, total;
        int ret;
 
        /* TODO: check all types */
@@ -474,6 +384,18 @@ static int convert_variable_type(Dwarf_Die *vr_die,
                return (tvar->type == NULL) ? -ENOMEM : 0;
        }
 
+       bsize = dwarf_bitsize(vr_die);
+       if (bsize > 0) {
+               /* This is a bitfield */
+               boffs = dwarf_bitoffset(vr_die);
+               total = dwarf_bytesize(vr_die);
+               if (boffs < 0 || total < 0)
+                       return -ENOENT;
+               ret = snprintf(buf, 16, "b%d@%d/%zd", bsize, boffs,
+                               BYTES_TO_BITS(total));
+               goto formatted;
+       }
+
        if (die_get_real_type(vr_die, &type) == NULL) {
                pr_warning("Failed to get a type information of %s.\n",
                           dwarf_diename(vr_die));
@@ -488,28 +410,29 @@ static int convert_variable_type(Dwarf_Die *vr_die,
                if (ret != DW_TAG_pointer_type &&
                    ret != DW_TAG_array_type) {
                        pr_warning("Failed to cast into string: "
-                                  "%s(%s) is not a pointer nor array.",
+                                  "%s(%s) is not a pointer nor array.\n",
                                   dwarf_diename(vr_die), dwarf_diename(&type));
                        return -EINVAL;
                }
                if (ret == DW_TAG_pointer_type) {
                        if (die_get_real_type(&type, &type) == NULL) {
-                               pr_warning("Failed to get a type information.");
+                               pr_warning("Failed to get a type"
+                                          " information.\n");
                                return -ENOENT;
                        }
                        while (*ref_ptr)
                                ref_ptr = &(*ref_ptr)->next;
                        /* Add new reference with offset +0 */
-                       *ref_ptr = zalloc(sizeof(struct kprobe_trace_arg_ref));
+                       *ref_ptr = zalloc(sizeof(struct probe_trace_arg_ref));
                        if (*ref_ptr == NULL) {
                                pr_warning("Out of memory error\n");
                                return -ENOMEM;
                        }
                }
-               if (die_compare_name(&type, "char") != 0 &&
-                   die_compare_name(&type, "unsigned char") != 0) {
+               if (!die_compare_name(&type, "char") &&
+                   !die_compare_name(&type, "unsigned char")) {
                        pr_warning("Failed to cast into string: "
-                                  "%s is not (unsigned) char *.",
+                                  "%s is not (unsigned) char *.\n",
                                   dwarf_diename(vr_die));
                        return -EINVAL;
                }
@@ -517,38 +440,41 @@ static int convert_variable_type(Dwarf_Die *vr_die,
                return (tvar->type == NULL) ? -ENOMEM : 0;
        }
 
-       ret = die_get_byte_size(&type) * 8;
-       if (ret) {
-               /* Check the bitwidth */
-               if (ret > MAX_BASIC_TYPE_BITS) {
-                       pr_info("%s exceeds max-bitwidth."
-                               " Cut down to %d bits.\n",
-                               dwarf_diename(&type), MAX_BASIC_TYPE_BITS);
-                       ret = MAX_BASIC_TYPE_BITS;
-               }
+       ret = dwarf_bytesize(&type);
+       if (ret <= 0)
+               /* No size ... try to use default type */
+               return 0;
+       ret = BYTES_TO_BITS(ret);
 
-               ret = snprintf(buf, 16, "%c%d",
-                              die_is_signed_type(&type) ? 's' : 'u', ret);
-               if (ret < 0 || ret >= 16) {
-                       if (ret >= 16)
-                               ret = -E2BIG;
-                       pr_warning("Failed to convert variable type: %s\n",
-                                  strerror(-ret));
-                       return ret;
-               }
-               tvar->type = strdup(buf);
-               if (tvar->type == NULL)
-                       return -ENOMEM;
+       /* Check the bitwidth */
+       if (ret > MAX_BASIC_TYPE_BITS) {
+               pr_info("%s exceeds max-bitwidth. Cut down to %d bits.\n",
+                       dwarf_diename(&type), MAX_BASIC_TYPE_BITS);
+               ret = MAX_BASIC_TYPE_BITS;
+       }
+       ret = snprintf(buf, 16, "%c%d",
+                      die_is_signed_type(&type) ? 's' : 'u', ret);
+
+formatted:
+       if (ret < 0 || ret >= 16) {
+               if (ret >= 16)
+                       ret = -E2BIG;
+               pr_warning("Failed to convert variable type: %s\n",
+                          strerror(-ret));
+               return ret;
        }
+       tvar->type = strdup(buf);
+       if (tvar->type == NULL)
+               return -ENOMEM;
        return 0;
 }
 
 static int convert_variable_fields(Dwarf_Die *vr_die, const char *varname,
                                    struct perf_probe_arg_field *field,
-                                   struct kprobe_trace_arg_ref **ref_ptr,
+                                   struct probe_trace_arg_ref **ref_ptr,
                                    Dwarf_Die *die_mem)
 {
-       struct kprobe_trace_arg_ref *ref = *ref_ptr;
+       struct probe_trace_arg_ref *ref = *ref_ptr;
        Dwarf_Die type;
        Dwarf_Word offs;
        int ret, tag;
@@ -574,7 +500,7 @@ static int convert_variable_fields(Dwarf_Die *vr_die, const char *varname,
                pr_debug2("Array real type: (%x)\n",
                         (unsigned)dwarf_dieoffset(&type));
                if (tag == DW_TAG_pointer_type) {
-                       ref = zalloc(sizeof(struct kprobe_trace_arg_ref));
+                       ref = zalloc(sizeof(struct probe_trace_arg_ref));
                        if (ref == NULL)
                                return -ENOMEM;
                        if (*ref_ptr)
@@ -582,7 +508,7 @@ static int convert_variable_fields(Dwarf_Die *vr_die, const char *varname,
                        else
                                *ref_ptr = ref;
                }
-               ref->offset += die_get_byte_size(&type) * field->index;
+               ref->offset += dwarf_bytesize(&type) * field->index;
                if (!field->next)
                        /* Save vr_die for converting types */
                        memcpy(die_mem, vr_die, sizeof(*die_mem));
@@ -605,7 +531,7 @@ static int convert_variable_fields(Dwarf_Die *vr_die, const char *varname,
                        return -EINVAL;
                }
 
-               ref = zalloc(sizeof(struct kprobe_trace_arg_ref));
+               ref = zalloc(sizeof(struct probe_trace_arg_ref));
                if (ref == NULL)
                        return -ENOMEM;
                if (*ref_ptr)
@@ -619,8 +545,8 @@ static int convert_variable_fields(Dwarf_Die *vr_die, const char *varname,
                        return -EINVAL;
                }
                if (field->name[0] == '[') {
-                       pr_err("Semantic error: %s is not a pointor nor array.",
-                              varname);
+                       pr_err("Semantic error: %s is not a pointor"
+                              " nor array.\n", varname);
                        return -EINVAL;
                }
                if (field->ref) {
@@ -667,8 +593,14 @@ static int convert_variable(Dwarf_Die *vr_die, struct probe_finder *pf)
        pr_debug("Converting variable %s into trace event.\n",
                 dwarf_diename(vr_die));
 
-       ret = convert_variable_location(vr_die, pf);
-       if (ret == 0 && pf->pvar->field) {
+       ret = convert_variable_location(vr_die, pf->addr, pf->fb_ops,
+                                       pf->tvar);
+       if (ret == -ENOENT)
+               pr_err("Failed to find the location of %s at this address.\n"
+                      " Perhaps, it has been optimized out.\n", pf->pvar->var);
+       else if (ret == -ENOTSUP)
+               pr_err("Sorry, we don't support this variable location yet.\n");
+       else if (pf->pvar->field) {
                ret = convert_variable_fields(vr_die, pf->pvar->var,
                                              pf->pvar->field, &pf->tvar->ref,
                                              &die_mem);
@@ -687,6 +619,25 @@ static int find_variable(Dwarf_Die *sp_die, struct probe_finder *pf)
        char buf[32], *ptr;
        int ret, nscopes;
 
+       if (!is_c_varname(pf->pvar->var)) {
+               /* Copy raw parameters */
+               pf->tvar->value = strdup(pf->pvar->var);
+               if (pf->tvar->value == NULL)
+                       return -ENOMEM;
+               if (pf->pvar->type) {
+                       pf->tvar->type = strdup(pf->pvar->type);
+                       if (pf->tvar->type == NULL)
+                               return -ENOMEM;
+               }
+               if (pf->pvar->name) {
+                       pf->tvar->name = strdup(pf->pvar->name);
+                       if (pf->tvar->name == NULL)
+                               return -ENOMEM;
+               } else
+                       pf->tvar->name = NULL;
+               return 0;
+       }
+
        if (pf->pvar->name)
                pf->tvar->name = strdup(pf->pvar->name);
        else {
@@ -701,33 +652,28 @@ static int find_variable(Dwarf_Die *sp_die, struct probe_finder *pf)
        if (pf->tvar->name == NULL)
                return -ENOMEM;
 
-       if (!is_c_varname(pf->pvar->var)) {
-               /* Copy raw parameters */
-               pf->tvar->value = strdup(pf->pvar->var);
-               if (pf->tvar->value == NULL)
-                       return -ENOMEM;
-               else
-                       return 0;
-       }
-
        pr_debug("Searching '%s' variable in context.\n",
                 pf->pvar->var);
        /* Search child die for local variables and parameters. */
-       if (die_find_variable(sp_die, pf->pvar->var, &vr_die))
+       if (die_find_variable_at(sp_die, pf->pvar->var, pf->addr, &vr_die))
                ret = convert_variable(&vr_die, pf);
        else {
                /* Search upper class */
                nscopes = dwarf_getscopes_die(sp_die, &scopes);
-               if (nscopes > 0) {
-                       ret = dwarf_getscopevar(scopes, nscopes, pf->pvar->var,
-                                               0, NULL, 0, 0, &vr_die);
-                       if (ret >= 0)
+               ret = -ENOENT;
+               while (nscopes-- > 1) {
+                       pr_debug("Searching variables in %s\n",
+                                dwarf_diename(&scopes[nscopes]));
+                       /* We should check this scope, so give dummy address */
+                       if (die_find_variable_at(&scopes[nscopes],
+                                                pf->pvar->var, 0,
+                                                &vr_die)) {
                                ret = convert_variable(&vr_die, pf);
-                       else
-                               ret = -ENOENT;
+                               break;
+                       }
+               }
+               if (scopes)
                        free(scopes);
-               } else
-                       ret = -ENOENT;
        }
        if (ret < 0)
                pr_warning("Failed to find '%s' in this function.\n",
@@ -735,53 +681,59 @@ static int find_variable(Dwarf_Die *sp_die, struct probe_finder *pf)
        return ret;
 }
 
-/* Show a probe point to output buffer */
-static int convert_probe_point(Dwarf_Die *sp_die, struct probe_finder *pf)
+/* Convert subprogram DIE to trace point */
+static int convert_to_trace_point(Dwarf_Die *sp_die, Dwarf_Addr paddr,
+                                 bool retprobe, struct probe_trace_point *tp)
 {
-       struct kprobe_trace_event *tev;
        Dwarf_Addr eaddr;
-       Dwarf_Die die_mem;
        const char *name;
-       int ret, i;
-       Dwarf_Attribute fb_attr;
-       size_t nops;
-
-       if (pf->ntevs == pf->max_tevs) {
-               pr_warning("Too many( > %d) probe point found.\n",
-                          pf->max_tevs);
-               return -ERANGE;
-       }
-       tev = &pf->tevs[pf->ntevs++];
-
-       /* If no real subprogram, find a real one */
-       if (!sp_die || dwarf_tag(sp_die) != DW_TAG_subprogram) {
-               sp_die = die_find_real_subprogram(&pf->cu_die,
-                                                pf->addr, &die_mem);
-               if (!sp_die) {
-                       pr_warning("Failed to find probe point in any "
-                                  "functions.\n");
-                       return -ENOENT;
-               }
-       }
 
        /* Copy the name of probe point */
        name = dwarf_diename(sp_die);
        if (name) {
                if (dwarf_entrypc(sp_die, &eaddr) != 0) {
-                       pr_warning("Failed to get entry pc of %s\n",
+                       pr_warning("Failed to get entry address of %s\n",
                                   dwarf_diename(sp_die));
                        return -ENOENT;
                }
-               tev->point.symbol = strdup(name);
-               if (tev->point.symbol == NULL)
+               tp->symbol = strdup(name);
+               if (tp->symbol == NULL)
                        return -ENOMEM;
-               tev->point.offset = (unsigned long)(pf->addr - eaddr);
+               tp->offset = (unsigned long)(paddr - eaddr);
        } else
                /* This function has no name. */
-               tev->point.offset = (unsigned long)pf->addr;
+               tp->offset = (unsigned long)paddr;
 
-       pr_debug("Probe point found: %s+%lu\n", tev->point.symbol,
-                tev->point.offset);
+       /* Return probe must be on the head of a subprogram */
+       if (retprobe) {
+               if (eaddr != paddr) {
+                       pr_warning("Return probe must be on the head of"
+                                  " a real function.\n");
+                       return -EINVAL;
+               }
+               tp->retprobe = true;
+       }
+
+       return 0;
+}
+
+/* Call probe_finder callback with real subprogram DIE */
+static int call_probe_finder(Dwarf_Die *sp_die, struct probe_finder *pf)
+{
+       Dwarf_Die die_mem;
+       Dwarf_Attribute fb_attr;
+       size_t nops;
+       int ret;
+
+       /* If no real subprogram, find a real one */
+       if (!sp_die || dwarf_tag(sp_die) != DW_TAG_subprogram) {
+               sp_die = die_find_realfunc(&pf->cu_die, pf->addr, &die_mem);
+               if (!sp_die) {
+                       pr_warning("Failed to find probe point in any "
+                                  "functions.\n");
+                       return -ENOENT;
+               }
+       }
 
        /* Get the frame base attribute/ops */
        dwarf_attr(sp_die, DW_AT_frame_base, &fb_attr);
@@ -794,182 +746,118 @@ static int convert_probe_point(Dwarf_Die *sp_die, struct probe_finder *pf)
                Dwarf_Frame *frame;
                if (dwarf_cfi_addrframe(pf->cfi, pf->addr, &frame) != 0 ||
                    dwarf_frame_cfa(frame, &pf->fb_ops, &nops) != 0) {
-                       pr_warning("Failed to get CFA on 0x%jx\n",
+                       pr_warning("Failed to get call frame on 0x%jx\n",
                                   (uintmax_t)pf->addr);
                        return -ENOENT;
                }
 #endif
        }
 
-       /* Find each argument */
-       tev->nargs = pf->pev->nargs;
-       tev->args = zalloc(sizeof(struct kprobe_trace_arg) * tev->nargs);
-       if (tev->args == NULL)
-               return -ENOMEM;
-       for (i = 0; i < pf->pev->nargs; i++) {
-               pf->pvar = &pf->pev->args[i];
-               pf->tvar = &tev->args[i];
-               ret = find_variable(sp_die, pf);
-               if (ret != 0)
-                       return ret;
-       }
+       /* Call finder's callback handler */
+       ret = pf->callback(sp_die, pf);
 
        /* *pf->fb_ops will be cached in libdw. Don't free it. */
        pf->fb_ops = NULL;
-       return 0;
+
+       return ret;
 }
 
-/* Find probe point from its line number */
-static int find_probe_point_by_line(struct probe_finder *pf)
+static int probe_point_line_walker(const char *fname, int lineno,
+                                  Dwarf_Addr addr, void *data)
 {
-       Dwarf_Lines *lines;
-       Dwarf_Line *line;
-       size_t nlines, i;
-       Dwarf_Addr addr;
-       int lineno;
-       int ret = 0;
-
-       if (dwarf_getsrclines(&pf->cu_die, &lines, &nlines) != 0) {
-               pr_warning("No source lines found in this CU.\n");
-               return -ENOENT;
-       }
+       struct probe_finder *pf = data;
+       int ret;
 
-       for (i = 0; i < nlines && ret == 0; i++) {
-               line = dwarf_onesrcline(lines, i);
-               if (dwarf_lineno(line, &lineno) != 0 ||
-                   lineno != pf->lno)
-                       continue;
+       if (lineno != pf->lno || strtailcmp(fname, pf->fname) != 0)
+               return 0;
 
-               /* TODO: Get fileno from line, but how? */
-               if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
-                       continue;
+       pf->addr = addr;
+       ret = call_probe_finder(NULL, pf);
 
-               if (dwarf_lineaddr(line, &addr) != 0) {
-                       pr_warning("Failed to get the address of the line.\n");
-                       return -ENOENT;
-               }
-               pr_debug("Probe line found: line[%d]:%d addr:0x%jx\n",
-                        (int)i, lineno, (uintmax_t)addr);
-               pf->addr = addr;
+       /* Continue if no error, because the line will be in inline function */
+       return ret < 0 ? ret : 0;
+}
 
-               ret = convert_probe_point(NULL, pf);
-               /* Continuing, because target line might be inlined. */
-       }
-       return ret;
+/* Find probe point from its line number */
+static int find_probe_point_by_line(struct probe_finder *pf)
+{
+       return die_walk_lines(&pf->cu_die, probe_point_line_walker, pf);
 }
 
 /* Find lines which match lazy pattern */
 static int find_lazy_match_lines(struct list_head *head,
                                 const char *fname, const char *pat)
 {
-       char *fbuf, *p1, *p2;
-       int fd, line, nlines = -1;
-       struct stat st;
-
-       fd = open(fname, O_RDONLY);
-       if (fd < 0) {
-               pr_warning("Failed to open %s: %s\n", fname, strerror(-fd));
+       FILE *fp;
+       char *line = NULL;
+       size_t line_len;
+       ssize_t len;
+       int count = 0, linenum = 1;
+
+       fp = fopen(fname, "r");
+       if (!fp) {
+               pr_warning("Failed to open %s: %s\n", fname, strerror(errno));
                return -errno;
        }
 
-       if (fstat(fd, &st) < 0) {
-               pr_warning("Failed to get the size of %s: %s\n",
-                          fname, strerror(errno));
-               nlines = -errno;
-               goto out_close;
-       }
+       while ((len = getline(&line, &line_len, fp)) > 0) {
 
-       nlines = -ENOMEM;
-       fbuf = malloc(st.st_size + 2);
-       if (fbuf == NULL)
-               goto out_close;
-       if (read(fd, fbuf, st.st_size) < 0) {
-               pr_warning("Failed to read %s: %s\n", fname, strerror(errno));
-               nlines = -errno;
-               goto out_free_fbuf;
-       }
-       fbuf[st.st_size] = '\n';        /* Dummy line */
-       fbuf[st.st_size + 1] = '\0';
-       p1 = fbuf;
-       line = 1;
-       nlines = 0;
-       while ((p2 = strchr(p1, '\n')) != NULL) {
-               *p2 = '\0';
-               if (strlazymatch(p1, pat)) {
-                       line_list__add_line(head, line);
-                       nlines++;
+               if (line[len - 1] == '\n')
+                       line[len - 1] = '\0';
+
+               if (strlazymatch(line, pat)) {
+                       line_list__add_line(head, linenum);
+                       count++;
                }
-               line++;
-               p1 = p2 + 1;
+               linenum++;
        }
-out_free_fbuf:
-       free(fbuf);
-out_close:
-       close(fd);
-       return nlines;
+
+       if (ferror(fp))
+               count = -errno;
+       free(line);
+       fclose(fp);
+
+       if (count == 0)
+               pr_debug("No matched lines found in %s.\n", fname);
+       return count;
+}
+
+static int probe_point_lazy_walker(const char *fname, int lineno,
+                                  Dwarf_Addr addr, void *data)
+{
+       struct probe_finder *pf = data;
+       int ret;
+
+       if (!line_list__has_line(&pf->lcache, lineno) ||
+           strtailcmp(fname, pf->fname) != 0)
+               return 0;
+
+       pr_debug("Probe line found: line:%d addr:0x%llx\n",
+                lineno, (unsigned long long)addr);
+       pf->addr = addr;
+       ret = call_probe_finder(NULL, pf);
+
+       /*
+        * Continue if no error, because the lazy pattern will match
+        * to other lines
+        */
+       return ret < 0 ? ret : 0;
 }
 
 /* Find probe points from lazy pattern  */
 static int find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf)
 {
-       Dwarf_Lines *lines;
-       Dwarf_Line *line;
-       size_t nlines, i;
-       Dwarf_Addr addr;
-       Dwarf_Die die_mem;
-       int lineno;
        int ret = 0;
 
        if (list_empty(&pf->lcache)) {
                /* Matching lazy line pattern */
                ret = find_lazy_match_lines(&pf->lcache, pf->fname,
                                            pf->pev->point.lazy_line);
-               if (ret == 0) {
-                       pr_debug("No matched lines found in %s.\n", pf->fname);
-                       return 0;
-               } else if (ret < 0)
+               if (ret <= 0)
                        return ret;
        }
 
-       if (dwarf_getsrclines(&pf->cu_die, &lines, &nlines) != 0) {
-               pr_warning("No source lines found in this CU.\n");
-               return -ENOENT;
-       }
-
-       for (i = 0; i < nlines && ret >= 0; i++) {
-               line = dwarf_onesrcline(lines, i);
-
-               if (dwarf_lineno(line, &lineno) != 0 ||
-                   !line_list__has_line(&pf->lcache, lineno))
-                       continue;
-
-               /* TODO: Get fileno from line, but how? */
-               if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
-                       continue;
-
-               if (dwarf_lineaddr(line, &addr) != 0) {
-                       pr_debug("Failed to get the address of line %d.\n",
-                                lineno);
-                       continue;
-               }
-               if (sp_die) {
-                       /* Address filtering 1: does sp_die include addr? */
-                       if (!dwarf_haspc(sp_die, addr))
-                               continue;
-                       /* Address filtering 2: No child include addr? */
-                       if (die_find_inlinefunc(sp_die, addr, &die_mem))
-                               continue;
-               }
-
-               pr_debug("Probe line found: line[%d]:%d addr:0x%llx\n",
-                        (int)i, lineno, (unsigned long long)addr);
-               pf->addr = addr;
-
-               ret = convert_probe_point(sp_die, pf);
-               /* Continuing, because target line might be inlined. */
-       }
-       /* TODO: deallocate lines, but how? */
-       return ret;
+       return die_walk_lines(sp_die, probe_point_lazy_walker, pf);
 }
 
 /* Callback parameter with return value */
@@ -990,7 +878,7 @@ static int probe_point_inline_cb(Dwarf_Die *in_die, void *data)
        else {
                /* Get probe address */
                if (dwarf_entrypc(in_die, &addr) != 0) {
-                       pr_warning("Failed to get entry pc of %s.\n",
+                       pr_warning("Failed to get entry address of %s.\n",
                                   dwarf_diename(in_die));
                        param->retval = -ENOENT;
                        return DWARF_CB_ABORT;
@@ -1000,7 +888,7 @@ static int probe_point_inline_cb(Dwarf_Die *in_die, void *data)
                pr_debug("found inline addr: 0x%jx\n",
                         (uintmax_t)pf->addr);
 
-               param->retval = convert_probe_point(in_die, pf);
+               param->retval = call_probe_finder(in_die, pf);
                if (param->retval < 0)
                        return DWARF_CB_ABORT;
        }
@@ -1017,7 +905,11 @@ static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
 
        /* Check tag and diename */
        if (dwarf_tag(sp_die) != DW_TAG_subprogram ||
-           die_compare_name(sp_die, pp->function) != 0)
+           !die_compare_name(sp_die, pp->function))
+               return DWARF_CB_OK;
+
+       /* Check declared file */
+       if (pp->file && strtailcmp(pp->file, dwarf_decl_file(sp_die)))
                return DWARF_CB_OK;
 
        pf->fname = dwarf_decl_file(sp_die);
@@ -1031,14 +923,14 @@ static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
                        param->retval = find_probe_point_lazy(sp_die, pf);
                else {
                        if (dwarf_entrypc(sp_die, &pf->addr) != 0) {
-                               pr_warning("Failed to get entry pc of %s.\n",
-                                          dwarf_diename(sp_die));
+                               pr_warning("Failed to get entry address of "
+                                          "%s.\n", dwarf_diename(sp_die));
                                param->retval = -ENOENT;
                                return DWARF_CB_ABORT;
                        }
                        pf->addr += pp->offset;
                        /* TODO: Check the address in this function */
-                       param->retval = convert_probe_point(sp_die, pf);
+                       param->retval = call_probe_finder(sp_die, pf);
                }
        } else {
                struct dwarf_callback_param _param = {.data = (void *)pf,
@@ -1060,155 +952,390 @@ static int find_probe_point_by_func(struct probe_finder *pf)
        return _param.retval;
 }
 
-/* Find kprobe_trace_events specified by perf_probe_event from debuginfo */
-int find_kprobe_trace_events(int fd, struct perf_probe_event *pev,
-                            struct kprobe_trace_event **tevs, int max_tevs)
+struct pubname_callback_param {
+       char *function;
+       char *file;
+       Dwarf_Die *cu_die;
+       Dwarf_Die *sp_die;
+       int found;
+};
+
+static int pubname_search_cb(Dwarf *dbg, Dwarf_Global *gl, void *data)
+{
+       struct pubname_callback_param *param = data;
+
+       if (dwarf_offdie(dbg, gl->die_offset, param->sp_die)) {
+               if (dwarf_tag(param->sp_die) != DW_TAG_subprogram)
+                       return DWARF_CB_OK;
+
+               if (die_compare_name(param->sp_die, param->function)) {
+                       if (!dwarf_offdie(dbg, gl->cu_offset, param->cu_die))
+                               return DWARF_CB_OK;
+
+                       if (param->file &&
+                           strtailcmp(param->file, dwarf_decl_file(param->sp_die)))
+                               return DWARF_CB_OK;
+
+                       param->found = 1;
+                       return DWARF_CB_ABORT;
+               }
+       }
+
+       return DWARF_CB_OK;
+}
+
+/* Find probe points from debuginfo */
+static int debuginfo__find_probes(struct debuginfo *self,
+                                 struct probe_finder *pf)
 {
-       struct probe_finder pf = {.pev = pev, .max_tevs = max_tevs};
-       struct perf_probe_point *pp = &pev->point;
+       struct perf_probe_point *pp = &pf->pev->point;
        Dwarf_Off off, noff;
        size_t cuhl;
        Dwarf_Die *diep;
-       Dwarf *dbg;
        int ret = 0;
 
-       pf.tevs = zalloc(sizeof(struct kprobe_trace_event) * max_tevs);
-       if (pf.tevs == NULL)
-               return -ENOMEM;
-       *tevs = pf.tevs;
-       pf.ntevs = 0;
-
-       dbg = dwarf_begin(fd, DWARF_C_READ);
-       if (!dbg) {
-               pr_warning("No dwarf info found in the vmlinux - "
-                       "please rebuild with CONFIG_DEBUG_INFO=y.\n");
-               free(pf.tevs);
-               *tevs = NULL;
-               return -EBADF;
-       }
-
 #if _ELFUTILS_PREREQ(0, 142)
        /* Get the call frame information from this dwarf */
-       pf.cfi = dwarf_getcfi(dbg);
+       pf->cfi = dwarf_getcfi(self->dbg);
 #endif
 
        off = 0;
-       line_list__init(&pf.lcache);
+       line_list__init(&pf->lcache);
+
+       /* Fastpath: lookup by function name from .debug_pubnames section */
+       if (pp->function) {
+               struct pubname_callback_param pubname_param = {
+                       .function = pp->function,
+                       .file     = pp->file,
+                       .cu_die   = &pf->cu_die,
+                       .sp_die   = &pf->sp_die,
+                       .found    = 0,
+               };
+               struct dwarf_callback_param probe_param = {
+                       .data = pf,
+               };
+
+               dwarf_getpubnames(self->dbg, pubname_search_cb,
+                                 &pubname_param, 0);
+               if (pubname_param.found) {
+                       ret = probe_point_search_cb(&pf->sp_die, &probe_param);
+                       if (ret)
+                               goto found;
+               }
+       }
+
        /* Loop on CUs (Compilation Unit) */
-       while (!dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL) &&
-              ret >= 0) {
+       while (!dwarf_nextcu(self->dbg, off, &noff, &cuhl, NULL, NULL, NULL)) {
                /* Get the DIE(Debugging Information Entry) of this CU */
-               diep = dwarf_offdie(dbg, off + cuhl, &pf.cu_die);
+               diep = dwarf_offdie(self->dbg, off + cuhl, &pf->cu_die);
                if (!diep)
                        continue;
 
                /* Check if target file is included. */
                if (pp->file)
-                       pf.fname = cu_find_realpath(&pf.cu_die, pp->file);
+                       pf->fname = cu_find_realpath(&pf->cu_die, pp->file);
                else
-                       pf.fname = NULL;
+                       pf->fname = NULL;
 
-               if (!pp->file || pf.fname) {
+               if (!pp->file || pf->fname) {
                        if (pp->function)
-                               ret = find_probe_point_by_func(&pf);
+                               ret = find_probe_point_by_func(pf);
                        else if (pp->lazy_line)
-                               ret = find_probe_point_lazy(NULL, &pf);
+                               ret = find_probe_point_lazy(NULL, pf);
                        else {
-                               pf.lno = pp->line;
-                               ret = find_probe_point_by_line(&pf);
+                               pf->lno = pp->line;
+                               ret = find_probe_point_by_line(pf);
                        }
+                       if (ret < 0)
+                               break;
                }
                off = noff;
        }
-       line_list__free(&pf.lcache);
-       dwarf_end(dbg);
 
-       return (ret < 0) ? ret : pf.ntevs;
+found:
+       line_list__free(&pf->lcache);
+
+       return ret;
+}
+
+/* Add a found probe point into trace event list */
+static int add_probe_trace_event(Dwarf_Die *sp_die, struct probe_finder *pf)
+{
+       struct trace_event_finder *tf =
+                       container_of(pf, struct trace_event_finder, pf);
+       struct probe_trace_event *tev;
+       int ret, i;
+
+       /* Check number of tevs */
+       if (tf->ntevs == tf->max_tevs) {
+               pr_warning("Too many( > %d) probe point found.\n",
+                          tf->max_tevs);
+               return -ERANGE;
+       }
+       tev = &tf->tevs[tf->ntevs++];
+
+       ret = convert_to_trace_point(sp_die, pf->addr, pf->pev->point.retprobe,
+                                    &tev->point);
+       if (ret < 0)
+               return ret;
+
+       pr_debug("Probe point found: %s+%lu\n", tev->point.symbol,
+                tev->point.offset);
+
+       /* Find each argument */
+       tev->nargs = pf->pev->nargs;
+       tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
+       if (tev->args == NULL)
+               return -ENOMEM;
+       for (i = 0; i < pf->pev->nargs; i++) {
+               pf->pvar = &pf->pev->args[i];
+               pf->tvar = &tev->args[i];
+               ret = find_variable(sp_die, pf);
+               if (ret != 0)
+                       return ret;
+       }
+
+       return 0;
+}
+
+/* Find probe_trace_events specified by perf_probe_event from debuginfo */
+int debuginfo__find_trace_events(struct debuginfo *self,
+                                struct perf_probe_event *pev,
+                                struct probe_trace_event **tevs, int max_tevs)
+{
+       struct trace_event_finder tf = {
+                       .pf = {.pev = pev, .callback = add_probe_trace_event},
+                       .max_tevs = max_tevs};
+       int ret;
+
+       /* Allocate result tevs array */
+       *tevs = zalloc(sizeof(struct probe_trace_event) * max_tevs);
+       if (*tevs == NULL)
+               return -ENOMEM;
+
+       tf.tevs = *tevs;
+       tf.ntevs = 0;
+
+       ret = debuginfo__find_probes(self, &tf.pf);
+       if (ret < 0) {
+               free(*tevs);
+               *tevs = NULL;
+               return ret;
+       }
+
+       return (ret < 0) ? ret : tf.ntevs;
+}
+
+#define MAX_VAR_LEN 64
+
+/* Collect available variables in this scope */
+static int collect_variables_cb(Dwarf_Die *die_mem, void *data)
+{
+       struct available_var_finder *af = data;
+       struct variable_list *vl;
+       char buf[MAX_VAR_LEN];
+       int tag, ret;
+
+       vl = &af->vls[af->nvls - 1];
+
+       tag = dwarf_tag(die_mem);
+       if (tag == DW_TAG_formal_parameter ||
+           tag == DW_TAG_variable) {
+               ret = convert_variable_location(die_mem, af->pf.addr,
+                                               af->pf.fb_ops, NULL);
+               if (ret == 0) {
+                       ret = die_get_varname(die_mem, buf, MAX_VAR_LEN);
+                       pr_debug2("Add new var: %s\n", buf);
+                       if (ret > 0)
+                               strlist__add(vl->vars, buf);
+               }
+       }
+
+       if (af->child && dwarf_haspc(die_mem, af->pf.addr))
+               return DIE_FIND_CB_CONTINUE;
+       else
+               return DIE_FIND_CB_SIBLING;
+}
+
+/* Add a found vars into available variables list */
+static int add_available_vars(Dwarf_Die *sp_die, struct probe_finder *pf)
+{
+       struct available_var_finder *af =
+                       container_of(pf, struct available_var_finder, pf);
+       struct variable_list *vl;
+       Dwarf_Die die_mem, *scopes = NULL;
+       int ret, nscopes;
+
+       /* Check number of tevs */
+       if (af->nvls == af->max_vls) {
+               pr_warning("Too many( > %d) probe point found.\n", af->max_vls);
+               return -ERANGE;
+       }
+       vl = &af->vls[af->nvls++];
+
+       ret = convert_to_trace_point(sp_die, pf->addr, pf->pev->point.retprobe,
+                                    &vl->point);
+       if (ret < 0)
+               return ret;
+
+       pr_debug("Probe point found: %s+%lu\n", vl->point.symbol,
+                vl->point.offset);
+
+       /* Find local variables */
+       vl->vars = strlist__new(true, NULL);
+       if (vl->vars == NULL)
+               return -ENOMEM;
+       af->child = true;
+       die_find_child(sp_die, collect_variables_cb, (void *)af, &die_mem);
+
+       /* Find external variables */
+       if (!af->externs)
+               goto out;
+       /* Don't need to search child DIE for externs. */
+       af->child = false;
+       nscopes = dwarf_getscopes_die(sp_die, &scopes);
+       while (nscopes-- > 1)
+               die_find_child(&scopes[nscopes], collect_variables_cb,
+                              (void *)af, &die_mem);
+       if (scopes)
+               free(scopes);
+
+out:
+       if (strlist__empty(vl->vars)) {
+               strlist__delete(vl->vars);
+               vl->vars = NULL;
+       }
+
+       return ret;
+}
+
+/* Find available variables at given probe point */
+int debuginfo__find_available_vars_at(struct debuginfo *self,
+                                     struct perf_probe_event *pev,
+                                     struct variable_list **vls,
+                                     int max_vls, bool externs)
+{
+       struct available_var_finder af = {
+                       .pf = {.pev = pev, .callback = add_available_vars},
+                       .max_vls = max_vls, .externs = externs};
+       int ret;
+
+       /* Allocate result vls array */
+       *vls = zalloc(sizeof(struct variable_list) * max_vls);
+       if (*vls == NULL)
+               return -ENOMEM;
+
+       af.vls = *vls;
+       af.nvls = 0;
+
+       ret = debuginfo__find_probes(self, &af.pf);
+       if (ret < 0) {
+               /* Free vlist for error */
+               while (af.nvls--) {
+                       if (af.vls[af.nvls].point.symbol)
+                               free(af.vls[af.nvls].point.symbol);
+                       if (af.vls[af.nvls].vars)
+                               strlist__delete(af.vls[af.nvls].vars);
+               }
+               free(af.vls);
+               *vls = NULL;
+               return ret;
+       }
+
+       return (ret < 0) ? ret : af.nvls;
 }
 
 /* Reverse search */
-int find_perf_probe_point(int fd, unsigned long addr,
-                         struct perf_probe_point *ppt)
+int debuginfo__find_probe_point(struct debuginfo *self, unsigned long addr,
+                               struct perf_probe_point *ppt)
 {
        Dwarf_Die cudie, spdie, indie;
-       Dwarf *dbg;
-       Dwarf_Line *line;
-       Dwarf_Addr laddr, eaddr;
-       const char *tmp;
-       int lineno, ret = 0;
-       bool found = false;
+       Dwarf_Addr _addr, baseaddr;
+       const char *fname = NULL, *func = NULL, *tmp;
+       int baseline = 0, lineno = 0, ret = 0;
 
-       dbg = dwarf_begin(fd, DWARF_C_READ);
-       if (!dbg)
-               return -EBADF;
+       /* Adjust address with bias */
+       addr += self->bias;
 
        /* Find cu die */
-       if (!dwarf_addrdie(dbg, (Dwarf_Addr)addr, &cudie)) {
+       if (!dwarf_addrdie(self->dbg, (Dwarf_Addr)addr - self->bias, &cudie)) {
+               pr_warning("Failed to find debug information for address %lx\n",
+                          addr);
                ret = -EINVAL;
                goto end;
        }
 
-       /* Find a corresponding line */
-       line = dwarf_getsrc_die(&cudie, (Dwarf_Addr)addr);
-       if (line) {
-               if (dwarf_lineaddr(line, &laddr) == 0 &&
-                   (Dwarf_Addr)addr == laddr &&
-                   dwarf_lineno(line, &lineno) == 0) {
-                       tmp = dwarf_linesrc(line, NULL, NULL);
-                       if (tmp) {
-                               ppt->line = lineno;
-                               ppt->file = strdup(tmp);
-                               if (ppt->file == NULL) {
-                                       ret = -ENOMEM;
-                                       goto end;
-                               }
-                               found = true;
-                       }
-               }
-       }
+       /* Find a corresponding line (filename and lineno) */
+       cu_find_lineinfo(&cudie, addr, &fname, &lineno);
+       /* Don't care whether it failed or not */
 
-       /* Find a corresponding function */
-       if (die_find_real_subprogram(&cudie, (Dwarf_Addr)addr, &spdie)) {
+       /* Find a corresponding function (name, baseline and baseaddr) */
+       if (die_find_realfunc(&cudie, (Dwarf_Addr)addr, &spdie)) {
+               /* Get function entry information */
                tmp = dwarf_diename(&spdie);
-               if (!tmp || dwarf_entrypc(&spdie, &eaddr) != 0)
-                       goto end;
-
-               if (ppt->line) {
-                       if (die_find_inlinefunc(&spdie, (Dwarf_Addr)addr,
-                                               &indie)) {
-                               /* addr in an inline function */
+               if (!tmp ||
+                   dwarf_entrypc(&spdie, &baseaddr) != 0 ||
+                   dwarf_decl_line(&spdie, &baseline) != 0)
+                       goto post;
+               func = tmp;
+
+               if (addr == (unsigned long)baseaddr)
+                       /* Function entry - Relative line number is 0 */
+                       lineno = baseline;
+               else if (die_find_inlinefunc(&spdie, (Dwarf_Addr)addr,
+                                            &indie)) {
+                       if (dwarf_entrypc(&indie, &_addr) == 0 &&
+                           _addr == addr)
+                               /*
+                                * addr is at an inline function entry.
+                                * In this case, lineno should be the call-site
+                                * line number.
+                                */
+                               lineno = die_get_call_lineno(&indie);
+                       else {
+                               /*
+                                * addr is in an inline function body.
+                                * Since lineno points one of the lines
+                                * of the inline function, baseline should
+                                * be the entry line of the inline function.
+                                */
                                tmp = dwarf_diename(&indie);
-                               if (!tmp)
-                                       goto end;
-                               ret = dwarf_decl_line(&indie, &lineno);
-                       } else {
-                               if (eaddr == addr) {    /* Function entry */
-                                       lineno = ppt->line;
-                                       ret = 0;
-                               } else
-                                       ret = dwarf_decl_line(&spdie, &lineno);
-                       }
-                       if (ret == 0) {
-                               /* Make a relative line number */
-                               ppt->line -= lineno;
-                               goto found;
+                               if (tmp &&
+                                   dwarf_decl_line(&spdie, &baseline) == 0)
+                                       func = tmp;
                        }
                }
-               /* We don't have a line number, let's use offset */
-               ppt->offset = addr - (unsigned long)eaddr;
-found:
-               ppt->function = strdup(tmp);
+       }
+
+post:
+       /* Make a relative line number or an offset */
+       if (lineno)
+               ppt->line = lineno - baseline;
+       else if (func)
+               ppt->offset = addr - (unsigned long)baseaddr;
+
+       /* Duplicate strings */
+       if (func) {
+               ppt->function = strdup(func);
                if (ppt->function == NULL) {
                        ret = -ENOMEM;
                        goto end;
                }
-               found = true;
        }
-
+       if (fname) {
+               ppt->file = strdup(fname);
+               if (ppt->file == NULL) {
+                       if (ppt->function) {
+                               free(ppt->function);
+                               ppt->function = NULL;
+                       }
+                       ret = -ENOMEM;
+                       goto end;
+               }
+       }
 end:
-       dwarf_end(dbg);
-       if (ret >= 0)
-               ret = found ? 1 : 0;
+       if (ret == 0 && (fname || func))
+               ret = 1;        /* Found a point */
        return ret;
 }
 
@@ -1225,91 +1352,28 @@ static int line_range_add_line(const char *src, unsigned int lineno,
        return line_list__add_line(&lr->line_list, lineno);
 }
 
-/* Search function declaration lines */
-static int line_range_funcdecl_cb(Dwarf_Die *sp_die, void *data)
+static int line_range_walk_cb(const char *fname, int lineno,
+                             Dwarf_Addr addr __used,
+                             void *data)
 {
-       struct dwarf_callback_param *param = data;
-       struct line_finder *lf = param->data;
-       const char *src;
-       int lineno;
-
-       src = dwarf_decl_file(sp_die);
-       if (src && strtailcmp(src, lf->fname) != 0)
-               return DWARF_CB_OK;
+       struct line_finder *lf = data;
 
-       if (dwarf_decl_line(sp_die, &lineno) != 0 ||
+       if ((strtailcmp(fname, lf->fname) != 0) ||
            (lf->lno_s > lineno || lf->lno_e < lineno))
-               return DWARF_CB_OK;
+               return 0;
 
-       param->retval = line_range_add_line(src, lineno, lf->lr);
-       if (param->retval < 0)
-               return DWARF_CB_ABORT;
-       return DWARF_CB_OK;
-}
+       if (line_range_add_line(fname, lineno, lf->lr) < 0)
+               return -EINVAL;
 
-static int find_line_range_func_decl_lines(struct line_finder *lf)
-{
-       struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
-       dwarf_getfuncs(&lf->cu_die, line_range_funcdecl_cb, &param, 0);
-       return param.retval;
+       return 0;
 }
 
 /* Find line range from its line number */
 static int find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
 {
-       Dwarf_Lines *lines;
-       Dwarf_Line *line;
-       size_t nlines, i;
-       Dwarf_Addr addr;
-       int lineno, ret = 0;
-       const char *src;
-       Dwarf_Die die_mem;
-
-       line_list__init(&lf->lr->line_list);
-       if (dwarf_getsrclines(&lf->cu_die, &lines, &nlines) != 0) {
-               pr_warning("No source lines found in this CU.\n");
-               return -ENOENT;
-       }
-
-       /* Search probable lines on lines list */
-       for (i = 0; i < nlines; i++) {
-               line = dwarf_onesrcline(lines, i);
-               if (dwarf_lineno(line, &lineno) != 0 ||
-                   (lf->lno_s > lineno || lf->lno_e < lineno))
-                       continue;
-
-               if (sp_die) {
-                       /* Address filtering 1: does sp_die include addr? */
-                       if (dwarf_lineaddr(line, &addr) != 0 ||
-                           !dwarf_haspc(sp_die, addr))
-                               continue;
-
-                       /* Address filtering 2: No child include addr? */
-                       if (die_find_inlinefunc(sp_die, addr, &die_mem))
-                               continue;
-               }
-
-               /* TODO: Get fileno from line, but how? */
-               src = dwarf_linesrc(line, NULL, NULL);
-               if (strtailcmp(src, lf->fname) != 0)
-                       continue;
+       int ret;
 
-               ret = line_range_add_line(src, lineno, lf->lr);
-               if (ret < 0)
-                       return ret;
-       }
-
-       /*
-        * Dwarf lines doesn't include function declarations. We have to
-        * check functions list or given function.
-        */
-       if (sp_die) {
-               src = dwarf_decl_file(sp_die);
-               if (src && dwarf_decl_line(sp_die, &lineno) == 0 &&
-                   (lf->lno_s <= lineno && lf->lno_e >= lineno))
-                       ret = line_range_add_line(src, lineno, lf->lr);
-       } else
-               ret = find_line_range_func_decl_lines(lf);
+       ret = die_walk_lines(sp_die ?: &lf->cu_die, line_range_walk_cb, lf);
 
        /* Update status */
        if (ret >= 0)
@@ -1329,7 +1393,13 @@ static int line_range_inline_cb(Dwarf_Die *in_die, void *data)
        struct dwarf_callback_param *param = data;
 
        param->retval = find_line_range_by_line(in_die, param->data);
-       return DWARF_CB_ABORT;  /* No need to find other instances */
+
+       /*
+        * We have to check all instances of inlined function, because
+        * some execution paths can be optimized out depends on the
+        * function argument of instances
+        */
+       return DWARF_CB_OK;
 }
 
 /* Search function from function name */
@@ -1339,8 +1409,12 @@ static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
        struct line_finder *lf = param->data;
        struct line_range *lr = lf->lr;
 
+       /* Check declared file */
+       if (lr->file && strtailcmp(lr->file, dwarf_decl_file(sp_die)))
+               return DWARF_CB_OK;
+
        if (dwarf_tag(sp_die) == DW_TAG_subprogram &&
-           die_compare_name(sp_die, lr->function) == 0) {
+           die_compare_name(sp_die, lr->function)) {
                lf->fname = dwarf_decl_file(sp_die);
                dwarf_decl_line(sp_die, &lr->offset);
                pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
@@ -1375,30 +1449,40 @@ static int find_line_range_by_func(struct line_finder *lf)
        return param.retval;
 }
 
-int find_line_range(int fd, struct line_range *lr)
+int debuginfo__find_line_range(struct debuginfo *self, struct line_range *lr)
 {
        struct line_finder lf = {.lr = lr, .found = 0};
        int ret = 0;
        Dwarf_Off off = 0, noff;
        size_t cuhl;
        Dwarf_Die *diep;
-       Dwarf *dbg;
        const char *comp_dir;
 
-       dbg = dwarf_begin(fd, DWARF_C_READ);
-       if (!dbg) {
-               pr_warning("No dwarf info found in the vmlinux - "
-                       "please rebuild with CONFIG_DEBUG_INFO=y.\n");
-               return -EBADF;
+       /* Fastpath: lookup by function name from .debug_pubnames section */
+       if (lr->function) {
+               struct pubname_callback_param pubname_param = {
+                       .function = lr->function, .file = lr->file,
+                       .cu_die = &lf.cu_die, .sp_die = &lf.sp_die, .found = 0};
+               struct dwarf_callback_param line_range_param = {
+                       .data = (void *)&lf, .retval = 0};
+
+               dwarf_getpubnames(self->dbg, pubname_search_cb,
+                                 &pubname_param, 0);
+               if (pubname_param.found) {
+                       line_range_search_cb(&lf.sp_die, &line_range_param);
+                       if (lf.found)
+                               goto found;
+               }
        }
 
        /* Loop on CUs (Compilation Unit) */
        while (!lf.found && ret >= 0) {
-               if (dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL) != 0)
+               if (dwarf_nextcu(self->dbg, off, &noff, &cuhl,
+                                NULL, NULL, NULL) != 0)
                        break;
 
                /* Get the DIE(Debugging Information Entry) of this CU */
-               diep = dwarf_offdie(dbg, off + cuhl, &lf.cu_die);
+               diep = dwarf_offdie(self->dbg, off + cuhl, &lf.cu_die);
                if (!diep)
                        continue;
 
@@ -1420,6 +1504,7 @@ int find_line_range(int fd, struct line_range *lr)
                off = noff;
        }
 
+found:
        /* Store comp_dir */
        if (lf.found) {
                comp_dir = cu_get_comp_dir(&lf.cu_die);
@@ -1431,8 +1516,6 @@ int find_line_range(int fd, struct line_range *lr)
        }
 
        pr_debug("path: %s\n", lr->path);
-       dwarf_end(dbg);
-
        return (ret < 0) ? ret : lf.found;
 }