perf report: Allow user to specify path to kallsyms file
[linux-2.6.git] / tools / perf / util / symbol.c
index e77c33a..f40c076 100644 (file)
@@ -11,6 +11,8 @@
 #include <sys/param.h>
 #include <fcntl.h>
 #include <unistd.h>
+#include "build-id.h"
+#include "debug.h"
 #include "symbol.h"
 #include "strlist.h"
 
@@ -24,6 +26,8 @@
 #define NT_GNU_BUILD_ID 3
 #endif
 
+static bool dso__build_id_equal(const struct dso *self, u8 *build_id);
+static int elf_read_build_id(Elf *elf, void *bf, size_t size);
 static void dsos__add(struct list_head *head, struct dso *dso);
 static struct map *map__new2(u64 start, struct dso *dso, enum map_type type);
 static int dso__load_kernel_sym(struct dso *self, struct map *map,
@@ -39,6 +43,14 @@ struct symbol_conf symbol_conf = {
        .try_vmlinux_path = true,
 };
 
+int dso__name_len(const struct dso *self)
+{
+       if (verbose)
+               return self->long_name_len;
+
+       return self->short_name_len;
+}
+
 bool dso__loaded(const struct dso *self, enum map_type type)
 {
        return self->loaded & (1 << type);
@@ -109,7 +121,7 @@ static void __map_groups__fixup_end(struct map_groups *self, enum map_type type)
         * We still haven't the actual symbols, so guess the
         * last map final address.
         */
-       curr->end = ~0UL;
+       curr->end = ~0ULL;
 }
 
 static void map_groups__fixup_end(struct map_groups *self)
@@ -119,7 +131,8 @@ static void map_groups__fixup_end(struct map_groups *self)
                __map_groups__fixup_end(self, i);
 }
 
-static struct symbol *symbol__new(u64 start, u64 len, const char *name)
+static struct symbol *symbol__new(u64 start, u64 len, u8 binding,
+                                 const char *name)
 {
        size_t namelen = strlen(name) + 1;
        struct symbol *self = calloc(1, (symbol_conf.priv_size +
@@ -130,8 +143,10 @@ static struct symbol *symbol__new(u64 start, u64 len, const char *name)
        if (symbol_conf.priv_size)
                self = ((void *)self) + symbol_conf.priv_size;
 
-       self->start = start;
-       self->end   = len ? start + len - 1 : start;
+       self->start   = start;
+       self->end     = len ? start + len - 1 : start;
+       self->binding = binding;
+       self->namelen = namelen - 1;
 
        pr_debug4("%s: %s %#Lx-%#Lx\n", __func__, name, start, self->end);
 
@@ -147,8 +162,11 @@ void symbol__delete(struct symbol *self)
 
 static size_t symbol__fprintf(struct symbol *self, FILE *fp)
 {
-       return fprintf(fp, " %llx-%llx %s\n",
-                      self->start, self->end, self->name);
+       return fprintf(fp, " %llx-%llx %c %s\n",
+                      self->start, self->end,
+                      self->binding == STB_GLOBAL ? 'g' :
+                      self->binding == STB_LOCAL  ? 'l' : 'w',
+                      self->name);
 }
 
 void dso__set_long_name(struct dso *self, char *name)
@@ -213,7 +231,9 @@ void dso__delete(struct dso *self)
        int i;
        for (i = 0; i < MAP__NR_TYPES; ++i)
                symbols__delete(&self->symbols[i]);
-       if (self->long_name != self->name)
+       if (self->sname_alloc)
+               free((char *)self->short_name);
+       if (self->lname_alloc)
                free(self->long_name);
        free(self);
 }
@@ -275,7 +295,9 @@ static void symbols__insert_by_name(struct rb_root *self, struct symbol *sym)
 {
        struct rb_node **p = &self->rb_node;
        struct rb_node *parent = NULL;
-       struct symbol_name_rb_node *symn = ((void *)sym) - sizeof(*parent), *s;
+       struct symbol_name_rb_node *symn, *s;
+
+       symn = container_of(sym, struct symbol_name_rb_node, sym);
 
        while (*p != NULL) {
                parent = *p;
@@ -368,6 +390,20 @@ size_t dso__fprintf_buildid(struct dso *self, FILE *fp)
        return fprintf(fp, "%s", sbuild_id);
 }
 
+size_t dso__fprintf_symbols_by_name(struct dso *self, enum map_type type, FILE *fp)
+{
+       size_t ret = 0;
+       struct rb_node *nd;
+       struct symbol_name_rb_node *pos;
+
+       for (nd = rb_first(&self->symbol_names[type]); nd; nd = rb_next(nd)) {
+               pos = rb_entry(nd, struct symbol_name_rb_node, rb_node);
+               fprintf(fp, "%s\n", pos->sym.name);
+       }
+
+       return ret;
+}
+
 size_t dso__fprintf(struct dso *self, enum map_type type, FILE *fp)
 {
        struct rb_node *nd;
@@ -438,6 +474,14 @@ struct process_kallsyms_args {
        struct dso *dso;
 };
 
+static u8 kallsyms2elf_type(char type)
+{
+       if (type == 'W')
+               return STB_WEAK;
+
+       return isupper(type) ? STB_GLOBAL : STB_LOCAL;
+}
+
 static int map__process_kallsym_symbol(void *arg, const char *name,
                                       char type, u64 start)
 {
@@ -451,7 +495,7 @@ static int map__process_kallsym_symbol(void *arg, const char *name,
        /*
         * Will fix up the end later, when we have all symbols sorted.
         */
-       sym = symbol__new(start, 0, name);
+       sym = symbol__new(start, 0, kallsyms2elf_type(type), name);
 
        if (sym == NULL)
                return -ENOMEM;
@@ -485,7 +529,7 @@ static int dso__split_kallsyms(struct dso *self, struct map *map,
                               symbol_filter_t filter)
 {
        struct map_groups *kmaps = map__kmap(map)->kmaps;
-       struct kernel_info *kerninfo = kmaps->this_kerninfo;
+       struct machine *machine = kmaps->machine;
        struct map *curr_map = map;
        struct symbol *pos;
        int count = 0;
@@ -508,8 +552,8 @@ static int dso__split_kallsyms(struct dso *self, struct map *map,
 
                        if (strcmp(curr_map->dso->short_name, module)) {
                                if (curr_map != map &&
-                                       self->kernel == DSO_TYPE_GUEST_KERNEL &&
-                                       is_default_guest(kerninfo)) {
+                                   self->kernel == DSO_TYPE_GUEST_KERNEL &&
+                                   machine__is_default_guest(machine)) {
                                        /*
                                         * We assume all symbols of a module are
                                         * continuous in * kallsyms, so curr_map
@@ -524,16 +568,16 @@ static int dso__split_kallsyms(struct dso *self, struct map *map,
                                curr_map = map_groups__find_by_name(kmaps,
                                                        map->type, module);
                                if (curr_map == NULL) {
-                                       pr_err("%s/proc/{kallsyms,modules} "
+                                       pr_debug("%s/proc/{kallsyms,modules} "
                                                 "inconsistency while looking "
                                                 "for \"%s\" module!\n",
-                                                kerninfo->root_dir, module);
+                                                machine->root_dir, module);
                                        curr_map = map;
                                        goto discard_symbol;
                                }
 
                                if (curr_map->dso->loaded &&
-                                       !is_default_guest(kmaps->this_kerninfo))
+                                   !machine__is_default_guest(machine))
                                        goto discard_symbol;
                        }
                        /*
@@ -586,7 +630,7 @@ discard_symbol:             rb_erase(&pos->rb_node, root);
 
        if (curr_map != map &&
            self->kernel == DSO_TYPE_GUEST_KERNEL &&
-           is_default_guest(kmaps->this_kerninfo)) {
+           machine__is_default_guest(kmaps->machine)) {
                dso__set_loaded(curr_map->dso, curr_map->type);
        }
 
@@ -646,7 +690,7 @@ static int dso__load_perf_map(struct dso *self, struct map *map,
                if (len + 2 >= line_len)
                        continue;
 
-               sym = symbol__new(start, size, line + len);
+               sym = symbol__new(start, size, STB_GLOBAL, line + len);
 
                if (sym == NULL)
                        goto out_delete_line;
@@ -858,7 +902,7 @@ static int dso__synthesize_plt_symbols(struct  dso *self, struct map *map,
                                 "%s@plt", elf_sym__name(&sym, symstrs));
 
                        f = symbol__new(plt_offset, shdr_plt.sh_entsize,
-                                       sympltname);
+                                       STB_GLOBAL, sympltname);
                        if (!f)
                                goto out_elf_end;
 
@@ -880,7 +924,7 @@ static int dso__synthesize_plt_symbols(struct  dso *self, struct map *map,
                                 "%s@plt", elf_sym__name(&sym, symstrs));
 
                        f = symbol__new(plt_offset, shdr_plt.sh_entsize,
-                                       sympltname);
+                                       STB_GLOBAL, sympltname);
                        if (!f)
                                goto out_elf_end;
 
@@ -931,8 +975,28 @@ static bool elf_sec__is_a(GElf_Shdr *self, Elf_Data *secstrs, enum map_type type
        }
 }
 
+static size_t elf_addr_to_index(Elf *elf, GElf_Addr addr)
+{
+       Elf_Scn *sec = NULL;
+       GElf_Shdr shdr;
+       size_t cnt = 1;
+
+       while ((sec = elf_nextscn(elf, sec)) != NULL) {
+               gelf_getshdr(sec, &shdr);
+
+               if ((addr >= shdr.sh_addr) &&
+                   (addr < (shdr.sh_addr + shdr.sh_size)))
+                       return cnt;
+
+               ++cnt;
+       }
+
+       return -1;
+}
+
 static int dso__load_sym(struct dso *self, struct map *map, const char *name,
-                        int fd, symbol_filter_t filter, int kmodule)
+                        int fd, symbol_filter_t filter, int kmodule,
+                        int want_symtab)
 {
        struct kmap *kmap = self->kernel ? map__kmap(map) : NULL;
        struct map *curr_map = map;
@@ -942,31 +1006,51 @@ static int dso__load_sym(struct dso *self, struct map *map, const char *name,
        int err = -1;
        uint32_t idx;
        GElf_Ehdr ehdr;
-       GElf_Shdr shdr;
-       Elf_Data *syms;
+       GElf_Shdr shdr, opdshdr;
+       Elf_Data *syms, *opddata = NULL;
        GElf_Sym sym;
-       Elf_Scn *sec, *sec_strndx;
+       Elf_Scn *sec, *sec_strndx, *opdsec;
        Elf *elf;
        int nr = 0;
+       size_t opdidx = 0;
 
        elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
        if (elf == NULL) {
-               pr_err("%s: cannot read %s ELF file.\n", __func__, name);
+               pr_debug("%s: cannot read %s ELF file.\n", __func__, name);
                goto out_close;
        }
 
        if (gelf_getehdr(elf, &ehdr) == NULL) {
-               pr_err("%s: cannot get elf header.\n", __func__);
+               pr_debug("%s: cannot get elf header.\n", __func__);
                goto out_elf_end;
        }
 
+       /* Always reject images with a mismatched build-id: */
+       if (self->has_build_id) {
+               u8 build_id[BUILD_ID_SIZE];
+
+               if (elf_read_build_id(elf, build_id,
+                                     BUILD_ID_SIZE) != BUILD_ID_SIZE)
+                       goto out_elf_end;
+
+               if (!dso__build_id_equal(self, build_id))
+                       goto out_elf_end;
+       }
+
        sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
        if (sec == NULL) {
+               if (want_symtab)
+                       goto out_elf_end;
+
                sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
                if (sec == NULL)
                        goto out_elf_end;
        }
 
+       opdsec = elf_section_by_name(elf, &ehdr, &opdshdr, ".opd", &opdidx);
+       if (opdsec)
+               opddata = elf_rawdata(opdsec, NULL);
+
        syms = elf_getdata(sec, NULL);
        if (syms == NULL)
                goto out_elf_end;
@@ -1011,6 +1095,23 @@ static int dso__load_sym(struct dso *self, struct map *map, const char *name,
                if (!is_label && !elf_sym__is_a(&sym, map->type))
                        continue;
 
+               /* Reject ARM ELF "mapping symbols": these aren't unique and
+                * don't identify functions, so will confuse the profile
+                * output: */
+               if (ehdr.e_machine == EM_ARM) {
+                       if (!strcmp(elf_name, "$a") ||
+                           !strcmp(elf_name, "$d") ||
+                           !strcmp(elf_name, "$t"))
+                               continue;
+               }
+
+               if (opdsec && sym.st_shndx == opdidx) {
+                       u32 offset = sym.st_value - opdshdr.sh_addr;
+                       u64 *opd = opddata->d_buf + offset;
+                       sym.st_value = *opd;
+                       sym.st_shndx = elf_addr_to_index(elf, sym.st_value);
+               }
+
                sec = elf_getscn(elf, sym.st_shndx);
                if (!sec)
                        goto out_elf_end;
@@ -1084,7 +1185,8 @@ static int dso__load_sym(struct dso *self, struct map *map, const char *name,
                if (demangled != NULL)
                        elf_name = demangled;
 new_symbol:
-               f = symbol__new(sym.st_value, sym.st_size, elf_name);
+               f = symbol__new(sym.st_value, sym.st_size,
+                               GELF_ST_BIND(sym.st_info), elf_name);
                free(demangled);
                if (!f)
                        goto out_elf_end;
@@ -1130,6 +1232,10 @@ bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
        list_for_each_entry(pos, head, node) {
                if (with_hits && !pos->hit)
                        continue;
+               if (pos->has_build_id) {
+                       have_build_id = true;
+                       continue;
+               }
                if (filename__read_build_id(pos->long_name, pos->build_id,
                                            sizeof(pos->build_id)) > 0) {
                        have_build_id     = true;
@@ -1145,37 +1251,26 @@ bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
  */
 #define NOTE_ALIGN(n) (((n) + 3) & -4U)
 
-int filename__read_build_id(const char *filename, void *bf, size_t size)
+static int elf_read_build_id(Elf *elf, void *bf, size_t size)
 {
-       int fd, err = -1;
+       int err = -1;
        GElf_Ehdr ehdr;
        GElf_Shdr shdr;
        Elf_Data *data;
        Elf_Scn *sec;
        Elf_Kind ek;
        void *ptr;
-       Elf *elf;
 
        if (size < BUILD_ID_SIZE)
                goto out;
 
-       fd = open(filename, O_RDONLY);
-       if (fd < 0)
-               goto out;
-
-       elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
-       if (elf == NULL) {
-               pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
-               goto out_close;
-       }
-
        ek = elf_kind(elf);
        if (ek != ELF_K_ELF)
-               goto out_elf_end;
+               goto out;
 
        if (gelf_getehdr(elf, &ehdr) == NULL) {
                pr_err("%s: cannot get elf header.\n", __func__);
-               goto out_elf_end;
+               goto out;
        }
 
        sec = elf_section_by_name(elf, &ehdr, &shdr,
@@ -1184,12 +1279,12 @@ int filename__read_build_id(const char *filename, void *bf, size_t size)
                sec = elf_section_by_name(elf, &ehdr, &shdr,
                                          ".notes", NULL);
                if (sec == NULL)
-                       goto out_elf_end;
+                       goto out;
        }
 
        data = elf_getdata(sec, NULL);
        if (data == NULL)
-               goto out_elf_end;
+               goto out;
 
        ptr = data->d_buf;
        while (ptr < (data->d_buf + data->d_size)) {
@@ -1211,7 +1306,31 @@ int filename__read_build_id(const char *filename, void *bf, size_t size)
                }
                ptr += descsz;
        }
-out_elf_end:
+
+out:
+       return err;
+}
+
+int filename__read_build_id(const char *filename, void *bf, size_t size)
+{
+       int fd, err = -1;
+       Elf *elf;
+
+       if (size < BUILD_ID_SIZE)
+               goto out;
+
+       fd = open(filename, O_RDONLY);
+       if (fd < 0)
+               goto out;
+
+       elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
+       if (elf == NULL) {
+               pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
+               goto out_close;
+       }
+
+       err = elf_read_build_id(elf, bf, size);
+
        elf_end(elf);
 out_close:
        close(fd);
@@ -1287,12 +1406,11 @@ int dso__load(struct dso *self, struct map *map, symbol_filter_t filter)
 {
        int size = PATH_MAX;
        char *name;
-       u8 build_id[BUILD_ID_SIZE];
-       char build_id_hex[BUILD_ID_SIZE * 2 + 1];
        int ret = -1;
        int fd;
-       struct kernel_info *kerninfo;
+       struct machine *machine;
        const char *root_dir;
+       int want_symtab;
 
        dso__set_loaded(self, map->type);
 
@@ -1301,10 +1419,10 @@ int dso__load(struct dso *self, struct map *map, symbol_filter_t filter)
        else if (self->kernel == DSO_TYPE_GUEST_KERNEL)
                return dso__load_guest_kernel_sym(self, map, filter);
 
-       if (map->groups && map->groups->this_kerninfo)
-               kerninfo = map->groups->this_kerninfo;
+       if (map->groups && map->groups->machine)
+               machine = map->groups->machine;
        else
-               kerninfo = NULL;
+               machine = NULL;
 
        name = malloc(size);
        if (!name)
@@ -1319,20 +1437,18 @@ int dso__load(struct dso *self, struct map *map, symbol_filter_t filter)
                return ret;
        }
 
-       self->origin = DSO__ORIG_BUILD_ID_CACHE;
-
-       if (self->has_build_id) {
-               build_id__sprintf(self->build_id, sizeof(self->build_id),
-                                 build_id_hex);
-               snprintf(name, size, "%s/%s/.build-id/%.2s/%s",
-                        getenv("HOME"), DEBUG_CACHE_DIR,
-                        build_id_hex, build_id_hex + 2);
-               goto open_file;
-       }
-more:
-       do {
-               self->origin++;
+       /* Iterate over candidate debug images.
+        * On the first pass, only load images if they have a full symtab.
+        * Failing that, do a second pass where we accept .dynsym also
+        */
+       for (self->origin = DSO__ORIG_BUILD_ID_CACHE, want_symtab = 1;
+            self->origin != DSO__ORIG_NOT_FOUND;
+            self->origin++) {
                switch (self->origin) {
+               case DSO__ORIG_BUILD_ID_CACHE:
+                       if (dso__build_id_filename(self, name, size) == NULL)
+                               continue;
+                       break;
                case DSO__ORIG_FEDORA:
                        snprintf(name, size, "/usr/lib/debug%s.debug",
                                 self->long_name);
@@ -1341,62 +1457,67 @@ more:
                        snprintf(name, size, "/usr/lib/debug%s",
                                 self->long_name);
                        break;
-               case DSO__ORIG_BUILDID:
-                       if (filename__read_build_id(self->long_name, build_id,
-                                                   sizeof(build_id))) {
-                               build_id__sprintf(build_id, sizeof(build_id),
-                                                 build_id_hex);
-                               snprintf(name, size,
-                                        "/usr/lib/debug/.build-id/%.2s/%s.debug",
-                                       build_id_hex, build_id_hex + 2);
-                               if (self->has_build_id)
-                                       goto compare_build_id;
-                               break;
+               case DSO__ORIG_BUILDID: {
+                       char build_id_hex[BUILD_ID_SIZE * 2 + 1];
+
+                       if (!self->has_build_id)
+                               continue;
+
+                       build_id__sprintf(self->build_id,
+                                         sizeof(self->build_id),
+                                         build_id_hex);
+                       snprintf(name, size,
+                                "/usr/lib/debug/.build-id/%.2s/%s.debug",
+                                build_id_hex, build_id_hex + 2);
                        }
-                       self->origin++;
-                       /* Fall thru */
+                       break;
                case DSO__ORIG_DSO:
                        snprintf(name, size, "%s", self->long_name);
                        break;
                case DSO__ORIG_GUEST_KMODULE:
-                       if (map->groups && map->groups->this_kerninfo)
-                               root_dir = map->groups->this_kerninfo->root_dir;
+                       if (map->groups && map->groups->machine)
+                               root_dir = map->groups->machine->root_dir;
                        else
                                root_dir = "";
                        snprintf(name, size, "%s%s", root_dir, self->long_name);
                        break;
 
                default:
-                       goto out;
+                       /*
+                        * If we wanted a full symtab but no image had one,
+                        * relax our requirements and repeat the search.
+                        */
+                       if (want_symtab) {
+                               want_symtab = 0;
+                               self->origin = DSO__ORIG_BUILD_ID_CACHE;
+                       } else
+                               continue;
                }
 
-               if (self->has_build_id) {
-                       if (filename__read_build_id(name, build_id,
-                                                   sizeof(build_id)) < 0)
-                               goto more;
-compare_build_id:
-                       if (!dso__build_id_equal(self, build_id))
-                               goto more;
-               }
-open_file:
+               /* Name is now the name of the next image to try */
                fd = open(name, O_RDONLY);
-       } while (fd < 0);
+               if (fd < 0)
+                       continue;
 
-       ret = dso__load_sym(self, map, name, fd, filter, 0);
-       close(fd);
+               ret = dso__load_sym(self, map, name, fd, filter, 0,
+                                   want_symtab);
+               close(fd);
 
-       /*
-        * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
-        */
-       if (!ret)
-               goto more;
+               /*
+                * Some people seem to have debuginfo files _WITHOUT_ debug
+                * info!?!?
+                */
+               if (!ret)
+                       continue;
 
-       if (ret > 0) {
-               int nr_plt = dso__synthesize_plt_symbols(self, map, filter);
-               if (nr_plt > 0)
-                       ret += nr_plt;
+               if (ret > 0) {
+                       int nr_plt = dso__synthesize_plt_symbols(self, map, filter);
+                       if (nr_plt > 0)
+                               ret += nr_plt;
+                       break;
+               }
        }
-out:
+
        free(name);
        if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
                return 0;
@@ -1444,6 +1565,7 @@ static int map_groups__set_modules_path_dir(struct map_groups *self,
 {
        struct dirent *dent;
        DIR *dir = opendir(dir_name);
+       int ret = 0;
 
        if (!dir) {
                pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
@@ -1466,8 +1588,9 @@ static int map_groups__set_modules_path_dir(struct map_groups *self,
 
                        snprintf(path, sizeof(path), "%s/%s",
                                 dir_name, dent->d_name);
-                       if (map_groups__set_modules_path_dir(self, path) < 0)
-                               goto failure;
+                       ret = map_groups__set_modules_path_dir(self, path);
+                       if (ret < 0)
+                               goto out;
                } else {
                        char *dot = strrchr(dent->d_name, '.'),
                             dso_name[PATH_MAX];
@@ -1488,17 +1611,19 @@ static int map_groups__set_modules_path_dir(struct map_groups *self,
                                 dir_name, dent->d_name);
 
                        long_name = strdup(path);
-                       if (long_name == NULL)
-                               goto failure;
+                       if (long_name == NULL) {
+                               ret = -1;
+                               goto out;
+                       }
                        dso__set_long_name(map->dso, long_name);
+                       map->dso->lname_alloc = 1;
                        dso__kernel_module_get_build_id(map->dso, "");
                }
        }
 
-       return 0;
-failure:
+out:
        closedir(dir);
-       return -1;
+       return ret;
 }
 
 static char *get_kernel_version(const char *root_dir)
@@ -1528,21 +1653,20 @@ static char *get_kernel_version(const char *root_dir)
        return strdup(name);
 }
 
-static int map_groups__set_modules_path(struct map_groups *self,
-                               const char *root_dir)
+static int machine__set_modules_path(struct machine *self)
 {
        char *version;
        char modules_path[PATH_MAX];
 
-       version = get_kernel_version(root_dir);
+       version = get_kernel_version(self->root_dir);
        if (!version)
                return -1;
 
        snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s/kernel",
-                root_dir, version);
+                self->root_dir, version);
        free(version);
 
-       return map_groups__set_modules_path_dir(self, modules_path);
+       return map_groups__set_modules_path_dir(&self->kmaps, modules_path);
 }
 
 /*
@@ -1564,14 +1688,12 @@ static struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
        return self;
 }
 
-struct map *map_groups__new_module(struct map_groups *self, u64 start,
-                               const char *filename,
-                               struct kernel_info *kerninfo)
+struct map *machine__new_module(struct machine *self, u64 start,
+                               const char *filename)
 {
        struct map *map;
-       struct dso *dso;
+       struct dso *dso = __dsos__findnew(&self->kernel_dsos, filename);
 
-       dso = __dsos__findnew(&kerninfo->dsos__kernel, filename);
        if (dso == NULL)
                return NULL;
 
@@ -1579,28 +1701,27 @@ struct map *map_groups__new_module(struct map_groups *self, u64 start,
        if (map == NULL)
                return NULL;
 
-       if (is_host_kernel(kerninfo))
+       if (machine__is_host(self))
                dso->origin = DSO__ORIG_KMODULE;
        else
                dso->origin = DSO__ORIG_GUEST_KMODULE;
-       map_groups__insert(self, map);
+       map_groups__insert(&self->kmaps, map);
        return map;
 }
 
-static int map_groups__create_modules(struct kernel_info *kerninfo)
+static int machine__create_modules(struct machine *self)
 {
        char *line = NULL;
        size_t n;
        FILE *file;
        struct map *map;
-       const char *root_dir;
        const char *modules;
        char path[PATH_MAX];
 
-       if (is_default_guest(kerninfo))
+       if (machine__is_default_guest(self))
                modules = symbol_conf.default_guest_modules;
        else {
-               sprintf(path, "%s/proc/modules", kerninfo->root_dir);
+               sprintf(path, "%s/proc/modules", self->root_dir);
                modules = path;
        }
 
@@ -1608,8 +1729,6 @@ static int map_groups__create_modules(struct kernel_info *kerninfo)
        if (file == NULL)
                return -1;
 
-       root_dir = kerninfo->root_dir;
-
        while (!feof(file)) {
                char name[PATH_MAX];
                u64 start;
@@ -1638,17 +1757,16 @@ static int map_groups__create_modules(struct kernel_info *kerninfo)
                *sep = '\0';
 
                snprintf(name, sizeof(name), "[%s]", line);
-               map = map_groups__new_module(&kerninfo->kmaps,
-                               start, name, kerninfo);
+               map = machine__new_module(self, start, name);
                if (map == NULL)
                        goto out_delete_line;
-               dso__kernel_module_get_build_id(map->dso, root_dir);
+               dso__kernel_module_get_build_id(map->dso, self->root_dir);
        }
 
        free(line);
        fclose(file);
 
-       return map_groups__set_modules_path(&kerninfo->kmaps, root_dir);
+       return machine__set_modules_path(self);
 
 out_delete_line:
        free(line);
@@ -1661,36 +1779,12 @@ static int dso__load_vmlinux(struct dso *self, struct map *map,
 {
        int err = -1, fd;
 
-       if (self->has_build_id) {
-               u8 build_id[BUILD_ID_SIZE];
-
-               if (filename__read_build_id(vmlinux, build_id,
-                                           sizeof(build_id)) < 0) {
-                       pr_debug("No build_id in %s, ignoring it\n", vmlinux);
-                       return -1;
-               }
-               if (!dso__build_id_equal(self, build_id)) {
-                       char expected_build_id[BUILD_ID_SIZE * 2 + 1],
-                            vmlinux_build_id[BUILD_ID_SIZE * 2 + 1];
-
-                       build_id__sprintf(self->build_id,
-                                         sizeof(self->build_id),
-                                         expected_build_id);
-                       build_id__sprintf(build_id, sizeof(build_id),
-                                         vmlinux_build_id);
-                       pr_debug("build_id in %s is %s while expected is %s, "
-                                "ignoring it\n", vmlinux, vmlinux_build_id,
-                                expected_build_id);
-                       return -1;
-               }
-       }
-
        fd = open(vmlinux, O_RDONLY);
        if (fd < 0)
                return -1;
 
        dso__set_loaded(self, map->type);
-       err = dso__load_sym(self, map, vmlinux, fd, filter, 0);
+       err = dso__load_sym(self, map, vmlinux, fd, filter, 0, 0);
        close(fd);
 
        if (err > 0)
@@ -1703,9 +1797,20 @@ int dso__load_vmlinux_path(struct dso *self, struct map *map,
                           symbol_filter_t filter)
 {
        int i, err = 0;
+       char *filename;
 
        pr_debug("Looking at the vmlinux_path (%d entries long)\n",
-                vmlinux_path__nr_entries);
+                vmlinux_path__nr_entries + 1);
+
+       filename = dso__build_id_filename(self, NULL, 0);
+       if (filename != NULL) {
+               err = dso__load_vmlinux(self, map, filename, filter);
+               if (err > 0) {
+                       dso__set_long_name(self, filename);
+                       goto out;
+               }
+               free(filename);
+       }
 
        for (i = 0; i < vmlinux_path__nr_entries; ++i) {
                err = dso__load_vmlinux(self, map, vmlinux_path[i], filter);
@@ -1714,7 +1819,7 @@ int dso__load_vmlinux_path(struct dso *self, struct map *map,
                        break;
                }
        }
-
+out:
        return err;
 }
 
@@ -1725,8 +1830,8 @@ static int dso__load_kernel_sym(struct dso *self, struct map *map,
        const char *kallsyms_filename = NULL;
        char *kallsyms_allocated_filename = NULL;
        /*
-        * Step 1: if the user specified a vmlinux filename, use it and only
-        * it, reporting errors to the user if it cannot be used.
+        * Step 1: if the user specified a kallsyms or vmlinux filename, use
+        * it and only it, reporting errors to the user if it cannot be used.
         *
         * For instance, try to analyse an ARM perf.data file _without_ a
         * build-id, or if the user specifies the wrong path to the right
@@ -1739,10 +1844,20 @@ static int dso__load_kernel_sym(struct dso *self, struct map *map,
         * validation in dso__load_vmlinux and will bail out if they don't
         * match.
         */
+       if (symbol_conf.kallsyms_name != NULL) {
+               kallsyms_filename = symbol_conf.kallsyms_name;
+               goto do_kallsyms;
+       }
+
        if (symbol_conf.vmlinux_name != NULL) {
                err = dso__load_vmlinux(self, map,
                                        symbol_conf.vmlinux_name, filter);
-               goto out_try_fixup;
+               if (err > 0) {
+                       dso__set_long_name(self,
+                                          strdup(symbol_conf.vmlinux_name));
+                       goto out_fixup;
+               }
+               return err;
        }
 
        if (vmlinux_path != NULL) {
@@ -1803,7 +1918,6 @@ do_kallsyms:
                pr_debug("Using %s for symbols\n", kallsyms_filename);
        free(kallsyms_allocated_filename);
 
-out_try_fixup:
        if (err > 0) {
 out_fixup:
                if (kallsyms_filename != NULL)
@@ -1820,16 +1934,16 @@ static int dso__load_guest_kernel_sym(struct dso *self, struct map *map,
 {
        int err;
        const char *kallsyms_filename = NULL;
-       struct kernel_info *kerninfo;
+       struct machine *machine;
        char path[PATH_MAX];
 
        if (!map->groups) {
                pr_debug("Guest kernel map hasn't the point to groups\n");
                return -1;
        }
-       kerninfo = map->groups->this_kerninfo;
+       machine = map->groups->machine;
 
-       if (is_default_guest(kerninfo)) {
+       if (machine__is_default_guest(machine)) {
                /*
                 * if the user specified a vmlinux filename, use it and only
                 * it, reporting errors to the user if it cannot be used.
@@ -1845,7 +1959,7 @@ static int dso__load_guest_kernel_sym(struct dso *self, struct map *map,
                if (!kallsyms_filename)
                        return -1;
        } else {
-               sprintf(path, "%s/proc/kallsyms", kerninfo->root_dir);
+               sprintf(path, "%s/proc/kallsyms", machine->root_dir);
                kallsyms_filename = path;
        }
 
@@ -1856,9 +1970,8 @@ static int dso__load_guest_kernel_sym(struct dso *self, struct map *map,
 out_try_fixup:
        if (err > 0) {
                if (kallsyms_filename != NULL) {
-                       kern_mmap_name(kerninfo, path);
-                       dso__set_long_name(self,
-                               strdup(path));
+                       machine__mmap_name(machine, path, sizeof(path));
+                       dso__set_long_name(self, strdup(path));
                }
                map__fixup_start(map);
                map__fixup_end(map);
@@ -1897,27 +2010,32 @@ struct dso *__dsos__findnew(struct list_head *head, const char *name)
        return dso;
 }
 
-static void __dsos__fprintf(struct list_head *head, FILE *fp)
+size_t __dsos__fprintf(struct list_head *head, FILE *fp)
 {
        struct dso *pos;
+       size_t ret = 0;
 
        list_for_each_entry(pos, head, node) {
                int i;
                for (i = 0; i < MAP__NR_TYPES; ++i)
-                       dso__fprintf(pos, i, fp);
+                       ret += dso__fprintf(pos, i, fp);
        }
+
+       return ret;
 }
 
-void dsos__fprintf(struct rb_root *kerninfo_root, FILE *fp)
+size_t machines__fprintf_dsos(struct rb_root *self, FILE *fp)
 {
        struct rb_node *nd;
+       size_t ret = 0;
 
-       for (nd = rb_first(kerninfo_root); nd; nd = rb_next(nd)) {
-               struct kernel_info *pos = rb_entry(nd, struct kernel_info,
-                               rb_node);
-               __dsos__fprintf(&pos->dsos__kernel, fp);
-               __dsos__fprintf(&pos->dsos__user, fp);
+       for (nd = rb_first(self); nd; nd = rb_next(nd)) {
+               struct machine *pos = rb_entry(nd, struct machine, rb_node);
+               ret += __dsos__fprintf(&pos->kernel_dsos, fp);
+               ret += __dsos__fprintf(&pos->user_dsos, fp);
        }
+
+       return ret;
 }
 
 static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
@@ -1935,19 +2053,20 @@ static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
        return ret;
 }
 
-size_t dsos__fprintf_buildid(struct rb_root *kerninfo_root,
-               FILE *fp, bool with_hits)
+size_t machine__fprintf_dsos_buildid(struct machine *self, FILE *fp, bool with_hits)
+{
+       return __dsos__fprintf_buildid(&self->kernel_dsos, fp, with_hits) +
+              __dsos__fprintf_buildid(&self->user_dsos, fp, with_hits);
+}
+
+size_t machines__fprintf_dsos_buildid(struct rb_root *self, FILE *fp, bool with_hits)
 {
        struct rb_node *nd;
        size_t ret = 0;
 
-       for (nd = rb_first(kerninfo_root); nd; nd = rb_next(nd)) {
-               struct kernel_info *pos = rb_entry(nd, struct kernel_info,
-                               rb_node);
-               ret += __dsos__fprintf_buildid(&pos->dsos__kernel,
-                                       fp, with_hits);
-               ret += __dsos__fprintf_buildid(&pos->dsos__user,
-                                       fp, with_hits);
+       for (nd = rb_first(self); nd; nd = rb_next(nd)) {
+               struct machine *pos = rb_entry(nd, struct machine, rb_node);
+               ret += machine__fprintf_dsos_buildid(pos, fp, with_hits);
        }
        return ret;
 }
@@ -1964,14 +2083,12 @@ struct dso *dso__new_kernel(const char *name)
        return self;
 }
 
-static struct dso *dso__new_guest_kernel(struct kernel_info *kerninfo,
+static struct dso *dso__new_guest_kernel(struct machine *machine,
                                        const char *name)
 {
-       char buff[PATH_MAX];
-       struct dso *self;
+       char bf[PATH_MAX];
+       struct dso *self = dso__new(name ?: machine__mmap_name(machine, bf, sizeof(bf)));
 
-       kern_mmap_name(kerninfo, buff);
-       self = dso__new(name ?: buff);
        if (self != NULL) {
                dso__set_short_name(self, "[guest.kernel]");
                self->kernel = DSO_TYPE_GUEST_KERNEL;
@@ -1980,64 +2097,108 @@ static struct dso *dso__new_guest_kernel(struct kernel_info *kerninfo,
        return self;
 }
 
-void dso__read_running_kernel_build_id(struct dso *self,
-                       struct kernel_info *kerninfo)
+void dso__read_running_kernel_build_id(struct dso *self, struct machine *machine)
 {
        char path[PATH_MAX];
 
-       if (is_default_guest(kerninfo))
+       if (machine__is_default_guest(machine))
                return;
-       sprintf(path, "%s/sys/kernel/notes", kerninfo->root_dir);
+       sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
        if (sysfs__read_build_id(path, self->build_id,
                                 sizeof(self->build_id)) == 0)
                self->has_build_id = true;
 }
 
-static struct dso *dsos__create_kernel(struct kernel_info *kerninfo)
+static struct dso *machine__create_kernel(struct machine *self)
 {
        const char *vmlinux_name = NULL;
        struct dso *kernel;
 
-       if (is_host_kernel(kerninfo)) {
+       if (machine__is_host(self)) {
                vmlinux_name = symbol_conf.vmlinux_name;
                kernel = dso__new_kernel(vmlinux_name);
        } else {
-               if (is_default_guest(kerninfo))
+               if (machine__is_default_guest(self))
                        vmlinux_name = symbol_conf.default_guest_vmlinux_name;
-               kernel = dso__new_guest_kernel(kerninfo, vmlinux_name);
+               kernel = dso__new_guest_kernel(self, vmlinux_name);
        }
 
        if (kernel != NULL) {
-               dso__read_running_kernel_build_id(kernel, kerninfo);
-               dsos__add(&kerninfo->dsos__kernel, kernel);
+               dso__read_running_kernel_build_id(kernel, self);
+               dsos__add(&self->kernel_dsos, kernel);
        }
        return kernel;
 }
 
-int __map_groups__create_kernel_maps(struct map_groups *self,
-                                    struct map *vmlinux_maps[MAP__NR_TYPES],
-                                    struct dso *kernel)
+int __machine__create_kernel_maps(struct machine *self, struct dso *kernel)
 {
        enum map_type type;
 
        for (type = 0; type < MAP__NR_TYPES; ++type) {
                struct kmap *kmap;
 
-               vmlinux_maps[type] = map__new2(0, kernel, type);
-               if (vmlinux_maps[type] == NULL)
+               self->vmlinux_maps[type] = map__new2(0, kernel, type);
+               if (self->vmlinux_maps[type] == NULL)
                        return -1;
 
-               vmlinux_maps[type]->map_ip =
-                       vmlinux_maps[type]->unmap_ip = identity__map_ip;
+               self->vmlinux_maps[type]->map_ip =
+                       self->vmlinux_maps[type]->unmap_ip = identity__map_ip;
 
-               kmap = map__kmap(vmlinux_maps[type]);
-               kmap->kmaps = self;
-               map_groups__insert(self, vmlinux_maps[type]);
+               kmap = map__kmap(self->vmlinux_maps[type]);
+               kmap->kmaps = &self->kmaps;
+               map_groups__insert(&self->kmaps, self->vmlinux_maps[type]);
        }
 
        return 0;
 }
 
+void machine__destroy_kernel_maps(struct machine *self)
+{
+       enum map_type type;
+
+       for (type = 0; type < MAP__NR_TYPES; ++type) {
+               struct kmap *kmap;
+
+               if (self->vmlinux_maps[type] == NULL)
+                       continue;
+
+               kmap = map__kmap(self->vmlinux_maps[type]);
+               map_groups__remove(&self->kmaps, self->vmlinux_maps[type]);
+               if (kmap->ref_reloc_sym) {
+                       /*
+                        * ref_reloc_sym is shared among all maps, so free just
+                        * on one of them.
+                        */
+                       if (type == MAP__FUNCTION) {
+                               free((char *)kmap->ref_reloc_sym->name);
+                               kmap->ref_reloc_sym->name = NULL;
+                               free(kmap->ref_reloc_sym);
+                       }
+                       kmap->ref_reloc_sym = NULL;
+               }
+
+               map__delete(self->vmlinux_maps[type]);
+               self->vmlinux_maps[type] = NULL;
+       }
+}
+
+int machine__create_kernel_maps(struct machine *self)
+{
+       struct dso *kernel = machine__create_kernel(self);
+
+       if (kernel == NULL ||
+           __machine__create_kernel_maps(self, kernel) < 0)
+               return -1;
+
+       if (symbol_conf.use_modules && machine__create_modules(self) < 0)
+               pr_debug("Problems creating module maps, continuing anyway...\n");
+       /*
+        * Now that we have all the maps created, just set the ->end of them:
+        */
+       map_groups__fixup_end(&self->kmaps);
+       return 0;
+}
+
 static void vmlinux_path__exit(void)
 {
        while (--vmlinux_path__nr_entries >= 0) {
@@ -2093,13 +2254,21 @@ out_fail:
        return -1;
 }
 
-size_t vmlinux_path__fprintf(FILE *fp)
+size_t machine__fprintf_vmlinux_path(struct machine *self, FILE *fp)
 {
        int i;
        size_t printed = 0;
+       struct dso *kdso = self->vmlinux_maps[MAP__FUNCTION]->dso;
+
+       if (kdso->has_build_id) {
+               char filename[PATH_MAX];
+               if (dso__build_id_filename(kdso, filename, sizeof(filename)))
+                       printed += fprintf(fp, "[0] %s\n", filename);
+       }
 
        for (i = 0; i < vmlinux_path__nr_entries; ++i)
-               printed += fprintf(fp, "[%d] %s\n", i, vmlinux_path[i]);
+               printed += fprintf(fp, "[%d] %s\n",
+                                  i + kdso->has_build_id, vmlinux_path[i]);
 
        return printed;
 }
@@ -2120,6 +2289,9 @@ static int setup_list(struct strlist **list, const char *list_str,
 
 int symbol__init(void)
 {
+       if (symbol_conf.initialized)
+               return 0;
+
        elf_version(EV_CURRENT);
        if (symbol_conf.sort_by_name)
                symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
@@ -2145,6 +2317,7 @@ int symbol__init(void)
                       symbol_conf.sym_list_str, "symbol") < 0)
                goto out_free_comm_list;
 
+       symbol_conf.initialized = true;
        return 0;
 
 out_free_dso_list:
@@ -2154,30 +2327,26 @@ out_free_comm_list:
        return -1;
 }
 
-int map_groups__create_kernel_maps(struct rb_root *kerninfo_root, pid_t pid)
+void symbol__exit(void)
 {
-       struct kernel_info *kerninfo;
-       struct dso *kernel;
+       if (!symbol_conf.initialized)
+               return;
+       strlist__delete(symbol_conf.sym_list);
+       strlist__delete(symbol_conf.dso_list);
+       strlist__delete(symbol_conf.comm_list);
+       vmlinux_path__exit();
+       symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
+       symbol_conf.initialized = false;
+}
 
-       kerninfo = kerninfo__findnew(kerninfo_root, pid);
-       if (kerninfo == NULL)
-               return -1;
-       kernel = dsos__create_kernel(kerninfo);
-       if (kernel == NULL)
-               return -1;
+int machines__create_kernel_maps(struct rb_root *self, pid_t pid)
+{
+       struct machine *machine = machines__findnew(self, pid);
 
-       if (__map_groups__create_kernel_maps(&kerninfo->kmaps,
-                       kerninfo->vmlinux_maps, kernel) < 0)
+       if (machine == NULL)
                return -1;
 
-       if (symbol_conf.use_modules &&
-               map_groups__create_modules(kerninfo) < 0)
-               pr_debug("Problems creating module maps, continuing anyway...\n");
-       /*
-        * Now that we have all the maps created, just set the ->end of them:
-        */
-       map_groups__fixup_end(&kerninfo->kmaps);
-       return 0;
+       return machine__create_kernel_maps(machine);
 }
 
 static int hex(char ch)
@@ -2223,7 +2392,7 @@ char *strxfrchar(char *s, char from, char to)
        return s;
 }
 
-int map_groups__create_guest_kernel_maps(struct rb_root *kerninfo_root)
+int machines__create_guest_kernel_maps(struct rb_root *self)
 {
        int ret = 0;
        struct dirent **namelist = NULL;
@@ -2234,8 +2403,7 @@ int map_groups__create_guest_kernel_maps(struct rb_root *kerninfo_root)
        if (symbol_conf.default_guest_vmlinux_name ||
            symbol_conf.default_guest_modules ||
            symbol_conf.default_guest_kallsyms) {
-               map_groups__create_kernel_maps(kerninfo_root,
-                                       DEFAULT_GUEST_KERNEL_ID);
+               machines__create_kernel_maps(self, DEFAULT_GUEST_KERNEL_ID);
        }
 
        if (symbol_conf.guestmount) {
@@ -2256,8 +2424,7 @@ int map_groups__create_guest_kernel_maps(struct rb_root *kerninfo_root)
                                pr_debug("Can't access file %s\n", path);
                                goto failure;
                        }
-                       map_groups__create_kernel_maps(kerninfo_root,
-                                                       pid);
+                       machines__create_kernel_maps(self, pid);
                }
 failure:
                free(namelist);
@@ -2265,3 +2432,49 @@ failure:
 
        return ret;
 }
+
+void machines__destroy_guest_kernel_maps(struct rb_root *self)
+{
+       struct rb_node *next = rb_first(self);
+
+       while (next) {
+               struct machine *pos = rb_entry(next, struct machine, rb_node);
+
+               next = rb_next(&pos->rb_node);
+               rb_erase(&pos->rb_node, self);
+               machine__delete(pos);
+       }
+}
+
+int machine__load_kallsyms(struct machine *self, const char *filename,
+                          enum map_type type, symbol_filter_t filter)
+{
+       struct map *map = self->vmlinux_maps[type];
+       int ret = dso__load_kallsyms(map->dso, filename, map, filter);
+
+       if (ret > 0) {
+               dso__set_loaded(map->dso, type);
+               /*
+                * Since /proc/kallsyms will have multiple sessions for the
+                * kernel, with modules between them, fixup the end of all
+                * sections.
+                */
+               __map_groups__fixup_end(&self->kmaps, type);
+       }
+
+       return ret;
+}
+
+int machine__load_vmlinux_path(struct machine *self, enum map_type type,
+                              symbol_filter_t filter)
+{
+       struct map *map = self->vmlinux_maps[type];
+       int ret = dso__load_vmlinux_path(map->dso, map, filter);
+
+       if (ret > 0) {
+               dso__set_loaded(map->dso, type);
+               map__reloc_vmlinux(map);
+       }
+
+       return ret;
+}