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__OPENEMBEDDED_DEBUGINFO] = 'o',
51 [DSO_BINARY_TYPE__BUILDID_DEBUGINFO] = 'b',
52 [DSO_BINARY_TYPE__SYSTEM_PATH_DSO] = 'd',
53 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE] = 'K',
54 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP] = 'm',
55 [DSO_BINARY_TYPE__GUEST_KALLSYMS] = 'g',
56 [DSO_BINARY_TYPE__GUEST_KMODULE] = 'G',
57 [DSO_BINARY_TYPE__GUEST_KMODULE_COMP] = 'M',
58 [DSO_BINARY_TYPE__GUEST_VMLINUX] = 'V',
61 if (dso == NULL || dso->symtab_type == DSO_BINARY_TYPE__NOT_FOUND)
63 return origin[dso->symtab_type];
66 int dso__read_binary_type_filename(const struct dso *dso,
67 enum dso_binary_type type,
68 char *root_dir, char *filename, size_t size)
70 char build_id_hex[SBUILD_ID_SIZE];
75 case DSO_BINARY_TYPE__DEBUGLINK:
77 const char *last_slash;
78 char dso_dir[PATH_MAX];
79 char symfile[PATH_MAX];
82 len = __symbol__join_symfs(filename, size, dso->long_name);
83 last_slash = filename + len;
84 while (last_slash != filename && *last_slash != '/')
87 strncpy(dso_dir, filename, last_slash - filename);
88 dso_dir[last_slash-filename] = '\0';
90 if (!is_regular_file(filename)) {
95 ret = filename__read_debuglink(filename, symfile, PATH_MAX);
99 /* Check predefined locations where debug file might reside */
101 for (i = 0; i < ARRAY_SIZE(debuglink_paths); i++) {
102 snprintf(filename, size,
103 debuglink_paths[i], dso_dir, symfile);
104 if (is_regular_file(filename)) {
112 case DSO_BINARY_TYPE__BUILD_ID_CACHE:
113 if (dso__build_id_filename(dso, filename, size, false) == NULL)
117 case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO:
118 if (dso__build_id_filename(dso, filename, size, true) == NULL)
122 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
123 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
124 snprintf(filename + len, size - len, "%s.debug", dso->long_name);
127 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
128 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
129 snprintf(filename + len, size - len, "%s", dso->long_name);
132 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
134 const char *last_slash;
137 last_slash = dso->long_name + dso->long_name_len;
138 while (last_slash != dso->long_name && *last_slash != '/')
141 len = __symbol__join_symfs(filename, size, "");
142 dir_size = last_slash - dso->long_name + 2;
143 if (dir_size > (size - len)) {
147 len += scnprintf(filename + len, dir_size, "%s", dso->long_name);
148 len += scnprintf(filename + len , size - len, ".debug%s",
153 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
154 if (!dso->has_build_id) {
159 build_id__sprintf(dso->build_id,
160 sizeof(dso->build_id),
162 len = __symbol__join_symfs(filename, size, "/usr/lib/debug/.build-id/");
163 snprintf(filename + len, size - len, "%.2s/%s.debug",
164 build_id_hex, build_id_hex + 2);
167 case DSO_BINARY_TYPE__VMLINUX:
168 case DSO_BINARY_TYPE__GUEST_VMLINUX:
169 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
170 __symbol__join_symfs(filename, size, dso->long_name);
173 case DSO_BINARY_TYPE__GUEST_KMODULE:
174 case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
175 path__join3(filename, size, symbol_conf.symfs,
176 root_dir, dso->long_name);
179 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
180 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
181 __symbol__join_symfs(filename, size, dso->long_name);
184 case DSO_BINARY_TYPE__KCORE:
185 case DSO_BINARY_TYPE__GUEST_KCORE:
186 snprintf(filename, size, "%s", dso->long_name);
190 case DSO_BINARY_TYPE__KALLSYMS:
191 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
192 case DSO_BINARY_TYPE__JAVA_JIT:
193 case DSO_BINARY_TYPE__BPF_PROG_INFO:
194 case DSO_BINARY_TYPE__BPF_IMAGE:
195 case DSO_BINARY_TYPE__NOT_FOUND:
207 static const struct {
209 int (*decompress)(const char *input, int output);
210 bool (*is_compressed)(const char *input);
212 [COMP_ID__NONE] = { .fmt = NULL, },
213 #ifdef HAVE_ZLIB_SUPPORT
214 { "gz", gzip_decompress_to_file, gzip_is_compressed },
216 #ifdef HAVE_LZMA_SUPPORT
217 { "xz", lzma_decompress_to_file, lzma_is_compressed },
219 { NULL, NULL, NULL },
222 static int is_supported_compression(const char *ext)
226 for (i = 1; compressions[i].fmt; i++) {
227 if (!strcmp(ext, compressions[i].fmt))
230 return COMP_ID__NONE;
233 bool is_kernel_module(const char *pathname, int cpumode)
236 int mode = cpumode & PERF_RECORD_MISC_CPUMODE_MASK;
238 WARN_ONCE(mode != cpumode,
239 "Internal error: passing unmasked cpumode (%x) to is_kernel_module",
243 case PERF_RECORD_MISC_USER:
244 case PERF_RECORD_MISC_HYPERVISOR:
245 case PERF_RECORD_MISC_GUEST_USER:
247 /* Treat PERF_RECORD_MISC_CPUMODE_UNKNOWN as kernel */
249 if (kmod_path__parse(&m, pathname)) {
250 pr_err("Failed to check whether %s is a kernel module or not. Assume it is.",
259 bool dso__needs_decompress(struct dso *dso)
261 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
262 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
265 static int decompress_kmodule(struct dso *dso, const char *name,
266 char *pathname, size_t len)
268 char tmpbuf[] = KMOD_DECOMP_NAME;
271 if (!dso__needs_decompress(dso))
274 if (dso->comp == COMP_ID__NONE)
278 * We have proper compression id for DSO and yet the file
279 * behind the 'name' can still be plain uncompressed object.
281 * The reason is behind the logic we open the DSO object files,
282 * when we try all possible 'debug' objects until we find the
283 * data. So even if the DSO is represented by 'krava.xz' module,
284 * we can end up here opening ~/.debug/....23432432/debug' file
285 * which is not compressed.
287 * To keep this transparent, we detect this and return the file
288 * descriptor to the uncompressed file.
290 if (!compressions[dso->comp].is_compressed(name))
291 return open(name, O_RDONLY);
293 fd = mkstemp(tmpbuf);
295 dso->load_errno = errno;
299 if (compressions[dso->comp].decompress(name, fd)) {
300 dso->load_errno = DSO_LOAD_ERRNO__DECOMPRESSION_FAILURE;
305 if (!pathname || (fd < 0))
308 if (pathname && (fd >= 0))
309 strlcpy(pathname, tmpbuf, len);
314 int dso__decompress_kmodule_fd(struct dso *dso, const char *name)
316 return decompress_kmodule(dso, name, NULL, 0);
319 int dso__decompress_kmodule_path(struct dso *dso, const char *name,
320 char *pathname, size_t len)
322 int fd = decompress_kmodule(dso, name, pathname, len);
325 return fd >= 0 ? 0 : -1;
329 * Parses kernel module specified in @path and updates
332 * @comp - true if @path contains supported compression suffix,
334 * @kmod - true if @path contains '.ko' suffix in right position,
336 * @name - if (@alloc_name && @kmod) is true, it contains strdup-ed base name
337 * of the kernel module without suffixes, otherwise strudup-ed
339 * @ext - if (@alloc_ext && @comp) is true, it contains strdup-ed string
340 * the compression suffix
342 * Returns 0 if there's no strdup error, -ENOMEM otherwise.
344 int __kmod_path__parse(struct kmod_path *m, const char *path,
347 const char *name = strrchr(path, '/');
348 const char *ext = strrchr(path, '.');
349 bool is_simple_name = false;
351 memset(m, 0x0, sizeof(*m));
352 name = name ? name + 1 : path;
355 * '.' is also a valid character for module name. For example:
356 * [aaa.bbb] is a valid module name. '[' should have higher
357 * priority than '.ko' suffix.
359 * The kernel names are from machine__mmap_name. Such
360 * name should belong to kernel itself, not kernel module.
362 if (name[0] == '[') {
363 is_simple_name = true;
364 if ((strncmp(name, "[kernel.kallsyms]", 17) == 0) ||
365 (strncmp(name, "[guest.kernel.kallsyms", 22) == 0) ||
366 (strncmp(name, "[vdso]", 6) == 0) ||
367 (strncmp(name, "[vdso32]", 8) == 0) ||
368 (strncmp(name, "[vdsox32]", 9) == 0) ||
369 (strncmp(name, "[vsyscall]", 10) == 0)) {
376 /* No extension, just return name. */
377 if ((ext == NULL) || is_simple_name) {
379 m->name = strdup(name);
380 return m->name ? 0 : -ENOMEM;
385 m->comp = is_supported_compression(ext + 1);
386 if (m->comp > COMP_ID__NONE)
389 /* Check .ko extension only if there's enough name left. */
391 m->kmod = !strncmp(ext, ".ko", 3);
395 if (asprintf(&m->name, "[%.*s]", (int) (ext - name), name) == -1)
398 if (asprintf(&m->name, "%s", name) == -1)
402 strreplace(m->name, '-', '_');
408 void dso__set_module_info(struct dso *dso, struct kmod_path *m,
409 struct machine *machine)
411 if (machine__is_host(machine))
412 dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE;
414 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE;
416 /* _KMODULE_COMP should be next to _KMODULE */
417 if (m->kmod && m->comp) {
422 dso__set_short_name(dso, strdup(m->name), true);
426 * Global list of open DSOs and the counter.
428 static LIST_HEAD(dso__data_open);
429 static long dso__data_open_cnt;
430 static pthread_mutex_t dso__data_open_lock = PTHREAD_MUTEX_INITIALIZER;
432 static void dso__list_add(struct dso *dso)
434 list_add_tail(&dso->data.open_entry, &dso__data_open);
435 dso__data_open_cnt++;
438 static void dso__list_del(struct dso *dso)
440 list_del_init(&dso->data.open_entry);
441 WARN_ONCE(dso__data_open_cnt <= 0,
442 "DSO data fd counter out of bounds.");
443 dso__data_open_cnt--;
446 static void close_first_dso(void);
448 static int do_open(char *name)
451 char sbuf[STRERR_BUFSIZE];
454 fd = open(name, O_RDONLY|O_CLOEXEC);
458 pr_debug("dso open failed: %s\n",
459 str_error_r(errno, sbuf, sizeof(sbuf)));
460 if (!dso__data_open_cnt || errno != EMFILE)
469 static int __open_dso(struct dso *dso, struct machine *machine)
472 char *root_dir = (char *)"";
473 char *name = malloc(PATH_MAX);
480 root_dir = machine->root_dir;
482 if (dso__read_binary_type_filename(dso, dso->binary_type,
483 root_dir, name, PATH_MAX))
486 if (!is_regular_file(name))
489 if (dso__needs_decompress(dso)) {
490 char newpath[KMOD_DECOMP_LEN];
491 size_t len = sizeof(newpath);
493 if (dso__decompress_kmodule_path(dso, name, newpath, len) < 0) {
494 fd = -dso->load_errno;
499 strcpy(name, newpath);
512 static void check_data_close(void);
515 * dso_close - Open DSO data file
518 * Open @dso's data file descriptor and updates
519 * list/count of open DSO objects.
521 static int open_dso(struct dso *dso, struct machine *machine)
526 if (dso->binary_type != DSO_BINARY_TYPE__BUILD_ID_CACHE)
527 nsinfo__mountns_enter(dso->nsinfo, &nsc);
528 fd = __open_dso(dso, machine);
529 if (dso->binary_type != DSO_BINARY_TYPE__BUILD_ID_CACHE)
530 nsinfo__mountns_exit(&nsc);
535 * Check if we crossed the allowed number
536 * of opened DSOs and close one if needed.
544 static void close_data_fd(struct dso *dso)
546 if (dso->data.fd >= 0) {
549 dso->data.file_size = 0;
555 * dso_close - Close DSO data file
558 * Close @dso's data file descriptor and updates
559 * list/count of open DSO objects.
561 static void close_dso(struct dso *dso)
566 static void close_first_dso(void)
570 dso = list_first_entry(&dso__data_open, struct dso, data.open_entry);
574 static rlim_t get_fd_limit(void)
579 /* Allow half of the current open fd limit. */
580 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
581 if (l.rlim_cur == RLIM_INFINITY)
584 limit = l.rlim_cur / 2;
586 pr_err("failed to get fd limit\n");
593 static rlim_t fd_limit;
596 * Used only by tests/dso-data.c to reset the environment
597 * for tests. I dont expect we should change this during
600 void reset_fd_limit(void)
605 static bool may_cache_fd(void)
608 fd_limit = get_fd_limit();
610 if (fd_limit == RLIM_INFINITY)
613 return fd_limit > (rlim_t) dso__data_open_cnt;
617 * Check and close LRU dso if we crossed allowed limit
618 * for opened dso file descriptors. The limit is half
619 * of the RLIMIT_NOFILE files opened.
621 static void check_data_close(void)
623 bool cache_fd = may_cache_fd();
630 * dso__data_close - Close DSO data file
633 * External interface to close @dso's data file descriptor.
635 void dso__data_close(struct dso *dso)
637 pthread_mutex_lock(&dso__data_open_lock);
639 pthread_mutex_unlock(&dso__data_open_lock);
642 static void try_to_open_dso(struct dso *dso, struct machine *machine)
644 enum dso_binary_type binary_type_data[] = {
645 DSO_BINARY_TYPE__BUILD_ID_CACHE,
646 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
647 DSO_BINARY_TYPE__NOT_FOUND,
651 if (dso->data.fd >= 0)
654 if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) {
655 dso->data.fd = open_dso(dso, machine);
660 dso->binary_type = binary_type_data[i++];
662 dso->data.fd = open_dso(dso, machine);
663 if (dso->data.fd >= 0)
666 } while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND);
668 if (dso->data.fd >= 0)
669 dso->data.status = DSO_DATA_STATUS_OK;
671 dso->data.status = DSO_DATA_STATUS_ERROR;
675 * dso__data_get_fd - Get dso's data file descriptor
677 * @machine: machine object
679 * External interface to find dso's file, open it and
680 * returns file descriptor. It should be paired with
681 * dso__data_put_fd() if it returns non-negative value.
683 int dso__data_get_fd(struct dso *dso, struct machine *machine)
685 if (dso->data.status == DSO_DATA_STATUS_ERROR)
688 if (pthread_mutex_lock(&dso__data_open_lock) < 0)
691 try_to_open_dso(dso, machine);
693 if (dso->data.fd < 0)
694 pthread_mutex_unlock(&dso__data_open_lock);
699 void dso__data_put_fd(struct dso *dso __maybe_unused)
701 pthread_mutex_unlock(&dso__data_open_lock);
704 bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by)
708 if (dso->data.status_seen & flag)
711 dso->data.status_seen |= flag;
716 static ssize_t bpf_read(struct dso *dso, u64 offset, char *data)
718 struct bpf_prog_info_node *node;
719 ssize_t size = DSO__DATA_CACHE_SIZE;
723 node = perf_env__find_bpf_prog_info(dso->bpf_prog.env, dso->bpf_prog.id);
724 if (!node || !node->info_linear) {
725 dso->data.status = DSO_DATA_STATUS_ERROR;
729 len = node->info_linear->info.jited_prog_len;
730 buf = (u8 *)(uintptr_t)node->info_linear->info.jited_prog_insns;
735 size = (ssize_t)min(len - offset, (u64)size);
736 memcpy(data, buf + offset, size);
740 static int bpf_size(struct dso *dso)
742 struct bpf_prog_info_node *node;
744 node = perf_env__find_bpf_prog_info(dso->bpf_prog.env, dso->bpf_prog.id);
745 if (!node || !node->info_linear) {
746 dso->data.status = DSO_DATA_STATUS_ERROR;
750 dso->data.file_size = node->info_linear->info.jited_prog_len;
755 dso_cache__free(struct dso *dso)
757 struct rb_root *root = &dso->data.cache;
758 struct rb_node *next = rb_first(root);
760 pthread_mutex_lock(&dso->lock);
762 struct dso_cache *cache;
764 cache = rb_entry(next, struct dso_cache, rb_node);
765 next = rb_next(&cache->rb_node);
766 rb_erase(&cache->rb_node, root);
769 pthread_mutex_unlock(&dso->lock);
772 static struct dso_cache *__dso_cache__find(struct dso *dso, u64 offset)
774 const struct rb_root *root = &dso->data.cache;
775 struct rb_node * const *p = &root->rb_node;
776 const struct rb_node *parent = NULL;
777 struct dso_cache *cache;
783 cache = rb_entry(parent, struct dso_cache, rb_node);
784 end = cache->offset + DSO__DATA_CACHE_SIZE;
786 if (offset < cache->offset)
788 else if (offset >= end)
797 static struct dso_cache *
798 dso_cache__insert(struct dso *dso, struct dso_cache *new)
800 struct rb_root *root = &dso->data.cache;
801 struct rb_node **p = &root->rb_node;
802 struct rb_node *parent = NULL;
803 struct dso_cache *cache;
804 u64 offset = new->offset;
806 pthread_mutex_lock(&dso->lock);
811 cache = rb_entry(parent, struct dso_cache, rb_node);
812 end = cache->offset + DSO__DATA_CACHE_SIZE;
814 if (offset < cache->offset)
816 else if (offset >= end)
822 rb_link_node(&new->rb_node, parent, p);
823 rb_insert_color(&new->rb_node, root);
827 pthread_mutex_unlock(&dso->lock);
831 static ssize_t dso_cache__memcpy(struct dso_cache *cache, u64 offset, u8 *data,
834 u64 cache_offset = offset - cache->offset;
835 u64 cache_size = min(cache->size - cache_offset, size);
838 memcpy(data, cache->data + cache_offset, cache_size);
840 memcpy(cache->data + cache_offset, data, cache_size);
844 static ssize_t file_read(struct dso *dso, struct machine *machine,
845 u64 offset, char *data)
849 pthread_mutex_lock(&dso__data_open_lock);
852 * dso->data.fd might be closed if other thread opened another
853 * file (dso) due to open file limit (RLIMIT_NOFILE).
855 try_to_open_dso(dso, machine);
857 if (dso->data.fd < 0) {
858 dso->data.status = DSO_DATA_STATUS_ERROR;
863 ret = pread(dso->data.fd, data, DSO__DATA_CACHE_SIZE, offset);
865 pthread_mutex_unlock(&dso__data_open_lock);
869 static struct dso_cache *dso_cache__populate(struct dso *dso,
870 struct machine *machine,
871 u64 offset, ssize_t *ret)
873 u64 cache_offset = offset & DSO__DATA_CACHE_MASK;
874 struct dso_cache *cache;
875 struct dso_cache *old;
877 cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
883 if (dso->binary_type == DSO_BINARY_TYPE__BPF_PROG_INFO)
884 *ret = bpf_read(dso, cache_offset, cache->data);
886 *ret = file_read(dso, machine, cache_offset, cache->data);
893 cache->offset = cache_offset;
896 old = dso_cache__insert(dso, cache);
898 /* we lose the race */
906 static struct dso_cache *dso_cache__find(struct dso *dso,
907 struct machine *machine,
911 struct dso_cache *cache = __dso_cache__find(dso, offset);
913 return cache ? cache : dso_cache__populate(dso, machine, offset, ret);
916 static ssize_t dso_cache_io(struct dso *dso, struct machine *machine,
917 u64 offset, u8 *data, ssize_t size, bool out)
919 struct dso_cache *cache;
922 cache = dso_cache__find(dso, machine, offset, &ret);
926 return dso_cache__memcpy(cache, offset, data, size, out);
930 * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
931 * in the rb_tree. Any read to already cached data is served
932 * by cached data. Writes update the cache only, not the backing file.
934 static ssize_t cached_io(struct dso *dso, struct machine *machine,
935 u64 offset, u8 *data, ssize_t size, bool out)
943 ret = dso_cache_io(dso, machine, offset, p, size, out);
947 /* Reached EOF, return what we have. */
963 static int file_size(struct dso *dso, struct machine *machine)
967 char sbuf[STRERR_BUFSIZE];
969 pthread_mutex_lock(&dso__data_open_lock);
972 * dso->data.fd might be closed if other thread opened another
973 * file (dso) due to open file limit (RLIMIT_NOFILE).
975 try_to_open_dso(dso, machine);
977 if (dso->data.fd < 0) {
979 dso->data.status = DSO_DATA_STATUS_ERROR;
983 if (fstat(dso->data.fd, &st) < 0) {
985 pr_err("dso cache fstat failed: %s\n",
986 str_error_r(errno, sbuf, sizeof(sbuf)));
987 dso->data.status = DSO_DATA_STATUS_ERROR;
990 dso->data.file_size = st.st_size;
993 pthread_mutex_unlock(&dso__data_open_lock);
997 int dso__data_file_size(struct dso *dso, struct machine *machine)
999 if (dso->data.file_size)
1002 if (dso->data.status == DSO_DATA_STATUS_ERROR)
1005 if (dso->binary_type == DSO_BINARY_TYPE__BPF_PROG_INFO)
1006 return bpf_size(dso);
1008 return file_size(dso, machine);
1012 * dso__data_size - Return dso data size
1014 * @machine: machine object
1016 * Return: dso data size
1018 off_t dso__data_size(struct dso *dso, struct machine *machine)
1020 if (dso__data_file_size(dso, machine))
1023 /* For now just estimate dso data size is close to file size */
1024 return dso->data.file_size;
1027 static ssize_t data_read_write_offset(struct dso *dso, struct machine *machine,
1028 u64 offset, u8 *data, ssize_t size,
1031 if (dso__data_file_size(dso, machine))
1034 /* Check the offset sanity. */
1035 if (offset > dso->data.file_size)
1038 if (offset + size < offset)
1041 return cached_io(dso, machine, offset, data, size, out);
1045 * dso__data_read_offset - Read data from dso file offset
1047 * @machine: machine object
1048 * @offset: file offset
1049 * @data: buffer to store data
1050 * @size: size of the @data buffer
1052 * External interface to read data from dso file offset. Open
1053 * dso data file and use cached_read to get the data.
1055 ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
1056 u64 offset, u8 *data, ssize_t size)
1058 if (dso->data.status == DSO_DATA_STATUS_ERROR)
1061 return data_read_write_offset(dso, machine, offset, data, size, true);
1065 * dso__data_read_addr - Read data from dso address
1067 * @machine: machine object
1068 * @add: virtual memory address
1069 * @data: buffer to store data
1070 * @size: size of the @data buffer
1072 * External interface to read data from dso address.
1074 ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
1075 struct machine *machine, u64 addr,
1076 u8 *data, ssize_t size)
1078 u64 offset = map->map_ip(map, addr);
1079 return dso__data_read_offset(dso, machine, offset, data, size);
1083 * dso__data_write_cache_offs - Write data to dso data cache at file offset
1085 * @machine: machine object
1086 * @offset: file offset
1087 * @data: buffer to write
1088 * @size: size of the @data buffer
1090 * Write into the dso file data cache, but do not change the file itself.
1092 ssize_t dso__data_write_cache_offs(struct dso *dso, struct machine *machine,
1093 u64 offset, const u8 *data_in, ssize_t size)
1095 u8 *data = (u8 *)data_in; /* cast away const to use same fns for r/w */
1097 if (dso->data.status == DSO_DATA_STATUS_ERROR)
1100 return data_read_write_offset(dso, machine, offset, data, size, false);
1104 * dso__data_write_cache_addr - Write data to dso data cache at dso address
1106 * @machine: machine object
1107 * @add: virtual memory address
1108 * @data: buffer to write
1109 * @size: size of the @data buffer
1111 * External interface to write into the dso file data cache, but do not change
1114 ssize_t dso__data_write_cache_addr(struct dso *dso, struct map *map,
1115 struct machine *machine, u64 addr,
1116 const u8 *data, ssize_t size)
1118 u64 offset = map->map_ip(map, addr);
1119 return dso__data_write_cache_offs(dso, machine, offset, data, size);
1122 struct map *dso__new_map(const char *name)
1124 struct map *map = NULL;
1125 struct dso *dso = dso__new(name);
1128 map = map__new2(0, dso);
1133 struct dso *machine__findnew_kernel(struct machine *machine, const char *name,
1134 const char *short_name, int dso_type)
1137 * The kernel dso could be created by build_id processing.
1139 struct dso *dso = machine__findnew_dso(machine, name);
1142 * We need to run this in all cases, since during the build_id
1143 * processing we had no idea this was the kernel dso.
1146 dso__set_short_name(dso, short_name, false);
1147 dso->kernel = dso_type;
1153 static void dso__set_long_name_id(struct dso *dso, const char *name, struct dso_id *id, bool name_allocated)
1155 struct rb_root *root = dso->root;
1160 if (dso->long_name_allocated)
1161 free((char *)dso->long_name);
1164 rb_erase(&dso->rb_node, root);
1166 * __dsos__findnew_link_by_longname_id() isn't guaranteed to
1167 * add it back, so a clean removal is required here.
1169 RB_CLEAR_NODE(&dso->rb_node);
1173 dso->long_name = name;
1174 dso->long_name_len = strlen(name);
1175 dso->long_name_allocated = name_allocated;
1178 __dsos__findnew_link_by_longname_id(root, dso, NULL, id);
1181 void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
1183 dso__set_long_name_id(dso, name, NULL, name_allocated);
1186 void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
1191 if (dso->short_name_allocated)
1192 free((char *)dso->short_name);
1194 dso->short_name = name;
1195 dso->short_name_len = strlen(name);
1196 dso->short_name_allocated = name_allocated;
1199 int dso__name_len(const struct dso *dso)
1202 return strlen("[unknown]");
1204 return dso->long_name_len;
1206 return dso->short_name_len;
1209 bool dso__loaded(const struct dso *dso)
1214 bool dso__sorted_by_name(const struct dso *dso)
1216 return dso->sorted_by_name;
1219 void dso__set_sorted_by_name(struct dso *dso)
1221 dso->sorted_by_name = true;
1224 struct dso *dso__new_id(const char *name, struct dso_id *id)
1226 struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
1229 strcpy(dso->name, name);
1232 dso__set_long_name_id(dso, dso->name, id, false);
1233 dso__set_short_name(dso, dso->name, false);
1234 dso->symbols = dso->symbol_names = RB_ROOT_CACHED;
1235 dso->data.cache = RB_ROOT;
1236 dso->inlined_nodes = RB_ROOT_CACHED;
1237 dso->srclines = RB_ROOT_CACHED;
1239 dso->data.status = DSO_DATA_STATUS_UNKNOWN;
1240 dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
1241 dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
1242 dso->is_64_bit = (sizeof(void *) == 8);
1245 dso->sorted_by_name = 0;
1246 dso->has_build_id = 0;
1247 dso->has_srcline = 1;
1249 dso->kernel = DSO_TYPE_USER;
1250 dso->needs_swap = DSO_SWAP__UNSET;
1251 dso->comp = COMP_ID__NONE;
1252 RB_CLEAR_NODE(&dso->rb_node);
1254 INIT_LIST_HEAD(&dso->node);
1255 INIT_LIST_HEAD(&dso->data.open_entry);
1256 pthread_mutex_init(&dso->lock, NULL);
1257 refcount_set(&dso->refcnt, 1);
1263 struct dso *dso__new(const char *name)
1265 return dso__new_id(name, NULL);
1268 void dso__delete(struct dso *dso)
1270 if (!RB_EMPTY_NODE(&dso->rb_node))
1271 pr_err("DSO %s is still in rbtree when being deleted!\n",
1274 /* free inlines first, as they reference symbols */
1275 inlines__tree_delete(&dso->inlined_nodes);
1276 srcline__tree_delete(&dso->srclines);
1277 symbols__delete(&dso->symbols);
1279 if (dso->short_name_allocated) {
1280 zfree((char **)&dso->short_name);
1281 dso->short_name_allocated = false;
1284 if (dso->long_name_allocated) {
1285 zfree((char **)&dso->long_name);
1286 dso->long_name_allocated = false;
1289 dso__data_close(dso);
1290 auxtrace_cache__free(dso->auxtrace_cache);
1291 dso_cache__free(dso);
1293 zfree(&dso->symsrc_filename);
1294 nsinfo__zput(dso->nsinfo);
1295 pthread_mutex_destroy(&dso->lock);
1299 struct dso *dso__get(struct dso *dso)
1302 refcount_inc(&dso->refcnt);
1306 void dso__put(struct dso *dso)
1308 if (dso && refcount_dec_and_test(&dso->refcnt))
1312 void dso__set_build_id(struct dso *dso, void *build_id)
1314 memcpy(dso->build_id, build_id, sizeof(dso->build_id));
1315 dso->has_build_id = 1;
1318 bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
1320 return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
1323 void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
1325 char path[PATH_MAX];
1327 if (machine__is_default_guest(machine))
1329 sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
1330 if (sysfs__read_build_id(path, dso->build_id,
1331 sizeof(dso->build_id)) == 0)
1332 dso->has_build_id = true;
1335 int dso__kernel_module_get_build_id(struct dso *dso,
1336 const char *root_dir)
1338 char filename[PATH_MAX];
1340 * kernel module short names are of the form "[module]" and
1341 * we need just "module" here.
1343 const char *name = dso->short_name + 1;
1345 snprintf(filename, sizeof(filename),
1346 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1347 root_dir, (int)strlen(name) - 1, name);
1349 if (sysfs__read_build_id(filename, dso->build_id,
1350 sizeof(dso->build_id)) == 0)
1351 dso->has_build_id = true;
1356 size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
1358 char sbuild_id[SBUILD_ID_SIZE];
1360 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1361 return fprintf(fp, "%s", sbuild_id);
1364 size_t dso__fprintf(struct dso *dso, FILE *fp)
1367 size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
1369 if (dso->short_name != dso->long_name)
1370 ret += fprintf(fp, "%s, ", dso->long_name);
1371 ret += fprintf(fp, "%sloaded, ", dso__loaded(dso) ? "" : "NOT ");
1372 ret += dso__fprintf_buildid(dso, fp);
1373 ret += fprintf(fp, ")\n");
1374 for (nd = rb_first_cached(&dso->symbols); nd; nd = rb_next(nd)) {
1375 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
1376 ret += symbol__fprintf(pos, fp);
1382 enum dso_type dso__type(struct dso *dso, struct machine *machine)
1385 enum dso_type type = DSO__TYPE_UNKNOWN;
1387 fd = dso__data_get_fd(dso, machine);
1389 type = dso__type_fd(fd);
1390 dso__data_put_fd(dso);
1396 int dso__strerror_load(struct dso *dso, char *buf, size_t buflen)
1398 int idx, errnum = dso->load_errno;
1400 * This must have a same ordering as the enum dso_load_errno.
1402 static const char *dso_load__error_str[] = {
1403 "Internal tools/perf/ library error",
1405 "Can not read build id",
1406 "Mismatching build id",
1407 "Decompression failure",
1410 BUG_ON(buflen == 0);
1413 const char *err = str_error_r(errnum, buf, buflen);
1416 scnprintf(buf, buflen, "%s", err);
1421 if (errnum < __DSO_LOAD_ERRNO__START || errnum >= __DSO_LOAD_ERRNO__END)
1424 idx = errnum - __DSO_LOAD_ERRNO__START;
1425 scnprintf(buf, buflen, "%s", dso_load__error_str[idx]);