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