1
2#include <asm/bug.h>
3#include <linux/kernel.h>
4#include <linux/string.h>
5#include <linux/zalloc.h>
6#include <sys/time.h>
7#include <sys/resource.h>
8#include <sys/types.h>
9#include <sys/stat.h>
10#include <unistd.h>
11#include <errno.h>
12#include <fcntl.h>
13#include <libgen.h>
14#include <bpf/libbpf.h>
15#include "bpf-event.h"
16#include "compress.h"
17#include "namespaces.h"
18#include "path.h"
19#include "map.h"
20#include "symbol.h"
21#include "srcline.h"
22#include "dso.h"
23#include "machine.h"
24#include "auxtrace.h"
25#include "util.h"
26#include "debug.h"
27#include "string2.h"
28#include "vdso.h"
29
30static const char * const debuglink_paths[] = {
31 "%.0s%s",
32 "%s/%s",
33 "%s/.debug/%s",
34 "/usr/lib/debug%s/%s"
35};
36
37char dso__symtab_origin(const struct dso *dso)
38{
39 static const char origin[] = {
40 [DSO_BINARY_TYPE__KALLSYMS] = 'k',
41 [DSO_BINARY_TYPE__VMLINUX] = 'v',
42 [DSO_BINARY_TYPE__JAVA_JIT] = 'j',
43 [DSO_BINARY_TYPE__DEBUGLINK] = 'l',
44 [DSO_BINARY_TYPE__BUILD_ID_CACHE] = 'B',
45 [DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO] = 'D',
46 [DSO_BINARY_TYPE__FEDORA_DEBUGINFO] = 'f',
47 [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO] = 'u',
48 [DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO] = 'o',
49 [DSO_BINARY_TYPE__BUILDID_DEBUGINFO] = 'b',
50 [DSO_BINARY_TYPE__SYSTEM_PATH_DSO] = 'd',
51 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE] = 'K',
52 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP] = 'm',
53 [DSO_BINARY_TYPE__GUEST_KALLSYMS] = 'g',
54 [DSO_BINARY_TYPE__GUEST_KMODULE] = 'G',
55 [DSO_BINARY_TYPE__GUEST_KMODULE_COMP] = 'M',
56 [DSO_BINARY_TYPE__GUEST_VMLINUX] = 'V',
57 };
58
59 if (dso == NULL || dso->symtab_type == DSO_BINARY_TYPE__NOT_FOUND)
60 return '!';
61 return origin[dso->symtab_type];
62}
63
64int dso__read_binary_type_filename(const struct dso *dso,
65 enum dso_binary_type type,
66 char *root_dir, char *filename, size_t size)
67{
68 char build_id_hex[SBUILD_ID_SIZE];
69 int ret = 0;
70 size_t len;
71
72 switch (type) {
73 case DSO_BINARY_TYPE__DEBUGLINK:
74 {
75 const char *last_slash;
76 char dso_dir[PATH_MAX];
77 char symfile[PATH_MAX];
78 unsigned int i;
79
80 len = __symbol__join_symfs(filename, size, dso->long_name);
81 last_slash = filename + len;
82 while (last_slash != filename && *last_slash != '/')
83 last_slash--;
84
85 strncpy(dso_dir, filename, last_slash - filename);
86 dso_dir[last_slash-filename] = '\0';
87
88 if (!is_regular_file(filename)) {
89 ret = -1;
90 break;
91 }
92
93 ret = filename__read_debuglink(filename, symfile, PATH_MAX);
94 if (ret)
95 break;
96
97
98 ret = -1;
99 for (i = 0; i < ARRAY_SIZE(debuglink_paths); i++) {
100 snprintf(filename, size,
101 debuglink_paths[i], dso_dir, symfile);
102 if (is_regular_file(filename)) {
103 ret = 0;
104 break;
105 }
106 }
107
108 break;
109 }
110 case DSO_BINARY_TYPE__BUILD_ID_CACHE:
111 if (dso__build_id_filename(dso, filename, size, false) == NULL)
112 ret = -1;
113 break;
114
115 case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO:
116 if (dso__build_id_filename(dso, filename, size, true) == NULL)
117 ret = -1;
118 break;
119
120 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
121 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
122 snprintf(filename + len, size - len, "%s.debug", dso->long_name);
123 break;
124
125 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
126 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
127 snprintf(filename + len, size - len, "%s", dso->long_name);
128 break;
129
130 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
131 {
132 const char *last_slash;
133 size_t dir_size;
134
135 last_slash = dso->long_name + dso->long_name_len;
136 while (last_slash != dso->long_name && *last_slash != '/')
137 last_slash--;
138
139 len = __symbol__join_symfs(filename, size, "");
140 dir_size = last_slash - dso->long_name + 2;
141 if (dir_size > (size - len)) {
142 ret = -1;
143 break;
144 }
145 len += scnprintf(filename + len, dir_size, "%s", dso->long_name);
146 len += scnprintf(filename + len , size - len, ".debug%s",
147 last_slash);
148 break;
149 }
150
151 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
152 if (!dso->has_build_id) {
153 ret = -1;
154 break;
155 }
156
157 build_id__sprintf(dso->build_id,
158 sizeof(dso->build_id),
159 build_id_hex);
160 len = __symbol__join_symfs(filename, size, "/usr/lib/debug/.build-id/");
161 snprintf(filename + len, size - len, "%.2s/%s.debug",
162 build_id_hex, build_id_hex + 2);
163 break;
164
165 case DSO_BINARY_TYPE__VMLINUX:
166 case DSO_BINARY_TYPE__GUEST_VMLINUX:
167 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
168 __symbol__join_symfs(filename, size, dso->long_name);
169 break;
170
171 case DSO_BINARY_TYPE__GUEST_KMODULE:
172 case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
173 path__join3(filename, size, symbol_conf.symfs,
174 root_dir, dso->long_name);
175 break;
176
177 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
178 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
179 __symbol__join_symfs(filename, size, dso->long_name);
180 break;
181
182 case DSO_BINARY_TYPE__KCORE:
183 case DSO_BINARY_TYPE__GUEST_KCORE:
184 snprintf(filename, size, "%s", dso->long_name);
185 break;
186
187 default:
188 case DSO_BINARY_TYPE__KALLSYMS:
189 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
190 case DSO_BINARY_TYPE__JAVA_JIT:
191 case DSO_BINARY_TYPE__BPF_PROG_INFO:
192 case DSO_BINARY_TYPE__NOT_FOUND:
193 ret = -1;
194 break;
195 }
196
197 return ret;
198}
199
200enum {
201 COMP_ID__NONE = 0,
202};
203
204static const struct {
205 const char *fmt;
206 int (*decompress)(const char *input, int output);
207 bool (*is_compressed)(const char *input);
208} compressions[] = {
209 [COMP_ID__NONE] = { .fmt = NULL, },
210#ifdef HAVE_ZLIB_SUPPORT
211 { "gz", gzip_decompress_to_file, gzip_is_compressed },
212#endif
213#ifdef HAVE_LZMA_SUPPORT
214 { "xz", lzma_decompress_to_file, lzma_is_compressed },
215#endif
216 { NULL, NULL, NULL },
217};
218
219static int is_supported_compression(const char *ext)
220{
221 unsigned i;
222
223 for (i = 1; compressions[i].fmt; i++) {
224 if (!strcmp(ext, compressions[i].fmt))
225 return i;
226 }
227 return COMP_ID__NONE;
228}
229
230bool is_kernel_module(const char *pathname, int cpumode)
231{
232 struct kmod_path m;
233 int mode = cpumode & PERF_RECORD_MISC_CPUMODE_MASK;
234
235 WARN_ONCE(mode != cpumode,
236 "Internal error: passing unmasked cpumode (%x) to is_kernel_module",
237 cpumode);
238
239 switch (mode) {
240 case PERF_RECORD_MISC_USER:
241 case PERF_RECORD_MISC_HYPERVISOR:
242 case PERF_RECORD_MISC_GUEST_USER:
243 return false;
244
245 default:
246 if (kmod_path__parse(&m, pathname)) {
247 pr_err("Failed to check whether %s is a kernel module or not. Assume it is.",
248 pathname);
249 return true;
250 }
251 }
252
253 return m.kmod;
254}
255
256bool dso__needs_decompress(struct dso *dso)
257{
258 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
259 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
260}
261
262static int decompress_kmodule(struct dso *dso, const char *name,
263 char *pathname, size_t len)
264{
265 char tmpbuf[] = KMOD_DECOMP_NAME;
266 int fd = -1;
267
268 if (!dso__needs_decompress(dso))
269 return -1;
270
271 if (dso->comp == COMP_ID__NONE)
272 return -1;
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287 if (!compressions[dso->comp].is_compressed(name))
288 return open(name, O_RDONLY);
289
290 fd = mkstemp(tmpbuf);
291 if (fd < 0) {
292 dso->load_errno = errno;
293 return -1;
294 }
295
296 if (compressions[dso->comp].decompress(name, fd)) {
297 dso->load_errno = DSO_LOAD_ERRNO__DECOMPRESSION_FAILURE;
298 close(fd);
299 fd = -1;
300 }
301
302 if (!pathname || (fd < 0))
303 unlink(tmpbuf);
304
305 if (pathname && (fd >= 0))
306 strlcpy(pathname, tmpbuf, len);
307
308 return fd;
309}
310
311int dso__decompress_kmodule_fd(struct dso *dso, const char *name)
312{
313 return decompress_kmodule(dso, name, NULL, 0);
314}
315
316int dso__decompress_kmodule_path(struct dso *dso, const char *name,
317 char *pathname, size_t len)
318{
319 int fd = decompress_kmodule(dso, name, pathname, len);
320
321 close(fd);
322 return fd >= 0 ? 0 : -1;
323}
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341int __kmod_path__parse(struct kmod_path *m, const char *path,
342 bool alloc_name)
343{
344 const char *name = strrchr(path, '/');
345 const char *ext = strrchr(path, '.');
346 bool is_simple_name = false;
347
348 memset(m, 0x0, sizeof(*m));
349 name = name ? name + 1 : path;
350
351
352
353
354
355
356
357
358
359 if (name[0] == '[') {
360 is_simple_name = true;
361 if ((strncmp(name, "[kernel.kallsyms]", 17) == 0) ||
362 (strncmp(name, "[guest.kernel.kallsyms", 22) == 0) ||
363 (strncmp(name, "[vdso]", 6) == 0) ||
364 (strncmp(name, "[vdso32]", 8) == 0) ||
365 (strncmp(name, "[vdsox32]", 9) == 0) ||
366 (strncmp(name, "[vsyscall]", 10) == 0)) {
367 m->kmod = false;
368
369 } else
370 m->kmod = true;
371 }
372
373
374 if ((ext == NULL) || is_simple_name) {
375 if (alloc_name) {
376 m->name = strdup(name);
377 return m->name ? 0 : -ENOMEM;
378 }
379 return 0;
380 }
381
382 m->comp = is_supported_compression(ext + 1);
383 if (m->comp > COMP_ID__NONE)
384 ext -= 3;
385
386
387 if (ext > name)
388 m->kmod = !strncmp(ext, ".ko", 3);
389
390 if (alloc_name) {
391 if (m->kmod) {
392 if (asprintf(&m->name, "[%.*s]", (int) (ext - name), name) == -1)
393 return -ENOMEM;
394 } else {
395 if (asprintf(&m->name, "%s", name) == -1)
396 return -ENOMEM;
397 }
398
399 strreplace(m->name, '-', '_');
400 }
401
402 return 0;
403}
404
405void dso__set_module_info(struct dso *dso, struct kmod_path *m,
406 struct machine *machine)
407{
408 if (machine__is_host(machine))
409 dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE;
410 else
411 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE;
412
413
414 if (m->kmod && m->comp) {
415 dso->symtab_type++;
416 dso->comp = m->comp;
417 }
418
419 dso__set_short_name(dso, strdup(m->name), true);
420}
421
422
423
424
425static LIST_HEAD(dso__data_open);
426static long dso__data_open_cnt;
427static pthread_mutex_t dso__data_open_lock = PTHREAD_MUTEX_INITIALIZER;
428
429static void dso__list_add(struct dso *dso)
430{
431 list_add_tail(&dso->data.open_entry, &dso__data_open);
432 dso__data_open_cnt++;
433}
434
435static void dso__list_del(struct dso *dso)
436{
437 list_del_init(&dso->data.open_entry);
438 WARN_ONCE(dso__data_open_cnt <= 0,
439 "DSO data fd counter out of bounds.");
440 dso__data_open_cnt--;
441}
442
443static void close_first_dso(void);
444
445static int do_open(char *name)
446{
447 int fd;
448 char sbuf[STRERR_BUFSIZE];
449
450 do {
451 fd = open(name, O_RDONLY|O_CLOEXEC);
452 if (fd >= 0)
453 return fd;
454
455 pr_debug("dso open failed: %s\n",
456 str_error_r(errno, sbuf, sizeof(sbuf)));
457 if (!dso__data_open_cnt || errno != EMFILE)
458 break;
459
460 close_first_dso();
461 } while (1);
462
463 return -1;
464}
465
466static int __open_dso(struct dso *dso, struct machine *machine)
467{
468 int fd = -EINVAL;
469 char *root_dir = (char *)"";
470 char *name = malloc(PATH_MAX);
471 bool decomp = false;
472
473 if (!name)
474 return -ENOMEM;
475
476 if (machine)
477 root_dir = machine->root_dir;
478
479 if (dso__read_binary_type_filename(dso, dso->binary_type,
480 root_dir, name, PATH_MAX))
481 goto out;
482
483 if (!is_regular_file(name))
484 goto out;
485
486 if (dso__needs_decompress(dso)) {
487 char newpath[KMOD_DECOMP_LEN];
488 size_t len = sizeof(newpath);
489
490 if (dso__decompress_kmodule_path(dso, name, newpath, len) < 0) {
491 fd = -dso->load_errno;
492 goto out;
493 }
494
495 decomp = true;
496 strcpy(name, newpath);
497 }
498
499 fd = do_open(name);
500
501 if (decomp)
502 unlink(name);
503
504out:
505 free(name);
506 return fd;
507}
508
509static void check_data_close(void);
510
511
512
513
514
515
516
517
518static int open_dso(struct dso *dso, struct machine *machine)
519{
520 int fd;
521 struct nscookie nsc;
522
523 if (dso->binary_type != DSO_BINARY_TYPE__BUILD_ID_CACHE)
524 nsinfo__mountns_enter(dso->nsinfo, &nsc);
525 fd = __open_dso(dso, machine);
526 if (dso->binary_type != DSO_BINARY_TYPE__BUILD_ID_CACHE)
527 nsinfo__mountns_exit(&nsc);
528
529 if (fd >= 0) {
530 dso__list_add(dso);
531
532
533
534
535 check_data_close();
536 }
537
538 return fd;
539}
540
541static void close_data_fd(struct dso *dso)
542{
543 if (dso->data.fd >= 0) {
544 close(dso->data.fd);
545 dso->data.fd = -1;
546 dso->data.file_size = 0;
547 dso__list_del(dso);
548 }
549}
550
551
552
553
554
555
556
557
558static void close_dso(struct dso *dso)
559{
560 close_data_fd(dso);
561}
562
563static void close_first_dso(void)
564{
565 struct dso *dso;
566
567 dso = list_first_entry(&dso__data_open, struct dso, data.open_entry);
568 close_dso(dso);
569}
570
571static rlim_t get_fd_limit(void)
572{
573 struct rlimit l;
574 rlim_t limit = 0;
575
576
577 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
578 if (l.rlim_cur == RLIM_INFINITY)
579 limit = l.rlim_cur;
580 else
581 limit = l.rlim_cur / 2;
582 } else {
583 pr_err("failed to get fd limit\n");
584 limit = 1;
585 }
586
587 return limit;
588}
589
590static rlim_t fd_limit;
591
592
593
594
595
596
597void reset_fd_limit(void)
598{
599 fd_limit = 0;
600}
601
602static bool may_cache_fd(void)
603{
604 if (!fd_limit)
605 fd_limit = get_fd_limit();
606
607 if (fd_limit == RLIM_INFINITY)
608 return true;
609
610 return fd_limit > (rlim_t) dso__data_open_cnt;
611}
612
613
614
615
616
617
618static void check_data_close(void)
619{
620 bool cache_fd = may_cache_fd();
621
622 if (!cache_fd)
623 close_first_dso();
624}
625
626
627
628
629
630
631
632void dso__data_close(struct dso *dso)
633{
634 pthread_mutex_lock(&dso__data_open_lock);
635 close_dso(dso);
636 pthread_mutex_unlock(&dso__data_open_lock);
637}
638
639static void try_to_open_dso(struct dso *dso, struct machine *machine)
640{
641 enum dso_binary_type binary_type_data[] = {
642 DSO_BINARY_TYPE__BUILD_ID_CACHE,
643 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
644 DSO_BINARY_TYPE__NOT_FOUND,
645 };
646 int i = 0;
647
648 if (dso->data.fd >= 0)
649 return;
650
651 if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) {
652 dso->data.fd = open_dso(dso, machine);
653 goto out;
654 }
655
656 do {
657 dso->binary_type = binary_type_data[i++];
658
659 dso->data.fd = open_dso(dso, machine);
660 if (dso->data.fd >= 0)
661 goto out;
662
663 } while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND);
664out:
665 if (dso->data.fd >= 0)
666 dso->data.status = DSO_DATA_STATUS_OK;
667 else
668 dso->data.status = DSO_DATA_STATUS_ERROR;
669}
670
671
672
673
674
675
676
677
678
679
680int dso__data_get_fd(struct dso *dso, struct machine *machine)
681{
682 if (dso->data.status == DSO_DATA_STATUS_ERROR)
683 return -1;
684
685 if (pthread_mutex_lock(&dso__data_open_lock) < 0)
686 return -1;
687
688 try_to_open_dso(dso, machine);
689
690 if (dso->data.fd < 0)
691 pthread_mutex_unlock(&dso__data_open_lock);
692
693 return dso->data.fd;
694}
695
696void dso__data_put_fd(struct dso *dso __maybe_unused)
697{
698 pthread_mutex_unlock(&dso__data_open_lock);
699}
700
701bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by)
702{
703 u32 flag = 1 << by;
704
705 if (dso->data.status_seen & flag)
706 return true;
707
708 dso->data.status_seen |= flag;
709
710 return false;
711}
712
713static ssize_t bpf_read(struct dso *dso, u64 offset, char *data)
714{
715 struct bpf_prog_info_node *node;
716 ssize_t size = DSO__DATA_CACHE_SIZE;
717 u64 len;
718 u8 *buf;
719
720 node = perf_env__find_bpf_prog_info(dso->bpf_prog.env, dso->bpf_prog.id);
721 if (!node || !node->info_linear) {
722 dso->data.status = DSO_DATA_STATUS_ERROR;
723 return -1;
724 }
725
726 len = node->info_linear->info.jited_prog_len;
727 buf = (u8 *)(uintptr_t)node->info_linear->info.jited_prog_insns;
728
729 if (offset >= len)
730 return -1;
731
732 size = (ssize_t)min(len - offset, (u64)size);
733 memcpy(data, buf + offset, size);
734 return size;
735}
736
737static int bpf_size(struct dso *dso)
738{
739 struct bpf_prog_info_node *node;
740
741 node = perf_env__find_bpf_prog_info(dso->bpf_prog.env, dso->bpf_prog.id);
742 if (!node || !node->info_linear) {
743 dso->data.status = DSO_DATA_STATUS_ERROR;
744 return -1;
745 }
746
747 dso->data.file_size = node->info_linear->info.jited_prog_len;
748 return 0;
749}
750
751static void
752dso_cache__free(struct dso *dso)
753{
754 struct rb_root *root = &dso->data.cache;
755 struct rb_node *next = rb_first(root);
756
757 pthread_mutex_lock(&dso->lock);
758 while (next) {
759 struct dso_cache *cache;
760
761 cache = rb_entry(next, struct dso_cache, rb_node);
762 next = rb_next(&cache->rb_node);
763 rb_erase(&cache->rb_node, root);
764 free(cache);
765 }
766 pthread_mutex_unlock(&dso->lock);
767}
768
769static struct dso_cache *dso_cache__find(struct dso *dso, u64 offset)
770{
771 const struct rb_root *root = &dso->data.cache;
772 struct rb_node * const *p = &root->rb_node;
773 const struct rb_node *parent = NULL;
774 struct dso_cache *cache;
775
776 while (*p != NULL) {
777 u64 end;
778
779 parent = *p;
780 cache = rb_entry(parent, struct dso_cache, rb_node);
781 end = cache->offset + DSO__DATA_CACHE_SIZE;
782
783 if (offset < cache->offset)
784 p = &(*p)->rb_left;
785 else if (offset >= end)
786 p = &(*p)->rb_right;
787 else
788 return cache;
789 }
790
791 return NULL;
792}
793
794static struct dso_cache *
795dso_cache__insert(struct dso *dso, struct dso_cache *new)
796{
797 struct rb_root *root = &dso->data.cache;
798 struct rb_node **p = &root->rb_node;
799 struct rb_node *parent = NULL;
800 struct dso_cache *cache;
801 u64 offset = new->offset;
802
803 pthread_mutex_lock(&dso->lock);
804 while (*p != NULL) {
805 u64 end;
806
807 parent = *p;
808 cache = rb_entry(parent, struct dso_cache, rb_node);
809 end = cache->offset + DSO__DATA_CACHE_SIZE;
810
811 if (offset < cache->offset)
812 p = &(*p)->rb_left;
813 else if (offset >= end)
814 p = &(*p)->rb_right;
815 else
816 goto out;
817 }
818
819 rb_link_node(&new->rb_node, parent, p);
820 rb_insert_color(&new->rb_node, root);
821
822 cache = NULL;
823out:
824 pthread_mutex_unlock(&dso->lock);
825 return cache;
826}
827
828static ssize_t
829dso_cache__memcpy(struct dso_cache *cache, u64 offset,
830 u8 *data, u64 size)
831{
832 u64 cache_offset = offset - cache->offset;
833 u64 cache_size = min(cache->size - cache_offset, size);
834
835 memcpy(data, cache->data + cache_offset, cache_size);
836 return cache_size;
837}
838
839static ssize_t file_read(struct dso *dso, struct machine *machine,
840 u64 offset, char *data)
841{
842 ssize_t ret;
843
844 pthread_mutex_lock(&dso__data_open_lock);
845
846
847
848
849
850 try_to_open_dso(dso, machine);
851
852 if (dso->data.fd < 0) {
853 dso->data.status = DSO_DATA_STATUS_ERROR;
854 ret = -errno;
855 goto out;
856 }
857
858 ret = pread(dso->data.fd, data, DSO__DATA_CACHE_SIZE, offset);
859out:
860 pthread_mutex_unlock(&dso__data_open_lock);
861 return ret;
862}
863
864static ssize_t
865dso_cache__read(struct dso *dso, struct machine *machine,
866 u64 offset, u8 *data, ssize_t size)
867{
868 u64 cache_offset = offset & DSO__DATA_CACHE_MASK;
869 struct dso_cache *cache;
870 struct dso_cache *old;
871 ssize_t ret;
872
873 cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
874 if (!cache)
875 return -ENOMEM;
876
877 if (dso->binary_type == DSO_BINARY_TYPE__BPF_PROG_INFO)
878 ret = bpf_read(dso, cache_offset, cache->data);
879 else
880 ret = file_read(dso, machine, cache_offset, cache->data);
881
882 if (ret > 0) {
883 cache->offset = cache_offset;
884 cache->size = ret;
885
886 old = dso_cache__insert(dso, cache);
887 if (old) {
888
889 free(cache);
890 cache = old;
891 }
892
893 ret = dso_cache__memcpy(cache, offset, data, size);
894 }
895
896 if (ret <= 0)
897 free(cache);
898
899 return ret;
900}
901
902static ssize_t dso_cache_read(struct dso *dso, struct machine *machine,
903 u64 offset, u8 *data, ssize_t size)
904{
905 struct dso_cache *cache;
906
907 cache = dso_cache__find(dso, offset);
908 if (cache)
909 return dso_cache__memcpy(cache, offset, data, size);
910 else
911 return dso_cache__read(dso, machine, offset, data, size);
912}
913
914
915
916
917
918
919static ssize_t cached_read(struct dso *dso, struct machine *machine,
920 u64 offset, u8 *data, ssize_t size)
921{
922 ssize_t r = 0;
923 u8 *p = data;
924
925 do {
926 ssize_t ret;
927
928 ret = dso_cache_read(dso, machine, offset, p, size);
929 if (ret < 0)
930 return ret;
931
932
933 if (!ret)
934 break;
935
936 BUG_ON(ret > size);
937
938 r += ret;
939 p += ret;
940 offset += ret;
941 size -= ret;
942
943 } while (size);
944
945 return r;
946}
947
948static int file_size(struct dso *dso, struct machine *machine)
949{
950 int ret = 0;
951 struct stat st;
952 char sbuf[STRERR_BUFSIZE];
953
954 pthread_mutex_lock(&dso__data_open_lock);
955
956
957
958
959
960 try_to_open_dso(dso, machine);
961
962 if (dso->data.fd < 0) {
963 ret = -errno;
964 dso->data.status = DSO_DATA_STATUS_ERROR;
965 goto out;
966 }
967
968 if (fstat(dso->data.fd, &st) < 0) {
969 ret = -errno;
970 pr_err("dso cache fstat failed: %s\n",
971 str_error_r(errno, sbuf, sizeof(sbuf)));
972 dso->data.status = DSO_DATA_STATUS_ERROR;
973 goto out;
974 }
975 dso->data.file_size = st.st_size;
976
977out:
978 pthread_mutex_unlock(&dso__data_open_lock);
979 return ret;
980}
981
982int dso__data_file_size(struct dso *dso, struct machine *machine)
983{
984 if (dso->data.file_size)
985 return 0;
986
987 if (dso->data.status == DSO_DATA_STATUS_ERROR)
988 return -1;
989
990 if (dso->binary_type == DSO_BINARY_TYPE__BPF_PROG_INFO)
991 return bpf_size(dso);
992
993 return file_size(dso, machine);
994}
995
996
997
998
999
1000
1001
1002
1003off_t dso__data_size(struct dso *dso, struct machine *machine)
1004{
1005 if (dso__data_file_size(dso, machine))
1006 return -1;
1007
1008
1009 return dso->data.file_size;
1010}
1011
1012static ssize_t data_read_offset(struct dso *dso, struct machine *machine,
1013 u64 offset, u8 *data, ssize_t size)
1014{
1015 if (dso__data_file_size(dso, machine))
1016 return -1;
1017
1018
1019 if (offset > dso->data.file_size)
1020 return -1;
1021
1022 if (offset + size < offset)
1023 return -1;
1024
1025 return cached_read(dso, machine, offset, data, size);
1026}
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
1040 u64 offset, u8 *data, ssize_t size)
1041{
1042 if (dso->data.status == DSO_DATA_STATUS_ERROR)
1043 return -1;
1044
1045 return data_read_offset(dso, machine, offset, data, size);
1046}
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
1059 struct machine *machine, u64 addr,
1060 u8 *data, ssize_t size)
1061{
1062 u64 offset = map->map_ip(map, addr);
1063 return dso__data_read_offset(dso, machine, offset, data, size);
1064}
1065
1066struct map *dso__new_map(const char *name)
1067{
1068 struct map *map = NULL;
1069 struct dso *dso = dso__new(name);
1070
1071 if (dso)
1072 map = map__new2(0, dso);
1073
1074 return map;
1075}
1076
1077struct dso *machine__findnew_kernel(struct machine *machine, const char *name,
1078 const char *short_name, int dso_type)
1079{
1080
1081
1082
1083 struct dso *dso = machine__findnew_dso(machine, name);
1084
1085
1086
1087
1088
1089 if (dso != NULL) {
1090 dso__set_short_name(dso, short_name, false);
1091 dso->kernel = dso_type;
1092 }
1093
1094 return dso;
1095}
1096
1097
1098
1099
1100
1101
1102static struct dso *__dso__findlink_by_longname(struct rb_root *root,
1103 struct dso *dso, const char *name)
1104{
1105 struct rb_node **p = &root->rb_node;
1106 struct rb_node *parent = NULL;
1107
1108 if (!name)
1109 name = dso->long_name;
1110
1111
1112
1113 while (*p) {
1114 struct dso *this = rb_entry(*p, struct dso, rb_node);
1115 int rc = strcmp(name, this->long_name);
1116
1117 parent = *p;
1118 if (rc == 0) {
1119
1120
1121
1122
1123
1124 if (!dso || (dso == this))
1125 return this;
1126
1127
1128
1129
1130
1131 rc = strcmp(dso->short_name, this->short_name);
1132 if (rc == 0) {
1133 pr_err("Duplicated dso name: %s\n", name);
1134 return NULL;
1135 }
1136 }
1137 if (rc < 0)
1138 p = &parent->rb_left;
1139 else
1140 p = &parent->rb_right;
1141 }
1142 if (dso) {
1143
1144 rb_link_node(&dso->rb_node, parent, p);
1145 rb_insert_color(&dso->rb_node, root);
1146 dso->root = root;
1147 }
1148 return NULL;
1149}
1150
1151static inline struct dso *__dso__find_by_longname(struct rb_root *root,
1152 const char *name)
1153{
1154 return __dso__findlink_by_longname(root, NULL, name);
1155}
1156
1157void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
1158{
1159 struct rb_root *root = dso->root;
1160
1161 if (name == NULL)
1162 return;
1163
1164 if (dso->long_name_allocated)
1165 free((char *)dso->long_name);
1166
1167 if (root) {
1168 rb_erase(&dso->rb_node, root);
1169
1170
1171
1172
1173 RB_CLEAR_NODE(&dso->rb_node);
1174 dso->root = NULL;
1175 }
1176
1177 dso->long_name = name;
1178 dso->long_name_len = strlen(name);
1179 dso->long_name_allocated = name_allocated;
1180
1181 if (root)
1182 __dso__findlink_by_longname(root, dso, NULL);
1183}
1184
1185void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
1186{
1187 if (name == NULL)
1188 return;
1189
1190 if (dso->short_name_allocated)
1191 free((char *)dso->short_name);
1192
1193 dso->short_name = name;
1194 dso->short_name_len = strlen(name);
1195 dso->short_name_allocated = name_allocated;
1196}
1197
1198static void dso__set_basename(struct dso *dso)
1199{
1200 char *base, *lname;
1201 int tid;
1202
1203 if (sscanf(dso->long_name, "/tmp/perf-%d.map", &tid) == 1) {
1204 if (asprintf(&base, "[JIT] tid %d", tid) < 0)
1205 return;
1206 } else {
1207
1208
1209
1210
1211 lname = strdup(dso->long_name);
1212 if (!lname)
1213 return;
1214
1215
1216
1217
1218
1219
1220 base = strdup(basename(lname));
1221
1222 free(lname);
1223
1224 if (!base)
1225 return;
1226 }
1227 dso__set_short_name(dso, base, true);
1228}
1229
1230int dso__name_len(const struct dso *dso)
1231{
1232 if (!dso)
1233 return strlen("[unknown]");
1234 if (verbose > 0)
1235 return dso->long_name_len;
1236
1237 return dso->short_name_len;
1238}
1239
1240bool dso__loaded(const struct dso *dso)
1241{
1242 return dso->loaded;
1243}
1244
1245bool dso__sorted_by_name(const struct dso *dso)
1246{
1247 return dso->sorted_by_name;
1248}
1249
1250void dso__set_sorted_by_name(struct dso *dso)
1251{
1252 dso->sorted_by_name = true;
1253}
1254
1255struct dso *dso__new(const char *name)
1256{
1257 struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
1258
1259 if (dso != NULL) {
1260 strcpy(dso->name, name);
1261 dso__set_long_name(dso, dso->name, false);
1262 dso__set_short_name(dso, dso->name, false);
1263 dso->symbols = dso->symbol_names = RB_ROOT_CACHED;
1264 dso->data.cache = RB_ROOT;
1265 dso->inlined_nodes = RB_ROOT_CACHED;
1266 dso->srclines = RB_ROOT_CACHED;
1267 dso->data.fd = -1;
1268 dso->data.status = DSO_DATA_STATUS_UNKNOWN;
1269 dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
1270 dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
1271 dso->is_64_bit = (sizeof(void *) == 8);
1272 dso->loaded = 0;
1273 dso->rel = 0;
1274 dso->sorted_by_name = 0;
1275 dso->has_build_id = 0;
1276 dso->has_srcline = 1;
1277 dso->a2l_fails = 1;
1278 dso->kernel = DSO_TYPE_USER;
1279 dso->needs_swap = DSO_SWAP__UNSET;
1280 dso->comp = COMP_ID__NONE;
1281 RB_CLEAR_NODE(&dso->rb_node);
1282 dso->root = NULL;
1283 INIT_LIST_HEAD(&dso->node);
1284 INIT_LIST_HEAD(&dso->data.open_entry);
1285 pthread_mutex_init(&dso->lock, NULL);
1286 refcount_set(&dso->refcnt, 1);
1287 }
1288
1289 return dso;
1290}
1291
1292void dso__delete(struct dso *dso)
1293{
1294 if (!RB_EMPTY_NODE(&dso->rb_node))
1295 pr_err("DSO %s is still in rbtree when being deleted!\n",
1296 dso->long_name);
1297
1298
1299 inlines__tree_delete(&dso->inlined_nodes);
1300 srcline__tree_delete(&dso->srclines);
1301 symbols__delete(&dso->symbols);
1302
1303 if (dso->short_name_allocated) {
1304 zfree((char **)&dso->short_name);
1305 dso->short_name_allocated = false;
1306 }
1307
1308 if (dso->long_name_allocated) {
1309 zfree((char **)&dso->long_name);
1310 dso->long_name_allocated = false;
1311 }
1312
1313 dso__data_close(dso);
1314 auxtrace_cache__free(dso->auxtrace_cache);
1315 dso_cache__free(dso);
1316 dso__free_a2l(dso);
1317 zfree(&dso->symsrc_filename);
1318 nsinfo__zput(dso->nsinfo);
1319 pthread_mutex_destroy(&dso->lock);
1320 free(dso);
1321}
1322
1323struct dso *dso__get(struct dso *dso)
1324{
1325 if (dso)
1326 refcount_inc(&dso->refcnt);
1327 return dso;
1328}
1329
1330void dso__put(struct dso *dso)
1331{
1332 if (dso && refcount_dec_and_test(&dso->refcnt))
1333 dso__delete(dso);
1334}
1335
1336void dso__set_build_id(struct dso *dso, void *build_id)
1337{
1338 memcpy(dso->build_id, build_id, sizeof(dso->build_id));
1339 dso->has_build_id = 1;
1340}
1341
1342bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
1343{
1344 return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
1345}
1346
1347void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
1348{
1349 char path[PATH_MAX];
1350
1351 if (machine__is_default_guest(machine))
1352 return;
1353 sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
1354 if (sysfs__read_build_id(path, dso->build_id,
1355 sizeof(dso->build_id)) == 0)
1356 dso->has_build_id = true;
1357}
1358
1359int dso__kernel_module_get_build_id(struct dso *dso,
1360 const char *root_dir)
1361{
1362 char filename[PATH_MAX];
1363
1364
1365
1366
1367 const char *name = dso->short_name + 1;
1368
1369 snprintf(filename, sizeof(filename),
1370 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1371 root_dir, (int)strlen(name) - 1, name);
1372
1373 if (sysfs__read_build_id(filename, dso->build_id,
1374 sizeof(dso->build_id)) == 0)
1375 dso->has_build_id = true;
1376
1377 return 0;
1378}
1379
1380bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
1381{
1382 bool have_build_id = false;
1383 struct dso *pos;
1384 struct nscookie nsc;
1385
1386 list_for_each_entry(pos, head, node) {
1387 if (with_hits && !pos->hit && !dso__is_vdso(pos))
1388 continue;
1389 if (pos->has_build_id) {
1390 have_build_id = true;
1391 continue;
1392 }
1393 nsinfo__mountns_enter(pos->nsinfo, &nsc);
1394 if (filename__read_build_id(pos->long_name, pos->build_id,
1395 sizeof(pos->build_id)) > 0) {
1396 have_build_id = true;
1397 pos->has_build_id = true;
1398 }
1399 nsinfo__mountns_exit(&nsc);
1400 }
1401
1402 return have_build_id;
1403}
1404
1405void __dsos__add(struct dsos *dsos, struct dso *dso)
1406{
1407 list_add_tail(&dso->node, &dsos->head);
1408 __dso__findlink_by_longname(&dsos->root, dso, NULL);
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429 dso__get(dso);
1430}
1431
1432void dsos__add(struct dsos *dsos, struct dso *dso)
1433{
1434 down_write(&dsos->lock);
1435 __dsos__add(dsos, dso);
1436 up_write(&dsos->lock);
1437}
1438
1439struct dso *__dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
1440{
1441 struct dso *pos;
1442
1443 if (cmp_short) {
1444 list_for_each_entry(pos, &dsos->head, node)
1445 if (strcmp(pos->short_name, name) == 0)
1446 return pos;
1447 return NULL;
1448 }
1449 return __dso__find_by_longname(&dsos->root, name);
1450}
1451
1452struct dso *dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
1453{
1454 struct dso *dso;
1455 down_read(&dsos->lock);
1456 dso = __dsos__find(dsos, name, cmp_short);
1457 up_read(&dsos->lock);
1458 return dso;
1459}
1460
1461struct dso *__dsos__addnew(struct dsos *dsos, const char *name)
1462{
1463 struct dso *dso = dso__new(name);
1464
1465 if (dso != NULL) {
1466 __dsos__add(dsos, dso);
1467 dso__set_basename(dso);
1468
1469 dso__put(dso);
1470 }
1471 return dso;
1472}
1473
1474struct dso *__dsos__findnew(struct dsos *dsos, const char *name)
1475{
1476 struct dso *dso = __dsos__find(dsos, name, false);
1477
1478 return dso ? dso : __dsos__addnew(dsos, name);
1479}
1480
1481struct dso *dsos__findnew(struct dsos *dsos, const char *name)
1482{
1483 struct dso *dso;
1484 down_write(&dsos->lock);
1485 dso = dso__get(__dsos__findnew(dsos, name));
1486 up_write(&dsos->lock);
1487 return dso;
1488}
1489
1490size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
1491 bool (skip)(struct dso *dso, int parm), int parm)
1492{
1493 struct dso *pos;
1494 size_t ret = 0;
1495
1496 list_for_each_entry(pos, head, node) {
1497 if (skip && skip(pos, parm))
1498 continue;
1499 ret += dso__fprintf_buildid(pos, fp);
1500 ret += fprintf(fp, " %s\n", pos->long_name);
1501 }
1502 return ret;
1503}
1504
1505size_t __dsos__fprintf(struct list_head *head, FILE *fp)
1506{
1507 struct dso *pos;
1508 size_t ret = 0;
1509
1510 list_for_each_entry(pos, head, node) {
1511 ret += dso__fprintf(pos, fp);
1512 }
1513
1514 return ret;
1515}
1516
1517size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
1518{
1519 char sbuild_id[SBUILD_ID_SIZE];
1520
1521 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1522 return fprintf(fp, "%s", sbuild_id);
1523}
1524
1525size_t dso__fprintf(struct dso *dso, FILE *fp)
1526{
1527 struct rb_node *nd;
1528 size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
1529
1530 if (dso->short_name != dso->long_name)
1531 ret += fprintf(fp, "%s, ", dso->long_name);
1532 ret += fprintf(fp, "%sloaded, ", dso__loaded(dso) ? "" : "NOT ");
1533 ret += dso__fprintf_buildid(dso, fp);
1534 ret += fprintf(fp, ")\n");
1535 for (nd = rb_first_cached(&dso->symbols); nd; nd = rb_next(nd)) {
1536 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
1537 ret += symbol__fprintf(pos, fp);
1538 }
1539
1540 return ret;
1541}
1542
1543enum dso_type dso__type(struct dso *dso, struct machine *machine)
1544{
1545 int fd;
1546 enum dso_type type = DSO__TYPE_UNKNOWN;
1547
1548 fd = dso__data_get_fd(dso, machine);
1549 if (fd >= 0) {
1550 type = dso__type_fd(fd);
1551 dso__data_put_fd(dso);
1552 }
1553
1554 return type;
1555}
1556
1557int dso__strerror_load(struct dso *dso, char *buf, size_t buflen)
1558{
1559 int idx, errnum = dso->load_errno;
1560
1561
1562
1563 static const char *dso_load__error_str[] = {
1564 "Internal tools/perf/ library error",
1565 "Invalid ELF file",
1566 "Can not read build id",
1567 "Mismatching build id",
1568 "Decompression failure",
1569 };
1570
1571 BUG_ON(buflen == 0);
1572
1573 if (errnum >= 0) {
1574 const char *err = str_error_r(errnum, buf, buflen);
1575
1576 if (err != buf)
1577 scnprintf(buf, buflen, "%s", err);
1578
1579 return 0;
1580 }
1581
1582 if (errnum < __DSO_LOAD_ERRNO__START || errnum >= __DSO_LOAD_ERRNO__END)
1583 return -1;
1584
1585 idx = errnum - __DSO_LOAD_ERRNO__START;
1586 scnprintf(buf, buflen, "%s", dso_load__error_str[idx]);
1587 return 0;
1588}
1589