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