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