3 #include <sys/resource.h>
11 char dso__symtab_origin(const struct dso
*dso
)
13 static const char origin
[] = {
14 [DSO_BINARY_TYPE__KALLSYMS
] = 'k',
15 [DSO_BINARY_TYPE__VMLINUX
] = 'v',
16 [DSO_BINARY_TYPE__JAVA_JIT
] = 'j',
17 [DSO_BINARY_TYPE__DEBUGLINK
] = 'l',
18 [DSO_BINARY_TYPE__BUILD_ID_CACHE
] = 'B',
19 [DSO_BINARY_TYPE__FEDORA_DEBUGINFO
] = 'f',
20 [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO
] = 'u',
21 [DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO
] = 'o',
22 [DSO_BINARY_TYPE__BUILDID_DEBUGINFO
] = 'b',
23 [DSO_BINARY_TYPE__SYSTEM_PATH_DSO
] = 'd',
24 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE
] = 'K',
25 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP
] = 'm',
26 [DSO_BINARY_TYPE__GUEST_KALLSYMS
] = 'g',
27 [DSO_BINARY_TYPE__GUEST_KMODULE
] = 'G',
28 [DSO_BINARY_TYPE__GUEST_KMODULE_COMP
] = 'M',
29 [DSO_BINARY_TYPE__GUEST_VMLINUX
] = 'V',
32 if (dso
== NULL
|| dso
->symtab_type
== DSO_BINARY_TYPE__NOT_FOUND
)
34 return origin
[dso
->symtab_type
];
37 int dso__read_binary_type_filename(const struct dso
*dso
,
38 enum dso_binary_type type
,
39 char *root_dir
, char *filename
, size_t size
)
41 char build_id_hex
[BUILD_ID_SIZE
* 2 + 1];
46 case DSO_BINARY_TYPE__DEBUGLINK
: {
49 len
= __symbol__join_symfs(filename
, size
, dso
->long_name
);
50 debuglink
= filename
+ len
;
51 while (debuglink
!= filename
&& *debuglink
!= '/')
53 if (*debuglink
== '/')
55 ret
= filename__read_debuglink(filename
, debuglink
,
56 size
- (debuglink
- filename
));
59 case DSO_BINARY_TYPE__BUILD_ID_CACHE
:
60 /* skip the locally configured cache if a symfs is given */
61 if (symbol_conf
.symfs
[0] ||
62 (dso__build_id_filename(dso
, filename
, size
) == NULL
))
66 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO
:
67 len
= __symbol__join_symfs(filename
, size
, "/usr/lib/debug");
68 snprintf(filename
+ len
, size
- len
, "%s.debug", dso
->long_name
);
71 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO
:
72 len
= __symbol__join_symfs(filename
, size
, "/usr/lib/debug");
73 snprintf(filename
+ len
, size
- len
, "%s", dso
->long_name
);
76 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO
:
78 const char *last_slash
;
81 last_slash
= dso
->long_name
+ dso
->long_name_len
;
82 while (last_slash
!= dso
->long_name
&& *last_slash
!= '/')
85 len
= __symbol__join_symfs(filename
, size
, "");
86 dir_size
= last_slash
- dso
->long_name
+ 2;
87 if (dir_size
> (size
- len
)) {
91 len
+= scnprintf(filename
+ len
, dir_size
, "%s", dso
->long_name
);
92 len
+= scnprintf(filename
+ len
, size
- len
, ".debug%s",
97 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO
:
98 if (!dso
->has_build_id
) {
103 build_id__sprintf(dso
->build_id
,
104 sizeof(dso
->build_id
),
106 len
= __symbol__join_symfs(filename
, size
, "/usr/lib/debug/.build-id/");
107 snprintf(filename
+ len
, size
- len
, "%.2s/%s.debug",
108 build_id_hex
, build_id_hex
+ 2);
111 case DSO_BINARY_TYPE__VMLINUX
:
112 case DSO_BINARY_TYPE__GUEST_VMLINUX
:
113 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO
:
114 __symbol__join_symfs(filename
, size
, dso
->long_name
);
117 case DSO_BINARY_TYPE__GUEST_KMODULE
:
118 case DSO_BINARY_TYPE__GUEST_KMODULE_COMP
:
119 path__join3(filename
, size
, symbol_conf
.symfs
,
120 root_dir
, dso
->long_name
);
123 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE
:
124 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP
:
125 __symbol__join_symfs(filename
, size
, dso
->long_name
);
128 case DSO_BINARY_TYPE__KCORE
:
129 case DSO_BINARY_TYPE__GUEST_KCORE
:
130 snprintf(filename
, size
, "%s", dso
->long_name
);
134 case DSO_BINARY_TYPE__KALLSYMS
:
135 case DSO_BINARY_TYPE__GUEST_KALLSYMS
:
136 case DSO_BINARY_TYPE__JAVA_JIT
:
137 case DSO_BINARY_TYPE__NOT_FOUND
:
145 static const struct {
147 int (*decompress
)(const char *input
, int output
);
149 #ifdef HAVE_ZLIB_SUPPORT
150 { "gz", gzip_decompress_to_file
},
152 #ifdef HAVE_LZMA_SUPPORT
153 { "xz", lzma_decompress_to_file
},
158 bool is_supported_compression(const char *ext
)
162 for (i
= 0; compressions
[i
].fmt
; i
++) {
163 if (!strcmp(ext
, compressions
[i
].fmt
))
169 bool is_kernel_module(const char *pathname
, int cpumode
)
172 int mode
= cpumode
& PERF_RECORD_MISC_CPUMODE_MASK
;
174 WARN_ONCE(mode
!= cpumode
,
175 "Internal error: passing unmasked cpumode (%x) to is_kernel_module",
179 case PERF_RECORD_MISC_USER
:
180 case PERF_RECORD_MISC_HYPERVISOR
:
181 case PERF_RECORD_MISC_GUEST_USER
:
183 /* Treat PERF_RECORD_MISC_CPUMODE_UNKNOWN as kernel */
185 if (kmod_path__parse(&m
, pathname
)) {
186 pr_err("Failed to check whether %s is a kernel module or not. Assume it is.",
195 bool decompress_to_file(const char *ext
, const char *filename
, int output_fd
)
199 for (i
= 0; compressions
[i
].fmt
; i
++) {
200 if (!strcmp(ext
, compressions
[i
].fmt
))
201 return !compressions
[i
].decompress(filename
,
207 bool dso__needs_decompress(struct dso
*dso
)
209 return dso
->symtab_type
== DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP
||
210 dso
->symtab_type
== DSO_BINARY_TYPE__GUEST_KMODULE_COMP
;
214 * Parses kernel module specified in @path and updates
217 * @comp - true if @path contains supported compression suffix,
219 * @kmod - true if @path contains '.ko' suffix in right position,
221 * @name - if (@alloc_name && @kmod) is true, it contains strdup-ed base name
222 * of the kernel module without suffixes, otherwise strudup-ed
224 * @ext - if (@alloc_ext && @comp) is true, it contains strdup-ed string
225 * the compression suffix
227 * Returns 0 if there's no strdup error, -ENOMEM otherwise.
229 int __kmod_path__parse(struct kmod_path
*m
, const char *path
,
230 bool alloc_name
, bool alloc_ext
)
232 const char *name
= strrchr(path
, '/');
233 const char *ext
= strrchr(path
, '.');
234 bool is_simple_name
= false;
236 memset(m
, 0x0, sizeof(*m
));
237 name
= name
? name
+ 1 : path
;
240 * '.' is also a valid character for module name. For example:
241 * [aaa.bbb] is a valid module name. '[' should have higher
242 * priority than '.ko' suffix.
244 * The kernel names are from machine__mmap_name. Such
245 * name should belong to kernel itself, not kernel module.
247 if (name
[0] == '[') {
248 is_simple_name
= true;
249 if ((strncmp(name
, "[kernel.kallsyms]", 17) == 0) ||
250 (strncmp(name
, "[guest.kernel.kallsyms", 22) == 0) ||
251 (strncmp(name
, "[vdso]", 6) == 0) ||
252 (strncmp(name
, "[vsyscall]", 10) == 0)) {
259 /* No extension, just return name. */
260 if ((ext
== NULL
) || is_simple_name
) {
262 m
->name
= strdup(name
);
263 return m
->name
? 0 : -ENOMEM
;
268 if (is_supported_compression(ext
+ 1)) {
273 /* Check .ko extension only if there's enough name left. */
275 m
->kmod
= !strncmp(ext
, ".ko", 3);
279 if (asprintf(&m
->name
, "[%.*s]", (int) (ext
- name
), name
) == -1)
282 if (asprintf(&m
->name
, "%s", name
) == -1)
286 strxfrchar(m
->name
, '-', '_');
289 if (alloc_ext
&& m
->comp
) {
290 m
->ext
= strdup(ext
+ 4);
292 free((void *) m
->name
);
301 * Global list of open DSOs and the counter.
303 static LIST_HEAD(dso__data_open
);
304 static long dso__data_open_cnt
;
305 static pthread_mutex_t dso__data_open_lock
= PTHREAD_MUTEX_INITIALIZER
;
307 static void dso__list_add(struct dso
*dso
)
309 list_add_tail(&dso
->data
.open_entry
, &dso__data_open
);
310 dso__data_open_cnt
++;
313 static void dso__list_del(struct dso
*dso
)
315 list_del(&dso
->data
.open_entry
);
316 WARN_ONCE(dso__data_open_cnt
<= 0,
317 "DSO data fd counter out of bounds.");
318 dso__data_open_cnt
--;
321 static void close_first_dso(void);
323 static int do_open(char *name
)
326 char sbuf
[STRERR_BUFSIZE
];
329 fd
= open(name
, O_RDONLY
);
333 pr_debug("dso open failed: %s\n",
334 strerror_r(errno
, sbuf
, sizeof(sbuf
)));
335 if (!dso__data_open_cnt
|| errno
!= EMFILE
)
344 static int __open_dso(struct dso
*dso
, struct machine
*machine
)
347 char *root_dir
= (char *)"";
348 char *name
= malloc(PATH_MAX
);
354 root_dir
= machine
->root_dir
;
356 if (dso__read_binary_type_filename(dso
, dso
->binary_type
,
357 root_dir
, name
, PATH_MAX
)) {
367 static void check_data_close(void);
370 * dso_close - Open DSO data file
373 * Open @dso's data file descriptor and updates
374 * list/count of open DSO objects.
376 static int open_dso(struct dso
*dso
, struct machine
*machine
)
378 int fd
= __open_dso(dso
, machine
);
383 * Check if we crossed the allowed number
384 * of opened DSOs and close one if needed.
392 static void close_data_fd(struct dso
*dso
)
394 if (dso
->data
.fd
>= 0) {
397 dso
->data
.file_size
= 0;
403 * dso_close - Close DSO data file
406 * Close @dso's data file descriptor and updates
407 * list/count of open DSO objects.
409 static void close_dso(struct dso
*dso
)
414 static void close_first_dso(void)
418 dso
= list_first_entry(&dso__data_open
, struct dso
, data
.open_entry
);
422 static rlim_t
get_fd_limit(void)
427 /* Allow half of the current open fd limit. */
428 if (getrlimit(RLIMIT_NOFILE
, &l
) == 0) {
429 if (l
.rlim_cur
== RLIM_INFINITY
)
432 limit
= l
.rlim_cur
/ 2;
434 pr_err("failed to get fd limit\n");
441 static bool may_cache_fd(void)
446 limit
= get_fd_limit();
448 if (limit
== RLIM_INFINITY
)
451 return limit
> (rlim_t
) dso__data_open_cnt
;
455 * Check and close LRU dso if we crossed allowed limit
456 * for opened dso file descriptors. The limit is half
457 * of the RLIMIT_NOFILE files opened.
459 static void check_data_close(void)
461 bool cache_fd
= may_cache_fd();
468 * dso__data_close - Close DSO data file
471 * External interface to close @dso's data file descriptor.
473 void dso__data_close(struct dso
*dso
)
475 pthread_mutex_lock(&dso__data_open_lock
);
477 pthread_mutex_unlock(&dso__data_open_lock
);
480 static void try_to_open_dso(struct dso
*dso
, struct machine
*machine
)
482 enum dso_binary_type binary_type_data
[] = {
483 DSO_BINARY_TYPE__BUILD_ID_CACHE
,
484 DSO_BINARY_TYPE__SYSTEM_PATH_DSO
,
485 DSO_BINARY_TYPE__NOT_FOUND
,
489 if (dso
->data
.fd
>= 0)
492 if (dso
->binary_type
!= DSO_BINARY_TYPE__NOT_FOUND
) {
493 dso
->data
.fd
= open_dso(dso
, machine
);
498 dso
->binary_type
= binary_type_data
[i
++];
500 dso
->data
.fd
= open_dso(dso
, machine
);
501 if (dso
->data
.fd
>= 0)
504 } while (dso
->binary_type
!= DSO_BINARY_TYPE__NOT_FOUND
);
506 if (dso
->data
.fd
>= 0)
507 dso
->data
.status
= DSO_DATA_STATUS_OK
;
509 dso
->data
.status
= DSO_DATA_STATUS_ERROR
;
513 * dso__data_get_fd - Get dso's data file descriptor
515 * @machine: machine object
517 * External interface to find dso's file, open it and
518 * returns file descriptor. It should be paired with
519 * dso__data_put_fd() if it returns non-negative value.
521 int dso__data_get_fd(struct dso
*dso
, struct machine
*machine
)
523 if (dso
->data
.status
== DSO_DATA_STATUS_ERROR
)
526 if (pthread_mutex_lock(&dso__data_open_lock
) < 0)
529 try_to_open_dso(dso
, machine
);
531 if (dso
->data
.fd
< 0)
532 pthread_mutex_unlock(&dso__data_open_lock
);
537 void dso__data_put_fd(struct dso
*dso __maybe_unused
)
539 pthread_mutex_unlock(&dso__data_open_lock
);
542 bool dso__data_status_seen(struct dso
*dso
, enum dso_data_status_seen by
)
546 if (dso
->data
.status_seen
& flag
)
549 dso
->data
.status_seen
|= flag
;
555 dso_cache__free(struct dso
*dso
)
557 struct rb_root
*root
= &dso
->data
.cache
;
558 struct rb_node
*next
= rb_first(root
);
560 pthread_mutex_lock(&dso
->lock
);
562 struct dso_cache
*cache
;
564 cache
= rb_entry(next
, struct dso_cache
, rb_node
);
565 next
= rb_next(&cache
->rb_node
);
566 rb_erase(&cache
->rb_node
, root
);
569 pthread_mutex_unlock(&dso
->lock
);
572 static struct dso_cache
*dso_cache__find(struct dso
*dso
, u64 offset
)
574 const struct rb_root
*root
= &dso
->data
.cache
;
575 struct rb_node
* const *p
= &root
->rb_node
;
576 const struct rb_node
*parent
= NULL
;
577 struct dso_cache
*cache
;
583 cache
= rb_entry(parent
, struct dso_cache
, rb_node
);
584 end
= cache
->offset
+ DSO__DATA_CACHE_SIZE
;
586 if (offset
< cache
->offset
)
588 else if (offset
>= end
)
597 static struct dso_cache
*
598 dso_cache__insert(struct dso
*dso
, struct dso_cache
*new)
600 struct rb_root
*root
= &dso
->data
.cache
;
601 struct rb_node
**p
= &root
->rb_node
;
602 struct rb_node
*parent
= NULL
;
603 struct dso_cache
*cache
;
604 u64 offset
= new->offset
;
606 pthread_mutex_lock(&dso
->lock
);
611 cache
= rb_entry(parent
, struct dso_cache
, rb_node
);
612 end
= cache
->offset
+ DSO__DATA_CACHE_SIZE
;
614 if (offset
< cache
->offset
)
616 else if (offset
>= end
)
622 rb_link_node(&new->rb_node
, parent
, p
);
623 rb_insert_color(&new->rb_node
, root
);
627 pthread_mutex_unlock(&dso
->lock
);
632 dso_cache__memcpy(struct dso_cache
*cache
, u64 offset
,
635 u64 cache_offset
= offset
- cache
->offset
;
636 u64 cache_size
= min(cache
->size
- cache_offset
, size
);
638 memcpy(data
, cache
->data
+ cache_offset
, cache_size
);
643 dso_cache__read(struct dso
*dso
, struct machine
*machine
,
644 u64 offset
, u8
*data
, ssize_t size
)
646 struct dso_cache
*cache
;
647 struct dso_cache
*old
;
653 cache
= zalloc(sizeof(*cache
) + DSO__DATA_CACHE_SIZE
);
657 pthread_mutex_lock(&dso__data_open_lock
);
660 * dso->data.fd might be closed if other thread opened another
661 * file (dso) due to open file limit (RLIMIT_NOFILE).
663 try_to_open_dso(dso
, machine
);
665 if (dso
->data
.fd
< 0) {
667 dso
->data
.status
= DSO_DATA_STATUS_ERROR
;
671 cache_offset
= offset
& DSO__DATA_CACHE_MASK
;
673 ret
= pread(dso
->data
.fd
, cache
->data
, DSO__DATA_CACHE_SIZE
, cache_offset
);
677 cache
->offset
= cache_offset
;
681 pthread_mutex_unlock(&dso__data_open_lock
);
684 old
= dso_cache__insert(dso
, cache
);
686 /* we lose the race */
691 ret
= dso_cache__memcpy(cache
, offset
, data
, size
);
700 static ssize_t
dso_cache_read(struct dso
*dso
, struct machine
*machine
,
701 u64 offset
, u8
*data
, ssize_t size
)
703 struct dso_cache
*cache
;
705 cache
= dso_cache__find(dso
, offset
);
707 return dso_cache__memcpy(cache
, offset
, data
, size
);
709 return dso_cache__read(dso
, machine
, offset
, data
, size
);
713 * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
714 * in the rb_tree. Any read to already cached data is served
717 static ssize_t
cached_read(struct dso
*dso
, struct machine
*machine
,
718 u64 offset
, u8
*data
, ssize_t size
)
726 ret
= dso_cache_read(dso
, machine
, offset
, p
, size
);
730 /* Reached EOF, return what we have. */
746 static int data_file_size(struct dso
*dso
, struct machine
*machine
)
750 char sbuf
[STRERR_BUFSIZE
];
752 if (dso
->data
.file_size
)
755 if (dso
->data
.status
== DSO_DATA_STATUS_ERROR
)
758 pthread_mutex_lock(&dso__data_open_lock
);
761 * dso->data.fd might be closed if other thread opened another
762 * file (dso) due to open file limit (RLIMIT_NOFILE).
764 try_to_open_dso(dso
, machine
);
766 if (dso
->data
.fd
< 0) {
768 dso
->data
.status
= DSO_DATA_STATUS_ERROR
;
772 if (fstat(dso
->data
.fd
, &st
) < 0) {
774 pr_err("dso cache fstat failed: %s\n",
775 strerror_r(errno
, sbuf
, sizeof(sbuf
)));
776 dso
->data
.status
= DSO_DATA_STATUS_ERROR
;
779 dso
->data
.file_size
= st
.st_size
;
782 pthread_mutex_unlock(&dso__data_open_lock
);
787 * dso__data_size - Return dso data size
789 * @machine: machine object
791 * Return: dso data size
793 off_t
dso__data_size(struct dso
*dso
, struct machine
*machine
)
795 if (data_file_size(dso
, machine
))
798 /* For now just estimate dso data size is close to file size */
799 return dso
->data
.file_size
;
802 static ssize_t
data_read_offset(struct dso
*dso
, struct machine
*machine
,
803 u64 offset
, u8
*data
, ssize_t size
)
805 if (data_file_size(dso
, machine
))
808 /* Check the offset sanity. */
809 if (offset
> dso
->data
.file_size
)
812 if (offset
+ size
< offset
)
815 return cached_read(dso
, machine
, offset
, data
, size
);
819 * dso__data_read_offset - Read data from dso file offset
821 * @machine: machine object
822 * @offset: file offset
823 * @data: buffer to store data
824 * @size: size of the @data buffer
826 * External interface to read data from dso file offset. Open
827 * dso data file and use cached_read to get the data.
829 ssize_t
dso__data_read_offset(struct dso
*dso
, struct machine
*machine
,
830 u64 offset
, u8
*data
, ssize_t size
)
832 if (dso
->data
.status
== DSO_DATA_STATUS_ERROR
)
835 return data_read_offset(dso
, machine
, offset
, data
, size
);
839 * dso__data_read_addr - Read data from dso address
841 * @machine: machine object
842 * @add: virtual memory address
843 * @data: buffer to store data
844 * @size: size of the @data buffer
846 * External interface to read data from dso address.
848 ssize_t
dso__data_read_addr(struct dso
*dso
, struct map
*map
,
849 struct machine
*machine
, u64 addr
,
850 u8
*data
, ssize_t size
)
852 u64 offset
= map
->map_ip(map
, addr
);
853 return dso__data_read_offset(dso
, machine
, offset
, data
, size
);
856 struct map
*dso__new_map(const char *name
)
858 struct map
*map
= NULL
;
859 struct dso
*dso
= dso__new(name
);
862 map
= map__new2(0, dso
, MAP__FUNCTION
);
867 struct dso
*machine__findnew_kernel(struct machine
*machine
, const char *name
,
868 const char *short_name
, int dso_type
)
871 * The kernel dso could be created by build_id processing.
873 struct dso
*dso
= machine__findnew_dso(machine
, name
);
876 * We need to run this in all cases, since during the build_id
877 * processing we had no idea this was the kernel dso.
880 dso__set_short_name(dso
, short_name
, false);
881 dso
->kernel
= dso_type
;
888 * Find a matching entry and/or link current entry to RB tree.
889 * Either one of the dso or name parameter must be non-NULL or the
890 * function will not work.
892 static struct dso
*__dso__findlink_by_longname(struct rb_root
*root
,
893 struct dso
*dso
, const char *name
)
895 struct rb_node
**p
= &root
->rb_node
;
896 struct rb_node
*parent
= NULL
;
899 name
= dso
->long_name
;
901 * Find node with the matching name
904 struct dso
*this = rb_entry(*p
, struct dso
, rb_node
);
905 int rc
= strcmp(name
, this->long_name
);
910 * In case the new DSO is a duplicate of an existing
911 * one, print an one-time warning & put the new entry
912 * at the end of the list of duplicates.
914 if (!dso
|| (dso
== this))
915 return this; /* Find matching dso */
917 * The core kernel DSOs may have duplicated long name.
918 * In this case, the short name should be different.
919 * Comparing the short names to differentiate the DSOs.
921 rc
= strcmp(dso
->short_name
, this->short_name
);
923 pr_err("Duplicated dso name: %s\n", name
);
928 p
= &parent
->rb_left
;
930 p
= &parent
->rb_right
;
933 /* Add new node and rebalance tree */
934 rb_link_node(&dso
->rb_node
, parent
, p
);
935 rb_insert_color(&dso
->rb_node
, root
);
940 static inline struct dso
*__dso__find_by_longname(struct rb_root
*root
,
943 return __dso__findlink_by_longname(root
, NULL
, name
);
946 void dso__set_long_name(struct dso
*dso
, const char *name
, bool name_allocated
)
951 if (dso
->long_name_allocated
)
952 free((char *)dso
->long_name
);
954 dso
->long_name
= name
;
955 dso
->long_name_len
= strlen(name
);
956 dso
->long_name_allocated
= name_allocated
;
959 void dso__set_short_name(struct dso
*dso
, const char *name
, bool name_allocated
)
964 if (dso
->short_name_allocated
)
965 free((char *)dso
->short_name
);
967 dso
->short_name
= name
;
968 dso
->short_name_len
= strlen(name
);
969 dso
->short_name_allocated
= name_allocated
;
972 static void dso__set_basename(struct dso
*dso
)
975 * basename() may modify path buffer, so we must pass
978 char *base
, *lname
= strdup(dso
->long_name
);
984 * basename() may return a pointer to internal
985 * storage which is reused in subsequent calls
986 * so copy the result.
988 base
= strdup(basename(lname
));
995 dso__set_short_name(dso
, base
, true);
998 int dso__name_len(const struct dso
*dso
)
1001 return strlen("[unknown]");
1003 return dso
->long_name_len
;
1005 return dso
->short_name_len
;
1008 bool dso__loaded(const struct dso
*dso
, enum map_type type
)
1010 return dso
->loaded
& (1 << type
);
1013 bool dso__sorted_by_name(const struct dso
*dso
, enum map_type type
)
1015 return dso
->sorted_by_name
& (1 << type
);
1018 void dso__set_sorted_by_name(struct dso
*dso
, enum map_type type
)
1020 dso
->sorted_by_name
|= (1 << type
);
1023 struct dso
*dso__new(const char *name
)
1025 struct dso
*dso
= calloc(1, sizeof(*dso
) + strlen(name
) + 1);
1029 strcpy(dso
->name
, name
);
1030 dso__set_long_name(dso
, dso
->name
, false);
1031 dso__set_short_name(dso
, dso
->name
, false);
1032 for (i
= 0; i
< MAP__NR_TYPES
; ++i
)
1033 dso
->symbols
[i
] = dso
->symbol_names
[i
] = RB_ROOT
;
1034 dso
->data
.cache
= RB_ROOT
;
1036 dso
->data
.status
= DSO_DATA_STATUS_UNKNOWN
;
1037 dso
->symtab_type
= DSO_BINARY_TYPE__NOT_FOUND
;
1038 dso
->binary_type
= DSO_BINARY_TYPE__NOT_FOUND
;
1039 dso
->is_64_bit
= (sizeof(void *) == 8);
1042 dso
->sorted_by_name
= 0;
1043 dso
->has_build_id
= 0;
1044 dso
->has_srcline
= 1;
1046 dso
->kernel
= DSO_TYPE_USER
;
1047 dso
->needs_swap
= DSO_SWAP__UNSET
;
1048 RB_CLEAR_NODE(&dso
->rb_node
);
1049 INIT_LIST_HEAD(&dso
->node
);
1050 INIT_LIST_HEAD(&dso
->data
.open_entry
);
1051 pthread_mutex_init(&dso
->lock
, NULL
);
1052 atomic_set(&dso
->refcnt
, 1);
1058 void dso__delete(struct dso
*dso
)
1062 if (!RB_EMPTY_NODE(&dso
->rb_node
))
1063 pr_err("DSO %s is still in rbtree when being deleted!\n",
1065 for (i
= 0; i
< MAP__NR_TYPES
; ++i
)
1066 symbols__delete(&dso
->symbols
[i
]);
1068 if (dso
->short_name_allocated
) {
1069 zfree((char **)&dso
->short_name
);
1070 dso
->short_name_allocated
= false;
1073 if (dso
->long_name_allocated
) {
1074 zfree((char **)&dso
->long_name
);
1075 dso
->long_name_allocated
= false;
1078 dso__data_close(dso
);
1079 auxtrace_cache__free(dso
->auxtrace_cache
);
1080 dso_cache__free(dso
);
1082 zfree(&dso
->symsrc_filename
);
1083 pthread_mutex_destroy(&dso
->lock
);
1087 struct dso
*dso__get(struct dso
*dso
)
1090 atomic_inc(&dso
->refcnt
);
1094 void dso__put(struct dso
*dso
)
1096 if (dso
&& atomic_dec_and_test(&dso
->refcnt
))
1100 void dso__set_build_id(struct dso
*dso
, void *build_id
)
1102 memcpy(dso
->build_id
, build_id
, sizeof(dso
->build_id
));
1103 dso
->has_build_id
= 1;
1106 bool dso__build_id_equal(const struct dso
*dso
, u8
*build_id
)
1108 return memcmp(dso
->build_id
, build_id
, sizeof(dso
->build_id
)) == 0;
1111 void dso__read_running_kernel_build_id(struct dso
*dso
, struct machine
*machine
)
1113 char path
[PATH_MAX
];
1115 if (machine__is_default_guest(machine
))
1117 sprintf(path
, "%s/sys/kernel/notes", machine
->root_dir
);
1118 if (sysfs__read_build_id(path
, dso
->build_id
,
1119 sizeof(dso
->build_id
)) == 0)
1120 dso
->has_build_id
= true;
1123 int dso__kernel_module_get_build_id(struct dso
*dso
,
1124 const char *root_dir
)
1126 char filename
[PATH_MAX
];
1128 * kernel module short names are of the form "[module]" and
1129 * we need just "module" here.
1131 const char *name
= dso
->short_name
+ 1;
1133 snprintf(filename
, sizeof(filename
),
1134 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1135 root_dir
, (int)strlen(name
) - 1, name
);
1137 if (sysfs__read_build_id(filename
, dso
->build_id
,
1138 sizeof(dso
->build_id
)) == 0)
1139 dso
->has_build_id
= true;
1144 bool __dsos__read_build_ids(struct list_head
*head
, bool with_hits
)
1146 bool have_build_id
= false;
1149 list_for_each_entry(pos
, head
, node
) {
1150 if (with_hits
&& !pos
->hit
)
1152 if (pos
->has_build_id
) {
1153 have_build_id
= true;
1156 if (filename__read_build_id(pos
->long_name
, pos
->build_id
,
1157 sizeof(pos
->build_id
)) > 0) {
1158 have_build_id
= true;
1159 pos
->has_build_id
= true;
1163 return have_build_id
;
1166 void __dsos__add(struct dsos
*dsos
, struct dso
*dso
)
1168 list_add_tail(&dso
->node
, &dsos
->head
);
1169 __dso__findlink_by_longname(&dsos
->root
, dso
, NULL
);
1171 * It is now in the linked list, grab a reference, then garbage collect
1172 * this when needing memory, by looking at LRU dso instances in the
1173 * list with atomic_read(&dso->refcnt) == 1, i.e. no references
1174 * anywhere besides the one for the list, do, under a lock for the
1175 * list: remove it from the list, then a dso__put(), that probably will
1176 * be the last and will then call dso__delete(), end of life.
1178 * That, or at the end of the 'struct machine' lifetime, when all
1179 * 'struct dso' instances will be removed from the list, in
1180 * dsos__exit(), if they have no other reference from some other data
1183 * E.g.: after processing a 'perf.data' file and storing references
1184 * to objects instantiated while processing events, we will have
1185 * references to the 'thread', 'map', 'dso' structs all from 'struct
1186 * hist_entry' instances, but we may not need anything not referenced,
1187 * so we might as well call machines__exit()/machines__delete() and
1188 * garbage collect it.
1193 void dsos__add(struct dsos
*dsos
, struct dso
*dso
)
1195 pthread_rwlock_wrlock(&dsos
->lock
);
1196 __dsos__add(dsos
, dso
);
1197 pthread_rwlock_unlock(&dsos
->lock
);
1200 struct dso
*__dsos__find(struct dsos
*dsos
, const char *name
, bool cmp_short
)
1205 list_for_each_entry(pos
, &dsos
->head
, node
)
1206 if (strcmp(pos
->short_name
, name
) == 0)
1210 return __dso__find_by_longname(&dsos
->root
, name
);
1213 struct dso
*dsos__find(struct dsos
*dsos
, const char *name
, bool cmp_short
)
1216 pthread_rwlock_rdlock(&dsos
->lock
);
1217 dso
= __dsos__find(dsos
, name
, cmp_short
);
1218 pthread_rwlock_unlock(&dsos
->lock
);
1222 struct dso
*__dsos__addnew(struct dsos
*dsos
, const char *name
)
1224 struct dso
*dso
= dso__new(name
);
1227 __dsos__add(dsos
, dso
);
1228 dso__set_basename(dso
);
1233 struct dso
*__dsos__findnew(struct dsos
*dsos
, const char *name
)
1235 struct dso
*dso
= __dsos__find(dsos
, name
, false);
1237 return dso
? dso
: __dsos__addnew(dsos
, name
);
1240 struct dso
*dsos__findnew(struct dsos
*dsos
, const char *name
)
1243 pthread_rwlock_wrlock(&dsos
->lock
);
1244 dso
= dso__get(__dsos__findnew(dsos
, name
));
1245 pthread_rwlock_unlock(&dsos
->lock
);
1249 size_t __dsos__fprintf_buildid(struct list_head
*head
, FILE *fp
,
1250 bool (skip
)(struct dso
*dso
, int parm
), int parm
)
1255 list_for_each_entry(pos
, head
, node
) {
1256 if (skip
&& skip(pos
, parm
))
1258 ret
+= dso__fprintf_buildid(pos
, fp
);
1259 ret
+= fprintf(fp
, " %s\n", pos
->long_name
);
1264 size_t __dsos__fprintf(struct list_head
*head
, FILE *fp
)
1269 list_for_each_entry(pos
, head
, node
) {
1271 for (i
= 0; i
< MAP__NR_TYPES
; ++i
)
1272 ret
+= dso__fprintf(pos
, i
, fp
);
1278 size_t dso__fprintf_buildid(struct dso
*dso
, FILE *fp
)
1280 char sbuild_id
[BUILD_ID_SIZE
* 2 + 1];
1282 build_id__sprintf(dso
->build_id
, sizeof(dso
->build_id
), sbuild_id
);
1283 return fprintf(fp
, "%s", sbuild_id
);
1286 size_t dso__fprintf(struct dso
*dso
, enum map_type type
, FILE *fp
)
1289 size_t ret
= fprintf(fp
, "dso: %s (", dso
->short_name
);
1291 if (dso
->short_name
!= dso
->long_name
)
1292 ret
+= fprintf(fp
, "%s, ", dso
->long_name
);
1293 ret
+= fprintf(fp
, "%s, %sloaded, ", map_type__name
[type
],
1294 dso__loaded(dso
, type
) ? "" : "NOT ");
1295 ret
+= dso__fprintf_buildid(dso
, fp
);
1296 ret
+= fprintf(fp
, ")\n");
1297 for (nd
= rb_first(&dso
->symbols
[type
]); nd
; nd
= rb_next(nd
)) {
1298 struct symbol
*pos
= rb_entry(nd
, struct symbol
, rb_node
);
1299 ret
+= symbol__fprintf(pos
, fp
);
1305 enum dso_type
dso__type(struct dso
*dso
, struct machine
*machine
)
1308 enum dso_type type
= DSO__TYPE_UNKNOWN
;
1310 fd
= dso__data_get_fd(dso
, machine
);
1312 type
= dso__type_fd(fd
);
1313 dso__data_put_fd(dso
);
1319 int dso__strerror_load(struct dso
*dso
, char *buf
, size_t buflen
)
1321 int idx
, errnum
= dso
->load_errno
;
1323 * This must have a same ordering as the enum dso_load_errno.
1325 static const char *dso_load__error_str
[] = {
1326 "Internal tools/perf/ library error",
1328 "Can not read build id",
1329 "Mismatching build id",
1330 "Decompression failure",
1333 BUG_ON(buflen
== 0);
1336 const char *err
= strerror_r(errnum
, buf
, buflen
);
1339 scnprintf(buf
, buflen
, "%s", err
);
1344 if (errnum
< __DSO_LOAD_ERRNO__START
|| errnum
>= __DSO_LOAD_ERRNO__END
)
1347 idx
= errnum
- __DSO_LOAD_ERRNO__START
;
1348 scnprintf(buf
, buflen
, "%s", dso_load__error_str
[idx
]);