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"
18 #include "namespaces.h"
27 #include "util.h" /* O_CLOEXEC for older systems */
32 static const char * const debuglink_paths[] = {
39 char dso__symtab_origin(const struct dso *dso)
41 static const char origin[] = {
42 [DSO_BINARY_TYPE__KALLSYMS] = 'k',
43 [DSO_BINARY_TYPE__VMLINUX] = 'v',
44 [DSO_BINARY_TYPE__JAVA_JIT] = 'j',
45 [DSO_BINARY_TYPE__DEBUGLINK] = 'l',
46 [DSO_BINARY_TYPE__BUILD_ID_CACHE] = 'B',
47 [DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO] = 'D',
48 [DSO_BINARY_TYPE__FEDORA_DEBUGINFO] = 'f',
49 [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO] = 'u',
50 [DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO] = 'x',
51 [DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO] = 'o',
52 [DSO_BINARY_TYPE__BUILDID_DEBUGINFO] = 'b',
53 [DSO_BINARY_TYPE__SYSTEM_PATH_DSO] = 'd',
54 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE] = 'K',
55 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP] = 'm',
56 [DSO_BINARY_TYPE__GUEST_KALLSYMS] = 'g',
57 [DSO_BINARY_TYPE__GUEST_KMODULE] = 'G',
58 [DSO_BINARY_TYPE__GUEST_KMODULE_COMP] = 'M',
59 [DSO_BINARY_TYPE__GUEST_VMLINUX] = 'V',
62 if (dso == NULL || dso->symtab_type == DSO_BINARY_TYPE__NOT_FOUND)
64 return origin[dso->symtab_type];
67 int dso__read_binary_type_filename(const struct dso *dso,
68 enum dso_binary_type type,
69 char *root_dir, char *filename, size_t size)
71 char build_id_hex[SBUILD_ID_SIZE];
76 case DSO_BINARY_TYPE__DEBUGLINK:
78 const char *last_slash;
79 char dso_dir[PATH_MAX];
80 char symfile[PATH_MAX];
83 len = __symbol__join_symfs(filename, size, dso->long_name);
84 last_slash = filename + len;
85 while (last_slash != filename && *last_slash != '/')
88 strncpy(dso_dir, filename, last_slash - filename);
89 dso_dir[last_slash-filename] = '\0';
91 if (!is_regular_file(filename)) {
96 ret = filename__read_debuglink(filename, symfile, PATH_MAX);
100 /* Check predefined locations where debug file might reside */
102 for (i = 0; i < ARRAY_SIZE(debuglink_paths); i++) {
103 snprintf(filename, size,
104 debuglink_paths[i], dso_dir, symfile);
105 if (is_regular_file(filename)) {
113 case DSO_BINARY_TYPE__BUILD_ID_CACHE:
114 if (dso__build_id_filename(dso, filename, size, false) == NULL)
118 case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO:
119 if (dso__build_id_filename(dso, filename, size, true) == NULL)
123 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
124 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
125 snprintf(filename + len, size - len, "%s.debug", dso->long_name);
128 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
129 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
130 snprintf(filename + len, size - len, "%s", dso->long_name);
133 case DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO:
135 * Ubuntu can mixup /usr/lib with /lib, putting debuginfo in
136 * /usr/lib/debug/lib when it is expected to be in
137 * /usr/lib/debug/usr/lib
139 if (strlen(dso->long_name) < 9 ||
140 strncmp(dso->long_name, "/usr/lib/", 9)) {
144 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
145 snprintf(filename + len, size - len, "%s", dso->long_name + 4);
148 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
150 const char *last_slash;
153 last_slash = dso->long_name + dso->long_name_len;
154 while (last_slash != dso->long_name && *last_slash != '/')
157 len = __symbol__join_symfs(filename, size, "");
158 dir_size = last_slash - dso->long_name + 2;
159 if (dir_size > (size - len)) {
163 len += scnprintf(filename + len, dir_size, "%s", dso->long_name);
164 len += scnprintf(filename + len , size - len, ".debug%s",
169 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
170 if (!dso->has_build_id) {
175 build_id__sprintf(&dso->bid, build_id_hex);
176 len = __symbol__join_symfs(filename, size, "/usr/lib/debug/.build-id/");
177 snprintf(filename + len, size - len, "%.2s/%s.debug",
178 build_id_hex, build_id_hex + 2);
181 case DSO_BINARY_TYPE__VMLINUX:
182 case DSO_BINARY_TYPE__GUEST_VMLINUX:
183 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
184 __symbol__join_symfs(filename, size, dso->long_name);
187 case DSO_BINARY_TYPE__GUEST_KMODULE:
188 case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
189 path__join3(filename, size, symbol_conf.symfs,
190 root_dir, dso->long_name);
193 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
194 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
195 __symbol__join_symfs(filename, size, dso->long_name);
198 case DSO_BINARY_TYPE__KCORE:
199 case DSO_BINARY_TYPE__GUEST_KCORE:
200 snprintf(filename, size, "%s", dso->long_name);
204 case DSO_BINARY_TYPE__KALLSYMS:
205 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
206 case DSO_BINARY_TYPE__JAVA_JIT:
207 case DSO_BINARY_TYPE__BPF_PROG_INFO:
208 case DSO_BINARY_TYPE__BPF_IMAGE:
209 case DSO_BINARY_TYPE__OOL:
210 case DSO_BINARY_TYPE__NOT_FOUND:
222 static const struct {
224 int (*decompress)(const char *input, int output);
225 bool (*is_compressed)(const char *input);
227 [COMP_ID__NONE] = { .fmt = NULL, },
228 #ifdef HAVE_ZLIB_SUPPORT
229 { "gz", gzip_decompress_to_file, gzip_is_compressed },
231 #ifdef HAVE_LZMA_SUPPORT
232 { "xz", lzma_decompress_to_file, lzma_is_compressed },
234 { NULL, NULL, NULL },
237 static int is_supported_compression(const char *ext)
241 for (i = 1; compressions[i].fmt; i++) {
242 if (!strcmp(ext, compressions[i].fmt))
245 return COMP_ID__NONE;
248 bool is_kernel_module(const char *pathname, int cpumode)
251 int mode = cpumode & PERF_RECORD_MISC_CPUMODE_MASK;
253 WARN_ONCE(mode != cpumode,
254 "Internal error: passing unmasked cpumode (%x) to is_kernel_module",
258 case PERF_RECORD_MISC_USER:
259 case PERF_RECORD_MISC_HYPERVISOR:
260 case PERF_RECORD_MISC_GUEST_USER:
262 /* Treat PERF_RECORD_MISC_CPUMODE_UNKNOWN as kernel */
264 if (kmod_path__parse(&m, pathname)) {
265 pr_err("Failed to check whether %s is a kernel module or not. Assume it is.",
274 bool dso__needs_decompress(struct dso *dso)
276 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
277 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
280 static int decompress_kmodule(struct dso *dso, const char *name,
281 char *pathname, size_t len)
283 char tmpbuf[] = KMOD_DECOMP_NAME;
286 if (!dso__needs_decompress(dso))
289 if (dso->comp == COMP_ID__NONE)
293 * We have proper compression id for DSO and yet the file
294 * behind the 'name' can still be plain uncompressed object.
296 * The reason is behind the logic we open the DSO object files,
297 * when we try all possible 'debug' objects until we find the
298 * data. So even if the DSO is represented by 'krava.xz' module,
299 * we can end up here opening ~/.debug/....23432432/debug' file
300 * which is not compressed.
302 * To keep this transparent, we detect this and return the file
303 * descriptor to the uncompressed file.
305 if (!compressions[dso->comp].is_compressed(name))
306 return open(name, O_RDONLY);
308 fd = mkstemp(tmpbuf);
310 dso->load_errno = errno;
314 if (compressions[dso->comp].decompress(name, fd)) {
315 dso->load_errno = DSO_LOAD_ERRNO__DECOMPRESSION_FAILURE;
320 if (!pathname || (fd < 0))
323 if (pathname && (fd >= 0))
324 strlcpy(pathname, tmpbuf, len);
329 int dso__decompress_kmodule_fd(struct dso *dso, const char *name)
331 return decompress_kmodule(dso, name, NULL, 0);
334 int dso__decompress_kmodule_path(struct dso *dso, const char *name,
335 char *pathname, size_t len)
337 int fd = decompress_kmodule(dso, name, pathname, len);
340 return fd >= 0 ? 0 : -1;
344 * Parses kernel module specified in @path and updates
347 * @comp - true if @path contains supported compression suffix,
349 * @kmod - true if @path contains '.ko' suffix in right position,
351 * @name - if (@alloc_name && @kmod) is true, it contains strdup-ed base name
352 * of the kernel module without suffixes, otherwise strudup-ed
354 * @ext - if (@alloc_ext && @comp) is true, it contains strdup-ed string
355 * the compression suffix
357 * Returns 0 if there's no strdup error, -ENOMEM otherwise.
359 int __kmod_path__parse(struct kmod_path *m, const char *path,
362 const char *name = strrchr(path, '/');
363 const char *ext = strrchr(path, '.');
364 bool is_simple_name = false;
366 memset(m, 0x0, sizeof(*m));
367 name = name ? name + 1 : path;
370 * '.' is also a valid character for module name. For example:
371 * [aaa.bbb] is a valid module name. '[' should have higher
372 * priority than '.ko' suffix.
374 * The kernel names are from machine__mmap_name. Such
375 * name should belong to kernel itself, not kernel module.
377 if (name[0] == '[') {
378 is_simple_name = true;
379 if ((strncmp(name, "[kernel.kallsyms]", 17) == 0) ||
380 (strncmp(name, "[guest.kernel.kallsyms", 22) == 0) ||
381 (strncmp(name, "[vdso]", 6) == 0) ||
382 (strncmp(name, "[vdso32]", 8) == 0) ||
383 (strncmp(name, "[vdsox32]", 9) == 0) ||
384 (strncmp(name, "[vsyscall]", 10) == 0)) {
391 /* No extension, just return name. */
392 if ((ext == NULL) || is_simple_name) {
394 m->name = strdup(name);
395 return m->name ? 0 : -ENOMEM;
400 m->comp = is_supported_compression(ext + 1);
401 if (m->comp > COMP_ID__NONE)
404 /* Check .ko extension only if there's enough name left. */
406 m->kmod = !strncmp(ext, ".ko", 3);
410 if (asprintf(&m->name, "[%.*s]", (int) (ext - name), name) == -1)
413 if (asprintf(&m->name, "%s", name) == -1)
417 strreplace(m->name, '-', '_');
423 void dso__set_module_info(struct dso *dso, struct kmod_path *m,
424 struct machine *machine)
426 if (machine__is_host(machine))
427 dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE;
429 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE;
431 /* _KMODULE_COMP should be next to _KMODULE */
432 if (m->kmod && m->comp) {
437 dso__set_short_name(dso, strdup(m->name), true);
441 * Global list of open DSOs and the counter.
443 static LIST_HEAD(dso__data_open);
444 static long dso__data_open_cnt;
445 static pthread_mutex_t dso__data_open_lock = PTHREAD_MUTEX_INITIALIZER;
447 static void dso__list_add(struct dso *dso)
449 list_add_tail(&dso->data.open_entry, &dso__data_open);
450 dso__data_open_cnt++;
453 static void dso__list_del(struct dso *dso)
455 list_del_init(&dso->data.open_entry);
456 WARN_ONCE(dso__data_open_cnt <= 0,
457 "DSO data fd counter out of bounds.");
458 dso__data_open_cnt--;
461 static void close_first_dso(void);
463 static int do_open(char *name)
466 char sbuf[STRERR_BUFSIZE];
469 fd = open(name, O_RDONLY|O_CLOEXEC);
473 pr_debug("dso open failed: %s\n",
474 str_error_r(errno, sbuf, sizeof(sbuf)));
475 if (!dso__data_open_cnt || errno != EMFILE)
484 static int __open_dso(struct dso *dso, struct machine *machine)
487 char *root_dir = (char *)"";
488 char *name = malloc(PATH_MAX);
495 root_dir = machine->root_dir;
497 if (dso__read_binary_type_filename(dso, dso->binary_type,
498 root_dir, name, PATH_MAX))
501 if (!is_regular_file(name))
504 if (dso__needs_decompress(dso)) {
505 char newpath[KMOD_DECOMP_LEN];
506 size_t len = sizeof(newpath);
508 if (dso__decompress_kmodule_path(dso, name, newpath, len) < 0) {
509 fd = -dso->load_errno;
514 strcpy(name, newpath);
527 static void check_data_close(void);
530 * dso_close - Open DSO data file
533 * Open @dso's data file descriptor and updates
534 * list/count of open DSO objects.
536 static int open_dso(struct dso *dso, struct machine *machine)
541 if (dso->binary_type != DSO_BINARY_TYPE__BUILD_ID_CACHE)
542 nsinfo__mountns_enter(dso->nsinfo, &nsc);
543 fd = __open_dso(dso, machine);
544 if (dso->binary_type != DSO_BINARY_TYPE__BUILD_ID_CACHE)
545 nsinfo__mountns_exit(&nsc);
550 * Check if we crossed the allowed number
551 * of opened DSOs and close one if needed.
559 static void close_data_fd(struct dso *dso)
561 if (dso->data.fd >= 0) {
564 dso->data.file_size = 0;
570 * dso_close - Close DSO data file
573 * Close @dso's data file descriptor and updates
574 * list/count of open DSO objects.
576 static void close_dso(struct dso *dso)
581 static void close_first_dso(void)
585 dso = list_first_entry(&dso__data_open, struct dso, data.open_entry);
589 static rlim_t get_fd_limit(void)
594 /* Allow half of the current open fd limit. */
595 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
596 if (l.rlim_cur == RLIM_INFINITY)
599 limit = l.rlim_cur / 2;
601 pr_err("failed to get fd limit\n");
608 static rlim_t fd_limit;
611 * Used only by tests/dso-data.c to reset the environment
612 * for tests. I dont expect we should change this during
615 void reset_fd_limit(void)
620 static bool may_cache_fd(void)
623 fd_limit = get_fd_limit();
625 if (fd_limit == RLIM_INFINITY)
628 return fd_limit > (rlim_t) dso__data_open_cnt;
632 * Check and close LRU dso if we crossed allowed limit
633 * for opened dso file descriptors. The limit is half
634 * of the RLIMIT_NOFILE files opened.
636 static void check_data_close(void)
638 bool cache_fd = may_cache_fd();
645 * dso__data_close - Close DSO data file
648 * External interface to close @dso's data file descriptor.
650 void dso__data_close(struct dso *dso)
652 pthread_mutex_lock(&dso__data_open_lock);
654 pthread_mutex_unlock(&dso__data_open_lock);
657 static void try_to_open_dso(struct dso *dso, struct machine *machine)
659 enum dso_binary_type binary_type_data[] = {
660 DSO_BINARY_TYPE__BUILD_ID_CACHE,
661 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
662 DSO_BINARY_TYPE__NOT_FOUND,
666 if (dso->data.fd >= 0)
669 if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) {
670 dso->data.fd = open_dso(dso, machine);
675 dso->binary_type = binary_type_data[i++];
677 dso->data.fd = open_dso(dso, machine);
678 if (dso->data.fd >= 0)
681 } while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND);
683 if (dso->data.fd >= 0)
684 dso->data.status = DSO_DATA_STATUS_OK;
686 dso->data.status = DSO_DATA_STATUS_ERROR;
690 * dso__data_get_fd - Get dso's data file descriptor
692 * @machine: machine object
694 * External interface to find dso's file, open it and
695 * returns file descriptor. It should be paired with
696 * dso__data_put_fd() if it returns non-negative value.
698 int dso__data_get_fd(struct dso *dso, struct machine *machine)
700 if (dso->data.status == DSO_DATA_STATUS_ERROR)
703 if (pthread_mutex_lock(&dso__data_open_lock) < 0)
706 try_to_open_dso(dso, machine);
708 if (dso->data.fd < 0)
709 pthread_mutex_unlock(&dso__data_open_lock);
714 void dso__data_put_fd(struct dso *dso __maybe_unused)
716 pthread_mutex_unlock(&dso__data_open_lock);
719 bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by)
723 if (dso->data.status_seen & flag)
726 dso->data.status_seen |= flag;
731 static ssize_t bpf_read(struct dso *dso, u64 offset, char *data)
733 struct bpf_prog_info_node *node;
734 ssize_t size = DSO__DATA_CACHE_SIZE;
738 node = perf_env__find_bpf_prog_info(dso->bpf_prog.env, dso->bpf_prog.id);
739 if (!node || !node->info_linear) {
740 dso->data.status = DSO_DATA_STATUS_ERROR;
744 len = node->info_linear->info.jited_prog_len;
745 buf = (u8 *)(uintptr_t)node->info_linear->info.jited_prog_insns;
750 size = (ssize_t)min(len - offset, (u64)size);
751 memcpy(data, buf + offset, size);
755 static int bpf_size(struct dso *dso)
757 struct bpf_prog_info_node *node;
759 node = perf_env__find_bpf_prog_info(dso->bpf_prog.env, dso->bpf_prog.id);
760 if (!node || !node->info_linear) {
761 dso->data.status = DSO_DATA_STATUS_ERROR;
765 dso->data.file_size = node->info_linear->info.jited_prog_len;
770 dso_cache__free(struct dso *dso)
772 struct rb_root *root = &dso->data.cache;
773 struct rb_node *next = rb_first(root);
775 pthread_mutex_lock(&dso->lock);
777 struct dso_cache *cache;
779 cache = rb_entry(next, struct dso_cache, rb_node);
780 next = rb_next(&cache->rb_node);
781 rb_erase(&cache->rb_node, root);
784 pthread_mutex_unlock(&dso->lock);
787 static struct dso_cache *__dso_cache__find(struct dso *dso, u64 offset)
789 const struct rb_root *root = &dso->data.cache;
790 struct rb_node * const *p = &root->rb_node;
791 const struct rb_node *parent = NULL;
792 struct dso_cache *cache;
798 cache = rb_entry(parent, struct dso_cache, rb_node);
799 end = cache->offset + DSO__DATA_CACHE_SIZE;
801 if (offset < cache->offset)
803 else if (offset >= end)
812 static struct dso_cache *
813 dso_cache__insert(struct dso *dso, struct dso_cache *new)
815 struct rb_root *root = &dso->data.cache;
816 struct rb_node **p = &root->rb_node;
817 struct rb_node *parent = NULL;
818 struct dso_cache *cache;
819 u64 offset = new->offset;
821 pthread_mutex_lock(&dso->lock);
826 cache = rb_entry(parent, struct dso_cache, rb_node);
827 end = cache->offset + DSO__DATA_CACHE_SIZE;
829 if (offset < cache->offset)
831 else if (offset >= end)
837 rb_link_node(&new->rb_node, parent, p);
838 rb_insert_color(&new->rb_node, root);
842 pthread_mutex_unlock(&dso->lock);
846 static ssize_t dso_cache__memcpy(struct dso_cache *cache, u64 offset, u8 *data,
849 u64 cache_offset = offset - cache->offset;
850 u64 cache_size = min(cache->size - cache_offset, size);
853 memcpy(data, cache->data + cache_offset, cache_size);
855 memcpy(cache->data + cache_offset, data, cache_size);
859 static ssize_t file_read(struct dso *dso, struct machine *machine,
860 u64 offset, char *data)
864 pthread_mutex_lock(&dso__data_open_lock);
867 * dso->data.fd might be closed if other thread opened another
868 * file (dso) due to open file limit (RLIMIT_NOFILE).
870 try_to_open_dso(dso, machine);
872 if (dso->data.fd < 0) {
873 dso->data.status = DSO_DATA_STATUS_ERROR;
878 ret = pread(dso->data.fd, data, DSO__DATA_CACHE_SIZE, offset);
880 pthread_mutex_unlock(&dso__data_open_lock);
884 static struct dso_cache *dso_cache__populate(struct dso *dso,
885 struct machine *machine,
886 u64 offset, ssize_t *ret)
888 u64 cache_offset = offset & DSO__DATA_CACHE_MASK;
889 struct dso_cache *cache;
890 struct dso_cache *old;
892 cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
898 if (dso->binary_type == DSO_BINARY_TYPE__BPF_PROG_INFO)
899 *ret = bpf_read(dso, cache_offset, cache->data);
900 else if (dso->binary_type == DSO_BINARY_TYPE__OOL)
901 *ret = DSO__DATA_CACHE_SIZE;
903 *ret = file_read(dso, machine, cache_offset, cache->data);
910 cache->offset = cache_offset;
913 old = dso_cache__insert(dso, cache);
915 /* we lose the race */
923 static struct dso_cache *dso_cache__find(struct dso *dso,
924 struct machine *machine,
928 struct dso_cache *cache = __dso_cache__find(dso, offset);
930 return cache ? cache : dso_cache__populate(dso, machine, offset, ret);
933 static ssize_t dso_cache_io(struct dso *dso, struct machine *machine,
934 u64 offset, u8 *data, ssize_t size, bool out)
936 struct dso_cache *cache;
939 cache = dso_cache__find(dso, machine, offset, &ret);
943 return dso_cache__memcpy(cache, offset, data, size, out);
947 * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
948 * in the rb_tree. Any read to already cached data is served
949 * by cached data. Writes update the cache only, not the backing file.
951 static ssize_t cached_io(struct dso *dso, struct machine *machine,
952 u64 offset, u8 *data, ssize_t size, bool out)
960 ret = dso_cache_io(dso, machine, offset, p, size, out);
964 /* Reached EOF, return what we have. */
980 static int file_size(struct dso *dso, struct machine *machine)
984 char sbuf[STRERR_BUFSIZE];
986 pthread_mutex_lock(&dso__data_open_lock);
989 * dso->data.fd might be closed if other thread opened another
990 * file (dso) due to open file limit (RLIMIT_NOFILE).
992 try_to_open_dso(dso, machine);
994 if (dso->data.fd < 0) {
996 dso->data.status = DSO_DATA_STATUS_ERROR;
1000 if (fstat(dso->data.fd, &st) < 0) {
1002 pr_err("dso cache fstat failed: %s\n",
1003 str_error_r(errno, sbuf, sizeof(sbuf)));
1004 dso->data.status = DSO_DATA_STATUS_ERROR;
1007 dso->data.file_size = st.st_size;
1010 pthread_mutex_unlock(&dso__data_open_lock);
1014 int dso__data_file_size(struct dso *dso, struct machine *machine)
1016 if (dso->data.file_size)
1019 if (dso->data.status == DSO_DATA_STATUS_ERROR)
1022 if (dso->binary_type == DSO_BINARY_TYPE__BPF_PROG_INFO)
1023 return bpf_size(dso);
1025 return file_size(dso, machine);
1029 * dso__data_size - Return dso data size
1031 * @machine: machine object
1033 * Return: dso data size
1035 off_t dso__data_size(struct dso *dso, struct machine *machine)
1037 if (dso__data_file_size(dso, machine))
1040 /* For now just estimate dso data size is close to file size */
1041 return dso->data.file_size;
1044 static ssize_t data_read_write_offset(struct dso *dso, struct machine *machine,
1045 u64 offset, u8 *data, ssize_t size,
1048 if (dso__data_file_size(dso, machine))
1051 /* Check the offset sanity. */
1052 if (offset > dso->data.file_size)
1055 if (offset + size < offset)
1058 return cached_io(dso, machine, offset, data, size, out);
1062 * dso__data_read_offset - Read data from dso file offset
1064 * @machine: machine object
1065 * @offset: file offset
1066 * @data: buffer to store data
1067 * @size: size of the @data buffer
1069 * External interface to read data from dso file offset. Open
1070 * dso data file and use cached_read to get the data.
1072 ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
1073 u64 offset, u8 *data, ssize_t size)
1075 if (dso->data.status == DSO_DATA_STATUS_ERROR)
1078 return data_read_write_offset(dso, machine, offset, data, size, true);
1082 * dso__data_read_addr - Read data from dso address
1084 * @machine: machine object
1085 * @add: virtual memory address
1086 * @data: buffer to store data
1087 * @size: size of the @data buffer
1089 * External interface to read data from dso address.
1091 ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
1092 struct machine *machine, u64 addr,
1093 u8 *data, ssize_t size)
1095 u64 offset = map->map_ip(map, addr);
1096 return dso__data_read_offset(dso, machine, offset, data, size);
1100 * dso__data_write_cache_offs - Write data to dso data cache at file offset
1102 * @machine: machine object
1103 * @offset: file offset
1104 * @data: buffer to write
1105 * @size: size of the @data buffer
1107 * Write into the dso file data cache, but do not change the file itself.
1109 ssize_t dso__data_write_cache_offs(struct dso *dso, struct machine *machine,
1110 u64 offset, const u8 *data_in, ssize_t size)
1112 u8 *data = (u8 *)data_in; /* cast away const to use same fns for r/w */
1114 if (dso->data.status == DSO_DATA_STATUS_ERROR)
1117 return data_read_write_offset(dso, machine, offset, data, size, false);
1121 * dso__data_write_cache_addr - Write data to dso data cache at dso address
1123 * @machine: machine object
1124 * @add: virtual memory address
1125 * @data: buffer to write
1126 * @size: size of the @data buffer
1128 * External interface to write into the dso file data cache, but do not change
1131 ssize_t dso__data_write_cache_addr(struct dso *dso, struct map *map,
1132 struct machine *machine, u64 addr,
1133 const u8 *data, ssize_t size)
1135 u64 offset = map->map_ip(map, addr);
1136 return dso__data_write_cache_offs(dso, machine, offset, data, size);
1139 struct map *dso__new_map(const char *name)
1141 struct map *map = NULL;
1142 struct dso *dso = dso__new(name);
1145 map = map__new2(0, dso);
1150 struct dso *machine__findnew_kernel(struct machine *machine, const char *name,
1151 const char *short_name, int dso_type)
1154 * The kernel dso could be created by build_id processing.
1156 struct dso *dso = machine__findnew_dso(machine, name);
1159 * We need to run this in all cases, since during the build_id
1160 * processing we had no idea this was the kernel dso.
1163 dso__set_short_name(dso, short_name, false);
1164 dso->kernel = dso_type;
1170 static void dso__set_long_name_id(struct dso *dso, const char *name, struct dso_id *id, bool name_allocated)
1172 struct rb_root *root = dso->root;
1177 if (dso->long_name_allocated)
1178 free((char *)dso->long_name);
1181 rb_erase(&dso->rb_node, root);
1183 * __dsos__findnew_link_by_longname_id() isn't guaranteed to
1184 * add it back, so a clean removal is required here.
1186 RB_CLEAR_NODE(&dso->rb_node);
1190 dso->long_name = name;
1191 dso->long_name_len = strlen(name);
1192 dso->long_name_allocated = name_allocated;
1195 __dsos__findnew_link_by_longname_id(root, dso, NULL, id);
1198 void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
1200 dso__set_long_name_id(dso, name, NULL, name_allocated);
1203 void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
1208 if (dso->short_name_allocated)
1209 free((char *)dso->short_name);
1211 dso->short_name = name;
1212 dso->short_name_len = strlen(name);
1213 dso->short_name_allocated = name_allocated;
1216 int dso__name_len(const struct dso *dso)
1219 return strlen("[unknown]");
1221 return dso->long_name_len;
1223 return dso->short_name_len;
1226 bool dso__loaded(const struct dso *dso)
1231 bool dso__sorted_by_name(const struct dso *dso)
1233 return dso->sorted_by_name;
1236 void dso__set_sorted_by_name(struct dso *dso)
1238 dso->sorted_by_name = true;
1241 struct dso *dso__new_id(const char *name, struct dso_id *id)
1243 struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
1246 strcpy(dso->name, name);
1249 dso__set_long_name_id(dso, dso->name, id, false);
1250 dso__set_short_name(dso, dso->name, false);
1251 dso->symbols = dso->symbol_names = RB_ROOT_CACHED;
1252 dso->data.cache = RB_ROOT;
1253 dso->inlined_nodes = RB_ROOT_CACHED;
1254 dso->srclines = RB_ROOT_CACHED;
1256 dso->data.status = DSO_DATA_STATUS_UNKNOWN;
1257 dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
1258 dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
1259 dso->is_64_bit = (sizeof(void *) == 8);
1262 dso->sorted_by_name = 0;
1263 dso->has_build_id = 0;
1264 dso->has_srcline = 1;
1266 dso->kernel = DSO_SPACE__USER;
1267 dso->needs_swap = DSO_SWAP__UNSET;
1268 dso->comp = COMP_ID__NONE;
1269 RB_CLEAR_NODE(&dso->rb_node);
1271 INIT_LIST_HEAD(&dso->node);
1272 INIT_LIST_HEAD(&dso->data.open_entry);
1273 pthread_mutex_init(&dso->lock, NULL);
1274 refcount_set(&dso->refcnt, 1);
1280 struct dso *dso__new(const char *name)
1282 return dso__new_id(name, NULL);
1285 void dso__delete(struct dso *dso)
1287 if (!RB_EMPTY_NODE(&dso->rb_node))
1288 pr_err("DSO %s is still in rbtree when being deleted!\n",
1291 /* free inlines first, as they reference symbols */
1292 inlines__tree_delete(&dso->inlined_nodes);
1293 srcline__tree_delete(&dso->srclines);
1294 symbols__delete(&dso->symbols);
1296 if (dso->short_name_allocated) {
1297 zfree((char **)&dso->short_name);
1298 dso->short_name_allocated = false;
1301 if (dso->long_name_allocated) {
1302 zfree((char **)&dso->long_name);
1303 dso->long_name_allocated = false;
1306 dso__data_close(dso);
1307 auxtrace_cache__free(dso->auxtrace_cache);
1308 dso_cache__free(dso);
1310 zfree(&dso->symsrc_filename);
1311 nsinfo__zput(dso->nsinfo);
1312 pthread_mutex_destroy(&dso->lock);
1316 struct dso *dso__get(struct dso *dso)
1319 refcount_inc(&dso->refcnt);
1323 void dso__put(struct dso *dso)
1325 if (dso && refcount_dec_and_test(&dso->refcnt))
1329 void dso__set_build_id(struct dso *dso, struct build_id *bid)
1332 dso->has_build_id = 1;
1335 bool dso__build_id_equal(const struct dso *dso, struct build_id *bid)
1337 return dso->bid.size == bid->size &&
1338 memcmp(dso->bid.data, bid->data, dso->bid.size) == 0;
1341 void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
1343 char path[PATH_MAX];
1345 if (machine__is_default_guest(machine))
1347 sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
1348 if (sysfs__read_build_id(path, &dso->bid) == 0)
1349 dso->has_build_id = true;
1352 int dso__kernel_module_get_build_id(struct dso *dso,
1353 const char *root_dir)
1355 char filename[PATH_MAX];
1357 * kernel module short names are of the form "[module]" and
1358 * we need just "module" here.
1360 const char *name = dso->short_name + 1;
1362 snprintf(filename, sizeof(filename),
1363 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1364 root_dir, (int)strlen(name) - 1, name);
1366 if (sysfs__read_build_id(filename, &dso->bid) == 0)
1367 dso->has_build_id = true;
1372 static size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
1374 char sbuild_id[SBUILD_ID_SIZE];
1376 build_id__sprintf(&dso->bid, sbuild_id);
1377 return fprintf(fp, "%s", sbuild_id);
1380 size_t dso__fprintf(struct dso *dso, FILE *fp)
1383 size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
1385 if (dso->short_name != dso->long_name)
1386 ret += fprintf(fp, "%s, ", dso->long_name);
1387 ret += fprintf(fp, "%sloaded, ", dso__loaded(dso) ? "" : "NOT ");
1388 ret += dso__fprintf_buildid(dso, fp);
1389 ret += fprintf(fp, ")\n");
1390 for (nd = rb_first_cached(&dso->symbols); nd; nd = rb_next(nd)) {
1391 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
1392 ret += symbol__fprintf(pos, fp);
1398 enum dso_type dso__type(struct dso *dso, struct machine *machine)
1401 enum dso_type type = DSO__TYPE_UNKNOWN;
1403 fd = dso__data_get_fd(dso, machine);
1405 type = dso__type_fd(fd);
1406 dso__data_put_fd(dso);
1412 int dso__strerror_load(struct dso *dso, char *buf, size_t buflen)
1414 int idx, errnum = dso->load_errno;
1416 * This must have a same ordering as the enum dso_load_errno.
1418 static const char *dso_load__error_str[] = {
1419 "Internal tools/perf/ library error",
1421 "Can not read build id",
1422 "Mismatching build id",
1423 "Decompression failure",
1426 BUG_ON(buflen == 0);
1429 const char *err = str_error_r(errnum, buf, buflen);
1432 scnprintf(buf, buflen, "%s", err);
1437 if (errnum < __DSO_LOAD_ERRNO__START || errnum >= __DSO_LOAD_ERRNO__END)
1440 idx = errnum - __DSO_LOAD_ERRNO__START;
1441 scnprintf(buf, buflen, "%s", dso_load__error_str[idx]);