1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/kernel.h>
5 #include <sys/resource.h>
21 static const char * const debuglink_paths
[] = {
28 char dso__symtab_origin(const struct dso
*dso
)
30 static const char origin
[] = {
31 [DSO_BINARY_TYPE__KALLSYMS
] = 'k',
32 [DSO_BINARY_TYPE__VMLINUX
] = 'v',
33 [DSO_BINARY_TYPE__JAVA_JIT
] = 'j',
34 [DSO_BINARY_TYPE__DEBUGLINK
] = 'l',
35 [DSO_BINARY_TYPE__BUILD_ID_CACHE
] = 'B',
36 [DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO
] = 'D',
37 [DSO_BINARY_TYPE__FEDORA_DEBUGINFO
] = 'f',
38 [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO
] = 'u',
39 [DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO
] = 'o',
40 [DSO_BINARY_TYPE__BUILDID_DEBUGINFO
] = 'b',
41 [DSO_BINARY_TYPE__SYSTEM_PATH_DSO
] = 'd',
42 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE
] = 'K',
43 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP
] = 'm',
44 [DSO_BINARY_TYPE__GUEST_KALLSYMS
] = 'g',
45 [DSO_BINARY_TYPE__GUEST_KMODULE
] = 'G',
46 [DSO_BINARY_TYPE__GUEST_KMODULE_COMP
] = 'M',
47 [DSO_BINARY_TYPE__GUEST_VMLINUX
] = 'V',
50 if (dso
== NULL
|| dso
->symtab_type
== DSO_BINARY_TYPE__NOT_FOUND
)
52 return origin
[dso
->symtab_type
];
55 int dso__read_binary_type_filename(const struct dso
*dso
,
56 enum dso_binary_type type
,
57 char *root_dir
, char *filename
, size_t size
)
59 char build_id_hex
[SBUILD_ID_SIZE
];
64 case DSO_BINARY_TYPE__DEBUGLINK
:
66 const char *last_slash
;
67 char dso_dir
[PATH_MAX
];
68 char symfile
[PATH_MAX
];
71 len
= __symbol__join_symfs(filename
, size
, dso
->long_name
);
72 last_slash
= filename
+ len
;
73 while (last_slash
!= filename
&& *last_slash
!= '/')
76 strncpy(dso_dir
, filename
, last_slash
- filename
);
77 dso_dir
[last_slash
-filename
] = '\0';
79 if (!is_regular_file(filename
)) {
84 ret
= filename__read_debuglink(filename
, symfile
, PATH_MAX
);
88 /* Check predefined locations where debug file might reside */
90 for (i
= 0; i
< ARRAY_SIZE(debuglink_paths
); i
++) {
91 snprintf(filename
, size
,
92 debuglink_paths
[i
], dso_dir
, symfile
);
93 if (is_regular_file(filename
)) {
101 case DSO_BINARY_TYPE__BUILD_ID_CACHE
:
102 if (dso__build_id_filename(dso
, filename
, size
, false) == NULL
)
106 case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO
:
107 if (dso__build_id_filename(dso
, filename
, size
, true) == NULL
)
111 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO
:
112 len
= __symbol__join_symfs(filename
, size
, "/usr/lib/debug");
113 snprintf(filename
+ len
, size
- len
, "%s.debug", dso
->long_name
);
116 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO
:
117 len
= __symbol__join_symfs(filename
, size
, "/usr/lib/debug");
118 snprintf(filename
+ len
, size
- len
, "%s", dso
->long_name
);
121 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO
:
123 const char *last_slash
;
126 last_slash
= dso
->long_name
+ dso
->long_name_len
;
127 while (last_slash
!= dso
->long_name
&& *last_slash
!= '/')
130 len
= __symbol__join_symfs(filename
, size
, "");
131 dir_size
= last_slash
- dso
->long_name
+ 2;
132 if (dir_size
> (size
- len
)) {
136 len
+= scnprintf(filename
+ len
, dir_size
, "%s", dso
->long_name
);
137 len
+= scnprintf(filename
+ len
, size
- len
, ".debug%s",
142 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO
:
143 if (!dso
->has_build_id
) {
148 build_id__sprintf(dso
->build_id
,
149 sizeof(dso
->build_id
),
151 len
= __symbol__join_symfs(filename
, size
, "/usr/lib/debug/.build-id/");
152 snprintf(filename
+ len
, size
- len
, "%.2s/%s.debug",
153 build_id_hex
, build_id_hex
+ 2);
156 case DSO_BINARY_TYPE__VMLINUX
:
157 case DSO_BINARY_TYPE__GUEST_VMLINUX
:
158 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO
:
159 __symbol__join_symfs(filename
, size
, dso
->long_name
);
162 case DSO_BINARY_TYPE__GUEST_KMODULE
:
163 case DSO_BINARY_TYPE__GUEST_KMODULE_COMP
:
164 path__join3(filename
, size
, symbol_conf
.symfs
,
165 root_dir
, dso
->long_name
);
168 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE
:
169 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP
:
170 __symbol__join_symfs(filename
, size
, dso
->long_name
);
173 case DSO_BINARY_TYPE__KCORE
:
174 case DSO_BINARY_TYPE__GUEST_KCORE
:
175 snprintf(filename
, size
, "%s", dso
->long_name
);
179 case DSO_BINARY_TYPE__KALLSYMS
:
180 case DSO_BINARY_TYPE__GUEST_KALLSYMS
:
181 case DSO_BINARY_TYPE__JAVA_JIT
:
182 case DSO_BINARY_TYPE__NOT_FOUND
:
190 static const struct {
192 int (*decompress
)(const char *input
, int output
);
194 #ifdef HAVE_ZLIB_SUPPORT
195 { "gz", gzip_decompress_to_file
},
197 #ifdef HAVE_LZMA_SUPPORT
198 { "xz", lzma_decompress_to_file
},
203 bool is_supported_compression(const char *ext
)
207 for (i
= 0; compressions
[i
].fmt
; i
++) {
208 if (!strcmp(ext
, compressions
[i
].fmt
))
214 bool is_kernel_module(const char *pathname
, int cpumode
)
217 int mode
= cpumode
& PERF_RECORD_MISC_CPUMODE_MASK
;
219 WARN_ONCE(mode
!= cpumode
,
220 "Internal error: passing unmasked cpumode (%x) to is_kernel_module",
224 case PERF_RECORD_MISC_USER
:
225 case PERF_RECORD_MISC_HYPERVISOR
:
226 case PERF_RECORD_MISC_GUEST_USER
:
228 /* Treat PERF_RECORD_MISC_CPUMODE_UNKNOWN as kernel */
230 if (kmod_path__parse(&m
, pathname
)) {
231 pr_err("Failed to check whether %s is a kernel module or not. Assume it is.",
240 bool decompress_to_file(const char *ext
, const char *filename
, int output_fd
)
244 for (i
= 0; compressions
[i
].fmt
; i
++) {
245 if (!strcmp(ext
, compressions
[i
].fmt
))
246 return !compressions
[i
].decompress(filename
,
252 bool dso__needs_decompress(struct dso
*dso
)
254 return dso
->symtab_type
== DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP
||
255 dso
->symtab_type
== DSO_BINARY_TYPE__GUEST_KMODULE_COMP
;
258 static int decompress_kmodule(struct dso
*dso
, const char *name
, char *tmpbuf
)
263 if (!dso__needs_decompress(dso
))
266 if (kmod_path__parse_ext(&m
, dso
->long_name
))
272 fd
= mkstemp(tmpbuf
);
274 dso
->load_errno
= errno
;
278 if (!decompress_to_file(m
.ext
, name
, fd
)) {
279 dso
->load_errno
= DSO_LOAD_ERRNO__DECOMPRESSION_FAILURE
;
289 int dso__decompress_kmodule_fd(struct dso
*dso
, const char *name
)
291 char tmpbuf
[] = KMOD_DECOMP_NAME
;
294 fd
= decompress_kmodule(dso
, name
, tmpbuf
);
299 int dso__decompress_kmodule_path(struct dso
*dso
, const char *name
,
300 char *pathname
, size_t len
)
302 char tmpbuf
[] = KMOD_DECOMP_NAME
;
305 fd
= decompress_kmodule(dso
, name
, tmpbuf
);
311 strncpy(pathname
, tmpbuf
, len
);
317 * Parses kernel module specified in @path and updates
320 * @comp - true if @path contains supported compression suffix,
322 * @kmod - true if @path contains '.ko' suffix in right position,
324 * @name - if (@alloc_name && @kmod) is true, it contains strdup-ed base name
325 * of the kernel module without suffixes, otherwise strudup-ed
327 * @ext - if (@alloc_ext && @comp) is true, it contains strdup-ed string
328 * the compression suffix
330 * Returns 0 if there's no strdup error, -ENOMEM otherwise.
332 int __kmod_path__parse(struct kmod_path
*m
, const char *path
,
333 bool alloc_name
, bool alloc_ext
)
335 const char *name
= strrchr(path
, '/');
336 const char *ext
= strrchr(path
, '.');
337 bool is_simple_name
= false;
339 memset(m
, 0x0, sizeof(*m
));
340 name
= name
? name
+ 1 : path
;
343 * '.' is also a valid character for module name. For example:
344 * [aaa.bbb] is a valid module name. '[' should have higher
345 * priority than '.ko' suffix.
347 * The kernel names are from machine__mmap_name. Such
348 * name should belong to kernel itself, not kernel module.
350 if (name
[0] == '[') {
351 is_simple_name
= true;
352 if ((strncmp(name
, "[kernel.kallsyms]", 17) == 0) ||
353 (strncmp(name
, "[guest.kernel.kallsyms", 22) == 0) ||
354 (strncmp(name
, "[vdso]", 6) == 0) ||
355 (strncmp(name
, "[vsyscall]", 10) == 0)) {
362 /* No extension, just return name. */
363 if ((ext
== NULL
) || is_simple_name
) {
365 m
->name
= strdup(name
);
366 return m
->name
? 0 : -ENOMEM
;
371 if (is_supported_compression(ext
+ 1)) {
376 /* Check .ko extension only if there's enough name left. */
378 m
->kmod
= !strncmp(ext
, ".ko", 3);
382 if (asprintf(&m
->name
, "[%.*s]", (int) (ext
- name
), name
) == -1)
385 if (asprintf(&m
->name
, "%s", name
) == -1)
389 strxfrchar(m
->name
, '-', '_');
392 if (alloc_ext
&& m
->comp
) {
393 m
->ext
= strdup(ext
+ 4);
395 free((void *) m
->name
);
403 void dso__set_module_info(struct dso
*dso
, struct kmod_path
*m
,
404 struct machine
*machine
)
406 if (machine__is_host(machine
))
407 dso
->symtab_type
= DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE
;
409 dso
->symtab_type
= DSO_BINARY_TYPE__GUEST_KMODULE
;
411 /* _KMODULE_COMP should be next to _KMODULE */
412 if (m
->kmod
&& m
->comp
)
415 dso__set_short_name(dso
, strdup(m
->name
), true);
419 * Global list of open DSOs and the counter.
421 static LIST_HEAD(dso__data_open
);
422 static long dso__data_open_cnt
;
423 static pthread_mutex_t dso__data_open_lock
= PTHREAD_MUTEX_INITIALIZER
;
425 static void dso__list_add(struct dso
*dso
)
427 list_add_tail(&dso
->data
.open_entry
, &dso__data_open
);
428 dso__data_open_cnt
++;
431 static void dso__list_del(struct dso
*dso
)
433 list_del(&dso
->data
.open_entry
);
434 WARN_ONCE(dso__data_open_cnt
<= 0,
435 "DSO data fd counter out of bounds.");
436 dso__data_open_cnt
--;
439 static void close_first_dso(void);
441 static int do_open(char *name
)
444 char sbuf
[STRERR_BUFSIZE
];
447 fd
= open(name
, O_RDONLY
);
451 pr_debug("dso open failed: %s\n",
452 str_error_r(errno
, sbuf
, sizeof(sbuf
)));
453 if (!dso__data_open_cnt
|| errno
!= EMFILE
)
462 static int __open_dso(struct dso
*dso
, struct machine
*machine
)
465 char *root_dir
= (char *)"";
466 char *name
= malloc(PATH_MAX
);
472 root_dir
= machine
->root_dir
;
474 if (dso__read_binary_type_filename(dso
, dso
->binary_type
,
475 root_dir
, name
, PATH_MAX
))
478 if (!is_regular_file(name
))
481 if (dso__needs_decompress(dso
)) {
482 char newpath
[KMOD_DECOMP_LEN
];
483 size_t len
= sizeof(newpath
);
485 if (dso__decompress_kmodule_path(dso
, name
, newpath
, len
) < 0) {
486 fd
= -dso
->load_errno
;
490 strcpy(name
, newpath
);
495 if (dso__needs_decompress(dso
))
503 static void check_data_close(void);
506 * dso_close - Open DSO data file
509 * Open @dso's data file descriptor and updates
510 * list/count of open DSO objects.
512 static int open_dso(struct dso
*dso
, struct machine
*machine
)
517 if (dso
->binary_type
!= DSO_BINARY_TYPE__BUILD_ID_CACHE
)
518 nsinfo__mountns_enter(dso
->nsinfo
, &nsc
);
519 fd
= __open_dso(dso
, machine
);
520 if (dso
->binary_type
!= DSO_BINARY_TYPE__BUILD_ID_CACHE
)
521 nsinfo__mountns_exit(&nsc
);
526 * Check if we crossed the allowed number
527 * of opened DSOs and close one if needed.
535 static void close_data_fd(struct dso
*dso
)
537 if (dso
->data
.fd
>= 0) {
540 dso
->data
.file_size
= 0;
546 * dso_close - Close DSO data file
549 * Close @dso's data file descriptor and updates
550 * list/count of open DSO objects.
552 static void close_dso(struct dso
*dso
)
557 static void close_first_dso(void)
561 dso
= list_first_entry(&dso__data_open
, struct dso
, data
.open_entry
);
565 static rlim_t
get_fd_limit(void)
570 /* Allow half of the current open fd limit. */
571 if (getrlimit(RLIMIT_NOFILE
, &l
) == 0) {
572 if (l
.rlim_cur
== RLIM_INFINITY
)
575 limit
= l
.rlim_cur
/ 2;
577 pr_err("failed to get fd limit\n");
584 static rlim_t fd_limit
;
587 * Used only by tests/dso-data.c to reset the environment
588 * for tests. I dont expect we should change this during
591 void reset_fd_limit(void)
596 static bool may_cache_fd(void)
599 fd_limit
= get_fd_limit();
601 if (fd_limit
== RLIM_INFINITY
)
604 return fd_limit
> (rlim_t
) dso__data_open_cnt
;
608 * Check and close LRU dso if we crossed allowed limit
609 * for opened dso file descriptors. The limit is half
610 * of the RLIMIT_NOFILE files opened.
612 static void check_data_close(void)
614 bool cache_fd
= may_cache_fd();
621 * dso__data_close - Close DSO data file
624 * External interface to close @dso's data file descriptor.
626 void dso__data_close(struct dso
*dso
)
628 pthread_mutex_lock(&dso__data_open_lock
);
630 pthread_mutex_unlock(&dso__data_open_lock
);
633 static void try_to_open_dso(struct dso
*dso
, struct machine
*machine
)
635 enum dso_binary_type binary_type_data
[] = {
636 DSO_BINARY_TYPE__BUILD_ID_CACHE
,
637 DSO_BINARY_TYPE__SYSTEM_PATH_DSO
,
638 DSO_BINARY_TYPE__NOT_FOUND
,
642 if (dso
->data
.fd
>= 0)
645 if (dso
->binary_type
!= DSO_BINARY_TYPE__NOT_FOUND
) {
646 dso
->data
.fd
= open_dso(dso
, machine
);
651 dso
->binary_type
= binary_type_data
[i
++];
653 dso
->data
.fd
= open_dso(dso
, machine
);
654 if (dso
->data
.fd
>= 0)
657 } while (dso
->binary_type
!= DSO_BINARY_TYPE__NOT_FOUND
);
659 if (dso
->data
.fd
>= 0)
660 dso
->data
.status
= DSO_DATA_STATUS_OK
;
662 dso
->data
.status
= DSO_DATA_STATUS_ERROR
;
666 * dso__data_get_fd - Get dso's data file descriptor
668 * @machine: machine object
670 * External interface to find dso's file, open it and
671 * returns file descriptor. It should be paired with
672 * dso__data_put_fd() if it returns non-negative value.
674 int dso__data_get_fd(struct dso
*dso
, struct machine
*machine
)
676 if (dso
->data
.status
== DSO_DATA_STATUS_ERROR
)
679 if (pthread_mutex_lock(&dso__data_open_lock
) < 0)
682 try_to_open_dso(dso
, machine
);
684 if (dso
->data
.fd
< 0)
685 pthread_mutex_unlock(&dso__data_open_lock
);
690 void dso__data_put_fd(struct dso
*dso __maybe_unused
)
692 pthread_mutex_unlock(&dso__data_open_lock
);
695 bool dso__data_status_seen(struct dso
*dso
, enum dso_data_status_seen by
)
699 if (dso
->data
.status_seen
& flag
)
702 dso
->data
.status_seen
|= flag
;
708 dso_cache__free(struct dso
*dso
)
710 struct rb_root
*root
= &dso
->data
.cache
;
711 struct rb_node
*next
= rb_first(root
);
713 pthread_mutex_lock(&dso
->lock
);
715 struct dso_cache
*cache
;
717 cache
= rb_entry(next
, struct dso_cache
, rb_node
);
718 next
= rb_next(&cache
->rb_node
);
719 rb_erase(&cache
->rb_node
, root
);
722 pthread_mutex_unlock(&dso
->lock
);
725 static struct dso_cache
*dso_cache__find(struct dso
*dso
, u64 offset
)
727 const struct rb_root
*root
= &dso
->data
.cache
;
728 struct rb_node
* const *p
= &root
->rb_node
;
729 const struct rb_node
*parent
= NULL
;
730 struct dso_cache
*cache
;
736 cache
= rb_entry(parent
, struct dso_cache
, rb_node
);
737 end
= cache
->offset
+ DSO__DATA_CACHE_SIZE
;
739 if (offset
< cache
->offset
)
741 else if (offset
>= end
)
750 static struct dso_cache
*
751 dso_cache__insert(struct dso
*dso
, struct dso_cache
*new)
753 struct rb_root
*root
= &dso
->data
.cache
;
754 struct rb_node
**p
= &root
->rb_node
;
755 struct rb_node
*parent
= NULL
;
756 struct dso_cache
*cache
;
757 u64 offset
= new->offset
;
759 pthread_mutex_lock(&dso
->lock
);
764 cache
= rb_entry(parent
, struct dso_cache
, rb_node
);
765 end
= cache
->offset
+ DSO__DATA_CACHE_SIZE
;
767 if (offset
< cache
->offset
)
769 else if (offset
>= end
)
775 rb_link_node(&new->rb_node
, parent
, p
);
776 rb_insert_color(&new->rb_node
, root
);
780 pthread_mutex_unlock(&dso
->lock
);
785 dso_cache__memcpy(struct dso_cache
*cache
, u64 offset
,
788 u64 cache_offset
= offset
- cache
->offset
;
789 u64 cache_size
= min(cache
->size
- cache_offset
, size
);
791 memcpy(data
, cache
->data
+ cache_offset
, cache_size
);
796 dso_cache__read(struct dso
*dso
, struct machine
*machine
,
797 u64 offset
, u8
*data
, ssize_t size
)
799 struct dso_cache
*cache
;
800 struct dso_cache
*old
;
806 cache
= zalloc(sizeof(*cache
) + DSO__DATA_CACHE_SIZE
);
810 pthread_mutex_lock(&dso__data_open_lock
);
813 * dso->data.fd might be closed if other thread opened another
814 * file (dso) due to open file limit (RLIMIT_NOFILE).
816 try_to_open_dso(dso
, machine
);
818 if (dso
->data
.fd
< 0) {
820 dso
->data
.status
= DSO_DATA_STATUS_ERROR
;
824 cache_offset
= offset
& DSO__DATA_CACHE_MASK
;
826 ret
= pread(dso
->data
.fd
, cache
->data
, DSO__DATA_CACHE_SIZE
, cache_offset
);
830 cache
->offset
= cache_offset
;
834 pthread_mutex_unlock(&dso__data_open_lock
);
837 old
= dso_cache__insert(dso
, cache
);
839 /* we lose the race */
844 ret
= dso_cache__memcpy(cache
, offset
, data
, size
);
853 static ssize_t
dso_cache_read(struct dso
*dso
, struct machine
*machine
,
854 u64 offset
, u8
*data
, ssize_t size
)
856 struct dso_cache
*cache
;
858 cache
= dso_cache__find(dso
, offset
);
860 return dso_cache__memcpy(cache
, offset
, data
, size
);
862 return dso_cache__read(dso
, machine
, offset
, data
, size
);
866 * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
867 * in the rb_tree. Any read to already cached data is served
870 static ssize_t
cached_read(struct dso
*dso
, struct machine
*machine
,
871 u64 offset
, u8
*data
, ssize_t size
)
879 ret
= dso_cache_read(dso
, machine
, offset
, p
, size
);
883 /* Reached EOF, return what we have. */
899 static int data_file_size(struct dso
*dso
, struct machine
*machine
)
903 char sbuf
[STRERR_BUFSIZE
];
905 if (dso
->data
.file_size
)
908 if (dso
->data
.status
== DSO_DATA_STATUS_ERROR
)
911 pthread_mutex_lock(&dso__data_open_lock
);
914 * dso->data.fd might be closed if other thread opened another
915 * file (dso) due to open file limit (RLIMIT_NOFILE).
917 try_to_open_dso(dso
, machine
);
919 if (dso
->data
.fd
< 0) {
921 dso
->data
.status
= DSO_DATA_STATUS_ERROR
;
925 if (fstat(dso
->data
.fd
, &st
) < 0) {
927 pr_err("dso cache fstat failed: %s\n",
928 str_error_r(errno
, sbuf
, sizeof(sbuf
)));
929 dso
->data
.status
= DSO_DATA_STATUS_ERROR
;
932 dso
->data
.file_size
= st
.st_size
;
935 pthread_mutex_unlock(&dso__data_open_lock
);
940 * dso__data_size - Return dso data size
942 * @machine: machine object
944 * Return: dso data size
946 off_t
dso__data_size(struct dso
*dso
, struct machine
*machine
)
948 if (data_file_size(dso
, machine
))
951 /* For now just estimate dso data size is close to file size */
952 return dso
->data
.file_size
;
955 static ssize_t
data_read_offset(struct dso
*dso
, struct machine
*machine
,
956 u64 offset
, u8
*data
, ssize_t size
)
958 if (data_file_size(dso
, machine
))
961 /* Check the offset sanity. */
962 if (offset
> dso
->data
.file_size
)
965 if (offset
+ size
< offset
)
968 return cached_read(dso
, machine
, offset
, data
, size
);
972 * dso__data_read_offset - Read data from dso file offset
974 * @machine: machine object
975 * @offset: file offset
976 * @data: buffer to store data
977 * @size: size of the @data buffer
979 * External interface to read data from dso file offset. Open
980 * dso data file and use cached_read to get the data.
982 ssize_t
dso__data_read_offset(struct dso
*dso
, struct machine
*machine
,
983 u64 offset
, u8
*data
, ssize_t size
)
985 if (dso
->data
.status
== DSO_DATA_STATUS_ERROR
)
988 return data_read_offset(dso
, machine
, offset
, data
, size
);
992 * dso__data_read_addr - Read data from dso address
994 * @machine: machine object
995 * @add: virtual memory address
996 * @data: buffer to store data
997 * @size: size of the @data buffer
999 * External interface to read data from dso address.
1001 ssize_t
dso__data_read_addr(struct dso
*dso
, struct map
*map
,
1002 struct machine
*machine
, u64 addr
,
1003 u8
*data
, ssize_t size
)
1005 u64 offset
= map
->map_ip(map
, addr
);
1006 return dso__data_read_offset(dso
, machine
, offset
, data
, size
);
1009 struct map
*dso__new_map(const char *name
)
1011 struct map
*map
= NULL
;
1012 struct dso
*dso
= dso__new(name
);
1015 map
= map__new2(0, dso
, MAP__FUNCTION
);
1020 struct dso
*machine__findnew_kernel(struct machine
*machine
, const char *name
,
1021 const char *short_name
, int dso_type
)
1024 * The kernel dso could be created by build_id processing.
1026 struct dso
*dso
= machine__findnew_dso(machine
, name
);
1029 * We need to run this in all cases, since during the build_id
1030 * processing we had no idea this was the kernel dso.
1033 dso__set_short_name(dso
, short_name
, false);
1034 dso
->kernel
= dso_type
;
1041 * Find a matching entry and/or link current entry to RB tree.
1042 * Either one of the dso or name parameter must be non-NULL or the
1043 * function will not work.
1045 static struct dso
*__dso__findlink_by_longname(struct rb_root
*root
,
1046 struct dso
*dso
, const char *name
)
1048 struct rb_node
**p
= &root
->rb_node
;
1049 struct rb_node
*parent
= NULL
;
1052 name
= dso
->long_name
;
1054 * Find node with the matching name
1057 struct dso
*this = rb_entry(*p
, struct dso
, rb_node
);
1058 int rc
= strcmp(name
, this->long_name
);
1063 * In case the new DSO is a duplicate of an existing
1064 * one, print a one-time warning & put the new entry
1065 * at the end of the list of duplicates.
1067 if (!dso
|| (dso
== this))
1068 return this; /* Find matching dso */
1070 * The core kernel DSOs may have duplicated long name.
1071 * In this case, the short name should be different.
1072 * Comparing the short names to differentiate the DSOs.
1074 rc
= strcmp(dso
->short_name
, this->short_name
);
1076 pr_err("Duplicated dso name: %s\n", name
);
1081 p
= &parent
->rb_left
;
1083 p
= &parent
->rb_right
;
1086 /* Add new node and rebalance tree */
1087 rb_link_node(&dso
->rb_node
, parent
, p
);
1088 rb_insert_color(&dso
->rb_node
, root
);
1094 static inline struct dso
*__dso__find_by_longname(struct rb_root
*root
,
1097 return __dso__findlink_by_longname(root
, NULL
, name
);
1100 void dso__set_long_name(struct dso
*dso
, const char *name
, bool name_allocated
)
1102 struct rb_root
*root
= dso
->root
;
1107 if (dso
->long_name_allocated
)
1108 free((char *)dso
->long_name
);
1111 rb_erase(&dso
->rb_node
, root
);
1113 * __dso__findlink_by_longname() isn't guaranteed to add it
1114 * back, so a clean removal is required here.
1116 RB_CLEAR_NODE(&dso
->rb_node
);
1120 dso
->long_name
= name
;
1121 dso
->long_name_len
= strlen(name
);
1122 dso
->long_name_allocated
= name_allocated
;
1125 __dso__findlink_by_longname(root
, dso
, NULL
);
1128 void dso__set_short_name(struct dso
*dso
, const char *name
, bool name_allocated
)
1133 if (dso
->short_name_allocated
)
1134 free((char *)dso
->short_name
);
1136 dso
->short_name
= name
;
1137 dso
->short_name_len
= strlen(name
);
1138 dso
->short_name_allocated
= name_allocated
;
1141 static void dso__set_basename(struct dso
*dso
)
1144 * basename() may modify path buffer, so we must pass
1147 char *base
, *lname
= strdup(dso
->long_name
);
1153 * basename() may return a pointer to internal
1154 * storage which is reused in subsequent calls
1155 * so copy the result.
1157 base
= strdup(basename(lname
));
1164 dso__set_short_name(dso
, base
, true);
1167 int dso__name_len(const struct dso
*dso
)
1170 return strlen("[unknown]");
1172 return dso
->long_name_len
;
1174 return dso
->short_name_len
;
1177 bool dso__loaded(const struct dso
*dso
, enum map_type type
)
1179 return dso
->loaded
& (1 << type
);
1182 bool dso__sorted_by_name(const struct dso
*dso
, enum map_type type
)
1184 return dso
->sorted_by_name
& (1 << type
);
1187 void dso__set_sorted_by_name(struct dso
*dso
, enum map_type type
)
1189 dso
->sorted_by_name
|= (1 << type
);
1192 struct dso
*dso__new(const char *name
)
1194 struct dso
*dso
= calloc(1, sizeof(*dso
) + strlen(name
) + 1);
1198 strcpy(dso
->name
, name
);
1199 dso__set_long_name(dso
, dso
->name
, false);
1200 dso__set_short_name(dso
, dso
->name
, false);
1201 for (i
= 0; i
< MAP__NR_TYPES
; ++i
)
1202 dso
->symbols
[i
] = dso
->symbol_names
[i
] = RB_ROOT
;
1203 dso
->data
.cache
= RB_ROOT
;
1205 dso
->data
.status
= DSO_DATA_STATUS_UNKNOWN
;
1206 dso
->symtab_type
= DSO_BINARY_TYPE__NOT_FOUND
;
1207 dso
->binary_type
= DSO_BINARY_TYPE__NOT_FOUND
;
1208 dso
->is_64_bit
= (sizeof(void *) == 8);
1211 dso
->sorted_by_name
= 0;
1212 dso
->has_build_id
= 0;
1213 dso
->has_srcline
= 1;
1215 dso
->kernel
= DSO_TYPE_USER
;
1216 dso
->needs_swap
= DSO_SWAP__UNSET
;
1217 RB_CLEAR_NODE(&dso
->rb_node
);
1219 INIT_LIST_HEAD(&dso
->node
);
1220 INIT_LIST_HEAD(&dso
->data
.open_entry
);
1221 pthread_mutex_init(&dso
->lock
, NULL
);
1222 refcount_set(&dso
->refcnt
, 1);
1228 void dso__delete(struct dso
*dso
)
1232 if (!RB_EMPTY_NODE(&dso
->rb_node
))
1233 pr_err("DSO %s is still in rbtree when being deleted!\n",
1235 for (i
= 0; i
< MAP__NR_TYPES
; ++i
)
1236 symbols__delete(&dso
->symbols
[i
]);
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 pthread_rwlock_wrlock(&dsos
->lock
);
1370 __dsos__add(dsos
, dso
);
1371 pthread_rwlock_unlock(&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 pthread_rwlock_rdlock(&dsos
->lock
);
1391 dso
= __dsos__find(dsos
, name
, cmp_short
);
1392 pthread_rwlock_unlock(&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 pthread_rwlock_wrlock(&dsos
->lock
);
1420 dso
= dso__get(__dsos__findnew(dsos
, name
));
1421 pthread_rwlock_unlock(&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
) {
1447 for (i
= 0; i
< MAP__NR_TYPES
; ++i
)
1448 ret
+= dso__fprintf(pos
, i
, fp
);
1454 size_t dso__fprintf_buildid(struct dso
*dso
, FILE *fp
)
1456 char sbuild_id
[SBUILD_ID_SIZE
];
1458 build_id__sprintf(dso
->build_id
, sizeof(dso
->build_id
), sbuild_id
);
1459 return fprintf(fp
, "%s", sbuild_id
);
1462 size_t dso__fprintf(struct dso
*dso
, enum map_type type
, FILE *fp
)
1465 size_t ret
= fprintf(fp
, "dso: %s (", dso
->short_name
);
1467 if (dso
->short_name
!= dso
->long_name
)
1468 ret
+= fprintf(fp
, "%s, ", dso
->long_name
);
1469 ret
+= fprintf(fp
, "%s, %sloaded, ", map_type__name
[type
],
1470 dso__loaded(dso
, type
) ? "" : "NOT ");
1471 ret
+= dso__fprintf_buildid(dso
, fp
);
1472 ret
+= fprintf(fp
, ")\n");
1473 for (nd
= rb_first(&dso
->symbols
[type
]); nd
; nd
= rb_next(nd
)) {
1474 struct symbol
*pos
= rb_entry(nd
, struct symbol
, rb_node
);
1475 ret
+= symbol__fprintf(pos
, fp
);
1481 enum dso_type
dso__type(struct dso
*dso
, struct machine
*machine
)
1484 enum dso_type type
= DSO__TYPE_UNKNOWN
;
1486 fd
= dso__data_get_fd(dso
, machine
);
1488 type
= dso__type_fd(fd
);
1489 dso__data_put_fd(dso
);
1495 int dso__strerror_load(struct dso
*dso
, char *buf
, size_t buflen
)
1497 int idx
, errnum
= dso
->load_errno
;
1499 * This must have a same ordering as the enum dso_load_errno.
1501 static const char *dso_load__error_str
[] = {
1502 "Internal tools/perf/ library error",
1504 "Can not read build id",
1505 "Mismatching build id",
1506 "Decompression failure",
1509 BUG_ON(buflen
== 0);
1512 const char *err
= str_error_r(errnum
, buf
, buflen
);
1515 scnprintf(buf
, buflen
, "%s", err
);
1520 if (errnum
< __DSO_LOAD_ERRNO__START
|| errnum
>= __DSO_LOAD_ERRNO__END
)
1523 idx
= errnum
- __DSO_LOAD_ERRNO__START
;
1524 scnprintf(buf
, buflen
, "%s", dso_load__error_str
[idx
]);