3 #include <sys/resource.h>
12 static const char * const debuglink_paths[] = {
19 char dso__symtab_origin(const struct dso *dso)
21 static const char origin[] = {
22 [DSO_BINARY_TYPE__KALLSYMS] = 'k',
23 [DSO_BINARY_TYPE__VMLINUX] = 'v',
24 [DSO_BINARY_TYPE__JAVA_JIT] = 'j',
25 [DSO_BINARY_TYPE__DEBUGLINK] = 'l',
26 [DSO_BINARY_TYPE__BUILD_ID_CACHE] = 'B',
27 [DSO_BINARY_TYPE__FEDORA_DEBUGINFO] = 'f',
28 [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO] = 'u',
29 [DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO] = 'o',
30 [DSO_BINARY_TYPE__BUILDID_DEBUGINFO] = 'b',
31 [DSO_BINARY_TYPE__SYSTEM_PATH_DSO] = 'd',
32 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE] = 'K',
33 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP] = 'm',
34 [DSO_BINARY_TYPE__GUEST_KALLSYMS] = 'g',
35 [DSO_BINARY_TYPE__GUEST_KMODULE] = 'G',
36 [DSO_BINARY_TYPE__GUEST_KMODULE_COMP] = 'M',
37 [DSO_BINARY_TYPE__GUEST_VMLINUX] = 'V',
40 if (dso == NULL || dso->symtab_type == DSO_BINARY_TYPE__NOT_FOUND)
42 return origin[dso->symtab_type];
45 int dso__read_binary_type_filename(const struct dso *dso,
46 enum dso_binary_type type,
47 char *root_dir, char *filename, size_t size)
49 char build_id_hex[SBUILD_ID_SIZE];
54 case DSO_BINARY_TYPE__DEBUGLINK:
56 const char *last_slash;
57 char dso_dir[PATH_MAX];
58 char symfile[PATH_MAX];
61 len = __symbol__join_symfs(filename, size, dso->long_name);
62 last_slash = filename + len;
63 while (last_slash != filename && *last_slash != '/')
66 strncpy(dso_dir, filename, last_slash - filename);
67 dso_dir[last_slash-filename] = '\0';
69 if (!is_regular_file(filename)) {
74 ret = filename__read_debuglink(filename, symfile, PATH_MAX);
78 /* Check predefined locations where debug file might reside */
80 for (i = 0; i < ARRAY_SIZE(debuglink_paths); i++) {
81 snprintf(filename, size,
82 debuglink_paths[i], dso_dir, symfile);
83 if (is_regular_file(filename)) {
91 case DSO_BINARY_TYPE__BUILD_ID_CACHE:
92 if (dso__build_id_filename(dso, filename, size) == NULL)
96 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
97 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
98 snprintf(filename + len, size - len, "%s.debug", dso->long_name);
101 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
102 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
103 snprintf(filename + len, size - len, "%s", dso->long_name);
106 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
108 const char *last_slash;
111 last_slash = dso->long_name + dso->long_name_len;
112 while (last_slash != dso->long_name && *last_slash != '/')
115 len = __symbol__join_symfs(filename, size, "");
116 dir_size = last_slash - dso->long_name + 2;
117 if (dir_size > (size - len)) {
121 len += scnprintf(filename + len, dir_size, "%s", dso->long_name);
122 len += scnprintf(filename + len , size - len, ".debug%s",
127 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
128 if (!dso->has_build_id) {
133 build_id__sprintf(dso->build_id,
134 sizeof(dso->build_id),
136 len = __symbol__join_symfs(filename, size, "/usr/lib/debug/.build-id/");
137 snprintf(filename + len, size - len, "%.2s/%s.debug",
138 build_id_hex, build_id_hex + 2);
141 case DSO_BINARY_TYPE__VMLINUX:
142 case DSO_BINARY_TYPE__GUEST_VMLINUX:
143 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
144 __symbol__join_symfs(filename, size, dso->long_name);
147 case DSO_BINARY_TYPE__GUEST_KMODULE:
148 case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
149 path__join3(filename, size, symbol_conf.symfs,
150 root_dir, dso->long_name);
153 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
154 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
155 __symbol__join_symfs(filename, size, dso->long_name);
158 case DSO_BINARY_TYPE__KCORE:
159 case DSO_BINARY_TYPE__GUEST_KCORE:
160 snprintf(filename, size, "%s", dso->long_name);
164 case DSO_BINARY_TYPE__KALLSYMS:
165 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
166 case DSO_BINARY_TYPE__JAVA_JIT:
167 case DSO_BINARY_TYPE__NOT_FOUND:
175 static const struct {
177 int (*decompress)(const char *input, int output);
179 #ifdef HAVE_ZLIB_SUPPORT
180 { "gz", gzip_decompress_to_file },
182 #ifdef HAVE_LZMA_SUPPORT
183 { "xz", lzma_decompress_to_file },
188 bool is_supported_compression(const char *ext)
192 for (i = 0; compressions[i].fmt; i++) {
193 if (!strcmp(ext, compressions[i].fmt))
199 bool is_kernel_module(const char *pathname, int cpumode)
202 int mode = cpumode & PERF_RECORD_MISC_CPUMODE_MASK;
204 WARN_ONCE(mode != cpumode,
205 "Internal error: passing unmasked cpumode (%x) to is_kernel_module",
209 case PERF_RECORD_MISC_USER:
210 case PERF_RECORD_MISC_HYPERVISOR:
211 case PERF_RECORD_MISC_GUEST_USER:
213 /* Treat PERF_RECORD_MISC_CPUMODE_UNKNOWN as kernel */
215 if (kmod_path__parse(&m, pathname)) {
216 pr_err("Failed to check whether %s is a kernel module or not. Assume it is.",
225 bool decompress_to_file(const char *ext, const char *filename, int output_fd)
229 for (i = 0; compressions[i].fmt; i++) {
230 if (!strcmp(ext, compressions[i].fmt))
231 return !compressions[i].decompress(filename,
237 bool dso__needs_decompress(struct dso *dso)
239 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
240 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
244 * Parses kernel module specified in @path and updates
247 * @comp - true if @path contains supported compression suffix,
249 * @kmod - true if @path contains '.ko' suffix in right position,
251 * @name - if (@alloc_name && @kmod) is true, it contains strdup-ed base name
252 * of the kernel module without suffixes, otherwise strudup-ed
254 * @ext - if (@alloc_ext && @comp) is true, it contains strdup-ed string
255 * the compression suffix
257 * Returns 0 if there's no strdup error, -ENOMEM otherwise.
259 int __kmod_path__parse(struct kmod_path *m, const char *path,
260 bool alloc_name, bool alloc_ext)
262 const char *name = strrchr(path, '/');
263 const char *ext = strrchr(path, '.');
264 bool is_simple_name = false;
266 memset(m, 0x0, sizeof(*m));
267 name = name ? name + 1 : path;
270 * '.' is also a valid character for module name. For example:
271 * [aaa.bbb] is a valid module name. '[' should have higher
272 * priority than '.ko' suffix.
274 * The kernel names are from machine__mmap_name. Such
275 * name should belong to kernel itself, not kernel module.
277 if (name[0] == '[') {
278 is_simple_name = true;
279 if ((strncmp(name, "[kernel.kallsyms]", 17) == 0) ||
280 (strncmp(name, "[guest.kernel.kallsyms", 22) == 0) ||
281 (strncmp(name, "[vdso]", 6) == 0) ||
282 (strncmp(name, "[vsyscall]", 10) == 0)) {
289 /* No extension, just return name. */
290 if ((ext == NULL) || is_simple_name) {
292 m->name = strdup(name);
293 return m->name ? 0 : -ENOMEM;
298 if (is_supported_compression(ext + 1)) {
303 /* Check .ko extension only if there's enough name left. */
305 m->kmod = !strncmp(ext, ".ko", 3);
309 if (asprintf(&m->name, "[%.*s]", (int) (ext - name), name) == -1)
312 if (asprintf(&m->name, "%s", name) == -1)
316 strxfrchar(m->name, '-', '_');
319 if (alloc_ext && m->comp) {
320 m->ext = strdup(ext + 4);
322 free((void *) m->name);
331 * Global list of open DSOs and the counter.
333 static LIST_HEAD(dso__data_open);
334 static long dso__data_open_cnt;
335 static pthread_mutex_t dso__data_open_lock = PTHREAD_MUTEX_INITIALIZER;
337 static void dso__list_add(struct dso *dso)
339 list_add_tail(&dso->data.open_entry, &dso__data_open);
340 dso__data_open_cnt++;
343 static void dso__list_del(struct dso *dso)
345 list_del(&dso->data.open_entry);
346 WARN_ONCE(dso__data_open_cnt <= 0,
347 "DSO data fd counter out of bounds.");
348 dso__data_open_cnt--;
351 static void close_first_dso(void);
353 static int do_open(char *name)
356 char sbuf[STRERR_BUFSIZE];
359 fd = open(name, O_RDONLY);
363 pr_debug("dso open failed: %s\n",
364 str_error_r(errno, sbuf, sizeof(sbuf)));
365 if (!dso__data_open_cnt || errno != EMFILE)
374 static int __open_dso(struct dso *dso, struct machine *machine)
377 char *root_dir = (char *)"";
378 char *name = malloc(PATH_MAX);
384 root_dir = machine->root_dir;
386 if (dso__read_binary_type_filename(dso, dso->binary_type,
387 root_dir, name, PATH_MAX)) {
392 if (!is_regular_file(name))
400 static void check_data_close(void);
403 * dso_close - Open DSO data file
406 * Open @dso's data file descriptor and updates
407 * list/count of open DSO objects.
409 static int open_dso(struct dso *dso, struct machine *machine)
411 int fd = __open_dso(dso, machine);
416 * Check if we crossed the allowed number
417 * of opened DSOs and close one if needed.
425 static void close_data_fd(struct dso *dso)
427 if (dso->data.fd >= 0) {
430 dso->data.file_size = 0;
436 * dso_close - Close DSO data file
439 * Close @dso's data file descriptor and updates
440 * list/count of open DSO objects.
442 static void close_dso(struct dso *dso)
447 static void close_first_dso(void)
451 dso = list_first_entry(&dso__data_open, struct dso, data.open_entry);
455 static rlim_t get_fd_limit(void)
460 /* Allow half of the current open fd limit. */
461 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
462 if (l.rlim_cur == RLIM_INFINITY)
465 limit = l.rlim_cur / 2;
467 pr_err("failed to get fd limit\n");
474 static rlim_t fd_limit;
477 * Used only by tests/dso-data.c to reset the environment
478 * for tests. I dont expect we should change this during
481 void reset_fd_limit(void)
486 static bool may_cache_fd(void)
489 fd_limit = get_fd_limit();
491 if (fd_limit == RLIM_INFINITY)
494 return fd_limit > (rlim_t) dso__data_open_cnt;
498 * Check and close LRU dso if we crossed allowed limit
499 * for opened dso file descriptors. The limit is half
500 * of the RLIMIT_NOFILE files opened.
502 static void check_data_close(void)
504 bool cache_fd = may_cache_fd();
511 * dso__data_close - Close DSO data file
514 * External interface to close @dso's data file descriptor.
516 void dso__data_close(struct dso *dso)
518 pthread_mutex_lock(&dso__data_open_lock);
520 pthread_mutex_unlock(&dso__data_open_lock);
523 static void try_to_open_dso(struct dso *dso, struct machine *machine)
525 enum dso_binary_type binary_type_data[] = {
526 DSO_BINARY_TYPE__BUILD_ID_CACHE,
527 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
528 DSO_BINARY_TYPE__NOT_FOUND,
532 if (dso->data.fd >= 0)
535 if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) {
536 dso->data.fd = open_dso(dso, machine);
541 dso->binary_type = binary_type_data[i++];
543 dso->data.fd = open_dso(dso, machine);
544 if (dso->data.fd >= 0)
547 } while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND);
549 if (dso->data.fd >= 0)
550 dso->data.status = DSO_DATA_STATUS_OK;
552 dso->data.status = DSO_DATA_STATUS_ERROR;
556 * dso__data_get_fd - Get dso's data file descriptor
558 * @machine: machine object
560 * External interface to find dso's file, open it and
561 * returns file descriptor. It should be paired with
562 * dso__data_put_fd() if it returns non-negative value.
564 int dso__data_get_fd(struct dso *dso, struct machine *machine)
566 if (dso->data.status == DSO_DATA_STATUS_ERROR)
569 if (pthread_mutex_lock(&dso__data_open_lock) < 0)
572 try_to_open_dso(dso, machine);
574 if (dso->data.fd < 0)
575 pthread_mutex_unlock(&dso__data_open_lock);
580 void dso__data_put_fd(struct dso *dso __maybe_unused)
582 pthread_mutex_unlock(&dso__data_open_lock);
585 bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by)
589 if (dso->data.status_seen & flag)
592 dso->data.status_seen |= flag;
598 dso_cache__free(struct dso *dso)
600 struct rb_root *root = &dso->data.cache;
601 struct rb_node *next = rb_first(root);
603 pthread_mutex_lock(&dso->lock);
605 struct dso_cache *cache;
607 cache = rb_entry(next, struct dso_cache, rb_node);
608 next = rb_next(&cache->rb_node);
609 rb_erase(&cache->rb_node, root);
612 pthread_mutex_unlock(&dso->lock);
615 static struct dso_cache *dso_cache__find(struct dso *dso, u64 offset)
617 const struct rb_root *root = &dso->data.cache;
618 struct rb_node * const *p = &root->rb_node;
619 const struct rb_node *parent = NULL;
620 struct dso_cache *cache;
626 cache = rb_entry(parent, struct dso_cache, rb_node);
627 end = cache->offset + DSO__DATA_CACHE_SIZE;
629 if (offset < cache->offset)
631 else if (offset >= end)
640 static struct dso_cache *
641 dso_cache__insert(struct dso *dso, struct dso_cache *new)
643 struct rb_root *root = &dso->data.cache;
644 struct rb_node **p = &root->rb_node;
645 struct rb_node *parent = NULL;
646 struct dso_cache *cache;
647 u64 offset = new->offset;
649 pthread_mutex_lock(&dso->lock);
654 cache = rb_entry(parent, struct dso_cache, rb_node);
655 end = cache->offset + DSO__DATA_CACHE_SIZE;
657 if (offset < cache->offset)
659 else if (offset >= end)
665 rb_link_node(&new->rb_node, parent, p);
666 rb_insert_color(&new->rb_node, root);
670 pthread_mutex_unlock(&dso->lock);
675 dso_cache__memcpy(struct dso_cache *cache, u64 offset,
678 u64 cache_offset = offset - cache->offset;
679 u64 cache_size = min(cache->size - cache_offset, size);
681 memcpy(data, cache->data + cache_offset, cache_size);
686 dso_cache__read(struct dso *dso, struct machine *machine,
687 u64 offset, u8 *data, ssize_t size)
689 struct dso_cache *cache;
690 struct dso_cache *old;
696 cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
700 pthread_mutex_lock(&dso__data_open_lock);
703 * dso->data.fd might be closed if other thread opened another
704 * file (dso) due to open file limit (RLIMIT_NOFILE).
706 try_to_open_dso(dso, machine);
708 if (dso->data.fd < 0) {
710 dso->data.status = DSO_DATA_STATUS_ERROR;
714 cache_offset = offset & DSO__DATA_CACHE_MASK;
716 ret = pread(dso->data.fd, cache->data, DSO__DATA_CACHE_SIZE, cache_offset);
720 cache->offset = cache_offset;
724 pthread_mutex_unlock(&dso__data_open_lock);
727 old = dso_cache__insert(dso, cache);
729 /* we lose the race */
734 ret = dso_cache__memcpy(cache, offset, data, size);
743 static ssize_t dso_cache_read(struct dso *dso, struct machine *machine,
744 u64 offset, u8 *data, ssize_t size)
746 struct dso_cache *cache;
748 cache = dso_cache__find(dso, offset);
750 return dso_cache__memcpy(cache, offset, data, size);
752 return dso_cache__read(dso, machine, offset, data, size);
756 * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
757 * in the rb_tree. Any read to already cached data is served
760 static ssize_t cached_read(struct dso *dso, struct machine *machine,
761 u64 offset, u8 *data, ssize_t size)
769 ret = dso_cache_read(dso, machine, offset, p, size);
773 /* Reached EOF, return what we have. */
789 static int data_file_size(struct dso *dso, struct machine *machine)
793 char sbuf[STRERR_BUFSIZE];
795 if (dso->data.file_size)
798 if (dso->data.status == DSO_DATA_STATUS_ERROR)
801 pthread_mutex_lock(&dso__data_open_lock);
804 * dso->data.fd might be closed if other thread opened another
805 * file (dso) due to open file limit (RLIMIT_NOFILE).
807 try_to_open_dso(dso, machine);
809 if (dso->data.fd < 0) {
811 dso->data.status = DSO_DATA_STATUS_ERROR;
815 if (fstat(dso->data.fd, &st) < 0) {
817 pr_err("dso cache fstat failed: %s\n",
818 str_error_r(errno, sbuf, sizeof(sbuf)));
819 dso->data.status = DSO_DATA_STATUS_ERROR;
822 dso->data.file_size = st.st_size;
825 pthread_mutex_unlock(&dso__data_open_lock);
830 * dso__data_size - Return dso data size
832 * @machine: machine object
834 * Return: dso data size
836 off_t dso__data_size(struct dso *dso, struct machine *machine)
838 if (data_file_size(dso, machine))
841 /* For now just estimate dso data size is close to file size */
842 return dso->data.file_size;
845 static ssize_t data_read_offset(struct dso *dso, struct machine *machine,
846 u64 offset, u8 *data, ssize_t size)
848 if (data_file_size(dso, machine))
851 /* Check the offset sanity. */
852 if (offset > dso->data.file_size)
855 if (offset + size < offset)
858 return cached_read(dso, machine, offset, data, size);
862 * dso__data_read_offset - Read data from dso file offset
864 * @machine: machine object
865 * @offset: file offset
866 * @data: buffer to store data
867 * @size: size of the @data buffer
869 * External interface to read data from dso file offset. Open
870 * dso data file and use cached_read to get the data.
872 ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
873 u64 offset, u8 *data, ssize_t size)
875 if (dso->data.status == DSO_DATA_STATUS_ERROR)
878 return data_read_offset(dso, machine, offset, data, size);
882 * dso__data_read_addr - Read data from dso address
884 * @machine: machine object
885 * @add: virtual memory address
886 * @data: buffer to store data
887 * @size: size of the @data buffer
889 * External interface to read data from dso address.
891 ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
892 struct machine *machine, u64 addr,
893 u8 *data, ssize_t size)
895 u64 offset = map->map_ip(map, addr);
896 return dso__data_read_offset(dso, machine, offset, data, size);
899 struct map *dso__new_map(const char *name)
901 struct map *map = NULL;
902 struct dso *dso = dso__new(name);
905 map = map__new2(0, dso, MAP__FUNCTION);
910 struct dso *machine__findnew_kernel(struct machine *machine, const char *name,
911 const char *short_name, int dso_type)
914 * The kernel dso could be created by build_id processing.
916 struct dso *dso = machine__findnew_dso(machine, name);
919 * We need to run this in all cases, since during the build_id
920 * processing we had no idea this was the kernel dso.
923 dso__set_short_name(dso, short_name, false);
924 dso->kernel = dso_type;
931 * Find a matching entry and/or link current entry to RB tree.
932 * Either one of the dso or name parameter must be non-NULL or the
933 * function will not work.
935 static struct dso *__dso__findlink_by_longname(struct rb_root *root,
936 struct dso *dso, const char *name)
938 struct rb_node **p = &root->rb_node;
939 struct rb_node *parent = NULL;
942 name = dso->long_name;
944 * Find node with the matching name
947 struct dso *this = rb_entry(*p, struct dso, rb_node);
948 int rc = strcmp(name, this->long_name);
953 * In case the new DSO is a duplicate of an existing
954 * one, print a one-time warning & put the new entry
955 * at the end of the list of duplicates.
957 if (!dso || (dso == this))
958 return this; /* Find matching dso */
960 * The core kernel DSOs may have duplicated long name.
961 * In this case, the short name should be different.
962 * Comparing the short names to differentiate the DSOs.
964 rc = strcmp(dso->short_name, this->short_name);
966 pr_err("Duplicated dso name: %s\n", name);
971 p = &parent->rb_left;
973 p = &parent->rb_right;
976 /* Add new node and rebalance tree */
977 rb_link_node(&dso->rb_node, parent, p);
978 rb_insert_color(&dso->rb_node, root);
984 static inline struct dso *__dso__find_by_longname(struct rb_root *root,
987 return __dso__findlink_by_longname(root, NULL, name);
990 void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
992 struct rb_root *root = dso->root;
997 if (dso->long_name_allocated)
998 free((char *)dso->long_name);
1001 rb_erase(&dso->rb_node, root);
1003 * __dso__findlink_by_longname() isn't guaranteed to add it
1004 * back, so a clean removal is required here.
1006 RB_CLEAR_NODE(&dso->rb_node);
1010 dso->long_name = name;
1011 dso->long_name_len = strlen(name);
1012 dso->long_name_allocated = name_allocated;
1015 __dso__findlink_by_longname(root, dso, NULL);
1018 void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
1023 if (dso->short_name_allocated)
1024 free((char *)dso->short_name);
1026 dso->short_name = name;
1027 dso->short_name_len = strlen(name);
1028 dso->short_name_allocated = name_allocated;
1031 static void dso__set_basename(struct dso *dso)
1034 * basename() may modify path buffer, so we must pass
1037 char *base, *lname = strdup(dso->long_name);
1043 * basename() may return a pointer to internal
1044 * storage which is reused in subsequent calls
1045 * so copy the result.
1047 base = strdup(basename(lname));
1054 dso__set_short_name(dso, base, true);
1057 int dso__name_len(const struct dso *dso)
1060 return strlen("[unknown]");
1062 return dso->long_name_len;
1064 return dso->short_name_len;
1067 bool dso__loaded(const struct dso *dso, enum map_type type)
1069 return dso->loaded & (1 << type);
1072 bool dso__sorted_by_name(const struct dso *dso, enum map_type type)
1074 return dso->sorted_by_name & (1 << type);
1077 void dso__set_sorted_by_name(struct dso *dso, enum map_type type)
1079 dso->sorted_by_name |= (1 << type);
1082 struct dso *dso__new(const char *name)
1084 struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
1088 strcpy(dso->name, name);
1089 dso__set_long_name(dso, dso->name, false);
1090 dso__set_short_name(dso, dso->name, false);
1091 for (i = 0; i < MAP__NR_TYPES; ++i)
1092 dso->symbols[i] = dso->symbol_names[i] = RB_ROOT;
1093 dso->data.cache = RB_ROOT;
1095 dso->data.status = DSO_DATA_STATUS_UNKNOWN;
1096 dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
1097 dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
1098 dso->is_64_bit = (sizeof(void *) == 8);
1101 dso->sorted_by_name = 0;
1102 dso->has_build_id = 0;
1103 dso->has_srcline = 1;
1105 dso->kernel = DSO_TYPE_USER;
1106 dso->needs_swap = DSO_SWAP__UNSET;
1107 RB_CLEAR_NODE(&dso->rb_node);
1109 INIT_LIST_HEAD(&dso->node);
1110 INIT_LIST_HEAD(&dso->data.open_entry);
1111 pthread_mutex_init(&dso->lock, NULL);
1112 atomic_set(&dso->refcnt, 1);
1118 void dso__delete(struct dso *dso)
1122 if (!RB_EMPTY_NODE(&dso->rb_node))
1123 pr_err("DSO %s is still in rbtree when being deleted!\n",
1125 for (i = 0; i < MAP__NR_TYPES; ++i)
1126 symbols__delete(&dso->symbols[i]);
1128 if (dso->short_name_allocated) {
1129 zfree((char **)&dso->short_name);
1130 dso->short_name_allocated = false;
1133 if (dso->long_name_allocated) {
1134 zfree((char **)&dso->long_name);
1135 dso->long_name_allocated = false;
1138 dso__data_close(dso);
1139 auxtrace_cache__free(dso->auxtrace_cache);
1140 dso_cache__free(dso);
1142 zfree(&dso->symsrc_filename);
1143 pthread_mutex_destroy(&dso->lock);
1147 struct dso *dso__get(struct dso *dso)
1150 atomic_inc(&dso->refcnt);
1154 void dso__put(struct dso *dso)
1156 if (dso && atomic_dec_and_test(&dso->refcnt))
1160 void dso__set_build_id(struct dso *dso, void *build_id)
1162 memcpy(dso->build_id, build_id, sizeof(dso->build_id));
1163 dso->has_build_id = 1;
1166 bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
1168 return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
1171 void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
1173 char path[PATH_MAX];
1175 if (machine__is_default_guest(machine))
1177 sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
1178 if (sysfs__read_build_id(path, dso->build_id,
1179 sizeof(dso->build_id)) == 0)
1180 dso->has_build_id = true;
1183 int dso__kernel_module_get_build_id(struct dso *dso,
1184 const char *root_dir)
1186 char filename[PATH_MAX];
1188 * kernel module short names are of the form "[module]" and
1189 * we need just "module" here.
1191 const char *name = dso->short_name + 1;
1193 snprintf(filename, sizeof(filename),
1194 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1195 root_dir, (int)strlen(name) - 1, name);
1197 if (sysfs__read_build_id(filename, dso->build_id,
1198 sizeof(dso->build_id)) == 0)
1199 dso->has_build_id = true;
1204 bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
1206 bool have_build_id = false;
1209 list_for_each_entry(pos, head, node) {
1210 if (with_hits && !pos->hit && !dso__is_vdso(pos))
1212 if (pos->has_build_id) {
1213 have_build_id = true;
1216 if (filename__read_build_id(pos->long_name, pos->build_id,
1217 sizeof(pos->build_id)) > 0) {
1218 have_build_id = true;
1219 pos->has_build_id = true;
1223 return have_build_id;
1226 void __dsos__add(struct dsos *dsos, struct dso *dso)
1228 list_add_tail(&dso->node, &dsos->head);
1229 __dso__findlink_by_longname(&dsos->root, dso, NULL);
1231 * It is now in the linked list, grab a reference, then garbage collect
1232 * this when needing memory, by looking at LRU dso instances in the
1233 * list with atomic_read(&dso->refcnt) == 1, i.e. no references
1234 * anywhere besides the one for the list, do, under a lock for the
1235 * list: remove it from the list, then a dso__put(), that probably will
1236 * be the last and will then call dso__delete(), end of life.
1238 * That, or at the end of the 'struct machine' lifetime, when all
1239 * 'struct dso' instances will be removed from the list, in
1240 * dsos__exit(), if they have no other reference from some other data
1243 * E.g.: after processing a 'perf.data' file and storing references
1244 * to objects instantiated while processing events, we will have
1245 * references to the 'thread', 'map', 'dso' structs all from 'struct
1246 * hist_entry' instances, but we may not need anything not referenced,
1247 * so we might as well call machines__exit()/machines__delete() and
1248 * garbage collect it.
1253 void dsos__add(struct dsos *dsos, struct dso *dso)
1255 pthread_rwlock_wrlock(&dsos->lock);
1256 __dsos__add(dsos, dso);
1257 pthread_rwlock_unlock(&dsos->lock);
1260 struct dso *__dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
1265 list_for_each_entry(pos, &dsos->head, node)
1266 if (strcmp(pos->short_name, name) == 0)
1270 return __dso__find_by_longname(&dsos->root, name);
1273 struct dso *dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
1276 pthread_rwlock_rdlock(&dsos->lock);
1277 dso = __dsos__find(dsos, name, cmp_short);
1278 pthread_rwlock_unlock(&dsos->lock);
1282 struct dso *__dsos__addnew(struct dsos *dsos, const char *name)
1284 struct dso *dso = dso__new(name);
1287 __dsos__add(dsos, dso);
1288 dso__set_basename(dso);
1289 /* Put dso here because __dsos_add already got it */
1295 struct dso *__dsos__findnew(struct dsos *dsos, const char *name)
1297 struct dso *dso = __dsos__find(dsos, name, false);
1299 return dso ? dso : __dsos__addnew(dsos, name);
1302 struct dso *dsos__findnew(struct dsos *dsos, const char *name)
1305 pthread_rwlock_wrlock(&dsos->lock);
1306 dso = dso__get(__dsos__findnew(dsos, name));
1307 pthread_rwlock_unlock(&dsos->lock);
1311 size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
1312 bool (skip)(struct dso *dso, int parm), int parm)
1317 list_for_each_entry(pos, head, node) {
1318 if (skip && skip(pos, parm))
1320 ret += dso__fprintf_buildid(pos, fp);
1321 ret += fprintf(fp, " %s\n", pos->long_name);
1326 size_t __dsos__fprintf(struct list_head *head, FILE *fp)
1331 list_for_each_entry(pos, head, node) {
1333 for (i = 0; i < MAP__NR_TYPES; ++i)
1334 ret += dso__fprintf(pos, i, fp);
1340 size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
1342 char sbuild_id[SBUILD_ID_SIZE];
1344 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1345 return fprintf(fp, "%s", sbuild_id);
1348 size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp)
1351 size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
1353 if (dso->short_name != dso->long_name)
1354 ret += fprintf(fp, "%s, ", dso->long_name);
1355 ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
1356 dso__loaded(dso, type) ? "" : "NOT ");
1357 ret += dso__fprintf_buildid(dso, fp);
1358 ret += fprintf(fp, ")\n");
1359 for (nd = rb_first(&dso->symbols[type]); nd; nd = rb_next(nd)) {
1360 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
1361 ret += symbol__fprintf(pos, fp);
1367 enum dso_type dso__type(struct dso *dso, struct machine *machine)
1370 enum dso_type type = DSO__TYPE_UNKNOWN;
1372 fd = dso__data_get_fd(dso, machine);
1374 type = dso__type_fd(fd);
1375 dso__data_put_fd(dso);
1381 int dso__strerror_load(struct dso *dso, char *buf, size_t buflen)
1383 int idx, errnum = dso->load_errno;
1385 * This must have a same ordering as the enum dso_load_errno.
1387 static const char *dso_load__error_str[] = {
1388 "Internal tools/perf/ library error",
1390 "Can not read build id",
1391 "Mismatching build id",
1392 "Decompression failure",
1395 BUG_ON(buflen == 0);
1398 const char *err = str_error_r(errnum, buf, buflen);
1401 scnprintf(buf, buflen, "%s", err);
1406 if (errnum < __DSO_LOAD_ERRNO__START || errnum >= __DSO_LOAD_ERRNO__END)
1409 idx = errnum - __DSO_LOAD_ERRNO__START;
1410 scnprintf(buf, buflen, "%s", dso_load__error_str[idx]);