1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/kernel.h>
5 #include <sys/resource.h>
23 static const char * const debuglink_paths[] = {
30 char dso__symtab_origin(const struct dso *dso)
32 static const char origin[] = {
33 [DSO_BINARY_TYPE__KALLSYMS] = 'k',
34 [DSO_BINARY_TYPE__VMLINUX] = 'v',
35 [DSO_BINARY_TYPE__JAVA_JIT] = 'j',
36 [DSO_BINARY_TYPE__DEBUGLINK] = 'l',
37 [DSO_BINARY_TYPE__BUILD_ID_CACHE] = 'B',
38 [DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO] = 'D',
39 [DSO_BINARY_TYPE__FEDORA_DEBUGINFO] = 'f',
40 [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO] = 'u',
41 [DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO] = 'x',
42 [DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO] = 'o',
43 [DSO_BINARY_TYPE__BUILDID_DEBUGINFO] = 'b',
44 [DSO_BINARY_TYPE__SYSTEM_PATH_DSO] = 'd',
45 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE] = 'K',
46 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP] = 'm',
47 [DSO_BINARY_TYPE__GUEST_KALLSYMS] = 'g',
48 [DSO_BINARY_TYPE__GUEST_KMODULE] = 'G',
49 [DSO_BINARY_TYPE__GUEST_KMODULE_COMP] = 'M',
50 [DSO_BINARY_TYPE__GUEST_VMLINUX] = 'V',
53 if (dso == NULL || dso->symtab_type == DSO_BINARY_TYPE__NOT_FOUND)
55 return origin[dso->symtab_type];
58 int dso__read_binary_type_filename(const struct dso *dso,
59 enum dso_binary_type type,
60 char *root_dir, char *filename, size_t size)
62 char build_id_hex[SBUILD_ID_SIZE];
67 case DSO_BINARY_TYPE__DEBUGLINK:
69 const char *last_slash;
70 char dso_dir[PATH_MAX];
71 char symfile[PATH_MAX];
74 len = __symbol__join_symfs(filename, size, dso->long_name);
75 last_slash = filename + len;
76 while (last_slash != filename && *last_slash != '/')
79 strncpy(dso_dir, filename, last_slash - filename);
80 dso_dir[last_slash-filename] = '\0';
82 if (!is_regular_file(filename)) {
87 ret = filename__read_debuglink(filename, symfile, PATH_MAX);
91 /* Check predefined locations where debug file might reside */
93 for (i = 0; i < ARRAY_SIZE(debuglink_paths); i++) {
94 snprintf(filename, size,
95 debuglink_paths[i], dso_dir, symfile);
96 if (is_regular_file(filename)) {
104 case DSO_BINARY_TYPE__BUILD_ID_CACHE:
105 if (dso__build_id_filename(dso, filename, size, false) == NULL)
109 case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO:
110 if (dso__build_id_filename(dso, filename, size, true) == NULL)
114 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
115 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
116 snprintf(filename + len, size - len, "%s.debug", dso->long_name);
119 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
120 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
121 snprintf(filename + len, size - len, "%s", dso->long_name);
124 case DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO:
126 * Ubuntu can mixup /usr/lib with /lib, putting debuginfo in
127 * /usr/lib/debug/lib when it is expected to be in
128 * /usr/lib/debug/usr/lib
130 if (strlen(dso->long_name) < 9 ||
131 strncmp(dso->long_name, "/usr/lib/", 9)) {
135 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
136 snprintf(filename + len, size - len, "%s", dso->long_name + 4);
139 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
141 const char *last_slash;
144 last_slash = dso->long_name + dso->long_name_len;
145 while (last_slash != dso->long_name && *last_slash != '/')
148 len = __symbol__join_symfs(filename, size, "");
149 dir_size = last_slash - dso->long_name + 2;
150 if (dir_size > (size - len)) {
154 len += scnprintf(filename + len, dir_size, "%s", dso->long_name);
155 len += scnprintf(filename + len , size - len, ".debug%s",
160 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
161 if (!dso->has_build_id) {
166 build_id__sprintf(dso->build_id,
167 sizeof(dso->build_id),
169 len = __symbol__join_symfs(filename, size, "/usr/lib/debug/.build-id/");
170 snprintf(filename + len, size - len, "%.2s/%s.debug",
171 build_id_hex, build_id_hex + 2);
174 case DSO_BINARY_TYPE__VMLINUX:
175 case DSO_BINARY_TYPE__GUEST_VMLINUX:
176 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
177 __symbol__join_symfs(filename, size, dso->long_name);
180 case DSO_BINARY_TYPE__GUEST_KMODULE:
181 case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
182 path__join3(filename, size, symbol_conf.symfs,
183 root_dir, dso->long_name);
186 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
187 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
188 __symbol__join_symfs(filename, size, dso->long_name);
191 case DSO_BINARY_TYPE__KCORE:
192 case DSO_BINARY_TYPE__GUEST_KCORE:
193 snprintf(filename, size, "%s", dso->long_name);
197 case DSO_BINARY_TYPE__KALLSYMS:
198 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
199 case DSO_BINARY_TYPE__JAVA_JIT:
200 case DSO_BINARY_TYPE__NOT_FOUND:
212 static const struct {
214 int (*decompress)(const char *input, int output);
215 bool (*is_compressed)(const char *input);
217 [COMP_ID__NONE] = { .fmt = NULL, },
218 #ifdef HAVE_ZLIB_SUPPORT
219 { "gz", gzip_decompress_to_file, gzip_is_compressed },
221 #ifdef HAVE_LZMA_SUPPORT
222 { "xz", lzma_decompress_to_file, lzma_is_compressed },
224 { NULL, NULL, NULL },
227 static int is_supported_compression(const char *ext)
231 for (i = 1; compressions[i].fmt; i++) {
232 if (!strcmp(ext, compressions[i].fmt))
235 return COMP_ID__NONE;
238 bool is_kernel_module(const char *pathname, int cpumode)
241 int mode = cpumode & PERF_RECORD_MISC_CPUMODE_MASK;
243 WARN_ONCE(mode != cpumode,
244 "Internal error: passing unmasked cpumode (%x) to is_kernel_module",
248 case PERF_RECORD_MISC_USER:
249 case PERF_RECORD_MISC_HYPERVISOR:
250 case PERF_RECORD_MISC_GUEST_USER:
252 /* Treat PERF_RECORD_MISC_CPUMODE_UNKNOWN as kernel */
254 if (kmod_path__parse(&m, pathname)) {
255 pr_err("Failed to check whether %s is a kernel module or not. Assume it is.",
264 bool dso__needs_decompress(struct dso *dso)
266 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
267 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
270 static int decompress_kmodule(struct dso *dso, const char *name,
271 char *pathname, size_t len)
273 char tmpbuf[] = KMOD_DECOMP_NAME;
276 if (!dso__needs_decompress(dso))
279 if (dso->comp == COMP_ID__NONE)
283 * We have proper compression id for DSO and yet the file
284 * behind the 'name' can still be plain uncompressed object.
286 * The reason is behind the logic we open the DSO object files,
287 * when we try all possible 'debug' objects until we find the
288 * data. So even if the DSO is represented by 'krava.xz' module,
289 * we can end up here opening ~/.debug/....23432432/debug' file
290 * which is not compressed.
292 * To keep this transparent, we detect this and return the file
293 * descriptor to the uncompressed file.
295 if (!compressions[dso->comp].is_compressed(name))
296 return open(name, O_RDONLY);
298 fd = mkstemp(tmpbuf);
300 dso->load_errno = errno;
304 if (compressions[dso->comp].decompress(name, fd)) {
305 dso->load_errno = DSO_LOAD_ERRNO__DECOMPRESSION_FAILURE;
310 if (!pathname || (fd < 0))
313 if (pathname && (fd >= 0))
314 strlcpy(pathname, tmpbuf, len);
319 int dso__decompress_kmodule_fd(struct dso *dso, const char *name)
321 return decompress_kmodule(dso, name, NULL, 0);
324 int dso__decompress_kmodule_path(struct dso *dso, const char *name,
325 char *pathname, size_t len)
327 int fd = decompress_kmodule(dso, name, pathname, len);
330 return fd >= 0 ? 0 : -1;
334 * Parses kernel module specified in @path and updates
337 * @comp - true if @path contains supported compression suffix,
339 * @kmod - true if @path contains '.ko' suffix in right position,
341 * @name - if (@alloc_name && @kmod) is true, it contains strdup-ed base name
342 * of the kernel module without suffixes, otherwise strudup-ed
344 * @ext - if (@alloc_ext && @comp) is true, it contains strdup-ed string
345 * the compression suffix
347 * Returns 0 if there's no strdup error, -ENOMEM otherwise.
349 int __kmod_path__parse(struct kmod_path *m, const char *path,
352 const char *name = strrchr(path, '/');
353 const char *ext = strrchr(path, '.');
354 bool is_simple_name = false;
356 memset(m, 0x0, sizeof(*m));
357 name = name ? name + 1 : path;
360 * '.' is also a valid character for module name. For example:
361 * [aaa.bbb] is a valid module name. '[' should have higher
362 * priority than '.ko' suffix.
364 * The kernel names are from machine__mmap_name. Such
365 * name should belong to kernel itself, not kernel module.
367 if (name[0] == '[') {
368 is_simple_name = true;
369 if ((strncmp(name, "[kernel.kallsyms]", 17) == 0) ||
370 (strncmp(name, "[guest.kernel.kallsyms", 22) == 0) ||
371 (strncmp(name, "[vdso]", 6) == 0) ||
372 (strncmp(name, "[vdso32]", 8) == 0) ||
373 (strncmp(name, "[vdsox32]", 9) == 0) ||
374 (strncmp(name, "[vsyscall]", 10) == 0)) {
381 /* No extension, just return name. */
382 if ((ext == NULL) || is_simple_name) {
384 m->name = strdup(name);
385 return m->name ? 0 : -ENOMEM;
390 m->comp = is_supported_compression(ext + 1);
391 if (m->comp > COMP_ID__NONE)
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, '-', '_');
413 void dso__set_module_info(struct dso *dso, struct kmod_path *m,
414 struct machine *machine)
416 if (machine__is_host(machine))
417 dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE;
419 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE;
421 /* _KMODULE_COMP should be next to _KMODULE */
422 if (m->kmod && m->comp) {
427 dso__set_short_name(dso, strdup(m->name), true);
431 * Global list of open DSOs and the counter.
433 static LIST_HEAD(dso__data_open);
434 static long dso__data_open_cnt;
435 static pthread_mutex_t dso__data_open_lock = PTHREAD_MUTEX_INITIALIZER;
437 static void dso__list_add(struct dso *dso)
439 list_add_tail(&dso->data.open_entry, &dso__data_open);
440 dso__data_open_cnt++;
443 static void dso__list_del(struct dso *dso)
445 list_del(&dso->data.open_entry);
446 WARN_ONCE(dso__data_open_cnt <= 0,
447 "DSO data fd counter out of bounds.");
448 dso__data_open_cnt--;
451 static void close_first_dso(void);
453 static int do_open(char *name)
456 char sbuf[STRERR_BUFSIZE];
459 fd = open(name, O_RDONLY|O_CLOEXEC);
463 pr_debug("dso open failed: %s\n",
464 str_error_r(errno, sbuf, sizeof(sbuf)));
465 if (!dso__data_open_cnt || errno != EMFILE)
474 static int __open_dso(struct dso *dso, struct machine *machine)
477 char *root_dir = (char *)"";
478 char *name = malloc(PATH_MAX);
485 root_dir = machine->root_dir;
487 if (dso__read_binary_type_filename(dso, dso->binary_type,
488 root_dir, name, PATH_MAX))
491 if (!is_regular_file(name))
494 if (dso__needs_decompress(dso)) {
495 char newpath[KMOD_DECOMP_LEN];
496 size_t len = sizeof(newpath);
498 if (dso__decompress_kmodule_path(dso, name, newpath, len) < 0) {
499 fd = -dso->load_errno;
504 strcpy(name, newpath);
517 static void check_data_close(void);
520 * dso_close - Open DSO data file
523 * Open @dso's data file descriptor and updates
524 * list/count of open DSO objects.
526 static int open_dso(struct dso *dso, struct machine *machine)
531 if (dso->binary_type != DSO_BINARY_TYPE__BUILD_ID_CACHE)
532 nsinfo__mountns_enter(dso->nsinfo, &nsc);
533 fd = __open_dso(dso, machine);
534 if (dso->binary_type != DSO_BINARY_TYPE__BUILD_ID_CACHE)
535 nsinfo__mountns_exit(&nsc);
540 * Check if we crossed the allowed number
541 * of opened DSOs and close one if needed.
549 static void close_data_fd(struct dso *dso)
551 if (dso->data.fd >= 0) {
554 dso->data.file_size = 0;
560 * dso_close - Close DSO data file
563 * Close @dso's data file descriptor and updates
564 * list/count of open DSO objects.
566 static void close_dso(struct dso *dso)
571 static void close_first_dso(void)
575 dso = list_first_entry(&dso__data_open, struct dso, data.open_entry);
579 static rlim_t get_fd_limit(void)
584 /* Allow half of the current open fd limit. */
585 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
586 if (l.rlim_cur == RLIM_INFINITY)
589 limit = l.rlim_cur / 2;
591 pr_err("failed to get fd limit\n");
598 static rlim_t fd_limit;
601 * Used only by tests/dso-data.c to reset the environment
602 * for tests. I dont expect we should change this during
605 void reset_fd_limit(void)
610 static bool may_cache_fd(void)
613 fd_limit = get_fd_limit();
615 if (fd_limit == RLIM_INFINITY)
618 return fd_limit > (rlim_t) dso__data_open_cnt;
622 * Check and close LRU dso if we crossed allowed limit
623 * for opened dso file descriptors. The limit is half
624 * of the RLIMIT_NOFILE files opened.
626 static void check_data_close(void)
628 bool cache_fd = may_cache_fd();
635 * dso__data_close - Close DSO data file
638 * External interface to close @dso's data file descriptor.
640 void dso__data_close(struct dso *dso)
642 pthread_mutex_lock(&dso__data_open_lock);
644 pthread_mutex_unlock(&dso__data_open_lock);
647 static void try_to_open_dso(struct dso *dso, struct machine *machine)
649 enum dso_binary_type binary_type_data[] = {
650 DSO_BINARY_TYPE__BUILD_ID_CACHE,
651 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
652 DSO_BINARY_TYPE__NOT_FOUND,
656 if (dso->data.fd >= 0)
659 if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) {
660 dso->data.fd = open_dso(dso, machine);
665 dso->binary_type = binary_type_data[i++];
667 dso->data.fd = open_dso(dso, machine);
668 if (dso->data.fd >= 0)
671 } while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND);
673 if (dso->data.fd >= 0)
674 dso->data.status = DSO_DATA_STATUS_OK;
676 dso->data.status = DSO_DATA_STATUS_ERROR;
680 * dso__data_get_fd - Get dso's data file descriptor
682 * @machine: machine object
684 * External interface to find dso's file, open it and
685 * returns file descriptor. It should be paired with
686 * dso__data_put_fd() if it returns non-negative value.
688 int dso__data_get_fd(struct dso *dso, struct machine *machine)
690 if (dso->data.status == DSO_DATA_STATUS_ERROR)
693 if (pthread_mutex_lock(&dso__data_open_lock) < 0)
696 try_to_open_dso(dso, machine);
698 if (dso->data.fd < 0)
699 pthread_mutex_unlock(&dso__data_open_lock);
704 void dso__data_put_fd(struct dso *dso __maybe_unused)
706 pthread_mutex_unlock(&dso__data_open_lock);
709 bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by)
713 if (dso->data.status_seen & flag)
716 dso->data.status_seen |= flag;
722 dso_cache__free(struct dso *dso)
724 struct rb_root *root = &dso->data.cache;
725 struct rb_node *next = rb_first(root);
727 pthread_mutex_lock(&dso->lock);
729 struct dso_cache *cache;
731 cache = rb_entry(next, struct dso_cache, rb_node);
732 next = rb_next(&cache->rb_node);
733 rb_erase(&cache->rb_node, root);
736 pthread_mutex_unlock(&dso->lock);
739 static struct dso_cache *dso_cache__find(struct dso *dso, u64 offset)
741 const struct rb_root *root = &dso->data.cache;
742 struct rb_node * const *p = &root->rb_node;
743 const struct rb_node *parent = NULL;
744 struct dso_cache *cache;
750 cache = rb_entry(parent, struct dso_cache, rb_node);
751 end = cache->offset + DSO__DATA_CACHE_SIZE;
753 if (offset < cache->offset)
755 else if (offset >= end)
764 static struct dso_cache *
765 dso_cache__insert(struct dso *dso, struct dso_cache *new)
767 struct rb_root *root = &dso->data.cache;
768 struct rb_node **p = &root->rb_node;
769 struct rb_node *parent = NULL;
770 struct dso_cache *cache;
771 u64 offset = new->offset;
773 pthread_mutex_lock(&dso->lock);
778 cache = rb_entry(parent, struct dso_cache, rb_node);
779 end = cache->offset + DSO__DATA_CACHE_SIZE;
781 if (offset < cache->offset)
783 else if (offset >= end)
789 rb_link_node(&new->rb_node, parent, p);
790 rb_insert_color(&new->rb_node, root);
794 pthread_mutex_unlock(&dso->lock);
799 dso_cache__memcpy(struct dso_cache *cache, u64 offset,
802 u64 cache_offset = offset - cache->offset;
803 u64 cache_size = min(cache->size - cache_offset, size);
805 memcpy(data, cache->data + cache_offset, cache_size);
810 dso_cache__read(struct dso *dso, struct machine *machine,
811 u64 offset, u8 *data, ssize_t size)
813 struct dso_cache *cache;
814 struct dso_cache *old;
820 cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
824 pthread_mutex_lock(&dso__data_open_lock);
827 * dso->data.fd might be closed if other thread opened another
828 * file (dso) due to open file limit (RLIMIT_NOFILE).
830 try_to_open_dso(dso, machine);
832 if (dso->data.fd < 0) {
834 dso->data.status = DSO_DATA_STATUS_ERROR;
838 cache_offset = offset & DSO__DATA_CACHE_MASK;
840 ret = pread(dso->data.fd, cache->data, DSO__DATA_CACHE_SIZE, cache_offset);
844 cache->offset = cache_offset;
848 pthread_mutex_unlock(&dso__data_open_lock);
851 old = dso_cache__insert(dso, cache);
853 /* we lose the race */
858 ret = dso_cache__memcpy(cache, offset, data, size);
867 static ssize_t dso_cache_read(struct dso *dso, struct machine *machine,
868 u64 offset, u8 *data, ssize_t size)
870 struct dso_cache *cache;
872 cache = dso_cache__find(dso, offset);
874 return dso_cache__memcpy(cache, offset, data, size);
876 return dso_cache__read(dso, machine, offset, data, size);
880 * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
881 * in the rb_tree. Any read to already cached data is served
884 static ssize_t cached_read(struct dso *dso, struct machine *machine,
885 u64 offset, u8 *data, ssize_t size)
893 ret = dso_cache_read(dso, machine, offset, p, size);
897 /* Reached EOF, return what we have. */
913 static int data_file_size(struct dso *dso, struct machine *machine)
917 char sbuf[STRERR_BUFSIZE];
919 if (dso->data.file_size)
922 if (dso->data.status == DSO_DATA_STATUS_ERROR)
925 pthread_mutex_lock(&dso__data_open_lock);
928 * dso->data.fd might be closed if other thread opened another
929 * file (dso) due to open file limit (RLIMIT_NOFILE).
931 try_to_open_dso(dso, machine);
933 if (dso->data.fd < 0) {
935 dso->data.status = DSO_DATA_STATUS_ERROR;
939 if (fstat(dso->data.fd, &st) < 0) {
941 pr_err("dso cache fstat failed: %s\n",
942 str_error_r(errno, sbuf, sizeof(sbuf)));
943 dso->data.status = DSO_DATA_STATUS_ERROR;
946 dso->data.file_size = st.st_size;
949 pthread_mutex_unlock(&dso__data_open_lock);
954 * dso__data_size - Return dso data size
956 * @machine: machine object
958 * Return: dso data size
960 off_t dso__data_size(struct dso *dso, struct machine *machine)
962 if (data_file_size(dso, machine))
965 /* For now just estimate dso data size is close to file size */
966 return dso->data.file_size;
969 static ssize_t data_read_offset(struct dso *dso, struct machine *machine,
970 u64 offset, u8 *data, ssize_t size)
972 if (data_file_size(dso, machine))
975 /* Check the offset sanity. */
976 if (offset > dso->data.file_size)
979 if (offset + size < offset)
982 return cached_read(dso, machine, offset, data, size);
986 * dso__data_read_offset - Read data from dso file offset
988 * @machine: machine object
989 * @offset: file offset
990 * @data: buffer to store data
991 * @size: size of the @data buffer
993 * External interface to read data from dso file offset. Open
994 * dso data file and use cached_read to get the data.
996 ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
997 u64 offset, u8 *data, ssize_t size)
999 if (dso->data.status == DSO_DATA_STATUS_ERROR)
1002 return data_read_offset(dso, machine, offset, data, size);
1006 * dso__data_read_addr - Read data from dso address
1008 * @machine: machine object
1009 * @add: virtual memory address
1010 * @data: buffer to store data
1011 * @size: size of the @data buffer
1013 * External interface to read data from dso address.
1015 ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
1016 struct machine *machine, u64 addr,
1017 u8 *data, ssize_t size)
1019 u64 offset = map->map_ip(map, addr);
1020 return dso__data_read_offset(dso, machine, offset, data, size);
1023 struct map *dso__new_map(const char *name)
1025 struct map *map = NULL;
1026 struct dso *dso = dso__new(name);
1029 map = map__new2(0, dso);
1036 struct dso *machine__findnew_kernel(struct machine *machine, const char *name,
1037 const char *short_name, int dso_type)
1040 * The kernel dso could be created by build_id processing.
1042 struct dso *dso = machine__findnew_dso(machine, name);
1045 * We need to run this in all cases, since during the build_id
1046 * processing we had no idea this was the kernel dso.
1049 dso__set_short_name(dso, short_name, false);
1050 dso->kernel = dso_type;
1057 * Find a matching entry and/or link current entry to RB tree.
1058 * Either one of the dso or name parameter must be non-NULL or the
1059 * function will not work.
1061 static struct dso *__dso__findlink_by_longname(struct rb_root *root,
1062 struct dso *dso, const char *name)
1064 struct rb_node **p = &root->rb_node;
1065 struct rb_node *parent = NULL;
1068 name = dso->long_name;
1070 * Find node with the matching name
1073 struct dso *this = rb_entry(*p, struct dso, rb_node);
1074 int rc = strcmp(name, this->long_name);
1079 * In case the new DSO is a duplicate of an existing
1080 * one, print a one-time warning & put the new entry
1081 * at the end of the list of duplicates.
1083 if (!dso || (dso == this))
1084 return this; /* Find matching dso */
1086 * The core kernel DSOs may have duplicated long name.
1087 * In this case, the short name should be different.
1088 * Comparing the short names to differentiate the DSOs.
1090 rc = strcmp(dso->short_name, this->short_name);
1092 pr_err("Duplicated dso name: %s\n", name);
1097 p = &parent->rb_left;
1099 p = &parent->rb_right;
1102 /* Add new node and rebalance tree */
1103 rb_link_node(&dso->rb_node, parent, p);
1104 rb_insert_color(&dso->rb_node, root);
1110 static inline struct dso *__dso__find_by_longname(struct rb_root *root,
1113 return __dso__findlink_by_longname(root, NULL, name);
1116 void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
1118 struct rb_root *root = dso->root;
1123 if (dso->long_name_allocated)
1124 free((char *)dso->long_name);
1127 rb_erase(&dso->rb_node, root);
1129 * __dso__findlink_by_longname() isn't guaranteed to add it
1130 * back, so a clean removal is required here.
1132 RB_CLEAR_NODE(&dso->rb_node);
1136 dso->long_name = name;
1137 dso->long_name_len = strlen(name);
1138 dso->long_name_allocated = name_allocated;
1141 __dso__findlink_by_longname(root, dso, NULL);
1144 void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
1149 if (dso->short_name_allocated)
1150 free((char *)dso->short_name);
1152 dso->short_name = name;
1153 dso->short_name_len = strlen(name);
1154 dso->short_name_allocated = name_allocated;
1157 static void dso__set_basename(struct dso *dso)
1160 * basename() may modify path buffer, so we must pass
1163 char *base, *lname = strdup(dso->long_name);
1169 * basename() may return a pointer to internal
1170 * storage which is reused in subsequent calls
1171 * so copy the result.
1173 base = strdup(basename(lname));
1180 dso__set_short_name(dso, base, true);
1183 int dso__name_len(const struct dso *dso)
1186 return strlen("[unknown]");
1188 return dso->long_name_len;
1190 return dso->short_name_len;
1193 bool dso__loaded(const struct dso *dso)
1198 bool dso__sorted_by_name(const struct dso *dso)
1200 return dso->sorted_by_name;
1203 void dso__set_sorted_by_name(struct dso *dso)
1205 dso->sorted_by_name = true;
1208 struct dso *dso__new(const char *name)
1210 struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
1213 strcpy(dso->name, name);
1214 dso__set_long_name(dso, dso->name, false);
1215 dso__set_short_name(dso, dso->name, false);
1216 dso->symbols = dso->symbol_names = RB_ROOT;
1217 dso->data.cache = RB_ROOT;
1218 dso->inlined_nodes = RB_ROOT;
1219 dso->srclines = RB_ROOT;
1221 dso->data.status = DSO_DATA_STATUS_UNKNOWN;
1222 dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
1223 dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
1224 dso->is_64_bit = (sizeof(void *) == 8);
1227 dso->sorted_by_name = 0;
1228 dso->has_build_id = 0;
1229 dso->has_srcline = 1;
1231 dso->kernel = DSO_TYPE_USER;
1232 dso->needs_swap = DSO_SWAP__UNSET;
1233 dso->comp = COMP_ID__NONE;
1234 RB_CLEAR_NODE(&dso->rb_node);
1236 INIT_LIST_HEAD(&dso->node);
1237 INIT_LIST_HEAD(&dso->data.open_entry);
1238 pthread_mutex_init(&dso->lock, NULL);
1239 refcount_set(&dso->refcnt, 1);
1245 void dso__delete(struct dso *dso)
1247 if (!RB_EMPTY_NODE(&dso->rb_node))
1248 pr_err("DSO %s is still in rbtree when being deleted!\n",
1251 /* free inlines first, as they reference symbols */
1252 inlines__tree_delete(&dso->inlined_nodes);
1253 srcline__tree_delete(&dso->srclines);
1254 symbols__delete(&dso->symbols);
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 down_write(&dsos->lock);
1388 __dsos__add(dsos, dso);
1389 up_write(&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 down_read(&dsos->lock);
1409 dso = __dsos__find(dsos, name, cmp_short);
1410 up_read(&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 down_write(&dsos->lock);
1438 dso = dso__get(__dsos__findnew(dsos, name));
1439 up_write(&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) {
1464 ret += dso__fprintf(pos, fp);
1470 size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
1472 char sbuild_id[SBUILD_ID_SIZE];
1474 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1475 return fprintf(fp, "%s", sbuild_id);
1478 size_t dso__fprintf(struct dso *dso, FILE *fp)
1481 size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
1483 if (dso->short_name != dso->long_name)
1484 ret += fprintf(fp, "%s, ", dso->long_name);
1485 ret += fprintf(fp, "%sloaded, ", dso__loaded(dso) ? "" : "NOT ");
1486 ret += dso__fprintf_buildid(dso, fp);
1487 ret += fprintf(fp, ")\n");
1488 for (nd = rb_first(&dso->symbols); nd; nd = rb_next(nd)) {
1489 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
1490 ret += symbol__fprintf(pos, fp);
1496 enum dso_type dso__type(struct dso *dso, struct machine *machine)
1499 enum dso_type type = DSO__TYPE_UNKNOWN;
1501 fd = dso__data_get_fd(dso, machine);
1503 type = dso__type_fd(fd);
1504 dso__data_put_fd(dso);
1510 int dso__strerror_load(struct dso *dso, char *buf, size_t buflen)
1512 int idx, errnum = dso->load_errno;
1514 * This must have a same ordering as the enum dso_load_errno.
1516 static const char *dso_load__error_str[] = {
1517 "Internal tools/perf/ library error",
1519 "Can not read build id",
1520 "Mismatching build id",
1521 "Decompression failure",
1524 BUG_ON(buflen == 0);
1527 const char *err = str_error_r(errnum, buf, buflen);
1530 scnprintf(buf, buflen, "%s", err);
1535 if (errnum < __DSO_LOAD_ERRNO__START || errnum >= __DSO_LOAD_ERRNO__END)
1538 idx = errnum - __DSO_LOAD_ERRNO__START;
1539 scnprintf(buf, buflen, "%s", dso_load__error_str[idx]);