1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/kernel.h>
4 #include <linux/string.h>
5 #include <linux/zalloc.h>
7 #include <sys/resource.h>
14 #ifdef HAVE_LIBBPF_SUPPORT
15 #include <bpf/libbpf.h>
16 #include "bpf-event.h"
20 #include "namespaces.h"
29 #include "util.h" /* O_CLOEXEC for older systems */
34 static const char * const debuglink_paths
[] = {
41 char dso__symtab_origin(const struct dso
*dso
)
43 static const char origin
[] = {
44 [DSO_BINARY_TYPE__KALLSYMS
] = 'k',
45 [DSO_BINARY_TYPE__VMLINUX
] = 'v',
46 [DSO_BINARY_TYPE__JAVA_JIT
] = 'j',
47 [DSO_BINARY_TYPE__DEBUGLINK
] = 'l',
48 [DSO_BINARY_TYPE__BUILD_ID_CACHE
] = 'B',
49 [DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO
] = 'D',
50 [DSO_BINARY_TYPE__FEDORA_DEBUGINFO
] = 'f',
51 [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO
] = 'u',
52 [DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO
] = 'x',
53 [DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO
] = 'o',
54 [DSO_BINARY_TYPE__BUILDID_DEBUGINFO
] = 'b',
55 [DSO_BINARY_TYPE__SYSTEM_PATH_DSO
] = 'd',
56 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE
] = 'K',
57 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP
] = 'm',
58 [DSO_BINARY_TYPE__GUEST_KALLSYMS
] = 'g',
59 [DSO_BINARY_TYPE__GUEST_KMODULE
] = 'G',
60 [DSO_BINARY_TYPE__GUEST_KMODULE_COMP
] = 'M',
61 [DSO_BINARY_TYPE__GUEST_VMLINUX
] = 'V',
64 if (dso
== NULL
|| dso
->symtab_type
== DSO_BINARY_TYPE__NOT_FOUND
)
66 return origin
[dso
->symtab_type
];
69 int dso__read_binary_type_filename(const struct dso
*dso
,
70 enum dso_binary_type type
,
71 char *root_dir
, char *filename
, size_t size
)
73 char build_id_hex
[SBUILD_ID_SIZE
];
78 case DSO_BINARY_TYPE__DEBUGLINK
:
80 const char *last_slash
;
81 char dso_dir
[PATH_MAX
];
82 char symfile
[PATH_MAX
];
85 len
= __symbol__join_symfs(filename
, size
, dso
->long_name
);
86 last_slash
= filename
+ len
;
87 while (last_slash
!= filename
&& *last_slash
!= '/')
90 strncpy(dso_dir
, filename
, last_slash
- filename
);
91 dso_dir
[last_slash
-filename
] = '\0';
93 if (!is_regular_file(filename
)) {
98 ret
= filename__read_debuglink(filename
, symfile
, PATH_MAX
);
102 /* Check predefined locations where debug file might reside */
104 for (i
= 0; i
< ARRAY_SIZE(debuglink_paths
); i
++) {
105 snprintf(filename
, size
,
106 debuglink_paths
[i
], dso_dir
, symfile
);
107 if (is_regular_file(filename
)) {
115 case DSO_BINARY_TYPE__BUILD_ID_CACHE
:
116 if (dso__build_id_filename(dso
, filename
, size
, false) == NULL
)
120 case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO
:
121 if (dso__build_id_filename(dso
, filename
, size
, true) == NULL
)
125 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO
:
126 len
= __symbol__join_symfs(filename
, size
, "/usr/lib/debug");
127 snprintf(filename
+ len
, size
- len
, "%s.debug", dso
->long_name
);
130 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO
:
131 len
= __symbol__join_symfs(filename
, size
, "/usr/lib/debug");
132 snprintf(filename
+ len
, size
- len
, "%s", dso
->long_name
);
135 case DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO
:
137 * Ubuntu can mixup /usr/lib with /lib, putting debuginfo in
138 * /usr/lib/debug/lib when it is expected to be in
139 * /usr/lib/debug/usr/lib
141 if (strlen(dso
->long_name
) < 9 ||
142 strncmp(dso
->long_name
, "/usr/lib/", 9)) {
146 len
= __symbol__join_symfs(filename
, size
, "/usr/lib/debug");
147 snprintf(filename
+ len
, size
- len
, "%s", dso
->long_name
+ 4);
150 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO
:
152 const char *last_slash
;
155 last_slash
= dso
->long_name
+ dso
->long_name_len
;
156 while (last_slash
!= dso
->long_name
&& *last_slash
!= '/')
159 len
= __symbol__join_symfs(filename
, size
, "");
160 dir_size
= last_slash
- dso
->long_name
+ 2;
161 if (dir_size
> (size
- len
)) {
165 len
+= scnprintf(filename
+ len
, dir_size
, "%s", dso
->long_name
);
166 len
+= scnprintf(filename
+ len
, size
- len
, ".debug%s",
171 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO
:
172 if (!dso
->has_build_id
) {
177 build_id__sprintf(&dso
->bid
, build_id_hex
);
178 len
= __symbol__join_symfs(filename
, size
, "/usr/lib/debug/.build-id/");
179 snprintf(filename
+ len
, size
- len
, "%.2s/%s.debug",
180 build_id_hex
, build_id_hex
+ 2);
183 case DSO_BINARY_TYPE__VMLINUX
:
184 case DSO_BINARY_TYPE__GUEST_VMLINUX
:
185 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO
:
186 __symbol__join_symfs(filename
, size
, dso
->long_name
);
189 case DSO_BINARY_TYPE__GUEST_KMODULE
:
190 case DSO_BINARY_TYPE__GUEST_KMODULE_COMP
:
191 path__join3(filename
, size
, symbol_conf
.symfs
,
192 root_dir
, dso
->long_name
);
195 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE
:
196 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP
:
197 __symbol__join_symfs(filename
, size
, dso
->long_name
);
200 case DSO_BINARY_TYPE__KCORE
:
201 case DSO_BINARY_TYPE__GUEST_KCORE
:
202 snprintf(filename
, size
, "%s", dso
->long_name
);
206 case DSO_BINARY_TYPE__KALLSYMS
:
207 case DSO_BINARY_TYPE__GUEST_KALLSYMS
:
208 case DSO_BINARY_TYPE__JAVA_JIT
:
209 case DSO_BINARY_TYPE__BPF_PROG_INFO
:
210 case DSO_BINARY_TYPE__BPF_IMAGE
:
211 case DSO_BINARY_TYPE__OOL
:
212 case DSO_BINARY_TYPE__NOT_FOUND
:
224 static const struct {
226 int (*decompress
)(const char *input
, int output
);
227 bool (*is_compressed
)(const char *input
);
229 [COMP_ID__NONE
] = { .fmt
= NULL
, },
230 #ifdef HAVE_ZLIB_SUPPORT
231 { "gz", gzip_decompress_to_file
, gzip_is_compressed
},
233 #ifdef HAVE_LZMA_SUPPORT
234 { "xz", lzma_decompress_to_file
, lzma_is_compressed
},
236 { NULL
, NULL
, NULL
},
239 static int is_supported_compression(const char *ext
)
243 for (i
= 1; compressions
[i
].fmt
; i
++) {
244 if (!strcmp(ext
, compressions
[i
].fmt
))
247 return COMP_ID__NONE
;
250 bool is_kernel_module(const char *pathname
, int cpumode
)
253 int mode
= cpumode
& PERF_RECORD_MISC_CPUMODE_MASK
;
255 WARN_ONCE(mode
!= cpumode
,
256 "Internal error: passing unmasked cpumode (%x) to is_kernel_module",
260 case PERF_RECORD_MISC_USER
:
261 case PERF_RECORD_MISC_HYPERVISOR
:
262 case PERF_RECORD_MISC_GUEST_USER
:
264 /* Treat PERF_RECORD_MISC_CPUMODE_UNKNOWN as kernel */
266 if (kmod_path__parse(&m
, pathname
)) {
267 pr_err("Failed to check whether %s is a kernel module or not. Assume it is.",
276 bool dso__needs_decompress(struct dso
*dso
)
278 return dso
->symtab_type
== DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP
||
279 dso
->symtab_type
== DSO_BINARY_TYPE__GUEST_KMODULE_COMP
;
282 int filename__decompress(const char *name
, char *pathname
,
283 size_t len
, int comp
, int *err
)
285 char tmpbuf
[] = KMOD_DECOMP_NAME
;
289 * We have proper compression id for DSO and yet the file
290 * behind the 'name' can still be plain uncompressed object.
292 * The reason is behind the logic we open the DSO object files,
293 * when we try all possible 'debug' objects until we find the
294 * data. So even if the DSO is represented by 'krava.xz' module,
295 * we can end up here opening ~/.debug/....23432432/debug' file
296 * which is not compressed.
298 * To keep this transparent, we detect this and return the file
299 * descriptor to the uncompressed file.
301 if (!compressions
[comp
].is_compressed(name
))
302 return open(name
, O_RDONLY
);
304 fd
= mkstemp(tmpbuf
);
310 if (compressions
[comp
].decompress(name
, fd
)) {
311 *err
= DSO_LOAD_ERRNO__DECOMPRESSION_FAILURE
;
316 if (!pathname
|| (fd
< 0))
319 if (pathname
&& (fd
>= 0))
320 strlcpy(pathname
, tmpbuf
, len
);
325 static int decompress_kmodule(struct dso
*dso
, const char *name
,
326 char *pathname
, size_t len
)
328 if (!dso__needs_decompress(dso
))
331 if (dso
->comp
== COMP_ID__NONE
)
334 return filename__decompress(name
, pathname
, len
, dso
->comp
,
338 int dso__decompress_kmodule_fd(struct dso
*dso
, const char *name
)
340 return decompress_kmodule(dso
, name
, NULL
, 0);
343 int dso__decompress_kmodule_path(struct dso
*dso
, const char *name
,
344 char *pathname
, size_t len
)
346 int fd
= decompress_kmodule(dso
, name
, pathname
, len
);
349 return fd
>= 0 ? 0 : -1;
353 * Parses kernel module specified in @path and updates
356 * @comp - true if @path contains supported compression suffix,
358 * @kmod - true if @path contains '.ko' suffix in right position,
360 * @name - if (@alloc_name && @kmod) is true, it contains strdup-ed base name
361 * of the kernel module without suffixes, otherwise strudup-ed
363 * @ext - if (@alloc_ext && @comp) is true, it contains strdup-ed string
364 * the compression suffix
366 * Returns 0 if there's no strdup error, -ENOMEM otherwise.
368 int __kmod_path__parse(struct kmod_path
*m
, const char *path
,
371 const char *name
= strrchr(path
, '/');
372 const char *ext
= strrchr(path
, '.');
373 bool is_simple_name
= false;
375 memset(m
, 0x0, sizeof(*m
));
376 name
= name
? name
+ 1 : path
;
379 * '.' is also a valid character for module name. For example:
380 * [aaa.bbb] is a valid module name. '[' should have higher
381 * priority than '.ko' suffix.
383 * The kernel names are from machine__mmap_name. Such
384 * name should belong to kernel itself, not kernel module.
386 if (name
[0] == '[') {
387 is_simple_name
= true;
388 if ((strncmp(name
, "[kernel.kallsyms]", 17) == 0) ||
389 (strncmp(name
, "[guest.kernel.kallsyms", 22) == 0) ||
390 (strncmp(name
, "[vdso]", 6) == 0) ||
391 (strncmp(name
, "[vdso32]", 8) == 0) ||
392 (strncmp(name
, "[vdsox32]", 9) == 0) ||
393 (strncmp(name
, "[vsyscall]", 10) == 0)) {
400 /* No extension, just return name. */
401 if ((ext
== NULL
) || is_simple_name
) {
403 m
->name
= strdup(name
);
404 return m
->name
? 0 : -ENOMEM
;
409 m
->comp
= is_supported_compression(ext
+ 1);
410 if (m
->comp
> COMP_ID__NONE
)
413 /* Check .ko extension only if there's enough name left. */
415 m
->kmod
= !strncmp(ext
, ".ko", 3);
419 if (asprintf(&m
->name
, "[%.*s]", (int) (ext
- name
), name
) == -1)
422 if (asprintf(&m
->name
, "%s", name
) == -1)
426 strreplace(m
->name
, '-', '_');
432 void dso__set_module_info(struct dso
*dso
, struct kmod_path
*m
,
433 struct machine
*machine
)
435 if (machine__is_host(machine
))
436 dso
->symtab_type
= DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE
;
438 dso
->symtab_type
= DSO_BINARY_TYPE__GUEST_KMODULE
;
440 /* _KMODULE_COMP should be next to _KMODULE */
441 if (m
->kmod
&& m
->comp
) {
446 dso__set_short_name(dso
, strdup(m
->name
), true);
450 * Global list of open DSOs and the counter.
452 static LIST_HEAD(dso__data_open
);
453 static long dso__data_open_cnt
;
454 static pthread_mutex_t dso__data_open_lock
= PTHREAD_MUTEX_INITIALIZER
;
456 static void dso__list_add(struct dso
*dso
)
458 list_add_tail(&dso
->data
.open_entry
, &dso__data_open
);
459 dso__data_open_cnt
++;
462 static void dso__list_del(struct dso
*dso
)
464 list_del_init(&dso
->data
.open_entry
);
465 WARN_ONCE(dso__data_open_cnt
<= 0,
466 "DSO data fd counter out of bounds.");
467 dso__data_open_cnt
--;
470 static void close_first_dso(void);
472 static int do_open(char *name
)
475 char sbuf
[STRERR_BUFSIZE
];
478 fd
= open(name
, O_RDONLY
|O_CLOEXEC
);
482 pr_debug("dso open failed: %s\n",
483 str_error_r(errno
, sbuf
, sizeof(sbuf
)));
484 if (!dso__data_open_cnt
|| errno
!= EMFILE
)
493 static int __open_dso(struct dso
*dso
, struct machine
*machine
)
496 char *root_dir
= (char *)"";
497 char *name
= malloc(PATH_MAX
);
504 root_dir
= machine
->root_dir
;
506 if (dso__read_binary_type_filename(dso
, dso
->binary_type
,
507 root_dir
, name
, PATH_MAX
))
510 if (!is_regular_file(name
))
513 if (dso__needs_decompress(dso
)) {
514 char newpath
[KMOD_DECOMP_LEN
];
515 size_t len
= sizeof(newpath
);
517 if (dso__decompress_kmodule_path(dso
, name
, newpath
, len
) < 0) {
518 fd
= -dso
->load_errno
;
523 strcpy(name
, newpath
);
536 static void check_data_close(void);
539 * dso_close - Open DSO data file
542 * Open @dso's data file descriptor and updates
543 * list/count of open DSO objects.
545 static int open_dso(struct dso
*dso
, struct machine
*machine
)
550 if (dso
->binary_type
!= DSO_BINARY_TYPE__BUILD_ID_CACHE
)
551 nsinfo__mountns_enter(dso
->nsinfo
, &nsc
);
552 fd
= __open_dso(dso
, machine
);
553 if (dso
->binary_type
!= DSO_BINARY_TYPE__BUILD_ID_CACHE
)
554 nsinfo__mountns_exit(&nsc
);
559 * Check if we crossed the allowed number
560 * of opened DSOs and close one if needed.
568 static void close_data_fd(struct dso
*dso
)
570 if (dso
->data
.fd
>= 0) {
573 dso
->data
.file_size
= 0;
579 * dso_close - Close DSO data file
582 * Close @dso's data file descriptor and updates
583 * list/count of open DSO objects.
585 static void close_dso(struct dso
*dso
)
590 static void close_first_dso(void)
594 dso
= list_first_entry(&dso__data_open
, struct dso
, data
.open_entry
);
598 static rlim_t
get_fd_limit(void)
603 /* Allow half of the current open fd limit. */
604 if (getrlimit(RLIMIT_NOFILE
, &l
) == 0) {
605 if (l
.rlim_cur
== RLIM_INFINITY
)
608 limit
= l
.rlim_cur
/ 2;
610 pr_err("failed to get fd limit\n");
617 static rlim_t fd_limit
;
620 * Used only by tests/dso-data.c to reset the environment
621 * for tests. I dont expect we should change this during
624 void reset_fd_limit(void)
629 static bool may_cache_fd(void)
632 fd_limit
= get_fd_limit();
634 if (fd_limit
== RLIM_INFINITY
)
637 return fd_limit
> (rlim_t
) dso__data_open_cnt
;
641 * Check and close LRU dso if we crossed allowed limit
642 * for opened dso file descriptors. The limit is half
643 * of the RLIMIT_NOFILE files opened.
645 static void check_data_close(void)
647 bool cache_fd
= may_cache_fd();
654 * dso__data_close - Close DSO data file
657 * External interface to close @dso's data file descriptor.
659 void dso__data_close(struct dso
*dso
)
661 pthread_mutex_lock(&dso__data_open_lock
);
663 pthread_mutex_unlock(&dso__data_open_lock
);
666 static void try_to_open_dso(struct dso
*dso
, struct machine
*machine
)
668 enum dso_binary_type binary_type_data
[] = {
669 DSO_BINARY_TYPE__BUILD_ID_CACHE
,
670 DSO_BINARY_TYPE__SYSTEM_PATH_DSO
,
671 DSO_BINARY_TYPE__NOT_FOUND
,
675 if (dso
->data
.fd
>= 0)
678 if (dso
->binary_type
!= DSO_BINARY_TYPE__NOT_FOUND
) {
679 dso
->data
.fd
= open_dso(dso
, machine
);
684 dso
->binary_type
= binary_type_data
[i
++];
686 dso
->data
.fd
= open_dso(dso
, machine
);
687 if (dso
->data
.fd
>= 0)
690 } while (dso
->binary_type
!= DSO_BINARY_TYPE__NOT_FOUND
);
692 if (dso
->data
.fd
>= 0)
693 dso
->data
.status
= DSO_DATA_STATUS_OK
;
695 dso
->data
.status
= DSO_DATA_STATUS_ERROR
;
699 * dso__data_get_fd - Get dso's data file descriptor
701 * @machine: machine object
703 * External interface to find dso's file, open it and
704 * returns file descriptor. It should be paired with
705 * dso__data_put_fd() if it returns non-negative value.
707 int dso__data_get_fd(struct dso
*dso
, struct machine
*machine
)
709 if (dso
->data
.status
== DSO_DATA_STATUS_ERROR
)
712 if (pthread_mutex_lock(&dso__data_open_lock
) < 0)
715 try_to_open_dso(dso
, machine
);
717 if (dso
->data
.fd
< 0)
718 pthread_mutex_unlock(&dso__data_open_lock
);
723 void dso__data_put_fd(struct dso
*dso __maybe_unused
)
725 pthread_mutex_unlock(&dso__data_open_lock
);
728 bool dso__data_status_seen(struct dso
*dso
, enum dso_data_status_seen by
)
732 if (dso
->data
.status_seen
& flag
)
735 dso
->data
.status_seen
|= flag
;
740 #ifdef HAVE_LIBBPF_SUPPORT
741 static ssize_t
bpf_read(struct dso
*dso
, u64 offset
, char *data
)
743 struct bpf_prog_info_node
*node
;
744 ssize_t size
= DSO__DATA_CACHE_SIZE
;
748 node
= perf_env__find_bpf_prog_info(dso
->bpf_prog
.env
, dso
->bpf_prog
.id
);
749 if (!node
|| !node
->info_linear
) {
750 dso
->data
.status
= DSO_DATA_STATUS_ERROR
;
754 len
= node
->info_linear
->info
.jited_prog_len
;
755 buf
= (u8
*)(uintptr_t)node
->info_linear
->info
.jited_prog_insns
;
760 size
= (ssize_t
)min(len
- offset
, (u64
)size
);
761 memcpy(data
, buf
+ offset
, size
);
765 static int bpf_size(struct dso
*dso
)
767 struct bpf_prog_info_node
*node
;
769 node
= perf_env__find_bpf_prog_info(dso
->bpf_prog
.env
, dso
->bpf_prog
.id
);
770 if (!node
|| !node
->info_linear
) {
771 dso
->data
.status
= DSO_DATA_STATUS_ERROR
;
775 dso
->data
.file_size
= node
->info_linear
->info
.jited_prog_len
;
778 #endif // HAVE_LIBBPF_SUPPORT
781 dso_cache__free(struct dso
*dso
)
783 struct rb_root
*root
= &dso
->data
.cache
;
784 struct rb_node
*next
= rb_first(root
);
786 pthread_mutex_lock(&dso
->lock
);
788 struct dso_cache
*cache
;
790 cache
= rb_entry(next
, struct dso_cache
, rb_node
);
791 next
= rb_next(&cache
->rb_node
);
792 rb_erase(&cache
->rb_node
, root
);
795 pthread_mutex_unlock(&dso
->lock
);
798 static struct dso_cache
*__dso_cache__find(struct dso
*dso
, u64 offset
)
800 const struct rb_root
*root
= &dso
->data
.cache
;
801 struct rb_node
* const *p
= &root
->rb_node
;
802 const struct rb_node
*parent
= NULL
;
803 struct dso_cache
*cache
;
809 cache
= rb_entry(parent
, struct dso_cache
, rb_node
);
810 end
= cache
->offset
+ DSO__DATA_CACHE_SIZE
;
812 if (offset
< cache
->offset
)
814 else if (offset
>= end
)
823 static struct dso_cache
*
824 dso_cache__insert(struct dso
*dso
, struct dso_cache
*new)
826 struct rb_root
*root
= &dso
->data
.cache
;
827 struct rb_node
**p
= &root
->rb_node
;
828 struct rb_node
*parent
= NULL
;
829 struct dso_cache
*cache
;
830 u64 offset
= new->offset
;
832 pthread_mutex_lock(&dso
->lock
);
837 cache
= rb_entry(parent
, struct dso_cache
, rb_node
);
838 end
= cache
->offset
+ DSO__DATA_CACHE_SIZE
;
840 if (offset
< cache
->offset
)
842 else if (offset
>= end
)
848 rb_link_node(&new->rb_node
, parent
, p
);
849 rb_insert_color(&new->rb_node
, root
);
853 pthread_mutex_unlock(&dso
->lock
);
857 static ssize_t
dso_cache__memcpy(struct dso_cache
*cache
, u64 offset
, u8
*data
,
860 u64 cache_offset
= offset
- cache
->offset
;
861 u64 cache_size
= min(cache
->size
- cache_offset
, size
);
864 memcpy(data
, cache
->data
+ cache_offset
, cache_size
);
866 memcpy(cache
->data
+ cache_offset
, data
, cache_size
);
870 static ssize_t
file_read(struct dso
*dso
, struct machine
*machine
,
871 u64 offset
, char *data
)
875 pthread_mutex_lock(&dso__data_open_lock
);
878 * dso->data.fd might be closed if other thread opened another
879 * file (dso) due to open file limit (RLIMIT_NOFILE).
881 try_to_open_dso(dso
, machine
);
883 if (dso
->data
.fd
< 0) {
884 dso
->data
.status
= DSO_DATA_STATUS_ERROR
;
889 ret
= pread(dso
->data
.fd
, data
, DSO__DATA_CACHE_SIZE
, offset
);
891 pthread_mutex_unlock(&dso__data_open_lock
);
895 static struct dso_cache
*dso_cache__populate(struct dso
*dso
,
896 struct machine
*machine
,
897 u64 offset
, ssize_t
*ret
)
899 u64 cache_offset
= offset
& DSO__DATA_CACHE_MASK
;
900 struct dso_cache
*cache
;
901 struct dso_cache
*old
;
903 cache
= zalloc(sizeof(*cache
) + DSO__DATA_CACHE_SIZE
);
908 #ifdef HAVE_LIBBPF_SUPPORT
909 if (dso
->binary_type
== DSO_BINARY_TYPE__BPF_PROG_INFO
)
910 *ret
= bpf_read(dso
, cache_offset
, cache
->data
);
913 if (dso
->binary_type
== DSO_BINARY_TYPE__OOL
)
914 *ret
= DSO__DATA_CACHE_SIZE
;
916 *ret
= file_read(dso
, machine
, cache_offset
, cache
->data
);
923 cache
->offset
= cache_offset
;
926 old
= dso_cache__insert(dso
, cache
);
928 /* we lose the race */
936 static struct dso_cache
*dso_cache__find(struct dso
*dso
,
937 struct machine
*machine
,
941 struct dso_cache
*cache
= __dso_cache__find(dso
, offset
);
943 return cache
? cache
: dso_cache__populate(dso
, machine
, offset
, ret
);
946 static ssize_t
dso_cache_io(struct dso
*dso
, struct machine
*machine
,
947 u64 offset
, u8
*data
, ssize_t size
, bool out
)
949 struct dso_cache
*cache
;
952 cache
= dso_cache__find(dso
, machine
, offset
, &ret
);
956 return dso_cache__memcpy(cache
, offset
, data
, size
, out
);
960 * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
961 * in the rb_tree. Any read to already cached data is served
962 * by cached data. Writes update the cache only, not the backing file.
964 static ssize_t
cached_io(struct dso
*dso
, struct machine
*machine
,
965 u64 offset
, u8
*data
, ssize_t size
, bool out
)
973 ret
= dso_cache_io(dso
, machine
, offset
, p
, size
, out
);
977 /* Reached EOF, return what we have. */
993 static int file_size(struct dso
*dso
, struct machine
*machine
)
997 char sbuf
[STRERR_BUFSIZE
];
999 pthread_mutex_lock(&dso__data_open_lock
);
1002 * dso->data.fd might be closed if other thread opened another
1003 * file (dso) due to open file limit (RLIMIT_NOFILE).
1005 try_to_open_dso(dso
, machine
);
1007 if (dso
->data
.fd
< 0) {
1009 dso
->data
.status
= DSO_DATA_STATUS_ERROR
;
1013 if (fstat(dso
->data
.fd
, &st
) < 0) {
1015 pr_err("dso cache fstat failed: %s\n",
1016 str_error_r(errno
, sbuf
, sizeof(sbuf
)));
1017 dso
->data
.status
= DSO_DATA_STATUS_ERROR
;
1020 dso
->data
.file_size
= st
.st_size
;
1023 pthread_mutex_unlock(&dso__data_open_lock
);
1027 int dso__data_file_size(struct dso
*dso
, struct machine
*machine
)
1029 if (dso
->data
.file_size
)
1032 if (dso
->data
.status
== DSO_DATA_STATUS_ERROR
)
1034 #ifdef HAVE_LIBBPF_SUPPORT
1035 if (dso
->binary_type
== DSO_BINARY_TYPE__BPF_PROG_INFO
)
1036 return bpf_size(dso
);
1038 return file_size(dso
, machine
);
1042 * dso__data_size - Return dso data size
1044 * @machine: machine object
1046 * Return: dso data size
1048 off_t
dso__data_size(struct dso
*dso
, struct machine
*machine
)
1050 if (dso__data_file_size(dso
, machine
))
1053 /* For now just estimate dso data size is close to file size */
1054 return dso
->data
.file_size
;
1057 static ssize_t
data_read_write_offset(struct dso
*dso
, struct machine
*machine
,
1058 u64 offset
, u8
*data
, ssize_t size
,
1061 if (dso__data_file_size(dso
, machine
))
1064 /* Check the offset sanity. */
1065 if (offset
> dso
->data
.file_size
)
1068 if (offset
+ size
< offset
)
1071 return cached_io(dso
, machine
, offset
, data
, size
, out
);
1075 * dso__data_read_offset - Read data from dso file offset
1077 * @machine: machine object
1078 * @offset: file offset
1079 * @data: buffer to store data
1080 * @size: size of the @data buffer
1082 * External interface to read data from dso file offset. Open
1083 * dso data file and use cached_read to get the data.
1085 ssize_t
dso__data_read_offset(struct dso
*dso
, struct machine
*machine
,
1086 u64 offset
, u8
*data
, ssize_t size
)
1088 if (dso
->data
.status
== DSO_DATA_STATUS_ERROR
)
1091 return data_read_write_offset(dso
, machine
, offset
, data
, size
, true);
1095 * dso__data_read_addr - Read data from dso address
1097 * @machine: machine object
1098 * @add: virtual memory address
1099 * @data: buffer to store data
1100 * @size: size of the @data buffer
1102 * External interface to read data from dso address.
1104 ssize_t
dso__data_read_addr(struct dso
*dso
, struct map
*map
,
1105 struct machine
*machine
, u64 addr
,
1106 u8
*data
, ssize_t size
)
1108 u64 offset
= map
->map_ip(map
, addr
);
1109 return dso__data_read_offset(dso
, machine
, offset
, data
, size
);
1113 * dso__data_write_cache_offs - Write data to dso data cache at file offset
1115 * @machine: machine object
1116 * @offset: file offset
1117 * @data: buffer to write
1118 * @size: size of the @data buffer
1120 * Write into the dso file data cache, but do not change the file itself.
1122 ssize_t
dso__data_write_cache_offs(struct dso
*dso
, struct machine
*machine
,
1123 u64 offset
, const u8
*data_in
, ssize_t size
)
1125 u8
*data
= (u8
*)data_in
; /* cast away const to use same fns for r/w */
1127 if (dso
->data
.status
== DSO_DATA_STATUS_ERROR
)
1130 return data_read_write_offset(dso
, machine
, offset
, data
, size
, false);
1134 * dso__data_write_cache_addr - Write data to dso data cache at dso address
1136 * @machine: machine object
1137 * @add: virtual memory address
1138 * @data: buffer to write
1139 * @size: size of the @data buffer
1141 * External interface to write into the dso file data cache, but do not change
1144 ssize_t
dso__data_write_cache_addr(struct dso
*dso
, struct map
*map
,
1145 struct machine
*machine
, u64 addr
,
1146 const u8
*data
, ssize_t size
)
1148 u64 offset
= map
->map_ip(map
, addr
);
1149 return dso__data_write_cache_offs(dso
, machine
, offset
, data
, size
);
1152 struct map
*dso__new_map(const char *name
)
1154 struct map
*map
= NULL
;
1155 struct dso
*dso
= dso__new(name
);
1158 map
= map__new2(0, dso
);
1163 struct dso
*machine__findnew_kernel(struct machine
*machine
, const char *name
,
1164 const char *short_name
, int dso_type
)
1167 * The kernel dso could be created by build_id processing.
1169 struct dso
*dso
= machine__findnew_dso(machine
, name
);
1172 * We need to run this in all cases, since during the build_id
1173 * processing we had no idea this was the kernel dso.
1176 dso__set_short_name(dso
, short_name
, false);
1177 dso
->kernel
= dso_type
;
1183 static void dso__set_long_name_id(struct dso
*dso
, const char *name
, struct dso_id
*id
, bool name_allocated
)
1185 struct rb_root
*root
= dso
->root
;
1190 if (dso
->long_name_allocated
)
1191 free((char *)dso
->long_name
);
1194 rb_erase(&dso
->rb_node
, root
);
1196 * __dsos__findnew_link_by_longname_id() isn't guaranteed to
1197 * add it back, so a clean removal is required here.
1199 RB_CLEAR_NODE(&dso
->rb_node
);
1203 dso
->long_name
= name
;
1204 dso
->long_name_len
= strlen(name
);
1205 dso
->long_name_allocated
= name_allocated
;
1208 __dsos__findnew_link_by_longname_id(root
, dso
, NULL
, id
);
1211 void dso__set_long_name(struct dso
*dso
, const char *name
, bool name_allocated
)
1213 dso__set_long_name_id(dso
, name
, NULL
, name_allocated
);
1216 void dso__set_short_name(struct dso
*dso
, const char *name
, bool name_allocated
)
1221 if (dso
->short_name_allocated
)
1222 free((char *)dso
->short_name
);
1224 dso
->short_name
= name
;
1225 dso
->short_name_len
= strlen(name
);
1226 dso
->short_name_allocated
= name_allocated
;
1229 int dso__name_len(const struct dso
*dso
)
1232 return strlen("[unknown]");
1234 return dso
->long_name_len
;
1236 return dso
->short_name_len
;
1239 bool dso__loaded(const struct dso
*dso
)
1244 bool dso__sorted_by_name(const struct dso
*dso
)
1246 return dso
->sorted_by_name
;
1249 void dso__set_sorted_by_name(struct dso
*dso
)
1251 dso
->sorted_by_name
= true;
1254 struct dso
*dso__new_id(const char *name
, struct dso_id
*id
)
1256 struct dso
*dso
= calloc(1, sizeof(*dso
) + strlen(name
) + 1);
1259 strcpy(dso
->name
, name
);
1262 dso__set_long_name_id(dso
, dso
->name
, id
, false);
1263 dso__set_short_name(dso
, dso
->name
, false);
1264 dso
->symbols
= dso
->symbol_names
= RB_ROOT_CACHED
;
1265 dso
->data
.cache
= RB_ROOT
;
1266 dso
->inlined_nodes
= RB_ROOT_CACHED
;
1267 dso
->srclines
= RB_ROOT_CACHED
;
1269 dso
->data
.status
= DSO_DATA_STATUS_UNKNOWN
;
1270 dso
->symtab_type
= DSO_BINARY_TYPE__NOT_FOUND
;
1271 dso
->binary_type
= DSO_BINARY_TYPE__NOT_FOUND
;
1272 dso
->is_64_bit
= (sizeof(void *) == 8);
1275 dso
->sorted_by_name
= 0;
1276 dso
->has_build_id
= 0;
1277 dso
->has_srcline
= 1;
1279 dso
->kernel
= DSO_SPACE__USER
;
1280 dso
->needs_swap
= DSO_SWAP__UNSET
;
1281 dso
->comp
= COMP_ID__NONE
;
1282 RB_CLEAR_NODE(&dso
->rb_node
);
1284 INIT_LIST_HEAD(&dso
->node
);
1285 INIT_LIST_HEAD(&dso
->data
.open_entry
);
1286 pthread_mutex_init(&dso
->lock
, NULL
);
1287 refcount_set(&dso
->refcnt
, 1);
1293 struct dso
*dso__new(const char *name
)
1295 return dso__new_id(name
, NULL
);
1298 void dso__delete(struct dso
*dso
)
1300 if (!RB_EMPTY_NODE(&dso
->rb_node
))
1301 pr_err("DSO %s is still in rbtree when being deleted!\n",
1304 /* free inlines first, as they reference symbols */
1305 inlines__tree_delete(&dso
->inlined_nodes
);
1306 srcline__tree_delete(&dso
->srclines
);
1307 symbols__delete(&dso
->symbols
);
1309 if (dso
->short_name_allocated
) {
1310 zfree((char **)&dso
->short_name
);
1311 dso
->short_name_allocated
= false;
1314 if (dso
->long_name_allocated
) {
1315 zfree((char **)&dso
->long_name
);
1316 dso
->long_name_allocated
= false;
1319 dso__data_close(dso
);
1320 auxtrace_cache__free(dso
->auxtrace_cache
);
1321 dso_cache__free(dso
);
1323 zfree(&dso
->symsrc_filename
);
1324 nsinfo__zput(dso
->nsinfo
);
1325 pthread_mutex_destroy(&dso
->lock
);
1329 struct dso
*dso__get(struct dso
*dso
)
1332 refcount_inc(&dso
->refcnt
);
1336 void dso__put(struct dso
*dso
)
1338 if (dso
&& refcount_dec_and_test(&dso
->refcnt
))
1342 void dso__set_build_id(struct dso
*dso
, struct build_id
*bid
)
1345 dso
->has_build_id
= 1;
1348 bool dso__build_id_equal(const struct dso
*dso
, struct build_id
*bid
)
1350 return dso
->bid
.size
== bid
->size
&&
1351 memcmp(dso
->bid
.data
, bid
->data
, dso
->bid
.size
) == 0;
1354 void dso__read_running_kernel_build_id(struct dso
*dso
, struct machine
*machine
)
1356 char path
[PATH_MAX
];
1358 if (machine__is_default_guest(machine
))
1360 sprintf(path
, "%s/sys/kernel/notes", machine
->root_dir
);
1361 if (sysfs__read_build_id(path
, &dso
->bid
) == 0)
1362 dso
->has_build_id
= true;
1365 int dso__kernel_module_get_build_id(struct dso
*dso
,
1366 const char *root_dir
)
1368 char filename
[PATH_MAX
];
1370 * kernel module short names are of the form "[module]" and
1371 * we need just "module" here.
1373 const char *name
= dso
->short_name
+ 1;
1375 snprintf(filename
, sizeof(filename
),
1376 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1377 root_dir
, (int)strlen(name
) - 1, name
);
1379 if (sysfs__read_build_id(filename
, &dso
->bid
) == 0)
1380 dso
->has_build_id
= true;
1385 static size_t dso__fprintf_buildid(struct dso
*dso
, FILE *fp
)
1387 char sbuild_id
[SBUILD_ID_SIZE
];
1389 build_id__sprintf(&dso
->bid
, sbuild_id
);
1390 return fprintf(fp
, "%s", sbuild_id
);
1393 size_t dso__fprintf(struct dso
*dso
, FILE *fp
)
1396 size_t ret
= fprintf(fp
, "dso: %s (", dso
->short_name
);
1398 if (dso
->short_name
!= dso
->long_name
)
1399 ret
+= fprintf(fp
, "%s, ", dso
->long_name
);
1400 ret
+= fprintf(fp
, "%sloaded, ", dso__loaded(dso
) ? "" : "NOT ");
1401 ret
+= dso__fprintf_buildid(dso
, fp
);
1402 ret
+= fprintf(fp
, ")\n");
1403 for (nd
= rb_first_cached(&dso
->symbols
); nd
; nd
= rb_next(nd
)) {
1404 struct symbol
*pos
= rb_entry(nd
, struct symbol
, rb_node
);
1405 ret
+= symbol__fprintf(pos
, fp
);
1411 enum dso_type
dso__type(struct dso
*dso
, struct machine
*machine
)
1414 enum dso_type type
= DSO__TYPE_UNKNOWN
;
1416 fd
= dso__data_get_fd(dso
, machine
);
1418 type
= dso__type_fd(fd
);
1419 dso__data_put_fd(dso
);
1425 int dso__strerror_load(struct dso
*dso
, char *buf
, size_t buflen
)
1427 int idx
, errnum
= dso
->load_errno
;
1429 * This must have a same ordering as the enum dso_load_errno.
1431 static const char *dso_load__error_str
[] = {
1432 "Internal tools/perf/ library error",
1434 "Can not read build id",
1435 "Mismatching build id",
1436 "Decompression failure",
1439 BUG_ON(buflen
== 0);
1442 const char *err
= str_error_r(errnum
, buf
, buflen
);
1445 scnprintf(buf
, buflen
, "%s", err
);
1450 if (errnum
< __DSO_LOAD_ERRNO__START
|| errnum
>= __DSO_LOAD_ERRNO__END
)
1453 idx
= errnum
- __DSO_LOAD_ERRNO__START
;
1454 scnprintf(buf
, buflen
, "%s", dso_load__error_str
[idx
]);