2 #include <linux/kernel.h>
4 #include <sys/resource.h>
20 static const char * const debuglink_paths
[] = {
27 char dso__symtab_origin(const struct dso
*dso
)
29 static const char origin
[] = {
30 [DSO_BINARY_TYPE__KALLSYMS
] = 'k',
31 [DSO_BINARY_TYPE__VMLINUX
] = 'v',
32 [DSO_BINARY_TYPE__JAVA_JIT
] = 'j',
33 [DSO_BINARY_TYPE__DEBUGLINK
] = 'l',
34 [DSO_BINARY_TYPE__BUILD_ID_CACHE
] = 'B',
35 [DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO
] = 'D',
36 [DSO_BINARY_TYPE__FEDORA_DEBUGINFO
] = 'f',
37 [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO
] = 'u',
38 [DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO
] = 'o',
39 [DSO_BINARY_TYPE__BUILDID_DEBUGINFO
] = 'b',
40 [DSO_BINARY_TYPE__SYSTEM_PATH_DSO
] = 'd',
41 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE
] = 'K',
42 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP
] = 'm',
43 [DSO_BINARY_TYPE__GUEST_KALLSYMS
] = 'g',
44 [DSO_BINARY_TYPE__GUEST_KMODULE
] = 'G',
45 [DSO_BINARY_TYPE__GUEST_KMODULE_COMP
] = 'M',
46 [DSO_BINARY_TYPE__GUEST_VMLINUX
] = 'V',
49 if (dso
== NULL
|| dso
->symtab_type
== DSO_BINARY_TYPE__NOT_FOUND
)
51 return origin
[dso
->symtab_type
];
54 int dso__read_binary_type_filename(const struct dso
*dso
,
55 enum dso_binary_type type
,
56 char *root_dir
, char *filename
, size_t size
)
58 char build_id_hex
[SBUILD_ID_SIZE
];
63 case DSO_BINARY_TYPE__DEBUGLINK
:
65 const char *last_slash
;
66 char dso_dir
[PATH_MAX
];
67 char symfile
[PATH_MAX
];
70 len
= __symbol__join_symfs(filename
, size
, dso
->long_name
);
71 last_slash
= filename
+ len
;
72 while (last_slash
!= filename
&& *last_slash
!= '/')
75 strncpy(dso_dir
, filename
, last_slash
- filename
);
76 dso_dir
[last_slash
-filename
] = '\0';
78 if (!is_regular_file(filename
)) {
83 ret
= filename__read_debuglink(filename
, symfile
, PATH_MAX
);
87 /* Check predefined locations where debug file might reside */
89 for (i
= 0; i
< ARRAY_SIZE(debuglink_paths
); i
++) {
90 snprintf(filename
, size
,
91 debuglink_paths
[i
], dso_dir
, symfile
);
92 if (is_regular_file(filename
)) {
100 case DSO_BINARY_TYPE__BUILD_ID_CACHE
:
101 if (dso__build_id_filename(dso
, filename
, size
, false) == NULL
)
105 case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO
:
106 if (dso__build_id_filename(dso
, filename
, size
, true) == NULL
)
110 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO
:
111 len
= __symbol__join_symfs(filename
, size
, "/usr/lib/debug");
112 snprintf(filename
+ len
, size
- len
, "%s.debug", dso
->long_name
);
115 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO
:
116 len
= __symbol__join_symfs(filename
, size
, "/usr/lib/debug");
117 snprintf(filename
+ len
, size
- len
, "%s", dso
->long_name
);
120 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO
:
122 const char *last_slash
;
125 last_slash
= dso
->long_name
+ dso
->long_name_len
;
126 while (last_slash
!= dso
->long_name
&& *last_slash
!= '/')
129 len
= __symbol__join_symfs(filename
, size
, "");
130 dir_size
= last_slash
- dso
->long_name
+ 2;
131 if (dir_size
> (size
- len
)) {
135 len
+= scnprintf(filename
+ len
, dir_size
, "%s", dso
->long_name
);
136 len
+= scnprintf(filename
+ len
, size
- len
, ".debug%s",
141 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO
:
142 if (!dso
->has_build_id
) {
147 build_id__sprintf(dso
->build_id
,
148 sizeof(dso
->build_id
),
150 len
= __symbol__join_symfs(filename
, size
, "/usr/lib/debug/.build-id/");
151 snprintf(filename
+ len
, size
- len
, "%.2s/%s.debug",
152 build_id_hex
, build_id_hex
+ 2);
155 case DSO_BINARY_TYPE__VMLINUX
:
156 case DSO_BINARY_TYPE__GUEST_VMLINUX
:
157 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO
:
158 __symbol__join_symfs(filename
, size
, dso
->long_name
);
161 case DSO_BINARY_TYPE__GUEST_KMODULE
:
162 case DSO_BINARY_TYPE__GUEST_KMODULE_COMP
:
163 path__join3(filename
, size
, symbol_conf
.symfs
,
164 root_dir
, dso
->long_name
);
167 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE
:
168 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP
:
169 __symbol__join_symfs(filename
, size
, dso
->long_name
);
172 case DSO_BINARY_TYPE__KCORE
:
173 case DSO_BINARY_TYPE__GUEST_KCORE
:
174 snprintf(filename
, size
, "%s", dso
->long_name
);
178 case DSO_BINARY_TYPE__KALLSYMS
:
179 case DSO_BINARY_TYPE__GUEST_KALLSYMS
:
180 case DSO_BINARY_TYPE__JAVA_JIT
:
181 case DSO_BINARY_TYPE__NOT_FOUND
:
189 static const struct {
191 int (*decompress
)(const char *input
, int output
);
193 #ifdef HAVE_ZLIB_SUPPORT
194 { "gz", gzip_decompress_to_file
},
196 #ifdef HAVE_LZMA_SUPPORT
197 { "xz", lzma_decompress_to_file
},
202 bool is_supported_compression(const char *ext
)
206 for (i
= 0; compressions
[i
].fmt
; i
++) {
207 if (!strcmp(ext
, compressions
[i
].fmt
))
213 bool is_kernel_module(const char *pathname
, int cpumode
)
216 int mode
= cpumode
& PERF_RECORD_MISC_CPUMODE_MASK
;
218 WARN_ONCE(mode
!= cpumode
,
219 "Internal error: passing unmasked cpumode (%x) to is_kernel_module",
223 case PERF_RECORD_MISC_USER
:
224 case PERF_RECORD_MISC_HYPERVISOR
:
225 case PERF_RECORD_MISC_GUEST_USER
:
227 /* Treat PERF_RECORD_MISC_CPUMODE_UNKNOWN as kernel */
229 if (kmod_path__parse(&m
, pathname
)) {
230 pr_err("Failed to check whether %s is a kernel module or not. Assume it is.",
239 bool decompress_to_file(const char *ext
, const char *filename
, int output_fd
)
243 for (i
= 0; compressions
[i
].fmt
; i
++) {
244 if (!strcmp(ext
, compressions
[i
].fmt
))
245 return !compressions
[i
].decompress(filename
,
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
, char *tmpbuf
)
262 if (!dso__needs_decompress(dso
))
265 if (kmod_path__parse_ext(&m
, dso
->long_name
))
271 fd
= mkstemp(tmpbuf
);
273 dso
->load_errno
= errno
;
277 if (!decompress_to_file(m
.ext
, name
, fd
)) {
278 dso
->load_errno
= DSO_LOAD_ERRNO__DECOMPRESSION_FAILURE
;
288 int dso__decompress_kmodule_fd(struct dso
*dso
, const char *name
)
290 char tmpbuf
[] = KMOD_DECOMP_NAME
;
293 fd
= decompress_kmodule(dso
, name
, tmpbuf
);
298 int dso__decompress_kmodule_path(struct dso
*dso
, const char *name
,
299 char *pathname
, size_t len
)
301 char tmpbuf
[] = KMOD_DECOMP_NAME
;
304 fd
= decompress_kmodule(dso
, name
, tmpbuf
);
310 strncpy(pathname
, tmpbuf
, len
);
316 * Parses kernel module specified in @path and updates
319 * @comp - true if @path contains supported compression suffix,
321 * @kmod - true if @path contains '.ko' suffix in right position,
323 * @name - if (@alloc_name && @kmod) is true, it contains strdup-ed base name
324 * of the kernel module without suffixes, otherwise strudup-ed
326 * @ext - if (@alloc_ext && @comp) is true, it contains strdup-ed string
327 * the compression suffix
329 * Returns 0 if there's no strdup error, -ENOMEM otherwise.
331 int __kmod_path__parse(struct kmod_path
*m
, const char *path
,
332 bool alloc_name
, bool alloc_ext
)
334 const char *name
= strrchr(path
, '/');
335 const char *ext
= strrchr(path
, '.');
336 bool is_simple_name
= false;
338 memset(m
, 0x0, sizeof(*m
));
339 name
= name
? name
+ 1 : path
;
342 * '.' is also a valid character for module name. For example:
343 * [aaa.bbb] is a valid module name. '[' should have higher
344 * priority than '.ko' suffix.
346 * The kernel names are from machine__mmap_name. Such
347 * name should belong to kernel itself, not kernel module.
349 if (name
[0] == '[') {
350 is_simple_name
= true;
351 if ((strncmp(name
, "[kernel.kallsyms]", 17) == 0) ||
352 (strncmp(name
, "[guest.kernel.kallsyms", 22) == 0) ||
353 (strncmp(name
, "[vdso]", 6) == 0) ||
354 (strncmp(name
, "[vsyscall]", 10) == 0)) {
361 /* No extension, just return name. */
362 if ((ext
== NULL
) || is_simple_name
) {
364 m
->name
= strdup(name
);
365 return m
->name
? 0 : -ENOMEM
;
370 if (is_supported_compression(ext
+ 1)) {
375 /* Check .ko extension only if there's enough name left. */
377 m
->kmod
= !strncmp(ext
, ".ko", 3);
381 if (asprintf(&m
->name
, "[%.*s]", (int) (ext
- name
), name
) == -1)
384 if (asprintf(&m
->name
, "%s", name
) == -1)
388 strxfrchar(m
->name
, '-', '_');
391 if (alloc_ext
&& m
->comp
) {
392 m
->ext
= strdup(ext
+ 4);
394 free((void *) m
->name
);
402 void dso__set_module_info(struct dso
*dso
, struct kmod_path
*m
,
403 struct machine
*machine
)
405 if (machine__is_host(machine
))
406 dso
->symtab_type
= DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE
;
408 dso
->symtab_type
= DSO_BINARY_TYPE__GUEST_KMODULE
;
410 /* _KMODULE_COMP should be next to _KMODULE */
411 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
);
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
);
471 root_dir
= machine
->root_dir
;
473 if (dso__read_binary_type_filename(dso
, dso
->binary_type
,
474 root_dir
, name
, PATH_MAX
))
477 if (!is_regular_file(name
))
480 if (dso__needs_decompress(dso
)) {
481 char newpath
[KMOD_DECOMP_LEN
];
482 size_t len
= sizeof(newpath
);
484 if (dso__decompress_kmodule_path(dso
, name
, newpath
, len
) < 0) {
485 fd
= -dso
->load_errno
;
489 strcpy(name
, newpath
);
494 if (dso__needs_decompress(dso
))
502 static void check_data_close(void);
505 * dso_close - Open DSO data file
508 * Open @dso's data file descriptor and updates
509 * list/count of open DSO objects.
511 static int open_dso(struct dso
*dso
, struct machine
*machine
)
516 if (dso
->binary_type
!= DSO_BINARY_TYPE__BUILD_ID_CACHE
)
517 nsinfo__mountns_enter(dso
->nsinfo
, &nsc
);
518 fd
= __open_dso(dso
, machine
);
519 if (dso
->binary_type
!= DSO_BINARY_TYPE__BUILD_ID_CACHE
)
520 nsinfo__mountns_exit(&nsc
);
525 * Check if we crossed the allowed number
526 * of opened DSOs and close one if needed.
534 static void close_data_fd(struct dso
*dso
)
536 if (dso
->data
.fd
>= 0) {
539 dso
->data
.file_size
= 0;
545 * dso_close - Close DSO data file
548 * Close @dso's data file descriptor and updates
549 * list/count of open DSO objects.
551 static void close_dso(struct dso
*dso
)
556 static void close_first_dso(void)
560 dso
= list_first_entry(&dso__data_open
, struct dso
, data
.open_entry
);
564 static rlim_t
get_fd_limit(void)
569 /* Allow half of the current open fd limit. */
570 if (getrlimit(RLIMIT_NOFILE
, &l
) == 0) {
571 if (l
.rlim_cur
== RLIM_INFINITY
)
574 limit
= l
.rlim_cur
/ 2;
576 pr_err("failed to get fd limit\n");
583 static rlim_t fd_limit
;
586 * Used only by tests/dso-data.c to reset the environment
587 * for tests. I dont expect we should change this during
590 void reset_fd_limit(void)
595 static bool may_cache_fd(void)
598 fd_limit
= get_fd_limit();
600 if (fd_limit
== RLIM_INFINITY
)
603 return fd_limit
> (rlim_t
) dso__data_open_cnt
;
607 * Check and close LRU dso if we crossed allowed limit
608 * for opened dso file descriptors. The limit is half
609 * of the RLIMIT_NOFILE files opened.
611 static void check_data_close(void)
613 bool cache_fd
= may_cache_fd();
620 * dso__data_close - Close DSO data file
623 * External interface to close @dso's data file descriptor.
625 void dso__data_close(struct dso
*dso
)
627 pthread_mutex_lock(&dso__data_open_lock
);
629 pthread_mutex_unlock(&dso__data_open_lock
);
632 static void try_to_open_dso(struct dso
*dso
, struct machine
*machine
)
634 enum dso_binary_type binary_type_data
[] = {
635 DSO_BINARY_TYPE__BUILD_ID_CACHE
,
636 DSO_BINARY_TYPE__SYSTEM_PATH_DSO
,
637 DSO_BINARY_TYPE__NOT_FOUND
,
641 if (dso
->data
.fd
>= 0)
644 if (dso
->binary_type
!= DSO_BINARY_TYPE__NOT_FOUND
) {
645 dso
->data
.fd
= open_dso(dso
, machine
);
650 dso
->binary_type
= binary_type_data
[i
++];
652 dso
->data
.fd
= open_dso(dso
, machine
);
653 if (dso
->data
.fd
>= 0)
656 } while (dso
->binary_type
!= DSO_BINARY_TYPE__NOT_FOUND
);
658 if (dso
->data
.fd
>= 0)
659 dso
->data
.status
= DSO_DATA_STATUS_OK
;
661 dso
->data
.status
= DSO_DATA_STATUS_ERROR
;
665 * dso__data_get_fd - Get dso's data file descriptor
667 * @machine: machine object
669 * External interface to find dso's file, open it and
670 * returns file descriptor. It should be paired with
671 * dso__data_put_fd() if it returns non-negative value.
673 int dso__data_get_fd(struct dso
*dso
, struct machine
*machine
)
675 if (dso
->data
.status
== DSO_DATA_STATUS_ERROR
)
678 if (pthread_mutex_lock(&dso__data_open_lock
) < 0)
681 try_to_open_dso(dso
, machine
);
683 if (dso
->data
.fd
< 0)
684 pthread_mutex_unlock(&dso__data_open_lock
);
689 void dso__data_put_fd(struct dso
*dso __maybe_unused
)
691 pthread_mutex_unlock(&dso__data_open_lock
);
694 bool dso__data_status_seen(struct dso
*dso
, enum dso_data_status_seen by
)
698 if (dso
->data
.status_seen
& flag
)
701 dso
->data
.status_seen
|= flag
;
707 dso_cache__free(struct dso
*dso
)
709 struct rb_root
*root
= &dso
->data
.cache
;
710 struct rb_node
*next
= rb_first(root
);
712 pthread_mutex_lock(&dso
->lock
);
714 struct dso_cache
*cache
;
716 cache
= rb_entry(next
, struct dso_cache
, rb_node
);
717 next
= rb_next(&cache
->rb_node
);
718 rb_erase(&cache
->rb_node
, root
);
721 pthread_mutex_unlock(&dso
->lock
);
724 static struct dso_cache
*dso_cache__find(struct dso
*dso
, u64 offset
)
726 const struct rb_root
*root
= &dso
->data
.cache
;
727 struct rb_node
* const *p
= &root
->rb_node
;
728 const struct rb_node
*parent
= NULL
;
729 struct dso_cache
*cache
;
735 cache
= rb_entry(parent
, struct dso_cache
, rb_node
);
736 end
= cache
->offset
+ DSO__DATA_CACHE_SIZE
;
738 if (offset
< cache
->offset
)
740 else if (offset
>= end
)
749 static struct dso_cache
*
750 dso_cache__insert(struct dso
*dso
, struct dso_cache
*new)
752 struct rb_root
*root
= &dso
->data
.cache
;
753 struct rb_node
**p
= &root
->rb_node
;
754 struct rb_node
*parent
= NULL
;
755 struct dso_cache
*cache
;
756 u64 offset
= new->offset
;
758 pthread_mutex_lock(&dso
->lock
);
763 cache
= rb_entry(parent
, struct dso_cache
, rb_node
);
764 end
= cache
->offset
+ DSO__DATA_CACHE_SIZE
;
766 if (offset
< cache
->offset
)
768 else if (offset
>= end
)
774 rb_link_node(&new->rb_node
, parent
, p
);
775 rb_insert_color(&new->rb_node
, root
);
779 pthread_mutex_unlock(&dso
->lock
);
784 dso_cache__memcpy(struct dso_cache
*cache
, u64 offset
,
787 u64 cache_offset
= offset
- cache
->offset
;
788 u64 cache_size
= min(cache
->size
- cache_offset
, size
);
790 memcpy(data
, cache
->data
+ cache_offset
, cache_size
);
795 dso_cache__read(struct dso
*dso
, struct machine
*machine
,
796 u64 offset
, u8
*data
, ssize_t size
)
798 struct dso_cache
*cache
;
799 struct dso_cache
*old
;
805 cache
= zalloc(sizeof(*cache
) + DSO__DATA_CACHE_SIZE
);
809 pthread_mutex_lock(&dso__data_open_lock
);
812 * dso->data.fd might be closed if other thread opened another
813 * file (dso) due to open file limit (RLIMIT_NOFILE).
815 try_to_open_dso(dso
, machine
);
817 if (dso
->data
.fd
< 0) {
819 dso
->data
.status
= DSO_DATA_STATUS_ERROR
;
823 cache_offset
= offset
& DSO__DATA_CACHE_MASK
;
825 ret
= pread(dso
->data
.fd
, cache
->data
, DSO__DATA_CACHE_SIZE
, cache_offset
);
829 cache
->offset
= cache_offset
;
833 pthread_mutex_unlock(&dso__data_open_lock
);
836 old
= dso_cache__insert(dso
, cache
);
838 /* we lose the race */
843 ret
= dso_cache__memcpy(cache
, offset
, data
, size
);
852 static ssize_t
dso_cache_read(struct dso
*dso
, struct machine
*machine
,
853 u64 offset
, u8
*data
, ssize_t size
)
855 struct dso_cache
*cache
;
857 cache
= dso_cache__find(dso
, offset
);
859 return dso_cache__memcpy(cache
, offset
, data
, size
);
861 return dso_cache__read(dso
, machine
, offset
, data
, size
);
865 * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
866 * in the rb_tree. Any read to already cached data is served
869 static ssize_t
cached_read(struct dso
*dso
, struct machine
*machine
,
870 u64 offset
, u8
*data
, ssize_t size
)
878 ret
= dso_cache_read(dso
, machine
, offset
, p
, size
);
882 /* Reached EOF, return what we have. */
898 static int data_file_size(struct dso
*dso
, struct machine
*machine
)
902 char sbuf
[STRERR_BUFSIZE
];
904 if (dso
->data
.file_size
)
907 if (dso
->data
.status
== DSO_DATA_STATUS_ERROR
)
910 pthread_mutex_lock(&dso__data_open_lock
);
913 * dso->data.fd might be closed if other thread opened another
914 * file (dso) due to open file limit (RLIMIT_NOFILE).
916 try_to_open_dso(dso
, machine
);
918 if (dso
->data
.fd
< 0) {
920 dso
->data
.status
= DSO_DATA_STATUS_ERROR
;
924 if (fstat(dso
->data
.fd
, &st
) < 0) {
926 pr_err("dso cache fstat failed: %s\n",
927 str_error_r(errno
, sbuf
, sizeof(sbuf
)));
928 dso
->data
.status
= DSO_DATA_STATUS_ERROR
;
931 dso
->data
.file_size
= st
.st_size
;
934 pthread_mutex_unlock(&dso__data_open_lock
);
939 * dso__data_size - Return dso data size
941 * @machine: machine object
943 * Return: dso data size
945 off_t
dso__data_size(struct dso
*dso
, struct machine
*machine
)
947 if (data_file_size(dso
, machine
))
950 /* For now just estimate dso data size is close to file size */
951 return dso
->data
.file_size
;
954 static ssize_t
data_read_offset(struct dso
*dso
, struct machine
*machine
,
955 u64 offset
, u8
*data
, ssize_t size
)
957 if (data_file_size(dso
, machine
))
960 /* Check the offset sanity. */
961 if (offset
> dso
->data
.file_size
)
964 if (offset
+ size
< offset
)
967 return cached_read(dso
, machine
, offset
, data
, size
);
971 * dso__data_read_offset - Read data from dso file offset
973 * @machine: machine object
974 * @offset: file offset
975 * @data: buffer to store data
976 * @size: size of the @data buffer
978 * External interface to read data from dso file offset. Open
979 * dso data file and use cached_read to get the data.
981 ssize_t
dso__data_read_offset(struct dso
*dso
, struct machine
*machine
,
982 u64 offset
, u8
*data
, ssize_t size
)
984 if (dso
->data
.status
== DSO_DATA_STATUS_ERROR
)
987 return data_read_offset(dso
, machine
, offset
, data
, size
);
991 * dso__data_read_addr - Read data from dso address
993 * @machine: machine object
994 * @add: virtual memory address
995 * @data: buffer to store data
996 * @size: size of the @data buffer
998 * External interface to read data from dso address.
1000 ssize_t
dso__data_read_addr(struct dso
*dso
, struct map
*map
,
1001 struct machine
*machine
, u64 addr
,
1002 u8
*data
, ssize_t size
)
1004 u64 offset
= map
->map_ip(map
, addr
);
1005 return dso__data_read_offset(dso
, machine
, offset
, data
, size
);
1008 struct map
*dso__new_map(const char *name
)
1010 struct map
*map
= NULL
;
1011 struct dso
*dso
= dso__new(name
);
1014 map
= map__new2(0, dso
, MAP__FUNCTION
);
1019 struct dso
*machine__findnew_kernel(struct machine
*machine
, const char *name
,
1020 const char *short_name
, int dso_type
)
1023 * The kernel dso could be created by build_id processing.
1025 struct dso
*dso
= machine__findnew_dso(machine
, name
);
1028 * We need to run this in all cases, since during the build_id
1029 * processing we had no idea this was the kernel dso.
1032 dso__set_short_name(dso
, short_name
, false);
1033 dso
->kernel
= dso_type
;
1040 * Find a matching entry and/or link current entry to RB tree.
1041 * Either one of the dso or name parameter must be non-NULL or the
1042 * function will not work.
1044 static struct dso
*__dso__findlink_by_longname(struct rb_root
*root
,
1045 struct dso
*dso
, const char *name
)
1047 struct rb_node
**p
= &root
->rb_node
;
1048 struct rb_node
*parent
= NULL
;
1051 name
= dso
->long_name
;
1053 * Find node with the matching name
1056 struct dso
*this = rb_entry(*p
, struct dso
, rb_node
);
1057 int rc
= strcmp(name
, this->long_name
);
1062 * In case the new DSO is a duplicate of an existing
1063 * one, print a one-time warning & put the new entry
1064 * at the end of the list of duplicates.
1066 if (!dso
|| (dso
== this))
1067 return this; /* Find matching dso */
1069 * The core kernel DSOs may have duplicated long name.
1070 * In this case, the short name should be different.
1071 * Comparing the short names to differentiate the DSOs.
1073 rc
= strcmp(dso
->short_name
, this->short_name
);
1075 pr_err("Duplicated dso name: %s\n", name
);
1080 p
= &parent
->rb_left
;
1082 p
= &parent
->rb_right
;
1085 /* Add new node and rebalance tree */
1086 rb_link_node(&dso
->rb_node
, parent
, p
);
1087 rb_insert_color(&dso
->rb_node
, root
);
1093 static inline struct dso
*__dso__find_by_longname(struct rb_root
*root
,
1096 return __dso__findlink_by_longname(root
, NULL
, name
);
1099 void dso__set_long_name(struct dso
*dso
, const char *name
, bool name_allocated
)
1101 struct rb_root
*root
= dso
->root
;
1106 if (dso
->long_name_allocated
)
1107 free((char *)dso
->long_name
);
1110 rb_erase(&dso
->rb_node
, root
);
1112 * __dso__findlink_by_longname() isn't guaranteed to add it
1113 * back, so a clean removal is required here.
1115 RB_CLEAR_NODE(&dso
->rb_node
);
1119 dso
->long_name
= name
;
1120 dso
->long_name_len
= strlen(name
);
1121 dso
->long_name_allocated
= name_allocated
;
1124 __dso__findlink_by_longname(root
, dso
, NULL
);
1127 void dso__set_short_name(struct dso
*dso
, const char *name
, bool name_allocated
)
1132 if (dso
->short_name_allocated
)
1133 free((char *)dso
->short_name
);
1135 dso
->short_name
= name
;
1136 dso
->short_name_len
= strlen(name
);
1137 dso
->short_name_allocated
= name_allocated
;
1140 static void dso__set_basename(struct dso
*dso
)
1143 * basename() may modify path buffer, so we must pass
1146 char *base
, *lname
= strdup(dso
->long_name
);
1152 * basename() may return a pointer to internal
1153 * storage which is reused in subsequent calls
1154 * so copy the result.
1156 base
= strdup(basename(lname
));
1163 dso__set_short_name(dso
, base
, true);
1166 int dso__name_len(const struct dso
*dso
)
1169 return strlen("[unknown]");
1171 return dso
->long_name_len
;
1173 return dso
->short_name_len
;
1176 bool dso__loaded(const struct dso
*dso
, enum map_type type
)
1178 return dso
->loaded
& (1 << type
);
1181 bool dso__sorted_by_name(const struct dso
*dso
, enum map_type type
)
1183 return dso
->sorted_by_name
& (1 << type
);
1186 void dso__set_sorted_by_name(struct dso
*dso
, enum map_type type
)
1188 dso
->sorted_by_name
|= (1 << type
);
1191 struct dso
*dso__new(const char *name
)
1193 struct dso
*dso
= calloc(1, sizeof(*dso
) + strlen(name
) + 1);
1197 strcpy(dso
->name
, name
);
1198 dso__set_long_name(dso
, dso
->name
, false);
1199 dso__set_short_name(dso
, dso
->name
, false);
1200 for (i
= 0; i
< MAP__NR_TYPES
; ++i
)
1201 dso
->symbols
[i
] = dso
->symbol_names
[i
] = RB_ROOT
;
1202 dso
->data
.cache
= RB_ROOT
;
1204 dso
->data
.status
= DSO_DATA_STATUS_UNKNOWN
;
1205 dso
->symtab_type
= DSO_BINARY_TYPE__NOT_FOUND
;
1206 dso
->binary_type
= DSO_BINARY_TYPE__NOT_FOUND
;
1207 dso
->is_64_bit
= (sizeof(void *) == 8);
1210 dso
->sorted_by_name
= 0;
1211 dso
->has_build_id
= 0;
1212 dso
->has_srcline
= 1;
1214 dso
->kernel
= DSO_TYPE_USER
;
1215 dso
->needs_swap
= DSO_SWAP__UNSET
;
1216 RB_CLEAR_NODE(&dso
->rb_node
);
1218 INIT_LIST_HEAD(&dso
->node
);
1219 INIT_LIST_HEAD(&dso
->data
.open_entry
);
1220 pthread_mutex_init(&dso
->lock
, NULL
);
1221 refcount_set(&dso
->refcnt
, 1);
1227 void dso__delete(struct dso
*dso
)
1231 if (!RB_EMPTY_NODE(&dso
->rb_node
))
1232 pr_err("DSO %s is still in rbtree when being deleted!\n",
1234 for (i
= 0; i
< MAP__NR_TYPES
; ++i
)
1235 symbols__delete(&dso
->symbols
[i
]);
1237 if (dso
->short_name_allocated
) {
1238 zfree((char **)&dso
->short_name
);
1239 dso
->short_name_allocated
= false;
1242 if (dso
->long_name_allocated
) {
1243 zfree((char **)&dso
->long_name
);
1244 dso
->long_name_allocated
= false;
1247 dso__data_close(dso
);
1248 auxtrace_cache__free(dso
->auxtrace_cache
);
1249 dso_cache__free(dso
);
1251 zfree(&dso
->symsrc_filename
);
1252 nsinfo__zput(dso
->nsinfo
);
1253 pthread_mutex_destroy(&dso
->lock
);
1257 struct dso
*dso__get(struct dso
*dso
)
1260 refcount_inc(&dso
->refcnt
);
1264 void dso__put(struct dso
*dso
)
1266 if (dso
&& refcount_dec_and_test(&dso
->refcnt
))
1270 void dso__set_build_id(struct dso
*dso
, void *build_id
)
1272 memcpy(dso
->build_id
, build_id
, sizeof(dso
->build_id
));
1273 dso
->has_build_id
= 1;
1276 bool dso__build_id_equal(const struct dso
*dso
, u8
*build_id
)
1278 return memcmp(dso
->build_id
, build_id
, sizeof(dso
->build_id
)) == 0;
1281 void dso__read_running_kernel_build_id(struct dso
*dso
, struct machine
*machine
)
1283 char path
[PATH_MAX
];
1285 if (machine__is_default_guest(machine
))
1287 sprintf(path
, "%s/sys/kernel/notes", machine
->root_dir
);
1288 if (sysfs__read_build_id(path
, dso
->build_id
,
1289 sizeof(dso
->build_id
)) == 0)
1290 dso
->has_build_id
= true;
1293 int dso__kernel_module_get_build_id(struct dso
*dso
,
1294 const char *root_dir
)
1296 char filename
[PATH_MAX
];
1298 * kernel module short names are of the form "[module]" and
1299 * we need just "module" here.
1301 const char *name
= dso
->short_name
+ 1;
1303 snprintf(filename
, sizeof(filename
),
1304 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1305 root_dir
, (int)strlen(name
) - 1, name
);
1307 if (sysfs__read_build_id(filename
, dso
->build_id
,
1308 sizeof(dso
->build_id
)) == 0)
1309 dso
->has_build_id
= true;
1314 bool __dsos__read_build_ids(struct list_head
*head
, bool with_hits
)
1316 bool have_build_id
= false;
1318 struct nscookie nsc
;
1320 list_for_each_entry(pos
, head
, node
) {
1321 if (with_hits
&& !pos
->hit
&& !dso__is_vdso(pos
))
1323 if (pos
->has_build_id
) {
1324 have_build_id
= true;
1327 nsinfo__mountns_enter(pos
->nsinfo
, &nsc
);
1328 if (filename__read_build_id(pos
->long_name
, pos
->build_id
,
1329 sizeof(pos
->build_id
)) > 0) {
1330 have_build_id
= true;
1331 pos
->has_build_id
= true;
1333 nsinfo__mountns_exit(&nsc
);
1336 return have_build_id
;
1339 void __dsos__add(struct dsos
*dsos
, struct dso
*dso
)
1341 list_add_tail(&dso
->node
, &dsos
->head
);
1342 __dso__findlink_by_longname(&dsos
->root
, dso
, NULL
);
1344 * It is now in the linked list, grab a reference, then garbage collect
1345 * this when needing memory, by looking at LRU dso instances in the
1346 * list with atomic_read(&dso->refcnt) == 1, i.e. no references
1347 * anywhere besides the one for the list, do, under a lock for the
1348 * list: remove it from the list, then a dso__put(), that probably will
1349 * be the last and will then call dso__delete(), end of life.
1351 * That, or at the end of the 'struct machine' lifetime, when all
1352 * 'struct dso' instances will be removed from the list, in
1353 * dsos__exit(), if they have no other reference from some other data
1356 * E.g.: after processing a 'perf.data' file and storing references
1357 * to objects instantiated while processing events, we will have
1358 * references to the 'thread', 'map', 'dso' structs all from 'struct
1359 * hist_entry' instances, but we may not need anything not referenced,
1360 * so we might as well call machines__exit()/machines__delete() and
1361 * garbage collect it.
1366 void dsos__add(struct dsos
*dsos
, struct dso
*dso
)
1368 pthread_rwlock_wrlock(&dsos
->lock
);
1369 __dsos__add(dsos
, dso
);
1370 pthread_rwlock_unlock(&dsos
->lock
);
1373 struct dso
*__dsos__find(struct dsos
*dsos
, const char *name
, bool cmp_short
)
1378 list_for_each_entry(pos
, &dsos
->head
, node
)
1379 if (strcmp(pos
->short_name
, name
) == 0)
1383 return __dso__find_by_longname(&dsos
->root
, name
);
1386 struct dso
*dsos__find(struct dsos
*dsos
, const char *name
, bool cmp_short
)
1389 pthread_rwlock_rdlock(&dsos
->lock
);
1390 dso
= __dsos__find(dsos
, name
, cmp_short
);
1391 pthread_rwlock_unlock(&dsos
->lock
);
1395 struct dso
*__dsos__addnew(struct dsos
*dsos
, const char *name
)
1397 struct dso
*dso
= dso__new(name
);
1400 __dsos__add(dsos
, dso
);
1401 dso__set_basename(dso
);
1402 /* Put dso here because __dsos_add already got it */
1408 struct dso
*__dsos__findnew(struct dsos
*dsos
, const char *name
)
1410 struct dso
*dso
= __dsos__find(dsos
, name
, false);
1412 return dso
? dso
: __dsos__addnew(dsos
, name
);
1415 struct dso
*dsos__findnew(struct dsos
*dsos
, const char *name
)
1418 pthread_rwlock_wrlock(&dsos
->lock
);
1419 dso
= dso__get(__dsos__findnew(dsos
, name
));
1420 pthread_rwlock_unlock(&dsos
->lock
);
1424 size_t __dsos__fprintf_buildid(struct list_head
*head
, FILE *fp
,
1425 bool (skip
)(struct dso
*dso
, int parm
), int parm
)
1430 list_for_each_entry(pos
, head
, node
) {
1431 if (skip
&& skip(pos
, parm
))
1433 ret
+= dso__fprintf_buildid(pos
, fp
);
1434 ret
+= fprintf(fp
, " %s\n", pos
->long_name
);
1439 size_t __dsos__fprintf(struct list_head
*head
, FILE *fp
)
1444 list_for_each_entry(pos
, head
, node
) {
1446 for (i
= 0; i
< MAP__NR_TYPES
; ++i
)
1447 ret
+= dso__fprintf(pos
, i
, fp
);
1453 size_t dso__fprintf_buildid(struct dso
*dso
, FILE *fp
)
1455 char sbuild_id
[SBUILD_ID_SIZE
];
1457 build_id__sprintf(dso
->build_id
, sizeof(dso
->build_id
), sbuild_id
);
1458 return fprintf(fp
, "%s", sbuild_id
);
1461 size_t dso__fprintf(struct dso
*dso
, enum map_type type
, FILE *fp
)
1464 size_t ret
= fprintf(fp
, "dso: %s (", dso
->short_name
);
1466 if (dso
->short_name
!= dso
->long_name
)
1467 ret
+= fprintf(fp
, "%s, ", dso
->long_name
);
1468 ret
+= fprintf(fp
, "%s, %sloaded, ", map_type__name
[type
],
1469 dso__loaded(dso
, type
) ? "" : "NOT ");
1470 ret
+= dso__fprintf_buildid(dso
, fp
);
1471 ret
+= fprintf(fp
, ")\n");
1472 for (nd
= rb_first(&dso
->symbols
[type
]); nd
; nd
= rb_next(nd
)) {
1473 struct symbol
*pos
= rb_entry(nd
, struct symbol
, rb_node
);
1474 ret
+= symbol__fprintf(pos
, fp
);
1480 enum dso_type
dso__type(struct dso
*dso
, struct machine
*machine
)
1483 enum dso_type type
= DSO__TYPE_UNKNOWN
;
1485 fd
= dso__data_get_fd(dso
, machine
);
1487 type
= dso__type_fd(fd
);
1488 dso__data_put_fd(dso
);
1494 int dso__strerror_load(struct dso
*dso
, char *buf
, size_t buflen
)
1496 int idx
, errnum
= dso
->load_errno
;
1498 * This must have a same ordering as the enum dso_load_errno.
1500 static const char *dso_load__error_str
[] = {
1501 "Internal tools/perf/ library error",
1503 "Can not read build id",
1504 "Mismatching build id",
1505 "Decompression failure",
1508 BUG_ON(buflen
== 0);
1511 const char *err
= str_error_r(errnum
, buf
, buflen
);
1514 scnprintf(buf
, buflen
, "%s", err
);
1519 if (errnum
< __DSO_LOAD_ERRNO__START
|| errnum
>= __DSO_LOAD_ERRNO__END
)
1522 idx
= errnum
- __DSO_LOAD_ERRNO__START
;
1523 scnprintf(buf
, buflen
, "%s", dso_load__error_str
[idx
]);