7 char dso__symtab_origin(const struct dso
*dso
)
9 static const char origin
[] = {
10 [DSO_BINARY_TYPE__KALLSYMS
] = 'k',
11 [DSO_BINARY_TYPE__VMLINUX
] = 'v',
12 [DSO_BINARY_TYPE__JAVA_JIT
] = 'j',
13 [DSO_BINARY_TYPE__DEBUGLINK
] = 'l',
14 [DSO_BINARY_TYPE__BUILD_ID_CACHE
] = 'B',
15 [DSO_BINARY_TYPE__FEDORA_DEBUGINFO
] = 'f',
16 [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO
] = 'u',
17 [DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO
] = 'o',
18 [DSO_BINARY_TYPE__BUILDID_DEBUGINFO
] = 'b',
19 [DSO_BINARY_TYPE__SYSTEM_PATH_DSO
] = 'd',
20 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE
] = 'K',
21 [DSO_BINARY_TYPE__GUEST_KALLSYMS
] = 'g',
22 [DSO_BINARY_TYPE__GUEST_KMODULE
] = 'G',
23 [DSO_BINARY_TYPE__GUEST_VMLINUX
] = 'V',
26 if (dso
== NULL
|| dso
->symtab_type
== DSO_BINARY_TYPE__NOT_FOUND
)
28 return origin
[dso
->symtab_type
];
31 int dso__read_binary_type_filename(const struct dso
*dso
,
32 enum dso_binary_type type
,
33 char *root_dir
, char *filename
, size_t size
)
35 char build_id_hex
[BUILD_ID_SIZE
* 2 + 1];
39 case DSO_BINARY_TYPE__DEBUGLINK
: {
42 strncpy(filename
, dso
->long_name
, size
);
43 debuglink
= filename
+ dso
->long_name_len
;
44 while (debuglink
!= filename
&& *debuglink
!= '/')
46 if (*debuglink
== '/')
48 filename__read_debuglink(dso
->long_name
, debuglink
,
49 size
- (debuglink
- filename
));
52 case DSO_BINARY_TYPE__BUILD_ID_CACHE
:
53 /* skip the locally configured cache if a symfs is given */
54 if (symbol_conf
.symfs
[0] ||
55 (dso__build_id_filename(dso
, filename
, size
) == NULL
))
59 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO
:
60 snprintf(filename
, size
, "%s/usr/lib/debug%s.debug",
61 symbol_conf
.symfs
, dso
->long_name
);
64 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO
:
65 snprintf(filename
, size
, "%s/usr/lib/debug%s",
66 symbol_conf
.symfs
, dso
->long_name
);
69 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO
:
71 const char *last_slash
;
75 last_slash
= dso
->long_name
+ dso
->long_name_len
;
76 while (last_slash
!= dso
->long_name
&& *last_slash
!= '/')
79 len
= scnprintf(filename
, size
, "%s", symbol_conf
.symfs
);
80 dir_size
= last_slash
- dso
->long_name
+ 2;
81 if (dir_size
> (size
- len
)) {
85 len
+= scnprintf(filename
+ len
, dir_size
, "%s", dso
->long_name
);
86 len
+= scnprintf(filename
+ len
, size
- len
, ".debug%s",
91 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO
:
92 if (!dso
->has_build_id
) {
97 build_id__sprintf(dso
->build_id
,
98 sizeof(dso
->build_id
),
100 snprintf(filename
, size
,
101 "%s/usr/lib/debug/.build-id/%.2s/%s.debug",
102 symbol_conf
.symfs
, build_id_hex
, build_id_hex
+ 2);
105 case DSO_BINARY_TYPE__VMLINUX
:
106 case DSO_BINARY_TYPE__GUEST_VMLINUX
:
107 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO
:
108 snprintf(filename
, size
, "%s%s",
109 symbol_conf
.symfs
, dso
->long_name
);
112 case DSO_BINARY_TYPE__GUEST_KMODULE
:
113 snprintf(filename
, size
, "%s%s%s", symbol_conf
.symfs
,
114 root_dir
, dso
->long_name
);
117 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE
:
118 snprintf(filename
, size
, "%s%s", symbol_conf
.symfs
,
122 case DSO_BINARY_TYPE__KCORE
:
123 case DSO_BINARY_TYPE__GUEST_KCORE
:
124 snprintf(filename
, size
, "%s", dso
->long_name
);
128 case DSO_BINARY_TYPE__KALLSYMS
:
129 case DSO_BINARY_TYPE__GUEST_KALLSYMS
:
130 case DSO_BINARY_TYPE__JAVA_JIT
:
131 case DSO_BINARY_TYPE__NOT_FOUND
:
139 static int open_dso(struct dso
*dso
, struct machine
*machine
)
142 char *root_dir
= (char *)"";
143 char *name
= malloc(PATH_MAX
);
149 root_dir
= machine
->root_dir
;
151 if (dso__read_binary_type_filename(dso
, dso
->binary_type
,
152 root_dir
, name
, PATH_MAX
)) {
157 fd
= open(name
, O_RDONLY
);
162 int dso__data_fd(struct dso
*dso
, struct machine
*machine
)
164 enum dso_binary_type binary_type_data
[] = {
165 DSO_BINARY_TYPE__BUILD_ID_CACHE
,
166 DSO_BINARY_TYPE__SYSTEM_PATH_DSO
,
167 DSO_BINARY_TYPE__NOT_FOUND
,
171 if (dso
->binary_type
!= DSO_BINARY_TYPE__NOT_FOUND
)
172 return open_dso(dso
, machine
);
177 dso
->binary_type
= binary_type_data
[i
++];
179 fd
= open_dso(dso
, machine
);
183 } while (dso
->binary_type
!= DSO_BINARY_TYPE__NOT_FOUND
);
189 dso_cache__free(struct rb_root
*root
)
191 struct rb_node
*next
= rb_first(root
);
194 struct dso_cache
*cache
;
196 cache
= rb_entry(next
, struct dso_cache
, rb_node
);
197 next
= rb_next(&cache
->rb_node
);
198 rb_erase(&cache
->rb_node
, root
);
203 static struct dso_cache
*dso_cache__find(const struct rb_root
*root
, u64 offset
)
205 struct rb_node
* const *p
= &root
->rb_node
;
206 const struct rb_node
*parent
= NULL
;
207 struct dso_cache
*cache
;
213 cache
= rb_entry(parent
, struct dso_cache
, rb_node
);
214 end
= cache
->offset
+ DSO__DATA_CACHE_SIZE
;
216 if (offset
< cache
->offset
)
218 else if (offset
>= end
)
227 dso_cache__insert(struct rb_root
*root
, struct dso_cache
*new)
229 struct rb_node
**p
= &root
->rb_node
;
230 struct rb_node
*parent
= NULL
;
231 struct dso_cache
*cache
;
232 u64 offset
= new->offset
;
238 cache
= rb_entry(parent
, struct dso_cache
, rb_node
);
239 end
= cache
->offset
+ DSO__DATA_CACHE_SIZE
;
241 if (offset
< cache
->offset
)
243 else if (offset
>= end
)
247 rb_link_node(&new->rb_node
, parent
, p
);
248 rb_insert_color(&new->rb_node
, root
);
252 dso_cache__memcpy(struct dso_cache
*cache
, u64 offset
,
255 u64 cache_offset
= offset
- cache
->offset
;
256 u64 cache_size
= min(cache
->size
- cache_offset
, size
);
258 memcpy(data
, cache
->data
+ cache_offset
, cache_size
);
263 dso_cache__read(struct dso
*dso
, struct machine
*machine
,
264 u64 offset
, u8
*data
, ssize_t size
)
266 struct dso_cache
*cache
;
270 fd
= dso__data_fd(dso
, machine
);
279 cache
= zalloc(sizeof(*cache
) + DSO__DATA_CACHE_SIZE
);
283 cache_offset
= offset
& DSO__DATA_CACHE_MASK
;
286 if (-1 == lseek(fd
, cache_offset
, SEEK_SET
))
289 ret
= read(fd
, cache
->data
, DSO__DATA_CACHE_SIZE
);
293 cache
->offset
= cache_offset
;
295 dso_cache__insert(&dso
->cache
, cache
);
297 ret
= dso_cache__memcpy(cache
, offset
, data
, size
);
308 static ssize_t
dso_cache_read(struct dso
*dso
, struct machine
*machine
,
309 u64 offset
, u8
*data
, ssize_t size
)
311 struct dso_cache
*cache
;
313 cache
= dso_cache__find(&dso
->cache
, offset
);
315 return dso_cache__memcpy(cache
, offset
, data
, size
);
317 return dso_cache__read(dso
, machine
, offset
, data
, size
);
320 ssize_t
dso__data_read_offset(struct dso
*dso
, struct machine
*machine
,
321 u64 offset
, u8
*data
, ssize_t size
)
329 ret
= dso_cache_read(dso
, machine
, offset
, p
, size
);
333 /* Reached EOF, return what we have. */
349 ssize_t
dso__data_read_addr(struct dso
*dso
, struct map
*map
,
350 struct machine
*machine
, u64 addr
,
351 u8
*data
, ssize_t size
)
353 u64 offset
= map
->map_ip(map
, addr
);
354 return dso__data_read_offset(dso
, machine
, offset
, data
, size
);
357 struct map
*dso__new_map(const char *name
)
359 struct map
*map
= NULL
;
360 struct dso
*dso
= dso__new(name
);
363 map
= map__new2(0, dso
, MAP__FUNCTION
);
368 struct dso
*dso__kernel_findnew(struct machine
*machine
, const char *name
,
369 const char *short_name
, int dso_type
)
372 * The kernel dso could be created by build_id processing.
374 struct dso
*dso
= __dsos__findnew(&machine
->kernel_dsos
, name
);
377 * We need to run this in all cases, since during the build_id
378 * processing we had no idea this was the kernel dso.
381 dso__set_short_name(dso
, short_name
, false);
382 dso
->kernel
= dso_type
;
388 void dso__set_long_name(struct dso
*dso
, const char *name
, bool name_allocated
)
393 if (dso
->long_name_allocated
)
394 free((char *)dso
->long_name
);
396 dso
->long_name
= name
;
397 dso
->long_name_len
= strlen(name
);
398 dso
->long_name_allocated
= name_allocated
;
401 void dso__set_short_name(struct dso
*dso
, const char *name
, bool name_allocated
)
406 if (dso
->short_name_allocated
)
407 free((char *)dso
->short_name
);
409 dso
->short_name
= name
;
410 dso
->short_name_len
= strlen(name
);
411 dso
->short_name_allocated
= name_allocated
;
414 static void dso__set_basename(struct dso
*dso
)
417 * basename() may modify path buffer, so we must pass
420 char *base
, *lname
= strdup(dso
->long_name
);
426 * basename() may return a pointer to internal
427 * storage which is reused in subsequent calls
428 * so copy the result.
430 base
= strdup(basename(lname
));
437 dso__set_short_name(dso
, base
, true);
440 int dso__name_len(const struct dso
*dso
)
443 return strlen("[unknown]");
445 return dso
->long_name_len
;
447 return dso
->short_name_len
;
450 bool dso__loaded(const struct dso
*dso
, enum map_type type
)
452 return dso
->loaded
& (1 << type
);
455 bool dso__sorted_by_name(const struct dso
*dso
, enum map_type type
)
457 return dso
->sorted_by_name
& (1 << type
);
460 void dso__set_sorted_by_name(struct dso
*dso
, enum map_type type
)
462 dso
->sorted_by_name
|= (1 << type
);
465 struct dso
*dso__new(const char *name
)
467 struct dso
*dso
= calloc(1, sizeof(*dso
) + strlen(name
) + 1);
471 strcpy(dso
->name
, name
);
472 dso__set_long_name(dso
, dso
->name
, false);
473 dso__set_short_name(dso
, dso
->name
, false);
474 for (i
= 0; i
< MAP__NR_TYPES
; ++i
)
475 dso
->symbols
[i
] = dso
->symbol_names
[i
] = RB_ROOT
;
476 dso
->cache
= RB_ROOT
;
477 dso
->symtab_type
= DSO_BINARY_TYPE__NOT_FOUND
;
478 dso
->binary_type
= DSO_BINARY_TYPE__NOT_FOUND
;
481 dso
->sorted_by_name
= 0;
482 dso
->has_build_id
= 0;
483 dso
->has_srcline
= 1;
485 dso
->kernel
= DSO_TYPE_USER
;
486 dso
->needs_swap
= DSO_SWAP__UNSET
;
487 INIT_LIST_HEAD(&dso
->node
);
493 void dso__delete(struct dso
*dso
)
496 for (i
= 0; i
< MAP__NR_TYPES
; ++i
)
497 symbols__delete(&dso
->symbols
[i
]);
499 if (dso
->short_name_allocated
) {
500 zfree((char **)&dso
->short_name
);
501 dso
->short_name_allocated
= false;
504 if (dso
->long_name_allocated
) {
505 zfree((char **)&dso
->long_name
);
506 dso
->long_name_allocated
= false;
509 dso_cache__free(&dso
->cache
);
511 zfree(&dso
->symsrc_filename
);
515 void dso__set_build_id(struct dso
*dso
, void *build_id
)
517 memcpy(dso
->build_id
, build_id
, sizeof(dso
->build_id
));
518 dso
->has_build_id
= 1;
521 bool dso__build_id_equal(const struct dso
*dso
, u8
*build_id
)
523 return memcmp(dso
->build_id
, build_id
, sizeof(dso
->build_id
)) == 0;
526 void dso__read_running_kernel_build_id(struct dso
*dso
, struct machine
*machine
)
530 if (machine__is_default_guest(machine
))
532 sprintf(path
, "%s/sys/kernel/notes", machine
->root_dir
);
533 if (sysfs__read_build_id(path
, dso
->build_id
,
534 sizeof(dso
->build_id
)) == 0)
535 dso
->has_build_id
= true;
538 int dso__kernel_module_get_build_id(struct dso
*dso
,
539 const char *root_dir
)
541 char filename
[PATH_MAX
];
543 * kernel module short names are of the form "[module]" and
544 * we need just "module" here.
546 const char *name
= dso
->short_name
+ 1;
548 snprintf(filename
, sizeof(filename
),
549 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
550 root_dir
, (int)strlen(name
) - 1, name
);
552 if (sysfs__read_build_id(filename
, dso
->build_id
,
553 sizeof(dso
->build_id
)) == 0)
554 dso
->has_build_id
= true;
559 bool __dsos__read_build_ids(struct list_head
*head
, bool with_hits
)
561 bool have_build_id
= false;
564 list_for_each_entry(pos
, head
, node
) {
565 if (with_hits
&& !pos
->hit
)
567 if (pos
->has_build_id
) {
568 have_build_id
= true;
571 if (filename__read_build_id(pos
->long_name
, pos
->build_id
,
572 sizeof(pos
->build_id
)) > 0) {
573 have_build_id
= true;
574 pos
->has_build_id
= true;
578 return have_build_id
;
581 void dsos__add(struct list_head
*head
, struct dso
*dso
)
583 list_add_tail(&dso
->node
, head
);
586 struct dso
*dsos__find(const struct list_head
*head
, const char *name
, bool cmp_short
)
591 list_for_each_entry(pos
, head
, node
)
592 if (strcmp(pos
->short_name
, name
) == 0)
596 list_for_each_entry(pos
, head
, node
)
597 if (strcmp(pos
->long_name
, name
) == 0)
602 struct dso
*__dsos__findnew(struct list_head
*head
, const char *name
)
604 struct dso
*dso
= dsos__find(head
, name
, false);
607 dso
= dso__new(name
);
609 dsos__add(head
, dso
);
610 dso__set_basename(dso
);
617 size_t __dsos__fprintf_buildid(struct list_head
*head
, FILE *fp
,
618 bool (skip
)(struct dso
*dso
, int parm
), int parm
)
623 list_for_each_entry(pos
, head
, node
) {
624 if (skip
&& skip(pos
, parm
))
626 ret
+= dso__fprintf_buildid(pos
, fp
);
627 ret
+= fprintf(fp
, " %s\n", pos
->long_name
);
632 size_t __dsos__fprintf(struct list_head
*head
, FILE *fp
)
637 list_for_each_entry(pos
, head
, node
) {
639 for (i
= 0; i
< MAP__NR_TYPES
; ++i
)
640 ret
+= dso__fprintf(pos
, i
, fp
);
646 size_t dso__fprintf_buildid(struct dso
*dso
, FILE *fp
)
648 char sbuild_id
[BUILD_ID_SIZE
* 2 + 1];
650 build_id__sprintf(dso
->build_id
, sizeof(dso
->build_id
), sbuild_id
);
651 return fprintf(fp
, "%s", sbuild_id
);
654 size_t dso__fprintf(struct dso
*dso
, enum map_type type
, FILE *fp
)
657 size_t ret
= fprintf(fp
, "dso: %s (", dso
->short_name
);
659 if (dso
->short_name
!= dso
->long_name
)
660 ret
+= fprintf(fp
, "%s, ", dso
->long_name
);
661 ret
+= fprintf(fp
, "%s, %sloaded, ", map_type__name
[type
],
662 dso__loaded(dso
, type
) ? "" : "NOT ");
663 ret
+= dso__fprintf_buildid(dso
, fp
);
664 ret
+= fprintf(fp
, ")\n");
665 for (nd
= rb_first(&dso
->symbols
[type
]); nd
; nd
= rb_next(nd
)) {
666 struct symbol
*pos
= rb_entry(nd
, struct symbol
, rb_node
);
667 ret
+= symbol__fprintf(pos
, fp
);