Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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"
10#include "vdso.h"
11
12static const char * const debuglink_paths[] = {
13 "%.0s%s",
14 "%s/%s",
15 "%s/.debug/%s",
16 "/usr/lib/debug%s/%s"
17};
18
19char dso__symtab_origin(const struct dso *dso)
20{
21 static const char origin[] = {
22 [DSO_BINARY_TYPE__KALLSYMS] = 'k',
23 [DSO_BINARY_TYPE__VMLINUX] = 'v',
24 [DSO_BINARY_TYPE__JAVA_JIT] = 'j',
25 [DSO_BINARY_TYPE__DEBUGLINK] = 'l',
26 [DSO_BINARY_TYPE__BUILD_ID_CACHE] = 'B',
27 [DSO_BINARY_TYPE__FEDORA_DEBUGINFO] = 'f',
28 [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO] = 'u',
29 [DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO] = 'o',
30 [DSO_BINARY_TYPE__BUILDID_DEBUGINFO] = 'b',
31 [DSO_BINARY_TYPE__SYSTEM_PATH_DSO] = 'd',
32 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE] = 'K',
33 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP] = 'm',
34 [DSO_BINARY_TYPE__GUEST_KALLSYMS] = 'g',
35 [DSO_BINARY_TYPE__GUEST_KMODULE] = 'G',
36 [DSO_BINARY_TYPE__GUEST_KMODULE_COMP] = 'M',
37 [DSO_BINARY_TYPE__GUEST_VMLINUX] = 'V',
38 };
39
40 if (dso == NULL || dso->symtab_type == DSO_BINARY_TYPE__NOT_FOUND)
41 return '!';
42 return origin[dso->symtab_type];
43}
44
45int dso__read_binary_type_filename(const struct dso *dso,
46 enum dso_binary_type type,
47 char *root_dir, char *filename, size_t size)
48{
49 char build_id_hex[SBUILD_ID_SIZE];
50 int ret = 0;
51 size_t len;
52
53 switch (type) {
54 case DSO_BINARY_TYPE__DEBUGLINK:
55 {
56 const char *last_slash;
57 char dso_dir[PATH_MAX];
58 char symfile[PATH_MAX];
59 unsigned int i;
60
61 len = __symbol__join_symfs(filename, size, dso->long_name);
62 last_slash = filename + len;
63 while (last_slash != filename && *last_slash != '/')
64 last_slash--;
65
66 strncpy(dso_dir, filename, last_slash - filename);
67 dso_dir[last_slash-filename] = '\0';
68
69 if (!is_regular_file(filename)) {
70 ret = -1;
71 break;
72 }
73
74 ret = filename__read_debuglink(filename, symfile, PATH_MAX);
75 if (ret)
76 break;
77
78 /* Check predefined locations where debug file might reside */
79 ret = -1;
80 for (i = 0; i < ARRAY_SIZE(debuglink_paths); i++) {
81 snprintf(filename, size,
82 debuglink_paths[i], dso_dir, symfile);
83 if (is_regular_file(filename)) {
84 ret = 0;
85 break;
86 }
87 }
88
89 break;
90 }
91 case DSO_BINARY_TYPE__BUILD_ID_CACHE:
92 if (dso__build_id_filename(dso, filename, size) == NULL)
93 ret = -1;
94 break;
95
96 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
97 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
98 snprintf(filename + len, size - len, "%s.debug", dso->long_name);
99 break;
100
101 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
102 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
103 snprintf(filename + len, size - len, "%s", dso->long_name);
104 break;
105
106 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
107 {
108 const char *last_slash;
109 size_t dir_size;
110
111 last_slash = dso->long_name + dso->long_name_len;
112 while (last_slash != dso->long_name && *last_slash != '/')
113 last_slash--;
114
115 len = __symbol__join_symfs(filename, size, "");
116 dir_size = last_slash - dso->long_name + 2;
117 if (dir_size > (size - len)) {
118 ret = -1;
119 break;
120 }
121 len += scnprintf(filename + len, dir_size, "%s", dso->long_name);
122 len += scnprintf(filename + len , size - len, ".debug%s",
123 last_slash);
124 break;
125 }
126
127 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
128 if (!dso->has_build_id) {
129 ret = -1;
130 break;
131 }
132
133 build_id__sprintf(dso->build_id,
134 sizeof(dso->build_id),
135 build_id_hex);
136 len = __symbol__join_symfs(filename, size, "/usr/lib/debug/.build-id/");
137 snprintf(filename + len, size - len, "%.2s/%s.debug",
138 build_id_hex, build_id_hex + 2);
139 break;
140
141 case DSO_BINARY_TYPE__VMLINUX:
142 case DSO_BINARY_TYPE__GUEST_VMLINUX:
143 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
144 __symbol__join_symfs(filename, size, dso->long_name);
145 break;
146
147 case DSO_BINARY_TYPE__GUEST_KMODULE:
148 case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
149 path__join3(filename, size, symbol_conf.symfs,
150 root_dir, dso->long_name);
151 break;
152
153 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
154 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
155 __symbol__join_symfs(filename, size, dso->long_name);
156 break;
157
158 case DSO_BINARY_TYPE__KCORE:
159 case DSO_BINARY_TYPE__GUEST_KCORE:
160 snprintf(filename, size, "%s", dso->long_name);
161 break;
162
163 default:
164 case DSO_BINARY_TYPE__KALLSYMS:
165 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
166 case DSO_BINARY_TYPE__JAVA_JIT:
167 case DSO_BINARY_TYPE__NOT_FOUND:
168 ret = -1;
169 break;
170 }
171
172 return ret;
173}
174
175static const struct {
176 const char *fmt;
177 int (*decompress)(const char *input, int output);
178} compressions[] = {
179#ifdef HAVE_ZLIB_SUPPORT
180 { "gz", gzip_decompress_to_file },
181#endif
182#ifdef HAVE_LZMA_SUPPORT
183 { "xz", lzma_decompress_to_file },
184#endif
185 { NULL, NULL },
186};
187
188bool is_supported_compression(const char *ext)
189{
190 unsigned i;
191
192 for (i = 0; compressions[i].fmt; i++) {
193 if (!strcmp(ext, compressions[i].fmt))
194 return true;
195 }
196 return false;
197}
198
199bool is_kernel_module(const char *pathname, int cpumode)
200{
201 struct kmod_path m;
202 int mode = cpumode & PERF_RECORD_MISC_CPUMODE_MASK;
203
204 WARN_ONCE(mode != cpumode,
205 "Internal error: passing unmasked cpumode (%x) to is_kernel_module",
206 cpumode);
207
208 switch (mode) {
209 case PERF_RECORD_MISC_USER:
210 case PERF_RECORD_MISC_HYPERVISOR:
211 case PERF_RECORD_MISC_GUEST_USER:
212 return false;
213 /* Treat PERF_RECORD_MISC_CPUMODE_UNKNOWN as kernel */
214 default:
215 if (kmod_path__parse(&m, pathname)) {
216 pr_err("Failed to check whether %s is a kernel module or not. Assume it is.",
217 pathname);
218 return true;
219 }
220 }
221
222 return m.kmod;
223}
224
225bool decompress_to_file(const char *ext, const char *filename, int output_fd)
226{
227 unsigned i;
228
229 for (i = 0; compressions[i].fmt; i++) {
230 if (!strcmp(ext, compressions[i].fmt))
231 return !compressions[i].decompress(filename,
232 output_fd);
233 }
234 return false;
235}
236
237bool dso__needs_decompress(struct dso *dso)
238{
239 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
240 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
241}
242
243/*
244 * Parses kernel module specified in @path and updates
245 * @m argument like:
246 *
247 * @comp - true if @path contains supported compression suffix,
248 * false otherwise
249 * @kmod - true if @path contains '.ko' suffix in right position,
250 * false otherwise
251 * @name - if (@alloc_name && @kmod) is true, it contains strdup-ed base name
252 * of the kernel module without suffixes, otherwise strudup-ed
253 * base name of @path
254 * @ext - if (@alloc_ext && @comp) is true, it contains strdup-ed string
255 * the compression suffix
256 *
257 * Returns 0 if there's no strdup error, -ENOMEM otherwise.
258 */
259int __kmod_path__parse(struct kmod_path *m, const char *path,
260 bool alloc_name, bool alloc_ext)
261{
262 const char *name = strrchr(path, '/');
263 const char *ext = strrchr(path, '.');
264 bool is_simple_name = false;
265
266 memset(m, 0x0, sizeof(*m));
267 name = name ? name + 1 : path;
268
269 /*
270 * '.' is also a valid character for module name. For example:
271 * [aaa.bbb] is a valid module name. '[' should have higher
272 * priority than '.ko' suffix.
273 *
274 * The kernel names are from machine__mmap_name. Such
275 * name should belong to kernel itself, not kernel module.
276 */
277 if (name[0] == '[') {
278 is_simple_name = true;
279 if ((strncmp(name, "[kernel.kallsyms]", 17) == 0) ||
280 (strncmp(name, "[guest.kernel.kallsyms", 22) == 0) ||
281 (strncmp(name, "[vdso]", 6) == 0) ||
282 (strncmp(name, "[vsyscall]", 10) == 0)) {
283 m->kmod = false;
284
285 } else
286 m->kmod = true;
287 }
288
289 /* No extension, just return name. */
290 if ((ext == NULL) || is_simple_name) {
291 if (alloc_name) {
292 m->name = strdup(name);
293 return m->name ? 0 : -ENOMEM;
294 }
295 return 0;
296 }
297
298 if (is_supported_compression(ext + 1)) {
299 m->comp = true;
300 ext -= 3;
301 }
302
303 /* Check .ko extension only if there's enough name left. */
304 if (ext > name)
305 m->kmod = !strncmp(ext, ".ko", 3);
306
307 if (alloc_name) {
308 if (m->kmod) {
309 if (asprintf(&m->name, "[%.*s]", (int) (ext - name), name) == -1)
310 return -ENOMEM;
311 } else {
312 if (asprintf(&m->name, "%s", name) == -1)
313 return -ENOMEM;
314 }
315
316 strxfrchar(m->name, '-', '_');
317 }
318
319 if (alloc_ext && m->comp) {
320 m->ext = strdup(ext + 4);
321 if (!m->ext) {
322 free((void *) m->name);
323 return -ENOMEM;
324 }
325 }
326
327 return 0;
328}
329
330/*
331 * Global list of open DSOs and the counter.
332 */
333static LIST_HEAD(dso__data_open);
334static long dso__data_open_cnt;
335static pthread_mutex_t dso__data_open_lock = PTHREAD_MUTEX_INITIALIZER;
336
337static void dso__list_add(struct dso *dso)
338{
339 list_add_tail(&dso->data.open_entry, &dso__data_open);
340 dso__data_open_cnt++;
341}
342
343static void dso__list_del(struct dso *dso)
344{
345 list_del(&dso->data.open_entry);
346 WARN_ONCE(dso__data_open_cnt <= 0,
347 "DSO data fd counter out of bounds.");
348 dso__data_open_cnt--;
349}
350
351static void close_first_dso(void);
352
353static int do_open(char *name)
354{
355 int fd;
356 char sbuf[STRERR_BUFSIZE];
357
358 do {
359 fd = open(name, O_RDONLY);
360 if (fd >= 0)
361 return fd;
362
363 pr_debug("dso open failed: %s\n",
364 str_error_r(errno, sbuf, sizeof(sbuf)));
365 if (!dso__data_open_cnt || errno != EMFILE)
366 break;
367
368 close_first_dso();
369 } while (1);
370
371 return -1;
372}
373
374static int __open_dso(struct dso *dso, struct machine *machine)
375{
376 int fd;
377 char *root_dir = (char *)"";
378 char *name = malloc(PATH_MAX);
379
380 if (!name)
381 return -ENOMEM;
382
383 if (machine)
384 root_dir = machine->root_dir;
385
386 if (dso__read_binary_type_filename(dso, dso->binary_type,
387 root_dir, name, PATH_MAX)) {
388 free(name);
389 return -EINVAL;
390 }
391
392 if (!is_regular_file(name))
393 return -EINVAL;
394
395 fd = do_open(name);
396 free(name);
397 return fd;
398}
399
400static void check_data_close(void);
401
402/**
403 * dso_close - Open DSO data file
404 * @dso: dso object
405 *
406 * Open @dso's data file descriptor and updates
407 * list/count of open DSO objects.
408 */
409static int open_dso(struct dso *dso, struct machine *machine)
410{
411 int fd = __open_dso(dso, machine);
412
413 if (fd >= 0) {
414 dso__list_add(dso);
415 /*
416 * Check if we crossed the allowed number
417 * of opened DSOs and close one if needed.
418 */
419 check_data_close();
420 }
421
422 return fd;
423}
424
425static void close_data_fd(struct dso *dso)
426{
427 if (dso->data.fd >= 0) {
428 close(dso->data.fd);
429 dso->data.fd = -1;
430 dso->data.file_size = 0;
431 dso__list_del(dso);
432 }
433}
434
435/**
436 * dso_close - Close DSO data file
437 * @dso: dso object
438 *
439 * Close @dso's data file descriptor and updates
440 * list/count of open DSO objects.
441 */
442static void close_dso(struct dso *dso)
443{
444 close_data_fd(dso);
445}
446
447static void close_first_dso(void)
448{
449 struct dso *dso;
450
451 dso = list_first_entry(&dso__data_open, struct dso, data.open_entry);
452 close_dso(dso);
453}
454
455static rlim_t get_fd_limit(void)
456{
457 struct rlimit l;
458 rlim_t limit = 0;
459
460 /* Allow half of the current open fd limit. */
461 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
462 if (l.rlim_cur == RLIM_INFINITY)
463 limit = l.rlim_cur;
464 else
465 limit = l.rlim_cur / 2;
466 } else {
467 pr_err("failed to get fd limit\n");
468 limit = 1;
469 }
470
471 return limit;
472}
473
474static rlim_t fd_limit;
475
476/*
477 * Used only by tests/dso-data.c to reset the environment
478 * for tests. I dont expect we should change this during
479 * standard runtime.
480 */
481void reset_fd_limit(void)
482{
483 fd_limit = 0;
484}
485
486static bool may_cache_fd(void)
487{
488 if (!fd_limit)
489 fd_limit = get_fd_limit();
490
491 if (fd_limit == RLIM_INFINITY)
492 return true;
493
494 return fd_limit > (rlim_t) dso__data_open_cnt;
495}
496
497/*
498 * Check and close LRU dso if we crossed allowed limit
499 * for opened dso file descriptors. The limit is half
500 * of the RLIMIT_NOFILE files opened.
501*/
502static void check_data_close(void)
503{
504 bool cache_fd = may_cache_fd();
505
506 if (!cache_fd)
507 close_first_dso();
508}
509
510/**
511 * dso__data_close - Close DSO data file
512 * @dso: dso object
513 *
514 * External interface to close @dso's data file descriptor.
515 */
516void dso__data_close(struct dso *dso)
517{
518 pthread_mutex_lock(&dso__data_open_lock);
519 close_dso(dso);
520 pthread_mutex_unlock(&dso__data_open_lock);
521}
522
523static void try_to_open_dso(struct dso *dso, struct machine *machine)
524{
525 enum dso_binary_type binary_type_data[] = {
526 DSO_BINARY_TYPE__BUILD_ID_CACHE,
527 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
528 DSO_BINARY_TYPE__NOT_FOUND,
529 };
530 int i = 0;
531
532 if (dso->data.fd >= 0)
533 return;
534
535 if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) {
536 dso->data.fd = open_dso(dso, machine);
537 goto out;
538 }
539
540 do {
541 dso->binary_type = binary_type_data[i++];
542
543 dso->data.fd = open_dso(dso, machine);
544 if (dso->data.fd >= 0)
545 goto out;
546
547 } while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND);
548out:
549 if (dso->data.fd >= 0)
550 dso->data.status = DSO_DATA_STATUS_OK;
551 else
552 dso->data.status = DSO_DATA_STATUS_ERROR;
553}
554
555/**
556 * dso__data_get_fd - Get dso's data file descriptor
557 * @dso: dso object
558 * @machine: machine object
559 *
560 * External interface to find dso's file, open it and
561 * returns file descriptor. It should be paired with
562 * dso__data_put_fd() if it returns non-negative value.
563 */
564int dso__data_get_fd(struct dso *dso, struct machine *machine)
565{
566 if (dso->data.status == DSO_DATA_STATUS_ERROR)
567 return -1;
568
569 if (pthread_mutex_lock(&dso__data_open_lock) < 0)
570 return -1;
571
572 try_to_open_dso(dso, machine);
573
574 if (dso->data.fd < 0)
575 pthread_mutex_unlock(&dso__data_open_lock);
576
577 return dso->data.fd;
578}
579
580void dso__data_put_fd(struct dso *dso __maybe_unused)
581{
582 pthread_mutex_unlock(&dso__data_open_lock);
583}
584
585bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by)
586{
587 u32 flag = 1 << by;
588
589 if (dso->data.status_seen & flag)
590 return true;
591
592 dso->data.status_seen |= flag;
593
594 return false;
595}
596
597static void
598dso_cache__free(struct dso *dso)
599{
600 struct rb_root *root = &dso->data.cache;
601 struct rb_node *next = rb_first(root);
602
603 pthread_mutex_lock(&dso->lock);
604 while (next) {
605 struct dso_cache *cache;
606
607 cache = rb_entry(next, struct dso_cache, rb_node);
608 next = rb_next(&cache->rb_node);
609 rb_erase(&cache->rb_node, root);
610 free(cache);
611 }
612 pthread_mutex_unlock(&dso->lock);
613}
614
615static struct dso_cache *dso_cache__find(struct dso *dso, u64 offset)
616{
617 const struct rb_root *root = &dso->data.cache;
618 struct rb_node * const *p = &root->rb_node;
619 const struct rb_node *parent = NULL;
620 struct dso_cache *cache;
621
622 while (*p != NULL) {
623 u64 end;
624
625 parent = *p;
626 cache = rb_entry(parent, struct dso_cache, rb_node);
627 end = cache->offset + DSO__DATA_CACHE_SIZE;
628
629 if (offset < cache->offset)
630 p = &(*p)->rb_left;
631 else if (offset >= end)
632 p = &(*p)->rb_right;
633 else
634 return cache;
635 }
636
637 return NULL;
638}
639
640static struct dso_cache *
641dso_cache__insert(struct dso *dso, struct dso_cache *new)
642{
643 struct rb_root *root = &dso->data.cache;
644 struct rb_node **p = &root->rb_node;
645 struct rb_node *parent = NULL;
646 struct dso_cache *cache;
647 u64 offset = new->offset;
648
649 pthread_mutex_lock(&dso->lock);
650 while (*p != NULL) {
651 u64 end;
652
653 parent = *p;
654 cache = rb_entry(parent, struct dso_cache, rb_node);
655 end = cache->offset + DSO__DATA_CACHE_SIZE;
656
657 if (offset < cache->offset)
658 p = &(*p)->rb_left;
659 else if (offset >= end)
660 p = &(*p)->rb_right;
661 else
662 goto out;
663 }
664
665 rb_link_node(&new->rb_node, parent, p);
666 rb_insert_color(&new->rb_node, root);
667
668 cache = NULL;
669out:
670 pthread_mutex_unlock(&dso->lock);
671 return cache;
672}
673
674static ssize_t
675dso_cache__memcpy(struct dso_cache *cache, u64 offset,
676 u8 *data, u64 size)
677{
678 u64 cache_offset = offset - cache->offset;
679 u64 cache_size = min(cache->size - cache_offset, size);
680
681 memcpy(data, cache->data + cache_offset, cache_size);
682 return cache_size;
683}
684
685static ssize_t
686dso_cache__read(struct dso *dso, struct machine *machine,
687 u64 offset, u8 *data, ssize_t size)
688{
689 struct dso_cache *cache;
690 struct dso_cache *old;
691 ssize_t ret;
692
693 do {
694 u64 cache_offset;
695
696 cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
697 if (!cache)
698 return -ENOMEM;
699
700 pthread_mutex_lock(&dso__data_open_lock);
701
702 /*
703 * dso->data.fd might be closed if other thread opened another
704 * file (dso) due to open file limit (RLIMIT_NOFILE).
705 */
706 try_to_open_dso(dso, machine);
707
708 if (dso->data.fd < 0) {
709 ret = -errno;
710 dso->data.status = DSO_DATA_STATUS_ERROR;
711 break;
712 }
713
714 cache_offset = offset & DSO__DATA_CACHE_MASK;
715
716 ret = pread(dso->data.fd, cache->data, DSO__DATA_CACHE_SIZE, cache_offset);
717 if (ret <= 0)
718 break;
719
720 cache->offset = cache_offset;
721 cache->size = ret;
722 } while (0);
723
724 pthread_mutex_unlock(&dso__data_open_lock);
725
726 if (ret > 0) {
727 old = dso_cache__insert(dso, cache);
728 if (old) {
729 /* we lose the race */
730 free(cache);
731 cache = old;
732 }
733
734 ret = dso_cache__memcpy(cache, offset, data, size);
735 }
736
737 if (ret <= 0)
738 free(cache);
739
740 return ret;
741}
742
743static ssize_t dso_cache_read(struct dso *dso, struct machine *machine,
744 u64 offset, u8 *data, ssize_t size)
745{
746 struct dso_cache *cache;
747
748 cache = dso_cache__find(dso, offset);
749 if (cache)
750 return dso_cache__memcpy(cache, offset, data, size);
751 else
752 return dso_cache__read(dso, machine, offset, data, size);
753}
754
755/*
756 * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
757 * in the rb_tree. Any read to already cached data is served
758 * by cached data.
759 */
760static ssize_t cached_read(struct dso *dso, struct machine *machine,
761 u64 offset, u8 *data, ssize_t size)
762{
763 ssize_t r = 0;
764 u8 *p = data;
765
766 do {
767 ssize_t ret;
768
769 ret = dso_cache_read(dso, machine, offset, p, size);
770 if (ret < 0)
771 return ret;
772
773 /* Reached EOF, return what we have. */
774 if (!ret)
775 break;
776
777 BUG_ON(ret > size);
778
779 r += ret;
780 p += ret;
781 offset += ret;
782 size -= ret;
783
784 } while (size);
785
786 return r;
787}
788
789static int data_file_size(struct dso *dso, struct machine *machine)
790{
791 int ret = 0;
792 struct stat st;
793 char sbuf[STRERR_BUFSIZE];
794
795 if (dso->data.file_size)
796 return 0;
797
798 if (dso->data.status == DSO_DATA_STATUS_ERROR)
799 return -1;
800
801 pthread_mutex_lock(&dso__data_open_lock);
802
803 /*
804 * dso->data.fd might be closed if other thread opened another
805 * file (dso) due to open file limit (RLIMIT_NOFILE).
806 */
807 try_to_open_dso(dso, machine);
808
809 if (dso->data.fd < 0) {
810 ret = -errno;
811 dso->data.status = DSO_DATA_STATUS_ERROR;
812 goto out;
813 }
814
815 if (fstat(dso->data.fd, &st) < 0) {
816 ret = -errno;
817 pr_err("dso cache fstat failed: %s\n",
818 str_error_r(errno, sbuf, sizeof(sbuf)));
819 dso->data.status = DSO_DATA_STATUS_ERROR;
820 goto out;
821 }
822 dso->data.file_size = st.st_size;
823
824out:
825 pthread_mutex_unlock(&dso__data_open_lock);
826 return ret;
827}
828
829/**
830 * dso__data_size - Return dso data size
831 * @dso: dso object
832 * @machine: machine object
833 *
834 * Return: dso data size
835 */
836off_t dso__data_size(struct dso *dso, struct machine *machine)
837{
838 if (data_file_size(dso, machine))
839 return -1;
840
841 /* For now just estimate dso data size is close to file size */
842 return dso->data.file_size;
843}
844
845static ssize_t data_read_offset(struct dso *dso, struct machine *machine,
846 u64 offset, u8 *data, ssize_t size)
847{
848 if (data_file_size(dso, machine))
849 return -1;
850
851 /* Check the offset sanity. */
852 if (offset > dso->data.file_size)
853 return -1;
854
855 if (offset + size < offset)
856 return -1;
857
858 return cached_read(dso, machine, offset, data, size);
859}
860
861/**
862 * dso__data_read_offset - Read data from dso file offset
863 * @dso: dso object
864 * @machine: machine object
865 * @offset: file offset
866 * @data: buffer to store data
867 * @size: size of the @data buffer
868 *
869 * External interface to read data from dso file offset. Open
870 * dso data file and use cached_read to get the data.
871 */
872ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
873 u64 offset, u8 *data, ssize_t size)
874{
875 if (dso->data.status == DSO_DATA_STATUS_ERROR)
876 return -1;
877
878 return data_read_offset(dso, machine, offset, data, size);
879}
880
881/**
882 * dso__data_read_addr - Read data from dso address
883 * @dso: dso object
884 * @machine: machine object
885 * @add: virtual memory address
886 * @data: buffer to store data
887 * @size: size of the @data buffer
888 *
889 * External interface to read data from dso address.
890 */
891ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
892 struct machine *machine, u64 addr,
893 u8 *data, ssize_t size)
894{
895 u64 offset = map->map_ip(map, addr);
896 return dso__data_read_offset(dso, machine, offset, data, size);
897}
898
899struct map *dso__new_map(const char *name)
900{
901 struct map *map = NULL;
902 struct dso *dso = dso__new(name);
903
904 if (dso)
905 map = map__new2(0, dso, MAP__FUNCTION);
906
907 return map;
908}
909
910struct dso *machine__findnew_kernel(struct machine *machine, const char *name,
911 const char *short_name, int dso_type)
912{
913 /*
914 * The kernel dso could be created by build_id processing.
915 */
916 struct dso *dso = machine__findnew_dso(machine, name);
917
918 /*
919 * We need to run this in all cases, since during the build_id
920 * processing we had no idea this was the kernel dso.
921 */
922 if (dso != NULL) {
923 dso__set_short_name(dso, short_name, false);
924 dso->kernel = dso_type;
925 }
926
927 return dso;
928}
929
930/*
931 * Find a matching entry and/or link current entry to RB tree.
932 * Either one of the dso or name parameter must be non-NULL or the
933 * function will not work.
934 */
935static struct dso *__dso__findlink_by_longname(struct rb_root *root,
936 struct dso *dso, const char *name)
937{
938 struct rb_node **p = &root->rb_node;
939 struct rb_node *parent = NULL;
940
941 if (!name)
942 name = dso->long_name;
943 /*
944 * Find node with the matching name
945 */
946 while (*p) {
947 struct dso *this = rb_entry(*p, struct dso, rb_node);
948 int rc = strcmp(name, this->long_name);
949
950 parent = *p;
951 if (rc == 0) {
952 /*
953 * In case the new DSO is a duplicate of an existing
954 * one, print a one-time warning & put the new entry
955 * at the end of the list of duplicates.
956 */
957 if (!dso || (dso == this))
958 return this; /* Find matching dso */
959 /*
960 * The core kernel DSOs may have duplicated long name.
961 * In this case, the short name should be different.
962 * Comparing the short names to differentiate the DSOs.
963 */
964 rc = strcmp(dso->short_name, this->short_name);
965 if (rc == 0) {
966 pr_err("Duplicated dso name: %s\n", name);
967 return NULL;
968 }
969 }
970 if (rc < 0)
971 p = &parent->rb_left;
972 else
973 p = &parent->rb_right;
974 }
975 if (dso) {
976 /* Add new node and rebalance tree */
977 rb_link_node(&dso->rb_node, parent, p);
978 rb_insert_color(&dso->rb_node, root);
979 dso->root = root;
980 }
981 return NULL;
982}
983
984static inline struct dso *__dso__find_by_longname(struct rb_root *root,
985 const char *name)
986{
987 return __dso__findlink_by_longname(root, NULL, name);
988}
989
990void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
991{
992 struct rb_root *root = dso->root;
993
994 if (name == NULL)
995 return;
996
997 if (dso->long_name_allocated)
998 free((char *)dso->long_name);
999
1000 if (root) {
1001 rb_erase(&dso->rb_node, root);
1002 /*
1003 * __dso__findlink_by_longname() isn't guaranteed to add it
1004 * back, so a clean removal is required here.
1005 */
1006 RB_CLEAR_NODE(&dso->rb_node);
1007 dso->root = NULL;
1008 }
1009
1010 dso->long_name = name;
1011 dso->long_name_len = strlen(name);
1012 dso->long_name_allocated = name_allocated;
1013
1014 if (root)
1015 __dso__findlink_by_longname(root, dso, NULL);
1016}
1017
1018void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
1019{
1020 if (name == NULL)
1021 return;
1022
1023 if (dso->short_name_allocated)
1024 free((char *)dso->short_name);
1025
1026 dso->short_name = name;
1027 dso->short_name_len = strlen(name);
1028 dso->short_name_allocated = name_allocated;
1029}
1030
1031static void dso__set_basename(struct dso *dso)
1032{
1033 /*
1034 * basename() may modify path buffer, so we must pass
1035 * a copy.
1036 */
1037 char *base, *lname = strdup(dso->long_name);
1038
1039 if (!lname)
1040 return;
1041
1042 /*
1043 * basename() may return a pointer to internal
1044 * storage which is reused in subsequent calls
1045 * so copy the result.
1046 */
1047 base = strdup(basename(lname));
1048
1049 free(lname);
1050
1051 if (!base)
1052 return;
1053
1054 dso__set_short_name(dso, base, true);
1055}
1056
1057int dso__name_len(const struct dso *dso)
1058{
1059 if (!dso)
1060 return strlen("[unknown]");
1061 if (verbose > 0)
1062 return dso->long_name_len;
1063
1064 return dso->short_name_len;
1065}
1066
1067bool dso__loaded(const struct dso *dso, enum map_type type)
1068{
1069 return dso->loaded & (1 << type);
1070}
1071
1072bool dso__sorted_by_name(const struct dso *dso, enum map_type type)
1073{
1074 return dso->sorted_by_name & (1 << type);
1075}
1076
1077void dso__set_sorted_by_name(struct dso *dso, enum map_type type)
1078{
1079 dso->sorted_by_name |= (1 << type);
1080}
1081
1082struct dso *dso__new(const char *name)
1083{
1084 struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
1085
1086 if (dso != NULL) {
1087 int i;
1088 strcpy(dso->name, name);
1089 dso__set_long_name(dso, dso->name, false);
1090 dso__set_short_name(dso, dso->name, false);
1091 for (i = 0; i < MAP__NR_TYPES; ++i)
1092 dso->symbols[i] = dso->symbol_names[i] = RB_ROOT;
1093 dso->data.cache = RB_ROOT;
1094 dso->data.fd = -1;
1095 dso->data.status = DSO_DATA_STATUS_UNKNOWN;
1096 dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
1097 dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
1098 dso->is_64_bit = (sizeof(void *) == 8);
1099 dso->loaded = 0;
1100 dso->rel = 0;
1101 dso->sorted_by_name = 0;
1102 dso->has_build_id = 0;
1103 dso->has_srcline = 1;
1104 dso->a2l_fails = 1;
1105 dso->kernel = DSO_TYPE_USER;
1106 dso->needs_swap = DSO_SWAP__UNSET;
1107 RB_CLEAR_NODE(&dso->rb_node);
1108 dso->root = NULL;
1109 INIT_LIST_HEAD(&dso->node);
1110 INIT_LIST_HEAD(&dso->data.open_entry);
1111 pthread_mutex_init(&dso->lock, NULL);
1112 atomic_set(&dso->refcnt, 1);
1113 }
1114
1115 return dso;
1116}
1117
1118void dso__delete(struct dso *dso)
1119{
1120 int i;
1121
1122 if (!RB_EMPTY_NODE(&dso->rb_node))
1123 pr_err("DSO %s is still in rbtree when being deleted!\n",
1124 dso->long_name);
1125 for (i = 0; i < MAP__NR_TYPES; ++i)
1126 symbols__delete(&dso->symbols[i]);
1127
1128 if (dso->short_name_allocated) {
1129 zfree((char **)&dso->short_name);
1130 dso->short_name_allocated = false;
1131 }
1132
1133 if (dso->long_name_allocated) {
1134 zfree((char **)&dso->long_name);
1135 dso->long_name_allocated = false;
1136 }
1137
1138 dso__data_close(dso);
1139 auxtrace_cache__free(dso->auxtrace_cache);
1140 dso_cache__free(dso);
1141 dso__free_a2l(dso);
1142 zfree(&dso->symsrc_filename);
1143 pthread_mutex_destroy(&dso->lock);
1144 free(dso);
1145}
1146
1147struct dso *dso__get(struct dso *dso)
1148{
1149 if (dso)
1150 atomic_inc(&dso->refcnt);
1151 return dso;
1152}
1153
1154void dso__put(struct dso *dso)
1155{
1156 if (dso && atomic_dec_and_test(&dso->refcnt))
1157 dso__delete(dso);
1158}
1159
1160void dso__set_build_id(struct dso *dso, void *build_id)
1161{
1162 memcpy(dso->build_id, build_id, sizeof(dso->build_id));
1163 dso->has_build_id = 1;
1164}
1165
1166bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
1167{
1168 return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
1169}
1170
1171void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
1172{
1173 char path[PATH_MAX];
1174
1175 if (machine__is_default_guest(machine))
1176 return;
1177 sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
1178 if (sysfs__read_build_id(path, dso->build_id,
1179 sizeof(dso->build_id)) == 0)
1180 dso->has_build_id = true;
1181}
1182
1183int dso__kernel_module_get_build_id(struct dso *dso,
1184 const char *root_dir)
1185{
1186 char filename[PATH_MAX];
1187 /*
1188 * kernel module short names are of the form "[module]" and
1189 * we need just "module" here.
1190 */
1191 const char *name = dso->short_name + 1;
1192
1193 snprintf(filename, sizeof(filename),
1194 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1195 root_dir, (int)strlen(name) - 1, name);
1196
1197 if (sysfs__read_build_id(filename, dso->build_id,
1198 sizeof(dso->build_id)) == 0)
1199 dso->has_build_id = true;
1200
1201 return 0;
1202}
1203
1204bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
1205{
1206 bool have_build_id = false;
1207 struct dso *pos;
1208
1209 list_for_each_entry(pos, head, node) {
1210 if (with_hits && !pos->hit && !dso__is_vdso(pos))
1211 continue;
1212 if (pos->has_build_id) {
1213 have_build_id = true;
1214 continue;
1215 }
1216 if (filename__read_build_id(pos->long_name, pos->build_id,
1217 sizeof(pos->build_id)) > 0) {
1218 have_build_id = true;
1219 pos->has_build_id = true;
1220 }
1221 }
1222
1223 return have_build_id;
1224}
1225
1226void __dsos__add(struct dsos *dsos, struct dso *dso)
1227{
1228 list_add_tail(&dso->node, &dsos->head);
1229 __dso__findlink_by_longname(&dsos->root, dso, NULL);
1230 /*
1231 * It is now in the linked list, grab a reference, then garbage collect
1232 * this when needing memory, by looking at LRU dso instances in the
1233 * list with atomic_read(&dso->refcnt) == 1, i.e. no references
1234 * anywhere besides the one for the list, do, under a lock for the
1235 * list: remove it from the list, then a dso__put(), that probably will
1236 * be the last and will then call dso__delete(), end of life.
1237 *
1238 * That, or at the end of the 'struct machine' lifetime, when all
1239 * 'struct dso' instances will be removed from the list, in
1240 * dsos__exit(), if they have no other reference from some other data
1241 * structure.
1242 *
1243 * E.g.: after processing a 'perf.data' file and storing references
1244 * to objects instantiated while processing events, we will have
1245 * references to the 'thread', 'map', 'dso' structs all from 'struct
1246 * hist_entry' instances, but we may not need anything not referenced,
1247 * so we might as well call machines__exit()/machines__delete() and
1248 * garbage collect it.
1249 */
1250 dso__get(dso);
1251}
1252
1253void dsos__add(struct dsos *dsos, struct dso *dso)
1254{
1255 pthread_rwlock_wrlock(&dsos->lock);
1256 __dsos__add(dsos, dso);
1257 pthread_rwlock_unlock(&dsos->lock);
1258}
1259
1260struct dso *__dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
1261{
1262 struct dso *pos;
1263
1264 if (cmp_short) {
1265 list_for_each_entry(pos, &dsos->head, node)
1266 if (strcmp(pos->short_name, name) == 0)
1267 return pos;
1268 return NULL;
1269 }
1270 return __dso__find_by_longname(&dsos->root, name);
1271}
1272
1273struct dso *dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
1274{
1275 struct dso *dso;
1276 pthread_rwlock_rdlock(&dsos->lock);
1277 dso = __dsos__find(dsos, name, cmp_short);
1278 pthread_rwlock_unlock(&dsos->lock);
1279 return dso;
1280}
1281
1282struct dso *__dsos__addnew(struct dsos *dsos, const char *name)
1283{
1284 struct dso *dso = dso__new(name);
1285
1286 if (dso != NULL) {
1287 __dsos__add(dsos, dso);
1288 dso__set_basename(dso);
1289 /* Put dso here because __dsos_add already got it */
1290 dso__put(dso);
1291 }
1292 return dso;
1293}
1294
1295struct dso *__dsos__findnew(struct dsos *dsos, const char *name)
1296{
1297 struct dso *dso = __dsos__find(dsos, name, false);
1298
1299 return dso ? dso : __dsos__addnew(dsos, name);
1300}
1301
1302struct dso *dsos__findnew(struct dsos *dsos, const char *name)
1303{
1304 struct dso *dso;
1305 pthread_rwlock_wrlock(&dsos->lock);
1306 dso = dso__get(__dsos__findnew(dsos, name));
1307 pthread_rwlock_unlock(&dsos->lock);
1308 return dso;
1309}
1310
1311size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
1312 bool (skip)(struct dso *dso, int parm), int parm)
1313{
1314 struct dso *pos;
1315 size_t ret = 0;
1316
1317 list_for_each_entry(pos, head, node) {
1318 if (skip && skip(pos, parm))
1319 continue;
1320 ret += dso__fprintf_buildid(pos, fp);
1321 ret += fprintf(fp, " %s\n", pos->long_name);
1322 }
1323 return ret;
1324}
1325
1326size_t __dsos__fprintf(struct list_head *head, FILE *fp)
1327{
1328 struct dso *pos;
1329 size_t ret = 0;
1330
1331 list_for_each_entry(pos, head, node) {
1332 int i;
1333 for (i = 0; i < MAP__NR_TYPES; ++i)
1334 ret += dso__fprintf(pos, i, fp);
1335 }
1336
1337 return ret;
1338}
1339
1340size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
1341{
1342 char sbuild_id[SBUILD_ID_SIZE];
1343
1344 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1345 return fprintf(fp, "%s", sbuild_id);
1346}
1347
1348size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp)
1349{
1350 struct rb_node *nd;
1351 size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
1352
1353 if (dso->short_name != dso->long_name)
1354 ret += fprintf(fp, "%s, ", dso->long_name);
1355 ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
1356 dso__loaded(dso, type) ? "" : "NOT ");
1357 ret += dso__fprintf_buildid(dso, fp);
1358 ret += fprintf(fp, ")\n");
1359 for (nd = rb_first(&dso->symbols[type]); nd; nd = rb_next(nd)) {
1360 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
1361 ret += symbol__fprintf(pos, fp);
1362 }
1363
1364 return ret;
1365}
1366
1367enum dso_type dso__type(struct dso *dso, struct machine *machine)
1368{
1369 int fd;
1370 enum dso_type type = DSO__TYPE_UNKNOWN;
1371
1372 fd = dso__data_get_fd(dso, machine);
1373 if (fd >= 0) {
1374 type = dso__type_fd(fd);
1375 dso__data_put_fd(dso);
1376 }
1377
1378 return type;
1379}
1380
1381int dso__strerror_load(struct dso *dso, char *buf, size_t buflen)
1382{
1383 int idx, errnum = dso->load_errno;
1384 /*
1385 * This must have a same ordering as the enum dso_load_errno.
1386 */
1387 static const char *dso_load__error_str[] = {
1388 "Internal tools/perf/ library error",
1389 "Invalid ELF file",
1390 "Can not read build id",
1391 "Mismatching build id",
1392 "Decompression failure",
1393 };
1394
1395 BUG_ON(buflen == 0);
1396
1397 if (errnum >= 0) {
1398 const char *err = str_error_r(errnum, buf, buflen);
1399
1400 if (err != buf)
1401 scnprintf(buf, buflen, "%s", err);
1402
1403 return 0;
1404 }
1405
1406 if (errnum < __DSO_LOAD_ERRNO__START || errnum >= __DSO_LOAD_ERRNO__END)
1407 return -1;
1408
1409 idx = errnum - __DSO_LOAD_ERRNO__START;
1410 scnprintf(buf, buflen, "%s", dso_load__error_str[idx]);
1411 return 0;
1412}