2 #include <linux/kernel.h>
4 #include <sys/resource.h>
13 static const char * const debuglink_paths[] = {
20 char dso__symtab_origin(const struct dso *dso)
22 static const char origin[] = {
23 [DSO_BINARY_TYPE__KALLSYMS] = 'k',
24 [DSO_BINARY_TYPE__VMLINUX] = 'v',
25 [DSO_BINARY_TYPE__JAVA_JIT] = 'j',
26 [DSO_BINARY_TYPE__DEBUGLINK] = 'l',
27 [DSO_BINARY_TYPE__BUILD_ID_CACHE] = 'B',
28 [DSO_BINARY_TYPE__FEDORA_DEBUGINFO] = 'f',
29 [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO] = 'u',
30 [DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO] = 'o',
31 [DSO_BINARY_TYPE__BUILDID_DEBUGINFO] = 'b',
32 [DSO_BINARY_TYPE__SYSTEM_PATH_DSO] = 'd',
33 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE] = 'K',
34 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP] = 'm',
35 [DSO_BINARY_TYPE__GUEST_KALLSYMS] = 'g',
36 [DSO_BINARY_TYPE__GUEST_KMODULE] = 'G',
37 [DSO_BINARY_TYPE__GUEST_KMODULE_COMP] = 'M',
38 [DSO_BINARY_TYPE__GUEST_VMLINUX] = 'V',
41 if (dso == NULL || dso->symtab_type == DSO_BINARY_TYPE__NOT_FOUND)
43 return origin[dso->symtab_type];
46 int dso__read_binary_type_filename(const struct dso *dso,
47 enum dso_binary_type type,
48 char *root_dir, char *filename, size_t size)
50 char build_id_hex[SBUILD_ID_SIZE];
55 case DSO_BINARY_TYPE__DEBUGLINK:
57 const char *last_slash;
58 char dso_dir[PATH_MAX];
59 char symfile[PATH_MAX];
62 len = __symbol__join_symfs(filename, size, dso->long_name);
63 last_slash = filename + len;
64 while (last_slash != filename && *last_slash != '/')
67 strncpy(dso_dir, filename, last_slash - filename);
68 dso_dir[last_slash-filename] = '\0';
70 if (!is_regular_file(filename)) {
75 ret = filename__read_debuglink(filename, symfile, PATH_MAX);
79 /* Check predefined locations where debug file might reside */
81 for (i = 0; i < ARRAY_SIZE(debuglink_paths); i++) {
82 snprintf(filename, size,
83 debuglink_paths[i], dso_dir, symfile);
84 if (is_regular_file(filename)) {
92 case DSO_BINARY_TYPE__BUILD_ID_CACHE:
93 if (dso__build_id_filename(dso, filename, size) == NULL)
97 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
98 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
99 snprintf(filename + len, size - len, "%s.debug", dso->long_name);
102 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
103 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
104 snprintf(filename + len, size - len, "%s", dso->long_name);
107 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
109 const char *last_slash;
112 last_slash = dso->long_name + dso->long_name_len;
113 while (last_slash != dso->long_name && *last_slash != '/')
116 len = __symbol__join_symfs(filename, size, "");
117 dir_size = last_slash - dso->long_name + 2;
118 if (dir_size > (size - len)) {
122 len += scnprintf(filename + len, dir_size, "%s", dso->long_name);
123 len += scnprintf(filename + len , size - len, ".debug%s",
128 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
129 if (!dso->has_build_id) {
134 build_id__sprintf(dso->build_id,
135 sizeof(dso->build_id),
137 len = __symbol__join_symfs(filename, size, "/usr/lib/debug/.build-id/");
138 snprintf(filename + len, size - len, "%.2s/%s.debug",
139 build_id_hex, build_id_hex + 2);
142 case DSO_BINARY_TYPE__VMLINUX:
143 case DSO_BINARY_TYPE__GUEST_VMLINUX:
144 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
145 __symbol__join_symfs(filename, size, dso->long_name);
148 case DSO_BINARY_TYPE__GUEST_KMODULE:
149 case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
150 path__join3(filename, size, symbol_conf.symfs,
151 root_dir, dso->long_name);
154 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
155 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
156 __symbol__join_symfs(filename, size, dso->long_name);
159 case DSO_BINARY_TYPE__KCORE:
160 case DSO_BINARY_TYPE__GUEST_KCORE:
161 snprintf(filename, size, "%s", dso->long_name);
165 case DSO_BINARY_TYPE__KALLSYMS:
166 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
167 case DSO_BINARY_TYPE__JAVA_JIT:
168 case DSO_BINARY_TYPE__NOT_FOUND:
176 static const struct {
178 int (*decompress)(const char *input, int output);
180 #ifdef HAVE_ZLIB_SUPPORT
181 { "gz", gzip_decompress_to_file },
183 #ifdef HAVE_LZMA_SUPPORT
184 { "xz", lzma_decompress_to_file },
189 bool is_supported_compression(const char *ext)
193 for (i = 0; compressions[i].fmt; i++) {
194 if (!strcmp(ext, compressions[i].fmt))
200 bool is_kernel_module(const char *pathname, int cpumode)
203 int mode = cpumode & PERF_RECORD_MISC_CPUMODE_MASK;
205 WARN_ONCE(mode != cpumode,
206 "Internal error: passing unmasked cpumode (%x) to is_kernel_module",
210 case PERF_RECORD_MISC_USER:
211 case PERF_RECORD_MISC_HYPERVISOR:
212 case PERF_RECORD_MISC_GUEST_USER:
214 /* Treat PERF_RECORD_MISC_CPUMODE_UNKNOWN as kernel */
216 if (kmod_path__parse(&m, pathname)) {
217 pr_err("Failed to check whether %s is a kernel module or not. Assume it is.",
226 bool decompress_to_file(const char *ext, const char *filename, int output_fd)
230 for (i = 0; compressions[i].fmt; i++) {
231 if (!strcmp(ext, compressions[i].fmt))
232 return !compressions[i].decompress(filename,
238 bool dso__needs_decompress(struct dso *dso)
240 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
241 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
245 * Parses kernel module specified in @path and updates
248 * @comp - true if @path contains supported compression suffix,
250 * @kmod - true if @path contains '.ko' suffix in right position,
252 * @name - if (@alloc_name && @kmod) is true, it contains strdup-ed base name
253 * of the kernel module without suffixes, otherwise strudup-ed
255 * @ext - if (@alloc_ext && @comp) is true, it contains strdup-ed string
256 * the compression suffix
258 * Returns 0 if there's no strdup error, -ENOMEM otherwise.
260 int __kmod_path__parse(struct kmod_path *m, const char *path,
261 bool alloc_name, bool alloc_ext)
263 const char *name = strrchr(path, '/');
264 const char *ext = strrchr(path, '.');
265 bool is_simple_name = false;
267 memset(m, 0x0, sizeof(*m));
268 name = name ? name + 1 : path;
271 * '.' is also a valid character for module name. For example:
272 * [aaa.bbb] is a valid module name. '[' should have higher
273 * priority than '.ko' suffix.
275 * The kernel names are from machine__mmap_name. Such
276 * name should belong to kernel itself, not kernel module.
278 if (name[0] == '[') {
279 is_simple_name = true;
280 if ((strncmp(name, "[kernel.kallsyms]", 17) == 0) ||
281 (strncmp(name, "[guest.kernel.kallsyms", 22) == 0) ||
282 (strncmp(name, "[vdso]", 6) == 0) ||
283 (strncmp(name, "[vsyscall]", 10) == 0)) {
290 /* No extension, just return name. */
291 if ((ext == NULL) || is_simple_name) {
293 m->name = strdup(name);
294 return m->name ? 0 : -ENOMEM;
299 if (is_supported_compression(ext + 1)) {
304 /* Check .ko extension only if there's enough name left. */
306 m->kmod = !strncmp(ext, ".ko", 3);
310 if (asprintf(&m->name, "[%.*s]", (int) (ext - name), name) == -1)
313 if (asprintf(&m->name, "%s", name) == -1)
317 strxfrchar(m->name, '-', '_');
320 if (alloc_ext && m->comp) {
321 m->ext = strdup(ext + 4);
323 free((void *) m->name);
332 * Global list of open DSOs and the counter.
334 static LIST_HEAD(dso__data_open);
335 static long dso__data_open_cnt;
336 static pthread_mutex_t dso__data_open_lock = PTHREAD_MUTEX_INITIALIZER;
338 static void dso__list_add(struct dso *dso)
340 list_add_tail(&dso->data.open_entry, &dso__data_open);
341 dso__data_open_cnt++;
344 static void dso__list_del(struct dso *dso)
346 list_del(&dso->data.open_entry);
347 WARN_ONCE(dso__data_open_cnt <= 0,
348 "DSO data fd counter out of bounds.");
349 dso__data_open_cnt--;
352 static void close_first_dso(void);
354 static int do_open(char *name)
357 char sbuf[STRERR_BUFSIZE];
360 fd = open(name, O_RDONLY);
364 pr_debug("dso open failed: %s\n",
365 str_error_r(errno, sbuf, sizeof(sbuf)));
366 if (!dso__data_open_cnt || errno != EMFILE)
375 static int __open_dso(struct dso *dso, struct machine *machine)
378 char *root_dir = (char *)"";
379 char *name = malloc(PATH_MAX);
385 root_dir = machine->root_dir;
387 if (dso__read_binary_type_filename(dso, dso->binary_type,
388 root_dir, name, PATH_MAX)) {
393 if (!is_regular_file(name))
401 static void check_data_close(void);
404 * dso_close - Open DSO data file
407 * Open @dso's data file descriptor and updates
408 * list/count of open DSO objects.
410 static int open_dso(struct dso *dso, struct machine *machine)
412 int fd = __open_dso(dso, machine);
417 * Check if we crossed the allowed number
418 * of opened DSOs and close one if needed.
426 static void close_data_fd(struct dso *dso)
428 if (dso->data.fd >= 0) {
431 dso->data.file_size = 0;
437 * dso_close - Close DSO data file
440 * Close @dso's data file descriptor and updates
441 * list/count of open DSO objects.
443 static void close_dso(struct dso *dso)
448 static void close_first_dso(void)
452 dso = list_first_entry(&dso__data_open, struct dso, data.open_entry);
456 static rlim_t get_fd_limit(void)
461 /* Allow half of the current open fd limit. */
462 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
463 if (l.rlim_cur == RLIM_INFINITY)
466 limit = l.rlim_cur / 2;
468 pr_err("failed to get fd limit\n");
475 static rlim_t fd_limit;
478 * Used only by tests/dso-data.c to reset the environment
479 * for tests. I dont expect we should change this during
482 void reset_fd_limit(void)
487 static bool may_cache_fd(void)
490 fd_limit = get_fd_limit();
492 if (fd_limit == RLIM_INFINITY)
495 return fd_limit > (rlim_t) dso__data_open_cnt;
499 * Check and close LRU dso if we crossed allowed limit
500 * for opened dso file descriptors. The limit is half
501 * of the RLIMIT_NOFILE files opened.
503 static void check_data_close(void)
505 bool cache_fd = may_cache_fd();
512 * dso__data_close - Close DSO data file
515 * External interface to close @dso's data file descriptor.
517 void dso__data_close(struct dso *dso)
519 pthread_mutex_lock(&dso__data_open_lock);
521 pthread_mutex_unlock(&dso__data_open_lock);
524 static void try_to_open_dso(struct dso *dso, struct machine *machine)
526 enum dso_binary_type binary_type_data[] = {
527 DSO_BINARY_TYPE__BUILD_ID_CACHE,
528 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
529 DSO_BINARY_TYPE__NOT_FOUND,
533 if (dso->data.fd >= 0)
536 if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) {
537 dso->data.fd = open_dso(dso, machine);
542 dso->binary_type = binary_type_data[i++];
544 dso->data.fd = open_dso(dso, machine);
545 if (dso->data.fd >= 0)
548 } while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND);
550 if (dso->data.fd >= 0)
551 dso->data.status = DSO_DATA_STATUS_OK;
553 dso->data.status = DSO_DATA_STATUS_ERROR;
557 * dso__data_get_fd - Get dso's data file descriptor
559 * @machine: machine object
561 * External interface to find dso's file, open it and
562 * returns file descriptor. It should be paired with
563 * dso__data_put_fd() if it returns non-negative value.
565 int dso__data_get_fd(struct dso *dso, struct machine *machine)
567 if (dso->data.status == DSO_DATA_STATUS_ERROR)
570 if (pthread_mutex_lock(&dso__data_open_lock) < 0)
573 try_to_open_dso(dso, machine);
575 if (dso->data.fd < 0)
576 pthread_mutex_unlock(&dso__data_open_lock);
581 void dso__data_put_fd(struct dso *dso __maybe_unused)
583 pthread_mutex_unlock(&dso__data_open_lock);
586 bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by)
590 if (dso->data.status_seen & flag)
593 dso->data.status_seen |= flag;
599 dso_cache__free(struct dso *dso)
601 struct rb_root *root = &dso->data.cache;
602 struct rb_node *next = rb_first(root);
604 pthread_mutex_lock(&dso->lock);
606 struct dso_cache *cache;
608 cache = rb_entry(next, struct dso_cache, rb_node);
609 next = rb_next(&cache->rb_node);
610 rb_erase(&cache->rb_node, root);
613 pthread_mutex_unlock(&dso->lock);
616 static struct dso_cache *dso_cache__find(struct dso *dso, u64 offset)
618 const struct rb_root *root = &dso->data.cache;
619 struct rb_node * const *p = &root->rb_node;
620 const struct rb_node *parent = NULL;
621 struct dso_cache *cache;
627 cache = rb_entry(parent, struct dso_cache, rb_node);
628 end = cache->offset + DSO__DATA_CACHE_SIZE;
630 if (offset < cache->offset)
632 else if (offset >= end)
641 static struct dso_cache *
642 dso_cache__insert(struct dso *dso, struct dso_cache *new)
644 struct rb_root *root = &dso->data.cache;
645 struct rb_node **p = &root->rb_node;
646 struct rb_node *parent = NULL;
647 struct dso_cache *cache;
648 u64 offset = new->offset;
650 pthread_mutex_lock(&dso->lock);
655 cache = rb_entry(parent, struct dso_cache, rb_node);
656 end = cache->offset + DSO__DATA_CACHE_SIZE;
658 if (offset < cache->offset)
660 else if (offset >= end)
666 rb_link_node(&new->rb_node, parent, p);
667 rb_insert_color(&new->rb_node, root);
671 pthread_mutex_unlock(&dso->lock);
676 dso_cache__memcpy(struct dso_cache *cache, u64 offset,
679 u64 cache_offset = offset - cache->offset;
680 u64 cache_size = min(cache->size - cache_offset, size);
682 memcpy(data, cache->data + cache_offset, cache_size);
687 dso_cache__read(struct dso *dso, struct machine *machine,
688 u64 offset, u8 *data, ssize_t size)
690 struct dso_cache *cache;
691 struct dso_cache *old;
697 cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
701 pthread_mutex_lock(&dso__data_open_lock);
704 * dso->data.fd might be closed if other thread opened another
705 * file (dso) due to open file limit (RLIMIT_NOFILE).
707 try_to_open_dso(dso, machine);
709 if (dso->data.fd < 0) {
711 dso->data.status = DSO_DATA_STATUS_ERROR;
715 cache_offset = offset & DSO__DATA_CACHE_MASK;
717 ret = pread(dso->data.fd, cache->data, DSO__DATA_CACHE_SIZE, cache_offset);
721 cache->offset = cache_offset;
725 pthread_mutex_unlock(&dso__data_open_lock);
728 old = dso_cache__insert(dso, cache);
730 /* we lose the race */
735 ret = dso_cache__memcpy(cache, offset, data, size);
744 static ssize_t dso_cache_read(struct dso *dso, struct machine *machine,
745 u64 offset, u8 *data, ssize_t size)
747 struct dso_cache *cache;
749 cache = dso_cache__find(dso, offset);
751 return dso_cache__memcpy(cache, offset, data, size);
753 return dso_cache__read(dso, machine, offset, data, size);
757 * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
758 * in the rb_tree. Any read to already cached data is served
761 static ssize_t cached_read(struct dso *dso, struct machine *machine,
762 u64 offset, u8 *data, ssize_t size)
770 ret = dso_cache_read(dso, machine, offset, p, size);
774 /* Reached EOF, return what we have. */
790 static int data_file_size(struct dso *dso, struct machine *machine)
794 char sbuf[STRERR_BUFSIZE];
796 if (dso->data.file_size)
799 if (dso->data.status == DSO_DATA_STATUS_ERROR)
802 pthread_mutex_lock(&dso__data_open_lock);
805 * dso->data.fd might be closed if other thread opened another
806 * file (dso) due to open file limit (RLIMIT_NOFILE).
808 try_to_open_dso(dso, machine);
810 if (dso->data.fd < 0) {
812 dso->data.status = DSO_DATA_STATUS_ERROR;
816 if (fstat(dso->data.fd, &st) < 0) {
818 pr_err("dso cache fstat failed: %s\n",
819 str_error_r(errno, sbuf, sizeof(sbuf)));
820 dso->data.status = DSO_DATA_STATUS_ERROR;
823 dso->data.file_size = st.st_size;
826 pthread_mutex_unlock(&dso__data_open_lock);
831 * dso__data_size - Return dso data size
833 * @machine: machine object
835 * Return: dso data size
837 off_t dso__data_size(struct dso *dso, struct machine *machine)
839 if (data_file_size(dso, machine))
842 /* For now just estimate dso data size is close to file size */
843 return dso->data.file_size;
846 static ssize_t data_read_offset(struct dso *dso, struct machine *machine,
847 u64 offset, u8 *data, ssize_t size)
849 if (data_file_size(dso, machine))
852 /* Check the offset sanity. */
853 if (offset > dso->data.file_size)
856 if (offset + size < offset)
859 return cached_read(dso, machine, offset, data, size);
863 * dso__data_read_offset - Read data from dso file offset
865 * @machine: machine object
866 * @offset: file offset
867 * @data: buffer to store data
868 * @size: size of the @data buffer
870 * External interface to read data from dso file offset. Open
871 * dso data file and use cached_read to get the data.
873 ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
874 u64 offset, u8 *data, ssize_t size)
876 if (dso->data.status == DSO_DATA_STATUS_ERROR)
879 return data_read_offset(dso, machine, offset, data, size);
883 * dso__data_read_addr - Read data from dso address
885 * @machine: machine object
886 * @add: virtual memory address
887 * @data: buffer to store data
888 * @size: size of the @data buffer
890 * External interface to read data from dso address.
892 ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
893 struct machine *machine, u64 addr,
894 u8 *data, ssize_t size)
896 u64 offset = map->map_ip(map, addr);
897 return dso__data_read_offset(dso, machine, offset, data, size);
900 struct map *dso__new_map(const char *name)
902 struct map *map = NULL;
903 struct dso *dso = dso__new(name);
906 map = map__new2(0, dso, MAP__FUNCTION);
911 struct dso *machine__findnew_kernel(struct machine *machine, const char *name,
912 const char *short_name, int dso_type)
915 * The kernel dso could be created by build_id processing.
917 struct dso *dso = machine__findnew_dso(machine, name);
920 * We need to run this in all cases, since during the build_id
921 * processing we had no idea this was the kernel dso.
924 dso__set_short_name(dso, short_name, false);
925 dso->kernel = dso_type;
932 * Find a matching entry and/or link current entry to RB tree.
933 * Either one of the dso or name parameter must be non-NULL or the
934 * function will not work.
936 static struct dso *__dso__findlink_by_longname(struct rb_root *root,
937 struct dso *dso, const char *name)
939 struct rb_node **p = &root->rb_node;
940 struct rb_node *parent = NULL;
943 name = dso->long_name;
945 * Find node with the matching name
948 struct dso *this = rb_entry(*p, struct dso, rb_node);
949 int rc = strcmp(name, this->long_name);
954 * In case the new DSO is a duplicate of an existing
955 * one, print a one-time warning & put the new entry
956 * at the end of the list of duplicates.
958 if (!dso || (dso == this))
959 return this; /* Find matching dso */
961 * The core kernel DSOs may have duplicated long name.
962 * In this case, the short name should be different.
963 * Comparing the short names to differentiate the DSOs.
965 rc = strcmp(dso->short_name, this->short_name);
967 pr_err("Duplicated dso name: %s\n", name);
972 p = &parent->rb_left;
974 p = &parent->rb_right;
977 /* Add new node and rebalance tree */
978 rb_link_node(&dso->rb_node, parent, p);
979 rb_insert_color(&dso->rb_node, root);
985 static inline struct dso *__dso__find_by_longname(struct rb_root *root,
988 return __dso__findlink_by_longname(root, NULL, name);
991 void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
993 struct rb_root *root = dso->root;
998 if (dso->long_name_allocated)
999 free((char *)dso->long_name);
1002 rb_erase(&dso->rb_node, root);
1004 * __dso__findlink_by_longname() isn't guaranteed to add it
1005 * back, so a clean removal is required here.
1007 RB_CLEAR_NODE(&dso->rb_node);
1011 dso->long_name = name;
1012 dso->long_name_len = strlen(name);
1013 dso->long_name_allocated = name_allocated;
1016 __dso__findlink_by_longname(root, dso, NULL);
1019 void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
1024 if (dso->short_name_allocated)
1025 free((char *)dso->short_name);
1027 dso->short_name = name;
1028 dso->short_name_len = strlen(name);
1029 dso->short_name_allocated = name_allocated;
1032 static void dso__set_basename(struct dso *dso)
1035 * basename() may modify path buffer, so we must pass
1038 char *base, *lname = strdup(dso->long_name);
1044 * basename() may return a pointer to internal
1045 * storage which is reused in subsequent calls
1046 * so copy the result.
1048 base = strdup(basename(lname));
1055 dso__set_short_name(dso, base, true);
1058 int dso__name_len(const struct dso *dso)
1061 return strlen("[unknown]");
1063 return dso->long_name_len;
1065 return dso->short_name_len;
1068 bool dso__loaded(const struct dso *dso, enum map_type type)
1070 return dso->loaded & (1 << type);
1073 bool dso__sorted_by_name(const struct dso *dso, enum map_type type)
1075 return dso->sorted_by_name & (1 << type);
1078 void dso__set_sorted_by_name(struct dso *dso, enum map_type type)
1080 dso->sorted_by_name |= (1 << type);
1083 struct dso *dso__new(const char *name)
1085 struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
1089 strcpy(dso->name, name);
1090 dso__set_long_name(dso, dso->name, false);
1091 dso__set_short_name(dso, dso->name, false);
1092 for (i = 0; i < MAP__NR_TYPES; ++i)
1093 dso->symbols[i] = dso->symbol_names[i] = RB_ROOT;
1094 dso->data.cache = RB_ROOT;
1096 dso->data.status = DSO_DATA_STATUS_UNKNOWN;
1097 dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
1098 dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
1099 dso->is_64_bit = (sizeof(void *) == 8);
1102 dso->sorted_by_name = 0;
1103 dso->has_build_id = 0;
1104 dso->has_srcline = 1;
1106 dso->kernel = DSO_TYPE_USER;
1107 dso->needs_swap = DSO_SWAP__UNSET;
1108 RB_CLEAR_NODE(&dso->rb_node);
1110 INIT_LIST_HEAD(&dso->node);
1111 INIT_LIST_HEAD(&dso->data.open_entry);
1112 pthread_mutex_init(&dso->lock, NULL);
1113 refcount_set(&dso->refcnt, 1);
1119 void dso__delete(struct dso *dso)
1123 if (!RB_EMPTY_NODE(&dso->rb_node))
1124 pr_err("DSO %s is still in rbtree when being deleted!\n",
1126 for (i = 0; i < MAP__NR_TYPES; ++i)
1127 symbols__delete(&dso->symbols[i]);
1129 if (dso->short_name_allocated) {
1130 zfree((char **)&dso->short_name);
1131 dso->short_name_allocated = false;
1134 if (dso->long_name_allocated) {
1135 zfree((char **)&dso->long_name);
1136 dso->long_name_allocated = false;
1139 dso__data_close(dso);
1140 auxtrace_cache__free(dso->auxtrace_cache);
1141 dso_cache__free(dso);
1143 zfree(&dso->symsrc_filename);
1144 pthread_mutex_destroy(&dso->lock);
1148 struct dso *dso__get(struct dso *dso)
1151 refcount_inc(&dso->refcnt);
1155 void dso__put(struct dso *dso)
1157 if (dso && refcount_dec_and_test(&dso->refcnt))
1161 void dso__set_build_id(struct dso *dso, void *build_id)
1163 memcpy(dso->build_id, build_id, sizeof(dso->build_id));
1164 dso->has_build_id = 1;
1167 bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
1169 return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
1172 void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
1174 char path[PATH_MAX];
1176 if (machine__is_default_guest(machine))
1178 sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
1179 if (sysfs__read_build_id(path, dso->build_id,
1180 sizeof(dso->build_id)) == 0)
1181 dso->has_build_id = true;
1184 int dso__kernel_module_get_build_id(struct dso *dso,
1185 const char *root_dir)
1187 char filename[PATH_MAX];
1189 * kernel module short names are of the form "[module]" and
1190 * we need just "module" here.
1192 const char *name = dso->short_name + 1;
1194 snprintf(filename, sizeof(filename),
1195 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1196 root_dir, (int)strlen(name) - 1, name);
1198 if (sysfs__read_build_id(filename, dso->build_id,
1199 sizeof(dso->build_id)) == 0)
1200 dso->has_build_id = true;
1205 bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
1207 bool have_build_id = false;
1210 list_for_each_entry(pos, head, node) {
1211 if (with_hits && !pos->hit && !dso__is_vdso(pos))
1213 if (pos->has_build_id) {
1214 have_build_id = true;
1217 if (filename__read_build_id(pos->long_name, pos->build_id,
1218 sizeof(pos->build_id)) > 0) {
1219 have_build_id = true;
1220 pos->has_build_id = true;
1224 return have_build_id;
1227 void __dsos__add(struct dsos *dsos, struct dso *dso)
1229 list_add_tail(&dso->node, &dsos->head);
1230 __dso__findlink_by_longname(&dsos->root, dso, NULL);
1232 * It is now in the linked list, grab a reference, then garbage collect
1233 * this when needing memory, by looking at LRU dso instances in the
1234 * list with atomic_read(&dso->refcnt) == 1, i.e. no references
1235 * anywhere besides the one for the list, do, under a lock for the
1236 * list: remove it from the list, then a dso__put(), that probably will
1237 * be the last and will then call dso__delete(), end of life.
1239 * That, or at the end of the 'struct machine' lifetime, when all
1240 * 'struct dso' instances will be removed from the list, in
1241 * dsos__exit(), if they have no other reference from some other data
1244 * E.g.: after processing a 'perf.data' file and storing references
1245 * to objects instantiated while processing events, we will have
1246 * references to the 'thread', 'map', 'dso' structs all from 'struct
1247 * hist_entry' instances, but we may not need anything not referenced,
1248 * so we might as well call machines__exit()/machines__delete() and
1249 * garbage collect it.
1254 void dsos__add(struct dsos *dsos, struct dso *dso)
1256 pthread_rwlock_wrlock(&dsos->lock);
1257 __dsos__add(dsos, dso);
1258 pthread_rwlock_unlock(&dsos->lock);
1261 struct dso *__dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
1266 list_for_each_entry(pos, &dsos->head, node)
1267 if (strcmp(pos->short_name, name) == 0)
1271 return __dso__find_by_longname(&dsos->root, name);
1274 struct dso *dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
1277 pthread_rwlock_rdlock(&dsos->lock);
1278 dso = __dsos__find(dsos, name, cmp_short);
1279 pthread_rwlock_unlock(&dsos->lock);
1283 struct dso *__dsos__addnew(struct dsos *dsos, const char *name)
1285 struct dso *dso = dso__new(name);
1288 __dsos__add(dsos, dso);
1289 dso__set_basename(dso);
1290 /* Put dso here because __dsos_add already got it */
1296 struct dso *__dsos__findnew(struct dsos *dsos, const char *name)
1298 struct dso *dso = __dsos__find(dsos, name, false);
1300 return dso ? dso : __dsos__addnew(dsos, name);
1303 struct dso *dsos__findnew(struct dsos *dsos, const char *name)
1306 pthread_rwlock_wrlock(&dsos->lock);
1307 dso = dso__get(__dsos__findnew(dsos, name));
1308 pthread_rwlock_unlock(&dsos->lock);
1312 size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
1313 bool (skip)(struct dso *dso, int parm), int parm)
1318 list_for_each_entry(pos, head, node) {
1319 if (skip && skip(pos, parm))
1321 ret += dso__fprintf_buildid(pos, fp);
1322 ret += fprintf(fp, " %s\n", pos->long_name);
1327 size_t __dsos__fprintf(struct list_head *head, FILE *fp)
1332 list_for_each_entry(pos, head, node) {
1334 for (i = 0; i < MAP__NR_TYPES; ++i)
1335 ret += dso__fprintf(pos, i, fp);
1341 size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
1343 char sbuild_id[SBUILD_ID_SIZE];
1345 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1346 return fprintf(fp, "%s", sbuild_id);
1349 size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp)
1352 size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
1354 if (dso->short_name != dso->long_name)
1355 ret += fprintf(fp, "%s, ", dso->long_name);
1356 ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
1357 dso__loaded(dso, type) ? "" : "NOT ");
1358 ret += dso__fprintf_buildid(dso, fp);
1359 ret += fprintf(fp, ")\n");
1360 for (nd = rb_first(&dso->symbols[type]); nd; nd = rb_next(nd)) {
1361 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
1362 ret += symbol__fprintf(pos, fp);
1368 enum dso_type dso__type(struct dso *dso, struct machine *machine)
1371 enum dso_type type = DSO__TYPE_UNKNOWN;
1373 fd = dso__data_get_fd(dso, machine);
1375 type = dso__type_fd(fd);
1376 dso__data_put_fd(dso);
1382 int dso__strerror_load(struct dso *dso, char *buf, size_t buflen)
1384 int idx, errnum = dso->load_errno;
1386 * This must have a same ordering as the enum dso_load_errno.
1388 static const char *dso_load__error_str[] = {
1389 "Internal tools/perf/ library error",
1391 "Can not read build id",
1392 "Mismatching build id",
1393 "Decompression failure",
1396 BUG_ON(buflen == 0);
1399 const char *err = str_error_r(errnum, buf, buflen);
1402 scnprintf(buf, buflen, "%s", err);
1407 if (errnum < __DSO_LOAD_ERRNO__START || errnum >= __DSO_LOAD_ERRNO__END)
1410 idx = errnum - __DSO_LOAD_ERRNO__START;
1411 scnprintf(buf, buflen, "%s", dso_load__error_str[idx]);