1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/kernel.h>
4 #include <linux/string.h>
5 #include <linux/zalloc.h>
7 #include <sys/resource.h>
14 #include <bpf/libbpf.h>
15 #include "bpf-event.h"
17 #include "namespaces.h"
25 #include "util.h" /* O_CLOEXEC for older systems */
30 static const char * const debuglink_paths[] = {
37 char dso__symtab_origin(const struct dso *dso)
39 static const char origin[] = {
40 [DSO_BINARY_TYPE__KALLSYMS] = 'k',
41 [DSO_BINARY_TYPE__VMLINUX] = 'v',
42 [DSO_BINARY_TYPE__JAVA_JIT] = 'j',
43 [DSO_BINARY_TYPE__DEBUGLINK] = 'l',
44 [DSO_BINARY_TYPE__BUILD_ID_CACHE] = 'B',
45 [DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO] = 'D',
46 [DSO_BINARY_TYPE__FEDORA_DEBUGINFO] = 'f',
47 [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO] = 'u',
48 [DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO] = 'o',
49 [DSO_BINARY_TYPE__BUILDID_DEBUGINFO] = 'b',
50 [DSO_BINARY_TYPE__SYSTEM_PATH_DSO] = 'd',
51 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE] = 'K',
52 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP] = 'm',
53 [DSO_BINARY_TYPE__GUEST_KALLSYMS] = 'g',
54 [DSO_BINARY_TYPE__GUEST_KMODULE] = 'G',
55 [DSO_BINARY_TYPE__GUEST_KMODULE_COMP] = 'M',
56 [DSO_BINARY_TYPE__GUEST_VMLINUX] = 'V',
59 if (dso == NULL || dso->symtab_type == DSO_BINARY_TYPE__NOT_FOUND)
61 return origin[dso->symtab_type];
64 int dso__read_binary_type_filename(const struct dso *dso,
65 enum dso_binary_type type,
66 char *root_dir, char *filename, size_t size)
68 char build_id_hex[SBUILD_ID_SIZE];
73 case DSO_BINARY_TYPE__DEBUGLINK:
75 const char *last_slash;
76 char dso_dir[PATH_MAX];
77 char symfile[PATH_MAX];
80 len = __symbol__join_symfs(filename, size, dso->long_name);
81 last_slash = filename + len;
82 while (last_slash != filename && *last_slash != '/')
85 strncpy(dso_dir, filename, last_slash - filename);
86 dso_dir[last_slash-filename] = '\0';
88 if (!is_regular_file(filename)) {
93 ret = filename__read_debuglink(filename, symfile, PATH_MAX);
97 /* Check predefined locations where debug file might reside */
99 for (i = 0; i < ARRAY_SIZE(debuglink_paths); i++) {
100 snprintf(filename, size,
101 debuglink_paths[i], dso_dir, symfile);
102 if (is_regular_file(filename)) {
110 case DSO_BINARY_TYPE__BUILD_ID_CACHE:
111 if (dso__build_id_filename(dso, filename, size, false) == NULL)
115 case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO:
116 if (dso__build_id_filename(dso, filename, size, true) == NULL)
120 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
121 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
122 snprintf(filename + len, size - len, "%s.debug", dso->long_name);
125 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
126 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
127 snprintf(filename + len, size - len, "%s", dso->long_name);
130 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
132 const char *last_slash;
135 last_slash = dso->long_name + dso->long_name_len;
136 while (last_slash != dso->long_name && *last_slash != '/')
139 len = __symbol__join_symfs(filename, size, "");
140 dir_size = last_slash - dso->long_name + 2;
141 if (dir_size > (size - len)) {
145 len += scnprintf(filename + len, dir_size, "%s", dso->long_name);
146 len += scnprintf(filename + len , size - len, ".debug%s",
151 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
152 if (!dso->has_build_id) {
157 build_id__sprintf(dso->build_id,
158 sizeof(dso->build_id),
160 len = __symbol__join_symfs(filename, size, "/usr/lib/debug/.build-id/");
161 snprintf(filename + len, size - len, "%.2s/%s.debug",
162 build_id_hex, build_id_hex + 2);
165 case DSO_BINARY_TYPE__VMLINUX:
166 case DSO_BINARY_TYPE__GUEST_VMLINUX:
167 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
168 __symbol__join_symfs(filename, size, dso->long_name);
171 case DSO_BINARY_TYPE__GUEST_KMODULE:
172 case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
173 path__join3(filename, size, symbol_conf.symfs,
174 root_dir, dso->long_name);
177 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
178 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
179 __symbol__join_symfs(filename, size, dso->long_name);
182 case DSO_BINARY_TYPE__KCORE:
183 case DSO_BINARY_TYPE__GUEST_KCORE:
184 snprintf(filename, size, "%s", dso->long_name);
188 case DSO_BINARY_TYPE__KALLSYMS:
189 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
190 case DSO_BINARY_TYPE__JAVA_JIT:
191 case DSO_BINARY_TYPE__BPF_PROG_INFO:
192 case DSO_BINARY_TYPE__NOT_FOUND:
204 static const struct {
206 int (*decompress)(const char *input, int output);
207 bool (*is_compressed)(const char *input);
209 [COMP_ID__NONE] = { .fmt = NULL, },
210 #ifdef HAVE_ZLIB_SUPPORT
211 { "gz", gzip_decompress_to_file, gzip_is_compressed },
213 #ifdef HAVE_LZMA_SUPPORT
214 { "xz", lzma_decompress_to_file, lzma_is_compressed },
216 { NULL, NULL, NULL },
219 static int is_supported_compression(const char *ext)
223 for (i = 1; compressions[i].fmt; i++) {
224 if (!strcmp(ext, compressions[i].fmt))
227 return COMP_ID__NONE;
230 bool is_kernel_module(const char *pathname, int cpumode)
233 int mode = cpumode & PERF_RECORD_MISC_CPUMODE_MASK;
235 WARN_ONCE(mode != cpumode,
236 "Internal error: passing unmasked cpumode (%x) to is_kernel_module",
240 case PERF_RECORD_MISC_USER:
241 case PERF_RECORD_MISC_HYPERVISOR:
242 case PERF_RECORD_MISC_GUEST_USER:
244 /* Treat PERF_RECORD_MISC_CPUMODE_UNKNOWN as kernel */
246 if (kmod_path__parse(&m, pathname)) {
247 pr_err("Failed to check whether %s is a kernel module or not. Assume it is.",
256 bool dso__needs_decompress(struct dso *dso)
258 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
259 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
262 static int decompress_kmodule(struct dso *dso, const char *name,
263 char *pathname, size_t len)
265 char tmpbuf[] = KMOD_DECOMP_NAME;
268 if (!dso__needs_decompress(dso))
271 if (dso->comp == COMP_ID__NONE)
275 * We have proper compression id for DSO and yet the file
276 * behind the 'name' can still be plain uncompressed object.
278 * The reason is behind the logic we open the DSO object files,
279 * when we try all possible 'debug' objects until we find the
280 * data. So even if the DSO is represented by 'krava.xz' module,
281 * we can end up here opening ~/.debug/....23432432/debug' file
282 * which is not compressed.
284 * To keep this transparent, we detect this and return the file
285 * descriptor to the uncompressed file.
287 if (!compressions[dso->comp].is_compressed(name))
288 return open(name, O_RDONLY);
290 fd = mkstemp(tmpbuf);
292 dso->load_errno = errno;
296 if (compressions[dso->comp].decompress(name, fd)) {
297 dso->load_errno = DSO_LOAD_ERRNO__DECOMPRESSION_FAILURE;
302 if (!pathname || (fd < 0))
305 if (pathname && (fd >= 0))
306 strlcpy(pathname, tmpbuf, len);
311 int dso__decompress_kmodule_fd(struct dso *dso, const char *name)
313 return decompress_kmodule(dso, name, NULL, 0);
316 int dso__decompress_kmodule_path(struct dso *dso, const char *name,
317 char *pathname, size_t len)
319 int fd = decompress_kmodule(dso, name, pathname, len);
322 return fd >= 0 ? 0 : -1;
326 * Parses kernel module specified in @path and updates
329 * @comp - true if @path contains supported compression suffix,
331 * @kmod - true if @path contains '.ko' suffix in right position,
333 * @name - if (@alloc_name && @kmod) is true, it contains strdup-ed base name
334 * of the kernel module without suffixes, otherwise strudup-ed
336 * @ext - if (@alloc_ext && @comp) is true, it contains strdup-ed string
337 * the compression suffix
339 * Returns 0 if there's no strdup error, -ENOMEM otherwise.
341 int __kmod_path__parse(struct kmod_path *m, const char *path,
344 const char *name = strrchr(path, '/');
345 const char *ext = strrchr(path, '.');
346 bool is_simple_name = false;
348 memset(m, 0x0, sizeof(*m));
349 name = name ? name + 1 : path;
352 * '.' is also a valid character for module name. For example:
353 * [aaa.bbb] is a valid module name. '[' should have higher
354 * priority than '.ko' suffix.
356 * The kernel names are from machine__mmap_name. Such
357 * name should belong to kernel itself, not kernel module.
359 if (name[0] == '[') {
360 is_simple_name = true;
361 if ((strncmp(name, "[kernel.kallsyms]", 17) == 0) ||
362 (strncmp(name, "[guest.kernel.kallsyms", 22) == 0) ||
363 (strncmp(name, "[vdso]", 6) == 0) ||
364 (strncmp(name, "[vdso32]", 8) == 0) ||
365 (strncmp(name, "[vdsox32]", 9) == 0) ||
366 (strncmp(name, "[vsyscall]", 10) == 0)) {
373 /* No extension, just return name. */
374 if ((ext == NULL) || is_simple_name) {
376 m->name = strdup(name);
377 return m->name ? 0 : -ENOMEM;
382 m->comp = is_supported_compression(ext + 1);
383 if (m->comp > COMP_ID__NONE)
386 /* Check .ko extension only if there's enough name left. */
388 m->kmod = !strncmp(ext, ".ko", 3);
392 if (asprintf(&m->name, "[%.*s]", (int) (ext - name), name) == -1)
395 if (asprintf(&m->name, "%s", name) == -1)
399 strreplace(m->name, '-', '_');
405 void dso__set_module_info(struct dso *dso, struct kmod_path *m,
406 struct machine *machine)
408 if (machine__is_host(machine))
409 dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE;
411 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE;
413 /* _KMODULE_COMP should be next to _KMODULE */
414 if (m->kmod && m->comp) {
419 dso__set_short_name(dso, strdup(m->name), true);
423 * Global list of open DSOs and the counter.
425 static LIST_HEAD(dso__data_open);
426 static long dso__data_open_cnt;
427 static pthread_mutex_t dso__data_open_lock = PTHREAD_MUTEX_INITIALIZER;
429 static void dso__list_add(struct dso *dso)
431 list_add_tail(&dso->data.open_entry, &dso__data_open);
432 dso__data_open_cnt++;
435 static void dso__list_del(struct dso *dso)
437 list_del_init(&dso->data.open_entry);
438 WARN_ONCE(dso__data_open_cnt <= 0,
439 "DSO data fd counter out of bounds.");
440 dso__data_open_cnt--;
443 static void close_first_dso(void);
445 static int do_open(char *name)
448 char sbuf[STRERR_BUFSIZE];
451 fd = open(name, O_RDONLY|O_CLOEXEC);
455 pr_debug("dso open failed: %s\n",
456 str_error_r(errno, sbuf, sizeof(sbuf)));
457 if (!dso__data_open_cnt || errno != EMFILE)
466 static int __open_dso(struct dso *dso, struct machine *machine)
469 char *root_dir = (char *)"";
470 char *name = malloc(PATH_MAX);
477 root_dir = machine->root_dir;
479 if (dso__read_binary_type_filename(dso, dso->binary_type,
480 root_dir, name, PATH_MAX))
483 if (!is_regular_file(name))
486 if (dso__needs_decompress(dso)) {
487 char newpath[KMOD_DECOMP_LEN];
488 size_t len = sizeof(newpath);
490 if (dso__decompress_kmodule_path(dso, name, newpath, len) < 0) {
491 fd = -dso->load_errno;
496 strcpy(name, newpath);
509 static void check_data_close(void);
512 * dso_close - Open DSO data file
515 * Open @dso's data file descriptor and updates
516 * list/count of open DSO objects.
518 static int open_dso(struct dso *dso, struct machine *machine)
523 if (dso->binary_type != DSO_BINARY_TYPE__BUILD_ID_CACHE)
524 nsinfo__mountns_enter(dso->nsinfo, &nsc);
525 fd = __open_dso(dso, machine);
526 if (dso->binary_type != DSO_BINARY_TYPE__BUILD_ID_CACHE)
527 nsinfo__mountns_exit(&nsc);
532 * Check if we crossed the allowed number
533 * of opened DSOs and close one if needed.
541 static void close_data_fd(struct dso *dso)
543 if (dso->data.fd >= 0) {
546 dso->data.file_size = 0;
552 * dso_close - Close DSO data file
555 * Close @dso's data file descriptor and updates
556 * list/count of open DSO objects.
558 static void close_dso(struct dso *dso)
563 static void close_first_dso(void)
567 dso = list_first_entry(&dso__data_open, struct dso, data.open_entry);
571 static rlim_t get_fd_limit(void)
576 /* Allow half of the current open fd limit. */
577 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
578 if (l.rlim_cur == RLIM_INFINITY)
581 limit = l.rlim_cur / 2;
583 pr_err("failed to get fd limit\n");
590 static rlim_t fd_limit;
593 * Used only by tests/dso-data.c to reset the environment
594 * for tests. I dont expect we should change this during
597 void reset_fd_limit(void)
602 static bool may_cache_fd(void)
605 fd_limit = get_fd_limit();
607 if (fd_limit == RLIM_INFINITY)
610 return fd_limit > (rlim_t) dso__data_open_cnt;
614 * Check and close LRU dso if we crossed allowed limit
615 * for opened dso file descriptors. The limit is half
616 * of the RLIMIT_NOFILE files opened.
618 static void check_data_close(void)
620 bool cache_fd = may_cache_fd();
627 * dso__data_close - Close DSO data file
630 * External interface to close @dso's data file descriptor.
632 void dso__data_close(struct dso *dso)
634 pthread_mutex_lock(&dso__data_open_lock);
636 pthread_mutex_unlock(&dso__data_open_lock);
639 static void try_to_open_dso(struct dso *dso, struct machine *machine)
641 enum dso_binary_type binary_type_data[] = {
642 DSO_BINARY_TYPE__BUILD_ID_CACHE,
643 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
644 DSO_BINARY_TYPE__NOT_FOUND,
648 if (dso->data.fd >= 0)
651 if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) {
652 dso->data.fd = open_dso(dso, machine);
657 dso->binary_type = binary_type_data[i++];
659 dso->data.fd = open_dso(dso, machine);
660 if (dso->data.fd >= 0)
663 } while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND);
665 if (dso->data.fd >= 0)
666 dso->data.status = DSO_DATA_STATUS_OK;
668 dso->data.status = DSO_DATA_STATUS_ERROR;
672 * dso__data_get_fd - Get dso's data file descriptor
674 * @machine: machine object
676 * External interface to find dso's file, open it and
677 * returns file descriptor. It should be paired with
678 * dso__data_put_fd() if it returns non-negative value.
680 int dso__data_get_fd(struct dso *dso, struct machine *machine)
682 if (dso->data.status == DSO_DATA_STATUS_ERROR)
685 if (pthread_mutex_lock(&dso__data_open_lock) < 0)
688 try_to_open_dso(dso, machine);
690 if (dso->data.fd < 0)
691 pthread_mutex_unlock(&dso__data_open_lock);
696 void dso__data_put_fd(struct dso *dso __maybe_unused)
698 pthread_mutex_unlock(&dso__data_open_lock);
701 bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by)
705 if (dso->data.status_seen & flag)
708 dso->data.status_seen |= flag;
713 static ssize_t bpf_read(struct dso *dso, u64 offset, char *data)
715 struct bpf_prog_info_node *node;
716 ssize_t size = DSO__DATA_CACHE_SIZE;
720 node = perf_env__find_bpf_prog_info(dso->bpf_prog.env, dso->bpf_prog.id);
721 if (!node || !node->info_linear) {
722 dso->data.status = DSO_DATA_STATUS_ERROR;
726 len = node->info_linear->info.jited_prog_len;
727 buf = (u8 *)(uintptr_t)node->info_linear->info.jited_prog_insns;
732 size = (ssize_t)min(len - offset, (u64)size);
733 memcpy(data, buf + offset, size);
737 static int bpf_size(struct dso *dso)
739 struct bpf_prog_info_node *node;
741 node = perf_env__find_bpf_prog_info(dso->bpf_prog.env, dso->bpf_prog.id);
742 if (!node || !node->info_linear) {
743 dso->data.status = DSO_DATA_STATUS_ERROR;
747 dso->data.file_size = node->info_linear->info.jited_prog_len;
752 dso_cache__free(struct dso *dso)
754 struct rb_root *root = &dso->data.cache;
755 struct rb_node *next = rb_first(root);
757 pthread_mutex_lock(&dso->lock);
759 struct dso_cache *cache;
761 cache = rb_entry(next, struct dso_cache, rb_node);
762 next = rb_next(&cache->rb_node);
763 rb_erase(&cache->rb_node, root);
766 pthread_mutex_unlock(&dso->lock);
769 static struct dso_cache *dso_cache__find(struct dso *dso, u64 offset)
771 const struct rb_root *root = &dso->data.cache;
772 struct rb_node * const *p = &root->rb_node;
773 const struct rb_node *parent = NULL;
774 struct dso_cache *cache;
780 cache = rb_entry(parent, struct dso_cache, rb_node);
781 end = cache->offset + DSO__DATA_CACHE_SIZE;
783 if (offset < cache->offset)
785 else if (offset >= end)
794 static struct dso_cache *
795 dso_cache__insert(struct dso *dso, struct dso_cache *new)
797 struct rb_root *root = &dso->data.cache;
798 struct rb_node **p = &root->rb_node;
799 struct rb_node *parent = NULL;
800 struct dso_cache *cache;
801 u64 offset = new->offset;
803 pthread_mutex_lock(&dso->lock);
808 cache = rb_entry(parent, struct dso_cache, rb_node);
809 end = cache->offset + DSO__DATA_CACHE_SIZE;
811 if (offset < cache->offset)
813 else if (offset >= end)
819 rb_link_node(&new->rb_node, parent, p);
820 rb_insert_color(&new->rb_node, root);
824 pthread_mutex_unlock(&dso->lock);
829 dso_cache__memcpy(struct dso_cache *cache, u64 offset,
832 u64 cache_offset = offset - cache->offset;
833 u64 cache_size = min(cache->size - cache_offset, size);
835 memcpy(data, cache->data + cache_offset, cache_size);
839 static ssize_t file_read(struct dso *dso, struct machine *machine,
840 u64 offset, char *data)
844 pthread_mutex_lock(&dso__data_open_lock);
847 * dso->data.fd might be closed if other thread opened another
848 * file (dso) due to open file limit (RLIMIT_NOFILE).
850 try_to_open_dso(dso, machine);
852 if (dso->data.fd < 0) {
853 dso->data.status = DSO_DATA_STATUS_ERROR;
858 ret = pread(dso->data.fd, data, DSO__DATA_CACHE_SIZE, offset);
860 pthread_mutex_unlock(&dso__data_open_lock);
865 dso_cache__read(struct dso *dso, struct machine *machine,
866 u64 offset, u8 *data, ssize_t size)
868 u64 cache_offset = offset & DSO__DATA_CACHE_MASK;
869 struct dso_cache *cache;
870 struct dso_cache *old;
873 cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
877 if (dso->binary_type == DSO_BINARY_TYPE__BPF_PROG_INFO)
878 ret = bpf_read(dso, cache_offset, cache->data);
880 ret = file_read(dso, machine, cache_offset, cache->data);
883 cache->offset = cache_offset;
886 old = dso_cache__insert(dso, cache);
888 /* we lose the race */
893 ret = dso_cache__memcpy(cache, offset, data, size);
902 static ssize_t dso_cache_read(struct dso *dso, struct machine *machine,
903 u64 offset, u8 *data, ssize_t size)
905 struct dso_cache *cache;
907 cache = dso_cache__find(dso, offset);
909 return dso_cache__memcpy(cache, offset, data, size);
911 return dso_cache__read(dso, machine, offset, data, size);
915 * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
916 * in the rb_tree. Any read to already cached data is served
919 static ssize_t cached_read(struct dso *dso, struct machine *machine,
920 u64 offset, u8 *data, ssize_t size)
928 ret = dso_cache_read(dso, machine, offset, p, size);
932 /* Reached EOF, return what we have. */
948 static int file_size(struct dso *dso, struct machine *machine)
952 char sbuf[STRERR_BUFSIZE];
954 pthread_mutex_lock(&dso__data_open_lock);
957 * dso->data.fd might be closed if other thread opened another
958 * file (dso) due to open file limit (RLIMIT_NOFILE).
960 try_to_open_dso(dso, machine);
962 if (dso->data.fd < 0) {
964 dso->data.status = DSO_DATA_STATUS_ERROR;
968 if (fstat(dso->data.fd, &st) < 0) {
970 pr_err("dso cache fstat failed: %s\n",
971 str_error_r(errno, sbuf, sizeof(sbuf)));
972 dso->data.status = DSO_DATA_STATUS_ERROR;
975 dso->data.file_size = st.st_size;
978 pthread_mutex_unlock(&dso__data_open_lock);
982 int dso__data_file_size(struct dso *dso, struct machine *machine)
984 if (dso->data.file_size)
987 if (dso->data.status == DSO_DATA_STATUS_ERROR)
990 if (dso->binary_type == DSO_BINARY_TYPE__BPF_PROG_INFO)
991 return bpf_size(dso);
993 return file_size(dso, machine);
997 * dso__data_size - Return dso data size
999 * @machine: machine object
1001 * Return: dso data size
1003 off_t dso__data_size(struct dso *dso, struct machine *machine)
1005 if (dso__data_file_size(dso, machine))
1008 /* For now just estimate dso data size is close to file size */
1009 return dso->data.file_size;
1012 static ssize_t data_read_offset(struct dso *dso, struct machine *machine,
1013 u64 offset, u8 *data, ssize_t size)
1015 if (dso__data_file_size(dso, machine))
1018 /* Check the offset sanity. */
1019 if (offset > dso->data.file_size)
1022 if (offset + size < offset)
1025 return cached_read(dso, machine, offset, data, size);
1029 * dso__data_read_offset - Read data from dso file offset
1031 * @machine: machine object
1032 * @offset: file offset
1033 * @data: buffer to store data
1034 * @size: size of the @data buffer
1036 * External interface to read data from dso file offset. Open
1037 * dso data file and use cached_read to get the data.
1039 ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
1040 u64 offset, u8 *data, ssize_t size)
1042 if (dso->data.status == DSO_DATA_STATUS_ERROR)
1045 return data_read_offset(dso, machine, offset, data, size);
1049 * dso__data_read_addr - Read data from dso address
1051 * @machine: machine object
1052 * @add: virtual memory address
1053 * @data: buffer to store data
1054 * @size: size of the @data buffer
1056 * External interface to read data from dso address.
1058 ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
1059 struct machine *machine, u64 addr,
1060 u8 *data, ssize_t size)
1062 u64 offset = map->map_ip(map, addr);
1063 return dso__data_read_offset(dso, machine, offset, data, size);
1066 struct map *dso__new_map(const char *name)
1068 struct map *map = NULL;
1069 struct dso *dso = dso__new(name);
1072 map = map__new2(0, dso);
1077 struct dso *machine__findnew_kernel(struct machine *machine, const char *name,
1078 const char *short_name, int dso_type)
1081 * The kernel dso could be created by build_id processing.
1083 struct dso *dso = machine__findnew_dso(machine, name);
1086 * We need to run this in all cases, since during the build_id
1087 * processing we had no idea this was the kernel dso.
1090 dso__set_short_name(dso, short_name, false);
1091 dso->kernel = dso_type;
1098 * Find a matching entry and/or link current entry to RB tree.
1099 * Either one of the dso or name parameter must be non-NULL or the
1100 * function will not work.
1102 static struct dso *__dso__findlink_by_longname(struct rb_root *root,
1103 struct dso *dso, const char *name)
1105 struct rb_node **p = &root->rb_node;
1106 struct rb_node *parent = NULL;
1109 name = dso->long_name;
1111 * Find node with the matching name
1114 struct dso *this = rb_entry(*p, struct dso, rb_node);
1115 int rc = strcmp(name, this->long_name);
1120 * In case the new DSO is a duplicate of an existing
1121 * one, print a one-time warning & put the new entry
1122 * at the end of the list of duplicates.
1124 if (!dso || (dso == this))
1125 return this; /* Find matching dso */
1127 * The core kernel DSOs may have duplicated long name.
1128 * In this case, the short name should be different.
1129 * Comparing the short names to differentiate the DSOs.
1131 rc = strcmp(dso->short_name, this->short_name);
1133 pr_err("Duplicated dso name: %s\n", name);
1138 p = &parent->rb_left;
1140 p = &parent->rb_right;
1143 /* Add new node and rebalance tree */
1144 rb_link_node(&dso->rb_node, parent, p);
1145 rb_insert_color(&dso->rb_node, root);
1151 static inline struct dso *__dso__find_by_longname(struct rb_root *root,
1154 return __dso__findlink_by_longname(root, NULL, name);
1157 void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
1159 struct rb_root *root = dso->root;
1164 if (dso->long_name_allocated)
1165 free((char *)dso->long_name);
1168 rb_erase(&dso->rb_node, root);
1170 * __dso__findlink_by_longname() isn't guaranteed to add it
1171 * back, so a clean removal is required here.
1173 RB_CLEAR_NODE(&dso->rb_node);
1177 dso->long_name = name;
1178 dso->long_name_len = strlen(name);
1179 dso->long_name_allocated = name_allocated;
1182 __dso__findlink_by_longname(root, dso, NULL);
1185 void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
1190 if (dso->short_name_allocated)
1191 free((char *)dso->short_name);
1193 dso->short_name = name;
1194 dso->short_name_len = strlen(name);
1195 dso->short_name_allocated = name_allocated;
1198 static void dso__set_basename(struct dso *dso)
1203 if (sscanf(dso->long_name, "/tmp/perf-%d.map", &tid) == 1) {
1204 if (asprintf(&base, "[JIT] tid %d", tid) < 0)
1208 * basename() may modify path buffer, so we must pass
1211 lname = strdup(dso->long_name);
1216 * basename() may return a pointer to internal
1217 * storage which is reused in subsequent calls
1218 * so copy the result.
1220 base = strdup(basename(lname));
1227 dso__set_short_name(dso, base, true);
1230 int dso__name_len(const struct dso *dso)
1233 return strlen("[unknown]");
1235 return dso->long_name_len;
1237 return dso->short_name_len;
1240 bool dso__loaded(const struct dso *dso)
1245 bool dso__sorted_by_name(const struct dso *dso)
1247 return dso->sorted_by_name;
1250 void dso__set_sorted_by_name(struct dso *dso)
1252 dso->sorted_by_name = true;
1255 struct dso *dso__new(const char *name)
1257 struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
1260 strcpy(dso->name, name);
1261 dso__set_long_name(dso, dso->name, false);
1262 dso__set_short_name(dso, dso->name, false);
1263 dso->symbols = dso->symbol_names = RB_ROOT_CACHED;
1264 dso->data.cache = RB_ROOT;
1265 dso->inlined_nodes = RB_ROOT_CACHED;
1266 dso->srclines = RB_ROOT_CACHED;
1268 dso->data.status = DSO_DATA_STATUS_UNKNOWN;
1269 dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
1270 dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
1271 dso->is_64_bit = (sizeof(void *) == 8);
1274 dso->sorted_by_name = 0;
1275 dso->has_build_id = 0;
1276 dso->has_srcline = 1;
1278 dso->kernel = DSO_TYPE_USER;
1279 dso->needs_swap = DSO_SWAP__UNSET;
1280 dso->comp = COMP_ID__NONE;
1281 RB_CLEAR_NODE(&dso->rb_node);
1283 INIT_LIST_HEAD(&dso->node);
1284 INIT_LIST_HEAD(&dso->data.open_entry);
1285 pthread_mutex_init(&dso->lock, NULL);
1286 refcount_set(&dso->refcnt, 1);
1292 void dso__delete(struct dso *dso)
1294 if (!RB_EMPTY_NODE(&dso->rb_node))
1295 pr_err("DSO %s is still in rbtree when being deleted!\n",
1298 /* free inlines first, as they reference symbols */
1299 inlines__tree_delete(&dso->inlined_nodes);
1300 srcline__tree_delete(&dso->srclines);
1301 symbols__delete(&dso->symbols);
1303 if (dso->short_name_allocated) {
1304 zfree((char **)&dso->short_name);
1305 dso->short_name_allocated = false;
1308 if (dso->long_name_allocated) {
1309 zfree((char **)&dso->long_name);
1310 dso->long_name_allocated = false;
1313 dso__data_close(dso);
1314 auxtrace_cache__free(dso->auxtrace_cache);
1315 dso_cache__free(dso);
1317 zfree(&dso->symsrc_filename);
1318 nsinfo__zput(dso->nsinfo);
1319 pthread_mutex_destroy(&dso->lock);
1323 struct dso *dso__get(struct dso *dso)
1326 refcount_inc(&dso->refcnt);
1330 void dso__put(struct dso *dso)
1332 if (dso && refcount_dec_and_test(&dso->refcnt))
1336 void dso__set_build_id(struct dso *dso, void *build_id)
1338 memcpy(dso->build_id, build_id, sizeof(dso->build_id));
1339 dso->has_build_id = 1;
1342 bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
1344 return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
1347 void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
1349 char path[PATH_MAX];
1351 if (machine__is_default_guest(machine))
1353 sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
1354 if (sysfs__read_build_id(path, dso->build_id,
1355 sizeof(dso->build_id)) == 0)
1356 dso->has_build_id = true;
1359 int dso__kernel_module_get_build_id(struct dso *dso,
1360 const char *root_dir)
1362 char filename[PATH_MAX];
1364 * kernel module short names are of the form "[module]" and
1365 * we need just "module" here.
1367 const char *name = dso->short_name + 1;
1369 snprintf(filename, sizeof(filename),
1370 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1371 root_dir, (int)strlen(name) - 1, name);
1373 if (sysfs__read_build_id(filename, dso->build_id,
1374 sizeof(dso->build_id)) == 0)
1375 dso->has_build_id = true;
1380 bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
1382 bool have_build_id = false;
1384 struct nscookie nsc;
1386 list_for_each_entry(pos, head, node) {
1387 if (with_hits && !pos->hit && !dso__is_vdso(pos))
1389 if (pos->has_build_id) {
1390 have_build_id = true;
1393 nsinfo__mountns_enter(pos->nsinfo, &nsc);
1394 if (filename__read_build_id(pos->long_name, pos->build_id,
1395 sizeof(pos->build_id)) > 0) {
1396 have_build_id = true;
1397 pos->has_build_id = true;
1399 nsinfo__mountns_exit(&nsc);
1402 return have_build_id;
1405 void __dsos__add(struct dsos *dsos, struct dso *dso)
1407 list_add_tail(&dso->node, &dsos->head);
1408 __dso__findlink_by_longname(&dsos->root, dso, NULL);
1410 * It is now in the linked list, grab a reference, then garbage collect
1411 * this when needing memory, by looking at LRU dso instances in the
1412 * list with atomic_read(&dso->refcnt) == 1, i.e. no references
1413 * anywhere besides the one for the list, do, under a lock for the
1414 * list: remove it from the list, then a dso__put(), that probably will
1415 * be the last and will then call dso__delete(), end of life.
1417 * That, or at the end of the 'struct machine' lifetime, when all
1418 * 'struct dso' instances will be removed from the list, in
1419 * dsos__exit(), if they have no other reference from some other data
1422 * E.g.: after processing a 'perf.data' file and storing references
1423 * to objects instantiated while processing events, we will have
1424 * references to the 'thread', 'map', 'dso' structs all from 'struct
1425 * hist_entry' instances, but we may not need anything not referenced,
1426 * so we might as well call machines__exit()/machines__delete() and
1427 * garbage collect it.
1432 void dsos__add(struct dsos *dsos, struct dso *dso)
1434 down_write(&dsos->lock);
1435 __dsos__add(dsos, dso);
1436 up_write(&dsos->lock);
1439 struct dso *__dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
1444 list_for_each_entry(pos, &dsos->head, node)
1445 if (strcmp(pos->short_name, name) == 0)
1449 return __dso__find_by_longname(&dsos->root, name);
1452 struct dso *dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
1455 down_read(&dsos->lock);
1456 dso = __dsos__find(dsos, name, cmp_short);
1457 up_read(&dsos->lock);
1461 struct dso *__dsos__addnew(struct dsos *dsos, const char *name)
1463 struct dso *dso = dso__new(name);
1466 __dsos__add(dsos, dso);
1467 dso__set_basename(dso);
1468 /* Put dso here because __dsos_add already got it */
1474 struct dso *__dsos__findnew(struct dsos *dsos, const char *name)
1476 struct dso *dso = __dsos__find(dsos, name, false);
1478 return dso ? dso : __dsos__addnew(dsos, name);
1481 struct dso *dsos__findnew(struct dsos *dsos, const char *name)
1484 down_write(&dsos->lock);
1485 dso = dso__get(__dsos__findnew(dsos, name));
1486 up_write(&dsos->lock);
1490 size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
1491 bool (skip)(struct dso *dso, int parm), int parm)
1496 list_for_each_entry(pos, head, node) {
1497 if (skip && skip(pos, parm))
1499 ret += dso__fprintf_buildid(pos, fp);
1500 ret += fprintf(fp, " %s\n", pos->long_name);
1505 size_t __dsos__fprintf(struct list_head *head, FILE *fp)
1510 list_for_each_entry(pos, head, node) {
1511 ret += dso__fprintf(pos, fp);
1517 size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
1519 char sbuild_id[SBUILD_ID_SIZE];
1521 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1522 return fprintf(fp, "%s", sbuild_id);
1525 size_t dso__fprintf(struct dso *dso, FILE *fp)
1528 size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
1530 if (dso->short_name != dso->long_name)
1531 ret += fprintf(fp, "%s, ", dso->long_name);
1532 ret += fprintf(fp, "%sloaded, ", dso__loaded(dso) ? "" : "NOT ");
1533 ret += dso__fprintf_buildid(dso, fp);
1534 ret += fprintf(fp, ")\n");
1535 for (nd = rb_first_cached(&dso->symbols); nd; nd = rb_next(nd)) {
1536 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
1537 ret += symbol__fprintf(pos, fp);
1543 enum dso_type dso__type(struct dso *dso, struct machine *machine)
1546 enum dso_type type = DSO__TYPE_UNKNOWN;
1548 fd = dso__data_get_fd(dso, machine);
1550 type = dso__type_fd(fd);
1551 dso__data_put_fd(dso);
1557 int dso__strerror_load(struct dso *dso, char *buf, size_t buflen)
1559 int idx, errnum = dso->load_errno;
1561 * This must have a same ordering as the enum dso_load_errno.
1563 static const char *dso_load__error_str[] = {
1564 "Internal tools/perf/ library error",
1566 "Can not read build id",
1567 "Mismatching build id",
1568 "Decompression failure",
1571 BUG_ON(buflen == 0);
1574 const char *err = str_error_r(errnum, buf, buflen);
1577 scnprintf(buf, buflen, "%s", err);
1582 if (errnum < __DSO_LOAD_ERRNO__START || errnum >= __DSO_LOAD_ERRNO__END)
1585 idx = errnum - __DSO_LOAD_ERRNO__START;
1586 scnprintf(buf, buflen, "%s", dso_load__error_str[idx]);