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__OPENEMBEDDED_DEBUGINFO
] = 'o',
42 [DSO_BINARY_TYPE__BUILDID_DEBUGINFO
] = 'b',
43 [DSO_BINARY_TYPE__SYSTEM_PATH_DSO
] = 'd',
44 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE
] = 'K',
45 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP
] = 'm',
46 [DSO_BINARY_TYPE__GUEST_KALLSYMS
] = 'g',
47 [DSO_BINARY_TYPE__GUEST_KMODULE
] = 'G',
48 [DSO_BINARY_TYPE__GUEST_KMODULE_COMP
] = 'M',
49 [DSO_BINARY_TYPE__GUEST_VMLINUX
] = 'V',
52 if (dso
== NULL
|| dso
->symtab_type
== DSO_BINARY_TYPE__NOT_FOUND
)
54 return origin
[dso
->symtab_type
];
57 int dso__read_binary_type_filename(const struct dso
*dso
,
58 enum dso_binary_type type
,
59 char *root_dir
, char *filename
, size_t size
)
61 char build_id_hex
[SBUILD_ID_SIZE
];
66 case DSO_BINARY_TYPE__DEBUGLINK
:
68 const char *last_slash
;
69 char dso_dir
[PATH_MAX
];
70 char symfile
[PATH_MAX
];
73 len
= __symbol__join_symfs(filename
, size
, dso
->long_name
);
74 last_slash
= filename
+ len
;
75 while (last_slash
!= filename
&& *last_slash
!= '/')
78 strncpy(dso_dir
, filename
, last_slash
- filename
);
79 dso_dir
[last_slash
-filename
] = '\0';
81 if (!is_regular_file(filename
)) {
86 ret
= filename__read_debuglink(filename
, symfile
, PATH_MAX
);
90 /* Check predefined locations where debug file might reside */
92 for (i
= 0; i
< ARRAY_SIZE(debuglink_paths
); i
++) {
93 snprintf(filename
, size
,
94 debuglink_paths
[i
], dso_dir
, symfile
);
95 if (is_regular_file(filename
)) {
103 case DSO_BINARY_TYPE__BUILD_ID_CACHE
:
104 if (dso__build_id_filename(dso
, filename
, size
, false) == NULL
)
108 case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO
:
109 if (dso__build_id_filename(dso
, filename
, size
, true) == NULL
)
113 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO
:
114 len
= __symbol__join_symfs(filename
, size
, "/usr/lib/debug");
115 snprintf(filename
+ len
, size
- len
, "%s.debug", dso
->long_name
);
118 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO
:
119 len
= __symbol__join_symfs(filename
, size
, "/usr/lib/debug");
120 snprintf(filename
+ len
, size
- len
, "%s", dso
->long_name
);
123 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO
:
125 const char *last_slash
;
128 last_slash
= dso
->long_name
+ dso
->long_name_len
;
129 while (last_slash
!= dso
->long_name
&& *last_slash
!= '/')
132 len
= __symbol__join_symfs(filename
, size
, "");
133 dir_size
= last_slash
- dso
->long_name
+ 2;
134 if (dir_size
> (size
- len
)) {
138 len
+= scnprintf(filename
+ len
, dir_size
, "%s", dso
->long_name
);
139 len
+= scnprintf(filename
+ len
, size
- len
, ".debug%s",
144 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO
:
145 if (!dso
->has_build_id
) {
150 build_id__sprintf(dso
->build_id
,
151 sizeof(dso
->build_id
),
153 len
= __symbol__join_symfs(filename
, size
, "/usr/lib/debug/.build-id/");
154 snprintf(filename
+ len
, size
- len
, "%.2s/%s.debug",
155 build_id_hex
, build_id_hex
+ 2);
158 case DSO_BINARY_TYPE__VMLINUX
:
159 case DSO_BINARY_TYPE__GUEST_VMLINUX
:
160 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO
:
161 __symbol__join_symfs(filename
, size
, dso
->long_name
);
164 case DSO_BINARY_TYPE__GUEST_KMODULE
:
165 case DSO_BINARY_TYPE__GUEST_KMODULE_COMP
:
166 path__join3(filename
, size
, symbol_conf
.symfs
,
167 root_dir
, dso
->long_name
);
170 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE
:
171 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP
:
172 __symbol__join_symfs(filename
, size
, dso
->long_name
);
175 case DSO_BINARY_TYPE__KCORE
:
176 case DSO_BINARY_TYPE__GUEST_KCORE
:
177 snprintf(filename
, size
, "%s", dso
->long_name
);
181 case DSO_BINARY_TYPE__KALLSYMS
:
182 case DSO_BINARY_TYPE__GUEST_KALLSYMS
:
183 case DSO_BINARY_TYPE__JAVA_JIT
:
184 case DSO_BINARY_TYPE__NOT_FOUND
:
196 static const struct {
198 int (*decompress
)(const char *input
, int output
);
199 bool (*is_compressed
)(const char *input
);
201 [COMP_ID__NONE
] = { .fmt
= NULL
, },
202 #ifdef HAVE_ZLIB_SUPPORT
203 { "gz", gzip_decompress_to_file
, gzip_is_compressed
},
205 #ifdef HAVE_LZMA_SUPPORT
206 { "xz", lzma_decompress_to_file
, lzma_is_compressed
},
208 { NULL
, NULL
, NULL
},
211 static int is_supported_compression(const char *ext
)
215 for (i
= 1; compressions
[i
].fmt
; i
++) {
216 if (!strcmp(ext
, compressions
[i
].fmt
))
219 return COMP_ID__NONE
;
222 bool is_kernel_module(const char *pathname
, int cpumode
)
225 int mode
= cpumode
& PERF_RECORD_MISC_CPUMODE_MASK
;
227 WARN_ONCE(mode
!= cpumode
,
228 "Internal error: passing unmasked cpumode (%x) to is_kernel_module",
232 case PERF_RECORD_MISC_USER
:
233 case PERF_RECORD_MISC_HYPERVISOR
:
234 case PERF_RECORD_MISC_GUEST_USER
:
236 /* Treat PERF_RECORD_MISC_CPUMODE_UNKNOWN as kernel */
238 if (kmod_path__parse(&m
, pathname
)) {
239 pr_err("Failed to check whether %s is a kernel module or not. Assume it is.",
248 bool dso__needs_decompress(struct dso
*dso
)
250 return dso
->symtab_type
== DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP
||
251 dso
->symtab_type
== DSO_BINARY_TYPE__GUEST_KMODULE_COMP
;
254 static int decompress_kmodule(struct dso
*dso
, const char *name
,
255 char *pathname
, size_t len
)
257 char tmpbuf
[] = KMOD_DECOMP_NAME
;
260 if (!dso__needs_decompress(dso
))
263 if (dso
->comp
== COMP_ID__NONE
)
267 * We have proper compression id for DSO and yet the file
268 * behind the 'name' can still be plain uncompressed object.
270 * The reason is behind the logic we open the DSO object files,
271 * when we try all possible 'debug' objects until we find the
272 * data. So even if the DSO is represented by 'krava.xz' module,
273 * we can end up here opening ~/.debug/....23432432/debug' file
274 * which is not compressed.
276 * To keep this transparent, we detect this and return the file
277 * descriptor to the uncompressed file.
279 if (!compressions
[dso
->comp
].is_compressed(name
))
280 return open(name
, O_RDONLY
);
282 fd
= mkstemp(tmpbuf
);
284 dso
->load_errno
= errno
;
288 if (compressions
[dso
->comp
].decompress(name
, fd
)) {
289 dso
->load_errno
= DSO_LOAD_ERRNO__DECOMPRESSION_FAILURE
;
294 if (!pathname
|| (fd
< 0))
297 if (pathname
&& (fd
>= 0))
298 strncpy(pathname
, tmpbuf
, len
);
303 int dso__decompress_kmodule_fd(struct dso
*dso
, const char *name
)
305 return decompress_kmodule(dso
, name
, NULL
, 0);
308 int dso__decompress_kmodule_path(struct dso
*dso
, const char *name
,
309 char *pathname
, size_t len
)
311 int fd
= decompress_kmodule(dso
, name
, pathname
, len
);
314 return fd
>= 0 ? 0 : -1;
318 * Parses kernel module specified in @path and updates
321 * @comp - true if @path contains supported compression suffix,
323 * @kmod - true if @path contains '.ko' suffix in right position,
325 * @name - if (@alloc_name && @kmod) is true, it contains strdup-ed base name
326 * of the kernel module without suffixes, otherwise strudup-ed
328 * @ext - if (@alloc_ext && @comp) is true, it contains strdup-ed string
329 * the compression suffix
331 * Returns 0 if there's no strdup error, -ENOMEM otherwise.
333 int __kmod_path__parse(struct kmod_path
*m
, const char *path
,
336 const char *name
= strrchr(path
, '/');
337 const char *ext
= strrchr(path
, '.');
338 bool is_simple_name
= false;
340 memset(m
, 0x0, sizeof(*m
));
341 name
= name
? name
+ 1 : path
;
344 * '.' is also a valid character for module name. For example:
345 * [aaa.bbb] is a valid module name. '[' should have higher
346 * priority than '.ko' suffix.
348 * The kernel names are from machine__mmap_name. Such
349 * name should belong to kernel itself, not kernel module.
351 if (name
[0] == '[') {
352 is_simple_name
= true;
353 if ((strncmp(name
, "[kernel.kallsyms]", 17) == 0) ||
354 (strncmp(name
, "[guest.kernel.kallsyms", 22) == 0) ||
355 (strncmp(name
, "[vdso]", 6) == 0) ||
356 (strncmp(name
, "[vdso32]", 8) == 0) ||
357 (strncmp(name
, "[vdsox32]", 9) == 0) ||
358 (strncmp(name
, "[vsyscall]", 10) == 0)) {
365 /* No extension, just return name. */
366 if ((ext
== NULL
) || is_simple_name
) {
368 m
->name
= strdup(name
);
369 return m
->name
? 0 : -ENOMEM
;
374 m
->comp
= is_supported_compression(ext
+ 1);
375 if (m
->comp
> COMP_ID__NONE
)
378 /* Check .ko extension only if there's enough name left. */
380 m
->kmod
= !strncmp(ext
, ".ko", 3);
384 if (asprintf(&m
->name
, "[%.*s]", (int) (ext
- name
), name
) == -1)
387 if (asprintf(&m
->name
, "%s", name
) == -1)
391 strxfrchar(m
->name
, '-', '_');
397 void dso__set_module_info(struct dso
*dso
, struct kmod_path
*m
,
398 struct machine
*machine
)
400 if (machine__is_host(machine
))
401 dso
->symtab_type
= DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE
;
403 dso
->symtab_type
= DSO_BINARY_TYPE__GUEST_KMODULE
;
405 /* _KMODULE_COMP should be next to _KMODULE */
406 if (m
->kmod
&& m
->comp
) {
411 dso__set_short_name(dso
, strdup(m
->name
), true);
415 * Global list of open DSOs and the counter.
417 static LIST_HEAD(dso__data_open
);
418 static long dso__data_open_cnt
;
419 static pthread_mutex_t dso__data_open_lock
= PTHREAD_MUTEX_INITIALIZER
;
421 static void dso__list_add(struct dso
*dso
)
423 list_add_tail(&dso
->data
.open_entry
, &dso__data_open
);
424 dso__data_open_cnt
++;
427 static void dso__list_del(struct dso
*dso
)
429 list_del(&dso
->data
.open_entry
);
430 WARN_ONCE(dso__data_open_cnt
<= 0,
431 "DSO data fd counter out of bounds.");
432 dso__data_open_cnt
--;
435 static void close_first_dso(void);
437 static int do_open(char *name
)
440 char sbuf
[STRERR_BUFSIZE
];
443 fd
= open(name
, O_RDONLY
|O_CLOEXEC
);
447 pr_debug("dso open failed: %s\n",
448 str_error_r(errno
, sbuf
, sizeof(sbuf
)));
449 if (!dso__data_open_cnt
|| errno
!= EMFILE
)
458 static int __open_dso(struct dso
*dso
, struct machine
*machine
)
461 char *root_dir
= (char *)"";
462 char *name
= malloc(PATH_MAX
);
469 root_dir
= machine
->root_dir
;
471 if (dso__read_binary_type_filename(dso
, dso
->binary_type
,
472 root_dir
, name
, PATH_MAX
))
475 if (!is_regular_file(name
))
478 if (dso__needs_decompress(dso
)) {
479 char newpath
[KMOD_DECOMP_LEN
];
480 size_t len
= sizeof(newpath
);
482 if (dso__decompress_kmodule_path(dso
, name
, newpath
, len
) < 0) {
483 fd
= -dso
->load_errno
;
488 strcpy(name
, newpath
);
501 static void check_data_close(void);
504 * dso_close - Open DSO data file
507 * Open @dso's data file descriptor and updates
508 * list/count of open DSO objects.
510 static int open_dso(struct dso
*dso
, struct machine
*machine
)
515 if (dso
->binary_type
!= DSO_BINARY_TYPE__BUILD_ID_CACHE
)
516 nsinfo__mountns_enter(dso
->nsinfo
, &nsc
);
517 fd
= __open_dso(dso
, machine
);
518 if (dso
->binary_type
!= DSO_BINARY_TYPE__BUILD_ID_CACHE
)
519 nsinfo__mountns_exit(&nsc
);
524 * Check if we crossed the allowed number
525 * of opened DSOs and close one if needed.
533 static void close_data_fd(struct dso
*dso
)
535 if (dso
->data
.fd
>= 0) {
538 dso
->data
.file_size
= 0;
544 * dso_close - Close DSO data file
547 * Close @dso's data file descriptor and updates
548 * list/count of open DSO objects.
550 static void close_dso(struct dso
*dso
)
555 static void close_first_dso(void)
559 dso
= list_first_entry(&dso__data_open
, struct dso
, data
.open_entry
);
563 static rlim_t
get_fd_limit(void)
568 /* Allow half of the current open fd limit. */
569 if (getrlimit(RLIMIT_NOFILE
, &l
) == 0) {
570 if (l
.rlim_cur
== RLIM_INFINITY
)
573 limit
= l
.rlim_cur
/ 2;
575 pr_err("failed to get fd limit\n");
582 static rlim_t fd_limit
;
585 * Used only by tests/dso-data.c to reset the environment
586 * for tests. I dont expect we should change this during
589 void reset_fd_limit(void)
594 static bool may_cache_fd(void)
597 fd_limit
= get_fd_limit();
599 if (fd_limit
== RLIM_INFINITY
)
602 return fd_limit
> (rlim_t
) dso__data_open_cnt
;
606 * Check and close LRU dso if we crossed allowed limit
607 * for opened dso file descriptors. The limit is half
608 * of the RLIMIT_NOFILE files opened.
610 static void check_data_close(void)
612 bool cache_fd
= may_cache_fd();
619 * dso__data_close - Close DSO data file
622 * External interface to close @dso's data file descriptor.
624 void dso__data_close(struct dso
*dso
)
626 pthread_mutex_lock(&dso__data_open_lock
);
628 pthread_mutex_unlock(&dso__data_open_lock
);
631 static void try_to_open_dso(struct dso
*dso
, struct machine
*machine
)
633 enum dso_binary_type binary_type_data
[] = {
634 DSO_BINARY_TYPE__BUILD_ID_CACHE
,
635 DSO_BINARY_TYPE__SYSTEM_PATH_DSO
,
636 DSO_BINARY_TYPE__NOT_FOUND
,
640 if (dso
->data
.fd
>= 0)
643 if (dso
->binary_type
!= DSO_BINARY_TYPE__NOT_FOUND
) {
644 dso
->data
.fd
= open_dso(dso
, machine
);
649 dso
->binary_type
= binary_type_data
[i
++];
651 dso
->data
.fd
= open_dso(dso
, machine
);
652 if (dso
->data
.fd
>= 0)
655 } while (dso
->binary_type
!= DSO_BINARY_TYPE__NOT_FOUND
);
657 if (dso
->data
.fd
>= 0)
658 dso
->data
.status
= DSO_DATA_STATUS_OK
;
660 dso
->data
.status
= DSO_DATA_STATUS_ERROR
;
664 * dso__data_get_fd - Get dso's data file descriptor
666 * @machine: machine object
668 * External interface to find dso's file, open it and
669 * returns file descriptor. It should be paired with
670 * dso__data_put_fd() if it returns non-negative value.
672 int dso__data_get_fd(struct dso
*dso
, struct machine
*machine
)
674 if (dso
->data
.status
== DSO_DATA_STATUS_ERROR
)
677 if (pthread_mutex_lock(&dso__data_open_lock
) < 0)
680 try_to_open_dso(dso
, machine
);
682 if (dso
->data
.fd
< 0)
683 pthread_mutex_unlock(&dso__data_open_lock
);
688 void dso__data_put_fd(struct dso
*dso __maybe_unused
)
690 pthread_mutex_unlock(&dso__data_open_lock
);
693 bool dso__data_status_seen(struct dso
*dso
, enum dso_data_status_seen by
)
697 if (dso
->data
.status_seen
& flag
)
700 dso
->data
.status_seen
|= flag
;
706 dso_cache__free(struct dso
*dso
)
708 struct rb_root
*root
= &dso
->data
.cache
;
709 struct rb_node
*next
= rb_first(root
);
711 pthread_mutex_lock(&dso
->lock
);
713 struct dso_cache
*cache
;
715 cache
= rb_entry(next
, struct dso_cache
, rb_node
);
716 next
= rb_next(&cache
->rb_node
);
717 rb_erase(&cache
->rb_node
, root
);
720 pthread_mutex_unlock(&dso
->lock
);
723 static struct dso_cache
*dso_cache__find(struct dso
*dso
, u64 offset
)
725 const struct rb_root
*root
= &dso
->data
.cache
;
726 struct rb_node
* const *p
= &root
->rb_node
;
727 const struct rb_node
*parent
= NULL
;
728 struct dso_cache
*cache
;
734 cache
= rb_entry(parent
, struct dso_cache
, rb_node
);
735 end
= cache
->offset
+ DSO__DATA_CACHE_SIZE
;
737 if (offset
< cache
->offset
)
739 else if (offset
>= end
)
748 static struct dso_cache
*
749 dso_cache__insert(struct dso
*dso
, struct dso_cache
*new)
751 struct rb_root
*root
= &dso
->data
.cache
;
752 struct rb_node
**p
= &root
->rb_node
;
753 struct rb_node
*parent
= NULL
;
754 struct dso_cache
*cache
;
755 u64 offset
= new->offset
;
757 pthread_mutex_lock(&dso
->lock
);
762 cache
= rb_entry(parent
, struct dso_cache
, rb_node
);
763 end
= cache
->offset
+ DSO__DATA_CACHE_SIZE
;
765 if (offset
< cache
->offset
)
767 else if (offset
>= end
)
773 rb_link_node(&new->rb_node
, parent
, p
);
774 rb_insert_color(&new->rb_node
, root
);
778 pthread_mutex_unlock(&dso
->lock
);
783 dso_cache__memcpy(struct dso_cache
*cache
, u64 offset
,
786 u64 cache_offset
= offset
- cache
->offset
;
787 u64 cache_size
= min(cache
->size
- cache_offset
, size
);
789 memcpy(data
, cache
->data
+ cache_offset
, cache_size
);
794 dso_cache__read(struct dso
*dso
, struct machine
*machine
,
795 u64 offset
, u8
*data
, ssize_t size
)
797 struct dso_cache
*cache
;
798 struct dso_cache
*old
;
804 cache
= zalloc(sizeof(*cache
) + DSO__DATA_CACHE_SIZE
);
808 pthread_mutex_lock(&dso__data_open_lock
);
811 * dso->data.fd might be closed if other thread opened another
812 * file (dso) due to open file limit (RLIMIT_NOFILE).
814 try_to_open_dso(dso
, machine
);
816 if (dso
->data
.fd
< 0) {
818 dso
->data
.status
= DSO_DATA_STATUS_ERROR
;
822 cache_offset
= offset
& DSO__DATA_CACHE_MASK
;
824 ret
= pread(dso
->data
.fd
, cache
->data
, DSO__DATA_CACHE_SIZE
, cache_offset
);
828 cache
->offset
= cache_offset
;
832 pthread_mutex_unlock(&dso__data_open_lock
);
835 old
= dso_cache__insert(dso
, cache
);
837 /* we lose the race */
842 ret
= dso_cache__memcpy(cache
, offset
, data
, size
);
851 static ssize_t
dso_cache_read(struct dso
*dso
, struct machine
*machine
,
852 u64 offset
, u8
*data
, ssize_t size
)
854 struct dso_cache
*cache
;
856 cache
= dso_cache__find(dso
, offset
);
858 return dso_cache__memcpy(cache
, offset
, data
, size
);
860 return dso_cache__read(dso
, machine
, offset
, data
, size
);
864 * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
865 * in the rb_tree. Any read to already cached data is served
868 static ssize_t
cached_read(struct dso
*dso
, struct machine
*machine
,
869 u64 offset
, u8
*data
, ssize_t size
)
877 ret
= dso_cache_read(dso
, machine
, offset
, p
, size
);
881 /* Reached EOF, return what we have. */
897 static int data_file_size(struct dso
*dso
, struct machine
*machine
)
901 char sbuf
[STRERR_BUFSIZE
];
903 if (dso
->data
.file_size
)
906 if (dso
->data
.status
== DSO_DATA_STATUS_ERROR
)
909 pthread_mutex_lock(&dso__data_open_lock
);
912 * dso->data.fd might be closed if other thread opened another
913 * file (dso) due to open file limit (RLIMIT_NOFILE).
915 try_to_open_dso(dso
, machine
);
917 if (dso
->data
.fd
< 0) {
919 dso
->data
.status
= DSO_DATA_STATUS_ERROR
;
923 if (fstat(dso
->data
.fd
, &st
) < 0) {
925 pr_err("dso cache fstat failed: %s\n",
926 str_error_r(errno
, sbuf
, sizeof(sbuf
)));
927 dso
->data
.status
= DSO_DATA_STATUS_ERROR
;
930 dso
->data
.file_size
= st
.st_size
;
933 pthread_mutex_unlock(&dso__data_open_lock
);
938 * dso__data_size - Return dso data size
940 * @machine: machine object
942 * Return: dso data size
944 off_t
dso__data_size(struct dso
*dso
, struct machine
*machine
)
946 if (data_file_size(dso
, machine
))
949 /* For now just estimate dso data size is close to file size */
950 return dso
->data
.file_size
;
953 static ssize_t
data_read_offset(struct dso
*dso
, struct machine
*machine
,
954 u64 offset
, u8
*data
, ssize_t size
)
956 if (data_file_size(dso
, machine
))
959 /* Check the offset sanity. */
960 if (offset
> dso
->data
.file_size
)
963 if (offset
+ size
< offset
)
966 return cached_read(dso
, machine
, offset
, data
, size
);
970 * dso__data_read_offset - Read data from dso file offset
972 * @machine: machine object
973 * @offset: file offset
974 * @data: buffer to store data
975 * @size: size of the @data buffer
977 * External interface to read data from dso file offset. Open
978 * dso data file and use cached_read to get the data.
980 ssize_t
dso__data_read_offset(struct dso
*dso
, struct machine
*machine
,
981 u64 offset
, u8
*data
, ssize_t size
)
983 if (dso
->data
.status
== DSO_DATA_STATUS_ERROR
)
986 return data_read_offset(dso
, machine
, offset
, data
, size
);
990 * dso__data_read_addr - Read data from dso address
992 * @machine: machine object
993 * @add: virtual memory address
994 * @data: buffer to store data
995 * @size: size of the @data buffer
997 * External interface to read data from dso address.
999 ssize_t
dso__data_read_addr(struct dso
*dso
, struct map
*map
,
1000 struct machine
*machine
, u64 addr
,
1001 u8
*data
, ssize_t size
)
1003 u64 offset
= map
->map_ip(map
, addr
);
1004 return dso__data_read_offset(dso
, machine
, offset
, data
, size
);
1007 struct map
*dso__new_map(const char *name
)
1009 struct map
*map
= NULL
;
1010 struct dso
*dso
= dso__new(name
);
1013 map
= map__new2(0, dso
);
1018 struct dso
*machine__findnew_kernel(struct machine
*machine
, const char *name
,
1019 const char *short_name
, int dso_type
)
1022 * The kernel dso could be created by build_id processing.
1024 struct dso
*dso
= machine__findnew_dso(machine
, name
);
1027 * We need to run this in all cases, since during the build_id
1028 * processing we had no idea this was the kernel dso.
1031 dso__set_short_name(dso
, short_name
, false);
1032 dso
->kernel
= dso_type
;
1039 * Find a matching entry and/or link current entry to RB tree.
1040 * Either one of the dso or name parameter must be non-NULL or the
1041 * function will not work.
1043 static struct dso
*__dso__findlink_by_longname(struct rb_root
*root
,
1044 struct dso
*dso
, const char *name
)
1046 struct rb_node
**p
= &root
->rb_node
;
1047 struct rb_node
*parent
= NULL
;
1050 name
= dso
->long_name
;
1052 * Find node with the matching name
1055 struct dso
*this = rb_entry(*p
, struct dso
, rb_node
);
1056 int rc
= strcmp(name
, this->long_name
);
1061 * In case the new DSO is a duplicate of an existing
1062 * one, print a one-time warning & put the new entry
1063 * at the end of the list of duplicates.
1065 if (!dso
|| (dso
== this))
1066 return this; /* Find matching dso */
1068 * The core kernel DSOs may have duplicated long name.
1069 * In this case, the short name should be different.
1070 * Comparing the short names to differentiate the DSOs.
1072 rc
= strcmp(dso
->short_name
, this->short_name
);
1074 pr_err("Duplicated dso name: %s\n", name
);
1079 p
= &parent
->rb_left
;
1081 p
= &parent
->rb_right
;
1084 /* Add new node and rebalance tree */
1085 rb_link_node(&dso
->rb_node
, parent
, p
);
1086 rb_insert_color(&dso
->rb_node
, root
);
1092 static inline struct dso
*__dso__find_by_longname(struct rb_root
*root
,
1095 return __dso__findlink_by_longname(root
, NULL
, name
);
1098 void dso__set_long_name(struct dso
*dso
, const char *name
, bool name_allocated
)
1100 struct rb_root
*root
= dso
->root
;
1105 if (dso
->long_name_allocated
)
1106 free((char *)dso
->long_name
);
1109 rb_erase(&dso
->rb_node
, root
);
1111 * __dso__findlink_by_longname() isn't guaranteed to add it
1112 * back, so a clean removal is required here.
1114 RB_CLEAR_NODE(&dso
->rb_node
);
1118 dso
->long_name
= name
;
1119 dso
->long_name_len
= strlen(name
);
1120 dso
->long_name_allocated
= name_allocated
;
1123 __dso__findlink_by_longname(root
, dso
, NULL
);
1126 void dso__set_short_name(struct dso
*dso
, const char *name
, bool name_allocated
)
1131 if (dso
->short_name_allocated
)
1132 free((char *)dso
->short_name
);
1134 dso
->short_name
= name
;
1135 dso
->short_name_len
= strlen(name
);
1136 dso
->short_name_allocated
= name_allocated
;
1139 static void dso__set_basename(struct dso
*dso
)
1142 * basename() may modify path buffer, so we must pass
1145 char *base
, *lname
= strdup(dso
->long_name
);
1151 * basename() may return a pointer to internal
1152 * storage which is reused in subsequent calls
1153 * so copy the result.
1155 base
= strdup(basename(lname
));
1162 dso__set_short_name(dso
, base
, true);
1165 int dso__name_len(const struct dso
*dso
)
1168 return strlen("[unknown]");
1170 return dso
->long_name_len
;
1172 return dso
->short_name_len
;
1175 bool dso__loaded(const struct dso
*dso
)
1180 bool dso__sorted_by_name(const struct dso
*dso
)
1182 return dso
->sorted_by_name
;
1185 void dso__set_sorted_by_name(struct dso
*dso
)
1187 dso
->sorted_by_name
= true;
1190 struct dso
*dso__new(const char *name
)
1192 struct dso
*dso
= calloc(1, sizeof(*dso
) + strlen(name
) + 1);
1195 strcpy(dso
->name
, name
);
1196 dso__set_long_name(dso
, dso
->name
, false);
1197 dso__set_short_name(dso
, dso
->name
, false);
1198 dso
->symbols
= dso
->symbol_names
= RB_ROOT
;
1199 dso
->data
.cache
= RB_ROOT
;
1200 dso
->inlined_nodes
= RB_ROOT
;
1201 dso
->srclines
= RB_ROOT
;
1203 dso
->data
.status
= DSO_DATA_STATUS_UNKNOWN
;
1204 dso
->symtab_type
= DSO_BINARY_TYPE__NOT_FOUND
;
1205 dso
->binary_type
= DSO_BINARY_TYPE__NOT_FOUND
;
1206 dso
->is_64_bit
= (sizeof(void *) == 8);
1209 dso
->sorted_by_name
= 0;
1210 dso
->has_build_id
= 0;
1211 dso
->has_srcline
= 1;
1213 dso
->kernel
= DSO_TYPE_USER
;
1214 dso
->needs_swap
= DSO_SWAP__UNSET
;
1215 dso
->comp
= COMP_ID__NONE
;
1216 RB_CLEAR_NODE(&dso
->rb_node
);
1218 INIT_LIST_HEAD(&dso
->node
);
1219 INIT_LIST_HEAD(&dso
->data
.open_entry
);
1220 pthread_mutex_init(&dso
->lock
, NULL
);
1221 refcount_set(&dso
->refcnt
, 1);
1227 void dso__delete(struct dso
*dso
)
1229 if (!RB_EMPTY_NODE(&dso
->rb_node
))
1230 pr_err("DSO %s is still in rbtree when being deleted!\n",
1233 /* free inlines first, as they reference symbols */
1234 inlines__tree_delete(&dso
->inlined_nodes
);
1235 srcline__tree_delete(&dso
->srclines
);
1236 symbols__delete(&dso
->symbols
);
1238 if (dso
->short_name_allocated
) {
1239 zfree((char **)&dso
->short_name
);
1240 dso
->short_name_allocated
= false;
1243 if (dso
->long_name_allocated
) {
1244 zfree((char **)&dso
->long_name
);
1245 dso
->long_name_allocated
= false;
1248 dso__data_close(dso
);
1249 auxtrace_cache__free(dso
->auxtrace_cache
);
1250 dso_cache__free(dso
);
1252 zfree(&dso
->symsrc_filename
);
1253 nsinfo__zput(dso
->nsinfo
);
1254 pthread_mutex_destroy(&dso
->lock
);
1258 struct dso
*dso__get(struct dso
*dso
)
1261 refcount_inc(&dso
->refcnt
);
1265 void dso__put(struct dso
*dso
)
1267 if (dso
&& refcount_dec_and_test(&dso
->refcnt
))
1271 void dso__set_build_id(struct dso
*dso
, void *build_id
)
1273 memcpy(dso
->build_id
, build_id
, sizeof(dso
->build_id
));
1274 dso
->has_build_id
= 1;
1277 bool dso__build_id_equal(const struct dso
*dso
, u8
*build_id
)
1279 return memcmp(dso
->build_id
, build_id
, sizeof(dso
->build_id
)) == 0;
1282 void dso__read_running_kernel_build_id(struct dso
*dso
, struct machine
*machine
)
1284 char path
[PATH_MAX
];
1286 if (machine__is_default_guest(machine
))
1288 sprintf(path
, "%s/sys/kernel/notes", machine
->root_dir
);
1289 if (sysfs__read_build_id(path
, dso
->build_id
,
1290 sizeof(dso
->build_id
)) == 0)
1291 dso
->has_build_id
= true;
1294 int dso__kernel_module_get_build_id(struct dso
*dso
,
1295 const char *root_dir
)
1297 char filename
[PATH_MAX
];
1299 * kernel module short names are of the form "[module]" and
1300 * we need just "module" here.
1302 const char *name
= dso
->short_name
+ 1;
1304 snprintf(filename
, sizeof(filename
),
1305 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1306 root_dir
, (int)strlen(name
) - 1, name
);
1308 if (sysfs__read_build_id(filename
, dso
->build_id
,
1309 sizeof(dso
->build_id
)) == 0)
1310 dso
->has_build_id
= true;
1315 bool __dsos__read_build_ids(struct list_head
*head
, bool with_hits
)
1317 bool have_build_id
= false;
1319 struct nscookie nsc
;
1321 list_for_each_entry(pos
, head
, node
) {
1322 if (with_hits
&& !pos
->hit
&& !dso__is_vdso(pos
))
1324 if (pos
->has_build_id
) {
1325 have_build_id
= true;
1328 nsinfo__mountns_enter(pos
->nsinfo
, &nsc
);
1329 if (filename__read_build_id(pos
->long_name
, pos
->build_id
,
1330 sizeof(pos
->build_id
)) > 0) {
1331 have_build_id
= true;
1332 pos
->has_build_id
= true;
1334 nsinfo__mountns_exit(&nsc
);
1337 return have_build_id
;
1340 void __dsos__add(struct dsos
*dsos
, struct dso
*dso
)
1342 list_add_tail(&dso
->node
, &dsos
->head
);
1343 __dso__findlink_by_longname(&dsos
->root
, dso
, NULL
);
1345 * It is now in the linked list, grab a reference, then garbage collect
1346 * this when needing memory, by looking at LRU dso instances in the
1347 * list with atomic_read(&dso->refcnt) == 1, i.e. no references
1348 * anywhere besides the one for the list, do, under a lock for the
1349 * list: remove it from the list, then a dso__put(), that probably will
1350 * be the last and will then call dso__delete(), end of life.
1352 * That, or at the end of the 'struct machine' lifetime, when all
1353 * 'struct dso' instances will be removed from the list, in
1354 * dsos__exit(), if they have no other reference from some other data
1357 * E.g.: after processing a 'perf.data' file and storing references
1358 * to objects instantiated while processing events, we will have
1359 * references to the 'thread', 'map', 'dso' structs all from 'struct
1360 * hist_entry' instances, but we may not need anything not referenced,
1361 * so we might as well call machines__exit()/machines__delete() and
1362 * garbage collect it.
1367 void dsos__add(struct dsos
*dsos
, struct dso
*dso
)
1369 down_write(&dsos
->lock
);
1370 __dsos__add(dsos
, dso
);
1371 up_write(&dsos
->lock
);
1374 struct dso
*__dsos__find(struct dsos
*dsos
, const char *name
, bool cmp_short
)
1379 list_for_each_entry(pos
, &dsos
->head
, node
)
1380 if (strcmp(pos
->short_name
, name
) == 0)
1384 return __dso__find_by_longname(&dsos
->root
, name
);
1387 struct dso
*dsos__find(struct dsos
*dsos
, const char *name
, bool cmp_short
)
1390 down_read(&dsos
->lock
);
1391 dso
= __dsos__find(dsos
, name
, cmp_short
);
1392 up_read(&dsos
->lock
);
1396 struct dso
*__dsos__addnew(struct dsos
*dsos
, const char *name
)
1398 struct dso
*dso
= dso__new(name
);
1401 __dsos__add(dsos
, dso
);
1402 dso__set_basename(dso
);
1403 /* Put dso here because __dsos_add already got it */
1409 struct dso
*__dsos__findnew(struct dsos
*dsos
, const char *name
)
1411 struct dso
*dso
= __dsos__find(dsos
, name
, false);
1413 return dso
? dso
: __dsos__addnew(dsos
, name
);
1416 struct dso
*dsos__findnew(struct dsos
*dsos
, const char *name
)
1419 down_write(&dsos
->lock
);
1420 dso
= dso__get(__dsos__findnew(dsos
, name
));
1421 up_write(&dsos
->lock
);
1425 size_t __dsos__fprintf_buildid(struct list_head
*head
, FILE *fp
,
1426 bool (skip
)(struct dso
*dso
, int parm
), int parm
)
1431 list_for_each_entry(pos
, head
, node
) {
1432 if (skip
&& skip(pos
, parm
))
1434 ret
+= dso__fprintf_buildid(pos
, fp
);
1435 ret
+= fprintf(fp
, " %s\n", pos
->long_name
);
1440 size_t __dsos__fprintf(struct list_head
*head
, FILE *fp
)
1445 list_for_each_entry(pos
, head
, node
) {
1446 ret
+= dso__fprintf(pos
, fp
);
1452 size_t dso__fprintf_buildid(struct dso
*dso
, FILE *fp
)
1454 char sbuild_id
[SBUILD_ID_SIZE
];
1456 build_id__sprintf(dso
->build_id
, sizeof(dso
->build_id
), sbuild_id
);
1457 return fprintf(fp
, "%s", sbuild_id
);
1460 size_t dso__fprintf(struct dso
*dso
, FILE *fp
)
1463 size_t ret
= fprintf(fp
, "dso: %s (", dso
->short_name
);
1465 if (dso
->short_name
!= dso
->long_name
)
1466 ret
+= fprintf(fp
, "%s, ", dso
->long_name
);
1467 ret
+= fprintf(fp
, "%sloaded, ", dso__loaded(dso
) ? "" : "NOT ");
1468 ret
+= dso__fprintf_buildid(dso
, fp
);
1469 ret
+= fprintf(fp
, ")\n");
1470 for (nd
= rb_first(&dso
->symbols
); nd
; nd
= rb_next(nd
)) {
1471 struct symbol
*pos
= rb_entry(nd
, struct symbol
, rb_node
);
1472 ret
+= symbol__fprintf(pos
, fp
);
1478 enum dso_type
dso__type(struct dso
*dso
, struct machine
*machine
)
1481 enum dso_type type
= DSO__TYPE_UNKNOWN
;
1483 fd
= dso__data_get_fd(dso
, machine
);
1485 type
= dso__type_fd(fd
);
1486 dso__data_put_fd(dso
);
1492 int dso__strerror_load(struct dso
*dso
, char *buf
, size_t buflen
)
1494 int idx
, errnum
= dso
->load_errno
;
1496 * This must have a same ordering as the enum dso_load_errno.
1498 static const char *dso_load__error_str
[] = {
1499 "Internal tools/perf/ library error",
1501 "Can not read build id",
1502 "Mismatching build id",
1503 "Decompression failure",
1506 BUG_ON(buflen
== 0);
1509 const char *err
= str_error_r(errnum
, buf
, buflen
);
1512 scnprintf(buf
, buflen
, "%s", err
);
1517 if (errnum
< __DSO_LOAD_ERRNO__START
|| errnum
>= __DSO_LOAD_ERRNO__END
)
1520 idx
= errnum
- __DSO_LOAD_ERRNO__START
;
1521 scnprintf(buf
, buflen
, "%s", dso_load__error_str
[idx
]);