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 #include <bpf/libbpf.h>
15 #include "bpf-event.h"
18 #include "namespaces.h"
27 #include "util.h" /* O_CLOEXEC for older systems */
32 static const char * const debuglink_paths
[] = {
39 char dso__symtab_origin(const struct dso
*dso
)
41 static const char origin
[] = {
42 [DSO_BINARY_TYPE__KALLSYMS
] = 'k',
43 [DSO_BINARY_TYPE__VMLINUX
] = 'v',
44 [DSO_BINARY_TYPE__JAVA_JIT
] = 'j',
45 [DSO_BINARY_TYPE__DEBUGLINK
] = 'l',
46 [DSO_BINARY_TYPE__BUILD_ID_CACHE
] = 'B',
47 [DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO
] = 'D',
48 [DSO_BINARY_TYPE__FEDORA_DEBUGINFO
] = 'f',
49 [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO
] = 'u',
50 [DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO
] = 'x',
51 [DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO
] = 'o',
52 [DSO_BINARY_TYPE__BUILDID_DEBUGINFO
] = 'b',
53 [DSO_BINARY_TYPE__SYSTEM_PATH_DSO
] = 'd',
54 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE
] = 'K',
55 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP
] = 'm',
56 [DSO_BINARY_TYPE__GUEST_KALLSYMS
] = 'g',
57 [DSO_BINARY_TYPE__GUEST_KMODULE
] = 'G',
58 [DSO_BINARY_TYPE__GUEST_KMODULE_COMP
] = 'M',
59 [DSO_BINARY_TYPE__GUEST_VMLINUX
] = 'V',
62 if (dso
== NULL
|| dso
->symtab_type
== DSO_BINARY_TYPE__NOT_FOUND
)
64 return origin
[dso
->symtab_type
];
67 int dso__read_binary_type_filename(const struct dso
*dso
,
68 enum dso_binary_type type
,
69 char *root_dir
, char *filename
, size_t size
)
71 char build_id_hex
[SBUILD_ID_SIZE
];
76 case DSO_BINARY_TYPE__DEBUGLINK
:
78 const char *last_slash
;
79 char dso_dir
[PATH_MAX
];
80 char symfile
[PATH_MAX
];
83 len
= __symbol__join_symfs(filename
, size
, dso
->long_name
);
84 last_slash
= filename
+ len
;
85 while (last_slash
!= filename
&& *last_slash
!= '/')
88 strncpy(dso_dir
, filename
, last_slash
- filename
);
89 dso_dir
[last_slash
-filename
] = '\0';
91 if (!is_regular_file(filename
)) {
96 ret
= filename__read_debuglink(filename
, symfile
, PATH_MAX
);
100 /* Check predefined locations where debug file might reside */
102 for (i
= 0; i
< ARRAY_SIZE(debuglink_paths
); i
++) {
103 snprintf(filename
, size
,
104 debuglink_paths
[i
], dso_dir
, symfile
);
105 if (is_regular_file(filename
)) {
113 case DSO_BINARY_TYPE__BUILD_ID_CACHE
:
114 if (dso__build_id_filename(dso
, filename
, size
, false) == NULL
)
118 case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO
:
119 if (dso__build_id_filename(dso
, filename
, size
, true) == NULL
)
123 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO
:
124 len
= __symbol__join_symfs(filename
, size
, "/usr/lib/debug");
125 snprintf(filename
+ len
, size
- len
, "%s.debug", dso
->long_name
);
128 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO
:
129 len
= __symbol__join_symfs(filename
, size
, "/usr/lib/debug");
130 snprintf(filename
+ len
, size
- len
, "%s", dso
->long_name
);
133 case DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO
:
135 * Ubuntu can mixup /usr/lib with /lib, putting debuginfo in
136 * /usr/lib/debug/lib when it is expected to be in
137 * /usr/lib/debug/usr/lib
139 if (strlen(dso
->long_name
) < 9 ||
140 strncmp(dso
->long_name
, "/usr/lib/", 9)) {
144 len
= __symbol__join_symfs(filename
, size
, "/usr/lib/debug");
145 snprintf(filename
+ len
, size
- len
, "%s", dso
->long_name
+ 4);
148 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO
:
150 const char *last_slash
;
153 last_slash
= dso
->long_name
+ dso
->long_name_len
;
154 while (last_slash
!= dso
->long_name
&& *last_slash
!= '/')
157 len
= __symbol__join_symfs(filename
, size
, "");
158 dir_size
= last_slash
- dso
->long_name
+ 2;
159 if (dir_size
> (size
- len
)) {
163 len
+= scnprintf(filename
+ len
, dir_size
, "%s", dso
->long_name
);
164 len
+= scnprintf(filename
+ len
, size
- len
, ".debug%s",
169 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO
:
170 if (!dso
->has_build_id
) {
175 build_id__sprintf(dso
->build_id
,
176 sizeof(dso
->build_id
),
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__NOT_FOUND
:
222 static const struct {
224 int (*decompress
)(const char *input
, int output
);
225 bool (*is_compressed
)(const char *input
);
227 [COMP_ID__NONE
] = { .fmt
= NULL
, },
228 #ifdef HAVE_ZLIB_SUPPORT
229 { "gz", gzip_decompress_to_file
, gzip_is_compressed
},
231 #ifdef HAVE_LZMA_SUPPORT
232 { "xz", lzma_decompress_to_file
, lzma_is_compressed
},
234 { NULL
, NULL
, NULL
},
237 static int is_supported_compression(const char *ext
)
241 for (i
= 1; compressions
[i
].fmt
; i
++) {
242 if (!strcmp(ext
, compressions
[i
].fmt
))
245 return COMP_ID__NONE
;
248 bool is_kernel_module(const char *pathname
, int cpumode
)
251 int mode
= cpumode
& PERF_RECORD_MISC_CPUMODE_MASK
;
253 WARN_ONCE(mode
!= cpumode
,
254 "Internal error: passing unmasked cpumode (%x) to is_kernel_module",
258 case PERF_RECORD_MISC_USER
:
259 case PERF_RECORD_MISC_HYPERVISOR
:
260 case PERF_RECORD_MISC_GUEST_USER
:
262 /* Treat PERF_RECORD_MISC_CPUMODE_UNKNOWN as kernel */
264 if (kmod_path__parse(&m
, pathname
)) {
265 pr_err("Failed to check whether %s is a kernel module or not. Assume it is.",
274 bool dso__needs_decompress(struct dso
*dso
)
276 return dso
->symtab_type
== DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP
||
277 dso
->symtab_type
== DSO_BINARY_TYPE__GUEST_KMODULE_COMP
;
280 static int decompress_kmodule(struct dso
*dso
, const char *name
,
281 char *pathname
, size_t len
)
283 char tmpbuf
[] = KMOD_DECOMP_NAME
;
286 if (!dso__needs_decompress(dso
))
289 if (dso
->comp
== COMP_ID__NONE
)
293 * We have proper compression id for DSO and yet the file
294 * behind the 'name' can still be plain uncompressed object.
296 * The reason is behind the logic we open the DSO object files,
297 * when we try all possible 'debug' objects until we find the
298 * data. So even if the DSO is represented by 'krava.xz' module,
299 * we can end up here opening ~/.debug/....23432432/debug' file
300 * which is not compressed.
302 * To keep this transparent, we detect this and return the file
303 * descriptor to the uncompressed file.
305 if (!compressions
[dso
->comp
].is_compressed(name
))
306 return open(name
, O_RDONLY
);
308 fd
= mkstemp(tmpbuf
);
310 dso
->load_errno
= errno
;
314 if (compressions
[dso
->comp
].decompress(name
, fd
)) {
315 dso
->load_errno
= DSO_LOAD_ERRNO__DECOMPRESSION_FAILURE
;
320 if (!pathname
|| (fd
< 0))
323 if (pathname
&& (fd
>= 0))
324 strlcpy(pathname
, tmpbuf
, len
);
329 int dso__decompress_kmodule_fd(struct dso
*dso
, const char *name
)
331 return decompress_kmodule(dso
, name
, NULL
, 0);
334 int dso__decompress_kmodule_path(struct dso
*dso
, const char *name
,
335 char *pathname
, size_t len
)
337 int fd
= decompress_kmodule(dso
, name
, pathname
, len
);
340 return fd
>= 0 ? 0 : -1;
344 * Parses kernel module specified in @path and updates
347 * @comp - true if @path contains supported compression suffix,
349 * @kmod - true if @path contains '.ko' suffix in right position,
351 * @name - if (@alloc_name && @kmod) is true, it contains strdup-ed base name
352 * of the kernel module without suffixes, otherwise strudup-ed
354 * @ext - if (@alloc_ext && @comp) is true, it contains strdup-ed string
355 * the compression suffix
357 * Returns 0 if there's no strdup error, -ENOMEM otherwise.
359 int __kmod_path__parse(struct kmod_path
*m
, const char *path
,
362 const char *name
= strrchr(path
, '/');
363 const char *ext
= strrchr(path
, '.');
364 bool is_simple_name
= false;
366 memset(m
, 0x0, sizeof(*m
));
367 name
= name
? name
+ 1 : path
;
370 * '.' is also a valid character for module name. For example:
371 * [aaa.bbb] is a valid module name. '[' should have higher
372 * priority than '.ko' suffix.
374 * The kernel names are from machine__mmap_name. Such
375 * name should belong to kernel itself, not kernel module.
377 if (name
[0] == '[') {
378 is_simple_name
= true;
379 if ((strncmp(name
, "[kernel.kallsyms]", 17) == 0) ||
380 (strncmp(name
, "[guest.kernel.kallsyms", 22) == 0) ||
381 (strncmp(name
, "[vdso]", 6) == 0) ||
382 (strncmp(name
, "[vdso32]", 8) == 0) ||
383 (strncmp(name
, "[vdsox32]", 9) == 0) ||
384 (strncmp(name
, "[vsyscall]", 10) == 0)) {
391 /* No extension, just return name. */
392 if ((ext
== NULL
) || is_simple_name
) {
394 m
->name
= strdup(name
);
395 return m
->name
? 0 : -ENOMEM
;
400 m
->comp
= is_supported_compression(ext
+ 1);
401 if (m
->comp
> COMP_ID__NONE
)
404 /* Check .ko extension only if there's enough name left. */
406 m
->kmod
= !strncmp(ext
, ".ko", 3);
410 if (asprintf(&m
->name
, "[%.*s]", (int) (ext
- name
), name
) == -1)
413 if (asprintf(&m
->name
, "%s", name
) == -1)
417 strreplace(m
->name
, '-', '_');
423 void dso__set_module_info(struct dso
*dso
, struct kmod_path
*m
,
424 struct machine
*machine
)
426 if (machine__is_host(machine
))
427 dso
->symtab_type
= DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE
;
429 dso
->symtab_type
= DSO_BINARY_TYPE__GUEST_KMODULE
;
431 /* _KMODULE_COMP should be next to _KMODULE */
432 if (m
->kmod
&& m
->comp
) {
437 dso__set_short_name(dso
, strdup(m
->name
), true);
441 * Global list of open DSOs and the counter.
443 static LIST_HEAD(dso__data_open
);
444 static long dso__data_open_cnt
;
445 static pthread_mutex_t dso__data_open_lock
= PTHREAD_MUTEX_INITIALIZER
;
447 static void dso__list_add(struct dso
*dso
)
449 list_add_tail(&dso
->data
.open_entry
, &dso__data_open
);
450 dso__data_open_cnt
++;
453 static void dso__list_del(struct dso
*dso
)
455 list_del_init(&dso
->data
.open_entry
);
456 WARN_ONCE(dso__data_open_cnt
<= 0,
457 "DSO data fd counter out of bounds.");
458 dso__data_open_cnt
--;
461 static void close_first_dso(void);
463 static int do_open(char *name
)
466 char sbuf
[STRERR_BUFSIZE
];
469 fd
= open(name
, O_RDONLY
|O_CLOEXEC
);
473 pr_debug("dso open failed: %s\n",
474 str_error_r(errno
, sbuf
, sizeof(sbuf
)));
475 if (!dso__data_open_cnt
|| errno
!= EMFILE
)
484 static int __open_dso(struct dso
*dso
, struct machine
*machine
)
487 char *root_dir
= (char *)"";
488 char *name
= malloc(PATH_MAX
);
495 root_dir
= machine
->root_dir
;
497 if (dso__read_binary_type_filename(dso
, dso
->binary_type
,
498 root_dir
, name
, PATH_MAX
))
501 if (!is_regular_file(name
))
504 if (dso__needs_decompress(dso
)) {
505 char newpath
[KMOD_DECOMP_LEN
];
506 size_t len
= sizeof(newpath
);
508 if (dso__decompress_kmodule_path(dso
, name
, newpath
, len
) < 0) {
509 fd
= -dso
->load_errno
;
514 strcpy(name
, newpath
);
527 static void check_data_close(void);
530 * dso_close - Open DSO data file
533 * Open @dso's data file descriptor and updates
534 * list/count of open DSO objects.
536 static int open_dso(struct dso
*dso
, struct machine
*machine
)
541 if (dso
->binary_type
!= DSO_BINARY_TYPE__BUILD_ID_CACHE
)
542 nsinfo__mountns_enter(dso
->nsinfo
, &nsc
);
543 fd
= __open_dso(dso
, machine
);
544 if (dso
->binary_type
!= DSO_BINARY_TYPE__BUILD_ID_CACHE
)
545 nsinfo__mountns_exit(&nsc
);
550 * Check if we crossed the allowed number
551 * of opened DSOs and close one if needed.
559 static void close_data_fd(struct dso
*dso
)
561 if (dso
->data
.fd
>= 0) {
564 dso
->data
.file_size
= 0;
570 * dso_close - Close DSO data file
573 * Close @dso's data file descriptor and updates
574 * list/count of open DSO objects.
576 static void close_dso(struct dso
*dso
)
581 static void close_first_dso(void)
585 dso
= list_first_entry(&dso__data_open
, struct dso
, data
.open_entry
);
589 static rlim_t
get_fd_limit(void)
594 /* Allow half of the current open fd limit. */
595 if (getrlimit(RLIMIT_NOFILE
, &l
) == 0) {
596 if (l
.rlim_cur
== RLIM_INFINITY
)
599 limit
= l
.rlim_cur
/ 2;
601 pr_err("failed to get fd limit\n");
608 static rlim_t fd_limit
;
611 * Used only by tests/dso-data.c to reset the environment
612 * for tests. I dont expect we should change this during
615 void reset_fd_limit(void)
620 static bool may_cache_fd(void)
623 fd_limit
= get_fd_limit();
625 if (fd_limit
== RLIM_INFINITY
)
628 return fd_limit
> (rlim_t
) dso__data_open_cnt
;
632 * Check and close LRU dso if we crossed allowed limit
633 * for opened dso file descriptors. The limit is half
634 * of the RLIMIT_NOFILE files opened.
636 static void check_data_close(void)
638 bool cache_fd
= may_cache_fd();
645 * dso__data_close - Close DSO data file
648 * External interface to close @dso's data file descriptor.
650 void dso__data_close(struct dso
*dso
)
652 pthread_mutex_lock(&dso__data_open_lock
);
654 pthread_mutex_unlock(&dso__data_open_lock
);
657 static void try_to_open_dso(struct dso
*dso
, struct machine
*machine
)
659 enum dso_binary_type binary_type_data
[] = {
660 DSO_BINARY_TYPE__BUILD_ID_CACHE
,
661 DSO_BINARY_TYPE__SYSTEM_PATH_DSO
,
662 DSO_BINARY_TYPE__NOT_FOUND
,
666 if (dso
->data
.fd
>= 0)
669 if (dso
->binary_type
!= DSO_BINARY_TYPE__NOT_FOUND
) {
670 dso
->data
.fd
= open_dso(dso
, machine
);
675 dso
->binary_type
= binary_type_data
[i
++];
677 dso
->data
.fd
= open_dso(dso
, machine
);
678 if (dso
->data
.fd
>= 0)
681 } while (dso
->binary_type
!= DSO_BINARY_TYPE__NOT_FOUND
);
683 if (dso
->data
.fd
>= 0)
684 dso
->data
.status
= DSO_DATA_STATUS_OK
;
686 dso
->data
.status
= DSO_DATA_STATUS_ERROR
;
690 * dso__data_get_fd - Get dso's data file descriptor
692 * @machine: machine object
694 * External interface to find dso's file, open it and
695 * returns file descriptor. It should be paired with
696 * dso__data_put_fd() if it returns non-negative value.
698 int dso__data_get_fd(struct dso
*dso
, struct machine
*machine
)
700 if (dso
->data
.status
== DSO_DATA_STATUS_ERROR
)
703 if (pthread_mutex_lock(&dso__data_open_lock
) < 0)
706 try_to_open_dso(dso
, machine
);
708 if (dso
->data
.fd
< 0)
709 pthread_mutex_unlock(&dso__data_open_lock
);
714 void dso__data_put_fd(struct dso
*dso __maybe_unused
)
716 pthread_mutex_unlock(&dso__data_open_lock
);
719 bool dso__data_status_seen(struct dso
*dso
, enum dso_data_status_seen by
)
723 if (dso
->data
.status_seen
& flag
)
726 dso
->data
.status_seen
|= flag
;
731 static ssize_t
bpf_read(struct dso
*dso
, u64 offset
, char *data
)
733 struct bpf_prog_info_node
*node
;
734 ssize_t size
= DSO__DATA_CACHE_SIZE
;
738 node
= perf_env__find_bpf_prog_info(dso
->bpf_prog
.env
, dso
->bpf_prog
.id
);
739 if (!node
|| !node
->info_linear
) {
740 dso
->data
.status
= DSO_DATA_STATUS_ERROR
;
744 len
= node
->info_linear
->info
.jited_prog_len
;
745 buf
= (u8
*)(uintptr_t)node
->info_linear
->info
.jited_prog_insns
;
750 size
= (ssize_t
)min(len
- offset
, (u64
)size
);
751 memcpy(data
, buf
+ offset
, size
);
755 static int bpf_size(struct dso
*dso
)
757 struct bpf_prog_info_node
*node
;
759 node
= perf_env__find_bpf_prog_info(dso
->bpf_prog
.env
, dso
->bpf_prog
.id
);
760 if (!node
|| !node
->info_linear
) {
761 dso
->data
.status
= DSO_DATA_STATUS_ERROR
;
765 dso
->data
.file_size
= node
->info_linear
->info
.jited_prog_len
;
770 dso_cache__free(struct dso
*dso
)
772 struct rb_root
*root
= &dso
->data
.cache
;
773 struct rb_node
*next
= rb_first(root
);
775 pthread_mutex_lock(&dso
->lock
);
777 struct dso_cache
*cache
;
779 cache
= rb_entry(next
, struct dso_cache
, rb_node
);
780 next
= rb_next(&cache
->rb_node
);
781 rb_erase(&cache
->rb_node
, root
);
784 pthread_mutex_unlock(&dso
->lock
);
787 static struct dso_cache
*dso_cache__find(struct dso
*dso
, u64 offset
)
789 const struct rb_root
*root
= &dso
->data
.cache
;
790 struct rb_node
* const *p
= &root
->rb_node
;
791 const struct rb_node
*parent
= NULL
;
792 struct dso_cache
*cache
;
798 cache
= rb_entry(parent
, struct dso_cache
, rb_node
);
799 end
= cache
->offset
+ DSO__DATA_CACHE_SIZE
;
801 if (offset
< cache
->offset
)
803 else if (offset
>= end
)
812 static struct dso_cache
*
813 dso_cache__insert(struct dso
*dso
, struct dso_cache
*new)
815 struct rb_root
*root
= &dso
->data
.cache
;
816 struct rb_node
**p
= &root
->rb_node
;
817 struct rb_node
*parent
= NULL
;
818 struct dso_cache
*cache
;
819 u64 offset
= new->offset
;
821 pthread_mutex_lock(&dso
->lock
);
826 cache
= rb_entry(parent
, struct dso_cache
, rb_node
);
827 end
= cache
->offset
+ DSO__DATA_CACHE_SIZE
;
829 if (offset
< cache
->offset
)
831 else if (offset
>= end
)
837 rb_link_node(&new->rb_node
, parent
, p
);
838 rb_insert_color(&new->rb_node
, root
);
842 pthread_mutex_unlock(&dso
->lock
);
847 dso_cache__memcpy(struct dso_cache
*cache
, u64 offset
,
850 u64 cache_offset
= offset
- cache
->offset
;
851 u64 cache_size
= min(cache
->size
- cache_offset
, size
);
853 memcpy(data
, cache
->data
+ cache_offset
, cache_size
);
857 static ssize_t
file_read(struct dso
*dso
, struct machine
*machine
,
858 u64 offset
, char *data
)
862 pthread_mutex_lock(&dso__data_open_lock
);
865 * dso->data.fd might be closed if other thread opened another
866 * file (dso) due to open file limit (RLIMIT_NOFILE).
868 try_to_open_dso(dso
, machine
);
870 if (dso
->data
.fd
< 0) {
871 dso
->data
.status
= DSO_DATA_STATUS_ERROR
;
876 ret
= pread(dso
->data
.fd
, data
, DSO__DATA_CACHE_SIZE
, offset
);
878 pthread_mutex_unlock(&dso__data_open_lock
);
883 dso_cache__read(struct dso
*dso
, struct machine
*machine
,
884 u64 offset
, u8
*data
, ssize_t size
)
886 u64 cache_offset
= offset
& DSO__DATA_CACHE_MASK
;
887 struct dso_cache
*cache
;
888 struct dso_cache
*old
;
891 cache
= zalloc(sizeof(*cache
) + DSO__DATA_CACHE_SIZE
);
895 if (dso
->binary_type
== DSO_BINARY_TYPE__BPF_PROG_INFO
)
896 ret
= bpf_read(dso
, cache_offset
, cache
->data
);
898 ret
= file_read(dso
, machine
, cache_offset
, cache
->data
);
901 cache
->offset
= cache_offset
;
904 old
= dso_cache__insert(dso
, cache
);
906 /* we lose the race */
911 ret
= dso_cache__memcpy(cache
, offset
, data
, size
);
920 static ssize_t
dso_cache_read(struct dso
*dso
, struct machine
*machine
,
921 u64 offset
, u8
*data
, ssize_t size
)
923 struct dso_cache
*cache
;
925 cache
= dso_cache__find(dso
, offset
);
927 return dso_cache__memcpy(cache
, offset
, data
, size
);
929 return dso_cache__read(dso
, machine
, offset
, data
, size
);
933 * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
934 * in the rb_tree. Any read to already cached data is served
937 static ssize_t
cached_read(struct dso
*dso
, struct machine
*machine
,
938 u64 offset
, u8
*data
, ssize_t size
)
946 ret
= dso_cache_read(dso
, machine
, offset
, p
, size
);
950 /* Reached EOF, return what we have. */
966 static int file_size(struct dso
*dso
, struct machine
*machine
)
970 char sbuf
[STRERR_BUFSIZE
];
972 pthread_mutex_lock(&dso__data_open_lock
);
975 * dso->data.fd might be closed if other thread opened another
976 * file (dso) due to open file limit (RLIMIT_NOFILE).
978 try_to_open_dso(dso
, machine
);
980 if (dso
->data
.fd
< 0) {
982 dso
->data
.status
= DSO_DATA_STATUS_ERROR
;
986 if (fstat(dso
->data
.fd
, &st
) < 0) {
988 pr_err("dso cache fstat failed: %s\n",
989 str_error_r(errno
, sbuf
, sizeof(sbuf
)));
990 dso
->data
.status
= DSO_DATA_STATUS_ERROR
;
993 dso
->data
.file_size
= st
.st_size
;
996 pthread_mutex_unlock(&dso__data_open_lock
);
1000 int dso__data_file_size(struct dso
*dso
, struct machine
*machine
)
1002 if (dso
->data
.file_size
)
1005 if (dso
->data
.status
== DSO_DATA_STATUS_ERROR
)
1008 if (dso
->binary_type
== DSO_BINARY_TYPE__BPF_PROG_INFO
)
1009 return bpf_size(dso
);
1011 return file_size(dso
, machine
);
1015 * dso__data_size - Return dso data size
1017 * @machine: machine object
1019 * Return: dso data size
1021 off_t
dso__data_size(struct dso
*dso
, struct machine
*machine
)
1023 if (dso__data_file_size(dso
, machine
))
1026 /* For now just estimate dso data size is close to file size */
1027 return dso
->data
.file_size
;
1030 static ssize_t
data_read_offset(struct dso
*dso
, struct machine
*machine
,
1031 u64 offset
, u8
*data
, ssize_t size
)
1033 if (dso__data_file_size(dso
, machine
))
1036 /* Check the offset sanity. */
1037 if (offset
> dso
->data
.file_size
)
1040 if (offset
+ size
< offset
)
1043 return cached_read(dso
, machine
, offset
, data
, size
);
1047 * dso__data_read_offset - Read data from dso file offset
1049 * @machine: machine object
1050 * @offset: file offset
1051 * @data: buffer to store data
1052 * @size: size of the @data buffer
1054 * External interface to read data from dso file offset. Open
1055 * dso data file and use cached_read to get the data.
1057 ssize_t
dso__data_read_offset(struct dso
*dso
, struct machine
*machine
,
1058 u64 offset
, u8
*data
, ssize_t size
)
1060 if (dso
->data
.status
== DSO_DATA_STATUS_ERROR
)
1063 return data_read_offset(dso
, machine
, offset
, data
, size
);
1067 * dso__data_read_addr - Read data from dso address
1069 * @machine: machine object
1070 * @add: virtual memory address
1071 * @data: buffer to store data
1072 * @size: size of the @data buffer
1074 * External interface to read data from dso address.
1076 ssize_t
dso__data_read_addr(struct dso
*dso
, struct map
*map
,
1077 struct machine
*machine
, u64 addr
,
1078 u8
*data
, ssize_t size
)
1080 u64 offset
= map
->map_ip(map
, addr
);
1081 return dso__data_read_offset(dso
, machine
, offset
, data
, size
);
1084 struct map
*dso__new_map(const char *name
)
1086 struct map
*map
= NULL
;
1087 struct dso
*dso
= dso__new(name
);
1090 map
= map__new2(0, dso
);
1097 struct dso
*machine__findnew_kernel(struct machine
*machine
, const char *name
,
1098 const char *short_name
, int dso_type
)
1101 * The kernel dso could be created by build_id processing.
1103 struct dso
*dso
= machine__findnew_dso(machine
, name
);
1106 * We need to run this in all cases, since during the build_id
1107 * processing we had no idea this was the kernel dso.
1110 dso__set_short_name(dso
, short_name
, false);
1111 dso
->kernel
= dso_type
;
1117 void dso__set_long_name(struct dso
*dso
, const char *name
, bool name_allocated
)
1119 struct rb_root
*root
= dso
->root
;
1124 if (dso
->long_name_allocated
)
1125 free((char *)dso
->long_name
);
1128 rb_erase(&dso
->rb_node
, root
);
1130 * __dsos__findnew_link_by_longname() isn't guaranteed to add it
1131 * back, so a clean removal is required here.
1133 RB_CLEAR_NODE(&dso
->rb_node
);
1137 dso
->long_name
= name
;
1138 dso
->long_name_len
= strlen(name
);
1139 dso
->long_name_allocated
= name_allocated
;
1142 __dsos__findnew_link_by_longname(root
, dso
, NULL
);
1145 void dso__set_short_name(struct dso
*dso
, const char *name
, bool name_allocated
)
1150 if (dso
->short_name_allocated
)
1151 free((char *)dso
->short_name
);
1153 dso
->short_name
= name
;
1154 dso
->short_name_len
= strlen(name
);
1155 dso
->short_name_allocated
= name_allocated
;
1158 int dso__name_len(const struct dso
*dso
)
1161 return strlen("[unknown]");
1163 return dso
->long_name_len
;
1165 return dso
->short_name_len
;
1168 bool dso__loaded(const struct dso
*dso
)
1173 bool dso__sorted_by_name(const struct dso
*dso
)
1175 return dso
->sorted_by_name
;
1178 void dso__set_sorted_by_name(struct dso
*dso
)
1180 dso
->sorted_by_name
= true;
1183 struct dso
*dso__new(const char *name
)
1185 struct dso
*dso
= calloc(1, sizeof(*dso
) + strlen(name
) + 1);
1188 strcpy(dso
->name
, name
);
1189 dso__set_long_name(dso
, dso
->name
, false);
1190 dso__set_short_name(dso
, dso
->name
, false);
1191 dso
->symbols
= dso
->symbol_names
= RB_ROOT_CACHED
;
1192 dso
->data
.cache
= RB_ROOT
;
1193 dso
->inlined_nodes
= RB_ROOT_CACHED
;
1194 dso
->srclines
= RB_ROOT_CACHED
;
1196 dso
->data
.status
= DSO_DATA_STATUS_UNKNOWN
;
1197 dso
->symtab_type
= DSO_BINARY_TYPE__NOT_FOUND
;
1198 dso
->binary_type
= DSO_BINARY_TYPE__NOT_FOUND
;
1199 dso
->is_64_bit
= (sizeof(void *) == 8);
1202 dso
->sorted_by_name
= 0;
1203 dso
->has_build_id
= 0;
1204 dso
->has_srcline
= 1;
1206 dso
->kernel
= DSO_TYPE_USER
;
1207 dso
->needs_swap
= DSO_SWAP__UNSET
;
1208 dso
->comp
= COMP_ID__NONE
;
1209 RB_CLEAR_NODE(&dso
->rb_node
);
1211 INIT_LIST_HEAD(&dso
->node
);
1212 INIT_LIST_HEAD(&dso
->data
.open_entry
);
1213 pthread_mutex_init(&dso
->lock
, NULL
);
1214 refcount_set(&dso
->refcnt
, 1);
1220 void dso__delete(struct dso
*dso
)
1222 if (!RB_EMPTY_NODE(&dso
->rb_node
))
1223 pr_err("DSO %s is still in rbtree when being deleted!\n",
1226 /* free inlines first, as they reference symbols */
1227 inlines__tree_delete(&dso
->inlined_nodes
);
1228 srcline__tree_delete(&dso
->srclines
);
1229 symbols__delete(&dso
->symbols
);
1231 if (dso
->short_name_allocated
) {
1232 zfree((char **)&dso
->short_name
);
1233 dso
->short_name_allocated
= false;
1236 if (dso
->long_name_allocated
) {
1237 zfree((char **)&dso
->long_name
);
1238 dso
->long_name_allocated
= false;
1241 dso__data_close(dso
);
1242 auxtrace_cache__free(dso
->auxtrace_cache
);
1243 dso_cache__free(dso
);
1245 zfree(&dso
->symsrc_filename
);
1246 nsinfo__zput(dso
->nsinfo
);
1247 pthread_mutex_destroy(&dso
->lock
);
1251 struct dso
*dso__get(struct dso
*dso
)
1254 refcount_inc(&dso
->refcnt
);
1258 void dso__put(struct dso
*dso
)
1260 if (dso
&& refcount_dec_and_test(&dso
->refcnt
))
1264 void dso__set_build_id(struct dso
*dso
, void *build_id
)
1266 memcpy(dso
->build_id
, build_id
, sizeof(dso
->build_id
));
1267 dso
->has_build_id
= 1;
1270 bool dso__build_id_equal(const struct dso
*dso
, u8
*build_id
)
1272 return memcmp(dso
->build_id
, build_id
, sizeof(dso
->build_id
)) == 0;
1275 void dso__read_running_kernel_build_id(struct dso
*dso
, struct machine
*machine
)
1277 char path
[PATH_MAX
];
1279 if (machine__is_default_guest(machine
))
1281 sprintf(path
, "%s/sys/kernel/notes", machine
->root_dir
);
1282 if (sysfs__read_build_id(path
, dso
->build_id
,
1283 sizeof(dso
->build_id
)) == 0)
1284 dso
->has_build_id
= true;
1287 int dso__kernel_module_get_build_id(struct dso
*dso
,
1288 const char *root_dir
)
1290 char filename
[PATH_MAX
];
1292 * kernel module short names are of the form "[module]" and
1293 * we need just "module" here.
1295 const char *name
= dso
->short_name
+ 1;
1297 snprintf(filename
, sizeof(filename
),
1298 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1299 root_dir
, (int)strlen(name
) - 1, name
);
1301 if (sysfs__read_build_id(filename
, dso
->build_id
,
1302 sizeof(dso
->build_id
)) == 0)
1303 dso
->has_build_id
= true;
1308 size_t dso__fprintf_buildid(struct dso
*dso
, FILE *fp
)
1310 char sbuild_id
[SBUILD_ID_SIZE
];
1312 build_id__sprintf(dso
->build_id
, sizeof(dso
->build_id
), sbuild_id
);
1313 return fprintf(fp
, "%s", sbuild_id
);
1316 size_t dso__fprintf(struct dso
*dso
, FILE *fp
)
1319 size_t ret
= fprintf(fp
, "dso: %s (", dso
->short_name
);
1321 if (dso
->short_name
!= dso
->long_name
)
1322 ret
+= fprintf(fp
, "%s, ", dso
->long_name
);
1323 ret
+= fprintf(fp
, "%sloaded, ", dso__loaded(dso
) ? "" : "NOT ");
1324 ret
+= dso__fprintf_buildid(dso
, fp
);
1325 ret
+= fprintf(fp
, ")\n");
1326 for (nd
= rb_first_cached(&dso
->symbols
); nd
; nd
= rb_next(nd
)) {
1327 struct symbol
*pos
= rb_entry(nd
, struct symbol
, rb_node
);
1328 ret
+= symbol__fprintf(pos
, fp
);
1334 enum dso_type
dso__type(struct dso
*dso
, struct machine
*machine
)
1337 enum dso_type type
= DSO__TYPE_UNKNOWN
;
1339 fd
= dso__data_get_fd(dso
, machine
);
1341 type
= dso__type_fd(fd
);
1342 dso__data_put_fd(dso
);
1348 int dso__strerror_load(struct dso
*dso
, char *buf
, size_t buflen
)
1350 int idx
, errnum
= dso
->load_errno
;
1352 * This must have a same ordering as the enum dso_load_errno.
1354 static const char *dso_load__error_str
[] = {
1355 "Internal tools/perf/ library error",
1357 "Can not read build id",
1358 "Mismatching build id",
1359 "Decompression failure",
1362 BUG_ON(buflen
== 0);
1365 const char *err
= str_error_r(errnum
, buf
, buflen
);
1368 scnprintf(buf
, buflen
, "%s", err
);
1373 if (errnum
< __DSO_LOAD_ERRNO__START
|| errnum
>= __DSO_LOAD_ERRNO__END
)
1376 idx
= errnum
- __DSO_LOAD_ERRNO__START
;
1377 scnprintf(buf
, buflen
, "%s", dso_load__error_str
[idx
]);