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