xref: /openbmc/linux/tools/perf/util/symbol.c (revision b24413180f5600bcb3bb70fbed5cf186b60864bd)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <dirent.h>
3 #include <errno.h>
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <string.h>
7 #include <linux/kernel.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <sys/param.h>
11 #include <fcntl.h>
12 #include <unistd.h>
13 #include <inttypes.h>
14 #include "annotate.h"
15 #include "build-id.h"
16 #include "util.h"
17 #include "debug.h"
18 #include "machine.h"
19 #include "symbol.h"
20 #include "strlist.h"
21 #include "intlist.h"
22 #include "namespaces.h"
23 #include "header.h"
24 #include "path.h"
25 #include "sane_ctype.h"
26 
27 #include <elf.h>
28 #include <limits.h>
29 #include <symbol/kallsyms.h>
30 #include <sys/utsname.h>
31 
32 static int dso__load_kernel_sym(struct dso *dso, struct map *map);
33 static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map);
34 static bool symbol__is_idle(const char *name);
35 
36 int vmlinux_path__nr_entries;
37 char **vmlinux_path;
38 
39 struct symbol_conf symbol_conf = {
40 	.use_modules		= true,
41 	.try_vmlinux_path	= true,
42 	.annotate_src		= true,
43 	.demangle		= true,
44 	.demangle_kernel	= false,
45 	.cumulate_callchain	= true,
46 	.show_hist_headers	= true,
47 	.symfs			= "",
48 	.event_group		= true,
49 };
50 
51 static enum dso_binary_type binary_type_symtab[] = {
52 	DSO_BINARY_TYPE__KALLSYMS,
53 	DSO_BINARY_TYPE__GUEST_KALLSYMS,
54 	DSO_BINARY_TYPE__JAVA_JIT,
55 	DSO_BINARY_TYPE__DEBUGLINK,
56 	DSO_BINARY_TYPE__BUILD_ID_CACHE,
57 	DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO,
58 	DSO_BINARY_TYPE__FEDORA_DEBUGINFO,
59 	DSO_BINARY_TYPE__UBUNTU_DEBUGINFO,
60 	DSO_BINARY_TYPE__BUILDID_DEBUGINFO,
61 	DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
62 	DSO_BINARY_TYPE__GUEST_KMODULE,
63 	DSO_BINARY_TYPE__GUEST_KMODULE_COMP,
64 	DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE,
65 	DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP,
66 	DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO,
67 	DSO_BINARY_TYPE__NOT_FOUND,
68 };
69 
70 #define DSO_BINARY_TYPE__SYMTAB_CNT ARRAY_SIZE(binary_type_symtab)
71 
72 bool symbol_type__is_a(char symbol_type, enum map_type map_type)
73 {
74 	symbol_type = toupper(symbol_type);
75 
76 	switch (map_type) {
77 	case MAP__FUNCTION:
78 		return symbol_type == 'T' || symbol_type == 'W';
79 	case MAP__VARIABLE:
80 		return symbol_type == 'D';
81 	default:
82 		return false;
83 	}
84 }
85 
86 static int prefix_underscores_count(const char *str)
87 {
88 	const char *tail = str;
89 
90 	while (*tail == '_')
91 		tail++;
92 
93 	return tail - str;
94 }
95 
96 int __weak arch__compare_symbol_names(const char *namea, const char *nameb)
97 {
98 	return strcmp(namea, nameb);
99 }
100 
101 int __weak arch__compare_symbol_names_n(const char *namea, const char *nameb,
102 					unsigned int n)
103 {
104 	return strncmp(namea, nameb, n);
105 }
106 
107 int __weak arch__choose_best_symbol(struct symbol *syma,
108 				    struct symbol *symb __maybe_unused)
109 {
110 	/* Avoid "SyS" kernel syscall aliases */
111 	if (strlen(syma->name) >= 3 && !strncmp(syma->name, "SyS", 3))
112 		return SYMBOL_B;
113 	if (strlen(syma->name) >= 10 && !strncmp(syma->name, "compat_SyS", 10))
114 		return SYMBOL_B;
115 
116 	return SYMBOL_A;
117 }
118 
119 static int choose_best_symbol(struct symbol *syma, struct symbol *symb)
120 {
121 	s64 a;
122 	s64 b;
123 	size_t na, nb;
124 
125 	/* Prefer a symbol with non zero length */
126 	a = syma->end - syma->start;
127 	b = symb->end - symb->start;
128 	if ((b == 0) && (a > 0))
129 		return SYMBOL_A;
130 	else if ((a == 0) && (b > 0))
131 		return SYMBOL_B;
132 
133 	/* Prefer a non weak symbol over a weak one */
134 	a = syma->binding == STB_WEAK;
135 	b = symb->binding == STB_WEAK;
136 	if (b && !a)
137 		return SYMBOL_A;
138 	if (a && !b)
139 		return SYMBOL_B;
140 
141 	/* Prefer a global symbol over a non global one */
142 	a = syma->binding == STB_GLOBAL;
143 	b = symb->binding == STB_GLOBAL;
144 	if (a && !b)
145 		return SYMBOL_A;
146 	if (b && !a)
147 		return SYMBOL_B;
148 
149 	/* Prefer a symbol with less underscores */
150 	a = prefix_underscores_count(syma->name);
151 	b = prefix_underscores_count(symb->name);
152 	if (b > a)
153 		return SYMBOL_A;
154 	else if (a > b)
155 		return SYMBOL_B;
156 
157 	/* Choose the symbol with the longest name */
158 	na = strlen(syma->name);
159 	nb = strlen(symb->name);
160 	if (na > nb)
161 		return SYMBOL_A;
162 	else if (na < nb)
163 		return SYMBOL_B;
164 
165 	return arch__choose_best_symbol(syma, symb);
166 }
167 
168 void symbols__fixup_duplicate(struct rb_root *symbols)
169 {
170 	struct rb_node *nd;
171 	struct symbol *curr, *next;
172 
173 	if (symbol_conf.allow_aliases)
174 		return;
175 
176 	nd = rb_first(symbols);
177 
178 	while (nd) {
179 		curr = rb_entry(nd, struct symbol, rb_node);
180 again:
181 		nd = rb_next(&curr->rb_node);
182 		next = rb_entry(nd, struct symbol, rb_node);
183 
184 		if (!nd)
185 			break;
186 
187 		if (curr->start != next->start)
188 			continue;
189 
190 		if (choose_best_symbol(curr, next) == SYMBOL_A) {
191 			rb_erase(&next->rb_node, symbols);
192 			symbol__delete(next);
193 			goto again;
194 		} else {
195 			nd = rb_next(&curr->rb_node);
196 			rb_erase(&curr->rb_node, symbols);
197 			symbol__delete(curr);
198 		}
199 	}
200 }
201 
202 void symbols__fixup_end(struct rb_root *symbols)
203 {
204 	struct rb_node *nd, *prevnd = rb_first(symbols);
205 	struct symbol *curr, *prev;
206 
207 	if (prevnd == NULL)
208 		return;
209 
210 	curr = rb_entry(prevnd, struct symbol, rb_node);
211 
212 	for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
213 		prev = curr;
214 		curr = rb_entry(nd, struct symbol, rb_node);
215 
216 		if (prev->end == prev->start && prev->end != curr->start)
217 			prev->end = curr->start;
218 	}
219 
220 	/* Last entry */
221 	if (curr->end == curr->start)
222 		curr->end = roundup(curr->start, 4096) + 4096;
223 }
224 
225 void __map_groups__fixup_end(struct map_groups *mg, enum map_type type)
226 {
227 	struct maps *maps = &mg->maps[type];
228 	struct map *next, *curr;
229 
230 	pthread_rwlock_wrlock(&maps->lock);
231 
232 	curr = maps__first(maps);
233 	if (curr == NULL)
234 		goto out_unlock;
235 
236 	for (next = map__next(curr); next; next = map__next(curr)) {
237 		if (!curr->end)
238 			curr->end = next->start;
239 		curr = next;
240 	}
241 
242 	/*
243 	 * We still haven't the actual symbols, so guess the
244 	 * last map final address.
245 	 */
246 	if (!curr->end)
247 		curr->end = ~0ULL;
248 
249 out_unlock:
250 	pthread_rwlock_unlock(&maps->lock);
251 }
252 
253 struct symbol *symbol__new(u64 start, u64 len, u8 binding, const char *name)
254 {
255 	size_t namelen = strlen(name) + 1;
256 	struct symbol *sym = calloc(1, (symbol_conf.priv_size +
257 					sizeof(*sym) + namelen));
258 	if (sym == NULL)
259 		return NULL;
260 
261 	if (symbol_conf.priv_size) {
262 		if (symbol_conf.init_annotation) {
263 			struct annotation *notes = (void *)sym;
264 			pthread_mutex_init(&notes->lock, NULL);
265 		}
266 		sym = ((void *)sym) + symbol_conf.priv_size;
267 	}
268 
269 	sym->start   = start;
270 	sym->end     = len ? start + len : start;
271 	sym->binding = binding;
272 	sym->namelen = namelen - 1;
273 
274 	pr_debug4("%s: %s %#" PRIx64 "-%#" PRIx64 "\n",
275 		  __func__, name, start, sym->end);
276 	memcpy(sym->name, name, namelen);
277 
278 	return sym;
279 }
280 
281 void symbol__delete(struct symbol *sym)
282 {
283 	free(((void *)sym) - symbol_conf.priv_size);
284 }
285 
286 void symbols__delete(struct rb_root *symbols)
287 {
288 	struct symbol *pos;
289 	struct rb_node *next = rb_first(symbols);
290 
291 	while (next) {
292 		pos = rb_entry(next, struct symbol, rb_node);
293 		next = rb_next(&pos->rb_node);
294 		rb_erase(&pos->rb_node, symbols);
295 		symbol__delete(pos);
296 	}
297 }
298 
299 void __symbols__insert(struct rb_root *symbols, struct symbol *sym, bool kernel)
300 {
301 	struct rb_node **p = &symbols->rb_node;
302 	struct rb_node *parent = NULL;
303 	const u64 ip = sym->start;
304 	struct symbol *s;
305 
306 	if (kernel) {
307 		const char *name = sym->name;
308 		/*
309 		 * ppc64 uses function descriptors and appends a '.' to the
310 		 * start of every instruction address. Remove it.
311 		 */
312 		if (name[0] == '.')
313 			name++;
314 		sym->idle = symbol__is_idle(name);
315 	}
316 
317 	while (*p != NULL) {
318 		parent = *p;
319 		s = rb_entry(parent, struct symbol, rb_node);
320 		if (ip < s->start)
321 			p = &(*p)->rb_left;
322 		else
323 			p = &(*p)->rb_right;
324 	}
325 	rb_link_node(&sym->rb_node, parent, p);
326 	rb_insert_color(&sym->rb_node, symbols);
327 }
328 
329 void symbols__insert(struct rb_root *symbols, struct symbol *sym)
330 {
331 	__symbols__insert(symbols, sym, false);
332 }
333 
334 static struct symbol *symbols__find(struct rb_root *symbols, u64 ip)
335 {
336 	struct rb_node *n;
337 
338 	if (symbols == NULL)
339 		return NULL;
340 
341 	n = symbols->rb_node;
342 
343 	while (n) {
344 		struct symbol *s = rb_entry(n, struct symbol, rb_node);
345 
346 		if (ip < s->start)
347 			n = n->rb_left;
348 		else if (ip > s->end || (ip == s->end && ip != s->start))
349 			n = n->rb_right;
350 		else
351 			return s;
352 	}
353 
354 	return NULL;
355 }
356 
357 static struct symbol *symbols__first(struct rb_root *symbols)
358 {
359 	struct rb_node *n = rb_first(symbols);
360 
361 	if (n)
362 		return rb_entry(n, struct symbol, rb_node);
363 
364 	return NULL;
365 }
366 
367 static struct symbol *symbols__last(struct rb_root *symbols)
368 {
369 	struct rb_node *n = rb_last(symbols);
370 
371 	if (n)
372 		return rb_entry(n, struct symbol, rb_node);
373 
374 	return NULL;
375 }
376 
377 static struct symbol *symbols__next(struct symbol *sym)
378 {
379 	struct rb_node *n = rb_next(&sym->rb_node);
380 
381 	if (n)
382 		return rb_entry(n, struct symbol, rb_node);
383 
384 	return NULL;
385 }
386 
387 static void symbols__insert_by_name(struct rb_root *symbols, struct symbol *sym)
388 {
389 	struct rb_node **p = &symbols->rb_node;
390 	struct rb_node *parent = NULL;
391 	struct symbol_name_rb_node *symn, *s;
392 
393 	symn = container_of(sym, struct symbol_name_rb_node, sym);
394 
395 	while (*p != NULL) {
396 		parent = *p;
397 		s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
398 		if (strcmp(sym->name, s->sym.name) < 0)
399 			p = &(*p)->rb_left;
400 		else
401 			p = &(*p)->rb_right;
402 	}
403 	rb_link_node(&symn->rb_node, parent, p);
404 	rb_insert_color(&symn->rb_node, symbols);
405 }
406 
407 static void symbols__sort_by_name(struct rb_root *symbols,
408 				  struct rb_root *source)
409 {
410 	struct rb_node *nd;
411 
412 	for (nd = rb_first(source); nd; nd = rb_next(nd)) {
413 		struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
414 		symbols__insert_by_name(symbols, pos);
415 	}
416 }
417 
418 int symbol__match_symbol_name(const char *name, const char *str,
419 			      enum symbol_tag_include includes)
420 {
421 	const char *versioning;
422 
423 	if (includes == SYMBOL_TAG_INCLUDE__DEFAULT_ONLY &&
424 	    (versioning = strstr(name, "@@"))) {
425 		int len = strlen(str);
426 
427 		if (len < versioning - name)
428 			len = versioning - name;
429 
430 		return arch__compare_symbol_names_n(name, str, len);
431 	} else
432 		return arch__compare_symbol_names(name, str);
433 }
434 
435 static struct symbol *symbols__find_by_name(struct rb_root *symbols,
436 					    const char *name,
437 					    enum symbol_tag_include includes)
438 {
439 	struct rb_node *n;
440 	struct symbol_name_rb_node *s = NULL;
441 
442 	if (symbols == NULL)
443 		return NULL;
444 
445 	n = symbols->rb_node;
446 
447 	while (n) {
448 		int cmp;
449 
450 		s = rb_entry(n, struct symbol_name_rb_node, rb_node);
451 		cmp = symbol__match_symbol_name(s->sym.name, name, includes);
452 
453 		if (cmp > 0)
454 			n = n->rb_left;
455 		else if (cmp < 0)
456 			n = n->rb_right;
457 		else
458 			break;
459 	}
460 
461 	if (n == NULL)
462 		return NULL;
463 
464 	if (includes != SYMBOL_TAG_INCLUDE__DEFAULT_ONLY)
465 		/* return first symbol that has same name (if any) */
466 		for (n = rb_prev(n); n; n = rb_prev(n)) {
467 			struct symbol_name_rb_node *tmp;
468 
469 			tmp = rb_entry(n, struct symbol_name_rb_node, rb_node);
470 			if (arch__compare_symbol_names(tmp->sym.name, s->sym.name))
471 				break;
472 
473 			s = tmp;
474 		}
475 
476 	return &s->sym;
477 }
478 
479 void dso__reset_find_symbol_cache(struct dso *dso)
480 {
481 	enum map_type type;
482 
483 	for (type = MAP__FUNCTION; type <= MAP__VARIABLE; ++type) {
484 		dso->last_find_result[type].addr   = 0;
485 		dso->last_find_result[type].symbol = NULL;
486 	}
487 }
488 
489 void dso__insert_symbol(struct dso *dso, enum map_type type, struct symbol *sym)
490 {
491 	__symbols__insert(&dso->symbols[type], sym, dso->kernel);
492 
493 	/* update the symbol cache if necessary */
494 	if (dso->last_find_result[type].addr >= sym->start &&
495 	    (dso->last_find_result[type].addr < sym->end ||
496 	    sym->start == sym->end)) {
497 		dso->last_find_result[type].symbol = sym;
498 	}
499 }
500 
501 struct symbol *dso__find_symbol(struct dso *dso,
502 				enum map_type type, u64 addr)
503 {
504 	if (dso->last_find_result[type].addr != addr || dso->last_find_result[type].symbol == NULL) {
505 		dso->last_find_result[type].addr   = addr;
506 		dso->last_find_result[type].symbol = symbols__find(&dso->symbols[type], addr);
507 	}
508 
509 	return dso->last_find_result[type].symbol;
510 }
511 
512 struct symbol *dso__first_symbol(struct dso *dso, enum map_type type)
513 {
514 	return symbols__first(&dso->symbols[type]);
515 }
516 
517 struct symbol *dso__last_symbol(struct dso *dso, enum map_type type)
518 {
519 	return symbols__last(&dso->symbols[type]);
520 }
521 
522 struct symbol *dso__next_symbol(struct symbol *sym)
523 {
524 	return symbols__next(sym);
525 }
526 
527 struct symbol *symbol__next_by_name(struct symbol *sym)
528 {
529 	struct symbol_name_rb_node *s = container_of(sym, struct symbol_name_rb_node, sym);
530 	struct rb_node *n = rb_next(&s->rb_node);
531 
532 	return n ? &rb_entry(n, struct symbol_name_rb_node, rb_node)->sym : NULL;
533 }
534 
535  /*
536   * Teturns first symbol that matched with @name.
537   */
538 struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type,
539 					const char *name)
540 {
541 	struct symbol *s = symbols__find_by_name(&dso->symbol_names[type], name,
542 						 SYMBOL_TAG_INCLUDE__NONE);
543 	if (!s)
544 		s = symbols__find_by_name(&dso->symbol_names[type], name,
545 					  SYMBOL_TAG_INCLUDE__DEFAULT_ONLY);
546 	return s;
547 }
548 
549 void dso__sort_by_name(struct dso *dso, enum map_type type)
550 {
551 	dso__set_sorted_by_name(dso, type);
552 	return symbols__sort_by_name(&dso->symbol_names[type],
553 				     &dso->symbols[type]);
554 }
555 
556 int modules__parse(const char *filename, void *arg,
557 		   int (*process_module)(void *arg, const char *name,
558 					 u64 start, u64 size))
559 {
560 	char *line = NULL;
561 	size_t n;
562 	FILE *file;
563 	int err = 0;
564 
565 	file = fopen(filename, "r");
566 	if (file == NULL)
567 		return -1;
568 
569 	while (1) {
570 		char name[PATH_MAX];
571 		u64 start, size;
572 		char *sep, *endptr;
573 		ssize_t line_len;
574 
575 		line_len = getline(&line, &n, file);
576 		if (line_len < 0) {
577 			if (feof(file))
578 				break;
579 			err = -1;
580 			goto out;
581 		}
582 
583 		if (!line) {
584 			err = -1;
585 			goto out;
586 		}
587 
588 		line[--line_len] = '\0'; /* \n */
589 
590 		sep = strrchr(line, 'x');
591 		if (sep == NULL)
592 			continue;
593 
594 		hex2u64(sep + 1, &start);
595 
596 		sep = strchr(line, ' ');
597 		if (sep == NULL)
598 			continue;
599 
600 		*sep = '\0';
601 
602 		scnprintf(name, sizeof(name), "[%s]", line);
603 
604 		size = strtoul(sep + 1, &endptr, 0);
605 		if (*endptr != ' ' && *endptr != '\t')
606 			continue;
607 
608 		err = process_module(arg, name, start, size);
609 		if (err)
610 			break;
611 	}
612 out:
613 	free(line);
614 	fclose(file);
615 	return err;
616 }
617 
618 struct process_kallsyms_args {
619 	struct map *map;
620 	struct dso *dso;
621 };
622 
623 /*
624  * These are symbols in the kernel image, so make sure that
625  * sym is from a kernel DSO.
626  */
627 static bool symbol__is_idle(const char *name)
628 {
629 	const char * const idle_symbols[] = {
630 		"cpu_idle",
631 		"cpu_startup_entry",
632 		"intel_idle",
633 		"default_idle",
634 		"native_safe_halt",
635 		"enter_idle",
636 		"exit_idle",
637 		"mwait_idle",
638 		"mwait_idle_with_hints",
639 		"poll_idle",
640 		"ppc64_runlatch_off",
641 		"pseries_dedicated_idle_sleep",
642 		NULL
643 	};
644 	int i;
645 
646 	for (i = 0; idle_symbols[i]; i++) {
647 		if (!strcmp(idle_symbols[i], name))
648 			return true;
649 	}
650 
651 	return false;
652 }
653 
654 static int map__process_kallsym_symbol(void *arg, const char *name,
655 				       char type, u64 start)
656 {
657 	struct symbol *sym;
658 	struct process_kallsyms_args *a = arg;
659 	struct rb_root *root = &a->dso->symbols[a->map->type];
660 
661 	if (!symbol_type__is_a(type, a->map->type))
662 		return 0;
663 
664 	/*
665 	 * module symbols are not sorted so we add all
666 	 * symbols, setting length to 0, and rely on
667 	 * symbols__fixup_end() to fix it up.
668 	 */
669 	sym = symbol__new(start, 0, kallsyms2elf_binding(type), name);
670 	if (sym == NULL)
671 		return -ENOMEM;
672 	/*
673 	 * We will pass the symbols to the filter later, in
674 	 * map__split_kallsyms, when we have split the maps per module
675 	 */
676 	__symbols__insert(root, sym, !strchr(name, '['));
677 
678 	return 0;
679 }
680 
681 /*
682  * Loads the function entries in /proc/kallsyms into kernel_map->dso,
683  * so that we can in the next step set the symbol ->end address and then
684  * call kernel_maps__split_kallsyms.
685  */
686 static int dso__load_all_kallsyms(struct dso *dso, const char *filename,
687 				  struct map *map)
688 {
689 	struct process_kallsyms_args args = { .map = map, .dso = dso, };
690 	return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
691 }
692 
693 static int dso__split_kallsyms_for_kcore(struct dso *dso, struct map *map)
694 {
695 	struct map_groups *kmaps = map__kmaps(map);
696 	struct map *curr_map;
697 	struct symbol *pos;
698 	int count = 0;
699 	struct rb_root old_root = dso->symbols[map->type];
700 	struct rb_root *root = &dso->symbols[map->type];
701 	struct rb_node *next = rb_first(root);
702 
703 	if (!kmaps)
704 		return -1;
705 
706 	*root = RB_ROOT;
707 
708 	while (next) {
709 		char *module;
710 
711 		pos = rb_entry(next, struct symbol, rb_node);
712 		next = rb_next(&pos->rb_node);
713 
714 		rb_erase_init(&pos->rb_node, &old_root);
715 
716 		module = strchr(pos->name, '\t');
717 		if (module)
718 			*module = '\0';
719 
720 		curr_map = map_groups__find(kmaps, map->type, pos->start);
721 
722 		if (!curr_map) {
723 			symbol__delete(pos);
724 			continue;
725 		}
726 
727 		pos->start -= curr_map->start - curr_map->pgoff;
728 		if (pos->end)
729 			pos->end -= curr_map->start - curr_map->pgoff;
730 		symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
731 		++count;
732 	}
733 
734 	/* Symbols have been adjusted */
735 	dso->adjust_symbols = 1;
736 
737 	return count;
738 }
739 
740 /*
741  * Split the symbols into maps, making sure there are no overlaps, i.e. the
742  * kernel range is broken in several maps, named [kernel].N, as we don't have
743  * the original ELF section names vmlinux have.
744  */
745 static int dso__split_kallsyms(struct dso *dso, struct map *map, u64 delta)
746 {
747 	struct map_groups *kmaps = map__kmaps(map);
748 	struct machine *machine;
749 	struct map *curr_map = map;
750 	struct symbol *pos;
751 	int count = 0, moved = 0;
752 	struct rb_root *root = &dso->symbols[map->type];
753 	struct rb_node *next = rb_first(root);
754 	int kernel_range = 0;
755 
756 	if (!kmaps)
757 		return -1;
758 
759 	machine = kmaps->machine;
760 
761 	while (next) {
762 		char *module;
763 
764 		pos = rb_entry(next, struct symbol, rb_node);
765 		next = rb_next(&pos->rb_node);
766 
767 		module = strchr(pos->name, '\t');
768 		if (module) {
769 			if (!symbol_conf.use_modules)
770 				goto discard_symbol;
771 
772 			*module++ = '\0';
773 
774 			if (strcmp(curr_map->dso->short_name, module)) {
775 				if (curr_map != map &&
776 				    dso->kernel == DSO_TYPE_GUEST_KERNEL &&
777 				    machine__is_default_guest(machine)) {
778 					/*
779 					 * We assume all symbols of a module are
780 					 * continuous in * kallsyms, so curr_map
781 					 * points to a module and all its
782 					 * symbols are in its kmap. Mark it as
783 					 * loaded.
784 					 */
785 					dso__set_loaded(curr_map->dso,
786 							curr_map->type);
787 				}
788 
789 				curr_map = map_groups__find_by_name(kmaps,
790 							map->type, module);
791 				if (curr_map == NULL) {
792 					pr_debug("%s/proc/{kallsyms,modules} "
793 					         "inconsistency while looking "
794 						 "for \"%s\" module!\n",
795 						 machine->root_dir, module);
796 					curr_map = map;
797 					goto discard_symbol;
798 				}
799 
800 				if (curr_map->dso->loaded &&
801 				    !machine__is_default_guest(machine))
802 					goto discard_symbol;
803 			}
804 			/*
805 			 * So that we look just like we get from .ko files,
806 			 * i.e. not prelinked, relative to map->start.
807 			 */
808 			pos->start = curr_map->map_ip(curr_map, pos->start);
809 			pos->end   = curr_map->map_ip(curr_map, pos->end);
810 		} else if (curr_map != map) {
811 			char dso_name[PATH_MAX];
812 			struct dso *ndso;
813 
814 			if (delta) {
815 				/* Kernel was relocated at boot time */
816 				pos->start -= delta;
817 				pos->end -= delta;
818 			}
819 
820 			if (count == 0) {
821 				curr_map = map;
822 				goto add_symbol;
823 			}
824 
825 			if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
826 				snprintf(dso_name, sizeof(dso_name),
827 					"[guest.kernel].%d",
828 					kernel_range++);
829 			else
830 				snprintf(dso_name, sizeof(dso_name),
831 					"[kernel].%d",
832 					kernel_range++);
833 
834 			ndso = dso__new(dso_name);
835 			if (ndso == NULL)
836 				return -1;
837 
838 			ndso->kernel = dso->kernel;
839 
840 			curr_map = map__new2(pos->start, ndso, map->type);
841 			if (curr_map == NULL) {
842 				dso__put(ndso);
843 				return -1;
844 			}
845 
846 			curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
847 			map_groups__insert(kmaps, curr_map);
848 			++kernel_range;
849 		} else if (delta) {
850 			/* Kernel was relocated at boot time */
851 			pos->start -= delta;
852 			pos->end -= delta;
853 		}
854 add_symbol:
855 		if (curr_map != map) {
856 			rb_erase(&pos->rb_node, root);
857 			symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
858 			++moved;
859 		} else
860 			++count;
861 
862 		continue;
863 discard_symbol:
864 		rb_erase(&pos->rb_node, root);
865 		symbol__delete(pos);
866 	}
867 
868 	if (curr_map != map &&
869 	    dso->kernel == DSO_TYPE_GUEST_KERNEL &&
870 	    machine__is_default_guest(kmaps->machine)) {
871 		dso__set_loaded(curr_map->dso, curr_map->type);
872 	}
873 
874 	return count + moved;
875 }
876 
877 bool symbol__restricted_filename(const char *filename,
878 				 const char *restricted_filename)
879 {
880 	bool restricted = false;
881 
882 	if (symbol_conf.kptr_restrict) {
883 		char *r = realpath(filename, NULL);
884 
885 		if (r != NULL) {
886 			restricted = strcmp(r, restricted_filename) == 0;
887 			free(r);
888 			return restricted;
889 		}
890 	}
891 
892 	return restricted;
893 }
894 
895 struct module_info {
896 	struct rb_node rb_node;
897 	char *name;
898 	u64 start;
899 };
900 
901 static void add_module(struct module_info *mi, struct rb_root *modules)
902 {
903 	struct rb_node **p = &modules->rb_node;
904 	struct rb_node *parent = NULL;
905 	struct module_info *m;
906 
907 	while (*p != NULL) {
908 		parent = *p;
909 		m = rb_entry(parent, struct module_info, rb_node);
910 		if (strcmp(mi->name, m->name) < 0)
911 			p = &(*p)->rb_left;
912 		else
913 			p = &(*p)->rb_right;
914 	}
915 	rb_link_node(&mi->rb_node, parent, p);
916 	rb_insert_color(&mi->rb_node, modules);
917 }
918 
919 static void delete_modules(struct rb_root *modules)
920 {
921 	struct module_info *mi;
922 	struct rb_node *next = rb_first(modules);
923 
924 	while (next) {
925 		mi = rb_entry(next, struct module_info, rb_node);
926 		next = rb_next(&mi->rb_node);
927 		rb_erase(&mi->rb_node, modules);
928 		zfree(&mi->name);
929 		free(mi);
930 	}
931 }
932 
933 static struct module_info *find_module(const char *name,
934 				       struct rb_root *modules)
935 {
936 	struct rb_node *n = modules->rb_node;
937 
938 	while (n) {
939 		struct module_info *m;
940 		int cmp;
941 
942 		m = rb_entry(n, struct module_info, rb_node);
943 		cmp = strcmp(name, m->name);
944 		if (cmp < 0)
945 			n = n->rb_left;
946 		else if (cmp > 0)
947 			n = n->rb_right;
948 		else
949 			return m;
950 	}
951 
952 	return NULL;
953 }
954 
955 static int __read_proc_modules(void *arg, const char *name, u64 start,
956 			       u64 size __maybe_unused)
957 {
958 	struct rb_root *modules = arg;
959 	struct module_info *mi;
960 
961 	mi = zalloc(sizeof(struct module_info));
962 	if (!mi)
963 		return -ENOMEM;
964 
965 	mi->name = strdup(name);
966 	mi->start = start;
967 
968 	if (!mi->name) {
969 		free(mi);
970 		return -ENOMEM;
971 	}
972 
973 	add_module(mi, modules);
974 
975 	return 0;
976 }
977 
978 static int read_proc_modules(const char *filename, struct rb_root *modules)
979 {
980 	if (symbol__restricted_filename(filename, "/proc/modules"))
981 		return -1;
982 
983 	if (modules__parse(filename, modules, __read_proc_modules)) {
984 		delete_modules(modules);
985 		return -1;
986 	}
987 
988 	return 0;
989 }
990 
991 int compare_proc_modules(const char *from, const char *to)
992 {
993 	struct rb_root from_modules = RB_ROOT;
994 	struct rb_root to_modules = RB_ROOT;
995 	struct rb_node *from_node, *to_node;
996 	struct module_info *from_m, *to_m;
997 	int ret = -1;
998 
999 	if (read_proc_modules(from, &from_modules))
1000 		return -1;
1001 
1002 	if (read_proc_modules(to, &to_modules))
1003 		goto out_delete_from;
1004 
1005 	from_node = rb_first(&from_modules);
1006 	to_node = rb_first(&to_modules);
1007 	while (from_node) {
1008 		if (!to_node)
1009 			break;
1010 
1011 		from_m = rb_entry(from_node, struct module_info, rb_node);
1012 		to_m = rb_entry(to_node, struct module_info, rb_node);
1013 
1014 		if (from_m->start != to_m->start ||
1015 		    strcmp(from_m->name, to_m->name))
1016 			break;
1017 
1018 		from_node = rb_next(from_node);
1019 		to_node = rb_next(to_node);
1020 	}
1021 
1022 	if (!from_node && !to_node)
1023 		ret = 0;
1024 
1025 	delete_modules(&to_modules);
1026 out_delete_from:
1027 	delete_modules(&from_modules);
1028 
1029 	return ret;
1030 }
1031 
1032 static int do_validate_kcore_modules(const char *filename, struct map *map,
1033 				  struct map_groups *kmaps)
1034 {
1035 	struct rb_root modules = RB_ROOT;
1036 	struct map *old_map;
1037 	int err;
1038 
1039 	err = read_proc_modules(filename, &modules);
1040 	if (err)
1041 		return err;
1042 
1043 	old_map = map_groups__first(kmaps, map->type);
1044 	while (old_map) {
1045 		struct map *next = map_groups__next(old_map);
1046 		struct module_info *mi;
1047 
1048 		if (old_map == map || old_map->start == map->start) {
1049 			/* The kernel map */
1050 			old_map = next;
1051 			continue;
1052 		}
1053 
1054 		/* Module must be in memory at the same address */
1055 		mi = find_module(old_map->dso->short_name, &modules);
1056 		if (!mi || mi->start != old_map->start) {
1057 			err = -EINVAL;
1058 			goto out;
1059 		}
1060 
1061 		old_map = next;
1062 	}
1063 out:
1064 	delete_modules(&modules);
1065 	return err;
1066 }
1067 
1068 /*
1069  * If kallsyms is referenced by name then we look for filename in the same
1070  * directory.
1071  */
1072 static bool filename_from_kallsyms_filename(char *filename,
1073 					    const char *base_name,
1074 					    const char *kallsyms_filename)
1075 {
1076 	char *name;
1077 
1078 	strcpy(filename, kallsyms_filename);
1079 	name = strrchr(filename, '/');
1080 	if (!name)
1081 		return false;
1082 
1083 	name += 1;
1084 
1085 	if (!strcmp(name, "kallsyms")) {
1086 		strcpy(name, base_name);
1087 		return true;
1088 	}
1089 
1090 	return false;
1091 }
1092 
1093 static int validate_kcore_modules(const char *kallsyms_filename,
1094 				  struct map *map)
1095 {
1096 	struct map_groups *kmaps = map__kmaps(map);
1097 	char modules_filename[PATH_MAX];
1098 
1099 	if (!kmaps)
1100 		return -EINVAL;
1101 
1102 	if (!filename_from_kallsyms_filename(modules_filename, "modules",
1103 					     kallsyms_filename))
1104 		return -EINVAL;
1105 
1106 	if (do_validate_kcore_modules(modules_filename, map, kmaps))
1107 		return -EINVAL;
1108 
1109 	return 0;
1110 }
1111 
1112 static int validate_kcore_addresses(const char *kallsyms_filename,
1113 				    struct map *map)
1114 {
1115 	struct kmap *kmap = map__kmap(map);
1116 
1117 	if (!kmap)
1118 		return -EINVAL;
1119 
1120 	if (kmap->ref_reloc_sym && kmap->ref_reloc_sym->name) {
1121 		u64 start;
1122 
1123 		if (kallsyms__get_function_start(kallsyms_filename,
1124 						 kmap->ref_reloc_sym->name, &start))
1125 			return -ENOENT;
1126 		if (start != kmap->ref_reloc_sym->addr)
1127 			return -EINVAL;
1128 	}
1129 
1130 	return validate_kcore_modules(kallsyms_filename, map);
1131 }
1132 
1133 struct kcore_mapfn_data {
1134 	struct dso *dso;
1135 	enum map_type type;
1136 	struct list_head maps;
1137 };
1138 
1139 static int kcore_mapfn(u64 start, u64 len, u64 pgoff, void *data)
1140 {
1141 	struct kcore_mapfn_data *md = data;
1142 	struct map *map;
1143 
1144 	map = map__new2(start, md->dso, md->type);
1145 	if (map == NULL)
1146 		return -ENOMEM;
1147 
1148 	map->end = map->start + len;
1149 	map->pgoff = pgoff;
1150 
1151 	list_add(&map->node, &md->maps);
1152 
1153 	return 0;
1154 }
1155 
1156 static int dso__load_kcore(struct dso *dso, struct map *map,
1157 			   const char *kallsyms_filename)
1158 {
1159 	struct map_groups *kmaps = map__kmaps(map);
1160 	struct machine *machine;
1161 	struct kcore_mapfn_data md;
1162 	struct map *old_map, *new_map, *replacement_map = NULL;
1163 	bool is_64_bit;
1164 	int err, fd;
1165 	char kcore_filename[PATH_MAX];
1166 	struct symbol *sym;
1167 
1168 	if (!kmaps)
1169 		return -EINVAL;
1170 
1171 	machine = kmaps->machine;
1172 
1173 	/* This function requires that the map is the kernel map */
1174 	if (map != machine->vmlinux_maps[map->type])
1175 		return -EINVAL;
1176 
1177 	if (!filename_from_kallsyms_filename(kcore_filename, "kcore",
1178 					     kallsyms_filename))
1179 		return -EINVAL;
1180 
1181 	/* Modules and kernel must be present at their original addresses */
1182 	if (validate_kcore_addresses(kallsyms_filename, map))
1183 		return -EINVAL;
1184 
1185 	md.dso = dso;
1186 	md.type = map->type;
1187 	INIT_LIST_HEAD(&md.maps);
1188 
1189 	fd = open(kcore_filename, O_RDONLY);
1190 	if (fd < 0) {
1191 		pr_debug("Failed to open %s. Note /proc/kcore requires CAP_SYS_RAWIO capability to access.\n",
1192 			 kcore_filename);
1193 		return -EINVAL;
1194 	}
1195 
1196 	/* Read new maps into temporary lists */
1197 	err = file__read_maps(fd, md.type == MAP__FUNCTION, kcore_mapfn, &md,
1198 			      &is_64_bit);
1199 	if (err)
1200 		goto out_err;
1201 	dso->is_64_bit = is_64_bit;
1202 
1203 	if (list_empty(&md.maps)) {
1204 		err = -EINVAL;
1205 		goto out_err;
1206 	}
1207 
1208 	/* Remove old maps */
1209 	old_map = map_groups__first(kmaps, map->type);
1210 	while (old_map) {
1211 		struct map *next = map_groups__next(old_map);
1212 
1213 		if (old_map != map)
1214 			map_groups__remove(kmaps, old_map);
1215 		old_map = next;
1216 	}
1217 
1218 	/* Find the kernel map using the first symbol */
1219 	sym = dso__first_symbol(dso, map->type);
1220 	list_for_each_entry(new_map, &md.maps, node) {
1221 		if (sym && sym->start >= new_map->start &&
1222 		    sym->start < new_map->end) {
1223 			replacement_map = new_map;
1224 			break;
1225 		}
1226 	}
1227 
1228 	if (!replacement_map)
1229 		replacement_map = list_entry(md.maps.next, struct map, node);
1230 
1231 	/* Add new maps */
1232 	while (!list_empty(&md.maps)) {
1233 		new_map = list_entry(md.maps.next, struct map, node);
1234 		list_del_init(&new_map->node);
1235 		if (new_map == replacement_map) {
1236 			map->start	= new_map->start;
1237 			map->end	= new_map->end;
1238 			map->pgoff	= new_map->pgoff;
1239 			map->map_ip	= new_map->map_ip;
1240 			map->unmap_ip	= new_map->unmap_ip;
1241 			/* Ensure maps are correctly ordered */
1242 			map__get(map);
1243 			map_groups__remove(kmaps, map);
1244 			map_groups__insert(kmaps, map);
1245 			map__put(map);
1246 		} else {
1247 			map_groups__insert(kmaps, new_map);
1248 		}
1249 
1250 		map__put(new_map);
1251 	}
1252 
1253 	/*
1254 	 * Set the data type and long name so that kcore can be read via
1255 	 * dso__data_read_addr().
1256 	 */
1257 	if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1258 		dso->binary_type = DSO_BINARY_TYPE__GUEST_KCORE;
1259 	else
1260 		dso->binary_type = DSO_BINARY_TYPE__KCORE;
1261 	dso__set_long_name(dso, strdup(kcore_filename), true);
1262 
1263 	close(fd);
1264 
1265 	if (map->type == MAP__FUNCTION)
1266 		pr_debug("Using %s for kernel object code\n", kcore_filename);
1267 	else
1268 		pr_debug("Using %s for kernel data\n", kcore_filename);
1269 
1270 	return 0;
1271 
1272 out_err:
1273 	while (!list_empty(&md.maps)) {
1274 		map = list_entry(md.maps.next, struct map, node);
1275 		list_del_init(&map->node);
1276 		map__put(map);
1277 	}
1278 	close(fd);
1279 	return -EINVAL;
1280 }
1281 
1282 /*
1283  * If the kernel is relocated at boot time, kallsyms won't match.  Compute the
1284  * delta based on the relocation reference symbol.
1285  */
1286 static int kallsyms__delta(struct map *map, const char *filename, u64 *delta)
1287 {
1288 	struct kmap *kmap = map__kmap(map);
1289 	u64 addr;
1290 
1291 	if (!kmap)
1292 		return -1;
1293 
1294 	if (!kmap->ref_reloc_sym || !kmap->ref_reloc_sym->name)
1295 		return 0;
1296 
1297 	if (kallsyms__get_function_start(filename, kmap->ref_reloc_sym->name, &addr))
1298 		return -1;
1299 
1300 	*delta = addr - kmap->ref_reloc_sym->addr;
1301 	return 0;
1302 }
1303 
1304 int __dso__load_kallsyms(struct dso *dso, const char *filename,
1305 			 struct map *map, bool no_kcore)
1306 {
1307 	u64 delta = 0;
1308 
1309 	if (symbol__restricted_filename(filename, "/proc/kallsyms"))
1310 		return -1;
1311 
1312 	if (dso__load_all_kallsyms(dso, filename, map) < 0)
1313 		return -1;
1314 
1315 	if (kallsyms__delta(map, filename, &delta))
1316 		return -1;
1317 
1318 	symbols__fixup_end(&dso->symbols[map->type]);
1319 	symbols__fixup_duplicate(&dso->symbols[map->type]);
1320 
1321 	if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1322 		dso->symtab_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
1323 	else
1324 		dso->symtab_type = DSO_BINARY_TYPE__KALLSYMS;
1325 
1326 	if (!no_kcore && !dso__load_kcore(dso, map, filename))
1327 		return dso__split_kallsyms_for_kcore(dso, map);
1328 	else
1329 		return dso__split_kallsyms(dso, map, delta);
1330 }
1331 
1332 int dso__load_kallsyms(struct dso *dso, const char *filename,
1333 		       struct map *map)
1334 {
1335 	return __dso__load_kallsyms(dso, filename, map, false);
1336 }
1337 
1338 static int dso__load_perf_map(const char *map_path, struct dso *dso,
1339 			      struct map *map)
1340 {
1341 	char *line = NULL;
1342 	size_t n;
1343 	FILE *file;
1344 	int nr_syms = 0;
1345 
1346 	file = fopen(map_path, "r");
1347 	if (file == NULL)
1348 		goto out_failure;
1349 
1350 	while (!feof(file)) {
1351 		u64 start, size;
1352 		struct symbol *sym;
1353 		int line_len, len;
1354 
1355 		line_len = getline(&line, &n, file);
1356 		if (line_len < 0)
1357 			break;
1358 
1359 		if (!line)
1360 			goto out_failure;
1361 
1362 		line[--line_len] = '\0'; /* \n */
1363 
1364 		len = hex2u64(line, &start);
1365 
1366 		len++;
1367 		if (len + 2 >= line_len)
1368 			continue;
1369 
1370 		len += hex2u64(line + len, &size);
1371 
1372 		len++;
1373 		if (len + 2 >= line_len)
1374 			continue;
1375 
1376 		sym = symbol__new(start, size, STB_GLOBAL, line + len);
1377 
1378 		if (sym == NULL)
1379 			goto out_delete_line;
1380 
1381 		symbols__insert(&dso->symbols[map->type], sym);
1382 		nr_syms++;
1383 	}
1384 
1385 	free(line);
1386 	fclose(file);
1387 
1388 	return nr_syms;
1389 
1390 out_delete_line:
1391 	free(line);
1392 out_failure:
1393 	return -1;
1394 }
1395 
1396 static bool dso__is_compatible_symtab_type(struct dso *dso, bool kmod,
1397 					   enum dso_binary_type type)
1398 {
1399 	switch (type) {
1400 	case DSO_BINARY_TYPE__JAVA_JIT:
1401 	case DSO_BINARY_TYPE__DEBUGLINK:
1402 	case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
1403 	case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
1404 	case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
1405 	case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
1406 	case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
1407 		return !kmod && dso->kernel == DSO_TYPE_USER;
1408 
1409 	case DSO_BINARY_TYPE__KALLSYMS:
1410 	case DSO_BINARY_TYPE__VMLINUX:
1411 	case DSO_BINARY_TYPE__KCORE:
1412 		return dso->kernel == DSO_TYPE_KERNEL;
1413 
1414 	case DSO_BINARY_TYPE__GUEST_KALLSYMS:
1415 	case DSO_BINARY_TYPE__GUEST_VMLINUX:
1416 	case DSO_BINARY_TYPE__GUEST_KCORE:
1417 		return dso->kernel == DSO_TYPE_GUEST_KERNEL;
1418 
1419 	case DSO_BINARY_TYPE__GUEST_KMODULE:
1420 	case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
1421 	case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
1422 	case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
1423 		/*
1424 		 * kernel modules know their symtab type - it's set when
1425 		 * creating a module dso in machine__findnew_module_map().
1426 		 */
1427 		return kmod && dso->symtab_type == type;
1428 
1429 	case DSO_BINARY_TYPE__BUILD_ID_CACHE:
1430 	case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO:
1431 		return true;
1432 
1433 	case DSO_BINARY_TYPE__NOT_FOUND:
1434 	default:
1435 		return false;
1436 	}
1437 }
1438 
1439 /* Checks for the existence of the perf-<pid>.map file in two different
1440  * locations.  First, if the process is a separate mount namespace, check in
1441  * that namespace using the pid of the innermost pid namespace.  If's not in a
1442  * namespace, or the file can't be found there, try in the mount namespace of
1443  * the tracing process using our view of its pid.
1444  */
1445 static int dso__find_perf_map(char *filebuf, size_t bufsz,
1446 			      struct nsinfo **nsip)
1447 {
1448 	struct nscookie nsc;
1449 	struct nsinfo *nsi;
1450 	struct nsinfo *nnsi;
1451 	int rc = -1;
1452 
1453 	nsi = *nsip;
1454 
1455 	if (nsi->need_setns) {
1456 		snprintf(filebuf, bufsz, "/tmp/perf-%d.map", nsi->nstgid);
1457 		nsinfo__mountns_enter(nsi, &nsc);
1458 		rc = access(filebuf, R_OK);
1459 		nsinfo__mountns_exit(&nsc);
1460 		if (rc == 0)
1461 			return rc;
1462 	}
1463 
1464 	nnsi = nsinfo__copy(nsi);
1465 	if (nnsi) {
1466 		nsinfo__put(nsi);
1467 
1468 		nnsi->need_setns = false;
1469 		snprintf(filebuf, bufsz, "/tmp/perf-%d.map", nnsi->tgid);
1470 		*nsip = nnsi;
1471 		rc = 0;
1472 	}
1473 
1474 	return rc;
1475 }
1476 
1477 int dso__load(struct dso *dso, struct map *map)
1478 {
1479 	char *name;
1480 	int ret = -1;
1481 	u_int i;
1482 	struct machine *machine;
1483 	char *root_dir = (char *) "";
1484 	int ss_pos = 0;
1485 	struct symsrc ss_[2];
1486 	struct symsrc *syms_ss = NULL, *runtime_ss = NULL;
1487 	bool kmod;
1488 	bool perfmap;
1489 	unsigned char build_id[BUILD_ID_SIZE];
1490 	struct nscookie nsc;
1491 	char newmapname[PATH_MAX];
1492 	const char *map_path = dso->long_name;
1493 
1494 	perfmap = strncmp(dso->name, "/tmp/perf-", 10) == 0;
1495 	if (perfmap) {
1496 		if (dso->nsinfo && (dso__find_perf_map(newmapname,
1497 		    sizeof(newmapname), &dso->nsinfo) == 0)) {
1498 			map_path = newmapname;
1499 		}
1500 	}
1501 
1502 	nsinfo__mountns_enter(dso->nsinfo, &nsc);
1503 	pthread_mutex_lock(&dso->lock);
1504 
1505 	/* check again under the dso->lock */
1506 	if (dso__loaded(dso, map->type)) {
1507 		ret = 1;
1508 		goto out;
1509 	}
1510 
1511 	if (dso->kernel) {
1512 		if (dso->kernel == DSO_TYPE_KERNEL)
1513 			ret = dso__load_kernel_sym(dso, map);
1514 		else if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1515 			ret = dso__load_guest_kernel_sym(dso, map);
1516 
1517 		goto out;
1518 	}
1519 
1520 	if (map->groups && map->groups->machine)
1521 		machine = map->groups->machine;
1522 	else
1523 		machine = NULL;
1524 
1525 	dso->adjust_symbols = 0;
1526 
1527 	if (perfmap) {
1528 		struct stat st;
1529 
1530 		if (lstat(map_path, &st) < 0)
1531 			goto out;
1532 
1533 		if (!symbol_conf.force && st.st_uid && (st.st_uid != geteuid())) {
1534 			pr_warning("File %s not owned by current user or root, "
1535 				   "ignoring it (use -f to override).\n", map_path);
1536 			goto out;
1537 		}
1538 
1539 		ret = dso__load_perf_map(map_path, dso, map);
1540 		dso->symtab_type = ret > 0 ? DSO_BINARY_TYPE__JAVA_JIT :
1541 					     DSO_BINARY_TYPE__NOT_FOUND;
1542 		goto out;
1543 	}
1544 
1545 	if (machine)
1546 		root_dir = machine->root_dir;
1547 
1548 	name = malloc(PATH_MAX);
1549 	if (!name)
1550 		goto out;
1551 
1552 	kmod = dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
1553 		dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
1554 		dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE ||
1555 		dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
1556 
1557 
1558 	/*
1559 	 * Read the build id if possible. This is required for
1560 	 * DSO_BINARY_TYPE__BUILDID_DEBUGINFO to work
1561 	 */
1562 	if (!dso->has_build_id &&
1563 	    is_regular_file(dso->long_name)) {
1564 	    __symbol__join_symfs(name, PATH_MAX, dso->long_name);
1565 	    if (filename__read_build_id(name, build_id, BUILD_ID_SIZE) > 0)
1566 		dso__set_build_id(dso, build_id);
1567 	}
1568 
1569 	/*
1570 	 * Iterate over candidate debug images.
1571 	 * Keep track of "interesting" ones (those which have a symtab, dynsym,
1572 	 * and/or opd section) for processing.
1573 	 */
1574 	for (i = 0; i < DSO_BINARY_TYPE__SYMTAB_CNT; i++) {
1575 		struct symsrc *ss = &ss_[ss_pos];
1576 		bool next_slot = false;
1577 		bool is_reg;
1578 		bool nsexit;
1579 		int sirc;
1580 
1581 		enum dso_binary_type symtab_type = binary_type_symtab[i];
1582 
1583 		nsexit = (symtab_type == DSO_BINARY_TYPE__BUILD_ID_CACHE ||
1584 		    symtab_type == DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO);
1585 
1586 		if (!dso__is_compatible_symtab_type(dso, kmod, symtab_type))
1587 			continue;
1588 
1589 		if (dso__read_binary_type_filename(dso, symtab_type,
1590 						   root_dir, name, PATH_MAX))
1591 			continue;
1592 
1593 		if (nsexit)
1594 			nsinfo__mountns_exit(&nsc);
1595 
1596 		is_reg = is_regular_file(name);
1597 		sirc = symsrc__init(ss, dso, name, symtab_type);
1598 
1599 		if (nsexit)
1600 			nsinfo__mountns_enter(dso->nsinfo, &nsc);
1601 
1602 		if (!is_reg || sirc < 0) {
1603 			if (sirc >= 0)
1604 				symsrc__destroy(ss);
1605 			continue;
1606 		}
1607 
1608 		if (!syms_ss && symsrc__has_symtab(ss)) {
1609 			syms_ss = ss;
1610 			next_slot = true;
1611 			if (!dso->symsrc_filename)
1612 				dso->symsrc_filename = strdup(name);
1613 		}
1614 
1615 		if (!runtime_ss && symsrc__possibly_runtime(ss)) {
1616 			runtime_ss = ss;
1617 			next_slot = true;
1618 		}
1619 
1620 		if (next_slot) {
1621 			ss_pos++;
1622 
1623 			if (syms_ss && runtime_ss)
1624 				break;
1625 		} else {
1626 			symsrc__destroy(ss);
1627 		}
1628 
1629 	}
1630 
1631 	if (!runtime_ss && !syms_ss)
1632 		goto out_free;
1633 
1634 	if (runtime_ss && !syms_ss) {
1635 		syms_ss = runtime_ss;
1636 	}
1637 
1638 	/* We'll have to hope for the best */
1639 	if (!runtime_ss && syms_ss)
1640 		runtime_ss = syms_ss;
1641 
1642 	if (syms_ss)
1643 		ret = dso__load_sym(dso, map, syms_ss, runtime_ss, kmod);
1644 	else
1645 		ret = -1;
1646 
1647 	if (ret > 0) {
1648 		int nr_plt;
1649 
1650 		nr_plt = dso__synthesize_plt_symbols(dso, runtime_ss, map);
1651 		if (nr_plt > 0)
1652 			ret += nr_plt;
1653 	}
1654 
1655 	for (; ss_pos > 0; ss_pos--)
1656 		symsrc__destroy(&ss_[ss_pos - 1]);
1657 out_free:
1658 	free(name);
1659 	if (ret < 0 && strstr(dso->name, " (deleted)") != NULL)
1660 		ret = 0;
1661 out:
1662 	dso__set_loaded(dso, map->type);
1663 	pthread_mutex_unlock(&dso->lock);
1664 	nsinfo__mountns_exit(&nsc);
1665 
1666 	return ret;
1667 }
1668 
1669 struct map *map_groups__find_by_name(struct map_groups *mg,
1670 				     enum map_type type, const char *name)
1671 {
1672 	struct maps *maps = &mg->maps[type];
1673 	struct map *map;
1674 
1675 	pthread_rwlock_rdlock(&maps->lock);
1676 
1677 	for (map = maps__first(maps); map; map = map__next(map)) {
1678 		if (map->dso && strcmp(map->dso->short_name, name) == 0)
1679 			goto out_unlock;
1680 	}
1681 
1682 	map = NULL;
1683 
1684 out_unlock:
1685 	pthread_rwlock_unlock(&maps->lock);
1686 	return map;
1687 }
1688 
1689 int dso__load_vmlinux(struct dso *dso, struct map *map,
1690 		      const char *vmlinux, bool vmlinux_allocated)
1691 {
1692 	int err = -1;
1693 	struct symsrc ss;
1694 	char symfs_vmlinux[PATH_MAX];
1695 	enum dso_binary_type symtab_type;
1696 
1697 	if (vmlinux[0] == '/')
1698 		snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s", vmlinux);
1699 	else
1700 		symbol__join_symfs(symfs_vmlinux, vmlinux);
1701 
1702 	if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1703 		symtab_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
1704 	else
1705 		symtab_type = DSO_BINARY_TYPE__VMLINUX;
1706 
1707 	if (symsrc__init(&ss, dso, symfs_vmlinux, symtab_type))
1708 		return -1;
1709 
1710 	err = dso__load_sym(dso, map, &ss, &ss, 0);
1711 	symsrc__destroy(&ss);
1712 
1713 	if (err > 0) {
1714 		if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1715 			dso->binary_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
1716 		else
1717 			dso->binary_type = DSO_BINARY_TYPE__VMLINUX;
1718 		dso__set_long_name(dso, vmlinux, vmlinux_allocated);
1719 		dso__set_loaded(dso, map->type);
1720 		pr_debug("Using %s for symbols\n", symfs_vmlinux);
1721 	}
1722 
1723 	return err;
1724 }
1725 
1726 int dso__load_vmlinux_path(struct dso *dso, struct map *map)
1727 {
1728 	int i, err = 0;
1729 	char *filename = NULL;
1730 
1731 	pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1732 		 vmlinux_path__nr_entries + 1);
1733 
1734 	for (i = 0; i < vmlinux_path__nr_entries; ++i) {
1735 		err = dso__load_vmlinux(dso, map, vmlinux_path[i], false);
1736 		if (err > 0)
1737 			goto out;
1738 	}
1739 
1740 	if (!symbol_conf.ignore_vmlinux_buildid)
1741 		filename = dso__build_id_filename(dso, NULL, 0, false);
1742 	if (filename != NULL) {
1743 		err = dso__load_vmlinux(dso, map, filename, true);
1744 		if (err > 0)
1745 			goto out;
1746 		free(filename);
1747 	}
1748 out:
1749 	return err;
1750 }
1751 
1752 static bool visible_dir_filter(const char *name, struct dirent *d)
1753 {
1754 	if (d->d_type != DT_DIR)
1755 		return false;
1756 	return lsdir_no_dot_filter(name, d);
1757 }
1758 
1759 static int find_matching_kcore(struct map *map, char *dir, size_t dir_sz)
1760 {
1761 	char kallsyms_filename[PATH_MAX];
1762 	int ret = -1;
1763 	struct strlist *dirs;
1764 	struct str_node *nd;
1765 
1766 	dirs = lsdir(dir, visible_dir_filter);
1767 	if (!dirs)
1768 		return -1;
1769 
1770 	strlist__for_each_entry(nd, dirs) {
1771 		scnprintf(kallsyms_filename, sizeof(kallsyms_filename),
1772 			  "%s/%s/kallsyms", dir, nd->s);
1773 		if (!validate_kcore_addresses(kallsyms_filename, map)) {
1774 			strlcpy(dir, kallsyms_filename, dir_sz);
1775 			ret = 0;
1776 			break;
1777 		}
1778 	}
1779 
1780 	strlist__delete(dirs);
1781 
1782 	return ret;
1783 }
1784 
1785 /*
1786  * Use open(O_RDONLY) to check readability directly instead of access(R_OK)
1787  * since access(R_OK) only checks with real UID/GID but open() use effective
1788  * UID/GID and actual capabilities (e.g. /proc/kcore requires CAP_SYS_RAWIO).
1789  */
1790 static bool filename__readable(const char *file)
1791 {
1792 	int fd = open(file, O_RDONLY);
1793 	if (fd < 0)
1794 		return false;
1795 	close(fd);
1796 	return true;
1797 }
1798 
1799 static char *dso__find_kallsyms(struct dso *dso, struct map *map)
1800 {
1801 	u8 host_build_id[BUILD_ID_SIZE];
1802 	char sbuild_id[SBUILD_ID_SIZE];
1803 	bool is_host = false;
1804 	char path[PATH_MAX];
1805 
1806 	if (!dso->has_build_id) {
1807 		/*
1808 		 * Last resort, if we don't have a build-id and couldn't find
1809 		 * any vmlinux file, try the running kernel kallsyms table.
1810 		 */
1811 		goto proc_kallsyms;
1812 	}
1813 
1814 	if (sysfs__read_build_id("/sys/kernel/notes", host_build_id,
1815 				 sizeof(host_build_id)) == 0)
1816 		is_host = dso__build_id_equal(dso, host_build_id);
1817 
1818 	/* Try a fast path for /proc/kallsyms if possible */
1819 	if (is_host) {
1820 		/*
1821 		 * Do not check the build-id cache, unless we know we cannot use
1822 		 * /proc/kcore or module maps don't match to /proc/kallsyms.
1823 		 * To check readability of /proc/kcore, do not use access(R_OK)
1824 		 * since /proc/kcore requires CAP_SYS_RAWIO to read and access
1825 		 * can't check it.
1826 		 */
1827 		if (filename__readable("/proc/kcore") &&
1828 		    !validate_kcore_addresses("/proc/kallsyms", map))
1829 			goto proc_kallsyms;
1830 	}
1831 
1832 	build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1833 
1834 	/* Find kallsyms in build-id cache with kcore */
1835 	scnprintf(path, sizeof(path), "%s/%s/%s",
1836 		  buildid_dir, DSO__NAME_KCORE, sbuild_id);
1837 
1838 	if (!find_matching_kcore(map, path, sizeof(path)))
1839 		return strdup(path);
1840 
1841 	/* Use current /proc/kallsyms if possible */
1842 	if (is_host) {
1843 proc_kallsyms:
1844 		return strdup("/proc/kallsyms");
1845 	}
1846 
1847 	/* Finally, find a cache of kallsyms */
1848 	if (!build_id_cache__kallsyms_path(sbuild_id, path, sizeof(path))) {
1849 		pr_err("No kallsyms or vmlinux with build-id %s was found\n",
1850 		       sbuild_id);
1851 		return NULL;
1852 	}
1853 
1854 	return strdup(path);
1855 }
1856 
1857 static int dso__load_kernel_sym(struct dso *dso, struct map *map)
1858 {
1859 	int err;
1860 	const char *kallsyms_filename = NULL;
1861 	char *kallsyms_allocated_filename = NULL;
1862 	/*
1863 	 * Step 1: if the user specified a kallsyms or vmlinux filename, use
1864 	 * it and only it, reporting errors to the user if it cannot be used.
1865 	 *
1866 	 * For instance, try to analyse an ARM perf.data file _without_ a
1867 	 * build-id, or if the user specifies the wrong path to the right
1868 	 * vmlinux file, obviously we can't fallback to another vmlinux (a
1869 	 * x86_86 one, on the machine where analysis is being performed, say),
1870 	 * or worse, /proc/kallsyms.
1871 	 *
1872 	 * If the specified file _has_ a build-id and there is a build-id
1873 	 * section in the perf.data file, we will still do the expected
1874 	 * validation in dso__load_vmlinux and will bail out if they don't
1875 	 * match.
1876 	 */
1877 	if (symbol_conf.kallsyms_name != NULL) {
1878 		kallsyms_filename = symbol_conf.kallsyms_name;
1879 		goto do_kallsyms;
1880 	}
1881 
1882 	if (!symbol_conf.ignore_vmlinux && symbol_conf.vmlinux_name != NULL) {
1883 		return dso__load_vmlinux(dso, map, symbol_conf.vmlinux_name, false);
1884 	}
1885 
1886 	if (!symbol_conf.ignore_vmlinux && vmlinux_path != NULL) {
1887 		err = dso__load_vmlinux_path(dso, map);
1888 		if (err > 0)
1889 			return err;
1890 	}
1891 
1892 	/* do not try local files if a symfs was given */
1893 	if (symbol_conf.symfs[0] != 0)
1894 		return -1;
1895 
1896 	kallsyms_allocated_filename = dso__find_kallsyms(dso, map);
1897 	if (!kallsyms_allocated_filename)
1898 		return -1;
1899 
1900 	kallsyms_filename = kallsyms_allocated_filename;
1901 
1902 do_kallsyms:
1903 	err = dso__load_kallsyms(dso, kallsyms_filename, map);
1904 	if (err > 0)
1905 		pr_debug("Using %s for symbols\n", kallsyms_filename);
1906 	free(kallsyms_allocated_filename);
1907 
1908 	if (err > 0 && !dso__is_kcore(dso)) {
1909 		dso->binary_type = DSO_BINARY_TYPE__KALLSYMS;
1910 		dso__set_long_name(dso, DSO__NAME_KALLSYMS, false);
1911 		map__fixup_start(map);
1912 		map__fixup_end(map);
1913 	}
1914 
1915 	return err;
1916 }
1917 
1918 static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map)
1919 {
1920 	int err;
1921 	const char *kallsyms_filename = NULL;
1922 	struct machine *machine;
1923 	char path[PATH_MAX];
1924 
1925 	if (!map->groups) {
1926 		pr_debug("Guest kernel map hasn't the point to groups\n");
1927 		return -1;
1928 	}
1929 	machine = map->groups->machine;
1930 
1931 	if (machine__is_default_guest(machine)) {
1932 		/*
1933 		 * if the user specified a vmlinux filename, use it and only
1934 		 * it, reporting errors to the user if it cannot be used.
1935 		 * Or use file guest_kallsyms inputted by user on commandline
1936 		 */
1937 		if (symbol_conf.default_guest_vmlinux_name != NULL) {
1938 			err = dso__load_vmlinux(dso, map,
1939 						symbol_conf.default_guest_vmlinux_name,
1940 						false);
1941 			return err;
1942 		}
1943 
1944 		kallsyms_filename = symbol_conf.default_guest_kallsyms;
1945 		if (!kallsyms_filename)
1946 			return -1;
1947 	} else {
1948 		sprintf(path, "%s/proc/kallsyms", machine->root_dir);
1949 		kallsyms_filename = path;
1950 	}
1951 
1952 	err = dso__load_kallsyms(dso, kallsyms_filename, map);
1953 	if (err > 0)
1954 		pr_debug("Using %s for symbols\n", kallsyms_filename);
1955 	if (err > 0 && !dso__is_kcore(dso)) {
1956 		dso->binary_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
1957 		machine__mmap_name(machine, path, sizeof(path));
1958 		dso__set_long_name(dso, strdup(path), true);
1959 		map__fixup_start(map);
1960 		map__fixup_end(map);
1961 	}
1962 
1963 	return err;
1964 }
1965 
1966 static void vmlinux_path__exit(void)
1967 {
1968 	while (--vmlinux_path__nr_entries >= 0)
1969 		zfree(&vmlinux_path[vmlinux_path__nr_entries]);
1970 	vmlinux_path__nr_entries = 0;
1971 
1972 	zfree(&vmlinux_path);
1973 }
1974 
1975 static const char * const vmlinux_paths[] = {
1976 	"vmlinux",
1977 	"/boot/vmlinux"
1978 };
1979 
1980 static const char * const vmlinux_paths_upd[] = {
1981 	"/boot/vmlinux-%s",
1982 	"/usr/lib/debug/boot/vmlinux-%s",
1983 	"/lib/modules/%s/build/vmlinux",
1984 	"/usr/lib/debug/lib/modules/%s/vmlinux",
1985 	"/usr/lib/debug/boot/vmlinux-%s.debug"
1986 };
1987 
1988 static int vmlinux_path__add(const char *new_entry)
1989 {
1990 	vmlinux_path[vmlinux_path__nr_entries] = strdup(new_entry);
1991 	if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1992 		return -1;
1993 	++vmlinux_path__nr_entries;
1994 
1995 	return 0;
1996 }
1997 
1998 static int vmlinux_path__init(struct perf_env *env)
1999 {
2000 	struct utsname uts;
2001 	char bf[PATH_MAX];
2002 	char *kernel_version;
2003 	unsigned int i;
2004 
2005 	vmlinux_path = malloc(sizeof(char *) * (ARRAY_SIZE(vmlinux_paths) +
2006 			      ARRAY_SIZE(vmlinux_paths_upd)));
2007 	if (vmlinux_path == NULL)
2008 		return -1;
2009 
2010 	for (i = 0; i < ARRAY_SIZE(vmlinux_paths); i++)
2011 		if (vmlinux_path__add(vmlinux_paths[i]) < 0)
2012 			goto out_fail;
2013 
2014 	/* only try kernel version if no symfs was given */
2015 	if (symbol_conf.symfs[0] != 0)
2016 		return 0;
2017 
2018 	if (env) {
2019 		kernel_version = env->os_release;
2020 	} else {
2021 		if (uname(&uts) < 0)
2022 			goto out_fail;
2023 
2024 		kernel_version = uts.release;
2025 	}
2026 
2027 	for (i = 0; i < ARRAY_SIZE(vmlinux_paths_upd); i++) {
2028 		snprintf(bf, sizeof(bf), vmlinux_paths_upd[i], kernel_version);
2029 		if (vmlinux_path__add(bf) < 0)
2030 			goto out_fail;
2031 	}
2032 
2033 	return 0;
2034 
2035 out_fail:
2036 	vmlinux_path__exit();
2037 	return -1;
2038 }
2039 
2040 int setup_list(struct strlist **list, const char *list_str,
2041 		      const char *list_name)
2042 {
2043 	if (list_str == NULL)
2044 		return 0;
2045 
2046 	*list = strlist__new(list_str, NULL);
2047 	if (!*list) {
2048 		pr_err("problems parsing %s list\n", list_name);
2049 		return -1;
2050 	}
2051 
2052 	symbol_conf.has_filter = true;
2053 	return 0;
2054 }
2055 
2056 int setup_intlist(struct intlist **list, const char *list_str,
2057 		  const char *list_name)
2058 {
2059 	if (list_str == NULL)
2060 		return 0;
2061 
2062 	*list = intlist__new(list_str);
2063 	if (!*list) {
2064 		pr_err("problems parsing %s list\n", list_name);
2065 		return -1;
2066 	}
2067 	return 0;
2068 }
2069 
2070 static bool symbol__read_kptr_restrict(void)
2071 {
2072 	bool value = false;
2073 	FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
2074 
2075 	if (fp != NULL) {
2076 		char line[8];
2077 
2078 		if (fgets(line, sizeof(line), fp) != NULL)
2079 			value = ((geteuid() != 0) || (getuid() != 0)) ?
2080 					(atoi(line) != 0) :
2081 					(atoi(line) == 2);
2082 
2083 		fclose(fp);
2084 	}
2085 
2086 	return value;
2087 }
2088 
2089 int symbol__annotation_init(void)
2090 {
2091 	if (symbol_conf.initialized) {
2092 		pr_err("Annotation needs to be init before symbol__init()\n");
2093 		return -1;
2094 	}
2095 
2096 	if (symbol_conf.init_annotation) {
2097 		pr_warning("Annotation being initialized multiple times\n");
2098 		return 0;
2099 	}
2100 
2101 	symbol_conf.priv_size += sizeof(struct annotation);
2102 	symbol_conf.init_annotation = true;
2103 	return 0;
2104 }
2105 
2106 int symbol__init(struct perf_env *env)
2107 {
2108 	const char *symfs;
2109 
2110 	if (symbol_conf.initialized)
2111 		return 0;
2112 
2113 	symbol_conf.priv_size = PERF_ALIGN(symbol_conf.priv_size, sizeof(u64));
2114 
2115 	symbol__elf_init();
2116 
2117 	if (symbol_conf.sort_by_name)
2118 		symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
2119 					  sizeof(struct symbol));
2120 
2121 	if (symbol_conf.try_vmlinux_path && vmlinux_path__init(env) < 0)
2122 		return -1;
2123 
2124 	if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
2125 		pr_err("'.' is the only non valid --field-separator argument\n");
2126 		return -1;
2127 	}
2128 
2129 	if (setup_list(&symbol_conf.dso_list,
2130 		       symbol_conf.dso_list_str, "dso") < 0)
2131 		return -1;
2132 
2133 	if (setup_list(&symbol_conf.comm_list,
2134 		       symbol_conf.comm_list_str, "comm") < 0)
2135 		goto out_free_dso_list;
2136 
2137 	if (setup_intlist(&symbol_conf.pid_list,
2138 		       symbol_conf.pid_list_str, "pid") < 0)
2139 		goto out_free_comm_list;
2140 
2141 	if (setup_intlist(&symbol_conf.tid_list,
2142 		       symbol_conf.tid_list_str, "tid") < 0)
2143 		goto out_free_pid_list;
2144 
2145 	if (setup_list(&symbol_conf.sym_list,
2146 		       symbol_conf.sym_list_str, "symbol") < 0)
2147 		goto out_free_tid_list;
2148 
2149 	if (setup_list(&symbol_conf.bt_stop_list,
2150 		       symbol_conf.bt_stop_list_str, "symbol") < 0)
2151 		goto out_free_sym_list;
2152 
2153 	/*
2154 	 * A path to symbols of "/" is identical to ""
2155 	 * reset here for simplicity.
2156 	 */
2157 	symfs = realpath(symbol_conf.symfs, NULL);
2158 	if (symfs == NULL)
2159 		symfs = symbol_conf.symfs;
2160 	if (strcmp(symfs, "/") == 0)
2161 		symbol_conf.symfs = "";
2162 	if (symfs != symbol_conf.symfs)
2163 		free((void *)symfs);
2164 
2165 	symbol_conf.kptr_restrict = symbol__read_kptr_restrict();
2166 
2167 	symbol_conf.initialized = true;
2168 	return 0;
2169 
2170 out_free_sym_list:
2171 	strlist__delete(symbol_conf.sym_list);
2172 out_free_tid_list:
2173 	intlist__delete(symbol_conf.tid_list);
2174 out_free_pid_list:
2175 	intlist__delete(symbol_conf.pid_list);
2176 out_free_comm_list:
2177 	strlist__delete(symbol_conf.comm_list);
2178 out_free_dso_list:
2179 	strlist__delete(symbol_conf.dso_list);
2180 	return -1;
2181 }
2182 
2183 void symbol__exit(void)
2184 {
2185 	if (!symbol_conf.initialized)
2186 		return;
2187 	strlist__delete(symbol_conf.bt_stop_list);
2188 	strlist__delete(symbol_conf.sym_list);
2189 	strlist__delete(symbol_conf.dso_list);
2190 	strlist__delete(symbol_conf.comm_list);
2191 	intlist__delete(symbol_conf.tid_list);
2192 	intlist__delete(symbol_conf.pid_list);
2193 	vmlinux_path__exit();
2194 	symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
2195 	symbol_conf.bt_stop_list = NULL;
2196 	symbol_conf.initialized = false;
2197 }
2198 
2199 int symbol__config_symfs(const struct option *opt __maybe_unused,
2200 			 const char *dir, int unset __maybe_unused)
2201 {
2202 	char *bf = NULL;
2203 	int ret;
2204 
2205 	symbol_conf.symfs = strdup(dir);
2206 	if (symbol_conf.symfs == NULL)
2207 		return -ENOMEM;
2208 
2209 	/* skip the locally configured cache if a symfs is given, and
2210 	 * config buildid dir to symfs/.debug
2211 	 */
2212 	ret = asprintf(&bf, "%s/%s", dir, ".debug");
2213 	if (ret < 0)
2214 		return -ENOMEM;
2215 
2216 	set_buildid_dir(bf);
2217 
2218 	free(bf);
2219 	return 0;
2220 }
2221