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