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