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];
43 case DSO_BINARY_TYPE__DEBUGLINK: {
46 strncpy(filename, dso->long_name, size);
47 debuglink = filename + dso->long_name_len;
48 while (debuglink != filename && *debuglink != '/')
50 if (*debuglink == '/')
52 ret = filename__read_debuglink(dso->long_name, debuglink,
53 size - (debuglink - filename));
56 case DSO_BINARY_TYPE__BUILD_ID_CACHE:
57 /* skip the locally configured cache if a symfs is given */
58 if (symbol_conf.symfs[0] ||
59 (dso__build_id_filename(dso, filename, size) == NULL))
63 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
64 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
65 snprintf(filename + len, size - len, "%s.debug", dso->long_name);
68 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
69 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
70 snprintf(filename + len, size - len, "%s", dso->long_name);
73 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
75 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 = __symbol__join_symfs(filename, size, "");
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 len = __symbol__join_symfs(filename, size, "/usr/lib/debug/.build-id/");
104 snprintf(filename + len, size - len, "%.2s/%s.debug",
105 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 __symbol__join_symfs(filename, size, dso->long_name);
114 case DSO_BINARY_TYPE__GUEST_KMODULE:
115 path__join3(filename, size, symbol_conf.symfs,
116 root_dir, dso->long_name);
119 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
120 __symbol__join_symfs(filename, size, dso->long_name);
123 case DSO_BINARY_TYPE__KCORE:
124 case DSO_BINARY_TYPE__GUEST_KCORE:
125 snprintf(filename, size, "%s", dso->long_name);
129 case DSO_BINARY_TYPE__KALLSYMS:
130 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
131 case DSO_BINARY_TYPE__JAVA_JIT:
132 case DSO_BINARY_TYPE__NOT_FOUND:
141 * Global list of open DSOs and the counter.
143 static LIST_HEAD(dso__data_open);
144 static long dso__data_open_cnt;
146 static void dso__list_add(struct dso *dso)
148 list_add_tail(&dso->data.open_entry, &dso__data_open);
149 dso__data_open_cnt++;
152 static void dso__list_del(struct dso *dso)
154 list_del(&dso->data.open_entry);
155 WARN_ONCE(dso__data_open_cnt <= 0,
156 "DSO data fd counter out of bounds.");
157 dso__data_open_cnt--;
160 static void close_first_dso(void);
162 static int do_open(char *name)
165 char sbuf[STRERR_BUFSIZE];
168 fd = open(name, O_RDONLY);
172 pr_debug("dso open failed, mmap: %s\n",
173 strerror_r(errno, sbuf, sizeof(sbuf)));
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)
535 char sbuf[STRERR_BUFSIZE];
537 if (!dso->data.file_size) {
538 if (fstat(dso->data.fd, &st)) {
539 pr_err("dso mmap failed, fstat: %s\n",
540 strerror_r(errno, sbuf, sizeof(sbuf)));
543 dso->data.file_size = st.st_size;
550 * dso__data_size - Return dso data size
552 * @machine: machine object
554 * Return: dso data size
556 off_t dso__data_size(struct dso *dso, struct machine *machine)
560 fd = dso__data_fd(dso, machine);
564 if (data_file_size(dso))
567 /* For now just estimate dso data size is close to file size */
568 return dso->data.file_size;
571 static ssize_t data_read_offset(struct dso *dso, u64 offset,
572 u8 *data, ssize_t size)
574 if (data_file_size(dso))
577 /* Check the offset sanity. */
578 if (offset > dso->data.file_size)
581 if (offset + size < offset)
584 return cached_read(dso, offset, data, size);
588 * dso__data_read_offset - Read data from dso file offset
590 * @machine: machine object
591 * @offset: file offset
592 * @data: buffer to store data
593 * @size: size of the @data buffer
595 * External interface to read data from dso file offset. Open
596 * dso data file and use cached_read to get the data.
598 ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
599 u64 offset, u8 *data, ssize_t size)
601 if (dso__data_fd(dso, machine) < 0)
604 return data_read_offset(dso, offset, data, size);
608 * dso__data_read_addr - Read data from dso address
610 * @machine: machine object
611 * @add: virtual memory address
612 * @data: buffer to store data
613 * @size: size of the @data buffer
615 * External interface to read data from dso address.
617 ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
618 struct machine *machine, u64 addr,
619 u8 *data, ssize_t size)
621 u64 offset = map->map_ip(map, addr);
622 return dso__data_read_offset(dso, machine, offset, data, size);
625 struct map *dso__new_map(const char *name)
627 struct map *map = NULL;
628 struct dso *dso = dso__new(name);
631 map = map__new2(0, dso, MAP__FUNCTION);
636 struct dso *dso__kernel_findnew(struct machine *machine, const char *name,
637 const char *short_name, int dso_type)
640 * The kernel dso could be created by build_id processing.
642 struct dso *dso = __dsos__findnew(&machine->kernel_dsos, name);
645 * We need to run this in all cases, since during the build_id
646 * processing we had no idea this was the kernel dso.
649 dso__set_short_name(dso, short_name, false);
650 dso->kernel = dso_type;
657 * Find a matching entry and/or link current entry to RB tree.
658 * Either one of the dso or name parameter must be non-NULL or the
659 * function will not work.
661 static struct dso *dso__findlink_by_longname(struct rb_root *root,
662 struct dso *dso, const char *name)
664 struct rb_node **p = &root->rb_node;
665 struct rb_node *parent = NULL;
668 name = dso->long_name;
670 * Find node with the matching name
673 struct dso *this = rb_entry(*p, struct dso, rb_node);
674 int rc = strcmp(name, this->long_name);
679 * In case the new DSO is a duplicate of an existing
680 * one, print an one-time warning & put the new entry
681 * at the end of the list of duplicates.
683 if (!dso || (dso == this))
684 return this; /* Find matching dso */
686 * The core kernel DSOs may have duplicated long name.
687 * In this case, the short name should be different.
688 * Comparing the short names to differentiate the DSOs.
690 rc = strcmp(dso->short_name, this->short_name);
692 pr_err("Duplicated dso name: %s\n", name);
697 p = &parent->rb_left;
699 p = &parent->rb_right;
702 /* Add new node and rebalance tree */
703 rb_link_node(&dso->rb_node, parent, p);
704 rb_insert_color(&dso->rb_node, root);
709 static inline struct dso *
710 dso__find_by_longname(const struct rb_root *root, const char *name)
712 return dso__findlink_by_longname((struct rb_root *)root, NULL, name);
715 void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
720 if (dso->long_name_allocated)
721 free((char *)dso->long_name);
723 dso->long_name = name;
724 dso->long_name_len = strlen(name);
725 dso->long_name_allocated = name_allocated;
728 void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
733 if (dso->short_name_allocated)
734 free((char *)dso->short_name);
736 dso->short_name = name;
737 dso->short_name_len = strlen(name);
738 dso->short_name_allocated = name_allocated;
741 static void dso__set_basename(struct dso *dso)
744 * basename() may modify path buffer, so we must pass
747 char *base, *lname = strdup(dso->long_name);
753 * basename() may return a pointer to internal
754 * storage which is reused in subsequent calls
755 * so copy the result.
757 base = strdup(basename(lname));
764 dso__set_short_name(dso, base, true);
767 int dso__name_len(const struct dso *dso)
770 return strlen("[unknown]");
772 return dso->long_name_len;
774 return dso->short_name_len;
777 bool dso__loaded(const struct dso *dso, enum map_type type)
779 return dso->loaded & (1 << type);
782 bool dso__sorted_by_name(const struct dso *dso, enum map_type type)
784 return dso->sorted_by_name & (1 << type);
787 void dso__set_sorted_by_name(struct dso *dso, enum map_type type)
789 dso->sorted_by_name |= (1 << type);
792 struct dso *dso__new(const char *name)
794 struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
798 strcpy(dso->name, name);
799 dso__set_long_name(dso, dso->name, false);
800 dso__set_short_name(dso, dso->name, false);
801 for (i = 0; i < MAP__NR_TYPES; ++i)
802 dso->symbols[i] = dso->symbol_names[i] = RB_ROOT;
803 dso->data.cache = RB_ROOT;
805 dso->data.status = DSO_DATA_STATUS_UNKNOWN;
806 dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
807 dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
808 dso->is_64_bit = (sizeof(void *) == 8);
811 dso->sorted_by_name = 0;
812 dso->has_build_id = 0;
813 dso->has_srcline = 1;
815 dso->kernel = DSO_TYPE_USER;
816 dso->needs_swap = DSO_SWAP__UNSET;
817 RB_CLEAR_NODE(&dso->rb_node);
818 INIT_LIST_HEAD(&dso->node);
819 INIT_LIST_HEAD(&dso->data.open_entry);
825 void dso__delete(struct dso *dso)
829 if (!RB_EMPTY_NODE(&dso->rb_node))
830 pr_err("DSO %s is still in rbtree when being deleted!\n",
832 for (i = 0; i < MAP__NR_TYPES; ++i)
833 symbols__delete(&dso->symbols[i]);
835 if (dso->short_name_allocated) {
836 zfree((char **)&dso->short_name);
837 dso->short_name_allocated = false;
840 if (dso->long_name_allocated) {
841 zfree((char **)&dso->long_name);
842 dso->long_name_allocated = false;
845 dso__data_close(dso);
846 dso_cache__free(&dso->data.cache);
848 zfree(&dso->symsrc_filename);
852 void dso__set_build_id(struct dso *dso, void *build_id)
854 memcpy(dso->build_id, build_id, sizeof(dso->build_id));
855 dso->has_build_id = 1;
858 bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
860 return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
863 void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
867 if (machine__is_default_guest(machine))
869 sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
870 if (sysfs__read_build_id(path, dso->build_id,
871 sizeof(dso->build_id)) == 0)
872 dso->has_build_id = true;
875 int dso__kernel_module_get_build_id(struct dso *dso,
876 const char *root_dir)
878 char filename[PATH_MAX];
880 * kernel module short names are of the form "[module]" and
881 * we need just "module" here.
883 const char *name = dso->short_name + 1;
885 snprintf(filename, sizeof(filename),
886 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
887 root_dir, (int)strlen(name) - 1, name);
889 if (sysfs__read_build_id(filename, dso->build_id,
890 sizeof(dso->build_id)) == 0)
891 dso->has_build_id = true;
896 bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
898 bool have_build_id = false;
901 list_for_each_entry(pos, head, node) {
902 if (with_hits && !pos->hit)
904 if (pos->has_build_id) {
905 have_build_id = true;
908 if (filename__read_build_id(pos->long_name, pos->build_id,
909 sizeof(pos->build_id)) > 0) {
910 have_build_id = true;
911 pos->has_build_id = true;
915 return have_build_id;
918 void dsos__add(struct dsos *dsos, struct dso *dso)
920 list_add_tail(&dso->node, &dsos->head);
921 dso__findlink_by_longname(&dsos->root, dso, NULL);
924 struct dso *dsos__find(const struct dsos *dsos, const char *name,
930 list_for_each_entry(pos, &dsos->head, node)
931 if (strcmp(pos->short_name, name) == 0)
935 return dso__find_by_longname(&dsos->root, name);
938 struct dso *__dsos__findnew(struct dsos *dsos, const char *name)
940 struct dso *dso = dsos__find(dsos, name, false);
943 dso = dso__new(name);
945 dsos__add(dsos, dso);
946 dso__set_basename(dso);
953 size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
954 bool (skip)(struct dso *dso, int parm), int parm)
959 list_for_each_entry(pos, head, node) {
960 if (skip && skip(pos, parm))
962 ret += dso__fprintf_buildid(pos, fp);
963 ret += fprintf(fp, " %s\n", pos->long_name);
968 size_t __dsos__fprintf(struct list_head *head, FILE *fp)
973 list_for_each_entry(pos, head, node) {
975 for (i = 0; i < MAP__NR_TYPES; ++i)
976 ret += dso__fprintf(pos, i, fp);
982 size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
984 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
986 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
987 return fprintf(fp, "%s", sbuild_id);
990 size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp)
993 size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
995 if (dso->short_name != dso->long_name)
996 ret += fprintf(fp, "%s, ", dso->long_name);
997 ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
998 dso__loaded(dso, type) ? "" : "NOT ");
999 ret += dso__fprintf_buildid(dso, fp);
1000 ret += fprintf(fp, ")\n");
1001 for (nd = rb_first(&dso->symbols[type]); nd; nd = rb_next(nd)) {
1002 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
1003 ret += symbol__fprintf(pos, fp);
1009 enum dso_type dso__type(struct dso *dso, struct machine *machine)
1013 fd = dso__data_fd(dso, machine);
1015 return DSO__TYPE_UNKNOWN;
1017 return dso__type_fd(fd);