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