Arnaldo Carvalho de Melo | 5aab621 | 2010-03-25 19:59:00 -0300 | [diff] [blame] | 1 | #include <dirent.h> |
| 2 | #include <errno.h> |
Arnaldo Carvalho de Melo | 5aab621 | 2010-03-25 19:59:00 -0300 | [diff] [blame] | 3 | #include <stdlib.h> |
| 4 | #include <stdio.h> |
| 5 | #include <string.h> |
| 6 | #include <sys/types.h> |
| 7 | #include <sys/stat.h> |
| 8 | #include <sys/param.h> |
| 9 | #include <fcntl.h> |
| 10 | #include <unistd.h> |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 11 | #include <inttypes.h> |
Arnaldo Carvalho de Melo | b36f19d | 2010-05-20 12:15:33 -0300 | [diff] [blame] | 12 | #include "build-id.h" |
Namhyung Kim | e334c72 | 2012-02-10 10:10:17 +0900 | [diff] [blame] | 13 | #include "util.h" |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 14 | #include "debug.h" |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 15 | #include "machine.h" |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 16 | #include "symbol.h" |
Arnaldo Carvalho de Melo | 5aab621 | 2010-03-25 19:59:00 -0300 | [diff] [blame] | 17 | #include "strlist.h" |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 18 | |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 19 | #include <elf.h> |
Arnaldo Carvalho de Melo | f1617b4 | 2009-11-18 20:20:52 -0200 | [diff] [blame] | 20 | #include <limits.h> |
Arnaldo Carvalho de Melo | c506c96 | 2013-12-11 09:15:00 -0300 | [diff] [blame] | 21 | #include <symbol/kallsyms.h> |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 22 | #include <sys/utsname.h> |
Peter Zijlstra | 2cdbc46 | 2009-08-05 14:05:16 +0200 | [diff] [blame] | 23 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 24 | 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] | 25 | symbol_filter_t filter); |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 26 | 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] | 27 | symbol_filter_t filter); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 28 | int vmlinux_path__nr_entries; |
| 29 | char **vmlinux_path; |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 30 | |
Arnaldo Carvalho de Melo | 75be6cf | 2009-12-15 20:04:39 -0200 | [diff] [blame] | 31 | struct symbol_conf symbol_conf = { |
Namhyung Kim | e511db5 | 2013-12-24 16:19:25 +0900 | [diff] [blame] | 32 | .use_modules = true, |
| 33 | .try_vmlinux_path = true, |
| 34 | .annotate_src = true, |
| 35 | .demangle = true, |
| 36 | .cumulate_callchain = true, |
Jiri Olsa | c830236 | 2014-06-27 18:26:58 +0200 | [diff] [blame] | 37 | .show_hist_headers = true, |
Namhyung Kim | e511db5 | 2013-12-24 16:19:25 +0900 | [diff] [blame] | 38 | .symfs = "", |
Arnaldo Carvalho de Melo | b32d133 | 2009-11-24 12:05:15 -0200 | [diff] [blame] | 39 | }; |
| 40 | |
Jiri Olsa | 44f24cb | 2012-07-22 14:14:32 +0200 | [diff] [blame] | 41 | static enum dso_binary_type binary_type_symtab[] = { |
| 42 | DSO_BINARY_TYPE__KALLSYMS, |
| 43 | DSO_BINARY_TYPE__GUEST_KALLSYMS, |
| 44 | DSO_BINARY_TYPE__JAVA_JIT, |
| 45 | DSO_BINARY_TYPE__DEBUGLINK, |
| 46 | DSO_BINARY_TYPE__BUILD_ID_CACHE, |
| 47 | DSO_BINARY_TYPE__FEDORA_DEBUGINFO, |
| 48 | DSO_BINARY_TYPE__UBUNTU_DEBUGINFO, |
| 49 | DSO_BINARY_TYPE__BUILDID_DEBUGINFO, |
| 50 | DSO_BINARY_TYPE__SYSTEM_PATH_DSO, |
| 51 | DSO_BINARY_TYPE__GUEST_KMODULE, |
| 52 | DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE, |
Ricardo Ribalda Delgado | 9cd0094 | 2013-09-18 15:56:14 +0200 | [diff] [blame] | 53 | DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO, |
Jiri Olsa | 44f24cb | 2012-07-22 14:14:32 +0200 | [diff] [blame] | 54 | DSO_BINARY_TYPE__NOT_FOUND, |
| 55 | }; |
| 56 | |
Jiri Olsa | 028df76 | 2012-08-01 14:47:57 +0200 | [diff] [blame] | 57 | #define DSO_BINARY_TYPE__SYMTAB_CNT ARRAY_SIZE(binary_type_symtab) |
Jiri Olsa | 44f24cb | 2012-07-22 14:14:32 +0200 | [diff] [blame] | 58 | |
Arnaldo Carvalho de Melo | 36a3e64 | 2010-01-04 16:19:27 -0200 | [diff] [blame] | 59 | 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] | 60 | { |
Anton Blanchard | 3187790 | 2011-08-24 16:40:16 +1000 | [diff] [blame] | 61 | symbol_type = toupper(symbol_type); |
| 62 | |
Arnaldo Carvalho de Melo | 6893d4e | 2009-12-11 14:50:37 -0200 | [diff] [blame] | 63 | switch (map_type) { |
| 64 | case MAP__FUNCTION: |
| 65 | return symbol_type == 'T' || symbol_type == 'W'; |
Arnaldo Carvalho de Melo | f1dfa0b | 2009-12-11 14:50:39 -0200 | [diff] [blame] | 66 | case MAP__VARIABLE: |
Anton Blanchard | 3187790 | 2011-08-24 16:40:16 +1000 | [diff] [blame] | 67 | return symbol_type == 'D'; |
Arnaldo Carvalho de Melo | 6893d4e | 2009-12-11 14:50:37 -0200 | [diff] [blame] | 68 | default: |
| 69 | return false; |
| 70 | } |
| 71 | } |
| 72 | |
Anton Blanchard | 694bf40 | 2011-08-24 16:40:17 +1000 | [diff] [blame] | 73 | static int prefix_underscores_count(const char *str) |
| 74 | { |
| 75 | const char *tail = str; |
| 76 | |
| 77 | while (*tail == '_') |
| 78 | tail++; |
| 79 | |
| 80 | return tail - str; |
| 81 | } |
| 82 | |
| 83 | #define SYMBOL_A 0 |
| 84 | #define SYMBOL_B 1 |
| 85 | |
| 86 | static int choose_best_symbol(struct symbol *syma, struct symbol *symb) |
| 87 | { |
| 88 | s64 a; |
| 89 | s64 b; |
Adrian Hunter | 3445432 | 2013-08-07 14:38:49 +0300 | [diff] [blame] | 90 | size_t na, nb; |
Anton Blanchard | 694bf40 | 2011-08-24 16:40:17 +1000 | [diff] [blame] | 91 | |
| 92 | /* Prefer a symbol with non zero length */ |
| 93 | a = syma->end - syma->start; |
| 94 | b = symb->end - symb->start; |
| 95 | if ((b == 0) && (a > 0)) |
| 96 | return SYMBOL_A; |
| 97 | else if ((a == 0) && (b > 0)) |
| 98 | return SYMBOL_B; |
| 99 | |
| 100 | /* Prefer a non weak symbol over a weak one */ |
| 101 | a = syma->binding == STB_WEAK; |
| 102 | b = symb->binding == STB_WEAK; |
| 103 | if (b && !a) |
| 104 | return SYMBOL_A; |
| 105 | if (a && !b) |
| 106 | return SYMBOL_B; |
| 107 | |
| 108 | /* Prefer a global symbol over a non global one */ |
| 109 | a = syma->binding == STB_GLOBAL; |
| 110 | b = symb->binding == STB_GLOBAL; |
| 111 | if (a && !b) |
| 112 | return SYMBOL_A; |
| 113 | if (b && !a) |
| 114 | return SYMBOL_B; |
| 115 | |
| 116 | /* Prefer a symbol with less underscores */ |
| 117 | a = prefix_underscores_count(syma->name); |
| 118 | b = prefix_underscores_count(symb->name); |
| 119 | if (b > a) |
| 120 | return SYMBOL_A; |
| 121 | else if (a > b) |
| 122 | return SYMBOL_B; |
| 123 | |
Adrian Hunter | 3445432 | 2013-08-07 14:38:49 +0300 | [diff] [blame] | 124 | /* Choose the symbol with the longest name */ |
| 125 | na = strlen(syma->name); |
| 126 | nb = strlen(symb->name); |
| 127 | if (na > nb) |
Anton Blanchard | 694bf40 | 2011-08-24 16:40:17 +1000 | [diff] [blame] | 128 | return SYMBOL_A; |
Adrian Hunter | 3445432 | 2013-08-07 14:38:49 +0300 | [diff] [blame] | 129 | else if (na < nb) |
Anton Blanchard | 694bf40 | 2011-08-24 16:40:17 +1000 | [diff] [blame] | 130 | return SYMBOL_B; |
Adrian Hunter | 3445432 | 2013-08-07 14:38:49 +0300 | [diff] [blame] | 131 | |
| 132 | /* Avoid "SyS" kernel syscall aliases */ |
| 133 | if (na >= 3 && !strncmp(syma->name, "SyS", 3)) |
| 134 | return SYMBOL_B; |
| 135 | if (na >= 10 && !strncmp(syma->name, "compat_SyS", 10)) |
| 136 | return SYMBOL_B; |
| 137 | |
| 138 | return SYMBOL_A; |
Anton Blanchard | 694bf40 | 2011-08-24 16:40:17 +1000 | [diff] [blame] | 139 | } |
| 140 | |
Namhyung Kim | e5a1845 | 2012-08-06 13:41:20 +0900 | [diff] [blame] | 141 | void symbols__fixup_duplicate(struct rb_root *symbols) |
Anton Blanchard | 694bf40 | 2011-08-24 16:40:17 +1000 | [diff] [blame] | 142 | { |
| 143 | struct rb_node *nd; |
| 144 | struct symbol *curr, *next; |
| 145 | |
| 146 | nd = rb_first(symbols); |
| 147 | |
| 148 | while (nd) { |
| 149 | curr = rb_entry(nd, struct symbol, rb_node); |
| 150 | again: |
| 151 | nd = rb_next(&curr->rb_node); |
| 152 | next = rb_entry(nd, struct symbol, rb_node); |
| 153 | |
| 154 | if (!nd) |
| 155 | break; |
| 156 | |
| 157 | if (curr->start != next->start) |
| 158 | continue; |
| 159 | |
| 160 | if (choose_best_symbol(curr, next) == SYMBOL_A) { |
| 161 | rb_erase(&next->rb_node, symbols); |
Chenggang Qin | d4f74eb | 2013-10-11 08:27:59 +0800 | [diff] [blame] | 162 | symbol__delete(next); |
Anton Blanchard | 694bf40 | 2011-08-24 16:40:17 +1000 | [diff] [blame] | 163 | goto again; |
| 164 | } else { |
| 165 | nd = rb_next(&curr->rb_node); |
| 166 | rb_erase(&curr->rb_node, symbols); |
Chenggang Qin | d4f74eb | 2013-10-11 08:27:59 +0800 | [diff] [blame] | 167 | symbol__delete(curr); |
Anton Blanchard | 694bf40 | 2011-08-24 16:40:17 +1000 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
Namhyung Kim | e5a1845 | 2012-08-06 13:41:20 +0900 | [diff] [blame] | 172 | void symbols__fixup_end(struct rb_root *symbols) |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 173 | { |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 174 | struct rb_node *nd, *prevnd = rb_first(symbols); |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 175 | struct symbol *curr, *prev; |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 176 | |
| 177 | if (prevnd == NULL) |
| 178 | return; |
| 179 | |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 180 | curr = rb_entry(prevnd, struct symbol, rb_node); |
| 181 | |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 182 | for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) { |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 183 | prev = curr; |
| 184 | curr = rb_entry(nd, struct symbol, rb_node); |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 185 | |
Arnaldo Carvalho de Melo | 3b01a41 | 2010-12-22 01:08:36 -0200 | [diff] [blame] | 186 | if (prev->end == prev->start && prev->end != curr->start) |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 187 | prev->end = curr->start - 1; |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 188 | } |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 189 | |
| 190 | /* Last entry */ |
| 191 | if (curr->end == curr->start) |
| 192 | curr->end = roundup(curr->start, 4096); |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 193 | } |
| 194 | |
Namhyung Kim | e5a1845 | 2012-08-06 13:41:20 +0900 | [diff] [blame] | 195 | 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] | 196 | { |
| 197 | struct map *prev, *curr; |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 198 | struct rb_node *nd, *prevnd = rb_first(&mg->maps[type]); |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 199 | |
| 200 | if (prevnd == NULL) |
| 201 | return; |
| 202 | |
| 203 | curr = rb_entry(prevnd, struct map, rb_node); |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 204 | |
| 205 | for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) { |
| 206 | prev = curr; |
| 207 | curr = rb_entry(nd, struct map, rb_node); |
| 208 | prev->end = curr->start - 1; |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 209 | } |
Arnaldo Carvalho de Melo | 90c8321 | 2009-11-21 14:31:24 -0200 | [diff] [blame] | 210 | |
| 211 | /* |
| 212 | * We still haven't the actual symbols, so guess the |
| 213 | * last map final address. |
| 214 | */ |
Ian Munsie | 9d1faba | 2010-11-25 15:12:53 +1100 | [diff] [blame] | 215 | curr->end = ~0ULL; |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 216 | } |
| 217 | |
Namhyung Kim | e5a1845 | 2012-08-06 13:41:20 +0900 | [diff] [blame] | 218 | struct symbol *symbol__new(u64 start, u64 len, u8 binding, const char *name) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 219 | { |
Arnaldo Carvalho de Melo | 0085c954 | 2009-05-28 14:55:13 -0300 | [diff] [blame] | 220 | size_t namelen = strlen(name) + 1; |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 221 | struct symbol *sym = calloc(1, (symbol_conf.priv_size + |
| 222 | sizeof(*sym) + namelen)); |
| 223 | if (sym == NULL) |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 224 | return NULL; |
| 225 | |
Arnaldo Carvalho de Melo | 75be6cf | 2009-12-15 20:04:39 -0200 | [diff] [blame] | 226 | if (symbol_conf.priv_size) |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 227 | sym = ((void *)sym) + symbol_conf.priv_size; |
Arnaldo Carvalho de Melo | 3647948 | 2009-11-24 12:05:16 -0200 | [diff] [blame] | 228 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 229 | sym->start = start; |
| 230 | sym->end = len ? start + len - 1 : start; |
| 231 | sym->binding = binding; |
| 232 | sym->namelen = namelen - 1; |
Arnaldo Carvalho de Melo | e420499 | 2009-10-20 14:25:40 -0200 | [diff] [blame] | 233 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 234 | pr_debug4("%s: %s %#" PRIx64 "-%#" PRIx64 "\n", |
| 235 | __func__, name, start, sym->end); |
| 236 | memcpy(sym->name, name, namelen); |
Arnaldo Carvalho de Melo | e420499 | 2009-10-20 14:25:40 -0200 | [diff] [blame] | 237 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 238 | return sym; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 239 | } |
| 240 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 241 | void symbol__delete(struct symbol *sym) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 242 | { |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 243 | free(((void *)sym) - symbol_conf.priv_size); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 244 | } |
| 245 | |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 246 | size_t symbol__fprintf(struct symbol *sym, FILE *fp) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 247 | { |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 248 | return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %c %s\n", |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 249 | sym->start, sym->end, |
| 250 | sym->binding == STB_GLOBAL ? 'g' : |
| 251 | sym->binding == STB_LOCAL ? 'l' : 'w', |
| 252 | sym->name); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 253 | } |
| 254 | |
Akihiro Nagai | a978f2a | 2012-01-30 13:43:15 +0900 | [diff] [blame] | 255 | size_t symbol__fprintf_symname_offs(const struct symbol *sym, |
| 256 | const struct addr_location *al, FILE *fp) |
| 257 | { |
| 258 | unsigned long offset; |
| 259 | size_t length; |
| 260 | |
| 261 | if (sym && sym->name) { |
| 262 | length = fprintf(fp, "%s", sym->name); |
| 263 | if (al) { |
David Ahern | 0b8c25d | 2013-07-28 09:48:32 -0600 | [diff] [blame] | 264 | if (al->addr < sym->end) |
| 265 | offset = al->addr - sym->start; |
| 266 | else |
| 267 | offset = al->addr - al->map->start - sym->start; |
Akihiro Nagai | a978f2a | 2012-01-30 13:43:15 +0900 | [diff] [blame] | 268 | length += fprintf(fp, "+0x%lx", offset); |
| 269 | } |
| 270 | return length; |
| 271 | } else |
| 272 | return fprintf(fp, "[unknown]"); |
| 273 | } |
| 274 | |
Akihiro Nagai | 547a92e | 2012-01-30 13:42:57 +0900 | [diff] [blame] | 275 | size_t symbol__fprintf_symname(const struct symbol *sym, FILE *fp) |
| 276 | { |
Akihiro Nagai | a978f2a | 2012-01-30 13:43:15 +0900 | [diff] [blame] | 277 | return symbol__fprintf_symname_offs(sym, NULL, fp); |
Akihiro Nagai | 547a92e | 2012-01-30 13:42:57 +0900 | [diff] [blame] | 278 | } |
| 279 | |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 280 | void symbols__delete(struct rb_root *symbols) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 281 | { |
| 282 | struct symbol *pos; |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 283 | struct rb_node *next = rb_first(symbols); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 284 | |
| 285 | while (next) { |
| 286 | pos = rb_entry(next, struct symbol, rb_node); |
| 287 | next = rb_next(&pos->rb_node); |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 288 | rb_erase(&pos->rb_node, symbols); |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 289 | symbol__delete(pos); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | |
Namhyung Kim | e5a1845 | 2012-08-06 13:41:20 +0900 | [diff] [blame] | 293 | void symbols__insert(struct rb_root *symbols, struct symbol *sym) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 294 | { |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 295 | struct rb_node **p = &symbols->rb_node; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 296 | struct rb_node *parent = NULL; |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 297 | const u64 ip = sym->start; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 298 | struct symbol *s; |
| 299 | |
| 300 | while (*p != NULL) { |
| 301 | parent = *p; |
| 302 | s = rb_entry(parent, struct symbol, rb_node); |
| 303 | if (ip < s->start) |
| 304 | p = &(*p)->rb_left; |
| 305 | else |
| 306 | p = &(*p)->rb_right; |
| 307 | } |
| 308 | rb_link_node(&sym->rb_node, parent, p); |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 309 | rb_insert_color(&sym->rb_node, symbols); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 310 | } |
| 311 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 312 | 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] | 313 | { |
| 314 | struct rb_node *n; |
| 315 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 316 | if (symbols == NULL) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 317 | return NULL; |
| 318 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 319 | n = symbols->rb_node; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 320 | |
| 321 | while (n) { |
| 322 | struct symbol *s = rb_entry(n, struct symbol, rb_node); |
| 323 | |
| 324 | if (ip < s->start) |
| 325 | n = n->rb_left; |
| 326 | else if (ip > s->end) |
| 327 | n = n->rb_right; |
| 328 | else |
| 329 | return s; |
| 330 | } |
| 331 | |
| 332 | return NULL; |
| 333 | } |
| 334 | |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 335 | static struct symbol *symbols__first(struct rb_root *symbols) |
| 336 | { |
| 337 | struct rb_node *n = rb_first(symbols); |
| 338 | |
| 339 | if (n) |
| 340 | return rb_entry(n, struct symbol, rb_node); |
| 341 | |
| 342 | return NULL; |
| 343 | } |
| 344 | |
Adrian Hunter | 9c00a81 | 2014-07-14 13:02:50 +0300 | [diff] [blame] | 345 | static struct symbol *symbols__next(struct symbol *sym) |
| 346 | { |
| 347 | struct rb_node *n = rb_next(&sym->rb_node); |
| 348 | |
| 349 | if (n) |
| 350 | return rb_entry(n, struct symbol, rb_node); |
| 351 | |
| 352 | return NULL; |
| 353 | } |
| 354 | |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 355 | struct symbol_name_rb_node { |
| 356 | struct rb_node rb_node; |
| 357 | struct symbol sym; |
| 358 | }; |
| 359 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 360 | 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] | 361 | { |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 362 | struct rb_node **p = &symbols->rb_node; |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 363 | struct rb_node *parent = NULL; |
Rabin Vincent | 02a9d03 | 2010-11-23 22:08:18 +0530 | [diff] [blame] | 364 | struct symbol_name_rb_node *symn, *s; |
| 365 | |
| 366 | symn = container_of(sym, struct symbol_name_rb_node, sym); |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 367 | |
| 368 | while (*p != NULL) { |
| 369 | parent = *p; |
| 370 | s = rb_entry(parent, struct symbol_name_rb_node, rb_node); |
| 371 | if (strcmp(sym->name, s->sym.name) < 0) |
| 372 | p = &(*p)->rb_left; |
| 373 | else |
| 374 | p = &(*p)->rb_right; |
| 375 | } |
| 376 | rb_link_node(&symn->rb_node, parent, p); |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 377 | rb_insert_color(&symn->rb_node, symbols); |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 378 | } |
| 379 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 380 | static void symbols__sort_by_name(struct rb_root *symbols, |
| 381 | struct rb_root *source) |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 382 | { |
| 383 | struct rb_node *nd; |
| 384 | |
| 385 | for (nd = rb_first(source); nd; nd = rb_next(nd)) { |
| 386 | struct symbol *pos = rb_entry(nd, struct symbol, rb_node); |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 387 | symbols__insert_by_name(symbols, pos); |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 388 | } |
| 389 | } |
| 390 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 391 | static struct symbol *symbols__find_by_name(struct rb_root *symbols, |
| 392 | const char *name) |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 393 | { |
| 394 | struct rb_node *n; |
| 395 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 396 | if (symbols == NULL) |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 397 | return NULL; |
| 398 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 399 | n = symbols->rb_node; |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 400 | |
| 401 | while (n) { |
| 402 | struct symbol_name_rb_node *s; |
| 403 | int cmp; |
| 404 | |
| 405 | s = rb_entry(n, struct symbol_name_rb_node, rb_node); |
| 406 | cmp = strcmp(name, s->sym.name); |
| 407 | |
| 408 | if (cmp < 0) |
| 409 | n = n->rb_left; |
| 410 | else if (cmp > 0) |
| 411 | n = n->rb_right; |
| 412 | else |
| 413 | return &s->sym; |
| 414 | } |
| 415 | |
| 416 | return NULL; |
| 417 | } |
| 418 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 419 | struct symbol *dso__find_symbol(struct dso *dso, |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 420 | enum map_type type, u64 addr) |
Arnaldo Carvalho de Melo | fcf1203 | 2009-11-24 13:01:52 -0200 | [diff] [blame] | 421 | { |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 422 | return symbols__find(&dso->symbols[type], addr); |
Arnaldo Carvalho de Melo | fcf1203 | 2009-11-24 13:01:52 -0200 | [diff] [blame] | 423 | } |
| 424 | |
Adrian Hunter | 9c00a81 | 2014-07-14 13:02:50 +0300 | [diff] [blame] | 425 | struct symbol *dso__first_symbol(struct dso *dso, enum map_type type) |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 426 | { |
| 427 | return symbols__first(&dso->symbols[type]); |
| 428 | } |
| 429 | |
Adrian Hunter | 9c00a81 | 2014-07-14 13:02:50 +0300 | [diff] [blame] | 430 | struct symbol *dso__next_symbol(struct symbol *sym) |
| 431 | { |
| 432 | return symbols__next(sym); |
| 433 | } |
| 434 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 435 | 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] | 436 | const char *name) |
| 437 | { |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 438 | return symbols__find_by_name(&dso->symbol_names[type], name); |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 439 | } |
| 440 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 441 | 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] | 442 | { |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 443 | dso__set_sorted_by_name(dso, type); |
| 444 | return symbols__sort_by_name(&dso->symbol_names[type], |
| 445 | &dso->symbols[type]); |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 446 | } |
| 447 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 448 | size_t dso__fprintf_symbols_by_name(struct dso *dso, |
| 449 | enum map_type type, FILE *fp) |
Srikar Dronamraju | 90f18e6 | 2010-08-25 19:13:29 +0530 | [diff] [blame] | 450 | { |
| 451 | size_t ret = 0; |
| 452 | struct rb_node *nd; |
| 453 | struct symbol_name_rb_node *pos; |
| 454 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 455 | 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] | 456 | pos = rb_entry(nd, struct symbol_name_rb_node, rb_node); |
| 457 | fprintf(fp, "%s\n", pos->sym.name); |
| 458 | } |
| 459 | |
| 460 | return ret; |
| 461 | } |
| 462 | |
Adrian Hunter | 316d70d | 2013-10-08 11:45:48 +0300 | [diff] [blame] | 463 | int modules__parse(const char *filename, void *arg, |
| 464 | int (*process_module)(void *arg, const char *name, |
| 465 | u64 start)) |
| 466 | { |
| 467 | char *line = NULL; |
| 468 | size_t n; |
| 469 | FILE *file; |
| 470 | int err = 0; |
| 471 | |
| 472 | file = fopen(filename, "r"); |
| 473 | if (file == NULL) |
| 474 | return -1; |
| 475 | |
| 476 | while (1) { |
| 477 | char name[PATH_MAX]; |
| 478 | u64 start; |
| 479 | char *sep; |
| 480 | ssize_t line_len; |
| 481 | |
| 482 | line_len = getline(&line, &n, file); |
| 483 | if (line_len < 0) { |
| 484 | if (feof(file)) |
| 485 | break; |
| 486 | err = -1; |
| 487 | goto out; |
| 488 | } |
| 489 | |
| 490 | if (!line) { |
| 491 | err = -1; |
| 492 | goto out; |
| 493 | } |
| 494 | |
| 495 | line[--line_len] = '\0'; /* \n */ |
| 496 | |
| 497 | sep = strrchr(line, 'x'); |
| 498 | if (sep == NULL) |
| 499 | continue; |
| 500 | |
| 501 | hex2u64(sep + 1, &start); |
| 502 | |
| 503 | sep = strchr(line, ' '); |
| 504 | if (sep == NULL) |
| 505 | continue; |
| 506 | |
| 507 | *sep = '\0'; |
| 508 | |
| 509 | scnprintf(name, sizeof(name), "[%s]", line); |
| 510 | |
| 511 | err = process_module(arg, name, start); |
| 512 | if (err) |
| 513 | break; |
| 514 | } |
| 515 | out: |
| 516 | free(line); |
| 517 | fclose(file); |
| 518 | return err; |
| 519 | } |
| 520 | |
Arnaldo Carvalho de Melo | 682b335 | 2010-01-04 16:19:26 -0200 | [diff] [blame] | 521 | struct process_kallsyms_args { |
| 522 | struct map *map; |
| 523 | struct dso *dso; |
| 524 | }; |
| 525 | |
David Ahern | 82d1deb | 2013-11-18 13:32:45 -0700 | [diff] [blame] | 526 | bool symbol__is_idle(struct symbol *sym) |
| 527 | { |
| 528 | const char * const idle_symbols[] = { |
| 529 | "cpu_idle", |
| 530 | "intel_idle", |
| 531 | "default_idle", |
| 532 | "native_safe_halt", |
| 533 | "enter_idle", |
| 534 | "exit_idle", |
| 535 | "mwait_idle", |
| 536 | "mwait_idle_with_hints", |
| 537 | "poll_idle", |
| 538 | "ppc64_runlatch_off", |
| 539 | "pseries_dedicated_idle_sleep", |
| 540 | NULL |
| 541 | }; |
| 542 | |
| 543 | int i; |
| 544 | |
| 545 | if (!sym) |
| 546 | return false; |
| 547 | |
| 548 | for (i = 0; idle_symbols[i]; i++) { |
| 549 | if (!strcmp(idle_symbols[i], sym->name)) |
| 550 | return true; |
| 551 | } |
| 552 | |
| 553 | return false; |
| 554 | } |
| 555 | |
Arnaldo Carvalho de Melo | 682b335 | 2010-01-04 16:19:26 -0200 | [diff] [blame] | 556 | static int map__process_kallsym_symbol(void *arg, const char *name, |
Cody P Schafer | 8215152 | 2012-08-10 15:22:48 -0700 | [diff] [blame] | 557 | char type, u64 start) |
Arnaldo Carvalho de Melo | 682b335 | 2010-01-04 16:19:26 -0200 | [diff] [blame] | 558 | { |
| 559 | struct symbol *sym; |
| 560 | struct process_kallsyms_args *a = arg; |
| 561 | struct rb_root *root = &a->dso->symbols[a->map->type]; |
| 562 | |
| 563 | if (!symbol_type__is_a(type, a->map->type)) |
| 564 | return 0; |
| 565 | |
Cody P Schafer | 8215152 | 2012-08-10 15:22:48 -0700 | [diff] [blame] | 566 | /* |
| 567 | * module symbols are not sorted so we add all |
| 568 | * symbols, setting length to 0, and rely on |
| 569 | * symbols__fixup_end() to fix it up. |
| 570 | */ |
| 571 | sym = symbol__new(start, 0, kallsyms2elf_type(type), name); |
Arnaldo Carvalho de Melo | 682b335 | 2010-01-04 16:19:26 -0200 | [diff] [blame] | 572 | if (sym == NULL) |
| 573 | return -ENOMEM; |
| 574 | /* |
| 575 | * We will pass the symbols to the filter later, in |
| 576 | * map__split_kallsyms, when we have split the maps per module |
| 577 | */ |
| 578 | symbols__insert(root, sym); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 579 | |
Arnaldo Carvalho de Melo | 682b335 | 2010-01-04 16:19:26 -0200 | [diff] [blame] | 580 | return 0; |
| 581 | } |
| 582 | |
| 583 | /* |
| 584 | * Loads the function entries in /proc/kallsyms into kernel_map->dso, |
| 585 | * so that we can in the next step set the symbol ->end address and then |
| 586 | * call kernel_maps__split_kallsyms. |
| 587 | */ |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 588 | 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] | 589 | struct map *map) |
Arnaldo Carvalho de Melo | 682b335 | 2010-01-04 16:19:26 -0200 | [diff] [blame] | 590 | { |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 591 | struct process_kallsyms_args args = { .map = map, .dso = dso, }; |
Arnaldo Carvalho de Melo | 9e20144 | 2010-01-14 18:30:06 -0200 | [diff] [blame] | 592 | return kallsyms__parse(filename, &args, map__process_kallsym_symbol); |
Arnaldo Carvalho de Melo | 682b335 | 2010-01-04 16:19:26 -0200 | [diff] [blame] | 593 | } |
| 594 | |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 595 | static int dso__split_kallsyms_for_kcore(struct dso *dso, struct map *map, |
| 596 | symbol_filter_t filter) |
| 597 | { |
| 598 | struct map_groups *kmaps = map__kmap(map)->kmaps; |
| 599 | struct map *curr_map; |
| 600 | struct symbol *pos; |
| 601 | int count = 0, moved = 0; |
| 602 | struct rb_root *root = &dso->symbols[map->type]; |
| 603 | struct rb_node *next = rb_first(root); |
| 604 | |
| 605 | while (next) { |
| 606 | char *module; |
| 607 | |
| 608 | pos = rb_entry(next, struct symbol, rb_node); |
| 609 | next = rb_next(&pos->rb_node); |
| 610 | |
| 611 | module = strchr(pos->name, '\t'); |
| 612 | if (module) |
| 613 | *module = '\0'; |
| 614 | |
| 615 | curr_map = map_groups__find(kmaps, map->type, pos->start); |
| 616 | |
| 617 | if (!curr_map || (filter && filter(curr_map, pos))) { |
| 618 | rb_erase(&pos->rb_node, root); |
| 619 | symbol__delete(pos); |
| 620 | } else { |
| 621 | pos->start -= curr_map->start - curr_map->pgoff; |
| 622 | if (pos->end) |
| 623 | pos->end -= curr_map->start - curr_map->pgoff; |
| 624 | if (curr_map != map) { |
| 625 | rb_erase(&pos->rb_node, root); |
| 626 | symbols__insert( |
| 627 | &curr_map->dso->symbols[curr_map->type], |
| 628 | pos); |
| 629 | ++moved; |
| 630 | } else { |
| 631 | ++count; |
| 632 | } |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | /* Symbols have been adjusted */ |
| 637 | dso->adjust_symbols = 1; |
| 638 | |
| 639 | return count + moved; |
| 640 | } |
| 641 | |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 642 | /* |
| 643 | * Split the symbols into maps, making sure there are no overlaps, i.e. the |
| 644 | * kernel range is broken in several maps, named [kernel].N, as we don't have |
| 645 | * the original ELF section names vmlinux have. |
| 646 | */ |
Adrian Hunter | d9b62ab | 2014-01-29 16:14:43 +0200 | [diff] [blame] | 647 | static int dso__split_kallsyms(struct dso *dso, struct map *map, u64 delta, |
Arnaldo Carvalho de Melo | 9de89fe | 2010-02-03 16:52:00 -0200 | [diff] [blame] | 648 | symbol_filter_t filter) |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 649 | { |
Arnaldo Carvalho de Melo | 9de89fe | 2010-02-03 16:52:00 -0200 | [diff] [blame] | 650 | struct map_groups *kmaps = map__kmap(map)->kmaps; |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 651 | struct machine *machine = kmaps->machine; |
Arnaldo Carvalho de Melo | 4e06255 | 2009-11-27 16:29:18 -0200 | [diff] [blame] | 652 | struct map *curr_map = map; |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 653 | struct symbol *pos; |
Arnaldo Carvalho de Melo | 8a95331 | 2010-11-29 12:44:15 -0200 | [diff] [blame] | 654 | int count = 0, moved = 0; |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 655 | struct rb_root *root = &dso->symbols[map->type]; |
Arnaldo Carvalho de Melo | 4e06255 | 2009-11-27 16:29:18 -0200 | [diff] [blame] | 656 | struct rb_node *next = rb_first(root); |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 657 | int kernel_range = 0; |
| 658 | |
| 659 | while (next) { |
| 660 | char *module; |
| 661 | |
| 662 | pos = rb_entry(next, struct symbol, rb_node); |
| 663 | next = rb_next(&pos->rb_node); |
| 664 | |
| 665 | module = strchr(pos->name, '\t'); |
| 666 | if (module) { |
Arnaldo Carvalho de Melo | 75be6cf | 2009-12-15 20:04:39 -0200 | [diff] [blame] | 667 | if (!symbol_conf.use_modules) |
Arnaldo Carvalho de Melo | 1de8e24 | 2009-11-27 16:29:21 -0200 | [diff] [blame] | 668 | goto discard_symbol; |
| 669 | |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 670 | *module++ = '\0'; |
| 671 | |
Arnaldo Carvalho de Melo | b7cece76 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 672 | if (strcmp(curr_map->dso->short_name, module)) { |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 673 | if (curr_map != map && |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 674 | dso->kernel == DSO_TYPE_GUEST_KERNEL && |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 675 | machine__is_default_guest(machine)) { |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 676 | /* |
| 677 | * We assume all symbols of a module are |
| 678 | * continuous in * kallsyms, so curr_map |
| 679 | * points to a module and all its |
| 680 | * symbols are in its kmap. Mark it as |
| 681 | * loaded. |
| 682 | */ |
| 683 | dso__set_loaded(curr_map->dso, |
| 684 | curr_map->type); |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 685 | } |
Arnaldo Carvalho de Melo | b7cece76 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 686 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 687 | curr_map = map_groups__find_by_name(kmaps, |
| 688 | map->type, module); |
| 689 | if (curr_map == NULL) { |
Arnaldo Carvalho de Melo | 2f51903 | 2010-05-17 17:57:59 -0300 | [diff] [blame] | 690 | pr_debug("%s/proc/{kallsyms,modules} " |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 691 | "inconsistency while looking " |
| 692 | "for \"%s\" module!\n", |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 693 | machine->root_dir, module); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 694 | curr_map = map; |
| 695 | goto discard_symbol; |
| 696 | } |
| 697 | |
| 698 | if (curr_map->dso->loaded && |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 699 | !machine__is_default_guest(machine)) |
Arnaldo Carvalho de Melo | b7cece76 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 700 | goto discard_symbol; |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 701 | } |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 702 | /* |
| 703 | * So that we look just like we get from .ko files, |
| 704 | * i.e. not prelinked, relative to map->start. |
| 705 | */ |
Arnaldo Carvalho de Melo | 4e06255 | 2009-11-27 16:29:18 -0200 | [diff] [blame] | 706 | pos->start = curr_map->map_ip(curr_map, pos->start); |
| 707 | pos->end = curr_map->map_ip(curr_map, pos->end); |
| 708 | } else if (curr_map != map) { |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 709 | char dso_name[PATH_MAX]; |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 710 | struct dso *ndso; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 711 | |
Adrian Hunter | d9b62ab | 2014-01-29 16:14:43 +0200 | [diff] [blame] | 712 | if (delta) { |
| 713 | /* Kernel was relocated at boot time */ |
| 714 | pos->start -= delta; |
| 715 | pos->end -= delta; |
| 716 | } |
| 717 | |
Arnaldo Carvalho de Melo | 8a95331 | 2010-11-29 12:44:15 -0200 | [diff] [blame] | 718 | if (count == 0) { |
| 719 | curr_map = map; |
| 720 | goto filter_symbol; |
| 721 | } |
| 722 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 723 | if (dso->kernel == DSO_TYPE_GUEST_KERNEL) |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 724 | snprintf(dso_name, sizeof(dso_name), |
| 725 | "[guest.kernel].%d", |
| 726 | kernel_range++); |
| 727 | else |
| 728 | snprintf(dso_name, sizeof(dso_name), |
| 729 | "[kernel].%d", |
| 730 | kernel_range++); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 731 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 732 | ndso = dso__new(dso_name); |
| 733 | if (ndso == NULL) |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 734 | return -1; |
| 735 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 736 | ndso->kernel = dso->kernel; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 737 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 738 | curr_map = map__new2(pos->start, ndso, map->type); |
Zhang, Yanmin | 37fe5fc | 2010-02-25 11:00:51 +0800 | [diff] [blame] | 739 | if (curr_map == NULL) { |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 740 | dso__delete(ndso); |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 741 | return -1; |
| 742 | } |
| 743 | |
Arnaldo Carvalho de Melo | 4e06255 | 2009-11-27 16:29:18 -0200 | [diff] [blame] | 744 | 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] | 745 | map_groups__insert(kmaps, curr_map); |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 746 | ++kernel_range; |
Adrian Hunter | d9b62ab | 2014-01-29 16:14:43 +0200 | [diff] [blame] | 747 | } else if (delta) { |
| 748 | /* Kernel was relocated at boot time */ |
| 749 | pos->start -= delta; |
| 750 | pos->end -= delta; |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 751 | } |
Arnaldo Carvalho de Melo | 8a95331 | 2010-11-29 12:44:15 -0200 | [diff] [blame] | 752 | filter_symbol: |
Arnaldo Carvalho de Melo | 4e06255 | 2009-11-27 16:29:18 -0200 | [diff] [blame] | 753 | if (filter && filter(curr_map, pos)) { |
Arnaldo Carvalho de Melo | 1de8e24 | 2009-11-27 16:29:21 -0200 | [diff] [blame] | 754 | discard_symbol: rb_erase(&pos->rb_node, root); |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 755 | symbol__delete(pos); |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 756 | } else { |
Arnaldo Carvalho de Melo | 4e06255 | 2009-11-27 16:29:18 -0200 | [diff] [blame] | 757 | if (curr_map != map) { |
| 758 | rb_erase(&pos->rb_node, root); |
| 759 | symbols__insert(&curr_map->dso->symbols[curr_map->type], pos); |
Arnaldo Carvalho de Melo | 8a95331 | 2010-11-29 12:44:15 -0200 | [diff] [blame] | 760 | ++moved; |
| 761 | } else |
| 762 | ++count; |
Mike Galbraith | 9974f49 | 2009-07-02 08:05:58 +0200 | [diff] [blame] | 763 | } |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 764 | } |
| 765 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 766 | if (curr_map != map && |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 767 | dso->kernel == DSO_TYPE_GUEST_KERNEL && |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 768 | machine__is_default_guest(kmaps->machine)) { |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 769 | dso__set_loaded(curr_map->dso, curr_map->type); |
| 770 | } |
| 771 | |
Arnaldo Carvalho de Melo | 8a95331 | 2010-11-29 12:44:15 -0200 | [diff] [blame] | 772 | return count + moved; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 773 | } |
| 774 | |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 775 | bool symbol__restricted_filename(const char *filename, |
| 776 | const char *restricted_filename) |
Arnaldo Carvalho de Melo | ec80fde | 2011-05-26 09:53:51 -0300 | [diff] [blame] | 777 | { |
| 778 | bool restricted = false; |
| 779 | |
| 780 | if (symbol_conf.kptr_restrict) { |
| 781 | char *r = realpath(filename, NULL); |
| 782 | |
| 783 | if (r != NULL) { |
| 784 | restricted = strcmp(r, restricted_filename) == 0; |
| 785 | free(r); |
| 786 | return restricted; |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | return restricted; |
| 791 | } |
| 792 | |
Adrian Hunter | 52afdaf | 2013-10-09 15:01:11 +0300 | [diff] [blame] | 793 | struct module_info { |
| 794 | struct rb_node rb_node; |
| 795 | char *name; |
| 796 | u64 start; |
| 797 | }; |
| 798 | |
| 799 | static void add_module(struct module_info *mi, struct rb_root *modules) |
| 800 | { |
| 801 | struct rb_node **p = &modules->rb_node; |
| 802 | struct rb_node *parent = NULL; |
| 803 | struct module_info *m; |
| 804 | |
| 805 | while (*p != NULL) { |
| 806 | parent = *p; |
| 807 | m = rb_entry(parent, struct module_info, rb_node); |
| 808 | if (strcmp(mi->name, m->name) < 0) |
| 809 | p = &(*p)->rb_left; |
| 810 | else |
| 811 | p = &(*p)->rb_right; |
| 812 | } |
| 813 | rb_link_node(&mi->rb_node, parent, p); |
| 814 | rb_insert_color(&mi->rb_node, modules); |
| 815 | } |
| 816 | |
| 817 | static void delete_modules(struct rb_root *modules) |
| 818 | { |
| 819 | struct module_info *mi; |
| 820 | struct rb_node *next = rb_first(modules); |
| 821 | |
| 822 | while (next) { |
| 823 | mi = rb_entry(next, struct module_info, rb_node); |
| 824 | next = rb_next(&mi->rb_node); |
| 825 | rb_erase(&mi->rb_node, modules); |
Arnaldo Carvalho de Melo | 74cf249 | 2013-12-27 16:55:14 -0300 | [diff] [blame] | 826 | zfree(&mi->name); |
Adrian Hunter | 52afdaf | 2013-10-09 15:01:11 +0300 | [diff] [blame] | 827 | free(mi); |
| 828 | } |
| 829 | } |
| 830 | |
| 831 | static struct module_info *find_module(const char *name, |
| 832 | struct rb_root *modules) |
| 833 | { |
| 834 | struct rb_node *n = modules->rb_node; |
| 835 | |
| 836 | while (n) { |
| 837 | struct module_info *m; |
| 838 | int cmp; |
| 839 | |
| 840 | m = rb_entry(n, struct module_info, rb_node); |
| 841 | cmp = strcmp(name, m->name); |
| 842 | if (cmp < 0) |
| 843 | n = n->rb_left; |
| 844 | else if (cmp > 0) |
| 845 | n = n->rb_right; |
| 846 | else |
| 847 | return m; |
| 848 | } |
| 849 | |
| 850 | return NULL; |
| 851 | } |
| 852 | |
| 853 | static int __read_proc_modules(void *arg, const char *name, u64 start) |
| 854 | { |
| 855 | struct rb_root *modules = arg; |
| 856 | struct module_info *mi; |
| 857 | |
| 858 | mi = zalloc(sizeof(struct module_info)); |
| 859 | if (!mi) |
| 860 | return -ENOMEM; |
| 861 | |
| 862 | mi->name = strdup(name); |
| 863 | mi->start = start; |
| 864 | |
| 865 | if (!mi->name) { |
| 866 | free(mi); |
| 867 | return -ENOMEM; |
| 868 | } |
| 869 | |
| 870 | add_module(mi, modules); |
| 871 | |
| 872 | return 0; |
| 873 | } |
| 874 | |
| 875 | static int read_proc_modules(const char *filename, struct rb_root *modules) |
| 876 | { |
| 877 | if (symbol__restricted_filename(filename, "/proc/modules")) |
| 878 | return -1; |
| 879 | |
| 880 | if (modules__parse(filename, modules, __read_proc_modules)) { |
| 881 | delete_modules(modules); |
| 882 | return -1; |
| 883 | } |
| 884 | |
| 885 | return 0; |
| 886 | } |
| 887 | |
Adrian Hunter | fc1b691 | 2013-10-14 16:57:29 +0300 | [diff] [blame] | 888 | int compare_proc_modules(const char *from, const char *to) |
| 889 | { |
| 890 | struct rb_root from_modules = RB_ROOT; |
| 891 | struct rb_root to_modules = RB_ROOT; |
| 892 | struct rb_node *from_node, *to_node; |
| 893 | struct module_info *from_m, *to_m; |
| 894 | int ret = -1; |
| 895 | |
| 896 | if (read_proc_modules(from, &from_modules)) |
| 897 | return -1; |
| 898 | |
| 899 | if (read_proc_modules(to, &to_modules)) |
| 900 | goto out_delete_from; |
| 901 | |
| 902 | from_node = rb_first(&from_modules); |
| 903 | to_node = rb_first(&to_modules); |
| 904 | while (from_node) { |
| 905 | if (!to_node) |
| 906 | break; |
| 907 | |
| 908 | from_m = rb_entry(from_node, struct module_info, rb_node); |
| 909 | to_m = rb_entry(to_node, struct module_info, rb_node); |
| 910 | |
| 911 | if (from_m->start != to_m->start || |
| 912 | strcmp(from_m->name, to_m->name)) |
| 913 | break; |
| 914 | |
| 915 | from_node = rb_next(from_node); |
| 916 | to_node = rb_next(to_node); |
| 917 | } |
| 918 | |
| 919 | if (!from_node && !to_node) |
| 920 | ret = 0; |
| 921 | |
| 922 | delete_modules(&to_modules); |
| 923 | out_delete_from: |
| 924 | delete_modules(&from_modules); |
| 925 | |
| 926 | return ret; |
| 927 | } |
| 928 | |
Adrian Hunter | 52afdaf | 2013-10-09 15:01:11 +0300 | [diff] [blame] | 929 | static int do_validate_kcore_modules(const char *filename, struct map *map, |
| 930 | struct map_groups *kmaps) |
| 931 | { |
| 932 | struct rb_root modules = RB_ROOT; |
| 933 | struct map *old_map; |
| 934 | int err; |
| 935 | |
| 936 | err = read_proc_modules(filename, &modules); |
| 937 | if (err) |
| 938 | return err; |
| 939 | |
| 940 | old_map = map_groups__first(kmaps, map->type); |
| 941 | while (old_map) { |
| 942 | struct map *next = map_groups__next(old_map); |
| 943 | struct module_info *mi; |
| 944 | |
| 945 | if (old_map == map || old_map->start == map->start) { |
| 946 | /* The kernel map */ |
| 947 | old_map = next; |
| 948 | continue; |
| 949 | } |
| 950 | |
| 951 | /* Module must be in memory at the same address */ |
| 952 | mi = find_module(old_map->dso->short_name, &modules); |
| 953 | if (!mi || mi->start != old_map->start) { |
| 954 | err = -EINVAL; |
| 955 | goto out; |
| 956 | } |
| 957 | |
| 958 | old_map = next; |
| 959 | } |
| 960 | out: |
| 961 | delete_modules(&modules); |
| 962 | return err; |
| 963 | } |
| 964 | |
| 965 | /* |
| 966 | * If kallsyms is referenced by name then we look for filename in the same |
| 967 | * directory. |
| 968 | */ |
| 969 | static bool filename_from_kallsyms_filename(char *filename, |
| 970 | const char *base_name, |
| 971 | const char *kallsyms_filename) |
| 972 | { |
| 973 | char *name; |
| 974 | |
| 975 | strcpy(filename, kallsyms_filename); |
| 976 | name = strrchr(filename, '/'); |
| 977 | if (!name) |
| 978 | return false; |
| 979 | |
| 980 | name += 1; |
| 981 | |
| 982 | if (!strcmp(name, "kallsyms")) { |
| 983 | strcpy(name, base_name); |
| 984 | return true; |
| 985 | } |
| 986 | |
| 987 | return false; |
| 988 | } |
| 989 | |
| 990 | static int validate_kcore_modules(const char *kallsyms_filename, |
| 991 | struct map *map) |
| 992 | { |
| 993 | struct map_groups *kmaps = map__kmap(map)->kmaps; |
| 994 | char modules_filename[PATH_MAX]; |
| 995 | |
| 996 | if (!filename_from_kallsyms_filename(modules_filename, "modules", |
| 997 | kallsyms_filename)) |
| 998 | return -EINVAL; |
| 999 | |
| 1000 | if (do_validate_kcore_modules(modules_filename, map, kmaps)) |
| 1001 | return -EINVAL; |
| 1002 | |
| 1003 | return 0; |
| 1004 | } |
| 1005 | |
Adrian Hunter | a00d28c | 2014-01-29 16:14:41 +0200 | [diff] [blame] | 1006 | static int validate_kcore_addresses(const char *kallsyms_filename, |
| 1007 | struct map *map) |
| 1008 | { |
| 1009 | struct kmap *kmap = map__kmap(map); |
| 1010 | |
| 1011 | if (kmap->ref_reloc_sym && kmap->ref_reloc_sym->name) { |
| 1012 | u64 start; |
| 1013 | |
| 1014 | start = kallsyms__get_function_start(kallsyms_filename, |
| 1015 | kmap->ref_reloc_sym->name); |
| 1016 | if (start != kmap->ref_reloc_sym->addr) |
| 1017 | return -EINVAL; |
| 1018 | } |
| 1019 | |
| 1020 | return validate_kcore_modules(kallsyms_filename, map); |
| 1021 | } |
| 1022 | |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 1023 | struct kcore_mapfn_data { |
| 1024 | struct dso *dso; |
| 1025 | enum map_type type; |
| 1026 | struct list_head maps; |
| 1027 | }; |
| 1028 | |
| 1029 | static int kcore_mapfn(u64 start, u64 len, u64 pgoff, void *data) |
| 1030 | { |
| 1031 | struct kcore_mapfn_data *md = data; |
| 1032 | struct map *map; |
| 1033 | |
| 1034 | map = map__new2(start, md->dso, md->type); |
| 1035 | if (map == NULL) |
| 1036 | return -ENOMEM; |
| 1037 | |
| 1038 | map->end = map->start + len; |
| 1039 | map->pgoff = pgoff; |
| 1040 | |
| 1041 | list_add(&map->node, &md->maps); |
| 1042 | |
| 1043 | return 0; |
| 1044 | } |
| 1045 | |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 1046 | static int dso__load_kcore(struct dso *dso, struct map *map, |
| 1047 | const char *kallsyms_filename) |
| 1048 | { |
| 1049 | struct map_groups *kmaps = map__kmap(map)->kmaps; |
| 1050 | struct machine *machine = kmaps->machine; |
| 1051 | struct kcore_mapfn_data md; |
| 1052 | struct map *old_map, *new_map, *replacement_map = NULL; |
| 1053 | bool is_64_bit; |
| 1054 | int err, fd; |
| 1055 | char kcore_filename[PATH_MAX]; |
| 1056 | struct symbol *sym; |
| 1057 | |
| 1058 | /* This function requires that the map is the kernel map */ |
| 1059 | if (map != machine->vmlinux_maps[map->type]) |
| 1060 | return -EINVAL; |
| 1061 | |
Adrian Hunter | 52afdaf | 2013-10-09 15:01:11 +0300 | [diff] [blame] | 1062 | if (!filename_from_kallsyms_filename(kcore_filename, "kcore", |
| 1063 | kallsyms_filename)) |
| 1064 | return -EINVAL; |
| 1065 | |
Adrian Hunter | a00d28c | 2014-01-29 16:14:41 +0200 | [diff] [blame] | 1066 | /* Modules and kernel must be present at their original addresses */ |
| 1067 | if (validate_kcore_addresses(kallsyms_filename, map)) |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 1068 | return -EINVAL; |
| 1069 | |
| 1070 | md.dso = dso; |
| 1071 | md.type = map->type; |
| 1072 | INIT_LIST_HEAD(&md.maps); |
| 1073 | |
| 1074 | fd = open(kcore_filename, O_RDONLY); |
| 1075 | if (fd < 0) |
| 1076 | return -EINVAL; |
| 1077 | |
| 1078 | /* Read new maps into temporary lists */ |
| 1079 | err = file__read_maps(fd, md.type == MAP__FUNCTION, kcore_mapfn, &md, |
| 1080 | &is_64_bit); |
| 1081 | if (err) |
| 1082 | goto out_err; |
Adrian Hunter | c6d8f2a | 2014-07-14 13:02:41 +0300 | [diff] [blame] | 1083 | dso->is_64_bit = is_64_bit; |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 1084 | |
| 1085 | if (list_empty(&md.maps)) { |
| 1086 | err = -EINVAL; |
| 1087 | goto out_err; |
| 1088 | } |
| 1089 | |
| 1090 | /* Remove old maps */ |
| 1091 | old_map = map_groups__first(kmaps, map->type); |
| 1092 | while (old_map) { |
| 1093 | struct map *next = map_groups__next(old_map); |
| 1094 | |
| 1095 | if (old_map != map) |
| 1096 | map_groups__remove(kmaps, old_map); |
| 1097 | old_map = next; |
| 1098 | } |
| 1099 | |
| 1100 | /* Find the kernel map using the first symbol */ |
| 1101 | sym = dso__first_symbol(dso, map->type); |
| 1102 | list_for_each_entry(new_map, &md.maps, node) { |
| 1103 | if (sym && sym->start >= new_map->start && |
| 1104 | sym->start < new_map->end) { |
| 1105 | replacement_map = new_map; |
| 1106 | break; |
| 1107 | } |
| 1108 | } |
| 1109 | |
| 1110 | if (!replacement_map) |
| 1111 | replacement_map = list_entry(md.maps.next, struct map, node); |
| 1112 | |
| 1113 | /* Add new maps */ |
| 1114 | while (!list_empty(&md.maps)) { |
| 1115 | new_map = list_entry(md.maps.next, struct map, node); |
| 1116 | list_del(&new_map->node); |
| 1117 | if (new_map == replacement_map) { |
| 1118 | map->start = new_map->start; |
| 1119 | map->end = new_map->end; |
| 1120 | map->pgoff = new_map->pgoff; |
| 1121 | map->map_ip = new_map->map_ip; |
| 1122 | map->unmap_ip = new_map->unmap_ip; |
| 1123 | map__delete(new_map); |
| 1124 | /* Ensure maps are correctly ordered */ |
| 1125 | map_groups__remove(kmaps, map); |
| 1126 | map_groups__insert(kmaps, map); |
| 1127 | } else { |
| 1128 | map_groups__insert(kmaps, new_map); |
| 1129 | } |
| 1130 | } |
| 1131 | |
| 1132 | /* |
| 1133 | * Set the data type and long name so that kcore can be read via |
| 1134 | * dso__data_read_addr(). |
| 1135 | */ |
| 1136 | if (dso->kernel == DSO_TYPE_GUEST_KERNEL) |
Arnaldo Carvalho de Melo | 5f70619 | 2013-12-17 16:14:07 -0300 | [diff] [blame] | 1137 | dso->binary_type = DSO_BINARY_TYPE__GUEST_KCORE; |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 1138 | else |
Arnaldo Carvalho de Melo | 5f70619 | 2013-12-17 16:14:07 -0300 | [diff] [blame] | 1139 | dso->binary_type = DSO_BINARY_TYPE__KCORE; |
Arnaldo Carvalho de Melo | 7e155d4 | 2013-12-10 15:08:44 -0300 | [diff] [blame] | 1140 | dso__set_long_name(dso, strdup(kcore_filename), true); |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 1141 | |
| 1142 | close(fd); |
| 1143 | |
| 1144 | if (map->type == MAP__FUNCTION) |
| 1145 | pr_debug("Using %s for kernel object code\n", kcore_filename); |
| 1146 | else |
| 1147 | pr_debug("Using %s for kernel data\n", kcore_filename); |
| 1148 | |
| 1149 | return 0; |
| 1150 | |
| 1151 | out_err: |
| 1152 | while (!list_empty(&md.maps)) { |
| 1153 | map = list_entry(md.maps.next, struct map, node); |
| 1154 | list_del(&map->node); |
| 1155 | map__delete(map); |
| 1156 | } |
| 1157 | close(fd); |
| 1158 | return -EINVAL; |
| 1159 | } |
| 1160 | |
Adrian Hunter | d9b62ab | 2014-01-29 16:14:43 +0200 | [diff] [blame] | 1161 | /* |
| 1162 | * If the kernel is relocated at boot time, kallsyms won't match. Compute the |
| 1163 | * delta based on the relocation reference symbol. |
| 1164 | */ |
| 1165 | static int kallsyms__delta(struct map *map, const char *filename, u64 *delta) |
| 1166 | { |
| 1167 | struct kmap *kmap = map__kmap(map); |
| 1168 | u64 addr; |
| 1169 | |
| 1170 | if (!kmap->ref_reloc_sym || !kmap->ref_reloc_sym->name) |
| 1171 | return 0; |
| 1172 | |
| 1173 | addr = kallsyms__get_function_start(filename, |
| 1174 | kmap->ref_reloc_sym->name); |
| 1175 | if (!addr) |
| 1176 | return -1; |
| 1177 | |
| 1178 | *delta = addr - kmap->ref_reloc_sym->addr; |
| 1179 | return 0; |
| 1180 | } |
| 1181 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 1182 | int dso__load_kallsyms(struct dso *dso, const char *filename, |
Arnaldo Carvalho de Melo | 9de89fe | 2010-02-03 16:52:00 -0200 | [diff] [blame] | 1183 | struct map *map, symbol_filter_t filter) |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 1184 | { |
Adrian Hunter | d9b62ab | 2014-01-29 16:14:43 +0200 | [diff] [blame] | 1185 | u64 delta = 0; |
| 1186 | |
Arnaldo Carvalho de Melo | ec80fde | 2011-05-26 09:53:51 -0300 | [diff] [blame] | 1187 | if (symbol__restricted_filename(filename, "/proc/kallsyms")) |
| 1188 | return -1; |
| 1189 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 1190 | if (dso__load_all_kallsyms(dso, filename, map) < 0) |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 1191 | return -1; |
| 1192 | |
Adrian Hunter | d9b62ab | 2014-01-29 16:14:43 +0200 | [diff] [blame] | 1193 | if (kallsyms__delta(map, filename, &delta)) |
| 1194 | return -1; |
| 1195 | |
Anton Blanchard | 694bf40 | 2011-08-24 16:40:17 +1000 | [diff] [blame] | 1196 | symbols__fixup_duplicate(&dso->symbols[map->type]); |
Anton Blanchard | 3f5a427 | 2011-08-24 16:40:15 +1000 | [diff] [blame] | 1197 | symbols__fixup_end(&dso->symbols[map->type]); |
| 1198 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 1199 | if (dso->kernel == DSO_TYPE_GUEST_KERNEL) |
Jiri Olsa | 44f24cb | 2012-07-22 14:14:32 +0200 | [diff] [blame] | 1200 | dso->symtab_type = DSO_BINARY_TYPE__GUEST_KALLSYMS; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1201 | else |
Jiri Olsa | 44f24cb | 2012-07-22 14:14:32 +0200 | [diff] [blame] | 1202 | dso->symtab_type = DSO_BINARY_TYPE__KALLSYMS; |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 1203 | |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 1204 | if (!dso__load_kcore(dso, map, filename)) |
| 1205 | return dso__split_kallsyms_for_kcore(dso, map, filter); |
| 1206 | else |
Adrian Hunter | d9b62ab | 2014-01-29 16:14:43 +0200 | [diff] [blame] | 1207 | return dso__split_kallsyms(dso, map, delta, filter); |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 1208 | } |
| 1209 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 1210 | 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] | 1211 | symbol_filter_t filter) |
Pekka Enberg | 80d496b | 2009-06-08 21:12:48 +0300 | [diff] [blame] | 1212 | { |
| 1213 | char *line = NULL; |
| 1214 | size_t n; |
| 1215 | FILE *file; |
| 1216 | int nr_syms = 0; |
| 1217 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 1218 | file = fopen(dso->long_name, "r"); |
Pekka Enberg | 80d496b | 2009-06-08 21:12:48 +0300 | [diff] [blame] | 1219 | if (file == NULL) |
| 1220 | goto out_failure; |
| 1221 | |
| 1222 | while (!feof(file)) { |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 1223 | u64 start, size; |
Pekka Enberg | 80d496b | 2009-06-08 21:12:48 +0300 | [diff] [blame] | 1224 | struct symbol *sym; |
| 1225 | int line_len, len; |
| 1226 | |
| 1227 | line_len = getline(&line, &n, file); |
| 1228 | if (line_len < 0) |
| 1229 | break; |
| 1230 | |
| 1231 | if (!line) |
| 1232 | goto out_failure; |
| 1233 | |
| 1234 | line[--line_len] = '\0'; /* \n */ |
| 1235 | |
| 1236 | len = hex2u64(line, &start); |
| 1237 | |
| 1238 | len++; |
| 1239 | if (len + 2 >= line_len) |
| 1240 | continue; |
| 1241 | |
| 1242 | len += hex2u64(line + len, &size); |
| 1243 | |
| 1244 | len++; |
| 1245 | if (len + 2 >= line_len) |
| 1246 | continue; |
| 1247 | |
Arnaldo Carvalho de Melo | c408fed | 2010-08-05 12:59:47 -0300 | [diff] [blame] | 1248 | sym = symbol__new(start, size, STB_GLOBAL, line + len); |
Pekka Enberg | 80d496b | 2009-06-08 21:12:48 +0300 | [diff] [blame] | 1249 | |
| 1250 | if (sym == NULL) |
| 1251 | goto out_delete_line; |
| 1252 | |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1253 | if (filter && filter(map, sym)) |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 1254 | symbol__delete(sym); |
Pekka Enberg | 80d496b | 2009-06-08 21:12:48 +0300 | [diff] [blame] | 1255 | else { |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 1256 | symbols__insert(&dso->symbols[map->type], sym); |
Pekka Enberg | 80d496b | 2009-06-08 21:12:48 +0300 | [diff] [blame] | 1257 | nr_syms++; |
| 1258 | } |
| 1259 | } |
| 1260 | |
| 1261 | free(line); |
| 1262 | fclose(file); |
| 1263 | |
| 1264 | return nr_syms; |
| 1265 | |
| 1266 | out_delete_line: |
| 1267 | free(line); |
| 1268 | out_failure: |
| 1269 | return -1; |
| 1270 | } |
| 1271 | |
Namhyung Kim | 1029f9f | 2014-02-20 10:32:56 +0900 | [diff] [blame] | 1272 | static bool dso__is_compatible_symtab_type(struct dso *dso, bool kmod, |
| 1273 | enum dso_binary_type type) |
| 1274 | { |
| 1275 | switch (type) { |
| 1276 | case DSO_BINARY_TYPE__JAVA_JIT: |
| 1277 | case DSO_BINARY_TYPE__DEBUGLINK: |
| 1278 | case DSO_BINARY_TYPE__SYSTEM_PATH_DSO: |
| 1279 | case DSO_BINARY_TYPE__FEDORA_DEBUGINFO: |
| 1280 | case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO: |
| 1281 | case DSO_BINARY_TYPE__BUILDID_DEBUGINFO: |
| 1282 | case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO: |
| 1283 | return !kmod && dso->kernel == DSO_TYPE_USER; |
| 1284 | |
| 1285 | case DSO_BINARY_TYPE__KALLSYMS: |
| 1286 | case DSO_BINARY_TYPE__VMLINUX: |
| 1287 | case DSO_BINARY_TYPE__KCORE: |
| 1288 | return dso->kernel == DSO_TYPE_KERNEL; |
| 1289 | |
| 1290 | case DSO_BINARY_TYPE__GUEST_KALLSYMS: |
| 1291 | case DSO_BINARY_TYPE__GUEST_VMLINUX: |
| 1292 | case DSO_BINARY_TYPE__GUEST_KCORE: |
| 1293 | return dso->kernel == DSO_TYPE_GUEST_KERNEL; |
| 1294 | |
| 1295 | case DSO_BINARY_TYPE__GUEST_KMODULE: |
| 1296 | case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE: |
| 1297 | /* |
| 1298 | * kernel modules know their symtab type - it's set when |
| 1299 | * creating a module dso in machine__new_module(). |
| 1300 | */ |
| 1301 | return kmod && dso->symtab_type == type; |
| 1302 | |
| 1303 | case DSO_BINARY_TYPE__BUILD_ID_CACHE: |
| 1304 | return true; |
| 1305 | |
| 1306 | case DSO_BINARY_TYPE__NOT_FOUND: |
| 1307 | default: |
| 1308 | return false; |
| 1309 | } |
| 1310 | } |
| 1311 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 1312 | 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] | 1313 | { |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 1314 | char *name; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1315 | int ret = -1; |
Jiri Olsa | 44f24cb | 2012-07-22 14:14:32 +0200 | [diff] [blame] | 1316 | u_int i; |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 1317 | struct machine *machine; |
Jiri Olsa | 44f24cb | 2012-07-22 14:14:32 +0200 | [diff] [blame] | 1318 | char *root_dir = (char *) ""; |
Cody P Schafer | 3aafe5a | 2012-08-10 15:23:02 -0700 | [diff] [blame] | 1319 | int ss_pos = 0; |
| 1320 | struct symsrc ss_[2]; |
| 1321 | struct symsrc *syms_ss = NULL, *runtime_ss = NULL; |
Namhyung Kim | 1029f9f | 2014-02-20 10:32:56 +0900 | [diff] [blame] | 1322 | bool kmod; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1323 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 1324 | dso__set_loaded(dso, map->type); |
Arnaldo Carvalho de Melo | 66bd842 | 2009-10-28 21:51:21 -0200 | [diff] [blame] | 1325 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 1326 | if (dso->kernel == DSO_TYPE_KERNEL) |
| 1327 | return dso__load_kernel_sym(dso, map, filter); |
| 1328 | else if (dso->kernel == DSO_TYPE_GUEST_KERNEL) |
| 1329 | return dso__load_guest_kernel_sym(dso, map, filter); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1330 | |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 1331 | if (map->groups && map->groups->machine) |
| 1332 | machine = map->groups->machine; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1333 | else |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 1334 | machine = NULL; |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 1335 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 1336 | dso->adjust_symbols = 0; |
Arnaldo Carvalho de Melo | f5812a7 | 2009-06-30 11:43:17 -0300 | [diff] [blame] | 1337 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 1338 | if (strncmp(dso->name, "/tmp/perf-", 10) == 0) { |
Pekka Enberg | 981c125 | 2011-08-09 22:54:18 +0300 | [diff] [blame] | 1339 | struct stat st; |
| 1340 | |
Vasiliy Kulikov | e9b52ef | 2011-08-12 00:55:37 +0400 | [diff] [blame] | 1341 | if (lstat(dso->name, &st) < 0) |
Pekka Enberg | 981c125 | 2011-08-09 22:54:18 +0300 | [diff] [blame] | 1342 | return -1; |
| 1343 | |
| 1344 | if (st.st_uid && (st.st_uid != geteuid())) { |
| 1345 | pr_warning("File %s not owned by current user or root, " |
| 1346 | "ignoring it.\n", dso->name); |
| 1347 | return -1; |
| 1348 | } |
| 1349 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 1350 | ret = dso__load_perf_map(dso, map, filter); |
Jiri Olsa | 44f24cb | 2012-07-22 14:14:32 +0200 | [diff] [blame] | 1351 | dso->symtab_type = ret > 0 ? DSO_BINARY_TYPE__JAVA_JIT : |
| 1352 | DSO_BINARY_TYPE__NOT_FOUND; |
Arnaldo Carvalho de Melo | 94cb9e3 | 2009-08-06 14:43:17 -0300 | [diff] [blame] | 1353 | return ret; |
| 1354 | } |
| 1355 | |
Jiri Olsa | 44f24cb | 2012-07-22 14:14:32 +0200 | [diff] [blame] | 1356 | if (machine) |
| 1357 | root_dir = machine->root_dir; |
| 1358 | |
David Ahern | 164c800 | 2013-01-14 10:46:47 -0700 | [diff] [blame] | 1359 | name = malloc(PATH_MAX); |
| 1360 | if (!name) |
| 1361 | return -1; |
| 1362 | |
Namhyung Kim | 1029f9f | 2014-02-20 10:32:56 +0900 | [diff] [blame] | 1363 | kmod = dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE || |
| 1364 | dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE; |
| 1365 | |
| 1366 | /* |
| 1367 | * Iterate over candidate debug images. |
Cody P Schafer | 3aafe5a | 2012-08-10 15:23:02 -0700 | [diff] [blame] | 1368 | * Keep track of "interesting" ones (those which have a symtab, dynsym, |
| 1369 | * and/or opd section) for processing. |
Dave Martin | 6da80ce | 2010-07-30 09:50:09 -0300 | [diff] [blame] | 1370 | */ |
Jiri Olsa | 44f24cb | 2012-07-22 14:14:32 +0200 | [diff] [blame] | 1371 | for (i = 0; i < DSO_BINARY_TYPE__SYMTAB_CNT; i++) { |
Cody P Schafer | 3aafe5a | 2012-08-10 15:23:02 -0700 | [diff] [blame] | 1372 | struct symsrc *ss = &ss_[ss_pos]; |
| 1373 | bool next_slot = false; |
Dave Martin | 6da80ce | 2010-07-30 09:50:09 -0300 | [diff] [blame] | 1374 | |
Cody P Schafer | 005f929 | 2012-08-10 15:22:58 -0700 | [diff] [blame] | 1375 | enum dso_binary_type symtab_type = binary_type_symtab[i]; |
Dave Martin | 6da80ce | 2010-07-30 09:50:09 -0300 | [diff] [blame] | 1376 | |
Namhyung Kim | 1029f9f | 2014-02-20 10:32:56 +0900 | [diff] [blame] | 1377 | if (!dso__is_compatible_symtab_type(dso, kmod, symtab_type)) |
| 1378 | continue; |
| 1379 | |
Arnaldo Carvalho de Melo | ee4e962 | 2013-12-16 17:03:18 -0300 | [diff] [blame] | 1380 | if (dso__read_binary_type_filename(dso, symtab_type, |
| 1381 | root_dir, name, PATH_MAX)) |
Jiri Olsa | 44f24cb | 2012-07-22 14:14:32 +0200 | [diff] [blame] | 1382 | continue; |
Dave Martin | 6da80ce | 2010-07-30 09:50:09 -0300 | [diff] [blame] | 1383 | |
| 1384 | /* Name is now the name of the next image to try */ |
Cody P Schafer | 3aafe5a | 2012-08-10 15:23:02 -0700 | [diff] [blame] | 1385 | if (symsrc__init(ss, dso, name, symtab_type) < 0) |
Dave Martin | 6da80ce | 2010-07-30 09:50:09 -0300 | [diff] [blame] | 1386 | continue; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1387 | |
Cody P Schafer | 3aafe5a | 2012-08-10 15:23:02 -0700 | [diff] [blame] | 1388 | if (!syms_ss && symsrc__has_symtab(ss)) { |
| 1389 | syms_ss = ss; |
| 1390 | next_slot = true; |
Adrian Hunter | 0058aef | 2013-12-03 09:23:08 +0200 | [diff] [blame] | 1391 | if (!dso->symsrc_filename) |
| 1392 | dso->symsrc_filename = strdup(name); |
Cody P Schafer | d26cd12 | 2012-08-10 15:23:00 -0700 | [diff] [blame] | 1393 | } |
| 1394 | |
Cody P Schafer | 3aafe5a | 2012-08-10 15:23:02 -0700 | [diff] [blame] | 1395 | if (!runtime_ss && symsrc__possibly_runtime(ss)) { |
| 1396 | runtime_ss = ss; |
| 1397 | next_slot = true; |
Cody P Schafer | a44f605 | 2012-08-10 15:22:59 -0700 | [diff] [blame] | 1398 | } |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1399 | |
Cody P Schafer | 3aafe5a | 2012-08-10 15:23:02 -0700 | [diff] [blame] | 1400 | if (next_slot) { |
| 1401 | ss_pos++; |
Jiri Olsa | 33ff581 | 2012-04-18 15:46:58 +0200 | [diff] [blame] | 1402 | |
Cody P Schafer | 3aafe5a | 2012-08-10 15:23:02 -0700 | [diff] [blame] | 1403 | if (syms_ss && runtime_ss) |
| 1404 | break; |
Namhyung Kim | 98e9f03 | 2014-02-20 10:32:54 +0900 | [diff] [blame] | 1405 | } else { |
| 1406 | symsrc__destroy(ss); |
Dave Martin | 6da80ce | 2010-07-30 09:50:09 -0300 | [diff] [blame] | 1407 | } |
Cody P Schafer | 3aafe5a | 2012-08-10 15:23:02 -0700 | [diff] [blame] | 1408 | |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 1409 | } |
Dave Martin | 6da80ce | 2010-07-30 09:50:09 -0300 | [diff] [blame] | 1410 | |
Cody P Schafer | 3aafe5a | 2012-08-10 15:23:02 -0700 | [diff] [blame] | 1411 | if (!runtime_ss && !syms_ss) |
| 1412 | goto out_free; |
| 1413 | |
| 1414 | if (runtime_ss && !syms_ss) { |
| 1415 | syms_ss = runtime_ss; |
Arnaldo Carvalho de Melo | 60e4b10 | 2011-03-22 15:42:14 -0300 | [diff] [blame] | 1416 | } |
| 1417 | |
Cody P Schafer | 3aafe5a | 2012-08-10 15:23:02 -0700 | [diff] [blame] | 1418 | /* We'll have to hope for the best */ |
| 1419 | if (!runtime_ss && syms_ss) |
| 1420 | runtime_ss = syms_ss; |
| 1421 | |
Namhyung Kim | 1029f9f | 2014-02-20 10:32:56 +0900 | [diff] [blame] | 1422 | if (syms_ss) |
| 1423 | ret = dso__load_sym(dso, map, syms_ss, runtime_ss, filter, kmod); |
| 1424 | else |
Cody P Schafer | 3aafe5a | 2012-08-10 15:23:02 -0700 | [diff] [blame] | 1425 | ret = -1; |
| 1426 | |
David Ahern | f47b58b | 2012-08-19 09:47:14 -0600 | [diff] [blame] | 1427 | if (ret > 0) { |
Cody P Schafer | 3aafe5a | 2012-08-10 15:23:02 -0700 | [diff] [blame] | 1428 | int nr_plt; |
| 1429 | |
| 1430 | nr_plt = dso__synthesize_plt_symbols(dso, runtime_ss, map, filter); |
| 1431 | if (nr_plt > 0) |
| 1432 | ret += nr_plt; |
| 1433 | } |
| 1434 | |
| 1435 | for (; ss_pos > 0; ss_pos--) |
| 1436 | symsrc__destroy(&ss_[ss_pos - 1]); |
| 1437 | out_free: |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1438 | free(name); |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 1439 | if (ret < 0 && strstr(dso->name, " (deleted)") != NULL) |
Arnaldo Carvalho de Melo | 1340e6b | 2009-08-11 17:04:36 -0300 | [diff] [blame] | 1440 | return 0; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1441 | return ret; |
| 1442 | } |
| 1443 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 1444 | 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] | 1445 | enum map_type type, const char *name) |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1446 | { |
| 1447 | struct rb_node *nd; |
| 1448 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 1449 | 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] | 1450 | struct map *map = rb_entry(nd, struct map, rb_node); |
| 1451 | |
Arnaldo Carvalho de Melo | b7cece76 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 1452 | if (map->dso && strcmp(map->dso->short_name, name) == 0) |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1453 | return map; |
| 1454 | } |
| 1455 | |
| 1456 | return NULL; |
| 1457 | } |
| 1458 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 1459 | int dso__load_vmlinux(struct dso *dso, struct map *map, |
Arnaldo Carvalho de Melo | 5230fb7 | 2013-12-10 11:58:52 -0300 | [diff] [blame] | 1460 | const char *vmlinux, bool vmlinux_allocated, |
| 1461 | symbol_filter_t filter) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1462 | { |
Cody P Schafer | b68e2f9 | 2012-08-10 15:22:57 -0700 | [diff] [blame] | 1463 | int err = -1; |
| 1464 | struct symsrc ss; |
David Ahern | ec5761e | 2010-12-09 13:27:07 -0700 | [diff] [blame] | 1465 | char symfs_vmlinux[PATH_MAX]; |
Cody P Schafer | 005f929 | 2012-08-10 15:22:58 -0700 | [diff] [blame] | 1466 | enum dso_binary_type symtab_type; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1467 | |
Namhyung Kim | 5698d2c | 2013-07-17 17:08:15 +0900 | [diff] [blame] | 1468 | if (vmlinux[0] == '/') |
| 1469 | snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s", vmlinux); |
| 1470 | else |
Arnaldo Carvalho de Melo | 972f393 | 2014-07-29 10:21:58 -0300 | [diff] [blame^] | 1471 | symbol__join_symfs(symfs_vmlinux, vmlinux); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1472 | |
Cody P Schafer | 21ea453 | 2012-08-10 15:22:56 -0700 | [diff] [blame] | 1473 | if (dso->kernel == DSO_TYPE_GUEST_KERNEL) |
Cody P Schafer | 005f929 | 2012-08-10 15:22:58 -0700 | [diff] [blame] | 1474 | symtab_type = DSO_BINARY_TYPE__GUEST_VMLINUX; |
Cody P Schafer | 21ea453 | 2012-08-10 15:22:56 -0700 | [diff] [blame] | 1475 | else |
Cody P Schafer | 005f929 | 2012-08-10 15:22:58 -0700 | [diff] [blame] | 1476 | symtab_type = DSO_BINARY_TYPE__VMLINUX; |
Cody P Schafer | 21ea453 | 2012-08-10 15:22:56 -0700 | [diff] [blame] | 1477 | |
Cody P Schafer | 005f929 | 2012-08-10 15:22:58 -0700 | [diff] [blame] | 1478 | if (symsrc__init(&ss, dso, symfs_vmlinux, symtab_type)) |
Cody P Schafer | b68e2f9 | 2012-08-10 15:22:57 -0700 | [diff] [blame] | 1479 | return -1; |
| 1480 | |
Cody P Schafer | 261360b | 2012-08-10 15:23:01 -0700 | [diff] [blame] | 1481 | err = dso__load_sym(dso, map, &ss, &ss, filter, 0); |
Cody P Schafer | b68e2f9 | 2012-08-10 15:22:57 -0700 | [diff] [blame] | 1482 | symsrc__destroy(&ss); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1483 | |
Cody P Schafer | 515850e | 2012-08-10 15:22:54 -0700 | [diff] [blame] | 1484 | if (err > 0) { |
Adrian Hunter | 39b12f78 | 2013-08-07 14:38:47 +0300 | [diff] [blame] | 1485 | if (dso->kernel == DSO_TYPE_GUEST_KERNEL) |
Arnaldo Carvalho de Melo | 5f70619 | 2013-12-17 16:14:07 -0300 | [diff] [blame] | 1486 | dso->binary_type = DSO_BINARY_TYPE__GUEST_VMLINUX; |
Adrian Hunter | 39b12f78 | 2013-08-07 14:38:47 +0300 | [diff] [blame] | 1487 | else |
Arnaldo Carvalho de Melo | 5f70619 | 2013-12-17 16:14:07 -0300 | [diff] [blame] | 1488 | dso->binary_type = DSO_BINARY_TYPE__VMLINUX; |
Arnaldo Carvalho de Melo | bf4414a | 2013-12-10 15:19:23 -0300 | [diff] [blame] | 1489 | dso__set_long_name(dso, vmlinux, vmlinux_allocated); |
Cody P Schafer | 515850e | 2012-08-10 15:22:54 -0700 | [diff] [blame] | 1490 | dso__set_loaded(dso, map->type); |
David Ahern | ec5761e | 2010-12-09 13:27:07 -0700 | [diff] [blame] | 1491 | pr_debug("Using %s for symbols\n", symfs_vmlinux); |
Cody P Schafer | 515850e | 2012-08-10 15:22:54 -0700 | [diff] [blame] | 1492 | } |
Arnaldo Carvalho de Melo | 3846df2 | 2010-02-22 16:15:39 -0300 | [diff] [blame] | 1493 | |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1494 | return err; |
| 1495 | } |
| 1496 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 1497 | 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] | 1498 | symbol_filter_t filter) |
Arnaldo Carvalho de Melo | a19afe4 | 2010-01-27 21:05:50 -0200 | [diff] [blame] | 1499 | { |
| 1500 | int i, err = 0; |
Arnaldo Carvalho de Melo | 5ad90e4 | 2010-05-26 13:26:02 -0300 | [diff] [blame] | 1501 | char *filename; |
Arnaldo Carvalho de Melo | a19afe4 | 2010-01-27 21:05:50 -0200 | [diff] [blame] | 1502 | |
| 1503 | 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] | 1504 | vmlinux_path__nr_entries + 1); |
| 1505 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 1506 | filename = dso__build_id_filename(dso, NULL, 0); |
Arnaldo Carvalho de Melo | 5ad90e4 | 2010-05-26 13:26:02 -0300 | [diff] [blame] | 1507 | if (filename != NULL) { |
Arnaldo Carvalho de Melo | 5230fb7 | 2013-12-10 11:58:52 -0300 | [diff] [blame] | 1508 | err = dso__load_vmlinux(dso, map, filename, true, filter); |
| 1509 | if (err > 0) |
Arnaldo Carvalho de Melo | 5ad90e4 | 2010-05-26 13:26:02 -0300 | [diff] [blame] | 1510 | goto out; |
Arnaldo Carvalho de Melo | 5ad90e4 | 2010-05-26 13:26:02 -0300 | [diff] [blame] | 1511 | free(filename); |
| 1512 | } |
Arnaldo Carvalho de Melo | a19afe4 | 2010-01-27 21:05:50 -0200 | [diff] [blame] | 1513 | |
| 1514 | for (i = 0; i < vmlinux_path__nr_entries; ++i) { |
Arnaldo Carvalho de Melo | 5230fb7 | 2013-12-10 11:58:52 -0300 | [diff] [blame] | 1515 | err = dso__load_vmlinux(dso, map, vmlinux_path[i], false, filter); |
| 1516 | if (err > 0) |
Arnaldo Carvalho de Melo | a19afe4 | 2010-01-27 21:05:50 -0200 | [diff] [blame] | 1517 | break; |
Arnaldo Carvalho de Melo | a19afe4 | 2010-01-27 21:05:50 -0200 | [diff] [blame] | 1518 | } |
Arnaldo Carvalho de Melo | 5ad90e4 | 2010-05-26 13:26:02 -0300 | [diff] [blame] | 1519 | out: |
Arnaldo Carvalho de Melo | a19afe4 | 2010-01-27 21:05:50 -0200 | [diff] [blame] | 1520 | return err; |
| 1521 | } |
| 1522 | |
Adrian Hunter | 0544d42 | 2013-10-14 13:43:43 +0300 | [diff] [blame] | 1523 | static int find_matching_kcore(struct map *map, char *dir, size_t dir_sz) |
| 1524 | { |
| 1525 | char kallsyms_filename[PATH_MAX]; |
| 1526 | struct dirent *dent; |
| 1527 | int ret = -1; |
| 1528 | DIR *d; |
| 1529 | |
| 1530 | d = opendir(dir); |
| 1531 | if (!d) |
| 1532 | return -1; |
| 1533 | |
| 1534 | while (1) { |
| 1535 | dent = readdir(d); |
| 1536 | if (!dent) |
| 1537 | break; |
| 1538 | if (dent->d_type != DT_DIR) |
| 1539 | continue; |
| 1540 | scnprintf(kallsyms_filename, sizeof(kallsyms_filename), |
| 1541 | "%s/%s/kallsyms", dir, dent->d_name); |
Adrian Hunter | a00d28c | 2014-01-29 16:14:41 +0200 | [diff] [blame] | 1542 | if (!validate_kcore_addresses(kallsyms_filename, map)) { |
Adrian Hunter | 0544d42 | 2013-10-14 13:43:43 +0300 | [diff] [blame] | 1543 | strlcpy(dir, kallsyms_filename, dir_sz); |
| 1544 | ret = 0; |
| 1545 | break; |
| 1546 | } |
| 1547 | } |
| 1548 | |
| 1549 | closedir(d); |
| 1550 | |
| 1551 | return ret; |
| 1552 | } |
| 1553 | |
| 1554 | static char *dso__find_kallsyms(struct dso *dso, struct map *map) |
| 1555 | { |
| 1556 | u8 host_build_id[BUILD_ID_SIZE]; |
| 1557 | char sbuild_id[BUILD_ID_SIZE * 2 + 1]; |
| 1558 | bool is_host = false; |
| 1559 | char path[PATH_MAX]; |
| 1560 | |
| 1561 | if (!dso->has_build_id) { |
| 1562 | /* |
| 1563 | * Last resort, if we don't have a build-id and couldn't find |
| 1564 | * any vmlinux file, try the running kernel kallsyms table. |
| 1565 | */ |
| 1566 | goto proc_kallsyms; |
| 1567 | } |
| 1568 | |
| 1569 | if (sysfs__read_build_id("/sys/kernel/notes", host_build_id, |
| 1570 | sizeof(host_build_id)) == 0) |
| 1571 | is_host = dso__build_id_equal(dso, host_build_id); |
| 1572 | |
| 1573 | build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id); |
| 1574 | |
Adrian Hunter | 449867e | 2013-11-26 15:19:24 +0200 | [diff] [blame] | 1575 | scnprintf(path, sizeof(path), "%s/[kernel.kcore]/%s", buildid_dir, |
| 1576 | sbuild_id); |
| 1577 | |
Adrian Hunter | 0544d42 | 2013-10-14 13:43:43 +0300 | [diff] [blame] | 1578 | /* Use /proc/kallsyms if possible */ |
| 1579 | if (is_host) { |
| 1580 | DIR *d; |
| 1581 | int fd; |
| 1582 | |
| 1583 | /* If no cached kcore go with /proc/kallsyms */ |
Adrian Hunter | 0544d42 | 2013-10-14 13:43:43 +0300 | [diff] [blame] | 1584 | d = opendir(path); |
| 1585 | if (!d) |
| 1586 | goto proc_kallsyms; |
| 1587 | closedir(d); |
| 1588 | |
| 1589 | /* |
| 1590 | * Do not check the build-id cache, until we know we cannot use |
| 1591 | * /proc/kcore. |
| 1592 | */ |
| 1593 | fd = open("/proc/kcore", O_RDONLY); |
| 1594 | if (fd != -1) { |
| 1595 | close(fd); |
| 1596 | /* If module maps match go with /proc/kallsyms */ |
Adrian Hunter | a00d28c | 2014-01-29 16:14:41 +0200 | [diff] [blame] | 1597 | if (!validate_kcore_addresses("/proc/kallsyms", map)) |
Adrian Hunter | 0544d42 | 2013-10-14 13:43:43 +0300 | [diff] [blame] | 1598 | goto proc_kallsyms; |
| 1599 | } |
| 1600 | |
| 1601 | /* Find kallsyms in build-id cache with kcore */ |
| 1602 | if (!find_matching_kcore(map, path, sizeof(path))) |
| 1603 | return strdup(path); |
| 1604 | |
| 1605 | goto proc_kallsyms; |
| 1606 | } |
| 1607 | |
Adrian Hunter | 449867e | 2013-11-26 15:19:24 +0200 | [diff] [blame] | 1608 | /* Find kallsyms in build-id cache with kcore */ |
| 1609 | if (!find_matching_kcore(map, path, sizeof(path))) |
| 1610 | return strdup(path); |
| 1611 | |
Adrian Hunter | 0544d42 | 2013-10-14 13:43:43 +0300 | [diff] [blame] | 1612 | scnprintf(path, sizeof(path), "%s/[kernel.kallsyms]/%s", |
| 1613 | buildid_dir, sbuild_id); |
| 1614 | |
| 1615 | if (access(path, F_OK)) { |
| 1616 | pr_err("No kallsyms or vmlinux with build-id %s was found\n", |
| 1617 | sbuild_id); |
| 1618 | return NULL; |
| 1619 | } |
| 1620 | |
| 1621 | return strdup(path); |
| 1622 | |
| 1623 | proc_kallsyms: |
| 1624 | return strdup("/proc/kallsyms"); |
| 1625 | } |
| 1626 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 1627 | 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] | 1628 | symbol_filter_t filter) |
Arnaldo Carvalho de Melo | a827c87 | 2009-05-28 14:55:19 -0300 | [diff] [blame] | 1629 | { |
Arnaldo Carvalho de Melo | cc612d8 | 2009-11-23 16:39:10 -0200 | [diff] [blame] | 1630 | int err; |
Arnaldo Carvalho de Melo | 9e20144 | 2010-01-14 18:30:06 -0200 | [diff] [blame] | 1631 | const char *kallsyms_filename = NULL; |
| 1632 | char *kallsyms_allocated_filename = NULL; |
Arnaldo Carvalho de Melo | dc8d6ab | 2010-01-19 10:36:14 -0200 | [diff] [blame] | 1633 | /* |
David Ahern | b226a5a7 | 2010-12-07 19:39:46 -0700 | [diff] [blame] | 1634 | * Step 1: if the user specified a kallsyms or vmlinux filename, use |
| 1635 | * 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] | 1636 | * |
| 1637 | * For instance, try to analyse an ARM perf.data file _without_ a |
| 1638 | * build-id, or if the user specifies the wrong path to the right |
| 1639 | * vmlinux file, obviously we can't fallback to another vmlinux (a |
| 1640 | * x86_86 one, on the machine where analysis is being performed, say), |
| 1641 | * or worse, /proc/kallsyms. |
| 1642 | * |
| 1643 | * If the specified file _has_ a build-id and there is a build-id |
| 1644 | * section in the perf.data file, we will still do the expected |
| 1645 | * validation in dso__load_vmlinux and will bail out if they don't |
| 1646 | * match. |
| 1647 | */ |
David Ahern | b226a5a7 | 2010-12-07 19:39:46 -0700 | [diff] [blame] | 1648 | if (symbol_conf.kallsyms_name != NULL) { |
| 1649 | kallsyms_filename = symbol_conf.kallsyms_name; |
| 1650 | goto do_kallsyms; |
| 1651 | } |
| 1652 | |
Willy Tarreau | fc2be69 | 2013-09-14 10:32:59 +0200 | [diff] [blame] | 1653 | if (!symbol_conf.ignore_vmlinux && symbol_conf.vmlinux_name != NULL) { |
Arnaldo Carvalho de Melo | 5230fb7 | 2013-12-10 11:58:52 -0300 | [diff] [blame] | 1654 | return dso__load_vmlinux(dso, map, symbol_conf.vmlinux_name, |
| 1655 | false, filter); |
Arnaldo Carvalho de Melo | dc8d6ab | 2010-01-19 10:36:14 -0200 | [diff] [blame] | 1656 | } |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1657 | |
Willy Tarreau | fc2be69 | 2013-09-14 10:32:59 +0200 | [diff] [blame] | 1658 | if (!symbol_conf.ignore_vmlinux && vmlinux_path != NULL) { |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 1659 | err = dso__load_vmlinux_path(dso, map, filter); |
Arnaldo Carvalho de Melo | a19afe4 | 2010-01-27 21:05:50 -0200 | [diff] [blame] | 1660 | if (err > 0) |
Adrian Hunter | 39b12f78 | 2013-08-07 14:38:47 +0300 | [diff] [blame] | 1661 | return err; |
Arnaldo Carvalho de Melo | cc612d8 | 2009-11-23 16:39:10 -0200 | [diff] [blame] | 1662 | } |
| 1663 | |
David Ahern | ec5761e | 2010-12-09 13:27:07 -0700 | [diff] [blame] | 1664 | /* do not try local files if a symfs was given */ |
| 1665 | if (symbol_conf.symfs[0] != 0) |
| 1666 | return -1; |
| 1667 | |
Adrian Hunter | 0544d42 | 2013-10-14 13:43:43 +0300 | [diff] [blame] | 1668 | kallsyms_allocated_filename = dso__find_kallsyms(dso, map); |
| 1669 | if (!kallsyms_allocated_filename) |
| 1670 | return -1; |
Arnaldo Carvalho de Melo | b7cece76 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 1671 | |
Adrian Hunter | 0544d42 | 2013-10-14 13:43:43 +0300 | [diff] [blame] | 1672 | kallsyms_filename = kallsyms_allocated_filename; |
Arnaldo Carvalho de Melo | a827c87 | 2009-05-28 14:55:19 -0300 | [diff] [blame] | 1673 | |
Arnaldo Carvalho de Melo | dc8d6ab | 2010-01-19 10:36:14 -0200 | [diff] [blame] | 1674 | do_kallsyms: |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 1675 | err = dso__load_kallsyms(dso, kallsyms_filename, map, filter); |
Arnaldo Carvalho de Melo | 3846df2 | 2010-02-22 16:15:39 -0300 | [diff] [blame] | 1676 | if (err > 0) |
| 1677 | pr_debug("Using %s for symbols\n", kallsyms_filename); |
Arnaldo Carvalho de Melo | dc8d6ab | 2010-01-19 10:36:14 -0200 | [diff] [blame] | 1678 | free(kallsyms_allocated_filename); |
| 1679 | |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 1680 | if (err > 0 && !dso__is_kcore(dso)) { |
Adrian Hunter | bdac0bc | 2014-07-14 13:02:43 +0300 | [diff] [blame] | 1681 | dso->binary_type = DSO_BINARY_TYPE__KALLSYMS; |
Arnaldo Carvalho de Melo | bf4414a | 2013-12-10 15:19:23 -0300 | [diff] [blame] | 1682 | dso__set_long_name(dso, "[kernel.kallsyms]", false); |
Arnaldo Carvalho de Melo | 6a4694a | 2009-11-27 16:29:17 -0200 | [diff] [blame] | 1683 | map__fixup_start(map); |
| 1684 | map__fixup_end(map); |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1685 | } |
Arnaldo Carvalho de Melo | 94cb9e3 | 2009-08-06 14:43:17 -0300 | [diff] [blame] | 1686 | |
Arnaldo Carvalho de Melo | a827c87 | 2009-05-28 14:55:19 -0300 | [diff] [blame] | 1687 | return err; |
| 1688 | } |
| 1689 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 1690 | static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map, |
| 1691 | symbol_filter_t filter) |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1692 | { |
| 1693 | int err; |
| 1694 | const char *kallsyms_filename = NULL; |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 1695 | struct machine *machine; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1696 | char path[PATH_MAX]; |
| 1697 | |
| 1698 | if (!map->groups) { |
| 1699 | pr_debug("Guest kernel map hasn't the point to groups\n"); |
| 1700 | return -1; |
| 1701 | } |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 1702 | machine = map->groups->machine; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1703 | |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 1704 | if (machine__is_default_guest(machine)) { |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1705 | /* |
| 1706 | * if the user specified a vmlinux filename, use it and only |
| 1707 | * it, reporting errors to the user if it cannot be used. |
| 1708 | * Or use file guest_kallsyms inputted by user on commandline |
| 1709 | */ |
| 1710 | if (symbol_conf.default_guest_vmlinux_name != NULL) { |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 1711 | err = dso__load_vmlinux(dso, map, |
Arnaldo Carvalho de Melo | 5230fb7 | 2013-12-10 11:58:52 -0300 | [diff] [blame] | 1712 | symbol_conf.default_guest_vmlinux_name, |
| 1713 | false, filter); |
Adrian Hunter | 39b12f78 | 2013-08-07 14:38:47 +0300 | [diff] [blame] | 1714 | return err; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1715 | } |
| 1716 | |
| 1717 | kallsyms_filename = symbol_conf.default_guest_kallsyms; |
| 1718 | if (!kallsyms_filename) |
| 1719 | return -1; |
| 1720 | } else { |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 1721 | sprintf(path, "%s/proc/kallsyms", machine->root_dir); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1722 | kallsyms_filename = path; |
| 1723 | } |
| 1724 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 1725 | err = dso__load_kallsyms(dso, kallsyms_filename, map, filter); |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 1726 | if (err > 0) |
Adrian Hunter | 39b12f78 | 2013-08-07 14:38:47 +0300 | [diff] [blame] | 1727 | pr_debug("Using %s for symbols\n", kallsyms_filename); |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 1728 | if (err > 0 && !dso__is_kcore(dso)) { |
Adrian Hunter | bdac0bc | 2014-07-14 13:02:43 +0300 | [diff] [blame] | 1729 | dso->binary_type = DSO_BINARY_TYPE__GUEST_KALLSYMS; |
Adrian Hunter | 39b12f78 | 2013-08-07 14:38:47 +0300 | [diff] [blame] | 1730 | machine__mmap_name(machine, path, sizeof(path)); |
Arnaldo Carvalho de Melo | 7e155d4 | 2013-12-10 15:08:44 -0300 | [diff] [blame] | 1731 | dso__set_long_name(dso, strdup(path), true); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1732 | map__fixup_start(map); |
| 1733 | map__fixup_end(map); |
| 1734 | } |
| 1735 | |
| 1736 | return err; |
| 1737 | } |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 1738 | |
Arnaldo Carvalho de Melo | cc612d8 | 2009-11-23 16:39:10 -0200 | [diff] [blame] | 1739 | static void vmlinux_path__exit(void) |
Arnaldo Carvalho de Melo | 2446042 | 2009-11-18 20:20:53 -0200 | [diff] [blame] | 1740 | { |
Arnaldo Carvalho de Melo | 0466252 | 2013-12-26 17:41:15 -0300 | [diff] [blame] | 1741 | while (--vmlinux_path__nr_entries >= 0) |
| 1742 | zfree(&vmlinux_path[vmlinux_path__nr_entries]); |
Arnaldo Carvalho de Melo | cc612d8 | 2009-11-23 16:39:10 -0200 | [diff] [blame] | 1743 | |
Arnaldo Carvalho de Melo | 0466252 | 2013-12-26 17:41:15 -0300 | [diff] [blame] | 1744 | zfree(&vmlinux_path); |
Arnaldo Carvalho de Melo | cc612d8 | 2009-11-23 16:39:10 -0200 | [diff] [blame] | 1745 | } |
| 1746 | |
| 1747 | static int vmlinux_path__init(void) |
| 1748 | { |
| 1749 | struct utsname uts; |
| 1750 | char bf[PATH_MAX]; |
| 1751 | |
Arnaldo Carvalho de Melo | cc612d8 | 2009-11-23 16:39:10 -0200 | [diff] [blame] | 1752 | vmlinux_path = malloc(sizeof(char *) * 5); |
| 1753 | if (vmlinux_path == NULL) |
| 1754 | return -1; |
| 1755 | |
| 1756 | vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux"); |
| 1757 | if (vmlinux_path[vmlinux_path__nr_entries] == NULL) |
| 1758 | goto out_fail; |
| 1759 | ++vmlinux_path__nr_entries; |
| 1760 | vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux"); |
| 1761 | if (vmlinux_path[vmlinux_path__nr_entries] == NULL) |
| 1762 | goto out_fail; |
| 1763 | ++vmlinux_path__nr_entries; |
David Ahern | ec5761e | 2010-12-09 13:27:07 -0700 | [diff] [blame] | 1764 | |
| 1765 | /* only try running kernel version if no symfs was given */ |
| 1766 | if (symbol_conf.symfs[0] != 0) |
| 1767 | return 0; |
| 1768 | |
| 1769 | if (uname(&uts) < 0) |
| 1770 | return -1; |
| 1771 | |
Arnaldo Carvalho de Melo | cc612d8 | 2009-11-23 16:39:10 -0200 | [diff] [blame] | 1772 | snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release); |
| 1773 | vmlinux_path[vmlinux_path__nr_entries] = strdup(bf); |
| 1774 | if (vmlinux_path[vmlinux_path__nr_entries] == NULL) |
| 1775 | goto out_fail; |
| 1776 | ++vmlinux_path__nr_entries; |
| 1777 | snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release); |
| 1778 | vmlinux_path[vmlinux_path__nr_entries] = strdup(bf); |
| 1779 | if (vmlinux_path[vmlinux_path__nr_entries] == NULL) |
| 1780 | goto out_fail; |
| 1781 | ++vmlinux_path__nr_entries; |
| 1782 | snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux", |
| 1783 | uts.release); |
| 1784 | vmlinux_path[vmlinux_path__nr_entries] = strdup(bf); |
| 1785 | if (vmlinux_path[vmlinux_path__nr_entries] == NULL) |
| 1786 | goto out_fail; |
| 1787 | ++vmlinux_path__nr_entries; |
| 1788 | |
| 1789 | return 0; |
| 1790 | |
| 1791 | out_fail: |
| 1792 | vmlinux_path__exit(); |
| 1793 | return -1; |
| 1794 | } |
| 1795 | |
David Ahern | 3bfe5f8 | 2013-11-18 13:32:48 -0700 | [diff] [blame] | 1796 | int setup_list(struct strlist **list, const char *list_str, |
Arnaldo Carvalho de Melo | 655000e | 2009-12-15 20:04:40 -0200 | [diff] [blame] | 1797 | const char *list_name) |
| 1798 | { |
| 1799 | if (list_str == NULL) |
| 1800 | return 0; |
| 1801 | |
| 1802 | *list = strlist__new(true, list_str); |
| 1803 | if (!*list) { |
| 1804 | pr_err("problems parsing %s list\n", list_name); |
| 1805 | return -1; |
| 1806 | } |
| 1807 | return 0; |
| 1808 | } |
| 1809 | |
Arnaldo Carvalho de Melo | ec80fde | 2011-05-26 09:53:51 -0300 | [diff] [blame] | 1810 | static bool symbol__read_kptr_restrict(void) |
| 1811 | { |
| 1812 | bool value = false; |
| 1813 | |
| 1814 | if (geteuid() != 0) { |
| 1815 | FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r"); |
| 1816 | if (fp != NULL) { |
| 1817 | char line[8]; |
| 1818 | |
| 1819 | if (fgets(line, sizeof(line), fp) != NULL) |
| 1820 | value = atoi(line) != 0; |
| 1821 | |
| 1822 | fclose(fp); |
| 1823 | } |
| 1824 | } |
| 1825 | |
| 1826 | return value; |
| 1827 | } |
| 1828 | |
Arnaldo Carvalho de Melo | 75be6cf | 2009-12-15 20:04:39 -0200 | [diff] [blame] | 1829 | int symbol__init(void) |
Arnaldo Carvalho de Melo | cc612d8 | 2009-11-23 16:39:10 -0200 | [diff] [blame] | 1830 | { |
David Ahern | ec5761e | 2010-12-09 13:27:07 -0700 | [diff] [blame] | 1831 | const char *symfs; |
| 1832 | |
Jovi Zhang | 85e00b5 | 2010-09-09 13:30:59 -0300 | [diff] [blame] | 1833 | if (symbol_conf.initialized) |
| 1834 | return 0; |
| 1835 | |
Irina Tirdea | 9ac3e48 | 2012-09-11 01:15:01 +0300 | [diff] [blame] | 1836 | symbol_conf.priv_size = PERF_ALIGN(symbol_conf.priv_size, sizeof(u64)); |
David S. Miller | 4d43951 | 2011-03-29 14:18:39 -0300 | [diff] [blame] | 1837 | |
Namhyung Kim | 166ccc9 | 2012-08-06 13:41:19 +0900 | [diff] [blame] | 1838 | symbol__elf_init(); |
| 1839 | |
Arnaldo Carvalho de Melo | 75be6cf | 2009-12-15 20:04:39 -0200 | [diff] [blame] | 1840 | if (symbol_conf.sort_by_name) |
| 1841 | symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) - |
| 1842 | sizeof(struct symbol)); |
Arnaldo Carvalho de Melo | b32d133 | 2009-11-24 12:05:15 -0200 | [diff] [blame] | 1843 | |
Arnaldo Carvalho de Melo | 75be6cf | 2009-12-15 20:04:39 -0200 | [diff] [blame] | 1844 | if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0) |
Arnaldo Carvalho de Melo | cc612d8 | 2009-11-23 16:39:10 -0200 | [diff] [blame] | 1845 | return -1; |
| 1846 | |
Arnaldo Carvalho de Melo | c410a33 | 2009-12-15 20:04:41 -0200 | [diff] [blame] | 1847 | if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') { |
| 1848 | pr_err("'.' is the only non valid --field-separator argument\n"); |
| 1849 | return -1; |
| 1850 | } |
| 1851 | |
Arnaldo Carvalho de Melo | 655000e | 2009-12-15 20:04:40 -0200 | [diff] [blame] | 1852 | if (setup_list(&symbol_conf.dso_list, |
| 1853 | symbol_conf.dso_list_str, "dso") < 0) |
| 1854 | return -1; |
| 1855 | |
| 1856 | if (setup_list(&symbol_conf.comm_list, |
| 1857 | symbol_conf.comm_list_str, "comm") < 0) |
| 1858 | goto out_free_dso_list; |
| 1859 | |
| 1860 | if (setup_list(&symbol_conf.sym_list, |
| 1861 | symbol_conf.sym_list_str, "symbol") < 0) |
| 1862 | goto out_free_comm_list; |
| 1863 | |
David Ahern | ec5761e | 2010-12-09 13:27:07 -0700 | [diff] [blame] | 1864 | /* |
| 1865 | * A path to symbols of "/" is identical to "" |
| 1866 | * reset here for simplicity. |
| 1867 | */ |
| 1868 | symfs = realpath(symbol_conf.symfs, NULL); |
| 1869 | if (symfs == NULL) |
| 1870 | symfs = symbol_conf.symfs; |
| 1871 | if (strcmp(symfs, "/") == 0) |
| 1872 | symbol_conf.symfs = ""; |
| 1873 | if (symfs != symbol_conf.symfs) |
| 1874 | free((void *)symfs); |
| 1875 | |
Arnaldo Carvalho de Melo | ec80fde | 2011-05-26 09:53:51 -0300 | [diff] [blame] | 1876 | symbol_conf.kptr_restrict = symbol__read_kptr_restrict(); |
| 1877 | |
Jovi Zhang | 85e00b5 | 2010-09-09 13:30:59 -0300 | [diff] [blame] | 1878 | symbol_conf.initialized = true; |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 1879 | return 0; |
Arnaldo Carvalho de Melo | 655000e | 2009-12-15 20:04:40 -0200 | [diff] [blame] | 1880 | |
Arnaldo Carvalho de Melo | 655000e | 2009-12-15 20:04:40 -0200 | [diff] [blame] | 1881 | out_free_comm_list: |
| 1882 | strlist__delete(symbol_conf.comm_list); |
Namhyung Kim | d74c896 | 2011-12-13 00:16:52 +0900 | [diff] [blame] | 1883 | out_free_dso_list: |
| 1884 | strlist__delete(symbol_conf.dso_list); |
Arnaldo Carvalho de Melo | 655000e | 2009-12-15 20:04:40 -0200 | [diff] [blame] | 1885 | return -1; |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 1886 | } |
Arnaldo Carvalho de Melo | cc612d8 | 2009-11-23 16:39:10 -0200 | [diff] [blame] | 1887 | |
Arnaldo Carvalho de Melo | d65a458 | 2010-07-30 18:31:28 -0300 | [diff] [blame] | 1888 | void symbol__exit(void) |
| 1889 | { |
Jovi Zhang | 85e00b5 | 2010-09-09 13:30:59 -0300 | [diff] [blame] | 1890 | if (!symbol_conf.initialized) |
| 1891 | return; |
Arnaldo Carvalho de Melo | d65a458 | 2010-07-30 18:31:28 -0300 | [diff] [blame] | 1892 | strlist__delete(symbol_conf.sym_list); |
| 1893 | strlist__delete(symbol_conf.dso_list); |
| 1894 | strlist__delete(symbol_conf.comm_list); |
| 1895 | vmlinux_path__exit(); |
| 1896 | 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] | 1897 | symbol_conf.initialized = false; |
Arnaldo Carvalho de Melo | d65a458 | 2010-07-30 18:31:28 -0300 | [diff] [blame] | 1898 | } |