Merge tag 'fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/rusty...
[linux/fpc-iii.git] / tools / perf / util / dso.c
blob45be944d450adfcfaf9c0dff3a0c68296769027a
1 #include <asm/bug.h>
2 #include <sys/time.h>
3 #include <sys/resource.h>
4 #include "symbol.h"
5 #include "dso.h"
6 #include "machine.h"
7 #include "util.h"
8 #include "debug.h"
10 char dso__symtab_origin(const struct dso *dso)
12 static const char origin[] = {
13 [DSO_BINARY_TYPE__KALLSYMS] = 'k',
14 [DSO_BINARY_TYPE__VMLINUX] = 'v',
15 [DSO_BINARY_TYPE__JAVA_JIT] = 'j',
16 [DSO_BINARY_TYPE__DEBUGLINK] = 'l',
17 [DSO_BINARY_TYPE__BUILD_ID_CACHE] = 'B',
18 [DSO_BINARY_TYPE__FEDORA_DEBUGINFO] = 'f',
19 [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO] = 'u',
20 [DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO] = 'o',
21 [DSO_BINARY_TYPE__BUILDID_DEBUGINFO] = 'b',
22 [DSO_BINARY_TYPE__SYSTEM_PATH_DSO] = 'd',
23 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE] = 'K',
24 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP] = 'm',
25 [DSO_BINARY_TYPE__GUEST_KALLSYMS] = 'g',
26 [DSO_BINARY_TYPE__GUEST_KMODULE] = 'G',
27 [DSO_BINARY_TYPE__GUEST_KMODULE_COMP] = 'M',
28 [DSO_BINARY_TYPE__GUEST_VMLINUX] = 'V',
31 if (dso == NULL || dso->symtab_type == DSO_BINARY_TYPE__NOT_FOUND)
32 return '!';
33 return origin[dso->symtab_type];
36 int dso__read_binary_type_filename(const struct dso *dso,
37 enum dso_binary_type type,
38 char *root_dir, char *filename, size_t size)
40 char build_id_hex[BUILD_ID_SIZE * 2 + 1];
41 int ret = 0;
42 size_t len;
44 switch (type) {
45 case DSO_BINARY_TYPE__DEBUGLINK: {
46 char *debuglink;
48 strncpy(filename, dso->long_name, size);
49 debuglink = filename + dso->long_name_len;
50 while (debuglink != filename && *debuglink != '/')
51 debuglink--;
52 if (*debuglink == '/')
53 debuglink++;
54 ret = filename__read_debuglink(dso->long_name, debuglink,
55 size - (debuglink - filename));
57 break;
58 case DSO_BINARY_TYPE__BUILD_ID_CACHE:
59 /* skip the locally configured cache if a symfs is given */
60 if (symbol_conf.symfs[0] ||
61 (dso__build_id_filename(dso, filename, size) == NULL))
62 ret = -1;
63 break;
65 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
66 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
67 snprintf(filename + len, size - len, "%s.debug", dso->long_name);
68 break;
70 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
71 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
72 snprintf(filename + len, size - len, "%s", dso->long_name);
73 break;
75 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
77 const char *last_slash;
78 size_t dir_size;
80 last_slash = dso->long_name + dso->long_name_len;
81 while (last_slash != dso->long_name && *last_slash != '/')
82 last_slash--;
84 len = __symbol__join_symfs(filename, size, "");
85 dir_size = last_slash - dso->long_name + 2;
86 if (dir_size > (size - len)) {
87 ret = -1;
88 break;
90 len += scnprintf(filename + len, dir_size, "%s", dso->long_name);
91 len += scnprintf(filename + len , size - len, ".debug%s",
92 last_slash);
93 break;
96 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
97 if (!dso->has_build_id) {
98 ret = -1;
99 break;
102 build_id__sprintf(dso->build_id,
103 sizeof(dso->build_id),
104 build_id_hex);
105 len = __symbol__join_symfs(filename, size, "/usr/lib/debug/.build-id/");
106 snprintf(filename + len, size - len, "%.2s/%s.debug",
107 build_id_hex, build_id_hex + 2);
108 break;
110 case DSO_BINARY_TYPE__VMLINUX:
111 case DSO_BINARY_TYPE__GUEST_VMLINUX:
112 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
113 __symbol__join_symfs(filename, size, dso->long_name);
114 break;
116 case DSO_BINARY_TYPE__GUEST_KMODULE:
117 case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
118 path__join3(filename, size, symbol_conf.symfs,
119 root_dir, dso->long_name);
120 break;
122 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
123 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
124 __symbol__join_symfs(filename, size, dso->long_name);
125 break;
127 case DSO_BINARY_TYPE__KCORE:
128 case DSO_BINARY_TYPE__GUEST_KCORE:
129 snprintf(filename, size, "%s", dso->long_name);
130 break;
132 default:
133 case DSO_BINARY_TYPE__KALLSYMS:
134 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
135 case DSO_BINARY_TYPE__JAVA_JIT:
136 case DSO_BINARY_TYPE__NOT_FOUND:
137 ret = -1;
138 break;
141 return ret;
144 static const struct {
145 const char *fmt;
146 int (*decompress)(const char *input, int output);
147 } compressions[] = {
148 #ifdef HAVE_ZLIB_SUPPORT
149 { "gz", gzip_decompress_to_file },
150 #endif
151 { NULL, NULL },
154 bool is_supported_compression(const char *ext)
156 unsigned i;
158 for (i = 0; compressions[i].fmt; i++) {
159 if (!strcmp(ext, compressions[i].fmt))
160 return true;
162 return false;
165 bool is_kmodule_extension(const char *ext)
167 if (strncmp(ext, "ko", 2))
168 return false;
170 if (ext[2] == '\0' || (ext[2] == '.' && is_supported_compression(ext+3)))
171 return true;
173 return false;
176 bool is_kernel_module(const char *pathname, bool *compressed)
178 const char *ext = strrchr(pathname, '.');
180 if (ext == NULL)
181 return false;
183 if (is_supported_compression(ext + 1)) {
184 if (compressed)
185 *compressed = true;
186 ext -= 3;
187 } else if (compressed)
188 *compressed = false;
190 return is_kmodule_extension(ext + 1);
193 bool decompress_to_file(const char *ext, const char *filename, int output_fd)
195 unsigned i;
197 for (i = 0; compressions[i].fmt; i++) {
198 if (!strcmp(ext, compressions[i].fmt))
199 return !compressions[i].decompress(filename,
200 output_fd);
202 return false;
205 bool dso__needs_decompress(struct dso *dso)
207 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
208 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
212 * Global list of open DSOs and the counter.
214 static LIST_HEAD(dso__data_open);
215 static long dso__data_open_cnt;
217 static void dso__list_add(struct dso *dso)
219 list_add_tail(&dso->data.open_entry, &dso__data_open);
220 dso__data_open_cnt++;
223 static void dso__list_del(struct dso *dso)
225 list_del(&dso->data.open_entry);
226 WARN_ONCE(dso__data_open_cnt <= 0,
227 "DSO data fd counter out of bounds.");
228 dso__data_open_cnt--;
231 static void close_first_dso(void);
233 static int do_open(char *name)
235 int fd;
236 char sbuf[STRERR_BUFSIZE];
238 do {
239 fd = open(name, O_RDONLY);
240 if (fd >= 0)
241 return fd;
243 pr_debug("dso open failed, mmap: %s\n",
244 strerror_r(errno, sbuf, sizeof(sbuf)));
245 if (!dso__data_open_cnt || errno != EMFILE)
246 break;
248 close_first_dso();
249 } while (1);
251 return -1;
254 static int __open_dso(struct dso *dso, struct machine *machine)
256 int fd;
257 char *root_dir = (char *)"";
258 char *name = malloc(PATH_MAX);
260 if (!name)
261 return -ENOMEM;
263 if (machine)
264 root_dir = machine->root_dir;
266 if (dso__read_binary_type_filename(dso, dso->binary_type,
267 root_dir, name, PATH_MAX)) {
268 free(name);
269 return -EINVAL;
272 fd = do_open(name);
273 free(name);
274 return fd;
277 static void check_data_close(void);
280 * dso_close - Open DSO data file
281 * @dso: dso object
283 * Open @dso's data file descriptor and updates
284 * list/count of open DSO objects.
286 static int open_dso(struct dso *dso, struct machine *machine)
288 int fd = __open_dso(dso, machine);
290 if (fd >= 0) {
291 dso__list_add(dso);
293 * Check if we crossed the allowed number
294 * of opened DSOs and close one if needed.
296 check_data_close();
299 return fd;
302 static void close_data_fd(struct dso *dso)
304 if (dso->data.fd >= 0) {
305 close(dso->data.fd);
306 dso->data.fd = -1;
307 dso->data.file_size = 0;
308 dso__list_del(dso);
313 * dso_close - Close DSO data file
314 * @dso: dso object
316 * Close @dso's data file descriptor and updates
317 * list/count of open DSO objects.
319 static void close_dso(struct dso *dso)
321 close_data_fd(dso);
324 static void close_first_dso(void)
326 struct dso *dso;
328 dso = list_first_entry(&dso__data_open, struct dso, data.open_entry);
329 close_dso(dso);
332 static rlim_t get_fd_limit(void)
334 struct rlimit l;
335 rlim_t limit = 0;
337 /* Allow half of the current open fd limit. */
338 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
339 if (l.rlim_cur == RLIM_INFINITY)
340 limit = l.rlim_cur;
341 else
342 limit = l.rlim_cur / 2;
343 } else {
344 pr_err("failed to get fd limit\n");
345 limit = 1;
348 return limit;
351 static bool may_cache_fd(void)
353 static rlim_t limit;
355 if (!limit)
356 limit = get_fd_limit();
358 if (limit == RLIM_INFINITY)
359 return true;
361 return limit > (rlim_t) dso__data_open_cnt;
365 * Check and close LRU dso if we crossed allowed limit
366 * for opened dso file descriptors. The limit is half
367 * of the RLIMIT_NOFILE files opened.
369 static void check_data_close(void)
371 bool cache_fd = may_cache_fd();
373 if (!cache_fd)
374 close_first_dso();
378 * dso__data_close - Close DSO data file
379 * @dso: dso object
381 * External interface to close @dso's data file descriptor.
383 void dso__data_close(struct dso *dso)
385 close_dso(dso);
389 * dso__data_fd - Get dso's data file descriptor
390 * @dso: dso object
391 * @machine: machine object
393 * External interface to find dso's file, open it and
394 * returns file descriptor.
396 int dso__data_fd(struct dso *dso, struct machine *machine)
398 enum dso_binary_type binary_type_data[] = {
399 DSO_BINARY_TYPE__BUILD_ID_CACHE,
400 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
401 DSO_BINARY_TYPE__NOT_FOUND,
403 int i = 0;
405 if (dso->data.status == DSO_DATA_STATUS_ERROR)
406 return -1;
408 if (dso->data.fd >= 0)
409 goto out;
411 if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) {
412 dso->data.fd = open_dso(dso, machine);
413 goto out;
416 do {
417 dso->binary_type = binary_type_data[i++];
419 dso->data.fd = open_dso(dso, machine);
420 if (dso->data.fd >= 0)
421 goto out;
423 } while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND);
424 out:
425 if (dso->data.fd >= 0)
426 dso->data.status = DSO_DATA_STATUS_OK;
427 else
428 dso->data.status = DSO_DATA_STATUS_ERROR;
430 return dso->data.fd;
433 bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by)
435 u32 flag = 1 << by;
437 if (dso->data.status_seen & flag)
438 return true;
440 dso->data.status_seen |= flag;
442 return false;
445 static void
446 dso_cache__free(struct rb_root *root)
448 struct rb_node *next = rb_first(root);
450 while (next) {
451 struct dso_cache *cache;
453 cache = rb_entry(next, struct dso_cache, rb_node);
454 next = rb_next(&cache->rb_node);
455 rb_erase(&cache->rb_node, root);
456 free(cache);
460 static struct dso_cache *dso_cache__find(const struct rb_root *root, u64 offset)
462 struct rb_node * const *p = &root->rb_node;
463 const struct rb_node *parent = NULL;
464 struct dso_cache *cache;
466 while (*p != NULL) {
467 u64 end;
469 parent = *p;
470 cache = rb_entry(parent, struct dso_cache, rb_node);
471 end = cache->offset + DSO__DATA_CACHE_SIZE;
473 if (offset < cache->offset)
474 p = &(*p)->rb_left;
475 else if (offset >= end)
476 p = &(*p)->rb_right;
477 else
478 return cache;
480 return NULL;
483 static void
484 dso_cache__insert(struct rb_root *root, struct dso_cache *new)
486 struct rb_node **p = &root->rb_node;
487 struct rb_node *parent = NULL;
488 struct dso_cache *cache;
489 u64 offset = new->offset;
491 while (*p != NULL) {
492 u64 end;
494 parent = *p;
495 cache = rb_entry(parent, struct dso_cache, rb_node);
496 end = cache->offset + DSO__DATA_CACHE_SIZE;
498 if (offset < cache->offset)
499 p = &(*p)->rb_left;
500 else if (offset >= end)
501 p = &(*p)->rb_right;
504 rb_link_node(&new->rb_node, parent, p);
505 rb_insert_color(&new->rb_node, root);
508 static ssize_t
509 dso_cache__memcpy(struct dso_cache *cache, u64 offset,
510 u8 *data, u64 size)
512 u64 cache_offset = offset - cache->offset;
513 u64 cache_size = min(cache->size - cache_offset, size);
515 memcpy(data, cache->data + cache_offset, cache_size);
516 return cache_size;
519 static ssize_t
520 dso_cache__read(struct dso *dso, u64 offset, u8 *data, ssize_t size)
522 struct dso_cache *cache;
523 ssize_t ret;
525 do {
526 u64 cache_offset;
528 ret = -ENOMEM;
530 cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
531 if (!cache)
532 break;
534 cache_offset = offset & DSO__DATA_CACHE_MASK;
535 ret = -EINVAL;
537 if (-1 == lseek(dso->data.fd, cache_offset, SEEK_SET))
538 break;
540 ret = read(dso->data.fd, cache->data, DSO__DATA_CACHE_SIZE);
541 if (ret <= 0)
542 break;
544 cache->offset = cache_offset;
545 cache->size = ret;
546 dso_cache__insert(&dso->data.cache, cache);
548 ret = dso_cache__memcpy(cache, offset, data, size);
550 } while (0);
552 if (ret <= 0)
553 free(cache);
555 return ret;
558 static ssize_t dso_cache_read(struct dso *dso, u64 offset,
559 u8 *data, ssize_t size)
561 struct dso_cache *cache;
563 cache = dso_cache__find(&dso->data.cache, offset);
564 if (cache)
565 return dso_cache__memcpy(cache, offset, data, size);
566 else
567 return dso_cache__read(dso, offset, data, size);
571 * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
572 * in the rb_tree. Any read to already cached data is served
573 * by cached data.
575 static ssize_t cached_read(struct dso *dso, u64 offset, u8 *data, ssize_t size)
577 ssize_t r = 0;
578 u8 *p = data;
580 do {
581 ssize_t ret;
583 ret = dso_cache_read(dso, offset, p, size);
584 if (ret < 0)
585 return ret;
587 /* Reached EOF, return what we have. */
588 if (!ret)
589 break;
591 BUG_ON(ret > size);
593 r += ret;
594 p += ret;
595 offset += ret;
596 size -= ret;
598 } while (size);
600 return r;
603 static int data_file_size(struct dso *dso)
605 struct stat st;
606 char sbuf[STRERR_BUFSIZE];
608 if (!dso->data.file_size) {
609 if (fstat(dso->data.fd, &st)) {
610 pr_err("dso mmap failed, fstat: %s\n",
611 strerror_r(errno, sbuf, sizeof(sbuf)));
612 return -1;
614 dso->data.file_size = st.st_size;
617 return 0;
621 * dso__data_size - Return dso data size
622 * @dso: dso object
623 * @machine: machine object
625 * Return: dso data size
627 off_t dso__data_size(struct dso *dso, struct machine *machine)
629 int fd;
631 fd = dso__data_fd(dso, machine);
632 if (fd < 0)
633 return fd;
635 if (data_file_size(dso))
636 return -1;
638 /* For now just estimate dso data size is close to file size */
639 return dso->data.file_size;
642 static ssize_t data_read_offset(struct dso *dso, u64 offset,
643 u8 *data, ssize_t size)
645 if (data_file_size(dso))
646 return -1;
648 /* Check the offset sanity. */
649 if (offset > dso->data.file_size)
650 return -1;
652 if (offset + size < offset)
653 return -1;
655 return cached_read(dso, offset, data, size);
659 * dso__data_read_offset - Read data from dso file offset
660 * @dso: dso object
661 * @machine: machine object
662 * @offset: file offset
663 * @data: buffer to store data
664 * @size: size of the @data buffer
666 * External interface to read data from dso file offset. Open
667 * dso data file and use cached_read to get the data.
669 ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
670 u64 offset, u8 *data, ssize_t size)
672 if (dso__data_fd(dso, machine) < 0)
673 return -1;
675 return data_read_offset(dso, offset, data, size);
679 * dso__data_read_addr - Read data from dso address
680 * @dso: dso object
681 * @machine: machine object
682 * @add: virtual memory address
683 * @data: buffer to store data
684 * @size: size of the @data buffer
686 * External interface to read data from dso address.
688 ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
689 struct machine *machine, u64 addr,
690 u8 *data, ssize_t size)
692 u64 offset = map->map_ip(map, addr);
693 return dso__data_read_offset(dso, machine, offset, data, size);
696 struct map *dso__new_map(const char *name)
698 struct map *map = NULL;
699 struct dso *dso = dso__new(name);
701 if (dso)
702 map = map__new2(0, dso, MAP__FUNCTION);
704 return map;
707 struct dso *dso__kernel_findnew(struct machine *machine, const char *name,
708 const char *short_name, int dso_type)
711 * The kernel dso could be created by build_id processing.
713 struct dso *dso = __dsos__findnew(&machine->kernel_dsos, name);
716 * We need to run this in all cases, since during the build_id
717 * processing we had no idea this was the kernel dso.
719 if (dso != NULL) {
720 dso__set_short_name(dso, short_name, false);
721 dso->kernel = dso_type;
724 return dso;
728 * Find a matching entry and/or link current entry to RB tree.
729 * Either one of the dso or name parameter must be non-NULL or the
730 * function will not work.
732 static struct dso *dso__findlink_by_longname(struct rb_root *root,
733 struct dso *dso, const char *name)
735 struct rb_node **p = &root->rb_node;
736 struct rb_node *parent = NULL;
738 if (!name)
739 name = dso->long_name;
741 * Find node with the matching name
743 while (*p) {
744 struct dso *this = rb_entry(*p, struct dso, rb_node);
745 int rc = strcmp(name, this->long_name);
747 parent = *p;
748 if (rc == 0) {
750 * In case the new DSO is a duplicate of an existing
751 * one, print an one-time warning & put the new entry
752 * at the end of the list of duplicates.
754 if (!dso || (dso == this))
755 return this; /* Find matching dso */
757 * The core kernel DSOs may have duplicated long name.
758 * In this case, the short name should be different.
759 * Comparing the short names to differentiate the DSOs.
761 rc = strcmp(dso->short_name, this->short_name);
762 if (rc == 0) {
763 pr_err("Duplicated dso name: %s\n", name);
764 return NULL;
767 if (rc < 0)
768 p = &parent->rb_left;
769 else
770 p = &parent->rb_right;
772 if (dso) {
773 /* Add new node and rebalance tree */
774 rb_link_node(&dso->rb_node, parent, p);
775 rb_insert_color(&dso->rb_node, root);
777 return NULL;
780 static inline struct dso *
781 dso__find_by_longname(const struct rb_root *root, const char *name)
783 return dso__findlink_by_longname((struct rb_root *)root, NULL, name);
786 void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
788 if (name == NULL)
789 return;
791 if (dso->long_name_allocated)
792 free((char *)dso->long_name);
794 dso->long_name = name;
795 dso->long_name_len = strlen(name);
796 dso->long_name_allocated = name_allocated;
799 void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
801 if (name == NULL)
802 return;
804 if (dso->short_name_allocated)
805 free((char *)dso->short_name);
807 dso->short_name = name;
808 dso->short_name_len = strlen(name);
809 dso->short_name_allocated = name_allocated;
812 static void dso__set_basename(struct dso *dso)
815 * basename() may modify path buffer, so we must pass
816 * a copy.
818 char *base, *lname = strdup(dso->long_name);
820 if (!lname)
821 return;
824 * basename() may return a pointer to internal
825 * storage which is reused in subsequent calls
826 * so copy the result.
828 base = strdup(basename(lname));
830 free(lname);
832 if (!base)
833 return;
835 dso__set_short_name(dso, base, true);
838 int dso__name_len(const struct dso *dso)
840 if (!dso)
841 return strlen("[unknown]");
842 if (verbose)
843 return dso->long_name_len;
845 return dso->short_name_len;
848 bool dso__loaded(const struct dso *dso, enum map_type type)
850 return dso->loaded & (1 << type);
853 bool dso__sorted_by_name(const struct dso *dso, enum map_type type)
855 return dso->sorted_by_name & (1 << type);
858 void dso__set_sorted_by_name(struct dso *dso, enum map_type type)
860 dso->sorted_by_name |= (1 << type);
863 struct dso *dso__new(const char *name)
865 struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
867 if (dso != NULL) {
868 int i;
869 strcpy(dso->name, name);
870 dso__set_long_name(dso, dso->name, false);
871 dso__set_short_name(dso, dso->name, false);
872 for (i = 0; i < MAP__NR_TYPES; ++i)
873 dso->symbols[i] = dso->symbol_names[i] = RB_ROOT;
874 dso->data.cache = RB_ROOT;
875 dso->data.fd = -1;
876 dso->data.status = DSO_DATA_STATUS_UNKNOWN;
877 dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
878 dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
879 dso->is_64_bit = (sizeof(void *) == 8);
880 dso->loaded = 0;
881 dso->rel = 0;
882 dso->sorted_by_name = 0;
883 dso->has_build_id = 0;
884 dso->has_srcline = 1;
885 dso->a2l_fails = 1;
886 dso->kernel = DSO_TYPE_USER;
887 dso->needs_swap = DSO_SWAP__UNSET;
888 RB_CLEAR_NODE(&dso->rb_node);
889 INIT_LIST_HEAD(&dso->node);
890 INIT_LIST_HEAD(&dso->data.open_entry);
893 return dso;
896 void dso__delete(struct dso *dso)
898 int i;
900 if (!RB_EMPTY_NODE(&dso->rb_node))
901 pr_err("DSO %s is still in rbtree when being deleted!\n",
902 dso->long_name);
903 for (i = 0; i < MAP__NR_TYPES; ++i)
904 symbols__delete(&dso->symbols[i]);
906 if (dso->short_name_allocated) {
907 zfree((char **)&dso->short_name);
908 dso->short_name_allocated = false;
911 if (dso->long_name_allocated) {
912 zfree((char **)&dso->long_name);
913 dso->long_name_allocated = false;
916 dso__data_close(dso);
917 dso_cache__free(&dso->data.cache);
918 dso__free_a2l(dso);
919 zfree(&dso->symsrc_filename);
920 free(dso);
923 void dso__set_build_id(struct dso *dso, void *build_id)
925 memcpy(dso->build_id, build_id, sizeof(dso->build_id));
926 dso->has_build_id = 1;
929 bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
931 return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
934 void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
936 char path[PATH_MAX];
938 if (machine__is_default_guest(machine))
939 return;
940 sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
941 if (sysfs__read_build_id(path, dso->build_id,
942 sizeof(dso->build_id)) == 0)
943 dso->has_build_id = true;
946 int dso__kernel_module_get_build_id(struct dso *dso,
947 const char *root_dir)
949 char filename[PATH_MAX];
951 * kernel module short names are of the form "[module]" and
952 * we need just "module" here.
954 const char *name = dso->short_name + 1;
956 snprintf(filename, sizeof(filename),
957 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
958 root_dir, (int)strlen(name) - 1, name);
960 if (sysfs__read_build_id(filename, dso->build_id,
961 sizeof(dso->build_id)) == 0)
962 dso->has_build_id = true;
964 return 0;
967 bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
969 bool have_build_id = false;
970 struct dso *pos;
972 list_for_each_entry(pos, head, node) {
973 if (with_hits && !pos->hit)
974 continue;
975 if (pos->has_build_id) {
976 have_build_id = true;
977 continue;
979 if (filename__read_build_id(pos->long_name, pos->build_id,
980 sizeof(pos->build_id)) > 0) {
981 have_build_id = true;
982 pos->has_build_id = true;
986 return have_build_id;
989 void dsos__add(struct dsos *dsos, struct dso *dso)
991 list_add_tail(&dso->node, &dsos->head);
992 dso__findlink_by_longname(&dsos->root, dso, NULL);
995 struct dso *dsos__find(const struct dsos *dsos, const char *name,
996 bool cmp_short)
998 struct dso *pos;
1000 if (cmp_short) {
1001 list_for_each_entry(pos, &dsos->head, node)
1002 if (strcmp(pos->short_name, name) == 0)
1003 return pos;
1004 return NULL;
1006 return dso__find_by_longname(&dsos->root, name);
1009 struct dso *__dsos__findnew(struct dsos *dsos, const char *name)
1011 struct dso *dso = dsos__find(dsos, name, false);
1013 if (!dso) {
1014 dso = dso__new(name);
1015 if (dso != NULL) {
1016 dsos__add(dsos, dso);
1017 dso__set_basename(dso);
1021 return dso;
1024 size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
1025 bool (skip)(struct dso *dso, int parm), int parm)
1027 struct dso *pos;
1028 size_t ret = 0;
1030 list_for_each_entry(pos, head, node) {
1031 if (skip && skip(pos, parm))
1032 continue;
1033 ret += dso__fprintf_buildid(pos, fp);
1034 ret += fprintf(fp, " %s\n", pos->long_name);
1036 return ret;
1039 size_t __dsos__fprintf(struct list_head *head, FILE *fp)
1041 struct dso *pos;
1042 size_t ret = 0;
1044 list_for_each_entry(pos, head, node) {
1045 int i;
1046 for (i = 0; i < MAP__NR_TYPES; ++i)
1047 ret += dso__fprintf(pos, i, fp);
1050 return ret;
1053 size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
1055 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1057 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1058 return fprintf(fp, "%s", sbuild_id);
1061 size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp)
1063 struct rb_node *nd;
1064 size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
1066 if (dso->short_name != dso->long_name)
1067 ret += fprintf(fp, "%s, ", dso->long_name);
1068 ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
1069 dso__loaded(dso, type) ? "" : "NOT ");
1070 ret += dso__fprintf_buildid(dso, fp);
1071 ret += fprintf(fp, ")\n");
1072 for (nd = rb_first(&dso->symbols[type]); nd; nd = rb_next(nd)) {
1073 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
1074 ret += symbol__fprintf(pos, fp);
1077 return ret;
1080 enum dso_type dso__type(struct dso *dso, struct machine *machine)
1082 int fd;
1084 fd = dso__data_fd(dso, machine);
1085 if (fd < 0)
1086 return DSO__TYPE_UNKNOWN;
1088 return dso__type_fd(fd);