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