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