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