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