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 #ifdef HAVE_LIBBPF_SUPPORT
15 #include <bpf/libbpf.h>
16 #include "bpf-event.h"
17 #include "bpf-utils.h"
21 #include "namespaces.h"
30 #include "util.h" /* O_CLOEXEC for older systems */
35 static const char * const debuglink_paths[] = {
42 char dso__symtab_origin(const struct dso *dso)
44 static const char origin[] = {
45 [DSO_BINARY_TYPE__KALLSYMS] = 'k',
46 [DSO_BINARY_TYPE__VMLINUX] = 'v',
47 [DSO_BINARY_TYPE__JAVA_JIT] = 'j',
48 [DSO_BINARY_TYPE__DEBUGLINK] = 'l',
49 [DSO_BINARY_TYPE__BUILD_ID_CACHE] = 'B',
50 [DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO] = 'D',
51 [DSO_BINARY_TYPE__FEDORA_DEBUGINFO] = 'f',
52 [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO] = 'u',
53 [DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO] = 'x',
54 [DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO] = 'o',
55 [DSO_BINARY_TYPE__BUILDID_DEBUGINFO] = 'b',
56 [DSO_BINARY_TYPE__SYSTEM_PATH_DSO] = 'd',
57 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE] = 'K',
58 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP] = 'm',
59 [DSO_BINARY_TYPE__GUEST_KALLSYMS] = 'g',
60 [DSO_BINARY_TYPE__GUEST_KMODULE] = 'G',
61 [DSO_BINARY_TYPE__GUEST_KMODULE_COMP] = 'M',
62 [DSO_BINARY_TYPE__GUEST_VMLINUX] = 'V',
65 if (dso == NULL || dso->symtab_type == DSO_BINARY_TYPE__NOT_FOUND)
67 return origin[dso->symtab_type];
70 bool dso__is_object_file(const struct dso *dso)
72 switch (dso->binary_type) {
73 case DSO_BINARY_TYPE__KALLSYMS:
74 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
75 case DSO_BINARY_TYPE__JAVA_JIT:
76 case DSO_BINARY_TYPE__BPF_PROG_INFO:
77 case DSO_BINARY_TYPE__BPF_IMAGE:
78 case DSO_BINARY_TYPE__OOL:
80 case DSO_BINARY_TYPE__VMLINUX:
81 case DSO_BINARY_TYPE__GUEST_VMLINUX:
82 case DSO_BINARY_TYPE__DEBUGLINK:
83 case DSO_BINARY_TYPE__BUILD_ID_CACHE:
84 case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO:
85 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
86 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
87 case DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO:
88 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
89 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
90 case DSO_BINARY_TYPE__GUEST_KMODULE:
91 case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
92 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
93 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
94 case DSO_BINARY_TYPE__KCORE:
95 case DSO_BINARY_TYPE__GUEST_KCORE:
96 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
97 case DSO_BINARY_TYPE__NOT_FOUND:
103 int dso__read_binary_type_filename(const struct dso *dso,
104 enum dso_binary_type type,
105 char *root_dir, char *filename, size_t size)
107 char build_id_hex[SBUILD_ID_SIZE];
112 case DSO_BINARY_TYPE__DEBUGLINK:
114 const char *last_slash;
115 char dso_dir[PATH_MAX];
116 char symfile[PATH_MAX];
119 len = __symbol__join_symfs(filename, size, dso->long_name);
120 last_slash = filename + len;
121 while (last_slash != filename && *last_slash != '/')
124 strncpy(dso_dir, filename, last_slash - filename);
125 dso_dir[last_slash-filename] = '\0';
127 if (!is_regular_file(filename)) {
132 ret = filename__read_debuglink(filename, symfile, PATH_MAX);
136 /* Check predefined locations where debug file might reside */
138 for (i = 0; i < ARRAY_SIZE(debuglink_paths); i++) {
139 snprintf(filename, size,
140 debuglink_paths[i], dso_dir, symfile);
141 if (is_regular_file(filename)) {
149 case DSO_BINARY_TYPE__BUILD_ID_CACHE:
150 if (dso__build_id_filename(dso, filename, size, false) == NULL)
154 case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO:
155 if (dso__build_id_filename(dso, filename, size, true) == NULL)
159 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
160 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
161 snprintf(filename + len, size - len, "%s.debug", dso->long_name);
164 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
165 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
166 snprintf(filename + len, size - len, "%s", dso->long_name);
169 case DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO:
171 * Ubuntu can mixup /usr/lib with /lib, putting debuginfo in
172 * /usr/lib/debug/lib when it is expected to be in
173 * /usr/lib/debug/usr/lib
175 if (strlen(dso->long_name) < 9 ||
176 strncmp(dso->long_name, "/usr/lib/", 9)) {
180 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
181 snprintf(filename + len, size - len, "%s", dso->long_name + 4);
184 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
186 const char *last_slash;
189 last_slash = dso->long_name + dso->long_name_len;
190 while (last_slash != dso->long_name && *last_slash != '/')
193 len = __symbol__join_symfs(filename, size, "");
194 dir_size = last_slash - dso->long_name + 2;
195 if (dir_size > (size - len)) {
199 len += scnprintf(filename + len, dir_size, "%s", dso->long_name);
200 len += scnprintf(filename + len , size - len, ".debug%s",
205 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
206 if (!dso->has_build_id) {
211 build_id__sprintf(&dso->bid, build_id_hex);
212 len = __symbol__join_symfs(filename, size, "/usr/lib/debug/.build-id/");
213 snprintf(filename + len, size - len, "%.2s/%s.debug",
214 build_id_hex, build_id_hex + 2);
217 case DSO_BINARY_TYPE__VMLINUX:
218 case DSO_BINARY_TYPE__GUEST_VMLINUX:
219 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
220 __symbol__join_symfs(filename, size, dso->long_name);
223 case DSO_BINARY_TYPE__GUEST_KMODULE:
224 case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
225 path__join3(filename, size, symbol_conf.symfs,
226 root_dir, dso->long_name);
229 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
230 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
231 __symbol__join_symfs(filename, size, dso->long_name);
234 case DSO_BINARY_TYPE__KCORE:
235 case DSO_BINARY_TYPE__GUEST_KCORE:
236 snprintf(filename, size, "%s", dso->long_name);
240 case DSO_BINARY_TYPE__KALLSYMS:
241 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
242 case DSO_BINARY_TYPE__JAVA_JIT:
243 case DSO_BINARY_TYPE__BPF_PROG_INFO:
244 case DSO_BINARY_TYPE__BPF_IMAGE:
245 case DSO_BINARY_TYPE__OOL:
246 case DSO_BINARY_TYPE__NOT_FOUND:
258 static const struct {
260 int (*decompress)(const char *input, int output);
261 bool (*is_compressed)(const char *input);
263 [COMP_ID__NONE] = { .fmt = NULL, },
264 #ifdef HAVE_ZLIB_SUPPORT
265 { "gz", gzip_decompress_to_file, gzip_is_compressed },
267 #ifdef HAVE_LZMA_SUPPORT
268 { "xz", lzma_decompress_to_file, lzma_is_compressed },
270 { NULL, NULL, NULL },
273 static int is_supported_compression(const char *ext)
277 for (i = 1; compressions[i].fmt; i++) {
278 if (!strcmp(ext, compressions[i].fmt))
281 return COMP_ID__NONE;
284 bool is_kernel_module(const char *pathname, int cpumode)
287 int mode = cpumode & PERF_RECORD_MISC_CPUMODE_MASK;
289 WARN_ONCE(mode != cpumode,
290 "Internal error: passing unmasked cpumode (%x) to is_kernel_module",
294 case PERF_RECORD_MISC_USER:
295 case PERF_RECORD_MISC_HYPERVISOR:
296 case PERF_RECORD_MISC_GUEST_USER:
298 /* Treat PERF_RECORD_MISC_CPUMODE_UNKNOWN as kernel */
300 if (kmod_path__parse(&m, pathname)) {
301 pr_err("Failed to check whether %s is a kernel module or not. Assume it is.",
310 bool dso__needs_decompress(struct dso *dso)
312 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
313 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
316 int filename__decompress(const char *name, char *pathname,
317 size_t len, int comp, int *err)
319 char tmpbuf[] = KMOD_DECOMP_NAME;
323 * We have proper compression id for DSO and yet the file
324 * behind the 'name' can still be plain uncompressed object.
326 * The reason is behind the logic we open the DSO object files,
327 * when we try all possible 'debug' objects until we find the
328 * data. So even if the DSO is represented by 'krava.xz' module,
329 * we can end up here opening ~/.debug/....23432432/debug' file
330 * which is not compressed.
332 * To keep this transparent, we detect this and return the file
333 * descriptor to the uncompressed file.
335 if (!compressions[comp].is_compressed(name))
336 return open(name, O_RDONLY);
338 fd = mkstemp(tmpbuf);
344 if (compressions[comp].decompress(name, fd)) {
345 *err = DSO_LOAD_ERRNO__DECOMPRESSION_FAILURE;
350 if (!pathname || (fd < 0))
353 if (pathname && (fd >= 0))
354 strlcpy(pathname, tmpbuf, len);
359 static int decompress_kmodule(struct dso *dso, const char *name,
360 char *pathname, size_t len)
362 if (!dso__needs_decompress(dso))
365 if (dso->comp == COMP_ID__NONE)
368 return filename__decompress(name, pathname, len, dso->comp,
372 int dso__decompress_kmodule_fd(struct dso *dso, const char *name)
374 return decompress_kmodule(dso, name, NULL, 0);
377 int dso__decompress_kmodule_path(struct dso *dso, const char *name,
378 char *pathname, size_t len)
380 int fd = decompress_kmodule(dso, name, pathname, len);
383 return fd >= 0 ? 0 : -1;
387 * Parses kernel module specified in @path and updates
390 * @comp - true if @path contains supported compression suffix,
392 * @kmod - true if @path contains '.ko' suffix in right position,
394 * @name - if (@alloc_name && @kmod) is true, it contains strdup-ed base name
395 * of the kernel module without suffixes, otherwise strudup-ed
397 * @ext - if (@alloc_ext && @comp) is true, it contains strdup-ed string
398 * the compression suffix
400 * Returns 0 if there's no strdup error, -ENOMEM otherwise.
402 int __kmod_path__parse(struct kmod_path *m, const char *path,
405 const char *name = strrchr(path, '/');
406 const char *ext = strrchr(path, '.');
407 bool is_simple_name = false;
409 memset(m, 0x0, sizeof(*m));
410 name = name ? name + 1 : path;
413 * '.' is also a valid character for module name. For example:
414 * [aaa.bbb] is a valid module name. '[' should have higher
415 * priority than '.ko' suffix.
417 * The kernel names are from machine__mmap_name. Such
418 * name should belong to kernel itself, not kernel module.
420 if (name[0] == '[') {
421 is_simple_name = true;
422 if ((strncmp(name, "[kernel.kallsyms]", 17) == 0) ||
423 (strncmp(name, "[guest.kernel.kallsyms", 22) == 0) ||
424 (strncmp(name, "[vdso]", 6) == 0) ||
425 (strncmp(name, "[vdso32]", 8) == 0) ||
426 (strncmp(name, "[vdsox32]", 9) == 0) ||
427 (strncmp(name, "[vsyscall]", 10) == 0)) {
434 /* No extension, just return name. */
435 if ((ext == NULL) || is_simple_name) {
437 m->name = strdup(name);
438 return m->name ? 0 : -ENOMEM;
443 m->comp = is_supported_compression(ext + 1);
444 if (m->comp > COMP_ID__NONE)
447 /* Check .ko extension only if there's enough name left. */
449 m->kmod = !strncmp(ext, ".ko", 3);
453 if (asprintf(&m->name, "[%.*s]", (int) (ext - name), name) == -1)
456 if (asprintf(&m->name, "%s", name) == -1)
460 strreplace(m->name, '-', '_');
466 void dso__set_module_info(struct dso *dso, struct kmod_path *m,
467 struct machine *machine)
469 if (machine__is_host(machine))
470 dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE;
472 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE;
474 /* _KMODULE_COMP should be next to _KMODULE */
475 if (m->kmod && m->comp) {
480 dso__set_short_name(dso, strdup(m->name), true);
484 * Global list of open DSOs and the counter.
486 static LIST_HEAD(dso__data_open);
487 static long dso__data_open_cnt;
488 static pthread_mutex_t dso__data_open_lock = PTHREAD_MUTEX_INITIALIZER;
490 static void dso__list_add(struct dso *dso)
492 list_add_tail(&dso->data.open_entry, &dso__data_open);
493 dso__data_open_cnt++;
496 static void dso__list_del(struct dso *dso)
498 list_del_init(&dso->data.open_entry);
499 WARN_ONCE(dso__data_open_cnt <= 0,
500 "DSO data fd counter out of bounds.");
501 dso__data_open_cnt--;
504 static void close_first_dso(void);
506 static int do_open(char *name)
509 char sbuf[STRERR_BUFSIZE];
512 fd = open(name, O_RDONLY|O_CLOEXEC);
516 pr_debug("dso open failed: %s\n",
517 str_error_r(errno, sbuf, sizeof(sbuf)));
518 if (!dso__data_open_cnt || errno != EMFILE)
527 char *dso__filename_with_chroot(const struct dso *dso, const char *filename)
529 return filename_with_chroot(nsinfo__pid(dso->nsinfo), filename);
532 static int __open_dso(struct dso *dso, struct machine *machine)
535 char *root_dir = (char *)"";
536 char *name = malloc(PATH_MAX);
542 mutex_lock(&dso->lock);
544 root_dir = machine->root_dir;
546 if (dso__read_binary_type_filename(dso, dso->binary_type,
547 root_dir, name, PATH_MAX))
550 if (!is_regular_file(name)) {
553 if (errno != ENOENT || dso->nsinfo == NULL)
556 new_name = dso__filename_with_chroot(dso, name);
564 if (dso__needs_decompress(dso)) {
565 char newpath[KMOD_DECOMP_LEN];
566 size_t len = sizeof(newpath);
568 if (dso__decompress_kmodule_path(dso, name, newpath, len) < 0) {
569 fd = -dso->load_errno;
574 strcpy(name, newpath);
583 mutex_unlock(&dso->lock);
588 static void check_data_close(void);
591 * dso_close - Open DSO data file
594 * Open @dso's data file descriptor and updates
595 * list/count of open DSO objects.
597 static int open_dso(struct dso *dso, struct machine *machine)
602 if (dso->binary_type != DSO_BINARY_TYPE__BUILD_ID_CACHE) {
603 mutex_lock(&dso->lock);
604 nsinfo__mountns_enter(dso->nsinfo, &nsc);
605 mutex_unlock(&dso->lock);
607 fd = __open_dso(dso, machine);
608 if (dso->binary_type != DSO_BINARY_TYPE__BUILD_ID_CACHE)
609 nsinfo__mountns_exit(&nsc);
614 * Check if we crossed the allowed number
615 * of opened DSOs and close one if needed.
623 static void close_data_fd(struct dso *dso)
625 if (dso->data.fd >= 0) {
628 dso->data.file_size = 0;
634 * dso_close - Close DSO data file
637 * Close @dso's data file descriptor and updates
638 * list/count of open DSO objects.
640 static void close_dso(struct dso *dso)
645 static void close_first_dso(void)
649 dso = list_first_entry(&dso__data_open, struct dso, data.open_entry);
653 static rlim_t get_fd_limit(void)
658 /* Allow half of the current open fd limit. */
659 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
660 if (l.rlim_cur == RLIM_INFINITY)
663 limit = l.rlim_cur / 2;
665 pr_err("failed to get fd limit\n");
672 static rlim_t fd_limit;
675 * Used only by tests/dso-data.c to reset the environment
676 * for tests. I dont expect we should change this during
679 void reset_fd_limit(void)
684 static bool may_cache_fd(void)
687 fd_limit = get_fd_limit();
689 if (fd_limit == RLIM_INFINITY)
692 return fd_limit > (rlim_t) dso__data_open_cnt;
696 * Check and close LRU dso if we crossed allowed limit
697 * for opened dso file descriptors. The limit is half
698 * of the RLIMIT_NOFILE files opened.
700 static void check_data_close(void)
702 bool cache_fd = may_cache_fd();
709 * dso__data_close - Close DSO data file
712 * External interface to close @dso's data file descriptor.
714 void dso__data_close(struct dso *dso)
716 pthread_mutex_lock(&dso__data_open_lock);
718 pthread_mutex_unlock(&dso__data_open_lock);
721 static void try_to_open_dso(struct dso *dso, struct machine *machine)
723 enum dso_binary_type binary_type_data[] = {
724 DSO_BINARY_TYPE__BUILD_ID_CACHE,
725 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
726 DSO_BINARY_TYPE__NOT_FOUND,
730 if (dso->data.fd >= 0)
733 if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) {
734 dso->data.fd = open_dso(dso, machine);
739 dso->binary_type = binary_type_data[i++];
741 dso->data.fd = open_dso(dso, machine);
742 if (dso->data.fd >= 0)
745 } while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND);
747 if (dso->data.fd >= 0)
748 dso->data.status = DSO_DATA_STATUS_OK;
750 dso->data.status = DSO_DATA_STATUS_ERROR;
754 * dso__data_get_fd - Get dso's data file descriptor
756 * @machine: machine object
758 * External interface to find dso's file, open it and
759 * returns file descriptor. It should be paired with
760 * dso__data_put_fd() if it returns non-negative value.
762 int dso__data_get_fd(struct dso *dso, struct machine *machine)
764 if (dso->data.status == DSO_DATA_STATUS_ERROR)
767 if (pthread_mutex_lock(&dso__data_open_lock) < 0)
770 try_to_open_dso(dso, machine);
772 if (dso->data.fd < 0)
773 pthread_mutex_unlock(&dso__data_open_lock);
778 void dso__data_put_fd(struct dso *dso __maybe_unused)
780 pthread_mutex_unlock(&dso__data_open_lock);
783 bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by)
787 if (dso->data.status_seen & flag)
790 dso->data.status_seen |= flag;
795 #ifdef HAVE_LIBBPF_SUPPORT
796 static ssize_t bpf_read(struct dso *dso, u64 offset, char *data)
798 struct bpf_prog_info_node *node;
799 ssize_t size = DSO__DATA_CACHE_SIZE;
803 node = perf_env__find_bpf_prog_info(dso->bpf_prog.env, dso->bpf_prog.id);
804 if (!node || !node->info_linear) {
805 dso->data.status = DSO_DATA_STATUS_ERROR;
809 len = node->info_linear->info.jited_prog_len;
810 buf = (u8 *)(uintptr_t)node->info_linear->info.jited_prog_insns;
815 size = (ssize_t)min(len - offset, (u64)size);
816 memcpy(data, buf + offset, size);
820 static int bpf_size(struct dso *dso)
822 struct bpf_prog_info_node *node;
824 node = perf_env__find_bpf_prog_info(dso->bpf_prog.env, dso->bpf_prog.id);
825 if (!node || !node->info_linear) {
826 dso->data.status = DSO_DATA_STATUS_ERROR;
830 dso->data.file_size = node->info_linear->info.jited_prog_len;
833 #endif // HAVE_LIBBPF_SUPPORT
836 dso_cache__free(struct dso *dso)
838 struct rb_root *root = &dso->data.cache;
839 struct rb_node *next = rb_first(root);
841 mutex_lock(&dso->lock);
843 struct dso_cache *cache;
845 cache = rb_entry(next, struct dso_cache, rb_node);
846 next = rb_next(&cache->rb_node);
847 rb_erase(&cache->rb_node, root);
850 mutex_unlock(&dso->lock);
853 static struct dso_cache *__dso_cache__find(struct dso *dso, u64 offset)
855 const struct rb_root *root = &dso->data.cache;
856 struct rb_node * const *p = &root->rb_node;
857 const struct rb_node *parent = NULL;
858 struct dso_cache *cache;
864 cache = rb_entry(parent, struct dso_cache, rb_node);
865 end = cache->offset + DSO__DATA_CACHE_SIZE;
867 if (offset < cache->offset)
869 else if (offset >= end)
878 static struct dso_cache *
879 dso_cache__insert(struct dso *dso, struct dso_cache *new)
881 struct rb_root *root = &dso->data.cache;
882 struct rb_node **p = &root->rb_node;
883 struct rb_node *parent = NULL;
884 struct dso_cache *cache;
885 u64 offset = new->offset;
887 mutex_lock(&dso->lock);
892 cache = rb_entry(parent, struct dso_cache, rb_node);
893 end = cache->offset + DSO__DATA_CACHE_SIZE;
895 if (offset < cache->offset)
897 else if (offset >= end)
903 rb_link_node(&new->rb_node, parent, p);
904 rb_insert_color(&new->rb_node, root);
908 mutex_unlock(&dso->lock);
912 static ssize_t dso_cache__memcpy(struct dso_cache *cache, u64 offset, u8 *data,
915 u64 cache_offset = offset - cache->offset;
916 u64 cache_size = min(cache->size - cache_offset, size);
919 memcpy(data, cache->data + cache_offset, cache_size);
921 memcpy(cache->data + cache_offset, data, cache_size);
925 static ssize_t file_read(struct dso *dso, struct machine *machine,
926 u64 offset, char *data)
930 pthread_mutex_lock(&dso__data_open_lock);
933 * dso->data.fd might be closed if other thread opened another
934 * file (dso) due to open file limit (RLIMIT_NOFILE).
936 try_to_open_dso(dso, machine);
938 if (dso->data.fd < 0) {
939 dso->data.status = DSO_DATA_STATUS_ERROR;
944 ret = pread(dso->data.fd, data, DSO__DATA_CACHE_SIZE, offset);
946 pthread_mutex_unlock(&dso__data_open_lock);
950 static struct dso_cache *dso_cache__populate(struct dso *dso,
951 struct machine *machine,
952 u64 offset, ssize_t *ret)
954 u64 cache_offset = offset & DSO__DATA_CACHE_MASK;
955 struct dso_cache *cache;
956 struct dso_cache *old;
958 cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
963 #ifdef HAVE_LIBBPF_SUPPORT
964 if (dso->binary_type == DSO_BINARY_TYPE__BPF_PROG_INFO)
965 *ret = bpf_read(dso, cache_offset, cache->data);
968 if (dso->binary_type == DSO_BINARY_TYPE__OOL)
969 *ret = DSO__DATA_CACHE_SIZE;
971 *ret = file_read(dso, machine, cache_offset, cache->data);
978 cache->offset = cache_offset;
981 old = dso_cache__insert(dso, cache);
983 /* we lose the race */
991 static struct dso_cache *dso_cache__find(struct dso *dso,
992 struct machine *machine,
996 struct dso_cache *cache = __dso_cache__find(dso, offset);
998 return cache ? cache : dso_cache__populate(dso, machine, offset, ret);
1001 static ssize_t dso_cache_io(struct dso *dso, struct machine *machine,
1002 u64 offset, u8 *data, ssize_t size, bool out)
1004 struct dso_cache *cache;
1007 cache = dso_cache__find(dso, machine, offset, &ret);
1011 return dso_cache__memcpy(cache, offset, data, size, out);
1015 * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
1016 * in the rb_tree. Any read to already cached data is served
1017 * by cached data. Writes update the cache only, not the backing file.
1019 static ssize_t cached_io(struct dso *dso, struct machine *machine,
1020 u64 offset, u8 *data, ssize_t size, bool out)
1028 ret = dso_cache_io(dso, machine, offset, p, size, out);
1032 /* Reached EOF, return what we have. */
1048 static int file_size(struct dso *dso, struct machine *machine)
1052 char sbuf[STRERR_BUFSIZE];
1054 pthread_mutex_lock(&dso__data_open_lock);
1057 * dso->data.fd might be closed if other thread opened another
1058 * file (dso) due to open file limit (RLIMIT_NOFILE).
1060 try_to_open_dso(dso, machine);
1062 if (dso->data.fd < 0) {
1064 dso->data.status = DSO_DATA_STATUS_ERROR;
1068 if (fstat(dso->data.fd, &st) < 0) {
1070 pr_err("dso cache fstat failed: %s\n",
1071 str_error_r(errno, sbuf, sizeof(sbuf)));
1072 dso->data.status = DSO_DATA_STATUS_ERROR;
1075 dso->data.file_size = st.st_size;
1078 pthread_mutex_unlock(&dso__data_open_lock);
1082 int dso__data_file_size(struct dso *dso, struct machine *machine)
1084 if (dso->data.file_size)
1087 if (dso->data.status == DSO_DATA_STATUS_ERROR)
1089 #ifdef HAVE_LIBBPF_SUPPORT
1090 if (dso->binary_type == DSO_BINARY_TYPE__BPF_PROG_INFO)
1091 return bpf_size(dso);
1093 return file_size(dso, machine);
1097 * dso__data_size - Return dso data size
1099 * @machine: machine object
1101 * Return: dso data size
1103 off_t dso__data_size(struct dso *dso, struct machine *machine)
1105 if (dso__data_file_size(dso, machine))
1108 /* For now just estimate dso data size is close to file size */
1109 return dso->data.file_size;
1112 static ssize_t data_read_write_offset(struct dso *dso, struct machine *machine,
1113 u64 offset, u8 *data, ssize_t size,
1116 if (dso__data_file_size(dso, machine))
1119 /* Check the offset sanity. */
1120 if (offset > dso->data.file_size)
1123 if (offset + size < offset)
1126 return cached_io(dso, machine, offset, data, size, out);
1130 * dso__data_read_offset - Read data from dso file offset
1132 * @machine: machine object
1133 * @offset: file offset
1134 * @data: buffer to store data
1135 * @size: size of the @data buffer
1137 * External interface to read data from dso file offset. Open
1138 * dso data file and use cached_read to get the data.
1140 ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
1141 u64 offset, u8 *data, ssize_t size)
1143 if (dso->data.status == DSO_DATA_STATUS_ERROR)
1146 return data_read_write_offset(dso, machine, offset, data, size, true);
1150 * dso__data_read_addr - Read data from dso address
1152 * @machine: machine object
1153 * @add: virtual memory address
1154 * @data: buffer to store data
1155 * @size: size of the @data buffer
1157 * External interface to read data from dso address.
1159 ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
1160 struct machine *machine, u64 addr,
1161 u8 *data, ssize_t size)
1163 u64 offset = map__map_ip(map, addr);
1165 return dso__data_read_offset(dso, machine, offset, data, size);
1169 * dso__data_write_cache_offs - Write data to dso data cache at file offset
1171 * @machine: machine object
1172 * @offset: file offset
1173 * @data: buffer to write
1174 * @size: size of the @data buffer
1176 * Write into the dso file data cache, but do not change the file itself.
1178 ssize_t dso__data_write_cache_offs(struct dso *dso, struct machine *machine,
1179 u64 offset, const u8 *data_in, ssize_t size)
1181 u8 *data = (u8 *)data_in; /* cast away const to use same fns for r/w */
1183 if (dso->data.status == DSO_DATA_STATUS_ERROR)
1186 return data_read_write_offset(dso, machine, offset, data, size, false);
1190 * dso__data_write_cache_addr - Write data to dso data cache at dso address
1192 * @machine: machine object
1193 * @add: virtual memory address
1194 * @data: buffer to write
1195 * @size: size of the @data buffer
1197 * External interface to write into the dso file data cache, but do not change
1200 ssize_t dso__data_write_cache_addr(struct dso *dso, struct map *map,
1201 struct machine *machine, u64 addr,
1202 const u8 *data, ssize_t size)
1204 u64 offset = map__map_ip(map, addr);
1206 return dso__data_write_cache_offs(dso, machine, offset, data, size);
1209 struct map *dso__new_map(const char *name)
1211 struct map *map = NULL;
1212 struct dso *dso = dso__new(name);
1215 map = map__new2(0, dso);
1222 struct dso *machine__findnew_kernel(struct machine *machine, const char *name,
1223 const char *short_name, int dso_type)
1226 * The kernel dso could be created by build_id processing.
1228 struct dso *dso = machine__findnew_dso(machine, name);
1231 * We need to run this in all cases, since during the build_id
1232 * processing we had no idea this was the kernel dso.
1235 dso__set_short_name(dso, short_name, false);
1236 dso->kernel = dso_type;
1242 static void dso__set_long_name_id(struct dso *dso, const char *name, struct dso_id *id, bool name_allocated)
1244 struct rb_root *root = dso->root;
1249 if (dso->long_name_allocated)
1250 free((char *)dso->long_name);
1253 rb_erase(&dso->rb_node, root);
1255 * __dsos__findnew_link_by_longname_id() isn't guaranteed to
1256 * add it back, so a clean removal is required here.
1258 RB_CLEAR_NODE(&dso->rb_node);
1262 dso->long_name = name;
1263 dso->long_name_len = strlen(name);
1264 dso->long_name_allocated = name_allocated;
1267 __dsos__findnew_link_by_longname_id(root, dso, NULL, id);
1270 void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
1272 dso__set_long_name_id(dso, name, NULL, name_allocated);
1275 void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
1280 if (dso->short_name_allocated)
1281 free((char *)dso->short_name);
1283 dso->short_name = name;
1284 dso->short_name_len = strlen(name);
1285 dso->short_name_allocated = name_allocated;
1288 int dso__name_len(const struct dso *dso)
1291 return strlen("[unknown]");
1293 return dso->long_name_len;
1295 return dso->short_name_len;
1298 bool dso__loaded(const struct dso *dso)
1303 bool dso__sorted_by_name(const struct dso *dso)
1305 return dso->sorted_by_name;
1308 void dso__set_sorted_by_name(struct dso *dso)
1310 dso->sorted_by_name = true;
1313 struct dso *dso__new_id(const char *name, struct dso_id *id)
1315 struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
1318 strcpy(dso->name, name);
1321 dso__set_long_name_id(dso, dso->name, id, false);
1322 dso__set_short_name(dso, dso->name, false);
1323 dso->symbols = RB_ROOT_CACHED;
1324 dso->symbol_names = NULL;
1325 dso->symbol_names_len = 0;
1326 dso->data.cache = RB_ROOT;
1327 dso->inlined_nodes = RB_ROOT_CACHED;
1328 dso->srclines = RB_ROOT_CACHED;
1330 dso->data.status = DSO_DATA_STATUS_UNKNOWN;
1331 dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
1332 dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
1333 dso->is_64_bit = (sizeof(void *) == 8);
1336 dso->sorted_by_name = 0;
1337 dso->has_build_id = 0;
1338 dso->has_srcline = 1;
1340 dso->kernel = DSO_SPACE__USER;
1341 dso->needs_swap = DSO_SWAP__UNSET;
1342 dso->comp = COMP_ID__NONE;
1343 RB_CLEAR_NODE(&dso->rb_node);
1345 INIT_LIST_HEAD(&dso->node);
1346 INIT_LIST_HEAD(&dso->data.open_entry);
1347 mutex_init(&dso->lock);
1348 refcount_set(&dso->refcnt, 1);
1354 struct dso *dso__new(const char *name)
1356 return dso__new_id(name, NULL);
1359 void dso__delete(struct dso *dso)
1361 if (!RB_EMPTY_NODE(&dso->rb_node))
1362 pr_err("DSO %s is still in rbtree when being deleted!\n",
1365 /* free inlines first, as they reference symbols */
1366 inlines__tree_delete(&dso->inlined_nodes);
1367 srcline__tree_delete(&dso->srclines);
1368 symbols__delete(&dso->symbols);
1369 dso->symbol_names_len = 0;
1370 zfree(&dso->symbol_names);
1371 if (dso->short_name_allocated) {
1372 zfree((char **)&dso->short_name);
1373 dso->short_name_allocated = false;
1376 if (dso->long_name_allocated) {
1377 zfree((char **)&dso->long_name);
1378 dso->long_name_allocated = false;
1381 dso__data_close(dso);
1382 auxtrace_cache__free(dso->auxtrace_cache);
1383 dso_cache__free(dso);
1385 zfree(&dso->symsrc_filename);
1386 nsinfo__zput(dso->nsinfo);
1387 mutex_destroy(&dso->lock);
1391 struct dso *dso__get(struct dso *dso)
1394 refcount_inc(&dso->refcnt);
1398 void dso__put(struct dso *dso)
1400 if (dso && refcount_dec_and_test(&dso->refcnt))
1404 void dso__set_build_id(struct dso *dso, struct build_id *bid)
1407 dso->has_build_id = 1;
1410 bool dso__build_id_equal(const struct dso *dso, struct build_id *bid)
1412 if (dso->bid.size > bid->size && dso->bid.size == BUILD_ID_SIZE) {
1414 * For the backward compatibility, it allows a build-id has
1417 return !memcmp(dso->bid.data, bid->data, bid->size) &&
1418 !memchr_inv(&dso->bid.data[bid->size], 0,
1419 dso->bid.size - bid->size);
1422 return dso->bid.size == bid->size &&
1423 memcmp(dso->bid.data, bid->data, dso->bid.size) == 0;
1426 void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
1428 char path[PATH_MAX];
1430 if (machine__is_default_guest(machine))
1432 sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
1433 if (sysfs__read_build_id(path, &dso->bid) == 0)
1434 dso->has_build_id = true;
1437 int dso__kernel_module_get_build_id(struct dso *dso,
1438 const char *root_dir)
1440 char filename[PATH_MAX];
1442 * kernel module short names are of the form "[module]" and
1443 * we need just "module" here.
1445 const char *name = dso->short_name + 1;
1447 snprintf(filename, sizeof(filename),
1448 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1449 root_dir, (int)strlen(name) - 1, name);
1451 if (sysfs__read_build_id(filename, &dso->bid) == 0)
1452 dso->has_build_id = true;
1457 static size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
1459 char sbuild_id[SBUILD_ID_SIZE];
1461 build_id__sprintf(&dso->bid, sbuild_id);
1462 return fprintf(fp, "%s", sbuild_id);
1465 size_t dso__fprintf(struct dso *dso, FILE *fp)
1468 size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
1470 if (dso->short_name != dso->long_name)
1471 ret += fprintf(fp, "%s, ", dso->long_name);
1472 ret += fprintf(fp, "%sloaded, ", dso__loaded(dso) ? "" : "NOT ");
1473 ret += dso__fprintf_buildid(dso, fp);
1474 ret += fprintf(fp, ")\n");
1475 for (nd = rb_first_cached(&dso->symbols); nd; nd = rb_next(nd)) {
1476 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
1477 ret += symbol__fprintf(pos, fp);
1483 enum dso_type dso__type(struct dso *dso, struct machine *machine)
1486 enum dso_type type = DSO__TYPE_UNKNOWN;
1488 fd = dso__data_get_fd(dso, machine);
1490 type = dso__type_fd(fd);
1491 dso__data_put_fd(dso);
1497 int dso__strerror_load(struct dso *dso, char *buf, size_t buflen)
1499 int idx, errnum = dso->load_errno;
1501 * This must have a same ordering as the enum dso_load_errno.
1503 static const char *dso_load__error_str[] = {
1504 "Internal tools/perf/ library error",
1506 "Can not read build id",
1507 "Mismatching build id",
1508 "Decompression failure",
1511 BUG_ON(buflen == 0);
1514 const char *err = str_error_r(errnum, buf, buflen);
1517 scnprintf(buf, buflen, "%s", err);
1522 if (errnum < __DSO_LOAD_ERRNO__START || errnum >= __DSO_LOAD_ERRNO__END)
1525 idx = errnum - __DSO_LOAD_ERRNO__START;
1526 scnprintf(buf, buflen, "%s", dso_load__error_str[idx]);