perf symbols: Properly align symbol_conf.priv_size
[deliverable/linux.git] / tools / perf / util / symbol.c
CommitLineData
5aab621b
ACM
1#define _GNU_SOURCE
2#include <ctype.h>
3#include <dirent.h>
4#include <errno.h>
5#include <libgen.h>
6#include <stdlib.h>
7#include <stdio.h>
8#include <string.h>
9#include <sys/types.h>
10#include <sys/stat.h>
11#include <sys/param.h>
12#include <fcntl.h>
13#include <unistd.h>
9486aa38 14#include <inttypes.h>
b36f19d5 15#include "build-id.h"
8a6c5b26 16#include "debug.h"
a2928c42 17#include "symbol.h"
5aab621b 18#include "strlist.h"
a2928c42
ACM
19
20#include <libelf.h>
21#include <gelf.h>
22#include <elf.h>
f1617b40 23#include <limits.h>
439d473b 24#include <sys/utsname.h>
2cdbc46d 25
3b01a413
ACM
26#ifndef KSYM_NAME_LEN
27#define KSYM_NAME_LEN 128
28#endif
29
c12e15e7
ACM
30#ifndef NT_GNU_BUILD_ID
31#define NT_GNU_BUILD_ID 3
32#endif
33
21916c38
DM
34static bool dso__build_id_equal(const struct dso *self, u8 *build_id);
35static int elf_read_build_id(Elf *elf, void *bf, size_t size);
b0da954a 36static void dsos__add(struct list_head *head, struct dso *dso);
3610583c 37static struct map *map__new2(u64 start, struct dso *dso, enum map_type type);
c338aee8 38static int dso__load_kernel_sym(struct dso *self, struct map *map,
9de89fe7 39 symbol_filter_t filter);
a1645ce1
ZY
40static int dso__load_guest_kernel_sym(struct dso *self, struct map *map,
41 symbol_filter_t filter);
cc612d81
ACM
42static int vmlinux_path__nr_entries;
43static char **vmlinux_path;
439d473b 44
75be6cf4 45struct symbol_conf symbol_conf = {
d599db3f 46 .exclude_other = true,
b32d133a
ACM
47 .use_modules = true,
48 .try_vmlinux_path = true,
ec5761ea 49 .symfs = "",
b32d133a
ACM
50};
51
8a6c5b26
ACM
52int dso__name_len(const struct dso *self)
53{
54 if (verbose)
55 return self->long_name_len;
56
57 return self->short_name_len;
58}
59
3610583c
ACM
60bool dso__loaded(const struct dso *self, enum map_type type)
61{
62 return self->loaded & (1 << type);
63}
64
79406cd7
ACM
65bool dso__sorted_by_name(const struct dso *self, enum map_type type)
66{
67 return self->sorted_by_name & (1 << type);
68}
69
79406cd7
ACM
70static void dso__set_sorted_by_name(struct dso *self, enum map_type type)
71{
72 self->sorted_by_name |= (1 << type);
73}
74
36a3e646 75bool symbol_type__is_a(char symbol_type, enum map_type map_type)
6893d4ee
ACM
76{
77 switch (map_type) {
78 case MAP__FUNCTION:
79 return symbol_type == 'T' || symbol_type == 'W';
f1dfa0b1
ACM
80 case MAP__VARIABLE:
81 return symbol_type == 'D' || symbol_type == 'd';
6893d4ee
ACM
82 default:
83 return false;
84 }
85}
86
fcf1203a 87static void symbols__fixup_end(struct rb_root *self)
af427bf5 88{
fcf1203a 89 struct rb_node *nd, *prevnd = rb_first(self);
2e538c4a 90 struct symbol *curr, *prev;
af427bf5
ACM
91
92 if (prevnd == NULL)
93 return;
94
2e538c4a
ACM
95 curr = rb_entry(prevnd, struct symbol, rb_node);
96
af427bf5 97 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
2e538c4a
ACM
98 prev = curr;
99 curr = rb_entry(nd, struct symbol, rb_node);
af427bf5 100
3b01a413 101 if (prev->end == prev->start && prev->end != curr->start)
af427bf5 102 prev->end = curr->start - 1;
af427bf5 103 }
2e538c4a
ACM
104
105 /* Last entry */
106 if (curr->end == curr->start)
107 curr->end = roundup(curr->start, 4096);
af427bf5
ACM
108}
109
9958e1f0 110static void __map_groups__fixup_end(struct map_groups *self, enum map_type type)
af427bf5
ACM
111{
112 struct map *prev, *curr;
95011c60 113 struct rb_node *nd, *prevnd = rb_first(&self->maps[type]);
af427bf5
ACM
114
115 if (prevnd == NULL)
116 return;
117
118 curr = rb_entry(prevnd, struct map, rb_node);
af427bf5
ACM
119
120 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
121 prev = curr;
122 curr = rb_entry(nd, struct map, rb_node);
123 prev->end = curr->start - 1;
2e538c4a 124 }
90c83218
ACM
125
126 /*
127 * We still haven't the actual symbols, so guess the
128 * last map final address.
129 */
9d1faba5 130 curr->end = ~0ULL;
af427bf5
ACM
131}
132
9958e1f0 133static void map_groups__fixup_end(struct map_groups *self)
23ea4a3f
ACM
134{
135 int i;
136 for (i = 0; i < MAP__NR_TYPES; ++i)
9958e1f0 137 __map_groups__fixup_end(self, i);
23ea4a3f
ACM
138}
139
c408fedf
ACM
140static struct symbol *symbol__new(u64 start, u64 len, u8 binding,
141 const char *name)
a2928c42 142{
0085c954 143 size_t namelen = strlen(name) + 1;
5aab621b
ACM
144 struct symbol *self = calloc(1, (symbol_conf.priv_size +
145 sizeof(*self) + namelen));
36479484 146 if (self == NULL)
0b73da3f
IM
147 return NULL;
148
75be6cf4
ACM
149 if (symbol_conf.priv_size)
150 self = ((void *)self) + symbol_conf.priv_size;
36479484 151
fefb0b94
ACM
152 self->start = start;
153 self->end = len ? start + len - 1 : start;
c408fedf 154 self->binding = binding;
fefb0b94 155 self->namelen = namelen - 1;
e4204992 156
9486aa38 157 pr_debug4("%s: %s %#" PRIx64 "-%#" PRIx64 "\n", __func__, name, start, self->end);
e4204992 158
0b73da3f 159 memcpy(self->name, name, namelen);
a2928c42
ACM
160
161 return self;
162}
163
628ada0c 164void symbol__delete(struct symbol *self)
a2928c42 165{
75be6cf4 166 free(((void *)self) - symbol_conf.priv_size);
a2928c42
ACM
167}
168
169static size_t symbol__fprintf(struct symbol *self, FILE *fp)
170{
9486aa38 171 return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %c %s\n",
c408fedf
ACM
172 self->start, self->end,
173 self->binding == STB_GLOBAL ? 'g' :
174 self->binding == STB_LOCAL ? 'l' : 'w',
175 self->name);
a2928c42
ACM
176}
177
b7cece76 178void dso__set_long_name(struct dso *self, char *name)
cfc10d3b 179{
ef6ae724
ACM
180 if (name == NULL)
181 return;
cfc10d3b
ACM
182 self->long_name = name;
183 self->long_name_len = strlen(name);
184}
185
b63be8d7
ACM
186static void dso__set_short_name(struct dso *self, const char *name)
187{
188 if (name == NULL)
189 return;
190 self->short_name = name;
191 self->short_name_len = strlen(name);
192}
193
cfc10d3b
ACM
194static void dso__set_basename(struct dso *self)
195{
b63be8d7 196 dso__set_short_name(self, basename(self->long_name));
cfc10d3b
ACM
197}
198
00a192b3 199struct dso *dso__new(const char *name)
a2928c42 200{
5aab621b 201 struct dso *self = calloc(1, sizeof(*self) + strlen(name) + 1);
a2928c42
ACM
202
203 if (self != NULL) {
6a4694a4 204 int i;
a2928c42 205 strcpy(self->name, name);
cfc10d3b 206 dso__set_long_name(self, self->name);
b63be8d7 207 dso__set_short_name(self, self->name);
6a4694a4 208 for (i = 0; i < MAP__NR_TYPES; ++i)
79406cd7 209 self->symbols[i] = self->symbol_names[i] = RB_ROOT;
878b439d 210 self->symtab_type = SYMTAB__NOT_FOUND;
8d06367f 211 self->loaded = 0;
79406cd7 212 self->sorted_by_name = 0;
8d06367f 213 self->has_build_id = 0;
a1645ce1 214 self->kernel = DSO_TYPE_USER;
0ab061cd 215 INIT_LIST_HEAD(&self->node);
a2928c42
ACM
216 }
217
218 return self;
219}
220
fcf1203a 221static void symbols__delete(struct rb_root *self)
a2928c42
ACM
222{
223 struct symbol *pos;
fcf1203a 224 struct rb_node *next = rb_first(self);
a2928c42
ACM
225
226 while (next) {
227 pos = rb_entry(next, struct symbol, rb_node);
228 next = rb_next(&pos->rb_node);
fcf1203a 229 rb_erase(&pos->rb_node, self);
00a192b3 230 symbol__delete(pos);
a2928c42
ACM
231 }
232}
233
234void dso__delete(struct dso *self)
235{
6a4694a4
ACM
236 int i;
237 for (i = 0; i < MAP__NR_TYPES; ++i)
238 symbols__delete(&self->symbols[i]);
6e406257
ACM
239 if (self->sname_alloc)
240 free((char *)self->short_name);
241 if (self->lname_alloc)
439d473b 242 free(self->long_name);
a2928c42
ACM
243 free(self);
244}
245
8d06367f
ACM
246void dso__set_build_id(struct dso *self, void *build_id)
247{
248 memcpy(self->build_id, build_id, sizeof(self->build_id));
249 self->has_build_id = 1;
250}
251
fcf1203a 252static void symbols__insert(struct rb_root *self, struct symbol *sym)
a2928c42 253{
fcf1203a 254 struct rb_node **p = &self->rb_node;
a2928c42 255 struct rb_node *parent = NULL;
9cffa8d5 256 const u64 ip = sym->start;
a2928c42
ACM
257 struct symbol *s;
258
259 while (*p != NULL) {
260 parent = *p;
261 s = rb_entry(parent, struct symbol, rb_node);
262 if (ip < s->start)
263 p = &(*p)->rb_left;
264 else
265 p = &(*p)->rb_right;
266 }
267 rb_link_node(&sym->rb_node, parent, p);
fcf1203a 268 rb_insert_color(&sym->rb_node, self);
a2928c42
ACM
269}
270
fcf1203a 271static struct symbol *symbols__find(struct rb_root *self, u64 ip)
a2928c42
ACM
272{
273 struct rb_node *n;
274
275 if (self == NULL)
276 return NULL;
277
fcf1203a 278 n = self->rb_node;
a2928c42
ACM
279
280 while (n) {
281 struct symbol *s = rb_entry(n, struct symbol, rb_node);
282
283 if (ip < s->start)
284 n = n->rb_left;
285 else if (ip > s->end)
286 n = n->rb_right;
287 else
288 return s;
289 }
290
291 return NULL;
292}
293
79406cd7
ACM
294struct symbol_name_rb_node {
295 struct rb_node rb_node;
296 struct symbol sym;
297};
298
299static void symbols__insert_by_name(struct rb_root *self, struct symbol *sym)
300{
301 struct rb_node **p = &self->rb_node;
302 struct rb_node *parent = NULL;
02a9d037
RV
303 struct symbol_name_rb_node *symn, *s;
304
305 symn = container_of(sym, struct symbol_name_rb_node, sym);
79406cd7
ACM
306
307 while (*p != NULL) {
308 parent = *p;
309 s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
310 if (strcmp(sym->name, s->sym.name) < 0)
311 p = &(*p)->rb_left;
312 else
313 p = &(*p)->rb_right;
314 }
315 rb_link_node(&symn->rb_node, parent, p);
316 rb_insert_color(&symn->rb_node, self);
317}
318
319static void symbols__sort_by_name(struct rb_root *self, struct rb_root *source)
320{
321 struct rb_node *nd;
322
323 for (nd = rb_first(source); nd; nd = rb_next(nd)) {
324 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
325 symbols__insert_by_name(self, pos);
326 }
327}
328
329static struct symbol *symbols__find_by_name(struct rb_root *self, const char *name)
330{
331 struct rb_node *n;
332
333 if (self == NULL)
334 return NULL;
335
336 n = self->rb_node;
337
338 while (n) {
339 struct symbol_name_rb_node *s;
340 int cmp;
341
342 s = rb_entry(n, struct symbol_name_rb_node, rb_node);
343 cmp = strcmp(name, s->sym.name);
344
345 if (cmp < 0)
346 n = n->rb_left;
347 else if (cmp > 0)
348 n = n->rb_right;
349 else
350 return &s->sym;
351 }
352
353 return NULL;
354}
355
356struct symbol *dso__find_symbol(struct dso *self,
357 enum map_type type, u64 addr)
fcf1203a 358{
6a4694a4 359 return symbols__find(&self->symbols[type], addr);
fcf1203a
ACM
360}
361
79406cd7
ACM
362struct symbol *dso__find_symbol_by_name(struct dso *self, enum map_type type,
363 const char *name)
364{
365 return symbols__find_by_name(&self->symbol_names[type], name);
366}
367
368void dso__sort_by_name(struct dso *self, enum map_type type)
369{
370 dso__set_sorted_by_name(self, type);
371 return symbols__sort_by_name(&self->symbol_names[type],
372 &self->symbols[type]);
373}
374
ef12a141 375int build_id__sprintf(const u8 *self, int len, char *bf)
a2928c42 376{
8d06367f 377 char *bid = bf;
ef12a141 378 const u8 *raw = self;
8d06367f 379 int i;
a2928c42 380
8d06367f
ACM
381 for (i = 0; i < len; ++i) {
382 sprintf(bid, "%02x", *raw);
383 ++raw;
384 bid += 2;
385 }
386
387 return raw - self;
388}
389
9e03eb2d 390size_t dso__fprintf_buildid(struct dso *self, FILE *fp)
8d06367f
ACM
391{
392 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
8d06367f
ACM
393
394 build_id__sprintf(self->build_id, sizeof(self->build_id), sbuild_id);
9e03eb2d
ACM
395 return fprintf(fp, "%s", sbuild_id);
396}
397
90f18e63
SD
398size_t dso__fprintf_symbols_by_name(struct dso *self, enum map_type type, FILE *fp)
399{
400 size_t ret = 0;
401 struct rb_node *nd;
402 struct symbol_name_rb_node *pos;
403
404 for (nd = rb_first(&self->symbol_names[type]); nd; nd = rb_next(nd)) {
405 pos = rb_entry(nd, struct symbol_name_rb_node, rb_node);
406 fprintf(fp, "%s\n", pos->sym.name);
407 }
408
409 return ret;
410}
411
95011c60 412size_t dso__fprintf(struct dso *self, enum map_type type, FILE *fp)
9e03eb2d
ACM
413{
414 struct rb_node *nd;
415 size_t ret = fprintf(fp, "dso: %s (", self->short_name);
416
3846df2e
ACM
417 if (self->short_name != self->long_name)
418 ret += fprintf(fp, "%s, ", self->long_name);
419 ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
420 self->loaded ? "" : "NOT ");
9e03eb2d 421 ret += dso__fprintf_buildid(self, fp);
6a4694a4 422 ret += fprintf(fp, ")\n");
95011c60
ACM
423 for (nd = rb_first(&self->symbols[type]); nd; nd = rb_next(nd)) {
424 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
425 ret += symbol__fprintf(pos, fp);
a2928c42
ACM
426 }
427
428 return ret;
429}
430
9e201442
ACM
431int kallsyms__parse(const char *filename, void *arg,
432 int (*process_symbol)(void *arg, const char *name,
3b01a413 433 char type, u64 start, u64 end))
a2928c42 434{
a2928c42
ACM
435 char *line = NULL;
436 size_t n;
3b01a413
ACM
437 int err = -1;
438 u64 prev_start = 0;
439 char prev_symbol_type = 0;
440 char *prev_symbol_name;
9e201442 441 FILE *file = fopen(filename, "r");
a2928c42
ACM
442
443 if (file == NULL)
444 goto out_failure;
445
3b01a413
ACM
446 prev_symbol_name = malloc(KSYM_NAME_LEN);
447 if (prev_symbol_name == NULL)
448 goto out_close;
449
450 err = 0;
451
a2928c42 452 while (!feof(file)) {
9cffa8d5 453 u64 start;
a2928c42
ACM
454 int line_len, len;
455 char symbol_type;
2e538c4a 456 char *symbol_name;
a2928c42
ACM
457
458 line_len = getline(&line, &n, file);
a1645ce1 459 if (line_len < 0 || !line)
a2928c42
ACM
460 break;
461
a2928c42
ACM
462 line[--line_len] = '\0'; /* \n */
463
a0055ae2 464 len = hex2u64(line, &start);
a2928c42
ACM
465
466 len++;
467 if (len + 2 >= line_len)
468 continue;
469
470 symbol_type = toupper(line[len]);
3b01a413
ACM
471 len += 2;
472 symbol_name = line + len;
473 len = line_len - len;
682b335a 474
3b01a413
ACM
475 if (len >= KSYM_NAME_LEN) {
476 err = -1;
682b335a 477 break;
3b01a413
ACM
478 }
479
480 if (prev_symbol_type) {
481 u64 end = start;
482 if (end != prev_start)
483 --end;
484 err = process_symbol(arg, prev_symbol_name,
485 prev_symbol_type, prev_start, end);
486 if (err)
487 break;
488 }
489
490 memcpy(prev_symbol_name, symbol_name, len + 1);
491 prev_symbol_type = symbol_type;
492 prev_start = start;
2e538c4a
ACM
493 }
494
3b01a413 495 free(prev_symbol_name);
2e538c4a 496 free(line);
3b01a413 497out_close:
2e538c4a 498 fclose(file);
682b335a 499 return err;
2e538c4a 500
2e538c4a
ACM
501out_failure:
502 return -1;
503}
504
682b335a
ACM
505struct process_kallsyms_args {
506 struct map *map;
507 struct dso *dso;
508};
509
c408fedf
ACM
510static u8 kallsyms2elf_type(char type)
511{
512 if (type == 'W')
513 return STB_WEAK;
514
515 return isupper(type) ? STB_GLOBAL : STB_LOCAL;
516}
517
682b335a 518static int map__process_kallsym_symbol(void *arg, const char *name,
3b01a413 519 char type, u64 start, u64 end)
682b335a
ACM
520{
521 struct symbol *sym;
522 struct process_kallsyms_args *a = arg;
523 struct rb_root *root = &a->dso->symbols[a->map->type];
524
525 if (!symbol_type__is_a(type, a->map->type))
526 return 0;
527
3b01a413
ACM
528 sym = symbol__new(start, end - start + 1,
529 kallsyms2elf_type(type), name);
682b335a
ACM
530 if (sym == NULL)
531 return -ENOMEM;
532 /*
533 * We will pass the symbols to the filter later, in
534 * map__split_kallsyms, when we have split the maps per module
535 */
536 symbols__insert(root, sym);
a1645ce1 537
682b335a
ACM
538 return 0;
539}
540
541/*
542 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
543 * so that we can in the next step set the symbol ->end address and then
544 * call kernel_maps__split_kallsyms.
545 */
9e201442
ACM
546static int dso__load_all_kallsyms(struct dso *self, const char *filename,
547 struct map *map)
682b335a
ACM
548{
549 struct process_kallsyms_args args = { .map = map, .dso = self, };
9e201442 550 return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
682b335a
ACM
551}
552
2e538c4a
ACM
553/*
554 * Split the symbols into maps, making sure there are no overlaps, i.e. the
555 * kernel range is broken in several maps, named [kernel].N, as we don't have
556 * the original ELF section names vmlinux have.
557 */
9958e1f0 558static int dso__split_kallsyms(struct dso *self, struct map *map,
9de89fe7 559 symbol_filter_t filter)
2e538c4a 560{
9de89fe7 561 struct map_groups *kmaps = map__kmap(map)->kmaps;
23346f21 562 struct machine *machine = kmaps->machine;
4e06255f 563 struct map *curr_map = map;
2e538c4a 564 struct symbol *pos;
8a953312 565 int count = 0, moved = 0;
4e06255f
ACM
566 struct rb_root *root = &self->symbols[map->type];
567 struct rb_node *next = rb_first(root);
2e538c4a
ACM
568 int kernel_range = 0;
569
570 while (next) {
571 char *module;
572
573 pos = rb_entry(next, struct symbol, rb_node);
574 next = rb_next(&pos->rb_node);
575
576 module = strchr(pos->name, '\t');
577 if (module) {
75be6cf4 578 if (!symbol_conf.use_modules)
1de8e245
ACM
579 goto discard_symbol;
580
2e538c4a
ACM
581 *module++ = '\0';
582
b7cece76 583 if (strcmp(curr_map->dso->short_name, module)) {
a1645ce1 584 if (curr_map != map &&
23346f21
ACM
585 self->kernel == DSO_TYPE_GUEST_KERNEL &&
586 machine__is_default_guest(machine)) {
a1645ce1
ZY
587 /*
588 * We assume all symbols of a module are
589 * continuous in * kallsyms, so curr_map
590 * points to a module and all its
591 * symbols are in its kmap. Mark it as
592 * loaded.
593 */
594 dso__set_loaded(curr_map->dso,
595 curr_map->type);
596 }
597
598 curr_map = map_groups__find_by_name(kmaps,
599 map->type, module);
4e06255f 600 if (curr_map == NULL) {
2f51903b 601 pr_debug("%s/proc/{kallsyms,modules} "
b7cece76 602 "inconsistency while looking "
a1645ce1 603 "for \"%s\" module!\n",
23346f21 604 machine->root_dir, module);
a1645ce1
ZY
605 curr_map = map;
606 goto discard_symbol;
af427bf5 607 }
b7cece76 608
a1645ce1 609 if (curr_map->dso->loaded &&
23346f21 610 !machine__is_default_guest(machine))
b7cece76 611 goto discard_symbol;
af427bf5 612 }
2e538c4a
ACM
613 /*
614 * So that we look just like we get from .ko files,
615 * i.e. not prelinked, relative to map->start.
616 */
4e06255f
ACM
617 pos->start = curr_map->map_ip(curr_map, pos->start);
618 pos->end = curr_map->map_ip(curr_map, pos->end);
619 } else if (curr_map != map) {
2e538c4a
ACM
620 char dso_name[PATH_MAX];
621 struct dso *dso;
622
8a953312
ACM
623 if (count == 0) {
624 curr_map = map;
625 goto filter_symbol;
626 }
627
a1645ce1
ZY
628 if (self->kernel == DSO_TYPE_GUEST_KERNEL)
629 snprintf(dso_name, sizeof(dso_name),
630 "[guest.kernel].%d",
631 kernel_range++);
632 else
633 snprintf(dso_name, sizeof(dso_name),
634 "[kernel].%d",
635 kernel_range++);
2e538c4a 636
00a192b3 637 dso = dso__new(dso_name);
2e538c4a
ACM
638 if (dso == NULL)
639 return -1;
640
a1645ce1
ZY
641 dso->kernel = self->kernel;
642
4e06255f 643 curr_map = map__new2(pos->start, dso, map->type);
37fe5fcb 644 if (curr_map == NULL) {
2e538c4a
ACM
645 dso__delete(dso);
646 return -1;
647 }
a2928c42 648
4e06255f 649 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
9de89fe7 650 map_groups__insert(kmaps, curr_map);
2e538c4a
ACM
651 ++kernel_range;
652 }
8a953312 653filter_symbol:
4e06255f 654 if (filter && filter(curr_map, pos)) {
1de8e245 655discard_symbol: rb_erase(&pos->rb_node, root);
00a192b3 656 symbol__delete(pos);
2e538c4a 657 } else {
4e06255f
ACM
658 if (curr_map != map) {
659 rb_erase(&pos->rb_node, root);
660 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
8a953312
ACM
661 ++moved;
662 } else
663 ++count;
9974f496 664 }
a2928c42
ACM
665 }
666
a1645ce1
ZY
667 if (curr_map != map &&
668 self->kernel == DSO_TYPE_GUEST_KERNEL &&
23346f21 669 machine__is_default_guest(kmaps->machine)) {
a1645ce1
ZY
670 dso__set_loaded(curr_map->dso, curr_map->type);
671 }
672
8a953312 673 return count + moved;
2e538c4a 674}
a2928c42 675
9de89fe7
ACM
676int dso__load_kallsyms(struct dso *self, const char *filename,
677 struct map *map, symbol_filter_t filter)
2e538c4a 678{
9e201442 679 if (dso__load_all_kallsyms(self, filename, map) < 0)
2e538c4a
ACM
680 return -1;
681
a1645ce1 682 if (self->kernel == DSO_TYPE_GUEST_KERNEL)
878b439d 683 self->symtab_type = SYMTAB__GUEST_KALLSYMS;
a1645ce1 684 else
878b439d 685 self->symtab_type = SYMTAB__KALLSYMS;
2e538c4a 686
9de89fe7 687 return dso__split_kallsyms(self, map, filter);
af427bf5
ACM
688}
689
439d473b 690static int dso__load_perf_map(struct dso *self, struct map *map,
6beba7ad 691 symbol_filter_t filter)
80d496be
PE
692{
693 char *line = NULL;
694 size_t n;
695 FILE *file;
696 int nr_syms = 0;
697
439d473b 698 file = fopen(self->long_name, "r");
80d496be
PE
699 if (file == NULL)
700 goto out_failure;
701
702 while (!feof(file)) {
9cffa8d5 703 u64 start, size;
80d496be
PE
704 struct symbol *sym;
705 int line_len, len;
706
707 line_len = getline(&line, &n, file);
708 if (line_len < 0)
709 break;
710
711 if (!line)
712 goto out_failure;
713
714 line[--line_len] = '\0'; /* \n */
715
716 len = hex2u64(line, &start);
717
718 len++;
719 if (len + 2 >= line_len)
720 continue;
721
722 len += hex2u64(line + len, &size);
723
724 len++;
725 if (len + 2 >= line_len)
726 continue;
727
c408fedf 728 sym = symbol__new(start, size, STB_GLOBAL, line + len);
80d496be
PE
729
730 if (sym == NULL)
731 goto out_delete_line;
732
439d473b 733 if (filter && filter(map, sym))
00a192b3 734 symbol__delete(sym);
80d496be 735 else {
6a4694a4 736 symbols__insert(&self->symbols[map->type], sym);
80d496be
PE
737 nr_syms++;
738 }
739 }
740
741 free(line);
742 fclose(file);
743
744 return nr_syms;
745
746out_delete_line:
747 free(line);
748out_failure:
749 return -1;
750}
751
a2928c42
ACM
752/**
753 * elf_symtab__for_each_symbol - iterate thru all the symbols
754 *
755 * @self: struct elf_symtab instance to iterate
83a0944f 756 * @idx: uint32_t idx
a2928c42
ACM
757 * @sym: GElf_Sym iterator
758 */
83a0944f
IM
759#define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
760 for (idx = 0, gelf_getsym(syms, idx, &sym);\
761 idx < nr_syms; \
762 idx++, gelf_getsym(syms, idx, &sym))
a2928c42
ACM
763
764static inline uint8_t elf_sym__type(const GElf_Sym *sym)
765{
766 return GELF_ST_TYPE(sym->st_info);
767}
768
769static inline int elf_sym__is_function(const GElf_Sym *sym)
770{
771 return elf_sym__type(sym) == STT_FUNC &&
772 sym->st_name != 0 &&
81833130 773 sym->st_shndx != SHN_UNDEF;
a2928c42
ACM
774}
775
f1dfa0b1
ACM
776static inline bool elf_sym__is_object(const GElf_Sym *sym)
777{
778 return elf_sym__type(sym) == STT_OBJECT &&
779 sym->st_name != 0 &&
780 sym->st_shndx != SHN_UNDEF;
781}
782
6cfcc53e
MG
783static inline int elf_sym__is_label(const GElf_Sym *sym)
784{
785 return elf_sym__type(sym) == STT_NOTYPE &&
786 sym->st_name != 0 &&
787 sym->st_shndx != SHN_UNDEF &&
788 sym->st_shndx != SHN_ABS;
789}
790
791static inline const char *elf_sec__name(const GElf_Shdr *shdr,
792 const Elf_Data *secstrs)
793{
794 return secstrs->d_buf + shdr->sh_name;
795}
796
797static inline int elf_sec__is_text(const GElf_Shdr *shdr,
798 const Elf_Data *secstrs)
799{
800 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
801}
802
f1dfa0b1
ACM
803static inline bool elf_sec__is_data(const GElf_Shdr *shdr,
804 const Elf_Data *secstrs)
805{
806 return strstr(elf_sec__name(shdr, secstrs), "data") != NULL;
807}
808
a2928c42
ACM
809static inline const char *elf_sym__name(const GElf_Sym *sym,
810 const Elf_Data *symstrs)
811{
812 return symstrs->d_buf + sym->st_name;
813}
814
815static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
816 GElf_Shdr *shp, const char *name,
83a0944f 817 size_t *idx)
a2928c42
ACM
818{
819 Elf_Scn *sec = NULL;
820 size_t cnt = 1;
821
822 while ((sec = elf_nextscn(elf, sec)) != NULL) {
823 char *str;
824
825 gelf_getshdr(sec, shp);
826 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
827 if (!strcmp(name, str)) {
83a0944f
IM
828 if (idx)
829 *idx = cnt;
a2928c42
ACM
830 break;
831 }
832 ++cnt;
833 }
834
835 return sec;
836}
837
8ce998d6
ACM
838#define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
839 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
840 idx < nr_entries; \
841 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
842
843#define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
844 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
845 idx < nr_entries; \
846 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
847
a25e46c4
ACM
848/*
849 * We need to check if we have a .dynsym, so that we can handle the
850 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
851 * .dynsym or .symtab).
852 * And always look at the original dso, not at debuginfo packages, that
853 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
854 */
82164161
ACM
855static int dso__synthesize_plt_symbols(struct dso *self, struct map *map,
856 symbol_filter_t filter)
8ce998d6
ACM
857{
858 uint32_t nr_rel_entries, idx;
859 GElf_Sym sym;
9cffa8d5 860 u64 plt_offset;
8ce998d6
ACM
861 GElf_Shdr shdr_plt;
862 struct symbol *f;
a25e46c4 863 GElf_Shdr shdr_rel_plt, shdr_dynsym;
8ce998d6 864 Elf_Data *reldata, *syms, *symstrs;
a25e46c4
ACM
865 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
866 size_t dynsym_idx;
867 GElf_Ehdr ehdr;
8ce998d6 868 char sympltname[1024];
a25e46c4
ACM
869 Elf *elf;
870 int nr = 0, symidx, fd, err = 0;
ec5761ea 871 char name[PATH_MAX];
a25e46c4 872
ec5761ea
DA
873 snprintf(name, sizeof(name), "%s%s",
874 symbol_conf.symfs, self->long_name);
875 fd = open(name, O_RDONLY);
a25e46c4
ACM
876 if (fd < 0)
877 goto out;
878
84087126 879 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
a25e46c4
ACM
880 if (elf == NULL)
881 goto out_close;
882
883 if (gelf_getehdr(elf, &ehdr) == NULL)
884 goto out_elf_end;
885
886 scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
887 ".dynsym", &dynsym_idx);
888 if (scn_dynsym == NULL)
889 goto out_elf_end;
8ce998d6 890
a25e46c4 891 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
8ce998d6
ACM
892 ".rela.plt", NULL);
893 if (scn_plt_rel == NULL) {
a25e46c4 894 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
8ce998d6
ACM
895 ".rel.plt", NULL);
896 if (scn_plt_rel == NULL)
a25e46c4 897 goto out_elf_end;
8ce998d6
ACM
898 }
899
a25e46c4
ACM
900 err = -1;
901
8ce998d6 902 if (shdr_rel_plt.sh_link != dynsym_idx)
a25e46c4 903 goto out_elf_end;
8ce998d6 904
a25e46c4
ACM
905 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
906 goto out_elf_end;
8ce998d6
ACM
907
908 /*
83a0944f 909 * Fetch the relocation section to find the idxes to the GOT
8ce998d6
ACM
910 * and the symbols in the .dynsym they refer to.
911 */
912 reldata = elf_getdata(scn_plt_rel, NULL);
913 if (reldata == NULL)
a25e46c4 914 goto out_elf_end;
8ce998d6
ACM
915
916 syms = elf_getdata(scn_dynsym, NULL);
917 if (syms == NULL)
a25e46c4 918 goto out_elf_end;
8ce998d6 919
a25e46c4 920 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
8ce998d6 921 if (scn_symstrs == NULL)
a25e46c4 922 goto out_elf_end;
8ce998d6
ACM
923
924 symstrs = elf_getdata(scn_symstrs, NULL);
925 if (symstrs == NULL)
a25e46c4 926 goto out_elf_end;
8ce998d6
ACM
927
928 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
929 plt_offset = shdr_plt.sh_offset;
930
931 if (shdr_rel_plt.sh_type == SHT_RELA) {
932 GElf_Rela pos_mem, *pos;
933
934 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
935 nr_rel_entries) {
936 symidx = GELF_R_SYM(pos->r_info);
937 plt_offset += shdr_plt.sh_entsize;
938 gelf_getsym(syms, symidx, &sym);
939 snprintf(sympltname, sizeof(sympltname),
940 "%s@plt", elf_sym__name(&sym, symstrs));
941
942 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
c408fedf 943 STB_GLOBAL, sympltname);
8ce998d6 944 if (!f)
a25e46c4 945 goto out_elf_end;
8ce998d6 946
82164161
ACM
947 if (filter && filter(map, f))
948 symbol__delete(f);
949 else {
6a4694a4 950 symbols__insert(&self->symbols[map->type], f);
82164161
ACM
951 ++nr;
952 }
8ce998d6
ACM
953 }
954 } else if (shdr_rel_plt.sh_type == SHT_REL) {
955 GElf_Rel pos_mem, *pos;
956 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
957 nr_rel_entries) {
958 symidx = GELF_R_SYM(pos->r_info);
959 plt_offset += shdr_plt.sh_entsize;
960 gelf_getsym(syms, symidx, &sym);
961 snprintf(sympltname, sizeof(sympltname),
962 "%s@plt", elf_sym__name(&sym, symstrs));
963
964 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
c408fedf 965 STB_GLOBAL, sympltname);
8ce998d6 966 if (!f)
a25e46c4 967 goto out_elf_end;
8ce998d6 968
82164161
ACM
969 if (filter && filter(map, f))
970 symbol__delete(f);
971 else {
6a4694a4 972 symbols__insert(&self->symbols[map->type], f);
82164161
ACM
973 ++nr;
974 }
8ce998d6 975 }
8ce998d6
ACM
976 }
977
a25e46c4
ACM
978 err = 0;
979out_elf_end:
980 elf_end(elf);
981out_close:
982 close(fd);
983
984 if (err == 0)
985 return nr;
986out:
fe2197b8
ACM
987 pr_debug("%s: problems reading %s PLT info.\n",
988 __func__, self->long_name);
a25e46c4 989 return 0;
8ce998d6
ACM
990}
991
d45868d3
ACM
992static bool elf_sym__is_a(GElf_Sym *self, enum map_type type)
993{
994 switch (type) {
995 case MAP__FUNCTION:
996 return elf_sym__is_function(self);
f1dfa0b1
ACM
997 case MAP__VARIABLE:
998 return elf_sym__is_object(self);
d45868d3
ACM
999 default:
1000 return false;
1001 }
1002}
1003
1004static bool elf_sec__is_a(GElf_Shdr *self, Elf_Data *secstrs, enum map_type type)
1005{
1006 switch (type) {
1007 case MAP__FUNCTION:
1008 return elf_sec__is_text(self, secstrs);
f1dfa0b1
ACM
1009 case MAP__VARIABLE:
1010 return elf_sec__is_data(self, secstrs);
d45868d3
ACM
1011 default:
1012 return false;
1013 }
1014}
1015
70c3856b
EM
1016static size_t elf_addr_to_index(Elf *elf, GElf_Addr addr)
1017{
1018 Elf_Scn *sec = NULL;
1019 GElf_Shdr shdr;
1020 size_t cnt = 1;
1021
1022 while ((sec = elf_nextscn(elf, sec)) != NULL) {
1023 gelf_getshdr(sec, &shdr);
1024
1025 if ((addr >= shdr.sh_addr) &&
1026 (addr < (shdr.sh_addr + shdr.sh_size)))
1027 return cnt;
1028
1029 ++cnt;
1030 }
1031
1032 return -1;
1033}
1034
9de89fe7 1035static int dso__load_sym(struct dso *self, struct map *map, const char *name,
6da80ce8
DM
1036 int fd, symbol_filter_t filter, int kmodule,
1037 int want_symtab)
a2928c42 1038{
9de89fe7 1039 struct kmap *kmap = self->kernel ? map__kmap(map) : NULL;
2e538c4a
ACM
1040 struct map *curr_map = map;
1041 struct dso *curr_dso = self;
6cfcc53e 1042 Elf_Data *symstrs, *secstrs;
a2928c42
ACM
1043 uint32_t nr_syms;
1044 int err = -1;
83a0944f 1045 uint32_t idx;
a2928c42 1046 GElf_Ehdr ehdr;
70c3856b
EM
1047 GElf_Shdr shdr, opdshdr;
1048 Elf_Data *syms, *opddata = NULL;
a2928c42 1049 GElf_Sym sym;
70c3856b 1050 Elf_Scn *sec, *sec_strndx, *opdsec;
a2928c42 1051 Elf *elf;
439d473b 1052 int nr = 0;
70c3856b 1053 size_t opdidx = 0;
a2928c42 1054
84087126 1055 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
a2928c42 1056 if (elf == NULL) {
8b1389ef 1057 pr_debug("%s: cannot read %s ELF file.\n", __func__, name);
a2928c42
ACM
1058 goto out_close;
1059 }
1060
1061 if (gelf_getehdr(elf, &ehdr) == NULL) {
8b1389ef 1062 pr_debug("%s: cannot get elf header.\n", __func__);
a2928c42
ACM
1063 goto out_elf_end;
1064 }
1065
6da80ce8 1066 /* Always reject images with a mismatched build-id: */
21916c38
DM
1067 if (self->has_build_id) {
1068 u8 build_id[BUILD_ID_SIZE];
1069
1070 if (elf_read_build_id(elf, build_id,
1071 BUILD_ID_SIZE) != BUILD_ID_SIZE)
1072 goto out_elf_end;
1073
1074 if (!dso__build_id_equal(self, build_id))
1075 goto out_elf_end;
1076 }
1077
a2928c42 1078 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
8ce998d6 1079 if (sec == NULL) {
6da80ce8
DM
1080 if (want_symtab)
1081 goto out_elf_end;
1082
a25e46c4
ACM
1083 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
1084 if (sec == NULL)
8ce998d6 1085 goto out_elf_end;
8ce998d6 1086 }
a2928c42 1087
70c3856b
EM
1088 opdsec = elf_section_by_name(elf, &ehdr, &opdshdr, ".opd", &opdidx);
1089 if (opdsec)
1090 opddata = elf_rawdata(opdsec, NULL);
1091
a2928c42
ACM
1092 syms = elf_getdata(sec, NULL);
1093 if (syms == NULL)
1094 goto out_elf_end;
1095
1096 sec = elf_getscn(elf, shdr.sh_link);
1097 if (sec == NULL)
1098 goto out_elf_end;
1099
1100 symstrs = elf_getdata(sec, NULL);
1101 if (symstrs == NULL)
1102 goto out_elf_end;
1103
6cfcc53e
MG
1104 sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
1105 if (sec_strndx == NULL)
1106 goto out_elf_end;
1107
1108 secstrs = elf_getdata(sec_strndx, NULL);
9b30a26b 1109 if (secstrs == NULL)
6cfcc53e
MG
1110 goto out_elf_end;
1111
a2928c42
ACM
1112 nr_syms = shdr.sh_size / shdr.sh_entsize;
1113
e9fbc9dc 1114 memset(&sym, 0, sizeof(sym));
a1645ce1 1115 if (self->kernel == DSO_TYPE_USER) {
d20ff6bd 1116 self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
30d7a77d
ACM
1117 elf_section_by_name(elf, &ehdr, &shdr,
1118 ".gnu.prelink_undo",
1119 NULL) != NULL);
d20ff6bd
MG
1120 } else self->adjust_symbols = 0;
1121
83a0944f 1122 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
a2928c42 1123 struct symbol *f;
56b03f3c 1124 const char *elf_name = elf_sym__name(&sym, symstrs);
2e538c4a 1125 char *demangled = NULL;
6cfcc53e
MG
1126 int is_label = elf_sym__is_label(&sym);
1127 const char *section_name;
a2928c42 1128
9de89fe7
ACM
1129 if (kmap && kmap->ref_reloc_sym && kmap->ref_reloc_sym->name &&
1130 strcmp(elf_name, kmap->ref_reloc_sym->name) == 0)
1131 kmap->ref_reloc_sym->unrelocated_addr = sym.st_value;
56b03f3c 1132
d45868d3 1133 if (!is_label && !elf_sym__is_a(&sym, map->type))
a2928c42
ACM
1134 continue;
1135
696b97a5
DM
1136 /* Reject ARM ELF "mapping symbols": these aren't unique and
1137 * don't identify functions, so will confuse the profile
1138 * output: */
1139 if (ehdr.e_machine == EM_ARM) {
1140 if (!strcmp(elf_name, "$a") ||
1141 !strcmp(elf_name, "$d") ||
1142 !strcmp(elf_name, "$t"))
1143 continue;
1144 }
1145
70c3856b
EM
1146 if (opdsec && sym.st_shndx == opdidx) {
1147 u32 offset = sym.st_value - opdshdr.sh_addr;
1148 u64 *opd = opddata->d_buf + offset;
1149 sym.st_value = *opd;
1150 sym.st_shndx = elf_addr_to_index(elf, sym.st_value);
1151 }
1152
a2928c42
ACM
1153 sec = elf_getscn(elf, sym.st_shndx);
1154 if (!sec)
1155 goto out_elf_end;
1156
1157 gelf_getshdr(sec, &shdr);
6cfcc53e 1158
d45868d3 1159 if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type))
6cfcc53e
MG
1160 continue;
1161
1162 section_name = elf_sec__name(&shdr, secstrs);
0b73da3f 1163
b2f8fb23
DDAG
1164 /* On ARM, symbols for thumb functions have 1 added to
1165 * the symbol address as a flag - remove it */
1166 if ((ehdr.e_machine == EM_ARM) &&
1167 (map->type == MAP__FUNCTION) &&
1168 (sym.st_value & 1))
1169 --sym.st_value;
1170
a1645ce1 1171 if (self->kernel != DSO_TYPE_USER || kmodule) {
2e538c4a
ACM
1172 char dso_name[PATH_MAX];
1173
1174 if (strcmp(section_name,
b63be8d7
ACM
1175 (curr_dso->short_name +
1176 self->short_name_len)) == 0)
2e538c4a
ACM
1177 goto new_symbol;
1178
1179 if (strcmp(section_name, ".text") == 0) {
1180 curr_map = map;
1181 curr_dso = self;
1182 goto new_symbol;
1183 }
1184
1185 snprintf(dso_name, sizeof(dso_name),
1186 "%s%s", self->short_name, section_name);
1187
9de89fe7 1188 curr_map = map_groups__find_by_name(kmap->kmaps, map->type, dso_name);
2e538c4a
ACM
1189 if (curr_map == NULL) {
1190 u64 start = sym.st_value;
1191
1192 if (kmodule)
1193 start += map->start + shdr.sh_offset;
1194
00a192b3 1195 curr_dso = dso__new(dso_name);
2e538c4a
ACM
1196 if (curr_dso == NULL)
1197 goto out_elf_end;
a1645ce1 1198 curr_dso->kernel = self->kernel;
6c6804fb
AL
1199 curr_dso->long_name = self->long_name;
1200 curr_dso->long_name_len = self->long_name_len;
3610583c 1201 curr_map = map__new2(start, curr_dso,
6275ce2d 1202 map->type);
2e538c4a
ACM
1203 if (curr_map == NULL) {
1204 dso__delete(curr_dso);
1205 goto out_elf_end;
1206 }
ed52ce2e
ACM
1207 curr_map->map_ip = identity__map_ip;
1208 curr_map->unmap_ip = identity__map_ip;
878b439d 1209 curr_dso->symtab_type = self->symtab_type;
9de89fe7 1210 map_groups__insert(kmap->kmaps, curr_map);
a1645ce1 1211 dsos__add(&self->node, curr_dso);
6275ce2d 1212 dso__set_loaded(curr_dso, map->type);
2e538c4a
ACM
1213 } else
1214 curr_dso = curr_map->dso;
1215
1216 goto new_symbol;
af427bf5
ACM
1217 }
1218
2e538c4a 1219 if (curr_dso->adjust_symbols) {
9486aa38
ACM
1220 pr_debug4("%s: adjusting symbol: st_value: %#" PRIx64 " "
1221 "sh_addr: %#" PRIx64 " sh_offset: %#" PRIx64 "\n", __func__,
29a9f66d
ACM
1222 (u64)sym.st_value, (u64)shdr.sh_addr,
1223 (u64)shdr.sh_offset);
f5812a7a 1224 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
af427bf5 1225 }
28ac909b
ACM
1226 /*
1227 * We need to figure out if the object was created from C++ sources
1228 * DWARF DW_compile_unit has this, but we don't always have access
1229 * to it...
1230 */
83a0944f 1231 demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
28ac909b 1232 if (demangled != NULL)
83a0944f 1233 elf_name = demangled;
2e538c4a 1234new_symbol:
c408fedf
ACM
1235 f = symbol__new(sym.st_value, sym.st_size,
1236 GELF_ST_BIND(sym.st_info), elf_name);
28ac909b 1237 free(demangled);
a2928c42
ACM
1238 if (!f)
1239 goto out_elf_end;
1240
2e538c4a 1241 if (filter && filter(curr_map, f))
00a192b3 1242 symbol__delete(f);
69ee69f6 1243 else {
6a4694a4 1244 symbols__insert(&curr_dso->symbols[curr_map->type], f);
69ee69f6
ACM
1245 nr++;
1246 }
a2928c42
ACM
1247 }
1248
2e538c4a
ACM
1249 /*
1250 * For misannotated, zeroed, ASM function sizes.
1251 */
6275ce2d 1252 if (nr > 0) {
6a4694a4 1253 symbols__fixup_end(&self->symbols[map->type]);
6275ce2d
ACM
1254 if (kmap) {
1255 /*
1256 * We need to fixup this here too because we create new
1257 * maps here, for things like vsyscall sections.
1258 */
1259 __map_groups__fixup_end(kmap->kmaps, map->type);
1260 }
1261 }
a2928c42
ACM
1262 err = nr;
1263out_elf_end:
1264 elf_end(elf);
1265out_close:
1266 return err;
1267}
1268
78075caa
ACM
1269static bool dso__build_id_equal(const struct dso *self, u8 *build_id)
1270{
1271 return memcmp(self->build_id, build_id, sizeof(self->build_id)) == 0;
1272}
1273
a1645ce1 1274bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
57f395a7 1275{
e30a3d12 1276 bool have_build_id = false;
57f395a7
FW
1277 struct dso *pos;
1278
6122e4e4
ACM
1279 list_for_each_entry(pos, head, node) {
1280 if (with_hits && !pos->hit)
1281 continue;
f6e1467d
ACM
1282 if (pos->has_build_id) {
1283 have_build_id = true;
1284 continue;
1285 }
e30a3d12
ACM
1286 if (filename__read_build_id(pos->long_name, pos->build_id,
1287 sizeof(pos->build_id)) > 0) {
1288 have_build_id = true;
1289 pos->has_build_id = true;
1290 }
6122e4e4 1291 }
57f395a7 1292
e30a3d12 1293 return have_build_id;
57f395a7
FW
1294}
1295
fd7a346e
ACM
1296/*
1297 * Align offset to 4 bytes as needed for note name and descriptor data.
1298 */
1299#define NOTE_ALIGN(n) (((n) + 3) & -4U)
1300
21916c38 1301static int elf_read_build_id(Elf *elf, void *bf, size_t size)
4d1e00a8 1302{
21916c38 1303 int err = -1;
4d1e00a8
ACM
1304 GElf_Ehdr ehdr;
1305 GElf_Shdr shdr;
fd7a346e 1306 Elf_Data *data;
4d1e00a8 1307 Elf_Scn *sec;
e57cfcda 1308 Elf_Kind ek;
fd7a346e 1309 void *ptr;
4d1e00a8 1310
2643ce11
ACM
1311 if (size < BUILD_ID_SIZE)
1312 goto out;
1313
e57cfcda
PE
1314 ek = elf_kind(elf);
1315 if (ek != ELF_K_ELF)
21916c38 1316 goto out;
e57cfcda 1317
4d1e00a8 1318 if (gelf_getehdr(elf, &ehdr) == NULL) {
6beba7ad 1319 pr_err("%s: cannot get elf header.\n", __func__);
21916c38 1320 goto out;
4d1e00a8
ACM
1321 }
1322
2643ce11
ACM
1323 sec = elf_section_by_name(elf, &ehdr, &shdr,
1324 ".note.gnu.build-id", NULL);
fd7a346e
ACM
1325 if (sec == NULL) {
1326 sec = elf_section_by_name(elf, &ehdr, &shdr,
1327 ".notes", NULL);
1328 if (sec == NULL)
21916c38 1329 goto out;
fd7a346e 1330 }
4d1e00a8 1331
fd7a346e
ACM
1332 data = elf_getdata(sec, NULL);
1333 if (data == NULL)
21916c38 1334 goto out;
fd7a346e
ACM
1335
1336 ptr = data->d_buf;
1337 while (ptr < (data->d_buf + data->d_size)) {
1338 GElf_Nhdr *nhdr = ptr;
1339 int namesz = NOTE_ALIGN(nhdr->n_namesz),
1340 descsz = NOTE_ALIGN(nhdr->n_descsz);
1341 const char *name;
1342
1343 ptr += sizeof(*nhdr);
1344 name = ptr;
1345 ptr += namesz;
1346 if (nhdr->n_type == NT_GNU_BUILD_ID &&
1347 nhdr->n_namesz == sizeof("GNU")) {
1348 if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
1349 memcpy(bf, ptr, BUILD_ID_SIZE);
1350 err = BUILD_ID_SIZE;
1351 break;
1352 }
1353 }
1354 ptr += descsz;
1355 }
21916c38
DM
1356
1357out:
1358 return err;
1359}
1360
1361int filename__read_build_id(const char *filename, void *bf, size_t size)
1362{
1363 int fd, err = -1;
1364 Elf *elf;
1365
1366 if (size < BUILD_ID_SIZE)
1367 goto out;
1368
1369 fd = open(filename, O_RDONLY);
1370 if (fd < 0)
1371 goto out;
1372
1373 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1374 if (elf == NULL) {
1375 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
1376 goto out_close;
1377 }
1378
1379 err = elf_read_build_id(elf, bf, size);
1380
2643ce11
ACM
1381 elf_end(elf);
1382out_close:
1383 close(fd);
1384out:
1385 return err;
1386}
1387
f1617b40
ACM
1388int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
1389{
1390 int fd, err = -1;
1391
1392 if (size < BUILD_ID_SIZE)
1393 goto out;
1394
1395 fd = open(filename, O_RDONLY);
1396 if (fd < 0)
1397 goto out;
1398
1399 while (1) {
1400 char bf[BUFSIZ];
1401 GElf_Nhdr nhdr;
1402 int namesz, descsz;
1403
1404 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
1405 break;
1406
fd7a346e
ACM
1407 namesz = NOTE_ALIGN(nhdr.n_namesz);
1408 descsz = NOTE_ALIGN(nhdr.n_descsz);
f1617b40
ACM
1409 if (nhdr.n_type == NT_GNU_BUILD_ID &&
1410 nhdr.n_namesz == sizeof("GNU")) {
1411 if (read(fd, bf, namesz) != namesz)
1412 break;
1413 if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
1414 if (read(fd, build_id,
1415 BUILD_ID_SIZE) == BUILD_ID_SIZE) {
1416 err = 0;
1417 break;
1418 }
1419 } else if (read(fd, bf, descsz) != descsz)
1420 break;
1421 } else {
1422 int n = namesz + descsz;
1423 if (read(fd, bf, n) != n)
1424 break;
1425 }
1426 }
1427 close(fd);
1428out:
1429 return err;
1430}
1431
94cb9e38
ACM
1432char dso__symtab_origin(const struct dso *self)
1433{
1434 static const char origin[] = {
878b439d
ACM
1435 [SYMTAB__KALLSYMS] = 'k',
1436 [SYMTAB__JAVA_JIT] = 'j',
1437 [SYMTAB__BUILD_ID_CACHE] = 'B',
1438 [SYMTAB__FEDORA_DEBUGINFO] = 'f',
1439 [SYMTAB__UBUNTU_DEBUGINFO] = 'u',
1440 [SYMTAB__BUILDID_DEBUGINFO] = 'b',
1441 [SYMTAB__SYSTEM_PATH_DSO] = 'd',
1442 [SYMTAB__SYSTEM_PATH_KMODULE] = 'K',
1443 [SYMTAB__GUEST_KALLSYMS] = 'g',
1444 [SYMTAB__GUEST_KMODULE] = 'G',
94cb9e38
ACM
1445 };
1446
878b439d 1447 if (self == NULL || self->symtab_type == SYMTAB__NOT_FOUND)
94cb9e38 1448 return '!';
878b439d 1449 return origin[self->symtab_type];
94cb9e38
ACM
1450}
1451
9de89fe7 1452int dso__load(struct dso *self, struct map *map, symbol_filter_t filter)
a2928c42 1453{
4d1e00a8 1454 int size = PATH_MAX;
c338aee8 1455 char *name;
a2928c42
ACM
1456 int ret = -1;
1457 int fd;
23346f21 1458 struct machine *machine;
a1645ce1 1459 const char *root_dir;
6da80ce8 1460 int want_symtab;
a2928c42 1461
3610583c 1462 dso__set_loaded(self, map->type);
66bd8424 1463
a1645ce1 1464 if (self->kernel == DSO_TYPE_KERNEL)
9de89fe7 1465 return dso__load_kernel_sym(self, map, filter);
a1645ce1
ZY
1466 else if (self->kernel == DSO_TYPE_GUEST_KERNEL)
1467 return dso__load_guest_kernel_sym(self, map, filter);
1468
23346f21
ACM
1469 if (map->groups && map->groups->machine)
1470 machine = map->groups->machine;
a1645ce1 1471 else
23346f21 1472 machine = NULL;
c338aee8
ACM
1473
1474 name = malloc(size);
a2928c42
ACM
1475 if (!name)
1476 return -1;
1477
30d7a77d 1478 self->adjust_symbols = 0;
f5812a7a 1479
94cb9e38 1480 if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
6beba7ad 1481 ret = dso__load_perf_map(self, map, filter);
878b439d
ACM
1482 self->symtab_type = ret > 0 ? SYMTAB__JAVA_JIT :
1483 SYMTAB__NOT_FOUND;
94cb9e38
ACM
1484 return ret;
1485 }
1486
6da80ce8
DM
1487 /* Iterate over candidate debug images.
1488 * On the first pass, only load images if they have a full symtab.
1489 * Failing that, do a second pass where we accept .dynsym also
1490 */
60e4b10c
ACM
1491 want_symtab = 1;
1492restart:
1493 for (self->symtab_type = SYMTAB__BUILD_ID_CACHE;
878b439d
ACM
1494 self->symtab_type != SYMTAB__NOT_FOUND;
1495 self->symtab_type++) {
1496 switch (self->symtab_type) {
1497 case SYMTAB__BUILD_ID_CACHE:
ec5761ea
DA
1498 /* skip the locally configured cache if a symfs is given */
1499 if (symbol_conf.symfs[0] ||
1500 (dso__build_id_filename(self, name, size) == NULL)) {
6da80ce8 1501 continue;
ec5761ea 1502 }
6da80ce8 1503 break;
878b439d 1504 case SYMTAB__FEDORA_DEBUGINFO:
ec5761ea
DA
1505 snprintf(name, size, "%s/usr/lib/debug%s.debug",
1506 symbol_conf.symfs, self->long_name);
a2928c42 1507 break;
878b439d 1508 case SYMTAB__UBUNTU_DEBUGINFO:
ec5761ea
DA
1509 snprintf(name, size, "%s/usr/lib/debug%s",
1510 symbol_conf.symfs, self->long_name);
a2928c42 1511 break;
878b439d 1512 case SYMTAB__BUILDID_DEBUGINFO: {
6da80ce8
DM
1513 char build_id_hex[BUILD_ID_SIZE * 2 + 1];
1514
1515 if (!self->has_build_id)
1516 continue;
1517
1518 build_id__sprintf(self->build_id,
1519 sizeof(self->build_id),
1520 build_id_hex);
1521 snprintf(name, size,
ec5761ea
DA
1522 "%s/usr/lib/debug/.build-id/%.2s/%s.debug",
1523 symbol_conf.symfs, build_id_hex, build_id_hex + 2);
4d1e00a8 1524 }
6da80ce8 1525 break;
878b439d 1526 case SYMTAB__SYSTEM_PATH_DSO:
ec5761ea
DA
1527 snprintf(name, size, "%s%s",
1528 symbol_conf.symfs, self->long_name);
a2928c42 1529 break;
878b439d 1530 case SYMTAB__GUEST_KMODULE:
fb7d0b3c
KM
1531 if (map->groups && machine)
1532 root_dir = machine->root_dir;
a1645ce1
ZY
1533 else
1534 root_dir = "";
ec5761ea
DA
1535 snprintf(name, size, "%s%s%s", symbol_conf.symfs,
1536 root_dir, self->long_name);
1537 break;
1538
878b439d 1539 case SYMTAB__SYSTEM_PATH_KMODULE:
ec5761ea
DA
1540 snprintf(name, size, "%s%s", symbol_conf.symfs,
1541 self->long_name);
a1645ce1 1542 break;
60e4b10c 1543 default:;
a2928c42 1544 }
6da80ce8
DM
1545
1546 /* Name is now the name of the next image to try */
a2928c42 1547 fd = open(name, O_RDONLY);
6da80ce8
DM
1548 if (fd < 0)
1549 continue;
a2928c42 1550
6da80ce8
DM
1551 ret = dso__load_sym(self, map, name, fd, filter, 0,
1552 want_symtab);
1553 close(fd);
a2928c42 1554
6da80ce8
DM
1555 /*
1556 * Some people seem to have debuginfo files _WITHOUT_ debug
1557 * info!?!?
1558 */
1559 if (!ret)
1560 continue;
a2928c42 1561
6da80ce8
DM
1562 if (ret > 0) {
1563 int nr_plt = dso__synthesize_plt_symbols(self, map, filter);
1564 if (nr_plt > 0)
1565 ret += nr_plt;
1566 break;
1567 }
a25e46c4 1568 }
6da80ce8 1569
60e4b10c
ACM
1570 /*
1571 * If we wanted a full symtab but no image had one,
1572 * relax our requirements and repeat the search.
1573 */
1574 if (ret <= 0 && want_symtab) {
1575 want_symtab = 0;
1576 goto restart;
1577 }
1578
a2928c42 1579 free(name);
1340e6bb
ACM
1580 if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
1581 return 0;
a2928c42
ACM
1582 return ret;
1583}
1584
79406cd7
ACM
1585struct map *map_groups__find_by_name(struct map_groups *self,
1586 enum map_type type, const char *name)
439d473b
ACM
1587{
1588 struct rb_node *nd;
1589
79406cd7 1590 for (nd = rb_first(&self->maps[type]); nd; nd = rb_next(nd)) {
439d473b
ACM
1591 struct map *map = rb_entry(nd, struct map, rb_node);
1592
b7cece76 1593 if (map->dso && strcmp(map->dso->short_name, name) == 0)
439d473b
ACM
1594 return map;
1595 }
1596
1597 return NULL;
1598}
1599
a1645ce1
ZY
1600static int dso__kernel_module_get_build_id(struct dso *self,
1601 const char *root_dir)
b7cece76
ACM
1602{
1603 char filename[PATH_MAX];
1604 /*
1605 * kernel module short names are of the form "[module]" and
1606 * we need just "module" here.
1607 */
1608 const char *name = self->short_name + 1;
1609
1610 snprintf(filename, sizeof(filename),
a1645ce1
ZY
1611 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1612 root_dir, (int)strlen(name) - 1, name);
b7cece76
ACM
1613
1614 if (sysfs__read_build_id(filename, self->build_id,
1615 sizeof(self->build_id)) == 0)
1616 self->has_build_id = true;
1617
1618 return 0;
1619}
1620
a1645ce1
ZY
1621static int map_groups__set_modules_path_dir(struct map_groups *self,
1622 const char *dir_name)
6cfcc53e 1623{
439d473b 1624 struct dirent *dent;
5aab621b 1625 DIR *dir = opendir(dir_name);
74534341 1626 int ret = 0;
6cfcc53e 1627
439d473b 1628 if (!dir) {
5aab621b 1629 pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
439d473b
ACM
1630 return -1;
1631 }
6cfcc53e 1632
439d473b
ACM
1633 while ((dent = readdir(dir)) != NULL) {
1634 char path[PATH_MAX];
a1645ce1
ZY
1635 struct stat st;
1636
1637 /*sshfs might return bad dent->d_type, so we have to stat*/
1638 sprintf(path, "%s/%s", dir_name, dent->d_name);
1639 if (stat(path, &st))
1640 continue;
439d473b 1641
a1645ce1 1642 if (S_ISDIR(st.st_mode)) {
439d473b
ACM
1643 if (!strcmp(dent->d_name, ".") ||
1644 !strcmp(dent->d_name, ".."))
1645 continue;
1646
1647 snprintf(path, sizeof(path), "%s/%s",
5aab621b 1648 dir_name, dent->d_name);
74534341
GJ
1649 ret = map_groups__set_modules_path_dir(self, path);
1650 if (ret < 0)
1651 goto out;
439d473b
ACM
1652 } else {
1653 char *dot = strrchr(dent->d_name, '.'),
1654 dso_name[PATH_MAX];
1655 struct map *map;
cfc10d3b 1656 char *long_name;
439d473b
ACM
1657
1658 if (dot == NULL || strcmp(dot, ".ko"))
1659 continue;
1660 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
1661 (int)(dot - dent->d_name), dent->d_name);
1662
a2a99e8e 1663 strxfrchar(dso_name, '-', '_');
9de89fe7 1664 map = map_groups__find_by_name(self, MAP__FUNCTION, dso_name);
439d473b
ACM
1665 if (map == NULL)
1666 continue;
1667
1668 snprintf(path, sizeof(path), "%s/%s",
5aab621b 1669 dir_name, dent->d_name);
439d473b 1670
cfc10d3b 1671 long_name = strdup(path);
74534341
GJ
1672 if (long_name == NULL) {
1673 ret = -1;
1674 goto out;
1675 }
cfc10d3b 1676 dso__set_long_name(map->dso, long_name);
6e406257 1677 map->dso->lname_alloc = 1;
a1645ce1 1678 dso__kernel_module_get_build_id(map->dso, "");
439d473b 1679 }
439d473b 1680 }
6cfcc53e 1681
74534341 1682out:
439d473b 1683 closedir(dir);
74534341 1684 return ret;
439d473b 1685}
6cfcc53e 1686
a1645ce1 1687static char *get_kernel_version(const char *root_dir)
439d473b 1688{
a1645ce1
ZY
1689 char version[PATH_MAX];
1690 FILE *file;
1691 char *name, *tmp;
1692 const char *prefix = "Linux version ";
1693
1694 sprintf(version, "%s/proc/version", root_dir);
1695 file = fopen(version, "r");
1696 if (!file)
1697 return NULL;
1698
1699 version[0] = '\0';
1700 tmp = fgets(version, sizeof(version), file);
1701 fclose(file);
1702
1703 name = strstr(version, prefix);
1704 if (!name)
1705 return NULL;
1706 name += strlen(prefix);
1707 tmp = strchr(name, ' ');
1708 if (tmp)
1709 *tmp = '\0';
1710
1711 return strdup(name);
1712}
1713
d28c6223 1714static int machine__set_modules_path(struct machine *self)
a1645ce1
ZY
1715{
1716 char *version;
439d473b 1717 char modules_path[PATH_MAX];
6cfcc53e 1718
d28c6223 1719 version = get_kernel_version(self->root_dir);
a1645ce1 1720 if (!version)
439d473b 1721 return -1;
6cfcc53e 1722
a1645ce1 1723 snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s/kernel",
d28c6223 1724 self->root_dir, version);
a1645ce1 1725 free(version);
6cfcc53e 1726
d28c6223 1727 return map_groups__set_modules_path_dir(&self->kmaps, modules_path);
6cfcc53e
MG
1728}
1729
439d473b
ACM
1730/*
1731 * Constructor variant for modules (where we know from /proc/modules where
1732 * they are loaded) and for vmlinux, where only after we load all the
1733 * symbols we'll know where it starts and ends.
1734 */
3610583c 1735static struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
6cfcc53e 1736{
5aab621b
ACM
1737 struct map *self = calloc(1, (sizeof(*self) +
1738 (dso->kernel ? sizeof(struct kmap) : 0)));
439d473b 1739 if (self != NULL) {
439d473b 1740 /*
afb7b4f0 1741 * ->end will be filled after we load all the symbols
439d473b 1742 */
3610583c 1743 map__init(self, type, start, 0, 0, dso);
439d473b 1744 }
afb7b4f0 1745
439d473b
ACM
1746 return self;
1747}
1748
d28c6223
ACM
1749struct map *machine__new_module(struct machine *self, u64 start,
1750 const char *filename)
b7cece76
ACM
1751{
1752 struct map *map;
d28c6223 1753 struct dso *dso = __dsos__findnew(&self->kernel_dsos, filename);
b7cece76
ACM
1754
1755 if (dso == NULL)
1756 return NULL;
1757
1758 map = map__new2(start, dso, MAP__FUNCTION);
1759 if (map == NULL)
1760 return NULL;
1761
d28c6223 1762 if (machine__is_host(self))
878b439d 1763 dso->symtab_type = SYMTAB__SYSTEM_PATH_KMODULE;
a1645ce1 1764 else
878b439d 1765 dso->symtab_type = SYMTAB__GUEST_KMODULE;
d28c6223 1766 map_groups__insert(&self->kmaps, map);
b7cece76
ACM
1767 return map;
1768}
1769
d28c6223 1770static int machine__create_modules(struct machine *self)
439d473b
ACM
1771{
1772 char *line = NULL;
1773 size_t n;
a1645ce1 1774 FILE *file;
439d473b 1775 struct map *map;
a1645ce1
ZY
1776 const char *modules;
1777 char path[PATH_MAX];
1778
d28c6223 1779 if (machine__is_default_guest(self))
a1645ce1
ZY
1780 modules = symbol_conf.default_guest_modules;
1781 else {
d28c6223 1782 sprintf(path, "%s/proc/modules", self->root_dir);
a1645ce1
ZY
1783 modules = path;
1784 }
6cfcc53e 1785
a1645ce1 1786 file = fopen(modules, "r");
439d473b
ACM
1787 if (file == NULL)
1788 return -1;
6cfcc53e 1789
439d473b
ACM
1790 while (!feof(file)) {
1791 char name[PATH_MAX];
1792 u64 start;
439d473b
ACM
1793 char *sep;
1794 int line_len;
6cfcc53e 1795
439d473b
ACM
1796 line_len = getline(&line, &n, file);
1797 if (line_len < 0)
1798 break;
1799
1800 if (!line)
1801 goto out_failure;
1802
1803 line[--line_len] = '\0'; /* \n */
1804
1805 sep = strrchr(line, 'x');
1806 if (sep == NULL)
1807 continue;
1808
1809 hex2u64(sep + 1, &start);
1810
1811 sep = strchr(line, ' ');
1812 if (sep == NULL)
1813 continue;
1814
1815 *sep = '\0';
1816
1817 snprintf(name, sizeof(name), "[%s]", line);
d28c6223 1818 map = machine__new_module(self, start, name);
b7cece76 1819 if (map == NULL)
439d473b 1820 goto out_delete_line;
d28c6223 1821 dso__kernel_module_get_build_id(map->dso, self->root_dir);
6cfcc53e 1822 }
439d473b
ACM
1823
1824 free(line);
1825 fclose(file);
1826
d28c6223 1827 return machine__set_modules_path(self);
439d473b
ACM
1828
1829out_delete_line:
1830 free(line);
1831out_failure:
1832 return -1;
6cfcc53e
MG
1833}
1834
fd930ff9
FBH
1835int dso__load_vmlinux(struct dso *self, struct map *map,
1836 const char *vmlinux, symbol_filter_t filter)
a2928c42 1837{
fbd733b8 1838 int err = -1, fd;
ec5761ea 1839 char symfs_vmlinux[PATH_MAX];
a2928c42 1840
a639dc64 1841 snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s%s",
ec5761ea
DA
1842 symbol_conf.symfs, vmlinux);
1843 fd = open(symfs_vmlinux, O_RDONLY);
a2928c42
ACM
1844 if (fd < 0)
1845 return -1;
1846
6c6804fb 1847 dso__set_long_name(self, (char *)vmlinux);
3610583c 1848 dso__set_loaded(self, map->type);
ec5761ea 1849 err = dso__load_sym(self, map, symfs_vmlinux, fd, filter, 0, 0);
a2928c42
ACM
1850 close(fd);
1851
3846df2e 1852 if (err > 0)
ec5761ea 1853 pr_debug("Using %s for symbols\n", symfs_vmlinux);
3846df2e 1854
a2928c42
ACM
1855 return err;
1856}
1857
a19afe46 1858int dso__load_vmlinux_path(struct dso *self, struct map *map,
9de89fe7 1859 symbol_filter_t filter)
a19afe46
ACM
1860{
1861 int i, err = 0;
5ad90e4e 1862 char *filename;
a19afe46
ACM
1863
1864 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
5ad90e4e
ACM
1865 vmlinux_path__nr_entries + 1);
1866
1867 filename = dso__build_id_filename(self, NULL, 0);
1868 if (filename != NULL) {
1869 err = dso__load_vmlinux(self, map, filename, filter);
1870 if (err > 0) {
1871 dso__set_long_name(self, filename);
1872 goto out;
1873 }
1874 free(filename);
1875 }
a19afe46
ACM
1876
1877 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
9de89fe7 1878 err = dso__load_vmlinux(self, map, vmlinux_path[i], filter);
a19afe46 1879 if (err > 0) {
a19afe46
ACM
1880 dso__set_long_name(self, strdup(vmlinux_path[i]));
1881 break;
1882 }
1883 }
5ad90e4e 1884out:
a19afe46
ACM
1885 return err;
1886}
1887
c338aee8 1888static int dso__load_kernel_sym(struct dso *self, struct map *map,
9de89fe7 1889 symbol_filter_t filter)
a827c875 1890{
cc612d81 1891 int err;
9e201442
ACM
1892 const char *kallsyms_filename = NULL;
1893 char *kallsyms_allocated_filename = NULL;
dc8d6ab2 1894 /*
b226a5a7
DA
1895 * Step 1: if the user specified a kallsyms or vmlinux filename, use
1896 * it and only it, reporting errors to the user if it cannot be used.
dc8d6ab2
ACM
1897 *
1898 * For instance, try to analyse an ARM perf.data file _without_ a
1899 * build-id, or if the user specifies the wrong path to the right
1900 * vmlinux file, obviously we can't fallback to another vmlinux (a
1901 * x86_86 one, on the machine where analysis is being performed, say),
1902 * or worse, /proc/kallsyms.
1903 *
1904 * If the specified file _has_ a build-id and there is a build-id
1905 * section in the perf.data file, we will still do the expected
1906 * validation in dso__load_vmlinux and will bail out if they don't
1907 * match.
1908 */
b226a5a7
DA
1909 if (symbol_conf.kallsyms_name != NULL) {
1910 kallsyms_filename = symbol_conf.kallsyms_name;
1911 goto do_kallsyms;
1912 }
1913
dc8d6ab2 1914 if (symbol_conf.vmlinux_name != NULL) {
9de89fe7 1915 err = dso__load_vmlinux(self, map,
dc8d6ab2 1916 symbol_conf.vmlinux_name, filter);
e7dadc00
ACM
1917 if (err > 0) {
1918 dso__set_long_name(self,
1919 strdup(symbol_conf.vmlinux_name));
1920 goto out_fixup;
1921 }
1922 return err;
dc8d6ab2 1923 }
cc612d81
ACM
1924
1925 if (vmlinux_path != NULL) {
9de89fe7 1926 err = dso__load_vmlinux_path(self, map, filter);
a19afe46
ACM
1927 if (err > 0)
1928 goto out_fixup;
cc612d81
ACM
1929 }
1930
ec5761ea
DA
1931 /* do not try local files if a symfs was given */
1932 if (symbol_conf.symfs[0] != 0)
1933 return -1;
1934
b7cece76
ACM
1935 /*
1936 * Say the kernel DSO was created when processing the build-id header table,
1937 * we have a build-id, so check if it is the same as the running kernel,
1938 * using it if it is.
1939 */
1940 if (self->has_build_id) {
1941 u8 kallsyms_build_id[BUILD_ID_SIZE];
9e201442 1942 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
b7cece76
ACM
1943
1944 if (sysfs__read_build_id("/sys/kernel/notes", kallsyms_build_id,
8d0591f6 1945 sizeof(kallsyms_build_id)) == 0) {
9e201442
ACM
1946 if (dso__build_id_equal(self, kallsyms_build_id)) {
1947 kallsyms_filename = "/proc/kallsyms";
8d0591f6 1948 goto do_kallsyms;
9e201442 1949 }
8d0591f6 1950 }
dc8d6ab2
ACM
1951 /*
1952 * Now look if we have it on the build-id cache in
1953 * $HOME/.debug/[kernel.kallsyms].
1954 */
9e201442
ACM
1955 build_id__sprintf(self->build_id, sizeof(self->build_id),
1956 sbuild_id);
1957
1958 if (asprintf(&kallsyms_allocated_filename,
1959 "%s/.debug/[kernel.kallsyms]/%s",
3846df2e
ACM
1960 getenv("HOME"), sbuild_id) == -1) {
1961 pr_err("Not enough memory for kallsyms file lookup\n");
dc8d6ab2 1962 return -1;
3846df2e 1963 }
dc8d6ab2 1964
19fc2ded
ACM
1965 kallsyms_filename = kallsyms_allocated_filename;
1966
dc8d6ab2 1967 if (access(kallsyms_filename, F_OK)) {
3846df2e
ACM
1968 pr_err("No kallsyms or vmlinux with build-id %s "
1969 "was found\n", sbuild_id);
9e201442 1970 free(kallsyms_allocated_filename);
dc8d6ab2 1971 return -1;
9e201442 1972 }
dc8d6ab2
ACM
1973 } else {
1974 /*
1975 * Last resort, if we don't have a build-id and couldn't find
1976 * any vmlinux file, try the running kernel kallsyms table.
1977 */
9e201442 1978 kallsyms_filename = "/proc/kallsyms";
9e201442 1979 }
439d473b 1980
cc612d81 1981do_kallsyms:
9de89fe7 1982 err = dso__load_kallsyms(self, kallsyms_filename, map, filter);
3846df2e
ACM
1983 if (err > 0)
1984 pr_debug("Using %s for symbols\n", kallsyms_filename);
dc8d6ab2 1985 free(kallsyms_allocated_filename);
439d473b
ACM
1986
1987 if (err > 0) {
cc612d81 1988out_fixup:
e1c7c6a4 1989 if (kallsyms_filename != NULL)
dc8d6ab2 1990 dso__set_long_name(self, strdup("[kernel.kallsyms]"));
6a4694a4
ACM
1991 map__fixup_start(map);
1992 map__fixup_end(map);
439d473b 1993 }
94cb9e38 1994
a827c875
ACM
1995 return err;
1996}
1997
a1645ce1
ZY
1998static int dso__load_guest_kernel_sym(struct dso *self, struct map *map,
1999 symbol_filter_t filter)
2000{
2001 int err;
2002 const char *kallsyms_filename = NULL;
23346f21 2003 struct machine *machine;
a1645ce1
ZY
2004 char path[PATH_MAX];
2005
2006 if (!map->groups) {
2007 pr_debug("Guest kernel map hasn't the point to groups\n");
2008 return -1;
2009 }
23346f21 2010 machine = map->groups->machine;
a1645ce1 2011
23346f21 2012 if (machine__is_default_guest(machine)) {
a1645ce1
ZY
2013 /*
2014 * if the user specified a vmlinux filename, use it and only
2015 * it, reporting errors to the user if it cannot be used.
2016 * Or use file guest_kallsyms inputted by user on commandline
2017 */
2018 if (symbol_conf.default_guest_vmlinux_name != NULL) {
2019 err = dso__load_vmlinux(self, map,
2020 symbol_conf.default_guest_vmlinux_name, filter);
2021 goto out_try_fixup;
2022 }
2023
2024 kallsyms_filename = symbol_conf.default_guest_kallsyms;
2025 if (!kallsyms_filename)
2026 return -1;
2027 } else {
23346f21 2028 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
a1645ce1
ZY
2029 kallsyms_filename = path;
2030 }
2031
2032 err = dso__load_kallsyms(self, kallsyms_filename, map, filter);
2033 if (err > 0)
2034 pr_debug("Using %s for symbols\n", kallsyms_filename);
2035
2036out_try_fixup:
2037 if (err > 0) {
2038 if (kallsyms_filename != NULL) {
48ea8f54 2039 machine__mmap_name(machine, path, sizeof(path));
23346f21 2040 dso__set_long_name(self, strdup(path));
a1645ce1
ZY
2041 }
2042 map__fixup_start(map);
2043 map__fixup_end(map);
2044 }
2045
2046 return err;
2047}
cd84c2ac 2048
b0da954a 2049static void dsos__add(struct list_head *head, struct dso *dso)
cd84c2ac 2050{
b0da954a 2051 list_add_tail(&dso->node, head);
cd84c2ac
FW
2052}
2053
b0da954a 2054static struct dso *dsos__find(struct list_head *head, const char *name)
cd84c2ac
FW
2055{
2056 struct dso *pos;
2057
b0da954a 2058 list_for_each_entry(pos, head, node)
cf4e5b08 2059 if (strcmp(pos->long_name, name) == 0)
cd84c2ac
FW
2060 return pos;
2061 return NULL;
2062}
2063
a89e5abe 2064struct dso *__dsos__findnew(struct list_head *head, const char *name)
cd84c2ac 2065{
a89e5abe 2066 struct dso *dso = dsos__find(head, name);
cd84c2ac 2067
e4204992 2068 if (!dso) {
00a192b3 2069 dso = dso__new(name);
cfc10d3b 2070 if (dso != NULL) {
a89e5abe 2071 dsos__add(head, dso);
cfc10d3b
ACM
2072 dso__set_basename(dso);
2073 }
66bd8424 2074 }
cd84c2ac
FW
2075
2076 return dso;
cd84c2ac
FW
2077}
2078
1f626bc3 2079size_t __dsos__fprintf(struct list_head *head, FILE *fp)
cd84c2ac
FW
2080{
2081 struct dso *pos;
cbf69680 2082 size_t ret = 0;
cd84c2ac 2083
95011c60
ACM
2084 list_for_each_entry(pos, head, node) {
2085 int i;
2086 for (i = 0; i < MAP__NR_TYPES; ++i)
cbf69680 2087 ret += dso__fprintf(pos, i, fp);
95011c60 2088 }
cbf69680
ACM
2089
2090 return ret;
cd84c2ac
FW
2091}
2092
cbf69680 2093size_t machines__fprintf_dsos(struct rb_root *self, FILE *fp)
b0da954a 2094{
a1645ce1 2095 struct rb_node *nd;
cbf69680 2096 size_t ret = 0;
a1645ce1 2097
cbf69680 2098 for (nd = rb_first(self); nd; nd = rb_next(nd)) {
23346f21 2099 struct machine *pos = rb_entry(nd, struct machine, rb_node);
cbf69680
ACM
2100 ret += __dsos__fprintf(&pos->kernel_dsos, fp);
2101 ret += __dsos__fprintf(&pos->user_dsos, fp);
a1645ce1 2102 }
cbf69680
ACM
2103
2104 return ret;
b0da954a
ACM
2105}
2106
88d3d9b7
ACM
2107static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
2108 bool with_hits)
9e03eb2d
ACM
2109{
2110 struct dso *pos;
2111 size_t ret = 0;
2112
b0da954a 2113 list_for_each_entry(pos, head, node) {
88d3d9b7
ACM
2114 if (with_hits && !pos->hit)
2115 continue;
9e03eb2d 2116 ret += dso__fprintf_buildid(pos, fp);
1124ba73 2117 ret += fprintf(fp, " %s\n", pos->long_name);
9e03eb2d
ACM
2118 }
2119 return ret;
2120}
2121
f869097e
ACM
2122size_t machine__fprintf_dsos_buildid(struct machine *self, FILE *fp, bool with_hits)
2123{
2124 return __dsos__fprintf_buildid(&self->kernel_dsos, fp, with_hits) +
2125 __dsos__fprintf_buildid(&self->user_dsos, fp, with_hits);
2126}
2127
cbf69680 2128size_t machines__fprintf_dsos_buildid(struct rb_root *self, FILE *fp, bool with_hits)
b0da954a 2129{
a1645ce1
ZY
2130 struct rb_node *nd;
2131 size_t ret = 0;
2132
cbf69680 2133 for (nd = rb_first(self); nd; nd = rb_next(nd)) {
23346f21 2134 struct machine *pos = rb_entry(nd, struct machine, rb_node);
f869097e 2135 ret += machine__fprintf_dsos_buildid(pos, fp, with_hits);
a1645ce1
ZY
2136 }
2137 return ret;
b0da954a
ACM
2138}
2139
fd1d908c
ACM
2140struct dso *dso__new_kernel(const char *name)
2141{
2142 struct dso *self = dso__new(name ?: "[kernel.kallsyms]");
2143
2144 if (self != NULL) {
b63be8d7 2145 dso__set_short_name(self, "[kernel]");
a1645ce1
ZY
2146 self->kernel = DSO_TYPE_KERNEL;
2147 }
2148
2149 return self;
2150}
2151
23346f21 2152static struct dso *dso__new_guest_kernel(struct machine *machine,
a1645ce1
ZY
2153 const char *name)
2154{
48ea8f54
ACM
2155 char bf[PATH_MAX];
2156 struct dso *self = dso__new(name ?: machine__mmap_name(machine, bf, sizeof(bf)));
a1645ce1 2157
a1645ce1
ZY
2158 if (self != NULL) {
2159 dso__set_short_name(self, "[guest.kernel]");
2160 self->kernel = DSO_TYPE_GUEST_KERNEL;
fd1d908c
ACM
2161 }
2162
2163 return self;
2164}
2165
23346f21 2166void dso__read_running_kernel_build_id(struct dso *self, struct machine *machine)
fd1d908c 2167{
a1645ce1
ZY
2168 char path[PATH_MAX];
2169
23346f21 2170 if (machine__is_default_guest(machine))
a1645ce1 2171 return;
23346f21 2172 sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
a1645ce1 2173 if (sysfs__read_build_id(path, self->build_id,
fd1d908c
ACM
2174 sizeof(self->build_id)) == 0)
2175 self->has_build_id = true;
2176}
2177
5c0541d5 2178static struct dso *machine__create_kernel(struct machine *self)
cd84c2ac 2179{
a1645ce1
ZY
2180 const char *vmlinux_name = NULL;
2181 struct dso *kernel;
cd84c2ac 2182
5c0541d5 2183 if (machine__is_host(self)) {
a1645ce1
ZY
2184 vmlinux_name = symbol_conf.vmlinux_name;
2185 kernel = dso__new_kernel(vmlinux_name);
2186 } else {
5c0541d5 2187 if (machine__is_default_guest(self))
a1645ce1 2188 vmlinux_name = symbol_conf.default_guest_vmlinux_name;
5c0541d5 2189 kernel = dso__new_guest_kernel(self, vmlinux_name);
8d92c02a 2190 }
cd84c2ac 2191
a1645ce1 2192 if (kernel != NULL) {
5c0541d5
ACM
2193 dso__read_running_kernel_build_id(kernel, self);
2194 dsos__add(&self->kernel_dsos, kernel);
a1645ce1 2195 }
f1dfa0b1 2196 return kernel;
f1dfa0b1
ACM
2197}
2198
d214afbd
ML
2199struct process_args {
2200 u64 start;
2201};
2202
2203static int symbol__in_kernel(void *arg, const char *name,
3b01a413 2204 char type __used, u64 start, u64 end __used)
d214afbd
ML
2205{
2206 struct process_args *args = arg;
2207
2208 if (strchr(name, '['))
2209 return 0;
2210
2211 args->start = start;
2212 return 1;
2213}
2214
2215/* Figure out the start address of kernel map from /proc/kallsyms */
2216static u64 machine__get_kernel_start_addr(struct machine *machine)
2217{
2218 const char *filename;
2219 char path[PATH_MAX];
2220 struct process_args args;
2221
2222 if (machine__is_host(machine)) {
2223 filename = "/proc/kallsyms";
2224 } else {
2225 if (machine__is_default_guest(machine))
2226 filename = (char *)symbol_conf.default_guest_kallsyms;
2227 else {
2228 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
2229 filename = path;
2230 }
2231 }
2232
2233 if (kallsyms__parse(filename, &args, symbol__in_kernel) <= 0)
2234 return 0;
2235
2236 return args.start;
2237}
2238
d28c6223 2239int __machine__create_kernel_maps(struct machine *self, struct dso *kernel)
f1dfa0b1 2240{
de176489 2241 enum map_type type;
d214afbd 2242 u64 start = machine__get_kernel_start_addr(self);
f1dfa0b1 2243
de176489 2244 for (type = 0; type < MAP__NR_TYPES; ++type) {
9de89fe7
ACM
2245 struct kmap *kmap;
2246
d214afbd 2247 self->vmlinux_maps[type] = map__new2(start, kernel, type);
d28c6223 2248 if (self->vmlinux_maps[type] == NULL)
de176489 2249 return -1;
f1dfa0b1 2250
d28c6223
ACM
2251 self->vmlinux_maps[type]->map_ip =
2252 self->vmlinux_maps[type]->unmap_ip = identity__map_ip;
9de89fe7 2253
d28c6223
ACM
2254 kmap = map__kmap(self->vmlinux_maps[type]);
2255 kmap->kmaps = &self->kmaps;
2256 map_groups__insert(&self->kmaps, self->vmlinux_maps[type]);
f1dfa0b1
ACM
2257 }
2258
f1dfa0b1 2259 return 0;
2446042c
ACM
2260}
2261
076c6e45
ACM
2262void machine__destroy_kernel_maps(struct machine *self)
2263{
2264 enum map_type type;
2265
2266 for (type = 0; type < MAP__NR_TYPES; ++type) {
2267 struct kmap *kmap;
2268
2269 if (self->vmlinux_maps[type] == NULL)
2270 continue;
2271
2272 kmap = map__kmap(self->vmlinux_maps[type]);
2273 map_groups__remove(&self->kmaps, self->vmlinux_maps[type]);
2274 if (kmap->ref_reloc_sym) {
2275 /*
2276 * ref_reloc_sym is shared among all maps, so free just
2277 * on one of them.
2278 */
2279 if (type == MAP__FUNCTION) {
2280 free((char *)kmap->ref_reloc_sym->name);
2281 kmap->ref_reloc_sym->name = NULL;
2282 free(kmap->ref_reloc_sym);
2283 }
2284 kmap->ref_reloc_sym = NULL;
2285 }
2286
2287 map__delete(self->vmlinux_maps[type]);
2288 self->vmlinux_maps[type] = NULL;
2289 }
2290}
2291
5c0541d5
ACM
2292int machine__create_kernel_maps(struct machine *self)
2293{
2294 struct dso *kernel = machine__create_kernel(self);
2295
2296 if (kernel == NULL ||
2297 __machine__create_kernel_maps(self, kernel) < 0)
2298 return -1;
2299
2300 if (symbol_conf.use_modules && machine__create_modules(self) < 0)
2301 pr_debug("Problems creating module maps, continuing anyway...\n");
2302 /*
2303 * Now that we have all the maps created, just set the ->end of them:
2304 */
2305 map_groups__fixup_end(&self->kmaps);
2306 return 0;
2307}
2308
cc612d81
ACM
2309static void vmlinux_path__exit(void)
2310{
2311 while (--vmlinux_path__nr_entries >= 0) {
2312 free(vmlinux_path[vmlinux_path__nr_entries]);
2313 vmlinux_path[vmlinux_path__nr_entries] = NULL;
2314 }
2315
2316 free(vmlinux_path);
2317 vmlinux_path = NULL;
2318}
2319
2320static int vmlinux_path__init(void)
2321{
2322 struct utsname uts;
2323 char bf[PATH_MAX];
2324
cc612d81
ACM
2325 vmlinux_path = malloc(sizeof(char *) * 5);
2326 if (vmlinux_path == NULL)
2327 return -1;
2328
2329 vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
2330 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2331 goto out_fail;
2332 ++vmlinux_path__nr_entries;
2333 vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
2334 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2335 goto out_fail;
2336 ++vmlinux_path__nr_entries;
ec5761ea
DA
2337
2338 /* only try running kernel version if no symfs was given */
2339 if (symbol_conf.symfs[0] != 0)
2340 return 0;
2341
2342 if (uname(&uts) < 0)
2343 return -1;
2344
cc612d81
ACM
2345 snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
2346 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
2347 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2348 goto out_fail;
2349 ++vmlinux_path__nr_entries;
2350 snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
2351 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
2352 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2353 goto out_fail;
2354 ++vmlinux_path__nr_entries;
2355 snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
2356 uts.release);
2357 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
2358 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2359 goto out_fail;
2360 ++vmlinux_path__nr_entries;
2361
2362 return 0;
2363
2364out_fail:
2365 vmlinux_path__exit();
2366 return -1;
2367}
2368
5ad90e4e 2369size_t machine__fprintf_vmlinux_path(struct machine *self, FILE *fp)
b0a9ab62
ACM
2370{
2371 int i;
2372 size_t printed = 0;
5ad90e4e
ACM
2373 struct dso *kdso = self->vmlinux_maps[MAP__FUNCTION]->dso;
2374
2375 if (kdso->has_build_id) {
2376 char filename[PATH_MAX];
2377 if (dso__build_id_filename(kdso, filename, sizeof(filename)))
2378 printed += fprintf(fp, "[0] %s\n", filename);
2379 }
b0a9ab62
ACM
2380
2381 for (i = 0; i < vmlinux_path__nr_entries; ++i)
5ad90e4e
ACM
2382 printed += fprintf(fp, "[%d] %s\n",
2383 i + kdso->has_build_id, vmlinux_path[i]);
b0a9ab62
ACM
2384
2385 return printed;
2386}
2387
655000e7
ACM
2388static int setup_list(struct strlist **list, const char *list_str,
2389 const char *list_name)
2390{
2391 if (list_str == NULL)
2392 return 0;
2393
2394 *list = strlist__new(true, list_str);
2395 if (!*list) {
2396 pr_err("problems parsing %s list\n", list_name);
2397 return -1;
2398 }
2399 return 0;
2400}
2401
75be6cf4 2402int symbol__init(void)
2446042c 2403{
ec5761ea
DA
2404 const char *symfs;
2405
85e00b55
JZ
2406 if (symbol_conf.initialized)
2407 return 0;
2408
4d439517
DM
2409 symbol_conf.priv_size = ALIGN(symbol_conf.priv_size, sizeof(u64));
2410
95011c60 2411 elf_version(EV_CURRENT);
75be6cf4
ACM
2412 if (symbol_conf.sort_by_name)
2413 symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
2414 sizeof(struct symbol));
b32d133a 2415
75be6cf4 2416 if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0)
2446042c
ACM
2417 return -1;
2418
c410a338
ACM
2419 if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
2420 pr_err("'.' is the only non valid --field-separator argument\n");
2421 return -1;
2422 }
2423
655000e7
ACM
2424 if (setup_list(&symbol_conf.dso_list,
2425 symbol_conf.dso_list_str, "dso") < 0)
2426 return -1;
2427
2428 if (setup_list(&symbol_conf.comm_list,
2429 symbol_conf.comm_list_str, "comm") < 0)
2430 goto out_free_dso_list;
2431
2432 if (setup_list(&symbol_conf.sym_list,
2433 symbol_conf.sym_list_str, "symbol") < 0)
2434 goto out_free_comm_list;
2435
ec5761ea
DA
2436 /*
2437 * A path to symbols of "/" is identical to ""
2438 * reset here for simplicity.
2439 */
2440 symfs = realpath(symbol_conf.symfs, NULL);
2441 if (symfs == NULL)
2442 symfs = symbol_conf.symfs;
2443 if (strcmp(symfs, "/") == 0)
2444 symbol_conf.symfs = "";
2445 if (symfs != symbol_conf.symfs)
2446 free((void *)symfs);
2447
85e00b55 2448 symbol_conf.initialized = true;
4aa65636 2449 return 0;
655000e7
ACM
2450
2451out_free_dso_list:
2452 strlist__delete(symbol_conf.dso_list);
2453out_free_comm_list:
2454 strlist__delete(symbol_conf.comm_list);
2455 return -1;
4aa65636
ACM
2456}
2457
d65a458b
ACM
2458void symbol__exit(void)
2459{
85e00b55
JZ
2460 if (!symbol_conf.initialized)
2461 return;
d65a458b
ACM
2462 strlist__delete(symbol_conf.sym_list);
2463 strlist__delete(symbol_conf.dso_list);
2464 strlist__delete(symbol_conf.comm_list);
2465 vmlinux_path__exit();
2466 symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
85e00b55 2467 symbol_conf.initialized = false;
d65a458b
ACM
2468}
2469
d28c6223 2470int machines__create_kernel_maps(struct rb_root *self, pid_t pid)
4aa65636 2471{
d28c6223 2472 struct machine *machine = machines__findnew(self, pid);
9de89fe7 2473
23346f21 2474 if (machine == NULL)
a1645ce1 2475 return -1;
cc612d81 2476
5c0541d5 2477 return machine__create_kernel_maps(machine);
cd84c2ac 2478}
5aab621b
ACM
2479
2480static int hex(char ch)
2481{
2482 if ((ch >= '0') && (ch <= '9'))
2483 return ch - '0';
2484 if ((ch >= 'a') && (ch <= 'f'))
2485 return ch - 'a' + 10;
2486 if ((ch >= 'A') && (ch <= 'F'))
2487 return ch - 'A' + 10;
2488 return -1;
2489}
2490
2491/*
2492 * While we find nice hex chars, build a long_val.
2493 * Return number of chars processed.
2494 */
2495int hex2u64(const char *ptr, u64 *long_val)
2496{
2497 const char *p = ptr;
2498 *long_val = 0;
2499
2500 while (*p) {
2501 const int hex_val = hex(*p);
2502
2503 if (hex_val < 0)
2504 break;
2505
2506 *long_val = (*long_val << 4) | hex_val;
2507 p++;
2508 }
2509
2510 return p - ptr;
2511}
2512
2513char *strxfrchar(char *s, char from, char to)
2514{
2515 char *p = s;
2516
2517 while ((p = strchr(p, from)) != NULL)
2518 *p++ = to;
2519
2520 return s;
2521}
a1645ce1 2522
d28c6223 2523int machines__create_guest_kernel_maps(struct rb_root *self)
a1645ce1
ZY
2524{
2525 int ret = 0;
2526 struct dirent **namelist = NULL;
2527 int i, items = 0;
2528 char path[PATH_MAX];
2529 pid_t pid;
2530
2531 if (symbol_conf.default_guest_vmlinux_name ||
2532 symbol_conf.default_guest_modules ||
2533 symbol_conf.default_guest_kallsyms) {
d28c6223 2534 machines__create_kernel_maps(self, DEFAULT_GUEST_KERNEL_ID);
a1645ce1
ZY
2535 }
2536
2537 if (symbol_conf.guestmount) {
2538 items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
2539 if (items <= 0)
2540 return -ENOENT;
2541 for (i = 0; i < items; i++) {
2542 if (!isdigit(namelist[i]->d_name[0])) {
2543 /* Filter out . and .. */
2544 continue;
2545 }
2546 pid = atoi(namelist[i]->d_name);
2547 sprintf(path, "%s/%s/proc/kallsyms",
2548 symbol_conf.guestmount,
2549 namelist[i]->d_name);
2550 ret = access(path, R_OK);
2551 if (ret) {
2552 pr_debug("Can't access file %s\n", path);
2553 goto failure;
2554 }
d28c6223 2555 machines__create_kernel_maps(self, pid);
a1645ce1
ZY
2556 }
2557failure:
2558 free(namelist);
2559 }
2560
2561 return ret;
2562}
5c0541d5 2563
076c6e45
ACM
2564void machines__destroy_guest_kernel_maps(struct rb_root *self)
2565{
2566 struct rb_node *next = rb_first(self);
2567
2568 while (next) {
2569 struct machine *pos = rb_entry(next, struct machine, rb_node);
2570
2571 next = rb_next(&pos->rb_node);
2572 rb_erase(&pos->rb_node, self);
2573 machine__delete(pos);
2574 }
2575}
2576
5c0541d5
ACM
2577int machine__load_kallsyms(struct machine *self, const char *filename,
2578 enum map_type type, symbol_filter_t filter)
2579{
2580 struct map *map = self->vmlinux_maps[type];
2581 int ret = dso__load_kallsyms(map->dso, filename, map, filter);
2582
2583 if (ret > 0) {
2584 dso__set_loaded(map->dso, type);
2585 /*
2586 * Since /proc/kallsyms will have multiple sessions for the
2587 * kernel, with modules between them, fixup the end of all
2588 * sections.
2589 */
2590 __map_groups__fixup_end(&self->kmaps, type);
2591 }
2592
2593 return ret;
2594}
2595
2596int machine__load_vmlinux_path(struct machine *self, enum map_type type,
2597 symbol_filter_t filter)
2598{
2599 struct map *map = self->vmlinux_maps[type];
2600 int ret = dso__load_vmlinux_path(map->dso, map, filter);
2601
2602 if (ret > 0) {
2603 dso__set_loaded(map->dso, type);
2604 map__reloc_vmlinux(map);
2605 }
2606
2607 return ret;
2608}
This page took 0.277731 seconds and 5 git commands to generate.