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
);
1034 struct dso
*machine__findnew_kernel(struct machine
*machine
, const char *name
,
1035 const char *short_name
, int dso_type
)
1038 * The kernel dso could be created by build_id processing.
1040 struct dso
*dso
= machine__findnew_dso(machine
, name
);
1043 * We need to run this in all cases, since during the build_id
1044 * processing we had no idea this was the kernel dso.
1047 dso__set_short_name(dso
, short_name
, false);
1048 dso
->kernel
= dso_type
;
1055 * Find a matching entry and/or link current entry to RB tree.
1056 * Either one of the dso or name parameter must be non-NULL or the
1057 * function will not work.
1059 static struct dso
*__dso__findlink_by_longname(struct rb_root
*root
,
1060 struct dso
*dso
, const char *name
)
1062 struct rb_node
**p
= &root
->rb_node
;
1063 struct rb_node
*parent
= NULL
;
1066 name
= dso
->long_name
;
1068 * Find node with the matching name
1071 struct dso
*this = rb_entry(*p
, struct dso
, rb_node
);
1072 int rc
= strcmp(name
, this->long_name
);
1077 * In case the new DSO is a duplicate of an existing
1078 * one, print a one-time warning & put the new entry
1079 * at the end of the list of duplicates.
1081 if (!dso
|| (dso
== this))
1082 return this; /* Find matching dso */
1084 * The core kernel DSOs may have duplicated long name.
1085 * In this case, the short name should be different.
1086 * Comparing the short names to differentiate the DSOs.
1088 rc
= strcmp(dso
->short_name
, this->short_name
);
1090 pr_err("Duplicated dso name: %s\n", name
);
1095 p
= &parent
->rb_left
;
1097 p
= &parent
->rb_right
;
1100 /* Add new node and rebalance tree */
1101 rb_link_node(&dso
->rb_node
, parent
, p
);
1102 rb_insert_color(&dso
->rb_node
, root
);
1108 static inline struct dso
*__dso__find_by_longname(struct rb_root
*root
,
1111 return __dso__findlink_by_longname(root
, NULL
, name
);
1114 void dso__set_long_name(struct dso
*dso
, const char *name
, bool name_allocated
)
1116 struct rb_root
*root
= dso
->root
;
1121 if (dso
->long_name_allocated
)
1122 free((char *)dso
->long_name
);
1125 rb_erase(&dso
->rb_node
, root
);
1127 * __dso__findlink_by_longname() isn't guaranteed to add it
1128 * back, so a clean removal is required here.
1130 RB_CLEAR_NODE(&dso
->rb_node
);
1134 dso
->long_name
= name
;
1135 dso
->long_name_len
= strlen(name
);
1136 dso
->long_name_allocated
= name_allocated
;
1139 __dso__findlink_by_longname(root
, dso
, NULL
);
1142 void dso__set_short_name(struct dso
*dso
, const char *name
, bool name_allocated
)
1147 if (dso
->short_name_allocated
)
1148 free((char *)dso
->short_name
);
1150 dso
->short_name
= name
;
1151 dso
->short_name_len
= strlen(name
);
1152 dso
->short_name_allocated
= name_allocated
;
1155 static void dso__set_basename(struct dso
*dso
)
1158 * basename() may modify path buffer, so we must pass
1161 char *base
, *lname
= strdup(dso
->long_name
);
1167 * basename() may return a pointer to internal
1168 * storage which is reused in subsequent calls
1169 * so copy the result.
1171 base
= strdup(basename(lname
));
1178 dso__set_short_name(dso
, base
, true);
1181 int dso__name_len(const struct dso
*dso
)
1184 return strlen("[unknown]");
1186 return dso
->long_name_len
;
1188 return dso
->short_name_len
;
1191 bool dso__loaded(const struct dso
*dso
)
1196 bool dso__sorted_by_name(const struct dso
*dso
)
1198 return dso
->sorted_by_name
;
1201 void dso__set_sorted_by_name(struct dso
*dso
)
1203 dso
->sorted_by_name
= true;
1206 struct dso
*dso__new(const char *name
)
1208 struct dso
*dso
= calloc(1, sizeof(*dso
) + strlen(name
) + 1);
1211 strcpy(dso
->name
, name
);
1212 dso__set_long_name(dso
, dso
->name
, false);
1213 dso__set_short_name(dso
, dso
->name
, false);
1214 dso
->symbols
= dso
->symbol_names
= RB_ROOT
;
1215 dso
->data
.cache
= RB_ROOT
;
1216 dso
->inlined_nodes
= RB_ROOT
;
1217 dso
->srclines
= RB_ROOT
;
1219 dso
->data
.status
= DSO_DATA_STATUS_UNKNOWN
;
1220 dso
->symtab_type
= DSO_BINARY_TYPE__NOT_FOUND
;
1221 dso
->binary_type
= DSO_BINARY_TYPE__NOT_FOUND
;
1222 dso
->is_64_bit
= (sizeof(void *) == 8);
1225 dso
->sorted_by_name
= 0;
1226 dso
->has_build_id
= 0;
1227 dso
->has_srcline
= 1;
1229 dso
->kernel
= DSO_TYPE_USER
;
1230 dso
->needs_swap
= DSO_SWAP__UNSET
;
1231 dso
->comp
= COMP_ID__NONE
;
1232 RB_CLEAR_NODE(&dso
->rb_node
);
1234 INIT_LIST_HEAD(&dso
->node
);
1235 INIT_LIST_HEAD(&dso
->data
.open_entry
);
1236 pthread_mutex_init(&dso
->lock
, NULL
);
1237 refcount_set(&dso
->refcnt
, 1);
1243 void dso__delete(struct dso
*dso
)
1245 if (!RB_EMPTY_NODE(&dso
->rb_node
))
1246 pr_err("DSO %s is still in rbtree when being deleted!\n",
1249 /* free inlines first, as they reference symbols */
1250 inlines__tree_delete(&dso
->inlined_nodes
);
1251 srcline__tree_delete(&dso
->srclines
);
1252 symbols__delete(&dso
->symbols
);
1254 if (dso
->short_name_allocated
) {
1255 zfree((char **)&dso
->short_name
);
1256 dso
->short_name_allocated
= false;
1259 if (dso
->long_name_allocated
) {
1260 zfree((char **)&dso
->long_name
);
1261 dso
->long_name_allocated
= false;
1264 dso__data_close(dso
);
1265 auxtrace_cache__free(dso
->auxtrace_cache
);
1266 dso_cache__free(dso
);
1268 zfree(&dso
->symsrc_filename
);
1269 nsinfo__zput(dso
->nsinfo
);
1270 pthread_mutex_destroy(&dso
->lock
);
1274 struct dso
*dso__get(struct dso
*dso
)
1277 refcount_inc(&dso
->refcnt
);
1281 void dso__put(struct dso
*dso
)
1283 if (dso
&& refcount_dec_and_test(&dso
->refcnt
))
1287 void dso__set_build_id(struct dso
*dso
, void *build_id
)
1289 memcpy(dso
->build_id
, build_id
, sizeof(dso
->build_id
));
1290 dso
->has_build_id
= 1;
1293 bool dso__build_id_equal(const struct dso
*dso
, u8
*build_id
)
1295 return memcmp(dso
->build_id
, build_id
, sizeof(dso
->build_id
)) == 0;
1298 void dso__read_running_kernel_build_id(struct dso
*dso
, struct machine
*machine
)
1300 char path
[PATH_MAX
];
1302 if (machine__is_default_guest(machine
))
1304 sprintf(path
, "%s/sys/kernel/notes", machine
->root_dir
);
1305 if (sysfs__read_build_id(path
, dso
->build_id
,
1306 sizeof(dso
->build_id
)) == 0)
1307 dso
->has_build_id
= true;
1310 int dso__kernel_module_get_build_id(struct dso
*dso
,
1311 const char *root_dir
)
1313 char filename
[PATH_MAX
];
1315 * kernel module short names are of the form "[module]" and
1316 * we need just "module" here.
1318 const char *name
= dso
->short_name
+ 1;
1320 snprintf(filename
, sizeof(filename
),
1321 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1322 root_dir
, (int)strlen(name
) - 1, name
);
1324 if (sysfs__read_build_id(filename
, dso
->build_id
,
1325 sizeof(dso
->build_id
)) == 0)
1326 dso
->has_build_id
= true;
1331 bool __dsos__read_build_ids(struct list_head
*head
, bool with_hits
)
1333 bool have_build_id
= false;
1335 struct nscookie nsc
;
1337 list_for_each_entry(pos
, head
, node
) {
1338 if (with_hits
&& !pos
->hit
&& !dso__is_vdso(pos
))
1340 if (pos
->has_build_id
) {
1341 have_build_id
= true;
1344 nsinfo__mountns_enter(pos
->nsinfo
, &nsc
);
1345 if (filename__read_build_id(pos
->long_name
, pos
->build_id
,
1346 sizeof(pos
->build_id
)) > 0) {
1347 have_build_id
= true;
1348 pos
->has_build_id
= true;
1350 nsinfo__mountns_exit(&nsc
);
1353 return have_build_id
;
1356 void __dsos__add(struct dsos
*dsos
, struct dso
*dso
)
1358 list_add_tail(&dso
->node
, &dsos
->head
);
1359 __dso__findlink_by_longname(&dsos
->root
, dso
, NULL
);
1361 * It is now in the linked list, grab a reference, then garbage collect
1362 * this when needing memory, by looking at LRU dso instances in the
1363 * list with atomic_read(&dso->refcnt) == 1, i.e. no references
1364 * anywhere besides the one for the list, do, under a lock for the
1365 * list: remove it from the list, then a dso__put(), that probably will
1366 * be the last and will then call dso__delete(), end of life.
1368 * That, or at the end of the 'struct machine' lifetime, when all
1369 * 'struct dso' instances will be removed from the list, in
1370 * dsos__exit(), if they have no other reference from some other data
1373 * E.g.: after processing a 'perf.data' file and storing references
1374 * to objects instantiated while processing events, we will have
1375 * references to the 'thread', 'map', 'dso' structs all from 'struct
1376 * hist_entry' instances, but we may not need anything not referenced,
1377 * so we might as well call machines__exit()/machines__delete() and
1378 * garbage collect it.
1383 void dsos__add(struct dsos
*dsos
, struct dso
*dso
)
1385 down_write(&dsos
->lock
);
1386 __dsos__add(dsos
, dso
);
1387 up_write(&dsos
->lock
);
1390 struct dso
*__dsos__find(struct dsos
*dsos
, const char *name
, bool cmp_short
)
1395 list_for_each_entry(pos
, &dsos
->head
, node
)
1396 if (strcmp(pos
->short_name
, name
) == 0)
1400 return __dso__find_by_longname(&dsos
->root
, name
);
1403 struct dso
*dsos__find(struct dsos
*dsos
, const char *name
, bool cmp_short
)
1406 down_read(&dsos
->lock
);
1407 dso
= __dsos__find(dsos
, name
, cmp_short
);
1408 up_read(&dsos
->lock
);
1412 struct dso
*__dsos__addnew(struct dsos
*dsos
, const char *name
)
1414 struct dso
*dso
= dso__new(name
);
1417 __dsos__add(dsos
, dso
);
1418 dso__set_basename(dso
);
1419 /* Put dso here because __dsos_add already got it */
1425 struct dso
*__dsos__findnew(struct dsos
*dsos
, const char *name
)
1427 struct dso
*dso
= __dsos__find(dsos
, name
, false);
1429 return dso
? dso
: __dsos__addnew(dsos
, name
);
1432 struct dso
*dsos__findnew(struct dsos
*dsos
, const char *name
)
1435 down_write(&dsos
->lock
);
1436 dso
= dso__get(__dsos__findnew(dsos
, name
));
1437 up_write(&dsos
->lock
);
1441 size_t __dsos__fprintf_buildid(struct list_head
*head
, FILE *fp
,
1442 bool (skip
)(struct dso
*dso
, int parm
), int parm
)
1447 list_for_each_entry(pos
, head
, node
) {
1448 if (skip
&& skip(pos
, parm
))
1450 ret
+= dso__fprintf_buildid(pos
, fp
);
1451 ret
+= fprintf(fp
, " %s\n", pos
->long_name
);
1456 size_t __dsos__fprintf(struct list_head
*head
, FILE *fp
)
1461 list_for_each_entry(pos
, head
, node
) {
1462 ret
+= dso__fprintf(pos
, fp
);
1468 size_t dso__fprintf_buildid(struct dso
*dso
, FILE *fp
)
1470 char sbuild_id
[SBUILD_ID_SIZE
];
1472 build_id__sprintf(dso
->build_id
, sizeof(dso
->build_id
), sbuild_id
);
1473 return fprintf(fp
, "%s", sbuild_id
);
1476 size_t dso__fprintf(struct dso
*dso
, FILE *fp
)
1479 size_t ret
= fprintf(fp
, "dso: %s (", dso
->short_name
);
1481 if (dso
->short_name
!= dso
->long_name
)
1482 ret
+= fprintf(fp
, "%s, ", dso
->long_name
);
1483 ret
+= fprintf(fp
, "%sloaded, ", dso__loaded(dso
) ? "" : "NOT ");
1484 ret
+= dso__fprintf_buildid(dso
, fp
);
1485 ret
+= fprintf(fp
, ")\n");
1486 for (nd
= rb_first(&dso
->symbols
); nd
; nd
= rb_next(nd
)) {
1487 struct symbol
*pos
= rb_entry(nd
, struct symbol
, rb_node
);
1488 ret
+= symbol__fprintf(pos
, fp
);
1494 enum dso_type
dso__type(struct dso
*dso
, struct machine
*machine
)
1497 enum dso_type type
= DSO__TYPE_UNKNOWN
;
1499 fd
= dso__data_get_fd(dso
, machine
);
1501 type
= dso__type_fd(fd
);
1502 dso__data_put_fd(dso
);
1508 int dso__strerror_load(struct dso
*dso
, char *buf
, size_t buflen
)
1510 int idx
, errnum
= dso
->load_errno
;
1512 * This must have a same ordering as the enum dso_load_errno.
1514 static const char *dso_load__error_str
[] = {
1515 "Internal tools/perf/ library error",
1517 "Can not read build id",
1518 "Mismatching build id",
1519 "Decompression failure",
1522 BUG_ON(buflen
== 0);
1525 const char *err
= str_error_r(errnum
, buf
, buflen
);
1528 scnprintf(buf
, buflen
, "%s", err
);
1533 if (errnum
< __DSO_LOAD_ERRNO__START
|| errnum
>= __DSO_LOAD_ERRNO__END
)
1536 idx
= errnum
- __DSO_LOAD_ERRNO__START
;
1537 scnprintf(buf
, buflen
, "%s", dso_load__error_str
[idx
]);