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