1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/kernel.h>
5 #include <sys/resource.h>
21 static const char * const debuglink_paths[] = {
28 char dso__symtab_origin(const struct dso *dso)
30 static const char origin[] = {
31 [DSO_BINARY_TYPE__KALLSYMS] = 'k',
32 [DSO_BINARY_TYPE__VMLINUX] = 'v',
33 [DSO_BINARY_TYPE__JAVA_JIT] = 'j',
34 [DSO_BINARY_TYPE__DEBUGLINK] = 'l',
35 [DSO_BINARY_TYPE__BUILD_ID_CACHE] = 'B',
36 [DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO] = 'D',
37 [DSO_BINARY_TYPE__FEDORA_DEBUGINFO] = 'f',
38 [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO] = 'u',
39 [DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO] = 'x',
40 [DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO] = 'o',
41 [DSO_BINARY_TYPE__BUILDID_DEBUGINFO] = 'b',
42 [DSO_BINARY_TYPE__SYSTEM_PATH_DSO] = 'd',
43 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE] = 'K',
44 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP] = 'm',
45 [DSO_BINARY_TYPE__GUEST_KALLSYMS] = 'g',
46 [DSO_BINARY_TYPE__GUEST_KMODULE] = 'G',
47 [DSO_BINARY_TYPE__GUEST_KMODULE_COMP] = 'M',
48 [DSO_BINARY_TYPE__GUEST_VMLINUX] = 'V',
51 if (dso == NULL || dso->symtab_type == DSO_BINARY_TYPE__NOT_FOUND)
53 return origin[dso->symtab_type];
56 int dso__read_binary_type_filename(const struct dso *dso,
57 enum dso_binary_type type,
58 char *root_dir, char *filename, size_t size)
60 char build_id_hex[SBUILD_ID_SIZE];
65 case DSO_BINARY_TYPE__DEBUGLINK:
67 const char *last_slash;
68 char dso_dir[PATH_MAX];
69 char symfile[PATH_MAX];
72 len = __symbol__join_symfs(filename, size, dso->long_name);
73 last_slash = filename + len;
74 while (last_slash != filename && *last_slash != '/')
77 strncpy(dso_dir, filename, last_slash - filename);
78 dso_dir[last_slash-filename] = '\0';
80 if (!is_regular_file(filename)) {
85 ret = filename__read_debuglink(filename, symfile, PATH_MAX);
89 /* Check predefined locations where debug file might reside */
91 for (i = 0; i < ARRAY_SIZE(debuglink_paths); i++) {
92 snprintf(filename, size,
93 debuglink_paths[i], dso_dir, symfile);
94 if (is_regular_file(filename)) {
102 case DSO_BINARY_TYPE__BUILD_ID_CACHE:
103 if (dso__build_id_filename(dso, filename, size, false) == NULL)
107 case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO:
108 if (dso__build_id_filename(dso, filename, size, true) == NULL)
112 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
113 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
114 snprintf(filename + len, size - len, "%s.debug", dso->long_name);
117 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
118 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
119 snprintf(filename + len, size - len, "%s", dso->long_name);
122 case DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO:
124 * Ubuntu can mixup /usr/lib with /lib, putting debuginfo in
125 * /usr/lib/debug/lib when it is expected to be in
126 * /usr/lib/debug/usr/lib
128 if (strlen(dso->long_name) < 9 ||
129 strncmp(dso->long_name, "/usr/lib/", 9)) {
133 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
134 snprintf(filename + len, size - len, "%s", dso->long_name + 4);
137 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
139 const char *last_slash;
142 last_slash = dso->long_name + dso->long_name_len;
143 while (last_slash != dso->long_name && *last_slash != '/')
146 len = __symbol__join_symfs(filename, size, "");
147 dir_size = last_slash - dso->long_name + 2;
148 if (dir_size > (size - len)) {
152 len += scnprintf(filename + len, dir_size, "%s", dso->long_name);
153 len += scnprintf(filename + len , size - len, ".debug%s",
158 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
159 if (!dso->has_build_id) {
164 build_id__sprintf(dso->build_id,
165 sizeof(dso->build_id),
167 len = __symbol__join_symfs(filename, size, "/usr/lib/debug/.build-id/");
168 snprintf(filename + len, size - len, "%.2s/%s.debug",
169 build_id_hex, build_id_hex + 2);
172 case DSO_BINARY_TYPE__VMLINUX:
173 case DSO_BINARY_TYPE__GUEST_VMLINUX:
174 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
175 __symbol__join_symfs(filename, size, dso->long_name);
178 case DSO_BINARY_TYPE__GUEST_KMODULE:
179 case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
180 path__join3(filename, size, symbol_conf.symfs,
181 root_dir, dso->long_name);
184 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
185 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
186 __symbol__join_symfs(filename, size, dso->long_name);
189 case DSO_BINARY_TYPE__KCORE:
190 case DSO_BINARY_TYPE__GUEST_KCORE:
191 snprintf(filename, size, "%s", dso->long_name);
195 case DSO_BINARY_TYPE__KALLSYMS:
196 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
197 case DSO_BINARY_TYPE__JAVA_JIT:
198 case DSO_BINARY_TYPE__NOT_FOUND:
206 static const struct {
208 int (*decompress)(const char *input, int output);
210 #ifdef HAVE_ZLIB_SUPPORT
211 { "gz", gzip_decompress_to_file },
213 #ifdef HAVE_LZMA_SUPPORT
214 { "xz", lzma_decompress_to_file },
219 bool is_supported_compression(const char *ext)
223 for (i = 0; compressions[i].fmt; i++) {
224 if (!strcmp(ext, compressions[i].fmt))
230 bool is_kernel_module(const char *pathname, int cpumode)
233 int mode = cpumode & PERF_RECORD_MISC_CPUMODE_MASK;
235 WARN_ONCE(mode != cpumode,
236 "Internal error: passing unmasked cpumode (%x) to is_kernel_module",
240 case PERF_RECORD_MISC_USER:
241 case PERF_RECORD_MISC_HYPERVISOR:
242 case PERF_RECORD_MISC_GUEST_USER:
244 /* Treat PERF_RECORD_MISC_CPUMODE_UNKNOWN as kernel */
246 if (kmod_path__parse(&m, pathname)) {
247 pr_err("Failed to check whether %s is a kernel module or not. Assume it is.",
256 bool decompress_to_file(const char *ext, const char *filename, int output_fd)
260 for (i = 0; compressions[i].fmt; i++) {
261 if (!strcmp(ext, compressions[i].fmt))
262 return !compressions[i].decompress(filename,
268 bool dso__needs_decompress(struct dso *dso)
270 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
271 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
274 static int decompress_kmodule(struct dso *dso, const char *name, char *tmpbuf)
279 if (!dso__needs_decompress(dso))
282 if (kmod_path__parse_ext(&m, dso->long_name))
288 fd = mkstemp(tmpbuf);
290 dso->load_errno = errno;
294 if (!decompress_to_file(m.ext, name, fd)) {
295 dso->load_errno = DSO_LOAD_ERRNO__DECOMPRESSION_FAILURE;
305 int dso__decompress_kmodule_fd(struct dso *dso, const char *name)
307 char tmpbuf[] = KMOD_DECOMP_NAME;
310 fd = decompress_kmodule(dso, name, tmpbuf);
315 int dso__decompress_kmodule_path(struct dso *dso, const char *name,
316 char *pathname, size_t len)
318 char tmpbuf[] = KMOD_DECOMP_NAME;
321 fd = decompress_kmodule(dso, name, tmpbuf);
327 strncpy(pathname, tmpbuf, len);
333 * Parses kernel module specified in @path and updates
336 * @comp - true if @path contains supported compression suffix,
338 * @kmod - true if @path contains '.ko' suffix in right position,
340 * @name - if (@alloc_name && @kmod) is true, it contains strdup-ed base name
341 * of the kernel module without suffixes, otherwise strudup-ed
343 * @ext - if (@alloc_ext && @comp) is true, it contains strdup-ed string
344 * the compression suffix
346 * Returns 0 if there's no strdup error, -ENOMEM otherwise.
348 int __kmod_path__parse(struct kmod_path *m, const char *path,
349 bool alloc_name, bool alloc_ext)
351 const char *name = strrchr(path, '/');
352 const char *ext = strrchr(path, '.');
353 bool is_simple_name = false;
355 memset(m, 0x0, sizeof(*m));
356 name = name ? name + 1 : path;
359 * '.' is also a valid character for module name. For example:
360 * [aaa.bbb] is a valid module name. '[' should have higher
361 * priority than '.ko' suffix.
363 * The kernel names are from machine__mmap_name. Such
364 * name should belong to kernel itself, not kernel module.
366 if (name[0] == '[') {
367 is_simple_name = true;
368 if ((strncmp(name, "[kernel.kallsyms]", 17) == 0) ||
369 (strncmp(name, "[guest.kernel.kallsyms", 22) == 0) ||
370 (strncmp(name, "[vdso]", 6) == 0) ||
371 (strncmp(name, "[vdso32]", 8) == 0) ||
372 (strncmp(name, "[vdsox32]", 9) == 0) ||
373 (strncmp(name, "[vsyscall]", 10) == 0)) {
380 /* No extension, just return name. */
381 if ((ext == NULL) || is_simple_name) {
383 m->name = strdup(name);
384 return m->name ? 0 : -ENOMEM;
389 if (is_supported_compression(ext + 1)) {
394 /* Check .ko extension only if there's enough name left. */
396 m->kmod = !strncmp(ext, ".ko", 3);
400 if (asprintf(&m->name, "[%.*s]", (int) (ext - name), name) == -1)
403 if (asprintf(&m->name, "%s", name) == -1)
407 strxfrchar(m->name, '-', '_');
410 if (alloc_ext && m->comp) {
411 m->ext = strdup(ext + 4);
413 free((void *) m->name);
421 void dso__set_module_info(struct dso *dso, struct kmod_path *m,
422 struct machine *machine)
424 if (machine__is_host(machine))
425 dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE;
427 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE;
429 /* _KMODULE_COMP should be next to _KMODULE */
430 if (m->kmod && m->comp)
433 dso__set_short_name(dso, strdup(m->name), true);
437 * Global list of open DSOs and the counter.
439 static LIST_HEAD(dso__data_open);
440 static long dso__data_open_cnt;
441 static pthread_mutex_t dso__data_open_lock = PTHREAD_MUTEX_INITIALIZER;
443 static void dso__list_add(struct dso *dso)
445 list_add_tail(&dso->data.open_entry, &dso__data_open);
446 dso__data_open_cnt++;
449 static void dso__list_del(struct dso *dso)
451 list_del(&dso->data.open_entry);
452 WARN_ONCE(dso__data_open_cnt <= 0,
453 "DSO data fd counter out of bounds.");
454 dso__data_open_cnt--;
457 static void close_first_dso(void);
459 static int do_open(char *name)
462 char sbuf[STRERR_BUFSIZE];
465 fd = open(name, O_RDONLY);
469 pr_debug("dso open failed: %s\n",
470 str_error_r(errno, sbuf, sizeof(sbuf)));
471 if (!dso__data_open_cnt || errno != EMFILE)
480 static int __open_dso(struct dso *dso, struct machine *machine)
483 char *root_dir = (char *)"";
484 char *name = malloc(PATH_MAX);
490 root_dir = machine->root_dir;
492 if (dso__read_binary_type_filename(dso, dso->binary_type,
493 root_dir, name, PATH_MAX))
496 if (!is_regular_file(name))
499 if (dso__needs_decompress(dso)) {
500 char newpath[KMOD_DECOMP_LEN];
501 size_t len = sizeof(newpath);
503 if (dso__decompress_kmodule_path(dso, name, newpath, len) < 0) {
504 fd = -dso->load_errno;
508 strcpy(name, newpath);
513 if (dso__needs_decompress(dso))
521 static void check_data_close(void);
524 * dso_close - Open DSO data file
527 * Open @dso's data file descriptor and updates
528 * list/count of open DSO objects.
530 static int open_dso(struct dso *dso, struct machine *machine)
535 if (dso->binary_type != DSO_BINARY_TYPE__BUILD_ID_CACHE)
536 nsinfo__mountns_enter(dso->nsinfo, &nsc);
537 fd = __open_dso(dso, machine);
538 if (dso->binary_type != DSO_BINARY_TYPE__BUILD_ID_CACHE)
539 nsinfo__mountns_exit(&nsc);
544 * Check if we crossed the allowed number
545 * of opened DSOs and close one if needed.
553 static void close_data_fd(struct dso *dso)
555 if (dso->data.fd >= 0) {
558 dso->data.file_size = 0;
564 * dso_close - Close DSO data file
567 * Close @dso's data file descriptor and updates
568 * list/count of open DSO objects.
570 static void close_dso(struct dso *dso)
575 static void close_first_dso(void)
579 dso = list_first_entry(&dso__data_open, struct dso, data.open_entry);
583 static rlim_t get_fd_limit(void)
588 /* Allow half of the current open fd limit. */
589 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
590 if (l.rlim_cur == RLIM_INFINITY)
593 limit = l.rlim_cur / 2;
595 pr_err("failed to get fd limit\n");
602 static rlim_t fd_limit;
605 * Used only by tests/dso-data.c to reset the environment
606 * for tests. I dont expect we should change this during
609 void reset_fd_limit(void)
614 static bool may_cache_fd(void)
617 fd_limit = get_fd_limit();
619 if (fd_limit == RLIM_INFINITY)
622 return fd_limit > (rlim_t) dso__data_open_cnt;
626 * Check and close LRU dso if we crossed allowed limit
627 * for opened dso file descriptors. The limit is half
628 * of the RLIMIT_NOFILE files opened.
630 static void check_data_close(void)
632 bool cache_fd = may_cache_fd();
639 * dso__data_close - Close DSO data file
642 * External interface to close @dso's data file descriptor.
644 void dso__data_close(struct dso *dso)
646 pthread_mutex_lock(&dso__data_open_lock);
648 pthread_mutex_unlock(&dso__data_open_lock);
651 static void try_to_open_dso(struct dso *dso, struct machine *machine)
653 enum dso_binary_type binary_type_data[] = {
654 DSO_BINARY_TYPE__BUILD_ID_CACHE,
655 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
656 DSO_BINARY_TYPE__NOT_FOUND,
660 if (dso->data.fd >= 0)
663 if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) {
664 dso->data.fd = open_dso(dso, machine);
669 dso->binary_type = binary_type_data[i++];
671 dso->data.fd = open_dso(dso, machine);
672 if (dso->data.fd >= 0)
675 } while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND);
677 if (dso->data.fd >= 0)
678 dso->data.status = DSO_DATA_STATUS_OK;
680 dso->data.status = DSO_DATA_STATUS_ERROR;
684 * dso__data_get_fd - Get dso's data file descriptor
686 * @machine: machine object
688 * External interface to find dso's file, open it and
689 * returns file descriptor. It should be paired with
690 * dso__data_put_fd() if it returns non-negative value.
692 int dso__data_get_fd(struct dso *dso, struct machine *machine)
694 if (dso->data.status == DSO_DATA_STATUS_ERROR)
697 if (pthread_mutex_lock(&dso__data_open_lock) < 0)
700 try_to_open_dso(dso, machine);
702 if (dso->data.fd < 0)
703 pthread_mutex_unlock(&dso__data_open_lock);
708 void dso__data_put_fd(struct dso *dso __maybe_unused)
710 pthread_mutex_unlock(&dso__data_open_lock);
713 bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by)
717 if (dso->data.status_seen & flag)
720 dso->data.status_seen |= flag;
726 dso_cache__free(struct dso *dso)
728 struct rb_root *root = &dso->data.cache;
729 struct rb_node *next = rb_first(root);
731 pthread_mutex_lock(&dso->lock);
733 struct dso_cache *cache;
735 cache = rb_entry(next, struct dso_cache, rb_node);
736 next = rb_next(&cache->rb_node);
737 rb_erase(&cache->rb_node, root);
740 pthread_mutex_unlock(&dso->lock);
743 static struct dso_cache *dso_cache__find(struct dso *dso, u64 offset)
745 const struct rb_root *root = &dso->data.cache;
746 struct rb_node * const *p = &root->rb_node;
747 const struct rb_node *parent = NULL;
748 struct dso_cache *cache;
754 cache = rb_entry(parent, struct dso_cache, rb_node);
755 end = cache->offset + DSO__DATA_CACHE_SIZE;
757 if (offset < cache->offset)
759 else if (offset >= end)
768 static struct dso_cache *
769 dso_cache__insert(struct dso *dso, struct dso_cache *new)
771 struct rb_root *root = &dso->data.cache;
772 struct rb_node **p = &root->rb_node;
773 struct rb_node *parent = NULL;
774 struct dso_cache *cache;
775 u64 offset = new->offset;
777 pthread_mutex_lock(&dso->lock);
782 cache = rb_entry(parent, struct dso_cache, rb_node);
783 end = cache->offset + DSO__DATA_CACHE_SIZE;
785 if (offset < cache->offset)
787 else if (offset >= end)
793 rb_link_node(&new->rb_node, parent, p);
794 rb_insert_color(&new->rb_node, root);
798 pthread_mutex_unlock(&dso->lock);
803 dso_cache__memcpy(struct dso_cache *cache, u64 offset,
806 u64 cache_offset = offset - cache->offset;
807 u64 cache_size = min(cache->size - cache_offset, size);
809 memcpy(data, cache->data + cache_offset, cache_size);
814 dso_cache__read(struct dso *dso, struct machine *machine,
815 u64 offset, u8 *data, ssize_t size)
817 struct dso_cache *cache;
818 struct dso_cache *old;
824 cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
828 pthread_mutex_lock(&dso__data_open_lock);
831 * dso->data.fd might be closed if other thread opened another
832 * file (dso) due to open file limit (RLIMIT_NOFILE).
834 try_to_open_dso(dso, machine);
836 if (dso->data.fd < 0) {
838 dso->data.status = DSO_DATA_STATUS_ERROR;
842 cache_offset = offset & DSO__DATA_CACHE_MASK;
844 ret = pread(dso->data.fd, cache->data, DSO__DATA_CACHE_SIZE, cache_offset);
848 cache->offset = cache_offset;
852 pthread_mutex_unlock(&dso__data_open_lock);
855 old = dso_cache__insert(dso, cache);
857 /* we lose the race */
862 ret = dso_cache__memcpy(cache, offset, data, size);
871 static ssize_t dso_cache_read(struct dso *dso, struct machine *machine,
872 u64 offset, u8 *data, ssize_t size)
874 struct dso_cache *cache;
876 cache = dso_cache__find(dso, offset);
878 return dso_cache__memcpy(cache, offset, data, size);
880 return dso_cache__read(dso, machine, offset, data, size);
884 * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
885 * in the rb_tree. Any read to already cached data is served
888 static ssize_t cached_read(struct dso *dso, struct machine *machine,
889 u64 offset, u8 *data, ssize_t size)
897 ret = dso_cache_read(dso, machine, offset, p, size);
901 /* Reached EOF, return what we have. */
917 static int data_file_size(struct dso *dso, struct machine *machine)
921 char sbuf[STRERR_BUFSIZE];
923 if (dso->data.file_size)
926 if (dso->data.status == DSO_DATA_STATUS_ERROR)
929 pthread_mutex_lock(&dso__data_open_lock);
932 * dso->data.fd might be closed if other thread opened another
933 * file (dso) due to open file limit (RLIMIT_NOFILE).
935 try_to_open_dso(dso, machine);
937 if (dso->data.fd < 0) {
939 dso->data.status = DSO_DATA_STATUS_ERROR;
943 if (fstat(dso->data.fd, &st) < 0) {
945 pr_err("dso cache fstat failed: %s\n",
946 str_error_r(errno, sbuf, sizeof(sbuf)));
947 dso->data.status = DSO_DATA_STATUS_ERROR;
950 dso->data.file_size = st.st_size;
953 pthread_mutex_unlock(&dso__data_open_lock);
958 * dso__data_size - Return dso data size
960 * @machine: machine object
962 * Return: dso data size
964 off_t dso__data_size(struct dso *dso, struct machine *machine)
966 if (data_file_size(dso, machine))
969 /* For now just estimate dso data size is close to file size */
970 return dso->data.file_size;
973 static ssize_t data_read_offset(struct dso *dso, struct machine *machine,
974 u64 offset, u8 *data, ssize_t size)
976 if (data_file_size(dso, machine))
979 /* Check the offset sanity. */
980 if (offset > dso->data.file_size)
983 if (offset + size < offset)
986 return cached_read(dso, machine, offset, data, size);
990 * dso__data_read_offset - Read data from dso file offset
992 * @machine: machine object
993 * @offset: file offset
994 * @data: buffer to store data
995 * @size: size of the @data buffer
997 * External interface to read data from dso file offset. Open
998 * dso data file and use cached_read to get the data.
1000 ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
1001 u64 offset, u8 *data, ssize_t size)
1003 if (dso->data.status == DSO_DATA_STATUS_ERROR)
1006 return data_read_offset(dso, machine, offset, data, size);
1010 * dso__data_read_addr - Read data from dso address
1012 * @machine: machine object
1013 * @add: virtual memory address
1014 * @data: buffer to store data
1015 * @size: size of the @data buffer
1017 * External interface to read data from dso address.
1019 ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
1020 struct machine *machine, u64 addr,
1021 u8 *data, ssize_t size)
1023 u64 offset = map->map_ip(map, addr);
1024 return dso__data_read_offset(dso, machine, offset, data, size);
1027 struct map *dso__new_map(const char *name)
1029 struct map *map = NULL;
1030 struct dso *dso = dso__new(name);
1033 map = map__new2(0, dso, MAP__FUNCTION);
1038 struct dso *machine__findnew_kernel(struct machine *machine, const char *name,
1039 const char *short_name, int dso_type)
1042 * The kernel dso could be created by build_id processing.
1044 struct dso *dso = machine__findnew_dso(machine, name);
1047 * We need to run this in all cases, since during the build_id
1048 * processing we had no idea this was the kernel dso.
1051 dso__set_short_name(dso, short_name, false);
1052 dso->kernel = dso_type;
1059 * Find a matching entry and/or link current entry to RB tree.
1060 * Either one of the dso or name parameter must be non-NULL or the
1061 * function will not work.
1063 static struct dso *__dso__findlink_by_longname(struct rb_root *root,
1064 struct dso *dso, const char *name)
1066 struct rb_node **p = &root->rb_node;
1067 struct rb_node *parent = NULL;
1070 name = dso->long_name;
1072 * Find node with the matching name
1075 struct dso *this = rb_entry(*p, struct dso, rb_node);
1076 int rc = strcmp(name, this->long_name);
1081 * In case the new DSO is a duplicate of an existing
1082 * one, print a one-time warning & put the new entry
1083 * at the end of the list of duplicates.
1085 if (!dso || (dso == this))
1086 return this; /* Find matching dso */
1088 * The core kernel DSOs may have duplicated long name.
1089 * In this case, the short name should be different.
1090 * Comparing the short names to differentiate the DSOs.
1092 rc = strcmp(dso->short_name, this->short_name);
1094 pr_err("Duplicated dso name: %s\n", name);
1099 p = &parent->rb_left;
1101 p = &parent->rb_right;
1104 /* Add new node and rebalance tree */
1105 rb_link_node(&dso->rb_node, parent, p);
1106 rb_insert_color(&dso->rb_node, root);
1112 static inline struct dso *__dso__find_by_longname(struct rb_root *root,
1115 return __dso__findlink_by_longname(root, NULL, name);
1118 void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
1120 struct rb_root *root = dso->root;
1125 if (dso->long_name_allocated)
1126 free((char *)dso->long_name);
1129 rb_erase(&dso->rb_node, root);
1131 * __dso__findlink_by_longname() isn't guaranteed to add it
1132 * back, so a clean removal is required here.
1134 RB_CLEAR_NODE(&dso->rb_node);
1138 dso->long_name = name;
1139 dso->long_name_len = strlen(name);
1140 dso->long_name_allocated = name_allocated;
1143 __dso__findlink_by_longname(root, dso, NULL);
1146 void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
1151 if (dso->short_name_allocated)
1152 free((char *)dso->short_name);
1154 dso->short_name = name;
1155 dso->short_name_len = strlen(name);
1156 dso->short_name_allocated = name_allocated;
1159 static void dso__set_basename(struct dso *dso)
1162 * basename() may modify path buffer, so we must pass
1165 char *base, *lname = strdup(dso->long_name);
1171 * basename() may return a pointer to internal
1172 * storage which is reused in subsequent calls
1173 * so copy the result.
1175 base = strdup(basename(lname));
1182 dso__set_short_name(dso, base, true);
1185 int dso__name_len(const struct dso *dso)
1188 return strlen("[unknown]");
1190 return dso->long_name_len;
1192 return dso->short_name_len;
1195 bool dso__loaded(const struct dso *dso, enum map_type type)
1197 return dso->loaded & (1 << type);
1200 bool dso__sorted_by_name(const struct dso *dso, enum map_type type)
1202 return dso->sorted_by_name & (1 << type);
1205 void dso__set_sorted_by_name(struct dso *dso, enum map_type type)
1207 dso->sorted_by_name |= (1 << type);
1210 struct dso *dso__new(const char *name)
1212 struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
1216 strcpy(dso->name, name);
1217 dso__set_long_name(dso, dso->name, false);
1218 dso__set_short_name(dso, dso->name, false);
1219 for (i = 0; i < MAP__NR_TYPES; ++i)
1220 dso->symbols[i] = dso->symbol_names[i] = RB_ROOT;
1221 dso->data.cache = RB_ROOT;
1223 dso->data.status = DSO_DATA_STATUS_UNKNOWN;
1224 dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
1225 dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
1226 dso->is_64_bit = (sizeof(void *) == 8);
1229 dso->sorted_by_name = 0;
1230 dso->has_build_id = 0;
1231 dso->has_srcline = 1;
1233 dso->kernel = DSO_TYPE_USER;
1234 dso->needs_swap = DSO_SWAP__UNSET;
1235 RB_CLEAR_NODE(&dso->rb_node);
1237 INIT_LIST_HEAD(&dso->node);
1238 INIT_LIST_HEAD(&dso->data.open_entry);
1239 pthread_mutex_init(&dso->lock, NULL);
1240 refcount_set(&dso->refcnt, 1);
1246 void dso__delete(struct dso *dso)
1250 if (!RB_EMPTY_NODE(&dso->rb_node))
1251 pr_err("DSO %s is still in rbtree when being deleted!\n",
1253 for (i = 0; i < MAP__NR_TYPES; ++i)
1254 symbols__delete(&dso->symbols[i]);
1256 if (dso->short_name_allocated) {
1257 zfree((char **)&dso->short_name);
1258 dso->short_name_allocated = false;
1261 if (dso->long_name_allocated) {
1262 zfree((char **)&dso->long_name);
1263 dso->long_name_allocated = false;
1266 dso__data_close(dso);
1267 auxtrace_cache__free(dso->auxtrace_cache);
1268 dso_cache__free(dso);
1270 zfree(&dso->symsrc_filename);
1271 nsinfo__zput(dso->nsinfo);
1272 pthread_mutex_destroy(&dso->lock);
1276 struct dso *dso__get(struct dso *dso)
1279 refcount_inc(&dso->refcnt);
1283 void dso__put(struct dso *dso)
1285 if (dso && refcount_dec_and_test(&dso->refcnt))
1289 void dso__set_build_id(struct dso *dso, void *build_id)
1291 memcpy(dso->build_id, build_id, sizeof(dso->build_id));
1292 dso->has_build_id = 1;
1295 bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
1297 return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
1300 void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
1302 char path[PATH_MAX];
1304 if (machine__is_default_guest(machine))
1306 sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
1307 if (sysfs__read_build_id(path, dso->build_id,
1308 sizeof(dso->build_id)) == 0)
1309 dso->has_build_id = true;
1312 int dso__kernel_module_get_build_id(struct dso *dso,
1313 const char *root_dir)
1315 char filename[PATH_MAX];
1317 * kernel module short names are of the form "[module]" and
1318 * we need just "module" here.
1320 const char *name = dso->short_name + 1;
1322 snprintf(filename, sizeof(filename),
1323 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1324 root_dir, (int)strlen(name) - 1, name);
1326 if (sysfs__read_build_id(filename, dso->build_id,
1327 sizeof(dso->build_id)) == 0)
1328 dso->has_build_id = true;
1333 bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
1335 bool have_build_id = false;
1337 struct nscookie nsc;
1339 list_for_each_entry(pos, head, node) {
1340 if (with_hits && !pos->hit && !dso__is_vdso(pos))
1342 if (pos->has_build_id) {
1343 have_build_id = true;
1346 nsinfo__mountns_enter(pos->nsinfo, &nsc);
1347 if (filename__read_build_id(pos->long_name, pos->build_id,
1348 sizeof(pos->build_id)) > 0) {
1349 have_build_id = true;
1350 pos->has_build_id = true;
1352 nsinfo__mountns_exit(&nsc);
1355 return have_build_id;
1358 void __dsos__add(struct dsos *dsos, struct dso *dso)
1360 list_add_tail(&dso->node, &dsos->head);
1361 __dso__findlink_by_longname(&dsos->root, dso, NULL);
1363 * It is now in the linked list, grab a reference, then garbage collect
1364 * this when needing memory, by looking at LRU dso instances in the
1365 * list with atomic_read(&dso->refcnt) == 1, i.e. no references
1366 * anywhere besides the one for the list, do, under a lock for the
1367 * list: remove it from the list, then a dso__put(), that probably will
1368 * be the last and will then call dso__delete(), end of life.
1370 * That, or at the end of the 'struct machine' lifetime, when all
1371 * 'struct dso' instances will be removed from the list, in
1372 * dsos__exit(), if they have no other reference from some other data
1375 * E.g.: after processing a 'perf.data' file and storing references
1376 * to objects instantiated while processing events, we will have
1377 * references to the 'thread', 'map', 'dso' structs all from 'struct
1378 * hist_entry' instances, but we may not need anything not referenced,
1379 * so we might as well call machines__exit()/machines__delete() and
1380 * garbage collect it.
1385 void dsos__add(struct dsos *dsos, struct dso *dso)
1387 pthread_rwlock_wrlock(&dsos->lock);
1388 __dsos__add(dsos, dso);
1389 pthread_rwlock_unlock(&dsos->lock);
1392 struct dso *__dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
1397 list_for_each_entry(pos, &dsos->head, node)
1398 if (strcmp(pos->short_name, name) == 0)
1402 return __dso__find_by_longname(&dsos->root, name);
1405 struct dso *dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
1408 pthread_rwlock_rdlock(&dsos->lock);
1409 dso = __dsos__find(dsos, name, cmp_short);
1410 pthread_rwlock_unlock(&dsos->lock);
1414 struct dso *__dsos__addnew(struct dsos *dsos, const char *name)
1416 struct dso *dso = dso__new(name);
1419 __dsos__add(dsos, dso);
1420 dso__set_basename(dso);
1421 /* Put dso here because __dsos_add already got it */
1427 struct dso *__dsos__findnew(struct dsos *dsos, const char *name)
1429 struct dso *dso = __dsos__find(dsos, name, false);
1431 return dso ? dso : __dsos__addnew(dsos, name);
1434 struct dso *dsos__findnew(struct dsos *dsos, const char *name)
1437 pthread_rwlock_wrlock(&dsos->lock);
1438 dso = dso__get(__dsos__findnew(dsos, name));
1439 pthread_rwlock_unlock(&dsos->lock);
1443 size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
1444 bool (skip)(struct dso *dso, int parm), int parm)
1449 list_for_each_entry(pos, head, node) {
1450 if (skip && skip(pos, parm))
1452 ret += dso__fprintf_buildid(pos, fp);
1453 ret += fprintf(fp, " %s\n", pos->long_name);
1458 size_t __dsos__fprintf(struct list_head *head, FILE *fp)
1463 list_for_each_entry(pos, head, node) {
1465 for (i = 0; i < MAP__NR_TYPES; ++i)
1466 ret += dso__fprintf(pos, i, fp);
1472 size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
1474 char sbuild_id[SBUILD_ID_SIZE];
1476 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1477 return fprintf(fp, "%s", sbuild_id);
1480 size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp)
1483 size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
1485 if (dso->short_name != dso->long_name)
1486 ret += fprintf(fp, "%s, ", dso->long_name);
1487 ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
1488 dso__loaded(dso, type) ? "" : "NOT ");
1489 ret += dso__fprintf_buildid(dso, fp);
1490 ret += fprintf(fp, ")\n");
1491 for (nd = rb_first(&dso->symbols[type]); nd; nd = rb_next(nd)) {
1492 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
1493 ret += symbol__fprintf(pos, fp);
1499 enum dso_type dso__type(struct dso *dso, struct machine *machine)
1502 enum dso_type type = DSO__TYPE_UNKNOWN;
1504 fd = dso__data_get_fd(dso, machine);
1506 type = dso__type_fd(fd);
1507 dso__data_put_fd(dso);
1513 int dso__strerror_load(struct dso *dso, char *buf, size_t buflen)
1515 int idx, errnum = dso->load_errno;
1517 * This must have a same ordering as the enum dso_load_errno.
1519 static const char *dso_load__error_str[] = {
1520 "Internal tools/perf/ library error",
1522 "Can not read build id",
1523 "Mismatching build id",
1524 "Decompression failure",
1527 BUG_ON(buflen == 0);
1530 const char *err = str_error_r(errnum, buf, buflen);
1533 scnprintf(buf, buflen, "%s", err);
1538 if (errnum < __DSO_LOAD_ERRNO__START || errnum >= __DSO_LOAD_ERRNO__END)
1541 idx = errnum - __DSO_LOAD_ERRNO__START;
1542 scnprintf(buf, buflen, "%s", dso_load__error_str[idx]);