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