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 int __open_dso(struct dso *dso, struct machine *machine)
165 char *root_dir = (char *)"";
166 char *name = malloc(PATH_MAX);
172 root_dir = machine->root_dir;
174 if (dso__read_binary_type_filename(dso, dso->binary_type,
175 root_dir, name, PATH_MAX)) {
180 fd = open(name, O_RDONLY);
185 static void check_data_close(void);
187 static int open_dso(struct dso *dso, struct machine *machine)
189 int fd = __open_dso(dso, machine);
194 * Check if we crossed the allowed number
195 * of opened DSOs and close one if needed.
203 static void close_data_fd(struct dso *dso)
205 if (dso->data.fd >= 0) {
212 static void close_dso(struct dso *dso)
217 static void close_first_dso(void)
221 dso = list_first_entry(&dso__data_open, struct dso, data.open_entry);
225 static rlim_t get_fd_limit(void)
230 /* Allow half of the current open fd limit. */
231 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
232 if (l.rlim_cur == RLIM_INFINITY)
235 limit = l.rlim_cur / 2;
237 pr_err("failed to get fd limit\n");
244 static bool may_cache_fd(void)
249 limit = get_fd_limit();
251 if (limit == RLIM_INFINITY)
254 return limit > (rlim_t) dso__data_open_cnt;
257 static void check_data_close(void)
259 bool cache_fd = may_cache_fd();
265 void dso__data_close(struct dso *dso)
270 int dso__data_fd(struct dso *dso, struct machine *machine)
272 enum dso_binary_type binary_type_data[] = {
273 DSO_BINARY_TYPE__BUILD_ID_CACHE,
274 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
275 DSO_BINARY_TYPE__NOT_FOUND,
279 if (dso->data.fd >= 0)
282 if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) {
283 dso->data.fd = open_dso(dso, machine);
290 dso->binary_type = binary_type_data[i++];
292 fd = open_dso(dso, machine);
294 return dso->data.fd = fd;
296 } while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND);
302 dso_cache__free(struct rb_root *root)
304 struct rb_node *next = rb_first(root);
307 struct dso_cache *cache;
309 cache = rb_entry(next, struct dso_cache, rb_node);
310 next = rb_next(&cache->rb_node);
311 rb_erase(&cache->rb_node, root);
316 static struct dso_cache *dso_cache__find(const struct rb_root *root, u64 offset)
318 struct rb_node * const *p = &root->rb_node;
319 const struct rb_node *parent = NULL;
320 struct dso_cache *cache;
326 cache = rb_entry(parent, struct dso_cache, rb_node);
327 end = cache->offset + DSO__DATA_CACHE_SIZE;
329 if (offset < cache->offset)
331 else if (offset >= end)
340 dso_cache__insert(struct rb_root *root, struct dso_cache *new)
342 struct rb_node **p = &root->rb_node;
343 struct rb_node *parent = NULL;
344 struct dso_cache *cache;
345 u64 offset = new->offset;
351 cache = rb_entry(parent, struct dso_cache, rb_node);
352 end = cache->offset + DSO__DATA_CACHE_SIZE;
354 if (offset < cache->offset)
356 else if (offset >= end)
360 rb_link_node(&new->rb_node, parent, p);
361 rb_insert_color(&new->rb_node, root);
365 dso_cache__memcpy(struct dso_cache *cache, u64 offset,
368 u64 cache_offset = offset - cache->offset;
369 u64 cache_size = min(cache->size - cache_offset, size);
371 memcpy(data, cache->data + cache_offset, cache_size);
376 dso_cache__read(struct dso *dso, struct machine *machine,
377 u64 offset, u8 *data, ssize_t size)
379 struct dso_cache *cache;
383 fd = dso__data_fd(dso, machine);
392 cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
396 cache_offset = offset & DSO__DATA_CACHE_MASK;
399 if (-1 == lseek(fd, cache_offset, SEEK_SET))
402 ret = read(fd, cache->data, DSO__DATA_CACHE_SIZE);
406 cache->offset = cache_offset;
408 dso_cache__insert(&dso->data.cache, cache);
410 ret = dso_cache__memcpy(cache, offset, data, size);
420 static ssize_t dso_cache_read(struct dso *dso, struct machine *machine,
421 u64 offset, u8 *data, ssize_t size)
423 struct dso_cache *cache;
425 cache = dso_cache__find(&dso->data.cache, offset);
427 return dso_cache__memcpy(cache, offset, data, size);
429 return dso_cache__read(dso, machine, offset, data, size);
432 ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
433 u64 offset, u8 *data, ssize_t size)
441 ret = dso_cache_read(dso, machine, offset, p, size);
445 /* Reached EOF, return what we have. */
461 ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
462 struct machine *machine, u64 addr,
463 u8 *data, ssize_t size)
465 u64 offset = map->map_ip(map, addr);
466 return dso__data_read_offset(dso, machine, offset, data, size);
469 struct map *dso__new_map(const char *name)
471 struct map *map = NULL;
472 struct dso *dso = dso__new(name);
475 map = map__new2(0, dso, MAP__FUNCTION);
480 struct dso *dso__kernel_findnew(struct machine *machine, const char *name,
481 const char *short_name, int dso_type)
484 * The kernel dso could be created by build_id processing.
486 struct dso *dso = __dsos__findnew(&machine->kernel_dsos, name);
489 * We need to run this in all cases, since during the build_id
490 * processing we had no idea this was the kernel dso.
493 dso__set_short_name(dso, short_name, false);
494 dso->kernel = dso_type;
500 void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
505 if (dso->long_name_allocated)
506 free((char *)dso->long_name);
508 dso->long_name = name;
509 dso->long_name_len = strlen(name);
510 dso->long_name_allocated = name_allocated;
513 void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
518 if (dso->short_name_allocated)
519 free((char *)dso->short_name);
521 dso->short_name = name;
522 dso->short_name_len = strlen(name);
523 dso->short_name_allocated = name_allocated;
526 static void dso__set_basename(struct dso *dso)
529 * basename() may modify path buffer, so we must pass
532 char *base, *lname = strdup(dso->long_name);
538 * basename() may return a pointer to internal
539 * storage which is reused in subsequent calls
540 * so copy the result.
542 base = strdup(basename(lname));
549 dso__set_short_name(dso, base, true);
552 int dso__name_len(const struct dso *dso)
555 return strlen("[unknown]");
557 return dso->long_name_len;
559 return dso->short_name_len;
562 bool dso__loaded(const struct dso *dso, enum map_type type)
564 return dso->loaded & (1 << type);
567 bool dso__sorted_by_name(const struct dso *dso, enum map_type type)
569 return dso->sorted_by_name & (1 << type);
572 void dso__set_sorted_by_name(struct dso *dso, enum map_type type)
574 dso->sorted_by_name |= (1 << type);
577 struct dso *dso__new(const char *name)
579 struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
583 strcpy(dso->name, name);
584 dso__set_long_name(dso, dso->name, false);
585 dso__set_short_name(dso, dso->name, false);
586 for (i = 0; i < MAP__NR_TYPES; ++i)
587 dso->symbols[i] = dso->symbol_names[i] = RB_ROOT;
588 dso->data.cache = RB_ROOT;
590 dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
591 dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
594 dso->sorted_by_name = 0;
595 dso->has_build_id = 0;
596 dso->has_srcline = 1;
598 dso->kernel = DSO_TYPE_USER;
599 dso->needs_swap = DSO_SWAP__UNSET;
600 INIT_LIST_HEAD(&dso->node);
601 INIT_LIST_HEAD(&dso->data.open_entry);
607 void dso__delete(struct dso *dso)
610 for (i = 0; i < MAP__NR_TYPES; ++i)
611 symbols__delete(&dso->symbols[i]);
613 if (dso->short_name_allocated) {
614 zfree((char **)&dso->short_name);
615 dso->short_name_allocated = false;
618 if (dso->long_name_allocated) {
619 zfree((char **)&dso->long_name);
620 dso->long_name_allocated = false;
623 dso__data_close(dso);
624 dso_cache__free(&dso->data.cache);
626 zfree(&dso->symsrc_filename);
630 void dso__set_build_id(struct dso *dso, void *build_id)
632 memcpy(dso->build_id, build_id, sizeof(dso->build_id));
633 dso->has_build_id = 1;
636 bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
638 return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
641 void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
645 if (machine__is_default_guest(machine))
647 sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
648 if (sysfs__read_build_id(path, dso->build_id,
649 sizeof(dso->build_id)) == 0)
650 dso->has_build_id = true;
653 int dso__kernel_module_get_build_id(struct dso *dso,
654 const char *root_dir)
656 char filename[PATH_MAX];
658 * kernel module short names are of the form "[module]" and
659 * we need just "module" here.
661 const char *name = dso->short_name + 1;
663 snprintf(filename, sizeof(filename),
664 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
665 root_dir, (int)strlen(name) - 1, name);
667 if (sysfs__read_build_id(filename, dso->build_id,
668 sizeof(dso->build_id)) == 0)
669 dso->has_build_id = true;
674 bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
676 bool have_build_id = false;
679 list_for_each_entry(pos, head, node) {
680 if (with_hits && !pos->hit)
682 if (pos->has_build_id) {
683 have_build_id = true;
686 if (filename__read_build_id(pos->long_name, pos->build_id,
687 sizeof(pos->build_id)) > 0) {
688 have_build_id = true;
689 pos->has_build_id = true;
693 return have_build_id;
696 void dsos__add(struct list_head *head, struct dso *dso)
698 list_add_tail(&dso->node, head);
701 struct dso *dsos__find(const struct list_head *head, const char *name, bool cmp_short)
706 list_for_each_entry(pos, head, node)
707 if (strcmp(pos->short_name, name) == 0)
711 list_for_each_entry(pos, head, node)
712 if (strcmp(pos->long_name, name) == 0)
717 struct dso *__dsos__findnew(struct list_head *head, const char *name)
719 struct dso *dso = dsos__find(head, name, false);
722 dso = dso__new(name);
724 dsos__add(head, dso);
725 dso__set_basename(dso);
732 size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
733 bool (skip)(struct dso *dso, int parm), int parm)
738 list_for_each_entry(pos, head, node) {
739 if (skip && skip(pos, parm))
741 ret += dso__fprintf_buildid(pos, fp);
742 ret += fprintf(fp, " %s\n", pos->long_name);
747 size_t __dsos__fprintf(struct list_head *head, FILE *fp)
752 list_for_each_entry(pos, head, node) {
754 for (i = 0; i < MAP__NR_TYPES; ++i)
755 ret += dso__fprintf(pos, i, fp);
761 size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
763 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
765 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
766 return fprintf(fp, "%s", sbuild_id);
769 size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp)
772 size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
774 if (dso->short_name != dso->long_name)
775 ret += fprintf(fp, "%s, ", dso->long_name);
776 ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
777 dso__loaded(dso, type) ? "" : "NOT ");
778 ret += dso__fprintf_buildid(dso, fp);
779 ret += fprintf(fp, ")\n");
780 for (nd = rb_first(&dso->symbols[type]); nd; nd = rb_next(nd)) {
781 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
782 ret += symbol__fprintf(pos, fp);