]> git.karo-electronics.de Git - karo-tx-linux.git/blobdiff - tools/perf/util/dso.c
perf tools: Consolidate error path in __open_dso()
[karo-tx-linux.git] / tools / perf / util / dso.c
index a96a99d2369f800634025bcdfa9838d1d6bc9d97..4e7ab611377acd56c1be78cbd983058e5ce5142b 100644 (file)
@@ -248,6 +248,64 @@ bool dso__needs_decompress(struct dso *dso)
                dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
 }
 
+static int decompress_kmodule(struct dso *dso, const char *name, char *tmpbuf)
+{
+       int fd = -1;
+       struct kmod_path m;
+
+       if (!dso__needs_decompress(dso))
+               return -1;
+
+       if (kmod_path__parse_ext(&m, dso->long_name))
+               return -1;
+
+       if (!m.comp)
+               goto out;
+
+       fd = mkstemp(tmpbuf);
+       if (fd < 0) {
+               dso->load_errno = errno;
+               goto out;
+       }
+
+       if (!decompress_to_file(m.ext, name, fd)) {
+               dso->load_errno = DSO_LOAD_ERRNO__DECOMPRESSION_FAILURE;
+               close(fd);
+               fd = -1;
+       }
+
+out:
+       free(m.ext);
+       return fd;
+}
+
+int dso__decompress_kmodule_fd(struct dso *dso, const char *name)
+{
+       char tmpbuf[] = KMOD_DECOMP_NAME;
+       int fd;
+
+       fd = decompress_kmodule(dso, name, tmpbuf);
+       unlink(tmpbuf);
+       return fd;
+}
+
+int dso__decompress_kmodule_path(struct dso *dso, const char *name,
+                                char *pathname, size_t len)
+{
+       char tmpbuf[] = KMOD_DECOMP_NAME;
+       int fd;
+
+       fd = decompress_kmodule(dso, name, tmpbuf);
+       if (fd < 0) {
+               unlink(tmpbuf);
+               return -1;
+       }
+
+       strncpy(pathname, tmpbuf, len);
+       close(fd);
+       return 0;
+}
+
 /*
  * Parses kernel module specified in @path and updates
  * @m argument like:
@@ -335,6 +393,21 @@ int __kmod_path__parse(struct kmod_path *m, const char *path,
        return 0;
 }
 
+void dso__set_module_info(struct dso *dso, struct kmod_path *m,
+                         struct machine *machine)
+{
+       if (machine__is_host(machine))
+               dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE;
+       else
+               dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE;
+
+       /* _KMODULE_COMP should be next to _KMODULE */
+       if (m->kmod && m->comp)
+               dso->symtab_type++;
+
+       dso__set_short_name(dso, strdup(m->name), true);
+}
+
 /*
  * Global list of open DSOs and the counter.
  */
@@ -381,7 +454,7 @@ static int do_open(char *name)
 
 static int __open_dso(struct dso *dso, struct machine *machine)
 {
-       int fd;
+       int fd = -EINVAL;
        char *root_dir = (char *)"";
        char *name = malloc(PATH_MAX);
 
@@ -392,15 +465,30 @@ static int __open_dso(struct dso *dso, struct machine *machine)
                root_dir = machine->root_dir;
 
        if (dso__read_binary_type_filename(dso, dso->binary_type,
-                                           root_dir, name, PATH_MAX)) {
-               free(name);
-               return -EINVAL;
-       }
+                                           root_dir, name, PATH_MAX))
+               goto out;
 
        if (!is_regular_file(name))
-               return -EINVAL;
+               goto out;
+
+       if (dso__needs_decompress(dso)) {
+               char newpath[KMOD_DECOMP_LEN];
+               size_t len = sizeof(newpath);
+
+               if (dso__decompress_kmodule_path(dso, name, newpath, len) < 0) {
+                       fd = -dso->load_errno;
+                       goto out;
+               }
+
+               strcpy(name, newpath);
+       }
 
        fd = do_open(name);
+
+       if (dso__needs_decompress(dso))
+               unlink(name);
+
+out:
        free(name);
        return fd;
 }