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