3 #include <sys/resource.h>
11 char dso__symtab_origin(const struct dso *dso)
13 static const char origin[] = {
14 [DSO_BINARY_TYPE__KALLSYMS] = 'k',
15 [DSO_BINARY_TYPE__VMLINUX] = 'v',
16 [DSO_BINARY_TYPE__JAVA_JIT] = 'j',
17 [DSO_BINARY_TYPE__DEBUGLINK] = 'l',
18 [DSO_BINARY_TYPE__BUILD_ID_CACHE] = 'B',
19 [DSO_BINARY_TYPE__FEDORA_DEBUGINFO] = 'f',
20 [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO] = 'u',
21 [DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO] = 'o',
22 [DSO_BINARY_TYPE__BUILDID_DEBUGINFO] = 'b',
23 [DSO_BINARY_TYPE__SYSTEM_PATH_DSO] = 'd',
24 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE] = 'K',
25 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP] = 'm',
26 [DSO_BINARY_TYPE__GUEST_KALLSYMS] = 'g',
27 [DSO_BINARY_TYPE__GUEST_KMODULE] = 'G',
28 [DSO_BINARY_TYPE__GUEST_KMODULE_COMP] = 'M',
29 [DSO_BINARY_TYPE__GUEST_VMLINUX] = 'V',
32 if (dso == NULL || dso->symtab_type == DSO_BINARY_TYPE__NOT_FOUND)
34 return origin[dso->symtab_type];
37 int dso__read_binary_type_filename(const struct dso *dso,
38 enum dso_binary_type type,
39 char *root_dir, char *filename, size_t size)
41 char build_id_hex[BUILD_ID_SIZE * 2 + 1];
46 case DSO_BINARY_TYPE__DEBUGLINK: {
49 len = __symbol__join_symfs(filename, size, dso->long_name);
50 debuglink = filename + len;
51 while (debuglink != filename && *debuglink != '/')
53 if (*debuglink == '/')
55 ret = filename__read_debuglink(filename, debuglink,
56 size - (debuglink - filename));
59 case DSO_BINARY_TYPE__BUILD_ID_CACHE:
60 /* skip the locally configured cache if a symfs is given */
61 if (symbol_conf.symfs[0] ||
62 (dso__build_id_filename(dso, filename, size) == NULL))
66 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
67 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
68 snprintf(filename + len, size - len, "%s.debug", dso->long_name);
71 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
72 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
73 snprintf(filename + len, size - len, "%s", dso->long_name);
76 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
78 const char *last_slash;
81 last_slash = dso->long_name + dso->long_name_len;
82 while (last_slash != dso->long_name && *last_slash != '/')
85 len = __symbol__join_symfs(filename, size, "");
86 dir_size = last_slash - dso->long_name + 2;
87 if (dir_size > (size - len)) {
91 len += scnprintf(filename + len, dir_size, "%s", dso->long_name);
92 len += scnprintf(filename + len , size - len, ".debug%s",
97 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
98 if (!dso->has_build_id) {
103 build_id__sprintf(dso->build_id,
104 sizeof(dso->build_id),
106 len = __symbol__join_symfs(filename, size, "/usr/lib/debug/.build-id/");
107 snprintf(filename + len, size - len, "%.2s/%s.debug",
108 build_id_hex, build_id_hex + 2);
111 case DSO_BINARY_TYPE__VMLINUX:
112 case DSO_BINARY_TYPE__GUEST_VMLINUX:
113 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
114 __symbol__join_symfs(filename, size, dso->long_name);
117 case DSO_BINARY_TYPE__GUEST_KMODULE:
118 case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
119 path__join3(filename, size, symbol_conf.symfs,
120 root_dir, dso->long_name);
123 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
124 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
125 __symbol__join_symfs(filename, size, dso->long_name);
128 case DSO_BINARY_TYPE__KCORE:
129 case DSO_BINARY_TYPE__GUEST_KCORE:
130 snprintf(filename, size, "%s", dso->long_name);
134 case DSO_BINARY_TYPE__KALLSYMS:
135 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
136 case DSO_BINARY_TYPE__JAVA_JIT:
137 case DSO_BINARY_TYPE__NOT_FOUND:
145 static const struct {
147 int (*decompress)(const char *input, int output);
149 #ifdef HAVE_ZLIB_SUPPORT
150 { "gz", gzip_decompress_to_file },
152 #ifdef HAVE_LZMA_SUPPORT
153 { "xz", lzma_decompress_to_file },
158 bool is_supported_compression(const char *ext)
162 for (i = 0; compressions[i].fmt; i++) {
163 if (!strcmp(ext, compressions[i].fmt))
169 bool is_kernel_module(const char *pathname)
173 if (kmod_path__parse(&m, pathname))
179 bool decompress_to_file(const char *ext, const char *filename, int output_fd)
183 for (i = 0; compressions[i].fmt; i++) {
184 if (!strcmp(ext, compressions[i].fmt))
185 return !compressions[i].decompress(filename,
191 bool dso__needs_decompress(struct dso *dso)
193 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
194 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
198 * Parses kernel module specified in @path and updates
201 * @comp - true if @path contains supported compression suffix,
203 * @kmod - true if @path contains '.ko' suffix in right position,
205 * @name - if (@alloc_name && @kmod) is true, it contains strdup-ed base name
206 * of the kernel module without suffixes, otherwise strudup-ed
208 * @ext - if (@alloc_ext && @comp) is true, it contains strdup-ed string
209 * the compression suffix
211 * Returns 0 if there's no strdup error, -ENOMEM otherwise.
213 int __kmod_path__parse(struct kmod_path *m, const char *path,
214 bool alloc_name, bool alloc_ext)
216 const char *name = strrchr(path, '/');
217 const char *ext = strrchr(path, '.');
219 memset(m, 0x0, sizeof(*m));
220 name = name ? name + 1 : path;
222 /* No extension, just return name. */
225 m->name = strdup(name);
226 return m->name ? 0 : -ENOMEM;
231 if (is_supported_compression(ext + 1)) {
236 /* Check .ko extension only if there's enough name left. */
238 m->kmod = !strncmp(ext, ".ko", 3);
242 if (asprintf(&m->name, "[%.*s]", (int) (ext - name), name) == -1)
245 if (asprintf(&m->name, "%s", name) == -1)
249 strxfrchar(m->name, '-', '_');
252 if (alloc_ext && m->comp) {
253 m->ext = strdup(ext + 4);
255 free((void *) m->name);
264 * Global list of open DSOs and the counter.
266 static LIST_HEAD(dso__data_open);
267 static long dso__data_open_cnt;
268 static pthread_mutex_t dso__data_open_lock = PTHREAD_MUTEX_INITIALIZER;
270 static void dso__list_add(struct dso *dso)
272 list_add_tail(&dso->data.open_entry, &dso__data_open);
273 dso__data_open_cnt++;
276 static void dso__list_del(struct dso *dso)
278 list_del(&dso->data.open_entry);
279 WARN_ONCE(dso__data_open_cnt <= 0,
280 "DSO data fd counter out of bounds.");
281 dso__data_open_cnt--;
284 static void close_first_dso(void);
286 static int do_open(char *name)
289 char sbuf[STRERR_BUFSIZE];
292 fd = open(name, O_RDONLY);
296 pr_debug("dso open failed: %s\n",
297 strerror_r(errno, sbuf, sizeof(sbuf)));
298 if (!dso__data_open_cnt || errno != EMFILE)
307 static int __open_dso(struct dso *dso, struct machine *machine)
310 char *root_dir = (char *)"";
311 char *name = malloc(PATH_MAX);
317 root_dir = machine->root_dir;
319 if (dso__read_binary_type_filename(dso, dso->binary_type,
320 root_dir, name, PATH_MAX)) {
330 static void check_data_close(void);
333 * dso_close - Open DSO data file
336 * Open @dso's data file descriptor and updates
337 * list/count of open DSO objects.
339 static int open_dso(struct dso *dso, struct machine *machine)
341 int fd = __open_dso(dso, machine);
346 * Check if we crossed the allowed number
347 * of opened DSOs and close one if needed.
355 static void close_data_fd(struct dso *dso)
357 if (dso->data.fd >= 0) {
360 dso->data.file_size = 0;
366 * dso_close - Close DSO data file
369 * Close @dso's data file descriptor and updates
370 * list/count of open DSO objects.
372 static void close_dso(struct dso *dso)
377 static void close_first_dso(void)
381 dso = list_first_entry(&dso__data_open, struct dso, data.open_entry);
385 static rlim_t get_fd_limit(void)
390 /* Allow half of the current open fd limit. */
391 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
392 if (l.rlim_cur == RLIM_INFINITY)
395 limit = l.rlim_cur / 2;
397 pr_err("failed to get fd limit\n");
404 static bool may_cache_fd(void)
409 limit = get_fd_limit();
411 if (limit == RLIM_INFINITY)
414 return limit > (rlim_t) dso__data_open_cnt;
418 * Check and close LRU dso if we crossed allowed limit
419 * for opened dso file descriptors. The limit is half
420 * of the RLIMIT_NOFILE files opened.
422 static void check_data_close(void)
424 bool cache_fd = may_cache_fd();
431 * dso__data_close - Close DSO data file
434 * External interface to close @dso's data file descriptor.
436 void dso__data_close(struct dso *dso)
438 pthread_mutex_lock(&dso__data_open_lock);
440 pthread_mutex_unlock(&dso__data_open_lock);
443 static void try_to_open_dso(struct dso *dso, struct machine *machine)
445 enum dso_binary_type binary_type_data[] = {
446 DSO_BINARY_TYPE__BUILD_ID_CACHE,
447 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
448 DSO_BINARY_TYPE__NOT_FOUND,
452 if (dso->data.fd >= 0)
455 if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) {
456 dso->data.fd = open_dso(dso, machine);
461 dso->binary_type = binary_type_data[i++];
463 dso->data.fd = open_dso(dso, machine);
464 if (dso->data.fd >= 0)
467 } while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND);
469 if (dso->data.fd >= 0)
470 dso->data.status = DSO_DATA_STATUS_OK;
472 dso->data.status = DSO_DATA_STATUS_ERROR;
476 * dso__data_get_fd - Get dso's data file descriptor
478 * @machine: machine object
480 * External interface to find dso's file, open it and
481 * returns file descriptor. It should be paired with
482 * dso__data_put_fd() if it returns non-negative value.
484 int dso__data_get_fd(struct dso *dso, struct machine *machine)
486 if (dso->data.status == DSO_DATA_STATUS_ERROR)
489 if (pthread_mutex_lock(&dso__data_open_lock) < 0)
492 try_to_open_dso(dso, machine);
494 if (dso->data.fd < 0)
495 pthread_mutex_unlock(&dso__data_open_lock);
500 void dso__data_put_fd(struct dso *dso __maybe_unused)
502 pthread_mutex_unlock(&dso__data_open_lock);
505 bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by)
509 if (dso->data.status_seen & flag)
512 dso->data.status_seen |= flag;
518 dso_cache__free(struct dso *dso)
520 struct rb_root *root = &dso->data.cache;
521 struct rb_node *next = rb_first(root);
523 pthread_mutex_lock(&dso->lock);
525 struct dso_cache *cache;
527 cache = rb_entry(next, struct dso_cache, rb_node);
528 next = rb_next(&cache->rb_node);
529 rb_erase(&cache->rb_node, root);
532 pthread_mutex_unlock(&dso->lock);
535 static struct dso_cache *dso_cache__find(struct dso *dso, u64 offset)
537 const struct rb_root *root = &dso->data.cache;
538 struct rb_node * const *p = &root->rb_node;
539 const struct rb_node *parent = NULL;
540 struct dso_cache *cache;
546 cache = rb_entry(parent, struct dso_cache, rb_node);
547 end = cache->offset + DSO__DATA_CACHE_SIZE;
549 if (offset < cache->offset)
551 else if (offset >= end)
560 static struct dso_cache *
561 dso_cache__insert(struct dso *dso, struct dso_cache *new)
563 struct rb_root *root = &dso->data.cache;
564 struct rb_node **p = &root->rb_node;
565 struct rb_node *parent = NULL;
566 struct dso_cache *cache;
567 u64 offset = new->offset;
569 pthread_mutex_lock(&dso->lock);
574 cache = rb_entry(parent, struct dso_cache, rb_node);
575 end = cache->offset + DSO__DATA_CACHE_SIZE;
577 if (offset < cache->offset)
579 else if (offset >= end)
585 rb_link_node(&new->rb_node, parent, p);
586 rb_insert_color(&new->rb_node, root);
590 pthread_mutex_unlock(&dso->lock);
595 dso_cache__memcpy(struct dso_cache *cache, u64 offset,
598 u64 cache_offset = offset - cache->offset;
599 u64 cache_size = min(cache->size - cache_offset, size);
601 memcpy(data, cache->data + cache_offset, cache_size);
606 dso_cache__read(struct dso *dso, struct machine *machine,
607 u64 offset, u8 *data, ssize_t size)
609 struct dso_cache *cache;
610 struct dso_cache *old;
616 cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
620 pthread_mutex_lock(&dso__data_open_lock);
623 * dso->data.fd might be closed if other thread opened another
624 * file (dso) due to open file limit (RLIMIT_NOFILE).
626 try_to_open_dso(dso, machine);
628 if (dso->data.fd < 0) {
630 dso->data.status = DSO_DATA_STATUS_ERROR;
634 cache_offset = offset & DSO__DATA_CACHE_MASK;
636 ret = pread(dso->data.fd, cache->data, DSO__DATA_CACHE_SIZE, cache_offset);
640 cache->offset = cache_offset;
644 pthread_mutex_unlock(&dso__data_open_lock);
647 old = dso_cache__insert(dso, cache);
649 /* we lose the race */
654 ret = dso_cache__memcpy(cache, offset, data, size);
663 static ssize_t dso_cache_read(struct dso *dso, struct machine *machine,
664 u64 offset, u8 *data, ssize_t size)
666 struct dso_cache *cache;
668 cache = dso_cache__find(dso, offset);
670 return dso_cache__memcpy(cache, offset, data, size);
672 return dso_cache__read(dso, machine, offset, data, size);
676 * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
677 * in the rb_tree. Any read to already cached data is served
680 static ssize_t cached_read(struct dso *dso, struct machine *machine,
681 u64 offset, u8 *data, ssize_t size)
689 ret = dso_cache_read(dso, machine, offset, p, size);
693 /* Reached EOF, return what we have. */
709 static int data_file_size(struct dso *dso, struct machine *machine)
713 char sbuf[STRERR_BUFSIZE];
715 if (dso->data.file_size)
718 if (dso->data.status == DSO_DATA_STATUS_ERROR)
721 pthread_mutex_lock(&dso__data_open_lock);
724 * dso->data.fd might be closed if other thread opened another
725 * file (dso) due to open file limit (RLIMIT_NOFILE).
727 try_to_open_dso(dso, machine);
729 if (dso->data.fd < 0) {
731 dso->data.status = DSO_DATA_STATUS_ERROR;
735 if (fstat(dso->data.fd, &st) < 0) {
737 pr_err("dso cache fstat failed: %s\n",
738 strerror_r(errno, sbuf, sizeof(sbuf)));
739 dso->data.status = DSO_DATA_STATUS_ERROR;
742 dso->data.file_size = st.st_size;
745 pthread_mutex_unlock(&dso__data_open_lock);
750 * dso__data_size - Return dso data size
752 * @machine: machine object
754 * Return: dso data size
756 off_t dso__data_size(struct dso *dso, struct machine *machine)
758 if (data_file_size(dso, machine))
761 /* For now just estimate dso data size is close to file size */
762 return dso->data.file_size;
765 static ssize_t data_read_offset(struct dso *dso, struct machine *machine,
766 u64 offset, u8 *data, ssize_t size)
768 if (data_file_size(dso, machine))
771 /* Check the offset sanity. */
772 if (offset > dso->data.file_size)
775 if (offset + size < offset)
778 return cached_read(dso, machine, offset, data, size);
782 * dso__data_read_offset - Read data from dso file offset
784 * @machine: machine object
785 * @offset: file offset
786 * @data: buffer to store data
787 * @size: size of the @data buffer
789 * External interface to read data from dso file offset. Open
790 * dso data file and use cached_read to get the data.
792 ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
793 u64 offset, u8 *data, ssize_t size)
795 if (dso->data.status == DSO_DATA_STATUS_ERROR)
798 return data_read_offset(dso, machine, offset, data, size);
802 * dso__data_read_addr - Read data from dso address
804 * @machine: machine object
805 * @add: virtual memory address
806 * @data: buffer to store data
807 * @size: size of the @data buffer
809 * External interface to read data from dso address.
811 ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
812 struct machine *machine, u64 addr,
813 u8 *data, ssize_t size)
815 u64 offset = map->map_ip(map, addr);
816 return dso__data_read_offset(dso, machine, offset, data, size);
819 struct map *dso__new_map(const char *name)
821 struct map *map = NULL;
822 struct dso *dso = dso__new(name);
825 map = map__new2(0, dso, MAP__FUNCTION);
830 struct dso *dso__kernel_findnew(struct machine *machine, const char *name,
831 const char *short_name, int dso_type)
834 * The kernel dso could be created by build_id processing.
836 struct dso *dso = __dsos__findnew(&machine->kernel_dsos, name);
839 * We need to run this in all cases, since during the build_id
840 * processing we had no idea this was the kernel dso.
843 dso__set_short_name(dso, short_name, false);
844 dso->kernel = dso_type;
851 * Find a matching entry and/or link current entry to RB tree.
852 * Either one of the dso or name parameter must be non-NULL or the
853 * function will not work.
855 static struct dso *dso__findlink_by_longname(struct rb_root *root,
856 struct dso *dso, const char *name)
858 struct rb_node **p = &root->rb_node;
859 struct rb_node *parent = NULL;
862 name = dso->long_name;
864 * Find node with the matching name
867 struct dso *this = rb_entry(*p, struct dso, rb_node);
868 int rc = strcmp(name, this->long_name);
873 * In case the new DSO is a duplicate of an existing
874 * one, print an one-time warning & put the new entry
875 * at the end of the list of duplicates.
877 if (!dso || (dso == this))
878 return this; /* Find matching dso */
880 * The core kernel DSOs may have duplicated long name.
881 * In this case, the short name should be different.
882 * Comparing the short names to differentiate the DSOs.
884 rc = strcmp(dso->short_name, this->short_name);
886 pr_err("Duplicated dso name: %s\n", name);
891 p = &parent->rb_left;
893 p = &parent->rb_right;
896 /* Add new node and rebalance tree */
897 rb_link_node(&dso->rb_node, parent, p);
898 rb_insert_color(&dso->rb_node, root);
903 static inline struct dso *
904 dso__find_by_longname(const struct rb_root *root, const char *name)
906 return dso__findlink_by_longname((struct rb_root *)root, NULL, name);
909 void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
914 if (dso->long_name_allocated)
915 free((char *)dso->long_name);
917 dso->long_name = name;
918 dso->long_name_len = strlen(name);
919 dso->long_name_allocated = name_allocated;
922 void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
927 if (dso->short_name_allocated)
928 free((char *)dso->short_name);
930 dso->short_name = name;
931 dso->short_name_len = strlen(name);
932 dso->short_name_allocated = name_allocated;
935 static void dso__set_basename(struct dso *dso)
938 * basename() may modify path buffer, so we must pass
941 char *base, *lname = strdup(dso->long_name);
947 * basename() may return a pointer to internal
948 * storage which is reused in subsequent calls
949 * so copy the result.
951 base = strdup(basename(lname));
958 dso__set_short_name(dso, base, true);
961 int dso__name_len(const struct dso *dso)
964 return strlen("[unknown]");
966 return dso->long_name_len;
968 return dso->short_name_len;
971 bool dso__loaded(const struct dso *dso, enum map_type type)
973 return dso->loaded & (1 << type);
976 bool dso__sorted_by_name(const struct dso *dso, enum map_type type)
978 return dso->sorted_by_name & (1 << type);
981 void dso__set_sorted_by_name(struct dso *dso, enum map_type type)
983 dso->sorted_by_name |= (1 << type);
986 struct dso *dso__new(const char *name)
988 struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
992 strcpy(dso->name, name);
993 dso__set_long_name(dso, dso->name, false);
994 dso__set_short_name(dso, dso->name, false);
995 for (i = 0; i < MAP__NR_TYPES; ++i)
996 dso->symbols[i] = dso->symbol_names[i] = RB_ROOT;
997 dso->data.cache = RB_ROOT;
999 dso->data.status = DSO_DATA_STATUS_UNKNOWN;
1000 dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
1001 dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
1002 dso->is_64_bit = (sizeof(void *) == 8);
1005 dso->sorted_by_name = 0;
1006 dso->has_build_id = 0;
1007 dso->has_srcline = 1;
1009 dso->kernel = DSO_TYPE_USER;
1010 dso->needs_swap = DSO_SWAP__UNSET;
1011 RB_CLEAR_NODE(&dso->rb_node);
1012 INIT_LIST_HEAD(&dso->node);
1013 INIT_LIST_HEAD(&dso->data.open_entry);
1014 pthread_mutex_init(&dso->lock, NULL);
1020 void dso__delete(struct dso *dso)
1024 if (!RB_EMPTY_NODE(&dso->rb_node))
1025 pr_err("DSO %s is still in rbtree when being deleted!\n",
1027 for (i = 0; i < MAP__NR_TYPES; ++i)
1028 symbols__delete(&dso->symbols[i]);
1030 if (dso->short_name_allocated) {
1031 zfree((char **)&dso->short_name);
1032 dso->short_name_allocated = false;
1035 if (dso->long_name_allocated) {
1036 zfree((char **)&dso->long_name);
1037 dso->long_name_allocated = false;
1040 dso__data_close(dso);
1041 auxtrace_cache__free(dso->auxtrace_cache);
1042 dso_cache__free(dso);
1044 zfree(&dso->symsrc_filename);
1045 pthread_mutex_destroy(&dso->lock);
1049 void dso__set_build_id(struct dso *dso, void *build_id)
1051 memcpy(dso->build_id, build_id, sizeof(dso->build_id));
1052 dso->has_build_id = 1;
1055 bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
1057 return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
1060 void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
1062 char path[PATH_MAX];
1064 if (machine__is_default_guest(machine))
1066 sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
1067 if (sysfs__read_build_id(path, dso->build_id,
1068 sizeof(dso->build_id)) == 0)
1069 dso->has_build_id = true;
1072 int dso__kernel_module_get_build_id(struct dso *dso,
1073 const char *root_dir)
1075 char filename[PATH_MAX];
1077 * kernel module short names are of the form "[module]" and
1078 * we need just "module" here.
1080 const char *name = dso->short_name + 1;
1082 snprintf(filename, sizeof(filename),
1083 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1084 root_dir, (int)strlen(name) - 1, name);
1086 if (sysfs__read_build_id(filename, dso->build_id,
1087 sizeof(dso->build_id)) == 0)
1088 dso->has_build_id = true;
1093 bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
1095 bool have_build_id = false;
1098 list_for_each_entry(pos, head, node) {
1099 if (with_hits && !pos->hit)
1101 if (pos->has_build_id) {
1102 have_build_id = true;
1105 if (filename__read_build_id(pos->long_name, pos->build_id,
1106 sizeof(pos->build_id)) > 0) {
1107 have_build_id = true;
1108 pos->has_build_id = true;
1112 return have_build_id;
1115 void dsos__add(struct dsos *dsos, struct dso *dso)
1117 list_add_tail(&dso->node, &dsos->head);
1118 dso__findlink_by_longname(&dsos->root, dso, NULL);
1121 struct dso *dsos__find(const struct dsos *dsos, const char *name,
1127 list_for_each_entry(pos, &dsos->head, node)
1128 if (strcmp(pos->short_name, name) == 0)
1132 return dso__find_by_longname(&dsos->root, name);
1135 struct dso *dsos__addnew(struct dsos *dsos, const char *name)
1137 struct dso *dso = dso__new(name);
1140 dsos__add(dsos, dso);
1141 dso__set_basename(dso);
1146 struct dso *__dsos__findnew(struct dsos *dsos, const char *name)
1148 struct dso *dso = dsos__find(dsos, name, false);
1150 return dso ? dso : dsos__addnew(dsos, name);
1153 size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
1154 bool (skip)(struct dso *dso, int parm), int parm)
1159 list_for_each_entry(pos, head, node) {
1160 if (skip && skip(pos, parm))
1162 ret += dso__fprintf_buildid(pos, fp);
1163 ret += fprintf(fp, " %s\n", pos->long_name);
1168 size_t __dsos__fprintf(struct list_head *head, FILE *fp)
1173 list_for_each_entry(pos, head, node) {
1175 for (i = 0; i < MAP__NR_TYPES; ++i)
1176 ret += dso__fprintf(pos, i, fp);
1182 size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
1184 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1186 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1187 return fprintf(fp, "%s", sbuild_id);
1190 size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp)
1193 size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
1195 if (dso->short_name != dso->long_name)
1196 ret += fprintf(fp, "%s, ", dso->long_name);
1197 ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
1198 dso__loaded(dso, type) ? "" : "NOT ");
1199 ret += dso__fprintf_buildid(dso, fp);
1200 ret += fprintf(fp, ")\n");
1201 for (nd = rb_first(&dso->symbols[type]); nd; nd = rb_next(nd)) {
1202 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
1203 ret += symbol__fprintf(pos, fp);
1209 enum dso_type dso__type(struct dso *dso, struct machine *machine)
1212 enum dso_type type = DSO__TYPE_UNKNOWN;
1214 fd = dso__data_get_fd(dso, machine);
1216 type = dso__type_fd(fd);
1217 dso__data_put_fd(dso);
1223 int dso__strerror_load(struct dso *dso, char *buf, size_t buflen)
1225 int idx, errnum = dso->load_errno;
1227 * This must have a same ordering as the enum dso_load_errno.
1229 static const char *dso_load__error_str[] = {
1230 "Internal tools/perf/ library error",
1232 "Can not read build id",
1233 "Mismatching build id",
1234 "Decompression failure",
1237 BUG_ON(buflen == 0);
1240 const char *err = strerror_r(errnum, buf, buflen);
1243 scnprintf(buf, buflen, "%s", err);
1248 if (errnum < __DSO_LOAD_ERRNO__START || errnum >= __DSO_LOAD_ERRNO__END)
1251 idx = errnum - __DSO_LOAD_ERRNO__START;
1252 scnprintf(buf, buflen, "%s", dso_load__error_str[idx]);