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);
338 void dso__set_module_info(struct dso *dso, struct kmod_path *m,
339 struct machine *machine)
341 if (machine__is_host(machine))
342 dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE;
344 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE;
346 /* _KMODULE_COMP should be next to _KMODULE */
347 if (m->kmod && m->comp)
350 dso__set_short_name(dso, strdup(m->name), true);
354 * Global list of open DSOs and the counter.
356 static LIST_HEAD(dso__data_open);
357 static long dso__data_open_cnt;
358 static pthread_mutex_t dso__data_open_lock = PTHREAD_MUTEX_INITIALIZER;
360 static void dso__list_add(struct dso *dso)
362 list_add_tail(&dso->data.open_entry, &dso__data_open);
363 dso__data_open_cnt++;
366 static void dso__list_del(struct dso *dso)
368 list_del(&dso->data.open_entry);
369 WARN_ONCE(dso__data_open_cnt <= 0,
370 "DSO data fd counter out of bounds.");
371 dso__data_open_cnt--;
374 static void close_first_dso(void);
376 static int do_open(char *name)
379 char sbuf[STRERR_BUFSIZE];
382 fd = open(name, O_RDONLY);
386 pr_debug("dso open failed: %s\n",
387 str_error_r(errno, sbuf, sizeof(sbuf)));
388 if (!dso__data_open_cnt || errno != EMFILE)
397 static int __open_dso(struct dso *dso, struct machine *machine)
400 char *root_dir = (char *)"";
401 char *name = malloc(PATH_MAX);
407 root_dir = machine->root_dir;
409 if (dso__read_binary_type_filename(dso, dso->binary_type,
410 root_dir, name, PATH_MAX)) {
415 if (!is_regular_file(name))
423 static void check_data_close(void);
426 * dso_close - Open DSO data file
429 * Open @dso's data file descriptor and updates
430 * list/count of open DSO objects.
432 static int open_dso(struct dso *dso, struct machine *machine)
434 int fd = __open_dso(dso, machine);
439 * Check if we crossed the allowed number
440 * of opened DSOs and close one if needed.
448 static void close_data_fd(struct dso *dso)
450 if (dso->data.fd >= 0) {
453 dso->data.file_size = 0;
459 * dso_close - Close DSO data file
462 * Close @dso's data file descriptor and updates
463 * list/count of open DSO objects.
465 static void close_dso(struct dso *dso)
470 static void close_first_dso(void)
474 dso = list_first_entry(&dso__data_open, struct dso, data.open_entry);
478 static rlim_t get_fd_limit(void)
483 /* Allow half of the current open fd limit. */
484 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
485 if (l.rlim_cur == RLIM_INFINITY)
488 limit = l.rlim_cur / 2;
490 pr_err("failed to get fd limit\n");
497 static rlim_t fd_limit;
500 * Used only by tests/dso-data.c to reset the environment
501 * for tests. I dont expect we should change this during
504 void reset_fd_limit(void)
509 static bool may_cache_fd(void)
512 fd_limit = get_fd_limit();
514 if (fd_limit == RLIM_INFINITY)
517 return fd_limit > (rlim_t) dso__data_open_cnt;
521 * Check and close LRU dso if we crossed allowed limit
522 * for opened dso file descriptors. The limit is half
523 * of the RLIMIT_NOFILE files opened.
525 static void check_data_close(void)
527 bool cache_fd = may_cache_fd();
534 * dso__data_close - Close DSO data file
537 * External interface to close @dso's data file descriptor.
539 void dso__data_close(struct dso *dso)
541 pthread_mutex_lock(&dso__data_open_lock);
543 pthread_mutex_unlock(&dso__data_open_lock);
546 static void try_to_open_dso(struct dso *dso, struct machine *machine)
548 enum dso_binary_type binary_type_data[] = {
549 DSO_BINARY_TYPE__BUILD_ID_CACHE,
550 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
551 DSO_BINARY_TYPE__NOT_FOUND,
555 if (dso->data.fd >= 0)
558 if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) {
559 dso->data.fd = open_dso(dso, machine);
564 dso->binary_type = binary_type_data[i++];
566 dso->data.fd = open_dso(dso, machine);
567 if (dso->data.fd >= 0)
570 } while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND);
572 if (dso->data.fd >= 0)
573 dso->data.status = DSO_DATA_STATUS_OK;
575 dso->data.status = DSO_DATA_STATUS_ERROR;
579 * dso__data_get_fd - Get dso's data file descriptor
581 * @machine: machine object
583 * External interface to find dso's file, open it and
584 * returns file descriptor. It should be paired with
585 * dso__data_put_fd() if it returns non-negative value.
587 int dso__data_get_fd(struct dso *dso, struct machine *machine)
589 if (dso->data.status == DSO_DATA_STATUS_ERROR)
592 if (pthread_mutex_lock(&dso__data_open_lock) < 0)
595 try_to_open_dso(dso, machine);
597 if (dso->data.fd < 0)
598 pthread_mutex_unlock(&dso__data_open_lock);
603 void dso__data_put_fd(struct dso *dso __maybe_unused)
605 pthread_mutex_unlock(&dso__data_open_lock);
608 bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by)
612 if (dso->data.status_seen & flag)
615 dso->data.status_seen |= flag;
621 dso_cache__free(struct dso *dso)
623 struct rb_root *root = &dso->data.cache;
624 struct rb_node *next = rb_first(root);
626 pthread_mutex_lock(&dso->lock);
628 struct dso_cache *cache;
630 cache = rb_entry(next, struct dso_cache, rb_node);
631 next = rb_next(&cache->rb_node);
632 rb_erase(&cache->rb_node, root);
635 pthread_mutex_unlock(&dso->lock);
638 static struct dso_cache *dso_cache__find(struct dso *dso, u64 offset)
640 const struct rb_root *root = &dso->data.cache;
641 struct rb_node * const *p = &root->rb_node;
642 const struct rb_node *parent = NULL;
643 struct dso_cache *cache;
649 cache = rb_entry(parent, struct dso_cache, rb_node);
650 end = cache->offset + DSO__DATA_CACHE_SIZE;
652 if (offset < cache->offset)
654 else if (offset >= end)
663 static struct dso_cache *
664 dso_cache__insert(struct dso *dso, struct dso_cache *new)
666 struct rb_root *root = &dso->data.cache;
667 struct rb_node **p = &root->rb_node;
668 struct rb_node *parent = NULL;
669 struct dso_cache *cache;
670 u64 offset = new->offset;
672 pthread_mutex_lock(&dso->lock);
677 cache = rb_entry(parent, struct dso_cache, rb_node);
678 end = cache->offset + DSO__DATA_CACHE_SIZE;
680 if (offset < cache->offset)
682 else if (offset >= end)
688 rb_link_node(&new->rb_node, parent, p);
689 rb_insert_color(&new->rb_node, root);
693 pthread_mutex_unlock(&dso->lock);
698 dso_cache__memcpy(struct dso_cache *cache, u64 offset,
701 u64 cache_offset = offset - cache->offset;
702 u64 cache_size = min(cache->size - cache_offset, size);
704 memcpy(data, cache->data + cache_offset, cache_size);
709 dso_cache__read(struct dso *dso, struct machine *machine,
710 u64 offset, u8 *data, ssize_t size)
712 struct dso_cache *cache;
713 struct dso_cache *old;
719 cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
723 pthread_mutex_lock(&dso__data_open_lock);
726 * dso->data.fd might be closed if other thread opened another
727 * file (dso) due to open file limit (RLIMIT_NOFILE).
729 try_to_open_dso(dso, machine);
731 if (dso->data.fd < 0) {
733 dso->data.status = DSO_DATA_STATUS_ERROR;
737 cache_offset = offset & DSO__DATA_CACHE_MASK;
739 ret = pread(dso->data.fd, cache->data, DSO__DATA_CACHE_SIZE, cache_offset);
743 cache->offset = cache_offset;
747 pthread_mutex_unlock(&dso__data_open_lock);
750 old = dso_cache__insert(dso, cache);
752 /* we lose the race */
757 ret = dso_cache__memcpy(cache, offset, data, size);
766 static ssize_t dso_cache_read(struct dso *dso, struct machine *machine,
767 u64 offset, u8 *data, ssize_t size)
769 struct dso_cache *cache;
771 cache = dso_cache__find(dso, offset);
773 return dso_cache__memcpy(cache, offset, data, size);
775 return dso_cache__read(dso, machine, offset, data, size);
779 * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
780 * in the rb_tree. Any read to already cached data is served
783 static ssize_t cached_read(struct dso *dso, struct machine *machine,
784 u64 offset, u8 *data, ssize_t size)
792 ret = dso_cache_read(dso, machine, offset, p, size);
796 /* Reached EOF, return what we have. */
812 static int data_file_size(struct dso *dso, struct machine *machine)
816 char sbuf[STRERR_BUFSIZE];
818 if (dso->data.file_size)
821 if (dso->data.status == DSO_DATA_STATUS_ERROR)
824 pthread_mutex_lock(&dso__data_open_lock);
827 * dso->data.fd might be closed if other thread opened another
828 * file (dso) due to open file limit (RLIMIT_NOFILE).
830 try_to_open_dso(dso, machine);
832 if (dso->data.fd < 0) {
834 dso->data.status = DSO_DATA_STATUS_ERROR;
838 if (fstat(dso->data.fd, &st) < 0) {
840 pr_err("dso cache fstat failed: %s\n",
841 str_error_r(errno, sbuf, sizeof(sbuf)));
842 dso->data.status = DSO_DATA_STATUS_ERROR;
845 dso->data.file_size = st.st_size;
848 pthread_mutex_unlock(&dso__data_open_lock);
853 * dso__data_size - Return dso data size
855 * @machine: machine object
857 * Return: dso data size
859 off_t dso__data_size(struct dso *dso, struct machine *machine)
861 if (data_file_size(dso, machine))
864 /* For now just estimate dso data size is close to file size */
865 return dso->data.file_size;
868 static ssize_t data_read_offset(struct dso *dso, struct machine *machine,
869 u64 offset, u8 *data, ssize_t size)
871 if (data_file_size(dso, machine))
874 /* Check the offset sanity. */
875 if (offset > dso->data.file_size)
878 if (offset + size < offset)
881 return cached_read(dso, machine, offset, data, size);
885 * dso__data_read_offset - Read data from dso file offset
887 * @machine: machine object
888 * @offset: file offset
889 * @data: buffer to store data
890 * @size: size of the @data buffer
892 * External interface to read data from dso file offset. Open
893 * dso data file and use cached_read to get the data.
895 ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
896 u64 offset, u8 *data, ssize_t size)
898 if (dso->data.status == DSO_DATA_STATUS_ERROR)
901 return data_read_offset(dso, machine, offset, data, size);
905 * dso__data_read_addr - Read data from dso address
907 * @machine: machine object
908 * @add: virtual memory address
909 * @data: buffer to store data
910 * @size: size of the @data buffer
912 * External interface to read data from dso address.
914 ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
915 struct machine *machine, u64 addr,
916 u8 *data, ssize_t size)
918 u64 offset = map->map_ip(map, addr);
919 return dso__data_read_offset(dso, machine, offset, data, size);
922 struct map *dso__new_map(const char *name)
924 struct map *map = NULL;
925 struct dso *dso = dso__new(name);
928 map = map__new2(0, dso, MAP__FUNCTION);
933 struct dso *machine__findnew_kernel(struct machine *machine, const char *name,
934 const char *short_name, int dso_type)
937 * The kernel dso could be created by build_id processing.
939 struct dso *dso = machine__findnew_dso(machine, name);
942 * We need to run this in all cases, since during the build_id
943 * processing we had no idea this was the kernel dso.
946 dso__set_short_name(dso, short_name, false);
947 dso->kernel = dso_type;
954 * Find a matching entry and/or link current entry to RB tree.
955 * Either one of the dso or name parameter must be non-NULL or the
956 * function will not work.
958 static struct dso *__dso__findlink_by_longname(struct rb_root *root,
959 struct dso *dso, const char *name)
961 struct rb_node **p = &root->rb_node;
962 struct rb_node *parent = NULL;
965 name = dso->long_name;
967 * Find node with the matching name
970 struct dso *this = rb_entry(*p, struct dso, rb_node);
971 int rc = strcmp(name, this->long_name);
976 * In case the new DSO is a duplicate of an existing
977 * one, print a one-time warning & put the new entry
978 * at the end of the list of duplicates.
980 if (!dso || (dso == this))
981 return this; /* Find matching dso */
983 * The core kernel DSOs may have duplicated long name.
984 * In this case, the short name should be different.
985 * Comparing the short names to differentiate the DSOs.
987 rc = strcmp(dso->short_name, this->short_name);
989 pr_err("Duplicated dso name: %s\n", name);
994 p = &parent->rb_left;
996 p = &parent->rb_right;
999 /* Add new node and rebalance tree */
1000 rb_link_node(&dso->rb_node, parent, p);
1001 rb_insert_color(&dso->rb_node, root);
1007 static inline struct dso *__dso__find_by_longname(struct rb_root *root,
1010 return __dso__findlink_by_longname(root, NULL, name);
1013 void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
1015 struct rb_root *root = dso->root;
1020 if (dso->long_name_allocated)
1021 free((char *)dso->long_name);
1024 rb_erase(&dso->rb_node, root);
1026 * __dso__findlink_by_longname() isn't guaranteed to add it
1027 * back, so a clean removal is required here.
1029 RB_CLEAR_NODE(&dso->rb_node);
1033 dso->long_name = name;
1034 dso->long_name_len = strlen(name);
1035 dso->long_name_allocated = name_allocated;
1038 __dso__findlink_by_longname(root, dso, NULL);
1041 void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
1046 if (dso->short_name_allocated)
1047 free((char *)dso->short_name);
1049 dso->short_name = name;
1050 dso->short_name_len = strlen(name);
1051 dso->short_name_allocated = name_allocated;
1054 static void dso__set_basename(struct dso *dso)
1057 * basename() may modify path buffer, so we must pass
1060 char *base, *lname = strdup(dso->long_name);
1066 * basename() may return a pointer to internal
1067 * storage which is reused in subsequent calls
1068 * so copy the result.
1070 base = strdup(basename(lname));
1077 dso__set_short_name(dso, base, true);
1080 int dso__name_len(const struct dso *dso)
1083 return strlen("[unknown]");
1085 return dso->long_name_len;
1087 return dso->short_name_len;
1090 bool dso__loaded(const struct dso *dso, enum map_type type)
1092 return dso->loaded & (1 << type);
1095 bool dso__sorted_by_name(const struct dso *dso, enum map_type type)
1097 return dso->sorted_by_name & (1 << type);
1100 void dso__set_sorted_by_name(struct dso *dso, enum map_type type)
1102 dso->sorted_by_name |= (1 << type);
1105 struct dso *dso__new(const char *name)
1107 struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
1111 strcpy(dso->name, name);
1112 dso__set_long_name(dso, dso->name, false);
1113 dso__set_short_name(dso, dso->name, false);
1114 for (i = 0; i < MAP__NR_TYPES; ++i)
1115 dso->symbols[i] = dso->symbol_names[i] = RB_ROOT;
1116 dso->data.cache = RB_ROOT;
1118 dso->data.status = DSO_DATA_STATUS_UNKNOWN;
1119 dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
1120 dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
1121 dso->is_64_bit = (sizeof(void *) == 8);
1124 dso->sorted_by_name = 0;
1125 dso->has_build_id = 0;
1126 dso->has_srcline = 1;
1128 dso->kernel = DSO_TYPE_USER;
1129 dso->needs_swap = DSO_SWAP__UNSET;
1130 RB_CLEAR_NODE(&dso->rb_node);
1132 INIT_LIST_HEAD(&dso->node);
1133 INIT_LIST_HEAD(&dso->data.open_entry);
1134 pthread_mutex_init(&dso->lock, NULL);
1135 refcount_set(&dso->refcnt, 1);
1141 void dso__delete(struct dso *dso)
1145 if (!RB_EMPTY_NODE(&dso->rb_node))
1146 pr_err("DSO %s is still in rbtree when being deleted!\n",
1148 for (i = 0; i < MAP__NR_TYPES; ++i)
1149 symbols__delete(&dso->symbols[i]);
1151 if (dso->short_name_allocated) {
1152 zfree((char **)&dso->short_name);
1153 dso->short_name_allocated = false;
1156 if (dso->long_name_allocated) {
1157 zfree((char **)&dso->long_name);
1158 dso->long_name_allocated = false;
1161 dso__data_close(dso);
1162 auxtrace_cache__free(dso->auxtrace_cache);
1163 dso_cache__free(dso);
1165 zfree(&dso->symsrc_filename);
1166 pthread_mutex_destroy(&dso->lock);
1170 struct dso *dso__get(struct dso *dso)
1173 refcount_inc(&dso->refcnt);
1177 void dso__put(struct dso *dso)
1179 if (dso && refcount_dec_and_test(&dso->refcnt))
1183 void dso__set_build_id(struct dso *dso, void *build_id)
1185 memcpy(dso->build_id, build_id, sizeof(dso->build_id));
1186 dso->has_build_id = 1;
1189 bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
1191 return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
1194 void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
1196 char path[PATH_MAX];
1198 if (machine__is_default_guest(machine))
1200 sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
1201 if (sysfs__read_build_id(path, dso->build_id,
1202 sizeof(dso->build_id)) == 0)
1203 dso->has_build_id = true;
1206 int dso__kernel_module_get_build_id(struct dso *dso,
1207 const char *root_dir)
1209 char filename[PATH_MAX];
1211 * kernel module short names are of the form "[module]" and
1212 * we need just "module" here.
1214 const char *name = dso->short_name + 1;
1216 snprintf(filename, sizeof(filename),
1217 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1218 root_dir, (int)strlen(name) - 1, name);
1220 if (sysfs__read_build_id(filename, dso->build_id,
1221 sizeof(dso->build_id)) == 0)
1222 dso->has_build_id = true;
1227 bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
1229 bool have_build_id = false;
1232 list_for_each_entry(pos, head, node) {
1233 if (with_hits && !pos->hit && !dso__is_vdso(pos))
1235 if (pos->has_build_id) {
1236 have_build_id = true;
1239 if (filename__read_build_id(pos->long_name, pos->build_id,
1240 sizeof(pos->build_id)) > 0) {
1241 have_build_id = true;
1242 pos->has_build_id = true;
1246 return have_build_id;
1249 void __dsos__add(struct dsos *dsos, struct dso *dso)
1251 list_add_tail(&dso->node, &dsos->head);
1252 __dso__findlink_by_longname(&dsos->root, dso, NULL);
1254 * It is now in the linked list, grab a reference, then garbage collect
1255 * this when needing memory, by looking at LRU dso instances in the
1256 * list with atomic_read(&dso->refcnt) == 1, i.e. no references
1257 * anywhere besides the one for the list, do, under a lock for the
1258 * list: remove it from the list, then a dso__put(), that probably will
1259 * be the last and will then call dso__delete(), end of life.
1261 * That, or at the end of the 'struct machine' lifetime, when all
1262 * 'struct dso' instances will be removed from the list, in
1263 * dsos__exit(), if they have no other reference from some other data
1266 * E.g.: after processing a 'perf.data' file and storing references
1267 * to objects instantiated while processing events, we will have
1268 * references to the 'thread', 'map', 'dso' structs all from 'struct
1269 * hist_entry' instances, but we may not need anything not referenced,
1270 * so we might as well call machines__exit()/machines__delete() and
1271 * garbage collect it.
1276 void dsos__add(struct dsos *dsos, struct dso *dso)
1278 pthread_rwlock_wrlock(&dsos->lock);
1279 __dsos__add(dsos, dso);
1280 pthread_rwlock_unlock(&dsos->lock);
1283 struct dso *__dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
1288 list_for_each_entry(pos, &dsos->head, node)
1289 if (strcmp(pos->short_name, name) == 0)
1293 return __dso__find_by_longname(&dsos->root, name);
1296 struct dso *dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
1299 pthread_rwlock_rdlock(&dsos->lock);
1300 dso = __dsos__find(dsos, name, cmp_short);
1301 pthread_rwlock_unlock(&dsos->lock);
1305 struct dso *__dsos__addnew(struct dsos *dsos, const char *name)
1307 struct dso *dso = dso__new(name);
1310 __dsos__add(dsos, dso);
1311 dso__set_basename(dso);
1312 /* Put dso here because __dsos_add already got it */
1318 struct dso *__dsos__findnew(struct dsos *dsos, const char *name)
1320 struct dso *dso = __dsos__find(dsos, name, false);
1322 return dso ? dso : __dsos__addnew(dsos, name);
1325 struct dso *dsos__findnew(struct dsos *dsos, const char *name)
1328 pthread_rwlock_wrlock(&dsos->lock);
1329 dso = dso__get(__dsos__findnew(dsos, name));
1330 pthread_rwlock_unlock(&dsos->lock);
1334 size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
1335 bool (skip)(struct dso *dso, int parm), int parm)
1340 list_for_each_entry(pos, head, node) {
1341 if (skip && skip(pos, parm))
1343 ret += dso__fprintf_buildid(pos, fp);
1344 ret += fprintf(fp, " %s\n", pos->long_name);
1349 size_t __dsos__fprintf(struct list_head *head, FILE *fp)
1354 list_for_each_entry(pos, head, node) {
1356 for (i = 0; i < MAP__NR_TYPES; ++i)
1357 ret += dso__fprintf(pos, i, fp);
1363 size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
1365 char sbuild_id[SBUILD_ID_SIZE];
1367 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1368 return fprintf(fp, "%s", sbuild_id);
1371 size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp)
1374 size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
1376 if (dso->short_name != dso->long_name)
1377 ret += fprintf(fp, "%s, ", dso->long_name);
1378 ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
1379 dso__loaded(dso, type) ? "" : "NOT ");
1380 ret += dso__fprintf_buildid(dso, fp);
1381 ret += fprintf(fp, ")\n");
1382 for (nd = rb_first(&dso->symbols[type]); nd; nd = rb_next(nd)) {
1383 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
1384 ret += symbol__fprintf(pos, fp);
1390 enum dso_type dso__type(struct dso *dso, struct machine *machine)
1393 enum dso_type type = DSO__TYPE_UNKNOWN;
1395 fd = dso__data_get_fd(dso, machine);
1397 type = dso__type_fd(fd);
1398 dso__data_put_fd(dso);
1404 int dso__strerror_load(struct dso *dso, char *buf, size_t buflen)
1406 int idx, errnum = dso->load_errno;
1408 * This must have a same ordering as the enum dso_load_errno.
1410 static const char *dso_load__error_str[] = {
1411 "Internal tools/perf/ library error",
1413 "Can not read build id",
1414 "Mismatching build id",
1415 "Decompression failure",
1418 BUG_ON(buflen == 0);
1421 const char *err = str_error_r(errnum, buf, buflen);
1424 scnprintf(buf, buflen, "%s", err);
1429 if (errnum < __DSO_LOAD_ERRNO__START || errnum >= __DSO_LOAD_ERRNO__END)
1432 idx = errnum - __DSO_LOAD_ERRNO__START;
1433 scnprintf(buf, buflen, "%s", dso_load__error_str[idx]);