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