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 | b01141f | 2016-08-25 16:09:21 -0300 | [diff] [blame] | 12 | #include "annotate.h" |
Arnaldo Carvalho de Melo | b36f19d | 2010-05-20 12:15:33 -0300 | [diff] [blame] | 13 | #include "build-id.h" |
Namhyung Kim | e334c72 | 2012-02-10 10:10:17 +0900 | [diff] [blame] | 14 | #include "util.h" |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 15 | #include "debug.h" |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 16 | #include "machine.h" |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 17 | #include "symbol.h" |
Arnaldo Carvalho de Melo | 5aab621 | 2010-03-25 19:59:00 -0300 | [diff] [blame] | 18 | #include "strlist.h" |
David Ahern | e03eaa4 | 2015-03-24 09:52:41 -0600 | [diff] [blame] | 19 | #include "intlist.h" |
Namhyung Kim | 0a7e6d1 | 2014-08-12 15:40:45 +0900 | [diff] [blame] | 20 | #include "header.h" |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 21 | |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 22 | #include <elf.h> |
Arnaldo Carvalho de Melo | f1617b4 | 2009-11-18 20:20:52 -0200 | [diff] [blame] | 23 | #include <limits.h> |
Arnaldo Carvalho de Melo | c506c96 | 2013-12-11 09:15:00 -0300 | [diff] [blame] | 24 | #include <symbol/kallsyms.h> |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 25 | #include <sys/utsname.h> |
Peter Zijlstra | 2cdbc46 | 2009-08-05 14:05:16 +0200 | [diff] [blame] | 26 | |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame^] | 27 | static int dso__load_kernel_sym(struct dso *dso, struct map *map); |
| 28 | static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map); |
Arnaldo Carvalho de Melo | 608c34d | 2016-09-01 17:54:31 -0300 | [diff] [blame] | 29 | static bool symbol__is_idle(const char *name); |
| 30 | |
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 = { |
Namhyung Kim | e511db5 | 2013-12-24 16:19:25 +0900 | [diff] [blame] | 35 | .use_modules = true, |
| 36 | .try_vmlinux_path = true, |
| 37 | .annotate_src = true, |
| 38 | .demangle = true, |
Avi Kivity | 763122a | 2014-09-13 07:15:05 +0300 | [diff] [blame] | 39 | .demangle_kernel = false, |
Namhyung Kim | e511db5 | 2013-12-24 16:19:25 +0900 | [diff] [blame] | 40 | .cumulate_callchain = true, |
Jiri Olsa | c830236 | 2014-06-27 18:26:58 +0200 | [diff] [blame] | 41 | .show_hist_headers = true, |
Namhyung Kim | e511db5 | 2013-12-24 16:19:25 +0900 | [diff] [blame] | 42 | .symfs = "", |
Namhyung Kim | 1e9abf8 | 2015-11-29 23:24:17 +0900 | [diff] [blame] | 43 | .event_group = true, |
Arnaldo Carvalho de Melo | b32d133 | 2009-11-24 12:05:15 -0200 | [diff] [blame] | 44 | }; |
| 45 | |
Jiri Olsa | 44f24cb | 2012-07-22 14:14:32 +0200 | [diff] [blame] | 46 | static enum dso_binary_type binary_type_symtab[] = { |
| 47 | DSO_BINARY_TYPE__KALLSYMS, |
| 48 | DSO_BINARY_TYPE__GUEST_KALLSYMS, |
| 49 | DSO_BINARY_TYPE__JAVA_JIT, |
| 50 | DSO_BINARY_TYPE__DEBUGLINK, |
| 51 | DSO_BINARY_TYPE__BUILD_ID_CACHE, |
| 52 | DSO_BINARY_TYPE__FEDORA_DEBUGINFO, |
| 53 | DSO_BINARY_TYPE__UBUNTU_DEBUGINFO, |
| 54 | DSO_BINARY_TYPE__BUILDID_DEBUGINFO, |
| 55 | DSO_BINARY_TYPE__SYSTEM_PATH_DSO, |
| 56 | DSO_BINARY_TYPE__GUEST_KMODULE, |
Namhyung Kim | c00c48f | 2014-11-04 10:14:27 +0900 | [diff] [blame] | 57 | DSO_BINARY_TYPE__GUEST_KMODULE_COMP, |
Jiri Olsa | 44f24cb | 2012-07-22 14:14:32 +0200 | [diff] [blame] | 58 | DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE, |
Namhyung Kim | c00c48f | 2014-11-04 10:14:27 +0900 | [diff] [blame] | 59 | DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP, |
Ricardo Ribalda Delgado | 9cd0094 | 2013-09-18 15:56:14 +0200 | [diff] [blame] | 60 | DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO, |
Jiri Olsa | 44f24cb | 2012-07-22 14:14:32 +0200 | [diff] [blame] | 61 | DSO_BINARY_TYPE__NOT_FOUND, |
| 62 | }; |
| 63 | |
Jiri Olsa | 028df76 | 2012-08-01 14:47:57 +0200 | [diff] [blame] | 64 | #define DSO_BINARY_TYPE__SYMTAB_CNT ARRAY_SIZE(binary_type_symtab) |
Jiri Olsa | 44f24cb | 2012-07-22 14:14:32 +0200 | [diff] [blame] | 65 | |
Arnaldo Carvalho de Melo | 36a3e64 | 2010-01-04 16:19:27 -0200 | [diff] [blame] | 66 | 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] | 67 | { |
Anton Blanchard | 3187790 | 2011-08-24 16:40:16 +1000 | [diff] [blame] | 68 | symbol_type = toupper(symbol_type); |
| 69 | |
Arnaldo Carvalho de Melo | 6893d4e | 2009-12-11 14:50:37 -0200 | [diff] [blame] | 70 | switch (map_type) { |
| 71 | case MAP__FUNCTION: |
| 72 | return symbol_type == 'T' || symbol_type == 'W'; |
Arnaldo Carvalho de Melo | f1dfa0b | 2009-12-11 14:50:39 -0200 | [diff] [blame] | 73 | case MAP__VARIABLE: |
Anton Blanchard | 3187790 | 2011-08-24 16:40:16 +1000 | [diff] [blame] | 74 | return symbol_type == 'D'; |
Arnaldo Carvalho de Melo | 6893d4e | 2009-12-11 14:50:37 -0200 | [diff] [blame] | 75 | default: |
| 76 | return false; |
| 77 | } |
| 78 | } |
| 79 | |
Anton Blanchard | 694bf40 | 2011-08-24 16:40:17 +1000 | [diff] [blame] | 80 | static int prefix_underscores_count(const char *str) |
| 81 | { |
| 82 | const char *tail = str; |
| 83 | |
| 84 | while (*tail == '_') |
| 85 | tail++; |
| 86 | |
| 87 | return tail - str; |
| 88 | } |
| 89 | |
Naveen N. Rao | fb6d594 | 2015-04-28 17:35:36 +0530 | [diff] [blame] | 90 | int __weak arch__choose_best_symbol(struct symbol *syma, |
| 91 | struct symbol *symb __maybe_unused) |
| 92 | { |
| 93 | /* Avoid "SyS" kernel syscall aliases */ |
| 94 | if (strlen(syma->name) >= 3 && !strncmp(syma->name, "SyS", 3)) |
| 95 | return SYMBOL_B; |
| 96 | if (strlen(syma->name) >= 10 && !strncmp(syma->name, "compat_SyS", 10)) |
| 97 | return SYMBOL_B; |
| 98 | |
| 99 | return SYMBOL_A; |
| 100 | } |
Anton Blanchard | 694bf40 | 2011-08-24 16:40:17 +1000 | [diff] [blame] | 101 | |
| 102 | static int choose_best_symbol(struct symbol *syma, struct symbol *symb) |
| 103 | { |
| 104 | s64 a; |
| 105 | s64 b; |
Adrian Hunter | 3445432 | 2013-08-07 14:38:49 +0300 | [diff] [blame] | 106 | size_t na, nb; |
Anton Blanchard | 694bf40 | 2011-08-24 16:40:17 +1000 | [diff] [blame] | 107 | |
| 108 | /* Prefer a symbol with non zero length */ |
| 109 | a = syma->end - syma->start; |
| 110 | b = symb->end - symb->start; |
| 111 | if ((b == 0) && (a > 0)) |
| 112 | return SYMBOL_A; |
| 113 | else if ((a == 0) && (b > 0)) |
| 114 | return SYMBOL_B; |
| 115 | |
| 116 | /* Prefer a non weak symbol over a weak one */ |
| 117 | a = syma->binding == STB_WEAK; |
| 118 | b = symb->binding == STB_WEAK; |
| 119 | if (b && !a) |
| 120 | return SYMBOL_A; |
| 121 | if (a && !b) |
| 122 | return SYMBOL_B; |
| 123 | |
| 124 | /* Prefer a global symbol over a non global one */ |
| 125 | a = syma->binding == STB_GLOBAL; |
| 126 | b = symb->binding == STB_GLOBAL; |
| 127 | if (a && !b) |
| 128 | return SYMBOL_A; |
| 129 | if (b && !a) |
| 130 | return SYMBOL_B; |
| 131 | |
| 132 | /* Prefer a symbol with less underscores */ |
| 133 | a = prefix_underscores_count(syma->name); |
| 134 | b = prefix_underscores_count(symb->name); |
| 135 | if (b > a) |
| 136 | return SYMBOL_A; |
| 137 | else if (a > b) |
| 138 | return SYMBOL_B; |
| 139 | |
Adrian Hunter | 3445432 | 2013-08-07 14:38:49 +0300 | [diff] [blame] | 140 | /* Choose the symbol with the longest name */ |
| 141 | na = strlen(syma->name); |
| 142 | nb = strlen(symb->name); |
| 143 | if (na > nb) |
Anton Blanchard | 694bf40 | 2011-08-24 16:40:17 +1000 | [diff] [blame] | 144 | return SYMBOL_A; |
Adrian Hunter | 3445432 | 2013-08-07 14:38:49 +0300 | [diff] [blame] | 145 | else if (na < nb) |
Anton Blanchard | 694bf40 | 2011-08-24 16:40:17 +1000 | [diff] [blame] | 146 | return SYMBOL_B; |
Adrian Hunter | 3445432 | 2013-08-07 14:38:49 +0300 | [diff] [blame] | 147 | |
Naveen N. Rao | fb6d594 | 2015-04-28 17:35:36 +0530 | [diff] [blame] | 148 | return arch__choose_best_symbol(syma, symb); |
Anton Blanchard | 694bf40 | 2011-08-24 16:40:17 +1000 | [diff] [blame] | 149 | } |
| 150 | |
Namhyung Kim | e5a1845 | 2012-08-06 13:41:20 +0900 | [diff] [blame] | 151 | void symbols__fixup_duplicate(struct rb_root *symbols) |
Anton Blanchard | 694bf40 | 2011-08-24 16:40:17 +1000 | [diff] [blame] | 152 | { |
| 153 | struct rb_node *nd; |
| 154 | struct symbol *curr, *next; |
| 155 | |
Arnaldo Carvalho de Melo | c97b40e | 2016-09-01 10:56:06 -0300 | [diff] [blame] | 156 | if (symbol_conf.allow_aliases) |
| 157 | return; |
| 158 | |
Anton Blanchard | 694bf40 | 2011-08-24 16:40:17 +1000 | [diff] [blame] | 159 | nd = rb_first(symbols); |
| 160 | |
| 161 | while (nd) { |
| 162 | curr = rb_entry(nd, struct symbol, rb_node); |
| 163 | again: |
| 164 | nd = rb_next(&curr->rb_node); |
| 165 | next = rb_entry(nd, struct symbol, rb_node); |
| 166 | |
| 167 | if (!nd) |
| 168 | break; |
| 169 | |
| 170 | if (curr->start != next->start) |
| 171 | continue; |
| 172 | |
| 173 | if (choose_best_symbol(curr, next) == SYMBOL_A) { |
| 174 | rb_erase(&next->rb_node, symbols); |
Chenggang Qin | d4f74eb | 2013-10-11 08:27:59 +0800 | [diff] [blame] | 175 | symbol__delete(next); |
Anton Blanchard | 694bf40 | 2011-08-24 16:40:17 +1000 | [diff] [blame] | 176 | goto again; |
| 177 | } else { |
| 178 | nd = rb_next(&curr->rb_node); |
| 179 | rb_erase(&curr->rb_node, symbols); |
Chenggang Qin | d4f74eb | 2013-10-11 08:27:59 +0800 | [diff] [blame] | 180 | symbol__delete(curr); |
Anton Blanchard | 694bf40 | 2011-08-24 16:40:17 +1000 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | } |
| 184 | |
Namhyung Kim | e5a1845 | 2012-08-06 13:41:20 +0900 | [diff] [blame] | 185 | void symbols__fixup_end(struct rb_root *symbols) |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 186 | { |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 187 | struct rb_node *nd, *prevnd = rb_first(symbols); |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 188 | struct symbol *curr, *prev; |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 189 | |
| 190 | if (prevnd == NULL) |
| 191 | return; |
| 192 | |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 193 | curr = rb_entry(prevnd, struct symbol, rb_node); |
| 194 | |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 195 | for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) { |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 196 | prev = curr; |
| 197 | curr = rb_entry(nd, struct symbol, rb_node); |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 198 | |
Arnaldo Carvalho de Melo | 3b01a41 | 2010-12-22 01:08:36 -0200 | [diff] [blame] | 199 | if (prev->end == prev->start && prev->end != curr->start) |
Arnaldo Carvalho de Melo | 2c241bd | 2014-10-14 17:19:44 -0300 | [diff] [blame] | 200 | prev->end = curr->start; |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 201 | } |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 202 | |
| 203 | /* Last entry */ |
| 204 | if (curr->end == curr->start) |
| 205 | curr->end = roundup(curr->start, 4096); |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 206 | } |
| 207 | |
Namhyung Kim | e5a1845 | 2012-08-06 13:41:20 +0900 | [diff] [blame] | 208 | 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] | 209 | { |
Arnaldo Carvalho de Melo | 1eee78a | 2015-05-22 12:58:53 -0300 | [diff] [blame] | 210 | struct maps *maps = &mg->maps[type]; |
Arnaldo Carvalho de Melo | 4bb7123d | 2015-05-22 11:52:22 -0300 | [diff] [blame] | 211 | struct map *next, *curr; |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 212 | |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 213 | pthread_rwlock_wrlock(&maps->lock); |
| 214 | |
Arnaldo Carvalho de Melo | 4bb7123d | 2015-05-22 11:52:22 -0300 | [diff] [blame] | 215 | curr = maps__first(maps); |
| 216 | if (curr == NULL) |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 217 | goto out_unlock; |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 218 | |
Arnaldo Carvalho de Melo | 4bb7123d | 2015-05-22 11:52:22 -0300 | [diff] [blame] | 219 | for (next = map__next(curr); next; next = map__next(curr)) { |
| 220 | curr->end = next->start; |
| 221 | curr = next; |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 222 | } |
Arnaldo Carvalho de Melo | 90c8321 | 2009-11-21 14:31:24 -0200 | [diff] [blame] | 223 | |
| 224 | /* |
| 225 | * We still haven't the actual symbols, so guess the |
| 226 | * last map final address. |
| 227 | */ |
Ian Munsie | 9d1faba | 2010-11-25 15:12:53 +1100 | [diff] [blame] | 228 | curr->end = ~0ULL; |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 229 | |
| 230 | out_unlock: |
| 231 | pthread_rwlock_unlock(&maps->lock); |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 232 | } |
| 233 | |
Namhyung Kim | e5a1845 | 2012-08-06 13:41:20 +0900 | [diff] [blame] | 234 | 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] | 235 | { |
Arnaldo Carvalho de Melo | 0085c954 | 2009-05-28 14:55:13 -0300 | [diff] [blame] | 236 | size_t namelen = strlen(name) + 1; |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 237 | struct symbol *sym = calloc(1, (symbol_conf.priv_size + |
| 238 | sizeof(*sym) + namelen)); |
| 239 | if (sym == NULL) |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 240 | return NULL; |
| 241 | |
Arnaldo Carvalho de Melo | b01141f | 2016-08-25 16:09:21 -0300 | [diff] [blame] | 242 | if (symbol_conf.priv_size) { |
| 243 | if (symbol_conf.init_annotation) { |
| 244 | struct annotation *notes = (void *)sym; |
| 245 | pthread_mutex_init(¬es->lock, NULL); |
| 246 | } |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 247 | sym = ((void *)sym) + symbol_conf.priv_size; |
Arnaldo Carvalho de Melo | b01141f | 2016-08-25 16:09:21 -0300 | [diff] [blame] | 248 | } |
Arnaldo Carvalho de Melo | 3647948 | 2009-11-24 12:05:16 -0200 | [diff] [blame] | 249 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 250 | sym->start = start; |
Arnaldo Carvalho de Melo | 2c241bd | 2014-10-14 17:19:44 -0300 | [diff] [blame] | 251 | sym->end = len ? start + len : start; |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 252 | sym->binding = binding; |
| 253 | sym->namelen = namelen - 1; |
Arnaldo Carvalho de Melo | e420499 | 2009-10-20 14:25:40 -0200 | [diff] [blame] | 254 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 255 | pr_debug4("%s: %s %#" PRIx64 "-%#" PRIx64 "\n", |
| 256 | __func__, name, start, sym->end); |
| 257 | memcpy(sym->name, name, namelen); |
Arnaldo Carvalho de Melo | e420499 | 2009-10-20 14:25:40 -0200 | [diff] [blame] | 258 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 259 | return sym; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 260 | } |
| 261 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 262 | void symbol__delete(struct symbol *sym) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 263 | { |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 264 | free(((void *)sym) - symbol_conf.priv_size); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 265 | } |
| 266 | |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 267 | void symbols__delete(struct rb_root *symbols) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 268 | { |
| 269 | struct symbol *pos; |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 270 | struct rb_node *next = rb_first(symbols); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 271 | |
| 272 | while (next) { |
| 273 | pos = rb_entry(next, struct symbol, rb_node); |
| 274 | next = rb_next(&pos->rb_node); |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 275 | rb_erase(&pos->rb_node, symbols); |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 276 | symbol__delete(pos); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 277 | } |
| 278 | } |
| 279 | |
Arnaldo Carvalho de Melo | 608c34d | 2016-09-01 17:54:31 -0300 | [diff] [blame] | 280 | void __symbols__insert(struct rb_root *symbols, struct symbol *sym, bool kernel) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 281 | { |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 282 | struct rb_node **p = &symbols->rb_node; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 283 | struct rb_node *parent = NULL; |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 284 | const u64 ip = sym->start; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 285 | struct symbol *s; |
| 286 | |
Arnaldo Carvalho de Melo | 608c34d | 2016-09-01 17:54:31 -0300 | [diff] [blame] | 287 | if (kernel) { |
| 288 | const char *name = sym->name; |
| 289 | /* |
| 290 | * ppc64 uses function descriptors and appends a '.' to the |
| 291 | * start of every instruction address. Remove it. |
| 292 | */ |
| 293 | if (name[0] == '.') |
| 294 | name++; |
| 295 | sym->idle = symbol__is_idle(name); |
| 296 | } |
| 297 | |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 298 | while (*p != NULL) { |
| 299 | parent = *p; |
| 300 | s = rb_entry(parent, struct symbol, rb_node); |
| 301 | if (ip < s->start) |
| 302 | p = &(*p)->rb_left; |
| 303 | else |
| 304 | p = &(*p)->rb_right; |
| 305 | } |
| 306 | rb_link_node(&sym->rb_node, parent, p); |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 307 | rb_insert_color(&sym->rb_node, symbols); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 308 | } |
| 309 | |
Arnaldo Carvalho de Melo | 608c34d | 2016-09-01 17:54:31 -0300 | [diff] [blame] | 310 | void symbols__insert(struct rb_root *symbols, struct symbol *sym) |
| 311 | { |
| 312 | __symbols__insert(symbols, sym, false); |
| 313 | } |
| 314 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 315 | 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] | 316 | { |
| 317 | struct rb_node *n; |
| 318 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 319 | if (symbols == NULL) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 320 | return NULL; |
| 321 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 322 | n = symbols->rb_node; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 323 | |
| 324 | while (n) { |
| 325 | struct symbol *s = rb_entry(n, struct symbol, rb_node); |
| 326 | |
| 327 | if (ip < s->start) |
| 328 | n = n->rb_left; |
Chris Phlipot | 9c7b37c | 2016-05-07 02:16:59 -0700 | [diff] [blame] | 329 | else if (ip > s->end || (ip == s->end && ip != s->start)) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 330 | n = n->rb_right; |
| 331 | else |
| 332 | return s; |
| 333 | } |
| 334 | |
| 335 | return NULL; |
| 336 | } |
| 337 | |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 338 | static struct symbol *symbols__first(struct rb_root *symbols) |
| 339 | { |
| 340 | struct rb_node *n = rb_first(symbols); |
| 341 | |
| 342 | if (n) |
| 343 | return rb_entry(n, struct symbol, rb_node); |
| 344 | |
| 345 | return NULL; |
| 346 | } |
| 347 | |
Adrian Hunter | 9c00a81 | 2014-07-14 13:02:50 +0300 | [diff] [blame] | 348 | static struct symbol *symbols__next(struct symbol *sym) |
| 349 | { |
| 350 | struct rb_node *n = rb_next(&sym->rb_node); |
| 351 | |
| 352 | if (n) |
| 353 | return rb_entry(n, struct symbol, rb_node); |
| 354 | |
| 355 | return NULL; |
| 356 | } |
| 357 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 358 | 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] | 359 | { |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 360 | struct rb_node **p = &symbols->rb_node; |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 361 | struct rb_node *parent = NULL; |
Rabin Vincent | 02a9d03 | 2010-11-23 22:08:18 +0530 | [diff] [blame] | 362 | struct symbol_name_rb_node *symn, *s; |
| 363 | |
| 364 | symn = container_of(sym, struct symbol_name_rb_node, sym); |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 365 | |
| 366 | while (*p != NULL) { |
| 367 | parent = *p; |
| 368 | s = rb_entry(parent, struct symbol_name_rb_node, rb_node); |
| 369 | if (strcmp(sym->name, s->sym.name) < 0) |
| 370 | p = &(*p)->rb_left; |
| 371 | else |
| 372 | p = &(*p)->rb_right; |
| 373 | } |
| 374 | rb_link_node(&symn->rb_node, parent, p); |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 375 | rb_insert_color(&symn->rb_node, symbols); |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 376 | } |
| 377 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 378 | static void symbols__sort_by_name(struct rb_root *symbols, |
| 379 | struct rb_root *source) |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 380 | { |
| 381 | struct rb_node *nd; |
| 382 | |
| 383 | for (nd = rb_first(source); nd; nd = rb_next(nd)) { |
| 384 | struct symbol *pos = rb_entry(nd, struct symbol, rb_node); |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 385 | symbols__insert_by_name(symbols, pos); |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 386 | } |
| 387 | } |
| 388 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 389 | static struct symbol *symbols__find_by_name(struct rb_root *symbols, |
| 390 | const char *name) |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 391 | { |
| 392 | struct rb_node *n; |
Martin Liška | 5bcaaca | 2015-05-26 11:41:37 -0300 | [diff] [blame] | 393 | struct symbol_name_rb_node *s = NULL; |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 394 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 395 | if (symbols == NULL) |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 396 | return NULL; |
| 397 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 398 | n = symbols->rb_node; |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 399 | |
| 400 | while (n) { |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 401 | int cmp; |
| 402 | |
| 403 | s = rb_entry(n, struct symbol_name_rb_node, rb_node); |
Naveen N. Rao | 031b84c | 2015-04-28 17:35:37 +0530 | [diff] [blame] | 404 | cmp = arch__compare_symbol_names(name, s->sym.name); |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 405 | |
| 406 | if (cmp < 0) |
| 407 | n = n->rb_left; |
| 408 | else if (cmp > 0) |
| 409 | n = n->rb_right; |
| 410 | else |
Namhyung Kim | de48099 | 2015-01-16 15:31:28 -0300 | [diff] [blame] | 411 | break; |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 412 | } |
| 413 | |
Namhyung Kim | de48099 | 2015-01-16 15:31:28 -0300 | [diff] [blame] | 414 | if (n == NULL) |
| 415 | return NULL; |
| 416 | |
| 417 | /* return first symbol that has same name (if any) */ |
| 418 | for (n = rb_prev(n); n; n = rb_prev(n)) { |
| 419 | struct symbol_name_rb_node *tmp; |
| 420 | |
| 421 | tmp = rb_entry(n, struct symbol_name_rb_node, rb_node); |
Naveen N. Rao | 031b84c | 2015-04-28 17:35:37 +0530 | [diff] [blame] | 422 | if (arch__compare_symbol_names(tmp->sym.name, s->sym.name)) |
Namhyung Kim | de48099 | 2015-01-16 15:31:28 -0300 | [diff] [blame] | 423 | break; |
| 424 | |
| 425 | s = tmp; |
| 426 | } |
| 427 | |
| 428 | return &s->sym; |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 429 | } |
| 430 | |
Arnaldo Carvalho de Melo | c0b4dff | 2015-08-24 13:33:14 -0300 | [diff] [blame] | 431 | void dso__reset_find_symbol_cache(struct dso *dso) |
| 432 | { |
| 433 | enum map_type type; |
| 434 | |
| 435 | for (type = MAP__FUNCTION; type <= MAP__VARIABLE; ++type) { |
| 436 | dso->last_find_result[type].addr = 0; |
| 437 | dso->last_find_result[type].symbol = NULL; |
| 438 | } |
| 439 | } |
| 440 | |
Chris Phlipot | ae93a6c | 2016-05-10 20:26:46 -0700 | [diff] [blame] | 441 | void dso__insert_symbol(struct dso *dso, enum map_type type, struct symbol *sym) |
| 442 | { |
Arnaldo Carvalho de Melo | 608c34d | 2016-09-01 17:54:31 -0300 | [diff] [blame] | 443 | __symbols__insert(&dso->symbols[type], sym, dso->kernel); |
Chris Phlipot | ae93a6c | 2016-05-10 20:26:46 -0700 | [diff] [blame] | 444 | |
| 445 | /* update the symbol cache if necessary */ |
| 446 | if (dso->last_find_result[type].addr >= sym->start && |
| 447 | (dso->last_find_result[type].addr < sym->end || |
| 448 | sym->start == sym->end)) { |
| 449 | dso->last_find_result[type].symbol = sym; |
| 450 | } |
| 451 | } |
| 452 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 453 | struct symbol *dso__find_symbol(struct dso *dso, |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 454 | enum map_type type, u64 addr) |
Arnaldo Carvalho de Melo | fcf1203 | 2009-11-24 13:01:52 -0200 | [diff] [blame] | 455 | { |
Arnaldo Carvalho de Melo | b685ac2 | 2015-07-22 12:52:17 -0300 | [diff] [blame] | 456 | if (dso->last_find_result[type].addr != addr) { |
| 457 | dso->last_find_result[type].addr = addr; |
| 458 | dso->last_find_result[type].symbol = symbols__find(&dso->symbols[type], addr); |
| 459 | } |
| 460 | |
| 461 | return dso->last_find_result[type].symbol; |
Arnaldo Carvalho de Melo | fcf1203 | 2009-11-24 13:01:52 -0200 | [diff] [blame] | 462 | } |
| 463 | |
Adrian Hunter | 9c00a81 | 2014-07-14 13:02:50 +0300 | [diff] [blame] | 464 | struct symbol *dso__first_symbol(struct dso *dso, enum map_type type) |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 465 | { |
| 466 | return symbols__first(&dso->symbols[type]); |
| 467 | } |
| 468 | |
Adrian Hunter | 9c00a81 | 2014-07-14 13:02:50 +0300 | [diff] [blame] | 469 | struct symbol *dso__next_symbol(struct symbol *sym) |
| 470 | { |
| 471 | return symbols__next(sym); |
| 472 | } |
| 473 | |
Arnaldo Carvalho de Melo | 18bd726 | 2015-01-16 15:39:53 -0300 | [diff] [blame] | 474 | struct symbol *symbol__next_by_name(struct symbol *sym) |
| 475 | { |
| 476 | struct symbol_name_rb_node *s = container_of(sym, struct symbol_name_rb_node, sym); |
| 477 | struct rb_node *n = rb_next(&s->rb_node); |
| 478 | |
| 479 | return n ? &rb_entry(n, struct symbol_name_rb_node, rb_node)->sym : NULL; |
| 480 | } |
| 481 | |
| 482 | /* |
| 483 | * Teturns first symbol that matched with @name. |
| 484 | */ |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 485 | 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] | 486 | const char *name) |
| 487 | { |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 488 | return symbols__find_by_name(&dso->symbol_names[type], name); |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 489 | } |
| 490 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 491 | 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] | 492 | { |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 493 | dso__set_sorted_by_name(dso, type); |
| 494 | return symbols__sort_by_name(&dso->symbol_names[type], |
| 495 | &dso->symbols[type]); |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 496 | } |
| 497 | |
Adrian Hunter | 316d70d | 2013-10-08 11:45:48 +0300 | [diff] [blame] | 498 | int modules__parse(const char *filename, void *arg, |
| 499 | int (*process_module)(void *arg, const char *name, |
| 500 | u64 start)) |
| 501 | { |
| 502 | char *line = NULL; |
| 503 | size_t n; |
| 504 | FILE *file; |
| 505 | int err = 0; |
| 506 | |
| 507 | file = fopen(filename, "r"); |
| 508 | if (file == NULL) |
| 509 | return -1; |
| 510 | |
| 511 | while (1) { |
| 512 | char name[PATH_MAX]; |
| 513 | u64 start; |
| 514 | char *sep; |
| 515 | ssize_t line_len; |
| 516 | |
| 517 | line_len = getline(&line, &n, file); |
| 518 | if (line_len < 0) { |
| 519 | if (feof(file)) |
| 520 | break; |
| 521 | err = -1; |
| 522 | goto out; |
| 523 | } |
| 524 | |
| 525 | if (!line) { |
| 526 | err = -1; |
| 527 | goto out; |
| 528 | } |
| 529 | |
| 530 | line[--line_len] = '\0'; /* \n */ |
| 531 | |
| 532 | sep = strrchr(line, 'x'); |
| 533 | if (sep == NULL) |
| 534 | continue; |
| 535 | |
| 536 | hex2u64(sep + 1, &start); |
| 537 | |
| 538 | sep = strchr(line, ' '); |
| 539 | if (sep == NULL) |
| 540 | continue; |
| 541 | |
| 542 | *sep = '\0'; |
| 543 | |
| 544 | scnprintf(name, sizeof(name), "[%s]", line); |
| 545 | |
| 546 | err = process_module(arg, name, start); |
| 547 | if (err) |
| 548 | break; |
| 549 | } |
| 550 | out: |
| 551 | free(line); |
| 552 | fclose(file); |
| 553 | return err; |
| 554 | } |
| 555 | |
Arnaldo Carvalho de Melo | 682b335 | 2010-01-04 16:19:26 -0200 | [diff] [blame] | 556 | struct process_kallsyms_args { |
| 557 | struct map *map; |
| 558 | struct dso *dso; |
| 559 | }; |
| 560 | |
Arnaldo Carvalho de Melo | e7110b9 | 2014-08-08 18:00:39 -0300 | [diff] [blame] | 561 | /* |
| 562 | * These are symbols in the kernel image, so make sure that |
| 563 | * sym is from a kernel DSO. |
| 564 | */ |
Arnaldo Carvalho de Melo | 608c34d | 2016-09-01 17:54:31 -0300 | [diff] [blame] | 565 | static bool symbol__is_idle(const char *name) |
David Ahern | 82d1deb | 2013-11-18 13:32:45 -0700 | [diff] [blame] | 566 | { |
| 567 | const char * const idle_symbols[] = { |
| 568 | "cpu_idle", |
Arnaldo Carvalho de Melo | e0336ed | 2014-08-08 18:02:41 -0300 | [diff] [blame] | 569 | "cpu_startup_entry", |
David Ahern | 82d1deb | 2013-11-18 13:32:45 -0700 | [diff] [blame] | 570 | "intel_idle", |
| 571 | "default_idle", |
| 572 | "native_safe_halt", |
| 573 | "enter_idle", |
| 574 | "exit_idle", |
| 575 | "mwait_idle", |
| 576 | "mwait_idle_with_hints", |
| 577 | "poll_idle", |
| 578 | "ppc64_runlatch_off", |
| 579 | "pseries_dedicated_idle_sleep", |
| 580 | NULL |
| 581 | }; |
David Ahern | 82d1deb | 2013-11-18 13:32:45 -0700 | [diff] [blame] | 582 | int i; |
| 583 | |
David Ahern | 82d1deb | 2013-11-18 13:32:45 -0700 | [diff] [blame] | 584 | for (i = 0; idle_symbols[i]; i++) { |
Arnaldo Carvalho de Melo | 608c34d | 2016-09-01 17:54:31 -0300 | [diff] [blame] | 585 | if (!strcmp(idle_symbols[i], name)) |
David Ahern | 82d1deb | 2013-11-18 13:32:45 -0700 | [diff] [blame] | 586 | return true; |
| 587 | } |
| 588 | |
| 589 | return false; |
| 590 | } |
| 591 | |
Arnaldo Carvalho de Melo | 682b335 | 2010-01-04 16:19:26 -0200 | [diff] [blame] | 592 | static int map__process_kallsym_symbol(void *arg, const char *name, |
Cody P Schafer | 8215152 | 2012-08-10 15:22:48 -0700 | [diff] [blame] | 593 | char type, u64 start) |
Arnaldo Carvalho de Melo | 682b335 | 2010-01-04 16:19:26 -0200 | [diff] [blame] | 594 | { |
| 595 | struct symbol *sym; |
| 596 | struct process_kallsyms_args *a = arg; |
| 597 | struct rb_root *root = &a->dso->symbols[a->map->type]; |
| 598 | |
| 599 | if (!symbol_type__is_a(type, a->map->type)) |
| 600 | return 0; |
| 601 | |
Cody P Schafer | 8215152 | 2012-08-10 15:22:48 -0700 | [diff] [blame] | 602 | /* |
| 603 | * module symbols are not sorted so we add all |
| 604 | * symbols, setting length to 0, and rely on |
| 605 | * symbols__fixup_end() to fix it up. |
| 606 | */ |
Arnaldo Carvalho de Melo | 8e947f1 | 2015-09-30 12:42:07 -0300 | [diff] [blame] | 607 | sym = symbol__new(start, 0, kallsyms2elf_binding(type), name); |
Arnaldo Carvalho de Melo | 682b335 | 2010-01-04 16:19:26 -0200 | [diff] [blame] | 608 | if (sym == NULL) |
| 609 | return -ENOMEM; |
| 610 | /* |
| 611 | * We will pass the symbols to the filter later, in |
| 612 | * map__split_kallsyms, when we have split the maps per module |
| 613 | */ |
Arnaldo Carvalho de Melo | 608c34d | 2016-09-01 17:54:31 -0300 | [diff] [blame] | 614 | __symbols__insert(root, sym, !strchr(name, '[')); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 615 | |
Arnaldo Carvalho de Melo | 682b335 | 2010-01-04 16:19:26 -0200 | [diff] [blame] | 616 | return 0; |
| 617 | } |
| 618 | |
| 619 | /* |
| 620 | * Loads the function entries in /proc/kallsyms into kernel_map->dso, |
| 621 | * so that we can in the next step set the symbol ->end address and then |
| 622 | * call kernel_maps__split_kallsyms. |
| 623 | */ |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 624 | 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] | 625 | struct map *map) |
Arnaldo Carvalho de Melo | 682b335 | 2010-01-04 16:19:26 -0200 | [diff] [blame] | 626 | { |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 627 | struct process_kallsyms_args args = { .map = map, .dso = dso, }; |
Arnaldo Carvalho de Melo | 9e20144 | 2010-01-14 18:30:06 -0200 | [diff] [blame] | 628 | return kallsyms__parse(filename, &args, map__process_kallsym_symbol); |
Arnaldo Carvalho de Melo | 682b335 | 2010-01-04 16:19:26 -0200 | [diff] [blame] | 629 | } |
| 630 | |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame^] | 631 | static int dso__split_kallsyms_for_kcore(struct dso *dso, struct map *map) |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 632 | { |
Wang Nan | ba92732 | 2015-04-07 08:22:45 +0000 | [diff] [blame] | 633 | struct map_groups *kmaps = map__kmaps(map); |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 634 | struct map *curr_map; |
| 635 | struct symbol *pos; |
Adrian Hunter | 866548d | 2015-11-06 15:59:29 +0200 | [diff] [blame] | 636 | int count = 0; |
| 637 | struct rb_root old_root = dso->symbols[map->type]; |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 638 | struct rb_root *root = &dso->symbols[map->type]; |
| 639 | struct rb_node *next = rb_first(root); |
| 640 | |
Wang Nan | ba92732 | 2015-04-07 08:22:45 +0000 | [diff] [blame] | 641 | if (!kmaps) |
| 642 | return -1; |
| 643 | |
Adrian Hunter | 866548d | 2015-11-06 15:59:29 +0200 | [diff] [blame] | 644 | *root = RB_ROOT; |
| 645 | |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 646 | while (next) { |
| 647 | char *module; |
| 648 | |
| 649 | pos = rb_entry(next, struct symbol, rb_node); |
| 650 | next = rb_next(&pos->rb_node); |
| 651 | |
Adrian Hunter | 866548d | 2015-11-06 15:59:29 +0200 | [diff] [blame] | 652 | rb_erase_init(&pos->rb_node, &old_root); |
| 653 | |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 654 | module = strchr(pos->name, '\t'); |
| 655 | if (module) |
| 656 | *module = '\0'; |
| 657 | |
| 658 | curr_map = map_groups__find(kmaps, map->type, pos->start); |
| 659 | |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame^] | 660 | if (!curr_map) { |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 661 | symbol__delete(pos); |
Adrian Hunter | 866548d | 2015-11-06 15:59:29 +0200 | [diff] [blame] | 662 | continue; |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 663 | } |
Adrian Hunter | 866548d | 2015-11-06 15:59:29 +0200 | [diff] [blame] | 664 | |
| 665 | pos->start -= curr_map->start - curr_map->pgoff; |
| 666 | if (pos->end) |
| 667 | pos->end -= curr_map->start - curr_map->pgoff; |
| 668 | symbols__insert(&curr_map->dso->symbols[curr_map->type], pos); |
| 669 | ++count; |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | /* Symbols have been adjusted */ |
| 673 | dso->adjust_symbols = 1; |
| 674 | |
Adrian Hunter | 866548d | 2015-11-06 15:59:29 +0200 | [diff] [blame] | 675 | return count; |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 676 | } |
| 677 | |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 678 | /* |
| 679 | * Split the symbols into maps, making sure there are no overlaps, i.e. the |
| 680 | * kernel range is broken in several maps, named [kernel].N, as we don't have |
| 681 | * the original ELF section names vmlinux have. |
| 682 | */ |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame^] | 683 | static int dso__split_kallsyms(struct dso *dso, struct map *map, u64 delta) |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 684 | { |
Wang Nan | ba92732 | 2015-04-07 08:22:45 +0000 | [diff] [blame] | 685 | struct map_groups *kmaps = map__kmaps(map); |
| 686 | struct machine *machine; |
Arnaldo Carvalho de Melo | 4e06255 | 2009-11-27 16:29:18 -0200 | [diff] [blame] | 687 | struct map *curr_map = map; |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 688 | struct symbol *pos; |
Arnaldo Carvalho de Melo | 48000a1 | 2014-12-17 17:24:45 -0300 | [diff] [blame] | 689 | int count = 0, moved = 0; |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 690 | struct rb_root *root = &dso->symbols[map->type]; |
Arnaldo Carvalho de Melo | 4e06255 | 2009-11-27 16:29:18 -0200 | [diff] [blame] | 691 | struct rb_node *next = rb_first(root); |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 692 | int kernel_range = 0; |
| 693 | |
Wang Nan | ba92732 | 2015-04-07 08:22:45 +0000 | [diff] [blame] | 694 | if (!kmaps) |
| 695 | return -1; |
| 696 | |
| 697 | machine = kmaps->machine; |
| 698 | |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 699 | while (next) { |
| 700 | char *module; |
| 701 | |
| 702 | pos = rb_entry(next, struct symbol, rb_node); |
| 703 | next = rb_next(&pos->rb_node); |
| 704 | |
| 705 | module = strchr(pos->name, '\t'); |
| 706 | if (module) { |
Arnaldo Carvalho de Melo | 75be6cf | 2009-12-15 20:04:39 -0200 | [diff] [blame] | 707 | if (!symbol_conf.use_modules) |
Arnaldo Carvalho de Melo | 1de8e24 | 2009-11-27 16:29:21 -0200 | [diff] [blame] | 708 | goto discard_symbol; |
| 709 | |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 710 | *module++ = '\0'; |
| 711 | |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 712 | if (strcmp(curr_map->dso->short_name, module)) { |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 713 | if (curr_map != map && |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 714 | dso->kernel == DSO_TYPE_GUEST_KERNEL && |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 715 | machine__is_default_guest(machine)) { |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 716 | /* |
| 717 | * We assume all symbols of a module are |
| 718 | * continuous in * kallsyms, so curr_map |
| 719 | * points to a module and all its |
| 720 | * symbols are in its kmap. Mark it as |
| 721 | * loaded. |
| 722 | */ |
| 723 | dso__set_loaded(curr_map->dso, |
| 724 | curr_map->type); |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 725 | } |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 726 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 727 | curr_map = map_groups__find_by_name(kmaps, |
| 728 | map->type, module); |
| 729 | if (curr_map == NULL) { |
Arnaldo Carvalho de Melo | 2f51903 | 2010-05-17 17:57:59 -0300 | [diff] [blame] | 730 | pr_debug("%s/proc/{kallsyms,modules} " |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 731 | "inconsistency while looking " |
| 732 | "for \"%s\" module!\n", |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 733 | machine->root_dir, module); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 734 | curr_map = map; |
| 735 | goto discard_symbol; |
| 736 | } |
| 737 | |
| 738 | if (curr_map->dso->loaded && |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 739 | !machine__is_default_guest(machine)) |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 740 | goto discard_symbol; |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 741 | } |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 742 | /* |
| 743 | * So that we look just like we get from .ko files, |
| 744 | * i.e. not prelinked, relative to map->start. |
| 745 | */ |
Arnaldo Carvalho de Melo | 4e06255 | 2009-11-27 16:29:18 -0200 | [diff] [blame] | 746 | pos->start = curr_map->map_ip(curr_map, pos->start); |
| 747 | pos->end = curr_map->map_ip(curr_map, pos->end); |
| 748 | } else if (curr_map != map) { |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 749 | char dso_name[PATH_MAX]; |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 750 | struct dso *ndso; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 751 | |
Adrian Hunter | d9b62ab | 2014-01-29 16:14:43 +0200 | [diff] [blame] | 752 | if (delta) { |
| 753 | /* Kernel was relocated at boot time */ |
| 754 | pos->start -= delta; |
| 755 | pos->end -= delta; |
| 756 | } |
| 757 | |
Arnaldo Carvalho de Melo | 8a95331 | 2010-11-29 12:44:15 -0200 | [diff] [blame] | 758 | if (count == 0) { |
| 759 | curr_map = map; |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame^] | 760 | goto add_symbol; |
Arnaldo Carvalho de Melo | 8a95331 | 2010-11-29 12:44:15 -0200 | [diff] [blame] | 761 | } |
| 762 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 763 | if (dso->kernel == DSO_TYPE_GUEST_KERNEL) |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 764 | snprintf(dso_name, sizeof(dso_name), |
| 765 | "[guest.kernel].%d", |
| 766 | kernel_range++); |
| 767 | else |
| 768 | snprintf(dso_name, sizeof(dso_name), |
| 769 | "[kernel].%d", |
| 770 | kernel_range++); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 771 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 772 | ndso = dso__new(dso_name); |
| 773 | if (ndso == NULL) |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 774 | return -1; |
| 775 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 776 | ndso->kernel = dso->kernel; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 777 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 778 | curr_map = map__new2(pos->start, ndso, map->type); |
Zhang, Yanmin | 37fe5fc | 2010-02-25 11:00:51 +0800 | [diff] [blame] | 779 | if (curr_map == NULL) { |
Arnaldo Carvalho de Melo | d3a7c48 | 2015-06-02 11:53:26 -0300 | [diff] [blame] | 780 | dso__put(ndso); |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 781 | return -1; |
| 782 | } |
| 783 | |
Arnaldo Carvalho de Melo | 4e06255 | 2009-11-27 16:29:18 -0200 | [diff] [blame] | 784 | 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] | 785 | map_groups__insert(kmaps, curr_map); |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 786 | ++kernel_range; |
Adrian Hunter | d9b62ab | 2014-01-29 16:14:43 +0200 | [diff] [blame] | 787 | } else if (delta) { |
| 788 | /* Kernel was relocated at boot time */ |
| 789 | pos->start -= delta; |
| 790 | pos->end -= delta; |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 791 | } |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame^] | 792 | add_symbol: |
| 793 | if (curr_map != map) { |
| 794 | rb_erase(&pos->rb_node, root); |
| 795 | symbols__insert(&curr_map->dso->symbols[curr_map->type], pos); |
| 796 | ++moved; |
| 797 | } else |
| 798 | ++count; |
| 799 | |
| 800 | continue; |
| 801 | discard_symbol: |
| 802 | rb_erase(&pos->rb_node, root); |
| 803 | symbol__delete(pos); |
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); |
Arnaldo Carvalho de Melo | 74cf249 | 2013-12-27 16:55:14 -0300 | [diff] [blame] | 866 | zfree(&mi->name); |
Adrian Hunter | 52afdaf | 2013-10-09 15:01:11 +0300 | [diff] [blame] | 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 | { |
Wang Nan | ba92732 | 2015-04-07 08:22:45 +0000 | [diff] [blame] | 1033 | struct map_groups *kmaps = map__kmaps(map); |
Adrian Hunter | 52afdaf | 2013-10-09 15:01:11 +0300 | [diff] [blame] | 1034 | char modules_filename[PATH_MAX]; |
| 1035 | |
Wang Nan | ba92732 | 2015-04-07 08:22:45 +0000 | [diff] [blame] | 1036 | if (!kmaps) |
| 1037 | return -EINVAL; |
| 1038 | |
Adrian Hunter | 52afdaf | 2013-10-09 15:01:11 +0300 | [diff] [blame] | 1039 | if (!filename_from_kallsyms_filename(modules_filename, "modules", |
| 1040 | kallsyms_filename)) |
| 1041 | return -EINVAL; |
| 1042 | |
| 1043 | if (do_validate_kcore_modules(modules_filename, map, kmaps)) |
| 1044 | return -EINVAL; |
| 1045 | |
| 1046 | return 0; |
| 1047 | } |
| 1048 | |
Adrian Hunter | a00d28c | 2014-01-29 16:14:41 +0200 | [diff] [blame] | 1049 | static int validate_kcore_addresses(const char *kallsyms_filename, |
| 1050 | struct map *map) |
| 1051 | { |
| 1052 | struct kmap *kmap = map__kmap(map); |
| 1053 | |
Wang Nan | ba92732 | 2015-04-07 08:22:45 +0000 | [diff] [blame] | 1054 | if (!kmap) |
| 1055 | return -EINVAL; |
| 1056 | |
Adrian Hunter | a00d28c | 2014-01-29 16:14:41 +0200 | [diff] [blame] | 1057 | if (kmap->ref_reloc_sym && kmap->ref_reloc_sym->name) { |
| 1058 | u64 start; |
| 1059 | |
| 1060 | start = kallsyms__get_function_start(kallsyms_filename, |
| 1061 | kmap->ref_reloc_sym->name); |
| 1062 | if (start != kmap->ref_reloc_sym->addr) |
| 1063 | return -EINVAL; |
| 1064 | } |
| 1065 | |
| 1066 | return validate_kcore_modules(kallsyms_filename, map); |
| 1067 | } |
| 1068 | |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 1069 | struct kcore_mapfn_data { |
| 1070 | struct dso *dso; |
| 1071 | enum map_type type; |
| 1072 | struct list_head maps; |
| 1073 | }; |
| 1074 | |
| 1075 | static int kcore_mapfn(u64 start, u64 len, u64 pgoff, void *data) |
| 1076 | { |
| 1077 | struct kcore_mapfn_data *md = data; |
| 1078 | struct map *map; |
| 1079 | |
| 1080 | map = map__new2(start, md->dso, md->type); |
| 1081 | if (map == NULL) |
| 1082 | return -ENOMEM; |
| 1083 | |
| 1084 | map->end = map->start + len; |
| 1085 | map->pgoff = pgoff; |
| 1086 | |
| 1087 | list_add(&map->node, &md->maps); |
| 1088 | |
| 1089 | return 0; |
| 1090 | } |
| 1091 | |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 1092 | static int dso__load_kcore(struct dso *dso, struct map *map, |
| 1093 | const char *kallsyms_filename) |
| 1094 | { |
Wang Nan | ba92732 | 2015-04-07 08:22:45 +0000 | [diff] [blame] | 1095 | struct map_groups *kmaps = map__kmaps(map); |
| 1096 | struct machine *machine; |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 1097 | struct kcore_mapfn_data md; |
| 1098 | struct map *old_map, *new_map, *replacement_map = NULL; |
| 1099 | bool is_64_bit; |
| 1100 | int err, fd; |
| 1101 | char kcore_filename[PATH_MAX]; |
| 1102 | struct symbol *sym; |
| 1103 | |
Wang Nan | ba92732 | 2015-04-07 08:22:45 +0000 | [diff] [blame] | 1104 | if (!kmaps) |
| 1105 | return -EINVAL; |
| 1106 | |
| 1107 | machine = kmaps->machine; |
| 1108 | |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 1109 | /* This function requires that the map is the kernel map */ |
| 1110 | if (map != machine->vmlinux_maps[map->type]) |
| 1111 | return -EINVAL; |
| 1112 | |
Adrian Hunter | 52afdaf | 2013-10-09 15:01:11 +0300 | [diff] [blame] | 1113 | if (!filename_from_kallsyms_filename(kcore_filename, "kcore", |
| 1114 | kallsyms_filename)) |
| 1115 | return -EINVAL; |
| 1116 | |
Adrian Hunter | a00d28c | 2014-01-29 16:14:41 +0200 | [diff] [blame] | 1117 | /* Modules and kernel must be present at their original addresses */ |
| 1118 | if (validate_kcore_addresses(kallsyms_filename, map)) |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 1119 | return -EINVAL; |
| 1120 | |
| 1121 | md.dso = dso; |
| 1122 | md.type = map->type; |
| 1123 | INIT_LIST_HEAD(&md.maps); |
| 1124 | |
| 1125 | fd = open(kcore_filename, O_RDONLY); |
Li Zhang | 36c8bb5 | 2015-06-19 16:57:33 +0800 | [diff] [blame] | 1126 | if (fd < 0) { |
Adrian Hunter | 133de94 | 2015-08-20 13:07:40 +0300 | [diff] [blame] | 1127 | pr_debug("Failed to open %s. Note /proc/kcore requires CAP_SYS_RAWIO capability to access.\n", |
| 1128 | kcore_filename); |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 1129 | return -EINVAL; |
Li Zhang | 36c8bb5 | 2015-06-19 16:57:33 +0800 | [diff] [blame] | 1130 | } |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 1131 | |
| 1132 | /* Read new maps into temporary lists */ |
| 1133 | err = file__read_maps(fd, md.type == MAP__FUNCTION, kcore_mapfn, &md, |
| 1134 | &is_64_bit); |
| 1135 | if (err) |
| 1136 | goto out_err; |
Adrian Hunter | c6d8f2a | 2014-07-14 13:02:41 +0300 | [diff] [blame] | 1137 | dso->is_64_bit = is_64_bit; |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 1138 | |
| 1139 | if (list_empty(&md.maps)) { |
| 1140 | err = -EINVAL; |
| 1141 | goto out_err; |
| 1142 | } |
| 1143 | |
| 1144 | /* Remove old maps */ |
| 1145 | old_map = map_groups__first(kmaps, map->type); |
| 1146 | while (old_map) { |
| 1147 | struct map *next = map_groups__next(old_map); |
| 1148 | |
| 1149 | if (old_map != map) |
| 1150 | map_groups__remove(kmaps, old_map); |
| 1151 | old_map = next; |
| 1152 | } |
| 1153 | |
| 1154 | /* Find the kernel map using the first symbol */ |
| 1155 | sym = dso__first_symbol(dso, map->type); |
| 1156 | list_for_each_entry(new_map, &md.maps, node) { |
| 1157 | if (sym && sym->start >= new_map->start && |
| 1158 | sym->start < new_map->end) { |
| 1159 | replacement_map = new_map; |
| 1160 | break; |
| 1161 | } |
| 1162 | } |
| 1163 | |
| 1164 | if (!replacement_map) |
| 1165 | replacement_map = list_entry(md.maps.next, struct map, node); |
| 1166 | |
| 1167 | /* Add new maps */ |
| 1168 | while (!list_empty(&md.maps)) { |
| 1169 | new_map = list_entry(md.maps.next, struct map, node); |
Arnaldo Carvalho de Melo | facf3f0 | 2015-05-25 15:30:09 -0300 | [diff] [blame] | 1170 | list_del_init(&new_map->node); |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 1171 | if (new_map == replacement_map) { |
| 1172 | map->start = new_map->start; |
| 1173 | map->end = new_map->end; |
| 1174 | map->pgoff = new_map->pgoff; |
| 1175 | map->map_ip = new_map->map_ip; |
| 1176 | map->unmap_ip = new_map->unmap_ip; |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 1177 | /* Ensure maps are correctly ordered */ |
Arnaldo Carvalho de Melo | 84c2caf | 2015-05-25 16:59:56 -0300 | [diff] [blame] | 1178 | map__get(map); |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 1179 | map_groups__remove(kmaps, map); |
| 1180 | map_groups__insert(kmaps, map); |
Arnaldo Carvalho de Melo | 84c2caf | 2015-05-25 16:59:56 -0300 | [diff] [blame] | 1181 | map__put(map); |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 1182 | } else { |
| 1183 | map_groups__insert(kmaps, new_map); |
| 1184 | } |
Arnaldo Carvalho de Melo | 84c2caf | 2015-05-25 16:59:56 -0300 | [diff] [blame] | 1185 | |
| 1186 | map__put(new_map); |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 1187 | } |
| 1188 | |
| 1189 | /* |
| 1190 | * Set the data type and long name so that kcore can be read via |
| 1191 | * dso__data_read_addr(). |
| 1192 | */ |
| 1193 | if (dso->kernel == DSO_TYPE_GUEST_KERNEL) |
Arnaldo Carvalho de Melo | 5f70619 | 2013-12-17 16:14:07 -0300 | [diff] [blame] | 1194 | dso->binary_type = DSO_BINARY_TYPE__GUEST_KCORE; |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 1195 | else |
Arnaldo Carvalho de Melo | 5f70619 | 2013-12-17 16:14:07 -0300 | [diff] [blame] | 1196 | dso->binary_type = DSO_BINARY_TYPE__KCORE; |
Arnaldo Carvalho de Melo | 7e155d4 | 2013-12-10 15:08:44 -0300 | [diff] [blame] | 1197 | dso__set_long_name(dso, strdup(kcore_filename), true); |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 1198 | |
| 1199 | close(fd); |
| 1200 | |
| 1201 | if (map->type == MAP__FUNCTION) |
| 1202 | pr_debug("Using %s for kernel object code\n", kcore_filename); |
| 1203 | else |
| 1204 | pr_debug("Using %s for kernel data\n", kcore_filename); |
| 1205 | |
| 1206 | return 0; |
| 1207 | |
| 1208 | out_err: |
| 1209 | while (!list_empty(&md.maps)) { |
| 1210 | map = list_entry(md.maps.next, struct map, node); |
Arnaldo Carvalho de Melo | facf3f0 | 2015-05-25 15:30:09 -0300 | [diff] [blame] | 1211 | list_del_init(&map->node); |
Arnaldo Carvalho de Melo | 84c2caf | 2015-05-25 16:59:56 -0300 | [diff] [blame] | 1212 | map__put(map); |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 1213 | } |
| 1214 | close(fd); |
| 1215 | return -EINVAL; |
| 1216 | } |
| 1217 | |
Adrian Hunter | d9b62ab | 2014-01-29 16:14:43 +0200 | [diff] [blame] | 1218 | /* |
| 1219 | * If the kernel is relocated at boot time, kallsyms won't match. Compute the |
| 1220 | * delta based on the relocation reference symbol. |
| 1221 | */ |
| 1222 | static int kallsyms__delta(struct map *map, const char *filename, u64 *delta) |
| 1223 | { |
| 1224 | struct kmap *kmap = map__kmap(map); |
| 1225 | u64 addr; |
| 1226 | |
Wang Nan | ba92732 | 2015-04-07 08:22:45 +0000 | [diff] [blame] | 1227 | if (!kmap) |
| 1228 | return -1; |
| 1229 | |
Adrian Hunter | d9b62ab | 2014-01-29 16:14:43 +0200 | [diff] [blame] | 1230 | if (!kmap->ref_reloc_sym || !kmap->ref_reloc_sym->name) |
| 1231 | return 0; |
| 1232 | |
| 1233 | addr = kallsyms__get_function_start(filename, |
| 1234 | kmap->ref_reloc_sym->name); |
| 1235 | if (!addr) |
| 1236 | return -1; |
| 1237 | |
| 1238 | *delta = addr - kmap->ref_reloc_sym->addr; |
| 1239 | return 0; |
| 1240 | } |
| 1241 | |
Arnaldo Carvalho de Melo | e02092b | 2016-04-19 12:12:49 -0300 | [diff] [blame] | 1242 | int __dso__load_kallsyms(struct dso *dso, const char *filename, |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame^] | 1243 | struct map *map, bool no_kcore) |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 1244 | { |
Adrian Hunter | d9b62ab | 2014-01-29 16:14:43 +0200 | [diff] [blame] | 1245 | u64 delta = 0; |
| 1246 | |
Arnaldo Carvalho de Melo | ec80fde | 2011-05-26 09:53:51 -0300 | [diff] [blame] | 1247 | if (symbol__restricted_filename(filename, "/proc/kallsyms")) |
| 1248 | return -1; |
| 1249 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 1250 | if (dso__load_all_kallsyms(dso, filename, map) < 0) |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 1251 | return -1; |
| 1252 | |
Adrian Hunter | d9b62ab | 2014-01-29 16:14:43 +0200 | [diff] [blame] | 1253 | if (kallsyms__delta(map, filename, &delta)) |
| 1254 | return -1; |
| 1255 | |
Anton Blanchard | 3f5a427 | 2011-08-24 16:40:15 +1000 | [diff] [blame] | 1256 | symbols__fixup_end(&dso->symbols[map->type]); |
Arnaldo Carvalho de Melo | 432746f | 2016-09-01 11:00:23 -0300 | [diff] [blame] | 1257 | symbols__fixup_duplicate(&dso->symbols[map->type]); |
Anton Blanchard | 3f5a427 | 2011-08-24 16:40:15 +1000 | [diff] [blame] | 1258 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 1259 | if (dso->kernel == DSO_TYPE_GUEST_KERNEL) |
Jiri Olsa | 44f24cb | 2012-07-22 14:14:32 +0200 | [diff] [blame] | 1260 | dso->symtab_type = DSO_BINARY_TYPE__GUEST_KALLSYMS; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1261 | else |
Jiri Olsa | 44f24cb | 2012-07-22 14:14:32 +0200 | [diff] [blame] | 1262 | dso->symtab_type = DSO_BINARY_TYPE__KALLSYMS; |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 1263 | |
Arnaldo Carvalho de Melo | e02092b | 2016-04-19 12:12:49 -0300 | [diff] [blame] | 1264 | if (!no_kcore && !dso__load_kcore(dso, map, filename)) |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame^] | 1265 | return dso__split_kallsyms_for_kcore(dso, map); |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 1266 | else |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame^] | 1267 | return dso__split_kallsyms(dso, map, delta); |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 1268 | } |
| 1269 | |
Arnaldo Carvalho de Melo | e02092b | 2016-04-19 12:12:49 -0300 | [diff] [blame] | 1270 | int dso__load_kallsyms(struct dso *dso, const char *filename, |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame^] | 1271 | struct map *map) |
Arnaldo Carvalho de Melo | e02092b | 2016-04-19 12:12:49 -0300 | [diff] [blame] | 1272 | { |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame^] | 1273 | return __dso__load_kallsyms(dso, filename, map, false); |
Arnaldo Carvalho de Melo | e02092b | 2016-04-19 12:12:49 -0300 | [diff] [blame] | 1274 | } |
| 1275 | |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame^] | 1276 | static int dso__load_perf_map(struct dso *dso, struct map *map) |
Pekka Enberg | 80d496b | 2009-06-08 21:12:48 +0300 | [diff] [blame] | 1277 | { |
| 1278 | char *line = NULL; |
| 1279 | size_t n; |
| 1280 | FILE *file; |
| 1281 | int nr_syms = 0; |
| 1282 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 1283 | file = fopen(dso->long_name, "r"); |
Pekka Enberg | 80d496b | 2009-06-08 21:12:48 +0300 | [diff] [blame] | 1284 | if (file == NULL) |
| 1285 | goto out_failure; |
| 1286 | |
| 1287 | while (!feof(file)) { |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 1288 | u64 start, size; |
Pekka Enberg | 80d496b | 2009-06-08 21:12:48 +0300 | [diff] [blame] | 1289 | struct symbol *sym; |
| 1290 | int line_len, len; |
| 1291 | |
| 1292 | line_len = getline(&line, &n, file); |
| 1293 | if (line_len < 0) |
| 1294 | break; |
| 1295 | |
| 1296 | if (!line) |
| 1297 | goto out_failure; |
| 1298 | |
| 1299 | line[--line_len] = '\0'; /* \n */ |
| 1300 | |
| 1301 | len = hex2u64(line, &start); |
| 1302 | |
| 1303 | len++; |
| 1304 | if (len + 2 >= line_len) |
| 1305 | continue; |
| 1306 | |
| 1307 | len += hex2u64(line + len, &size); |
| 1308 | |
| 1309 | len++; |
| 1310 | if (len + 2 >= line_len) |
| 1311 | continue; |
| 1312 | |
Arnaldo Carvalho de Melo | c408fed | 2010-08-05 12:59:47 -0300 | [diff] [blame] | 1313 | sym = symbol__new(start, size, STB_GLOBAL, line + len); |
Pekka Enberg | 80d496b | 2009-06-08 21:12:48 +0300 | [diff] [blame] | 1314 | |
| 1315 | if (sym == NULL) |
| 1316 | goto out_delete_line; |
| 1317 | |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame^] | 1318 | symbols__insert(&dso->symbols[map->type], sym); |
| 1319 | nr_syms++; |
Pekka Enberg | 80d496b | 2009-06-08 21:12:48 +0300 | [diff] [blame] | 1320 | } |
| 1321 | |
| 1322 | free(line); |
| 1323 | fclose(file); |
| 1324 | |
| 1325 | return nr_syms; |
| 1326 | |
| 1327 | out_delete_line: |
| 1328 | free(line); |
| 1329 | out_failure: |
| 1330 | return -1; |
| 1331 | } |
| 1332 | |
Namhyung Kim | 1029f9f | 2014-02-20 10:32:56 +0900 | [diff] [blame] | 1333 | static bool dso__is_compatible_symtab_type(struct dso *dso, bool kmod, |
| 1334 | enum dso_binary_type type) |
| 1335 | { |
| 1336 | switch (type) { |
| 1337 | case DSO_BINARY_TYPE__JAVA_JIT: |
| 1338 | case DSO_BINARY_TYPE__DEBUGLINK: |
| 1339 | case DSO_BINARY_TYPE__SYSTEM_PATH_DSO: |
| 1340 | case DSO_BINARY_TYPE__FEDORA_DEBUGINFO: |
| 1341 | case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO: |
| 1342 | case DSO_BINARY_TYPE__BUILDID_DEBUGINFO: |
| 1343 | case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO: |
| 1344 | return !kmod && dso->kernel == DSO_TYPE_USER; |
| 1345 | |
| 1346 | case DSO_BINARY_TYPE__KALLSYMS: |
| 1347 | case DSO_BINARY_TYPE__VMLINUX: |
| 1348 | case DSO_BINARY_TYPE__KCORE: |
| 1349 | return dso->kernel == DSO_TYPE_KERNEL; |
| 1350 | |
| 1351 | case DSO_BINARY_TYPE__GUEST_KALLSYMS: |
| 1352 | case DSO_BINARY_TYPE__GUEST_VMLINUX: |
| 1353 | case DSO_BINARY_TYPE__GUEST_KCORE: |
| 1354 | return dso->kernel == DSO_TYPE_GUEST_KERNEL; |
| 1355 | |
| 1356 | case DSO_BINARY_TYPE__GUEST_KMODULE: |
Namhyung Kim | c00c48f | 2014-11-04 10:14:27 +0900 | [diff] [blame] | 1357 | case DSO_BINARY_TYPE__GUEST_KMODULE_COMP: |
Namhyung Kim | 1029f9f | 2014-02-20 10:32:56 +0900 | [diff] [blame] | 1358 | case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE: |
Namhyung Kim | c00c48f | 2014-11-04 10:14:27 +0900 | [diff] [blame] | 1359 | case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP: |
Namhyung Kim | 1029f9f | 2014-02-20 10:32:56 +0900 | [diff] [blame] | 1360 | /* |
| 1361 | * kernel modules know their symtab type - it's set when |
Arnaldo Carvalho de Melo | 9f2de31 | 2015-06-01 12:01:02 -0300 | [diff] [blame] | 1362 | * creating a module dso in machine__findnew_module_map(). |
Namhyung Kim | 1029f9f | 2014-02-20 10:32:56 +0900 | [diff] [blame] | 1363 | */ |
| 1364 | return kmod && dso->symtab_type == type; |
| 1365 | |
| 1366 | case DSO_BINARY_TYPE__BUILD_ID_CACHE: |
| 1367 | return true; |
| 1368 | |
| 1369 | case DSO_BINARY_TYPE__NOT_FOUND: |
| 1370 | default: |
| 1371 | return false; |
| 1372 | } |
| 1373 | } |
| 1374 | |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame^] | 1375 | int dso__load(struct dso *dso, struct map *map) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1376 | { |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 1377 | char *name; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1378 | int ret = -1; |
Jiri Olsa | 44f24cb | 2012-07-22 14:14:32 +0200 | [diff] [blame] | 1379 | u_int i; |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 1380 | struct machine *machine; |
Jiri Olsa | 44f24cb | 2012-07-22 14:14:32 +0200 | [diff] [blame] | 1381 | char *root_dir = (char *) ""; |
Cody P Schafer | 3aafe5a | 2012-08-10 15:23:02 -0700 | [diff] [blame] | 1382 | int ss_pos = 0; |
| 1383 | struct symsrc ss_[2]; |
| 1384 | struct symsrc *syms_ss = NULL, *runtime_ss = NULL; |
Namhyung Kim | 1029f9f | 2014-02-20 10:32:56 +0900 | [diff] [blame] | 1385 | bool kmod; |
Dima Kogan | 5baecbc | 2015-09-07 17:34:19 -0700 | [diff] [blame] | 1386 | unsigned char build_id[BUILD_ID_SIZE]; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1387 | |
Namhyung Kim | 4a936ed | 2015-05-18 09:30:40 +0900 | [diff] [blame] | 1388 | pthread_mutex_lock(&dso->lock); |
Arnaldo Carvalho de Melo | 66bd842 | 2009-10-28 21:51:21 -0200 | [diff] [blame] | 1389 | |
Namhyung Kim | 4a936ed | 2015-05-18 09:30:40 +0900 | [diff] [blame] | 1390 | /* check again under the dso->lock */ |
| 1391 | if (dso__loaded(dso, map->type)) { |
| 1392 | ret = 1; |
| 1393 | goto out; |
| 1394 | } |
| 1395 | |
| 1396 | if (dso->kernel) { |
| 1397 | if (dso->kernel == DSO_TYPE_KERNEL) |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame^] | 1398 | ret = dso__load_kernel_sym(dso, map); |
Namhyung Kim | 4a936ed | 2015-05-18 09:30:40 +0900 | [diff] [blame] | 1399 | else if (dso->kernel == DSO_TYPE_GUEST_KERNEL) |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame^] | 1400 | ret = dso__load_guest_kernel_sym(dso, map); |
Namhyung Kim | 4a936ed | 2015-05-18 09:30:40 +0900 | [diff] [blame] | 1401 | |
| 1402 | goto out; |
| 1403 | } |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1404 | |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 1405 | if (map->groups && map->groups->machine) |
| 1406 | machine = map->groups->machine; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1407 | else |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 1408 | machine = NULL; |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 1409 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 1410 | dso->adjust_symbols = 0; |
Arnaldo Carvalho de Melo | f5812a7 | 2009-06-30 11:43:17 -0300 | [diff] [blame] | 1411 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 1412 | if (strncmp(dso->name, "/tmp/perf-", 10) == 0) { |
Pekka Enberg | 981c125 | 2011-08-09 22:54:18 +0300 | [diff] [blame] | 1413 | struct stat st; |
| 1414 | |
Vasiliy Kulikov | e9b52ef | 2011-08-12 00:55:37 +0400 | [diff] [blame] | 1415 | if (lstat(dso->name, &st) < 0) |
Namhyung Kim | 4a936ed | 2015-05-18 09:30:40 +0900 | [diff] [blame] | 1416 | goto out; |
Pekka Enberg | 981c125 | 2011-08-09 22:54:18 +0300 | [diff] [blame] | 1417 | |
Arnaldo Carvalho de Melo | 2059fc7 | 2015-11-12 16:50:13 -0300 | [diff] [blame] | 1418 | if (!symbol_conf.force && st.st_uid && (st.st_uid != geteuid())) { |
Pekka Enberg | 981c125 | 2011-08-09 22:54:18 +0300 | [diff] [blame] | 1419 | pr_warning("File %s not owned by current user or root, " |
Arnaldo Carvalho de Melo | 2059fc7 | 2015-11-12 16:50:13 -0300 | [diff] [blame] | 1420 | "ignoring it (use -f to override).\n", dso->name); |
Namhyung Kim | 4a936ed | 2015-05-18 09:30:40 +0900 | [diff] [blame] | 1421 | goto out; |
Pekka Enberg | 981c125 | 2011-08-09 22:54:18 +0300 | [diff] [blame] | 1422 | } |
| 1423 | |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame^] | 1424 | ret = dso__load_perf_map(dso, map); |
Jiri Olsa | 44f24cb | 2012-07-22 14:14:32 +0200 | [diff] [blame] | 1425 | dso->symtab_type = ret > 0 ? DSO_BINARY_TYPE__JAVA_JIT : |
| 1426 | DSO_BINARY_TYPE__NOT_FOUND; |
Namhyung Kim | 4a936ed | 2015-05-18 09:30:40 +0900 | [diff] [blame] | 1427 | goto out; |
Arnaldo Carvalho de Melo | 94cb9e3 | 2009-08-06 14:43:17 -0300 | [diff] [blame] | 1428 | } |
| 1429 | |
Jiri Olsa | 44f24cb | 2012-07-22 14:14:32 +0200 | [diff] [blame] | 1430 | if (machine) |
| 1431 | root_dir = machine->root_dir; |
| 1432 | |
David Ahern | 164c800 | 2013-01-14 10:46:47 -0700 | [diff] [blame] | 1433 | name = malloc(PATH_MAX); |
| 1434 | if (!name) |
Namhyung Kim | 4a936ed | 2015-05-18 09:30:40 +0900 | [diff] [blame] | 1435 | goto out; |
David Ahern | 164c800 | 2013-01-14 10:46:47 -0700 | [diff] [blame] | 1436 | |
Namhyung Kim | 1029f9f | 2014-02-20 10:32:56 +0900 | [diff] [blame] | 1437 | kmod = dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE || |
Namhyung Kim | c00c48f | 2014-11-04 10:14:27 +0900 | [diff] [blame] | 1438 | dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP || |
| 1439 | dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE || |
| 1440 | dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP; |
Namhyung Kim | 1029f9f | 2014-02-20 10:32:56 +0900 | [diff] [blame] | 1441 | |
Dima Kogan | 5baecbc | 2015-09-07 17:34:19 -0700 | [diff] [blame] | 1442 | |
| 1443 | /* |
| 1444 | * Read the build id if possible. This is required for |
| 1445 | * DSO_BINARY_TYPE__BUILDID_DEBUGINFO to work |
| 1446 | */ |
Jiri Olsa | ed7b630 | 2016-06-24 14:40:25 +0200 | [diff] [blame] | 1447 | if (is_regular_file(dso->long_name) && |
Jiri Olsa | 4035672 | 2016-01-20 12:56:32 +0100 | [diff] [blame] | 1448 | filename__read_build_id(dso->long_name, build_id, BUILD_ID_SIZE) > 0) |
Dima Kogan | 5baecbc | 2015-09-07 17:34:19 -0700 | [diff] [blame] | 1449 | dso__set_build_id(dso, build_id); |
| 1450 | |
Namhyung Kim | 1029f9f | 2014-02-20 10:32:56 +0900 | [diff] [blame] | 1451 | /* |
| 1452 | * Iterate over candidate debug images. |
Cody P Schafer | 3aafe5a | 2012-08-10 15:23:02 -0700 | [diff] [blame] | 1453 | * Keep track of "interesting" ones (those which have a symtab, dynsym, |
| 1454 | * and/or opd section) for processing. |
Dave Martin | 6da80ce | 2010-07-30 09:50:09 -0300 | [diff] [blame] | 1455 | */ |
Jiri Olsa | 44f24cb | 2012-07-22 14:14:32 +0200 | [diff] [blame] | 1456 | for (i = 0; i < DSO_BINARY_TYPE__SYMTAB_CNT; i++) { |
Cody P Schafer | 3aafe5a | 2012-08-10 15:23:02 -0700 | [diff] [blame] | 1457 | struct symsrc *ss = &ss_[ss_pos]; |
| 1458 | bool next_slot = false; |
Dave Martin | 6da80ce | 2010-07-30 09:50:09 -0300 | [diff] [blame] | 1459 | |
Cody P Schafer | 005f929 | 2012-08-10 15:22:58 -0700 | [diff] [blame] | 1460 | enum dso_binary_type symtab_type = binary_type_symtab[i]; |
Dave Martin | 6da80ce | 2010-07-30 09:50:09 -0300 | [diff] [blame] | 1461 | |
Namhyung Kim | 1029f9f | 2014-02-20 10:32:56 +0900 | [diff] [blame] | 1462 | if (!dso__is_compatible_symtab_type(dso, kmod, symtab_type)) |
| 1463 | continue; |
| 1464 | |
Arnaldo Carvalho de Melo | ee4e962 | 2013-12-16 17:03:18 -0300 | [diff] [blame] | 1465 | if (dso__read_binary_type_filename(dso, symtab_type, |
| 1466 | root_dir, name, PATH_MAX)) |
Jiri Olsa | 44f24cb | 2012-07-22 14:14:32 +0200 | [diff] [blame] | 1467 | continue; |
Dave Martin | 6da80ce | 2010-07-30 09:50:09 -0300 | [diff] [blame] | 1468 | |
Jiri Olsa | 4035672 | 2016-01-20 12:56:32 +0100 | [diff] [blame] | 1469 | if (!is_regular_file(name)) |
| 1470 | continue; |
| 1471 | |
Dave Martin | 6da80ce | 2010-07-30 09:50:09 -0300 | [diff] [blame] | 1472 | /* Name is now the name of the next image to try */ |
Cody P Schafer | 3aafe5a | 2012-08-10 15:23:02 -0700 | [diff] [blame] | 1473 | if (symsrc__init(ss, dso, name, symtab_type) < 0) |
Dave Martin | 6da80ce | 2010-07-30 09:50:09 -0300 | [diff] [blame] | 1474 | continue; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1475 | |
Cody P Schafer | 3aafe5a | 2012-08-10 15:23:02 -0700 | [diff] [blame] | 1476 | if (!syms_ss && symsrc__has_symtab(ss)) { |
| 1477 | syms_ss = ss; |
| 1478 | next_slot = true; |
Adrian Hunter | 0058aef | 2013-12-03 09:23:08 +0200 | [diff] [blame] | 1479 | if (!dso->symsrc_filename) |
| 1480 | dso->symsrc_filename = strdup(name); |
Cody P Schafer | d26cd12 | 2012-08-10 15:23:00 -0700 | [diff] [blame] | 1481 | } |
| 1482 | |
Cody P Schafer | 3aafe5a | 2012-08-10 15:23:02 -0700 | [diff] [blame] | 1483 | if (!runtime_ss && symsrc__possibly_runtime(ss)) { |
| 1484 | runtime_ss = ss; |
| 1485 | next_slot = true; |
Cody P Schafer | a44f605 | 2012-08-10 15:22:59 -0700 | [diff] [blame] | 1486 | } |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1487 | |
Cody P Schafer | 3aafe5a | 2012-08-10 15:23:02 -0700 | [diff] [blame] | 1488 | if (next_slot) { |
| 1489 | ss_pos++; |
Jiri Olsa | 33ff581 | 2012-04-18 15:46:58 +0200 | [diff] [blame] | 1490 | |
Cody P Schafer | 3aafe5a | 2012-08-10 15:23:02 -0700 | [diff] [blame] | 1491 | if (syms_ss && runtime_ss) |
| 1492 | break; |
Namhyung Kim | 98e9f03 | 2014-02-20 10:32:54 +0900 | [diff] [blame] | 1493 | } else { |
| 1494 | symsrc__destroy(ss); |
Dave Martin | 6da80ce | 2010-07-30 09:50:09 -0300 | [diff] [blame] | 1495 | } |
Cody P Schafer | 3aafe5a | 2012-08-10 15:23:02 -0700 | [diff] [blame] | 1496 | |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 1497 | } |
Dave Martin | 6da80ce | 2010-07-30 09:50:09 -0300 | [diff] [blame] | 1498 | |
Cody P Schafer | 3aafe5a | 2012-08-10 15:23:02 -0700 | [diff] [blame] | 1499 | if (!runtime_ss && !syms_ss) |
| 1500 | goto out_free; |
| 1501 | |
| 1502 | if (runtime_ss && !syms_ss) { |
| 1503 | syms_ss = runtime_ss; |
Arnaldo Carvalho de Melo | 60e4b10 | 2011-03-22 15:42:14 -0300 | [diff] [blame] | 1504 | } |
| 1505 | |
Cody P Schafer | 3aafe5a | 2012-08-10 15:23:02 -0700 | [diff] [blame] | 1506 | /* We'll have to hope for the best */ |
| 1507 | if (!runtime_ss && syms_ss) |
| 1508 | runtime_ss = syms_ss; |
| 1509 | |
Wang Nan | e7ee404 | 2016-02-05 14:01:27 +0000 | [diff] [blame] | 1510 | if (syms_ss && syms_ss->type == DSO_BINARY_TYPE__BUILD_ID_CACHE) |
| 1511 | if (dso__build_id_is_kmod(dso, name, PATH_MAX)) |
| 1512 | kmod = true; |
| 1513 | |
Namhyung Kim | 1029f9f | 2014-02-20 10:32:56 +0900 | [diff] [blame] | 1514 | if (syms_ss) |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame^] | 1515 | ret = dso__load_sym(dso, map, syms_ss, runtime_ss, kmod); |
Namhyung Kim | 1029f9f | 2014-02-20 10:32:56 +0900 | [diff] [blame] | 1516 | else |
Cody P Schafer | 3aafe5a | 2012-08-10 15:23:02 -0700 | [diff] [blame] | 1517 | ret = -1; |
| 1518 | |
David Ahern | f47b58b | 2012-08-19 09:47:14 -0600 | [diff] [blame] | 1519 | if (ret > 0) { |
Cody P Schafer | 3aafe5a | 2012-08-10 15:23:02 -0700 | [diff] [blame] | 1520 | int nr_plt; |
| 1521 | |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame^] | 1522 | nr_plt = dso__synthesize_plt_symbols(dso, runtime_ss, map); |
Cody P Schafer | 3aafe5a | 2012-08-10 15:23:02 -0700 | [diff] [blame] | 1523 | if (nr_plt > 0) |
| 1524 | ret += nr_plt; |
| 1525 | } |
| 1526 | |
| 1527 | for (; ss_pos > 0; ss_pos--) |
| 1528 | symsrc__destroy(&ss_[ss_pos - 1]); |
| 1529 | out_free: |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1530 | free(name); |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 1531 | if (ret < 0 && strstr(dso->name, " (deleted)") != NULL) |
Namhyung Kim | 4a936ed | 2015-05-18 09:30:40 +0900 | [diff] [blame] | 1532 | ret = 0; |
| 1533 | out: |
| 1534 | dso__set_loaded(dso, map->type); |
| 1535 | pthread_mutex_unlock(&dso->lock); |
| 1536 | |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1537 | return ret; |
| 1538 | } |
| 1539 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 1540 | 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] | 1541 | enum map_type type, const char *name) |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1542 | { |
Arnaldo Carvalho de Melo | 1eee78a | 2015-05-22 12:58:53 -0300 | [diff] [blame] | 1543 | struct maps *maps = &mg->maps[type]; |
Arnaldo Carvalho de Melo | 4bb7123d | 2015-05-22 11:52:22 -0300 | [diff] [blame] | 1544 | struct map *map; |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1545 | |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 1546 | pthread_rwlock_rdlock(&maps->lock); |
| 1547 | |
Arnaldo Carvalho de Melo | 4bb7123d | 2015-05-22 11:52:22 -0300 | [diff] [blame] | 1548 | for (map = maps__first(maps); map; map = map__next(map)) { |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 1549 | if (map->dso && strcmp(map->dso->short_name, name) == 0) |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 1550 | goto out_unlock; |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1551 | } |
| 1552 | |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 1553 | map = NULL; |
| 1554 | |
| 1555 | out_unlock: |
| 1556 | pthread_rwlock_unlock(&maps->lock); |
| 1557 | return map; |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1558 | } |
| 1559 | |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 1560 | int dso__load_vmlinux(struct dso *dso, struct map *map, |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame^] | 1561 | const char *vmlinux, bool vmlinux_allocated) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1562 | { |
Cody P Schafer | b68e2f9 | 2012-08-10 15:22:57 -0700 | [diff] [blame] | 1563 | int err = -1; |
| 1564 | struct symsrc ss; |
David Ahern | ec5761e | 2010-12-09 13:27:07 -0700 | [diff] [blame] | 1565 | char symfs_vmlinux[PATH_MAX]; |
Cody P Schafer | 005f929 | 2012-08-10 15:22:58 -0700 | [diff] [blame] | 1566 | enum dso_binary_type symtab_type; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1567 | |
Namhyung Kim | 5698d2c | 2013-07-17 17:08:15 +0900 | [diff] [blame] | 1568 | if (vmlinux[0] == '/') |
| 1569 | snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s", vmlinux); |
| 1570 | else |
Arnaldo Carvalho de Melo | 972f393 | 2014-07-29 10:21:58 -0300 | [diff] [blame] | 1571 | symbol__join_symfs(symfs_vmlinux, vmlinux); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1572 | |
Cody P Schafer | 21ea453 | 2012-08-10 15:22:56 -0700 | [diff] [blame] | 1573 | if (dso->kernel == DSO_TYPE_GUEST_KERNEL) |
Cody P Schafer | 005f929 | 2012-08-10 15:22:58 -0700 | [diff] [blame] | 1574 | symtab_type = DSO_BINARY_TYPE__GUEST_VMLINUX; |
Cody P Schafer | 21ea453 | 2012-08-10 15:22:56 -0700 | [diff] [blame] | 1575 | else |
Cody P Schafer | 005f929 | 2012-08-10 15:22:58 -0700 | [diff] [blame] | 1576 | symtab_type = DSO_BINARY_TYPE__VMLINUX; |
Cody P Schafer | 21ea453 | 2012-08-10 15:22:56 -0700 | [diff] [blame] | 1577 | |
Cody P Schafer | 005f929 | 2012-08-10 15:22:58 -0700 | [diff] [blame] | 1578 | if (symsrc__init(&ss, dso, symfs_vmlinux, symtab_type)) |
Cody P Schafer | b68e2f9 | 2012-08-10 15:22:57 -0700 | [diff] [blame] | 1579 | return -1; |
| 1580 | |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame^] | 1581 | err = dso__load_sym(dso, map, &ss, &ss, 0); |
Cody P Schafer | b68e2f9 | 2012-08-10 15:22:57 -0700 | [diff] [blame] | 1582 | symsrc__destroy(&ss); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1583 | |
Cody P Schafer | 515850e | 2012-08-10 15:22:54 -0700 | [diff] [blame] | 1584 | if (err > 0) { |
Adrian Hunter | 39b12f78 | 2013-08-07 14:38:47 +0300 | [diff] [blame] | 1585 | if (dso->kernel == DSO_TYPE_GUEST_KERNEL) |
Arnaldo Carvalho de Melo | 5f70619 | 2013-12-17 16:14:07 -0300 | [diff] [blame] | 1586 | dso->binary_type = DSO_BINARY_TYPE__GUEST_VMLINUX; |
Adrian Hunter | 39b12f78 | 2013-08-07 14:38:47 +0300 | [diff] [blame] | 1587 | else |
Arnaldo Carvalho de Melo | 5f70619 | 2013-12-17 16:14:07 -0300 | [diff] [blame] | 1588 | dso->binary_type = DSO_BINARY_TYPE__VMLINUX; |
Arnaldo Carvalho de Melo | bf4414a | 2013-12-10 15:19:23 -0300 | [diff] [blame] | 1589 | dso__set_long_name(dso, vmlinux, vmlinux_allocated); |
Cody P Schafer | 515850e | 2012-08-10 15:22:54 -0700 | [diff] [blame] | 1590 | dso__set_loaded(dso, map->type); |
David Ahern | ec5761e | 2010-12-09 13:27:07 -0700 | [diff] [blame] | 1591 | pr_debug("Using %s for symbols\n", symfs_vmlinux); |
Cody P Schafer | 515850e | 2012-08-10 15:22:54 -0700 | [diff] [blame] | 1592 | } |
Arnaldo Carvalho de Melo | 3846df2 | 2010-02-22 16:15:39 -0300 | [diff] [blame] | 1593 | |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1594 | return err; |
| 1595 | } |
| 1596 | |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame^] | 1597 | int dso__load_vmlinux_path(struct dso *dso, struct map *map) |
Arnaldo Carvalho de Melo | a19afe4 | 2010-01-27 21:05:50 -0200 | [diff] [blame] | 1598 | { |
| 1599 | int i, err = 0; |
Namhyung Kim | 00dc865 | 2014-11-04 10:14:32 +0900 | [diff] [blame] | 1600 | char *filename = NULL; |
Arnaldo Carvalho de Melo | a19afe4 | 2010-01-27 21:05:50 -0200 | [diff] [blame] | 1601 | |
Arnaldo Carvalho de Melo | dc38218 | 2015-10-11 17:17:24 -0300 | [diff] [blame] | 1602 | pr_debug("Looking at the vmlinux_path (%d entries long)\n", |
| 1603 | vmlinux_path__nr_entries + 1); |
| 1604 | |
| 1605 | for (i = 0; i < vmlinux_path__nr_entries; ++i) { |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame^] | 1606 | err = dso__load_vmlinux(dso, map, vmlinux_path[i], false); |
Arnaldo Carvalho de Melo | dc38218 | 2015-10-11 17:17:24 -0300 | [diff] [blame] | 1607 | if (err > 0) |
| 1608 | goto out; |
| 1609 | } |
| 1610 | |
Namhyung Kim | 00dc865 | 2014-11-04 10:14:32 +0900 | [diff] [blame] | 1611 | if (!symbol_conf.ignore_vmlinux_buildid) |
| 1612 | filename = dso__build_id_filename(dso, NULL, 0); |
Arnaldo Carvalho de Melo | 5ad90e4 | 2010-05-26 13:26:02 -0300 | [diff] [blame] | 1613 | if (filename != NULL) { |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame^] | 1614 | err = dso__load_vmlinux(dso, map, filename, true); |
Arnaldo Carvalho de Melo | 5230fb7 | 2013-12-10 11:58:52 -0300 | [diff] [blame] | 1615 | if (err > 0) |
Arnaldo Carvalho de Melo | 5ad90e4 | 2010-05-26 13:26:02 -0300 | [diff] [blame] | 1616 | goto out; |
Arnaldo Carvalho de Melo | 5ad90e4 | 2010-05-26 13:26:02 -0300 | [diff] [blame] | 1617 | free(filename); |
| 1618 | } |
Arnaldo Carvalho de Melo | 5ad90e4 | 2010-05-26 13:26:02 -0300 | [diff] [blame] | 1619 | out: |
Arnaldo Carvalho de Melo | a19afe4 | 2010-01-27 21:05:50 -0200 | [diff] [blame] | 1620 | return err; |
| 1621 | } |
| 1622 | |
Masami Hiramatsu | c48903b | 2016-05-11 22:52:08 +0900 | [diff] [blame] | 1623 | static bool visible_dir_filter(const char *name, struct dirent *d) |
| 1624 | { |
| 1625 | if (d->d_type != DT_DIR) |
| 1626 | return false; |
| 1627 | return lsdir_no_dot_filter(name, d); |
| 1628 | } |
| 1629 | |
Adrian Hunter | 0544d42 | 2013-10-14 13:43:43 +0300 | [diff] [blame] | 1630 | static int find_matching_kcore(struct map *map, char *dir, size_t dir_sz) |
| 1631 | { |
| 1632 | char kallsyms_filename[PATH_MAX]; |
Adrian Hunter | 0544d42 | 2013-10-14 13:43:43 +0300 | [diff] [blame] | 1633 | int ret = -1; |
Masami Hiramatsu | c48903b | 2016-05-11 22:52:08 +0900 | [diff] [blame] | 1634 | struct strlist *dirs; |
| 1635 | struct str_node *nd; |
Adrian Hunter | 0544d42 | 2013-10-14 13:43:43 +0300 | [diff] [blame] | 1636 | |
Masami Hiramatsu | c48903b | 2016-05-11 22:52:08 +0900 | [diff] [blame] | 1637 | dirs = lsdir(dir, visible_dir_filter); |
| 1638 | if (!dirs) |
Adrian Hunter | 0544d42 | 2013-10-14 13:43:43 +0300 | [diff] [blame] | 1639 | return -1; |
| 1640 | |
Arnaldo Carvalho de Melo | 602a1f4 | 2016-06-23 11:31:20 -0300 | [diff] [blame] | 1641 | strlist__for_each_entry(nd, dirs) { |
Adrian Hunter | 0544d42 | 2013-10-14 13:43:43 +0300 | [diff] [blame] | 1642 | scnprintf(kallsyms_filename, sizeof(kallsyms_filename), |
Masami Hiramatsu | c48903b | 2016-05-11 22:52:08 +0900 | [diff] [blame] | 1643 | "%s/%s/kallsyms", dir, nd->s); |
Adrian Hunter | a00d28c | 2014-01-29 16:14:41 +0200 | [diff] [blame] | 1644 | if (!validate_kcore_addresses(kallsyms_filename, map)) { |
Adrian Hunter | 0544d42 | 2013-10-14 13:43:43 +0300 | [diff] [blame] | 1645 | strlcpy(dir, kallsyms_filename, dir_sz); |
| 1646 | ret = 0; |
| 1647 | break; |
| 1648 | } |
| 1649 | } |
| 1650 | |
Masami Hiramatsu | c48903b | 2016-05-11 22:52:08 +0900 | [diff] [blame] | 1651 | strlist__delete(dirs); |
Adrian Hunter | 0544d42 | 2013-10-14 13:43:43 +0300 | [diff] [blame] | 1652 | |
| 1653 | return ret; |
| 1654 | } |
| 1655 | |
Masami Hiramatsu | 11870d7 | 2016-05-29 00:15:13 +0900 | [diff] [blame] | 1656 | /* |
| 1657 | * Use open(O_RDONLY) to check readability directly instead of access(R_OK) |
| 1658 | * since access(R_OK) only checks with real UID/GID but open() use effective |
| 1659 | * UID/GID and actual capabilities (e.g. /proc/kcore requires CAP_SYS_RAWIO). |
| 1660 | */ |
| 1661 | static bool filename__readable(const char *file) |
| 1662 | { |
| 1663 | int fd = open(file, O_RDONLY); |
| 1664 | if (fd < 0) |
| 1665 | return false; |
| 1666 | close(fd); |
| 1667 | return true; |
| 1668 | } |
| 1669 | |
Adrian Hunter | 0544d42 | 2013-10-14 13:43:43 +0300 | [diff] [blame] | 1670 | static char *dso__find_kallsyms(struct dso *dso, struct map *map) |
| 1671 | { |
| 1672 | u8 host_build_id[BUILD_ID_SIZE]; |
Masami Hiramatsu | b5d8bbe | 2016-05-11 22:51:59 +0900 | [diff] [blame] | 1673 | char sbuild_id[SBUILD_ID_SIZE]; |
Adrian Hunter | 0544d42 | 2013-10-14 13:43:43 +0300 | [diff] [blame] | 1674 | bool is_host = false; |
| 1675 | char path[PATH_MAX]; |
| 1676 | |
| 1677 | if (!dso->has_build_id) { |
| 1678 | /* |
| 1679 | * Last resort, if we don't have a build-id and couldn't find |
| 1680 | * any vmlinux file, try the running kernel kallsyms table. |
| 1681 | */ |
| 1682 | goto proc_kallsyms; |
| 1683 | } |
| 1684 | |
| 1685 | if (sysfs__read_build_id("/sys/kernel/notes", host_build_id, |
| 1686 | sizeof(host_build_id)) == 0) |
| 1687 | is_host = dso__build_id_equal(dso, host_build_id); |
| 1688 | |
Masami Hiramatsu | 4e4b6c0 | 2016-05-29 00:15:28 +0900 | [diff] [blame] | 1689 | /* Try a fast path for /proc/kallsyms if possible */ |
Adrian Hunter | 0544d42 | 2013-10-14 13:43:43 +0300 | [diff] [blame] | 1690 | if (is_host) { |
Adrian Hunter | 0544d42 | 2013-10-14 13:43:43 +0300 | [diff] [blame] | 1691 | /* |
Masami Hiramatsu | 11870d7 | 2016-05-29 00:15:13 +0900 | [diff] [blame] | 1692 | * Do not check the build-id cache, unless we know we cannot use |
| 1693 | * /proc/kcore or module maps don't match to /proc/kallsyms. |
| 1694 | * To check readability of /proc/kcore, do not use access(R_OK) |
| 1695 | * since /proc/kcore requires CAP_SYS_RAWIO to read and access |
| 1696 | * can't check it. |
Adrian Hunter | 0544d42 | 2013-10-14 13:43:43 +0300 | [diff] [blame] | 1697 | */ |
Masami Hiramatsu | 11870d7 | 2016-05-29 00:15:13 +0900 | [diff] [blame] | 1698 | if (filename__readable("/proc/kcore") && |
| 1699 | !validate_kcore_addresses("/proc/kallsyms", map)) |
| 1700 | goto proc_kallsyms; |
Adrian Hunter | 0544d42 | 2013-10-14 13:43:43 +0300 | [diff] [blame] | 1701 | } |
| 1702 | |
Masami Hiramatsu | 4e4b6c0 | 2016-05-29 00:15:28 +0900 | [diff] [blame] | 1703 | build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id); |
| 1704 | |
Adrian Hunter | 449867e | 2013-11-26 15:19:24 +0200 | [diff] [blame] | 1705 | /* Find kallsyms in build-id cache with kcore */ |
Masami Hiramatsu | 4e4b6c0 | 2016-05-29 00:15:28 +0900 | [diff] [blame] | 1706 | scnprintf(path, sizeof(path), "%s/%s/%s", |
| 1707 | buildid_dir, DSO__NAME_KCORE, sbuild_id); |
| 1708 | |
Adrian Hunter | 449867e | 2013-11-26 15:19:24 +0200 | [diff] [blame] | 1709 | if (!find_matching_kcore(map, path, sizeof(path))) |
| 1710 | return strdup(path); |
| 1711 | |
Masami Hiramatsu | 4e4b6c0 | 2016-05-29 00:15:28 +0900 | [diff] [blame] | 1712 | /* Use current /proc/kallsyms if possible */ |
| 1713 | if (is_host) { |
| 1714 | proc_kallsyms: |
| 1715 | return strdup("/proc/kallsyms"); |
| 1716 | } |
| 1717 | |
| 1718 | /* Finally, find a cache of kallsyms */ |
Masami Hiramatsu | 0141226 | 2016-05-29 00:15:37 +0900 | [diff] [blame] | 1719 | if (!build_id_cache__kallsyms_path(sbuild_id, path, sizeof(path))) { |
Adrian Hunter | 0544d42 | 2013-10-14 13:43:43 +0300 | [diff] [blame] | 1720 | pr_err("No kallsyms or vmlinux with build-id %s was found\n", |
| 1721 | sbuild_id); |
| 1722 | return NULL; |
| 1723 | } |
| 1724 | |
| 1725 | return strdup(path); |
Adrian Hunter | 0544d42 | 2013-10-14 13:43:43 +0300 | [diff] [blame] | 1726 | } |
| 1727 | |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame^] | 1728 | static int dso__load_kernel_sym(struct dso *dso, struct map *map) |
Arnaldo Carvalho de Melo | a827c87 | 2009-05-28 14:55:19 -0300 | [diff] [blame] | 1729 | { |
Arnaldo Carvalho de Melo | cc612d8 | 2009-11-23 16:39:10 -0200 | [diff] [blame] | 1730 | int err; |
Arnaldo Carvalho de Melo | 9e20144 | 2010-01-14 18:30:06 -0200 | [diff] [blame] | 1731 | const char *kallsyms_filename = NULL; |
| 1732 | char *kallsyms_allocated_filename = NULL; |
Arnaldo Carvalho de Melo | dc8d6ab | 2010-01-19 10:36:14 -0200 | [diff] [blame] | 1733 | /* |
David Ahern | b226a5a7 | 2010-12-07 19:39:46 -0700 | [diff] [blame] | 1734 | * Step 1: if the user specified a kallsyms or vmlinux filename, use |
| 1735 | * 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] | 1736 | * |
| 1737 | * For instance, try to analyse an ARM perf.data file _without_ a |
| 1738 | * build-id, or if the user specifies the wrong path to the right |
| 1739 | * vmlinux file, obviously we can't fallback to another vmlinux (a |
| 1740 | * x86_86 one, on the machine where analysis is being performed, say), |
| 1741 | * or worse, /proc/kallsyms. |
| 1742 | * |
| 1743 | * If the specified file _has_ a build-id and there is a build-id |
| 1744 | * section in the perf.data file, we will still do the expected |
| 1745 | * validation in dso__load_vmlinux and will bail out if they don't |
| 1746 | * match. |
| 1747 | */ |
David Ahern | b226a5a7 | 2010-12-07 19:39:46 -0700 | [diff] [blame] | 1748 | if (symbol_conf.kallsyms_name != NULL) { |
| 1749 | kallsyms_filename = symbol_conf.kallsyms_name; |
| 1750 | goto do_kallsyms; |
| 1751 | } |
| 1752 | |
Willy Tarreau | fc2be69 | 2013-09-14 10:32:59 +0200 | [diff] [blame] | 1753 | if (!symbol_conf.ignore_vmlinux && symbol_conf.vmlinux_name != NULL) { |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame^] | 1754 | return dso__load_vmlinux(dso, map, symbol_conf.vmlinux_name, false); |
Arnaldo Carvalho de Melo | dc8d6ab | 2010-01-19 10:36:14 -0200 | [diff] [blame] | 1755 | } |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1756 | |
Willy Tarreau | fc2be69 | 2013-09-14 10:32:59 +0200 | [diff] [blame] | 1757 | if (!symbol_conf.ignore_vmlinux && vmlinux_path != NULL) { |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame^] | 1758 | err = dso__load_vmlinux_path(dso, map); |
Arnaldo Carvalho de Melo | a19afe4 | 2010-01-27 21:05:50 -0200 | [diff] [blame] | 1759 | if (err > 0) |
Adrian Hunter | 39b12f78 | 2013-08-07 14:38:47 +0300 | [diff] [blame] | 1760 | return err; |
Arnaldo Carvalho de Melo | cc612d8 | 2009-11-23 16:39:10 -0200 | [diff] [blame] | 1761 | } |
| 1762 | |
David Ahern | ec5761e | 2010-12-09 13:27:07 -0700 | [diff] [blame] | 1763 | /* do not try local files if a symfs was given */ |
| 1764 | if (symbol_conf.symfs[0] != 0) |
| 1765 | return -1; |
| 1766 | |
Adrian Hunter | 0544d42 | 2013-10-14 13:43:43 +0300 | [diff] [blame] | 1767 | kallsyms_allocated_filename = dso__find_kallsyms(dso, map); |
| 1768 | if (!kallsyms_allocated_filename) |
| 1769 | return -1; |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 1770 | |
Adrian Hunter | 0544d42 | 2013-10-14 13:43:43 +0300 | [diff] [blame] | 1771 | kallsyms_filename = kallsyms_allocated_filename; |
Arnaldo Carvalho de Melo | a827c87 | 2009-05-28 14:55:19 -0300 | [diff] [blame] | 1772 | |
Arnaldo Carvalho de Melo | dc8d6ab | 2010-01-19 10:36:14 -0200 | [diff] [blame] | 1773 | do_kallsyms: |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame^] | 1774 | err = dso__load_kallsyms(dso, kallsyms_filename, map); |
Arnaldo Carvalho de Melo | 3846df2 | 2010-02-22 16:15:39 -0300 | [diff] [blame] | 1775 | if (err > 0) |
| 1776 | pr_debug("Using %s for symbols\n", kallsyms_filename); |
Arnaldo Carvalho de Melo | dc8d6ab | 2010-01-19 10:36:14 -0200 | [diff] [blame] | 1777 | free(kallsyms_allocated_filename); |
| 1778 | |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 1779 | if (err > 0 && !dso__is_kcore(dso)) { |
Adrian Hunter | bdac0bc | 2014-07-14 13:02:43 +0300 | [diff] [blame] | 1780 | dso->binary_type = DSO_BINARY_TYPE__KALLSYMS; |
Masami Hiramatsu | 0a77582 | 2016-05-15 12:19:40 +0900 | [diff] [blame] | 1781 | dso__set_long_name(dso, DSO__NAME_KALLSYMS, false); |
Arnaldo Carvalho de Melo | 6a4694a | 2009-11-27 16:29:17 -0200 | [diff] [blame] | 1782 | map__fixup_start(map); |
| 1783 | map__fixup_end(map); |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1784 | } |
Arnaldo Carvalho de Melo | 94cb9e3 | 2009-08-06 14:43:17 -0300 | [diff] [blame] | 1785 | |
Arnaldo Carvalho de Melo | a827c87 | 2009-05-28 14:55:19 -0300 | [diff] [blame] | 1786 | return err; |
| 1787 | } |
| 1788 | |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame^] | 1789 | 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] | 1790 | { |
| 1791 | int err; |
| 1792 | const char *kallsyms_filename = NULL; |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 1793 | struct machine *machine; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1794 | char path[PATH_MAX]; |
| 1795 | |
| 1796 | if (!map->groups) { |
| 1797 | pr_debug("Guest kernel map hasn't the point to groups\n"); |
| 1798 | return -1; |
| 1799 | } |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 1800 | machine = map->groups->machine; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1801 | |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 1802 | if (machine__is_default_guest(machine)) { |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1803 | /* |
| 1804 | * if the user specified a vmlinux filename, use it and only |
| 1805 | * it, reporting errors to the user if it cannot be used. |
| 1806 | * Or use file guest_kallsyms inputted by user on commandline |
| 1807 | */ |
| 1808 | if (symbol_conf.default_guest_vmlinux_name != NULL) { |
Arnaldo Carvalho de Melo | aeafcba | 2011-03-31 10:56:28 -0300 | [diff] [blame] | 1809 | err = dso__load_vmlinux(dso, map, |
Arnaldo Carvalho de Melo | 5230fb7 | 2013-12-10 11:58:52 -0300 | [diff] [blame] | 1810 | symbol_conf.default_guest_vmlinux_name, |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame^] | 1811 | false); |
Adrian Hunter | 39b12f78 | 2013-08-07 14:38:47 +0300 | [diff] [blame] | 1812 | return err; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1813 | } |
| 1814 | |
| 1815 | kallsyms_filename = symbol_conf.default_guest_kallsyms; |
| 1816 | if (!kallsyms_filename) |
| 1817 | return -1; |
| 1818 | } else { |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 1819 | sprintf(path, "%s/proc/kallsyms", machine->root_dir); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1820 | kallsyms_filename = path; |
| 1821 | } |
| 1822 | |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame^] | 1823 | err = dso__load_kallsyms(dso, kallsyms_filename, map); |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 1824 | if (err > 0) |
Adrian Hunter | 39b12f78 | 2013-08-07 14:38:47 +0300 | [diff] [blame] | 1825 | pr_debug("Using %s for symbols\n", kallsyms_filename); |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 1826 | if (err > 0 && !dso__is_kcore(dso)) { |
Adrian Hunter | bdac0bc | 2014-07-14 13:02:43 +0300 | [diff] [blame] | 1827 | dso->binary_type = DSO_BINARY_TYPE__GUEST_KALLSYMS; |
Adrian Hunter | 39b12f78 | 2013-08-07 14:38:47 +0300 | [diff] [blame] | 1828 | machine__mmap_name(machine, path, sizeof(path)); |
Arnaldo Carvalho de Melo | 7e155d4 | 2013-12-10 15:08:44 -0300 | [diff] [blame] | 1829 | dso__set_long_name(dso, strdup(path), true); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1830 | map__fixup_start(map); |
| 1831 | map__fixup_end(map); |
| 1832 | } |
| 1833 | |
| 1834 | return err; |
| 1835 | } |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 1836 | |
Arnaldo Carvalho de Melo | cc612d8 | 2009-11-23 16:39:10 -0200 | [diff] [blame] | 1837 | static void vmlinux_path__exit(void) |
Arnaldo Carvalho de Melo | 2446042 | 2009-11-18 20:20:53 -0200 | [diff] [blame] | 1838 | { |
Arnaldo Carvalho de Melo | 0466252 | 2013-12-26 17:41:15 -0300 | [diff] [blame] | 1839 | while (--vmlinux_path__nr_entries >= 0) |
| 1840 | zfree(&vmlinux_path[vmlinux_path__nr_entries]); |
Wang Nan | c4f0354 | 2015-05-17 10:56:27 +0000 | [diff] [blame] | 1841 | vmlinux_path__nr_entries = 0; |
Arnaldo Carvalho de Melo | cc612d8 | 2009-11-23 16:39:10 -0200 | [diff] [blame] | 1842 | |
Arnaldo Carvalho de Melo | 0466252 | 2013-12-26 17:41:15 -0300 | [diff] [blame] | 1843 | zfree(&vmlinux_path); |
Arnaldo Carvalho de Melo | cc612d8 | 2009-11-23 16:39:10 -0200 | [diff] [blame] | 1844 | } |
| 1845 | |
Ekaterina Tumanova | aac4864 | 2015-11-25 17:32:45 +0100 | [diff] [blame] | 1846 | static const char * const vmlinux_paths[] = { |
| 1847 | "vmlinux", |
| 1848 | "/boot/vmlinux" |
| 1849 | }; |
| 1850 | |
| 1851 | static const char * const vmlinux_paths_upd[] = { |
| 1852 | "/boot/vmlinux-%s", |
| 1853 | "/usr/lib/debug/boot/vmlinux-%s", |
| 1854 | "/lib/modules/%s/build/vmlinux", |
Ekaterina Tumanova | f55ae95 | 2015-11-25 17:32:46 +0100 | [diff] [blame] | 1855 | "/usr/lib/debug/lib/modules/%s/vmlinux", |
| 1856 | "/usr/lib/debug/boot/vmlinux-%s.debug" |
Ekaterina Tumanova | aac4864 | 2015-11-25 17:32:45 +0100 | [diff] [blame] | 1857 | }; |
| 1858 | |
| 1859 | static int vmlinux_path__add(const char *new_entry) |
| 1860 | { |
| 1861 | vmlinux_path[vmlinux_path__nr_entries] = strdup(new_entry); |
| 1862 | if (vmlinux_path[vmlinux_path__nr_entries] == NULL) |
| 1863 | return -1; |
| 1864 | ++vmlinux_path__nr_entries; |
| 1865 | |
| 1866 | return 0; |
| 1867 | } |
| 1868 | |
Kan Liang | ce80d3b | 2015-08-28 05:48:04 -0400 | [diff] [blame] | 1869 | static int vmlinux_path__init(struct perf_env *env) |
Arnaldo Carvalho de Melo | cc612d8 | 2009-11-23 16:39:10 -0200 | [diff] [blame] | 1870 | { |
| 1871 | struct utsname uts; |
| 1872 | char bf[PATH_MAX]; |
Namhyung Kim | 0a7e6d1 | 2014-08-12 15:40:45 +0900 | [diff] [blame] | 1873 | char *kernel_version; |
Ekaterina Tumanova | aac4864 | 2015-11-25 17:32:45 +0100 | [diff] [blame] | 1874 | unsigned int i; |
Arnaldo Carvalho de Melo | cc612d8 | 2009-11-23 16:39:10 -0200 | [diff] [blame] | 1875 | |
Ekaterina Tumanova | aac4864 | 2015-11-25 17:32:45 +0100 | [diff] [blame] | 1876 | vmlinux_path = malloc(sizeof(char *) * (ARRAY_SIZE(vmlinux_paths) + |
| 1877 | ARRAY_SIZE(vmlinux_paths_upd))); |
Arnaldo Carvalho de Melo | cc612d8 | 2009-11-23 16:39:10 -0200 | [diff] [blame] | 1878 | if (vmlinux_path == NULL) |
| 1879 | return -1; |
| 1880 | |
Ekaterina Tumanova | aac4864 | 2015-11-25 17:32:45 +0100 | [diff] [blame] | 1881 | for (i = 0; i < ARRAY_SIZE(vmlinux_paths); i++) |
| 1882 | if (vmlinux_path__add(vmlinux_paths[i]) < 0) |
| 1883 | goto out_fail; |
David Ahern | ec5761e | 2010-12-09 13:27:07 -0700 | [diff] [blame] | 1884 | |
Namhyung Kim | 0a7e6d1 | 2014-08-12 15:40:45 +0900 | [diff] [blame] | 1885 | /* only try kernel version if no symfs was given */ |
David Ahern | ec5761e | 2010-12-09 13:27:07 -0700 | [diff] [blame] | 1886 | if (symbol_conf.symfs[0] != 0) |
| 1887 | return 0; |
| 1888 | |
Namhyung Kim | 0a7e6d1 | 2014-08-12 15:40:45 +0900 | [diff] [blame] | 1889 | if (env) { |
| 1890 | kernel_version = env->os_release; |
| 1891 | } else { |
| 1892 | if (uname(&uts) < 0) |
| 1893 | goto out_fail; |
David Ahern | ec5761e | 2010-12-09 13:27:07 -0700 | [diff] [blame] | 1894 | |
Namhyung Kim | 0a7e6d1 | 2014-08-12 15:40:45 +0900 | [diff] [blame] | 1895 | kernel_version = uts.release; |
| 1896 | } |
| 1897 | |
Ekaterina Tumanova | aac4864 | 2015-11-25 17:32:45 +0100 | [diff] [blame] | 1898 | for (i = 0; i < ARRAY_SIZE(vmlinux_paths_upd); i++) { |
| 1899 | snprintf(bf, sizeof(bf), vmlinux_paths_upd[i], kernel_version); |
| 1900 | if (vmlinux_path__add(bf) < 0) |
| 1901 | goto out_fail; |
| 1902 | } |
Arnaldo Carvalho de Melo | cc612d8 | 2009-11-23 16:39:10 -0200 | [diff] [blame] | 1903 | |
| 1904 | return 0; |
| 1905 | |
| 1906 | out_fail: |
| 1907 | vmlinux_path__exit(); |
| 1908 | return -1; |
| 1909 | } |
| 1910 | |
David Ahern | 3bfe5f8 | 2013-11-18 13:32:48 -0700 | [diff] [blame] | 1911 | int setup_list(struct strlist **list, const char *list_str, |
Arnaldo Carvalho de Melo | 655000e | 2009-12-15 20:04:40 -0200 | [diff] [blame] | 1912 | const char *list_name) |
| 1913 | { |
| 1914 | if (list_str == NULL) |
| 1915 | return 0; |
| 1916 | |
Arnaldo Carvalho de Melo | 4a77e21 | 2015-07-20 12:13:34 -0300 | [diff] [blame] | 1917 | *list = strlist__new(list_str, NULL); |
Arnaldo Carvalho de Melo | 655000e | 2009-12-15 20:04:40 -0200 | [diff] [blame] | 1918 | if (!*list) { |
| 1919 | pr_err("problems parsing %s list\n", list_name); |
| 1920 | return -1; |
| 1921 | } |
Arnaldo Carvalho de Melo | 0bc2f2f | 2015-07-13 08:21:57 -0300 | [diff] [blame] | 1922 | |
| 1923 | symbol_conf.has_filter = true; |
Arnaldo Carvalho de Melo | 655000e | 2009-12-15 20:04:40 -0200 | [diff] [blame] | 1924 | return 0; |
| 1925 | } |
| 1926 | |
David Ahern | e03eaa4 | 2015-03-24 09:52:41 -0600 | [diff] [blame] | 1927 | int setup_intlist(struct intlist **list, const char *list_str, |
| 1928 | const char *list_name) |
| 1929 | { |
| 1930 | if (list_str == NULL) |
| 1931 | return 0; |
| 1932 | |
| 1933 | *list = intlist__new(list_str); |
| 1934 | if (!*list) { |
| 1935 | pr_err("problems parsing %s list\n", list_name); |
| 1936 | return -1; |
| 1937 | } |
| 1938 | return 0; |
| 1939 | } |
| 1940 | |
Arnaldo Carvalho de Melo | ec80fde | 2011-05-26 09:53:51 -0300 | [diff] [blame] | 1941 | static bool symbol__read_kptr_restrict(void) |
| 1942 | { |
| 1943 | bool value = false; |
Wang Nan | 38272dc | 2016-05-24 09:21:27 +0000 | [diff] [blame] | 1944 | FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r"); |
Arnaldo Carvalho de Melo | ec80fde | 2011-05-26 09:53:51 -0300 | [diff] [blame] | 1945 | |
Wang Nan | 38272dc | 2016-05-24 09:21:27 +0000 | [diff] [blame] | 1946 | if (fp != NULL) { |
| 1947 | char line[8]; |
Arnaldo Carvalho de Melo | ec80fde | 2011-05-26 09:53:51 -0300 | [diff] [blame] | 1948 | |
Wang Nan | 38272dc | 2016-05-24 09:21:27 +0000 | [diff] [blame] | 1949 | if (fgets(line, sizeof(line), fp) != NULL) |
| 1950 | value = (geteuid() != 0) ? |
| 1951 | (atoi(line) != 0) : |
| 1952 | (atoi(line) == 2); |
Arnaldo Carvalho de Melo | ec80fde | 2011-05-26 09:53:51 -0300 | [diff] [blame] | 1953 | |
Wang Nan | 38272dc | 2016-05-24 09:21:27 +0000 | [diff] [blame] | 1954 | fclose(fp); |
Arnaldo Carvalho de Melo | ec80fde | 2011-05-26 09:53:51 -0300 | [diff] [blame] | 1955 | } |
| 1956 | |
| 1957 | return value; |
| 1958 | } |
| 1959 | |
Arnaldo Carvalho de Melo | b01141f | 2016-08-25 16:09:21 -0300 | [diff] [blame] | 1960 | int symbol__annotation_init(void) |
| 1961 | { |
| 1962 | if (symbol_conf.initialized) { |
| 1963 | pr_err("Annotation needs to be init before symbol__init()\n"); |
| 1964 | return -1; |
| 1965 | } |
| 1966 | |
| 1967 | if (symbol_conf.init_annotation) { |
| 1968 | pr_warning("Annotation being initialized multiple times\n"); |
| 1969 | return 0; |
| 1970 | } |
| 1971 | |
| 1972 | symbol_conf.priv_size += sizeof(struct annotation); |
| 1973 | symbol_conf.init_annotation = true; |
| 1974 | return 0; |
| 1975 | } |
| 1976 | |
Kan Liang | ce80d3b | 2015-08-28 05:48:04 -0400 | [diff] [blame] | 1977 | int symbol__init(struct perf_env *env) |
Arnaldo Carvalho de Melo | cc612d8 | 2009-11-23 16:39:10 -0200 | [diff] [blame] | 1978 | { |
David Ahern | ec5761e | 2010-12-09 13:27:07 -0700 | [diff] [blame] | 1979 | const char *symfs; |
| 1980 | |
Jovi Zhang | 85e00b5 | 2010-09-09 13:30:59 -0300 | [diff] [blame] | 1981 | if (symbol_conf.initialized) |
| 1982 | return 0; |
| 1983 | |
Irina Tirdea | 9ac3e48 | 2012-09-11 01:15:01 +0300 | [diff] [blame] | 1984 | 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] | 1985 | |
Namhyung Kim | 166ccc9 | 2012-08-06 13:41:19 +0900 | [diff] [blame] | 1986 | symbol__elf_init(); |
| 1987 | |
Arnaldo Carvalho de Melo | 75be6cf | 2009-12-15 20:04:39 -0200 | [diff] [blame] | 1988 | if (symbol_conf.sort_by_name) |
| 1989 | symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) - |
| 1990 | sizeof(struct symbol)); |
Arnaldo Carvalho de Melo | b32d133 | 2009-11-24 12:05:15 -0200 | [diff] [blame] | 1991 | |
Namhyung Kim | 0a7e6d1 | 2014-08-12 15:40:45 +0900 | [diff] [blame] | 1992 | if (symbol_conf.try_vmlinux_path && vmlinux_path__init(env) < 0) |
Arnaldo Carvalho de Melo | cc612d8 | 2009-11-23 16:39:10 -0200 | [diff] [blame] | 1993 | return -1; |
| 1994 | |
Arnaldo Carvalho de Melo | c410a33 | 2009-12-15 20:04:41 -0200 | [diff] [blame] | 1995 | if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') { |
| 1996 | pr_err("'.' is the only non valid --field-separator argument\n"); |
| 1997 | return -1; |
| 1998 | } |
| 1999 | |
Arnaldo Carvalho de Melo | 655000e | 2009-12-15 20:04:40 -0200 | [diff] [blame] | 2000 | if (setup_list(&symbol_conf.dso_list, |
| 2001 | symbol_conf.dso_list_str, "dso") < 0) |
| 2002 | return -1; |
| 2003 | |
| 2004 | if (setup_list(&symbol_conf.comm_list, |
| 2005 | symbol_conf.comm_list_str, "comm") < 0) |
| 2006 | goto out_free_dso_list; |
| 2007 | |
David Ahern | e03eaa4 | 2015-03-24 09:52:41 -0600 | [diff] [blame] | 2008 | if (setup_intlist(&symbol_conf.pid_list, |
| 2009 | symbol_conf.pid_list_str, "pid") < 0) |
| 2010 | goto out_free_comm_list; |
| 2011 | |
| 2012 | if (setup_intlist(&symbol_conf.tid_list, |
| 2013 | symbol_conf.tid_list_str, "tid") < 0) |
| 2014 | goto out_free_pid_list; |
| 2015 | |
Arnaldo Carvalho de Melo | 655000e | 2009-12-15 20:04:40 -0200 | [diff] [blame] | 2016 | if (setup_list(&symbol_conf.sym_list, |
| 2017 | symbol_conf.sym_list_str, "symbol") < 0) |
David Ahern | e03eaa4 | 2015-03-24 09:52:41 -0600 | [diff] [blame] | 2018 | goto out_free_tid_list; |
Arnaldo Carvalho de Melo | 655000e | 2009-12-15 20:04:40 -0200 | [diff] [blame] | 2019 | |
David Ahern | ec5761e | 2010-12-09 13:27:07 -0700 | [diff] [blame] | 2020 | /* |
| 2021 | * A path to symbols of "/" is identical to "" |
| 2022 | * reset here for simplicity. |
| 2023 | */ |
| 2024 | symfs = realpath(symbol_conf.symfs, NULL); |
| 2025 | if (symfs == NULL) |
| 2026 | symfs = symbol_conf.symfs; |
| 2027 | if (strcmp(symfs, "/") == 0) |
| 2028 | symbol_conf.symfs = ""; |
| 2029 | if (symfs != symbol_conf.symfs) |
| 2030 | free((void *)symfs); |
| 2031 | |
Arnaldo Carvalho de Melo | ec80fde | 2011-05-26 09:53:51 -0300 | [diff] [blame] | 2032 | symbol_conf.kptr_restrict = symbol__read_kptr_restrict(); |
| 2033 | |
Jovi Zhang | 85e00b5 | 2010-09-09 13:30:59 -0300 | [diff] [blame] | 2034 | symbol_conf.initialized = true; |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 2035 | return 0; |
Arnaldo Carvalho de Melo | 655000e | 2009-12-15 20:04:40 -0200 | [diff] [blame] | 2036 | |
David Ahern | e03eaa4 | 2015-03-24 09:52:41 -0600 | [diff] [blame] | 2037 | out_free_tid_list: |
| 2038 | intlist__delete(symbol_conf.tid_list); |
| 2039 | out_free_pid_list: |
| 2040 | intlist__delete(symbol_conf.pid_list); |
Arnaldo Carvalho de Melo | 655000e | 2009-12-15 20:04:40 -0200 | [diff] [blame] | 2041 | out_free_comm_list: |
| 2042 | strlist__delete(symbol_conf.comm_list); |
Namhyung Kim | d74c896 | 2011-12-13 00:16:52 +0900 | [diff] [blame] | 2043 | out_free_dso_list: |
| 2044 | strlist__delete(symbol_conf.dso_list); |
Arnaldo Carvalho de Melo | 655000e | 2009-12-15 20:04:40 -0200 | [diff] [blame] | 2045 | return -1; |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 2046 | } |
Arnaldo Carvalho de Melo | cc612d8 | 2009-11-23 16:39:10 -0200 | [diff] [blame] | 2047 | |
Arnaldo Carvalho de Melo | d65a458 | 2010-07-30 18:31:28 -0300 | [diff] [blame] | 2048 | void symbol__exit(void) |
| 2049 | { |
Jovi Zhang | 85e00b5 | 2010-09-09 13:30:59 -0300 | [diff] [blame] | 2050 | if (!symbol_conf.initialized) |
| 2051 | return; |
Arnaldo Carvalho de Melo | d65a458 | 2010-07-30 18:31:28 -0300 | [diff] [blame] | 2052 | strlist__delete(symbol_conf.sym_list); |
| 2053 | strlist__delete(symbol_conf.dso_list); |
| 2054 | strlist__delete(symbol_conf.comm_list); |
David Ahern | e03eaa4 | 2015-03-24 09:52:41 -0600 | [diff] [blame] | 2055 | intlist__delete(symbol_conf.tid_list); |
| 2056 | intlist__delete(symbol_conf.pid_list); |
Arnaldo Carvalho de Melo | d65a458 | 2010-07-30 18:31:28 -0300 | [diff] [blame] | 2057 | vmlinux_path__exit(); |
| 2058 | 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] | 2059 | symbol_conf.initialized = false; |
Arnaldo Carvalho de Melo | d65a458 | 2010-07-30 18:31:28 -0300 | [diff] [blame] | 2060 | } |
He Kuang | a706670 | 2016-05-19 11:47:37 +0000 | [diff] [blame] | 2061 | |
| 2062 | int symbol__config_symfs(const struct option *opt __maybe_unused, |
| 2063 | const char *dir, int unset __maybe_unused) |
| 2064 | { |
| 2065 | char *bf = NULL; |
| 2066 | int ret; |
| 2067 | |
| 2068 | symbol_conf.symfs = strdup(dir); |
| 2069 | if (symbol_conf.symfs == NULL) |
| 2070 | return -ENOMEM; |
| 2071 | |
| 2072 | /* skip the locally configured cache if a symfs is given, and |
| 2073 | * config buildid dir to symfs/.debug |
| 2074 | */ |
| 2075 | ret = asprintf(&bf, "%s/%s", dir, ".debug"); |
| 2076 | if (ret < 0) |
| 2077 | return -ENOMEM; |
| 2078 | |
| 2079 | set_buildid_dir(bf); |
| 2080 | |
| 2081 | free(bf); |
| 2082 | return 0; |
| 2083 | } |