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__SYSTEM_PATH_DSO:
82 snprintf(file, size, "%s%s",
83 symbol_conf.symfs, dso->long_name);
86 case DSO_BINARY_TYPE__GUEST_KMODULE:
87 snprintf(file, size, "%s%s%s", symbol_conf.symfs,
88 root_dir, dso->long_name);
91 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
92 snprintf(file, size, "%s%s", symbol_conf.symfs,
97 case DSO_BINARY_TYPE__KALLSYMS:
98 case DSO_BINARY_TYPE__VMLINUX:
99 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
100 case DSO_BINARY_TYPE__GUEST_VMLINUX:
101 case DSO_BINARY_TYPE__JAVA_JIT:
102 case DSO_BINARY_TYPE__NOT_FOUND:
110 static int open_dso(struct dso *dso, struct machine *machine)
112 char *root_dir = (char *) "";
116 name = malloc(PATH_MAX);
121 root_dir = machine->root_dir;
123 if (dso__binary_type_file(dso, dso->data_type,
124 root_dir, name, PATH_MAX)) {
129 fd = open(name, O_RDONLY);
134 int dso__data_fd(struct dso *dso, struct machine *machine)
136 static enum dso_binary_type binary_type_data[] = {
137 DSO_BINARY_TYPE__BUILD_ID_CACHE,
138 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
139 DSO_BINARY_TYPE__NOT_FOUND,
143 if (dso->data_type != DSO_BINARY_TYPE__NOT_FOUND)
144 return open_dso(dso, machine);
149 dso->data_type = binary_type_data[i++];
151 fd = open_dso(dso, machine);
155 } while (dso->data_type != DSO_BINARY_TYPE__NOT_FOUND);
161 dso_cache__free(struct rb_root *root)
163 struct rb_node *next = rb_first(root);
166 struct dso_cache *cache;
168 cache = rb_entry(next, struct dso_cache, rb_node);
169 next = rb_next(&cache->rb_node);
170 rb_erase(&cache->rb_node, root);
175 static struct dso_cache*
176 dso_cache__find(struct rb_root *root, u64 offset)
178 struct rb_node **p = &root->rb_node;
179 struct rb_node *parent = NULL;
180 struct dso_cache *cache;
186 cache = rb_entry(parent, struct dso_cache, rb_node);
187 end = cache->offset + DSO__DATA_CACHE_SIZE;
189 if (offset < cache->offset)
191 else if (offset >= end)
200 dso_cache__insert(struct rb_root *root, struct dso_cache *new)
202 struct rb_node **p = &root->rb_node;
203 struct rb_node *parent = NULL;
204 struct dso_cache *cache;
205 u64 offset = new->offset;
211 cache = rb_entry(parent, struct dso_cache, rb_node);
212 end = cache->offset + DSO__DATA_CACHE_SIZE;
214 if (offset < cache->offset)
216 else if (offset >= end)
220 rb_link_node(&new->rb_node, parent, p);
221 rb_insert_color(&new->rb_node, root);
225 dso_cache__memcpy(struct dso_cache *cache, u64 offset,
228 u64 cache_offset = offset - cache->offset;
229 u64 cache_size = min(cache->size - cache_offset, size);
231 memcpy(data, cache->data + cache_offset, cache_size);
236 dso_cache__read(struct dso *dso, struct machine *machine,
237 u64 offset, u8 *data, ssize_t size)
239 struct dso_cache *cache;
243 fd = dso__data_fd(dso, machine);
252 cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
256 cache_offset = offset & DSO__DATA_CACHE_MASK;
259 if (-1 == lseek(fd, cache_offset, SEEK_SET))
262 ret = read(fd, cache->data, DSO__DATA_CACHE_SIZE);
266 cache->offset = cache_offset;
268 dso_cache__insert(&dso->cache, cache);
270 ret = dso_cache__memcpy(cache, offset, data, size);
281 static ssize_t dso_cache_read(struct dso *dso, struct machine *machine,
282 u64 offset, u8 *data, ssize_t size)
284 struct dso_cache *cache;
286 cache = dso_cache__find(&dso->cache, offset);
288 return dso_cache__memcpy(cache, offset, data, size);
290 return dso_cache__read(dso, machine, offset, data, size);
293 ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
294 u64 offset, u8 *data, ssize_t size)
302 ret = dso_cache_read(dso, machine, offset, p, size);
306 /* Reached EOF, return what we have. */
322 ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
323 struct machine *machine, u64 addr,
324 u8 *data, ssize_t size)
326 u64 offset = map->map_ip(map, addr);
327 return dso__data_read_offset(dso, machine, offset, data, size);
330 struct map *dso__new_map(const char *name)
332 struct map *map = NULL;
333 struct dso *dso = dso__new(name);
336 map = map__new2(0, dso, MAP__FUNCTION);
341 struct dso *dso__kernel_findnew(struct machine *machine, const char *name,
342 const char *short_name, int dso_type)
345 * The kernel dso could be created by build_id processing.
347 struct dso *dso = __dsos__findnew(&machine->kernel_dsos, name);
350 * We need to run this in all cases, since during the build_id
351 * processing we had no idea this was the kernel dso.
354 dso__set_short_name(dso, short_name);
355 dso->kernel = dso_type;
361 void dso__set_long_name(struct dso *dso, char *name)
365 dso->long_name = name;
366 dso->long_name_len = strlen(name);
369 void dso__set_short_name(struct dso *dso, const char *name)
373 dso->short_name = name;
374 dso->short_name_len = strlen(name);
377 static void dso__set_basename(struct dso *dso)
379 dso__set_short_name(dso, basename(dso->long_name));
382 int dso__name_len(const struct dso *dso)
385 return strlen("[unknown]");
387 return dso->long_name_len;
389 return dso->short_name_len;
392 bool dso__loaded(const struct dso *dso, enum map_type type)
394 return dso->loaded & (1 << type);
397 bool dso__sorted_by_name(const struct dso *dso, enum map_type type)
399 return dso->sorted_by_name & (1 << type);
402 void dso__set_sorted_by_name(struct dso *dso, enum map_type type)
404 dso->sorted_by_name |= (1 << type);
407 struct dso *dso__new(const char *name)
409 struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
413 strcpy(dso->name, name);
414 dso__set_long_name(dso, dso->name);
415 dso__set_short_name(dso, dso->name);
416 for (i = 0; i < MAP__NR_TYPES; ++i)
417 dso->symbols[i] = dso->symbol_names[i] = RB_ROOT;
418 dso->cache = RB_ROOT;
419 dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
420 dso->data_type = DSO_BINARY_TYPE__NOT_FOUND;
422 dso->sorted_by_name = 0;
423 dso->has_build_id = 0;
424 dso->kernel = DSO_TYPE_USER;
425 dso->needs_swap = DSO_SWAP__UNSET;
426 INIT_LIST_HEAD(&dso->node);
432 void dso__delete(struct dso *dso)
435 for (i = 0; i < MAP__NR_TYPES; ++i)
436 symbols__delete(&dso->symbols[i]);
437 if (dso->sname_alloc)
438 free((char *)dso->short_name);
439 if (dso->lname_alloc)
440 free(dso->long_name);
441 dso_cache__free(&dso->cache);
445 void dso__set_build_id(struct dso *dso, void *build_id)
447 memcpy(dso->build_id, build_id, sizeof(dso->build_id));
448 dso->has_build_id = 1;
451 bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
453 return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
456 void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
460 if (machine__is_default_guest(machine))
462 sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
463 if (sysfs__read_build_id(path, dso->build_id,
464 sizeof(dso->build_id)) == 0)
465 dso->has_build_id = true;
468 int dso__kernel_module_get_build_id(struct dso *dso,
469 const char *root_dir)
471 char filename[PATH_MAX];
473 * kernel module short names are of the form "[module]" and
474 * we need just "module" here.
476 const char *name = dso->short_name + 1;
478 snprintf(filename, sizeof(filename),
479 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
480 root_dir, (int)strlen(name) - 1, name);
482 if (sysfs__read_build_id(filename, dso->build_id,
483 sizeof(dso->build_id)) == 0)
484 dso->has_build_id = true;
489 bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
491 bool have_build_id = false;
494 list_for_each_entry(pos, head, node) {
495 if (with_hits && !pos->hit)
497 if (pos->has_build_id) {
498 have_build_id = true;
501 if (filename__read_build_id(pos->long_name, pos->build_id,
502 sizeof(pos->build_id)) > 0) {
503 have_build_id = true;
504 pos->has_build_id = true;
508 return have_build_id;
511 void dsos__add(struct list_head *head, struct dso *dso)
513 list_add_tail(&dso->node, head);
516 struct dso *dsos__find(struct list_head *head, const char *name, bool cmp_short)
521 list_for_each_entry(pos, head, node)
522 if (strcmp(pos->short_name, name) == 0)
526 list_for_each_entry(pos, head, node)
527 if (strcmp(pos->long_name, name) == 0)
532 struct dso *__dsos__findnew(struct list_head *head, const char *name)
534 struct dso *dso = dsos__find(head, name, false);
537 dso = dso__new(name);
539 dsos__add(head, dso);
540 dso__set_basename(dso);
547 size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
548 bool (skip)(struct dso *dso, int parm), int parm)
553 list_for_each_entry(pos, head, node) {
554 if (skip && skip(pos, parm))
556 ret += dso__fprintf_buildid(pos, fp);
557 ret += fprintf(fp, " %s\n", pos->long_name);
562 size_t __dsos__fprintf(struct list_head *head, FILE *fp)
567 list_for_each_entry(pos, head, node) {
569 for (i = 0; i < MAP__NR_TYPES; ++i)
570 ret += dso__fprintf(pos, i, fp);
576 size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
578 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
580 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
581 return fprintf(fp, "%s", sbuild_id);
584 size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp)
587 size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
589 if (dso->short_name != dso->long_name)
590 ret += fprintf(fp, "%s, ", dso->long_name);
591 ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
592 dso__loaded(dso, type) ? "" : "NOT ");
593 ret += dso__fprintf_buildid(dso, fp);
594 ret += fprintf(fp, ")\n");
595 for (nd = rb_first(&dso->symbols[type]); nd; nd = rb_next(nd)) {
596 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
597 ret += symbol__fprintf(pos, fp);