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