1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/kernel.h>
4 #include <linux/string.h>
6 #include <sys/resource.h>
13 #include <bpf/libbpf.h>
14 #include "bpf-event.h"
16 #include "namespaces.h"
29 static const char * const debuglink_paths[] = {
36 char dso__symtab_origin(const struct dso *dso)
38 static const char origin[] = {
39 [DSO_BINARY_TYPE__KALLSYMS] = 'k',
40 [DSO_BINARY_TYPE__VMLINUX] = 'v',
41 [DSO_BINARY_TYPE__JAVA_JIT] = 'j',
42 [DSO_BINARY_TYPE__DEBUGLINK] = 'l',
43 [DSO_BINARY_TYPE__BUILD_ID_CACHE] = 'B',
44 [DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO] = 'D',
45 [DSO_BINARY_TYPE__FEDORA_DEBUGINFO] = 'f',
46 [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO] = 'u',
47 [DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO] = 'o',
48 [DSO_BINARY_TYPE__BUILDID_DEBUGINFO] = 'b',
49 [DSO_BINARY_TYPE__SYSTEM_PATH_DSO] = 'd',
50 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE] = 'K',
51 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP] = 'm',
52 [DSO_BINARY_TYPE__GUEST_KALLSYMS] = 'g',
53 [DSO_BINARY_TYPE__GUEST_KMODULE] = 'G',
54 [DSO_BINARY_TYPE__GUEST_KMODULE_COMP] = 'M',
55 [DSO_BINARY_TYPE__GUEST_VMLINUX] = 'V',
58 if (dso == NULL || dso->symtab_type == DSO_BINARY_TYPE__NOT_FOUND)
60 return origin[dso->symtab_type];
63 int dso__read_binary_type_filename(const struct dso *dso,
64 enum dso_binary_type type,
65 char *root_dir, char *filename, size_t size)
67 char build_id_hex[SBUILD_ID_SIZE];
72 case DSO_BINARY_TYPE__DEBUGLINK:
74 const char *last_slash;
75 char dso_dir[PATH_MAX];
76 char symfile[PATH_MAX];
79 len = __symbol__join_symfs(filename, size, dso->long_name);
80 last_slash = filename + len;
81 while (last_slash != filename && *last_slash != '/')
84 strncpy(dso_dir, filename, last_slash - filename);
85 dso_dir[last_slash-filename] = '\0';
87 if (!is_regular_file(filename)) {
92 ret = filename__read_debuglink(filename, symfile, PATH_MAX);
96 /* Check predefined locations where debug file might reside */
98 for (i = 0; i < ARRAY_SIZE(debuglink_paths); i++) {
99 snprintf(filename, size,
100 debuglink_paths[i], dso_dir, symfile);
101 if (is_regular_file(filename)) {
109 case DSO_BINARY_TYPE__BUILD_ID_CACHE:
110 if (dso__build_id_filename(dso, filename, size, false) == NULL)
114 case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO:
115 if (dso__build_id_filename(dso, filename, size, true) == NULL)
119 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
120 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
121 snprintf(filename + len, size - len, "%s.debug", dso->long_name);
124 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
125 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
126 snprintf(filename + len, size - len, "%s", dso->long_name);
129 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
131 const char *last_slash;
134 last_slash = dso->long_name + dso->long_name_len;
135 while (last_slash != dso->long_name && *last_slash != '/')
138 len = __symbol__join_symfs(filename, size, "");
139 dir_size = last_slash - dso->long_name + 2;
140 if (dir_size > (size - len)) {
144 len += scnprintf(filename + len, dir_size, "%s", dso->long_name);
145 len += scnprintf(filename + len , size - len, ".debug%s",
150 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
151 if (!dso->has_build_id) {
156 build_id__sprintf(dso->build_id,
157 sizeof(dso->build_id),
159 len = __symbol__join_symfs(filename, size, "/usr/lib/debug/.build-id/");
160 snprintf(filename + len, size - len, "%.2s/%s.debug",
161 build_id_hex, build_id_hex + 2);
164 case DSO_BINARY_TYPE__VMLINUX:
165 case DSO_BINARY_TYPE__GUEST_VMLINUX:
166 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
167 __symbol__join_symfs(filename, size, dso->long_name);
170 case DSO_BINARY_TYPE__GUEST_KMODULE:
171 case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
172 path__join3(filename, size, symbol_conf.symfs,
173 root_dir, dso->long_name);
176 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
177 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
178 __symbol__join_symfs(filename, size, dso->long_name);
181 case DSO_BINARY_TYPE__KCORE:
182 case DSO_BINARY_TYPE__GUEST_KCORE:
183 snprintf(filename, size, "%s", dso->long_name);
187 case DSO_BINARY_TYPE__KALLSYMS:
188 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
189 case DSO_BINARY_TYPE__JAVA_JIT:
190 case DSO_BINARY_TYPE__BPF_PROG_INFO:
191 case DSO_BINARY_TYPE__NOT_FOUND:
203 static const struct {
205 int (*decompress)(const char *input, int output);
206 bool (*is_compressed)(const char *input);
208 [COMP_ID__NONE] = { .fmt = NULL, },
209 #ifdef HAVE_ZLIB_SUPPORT
210 { "gz", gzip_decompress_to_file, gzip_is_compressed },
212 #ifdef HAVE_LZMA_SUPPORT
213 { "xz", lzma_decompress_to_file, lzma_is_compressed },
215 { NULL, NULL, NULL },
218 static int is_supported_compression(const char *ext)
222 for (i = 1; compressions[i].fmt; i++) {
223 if (!strcmp(ext, compressions[i].fmt))
226 return COMP_ID__NONE;
229 bool is_kernel_module(const char *pathname, int cpumode)
232 int mode = cpumode & PERF_RECORD_MISC_CPUMODE_MASK;
234 WARN_ONCE(mode != cpumode,
235 "Internal error: passing unmasked cpumode (%x) to is_kernel_module",
239 case PERF_RECORD_MISC_USER:
240 case PERF_RECORD_MISC_HYPERVISOR:
241 case PERF_RECORD_MISC_GUEST_USER:
243 /* Treat PERF_RECORD_MISC_CPUMODE_UNKNOWN as kernel */
245 if (kmod_path__parse(&m, pathname)) {
246 pr_err("Failed to check whether %s is a kernel module or not. Assume it is.",
255 bool dso__needs_decompress(struct dso *dso)
257 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
258 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
261 static int decompress_kmodule(struct dso *dso, const char *name,
262 char *pathname, size_t len)
264 char tmpbuf[] = KMOD_DECOMP_NAME;
267 if (!dso__needs_decompress(dso))
270 if (dso->comp == COMP_ID__NONE)
274 * We have proper compression id for DSO and yet the file
275 * behind the 'name' can still be plain uncompressed object.
277 * The reason is behind the logic we open the DSO object files,
278 * when we try all possible 'debug' objects until we find the
279 * data. So even if the DSO is represented by 'krava.xz' module,
280 * we can end up here opening ~/.debug/....23432432/debug' file
281 * which is not compressed.
283 * To keep this transparent, we detect this and return the file
284 * descriptor to the uncompressed file.
286 if (!compressions[dso->comp].is_compressed(name))
287 return open(name, O_RDONLY);
289 fd = mkstemp(tmpbuf);
291 dso->load_errno = errno;
295 if (compressions[dso->comp].decompress(name, fd)) {
296 dso->load_errno = DSO_LOAD_ERRNO__DECOMPRESSION_FAILURE;
301 if (!pathname || (fd < 0))
304 if (pathname && (fd >= 0))
305 strlcpy(pathname, tmpbuf, len);
310 int dso__decompress_kmodule_fd(struct dso *dso, const char *name)
312 return decompress_kmodule(dso, name, NULL, 0);
315 int dso__decompress_kmodule_path(struct dso *dso, const char *name,
316 char *pathname, size_t len)
318 int fd = decompress_kmodule(dso, name, pathname, len);
321 return fd >= 0 ? 0 : -1;
325 * Parses kernel module specified in @path and updates
328 * @comp - true if @path contains supported compression suffix,
330 * @kmod - true if @path contains '.ko' suffix in right position,
332 * @name - if (@alloc_name && @kmod) is true, it contains strdup-ed base name
333 * of the kernel module without suffixes, otherwise strudup-ed
335 * @ext - if (@alloc_ext && @comp) is true, it contains strdup-ed string
336 * the compression suffix
338 * Returns 0 if there's no strdup error, -ENOMEM otherwise.
340 int __kmod_path__parse(struct kmod_path *m, const char *path,
343 const char *name = strrchr(path, '/');
344 const char *ext = strrchr(path, '.');
345 bool is_simple_name = false;
347 memset(m, 0x0, sizeof(*m));
348 name = name ? name + 1 : path;
351 * '.' is also a valid character for module name. For example:
352 * [aaa.bbb] is a valid module name. '[' should have higher
353 * priority than '.ko' suffix.
355 * The kernel names are from machine__mmap_name. Such
356 * name should belong to kernel itself, not kernel module.
358 if (name[0] == '[') {
359 is_simple_name = true;
360 if ((strncmp(name, "[kernel.kallsyms]", 17) == 0) ||
361 (strncmp(name, "[guest.kernel.kallsyms", 22) == 0) ||
362 (strncmp(name, "[vdso]", 6) == 0) ||
363 (strncmp(name, "[vdso32]", 8) == 0) ||
364 (strncmp(name, "[vdsox32]", 9) == 0) ||
365 (strncmp(name, "[vsyscall]", 10) == 0)) {
372 /* No extension, just return name. */
373 if ((ext == NULL) || is_simple_name) {
375 m->name = strdup(name);
376 return m->name ? 0 : -ENOMEM;
381 m->comp = is_supported_compression(ext + 1);
382 if (m->comp > COMP_ID__NONE)
385 /* Check .ko extension only if there's enough name left. */
387 m->kmod = !strncmp(ext, ".ko", 3);
391 if (asprintf(&m->name, "[%.*s]", (int) (ext - name), name) == -1)
394 if (asprintf(&m->name, "%s", name) == -1)
398 strreplace(m->name, '-', '_');
404 void dso__set_module_info(struct dso *dso, struct kmod_path *m,
405 struct machine *machine)
407 if (machine__is_host(machine))
408 dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE;
410 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE;
412 /* _KMODULE_COMP should be next to _KMODULE */
413 if (m->kmod && m->comp) {
418 dso__set_short_name(dso, strdup(m->name), true);
422 * Global list of open DSOs and the counter.
424 static LIST_HEAD(dso__data_open);
425 static long dso__data_open_cnt;
426 static pthread_mutex_t dso__data_open_lock = PTHREAD_MUTEX_INITIALIZER;
428 static void dso__list_add(struct dso *dso)
430 list_add_tail(&dso->data.open_entry, &dso__data_open);
431 dso__data_open_cnt++;
434 static void dso__list_del(struct dso *dso)
436 list_del(&dso->data.open_entry);
437 WARN_ONCE(dso__data_open_cnt <= 0,
438 "DSO data fd counter out of bounds.");
439 dso__data_open_cnt--;
442 static void close_first_dso(void);
444 static int do_open(char *name)
447 char sbuf[STRERR_BUFSIZE];
450 fd = open(name, O_RDONLY|O_CLOEXEC);
454 pr_debug("dso open failed: %s\n",
455 str_error_r(errno, sbuf, sizeof(sbuf)));
456 if (!dso__data_open_cnt || errno != EMFILE)
465 static int __open_dso(struct dso *dso, struct machine *machine)
468 char *root_dir = (char *)"";
469 char *name = malloc(PATH_MAX);
476 root_dir = machine->root_dir;
478 if (dso__read_binary_type_filename(dso, dso->binary_type,
479 root_dir, name, PATH_MAX))
482 if (!is_regular_file(name))
485 if (dso__needs_decompress(dso)) {
486 char newpath[KMOD_DECOMP_LEN];
487 size_t len = sizeof(newpath);
489 if (dso__decompress_kmodule_path(dso, name, newpath, len) < 0) {
490 fd = -dso->load_errno;
495 strcpy(name, newpath);
508 static void check_data_close(void);
511 * dso_close - Open DSO data file
514 * Open @dso's data file descriptor and updates
515 * list/count of open DSO objects.
517 static int open_dso(struct dso *dso, struct machine *machine)
522 if (dso->binary_type != DSO_BINARY_TYPE__BUILD_ID_CACHE)
523 nsinfo__mountns_enter(dso->nsinfo, &nsc);
524 fd = __open_dso(dso, machine);
525 if (dso->binary_type != DSO_BINARY_TYPE__BUILD_ID_CACHE)
526 nsinfo__mountns_exit(&nsc);
531 * Check if we crossed the allowed number
532 * of opened DSOs and close one if needed.
540 static void close_data_fd(struct dso *dso)
542 if (dso->data.fd >= 0) {
545 dso->data.file_size = 0;
551 * dso_close - Close DSO data file
554 * Close @dso's data file descriptor and updates
555 * list/count of open DSO objects.
557 static void close_dso(struct dso *dso)
562 static void close_first_dso(void)
566 dso = list_first_entry(&dso__data_open, struct dso, data.open_entry);
570 static rlim_t get_fd_limit(void)
575 /* Allow half of the current open fd limit. */
576 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
577 if (l.rlim_cur == RLIM_INFINITY)
580 limit = l.rlim_cur / 2;
582 pr_err("failed to get fd limit\n");
589 static rlim_t fd_limit;
592 * Used only by tests/dso-data.c to reset the environment
593 * for tests. I dont expect we should change this during
596 void reset_fd_limit(void)
601 static bool may_cache_fd(void)
604 fd_limit = get_fd_limit();
606 if (fd_limit == RLIM_INFINITY)
609 return fd_limit > (rlim_t) dso__data_open_cnt;
613 * Check and close LRU dso if we crossed allowed limit
614 * for opened dso file descriptors. The limit is half
615 * of the RLIMIT_NOFILE files opened.
617 static void check_data_close(void)
619 bool cache_fd = may_cache_fd();
626 * dso__data_close - Close DSO data file
629 * External interface to close @dso's data file descriptor.
631 void dso__data_close(struct dso *dso)
633 pthread_mutex_lock(&dso__data_open_lock);
635 pthread_mutex_unlock(&dso__data_open_lock);
638 static void try_to_open_dso(struct dso *dso, struct machine *machine)
640 enum dso_binary_type binary_type_data[] = {
641 DSO_BINARY_TYPE__BUILD_ID_CACHE,
642 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
643 DSO_BINARY_TYPE__NOT_FOUND,
647 if (dso->data.fd >= 0)
650 if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) {
651 dso->data.fd = open_dso(dso, machine);
656 dso->binary_type = binary_type_data[i++];
658 dso->data.fd = open_dso(dso, machine);
659 if (dso->data.fd >= 0)
662 } while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND);
664 if (dso->data.fd >= 0)
665 dso->data.status = DSO_DATA_STATUS_OK;
667 dso->data.status = DSO_DATA_STATUS_ERROR;
671 * dso__data_get_fd - Get dso's data file descriptor
673 * @machine: machine object
675 * External interface to find dso's file, open it and
676 * returns file descriptor. It should be paired with
677 * dso__data_put_fd() if it returns non-negative value.
679 int dso__data_get_fd(struct dso *dso, struct machine *machine)
681 if (dso->data.status == DSO_DATA_STATUS_ERROR)
684 if (pthread_mutex_lock(&dso__data_open_lock) < 0)
687 try_to_open_dso(dso, machine);
689 if (dso->data.fd < 0)
690 pthread_mutex_unlock(&dso__data_open_lock);
695 void dso__data_put_fd(struct dso *dso __maybe_unused)
697 pthread_mutex_unlock(&dso__data_open_lock);
700 bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by)
704 if (dso->data.status_seen & flag)
707 dso->data.status_seen |= flag;
712 static ssize_t bpf_read(struct dso *dso, u64 offset, char *data)
714 struct bpf_prog_info_node *node;
715 ssize_t size = DSO__DATA_CACHE_SIZE;
719 node = perf_env__find_bpf_prog_info(dso->bpf_prog.env, dso->bpf_prog.id);
720 if (!node || !node->info_linear) {
721 dso->data.status = DSO_DATA_STATUS_ERROR;
725 len = node->info_linear->info.jited_prog_len;
726 buf = (u8 *)(uintptr_t)node->info_linear->info.jited_prog_insns;
731 size = (ssize_t)min(len - offset, (u64)size);
732 memcpy(data, buf + offset, size);
736 static int bpf_size(struct dso *dso)
738 struct bpf_prog_info_node *node;
740 node = perf_env__find_bpf_prog_info(dso->bpf_prog.env, dso->bpf_prog.id);
741 if (!node || !node->info_linear) {
742 dso->data.status = DSO_DATA_STATUS_ERROR;
746 dso->data.file_size = node->info_linear->info.jited_prog_len;
751 dso_cache__free(struct dso *dso)
753 struct rb_root *root = &dso->data.cache;
754 struct rb_node *next = rb_first(root);
756 pthread_mutex_lock(&dso->lock);
758 struct dso_cache *cache;
760 cache = rb_entry(next, struct dso_cache, rb_node);
761 next = rb_next(&cache->rb_node);
762 rb_erase(&cache->rb_node, root);
765 pthread_mutex_unlock(&dso->lock);
768 static struct dso_cache *dso_cache__find(struct dso *dso, u64 offset)
770 const struct rb_root *root = &dso->data.cache;
771 struct rb_node * const *p = &root->rb_node;
772 const struct rb_node *parent = NULL;
773 struct dso_cache *cache;
779 cache = rb_entry(parent, struct dso_cache, rb_node);
780 end = cache->offset + DSO__DATA_CACHE_SIZE;
782 if (offset < cache->offset)
784 else if (offset >= end)
793 static struct dso_cache *
794 dso_cache__insert(struct dso *dso, struct dso_cache *new)
796 struct rb_root *root = &dso->data.cache;
797 struct rb_node **p = &root->rb_node;
798 struct rb_node *parent = NULL;
799 struct dso_cache *cache;
800 u64 offset = new->offset;
802 pthread_mutex_lock(&dso->lock);
807 cache = rb_entry(parent, struct dso_cache, rb_node);
808 end = cache->offset + DSO__DATA_CACHE_SIZE;
810 if (offset < cache->offset)
812 else if (offset >= end)
818 rb_link_node(&new->rb_node, parent, p);
819 rb_insert_color(&new->rb_node, root);
823 pthread_mutex_unlock(&dso->lock);
828 dso_cache__memcpy(struct dso_cache *cache, u64 offset,
831 u64 cache_offset = offset - cache->offset;
832 u64 cache_size = min(cache->size - cache_offset, size);
834 memcpy(data, cache->data + cache_offset, cache_size);
838 static ssize_t file_read(struct dso *dso, struct machine *machine,
839 u64 offset, char *data)
843 pthread_mutex_lock(&dso__data_open_lock);
846 * dso->data.fd might be closed if other thread opened another
847 * file (dso) due to open file limit (RLIMIT_NOFILE).
849 try_to_open_dso(dso, machine);
851 if (dso->data.fd < 0) {
852 dso->data.status = DSO_DATA_STATUS_ERROR;
857 ret = pread(dso->data.fd, data, DSO__DATA_CACHE_SIZE, offset);
859 pthread_mutex_unlock(&dso__data_open_lock);
864 dso_cache__read(struct dso *dso, struct machine *machine,
865 u64 offset, u8 *data, ssize_t size)
867 u64 cache_offset = offset & DSO__DATA_CACHE_MASK;
868 struct dso_cache *cache;
869 struct dso_cache *old;
872 cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
876 if (dso->binary_type == DSO_BINARY_TYPE__BPF_PROG_INFO)
877 ret = bpf_read(dso, cache_offset, cache->data);
879 ret = file_read(dso, machine, cache_offset, cache->data);
882 cache->offset = cache_offset;
885 old = dso_cache__insert(dso, cache);
887 /* we lose the race */
892 ret = dso_cache__memcpy(cache, offset, data, size);
901 static ssize_t dso_cache_read(struct dso *dso, struct machine *machine,
902 u64 offset, u8 *data, ssize_t size)
904 struct dso_cache *cache;
906 cache = dso_cache__find(dso, offset);
908 return dso_cache__memcpy(cache, offset, data, size);
910 return dso_cache__read(dso, machine, offset, data, size);
914 * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
915 * in the rb_tree. Any read to already cached data is served
918 static ssize_t cached_read(struct dso *dso, struct machine *machine,
919 u64 offset, u8 *data, ssize_t size)
927 ret = dso_cache_read(dso, machine, offset, p, size);
931 /* Reached EOF, return what we have. */
947 static int file_size(struct dso *dso, struct machine *machine)
951 char sbuf[STRERR_BUFSIZE];
953 pthread_mutex_lock(&dso__data_open_lock);
956 * dso->data.fd might be closed if other thread opened another
957 * file (dso) due to open file limit (RLIMIT_NOFILE).
959 try_to_open_dso(dso, machine);
961 if (dso->data.fd < 0) {
963 dso->data.status = DSO_DATA_STATUS_ERROR;
967 if (fstat(dso->data.fd, &st) < 0) {
969 pr_err("dso cache fstat failed: %s\n",
970 str_error_r(errno, sbuf, sizeof(sbuf)));
971 dso->data.status = DSO_DATA_STATUS_ERROR;
974 dso->data.file_size = st.st_size;
977 pthread_mutex_unlock(&dso__data_open_lock);
981 int dso__data_file_size(struct dso *dso, struct machine *machine)
983 if (dso->data.file_size)
986 if (dso->data.status == DSO_DATA_STATUS_ERROR)
989 if (dso->binary_type == DSO_BINARY_TYPE__BPF_PROG_INFO)
990 return bpf_size(dso);
992 return file_size(dso, machine);
996 * dso__data_size - Return dso data size
998 * @machine: machine object
1000 * Return: dso data size
1002 off_t dso__data_size(struct dso *dso, struct machine *machine)
1004 if (dso__data_file_size(dso, machine))
1007 /* For now just estimate dso data size is close to file size */
1008 return dso->data.file_size;
1011 static ssize_t data_read_offset(struct dso *dso, struct machine *machine,
1012 u64 offset, u8 *data, ssize_t size)
1014 if (dso__data_file_size(dso, machine))
1017 /* Check the offset sanity. */
1018 if (offset > dso->data.file_size)
1021 if (offset + size < offset)
1024 return cached_read(dso, machine, offset, data, size);
1028 * dso__data_read_offset - Read data from dso file offset
1030 * @machine: machine object
1031 * @offset: file offset
1032 * @data: buffer to store data
1033 * @size: size of the @data buffer
1035 * External interface to read data from dso file offset. Open
1036 * dso data file and use cached_read to get the data.
1038 ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
1039 u64 offset, u8 *data, ssize_t size)
1041 if (dso->data.status == DSO_DATA_STATUS_ERROR)
1044 return data_read_offset(dso, machine, offset, data, size);
1048 * dso__data_read_addr - Read data from dso address
1050 * @machine: machine object
1051 * @add: virtual memory address
1052 * @data: buffer to store data
1053 * @size: size of the @data buffer
1055 * External interface to read data from dso address.
1057 ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
1058 struct machine *machine, u64 addr,
1059 u8 *data, ssize_t size)
1061 u64 offset = map->map_ip(map, addr);
1062 return dso__data_read_offset(dso, machine, offset, data, size);
1065 struct map *dso__new_map(const char *name)
1067 struct map *map = NULL;
1068 struct dso *dso = dso__new(name);
1071 map = map__new2(0, dso);
1076 struct dso *machine__findnew_kernel(struct machine *machine, const char *name,
1077 const char *short_name, int dso_type)
1080 * The kernel dso could be created by build_id processing.
1082 struct dso *dso = machine__findnew_dso(machine, name);
1085 * We need to run this in all cases, since during the build_id
1086 * processing we had no idea this was the kernel dso.
1089 dso__set_short_name(dso, short_name, false);
1090 dso->kernel = dso_type;
1097 * Find a matching entry and/or link current entry to RB tree.
1098 * Either one of the dso or name parameter must be non-NULL or the
1099 * function will not work.
1101 static struct dso *__dso__findlink_by_longname(struct rb_root *root,
1102 struct dso *dso, const char *name)
1104 struct rb_node **p = &root->rb_node;
1105 struct rb_node *parent = NULL;
1108 name = dso->long_name;
1110 * Find node with the matching name
1113 struct dso *this = rb_entry(*p, struct dso, rb_node);
1114 int rc = strcmp(name, this->long_name);
1119 * In case the new DSO is a duplicate of an existing
1120 * one, print a one-time warning & put the new entry
1121 * at the end of the list of duplicates.
1123 if (!dso || (dso == this))
1124 return this; /* Find matching dso */
1126 * The core kernel DSOs may have duplicated long name.
1127 * In this case, the short name should be different.
1128 * Comparing the short names to differentiate the DSOs.
1130 rc = strcmp(dso->short_name, this->short_name);
1132 pr_err("Duplicated dso name: %s\n", name);
1137 p = &parent->rb_left;
1139 p = &parent->rb_right;
1142 /* Add new node and rebalance tree */
1143 rb_link_node(&dso->rb_node, parent, p);
1144 rb_insert_color(&dso->rb_node, root);
1150 static inline struct dso *__dso__find_by_longname(struct rb_root *root,
1153 return __dso__findlink_by_longname(root, NULL, name);
1156 void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
1158 struct rb_root *root = dso->root;
1163 if (dso->long_name_allocated)
1164 free((char *)dso->long_name);
1167 rb_erase(&dso->rb_node, root);
1169 * __dso__findlink_by_longname() isn't guaranteed to add it
1170 * back, so a clean removal is required here.
1172 RB_CLEAR_NODE(&dso->rb_node);
1176 dso->long_name = name;
1177 dso->long_name_len = strlen(name);
1178 dso->long_name_allocated = name_allocated;
1181 __dso__findlink_by_longname(root, dso, NULL);
1184 void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
1189 if (dso->short_name_allocated)
1190 free((char *)dso->short_name);
1192 dso->short_name = name;
1193 dso->short_name_len = strlen(name);
1194 dso->short_name_allocated = name_allocated;
1197 static void dso__set_basename(struct dso *dso)
1202 if (sscanf(dso->long_name, "/tmp/perf-%d.map", &tid) == 1) {
1203 if (asprintf(&base, "[JIT] tid %d", tid) < 0)
1207 * basename() may modify path buffer, so we must pass
1210 lname = strdup(dso->long_name);
1215 * basename() may return a pointer to internal
1216 * storage which is reused in subsequent calls
1217 * so copy the result.
1219 base = strdup(basename(lname));
1226 dso__set_short_name(dso, base, true);
1229 int dso__name_len(const struct dso *dso)
1232 return strlen("[unknown]");
1234 return dso->long_name_len;
1236 return dso->short_name_len;
1239 bool dso__loaded(const struct dso *dso)
1244 bool dso__sorted_by_name(const struct dso *dso)
1246 return dso->sorted_by_name;
1249 void dso__set_sorted_by_name(struct dso *dso)
1251 dso->sorted_by_name = true;
1254 struct dso *dso__new(const char *name)
1256 struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
1259 strcpy(dso->name, name);
1260 dso__set_long_name(dso, dso->name, false);
1261 dso__set_short_name(dso, dso->name, false);
1262 dso->symbols = dso->symbol_names = RB_ROOT_CACHED;
1263 dso->data.cache = RB_ROOT;
1264 dso->inlined_nodes = RB_ROOT_CACHED;
1265 dso->srclines = RB_ROOT_CACHED;
1267 dso->data.status = DSO_DATA_STATUS_UNKNOWN;
1268 dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
1269 dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
1270 dso->is_64_bit = (sizeof(void *) == 8);
1273 dso->sorted_by_name = 0;
1274 dso->has_build_id = 0;
1275 dso->has_srcline = 1;
1277 dso->kernel = DSO_TYPE_USER;
1278 dso->needs_swap = DSO_SWAP__UNSET;
1279 dso->comp = COMP_ID__NONE;
1280 RB_CLEAR_NODE(&dso->rb_node);
1282 INIT_LIST_HEAD(&dso->node);
1283 INIT_LIST_HEAD(&dso->data.open_entry);
1284 pthread_mutex_init(&dso->lock, NULL);
1285 refcount_set(&dso->refcnt, 1);
1291 void dso__delete(struct dso *dso)
1293 if (!RB_EMPTY_NODE(&dso->rb_node))
1294 pr_err("DSO %s is still in rbtree when being deleted!\n",
1297 /* free inlines first, as they reference symbols */
1298 inlines__tree_delete(&dso->inlined_nodes);
1299 srcline__tree_delete(&dso->srclines);
1300 symbols__delete(&dso->symbols);
1302 if (dso->short_name_allocated) {
1303 zfree((char **)&dso->short_name);
1304 dso->short_name_allocated = false;
1307 if (dso->long_name_allocated) {
1308 zfree((char **)&dso->long_name);
1309 dso->long_name_allocated = false;
1312 dso__data_close(dso);
1313 auxtrace_cache__free(dso->auxtrace_cache);
1314 dso_cache__free(dso);
1316 zfree(&dso->symsrc_filename);
1317 nsinfo__zput(dso->nsinfo);
1318 pthread_mutex_destroy(&dso->lock);
1322 struct dso *dso__get(struct dso *dso)
1325 refcount_inc(&dso->refcnt);
1329 void dso__put(struct dso *dso)
1331 if (dso && refcount_dec_and_test(&dso->refcnt))
1335 void dso__set_build_id(struct dso *dso, void *build_id)
1337 memcpy(dso->build_id, build_id, sizeof(dso->build_id));
1338 dso->has_build_id = 1;
1341 bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
1343 return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
1346 void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
1348 char path[PATH_MAX];
1350 if (machine__is_default_guest(machine))
1352 sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
1353 if (sysfs__read_build_id(path, dso->build_id,
1354 sizeof(dso->build_id)) == 0)
1355 dso->has_build_id = true;
1358 int dso__kernel_module_get_build_id(struct dso *dso,
1359 const char *root_dir)
1361 char filename[PATH_MAX];
1363 * kernel module short names are of the form "[module]" and
1364 * we need just "module" here.
1366 const char *name = dso->short_name + 1;
1368 snprintf(filename, sizeof(filename),
1369 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1370 root_dir, (int)strlen(name) - 1, name);
1372 if (sysfs__read_build_id(filename, dso->build_id,
1373 sizeof(dso->build_id)) == 0)
1374 dso->has_build_id = true;
1379 bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
1381 bool have_build_id = false;
1383 struct nscookie nsc;
1385 list_for_each_entry(pos, head, node) {
1386 if (with_hits && !pos->hit && !dso__is_vdso(pos))
1388 if (pos->has_build_id) {
1389 have_build_id = true;
1392 nsinfo__mountns_enter(pos->nsinfo, &nsc);
1393 if (filename__read_build_id(pos->long_name, pos->build_id,
1394 sizeof(pos->build_id)) > 0) {
1395 have_build_id = true;
1396 pos->has_build_id = true;
1398 nsinfo__mountns_exit(&nsc);
1401 return have_build_id;
1404 void __dsos__add(struct dsos *dsos, struct dso *dso)
1406 list_add_tail(&dso->node, &dsos->head);
1407 __dso__findlink_by_longname(&dsos->root, dso, NULL);
1409 * It is now in the linked list, grab a reference, then garbage collect
1410 * this when needing memory, by looking at LRU dso instances in the
1411 * list with atomic_read(&dso->refcnt) == 1, i.e. no references
1412 * anywhere besides the one for the list, do, under a lock for the
1413 * list: remove it from the list, then a dso__put(), that probably will
1414 * be the last and will then call dso__delete(), end of life.
1416 * That, or at the end of the 'struct machine' lifetime, when all
1417 * 'struct dso' instances will be removed from the list, in
1418 * dsos__exit(), if they have no other reference from some other data
1421 * E.g.: after processing a 'perf.data' file and storing references
1422 * to objects instantiated while processing events, we will have
1423 * references to the 'thread', 'map', 'dso' structs all from 'struct
1424 * hist_entry' instances, but we may not need anything not referenced,
1425 * so we might as well call machines__exit()/machines__delete() and
1426 * garbage collect it.
1431 void dsos__add(struct dsos *dsos, struct dso *dso)
1433 down_write(&dsos->lock);
1434 __dsos__add(dsos, dso);
1435 up_write(&dsos->lock);
1438 struct dso *__dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
1443 list_for_each_entry(pos, &dsos->head, node)
1444 if (strcmp(pos->short_name, name) == 0)
1448 return __dso__find_by_longname(&dsos->root, name);
1451 struct dso *dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
1454 down_read(&dsos->lock);
1455 dso = __dsos__find(dsos, name, cmp_short);
1456 up_read(&dsos->lock);
1460 struct dso *__dsos__addnew(struct dsos *dsos, const char *name)
1462 struct dso *dso = dso__new(name);
1465 __dsos__add(dsos, dso);
1466 dso__set_basename(dso);
1467 /* Put dso here because __dsos_add already got it */
1473 struct dso *__dsos__findnew(struct dsos *dsos, const char *name)
1475 struct dso *dso = __dsos__find(dsos, name, false);
1477 return dso ? dso : __dsos__addnew(dsos, name);
1480 struct dso *dsos__findnew(struct dsos *dsos, const char *name)
1483 down_write(&dsos->lock);
1484 dso = dso__get(__dsos__findnew(dsos, name));
1485 up_write(&dsos->lock);
1489 size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
1490 bool (skip)(struct dso *dso, int parm), int parm)
1495 list_for_each_entry(pos, head, node) {
1496 if (skip && skip(pos, parm))
1498 ret += dso__fprintf_buildid(pos, fp);
1499 ret += fprintf(fp, " %s\n", pos->long_name);
1504 size_t __dsos__fprintf(struct list_head *head, FILE *fp)
1509 list_for_each_entry(pos, head, node) {
1510 ret += dso__fprintf(pos, fp);
1516 size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
1518 char sbuild_id[SBUILD_ID_SIZE];
1520 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1521 return fprintf(fp, "%s", sbuild_id);
1524 size_t dso__fprintf(struct dso *dso, FILE *fp)
1527 size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
1529 if (dso->short_name != dso->long_name)
1530 ret += fprintf(fp, "%s, ", dso->long_name);
1531 ret += fprintf(fp, "%sloaded, ", dso__loaded(dso) ? "" : "NOT ");
1532 ret += dso__fprintf_buildid(dso, fp);
1533 ret += fprintf(fp, ")\n");
1534 for (nd = rb_first_cached(&dso->symbols); nd; nd = rb_next(nd)) {
1535 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
1536 ret += symbol__fprintf(pos, fp);
1542 enum dso_type dso__type(struct dso *dso, struct machine *machine)
1545 enum dso_type type = DSO__TYPE_UNKNOWN;
1547 fd = dso__data_get_fd(dso, machine);
1549 type = dso__type_fd(fd);
1550 dso__data_put_fd(dso);
1556 int dso__strerror_load(struct dso *dso, char *buf, size_t buflen)
1558 int idx, errnum = dso->load_errno;
1560 * This must have a same ordering as the enum dso_load_errno.
1562 static const char *dso_load__error_str[] = {
1563 "Internal tools/perf/ library error",
1565 "Can not read build id",
1566 "Mismatching build id",
1567 "Decompression failure",
1570 BUG_ON(buflen == 0);
1573 const char *err = str_error_r(errnum, buf, buflen);
1576 scnprintf(buf, buflen, "%s", err);
1581 if (errnum < __DSO_LOAD_ERRNO__START || errnum >= __DSO_LOAD_ERRNO__END)
1584 idx = errnum - __DSO_LOAD_ERRNO__START;
1585 scnprintf(buf, buflen, "%s", dso_load__error_str[idx]);