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> |
439d473b | 21 | #include <sys/utsname.h> |
2cdbc46d | 22 | |
3b01a413 | 23 | #ifndef KSYM_NAME_LEN |
c752d040 | 24 | #define KSYM_NAME_LEN 256 |
3b01a413 ACM |
25 | #endif |
26 | ||
aeafcbaf | 27 | static int dso__load_kernel_sym(struct dso *dso, struct map *map, |
9de89fe7 | 28 | symbol_filter_t filter); |
aeafcbaf | 29 | static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map, |
a1645ce1 | 30 | symbol_filter_t filter); |
3f067dca ACM |
31 | int vmlinux_path__nr_entries; |
32 | char **vmlinux_path; | |
439d473b | 33 | |
75be6cf4 | 34 | struct symbol_conf symbol_conf = { |
d599db3f | 35 | .exclude_other = true, |
b32d133a ACM |
36 | .use_modules = true, |
37 | .try_vmlinux_path = true, | |
3e6a2a7f | 38 | .annotate_src = true, |
328ccdac | 39 | .demangle = true, |
ec5761ea | 40 | .symfs = "", |
b32d133a ACM |
41 | }; |
42 | ||
44f24cb3 JO |
43 | static enum dso_binary_type binary_type_symtab[] = { |
44 | DSO_BINARY_TYPE__KALLSYMS, | |
45 | DSO_BINARY_TYPE__GUEST_KALLSYMS, | |
46 | DSO_BINARY_TYPE__JAVA_JIT, | |
47 | DSO_BINARY_TYPE__DEBUGLINK, | |
48 | DSO_BINARY_TYPE__BUILD_ID_CACHE, | |
49 | DSO_BINARY_TYPE__FEDORA_DEBUGINFO, | |
50 | DSO_BINARY_TYPE__UBUNTU_DEBUGINFO, | |
51 | DSO_BINARY_TYPE__BUILDID_DEBUGINFO, | |
52 | DSO_BINARY_TYPE__SYSTEM_PATH_DSO, | |
53 | DSO_BINARY_TYPE__GUEST_KMODULE, | |
54 | DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE, | |
55 | DSO_BINARY_TYPE__NOT_FOUND, | |
56 | }; | |
57 | ||
028df767 | 58 | #define DSO_BINARY_TYPE__SYMTAB_CNT ARRAY_SIZE(binary_type_symtab) |
44f24cb3 | 59 | |
36a3e646 | 60 | bool symbol_type__is_a(char symbol_type, enum map_type map_type) |
6893d4ee | 61 | { |
31877908 AB |
62 | symbol_type = toupper(symbol_type); |
63 | ||
6893d4ee ACM |
64 | switch (map_type) { |
65 | case MAP__FUNCTION: | |
66 | return symbol_type == 'T' || symbol_type == 'W'; | |
f1dfa0b1 | 67 | case MAP__VARIABLE: |
31877908 | 68 | return symbol_type == 'D'; |
6893d4ee ACM |
69 | default: |
70 | return false; | |
71 | } | |
72 | } | |
73 | ||
694bf407 AB |
74 | static int prefix_underscores_count(const char *str) |
75 | { | |
76 | const char *tail = str; | |
77 | ||
78 | while (*tail == '_') | |
79 | tail++; | |
80 | ||
81 | return tail - str; | |
82 | } | |
83 | ||
84 | #define SYMBOL_A 0 | |
85 | #define SYMBOL_B 1 | |
86 | ||
87 | static int choose_best_symbol(struct symbol *syma, struct symbol *symb) | |
88 | { | |
89 | s64 a; | |
90 | s64 b; | |
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 | ||
124 | /* If all else fails, choose the symbol with the longest name */ | |
125 | if (strlen(syma->name) >= strlen(symb->name)) | |
126 | return SYMBOL_A; | |
127 | else | |
128 | return SYMBOL_B; | |
129 | } | |
130 | ||
e5a1845f | 131 | void symbols__fixup_duplicate(struct rb_root *symbols) |
694bf407 AB |
132 | { |
133 | struct rb_node *nd; | |
134 | struct symbol *curr, *next; | |
135 | ||
136 | nd = rb_first(symbols); | |
137 | ||
138 | while (nd) { | |
139 | curr = rb_entry(nd, struct symbol, rb_node); | |
140 | again: | |
141 | nd = rb_next(&curr->rb_node); | |
142 | next = rb_entry(nd, struct symbol, rb_node); | |
143 | ||
144 | if (!nd) | |
145 | break; | |
146 | ||
147 | if (curr->start != next->start) | |
148 | continue; | |
149 | ||
150 | if (choose_best_symbol(curr, next) == SYMBOL_A) { | |
151 | rb_erase(&next->rb_node, symbols); | |
152 | goto again; | |
153 | } else { | |
154 | nd = rb_next(&curr->rb_node); | |
155 | rb_erase(&curr->rb_node, symbols); | |
156 | } | |
157 | } | |
158 | } | |
159 | ||
e5a1845f | 160 | void symbols__fixup_end(struct rb_root *symbols) |
af427bf5 | 161 | { |
aeafcbaf | 162 | struct rb_node *nd, *prevnd = rb_first(symbols); |
2e538c4a | 163 | struct symbol *curr, *prev; |
af427bf5 ACM |
164 | |
165 | if (prevnd == NULL) | |
166 | return; | |
167 | ||
2e538c4a ACM |
168 | curr = rb_entry(prevnd, struct symbol, rb_node); |
169 | ||
af427bf5 | 170 | for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) { |
2e538c4a ACM |
171 | prev = curr; |
172 | curr = rb_entry(nd, struct symbol, rb_node); | |
af427bf5 | 173 | |
3b01a413 | 174 | if (prev->end == prev->start && prev->end != curr->start) |
af427bf5 | 175 | prev->end = curr->start - 1; |
af427bf5 | 176 | } |
2e538c4a ACM |
177 | |
178 | /* Last entry */ | |
179 | if (curr->end == curr->start) | |
180 | curr->end = roundup(curr->start, 4096); | |
af427bf5 ACM |
181 | } |
182 | ||
e5a1845f | 183 | void __map_groups__fixup_end(struct map_groups *mg, enum map_type type) |
af427bf5 ACM |
184 | { |
185 | struct map *prev, *curr; | |
aeafcbaf | 186 | struct rb_node *nd, *prevnd = rb_first(&mg->maps[type]); |
af427bf5 ACM |
187 | |
188 | if (prevnd == NULL) | |
189 | return; | |
190 | ||
191 | curr = rb_entry(prevnd, struct map, rb_node); | |
af427bf5 ACM |
192 | |
193 | for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) { | |
194 | prev = curr; | |
195 | curr = rb_entry(nd, struct map, rb_node); | |
196 | prev->end = curr->start - 1; | |
2e538c4a | 197 | } |
90c83218 ACM |
198 | |
199 | /* | |
200 | * We still haven't the actual symbols, so guess the | |
201 | * last map final address. | |
202 | */ | |
9d1faba5 | 203 | curr->end = ~0ULL; |
af427bf5 ACM |
204 | } |
205 | ||
e5a1845f | 206 | struct symbol *symbol__new(u64 start, u64 len, u8 binding, const char *name) |
a2928c42 | 207 | { |
0085c954 | 208 | size_t namelen = strlen(name) + 1; |
aeafcbaf ACM |
209 | struct symbol *sym = calloc(1, (symbol_conf.priv_size + |
210 | sizeof(*sym) + namelen)); | |
211 | if (sym == NULL) | |
0b73da3f IM |
212 | return NULL; |
213 | ||
75be6cf4 | 214 | if (symbol_conf.priv_size) |
aeafcbaf | 215 | sym = ((void *)sym) + symbol_conf.priv_size; |
e4204992 | 216 | |
aeafcbaf ACM |
217 | sym->start = start; |
218 | sym->end = len ? start + len - 1 : start; | |
219 | sym->binding = binding; | |
220 | sym->namelen = namelen - 1; | |
e4204992 | 221 | |
aeafcbaf ACM |
222 | pr_debug4("%s: %s %#" PRIx64 "-%#" PRIx64 "\n", |
223 | __func__, name, start, sym->end); | |
224 | memcpy(sym->name, name, namelen); | |
a2928c42 | 225 | |
aeafcbaf | 226 | return sym; |
a2928c42 ACM |
227 | } |
228 | ||
aeafcbaf | 229 | void symbol__delete(struct symbol *sym) |
a2928c42 | 230 | { |
aeafcbaf | 231 | free(((void *)sym) - symbol_conf.priv_size); |
a2928c42 ACM |
232 | } |
233 | ||
cdd059d7 | 234 | size_t symbol__fprintf(struct symbol *sym, FILE *fp) |
a2928c42 | 235 | { |
9486aa38 | 236 | return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %c %s\n", |
aeafcbaf ACM |
237 | sym->start, sym->end, |
238 | sym->binding == STB_GLOBAL ? 'g' : | |
239 | sym->binding == STB_LOCAL ? 'l' : 'w', | |
240 | sym->name); | |
a2928c42 ACM |
241 | } |
242 | ||
a978f2ab AN |
243 | size_t symbol__fprintf_symname_offs(const struct symbol *sym, |
244 | const struct addr_location *al, FILE *fp) | |
547a92e0 | 245 | { |
a978f2ab AN |
246 | unsigned long offset; |
247 | size_t length; | |
248 | ||
249 | if (sym && sym->name) { | |
250 | length = fprintf(fp, "%s", sym->name); | |
251 | if (al) { | |
252 | offset = al->addr - sym->start; | |
253 | length += fprintf(fp, "+0x%lx", offset); | |
254 | } | |
255 | return length; | |
256 | } else | |
257 | return fprintf(fp, "[unknown]"); | |
258 | } | |
547a92e0 | 259 | |
a978f2ab AN |
260 | size_t symbol__fprintf_symname(const struct symbol *sym, FILE *fp) |
261 | { | |
262 | return symbol__fprintf_symname_offs(sym, NULL, fp); | |
547a92e0 AN |
263 | } |
264 | ||
cdd059d7 | 265 | void symbols__delete(struct rb_root *symbols) |
a2928c42 ACM |
266 | { |
267 | struct symbol *pos; | |
aeafcbaf | 268 | struct rb_node *next = rb_first(symbols); |
a2928c42 ACM |
269 | |
270 | while (next) { | |
271 | pos = rb_entry(next, struct symbol, rb_node); | |
272 | next = rb_next(&pos->rb_node); | |
aeafcbaf | 273 | rb_erase(&pos->rb_node, symbols); |
00a192b3 | 274 | symbol__delete(pos); |
a2928c42 ACM |
275 | } |
276 | } | |
277 | ||
e5a1845f | 278 | void symbols__insert(struct rb_root *symbols, struct symbol *sym) |
a2928c42 | 279 | { |
aeafcbaf | 280 | struct rb_node **p = &symbols->rb_node; |
a2928c42 | 281 | struct rb_node *parent = NULL; |
9cffa8d5 | 282 | const u64 ip = sym->start; |
a2928c42 ACM |
283 | struct symbol *s; |
284 | ||
285 | while (*p != NULL) { | |
286 | parent = *p; | |
287 | s = rb_entry(parent, struct symbol, rb_node); | |
288 | if (ip < s->start) | |
289 | p = &(*p)->rb_left; | |
290 | else | |
291 | p = &(*p)->rb_right; | |
292 | } | |
293 | rb_link_node(&sym->rb_node, parent, p); | |
aeafcbaf | 294 | rb_insert_color(&sym->rb_node, symbols); |
a2928c42 ACM |
295 | } |
296 | ||
aeafcbaf | 297 | static struct symbol *symbols__find(struct rb_root *symbols, u64 ip) |
a2928c42 ACM |
298 | { |
299 | struct rb_node *n; | |
300 | ||
aeafcbaf | 301 | if (symbols == NULL) |
a2928c42 ACM |
302 | return NULL; |
303 | ||
aeafcbaf | 304 | n = symbols->rb_node; |
a2928c42 ACM |
305 | |
306 | while (n) { | |
307 | struct symbol *s = rb_entry(n, struct symbol, rb_node); | |
308 | ||
309 | if (ip < s->start) | |
310 | n = n->rb_left; | |
311 | else if (ip > s->end) | |
312 | n = n->rb_right; | |
313 | else | |
314 | return s; | |
315 | } | |
316 | ||
317 | return NULL; | |
318 | } | |
319 | ||
79406cd7 ACM |
320 | struct symbol_name_rb_node { |
321 | struct rb_node rb_node; | |
322 | struct symbol sym; | |
323 | }; | |
324 | ||
aeafcbaf | 325 | static void symbols__insert_by_name(struct rb_root *symbols, struct symbol *sym) |
79406cd7 | 326 | { |
aeafcbaf | 327 | struct rb_node **p = &symbols->rb_node; |
79406cd7 | 328 | struct rb_node *parent = NULL; |
02a9d037 RV |
329 | struct symbol_name_rb_node *symn, *s; |
330 | ||
331 | symn = container_of(sym, struct symbol_name_rb_node, sym); | |
79406cd7 ACM |
332 | |
333 | while (*p != NULL) { | |
334 | parent = *p; | |
335 | s = rb_entry(parent, struct symbol_name_rb_node, rb_node); | |
336 | if (strcmp(sym->name, s->sym.name) < 0) | |
337 | p = &(*p)->rb_left; | |
338 | else | |
339 | p = &(*p)->rb_right; | |
340 | } | |
341 | rb_link_node(&symn->rb_node, parent, p); | |
aeafcbaf | 342 | rb_insert_color(&symn->rb_node, symbols); |
79406cd7 ACM |
343 | } |
344 | ||
aeafcbaf ACM |
345 | static void symbols__sort_by_name(struct rb_root *symbols, |
346 | struct rb_root *source) | |
79406cd7 ACM |
347 | { |
348 | struct rb_node *nd; | |
349 | ||
350 | for (nd = rb_first(source); nd; nd = rb_next(nd)) { | |
351 | struct symbol *pos = rb_entry(nd, struct symbol, rb_node); | |
aeafcbaf | 352 | symbols__insert_by_name(symbols, pos); |
79406cd7 ACM |
353 | } |
354 | } | |
355 | ||
aeafcbaf ACM |
356 | static struct symbol *symbols__find_by_name(struct rb_root *symbols, |
357 | const char *name) | |
79406cd7 ACM |
358 | { |
359 | struct rb_node *n; | |
360 | ||
aeafcbaf | 361 | if (symbols == NULL) |
79406cd7 ACM |
362 | return NULL; |
363 | ||
aeafcbaf | 364 | n = symbols->rb_node; |
79406cd7 ACM |
365 | |
366 | while (n) { | |
367 | struct symbol_name_rb_node *s; | |
368 | int cmp; | |
369 | ||
370 | s = rb_entry(n, struct symbol_name_rb_node, rb_node); | |
371 | cmp = strcmp(name, s->sym.name); | |
372 | ||
373 | if (cmp < 0) | |
374 | n = n->rb_left; | |
375 | else if (cmp > 0) | |
376 | n = n->rb_right; | |
377 | else | |
378 | return &s->sym; | |
379 | } | |
380 | ||
381 | return NULL; | |
382 | } | |
383 | ||
aeafcbaf | 384 | struct symbol *dso__find_symbol(struct dso *dso, |
79406cd7 | 385 | enum map_type type, u64 addr) |
fcf1203a | 386 | { |
aeafcbaf | 387 | return symbols__find(&dso->symbols[type], addr); |
fcf1203a ACM |
388 | } |
389 | ||
aeafcbaf | 390 | struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type, |
79406cd7 ACM |
391 | const char *name) |
392 | { | |
aeafcbaf | 393 | return symbols__find_by_name(&dso->symbol_names[type], name); |
79406cd7 ACM |
394 | } |
395 | ||
aeafcbaf | 396 | void dso__sort_by_name(struct dso *dso, enum map_type type) |
79406cd7 | 397 | { |
aeafcbaf ACM |
398 | dso__set_sorted_by_name(dso, type); |
399 | return symbols__sort_by_name(&dso->symbol_names[type], | |
400 | &dso->symbols[type]); | |
79406cd7 ACM |
401 | } |
402 | ||
aeafcbaf ACM |
403 | size_t dso__fprintf_symbols_by_name(struct dso *dso, |
404 | enum map_type type, FILE *fp) | |
90f18e63 SD |
405 | { |
406 | size_t ret = 0; | |
407 | struct rb_node *nd; | |
408 | struct symbol_name_rb_node *pos; | |
409 | ||
aeafcbaf | 410 | for (nd = rb_first(&dso->symbol_names[type]); nd; nd = rb_next(nd)) { |
90f18e63 SD |
411 | pos = rb_entry(nd, struct symbol_name_rb_node, rb_node); |
412 | fprintf(fp, "%s\n", pos->sym.name); | |
413 | } | |
414 | ||
415 | return ret; | |
416 | } | |
417 | ||
9e201442 ACM |
418 | int kallsyms__parse(const char *filename, void *arg, |
419 | int (*process_symbol)(void *arg, const char *name, | |
82151520 | 420 | char type, u64 start)) |
a2928c42 | 421 | { |
a2928c42 ACM |
422 | char *line = NULL; |
423 | size_t n; | |
3b01a413 | 424 | int err = -1; |
9e201442 | 425 | FILE *file = fopen(filename, "r"); |
a2928c42 ACM |
426 | |
427 | if (file == NULL) | |
428 | goto out_failure; | |
429 | ||
3b01a413 ACM |
430 | err = 0; |
431 | ||
a2928c42 | 432 | while (!feof(file)) { |
9cffa8d5 | 433 | u64 start; |
a2928c42 ACM |
434 | int line_len, len; |
435 | char symbol_type; | |
2e538c4a | 436 | char *symbol_name; |
a2928c42 ACM |
437 | |
438 | line_len = getline(&line, &n, file); | |
a1645ce1 | 439 | if (line_len < 0 || !line) |
a2928c42 ACM |
440 | break; |
441 | ||
a2928c42 ACM |
442 | line[--line_len] = '\0'; /* \n */ |
443 | ||
a0055ae2 | 444 | len = hex2u64(line, &start); |
a2928c42 ACM |
445 | |
446 | len++; | |
447 | if (len + 2 >= line_len) | |
448 | continue; | |
449 | ||
31877908 | 450 | symbol_type = line[len]; |
3b01a413 ACM |
451 | len += 2; |
452 | symbol_name = line + len; | |
453 | len = line_len - len; | |
682b335a | 454 | |
3b01a413 ACM |
455 | if (len >= KSYM_NAME_LEN) { |
456 | err = -1; | |
682b335a | 457 | break; |
3b01a413 ACM |
458 | } |
459 | ||
3f5a4272 | 460 | err = process_symbol(arg, symbol_name, |
82151520 | 461 | symbol_type, start); |
3f5a4272 AB |
462 | if (err) |
463 | break; | |
2e538c4a ACM |
464 | } |
465 | ||
466 | free(line); | |
467 | fclose(file); | |
682b335a | 468 | return err; |
2e538c4a | 469 | |
2e538c4a ACM |
470 | out_failure: |
471 | return -1; | |
472 | } | |
473 | ||
682b335a ACM |
474 | struct process_kallsyms_args { |
475 | struct map *map; | |
476 | struct dso *dso; | |
477 | }; | |
478 | ||
c408fedf ACM |
479 | static u8 kallsyms2elf_type(char type) |
480 | { | |
481 | if (type == 'W') | |
482 | return STB_WEAK; | |
483 | ||
484 | return isupper(type) ? STB_GLOBAL : STB_LOCAL; | |
485 | } | |
486 | ||
682b335a | 487 | static int map__process_kallsym_symbol(void *arg, const char *name, |
82151520 | 488 | char type, u64 start) |
682b335a ACM |
489 | { |
490 | struct symbol *sym; | |
491 | struct process_kallsyms_args *a = arg; | |
492 | struct rb_root *root = &a->dso->symbols[a->map->type]; | |
493 | ||
494 | if (!symbol_type__is_a(type, a->map->type)) | |
495 | return 0; | |
496 | ||
82151520 CS |
497 | /* |
498 | * module symbols are not sorted so we add all | |
499 | * symbols, setting length to 0, and rely on | |
500 | * symbols__fixup_end() to fix it up. | |
501 | */ | |
502 | sym = symbol__new(start, 0, kallsyms2elf_type(type), name); | |
682b335a ACM |
503 | if (sym == NULL) |
504 | return -ENOMEM; | |
505 | /* | |
506 | * We will pass the symbols to the filter later, in | |
507 | * map__split_kallsyms, when we have split the maps per module | |
508 | */ | |
509 | symbols__insert(root, sym); | |
a1645ce1 | 510 | |
682b335a ACM |
511 | return 0; |
512 | } | |
513 | ||
514 | /* | |
515 | * Loads the function entries in /proc/kallsyms into kernel_map->dso, | |
516 | * so that we can in the next step set the symbol ->end address and then | |
517 | * call kernel_maps__split_kallsyms. | |
518 | */ | |
aeafcbaf | 519 | static int dso__load_all_kallsyms(struct dso *dso, const char *filename, |
9e201442 | 520 | struct map *map) |
682b335a | 521 | { |
aeafcbaf | 522 | struct process_kallsyms_args args = { .map = map, .dso = dso, }; |
9e201442 | 523 | return kallsyms__parse(filename, &args, map__process_kallsym_symbol); |
682b335a ACM |
524 | } |
525 | ||
2e538c4a ACM |
526 | /* |
527 | * Split the symbols into maps, making sure there are no overlaps, i.e. the | |
528 | * kernel range is broken in several maps, named [kernel].N, as we don't have | |
529 | * the original ELF section names vmlinux have. | |
530 | */ | |
aeafcbaf | 531 | static int dso__split_kallsyms(struct dso *dso, struct map *map, |
9de89fe7 | 532 | symbol_filter_t filter) |
2e538c4a | 533 | { |
9de89fe7 | 534 | struct map_groups *kmaps = map__kmap(map)->kmaps; |
23346f21 | 535 | struct machine *machine = kmaps->machine; |
4e06255f | 536 | struct map *curr_map = map; |
2e538c4a | 537 | struct symbol *pos; |
8a953312 | 538 | int count = 0, moved = 0; |
aeafcbaf | 539 | struct rb_root *root = &dso->symbols[map->type]; |
4e06255f | 540 | struct rb_node *next = rb_first(root); |
2e538c4a ACM |
541 | int kernel_range = 0; |
542 | ||
543 | while (next) { | |
544 | char *module; | |
545 | ||
546 | pos = rb_entry(next, struct symbol, rb_node); | |
547 | next = rb_next(&pos->rb_node); | |
548 | ||
549 | module = strchr(pos->name, '\t'); | |
550 | if (module) { | |
75be6cf4 | 551 | if (!symbol_conf.use_modules) |
1de8e245 ACM |
552 | goto discard_symbol; |
553 | ||
2e538c4a ACM |
554 | *module++ = '\0'; |
555 | ||
b7cece76 | 556 | if (strcmp(curr_map->dso->short_name, module)) { |
a1645ce1 | 557 | if (curr_map != map && |
aeafcbaf | 558 | dso->kernel == DSO_TYPE_GUEST_KERNEL && |
23346f21 | 559 | machine__is_default_guest(machine)) { |
a1645ce1 ZY |
560 | /* |
561 | * We assume all symbols of a module are | |
562 | * continuous in * kallsyms, so curr_map | |
563 | * points to a module and all its | |
564 | * symbols are in its kmap. Mark it as | |
565 | * loaded. | |
566 | */ | |
567 | dso__set_loaded(curr_map->dso, | |
568 | curr_map->type); | |
569 | } | |
570 | ||
571 | curr_map = map_groups__find_by_name(kmaps, | |
572 | map->type, module); | |
4e06255f | 573 | if (curr_map == NULL) { |
2f51903b | 574 | pr_debug("%s/proc/{kallsyms,modules} " |
b7cece76 | 575 | "inconsistency while looking " |
a1645ce1 | 576 | "for \"%s\" module!\n", |
23346f21 | 577 | machine->root_dir, module); |
a1645ce1 ZY |
578 | curr_map = map; |
579 | goto discard_symbol; | |
af427bf5 | 580 | } |
b7cece76 | 581 | |
a1645ce1 | 582 | if (curr_map->dso->loaded && |
23346f21 | 583 | !machine__is_default_guest(machine)) |
b7cece76 | 584 | goto discard_symbol; |
af427bf5 | 585 | } |
2e538c4a ACM |
586 | /* |
587 | * So that we look just like we get from .ko files, | |
588 | * i.e. not prelinked, relative to map->start. | |
589 | */ | |
4e06255f ACM |
590 | pos->start = curr_map->map_ip(curr_map, pos->start); |
591 | pos->end = curr_map->map_ip(curr_map, pos->end); | |
592 | } else if (curr_map != map) { | |
2e538c4a | 593 | char dso_name[PATH_MAX]; |
aeafcbaf | 594 | struct dso *ndso; |
2e538c4a | 595 | |
8a953312 ACM |
596 | if (count == 0) { |
597 | curr_map = map; | |
598 | goto filter_symbol; | |
599 | } | |
600 | ||
aeafcbaf | 601 | if (dso->kernel == DSO_TYPE_GUEST_KERNEL) |
a1645ce1 ZY |
602 | snprintf(dso_name, sizeof(dso_name), |
603 | "[guest.kernel].%d", | |
604 | kernel_range++); | |
605 | else | |
606 | snprintf(dso_name, sizeof(dso_name), | |
607 | "[kernel].%d", | |
608 | kernel_range++); | |
2e538c4a | 609 | |
aeafcbaf ACM |
610 | ndso = dso__new(dso_name); |
611 | if (ndso == NULL) | |
2e538c4a ACM |
612 | return -1; |
613 | ||
aeafcbaf | 614 | ndso->kernel = dso->kernel; |
a1645ce1 | 615 | |
aeafcbaf | 616 | curr_map = map__new2(pos->start, ndso, map->type); |
37fe5fcb | 617 | if (curr_map == NULL) { |
aeafcbaf | 618 | dso__delete(ndso); |
2e538c4a ACM |
619 | return -1; |
620 | } | |
a2928c42 | 621 | |
4e06255f | 622 | curr_map->map_ip = curr_map->unmap_ip = identity__map_ip; |
9de89fe7 | 623 | map_groups__insert(kmaps, curr_map); |
2e538c4a ACM |
624 | ++kernel_range; |
625 | } | |
8a953312 | 626 | filter_symbol: |
4e06255f | 627 | if (filter && filter(curr_map, pos)) { |
1de8e245 | 628 | discard_symbol: rb_erase(&pos->rb_node, root); |
00a192b3 | 629 | symbol__delete(pos); |
2e538c4a | 630 | } else { |
4e06255f ACM |
631 | if (curr_map != map) { |
632 | rb_erase(&pos->rb_node, root); | |
633 | symbols__insert(&curr_map->dso->symbols[curr_map->type], pos); | |
8a953312 ACM |
634 | ++moved; |
635 | } else | |
636 | ++count; | |
9974f496 | 637 | } |
a2928c42 ACM |
638 | } |
639 | ||
a1645ce1 | 640 | if (curr_map != map && |
aeafcbaf | 641 | dso->kernel == DSO_TYPE_GUEST_KERNEL && |
23346f21 | 642 | machine__is_default_guest(kmaps->machine)) { |
a1645ce1 ZY |
643 | dso__set_loaded(curr_map->dso, curr_map->type); |
644 | } | |
645 | ||
8a953312 | 646 | return count + moved; |
2e538c4a | 647 | } |
a2928c42 | 648 | |
3f067dca ACM |
649 | bool symbol__restricted_filename(const char *filename, |
650 | const char *restricted_filename) | |
ec80fde7 ACM |
651 | { |
652 | bool restricted = false; | |
653 | ||
654 | if (symbol_conf.kptr_restrict) { | |
655 | char *r = realpath(filename, NULL); | |
656 | ||
657 | if (r != NULL) { | |
658 | restricted = strcmp(r, restricted_filename) == 0; | |
659 | free(r); | |
660 | return restricted; | |
661 | } | |
662 | } | |
663 | ||
664 | return restricted; | |
665 | } | |
666 | ||
aeafcbaf | 667 | int dso__load_kallsyms(struct dso *dso, const char *filename, |
9de89fe7 | 668 | struct map *map, symbol_filter_t filter) |
2e538c4a | 669 | { |
ec80fde7 ACM |
670 | if (symbol__restricted_filename(filename, "/proc/kallsyms")) |
671 | return -1; | |
672 | ||
aeafcbaf | 673 | if (dso__load_all_kallsyms(dso, filename, map) < 0) |
2e538c4a ACM |
674 | return -1; |
675 | ||
3f5a4272 | 676 | symbols__fixup_end(&dso->symbols[map->type]); |
d636f64a | 677 | symbols__fixup_duplicate(&dso->symbols[map->type]); |
3f5a4272 | 678 | |
aeafcbaf | 679 | if (dso->kernel == DSO_TYPE_GUEST_KERNEL) |
44f24cb3 | 680 | dso->symtab_type = DSO_BINARY_TYPE__GUEST_KALLSYMS; |
a1645ce1 | 681 | else |
44f24cb3 | 682 | dso->symtab_type = DSO_BINARY_TYPE__KALLSYMS; |
2e538c4a | 683 | |
aeafcbaf | 684 | return dso__split_kallsyms(dso, map, filter); |
af427bf5 ACM |
685 | } |
686 | ||
aeafcbaf | 687 | static int dso__load_perf_map(struct dso *dso, struct map *map, |
6beba7ad | 688 | symbol_filter_t filter) |
80d496be PE |
689 | { |
690 | char *line = NULL; | |
691 | size_t n; | |
692 | FILE *file; | |
693 | int nr_syms = 0; | |
694 | ||
aeafcbaf | 695 | file = fopen(dso->long_name, "r"); |
80d496be PE |
696 | if (file == NULL) |
697 | goto out_failure; | |
698 | ||
699 | while (!feof(file)) { | |
9cffa8d5 | 700 | u64 start, size; |
80d496be PE |
701 | struct symbol *sym; |
702 | int line_len, len; | |
703 | ||
704 | line_len = getline(&line, &n, file); | |
705 | if (line_len < 0) | |
706 | break; | |
707 | ||
708 | if (!line) | |
709 | goto out_failure; | |
710 | ||
711 | line[--line_len] = '\0'; /* \n */ | |
712 | ||
713 | len = hex2u64(line, &start); | |
714 | ||
715 | len++; | |
716 | if (len + 2 >= line_len) | |
717 | continue; | |
718 | ||
719 | len += hex2u64(line + len, &size); | |
720 | ||
721 | len++; | |
722 | if (len + 2 >= line_len) | |
723 | continue; | |
724 | ||
c408fedf | 725 | sym = symbol__new(start, size, STB_GLOBAL, line + len); |
80d496be PE |
726 | |
727 | if (sym == NULL) | |
728 | goto out_delete_line; | |
729 | ||
439d473b | 730 | if (filter && filter(map, sym)) |
00a192b3 | 731 | symbol__delete(sym); |
80d496be | 732 | else { |
aeafcbaf | 733 | symbols__insert(&dso->symbols[map->type], sym); |
80d496be PE |
734 | nr_syms++; |
735 | } | |
736 | } | |
737 | ||
738 | free(line); | |
739 | fclose(file); | |
740 | ||
741 | return nr_syms; | |
742 | ||
743 | out_delete_line: | |
744 | free(line); | |
745 | out_failure: | |
746 | return -1; | |
747 | } | |
748 | ||
aeafcbaf | 749 | int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter) |
a2928c42 | 750 | { |
c338aee8 | 751 | char *name; |
a2928c42 | 752 | int ret = -1; |
44f24cb3 | 753 | u_int i; |
23346f21 | 754 | struct machine *machine; |
44f24cb3 | 755 | char *root_dir = (char *) ""; |
3aafe5ae CS |
756 | int ss_pos = 0; |
757 | struct symsrc ss_[2]; | |
758 | struct symsrc *syms_ss = NULL, *runtime_ss = NULL; | |
a2928c42 | 759 | |
aeafcbaf | 760 | dso__set_loaded(dso, map->type); |
66bd8424 | 761 | |
aeafcbaf ACM |
762 | if (dso->kernel == DSO_TYPE_KERNEL) |
763 | return dso__load_kernel_sym(dso, map, filter); | |
764 | else if (dso->kernel == DSO_TYPE_GUEST_KERNEL) | |
765 | return dso__load_guest_kernel_sym(dso, map, filter); | |
a1645ce1 | 766 | |
23346f21 ACM |
767 | if (map->groups && map->groups->machine) |
768 | machine = map->groups->machine; | |
a1645ce1 | 769 | else |
23346f21 | 770 | machine = NULL; |
c338aee8 | 771 | |
aeafcbaf | 772 | dso->adjust_symbols = 0; |
f5812a7a | 773 | |
aeafcbaf | 774 | if (strncmp(dso->name, "/tmp/perf-", 10) == 0) { |
981c1252 PE |
775 | struct stat st; |
776 | ||
e9b52ef2 | 777 | if (lstat(dso->name, &st) < 0) |
981c1252 PE |
778 | return -1; |
779 | ||
780 | if (st.st_uid && (st.st_uid != geteuid())) { | |
781 | pr_warning("File %s not owned by current user or root, " | |
782 | "ignoring it.\n", dso->name); | |
783 | return -1; | |
784 | } | |
785 | ||
aeafcbaf | 786 | ret = dso__load_perf_map(dso, map, filter); |
44f24cb3 JO |
787 | dso->symtab_type = ret > 0 ? DSO_BINARY_TYPE__JAVA_JIT : |
788 | DSO_BINARY_TYPE__NOT_FOUND; | |
94cb9e38 ACM |
789 | return ret; |
790 | } | |
791 | ||
44f24cb3 JO |
792 | if (machine) |
793 | root_dir = machine->root_dir; | |
794 | ||
164c800e DA |
795 | name = malloc(PATH_MAX); |
796 | if (!name) | |
797 | return -1; | |
798 | ||
6da80ce8 | 799 | /* Iterate over candidate debug images. |
3aafe5ae CS |
800 | * Keep track of "interesting" ones (those which have a symtab, dynsym, |
801 | * and/or opd section) for processing. | |
6da80ce8 | 802 | */ |
44f24cb3 | 803 | for (i = 0; i < DSO_BINARY_TYPE__SYMTAB_CNT; i++) { |
3aafe5ae CS |
804 | struct symsrc *ss = &ss_[ss_pos]; |
805 | bool next_slot = false; | |
6da80ce8 | 806 | |
005f9294 | 807 | enum dso_binary_type symtab_type = binary_type_symtab[i]; |
ec5761ea | 808 | |
005f9294 | 809 | if (dso__binary_type_file(dso, symtab_type, |
44f24cb3 JO |
810 | root_dir, name, PATH_MAX)) |
811 | continue; | |
6da80ce8 DM |
812 | |
813 | /* Name is now the name of the next image to try */ | |
3aafe5ae | 814 | if (symsrc__init(ss, dso, name, symtab_type) < 0) |
6da80ce8 | 815 | continue; |
a2928c42 | 816 | |
3aafe5ae CS |
817 | if (!syms_ss && symsrc__has_symtab(ss)) { |
818 | syms_ss = ss; | |
819 | next_slot = true; | |
d26cd12b CS |
820 | } |
821 | ||
3aafe5ae CS |
822 | if (!runtime_ss && symsrc__possibly_runtime(ss)) { |
823 | runtime_ss = ss; | |
824 | next_slot = true; | |
a44f605b | 825 | } |
a2928c42 | 826 | |
3aafe5ae CS |
827 | if (next_slot) { |
828 | ss_pos++; | |
33ff581e | 829 | |
3aafe5ae CS |
830 | if (syms_ss && runtime_ss) |
831 | break; | |
6da80ce8 | 832 | } |
3aafe5ae | 833 | |
a25e46c4 | 834 | } |
6da80ce8 | 835 | |
3aafe5ae CS |
836 | if (!runtime_ss && !syms_ss) |
837 | goto out_free; | |
838 | ||
839 | if (runtime_ss && !syms_ss) { | |
840 | syms_ss = runtime_ss; | |
841 | } | |
842 | ||
843 | /* We'll have to hope for the best */ | |
844 | if (!runtime_ss && syms_ss) | |
845 | runtime_ss = syms_ss; | |
846 | ||
847 | if (syms_ss) | |
848 | ret = dso__load_sym(dso, map, syms_ss, runtime_ss, filter, 0); | |
849 | else | |
850 | ret = -1; | |
851 | ||
f47b58b7 | 852 | if (ret > 0) { |
3aafe5ae CS |
853 | int nr_plt; |
854 | ||
855 | nr_plt = dso__synthesize_plt_symbols(dso, runtime_ss, map, filter); | |
856 | if (nr_plt > 0) | |
857 | ret += nr_plt; | |
60e4b10c ACM |
858 | } |
859 | ||
3aafe5ae CS |
860 | for (; ss_pos > 0; ss_pos--) |
861 | symsrc__destroy(&ss_[ss_pos - 1]); | |
862 | out_free: | |
a2928c42 | 863 | free(name); |
aeafcbaf | 864 | if (ret < 0 && strstr(dso->name, " (deleted)") != NULL) |
1340e6bb | 865 | return 0; |
a2928c42 ACM |
866 | return ret; |
867 | } | |
868 | ||
aeafcbaf | 869 | struct map *map_groups__find_by_name(struct map_groups *mg, |
79406cd7 | 870 | enum map_type type, const char *name) |
439d473b ACM |
871 | { |
872 | struct rb_node *nd; | |
873 | ||
aeafcbaf | 874 | for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) { |
439d473b ACM |
875 | struct map *map = rb_entry(nd, struct map, rb_node); |
876 | ||
b7cece76 | 877 | if (map->dso && strcmp(map->dso->short_name, name) == 0) |
439d473b ACM |
878 | return map; |
879 | } | |
880 | ||
881 | return NULL; | |
882 | } | |
883 | ||
aeafcbaf | 884 | int dso__load_vmlinux(struct dso *dso, struct map *map, |
fd930ff9 | 885 | const char *vmlinux, symbol_filter_t filter) |
a2928c42 | 886 | { |
b68e2f91 CS |
887 | int err = -1; |
888 | struct symsrc ss; | |
ec5761ea | 889 | char symfs_vmlinux[PATH_MAX]; |
005f9294 | 890 | enum dso_binary_type symtab_type; |
a2928c42 | 891 | |
a639dc64 | 892 | snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s%s", |
ec5761ea | 893 | symbol_conf.symfs, vmlinux); |
a2928c42 | 894 | |
21ea4539 | 895 | if (dso->kernel == DSO_TYPE_GUEST_KERNEL) |
005f9294 | 896 | symtab_type = DSO_BINARY_TYPE__GUEST_VMLINUX; |
21ea4539 | 897 | else |
005f9294 | 898 | symtab_type = DSO_BINARY_TYPE__VMLINUX; |
21ea4539 | 899 | |
005f9294 | 900 | if (symsrc__init(&ss, dso, symfs_vmlinux, symtab_type)) |
b68e2f91 CS |
901 | return -1; |
902 | ||
261360b6 | 903 | err = dso__load_sym(dso, map, &ss, &ss, filter, 0); |
b68e2f91 | 904 | symsrc__destroy(&ss); |
a2928c42 | 905 | |
515850e4 CS |
906 | if (err > 0) { |
907 | dso__set_long_name(dso, (char *)vmlinux); | |
908 | dso__set_loaded(dso, map->type); | |
ec5761ea | 909 | pr_debug("Using %s for symbols\n", symfs_vmlinux); |
515850e4 | 910 | } |
3846df2e | 911 | |
a2928c42 ACM |
912 | return err; |
913 | } | |
914 | ||
aeafcbaf | 915 | int dso__load_vmlinux_path(struct dso *dso, struct map *map, |
9de89fe7 | 916 | symbol_filter_t filter) |
a19afe46 ACM |
917 | { |
918 | int i, err = 0; | |
5ad90e4e | 919 | char *filename; |
a19afe46 ACM |
920 | |
921 | pr_debug("Looking at the vmlinux_path (%d entries long)\n", | |
5ad90e4e ACM |
922 | vmlinux_path__nr_entries + 1); |
923 | ||
aeafcbaf | 924 | filename = dso__build_id_filename(dso, NULL, 0); |
5ad90e4e | 925 | if (filename != NULL) { |
aeafcbaf | 926 | err = dso__load_vmlinux(dso, map, filename, filter); |
b7c14a0b DA |
927 | if (err > 0) { |
928 | dso->lname_alloc = 1; | |
5ad90e4e | 929 | goto out; |
b7c14a0b | 930 | } |
5ad90e4e ACM |
931 | free(filename); |
932 | } | |
a19afe46 ACM |
933 | |
934 | for (i = 0; i < vmlinux_path__nr_entries; ++i) { | |
aeafcbaf | 935 | err = dso__load_vmlinux(dso, map, vmlinux_path[i], filter); |
a19afe46 | 936 | if (err > 0) { |
aeafcbaf | 937 | dso__set_long_name(dso, strdup(vmlinux_path[i])); |
b7c14a0b | 938 | dso->lname_alloc = 1; |
a19afe46 ACM |
939 | break; |
940 | } | |
941 | } | |
5ad90e4e | 942 | out: |
a19afe46 ACM |
943 | return err; |
944 | } | |
945 | ||
aeafcbaf | 946 | static int dso__load_kernel_sym(struct dso *dso, struct map *map, |
9de89fe7 | 947 | symbol_filter_t filter) |
a827c875 | 948 | { |
cc612d81 | 949 | int err; |
9e201442 ACM |
950 | const char *kallsyms_filename = NULL; |
951 | char *kallsyms_allocated_filename = NULL; | |
dc8d6ab2 | 952 | /* |
b226a5a7 DA |
953 | * Step 1: if the user specified a kallsyms or vmlinux filename, use |
954 | * it and only it, reporting errors to the user if it cannot be used. | |
dc8d6ab2 ACM |
955 | * |
956 | * For instance, try to analyse an ARM perf.data file _without_ a | |
957 | * build-id, or if the user specifies the wrong path to the right | |
958 | * vmlinux file, obviously we can't fallback to another vmlinux (a | |
959 | * x86_86 one, on the machine where analysis is being performed, say), | |
960 | * or worse, /proc/kallsyms. | |
961 | * | |
962 | * If the specified file _has_ a build-id and there is a build-id | |
963 | * section in the perf.data file, we will still do the expected | |
964 | * validation in dso__load_vmlinux and will bail out if they don't | |
965 | * match. | |
966 | */ | |
b226a5a7 DA |
967 | if (symbol_conf.kallsyms_name != NULL) { |
968 | kallsyms_filename = symbol_conf.kallsyms_name; | |
969 | goto do_kallsyms; | |
970 | } | |
971 | ||
dc8d6ab2 | 972 | if (symbol_conf.vmlinux_name != NULL) { |
aeafcbaf | 973 | err = dso__load_vmlinux(dso, map, |
dc8d6ab2 | 974 | symbol_conf.vmlinux_name, filter); |
e7dadc00 | 975 | if (err > 0) { |
aeafcbaf | 976 | dso__set_long_name(dso, |
e7dadc00 | 977 | strdup(symbol_conf.vmlinux_name)); |
b7c14a0b | 978 | dso->lname_alloc = 1; |
e7dadc00 ACM |
979 | goto out_fixup; |
980 | } | |
981 | return err; | |
dc8d6ab2 | 982 | } |
cc612d81 ACM |
983 | |
984 | if (vmlinux_path != NULL) { | |
aeafcbaf | 985 | err = dso__load_vmlinux_path(dso, map, filter); |
a19afe46 ACM |
986 | if (err > 0) |
987 | goto out_fixup; | |
cc612d81 ACM |
988 | } |
989 | ||
ec5761ea DA |
990 | /* do not try local files if a symfs was given */ |
991 | if (symbol_conf.symfs[0] != 0) | |
992 | return -1; | |
993 | ||
b7cece76 ACM |
994 | /* |
995 | * Say the kernel DSO was created when processing the build-id header table, | |
996 | * we have a build-id, so check if it is the same as the running kernel, | |
997 | * using it if it is. | |
998 | */ | |
aeafcbaf | 999 | if (dso->has_build_id) { |
b7cece76 | 1000 | u8 kallsyms_build_id[BUILD_ID_SIZE]; |
9e201442 | 1001 | char sbuild_id[BUILD_ID_SIZE * 2 + 1]; |
b7cece76 ACM |
1002 | |
1003 | if (sysfs__read_build_id("/sys/kernel/notes", kallsyms_build_id, | |
8d0591f6 | 1004 | sizeof(kallsyms_build_id)) == 0) { |
aeafcbaf | 1005 | if (dso__build_id_equal(dso, kallsyms_build_id)) { |
9e201442 | 1006 | kallsyms_filename = "/proc/kallsyms"; |
8d0591f6 | 1007 | goto do_kallsyms; |
9e201442 | 1008 | } |
8d0591f6 | 1009 | } |
dc8d6ab2 ACM |
1010 | /* |
1011 | * Now look if we have it on the build-id cache in | |
1012 | * $HOME/.debug/[kernel.kallsyms]. | |
1013 | */ | |
aeafcbaf | 1014 | build_id__sprintf(dso->build_id, sizeof(dso->build_id), |
9e201442 ACM |
1015 | sbuild_id); |
1016 | ||
1017 | if (asprintf(&kallsyms_allocated_filename, | |
1018 | "%s/.debug/[kernel.kallsyms]/%s", | |
3846df2e ACM |
1019 | getenv("HOME"), sbuild_id) == -1) { |
1020 | pr_err("Not enough memory for kallsyms file lookup\n"); | |
dc8d6ab2 | 1021 | return -1; |
3846df2e | 1022 | } |
dc8d6ab2 | 1023 | |
19fc2ded ACM |
1024 | kallsyms_filename = kallsyms_allocated_filename; |
1025 | ||
dc8d6ab2 | 1026 | if (access(kallsyms_filename, F_OK)) { |
3846df2e ACM |
1027 | pr_err("No kallsyms or vmlinux with build-id %s " |
1028 | "was found\n", sbuild_id); | |
9e201442 | 1029 | free(kallsyms_allocated_filename); |
dc8d6ab2 | 1030 | return -1; |
9e201442 | 1031 | } |
dc8d6ab2 ACM |
1032 | } else { |
1033 | /* | |
1034 | * Last resort, if we don't have a build-id and couldn't find | |
1035 | * any vmlinux file, try the running kernel kallsyms table. | |
1036 | */ | |
9e201442 | 1037 | kallsyms_filename = "/proc/kallsyms"; |
9e201442 | 1038 | } |
439d473b | 1039 | |
cc612d81 | 1040 | do_kallsyms: |
aeafcbaf | 1041 | err = dso__load_kallsyms(dso, kallsyms_filename, map, filter); |
3846df2e ACM |
1042 | if (err > 0) |
1043 | pr_debug("Using %s for symbols\n", kallsyms_filename); | |
dc8d6ab2 | 1044 | free(kallsyms_allocated_filename); |
439d473b ACM |
1045 | |
1046 | if (err > 0) { | |
0a0317b4 | 1047 | dso__set_long_name(dso, strdup("[kernel.kallsyms]")); |
cc612d81 | 1048 | out_fixup: |
6a4694a4 ACM |
1049 | map__fixup_start(map); |
1050 | map__fixup_end(map); | |
439d473b | 1051 | } |
94cb9e38 | 1052 | |
a827c875 ACM |
1053 | return err; |
1054 | } | |
1055 | ||
aeafcbaf ACM |
1056 | static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map, |
1057 | symbol_filter_t filter) | |
a1645ce1 ZY |
1058 | { |
1059 | int err; | |
1060 | const char *kallsyms_filename = NULL; | |
23346f21 | 1061 | struct machine *machine; |
a1645ce1 ZY |
1062 | char path[PATH_MAX]; |
1063 | ||
1064 | if (!map->groups) { | |
1065 | pr_debug("Guest kernel map hasn't the point to groups\n"); | |
1066 | return -1; | |
1067 | } | |
23346f21 | 1068 | machine = map->groups->machine; |
a1645ce1 | 1069 | |
23346f21 | 1070 | if (machine__is_default_guest(machine)) { |
a1645ce1 ZY |
1071 | /* |
1072 | * if the user specified a vmlinux filename, use it and only | |
1073 | * it, reporting errors to the user if it cannot be used. | |
1074 | * Or use file guest_kallsyms inputted by user on commandline | |
1075 | */ | |
1076 | if (symbol_conf.default_guest_vmlinux_name != NULL) { | |
aeafcbaf | 1077 | err = dso__load_vmlinux(dso, map, |
a1645ce1 ZY |
1078 | symbol_conf.default_guest_vmlinux_name, filter); |
1079 | goto out_try_fixup; | |
1080 | } | |
1081 | ||
1082 | kallsyms_filename = symbol_conf.default_guest_kallsyms; | |
1083 | if (!kallsyms_filename) | |
1084 | return -1; | |
1085 | } else { | |
23346f21 | 1086 | sprintf(path, "%s/proc/kallsyms", machine->root_dir); |
a1645ce1 ZY |
1087 | kallsyms_filename = path; |
1088 | } | |
1089 | ||
aeafcbaf | 1090 | err = dso__load_kallsyms(dso, kallsyms_filename, map, filter); |
a1645ce1 ZY |
1091 | if (err > 0) |
1092 | pr_debug("Using %s for symbols\n", kallsyms_filename); | |
1093 | ||
1094 | out_try_fixup: | |
1095 | if (err > 0) { | |
1096 | if (kallsyms_filename != NULL) { | |
48ea8f54 | 1097 | machine__mmap_name(machine, path, sizeof(path)); |
aeafcbaf | 1098 | dso__set_long_name(dso, strdup(path)); |
a1645ce1 ZY |
1099 | } |
1100 | map__fixup_start(map); | |
1101 | map__fixup_end(map); | |
1102 | } | |
1103 | ||
1104 | return err; | |
1105 | } | |
cd84c2ac | 1106 | |
cc612d81 ACM |
1107 | static void vmlinux_path__exit(void) |
1108 | { | |
1109 | while (--vmlinux_path__nr_entries >= 0) { | |
1110 | free(vmlinux_path[vmlinux_path__nr_entries]); | |
1111 | vmlinux_path[vmlinux_path__nr_entries] = NULL; | |
1112 | } | |
1113 | ||
1114 | free(vmlinux_path); | |
1115 | vmlinux_path = NULL; | |
1116 | } | |
1117 | ||
1118 | static int vmlinux_path__init(void) | |
1119 | { | |
1120 | struct utsname uts; | |
1121 | char bf[PATH_MAX]; | |
1122 | ||
cc612d81 ACM |
1123 | vmlinux_path = malloc(sizeof(char *) * 5); |
1124 | if (vmlinux_path == NULL) | |
1125 | return -1; | |
1126 | ||
1127 | vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux"); | |
1128 | if (vmlinux_path[vmlinux_path__nr_entries] == NULL) | |
1129 | goto out_fail; | |
1130 | ++vmlinux_path__nr_entries; | |
1131 | vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux"); | |
1132 | if (vmlinux_path[vmlinux_path__nr_entries] == NULL) | |
1133 | goto out_fail; | |
1134 | ++vmlinux_path__nr_entries; | |
ec5761ea DA |
1135 | |
1136 | /* only try running kernel version if no symfs was given */ | |
1137 | if (symbol_conf.symfs[0] != 0) | |
1138 | return 0; | |
1139 | ||
1140 | if (uname(&uts) < 0) | |
1141 | return -1; | |
1142 | ||
cc612d81 ACM |
1143 | snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release); |
1144 | vmlinux_path[vmlinux_path__nr_entries] = strdup(bf); | |
1145 | if (vmlinux_path[vmlinux_path__nr_entries] == NULL) | |
1146 | goto out_fail; | |
1147 | ++vmlinux_path__nr_entries; | |
1148 | snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release); | |
1149 | vmlinux_path[vmlinux_path__nr_entries] = strdup(bf); | |
1150 | if (vmlinux_path[vmlinux_path__nr_entries] == NULL) | |
1151 | goto out_fail; | |
1152 | ++vmlinux_path__nr_entries; | |
1153 | snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux", | |
1154 | uts.release); | |
1155 | vmlinux_path[vmlinux_path__nr_entries] = strdup(bf); | |
1156 | if (vmlinux_path[vmlinux_path__nr_entries] == NULL) | |
1157 | goto out_fail; | |
1158 | ++vmlinux_path__nr_entries; | |
1159 | ||
1160 | return 0; | |
1161 | ||
1162 | out_fail: | |
1163 | vmlinux_path__exit(); | |
1164 | return -1; | |
1165 | } | |
1166 | ||
655000e7 ACM |
1167 | static int setup_list(struct strlist **list, const char *list_str, |
1168 | const char *list_name) | |
1169 | { | |
1170 | if (list_str == NULL) | |
1171 | return 0; | |
1172 | ||
1173 | *list = strlist__new(true, list_str); | |
1174 | if (!*list) { | |
1175 | pr_err("problems parsing %s list\n", list_name); | |
1176 | return -1; | |
1177 | } | |
1178 | return 0; | |
1179 | } | |
1180 | ||
ec80fde7 ACM |
1181 | static bool symbol__read_kptr_restrict(void) |
1182 | { | |
1183 | bool value = false; | |
1184 | ||
1185 | if (geteuid() != 0) { | |
1186 | FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r"); | |
1187 | if (fp != NULL) { | |
1188 | char line[8]; | |
1189 | ||
1190 | if (fgets(line, sizeof(line), fp) != NULL) | |
1191 | value = atoi(line) != 0; | |
1192 | ||
1193 | fclose(fp); | |
1194 | } | |
1195 | } | |
1196 | ||
1197 | return value; | |
1198 | } | |
1199 | ||
75be6cf4 | 1200 | int symbol__init(void) |
2446042c | 1201 | { |
ec5761ea DA |
1202 | const char *symfs; |
1203 | ||
85e00b55 JZ |
1204 | if (symbol_conf.initialized) |
1205 | return 0; | |
1206 | ||
9ac3e487 | 1207 | symbol_conf.priv_size = PERF_ALIGN(symbol_conf.priv_size, sizeof(u64)); |
4d439517 | 1208 | |
166ccc9c NK |
1209 | symbol__elf_init(); |
1210 | ||
75be6cf4 ACM |
1211 | if (symbol_conf.sort_by_name) |
1212 | symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) - | |
1213 | sizeof(struct symbol)); | |
b32d133a | 1214 | |
75be6cf4 | 1215 | if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0) |
2446042c ACM |
1216 | return -1; |
1217 | ||
c410a338 ACM |
1218 | if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') { |
1219 | pr_err("'.' is the only non valid --field-separator argument\n"); | |
1220 | return -1; | |
1221 | } | |
1222 | ||
655000e7 ACM |
1223 | if (setup_list(&symbol_conf.dso_list, |
1224 | symbol_conf.dso_list_str, "dso") < 0) | |
1225 | return -1; | |
1226 | ||
1227 | if (setup_list(&symbol_conf.comm_list, | |
1228 | symbol_conf.comm_list_str, "comm") < 0) | |
1229 | goto out_free_dso_list; | |
1230 | ||
1231 | if (setup_list(&symbol_conf.sym_list, | |
1232 | symbol_conf.sym_list_str, "symbol") < 0) | |
1233 | goto out_free_comm_list; | |
1234 | ||
ec5761ea DA |
1235 | /* |
1236 | * A path to symbols of "/" is identical to "" | |
1237 | * reset here for simplicity. | |
1238 | */ | |
1239 | symfs = realpath(symbol_conf.symfs, NULL); | |
1240 | if (symfs == NULL) | |
1241 | symfs = symbol_conf.symfs; | |
1242 | if (strcmp(symfs, "/") == 0) | |
1243 | symbol_conf.symfs = ""; | |
1244 | if (symfs != symbol_conf.symfs) | |
1245 | free((void *)symfs); | |
1246 | ||
ec80fde7 ACM |
1247 | symbol_conf.kptr_restrict = symbol__read_kptr_restrict(); |
1248 | ||
85e00b55 | 1249 | symbol_conf.initialized = true; |
4aa65636 | 1250 | return 0; |
655000e7 | 1251 | |
655000e7 ACM |
1252 | out_free_comm_list: |
1253 | strlist__delete(symbol_conf.comm_list); | |
d74c896b NK |
1254 | out_free_dso_list: |
1255 | strlist__delete(symbol_conf.dso_list); | |
655000e7 | 1256 | return -1; |
4aa65636 ACM |
1257 | } |
1258 | ||
d65a458b ACM |
1259 | void symbol__exit(void) |
1260 | { | |
85e00b55 JZ |
1261 | if (!symbol_conf.initialized) |
1262 | return; | |
d65a458b ACM |
1263 | strlist__delete(symbol_conf.sym_list); |
1264 | strlist__delete(symbol_conf.dso_list); | |
1265 | strlist__delete(symbol_conf.comm_list); | |
1266 | vmlinux_path__exit(); | |
1267 | symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL; | |
85e00b55 | 1268 | symbol_conf.initialized = false; |
d65a458b | 1269 | } |