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 void close_first_dso(void);
164 static int do_open(char *name)
169 fd = open(name, O_RDONLY);
173 pr_debug("dso open failed, mmap: %s\n", strerror(errno));
174 if (!dso__data_open_cnt || errno != EMFILE)
183 static int __open_dso(struct dso *dso, struct machine *machine)
186 char *root_dir = (char *)"";
187 char *name = malloc(PATH_MAX);
193 root_dir = machine->root_dir;
195 if (dso__read_binary_type_filename(dso, dso->binary_type,
196 root_dir, name, PATH_MAX)) {
206 static void check_data_close(void);
209 * dso_close - Open DSO data file
212 * Open @dso's data file descriptor and updates
213 * list/count of open DSO objects.
215 static int open_dso(struct dso *dso, struct machine *machine)
217 int fd = __open_dso(dso, machine);
222 * Check if we crossed the allowed number
223 * of opened DSOs and close one if needed.
231 static void close_data_fd(struct dso *dso)
233 if (dso->data.fd >= 0) {
236 dso->data.file_size = 0;
242 * dso_close - Close DSO data file
245 * Close @dso's data file descriptor and updates
246 * list/count of open DSO objects.
248 static void close_dso(struct dso *dso)
253 static void close_first_dso(void)
257 dso = list_first_entry(&dso__data_open, struct dso, data.open_entry);
261 static rlim_t get_fd_limit(void)
266 /* Allow half of the current open fd limit. */
267 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
268 if (l.rlim_cur == RLIM_INFINITY)
271 limit = l.rlim_cur / 2;
273 pr_err("failed to get fd limit\n");
280 static bool may_cache_fd(void)
285 limit = get_fd_limit();
287 if (limit == RLIM_INFINITY)
290 return limit > (rlim_t) dso__data_open_cnt;
294 * Check and close LRU dso if we crossed allowed limit
295 * for opened dso file descriptors. The limit is half
296 * of the RLIMIT_NOFILE files opened.
298 static void check_data_close(void)
300 bool cache_fd = may_cache_fd();
307 * dso__data_close - Close DSO data file
310 * External interface to close @dso's data file descriptor.
312 void dso__data_close(struct dso *dso)
318 * dso__data_fd - Get dso's data file descriptor
320 * @machine: machine object
322 * External interface to find dso's file, open it and
323 * returns file descriptor.
325 int dso__data_fd(struct dso *dso, struct machine *machine)
327 enum dso_binary_type binary_type_data[] = {
328 DSO_BINARY_TYPE__BUILD_ID_CACHE,
329 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
330 DSO_BINARY_TYPE__NOT_FOUND,
334 if (dso->data.status == DSO_DATA_STATUS_ERROR)
337 if (dso->data.fd >= 0)
340 if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) {
341 dso->data.fd = open_dso(dso, machine);
346 dso->binary_type = binary_type_data[i++];
348 dso->data.fd = open_dso(dso, machine);
349 if (dso->data.fd >= 0)
352 } while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND);
354 if (dso->data.fd >= 0)
355 dso->data.status = DSO_DATA_STATUS_OK;
357 dso->data.status = DSO_DATA_STATUS_ERROR;
362 bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by)
366 if (dso->data.status_seen & flag)
369 dso->data.status_seen |= flag;
375 dso_cache__free(struct rb_root *root)
377 struct rb_node *next = rb_first(root);
380 struct dso_cache *cache;
382 cache = rb_entry(next, struct dso_cache, rb_node);
383 next = rb_next(&cache->rb_node);
384 rb_erase(&cache->rb_node, root);
389 static struct dso_cache *dso_cache__find(const struct rb_root *root, u64 offset)
391 struct rb_node * const *p = &root->rb_node;
392 const struct rb_node *parent = NULL;
393 struct dso_cache *cache;
399 cache = rb_entry(parent, struct dso_cache, rb_node);
400 end = cache->offset + DSO__DATA_CACHE_SIZE;
402 if (offset < cache->offset)
404 else if (offset >= end)
413 dso_cache__insert(struct rb_root *root, struct dso_cache *new)
415 struct rb_node **p = &root->rb_node;
416 struct rb_node *parent = NULL;
417 struct dso_cache *cache;
418 u64 offset = new->offset;
424 cache = rb_entry(parent, struct dso_cache, rb_node);
425 end = cache->offset + DSO__DATA_CACHE_SIZE;
427 if (offset < cache->offset)
429 else if (offset >= end)
433 rb_link_node(&new->rb_node, parent, p);
434 rb_insert_color(&new->rb_node, root);
438 dso_cache__memcpy(struct dso_cache *cache, u64 offset,
441 u64 cache_offset = offset - cache->offset;
442 u64 cache_size = min(cache->size - cache_offset, size);
444 memcpy(data, cache->data + cache_offset, cache_size);
449 dso_cache__read(struct dso *dso, u64 offset, u8 *data, ssize_t size)
451 struct dso_cache *cache;
459 cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
463 cache_offset = offset & DSO__DATA_CACHE_MASK;
466 if (-1 == lseek(dso->data.fd, cache_offset, SEEK_SET))
469 ret = read(dso->data.fd, cache->data, DSO__DATA_CACHE_SIZE);
473 cache->offset = cache_offset;
475 dso_cache__insert(&dso->data.cache, cache);
477 ret = dso_cache__memcpy(cache, offset, data, size);
487 static ssize_t dso_cache_read(struct dso *dso, u64 offset,
488 u8 *data, ssize_t size)
490 struct dso_cache *cache;
492 cache = dso_cache__find(&dso->data.cache, offset);
494 return dso_cache__memcpy(cache, offset, data, size);
496 return dso_cache__read(dso, offset, data, size);
500 * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
501 * in the rb_tree. Any read to already cached data is served
504 static ssize_t cached_read(struct dso *dso, u64 offset, u8 *data, ssize_t size)
512 ret = dso_cache_read(dso, offset, p, size);
516 /* Reached EOF, return what we have. */
532 static int data_file_size(struct dso *dso)
536 if (!dso->data.file_size) {
537 if (fstat(dso->data.fd, &st)) {
538 pr_err("dso mmap failed, fstat: %s\n", strerror(errno));
541 dso->data.file_size = st.st_size;
548 * dso__data_size - Return dso data size
550 * @machine: machine object
552 * Return: dso data size
554 off_t dso__data_size(struct dso *dso, struct machine *machine)
558 fd = dso__data_fd(dso, machine);
562 if (data_file_size(dso))
565 /* For now just estimate dso data size is close to file size */
566 return dso->data.file_size;
569 static ssize_t data_read_offset(struct dso *dso, u64 offset,
570 u8 *data, ssize_t size)
572 if (data_file_size(dso))
575 /* Check the offset sanity. */
576 if (offset > dso->data.file_size)
579 if (offset + size < offset)
582 return cached_read(dso, offset, data, size);
586 * dso__data_read_offset - Read data from dso file offset
588 * @machine: machine object
589 * @offset: file offset
590 * @data: buffer to store data
591 * @size: size of the @data buffer
593 * External interface to read data from dso file offset. Open
594 * dso data file and use cached_read to get the data.
596 ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
597 u64 offset, u8 *data, ssize_t size)
599 if (dso__data_fd(dso, machine) < 0)
602 return data_read_offset(dso, offset, data, size);
606 * dso__data_read_addr - Read data from dso address
608 * @machine: machine object
609 * @add: virtual memory address
610 * @data: buffer to store data
611 * @size: size of the @data buffer
613 * External interface to read data from dso address.
615 ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
616 struct machine *machine, u64 addr,
617 u8 *data, ssize_t size)
619 u64 offset = map->map_ip(map, addr);
620 return dso__data_read_offset(dso, machine, offset, data, size);
623 struct map *dso__new_map(const char *name)
625 struct map *map = NULL;
626 struct dso *dso = dso__new(name);
629 map = map__new2(0, dso, MAP__FUNCTION);
634 struct dso *dso__kernel_findnew(struct machine *machine, const char *name,
635 const char *short_name, int dso_type)
638 * The kernel dso could be created by build_id processing.
640 struct dso *dso = __dsos__findnew(&machine->kernel_dsos, name);
643 * We need to run this in all cases, since during the build_id
644 * processing we had no idea this was the kernel dso.
647 dso__set_short_name(dso, short_name, false);
648 dso->kernel = dso_type;
654 void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
659 if (dso->long_name_allocated)
660 free((char *)dso->long_name);
662 dso->long_name = name;
663 dso->long_name_len = strlen(name);
664 dso->long_name_allocated = name_allocated;
667 void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
672 if (dso->short_name_allocated)
673 free((char *)dso->short_name);
675 dso->short_name = name;
676 dso->short_name_len = strlen(name);
677 dso->short_name_allocated = name_allocated;
680 static void dso__set_basename(struct dso *dso)
683 * basename() may modify path buffer, so we must pass
686 char *base, *lname = strdup(dso->long_name);
692 * basename() may return a pointer to internal
693 * storage which is reused in subsequent calls
694 * so copy the result.
696 base = strdup(basename(lname));
703 dso__set_short_name(dso, base, true);
706 int dso__name_len(const struct dso *dso)
709 return strlen("[unknown]");
711 return dso->long_name_len;
713 return dso->short_name_len;
716 bool dso__loaded(const struct dso *dso, enum map_type type)
718 return dso->loaded & (1 << type);
721 bool dso__sorted_by_name(const struct dso *dso, enum map_type type)
723 return dso->sorted_by_name & (1 << type);
726 void dso__set_sorted_by_name(struct dso *dso, enum map_type type)
728 dso->sorted_by_name |= (1 << type);
731 struct dso *dso__new(const char *name)
733 struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
737 strcpy(dso->name, name);
738 dso__set_long_name(dso, dso->name, false);
739 dso__set_short_name(dso, dso->name, false);
740 for (i = 0; i < MAP__NR_TYPES; ++i)
741 dso->symbols[i] = dso->symbol_names[i] = RB_ROOT;
742 dso->data.cache = RB_ROOT;
744 dso->data.status = DSO_DATA_STATUS_UNKNOWN;
745 dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
746 dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
747 dso->is_64_bit = (sizeof(void *) == 8);
750 dso->sorted_by_name = 0;
751 dso->has_build_id = 0;
752 dso->has_srcline = 1;
754 dso->kernel = DSO_TYPE_USER;
755 dso->needs_swap = DSO_SWAP__UNSET;
756 INIT_LIST_HEAD(&dso->node);
757 INIT_LIST_HEAD(&dso->data.open_entry);
763 void dso__delete(struct dso *dso)
766 for (i = 0; i < MAP__NR_TYPES; ++i)
767 symbols__delete(&dso->symbols[i]);
769 if (dso->short_name_allocated) {
770 zfree((char **)&dso->short_name);
771 dso->short_name_allocated = false;
774 if (dso->long_name_allocated) {
775 zfree((char **)&dso->long_name);
776 dso->long_name_allocated = false;
779 dso__data_close(dso);
780 dso_cache__free(&dso->data.cache);
782 zfree(&dso->symsrc_filename);
786 void dso__set_build_id(struct dso *dso, void *build_id)
788 memcpy(dso->build_id, build_id, sizeof(dso->build_id));
789 dso->has_build_id = 1;
792 bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
794 return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
797 void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
801 if (machine__is_default_guest(machine))
803 sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
804 if (sysfs__read_build_id(path, dso->build_id,
805 sizeof(dso->build_id)) == 0)
806 dso->has_build_id = true;
809 int dso__kernel_module_get_build_id(struct dso *dso,
810 const char *root_dir)
812 char filename[PATH_MAX];
814 * kernel module short names are of the form "[module]" and
815 * we need just "module" here.
817 const char *name = dso->short_name + 1;
819 snprintf(filename, sizeof(filename),
820 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
821 root_dir, (int)strlen(name) - 1, name);
823 if (sysfs__read_build_id(filename, dso->build_id,
824 sizeof(dso->build_id)) == 0)
825 dso->has_build_id = true;
830 bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
832 bool have_build_id = false;
835 list_for_each_entry(pos, head, node) {
836 if (with_hits && !pos->hit)
838 if (pos->has_build_id) {
839 have_build_id = true;
842 if (filename__read_build_id(pos->long_name, pos->build_id,
843 sizeof(pos->build_id)) > 0) {
844 have_build_id = true;
845 pos->has_build_id = true;
849 return have_build_id;
852 void dsos__add(struct list_head *head, struct dso *dso)
854 list_add_tail(&dso->node, head);
857 struct dso *dsos__find(const struct list_head *head, const char *name, bool cmp_short)
862 list_for_each_entry(pos, head, node)
863 if (strcmp(pos->short_name, name) == 0)
867 list_for_each_entry(pos, head, node)
868 if (strcmp(pos->long_name, name) == 0)
873 struct dso *__dsos__findnew(struct list_head *head, const char *name)
875 struct dso *dso = dsos__find(head, name, false);
878 dso = dso__new(name);
880 dsos__add(head, dso);
881 dso__set_basename(dso);
888 size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
889 bool (skip)(struct dso *dso, int parm), int parm)
894 list_for_each_entry(pos, head, node) {
895 if (skip && skip(pos, parm))
897 ret += dso__fprintf_buildid(pos, fp);
898 ret += fprintf(fp, " %s\n", pos->long_name);
903 size_t __dsos__fprintf(struct list_head *head, FILE *fp)
908 list_for_each_entry(pos, head, node) {
910 for (i = 0; i < MAP__NR_TYPES; ++i)
911 ret += dso__fprintf(pos, i, fp);
917 size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
919 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
921 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
922 return fprintf(fp, "%s", sbuild_id);
925 size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp)
928 size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
930 if (dso->short_name != dso->long_name)
931 ret += fprintf(fp, "%s, ", dso->long_name);
932 ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
933 dso__loaded(dso, type) ? "" : "NOT ");
934 ret += dso__fprintf_buildid(dso, fp);
935 ret += fprintf(fp, ")\n");
936 for (nd = rb_first(&dso->symbols[type]); nd; nd = rb_next(nd)) {
937 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
938 ret += symbol__fprintf(pos, fp);
944 enum dso_type dso__type(struct dso *dso, struct machine *machine)
948 fd = dso__data_fd(dso, machine);
950 return DSO__TYPE_UNKNOWN;
952 return dso__type_fd(fd);