3 #include <sys/resource.h>
11 char dso__symtab_origin(const struct dso *dso)
13 static const char origin[] = {
14 [DSO_BINARY_TYPE__KALLSYMS] = 'k',
15 [DSO_BINARY_TYPE__VMLINUX] = 'v',
16 [DSO_BINARY_TYPE__JAVA_JIT] = 'j',
17 [DSO_BINARY_TYPE__DEBUGLINK] = 'l',
18 [DSO_BINARY_TYPE__BUILD_ID_CACHE] = 'B',
19 [DSO_BINARY_TYPE__FEDORA_DEBUGINFO] = 'f',
20 [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO] = 'u',
21 [DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO] = 'x',
22 [DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO] = 'o',
23 [DSO_BINARY_TYPE__BUILDID_DEBUGINFO] = 'b',
24 [DSO_BINARY_TYPE__SYSTEM_PATH_DSO] = 'd',
25 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE] = 'K',
26 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP] = 'm',
27 [DSO_BINARY_TYPE__GUEST_KALLSYMS] = 'g',
28 [DSO_BINARY_TYPE__GUEST_KMODULE] = 'G',
29 [DSO_BINARY_TYPE__GUEST_KMODULE_COMP] = 'M',
30 [DSO_BINARY_TYPE__GUEST_VMLINUX] = 'V',
33 if (dso == NULL || dso->symtab_type == DSO_BINARY_TYPE__NOT_FOUND)
35 return origin[dso->symtab_type];
38 int dso__read_binary_type_filename(const struct dso *dso,
39 enum dso_binary_type type,
40 char *root_dir, char *filename, size_t size)
42 char build_id_hex[BUILD_ID_SIZE * 2 + 1];
47 case DSO_BINARY_TYPE__DEBUGLINK: {
50 len = __symbol__join_symfs(filename, size, dso->long_name);
51 debuglink = filename + len;
52 while (debuglink != filename && *debuglink != '/')
54 if (*debuglink == '/')
56 ret = filename__read_debuglink(filename, debuglink,
57 size - (debuglink - filename));
60 case DSO_BINARY_TYPE__BUILD_ID_CACHE:
61 /* skip the locally configured cache if a symfs is given */
62 if (symbol_conf.symfs[0] ||
63 (dso__build_id_filename(dso, filename, size) == NULL))
67 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
68 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
69 snprintf(filename + len, size - len, "%s.debug", dso->long_name);
72 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
73 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
74 snprintf(filename + len, size - len, "%s", dso->long_name);
77 case DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO:
79 * Ubuntu can mixup /usr/lib with /lib, putting debuginfo in
80 * /usr/lib/debug/lib when it is expected to be in
81 * /usr/lib/debug/usr/lib
83 if (strlen(dso->long_name) < 9 ||
84 strncmp(dso->long_name, "/usr/lib/", 9)) {
88 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
89 snprintf(filename + len, size - len, "%s", dso->long_name + 4);
92 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
94 const char *last_slash;
97 last_slash = dso->long_name + dso->long_name_len;
98 while (last_slash != dso->long_name && *last_slash != '/')
101 len = __symbol__join_symfs(filename, size, "");
102 dir_size = last_slash - dso->long_name + 2;
103 if (dir_size > (size - len)) {
107 len += scnprintf(filename + len, dir_size, "%s", dso->long_name);
108 len += scnprintf(filename + len , size - len, ".debug%s",
113 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
114 if (!dso->has_build_id) {
119 build_id__sprintf(dso->build_id,
120 sizeof(dso->build_id),
122 len = __symbol__join_symfs(filename, size, "/usr/lib/debug/.build-id/");
123 snprintf(filename + len, size - len, "%.2s/%s.debug",
124 build_id_hex, build_id_hex + 2);
127 case DSO_BINARY_TYPE__VMLINUX:
128 case DSO_BINARY_TYPE__GUEST_VMLINUX:
129 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
130 __symbol__join_symfs(filename, size, dso->long_name);
133 case DSO_BINARY_TYPE__GUEST_KMODULE:
134 case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
135 path__join3(filename, size, symbol_conf.symfs,
136 root_dir, dso->long_name);
139 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
140 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
141 __symbol__join_symfs(filename, size, dso->long_name);
144 case DSO_BINARY_TYPE__KCORE:
145 case DSO_BINARY_TYPE__GUEST_KCORE:
146 snprintf(filename, size, "%s", dso->long_name);
150 case DSO_BINARY_TYPE__KALLSYMS:
151 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
152 case DSO_BINARY_TYPE__JAVA_JIT:
153 case DSO_BINARY_TYPE__NOT_FOUND:
161 static const struct {
163 int (*decompress)(const char *input, int output);
165 #ifdef HAVE_ZLIB_SUPPORT
166 { "gz", gzip_decompress_to_file },
168 #ifdef HAVE_LZMA_SUPPORT
169 { "xz", lzma_decompress_to_file },
174 bool is_supported_compression(const char *ext)
178 for (i = 0; compressions[i].fmt; i++) {
179 if (!strcmp(ext, compressions[i].fmt))
185 bool is_kernel_module(const char *pathname, int cpumode)
188 int mode = cpumode & PERF_RECORD_MISC_CPUMODE_MASK;
190 WARN_ONCE(mode != cpumode,
191 "Internal error: passing unmasked cpumode (%x) to is_kernel_module",
195 case PERF_RECORD_MISC_USER:
196 case PERF_RECORD_MISC_HYPERVISOR:
197 case PERF_RECORD_MISC_GUEST_USER:
199 /* Treat PERF_RECORD_MISC_CPUMODE_UNKNOWN as kernel */
201 if (kmod_path__parse(&m, pathname)) {
202 pr_err("Failed to check whether %s is a kernel module or not. Assume it is.",
211 bool decompress_to_file(const char *ext, const char *filename, int output_fd)
215 for (i = 0; compressions[i].fmt; i++) {
216 if (!strcmp(ext, compressions[i].fmt))
217 return !compressions[i].decompress(filename,
223 bool dso__needs_decompress(struct dso *dso)
225 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
226 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
230 * Parses kernel module specified in @path and updates
233 * @comp - true if @path contains supported compression suffix,
235 * @kmod - true if @path contains '.ko' suffix in right position,
237 * @name - if (@alloc_name && @kmod) is true, it contains strdup-ed base name
238 * of the kernel module without suffixes, otherwise strudup-ed
240 * @ext - if (@alloc_ext && @comp) is true, it contains strdup-ed string
241 * the compression suffix
243 * Returns 0 if there's no strdup error, -ENOMEM otherwise.
245 int __kmod_path__parse(struct kmod_path *m, const char *path,
246 bool alloc_name, bool alloc_ext)
248 const char *name = strrchr(path, '/');
249 const char *ext = strrchr(path, '.');
250 bool is_simple_name = false;
252 memset(m, 0x0, sizeof(*m));
253 name = name ? name + 1 : path;
256 * '.' is also a valid character for module name. For example:
257 * [aaa.bbb] is a valid module name. '[' should have higher
258 * priority than '.ko' suffix.
260 * The kernel names are from machine__mmap_name. Such
261 * name should belong to kernel itself, not kernel module.
263 if (name[0] == '[') {
264 is_simple_name = true;
265 if ((strncmp(name, "[kernel.kallsyms]", 17) == 0) ||
266 (strncmp(name, "[guest.kernel.kallsyms", 22) == 0) ||
267 (strncmp(name, "[vdso]", 6) == 0) ||
268 (strncmp(name, "[vdso32]", 8) == 0) ||
269 (strncmp(name, "[vdsox32]", 9) == 0) ||
270 (strncmp(name, "[vsyscall]", 10) == 0)) {
277 /* No extension, just return name. */
278 if ((ext == NULL) || is_simple_name) {
280 m->name = strdup(name);
281 return m->name ? 0 : -ENOMEM;
286 if (is_supported_compression(ext + 1)) {
291 /* Check .ko extension only if there's enough name left. */
293 m->kmod = !strncmp(ext, ".ko", 3);
297 if (asprintf(&m->name, "[%.*s]", (int) (ext - name), name) == -1)
300 if (asprintf(&m->name, "%s", name) == -1)
304 strxfrchar(m->name, '-', '_');
307 if (alloc_ext && m->comp) {
308 m->ext = strdup(ext + 4);
310 free((void *) m->name);
319 * Global list of open DSOs and the counter.
321 static LIST_HEAD(dso__data_open);
322 static long dso__data_open_cnt;
323 static pthread_mutex_t dso__data_open_lock = PTHREAD_MUTEX_INITIALIZER;
325 static void dso__list_add(struct dso *dso)
327 list_add_tail(&dso->data.open_entry, &dso__data_open);
328 dso__data_open_cnt++;
331 static void dso__list_del(struct dso *dso)
333 list_del(&dso->data.open_entry);
334 WARN_ONCE(dso__data_open_cnt <= 0,
335 "DSO data fd counter out of bounds.");
336 dso__data_open_cnt--;
339 static void close_first_dso(void);
341 static int do_open(char *name)
344 char sbuf[STRERR_BUFSIZE];
347 fd = open(name, O_RDONLY);
351 pr_debug("dso open failed: %s\n",
352 strerror_r(errno, sbuf, sizeof(sbuf)));
353 if (!dso__data_open_cnt || errno != EMFILE)
362 static int __open_dso(struct dso *dso, struct machine *machine)
365 char *root_dir = (char *)"";
366 char *name = malloc(PATH_MAX);
372 root_dir = machine->root_dir;
374 if (dso__read_binary_type_filename(dso, dso->binary_type,
375 root_dir, name, PATH_MAX)) {
385 static void check_data_close(void);
388 * dso_close - Open DSO data file
391 * Open @dso's data file descriptor and updates
392 * list/count of open DSO objects.
394 static int open_dso(struct dso *dso, struct machine *machine)
396 int fd = __open_dso(dso, machine);
401 * Check if we crossed the allowed number
402 * of opened DSOs and close one if needed.
410 static void close_data_fd(struct dso *dso)
412 if (dso->data.fd >= 0) {
415 dso->data.file_size = 0;
421 * dso_close - Close DSO data file
424 * Close @dso's data file descriptor and updates
425 * list/count of open DSO objects.
427 static void close_dso(struct dso *dso)
432 static void close_first_dso(void)
436 dso = list_first_entry(&dso__data_open, struct dso, data.open_entry);
440 static rlim_t get_fd_limit(void)
445 /* Allow half of the current open fd limit. */
446 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
447 if (l.rlim_cur == RLIM_INFINITY)
450 limit = l.rlim_cur / 2;
452 pr_err("failed to get fd limit\n");
459 static bool may_cache_fd(void)
464 limit = get_fd_limit();
466 if (limit == RLIM_INFINITY)
469 return limit > (rlim_t) dso__data_open_cnt;
473 * Check and close LRU dso if we crossed allowed limit
474 * for opened dso file descriptors. The limit is half
475 * of the RLIMIT_NOFILE files opened.
477 static void check_data_close(void)
479 bool cache_fd = may_cache_fd();
486 * dso__data_close - Close DSO data file
489 * External interface to close @dso's data file descriptor.
491 void dso__data_close(struct dso *dso)
493 pthread_mutex_lock(&dso__data_open_lock);
495 pthread_mutex_unlock(&dso__data_open_lock);
498 static void try_to_open_dso(struct dso *dso, struct machine *machine)
500 enum dso_binary_type binary_type_data[] = {
501 DSO_BINARY_TYPE__BUILD_ID_CACHE,
502 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
503 DSO_BINARY_TYPE__NOT_FOUND,
507 if (dso->data.fd >= 0)
510 if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) {
511 dso->data.fd = open_dso(dso, machine);
516 dso->binary_type = binary_type_data[i++];
518 dso->data.fd = open_dso(dso, machine);
519 if (dso->data.fd >= 0)
522 } while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND);
524 if (dso->data.fd >= 0)
525 dso->data.status = DSO_DATA_STATUS_OK;
527 dso->data.status = DSO_DATA_STATUS_ERROR;
531 * dso__data_get_fd - Get dso's data file descriptor
533 * @machine: machine object
535 * External interface to find dso's file, open it and
536 * returns file descriptor. It should be paired with
537 * dso__data_put_fd() if it returns non-negative value.
539 int dso__data_get_fd(struct dso *dso, struct machine *machine)
541 if (dso->data.status == DSO_DATA_STATUS_ERROR)
544 if (pthread_mutex_lock(&dso__data_open_lock) < 0)
547 try_to_open_dso(dso, machine);
549 if (dso->data.fd < 0)
550 pthread_mutex_unlock(&dso__data_open_lock);
555 void dso__data_put_fd(struct dso *dso __maybe_unused)
557 pthread_mutex_unlock(&dso__data_open_lock);
560 bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by)
564 if (dso->data.status_seen & flag)
567 dso->data.status_seen |= flag;
573 dso_cache__free(struct dso *dso)
575 struct rb_root *root = &dso->data.cache;
576 struct rb_node *next = rb_first(root);
578 pthread_mutex_lock(&dso->lock);
580 struct dso_cache *cache;
582 cache = rb_entry(next, struct dso_cache, rb_node);
583 next = rb_next(&cache->rb_node);
584 rb_erase(&cache->rb_node, root);
587 pthread_mutex_unlock(&dso->lock);
590 static struct dso_cache *dso_cache__find(struct dso *dso, u64 offset)
592 const struct rb_root *root = &dso->data.cache;
593 struct rb_node * const *p = &root->rb_node;
594 const struct rb_node *parent = NULL;
595 struct dso_cache *cache;
601 cache = rb_entry(parent, struct dso_cache, rb_node);
602 end = cache->offset + DSO__DATA_CACHE_SIZE;
604 if (offset < cache->offset)
606 else if (offset >= end)
615 static struct dso_cache *
616 dso_cache__insert(struct dso *dso, struct dso_cache *new)
618 struct rb_root *root = &dso->data.cache;
619 struct rb_node **p = &root->rb_node;
620 struct rb_node *parent = NULL;
621 struct dso_cache *cache;
622 u64 offset = new->offset;
624 pthread_mutex_lock(&dso->lock);
629 cache = rb_entry(parent, struct dso_cache, rb_node);
630 end = cache->offset + DSO__DATA_CACHE_SIZE;
632 if (offset < cache->offset)
634 else if (offset >= end)
640 rb_link_node(&new->rb_node, parent, p);
641 rb_insert_color(&new->rb_node, root);
645 pthread_mutex_unlock(&dso->lock);
650 dso_cache__memcpy(struct dso_cache *cache, u64 offset,
653 u64 cache_offset = offset - cache->offset;
654 u64 cache_size = min(cache->size - cache_offset, size);
656 memcpy(data, cache->data + cache_offset, cache_size);
661 dso_cache__read(struct dso *dso, struct machine *machine,
662 u64 offset, u8 *data, ssize_t size)
664 struct dso_cache *cache;
665 struct dso_cache *old;
671 cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
675 pthread_mutex_lock(&dso__data_open_lock);
678 * dso->data.fd might be closed if other thread opened another
679 * file (dso) due to open file limit (RLIMIT_NOFILE).
681 try_to_open_dso(dso, machine);
683 if (dso->data.fd < 0) {
685 dso->data.status = DSO_DATA_STATUS_ERROR;
689 cache_offset = offset & DSO__DATA_CACHE_MASK;
691 ret = pread(dso->data.fd, cache->data, DSO__DATA_CACHE_SIZE, cache_offset);
695 cache->offset = cache_offset;
699 pthread_mutex_unlock(&dso__data_open_lock);
702 old = dso_cache__insert(dso, cache);
704 /* we lose the race */
709 ret = dso_cache__memcpy(cache, offset, data, size);
718 static ssize_t dso_cache_read(struct dso *dso, struct machine *machine,
719 u64 offset, u8 *data, ssize_t size)
721 struct dso_cache *cache;
723 cache = dso_cache__find(dso, offset);
725 return dso_cache__memcpy(cache, offset, data, size);
727 return dso_cache__read(dso, machine, offset, data, size);
731 * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
732 * in the rb_tree. Any read to already cached data is served
735 static ssize_t cached_read(struct dso *dso, struct machine *machine,
736 u64 offset, u8 *data, ssize_t size)
744 ret = dso_cache_read(dso, machine, offset, p, size);
748 /* Reached EOF, return what we have. */
764 static int data_file_size(struct dso *dso, struct machine *machine)
768 char sbuf[STRERR_BUFSIZE];
770 if (dso->data.file_size)
773 if (dso->data.status == DSO_DATA_STATUS_ERROR)
776 pthread_mutex_lock(&dso__data_open_lock);
779 * dso->data.fd might be closed if other thread opened another
780 * file (dso) due to open file limit (RLIMIT_NOFILE).
782 try_to_open_dso(dso, machine);
784 if (dso->data.fd < 0) {
786 dso->data.status = DSO_DATA_STATUS_ERROR;
790 if (fstat(dso->data.fd, &st) < 0) {
792 pr_err("dso cache fstat failed: %s\n",
793 strerror_r(errno, sbuf, sizeof(sbuf)));
794 dso->data.status = DSO_DATA_STATUS_ERROR;
797 dso->data.file_size = st.st_size;
800 pthread_mutex_unlock(&dso__data_open_lock);
805 * dso__data_size - Return dso data size
807 * @machine: machine object
809 * Return: dso data size
811 off_t dso__data_size(struct dso *dso, struct machine *machine)
813 if (data_file_size(dso, machine))
816 /* For now just estimate dso data size is close to file size */
817 return dso->data.file_size;
820 static ssize_t data_read_offset(struct dso *dso, struct machine *machine,
821 u64 offset, u8 *data, ssize_t size)
823 if (data_file_size(dso, machine))
826 /* Check the offset sanity. */
827 if (offset > dso->data.file_size)
830 if (offset + size < offset)
833 return cached_read(dso, machine, offset, data, size);
837 * dso__data_read_offset - Read data from dso file offset
839 * @machine: machine object
840 * @offset: file offset
841 * @data: buffer to store data
842 * @size: size of the @data buffer
844 * External interface to read data from dso file offset. Open
845 * dso data file and use cached_read to get the data.
847 ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
848 u64 offset, u8 *data, ssize_t size)
850 if (dso->data.status == DSO_DATA_STATUS_ERROR)
853 return data_read_offset(dso, machine, offset, data, size);
857 * dso__data_read_addr - Read data from dso address
859 * @machine: machine object
860 * @add: virtual memory address
861 * @data: buffer to store data
862 * @size: size of the @data buffer
864 * External interface to read data from dso address.
866 ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
867 struct machine *machine, u64 addr,
868 u8 *data, ssize_t size)
870 u64 offset = map->map_ip(map, addr);
871 return dso__data_read_offset(dso, machine, offset, data, size);
874 struct map *dso__new_map(const char *name)
876 struct map *map = NULL;
877 struct dso *dso = dso__new(name);
880 map = map__new2(0, dso, MAP__FUNCTION);
885 struct dso *machine__findnew_kernel(struct machine *machine, const char *name,
886 const char *short_name, int dso_type)
889 * The kernel dso could be created by build_id processing.
891 struct dso *dso = machine__findnew_dso(machine, name);
894 * We need to run this in all cases, since during the build_id
895 * processing we had no idea this was the kernel dso.
898 dso__set_short_name(dso, short_name, false);
899 dso->kernel = dso_type;
906 * Find a matching entry and/or link current entry to RB tree.
907 * Either one of the dso or name parameter must be non-NULL or the
908 * function will not work.
910 static struct dso *__dso__findlink_by_longname(struct rb_root *root,
911 struct dso *dso, const char *name)
913 struct rb_node **p = &root->rb_node;
914 struct rb_node *parent = NULL;
917 name = dso->long_name;
919 * Find node with the matching name
922 struct dso *this = rb_entry(*p, struct dso, rb_node);
923 int rc = strcmp(name, this->long_name);
928 * In case the new DSO is a duplicate of an existing
929 * one, print an one-time warning & put the new entry
930 * at the end of the list of duplicates.
932 if (!dso || (dso == this))
933 return this; /* Find matching dso */
935 * The core kernel DSOs may have duplicated long name.
936 * In this case, the short name should be different.
937 * Comparing the short names to differentiate the DSOs.
939 rc = strcmp(dso->short_name, this->short_name);
941 pr_err("Duplicated dso name: %s\n", name);
946 p = &parent->rb_left;
948 p = &parent->rb_right;
951 /* Add new node and rebalance tree */
952 rb_link_node(&dso->rb_node, parent, p);
953 rb_insert_color(&dso->rb_node, root);
959 static inline struct dso *__dso__find_by_longname(struct rb_root *root,
962 return __dso__findlink_by_longname(root, NULL, name);
965 void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
967 struct rb_root *root = dso->root;
972 if (dso->long_name_allocated)
973 free((char *)dso->long_name);
976 rb_erase(&dso->rb_node, root);
978 * __dso__findlink_by_longname() isn't guaranteed to add it
979 * back, so a clean removal is required here.
981 RB_CLEAR_NODE(&dso->rb_node);
985 dso->long_name = name;
986 dso->long_name_len = strlen(name);
987 dso->long_name_allocated = name_allocated;
990 __dso__findlink_by_longname(root, dso, NULL);
993 void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
998 if (dso->short_name_allocated)
999 free((char *)dso->short_name);
1001 dso->short_name = name;
1002 dso->short_name_len = strlen(name);
1003 dso->short_name_allocated = name_allocated;
1006 static void dso__set_basename(struct dso *dso)
1009 * basename() may modify path buffer, so we must pass
1012 char *base, *lname = strdup(dso->long_name);
1018 * basename() may return a pointer to internal
1019 * storage which is reused in subsequent calls
1020 * so copy the result.
1022 base = strdup(basename(lname));
1029 dso__set_short_name(dso, base, true);
1032 int dso__name_len(const struct dso *dso)
1035 return strlen("[unknown]");
1037 return dso->long_name_len;
1039 return dso->short_name_len;
1042 bool dso__loaded(const struct dso *dso, enum map_type type)
1044 return dso->loaded & (1 << type);
1047 bool dso__sorted_by_name(const struct dso *dso, enum map_type type)
1049 return dso->sorted_by_name & (1 << type);
1052 void dso__set_sorted_by_name(struct dso *dso, enum map_type type)
1054 dso->sorted_by_name |= (1 << type);
1057 struct dso *dso__new(const char *name)
1059 struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
1063 strcpy(dso->name, name);
1064 dso__set_long_name(dso, dso->name, false);
1065 dso__set_short_name(dso, dso->name, false);
1066 for (i = 0; i < MAP__NR_TYPES; ++i)
1067 dso->symbols[i] = dso->symbol_names[i] = RB_ROOT;
1068 dso->data.cache = RB_ROOT;
1070 dso->data.status = DSO_DATA_STATUS_UNKNOWN;
1071 dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
1072 dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
1073 dso->is_64_bit = (sizeof(void *) == 8);
1076 dso->sorted_by_name = 0;
1077 dso->has_build_id = 0;
1078 dso->has_srcline = 1;
1080 dso->kernel = DSO_TYPE_USER;
1081 dso->needs_swap = DSO_SWAP__UNSET;
1082 RB_CLEAR_NODE(&dso->rb_node);
1084 INIT_LIST_HEAD(&dso->node);
1085 INIT_LIST_HEAD(&dso->data.open_entry);
1086 pthread_mutex_init(&dso->lock, NULL);
1087 atomic_set(&dso->refcnt, 1);
1093 void dso__delete(struct dso *dso)
1097 if (!RB_EMPTY_NODE(&dso->rb_node))
1098 pr_err("DSO %s is still in rbtree when being deleted!\n",
1100 for (i = 0; i < MAP__NR_TYPES; ++i)
1101 symbols__delete(&dso->symbols[i]);
1103 if (dso->short_name_allocated) {
1104 zfree((char **)&dso->short_name);
1105 dso->short_name_allocated = false;
1108 if (dso->long_name_allocated) {
1109 zfree((char **)&dso->long_name);
1110 dso->long_name_allocated = false;
1113 dso__data_close(dso);
1114 auxtrace_cache__free(dso->auxtrace_cache);
1115 dso_cache__free(dso);
1117 zfree(&dso->symsrc_filename);
1118 pthread_mutex_destroy(&dso->lock);
1122 struct dso *dso__get(struct dso *dso)
1125 atomic_inc(&dso->refcnt);
1129 void dso__put(struct dso *dso)
1131 if (dso && atomic_dec_and_test(&dso->refcnt))
1135 void dso__set_build_id(struct dso *dso, void *build_id)
1137 memcpy(dso->build_id, build_id, sizeof(dso->build_id));
1138 dso->has_build_id = 1;
1141 bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
1143 return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
1146 void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
1148 char path[PATH_MAX];
1150 if (machine__is_default_guest(machine))
1152 sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
1153 if (sysfs__read_build_id(path, dso->build_id,
1154 sizeof(dso->build_id)) == 0)
1155 dso->has_build_id = true;
1158 int dso__kernel_module_get_build_id(struct dso *dso,
1159 const char *root_dir)
1161 char filename[PATH_MAX];
1163 * kernel module short names are of the form "[module]" and
1164 * we need just "module" here.
1166 const char *name = dso->short_name + 1;
1168 snprintf(filename, sizeof(filename),
1169 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1170 root_dir, (int)strlen(name) - 1, name);
1172 if (sysfs__read_build_id(filename, dso->build_id,
1173 sizeof(dso->build_id)) == 0)
1174 dso->has_build_id = true;
1179 bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
1181 bool have_build_id = false;
1184 list_for_each_entry(pos, head, node) {
1185 if (with_hits && !pos->hit)
1187 if (pos->has_build_id) {
1188 have_build_id = true;
1191 if (filename__read_build_id(pos->long_name, pos->build_id,
1192 sizeof(pos->build_id)) > 0) {
1193 have_build_id = true;
1194 pos->has_build_id = true;
1198 return have_build_id;
1201 void __dsos__add(struct dsos *dsos, struct dso *dso)
1203 list_add_tail(&dso->node, &dsos->head);
1204 __dso__findlink_by_longname(&dsos->root, dso, NULL);
1206 * It is now in the linked list, grab a reference, then garbage collect
1207 * this when needing memory, by looking at LRU dso instances in the
1208 * list with atomic_read(&dso->refcnt) == 1, i.e. no references
1209 * anywhere besides the one for the list, do, under a lock for the
1210 * list: remove it from the list, then a dso__put(), that probably will
1211 * be the last and will then call dso__delete(), end of life.
1213 * That, or at the end of the 'struct machine' lifetime, when all
1214 * 'struct dso' instances will be removed from the list, in
1215 * dsos__exit(), if they have no other reference from some other data
1218 * E.g.: after processing a 'perf.data' file and storing references
1219 * to objects instantiated while processing events, we will have
1220 * references to the 'thread', 'map', 'dso' structs all from 'struct
1221 * hist_entry' instances, but we may not need anything not referenced,
1222 * so we might as well call machines__exit()/machines__delete() and
1223 * garbage collect it.
1228 void dsos__add(struct dsos *dsos, struct dso *dso)
1230 pthread_rwlock_wrlock(&dsos->lock);
1231 __dsos__add(dsos, dso);
1232 pthread_rwlock_unlock(&dsos->lock);
1235 struct dso *__dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
1240 list_for_each_entry(pos, &dsos->head, node)
1241 if (strcmp(pos->short_name, name) == 0)
1245 return __dso__find_by_longname(&dsos->root, name);
1248 struct dso *dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
1251 pthread_rwlock_rdlock(&dsos->lock);
1252 dso = __dsos__find(dsos, name, cmp_short);
1253 pthread_rwlock_unlock(&dsos->lock);
1257 struct dso *__dsos__addnew(struct dsos *dsos, const char *name)
1259 struct dso *dso = dso__new(name);
1262 __dsos__add(dsos, dso);
1263 dso__set_basename(dso);
1268 struct dso *__dsos__findnew(struct dsos *dsos, const char *name)
1270 struct dso *dso = __dsos__find(dsos, name, false);
1272 return dso ? dso : __dsos__addnew(dsos, name);
1275 struct dso *dsos__findnew(struct dsos *dsos, const char *name)
1278 pthread_rwlock_wrlock(&dsos->lock);
1279 dso = dso__get(__dsos__findnew(dsos, name));
1280 pthread_rwlock_unlock(&dsos->lock);
1284 size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
1285 bool (skip)(struct dso *dso, int parm), int parm)
1290 list_for_each_entry(pos, head, node) {
1291 if (skip && skip(pos, parm))
1293 ret += dso__fprintf_buildid(pos, fp);
1294 ret += fprintf(fp, " %s\n", pos->long_name);
1299 size_t __dsos__fprintf(struct list_head *head, FILE *fp)
1304 list_for_each_entry(pos, head, node) {
1306 for (i = 0; i < MAP__NR_TYPES; ++i)
1307 ret += dso__fprintf(pos, i, fp);
1313 size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
1315 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1317 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1318 return fprintf(fp, "%s", sbuild_id);
1321 size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp)
1324 size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
1326 if (dso->short_name != dso->long_name)
1327 ret += fprintf(fp, "%s, ", dso->long_name);
1328 ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
1329 dso__loaded(dso, type) ? "" : "NOT ");
1330 ret += dso__fprintf_buildid(dso, fp);
1331 ret += fprintf(fp, ")\n");
1332 for (nd = rb_first(&dso->symbols[type]); nd; nd = rb_next(nd)) {
1333 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
1334 ret += symbol__fprintf(pos, fp);
1340 enum dso_type dso__type(struct dso *dso, struct machine *machine)
1343 enum dso_type type = DSO__TYPE_UNKNOWN;
1345 fd = dso__data_get_fd(dso, machine);
1347 type = dso__type_fd(fd);
1348 dso__data_put_fd(dso);
1354 int dso__strerror_load(struct dso *dso, char *buf, size_t buflen)
1356 int idx, errnum = dso->load_errno;
1358 * This must have a same ordering as the enum dso_load_errno.
1360 static const char *dso_load__error_str[] = {
1361 "Internal tools/perf/ library error",
1363 "Can not read build id",
1364 "Mismatching build id",
1365 "Decompression failure",
1368 BUG_ON(buflen == 0);
1371 const char *err = strerror_r(errnum, buf, buflen);
1374 scnprintf(buf, buflen, "%s", err);
1379 if (errnum < __DSO_LOAD_ERRNO__START || errnum >= __DSO_LOAD_ERRNO__END)
1382 idx = errnum - __DSO_LOAD_ERRNO__START;
1383 scnprintf(buf, buflen, "%s", dso_load__error_str[idx]);