xref: /kernel/linux/linux-5.10/tools/perf/util/dso.c (revision 8c2ecf20)
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
32static const char * const debuglink_paths[] = {
33	"%.0s%s",
34	"%s/%s",
35	"%s/.debug/%s",
36	"/usr/lib/debug%s/%s"
37};
38
39char 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
67int 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->bid, build_id_hex);
176		len = __symbol__join_symfs(filename, size, "/usr/lib/debug/.build-id/");
177		snprintf(filename + len, size - len, "%.2s/%s.debug",
178			 build_id_hex, build_id_hex + 2);
179		break;
180
181	case DSO_BINARY_TYPE__VMLINUX:
182	case DSO_BINARY_TYPE__GUEST_VMLINUX:
183	case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
184		__symbol__join_symfs(filename, size, dso->long_name);
185		break;
186
187	case DSO_BINARY_TYPE__GUEST_KMODULE:
188	case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
189		path__join3(filename, size, symbol_conf.symfs,
190			    root_dir, dso->long_name);
191		break;
192
193	case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
194	case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
195		__symbol__join_symfs(filename, size, dso->long_name);
196		break;
197
198	case DSO_BINARY_TYPE__KCORE:
199	case DSO_BINARY_TYPE__GUEST_KCORE:
200		snprintf(filename, size, "%s", dso->long_name);
201		break;
202
203	default:
204	case DSO_BINARY_TYPE__KALLSYMS:
205	case DSO_BINARY_TYPE__GUEST_KALLSYMS:
206	case DSO_BINARY_TYPE__JAVA_JIT:
207	case DSO_BINARY_TYPE__BPF_PROG_INFO:
208	case DSO_BINARY_TYPE__BPF_IMAGE:
209	case DSO_BINARY_TYPE__OOL:
210	case DSO_BINARY_TYPE__NOT_FOUND:
211		ret = -1;
212		break;
213	}
214
215	return ret;
216}
217
218enum {
219	COMP_ID__NONE = 0,
220};
221
222static 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
237static 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
248bool 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
274bool 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
280static 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
329int dso__decompress_kmodule_fd(struct dso *dso, const char *name)
330{
331	return decompress_kmodule(dso, name, NULL, 0);
332}
333
334int 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 */
359int __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
423void 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 */
443static LIST_HEAD(dso__data_open);
444static long dso__data_open_cnt;
445static pthread_mutex_t dso__data_open_lock = PTHREAD_MUTEX_INITIALIZER;
446
447static 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
453static 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
461static void close_first_dso(void);
462
463static 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
484static 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
522out:
523	free(name);
524	return fd;
525}
526
527static 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 */
536static 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
559static 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 */
576static void close_dso(struct dso *dso)
577{
578	close_data_fd(dso);
579}
580
581static 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
589static 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
608static 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 */
615void reset_fd_limit(void)
616{
617	fd_limit = 0;
618}
619
620static 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*/
636static 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 */
650void 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
657static 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);
682out:
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 */
698int 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
714void dso__data_put_fd(struct dso *dso __maybe_unused)
715{
716	pthread_mutex_unlock(&dso__data_open_lock);
717}
718
719bool 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
731static 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
755static 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
769static void
770dso_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
787static 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
812static struct dso_cache *
813dso_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;
841out:
842	pthread_mutex_unlock(&dso->lock);
843	return cache;
844}
845
846static ssize_t dso_cache__memcpy(struct dso_cache *cache, u64 offset, u8 *data,
847				 u64 size, bool out)
848{
849	u64 cache_offset = offset - cache->offset;
850	u64 cache_size   = min(cache->size - cache_offset, size);
851
852	if (out)
853		memcpy(data, cache->data + cache_offset, cache_size);
854	else
855		memcpy(cache->data + cache_offset, data, cache_size);
856	return cache_size;
857}
858
859static ssize_t file_read(struct dso *dso, struct machine *machine,
860			 u64 offset, char *data)
861{
862	ssize_t ret;
863
864	pthread_mutex_lock(&dso__data_open_lock);
865
866	/*
867	 * dso->data.fd might be closed if other thread opened another
868	 * file (dso) due to open file limit (RLIMIT_NOFILE).
869	 */
870	try_to_open_dso(dso, machine);
871
872	if (dso->data.fd < 0) {
873		dso->data.status = DSO_DATA_STATUS_ERROR;
874		ret = -errno;
875		goto out;
876	}
877
878	ret = pread(dso->data.fd, data, DSO__DATA_CACHE_SIZE, offset);
879out:
880	pthread_mutex_unlock(&dso__data_open_lock);
881	return ret;
882}
883
884static struct dso_cache *dso_cache__populate(struct dso *dso,
885					     struct machine *machine,
886					     u64 offset, ssize_t *ret)
887{
888	u64 cache_offset = offset & DSO__DATA_CACHE_MASK;
889	struct dso_cache *cache;
890	struct dso_cache *old;
891
892	cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
893	if (!cache) {
894		*ret = -ENOMEM;
895		return NULL;
896	}
897
898	if (dso->binary_type == DSO_BINARY_TYPE__BPF_PROG_INFO)
899		*ret = bpf_read(dso, cache_offset, cache->data);
900	else if (dso->binary_type == DSO_BINARY_TYPE__OOL)
901		*ret = DSO__DATA_CACHE_SIZE;
902	else
903		*ret = file_read(dso, machine, cache_offset, cache->data);
904
905	if (*ret <= 0) {
906		free(cache);
907		return NULL;
908	}
909
910	cache->offset = cache_offset;
911	cache->size   = *ret;
912
913	old = dso_cache__insert(dso, cache);
914	if (old) {
915		/* we lose the race */
916		free(cache);
917		cache = old;
918	}
919
920	return cache;
921}
922
923static struct dso_cache *dso_cache__find(struct dso *dso,
924					 struct machine *machine,
925					 u64 offset,
926					 ssize_t *ret)
927{
928	struct dso_cache *cache = __dso_cache__find(dso, offset);
929
930	return cache ? cache : dso_cache__populate(dso, machine, offset, ret);
931}
932
933static ssize_t dso_cache_io(struct dso *dso, struct machine *machine,
934			    u64 offset, u8 *data, ssize_t size, bool out)
935{
936	struct dso_cache *cache;
937	ssize_t ret = 0;
938
939	cache = dso_cache__find(dso, machine, offset, &ret);
940	if (!cache)
941		return ret;
942
943	return dso_cache__memcpy(cache, offset, data, size, out);
944}
945
946/*
947 * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
948 * in the rb_tree. Any read to already cached data is served
949 * by cached data. Writes update the cache only, not the backing file.
950 */
951static ssize_t cached_io(struct dso *dso, struct machine *machine,
952			 u64 offset, u8 *data, ssize_t size, bool out)
953{
954	ssize_t r = 0;
955	u8 *p = data;
956
957	do {
958		ssize_t ret;
959
960		ret = dso_cache_io(dso, machine, offset, p, size, out);
961		if (ret < 0)
962			return ret;
963
964		/* Reached EOF, return what we have. */
965		if (!ret)
966			break;
967
968		BUG_ON(ret > size);
969
970		r      += ret;
971		p      += ret;
972		offset += ret;
973		size   -= ret;
974
975	} while (size);
976
977	return r;
978}
979
980static int file_size(struct dso *dso, struct machine *machine)
981{
982	int ret = 0;
983	struct stat st;
984	char sbuf[STRERR_BUFSIZE];
985
986	pthread_mutex_lock(&dso__data_open_lock);
987
988	/*
989	 * dso->data.fd might be closed if other thread opened another
990	 * file (dso) due to open file limit (RLIMIT_NOFILE).
991	 */
992	try_to_open_dso(dso, machine);
993
994	if (dso->data.fd < 0) {
995		ret = -errno;
996		dso->data.status = DSO_DATA_STATUS_ERROR;
997		goto out;
998	}
999
1000	if (fstat(dso->data.fd, &st) < 0) {
1001		ret = -errno;
1002		pr_err("dso cache fstat failed: %s\n",
1003		       str_error_r(errno, sbuf, sizeof(sbuf)));
1004		dso->data.status = DSO_DATA_STATUS_ERROR;
1005		goto out;
1006	}
1007	dso->data.file_size = st.st_size;
1008
1009out:
1010	pthread_mutex_unlock(&dso__data_open_lock);
1011	return ret;
1012}
1013
1014int dso__data_file_size(struct dso *dso, struct machine *machine)
1015{
1016	if (dso->data.file_size)
1017		return 0;
1018
1019	if (dso->data.status == DSO_DATA_STATUS_ERROR)
1020		return -1;
1021
1022	if (dso->binary_type == DSO_BINARY_TYPE__BPF_PROG_INFO)
1023		return bpf_size(dso);
1024
1025	return file_size(dso, machine);
1026}
1027
1028/**
1029 * dso__data_size - Return dso data size
1030 * @dso: dso object
1031 * @machine: machine object
1032 *
1033 * Return: dso data size
1034 */
1035off_t dso__data_size(struct dso *dso, struct machine *machine)
1036{
1037	if (dso__data_file_size(dso, machine))
1038		return -1;
1039
1040	/* For now just estimate dso data size is close to file size */
1041	return dso->data.file_size;
1042}
1043
1044static ssize_t data_read_write_offset(struct dso *dso, struct machine *machine,
1045				      u64 offset, u8 *data, ssize_t size,
1046				      bool out)
1047{
1048	if (dso__data_file_size(dso, machine))
1049		return -1;
1050
1051	/* Check the offset sanity. */
1052	if (offset > dso->data.file_size)
1053		return -1;
1054
1055	if (offset + size < offset)
1056		return -1;
1057
1058	return cached_io(dso, machine, offset, data, size, out);
1059}
1060
1061/**
1062 * dso__data_read_offset - Read data from dso file offset
1063 * @dso: dso object
1064 * @machine: machine object
1065 * @offset: file offset
1066 * @data: buffer to store data
1067 * @size: size of the @data buffer
1068 *
1069 * External interface to read data from dso file offset. Open
1070 * dso data file and use cached_read to get the data.
1071 */
1072ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
1073			      u64 offset, u8 *data, ssize_t size)
1074{
1075	if (dso->data.status == DSO_DATA_STATUS_ERROR)
1076		return -1;
1077
1078	return data_read_write_offset(dso, machine, offset, data, size, true);
1079}
1080
1081/**
1082 * dso__data_read_addr - Read data from dso address
1083 * @dso: dso object
1084 * @machine: machine object
1085 * @add: virtual memory address
1086 * @data: buffer to store data
1087 * @size: size of the @data buffer
1088 *
1089 * External interface to read data from dso address.
1090 */
1091ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
1092			    struct machine *machine, u64 addr,
1093			    u8 *data, ssize_t size)
1094{
1095	u64 offset = map->map_ip(map, addr);
1096	return dso__data_read_offset(dso, machine, offset, data, size);
1097}
1098
1099/**
1100 * dso__data_write_cache_offs - Write data to dso data cache at file offset
1101 * @dso: dso object
1102 * @machine: machine object
1103 * @offset: file offset
1104 * @data: buffer to write
1105 * @size: size of the @data buffer
1106 *
1107 * Write into the dso file data cache, but do not change the file itself.
1108 */
1109ssize_t dso__data_write_cache_offs(struct dso *dso, struct machine *machine,
1110				   u64 offset, const u8 *data_in, ssize_t size)
1111{
1112	u8 *data = (u8 *)data_in; /* cast away const to use same fns for r/w */
1113
1114	if (dso->data.status == DSO_DATA_STATUS_ERROR)
1115		return -1;
1116
1117	return data_read_write_offset(dso, machine, offset, data, size, false);
1118}
1119
1120/**
1121 * dso__data_write_cache_addr - Write data to dso data cache at dso address
1122 * @dso: dso object
1123 * @machine: machine object
1124 * @add: virtual memory address
1125 * @data: buffer to write
1126 * @size: size of the @data buffer
1127 *
1128 * External interface to write into the dso file data cache, but do not change
1129 * the file itself.
1130 */
1131ssize_t dso__data_write_cache_addr(struct dso *dso, struct map *map,
1132				   struct machine *machine, u64 addr,
1133				   const u8 *data, ssize_t size)
1134{
1135	u64 offset = map->map_ip(map, addr);
1136	return dso__data_write_cache_offs(dso, machine, offset, data, size);
1137}
1138
1139struct map *dso__new_map(const char *name)
1140{
1141	struct map *map = NULL;
1142	struct dso *dso = dso__new(name);
1143
1144	if (dso) {
1145		map = map__new2(0, dso);
1146		dso__put(dso);
1147	}
1148
1149	return map;
1150}
1151
1152struct dso *machine__findnew_kernel(struct machine *machine, const char *name,
1153				    const char *short_name, int dso_type)
1154{
1155	/*
1156	 * The kernel dso could be created by build_id processing.
1157	 */
1158	struct dso *dso = machine__findnew_dso(machine, name);
1159
1160	/*
1161	 * We need to run this in all cases, since during the build_id
1162	 * processing we had no idea this was the kernel dso.
1163	 */
1164	if (dso != NULL) {
1165		dso__set_short_name(dso, short_name, false);
1166		dso->kernel = dso_type;
1167	}
1168
1169	return dso;
1170}
1171
1172static void dso__set_long_name_id(struct dso *dso, const char *name, struct dso_id *id, bool name_allocated)
1173{
1174	struct rb_root *root = dso->root;
1175
1176	if (name == NULL)
1177		return;
1178
1179	if (dso->long_name_allocated)
1180		free((char *)dso->long_name);
1181
1182	if (root) {
1183		rb_erase(&dso->rb_node, root);
1184		/*
1185		 * __dsos__findnew_link_by_longname_id() isn't guaranteed to
1186		 * add it back, so a clean removal is required here.
1187		 */
1188		RB_CLEAR_NODE(&dso->rb_node);
1189		dso->root = NULL;
1190	}
1191
1192	dso->long_name		 = name;
1193	dso->long_name_len	 = strlen(name);
1194	dso->long_name_allocated = name_allocated;
1195
1196	if (root)
1197		__dsos__findnew_link_by_longname_id(root, dso, NULL, id);
1198}
1199
1200void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
1201{
1202	dso__set_long_name_id(dso, name, NULL, name_allocated);
1203}
1204
1205void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
1206{
1207	if (name == NULL)
1208		return;
1209
1210	if (dso->short_name_allocated)
1211		free((char *)dso->short_name);
1212
1213	dso->short_name		  = name;
1214	dso->short_name_len	  = strlen(name);
1215	dso->short_name_allocated = name_allocated;
1216}
1217
1218int dso__name_len(const struct dso *dso)
1219{
1220	if (!dso)
1221		return strlen("[unknown]");
1222	if (verbose > 0)
1223		return dso->long_name_len;
1224
1225	return dso->short_name_len;
1226}
1227
1228bool dso__loaded(const struct dso *dso)
1229{
1230	return dso->loaded;
1231}
1232
1233bool dso__sorted_by_name(const struct dso *dso)
1234{
1235	return dso->sorted_by_name;
1236}
1237
1238void dso__set_sorted_by_name(struct dso *dso)
1239{
1240	dso->sorted_by_name = true;
1241}
1242
1243struct dso *dso__new_id(const char *name, struct dso_id *id)
1244{
1245	struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
1246
1247	if (dso != NULL) {
1248		strcpy(dso->name, name);
1249		if (id)
1250			dso->id = *id;
1251		dso__set_long_name_id(dso, dso->name, id, false);
1252		dso__set_short_name(dso, dso->name, false);
1253		dso->symbols = dso->symbol_names = RB_ROOT_CACHED;
1254		dso->data.cache = RB_ROOT;
1255		dso->inlined_nodes = RB_ROOT_CACHED;
1256		dso->srclines = RB_ROOT_CACHED;
1257		dso->data.fd = -1;
1258		dso->data.status = DSO_DATA_STATUS_UNKNOWN;
1259		dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
1260		dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
1261		dso->is_64_bit = (sizeof(void *) == 8);
1262		dso->loaded = 0;
1263		dso->rel = 0;
1264		dso->sorted_by_name = 0;
1265		dso->has_build_id = 0;
1266		dso->has_srcline = 1;
1267		dso->a2l_fails = 1;
1268		dso->kernel = DSO_SPACE__USER;
1269		dso->needs_swap = DSO_SWAP__UNSET;
1270		dso->comp = COMP_ID__NONE;
1271		RB_CLEAR_NODE(&dso->rb_node);
1272		dso->root = NULL;
1273		INIT_LIST_HEAD(&dso->node);
1274		INIT_LIST_HEAD(&dso->data.open_entry);
1275		pthread_mutex_init(&dso->lock, NULL);
1276		refcount_set(&dso->refcnt, 1);
1277	}
1278
1279	return dso;
1280}
1281
1282struct dso *dso__new(const char *name)
1283{
1284	return dso__new_id(name, NULL);
1285}
1286
1287void dso__delete(struct dso *dso)
1288{
1289	if (!RB_EMPTY_NODE(&dso->rb_node))
1290		pr_err("DSO %s is still in rbtree when being deleted!\n",
1291		       dso->long_name);
1292
1293	/* free inlines first, as they reference symbols */
1294	inlines__tree_delete(&dso->inlined_nodes);
1295	srcline__tree_delete(&dso->srclines);
1296	symbols__delete(&dso->symbols);
1297
1298	if (dso->short_name_allocated) {
1299		zfree((char **)&dso->short_name);
1300		dso->short_name_allocated = false;
1301	}
1302
1303	if (dso->long_name_allocated) {
1304		zfree((char **)&dso->long_name);
1305		dso->long_name_allocated = false;
1306	}
1307
1308	dso__data_close(dso);
1309	auxtrace_cache__free(dso->auxtrace_cache);
1310	dso_cache__free(dso);
1311	dso__free_a2l(dso);
1312	zfree(&dso->symsrc_filename);
1313	nsinfo__zput(dso->nsinfo);
1314	pthread_mutex_destroy(&dso->lock);
1315	free(dso);
1316}
1317
1318struct dso *dso__get(struct dso *dso)
1319{
1320	if (dso)
1321		refcount_inc(&dso->refcnt);
1322	return dso;
1323}
1324
1325void dso__put(struct dso *dso)
1326{
1327	if (dso && refcount_dec_and_test(&dso->refcnt))
1328		dso__delete(dso);
1329}
1330
1331void dso__set_build_id(struct dso *dso, struct build_id *bid)
1332{
1333	dso->bid = *bid;
1334	dso->has_build_id = 1;
1335}
1336
1337bool dso__build_id_equal(const struct dso *dso, struct build_id *bid)
1338{
1339	if (dso->bid.size > bid->size && dso->bid.size == BUILD_ID_SIZE) {
1340		/*
1341		 * For the backward compatibility, it allows a build-id has
1342		 * trailing zeros.
1343		 */
1344		return !memcmp(dso->bid.data, bid->data, bid->size) &&
1345			!memchr_inv(&dso->bid.data[bid->size], 0,
1346				    dso->bid.size - bid->size);
1347	}
1348
1349	return dso->bid.size == bid->size &&
1350	       memcmp(dso->bid.data, bid->data, dso->bid.size) == 0;
1351}
1352
1353void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
1354{
1355	char path[PATH_MAX];
1356
1357	if (machine__is_default_guest(machine))
1358		return;
1359	sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
1360	if (sysfs__read_build_id(path, &dso->bid) == 0)
1361		dso->has_build_id = true;
1362}
1363
1364int dso__kernel_module_get_build_id(struct dso *dso,
1365				    const char *root_dir)
1366{
1367	char filename[PATH_MAX];
1368	/*
1369	 * kernel module short names are of the form "[module]" and
1370	 * we need just "module" here.
1371	 */
1372	const char *name = dso->short_name + 1;
1373
1374	snprintf(filename, sizeof(filename),
1375		 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1376		 root_dir, (int)strlen(name) - 1, name);
1377
1378	if (sysfs__read_build_id(filename, &dso->bid) == 0)
1379		dso->has_build_id = true;
1380
1381	return 0;
1382}
1383
1384static size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
1385{
1386	char sbuild_id[SBUILD_ID_SIZE];
1387
1388	build_id__sprintf(&dso->bid, sbuild_id);
1389	return fprintf(fp, "%s", sbuild_id);
1390}
1391
1392size_t dso__fprintf(struct dso *dso, FILE *fp)
1393{
1394	struct rb_node *nd;
1395	size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
1396
1397	if (dso->short_name != dso->long_name)
1398		ret += fprintf(fp, "%s, ", dso->long_name);
1399	ret += fprintf(fp, "%sloaded, ", dso__loaded(dso) ? "" : "NOT ");
1400	ret += dso__fprintf_buildid(dso, fp);
1401	ret += fprintf(fp, ")\n");
1402	for (nd = rb_first_cached(&dso->symbols); nd; nd = rb_next(nd)) {
1403		struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
1404		ret += symbol__fprintf(pos, fp);
1405	}
1406
1407	return ret;
1408}
1409
1410enum dso_type dso__type(struct dso *dso, struct machine *machine)
1411{
1412	int fd;
1413	enum dso_type type = DSO__TYPE_UNKNOWN;
1414
1415	fd = dso__data_get_fd(dso, machine);
1416	if (fd >= 0) {
1417		type = dso__type_fd(fd);
1418		dso__data_put_fd(dso);
1419	}
1420
1421	return type;
1422}
1423
1424int dso__strerror_load(struct dso *dso, char *buf, size_t buflen)
1425{
1426	int idx, errnum = dso->load_errno;
1427	/*
1428	 * This must have a same ordering as the enum dso_load_errno.
1429	 */
1430	static const char *dso_load__error_str[] = {
1431	"Internal tools/perf/ library error",
1432	"Invalid ELF file",
1433	"Can not read build id",
1434	"Mismatching build id",
1435	"Decompression failure",
1436	};
1437
1438	BUG_ON(buflen == 0);
1439
1440	if (errnum >= 0) {
1441		const char *err = str_error_r(errnum, buf, buflen);
1442
1443		if (err != buf)
1444			scnprintf(buf, buflen, "%s", err);
1445
1446		return 0;
1447	}
1448
1449	if (errnum <  __DSO_LOAD_ERRNO__START || errnum >= __DSO_LOAD_ERRNO__END)
1450		return -1;
1451
1452	idx = errnum - __DSO_LOAD_ERRNO__START;
1453	scnprintf(buf, buflen, "%s", dso_load__error_str[idx]);
1454	return 0;
1455}
1456