regulator: s2mps11: Adjust supported buck voltages to real values
[linux/fpc-iii.git] / tools / perf / util / dso.c
blobba58ba603b69c800454688d21bbb5d7134d5abac
1 // SPDX-License-Identifier: GPL-2.0
2 #include <asm/bug.h>
3 #include <linux/kernel.h>
4 #include <sys/time.h>
5 #include <sys/resource.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <unistd.h>
9 #include <errno.h>
10 #include <fcntl.h>
11 #include <libgen.h>
12 #include "compress.h"
13 #include "namespaces.h"
14 #include "path.h"
15 #include "map.h"
16 #include "symbol.h"
17 #include "srcline.h"
18 #include "dso.h"
19 #include "machine.h"
20 #include "auxtrace.h"
21 #include "util.h"
22 #include "debug.h"
23 #include "string2.h"
24 #include "vdso.h"
26 static const char * const debuglink_paths[] = {
27 "%.0s%s",
28 "%s/%s",
29 "%s/.debug/%s",
30 "/usr/lib/debug%s/%s"
33 char dso__symtab_origin(const struct dso *dso)
35 static const char origin[] = {
36 [DSO_BINARY_TYPE__KALLSYMS] = 'k',
37 [DSO_BINARY_TYPE__VMLINUX] = 'v',
38 [DSO_BINARY_TYPE__JAVA_JIT] = 'j',
39 [DSO_BINARY_TYPE__DEBUGLINK] = 'l',
40 [DSO_BINARY_TYPE__BUILD_ID_CACHE] = 'B',
41 [DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO] = 'D',
42 [DSO_BINARY_TYPE__FEDORA_DEBUGINFO] = 'f',
43 [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO] = 'u',
44 [DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO] = 'o',
45 [DSO_BINARY_TYPE__BUILDID_DEBUGINFO] = 'b',
46 [DSO_BINARY_TYPE__SYSTEM_PATH_DSO] = 'd',
47 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE] = 'K',
48 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP] = 'm',
49 [DSO_BINARY_TYPE__GUEST_KALLSYMS] = 'g',
50 [DSO_BINARY_TYPE__GUEST_KMODULE] = 'G',
51 [DSO_BINARY_TYPE__GUEST_KMODULE_COMP] = 'M',
52 [DSO_BINARY_TYPE__GUEST_VMLINUX] = 'V',
55 if (dso == NULL || dso->symtab_type == DSO_BINARY_TYPE__NOT_FOUND)
56 return '!';
57 return origin[dso->symtab_type];
60 int dso__read_binary_type_filename(const struct dso *dso,
61 enum dso_binary_type type,
62 char *root_dir, char *filename, size_t size)
64 char build_id_hex[SBUILD_ID_SIZE];
65 int ret = 0;
66 size_t len;
68 switch (type) {
69 case DSO_BINARY_TYPE__DEBUGLINK:
71 const char *last_slash;
72 char dso_dir[PATH_MAX];
73 char symfile[PATH_MAX];
74 unsigned int i;
76 len = __symbol__join_symfs(filename, size, dso->long_name);
77 last_slash = filename + len;
78 while (last_slash != filename && *last_slash != '/')
79 last_slash--;
81 strncpy(dso_dir, filename, last_slash - filename);
82 dso_dir[last_slash-filename] = '\0';
84 if (!is_regular_file(filename)) {
85 ret = -1;
86 break;
89 ret = filename__read_debuglink(filename, symfile, PATH_MAX);
90 if (ret)
91 break;
93 /* Check predefined locations where debug file might reside */
94 ret = -1;
95 for (i = 0; i < ARRAY_SIZE(debuglink_paths); i++) {
96 snprintf(filename, size,
97 debuglink_paths[i], dso_dir, symfile);
98 if (is_regular_file(filename)) {
99 ret = 0;
100 break;
104 break;
106 case DSO_BINARY_TYPE__BUILD_ID_CACHE:
107 if (dso__build_id_filename(dso, filename, size, false) == NULL)
108 ret = -1;
109 break;
111 case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO:
112 if (dso__build_id_filename(dso, filename, size, true) == NULL)
113 ret = -1;
114 break;
116 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
117 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
118 snprintf(filename + len, size - len, "%s.debug", dso->long_name);
119 break;
121 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
122 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
123 snprintf(filename + len, size - len, "%s", dso->long_name);
124 break;
126 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
128 const char *last_slash;
129 size_t dir_size;
131 last_slash = dso->long_name + dso->long_name_len;
132 while (last_slash != dso->long_name && *last_slash != '/')
133 last_slash--;
135 len = __symbol__join_symfs(filename, size, "");
136 dir_size = last_slash - dso->long_name + 2;
137 if (dir_size > (size - len)) {
138 ret = -1;
139 break;
141 len += scnprintf(filename + len, dir_size, "%s", dso->long_name);
142 len += scnprintf(filename + len , size - len, ".debug%s",
143 last_slash);
144 break;
147 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
148 if (!dso->has_build_id) {
149 ret = -1;
150 break;
153 build_id__sprintf(dso->build_id,
154 sizeof(dso->build_id),
155 build_id_hex);
156 len = __symbol__join_symfs(filename, size, "/usr/lib/debug/.build-id/");
157 snprintf(filename + len, size - len, "%.2s/%s.debug",
158 build_id_hex, build_id_hex + 2);
159 break;
161 case DSO_BINARY_TYPE__VMLINUX:
162 case DSO_BINARY_TYPE__GUEST_VMLINUX:
163 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
164 __symbol__join_symfs(filename, size, dso->long_name);
165 break;
167 case DSO_BINARY_TYPE__GUEST_KMODULE:
168 case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
169 path__join3(filename, size, symbol_conf.symfs,
170 root_dir, dso->long_name);
171 break;
173 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
174 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
175 __symbol__join_symfs(filename, size, dso->long_name);
176 break;
178 case DSO_BINARY_TYPE__KCORE:
179 case DSO_BINARY_TYPE__GUEST_KCORE:
180 snprintf(filename, size, "%s", dso->long_name);
181 break;
183 default:
184 case DSO_BINARY_TYPE__KALLSYMS:
185 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
186 case DSO_BINARY_TYPE__JAVA_JIT:
187 case DSO_BINARY_TYPE__NOT_FOUND:
188 ret = -1;
189 break;
192 return ret;
195 enum {
196 COMP_ID__NONE = 0,
199 static const struct {
200 const char *fmt;
201 int (*decompress)(const char *input, int output);
202 bool (*is_compressed)(const char *input);
203 } compressions[] = {
204 [COMP_ID__NONE] = { .fmt = NULL, },
205 #ifdef HAVE_ZLIB_SUPPORT
206 { "gz", gzip_decompress_to_file, gzip_is_compressed },
207 #endif
208 #ifdef HAVE_LZMA_SUPPORT
209 { "xz", lzma_decompress_to_file, lzma_is_compressed },
210 #endif
211 { NULL, NULL, NULL },
214 static int is_supported_compression(const char *ext)
216 unsigned i;
218 for (i = 1; compressions[i].fmt; i++) {
219 if (!strcmp(ext, compressions[i].fmt))
220 return i;
222 return COMP_ID__NONE;
225 bool is_kernel_module(const char *pathname, int cpumode)
227 struct kmod_path m;
228 int mode = cpumode & PERF_RECORD_MISC_CPUMODE_MASK;
230 WARN_ONCE(mode != cpumode,
231 "Internal error: passing unmasked cpumode (%x) to is_kernel_module",
232 cpumode);
234 switch (mode) {
235 case PERF_RECORD_MISC_USER:
236 case PERF_RECORD_MISC_HYPERVISOR:
237 case PERF_RECORD_MISC_GUEST_USER:
238 return false;
239 /* Treat PERF_RECORD_MISC_CPUMODE_UNKNOWN as kernel */
240 default:
241 if (kmod_path__parse(&m, pathname)) {
242 pr_err("Failed to check whether %s is a kernel module or not. Assume it is.",
243 pathname);
244 return true;
248 return m.kmod;
251 bool dso__needs_decompress(struct dso *dso)
253 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
254 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
257 static int decompress_kmodule(struct dso *dso, const char *name,
258 char *pathname, size_t len)
260 char tmpbuf[] = KMOD_DECOMP_NAME;
261 int fd = -1;
263 if (!dso__needs_decompress(dso))
264 return -1;
266 if (dso->comp == COMP_ID__NONE)
267 return -1;
270 * We have proper compression id for DSO and yet the file
271 * behind the 'name' can still be plain uncompressed object.
273 * The reason is behind the logic we open the DSO object files,
274 * when we try all possible 'debug' objects until we find the
275 * data. So even if the DSO is represented by 'krava.xz' module,
276 * we can end up here opening ~/.debug/....23432432/debug' file
277 * which is not compressed.
279 * To keep this transparent, we detect this and return the file
280 * descriptor to the uncompressed file.
282 if (!compressions[dso->comp].is_compressed(name))
283 return open(name, O_RDONLY);
285 fd = mkstemp(tmpbuf);
286 if (fd < 0) {
287 dso->load_errno = errno;
288 return -1;
291 if (compressions[dso->comp].decompress(name, fd)) {
292 dso->load_errno = DSO_LOAD_ERRNO__DECOMPRESSION_FAILURE;
293 close(fd);
294 fd = -1;
297 if (!pathname || (fd < 0))
298 unlink(tmpbuf);
300 if (pathname && (fd >= 0))
301 strlcpy(pathname, tmpbuf, len);
303 return fd;
306 int dso__decompress_kmodule_fd(struct dso *dso, const char *name)
308 return decompress_kmodule(dso, name, NULL, 0);
311 int dso__decompress_kmodule_path(struct dso *dso, const char *name,
312 char *pathname, size_t len)
314 int fd = decompress_kmodule(dso, name, pathname, len);
316 close(fd);
317 return fd >= 0 ? 0 : -1;
321 * Parses kernel module specified in @path and updates
322 * @m argument like:
324 * @comp - true if @path contains supported compression suffix,
325 * false otherwise
326 * @kmod - true if @path contains '.ko' suffix in right position,
327 * false otherwise
328 * @name - if (@alloc_name && @kmod) is true, it contains strdup-ed base name
329 * of the kernel module without suffixes, otherwise strudup-ed
330 * base name of @path
331 * @ext - if (@alloc_ext && @comp) is true, it contains strdup-ed string
332 * the compression suffix
334 * Returns 0 if there's no strdup error, -ENOMEM otherwise.
336 int __kmod_path__parse(struct kmod_path *m, const char *path,
337 bool alloc_name)
339 const char *name = strrchr(path, '/');
340 const char *ext = strrchr(path, '.');
341 bool is_simple_name = false;
343 memset(m, 0x0, sizeof(*m));
344 name = name ? name + 1 : path;
347 * '.' is also a valid character for module name. For example:
348 * [aaa.bbb] is a valid module name. '[' should have higher
349 * priority than '.ko' suffix.
351 * The kernel names are from machine__mmap_name. Such
352 * name should belong to kernel itself, not kernel module.
354 if (name[0] == '[') {
355 is_simple_name = true;
356 if ((strncmp(name, "[kernel.kallsyms]", 17) == 0) ||
357 (strncmp(name, "[guest.kernel.kallsyms", 22) == 0) ||
358 (strncmp(name, "[vdso]", 6) == 0) ||
359 (strncmp(name, "[vdso32]", 8) == 0) ||
360 (strncmp(name, "[vdsox32]", 9) == 0) ||
361 (strncmp(name, "[vsyscall]", 10) == 0)) {
362 m->kmod = false;
364 } else
365 m->kmod = true;
368 /* No extension, just return name. */
369 if ((ext == NULL) || is_simple_name) {
370 if (alloc_name) {
371 m->name = strdup(name);
372 return m->name ? 0 : -ENOMEM;
374 return 0;
377 m->comp = is_supported_compression(ext + 1);
378 if (m->comp > COMP_ID__NONE)
379 ext -= 3;
381 /* Check .ko extension only if there's enough name left. */
382 if (ext > name)
383 m->kmod = !strncmp(ext, ".ko", 3);
385 if (alloc_name) {
386 if (m->kmod) {
387 if (asprintf(&m->name, "[%.*s]", (int) (ext - name), name) == -1)
388 return -ENOMEM;
389 } else {
390 if (asprintf(&m->name, "%s", name) == -1)
391 return -ENOMEM;
394 strxfrchar(m->name, '-', '_');
397 return 0;
400 void dso__set_module_info(struct dso *dso, struct kmod_path *m,
401 struct machine *machine)
403 if (machine__is_host(machine))
404 dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE;
405 else
406 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE;
408 /* _KMODULE_COMP should be next to _KMODULE */
409 if (m->kmod && m->comp) {
410 dso->symtab_type++;
411 dso->comp = m->comp;
414 dso__set_short_name(dso, strdup(m->name), true);
418 * Global list of open DSOs and the counter.
420 static LIST_HEAD(dso__data_open);
421 static long dso__data_open_cnt;
422 static pthread_mutex_t dso__data_open_lock = PTHREAD_MUTEX_INITIALIZER;
424 static void dso__list_add(struct dso *dso)
426 list_add_tail(&dso->data.open_entry, &dso__data_open);
427 dso__data_open_cnt++;
430 static void dso__list_del(struct dso *dso)
432 list_del(&dso->data.open_entry);
433 WARN_ONCE(dso__data_open_cnt <= 0,
434 "DSO data fd counter out of bounds.");
435 dso__data_open_cnt--;
438 static void close_first_dso(void);
440 static int do_open(char *name)
442 int fd;
443 char sbuf[STRERR_BUFSIZE];
445 do {
446 fd = open(name, O_RDONLY|O_CLOEXEC);
447 if (fd >= 0)
448 return fd;
450 pr_debug("dso open failed: %s\n",
451 str_error_r(errno, sbuf, sizeof(sbuf)));
452 if (!dso__data_open_cnt || errno != EMFILE)
453 break;
455 close_first_dso();
456 } while (1);
458 return -1;
461 static int __open_dso(struct dso *dso, struct machine *machine)
463 int fd = -EINVAL;
464 char *root_dir = (char *)"";
465 char *name = malloc(PATH_MAX);
466 bool decomp = false;
468 if (!name)
469 return -ENOMEM;
471 if (machine)
472 root_dir = machine->root_dir;
474 if (dso__read_binary_type_filename(dso, dso->binary_type,
475 root_dir, name, PATH_MAX))
476 goto out;
478 if (!is_regular_file(name))
479 goto out;
481 if (dso__needs_decompress(dso)) {
482 char newpath[KMOD_DECOMP_LEN];
483 size_t len = sizeof(newpath);
485 if (dso__decompress_kmodule_path(dso, name, newpath, len) < 0) {
486 fd = -dso->load_errno;
487 goto out;
490 decomp = true;
491 strcpy(name, newpath);
494 fd = do_open(name);
496 if (decomp)
497 unlink(name);
499 out:
500 free(name);
501 return fd;
504 static void check_data_close(void);
507 * dso_close - Open DSO data file
508 * @dso: dso object
510 * Open @dso's data file descriptor and updates
511 * list/count of open DSO objects.
513 static int open_dso(struct dso *dso, struct machine *machine)
515 int fd;
516 struct nscookie nsc;
518 if (dso->binary_type != DSO_BINARY_TYPE__BUILD_ID_CACHE)
519 nsinfo__mountns_enter(dso->nsinfo, &nsc);
520 fd = __open_dso(dso, machine);
521 if (dso->binary_type != DSO_BINARY_TYPE__BUILD_ID_CACHE)
522 nsinfo__mountns_exit(&nsc);
524 if (fd >= 0) {
525 dso__list_add(dso);
527 * Check if we crossed the allowed number
528 * of opened DSOs and close one if needed.
530 check_data_close();
533 return fd;
536 static void close_data_fd(struct dso *dso)
538 if (dso->data.fd >= 0) {
539 close(dso->data.fd);
540 dso->data.fd = -1;
541 dso->data.file_size = 0;
542 dso__list_del(dso);
547 * dso_close - Close DSO data file
548 * @dso: dso object
550 * Close @dso's data file descriptor and updates
551 * list/count of open DSO objects.
553 static void close_dso(struct dso *dso)
555 close_data_fd(dso);
558 static void close_first_dso(void)
560 struct dso *dso;
562 dso = list_first_entry(&dso__data_open, struct dso, data.open_entry);
563 close_dso(dso);
566 static rlim_t get_fd_limit(void)
568 struct rlimit l;
569 rlim_t limit = 0;
571 /* Allow half of the current open fd limit. */
572 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
573 if (l.rlim_cur == RLIM_INFINITY)
574 limit = l.rlim_cur;
575 else
576 limit = l.rlim_cur / 2;
577 } else {
578 pr_err("failed to get fd limit\n");
579 limit = 1;
582 return limit;
585 static rlim_t fd_limit;
588 * Used only by tests/dso-data.c to reset the environment
589 * for tests. I dont expect we should change this during
590 * standard runtime.
592 void reset_fd_limit(void)
594 fd_limit = 0;
597 static bool may_cache_fd(void)
599 if (!fd_limit)
600 fd_limit = get_fd_limit();
602 if (fd_limit == RLIM_INFINITY)
603 return true;
605 return fd_limit > (rlim_t) dso__data_open_cnt;
609 * Check and close LRU dso if we crossed allowed limit
610 * for opened dso file descriptors. The limit is half
611 * of the RLIMIT_NOFILE files opened.
613 static void check_data_close(void)
615 bool cache_fd = may_cache_fd();
617 if (!cache_fd)
618 close_first_dso();
622 * dso__data_close - Close DSO data file
623 * @dso: dso object
625 * External interface to close @dso's data file descriptor.
627 void dso__data_close(struct dso *dso)
629 pthread_mutex_lock(&dso__data_open_lock);
630 close_dso(dso);
631 pthread_mutex_unlock(&dso__data_open_lock);
634 static void try_to_open_dso(struct dso *dso, struct machine *machine)
636 enum dso_binary_type binary_type_data[] = {
637 DSO_BINARY_TYPE__BUILD_ID_CACHE,
638 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
639 DSO_BINARY_TYPE__NOT_FOUND,
641 int i = 0;
643 if (dso->data.fd >= 0)
644 return;
646 if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) {
647 dso->data.fd = open_dso(dso, machine);
648 goto out;
651 do {
652 dso->binary_type = binary_type_data[i++];
654 dso->data.fd = open_dso(dso, machine);
655 if (dso->data.fd >= 0)
656 goto out;
658 } while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND);
659 out:
660 if (dso->data.fd >= 0)
661 dso->data.status = DSO_DATA_STATUS_OK;
662 else
663 dso->data.status = DSO_DATA_STATUS_ERROR;
667 * dso__data_get_fd - Get dso's data file descriptor
668 * @dso: dso object
669 * @machine: machine object
671 * External interface to find dso's file, open it and
672 * returns file descriptor. It should be paired with
673 * dso__data_put_fd() if it returns non-negative value.
675 int dso__data_get_fd(struct dso *dso, struct machine *machine)
677 if (dso->data.status == DSO_DATA_STATUS_ERROR)
678 return -1;
680 if (pthread_mutex_lock(&dso__data_open_lock) < 0)
681 return -1;
683 try_to_open_dso(dso, machine);
685 if (dso->data.fd < 0)
686 pthread_mutex_unlock(&dso__data_open_lock);
688 return dso->data.fd;
691 void dso__data_put_fd(struct dso *dso __maybe_unused)
693 pthread_mutex_unlock(&dso__data_open_lock);
696 bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by)
698 u32 flag = 1 << by;
700 if (dso->data.status_seen & flag)
701 return true;
703 dso->data.status_seen |= flag;
705 return false;
708 static void
709 dso_cache__free(struct dso *dso)
711 struct rb_root *root = &dso->data.cache;
712 struct rb_node *next = rb_first(root);
714 pthread_mutex_lock(&dso->lock);
715 while (next) {
716 struct dso_cache *cache;
718 cache = rb_entry(next, struct dso_cache, rb_node);
719 next = rb_next(&cache->rb_node);
720 rb_erase(&cache->rb_node, root);
721 free(cache);
723 pthread_mutex_unlock(&dso->lock);
726 static struct dso_cache *dso_cache__find(struct dso *dso, u64 offset)
728 const struct rb_root *root = &dso->data.cache;
729 struct rb_node * const *p = &root->rb_node;
730 const struct rb_node *parent = NULL;
731 struct dso_cache *cache;
733 while (*p != NULL) {
734 u64 end;
736 parent = *p;
737 cache = rb_entry(parent, struct dso_cache, rb_node);
738 end = cache->offset + DSO__DATA_CACHE_SIZE;
740 if (offset < cache->offset)
741 p = &(*p)->rb_left;
742 else if (offset >= end)
743 p = &(*p)->rb_right;
744 else
745 return cache;
748 return NULL;
751 static struct dso_cache *
752 dso_cache__insert(struct dso *dso, struct dso_cache *new)
754 struct rb_root *root = &dso->data.cache;
755 struct rb_node **p = &root->rb_node;
756 struct rb_node *parent = NULL;
757 struct dso_cache *cache;
758 u64 offset = new->offset;
760 pthread_mutex_lock(&dso->lock);
761 while (*p != NULL) {
762 u64 end;
764 parent = *p;
765 cache = rb_entry(parent, struct dso_cache, rb_node);
766 end = cache->offset + DSO__DATA_CACHE_SIZE;
768 if (offset < cache->offset)
769 p = &(*p)->rb_left;
770 else if (offset >= end)
771 p = &(*p)->rb_right;
772 else
773 goto out;
776 rb_link_node(&new->rb_node, parent, p);
777 rb_insert_color(&new->rb_node, root);
779 cache = NULL;
780 out:
781 pthread_mutex_unlock(&dso->lock);
782 return cache;
785 static ssize_t
786 dso_cache__memcpy(struct dso_cache *cache, u64 offset,
787 u8 *data, u64 size)
789 u64 cache_offset = offset - cache->offset;
790 u64 cache_size = min(cache->size - cache_offset, size);
792 memcpy(data, cache->data + cache_offset, cache_size);
793 return cache_size;
796 static ssize_t
797 dso_cache__read(struct dso *dso, struct machine *machine,
798 u64 offset, u8 *data, ssize_t size)
800 struct dso_cache *cache;
801 struct dso_cache *old;
802 ssize_t ret;
804 do {
805 u64 cache_offset;
807 cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
808 if (!cache)
809 return -ENOMEM;
811 pthread_mutex_lock(&dso__data_open_lock);
814 * dso->data.fd might be closed if other thread opened another
815 * file (dso) due to open file limit (RLIMIT_NOFILE).
817 try_to_open_dso(dso, machine);
819 if (dso->data.fd < 0) {
820 ret = -errno;
821 dso->data.status = DSO_DATA_STATUS_ERROR;
822 break;
825 cache_offset = offset & DSO__DATA_CACHE_MASK;
827 ret = pread(dso->data.fd, cache->data, DSO__DATA_CACHE_SIZE, cache_offset);
828 if (ret <= 0)
829 break;
831 cache->offset = cache_offset;
832 cache->size = ret;
833 } while (0);
835 pthread_mutex_unlock(&dso__data_open_lock);
837 if (ret > 0) {
838 old = dso_cache__insert(dso, cache);
839 if (old) {
840 /* we lose the race */
841 free(cache);
842 cache = old;
845 ret = dso_cache__memcpy(cache, offset, data, size);
848 if (ret <= 0)
849 free(cache);
851 return ret;
854 static ssize_t dso_cache_read(struct dso *dso, struct machine *machine,
855 u64 offset, u8 *data, ssize_t size)
857 struct dso_cache *cache;
859 cache = dso_cache__find(dso, offset);
860 if (cache)
861 return dso_cache__memcpy(cache, offset, data, size);
862 else
863 return dso_cache__read(dso, machine, offset, data, size);
867 * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
868 * in the rb_tree. Any read to already cached data is served
869 * by cached data.
871 static ssize_t cached_read(struct dso *dso, struct machine *machine,
872 u64 offset, u8 *data, ssize_t size)
874 ssize_t r = 0;
875 u8 *p = data;
877 do {
878 ssize_t ret;
880 ret = dso_cache_read(dso, machine, offset, p, size);
881 if (ret < 0)
882 return ret;
884 /* Reached EOF, return what we have. */
885 if (!ret)
886 break;
888 BUG_ON(ret > size);
890 r += ret;
891 p += ret;
892 offset += ret;
893 size -= ret;
895 } while (size);
897 return r;
900 int dso__data_file_size(struct dso *dso, struct machine *machine)
902 int ret = 0;
903 struct stat st;
904 char sbuf[STRERR_BUFSIZE];
906 if (dso->data.file_size)
907 return 0;
909 if (dso->data.status == DSO_DATA_STATUS_ERROR)
910 return -1;
912 pthread_mutex_lock(&dso__data_open_lock);
915 * dso->data.fd might be closed if other thread opened another
916 * file (dso) due to open file limit (RLIMIT_NOFILE).
918 try_to_open_dso(dso, machine);
920 if (dso->data.fd < 0) {
921 ret = -errno;
922 dso->data.status = DSO_DATA_STATUS_ERROR;
923 goto out;
926 if (fstat(dso->data.fd, &st) < 0) {
927 ret = -errno;
928 pr_err("dso cache fstat failed: %s\n",
929 str_error_r(errno, sbuf, sizeof(sbuf)));
930 dso->data.status = DSO_DATA_STATUS_ERROR;
931 goto out;
933 dso->data.file_size = st.st_size;
935 out:
936 pthread_mutex_unlock(&dso__data_open_lock);
937 return ret;
941 * dso__data_size - Return dso data size
942 * @dso: dso object
943 * @machine: machine object
945 * Return: dso data size
947 off_t dso__data_size(struct dso *dso, struct machine *machine)
949 if (dso__data_file_size(dso, machine))
950 return -1;
952 /* For now just estimate dso data size is close to file size */
953 return dso->data.file_size;
956 static ssize_t data_read_offset(struct dso *dso, struct machine *machine,
957 u64 offset, u8 *data, ssize_t size)
959 if (dso__data_file_size(dso, machine))
960 return -1;
962 /* Check the offset sanity. */
963 if (offset > dso->data.file_size)
964 return -1;
966 if (offset + size < offset)
967 return -1;
969 return cached_read(dso, machine, offset, data, size);
973 * dso__data_read_offset - Read data from dso file offset
974 * @dso: dso object
975 * @machine: machine object
976 * @offset: file offset
977 * @data: buffer to store data
978 * @size: size of the @data buffer
980 * External interface to read data from dso file offset. Open
981 * dso data file and use cached_read to get the data.
983 ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
984 u64 offset, u8 *data, ssize_t size)
986 if (dso->data.status == DSO_DATA_STATUS_ERROR)
987 return -1;
989 return data_read_offset(dso, machine, offset, data, size);
993 * dso__data_read_addr - Read data from dso address
994 * @dso: dso object
995 * @machine: machine object
996 * @add: virtual memory address
997 * @data: buffer to store data
998 * @size: size of the @data buffer
1000 * External interface to read data from dso address.
1002 ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
1003 struct machine *machine, u64 addr,
1004 u8 *data, ssize_t size)
1006 u64 offset = map->map_ip(map, addr);
1007 return dso__data_read_offset(dso, machine, offset, data, size);
1010 struct map *dso__new_map(const char *name)
1012 struct map *map = NULL;
1013 struct dso *dso = dso__new(name);
1015 if (dso)
1016 map = map__new2(0, dso);
1018 return map;
1021 struct dso *machine__findnew_kernel(struct machine *machine, const char *name,
1022 const char *short_name, int dso_type)
1025 * The kernel dso could be created by build_id processing.
1027 struct dso *dso = machine__findnew_dso(machine, name);
1030 * We need to run this in all cases, since during the build_id
1031 * processing we had no idea this was the kernel dso.
1033 if (dso != NULL) {
1034 dso__set_short_name(dso, short_name, false);
1035 dso->kernel = dso_type;
1038 return dso;
1042 * Find a matching entry and/or link current entry to RB tree.
1043 * Either one of the dso or name parameter must be non-NULL or the
1044 * function will not work.
1046 static struct dso *__dso__findlink_by_longname(struct rb_root *root,
1047 struct dso *dso, const char *name)
1049 struct rb_node **p = &root->rb_node;
1050 struct rb_node *parent = NULL;
1052 if (!name)
1053 name = dso->long_name;
1055 * Find node with the matching name
1057 while (*p) {
1058 struct dso *this = rb_entry(*p, struct dso, rb_node);
1059 int rc = strcmp(name, this->long_name);
1061 parent = *p;
1062 if (rc == 0) {
1064 * In case the new DSO is a duplicate of an existing
1065 * one, print a one-time warning & put the new entry
1066 * at the end of the list of duplicates.
1068 if (!dso || (dso == this))
1069 return this; /* Find matching dso */
1071 * The core kernel DSOs may have duplicated long name.
1072 * In this case, the short name should be different.
1073 * Comparing the short names to differentiate the DSOs.
1075 rc = strcmp(dso->short_name, this->short_name);
1076 if (rc == 0) {
1077 pr_err("Duplicated dso name: %s\n", name);
1078 return NULL;
1081 if (rc < 0)
1082 p = &parent->rb_left;
1083 else
1084 p = &parent->rb_right;
1086 if (dso) {
1087 /* Add new node and rebalance tree */
1088 rb_link_node(&dso->rb_node, parent, p);
1089 rb_insert_color(&dso->rb_node, root);
1090 dso->root = root;
1092 return NULL;
1095 static inline struct dso *__dso__find_by_longname(struct rb_root *root,
1096 const char *name)
1098 return __dso__findlink_by_longname(root, NULL, name);
1101 void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
1103 struct rb_root *root = dso->root;
1105 if (name == NULL)
1106 return;
1108 if (dso->long_name_allocated)
1109 free((char *)dso->long_name);
1111 if (root) {
1112 rb_erase(&dso->rb_node, root);
1114 * __dso__findlink_by_longname() isn't guaranteed to add it
1115 * back, so a clean removal is required here.
1117 RB_CLEAR_NODE(&dso->rb_node);
1118 dso->root = NULL;
1121 dso->long_name = name;
1122 dso->long_name_len = strlen(name);
1123 dso->long_name_allocated = name_allocated;
1125 if (root)
1126 __dso__findlink_by_longname(root, dso, NULL);
1129 void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
1131 if (name == NULL)
1132 return;
1134 if (dso->short_name_allocated)
1135 free((char *)dso->short_name);
1137 dso->short_name = name;
1138 dso->short_name_len = strlen(name);
1139 dso->short_name_allocated = name_allocated;
1142 static void dso__set_basename(struct dso *dso)
1145 * basename() may modify path buffer, so we must pass
1146 * a copy.
1148 char *base, *lname = strdup(dso->long_name);
1150 if (!lname)
1151 return;
1154 * basename() may return a pointer to internal
1155 * storage which is reused in subsequent calls
1156 * so copy the result.
1158 base = strdup(basename(lname));
1160 free(lname);
1162 if (!base)
1163 return;
1165 dso__set_short_name(dso, base, true);
1168 int dso__name_len(const struct dso *dso)
1170 if (!dso)
1171 return strlen("[unknown]");
1172 if (verbose > 0)
1173 return dso->long_name_len;
1175 return dso->short_name_len;
1178 bool dso__loaded(const struct dso *dso)
1180 return dso->loaded;
1183 bool dso__sorted_by_name(const struct dso *dso)
1185 return dso->sorted_by_name;
1188 void dso__set_sorted_by_name(struct dso *dso)
1190 dso->sorted_by_name = true;
1193 struct dso *dso__new(const char *name)
1195 struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
1197 if (dso != NULL) {
1198 strcpy(dso->name, name);
1199 dso__set_long_name(dso, dso->name, false);
1200 dso__set_short_name(dso, dso->name, false);
1201 dso->symbols = dso->symbol_names = RB_ROOT_CACHED;
1202 dso->data.cache = RB_ROOT;
1203 dso->inlined_nodes = RB_ROOT_CACHED;
1204 dso->srclines = RB_ROOT_CACHED;
1205 dso->data.fd = -1;
1206 dso->data.status = DSO_DATA_STATUS_UNKNOWN;
1207 dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
1208 dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
1209 dso->is_64_bit = (sizeof(void *) == 8);
1210 dso->loaded = 0;
1211 dso->rel = 0;
1212 dso->sorted_by_name = 0;
1213 dso->has_build_id = 0;
1214 dso->has_srcline = 1;
1215 dso->a2l_fails = 1;
1216 dso->kernel = DSO_TYPE_USER;
1217 dso->needs_swap = DSO_SWAP__UNSET;
1218 dso->comp = COMP_ID__NONE;
1219 RB_CLEAR_NODE(&dso->rb_node);
1220 dso->root = NULL;
1221 INIT_LIST_HEAD(&dso->node);
1222 INIT_LIST_HEAD(&dso->data.open_entry);
1223 pthread_mutex_init(&dso->lock, NULL);
1224 refcount_set(&dso->refcnt, 1);
1227 return dso;
1230 void dso__delete(struct dso *dso)
1232 if (!RB_EMPTY_NODE(&dso->rb_node))
1233 pr_err("DSO %s is still in rbtree when being deleted!\n",
1234 dso->long_name);
1236 /* free inlines first, as they reference symbols */
1237 inlines__tree_delete(&dso->inlined_nodes);
1238 srcline__tree_delete(&dso->srclines);
1239 symbols__delete(&dso->symbols);
1241 if (dso->short_name_allocated) {
1242 zfree((char **)&dso->short_name);
1243 dso->short_name_allocated = false;
1246 if (dso->long_name_allocated) {
1247 zfree((char **)&dso->long_name);
1248 dso->long_name_allocated = false;
1251 dso__data_close(dso);
1252 auxtrace_cache__free(dso->auxtrace_cache);
1253 dso_cache__free(dso);
1254 dso__free_a2l(dso);
1255 zfree(&dso->symsrc_filename);
1256 nsinfo__zput(dso->nsinfo);
1257 pthread_mutex_destroy(&dso->lock);
1258 free(dso);
1261 struct dso *dso__get(struct dso *dso)
1263 if (dso)
1264 refcount_inc(&dso->refcnt);
1265 return dso;
1268 void dso__put(struct dso *dso)
1270 if (dso && refcount_dec_and_test(&dso->refcnt))
1271 dso__delete(dso);
1274 void dso__set_build_id(struct dso *dso, void *build_id)
1276 memcpy(dso->build_id, build_id, sizeof(dso->build_id));
1277 dso->has_build_id = 1;
1280 bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
1282 return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
1285 void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
1287 char path[PATH_MAX];
1289 if (machine__is_default_guest(machine))
1290 return;
1291 sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
1292 if (sysfs__read_build_id(path, dso->build_id,
1293 sizeof(dso->build_id)) == 0)
1294 dso->has_build_id = true;
1297 int dso__kernel_module_get_build_id(struct dso *dso,
1298 const char *root_dir)
1300 char filename[PATH_MAX];
1302 * kernel module short names are of the form "[module]" and
1303 * we need just "module" here.
1305 const char *name = dso->short_name + 1;
1307 snprintf(filename, sizeof(filename),
1308 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1309 root_dir, (int)strlen(name) - 1, name);
1311 if (sysfs__read_build_id(filename, dso->build_id,
1312 sizeof(dso->build_id)) == 0)
1313 dso->has_build_id = true;
1315 return 0;
1318 bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
1320 bool have_build_id = false;
1321 struct dso *pos;
1322 struct nscookie nsc;
1324 list_for_each_entry(pos, head, node) {
1325 if (with_hits && !pos->hit && !dso__is_vdso(pos))
1326 continue;
1327 if (pos->has_build_id) {
1328 have_build_id = true;
1329 continue;
1331 nsinfo__mountns_enter(pos->nsinfo, &nsc);
1332 if (filename__read_build_id(pos->long_name, pos->build_id,
1333 sizeof(pos->build_id)) > 0) {
1334 have_build_id = true;
1335 pos->has_build_id = true;
1337 nsinfo__mountns_exit(&nsc);
1340 return have_build_id;
1343 void __dsos__add(struct dsos *dsos, struct dso *dso)
1345 list_add_tail(&dso->node, &dsos->head);
1346 __dso__findlink_by_longname(&dsos->root, dso, NULL);
1348 * It is now in the linked list, grab a reference, then garbage collect
1349 * this when needing memory, by looking at LRU dso instances in the
1350 * list with atomic_read(&dso->refcnt) == 1, i.e. no references
1351 * anywhere besides the one for the list, do, under a lock for the
1352 * list: remove it from the list, then a dso__put(), that probably will
1353 * be the last and will then call dso__delete(), end of life.
1355 * That, or at the end of the 'struct machine' lifetime, when all
1356 * 'struct dso' instances will be removed from the list, in
1357 * dsos__exit(), if they have no other reference from some other data
1358 * structure.
1360 * E.g.: after processing a 'perf.data' file and storing references
1361 * to objects instantiated while processing events, we will have
1362 * references to the 'thread', 'map', 'dso' structs all from 'struct
1363 * hist_entry' instances, but we may not need anything not referenced,
1364 * so we might as well call machines__exit()/machines__delete() and
1365 * garbage collect it.
1367 dso__get(dso);
1370 void dsos__add(struct dsos *dsos, struct dso *dso)
1372 down_write(&dsos->lock);
1373 __dsos__add(dsos, dso);
1374 up_write(&dsos->lock);
1377 struct dso *__dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
1379 struct dso *pos;
1381 if (cmp_short) {
1382 list_for_each_entry(pos, &dsos->head, node)
1383 if (strcmp(pos->short_name, name) == 0)
1384 return pos;
1385 return NULL;
1387 return __dso__find_by_longname(&dsos->root, name);
1390 struct dso *dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
1392 struct dso *dso;
1393 down_read(&dsos->lock);
1394 dso = __dsos__find(dsos, name, cmp_short);
1395 up_read(&dsos->lock);
1396 return dso;
1399 struct dso *__dsos__addnew(struct dsos *dsos, const char *name)
1401 struct dso *dso = dso__new(name);
1403 if (dso != NULL) {
1404 __dsos__add(dsos, dso);
1405 dso__set_basename(dso);
1406 /* Put dso here because __dsos_add already got it */
1407 dso__put(dso);
1409 return dso;
1412 struct dso *__dsos__findnew(struct dsos *dsos, const char *name)
1414 struct dso *dso = __dsos__find(dsos, name, false);
1416 return dso ? dso : __dsos__addnew(dsos, name);
1419 struct dso *dsos__findnew(struct dsos *dsos, const char *name)
1421 struct dso *dso;
1422 down_write(&dsos->lock);
1423 dso = dso__get(__dsos__findnew(dsos, name));
1424 up_write(&dsos->lock);
1425 return dso;
1428 size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
1429 bool (skip)(struct dso *dso, int parm), int parm)
1431 struct dso *pos;
1432 size_t ret = 0;
1434 list_for_each_entry(pos, head, node) {
1435 if (skip && skip(pos, parm))
1436 continue;
1437 ret += dso__fprintf_buildid(pos, fp);
1438 ret += fprintf(fp, " %s\n", pos->long_name);
1440 return ret;
1443 size_t __dsos__fprintf(struct list_head *head, FILE *fp)
1445 struct dso *pos;
1446 size_t ret = 0;
1448 list_for_each_entry(pos, head, node) {
1449 ret += dso__fprintf(pos, fp);
1452 return ret;
1455 size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
1457 char sbuild_id[SBUILD_ID_SIZE];
1459 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1460 return fprintf(fp, "%s", sbuild_id);
1463 size_t dso__fprintf(struct dso *dso, FILE *fp)
1465 struct rb_node *nd;
1466 size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
1468 if (dso->short_name != dso->long_name)
1469 ret += fprintf(fp, "%s, ", dso->long_name);
1470 ret += fprintf(fp, "%sloaded, ", dso__loaded(dso) ? "" : "NOT ");
1471 ret += dso__fprintf_buildid(dso, fp);
1472 ret += fprintf(fp, ")\n");
1473 for (nd = rb_first_cached(&dso->symbols); nd; nd = rb_next(nd)) {
1474 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
1475 ret += symbol__fprintf(pos, fp);
1478 return ret;
1481 enum dso_type dso__type(struct dso *dso, struct machine *machine)
1483 int fd;
1484 enum dso_type type = DSO__TYPE_UNKNOWN;
1486 fd = dso__data_get_fd(dso, machine);
1487 if (fd >= 0) {
1488 type = dso__type_fd(fd);
1489 dso__data_put_fd(dso);
1492 return type;
1495 int dso__strerror_load(struct dso *dso, char *buf, size_t buflen)
1497 int idx, errnum = dso->load_errno;
1499 * This must have a same ordering as the enum dso_load_errno.
1501 static const char *dso_load__error_str[] = {
1502 "Internal tools/perf/ library error",
1503 "Invalid ELF file",
1504 "Can not read build id",
1505 "Mismatching build id",
1506 "Decompression failure",
1509 BUG_ON(buflen == 0);
1511 if (errnum >= 0) {
1512 const char *err = str_error_r(errnum, buf, buflen);
1514 if (err != buf)
1515 scnprintf(buf, buflen, "%s", err);
1517 return 0;
1520 if (errnum < __DSO_LOAD_ERRNO__START || errnum >= __DSO_LOAD_ERRNO__END)
1521 return -1;
1523 idx = errnum - __DSO_LOAD_ERRNO__START;
1524 scnprintf(buf, buflen, "%s", dso_load__error_str[idx]);
1525 return 0;