2 #include <linux/kernel.h>
4 #include <sys/resource.h>
22 static const char * const debuglink_paths[] = {
29 char dso__symtab_origin(const struct dso *dso)
31 static const char origin[] = {
32 [DSO_BINARY_TYPE__KALLSYMS] = 'k',
33 [DSO_BINARY_TYPE__VMLINUX] = 'v',
34 [DSO_BINARY_TYPE__JAVA_JIT] = 'j',
35 [DSO_BINARY_TYPE__DEBUGLINK] = 'l',
36 [DSO_BINARY_TYPE__BUILD_ID_CACHE] = 'B',
37 [DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO] = 'D',
38 [DSO_BINARY_TYPE__FEDORA_DEBUGINFO] = 'f',
39 [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO] = 'u',
40 [DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO] = 'o',
41 [DSO_BINARY_TYPE__BUILDID_DEBUGINFO] = 'b',
42 [DSO_BINARY_TYPE__SYSTEM_PATH_DSO] = 'd',
43 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE] = 'K',
44 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP] = 'm',
45 [DSO_BINARY_TYPE__GUEST_KALLSYMS] = 'g',
46 [DSO_BINARY_TYPE__GUEST_KMODULE] = 'G',
47 [DSO_BINARY_TYPE__GUEST_KMODULE_COMP] = 'M',
48 [DSO_BINARY_TYPE__GUEST_VMLINUX] = 'V',
51 if (dso == NULL || dso->symtab_type == DSO_BINARY_TYPE__NOT_FOUND)
53 return origin[dso->symtab_type];
56 int dso__read_binary_type_filename(const struct dso *dso,
57 enum dso_binary_type type,
58 char *root_dir, char *filename, size_t size)
60 char build_id_hex[SBUILD_ID_SIZE];
65 case DSO_BINARY_TYPE__DEBUGLINK:
67 const char *last_slash;
68 char dso_dir[PATH_MAX];
69 char symfile[PATH_MAX];
72 len = __symbol__join_symfs(filename, size, dso->long_name);
73 last_slash = filename + len;
74 while (last_slash != filename && *last_slash != '/')
77 strncpy(dso_dir, filename, last_slash - filename);
78 dso_dir[last_slash-filename] = '\0';
80 if (!is_regular_file(filename)) {
85 ret = filename__read_debuglink(filename, symfile, PATH_MAX);
89 /* Check predefined locations where debug file might reside */
91 for (i = 0; i < ARRAY_SIZE(debuglink_paths); i++) {
92 snprintf(filename, size,
93 debuglink_paths[i], dso_dir, symfile);
94 if (is_regular_file(filename)) {
102 case DSO_BINARY_TYPE__BUILD_ID_CACHE:
103 if (dso__build_id_filename(dso, filename, size, false) == NULL)
107 case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO:
108 if (dso__build_id_filename(dso, filename, size, true) == NULL)
112 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
113 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
114 snprintf(filename + len, size - len, "%s.debug", dso->long_name);
117 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
118 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
119 snprintf(filename + len, size - len, "%s", dso->long_name);
122 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
124 const char *last_slash;
127 last_slash = dso->long_name + dso->long_name_len;
128 while (last_slash != dso->long_name && *last_slash != '/')
131 len = __symbol__join_symfs(filename, size, "");
132 dir_size = last_slash - dso->long_name + 2;
133 if (dir_size > (size - len)) {
137 len += scnprintf(filename + len, dir_size, "%s", dso->long_name);
138 len += scnprintf(filename + len , size - len, ".debug%s",
143 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
144 if (!dso->has_build_id) {
149 build_id__sprintf(dso->build_id,
150 sizeof(dso->build_id),
152 len = __symbol__join_symfs(filename, size, "/usr/lib/debug/.build-id/");
153 snprintf(filename + len, size - len, "%.2s/%s.debug",
154 build_id_hex, build_id_hex + 2);
157 case DSO_BINARY_TYPE__VMLINUX:
158 case DSO_BINARY_TYPE__GUEST_VMLINUX:
159 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
160 __symbol__join_symfs(filename, size, dso->long_name);
163 case DSO_BINARY_TYPE__GUEST_KMODULE:
164 case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
165 path__join3(filename, size, symbol_conf.symfs,
166 root_dir, dso->long_name);
169 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
170 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
171 __symbol__join_symfs(filename, size, dso->long_name);
174 case DSO_BINARY_TYPE__KCORE:
175 case DSO_BINARY_TYPE__GUEST_KCORE:
176 snprintf(filename, size, "%s", dso->long_name);
180 case DSO_BINARY_TYPE__KALLSYMS:
181 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
182 case DSO_BINARY_TYPE__JAVA_JIT:
183 case DSO_BINARY_TYPE__NOT_FOUND:
191 static const struct {
193 int (*decompress)(const char *input, int output);
195 #ifdef HAVE_ZLIB_SUPPORT
196 { "gz", gzip_decompress_to_file },
198 #ifdef HAVE_LZMA_SUPPORT
199 { "xz", lzma_decompress_to_file },
204 bool is_supported_compression(const char *ext)
208 for (i = 0; compressions[i].fmt; i++) {
209 if (!strcmp(ext, compressions[i].fmt))
215 bool is_kernel_module(const char *pathname, int cpumode)
218 int mode = cpumode & PERF_RECORD_MISC_CPUMODE_MASK;
220 WARN_ONCE(mode != cpumode,
221 "Internal error: passing unmasked cpumode (%x) to is_kernel_module",
225 case PERF_RECORD_MISC_USER:
226 case PERF_RECORD_MISC_HYPERVISOR:
227 case PERF_RECORD_MISC_GUEST_USER:
229 /* Treat PERF_RECORD_MISC_CPUMODE_UNKNOWN as kernel */
231 if (kmod_path__parse(&m, pathname)) {
232 pr_err("Failed to check whether %s is a kernel module or not. Assume it is.",
241 bool decompress_to_file(const char *ext, const char *filename, int output_fd)
245 for (i = 0; compressions[i].fmt; i++) {
246 if (!strcmp(ext, compressions[i].fmt))
247 return !compressions[i].decompress(filename,
253 bool dso__needs_decompress(struct dso *dso)
255 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
256 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
259 static int decompress_kmodule(struct dso *dso, const char *name, char *tmpbuf)
264 if (!dso__needs_decompress(dso))
267 if (kmod_path__parse_ext(&m, dso->long_name))
273 fd = mkstemp(tmpbuf);
275 dso->load_errno = errno;
279 if (!decompress_to_file(m.ext, name, fd)) {
280 dso->load_errno = DSO_LOAD_ERRNO__DECOMPRESSION_FAILURE;
290 int dso__decompress_kmodule_fd(struct dso *dso, const char *name)
292 char tmpbuf[] = KMOD_DECOMP_NAME;
295 fd = decompress_kmodule(dso, name, tmpbuf);
300 int dso__decompress_kmodule_path(struct dso *dso, const char *name,
301 char *pathname, size_t len)
303 char tmpbuf[] = KMOD_DECOMP_NAME;
306 fd = decompress_kmodule(dso, name, tmpbuf);
312 strncpy(pathname, tmpbuf, len);
318 * Parses kernel module specified in @path and updates
321 * @comp - true if @path contains supported compression suffix,
323 * @kmod - true if @path contains '.ko' suffix in right position,
325 * @name - if (@alloc_name && @kmod) is true, it contains strdup-ed base name
326 * of the kernel module without suffixes, otherwise strudup-ed
328 * @ext - if (@alloc_ext && @comp) is true, it contains strdup-ed string
329 * the compression suffix
331 * Returns 0 if there's no strdup error, -ENOMEM otherwise.
333 int __kmod_path__parse(struct kmod_path *m, const char *path,
334 bool alloc_name, bool alloc_ext)
336 const char *name = strrchr(path, '/');
337 const char *ext = strrchr(path, '.');
338 bool is_simple_name = false;
340 memset(m, 0x0, sizeof(*m));
341 name = name ? name + 1 : path;
344 * '.' is also a valid character for module name. For example:
345 * [aaa.bbb] is a valid module name. '[' should have higher
346 * priority than '.ko' suffix.
348 * The kernel names are from machine__mmap_name. Such
349 * name should belong to kernel itself, not kernel module.
351 if (name[0] == '[') {
352 is_simple_name = true;
353 if ((strncmp(name, "[kernel.kallsyms]", 17) == 0) ||
354 (strncmp(name, "[guest.kernel.kallsyms", 22) == 0) ||
355 (strncmp(name, "[vdso]", 6) == 0) ||
356 (strncmp(name, "[vsyscall]", 10) == 0)) {
363 /* No extension, just return name. */
364 if ((ext == NULL) || is_simple_name) {
366 m->name = strdup(name);
367 return m->name ? 0 : -ENOMEM;
372 if (is_supported_compression(ext + 1)) {
377 /* Check .ko extension only if there's enough name left. */
379 m->kmod = !strncmp(ext, ".ko", 3);
383 if (asprintf(&m->name, "[%.*s]", (int) (ext - name), name) == -1)
386 if (asprintf(&m->name, "%s", name) == -1)
390 strxfrchar(m->name, '-', '_');
393 if (alloc_ext && m->comp) {
394 m->ext = strdup(ext + 4);
396 free((void *) m->name);
404 void dso__set_module_info(struct dso *dso, struct kmod_path *m,
405 struct machine *machine)
407 if (machine__is_host(machine))
408 dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE;
410 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE;
412 /* _KMODULE_COMP should be next to _KMODULE */
413 if (m->kmod && m->comp)
416 dso__set_short_name(dso, strdup(m->name), true);
420 * Global list of open DSOs and the counter.
422 static LIST_HEAD(dso__data_open);
423 static long dso__data_open_cnt;
424 static pthread_mutex_t dso__data_open_lock = PTHREAD_MUTEX_INITIALIZER;
426 static void dso__list_add(struct dso *dso)
428 list_add_tail(&dso->data.open_entry, &dso__data_open);
429 dso__data_open_cnt++;
432 static void dso__list_del(struct dso *dso)
434 list_del(&dso->data.open_entry);
435 WARN_ONCE(dso__data_open_cnt <= 0,
436 "DSO data fd counter out of bounds.");
437 dso__data_open_cnt--;
440 static void close_first_dso(void);
442 static int do_open(char *name)
445 char sbuf[STRERR_BUFSIZE];
448 fd = open(name, O_RDONLY);
452 pr_debug("dso open failed: %s\n",
453 str_error_r(errno, sbuf, sizeof(sbuf)));
454 if (!dso__data_open_cnt || errno != EMFILE)
463 static int __open_dso(struct dso *dso, struct machine *machine)
466 char *root_dir = (char *)"";
467 char *name = malloc(PATH_MAX);
473 root_dir = machine->root_dir;
475 if (dso__read_binary_type_filename(dso, dso->binary_type,
476 root_dir, name, PATH_MAX))
479 if (!is_regular_file(name))
482 if (dso__needs_decompress(dso)) {
483 char newpath[KMOD_DECOMP_LEN];
484 size_t len = sizeof(newpath);
486 if (dso__decompress_kmodule_path(dso, name, newpath, len) < 0) {
487 fd = -dso->load_errno;
491 strcpy(name, newpath);
496 if (dso__needs_decompress(dso))
504 static void check_data_close(void);
507 * dso_close - Open DSO data file
510 * Open @dso's data file descriptor and updates
511 * list/count of open DSO objects.
513 static int open_dso(struct dso *dso, struct machine *machine)
518 if (dso->binary_type != DSO_BINARY_TYPE__BUILD_ID_CACHE)
519 nsinfo__mountns_enter(dso->nsinfo, &nsc);
520 fd = __open_dso(dso, machine);
521 if (dso->binary_type != DSO_BINARY_TYPE__BUILD_ID_CACHE)
522 nsinfo__mountns_exit(&nsc);
527 * Check if we crossed the allowed number
528 * of opened DSOs and close one if needed.
536 static void close_data_fd(struct dso *dso)
538 if (dso->data.fd >= 0) {
541 dso->data.file_size = 0;
547 * dso_close - Close DSO data file
550 * Close @dso's data file descriptor and updates
551 * list/count of open DSO objects.
553 static void close_dso(struct dso *dso)
558 static void close_first_dso(void)
562 dso = list_first_entry(&dso__data_open, struct dso, data.open_entry);
566 static rlim_t get_fd_limit(void)
571 /* Allow half of the current open fd limit. */
572 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
573 if (l.rlim_cur == RLIM_INFINITY)
576 limit = l.rlim_cur / 2;
578 pr_err("failed to get fd limit\n");
585 static rlim_t fd_limit;
588 * Used only by tests/dso-data.c to reset the environment
589 * for tests. I dont expect we should change this during
592 void reset_fd_limit(void)
597 static bool may_cache_fd(void)
600 fd_limit = get_fd_limit();
602 if (fd_limit == RLIM_INFINITY)
605 return fd_limit > (rlim_t) dso__data_open_cnt;
609 * Check and close LRU dso if we crossed allowed limit
610 * for opened dso file descriptors. The limit is half
611 * of the RLIMIT_NOFILE files opened.
613 static void check_data_close(void)
615 bool cache_fd = may_cache_fd();
622 * dso__data_close - Close DSO data file
625 * External interface to close @dso's data file descriptor.
627 void dso__data_close(struct dso *dso)
629 pthread_mutex_lock(&dso__data_open_lock);
631 pthread_mutex_unlock(&dso__data_open_lock);
634 static void try_to_open_dso(struct dso *dso, struct machine *machine)
636 enum dso_binary_type binary_type_data[] = {
637 DSO_BINARY_TYPE__BUILD_ID_CACHE,
638 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
639 DSO_BINARY_TYPE__NOT_FOUND,
643 if (dso->data.fd >= 0)
646 if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) {
647 dso->data.fd = open_dso(dso, machine);
652 dso->binary_type = binary_type_data[i++];
654 dso->data.fd = open_dso(dso, machine);
655 if (dso->data.fd >= 0)
658 } while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND);
660 if (dso->data.fd >= 0)
661 dso->data.status = DSO_DATA_STATUS_OK;
663 dso->data.status = DSO_DATA_STATUS_ERROR;
667 * dso__data_get_fd - Get dso's data file descriptor
669 * @machine: machine object
671 * External interface to find dso's file, open it and
672 * returns file descriptor. It should be paired with
673 * dso__data_put_fd() if it returns non-negative value.
675 int dso__data_get_fd(struct dso *dso, struct machine *machine)
677 if (dso->data.status == DSO_DATA_STATUS_ERROR)
680 if (pthread_mutex_lock(&dso__data_open_lock) < 0)
683 try_to_open_dso(dso, machine);
685 if (dso->data.fd < 0)
686 pthread_mutex_unlock(&dso__data_open_lock);
691 void dso__data_put_fd(struct dso *dso __maybe_unused)
693 pthread_mutex_unlock(&dso__data_open_lock);
696 bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by)
700 if (dso->data.status_seen & flag)
703 dso->data.status_seen |= flag;
709 dso_cache__free(struct dso *dso)
711 struct rb_root *root = &dso->data.cache;
712 struct rb_node *next = rb_first(root);
714 pthread_mutex_lock(&dso->lock);
716 struct dso_cache *cache;
718 cache = rb_entry(next, struct dso_cache, rb_node);
719 next = rb_next(&cache->rb_node);
720 rb_erase(&cache->rb_node, root);
723 pthread_mutex_unlock(&dso->lock);
726 static struct dso_cache *dso_cache__find(struct dso *dso, u64 offset)
728 const struct rb_root *root = &dso->data.cache;
729 struct rb_node * const *p = &root->rb_node;
730 const struct rb_node *parent = NULL;
731 struct dso_cache *cache;
737 cache = rb_entry(parent, struct dso_cache, rb_node);
738 end = cache->offset + DSO__DATA_CACHE_SIZE;
740 if (offset < cache->offset)
742 else if (offset >= end)
751 static struct dso_cache *
752 dso_cache__insert(struct dso *dso, struct dso_cache *new)
754 struct rb_root *root = &dso->data.cache;
755 struct rb_node **p = &root->rb_node;
756 struct rb_node *parent = NULL;
757 struct dso_cache *cache;
758 u64 offset = new->offset;
760 pthread_mutex_lock(&dso->lock);
765 cache = rb_entry(parent, struct dso_cache, rb_node);
766 end = cache->offset + DSO__DATA_CACHE_SIZE;
768 if (offset < cache->offset)
770 else if (offset >= end)
776 rb_link_node(&new->rb_node, parent, p);
777 rb_insert_color(&new->rb_node, root);
781 pthread_mutex_unlock(&dso->lock);
786 dso_cache__memcpy(struct dso_cache *cache, u64 offset,
789 u64 cache_offset = offset - cache->offset;
790 u64 cache_size = min(cache->size - cache_offset, size);
792 memcpy(data, cache->data + cache_offset, cache_size);
797 dso_cache__read(struct dso *dso, struct machine *machine,
798 u64 offset, u8 *data, ssize_t size)
800 struct dso_cache *cache;
801 struct dso_cache *old;
807 cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
811 pthread_mutex_lock(&dso__data_open_lock);
814 * dso->data.fd might be closed if other thread opened another
815 * file (dso) due to open file limit (RLIMIT_NOFILE).
817 try_to_open_dso(dso, machine);
819 if (dso->data.fd < 0) {
821 dso->data.status = DSO_DATA_STATUS_ERROR;
825 cache_offset = offset & DSO__DATA_CACHE_MASK;
827 ret = pread(dso->data.fd, cache->data, DSO__DATA_CACHE_SIZE, cache_offset);
831 cache->offset = cache_offset;
835 pthread_mutex_unlock(&dso__data_open_lock);
838 old = dso_cache__insert(dso, cache);
840 /* we lose the race */
845 ret = dso_cache__memcpy(cache, offset, data, size);
854 static ssize_t dso_cache_read(struct dso *dso, struct machine *machine,
855 u64 offset, u8 *data, ssize_t size)
857 struct dso_cache *cache;
859 cache = dso_cache__find(dso, offset);
861 return dso_cache__memcpy(cache, offset, data, size);
863 return dso_cache__read(dso, machine, offset, data, size);
867 * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
868 * in the rb_tree. Any read to already cached data is served
871 static ssize_t cached_read(struct dso *dso, struct machine *machine,
872 u64 offset, u8 *data, ssize_t size)
880 ret = dso_cache_read(dso, machine, offset, p, size);
884 /* Reached EOF, return what we have. */
900 static int data_file_size(struct dso *dso, struct machine *machine)
904 char sbuf[STRERR_BUFSIZE];
906 if (dso->data.file_size)
909 if (dso->data.status == DSO_DATA_STATUS_ERROR)
912 pthread_mutex_lock(&dso__data_open_lock);
915 * dso->data.fd might be closed if other thread opened another
916 * file (dso) due to open file limit (RLIMIT_NOFILE).
918 try_to_open_dso(dso, machine);
920 if (dso->data.fd < 0) {
922 dso->data.status = DSO_DATA_STATUS_ERROR;
926 if (fstat(dso->data.fd, &st) < 0) {
928 pr_err("dso cache fstat failed: %s\n",
929 str_error_r(errno, sbuf, sizeof(sbuf)));
930 dso->data.status = DSO_DATA_STATUS_ERROR;
933 dso->data.file_size = st.st_size;
936 pthread_mutex_unlock(&dso__data_open_lock);
941 * dso__data_size - Return dso data size
943 * @machine: machine object
945 * Return: dso data size
947 off_t dso__data_size(struct dso *dso, struct machine *machine)
949 if (data_file_size(dso, machine))
952 /* For now just estimate dso data size is close to file size */
953 return dso->data.file_size;
956 static ssize_t data_read_offset(struct dso *dso, struct machine *machine,
957 u64 offset, u8 *data, ssize_t size)
959 if (data_file_size(dso, machine))
962 /* Check the offset sanity. */
963 if (offset > dso->data.file_size)
966 if (offset + size < offset)
969 return cached_read(dso, machine, offset, data, size);
973 * dso__data_read_offset - Read data from dso file offset
975 * @machine: machine object
976 * @offset: file offset
977 * @data: buffer to store data
978 * @size: size of the @data buffer
980 * External interface to read data from dso file offset. Open
981 * dso data file and use cached_read to get the data.
983 ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
984 u64 offset, u8 *data, ssize_t size)
986 if (dso->data.status == DSO_DATA_STATUS_ERROR)
989 return data_read_offset(dso, machine, offset, data, size);
993 * dso__data_read_addr - Read data from dso address
995 * @machine: machine object
996 * @add: virtual memory address
997 * @data: buffer to store data
998 * @size: size of the @data buffer
1000 * External interface to read data from dso address.
1002 ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
1003 struct machine *machine, u64 addr,
1004 u8 *data, ssize_t size)
1006 u64 offset = map->map_ip(map, addr);
1007 return dso__data_read_offset(dso, machine, offset, data, size);
1010 struct map *dso__new_map(const char *name)
1012 struct map *map = NULL;
1013 struct dso *dso = dso__new(name);
1016 map = map__new2(0, dso, MAP__FUNCTION);
1021 struct dso *machine__findnew_kernel(struct machine *machine, const char *name,
1022 const char *short_name, int dso_type)
1025 * The kernel dso could be created by build_id processing.
1027 struct dso *dso = machine__findnew_dso(machine, name);
1030 * We need to run this in all cases, since during the build_id
1031 * processing we had no idea this was the kernel dso.
1034 dso__set_short_name(dso, short_name, false);
1035 dso->kernel = dso_type;
1042 * Find a matching entry and/or link current entry to RB tree.
1043 * Either one of the dso or name parameter must be non-NULL or the
1044 * function will not work.
1046 static struct dso *__dso__findlink_by_longname(struct rb_root *root,
1047 struct dso *dso, const char *name)
1049 struct rb_node **p = &root->rb_node;
1050 struct rb_node *parent = NULL;
1053 name = dso->long_name;
1055 * Find node with the matching name
1058 struct dso *this = rb_entry(*p, struct dso, rb_node);
1059 int rc = strcmp(name, this->long_name);
1064 * In case the new DSO is a duplicate of an existing
1065 * one, print a one-time warning & put the new entry
1066 * at the end of the list of duplicates.
1068 if (!dso || (dso == this))
1069 return this; /* Find matching dso */
1071 * The core kernel DSOs may have duplicated long name.
1072 * In this case, the short name should be different.
1073 * Comparing the short names to differentiate the DSOs.
1075 rc = strcmp(dso->short_name, this->short_name);
1077 pr_err("Duplicated dso name: %s\n", name);
1082 p = &parent->rb_left;
1084 p = &parent->rb_right;
1087 /* Add new node and rebalance tree */
1088 rb_link_node(&dso->rb_node, parent, p);
1089 rb_insert_color(&dso->rb_node, root);
1095 static inline struct dso *__dso__find_by_longname(struct rb_root *root,
1098 return __dso__findlink_by_longname(root, NULL, name);
1101 void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
1103 struct rb_root *root = dso->root;
1108 if (dso->long_name_allocated)
1109 free((char *)dso->long_name);
1112 rb_erase(&dso->rb_node, root);
1114 * __dso__findlink_by_longname() isn't guaranteed to add it
1115 * back, so a clean removal is required here.
1117 RB_CLEAR_NODE(&dso->rb_node);
1121 dso->long_name = name;
1122 dso->long_name_len = strlen(name);
1123 dso->long_name_allocated = name_allocated;
1126 __dso__findlink_by_longname(root, dso, NULL);
1129 void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
1134 if (dso->short_name_allocated)
1135 free((char *)dso->short_name);
1137 dso->short_name = name;
1138 dso->short_name_len = strlen(name);
1139 dso->short_name_allocated = name_allocated;
1142 static void dso__set_basename(struct dso *dso)
1145 * basename() may modify path buffer, so we must pass
1148 char *base, *lname = strdup(dso->long_name);
1154 * basename() may return a pointer to internal
1155 * storage which is reused in subsequent calls
1156 * so copy the result.
1158 base = strdup(basename(lname));
1165 dso__set_short_name(dso, base, true);
1168 int dso__name_len(const struct dso *dso)
1171 return strlen("[unknown]");
1173 return dso->long_name_len;
1175 return dso->short_name_len;
1178 bool dso__loaded(const struct dso *dso, enum map_type type)
1180 return dso->loaded & (1 << type);
1183 bool dso__sorted_by_name(const struct dso *dso, enum map_type type)
1185 return dso->sorted_by_name & (1 << type);
1188 void dso__set_sorted_by_name(struct dso *dso, enum map_type type)
1190 dso->sorted_by_name |= (1 << type);
1193 struct dso *dso__new(const char *name)
1195 struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
1199 strcpy(dso->name, name);
1200 dso__set_long_name(dso, dso->name, false);
1201 dso__set_short_name(dso, dso->name, false);
1202 for (i = 0; i < MAP__NR_TYPES; ++i)
1203 dso->symbols[i] = dso->symbol_names[i] = RB_ROOT;
1204 dso->data.cache = RB_ROOT;
1205 dso->inlined_nodes = RB_ROOT;
1206 dso->srclines = RB_ROOT;
1208 dso->data.status = DSO_DATA_STATUS_UNKNOWN;
1209 dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
1210 dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
1211 dso->is_64_bit = (sizeof(void *) == 8);
1214 dso->sorted_by_name = 0;
1215 dso->has_build_id = 0;
1216 dso->has_srcline = 1;
1218 dso->kernel = DSO_TYPE_USER;
1219 dso->needs_swap = DSO_SWAP__UNSET;
1220 RB_CLEAR_NODE(&dso->rb_node);
1222 INIT_LIST_HEAD(&dso->node);
1223 INIT_LIST_HEAD(&dso->data.open_entry);
1224 pthread_mutex_init(&dso->lock, NULL);
1225 refcount_set(&dso->refcnt, 1);
1231 void dso__delete(struct dso *dso)
1235 if (!RB_EMPTY_NODE(&dso->rb_node))
1236 pr_err("DSO %s is still in rbtree when being deleted!\n",
1239 /* free inlines first, as they reference symbols */
1240 inlines__tree_delete(&dso->inlined_nodes);
1241 srcline__tree_delete(&dso->srclines);
1242 for (i = 0; i < MAP__NR_TYPES; ++i)
1243 symbols__delete(&dso->symbols[i]);
1245 if (dso->short_name_allocated) {
1246 zfree((char **)&dso->short_name);
1247 dso->short_name_allocated = false;
1250 if (dso->long_name_allocated) {
1251 zfree((char **)&dso->long_name);
1252 dso->long_name_allocated = false;
1255 dso__data_close(dso);
1256 auxtrace_cache__free(dso->auxtrace_cache);
1257 dso_cache__free(dso);
1259 zfree(&dso->symsrc_filename);
1260 nsinfo__zput(dso->nsinfo);
1261 pthread_mutex_destroy(&dso->lock);
1265 struct dso *dso__get(struct dso *dso)
1268 refcount_inc(&dso->refcnt);
1272 void dso__put(struct dso *dso)
1274 if (dso && refcount_dec_and_test(&dso->refcnt))
1278 void dso__set_build_id(struct dso *dso, void *build_id)
1280 memcpy(dso->build_id, build_id, sizeof(dso->build_id));
1281 dso->has_build_id = 1;
1284 bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
1286 return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
1289 void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
1291 char path[PATH_MAX];
1293 if (machine__is_default_guest(machine))
1295 sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
1296 if (sysfs__read_build_id(path, dso->build_id,
1297 sizeof(dso->build_id)) == 0)
1298 dso->has_build_id = true;
1301 int dso__kernel_module_get_build_id(struct dso *dso,
1302 const char *root_dir)
1304 char filename[PATH_MAX];
1306 * kernel module short names are of the form "[module]" and
1307 * we need just "module" here.
1309 const char *name = dso->short_name + 1;
1311 snprintf(filename, sizeof(filename),
1312 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1313 root_dir, (int)strlen(name) - 1, name);
1315 if (sysfs__read_build_id(filename, dso->build_id,
1316 sizeof(dso->build_id)) == 0)
1317 dso->has_build_id = true;
1322 bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
1324 bool have_build_id = false;
1326 struct nscookie nsc;
1328 list_for_each_entry(pos, head, node) {
1329 if (with_hits && !pos->hit && !dso__is_vdso(pos))
1331 if (pos->has_build_id) {
1332 have_build_id = true;
1335 nsinfo__mountns_enter(pos->nsinfo, &nsc);
1336 if (filename__read_build_id(pos->long_name, pos->build_id,
1337 sizeof(pos->build_id)) > 0) {
1338 have_build_id = true;
1339 pos->has_build_id = true;
1341 nsinfo__mountns_exit(&nsc);
1344 return have_build_id;
1347 void __dsos__add(struct dsos *dsos, struct dso *dso)
1349 list_add_tail(&dso->node, &dsos->head);
1350 __dso__findlink_by_longname(&dsos->root, dso, NULL);
1352 * It is now in the linked list, grab a reference, then garbage collect
1353 * this when needing memory, by looking at LRU dso instances in the
1354 * list with atomic_read(&dso->refcnt) == 1, i.e. no references
1355 * anywhere besides the one for the list, do, under a lock for the
1356 * list: remove it from the list, then a dso__put(), that probably will
1357 * be the last and will then call dso__delete(), end of life.
1359 * That, or at the end of the 'struct machine' lifetime, when all
1360 * 'struct dso' instances will be removed from the list, in
1361 * dsos__exit(), if they have no other reference from some other data
1364 * E.g.: after processing a 'perf.data' file and storing references
1365 * to objects instantiated while processing events, we will have
1366 * references to the 'thread', 'map', 'dso' structs all from 'struct
1367 * hist_entry' instances, but we may not need anything not referenced,
1368 * so we might as well call machines__exit()/machines__delete() and
1369 * garbage collect it.
1374 void dsos__add(struct dsos *dsos, struct dso *dso)
1376 down_write(&dsos->lock);
1377 __dsos__add(dsos, dso);
1378 up_write(&dsos->lock);
1381 struct dso *__dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
1386 list_for_each_entry(pos, &dsos->head, node)
1387 if (strcmp(pos->short_name, name) == 0)
1391 return __dso__find_by_longname(&dsos->root, name);
1394 struct dso *dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
1397 down_read(&dsos->lock);
1398 dso = __dsos__find(dsos, name, cmp_short);
1399 up_read(&dsos->lock);
1403 struct dso *__dsos__addnew(struct dsos *dsos, const char *name)
1405 struct dso *dso = dso__new(name);
1408 __dsos__add(dsos, dso);
1409 dso__set_basename(dso);
1410 /* Put dso here because __dsos_add already got it */
1416 struct dso *__dsos__findnew(struct dsos *dsos, const char *name)
1418 struct dso *dso = __dsos__find(dsos, name, false);
1420 return dso ? dso : __dsos__addnew(dsos, name);
1423 struct dso *dsos__findnew(struct dsos *dsos, const char *name)
1426 down_write(&dsos->lock);
1427 dso = dso__get(__dsos__findnew(dsos, name));
1428 up_write(&dsos->lock);
1432 size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
1433 bool (skip)(struct dso *dso, int parm), int parm)
1438 list_for_each_entry(pos, head, node) {
1439 if (skip && skip(pos, parm))
1441 ret += dso__fprintf_buildid(pos, fp);
1442 ret += fprintf(fp, " %s\n", pos->long_name);
1447 size_t __dsos__fprintf(struct list_head *head, FILE *fp)
1452 list_for_each_entry(pos, head, node) {
1454 for (i = 0; i < MAP__NR_TYPES; ++i)
1455 ret += dso__fprintf(pos, i, fp);
1461 size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
1463 char sbuild_id[SBUILD_ID_SIZE];
1465 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1466 return fprintf(fp, "%s", sbuild_id);
1469 size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp)
1472 size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
1474 if (dso->short_name != dso->long_name)
1475 ret += fprintf(fp, "%s, ", dso->long_name);
1476 ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
1477 dso__loaded(dso, type) ? "" : "NOT ");
1478 ret += dso__fprintf_buildid(dso, fp);
1479 ret += fprintf(fp, ")\n");
1480 for (nd = rb_first(&dso->symbols[type]); nd; nd = rb_next(nd)) {
1481 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
1482 ret += symbol__fprintf(pos, fp);
1488 enum dso_type dso__type(struct dso *dso, struct machine *machine)
1491 enum dso_type type = DSO__TYPE_UNKNOWN;
1493 fd = dso__data_get_fd(dso, machine);
1495 type = dso__type_fd(fd);
1496 dso__data_put_fd(dso);
1502 int dso__strerror_load(struct dso *dso, char *buf, size_t buflen)
1504 int idx, errnum = dso->load_errno;
1506 * This must have a same ordering as the enum dso_load_errno.
1508 static const char *dso_load__error_str[] = {
1509 "Internal tools/perf/ library error",
1511 "Can not read build id",
1512 "Mismatching build id",
1513 "Decompression failure",
1516 BUG_ON(buflen == 0);
1519 const char *err = str_error_r(errnum, buf, buflen);
1522 scnprintf(buf, buflen, "%s", err);
1527 if (errnum < __DSO_LOAD_ERRNO__START || errnum >= __DSO_LOAD_ERRNO__END)
1530 idx = errnum - __DSO_LOAD_ERRNO__START;
1531 scnprintf(buf, buflen, "%s", dso_load__error_str[idx]);