xref: /openbmc/linux/tools/perf/util/dso.c (revision d2999e1b)
1 #include <asm/bug.h>
2 #include <sys/time.h>
3 #include <sys/resource.h>
4 #include "symbol.h"
5 #include "dso.h"
6 #include "machine.h"
7 #include "util.h"
8 #include "debug.h"
9 
10 char dso__symtab_origin(const struct dso *dso)
11 {
12 	static const char origin[] = {
13 		[DSO_BINARY_TYPE__KALLSYMS]			= 'k',
14 		[DSO_BINARY_TYPE__VMLINUX]			= 'v',
15 		[DSO_BINARY_TYPE__JAVA_JIT]			= 'j',
16 		[DSO_BINARY_TYPE__DEBUGLINK]			= 'l',
17 		[DSO_BINARY_TYPE__BUILD_ID_CACHE]		= 'B',
18 		[DSO_BINARY_TYPE__FEDORA_DEBUGINFO]		= 'f',
19 		[DSO_BINARY_TYPE__UBUNTU_DEBUGINFO]		= 'u',
20 		[DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO]	= 'o',
21 		[DSO_BINARY_TYPE__BUILDID_DEBUGINFO]		= 'b',
22 		[DSO_BINARY_TYPE__SYSTEM_PATH_DSO]		= 'd',
23 		[DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE]		= 'K',
24 		[DSO_BINARY_TYPE__GUEST_KALLSYMS]		= 'g',
25 		[DSO_BINARY_TYPE__GUEST_KMODULE]		= 'G',
26 		[DSO_BINARY_TYPE__GUEST_VMLINUX]		= 'V',
27 	};
28 
29 	if (dso == NULL || dso->symtab_type == DSO_BINARY_TYPE__NOT_FOUND)
30 		return '!';
31 	return origin[dso->symtab_type];
32 }
33 
34 int dso__read_binary_type_filename(const struct dso *dso,
35 				   enum dso_binary_type type,
36 				   char *root_dir, char *filename, size_t size)
37 {
38 	char build_id_hex[BUILD_ID_SIZE * 2 + 1];
39 	int ret = 0;
40 
41 	switch (type) {
42 	case DSO_BINARY_TYPE__DEBUGLINK: {
43 		char *debuglink;
44 
45 		strncpy(filename, dso->long_name, size);
46 		debuglink = filename + dso->long_name_len;
47 		while (debuglink != filename && *debuglink != '/')
48 			debuglink--;
49 		if (*debuglink == '/')
50 			debuglink++;
51 		ret = filename__read_debuglink(dso->long_name, debuglink,
52 					       size - (debuglink - filename));
53 		}
54 		break;
55 	case DSO_BINARY_TYPE__BUILD_ID_CACHE:
56 		/* skip the locally configured cache if a symfs is given */
57 		if (symbol_conf.symfs[0] ||
58 		    (dso__build_id_filename(dso, filename, size) == NULL))
59 			ret = -1;
60 		break;
61 
62 	case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
63 		snprintf(filename, size, "%s/usr/lib/debug%s.debug",
64 			 symbol_conf.symfs, dso->long_name);
65 		break;
66 
67 	case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
68 		snprintf(filename, size, "%s/usr/lib/debug%s",
69 			 symbol_conf.symfs, dso->long_name);
70 		break;
71 
72 	case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
73 	{
74 		const char *last_slash;
75 		size_t len;
76 		size_t dir_size;
77 
78 		last_slash = dso->long_name + dso->long_name_len;
79 		while (last_slash != dso->long_name && *last_slash != '/')
80 			last_slash--;
81 
82 		len = scnprintf(filename, size, "%s", symbol_conf.symfs);
83 		dir_size = last_slash - dso->long_name + 2;
84 		if (dir_size > (size - len)) {
85 			ret = -1;
86 			break;
87 		}
88 		len += scnprintf(filename + len, dir_size, "%s",  dso->long_name);
89 		len += scnprintf(filename + len , size - len, ".debug%s",
90 								last_slash);
91 		break;
92 	}
93 
94 	case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
95 		if (!dso->has_build_id) {
96 			ret = -1;
97 			break;
98 		}
99 
100 		build_id__sprintf(dso->build_id,
101 				  sizeof(dso->build_id),
102 				  build_id_hex);
103 		snprintf(filename, size,
104 			 "%s/usr/lib/debug/.build-id/%.2s/%s.debug",
105 			 symbol_conf.symfs, build_id_hex, build_id_hex + 2);
106 		break;
107 
108 	case DSO_BINARY_TYPE__VMLINUX:
109 	case DSO_BINARY_TYPE__GUEST_VMLINUX:
110 	case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
111 		snprintf(filename, size, "%s%s",
112 			 symbol_conf.symfs, dso->long_name);
113 		break;
114 
115 	case DSO_BINARY_TYPE__GUEST_KMODULE:
116 		snprintf(filename, size, "%s%s%s", symbol_conf.symfs,
117 			 root_dir, dso->long_name);
118 		break;
119 
120 	case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
121 		snprintf(filename, size, "%s%s", symbol_conf.symfs,
122 			 dso->long_name);
123 		break;
124 
125 	case DSO_BINARY_TYPE__KCORE:
126 	case DSO_BINARY_TYPE__GUEST_KCORE:
127 		snprintf(filename, size, "%s", dso->long_name);
128 		break;
129 
130 	default:
131 	case DSO_BINARY_TYPE__KALLSYMS:
132 	case DSO_BINARY_TYPE__GUEST_KALLSYMS:
133 	case DSO_BINARY_TYPE__JAVA_JIT:
134 	case DSO_BINARY_TYPE__NOT_FOUND:
135 		ret = -1;
136 		break;
137 	}
138 
139 	return ret;
140 }
141 
142 /*
143  * Global list of open DSOs and the counter.
144  */
145 static LIST_HEAD(dso__data_open);
146 static long dso__data_open_cnt;
147 
148 static void dso__list_add(struct dso *dso)
149 {
150 	list_add_tail(&dso->data.open_entry, &dso__data_open);
151 	dso__data_open_cnt++;
152 }
153 
154 static void dso__list_del(struct dso *dso)
155 {
156 	list_del(&dso->data.open_entry);
157 	WARN_ONCE(dso__data_open_cnt <= 0,
158 		  "DSO data fd counter out of bounds.");
159 	dso__data_open_cnt--;
160 }
161 
162 static void close_first_dso(void);
163 
164 static int do_open(char *name)
165 {
166 	int fd;
167 
168 	do {
169 		fd = open(name, O_RDONLY);
170 		if (fd >= 0)
171 			return fd;
172 
173 		pr_debug("dso open failed, mmap: %s\n", strerror(errno));
174 		if (!dso__data_open_cnt || errno != EMFILE)
175 			break;
176 
177 		close_first_dso();
178 	} while (1);
179 
180 	return -1;
181 }
182 
183 static int __open_dso(struct dso *dso, struct machine *machine)
184 {
185 	int fd;
186 	char *root_dir = (char *)"";
187 	char *name = malloc(PATH_MAX);
188 
189 	if (!name)
190 		return -ENOMEM;
191 
192 	if (machine)
193 		root_dir = machine->root_dir;
194 
195 	if (dso__read_binary_type_filename(dso, dso->binary_type,
196 					    root_dir, name, PATH_MAX)) {
197 		free(name);
198 		return -EINVAL;
199 	}
200 
201 	fd = do_open(name);
202 	free(name);
203 	return fd;
204 }
205 
206 static void check_data_close(void);
207 
208 /**
209  * dso_close - Open DSO data file
210  * @dso: dso object
211  *
212  * Open @dso's data file descriptor and updates
213  * list/count of open DSO objects.
214  */
215 static int open_dso(struct dso *dso, struct machine *machine)
216 {
217 	int fd = __open_dso(dso, machine);
218 
219 	if (fd > 0) {
220 		dso__list_add(dso);
221 		/*
222 		 * Check if we crossed the allowed number
223 		 * of opened DSOs and close one if needed.
224 		 */
225 		check_data_close();
226 	}
227 
228 	return fd;
229 }
230 
231 static void close_data_fd(struct dso *dso)
232 {
233 	if (dso->data.fd >= 0) {
234 		close(dso->data.fd);
235 		dso->data.fd = -1;
236 		dso->data.file_size = 0;
237 		dso__list_del(dso);
238 	}
239 }
240 
241 /**
242  * dso_close - Close DSO data file
243  * @dso: dso object
244  *
245  * Close @dso's data file descriptor and updates
246  * list/count of open DSO objects.
247  */
248 static void close_dso(struct dso *dso)
249 {
250 	close_data_fd(dso);
251 }
252 
253 static void close_first_dso(void)
254 {
255 	struct dso *dso;
256 
257 	dso = list_first_entry(&dso__data_open, struct dso, data.open_entry);
258 	close_dso(dso);
259 }
260 
261 static rlim_t get_fd_limit(void)
262 {
263 	struct rlimit l;
264 	rlim_t limit = 0;
265 
266 	/* Allow half of the current open fd limit. */
267 	if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
268 		if (l.rlim_cur == RLIM_INFINITY)
269 			limit = l.rlim_cur;
270 		else
271 			limit = l.rlim_cur / 2;
272 	} else {
273 		pr_err("failed to get fd limit\n");
274 		limit = 1;
275 	}
276 
277 	return limit;
278 }
279 
280 static bool may_cache_fd(void)
281 {
282 	static rlim_t limit;
283 
284 	if (!limit)
285 		limit = get_fd_limit();
286 
287 	if (limit == RLIM_INFINITY)
288 		return true;
289 
290 	return limit > (rlim_t) dso__data_open_cnt;
291 }
292 
293 /*
294  * Check and close LRU dso if we crossed allowed limit
295  * for opened dso file descriptors. The limit is half
296  * of the RLIMIT_NOFILE files opened.
297 */
298 static void check_data_close(void)
299 {
300 	bool cache_fd = may_cache_fd();
301 
302 	if (!cache_fd)
303 		close_first_dso();
304 }
305 
306 /**
307  * dso__data_close - Close DSO data file
308  * @dso: dso object
309  *
310  * External interface to close @dso's data file descriptor.
311  */
312 void dso__data_close(struct dso *dso)
313 {
314 	close_dso(dso);
315 }
316 
317 /**
318  * dso__data_fd - Get dso's data file descriptor
319  * @dso: dso object
320  * @machine: machine object
321  *
322  * External interface to find dso's file, open it and
323  * returns file descriptor.
324  */
325 int dso__data_fd(struct dso *dso, struct machine *machine)
326 {
327 	enum dso_binary_type binary_type_data[] = {
328 		DSO_BINARY_TYPE__BUILD_ID_CACHE,
329 		DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
330 		DSO_BINARY_TYPE__NOT_FOUND,
331 	};
332 	int i = 0;
333 
334 	if (dso->data.fd >= 0)
335 		return dso->data.fd;
336 
337 	if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) {
338 		dso->data.fd = open_dso(dso, machine);
339 		return dso->data.fd;
340 	}
341 
342 	do {
343 		int fd;
344 
345 		dso->binary_type = binary_type_data[i++];
346 
347 		fd = open_dso(dso, machine);
348 		if (fd >= 0)
349 			return dso->data.fd = fd;
350 
351 	} while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND);
352 
353 	return -EINVAL;
354 }
355 
356 static void
357 dso_cache__free(struct rb_root *root)
358 {
359 	struct rb_node *next = rb_first(root);
360 
361 	while (next) {
362 		struct dso_cache *cache;
363 
364 		cache = rb_entry(next, struct dso_cache, rb_node);
365 		next = rb_next(&cache->rb_node);
366 		rb_erase(&cache->rb_node, root);
367 		free(cache);
368 	}
369 }
370 
371 static struct dso_cache *dso_cache__find(const struct rb_root *root, u64 offset)
372 {
373 	struct rb_node * const *p = &root->rb_node;
374 	const struct rb_node *parent = NULL;
375 	struct dso_cache *cache;
376 
377 	while (*p != NULL) {
378 		u64 end;
379 
380 		parent = *p;
381 		cache = rb_entry(parent, struct dso_cache, rb_node);
382 		end = cache->offset + DSO__DATA_CACHE_SIZE;
383 
384 		if (offset < cache->offset)
385 			p = &(*p)->rb_left;
386 		else if (offset >= end)
387 			p = &(*p)->rb_right;
388 		else
389 			return cache;
390 	}
391 	return NULL;
392 }
393 
394 static void
395 dso_cache__insert(struct rb_root *root, struct dso_cache *new)
396 {
397 	struct rb_node **p = &root->rb_node;
398 	struct rb_node *parent = NULL;
399 	struct dso_cache *cache;
400 	u64 offset = new->offset;
401 
402 	while (*p != NULL) {
403 		u64 end;
404 
405 		parent = *p;
406 		cache = rb_entry(parent, struct dso_cache, rb_node);
407 		end = cache->offset + DSO__DATA_CACHE_SIZE;
408 
409 		if (offset < cache->offset)
410 			p = &(*p)->rb_left;
411 		else if (offset >= end)
412 			p = &(*p)->rb_right;
413 	}
414 
415 	rb_link_node(&new->rb_node, parent, p);
416 	rb_insert_color(&new->rb_node, root);
417 }
418 
419 static ssize_t
420 dso_cache__memcpy(struct dso_cache *cache, u64 offset,
421 		  u8 *data, u64 size)
422 {
423 	u64 cache_offset = offset - cache->offset;
424 	u64 cache_size   = min(cache->size - cache_offset, size);
425 
426 	memcpy(data, cache->data + cache_offset, cache_size);
427 	return cache_size;
428 }
429 
430 static ssize_t
431 dso_cache__read(struct dso *dso, u64 offset, u8 *data, ssize_t size)
432 {
433 	struct dso_cache *cache;
434 	ssize_t ret;
435 
436 	do {
437 		u64 cache_offset;
438 
439 		ret = -ENOMEM;
440 
441 		cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
442 		if (!cache)
443 			break;
444 
445 		cache_offset = offset & DSO__DATA_CACHE_MASK;
446 		ret = -EINVAL;
447 
448 		if (-1 == lseek(dso->data.fd, cache_offset, SEEK_SET))
449 			break;
450 
451 		ret = read(dso->data.fd, cache->data, DSO__DATA_CACHE_SIZE);
452 		if (ret <= 0)
453 			break;
454 
455 		cache->offset = cache_offset;
456 		cache->size   = ret;
457 		dso_cache__insert(&dso->data.cache, cache);
458 
459 		ret = dso_cache__memcpy(cache, offset, data, size);
460 
461 	} while (0);
462 
463 	if (ret <= 0)
464 		free(cache);
465 
466 	return ret;
467 }
468 
469 static ssize_t dso_cache_read(struct dso *dso, u64 offset,
470 			      u8 *data, ssize_t size)
471 {
472 	struct dso_cache *cache;
473 
474 	cache = dso_cache__find(&dso->data.cache, offset);
475 	if (cache)
476 		return dso_cache__memcpy(cache, offset, data, size);
477 	else
478 		return dso_cache__read(dso, offset, data, size);
479 }
480 
481 /*
482  * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
483  * in the rb_tree. Any read to already cached data is served
484  * by cached data.
485  */
486 static ssize_t cached_read(struct dso *dso, u64 offset, u8 *data, ssize_t size)
487 {
488 	ssize_t r = 0;
489 	u8 *p = data;
490 
491 	do {
492 		ssize_t ret;
493 
494 		ret = dso_cache_read(dso, offset, p, size);
495 		if (ret < 0)
496 			return ret;
497 
498 		/* Reached EOF, return what we have. */
499 		if (!ret)
500 			break;
501 
502 		BUG_ON(ret > size);
503 
504 		r      += ret;
505 		p      += ret;
506 		offset += ret;
507 		size   -= ret;
508 
509 	} while (size);
510 
511 	return r;
512 }
513 
514 static int data_file_size(struct dso *dso)
515 {
516 	struct stat st;
517 
518 	if (!dso->data.file_size) {
519 		if (fstat(dso->data.fd, &st)) {
520 			pr_err("dso mmap failed, fstat: %s\n", strerror(errno));
521 			return -1;
522 		}
523 		dso->data.file_size = st.st_size;
524 	}
525 
526 	return 0;
527 }
528 
529 static ssize_t data_read_offset(struct dso *dso, u64 offset,
530 				u8 *data, ssize_t size)
531 {
532 	if (data_file_size(dso))
533 		return -1;
534 
535 	/* Check the offset sanity. */
536 	if (offset > dso->data.file_size)
537 		return -1;
538 
539 	if (offset + size < offset)
540 		return -1;
541 
542 	return cached_read(dso, offset, data, size);
543 }
544 
545 /**
546  * dso__data_read_offset - Read data from dso file offset
547  * @dso: dso object
548  * @machine: machine object
549  * @offset: file offset
550  * @data: buffer to store data
551  * @size: size of the @data buffer
552  *
553  * External interface to read data from dso file offset. Open
554  * dso data file and use cached_read to get the data.
555  */
556 ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
557 			      u64 offset, u8 *data, ssize_t size)
558 {
559 	if (dso__data_fd(dso, machine) < 0)
560 		return -1;
561 
562 	return data_read_offset(dso, offset, data, size);
563 }
564 
565 /**
566  * dso__data_read_addr - Read data from dso address
567  * @dso: dso object
568  * @machine: machine object
569  * @add: virtual memory address
570  * @data: buffer to store data
571  * @size: size of the @data buffer
572  *
573  * External interface to read data from dso address.
574  */
575 ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
576 			    struct machine *machine, u64 addr,
577 			    u8 *data, ssize_t size)
578 {
579 	u64 offset = map->map_ip(map, addr);
580 	return dso__data_read_offset(dso, machine, offset, data, size);
581 }
582 
583 struct map *dso__new_map(const char *name)
584 {
585 	struct map *map = NULL;
586 	struct dso *dso = dso__new(name);
587 
588 	if (dso)
589 		map = map__new2(0, dso, MAP__FUNCTION);
590 
591 	return map;
592 }
593 
594 struct dso *dso__kernel_findnew(struct machine *machine, const char *name,
595 		    const char *short_name, int dso_type)
596 {
597 	/*
598 	 * The kernel dso could be created by build_id processing.
599 	 */
600 	struct dso *dso = __dsos__findnew(&machine->kernel_dsos, name);
601 
602 	/*
603 	 * We need to run this in all cases, since during the build_id
604 	 * processing we had no idea this was the kernel dso.
605 	 */
606 	if (dso != NULL) {
607 		dso__set_short_name(dso, short_name, false);
608 		dso->kernel = dso_type;
609 	}
610 
611 	return dso;
612 }
613 
614 void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
615 {
616 	if (name == NULL)
617 		return;
618 
619 	if (dso->long_name_allocated)
620 		free((char *)dso->long_name);
621 
622 	dso->long_name		 = name;
623 	dso->long_name_len	 = strlen(name);
624 	dso->long_name_allocated = name_allocated;
625 }
626 
627 void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
628 {
629 	if (name == NULL)
630 		return;
631 
632 	if (dso->short_name_allocated)
633 		free((char *)dso->short_name);
634 
635 	dso->short_name		  = name;
636 	dso->short_name_len	  = strlen(name);
637 	dso->short_name_allocated = name_allocated;
638 }
639 
640 static void dso__set_basename(struct dso *dso)
641 {
642        /*
643         * basename() may modify path buffer, so we must pass
644         * a copy.
645         */
646        char *base, *lname = strdup(dso->long_name);
647 
648        if (!lname)
649                return;
650 
651        /*
652         * basename() may return a pointer to internal
653         * storage which is reused in subsequent calls
654         * so copy the result.
655         */
656        base = strdup(basename(lname));
657 
658        free(lname);
659 
660        if (!base)
661                return;
662 
663        dso__set_short_name(dso, base, true);
664 }
665 
666 int dso__name_len(const struct dso *dso)
667 {
668 	if (!dso)
669 		return strlen("[unknown]");
670 	if (verbose)
671 		return dso->long_name_len;
672 
673 	return dso->short_name_len;
674 }
675 
676 bool dso__loaded(const struct dso *dso, enum map_type type)
677 {
678 	return dso->loaded & (1 << type);
679 }
680 
681 bool dso__sorted_by_name(const struct dso *dso, enum map_type type)
682 {
683 	return dso->sorted_by_name & (1 << type);
684 }
685 
686 void dso__set_sorted_by_name(struct dso *dso, enum map_type type)
687 {
688 	dso->sorted_by_name |= (1 << type);
689 }
690 
691 struct dso *dso__new(const char *name)
692 {
693 	struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
694 
695 	if (dso != NULL) {
696 		int i;
697 		strcpy(dso->name, name);
698 		dso__set_long_name(dso, dso->name, false);
699 		dso__set_short_name(dso, dso->name, false);
700 		for (i = 0; i < MAP__NR_TYPES; ++i)
701 			dso->symbols[i] = dso->symbol_names[i] = RB_ROOT;
702 		dso->data.cache = RB_ROOT;
703 		dso->data.fd = -1;
704 		dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
705 		dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
706 		dso->loaded = 0;
707 		dso->rel = 0;
708 		dso->sorted_by_name = 0;
709 		dso->has_build_id = 0;
710 		dso->has_srcline = 1;
711 		dso->a2l_fails = 1;
712 		dso->kernel = DSO_TYPE_USER;
713 		dso->needs_swap = DSO_SWAP__UNSET;
714 		INIT_LIST_HEAD(&dso->node);
715 		INIT_LIST_HEAD(&dso->data.open_entry);
716 	}
717 
718 	return dso;
719 }
720 
721 void dso__delete(struct dso *dso)
722 {
723 	int i;
724 	for (i = 0; i < MAP__NR_TYPES; ++i)
725 		symbols__delete(&dso->symbols[i]);
726 
727 	if (dso->short_name_allocated) {
728 		zfree((char **)&dso->short_name);
729 		dso->short_name_allocated = false;
730 	}
731 
732 	if (dso->long_name_allocated) {
733 		zfree((char **)&dso->long_name);
734 		dso->long_name_allocated = false;
735 	}
736 
737 	dso__data_close(dso);
738 	dso_cache__free(&dso->data.cache);
739 	dso__free_a2l(dso);
740 	zfree(&dso->symsrc_filename);
741 	free(dso);
742 }
743 
744 void dso__set_build_id(struct dso *dso, void *build_id)
745 {
746 	memcpy(dso->build_id, build_id, sizeof(dso->build_id));
747 	dso->has_build_id = 1;
748 }
749 
750 bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
751 {
752 	return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
753 }
754 
755 void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
756 {
757 	char path[PATH_MAX];
758 
759 	if (machine__is_default_guest(machine))
760 		return;
761 	sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
762 	if (sysfs__read_build_id(path, dso->build_id,
763 				 sizeof(dso->build_id)) == 0)
764 		dso->has_build_id = true;
765 }
766 
767 int dso__kernel_module_get_build_id(struct dso *dso,
768 				    const char *root_dir)
769 {
770 	char filename[PATH_MAX];
771 	/*
772 	 * kernel module short names are of the form "[module]" and
773 	 * we need just "module" here.
774 	 */
775 	const char *name = dso->short_name + 1;
776 
777 	snprintf(filename, sizeof(filename),
778 		 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
779 		 root_dir, (int)strlen(name) - 1, name);
780 
781 	if (sysfs__read_build_id(filename, dso->build_id,
782 				 sizeof(dso->build_id)) == 0)
783 		dso->has_build_id = true;
784 
785 	return 0;
786 }
787 
788 bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
789 {
790 	bool have_build_id = false;
791 	struct dso *pos;
792 
793 	list_for_each_entry(pos, head, node) {
794 		if (with_hits && !pos->hit)
795 			continue;
796 		if (pos->has_build_id) {
797 			have_build_id = true;
798 			continue;
799 		}
800 		if (filename__read_build_id(pos->long_name, pos->build_id,
801 					    sizeof(pos->build_id)) > 0) {
802 			have_build_id	  = true;
803 			pos->has_build_id = true;
804 		}
805 	}
806 
807 	return have_build_id;
808 }
809 
810 void dsos__add(struct list_head *head, struct dso *dso)
811 {
812 	list_add_tail(&dso->node, head);
813 }
814 
815 struct dso *dsos__find(const struct list_head *head, const char *name, bool cmp_short)
816 {
817 	struct dso *pos;
818 
819 	if (cmp_short) {
820 		list_for_each_entry(pos, head, node)
821 			if (strcmp(pos->short_name, name) == 0)
822 				return pos;
823 		return NULL;
824 	}
825 	list_for_each_entry(pos, head, node)
826 		if (strcmp(pos->long_name, name) == 0)
827 			return pos;
828 	return NULL;
829 }
830 
831 struct dso *__dsos__findnew(struct list_head *head, const char *name)
832 {
833 	struct dso *dso = dsos__find(head, name, false);
834 
835 	if (!dso) {
836 		dso = dso__new(name);
837 		if (dso != NULL) {
838 			dsos__add(head, dso);
839 			dso__set_basename(dso);
840 		}
841 	}
842 
843 	return dso;
844 }
845 
846 size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
847 			       bool (skip)(struct dso *dso, int parm), int parm)
848 {
849 	struct dso *pos;
850 	size_t ret = 0;
851 
852 	list_for_each_entry(pos, head, node) {
853 		if (skip && skip(pos, parm))
854 			continue;
855 		ret += dso__fprintf_buildid(pos, fp);
856 		ret += fprintf(fp, " %s\n", pos->long_name);
857 	}
858 	return ret;
859 }
860 
861 size_t __dsos__fprintf(struct list_head *head, FILE *fp)
862 {
863 	struct dso *pos;
864 	size_t ret = 0;
865 
866 	list_for_each_entry(pos, head, node) {
867 		int i;
868 		for (i = 0; i < MAP__NR_TYPES; ++i)
869 			ret += dso__fprintf(pos, i, fp);
870 	}
871 
872 	return ret;
873 }
874 
875 size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
876 {
877 	char sbuild_id[BUILD_ID_SIZE * 2 + 1];
878 
879 	build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
880 	return fprintf(fp, "%s", sbuild_id);
881 }
882 
883 size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp)
884 {
885 	struct rb_node *nd;
886 	size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
887 
888 	if (dso->short_name != dso->long_name)
889 		ret += fprintf(fp, "%s, ", dso->long_name);
890 	ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
891 		       dso__loaded(dso, type) ? "" : "NOT ");
892 	ret += dso__fprintf_buildid(dso, fp);
893 	ret += fprintf(fp, ")\n");
894 	for (nd = rb_first(&dso->symbols[type]); nd; nd = rb_next(nd)) {
895 		struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
896 		ret += symbol__fprintf(pos, fp);
897 	}
898 
899 	return ret;
900 }
901