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