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];
42 case DSO_BINARY_TYPE__DEBUGLINK
: {
45 strncpy(filename
, dso
->long_name
, size
);
46 debuglink
= filename
+ dso
->long_name_len
;
47 while (debuglink
!= filename
&& *debuglink
!= '/')
49 if (*debuglink
== '/')
51 ret
= filename__read_debuglink(dso
->long_name
, debuglink
,
52 size
- (debuglink
- filename
));
55 case DSO_BINARY_TYPE__BUILD_ID_CACHE
:
56 /* skip the locally configured cache if a symfs is given */
57 if (symbol_conf
.symfs
[0] ||
58 (dso__build_id_filename(dso
, filename
, size
) == NULL
))
62 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO
:
63 snprintf(filename
, size
, "%s/usr/lib/debug%s.debug",
64 symbol_conf
.symfs
, dso
->long_name
);
67 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO
:
68 snprintf(filename
, size
, "%s/usr/lib/debug%s",
69 symbol_conf
.symfs
, dso
->long_name
);
72 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO
:
74 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
= scnprintf(filename
, size
, "%s", symbol_conf
.symfs
);
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 snprintf(filename
, size
,
104 "%s/usr/lib/debug/.build-id/%.2s/%s.debug",
105 symbol_conf
.symfs
, 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 snprintf(filename
, size
, "%s%s",
112 symbol_conf
.symfs
, dso
->long_name
);
115 case DSO_BINARY_TYPE__GUEST_KMODULE
:
116 snprintf(filename
, size
, "%s%s%s", symbol_conf
.symfs
,
117 root_dir
, dso
->long_name
);
120 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE
:
121 snprintf(filename
, size
, "%s%s", symbol_conf
.symfs
,
125 case DSO_BINARY_TYPE__KCORE
:
126 case DSO_BINARY_TYPE__GUEST_KCORE
:
127 snprintf(filename
, size
, "%s", dso
->long_name
);
131 case DSO_BINARY_TYPE__KALLSYMS
:
132 case DSO_BINARY_TYPE__GUEST_KALLSYMS
:
133 case DSO_BINARY_TYPE__JAVA_JIT
:
134 case DSO_BINARY_TYPE__NOT_FOUND
:
143 * Global list of open DSOs and the counter.
145 static LIST_HEAD(dso__data_open
);
146 static long dso__data_open_cnt
;
148 static void dso__list_add(struct dso
*dso
)
150 list_add_tail(&dso
->data
.open_entry
, &dso__data_open
);
151 dso__data_open_cnt
++;
154 static void dso__list_del(struct dso
*dso
)
156 list_del(&dso
->data
.open_entry
);
157 WARN_ONCE(dso__data_open_cnt
<= 0,
158 "DSO data fd counter out of bounds.");
159 dso__data_open_cnt
--;
162 static void close_first_dso(void);
164 static int do_open(char *name
)
169 fd
= open(name
, O_RDONLY
);
173 pr_debug("dso open failed, mmap: %s\n", strerror(errno
));
174 if (!dso__data_open_cnt
|| errno
!= EMFILE
)
183 static int __open_dso(struct dso
*dso
, struct machine
*machine
)
186 char *root_dir
= (char *)"";
187 char *name
= malloc(PATH_MAX
);
193 root_dir
= machine
->root_dir
;
195 if (dso__read_binary_type_filename(dso
, dso
->binary_type
,
196 root_dir
, name
, PATH_MAX
)) {
206 static void check_data_close(void);
209 * dso_close - Open DSO data file
212 * Open @dso's data file descriptor and updates
213 * list/count of open DSO objects.
215 static int open_dso(struct dso
*dso
, struct machine
*machine
)
217 int fd
= __open_dso(dso
, machine
);
222 * Check if we crossed the allowed number
223 * of opened DSOs and close one if needed.
231 static void close_data_fd(struct dso
*dso
)
233 if (dso
->data
.fd
>= 0) {
236 dso
->data
.file_size
= 0;
242 * dso_close - Close DSO data file
245 * Close @dso's data file descriptor and updates
246 * list/count of open DSO objects.
248 static void close_dso(struct dso
*dso
)
253 static void close_first_dso(void)
257 dso
= list_first_entry(&dso__data_open
, struct dso
, data
.open_entry
);
261 static rlim_t
get_fd_limit(void)
266 /* Allow half of the current open fd limit. */
267 if (getrlimit(RLIMIT_NOFILE
, &l
) == 0) {
268 if (l
.rlim_cur
== RLIM_INFINITY
)
271 limit
= l
.rlim_cur
/ 2;
273 pr_err("failed to get fd limit\n");
280 static bool may_cache_fd(void)
285 limit
= get_fd_limit();
287 if (limit
== RLIM_INFINITY
)
290 return limit
> (rlim_t
) dso__data_open_cnt
;
294 * Check and close LRU dso if we crossed allowed limit
295 * for opened dso file descriptors. The limit is half
296 * of the RLIMIT_NOFILE files opened.
298 static void check_data_close(void)
300 bool cache_fd
= may_cache_fd();
307 * dso__data_close - Close DSO data file
310 * External interface to close @dso's data file descriptor.
312 void dso__data_close(struct dso
*dso
)
318 * dso__data_fd - Get dso's data file descriptor
320 * @machine: machine object
322 * External interface to find dso's file, open it and
323 * returns file descriptor.
325 int dso__data_fd(struct dso
*dso
, struct machine
*machine
)
327 enum dso_binary_type binary_type_data
[] = {
328 DSO_BINARY_TYPE__BUILD_ID_CACHE
,
329 DSO_BINARY_TYPE__SYSTEM_PATH_DSO
,
330 DSO_BINARY_TYPE__NOT_FOUND
,
334 if (dso
->data
.fd
>= 0)
337 if (dso
->binary_type
!= DSO_BINARY_TYPE__NOT_FOUND
) {
338 dso
->data
.fd
= open_dso(dso
, machine
);
345 dso
->binary_type
= binary_type_data
[i
++];
347 fd
= open_dso(dso
, machine
);
349 return dso
->data
.fd
= fd
;
351 } while (dso
->binary_type
!= DSO_BINARY_TYPE__NOT_FOUND
);
357 dso_cache__free(struct rb_root
*root
)
359 struct rb_node
*next
= rb_first(root
);
362 struct dso_cache
*cache
;
364 cache
= rb_entry(next
, struct dso_cache
, rb_node
);
365 next
= rb_next(&cache
->rb_node
);
366 rb_erase(&cache
->rb_node
, root
);
371 static struct dso_cache
*dso_cache__find(const struct rb_root
*root
, u64 offset
)
373 struct rb_node
* const *p
= &root
->rb_node
;
374 const struct rb_node
*parent
= NULL
;
375 struct dso_cache
*cache
;
381 cache
= rb_entry(parent
, struct dso_cache
, rb_node
);
382 end
= cache
->offset
+ DSO__DATA_CACHE_SIZE
;
384 if (offset
< cache
->offset
)
386 else if (offset
>= end
)
395 dso_cache__insert(struct rb_root
*root
, struct dso_cache
*new)
397 struct rb_node
**p
= &root
->rb_node
;
398 struct rb_node
*parent
= NULL
;
399 struct dso_cache
*cache
;
400 u64 offset
= new->offset
;
406 cache
= rb_entry(parent
, struct dso_cache
, rb_node
);
407 end
= cache
->offset
+ DSO__DATA_CACHE_SIZE
;
409 if (offset
< cache
->offset
)
411 else if (offset
>= end
)
415 rb_link_node(&new->rb_node
, parent
, p
);
416 rb_insert_color(&new->rb_node
, root
);
420 dso_cache__memcpy(struct dso_cache
*cache
, u64 offset
,
423 u64 cache_offset
= offset
- cache
->offset
;
424 u64 cache_size
= min(cache
->size
- cache_offset
, size
);
426 memcpy(data
, cache
->data
+ cache_offset
, cache_size
);
431 dso_cache__read(struct dso
*dso
, u64 offset
, u8
*data
, ssize_t size
)
433 struct dso_cache
*cache
;
441 cache
= zalloc(sizeof(*cache
) + DSO__DATA_CACHE_SIZE
);
445 cache_offset
= offset
& DSO__DATA_CACHE_MASK
;
448 if (-1 == lseek(dso
->data
.fd
, cache_offset
, SEEK_SET
))
451 ret
= read(dso
->data
.fd
, cache
->data
, DSO__DATA_CACHE_SIZE
);
455 cache
->offset
= cache_offset
;
457 dso_cache__insert(&dso
->data
.cache
, cache
);
459 ret
= dso_cache__memcpy(cache
, offset
, data
, size
);
469 static ssize_t
dso_cache_read(struct dso
*dso
, u64 offset
,
470 u8
*data
, ssize_t size
)
472 struct dso_cache
*cache
;
474 cache
= dso_cache__find(&dso
->data
.cache
, offset
);
476 return dso_cache__memcpy(cache
, offset
, data
, size
);
478 return dso_cache__read(dso
, offset
, data
, size
);
482 * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
483 * in the rb_tree. Any read to already cached data is served
486 static ssize_t
cached_read(struct dso
*dso
, u64 offset
, u8
*data
, ssize_t size
)
494 ret
= dso_cache_read(dso
, offset
, p
, size
);
498 /* Reached EOF, return what we have. */
514 static int data_file_size(struct dso
*dso
)
518 if (!dso
->data
.file_size
) {
519 if (fstat(dso
->data
.fd
, &st
)) {
520 pr_err("dso mmap failed, fstat: %s\n", strerror(errno
));
523 dso
->data
.file_size
= st
.st_size
;
529 static ssize_t
data_read_offset(struct dso
*dso
, u64 offset
,
530 u8
*data
, ssize_t size
)
532 if (data_file_size(dso
))
535 /* Check the offset sanity. */
536 if (offset
> dso
->data
.file_size
)
539 if (offset
+ size
< offset
)
542 return cached_read(dso
, offset
, data
, size
);
546 * dso__data_read_offset - Read data from dso file offset
548 * @machine: machine object
549 * @offset: file offset
550 * @data: buffer to store data
551 * @size: size of the @data buffer
553 * External interface to read data from dso file offset. Open
554 * dso data file and use cached_read to get the data.
556 ssize_t
dso__data_read_offset(struct dso
*dso
, struct machine
*machine
,
557 u64 offset
, u8
*data
, ssize_t size
)
559 if (dso__data_fd(dso
, machine
) < 0)
562 return data_read_offset(dso
, offset
, data
, size
);
566 * dso__data_read_addr - Read data from dso address
568 * @machine: machine object
569 * @add: virtual memory address
570 * @data: buffer to store data
571 * @size: size of the @data buffer
573 * External interface to read data from dso address.
575 ssize_t
dso__data_read_addr(struct dso
*dso
, struct map
*map
,
576 struct machine
*machine
, u64 addr
,
577 u8
*data
, ssize_t size
)
579 u64 offset
= map
->map_ip(map
, addr
);
580 return dso__data_read_offset(dso
, machine
, offset
, data
, size
);
583 struct map
*dso__new_map(const char *name
)
585 struct map
*map
= NULL
;
586 struct dso
*dso
= dso__new(name
);
589 map
= map__new2(0, dso
, MAP__FUNCTION
);
594 struct dso
*dso__kernel_findnew(struct machine
*machine
, const char *name
,
595 const char *short_name
, int dso_type
)
598 * The kernel dso could be created by build_id processing.
600 struct dso
*dso
= __dsos__findnew(&machine
->kernel_dsos
, name
);
603 * We need to run this in all cases, since during the build_id
604 * processing we had no idea this was the kernel dso.
607 dso__set_short_name(dso
, short_name
, false);
608 dso
->kernel
= dso_type
;
614 void dso__set_long_name(struct dso
*dso
, const char *name
, bool name_allocated
)
619 if (dso
->long_name_allocated
)
620 free((char *)dso
->long_name
);
622 dso
->long_name
= name
;
623 dso
->long_name_len
= strlen(name
);
624 dso
->long_name_allocated
= name_allocated
;
627 void dso__set_short_name(struct dso
*dso
, const char *name
, bool name_allocated
)
632 if (dso
->short_name_allocated
)
633 free((char *)dso
->short_name
);
635 dso
->short_name
= name
;
636 dso
->short_name_len
= strlen(name
);
637 dso
->short_name_allocated
= name_allocated
;
640 static void dso__set_basename(struct dso
*dso
)
643 * basename() may modify path buffer, so we must pass
646 char *base
, *lname
= strdup(dso
->long_name
);
652 * basename() may return a pointer to internal
653 * storage which is reused in subsequent calls
654 * so copy the result.
656 base
= strdup(basename(lname
));
663 dso__set_short_name(dso
, base
, true);
666 int dso__name_len(const struct dso
*dso
)
669 return strlen("[unknown]");
671 return dso
->long_name_len
;
673 return dso
->short_name_len
;
676 bool dso__loaded(const struct dso
*dso
, enum map_type type
)
678 return dso
->loaded
& (1 << type
);
681 bool dso__sorted_by_name(const struct dso
*dso
, enum map_type type
)
683 return dso
->sorted_by_name
& (1 << type
);
686 void dso__set_sorted_by_name(struct dso
*dso
, enum map_type type
)
688 dso
->sorted_by_name
|= (1 << type
);
691 struct dso
*dso__new(const char *name
)
693 struct dso
*dso
= calloc(1, sizeof(*dso
) + strlen(name
) + 1);
697 strcpy(dso
->name
, name
);
698 dso__set_long_name(dso
, dso
->name
, false);
699 dso__set_short_name(dso
, dso
->name
, false);
700 for (i
= 0; i
< MAP__NR_TYPES
; ++i
)
701 dso
->symbols
[i
] = dso
->symbol_names
[i
] = RB_ROOT
;
702 dso
->data
.cache
= RB_ROOT
;
704 dso
->symtab_type
= DSO_BINARY_TYPE__NOT_FOUND
;
705 dso
->binary_type
= DSO_BINARY_TYPE__NOT_FOUND
;
708 dso
->sorted_by_name
= 0;
709 dso
->has_build_id
= 0;
710 dso
->has_srcline
= 1;
712 dso
->kernel
= DSO_TYPE_USER
;
713 dso
->needs_swap
= DSO_SWAP__UNSET
;
714 INIT_LIST_HEAD(&dso
->node
);
715 INIT_LIST_HEAD(&dso
->data
.open_entry
);
721 void dso__delete(struct dso
*dso
)
724 for (i
= 0; i
< MAP__NR_TYPES
; ++i
)
725 symbols__delete(&dso
->symbols
[i
]);
727 if (dso
->short_name_allocated
) {
728 zfree((char **)&dso
->short_name
);
729 dso
->short_name_allocated
= false;
732 if (dso
->long_name_allocated
) {
733 zfree((char **)&dso
->long_name
);
734 dso
->long_name_allocated
= false;
737 dso__data_close(dso
);
738 dso_cache__free(&dso
->data
.cache
);
740 zfree(&dso
->symsrc_filename
);
744 void dso__set_build_id(struct dso
*dso
, void *build_id
)
746 memcpy(dso
->build_id
, build_id
, sizeof(dso
->build_id
));
747 dso
->has_build_id
= 1;
750 bool dso__build_id_equal(const struct dso
*dso
, u8
*build_id
)
752 return memcmp(dso
->build_id
, build_id
, sizeof(dso
->build_id
)) == 0;
755 void dso__read_running_kernel_build_id(struct dso
*dso
, struct machine
*machine
)
759 if (machine__is_default_guest(machine
))
761 sprintf(path
, "%s/sys/kernel/notes", machine
->root_dir
);
762 if (sysfs__read_build_id(path
, dso
->build_id
,
763 sizeof(dso
->build_id
)) == 0)
764 dso
->has_build_id
= true;
767 int dso__kernel_module_get_build_id(struct dso
*dso
,
768 const char *root_dir
)
770 char filename
[PATH_MAX
];
772 * kernel module short names are of the form "[module]" and
773 * we need just "module" here.
775 const char *name
= dso
->short_name
+ 1;
777 snprintf(filename
, sizeof(filename
),
778 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
779 root_dir
, (int)strlen(name
) - 1, name
);
781 if (sysfs__read_build_id(filename
, dso
->build_id
,
782 sizeof(dso
->build_id
)) == 0)
783 dso
->has_build_id
= true;
788 bool __dsos__read_build_ids(struct list_head
*head
, bool with_hits
)
790 bool have_build_id
= false;
793 list_for_each_entry(pos
, head
, node
) {
794 if (with_hits
&& !pos
->hit
)
796 if (pos
->has_build_id
) {
797 have_build_id
= true;
800 if (filename__read_build_id(pos
->long_name
, pos
->build_id
,
801 sizeof(pos
->build_id
)) > 0) {
802 have_build_id
= true;
803 pos
->has_build_id
= true;
807 return have_build_id
;
810 void dsos__add(struct list_head
*head
, struct dso
*dso
)
812 list_add_tail(&dso
->node
, head
);
815 struct dso
*dsos__find(const struct list_head
*head
, const char *name
, bool cmp_short
)
820 list_for_each_entry(pos
, head
, node
)
821 if (strcmp(pos
->short_name
, name
) == 0)
825 list_for_each_entry(pos
, head
, node
)
826 if (strcmp(pos
->long_name
, name
) == 0)
831 struct dso
*__dsos__findnew(struct list_head
*head
, const char *name
)
833 struct dso
*dso
= dsos__find(head
, name
, false);
836 dso
= dso__new(name
);
838 dsos__add(head
, dso
);
839 dso__set_basename(dso
);
846 size_t __dsos__fprintf_buildid(struct list_head
*head
, FILE *fp
,
847 bool (skip
)(struct dso
*dso
, int parm
), int parm
)
852 list_for_each_entry(pos
, head
, node
) {
853 if (skip
&& skip(pos
, parm
))
855 ret
+= dso__fprintf_buildid(pos
, fp
);
856 ret
+= fprintf(fp
, " %s\n", pos
->long_name
);
861 size_t __dsos__fprintf(struct list_head
*head
, FILE *fp
)
866 list_for_each_entry(pos
, head
, node
) {
868 for (i
= 0; i
< MAP__NR_TYPES
; ++i
)
869 ret
+= dso__fprintf(pos
, i
, fp
);
875 size_t dso__fprintf_buildid(struct dso
*dso
, FILE *fp
)
877 char sbuild_id
[BUILD_ID_SIZE
* 2 + 1];
879 build_id__sprintf(dso
->build_id
, sizeof(dso
->build_id
), sbuild_id
);
880 return fprintf(fp
, "%s", sbuild_id
);
883 size_t dso__fprintf(struct dso
*dso
, enum map_type type
, FILE *fp
)
886 size_t ret
= fprintf(fp
, "dso: %s (", dso
->short_name
);
888 if (dso
->short_name
!= dso
->long_name
)
889 ret
+= fprintf(fp
, "%s, ", dso
->long_name
);
890 ret
+= fprintf(fp
, "%s, %sloaded, ", map_type__name
[type
],
891 dso__loaded(dso
, type
) ? "" : "NOT ");
892 ret
+= dso__fprintf_buildid(dso
, fp
);
893 ret
+= fprintf(fp
, ")\n");
894 for (nd
= rb_first(&dso
->symbols
[type
]); nd
; nd
= rb_next(nd
)) {
895 struct symbol
*pos
= rb_entry(nd
, struct symbol
, rb_node
);
896 ret
+= symbol__fprintf(pos
, fp
);