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__FEDORA_DEBUGINFO
] = 'f',
36 [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO
] = 'u',
37 [DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO
] = 'o',
38 [DSO_BINARY_TYPE__BUILDID_DEBUGINFO
] = 'b',
39 [DSO_BINARY_TYPE__SYSTEM_PATH_DSO
] = 'd',
40 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE
] = 'K',
41 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP
] = 'm',
42 [DSO_BINARY_TYPE__GUEST_KALLSYMS
] = 'g',
43 [DSO_BINARY_TYPE__GUEST_KMODULE
] = 'G',
44 [DSO_BINARY_TYPE__GUEST_KMODULE_COMP
] = 'M',
45 [DSO_BINARY_TYPE__GUEST_VMLINUX
] = 'V',
48 if (dso
== NULL
|| dso
->symtab_type
== DSO_BINARY_TYPE__NOT_FOUND
)
50 return origin
[dso
->symtab_type
];
53 int dso__read_binary_type_filename(const struct dso
*dso
,
54 enum dso_binary_type type
,
55 char *root_dir
, char *filename
, size_t size
)
57 char build_id_hex
[SBUILD_ID_SIZE
];
62 case DSO_BINARY_TYPE__DEBUGLINK
:
64 const char *last_slash
;
65 char dso_dir
[PATH_MAX
];
66 char symfile
[PATH_MAX
];
69 len
= __symbol__join_symfs(filename
, size
, dso
->long_name
);
70 last_slash
= filename
+ len
;
71 while (last_slash
!= filename
&& *last_slash
!= '/')
74 strncpy(dso_dir
, filename
, last_slash
- filename
);
75 dso_dir
[last_slash
-filename
] = '\0';
77 if (!is_regular_file(filename
)) {
82 ret
= filename__read_debuglink(filename
, symfile
, PATH_MAX
);
86 /* Check predefined locations where debug file might reside */
88 for (i
= 0; i
< ARRAY_SIZE(debuglink_paths
); i
++) {
89 snprintf(filename
, size
,
90 debuglink_paths
[i
], dso_dir
, symfile
);
91 if (is_regular_file(filename
)) {
99 case DSO_BINARY_TYPE__BUILD_ID_CACHE
:
100 if (dso__build_id_filename(dso
, filename
, size
) == NULL
)
104 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO
:
105 len
= __symbol__join_symfs(filename
, size
, "/usr/lib/debug");
106 snprintf(filename
+ len
, size
- len
, "%s.debug", dso
->long_name
);
109 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO
:
110 len
= __symbol__join_symfs(filename
, size
, "/usr/lib/debug");
111 snprintf(filename
+ len
, size
- len
, "%s", dso
->long_name
);
114 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO
:
116 const char *last_slash
;
119 last_slash
= dso
->long_name
+ dso
->long_name_len
;
120 while (last_slash
!= dso
->long_name
&& *last_slash
!= '/')
123 len
= __symbol__join_symfs(filename
, size
, "");
124 dir_size
= last_slash
- dso
->long_name
+ 2;
125 if (dir_size
> (size
- len
)) {
129 len
+= scnprintf(filename
+ len
, dir_size
, "%s", dso
->long_name
);
130 len
+= scnprintf(filename
+ len
, size
- len
, ".debug%s",
135 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO
:
136 if (!dso
->has_build_id
) {
141 build_id__sprintf(dso
->build_id
,
142 sizeof(dso
->build_id
),
144 len
= __symbol__join_symfs(filename
, size
, "/usr/lib/debug/.build-id/");
145 snprintf(filename
+ len
, size
- len
, "%.2s/%s.debug",
146 build_id_hex
, build_id_hex
+ 2);
149 case DSO_BINARY_TYPE__VMLINUX
:
150 case DSO_BINARY_TYPE__GUEST_VMLINUX
:
151 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO
:
152 __symbol__join_symfs(filename
, size
, dso
->long_name
);
155 case DSO_BINARY_TYPE__GUEST_KMODULE
:
156 case DSO_BINARY_TYPE__GUEST_KMODULE_COMP
:
157 path__join3(filename
, size
, symbol_conf
.symfs
,
158 root_dir
, dso
->long_name
);
161 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE
:
162 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP
:
163 __symbol__join_symfs(filename
, size
, dso
->long_name
);
166 case DSO_BINARY_TYPE__KCORE
:
167 case DSO_BINARY_TYPE__GUEST_KCORE
:
168 snprintf(filename
, size
, "%s", dso
->long_name
);
172 case DSO_BINARY_TYPE__KALLSYMS
:
173 case DSO_BINARY_TYPE__GUEST_KALLSYMS
:
174 case DSO_BINARY_TYPE__JAVA_JIT
:
175 case DSO_BINARY_TYPE__NOT_FOUND
:
183 static const struct {
185 int (*decompress
)(const char *input
, int output
);
187 #ifdef HAVE_ZLIB_SUPPORT
188 { "gz", gzip_decompress_to_file
},
190 #ifdef HAVE_LZMA_SUPPORT
191 { "xz", lzma_decompress_to_file
},
196 bool is_supported_compression(const char *ext
)
200 for (i
= 0; compressions
[i
].fmt
; i
++) {
201 if (!strcmp(ext
, compressions
[i
].fmt
))
207 bool is_kernel_module(const char *pathname
, int cpumode
)
210 int mode
= cpumode
& PERF_RECORD_MISC_CPUMODE_MASK
;
212 WARN_ONCE(mode
!= cpumode
,
213 "Internal error: passing unmasked cpumode (%x) to is_kernel_module",
217 case PERF_RECORD_MISC_USER
:
218 case PERF_RECORD_MISC_HYPERVISOR
:
219 case PERF_RECORD_MISC_GUEST_USER
:
221 /* Treat PERF_RECORD_MISC_CPUMODE_UNKNOWN as kernel */
223 if (kmod_path__parse(&m
, pathname
)) {
224 pr_err("Failed to check whether %s is a kernel module or not. Assume it is.",
233 bool decompress_to_file(const char *ext
, const char *filename
, int output_fd
)
237 for (i
= 0; compressions
[i
].fmt
; i
++) {
238 if (!strcmp(ext
, compressions
[i
].fmt
))
239 return !compressions
[i
].decompress(filename
,
245 bool dso__needs_decompress(struct dso
*dso
)
247 return dso
->symtab_type
== DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP
||
248 dso
->symtab_type
== DSO_BINARY_TYPE__GUEST_KMODULE_COMP
;
251 static int decompress_kmodule(struct dso
*dso
, const char *name
, char *tmpbuf
)
256 if (!dso__needs_decompress(dso
))
259 if (kmod_path__parse_ext(&m
, dso
->long_name
))
265 fd
= mkstemp(tmpbuf
);
267 dso
->load_errno
= errno
;
271 if (!decompress_to_file(m
.ext
, name
, fd
)) {
272 dso
->load_errno
= DSO_LOAD_ERRNO__DECOMPRESSION_FAILURE
;
282 int dso__decompress_kmodule_fd(struct dso
*dso
, const char *name
)
284 char tmpbuf
[] = KMOD_DECOMP_NAME
;
287 fd
= decompress_kmodule(dso
, name
, tmpbuf
);
292 int dso__decompress_kmodule_path(struct dso
*dso
, const char *name
,
293 char *pathname
, size_t len
)
295 char tmpbuf
[] = KMOD_DECOMP_NAME
;
298 fd
= decompress_kmodule(dso
, name
, tmpbuf
);
304 strncpy(pathname
, tmpbuf
, len
);
310 * Parses kernel module specified in @path and updates
313 * @comp - true if @path contains supported compression suffix,
315 * @kmod - true if @path contains '.ko' suffix in right position,
317 * @name - if (@alloc_name && @kmod) is true, it contains strdup-ed base name
318 * of the kernel module without suffixes, otherwise strudup-ed
320 * @ext - if (@alloc_ext && @comp) is true, it contains strdup-ed string
321 * the compression suffix
323 * Returns 0 if there's no strdup error, -ENOMEM otherwise.
325 int __kmod_path__parse(struct kmod_path
*m
, const char *path
,
326 bool alloc_name
, bool alloc_ext
)
328 const char *name
= strrchr(path
, '/');
329 const char *ext
= strrchr(path
, '.');
330 bool is_simple_name
= false;
332 memset(m
, 0x0, sizeof(*m
));
333 name
= name
? name
+ 1 : path
;
336 * '.' is also a valid character for module name. For example:
337 * [aaa.bbb] is a valid module name. '[' should have higher
338 * priority than '.ko' suffix.
340 * The kernel names are from machine__mmap_name. Such
341 * name should belong to kernel itself, not kernel module.
343 if (name
[0] == '[') {
344 is_simple_name
= true;
345 if ((strncmp(name
, "[kernel.kallsyms]", 17) == 0) ||
346 (strncmp(name
, "[guest.kernel.kallsyms", 22) == 0) ||
347 (strncmp(name
, "[vdso]", 6) == 0) ||
348 (strncmp(name
, "[vsyscall]", 10) == 0)) {
355 /* No extension, just return name. */
356 if ((ext
== NULL
) || is_simple_name
) {
358 m
->name
= strdup(name
);
359 return m
->name
? 0 : -ENOMEM
;
364 if (is_supported_compression(ext
+ 1)) {
369 /* Check .ko extension only if there's enough name left. */
371 m
->kmod
= !strncmp(ext
, ".ko", 3);
375 if (asprintf(&m
->name
, "[%.*s]", (int) (ext
- name
), name
) == -1)
378 if (asprintf(&m
->name
, "%s", name
) == -1)
382 strxfrchar(m
->name
, '-', '_');
385 if (alloc_ext
&& m
->comp
) {
386 m
->ext
= strdup(ext
+ 4);
388 free((void *) m
->name
);
396 void dso__set_module_info(struct dso
*dso
, struct kmod_path
*m
,
397 struct machine
*machine
)
399 if (machine__is_host(machine
))
400 dso
->symtab_type
= DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE
;
402 dso
->symtab_type
= DSO_BINARY_TYPE__GUEST_KMODULE
;
404 /* _KMODULE_COMP should be next to _KMODULE */
405 if (m
->kmod
&& m
->comp
)
408 dso__set_short_name(dso
, strdup(m
->name
), true);
412 * Global list of open DSOs and the counter.
414 static LIST_HEAD(dso__data_open
);
415 static long dso__data_open_cnt
;
416 static pthread_mutex_t dso__data_open_lock
= PTHREAD_MUTEX_INITIALIZER
;
418 static void dso__list_add(struct dso
*dso
)
420 list_add_tail(&dso
->data
.open_entry
, &dso__data_open
);
421 dso__data_open_cnt
++;
424 static void dso__list_del(struct dso
*dso
)
426 list_del(&dso
->data
.open_entry
);
427 WARN_ONCE(dso__data_open_cnt
<= 0,
428 "DSO data fd counter out of bounds.");
429 dso__data_open_cnt
--;
432 static void close_first_dso(void);
434 static int do_open(char *name
)
437 char sbuf
[STRERR_BUFSIZE
];
440 fd
= open(name
, O_RDONLY
);
444 pr_debug("dso open failed: %s\n",
445 str_error_r(errno
, sbuf
, sizeof(sbuf
)));
446 if (!dso__data_open_cnt
|| errno
!= EMFILE
)
455 static int __open_dso(struct dso
*dso
, struct machine
*machine
)
458 char *root_dir
= (char *)"";
459 char *name
= malloc(PATH_MAX
);
465 root_dir
= machine
->root_dir
;
467 if (dso__read_binary_type_filename(dso
, dso
->binary_type
,
468 root_dir
, name
, PATH_MAX
))
471 if (!is_regular_file(name
))
474 if (dso__needs_decompress(dso
)) {
475 char newpath
[KMOD_DECOMP_LEN
];
476 size_t len
= sizeof(newpath
);
478 if (dso__decompress_kmodule_path(dso
, name
, newpath
, len
) < 0) {
479 fd
= -dso
->load_errno
;
483 strcpy(name
, newpath
);
488 if (dso__needs_decompress(dso
))
496 static void check_data_close(void);
499 * dso_close - Open DSO data file
502 * Open @dso's data file descriptor and updates
503 * list/count of open DSO objects.
505 static int open_dso(struct dso
*dso
, struct machine
*machine
)
507 int fd
= __open_dso(dso
, machine
);
512 * Check if we crossed the allowed number
513 * of opened DSOs and close one if needed.
521 static void close_data_fd(struct dso
*dso
)
523 if (dso
->data
.fd
>= 0) {
526 dso
->data
.file_size
= 0;
532 * dso_close - Close DSO data file
535 * Close @dso's data file descriptor and updates
536 * list/count of open DSO objects.
538 static void close_dso(struct dso
*dso
)
543 static void close_first_dso(void)
547 dso
= list_first_entry(&dso__data_open
, struct dso
, data
.open_entry
);
551 static rlim_t
get_fd_limit(void)
556 /* Allow half of the current open fd limit. */
557 if (getrlimit(RLIMIT_NOFILE
, &l
) == 0) {
558 if (l
.rlim_cur
== RLIM_INFINITY
)
561 limit
= l
.rlim_cur
/ 2;
563 pr_err("failed to get fd limit\n");
570 static rlim_t fd_limit
;
573 * Used only by tests/dso-data.c to reset the environment
574 * for tests. I dont expect we should change this during
577 void reset_fd_limit(void)
582 static bool may_cache_fd(void)
585 fd_limit
= get_fd_limit();
587 if (fd_limit
== RLIM_INFINITY
)
590 return fd_limit
> (rlim_t
) dso__data_open_cnt
;
594 * Check and close LRU dso if we crossed allowed limit
595 * for opened dso file descriptors. The limit is half
596 * of the RLIMIT_NOFILE files opened.
598 static void check_data_close(void)
600 bool cache_fd
= may_cache_fd();
607 * dso__data_close - Close DSO data file
610 * External interface to close @dso's data file descriptor.
612 void dso__data_close(struct dso
*dso
)
614 pthread_mutex_lock(&dso__data_open_lock
);
616 pthread_mutex_unlock(&dso__data_open_lock
);
619 static void try_to_open_dso(struct dso
*dso
, struct machine
*machine
)
621 enum dso_binary_type binary_type_data
[] = {
622 DSO_BINARY_TYPE__BUILD_ID_CACHE
,
623 DSO_BINARY_TYPE__SYSTEM_PATH_DSO
,
624 DSO_BINARY_TYPE__NOT_FOUND
,
628 if (dso
->data
.fd
>= 0)
631 if (dso
->binary_type
!= DSO_BINARY_TYPE__NOT_FOUND
) {
632 dso
->data
.fd
= open_dso(dso
, machine
);
637 dso
->binary_type
= binary_type_data
[i
++];
639 dso
->data
.fd
= open_dso(dso
, machine
);
640 if (dso
->data
.fd
>= 0)
643 } while (dso
->binary_type
!= DSO_BINARY_TYPE__NOT_FOUND
);
645 if (dso
->data
.fd
>= 0)
646 dso
->data
.status
= DSO_DATA_STATUS_OK
;
648 dso
->data
.status
= DSO_DATA_STATUS_ERROR
;
652 * dso__data_get_fd - Get dso's data file descriptor
654 * @machine: machine object
656 * External interface to find dso's file, open it and
657 * returns file descriptor. It should be paired with
658 * dso__data_put_fd() if it returns non-negative value.
660 int dso__data_get_fd(struct dso
*dso
, struct machine
*machine
)
662 if (dso
->data
.status
== DSO_DATA_STATUS_ERROR
)
665 if (pthread_mutex_lock(&dso__data_open_lock
) < 0)
668 try_to_open_dso(dso
, machine
);
670 if (dso
->data
.fd
< 0)
671 pthread_mutex_unlock(&dso__data_open_lock
);
676 void dso__data_put_fd(struct dso
*dso __maybe_unused
)
678 pthread_mutex_unlock(&dso__data_open_lock
);
681 bool dso__data_status_seen(struct dso
*dso
, enum dso_data_status_seen by
)
685 if (dso
->data
.status_seen
& flag
)
688 dso
->data
.status_seen
|= flag
;
694 dso_cache__free(struct dso
*dso
)
696 struct rb_root
*root
= &dso
->data
.cache
;
697 struct rb_node
*next
= rb_first(root
);
699 pthread_mutex_lock(&dso
->lock
);
701 struct dso_cache
*cache
;
703 cache
= rb_entry(next
, struct dso_cache
, rb_node
);
704 next
= rb_next(&cache
->rb_node
);
705 rb_erase(&cache
->rb_node
, root
);
708 pthread_mutex_unlock(&dso
->lock
);
711 static struct dso_cache
*dso_cache__find(struct dso
*dso
, u64 offset
)
713 const struct rb_root
*root
= &dso
->data
.cache
;
714 struct rb_node
* const *p
= &root
->rb_node
;
715 const struct rb_node
*parent
= NULL
;
716 struct dso_cache
*cache
;
722 cache
= rb_entry(parent
, struct dso_cache
, rb_node
);
723 end
= cache
->offset
+ DSO__DATA_CACHE_SIZE
;
725 if (offset
< cache
->offset
)
727 else if (offset
>= end
)
736 static struct dso_cache
*
737 dso_cache__insert(struct dso
*dso
, struct dso_cache
*new)
739 struct rb_root
*root
= &dso
->data
.cache
;
740 struct rb_node
**p
= &root
->rb_node
;
741 struct rb_node
*parent
= NULL
;
742 struct dso_cache
*cache
;
743 u64 offset
= new->offset
;
745 pthread_mutex_lock(&dso
->lock
);
750 cache
= rb_entry(parent
, struct dso_cache
, rb_node
);
751 end
= cache
->offset
+ DSO__DATA_CACHE_SIZE
;
753 if (offset
< cache
->offset
)
755 else if (offset
>= end
)
761 rb_link_node(&new->rb_node
, parent
, p
);
762 rb_insert_color(&new->rb_node
, root
);
766 pthread_mutex_unlock(&dso
->lock
);
771 dso_cache__memcpy(struct dso_cache
*cache
, u64 offset
,
774 u64 cache_offset
= offset
- cache
->offset
;
775 u64 cache_size
= min(cache
->size
- cache_offset
, size
);
777 memcpy(data
, cache
->data
+ cache_offset
, cache_size
);
782 dso_cache__read(struct dso
*dso
, struct machine
*machine
,
783 u64 offset
, u8
*data
, ssize_t size
)
785 struct dso_cache
*cache
;
786 struct dso_cache
*old
;
792 cache
= zalloc(sizeof(*cache
) + DSO__DATA_CACHE_SIZE
);
796 pthread_mutex_lock(&dso__data_open_lock
);
799 * dso->data.fd might be closed if other thread opened another
800 * file (dso) due to open file limit (RLIMIT_NOFILE).
802 try_to_open_dso(dso
, machine
);
804 if (dso
->data
.fd
< 0) {
806 dso
->data
.status
= DSO_DATA_STATUS_ERROR
;
810 cache_offset
= offset
& DSO__DATA_CACHE_MASK
;
812 ret
= pread(dso
->data
.fd
, cache
->data
, DSO__DATA_CACHE_SIZE
, cache_offset
);
816 cache
->offset
= cache_offset
;
820 pthread_mutex_unlock(&dso__data_open_lock
);
823 old
= dso_cache__insert(dso
, cache
);
825 /* we lose the race */
830 ret
= dso_cache__memcpy(cache
, offset
, data
, size
);
839 static ssize_t
dso_cache_read(struct dso
*dso
, struct machine
*machine
,
840 u64 offset
, u8
*data
, ssize_t size
)
842 struct dso_cache
*cache
;
844 cache
= dso_cache__find(dso
, offset
);
846 return dso_cache__memcpy(cache
, offset
, data
, size
);
848 return dso_cache__read(dso
, machine
, offset
, data
, size
);
852 * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
853 * in the rb_tree. Any read to already cached data is served
856 static ssize_t
cached_read(struct dso
*dso
, struct machine
*machine
,
857 u64 offset
, u8
*data
, ssize_t size
)
865 ret
= dso_cache_read(dso
, machine
, offset
, p
, size
);
869 /* Reached EOF, return what we have. */
885 static int data_file_size(struct dso
*dso
, struct machine
*machine
)
889 char sbuf
[STRERR_BUFSIZE
];
891 if (dso
->data
.file_size
)
894 if (dso
->data
.status
== DSO_DATA_STATUS_ERROR
)
897 pthread_mutex_lock(&dso__data_open_lock
);
900 * dso->data.fd might be closed if other thread opened another
901 * file (dso) due to open file limit (RLIMIT_NOFILE).
903 try_to_open_dso(dso
, machine
);
905 if (dso
->data
.fd
< 0) {
907 dso
->data
.status
= DSO_DATA_STATUS_ERROR
;
911 if (fstat(dso
->data
.fd
, &st
) < 0) {
913 pr_err("dso cache fstat failed: %s\n",
914 str_error_r(errno
, sbuf
, sizeof(sbuf
)));
915 dso
->data
.status
= DSO_DATA_STATUS_ERROR
;
918 dso
->data
.file_size
= st
.st_size
;
921 pthread_mutex_unlock(&dso__data_open_lock
);
926 * dso__data_size - Return dso data size
928 * @machine: machine object
930 * Return: dso data size
932 off_t
dso__data_size(struct dso
*dso
, struct machine
*machine
)
934 if (data_file_size(dso
, machine
))
937 /* For now just estimate dso data size is close to file size */
938 return dso
->data
.file_size
;
941 static ssize_t
data_read_offset(struct dso
*dso
, struct machine
*machine
,
942 u64 offset
, u8
*data
, ssize_t size
)
944 if (data_file_size(dso
, machine
))
947 /* Check the offset sanity. */
948 if (offset
> dso
->data
.file_size
)
951 if (offset
+ size
< offset
)
954 return cached_read(dso
, machine
, offset
, data
, size
);
958 * dso__data_read_offset - Read data from dso file offset
960 * @machine: machine object
961 * @offset: file offset
962 * @data: buffer to store data
963 * @size: size of the @data buffer
965 * External interface to read data from dso file offset. Open
966 * dso data file and use cached_read to get the data.
968 ssize_t
dso__data_read_offset(struct dso
*dso
, struct machine
*machine
,
969 u64 offset
, u8
*data
, ssize_t size
)
971 if (dso
->data
.status
== DSO_DATA_STATUS_ERROR
)
974 return data_read_offset(dso
, machine
, offset
, data
, size
);
978 * dso__data_read_addr - Read data from dso address
980 * @machine: machine object
981 * @add: virtual memory address
982 * @data: buffer to store data
983 * @size: size of the @data buffer
985 * External interface to read data from dso address.
987 ssize_t
dso__data_read_addr(struct dso
*dso
, struct map
*map
,
988 struct machine
*machine
, u64 addr
,
989 u8
*data
, ssize_t size
)
991 u64 offset
= map
->map_ip(map
, addr
);
992 return dso__data_read_offset(dso
, machine
, offset
, data
, size
);
995 struct map
*dso__new_map(const char *name
)
997 struct map
*map
= NULL
;
998 struct dso
*dso
= dso__new(name
);
1001 map
= map__new2(0, dso
, MAP__FUNCTION
);
1006 struct dso
*machine__findnew_kernel(struct machine
*machine
, const char *name
,
1007 const char *short_name
, int dso_type
)
1010 * The kernel dso could be created by build_id processing.
1012 struct dso
*dso
= machine__findnew_dso(machine
, name
);
1015 * We need to run this in all cases, since during the build_id
1016 * processing we had no idea this was the kernel dso.
1019 dso__set_short_name(dso
, short_name
, false);
1020 dso
->kernel
= dso_type
;
1027 * Find a matching entry and/or link current entry to RB tree.
1028 * Either one of the dso or name parameter must be non-NULL or the
1029 * function will not work.
1031 static struct dso
*__dso__findlink_by_longname(struct rb_root
*root
,
1032 struct dso
*dso
, const char *name
)
1034 struct rb_node
**p
= &root
->rb_node
;
1035 struct rb_node
*parent
= NULL
;
1038 name
= dso
->long_name
;
1040 * Find node with the matching name
1043 struct dso
*this = rb_entry(*p
, struct dso
, rb_node
);
1044 int rc
= strcmp(name
, this->long_name
);
1049 * In case the new DSO is a duplicate of an existing
1050 * one, print a one-time warning & put the new entry
1051 * at the end of the list of duplicates.
1053 if (!dso
|| (dso
== this))
1054 return this; /* Find matching dso */
1056 * The core kernel DSOs may have duplicated long name.
1057 * In this case, the short name should be different.
1058 * Comparing the short names to differentiate the DSOs.
1060 rc
= strcmp(dso
->short_name
, this->short_name
);
1062 pr_err("Duplicated dso name: %s\n", name
);
1067 p
= &parent
->rb_left
;
1069 p
= &parent
->rb_right
;
1072 /* Add new node and rebalance tree */
1073 rb_link_node(&dso
->rb_node
, parent
, p
);
1074 rb_insert_color(&dso
->rb_node
, root
);
1080 static inline struct dso
*__dso__find_by_longname(struct rb_root
*root
,
1083 return __dso__findlink_by_longname(root
, NULL
, name
);
1086 void dso__set_long_name(struct dso
*dso
, const char *name
, bool name_allocated
)
1088 struct rb_root
*root
= dso
->root
;
1093 if (dso
->long_name_allocated
)
1094 free((char *)dso
->long_name
);
1097 rb_erase(&dso
->rb_node
, root
);
1099 * __dso__findlink_by_longname() isn't guaranteed to add it
1100 * back, so a clean removal is required here.
1102 RB_CLEAR_NODE(&dso
->rb_node
);
1106 dso
->long_name
= name
;
1107 dso
->long_name_len
= strlen(name
);
1108 dso
->long_name_allocated
= name_allocated
;
1111 __dso__findlink_by_longname(root
, dso
, NULL
);
1114 void dso__set_short_name(struct dso
*dso
, const char *name
, bool name_allocated
)
1119 if (dso
->short_name_allocated
)
1120 free((char *)dso
->short_name
);
1122 dso
->short_name
= name
;
1123 dso
->short_name_len
= strlen(name
);
1124 dso
->short_name_allocated
= name_allocated
;
1127 static void dso__set_basename(struct dso
*dso
)
1130 * basename() may modify path buffer, so we must pass
1133 char *base
, *lname
= strdup(dso
->long_name
);
1139 * basename() may return a pointer to internal
1140 * storage which is reused in subsequent calls
1141 * so copy the result.
1143 base
= strdup(basename(lname
));
1150 dso__set_short_name(dso
, base
, true);
1153 int dso__name_len(const struct dso
*dso
)
1156 return strlen("[unknown]");
1158 return dso
->long_name_len
;
1160 return dso
->short_name_len
;
1163 bool dso__loaded(const struct dso
*dso
, enum map_type type
)
1165 return dso
->loaded
& (1 << type
);
1168 bool dso__sorted_by_name(const struct dso
*dso
, enum map_type type
)
1170 return dso
->sorted_by_name
& (1 << type
);
1173 void dso__set_sorted_by_name(struct dso
*dso
, enum map_type type
)
1175 dso
->sorted_by_name
|= (1 << type
);
1178 struct dso
*dso__new(const char *name
)
1180 struct dso
*dso
= calloc(1, sizeof(*dso
) + strlen(name
) + 1);
1184 strcpy(dso
->name
, name
);
1185 dso__set_long_name(dso
, dso
->name
, false);
1186 dso__set_short_name(dso
, dso
->name
, false);
1187 for (i
= 0; i
< MAP__NR_TYPES
; ++i
)
1188 dso
->symbols
[i
] = dso
->symbol_names
[i
] = RB_ROOT
;
1189 dso
->data
.cache
= RB_ROOT
;
1191 dso
->data
.status
= DSO_DATA_STATUS_UNKNOWN
;
1192 dso
->symtab_type
= DSO_BINARY_TYPE__NOT_FOUND
;
1193 dso
->binary_type
= DSO_BINARY_TYPE__NOT_FOUND
;
1194 dso
->is_64_bit
= (sizeof(void *) == 8);
1197 dso
->sorted_by_name
= 0;
1198 dso
->has_build_id
= 0;
1199 dso
->has_srcline
= 1;
1201 dso
->kernel
= DSO_TYPE_USER
;
1202 dso
->needs_swap
= DSO_SWAP__UNSET
;
1203 RB_CLEAR_NODE(&dso
->rb_node
);
1205 INIT_LIST_HEAD(&dso
->node
);
1206 INIT_LIST_HEAD(&dso
->data
.open_entry
);
1207 pthread_mutex_init(&dso
->lock
, NULL
);
1208 refcount_set(&dso
->refcnt
, 1);
1214 void dso__delete(struct dso
*dso
)
1218 if (!RB_EMPTY_NODE(&dso
->rb_node
))
1219 pr_err("DSO %s is still in rbtree when being deleted!\n",
1221 for (i
= 0; i
< MAP__NR_TYPES
; ++i
)
1222 symbols__delete(&dso
->symbols
[i
]);
1224 if (dso
->short_name_allocated
) {
1225 zfree((char **)&dso
->short_name
);
1226 dso
->short_name_allocated
= false;
1229 if (dso
->long_name_allocated
) {
1230 zfree((char **)&dso
->long_name
);
1231 dso
->long_name_allocated
= false;
1234 dso__data_close(dso
);
1235 auxtrace_cache__free(dso
->auxtrace_cache
);
1236 dso_cache__free(dso
);
1238 zfree(&dso
->symsrc_filename
);
1239 pthread_mutex_destroy(&dso
->lock
);
1243 struct dso
*dso__get(struct dso
*dso
)
1246 refcount_inc(&dso
->refcnt
);
1250 void dso__put(struct dso
*dso
)
1252 if (dso
&& refcount_dec_and_test(&dso
->refcnt
))
1256 void dso__set_build_id(struct dso
*dso
, void *build_id
)
1258 memcpy(dso
->build_id
, build_id
, sizeof(dso
->build_id
));
1259 dso
->has_build_id
= 1;
1262 bool dso__build_id_equal(const struct dso
*dso
, u8
*build_id
)
1264 return memcmp(dso
->build_id
, build_id
, sizeof(dso
->build_id
)) == 0;
1267 void dso__read_running_kernel_build_id(struct dso
*dso
, struct machine
*machine
)
1269 char path
[PATH_MAX
];
1271 if (machine__is_default_guest(machine
))
1273 sprintf(path
, "%s/sys/kernel/notes", machine
->root_dir
);
1274 if (sysfs__read_build_id(path
, dso
->build_id
,
1275 sizeof(dso
->build_id
)) == 0)
1276 dso
->has_build_id
= true;
1279 int dso__kernel_module_get_build_id(struct dso
*dso
,
1280 const char *root_dir
)
1282 char filename
[PATH_MAX
];
1284 * kernel module short names are of the form "[module]" and
1285 * we need just "module" here.
1287 const char *name
= dso
->short_name
+ 1;
1289 snprintf(filename
, sizeof(filename
),
1290 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1291 root_dir
, (int)strlen(name
) - 1, name
);
1293 if (sysfs__read_build_id(filename
, dso
->build_id
,
1294 sizeof(dso
->build_id
)) == 0)
1295 dso
->has_build_id
= true;
1300 bool __dsos__read_build_ids(struct list_head
*head
, bool with_hits
)
1302 bool have_build_id
= false;
1305 list_for_each_entry(pos
, head
, node
) {
1306 if (with_hits
&& !pos
->hit
&& !dso__is_vdso(pos
))
1308 if (pos
->has_build_id
) {
1309 have_build_id
= true;
1312 if (filename__read_build_id(pos
->long_name
, pos
->build_id
,
1313 sizeof(pos
->build_id
)) > 0) {
1314 have_build_id
= true;
1315 pos
->has_build_id
= true;
1319 return have_build_id
;
1322 void __dsos__add(struct dsos
*dsos
, struct dso
*dso
)
1324 list_add_tail(&dso
->node
, &dsos
->head
);
1325 __dso__findlink_by_longname(&dsos
->root
, dso
, NULL
);
1327 * It is now in the linked list, grab a reference, then garbage collect
1328 * this when needing memory, by looking at LRU dso instances in the
1329 * list with atomic_read(&dso->refcnt) == 1, i.e. no references
1330 * anywhere besides the one for the list, do, under a lock for the
1331 * list: remove it from the list, then a dso__put(), that probably will
1332 * be the last and will then call dso__delete(), end of life.
1334 * That, or at the end of the 'struct machine' lifetime, when all
1335 * 'struct dso' instances will be removed from the list, in
1336 * dsos__exit(), if they have no other reference from some other data
1339 * E.g.: after processing a 'perf.data' file and storing references
1340 * to objects instantiated while processing events, we will have
1341 * references to the 'thread', 'map', 'dso' structs all from 'struct
1342 * hist_entry' instances, but we may not need anything not referenced,
1343 * so we might as well call machines__exit()/machines__delete() and
1344 * garbage collect it.
1349 void dsos__add(struct dsos
*dsos
, struct dso
*dso
)
1351 pthread_rwlock_wrlock(&dsos
->lock
);
1352 __dsos__add(dsos
, dso
);
1353 pthread_rwlock_unlock(&dsos
->lock
);
1356 struct dso
*__dsos__find(struct dsos
*dsos
, const char *name
, bool cmp_short
)
1361 list_for_each_entry(pos
, &dsos
->head
, node
)
1362 if (strcmp(pos
->short_name
, name
) == 0)
1366 return __dso__find_by_longname(&dsos
->root
, name
);
1369 struct dso
*dsos__find(struct dsos
*dsos
, const char *name
, bool cmp_short
)
1372 pthread_rwlock_rdlock(&dsos
->lock
);
1373 dso
= __dsos__find(dsos
, name
, cmp_short
);
1374 pthread_rwlock_unlock(&dsos
->lock
);
1378 struct dso
*__dsos__addnew(struct dsos
*dsos
, const char *name
)
1380 struct dso
*dso
= dso__new(name
);
1383 __dsos__add(dsos
, dso
);
1384 dso__set_basename(dso
);
1385 /* Put dso here because __dsos_add already got it */
1391 struct dso
*__dsos__findnew(struct dsos
*dsos
, const char *name
)
1393 struct dso
*dso
= __dsos__find(dsos
, name
, false);
1395 return dso
? dso
: __dsos__addnew(dsos
, name
);
1398 struct dso
*dsos__findnew(struct dsos
*dsos
, const char *name
)
1401 pthread_rwlock_wrlock(&dsos
->lock
);
1402 dso
= dso__get(__dsos__findnew(dsos
, name
));
1403 pthread_rwlock_unlock(&dsos
->lock
);
1407 size_t __dsos__fprintf_buildid(struct list_head
*head
, FILE *fp
,
1408 bool (skip
)(struct dso
*dso
, int parm
), int parm
)
1413 list_for_each_entry(pos
, head
, node
) {
1414 if (skip
&& skip(pos
, parm
))
1416 ret
+= dso__fprintf_buildid(pos
, fp
);
1417 ret
+= fprintf(fp
, " %s\n", pos
->long_name
);
1422 size_t __dsos__fprintf(struct list_head
*head
, FILE *fp
)
1427 list_for_each_entry(pos
, head
, node
) {
1429 for (i
= 0; i
< MAP__NR_TYPES
; ++i
)
1430 ret
+= dso__fprintf(pos
, i
, fp
);
1436 size_t dso__fprintf_buildid(struct dso
*dso
, FILE *fp
)
1438 char sbuild_id
[SBUILD_ID_SIZE
];
1440 build_id__sprintf(dso
->build_id
, sizeof(dso
->build_id
), sbuild_id
);
1441 return fprintf(fp
, "%s", sbuild_id
);
1444 size_t dso__fprintf(struct dso
*dso
, enum map_type type
, FILE *fp
)
1447 size_t ret
= fprintf(fp
, "dso: %s (", dso
->short_name
);
1449 if (dso
->short_name
!= dso
->long_name
)
1450 ret
+= fprintf(fp
, "%s, ", dso
->long_name
);
1451 ret
+= fprintf(fp
, "%s, %sloaded, ", map_type__name
[type
],
1452 dso__loaded(dso
, type
) ? "" : "NOT ");
1453 ret
+= dso__fprintf_buildid(dso
, fp
);
1454 ret
+= fprintf(fp
, ")\n");
1455 for (nd
= rb_first(&dso
->symbols
[type
]); nd
; nd
= rb_next(nd
)) {
1456 struct symbol
*pos
= rb_entry(nd
, struct symbol
, rb_node
);
1457 ret
+= symbol__fprintf(pos
, fp
);
1463 enum dso_type
dso__type(struct dso
*dso
, struct machine
*machine
)
1466 enum dso_type type
= DSO__TYPE_UNKNOWN
;
1468 fd
= dso__data_get_fd(dso
, machine
);
1470 type
= dso__type_fd(fd
);
1471 dso__data_put_fd(dso
);
1477 int dso__strerror_load(struct dso
*dso
, char *buf
, size_t buflen
)
1479 int idx
, errnum
= dso
->load_errno
;
1481 * This must have a same ordering as the enum dso_load_errno.
1483 static const char *dso_load__error_str
[] = {
1484 "Internal tools/perf/ library error",
1486 "Can not read build id",
1487 "Mismatching build id",
1488 "Decompression failure",
1491 BUG_ON(buflen
== 0);
1494 const char *err
= str_error_r(errnum
, buf
, buflen
);
1497 scnprintf(buf
, buflen
, "%s", err
);
1502 if (errnum
< __DSO_LOAD_ERRNO__START
|| errnum
>= __DSO_LOAD_ERRNO__END
)
1505 idx
= errnum
- __DSO_LOAD_ERRNO__START
;
1506 scnprintf(buf
, buflen
, "%s", dso_load__error_str
[idx
]);