Linux 4.19.133
[linux/fpc-iii.git] / tools / perf / util / dso.c
blob56f86317694d2a1be25a5124840463e10c6c7464
1 // SPDX-License-Identifier: GPL-2.0
2 #include <asm/bug.h>
3 #include <linux/kernel.h>
4 #include <sys/time.h>
5 #include <sys/resource.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <unistd.h>
9 #include <errno.h>
10 #include <fcntl.h>
11 #include "compress.h"
12 #include "path.h"
13 #include "symbol.h"
14 #include "srcline.h"
15 #include "dso.h"
16 #include "machine.h"
17 #include "auxtrace.h"
18 #include "util.h"
19 #include "debug.h"
20 #include "string2.h"
21 #include "vdso.h"
23 static const char * const debuglink_paths[] = {
24 "%.0s%s",
25 "%s/%s",
26 "%s/.debug/%s",
27 "/usr/lib/debug%s/%s"
30 char dso__symtab_origin(const struct dso *dso)
32 static const char origin[] = {
33 [DSO_BINARY_TYPE__KALLSYMS] = 'k',
34 [DSO_BINARY_TYPE__VMLINUX] = 'v',
35 [DSO_BINARY_TYPE__JAVA_JIT] = 'j',
36 [DSO_BINARY_TYPE__DEBUGLINK] = 'l',
37 [DSO_BINARY_TYPE__BUILD_ID_CACHE] = 'B',
38 [DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO] = 'D',
39 [DSO_BINARY_TYPE__FEDORA_DEBUGINFO] = 'f',
40 [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO] = 'u',
41 [DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO] = 'x',
42 [DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO] = 'o',
43 [DSO_BINARY_TYPE__BUILDID_DEBUGINFO] = 'b',
44 [DSO_BINARY_TYPE__SYSTEM_PATH_DSO] = 'd',
45 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE] = 'K',
46 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP] = 'm',
47 [DSO_BINARY_TYPE__GUEST_KALLSYMS] = 'g',
48 [DSO_BINARY_TYPE__GUEST_KMODULE] = 'G',
49 [DSO_BINARY_TYPE__GUEST_KMODULE_COMP] = 'M',
50 [DSO_BINARY_TYPE__GUEST_VMLINUX] = 'V',
53 if (dso == NULL || dso->symtab_type == DSO_BINARY_TYPE__NOT_FOUND)
54 return '!';
55 return origin[dso->symtab_type];
58 int dso__read_binary_type_filename(const struct dso *dso,
59 enum dso_binary_type type,
60 char *root_dir, char *filename, size_t size)
62 char build_id_hex[SBUILD_ID_SIZE];
63 int ret = 0;
64 size_t len;
66 switch (type) {
67 case DSO_BINARY_TYPE__DEBUGLINK:
69 const char *last_slash;
70 char dso_dir[PATH_MAX];
71 char symfile[PATH_MAX];
72 unsigned int i;
74 len = __symbol__join_symfs(filename, size, dso->long_name);
75 last_slash = filename + len;
76 while (last_slash != filename && *last_slash != '/')
77 last_slash--;
79 strncpy(dso_dir, filename, last_slash - filename);
80 dso_dir[last_slash-filename] = '\0';
82 if (!is_regular_file(filename)) {
83 ret = -1;
84 break;
87 ret = filename__read_debuglink(filename, symfile, PATH_MAX);
88 if (ret)
89 break;
91 /* Check predefined locations where debug file might reside */
92 ret = -1;
93 for (i = 0; i < ARRAY_SIZE(debuglink_paths); i++) {
94 snprintf(filename, size,
95 debuglink_paths[i], dso_dir, symfile);
96 if (is_regular_file(filename)) {
97 ret = 0;
98 break;
102 break;
104 case DSO_BINARY_TYPE__BUILD_ID_CACHE:
105 if (dso__build_id_filename(dso, filename, size, false) == NULL)
106 ret = -1;
107 break;
109 case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO:
110 if (dso__build_id_filename(dso, filename, size, true) == NULL)
111 ret = -1;
112 break;
114 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
115 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
116 snprintf(filename + len, size - len, "%s.debug", dso->long_name);
117 break;
119 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
120 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
121 snprintf(filename + len, size - len, "%s", dso->long_name);
122 break;
124 case DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO:
126 * Ubuntu can mixup /usr/lib with /lib, putting debuginfo in
127 * /usr/lib/debug/lib when it is expected to be in
128 * /usr/lib/debug/usr/lib
130 if (strlen(dso->long_name) < 9 ||
131 strncmp(dso->long_name, "/usr/lib/", 9)) {
132 ret = -1;
133 break;
135 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
136 snprintf(filename + len, size - len, "%s", dso->long_name + 4);
137 break;
139 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
141 const char *last_slash;
142 size_t dir_size;
144 last_slash = dso->long_name + dso->long_name_len;
145 while (last_slash != dso->long_name && *last_slash != '/')
146 last_slash--;
148 len = __symbol__join_symfs(filename, size, "");
149 dir_size = last_slash - dso->long_name + 2;
150 if (dir_size > (size - len)) {
151 ret = -1;
152 break;
154 len += scnprintf(filename + len, dir_size, "%s", dso->long_name);
155 len += scnprintf(filename + len , size - len, ".debug%s",
156 last_slash);
157 break;
160 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
161 if (!dso->has_build_id) {
162 ret = -1;
163 break;
166 build_id__sprintf(dso->build_id,
167 sizeof(dso->build_id),
168 build_id_hex);
169 len = __symbol__join_symfs(filename, size, "/usr/lib/debug/.build-id/");
170 snprintf(filename + len, size - len, "%.2s/%s.debug",
171 build_id_hex, build_id_hex + 2);
172 break;
174 case DSO_BINARY_TYPE__VMLINUX:
175 case DSO_BINARY_TYPE__GUEST_VMLINUX:
176 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
177 __symbol__join_symfs(filename, size, dso->long_name);
178 break;
180 case DSO_BINARY_TYPE__GUEST_KMODULE:
181 case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
182 path__join3(filename, size, symbol_conf.symfs,
183 root_dir, dso->long_name);
184 break;
186 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
187 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
188 __symbol__join_symfs(filename, size, dso->long_name);
189 break;
191 case DSO_BINARY_TYPE__KCORE:
192 case DSO_BINARY_TYPE__GUEST_KCORE:
193 snprintf(filename, size, "%s", dso->long_name);
194 break;
196 default:
197 case DSO_BINARY_TYPE__KALLSYMS:
198 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
199 case DSO_BINARY_TYPE__JAVA_JIT:
200 case DSO_BINARY_TYPE__NOT_FOUND:
201 ret = -1;
202 break;
205 return ret;
208 enum {
209 COMP_ID__NONE = 0,
212 static const struct {
213 const char *fmt;
214 int (*decompress)(const char *input, int output);
215 bool (*is_compressed)(const char *input);
216 } compressions[] = {
217 [COMP_ID__NONE] = { .fmt = NULL, },
218 #ifdef HAVE_ZLIB_SUPPORT
219 { "gz", gzip_decompress_to_file, gzip_is_compressed },
220 #endif
221 #ifdef HAVE_LZMA_SUPPORT
222 { "xz", lzma_decompress_to_file, lzma_is_compressed },
223 #endif
224 { NULL, NULL, NULL },
227 static int is_supported_compression(const char *ext)
229 unsigned i;
231 for (i = 1; compressions[i].fmt; i++) {
232 if (!strcmp(ext, compressions[i].fmt))
233 return i;
235 return COMP_ID__NONE;
238 bool is_kernel_module(const char *pathname, int cpumode)
240 struct kmod_path m;
241 int mode = cpumode & PERF_RECORD_MISC_CPUMODE_MASK;
243 WARN_ONCE(mode != cpumode,
244 "Internal error: passing unmasked cpumode (%x) to is_kernel_module",
245 cpumode);
247 switch (mode) {
248 case PERF_RECORD_MISC_USER:
249 case PERF_RECORD_MISC_HYPERVISOR:
250 case PERF_RECORD_MISC_GUEST_USER:
251 return false;
252 /* Treat PERF_RECORD_MISC_CPUMODE_UNKNOWN as kernel */
253 default:
254 if (kmod_path__parse(&m, pathname)) {
255 pr_err("Failed to check whether %s is a kernel module or not. Assume it is.",
256 pathname);
257 return true;
261 return m.kmod;
264 bool dso__needs_decompress(struct dso *dso)
266 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
267 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
270 static int decompress_kmodule(struct dso *dso, const char *name,
271 char *pathname, size_t len)
273 char tmpbuf[] = KMOD_DECOMP_NAME;
274 int fd = -1;
276 if (!dso__needs_decompress(dso))
277 return -1;
279 if (dso->comp == COMP_ID__NONE)
280 return -1;
283 * We have proper compression id for DSO and yet the file
284 * behind the 'name' can still be plain uncompressed object.
286 * The reason is behind the logic we open the DSO object files,
287 * when we try all possible 'debug' objects until we find the
288 * data. So even if the DSO is represented by 'krava.xz' module,
289 * we can end up here opening ~/.debug/....23432432/debug' file
290 * which is not compressed.
292 * To keep this transparent, we detect this and return the file
293 * descriptor to the uncompressed file.
295 if (!compressions[dso->comp].is_compressed(name))
296 return open(name, O_RDONLY);
298 fd = mkstemp(tmpbuf);
299 if (fd < 0) {
300 dso->load_errno = errno;
301 return -1;
304 if (compressions[dso->comp].decompress(name, fd)) {
305 dso->load_errno = DSO_LOAD_ERRNO__DECOMPRESSION_FAILURE;
306 close(fd);
307 fd = -1;
310 if (!pathname || (fd < 0))
311 unlink(tmpbuf);
313 if (pathname && (fd >= 0))
314 strlcpy(pathname, tmpbuf, len);
316 return fd;
319 int dso__decompress_kmodule_fd(struct dso *dso, const char *name)
321 return decompress_kmodule(dso, name, NULL, 0);
324 int dso__decompress_kmodule_path(struct dso *dso, const char *name,
325 char *pathname, size_t len)
327 int fd = decompress_kmodule(dso, name, pathname, len);
329 close(fd);
330 return fd >= 0 ? 0 : -1;
334 * Parses kernel module specified in @path and updates
335 * @m argument like:
337 * @comp - true if @path contains supported compression suffix,
338 * false otherwise
339 * @kmod - true if @path contains '.ko' suffix in right position,
340 * false otherwise
341 * @name - if (@alloc_name && @kmod) is true, it contains strdup-ed base name
342 * of the kernel module without suffixes, otherwise strudup-ed
343 * base name of @path
344 * @ext - if (@alloc_ext && @comp) is true, it contains strdup-ed string
345 * the compression suffix
347 * Returns 0 if there's no strdup error, -ENOMEM otherwise.
349 int __kmod_path__parse(struct kmod_path *m, const char *path,
350 bool alloc_name)
352 const char *name = strrchr(path, '/');
353 const char *ext = strrchr(path, '.');
354 bool is_simple_name = false;
356 memset(m, 0x0, sizeof(*m));
357 name = name ? name + 1 : path;
360 * '.' is also a valid character for module name. For example:
361 * [aaa.bbb] is a valid module name. '[' should have higher
362 * priority than '.ko' suffix.
364 * The kernel names are from machine__mmap_name. Such
365 * name should belong to kernel itself, not kernel module.
367 if (name[0] == '[') {
368 is_simple_name = true;
369 if ((strncmp(name, "[kernel.kallsyms]", 17) == 0) ||
370 (strncmp(name, "[guest.kernel.kallsyms", 22) == 0) ||
371 (strncmp(name, "[vdso]", 6) == 0) ||
372 (strncmp(name, "[vdso32]", 8) == 0) ||
373 (strncmp(name, "[vdsox32]", 9) == 0) ||
374 (strncmp(name, "[vsyscall]", 10) == 0)) {
375 m->kmod = false;
377 } else
378 m->kmod = true;
381 /* No extension, just return name. */
382 if ((ext == NULL) || is_simple_name) {
383 if (alloc_name) {
384 m->name = strdup(name);
385 return m->name ? 0 : -ENOMEM;
387 return 0;
390 m->comp = is_supported_compression(ext + 1);
391 if (m->comp > COMP_ID__NONE)
392 ext -= 3;
394 /* Check .ko extension only if there's enough name left. */
395 if (ext > name)
396 m->kmod = !strncmp(ext, ".ko", 3);
398 if (alloc_name) {
399 if (m->kmod) {
400 if (asprintf(&m->name, "[%.*s]", (int) (ext - name), name) == -1)
401 return -ENOMEM;
402 } else {
403 if (asprintf(&m->name, "%s", name) == -1)
404 return -ENOMEM;
407 strxfrchar(m->name, '-', '_');
410 return 0;
413 void dso__set_module_info(struct dso *dso, struct kmod_path *m,
414 struct machine *machine)
416 if (machine__is_host(machine))
417 dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE;
418 else
419 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE;
421 /* _KMODULE_COMP should be next to _KMODULE */
422 if (m->kmod && m->comp) {
423 dso->symtab_type++;
424 dso->comp = m->comp;
427 dso__set_short_name(dso, strdup(m->name), true);
431 * Global list of open DSOs and the counter.
433 static LIST_HEAD(dso__data_open);
434 static long dso__data_open_cnt;
435 static pthread_mutex_t dso__data_open_lock = PTHREAD_MUTEX_INITIALIZER;
437 static void dso__list_add(struct dso *dso)
439 list_add_tail(&dso->data.open_entry, &dso__data_open);
440 dso__data_open_cnt++;
443 static void dso__list_del(struct dso *dso)
445 list_del(&dso->data.open_entry);
446 WARN_ONCE(dso__data_open_cnt <= 0,
447 "DSO data fd counter out of bounds.");
448 dso__data_open_cnt--;
451 static void close_first_dso(void);
453 static int do_open(char *name)
455 int fd;
456 char sbuf[STRERR_BUFSIZE];
458 do {
459 fd = open(name, O_RDONLY|O_CLOEXEC);
460 if (fd >= 0)
461 return fd;
463 pr_debug("dso open failed: %s\n",
464 str_error_r(errno, sbuf, sizeof(sbuf)));
465 if (!dso__data_open_cnt || errno != EMFILE)
466 break;
468 close_first_dso();
469 } while (1);
471 return -1;
474 static int __open_dso(struct dso *dso, struct machine *machine)
476 int fd = -EINVAL;
477 char *root_dir = (char *)"";
478 char *name = malloc(PATH_MAX);
479 bool decomp = false;
481 if (!name)
482 return -ENOMEM;
484 if (machine)
485 root_dir = machine->root_dir;
487 if (dso__read_binary_type_filename(dso, dso->binary_type,
488 root_dir, name, PATH_MAX))
489 goto out;
491 if (!is_regular_file(name))
492 goto out;
494 if (dso__needs_decompress(dso)) {
495 char newpath[KMOD_DECOMP_LEN];
496 size_t len = sizeof(newpath);
498 if (dso__decompress_kmodule_path(dso, name, newpath, len) < 0) {
499 fd = -dso->load_errno;
500 goto out;
503 decomp = true;
504 strcpy(name, newpath);
507 fd = do_open(name);
509 if (decomp)
510 unlink(name);
512 out:
513 free(name);
514 return fd;
517 static void check_data_close(void);
520 * dso_close - Open DSO data file
521 * @dso: dso object
523 * Open @dso's data file descriptor and updates
524 * list/count of open DSO objects.
526 static int open_dso(struct dso *dso, struct machine *machine)
528 int fd;
529 struct nscookie nsc;
531 if (dso->binary_type != DSO_BINARY_TYPE__BUILD_ID_CACHE)
532 nsinfo__mountns_enter(dso->nsinfo, &nsc);
533 fd = __open_dso(dso, machine);
534 if (dso->binary_type != DSO_BINARY_TYPE__BUILD_ID_CACHE)
535 nsinfo__mountns_exit(&nsc);
537 if (fd >= 0) {
538 dso__list_add(dso);
540 * Check if we crossed the allowed number
541 * of opened DSOs and close one if needed.
543 check_data_close();
546 return fd;
549 static void close_data_fd(struct dso *dso)
551 if (dso->data.fd >= 0) {
552 close(dso->data.fd);
553 dso->data.fd = -1;
554 dso->data.file_size = 0;
555 dso__list_del(dso);
560 * dso_close - Close DSO data file
561 * @dso: dso object
563 * Close @dso's data file descriptor and updates
564 * list/count of open DSO objects.
566 static void close_dso(struct dso *dso)
568 close_data_fd(dso);
571 static void close_first_dso(void)
573 struct dso *dso;
575 dso = list_first_entry(&dso__data_open, struct dso, data.open_entry);
576 close_dso(dso);
579 static rlim_t get_fd_limit(void)
581 struct rlimit l;
582 rlim_t limit = 0;
584 /* Allow half of the current open fd limit. */
585 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
586 if (l.rlim_cur == RLIM_INFINITY)
587 limit = l.rlim_cur;
588 else
589 limit = l.rlim_cur / 2;
590 } else {
591 pr_err("failed to get fd limit\n");
592 limit = 1;
595 return limit;
598 static rlim_t fd_limit;
601 * Used only by tests/dso-data.c to reset the environment
602 * for tests. I dont expect we should change this during
603 * standard runtime.
605 void reset_fd_limit(void)
607 fd_limit = 0;
610 static bool may_cache_fd(void)
612 if (!fd_limit)
613 fd_limit = get_fd_limit();
615 if (fd_limit == RLIM_INFINITY)
616 return true;
618 return fd_limit > (rlim_t) dso__data_open_cnt;
622 * Check and close LRU dso if we crossed allowed limit
623 * for opened dso file descriptors. The limit is half
624 * of the RLIMIT_NOFILE files opened.
626 static void check_data_close(void)
628 bool cache_fd = may_cache_fd();
630 if (!cache_fd)
631 close_first_dso();
635 * dso__data_close - Close DSO data file
636 * @dso: dso object
638 * External interface to close @dso's data file descriptor.
640 void dso__data_close(struct dso *dso)
642 pthread_mutex_lock(&dso__data_open_lock);
643 close_dso(dso);
644 pthread_mutex_unlock(&dso__data_open_lock);
647 static void try_to_open_dso(struct dso *dso, struct machine *machine)
649 enum dso_binary_type binary_type_data[] = {
650 DSO_BINARY_TYPE__BUILD_ID_CACHE,
651 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
652 DSO_BINARY_TYPE__NOT_FOUND,
654 int i = 0;
656 if (dso->data.fd >= 0)
657 return;
659 if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) {
660 dso->data.fd = open_dso(dso, machine);
661 goto out;
664 do {
665 dso->binary_type = binary_type_data[i++];
667 dso->data.fd = open_dso(dso, machine);
668 if (dso->data.fd >= 0)
669 goto out;
671 } while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND);
672 out:
673 if (dso->data.fd >= 0)
674 dso->data.status = DSO_DATA_STATUS_OK;
675 else
676 dso->data.status = DSO_DATA_STATUS_ERROR;
680 * dso__data_get_fd - Get dso's data file descriptor
681 * @dso: dso object
682 * @machine: machine object
684 * External interface to find dso's file, open it and
685 * returns file descriptor. It should be paired with
686 * dso__data_put_fd() if it returns non-negative value.
688 int dso__data_get_fd(struct dso *dso, struct machine *machine)
690 if (dso->data.status == DSO_DATA_STATUS_ERROR)
691 return -1;
693 if (pthread_mutex_lock(&dso__data_open_lock) < 0)
694 return -1;
696 try_to_open_dso(dso, machine);
698 if (dso->data.fd < 0)
699 pthread_mutex_unlock(&dso__data_open_lock);
701 return dso->data.fd;
704 void dso__data_put_fd(struct dso *dso __maybe_unused)
706 pthread_mutex_unlock(&dso__data_open_lock);
709 bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by)
711 u32 flag = 1 << by;
713 if (dso->data.status_seen & flag)
714 return true;
716 dso->data.status_seen |= flag;
718 return false;
721 static void
722 dso_cache__free(struct dso *dso)
724 struct rb_root *root = &dso->data.cache;
725 struct rb_node *next = rb_first(root);
727 pthread_mutex_lock(&dso->lock);
728 while (next) {
729 struct dso_cache *cache;
731 cache = rb_entry(next, struct dso_cache, rb_node);
732 next = rb_next(&cache->rb_node);
733 rb_erase(&cache->rb_node, root);
734 free(cache);
736 pthread_mutex_unlock(&dso->lock);
739 static struct dso_cache *dso_cache__find(struct dso *dso, u64 offset)
741 const struct rb_root *root = &dso->data.cache;
742 struct rb_node * const *p = &root->rb_node;
743 const struct rb_node *parent = NULL;
744 struct dso_cache *cache;
746 while (*p != NULL) {
747 u64 end;
749 parent = *p;
750 cache = rb_entry(parent, struct dso_cache, rb_node);
751 end = cache->offset + DSO__DATA_CACHE_SIZE;
753 if (offset < cache->offset)
754 p = &(*p)->rb_left;
755 else if (offset >= end)
756 p = &(*p)->rb_right;
757 else
758 return cache;
761 return NULL;
764 static struct dso_cache *
765 dso_cache__insert(struct dso *dso, struct dso_cache *new)
767 struct rb_root *root = &dso->data.cache;
768 struct rb_node **p = &root->rb_node;
769 struct rb_node *parent = NULL;
770 struct dso_cache *cache;
771 u64 offset = new->offset;
773 pthread_mutex_lock(&dso->lock);
774 while (*p != NULL) {
775 u64 end;
777 parent = *p;
778 cache = rb_entry(parent, struct dso_cache, rb_node);
779 end = cache->offset + DSO__DATA_CACHE_SIZE;
781 if (offset < cache->offset)
782 p = &(*p)->rb_left;
783 else if (offset >= end)
784 p = &(*p)->rb_right;
785 else
786 goto out;
789 rb_link_node(&new->rb_node, parent, p);
790 rb_insert_color(&new->rb_node, root);
792 cache = NULL;
793 out:
794 pthread_mutex_unlock(&dso->lock);
795 return cache;
798 static ssize_t
799 dso_cache__memcpy(struct dso_cache *cache, u64 offset,
800 u8 *data, u64 size)
802 u64 cache_offset = offset - cache->offset;
803 u64 cache_size = min(cache->size - cache_offset, size);
805 memcpy(data, cache->data + cache_offset, cache_size);
806 return cache_size;
809 static ssize_t
810 dso_cache__read(struct dso *dso, struct machine *machine,
811 u64 offset, u8 *data, ssize_t size)
813 struct dso_cache *cache;
814 struct dso_cache *old;
815 ssize_t ret;
817 do {
818 u64 cache_offset;
820 cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
821 if (!cache)
822 return -ENOMEM;
824 pthread_mutex_lock(&dso__data_open_lock);
827 * dso->data.fd might be closed if other thread opened another
828 * file (dso) due to open file limit (RLIMIT_NOFILE).
830 try_to_open_dso(dso, machine);
832 if (dso->data.fd < 0) {
833 ret = -errno;
834 dso->data.status = DSO_DATA_STATUS_ERROR;
835 break;
838 cache_offset = offset & DSO__DATA_CACHE_MASK;
840 ret = pread(dso->data.fd, cache->data, DSO__DATA_CACHE_SIZE, cache_offset);
841 if (ret <= 0)
842 break;
844 cache->offset = cache_offset;
845 cache->size = ret;
846 } while (0);
848 pthread_mutex_unlock(&dso__data_open_lock);
850 if (ret > 0) {
851 old = dso_cache__insert(dso, cache);
852 if (old) {
853 /* we lose the race */
854 free(cache);
855 cache = old;
858 ret = dso_cache__memcpy(cache, offset, data, size);
861 if (ret <= 0)
862 free(cache);
864 return ret;
867 static ssize_t dso_cache_read(struct dso *dso, struct machine *machine,
868 u64 offset, u8 *data, ssize_t size)
870 struct dso_cache *cache;
872 cache = dso_cache__find(dso, offset);
873 if (cache)
874 return dso_cache__memcpy(cache, offset, data, size);
875 else
876 return dso_cache__read(dso, machine, offset, data, size);
880 * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
881 * in the rb_tree. Any read to already cached data is served
882 * by cached data.
884 static ssize_t cached_read(struct dso *dso, struct machine *machine,
885 u64 offset, u8 *data, ssize_t size)
887 ssize_t r = 0;
888 u8 *p = data;
890 do {
891 ssize_t ret;
893 ret = dso_cache_read(dso, machine, offset, p, size);
894 if (ret < 0)
895 return ret;
897 /* Reached EOF, return what we have. */
898 if (!ret)
899 break;
901 BUG_ON(ret > size);
903 r += ret;
904 p += ret;
905 offset += ret;
906 size -= ret;
908 } while (size);
910 return r;
913 static int data_file_size(struct dso *dso, struct machine *machine)
915 int ret = 0;
916 struct stat st;
917 char sbuf[STRERR_BUFSIZE];
919 if (dso->data.file_size)
920 return 0;
922 if (dso->data.status == DSO_DATA_STATUS_ERROR)
923 return -1;
925 pthread_mutex_lock(&dso__data_open_lock);
928 * dso->data.fd might be closed if other thread opened another
929 * file (dso) due to open file limit (RLIMIT_NOFILE).
931 try_to_open_dso(dso, machine);
933 if (dso->data.fd < 0) {
934 ret = -errno;
935 dso->data.status = DSO_DATA_STATUS_ERROR;
936 goto out;
939 if (fstat(dso->data.fd, &st) < 0) {
940 ret = -errno;
941 pr_err("dso cache fstat failed: %s\n",
942 str_error_r(errno, sbuf, sizeof(sbuf)));
943 dso->data.status = DSO_DATA_STATUS_ERROR;
944 goto out;
946 dso->data.file_size = st.st_size;
948 out:
949 pthread_mutex_unlock(&dso__data_open_lock);
950 return ret;
954 * dso__data_size - Return dso data size
955 * @dso: dso object
956 * @machine: machine object
958 * Return: dso data size
960 off_t dso__data_size(struct dso *dso, struct machine *machine)
962 if (data_file_size(dso, machine))
963 return -1;
965 /* For now just estimate dso data size is close to file size */
966 return dso->data.file_size;
969 static ssize_t data_read_offset(struct dso *dso, struct machine *machine,
970 u64 offset, u8 *data, ssize_t size)
972 if (data_file_size(dso, machine))
973 return -1;
975 /* Check the offset sanity. */
976 if (offset > dso->data.file_size)
977 return -1;
979 if (offset + size < offset)
980 return -1;
982 return cached_read(dso, machine, offset, data, size);
986 * dso__data_read_offset - Read data from dso file offset
987 * @dso: dso object
988 * @machine: machine object
989 * @offset: file offset
990 * @data: buffer to store data
991 * @size: size of the @data buffer
993 * External interface to read data from dso file offset. Open
994 * dso data file and use cached_read to get the data.
996 ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
997 u64 offset, u8 *data, ssize_t size)
999 if (dso->data.status == DSO_DATA_STATUS_ERROR)
1000 return -1;
1002 return data_read_offset(dso, machine, offset, data, size);
1006 * dso__data_read_addr - Read data from dso address
1007 * @dso: dso object
1008 * @machine: machine object
1009 * @add: virtual memory address
1010 * @data: buffer to store data
1011 * @size: size of the @data buffer
1013 * External interface to read data from dso address.
1015 ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
1016 struct machine *machine, u64 addr,
1017 u8 *data, ssize_t size)
1019 u64 offset = map->map_ip(map, addr);
1020 return dso__data_read_offset(dso, machine, offset, data, size);
1023 struct map *dso__new_map(const char *name)
1025 struct map *map = NULL;
1026 struct dso *dso = dso__new(name);
1028 if (dso)
1029 map = map__new2(0, dso);
1031 return map;
1034 struct dso *machine__findnew_kernel(struct machine *machine, const char *name,
1035 const char *short_name, int dso_type)
1038 * The kernel dso could be created by build_id processing.
1040 struct dso *dso = machine__findnew_dso(machine, name);
1043 * We need to run this in all cases, since during the build_id
1044 * processing we had no idea this was the kernel dso.
1046 if (dso != NULL) {
1047 dso__set_short_name(dso, short_name, false);
1048 dso->kernel = dso_type;
1051 return dso;
1055 * Find a matching entry and/or link current entry to RB tree.
1056 * Either one of the dso or name parameter must be non-NULL or the
1057 * function will not work.
1059 static struct dso *__dso__findlink_by_longname(struct rb_root *root,
1060 struct dso *dso, const char *name)
1062 struct rb_node **p = &root->rb_node;
1063 struct rb_node *parent = NULL;
1065 if (!name)
1066 name = dso->long_name;
1068 * Find node with the matching name
1070 while (*p) {
1071 struct dso *this = rb_entry(*p, struct dso, rb_node);
1072 int rc = strcmp(name, this->long_name);
1074 parent = *p;
1075 if (rc == 0) {
1077 * In case the new DSO is a duplicate of an existing
1078 * one, print a one-time warning & put the new entry
1079 * at the end of the list of duplicates.
1081 if (!dso || (dso == this))
1082 return this; /* Find matching dso */
1084 * The core kernel DSOs may have duplicated long name.
1085 * In this case, the short name should be different.
1086 * Comparing the short names to differentiate the DSOs.
1088 rc = strcmp(dso->short_name, this->short_name);
1089 if (rc == 0) {
1090 pr_err("Duplicated dso name: %s\n", name);
1091 return NULL;
1094 if (rc < 0)
1095 p = &parent->rb_left;
1096 else
1097 p = &parent->rb_right;
1099 if (dso) {
1100 /* Add new node and rebalance tree */
1101 rb_link_node(&dso->rb_node, parent, p);
1102 rb_insert_color(&dso->rb_node, root);
1103 dso->root = root;
1105 return NULL;
1108 static inline struct dso *__dso__find_by_longname(struct rb_root *root,
1109 const char *name)
1111 return __dso__findlink_by_longname(root, NULL, name);
1114 void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
1116 struct rb_root *root = dso->root;
1118 if (name == NULL)
1119 return;
1121 if (dso->long_name_allocated)
1122 free((char *)dso->long_name);
1124 if (root) {
1125 rb_erase(&dso->rb_node, root);
1127 * __dso__findlink_by_longname() isn't guaranteed to add it
1128 * back, so a clean removal is required here.
1130 RB_CLEAR_NODE(&dso->rb_node);
1131 dso->root = NULL;
1134 dso->long_name = name;
1135 dso->long_name_len = strlen(name);
1136 dso->long_name_allocated = name_allocated;
1138 if (root)
1139 __dso__findlink_by_longname(root, dso, NULL);
1142 void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
1144 if (name == NULL)
1145 return;
1147 if (dso->short_name_allocated)
1148 free((char *)dso->short_name);
1150 dso->short_name = name;
1151 dso->short_name_len = strlen(name);
1152 dso->short_name_allocated = name_allocated;
1155 static void dso__set_basename(struct dso *dso)
1158 * basename() may modify path buffer, so we must pass
1159 * a copy.
1161 char *base, *lname = strdup(dso->long_name);
1163 if (!lname)
1164 return;
1167 * basename() may return a pointer to internal
1168 * storage which is reused in subsequent calls
1169 * so copy the result.
1171 base = strdup(basename(lname));
1173 free(lname);
1175 if (!base)
1176 return;
1178 dso__set_short_name(dso, base, true);
1181 int dso__name_len(const struct dso *dso)
1183 if (!dso)
1184 return strlen("[unknown]");
1185 if (verbose > 0)
1186 return dso->long_name_len;
1188 return dso->short_name_len;
1191 bool dso__loaded(const struct dso *dso)
1193 return dso->loaded;
1196 bool dso__sorted_by_name(const struct dso *dso)
1198 return dso->sorted_by_name;
1201 void dso__set_sorted_by_name(struct dso *dso)
1203 dso->sorted_by_name = true;
1206 struct dso *dso__new(const char *name)
1208 struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
1210 if (dso != NULL) {
1211 strcpy(dso->name, name);
1212 dso__set_long_name(dso, dso->name, false);
1213 dso__set_short_name(dso, dso->name, false);
1214 dso->symbols = dso->symbol_names = RB_ROOT;
1215 dso->data.cache = RB_ROOT;
1216 dso->inlined_nodes = RB_ROOT;
1217 dso->srclines = RB_ROOT;
1218 dso->data.fd = -1;
1219 dso->data.status = DSO_DATA_STATUS_UNKNOWN;
1220 dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
1221 dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
1222 dso->is_64_bit = (sizeof(void *) == 8);
1223 dso->loaded = 0;
1224 dso->rel = 0;
1225 dso->sorted_by_name = 0;
1226 dso->has_build_id = 0;
1227 dso->has_srcline = 1;
1228 dso->a2l_fails = 1;
1229 dso->kernel = DSO_TYPE_USER;
1230 dso->needs_swap = DSO_SWAP__UNSET;
1231 dso->comp = COMP_ID__NONE;
1232 RB_CLEAR_NODE(&dso->rb_node);
1233 dso->root = NULL;
1234 INIT_LIST_HEAD(&dso->node);
1235 INIT_LIST_HEAD(&dso->data.open_entry);
1236 pthread_mutex_init(&dso->lock, NULL);
1237 refcount_set(&dso->refcnt, 1);
1240 return dso;
1243 void dso__delete(struct dso *dso)
1245 if (!RB_EMPTY_NODE(&dso->rb_node))
1246 pr_err("DSO %s is still in rbtree when being deleted!\n",
1247 dso->long_name);
1249 /* free inlines first, as they reference symbols */
1250 inlines__tree_delete(&dso->inlined_nodes);
1251 srcline__tree_delete(&dso->srclines);
1252 symbols__delete(&dso->symbols);
1254 if (dso->short_name_allocated) {
1255 zfree((char **)&dso->short_name);
1256 dso->short_name_allocated = false;
1259 if (dso->long_name_allocated) {
1260 zfree((char **)&dso->long_name);
1261 dso->long_name_allocated = false;
1264 dso__data_close(dso);
1265 auxtrace_cache__free(dso->auxtrace_cache);
1266 dso_cache__free(dso);
1267 dso__free_a2l(dso);
1268 zfree(&dso->symsrc_filename);
1269 nsinfo__zput(dso->nsinfo);
1270 pthread_mutex_destroy(&dso->lock);
1271 free(dso);
1274 struct dso *dso__get(struct dso *dso)
1276 if (dso)
1277 refcount_inc(&dso->refcnt);
1278 return dso;
1281 void dso__put(struct dso *dso)
1283 if (dso && refcount_dec_and_test(&dso->refcnt))
1284 dso__delete(dso);
1287 void dso__set_build_id(struct dso *dso, void *build_id)
1289 memcpy(dso->build_id, build_id, sizeof(dso->build_id));
1290 dso->has_build_id = 1;
1293 bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
1295 return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
1298 void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
1300 char path[PATH_MAX];
1302 if (machine__is_default_guest(machine))
1303 return;
1304 sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
1305 if (sysfs__read_build_id(path, dso->build_id,
1306 sizeof(dso->build_id)) == 0)
1307 dso->has_build_id = true;
1310 int dso__kernel_module_get_build_id(struct dso *dso,
1311 const char *root_dir)
1313 char filename[PATH_MAX];
1315 * kernel module short names are of the form "[module]" and
1316 * we need just "module" here.
1318 const char *name = dso->short_name + 1;
1320 snprintf(filename, sizeof(filename),
1321 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1322 root_dir, (int)strlen(name) - 1, name);
1324 if (sysfs__read_build_id(filename, dso->build_id,
1325 sizeof(dso->build_id)) == 0)
1326 dso->has_build_id = true;
1328 return 0;
1331 bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
1333 bool have_build_id = false;
1334 struct dso *pos;
1335 struct nscookie nsc;
1337 list_for_each_entry(pos, head, node) {
1338 if (with_hits && !pos->hit && !dso__is_vdso(pos))
1339 continue;
1340 if (pos->has_build_id) {
1341 have_build_id = true;
1342 continue;
1344 nsinfo__mountns_enter(pos->nsinfo, &nsc);
1345 if (filename__read_build_id(pos->long_name, pos->build_id,
1346 sizeof(pos->build_id)) > 0) {
1347 have_build_id = true;
1348 pos->has_build_id = true;
1350 nsinfo__mountns_exit(&nsc);
1353 return have_build_id;
1356 void __dsos__add(struct dsos *dsos, struct dso *dso)
1358 list_add_tail(&dso->node, &dsos->head);
1359 __dso__findlink_by_longname(&dsos->root, dso, NULL);
1361 * It is now in the linked list, grab a reference, then garbage collect
1362 * this when needing memory, by looking at LRU dso instances in the
1363 * list with atomic_read(&dso->refcnt) == 1, i.e. no references
1364 * anywhere besides the one for the list, do, under a lock for the
1365 * list: remove it from the list, then a dso__put(), that probably will
1366 * be the last and will then call dso__delete(), end of life.
1368 * That, or at the end of the 'struct machine' lifetime, when all
1369 * 'struct dso' instances will be removed from the list, in
1370 * dsos__exit(), if they have no other reference from some other data
1371 * structure.
1373 * E.g.: after processing a 'perf.data' file and storing references
1374 * to objects instantiated while processing events, we will have
1375 * references to the 'thread', 'map', 'dso' structs all from 'struct
1376 * hist_entry' instances, but we may not need anything not referenced,
1377 * so we might as well call machines__exit()/machines__delete() and
1378 * garbage collect it.
1380 dso__get(dso);
1383 void dsos__add(struct dsos *dsos, struct dso *dso)
1385 down_write(&dsos->lock);
1386 __dsos__add(dsos, dso);
1387 up_write(&dsos->lock);
1390 struct dso *__dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
1392 struct dso *pos;
1394 if (cmp_short) {
1395 list_for_each_entry(pos, &dsos->head, node)
1396 if (strcmp(pos->short_name, name) == 0)
1397 return pos;
1398 return NULL;
1400 return __dso__find_by_longname(&dsos->root, name);
1403 struct dso *dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
1405 struct dso *dso;
1406 down_read(&dsos->lock);
1407 dso = __dsos__find(dsos, name, cmp_short);
1408 up_read(&dsos->lock);
1409 return dso;
1412 struct dso *__dsos__addnew(struct dsos *dsos, const char *name)
1414 struct dso *dso = dso__new(name);
1416 if (dso != NULL) {
1417 __dsos__add(dsos, dso);
1418 dso__set_basename(dso);
1419 /* Put dso here because __dsos_add already got it */
1420 dso__put(dso);
1422 return dso;
1425 struct dso *__dsos__findnew(struct dsos *dsos, const char *name)
1427 struct dso *dso = __dsos__find(dsos, name, false);
1429 return dso ? dso : __dsos__addnew(dsos, name);
1432 struct dso *dsos__findnew(struct dsos *dsos, const char *name)
1434 struct dso *dso;
1435 down_write(&dsos->lock);
1436 dso = dso__get(__dsos__findnew(dsos, name));
1437 up_write(&dsos->lock);
1438 return dso;
1441 size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
1442 bool (skip)(struct dso *dso, int parm), int parm)
1444 struct dso *pos;
1445 size_t ret = 0;
1447 list_for_each_entry(pos, head, node) {
1448 if (skip && skip(pos, parm))
1449 continue;
1450 ret += dso__fprintf_buildid(pos, fp);
1451 ret += fprintf(fp, " %s\n", pos->long_name);
1453 return ret;
1456 size_t __dsos__fprintf(struct list_head *head, FILE *fp)
1458 struct dso *pos;
1459 size_t ret = 0;
1461 list_for_each_entry(pos, head, node) {
1462 ret += dso__fprintf(pos, fp);
1465 return ret;
1468 size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
1470 char sbuild_id[SBUILD_ID_SIZE];
1472 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1473 return fprintf(fp, "%s", sbuild_id);
1476 size_t dso__fprintf(struct dso *dso, FILE *fp)
1478 struct rb_node *nd;
1479 size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
1481 if (dso->short_name != dso->long_name)
1482 ret += fprintf(fp, "%s, ", dso->long_name);
1483 ret += fprintf(fp, "%sloaded, ", dso__loaded(dso) ? "" : "NOT ");
1484 ret += dso__fprintf_buildid(dso, fp);
1485 ret += fprintf(fp, ")\n");
1486 for (nd = rb_first(&dso->symbols); nd; nd = rb_next(nd)) {
1487 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
1488 ret += symbol__fprintf(pos, fp);
1491 return ret;
1494 enum dso_type dso__type(struct dso *dso, struct machine *machine)
1496 int fd;
1497 enum dso_type type = DSO__TYPE_UNKNOWN;
1499 fd = dso__data_get_fd(dso, machine);
1500 if (fd >= 0) {
1501 type = dso__type_fd(fd);
1502 dso__data_put_fd(dso);
1505 return type;
1508 int dso__strerror_load(struct dso *dso, char *buf, size_t buflen)
1510 int idx, errnum = dso->load_errno;
1512 * This must have a same ordering as the enum dso_load_errno.
1514 static const char *dso_load__error_str[] = {
1515 "Internal tools/perf/ library error",
1516 "Invalid ELF file",
1517 "Can not read build id",
1518 "Mismatching build id",
1519 "Decompression failure",
1522 BUG_ON(buflen == 0);
1524 if (errnum >= 0) {
1525 const char *err = str_error_r(errnum, buf, buflen);
1527 if (err != buf)
1528 scnprintf(buf, buflen, "%s", err);
1530 return 0;
1533 if (errnum < __DSO_LOAD_ERRNO__START || errnum >= __DSO_LOAD_ERRNO__END)
1534 return -1;
1536 idx = errnum - __DSO_LOAD_ERRNO__START;
1537 scnprintf(buf, buflen, "%s", dso_load__error_str[idx]);
1538 return 0;