Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <asm/bug.h>
0003 #include <linux/kernel.h>
0004 #include <linux/string.h>
0005 #include <linux/zalloc.h>
0006 #include <sys/time.h>
0007 #include <sys/resource.h>
0008 #include <sys/types.h>
0009 #include <sys/stat.h>
0010 #include <unistd.h>
0011 #include <errno.h>
0012 #include <fcntl.h>
0013 #include <stdlib.h>
0014 #ifdef HAVE_LIBBPF_SUPPORT
0015 #include <bpf/libbpf.h>
0016 #include "bpf-event.h"
0017 #include "bpf-utils.h"
0018 #endif
0019 #include "compress.h"
0020 #include "env.h"
0021 #include "namespaces.h"
0022 #include "path.h"
0023 #include "map.h"
0024 #include "symbol.h"
0025 #include "srcline.h"
0026 #include "dso.h"
0027 #include "dsos.h"
0028 #include "machine.h"
0029 #include "auxtrace.h"
0030 #include "util.h" /* O_CLOEXEC for older systems */
0031 #include "debug.h"
0032 #include "string2.h"
0033 #include "vdso.h"
0034 
0035 static const char * const debuglink_paths[] = {
0036     "%.0s%s",
0037     "%s/%s",
0038     "%s/.debug/%s",
0039     "/usr/lib/debug%s/%s"
0040 };
0041 
0042 char dso__symtab_origin(const struct dso *dso)
0043 {
0044     static const char origin[] = {
0045         [DSO_BINARY_TYPE__KALLSYMS]         = 'k',
0046         [DSO_BINARY_TYPE__VMLINUX]          = 'v',
0047         [DSO_BINARY_TYPE__JAVA_JIT]         = 'j',
0048         [DSO_BINARY_TYPE__DEBUGLINK]            = 'l',
0049         [DSO_BINARY_TYPE__BUILD_ID_CACHE]       = 'B',
0050         [DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO] = 'D',
0051         [DSO_BINARY_TYPE__FEDORA_DEBUGINFO]     = 'f',
0052         [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO]     = 'u',
0053         [DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO] = 'x',
0054         [DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO]   = 'o',
0055         [DSO_BINARY_TYPE__BUILDID_DEBUGINFO]        = 'b',
0056         [DSO_BINARY_TYPE__SYSTEM_PATH_DSO]      = 'd',
0057         [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE]      = 'K',
0058         [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP] = 'm',
0059         [DSO_BINARY_TYPE__GUEST_KALLSYMS]       = 'g',
0060         [DSO_BINARY_TYPE__GUEST_KMODULE]        = 'G',
0061         [DSO_BINARY_TYPE__GUEST_KMODULE_COMP]       = 'M',
0062         [DSO_BINARY_TYPE__GUEST_VMLINUX]        = 'V',
0063     };
0064 
0065     if (dso == NULL || dso->symtab_type == DSO_BINARY_TYPE__NOT_FOUND)
0066         return '!';
0067     return origin[dso->symtab_type];
0068 }
0069 
0070 int dso__read_binary_type_filename(const struct dso *dso,
0071                    enum dso_binary_type type,
0072                    char *root_dir, char *filename, size_t size)
0073 {
0074     char build_id_hex[SBUILD_ID_SIZE];
0075     int ret = 0;
0076     size_t len;
0077 
0078     switch (type) {
0079     case DSO_BINARY_TYPE__DEBUGLINK:
0080     {
0081         const char *last_slash;
0082         char dso_dir[PATH_MAX];
0083         char symfile[PATH_MAX];
0084         unsigned int i;
0085 
0086         len = __symbol__join_symfs(filename, size, dso->long_name);
0087         last_slash = filename + len;
0088         while (last_slash != filename && *last_slash != '/')
0089             last_slash--;
0090 
0091         strncpy(dso_dir, filename, last_slash - filename);
0092         dso_dir[last_slash-filename] = '\0';
0093 
0094         if (!is_regular_file(filename)) {
0095             ret = -1;
0096             break;
0097         }
0098 
0099         ret = filename__read_debuglink(filename, symfile, PATH_MAX);
0100         if (ret)
0101             break;
0102 
0103         /* Check predefined locations where debug file might reside */
0104         ret = -1;
0105         for (i = 0; i < ARRAY_SIZE(debuglink_paths); i++) {
0106             snprintf(filename, size,
0107                     debuglink_paths[i], dso_dir, symfile);
0108             if (is_regular_file(filename)) {
0109                 ret = 0;
0110                 break;
0111             }
0112         }
0113 
0114         break;
0115     }
0116     case DSO_BINARY_TYPE__BUILD_ID_CACHE:
0117         if (dso__build_id_filename(dso, filename, size, false) == NULL)
0118             ret = -1;
0119         break;
0120 
0121     case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO:
0122         if (dso__build_id_filename(dso, filename, size, true) == NULL)
0123             ret = -1;
0124         break;
0125 
0126     case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
0127         len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
0128         snprintf(filename + len, size - len, "%s.debug", dso->long_name);
0129         break;
0130 
0131     case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
0132         len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
0133         snprintf(filename + len, size - len, "%s", dso->long_name);
0134         break;
0135 
0136     case DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO:
0137         /*
0138          * Ubuntu can mixup /usr/lib with /lib, putting debuginfo in
0139          * /usr/lib/debug/lib when it is expected to be in
0140          * /usr/lib/debug/usr/lib
0141          */
0142         if (strlen(dso->long_name) < 9 ||
0143             strncmp(dso->long_name, "/usr/lib/", 9)) {
0144             ret = -1;
0145             break;
0146         }
0147         len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
0148         snprintf(filename + len, size - len, "%s", dso->long_name + 4);
0149         break;
0150 
0151     case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
0152     {
0153         const char *last_slash;
0154         size_t dir_size;
0155 
0156         last_slash = dso->long_name + dso->long_name_len;
0157         while (last_slash != dso->long_name && *last_slash != '/')
0158             last_slash--;
0159 
0160         len = __symbol__join_symfs(filename, size, "");
0161         dir_size = last_slash - dso->long_name + 2;
0162         if (dir_size > (size - len)) {
0163             ret = -1;
0164             break;
0165         }
0166         len += scnprintf(filename + len, dir_size, "%s",  dso->long_name);
0167         len += scnprintf(filename + len , size - len, ".debug%s",
0168                                 last_slash);
0169         break;
0170     }
0171 
0172     case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
0173         if (!dso->has_build_id) {
0174             ret = -1;
0175             break;
0176         }
0177 
0178         build_id__sprintf(&dso->bid, build_id_hex);
0179         len = __symbol__join_symfs(filename, size, "/usr/lib/debug/.build-id/");
0180         snprintf(filename + len, size - len, "%.2s/%s.debug",
0181              build_id_hex, build_id_hex + 2);
0182         break;
0183 
0184     case DSO_BINARY_TYPE__VMLINUX:
0185     case DSO_BINARY_TYPE__GUEST_VMLINUX:
0186     case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
0187         __symbol__join_symfs(filename, size, dso->long_name);
0188         break;
0189 
0190     case DSO_BINARY_TYPE__GUEST_KMODULE:
0191     case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
0192         path__join3(filename, size, symbol_conf.symfs,
0193                 root_dir, dso->long_name);
0194         break;
0195 
0196     case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
0197     case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
0198         __symbol__join_symfs(filename, size, dso->long_name);
0199         break;
0200 
0201     case DSO_BINARY_TYPE__KCORE:
0202     case DSO_BINARY_TYPE__GUEST_KCORE:
0203         snprintf(filename, size, "%s", dso->long_name);
0204         break;
0205 
0206     default:
0207     case DSO_BINARY_TYPE__KALLSYMS:
0208     case DSO_BINARY_TYPE__GUEST_KALLSYMS:
0209     case DSO_BINARY_TYPE__JAVA_JIT:
0210     case DSO_BINARY_TYPE__BPF_PROG_INFO:
0211     case DSO_BINARY_TYPE__BPF_IMAGE:
0212     case DSO_BINARY_TYPE__OOL:
0213     case DSO_BINARY_TYPE__NOT_FOUND:
0214         ret = -1;
0215         break;
0216     }
0217 
0218     return ret;
0219 }
0220 
0221 enum {
0222     COMP_ID__NONE = 0,
0223 };
0224 
0225 static const struct {
0226     const char *fmt;
0227     int (*decompress)(const char *input, int output);
0228     bool (*is_compressed)(const char *input);
0229 } compressions[] = {
0230     [COMP_ID__NONE] = { .fmt = NULL, },
0231 #ifdef HAVE_ZLIB_SUPPORT
0232     { "gz", gzip_decompress_to_file, gzip_is_compressed },
0233 #endif
0234 #ifdef HAVE_LZMA_SUPPORT
0235     { "xz", lzma_decompress_to_file, lzma_is_compressed },
0236 #endif
0237     { NULL, NULL, NULL },
0238 };
0239 
0240 static int is_supported_compression(const char *ext)
0241 {
0242     unsigned i;
0243 
0244     for (i = 1; compressions[i].fmt; i++) {
0245         if (!strcmp(ext, compressions[i].fmt))
0246             return i;
0247     }
0248     return COMP_ID__NONE;
0249 }
0250 
0251 bool is_kernel_module(const char *pathname, int cpumode)
0252 {
0253     struct kmod_path m;
0254     int mode = cpumode & PERF_RECORD_MISC_CPUMODE_MASK;
0255 
0256     WARN_ONCE(mode != cpumode,
0257           "Internal error: passing unmasked cpumode (%x) to is_kernel_module",
0258           cpumode);
0259 
0260     switch (mode) {
0261     case PERF_RECORD_MISC_USER:
0262     case PERF_RECORD_MISC_HYPERVISOR:
0263     case PERF_RECORD_MISC_GUEST_USER:
0264         return false;
0265     /* Treat PERF_RECORD_MISC_CPUMODE_UNKNOWN as kernel */
0266     default:
0267         if (kmod_path__parse(&m, pathname)) {
0268             pr_err("Failed to check whether %s is a kernel module or not. Assume it is.",
0269                     pathname);
0270             return true;
0271         }
0272     }
0273 
0274     return m.kmod;
0275 }
0276 
0277 bool dso__needs_decompress(struct dso *dso)
0278 {
0279     return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
0280         dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
0281 }
0282 
0283 int filename__decompress(const char *name, char *pathname,
0284              size_t len, int comp, int *err)
0285 {
0286     char tmpbuf[] = KMOD_DECOMP_NAME;
0287     int fd = -1;
0288 
0289     /*
0290      * We have proper compression id for DSO and yet the file
0291      * behind the 'name' can still be plain uncompressed object.
0292      *
0293      * The reason is behind the logic we open the DSO object files,
0294      * when we try all possible 'debug' objects until we find the
0295      * data. So even if the DSO is represented by 'krava.xz' module,
0296      * we can end up here opening ~/.debug/....23432432/debug' file
0297      * which is not compressed.
0298      *
0299      * To keep this transparent, we detect this and return the file
0300      * descriptor to the uncompressed file.
0301      */
0302     if (!compressions[comp].is_compressed(name))
0303         return open(name, O_RDONLY);
0304 
0305     fd = mkstemp(tmpbuf);
0306     if (fd < 0) {
0307         *err = errno;
0308         return -1;
0309     }
0310 
0311     if (compressions[comp].decompress(name, fd)) {
0312         *err = DSO_LOAD_ERRNO__DECOMPRESSION_FAILURE;
0313         close(fd);
0314         fd = -1;
0315     }
0316 
0317     if (!pathname || (fd < 0))
0318         unlink(tmpbuf);
0319 
0320     if (pathname && (fd >= 0))
0321         strlcpy(pathname, tmpbuf, len);
0322 
0323     return fd;
0324 }
0325 
0326 static int decompress_kmodule(struct dso *dso, const char *name,
0327                   char *pathname, size_t len)
0328 {
0329     if (!dso__needs_decompress(dso))
0330         return -1;
0331 
0332     if (dso->comp == COMP_ID__NONE)
0333         return -1;
0334 
0335     return filename__decompress(name, pathname, len, dso->comp,
0336                     &dso->load_errno);
0337 }
0338 
0339 int dso__decompress_kmodule_fd(struct dso *dso, const char *name)
0340 {
0341     return decompress_kmodule(dso, name, NULL, 0);
0342 }
0343 
0344 int dso__decompress_kmodule_path(struct dso *dso, const char *name,
0345                  char *pathname, size_t len)
0346 {
0347     int fd = decompress_kmodule(dso, name, pathname, len);
0348 
0349     close(fd);
0350     return fd >= 0 ? 0 : -1;
0351 }
0352 
0353 /*
0354  * Parses kernel module specified in @path and updates
0355  * @m argument like:
0356  *
0357  *    @comp - true if @path contains supported compression suffix,
0358  *            false otherwise
0359  *    @kmod - true if @path contains '.ko' suffix in right position,
0360  *            false otherwise
0361  *    @name - if (@alloc_name && @kmod) is true, it contains strdup-ed base name
0362  *            of the kernel module without suffixes, otherwise strudup-ed
0363  *            base name of @path
0364  *    @ext  - if (@alloc_ext && @comp) is true, it contains strdup-ed string
0365  *            the compression suffix
0366  *
0367  * Returns 0 if there's no strdup error, -ENOMEM otherwise.
0368  */
0369 int __kmod_path__parse(struct kmod_path *m, const char *path,
0370                bool alloc_name)
0371 {
0372     const char *name = strrchr(path, '/');
0373     const char *ext  = strrchr(path, '.');
0374     bool is_simple_name = false;
0375 
0376     memset(m, 0x0, sizeof(*m));
0377     name = name ? name + 1 : path;
0378 
0379     /*
0380      * '.' is also a valid character for module name. For example:
0381      * [aaa.bbb] is a valid module name. '[' should have higher
0382      * priority than '.ko' suffix.
0383      *
0384      * The kernel names are from machine__mmap_name. Such
0385      * name should belong to kernel itself, not kernel module.
0386      */
0387     if (name[0] == '[') {
0388         is_simple_name = true;
0389         if ((strncmp(name, "[kernel.kallsyms]", 17) == 0) ||
0390             (strncmp(name, "[guest.kernel.kallsyms", 22) == 0) ||
0391             (strncmp(name, "[vdso]", 6) == 0) ||
0392             (strncmp(name, "[vdso32]", 8) == 0) ||
0393             (strncmp(name, "[vdsox32]", 9) == 0) ||
0394             (strncmp(name, "[vsyscall]", 10) == 0)) {
0395             m->kmod = false;
0396 
0397         } else
0398             m->kmod = true;
0399     }
0400 
0401     /* No extension, just return name. */
0402     if ((ext == NULL) || is_simple_name) {
0403         if (alloc_name) {
0404             m->name = strdup(name);
0405             return m->name ? 0 : -ENOMEM;
0406         }
0407         return 0;
0408     }
0409 
0410     m->comp = is_supported_compression(ext + 1);
0411     if (m->comp > COMP_ID__NONE)
0412         ext -= 3;
0413 
0414     /* Check .ko extension only if there's enough name left. */
0415     if (ext > name)
0416         m->kmod = !strncmp(ext, ".ko", 3);
0417 
0418     if (alloc_name) {
0419         if (m->kmod) {
0420             if (asprintf(&m->name, "[%.*s]", (int) (ext - name), name) == -1)
0421                 return -ENOMEM;
0422         } else {
0423             if (asprintf(&m->name, "%s", name) == -1)
0424                 return -ENOMEM;
0425         }
0426 
0427         strreplace(m->name, '-', '_');
0428     }
0429 
0430     return 0;
0431 }
0432 
0433 void dso__set_module_info(struct dso *dso, struct kmod_path *m,
0434               struct machine *machine)
0435 {
0436     if (machine__is_host(machine))
0437         dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE;
0438     else
0439         dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE;
0440 
0441     /* _KMODULE_COMP should be next to _KMODULE */
0442     if (m->kmod && m->comp) {
0443         dso->symtab_type++;
0444         dso->comp = m->comp;
0445     }
0446 
0447     dso__set_short_name(dso, strdup(m->name), true);
0448 }
0449 
0450 /*
0451  * Global list of open DSOs and the counter.
0452  */
0453 static LIST_HEAD(dso__data_open);
0454 static long dso__data_open_cnt;
0455 static pthread_mutex_t dso__data_open_lock = PTHREAD_MUTEX_INITIALIZER;
0456 
0457 static void dso__list_add(struct dso *dso)
0458 {
0459     list_add_tail(&dso->data.open_entry, &dso__data_open);
0460     dso__data_open_cnt++;
0461 }
0462 
0463 static void dso__list_del(struct dso *dso)
0464 {
0465     list_del_init(&dso->data.open_entry);
0466     WARN_ONCE(dso__data_open_cnt <= 0,
0467           "DSO data fd counter out of bounds.");
0468     dso__data_open_cnt--;
0469 }
0470 
0471 static void close_first_dso(void);
0472 
0473 static int do_open(char *name)
0474 {
0475     int fd;
0476     char sbuf[STRERR_BUFSIZE];
0477 
0478     do {
0479         fd = open(name, O_RDONLY|O_CLOEXEC);
0480         if (fd >= 0)
0481             return fd;
0482 
0483         pr_debug("dso open failed: %s\n",
0484              str_error_r(errno, sbuf, sizeof(sbuf)));
0485         if (!dso__data_open_cnt || errno != EMFILE)
0486             break;
0487 
0488         close_first_dso();
0489     } while (1);
0490 
0491     return -1;
0492 }
0493 
0494 static int __open_dso(struct dso *dso, struct machine *machine)
0495 {
0496     int fd = -EINVAL;
0497     char *root_dir = (char *)"";
0498     char *name = malloc(PATH_MAX);
0499     bool decomp = false;
0500 
0501     if (!name)
0502         return -ENOMEM;
0503 
0504     if (machine)
0505         root_dir = machine->root_dir;
0506 
0507     if (dso__read_binary_type_filename(dso, dso->binary_type,
0508                         root_dir, name, PATH_MAX))
0509         goto out;
0510 
0511     if (!is_regular_file(name)) {
0512         char *new_name;
0513 
0514         if (errno != ENOENT || dso->nsinfo == NULL)
0515             goto out;
0516 
0517         new_name = filename_with_chroot(dso->nsinfo->pid, name);
0518         if (!new_name)
0519             goto out;
0520 
0521         free(name);
0522         name = new_name;
0523     }
0524 
0525     if (dso__needs_decompress(dso)) {
0526         char newpath[KMOD_DECOMP_LEN];
0527         size_t len = sizeof(newpath);
0528 
0529         if (dso__decompress_kmodule_path(dso, name, newpath, len) < 0) {
0530             fd = -dso->load_errno;
0531             goto out;
0532         }
0533 
0534         decomp = true;
0535         strcpy(name, newpath);
0536     }
0537 
0538     fd = do_open(name);
0539 
0540     if (decomp)
0541         unlink(name);
0542 
0543 out:
0544     free(name);
0545     return fd;
0546 }
0547 
0548 static void check_data_close(void);
0549 
0550 /**
0551  * dso_close - Open DSO data file
0552  * @dso: dso object
0553  *
0554  * Open @dso's data file descriptor and updates
0555  * list/count of open DSO objects.
0556  */
0557 static int open_dso(struct dso *dso, struct machine *machine)
0558 {
0559     int fd;
0560     struct nscookie nsc;
0561 
0562     if (dso->binary_type != DSO_BINARY_TYPE__BUILD_ID_CACHE)
0563         nsinfo__mountns_enter(dso->nsinfo, &nsc);
0564     fd = __open_dso(dso, machine);
0565     if (dso->binary_type != DSO_BINARY_TYPE__BUILD_ID_CACHE)
0566         nsinfo__mountns_exit(&nsc);
0567 
0568     if (fd >= 0) {
0569         dso__list_add(dso);
0570         /*
0571          * Check if we crossed the allowed number
0572          * of opened DSOs and close one if needed.
0573          */
0574         check_data_close();
0575     }
0576 
0577     return fd;
0578 }
0579 
0580 static void close_data_fd(struct dso *dso)
0581 {
0582     if (dso->data.fd >= 0) {
0583         close(dso->data.fd);
0584         dso->data.fd = -1;
0585         dso->data.file_size = 0;
0586         dso__list_del(dso);
0587     }
0588 }
0589 
0590 /**
0591  * dso_close - Close DSO data file
0592  * @dso: dso object
0593  *
0594  * Close @dso's data file descriptor and updates
0595  * list/count of open DSO objects.
0596  */
0597 static void close_dso(struct dso *dso)
0598 {
0599     close_data_fd(dso);
0600 }
0601 
0602 static void close_first_dso(void)
0603 {
0604     struct dso *dso;
0605 
0606     dso = list_first_entry(&dso__data_open, struct dso, data.open_entry);
0607     close_dso(dso);
0608 }
0609 
0610 static rlim_t get_fd_limit(void)
0611 {
0612     struct rlimit l;
0613     rlim_t limit = 0;
0614 
0615     /* Allow half of the current open fd limit. */
0616     if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
0617         if (l.rlim_cur == RLIM_INFINITY)
0618             limit = l.rlim_cur;
0619         else
0620             limit = l.rlim_cur / 2;
0621     } else {
0622         pr_err("failed to get fd limit\n");
0623         limit = 1;
0624     }
0625 
0626     return limit;
0627 }
0628 
0629 static rlim_t fd_limit;
0630 
0631 /*
0632  * Used only by tests/dso-data.c to reset the environment
0633  * for tests. I dont expect we should change this during
0634  * standard runtime.
0635  */
0636 void reset_fd_limit(void)
0637 {
0638     fd_limit = 0;
0639 }
0640 
0641 static bool may_cache_fd(void)
0642 {
0643     if (!fd_limit)
0644         fd_limit = get_fd_limit();
0645 
0646     if (fd_limit == RLIM_INFINITY)
0647         return true;
0648 
0649     return fd_limit > (rlim_t) dso__data_open_cnt;
0650 }
0651 
0652 /*
0653  * Check and close LRU dso if we crossed allowed limit
0654  * for opened dso file descriptors. The limit is half
0655  * of the RLIMIT_NOFILE files opened.
0656 */
0657 static void check_data_close(void)
0658 {
0659     bool cache_fd = may_cache_fd();
0660 
0661     if (!cache_fd)
0662         close_first_dso();
0663 }
0664 
0665 /**
0666  * dso__data_close - Close DSO data file
0667  * @dso: dso object
0668  *
0669  * External interface to close @dso's data file descriptor.
0670  */
0671 void dso__data_close(struct dso *dso)
0672 {
0673     pthread_mutex_lock(&dso__data_open_lock);
0674     close_dso(dso);
0675     pthread_mutex_unlock(&dso__data_open_lock);
0676 }
0677 
0678 static void try_to_open_dso(struct dso *dso, struct machine *machine)
0679 {
0680     enum dso_binary_type binary_type_data[] = {
0681         DSO_BINARY_TYPE__BUILD_ID_CACHE,
0682         DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
0683         DSO_BINARY_TYPE__NOT_FOUND,
0684     };
0685     int i = 0;
0686 
0687     if (dso->data.fd >= 0)
0688         return;
0689 
0690     if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) {
0691         dso->data.fd = open_dso(dso, machine);
0692         goto out;
0693     }
0694 
0695     do {
0696         dso->binary_type = binary_type_data[i++];
0697 
0698         dso->data.fd = open_dso(dso, machine);
0699         if (dso->data.fd >= 0)
0700             goto out;
0701 
0702     } while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND);
0703 out:
0704     if (dso->data.fd >= 0)
0705         dso->data.status = DSO_DATA_STATUS_OK;
0706     else
0707         dso->data.status = DSO_DATA_STATUS_ERROR;
0708 }
0709 
0710 /**
0711  * dso__data_get_fd - Get dso's data file descriptor
0712  * @dso: dso object
0713  * @machine: machine object
0714  *
0715  * External interface to find dso's file, open it and
0716  * returns file descriptor.  It should be paired with
0717  * dso__data_put_fd() if it returns non-negative value.
0718  */
0719 int dso__data_get_fd(struct dso *dso, struct machine *machine)
0720 {
0721     if (dso->data.status == DSO_DATA_STATUS_ERROR)
0722         return -1;
0723 
0724     if (pthread_mutex_lock(&dso__data_open_lock) < 0)
0725         return -1;
0726 
0727     try_to_open_dso(dso, machine);
0728 
0729     if (dso->data.fd < 0)
0730         pthread_mutex_unlock(&dso__data_open_lock);
0731 
0732     return dso->data.fd;
0733 }
0734 
0735 void dso__data_put_fd(struct dso *dso __maybe_unused)
0736 {
0737     pthread_mutex_unlock(&dso__data_open_lock);
0738 }
0739 
0740 bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by)
0741 {
0742     u32 flag = 1 << by;
0743 
0744     if (dso->data.status_seen & flag)
0745         return true;
0746 
0747     dso->data.status_seen |= flag;
0748 
0749     return false;
0750 }
0751 
0752 #ifdef HAVE_LIBBPF_SUPPORT
0753 static ssize_t bpf_read(struct dso *dso, u64 offset, char *data)
0754 {
0755     struct bpf_prog_info_node *node;
0756     ssize_t size = DSO__DATA_CACHE_SIZE;
0757     u64 len;
0758     u8 *buf;
0759 
0760     node = perf_env__find_bpf_prog_info(dso->bpf_prog.env, dso->bpf_prog.id);
0761     if (!node || !node->info_linear) {
0762         dso->data.status = DSO_DATA_STATUS_ERROR;
0763         return -1;
0764     }
0765 
0766     len = node->info_linear->info.jited_prog_len;
0767     buf = (u8 *)(uintptr_t)node->info_linear->info.jited_prog_insns;
0768 
0769     if (offset >= len)
0770         return -1;
0771 
0772     size = (ssize_t)min(len - offset, (u64)size);
0773     memcpy(data, buf + offset, size);
0774     return size;
0775 }
0776 
0777 static int bpf_size(struct dso *dso)
0778 {
0779     struct bpf_prog_info_node *node;
0780 
0781     node = perf_env__find_bpf_prog_info(dso->bpf_prog.env, dso->bpf_prog.id);
0782     if (!node || !node->info_linear) {
0783         dso->data.status = DSO_DATA_STATUS_ERROR;
0784         return -1;
0785     }
0786 
0787     dso->data.file_size = node->info_linear->info.jited_prog_len;
0788     return 0;
0789 }
0790 #endif // HAVE_LIBBPF_SUPPORT
0791 
0792 static void
0793 dso_cache__free(struct dso *dso)
0794 {
0795     struct rb_root *root = &dso->data.cache;
0796     struct rb_node *next = rb_first(root);
0797 
0798     pthread_mutex_lock(&dso->lock);
0799     while (next) {
0800         struct dso_cache *cache;
0801 
0802         cache = rb_entry(next, struct dso_cache, rb_node);
0803         next = rb_next(&cache->rb_node);
0804         rb_erase(&cache->rb_node, root);
0805         free(cache);
0806     }
0807     pthread_mutex_unlock(&dso->lock);
0808 }
0809 
0810 static struct dso_cache *__dso_cache__find(struct dso *dso, u64 offset)
0811 {
0812     const struct rb_root *root = &dso->data.cache;
0813     struct rb_node * const *p = &root->rb_node;
0814     const struct rb_node *parent = NULL;
0815     struct dso_cache *cache;
0816 
0817     while (*p != NULL) {
0818         u64 end;
0819 
0820         parent = *p;
0821         cache = rb_entry(parent, struct dso_cache, rb_node);
0822         end = cache->offset + DSO__DATA_CACHE_SIZE;
0823 
0824         if (offset < cache->offset)
0825             p = &(*p)->rb_left;
0826         else if (offset >= end)
0827             p = &(*p)->rb_right;
0828         else
0829             return cache;
0830     }
0831 
0832     return NULL;
0833 }
0834 
0835 static struct dso_cache *
0836 dso_cache__insert(struct dso *dso, struct dso_cache *new)
0837 {
0838     struct rb_root *root = &dso->data.cache;
0839     struct rb_node **p = &root->rb_node;
0840     struct rb_node *parent = NULL;
0841     struct dso_cache *cache;
0842     u64 offset = new->offset;
0843 
0844     pthread_mutex_lock(&dso->lock);
0845     while (*p != NULL) {
0846         u64 end;
0847 
0848         parent = *p;
0849         cache = rb_entry(parent, struct dso_cache, rb_node);
0850         end = cache->offset + DSO__DATA_CACHE_SIZE;
0851 
0852         if (offset < cache->offset)
0853             p = &(*p)->rb_left;
0854         else if (offset >= end)
0855             p = &(*p)->rb_right;
0856         else
0857             goto out;
0858     }
0859 
0860     rb_link_node(&new->rb_node, parent, p);
0861     rb_insert_color(&new->rb_node, root);
0862 
0863     cache = NULL;
0864 out:
0865     pthread_mutex_unlock(&dso->lock);
0866     return cache;
0867 }
0868 
0869 static ssize_t dso_cache__memcpy(struct dso_cache *cache, u64 offset, u8 *data,
0870                  u64 size, bool out)
0871 {
0872     u64 cache_offset = offset - cache->offset;
0873     u64 cache_size   = min(cache->size - cache_offset, size);
0874 
0875     if (out)
0876         memcpy(data, cache->data + cache_offset, cache_size);
0877     else
0878         memcpy(cache->data + cache_offset, data, cache_size);
0879     return cache_size;
0880 }
0881 
0882 static ssize_t file_read(struct dso *dso, struct machine *machine,
0883              u64 offset, char *data)
0884 {
0885     ssize_t ret;
0886 
0887     pthread_mutex_lock(&dso__data_open_lock);
0888 
0889     /*
0890      * dso->data.fd might be closed if other thread opened another
0891      * file (dso) due to open file limit (RLIMIT_NOFILE).
0892      */
0893     try_to_open_dso(dso, machine);
0894 
0895     if (dso->data.fd < 0) {
0896         dso->data.status = DSO_DATA_STATUS_ERROR;
0897         ret = -errno;
0898         goto out;
0899     }
0900 
0901     ret = pread(dso->data.fd, data, DSO__DATA_CACHE_SIZE, offset);
0902 out:
0903     pthread_mutex_unlock(&dso__data_open_lock);
0904     return ret;
0905 }
0906 
0907 static struct dso_cache *dso_cache__populate(struct dso *dso,
0908                          struct machine *machine,
0909                          u64 offset, ssize_t *ret)
0910 {
0911     u64 cache_offset = offset & DSO__DATA_CACHE_MASK;
0912     struct dso_cache *cache;
0913     struct dso_cache *old;
0914 
0915     cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
0916     if (!cache) {
0917         *ret = -ENOMEM;
0918         return NULL;
0919     }
0920 #ifdef HAVE_LIBBPF_SUPPORT
0921     if (dso->binary_type == DSO_BINARY_TYPE__BPF_PROG_INFO)
0922         *ret = bpf_read(dso, cache_offset, cache->data);
0923     else
0924 #endif
0925     if (dso->binary_type == DSO_BINARY_TYPE__OOL)
0926         *ret = DSO__DATA_CACHE_SIZE;
0927     else
0928         *ret = file_read(dso, machine, cache_offset, cache->data);
0929 
0930     if (*ret <= 0) {
0931         free(cache);
0932         return NULL;
0933     }
0934 
0935     cache->offset = cache_offset;
0936     cache->size   = *ret;
0937 
0938     old = dso_cache__insert(dso, cache);
0939     if (old) {
0940         /* we lose the race */
0941         free(cache);
0942         cache = old;
0943     }
0944 
0945     return cache;
0946 }
0947 
0948 static struct dso_cache *dso_cache__find(struct dso *dso,
0949                      struct machine *machine,
0950                      u64 offset,
0951                      ssize_t *ret)
0952 {
0953     struct dso_cache *cache = __dso_cache__find(dso, offset);
0954 
0955     return cache ? cache : dso_cache__populate(dso, machine, offset, ret);
0956 }
0957 
0958 static ssize_t dso_cache_io(struct dso *dso, struct machine *machine,
0959                 u64 offset, u8 *data, ssize_t size, bool out)
0960 {
0961     struct dso_cache *cache;
0962     ssize_t ret = 0;
0963 
0964     cache = dso_cache__find(dso, machine, offset, &ret);
0965     if (!cache)
0966         return ret;
0967 
0968     return dso_cache__memcpy(cache, offset, data, size, out);
0969 }
0970 
0971 /*
0972  * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
0973  * in the rb_tree. Any read to already cached data is served
0974  * by cached data. Writes update the cache only, not the backing file.
0975  */
0976 static ssize_t cached_io(struct dso *dso, struct machine *machine,
0977              u64 offset, u8 *data, ssize_t size, bool out)
0978 {
0979     ssize_t r = 0;
0980     u8 *p = data;
0981 
0982     do {
0983         ssize_t ret;
0984 
0985         ret = dso_cache_io(dso, machine, offset, p, size, out);
0986         if (ret < 0)
0987             return ret;
0988 
0989         /* Reached EOF, return what we have. */
0990         if (!ret)
0991             break;
0992 
0993         BUG_ON(ret > size);
0994 
0995         r      += ret;
0996         p      += ret;
0997         offset += ret;
0998         size   -= ret;
0999 
1000     } while (size);
1001 
1002     return r;
1003 }
1004 
1005 static int file_size(struct dso *dso, struct machine *machine)
1006 {
1007     int ret = 0;
1008     struct stat st;
1009     char sbuf[STRERR_BUFSIZE];
1010 
1011     pthread_mutex_lock(&dso__data_open_lock);
1012 
1013     /*
1014      * dso->data.fd might be closed if other thread opened another
1015      * file (dso) due to open file limit (RLIMIT_NOFILE).
1016      */
1017     try_to_open_dso(dso, machine);
1018 
1019     if (dso->data.fd < 0) {
1020         ret = -errno;
1021         dso->data.status = DSO_DATA_STATUS_ERROR;
1022         goto out;
1023     }
1024 
1025     if (fstat(dso->data.fd, &st) < 0) {
1026         ret = -errno;
1027         pr_err("dso cache fstat failed: %s\n",
1028                str_error_r(errno, sbuf, sizeof(sbuf)));
1029         dso->data.status = DSO_DATA_STATUS_ERROR;
1030         goto out;
1031     }
1032     dso->data.file_size = st.st_size;
1033 
1034 out:
1035     pthread_mutex_unlock(&dso__data_open_lock);
1036     return ret;
1037 }
1038 
1039 int dso__data_file_size(struct dso *dso, struct machine *machine)
1040 {
1041     if (dso->data.file_size)
1042         return 0;
1043 
1044     if (dso->data.status == DSO_DATA_STATUS_ERROR)
1045         return -1;
1046 #ifdef HAVE_LIBBPF_SUPPORT
1047     if (dso->binary_type == DSO_BINARY_TYPE__BPF_PROG_INFO)
1048         return bpf_size(dso);
1049 #endif
1050     return file_size(dso, machine);
1051 }
1052 
1053 /**
1054  * dso__data_size - Return dso data size
1055  * @dso: dso object
1056  * @machine: machine object
1057  *
1058  * Return: dso data size
1059  */
1060 off_t dso__data_size(struct dso *dso, struct machine *machine)
1061 {
1062     if (dso__data_file_size(dso, machine))
1063         return -1;
1064 
1065     /* For now just estimate dso data size is close to file size */
1066     return dso->data.file_size;
1067 }
1068 
1069 static ssize_t data_read_write_offset(struct dso *dso, struct machine *machine,
1070                       u64 offset, u8 *data, ssize_t size,
1071                       bool out)
1072 {
1073     if (dso__data_file_size(dso, machine))
1074         return -1;
1075 
1076     /* Check the offset sanity. */
1077     if (offset > dso->data.file_size)
1078         return -1;
1079 
1080     if (offset + size < offset)
1081         return -1;
1082 
1083     return cached_io(dso, machine, offset, data, size, out);
1084 }
1085 
1086 /**
1087  * dso__data_read_offset - Read data from dso file offset
1088  * @dso: dso object
1089  * @machine: machine object
1090  * @offset: file offset
1091  * @data: buffer to store data
1092  * @size: size of the @data buffer
1093  *
1094  * External interface to read data from dso file offset. Open
1095  * dso data file and use cached_read to get the data.
1096  */
1097 ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
1098                   u64 offset, u8 *data, ssize_t size)
1099 {
1100     if (dso->data.status == DSO_DATA_STATUS_ERROR)
1101         return -1;
1102 
1103     return data_read_write_offset(dso, machine, offset, data, size, true);
1104 }
1105 
1106 /**
1107  * dso__data_read_addr - Read data from dso address
1108  * @dso: dso object
1109  * @machine: machine object
1110  * @add: virtual memory address
1111  * @data: buffer to store data
1112  * @size: size of the @data buffer
1113  *
1114  * External interface to read data from dso address.
1115  */
1116 ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
1117                 struct machine *machine, u64 addr,
1118                 u8 *data, ssize_t size)
1119 {
1120     u64 offset = map->map_ip(map, addr);
1121     return dso__data_read_offset(dso, machine, offset, data, size);
1122 }
1123 
1124 /**
1125  * dso__data_write_cache_offs - Write data to dso data cache at file offset
1126  * @dso: dso object
1127  * @machine: machine object
1128  * @offset: file offset
1129  * @data: buffer to write
1130  * @size: size of the @data buffer
1131  *
1132  * Write into the dso file data cache, but do not change the file itself.
1133  */
1134 ssize_t dso__data_write_cache_offs(struct dso *dso, struct machine *machine,
1135                    u64 offset, const u8 *data_in, ssize_t size)
1136 {
1137     u8 *data = (u8 *)data_in; /* cast away const to use same fns for r/w */
1138 
1139     if (dso->data.status == DSO_DATA_STATUS_ERROR)
1140         return -1;
1141 
1142     return data_read_write_offset(dso, machine, offset, data, size, false);
1143 }
1144 
1145 /**
1146  * dso__data_write_cache_addr - Write data to dso data cache at dso address
1147  * @dso: dso object
1148  * @machine: machine object
1149  * @add: virtual memory address
1150  * @data: buffer to write
1151  * @size: size of the @data buffer
1152  *
1153  * External interface to write into the dso file data cache, but do not change
1154  * the file itself.
1155  */
1156 ssize_t dso__data_write_cache_addr(struct dso *dso, struct map *map,
1157                    struct machine *machine, u64 addr,
1158                    const u8 *data, ssize_t size)
1159 {
1160     u64 offset = map->map_ip(map, addr);
1161     return dso__data_write_cache_offs(dso, machine, offset, data, size);
1162 }
1163 
1164 struct map *dso__new_map(const char *name)
1165 {
1166     struct map *map = NULL;
1167     struct dso *dso = dso__new(name);
1168 
1169     if (dso) {
1170         map = map__new2(0, dso);
1171         dso__put(dso);
1172     }
1173 
1174     return map;
1175 }
1176 
1177 struct dso *machine__findnew_kernel(struct machine *machine, const char *name,
1178                     const char *short_name, int dso_type)
1179 {
1180     /*
1181      * The kernel dso could be created by build_id processing.
1182      */
1183     struct dso *dso = machine__findnew_dso(machine, name);
1184 
1185     /*
1186      * We need to run this in all cases, since during the build_id
1187      * processing we had no idea this was the kernel dso.
1188      */
1189     if (dso != NULL) {
1190         dso__set_short_name(dso, short_name, false);
1191         dso->kernel = dso_type;
1192     }
1193 
1194     return dso;
1195 }
1196 
1197 static void dso__set_long_name_id(struct dso *dso, const char *name, struct dso_id *id, bool name_allocated)
1198 {
1199     struct rb_root *root = dso->root;
1200 
1201     if (name == NULL)
1202         return;
1203 
1204     if (dso->long_name_allocated)
1205         free((char *)dso->long_name);
1206 
1207     if (root) {
1208         rb_erase(&dso->rb_node, root);
1209         /*
1210          * __dsos__findnew_link_by_longname_id() isn't guaranteed to
1211          * add it back, so a clean removal is required here.
1212          */
1213         RB_CLEAR_NODE(&dso->rb_node);
1214         dso->root = NULL;
1215     }
1216 
1217     dso->long_name       = name;
1218     dso->long_name_len   = strlen(name);
1219     dso->long_name_allocated = name_allocated;
1220 
1221     if (root)
1222         __dsos__findnew_link_by_longname_id(root, dso, NULL, id);
1223 }
1224 
1225 void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
1226 {
1227     dso__set_long_name_id(dso, name, NULL, name_allocated);
1228 }
1229 
1230 void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
1231 {
1232     if (name == NULL)
1233         return;
1234 
1235     if (dso->short_name_allocated)
1236         free((char *)dso->short_name);
1237 
1238     dso->short_name       = name;
1239     dso->short_name_len   = strlen(name);
1240     dso->short_name_allocated = name_allocated;
1241 }
1242 
1243 int dso__name_len(const struct dso *dso)
1244 {
1245     if (!dso)
1246         return strlen("[unknown]");
1247     if (verbose > 0)
1248         return dso->long_name_len;
1249 
1250     return dso->short_name_len;
1251 }
1252 
1253 bool dso__loaded(const struct dso *dso)
1254 {
1255     return dso->loaded;
1256 }
1257 
1258 bool dso__sorted_by_name(const struct dso *dso)
1259 {
1260     return dso->sorted_by_name;
1261 }
1262 
1263 void dso__set_sorted_by_name(struct dso *dso)
1264 {
1265     dso->sorted_by_name = true;
1266 }
1267 
1268 struct dso *dso__new_id(const char *name, struct dso_id *id)
1269 {
1270     struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
1271 
1272     if (dso != NULL) {
1273         strcpy(dso->name, name);
1274         if (id)
1275             dso->id = *id;
1276         dso__set_long_name_id(dso, dso->name, id, false);
1277         dso__set_short_name(dso, dso->name, false);
1278         dso->symbols = dso->symbol_names = RB_ROOT_CACHED;
1279         dso->data.cache = RB_ROOT;
1280         dso->inlined_nodes = RB_ROOT_CACHED;
1281         dso->srclines = RB_ROOT_CACHED;
1282         dso->data.fd = -1;
1283         dso->data.status = DSO_DATA_STATUS_UNKNOWN;
1284         dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
1285         dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
1286         dso->is_64_bit = (sizeof(void *) == 8);
1287         dso->loaded = 0;
1288         dso->rel = 0;
1289         dso->sorted_by_name = 0;
1290         dso->has_build_id = 0;
1291         dso->has_srcline = 1;
1292         dso->a2l_fails = 1;
1293         dso->kernel = DSO_SPACE__USER;
1294         dso->needs_swap = DSO_SWAP__UNSET;
1295         dso->comp = COMP_ID__NONE;
1296         RB_CLEAR_NODE(&dso->rb_node);
1297         dso->root = NULL;
1298         INIT_LIST_HEAD(&dso->node);
1299         INIT_LIST_HEAD(&dso->data.open_entry);
1300         pthread_mutex_init(&dso->lock, NULL);
1301         refcount_set(&dso->refcnt, 1);
1302     }
1303 
1304     return dso;
1305 }
1306 
1307 struct dso *dso__new(const char *name)
1308 {
1309     return dso__new_id(name, NULL);
1310 }
1311 
1312 void dso__delete(struct dso *dso)
1313 {
1314     if (!RB_EMPTY_NODE(&dso->rb_node))
1315         pr_err("DSO %s is still in rbtree when being deleted!\n",
1316                dso->long_name);
1317 
1318     /* free inlines first, as they reference symbols */
1319     inlines__tree_delete(&dso->inlined_nodes);
1320     srcline__tree_delete(&dso->srclines);
1321     symbols__delete(&dso->symbols);
1322 
1323     if (dso->short_name_allocated) {
1324         zfree((char **)&dso->short_name);
1325         dso->short_name_allocated = false;
1326     }
1327 
1328     if (dso->long_name_allocated) {
1329         zfree((char **)&dso->long_name);
1330         dso->long_name_allocated = false;
1331     }
1332 
1333     dso__data_close(dso);
1334     auxtrace_cache__free(dso->auxtrace_cache);
1335     dso_cache__free(dso);
1336     dso__free_a2l(dso);
1337     zfree(&dso->symsrc_filename);
1338     nsinfo__zput(dso->nsinfo);
1339     pthread_mutex_destroy(&dso->lock);
1340     free(dso);
1341 }
1342 
1343 struct dso *dso__get(struct dso *dso)
1344 {
1345     if (dso)
1346         refcount_inc(&dso->refcnt);
1347     return dso;
1348 }
1349 
1350 void dso__put(struct dso *dso)
1351 {
1352     if (dso && refcount_dec_and_test(&dso->refcnt))
1353         dso__delete(dso);
1354 }
1355 
1356 void dso__set_build_id(struct dso *dso, struct build_id *bid)
1357 {
1358     dso->bid = *bid;
1359     dso->has_build_id = 1;
1360 }
1361 
1362 bool dso__build_id_equal(const struct dso *dso, struct build_id *bid)
1363 {
1364     if (dso->bid.size > bid->size && dso->bid.size == BUILD_ID_SIZE) {
1365         /*
1366          * For the backward compatibility, it allows a build-id has
1367          * trailing zeros.
1368          */
1369         return !memcmp(dso->bid.data, bid->data, bid->size) &&
1370             !memchr_inv(&dso->bid.data[bid->size], 0,
1371                     dso->bid.size - bid->size);
1372     }
1373 
1374     return dso->bid.size == bid->size &&
1375            memcmp(dso->bid.data, bid->data, dso->bid.size) == 0;
1376 }
1377 
1378 void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
1379 {
1380     char path[PATH_MAX];
1381 
1382     if (machine__is_default_guest(machine))
1383         return;
1384     sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
1385     if (sysfs__read_build_id(path, &dso->bid) == 0)
1386         dso->has_build_id = true;
1387 }
1388 
1389 int dso__kernel_module_get_build_id(struct dso *dso,
1390                     const char *root_dir)
1391 {
1392     char filename[PATH_MAX];
1393     /*
1394      * kernel module short names are of the form "[module]" and
1395      * we need just "module" here.
1396      */
1397     const char *name = dso->short_name + 1;
1398 
1399     snprintf(filename, sizeof(filename),
1400          "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1401          root_dir, (int)strlen(name) - 1, name);
1402 
1403     if (sysfs__read_build_id(filename, &dso->bid) == 0)
1404         dso->has_build_id = true;
1405 
1406     return 0;
1407 }
1408 
1409 static size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
1410 {
1411     char sbuild_id[SBUILD_ID_SIZE];
1412 
1413     build_id__sprintf(&dso->bid, sbuild_id);
1414     return fprintf(fp, "%s", sbuild_id);
1415 }
1416 
1417 size_t dso__fprintf(struct dso *dso, FILE *fp)
1418 {
1419     struct rb_node *nd;
1420     size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
1421 
1422     if (dso->short_name != dso->long_name)
1423         ret += fprintf(fp, "%s, ", dso->long_name);
1424     ret += fprintf(fp, "%sloaded, ", dso__loaded(dso) ? "" : "NOT ");
1425     ret += dso__fprintf_buildid(dso, fp);
1426     ret += fprintf(fp, ")\n");
1427     for (nd = rb_first_cached(&dso->symbols); nd; nd = rb_next(nd)) {
1428         struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
1429         ret += symbol__fprintf(pos, fp);
1430     }
1431 
1432     return ret;
1433 }
1434 
1435 enum dso_type dso__type(struct dso *dso, struct machine *machine)
1436 {
1437     int fd;
1438     enum dso_type type = DSO__TYPE_UNKNOWN;
1439 
1440     fd = dso__data_get_fd(dso, machine);
1441     if (fd >= 0) {
1442         type = dso__type_fd(fd);
1443         dso__data_put_fd(dso);
1444     }
1445 
1446     return type;
1447 }
1448 
1449 int dso__strerror_load(struct dso *dso, char *buf, size_t buflen)
1450 {
1451     int idx, errnum = dso->load_errno;
1452     /*
1453      * This must have a same ordering as the enum dso_load_errno.
1454      */
1455     static const char *dso_load__error_str[] = {
1456     "Internal tools/perf/ library error",
1457     "Invalid ELF file",
1458     "Can not read build id",
1459     "Mismatching build id",
1460     "Decompression failure",
1461     };
1462 
1463     BUG_ON(buflen == 0);
1464 
1465     if (errnum >= 0) {
1466         const char *err = str_error_r(errnum, buf, buflen);
1467 
1468         if (err != buf)
1469             scnprintf(buf, buflen, "%s", err);
1470 
1471         return 0;
1472     }
1473 
1474     if (errnum <  __DSO_LOAD_ERRNO__START || errnum >= __DSO_LOAD_ERRNO__END)
1475         return -1;
1476 
1477     idx = errnum - __DSO_LOAD_ERRNO__START;
1478     scnprintf(buf, buflen, "%s", dso_load__error_str[idx]);
1479     return 0;
1480 }