2 #include <linux/kernel.h>
4 #include <sys/resource.h>
20 static const char * const debuglink_paths[] = {
27 char dso__symtab_origin(const struct dso *dso)
29 static const char origin[] = {
30 [DSO_BINARY_TYPE__KALLSYMS] = 'k',
31 [DSO_BINARY_TYPE__VMLINUX] = 'v',
32 [DSO_BINARY_TYPE__JAVA_JIT] = 'j',
33 [DSO_BINARY_TYPE__DEBUGLINK] = 'l',
34 [DSO_BINARY_TYPE__BUILD_ID_CACHE] = 'B',
35 [DSO_BINARY_TYPE__FEDORA_DEBUGINFO] = 'f',
36 [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO] = 'u',
37 [DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO] = 'o',
38 [DSO_BINARY_TYPE__BUILDID_DEBUGINFO] = 'b',
39 [DSO_BINARY_TYPE__SYSTEM_PATH_DSO] = 'd',
40 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE] = 'K',
41 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP] = 'm',
42 [DSO_BINARY_TYPE__GUEST_KALLSYMS] = 'g',
43 [DSO_BINARY_TYPE__GUEST_KMODULE] = 'G',
44 [DSO_BINARY_TYPE__GUEST_KMODULE_COMP] = 'M',
45 [DSO_BINARY_TYPE__GUEST_VMLINUX] = 'V',
48 if (dso == NULL || dso->symtab_type == DSO_BINARY_TYPE__NOT_FOUND)
50 return origin[dso->symtab_type];
53 int dso__read_binary_type_filename(const struct dso *dso,
54 enum dso_binary_type type,
55 char *root_dir, char *filename, size_t size)
57 char build_id_hex[SBUILD_ID_SIZE];
62 case DSO_BINARY_TYPE__DEBUGLINK:
64 const char *last_slash;
65 char dso_dir[PATH_MAX];
66 char symfile[PATH_MAX];
69 len = __symbol__join_symfs(filename, size, dso->long_name);
70 last_slash = filename + len;
71 while (last_slash != filename && *last_slash != '/')
74 strncpy(dso_dir, filename, last_slash - filename);
75 dso_dir[last_slash-filename] = '\0';
77 if (!is_regular_file(filename)) {
82 ret = filename__read_debuglink(filename, symfile, PATH_MAX);
86 /* Check predefined locations where debug file might reside */
88 for (i = 0; i < ARRAY_SIZE(debuglink_paths); i++) {
89 snprintf(filename, size,
90 debuglink_paths[i], dso_dir, symfile);
91 if (is_regular_file(filename)) {
99 case DSO_BINARY_TYPE__BUILD_ID_CACHE:
100 if (dso__build_id_filename(dso, filename, size) == NULL)
104 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
105 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
106 snprintf(filename + len, size - len, "%s.debug", dso->long_name);
109 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
110 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
111 snprintf(filename + len, size - len, "%s", dso->long_name);
114 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
116 const char *last_slash;
119 last_slash = dso->long_name + dso->long_name_len;
120 while (last_slash != dso->long_name && *last_slash != '/')
123 len = __symbol__join_symfs(filename, size, "");
124 dir_size = last_slash - dso->long_name + 2;
125 if (dir_size > (size - len)) {
129 len += scnprintf(filename + len, dir_size, "%s", dso->long_name);
130 len += scnprintf(filename + len , size - len, ".debug%s",
135 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
136 if (!dso->has_build_id) {
141 build_id__sprintf(dso->build_id,
142 sizeof(dso->build_id),
144 len = __symbol__join_symfs(filename, size, "/usr/lib/debug/.build-id/");
145 snprintf(filename + len, size - len, "%.2s/%s.debug",
146 build_id_hex, build_id_hex + 2);
149 case DSO_BINARY_TYPE__VMLINUX:
150 case DSO_BINARY_TYPE__GUEST_VMLINUX:
151 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
152 __symbol__join_symfs(filename, size, dso->long_name);
155 case DSO_BINARY_TYPE__GUEST_KMODULE:
156 case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
157 path__join3(filename, size, symbol_conf.symfs,
158 root_dir, dso->long_name);
161 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
162 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
163 __symbol__join_symfs(filename, size, dso->long_name);
166 case DSO_BINARY_TYPE__KCORE:
167 case DSO_BINARY_TYPE__GUEST_KCORE:
168 snprintf(filename, size, "%s", dso->long_name);
172 case DSO_BINARY_TYPE__KALLSYMS:
173 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
174 case DSO_BINARY_TYPE__JAVA_JIT:
175 case DSO_BINARY_TYPE__NOT_FOUND:
183 static const struct {
185 int (*decompress)(const char *input, int output);
187 #ifdef HAVE_ZLIB_SUPPORT
188 { "gz", gzip_decompress_to_file },
190 #ifdef HAVE_LZMA_SUPPORT
191 { "xz", lzma_decompress_to_file },
196 bool is_supported_compression(const char *ext)
200 for (i = 0; compressions[i].fmt; i++) {
201 if (!strcmp(ext, compressions[i].fmt))
207 bool is_kernel_module(const char *pathname, int cpumode)
210 int mode = cpumode & PERF_RECORD_MISC_CPUMODE_MASK;
212 WARN_ONCE(mode != cpumode,
213 "Internal error: passing unmasked cpumode (%x) to is_kernel_module",
217 case PERF_RECORD_MISC_USER:
218 case PERF_RECORD_MISC_HYPERVISOR:
219 case PERF_RECORD_MISC_GUEST_USER:
221 /* Treat PERF_RECORD_MISC_CPUMODE_UNKNOWN as kernel */
223 if (kmod_path__parse(&m, pathname)) {
224 pr_err("Failed to check whether %s is a kernel module or not. Assume it is.",
233 bool decompress_to_file(const char *ext, const char *filename, int output_fd)
237 for (i = 0; compressions[i].fmt; i++) {
238 if (!strcmp(ext, compressions[i].fmt))
239 return !compressions[i].decompress(filename,
245 bool dso__needs_decompress(struct dso *dso)
247 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
248 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
252 * Parses kernel module specified in @path and updates
255 * @comp - true if @path contains supported compression suffix,
257 * @kmod - true if @path contains '.ko' suffix in right position,
259 * @name - if (@alloc_name && @kmod) is true, it contains strdup-ed base name
260 * of the kernel module without suffixes, otherwise strudup-ed
262 * @ext - if (@alloc_ext && @comp) is true, it contains strdup-ed string
263 * the compression suffix
265 * Returns 0 if there's no strdup error, -ENOMEM otherwise.
267 int __kmod_path__parse(struct kmod_path *m, const char *path,
268 bool alloc_name, bool alloc_ext)
270 const char *name = strrchr(path, '/');
271 const char *ext = strrchr(path, '.');
272 bool is_simple_name = false;
274 memset(m, 0x0, sizeof(*m));
275 name = name ? name + 1 : path;
278 * '.' is also a valid character for module name. For example:
279 * [aaa.bbb] is a valid module name. '[' should have higher
280 * priority than '.ko' suffix.
282 * The kernel names are from machine__mmap_name. Such
283 * name should belong to kernel itself, not kernel module.
285 if (name[0] == '[') {
286 is_simple_name = true;
287 if ((strncmp(name, "[kernel.kallsyms]", 17) == 0) ||
288 (strncmp(name, "[guest.kernel.kallsyms", 22) == 0) ||
289 (strncmp(name, "[vdso]", 6) == 0) ||
290 (strncmp(name, "[vsyscall]", 10) == 0)) {
297 /* No extension, just return name. */
298 if ((ext == NULL) || is_simple_name) {
300 m->name = strdup(name);
301 return m->name ? 0 : -ENOMEM;
306 if (is_supported_compression(ext + 1)) {
311 /* Check .ko extension only if there's enough name left. */
313 m->kmod = !strncmp(ext, ".ko", 3);
317 if (asprintf(&m->name, "[%.*s]", (int) (ext - name), name) == -1)
320 if (asprintf(&m->name, "%s", name) == -1)
324 strxfrchar(m->name, '-', '_');
327 if (alloc_ext && m->comp) {
328 m->ext = strdup(ext + 4);
330 free((void *) m->name);
339 * Global list of open DSOs and the counter.
341 static LIST_HEAD(dso__data_open);
342 static long dso__data_open_cnt;
343 static pthread_mutex_t dso__data_open_lock = PTHREAD_MUTEX_INITIALIZER;
345 static void dso__list_add(struct dso *dso)
347 list_add_tail(&dso->data.open_entry, &dso__data_open);
348 dso__data_open_cnt++;
351 static void dso__list_del(struct dso *dso)
353 list_del(&dso->data.open_entry);
354 WARN_ONCE(dso__data_open_cnt <= 0,
355 "DSO data fd counter out of bounds.");
356 dso__data_open_cnt--;
359 static void close_first_dso(void);
361 static int do_open(char *name)
364 char sbuf[STRERR_BUFSIZE];
367 fd = open(name, O_RDONLY);
371 pr_debug("dso open failed: %s\n",
372 str_error_r(errno, sbuf, sizeof(sbuf)));
373 if (!dso__data_open_cnt || errno != EMFILE)
382 static int __open_dso(struct dso *dso, struct machine *machine)
385 char *root_dir = (char *)"";
386 char *name = malloc(PATH_MAX);
392 root_dir = machine->root_dir;
394 if (dso__read_binary_type_filename(dso, dso->binary_type,
395 root_dir, name, PATH_MAX)) {
400 if (!is_regular_file(name))
408 static void check_data_close(void);
411 * dso_close - Open DSO data file
414 * Open @dso's data file descriptor and updates
415 * list/count of open DSO objects.
417 static int open_dso(struct dso *dso, struct machine *machine)
419 int fd = __open_dso(dso, machine);
424 * Check if we crossed the allowed number
425 * of opened DSOs and close one if needed.
433 static void close_data_fd(struct dso *dso)
435 if (dso->data.fd >= 0) {
438 dso->data.file_size = 0;
444 * dso_close - Close DSO data file
447 * Close @dso's data file descriptor and updates
448 * list/count of open DSO objects.
450 static void close_dso(struct dso *dso)
455 static void close_first_dso(void)
459 dso = list_first_entry(&dso__data_open, struct dso, data.open_entry);
463 static rlim_t get_fd_limit(void)
468 /* Allow half of the current open fd limit. */
469 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
470 if (l.rlim_cur == RLIM_INFINITY)
473 limit = l.rlim_cur / 2;
475 pr_err("failed to get fd limit\n");
482 static rlim_t fd_limit;
485 * Used only by tests/dso-data.c to reset the environment
486 * for tests. I dont expect we should change this during
489 void reset_fd_limit(void)
494 static bool may_cache_fd(void)
497 fd_limit = get_fd_limit();
499 if (fd_limit == RLIM_INFINITY)
502 return fd_limit > (rlim_t) dso__data_open_cnt;
506 * Check and close LRU dso if we crossed allowed limit
507 * for opened dso file descriptors. The limit is half
508 * of the RLIMIT_NOFILE files opened.
510 static void check_data_close(void)
512 bool cache_fd = may_cache_fd();
519 * dso__data_close - Close DSO data file
522 * External interface to close @dso's data file descriptor.
524 void dso__data_close(struct dso *dso)
526 pthread_mutex_lock(&dso__data_open_lock);
528 pthread_mutex_unlock(&dso__data_open_lock);
531 static void try_to_open_dso(struct dso *dso, struct machine *machine)
533 enum dso_binary_type binary_type_data[] = {
534 DSO_BINARY_TYPE__BUILD_ID_CACHE,
535 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
536 DSO_BINARY_TYPE__NOT_FOUND,
540 if (dso->data.fd >= 0)
543 if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) {
544 dso->data.fd = open_dso(dso, machine);
549 dso->binary_type = binary_type_data[i++];
551 dso->data.fd = open_dso(dso, machine);
552 if (dso->data.fd >= 0)
555 } while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND);
557 if (dso->data.fd >= 0)
558 dso->data.status = DSO_DATA_STATUS_OK;
560 dso->data.status = DSO_DATA_STATUS_ERROR;
564 * dso__data_get_fd - Get dso's data file descriptor
566 * @machine: machine object
568 * External interface to find dso's file, open it and
569 * returns file descriptor. It should be paired with
570 * dso__data_put_fd() if it returns non-negative value.
572 int dso__data_get_fd(struct dso *dso, struct machine *machine)
574 if (dso->data.status == DSO_DATA_STATUS_ERROR)
577 if (pthread_mutex_lock(&dso__data_open_lock) < 0)
580 try_to_open_dso(dso, machine);
582 if (dso->data.fd < 0)
583 pthread_mutex_unlock(&dso__data_open_lock);
588 void dso__data_put_fd(struct dso *dso __maybe_unused)
590 pthread_mutex_unlock(&dso__data_open_lock);
593 bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by)
597 if (dso->data.status_seen & flag)
600 dso->data.status_seen |= flag;
606 dso_cache__free(struct dso *dso)
608 struct rb_root *root = &dso->data.cache;
609 struct rb_node *next = rb_first(root);
611 pthread_mutex_lock(&dso->lock);
613 struct dso_cache *cache;
615 cache = rb_entry(next, struct dso_cache, rb_node);
616 next = rb_next(&cache->rb_node);
617 rb_erase(&cache->rb_node, root);
620 pthread_mutex_unlock(&dso->lock);
623 static struct dso_cache *dso_cache__find(struct dso *dso, u64 offset)
625 const struct rb_root *root = &dso->data.cache;
626 struct rb_node * const *p = &root->rb_node;
627 const struct rb_node *parent = NULL;
628 struct dso_cache *cache;
634 cache = rb_entry(parent, struct dso_cache, rb_node);
635 end = cache->offset + DSO__DATA_CACHE_SIZE;
637 if (offset < cache->offset)
639 else if (offset >= end)
648 static struct dso_cache *
649 dso_cache__insert(struct dso *dso, struct dso_cache *new)
651 struct rb_root *root = &dso->data.cache;
652 struct rb_node **p = &root->rb_node;
653 struct rb_node *parent = NULL;
654 struct dso_cache *cache;
655 u64 offset = new->offset;
657 pthread_mutex_lock(&dso->lock);
662 cache = rb_entry(parent, struct dso_cache, rb_node);
663 end = cache->offset + DSO__DATA_CACHE_SIZE;
665 if (offset < cache->offset)
667 else if (offset >= end)
673 rb_link_node(&new->rb_node, parent, p);
674 rb_insert_color(&new->rb_node, root);
678 pthread_mutex_unlock(&dso->lock);
683 dso_cache__memcpy(struct dso_cache *cache, u64 offset,
686 u64 cache_offset = offset - cache->offset;
687 u64 cache_size = min(cache->size - cache_offset, size);
689 memcpy(data, cache->data + cache_offset, cache_size);
694 dso_cache__read(struct dso *dso, struct machine *machine,
695 u64 offset, u8 *data, ssize_t size)
697 struct dso_cache *cache;
698 struct dso_cache *old;
704 cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
708 pthread_mutex_lock(&dso__data_open_lock);
711 * dso->data.fd might be closed if other thread opened another
712 * file (dso) due to open file limit (RLIMIT_NOFILE).
714 try_to_open_dso(dso, machine);
716 if (dso->data.fd < 0) {
718 dso->data.status = DSO_DATA_STATUS_ERROR;
722 cache_offset = offset & DSO__DATA_CACHE_MASK;
724 ret = pread(dso->data.fd, cache->data, DSO__DATA_CACHE_SIZE, cache_offset);
728 cache->offset = cache_offset;
732 pthread_mutex_unlock(&dso__data_open_lock);
735 old = dso_cache__insert(dso, cache);
737 /* we lose the race */
742 ret = dso_cache__memcpy(cache, offset, data, size);
751 static ssize_t dso_cache_read(struct dso *dso, struct machine *machine,
752 u64 offset, u8 *data, ssize_t size)
754 struct dso_cache *cache;
756 cache = dso_cache__find(dso, offset);
758 return dso_cache__memcpy(cache, offset, data, size);
760 return dso_cache__read(dso, machine, offset, data, size);
764 * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
765 * in the rb_tree. Any read to already cached data is served
768 static ssize_t cached_read(struct dso *dso, struct machine *machine,
769 u64 offset, u8 *data, ssize_t size)
777 ret = dso_cache_read(dso, machine, offset, p, size);
781 /* Reached EOF, return what we have. */
797 static int data_file_size(struct dso *dso, struct machine *machine)
801 char sbuf[STRERR_BUFSIZE];
803 if (dso->data.file_size)
806 if (dso->data.status == DSO_DATA_STATUS_ERROR)
809 pthread_mutex_lock(&dso__data_open_lock);
812 * dso->data.fd might be closed if other thread opened another
813 * file (dso) due to open file limit (RLIMIT_NOFILE).
815 try_to_open_dso(dso, machine);
817 if (dso->data.fd < 0) {
819 dso->data.status = DSO_DATA_STATUS_ERROR;
823 if (fstat(dso->data.fd, &st) < 0) {
825 pr_err("dso cache fstat failed: %s\n",
826 str_error_r(errno, sbuf, sizeof(sbuf)));
827 dso->data.status = DSO_DATA_STATUS_ERROR;
830 dso->data.file_size = st.st_size;
833 pthread_mutex_unlock(&dso__data_open_lock);
838 * dso__data_size - Return dso data size
840 * @machine: machine object
842 * Return: dso data size
844 off_t dso__data_size(struct dso *dso, struct machine *machine)
846 if (data_file_size(dso, machine))
849 /* For now just estimate dso data size is close to file size */
850 return dso->data.file_size;
853 static ssize_t data_read_offset(struct dso *dso, struct machine *machine,
854 u64 offset, u8 *data, ssize_t size)
856 if (data_file_size(dso, machine))
859 /* Check the offset sanity. */
860 if (offset > dso->data.file_size)
863 if (offset + size < offset)
866 return cached_read(dso, machine, offset, data, size);
870 * dso__data_read_offset - Read data from dso file offset
872 * @machine: machine object
873 * @offset: file offset
874 * @data: buffer to store data
875 * @size: size of the @data buffer
877 * External interface to read data from dso file offset. Open
878 * dso data file and use cached_read to get the data.
880 ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
881 u64 offset, u8 *data, ssize_t size)
883 if (dso->data.status == DSO_DATA_STATUS_ERROR)
886 return data_read_offset(dso, machine, offset, data, size);
890 * dso__data_read_addr - Read data from dso address
892 * @machine: machine object
893 * @add: virtual memory address
894 * @data: buffer to store data
895 * @size: size of the @data buffer
897 * External interface to read data from dso address.
899 ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
900 struct machine *machine, u64 addr,
901 u8 *data, ssize_t size)
903 u64 offset = map->map_ip(map, addr);
904 return dso__data_read_offset(dso, machine, offset, data, size);
907 struct map *dso__new_map(const char *name)
909 struct map *map = NULL;
910 struct dso *dso = dso__new(name);
913 map = map__new2(0, dso, MAP__FUNCTION);
918 struct dso *machine__findnew_kernel(struct machine *machine, const char *name,
919 const char *short_name, int dso_type)
922 * The kernel dso could be created by build_id processing.
924 struct dso *dso = machine__findnew_dso(machine, name);
927 * We need to run this in all cases, since during the build_id
928 * processing we had no idea this was the kernel dso.
931 dso__set_short_name(dso, short_name, false);
932 dso->kernel = dso_type;
939 * Find a matching entry and/or link current entry to RB tree.
940 * Either one of the dso or name parameter must be non-NULL or the
941 * function will not work.
943 static struct dso *__dso__findlink_by_longname(struct rb_root *root,
944 struct dso *dso, const char *name)
946 struct rb_node **p = &root->rb_node;
947 struct rb_node *parent = NULL;
950 name = dso->long_name;
952 * Find node with the matching name
955 struct dso *this = rb_entry(*p, struct dso, rb_node);
956 int rc = strcmp(name, this->long_name);
961 * In case the new DSO is a duplicate of an existing
962 * one, print a one-time warning & put the new entry
963 * at the end of the list of duplicates.
965 if (!dso || (dso == this))
966 return this; /* Find matching dso */
968 * The core kernel DSOs may have duplicated long name.
969 * In this case, the short name should be different.
970 * Comparing the short names to differentiate the DSOs.
972 rc = strcmp(dso->short_name, this->short_name);
974 pr_err("Duplicated dso name: %s\n", name);
979 p = &parent->rb_left;
981 p = &parent->rb_right;
984 /* Add new node and rebalance tree */
985 rb_link_node(&dso->rb_node, parent, p);
986 rb_insert_color(&dso->rb_node, root);
992 static inline struct dso *__dso__find_by_longname(struct rb_root *root,
995 return __dso__findlink_by_longname(root, NULL, name);
998 void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
1000 struct rb_root *root = dso->root;
1005 if (dso->long_name_allocated)
1006 free((char *)dso->long_name);
1009 rb_erase(&dso->rb_node, root);
1011 * __dso__findlink_by_longname() isn't guaranteed to add it
1012 * back, so a clean removal is required here.
1014 RB_CLEAR_NODE(&dso->rb_node);
1018 dso->long_name = name;
1019 dso->long_name_len = strlen(name);
1020 dso->long_name_allocated = name_allocated;
1023 __dso__findlink_by_longname(root, dso, NULL);
1026 void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
1031 if (dso->short_name_allocated)
1032 free((char *)dso->short_name);
1034 dso->short_name = name;
1035 dso->short_name_len = strlen(name);
1036 dso->short_name_allocated = name_allocated;
1039 static void dso__set_basename(struct dso *dso)
1042 * basename() may modify path buffer, so we must pass
1045 char *base, *lname = strdup(dso->long_name);
1051 * basename() may return a pointer to internal
1052 * storage which is reused in subsequent calls
1053 * so copy the result.
1055 base = strdup(basename(lname));
1062 dso__set_short_name(dso, base, true);
1065 int dso__name_len(const struct dso *dso)
1068 return strlen("[unknown]");
1070 return dso->long_name_len;
1072 return dso->short_name_len;
1075 bool dso__loaded(const struct dso *dso, enum map_type type)
1077 return dso->loaded & (1 << type);
1080 bool dso__sorted_by_name(const struct dso *dso, enum map_type type)
1082 return dso->sorted_by_name & (1 << type);
1085 void dso__set_sorted_by_name(struct dso *dso, enum map_type type)
1087 dso->sorted_by_name |= (1 << type);
1090 struct dso *dso__new(const char *name)
1092 struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
1096 strcpy(dso->name, name);
1097 dso__set_long_name(dso, dso->name, false);
1098 dso__set_short_name(dso, dso->name, false);
1099 for (i = 0; i < MAP__NR_TYPES; ++i)
1100 dso->symbols[i] = dso->symbol_names[i] = RB_ROOT;
1101 dso->data.cache = RB_ROOT;
1103 dso->data.status = DSO_DATA_STATUS_UNKNOWN;
1104 dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
1105 dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
1106 dso->is_64_bit = (sizeof(void *) == 8);
1109 dso->sorted_by_name = 0;
1110 dso->has_build_id = 0;
1111 dso->has_srcline = 1;
1113 dso->kernel = DSO_TYPE_USER;
1114 dso->needs_swap = DSO_SWAP__UNSET;
1115 RB_CLEAR_NODE(&dso->rb_node);
1117 INIT_LIST_HEAD(&dso->node);
1118 INIT_LIST_HEAD(&dso->data.open_entry);
1119 pthread_mutex_init(&dso->lock, NULL);
1120 refcount_set(&dso->refcnt, 1);
1126 void dso__delete(struct dso *dso)
1130 if (!RB_EMPTY_NODE(&dso->rb_node))
1131 pr_err("DSO %s is still in rbtree when being deleted!\n",
1133 for (i = 0; i < MAP__NR_TYPES; ++i)
1134 symbols__delete(&dso->symbols[i]);
1136 if (dso->short_name_allocated) {
1137 zfree((char **)&dso->short_name);
1138 dso->short_name_allocated = false;
1141 if (dso->long_name_allocated) {
1142 zfree((char **)&dso->long_name);
1143 dso->long_name_allocated = false;
1146 dso__data_close(dso);
1147 auxtrace_cache__free(dso->auxtrace_cache);
1148 dso_cache__free(dso);
1150 zfree(&dso->symsrc_filename);
1151 pthread_mutex_destroy(&dso->lock);
1155 struct dso *dso__get(struct dso *dso)
1158 refcount_inc(&dso->refcnt);
1162 void dso__put(struct dso *dso)
1164 if (dso && refcount_dec_and_test(&dso->refcnt))
1168 void dso__set_build_id(struct dso *dso, void *build_id)
1170 memcpy(dso->build_id, build_id, sizeof(dso->build_id));
1171 dso->has_build_id = 1;
1174 bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
1176 return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
1179 void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
1181 char path[PATH_MAX];
1183 if (machine__is_default_guest(machine))
1185 sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
1186 if (sysfs__read_build_id(path, dso->build_id,
1187 sizeof(dso->build_id)) == 0)
1188 dso->has_build_id = true;
1191 int dso__kernel_module_get_build_id(struct dso *dso,
1192 const char *root_dir)
1194 char filename[PATH_MAX];
1196 * kernel module short names are of the form "[module]" and
1197 * we need just "module" here.
1199 const char *name = dso->short_name + 1;
1201 snprintf(filename, sizeof(filename),
1202 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1203 root_dir, (int)strlen(name) - 1, name);
1205 if (sysfs__read_build_id(filename, dso->build_id,
1206 sizeof(dso->build_id)) == 0)
1207 dso->has_build_id = true;
1212 bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
1214 bool have_build_id = false;
1217 list_for_each_entry(pos, head, node) {
1218 if (with_hits && !pos->hit && !dso__is_vdso(pos))
1220 if (pos->has_build_id) {
1221 have_build_id = true;
1224 if (filename__read_build_id(pos->long_name, pos->build_id,
1225 sizeof(pos->build_id)) > 0) {
1226 have_build_id = true;
1227 pos->has_build_id = true;
1231 return have_build_id;
1234 void __dsos__add(struct dsos *dsos, struct dso *dso)
1236 list_add_tail(&dso->node, &dsos->head);
1237 __dso__findlink_by_longname(&dsos->root, dso, NULL);
1239 * It is now in the linked list, grab a reference, then garbage collect
1240 * this when needing memory, by looking at LRU dso instances in the
1241 * list with atomic_read(&dso->refcnt) == 1, i.e. no references
1242 * anywhere besides the one for the list, do, under a lock for the
1243 * list: remove it from the list, then a dso__put(), that probably will
1244 * be the last and will then call dso__delete(), end of life.
1246 * That, or at the end of the 'struct machine' lifetime, when all
1247 * 'struct dso' instances will be removed from the list, in
1248 * dsos__exit(), if they have no other reference from some other data
1251 * E.g.: after processing a 'perf.data' file and storing references
1252 * to objects instantiated while processing events, we will have
1253 * references to the 'thread', 'map', 'dso' structs all from 'struct
1254 * hist_entry' instances, but we may not need anything not referenced,
1255 * so we might as well call machines__exit()/machines__delete() and
1256 * garbage collect it.
1261 void dsos__add(struct dsos *dsos, struct dso *dso)
1263 pthread_rwlock_wrlock(&dsos->lock);
1264 __dsos__add(dsos, dso);
1265 pthread_rwlock_unlock(&dsos->lock);
1268 struct dso *__dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
1273 list_for_each_entry(pos, &dsos->head, node)
1274 if (strcmp(pos->short_name, name) == 0)
1278 return __dso__find_by_longname(&dsos->root, name);
1281 struct dso *dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
1284 pthread_rwlock_rdlock(&dsos->lock);
1285 dso = __dsos__find(dsos, name, cmp_short);
1286 pthread_rwlock_unlock(&dsos->lock);
1290 struct dso *__dsos__addnew(struct dsos *dsos, const char *name)
1292 struct dso *dso = dso__new(name);
1295 __dsos__add(dsos, dso);
1296 dso__set_basename(dso);
1297 /* Put dso here because __dsos_add already got it */
1303 struct dso *__dsos__findnew(struct dsos *dsos, const char *name)
1305 struct dso *dso = __dsos__find(dsos, name, false);
1307 return dso ? dso : __dsos__addnew(dsos, name);
1310 struct dso *dsos__findnew(struct dsos *dsos, const char *name)
1313 pthread_rwlock_wrlock(&dsos->lock);
1314 dso = dso__get(__dsos__findnew(dsos, name));
1315 pthread_rwlock_unlock(&dsos->lock);
1319 size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
1320 bool (skip)(struct dso *dso, int parm), int parm)
1325 list_for_each_entry(pos, head, node) {
1326 if (skip && skip(pos, parm))
1328 ret += dso__fprintf_buildid(pos, fp);
1329 ret += fprintf(fp, " %s\n", pos->long_name);
1334 size_t __dsos__fprintf(struct list_head *head, FILE *fp)
1339 list_for_each_entry(pos, head, node) {
1341 for (i = 0; i < MAP__NR_TYPES; ++i)
1342 ret += dso__fprintf(pos, i, fp);
1348 size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
1350 char sbuild_id[SBUILD_ID_SIZE];
1352 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1353 return fprintf(fp, "%s", sbuild_id);
1356 size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp)
1359 size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
1361 if (dso->short_name != dso->long_name)
1362 ret += fprintf(fp, "%s, ", dso->long_name);
1363 ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
1364 dso__loaded(dso, type) ? "" : "NOT ");
1365 ret += dso__fprintf_buildid(dso, fp);
1366 ret += fprintf(fp, ")\n");
1367 for (nd = rb_first(&dso->symbols[type]); nd; nd = rb_next(nd)) {
1368 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
1369 ret += symbol__fprintf(pos, fp);
1375 enum dso_type dso__type(struct dso *dso, struct machine *machine)
1378 enum dso_type type = DSO__TYPE_UNKNOWN;
1380 fd = dso__data_get_fd(dso, machine);
1382 type = dso__type_fd(fd);
1383 dso__data_put_fd(dso);
1389 int dso__strerror_load(struct dso *dso, char *buf, size_t buflen)
1391 int idx, errnum = dso->load_errno;
1393 * This must have a same ordering as the enum dso_load_errno.
1395 static const char *dso_load__error_str[] = {
1396 "Internal tools/perf/ library error",
1398 "Can not read build id",
1399 "Mismatching build id",
1400 "Decompression failure",
1403 BUG_ON(buflen == 0);
1406 const char *err = str_error_r(errnum, buf, buflen);
1409 scnprintf(buf, buflen, "%s", err);
1414 if (errnum < __DSO_LOAD_ERRNO__START || errnum >= __DSO_LOAD_ERRNO__END)
1417 idx = errnum - __DSO_LOAD_ERRNO__START;
1418 scnprintf(buf, buflen, "%s", dso_load__error_str[idx]);