]> bbs.cooldavid.org Git - net-next-2.6.git/blame - tools/perf/util/symbol.c
perf report: Update for the new FORK/EXIT events
[net-next-2.6.git] / tools / perf / util / symbol.c
CommitLineData
a2928c42
ACM
1#include "util.h"
2#include "../perf.h"
a0055ae2 3#include "string.h"
a2928c42
ACM
4#include "symbol.h"
5
6#include <libelf.h>
7#include <gelf.h>
8#include <elf.h>
28ac909b 9#include <bfd.h>
a2928c42 10
0b73da3f
IM
11const char *sym_hist_filter;
12
28ac909b
ACM
13#ifndef DMGL_PARAMS
14#define DMGL_PARAMS (1 << 0) /* Include function args */
15#define DMGL_ANSI (1 << 1) /* Include const, volatile, etc */
16#endif
17
9cffa8d5 18static struct symbol *symbol__new(u64 start, u64 len,
0b73da3f 19 const char *name, unsigned int priv_size,
9cffa8d5 20 u64 obj_start, int verbose)
a2928c42 21{
0085c954 22 size_t namelen = strlen(name) + 1;
0b73da3f 23 struct symbol *self = calloc(1, priv_size + sizeof(*self) + namelen);
a2928c42 24
0b73da3f
IM
25 if (!self)
26 return NULL;
27
28 if (verbose >= 2)
29 printf("new symbol: %016Lx [%08lx]: %s, hist: %p, obj_start: %p\n",
9cffa8d5 30 (u64)start, (unsigned long)len, name, self->hist, (void *)(unsigned long)obj_start);
0b73da3f
IM
31
32 self->obj_start= obj_start;
33 self->hist = NULL;
34 self->hist_sum = 0;
35
36 if (sym_hist_filter && !strcmp(name, sym_hist_filter))
9cffa8d5 37 self->hist = calloc(sizeof(u64), len);
0b73da3f
IM
38
39 if (priv_size) {
40 memset(self, 0, priv_size);
41 self = ((void *)self) + priv_size;
a2928c42 42 }
0b73da3f 43 self->start = start;
6cfcc53e 44 self->end = len ? start + len - 1 : start;
0b73da3f 45 memcpy(self->name, name, namelen);
a2928c42
ACM
46
47 return self;
48}
49
0085c954 50static void symbol__delete(struct symbol *self, unsigned int priv_size)
a2928c42 51{
0085c954 52 free(((void *)self) - priv_size);
a2928c42
ACM
53}
54
55static size_t symbol__fprintf(struct symbol *self, FILE *fp)
56{
6cfcc53e
MG
57 if (!self->module)
58 return fprintf(fp, " %llx-%llx %s\n",
a2928c42 59 self->start, self->end, self->name);
6cfcc53e
MG
60 else
61 return fprintf(fp, " %llx-%llx %s \t[%s]\n",
62 self->start, self->end, self->name, self->module->name);
a2928c42
ACM
63}
64
0085c954 65struct dso *dso__new(const char *name, unsigned int sym_priv_size)
a2928c42
ACM
66{
67 struct dso *self = malloc(sizeof(*self) + strlen(name) + 1);
68
69 if (self != NULL) {
70 strcpy(self->name, name);
71 self->syms = RB_ROOT;
0085c954 72 self->sym_priv_size = sym_priv_size;
fc54db51 73 self->find_symbol = dso__find_symbol;
52d422de 74 self->slen_calculated = 0;
a2928c42
ACM
75 }
76
77 return self;
78}
79
80static void dso__delete_symbols(struct dso *self)
81{
82 struct symbol *pos;
83 struct rb_node *next = rb_first(&self->syms);
84
85 while (next) {
86 pos = rb_entry(next, struct symbol, rb_node);
87 next = rb_next(&pos->rb_node);
c8c96525 88 rb_erase(&pos->rb_node, &self->syms);
0085c954 89 symbol__delete(pos, self->sym_priv_size);
a2928c42
ACM
90 }
91}
92
93void dso__delete(struct dso *self)
94{
95 dso__delete_symbols(self);
96 free(self);
97}
98
99static void dso__insert_symbol(struct dso *self, struct symbol *sym)
100{
101 struct rb_node **p = &self->syms.rb_node;
102 struct rb_node *parent = NULL;
9cffa8d5 103 const u64 ip = sym->start;
a2928c42
ACM
104 struct symbol *s;
105
106 while (*p != NULL) {
107 parent = *p;
108 s = rb_entry(parent, struct symbol, rb_node);
109 if (ip < s->start)
110 p = &(*p)->rb_left;
111 else
112 p = &(*p)->rb_right;
113 }
114 rb_link_node(&sym->rb_node, parent, p);
115 rb_insert_color(&sym->rb_node, &self->syms);
116}
117
9cffa8d5 118struct symbol *dso__find_symbol(struct dso *self, u64 ip)
a2928c42
ACM
119{
120 struct rb_node *n;
121
122 if (self == NULL)
123 return NULL;
124
125 n = self->syms.rb_node;
126
127 while (n) {
128 struct symbol *s = rb_entry(n, struct symbol, rb_node);
129
130 if (ip < s->start)
131 n = n->rb_left;
132 else if (ip > s->end)
133 n = n->rb_right;
134 else
135 return s;
136 }
137
138 return NULL;
139}
140
141size_t dso__fprintf(struct dso *self, FILE *fp)
142{
143 size_t ret = fprintf(fp, "dso: %s\n", self->name);
144
145 struct rb_node *nd;
146 for (nd = rb_first(&self->syms); nd; nd = rb_next(nd)) {
147 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
148 ret += symbol__fprintf(pos, fp);
149 }
150
151 return ret;
152}
153
bd74137e 154static int dso__load_kallsyms(struct dso *self, symbol_filter_t filter, int verbose)
a2928c42
ACM
155{
156 struct rb_node *nd, *prevnd;
157 char *line = NULL;
158 size_t n;
159 FILE *file = fopen("/proc/kallsyms", "r");
9974f496 160 int count = 0;
a2928c42
ACM
161
162 if (file == NULL)
163 goto out_failure;
164
165 while (!feof(file)) {
9cffa8d5 166 u64 start;
a2928c42
ACM
167 struct symbol *sym;
168 int line_len, len;
169 char symbol_type;
170
171 line_len = getline(&line, &n, file);
172 if (line_len < 0)
173 break;
174
175 if (!line)
176 goto out_failure;
177
178 line[--line_len] = '\0'; /* \n */
179
a0055ae2 180 len = hex2u64(line, &start);
a2928c42
ACM
181
182 len++;
183 if (len + 2 >= line_len)
184 continue;
185
186 symbol_type = toupper(line[len]);
187 /*
188 * We're interested only in code ('T'ext)
189 */
190 if (symbol_type != 'T' && symbol_type != 'W')
191 continue;
192 /*
193 * Well fix up the end later, when we have all sorted.
194 */
0085c954 195 sym = symbol__new(start, 0xdead, line + len + 2,
0b73da3f 196 self->sym_priv_size, 0, verbose);
a2928c42
ACM
197
198 if (sym == NULL)
199 goto out_delete_line;
200
69ee69f6
ACM
201 if (filter && filter(self, sym))
202 symbol__delete(sym, self->sym_priv_size);
9974f496 203 else {
69ee69f6 204 dso__insert_symbol(self, sym);
9974f496
MG
205 count++;
206 }
a2928c42
ACM
207 }
208
209 /*
210 * Now that we have all sorted out, just set the ->end of all
211 * symbols
212 */
213 prevnd = rb_first(&self->syms);
214
215 if (prevnd == NULL)
216 goto out_delete_line;
217
218 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
219 struct symbol *prev = rb_entry(prevnd, struct symbol, rb_node),
220 *curr = rb_entry(nd, struct symbol, rb_node);
221
222 prev->end = curr->start - 1;
223 prevnd = nd;
224 }
225
226 free(line);
227 fclose(file);
228
9974f496 229 return count;
a2928c42
ACM
230
231out_delete_line:
232 free(line);
233out_failure:
234 return -1;
235}
236
80d496be
PE
237static int dso__load_perf_map(struct dso *self, symbol_filter_t filter, int verbose)
238{
239 char *line = NULL;
240 size_t n;
241 FILE *file;
242 int nr_syms = 0;
243
244 file = fopen(self->name, "r");
245 if (file == NULL)
246 goto out_failure;
247
248 while (!feof(file)) {
9cffa8d5 249 u64 start, size;
80d496be
PE
250 struct symbol *sym;
251 int line_len, len;
252
253 line_len = getline(&line, &n, file);
254 if (line_len < 0)
255 break;
256
257 if (!line)
258 goto out_failure;
259
260 line[--line_len] = '\0'; /* \n */
261
262 len = hex2u64(line, &start);
263
264 len++;
265 if (len + 2 >= line_len)
266 continue;
267
268 len += hex2u64(line + len, &size);
269
270 len++;
271 if (len + 2 >= line_len)
272 continue;
273
274 sym = symbol__new(start, size, line + len,
275 self->sym_priv_size, start, verbose);
276
277 if (sym == NULL)
278 goto out_delete_line;
279
280 if (filter && filter(self, sym))
281 symbol__delete(sym, self->sym_priv_size);
282 else {
283 dso__insert_symbol(self, sym);
284 nr_syms++;
285 }
286 }
287
288 free(line);
289 fclose(file);
290
291 return nr_syms;
292
293out_delete_line:
294 free(line);
295out_failure:
296 return -1;
297}
298
a2928c42
ACM
299/**
300 * elf_symtab__for_each_symbol - iterate thru all the symbols
301 *
302 * @self: struct elf_symtab instance to iterate
303 * @index: uint32_t index
304 * @sym: GElf_Sym iterator
305 */
306#define elf_symtab__for_each_symbol(syms, nr_syms, index, sym) \
307 for (index = 0, gelf_getsym(syms, index, &sym);\
308 index < nr_syms; \
309 index++, gelf_getsym(syms, index, &sym))
310
311static inline uint8_t elf_sym__type(const GElf_Sym *sym)
312{
313 return GELF_ST_TYPE(sym->st_info);
314}
315
316static inline int elf_sym__is_function(const GElf_Sym *sym)
317{
318 return elf_sym__type(sym) == STT_FUNC &&
319 sym->st_name != 0 &&
320 sym->st_shndx != SHN_UNDEF &&
321 sym->st_size != 0;
322}
323
6cfcc53e
MG
324static inline int elf_sym__is_label(const GElf_Sym *sym)
325{
326 return elf_sym__type(sym) == STT_NOTYPE &&
327 sym->st_name != 0 &&
328 sym->st_shndx != SHN_UNDEF &&
329 sym->st_shndx != SHN_ABS;
330}
331
332static inline const char *elf_sec__name(const GElf_Shdr *shdr,
333 const Elf_Data *secstrs)
334{
335 return secstrs->d_buf + shdr->sh_name;
336}
337
338static inline int elf_sec__is_text(const GElf_Shdr *shdr,
339 const Elf_Data *secstrs)
340{
341 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
342}
343
a2928c42
ACM
344static inline const char *elf_sym__name(const GElf_Sym *sym,
345 const Elf_Data *symstrs)
346{
347 return symstrs->d_buf + sym->st_name;
348}
349
350static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
351 GElf_Shdr *shp, const char *name,
352 size_t *index)
353{
354 Elf_Scn *sec = NULL;
355 size_t cnt = 1;
356
357 while ((sec = elf_nextscn(elf, sec)) != NULL) {
358 char *str;
359
360 gelf_getshdr(sec, shp);
361 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
362 if (!strcmp(name, str)) {
363 if (index)
364 *index = cnt;
365 break;
366 }
367 ++cnt;
368 }
369
370 return sec;
371}
372
8ce998d6
ACM
373#define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
374 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
375 idx < nr_entries; \
376 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
377
378#define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
379 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
380 idx < nr_entries; \
381 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
382
a25e46c4
ACM
383/*
384 * We need to check if we have a .dynsym, so that we can handle the
385 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
386 * .dynsym or .symtab).
387 * And always look at the original dso, not at debuginfo packages, that
388 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
389 */
390static int dso__synthesize_plt_symbols(struct dso *self, int verbose)
8ce998d6
ACM
391{
392 uint32_t nr_rel_entries, idx;
393 GElf_Sym sym;
9cffa8d5 394 u64 plt_offset;
8ce998d6
ACM
395 GElf_Shdr shdr_plt;
396 struct symbol *f;
a25e46c4 397 GElf_Shdr shdr_rel_plt, shdr_dynsym;
8ce998d6 398 Elf_Data *reldata, *syms, *symstrs;
a25e46c4
ACM
399 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
400 size_t dynsym_idx;
401 GElf_Ehdr ehdr;
8ce998d6 402 char sympltname[1024];
a25e46c4
ACM
403 Elf *elf;
404 int nr = 0, symidx, fd, err = 0;
405
406 fd = open(self->name, O_RDONLY);
407 if (fd < 0)
408 goto out;
409
410 elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
411 if (elf == NULL)
412 goto out_close;
413
414 if (gelf_getehdr(elf, &ehdr) == NULL)
415 goto out_elf_end;
416
417 scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
418 ".dynsym", &dynsym_idx);
419 if (scn_dynsym == NULL)
420 goto out_elf_end;
8ce998d6 421
a25e46c4 422 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
8ce998d6
ACM
423 ".rela.plt", NULL);
424 if (scn_plt_rel == NULL) {
a25e46c4 425 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
8ce998d6
ACM
426 ".rel.plt", NULL);
427 if (scn_plt_rel == NULL)
a25e46c4 428 goto out_elf_end;
8ce998d6
ACM
429 }
430
a25e46c4
ACM
431 err = -1;
432
8ce998d6 433 if (shdr_rel_plt.sh_link != dynsym_idx)
a25e46c4 434 goto out_elf_end;
8ce998d6 435
a25e46c4
ACM
436 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
437 goto out_elf_end;
8ce998d6
ACM
438
439 /*
440 * Fetch the relocation section to find the indexes to the GOT
441 * and the symbols in the .dynsym they refer to.
442 */
443 reldata = elf_getdata(scn_plt_rel, NULL);
444 if (reldata == NULL)
a25e46c4 445 goto out_elf_end;
8ce998d6
ACM
446
447 syms = elf_getdata(scn_dynsym, NULL);
448 if (syms == NULL)
a25e46c4 449 goto out_elf_end;
8ce998d6 450
a25e46c4 451 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
8ce998d6 452 if (scn_symstrs == NULL)
a25e46c4 453 goto out_elf_end;
8ce998d6
ACM
454
455 symstrs = elf_getdata(scn_symstrs, NULL);
456 if (symstrs == NULL)
a25e46c4 457 goto out_elf_end;
8ce998d6
ACM
458
459 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
460 plt_offset = shdr_plt.sh_offset;
461
462 if (shdr_rel_plt.sh_type == SHT_RELA) {
463 GElf_Rela pos_mem, *pos;
464
465 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
466 nr_rel_entries) {
467 symidx = GELF_R_SYM(pos->r_info);
468 plt_offset += shdr_plt.sh_entsize;
469 gelf_getsym(syms, symidx, &sym);
470 snprintf(sympltname, sizeof(sympltname),
471 "%s@plt", elf_sym__name(&sym, symstrs));
472
473 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
0b73da3f 474 sympltname, self->sym_priv_size, 0, verbose);
8ce998d6 475 if (!f)
a25e46c4 476 goto out_elf_end;
8ce998d6
ACM
477
478 dso__insert_symbol(self, f);
479 ++nr;
480 }
481 } else if (shdr_rel_plt.sh_type == SHT_REL) {
482 GElf_Rel pos_mem, *pos;
483 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
484 nr_rel_entries) {
485 symidx = GELF_R_SYM(pos->r_info);
486 plt_offset += shdr_plt.sh_entsize;
487 gelf_getsym(syms, symidx, &sym);
488 snprintf(sympltname, sizeof(sympltname),
489 "%s@plt", elf_sym__name(&sym, symstrs));
490
491 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
0b73da3f 492 sympltname, self->sym_priv_size, 0, verbose);
8ce998d6 493 if (!f)
a25e46c4 494 goto out_elf_end;
8ce998d6
ACM
495
496 dso__insert_symbol(self, f);
497 ++nr;
498 }
8ce998d6
ACM
499 }
500
a25e46c4
ACM
501 err = 0;
502out_elf_end:
503 elf_end(elf);
504out_close:
505 close(fd);
506
507 if (err == 0)
508 return nr;
509out:
510 fprintf(stderr, "%s: problems reading %s PLT info.\n",
511 __func__, self->name);
512 return 0;
8ce998d6
ACM
513}
514
69ee69f6 515static int dso__load_sym(struct dso *self, int fd, const char *name,
6cfcc53e 516 symbol_filter_t filter, int verbose, struct module *mod)
a2928c42 517{
6cfcc53e 518 Elf_Data *symstrs, *secstrs;
a2928c42
ACM
519 uint32_t nr_syms;
520 int err = -1;
521 uint32_t index;
522 GElf_Ehdr ehdr;
523 GElf_Shdr shdr;
524 Elf_Data *syms;
525 GElf_Sym sym;
a25e46c4 526 Elf_Scn *sec, *sec_strndx;
a2928c42 527 Elf *elf;
d20ff6bd 528 int nr = 0, kernel = !strcmp("[kernel]", self->name);
a2928c42
ACM
529
530 elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
531 if (elf == NULL) {
bd74137e
IM
532 if (verbose)
533 fprintf(stderr, "%s: cannot read %s ELF file.\n",
534 __func__, name);
a2928c42
ACM
535 goto out_close;
536 }
537
538 if (gelf_getehdr(elf, &ehdr) == NULL) {
bd74137e
IM
539 if (verbose)
540 fprintf(stderr, "%s: cannot get elf header.\n", __func__);
a2928c42
ACM
541 goto out_elf_end;
542 }
543
544 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
8ce998d6 545 if (sec == NULL) {
a25e46c4
ACM
546 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
547 if (sec == NULL)
8ce998d6 548 goto out_elf_end;
8ce998d6 549 }
a2928c42
ACM
550
551 syms = elf_getdata(sec, NULL);
552 if (syms == NULL)
553 goto out_elf_end;
554
555 sec = elf_getscn(elf, shdr.sh_link);
556 if (sec == NULL)
557 goto out_elf_end;
558
559 symstrs = elf_getdata(sec, NULL);
560 if (symstrs == NULL)
561 goto out_elf_end;
562
6cfcc53e
MG
563 sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
564 if (sec_strndx == NULL)
565 goto out_elf_end;
566
567 secstrs = elf_getdata(sec_strndx, NULL);
568 if (symstrs == NULL)
569 goto out_elf_end;
570
a2928c42
ACM
571 nr_syms = shdr.sh_size / shdr.sh_entsize;
572
e9fbc9dc 573 memset(&sym, 0, sizeof(sym));
d20ff6bd
MG
574 if (!kernel) {
575 self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
30d7a77d
ACM
576 elf_section_by_name(elf, &ehdr, &shdr,
577 ".gnu.prelink_undo",
578 NULL) != NULL);
d20ff6bd
MG
579 } else self->adjust_symbols = 0;
580
a2928c42
ACM
581 elf_symtab__for_each_symbol(syms, nr_syms, index, sym) {
582 struct symbol *f;
28ac909b
ACM
583 const char *name;
584 char *demangled;
9cffa8d5 585 u64 obj_start;
6cfcc53e
MG
586 struct section *section = NULL;
587 int is_label = elf_sym__is_label(&sym);
588 const char *section_name;
a2928c42 589
6cfcc53e 590 if (!is_label && !elf_sym__is_function(&sym))
a2928c42
ACM
591 continue;
592
593 sec = elf_getscn(elf, sym.st_shndx);
594 if (!sec)
595 goto out_elf_end;
596
597 gelf_getshdr(sec, &shdr);
6cfcc53e
MG
598
599 if (is_label && !elf_sec__is_text(&shdr, secstrs))
600 continue;
601
602 section_name = elf_sec__name(&shdr, secstrs);
0b73da3f
IM
603 obj_start = sym.st_value;
604
30d7a77d 605 if (self->adjust_symbols) {
f5812a7a
ACM
606 if (verbose >= 2)
607 printf("adjusting symbol: st_value: %Lx sh_addr: %Lx sh_offset: %Lx\n",
608 (u64)sym.st_value, (u64)shdr.sh_addr, (u64)shdr.sh_offset);
520f2c34 609
f5812a7a
ACM
610 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
611 }
a2928c42 612
6cfcc53e
MG
613 if (mod) {
614 section = mod->sections->find_section(mod->sections, section_name);
615 if (section)
616 sym.st_value += section->vma;
617 else {
618 fprintf(stderr, "dso__load_sym() module %s lookup of %s failed\n",
619 mod->name, section_name);
620 goto out_elf_end;
621 }
622 }
28ac909b
ACM
623 /*
624 * We need to figure out if the object was created from C++ sources
625 * DWARF DW_compile_unit has this, but we don't always have access
626 * to it...
627 */
628 name = elf_sym__name(&sym, symstrs);
629 demangled = bfd_demangle(NULL, name, DMGL_PARAMS | DMGL_ANSI);
630 if (demangled != NULL)
631 name = demangled;
6cfcc53e 632
28ac909b 633 f = symbol__new(sym.st_value, sym.st_size, name,
0b73da3f 634 self->sym_priv_size, obj_start, verbose);
28ac909b 635 free(demangled);
a2928c42
ACM
636 if (!f)
637 goto out_elf_end;
638
69ee69f6
ACM
639 if (filter && filter(self, f))
640 symbol__delete(f, self->sym_priv_size);
641 else {
6cfcc53e 642 f->module = mod;
69ee69f6
ACM
643 dso__insert_symbol(self, f);
644 nr++;
645 }
a2928c42
ACM
646 }
647
648 err = nr;
649out_elf_end:
650 elf_end(elf);
651out_close:
652 return err;
653}
654
bd74137e 655int dso__load(struct dso *self, symbol_filter_t filter, int verbose)
a2928c42
ACM
656{
657 int size = strlen(self->name) + sizeof("/usr/lib/debug%s.debug");
658 char *name = malloc(size);
659 int variant = 0;
660 int ret = -1;
661 int fd;
662
663 if (!name)
664 return -1;
665
30d7a77d 666 self->adjust_symbols = 0;
f5812a7a 667
80d496be
PE
668 if (strncmp(self->name, "/tmp/perf-", 10) == 0)
669 return dso__load_perf_map(self, filter, verbose);
670
a2928c42
ACM
671more:
672 do {
673 switch (variant) {
674 case 0: /* Fedora */
675 snprintf(name, size, "/usr/lib/debug%s.debug", self->name);
676 break;
677 case 1: /* Ubuntu */
678 snprintf(name, size, "/usr/lib/debug%s", self->name);
679 break;
680 case 2: /* Sane people */
681 snprintf(name, size, "%s", self->name);
682 break;
683
684 default:
685 goto out;
686 }
687 variant++;
688
689 fd = open(name, O_RDONLY);
690 } while (fd < 0);
691
6cfcc53e 692 ret = dso__load_sym(self, fd, name, filter, verbose, NULL);
a2928c42
ACM
693 close(fd);
694
695 /*
696 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
697 */
698 if (!ret)
699 goto more;
700
a25e46c4
ACM
701 if (ret > 0) {
702 int nr_plt = dso__synthesize_plt_symbols(self, verbose);
703 if (nr_plt > 0)
704 ret += nr_plt;
705 }
a2928c42
ACM
706out:
707 free(name);
708 return ret;
709}
710
6cfcc53e
MG
711static int dso__load_module(struct dso *self, struct mod_dso *mods, const char *name,
712 symbol_filter_t filter, int verbose)
713{
714 struct module *mod = mod_dso__find_module(mods, name);
715 int err = 0, fd;
716
717 if (mod == NULL || !mod->active)
718 return err;
719
720 fd = open(mod->path, O_RDONLY);
721
722 if (fd < 0)
723 return err;
724
725 err = dso__load_sym(self, fd, name, filter, verbose, mod);
726 close(fd);
727
728 return err;
729}
730
731int dso__load_modules(struct dso *self, symbol_filter_t filter, int verbose)
732{
733 struct mod_dso *mods = mod_dso__new_dso("modules");
734 struct module *pos;
735 struct rb_node *next;
736 int err;
737
738 err = mod_dso__load_modules(mods);
739
740 if (err <= 0)
741 return err;
742
743 /*
744 * Iterate over modules, and load active symbols.
745 */
746 next = rb_first(&mods->mods);
747 while (next) {
748 pos = rb_entry(next, struct module, rb_node);
749 err = dso__load_module(self, mods, pos->name, filter, verbose);
750
751 if (err < 0)
752 break;
753
754 next = rb_next(&pos->rb_node);
755 }
756
757 if (err < 0) {
758 mod_dso__delete_modules(mods);
759 mod_dso__delete_self(mods);
760 }
761
762 return err;
763}
764
765static inline void dso__fill_symbol_holes(struct dso *self)
766{
767 struct symbol *prev = NULL;
768 struct rb_node *nd;
769
770 for (nd = rb_last(&self->syms); nd; nd = rb_prev(nd)) {
771 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
772
773 if (prev) {
774 u64 hole = 0;
775 int alias = pos->start == prev->start;
776
777 if (!alias)
778 hole = prev->start - pos->end - 1;
779
780 if (hole || alias) {
781 if (alias)
782 pos->end = prev->end;
783 else if (hole)
784 pos->end = prev->start - 1;
785 }
786 }
787 prev = pos;
788 }
789}
790
69ee69f6 791static int dso__load_vmlinux(struct dso *self, const char *vmlinux,
bd74137e 792 symbol_filter_t filter, int verbose)
a2928c42
ACM
793{
794 int err, fd = open(vmlinux, O_RDONLY);
795
796 if (fd < 0)
797 return -1;
798
6cfcc53e
MG
799 err = dso__load_sym(self, fd, vmlinux, filter, verbose, NULL);
800
801 if (err > 0)
802 dso__fill_symbol_holes(self);
803
a2928c42
ACM
804 close(fd);
805
806 return err;
807}
808
bd74137e 809int dso__load_kernel(struct dso *self, const char *vmlinux,
6cfcc53e 810 symbol_filter_t filter, int verbose, int modules)
a827c875
ACM
811{
812 int err = -1;
813
6cfcc53e 814 if (vmlinux) {
bd74137e 815 err = dso__load_vmlinux(self, vmlinux, filter, verbose);
6cfcc53e
MG
816 if (err > 0 && modules)
817 err = dso__load_modules(self, filter, verbose);
818 }
a827c875 819
9974f496 820 if (err <= 0)
bd74137e 821 err = dso__load_kallsyms(self, filter, verbose);
a827c875
ACM
822
823 return err;
824}
825
a2928c42
ACM
826void symbol__init(void)
827{
828 elf_version(EV_CURRENT);
829}