Commit | Line | Data |
---|---|---|
a2928c42 ACM |
1 | #include "util.h" |
2 | #include "../perf.h" | |
655000e7 | 3 | #include "sort.h" |
a0055ae2 | 4 | #include "string.h" |
a2928c42 | 5 | #include "symbol.h" |
439d473b | 6 | #include "thread.h" |
a2928c42 | 7 | |
8f28827a FW |
8 | #include "debug.h" |
9 | ||
b32d133a | 10 | #include <asm/bug.h> |
a2928c42 ACM |
11 | #include <libelf.h> |
12 | #include <gelf.h> | |
13 | #include <elf.h> | |
f1617b40 | 14 | #include <limits.h> |
439d473b | 15 | #include <sys/utsname.h> |
2cdbc46d | 16 | |
c12e15e7 ACM |
17 | #ifndef NT_GNU_BUILD_ID |
18 | #define NT_GNU_BUILD_ID 3 | |
19 | #endif | |
20 | ||
b0da954a | 21 | static void dsos__add(struct list_head *head, struct dso *dso); |
3610583c | 22 | static struct map *map__new2(u64 start, struct dso *dso, enum map_type type); |
c338aee8 | 23 | static int dso__load_kernel_sym(struct dso *self, struct map *map, |
9de89fe7 | 24 | symbol_filter_t filter); |
cc612d81 ACM |
25 | static int vmlinux_path__nr_entries; |
26 | static char **vmlinux_path; | |
439d473b | 27 | |
75be6cf4 | 28 | struct symbol_conf symbol_conf = { |
d599db3f | 29 | .exclude_other = true, |
b32d133a ACM |
30 | .use_modules = true, |
31 | .try_vmlinux_path = true, | |
32 | }; | |
33 | ||
3610583c ACM |
34 | bool dso__loaded(const struct dso *self, enum map_type type) |
35 | { | |
36 | return self->loaded & (1 << type); | |
37 | } | |
38 | ||
79406cd7 ACM |
39 | bool dso__sorted_by_name(const struct dso *self, enum map_type type) |
40 | { | |
41 | return self->sorted_by_name & (1 << type); | |
42 | } | |
43 | ||
79406cd7 ACM |
44 | static void dso__set_sorted_by_name(struct dso *self, enum map_type type) |
45 | { | |
46 | self->sorted_by_name |= (1 << type); | |
47 | } | |
48 | ||
36a3e646 | 49 | bool symbol_type__is_a(char symbol_type, enum map_type map_type) |
6893d4ee ACM |
50 | { |
51 | switch (map_type) { | |
52 | case MAP__FUNCTION: | |
53 | return symbol_type == 'T' || symbol_type == 'W'; | |
f1dfa0b1 ACM |
54 | case MAP__VARIABLE: |
55 | return symbol_type == 'D' || symbol_type == 'd'; | |
6893d4ee ACM |
56 | default: |
57 | return false; | |
58 | } | |
59 | } | |
60 | ||
fcf1203a | 61 | static void symbols__fixup_end(struct rb_root *self) |
af427bf5 | 62 | { |
fcf1203a | 63 | struct rb_node *nd, *prevnd = rb_first(self); |
2e538c4a | 64 | struct symbol *curr, *prev; |
af427bf5 ACM |
65 | |
66 | if (prevnd == NULL) | |
67 | return; | |
68 | ||
2e538c4a ACM |
69 | curr = rb_entry(prevnd, struct symbol, rb_node); |
70 | ||
af427bf5 | 71 | for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) { |
2e538c4a ACM |
72 | prev = curr; |
73 | curr = rb_entry(nd, struct symbol, rb_node); | |
af427bf5 ACM |
74 | |
75 | if (prev->end == prev->start) | |
76 | prev->end = curr->start - 1; | |
af427bf5 | 77 | } |
2e538c4a ACM |
78 | |
79 | /* Last entry */ | |
80 | if (curr->end == curr->start) | |
81 | curr->end = roundup(curr->start, 4096); | |
af427bf5 ACM |
82 | } |
83 | ||
9958e1f0 | 84 | static void __map_groups__fixup_end(struct map_groups *self, enum map_type type) |
af427bf5 ACM |
85 | { |
86 | struct map *prev, *curr; | |
95011c60 | 87 | struct rb_node *nd, *prevnd = rb_first(&self->maps[type]); |
af427bf5 ACM |
88 | |
89 | if (prevnd == NULL) | |
90 | return; | |
91 | ||
92 | curr = rb_entry(prevnd, struct map, rb_node); | |
af427bf5 ACM |
93 | |
94 | for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) { | |
95 | prev = curr; | |
96 | curr = rb_entry(nd, struct map, rb_node); | |
97 | prev->end = curr->start - 1; | |
2e538c4a | 98 | } |
90c83218 ACM |
99 | |
100 | /* | |
101 | * We still haven't the actual symbols, so guess the | |
102 | * last map final address. | |
103 | */ | |
104 | curr->end = ~0UL; | |
af427bf5 ACM |
105 | } |
106 | ||
9958e1f0 | 107 | static void map_groups__fixup_end(struct map_groups *self) |
23ea4a3f ACM |
108 | { |
109 | int i; | |
110 | for (i = 0; i < MAP__NR_TYPES; ++i) | |
9958e1f0 | 111 | __map_groups__fixup_end(self, i); |
23ea4a3f ACM |
112 | } |
113 | ||
00a192b3 | 114 | static struct symbol *symbol__new(u64 start, u64 len, const char *name) |
a2928c42 | 115 | { |
0085c954 | 116 | size_t namelen = strlen(name) + 1; |
75be6cf4 | 117 | struct symbol *self = zalloc(symbol_conf.priv_size + |
36479484 ACM |
118 | sizeof(*self) + namelen); |
119 | if (self == NULL) | |
0b73da3f IM |
120 | return NULL; |
121 | ||
75be6cf4 ACM |
122 | if (symbol_conf.priv_size) |
123 | self = ((void *)self) + symbol_conf.priv_size; | |
36479484 | 124 | |
0b73da3f | 125 | self->start = start; |
6cfcc53e | 126 | self->end = len ? start + len - 1 : start; |
e4204992 | 127 | |
29a9f66d | 128 | pr_debug4("%s: %s %#Lx-%#Lx\n", __func__, name, start, self->end); |
e4204992 | 129 | |
0b73da3f | 130 | memcpy(self->name, name, namelen); |
a2928c42 ACM |
131 | |
132 | return self; | |
133 | } | |
134 | ||
628ada0c | 135 | void symbol__delete(struct symbol *self) |
a2928c42 | 136 | { |
75be6cf4 | 137 | free(((void *)self) - symbol_conf.priv_size); |
a2928c42 ACM |
138 | } |
139 | ||
140 | static size_t symbol__fprintf(struct symbol *self, FILE *fp) | |
141 | { | |
439d473b | 142 | return fprintf(fp, " %llx-%llx %s\n", |
a2928c42 ACM |
143 | self->start, self->end, self->name); |
144 | } | |
145 | ||
b7cece76 | 146 | void dso__set_long_name(struct dso *self, char *name) |
cfc10d3b | 147 | { |
ef6ae724 ACM |
148 | if (name == NULL) |
149 | return; | |
cfc10d3b ACM |
150 | self->long_name = name; |
151 | self->long_name_len = strlen(name); | |
152 | } | |
153 | ||
b63be8d7 ACM |
154 | static void dso__set_short_name(struct dso *self, const char *name) |
155 | { | |
156 | if (name == NULL) | |
157 | return; | |
158 | self->short_name = name; | |
159 | self->short_name_len = strlen(name); | |
160 | } | |
161 | ||
cfc10d3b ACM |
162 | static void dso__set_basename(struct dso *self) |
163 | { | |
b63be8d7 | 164 | dso__set_short_name(self, basename(self->long_name)); |
cfc10d3b ACM |
165 | } |
166 | ||
00a192b3 | 167 | struct dso *dso__new(const char *name) |
a2928c42 | 168 | { |
b7cece76 | 169 | struct dso *self = zalloc(sizeof(*self) + strlen(name) + 1); |
a2928c42 ACM |
170 | |
171 | if (self != NULL) { | |
6a4694a4 | 172 | int i; |
a2928c42 | 173 | strcpy(self->name, name); |
cfc10d3b | 174 | dso__set_long_name(self, self->name); |
b63be8d7 | 175 | dso__set_short_name(self, self->name); |
6a4694a4 | 176 | for (i = 0; i < MAP__NR_TYPES; ++i) |
79406cd7 | 177 | self->symbols[i] = self->symbol_names[i] = RB_ROOT; |
52d422de | 178 | self->slen_calculated = 0; |
94cb9e38 | 179 | self->origin = DSO__ORIG_NOT_FOUND; |
8d06367f | 180 | self->loaded = 0; |
79406cd7 | 181 | self->sorted_by_name = 0; |
8d06367f | 182 | self->has_build_id = 0; |
a2928c42 ACM |
183 | } |
184 | ||
185 | return self; | |
186 | } | |
187 | ||
fcf1203a | 188 | static void symbols__delete(struct rb_root *self) |
a2928c42 ACM |
189 | { |
190 | struct symbol *pos; | |
fcf1203a | 191 | struct rb_node *next = rb_first(self); |
a2928c42 ACM |
192 | |
193 | while (next) { | |
194 | pos = rb_entry(next, struct symbol, rb_node); | |
195 | next = rb_next(&pos->rb_node); | |
fcf1203a | 196 | rb_erase(&pos->rb_node, self); |
00a192b3 | 197 | symbol__delete(pos); |
a2928c42 ACM |
198 | } |
199 | } | |
200 | ||
201 | void dso__delete(struct dso *self) | |
202 | { | |
6a4694a4 ACM |
203 | int i; |
204 | for (i = 0; i < MAP__NR_TYPES; ++i) | |
205 | symbols__delete(&self->symbols[i]); | |
439d473b ACM |
206 | if (self->long_name != self->name) |
207 | free(self->long_name); | |
a2928c42 ACM |
208 | free(self); |
209 | } | |
210 | ||
8d06367f ACM |
211 | void dso__set_build_id(struct dso *self, void *build_id) |
212 | { | |
213 | memcpy(self->build_id, build_id, sizeof(self->build_id)); | |
214 | self->has_build_id = 1; | |
215 | } | |
216 | ||
fcf1203a | 217 | static void symbols__insert(struct rb_root *self, struct symbol *sym) |
a2928c42 | 218 | { |
fcf1203a | 219 | struct rb_node **p = &self->rb_node; |
a2928c42 | 220 | struct rb_node *parent = NULL; |
9cffa8d5 | 221 | const u64 ip = sym->start; |
a2928c42 ACM |
222 | struct symbol *s; |
223 | ||
224 | while (*p != NULL) { | |
225 | parent = *p; | |
226 | s = rb_entry(parent, struct symbol, rb_node); | |
227 | if (ip < s->start) | |
228 | p = &(*p)->rb_left; | |
229 | else | |
230 | p = &(*p)->rb_right; | |
231 | } | |
232 | rb_link_node(&sym->rb_node, parent, p); | |
fcf1203a | 233 | rb_insert_color(&sym->rb_node, self); |
a2928c42 ACM |
234 | } |
235 | ||
fcf1203a | 236 | static struct symbol *symbols__find(struct rb_root *self, u64 ip) |
a2928c42 ACM |
237 | { |
238 | struct rb_node *n; | |
239 | ||
240 | if (self == NULL) | |
241 | return NULL; | |
242 | ||
fcf1203a | 243 | n = self->rb_node; |
a2928c42 ACM |
244 | |
245 | while (n) { | |
246 | struct symbol *s = rb_entry(n, struct symbol, rb_node); | |
247 | ||
248 | if (ip < s->start) | |
249 | n = n->rb_left; | |
250 | else if (ip > s->end) | |
251 | n = n->rb_right; | |
252 | else | |
253 | return s; | |
254 | } | |
255 | ||
256 | return NULL; | |
257 | } | |
258 | ||
79406cd7 ACM |
259 | struct symbol_name_rb_node { |
260 | struct rb_node rb_node; | |
261 | struct symbol sym; | |
262 | }; | |
263 | ||
264 | static void symbols__insert_by_name(struct rb_root *self, struct symbol *sym) | |
265 | { | |
266 | struct rb_node **p = &self->rb_node; | |
267 | struct rb_node *parent = NULL; | |
268 | struct symbol_name_rb_node *symn = ((void *)sym) - sizeof(*parent), *s; | |
269 | ||
270 | while (*p != NULL) { | |
271 | parent = *p; | |
272 | s = rb_entry(parent, struct symbol_name_rb_node, rb_node); | |
273 | if (strcmp(sym->name, s->sym.name) < 0) | |
274 | p = &(*p)->rb_left; | |
275 | else | |
276 | p = &(*p)->rb_right; | |
277 | } | |
278 | rb_link_node(&symn->rb_node, parent, p); | |
279 | rb_insert_color(&symn->rb_node, self); | |
280 | } | |
281 | ||
282 | static void symbols__sort_by_name(struct rb_root *self, struct rb_root *source) | |
283 | { | |
284 | struct rb_node *nd; | |
285 | ||
286 | for (nd = rb_first(source); nd; nd = rb_next(nd)) { | |
287 | struct symbol *pos = rb_entry(nd, struct symbol, rb_node); | |
288 | symbols__insert_by_name(self, pos); | |
289 | } | |
290 | } | |
291 | ||
292 | static struct symbol *symbols__find_by_name(struct rb_root *self, const char *name) | |
293 | { | |
294 | struct rb_node *n; | |
295 | ||
296 | if (self == NULL) | |
297 | return NULL; | |
298 | ||
299 | n = self->rb_node; | |
300 | ||
301 | while (n) { | |
302 | struct symbol_name_rb_node *s; | |
303 | int cmp; | |
304 | ||
305 | s = rb_entry(n, struct symbol_name_rb_node, rb_node); | |
306 | cmp = strcmp(name, s->sym.name); | |
307 | ||
308 | if (cmp < 0) | |
309 | n = n->rb_left; | |
310 | else if (cmp > 0) | |
311 | n = n->rb_right; | |
312 | else | |
313 | return &s->sym; | |
314 | } | |
315 | ||
316 | return NULL; | |
317 | } | |
318 | ||
319 | struct symbol *dso__find_symbol(struct dso *self, | |
320 | enum map_type type, u64 addr) | |
fcf1203a | 321 | { |
6a4694a4 | 322 | return symbols__find(&self->symbols[type], addr); |
fcf1203a ACM |
323 | } |
324 | ||
79406cd7 ACM |
325 | struct symbol *dso__find_symbol_by_name(struct dso *self, enum map_type type, |
326 | const char *name) | |
327 | { | |
328 | return symbols__find_by_name(&self->symbol_names[type], name); | |
329 | } | |
330 | ||
331 | void dso__sort_by_name(struct dso *self, enum map_type type) | |
332 | { | |
333 | dso__set_sorted_by_name(self, type); | |
334 | return symbols__sort_by_name(&self->symbol_names[type], | |
335 | &self->symbols[type]); | |
336 | } | |
337 | ||
ef12a141 | 338 | int build_id__sprintf(const u8 *self, int len, char *bf) |
a2928c42 | 339 | { |
8d06367f | 340 | char *bid = bf; |
ef12a141 | 341 | const u8 *raw = self; |
8d06367f | 342 | int i; |
a2928c42 | 343 | |
8d06367f ACM |
344 | for (i = 0; i < len; ++i) { |
345 | sprintf(bid, "%02x", *raw); | |
346 | ++raw; | |
347 | bid += 2; | |
348 | } | |
349 | ||
350 | return raw - self; | |
351 | } | |
352 | ||
9e03eb2d | 353 | size_t dso__fprintf_buildid(struct dso *self, FILE *fp) |
8d06367f ACM |
354 | { |
355 | char sbuild_id[BUILD_ID_SIZE * 2 + 1]; | |
8d06367f ACM |
356 | |
357 | build_id__sprintf(self->build_id, sizeof(self->build_id), sbuild_id); | |
9e03eb2d ACM |
358 | return fprintf(fp, "%s", sbuild_id); |
359 | } | |
360 | ||
95011c60 | 361 | size_t dso__fprintf(struct dso *self, enum map_type type, FILE *fp) |
9e03eb2d ACM |
362 | { |
363 | struct rb_node *nd; | |
364 | size_t ret = fprintf(fp, "dso: %s (", self->short_name); | |
365 | ||
3846df2e ACM |
366 | if (self->short_name != self->long_name) |
367 | ret += fprintf(fp, "%s, ", self->long_name); | |
368 | ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type], | |
369 | self->loaded ? "" : "NOT "); | |
9e03eb2d | 370 | ret += dso__fprintf_buildid(self, fp); |
6a4694a4 | 371 | ret += fprintf(fp, ")\n"); |
95011c60 ACM |
372 | for (nd = rb_first(&self->symbols[type]); nd; nd = rb_next(nd)) { |
373 | struct symbol *pos = rb_entry(nd, struct symbol, rb_node); | |
374 | ret += symbol__fprintf(pos, fp); | |
a2928c42 ACM |
375 | } |
376 | ||
377 | return ret; | |
378 | } | |
379 | ||
9e201442 ACM |
380 | int kallsyms__parse(const char *filename, void *arg, |
381 | int (*process_symbol)(void *arg, const char *name, | |
682b335a | 382 | char type, u64 start)) |
a2928c42 | 383 | { |
a2928c42 ACM |
384 | char *line = NULL; |
385 | size_t n; | |
682b335a | 386 | int err = 0; |
9e201442 | 387 | FILE *file = fopen(filename, "r"); |
a2928c42 ACM |
388 | |
389 | if (file == NULL) | |
390 | goto out_failure; | |
391 | ||
392 | while (!feof(file)) { | |
9cffa8d5 | 393 | u64 start; |
a2928c42 ACM |
394 | int line_len, len; |
395 | char symbol_type; | |
2e538c4a | 396 | char *symbol_name; |
a2928c42 ACM |
397 | |
398 | line_len = getline(&line, &n, file); | |
399 | if (line_len < 0) | |
400 | break; | |
401 | ||
402 | if (!line) | |
403 | goto out_failure; | |
404 | ||
405 | line[--line_len] = '\0'; /* \n */ | |
406 | ||
a0055ae2 | 407 | len = hex2u64(line, &start); |
a2928c42 ACM |
408 | |
409 | len++; | |
410 | if (len + 2 >= line_len) | |
411 | continue; | |
412 | ||
413 | symbol_type = toupper(line[len]); | |
af427bf5 | 414 | symbol_name = line + len + 2; |
682b335a ACM |
415 | |
416 | err = process_symbol(arg, symbol_name, symbol_type, start); | |
417 | if (err) | |
418 | break; | |
2e538c4a ACM |
419 | } |
420 | ||
421 | free(line); | |
422 | fclose(file); | |
682b335a | 423 | return err; |
2e538c4a | 424 | |
2e538c4a ACM |
425 | out_failure: |
426 | return -1; | |
427 | } | |
428 | ||
682b335a ACM |
429 | struct process_kallsyms_args { |
430 | struct map *map; | |
431 | struct dso *dso; | |
432 | }; | |
433 | ||
434 | static int map__process_kallsym_symbol(void *arg, const char *name, | |
435 | char type, u64 start) | |
436 | { | |
437 | struct symbol *sym; | |
438 | struct process_kallsyms_args *a = arg; | |
439 | struct rb_root *root = &a->dso->symbols[a->map->type]; | |
440 | ||
441 | if (!symbol_type__is_a(type, a->map->type)) | |
442 | return 0; | |
443 | ||
444 | /* | |
445 | * Will fix up the end later, when we have all symbols sorted. | |
446 | */ | |
447 | sym = symbol__new(start, 0, name); | |
448 | ||
449 | if (sym == NULL) | |
450 | return -ENOMEM; | |
451 | /* | |
452 | * We will pass the symbols to the filter later, in | |
453 | * map__split_kallsyms, when we have split the maps per module | |
454 | */ | |
455 | symbols__insert(root, sym); | |
456 | return 0; | |
457 | } | |
458 | ||
459 | /* | |
460 | * Loads the function entries in /proc/kallsyms into kernel_map->dso, | |
461 | * so that we can in the next step set the symbol ->end address and then | |
462 | * call kernel_maps__split_kallsyms. | |
463 | */ | |
9e201442 ACM |
464 | static int dso__load_all_kallsyms(struct dso *self, const char *filename, |
465 | struct map *map) | |
682b335a ACM |
466 | { |
467 | struct process_kallsyms_args args = { .map = map, .dso = self, }; | |
9e201442 | 468 | return kallsyms__parse(filename, &args, map__process_kallsym_symbol); |
682b335a ACM |
469 | } |
470 | ||
2e538c4a ACM |
471 | /* |
472 | * Split the symbols into maps, making sure there are no overlaps, i.e. the | |
473 | * kernel range is broken in several maps, named [kernel].N, as we don't have | |
474 | * the original ELF section names vmlinux have. | |
475 | */ | |
9958e1f0 | 476 | static int dso__split_kallsyms(struct dso *self, struct map *map, |
9de89fe7 | 477 | symbol_filter_t filter) |
2e538c4a | 478 | { |
9de89fe7 | 479 | struct map_groups *kmaps = map__kmap(map)->kmaps; |
4e06255f | 480 | struct map *curr_map = map; |
2e538c4a ACM |
481 | struct symbol *pos; |
482 | int count = 0; | |
4e06255f ACM |
483 | struct rb_root *root = &self->symbols[map->type]; |
484 | struct rb_node *next = rb_first(root); | |
2e538c4a ACM |
485 | int kernel_range = 0; |
486 | ||
487 | while (next) { | |
488 | char *module; | |
489 | ||
490 | pos = rb_entry(next, struct symbol, rb_node); | |
491 | next = rb_next(&pos->rb_node); | |
492 | ||
493 | module = strchr(pos->name, '\t'); | |
494 | if (module) { | |
75be6cf4 | 495 | if (!symbol_conf.use_modules) |
1de8e245 ACM |
496 | goto discard_symbol; |
497 | ||
2e538c4a ACM |
498 | *module++ = '\0'; |
499 | ||
b7cece76 | 500 | if (strcmp(curr_map->dso->short_name, module)) { |
9de89fe7 | 501 | curr_map = map_groups__find_by_name(kmaps, map->type, module); |
4e06255f | 502 | if (curr_map == NULL) { |
95011c60 | 503 | pr_debug("/proc/{kallsyms,modules} " |
b7cece76 ACM |
504 | "inconsistency while looking " |
505 | "for \"%s\" module!\n", module); | |
af427bf5 ACM |
506 | return -1; |
507 | } | |
b7cece76 ACM |
508 | |
509 | if (curr_map->dso->loaded) | |
510 | goto discard_symbol; | |
af427bf5 | 511 | } |
2e538c4a ACM |
512 | /* |
513 | * So that we look just like we get from .ko files, | |
514 | * i.e. not prelinked, relative to map->start. | |
515 | */ | |
4e06255f ACM |
516 | pos->start = curr_map->map_ip(curr_map, pos->start); |
517 | pos->end = curr_map->map_ip(curr_map, pos->end); | |
518 | } else if (curr_map != map) { | |
2e538c4a ACM |
519 | char dso_name[PATH_MAX]; |
520 | struct dso *dso; | |
521 | ||
522 | snprintf(dso_name, sizeof(dso_name), "[kernel].%d", | |
523 | kernel_range++); | |
524 | ||
00a192b3 | 525 | dso = dso__new(dso_name); |
2e538c4a ACM |
526 | if (dso == NULL) |
527 | return -1; | |
528 | ||
4e06255f | 529 | curr_map = map__new2(pos->start, dso, map->type); |
37fe5fcb | 530 | if (curr_map == NULL) { |
2e538c4a ACM |
531 | dso__delete(dso); |
532 | return -1; | |
533 | } | |
a2928c42 | 534 | |
4e06255f | 535 | curr_map->map_ip = curr_map->unmap_ip = identity__map_ip; |
9de89fe7 | 536 | map_groups__insert(kmaps, curr_map); |
2e538c4a ACM |
537 | ++kernel_range; |
538 | } | |
a2928c42 | 539 | |
4e06255f | 540 | if (filter && filter(curr_map, pos)) { |
1de8e245 | 541 | discard_symbol: rb_erase(&pos->rb_node, root); |
00a192b3 | 542 | symbol__delete(pos); |
2e538c4a | 543 | } else { |
4e06255f ACM |
544 | if (curr_map != map) { |
545 | rb_erase(&pos->rb_node, root); | |
546 | symbols__insert(&curr_map->dso->symbols[curr_map->type], pos); | |
2e538c4a | 547 | } |
9974f496 MG |
548 | count++; |
549 | } | |
a2928c42 ACM |
550 | } |
551 | ||
9974f496 | 552 | return count; |
2e538c4a | 553 | } |
a2928c42 | 554 | |
9de89fe7 ACM |
555 | int dso__load_kallsyms(struct dso *self, const char *filename, |
556 | struct map *map, symbol_filter_t filter) | |
2e538c4a | 557 | { |
9e201442 | 558 | if (dso__load_all_kallsyms(self, filename, map) < 0) |
2e538c4a ACM |
559 | return -1; |
560 | ||
4e06255f ACM |
561 | symbols__fixup_end(&self->symbols[map->type]); |
562 | self->origin = DSO__ORIG_KERNEL; | |
2e538c4a | 563 | |
9de89fe7 | 564 | return dso__split_kallsyms(self, map, filter); |
af427bf5 ACM |
565 | } |
566 | ||
439d473b | 567 | static int dso__load_perf_map(struct dso *self, struct map *map, |
6beba7ad | 568 | symbol_filter_t filter) |
80d496be PE |
569 | { |
570 | char *line = NULL; | |
571 | size_t n; | |
572 | FILE *file; | |
573 | int nr_syms = 0; | |
574 | ||
439d473b | 575 | file = fopen(self->long_name, "r"); |
80d496be PE |
576 | if (file == NULL) |
577 | goto out_failure; | |
578 | ||
579 | while (!feof(file)) { | |
9cffa8d5 | 580 | u64 start, size; |
80d496be PE |
581 | struct symbol *sym; |
582 | int line_len, len; | |
583 | ||
584 | line_len = getline(&line, &n, file); | |
585 | if (line_len < 0) | |
586 | break; | |
587 | ||
588 | if (!line) | |
589 | goto out_failure; | |
590 | ||
591 | line[--line_len] = '\0'; /* \n */ | |
592 | ||
593 | len = hex2u64(line, &start); | |
594 | ||
595 | len++; | |
596 | if (len + 2 >= line_len) | |
597 | continue; | |
598 | ||
599 | len += hex2u64(line + len, &size); | |
600 | ||
601 | len++; | |
602 | if (len + 2 >= line_len) | |
603 | continue; | |
604 | ||
00a192b3 | 605 | sym = symbol__new(start, size, line + len); |
80d496be PE |
606 | |
607 | if (sym == NULL) | |
608 | goto out_delete_line; | |
609 | ||
439d473b | 610 | if (filter && filter(map, sym)) |
00a192b3 | 611 | symbol__delete(sym); |
80d496be | 612 | else { |
6a4694a4 | 613 | symbols__insert(&self->symbols[map->type], sym); |
80d496be PE |
614 | nr_syms++; |
615 | } | |
616 | } | |
617 | ||
618 | free(line); | |
619 | fclose(file); | |
620 | ||
621 | return nr_syms; | |
622 | ||
623 | out_delete_line: | |
624 | free(line); | |
625 | out_failure: | |
626 | return -1; | |
627 | } | |
628 | ||
a2928c42 ACM |
629 | /** |
630 | * elf_symtab__for_each_symbol - iterate thru all the symbols | |
631 | * | |
632 | * @self: struct elf_symtab instance to iterate | |
83a0944f | 633 | * @idx: uint32_t idx |
a2928c42 ACM |
634 | * @sym: GElf_Sym iterator |
635 | */ | |
83a0944f IM |
636 | #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \ |
637 | for (idx = 0, gelf_getsym(syms, idx, &sym);\ | |
638 | idx < nr_syms; \ | |
639 | idx++, gelf_getsym(syms, idx, &sym)) | |
a2928c42 ACM |
640 | |
641 | static inline uint8_t elf_sym__type(const GElf_Sym *sym) | |
642 | { | |
643 | return GELF_ST_TYPE(sym->st_info); | |
644 | } | |
645 | ||
646 | static inline int elf_sym__is_function(const GElf_Sym *sym) | |
647 | { | |
648 | return elf_sym__type(sym) == STT_FUNC && | |
649 | sym->st_name != 0 && | |
81833130 | 650 | sym->st_shndx != SHN_UNDEF; |
a2928c42 ACM |
651 | } |
652 | ||
f1dfa0b1 ACM |
653 | static inline bool elf_sym__is_object(const GElf_Sym *sym) |
654 | { | |
655 | return elf_sym__type(sym) == STT_OBJECT && | |
656 | sym->st_name != 0 && | |
657 | sym->st_shndx != SHN_UNDEF; | |
658 | } | |
659 | ||
6cfcc53e MG |
660 | static inline int elf_sym__is_label(const GElf_Sym *sym) |
661 | { | |
662 | return elf_sym__type(sym) == STT_NOTYPE && | |
663 | sym->st_name != 0 && | |
664 | sym->st_shndx != SHN_UNDEF && | |
665 | sym->st_shndx != SHN_ABS; | |
666 | } | |
667 | ||
668 | static inline const char *elf_sec__name(const GElf_Shdr *shdr, | |
669 | const Elf_Data *secstrs) | |
670 | { | |
671 | return secstrs->d_buf + shdr->sh_name; | |
672 | } | |
673 | ||
674 | static inline int elf_sec__is_text(const GElf_Shdr *shdr, | |
675 | const Elf_Data *secstrs) | |
676 | { | |
677 | return strstr(elf_sec__name(shdr, secstrs), "text") != NULL; | |
678 | } | |
679 | ||
f1dfa0b1 ACM |
680 | static inline bool elf_sec__is_data(const GElf_Shdr *shdr, |
681 | const Elf_Data *secstrs) | |
682 | { | |
683 | return strstr(elf_sec__name(shdr, secstrs), "data") != NULL; | |
684 | } | |
685 | ||
a2928c42 ACM |
686 | static inline const char *elf_sym__name(const GElf_Sym *sym, |
687 | const Elf_Data *symstrs) | |
688 | { | |
689 | return symstrs->d_buf + sym->st_name; | |
690 | } | |
691 | ||
692 | static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep, | |
693 | GElf_Shdr *shp, const char *name, | |
83a0944f | 694 | size_t *idx) |
a2928c42 ACM |
695 | { |
696 | Elf_Scn *sec = NULL; | |
697 | size_t cnt = 1; | |
698 | ||
699 | while ((sec = elf_nextscn(elf, sec)) != NULL) { | |
700 | char *str; | |
701 | ||
702 | gelf_getshdr(sec, shp); | |
703 | str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name); | |
704 | if (!strcmp(name, str)) { | |
83a0944f IM |
705 | if (idx) |
706 | *idx = cnt; | |
a2928c42 ACM |
707 | break; |
708 | } | |
709 | ++cnt; | |
710 | } | |
711 | ||
712 | return sec; | |
713 | } | |
714 | ||
8ce998d6 ACM |
715 | #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \ |
716 | for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \ | |
717 | idx < nr_entries; \ | |
718 | ++idx, pos = gelf_getrel(reldata, idx, &pos_mem)) | |
719 | ||
720 | #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \ | |
721 | for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \ | |
722 | idx < nr_entries; \ | |
723 | ++idx, pos = gelf_getrela(reldata, idx, &pos_mem)) | |
724 | ||
a25e46c4 ACM |
725 | /* |
726 | * We need to check if we have a .dynsym, so that we can handle the | |
727 | * .plt, synthesizing its symbols, that aren't on the symtabs (be it | |
728 | * .dynsym or .symtab). | |
729 | * And always look at the original dso, not at debuginfo packages, that | |
730 | * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS). | |
731 | */ | |
82164161 ACM |
732 | static int dso__synthesize_plt_symbols(struct dso *self, struct map *map, |
733 | symbol_filter_t filter) | |
8ce998d6 ACM |
734 | { |
735 | uint32_t nr_rel_entries, idx; | |
736 | GElf_Sym sym; | |
9cffa8d5 | 737 | u64 plt_offset; |
8ce998d6 ACM |
738 | GElf_Shdr shdr_plt; |
739 | struct symbol *f; | |
a25e46c4 | 740 | GElf_Shdr shdr_rel_plt, shdr_dynsym; |
8ce998d6 | 741 | Elf_Data *reldata, *syms, *symstrs; |
a25e46c4 ACM |
742 | Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym; |
743 | size_t dynsym_idx; | |
744 | GElf_Ehdr ehdr; | |
8ce998d6 | 745 | char sympltname[1024]; |
a25e46c4 ACM |
746 | Elf *elf; |
747 | int nr = 0, symidx, fd, err = 0; | |
748 | ||
439d473b | 749 | fd = open(self->long_name, O_RDONLY); |
a25e46c4 ACM |
750 | if (fd < 0) |
751 | goto out; | |
752 | ||
84087126 | 753 | elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); |
a25e46c4 ACM |
754 | if (elf == NULL) |
755 | goto out_close; | |
756 | ||
757 | if (gelf_getehdr(elf, &ehdr) == NULL) | |
758 | goto out_elf_end; | |
759 | ||
760 | scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym, | |
761 | ".dynsym", &dynsym_idx); | |
762 | if (scn_dynsym == NULL) | |
763 | goto out_elf_end; | |
8ce998d6 | 764 | |
a25e46c4 | 765 | scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt, |
8ce998d6 ACM |
766 | ".rela.plt", NULL); |
767 | if (scn_plt_rel == NULL) { | |
a25e46c4 | 768 | scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt, |
8ce998d6 ACM |
769 | ".rel.plt", NULL); |
770 | if (scn_plt_rel == NULL) | |
a25e46c4 | 771 | goto out_elf_end; |
8ce998d6 ACM |
772 | } |
773 | ||
a25e46c4 ACM |
774 | err = -1; |
775 | ||
8ce998d6 | 776 | if (shdr_rel_plt.sh_link != dynsym_idx) |
a25e46c4 | 777 | goto out_elf_end; |
8ce998d6 | 778 | |
a25e46c4 ACM |
779 | if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL) |
780 | goto out_elf_end; | |
8ce998d6 ACM |
781 | |
782 | /* | |
83a0944f | 783 | * Fetch the relocation section to find the idxes to the GOT |
8ce998d6 ACM |
784 | * and the symbols in the .dynsym they refer to. |
785 | */ | |
786 | reldata = elf_getdata(scn_plt_rel, NULL); | |
787 | if (reldata == NULL) | |
a25e46c4 | 788 | goto out_elf_end; |
8ce998d6 ACM |
789 | |
790 | syms = elf_getdata(scn_dynsym, NULL); | |
791 | if (syms == NULL) | |
a25e46c4 | 792 | goto out_elf_end; |
8ce998d6 | 793 | |
a25e46c4 | 794 | scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link); |
8ce998d6 | 795 | if (scn_symstrs == NULL) |
a25e46c4 | 796 | goto out_elf_end; |
8ce998d6 ACM |
797 | |
798 | symstrs = elf_getdata(scn_symstrs, NULL); | |
799 | if (symstrs == NULL) | |
a25e46c4 | 800 | goto out_elf_end; |
8ce998d6 ACM |
801 | |
802 | nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize; | |
803 | plt_offset = shdr_plt.sh_offset; | |
804 | ||
805 | if (shdr_rel_plt.sh_type == SHT_RELA) { | |
806 | GElf_Rela pos_mem, *pos; | |
807 | ||
808 | elf_section__for_each_rela(reldata, pos, pos_mem, idx, | |
809 | nr_rel_entries) { | |
810 | symidx = GELF_R_SYM(pos->r_info); | |
811 | plt_offset += shdr_plt.sh_entsize; | |
812 | gelf_getsym(syms, symidx, &sym); | |
813 | snprintf(sympltname, sizeof(sympltname), | |
814 | "%s@plt", elf_sym__name(&sym, symstrs)); | |
815 | ||
816 | f = symbol__new(plt_offset, shdr_plt.sh_entsize, | |
00a192b3 | 817 | sympltname); |
8ce998d6 | 818 | if (!f) |
a25e46c4 | 819 | goto out_elf_end; |
8ce998d6 | 820 | |
82164161 ACM |
821 | if (filter && filter(map, f)) |
822 | symbol__delete(f); | |
823 | else { | |
6a4694a4 | 824 | symbols__insert(&self->symbols[map->type], f); |
82164161 ACM |
825 | ++nr; |
826 | } | |
8ce998d6 ACM |
827 | } |
828 | } else if (shdr_rel_plt.sh_type == SHT_REL) { | |
829 | GElf_Rel pos_mem, *pos; | |
830 | elf_section__for_each_rel(reldata, pos, pos_mem, idx, | |
831 | nr_rel_entries) { | |
832 | symidx = GELF_R_SYM(pos->r_info); | |
833 | plt_offset += shdr_plt.sh_entsize; | |
834 | gelf_getsym(syms, symidx, &sym); | |
835 | snprintf(sympltname, sizeof(sympltname), | |
836 | "%s@plt", elf_sym__name(&sym, symstrs)); | |
837 | ||
838 | f = symbol__new(plt_offset, shdr_plt.sh_entsize, | |
00a192b3 | 839 | sympltname); |
8ce998d6 | 840 | if (!f) |
a25e46c4 | 841 | goto out_elf_end; |
8ce998d6 | 842 | |
82164161 ACM |
843 | if (filter && filter(map, f)) |
844 | symbol__delete(f); | |
845 | else { | |
6a4694a4 | 846 | symbols__insert(&self->symbols[map->type], f); |
82164161 ACM |
847 | ++nr; |
848 | } | |
8ce998d6 | 849 | } |
8ce998d6 ACM |
850 | } |
851 | ||
a25e46c4 ACM |
852 | err = 0; |
853 | out_elf_end: | |
854 | elf_end(elf); | |
855 | out_close: | |
856 | close(fd); | |
857 | ||
858 | if (err == 0) | |
859 | return nr; | |
860 | out: | |
fe2197b8 ACM |
861 | pr_debug("%s: problems reading %s PLT info.\n", |
862 | __func__, self->long_name); | |
a25e46c4 | 863 | return 0; |
8ce998d6 ACM |
864 | } |
865 | ||
d45868d3 ACM |
866 | static bool elf_sym__is_a(GElf_Sym *self, enum map_type type) |
867 | { | |
868 | switch (type) { | |
869 | case MAP__FUNCTION: | |
870 | return elf_sym__is_function(self); | |
f1dfa0b1 ACM |
871 | case MAP__VARIABLE: |
872 | return elf_sym__is_object(self); | |
d45868d3 ACM |
873 | default: |
874 | return false; | |
875 | } | |
876 | } | |
877 | ||
878 | static bool elf_sec__is_a(GElf_Shdr *self, Elf_Data *secstrs, enum map_type type) | |
879 | { | |
880 | switch (type) { | |
881 | case MAP__FUNCTION: | |
882 | return elf_sec__is_text(self, secstrs); | |
f1dfa0b1 ACM |
883 | case MAP__VARIABLE: |
884 | return elf_sec__is_data(self, secstrs); | |
d45868d3 ACM |
885 | default: |
886 | return false; | |
887 | } | |
888 | } | |
889 | ||
9de89fe7 ACM |
890 | static int dso__load_sym(struct dso *self, struct map *map, const char *name, |
891 | int fd, symbol_filter_t filter, int kmodule) | |
a2928c42 | 892 | { |
9de89fe7 | 893 | struct kmap *kmap = self->kernel ? map__kmap(map) : NULL; |
2e538c4a ACM |
894 | struct map *curr_map = map; |
895 | struct dso *curr_dso = self; | |
6cfcc53e | 896 | Elf_Data *symstrs, *secstrs; |
a2928c42 ACM |
897 | uint32_t nr_syms; |
898 | int err = -1; | |
83a0944f | 899 | uint32_t idx; |
a2928c42 ACM |
900 | GElf_Ehdr ehdr; |
901 | GElf_Shdr shdr; | |
902 | Elf_Data *syms; | |
903 | GElf_Sym sym; | |
a25e46c4 | 904 | Elf_Scn *sec, *sec_strndx; |
a2928c42 | 905 | Elf *elf; |
439d473b | 906 | int nr = 0; |
a2928c42 | 907 | |
84087126 | 908 | elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); |
a2928c42 | 909 | if (elf == NULL) { |
6beba7ad | 910 | pr_err("%s: cannot read %s ELF file.\n", __func__, name); |
a2928c42 ACM |
911 | goto out_close; |
912 | } | |
913 | ||
914 | if (gelf_getehdr(elf, &ehdr) == NULL) { | |
6beba7ad | 915 | pr_err("%s: cannot get elf header.\n", __func__); |
a2928c42 ACM |
916 | goto out_elf_end; |
917 | } | |
918 | ||
919 | sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL); | |
8ce998d6 | 920 | if (sec == NULL) { |
a25e46c4 ACM |
921 | sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL); |
922 | if (sec == NULL) | |
8ce998d6 | 923 | goto out_elf_end; |
8ce998d6 | 924 | } |
a2928c42 ACM |
925 | |
926 | syms = elf_getdata(sec, NULL); | |
927 | if (syms == NULL) | |
928 | goto out_elf_end; | |
929 | ||
930 | sec = elf_getscn(elf, shdr.sh_link); | |
931 | if (sec == NULL) | |
932 | goto out_elf_end; | |
933 | ||
934 | symstrs = elf_getdata(sec, NULL); | |
935 | if (symstrs == NULL) | |
936 | goto out_elf_end; | |
937 | ||
6cfcc53e MG |
938 | sec_strndx = elf_getscn(elf, ehdr.e_shstrndx); |
939 | if (sec_strndx == NULL) | |
940 | goto out_elf_end; | |
941 | ||
942 | secstrs = elf_getdata(sec_strndx, NULL); | |
9b30a26b | 943 | if (secstrs == NULL) |
6cfcc53e MG |
944 | goto out_elf_end; |
945 | ||
a2928c42 ACM |
946 | nr_syms = shdr.sh_size / shdr.sh_entsize; |
947 | ||
e9fbc9dc | 948 | memset(&sym, 0, sizeof(sym)); |
9de89fe7 | 949 | if (!self->kernel) { |
d20ff6bd | 950 | self->adjust_symbols = (ehdr.e_type == ET_EXEC || |
30d7a77d ACM |
951 | elf_section_by_name(elf, &ehdr, &shdr, |
952 | ".gnu.prelink_undo", | |
953 | NULL) != NULL); | |
d20ff6bd MG |
954 | } else self->adjust_symbols = 0; |
955 | ||
83a0944f | 956 | elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) { |
a2928c42 | 957 | struct symbol *f; |
56b03f3c | 958 | const char *elf_name = elf_sym__name(&sym, symstrs); |
2e538c4a | 959 | char *demangled = NULL; |
6cfcc53e MG |
960 | int is_label = elf_sym__is_label(&sym); |
961 | const char *section_name; | |
a2928c42 | 962 | |
9de89fe7 ACM |
963 | if (kmap && kmap->ref_reloc_sym && kmap->ref_reloc_sym->name && |
964 | strcmp(elf_name, kmap->ref_reloc_sym->name) == 0) | |
965 | kmap->ref_reloc_sym->unrelocated_addr = sym.st_value; | |
56b03f3c | 966 | |
d45868d3 | 967 | if (!is_label && !elf_sym__is_a(&sym, map->type)) |
a2928c42 ACM |
968 | continue; |
969 | ||
970 | sec = elf_getscn(elf, sym.st_shndx); | |
971 | if (!sec) | |
972 | goto out_elf_end; | |
973 | ||
974 | gelf_getshdr(sec, &shdr); | |
6cfcc53e | 975 | |
d45868d3 | 976 | if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type)) |
6cfcc53e MG |
977 | continue; |
978 | ||
979 | section_name = elf_sec__name(&shdr, secstrs); | |
0b73da3f | 980 | |
9de89fe7 | 981 | if (self->kernel || kmodule) { |
2e538c4a ACM |
982 | char dso_name[PATH_MAX]; |
983 | ||
984 | if (strcmp(section_name, | |
b63be8d7 ACM |
985 | (curr_dso->short_name + |
986 | self->short_name_len)) == 0) | |
2e538c4a ACM |
987 | goto new_symbol; |
988 | ||
989 | if (strcmp(section_name, ".text") == 0) { | |
990 | curr_map = map; | |
991 | curr_dso = self; | |
992 | goto new_symbol; | |
993 | } | |
994 | ||
995 | snprintf(dso_name, sizeof(dso_name), | |
996 | "%s%s", self->short_name, section_name); | |
997 | ||
9de89fe7 | 998 | curr_map = map_groups__find_by_name(kmap->kmaps, map->type, dso_name); |
2e538c4a ACM |
999 | if (curr_map == NULL) { |
1000 | u64 start = sym.st_value; | |
1001 | ||
1002 | if (kmodule) | |
1003 | start += map->start + shdr.sh_offset; | |
1004 | ||
00a192b3 | 1005 | curr_dso = dso__new(dso_name); |
2e538c4a ACM |
1006 | if (curr_dso == NULL) |
1007 | goto out_elf_end; | |
3610583c | 1008 | curr_map = map__new2(start, curr_dso, |
6275ce2d | 1009 | map->type); |
2e538c4a ACM |
1010 | if (curr_map == NULL) { |
1011 | dso__delete(curr_dso); | |
1012 | goto out_elf_end; | |
1013 | } | |
ed52ce2e ACM |
1014 | curr_map->map_ip = identity__map_ip; |
1015 | curr_map->unmap_ip = identity__map_ip; | |
b0a9ab62 | 1016 | curr_dso->origin = self->origin; |
9de89fe7 | 1017 | map_groups__insert(kmap->kmaps, curr_map); |
b0da954a | 1018 | dsos__add(&dsos__kernel, curr_dso); |
6275ce2d | 1019 | dso__set_loaded(curr_dso, map->type); |
2e538c4a ACM |
1020 | } else |
1021 | curr_dso = curr_map->dso; | |
1022 | ||
1023 | goto new_symbol; | |
af427bf5 ACM |
1024 | } |
1025 | ||
2e538c4a | 1026 | if (curr_dso->adjust_symbols) { |
29a9f66d ACM |
1027 | pr_debug4("%s: adjusting symbol: st_value: %#Lx " |
1028 | "sh_addr: %#Lx sh_offset: %#Lx\n", __func__, | |
1029 | (u64)sym.st_value, (u64)shdr.sh_addr, | |
1030 | (u64)shdr.sh_offset); | |
f5812a7a | 1031 | sym.st_value -= shdr.sh_addr - shdr.sh_offset; |
af427bf5 | 1032 | } |
28ac909b ACM |
1033 | /* |
1034 | * We need to figure out if the object was created from C++ sources | |
1035 | * DWARF DW_compile_unit has this, but we don't always have access | |
1036 | * to it... | |
1037 | */ | |
83a0944f | 1038 | demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI); |
28ac909b | 1039 | if (demangled != NULL) |
83a0944f | 1040 | elf_name = demangled; |
2e538c4a | 1041 | new_symbol: |
00a192b3 | 1042 | f = symbol__new(sym.st_value, sym.st_size, elf_name); |
28ac909b | 1043 | free(demangled); |
a2928c42 ACM |
1044 | if (!f) |
1045 | goto out_elf_end; | |
1046 | ||
2e538c4a | 1047 | if (filter && filter(curr_map, f)) |
00a192b3 | 1048 | symbol__delete(f); |
69ee69f6 | 1049 | else { |
6a4694a4 | 1050 | symbols__insert(&curr_dso->symbols[curr_map->type], f); |
69ee69f6 ACM |
1051 | nr++; |
1052 | } | |
a2928c42 ACM |
1053 | } |
1054 | ||
2e538c4a ACM |
1055 | /* |
1056 | * For misannotated, zeroed, ASM function sizes. | |
1057 | */ | |
6275ce2d | 1058 | if (nr > 0) { |
6a4694a4 | 1059 | symbols__fixup_end(&self->symbols[map->type]); |
6275ce2d ACM |
1060 | if (kmap) { |
1061 | /* | |
1062 | * We need to fixup this here too because we create new | |
1063 | * maps here, for things like vsyscall sections. | |
1064 | */ | |
1065 | __map_groups__fixup_end(kmap->kmaps, map->type); | |
1066 | } | |
1067 | } | |
a2928c42 ACM |
1068 | err = nr; |
1069 | out_elf_end: | |
1070 | elf_end(elf); | |
1071 | out_close: | |
1072 | return err; | |
1073 | } | |
1074 | ||
78075caa ACM |
1075 | static bool dso__build_id_equal(const struct dso *self, u8 *build_id) |
1076 | { | |
1077 | return memcmp(self->build_id, build_id, sizeof(self->build_id)) == 0; | |
1078 | } | |
1079 | ||
6122e4e4 | 1080 | static bool __dsos__read_build_ids(struct list_head *head, bool with_hits) |
57f395a7 | 1081 | { |
e30a3d12 | 1082 | bool have_build_id = false; |
57f395a7 FW |
1083 | struct dso *pos; |
1084 | ||
6122e4e4 ACM |
1085 | list_for_each_entry(pos, head, node) { |
1086 | if (with_hits && !pos->hit) | |
1087 | continue; | |
e30a3d12 ACM |
1088 | if (filename__read_build_id(pos->long_name, pos->build_id, |
1089 | sizeof(pos->build_id)) > 0) { | |
1090 | have_build_id = true; | |
1091 | pos->has_build_id = true; | |
1092 | } | |
6122e4e4 | 1093 | } |
57f395a7 | 1094 | |
e30a3d12 | 1095 | return have_build_id; |
57f395a7 FW |
1096 | } |
1097 | ||
6122e4e4 | 1098 | bool dsos__read_build_ids(bool with_hits) |
b0da954a | 1099 | { |
6122e4e4 ACM |
1100 | bool kbuildids = __dsos__read_build_ids(&dsos__kernel, with_hits), |
1101 | ubuildids = __dsos__read_build_ids(&dsos__user, with_hits); | |
8b4825bf | 1102 | return kbuildids || ubuildids; |
b0da954a ACM |
1103 | } |
1104 | ||
fd7a346e ACM |
1105 | /* |
1106 | * Align offset to 4 bytes as needed for note name and descriptor data. | |
1107 | */ | |
1108 | #define NOTE_ALIGN(n) (((n) + 3) & -4U) | |
1109 | ||
2643ce11 | 1110 | int filename__read_build_id(const char *filename, void *bf, size_t size) |
4d1e00a8 | 1111 | { |
2643ce11 | 1112 | int fd, err = -1; |
4d1e00a8 ACM |
1113 | GElf_Ehdr ehdr; |
1114 | GElf_Shdr shdr; | |
fd7a346e | 1115 | Elf_Data *data; |
4d1e00a8 | 1116 | Elf_Scn *sec; |
e57cfcda | 1117 | Elf_Kind ek; |
fd7a346e | 1118 | void *ptr; |
4d1e00a8 | 1119 | Elf *elf; |
4d1e00a8 | 1120 | |
2643ce11 ACM |
1121 | if (size < BUILD_ID_SIZE) |
1122 | goto out; | |
1123 | ||
1124 | fd = open(filename, O_RDONLY); | |
4d1e00a8 ACM |
1125 | if (fd < 0) |
1126 | goto out; | |
1127 | ||
84087126 | 1128 | elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); |
4d1e00a8 | 1129 | if (elf == NULL) { |
8d06367f | 1130 | pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename); |
4d1e00a8 ACM |
1131 | goto out_close; |
1132 | } | |
1133 | ||
e57cfcda PE |
1134 | ek = elf_kind(elf); |
1135 | if (ek != ELF_K_ELF) | |
1136 | goto out_elf_end; | |
1137 | ||
4d1e00a8 | 1138 | if (gelf_getehdr(elf, &ehdr) == NULL) { |
6beba7ad | 1139 | pr_err("%s: cannot get elf header.\n", __func__); |
4d1e00a8 ACM |
1140 | goto out_elf_end; |
1141 | } | |
1142 | ||
2643ce11 ACM |
1143 | sec = elf_section_by_name(elf, &ehdr, &shdr, |
1144 | ".note.gnu.build-id", NULL); | |
fd7a346e ACM |
1145 | if (sec == NULL) { |
1146 | sec = elf_section_by_name(elf, &ehdr, &shdr, | |
1147 | ".notes", NULL); | |
1148 | if (sec == NULL) | |
1149 | goto out_elf_end; | |
1150 | } | |
4d1e00a8 | 1151 | |
fd7a346e ACM |
1152 | data = elf_getdata(sec, NULL); |
1153 | if (data == NULL) | |
4d1e00a8 | 1154 | goto out_elf_end; |
fd7a346e ACM |
1155 | |
1156 | ptr = data->d_buf; | |
1157 | while (ptr < (data->d_buf + data->d_size)) { | |
1158 | GElf_Nhdr *nhdr = ptr; | |
1159 | int namesz = NOTE_ALIGN(nhdr->n_namesz), | |
1160 | descsz = NOTE_ALIGN(nhdr->n_descsz); | |
1161 | const char *name; | |
1162 | ||
1163 | ptr += sizeof(*nhdr); | |
1164 | name = ptr; | |
1165 | ptr += namesz; | |
1166 | if (nhdr->n_type == NT_GNU_BUILD_ID && | |
1167 | nhdr->n_namesz == sizeof("GNU")) { | |
1168 | if (memcmp(name, "GNU", sizeof("GNU")) == 0) { | |
1169 | memcpy(bf, ptr, BUILD_ID_SIZE); | |
1170 | err = BUILD_ID_SIZE; | |
1171 | break; | |
1172 | } | |
1173 | } | |
1174 | ptr += descsz; | |
1175 | } | |
2643ce11 ACM |
1176 | out_elf_end: |
1177 | elf_end(elf); | |
1178 | out_close: | |
1179 | close(fd); | |
1180 | out: | |
1181 | return err; | |
1182 | } | |
1183 | ||
f1617b40 ACM |
1184 | int sysfs__read_build_id(const char *filename, void *build_id, size_t size) |
1185 | { | |
1186 | int fd, err = -1; | |
1187 | ||
1188 | if (size < BUILD_ID_SIZE) | |
1189 | goto out; | |
1190 | ||
1191 | fd = open(filename, O_RDONLY); | |
1192 | if (fd < 0) | |
1193 | goto out; | |
1194 | ||
1195 | while (1) { | |
1196 | char bf[BUFSIZ]; | |
1197 | GElf_Nhdr nhdr; | |
1198 | int namesz, descsz; | |
1199 | ||
1200 | if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr)) | |
1201 | break; | |
1202 | ||
fd7a346e ACM |
1203 | namesz = NOTE_ALIGN(nhdr.n_namesz); |
1204 | descsz = NOTE_ALIGN(nhdr.n_descsz); | |
f1617b40 ACM |
1205 | if (nhdr.n_type == NT_GNU_BUILD_ID && |
1206 | nhdr.n_namesz == sizeof("GNU")) { | |
1207 | if (read(fd, bf, namesz) != namesz) | |
1208 | break; | |
1209 | if (memcmp(bf, "GNU", sizeof("GNU")) == 0) { | |
1210 | if (read(fd, build_id, | |
1211 | BUILD_ID_SIZE) == BUILD_ID_SIZE) { | |
1212 | err = 0; | |
1213 | break; | |
1214 | } | |
1215 | } else if (read(fd, bf, descsz) != descsz) | |
1216 | break; | |
1217 | } else { | |
1218 | int n = namesz + descsz; | |
1219 | if (read(fd, bf, n) != n) | |
1220 | break; | |
1221 | } | |
1222 | } | |
1223 | close(fd); | |
1224 | out: | |
1225 | return err; | |
1226 | } | |
1227 | ||
94cb9e38 ACM |
1228 | char dso__symtab_origin(const struct dso *self) |
1229 | { | |
1230 | static const char origin[] = { | |
1231 | [DSO__ORIG_KERNEL] = 'k', | |
1232 | [DSO__ORIG_JAVA_JIT] = 'j', | |
4cf40131 | 1233 | [DSO__ORIG_BUILD_ID_CACHE] = 'B', |
94cb9e38 ACM |
1234 | [DSO__ORIG_FEDORA] = 'f', |
1235 | [DSO__ORIG_UBUNTU] = 'u', | |
1236 | [DSO__ORIG_BUILDID] = 'b', | |
1237 | [DSO__ORIG_DSO] = 'd', | |
439d473b | 1238 | [DSO__ORIG_KMODULE] = 'K', |
94cb9e38 ACM |
1239 | }; |
1240 | ||
1241 | if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND) | |
1242 | return '!'; | |
1243 | return origin[self->origin]; | |
1244 | } | |
1245 | ||
9de89fe7 | 1246 | int dso__load(struct dso *self, struct map *map, symbol_filter_t filter) |
a2928c42 | 1247 | { |
4d1e00a8 | 1248 | int size = PATH_MAX; |
c338aee8 | 1249 | char *name; |
d3379ab9 | 1250 | u8 build_id[BUILD_ID_SIZE]; |
4cf40131 | 1251 | char build_id_hex[BUILD_ID_SIZE * 2 + 1]; |
a2928c42 ACM |
1252 | int ret = -1; |
1253 | int fd; | |
1254 | ||
3610583c | 1255 | dso__set_loaded(self, map->type); |
66bd8424 | 1256 | |
c338aee8 | 1257 | if (self->kernel) |
9de89fe7 | 1258 | return dso__load_kernel_sym(self, map, filter); |
c338aee8 ACM |
1259 | |
1260 | name = malloc(size); | |
a2928c42 ACM |
1261 | if (!name) |
1262 | return -1; | |
1263 | ||
30d7a77d | 1264 | self->adjust_symbols = 0; |
f5812a7a | 1265 | |
94cb9e38 | 1266 | if (strncmp(self->name, "/tmp/perf-", 10) == 0) { |
6beba7ad | 1267 | ret = dso__load_perf_map(self, map, filter); |
94cb9e38 ACM |
1268 | self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT : |
1269 | DSO__ORIG_NOT_FOUND; | |
1270 | return ret; | |
1271 | } | |
1272 | ||
4cf40131 | 1273 | self->origin = DSO__ORIG_BUILD_ID_CACHE; |
80d496be | 1274 | |
4cf40131 ACM |
1275 | if (self->has_build_id) { |
1276 | build_id__sprintf(self->build_id, sizeof(self->build_id), | |
1277 | build_id_hex); | |
1278 | snprintf(name, size, "%s/%s/.build-id/%.2s/%s", | |
1279 | getenv("HOME"), DEBUG_CACHE_DIR, | |
1280 | build_id_hex, build_id_hex + 2); | |
1281 | goto open_file; | |
1282 | } | |
a2928c42 ACM |
1283 | more: |
1284 | do { | |
94cb9e38 ACM |
1285 | self->origin++; |
1286 | switch (self->origin) { | |
1287 | case DSO__ORIG_FEDORA: | |
439d473b ACM |
1288 | snprintf(name, size, "/usr/lib/debug%s.debug", |
1289 | self->long_name); | |
a2928c42 | 1290 | break; |
94cb9e38 | 1291 | case DSO__ORIG_UBUNTU: |
439d473b ACM |
1292 | snprintf(name, size, "/usr/lib/debug%s", |
1293 | self->long_name); | |
a2928c42 | 1294 | break; |
94cb9e38 | 1295 | case DSO__ORIG_BUILDID: |
d3379ab9 ACM |
1296 | if (filename__read_build_id(self->long_name, build_id, |
1297 | sizeof(build_id))) { | |
d3379ab9 ACM |
1298 | build_id__sprintf(build_id, sizeof(build_id), |
1299 | build_id_hex); | |
4d1e00a8 ACM |
1300 | snprintf(name, size, |
1301 | "/usr/lib/debug/.build-id/%.2s/%s.debug", | |
d3379ab9 ACM |
1302 | build_id_hex, build_id_hex + 2); |
1303 | if (self->has_build_id) | |
1304 | goto compare_build_id; | |
1305 | break; | |
4d1e00a8 | 1306 | } |
94cb9e38 | 1307 | self->origin++; |
4d1e00a8 | 1308 | /* Fall thru */ |
94cb9e38 | 1309 | case DSO__ORIG_DSO: |
439d473b | 1310 | snprintf(name, size, "%s", self->long_name); |
a2928c42 ACM |
1311 | break; |
1312 | ||
1313 | default: | |
1314 | goto out; | |
1315 | } | |
a2928c42 | 1316 | |
8d06367f | 1317 | if (self->has_build_id) { |
d3379ab9 ACM |
1318 | if (filename__read_build_id(name, build_id, |
1319 | sizeof(build_id)) < 0) | |
8d06367f | 1320 | goto more; |
8d06367f | 1321 | compare_build_id: |
78075caa | 1322 | if (!dso__build_id_equal(self, build_id)) |
8d06367f ACM |
1323 | goto more; |
1324 | } | |
4cf40131 | 1325 | open_file: |
a2928c42 ACM |
1326 | fd = open(name, O_RDONLY); |
1327 | } while (fd < 0); | |
1328 | ||
9de89fe7 | 1329 | ret = dso__load_sym(self, map, name, fd, filter, 0); |
a2928c42 ACM |
1330 | close(fd); |
1331 | ||
1332 | /* | |
1333 | * Some people seem to have debuginfo files _WITHOUT_ debug info!?!? | |
1334 | */ | |
1335 | if (!ret) | |
1336 | goto more; | |
1337 | ||
a25e46c4 | 1338 | if (ret > 0) { |
82164161 | 1339 | int nr_plt = dso__synthesize_plt_symbols(self, map, filter); |
a25e46c4 ACM |
1340 | if (nr_plt > 0) |
1341 | ret += nr_plt; | |
1342 | } | |
a2928c42 ACM |
1343 | out: |
1344 | free(name); | |
1340e6bb ACM |
1345 | if (ret < 0 && strstr(self->name, " (deleted)") != NULL) |
1346 | return 0; | |
a2928c42 ACM |
1347 | return ret; |
1348 | } | |
1349 | ||
79406cd7 ACM |
1350 | struct map *map_groups__find_by_name(struct map_groups *self, |
1351 | enum map_type type, const char *name) | |
439d473b ACM |
1352 | { |
1353 | struct rb_node *nd; | |
1354 | ||
79406cd7 | 1355 | for (nd = rb_first(&self->maps[type]); nd; nd = rb_next(nd)) { |
439d473b ACM |
1356 | struct map *map = rb_entry(nd, struct map, rb_node); |
1357 | ||
b7cece76 | 1358 | if (map->dso && strcmp(map->dso->short_name, name) == 0) |
439d473b ACM |
1359 | return map; |
1360 | } | |
1361 | ||
1362 | return NULL; | |
1363 | } | |
1364 | ||
b7cece76 ACM |
1365 | static int dso__kernel_module_get_build_id(struct dso *self) |
1366 | { | |
1367 | char filename[PATH_MAX]; | |
1368 | /* | |
1369 | * kernel module short names are of the form "[module]" and | |
1370 | * we need just "module" here. | |
1371 | */ | |
1372 | const char *name = self->short_name + 1; | |
1373 | ||
1374 | snprintf(filename, sizeof(filename), | |
1375 | "/sys/module/%.*s/notes/.note.gnu.build-id", | |
1376 | (int)strlen(name - 1), name); | |
1377 | ||
1378 | if (sysfs__read_build_id(filename, self->build_id, | |
1379 | sizeof(self->build_id)) == 0) | |
1380 | self->has_build_id = true; | |
1381 | ||
1382 | return 0; | |
1383 | } | |
1384 | ||
9de89fe7 | 1385 | static int map_groups__set_modules_path_dir(struct map_groups *self, char *dirname) |
6cfcc53e | 1386 | { |
439d473b | 1387 | struct dirent *dent; |
439d473b | 1388 | DIR *dir = opendir(dirname); |
6cfcc53e | 1389 | |
439d473b | 1390 | if (!dir) { |
87f8ea4c | 1391 | pr_debug("%s: cannot open %s dir\n", __func__, dirname); |
439d473b ACM |
1392 | return -1; |
1393 | } | |
6cfcc53e | 1394 | |
439d473b ACM |
1395 | while ((dent = readdir(dir)) != NULL) { |
1396 | char path[PATH_MAX]; | |
1397 | ||
1398 | if (dent->d_type == DT_DIR) { | |
1399 | if (!strcmp(dent->d_name, ".") || | |
1400 | !strcmp(dent->d_name, "..")) | |
1401 | continue; | |
1402 | ||
1403 | snprintf(path, sizeof(path), "%s/%s", | |
1404 | dirname, dent->d_name); | |
9de89fe7 | 1405 | if (map_groups__set_modules_path_dir(self, path) < 0) |
439d473b ACM |
1406 | goto failure; |
1407 | } else { | |
1408 | char *dot = strrchr(dent->d_name, '.'), | |
1409 | dso_name[PATH_MAX]; | |
1410 | struct map *map; | |
cfc10d3b | 1411 | char *long_name; |
439d473b ACM |
1412 | |
1413 | if (dot == NULL || strcmp(dot, ".ko")) | |
1414 | continue; | |
1415 | snprintf(dso_name, sizeof(dso_name), "[%.*s]", | |
1416 | (int)(dot - dent->d_name), dent->d_name); | |
1417 | ||
a2a99e8e | 1418 | strxfrchar(dso_name, '-', '_'); |
9de89fe7 | 1419 | map = map_groups__find_by_name(self, MAP__FUNCTION, dso_name); |
439d473b ACM |
1420 | if (map == NULL) |
1421 | continue; | |
1422 | ||
1423 | snprintf(path, sizeof(path), "%s/%s", | |
1424 | dirname, dent->d_name); | |
1425 | ||
cfc10d3b ACM |
1426 | long_name = strdup(path); |
1427 | if (long_name == NULL) | |
439d473b | 1428 | goto failure; |
cfc10d3b | 1429 | dso__set_long_name(map->dso, long_name); |
b7cece76 | 1430 | dso__kernel_module_get_build_id(map->dso); |
439d473b | 1431 | } |
439d473b | 1432 | } |
6cfcc53e | 1433 | |
c338aee8 | 1434 | return 0; |
439d473b ACM |
1435 | failure: |
1436 | closedir(dir); | |
1437 | return -1; | |
1438 | } | |
6cfcc53e | 1439 | |
9de89fe7 | 1440 | static int map_groups__set_modules_path(struct map_groups *self) |
439d473b ACM |
1441 | { |
1442 | struct utsname uts; | |
1443 | char modules_path[PATH_MAX]; | |
6cfcc53e | 1444 | |
439d473b ACM |
1445 | if (uname(&uts) < 0) |
1446 | return -1; | |
6cfcc53e | 1447 | |
439d473b ACM |
1448 | snprintf(modules_path, sizeof(modules_path), "/lib/modules/%s/kernel", |
1449 | uts.release); | |
6cfcc53e | 1450 | |
9de89fe7 | 1451 | return map_groups__set_modules_path_dir(self, modules_path); |
6cfcc53e MG |
1452 | } |
1453 | ||
439d473b ACM |
1454 | /* |
1455 | * Constructor variant for modules (where we know from /proc/modules where | |
1456 | * they are loaded) and for vmlinux, where only after we load all the | |
1457 | * symbols we'll know where it starts and ends. | |
1458 | */ | |
3610583c | 1459 | static struct map *map__new2(u64 start, struct dso *dso, enum map_type type) |
6cfcc53e | 1460 | { |
9de89fe7 ACM |
1461 | struct map *self = zalloc(sizeof(*self) + |
1462 | (dso->kernel ? sizeof(struct kmap) : 0)); | |
439d473b | 1463 | if (self != NULL) { |
439d473b | 1464 | /* |
afb7b4f0 | 1465 | * ->end will be filled after we load all the symbols |
439d473b | 1466 | */ |
3610583c | 1467 | map__init(self, type, start, 0, 0, dso); |
439d473b | 1468 | } |
afb7b4f0 | 1469 | |
439d473b ACM |
1470 | return self; |
1471 | } | |
1472 | ||
9de89fe7 ACM |
1473 | struct map *map_groups__new_module(struct map_groups *self, u64 start, |
1474 | const char *filename) | |
b7cece76 ACM |
1475 | { |
1476 | struct map *map; | |
1477 | struct dso *dso = __dsos__findnew(&dsos__kernel, filename); | |
1478 | ||
1479 | if (dso == NULL) | |
1480 | return NULL; | |
1481 | ||
1482 | map = map__new2(start, dso, MAP__FUNCTION); | |
1483 | if (map == NULL) | |
1484 | return NULL; | |
1485 | ||
1486 | dso->origin = DSO__ORIG_KMODULE; | |
9de89fe7 | 1487 | map_groups__insert(self, map); |
b7cece76 ACM |
1488 | return map; |
1489 | } | |
1490 | ||
9de89fe7 | 1491 | static int map_groups__create_modules(struct map_groups *self) |
439d473b ACM |
1492 | { |
1493 | char *line = NULL; | |
1494 | size_t n; | |
1495 | FILE *file = fopen("/proc/modules", "r"); | |
1496 | struct map *map; | |
6cfcc53e | 1497 | |
439d473b ACM |
1498 | if (file == NULL) |
1499 | return -1; | |
6cfcc53e | 1500 | |
439d473b ACM |
1501 | while (!feof(file)) { |
1502 | char name[PATH_MAX]; | |
1503 | u64 start; | |
439d473b ACM |
1504 | char *sep; |
1505 | int line_len; | |
6cfcc53e | 1506 | |
439d473b ACM |
1507 | line_len = getline(&line, &n, file); |
1508 | if (line_len < 0) | |
1509 | break; | |
1510 | ||
1511 | if (!line) | |
1512 | goto out_failure; | |
1513 | ||
1514 | line[--line_len] = '\0'; /* \n */ | |
1515 | ||
1516 | sep = strrchr(line, 'x'); | |
1517 | if (sep == NULL) | |
1518 | continue; | |
1519 | ||
1520 | hex2u64(sep + 1, &start); | |
1521 | ||
1522 | sep = strchr(line, ' '); | |
1523 | if (sep == NULL) | |
1524 | continue; | |
1525 | ||
1526 | *sep = '\0'; | |
1527 | ||
1528 | snprintf(name, sizeof(name), "[%s]", line); | |
9de89fe7 | 1529 | map = map_groups__new_module(self, start, name); |
b7cece76 | 1530 | if (map == NULL) |
439d473b | 1531 | goto out_delete_line; |
b7cece76 | 1532 | dso__kernel_module_get_build_id(map->dso); |
6cfcc53e | 1533 | } |
439d473b ACM |
1534 | |
1535 | free(line); | |
1536 | fclose(file); | |
1537 | ||
9de89fe7 | 1538 | return map_groups__set_modules_path(self); |
439d473b ACM |
1539 | |
1540 | out_delete_line: | |
1541 | free(line); | |
1542 | out_failure: | |
1543 | return -1; | |
6cfcc53e MG |
1544 | } |
1545 | ||
9958e1f0 | 1546 | static int dso__load_vmlinux(struct dso *self, struct map *map, |
6beba7ad | 1547 | const char *vmlinux, symbol_filter_t filter) |
a2928c42 | 1548 | { |
fbd733b8 | 1549 | int err = -1, fd; |
a2928c42 | 1550 | |
fbd733b8 ACM |
1551 | if (self->has_build_id) { |
1552 | u8 build_id[BUILD_ID_SIZE]; | |
1553 | ||
1554 | if (filename__read_build_id(vmlinux, build_id, | |
1555 | sizeof(build_id)) < 0) { | |
1556 | pr_debug("No build_id in %s, ignoring it\n", vmlinux); | |
1557 | return -1; | |
1558 | } | |
1559 | if (!dso__build_id_equal(self, build_id)) { | |
1560 | char expected_build_id[BUILD_ID_SIZE * 2 + 1], | |
1561 | vmlinux_build_id[BUILD_ID_SIZE * 2 + 1]; | |
1562 | ||
1563 | build_id__sprintf(self->build_id, | |
1564 | sizeof(self->build_id), | |
1565 | expected_build_id); | |
1566 | build_id__sprintf(build_id, sizeof(build_id), | |
1567 | vmlinux_build_id); | |
1568 | pr_debug("build_id in %s is %s while expected is %s, " | |
1569 | "ignoring it\n", vmlinux, vmlinux_build_id, | |
1570 | expected_build_id); | |
1571 | return -1; | |
1572 | } | |
1573 | } | |
66bd8424 | 1574 | |
fbd733b8 | 1575 | fd = open(vmlinux, O_RDONLY); |
a2928c42 ACM |
1576 | if (fd < 0) |
1577 | return -1; | |
1578 | ||
3610583c | 1579 | dso__set_loaded(self, map->type); |
9de89fe7 | 1580 | err = dso__load_sym(self, map, vmlinux, fd, filter, 0); |
a2928c42 ACM |
1581 | close(fd); |
1582 | ||
3846df2e ACM |
1583 | if (err > 0) |
1584 | pr_debug("Using %s for symbols\n", vmlinux); | |
1585 | ||
a2928c42 ACM |
1586 | return err; |
1587 | } | |
1588 | ||
a19afe46 | 1589 | int dso__load_vmlinux_path(struct dso *self, struct map *map, |
9de89fe7 | 1590 | symbol_filter_t filter) |
a19afe46 ACM |
1591 | { |
1592 | int i, err = 0; | |
1593 | ||
1594 | pr_debug("Looking at the vmlinux_path (%d entries long)\n", | |
1595 | vmlinux_path__nr_entries); | |
1596 | ||
1597 | for (i = 0; i < vmlinux_path__nr_entries; ++i) { | |
9de89fe7 | 1598 | err = dso__load_vmlinux(self, map, vmlinux_path[i], filter); |
a19afe46 | 1599 | if (err > 0) { |
a19afe46 ACM |
1600 | dso__set_long_name(self, strdup(vmlinux_path[i])); |
1601 | break; | |
1602 | } | |
1603 | } | |
1604 | ||
1605 | return err; | |
1606 | } | |
1607 | ||
c338aee8 | 1608 | static int dso__load_kernel_sym(struct dso *self, struct map *map, |
9de89fe7 | 1609 | symbol_filter_t filter) |
a827c875 | 1610 | { |
cc612d81 | 1611 | int err; |
9e201442 ACM |
1612 | const char *kallsyms_filename = NULL; |
1613 | char *kallsyms_allocated_filename = NULL; | |
dc8d6ab2 ACM |
1614 | /* |
1615 | * Step 1: if the user specified a vmlinux filename, use it and only | |
1616 | * it, reporting errors to the user if it cannot be used. | |
1617 | * | |
1618 | * For instance, try to analyse an ARM perf.data file _without_ a | |
1619 | * build-id, or if the user specifies the wrong path to the right | |
1620 | * vmlinux file, obviously we can't fallback to another vmlinux (a | |
1621 | * x86_86 one, on the machine where analysis is being performed, say), | |
1622 | * or worse, /proc/kallsyms. | |
1623 | * | |
1624 | * If the specified file _has_ a build-id and there is a build-id | |
1625 | * section in the perf.data file, we will still do the expected | |
1626 | * validation in dso__load_vmlinux and will bail out if they don't | |
1627 | * match. | |
1628 | */ | |
1629 | if (symbol_conf.vmlinux_name != NULL) { | |
9de89fe7 | 1630 | err = dso__load_vmlinux(self, map, |
dc8d6ab2 ACM |
1631 | symbol_conf.vmlinux_name, filter); |
1632 | goto out_try_fixup; | |
1633 | } | |
cc612d81 ACM |
1634 | |
1635 | if (vmlinux_path != NULL) { | |
9de89fe7 | 1636 | err = dso__load_vmlinux_path(self, map, filter); |
a19afe46 ACM |
1637 | if (err > 0) |
1638 | goto out_fixup; | |
cc612d81 ACM |
1639 | } |
1640 | ||
b7cece76 ACM |
1641 | /* |
1642 | * Say the kernel DSO was created when processing the build-id header table, | |
1643 | * we have a build-id, so check if it is the same as the running kernel, | |
1644 | * using it if it is. | |
1645 | */ | |
1646 | if (self->has_build_id) { | |
1647 | u8 kallsyms_build_id[BUILD_ID_SIZE]; | |
9e201442 | 1648 | char sbuild_id[BUILD_ID_SIZE * 2 + 1]; |
b7cece76 ACM |
1649 | |
1650 | if (sysfs__read_build_id("/sys/kernel/notes", kallsyms_build_id, | |
8d0591f6 | 1651 | sizeof(kallsyms_build_id)) == 0) { |
9e201442 ACM |
1652 | if (dso__build_id_equal(self, kallsyms_build_id)) { |
1653 | kallsyms_filename = "/proc/kallsyms"; | |
8d0591f6 | 1654 | goto do_kallsyms; |
9e201442 | 1655 | } |
8d0591f6 | 1656 | } |
dc8d6ab2 ACM |
1657 | /* |
1658 | * Now look if we have it on the build-id cache in | |
1659 | * $HOME/.debug/[kernel.kallsyms]. | |
1660 | */ | |
9e201442 ACM |
1661 | build_id__sprintf(self->build_id, sizeof(self->build_id), |
1662 | sbuild_id); | |
1663 | ||
1664 | if (asprintf(&kallsyms_allocated_filename, | |
1665 | "%s/.debug/[kernel.kallsyms]/%s", | |
3846df2e ACM |
1666 | getenv("HOME"), sbuild_id) == -1) { |
1667 | pr_err("Not enough memory for kallsyms file lookup\n"); | |
dc8d6ab2 | 1668 | return -1; |
3846df2e | 1669 | } |
dc8d6ab2 | 1670 | |
19fc2ded ACM |
1671 | kallsyms_filename = kallsyms_allocated_filename; |
1672 | ||
dc8d6ab2 | 1673 | if (access(kallsyms_filename, F_OK)) { |
3846df2e ACM |
1674 | pr_err("No kallsyms or vmlinux with build-id %s " |
1675 | "was found\n", sbuild_id); | |
9e201442 | 1676 | free(kallsyms_allocated_filename); |
dc8d6ab2 | 1677 | return -1; |
9e201442 | 1678 | } |
dc8d6ab2 ACM |
1679 | } else { |
1680 | /* | |
1681 | * Last resort, if we don't have a build-id and couldn't find | |
1682 | * any vmlinux file, try the running kernel kallsyms table. | |
1683 | */ | |
9e201442 | 1684 | kallsyms_filename = "/proc/kallsyms"; |
9e201442 | 1685 | } |
439d473b | 1686 | |
cc612d81 | 1687 | do_kallsyms: |
9de89fe7 | 1688 | err = dso__load_kallsyms(self, kallsyms_filename, map, filter); |
3846df2e ACM |
1689 | if (err > 0) |
1690 | pr_debug("Using %s for symbols\n", kallsyms_filename); | |
dc8d6ab2 | 1691 | free(kallsyms_allocated_filename); |
439d473b | 1692 | |
dc8d6ab2 | 1693 | out_try_fixup: |
439d473b | 1694 | if (err > 0) { |
cc612d81 | 1695 | out_fixup: |
e1c7c6a4 | 1696 | if (kallsyms_filename != NULL) |
dc8d6ab2 | 1697 | dso__set_long_name(self, strdup("[kernel.kallsyms]")); |
6a4694a4 ACM |
1698 | map__fixup_start(map); |
1699 | map__fixup_end(map); | |
439d473b | 1700 | } |
94cb9e38 | 1701 | |
a827c875 ACM |
1702 | return err; |
1703 | } | |
1704 | ||
b0da954a ACM |
1705 | LIST_HEAD(dsos__user); |
1706 | LIST_HEAD(dsos__kernel); | |
cd84c2ac | 1707 | |
b0da954a | 1708 | static void dsos__add(struct list_head *head, struct dso *dso) |
cd84c2ac | 1709 | { |
b0da954a | 1710 | list_add_tail(&dso->node, head); |
cd84c2ac FW |
1711 | } |
1712 | ||
b0da954a | 1713 | static struct dso *dsos__find(struct list_head *head, const char *name) |
cd84c2ac FW |
1714 | { |
1715 | struct dso *pos; | |
1716 | ||
b0da954a | 1717 | list_for_each_entry(pos, head, node) |
cf4e5b08 | 1718 | if (strcmp(pos->long_name, name) == 0) |
cd84c2ac FW |
1719 | return pos; |
1720 | return NULL; | |
1721 | } | |
1722 | ||
a89e5abe | 1723 | struct dso *__dsos__findnew(struct list_head *head, const char *name) |
cd84c2ac | 1724 | { |
a89e5abe | 1725 | struct dso *dso = dsos__find(head, name); |
cd84c2ac | 1726 | |
e4204992 | 1727 | if (!dso) { |
00a192b3 | 1728 | dso = dso__new(name); |
cfc10d3b | 1729 | if (dso != NULL) { |
a89e5abe | 1730 | dsos__add(head, dso); |
cfc10d3b ACM |
1731 | dso__set_basename(dso); |
1732 | } | |
66bd8424 | 1733 | } |
cd84c2ac FW |
1734 | |
1735 | return dso; | |
cd84c2ac FW |
1736 | } |
1737 | ||
b0da954a | 1738 | static void __dsos__fprintf(struct list_head *head, FILE *fp) |
cd84c2ac FW |
1739 | { |
1740 | struct dso *pos; | |
1741 | ||
95011c60 ACM |
1742 | list_for_each_entry(pos, head, node) { |
1743 | int i; | |
1744 | for (i = 0; i < MAP__NR_TYPES; ++i) | |
1745 | dso__fprintf(pos, i, fp); | |
1746 | } | |
cd84c2ac FW |
1747 | } |
1748 | ||
b0da954a ACM |
1749 | void dsos__fprintf(FILE *fp) |
1750 | { | |
1751 | __dsos__fprintf(&dsos__kernel, fp); | |
1752 | __dsos__fprintf(&dsos__user, fp); | |
1753 | } | |
1754 | ||
88d3d9b7 ACM |
1755 | static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp, |
1756 | bool with_hits) | |
9e03eb2d ACM |
1757 | { |
1758 | struct dso *pos; | |
1759 | size_t ret = 0; | |
1760 | ||
b0da954a | 1761 | list_for_each_entry(pos, head, node) { |
88d3d9b7 ACM |
1762 | if (with_hits && !pos->hit) |
1763 | continue; | |
9e03eb2d | 1764 | ret += dso__fprintf_buildid(pos, fp); |
1124ba73 | 1765 | ret += fprintf(fp, " %s\n", pos->long_name); |
9e03eb2d ACM |
1766 | } |
1767 | return ret; | |
1768 | } | |
1769 | ||
88d3d9b7 | 1770 | size_t dsos__fprintf_buildid(FILE *fp, bool with_hits) |
b0da954a | 1771 | { |
88d3d9b7 ACM |
1772 | return (__dsos__fprintf_buildid(&dsos__kernel, fp, with_hits) + |
1773 | __dsos__fprintf_buildid(&dsos__user, fp, with_hits)); | |
b0da954a ACM |
1774 | } |
1775 | ||
fd1d908c ACM |
1776 | struct dso *dso__new_kernel(const char *name) |
1777 | { | |
1778 | struct dso *self = dso__new(name ?: "[kernel.kallsyms]"); | |
1779 | ||
1780 | if (self != NULL) { | |
b63be8d7 | 1781 | dso__set_short_name(self, "[kernel]"); |
fd1d908c ACM |
1782 | self->kernel = 1; |
1783 | } | |
1784 | ||
1785 | return self; | |
1786 | } | |
1787 | ||
1788 | void dso__read_running_kernel_build_id(struct dso *self) | |
1789 | { | |
1790 | if (sysfs__read_build_id("/sys/kernel/notes", self->build_id, | |
1791 | sizeof(self->build_id)) == 0) | |
1792 | self->has_build_id = true; | |
1793 | } | |
1794 | ||
de176489 | 1795 | static struct dso *dsos__create_kernel(const char *vmlinux) |
cd84c2ac | 1796 | { |
fd1d908c | 1797 | struct dso *kernel = dso__new_kernel(vmlinux); |
cd84c2ac | 1798 | |
8d92c02a ACM |
1799 | if (kernel != NULL) { |
1800 | dso__read_running_kernel_build_id(kernel); | |
1801 | dsos__add(&dsos__kernel, kernel); | |
1802 | } | |
cd84c2ac | 1803 | |
f1dfa0b1 | 1804 | return kernel; |
f1dfa0b1 ACM |
1805 | } |
1806 | ||
b7cece76 ACM |
1807 | int __map_groups__create_kernel_maps(struct map_groups *self, |
1808 | struct map *vmlinux_maps[MAP__NR_TYPES], | |
1809 | struct dso *kernel) | |
f1dfa0b1 | 1810 | { |
de176489 | 1811 | enum map_type type; |
f1dfa0b1 | 1812 | |
de176489 | 1813 | for (type = 0; type < MAP__NR_TYPES; ++type) { |
9de89fe7 ACM |
1814 | struct kmap *kmap; |
1815 | ||
de176489 ACM |
1816 | vmlinux_maps[type] = map__new2(0, kernel, type); |
1817 | if (vmlinux_maps[type] == NULL) | |
1818 | return -1; | |
f1dfa0b1 | 1819 | |
de176489 ACM |
1820 | vmlinux_maps[type]->map_ip = |
1821 | vmlinux_maps[type]->unmap_ip = identity__map_ip; | |
9de89fe7 ACM |
1822 | |
1823 | kmap = map__kmap(vmlinux_maps[type]); | |
1824 | kmap->kmaps = self; | |
de176489 | 1825 | map_groups__insert(self, vmlinux_maps[type]); |
f1dfa0b1 ACM |
1826 | } |
1827 | ||
f1dfa0b1 | 1828 | return 0; |
2446042c ACM |
1829 | } |
1830 | ||
cc612d81 ACM |
1831 | static void vmlinux_path__exit(void) |
1832 | { | |
1833 | while (--vmlinux_path__nr_entries >= 0) { | |
1834 | free(vmlinux_path[vmlinux_path__nr_entries]); | |
1835 | vmlinux_path[vmlinux_path__nr_entries] = NULL; | |
1836 | } | |
1837 | ||
1838 | free(vmlinux_path); | |
1839 | vmlinux_path = NULL; | |
1840 | } | |
1841 | ||
1842 | static int vmlinux_path__init(void) | |
1843 | { | |
1844 | struct utsname uts; | |
1845 | char bf[PATH_MAX]; | |
1846 | ||
1847 | if (uname(&uts) < 0) | |
1848 | return -1; | |
1849 | ||
1850 | vmlinux_path = malloc(sizeof(char *) * 5); | |
1851 | if (vmlinux_path == NULL) | |
1852 | return -1; | |
1853 | ||
1854 | vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux"); | |
1855 | if (vmlinux_path[vmlinux_path__nr_entries] == NULL) | |
1856 | goto out_fail; | |
1857 | ++vmlinux_path__nr_entries; | |
1858 | vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux"); | |
1859 | if (vmlinux_path[vmlinux_path__nr_entries] == NULL) | |
1860 | goto out_fail; | |
1861 | ++vmlinux_path__nr_entries; | |
1862 | snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release); | |
1863 | vmlinux_path[vmlinux_path__nr_entries] = strdup(bf); | |
1864 | if (vmlinux_path[vmlinux_path__nr_entries] == NULL) | |
1865 | goto out_fail; | |
1866 | ++vmlinux_path__nr_entries; | |
1867 | snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release); | |
1868 | vmlinux_path[vmlinux_path__nr_entries] = strdup(bf); | |
1869 | if (vmlinux_path[vmlinux_path__nr_entries] == NULL) | |
1870 | goto out_fail; | |
1871 | ++vmlinux_path__nr_entries; | |
1872 | snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux", | |
1873 | uts.release); | |
1874 | vmlinux_path[vmlinux_path__nr_entries] = strdup(bf); | |
1875 | if (vmlinux_path[vmlinux_path__nr_entries] == NULL) | |
1876 | goto out_fail; | |
1877 | ++vmlinux_path__nr_entries; | |
1878 | ||
1879 | return 0; | |
1880 | ||
1881 | out_fail: | |
1882 | vmlinux_path__exit(); | |
1883 | return -1; | |
1884 | } | |
1885 | ||
b0a9ab62 ACM |
1886 | size_t vmlinux_path__fprintf(FILE *fp) |
1887 | { | |
1888 | int i; | |
1889 | size_t printed = 0; | |
1890 | ||
1891 | for (i = 0; i < vmlinux_path__nr_entries; ++i) | |
1892 | printed += fprintf(fp, "[%d] %s\n", i, vmlinux_path[i]); | |
1893 | ||
1894 | return printed; | |
1895 | } | |
1896 | ||
655000e7 ACM |
1897 | static int setup_list(struct strlist **list, const char *list_str, |
1898 | const char *list_name) | |
1899 | { | |
1900 | if (list_str == NULL) | |
1901 | return 0; | |
1902 | ||
1903 | *list = strlist__new(true, list_str); | |
1904 | if (!*list) { | |
1905 | pr_err("problems parsing %s list\n", list_name); | |
1906 | return -1; | |
1907 | } | |
1908 | return 0; | |
1909 | } | |
1910 | ||
75be6cf4 | 1911 | int symbol__init(void) |
2446042c | 1912 | { |
95011c60 | 1913 | elf_version(EV_CURRENT); |
75be6cf4 ACM |
1914 | if (symbol_conf.sort_by_name) |
1915 | symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) - | |
1916 | sizeof(struct symbol)); | |
b32d133a | 1917 | |
75be6cf4 | 1918 | if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0) |
2446042c ACM |
1919 | return -1; |
1920 | ||
c410a338 ACM |
1921 | if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') { |
1922 | pr_err("'.' is the only non valid --field-separator argument\n"); | |
1923 | return -1; | |
1924 | } | |
1925 | ||
655000e7 ACM |
1926 | if (setup_list(&symbol_conf.dso_list, |
1927 | symbol_conf.dso_list_str, "dso") < 0) | |
1928 | return -1; | |
1929 | ||
1930 | if (setup_list(&symbol_conf.comm_list, | |
1931 | symbol_conf.comm_list_str, "comm") < 0) | |
1932 | goto out_free_dso_list; | |
1933 | ||
1934 | if (setup_list(&symbol_conf.sym_list, | |
1935 | symbol_conf.sym_list_str, "symbol") < 0) | |
1936 | goto out_free_comm_list; | |
1937 | ||
4aa65636 | 1938 | return 0; |
655000e7 ACM |
1939 | |
1940 | out_free_dso_list: | |
1941 | strlist__delete(symbol_conf.dso_list); | |
1942 | out_free_comm_list: | |
1943 | strlist__delete(symbol_conf.comm_list); | |
1944 | return -1; | |
4aa65636 ACM |
1945 | } |
1946 | ||
9de89fe7 ACM |
1947 | int map_groups__create_kernel_maps(struct map_groups *self, |
1948 | struct map *vmlinux_maps[MAP__NR_TYPES]) | |
4aa65636 | 1949 | { |
9de89fe7 ACM |
1950 | struct dso *kernel = dsos__create_kernel(symbol_conf.vmlinux_name); |
1951 | ||
1952 | if (kernel == NULL) | |
1953 | return -1; | |
1954 | ||
1955 | if (__map_groups__create_kernel_maps(self, vmlinux_maps, kernel) < 0) | |
cc612d81 | 1956 | return -1; |
cc612d81 | 1957 | |
9de89fe7 | 1958 | if (symbol_conf.use_modules && map_groups__create_modules(self) < 0) |
10fe12ef | 1959 | pr_debug("Problems creating module maps, continuing anyway...\n"); |
90c83218 ACM |
1960 | /* |
1961 | * Now that we have all the maps created, just set the ->end of them: | |
1962 | */ | |
9de89fe7 | 1963 | map_groups__fixup_end(self); |
6671cb16 | 1964 | return 0; |
cd84c2ac | 1965 | } |