Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / tools / perf / util / dso.c
blobd786cf6b0cfa65f2af5209cb97e36b71774d0d3b
1 // SPDX-License-Identifier: GPL-2.0
2 #include <asm/bug.h>
3 #include <linux/kernel.h>
4 #include <linux/string.h>
5 #include <linux/zalloc.h>
6 #include <sys/time.h>
7 #include <sys/resource.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <unistd.h>
11 #include <errno.h>
12 #include <fcntl.h>
13 #include <stdlib.h>
14 #ifdef HAVE_LIBBPF_SUPPORT
15 #include <bpf/libbpf.h>
16 #include "bpf-event.h"
17 #endif
18 #include "compress.h"
19 #include "env.h"
20 #include "namespaces.h"
21 #include "path.h"
22 #include "map.h"
23 #include "symbol.h"
24 #include "srcline.h"
25 #include "dso.h"
26 #include "dsos.h"
27 #include "machine.h"
28 #include "auxtrace.h"
29 #include "util.h" /* O_CLOEXEC for older systems */
30 #include "debug.h"
31 #include "string2.h"
32 #include "vdso.h"
34 static const char * const debuglink_paths[] = {
35 "%.0s%s",
36 "%s/%s",
37 "%s/.debug/%s",
38 "/usr/lib/debug%s/%s"
41 char dso__symtab_origin(const struct dso *dso)
43 static const char origin[] = {
44 [DSO_BINARY_TYPE__KALLSYMS] = 'k',
45 [DSO_BINARY_TYPE__VMLINUX] = 'v',
46 [DSO_BINARY_TYPE__JAVA_JIT] = 'j',
47 [DSO_BINARY_TYPE__DEBUGLINK] = 'l',
48 [DSO_BINARY_TYPE__BUILD_ID_CACHE] = 'B',
49 [DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO] = 'D',
50 [DSO_BINARY_TYPE__FEDORA_DEBUGINFO] = 'f',
51 [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO] = 'u',
52 [DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO] = 'x',
53 [DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO] = 'o',
54 [DSO_BINARY_TYPE__BUILDID_DEBUGINFO] = 'b',
55 [DSO_BINARY_TYPE__SYSTEM_PATH_DSO] = 'd',
56 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE] = 'K',
57 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP] = 'm',
58 [DSO_BINARY_TYPE__GUEST_KALLSYMS] = 'g',
59 [DSO_BINARY_TYPE__GUEST_KMODULE] = 'G',
60 [DSO_BINARY_TYPE__GUEST_KMODULE_COMP] = 'M',
61 [DSO_BINARY_TYPE__GUEST_VMLINUX] = 'V',
64 if (dso == NULL || dso->symtab_type == DSO_BINARY_TYPE__NOT_FOUND)
65 return '!';
66 return origin[dso->symtab_type];
69 int dso__read_binary_type_filename(const struct dso *dso,
70 enum dso_binary_type type,
71 char *root_dir, char *filename, size_t size)
73 char build_id_hex[SBUILD_ID_SIZE];
74 int ret = 0;
75 size_t len;
77 switch (type) {
78 case DSO_BINARY_TYPE__DEBUGLINK:
80 const char *last_slash;
81 char dso_dir[PATH_MAX];
82 char symfile[PATH_MAX];
83 unsigned int i;
85 len = __symbol__join_symfs(filename, size, dso->long_name);
86 last_slash = filename + len;
87 while (last_slash != filename && *last_slash != '/')
88 last_slash--;
90 strncpy(dso_dir, filename, last_slash - filename);
91 dso_dir[last_slash-filename] = '\0';
93 if (!is_regular_file(filename)) {
94 ret = -1;
95 break;
98 ret = filename__read_debuglink(filename, symfile, PATH_MAX);
99 if (ret)
100 break;
102 /* Check predefined locations where debug file might reside */
103 ret = -1;
104 for (i = 0; i < ARRAY_SIZE(debuglink_paths); i++) {
105 snprintf(filename, size,
106 debuglink_paths[i], dso_dir, symfile);
107 if (is_regular_file(filename)) {
108 ret = 0;
109 break;
113 break;
115 case DSO_BINARY_TYPE__BUILD_ID_CACHE:
116 if (dso__build_id_filename(dso, filename, size, false) == NULL)
117 ret = -1;
118 break;
120 case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO:
121 if (dso__build_id_filename(dso, filename, size, true) == NULL)
122 ret = -1;
123 break;
125 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
126 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
127 snprintf(filename + len, size - len, "%s.debug", dso->long_name);
128 break;
130 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
131 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
132 snprintf(filename + len, size - len, "%s", dso->long_name);
133 break;
135 case DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO:
137 * Ubuntu can mixup /usr/lib with /lib, putting debuginfo in
138 * /usr/lib/debug/lib when it is expected to be in
139 * /usr/lib/debug/usr/lib
141 if (strlen(dso->long_name) < 9 ||
142 strncmp(dso->long_name, "/usr/lib/", 9)) {
143 ret = -1;
144 break;
146 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
147 snprintf(filename + len, size - len, "%s", dso->long_name + 4);
148 break;
150 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
152 const char *last_slash;
153 size_t dir_size;
155 last_slash = dso->long_name + dso->long_name_len;
156 while (last_slash != dso->long_name && *last_slash != '/')
157 last_slash--;
159 len = __symbol__join_symfs(filename, size, "");
160 dir_size = last_slash - dso->long_name + 2;
161 if (dir_size > (size - len)) {
162 ret = -1;
163 break;
165 len += scnprintf(filename + len, dir_size, "%s", dso->long_name);
166 len += scnprintf(filename + len , size - len, ".debug%s",
167 last_slash);
168 break;
171 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
172 if (!dso->has_build_id) {
173 ret = -1;
174 break;
177 build_id__sprintf(&dso->bid, build_id_hex);
178 len = __symbol__join_symfs(filename, size, "/usr/lib/debug/.build-id/");
179 snprintf(filename + len, size - len, "%.2s/%s.debug",
180 build_id_hex, build_id_hex + 2);
181 break;
183 case DSO_BINARY_TYPE__VMLINUX:
184 case DSO_BINARY_TYPE__GUEST_VMLINUX:
185 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
186 __symbol__join_symfs(filename, size, dso->long_name);
187 break;
189 case DSO_BINARY_TYPE__GUEST_KMODULE:
190 case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
191 path__join3(filename, size, symbol_conf.symfs,
192 root_dir, dso->long_name);
193 break;
195 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
196 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
197 __symbol__join_symfs(filename, size, dso->long_name);
198 break;
200 case DSO_BINARY_TYPE__KCORE:
201 case DSO_BINARY_TYPE__GUEST_KCORE:
202 snprintf(filename, size, "%s", dso->long_name);
203 break;
205 default:
206 case DSO_BINARY_TYPE__KALLSYMS:
207 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
208 case DSO_BINARY_TYPE__JAVA_JIT:
209 case DSO_BINARY_TYPE__BPF_PROG_INFO:
210 case DSO_BINARY_TYPE__BPF_IMAGE:
211 case DSO_BINARY_TYPE__OOL:
212 case DSO_BINARY_TYPE__NOT_FOUND:
213 ret = -1;
214 break;
217 return ret;
220 enum {
221 COMP_ID__NONE = 0,
224 static const struct {
225 const char *fmt;
226 int (*decompress)(const char *input, int output);
227 bool (*is_compressed)(const char *input);
228 } compressions[] = {
229 [COMP_ID__NONE] = { .fmt = NULL, },
230 #ifdef HAVE_ZLIB_SUPPORT
231 { "gz", gzip_decompress_to_file, gzip_is_compressed },
232 #endif
233 #ifdef HAVE_LZMA_SUPPORT
234 { "xz", lzma_decompress_to_file, lzma_is_compressed },
235 #endif
236 { NULL, NULL, NULL },
239 static int is_supported_compression(const char *ext)
241 unsigned i;
243 for (i = 1; compressions[i].fmt; i++) {
244 if (!strcmp(ext, compressions[i].fmt))
245 return i;
247 return COMP_ID__NONE;
250 bool is_kernel_module(const char *pathname, int cpumode)
252 struct kmod_path m;
253 int mode = cpumode & PERF_RECORD_MISC_CPUMODE_MASK;
255 WARN_ONCE(mode != cpumode,
256 "Internal error: passing unmasked cpumode (%x) to is_kernel_module",
257 cpumode);
259 switch (mode) {
260 case PERF_RECORD_MISC_USER:
261 case PERF_RECORD_MISC_HYPERVISOR:
262 case PERF_RECORD_MISC_GUEST_USER:
263 return false;
264 /* Treat PERF_RECORD_MISC_CPUMODE_UNKNOWN as kernel */
265 default:
266 if (kmod_path__parse(&m, pathname)) {
267 pr_err("Failed to check whether %s is a kernel module or not. Assume it is.",
268 pathname);
269 return true;
273 return m.kmod;
276 bool dso__needs_decompress(struct dso *dso)
278 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
279 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
282 int filename__decompress(const char *name, char *pathname,
283 size_t len, int comp, int *err)
285 char tmpbuf[] = KMOD_DECOMP_NAME;
286 int fd = -1;
289 * We have proper compression id for DSO and yet the file
290 * behind the 'name' can still be plain uncompressed object.
292 * The reason is behind the logic we open the DSO object files,
293 * when we try all possible 'debug' objects until we find the
294 * data. So even if the DSO is represented by 'krava.xz' module,
295 * we can end up here opening ~/.debug/....23432432/debug' file
296 * which is not compressed.
298 * To keep this transparent, we detect this and return the file
299 * descriptor to the uncompressed file.
301 if (!compressions[comp].is_compressed(name))
302 return open(name, O_RDONLY);
304 fd = mkstemp(tmpbuf);
305 if (fd < 0) {
306 *err = errno;
307 return -1;
310 if (compressions[comp].decompress(name, fd)) {
311 *err = DSO_LOAD_ERRNO__DECOMPRESSION_FAILURE;
312 close(fd);
313 fd = -1;
316 if (!pathname || (fd < 0))
317 unlink(tmpbuf);
319 if (pathname && (fd >= 0))
320 strlcpy(pathname, tmpbuf, len);
322 return fd;
325 static int decompress_kmodule(struct dso *dso, const char *name,
326 char *pathname, size_t len)
328 if (!dso__needs_decompress(dso))
329 return -1;
331 if (dso->comp == COMP_ID__NONE)
332 return -1;
334 return filename__decompress(name, pathname, len, dso->comp,
335 &dso->load_errno);
338 int dso__decompress_kmodule_fd(struct dso *dso, const char *name)
340 return decompress_kmodule(dso, name, NULL, 0);
343 int dso__decompress_kmodule_path(struct dso *dso, const char *name,
344 char *pathname, size_t len)
346 int fd = decompress_kmodule(dso, name, pathname, len);
348 close(fd);
349 return fd >= 0 ? 0 : -1;
353 * Parses kernel module specified in @path and updates
354 * @m argument like:
356 * @comp - true if @path contains supported compression suffix,
357 * false otherwise
358 * @kmod - true if @path contains '.ko' suffix in right position,
359 * false otherwise
360 * @name - if (@alloc_name && @kmod) is true, it contains strdup-ed base name
361 * of the kernel module without suffixes, otherwise strudup-ed
362 * base name of @path
363 * @ext - if (@alloc_ext && @comp) is true, it contains strdup-ed string
364 * the compression suffix
366 * Returns 0 if there's no strdup error, -ENOMEM otherwise.
368 int __kmod_path__parse(struct kmod_path *m, const char *path,
369 bool alloc_name)
371 const char *name = strrchr(path, '/');
372 const char *ext = strrchr(path, '.');
373 bool is_simple_name = false;
375 memset(m, 0x0, sizeof(*m));
376 name = name ? name + 1 : path;
379 * '.' is also a valid character for module name. For example:
380 * [aaa.bbb] is a valid module name. '[' should have higher
381 * priority than '.ko' suffix.
383 * The kernel names are from machine__mmap_name. Such
384 * name should belong to kernel itself, not kernel module.
386 if (name[0] == '[') {
387 is_simple_name = true;
388 if ((strncmp(name, "[kernel.kallsyms]", 17) == 0) ||
389 (strncmp(name, "[guest.kernel.kallsyms", 22) == 0) ||
390 (strncmp(name, "[vdso]", 6) == 0) ||
391 (strncmp(name, "[vdso32]", 8) == 0) ||
392 (strncmp(name, "[vdsox32]", 9) == 0) ||
393 (strncmp(name, "[vsyscall]", 10) == 0)) {
394 m->kmod = false;
396 } else
397 m->kmod = true;
400 /* No extension, just return name. */
401 if ((ext == NULL) || is_simple_name) {
402 if (alloc_name) {
403 m->name = strdup(name);
404 return m->name ? 0 : -ENOMEM;
406 return 0;
409 m->comp = is_supported_compression(ext + 1);
410 if (m->comp > COMP_ID__NONE)
411 ext -= 3;
413 /* Check .ko extension only if there's enough name left. */
414 if (ext > name)
415 m->kmod = !strncmp(ext, ".ko", 3);
417 if (alloc_name) {
418 if (m->kmod) {
419 if (asprintf(&m->name, "[%.*s]", (int) (ext - name), name) == -1)
420 return -ENOMEM;
421 } else {
422 if (asprintf(&m->name, "%s", name) == -1)
423 return -ENOMEM;
426 strreplace(m->name, '-', '_');
429 return 0;
432 void dso__set_module_info(struct dso *dso, struct kmod_path *m,
433 struct machine *machine)
435 if (machine__is_host(machine))
436 dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE;
437 else
438 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE;
440 /* _KMODULE_COMP should be next to _KMODULE */
441 if (m->kmod && m->comp) {
442 dso->symtab_type++;
443 dso->comp = m->comp;
446 dso__set_short_name(dso, strdup(m->name), true);
450 * Global list of open DSOs and the counter.
452 static LIST_HEAD(dso__data_open);
453 static long dso__data_open_cnt;
454 static pthread_mutex_t dso__data_open_lock = PTHREAD_MUTEX_INITIALIZER;
456 static void dso__list_add(struct dso *dso)
458 list_add_tail(&dso->data.open_entry, &dso__data_open);
459 dso__data_open_cnt++;
462 static void dso__list_del(struct dso *dso)
464 list_del_init(&dso->data.open_entry);
465 WARN_ONCE(dso__data_open_cnt <= 0,
466 "DSO data fd counter out of bounds.");
467 dso__data_open_cnt--;
470 static void close_first_dso(void);
472 static int do_open(char *name)
474 int fd;
475 char sbuf[STRERR_BUFSIZE];
477 do {
478 fd = open(name, O_RDONLY|O_CLOEXEC);
479 if (fd >= 0)
480 return fd;
482 pr_debug("dso open failed: %s\n",
483 str_error_r(errno, sbuf, sizeof(sbuf)));
484 if (!dso__data_open_cnt || errno != EMFILE)
485 break;
487 close_first_dso();
488 } while (1);
490 return -1;
493 static int __open_dso(struct dso *dso, struct machine *machine)
495 int fd = -EINVAL;
496 char *root_dir = (char *)"";
497 char *name = malloc(PATH_MAX);
498 bool decomp = false;
500 if (!name)
501 return -ENOMEM;
503 if (machine)
504 root_dir = machine->root_dir;
506 if (dso__read_binary_type_filename(dso, dso->binary_type,
507 root_dir, name, PATH_MAX))
508 goto out;
510 if (!is_regular_file(name))
511 goto out;
513 if (dso__needs_decompress(dso)) {
514 char newpath[KMOD_DECOMP_LEN];
515 size_t len = sizeof(newpath);
517 if (dso__decompress_kmodule_path(dso, name, newpath, len) < 0) {
518 fd = -dso->load_errno;
519 goto out;
522 decomp = true;
523 strcpy(name, newpath);
526 fd = do_open(name);
528 if (decomp)
529 unlink(name);
531 out:
532 free(name);
533 return fd;
536 static void check_data_close(void);
539 * dso_close - Open DSO data file
540 * @dso: dso object
542 * Open @dso's data file descriptor and updates
543 * list/count of open DSO objects.
545 static int open_dso(struct dso *dso, struct machine *machine)
547 int fd;
548 struct nscookie nsc;
550 if (dso->binary_type != DSO_BINARY_TYPE__BUILD_ID_CACHE)
551 nsinfo__mountns_enter(dso->nsinfo, &nsc);
552 fd = __open_dso(dso, machine);
553 if (dso->binary_type != DSO_BINARY_TYPE__BUILD_ID_CACHE)
554 nsinfo__mountns_exit(&nsc);
556 if (fd >= 0) {
557 dso__list_add(dso);
559 * Check if we crossed the allowed number
560 * of opened DSOs and close one if needed.
562 check_data_close();
565 return fd;
568 static void close_data_fd(struct dso *dso)
570 if (dso->data.fd >= 0) {
571 close(dso->data.fd);
572 dso->data.fd = -1;
573 dso->data.file_size = 0;
574 dso__list_del(dso);
579 * dso_close - Close DSO data file
580 * @dso: dso object
582 * Close @dso's data file descriptor and updates
583 * list/count of open DSO objects.
585 static void close_dso(struct dso *dso)
587 close_data_fd(dso);
590 static void close_first_dso(void)
592 struct dso *dso;
594 dso = list_first_entry(&dso__data_open, struct dso, data.open_entry);
595 close_dso(dso);
598 static rlim_t get_fd_limit(void)
600 struct rlimit l;
601 rlim_t limit = 0;
603 /* Allow half of the current open fd limit. */
604 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
605 if (l.rlim_cur == RLIM_INFINITY)
606 limit = l.rlim_cur;
607 else
608 limit = l.rlim_cur / 2;
609 } else {
610 pr_err("failed to get fd limit\n");
611 limit = 1;
614 return limit;
617 static rlim_t fd_limit;
620 * Used only by tests/dso-data.c to reset the environment
621 * for tests. I dont expect we should change this during
622 * standard runtime.
624 void reset_fd_limit(void)
626 fd_limit = 0;
629 static bool may_cache_fd(void)
631 if (!fd_limit)
632 fd_limit = get_fd_limit();
634 if (fd_limit == RLIM_INFINITY)
635 return true;
637 return fd_limit > (rlim_t) dso__data_open_cnt;
641 * Check and close LRU dso if we crossed allowed limit
642 * for opened dso file descriptors. The limit is half
643 * of the RLIMIT_NOFILE files opened.
645 static void check_data_close(void)
647 bool cache_fd = may_cache_fd();
649 if (!cache_fd)
650 close_first_dso();
654 * dso__data_close - Close DSO data file
655 * @dso: dso object
657 * External interface to close @dso's data file descriptor.
659 void dso__data_close(struct dso *dso)
661 pthread_mutex_lock(&dso__data_open_lock);
662 close_dso(dso);
663 pthread_mutex_unlock(&dso__data_open_lock);
666 static void try_to_open_dso(struct dso *dso, struct machine *machine)
668 enum dso_binary_type binary_type_data[] = {
669 DSO_BINARY_TYPE__BUILD_ID_CACHE,
670 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
671 DSO_BINARY_TYPE__NOT_FOUND,
673 int i = 0;
675 if (dso->data.fd >= 0)
676 return;
678 if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) {
679 dso->data.fd = open_dso(dso, machine);
680 goto out;
683 do {
684 dso->binary_type = binary_type_data[i++];
686 dso->data.fd = open_dso(dso, machine);
687 if (dso->data.fd >= 0)
688 goto out;
690 } while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND);
691 out:
692 if (dso->data.fd >= 0)
693 dso->data.status = DSO_DATA_STATUS_OK;
694 else
695 dso->data.status = DSO_DATA_STATUS_ERROR;
699 * dso__data_get_fd - Get dso's data file descriptor
700 * @dso: dso object
701 * @machine: machine object
703 * External interface to find dso's file, open it and
704 * returns file descriptor. It should be paired with
705 * dso__data_put_fd() if it returns non-negative value.
707 int dso__data_get_fd(struct dso *dso, struct machine *machine)
709 if (dso->data.status == DSO_DATA_STATUS_ERROR)
710 return -1;
712 if (pthread_mutex_lock(&dso__data_open_lock) < 0)
713 return -1;
715 try_to_open_dso(dso, machine);
717 if (dso->data.fd < 0)
718 pthread_mutex_unlock(&dso__data_open_lock);
720 return dso->data.fd;
723 void dso__data_put_fd(struct dso *dso __maybe_unused)
725 pthread_mutex_unlock(&dso__data_open_lock);
728 bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by)
730 u32 flag = 1 << by;
732 if (dso->data.status_seen & flag)
733 return true;
735 dso->data.status_seen |= flag;
737 return false;
740 #ifdef HAVE_LIBBPF_SUPPORT
741 static ssize_t bpf_read(struct dso *dso, u64 offset, char *data)
743 struct bpf_prog_info_node *node;
744 ssize_t size = DSO__DATA_CACHE_SIZE;
745 u64 len;
746 u8 *buf;
748 node = perf_env__find_bpf_prog_info(dso->bpf_prog.env, dso->bpf_prog.id);
749 if (!node || !node->info_linear) {
750 dso->data.status = DSO_DATA_STATUS_ERROR;
751 return -1;
754 len = node->info_linear->info.jited_prog_len;
755 buf = (u8 *)(uintptr_t)node->info_linear->info.jited_prog_insns;
757 if (offset >= len)
758 return -1;
760 size = (ssize_t)min(len - offset, (u64)size);
761 memcpy(data, buf + offset, size);
762 return size;
765 static int bpf_size(struct dso *dso)
767 struct bpf_prog_info_node *node;
769 node = perf_env__find_bpf_prog_info(dso->bpf_prog.env, dso->bpf_prog.id);
770 if (!node || !node->info_linear) {
771 dso->data.status = DSO_DATA_STATUS_ERROR;
772 return -1;
775 dso->data.file_size = node->info_linear->info.jited_prog_len;
776 return 0;
778 #endif // HAVE_LIBBPF_SUPPORT
780 static void
781 dso_cache__free(struct dso *dso)
783 struct rb_root *root = &dso->data.cache;
784 struct rb_node *next = rb_first(root);
786 pthread_mutex_lock(&dso->lock);
787 while (next) {
788 struct dso_cache *cache;
790 cache = rb_entry(next, struct dso_cache, rb_node);
791 next = rb_next(&cache->rb_node);
792 rb_erase(&cache->rb_node, root);
793 free(cache);
795 pthread_mutex_unlock(&dso->lock);
798 static struct dso_cache *__dso_cache__find(struct dso *dso, u64 offset)
800 const struct rb_root *root = &dso->data.cache;
801 struct rb_node * const *p = &root->rb_node;
802 const struct rb_node *parent = NULL;
803 struct dso_cache *cache;
805 while (*p != NULL) {
806 u64 end;
808 parent = *p;
809 cache = rb_entry(parent, struct dso_cache, rb_node);
810 end = cache->offset + DSO__DATA_CACHE_SIZE;
812 if (offset < cache->offset)
813 p = &(*p)->rb_left;
814 else if (offset >= end)
815 p = &(*p)->rb_right;
816 else
817 return cache;
820 return NULL;
823 static struct dso_cache *
824 dso_cache__insert(struct dso *dso, struct dso_cache *new)
826 struct rb_root *root = &dso->data.cache;
827 struct rb_node **p = &root->rb_node;
828 struct rb_node *parent = NULL;
829 struct dso_cache *cache;
830 u64 offset = new->offset;
832 pthread_mutex_lock(&dso->lock);
833 while (*p != NULL) {
834 u64 end;
836 parent = *p;
837 cache = rb_entry(parent, struct dso_cache, rb_node);
838 end = cache->offset + DSO__DATA_CACHE_SIZE;
840 if (offset < cache->offset)
841 p = &(*p)->rb_left;
842 else if (offset >= end)
843 p = &(*p)->rb_right;
844 else
845 goto out;
848 rb_link_node(&new->rb_node, parent, p);
849 rb_insert_color(&new->rb_node, root);
851 cache = NULL;
852 out:
853 pthread_mutex_unlock(&dso->lock);
854 return cache;
857 static ssize_t dso_cache__memcpy(struct dso_cache *cache, u64 offset, u8 *data,
858 u64 size, bool out)
860 u64 cache_offset = offset - cache->offset;
861 u64 cache_size = min(cache->size - cache_offset, size);
863 if (out)
864 memcpy(data, cache->data + cache_offset, cache_size);
865 else
866 memcpy(cache->data + cache_offset, data, cache_size);
867 return cache_size;
870 static ssize_t file_read(struct dso *dso, struct machine *machine,
871 u64 offset, char *data)
873 ssize_t ret;
875 pthread_mutex_lock(&dso__data_open_lock);
878 * dso->data.fd might be closed if other thread opened another
879 * file (dso) due to open file limit (RLIMIT_NOFILE).
881 try_to_open_dso(dso, machine);
883 if (dso->data.fd < 0) {
884 dso->data.status = DSO_DATA_STATUS_ERROR;
885 ret = -errno;
886 goto out;
889 ret = pread(dso->data.fd, data, DSO__DATA_CACHE_SIZE, offset);
890 out:
891 pthread_mutex_unlock(&dso__data_open_lock);
892 return ret;
895 static struct dso_cache *dso_cache__populate(struct dso *dso,
896 struct machine *machine,
897 u64 offset, ssize_t *ret)
899 u64 cache_offset = offset & DSO__DATA_CACHE_MASK;
900 struct dso_cache *cache;
901 struct dso_cache *old;
903 cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
904 if (!cache) {
905 *ret = -ENOMEM;
906 return NULL;
908 #ifdef HAVE_LIBBPF_SUPPORT
909 if (dso->binary_type == DSO_BINARY_TYPE__BPF_PROG_INFO)
910 *ret = bpf_read(dso, cache_offset, cache->data);
911 else
912 #endif
913 if (dso->binary_type == DSO_BINARY_TYPE__OOL)
914 *ret = DSO__DATA_CACHE_SIZE;
915 else
916 *ret = file_read(dso, machine, cache_offset, cache->data);
918 if (*ret <= 0) {
919 free(cache);
920 return NULL;
923 cache->offset = cache_offset;
924 cache->size = *ret;
926 old = dso_cache__insert(dso, cache);
927 if (old) {
928 /* we lose the race */
929 free(cache);
930 cache = old;
933 return cache;
936 static struct dso_cache *dso_cache__find(struct dso *dso,
937 struct machine *machine,
938 u64 offset,
939 ssize_t *ret)
941 struct dso_cache *cache = __dso_cache__find(dso, offset);
943 return cache ? cache : dso_cache__populate(dso, machine, offset, ret);
946 static ssize_t dso_cache_io(struct dso *dso, struct machine *machine,
947 u64 offset, u8 *data, ssize_t size, bool out)
949 struct dso_cache *cache;
950 ssize_t ret = 0;
952 cache = dso_cache__find(dso, machine, offset, &ret);
953 if (!cache)
954 return ret;
956 return dso_cache__memcpy(cache, offset, data, size, out);
960 * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
961 * in the rb_tree. Any read to already cached data is served
962 * by cached data. Writes update the cache only, not the backing file.
964 static ssize_t cached_io(struct dso *dso, struct machine *machine,
965 u64 offset, u8 *data, ssize_t size, bool out)
967 ssize_t r = 0;
968 u8 *p = data;
970 do {
971 ssize_t ret;
973 ret = dso_cache_io(dso, machine, offset, p, size, out);
974 if (ret < 0)
975 return ret;
977 /* Reached EOF, return what we have. */
978 if (!ret)
979 break;
981 BUG_ON(ret > size);
983 r += ret;
984 p += ret;
985 offset += ret;
986 size -= ret;
988 } while (size);
990 return r;
993 static int file_size(struct dso *dso, struct machine *machine)
995 int ret = 0;
996 struct stat st;
997 char sbuf[STRERR_BUFSIZE];
999 pthread_mutex_lock(&dso__data_open_lock);
1002 * dso->data.fd might be closed if other thread opened another
1003 * file (dso) due to open file limit (RLIMIT_NOFILE).
1005 try_to_open_dso(dso, machine);
1007 if (dso->data.fd < 0) {
1008 ret = -errno;
1009 dso->data.status = DSO_DATA_STATUS_ERROR;
1010 goto out;
1013 if (fstat(dso->data.fd, &st) < 0) {
1014 ret = -errno;
1015 pr_err("dso cache fstat failed: %s\n",
1016 str_error_r(errno, sbuf, sizeof(sbuf)));
1017 dso->data.status = DSO_DATA_STATUS_ERROR;
1018 goto out;
1020 dso->data.file_size = st.st_size;
1022 out:
1023 pthread_mutex_unlock(&dso__data_open_lock);
1024 return ret;
1027 int dso__data_file_size(struct dso *dso, struct machine *machine)
1029 if (dso->data.file_size)
1030 return 0;
1032 if (dso->data.status == DSO_DATA_STATUS_ERROR)
1033 return -1;
1034 #ifdef HAVE_LIBBPF_SUPPORT
1035 if (dso->binary_type == DSO_BINARY_TYPE__BPF_PROG_INFO)
1036 return bpf_size(dso);
1037 #endif
1038 return file_size(dso, machine);
1042 * dso__data_size - Return dso data size
1043 * @dso: dso object
1044 * @machine: machine object
1046 * Return: dso data size
1048 off_t dso__data_size(struct dso *dso, struct machine *machine)
1050 if (dso__data_file_size(dso, machine))
1051 return -1;
1053 /* For now just estimate dso data size is close to file size */
1054 return dso->data.file_size;
1057 static ssize_t data_read_write_offset(struct dso *dso, struct machine *machine,
1058 u64 offset, u8 *data, ssize_t size,
1059 bool out)
1061 if (dso__data_file_size(dso, machine))
1062 return -1;
1064 /* Check the offset sanity. */
1065 if (offset > dso->data.file_size)
1066 return -1;
1068 if (offset + size < offset)
1069 return -1;
1071 return cached_io(dso, machine, offset, data, size, out);
1075 * dso__data_read_offset - Read data from dso file offset
1076 * @dso: dso object
1077 * @machine: machine object
1078 * @offset: file offset
1079 * @data: buffer to store data
1080 * @size: size of the @data buffer
1082 * External interface to read data from dso file offset. Open
1083 * dso data file and use cached_read to get the data.
1085 ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
1086 u64 offset, u8 *data, ssize_t size)
1088 if (dso->data.status == DSO_DATA_STATUS_ERROR)
1089 return -1;
1091 return data_read_write_offset(dso, machine, offset, data, size, true);
1095 * dso__data_read_addr - Read data from dso address
1096 * @dso: dso object
1097 * @machine: machine object
1098 * @add: virtual memory address
1099 * @data: buffer to store data
1100 * @size: size of the @data buffer
1102 * External interface to read data from dso address.
1104 ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
1105 struct machine *machine, u64 addr,
1106 u8 *data, ssize_t size)
1108 u64 offset = map->map_ip(map, addr);
1109 return dso__data_read_offset(dso, machine, offset, data, size);
1113 * dso__data_write_cache_offs - Write data to dso data cache at file offset
1114 * @dso: dso object
1115 * @machine: machine object
1116 * @offset: file offset
1117 * @data: buffer to write
1118 * @size: size of the @data buffer
1120 * Write into the dso file data cache, but do not change the file itself.
1122 ssize_t dso__data_write_cache_offs(struct dso *dso, struct machine *machine,
1123 u64 offset, const u8 *data_in, ssize_t size)
1125 u8 *data = (u8 *)data_in; /* cast away const to use same fns for r/w */
1127 if (dso->data.status == DSO_DATA_STATUS_ERROR)
1128 return -1;
1130 return data_read_write_offset(dso, machine, offset, data, size, false);
1134 * dso__data_write_cache_addr - Write data to dso data cache at dso address
1135 * @dso: dso object
1136 * @machine: machine object
1137 * @add: virtual memory address
1138 * @data: buffer to write
1139 * @size: size of the @data buffer
1141 * External interface to write into the dso file data cache, but do not change
1142 * the file itself.
1144 ssize_t dso__data_write_cache_addr(struct dso *dso, struct map *map,
1145 struct machine *machine, u64 addr,
1146 const u8 *data, ssize_t size)
1148 u64 offset = map->map_ip(map, addr);
1149 return dso__data_write_cache_offs(dso, machine, offset, data, size);
1152 struct map *dso__new_map(const char *name)
1154 struct map *map = NULL;
1155 struct dso *dso = dso__new(name);
1157 if (dso)
1158 map = map__new2(0, dso);
1160 return map;
1163 struct dso *machine__findnew_kernel(struct machine *machine, const char *name,
1164 const char *short_name, int dso_type)
1167 * The kernel dso could be created by build_id processing.
1169 struct dso *dso = machine__findnew_dso(machine, name);
1172 * We need to run this in all cases, since during the build_id
1173 * processing we had no idea this was the kernel dso.
1175 if (dso != NULL) {
1176 dso__set_short_name(dso, short_name, false);
1177 dso->kernel = dso_type;
1180 return dso;
1183 static void dso__set_long_name_id(struct dso *dso, const char *name, struct dso_id *id, bool name_allocated)
1185 struct rb_root *root = dso->root;
1187 if (name == NULL)
1188 return;
1190 if (dso->long_name_allocated)
1191 free((char *)dso->long_name);
1193 if (root) {
1194 rb_erase(&dso->rb_node, root);
1196 * __dsos__findnew_link_by_longname_id() isn't guaranteed to
1197 * add it back, so a clean removal is required here.
1199 RB_CLEAR_NODE(&dso->rb_node);
1200 dso->root = NULL;
1203 dso->long_name = name;
1204 dso->long_name_len = strlen(name);
1205 dso->long_name_allocated = name_allocated;
1207 if (root)
1208 __dsos__findnew_link_by_longname_id(root, dso, NULL, id);
1211 void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
1213 dso__set_long_name_id(dso, name, NULL, name_allocated);
1216 void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
1218 if (name == NULL)
1219 return;
1221 if (dso->short_name_allocated)
1222 free((char *)dso->short_name);
1224 dso->short_name = name;
1225 dso->short_name_len = strlen(name);
1226 dso->short_name_allocated = name_allocated;
1229 int dso__name_len(const struct dso *dso)
1231 if (!dso)
1232 return strlen("[unknown]");
1233 if (verbose > 0)
1234 return dso->long_name_len;
1236 return dso->short_name_len;
1239 bool dso__loaded(const struct dso *dso)
1241 return dso->loaded;
1244 bool dso__sorted_by_name(const struct dso *dso)
1246 return dso->sorted_by_name;
1249 void dso__set_sorted_by_name(struct dso *dso)
1251 dso->sorted_by_name = true;
1254 struct dso *dso__new_id(const char *name, struct dso_id *id)
1256 struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
1258 if (dso != NULL) {
1259 strcpy(dso->name, name);
1260 if (id)
1261 dso->id = *id;
1262 dso__set_long_name_id(dso, dso->name, id, false);
1263 dso__set_short_name(dso, dso->name, false);
1264 dso->symbols = dso->symbol_names = RB_ROOT_CACHED;
1265 dso->data.cache = RB_ROOT;
1266 dso->inlined_nodes = RB_ROOT_CACHED;
1267 dso->srclines = RB_ROOT_CACHED;
1268 dso->data.fd = -1;
1269 dso->data.status = DSO_DATA_STATUS_UNKNOWN;
1270 dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
1271 dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
1272 dso->is_64_bit = (sizeof(void *) == 8);
1273 dso->loaded = 0;
1274 dso->rel = 0;
1275 dso->sorted_by_name = 0;
1276 dso->has_build_id = 0;
1277 dso->has_srcline = 1;
1278 dso->a2l_fails = 1;
1279 dso->kernel = DSO_SPACE__USER;
1280 dso->needs_swap = DSO_SWAP__UNSET;
1281 dso->comp = COMP_ID__NONE;
1282 RB_CLEAR_NODE(&dso->rb_node);
1283 dso->root = NULL;
1284 INIT_LIST_HEAD(&dso->node);
1285 INIT_LIST_HEAD(&dso->data.open_entry);
1286 pthread_mutex_init(&dso->lock, NULL);
1287 refcount_set(&dso->refcnt, 1);
1290 return dso;
1293 struct dso *dso__new(const char *name)
1295 return dso__new_id(name, NULL);
1298 void dso__delete(struct dso *dso)
1300 if (!RB_EMPTY_NODE(&dso->rb_node))
1301 pr_err("DSO %s is still in rbtree when being deleted!\n",
1302 dso->long_name);
1304 /* free inlines first, as they reference symbols */
1305 inlines__tree_delete(&dso->inlined_nodes);
1306 srcline__tree_delete(&dso->srclines);
1307 symbols__delete(&dso->symbols);
1309 if (dso->short_name_allocated) {
1310 zfree((char **)&dso->short_name);
1311 dso->short_name_allocated = false;
1314 if (dso->long_name_allocated) {
1315 zfree((char **)&dso->long_name);
1316 dso->long_name_allocated = false;
1319 dso__data_close(dso);
1320 auxtrace_cache__free(dso->auxtrace_cache);
1321 dso_cache__free(dso);
1322 dso__free_a2l(dso);
1323 zfree(&dso->symsrc_filename);
1324 nsinfo__zput(dso->nsinfo);
1325 pthread_mutex_destroy(&dso->lock);
1326 free(dso);
1329 struct dso *dso__get(struct dso *dso)
1331 if (dso)
1332 refcount_inc(&dso->refcnt);
1333 return dso;
1336 void dso__put(struct dso *dso)
1338 if (dso && refcount_dec_and_test(&dso->refcnt))
1339 dso__delete(dso);
1342 void dso__set_build_id(struct dso *dso, struct build_id *bid)
1344 dso->bid = *bid;
1345 dso->has_build_id = 1;
1348 bool dso__build_id_equal(const struct dso *dso, struct build_id *bid)
1350 return dso->bid.size == bid->size &&
1351 memcmp(dso->bid.data, bid->data, dso->bid.size) == 0;
1354 void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
1356 char path[PATH_MAX];
1358 if (machine__is_default_guest(machine))
1359 return;
1360 sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
1361 if (sysfs__read_build_id(path, &dso->bid) == 0)
1362 dso->has_build_id = true;
1365 int dso__kernel_module_get_build_id(struct dso *dso,
1366 const char *root_dir)
1368 char filename[PATH_MAX];
1370 * kernel module short names are of the form "[module]" and
1371 * we need just "module" here.
1373 const char *name = dso->short_name + 1;
1375 snprintf(filename, sizeof(filename),
1376 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1377 root_dir, (int)strlen(name) - 1, name);
1379 if (sysfs__read_build_id(filename, &dso->bid) == 0)
1380 dso->has_build_id = true;
1382 return 0;
1385 static size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
1387 char sbuild_id[SBUILD_ID_SIZE];
1389 build_id__sprintf(&dso->bid, sbuild_id);
1390 return fprintf(fp, "%s", sbuild_id);
1393 size_t dso__fprintf(struct dso *dso, FILE *fp)
1395 struct rb_node *nd;
1396 size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
1398 if (dso->short_name != dso->long_name)
1399 ret += fprintf(fp, "%s, ", dso->long_name);
1400 ret += fprintf(fp, "%sloaded, ", dso__loaded(dso) ? "" : "NOT ");
1401 ret += dso__fprintf_buildid(dso, fp);
1402 ret += fprintf(fp, ")\n");
1403 for (nd = rb_first_cached(&dso->symbols); nd; nd = rb_next(nd)) {
1404 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
1405 ret += symbol__fprintf(pos, fp);
1408 return ret;
1411 enum dso_type dso__type(struct dso *dso, struct machine *machine)
1413 int fd;
1414 enum dso_type type = DSO__TYPE_UNKNOWN;
1416 fd = dso__data_get_fd(dso, machine);
1417 if (fd >= 0) {
1418 type = dso__type_fd(fd);
1419 dso__data_put_fd(dso);
1422 return type;
1425 int dso__strerror_load(struct dso *dso, char *buf, size_t buflen)
1427 int idx, errnum = dso->load_errno;
1429 * This must have a same ordering as the enum dso_load_errno.
1431 static const char *dso_load__error_str[] = {
1432 "Internal tools/perf/ library error",
1433 "Invalid ELF file",
1434 "Can not read build id",
1435 "Mismatching build id",
1436 "Decompression failure",
1439 BUG_ON(buflen == 0);
1441 if (errnum >= 0) {
1442 const char *err = str_error_r(errnum, buf, buflen);
1444 if (err != buf)
1445 scnprintf(buf, buflen, "%s", err);
1447 return 0;
1450 if (errnum < __DSO_LOAD_ERRNO__START || errnum >= __DSO_LOAD_ERRNO__END)
1451 return -1;
1453 idx = errnum - __DSO_LOAD_ERRNO__START;
1454 scnprintf(buf, buflen, "%s", dso_load__error_str[idx]);
1455 return 0;