xref: /linux/tools/perf/util/symbol.c (revision d0b73b488c55df905ea8faaad079f8535629ed26)
1 #include <dirent.h>
2 #include <errno.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <sys/param.h>
9 #include <fcntl.h>
10 #include <unistd.h>
11 #include <inttypes.h>
12 #include "build-id.h"
13 #include "util.h"
14 #include "debug.h"
15 #include "machine.h"
16 #include "symbol.h"
17 #include "strlist.h"
18 
19 #include <elf.h>
20 #include <limits.h>
21 #include <sys/utsname.h>
22 
23 #ifndef KSYM_NAME_LEN
24 #define KSYM_NAME_LEN 256
25 #endif
26 
27 static int dso__load_kernel_sym(struct dso *dso, struct map *map,
28 				symbol_filter_t filter);
29 static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
30 			symbol_filter_t filter);
31 static int vmlinux_path__nr_entries;
32 static char **vmlinux_path;
33 
34 struct symbol_conf symbol_conf = {
35 	.exclude_other	  = true,
36 	.use_modules	  = true,
37 	.try_vmlinux_path = true,
38 	.annotate_src	  = true,
39 	.symfs            = "",
40 };
41 
42 static enum dso_binary_type binary_type_symtab[] = {
43 	DSO_BINARY_TYPE__KALLSYMS,
44 	DSO_BINARY_TYPE__GUEST_KALLSYMS,
45 	DSO_BINARY_TYPE__JAVA_JIT,
46 	DSO_BINARY_TYPE__DEBUGLINK,
47 	DSO_BINARY_TYPE__BUILD_ID_CACHE,
48 	DSO_BINARY_TYPE__FEDORA_DEBUGINFO,
49 	DSO_BINARY_TYPE__UBUNTU_DEBUGINFO,
50 	DSO_BINARY_TYPE__BUILDID_DEBUGINFO,
51 	DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
52 	DSO_BINARY_TYPE__GUEST_KMODULE,
53 	DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE,
54 	DSO_BINARY_TYPE__NOT_FOUND,
55 };
56 
57 #define DSO_BINARY_TYPE__SYMTAB_CNT ARRAY_SIZE(binary_type_symtab)
58 
59 bool symbol_type__is_a(char symbol_type, enum map_type map_type)
60 {
61 	symbol_type = toupper(symbol_type);
62 
63 	switch (map_type) {
64 	case MAP__FUNCTION:
65 		return symbol_type == 'T' || symbol_type == 'W';
66 	case MAP__VARIABLE:
67 		return symbol_type == 'D';
68 	default:
69 		return false;
70 	}
71 }
72 
73 static int prefix_underscores_count(const char *str)
74 {
75 	const char *tail = str;
76 
77 	while (*tail == '_')
78 		tail++;
79 
80 	return tail - str;
81 }
82 
83 #define SYMBOL_A 0
84 #define SYMBOL_B 1
85 
86 static int choose_best_symbol(struct symbol *syma, struct symbol *symb)
87 {
88 	s64 a;
89 	s64 b;
90 
91 	/* Prefer a symbol with non zero length */
92 	a = syma->end - syma->start;
93 	b = symb->end - symb->start;
94 	if ((b == 0) && (a > 0))
95 		return SYMBOL_A;
96 	else if ((a == 0) && (b > 0))
97 		return SYMBOL_B;
98 
99 	/* Prefer a non weak symbol over a weak one */
100 	a = syma->binding == STB_WEAK;
101 	b = symb->binding == STB_WEAK;
102 	if (b && !a)
103 		return SYMBOL_A;
104 	if (a && !b)
105 		return SYMBOL_B;
106 
107 	/* Prefer a global symbol over a non global one */
108 	a = syma->binding == STB_GLOBAL;
109 	b = symb->binding == STB_GLOBAL;
110 	if (a && !b)
111 		return SYMBOL_A;
112 	if (b && !a)
113 		return SYMBOL_B;
114 
115 	/* Prefer a symbol with less underscores */
116 	a = prefix_underscores_count(syma->name);
117 	b = prefix_underscores_count(symb->name);
118 	if (b > a)
119 		return SYMBOL_A;
120 	else if (a > b)
121 		return SYMBOL_B;
122 
123 	/* If all else fails, choose the symbol with the longest name */
124 	if (strlen(syma->name) >= strlen(symb->name))
125 		return SYMBOL_A;
126 	else
127 		return SYMBOL_B;
128 }
129 
130 void symbols__fixup_duplicate(struct rb_root *symbols)
131 {
132 	struct rb_node *nd;
133 	struct symbol *curr, *next;
134 
135 	nd = rb_first(symbols);
136 
137 	while (nd) {
138 		curr = rb_entry(nd, struct symbol, rb_node);
139 again:
140 		nd = rb_next(&curr->rb_node);
141 		next = rb_entry(nd, struct symbol, rb_node);
142 
143 		if (!nd)
144 			break;
145 
146 		if (curr->start != next->start)
147 			continue;
148 
149 		if (choose_best_symbol(curr, next) == SYMBOL_A) {
150 			rb_erase(&next->rb_node, symbols);
151 			goto again;
152 		} else {
153 			nd = rb_next(&curr->rb_node);
154 			rb_erase(&curr->rb_node, symbols);
155 		}
156 	}
157 }
158 
159 void symbols__fixup_end(struct rb_root *symbols)
160 {
161 	struct rb_node *nd, *prevnd = rb_first(symbols);
162 	struct symbol *curr, *prev;
163 
164 	if (prevnd == NULL)
165 		return;
166 
167 	curr = rb_entry(prevnd, struct symbol, rb_node);
168 
169 	for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
170 		prev = curr;
171 		curr = rb_entry(nd, struct symbol, rb_node);
172 
173 		if (prev->end == prev->start && prev->end != curr->start)
174 			prev->end = curr->start - 1;
175 	}
176 
177 	/* Last entry */
178 	if (curr->end == curr->start)
179 		curr->end = roundup(curr->start, 4096);
180 }
181 
182 void __map_groups__fixup_end(struct map_groups *mg, enum map_type type)
183 {
184 	struct map *prev, *curr;
185 	struct rb_node *nd, *prevnd = rb_first(&mg->maps[type]);
186 
187 	if (prevnd == NULL)
188 		return;
189 
190 	curr = rb_entry(prevnd, struct map, rb_node);
191 
192 	for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
193 		prev = curr;
194 		curr = rb_entry(nd, struct map, rb_node);
195 		prev->end = curr->start - 1;
196 	}
197 
198 	/*
199 	 * We still haven't the actual symbols, so guess the
200 	 * last map final address.
201 	 */
202 	curr->end = ~0ULL;
203 }
204 
205 static void map_groups__fixup_end(struct map_groups *mg)
206 {
207 	int i;
208 	for (i = 0; i < MAP__NR_TYPES; ++i)
209 		__map_groups__fixup_end(mg, i);
210 }
211 
212 struct symbol *symbol__new(u64 start, u64 len, u8 binding, const char *name)
213 {
214 	size_t namelen = strlen(name) + 1;
215 	struct symbol *sym = calloc(1, (symbol_conf.priv_size +
216 					sizeof(*sym) + namelen));
217 	if (sym == NULL)
218 		return NULL;
219 
220 	if (symbol_conf.priv_size)
221 		sym = ((void *)sym) + symbol_conf.priv_size;
222 
223 	sym->start   = start;
224 	sym->end     = len ? start + len - 1 : start;
225 	sym->binding = binding;
226 	sym->namelen = namelen - 1;
227 
228 	pr_debug4("%s: %s %#" PRIx64 "-%#" PRIx64 "\n",
229 		  __func__, name, start, sym->end);
230 	memcpy(sym->name, name, namelen);
231 
232 	return sym;
233 }
234 
235 void symbol__delete(struct symbol *sym)
236 {
237 	free(((void *)sym) - symbol_conf.priv_size);
238 }
239 
240 size_t symbol__fprintf(struct symbol *sym, FILE *fp)
241 {
242 	return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %c %s\n",
243 		       sym->start, sym->end,
244 		       sym->binding == STB_GLOBAL ? 'g' :
245 		       sym->binding == STB_LOCAL  ? 'l' : 'w',
246 		       sym->name);
247 }
248 
249 size_t symbol__fprintf_symname_offs(const struct symbol *sym,
250 				    const struct addr_location *al, FILE *fp)
251 {
252 	unsigned long offset;
253 	size_t length;
254 
255 	if (sym && sym->name) {
256 		length = fprintf(fp, "%s", sym->name);
257 		if (al) {
258 			offset = al->addr - sym->start;
259 			length += fprintf(fp, "+0x%lx", offset);
260 		}
261 		return length;
262 	} else
263 		return fprintf(fp, "[unknown]");
264 }
265 
266 size_t symbol__fprintf_symname(const struct symbol *sym, FILE *fp)
267 {
268 	return symbol__fprintf_symname_offs(sym, NULL, fp);
269 }
270 
271 void symbols__delete(struct rb_root *symbols)
272 {
273 	struct symbol *pos;
274 	struct rb_node *next = rb_first(symbols);
275 
276 	while (next) {
277 		pos = rb_entry(next, struct symbol, rb_node);
278 		next = rb_next(&pos->rb_node);
279 		rb_erase(&pos->rb_node, symbols);
280 		symbol__delete(pos);
281 	}
282 }
283 
284 void symbols__insert(struct rb_root *symbols, struct symbol *sym)
285 {
286 	struct rb_node **p = &symbols->rb_node;
287 	struct rb_node *parent = NULL;
288 	const u64 ip = sym->start;
289 	struct symbol *s;
290 
291 	while (*p != NULL) {
292 		parent = *p;
293 		s = rb_entry(parent, struct symbol, rb_node);
294 		if (ip < s->start)
295 			p = &(*p)->rb_left;
296 		else
297 			p = &(*p)->rb_right;
298 	}
299 	rb_link_node(&sym->rb_node, parent, p);
300 	rb_insert_color(&sym->rb_node, symbols);
301 }
302 
303 static struct symbol *symbols__find(struct rb_root *symbols, u64 ip)
304 {
305 	struct rb_node *n;
306 
307 	if (symbols == NULL)
308 		return NULL;
309 
310 	n = symbols->rb_node;
311 
312 	while (n) {
313 		struct symbol *s = rb_entry(n, struct symbol, rb_node);
314 
315 		if (ip < s->start)
316 			n = n->rb_left;
317 		else if (ip > s->end)
318 			n = n->rb_right;
319 		else
320 			return s;
321 	}
322 
323 	return NULL;
324 }
325 
326 struct symbol_name_rb_node {
327 	struct rb_node	rb_node;
328 	struct symbol	sym;
329 };
330 
331 static void symbols__insert_by_name(struct rb_root *symbols, struct symbol *sym)
332 {
333 	struct rb_node **p = &symbols->rb_node;
334 	struct rb_node *parent = NULL;
335 	struct symbol_name_rb_node *symn, *s;
336 
337 	symn = container_of(sym, struct symbol_name_rb_node, sym);
338 
339 	while (*p != NULL) {
340 		parent = *p;
341 		s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
342 		if (strcmp(sym->name, s->sym.name) < 0)
343 			p = &(*p)->rb_left;
344 		else
345 			p = &(*p)->rb_right;
346 	}
347 	rb_link_node(&symn->rb_node, parent, p);
348 	rb_insert_color(&symn->rb_node, symbols);
349 }
350 
351 static void symbols__sort_by_name(struct rb_root *symbols,
352 				  struct rb_root *source)
353 {
354 	struct rb_node *nd;
355 
356 	for (nd = rb_first(source); nd; nd = rb_next(nd)) {
357 		struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
358 		symbols__insert_by_name(symbols, pos);
359 	}
360 }
361 
362 static struct symbol *symbols__find_by_name(struct rb_root *symbols,
363 					    const char *name)
364 {
365 	struct rb_node *n;
366 
367 	if (symbols == NULL)
368 		return NULL;
369 
370 	n = symbols->rb_node;
371 
372 	while (n) {
373 		struct symbol_name_rb_node *s;
374 		int cmp;
375 
376 		s = rb_entry(n, struct symbol_name_rb_node, rb_node);
377 		cmp = strcmp(name, s->sym.name);
378 
379 		if (cmp < 0)
380 			n = n->rb_left;
381 		else if (cmp > 0)
382 			n = n->rb_right;
383 		else
384 			return &s->sym;
385 	}
386 
387 	return NULL;
388 }
389 
390 struct symbol *dso__find_symbol(struct dso *dso,
391 				enum map_type type, u64 addr)
392 {
393 	return symbols__find(&dso->symbols[type], addr);
394 }
395 
396 struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type,
397 					const char *name)
398 {
399 	return symbols__find_by_name(&dso->symbol_names[type], name);
400 }
401 
402 void dso__sort_by_name(struct dso *dso, enum map_type type)
403 {
404 	dso__set_sorted_by_name(dso, type);
405 	return symbols__sort_by_name(&dso->symbol_names[type],
406 				     &dso->symbols[type]);
407 }
408 
409 size_t dso__fprintf_symbols_by_name(struct dso *dso,
410 				    enum map_type type, FILE *fp)
411 {
412 	size_t ret = 0;
413 	struct rb_node *nd;
414 	struct symbol_name_rb_node *pos;
415 
416 	for (nd = rb_first(&dso->symbol_names[type]); nd; nd = rb_next(nd)) {
417 		pos = rb_entry(nd, struct symbol_name_rb_node, rb_node);
418 		fprintf(fp, "%s\n", pos->sym.name);
419 	}
420 
421 	return ret;
422 }
423 
424 int kallsyms__parse(const char *filename, void *arg,
425 		    int (*process_symbol)(void *arg, const char *name,
426 					  char type, u64 start))
427 {
428 	char *line = NULL;
429 	size_t n;
430 	int err = -1;
431 	FILE *file = fopen(filename, "r");
432 
433 	if (file == NULL)
434 		goto out_failure;
435 
436 	err = 0;
437 
438 	while (!feof(file)) {
439 		u64 start;
440 		int line_len, len;
441 		char symbol_type;
442 		char *symbol_name;
443 
444 		line_len = getline(&line, &n, file);
445 		if (line_len < 0 || !line)
446 			break;
447 
448 		line[--line_len] = '\0'; /* \n */
449 
450 		len = hex2u64(line, &start);
451 
452 		len++;
453 		if (len + 2 >= line_len)
454 			continue;
455 
456 		symbol_type = line[len];
457 		len += 2;
458 		symbol_name = line + len;
459 		len = line_len - len;
460 
461 		if (len >= KSYM_NAME_LEN) {
462 			err = -1;
463 			break;
464 		}
465 
466 		err = process_symbol(arg, symbol_name,
467 				     symbol_type, start);
468 		if (err)
469 			break;
470 	}
471 
472 	free(line);
473 	fclose(file);
474 	return err;
475 
476 out_failure:
477 	return -1;
478 }
479 
480 struct process_kallsyms_args {
481 	struct map *map;
482 	struct dso *dso;
483 };
484 
485 static u8 kallsyms2elf_type(char type)
486 {
487 	if (type == 'W')
488 		return STB_WEAK;
489 
490 	return isupper(type) ? STB_GLOBAL : STB_LOCAL;
491 }
492 
493 static int map__process_kallsym_symbol(void *arg, const char *name,
494 				       char type, u64 start)
495 {
496 	struct symbol *sym;
497 	struct process_kallsyms_args *a = arg;
498 	struct rb_root *root = &a->dso->symbols[a->map->type];
499 
500 	if (!symbol_type__is_a(type, a->map->type))
501 		return 0;
502 
503 	/*
504 	 * module symbols are not sorted so we add all
505 	 * symbols, setting length to 0, and rely on
506 	 * symbols__fixup_end() to fix it up.
507 	 */
508 	sym = symbol__new(start, 0, kallsyms2elf_type(type), name);
509 	if (sym == NULL)
510 		return -ENOMEM;
511 	/*
512 	 * We will pass the symbols to the filter later, in
513 	 * map__split_kallsyms, when we have split the maps per module
514 	 */
515 	symbols__insert(root, sym);
516 
517 	return 0;
518 }
519 
520 /*
521  * Loads the function entries in /proc/kallsyms into kernel_map->dso,
522  * so that we can in the next step set the symbol ->end address and then
523  * call kernel_maps__split_kallsyms.
524  */
525 static int dso__load_all_kallsyms(struct dso *dso, const char *filename,
526 				  struct map *map)
527 {
528 	struct process_kallsyms_args args = { .map = map, .dso = dso, };
529 	return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
530 }
531 
532 /*
533  * Split the symbols into maps, making sure there are no overlaps, i.e. the
534  * kernel range is broken in several maps, named [kernel].N, as we don't have
535  * the original ELF section names vmlinux have.
536  */
537 static int dso__split_kallsyms(struct dso *dso, struct map *map,
538 			       symbol_filter_t filter)
539 {
540 	struct map_groups *kmaps = map__kmap(map)->kmaps;
541 	struct machine *machine = kmaps->machine;
542 	struct map *curr_map = map;
543 	struct symbol *pos;
544 	int count = 0, moved = 0;
545 	struct rb_root *root = &dso->symbols[map->type];
546 	struct rb_node *next = rb_first(root);
547 	int kernel_range = 0;
548 
549 	while (next) {
550 		char *module;
551 
552 		pos = rb_entry(next, struct symbol, rb_node);
553 		next = rb_next(&pos->rb_node);
554 
555 		module = strchr(pos->name, '\t');
556 		if (module) {
557 			if (!symbol_conf.use_modules)
558 				goto discard_symbol;
559 
560 			*module++ = '\0';
561 
562 			if (strcmp(curr_map->dso->short_name, module)) {
563 				if (curr_map != map &&
564 				    dso->kernel == DSO_TYPE_GUEST_KERNEL &&
565 				    machine__is_default_guest(machine)) {
566 					/*
567 					 * We assume all symbols of a module are
568 					 * continuous in * kallsyms, so curr_map
569 					 * points to a module and all its
570 					 * symbols are in its kmap. Mark it as
571 					 * loaded.
572 					 */
573 					dso__set_loaded(curr_map->dso,
574 							curr_map->type);
575 				}
576 
577 				curr_map = map_groups__find_by_name(kmaps,
578 							map->type, module);
579 				if (curr_map == NULL) {
580 					pr_debug("%s/proc/{kallsyms,modules} "
581 					         "inconsistency while looking "
582 						 "for \"%s\" module!\n",
583 						 machine->root_dir, module);
584 					curr_map = map;
585 					goto discard_symbol;
586 				}
587 
588 				if (curr_map->dso->loaded &&
589 				    !machine__is_default_guest(machine))
590 					goto discard_symbol;
591 			}
592 			/*
593 			 * So that we look just like we get from .ko files,
594 			 * i.e. not prelinked, relative to map->start.
595 			 */
596 			pos->start = curr_map->map_ip(curr_map, pos->start);
597 			pos->end   = curr_map->map_ip(curr_map, pos->end);
598 		} else if (curr_map != map) {
599 			char dso_name[PATH_MAX];
600 			struct dso *ndso;
601 
602 			if (count == 0) {
603 				curr_map = map;
604 				goto filter_symbol;
605 			}
606 
607 			if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
608 				snprintf(dso_name, sizeof(dso_name),
609 					"[guest.kernel].%d",
610 					kernel_range++);
611 			else
612 				snprintf(dso_name, sizeof(dso_name),
613 					"[kernel].%d",
614 					kernel_range++);
615 
616 			ndso = dso__new(dso_name);
617 			if (ndso == NULL)
618 				return -1;
619 
620 			ndso->kernel = dso->kernel;
621 
622 			curr_map = map__new2(pos->start, ndso, map->type);
623 			if (curr_map == NULL) {
624 				dso__delete(ndso);
625 				return -1;
626 			}
627 
628 			curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
629 			map_groups__insert(kmaps, curr_map);
630 			++kernel_range;
631 		}
632 filter_symbol:
633 		if (filter && filter(curr_map, pos)) {
634 discard_symbol:		rb_erase(&pos->rb_node, root);
635 			symbol__delete(pos);
636 		} else {
637 			if (curr_map != map) {
638 				rb_erase(&pos->rb_node, root);
639 				symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
640 				++moved;
641 			} else
642 				++count;
643 		}
644 	}
645 
646 	if (curr_map != map &&
647 	    dso->kernel == DSO_TYPE_GUEST_KERNEL &&
648 	    machine__is_default_guest(kmaps->machine)) {
649 		dso__set_loaded(curr_map->dso, curr_map->type);
650 	}
651 
652 	return count + moved;
653 }
654 
655 static bool symbol__restricted_filename(const char *filename,
656 					const char *restricted_filename)
657 {
658 	bool restricted = false;
659 
660 	if (symbol_conf.kptr_restrict) {
661 		char *r = realpath(filename, NULL);
662 
663 		if (r != NULL) {
664 			restricted = strcmp(r, restricted_filename) == 0;
665 			free(r);
666 			return restricted;
667 		}
668 	}
669 
670 	return restricted;
671 }
672 
673 int dso__load_kallsyms(struct dso *dso, const char *filename,
674 		       struct map *map, symbol_filter_t filter)
675 {
676 	if (symbol__restricted_filename(filename, "/proc/kallsyms"))
677 		return -1;
678 
679 	if (dso__load_all_kallsyms(dso, filename, map) < 0)
680 		return -1;
681 
682 	symbols__fixup_duplicate(&dso->symbols[map->type]);
683 	symbols__fixup_end(&dso->symbols[map->type]);
684 
685 	if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
686 		dso->symtab_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
687 	else
688 		dso->symtab_type = DSO_BINARY_TYPE__KALLSYMS;
689 
690 	return dso__split_kallsyms(dso, map, filter);
691 }
692 
693 static int dso__load_perf_map(struct dso *dso, struct map *map,
694 			      symbol_filter_t filter)
695 {
696 	char *line = NULL;
697 	size_t n;
698 	FILE *file;
699 	int nr_syms = 0;
700 
701 	file = fopen(dso->long_name, "r");
702 	if (file == NULL)
703 		goto out_failure;
704 
705 	while (!feof(file)) {
706 		u64 start, size;
707 		struct symbol *sym;
708 		int line_len, len;
709 
710 		line_len = getline(&line, &n, file);
711 		if (line_len < 0)
712 			break;
713 
714 		if (!line)
715 			goto out_failure;
716 
717 		line[--line_len] = '\0'; /* \n */
718 
719 		len = hex2u64(line, &start);
720 
721 		len++;
722 		if (len + 2 >= line_len)
723 			continue;
724 
725 		len += hex2u64(line + len, &size);
726 
727 		len++;
728 		if (len + 2 >= line_len)
729 			continue;
730 
731 		sym = symbol__new(start, size, STB_GLOBAL, line + len);
732 
733 		if (sym == NULL)
734 			goto out_delete_line;
735 
736 		if (filter && filter(map, sym))
737 			symbol__delete(sym);
738 		else {
739 			symbols__insert(&dso->symbols[map->type], sym);
740 			nr_syms++;
741 		}
742 	}
743 
744 	free(line);
745 	fclose(file);
746 
747 	return nr_syms;
748 
749 out_delete_line:
750 	free(line);
751 out_failure:
752 	return -1;
753 }
754 
755 int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter)
756 {
757 	char *name;
758 	int ret = -1;
759 	u_int i;
760 	struct machine *machine;
761 	char *root_dir = (char *) "";
762 	int ss_pos = 0;
763 	struct symsrc ss_[2];
764 	struct symsrc *syms_ss = NULL, *runtime_ss = NULL;
765 
766 	dso__set_loaded(dso, map->type);
767 
768 	if (dso->kernel == DSO_TYPE_KERNEL)
769 		return dso__load_kernel_sym(dso, map, filter);
770 	else if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
771 		return dso__load_guest_kernel_sym(dso, map, filter);
772 
773 	if (map->groups && map->groups->machine)
774 		machine = map->groups->machine;
775 	else
776 		machine = NULL;
777 
778 	name = malloc(PATH_MAX);
779 	if (!name)
780 		return -1;
781 
782 	dso->adjust_symbols = 0;
783 
784 	if (strncmp(dso->name, "/tmp/perf-", 10) == 0) {
785 		struct stat st;
786 
787 		if (lstat(dso->name, &st) < 0)
788 			return -1;
789 
790 		if (st.st_uid && (st.st_uid != geteuid())) {
791 			pr_warning("File %s not owned by current user or root, "
792 				"ignoring it.\n", dso->name);
793 			return -1;
794 		}
795 
796 		ret = dso__load_perf_map(dso, map, filter);
797 		dso->symtab_type = ret > 0 ? DSO_BINARY_TYPE__JAVA_JIT :
798 					     DSO_BINARY_TYPE__NOT_FOUND;
799 		return ret;
800 	}
801 
802 	if (machine)
803 		root_dir = machine->root_dir;
804 
805 	/* Iterate over candidate debug images.
806 	 * Keep track of "interesting" ones (those which have a symtab, dynsym,
807 	 * and/or opd section) for processing.
808 	 */
809 	for (i = 0; i < DSO_BINARY_TYPE__SYMTAB_CNT; i++) {
810 		struct symsrc *ss = &ss_[ss_pos];
811 		bool next_slot = false;
812 
813 		enum dso_binary_type symtab_type = binary_type_symtab[i];
814 
815 		if (dso__binary_type_file(dso, symtab_type,
816 					  root_dir, name, PATH_MAX))
817 			continue;
818 
819 		/* Name is now the name of the next image to try */
820 		if (symsrc__init(ss, dso, name, symtab_type) < 0)
821 			continue;
822 
823 		if (!syms_ss && symsrc__has_symtab(ss)) {
824 			syms_ss = ss;
825 			next_slot = true;
826 		}
827 
828 		if (!runtime_ss && symsrc__possibly_runtime(ss)) {
829 			runtime_ss = ss;
830 			next_slot = true;
831 		}
832 
833 		if (next_slot) {
834 			ss_pos++;
835 
836 			if (syms_ss && runtime_ss)
837 				break;
838 		}
839 
840 	}
841 
842 	if (!runtime_ss && !syms_ss)
843 		goto out_free;
844 
845 	if (runtime_ss && !syms_ss) {
846 		syms_ss = runtime_ss;
847 	}
848 
849 	/* We'll have to hope for the best */
850 	if (!runtime_ss && syms_ss)
851 		runtime_ss = syms_ss;
852 
853 	if (syms_ss)
854 		ret = dso__load_sym(dso, map, syms_ss, runtime_ss, filter, 0);
855 	else
856 		ret = -1;
857 
858 	if (ret > 0) {
859 		int nr_plt;
860 
861 		nr_plt = dso__synthesize_plt_symbols(dso, runtime_ss, map, filter);
862 		if (nr_plt > 0)
863 			ret += nr_plt;
864 	}
865 
866 	for (; ss_pos > 0; ss_pos--)
867 		symsrc__destroy(&ss_[ss_pos - 1]);
868 out_free:
869 	free(name);
870 	if (ret < 0 && strstr(dso->name, " (deleted)") != NULL)
871 		return 0;
872 	return ret;
873 }
874 
875 struct map *map_groups__find_by_name(struct map_groups *mg,
876 				     enum map_type type, const char *name)
877 {
878 	struct rb_node *nd;
879 
880 	for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
881 		struct map *map = rb_entry(nd, struct map, rb_node);
882 
883 		if (map->dso && strcmp(map->dso->short_name, name) == 0)
884 			return map;
885 	}
886 
887 	return NULL;
888 }
889 
890 static int map_groups__set_modules_path_dir(struct map_groups *mg,
891 				const char *dir_name)
892 {
893 	struct dirent *dent;
894 	DIR *dir = opendir(dir_name);
895 	int ret = 0;
896 
897 	if (!dir) {
898 		pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
899 		return -1;
900 	}
901 
902 	while ((dent = readdir(dir)) != NULL) {
903 		char path[PATH_MAX];
904 		struct stat st;
905 
906 		/*sshfs might return bad dent->d_type, so we have to stat*/
907 		snprintf(path, sizeof(path), "%s/%s", dir_name, dent->d_name);
908 		if (stat(path, &st))
909 			continue;
910 
911 		if (S_ISDIR(st.st_mode)) {
912 			if (!strcmp(dent->d_name, ".") ||
913 			    !strcmp(dent->d_name, ".."))
914 				continue;
915 
916 			ret = map_groups__set_modules_path_dir(mg, path);
917 			if (ret < 0)
918 				goto out;
919 		} else {
920 			char *dot = strrchr(dent->d_name, '.'),
921 			     dso_name[PATH_MAX];
922 			struct map *map;
923 			char *long_name;
924 
925 			if (dot == NULL || strcmp(dot, ".ko"))
926 				continue;
927 			snprintf(dso_name, sizeof(dso_name), "[%.*s]",
928 				 (int)(dot - dent->d_name), dent->d_name);
929 
930 			strxfrchar(dso_name, '-', '_');
931 			map = map_groups__find_by_name(mg, MAP__FUNCTION,
932 						       dso_name);
933 			if (map == NULL)
934 				continue;
935 
936 			long_name = strdup(path);
937 			if (long_name == NULL) {
938 				ret = -1;
939 				goto out;
940 			}
941 			dso__set_long_name(map->dso, long_name);
942 			map->dso->lname_alloc = 1;
943 			dso__kernel_module_get_build_id(map->dso, "");
944 		}
945 	}
946 
947 out:
948 	closedir(dir);
949 	return ret;
950 }
951 
952 static char *get_kernel_version(const char *root_dir)
953 {
954 	char version[PATH_MAX];
955 	FILE *file;
956 	char *name, *tmp;
957 	const char *prefix = "Linux version ";
958 
959 	sprintf(version, "%s/proc/version", root_dir);
960 	file = fopen(version, "r");
961 	if (!file)
962 		return NULL;
963 
964 	version[0] = '\0';
965 	tmp = fgets(version, sizeof(version), file);
966 	fclose(file);
967 
968 	name = strstr(version, prefix);
969 	if (!name)
970 		return NULL;
971 	name += strlen(prefix);
972 	tmp = strchr(name, ' ');
973 	if (tmp)
974 		*tmp = '\0';
975 
976 	return strdup(name);
977 }
978 
979 static int machine__set_modules_path(struct machine *machine)
980 {
981 	char *version;
982 	char modules_path[PATH_MAX];
983 
984 	version = get_kernel_version(machine->root_dir);
985 	if (!version)
986 		return -1;
987 
988 	snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s/kernel",
989 		 machine->root_dir, version);
990 	free(version);
991 
992 	return map_groups__set_modules_path_dir(&machine->kmaps, modules_path);
993 }
994 
995 struct map *machine__new_module(struct machine *machine, u64 start,
996 				const char *filename)
997 {
998 	struct map *map;
999 	struct dso *dso = __dsos__findnew(&machine->kernel_dsos, filename);
1000 
1001 	if (dso == NULL)
1002 		return NULL;
1003 
1004 	map = map__new2(start, dso, MAP__FUNCTION);
1005 	if (map == NULL)
1006 		return NULL;
1007 
1008 	if (machine__is_host(machine))
1009 		dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE;
1010 	else
1011 		dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE;
1012 	map_groups__insert(&machine->kmaps, map);
1013 	return map;
1014 }
1015 
1016 static int machine__create_modules(struct machine *machine)
1017 {
1018 	char *line = NULL;
1019 	size_t n;
1020 	FILE *file;
1021 	struct map *map;
1022 	const char *modules;
1023 	char path[PATH_MAX];
1024 
1025 	if (machine__is_default_guest(machine))
1026 		modules = symbol_conf.default_guest_modules;
1027 	else {
1028 		sprintf(path, "%s/proc/modules", machine->root_dir);
1029 		modules = path;
1030 	}
1031 
1032 	if (symbol__restricted_filename(path, "/proc/modules"))
1033 		return -1;
1034 
1035 	file = fopen(modules, "r");
1036 	if (file == NULL)
1037 		return -1;
1038 
1039 	while (!feof(file)) {
1040 		char name[PATH_MAX];
1041 		u64 start;
1042 		char *sep;
1043 		int line_len;
1044 
1045 		line_len = getline(&line, &n, file);
1046 		if (line_len < 0)
1047 			break;
1048 
1049 		if (!line)
1050 			goto out_failure;
1051 
1052 		line[--line_len] = '\0'; /* \n */
1053 
1054 		sep = strrchr(line, 'x');
1055 		if (sep == NULL)
1056 			continue;
1057 
1058 		hex2u64(sep + 1, &start);
1059 
1060 		sep = strchr(line, ' ');
1061 		if (sep == NULL)
1062 			continue;
1063 
1064 		*sep = '\0';
1065 
1066 		snprintf(name, sizeof(name), "[%s]", line);
1067 		map = machine__new_module(machine, start, name);
1068 		if (map == NULL)
1069 			goto out_delete_line;
1070 		dso__kernel_module_get_build_id(map->dso, machine->root_dir);
1071 	}
1072 
1073 	free(line);
1074 	fclose(file);
1075 
1076 	return machine__set_modules_path(machine);
1077 
1078 out_delete_line:
1079 	free(line);
1080 out_failure:
1081 	return -1;
1082 }
1083 
1084 int dso__load_vmlinux(struct dso *dso, struct map *map,
1085 		      const char *vmlinux, symbol_filter_t filter)
1086 {
1087 	int err = -1;
1088 	struct symsrc ss;
1089 	char symfs_vmlinux[PATH_MAX];
1090 	enum dso_binary_type symtab_type;
1091 
1092 	snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s%s",
1093 		 symbol_conf.symfs, vmlinux);
1094 
1095 	if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1096 		symtab_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
1097 	else
1098 		symtab_type = DSO_BINARY_TYPE__VMLINUX;
1099 
1100 	if (symsrc__init(&ss, dso, symfs_vmlinux, symtab_type))
1101 		return -1;
1102 
1103 	err = dso__load_sym(dso, map, &ss, &ss, filter, 0);
1104 	symsrc__destroy(&ss);
1105 
1106 	if (err > 0) {
1107 		dso__set_long_name(dso, (char *)vmlinux);
1108 		dso__set_loaded(dso, map->type);
1109 		pr_debug("Using %s for symbols\n", symfs_vmlinux);
1110 	}
1111 
1112 	return err;
1113 }
1114 
1115 int dso__load_vmlinux_path(struct dso *dso, struct map *map,
1116 			   symbol_filter_t filter)
1117 {
1118 	int i, err = 0;
1119 	char *filename;
1120 
1121 	pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1122 		 vmlinux_path__nr_entries + 1);
1123 
1124 	filename = dso__build_id_filename(dso, NULL, 0);
1125 	if (filename != NULL) {
1126 		err = dso__load_vmlinux(dso, map, filename, filter);
1127 		if (err > 0)
1128 			goto out;
1129 		free(filename);
1130 	}
1131 
1132 	for (i = 0; i < vmlinux_path__nr_entries; ++i) {
1133 		err = dso__load_vmlinux(dso, map, vmlinux_path[i], filter);
1134 		if (err > 0) {
1135 			dso__set_long_name(dso, strdup(vmlinux_path[i]));
1136 			break;
1137 		}
1138 	}
1139 out:
1140 	return err;
1141 }
1142 
1143 static int dso__load_kernel_sym(struct dso *dso, struct map *map,
1144 				symbol_filter_t filter)
1145 {
1146 	int err;
1147 	const char *kallsyms_filename = NULL;
1148 	char *kallsyms_allocated_filename = NULL;
1149 	/*
1150 	 * Step 1: if the user specified a kallsyms or vmlinux filename, use
1151 	 * it and only it, reporting errors to the user if it cannot be used.
1152 	 *
1153 	 * For instance, try to analyse an ARM perf.data file _without_ a
1154 	 * build-id, or if the user specifies the wrong path to the right
1155 	 * vmlinux file, obviously we can't fallback to another vmlinux (a
1156 	 * x86_86 one, on the machine where analysis is being performed, say),
1157 	 * or worse, /proc/kallsyms.
1158 	 *
1159 	 * If the specified file _has_ a build-id and there is a build-id
1160 	 * section in the perf.data file, we will still do the expected
1161 	 * validation in dso__load_vmlinux and will bail out if they don't
1162 	 * match.
1163 	 */
1164 	if (symbol_conf.kallsyms_name != NULL) {
1165 		kallsyms_filename = symbol_conf.kallsyms_name;
1166 		goto do_kallsyms;
1167 	}
1168 
1169 	if (symbol_conf.vmlinux_name != NULL) {
1170 		err = dso__load_vmlinux(dso, map,
1171 					symbol_conf.vmlinux_name, filter);
1172 		if (err > 0) {
1173 			dso__set_long_name(dso,
1174 					   strdup(symbol_conf.vmlinux_name));
1175 			goto out_fixup;
1176 		}
1177 		return err;
1178 	}
1179 
1180 	if (vmlinux_path != NULL) {
1181 		err = dso__load_vmlinux_path(dso, map, filter);
1182 		if (err > 0)
1183 			goto out_fixup;
1184 	}
1185 
1186 	/* do not try local files if a symfs was given */
1187 	if (symbol_conf.symfs[0] != 0)
1188 		return -1;
1189 
1190 	/*
1191 	 * Say the kernel DSO was created when processing the build-id header table,
1192 	 * we have a build-id, so check if it is the same as the running kernel,
1193 	 * using it if it is.
1194 	 */
1195 	if (dso->has_build_id) {
1196 		u8 kallsyms_build_id[BUILD_ID_SIZE];
1197 		char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1198 
1199 		if (sysfs__read_build_id("/sys/kernel/notes", kallsyms_build_id,
1200 					 sizeof(kallsyms_build_id)) == 0) {
1201 			if (dso__build_id_equal(dso, kallsyms_build_id)) {
1202 				kallsyms_filename = "/proc/kallsyms";
1203 				goto do_kallsyms;
1204 			}
1205 		}
1206 		/*
1207 		 * Now look if we have it on the build-id cache in
1208 		 * $HOME/.debug/[kernel.kallsyms].
1209 		 */
1210 		build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1211 				  sbuild_id);
1212 
1213 		if (asprintf(&kallsyms_allocated_filename,
1214 			     "%s/.debug/[kernel.kallsyms]/%s",
1215 			     getenv("HOME"), sbuild_id) == -1) {
1216 			pr_err("Not enough memory for kallsyms file lookup\n");
1217 			return -1;
1218 		}
1219 
1220 		kallsyms_filename = kallsyms_allocated_filename;
1221 
1222 		if (access(kallsyms_filename, F_OK)) {
1223 			pr_err("No kallsyms or vmlinux with build-id %s "
1224 			       "was found\n", sbuild_id);
1225 			free(kallsyms_allocated_filename);
1226 			return -1;
1227 		}
1228 	} else {
1229 		/*
1230 		 * Last resort, if we don't have a build-id and couldn't find
1231 		 * any vmlinux file, try the running kernel kallsyms table.
1232 		 */
1233 		kallsyms_filename = "/proc/kallsyms";
1234 	}
1235 
1236 do_kallsyms:
1237 	err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
1238 	if (err > 0)
1239 		pr_debug("Using %s for symbols\n", kallsyms_filename);
1240 	free(kallsyms_allocated_filename);
1241 
1242 	if (err > 0) {
1243 		dso__set_long_name(dso, strdup("[kernel.kallsyms]"));
1244 out_fixup:
1245 		map__fixup_start(map);
1246 		map__fixup_end(map);
1247 	}
1248 
1249 	return err;
1250 }
1251 
1252 static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
1253 				      symbol_filter_t filter)
1254 {
1255 	int err;
1256 	const char *kallsyms_filename = NULL;
1257 	struct machine *machine;
1258 	char path[PATH_MAX];
1259 
1260 	if (!map->groups) {
1261 		pr_debug("Guest kernel map hasn't the point to groups\n");
1262 		return -1;
1263 	}
1264 	machine = map->groups->machine;
1265 
1266 	if (machine__is_default_guest(machine)) {
1267 		/*
1268 		 * if the user specified a vmlinux filename, use it and only
1269 		 * it, reporting errors to the user if it cannot be used.
1270 		 * Or use file guest_kallsyms inputted by user on commandline
1271 		 */
1272 		if (symbol_conf.default_guest_vmlinux_name != NULL) {
1273 			err = dso__load_vmlinux(dso, map,
1274 				symbol_conf.default_guest_vmlinux_name, filter);
1275 			goto out_try_fixup;
1276 		}
1277 
1278 		kallsyms_filename = symbol_conf.default_guest_kallsyms;
1279 		if (!kallsyms_filename)
1280 			return -1;
1281 	} else {
1282 		sprintf(path, "%s/proc/kallsyms", machine->root_dir);
1283 		kallsyms_filename = path;
1284 	}
1285 
1286 	err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
1287 	if (err > 0)
1288 		pr_debug("Using %s for symbols\n", kallsyms_filename);
1289 
1290 out_try_fixup:
1291 	if (err > 0) {
1292 		if (kallsyms_filename != NULL) {
1293 			machine__mmap_name(machine, path, sizeof(path));
1294 			dso__set_long_name(dso, strdup(path));
1295 		}
1296 		map__fixup_start(map);
1297 		map__fixup_end(map);
1298 	}
1299 
1300 	return err;
1301 }
1302 
1303 size_t machines__fprintf_dsos(struct rb_root *machines, FILE *fp)
1304 {
1305 	struct rb_node *nd;
1306 	size_t ret = 0;
1307 
1308 	for (nd = rb_first(machines); nd; nd = rb_next(nd)) {
1309 		struct machine *pos = rb_entry(nd, struct machine, rb_node);
1310 		ret += __dsos__fprintf(&pos->kernel_dsos, fp);
1311 		ret += __dsos__fprintf(&pos->user_dsos, fp);
1312 	}
1313 
1314 	return ret;
1315 }
1316 
1317 size_t machine__fprintf_dsos_buildid(struct machine *machine, FILE *fp,
1318 				     bool with_hits)
1319 {
1320 	return __dsos__fprintf_buildid(&machine->kernel_dsos, fp, with_hits) +
1321 	       __dsos__fprintf_buildid(&machine->user_dsos, fp, with_hits);
1322 }
1323 
1324 size_t machines__fprintf_dsos_buildid(struct rb_root *machines,
1325 				      FILE *fp, bool with_hits)
1326 {
1327 	struct rb_node *nd;
1328 	size_t ret = 0;
1329 
1330 	for (nd = rb_first(machines); nd; nd = rb_next(nd)) {
1331 		struct machine *pos = rb_entry(nd, struct machine, rb_node);
1332 		ret += machine__fprintf_dsos_buildid(pos, fp, with_hits);
1333 	}
1334 	return ret;
1335 }
1336 
1337 static struct dso *machine__get_kernel(struct machine *machine)
1338 {
1339 	const char *vmlinux_name = NULL;
1340 	struct dso *kernel;
1341 
1342 	if (machine__is_host(machine)) {
1343 		vmlinux_name = symbol_conf.vmlinux_name;
1344 		if (!vmlinux_name)
1345 			vmlinux_name = "[kernel.kallsyms]";
1346 
1347 		kernel = dso__kernel_findnew(machine, vmlinux_name,
1348 					     "[kernel]",
1349 					     DSO_TYPE_KERNEL);
1350 	} else {
1351 		char bf[PATH_MAX];
1352 
1353 		if (machine__is_default_guest(machine))
1354 			vmlinux_name = symbol_conf.default_guest_vmlinux_name;
1355 		if (!vmlinux_name)
1356 			vmlinux_name = machine__mmap_name(machine, bf,
1357 							  sizeof(bf));
1358 
1359 		kernel = dso__kernel_findnew(machine, vmlinux_name,
1360 					     "[guest.kernel]",
1361 					     DSO_TYPE_GUEST_KERNEL);
1362 	}
1363 
1364 	if (kernel != NULL && (!kernel->has_build_id))
1365 		dso__read_running_kernel_build_id(kernel, machine);
1366 
1367 	return kernel;
1368 }
1369 
1370 struct process_args {
1371 	u64 start;
1372 };
1373 
1374 static int symbol__in_kernel(void *arg, const char *name,
1375 			     char type __maybe_unused, u64 start)
1376 {
1377 	struct process_args *args = arg;
1378 
1379 	if (strchr(name, '['))
1380 		return 0;
1381 
1382 	args->start = start;
1383 	return 1;
1384 }
1385 
1386 /* Figure out the start address of kernel map from /proc/kallsyms */
1387 static u64 machine__get_kernel_start_addr(struct machine *machine)
1388 {
1389 	const char *filename;
1390 	char path[PATH_MAX];
1391 	struct process_args args;
1392 
1393 	if (machine__is_host(machine)) {
1394 		filename = "/proc/kallsyms";
1395 	} else {
1396 		if (machine__is_default_guest(machine))
1397 			filename = (char *)symbol_conf.default_guest_kallsyms;
1398 		else {
1399 			sprintf(path, "%s/proc/kallsyms", machine->root_dir);
1400 			filename = path;
1401 		}
1402 	}
1403 
1404 	if (symbol__restricted_filename(filename, "/proc/kallsyms"))
1405 		return 0;
1406 
1407 	if (kallsyms__parse(filename, &args, symbol__in_kernel) <= 0)
1408 		return 0;
1409 
1410 	return args.start;
1411 }
1412 
1413 int __machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
1414 {
1415 	enum map_type type;
1416 	u64 start = machine__get_kernel_start_addr(machine);
1417 
1418 	for (type = 0; type < MAP__NR_TYPES; ++type) {
1419 		struct kmap *kmap;
1420 
1421 		machine->vmlinux_maps[type] = map__new2(start, kernel, type);
1422 		if (machine->vmlinux_maps[type] == NULL)
1423 			return -1;
1424 
1425 		machine->vmlinux_maps[type]->map_ip =
1426 			machine->vmlinux_maps[type]->unmap_ip =
1427 				identity__map_ip;
1428 		kmap = map__kmap(machine->vmlinux_maps[type]);
1429 		kmap->kmaps = &machine->kmaps;
1430 		map_groups__insert(&machine->kmaps,
1431 				   machine->vmlinux_maps[type]);
1432 	}
1433 
1434 	return 0;
1435 }
1436 
1437 void machine__destroy_kernel_maps(struct machine *machine)
1438 {
1439 	enum map_type type;
1440 
1441 	for (type = 0; type < MAP__NR_TYPES; ++type) {
1442 		struct kmap *kmap;
1443 
1444 		if (machine->vmlinux_maps[type] == NULL)
1445 			continue;
1446 
1447 		kmap = map__kmap(machine->vmlinux_maps[type]);
1448 		map_groups__remove(&machine->kmaps,
1449 				   machine->vmlinux_maps[type]);
1450 		if (kmap->ref_reloc_sym) {
1451 			/*
1452 			 * ref_reloc_sym is shared among all maps, so free just
1453 			 * on one of them.
1454 			 */
1455 			if (type == MAP__FUNCTION) {
1456 				free((char *)kmap->ref_reloc_sym->name);
1457 				kmap->ref_reloc_sym->name = NULL;
1458 				free(kmap->ref_reloc_sym);
1459 			}
1460 			kmap->ref_reloc_sym = NULL;
1461 		}
1462 
1463 		map__delete(machine->vmlinux_maps[type]);
1464 		machine->vmlinux_maps[type] = NULL;
1465 	}
1466 }
1467 
1468 int machine__create_kernel_maps(struct machine *machine)
1469 {
1470 	struct dso *kernel = machine__get_kernel(machine);
1471 
1472 	if (kernel == NULL ||
1473 	    __machine__create_kernel_maps(machine, kernel) < 0)
1474 		return -1;
1475 
1476 	if (symbol_conf.use_modules && machine__create_modules(machine) < 0) {
1477 		if (machine__is_host(machine))
1478 			pr_debug("Problems creating module maps, "
1479 				 "continuing anyway...\n");
1480 		else
1481 			pr_debug("Problems creating module maps for guest %d, "
1482 				 "continuing anyway...\n", machine->pid);
1483 	}
1484 
1485 	/*
1486 	 * Now that we have all the maps created, just set the ->end of them:
1487 	 */
1488 	map_groups__fixup_end(&machine->kmaps);
1489 	return 0;
1490 }
1491 
1492 static void vmlinux_path__exit(void)
1493 {
1494 	while (--vmlinux_path__nr_entries >= 0) {
1495 		free(vmlinux_path[vmlinux_path__nr_entries]);
1496 		vmlinux_path[vmlinux_path__nr_entries] = NULL;
1497 	}
1498 
1499 	free(vmlinux_path);
1500 	vmlinux_path = NULL;
1501 }
1502 
1503 static int vmlinux_path__init(void)
1504 {
1505 	struct utsname uts;
1506 	char bf[PATH_MAX];
1507 
1508 	vmlinux_path = malloc(sizeof(char *) * 5);
1509 	if (vmlinux_path == NULL)
1510 		return -1;
1511 
1512 	vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
1513 	if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1514 		goto out_fail;
1515 	++vmlinux_path__nr_entries;
1516 	vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
1517 	if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1518 		goto out_fail;
1519 	++vmlinux_path__nr_entries;
1520 
1521 	/* only try running kernel version if no symfs was given */
1522 	if (symbol_conf.symfs[0] != 0)
1523 		return 0;
1524 
1525 	if (uname(&uts) < 0)
1526 		return -1;
1527 
1528 	snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
1529 	vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1530 	if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1531 		goto out_fail;
1532 	++vmlinux_path__nr_entries;
1533 	snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
1534 	vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1535 	if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1536 		goto out_fail;
1537 	++vmlinux_path__nr_entries;
1538 	snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
1539 		 uts.release);
1540 	vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1541 	if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1542 		goto out_fail;
1543 	++vmlinux_path__nr_entries;
1544 
1545 	return 0;
1546 
1547 out_fail:
1548 	vmlinux_path__exit();
1549 	return -1;
1550 }
1551 
1552 size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp)
1553 {
1554 	int i;
1555 	size_t printed = 0;
1556 	struct dso *kdso = machine->vmlinux_maps[MAP__FUNCTION]->dso;
1557 
1558 	if (kdso->has_build_id) {
1559 		char filename[PATH_MAX];
1560 		if (dso__build_id_filename(kdso, filename, sizeof(filename)))
1561 			printed += fprintf(fp, "[0] %s\n", filename);
1562 	}
1563 
1564 	for (i = 0; i < vmlinux_path__nr_entries; ++i)
1565 		printed += fprintf(fp, "[%d] %s\n",
1566 				   i + kdso->has_build_id, vmlinux_path[i]);
1567 
1568 	return printed;
1569 }
1570 
1571 static int setup_list(struct strlist **list, const char *list_str,
1572 		      const char *list_name)
1573 {
1574 	if (list_str == NULL)
1575 		return 0;
1576 
1577 	*list = strlist__new(true, list_str);
1578 	if (!*list) {
1579 		pr_err("problems parsing %s list\n", list_name);
1580 		return -1;
1581 	}
1582 	return 0;
1583 }
1584 
1585 static bool symbol__read_kptr_restrict(void)
1586 {
1587 	bool value = false;
1588 
1589 	if (geteuid() != 0) {
1590 		FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
1591 		if (fp != NULL) {
1592 			char line[8];
1593 
1594 			if (fgets(line, sizeof(line), fp) != NULL)
1595 				value = atoi(line) != 0;
1596 
1597 			fclose(fp);
1598 		}
1599 	}
1600 
1601 	return value;
1602 }
1603 
1604 int symbol__init(void)
1605 {
1606 	const char *symfs;
1607 
1608 	if (symbol_conf.initialized)
1609 		return 0;
1610 
1611 	symbol_conf.priv_size = PERF_ALIGN(symbol_conf.priv_size, sizeof(u64));
1612 
1613 	symbol__elf_init();
1614 
1615 	if (symbol_conf.sort_by_name)
1616 		symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
1617 					  sizeof(struct symbol));
1618 
1619 	if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0)
1620 		return -1;
1621 
1622 	if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
1623 		pr_err("'.' is the only non valid --field-separator argument\n");
1624 		return -1;
1625 	}
1626 
1627 	if (setup_list(&symbol_conf.dso_list,
1628 		       symbol_conf.dso_list_str, "dso") < 0)
1629 		return -1;
1630 
1631 	if (setup_list(&symbol_conf.comm_list,
1632 		       symbol_conf.comm_list_str, "comm") < 0)
1633 		goto out_free_dso_list;
1634 
1635 	if (setup_list(&symbol_conf.sym_list,
1636 		       symbol_conf.sym_list_str, "symbol") < 0)
1637 		goto out_free_comm_list;
1638 
1639 	/*
1640 	 * A path to symbols of "/" is identical to ""
1641 	 * reset here for simplicity.
1642 	 */
1643 	symfs = realpath(symbol_conf.symfs, NULL);
1644 	if (symfs == NULL)
1645 		symfs = symbol_conf.symfs;
1646 	if (strcmp(symfs, "/") == 0)
1647 		symbol_conf.symfs = "";
1648 	if (symfs != symbol_conf.symfs)
1649 		free((void *)symfs);
1650 
1651 	symbol_conf.kptr_restrict = symbol__read_kptr_restrict();
1652 
1653 	symbol_conf.initialized = true;
1654 	return 0;
1655 
1656 out_free_comm_list:
1657 	strlist__delete(symbol_conf.comm_list);
1658 out_free_dso_list:
1659 	strlist__delete(symbol_conf.dso_list);
1660 	return -1;
1661 }
1662 
1663 void symbol__exit(void)
1664 {
1665 	if (!symbol_conf.initialized)
1666 		return;
1667 	strlist__delete(symbol_conf.sym_list);
1668 	strlist__delete(symbol_conf.dso_list);
1669 	strlist__delete(symbol_conf.comm_list);
1670 	vmlinux_path__exit();
1671 	symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
1672 	symbol_conf.initialized = false;
1673 }
1674 
1675 int machines__create_kernel_maps(struct rb_root *machines, pid_t pid)
1676 {
1677 	struct machine *machine = machines__findnew(machines, pid);
1678 
1679 	if (machine == NULL)
1680 		return -1;
1681 
1682 	return machine__create_kernel_maps(machine);
1683 }
1684 
1685 int machines__create_guest_kernel_maps(struct rb_root *machines)
1686 {
1687 	int ret = 0;
1688 	struct dirent **namelist = NULL;
1689 	int i, items = 0;
1690 	char path[PATH_MAX];
1691 	pid_t pid;
1692 	char *endp;
1693 
1694 	if (symbol_conf.default_guest_vmlinux_name ||
1695 	    symbol_conf.default_guest_modules ||
1696 	    symbol_conf.default_guest_kallsyms) {
1697 		machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID);
1698 	}
1699 
1700 	if (symbol_conf.guestmount) {
1701 		items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
1702 		if (items <= 0)
1703 			return -ENOENT;
1704 		for (i = 0; i < items; i++) {
1705 			if (!isdigit(namelist[i]->d_name[0])) {
1706 				/* Filter out . and .. */
1707 				continue;
1708 			}
1709 			pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10);
1710 			if ((*endp != '\0') ||
1711 			    (endp == namelist[i]->d_name) ||
1712 			    (errno == ERANGE)) {
1713 				pr_debug("invalid directory (%s). Skipping.\n",
1714 					 namelist[i]->d_name);
1715 				continue;
1716 			}
1717 			sprintf(path, "%s/%s/proc/kallsyms",
1718 				symbol_conf.guestmount,
1719 				namelist[i]->d_name);
1720 			ret = access(path, R_OK);
1721 			if (ret) {
1722 				pr_debug("Can't access file %s\n", path);
1723 				goto failure;
1724 			}
1725 			machines__create_kernel_maps(machines, pid);
1726 		}
1727 failure:
1728 		free(namelist);
1729 	}
1730 
1731 	return ret;
1732 }
1733 
1734 void machines__destroy_guest_kernel_maps(struct rb_root *machines)
1735 {
1736 	struct rb_node *next = rb_first(machines);
1737 
1738 	while (next) {
1739 		struct machine *pos = rb_entry(next, struct machine, rb_node);
1740 
1741 		next = rb_next(&pos->rb_node);
1742 		rb_erase(&pos->rb_node, machines);
1743 		machine__delete(pos);
1744 	}
1745 }
1746 
1747 int machine__load_kallsyms(struct machine *machine, const char *filename,
1748 			   enum map_type type, symbol_filter_t filter)
1749 {
1750 	struct map *map = machine->vmlinux_maps[type];
1751 	int ret = dso__load_kallsyms(map->dso, filename, map, filter);
1752 
1753 	if (ret > 0) {
1754 		dso__set_loaded(map->dso, type);
1755 		/*
1756 		 * Since /proc/kallsyms will have multiple sessions for the
1757 		 * kernel, with modules between them, fixup the end of all
1758 		 * sections.
1759 		 */
1760 		__map_groups__fixup_end(&machine->kmaps, type);
1761 	}
1762 
1763 	return ret;
1764 }
1765 
1766 int machine__load_vmlinux_path(struct machine *machine, enum map_type type,
1767 			       symbol_filter_t filter)
1768 {
1769 	struct map *map = machine->vmlinux_maps[type];
1770 	int ret = dso__load_vmlinux_path(map->dso, map, filter);
1771 
1772 	if (ret > 0) {
1773 		dso__set_loaded(map->dso, type);
1774 		map__reloc_vmlinux(map);
1775 	}
1776 
1777 	return ret;
1778 }
1779