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