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