3 #include <sys/resource.h>
10 char dso__symtab_origin(const struct dso
*dso
)
12 static const char origin
[] = {
13 [DSO_BINARY_TYPE__KALLSYMS
] = 'k',
14 [DSO_BINARY_TYPE__VMLINUX
] = 'v',
15 [DSO_BINARY_TYPE__JAVA_JIT
] = 'j',
16 [DSO_BINARY_TYPE__DEBUGLINK
] = 'l',
17 [DSO_BINARY_TYPE__BUILD_ID_CACHE
] = 'B',
18 [DSO_BINARY_TYPE__FEDORA_DEBUGINFO
] = 'f',
19 [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO
] = 'u',
20 [DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO
] = 'o',
21 [DSO_BINARY_TYPE__BUILDID_DEBUGINFO
] = 'b',
22 [DSO_BINARY_TYPE__SYSTEM_PATH_DSO
] = 'd',
23 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE
] = 'K',
24 [DSO_BINARY_TYPE__GUEST_KALLSYMS
] = 'g',
25 [DSO_BINARY_TYPE__GUEST_KMODULE
] = 'G',
26 [DSO_BINARY_TYPE__GUEST_VMLINUX
] = 'V',
29 if (dso
== NULL
|| dso
->symtab_type
== DSO_BINARY_TYPE__NOT_FOUND
)
31 return origin
[dso
->symtab_type
];
34 int dso__read_binary_type_filename(const struct dso
*dso
,
35 enum dso_binary_type type
,
36 char *root_dir
, char *filename
, size_t size
)
38 char build_id_hex
[BUILD_ID_SIZE
* 2 + 1];
43 case DSO_BINARY_TYPE__DEBUGLINK
: {
46 strncpy(filename
, dso
->long_name
, size
);
47 debuglink
= filename
+ dso
->long_name_len
;
48 while (debuglink
!= filename
&& *debuglink
!= '/')
50 if (*debuglink
== '/')
52 ret
= filename__read_debuglink(dso
->long_name
, debuglink
,
53 size
- (debuglink
- filename
));
56 case DSO_BINARY_TYPE__BUILD_ID_CACHE
:
57 /* skip the locally configured cache if a symfs is given */
58 if (symbol_conf
.symfs
[0] ||
59 (dso__build_id_filename(dso
, filename
, size
) == NULL
))
63 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO
:
64 len
= __symbol__join_symfs(filename
, size
, "/usr/lib/debug");
65 snprintf(filename
+ len
, size
- len
, "%s.debug", dso
->long_name
);
68 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO
:
69 len
= __symbol__join_symfs(filename
, size
, "/usr/lib/debug");
70 snprintf(filename
+ len
, size
- len
, "%s", dso
->long_name
);
73 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO
:
75 const char *last_slash
;
78 last_slash
= dso
->long_name
+ dso
->long_name_len
;
79 while (last_slash
!= dso
->long_name
&& *last_slash
!= '/')
82 len
= __symbol__join_symfs(filename
, size
, "");
83 dir_size
= last_slash
- dso
->long_name
+ 2;
84 if (dir_size
> (size
- len
)) {
88 len
+= scnprintf(filename
+ len
, dir_size
, "%s", dso
->long_name
);
89 len
+= scnprintf(filename
+ len
, size
- len
, ".debug%s",
94 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO
:
95 if (!dso
->has_build_id
) {
100 build_id__sprintf(dso
->build_id
,
101 sizeof(dso
->build_id
),
103 len
= __symbol__join_symfs(filename
, size
, "/usr/lib/debug/.build-id/");
104 snprintf(filename
+ len
, size
- len
, "%.2s/%s.debug",
105 build_id_hex
, build_id_hex
+ 2);
108 case DSO_BINARY_TYPE__VMLINUX
:
109 case DSO_BINARY_TYPE__GUEST_VMLINUX
:
110 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO
:
111 __symbol__join_symfs(filename
, size
, dso
->long_name
);
114 case DSO_BINARY_TYPE__GUEST_KMODULE
:
115 path__join3(filename
, size
, symbol_conf
.symfs
,
116 root_dir
, dso
->long_name
);
119 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE
:
120 __symbol__join_symfs(filename
, size
, dso
->long_name
);
123 case DSO_BINARY_TYPE__KCORE
:
124 case DSO_BINARY_TYPE__GUEST_KCORE
:
125 snprintf(filename
, size
, "%s", dso
->long_name
);
129 case DSO_BINARY_TYPE__KALLSYMS
:
130 case DSO_BINARY_TYPE__GUEST_KALLSYMS
:
131 case DSO_BINARY_TYPE__JAVA_JIT
:
132 case DSO_BINARY_TYPE__NOT_FOUND
:
141 * Global list of open DSOs and the counter.
143 static LIST_HEAD(dso__data_open
);
144 static long dso__data_open_cnt
;
146 static void dso__list_add(struct dso
*dso
)
148 list_add_tail(&dso
->data
.open_entry
, &dso__data_open
);
149 dso__data_open_cnt
++;
152 static void dso__list_del(struct dso
*dso
)
154 list_del(&dso
->data
.open_entry
);
155 WARN_ONCE(dso__data_open_cnt
<= 0,
156 "DSO data fd counter out of bounds.");
157 dso__data_open_cnt
--;
160 static void close_first_dso(void);
162 static int do_open(char *name
)
167 fd
= open(name
, O_RDONLY
);
171 pr_debug("dso open failed, mmap: %s\n", strerror(errno
));
172 if (!dso__data_open_cnt
|| errno
!= EMFILE
)
181 static int __open_dso(struct dso
*dso
, struct machine
*machine
)
184 char *root_dir
= (char *)"";
185 char *name
= malloc(PATH_MAX
);
191 root_dir
= machine
->root_dir
;
193 if (dso__read_binary_type_filename(dso
, dso
->binary_type
,
194 root_dir
, name
, PATH_MAX
)) {
204 static void check_data_close(void);
207 * dso_close - Open DSO data file
210 * Open @dso's data file descriptor and updates
211 * list/count of open DSO objects.
213 static int open_dso(struct dso
*dso
, struct machine
*machine
)
215 int fd
= __open_dso(dso
, machine
);
220 * Check if we crossed the allowed number
221 * of opened DSOs and close one if needed.
229 static void close_data_fd(struct dso
*dso
)
231 if (dso
->data
.fd
>= 0) {
234 dso
->data
.file_size
= 0;
240 * dso_close - Close DSO data file
243 * Close @dso's data file descriptor and updates
244 * list/count of open DSO objects.
246 static void close_dso(struct dso
*dso
)
251 static void close_first_dso(void)
255 dso
= list_first_entry(&dso__data_open
, struct dso
, data
.open_entry
);
259 static rlim_t
get_fd_limit(void)
264 /* Allow half of the current open fd limit. */
265 if (getrlimit(RLIMIT_NOFILE
, &l
) == 0) {
266 if (l
.rlim_cur
== RLIM_INFINITY
)
269 limit
= l
.rlim_cur
/ 2;
271 pr_err("failed to get fd limit\n");
278 static bool may_cache_fd(void)
283 limit
= get_fd_limit();
285 if (limit
== RLIM_INFINITY
)
288 return limit
> (rlim_t
) dso__data_open_cnt
;
292 * Check and close LRU dso if we crossed allowed limit
293 * for opened dso file descriptors. The limit is half
294 * of the RLIMIT_NOFILE files opened.
296 static void check_data_close(void)
298 bool cache_fd
= may_cache_fd();
305 * dso__data_close - Close DSO data file
308 * External interface to close @dso's data file descriptor.
310 void dso__data_close(struct dso
*dso
)
316 * dso__data_fd - Get dso's data file descriptor
318 * @machine: machine object
320 * External interface to find dso's file, open it and
321 * returns file descriptor.
323 int dso__data_fd(struct dso
*dso
, struct machine
*machine
)
325 enum dso_binary_type binary_type_data
[] = {
326 DSO_BINARY_TYPE__BUILD_ID_CACHE
,
327 DSO_BINARY_TYPE__SYSTEM_PATH_DSO
,
328 DSO_BINARY_TYPE__NOT_FOUND
,
332 if (dso
->data
.status
== DSO_DATA_STATUS_ERROR
)
335 if (dso
->data
.fd
>= 0)
338 if (dso
->binary_type
!= DSO_BINARY_TYPE__NOT_FOUND
) {
339 dso
->data
.fd
= open_dso(dso
, machine
);
344 dso
->binary_type
= binary_type_data
[i
++];
346 dso
->data
.fd
= open_dso(dso
, machine
);
347 if (dso
->data
.fd
>= 0)
350 } while (dso
->binary_type
!= DSO_BINARY_TYPE__NOT_FOUND
);
352 if (dso
->data
.fd
>= 0)
353 dso
->data
.status
= DSO_DATA_STATUS_OK
;
355 dso
->data
.status
= DSO_DATA_STATUS_ERROR
;
360 bool dso__data_status_seen(struct dso
*dso
, enum dso_data_status_seen by
)
364 if (dso
->data
.status_seen
& flag
)
367 dso
->data
.status_seen
|= flag
;
373 dso_cache__free(struct rb_root
*root
)
375 struct rb_node
*next
= rb_first(root
);
378 struct dso_cache
*cache
;
380 cache
= rb_entry(next
, struct dso_cache
, rb_node
);
381 next
= rb_next(&cache
->rb_node
);
382 rb_erase(&cache
->rb_node
, root
);
387 static struct dso_cache
*dso_cache__find(const struct rb_root
*root
, u64 offset
)
389 struct rb_node
* const *p
= &root
->rb_node
;
390 const struct rb_node
*parent
= NULL
;
391 struct dso_cache
*cache
;
397 cache
= rb_entry(parent
, struct dso_cache
, rb_node
);
398 end
= cache
->offset
+ DSO__DATA_CACHE_SIZE
;
400 if (offset
< cache
->offset
)
402 else if (offset
>= end
)
411 dso_cache__insert(struct rb_root
*root
, struct dso_cache
*new)
413 struct rb_node
**p
= &root
->rb_node
;
414 struct rb_node
*parent
= NULL
;
415 struct dso_cache
*cache
;
416 u64 offset
= new->offset
;
422 cache
= rb_entry(parent
, struct dso_cache
, rb_node
);
423 end
= cache
->offset
+ DSO__DATA_CACHE_SIZE
;
425 if (offset
< cache
->offset
)
427 else if (offset
>= end
)
431 rb_link_node(&new->rb_node
, parent
, p
);
432 rb_insert_color(&new->rb_node
, root
);
436 dso_cache__memcpy(struct dso_cache
*cache
, u64 offset
,
439 u64 cache_offset
= offset
- cache
->offset
;
440 u64 cache_size
= min(cache
->size
- cache_offset
, size
);
442 memcpy(data
, cache
->data
+ cache_offset
, cache_size
);
447 dso_cache__read(struct dso
*dso
, u64 offset
, u8
*data
, ssize_t size
)
449 struct dso_cache
*cache
;
457 cache
= zalloc(sizeof(*cache
) + DSO__DATA_CACHE_SIZE
);
461 cache_offset
= offset
& DSO__DATA_CACHE_MASK
;
464 if (-1 == lseek(dso
->data
.fd
, cache_offset
, SEEK_SET
))
467 ret
= read(dso
->data
.fd
, cache
->data
, DSO__DATA_CACHE_SIZE
);
471 cache
->offset
= cache_offset
;
473 dso_cache__insert(&dso
->data
.cache
, cache
);
475 ret
= dso_cache__memcpy(cache
, offset
, data
, size
);
485 static ssize_t
dso_cache_read(struct dso
*dso
, u64 offset
,
486 u8
*data
, ssize_t size
)
488 struct dso_cache
*cache
;
490 cache
= dso_cache__find(&dso
->data
.cache
, offset
);
492 return dso_cache__memcpy(cache
, offset
, data
, size
);
494 return dso_cache__read(dso
, offset
, data
, size
);
498 * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
499 * in the rb_tree. Any read to already cached data is served
502 static ssize_t
cached_read(struct dso
*dso
, u64 offset
, u8
*data
, ssize_t size
)
510 ret
= dso_cache_read(dso
, offset
, p
, size
);
514 /* Reached EOF, return what we have. */
530 static int data_file_size(struct dso
*dso
)
534 if (!dso
->data
.file_size
) {
535 if (fstat(dso
->data
.fd
, &st
)) {
536 pr_err("dso mmap failed, fstat: %s\n", strerror(errno
));
539 dso
->data
.file_size
= st
.st_size
;
546 * dso__data_size - Return dso data size
548 * @machine: machine object
550 * Return: dso data size
552 off_t
dso__data_size(struct dso
*dso
, struct machine
*machine
)
556 fd
= dso__data_fd(dso
, machine
);
560 if (data_file_size(dso
))
563 /* For now just estimate dso data size is close to file size */
564 return dso
->data
.file_size
;
567 static ssize_t
data_read_offset(struct dso
*dso
, u64 offset
,
568 u8
*data
, ssize_t size
)
570 if (data_file_size(dso
))
573 /* Check the offset sanity. */
574 if (offset
> dso
->data
.file_size
)
577 if (offset
+ size
< offset
)
580 return cached_read(dso
, offset
, data
, size
);
584 * dso__data_read_offset - Read data from dso file offset
586 * @machine: machine object
587 * @offset: file offset
588 * @data: buffer to store data
589 * @size: size of the @data buffer
591 * External interface to read data from dso file offset. Open
592 * dso data file and use cached_read to get the data.
594 ssize_t
dso__data_read_offset(struct dso
*dso
, struct machine
*machine
,
595 u64 offset
, u8
*data
, ssize_t size
)
597 if (dso__data_fd(dso
, machine
) < 0)
600 return data_read_offset(dso
, offset
, data
, size
);
604 * dso__data_read_addr - Read data from dso address
606 * @machine: machine object
607 * @add: virtual memory address
608 * @data: buffer to store data
609 * @size: size of the @data buffer
611 * External interface to read data from dso address.
613 ssize_t
dso__data_read_addr(struct dso
*dso
, struct map
*map
,
614 struct machine
*machine
, u64 addr
,
615 u8
*data
, ssize_t size
)
617 u64 offset
= map
->map_ip(map
, addr
);
618 return dso__data_read_offset(dso
, machine
, offset
, data
, size
);
621 struct map
*dso__new_map(const char *name
)
623 struct map
*map
= NULL
;
624 struct dso
*dso
= dso__new(name
);
627 map
= map__new2(0, dso
, MAP__FUNCTION
);
632 struct dso
*dso__kernel_findnew(struct machine
*machine
, const char *name
,
633 const char *short_name
, int dso_type
)
636 * The kernel dso could be created by build_id processing.
638 struct dso
*dso
= __dsos__findnew(&machine
->kernel_dsos
, name
);
641 * We need to run this in all cases, since during the build_id
642 * processing we had no idea this was the kernel dso.
645 dso__set_short_name(dso
, short_name
, false);
646 dso
->kernel
= dso_type
;
652 void dso__set_long_name(struct dso
*dso
, const char *name
, bool name_allocated
)
657 if (dso
->long_name_allocated
)
658 free((char *)dso
->long_name
);
660 dso
->long_name
= name
;
661 dso
->long_name_len
= strlen(name
);
662 dso
->long_name_allocated
= name_allocated
;
665 void dso__set_short_name(struct dso
*dso
, const char *name
, bool name_allocated
)
670 if (dso
->short_name_allocated
)
671 free((char *)dso
->short_name
);
673 dso
->short_name
= name
;
674 dso
->short_name_len
= strlen(name
);
675 dso
->short_name_allocated
= name_allocated
;
678 static void dso__set_basename(struct dso
*dso
)
681 * basename() may modify path buffer, so we must pass
684 char *base
, *lname
= strdup(dso
->long_name
);
690 * basename() may return a pointer to internal
691 * storage which is reused in subsequent calls
692 * so copy the result.
694 base
= strdup(basename(lname
));
701 dso__set_short_name(dso
, base
, true);
704 int dso__name_len(const struct dso
*dso
)
707 return strlen("[unknown]");
709 return dso
->long_name_len
;
711 return dso
->short_name_len
;
714 bool dso__loaded(const struct dso
*dso
, enum map_type type
)
716 return dso
->loaded
& (1 << type
);
719 bool dso__sorted_by_name(const struct dso
*dso
, enum map_type type
)
721 return dso
->sorted_by_name
& (1 << type
);
724 void dso__set_sorted_by_name(struct dso
*dso
, enum map_type type
)
726 dso
->sorted_by_name
|= (1 << type
);
729 struct dso
*dso__new(const char *name
)
731 struct dso
*dso
= calloc(1, sizeof(*dso
) + strlen(name
) + 1);
735 strcpy(dso
->name
, name
);
736 dso__set_long_name(dso
, dso
->name
, false);
737 dso__set_short_name(dso
, dso
->name
, false);
738 for (i
= 0; i
< MAP__NR_TYPES
; ++i
)
739 dso
->symbols
[i
] = dso
->symbol_names
[i
] = RB_ROOT
;
740 dso
->data
.cache
= RB_ROOT
;
742 dso
->data
.status
= DSO_DATA_STATUS_UNKNOWN
;
743 dso
->symtab_type
= DSO_BINARY_TYPE__NOT_FOUND
;
744 dso
->binary_type
= DSO_BINARY_TYPE__NOT_FOUND
;
745 dso
->is_64_bit
= (sizeof(void *) == 8);
748 dso
->sorted_by_name
= 0;
749 dso
->has_build_id
= 0;
750 dso
->has_srcline
= 1;
752 dso
->kernel
= DSO_TYPE_USER
;
753 dso
->needs_swap
= DSO_SWAP__UNSET
;
754 INIT_LIST_HEAD(&dso
->node
);
755 INIT_LIST_HEAD(&dso
->data
.open_entry
);
761 void dso__delete(struct dso
*dso
)
764 for (i
= 0; i
< MAP__NR_TYPES
; ++i
)
765 symbols__delete(&dso
->symbols
[i
]);
767 if (dso
->short_name_allocated
) {
768 zfree((char **)&dso
->short_name
);
769 dso
->short_name_allocated
= false;
772 if (dso
->long_name_allocated
) {
773 zfree((char **)&dso
->long_name
);
774 dso
->long_name_allocated
= false;
777 dso__data_close(dso
);
778 dso_cache__free(&dso
->data
.cache
);
780 zfree(&dso
->symsrc_filename
);
784 void dso__set_build_id(struct dso
*dso
, void *build_id
)
786 memcpy(dso
->build_id
, build_id
, sizeof(dso
->build_id
));
787 dso
->has_build_id
= 1;
790 bool dso__build_id_equal(const struct dso
*dso
, u8
*build_id
)
792 return memcmp(dso
->build_id
, build_id
, sizeof(dso
->build_id
)) == 0;
795 void dso__read_running_kernel_build_id(struct dso
*dso
, struct machine
*machine
)
799 if (machine__is_default_guest(machine
))
801 sprintf(path
, "%s/sys/kernel/notes", machine
->root_dir
);
802 if (sysfs__read_build_id(path
, dso
->build_id
,
803 sizeof(dso
->build_id
)) == 0)
804 dso
->has_build_id
= true;
807 int dso__kernel_module_get_build_id(struct dso
*dso
,
808 const char *root_dir
)
810 char filename
[PATH_MAX
];
812 * kernel module short names are of the form "[module]" and
813 * we need just "module" here.
815 const char *name
= dso
->short_name
+ 1;
817 snprintf(filename
, sizeof(filename
),
818 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
819 root_dir
, (int)strlen(name
) - 1, name
);
821 if (sysfs__read_build_id(filename
, dso
->build_id
,
822 sizeof(dso
->build_id
)) == 0)
823 dso
->has_build_id
= true;
828 bool __dsos__read_build_ids(struct list_head
*head
, bool with_hits
)
830 bool have_build_id
= false;
833 list_for_each_entry(pos
, head
, node
) {
834 if (with_hits
&& !pos
->hit
)
836 if (pos
->has_build_id
) {
837 have_build_id
= true;
840 if (filename__read_build_id(pos
->long_name
, pos
->build_id
,
841 sizeof(pos
->build_id
)) > 0) {
842 have_build_id
= true;
843 pos
->has_build_id
= true;
847 return have_build_id
;
850 void dsos__add(struct list_head
*head
, struct dso
*dso
)
852 list_add_tail(&dso
->node
, head
);
855 struct dso
*dsos__find(const struct list_head
*head
, const char *name
, bool cmp_short
)
860 list_for_each_entry(pos
, head
, node
)
861 if (strcmp(pos
->short_name
, name
) == 0)
865 list_for_each_entry(pos
, head
, node
)
866 if (strcmp(pos
->long_name
, name
) == 0)
871 struct dso
*__dsos__findnew(struct list_head
*head
, const char *name
)
873 struct dso
*dso
= dsos__find(head
, name
, false);
876 dso
= dso__new(name
);
878 dsos__add(head
, dso
);
879 dso__set_basename(dso
);
886 size_t __dsos__fprintf_buildid(struct list_head
*head
, FILE *fp
,
887 bool (skip
)(struct dso
*dso
, int parm
), int parm
)
892 list_for_each_entry(pos
, head
, node
) {
893 if (skip
&& skip(pos
, parm
))
895 ret
+= dso__fprintf_buildid(pos
, fp
);
896 ret
+= fprintf(fp
, " %s\n", pos
->long_name
);
901 size_t __dsos__fprintf(struct list_head
*head
, FILE *fp
)
906 list_for_each_entry(pos
, head
, node
) {
908 for (i
= 0; i
< MAP__NR_TYPES
; ++i
)
909 ret
+= dso__fprintf(pos
, i
, fp
);
915 size_t dso__fprintf_buildid(struct dso
*dso
, FILE *fp
)
917 char sbuild_id
[BUILD_ID_SIZE
* 2 + 1];
919 build_id__sprintf(dso
->build_id
, sizeof(dso
->build_id
), sbuild_id
);
920 return fprintf(fp
, "%s", sbuild_id
);
923 size_t dso__fprintf(struct dso
*dso
, enum map_type type
, FILE *fp
)
926 size_t ret
= fprintf(fp
, "dso: %s (", dso
->short_name
);
928 if (dso
->short_name
!= dso
->long_name
)
929 ret
+= fprintf(fp
, "%s, ", dso
->long_name
);
930 ret
+= fprintf(fp
, "%s, %sloaded, ", map_type__name
[type
],
931 dso__loaded(dso
, type
) ? "" : "NOT ");
932 ret
+= dso__fprintf_buildid(dso
, fp
);
933 ret
+= fprintf(fp
, ")\n");
934 for (nd
= rb_first(&dso
->symbols
[type
]); nd
; nd
= rb_next(nd
)) {
935 struct symbol
*pos
= rb_entry(nd
, struct symbol
, rb_node
);
936 ret
+= symbol__fprintf(pos
, fp
);
942 enum dso_type
dso__type(struct dso
*dso
, struct machine
*machine
)
946 fd
= dso__data_fd(dso
, machine
);
948 return DSO__TYPE_UNKNOWN
;
950 return dso__type_fd(fd
);