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