xref: /linux/tools/perf/util/dso.c (revision fa0d98462fae5d4951f22f3ac1090d48c53396d1)
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 <bpf/libbpf.h>
14 #include "bpf-event.h"
15 #include "compress.h"
16 #include "namespaces.h"
17 #include "path.h"
18 #include "map.h"
19 #include "symbol.h"
20 #include "srcline.h"
21 #include "dso.h"
22 #include "dsos.h"
23 #include "machine.h"
24 #include "auxtrace.h"
25 #include "util.h" /* O_CLOEXEC for older systems */
26 #include "debug.h"
27 #include "string2.h"
28 #include "vdso.h"
29 
30 static const char * const debuglink_paths[] = {
31 	"%.0s%s",
32 	"%s/%s",
33 	"%s/.debug/%s",
34 	"/usr/lib/debug%s/%s"
35 };
36 
37 char 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 
64 int 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 		/* Check predefined locations where debug file might reside */
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 
200 enum {
201 	COMP_ID__NONE = 0,
202 };
203 
204 static 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 
219 static 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 
230 bool 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 	/* Treat PERF_RECORD_MISC_CPUMODE_UNKNOWN as kernel */
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 
256 bool 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 
262 static 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 	 * We have proper compression id for DSO and yet the file
276 	 * behind the 'name' can still be plain uncompressed object.
277 	 *
278 	 * The reason is behind the logic we open the DSO object files,
279 	 * when we try all possible 'debug' objects until we find the
280 	 * data. So even if the DSO is represented by 'krava.xz' module,
281 	 * we can end up here opening ~/.debug/....23432432/debug' file
282 	 * which is not compressed.
283 	 *
284 	 * To keep this transparent, we detect this and return the file
285 	 * descriptor to the uncompressed file.
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 
311 int dso__decompress_kmodule_fd(struct dso *dso, const char *name)
312 {
313 	return decompress_kmodule(dso, name, NULL, 0);
314 }
315 
316 int 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  * Parses kernel module specified in @path and updates
327  * @m argument like:
328  *
329  *    @comp - true if @path contains supported compression suffix,
330  *            false otherwise
331  *    @kmod - true if @path contains '.ko' suffix in right position,
332  *            false otherwise
333  *    @name - if (@alloc_name && @kmod) is true, it contains strdup-ed base name
334  *            of the kernel module without suffixes, otherwise strudup-ed
335  *            base name of @path
336  *    @ext  - if (@alloc_ext && @comp) is true, it contains strdup-ed string
337  *            the compression suffix
338  *
339  * Returns 0 if there's no strdup error, -ENOMEM otherwise.
340  */
341 int __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 	 * '.' is also a valid character for module name. For example:
353 	 * [aaa.bbb] is a valid module name. '[' should have higher
354 	 * priority than '.ko' suffix.
355 	 *
356 	 * The kernel names are from machine__mmap_name. Such
357 	 * name should belong to kernel itself, not kernel module.
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 	/* No extension, just return name. */
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 	/* Check .ko extension only if there's enough name left. */
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 
405 void 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 	/* _KMODULE_COMP should be next to _KMODULE */
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  * Global list of open DSOs and the counter.
424  */
425 static LIST_HEAD(dso__data_open);
426 static long dso__data_open_cnt;
427 static pthread_mutex_t dso__data_open_lock = PTHREAD_MUTEX_INITIALIZER;
428 
429 static 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 
435 static 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 
443 static void close_first_dso(void);
444 
445 static 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 
466 static 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 
504 out:
505 	free(name);
506 	return fd;
507 }
508 
509 static void check_data_close(void);
510 
511 /**
512  * dso_close - Open DSO data file
513  * @dso: dso object
514  *
515  * Open @dso's data file descriptor and updates
516  * list/count of open DSO objects.
517  */
518 static 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 		 * Check if we crossed the allowed number
533 		 * of opened DSOs and close one if needed.
534 		 */
535 		check_data_close();
536 	}
537 
538 	return fd;
539 }
540 
541 static 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  * dso_close - Close DSO data file
553  * @dso: dso object
554  *
555  * Close @dso's data file descriptor and updates
556  * list/count of open DSO objects.
557  */
558 static void close_dso(struct dso *dso)
559 {
560 	close_data_fd(dso);
561 }
562 
563 static 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 
571 static rlim_t get_fd_limit(void)
572 {
573 	struct rlimit l;
574 	rlim_t limit = 0;
575 
576 	/* Allow half of the current open fd limit. */
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 
590 static rlim_t fd_limit;
591 
592 /*
593  * Used only by tests/dso-data.c to reset the environment
594  * for tests. I dont expect we should change this during
595  * standard runtime.
596  */
597 void reset_fd_limit(void)
598 {
599 	fd_limit = 0;
600 }
601 
602 static 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  * Check and close LRU dso if we crossed allowed limit
615  * for opened dso file descriptors. The limit is half
616  * of the RLIMIT_NOFILE files opened.
617 */
618 static 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  * dso__data_close - Close DSO data file
628  * @dso: dso object
629  *
630  * External interface to close @dso's data file descriptor.
631  */
632 void 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 
639 static 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);
664 out:
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  * dso__data_get_fd - Get dso's data file descriptor
673  * @dso: dso object
674  * @machine: machine object
675  *
676  * External interface to find dso's file, open it and
677  * returns file descriptor.  It should be paired with
678  * dso__data_put_fd() if it returns non-negative value.
679  */
680 int 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 
696 void dso__data_put_fd(struct dso *dso __maybe_unused)
697 {
698 	pthread_mutex_unlock(&dso__data_open_lock);
699 }
700 
701 bool 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 
713 static 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 
737 static 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 
751 static void
752 dso_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 
769 static 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 
794 static struct dso_cache *
795 dso_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;
823 out:
824 	pthread_mutex_unlock(&dso->lock);
825 	return cache;
826 }
827 
828 static ssize_t
829 dso_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 
839 static 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 	 * dso->data.fd might be closed if other thread opened another
848 	 * file (dso) due to open file limit (RLIMIT_NOFILE).
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);
859 out:
860 	pthread_mutex_unlock(&dso__data_open_lock);
861 	return ret;
862 }
863 
864 static ssize_t
865 dso_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 			/* we lose the race */
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 
902 static 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  * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
916  * in the rb_tree. Any read to already cached data is served
917  * by cached data.
918  */
919 static 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 		/* Reached EOF, return what we have. */
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 
948 static 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 	 * dso->data.fd might be closed if other thread opened another
958 	 * file (dso) due to open file limit (RLIMIT_NOFILE).
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 
977 out:
978 	pthread_mutex_unlock(&dso__data_open_lock);
979 	return ret;
980 }
981 
982 int 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  * dso__data_size - Return dso data size
998  * @dso: dso object
999  * @machine: machine object
1000  *
1001  * Return: dso data size
1002  */
1003 off_t dso__data_size(struct dso *dso, struct machine *machine)
1004 {
1005 	if (dso__data_file_size(dso, machine))
1006 		return -1;
1007 
1008 	/* For now just estimate dso data size is close to file size */
1009 	return dso->data.file_size;
1010 }
1011 
1012 static 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 	/* Check the offset sanity. */
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  * dso__data_read_offset - Read data from dso file offset
1030  * @dso: dso object
1031  * @machine: machine object
1032  * @offset: file offset
1033  * @data: buffer to store data
1034  * @size: size of the @data buffer
1035  *
1036  * External interface to read data from dso file offset. Open
1037  * dso data file and use cached_read to get the data.
1038  */
1039 ssize_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  * dso__data_read_addr - Read data from dso address
1050  * @dso: dso object
1051  * @machine: machine object
1052  * @add: virtual memory address
1053  * @data: buffer to store data
1054  * @size: size of the @data buffer
1055  *
1056  * External interface to read data from dso address.
1057  */
1058 ssize_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 
1066 struct 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 
1077 struct dso *machine__findnew_kernel(struct machine *machine, const char *name,
1078 				    const char *short_name, int dso_type)
1079 {
1080 	/*
1081 	 * The kernel dso could be created by build_id processing.
1082 	 */
1083 	struct dso *dso = machine__findnew_dso(machine, name);
1084 
1085 	/*
1086 	 * We need to run this in all cases, since during the build_id
1087 	 * processing we had no idea this was the kernel dso.
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 void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
1098 {
1099 	struct rb_root *root = dso->root;
1100 
1101 	if (name == NULL)
1102 		return;
1103 
1104 	if (dso->long_name_allocated)
1105 		free((char *)dso->long_name);
1106 
1107 	if (root) {
1108 		rb_erase(&dso->rb_node, root);
1109 		/*
1110 		 * __dsos__findnew_link_by_longname() isn't guaranteed to add it
1111 		 * back, so a clean removal is required here.
1112 		 */
1113 		RB_CLEAR_NODE(&dso->rb_node);
1114 		dso->root = NULL;
1115 	}
1116 
1117 	dso->long_name		 = name;
1118 	dso->long_name_len	 = strlen(name);
1119 	dso->long_name_allocated = name_allocated;
1120 
1121 	if (root)
1122 		__dsos__findnew_link_by_longname(root, dso, NULL);
1123 }
1124 
1125 void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
1126 {
1127 	if (name == NULL)
1128 		return;
1129 
1130 	if (dso->short_name_allocated)
1131 		free((char *)dso->short_name);
1132 
1133 	dso->short_name		  = name;
1134 	dso->short_name_len	  = strlen(name);
1135 	dso->short_name_allocated = name_allocated;
1136 }
1137 
1138 int dso__name_len(const struct dso *dso)
1139 {
1140 	if (!dso)
1141 		return strlen("[unknown]");
1142 	if (verbose > 0)
1143 		return dso->long_name_len;
1144 
1145 	return dso->short_name_len;
1146 }
1147 
1148 bool dso__loaded(const struct dso *dso)
1149 {
1150 	return dso->loaded;
1151 }
1152 
1153 bool dso__sorted_by_name(const struct dso *dso)
1154 {
1155 	return dso->sorted_by_name;
1156 }
1157 
1158 void dso__set_sorted_by_name(struct dso *dso)
1159 {
1160 	dso->sorted_by_name = true;
1161 }
1162 
1163 struct dso *dso__new(const char *name)
1164 {
1165 	struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
1166 
1167 	if (dso != NULL) {
1168 		strcpy(dso->name, name);
1169 		dso__set_long_name(dso, dso->name, false);
1170 		dso__set_short_name(dso, dso->name, false);
1171 		dso->symbols = dso->symbol_names = RB_ROOT_CACHED;
1172 		dso->data.cache = RB_ROOT;
1173 		dso->inlined_nodes = RB_ROOT_CACHED;
1174 		dso->srclines = RB_ROOT_CACHED;
1175 		dso->data.fd = -1;
1176 		dso->data.status = DSO_DATA_STATUS_UNKNOWN;
1177 		dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
1178 		dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
1179 		dso->is_64_bit = (sizeof(void *) == 8);
1180 		dso->loaded = 0;
1181 		dso->rel = 0;
1182 		dso->sorted_by_name = 0;
1183 		dso->has_build_id = 0;
1184 		dso->has_srcline = 1;
1185 		dso->a2l_fails = 1;
1186 		dso->kernel = DSO_TYPE_USER;
1187 		dso->needs_swap = DSO_SWAP__UNSET;
1188 		dso->comp = COMP_ID__NONE;
1189 		RB_CLEAR_NODE(&dso->rb_node);
1190 		dso->root = NULL;
1191 		INIT_LIST_HEAD(&dso->node);
1192 		INIT_LIST_HEAD(&dso->data.open_entry);
1193 		pthread_mutex_init(&dso->lock, NULL);
1194 		refcount_set(&dso->refcnt, 1);
1195 	}
1196 
1197 	return dso;
1198 }
1199 
1200 void dso__delete(struct dso *dso)
1201 {
1202 	if (!RB_EMPTY_NODE(&dso->rb_node))
1203 		pr_err("DSO %s is still in rbtree when being deleted!\n",
1204 		       dso->long_name);
1205 
1206 	/* free inlines first, as they reference symbols */
1207 	inlines__tree_delete(&dso->inlined_nodes);
1208 	srcline__tree_delete(&dso->srclines);
1209 	symbols__delete(&dso->symbols);
1210 
1211 	if (dso->short_name_allocated) {
1212 		zfree((char **)&dso->short_name);
1213 		dso->short_name_allocated = false;
1214 	}
1215 
1216 	if (dso->long_name_allocated) {
1217 		zfree((char **)&dso->long_name);
1218 		dso->long_name_allocated = false;
1219 	}
1220 
1221 	dso__data_close(dso);
1222 	auxtrace_cache__free(dso->auxtrace_cache);
1223 	dso_cache__free(dso);
1224 	dso__free_a2l(dso);
1225 	zfree(&dso->symsrc_filename);
1226 	nsinfo__zput(dso->nsinfo);
1227 	pthread_mutex_destroy(&dso->lock);
1228 	free(dso);
1229 }
1230 
1231 struct dso *dso__get(struct dso *dso)
1232 {
1233 	if (dso)
1234 		refcount_inc(&dso->refcnt);
1235 	return dso;
1236 }
1237 
1238 void dso__put(struct dso *dso)
1239 {
1240 	if (dso && refcount_dec_and_test(&dso->refcnt))
1241 		dso__delete(dso);
1242 }
1243 
1244 void dso__set_build_id(struct dso *dso, void *build_id)
1245 {
1246 	memcpy(dso->build_id, build_id, sizeof(dso->build_id));
1247 	dso->has_build_id = 1;
1248 }
1249 
1250 bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
1251 {
1252 	return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
1253 }
1254 
1255 void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
1256 {
1257 	char path[PATH_MAX];
1258 
1259 	if (machine__is_default_guest(machine))
1260 		return;
1261 	sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
1262 	if (sysfs__read_build_id(path, dso->build_id,
1263 				 sizeof(dso->build_id)) == 0)
1264 		dso->has_build_id = true;
1265 }
1266 
1267 int dso__kernel_module_get_build_id(struct dso *dso,
1268 				    const char *root_dir)
1269 {
1270 	char filename[PATH_MAX];
1271 	/*
1272 	 * kernel module short names are of the form "[module]" and
1273 	 * we need just "module" here.
1274 	 */
1275 	const char *name = dso->short_name + 1;
1276 
1277 	snprintf(filename, sizeof(filename),
1278 		 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1279 		 root_dir, (int)strlen(name) - 1, name);
1280 
1281 	if (sysfs__read_build_id(filename, dso->build_id,
1282 				 sizeof(dso->build_id)) == 0)
1283 		dso->has_build_id = true;
1284 
1285 	return 0;
1286 }
1287 
1288 size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
1289 {
1290 	char sbuild_id[SBUILD_ID_SIZE];
1291 
1292 	build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1293 	return fprintf(fp, "%s", sbuild_id);
1294 }
1295 
1296 size_t dso__fprintf(struct dso *dso, FILE *fp)
1297 {
1298 	struct rb_node *nd;
1299 	size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
1300 
1301 	if (dso->short_name != dso->long_name)
1302 		ret += fprintf(fp, "%s, ", dso->long_name);
1303 	ret += fprintf(fp, "%sloaded, ", dso__loaded(dso) ? "" : "NOT ");
1304 	ret += dso__fprintf_buildid(dso, fp);
1305 	ret += fprintf(fp, ")\n");
1306 	for (nd = rb_first_cached(&dso->symbols); nd; nd = rb_next(nd)) {
1307 		struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
1308 		ret += symbol__fprintf(pos, fp);
1309 	}
1310 
1311 	return ret;
1312 }
1313 
1314 enum dso_type dso__type(struct dso *dso, struct machine *machine)
1315 {
1316 	int fd;
1317 	enum dso_type type = DSO__TYPE_UNKNOWN;
1318 
1319 	fd = dso__data_get_fd(dso, machine);
1320 	if (fd >= 0) {
1321 		type = dso__type_fd(fd);
1322 		dso__data_put_fd(dso);
1323 	}
1324 
1325 	return type;
1326 }
1327 
1328 int dso__strerror_load(struct dso *dso, char *buf, size_t buflen)
1329 {
1330 	int idx, errnum = dso->load_errno;
1331 	/*
1332 	 * This must have a same ordering as the enum dso_load_errno.
1333 	 */
1334 	static const char *dso_load__error_str[] = {
1335 	"Internal tools/perf/ library error",
1336 	"Invalid ELF file",
1337 	"Can not read build id",
1338 	"Mismatching build id",
1339 	"Decompression failure",
1340 	};
1341 
1342 	BUG_ON(buflen == 0);
1343 
1344 	if (errnum >= 0) {
1345 		const char *err = str_error_r(errnum, buf, buflen);
1346 
1347 		if (err != buf)
1348 			scnprintf(buf, buflen, "%s", err);
1349 
1350 		return 0;
1351 	}
1352 
1353 	if (errnum <  __DSO_LOAD_ERRNO__START || errnum >= __DSO_LOAD_ERRNO__END)
1354 		return -1;
1355 
1356 	idx = errnum - __DSO_LOAD_ERRNO__START;
1357 	scnprintf(buf, buflen, "%s", dso_load__error_str[idx]);
1358 	return 0;
1359 }
1360