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__BUILDID_DEBUGINFO] = 'b',
18 [DSO_BINARY_TYPE__SYSTEM_PATH_DSO] = 'd',
19 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE] = 'K',
20 [DSO_BINARY_TYPE__GUEST_KALLSYMS] = 'g',
21 [DSO_BINARY_TYPE__GUEST_KMODULE] = 'G',
22 [DSO_BINARY_TYPE__GUEST_VMLINUX] = 'V',
25 if (dso == NULL || dso->symtab_type == DSO_BINARY_TYPE__NOT_FOUND)
27 return origin[dso->symtab_type];
30 int dso__binary_type_file(struct dso *dso, enum dso_binary_type type,
31 char *root_dir, char *file, size_t size)
33 char build_id_hex[BUILD_ID_SIZE * 2 + 1];
37 case DSO_BINARY_TYPE__DEBUGLINK: {
40 strncpy(file, dso->long_name, size);
41 debuglink = file + dso->long_name_len;
42 while (debuglink != file && *debuglink != '/')
44 if (*debuglink == '/')
46 filename__read_debuglink(dso->long_name, debuglink,
47 size - (debuglink - file));
50 case DSO_BINARY_TYPE__BUILD_ID_CACHE:
51 /* skip the locally configured cache if a symfs is given */
52 if (symbol_conf.symfs[0] ||
53 (dso__build_id_filename(dso, file, size) == NULL))
57 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
58 snprintf(file, size, "%s/usr/lib/debug%s.debug",
59 symbol_conf.symfs, dso->long_name);
62 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
63 snprintf(file, size, "%s/usr/lib/debug%s",
64 symbol_conf.symfs, dso->long_name);
67 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
68 if (!dso->has_build_id) {
73 build_id__sprintf(dso->build_id,
74 sizeof(dso->build_id),
77 "%s/usr/lib/debug/.build-id/%.2s/%s.debug",
78 symbol_conf.symfs, build_id_hex, build_id_hex + 2);
81 case DSO_BINARY_TYPE__VMLINUX:
82 case DSO_BINARY_TYPE__GUEST_VMLINUX:
83 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
84 snprintf(file, size, "%s%s",
85 symbol_conf.symfs, dso->long_name);
88 case DSO_BINARY_TYPE__GUEST_KMODULE:
89 snprintf(file, size, "%s%s%s", symbol_conf.symfs,
90 root_dir, dso->long_name);
93 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
94 snprintf(file, size, "%s%s", symbol_conf.symfs,
98 case DSO_BINARY_TYPE__KCORE:
99 case DSO_BINARY_TYPE__GUEST_KCORE:
100 snprintf(file, size, "%s", dso->long_name);
104 case DSO_BINARY_TYPE__KALLSYMS:
105 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
106 case DSO_BINARY_TYPE__JAVA_JIT:
107 case DSO_BINARY_TYPE__NOT_FOUND:
115 static int open_dso(struct dso *dso, struct machine *machine)
117 char *root_dir = (char *) "";
121 name = malloc(PATH_MAX);
126 root_dir = machine->root_dir;
128 if (dso__binary_type_file(dso, dso->data_type,
129 root_dir, name, PATH_MAX)) {
134 fd = open(name, O_RDONLY);
139 int dso__data_fd(struct dso *dso, struct machine *machine)
141 static enum dso_binary_type binary_type_data[] = {
142 DSO_BINARY_TYPE__BUILD_ID_CACHE,
143 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
144 DSO_BINARY_TYPE__NOT_FOUND,
148 if (dso->data_type != DSO_BINARY_TYPE__NOT_FOUND)
149 return open_dso(dso, machine);
154 dso->data_type = binary_type_data[i++];
156 fd = open_dso(dso, machine);
160 } while (dso->data_type != DSO_BINARY_TYPE__NOT_FOUND);
166 dso_cache__free(struct rb_root *root)
168 struct rb_node *next = rb_first(root);
171 struct dso_cache *cache;
173 cache = rb_entry(next, struct dso_cache, rb_node);
174 next = rb_next(&cache->rb_node);
175 rb_erase(&cache->rb_node, root);
180 static struct dso_cache*
181 dso_cache__find(struct rb_root *root, u64 offset)
183 struct rb_node **p = &root->rb_node;
184 struct rb_node *parent = NULL;
185 struct dso_cache *cache;
191 cache = rb_entry(parent, struct dso_cache, rb_node);
192 end = cache->offset + DSO__DATA_CACHE_SIZE;
194 if (offset < cache->offset)
196 else if (offset >= end)
205 dso_cache__insert(struct rb_root *root, struct dso_cache *new)
207 struct rb_node **p = &root->rb_node;
208 struct rb_node *parent = NULL;
209 struct dso_cache *cache;
210 u64 offset = new->offset;
216 cache = rb_entry(parent, struct dso_cache, rb_node);
217 end = cache->offset + DSO__DATA_CACHE_SIZE;
219 if (offset < cache->offset)
221 else if (offset >= end)
225 rb_link_node(&new->rb_node, parent, p);
226 rb_insert_color(&new->rb_node, root);
230 dso_cache__memcpy(struct dso_cache *cache, u64 offset,
233 u64 cache_offset = offset - cache->offset;
234 u64 cache_size = min(cache->size - cache_offset, size);
236 memcpy(data, cache->data + cache_offset, cache_size);
241 dso_cache__read(struct dso *dso, struct machine *machine,
242 u64 offset, u8 *data, ssize_t size)
244 struct dso_cache *cache;
248 fd = dso__data_fd(dso, machine);
257 cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
261 cache_offset = offset & DSO__DATA_CACHE_MASK;
264 if (-1 == lseek(fd, cache_offset, SEEK_SET))
267 ret = read(fd, cache->data, DSO__DATA_CACHE_SIZE);
271 cache->offset = cache_offset;
273 dso_cache__insert(&dso->cache, cache);
275 ret = dso_cache__memcpy(cache, offset, data, size);
286 static ssize_t dso_cache_read(struct dso *dso, struct machine *machine,
287 u64 offset, u8 *data, ssize_t size)
289 struct dso_cache *cache;
291 cache = dso_cache__find(&dso->cache, offset);
293 return dso_cache__memcpy(cache, offset, data, size);
295 return dso_cache__read(dso, machine, offset, data, size);
298 ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
299 u64 offset, u8 *data, ssize_t size)
307 ret = dso_cache_read(dso, machine, offset, p, size);
311 /* Reached EOF, return what we have. */
327 ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
328 struct machine *machine, u64 addr,
329 u8 *data, ssize_t size)
331 u64 offset = map->map_ip(map, addr);
332 return dso__data_read_offset(dso, machine, offset, data, size);
335 struct map *dso__new_map(const char *name)
337 struct map *map = NULL;
338 struct dso *dso = dso__new(name);
341 map = map__new2(0, dso, MAP__FUNCTION);
346 struct dso *dso__kernel_findnew(struct machine *machine, const char *name,
347 const char *short_name, int dso_type)
350 * The kernel dso could be created by build_id processing.
352 struct dso *dso = __dsos__findnew(&machine->kernel_dsos, name);
355 * We need to run this in all cases, since during the build_id
356 * processing we had no idea this was the kernel dso.
359 dso__set_short_name(dso, short_name);
360 dso->kernel = dso_type;
366 void dso__set_long_name(struct dso *dso, char *name)
370 dso->long_name = name;
371 dso->long_name_len = strlen(name);
374 void dso__set_short_name(struct dso *dso, const char *name)
378 dso->short_name = name;
379 dso->short_name_len = strlen(name);
382 static void dso__set_basename(struct dso *dso)
384 dso__set_short_name(dso, basename(dso->long_name));
387 int dso__name_len(const struct dso *dso)
390 return strlen("[unknown]");
392 return dso->long_name_len;
394 return dso->short_name_len;
397 bool dso__loaded(const struct dso *dso, enum map_type type)
399 return dso->loaded & (1 << type);
402 bool dso__sorted_by_name(const struct dso *dso, enum map_type type)
404 return dso->sorted_by_name & (1 << type);
407 void dso__set_sorted_by_name(struct dso *dso, enum map_type type)
409 dso->sorted_by_name |= (1 << type);
412 struct dso *dso__new(const char *name)
414 struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
418 strcpy(dso->name, name);
419 dso__set_long_name(dso, dso->name);
420 dso__set_short_name(dso, dso->name);
421 for (i = 0; i < MAP__NR_TYPES; ++i)
422 dso->symbols[i] = dso->symbol_names[i] = RB_ROOT;
423 dso->cache = RB_ROOT;
424 dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
425 dso->data_type = DSO_BINARY_TYPE__NOT_FOUND;
428 dso->sorted_by_name = 0;
429 dso->has_build_id = 0;
430 dso->kernel = DSO_TYPE_USER;
431 dso->needs_swap = DSO_SWAP__UNSET;
432 INIT_LIST_HEAD(&dso->node);
438 void dso__delete(struct dso *dso)
441 for (i = 0; i < MAP__NR_TYPES; ++i)
442 symbols__delete(&dso->symbols[i]);
443 if (dso->sname_alloc)
444 free((char *)dso->short_name);
445 if (dso->lname_alloc)
446 free(dso->long_name);
447 dso_cache__free(&dso->cache);
451 void dso__set_build_id(struct dso *dso, void *build_id)
453 memcpy(dso->build_id, build_id, sizeof(dso->build_id));
454 dso->has_build_id = 1;
457 bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
459 return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
462 void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
466 if (machine__is_default_guest(machine))
468 sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
469 if (sysfs__read_build_id(path, dso->build_id,
470 sizeof(dso->build_id)) == 0)
471 dso->has_build_id = true;
474 int dso__kernel_module_get_build_id(struct dso *dso,
475 const char *root_dir)
477 char filename[PATH_MAX];
479 * kernel module short names are of the form "[module]" and
480 * we need just "module" here.
482 const char *name = dso->short_name + 1;
484 snprintf(filename, sizeof(filename),
485 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
486 root_dir, (int)strlen(name) - 1, name);
488 if (sysfs__read_build_id(filename, dso->build_id,
489 sizeof(dso->build_id)) == 0)
490 dso->has_build_id = true;
495 bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
497 bool have_build_id = false;
500 list_for_each_entry(pos, head, node) {
501 if (with_hits && !pos->hit)
503 if (pos->has_build_id) {
504 have_build_id = true;
507 if (filename__read_build_id(pos->long_name, pos->build_id,
508 sizeof(pos->build_id)) > 0) {
509 have_build_id = true;
510 pos->has_build_id = true;
514 return have_build_id;
517 void dsos__add(struct list_head *head, struct dso *dso)
519 list_add_tail(&dso->node, head);
522 struct dso *dsos__find(struct list_head *head, const char *name, bool cmp_short)
527 list_for_each_entry(pos, head, node)
528 if (strcmp(pos->short_name, name) == 0)
532 list_for_each_entry(pos, head, node)
533 if (strcmp(pos->long_name, name) == 0)
538 struct dso *__dsos__findnew(struct list_head *head, const char *name)
540 struct dso *dso = dsos__find(head, name, false);
543 dso = dso__new(name);
545 dsos__add(head, dso);
546 dso__set_basename(dso);
553 size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
554 bool (skip)(struct dso *dso, int parm), int parm)
559 list_for_each_entry(pos, head, node) {
560 if (skip && skip(pos, parm))
562 ret += dso__fprintf_buildid(pos, fp);
563 ret += fprintf(fp, " %s\n", pos->long_name);
568 size_t __dsos__fprintf(struct list_head *head, FILE *fp)
573 list_for_each_entry(pos, head, node) {
575 for (i = 0; i < MAP__NR_TYPES; ++i)
576 ret += dso__fprintf(pos, i, fp);
582 size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
584 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
586 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
587 return fprintf(fp, "%s", sbuild_id);
590 size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp)
593 size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
595 if (dso->short_name != dso->long_name)
596 ret += fprintf(fp, "%s, ", dso->long_name);
597 ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
598 dso__loaded(dso, type) ? "" : "NOT ");
599 ret += dso__fprintf_buildid(dso, fp);
600 ret += fprintf(fp, ")\n");
601 for (nd = rb_first(&dso->symbols[type]); nd; nd = rb_next(nd)) {
602 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
603 ret += symbol__fprintf(pos, fp);