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