blob: 504ac3132019ea866ac012333acb31a3c8087e0b [file] [log] [blame]
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001#include "util.h"
2#include "../perf.h"
3#include "symbol.h"
4
5#include <libelf.h>
6#include <gelf.h>
7#include <elf.h>
8
9static struct symbol *symbol__new(uint64_t start, uint64_t len,
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -030010 const char *name, unsigned int priv_size)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030011{
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -030012 size_t namelen = strlen(name) + 1;
13 struct symbol *self = malloc(priv_size + sizeof(*self) + namelen);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030014
15 if (self != NULL) {
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -030016 if (priv_size) {
17 memset(self, 0, priv_size);
18 self = ((void *)self) + priv_size;
19 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030020 self->start = start;
21 self->end = start + len;
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -030022 memcpy(self->name, name, namelen);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030023 }
24
25 return self;
26}
27
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -030028static void symbol__delete(struct symbol *self, unsigned int priv_size)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030029{
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -030030 free(((void *)self) - priv_size);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030031}
32
33static size_t symbol__fprintf(struct symbol *self, FILE *fp)
34{
35 return fprintf(fp, " %llx-%llx %s\n",
36 self->start, self->end, self->name);
37}
38
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -030039struct dso *dso__new(const char *name, unsigned int sym_priv_size)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030040{
41 struct dso *self = malloc(sizeof(*self) + strlen(name) + 1);
42
43 if (self != NULL) {
44 strcpy(self->name, name);
45 self->syms = RB_ROOT;
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -030046 self->sym_priv_size = sym_priv_size;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030047 }
48
49 return self;
50}
51
52static void dso__delete_symbols(struct dso *self)
53{
54 struct symbol *pos;
55 struct rb_node *next = rb_first(&self->syms);
56
57 while (next) {
58 pos = rb_entry(next, struct symbol, rb_node);
59 next = rb_next(&pos->rb_node);
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -030060 symbol__delete(pos, self->sym_priv_size);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030061 }
62}
63
64void dso__delete(struct dso *self)
65{
66 dso__delete_symbols(self);
67 free(self);
68}
69
70static void dso__insert_symbol(struct dso *self, struct symbol *sym)
71{
72 struct rb_node **p = &self->syms.rb_node;
73 struct rb_node *parent = NULL;
74 const uint64_t ip = sym->start;
75 struct symbol *s;
76
77 while (*p != NULL) {
78 parent = *p;
79 s = rb_entry(parent, struct symbol, rb_node);
80 if (ip < s->start)
81 p = &(*p)->rb_left;
82 else
83 p = &(*p)->rb_right;
84 }
85 rb_link_node(&sym->rb_node, parent, p);
86 rb_insert_color(&sym->rb_node, &self->syms);
87}
88
89struct symbol *dso__find_symbol(struct dso *self, uint64_t ip)
90{
91 struct rb_node *n;
92
93 if (self == NULL)
94 return NULL;
95
96 n = self->syms.rb_node;
97
98 while (n) {
99 struct symbol *s = rb_entry(n, struct symbol, rb_node);
100
101 if (ip < s->start)
102 n = n->rb_left;
103 else if (ip > s->end)
104 n = n->rb_right;
105 else
106 return s;
107 }
108
109 return NULL;
110}
111
112size_t dso__fprintf(struct dso *self, FILE *fp)
113{
114 size_t ret = fprintf(fp, "dso: %s\n", self->name);
115
116 struct rb_node *nd;
117 for (nd = rb_first(&self->syms); nd; nd = rb_next(nd)) {
118 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
119 ret += symbol__fprintf(pos, fp);
120 }
121
122 return ret;
123}
124
125static int hex(char ch)
126{
127 if ((ch >= '0') && (ch <= '9'))
128 return ch - '0';
129 if ((ch >= 'a') && (ch <= 'f'))
130 return ch - 'a' + 10;
131 if ((ch >= 'A') && (ch <= 'F'))
132 return ch - 'A' + 10;
133 return -1;
134}
135
136/*
137 * While we find nice hex chars, build a long_val.
138 * Return number of chars processed.
139 */
140static int hex2long(char *ptr, unsigned long *long_val)
141{
142 const char *p = ptr;
143 *long_val = 0;
144
145 while (*p) {
146 const int hex_val = hex(*p);
147
148 if (hex_val < 0)
149 break;
150
151 *long_val = (*long_val << 4) | hex_val;
152 p++;
153 }
154
155 return p - ptr;
156}
157
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -0300158static int dso__load_kallsyms(struct dso *self)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300159{
160 struct rb_node *nd, *prevnd;
161 char *line = NULL;
162 size_t n;
163 FILE *file = fopen("/proc/kallsyms", "r");
164
165 if (file == NULL)
166 goto out_failure;
167
168 while (!feof(file)) {
169 unsigned long start;
170 struct symbol *sym;
171 int line_len, len;
172 char symbol_type;
173
174 line_len = getline(&line, &n, file);
175 if (line_len < 0)
176 break;
177
178 if (!line)
179 goto out_failure;
180
181 line[--line_len] = '\0'; /* \n */
182
183 len = hex2long(line, &start);
184
185 len++;
186 if (len + 2 >= line_len)
187 continue;
188
189 symbol_type = toupper(line[len]);
190 /*
191 * We're interested only in code ('T'ext)
192 */
193 if (symbol_type != 'T' && symbol_type != 'W')
194 continue;
195 /*
196 * Well fix up the end later, when we have all sorted.
197 */
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -0300198 sym = symbol__new(start, 0xdead, line + len + 2,
199 self->sym_priv_size);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300200
201 if (sym == NULL)
202 goto out_delete_line;
203
204 dso__insert_symbol(self, sym);
205 }
206
207 /*
208 * Now that we have all sorted out, just set the ->end of all
209 * symbols
210 */
211 prevnd = rb_first(&self->syms);
212
213 if (prevnd == NULL)
214 goto out_delete_line;
215
216 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
217 struct symbol *prev = rb_entry(prevnd, struct symbol, rb_node),
218 *curr = rb_entry(nd, struct symbol, rb_node);
219
220 prev->end = curr->start - 1;
221 prevnd = nd;
222 }
223
224 free(line);
225 fclose(file);
226
227 return 0;
228
229out_delete_line:
230 free(line);
231out_failure:
232 return -1;
233}
234
235/**
236 * elf_symtab__for_each_symbol - iterate thru all the symbols
237 *
238 * @self: struct elf_symtab instance to iterate
239 * @index: uint32_t index
240 * @sym: GElf_Sym iterator
241 */
242#define elf_symtab__for_each_symbol(syms, nr_syms, index, sym) \
243 for (index = 0, gelf_getsym(syms, index, &sym);\
244 index < nr_syms; \
245 index++, gelf_getsym(syms, index, &sym))
246
247static inline uint8_t elf_sym__type(const GElf_Sym *sym)
248{
249 return GELF_ST_TYPE(sym->st_info);
250}
251
252static inline int elf_sym__is_function(const GElf_Sym *sym)
253{
254 return elf_sym__type(sym) == STT_FUNC &&
255 sym->st_name != 0 &&
256 sym->st_shndx != SHN_UNDEF &&
257 sym->st_size != 0;
258}
259
260static inline const char *elf_sym__name(const GElf_Sym *sym,
261 const Elf_Data *symstrs)
262{
263 return symstrs->d_buf + sym->st_name;
264}
265
266static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
267 GElf_Shdr *shp, const char *name,
268 size_t *index)
269{
270 Elf_Scn *sec = NULL;
271 size_t cnt = 1;
272
273 while ((sec = elf_nextscn(elf, sec)) != NULL) {
274 char *str;
275
276 gelf_getshdr(sec, shp);
277 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
278 if (!strcmp(name, str)) {
279 if (index)
280 *index = cnt;
281 break;
282 }
283 ++cnt;
284 }
285
286 return sec;
287}
288
289static int dso__load_sym(struct dso *self, int fd, const char *name)
290{
291 Elf_Data *symstrs;
292 uint32_t nr_syms;
293 int err = -1;
294 uint32_t index;
295 GElf_Ehdr ehdr;
296 GElf_Shdr shdr;
297 Elf_Data *syms;
298 GElf_Sym sym;
299 Elf_Scn *sec;
300 Elf *elf;
301 int nr = 0;
302
303 elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
304 if (elf == NULL) {
305 fprintf(stderr, "%s: cannot read %s ELF file.\n",
306 __func__, name);
307 goto out_close;
308 }
309
310 if (gelf_getehdr(elf, &ehdr) == NULL) {
311 fprintf(stderr, "%s: cannot get elf header.\n", __func__);
312 goto out_elf_end;
313 }
314
315 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
316 if (sec == NULL)
317 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
318
319 if (sec == NULL)
320 goto out_elf_end;
321
322 syms = elf_getdata(sec, NULL);
323 if (syms == NULL)
324 goto out_elf_end;
325
326 sec = elf_getscn(elf, shdr.sh_link);
327 if (sec == NULL)
328 goto out_elf_end;
329
330 symstrs = elf_getdata(sec, NULL);
331 if (symstrs == NULL)
332 goto out_elf_end;
333
334 nr_syms = shdr.sh_size / shdr.sh_entsize;
335
336 elf_symtab__for_each_symbol(syms, nr_syms, index, sym) {
337 struct symbol *f;
338
339 if (!elf_sym__is_function(&sym))
340 continue;
341
342 sec = elf_getscn(elf, sym.st_shndx);
343 if (!sec)
344 goto out_elf_end;
345
346 gelf_getshdr(sec, &shdr);
347 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
348
349 f = symbol__new(sym.st_value, sym.st_size,
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -0300350 elf_sym__name(&sym, symstrs),
351 self->sym_priv_size);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300352 if (!f)
353 goto out_elf_end;
354
355 dso__insert_symbol(self, f);
356
357 nr++;
358 }
359
360 err = nr;
361out_elf_end:
362 elf_end(elf);
363out_close:
364 return err;
365}
366
367int dso__load(struct dso *self)
368{
369 int size = strlen(self->name) + sizeof("/usr/lib/debug%s.debug");
370 char *name = malloc(size);
371 int variant = 0;
372 int ret = -1;
373 int fd;
374
375 if (!name)
376 return -1;
377
378more:
379 do {
380 switch (variant) {
381 case 0: /* Fedora */
382 snprintf(name, size, "/usr/lib/debug%s.debug", self->name);
383 break;
384 case 1: /* Ubuntu */
385 snprintf(name, size, "/usr/lib/debug%s", self->name);
386 break;
387 case 2: /* Sane people */
388 snprintf(name, size, "%s", self->name);
389 break;
390
391 default:
392 goto out;
393 }
394 variant++;
395
396 fd = open(name, O_RDONLY);
397 } while (fd < 0);
398
399 ret = dso__load_sym(self, fd, name);
400 close(fd);
401
402 /*
403 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
404 */
405 if (!ret)
406 goto more;
407
408out:
409 free(name);
410 return ret;
411}
412
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -0300413static int dso__load_vmlinux(struct dso *self, const char *vmlinux)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300414{
415 int err, fd = open(vmlinux, O_RDONLY);
416
417 if (fd < 0)
418 return -1;
419
420 err = dso__load_sym(self, fd, vmlinux);
421 close(fd);
422
423 return err;
424}
425
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -0300426int dso__load_kernel(struct dso *self, const char *vmlinux)
427{
428 int err = -1;
429
430 if (vmlinux)
431 err = dso__load_vmlinux(self, vmlinux);
432
433 if (err)
434 err = dso__load_kallsyms(self);
435
436 return err;
437}
438
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300439void symbol__init(void)
440{
441 elf_version(EV_CURRENT);
442}