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__binary_type_file(struct dso
*dso
, enum dso_binary_type type
,
32 char *root_dir
, char *file
, size_t size
)
34 char build_id_hex
[BUILD_ID_SIZE
* 2 + 1];
38 case DSO_BINARY_TYPE__DEBUGLINK
: {
41 strncpy(file
, dso
->long_name
, size
);
42 debuglink
= file
+ dso
->long_name_len
;
43 while (debuglink
!= file
&& *debuglink
!= '/')
45 if (*debuglink
== '/')
47 filename__read_debuglink(dso
->long_name
, debuglink
,
48 size
- (debuglink
- file
));
51 case DSO_BINARY_TYPE__BUILD_ID_CACHE
:
52 /* skip the locally configured cache if a symfs is given */
53 if (symbol_conf
.symfs
[0] ||
54 (dso__build_id_filename(dso
, file
, size
) == NULL
))
58 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO
:
59 snprintf(file
, size
, "%s/usr/lib/debug%s.debug",
60 symbol_conf
.symfs
, dso
->long_name
);
63 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO
:
64 snprintf(file
, size
, "%s/usr/lib/debug%s",
65 symbol_conf
.symfs
, dso
->long_name
);
68 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO
:
74 last_slash
= dso
->long_name
+ dso
->long_name_len
;
75 while (last_slash
!= dso
->long_name
&& *last_slash
!= '/')
78 len
= scnprintf(file
, size
, "%s", symbol_conf
.symfs
);
79 dir_size
= last_slash
- dso
->long_name
+ 2;
80 if (dir_size
> (size
- len
)) {
84 len
+= scnprintf(file
+ len
, dir_size
, "%s", dso
->long_name
);
85 len
+= scnprintf(file
+ len
, size
- len
, ".debug%s",
90 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO
:
91 if (!dso
->has_build_id
) {
96 build_id__sprintf(dso
->build_id
,
97 sizeof(dso
->build_id
),
100 "%s/usr/lib/debug/.build-id/%.2s/%s.debug",
101 symbol_conf
.symfs
, build_id_hex
, build_id_hex
+ 2);
104 case DSO_BINARY_TYPE__VMLINUX
:
105 case DSO_BINARY_TYPE__GUEST_VMLINUX
:
106 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO
:
107 snprintf(file
, size
, "%s%s",
108 symbol_conf
.symfs
, dso
->long_name
);
111 case DSO_BINARY_TYPE__GUEST_KMODULE
:
112 snprintf(file
, size
, "%s%s%s", symbol_conf
.symfs
,
113 root_dir
, dso
->long_name
);
116 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE
:
117 snprintf(file
, size
, "%s%s", symbol_conf
.symfs
,
121 case DSO_BINARY_TYPE__KCORE
:
122 case DSO_BINARY_TYPE__GUEST_KCORE
:
123 snprintf(file
, size
, "%s", dso
->long_name
);
127 case DSO_BINARY_TYPE__KALLSYMS
:
128 case DSO_BINARY_TYPE__GUEST_KALLSYMS
:
129 case DSO_BINARY_TYPE__JAVA_JIT
:
130 case DSO_BINARY_TYPE__NOT_FOUND
:
138 static int open_dso(struct dso
*dso
, struct machine
*machine
)
140 char *root_dir
= (char *) "";
144 name
= malloc(PATH_MAX
);
149 root_dir
= machine
->root_dir
;
151 if (dso__binary_type_file(dso
, dso
->data_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 static 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
->data_type
!= DSO_BINARY_TYPE__NOT_FOUND
)
172 return open_dso(dso
, machine
);
177 dso
->data_type
= binary_type_data
[i
++];
179 fd
= open_dso(dso
, machine
);
183 } while (dso
->data_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
*
204 dso_cache__find(struct rb_root
*root
, u64 offset
)
206 struct rb_node
**p
= &root
->rb_node
;
207 struct rb_node
*parent
= NULL
;
208 struct dso_cache
*cache
;
214 cache
= rb_entry(parent
, struct dso_cache
, rb_node
);
215 end
= cache
->offset
+ DSO__DATA_CACHE_SIZE
;
217 if (offset
< cache
->offset
)
219 else if (offset
>= end
)
228 dso_cache__insert(struct rb_root
*root
, struct dso_cache
*new)
230 struct rb_node
**p
= &root
->rb_node
;
231 struct rb_node
*parent
= NULL
;
232 struct dso_cache
*cache
;
233 u64 offset
= new->offset
;
239 cache
= rb_entry(parent
, struct dso_cache
, rb_node
);
240 end
= cache
->offset
+ DSO__DATA_CACHE_SIZE
;
242 if (offset
< cache
->offset
)
244 else if (offset
>= end
)
248 rb_link_node(&new->rb_node
, parent
, p
);
249 rb_insert_color(&new->rb_node
, root
);
253 dso_cache__memcpy(struct dso_cache
*cache
, u64 offset
,
256 u64 cache_offset
= offset
- cache
->offset
;
257 u64 cache_size
= min(cache
->size
- cache_offset
, size
);
259 memcpy(data
, cache
->data
+ cache_offset
, cache_size
);
264 dso_cache__read(struct dso
*dso
, struct machine
*machine
,
265 u64 offset
, u8
*data
, ssize_t size
)
267 struct dso_cache
*cache
;
271 fd
= dso__data_fd(dso
, machine
);
280 cache
= zalloc(sizeof(*cache
) + DSO__DATA_CACHE_SIZE
);
284 cache_offset
= offset
& DSO__DATA_CACHE_MASK
;
287 if (-1 == lseek(fd
, cache_offset
, SEEK_SET
))
290 ret
= read(fd
, cache
->data
, DSO__DATA_CACHE_SIZE
);
294 cache
->offset
= cache_offset
;
296 dso_cache__insert(&dso
->cache
, cache
);
298 ret
= dso_cache__memcpy(cache
, offset
, data
, size
);
309 static ssize_t
dso_cache_read(struct dso
*dso
, struct machine
*machine
,
310 u64 offset
, u8
*data
, ssize_t size
)
312 struct dso_cache
*cache
;
314 cache
= dso_cache__find(&dso
->cache
, offset
);
316 return dso_cache__memcpy(cache
, offset
, data
, size
);
318 return dso_cache__read(dso
, machine
, offset
, data
, size
);
321 ssize_t
dso__data_read_offset(struct dso
*dso
, struct machine
*machine
,
322 u64 offset
, u8
*data
, ssize_t size
)
330 ret
= dso_cache_read(dso
, machine
, offset
, p
, size
);
334 /* Reached EOF, return what we have. */
350 ssize_t
dso__data_read_addr(struct dso
*dso
, struct map
*map
,
351 struct machine
*machine
, u64 addr
,
352 u8
*data
, ssize_t size
)
354 u64 offset
= map
->map_ip(map
, addr
);
355 return dso__data_read_offset(dso
, machine
, offset
, data
, size
);
358 struct map
*dso__new_map(const char *name
)
360 struct map
*map
= NULL
;
361 struct dso
*dso
= dso__new(name
);
364 map
= map__new2(0, dso
, MAP__FUNCTION
);
369 struct dso
*dso__kernel_findnew(struct machine
*machine
, const char *name
,
370 const char *short_name
, int dso_type
)
373 * The kernel dso could be created by build_id processing.
375 struct dso
*dso
= __dsos__findnew(&machine
->kernel_dsos
, name
);
378 * We need to run this in all cases, since during the build_id
379 * processing we had no idea this was the kernel dso.
382 dso__set_short_name(dso
, short_name
);
383 dso
->kernel
= dso_type
;
389 void dso__set_long_name(struct dso
*dso
, char *name
)
393 dso
->long_name
= name
;
394 dso
->long_name_len
= strlen(name
);
397 void dso__set_short_name(struct dso
*dso
, const char *name
)
401 dso
->short_name
= name
;
402 dso
->short_name_len
= strlen(name
);
405 static void dso__set_basename(struct dso
*dso
)
407 dso__set_short_name(dso
, basename(dso
->long_name
));
410 int dso__name_len(const struct dso
*dso
)
413 return strlen("[unknown]");
415 return dso
->long_name_len
;
417 return dso
->short_name_len
;
420 bool dso__loaded(const struct dso
*dso
, enum map_type type
)
422 return dso
->loaded
& (1 << type
);
425 bool dso__sorted_by_name(const struct dso
*dso
, enum map_type type
)
427 return dso
->sorted_by_name
& (1 << type
);
430 void dso__set_sorted_by_name(struct dso
*dso
, enum map_type type
)
432 dso
->sorted_by_name
|= (1 << type
);
435 struct dso
*dso__new(const char *name
)
437 struct dso
*dso
= calloc(1, sizeof(*dso
) + strlen(name
) + 1);
441 strcpy(dso
->name
, name
);
442 dso__set_long_name(dso
, dso
->name
);
443 dso__set_short_name(dso
, dso
->name
);
444 for (i
= 0; i
< MAP__NR_TYPES
; ++i
)
445 dso
->symbols
[i
] = dso
->symbol_names
[i
] = RB_ROOT
;
446 dso
->cache
= RB_ROOT
;
447 dso
->symtab_type
= DSO_BINARY_TYPE__NOT_FOUND
;
448 dso
->data_type
= DSO_BINARY_TYPE__NOT_FOUND
;
451 dso
->sorted_by_name
= 0;
452 dso
->has_build_id
= 0;
453 dso
->has_srcline
= 1;
454 dso
->kernel
= DSO_TYPE_USER
;
455 dso
->needs_swap
= DSO_SWAP__UNSET
;
456 INIT_LIST_HEAD(&dso
->node
);
462 void dso__delete(struct dso
*dso
)
465 for (i
= 0; i
< MAP__NR_TYPES
; ++i
)
466 symbols__delete(&dso
->symbols
[i
]);
467 if (dso
->sname_alloc
)
468 free((char *)dso
->short_name
);
469 if (dso
->lname_alloc
)
470 free(dso
->long_name
);
471 dso_cache__free(&dso
->cache
);
473 free(dso
->symsrc_filename
);
477 void dso__set_build_id(struct dso
*dso
, void *build_id
)
479 memcpy(dso
->build_id
, build_id
, sizeof(dso
->build_id
));
480 dso
->has_build_id
= 1;
483 bool dso__build_id_equal(const struct dso
*dso
, u8
*build_id
)
485 return memcmp(dso
->build_id
, build_id
, sizeof(dso
->build_id
)) == 0;
488 void dso__read_running_kernel_build_id(struct dso
*dso
, struct machine
*machine
)
492 if (machine__is_default_guest(machine
))
494 sprintf(path
, "%s/sys/kernel/notes", machine
->root_dir
);
495 if (sysfs__read_build_id(path
, dso
->build_id
,
496 sizeof(dso
->build_id
)) == 0)
497 dso
->has_build_id
= true;
500 int dso__kernel_module_get_build_id(struct dso
*dso
,
501 const char *root_dir
)
503 char filename
[PATH_MAX
];
505 * kernel module short names are of the form "[module]" and
506 * we need just "module" here.
508 const char *name
= dso
->short_name
+ 1;
510 snprintf(filename
, sizeof(filename
),
511 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
512 root_dir
, (int)strlen(name
) - 1, name
);
514 if (sysfs__read_build_id(filename
, dso
->build_id
,
515 sizeof(dso
->build_id
)) == 0)
516 dso
->has_build_id
= true;
521 bool __dsos__read_build_ids(struct list_head
*head
, bool with_hits
)
523 bool have_build_id
= false;
526 list_for_each_entry(pos
, head
, node
) {
527 if (with_hits
&& !pos
->hit
)
529 if (pos
->has_build_id
) {
530 have_build_id
= true;
533 if (filename__read_build_id(pos
->long_name
, pos
->build_id
,
534 sizeof(pos
->build_id
)) > 0) {
535 have_build_id
= true;
536 pos
->has_build_id
= true;
540 return have_build_id
;
543 void dsos__add(struct list_head
*head
, struct dso
*dso
)
545 list_add_tail(&dso
->node
, head
);
548 struct dso
*dsos__find(struct list_head
*head
, const char *name
, bool cmp_short
)
553 list_for_each_entry(pos
, head
, node
)
554 if (strcmp(pos
->short_name
, name
) == 0)
558 list_for_each_entry(pos
, head
, node
)
559 if (strcmp(pos
->long_name
, name
) == 0)
564 struct dso
*__dsos__findnew(struct list_head
*head
, const char *name
)
566 struct dso
*dso
= dsos__find(head
, name
, false);
569 dso
= dso__new(name
);
571 dsos__add(head
, dso
);
572 dso__set_basename(dso
);
579 size_t __dsos__fprintf_buildid(struct list_head
*head
, FILE *fp
,
580 bool (skip
)(struct dso
*dso
, int parm
), int parm
)
585 list_for_each_entry(pos
, head
, node
) {
586 if (skip
&& skip(pos
, parm
))
588 ret
+= dso__fprintf_buildid(pos
, fp
);
589 ret
+= fprintf(fp
, " %s\n", pos
->long_name
);
594 size_t __dsos__fprintf(struct list_head
*head
, FILE *fp
)
599 list_for_each_entry(pos
, head
, node
) {
601 for (i
= 0; i
< MAP__NR_TYPES
; ++i
)
602 ret
+= dso__fprintf(pos
, i
, fp
);
608 size_t dso__fprintf_buildid(struct dso
*dso
, FILE *fp
)
610 char sbuild_id
[BUILD_ID_SIZE
* 2 + 1];
612 build_id__sprintf(dso
->build_id
, sizeof(dso
->build_id
), sbuild_id
);
613 return fprintf(fp
, "%s", sbuild_id
);
616 size_t dso__fprintf(struct dso
*dso
, enum map_type type
, FILE *fp
)
619 size_t ret
= fprintf(fp
, "dso: %s (", dso
->short_name
);
621 if (dso
->short_name
!= dso
->long_name
)
622 ret
+= fprintf(fp
, "%s, ", dso
->long_name
);
623 ret
+= fprintf(fp
, "%s, %sloaded, ", map_type__name
[type
],
624 dso__loaded(dso
, type
) ? "" : "NOT ");
625 ret
+= dso__fprintf_buildid(dso
, fp
);
626 ret
+= fprintf(fp
, ")\n");
627 for (nd
= rb_first(&dso
->symbols
[type
]); nd
; nd
= rb_next(nd
)) {
628 struct symbol
*pos
= rb_entry(nd
, struct symbol
, rb_node
);
629 ret
+= symbol__fprintf(pos
, fp
);