aboutsummaryrefslogtreecommitdiffstats
path: root/tools/perf/util/symbol.c
diff options
context:
space:
mode:
Diffstat (limited to 'tools/perf/util/symbol.c')
-rw-r--r--tools/perf/util/symbol.c785
1 files changed, 553 insertions, 232 deletions
diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c
index 47ea0609a760..8f0208ce237a 100644
--- a/tools/perf/util/symbol.c
+++ b/tools/perf/util/symbol.c
@@ -2,14 +2,14 @@
2#include "../perf.h" 2#include "../perf.h"
3#include "string.h" 3#include "string.h"
4#include "symbol.h" 4#include "symbol.h"
5#include "thread.h"
5 6
6#include "debug.h" 7#include "debug.h"
7 8
8#include <libelf.h> 9#include <libelf.h>
9#include <gelf.h> 10#include <gelf.h>
10#include <elf.h> 11#include <elf.h>
11 12#include <sys/utsname.h>
12const char *sym_hist_filter;
13 13
14enum dso_origin { 14enum dso_origin {
15 DSO__ORIG_KERNEL = 0, 15 DSO__ORIG_KERNEL = 0,
@@ -18,12 +18,65 @@ enum dso_origin {
18 DSO__ORIG_UBUNTU, 18 DSO__ORIG_UBUNTU,
19 DSO__ORIG_BUILDID, 19 DSO__ORIG_BUILDID,
20 DSO__ORIG_DSO, 20 DSO__ORIG_DSO,
21 DSO__ORIG_KMODULE,
21 DSO__ORIG_NOT_FOUND, 22 DSO__ORIG_NOT_FOUND,
22}; 23};
23 24
24static struct symbol *symbol__new(u64 start, u64 len, 25static void dsos__add(struct dso *dso);
25 const char *name, unsigned int priv_size, 26static struct dso *dsos__find(const char *name);
26 u64 obj_start, int v) 27static struct map *map__new2(u64 start, struct dso *dso);
28static void kernel_maps__insert(struct map *map);
29
30static struct rb_root kernel_maps;
31
32static void dso__fixup_sym_end(struct dso *self)
33{
34 struct rb_node *nd, *prevnd = rb_first(&self->syms);
35 struct symbol *curr, *prev;
36
37 if (prevnd == NULL)
38 return;
39
40 curr = rb_entry(prevnd, struct symbol, rb_node);
41
42 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
43 prev = curr;
44 curr = rb_entry(nd, struct symbol, rb_node);
45
46 if (prev->end == prev->start)
47 prev->end = curr->start - 1;
48 }
49
50 /* Last entry */
51 if (curr->end == curr->start)
52 curr->end = roundup(curr->start, 4096);
53}
54
55static void kernel_maps__fixup_end(void)
56{
57 struct map *prev, *curr;
58 struct rb_node *nd, *prevnd = rb_first(&kernel_maps);
59
60 if (prevnd == NULL)
61 return;
62
63 curr = rb_entry(prevnd, struct map, rb_node);
64
65 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
66 prev = curr;
67 curr = rb_entry(nd, struct map, rb_node);
68 prev->end = curr->start - 1;
69 }
70
71 nd = rb_last(&curr->dso->syms);
72 if (nd) {
73 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
74 curr->end = sym->end;
75 }
76}
77
78static struct symbol *symbol__new(u64 start, u64 len, const char *name,
79 unsigned int priv_size)
27{ 80{
28 size_t namelen = strlen(name) + 1; 81 size_t namelen = strlen(name) + 1;
29 struct symbol *self = calloc(1, priv_size + sizeof(*self) + namelen); 82 struct symbol *self = calloc(1, priv_size + sizeof(*self) + namelen);
@@ -31,23 +84,15 @@ static struct symbol *symbol__new(u64 start, u64 len,
31 if (!self) 84 if (!self)
32 return NULL; 85 return NULL;
33 86
34 if (v >= 2)
35 printf("new symbol: %016Lx [%08lx]: %s, hist: %p, obj_start: %p\n",
36 (u64)start, (unsigned long)len, name, self->hist, (void *)(unsigned long)obj_start);
37
38 self->obj_start= obj_start;
39 self->hist = NULL;
40 self->hist_sum = 0;
41
42 if (sym_hist_filter && !strcmp(name, sym_hist_filter))
43 self->hist = calloc(sizeof(u64), len);
44
45 if (priv_size) { 87 if (priv_size) {
46 memset(self, 0, priv_size); 88 memset(self, 0, priv_size);
47 self = ((void *)self) + priv_size; 89 self = ((void *)self) + priv_size;
48 } 90 }
49 self->start = start; 91 self->start = start;
50 self->end = len ? start + len - 1 : start; 92 self->end = len ? start + len - 1 : start;
93
94 pr_debug3("%s: %s %#Lx-%#Lx\n", __func__, name, start, self->end);
95
51 memcpy(self->name, name, namelen); 96 memcpy(self->name, name, namelen);
52 97
53 return self; 98 return self;
@@ -60,12 +105,8 @@ static void symbol__delete(struct symbol *self, unsigned int priv_size)
60 105
61static size_t symbol__fprintf(struct symbol *self, FILE *fp) 106static size_t symbol__fprintf(struct symbol *self, FILE *fp)
62{ 107{
63 if (!self->module) 108 return fprintf(fp, " %llx-%llx %s\n",
64 return fprintf(fp, " %llx-%llx %s\n",
65 self->start, self->end, self->name); 109 self->start, self->end, self->name);
66 else
67 return fprintf(fp, " %llx-%llx %s \t[%s]\n",
68 self->start, self->end, self->name, self->module->name);
69} 110}
70 111
71struct dso *dso__new(const char *name, unsigned int sym_priv_size) 112struct dso *dso__new(const char *name, unsigned int sym_priv_size)
@@ -74,6 +115,8 @@ struct dso *dso__new(const char *name, unsigned int sym_priv_size)
74 115
75 if (self != NULL) { 116 if (self != NULL) {
76 strcpy(self->name, name); 117 strcpy(self->name, name);
118 self->long_name = self->name;
119 self->short_name = self->name;
77 self->syms = RB_ROOT; 120 self->syms = RB_ROOT;
78 self->sym_priv_size = sym_priv_size; 121 self->sym_priv_size = sym_priv_size;
79 self->find_symbol = dso__find_symbol; 122 self->find_symbol = dso__find_symbol;
@@ -100,6 +143,8 @@ static void dso__delete_symbols(struct dso *self)
100void dso__delete(struct dso *self) 143void dso__delete(struct dso *self)
101{ 144{
102 dso__delete_symbols(self); 145 dso__delete_symbols(self);
146 if (self->long_name != self->name)
147 free(self->long_name);
103 free(self); 148 free(self);
104} 149}
105 150
@@ -147,7 +192,7 @@ struct symbol *dso__find_symbol(struct dso *self, u64 ip)
147 192
148size_t dso__fprintf(struct dso *self, FILE *fp) 193size_t dso__fprintf(struct dso *self, FILE *fp)
149{ 194{
150 size_t ret = fprintf(fp, "dso: %s\n", self->name); 195 size_t ret = fprintf(fp, "dso: %s\n", self->short_name);
151 196
152 struct rb_node *nd; 197 struct rb_node *nd;
153 for (nd = rb_first(&self->syms); nd; nd = rb_next(nd)) { 198 for (nd = rb_first(&self->syms); nd; nd = rb_next(nd)) {
@@ -158,13 +203,16 @@ size_t dso__fprintf(struct dso *self, FILE *fp)
158 return ret; 203 return ret;
159} 204}
160 205
161static int dso__load_kallsyms(struct dso *self, symbol_filter_t filter, int v) 206/*
207 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
208 * so that we can in the next step set the symbol ->end address and then
209 * call kernel_maps__split_kallsyms.
210 */
211static int kernel_maps__load_all_kallsyms(void)
162{ 212{
163 struct rb_node *nd, *prevnd;
164 char *line = NULL; 213 char *line = NULL;
165 size_t n; 214 size_t n;
166 FILE *file = fopen("/proc/kallsyms", "r"); 215 FILE *file = fopen("/proc/kallsyms", "r");
167 int count = 0;
168 216
169 if (file == NULL) 217 if (file == NULL)
170 goto out_failure; 218 goto out_failure;
@@ -174,6 +222,7 @@ static int dso__load_kallsyms(struct dso *self, symbol_filter_t filter, int v)
174 struct symbol *sym; 222 struct symbol *sym;
175 int line_len, len; 223 int line_len, len;
176 char symbol_type; 224 char symbol_type;
225 char *symbol_name;
177 226
178 line_len = getline(&line, &n, file); 227 line_len = getline(&line, &n, file);
179 if (line_len < 0) 228 if (line_len < 0)
@@ -196,44 +245,24 @@ static int dso__load_kallsyms(struct dso *self, symbol_filter_t filter, int v)
196 */ 245 */
197 if (symbol_type != 'T' && symbol_type != 'W') 246 if (symbol_type != 'T' && symbol_type != 'W')
198 continue; 247 continue;
248
249 symbol_name = line + len + 2;
199 /* 250 /*
200 * Well fix up the end later, when we have all sorted. 251 * Will fix up the end later, when we have all symbols sorted.
201 */ 252 */
202 sym = symbol__new(start, 0xdead, line + len + 2, 253 sym = symbol__new(start, 0, symbol_name,
203 self->sym_priv_size, 0, v); 254 kernel_map->dso->sym_priv_size);
204 255
205 if (sym == NULL) 256 if (sym == NULL)
206 goto out_delete_line; 257 goto out_delete_line;
207 258
208 if (filter && filter(self, sym)) 259 dso__insert_symbol(kernel_map->dso, sym);
209 symbol__delete(sym, self->sym_priv_size);
210 else {
211 dso__insert_symbol(self, sym);
212 count++;
213 }
214 }
215
216 /*
217 * Now that we have all sorted out, just set the ->end of all
218 * symbols
219 */
220 prevnd = rb_first(&self->syms);
221
222 if (prevnd == NULL)
223 goto out_delete_line;
224
225 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
226 struct symbol *prev = rb_entry(prevnd, struct symbol, rb_node),
227 *curr = rb_entry(nd, struct symbol, rb_node);
228
229 prev->end = curr->start - 1;
230 prevnd = nd;
231 } 260 }
232 261
233 free(line); 262 free(line);
234 fclose(file); 263 fclose(file);
235 264
236 return count; 265 return 0;
237 266
238out_delete_line: 267out_delete_line:
239 free(line); 268 free(line);
@@ -241,14 +270,124 @@ out_failure:
241 return -1; 270 return -1;
242} 271}
243 272
244static int dso__load_perf_map(struct dso *self, symbol_filter_t filter, int v) 273/*
274 * Split the symbols into maps, making sure there are no overlaps, i.e. the
275 * kernel range is broken in several maps, named [kernel].N, as we don't have
276 * the original ELF section names vmlinux have.
277 */
278static int kernel_maps__split_kallsyms(symbol_filter_t filter, int use_modules)
279{
280 struct map *map = kernel_map;
281 struct symbol *pos;
282 int count = 0;
283 struct rb_node *next = rb_first(&kernel_map->dso->syms);
284 int kernel_range = 0;
285
286 while (next) {
287 char *module;
288
289 pos = rb_entry(next, struct symbol, rb_node);
290 next = rb_next(&pos->rb_node);
291
292 module = strchr(pos->name, '\t');
293 if (module) {
294 if (!use_modules)
295 goto delete_symbol;
296
297 *module++ = '\0';
298
299 if (strcmp(map->dso->name, module)) {
300 map = kernel_maps__find_by_dso_name(module);
301 if (!map) {
302 pr_err("/proc/{kallsyms,modules} "
303 "inconsistency!\n");
304 return -1;
305 }
306 }
307 /*
308 * So that we look just like we get from .ko files,
309 * i.e. not prelinked, relative to map->start.
310 */
311 pos->start = map->map_ip(map, pos->start);
312 pos->end = map->map_ip(map, pos->end);
313 } else if (map != kernel_map) {
314 char dso_name[PATH_MAX];
315 struct dso *dso;
316
317 snprintf(dso_name, sizeof(dso_name), "[kernel].%d",
318 kernel_range++);
319
320 dso = dso__new(dso_name,
321 kernel_map->dso->sym_priv_size);
322 if (dso == NULL)
323 return -1;
324
325 map = map__new2(pos->start, dso);
326 if (map == NULL) {
327 dso__delete(dso);
328 return -1;
329 }
330
331 map->map_ip = map->unmap_ip = identity__map_ip;
332 kernel_maps__insert(map);
333 ++kernel_range;
334 }
335
336 if (filter && filter(map, pos)) {
337delete_symbol:
338 rb_erase(&pos->rb_node, &kernel_map->dso->syms);
339 symbol__delete(pos, kernel_map->dso->sym_priv_size);
340 } else {
341 if (map != kernel_map) {
342 rb_erase(&pos->rb_node, &kernel_map->dso->syms);
343 dso__insert_symbol(map->dso, pos);
344 }
345 count++;
346 }
347 }
348
349 return count;
350}
351
352
353static int kernel_maps__load_kallsyms(symbol_filter_t filter, int use_modules)
354{
355 if (kernel_maps__load_all_kallsyms())
356 return -1;
357
358 dso__fixup_sym_end(kernel_map->dso);
359
360 return kernel_maps__split_kallsyms(filter, use_modules);
361}
362
363static size_t kernel_maps__fprintf(FILE *fp)
364{
365 size_t printed = fprintf(fp, "Kernel maps:\n");
366 struct rb_node *nd;
367
368 for (nd = rb_first(&kernel_maps); nd; nd = rb_next(nd)) {
369 struct map *pos = rb_entry(nd, struct map, rb_node);
370
371 printed += fprintf(fp, "Map:");
372 printed += map__fprintf(pos, fp);
373 if (verbose > 1) {
374 printed += dso__fprintf(pos->dso, fp);
375 printed += fprintf(fp, "--\n");
376 }
377 }
378
379 return printed + fprintf(fp, "END kernel maps\n");
380}
381
382static int dso__load_perf_map(struct dso *self, struct map *map,
383 symbol_filter_t filter)
245{ 384{
246 char *line = NULL; 385 char *line = NULL;
247 size_t n; 386 size_t n;
248 FILE *file; 387 FILE *file;
249 int nr_syms = 0; 388 int nr_syms = 0;
250 389
251 file = fopen(self->name, "r"); 390 file = fopen(self->long_name, "r");
252 if (file == NULL) 391 if (file == NULL)
253 goto out_failure; 392 goto out_failure;
254 393
@@ -279,12 +418,12 @@ static int dso__load_perf_map(struct dso *self, symbol_filter_t filter, int v)
279 continue; 418 continue;
280 419
281 sym = symbol__new(start, size, line + len, 420 sym = symbol__new(start, size, line + len,
282 self->sym_priv_size, start, v); 421 self->sym_priv_size);
283 422
284 if (sym == NULL) 423 if (sym == NULL)
285 goto out_delete_line; 424 goto out_delete_line;
286 425
287 if (filter && filter(self, sym)) 426 if (filter && filter(map, sym))
288 symbol__delete(sym, self->sym_priv_size); 427 symbol__delete(sym, self->sym_priv_size);
289 else { 428 else {
290 dso__insert_symbol(self, sym); 429 dso__insert_symbol(self, sym);
@@ -393,7 +532,7 @@ static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
393 * And always look at the original dso, not at debuginfo packages, that 532 * And always look at the original dso, not at debuginfo packages, that
394 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS). 533 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
395 */ 534 */
396static int dso__synthesize_plt_symbols(struct dso *self, int v) 535static int dso__synthesize_plt_symbols(struct dso *self)
397{ 536{
398 uint32_t nr_rel_entries, idx; 537 uint32_t nr_rel_entries, idx;
399 GElf_Sym sym; 538 GElf_Sym sym;
@@ -409,7 +548,7 @@ static int dso__synthesize_plt_symbols(struct dso *self, int v)
409 Elf *elf; 548 Elf *elf;
410 int nr = 0, symidx, fd, err = 0; 549 int nr = 0, symidx, fd, err = 0;
411 550
412 fd = open(self->name, O_RDONLY); 551 fd = open(self->long_name, O_RDONLY);
413 if (fd < 0) 552 if (fd < 0)
414 goto out; 553 goto out;
415 554
@@ -477,7 +616,7 @@ static int dso__synthesize_plt_symbols(struct dso *self, int v)
477 "%s@plt", elf_sym__name(&sym, symstrs)); 616 "%s@plt", elf_sym__name(&sym, symstrs));
478 617
479 f = symbol__new(plt_offset, shdr_plt.sh_entsize, 618 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
480 sympltname, self->sym_priv_size, 0, v); 619 sympltname, self->sym_priv_size);
481 if (!f) 620 if (!f)
482 goto out_elf_end; 621 goto out_elf_end;
483 622
@@ -495,7 +634,7 @@ static int dso__synthesize_plt_symbols(struct dso *self, int v)
495 "%s@plt", elf_sym__name(&sym, symstrs)); 634 "%s@plt", elf_sym__name(&sym, symstrs));
496 635
497 f = symbol__new(plt_offset, shdr_plt.sh_entsize, 636 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
498 sympltname, self->sym_priv_size, 0, v); 637 sympltname, self->sym_priv_size);
499 if (!f) 638 if (!f)
500 goto out_elf_end; 639 goto out_elf_end;
501 640
@@ -513,14 +652,18 @@ out_close:
513 if (err == 0) 652 if (err == 0)
514 return nr; 653 return nr;
515out: 654out:
516 fprintf(stderr, "%s: problems reading %s PLT info.\n", 655 pr_warning("%s: problems reading %s PLT info.\n",
517 __func__, self->name); 656 __func__, self->long_name);
518 return 0; 657 return 0;
519} 658}
520 659
521static int dso__load_sym(struct dso *self, int fd, const char *name, 660static int dso__load_sym(struct dso *self, struct map *map, const char *name,
522 symbol_filter_t filter, int v, struct module *mod) 661 int fd, symbol_filter_t filter, int kernel,
662 int kmodule)
523{ 663{
664 struct map *curr_map = map;
665 struct dso *curr_dso = self;
666 size_t dso_name_len = strlen(self->short_name);
524 Elf_Data *symstrs, *secstrs; 667 Elf_Data *symstrs, *secstrs;
525 uint32_t nr_syms; 668 uint32_t nr_syms;
526 int err = -1; 669 int err = -1;
@@ -531,19 +674,16 @@ static int dso__load_sym(struct dso *self, int fd, const char *name,
531 GElf_Sym sym; 674 GElf_Sym sym;
532 Elf_Scn *sec, *sec_strndx; 675 Elf_Scn *sec, *sec_strndx;
533 Elf *elf; 676 Elf *elf;
534 int nr = 0, kernel = !strcmp("[kernel]", self->name); 677 int nr = 0;
535 678
536 elf = elf_begin(fd, ELF_C_READ_MMAP, NULL); 679 elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
537 if (elf == NULL) { 680 if (elf == NULL) {
538 if (v) 681 pr_err("%s: cannot read %s ELF file.\n", __func__, name);
539 fprintf(stderr, "%s: cannot read %s ELF file.\n",
540 __func__, name);
541 goto out_close; 682 goto out_close;
542 } 683 }
543 684
544 if (gelf_getehdr(elf, &ehdr) == NULL) { 685 if (gelf_getehdr(elf, &ehdr) == NULL) {
545 if (v) 686 pr_err("%s: cannot get elf header.\n", __func__);
546 fprintf(stderr, "%s: cannot get elf header.\n", __func__);
547 goto out_elf_end; 687 goto out_elf_end;
548 } 688 }
549 689
@@ -587,9 +727,7 @@ static int dso__load_sym(struct dso *self, int fd, const char *name,
587 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) { 727 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
588 struct symbol *f; 728 struct symbol *f;
589 const char *elf_name; 729 const char *elf_name;
590 char *demangled; 730 char *demangled = NULL;
591 u64 obj_start;
592 struct section *section = NULL;
593 int is_label = elf_sym__is_label(&sym); 731 int is_label = elf_sym__is_label(&sym);
594 const char *section_name; 732 const char *section_name;
595 733
@@ -605,52 +743,85 @@ static int dso__load_sym(struct dso *self, int fd, const char *name,
605 if (is_label && !elf_sec__is_text(&shdr, secstrs)) 743 if (is_label && !elf_sec__is_text(&shdr, secstrs))
606 continue; 744 continue;
607 745
746 elf_name = elf_sym__name(&sym, symstrs);
608 section_name = elf_sec__name(&shdr, secstrs); 747 section_name = elf_sec__name(&shdr, secstrs);
609 obj_start = sym.st_value;
610 748
611 if (self->adjust_symbols) { 749 if (kernel || kmodule) {
612 if (v >= 2) 750 char dso_name[PATH_MAX];
613 printf("adjusting symbol: st_value: %Lx sh_addr: %Lx sh_offset: %Lx\n",
614 (u64)sym.st_value, (u64)shdr.sh_addr, (u64)shdr.sh_offset);
615 751
616 sym.st_value -= shdr.sh_addr - shdr.sh_offset; 752 if (strcmp(section_name,
617 } 753 curr_dso->short_name + dso_name_len) == 0)
754 goto new_symbol;
618 755
619 if (mod) { 756 if (strcmp(section_name, ".text") == 0) {
620 section = mod->sections->find_section(mod->sections, section_name); 757 curr_map = map;
621 if (section) 758 curr_dso = self;
622 sym.st_value += section->vma; 759 goto new_symbol;
623 else {
624 fprintf(stderr, "dso__load_sym() module %s lookup of %s failed\n",
625 mod->name, section_name);
626 goto out_elf_end;
627 } 760 }
761
762 snprintf(dso_name, sizeof(dso_name),
763 "%s%s", self->short_name, section_name);
764
765 curr_map = kernel_maps__find_by_dso_name(dso_name);
766 if (curr_map == NULL) {
767 u64 start = sym.st_value;
768
769 if (kmodule)
770 start += map->start + shdr.sh_offset;
771
772 curr_dso = dso__new(dso_name, self->sym_priv_size);
773 if (curr_dso == NULL)
774 goto out_elf_end;
775 curr_map = map__new2(start, curr_dso);
776 if (curr_map == NULL) {
777 dso__delete(curr_dso);
778 goto out_elf_end;
779 }
780 curr_map->map_ip = identity__map_ip;
781 curr_map->unmap_ip = identity__map_ip;
782 curr_dso->origin = DSO__ORIG_KERNEL;
783 kernel_maps__insert(curr_map);
784 dsos__add(curr_dso);
785 } else
786 curr_dso = curr_map->dso;
787
788 goto new_symbol;
789 }
790
791 if (curr_dso->adjust_symbols) {
792 pr_debug2("adjusting symbol: st_value: %Lx sh_addr: "
793 "%Lx sh_offset: %Lx\n", (u64)sym.st_value,
794 (u64)shdr.sh_addr, (u64)shdr.sh_offset);
795 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
628 } 796 }
629 /* 797 /*
630 * We need to figure out if the object was created from C++ sources 798 * We need to figure out if the object was created from C++ sources
631 * DWARF DW_compile_unit has this, but we don't always have access 799 * DWARF DW_compile_unit has this, but we don't always have access
632 * to it... 800 * to it...
633 */ 801 */
634 elf_name = elf_sym__name(&sym, symstrs);
635 demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI); 802 demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
636 if (demangled != NULL) 803 if (demangled != NULL)
637 elf_name = demangled; 804 elf_name = demangled;
638 805new_symbol:
639 f = symbol__new(sym.st_value, sym.st_size, elf_name, 806 f = symbol__new(sym.st_value, sym.st_size, elf_name,
640 self->sym_priv_size, obj_start, v); 807 curr_dso->sym_priv_size);
641 free(demangled); 808 free(demangled);
642 if (!f) 809 if (!f)
643 goto out_elf_end; 810 goto out_elf_end;
644 811
645 if (filter && filter(self, f)) 812 if (filter && filter(curr_map, f))
646 symbol__delete(f, self->sym_priv_size); 813 symbol__delete(f, curr_dso->sym_priv_size);
647 else { 814 else {
648 f->module = mod; 815 dso__insert_symbol(curr_dso, f);
649 dso__insert_symbol(self, f);
650 nr++; 816 nr++;
651 } 817 }
652 } 818 }
653 819
820 /*
821 * For misannotated, zeroed, ASM function sizes.
822 */
823 if (nr > 0)
824 dso__fixup_sym_end(self);
654 err = nr; 825 err = nr;
655out_elf_end: 826out_elf_end:
656 elf_end(elf); 827 elf_end(elf);
@@ -660,7 +831,7 @@ out_close:
660 831
661#define BUILD_ID_SIZE 128 832#define BUILD_ID_SIZE 128
662 833
663static char *dso__read_build_id(struct dso *self, int v) 834static char *dso__read_build_id(struct dso *self)
664{ 835{
665 int i; 836 int i;
666 GElf_Ehdr ehdr; 837 GElf_Ehdr ehdr;
@@ -670,22 +841,20 @@ static char *dso__read_build_id(struct dso *self, int v)
670 char *build_id = NULL, *bid; 841 char *build_id = NULL, *bid;
671 unsigned char *raw; 842 unsigned char *raw;
672 Elf *elf; 843 Elf *elf;
673 int fd = open(self->name, O_RDONLY); 844 int fd = open(self->long_name, O_RDONLY);
674 845
675 if (fd < 0) 846 if (fd < 0)
676 goto out; 847 goto out;
677 848
678 elf = elf_begin(fd, ELF_C_READ_MMAP, NULL); 849 elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
679 if (elf == NULL) { 850 if (elf == NULL) {
680 if (v) 851 pr_err("%s: cannot read %s ELF file.\n", __func__,
681 fprintf(stderr, "%s: cannot read %s ELF file.\n", 852 self->long_name);
682 __func__, self->name);
683 goto out_close; 853 goto out_close;
684 } 854 }
685 855
686 if (gelf_getehdr(elf, &ehdr) == NULL) { 856 if (gelf_getehdr(elf, &ehdr) == NULL) {
687 if (v) 857 pr_err("%s: cannot get elf header.\n", __func__);
688 fprintf(stderr, "%s: cannot get elf header.\n", __func__);
689 goto out_elf_end; 858 goto out_elf_end;
690 } 859 }
691 860
@@ -707,8 +876,7 @@ static char *dso__read_build_id(struct dso *self, int v)
707 ++raw; 876 ++raw;
708 bid += 2; 877 bid += 2;
709 } 878 }
710 if (v >= 2) 879 pr_debug2("%s(%s): %s\n", __func__, self->long_name, build_id);
711 printf("%s(%s): %s\n", __func__, self->name, build_id);
712out_elf_end: 880out_elf_end:
713 elf_end(elf); 881 elf_end(elf);
714out_close: 882out_close:
@@ -726,6 +894,7 @@ char dso__symtab_origin(const struct dso *self)
726 [DSO__ORIG_UBUNTU] = 'u', 894 [DSO__ORIG_UBUNTU] = 'u',
727 [DSO__ORIG_BUILDID] = 'b', 895 [DSO__ORIG_BUILDID] = 'b',
728 [DSO__ORIG_DSO] = 'd', 896 [DSO__ORIG_DSO] = 'd',
897 [DSO__ORIG_KMODULE] = 'K',
729 }; 898 };
730 899
731 if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND) 900 if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
@@ -733,7 +902,7 @@ char dso__symtab_origin(const struct dso *self)
733 return origin[self->origin]; 902 return origin[self->origin];
734} 903}
735 904
736int dso__load(struct dso *self, symbol_filter_t filter, int v) 905int dso__load(struct dso *self, struct map *map, symbol_filter_t filter)
737{ 906{
738 int size = PATH_MAX; 907 int size = PATH_MAX;
739 char *name = malloc(size), *build_id = NULL; 908 char *name = malloc(size), *build_id = NULL;
@@ -746,7 +915,7 @@ int dso__load(struct dso *self, symbol_filter_t filter, int v)
746 self->adjust_symbols = 0; 915 self->adjust_symbols = 0;
747 916
748 if (strncmp(self->name, "/tmp/perf-", 10) == 0) { 917 if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
749 ret = dso__load_perf_map(self, filter, v); 918 ret = dso__load_perf_map(self, map, filter);
750 self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT : 919 self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
751 DSO__ORIG_NOT_FOUND; 920 DSO__ORIG_NOT_FOUND;
752 return ret; 921 return ret;
@@ -759,13 +928,15 @@ more:
759 self->origin++; 928 self->origin++;
760 switch (self->origin) { 929 switch (self->origin) {
761 case DSO__ORIG_FEDORA: 930 case DSO__ORIG_FEDORA:
762 snprintf(name, size, "/usr/lib/debug%s.debug", self->name); 931 snprintf(name, size, "/usr/lib/debug%s.debug",
932 self->long_name);
763 break; 933 break;
764 case DSO__ORIG_UBUNTU: 934 case DSO__ORIG_UBUNTU:
765 snprintf(name, size, "/usr/lib/debug%s", self->name); 935 snprintf(name, size, "/usr/lib/debug%s",
936 self->long_name);
766 break; 937 break;
767 case DSO__ORIG_BUILDID: 938 case DSO__ORIG_BUILDID:
768 build_id = dso__read_build_id(self, v); 939 build_id = dso__read_build_id(self);
769 if (build_id != NULL) { 940 if (build_id != NULL) {
770 snprintf(name, size, 941 snprintf(name, size,
771 "/usr/lib/debug/.build-id/%.2s/%s.debug", 942 "/usr/lib/debug/.build-id/%.2s/%s.debug",
@@ -776,7 +947,7 @@ more:
776 self->origin++; 947 self->origin++;
777 /* Fall thru */ 948 /* Fall thru */
778 case DSO__ORIG_DSO: 949 case DSO__ORIG_DSO:
779 snprintf(name, size, "%s", self->name); 950 snprintf(name, size, "%s", self->long_name);
780 break; 951 break;
781 952
782 default: 953 default:
@@ -786,7 +957,7 @@ more:
786 fd = open(name, O_RDONLY); 957 fd = open(name, O_RDONLY);
787 } while (fd < 0); 958 } while (fd < 0);
788 959
789 ret = dso__load_sym(self, fd, name, filter, v, NULL); 960 ret = dso__load_sym(self, map, name, fd, filter, 0, 0);
790 close(fd); 961 close(fd);
791 962
792 /* 963 /*
@@ -796,7 +967,7 @@ more:
796 goto more; 967 goto more;
797 968
798 if (ret > 0) { 969 if (ret > 0) {
799 int nr_plt = dso__synthesize_plt_symbols(self, v); 970 int nr_plt = dso__synthesize_plt_symbols(self);
800 if (nr_plt > 0) 971 if (nr_plt > 0)
801 ret += nr_plt; 972 ret += nr_plt;
802 } 973 }
@@ -807,137 +978,321 @@ out:
807 return ret; 978 return ret;
808} 979}
809 980
810static int dso__load_module(struct dso *self, struct mod_dso *mods, const char *name, 981struct map *kernel_map;
811 symbol_filter_t filter, int v) 982
983static void kernel_maps__insert(struct map *map)
812{ 984{
813 struct module *mod = mod_dso__find_module(mods, name); 985 maps__insert(&kernel_maps, map);
814 int err = 0, fd; 986}
815 987
816 if (mod == NULL || !mod->active) 988struct symbol *kernel_maps__find_symbol(u64 ip, struct map **mapp)
817 return err; 989{
990 struct map *map = maps__find(&kernel_maps, ip);
818 991
819 fd = open(mod->path, O_RDONLY); 992 if (mapp)
993 *mapp = map;
820 994
821 if (fd < 0) 995 if (map) {
996 ip = map->map_ip(map, ip);
997 return map->dso->find_symbol(map->dso, ip);
998 }
999
1000 return NULL;
1001}
1002
1003struct map *kernel_maps__find_by_dso_name(const char *name)
1004{
1005 struct rb_node *nd;
1006
1007 for (nd = rb_first(&kernel_maps); nd; nd = rb_next(nd)) {
1008 struct map *map = rb_entry(nd, struct map, rb_node);
1009
1010 if (map->dso && strcmp(map->dso->name, name) == 0)
1011 return map;
1012 }
1013
1014 return NULL;
1015}
1016
1017static int dso__load_module_sym(struct dso *self, struct map *map,
1018 symbol_filter_t filter)
1019{
1020 int err = 0, fd = open(self->long_name, O_RDONLY);
1021
1022 if (fd < 0) {
1023 pr_err("%s: cannot open %s\n", __func__, self->long_name);
822 return err; 1024 return err;
1025 }
823 1026
824 err = dso__load_sym(self, fd, name, filter, v, mod); 1027 err = dso__load_sym(self, map, self->long_name, fd, filter, 0, 1);
825 close(fd); 1028 close(fd);
826 1029
827 return err; 1030 return err;
828} 1031}
829 1032
830int dso__load_modules(struct dso *self, symbol_filter_t filter, int v) 1033static int dsos__load_modules_sym_dir(char *dirname, symbol_filter_t filter)
831{ 1034{
832 struct mod_dso *mods = mod_dso__new_dso("modules"); 1035 struct dirent *dent;
833 struct module *pos; 1036 int nr_symbols = 0, err;
834 struct rb_node *next; 1037 DIR *dir = opendir(dirname);
835 int err, count = 0;
836 1038
837 err = mod_dso__load_modules(mods); 1039 if (!dir) {
1040 pr_err("%s: cannot open %s dir\n", __func__, dirname);
1041 return -1;
1042 }
838 1043
839 if (err <= 0) 1044 while ((dent = readdir(dir)) != NULL) {
840 return err; 1045 char path[PATH_MAX];
1046
1047 if (dent->d_type == DT_DIR) {
1048 if (!strcmp(dent->d_name, ".") ||
1049 !strcmp(dent->d_name, ".."))
1050 continue;
1051
1052 snprintf(path, sizeof(path), "%s/%s",
1053 dirname, dent->d_name);
1054 err = dsos__load_modules_sym_dir(path, filter);
1055 if (err < 0)
1056 goto failure;
1057 } else {
1058 char *dot = strrchr(dent->d_name, '.'),
1059 dso_name[PATH_MAX];
1060 struct map *map;
1061 struct rb_node *last;
1062
1063 if (dot == NULL || strcmp(dot, ".ko"))
1064 continue;
1065 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
1066 (int)(dot - dent->d_name), dent->d_name);
1067
1068 strxfrchar(dso_name, '-', '_');
1069 map = kernel_maps__find_by_dso_name(dso_name);
1070 if (map == NULL)
1071 continue;
1072
1073 snprintf(path, sizeof(path), "%s/%s",
1074 dirname, dent->d_name);
1075
1076 map->dso->long_name = strdup(path);
1077 if (map->dso->long_name == NULL)
1078 goto failure;
1079
1080 err = dso__load_module_sym(map->dso, map, filter);
1081 if (err < 0)
1082 goto failure;
1083 last = rb_last(&map->dso->syms);
1084 if (last) {
1085 struct symbol *sym;
1086 /*
1087 * We do this here as well, even having the
1088 * symbol size found in the symtab because
1089 * misannotated ASM symbols may have the size
1090 * set to zero.
1091 */
1092 dso__fixup_sym_end(map->dso);
1093
1094 sym = rb_entry(last, struct symbol, rb_node);
1095 map->end = map->start + sym->end;
1096 }
1097 }
1098 nr_symbols += err;
1099 }
841 1100
842 /* 1101 return nr_symbols;
843 * Iterate over modules, and load active symbols. 1102failure:
844 */ 1103 closedir(dir);
845 next = rb_first(&mods->mods); 1104 return -1;
846 while (next) { 1105}
847 pos = rb_entry(next, struct module, rb_node);
848 err = dso__load_module(self, mods, pos->name, filter, v);
849 1106
850 if (err < 0) 1107static int dsos__load_modules_sym(symbol_filter_t filter)
851 break; 1108{
1109 struct utsname uts;
1110 char modules_path[PATH_MAX];
852 1111
853 next = rb_next(&pos->rb_node); 1112 if (uname(&uts) < 0)
854 count += err; 1113 return -1;
855 }
856 1114
857 if (err < 0) { 1115 snprintf(modules_path, sizeof(modules_path), "/lib/modules/%s/kernel",
858 mod_dso__delete_modules(mods); 1116 uts.release);
859 mod_dso__delete_self(mods);
860 return err;
861 }
862 1117
863 return count; 1118 return dsos__load_modules_sym_dir(modules_path, filter);
864} 1119}
865 1120
866static inline void dso__fill_symbol_holes(struct dso *self) 1121/*
1122 * Constructor variant for modules (where we know from /proc/modules where
1123 * they are loaded) and for vmlinux, where only after we load all the
1124 * symbols we'll know where it starts and ends.
1125 */
1126static struct map *map__new2(u64 start, struct dso *dso)
867{ 1127{
868 struct symbol *prev = NULL; 1128 struct map *self = malloc(sizeof(*self));
869 struct rb_node *nd;
870 1129
871 for (nd = rb_last(&self->syms); nd; nd = rb_prev(nd)) { 1130 if (self != NULL) {
872 struct symbol *pos = rb_entry(nd, struct symbol, rb_node); 1131 self->start = start;
1132 /*
1133 * Will be filled after we load all the symbols
1134 */
1135 self->end = 0;
873 1136
874 if (prev) { 1137 self->pgoff = 0;
875 u64 hole = 0; 1138 self->dso = dso;
876 int alias = pos->start == prev->start; 1139 self->map_ip = map__map_ip;
1140 self->unmap_ip = map__unmap_ip;
1141 RB_CLEAR_NODE(&self->rb_node);
1142 }
1143 return self;
1144}
877 1145
878 if (!alias) 1146static int dsos__load_modules(unsigned int sym_priv_size)
879 hole = prev->start - pos->end - 1; 1147{
1148 char *line = NULL;
1149 size_t n;
1150 FILE *file = fopen("/proc/modules", "r");
1151 struct map *map;
880 1152
881 if (hole || alias) { 1153 if (file == NULL)
882 if (alias) 1154 return -1;
883 pos->end = prev->end; 1155
884 else if (hole) 1156 while (!feof(file)) {
885 pos->end = prev->start - 1; 1157 char name[PATH_MAX];
886 } 1158 u64 start;
1159 struct dso *dso;
1160 char *sep;
1161 int line_len;
1162
1163 line_len = getline(&line, &n, file);
1164 if (line_len < 0)
1165 break;
1166
1167 if (!line)
1168 goto out_failure;
1169
1170 line[--line_len] = '\0'; /* \n */
1171
1172 sep = strrchr(line, 'x');
1173 if (sep == NULL)
1174 continue;
1175
1176 hex2u64(sep + 1, &start);
1177
1178 sep = strchr(line, ' ');
1179 if (sep == NULL)
1180 continue;
1181
1182 *sep = '\0';
1183
1184 snprintf(name, sizeof(name), "[%s]", line);
1185 dso = dso__new(name, sym_priv_size);
1186
1187 if (dso == NULL)
1188 goto out_delete_line;
1189
1190 map = map__new2(start, dso);
1191 if (map == NULL) {
1192 dso__delete(dso);
1193 goto out_delete_line;
887 } 1194 }
888 prev = pos; 1195
1196 dso->origin = DSO__ORIG_KMODULE;
1197 kernel_maps__insert(map);
1198 dsos__add(dso);
889 } 1199 }
1200
1201 free(line);
1202 fclose(file);
1203
1204 return 0;
1205
1206out_delete_line:
1207 free(line);
1208out_failure:
1209 return -1;
890} 1210}
891 1211
892static int dso__load_vmlinux(struct dso *self, const char *vmlinux, 1212static int dso__load_vmlinux(struct dso *self, struct map *map,
893 symbol_filter_t filter, int v) 1213 const char *vmlinux, symbol_filter_t filter)
894{ 1214{
895 int err, fd = open(vmlinux, O_RDONLY); 1215 int err, fd = open(vmlinux, O_RDONLY);
896 1216
897 if (fd < 0) 1217 if (fd < 0)
898 return -1; 1218 return -1;
899 1219
900 err = dso__load_sym(self, fd, vmlinux, filter, v, NULL); 1220 err = dso__load_sym(self, map, self->long_name, fd, filter, 1, 0);
901
902 if (err > 0)
903 dso__fill_symbol_holes(self);
904 1221
905 close(fd); 1222 close(fd);
906 1223
907 return err; 1224 return err;
908} 1225}
909 1226
910int dso__load_kernel(struct dso *self, const char *vmlinux, 1227int dsos__load_kernel(const char *vmlinux, unsigned int sym_priv_size,
911 symbol_filter_t filter, int v, int use_modules) 1228 symbol_filter_t filter, int use_modules)
912{ 1229{
913 int err = -1; 1230 int err = -1;
1231 struct dso *dso = dso__new(vmlinux, sym_priv_size);
1232
1233 if (dso == NULL)
1234 return -1;
1235
1236 dso->short_name = "[kernel]";
1237 kernel_map = map__new2(0, dso);
1238 if (kernel_map == NULL)
1239 goto out_delete_dso;
1240
1241 kernel_map->map_ip = kernel_map->unmap_ip = identity__map_ip;
1242
1243 if (use_modules && dsos__load_modules(sym_priv_size) < 0) {
1244 pr_warning("Failed to load list of modules in use! "
1245 "Continuing...\n");
1246 use_modules = 0;
1247 }
914 1248
915 if (vmlinux) { 1249 if (vmlinux) {
916 err = dso__load_vmlinux(self, vmlinux, filter, v); 1250 err = dso__load_vmlinux(dso, kernel_map, vmlinux, filter);
917 if (err > 0 && use_modules) { 1251 if (err > 0 && use_modules) {
918 int syms = dso__load_modules(self, filter, v); 1252 int syms = dsos__load_modules_sym(filter);
919 1253
920 if (syms < 0) { 1254 if (syms < 0)
921 fprintf(stderr, "dso__load_modules failed!\n"); 1255 pr_warning("Failed to read module symbols!"
922 return syms; 1256 " Continuing...\n");
923 } 1257 else
924 err += syms; 1258 err += syms;
925 } 1259 }
926 } 1260 }
927 1261
928 if (err <= 0) 1262 if (err <= 0)
929 err = dso__load_kallsyms(self, filter, v); 1263 err = kernel_maps__load_kallsyms(filter, use_modules);
1264
1265 if (err > 0) {
1266 struct rb_node *node = rb_first(&dso->syms);
1267 struct symbol *sym = rb_entry(node, struct symbol, rb_node);
1268
1269 kernel_map->start = sym->start;
1270 node = rb_last(&dso->syms);
1271 sym = rb_entry(node, struct symbol, rb_node);
1272 kernel_map->end = sym->end;
1273
1274 dso->origin = DSO__ORIG_KERNEL;
1275 kernel_maps__insert(kernel_map);
1276 /*
1277 * Now that we have all sorted out, just set the ->end of all
1278 * maps:
1279 */
1280 kernel_maps__fixup_end();
1281 dsos__add(dso);
930 1282
931 if (err > 0) 1283 if (verbose)
932 self->origin = DSO__ORIG_KERNEL; 1284 kernel_maps__fprintf(stderr);
1285 }
933 1286
934 return err; 1287 return err;
1288
1289out_delete_dso:
1290 dso__delete(dso);
1291 return -1;
935} 1292}
936 1293
937LIST_HEAD(dsos); 1294LIST_HEAD(dsos);
938struct dso *kernel_dso;
939struct dso *vdso; 1295struct dso *vdso;
940struct dso *hypervisor_dso;
941 1296
942const char *vmlinux_name = "vmlinux"; 1297const char *vmlinux_name = "vmlinux";
943int modules; 1298int modules;
@@ -957,33 +1312,21 @@ static struct dso *dsos__find(const char *name)
957 return NULL; 1312 return NULL;
958} 1313}
959 1314
960struct dso *dsos__findnew(const char *name) 1315struct dso *dsos__findnew(const char *name, unsigned int sym_priv_size,
1316 bool *is_new)
961{ 1317{
962 struct dso *dso = dsos__find(name); 1318 struct dso *dso = dsos__find(name);
963 int nr;
964 1319
965 if (dso) 1320 if (!dso) {
966 return dso; 1321 dso = dso__new(name, sym_priv_size);
967 1322 if (dso) {
968 dso = dso__new(name, 0); 1323 dsos__add(dso);
969 if (!dso) 1324 *is_new = true;
970 goto out_delete_dso; 1325 }
971 1326 } else
972 nr = dso__load(dso, NULL, verbose); 1327 *is_new = false;
973 if (nr < 0) {
974 eprintf("Failed to open: %s\n", name);
975 goto out_delete_dso;
976 }
977 if (!nr)
978 eprintf("No symbols found in: %s, maybe install a debug package?\n", name);
979
980 dsos__add(dso);
981 1328
982 return dso; 1329 return dso;
983
984out_delete_dso:
985 dso__delete(dso);
986 return NULL;
987} 1330}
988 1331
989void dsos__fprintf(FILE *fp) 1332void dsos__fprintf(FILE *fp)
@@ -994,43 +1337,21 @@ void dsos__fprintf(FILE *fp)
994 dso__fprintf(pos, fp); 1337 dso__fprintf(pos, fp);
995} 1338}
996 1339
997static struct symbol *vdso__find_symbol(struct dso *dso, u64 ip) 1340int load_kernel(unsigned int sym_priv_size, symbol_filter_t filter)
998{
999 return dso__find_symbol(dso, ip);
1000}
1001
1002int load_kernel(void)
1003{ 1341{
1004 int err; 1342 if (dsos__load_kernel(vmlinux_name, sym_priv_size, filter,
1005 1343 modules) <= 0)
1006 kernel_dso = dso__new("[kernel]", 0);
1007 if (!kernel_dso)
1008 return -1; 1344 return -1;
1009 1345
1010 err = dso__load_kernel(kernel_dso, vmlinux_name, NULL, verbose, modules);
1011 if (err <= 0) {
1012 dso__delete(kernel_dso);
1013 kernel_dso = NULL;
1014 } else
1015 dsos__add(kernel_dso);
1016
1017 vdso = dso__new("[vdso]", 0); 1346 vdso = dso__new("[vdso]", 0);
1018 if (!vdso) 1347 if (!vdso)
1019 return -1; 1348 return -1;
1020 1349
1021 vdso->find_symbol = vdso__find_symbol;
1022
1023 dsos__add(vdso); 1350 dsos__add(vdso);
1024 1351
1025 hypervisor_dso = dso__new("[hypervisor]", 0); 1352 return 0;
1026 if (!hypervisor_dso)
1027 return -1;
1028 dsos__add(hypervisor_dso);
1029
1030 return err;
1031} 1353}
1032 1354
1033
1034void symbol__init(void) 1355void symbol__init(void)
1035{ 1356{
1036 elf_version(EV_CURRENT); 1357 elf_version(EV_CURRENT);