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