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