GNU Linux-libre 4.14.290-gnu1
[releases.git] / tools / perf / util / dso.c
1 // SPDX-License-Identifier: GPL-2.0
2 #include <asm/bug.h>
3 #include <linux/kernel.h>
4 #include <sys/time.h>
5 #include <sys/resource.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <unistd.h>
9 #include <errno.h>
10 #include "compress.h"
11 #include "path.h"
12 #include "symbol.h"
13 #include "dso.h"
14 #include "machine.h"
15 #include "auxtrace.h"
16 #include "util.h"
17 #include "debug.h"
18 #include "string2.h"
19 #include "vdso.h"
20
21 static const char * const debuglink_paths[] = {
22         "%.0s%s",
23         "%s/%s",
24         "%s/.debug/%s",
25         "/usr/lib/debug%s/%s"
26 };
27
28 char dso__symtab_origin(const struct dso *dso)
29 {
30         static const char origin[] = {
31                 [DSO_BINARY_TYPE__KALLSYMS]                     = 'k',
32                 [DSO_BINARY_TYPE__VMLINUX]                      = 'v',
33                 [DSO_BINARY_TYPE__JAVA_JIT]                     = 'j',
34                 [DSO_BINARY_TYPE__DEBUGLINK]                    = 'l',
35                 [DSO_BINARY_TYPE__BUILD_ID_CACHE]               = 'B',
36                 [DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO]     = 'D',
37                 [DSO_BINARY_TYPE__FEDORA_DEBUGINFO]             = 'f',
38                 [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO]             = 'u',
39                 [DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO]     = 'x',
40                 [DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO]       = 'o',
41                 [DSO_BINARY_TYPE__BUILDID_DEBUGINFO]            = 'b',
42                 [DSO_BINARY_TYPE__SYSTEM_PATH_DSO]              = 'd',
43                 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE]          = 'K',
44                 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP]     = 'm',
45                 [DSO_BINARY_TYPE__GUEST_KALLSYMS]               = 'g',
46                 [DSO_BINARY_TYPE__GUEST_KMODULE]                = 'G',
47                 [DSO_BINARY_TYPE__GUEST_KMODULE_COMP]           = 'M',
48                 [DSO_BINARY_TYPE__GUEST_VMLINUX]                = 'V',
49         };
50
51         if (dso == NULL || dso->symtab_type == DSO_BINARY_TYPE__NOT_FOUND)
52                 return '!';
53         return origin[dso->symtab_type];
54 }
55
56 int dso__read_binary_type_filename(const struct dso *dso,
57                                    enum dso_binary_type type,
58                                    char *root_dir, char *filename, size_t size)
59 {
60         char build_id_hex[SBUILD_ID_SIZE];
61         int ret = 0;
62         size_t len;
63
64         switch (type) {
65         case DSO_BINARY_TYPE__DEBUGLINK:
66         {
67                 const char *last_slash;
68                 char dso_dir[PATH_MAX];
69                 char symfile[PATH_MAX];
70                 unsigned int i;
71
72                 len = __symbol__join_symfs(filename, size, dso->long_name);
73                 last_slash = filename + len;
74                 while (last_slash != filename && *last_slash != '/')
75                         last_slash--;
76
77                 strncpy(dso_dir, filename, last_slash - filename);
78                 dso_dir[last_slash-filename] = '\0';
79
80                 if (!is_regular_file(filename)) {
81                         ret = -1;
82                         break;
83                 }
84
85                 ret = filename__read_debuglink(filename, symfile, PATH_MAX);
86                 if (ret)
87                         break;
88
89                 /* Check predefined locations where debug file might reside */
90                 ret = -1;
91                 for (i = 0; i < ARRAY_SIZE(debuglink_paths); i++) {
92                         snprintf(filename, size,
93                                         debuglink_paths[i], dso_dir, symfile);
94                         if (is_regular_file(filename)) {
95                                 ret = 0;
96                                 break;
97                         }
98                 }
99
100                 break;
101         }
102         case DSO_BINARY_TYPE__BUILD_ID_CACHE:
103                 if (dso__build_id_filename(dso, filename, size, false) == NULL)
104                         ret = -1;
105                 break;
106
107         case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO:
108                 if (dso__build_id_filename(dso, filename, size, true) == NULL)
109                         ret = -1;
110                 break;
111
112         case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
113                 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
114                 snprintf(filename + len, size - len, "%s.debug", dso->long_name);
115                 break;
116
117         case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
118                 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
119                 snprintf(filename + len, size - len, "%s", dso->long_name);
120                 break;
121
122         case DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO:
123                 /*
124                  * Ubuntu can mixup /usr/lib with /lib, putting debuginfo in
125                  * /usr/lib/debug/lib when it is expected to be in
126                  * /usr/lib/debug/usr/lib
127                  */
128                 if (strlen(dso->long_name) < 9 ||
129                     strncmp(dso->long_name, "/usr/lib/", 9)) {
130                         ret = -1;
131                         break;
132                 }
133                 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
134                 snprintf(filename + len, size - len, "%s", dso->long_name + 4);
135                 break;
136
137         case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
138         {
139                 const char *last_slash;
140                 size_t dir_size;
141
142                 last_slash = dso->long_name + dso->long_name_len;
143                 while (last_slash != dso->long_name && *last_slash != '/')
144                         last_slash--;
145
146                 len = __symbol__join_symfs(filename, size, "");
147                 dir_size = last_slash - dso->long_name + 2;
148                 if (dir_size > (size - len)) {
149                         ret = -1;
150                         break;
151                 }
152                 len += scnprintf(filename + len, dir_size, "%s",  dso->long_name);
153                 len += scnprintf(filename + len , size - len, ".debug%s",
154                                                                 last_slash);
155                 break;
156         }
157
158         case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
159                 if (!dso->has_build_id) {
160                         ret = -1;
161                         break;
162                 }
163
164                 build_id__sprintf(dso->build_id,
165                                   sizeof(dso->build_id),
166                                   build_id_hex);
167                 len = __symbol__join_symfs(filename, size, "/usr/lib/debug/.build-id/");
168                 snprintf(filename + len, size - len, "%.2s/%s.debug",
169                          build_id_hex, build_id_hex + 2);
170                 break;
171
172         case DSO_BINARY_TYPE__VMLINUX:
173         case DSO_BINARY_TYPE__GUEST_VMLINUX:
174         case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
175                 __symbol__join_symfs(filename, size, dso->long_name);
176                 break;
177
178         case DSO_BINARY_TYPE__GUEST_KMODULE:
179         case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
180                 path__join3(filename, size, symbol_conf.symfs,
181                             root_dir, dso->long_name);
182                 break;
183
184         case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
185         case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
186                 __symbol__join_symfs(filename, size, dso->long_name);
187                 break;
188
189         case DSO_BINARY_TYPE__KCORE:
190         case DSO_BINARY_TYPE__GUEST_KCORE:
191                 snprintf(filename, size, "%s", dso->long_name);
192                 break;
193
194         default:
195         case DSO_BINARY_TYPE__KALLSYMS:
196         case DSO_BINARY_TYPE__GUEST_KALLSYMS:
197         case DSO_BINARY_TYPE__JAVA_JIT:
198         case DSO_BINARY_TYPE__NOT_FOUND:
199                 ret = -1;
200                 break;
201         }
202
203         return ret;
204 }
205
206 static const struct {
207         const char *fmt;
208         int (*decompress)(const char *input, int output);
209 } compressions[] = {
210 #ifdef HAVE_ZLIB_SUPPORT
211         { "gz", gzip_decompress_to_file },
212 #endif
213 #ifdef HAVE_LZMA_SUPPORT
214         { "xz", lzma_decompress_to_file },
215 #endif
216         { NULL, NULL },
217 };
218
219 bool is_supported_compression(const char *ext)
220 {
221         unsigned i;
222
223         for (i = 0; compressions[i].fmt; i++) {
224                 if (!strcmp(ext, compressions[i].fmt))
225                         return true;
226         }
227         return false;
228 }
229
230 bool is_kernel_module(const char *pathname, int cpumode)
231 {
232         struct kmod_path m;
233         int mode = cpumode & PERF_RECORD_MISC_CPUMODE_MASK;
234
235         WARN_ONCE(mode != cpumode,
236                   "Internal error: passing unmasked cpumode (%x) to is_kernel_module",
237                   cpumode);
238
239         switch (mode) {
240         case PERF_RECORD_MISC_USER:
241         case PERF_RECORD_MISC_HYPERVISOR:
242         case PERF_RECORD_MISC_GUEST_USER:
243                 return false;
244         /* Treat PERF_RECORD_MISC_CPUMODE_UNKNOWN as kernel */
245         default:
246                 if (kmod_path__parse(&m, pathname)) {
247                         pr_err("Failed to check whether %s is a kernel module or not. Assume it is.",
248                                         pathname);
249                         return true;
250                 }
251         }
252
253         return m.kmod;
254 }
255
256 bool decompress_to_file(const char *ext, const char *filename, int output_fd)
257 {
258         unsigned i;
259
260         for (i = 0; compressions[i].fmt; i++) {
261                 if (!strcmp(ext, compressions[i].fmt))
262                         return !compressions[i].decompress(filename,
263                                                            output_fd);
264         }
265         return false;
266 }
267
268 bool dso__needs_decompress(struct dso *dso)
269 {
270         return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
271                 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
272 }
273
274 static int decompress_kmodule(struct dso *dso, const char *name, char *tmpbuf)
275 {
276         int fd = -1;
277         struct kmod_path m;
278
279         if (!dso__needs_decompress(dso))
280                 return -1;
281
282         if (kmod_path__parse_ext(&m, dso->long_name))
283                 return -1;
284
285         if (!m.comp)
286                 goto out;
287
288         fd = mkstemp(tmpbuf);
289         if (fd < 0) {
290                 dso->load_errno = errno;
291                 goto out;
292         }
293
294         if (!decompress_to_file(m.ext, name, fd)) {
295                 dso->load_errno = DSO_LOAD_ERRNO__DECOMPRESSION_FAILURE;
296                 close(fd);
297                 fd = -1;
298         }
299
300 out:
301         free(m.ext);
302         return fd;
303 }
304
305 int dso__decompress_kmodule_fd(struct dso *dso, const char *name)
306 {
307         char tmpbuf[] = KMOD_DECOMP_NAME;
308         int fd;
309
310         fd = decompress_kmodule(dso, name, tmpbuf);
311         unlink(tmpbuf);
312         return fd;
313 }
314
315 int dso__decompress_kmodule_path(struct dso *dso, const char *name,
316                                  char *pathname, size_t len)
317 {
318         char tmpbuf[] = KMOD_DECOMP_NAME;
319         int fd;
320
321         fd = decompress_kmodule(dso, name, tmpbuf);
322         if (fd < 0) {
323                 unlink(tmpbuf);
324                 return -1;
325         }
326
327         strncpy(pathname, tmpbuf, len);
328         close(fd);
329         return 0;
330 }
331
332 /*
333  * Parses kernel module specified in @path and updates
334  * @m argument like:
335  *
336  *    @comp - true if @path contains supported compression suffix,
337  *            false otherwise
338  *    @kmod - true if @path contains '.ko' suffix in right position,
339  *            false otherwise
340  *    @name - if (@alloc_name && @kmod) is true, it contains strdup-ed base name
341  *            of the kernel module without suffixes, otherwise strudup-ed
342  *            base name of @path
343  *    @ext  - if (@alloc_ext && @comp) is true, it contains strdup-ed string
344  *            the compression suffix
345  *
346  * Returns 0 if there's no strdup error, -ENOMEM otherwise.
347  */
348 int __kmod_path__parse(struct kmod_path *m, const char *path,
349                        bool alloc_name, bool alloc_ext)
350 {
351         const char *name = strrchr(path, '/');
352         const char *ext  = strrchr(path, '.');
353         bool is_simple_name = false;
354
355         memset(m, 0x0, sizeof(*m));
356         name = name ? name + 1 : path;
357
358         /*
359          * '.' is also a valid character for module name. For example:
360          * [aaa.bbb] is a valid module name. '[' should have higher
361          * priority than '.ko' suffix.
362          *
363          * The kernel names are from machine__mmap_name. Such
364          * name should belong to kernel itself, not kernel module.
365          */
366         if (name[0] == '[') {
367                 is_simple_name = true;
368                 if ((strncmp(name, "[kernel.kallsyms]", 17) == 0) ||
369                     (strncmp(name, "[guest.kernel.kallsyms", 22) == 0) ||
370                     (strncmp(name, "[vdso]", 6) == 0) ||
371                     (strncmp(name, "[vdso32]", 8) == 0) ||
372                     (strncmp(name, "[vdsox32]", 9) == 0) ||
373                     (strncmp(name, "[vsyscall]", 10) == 0)) {
374                         m->kmod = false;
375
376                 } else
377                         m->kmod = true;
378         }
379
380         /* No extension, just return name. */
381         if ((ext == NULL) || is_simple_name) {
382                 if (alloc_name) {
383                         m->name = strdup(name);
384                         return m->name ? 0 : -ENOMEM;
385                 }
386                 return 0;
387         }
388
389         if (is_supported_compression(ext + 1)) {
390                 m->comp = true;
391                 ext -= 3;
392         }
393
394         /* Check .ko extension only if there's enough name left. */
395         if (ext > name)
396                 m->kmod = !strncmp(ext, ".ko", 3);
397
398         if (alloc_name) {
399                 if (m->kmod) {
400                         if (asprintf(&m->name, "[%.*s]", (int) (ext - name), name) == -1)
401                                 return -ENOMEM;
402                 } else {
403                         if (asprintf(&m->name, "%s", name) == -1)
404                                 return -ENOMEM;
405                 }
406
407                 strxfrchar(m->name, '-', '_');
408         }
409
410         if (alloc_ext && m->comp) {
411                 m->ext = strdup(ext + 4);
412                 if (!m->ext) {
413                         free((void *) m->name);
414                         return -ENOMEM;
415                 }
416         }
417
418         return 0;
419 }
420
421 void dso__set_module_info(struct dso *dso, struct kmod_path *m,
422                           struct machine *machine)
423 {
424         if (machine__is_host(machine))
425                 dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE;
426         else
427                 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE;
428
429         /* _KMODULE_COMP should be next to _KMODULE */
430         if (m->kmod && m->comp)
431                 dso->symtab_type++;
432
433         dso__set_short_name(dso, strdup(m->name), true);
434 }
435
436 /*
437  * Global list of open DSOs and the counter.
438  */
439 static LIST_HEAD(dso__data_open);
440 static long dso__data_open_cnt;
441 static pthread_mutex_t dso__data_open_lock = PTHREAD_MUTEX_INITIALIZER;
442
443 static void dso__list_add(struct dso *dso)
444 {
445         list_add_tail(&dso->data.open_entry, &dso__data_open);
446         dso__data_open_cnt++;
447 }
448
449 static void dso__list_del(struct dso *dso)
450 {
451         list_del(&dso->data.open_entry);
452         WARN_ONCE(dso__data_open_cnt <= 0,
453                   "DSO data fd counter out of bounds.");
454         dso__data_open_cnt--;
455 }
456
457 static void close_first_dso(void);
458
459 static int do_open(char *name)
460 {
461         int fd;
462         char sbuf[STRERR_BUFSIZE];
463
464         do {
465                 fd = open(name, O_RDONLY);
466                 if (fd >= 0)
467                         return fd;
468
469                 pr_debug("dso open failed: %s\n",
470                          str_error_r(errno, sbuf, sizeof(sbuf)));
471                 if (!dso__data_open_cnt || errno != EMFILE)
472                         break;
473
474                 close_first_dso();
475         } while (1);
476
477         return -1;
478 }
479
480 static int __open_dso(struct dso *dso, struct machine *machine)
481 {
482         int fd = -EINVAL;
483         char *root_dir = (char *)"";
484         char *name = malloc(PATH_MAX);
485
486         if (!name)
487                 return -ENOMEM;
488
489         if (machine)
490                 root_dir = machine->root_dir;
491
492         if (dso__read_binary_type_filename(dso, dso->binary_type,
493                                             root_dir, name, PATH_MAX))
494                 goto out;
495
496         if (!is_regular_file(name))
497                 goto out;
498
499         if (dso__needs_decompress(dso)) {
500                 char newpath[KMOD_DECOMP_LEN];
501                 size_t len = sizeof(newpath);
502
503                 if (dso__decompress_kmodule_path(dso, name, newpath, len) < 0) {
504                         fd = -dso->load_errno;
505                         goto out;
506                 }
507
508                 strcpy(name, newpath);
509         }
510
511         fd = do_open(name);
512
513         if (dso__needs_decompress(dso))
514                 unlink(name);
515
516 out:
517         free(name);
518         return fd;
519 }
520
521 static void check_data_close(void);
522
523 /**
524  * dso_close - Open DSO data file
525  * @dso: dso object
526  *
527  * Open @dso's data file descriptor and updates
528  * list/count of open DSO objects.
529  */
530 static int open_dso(struct dso *dso, struct machine *machine)
531 {
532         int fd;
533         struct nscookie nsc;
534
535         if (dso->binary_type != DSO_BINARY_TYPE__BUILD_ID_CACHE)
536                 nsinfo__mountns_enter(dso->nsinfo, &nsc);
537         fd = __open_dso(dso, machine);
538         if (dso->binary_type != DSO_BINARY_TYPE__BUILD_ID_CACHE)
539                 nsinfo__mountns_exit(&nsc);
540
541         if (fd >= 0) {
542                 dso__list_add(dso);
543                 /*
544                  * Check if we crossed the allowed number
545                  * of opened DSOs and close one if needed.
546                  */
547                 check_data_close();
548         }
549
550         return fd;
551 }
552
553 static void close_data_fd(struct dso *dso)
554 {
555         if (dso->data.fd >= 0) {
556                 close(dso->data.fd);
557                 dso->data.fd = -1;
558                 dso->data.file_size = 0;
559                 dso__list_del(dso);
560         }
561 }
562
563 /**
564  * dso_close - Close DSO data file
565  * @dso: dso object
566  *
567  * Close @dso's data file descriptor and updates
568  * list/count of open DSO objects.
569  */
570 static void close_dso(struct dso *dso)
571 {
572         close_data_fd(dso);
573 }
574
575 static void close_first_dso(void)
576 {
577         struct dso *dso;
578
579         dso = list_first_entry(&dso__data_open, struct dso, data.open_entry);
580         close_dso(dso);
581 }
582
583 static rlim_t get_fd_limit(void)
584 {
585         struct rlimit l;
586         rlim_t limit = 0;
587
588         /* Allow half of the current open fd limit. */
589         if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
590                 if (l.rlim_cur == RLIM_INFINITY)
591                         limit = l.rlim_cur;
592                 else
593                         limit = l.rlim_cur / 2;
594         } else {
595                 pr_err("failed to get fd limit\n");
596                 limit = 1;
597         }
598
599         return limit;
600 }
601
602 static rlim_t fd_limit;
603
604 /*
605  * Used only by tests/dso-data.c to reset the environment
606  * for tests. I dont expect we should change this during
607  * standard runtime.
608  */
609 void reset_fd_limit(void)
610 {
611         fd_limit = 0;
612 }
613
614 static bool may_cache_fd(void)
615 {
616         if (!fd_limit)
617                 fd_limit = get_fd_limit();
618
619         if (fd_limit == RLIM_INFINITY)
620                 return true;
621
622         return fd_limit > (rlim_t) dso__data_open_cnt;
623 }
624
625 /*
626  * Check and close LRU dso if we crossed allowed limit
627  * for opened dso file descriptors. The limit is half
628  * of the RLIMIT_NOFILE files opened.
629 */
630 static void check_data_close(void)
631 {
632         bool cache_fd = may_cache_fd();
633
634         if (!cache_fd)
635                 close_first_dso();
636 }
637
638 /**
639  * dso__data_close - Close DSO data file
640  * @dso: dso object
641  *
642  * External interface to close @dso's data file descriptor.
643  */
644 void dso__data_close(struct dso *dso)
645 {
646         pthread_mutex_lock(&dso__data_open_lock);
647         close_dso(dso);
648         pthread_mutex_unlock(&dso__data_open_lock);
649 }
650
651 static void try_to_open_dso(struct dso *dso, struct machine *machine)
652 {
653         enum dso_binary_type binary_type_data[] = {
654                 DSO_BINARY_TYPE__BUILD_ID_CACHE,
655                 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
656                 DSO_BINARY_TYPE__NOT_FOUND,
657         };
658         int i = 0;
659
660         if (dso->data.fd >= 0)
661                 return;
662
663         if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) {
664                 dso->data.fd = open_dso(dso, machine);
665                 goto out;
666         }
667
668         do {
669                 dso->binary_type = binary_type_data[i++];
670
671                 dso->data.fd = open_dso(dso, machine);
672                 if (dso->data.fd >= 0)
673                         goto out;
674
675         } while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND);
676 out:
677         if (dso->data.fd >= 0)
678                 dso->data.status = DSO_DATA_STATUS_OK;
679         else
680                 dso->data.status = DSO_DATA_STATUS_ERROR;
681 }
682
683 /**
684  * dso__data_get_fd - Get dso's data file descriptor
685  * @dso: dso object
686  * @machine: machine object
687  *
688  * External interface to find dso's file, open it and
689  * returns file descriptor.  It should be paired with
690  * dso__data_put_fd() if it returns non-negative value.
691  */
692 int dso__data_get_fd(struct dso *dso, struct machine *machine)
693 {
694         if (dso->data.status == DSO_DATA_STATUS_ERROR)
695                 return -1;
696
697         if (pthread_mutex_lock(&dso__data_open_lock) < 0)
698                 return -1;
699
700         try_to_open_dso(dso, machine);
701
702         if (dso->data.fd < 0)
703                 pthread_mutex_unlock(&dso__data_open_lock);
704
705         return dso->data.fd;
706 }
707
708 void dso__data_put_fd(struct dso *dso __maybe_unused)
709 {
710         pthread_mutex_unlock(&dso__data_open_lock);
711 }
712
713 bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by)
714 {
715         u32 flag = 1 << by;
716
717         if (dso->data.status_seen & flag)
718                 return true;
719
720         dso->data.status_seen |= flag;
721
722         return false;
723 }
724
725 static void
726 dso_cache__free(struct dso *dso)
727 {
728         struct rb_root *root = &dso->data.cache;
729         struct rb_node *next = rb_first(root);
730
731         pthread_mutex_lock(&dso->lock);
732         while (next) {
733                 struct dso_cache *cache;
734
735                 cache = rb_entry(next, struct dso_cache, rb_node);
736                 next = rb_next(&cache->rb_node);
737                 rb_erase(&cache->rb_node, root);
738                 free(cache);
739         }
740         pthread_mutex_unlock(&dso->lock);
741 }
742
743 static struct dso_cache *dso_cache__find(struct dso *dso, u64 offset)
744 {
745         const struct rb_root *root = &dso->data.cache;
746         struct rb_node * const *p = &root->rb_node;
747         const struct rb_node *parent = NULL;
748         struct dso_cache *cache;
749
750         while (*p != NULL) {
751                 u64 end;
752
753                 parent = *p;
754                 cache = rb_entry(parent, struct dso_cache, rb_node);
755                 end = cache->offset + DSO__DATA_CACHE_SIZE;
756
757                 if (offset < cache->offset)
758                         p = &(*p)->rb_left;
759                 else if (offset >= end)
760                         p = &(*p)->rb_right;
761                 else
762                         return cache;
763         }
764
765         return NULL;
766 }
767
768 static struct dso_cache *
769 dso_cache__insert(struct dso *dso, struct dso_cache *new)
770 {
771         struct rb_root *root = &dso->data.cache;
772         struct rb_node **p = &root->rb_node;
773         struct rb_node *parent = NULL;
774         struct dso_cache *cache;
775         u64 offset = new->offset;
776
777         pthread_mutex_lock(&dso->lock);
778         while (*p != NULL) {
779                 u64 end;
780
781                 parent = *p;
782                 cache = rb_entry(parent, struct dso_cache, rb_node);
783                 end = cache->offset + DSO__DATA_CACHE_SIZE;
784
785                 if (offset < cache->offset)
786                         p = &(*p)->rb_left;
787                 else if (offset >= end)
788                         p = &(*p)->rb_right;
789                 else
790                         goto out;
791         }
792
793         rb_link_node(&new->rb_node, parent, p);
794         rb_insert_color(&new->rb_node, root);
795
796         cache = NULL;
797 out:
798         pthread_mutex_unlock(&dso->lock);
799         return cache;
800 }
801
802 static ssize_t
803 dso_cache__memcpy(struct dso_cache *cache, u64 offset,
804                   u8 *data, u64 size)
805 {
806         u64 cache_offset = offset - cache->offset;
807         u64 cache_size   = min(cache->size - cache_offset, size);
808
809         memcpy(data, cache->data + cache_offset, cache_size);
810         return cache_size;
811 }
812
813 static ssize_t
814 dso_cache__read(struct dso *dso, struct machine *machine,
815                 u64 offset, u8 *data, ssize_t size)
816 {
817         struct dso_cache *cache;
818         struct dso_cache *old;
819         ssize_t ret;
820
821         do {
822                 u64 cache_offset;
823
824                 cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
825                 if (!cache)
826                         return -ENOMEM;
827
828                 pthread_mutex_lock(&dso__data_open_lock);
829
830                 /*
831                  * dso->data.fd might be closed if other thread opened another
832                  * file (dso) due to open file limit (RLIMIT_NOFILE).
833                  */
834                 try_to_open_dso(dso, machine);
835
836                 if (dso->data.fd < 0) {
837                         ret = -errno;
838                         dso->data.status = DSO_DATA_STATUS_ERROR;
839                         break;
840                 }
841
842                 cache_offset = offset & DSO__DATA_CACHE_MASK;
843
844                 ret = pread(dso->data.fd, cache->data, DSO__DATA_CACHE_SIZE, cache_offset);
845                 if (ret <= 0)
846                         break;
847
848                 cache->offset = cache_offset;
849                 cache->size   = ret;
850         } while (0);
851
852         pthread_mutex_unlock(&dso__data_open_lock);
853
854         if (ret > 0) {
855                 old = dso_cache__insert(dso, cache);
856                 if (old) {
857                         /* we lose the race */
858                         free(cache);
859                         cache = old;
860                 }
861
862                 ret = dso_cache__memcpy(cache, offset, data, size);
863         }
864
865         if (ret <= 0)
866                 free(cache);
867
868         return ret;
869 }
870
871 static ssize_t dso_cache_read(struct dso *dso, struct machine *machine,
872                               u64 offset, u8 *data, ssize_t size)
873 {
874         struct dso_cache *cache;
875
876         cache = dso_cache__find(dso, offset);
877         if (cache)
878                 return dso_cache__memcpy(cache, offset, data, size);
879         else
880                 return dso_cache__read(dso, machine, offset, data, size);
881 }
882
883 /*
884  * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
885  * in the rb_tree. Any read to already cached data is served
886  * by cached data.
887  */
888 static ssize_t cached_read(struct dso *dso, struct machine *machine,
889                            u64 offset, u8 *data, ssize_t size)
890 {
891         ssize_t r = 0;
892         u8 *p = data;
893
894         do {
895                 ssize_t ret;
896
897                 ret = dso_cache_read(dso, machine, offset, p, size);
898                 if (ret < 0)
899                         return ret;
900
901                 /* Reached EOF, return what we have. */
902                 if (!ret)
903                         break;
904
905                 BUG_ON(ret > size);
906
907                 r      += ret;
908                 p      += ret;
909                 offset += ret;
910                 size   -= ret;
911
912         } while (size);
913
914         return r;
915 }
916
917 static int data_file_size(struct dso *dso, struct machine *machine)
918 {
919         int ret = 0;
920         struct stat st;
921         char sbuf[STRERR_BUFSIZE];
922
923         if (dso->data.file_size)
924                 return 0;
925
926         if (dso->data.status == DSO_DATA_STATUS_ERROR)
927                 return -1;
928
929         pthread_mutex_lock(&dso__data_open_lock);
930
931         /*
932          * dso->data.fd might be closed if other thread opened another
933          * file (dso) due to open file limit (RLIMIT_NOFILE).
934          */
935         try_to_open_dso(dso, machine);
936
937         if (dso->data.fd < 0) {
938                 ret = -errno;
939                 dso->data.status = DSO_DATA_STATUS_ERROR;
940                 goto out;
941         }
942
943         if (fstat(dso->data.fd, &st) < 0) {
944                 ret = -errno;
945                 pr_err("dso cache fstat failed: %s\n",
946                        str_error_r(errno, sbuf, sizeof(sbuf)));
947                 dso->data.status = DSO_DATA_STATUS_ERROR;
948                 goto out;
949         }
950         dso->data.file_size = st.st_size;
951
952 out:
953         pthread_mutex_unlock(&dso__data_open_lock);
954         return ret;
955 }
956
957 /**
958  * dso__data_size - Return dso data size
959  * @dso: dso object
960  * @machine: machine object
961  *
962  * Return: dso data size
963  */
964 off_t dso__data_size(struct dso *dso, struct machine *machine)
965 {
966         if (data_file_size(dso, machine))
967                 return -1;
968
969         /* For now just estimate dso data size is close to file size */
970         return dso->data.file_size;
971 }
972
973 static ssize_t data_read_offset(struct dso *dso, struct machine *machine,
974                                 u64 offset, u8 *data, ssize_t size)
975 {
976         if (data_file_size(dso, machine))
977                 return -1;
978
979         /* Check the offset sanity. */
980         if (offset > dso->data.file_size)
981                 return -1;
982
983         if (offset + size < offset)
984                 return -1;
985
986         return cached_read(dso, machine, offset, data, size);
987 }
988
989 /**
990  * dso__data_read_offset - Read data from dso file offset
991  * @dso: dso object
992  * @machine: machine object
993  * @offset: file offset
994  * @data: buffer to store data
995  * @size: size of the @data buffer
996  *
997  * External interface to read data from dso file offset. Open
998  * dso data file and use cached_read to get the data.
999  */
1000 ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
1001                               u64 offset, u8 *data, ssize_t size)
1002 {
1003         if (dso->data.status == DSO_DATA_STATUS_ERROR)
1004                 return -1;
1005
1006         return data_read_offset(dso, machine, offset, data, size);
1007 }
1008
1009 /**
1010  * dso__data_read_addr - Read data from dso address
1011  * @dso: dso object
1012  * @machine: machine object
1013  * @add: virtual memory address
1014  * @data: buffer to store data
1015  * @size: size of the @data buffer
1016  *
1017  * External interface to read data from dso address.
1018  */
1019 ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
1020                             struct machine *machine, u64 addr,
1021                             u8 *data, ssize_t size)
1022 {
1023         u64 offset = map->map_ip(map, addr);
1024         return dso__data_read_offset(dso, machine, offset, data, size);
1025 }
1026
1027 struct map *dso__new_map(const char *name)
1028 {
1029         struct map *map = NULL;
1030         struct dso *dso = dso__new(name);
1031
1032         if (dso)
1033                 map = map__new2(0, dso, MAP__FUNCTION);
1034
1035         return map;
1036 }
1037
1038 struct dso *machine__findnew_kernel(struct machine *machine, const char *name,
1039                                     const char *short_name, int dso_type)
1040 {
1041         /*
1042          * The kernel dso could be created by build_id processing.
1043          */
1044         struct dso *dso = machine__findnew_dso(machine, name);
1045
1046         /*
1047          * We need to run this in all cases, since during the build_id
1048          * processing we had no idea this was the kernel dso.
1049          */
1050         if (dso != NULL) {
1051                 dso__set_short_name(dso, short_name, false);
1052                 dso->kernel = dso_type;
1053         }
1054
1055         return dso;
1056 }
1057
1058 /*
1059  * Find a matching entry and/or link current entry to RB tree.
1060  * Either one of the dso or name parameter must be non-NULL or the
1061  * function will not work.
1062  */
1063 static struct dso *__dso__findlink_by_longname(struct rb_root *root,
1064                                                struct dso *dso, const char *name)
1065 {
1066         struct rb_node **p = &root->rb_node;
1067         struct rb_node  *parent = NULL;
1068
1069         if (!name)
1070                 name = dso->long_name;
1071         /*
1072          * Find node with the matching name
1073          */
1074         while (*p) {
1075                 struct dso *this = rb_entry(*p, struct dso, rb_node);
1076                 int rc = strcmp(name, this->long_name);
1077
1078                 parent = *p;
1079                 if (rc == 0) {
1080                         /*
1081                          * In case the new DSO is a duplicate of an existing
1082                          * one, print a one-time warning & put the new entry
1083                          * at the end of the list of duplicates.
1084                          */
1085                         if (!dso || (dso == this))
1086                                 return this;    /* Find matching dso */
1087                         /*
1088                          * The core kernel DSOs may have duplicated long name.
1089                          * In this case, the short name should be different.
1090                          * Comparing the short names to differentiate the DSOs.
1091                          */
1092                         rc = strcmp(dso->short_name, this->short_name);
1093                         if (rc == 0) {
1094                                 pr_err("Duplicated dso name: %s\n", name);
1095                                 return NULL;
1096                         }
1097                 }
1098                 if (rc < 0)
1099                         p = &parent->rb_left;
1100                 else
1101                         p = &parent->rb_right;
1102         }
1103         if (dso) {
1104                 /* Add new node and rebalance tree */
1105                 rb_link_node(&dso->rb_node, parent, p);
1106                 rb_insert_color(&dso->rb_node, root);
1107                 dso->root = root;
1108         }
1109         return NULL;
1110 }
1111
1112 static inline struct dso *__dso__find_by_longname(struct rb_root *root,
1113                                                   const char *name)
1114 {
1115         return __dso__findlink_by_longname(root, NULL, name);
1116 }
1117
1118 void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
1119 {
1120         struct rb_root *root = dso->root;
1121
1122         if (name == NULL)
1123                 return;
1124
1125         if (dso->long_name_allocated)
1126                 free((char *)dso->long_name);
1127
1128         if (root) {
1129                 rb_erase(&dso->rb_node, root);
1130                 /*
1131                  * __dso__findlink_by_longname() isn't guaranteed to add it
1132                  * back, so a clean removal is required here.
1133                  */
1134                 RB_CLEAR_NODE(&dso->rb_node);
1135                 dso->root = NULL;
1136         }
1137
1138         dso->long_name           = name;
1139         dso->long_name_len       = strlen(name);
1140         dso->long_name_allocated = name_allocated;
1141
1142         if (root)
1143                 __dso__findlink_by_longname(root, dso, NULL);
1144 }
1145
1146 void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
1147 {
1148         if (name == NULL)
1149                 return;
1150
1151         if (dso->short_name_allocated)
1152                 free((char *)dso->short_name);
1153
1154         dso->short_name           = name;
1155         dso->short_name_len       = strlen(name);
1156         dso->short_name_allocated = name_allocated;
1157 }
1158
1159 static void dso__set_basename(struct dso *dso)
1160 {
1161        /*
1162         * basename() may modify path buffer, so we must pass
1163         * a copy.
1164         */
1165        char *base, *lname = strdup(dso->long_name);
1166
1167        if (!lname)
1168                return;
1169
1170        /*
1171         * basename() may return a pointer to internal
1172         * storage which is reused in subsequent calls
1173         * so copy the result.
1174         */
1175        base = strdup(basename(lname));
1176
1177        free(lname);
1178
1179        if (!base)
1180                return;
1181
1182        dso__set_short_name(dso, base, true);
1183 }
1184
1185 int dso__name_len(const struct dso *dso)
1186 {
1187         if (!dso)
1188                 return strlen("[unknown]");
1189         if (verbose > 0)
1190                 return dso->long_name_len;
1191
1192         return dso->short_name_len;
1193 }
1194
1195 bool dso__loaded(const struct dso *dso, enum map_type type)
1196 {
1197         return dso->loaded & (1 << type);
1198 }
1199
1200 bool dso__sorted_by_name(const struct dso *dso, enum map_type type)
1201 {
1202         return dso->sorted_by_name & (1 << type);
1203 }
1204
1205 void dso__set_sorted_by_name(struct dso *dso, enum map_type type)
1206 {
1207         dso->sorted_by_name |= (1 << type);
1208 }
1209
1210 struct dso *dso__new(const char *name)
1211 {
1212         struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
1213
1214         if (dso != NULL) {
1215                 int i;
1216                 strcpy(dso->name, name);
1217                 dso__set_long_name(dso, dso->name, false);
1218                 dso__set_short_name(dso, dso->name, false);
1219                 for (i = 0; i < MAP__NR_TYPES; ++i)
1220                         dso->symbols[i] = dso->symbol_names[i] = RB_ROOT;
1221                 dso->data.cache = RB_ROOT;
1222                 dso->data.fd = -1;
1223                 dso->data.status = DSO_DATA_STATUS_UNKNOWN;
1224                 dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
1225                 dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
1226                 dso->is_64_bit = (sizeof(void *) == 8);
1227                 dso->loaded = 0;
1228                 dso->rel = 0;
1229                 dso->sorted_by_name = 0;
1230                 dso->has_build_id = 0;
1231                 dso->has_srcline = 1;
1232                 dso->a2l_fails = 1;
1233                 dso->kernel = DSO_TYPE_USER;
1234                 dso->needs_swap = DSO_SWAP__UNSET;
1235                 RB_CLEAR_NODE(&dso->rb_node);
1236                 dso->root = NULL;
1237                 INIT_LIST_HEAD(&dso->node);
1238                 INIT_LIST_HEAD(&dso->data.open_entry);
1239                 pthread_mutex_init(&dso->lock, NULL);
1240                 refcount_set(&dso->refcnt, 1);
1241         }
1242
1243         return dso;
1244 }
1245
1246 void dso__delete(struct dso *dso)
1247 {
1248         int i;
1249
1250         if (!RB_EMPTY_NODE(&dso->rb_node))
1251                 pr_err("DSO %s is still in rbtree when being deleted!\n",
1252                        dso->long_name);
1253         for (i = 0; i < MAP__NR_TYPES; ++i)
1254                 symbols__delete(&dso->symbols[i]);
1255
1256         if (dso->short_name_allocated) {
1257                 zfree((char **)&dso->short_name);
1258                 dso->short_name_allocated = false;
1259         }
1260
1261         if (dso->long_name_allocated) {
1262                 zfree((char **)&dso->long_name);
1263                 dso->long_name_allocated = false;
1264         }
1265
1266         dso__data_close(dso);
1267         auxtrace_cache__free(dso->auxtrace_cache);
1268         dso_cache__free(dso);
1269         dso__free_a2l(dso);
1270         zfree(&dso->symsrc_filename);
1271         nsinfo__zput(dso->nsinfo);
1272         pthread_mutex_destroy(&dso->lock);
1273         free(dso);
1274 }
1275
1276 struct dso *dso__get(struct dso *dso)
1277 {
1278         if (dso)
1279                 refcount_inc(&dso->refcnt);
1280         return dso;
1281 }
1282
1283 void dso__put(struct dso *dso)
1284 {
1285         if (dso && refcount_dec_and_test(&dso->refcnt))
1286                 dso__delete(dso);
1287 }
1288
1289 void dso__set_build_id(struct dso *dso, void *build_id)
1290 {
1291         memcpy(dso->build_id, build_id, sizeof(dso->build_id));
1292         dso->has_build_id = 1;
1293 }
1294
1295 bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
1296 {
1297         return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
1298 }
1299
1300 void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
1301 {
1302         char path[PATH_MAX];
1303
1304         if (machine__is_default_guest(machine))
1305                 return;
1306         sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
1307         if (sysfs__read_build_id(path, dso->build_id,
1308                                  sizeof(dso->build_id)) == 0)
1309                 dso->has_build_id = true;
1310 }
1311
1312 int dso__kernel_module_get_build_id(struct dso *dso,
1313                                     const char *root_dir)
1314 {
1315         char filename[PATH_MAX];
1316         /*
1317          * kernel module short names are of the form "[module]" and
1318          * we need just "module" here.
1319          */
1320         const char *name = dso->short_name + 1;
1321
1322         snprintf(filename, sizeof(filename),
1323                  "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1324                  root_dir, (int)strlen(name) - 1, name);
1325
1326         if (sysfs__read_build_id(filename, dso->build_id,
1327                                  sizeof(dso->build_id)) == 0)
1328                 dso->has_build_id = true;
1329
1330         return 0;
1331 }
1332
1333 bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
1334 {
1335         bool have_build_id = false;
1336         struct dso *pos;
1337         struct nscookie nsc;
1338
1339         list_for_each_entry(pos, head, node) {
1340                 if (with_hits && !pos->hit && !dso__is_vdso(pos))
1341                         continue;
1342                 if (pos->has_build_id) {
1343                         have_build_id = true;
1344                         continue;
1345                 }
1346                 nsinfo__mountns_enter(pos->nsinfo, &nsc);
1347                 if (filename__read_build_id(pos->long_name, pos->build_id,
1348                                             sizeof(pos->build_id)) > 0) {
1349                         have_build_id     = true;
1350                         pos->has_build_id = true;
1351                 }
1352                 nsinfo__mountns_exit(&nsc);
1353         }
1354
1355         return have_build_id;
1356 }
1357
1358 void __dsos__add(struct dsos *dsos, struct dso *dso)
1359 {
1360         list_add_tail(&dso->node, &dsos->head);
1361         __dso__findlink_by_longname(&dsos->root, dso, NULL);
1362         /*
1363          * It is now in the linked list, grab a reference, then garbage collect
1364          * this when needing memory, by looking at LRU dso instances in the
1365          * list with atomic_read(&dso->refcnt) == 1, i.e. no references
1366          * anywhere besides the one for the list, do, under a lock for the
1367          * list: remove it from the list, then a dso__put(), that probably will
1368          * be the last and will then call dso__delete(), end of life.
1369          *
1370          * That, or at the end of the 'struct machine' lifetime, when all
1371          * 'struct dso' instances will be removed from the list, in
1372          * dsos__exit(), if they have no other reference from some other data
1373          * structure.
1374          *
1375          * E.g.: after processing a 'perf.data' file and storing references
1376          * to objects instantiated while processing events, we will have
1377          * references to the 'thread', 'map', 'dso' structs all from 'struct
1378          * hist_entry' instances, but we may not need anything not referenced,
1379          * so we might as well call machines__exit()/machines__delete() and
1380          * garbage collect it.
1381          */
1382         dso__get(dso);
1383 }
1384
1385 void dsos__add(struct dsos *dsos, struct dso *dso)
1386 {
1387         pthread_rwlock_wrlock(&dsos->lock);
1388         __dsos__add(dsos, dso);
1389         pthread_rwlock_unlock(&dsos->lock);
1390 }
1391
1392 struct dso *__dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
1393 {
1394         struct dso *pos;
1395
1396         if (cmp_short) {
1397                 list_for_each_entry(pos, &dsos->head, node)
1398                         if (strcmp(pos->short_name, name) == 0)
1399                                 return pos;
1400                 return NULL;
1401         }
1402         return __dso__find_by_longname(&dsos->root, name);
1403 }
1404
1405 struct dso *dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
1406 {
1407         struct dso *dso;
1408         pthread_rwlock_rdlock(&dsos->lock);
1409         dso = __dsos__find(dsos, name, cmp_short);
1410         pthread_rwlock_unlock(&dsos->lock);
1411         return dso;
1412 }
1413
1414 struct dso *__dsos__addnew(struct dsos *dsos, const char *name)
1415 {
1416         struct dso *dso = dso__new(name);
1417
1418         if (dso != NULL) {
1419                 __dsos__add(dsos, dso);
1420                 dso__set_basename(dso);
1421                 /* Put dso here because __dsos_add already got it */
1422                 dso__put(dso);
1423         }
1424         return dso;
1425 }
1426
1427 struct dso *__dsos__findnew(struct dsos *dsos, const char *name)
1428 {
1429         struct dso *dso = __dsos__find(dsos, name, false);
1430
1431         return dso ? dso : __dsos__addnew(dsos, name);
1432 }
1433
1434 struct dso *dsos__findnew(struct dsos *dsos, const char *name)
1435 {
1436         struct dso *dso;
1437         pthread_rwlock_wrlock(&dsos->lock);
1438         dso = dso__get(__dsos__findnew(dsos, name));
1439         pthread_rwlock_unlock(&dsos->lock);
1440         return dso;
1441 }
1442
1443 size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
1444                                bool (skip)(struct dso *dso, int parm), int parm)
1445 {
1446         struct dso *pos;
1447         size_t ret = 0;
1448
1449         list_for_each_entry(pos, head, node) {
1450                 if (skip && skip(pos, parm))
1451                         continue;
1452                 ret += dso__fprintf_buildid(pos, fp);
1453                 ret += fprintf(fp, " %s\n", pos->long_name);
1454         }
1455         return ret;
1456 }
1457
1458 size_t __dsos__fprintf(struct list_head *head, FILE *fp)
1459 {
1460         struct dso *pos;
1461         size_t ret = 0;
1462
1463         list_for_each_entry(pos, head, node) {
1464                 int i;
1465                 for (i = 0; i < MAP__NR_TYPES; ++i)
1466                         ret += dso__fprintf(pos, i, fp);
1467         }
1468
1469         return ret;
1470 }
1471
1472 size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
1473 {
1474         char sbuild_id[SBUILD_ID_SIZE];
1475
1476         build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1477         return fprintf(fp, "%s", sbuild_id);
1478 }
1479
1480 size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp)
1481 {
1482         struct rb_node *nd;
1483         size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
1484
1485         if (dso->short_name != dso->long_name)
1486                 ret += fprintf(fp, "%s, ", dso->long_name);
1487         ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
1488                        dso__loaded(dso, type) ? "" : "NOT ");
1489         ret += dso__fprintf_buildid(dso, fp);
1490         ret += fprintf(fp, ")\n");
1491         for (nd = rb_first(&dso->symbols[type]); nd; nd = rb_next(nd)) {
1492                 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
1493                 ret += symbol__fprintf(pos, fp);
1494         }
1495
1496         return ret;
1497 }
1498
1499 enum dso_type dso__type(struct dso *dso, struct machine *machine)
1500 {
1501         int fd;
1502         enum dso_type type = DSO__TYPE_UNKNOWN;
1503
1504         fd = dso__data_get_fd(dso, machine);
1505         if (fd >= 0) {
1506                 type = dso__type_fd(fd);
1507                 dso__data_put_fd(dso);
1508         }
1509
1510         return type;
1511 }
1512
1513 int dso__strerror_load(struct dso *dso, char *buf, size_t buflen)
1514 {
1515         int idx, errnum = dso->load_errno;
1516         /*
1517          * This must have a same ordering as the enum dso_load_errno.
1518          */
1519         static const char *dso_load__error_str[] = {
1520         "Internal tools/perf/ library error",
1521         "Invalid ELF file",
1522         "Can not read build id",
1523         "Mismatching build id",
1524         "Decompression failure",
1525         };
1526
1527         BUG_ON(buflen == 0);
1528
1529         if (errnum >= 0) {
1530                 const char *err = str_error_r(errnum, buf, buflen);
1531
1532                 if (err != buf)
1533                         scnprintf(buf, buflen, "%s", err);
1534
1535                 return 0;
1536         }
1537
1538         if (errnum <  __DSO_LOAD_ERRNO__START || errnum >= __DSO_LOAD_ERRNO__END)
1539                 return -1;
1540
1541         idx = errnum - __DSO_LOAD_ERRNO__START;
1542         scnprintf(buf, buflen, "%s", dso_load__error_str[idx]);
1543         return 0;
1544 }