perf tools: Get rid of dso__data_fd() from dso__data_size()
[linux-2.6/btrfs-unstable.git] / tools / perf / util / dso.c
blobe95e850dd832682409e62016797e8087d01ce0ae
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 "auxtrace.h"
8 #include "util.h"
9 #include "debug.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)
33 return '!';
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];
42 int ret = 0;
43 size_t len;
45 switch (type) {
46 case DSO_BINARY_TYPE__DEBUGLINK: {
47 char *debuglink;
49 len = __symbol__join_symfs(filename, size, dso->long_name);
50 debuglink = filename + len;
51 while (debuglink != filename && *debuglink != '/')
52 debuglink--;
53 if (*debuglink == '/')
54 debuglink++;
55 ret = filename__read_debuglink(filename, debuglink,
56 size - (debuglink - filename));
58 break;
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))
63 ret = -1;
64 break;
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);
69 break;
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);
74 break;
76 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
78 const char *last_slash;
79 size_t dir_size;
81 last_slash = dso->long_name + dso->long_name_len;
82 while (last_slash != dso->long_name && *last_slash != '/')
83 last_slash--;
85 len = __symbol__join_symfs(filename, size, "");
86 dir_size = last_slash - dso->long_name + 2;
87 if (dir_size > (size - len)) {
88 ret = -1;
89 break;
91 len += scnprintf(filename + len, dir_size, "%s", dso->long_name);
92 len += scnprintf(filename + len , size - len, ".debug%s",
93 last_slash);
94 break;
97 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
98 if (!dso->has_build_id) {
99 ret = -1;
100 break;
103 build_id__sprintf(dso->build_id,
104 sizeof(dso->build_id),
105 build_id_hex);
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);
109 break;
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);
115 break;
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);
121 break;
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);
126 break;
128 case DSO_BINARY_TYPE__KCORE:
129 case DSO_BINARY_TYPE__GUEST_KCORE:
130 snprintf(filename, size, "%s", dso->long_name);
131 break;
133 default:
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:
138 ret = -1;
139 break;
142 return ret;
145 static const struct {
146 const char *fmt;
147 int (*decompress)(const char *input, int output);
148 } compressions[] = {
149 #ifdef HAVE_ZLIB_SUPPORT
150 { "gz", gzip_decompress_to_file },
151 #endif
152 #ifdef HAVE_LZMA_SUPPORT
153 { "xz", lzma_decompress_to_file },
154 #endif
155 { NULL, NULL },
158 bool is_supported_compression(const char *ext)
160 unsigned i;
162 for (i = 0; compressions[i].fmt; i++) {
163 if (!strcmp(ext, compressions[i].fmt))
164 return true;
166 return false;
169 bool is_kernel_module(const char *pathname)
171 struct kmod_path m;
173 if (kmod_path__parse(&m, pathname))
174 return NULL;
176 return m.kmod;
179 bool decompress_to_file(const char *ext, const char *filename, int output_fd)
181 unsigned i;
183 for (i = 0; compressions[i].fmt; i++) {
184 if (!strcmp(ext, compressions[i].fmt))
185 return !compressions[i].decompress(filename,
186 output_fd);
188 return false;
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
199 * @m argument like:
201 * @comp - true if @path contains supported compression suffix,
202 * false otherwise
203 * @kmod - true if @path contains '.ko' suffix in right position,
204 * false otherwise
205 * @name - if (@alloc_name && @kmod) is true, it contains strdup-ed base name
206 * of the kernel module without suffixes, otherwise strudup-ed
207 * base name of @path
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. */
223 if (ext == NULL) {
224 if (alloc_name) {
225 m->name = strdup(name);
226 return m->name ? 0 : -ENOMEM;
228 return 0;
231 if (is_supported_compression(ext + 1)) {
232 m->comp = true;
233 ext -= 3;
236 /* Check .ko extension only if there's enough name left. */
237 if (ext > name)
238 m->kmod = !strncmp(ext, ".ko", 3);
240 if (alloc_name) {
241 if (m->kmod) {
242 if (asprintf(&m->name, "[%.*s]", (int) (ext - name), name) == -1)
243 return -ENOMEM;
244 } else {
245 if (asprintf(&m->name, "%s", name) == -1)
246 return -ENOMEM;
249 strxfrchar(m->name, '-', '_');
252 if (alloc_ext && m->comp) {
253 m->ext = strdup(ext + 4);
254 if (!m->ext) {
255 free((void *) m->name);
256 return -ENOMEM;
260 return 0;
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)
288 int fd;
289 char sbuf[STRERR_BUFSIZE];
291 do {
292 fd = open(name, O_RDONLY);
293 if (fd >= 0)
294 return fd;
296 pr_debug("dso open failed: %s\n",
297 strerror_r(errno, sbuf, sizeof(sbuf)));
298 if (!dso__data_open_cnt || errno != EMFILE)
299 break;
301 close_first_dso();
302 } while (1);
304 return -1;
307 static int __open_dso(struct dso *dso, struct machine *machine)
309 int fd;
310 char *root_dir = (char *)"";
311 char *name = malloc(PATH_MAX);
313 if (!name)
314 return -ENOMEM;
316 if (machine)
317 root_dir = machine->root_dir;
319 if (dso__read_binary_type_filename(dso, dso->binary_type,
320 root_dir, name, PATH_MAX)) {
321 free(name);
322 return -EINVAL;
325 fd = do_open(name);
326 free(name);
327 return fd;
330 static void check_data_close(void);
333 * dso_close - Open DSO data file
334 * @dso: dso object
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);
343 if (fd >= 0) {
344 dso__list_add(dso);
346 * Check if we crossed the allowed number
347 * of opened DSOs and close one if needed.
349 check_data_close();
352 return fd;
355 static void close_data_fd(struct dso *dso)
357 if (dso->data.fd >= 0) {
358 close(dso->data.fd);
359 dso->data.fd = -1;
360 dso->data.file_size = 0;
361 dso__list_del(dso);
366 * dso_close - Close DSO data file
367 * @dso: dso object
369 * Close @dso's data file descriptor and updates
370 * list/count of open DSO objects.
372 static void close_dso(struct dso *dso)
374 close_data_fd(dso);
377 static void close_first_dso(void)
379 struct dso *dso;
381 dso = list_first_entry(&dso__data_open, struct dso, data.open_entry);
382 close_dso(dso);
385 static rlim_t get_fd_limit(void)
387 struct rlimit l;
388 rlim_t limit = 0;
390 /* Allow half of the current open fd limit. */
391 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
392 if (l.rlim_cur == RLIM_INFINITY)
393 limit = l.rlim_cur;
394 else
395 limit = l.rlim_cur / 2;
396 } else {
397 pr_err("failed to get fd limit\n");
398 limit = 1;
401 return limit;
404 static bool may_cache_fd(void)
406 static rlim_t limit;
408 if (!limit)
409 limit = get_fd_limit();
411 if (limit == RLIM_INFINITY)
412 return true;
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();
426 if (!cache_fd)
427 close_first_dso();
431 * dso__data_close - Close DSO data file
432 * @dso: dso object
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);
439 close_dso(dso);
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,
450 int i = 0;
452 if (dso->data.fd >= 0)
453 return;
455 if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) {
456 dso->data.fd = open_dso(dso, machine);
457 goto out;
460 do {
461 dso->binary_type = binary_type_data[i++];
463 dso->data.fd = open_dso(dso, machine);
464 if (dso->data.fd >= 0)
465 goto out;
467 } while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND);
468 out:
469 if (dso->data.fd >= 0)
470 dso->data.status = DSO_DATA_STATUS_OK;
471 else
472 dso->data.status = DSO_DATA_STATUS_ERROR;
476 * dso__data_fd - Get dso's data file descriptor
477 * @dso: dso object
478 * @machine: machine object
480 * External interface to find dso's file, open it and
481 * returns file descriptor.
483 int dso__data_fd(struct dso *dso, struct machine *machine)
485 if (dso->data.status == DSO_DATA_STATUS_ERROR)
486 return -1;
488 pthread_mutex_lock(&dso__data_open_lock);
489 try_to_open_dso(dso, machine);
490 pthread_mutex_unlock(&dso__data_open_lock);
492 return dso->data.fd;
495 bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by)
497 u32 flag = 1 << by;
499 if (dso->data.status_seen & flag)
500 return true;
502 dso->data.status_seen |= flag;
504 return false;
507 static void
508 dso_cache__free(struct dso *dso)
510 struct rb_root *root = &dso->data.cache;
511 struct rb_node *next = rb_first(root);
513 pthread_mutex_lock(&dso->lock);
514 while (next) {
515 struct dso_cache *cache;
517 cache = rb_entry(next, struct dso_cache, rb_node);
518 next = rb_next(&cache->rb_node);
519 rb_erase(&cache->rb_node, root);
520 free(cache);
522 pthread_mutex_unlock(&dso->lock);
525 static struct dso_cache *dso_cache__find(struct dso *dso, u64 offset)
527 const struct rb_root *root = &dso->data.cache;
528 struct rb_node * const *p = &root->rb_node;
529 const struct rb_node *parent = NULL;
530 struct dso_cache *cache;
532 while (*p != NULL) {
533 u64 end;
535 parent = *p;
536 cache = rb_entry(parent, struct dso_cache, rb_node);
537 end = cache->offset + DSO__DATA_CACHE_SIZE;
539 if (offset < cache->offset)
540 p = &(*p)->rb_left;
541 else if (offset >= end)
542 p = &(*p)->rb_right;
543 else
544 return cache;
547 return NULL;
550 static struct dso_cache *
551 dso_cache__insert(struct dso *dso, struct dso_cache *new)
553 struct rb_root *root = &dso->data.cache;
554 struct rb_node **p = &root->rb_node;
555 struct rb_node *parent = NULL;
556 struct dso_cache *cache;
557 u64 offset = new->offset;
559 pthread_mutex_lock(&dso->lock);
560 while (*p != NULL) {
561 u64 end;
563 parent = *p;
564 cache = rb_entry(parent, struct dso_cache, rb_node);
565 end = cache->offset + DSO__DATA_CACHE_SIZE;
567 if (offset < cache->offset)
568 p = &(*p)->rb_left;
569 else if (offset >= end)
570 p = &(*p)->rb_right;
571 else
572 goto out;
575 rb_link_node(&new->rb_node, parent, p);
576 rb_insert_color(&new->rb_node, root);
578 cache = NULL;
579 out:
580 pthread_mutex_unlock(&dso->lock);
581 return cache;
584 static ssize_t
585 dso_cache__memcpy(struct dso_cache *cache, u64 offset,
586 u8 *data, u64 size)
588 u64 cache_offset = offset - cache->offset;
589 u64 cache_size = min(cache->size - cache_offset, size);
591 memcpy(data, cache->data + cache_offset, cache_size);
592 return cache_size;
595 static ssize_t
596 dso_cache__read(struct dso *dso, struct machine *machine,
597 u64 offset, u8 *data, ssize_t size)
599 struct dso_cache *cache;
600 struct dso_cache *old;
601 ssize_t ret;
603 do {
604 u64 cache_offset;
606 cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
607 if (!cache)
608 return -ENOMEM;
610 pthread_mutex_lock(&dso__data_open_lock);
613 * dso->data.fd might be closed if other thread opened another
614 * file (dso) due to open file limit (RLIMIT_NOFILE).
616 try_to_open_dso(dso, machine);
618 if (dso->data.fd < 0) {
619 ret = -errno;
620 dso->data.status = DSO_DATA_STATUS_ERROR;
621 break;
624 cache_offset = offset & DSO__DATA_CACHE_MASK;
626 ret = pread(dso->data.fd, cache->data, DSO__DATA_CACHE_SIZE, cache_offset);
627 if (ret <= 0)
628 break;
630 cache->offset = cache_offset;
631 cache->size = ret;
632 } while (0);
634 pthread_mutex_unlock(&dso__data_open_lock);
636 if (ret > 0) {
637 old = dso_cache__insert(dso, cache);
638 if (old) {
639 /* we lose the race */
640 free(cache);
641 cache = old;
644 ret = dso_cache__memcpy(cache, offset, data, size);
647 if (ret <= 0)
648 free(cache);
650 return ret;
653 static ssize_t dso_cache_read(struct dso *dso, struct machine *machine,
654 u64 offset, u8 *data, ssize_t size)
656 struct dso_cache *cache;
658 cache = dso_cache__find(dso, offset);
659 if (cache)
660 return dso_cache__memcpy(cache, offset, data, size);
661 else
662 return dso_cache__read(dso, machine, offset, data, size);
666 * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
667 * in the rb_tree. Any read to already cached data is served
668 * by cached data.
670 static ssize_t cached_read(struct dso *dso, struct machine *machine,
671 u64 offset, u8 *data, ssize_t size)
673 ssize_t r = 0;
674 u8 *p = data;
676 do {
677 ssize_t ret;
679 ret = dso_cache_read(dso, machine, offset, p, size);
680 if (ret < 0)
681 return ret;
683 /* Reached EOF, return what we have. */
684 if (!ret)
685 break;
687 BUG_ON(ret > size);
689 r += ret;
690 p += ret;
691 offset += ret;
692 size -= ret;
694 } while (size);
696 return r;
699 static int data_file_size(struct dso *dso, struct machine *machine)
701 int ret = 0;
702 struct stat st;
703 char sbuf[STRERR_BUFSIZE];
705 if (dso->data.file_size)
706 return 0;
708 if (dso->data.status == DSO_DATA_STATUS_ERROR)
709 return -1;
711 pthread_mutex_lock(&dso__data_open_lock);
714 * dso->data.fd might be closed if other thread opened another
715 * file (dso) due to open file limit (RLIMIT_NOFILE).
717 try_to_open_dso(dso, machine);
719 if (dso->data.fd < 0) {
720 ret = -errno;
721 dso->data.status = DSO_DATA_STATUS_ERROR;
722 goto out;
725 if (fstat(dso->data.fd, &st) < 0) {
726 ret = -errno;
727 pr_err("dso cache fstat failed: %s\n",
728 strerror_r(errno, sbuf, sizeof(sbuf)));
729 dso->data.status = DSO_DATA_STATUS_ERROR;
730 goto out;
732 dso->data.file_size = st.st_size;
734 out:
735 pthread_mutex_unlock(&dso__data_open_lock);
736 return ret;
740 * dso__data_size - Return dso data size
741 * @dso: dso object
742 * @machine: machine object
744 * Return: dso data size
746 off_t dso__data_size(struct dso *dso, struct machine *machine)
748 if (data_file_size(dso, machine))
749 return -1;
751 /* For now just estimate dso data size is close to file size */
752 return dso->data.file_size;
755 static ssize_t data_read_offset(struct dso *dso, struct machine *machine,
756 u64 offset, u8 *data, ssize_t size)
758 if (data_file_size(dso, machine))
759 return -1;
761 /* Check the offset sanity. */
762 if (offset > dso->data.file_size)
763 return -1;
765 if (offset + size < offset)
766 return -1;
768 return cached_read(dso, machine, offset, data, size);
772 * dso__data_read_offset - Read data from dso file offset
773 * @dso: dso object
774 * @machine: machine object
775 * @offset: file offset
776 * @data: buffer to store data
777 * @size: size of the @data buffer
779 * External interface to read data from dso file offset. Open
780 * dso data file and use cached_read to get the data.
782 ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
783 u64 offset, u8 *data, ssize_t size)
785 if (dso->data.status == DSO_DATA_STATUS_ERROR)
786 return -1;
788 return data_read_offset(dso, machine, offset, data, size);
792 * dso__data_read_addr - Read data from dso address
793 * @dso: dso object
794 * @machine: machine object
795 * @add: virtual memory address
796 * @data: buffer to store data
797 * @size: size of the @data buffer
799 * External interface to read data from dso address.
801 ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
802 struct machine *machine, u64 addr,
803 u8 *data, ssize_t size)
805 u64 offset = map->map_ip(map, addr);
806 return dso__data_read_offset(dso, machine, offset, data, size);
809 struct map *dso__new_map(const char *name)
811 struct map *map = NULL;
812 struct dso *dso = dso__new(name);
814 if (dso)
815 map = map__new2(0, dso, MAP__FUNCTION);
817 return map;
820 struct dso *dso__kernel_findnew(struct machine *machine, const char *name,
821 const char *short_name, int dso_type)
824 * The kernel dso could be created by build_id processing.
826 struct dso *dso = __dsos__findnew(&machine->kernel_dsos, name);
829 * We need to run this in all cases, since during the build_id
830 * processing we had no idea this was the kernel dso.
832 if (dso != NULL) {
833 dso__set_short_name(dso, short_name, false);
834 dso->kernel = dso_type;
837 return dso;
841 * Find a matching entry and/or link current entry to RB tree.
842 * Either one of the dso or name parameter must be non-NULL or the
843 * function will not work.
845 static struct dso *dso__findlink_by_longname(struct rb_root *root,
846 struct dso *dso, const char *name)
848 struct rb_node **p = &root->rb_node;
849 struct rb_node *parent = NULL;
851 if (!name)
852 name = dso->long_name;
854 * Find node with the matching name
856 while (*p) {
857 struct dso *this = rb_entry(*p, struct dso, rb_node);
858 int rc = strcmp(name, this->long_name);
860 parent = *p;
861 if (rc == 0) {
863 * In case the new DSO is a duplicate of an existing
864 * one, print an one-time warning & put the new entry
865 * at the end of the list of duplicates.
867 if (!dso || (dso == this))
868 return this; /* Find matching dso */
870 * The core kernel DSOs may have duplicated long name.
871 * In this case, the short name should be different.
872 * Comparing the short names to differentiate the DSOs.
874 rc = strcmp(dso->short_name, this->short_name);
875 if (rc == 0) {
876 pr_err("Duplicated dso name: %s\n", name);
877 return NULL;
880 if (rc < 0)
881 p = &parent->rb_left;
882 else
883 p = &parent->rb_right;
885 if (dso) {
886 /* Add new node and rebalance tree */
887 rb_link_node(&dso->rb_node, parent, p);
888 rb_insert_color(&dso->rb_node, root);
890 return NULL;
893 static inline struct dso *
894 dso__find_by_longname(const struct rb_root *root, const char *name)
896 return dso__findlink_by_longname((struct rb_root *)root, NULL, name);
899 void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
901 if (name == NULL)
902 return;
904 if (dso->long_name_allocated)
905 free((char *)dso->long_name);
907 dso->long_name = name;
908 dso->long_name_len = strlen(name);
909 dso->long_name_allocated = name_allocated;
912 void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
914 if (name == NULL)
915 return;
917 if (dso->short_name_allocated)
918 free((char *)dso->short_name);
920 dso->short_name = name;
921 dso->short_name_len = strlen(name);
922 dso->short_name_allocated = name_allocated;
925 static void dso__set_basename(struct dso *dso)
928 * basename() may modify path buffer, so we must pass
929 * a copy.
931 char *base, *lname = strdup(dso->long_name);
933 if (!lname)
934 return;
937 * basename() may return a pointer to internal
938 * storage which is reused in subsequent calls
939 * so copy the result.
941 base = strdup(basename(lname));
943 free(lname);
945 if (!base)
946 return;
948 dso__set_short_name(dso, base, true);
951 int dso__name_len(const struct dso *dso)
953 if (!dso)
954 return strlen("[unknown]");
955 if (verbose)
956 return dso->long_name_len;
958 return dso->short_name_len;
961 bool dso__loaded(const struct dso *dso, enum map_type type)
963 return dso->loaded & (1 << type);
966 bool dso__sorted_by_name(const struct dso *dso, enum map_type type)
968 return dso->sorted_by_name & (1 << type);
971 void dso__set_sorted_by_name(struct dso *dso, enum map_type type)
973 dso->sorted_by_name |= (1 << type);
976 struct dso *dso__new(const char *name)
978 struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
980 if (dso != NULL) {
981 int i;
982 strcpy(dso->name, name);
983 dso__set_long_name(dso, dso->name, false);
984 dso__set_short_name(dso, dso->name, false);
985 for (i = 0; i < MAP__NR_TYPES; ++i)
986 dso->symbols[i] = dso->symbol_names[i] = RB_ROOT;
987 dso->data.cache = RB_ROOT;
988 dso->data.fd = -1;
989 dso->data.status = DSO_DATA_STATUS_UNKNOWN;
990 dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
991 dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
992 dso->is_64_bit = (sizeof(void *) == 8);
993 dso->loaded = 0;
994 dso->rel = 0;
995 dso->sorted_by_name = 0;
996 dso->has_build_id = 0;
997 dso->has_srcline = 1;
998 dso->a2l_fails = 1;
999 dso->kernel = DSO_TYPE_USER;
1000 dso->needs_swap = DSO_SWAP__UNSET;
1001 RB_CLEAR_NODE(&dso->rb_node);
1002 INIT_LIST_HEAD(&dso->node);
1003 INIT_LIST_HEAD(&dso->data.open_entry);
1004 pthread_mutex_init(&dso->lock, NULL);
1007 return dso;
1010 void dso__delete(struct dso *dso)
1012 int i;
1014 if (!RB_EMPTY_NODE(&dso->rb_node))
1015 pr_err("DSO %s is still in rbtree when being deleted!\n",
1016 dso->long_name);
1017 for (i = 0; i < MAP__NR_TYPES; ++i)
1018 symbols__delete(&dso->symbols[i]);
1020 if (dso->short_name_allocated) {
1021 zfree((char **)&dso->short_name);
1022 dso->short_name_allocated = false;
1025 if (dso->long_name_allocated) {
1026 zfree((char **)&dso->long_name);
1027 dso->long_name_allocated = false;
1030 dso__data_close(dso);
1031 auxtrace_cache__free(dso->auxtrace_cache);
1032 dso_cache__free(dso);
1033 dso__free_a2l(dso);
1034 zfree(&dso->symsrc_filename);
1035 pthread_mutex_destroy(&dso->lock);
1036 free(dso);
1039 void dso__set_build_id(struct dso *dso, void *build_id)
1041 memcpy(dso->build_id, build_id, sizeof(dso->build_id));
1042 dso->has_build_id = 1;
1045 bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
1047 return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
1050 void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
1052 char path[PATH_MAX];
1054 if (machine__is_default_guest(machine))
1055 return;
1056 sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
1057 if (sysfs__read_build_id(path, dso->build_id,
1058 sizeof(dso->build_id)) == 0)
1059 dso->has_build_id = true;
1062 int dso__kernel_module_get_build_id(struct dso *dso,
1063 const char *root_dir)
1065 char filename[PATH_MAX];
1067 * kernel module short names are of the form "[module]" and
1068 * we need just "module" here.
1070 const char *name = dso->short_name + 1;
1072 snprintf(filename, sizeof(filename),
1073 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1074 root_dir, (int)strlen(name) - 1, name);
1076 if (sysfs__read_build_id(filename, dso->build_id,
1077 sizeof(dso->build_id)) == 0)
1078 dso->has_build_id = true;
1080 return 0;
1083 bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
1085 bool have_build_id = false;
1086 struct dso *pos;
1088 list_for_each_entry(pos, head, node) {
1089 if (with_hits && !pos->hit)
1090 continue;
1091 if (pos->has_build_id) {
1092 have_build_id = true;
1093 continue;
1095 if (filename__read_build_id(pos->long_name, pos->build_id,
1096 sizeof(pos->build_id)) > 0) {
1097 have_build_id = true;
1098 pos->has_build_id = true;
1102 return have_build_id;
1105 void dsos__add(struct dsos *dsos, struct dso *dso)
1107 list_add_tail(&dso->node, &dsos->head);
1108 dso__findlink_by_longname(&dsos->root, dso, NULL);
1111 struct dso *dsos__find(const struct dsos *dsos, const char *name,
1112 bool cmp_short)
1114 struct dso *pos;
1116 if (cmp_short) {
1117 list_for_each_entry(pos, &dsos->head, node)
1118 if (strcmp(pos->short_name, name) == 0)
1119 return pos;
1120 return NULL;
1122 return dso__find_by_longname(&dsos->root, name);
1125 struct dso *dsos__addnew(struct dsos *dsos, const char *name)
1127 struct dso *dso = dso__new(name);
1129 if (dso != NULL) {
1130 dsos__add(dsos, dso);
1131 dso__set_basename(dso);
1133 return dso;
1136 struct dso *__dsos__findnew(struct dsos *dsos, const char *name)
1138 struct dso *dso = dsos__find(dsos, name, false);
1140 return dso ? dso : dsos__addnew(dsos, name);
1143 size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
1144 bool (skip)(struct dso *dso, int parm), int parm)
1146 struct dso *pos;
1147 size_t ret = 0;
1149 list_for_each_entry(pos, head, node) {
1150 if (skip && skip(pos, parm))
1151 continue;
1152 ret += dso__fprintf_buildid(pos, fp);
1153 ret += fprintf(fp, " %s\n", pos->long_name);
1155 return ret;
1158 size_t __dsos__fprintf(struct list_head *head, FILE *fp)
1160 struct dso *pos;
1161 size_t ret = 0;
1163 list_for_each_entry(pos, head, node) {
1164 int i;
1165 for (i = 0; i < MAP__NR_TYPES; ++i)
1166 ret += dso__fprintf(pos, i, fp);
1169 return ret;
1172 size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
1174 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1176 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1177 return fprintf(fp, "%s", sbuild_id);
1180 size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp)
1182 struct rb_node *nd;
1183 size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
1185 if (dso->short_name != dso->long_name)
1186 ret += fprintf(fp, "%s, ", dso->long_name);
1187 ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
1188 dso__loaded(dso, type) ? "" : "NOT ");
1189 ret += dso__fprintf_buildid(dso, fp);
1190 ret += fprintf(fp, ")\n");
1191 for (nd = rb_first(&dso->symbols[type]); nd; nd = rb_next(nd)) {
1192 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
1193 ret += symbol__fprintf(pos, fp);
1196 return ret;
1199 enum dso_type dso__type(struct dso *dso, struct machine *machine)
1201 int fd;
1203 fd = dso__data_fd(dso, machine);
1204 if (fd < 0)
1205 return DSO__TYPE_UNKNOWN;
1207 return dso__type_fd(fd);
1210 int dso__strerror_load(struct dso *dso, char *buf, size_t buflen)
1212 int idx, errnum = dso->load_errno;
1214 * This must have a same ordering as the enum dso_load_errno.
1216 static const char *dso_load__error_str[] = {
1217 "Internal tools/perf/ library error",
1218 "Invalid ELF file",
1219 "Can not read build id",
1220 "Mismatching build id",
1221 "Decompression failure",
1224 BUG_ON(buflen == 0);
1226 if (errnum >= 0) {
1227 const char *err = strerror_r(errnum, buf, buflen);
1229 if (err != buf)
1230 scnprintf(buf, buflen, "%s", err);
1232 return 0;
1235 if (errnum < __DSO_LOAD_ERRNO__START || errnum >= __DSO_LOAD_ERRNO__END)
1236 return -1;
1238 idx = errnum - __DSO_LOAD_ERRNO__START;
1239 scnprintf(buf, buflen, "%s", dso_load__error_str[idx]);
1240 return 0;