1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/kernel.h>
5 #include <sys/resource.h>
13 #include "namespaces.h"
26 static const char * const debuglink_paths
[] = {
33 char dso__symtab_origin(const struct dso
*dso
)
35 static const char origin
[] = {
36 [DSO_BINARY_TYPE__KALLSYMS
] = 'k',
37 [DSO_BINARY_TYPE__VMLINUX
] = 'v',
38 [DSO_BINARY_TYPE__JAVA_JIT
] = 'j',
39 [DSO_BINARY_TYPE__DEBUGLINK
] = 'l',
40 [DSO_BINARY_TYPE__BUILD_ID_CACHE
] = 'B',
41 [DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO
] = 'D',
42 [DSO_BINARY_TYPE__FEDORA_DEBUGINFO
] = 'f',
43 [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO
] = 'u',
44 [DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO
] = 'o',
45 [DSO_BINARY_TYPE__BUILDID_DEBUGINFO
] = 'b',
46 [DSO_BINARY_TYPE__SYSTEM_PATH_DSO
] = 'd',
47 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE
] = 'K',
48 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP
] = 'm',
49 [DSO_BINARY_TYPE__GUEST_KALLSYMS
] = 'g',
50 [DSO_BINARY_TYPE__GUEST_KMODULE
] = 'G',
51 [DSO_BINARY_TYPE__GUEST_KMODULE_COMP
] = 'M',
52 [DSO_BINARY_TYPE__GUEST_VMLINUX
] = 'V',
55 if (dso
== NULL
|| dso
->symtab_type
== DSO_BINARY_TYPE__NOT_FOUND
)
57 return origin
[dso
->symtab_type
];
60 int dso__read_binary_type_filename(const struct dso
*dso
,
61 enum dso_binary_type type
,
62 char *root_dir
, char *filename
, size_t size
)
64 char build_id_hex
[SBUILD_ID_SIZE
];
69 case DSO_BINARY_TYPE__DEBUGLINK
:
71 const char *last_slash
;
72 char dso_dir
[PATH_MAX
];
73 char symfile
[PATH_MAX
];
76 len
= __symbol__join_symfs(filename
, size
, dso
->long_name
);
77 last_slash
= filename
+ len
;
78 while (last_slash
!= filename
&& *last_slash
!= '/')
81 strncpy(dso_dir
, filename
, last_slash
- filename
);
82 dso_dir
[last_slash
-filename
] = '\0';
84 if (!is_regular_file(filename
)) {
89 ret
= filename__read_debuglink(filename
, symfile
, PATH_MAX
);
93 /* Check predefined locations where debug file might reside */
95 for (i
= 0; i
< ARRAY_SIZE(debuglink_paths
); i
++) {
96 snprintf(filename
, size
,
97 debuglink_paths
[i
], dso_dir
, symfile
);
98 if (is_regular_file(filename
)) {
106 case DSO_BINARY_TYPE__BUILD_ID_CACHE
:
107 if (dso__build_id_filename(dso
, filename
, size
, false) == NULL
)
111 case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO
:
112 if (dso__build_id_filename(dso
, filename
, size
, true) == NULL
)
116 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO
:
117 len
= __symbol__join_symfs(filename
, size
, "/usr/lib/debug");
118 snprintf(filename
+ len
, size
- len
, "%s.debug", dso
->long_name
);
121 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO
:
122 len
= __symbol__join_symfs(filename
, size
, "/usr/lib/debug");
123 snprintf(filename
+ len
, size
- len
, "%s", dso
->long_name
);
126 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO
:
128 const char *last_slash
;
131 last_slash
= dso
->long_name
+ dso
->long_name_len
;
132 while (last_slash
!= dso
->long_name
&& *last_slash
!= '/')
135 len
= __symbol__join_symfs(filename
, size
, "");
136 dir_size
= last_slash
- dso
->long_name
+ 2;
137 if (dir_size
> (size
- len
)) {
141 len
+= scnprintf(filename
+ len
, dir_size
, "%s", dso
->long_name
);
142 len
+= scnprintf(filename
+ len
, size
- len
, ".debug%s",
147 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO
:
148 if (!dso
->has_build_id
) {
153 build_id__sprintf(dso
->build_id
,
154 sizeof(dso
->build_id
),
156 len
= __symbol__join_symfs(filename
, size
, "/usr/lib/debug/.build-id/");
157 snprintf(filename
+ len
, size
- len
, "%.2s/%s.debug",
158 build_id_hex
, build_id_hex
+ 2);
161 case DSO_BINARY_TYPE__VMLINUX
:
162 case DSO_BINARY_TYPE__GUEST_VMLINUX
:
163 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO
:
164 __symbol__join_symfs(filename
, size
, dso
->long_name
);
167 case DSO_BINARY_TYPE__GUEST_KMODULE
:
168 case DSO_BINARY_TYPE__GUEST_KMODULE_COMP
:
169 path__join3(filename
, size
, symbol_conf
.symfs
,
170 root_dir
, dso
->long_name
);
173 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE
:
174 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP
:
175 __symbol__join_symfs(filename
, size
, dso
->long_name
);
178 case DSO_BINARY_TYPE__KCORE
:
179 case DSO_BINARY_TYPE__GUEST_KCORE
:
180 snprintf(filename
, size
, "%s", dso
->long_name
);
184 case DSO_BINARY_TYPE__KALLSYMS
:
185 case DSO_BINARY_TYPE__GUEST_KALLSYMS
:
186 case DSO_BINARY_TYPE__JAVA_JIT
:
187 case DSO_BINARY_TYPE__NOT_FOUND
:
199 static const struct {
201 int (*decompress
)(const char *input
, int output
);
202 bool (*is_compressed
)(const char *input
);
204 [COMP_ID__NONE
] = { .fmt
= NULL
, },
205 #ifdef HAVE_ZLIB_SUPPORT
206 { "gz", gzip_decompress_to_file
, gzip_is_compressed
},
208 #ifdef HAVE_LZMA_SUPPORT
209 { "xz", lzma_decompress_to_file
, lzma_is_compressed
},
211 { NULL
, NULL
, NULL
},
214 static int is_supported_compression(const char *ext
)
218 for (i
= 1; compressions
[i
].fmt
; i
++) {
219 if (!strcmp(ext
, compressions
[i
].fmt
))
222 return COMP_ID__NONE
;
225 bool is_kernel_module(const char *pathname
, int cpumode
)
228 int mode
= cpumode
& PERF_RECORD_MISC_CPUMODE_MASK
;
230 WARN_ONCE(mode
!= cpumode
,
231 "Internal error: passing unmasked cpumode (%x) to is_kernel_module",
235 case PERF_RECORD_MISC_USER
:
236 case PERF_RECORD_MISC_HYPERVISOR
:
237 case PERF_RECORD_MISC_GUEST_USER
:
239 /* Treat PERF_RECORD_MISC_CPUMODE_UNKNOWN as kernel */
241 if (kmod_path__parse(&m
, pathname
)) {
242 pr_err("Failed to check whether %s is a kernel module or not. Assume it is.",
251 bool dso__needs_decompress(struct dso
*dso
)
253 return dso
->symtab_type
== DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP
||
254 dso
->symtab_type
== DSO_BINARY_TYPE__GUEST_KMODULE_COMP
;
257 static int decompress_kmodule(struct dso
*dso
, const char *name
,
258 char *pathname
, size_t len
)
260 char tmpbuf
[] = KMOD_DECOMP_NAME
;
263 if (!dso__needs_decompress(dso
))
266 if (dso
->comp
== COMP_ID__NONE
)
270 * We have proper compression id for DSO and yet the file
271 * behind the 'name' can still be plain uncompressed object.
273 * The reason is behind the logic we open the DSO object files,
274 * when we try all possible 'debug' objects until we find the
275 * data. So even if the DSO is represented by 'krava.xz' module,
276 * we can end up here opening ~/.debug/....23432432/debug' file
277 * which is not compressed.
279 * To keep this transparent, we detect this and return the file
280 * descriptor to the uncompressed file.
282 if (!compressions
[dso
->comp
].is_compressed(name
))
283 return open(name
, O_RDONLY
);
285 fd
= mkstemp(tmpbuf
);
287 dso
->load_errno
= errno
;
291 if (compressions
[dso
->comp
].decompress(name
, fd
)) {
292 dso
->load_errno
= DSO_LOAD_ERRNO__DECOMPRESSION_FAILURE
;
297 if (!pathname
|| (fd
< 0))
300 if (pathname
&& (fd
>= 0))
301 strlcpy(pathname
, tmpbuf
, len
);
306 int dso__decompress_kmodule_fd(struct dso
*dso
, const char *name
)
308 return decompress_kmodule(dso
, name
, NULL
, 0);
311 int dso__decompress_kmodule_path(struct dso
*dso
, const char *name
,
312 char *pathname
, size_t len
)
314 int fd
= decompress_kmodule(dso
, name
, pathname
, len
);
317 return fd
>= 0 ? 0 : -1;
321 * Parses kernel module specified in @path and updates
324 * @comp - true if @path contains supported compression suffix,
326 * @kmod - true if @path contains '.ko' suffix in right position,
328 * @name - if (@alloc_name && @kmod) is true, it contains strdup-ed base name
329 * of the kernel module without suffixes, otherwise strudup-ed
331 * @ext - if (@alloc_ext && @comp) is true, it contains strdup-ed string
332 * the compression suffix
334 * Returns 0 if there's no strdup error, -ENOMEM otherwise.
336 int __kmod_path__parse(struct kmod_path
*m
, const char *path
,
339 const char *name
= strrchr(path
, '/');
340 const char *ext
= strrchr(path
, '.');
341 bool is_simple_name
= false;
343 memset(m
, 0x0, sizeof(*m
));
344 name
= name
? name
+ 1 : path
;
347 * '.' is also a valid character for module name. For example:
348 * [aaa.bbb] is a valid module name. '[' should have higher
349 * priority than '.ko' suffix.
351 * The kernel names are from machine__mmap_name. Such
352 * name should belong to kernel itself, not kernel module.
354 if (name
[0] == '[') {
355 is_simple_name
= true;
356 if ((strncmp(name
, "[kernel.kallsyms]", 17) == 0) ||
357 (strncmp(name
, "[guest.kernel.kallsyms", 22) == 0) ||
358 (strncmp(name
, "[vdso]", 6) == 0) ||
359 (strncmp(name
, "[vdso32]", 8) == 0) ||
360 (strncmp(name
, "[vdsox32]", 9) == 0) ||
361 (strncmp(name
, "[vsyscall]", 10) == 0)) {
368 /* No extension, just return name. */
369 if ((ext
== NULL
) || is_simple_name
) {
371 m
->name
= strdup(name
);
372 return m
->name
? 0 : -ENOMEM
;
377 m
->comp
= is_supported_compression(ext
+ 1);
378 if (m
->comp
> COMP_ID__NONE
)
381 /* Check .ko extension only if there's enough name left. */
383 m
->kmod
= !strncmp(ext
, ".ko", 3);
387 if (asprintf(&m
->name
, "[%.*s]", (int) (ext
- name
), name
) == -1)
390 if (asprintf(&m
->name
, "%s", name
) == -1)
394 strxfrchar(m
->name
, '-', '_');
400 void dso__set_module_info(struct dso
*dso
, struct kmod_path
*m
,
401 struct machine
*machine
)
403 if (machine__is_host(machine
))
404 dso
->symtab_type
= DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE
;
406 dso
->symtab_type
= DSO_BINARY_TYPE__GUEST_KMODULE
;
408 /* _KMODULE_COMP should be next to _KMODULE */
409 if (m
->kmod
&& m
->comp
) {
414 dso__set_short_name(dso
, strdup(m
->name
), true);
418 * Global list of open DSOs and the counter.
420 static LIST_HEAD(dso__data_open
);
421 static long dso__data_open_cnt
;
422 static pthread_mutex_t dso__data_open_lock
= PTHREAD_MUTEX_INITIALIZER
;
424 static void dso__list_add(struct dso
*dso
)
426 list_add_tail(&dso
->data
.open_entry
, &dso__data_open
);
427 dso__data_open_cnt
++;
430 static void dso__list_del(struct dso
*dso
)
432 list_del(&dso
->data
.open_entry
);
433 WARN_ONCE(dso__data_open_cnt
<= 0,
434 "DSO data fd counter out of bounds.");
435 dso__data_open_cnt
--;
438 static void close_first_dso(void);
440 static int do_open(char *name
)
443 char sbuf
[STRERR_BUFSIZE
];
446 fd
= open(name
, O_RDONLY
|O_CLOEXEC
);
450 pr_debug("dso open failed: %s\n",
451 str_error_r(errno
, sbuf
, sizeof(sbuf
)));
452 if (!dso__data_open_cnt
|| errno
!= EMFILE
)
461 static int __open_dso(struct dso
*dso
, struct machine
*machine
)
464 char *root_dir
= (char *)"";
465 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
;
491 strcpy(name
, newpath
);
504 static void check_data_close(void);
507 * dso_close - Open DSO data file
510 * Open @dso's data file descriptor and updates
511 * list/count of open DSO objects.
513 static int open_dso(struct dso
*dso
, struct machine
*machine
)
518 if (dso
->binary_type
!= DSO_BINARY_TYPE__BUILD_ID_CACHE
)
519 nsinfo__mountns_enter(dso
->nsinfo
, &nsc
);
520 fd
= __open_dso(dso
, machine
);
521 if (dso
->binary_type
!= DSO_BINARY_TYPE__BUILD_ID_CACHE
)
522 nsinfo__mountns_exit(&nsc
);
527 * Check if we crossed the allowed number
528 * of opened DSOs and close one if needed.
536 static void close_data_fd(struct dso
*dso
)
538 if (dso
->data
.fd
>= 0) {
541 dso
->data
.file_size
= 0;
547 * dso_close - Close DSO data file
550 * Close @dso's data file descriptor and updates
551 * list/count of open DSO objects.
553 static void close_dso(struct dso
*dso
)
558 static void close_first_dso(void)
562 dso
= list_first_entry(&dso__data_open
, struct dso
, data
.open_entry
);
566 static rlim_t
get_fd_limit(void)
571 /* Allow half of the current open fd limit. */
572 if (getrlimit(RLIMIT_NOFILE
, &l
) == 0) {
573 if (l
.rlim_cur
== RLIM_INFINITY
)
576 limit
= l
.rlim_cur
/ 2;
578 pr_err("failed to get fd limit\n");
585 static rlim_t fd_limit
;
588 * Used only by tests/dso-data.c to reset the environment
589 * for tests. I dont expect we should change this during
592 void reset_fd_limit(void)
597 static bool may_cache_fd(void)
600 fd_limit
= get_fd_limit();
602 if (fd_limit
== RLIM_INFINITY
)
605 return fd_limit
> (rlim_t
) dso__data_open_cnt
;
609 * Check and close LRU dso if we crossed allowed limit
610 * for opened dso file descriptors. The limit is half
611 * of the RLIMIT_NOFILE files opened.
613 static void check_data_close(void)
615 bool cache_fd
= may_cache_fd();
622 * dso__data_close - Close DSO data file
625 * External interface to close @dso's data file descriptor.
627 void dso__data_close(struct dso
*dso
)
629 pthread_mutex_lock(&dso__data_open_lock
);
631 pthread_mutex_unlock(&dso__data_open_lock
);
634 static void try_to_open_dso(struct dso
*dso
, struct machine
*machine
)
636 enum dso_binary_type binary_type_data
[] = {
637 DSO_BINARY_TYPE__BUILD_ID_CACHE
,
638 DSO_BINARY_TYPE__SYSTEM_PATH_DSO
,
639 DSO_BINARY_TYPE__NOT_FOUND
,
643 if (dso
->data
.fd
>= 0)
646 if (dso
->binary_type
!= DSO_BINARY_TYPE__NOT_FOUND
) {
647 dso
->data
.fd
= open_dso(dso
, machine
);
652 dso
->binary_type
= binary_type_data
[i
++];
654 dso
->data
.fd
= open_dso(dso
, machine
);
655 if (dso
->data
.fd
>= 0)
658 } while (dso
->binary_type
!= DSO_BINARY_TYPE__NOT_FOUND
);
660 if (dso
->data
.fd
>= 0)
661 dso
->data
.status
= DSO_DATA_STATUS_OK
;
663 dso
->data
.status
= DSO_DATA_STATUS_ERROR
;
667 * dso__data_get_fd - Get dso's data file descriptor
669 * @machine: machine object
671 * External interface to find dso's file, open it and
672 * returns file descriptor. It should be paired with
673 * dso__data_put_fd() if it returns non-negative value.
675 int dso__data_get_fd(struct dso
*dso
, struct machine
*machine
)
677 if (dso
->data
.status
== DSO_DATA_STATUS_ERROR
)
680 if (pthread_mutex_lock(&dso__data_open_lock
) < 0)
683 try_to_open_dso(dso
, machine
);
685 if (dso
->data
.fd
< 0)
686 pthread_mutex_unlock(&dso__data_open_lock
);
691 void dso__data_put_fd(struct dso
*dso __maybe_unused
)
693 pthread_mutex_unlock(&dso__data_open_lock
);
696 bool dso__data_status_seen(struct dso
*dso
, enum dso_data_status_seen by
)
700 if (dso
->data
.status_seen
& flag
)
703 dso
->data
.status_seen
|= flag
;
709 dso_cache__free(struct dso
*dso
)
711 struct rb_root
*root
= &dso
->data
.cache
;
712 struct rb_node
*next
= rb_first(root
);
714 pthread_mutex_lock(&dso
->lock
);
716 struct dso_cache
*cache
;
718 cache
= rb_entry(next
, struct dso_cache
, rb_node
);
719 next
= rb_next(&cache
->rb_node
);
720 rb_erase(&cache
->rb_node
, root
);
723 pthread_mutex_unlock(&dso
->lock
);
726 static struct dso_cache
*dso_cache__find(struct dso
*dso
, u64 offset
)
728 const struct rb_root
*root
= &dso
->data
.cache
;
729 struct rb_node
* const *p
= &root
->rb_node
;
730 const struct rb_node
*parent
= NULL
;
731 struct dso_cache
*cache
;
737 cache
= rb_entry(parent
, struct dso_cache
, rb_node
);
738 end
= cache
->offset
+ DSO__DATA_CACHE_SIZE
;
740 if (offset
< cache
->offset
)
742 else if (offset
>= end
)
751 static struct dso_cache
*
752 dso_cache__insert(struct dso
*dso
, struct dso_cache
*new)
754 struct rb_root
*root
= &dso
->data
.cache
;
755 struct rb_node
**p
= &root
->rb_node
;
756 struct rb_node
*parent
= NULL
;
757 struct dso_cache
*cache
;
758 u64 offset
= new->offset
;
760 pthread_mutex_lock(&dso
->lock
);
765 cache
= rb_entry(parent
, struct dso_cache
, rb_node
);
766 end
= cache
->offset
+ DSO__DATA_CACHE_SIZE
;
768 if (offset
< cache
->offset
)
770 else if (offset
>= end
)
776 rb_link_node(&new->rb_node
, parent
, p
);
777 rb_insert_color(&new->rb_node
, root
);
781 pthread_mutex_unlock(&dso
->lock
);
786 dso_cache__memcpy(struct dso_cache
*cache
, u64 offset
,
789 u64 cache_offset
= offset
- cache
->offset
;
790 u64 cache_size
= min(cache
->size
- cache_offset
, size
);
792 memcpy(data
, cache
->data
+ cache_offset
, cache_size
);
797 dso_cache__read(struct dso
*dso
, struct machine
*machine
,
798 u64 offset
, u8
*data
, ssize_t size
)
800 struct dso_cache
*cache
;
801 struct dso_cache
*old
;
807 cache
= zalloc(sizeof(*cache
) + DSO__DATA_CACHE_SIZE
);
811 pthread_mutex_lock(&dso__data_open_lock
);
814 * dso->data.fd might be closed if other thread opened another
815 * file (dso) due to open file limit (RLIMIT_NOFILE).
817 try_to_open_dso(dso
, machine
);
819 if (dso
->data
.fd
< 0) {
821 dso
->data
.status
= DSO_DATA_STATUS_ERROR
;
825 cache_offset
= offset
& DSO__DATA_CACHE_MASK
;
827 ret
= pread(dso
->data
.fd
, cache
->data
, DSO__DATA_CACHE_SIZE
, cache_offset
);
831 cache
->offset
= cache_offset
;
835 pthread_mutex_unlock(&dso__data_open_lock
);
838 old
= dso_cache__insert(dso
, cache
);
840 /* we lose the race */
845 ret
= dso_cache__memcpy(cache
, offset
, data
, size
);
854 static ssize_t
dso_cache_read(struct dso
*dso
, struct machine
*machine
,
855 u64 offset
, u8
*data
, ssize_t size
)
857 struct dso_cache
*cache
;
859 cache
= dso_cache__find(dso
, offset
);
861 return dso_cache__memcpy(cache
, offset
, data
, size
);
863 return dso_cache__read(dso
, machine
, offset
, data
, size
);
867 * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
868 * in the rb_tree. Any read to already cached data is served
871 static ssize_t
cached_read(struct dso
*dso
, struct machine
*machine
,
872 u64 offset
, u8
*data
, ssize_t size
)
880 ret
= dso_cache_read(dso
, machine
, offset
, p
, size
);
884 /* Reached EOF, return what we have. */
900 int dso__data_file_size(struct dso
*dso
, struct machine
*machine
)
904 char sbuf
[STRERR_BUFSIZE
];
906 if (dso
->data
.file_size
)
909 if (dso
->data
.status
== DSO_DATA_STATUS_ERROR
)
912 pthread_mutex_lock(&dso__data_open_lock
);
915 * dso->data.fd might be closed if other thread opened another
916 * file (dso) due to open file limit (RLIMIT_NOFILE).
918 try_to_open_dso(dso
, machine
);
920 if (dso
->data
.fd
< 0) {
922 dso
->data
.status
= DSO_DATA_STATUS_ERROR
;
926 if (fstat(dso
->data
.fd
, &st
) < 0) {
928 pr_err("dso cache fstat failed: %s\n",
929 str_error_r(errno
, sbuf
, sizeof(sbuf
)));
930 dso
->data
.status
= DSO_DATA_STATUS_ERROR
;
933 dso
->data
.file_size
= st
.st_size
;
936 pthread_mutex_unlock(&dso__data_open_lock
);
941 * dso__data_size - Return dso data size
943 * @machine: machine object
945 * Return: dso data size
947 off_t
dso__data_size(struct dso
*dso
, struct machine
*machine
)
949 if (dso__data_file_size(dso
, machine
))
952 /* For now just estimate dso data size is close to file size */
953 return dso
->data
.file_size
;
956 static ssize_t
data_read_offset(struct dso
*dso
, struct machine
*machine
,
957 u64 offset
, u8
*data
, ssize_t size
)
959 if (dso__data_file_size(dso
, machine
))
962 /* Check the offset sanity. */
963 if (offset
> dso
->data
.file_size
)
966 if (offset
+ size
< offset
)
969 return cached_read(dso
, machine
, offset
, data
, size
);
973 * dso__data_read_offset - Read data from dso file offset
975 * @machine: machine object
976 * @offset: file offset
977 * @data: buffer to store data
978 * @size: size of the @data buffer
980 * External interface to read data from dso file offset. Open
981 * dso data file and use cached_read to get the data.
983 ssize_t
dso__data_read_offset(struct dso
*dso
, struct machine
*machine
,
984 u64 offset
, u8
*data
, ssize_t size
)
986 if (dso
->data
.status
== DSO_DATA_STATUS_ERROR
)
989 return data_read_offset(dso
, machine
, offset
, data
, size
);
993 * dso__data_read_addr - Read data from dso address
995 * @machine: machine object
996 * @add: virtual memory address
997 * @data: buffer to store data
998 * @size: size of the @data buffer
1000 * External interface to read data from dso address.
1002 ssize_t
dso__data_read_addr(struct dso
*dso
, struct map
*map
,
1003 struct machine
*machine
, u64 addr
,
1004 u8
*data
, ssize_t size
)
1006 u64 offset
= map
->map_ip(map
, addr
);
1007 return dso__data_read_offset(dso
, machine
, offset
, data
, size
);
1010 struct map
*dso__new_map(const char *name
)
1012 struct map
*map
= NULL
;
1013 struct dso
*dso
= dso__new(name
);
1016 map
= map__new2(0, dso
);
1021 struct dso
*machine__findnew_kernel(struct machine
*machine
, const char *name
,
1022 const char *short_name
, int dso_type
)
1025 * The kernel dso could be created by build_id processing.
1027 struct dso
*dso
= machine__findnew_dso(machine
, name
);
1030 * We need to run this in all cases, since during the build_id
1031 * processing we had no idea this was the kernel dso.
1034 dso__set_short_name(dso
, short_name
, false);
1035 dso
->kernel
= dso_type
;
1042 * Find a matching entry and/or link current entry to RB tree.
1043 * Either one of the dso or name parameter must be non-NULL or the
1044 * function will not work.
1046 static struct dso
*__dso__findlink_by_longname(struct rb_root
*root
,
1047 struct dso
*dso
, const char *name
)
1049 struct rb_node
**p
= &root
->rb_node
;
1050 struct rb_node
*parent
= NULL
;
1053 name
= dso
->long_name
;
1055 * Find node with the matching name
1058 struct dso
*this = rb_entry(*p
, struct dso
, rb_node
);
1059 int rc
= strcmp(name
, this->long_name
);
1064 * In case the new DSO is a duplicate of an existing
1065 * one, print a one-time warning & put the new entry
1066 * at the end of the list of duplicates.
1068 if (!dso
|| (dso
== this))
1069 return this; /* Find matching dso */
1071 * The core kernel DSOs may have duplicated long name.
1072 * In this case, the short name should be different.
1073 * Comparing the short names to differentiate the DSOs.
1075 rc
= strcmp(dso
->short_name
, this->short_name
);
1077 pr_err("Duplicated dso name: %s\n", name
);
1082 p
= &parent
->rb_left
;
1084 p
= &parent
->rb_right
;
1087 /* Add new node and rebalance tree */
1088 rb_link_node(&dso
->rb_node
, parent
, p
);
1089 rb_insert_color(&dso
->rb_node
, root
);
1095 static inline struct dso
*__dso__find_by_longname(struct rb_root
*root
,
1098 return __dso__findlink_by_longname(root
, NULL
, name
);
1101 void dso__set_long_name(struct dso
*dso
, const char *name
, bool name_allocated
)
1103 struct rb_root
*root
= dso
->root
;
1108 if (dso
->long_name_allocated
)
1109 free((char *)dso
->long_name
);
1112 rb_erase(&dso
->rb_node
, root
);
1114 * __dso__findlink_by_longname() isn't guaranteed to add it
1115 * back, so a clean removal is required here.
1117 RB_CLEAR_NODE(&dso
->rb_node
);
1121 dso
->long_name
= name
;
1122 dso
->long_name_len
= strlen(name
);
1123 dso
->long_name_allocated
= name_allocated
;
1126 __dso__findlink_by_longname(root
, dso
, NULL
);
1129 void dso__set_short_name(struct dso
*dso
, const char *name
, bool name_allocated
)
1134 if (dso
->short_name_allocated
)
1135 free((char *)dso
->short_name
);
1137 dso
->short_name
= name
;
1138 dso
->short_name_len
= strlen(name
);
1139 dso
->short_name_allocated
= name_allocated
;
1142 static void dso__set_basename(struct dso
*dso
)
1145 * basename() may modify path buffer, so we must pass
1148 char *base
, *lname
= strdup(dso
->long_name
);
1154 * basename() may return a pointer to internal
1155 * storage which is reused in subsequent calls
1156 * so copy the result.
1158 base
= strdup(basename(lname
));
1165 dso__set_short_name(dso
, base
, true);
1168 int dso__name_len(const struct dso
*dso
)
1171 return strlen("[unknown]");
1173 return dso
->long_name_len
;
1175 return dso
->short_name_len
;
1178 bool dso__loaded(const struct dso
*dso
)
1183 bool dso__sorted_by_name(const struct dso
*dso
)
1185 return dso
->sorted_by_name
;
1188 void dso__set_sorted_by_name(struct dso
*dso
)
1190 dso
->sorted_by_name
= true;
1193 struct dso
*dso__new(const char *name
)
1195 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 dso
->symbols
= dso
->symbol_names
= RB_ROOT_CACHED
;
1202 dso
->data
.cache
= RB_ROOT
;
1203 dso
->inlined_nodes
= RB_ROOT_CACHED
;
1204 dso
->srclines
= RB_ROOT_CACHED
;
1206 dso
->data
.status
= DSO_DATA_STATUS_UNKNOWN
;
1207 dso
->symtab_type
= DSO_BINARY_TYPE__NOT_FOUND
;
1208 dso
->binary_type
= DSO_BINARY_TYPE__NOT_FOUND
;
1209 dso
->is_64_bit
= (sizeof(void *) == 8);
1212 dso
->sorted_by_name
= 0;
1213 dso
->has_build_id
= 0;
1214 dso
->has_srcline
= 1;
1216 dso
->kernel
= DSO_TYPE_USER
;
1217 dso
->needs_swap
= DSO_SWAP__UNSET
;
1218 dso
->comp
= COMP_ID__NONE
;
1219 RB_CLEAR_NODE(&dso
->rb_node
);
1221 INIT_LIST_HEAD(&dso
->node
);
1222 INIT_LIST_HEAD(&dso
->data
.open_entry
);
1223 pthread_mutex_init(&dso
->lock
, NULL
);
1224 refcount_set(&dso
->refcnt
, 1);
1230 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",
1236 /* free inlines first, as they reference symbols */
1237 inlines__tree_delete(&dso
->inlined_nodes
);
1238 srcline__tree_delete(&dso
->srclines
);
1239 symbols__delete(&dso
->symbols
);
1241 if (dso
->short_name_allocated
) {
1242 zfree((char **)&dso
->short_name
);
1243 dso
->short_name_allocated
= false;
1246 if (dso
->long_name_allocated
) {
1247 zfree((char **)&dso
->long_name
);
1248 dso
->long_name_allocated
= false;
1251 dso__data_close(dso
);
1252 auxtrace_cache__free(dso
->auxtrace_cache
);
1253 dso_cache__free(dso
);
1255 zfree(&dso
->symsrc_filename
);
1256 nsinfo__zput(dso
->nsinfo
);
1257 pthread_mutex_destroy(&dso
->lock
);
1261 struct dso
*dso__get(struct dso
*dso
)
1264 refcount_inc(&dso
->refcnt
);
1268 void dso__put(struct dso
*dso
)
1270 if (dso
&& refcount_dec_and_test(&dso
->refcnt
))
1274 void dso__set_build_id(struct dso
*dso
, void *build_id
)
1276 memcpy(dso
->build_id
, build_id
, sizeof(dso
->build_id
));
1277 dso
->has_build_id
= 1;
1280 bool dso__build_id_equal(const struct dso
*dso
, u8
*build_id
)
1282 return memcmp(dso
->build_id
, build_id
, sizeof(dso
->build_id
)) == 0;
1285 void dso__read_running_kernel_build_id(struct dso
*dso
, struct machine
*machine
)
1287 char path
[PATH_MAX
];
1289 if (machine__is_default_guest(machine
))
1291 sprintf(path
, "%s/sys/kernel/notes", machine
->root_dir
);
1292 if (sysfs__read_build_id(path
, dso
->build_id
,
1293 sizeof(dso
->build_id
)) == 0)
1294 dso
->has_build_id
= true;
1297 int dso__kernel_module_get_build_id(struct dso
*dso
,
1298 const char *root_dir
)
1300 char filename
[PATH_MAX
];
1302 * kernel module short names are of the form "[module]" and
1303 * we need just "module" here.
1305 const char *name
= dso
->short_name
+ 1;
1307 snprintf(filename
, sizeof(filename
),
1308 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1309 root_dir
, (int)strlen(name
) - 1, name
);
1311 if (sysfs__read_build_id(filename
, dso
->build_id
,
1312 sizeof(dso
->build_id
)) == 0)
1313 dso
->has_build_id
= true;
1318 bool __dsos__read_build_ids(struct list_head
*head
, bool with_hits
)
1320 bool have_build_id
= false;
1322 struct nscookie nsc
;
1324 list_for_each_entry(pos
, head
, node
) {
1325 if (with_hits
&& !pos
->hit
&& !dso__is_vdso(pos
))
1327 if (pos
->has_build_id
) {
1328 have_build_id
= true;
1331 nsinfo__mountns_enter(pos
->nsinfo
, &nsc
);
1332 if (filename__read_build_id(pos
->long_name
, pos
->build_id
,
1333 sizeof(pos
->build_id
)) > 0) {
1334 have_build_id
= true;
1335 pos
->has_build_id
= true;
1337 nsinfo__mountns_exit(&nsc
);
1340 return have_build_id
;
1343 void __dsos__add(struct dsos
*dsos
, struct dso
*dso
)
1345 list_add_tail(&dso
->node
, &dsos
->head
);
1346 __dso__findlink_by_longname(&dsos
->root
, dso
, NULL
);
1348 * It is now in the linked list, grab a reference, then garbage collect
1349 * this when needing memory, by looking at LRU dso instances in the
1350 * list with atomic_read(&dso->refcnt) == 1, i.e. no references
1351 * anywhere besides the one for the list, do, under a lock for the
1352 * list: remove it from the list, then a dso__put(), that probably will
1353 * be the last and will then call dso__delete(), end of life.
1355 * That, or at the end of the 'struct machine' lifetime, when all
1356 * 'struct dso' instances will be removed from the list, in
1357 * dsos__exit(), if they have no other reference from some other data
1360 * E.g.: after processing a 'perf.data' file and storing references
1361 * to objects instantiated while processing events, we will have
1362 * references to the 'thread', 'map', 'dso' structs all from 'struct
1363 * hist_entry' instances, but we may not need anything not referenced,
1364 * so we might as well call machines__exit()/machines__delete() and
1365 * garbage collect it.
1370 void dsos__add(struct dsos
*dsos
, struct dso
*dso
)
1372 down_write(&dsos
->lock
);
1373 __dsos__add(dsos
, dso
);
1374 up_write(&dsos
->lock
);
1377 struct dso
*__dsos__find(struct dsos
*dsos
, const char *name
, bool cmp_short
)
1382 list_for_each_entry(pos
, &dsos
->head
, node
)
1383 if (strcmp(pos
->short_name
, name
) == 0)
1387 return __dso__find_by_longname(&dsos
->root
, name
);
1390 struct dso
*dsos__find(struct dsos
*dsos
, const char *name
, bool cmp_short
)
1393 down_read(&dsos
->lock
);
1394 dso
= __dsos__find(dsos
, name
, cmp_short
);
1395 up_read(&dsos
->lock
);
1399 struct dso
*__dsos__addnew(struct dsos
*dsos
, const char *name
)
1401 struct dso
*dso
= dso__new(name
);
1404 __dsos__add(dsos
, dso
);
1405 dso__set_basename(dso
);
1406 /* Put dso here because __dsos_add already got it */
1412 struct dso
*__dsos__findnew(struct dsos
*dsos
, const char *name
)
1414 struct dso
*dso
= __dsos__find(dsos
, name
, false);
1416 return dso
? dso
: __dsos__addnew(dsos
, name
);
1419 struct dso
*dsos__findnew(struct dsos
*dsos
, const char *name
)
1422 down_write(&dsos
->lock
);
1423 dso
= dso__get(__dsos__findnew(dsos
, name
));
1424 up_write(&dsos
->lock
);
1428 size_t __dsos__fprintf_buildid(struct list_head
*head
, FILE *fp
,
1429 bool (skip
)(struct dso
*dso
, int parm
), int parm
)
1434 list_for_each_entry(pos
, head
, node
) {
1435 if (skip
&& skip(pos
, parm
))
1437 ret
+= dso__fprintf_buildid(pos
, fp
);
1438 ret
+= fprintf(fp
, " %s\n", pos
->long_name
);
1443 size_t __dsos__fprintf(struct list_head
*head
, FILE *fp
)
1448 list_for_each_entry(pos
, head
, node
) {
1449 ret
+= dso__fprintf(pos
, fp
);
1455 size_t dso__fprintf_buildid(struct dso
*dso
, FILE *fp
)
1457 char sbuild_id
[SBUILD_ID_SIZE
];
1459 build_id__sprintf(dso
->build_id
, sizeof(dso
->build_id
), sbuild_id
);
1460 return fprintf(fp
, "%s", sbuild_id
);
1463 size_t dso__fprintf(struct dso
*dso
, FILE *fp
)
1466 size_t ret
= fprintf(fp
, "dso: %s (", dso
->short_name
);
1468 if (dso
->short_name
!= dso
->long_name
)
1469 ret
+= fprintf(fp
, "%s, ", dso
->long_name
);
1470 ret
+= fprintf(fp
, "%sloaded, ", dso__loaded(dso
) ? "" : "NOT ");
1471 ret
+= dso__fprintf_buildid(dso
, fp
);
1472 ret
+= fprintf(fp
, ")\n");
1473 for (nd
= rb_first_cached(&dso
->symbols
); 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
]);