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