Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1 | #include "util.h" |
| 2 | #include "../perf.h" |
Arnaldo Carvalho de Melo | a0055ae | 2009-06-01 17:50:19 -0300 | [diff] [blame] | 3 | #include "string.h" |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 4 | #include "symbol.h" |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 5 | #include "thread.h" |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 6 | |
Frederic Weisbecker | 8f28827 | 2009-08-16 22:05:48 +0200 | [diff] [blame] | 7 | #include "debug.h" |
| 8 | |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 9 | #include <libelf.h> |
| 10 | #include <gelf.h> |
| 11 | #include <elf.h> |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 12 | #include <sys/utsname.h> |
Peter Zijlstra | 2cdbc46 | 2009-08-05 14:05:16 +0200 | [diff] [blame] | 13 | |
Arnaldo Carvalho de Melo | 94cb9e3 | 2009-08-06 14:43:17 -0300 | [diff] [blame] | 14 | enum dso_origin { |
| 15 | DSO__ORIG_KERNEL = 0, |
| 16 | DSO__ORIG_JAVA_JIT, |
| 17 | DSO__ORIG_FEDORA, |
| 18 | DSO__ORIG_UBUNTU, |
| 19 | DSO__ORIG_BUILDID, |
| 20 | DSO__ORIG_DSO, |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 21 | DSO__ORIG_KMODULE, |
Arnaldo Carvalho de Melo | 94cb9e3 | 2009-08-06 14:43:17 -0300 | [diff] [blame] | 22 | DSO__ORIG_NOT_FOUND, |
| 23 | }; |
| 24 | |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 25 | static void dsos__add(struct dso *dso); |
| 26 | static struct dso *dsos__find(const char *name); |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 27 | static struct map *map__new2(u64 start, struct dso *dso); |
| 28 | static void kernel_maps__insert(struct map *map); |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 29 | unsigned int symbol__priv_size; |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 30 | |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 31 | static struct rb_root kernel_maps; |
| 32 | |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 33 | static void dso__fixup_sym_end(struct dso *self) |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 34 | { |
| 35 | struct rb_node *nd, *prevnd = rb_first(&self->syms); |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 36 | struct symbol *curr, *prev; |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 37 | |
| 38 | if (prevnd == NULL) |
| 39 | return; |
| 40 | |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 41 | curr = rb_entry(prevnd, struct symbol, rb_node); |
| 42 | |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 43 | for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) { |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 44 | prev = curr; |
| 45 | curr = rb_entry(nd, struct symbol, rb_node); |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 46 | |
| 47 | if (prev->end == prev->start) |
| 48 | prev->end = curr->start - 1; |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 49 | } |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 50 | |
| 51 | /* Last entry */ |
| 52 | if (curr->end == curr->start) |
| 53 | curr->end = roundup(curr->start, 4096); |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 54 | } |
| 55 | |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 56 | static void kernel_maps__fixup_end(void) |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 57 | { |
| 58 | struct map *prev, *curr; |
| 59 | struct rb_node *nd, *prevnd = rb_first(&kernel_maps); |
| 60 | |
| 61 | if (prevnd == NULL) |
| 62 | return; |
| 63 | |
| 64 | curr = rb_entry(prevnd, struct map, rb_node); |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 65 | |
| 66 | for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) { |
| 67 | prev = curr; |
| 68 | curr = rb_entry(nd, struct map, rb_node); |
| 69 | prev->end = curr->start - 1; |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | nd = rb_last(&curr->dso->syms); |
| 73 | if (nd) { |
| 74 | struct symbol *sym = rb_entry(nd, struct symbol, rb_node); |
| 75 | curr->end = sym->end; |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 79 | static struct symbol *symbol__new(u64 start, u64 len, const char *name) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 80 | { |
Arnaldo Carvalho de Melo | 0085c954 | 2009-05-28 14:55:13 -0300 | [diff] [blame] | 81 | size_t namelen = strlen(name) + 1; |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 82 | struct symbol *self = calloc(1, (symbol__priv_size + |
| 83 | sizeof(*self) + namelen)); |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 84 | if (!self) |
| 85 | return NULL; |
| 86 | |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 87 | if (symbol__priv_size) { |
| 88 | memset(self, 0, symbol__priv_size); |
| 89 | self = ((void *)self) + symbol__priv_size; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 90 | } |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 91 | self->start = start; |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 92 | self->end = len ? start + len - 1 : start; |
Arnaldo Carvalho de Melo | e420499 | 2009-10-20 14:25:40 -0200 | [diff] [blame] | 93 | |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 94 | pr_debug3("%s: %s %#Lx-%#Lx\n", __func__, name, start, self->end); |
Arnaldo Carvalho de Melo | e420499 | 2009-10-20 14:25:40 -0200 | [diff] [blame] | 95 | |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 96 | memcpy(self->name, name, namelen); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 97 | |
| 98 | return self; |
| 99 | } |
| 100 | |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 101 | static void symbol__delete(struct symbol *self) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 102 | { |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 103 | free(((void *)self) - symbol__priv_size); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | static size_t symbol__fprintf(struct symbol *self, FILE *fp) |
| 107 | { |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 108 | return fprintf(fp, " %llx-%llx %s\n", |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 109 | self->start, self->end, self->name); |
| 110 | } |
| 111 | |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 112 | struct dso *dso__new(const char *name) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 113 | { |
| 114 | struct dso *self = malloc(sizeof(*self) + strlen(name) + 1); |
| 115 | |
| 116 | if (self != NULL) { |
| 117 | strcpy(self->name, name); |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 118 | self->long_name = self->name; |
| 119 | self->short_name = self->name; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 120 | self->syms = RB_ROOT; |
Peter Zijlstra | fc54db5 | 2009-06-05 14:04:59 +0200 | [diff] [blame] | 121 | self->find_symbol = dso__find_symbol; |
Arnaldo Carvalho de Melo | 52d422d | 2009-07-10 22:47:28 -0300 | [diff] [blame] | 122 | self->slen_calculated = 0; |
Arnaldo Carvalho de Melo | 94cb9e3 | 2009-08-06 14:43:17 -0300 | [diff] [blame] | 123 | self->origin = DSO__ORIG_NOT_FOUND; |
Arnaldo Carvalho de Melo | 8d06367 | 2009-11-04 18:50:43 -0200 | [diff] [blame] | 124 | self->loaded = 0; |
| 125 | self->has_build_id = 0; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | return self; |
| 129 | } |
| 130 | |
| 131 | static void dso__delete_symbols(struct dso *self) |
| 132 | { |
| 133 | struct symbol *pos; |
| 134 | struct rb_node *next = rb_first(&self->syms); |
| 135 | |
| 136 | while (next) { |
| 137 | pos = rb_entry(next, struct symbol, rb_node); |
| 138 | next = rb_next(&pos->rb_node); |
Arnaldo Carvalho de Melo | c8c9652 | 2009-06-01 17:50:57 -0300 | [diff] [blame] | 139 | rb_erase(&pos->rb_node, &self->syms); |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 140 | symbol__delete(pos); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
| 144 | void dso__delete(struct dso *self) |
| 145 | { |
| 146 | dso__delete_symbols(self); |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 147 | if (self->long_name != self->name) |
| 148 | free(self->long_name); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 149 | free(self); |
| 150 | } |
| 151 | |
Arnaldo Carvalho de Melo | 8d06367 | 2009-11-04 18:50:43 -0200 | [diff] [blame] | 152 | void dso__set_build_id(struct dso *self, void *build_id) |
| 153 | { |
| 154 | memcpy(self->build_id, build_id, sizeof(self->build_id)); |
| 155 | self->has_build_id = 1; |
| 156 | } |
| 157 | |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 158 | static void dso__insert_symbol(struct dso *self, struct symbol *sym) |
| 159 | { |
| 160 | struct rb_node **p = &self->syms.rb_node; |
| 161 | struct rb_node *parent = NULL; |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 162 | const u64 ip = sym->start; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 163 | struct symbol *s; |
| 164 | |
| 165 | while (*p != NULL) { |
| 166 | parent = *p; |
| 167 | s = rb_entry(parent, struct symbol, rb_node); |
| 168 | if (ip < s->start) |
| 169 | p = &(*p)->rb_left; |
| 170 | else |
| 171 | p = &(*p)->rb_right; |
| 172 | } |
| 173 | rb_link_node(&sym->rb_node, parent, p); |
| 174 | rb_insert_color(&sym->rb_node, &self->syms); |
| 175 | } |
| 176 | |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 177 | struct symbol *dso__find_symbol(struct dso *self, u64 ip) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 178 | { |
| 179 | struct rb_node *n; |
| 180 | |
| 181 | if (self == NULL) |
| 182 | return NULL; |
| 183 | |
| 184 | n = self->syms.rb_node; |
| 185 | |
| 186 | while (n) { |
| 187 | struct symbol *s = rb_entry(n, struct symbol, rb_node); |
| 188 | |
| 189 | if (ip < s->start) |
| 190 | n = n->rb_left; |
| 191 | else if (ip > s->end) |
| 192 | n = n->rb_right; |
| 193 | else |
| 194 | return s; |
| 195 | } |
| 196 | |
| 197 | return NULL; |
| 198 | } |
| 199 | |
Arnaldo Carvalho de Melo | 8d06367 | 2009-11-04 18:50:43 -0200 | [diff] [blame] | 200 | int build_id__sprintf(u8 *self, int len, char *bf) |
| 201 | { |
| 202 | char *bid = bf; |
| 203 | u8 *raw = self; |
| 204 | int i; |
| 205 | |
| 206 | for (i = 0; i < len; ++i) { |
| 207 | sprintf(bid, "%02x", *raw); |
| 208 | ++raw; |
| 209 | bid += 2; |
| 210 | } |
| 211 | |
| 212 | return raw - self; |
| 213 | } |
| 214 | |
Arnaldo Carvalho de Melo | 9e03eb2 | 2009-11-16 16:32:44 -0200 | [diff] [blame] | 215 | size_t dso__fprintf_buildid(struct dso *self, FILE *fp) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 216 | { |
Arnaldo Carvalho de Melo | 8d06367 | 2009-11-04 18:50:43 -0200 | [diff] [blame] | 217 | char sbuild_id[BUILD_ID_SIZE * 2 + 1]; |
Arnaldo Carvalho de Melo | 8d06367 | 2009-11-04 18:50:43 -0200 | [diff] [blame] | 218 | |
| 219 | build_id__sprintf(self->build_id, sizeof(self->build_id), sbuild_id); |
Arnaldo Carvalho de Melo | 9e03eb2 | 2009-11-16 16:32:44 -0200 | [diff] [blame] | 220 | return fprintf(fp, "%s", sbuild_id); |
| 221 | } |
| 222 | |
| 223 | size_t dso__fprintf(struct dso *self, FILE *fp) |
| 224 | { |
| 225 | struct rb_node *nd; |
| 226 | size_t ret = fprintf(fp, "dso: %s (", self->short_name); |
| 227 | |
| 228 | ret += dso__fprintf_buildid(self, fp); |
| 229 | ret += fprintf(fp, ")\n"); |
Arnaldo Carvalho de Melo | 8d06367 | 2009-11-04 18:50:43 -0200 | [diff] [blame] | 230 | |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 231 | for (nd = rb_first(&self->syms); nd; nd = rb_next(nd)) { |
| 232 | struct symbol *pos = rb_entry(nd, struct symbol, rb_node); |
| 233 | ret += symbol__fprintf(pos, fp); |
| 234 | } |
| 235 | |
| 236 | return ret; |
| 237 | } |
| 238 | |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 239 | /* |
| 240 | * Loads the function entries in /proc/kallsyms into kernel_map->dso, |
| 241 | * so that we can in the next step set the symbol ->end address and then |
| 242 | * call kernel_maps__split_kallsyms. |
| 243 | */ |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 244 | static int kernel_maps__load_all_kallsyms(void) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 245 | { |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 246 | char *line = NULL; |
| 247 | size_t n; |
| 248 | FILE *file = fopen("/proc/kallsyms", "r"); |
| 249 | |
| 250 | if (file == NULL) |
| 251 | goto out_failure; |
| 252 | |
| 253 | while (!feof(file)) { |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 254 | u64 start; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 255 | struct symbol *sym; |
| 256 | int line_len, len; |
| 257 | char symbol_type; |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 258 | char *symbol_name; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 259 | |
| 260 | line_len = getline(&line, &n, file); |
| 261 | if (line_len < 0) |
| 262 | break; |
| 263 | |
| 264 | if (!line) |
| 265 | goto out_failure; |
| 266 | |
| 267 | line[--line_len] = '\0'; /* \n */ |
| 268 | |
Arnaldo Carvalho de Melo | a0055ae | 2009-06-01 17:50:19 -0300 | [diff] [blame] | 269 | len = hex2u64(line, &start); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 270 | |
| 271 | len++; |
| 272 | if (len + 2 >= line_len) |
| 273 | continue; |
| 274 | |
| 275 | symbol_type = toupper(line[len]); |
| 276 | /* |
| 277 | * We're interested only in code ('T'ext) |
| 278 | */ |
| 279 | if (symbol_type != 'T' && symbol_type != 'W') |
| 280 | continue; |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 281 | |
| 282 | symbol_name = line + len + 2; |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 283 | /* |
| 284 | * Will fix up the end later, when we have all symbols sorted. |
| 285 | */ |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 286 | sym = symbol__new(start, 0, symbol_name); |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 287 | |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 288 | if (sym == NULL) |
| 289 | goto out_delete_line; |
| 290 | |
Arnaldo Carvalho de Melo | 8216416 | 2009-11-16 13:48:11 -0200 | [diff] [blame] | 291 | /* |
| 292 | * We will pass the symbols to the filter later, in |
| 293 | * kernel_maps__split_kallsyms, when we have split the |
| 294 | * maps per module |
| 295 | */ |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 296 | dso__insert_symbol(kernel_map->dso, sym); |
| 297 | } |
| 298 | |
| 299 | free(line); |
| 300 | fclose(file); |
| 301 | |
| 302 | return 0; |
| 303 | |
| 304 | out_delete_line: |
| 305 | free(line); |
| 306 | out_failure: |
| 307 | return -1; |
| 308 | } |
| 309 | |
| 310 | /* |
| 311 | * Split the symbols into maps, making sure there are no overlaps, i.e. the |
| 312 | * kernel range is broken in several maps, named [kernel].N, as we don't have |
| 313 | * the original ELF section names vmlinux have. |
| 314 | */ |
| 315 | static int kernel_maps__split_kallsyms(symbol_filter_t filter, int use_modules) |
| 316 | { |
| 317 | struct map *map = kernel_map; |
| 318 | struct symbol *pos; |
| 319 | int count = 0; |
| 320 | struct rb_node *next = rb_first(&kernel_map->dso->syms); |
| 321 | int kernel_range = 0; |
| 322 | |
| 323 | while (next) { |
| 324 | char *module; |
| 325 | |
| 326 | pos = rb_entry(next, struct symbol, rb_node); |
| 327 | next = rb_next(&pos->rb_node); |
| 328 | |
| 329 | module = strchr(pos->name, '\t'); |
| 330 | if (module) { |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 331 | if (!use_modules) |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 332 | goto delete_symbol; |
| 333 | |
| 334 | *module++ = '\0'; |
| 335 | |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 336 | if (strcmp(map->dso->name, module)) { |
| 337 | map = kernel_maps__find_by_dso_name(module); |
| 338 | if (!map) { |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 339 | pr_err("/proc/{kallsyms,modules} " |
| 340 | "inconsistency!\n"); |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 341 | return -1; |
| 342 | } |
| 343 | } |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 344 | /* |
| 345 | * So that we look just like we get from .ko files, |
| 346 | * i.e. not prelinked, relative to map->start. |
| 347 | */ |
| 348 | pos->start = map->map_ip(map, pos->start); |
| 349 | pos->end = map->map_ip(map, pos->end); |
| 350 | } else if (map != kernel_map) { |
| 351 | char dso_name[PATH_MAX]; |
| 352 | struct dso *dso; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 353 | |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 354 | snprintf(dso_name, sizeof(dso_name), "[kernel].%d", |
| 355 | kernel_range++); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 356 | |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 357 | dso = dso__new(dso_name); |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 358 | if (dso == NULL) |
| 359 | return -1; |
| 360 | |
| 361 | map = map__new2(pos->start, dso); |
| 362 | if (map == NULL) { |
| 363 | dso__delete(dso); |
| 364 | return -1; |
| 365 | } |
| 366 | |
Arnaldo Carvalho de Melo | ed52ce2 | 2009-10-19 17:17:57 -0200 | [diff] [blame] | 367 | map->map_ip = map->unmap_ip = identity__map_ip; |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 368 | kernel_maps__insert(map); |
| 369 | ++kernel_range; |
| 370 | } |
| 371 | |
| 372 | if (filter && filter(map, pos)) { |
| 373 | delete_symbol: |
| 374 | rb_erase(&pos->rb_node, &kernel_map->dso->syms); |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 375 | symbol__delete(pos); |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 376 | } else { |
| 377 | if (map != kernel_map) { |
| 378 | rb_erase(&pos->rb_node, &kernel_map->dso->syms); |
| 379 | dso__insert_symbol(map->dso, pos); |
| 380 | } |
Mike Galbraith | 9974f49 | 2009-07-02 08:05:58 +0200 | [diff] [blame] | 381 | count++; |
| 382 | } |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 383 | } |
| 384 | |
Mike Galbraith | 9974f49 | 2009-07-02 08:05:58 +0200 | [diff] [blame] | 385 | return count; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 386 | } |
| 387 | |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 388 | |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 389 | static int kernel_maps__load_kallsyms(symbol_filter_t filter, int use_modules) |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 390 | { |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 391 | if (kernel_maps__load_all_kallsyms()) |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 392 | return -1; |
| 393 | |
| 394 | dso__fixup_sym_end(kernel_map->dso); |
| 395 | |
| 396 | return kernel_maps__split_kallsyms(filter, use_modules); |
| 397 | } |
| 398 | |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 399 | static size_t kernel_maps__fprintf(FILE *fp) |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 400 | { |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 401 | size_t printed = fprintf(fp, "Kernel maps:\n"); |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 402 | struct rb_node *nd; |
| 403 | |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 404 | for (nd = rb_first(&kernel_maps); nd; nd = rb_next(nd)) { |
| 405 | struct map *pos = rb_entry(nd, struct map, rb_node); |
| 406 | |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 407 | printed += fprintf(fp, "Map:"); |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 408 | printed += map__fprintf(pos, fp); |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 409 | if (verbose > 1) { |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 410 | printed += dso__fprintf(pos->dso, fp); |
| 411 | printed += fprintf(fp, "--\n"); |
| 412 | } |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 413 | } |
| 414 | |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 415 | return printed + fprintf(fp, "END kernel maps\n"); |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 416 | } |
| 417 | |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 418 | static int dso__load_perf_map(struct dso *self, struct map *map, |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 419 | symbol_filter_t filter) |
Pekka Enberg | 80d496b | 2009-06-08 21:12:48 +0300 | [diff] [blame] | 420 | { |
| 421 | char *line = NULL; |
| 422 | size_t n; |
| 423 | FILE *file; |
| 424 | int nr_syms = 0; |
| 425 | |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 426 | file = fopen(self->long_name, "r"); |
Pekka Enberg | 80d496b | 2009-06-08 21:12:48 +0300 | [diff] [blame] | 427 | if (file == NULL) |
| 428 | goto out_failure; |
| 429 | |
| 430 | while (!feof(file)) { |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 431 | u64 start, size; |
Pekka Enberg | 80d496b | 2009-06-08 21:12:48 +0300 | [diff] [blame] | 432 | struct symbol *sym; |
| 433 | int line_len, len; |
| 434 | |
| 435 | line_len = getline(&line, &n, file); |
| 436 | if (line_len < 0) |
| 437 | break; |
| 438 | |
| 439 | if (!line) |
| 440 | goto out_failure; |
| 441 | |
| 442 | line[--line_len] = '\0'; /* \n */ |
| 443 | |
| 444 | len = hex2u64(line, &start); |
| 445 | |
| 446 | len++; |
| 447 | if (len + 2 >= line_len) |
| 448 | continue; |
| 449 | |
| 450 | len += hex2u64(line + len, &size); |
| 451 | |
| 452 | len++; |
| 453 | if (len + 2 >= line_len) |
| 454 | continue; |
| 455 | |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 456 | sym = symbol__new(start, size, line + len); |
Pekka Enberg | 80d496b | 2009-06-08 21:12:48 +0300 | [diff] [blame] | 457 | |
| 458 | if (sym == NULL) |
| 459 | goto out_delete_line; |
| 460 | |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 461 | if (filter && filter(map, sym)) |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 462 | symbol__delete(sym); |
Pekka Enberg | 80d496b | 2009-06-08 21:12:48 +0300 | [diff] [blame] | 463 | else { |
| 464 | dso__insert_symbol(self, sym); |
| 465 | nr_syms++; |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | free(line); |
| 470 | fclose(file); |
| 471 | |
| 472 | return nr_syms; |
| 473 | |
| 474 | out_delete_line: |
| 475 | free(line); |
| 476 | out_failure: |
| 477 | return -1; |
| 478 | } |
| 479 | |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 480 | /** |
| 481 | * elf_symtab__for_each_symbol - iterate thru all the symbols |
| 482 | * |
| 483 | * @self: struct elf_symtab instance to iterate |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 484 | * @idx: uint32_t idx |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 485 | * @sym: GElf_Sym iterator |
| 486 | */ |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 487 | #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \ |
| 488 | for (idx = 0, gelf_getsym(syms, idx, &sym);\ |
| 489 | idx < nr_syms; \ |
| 490 | idx++, gelf_getsym(syms, idx, &sym)) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 491 | |
| 492 | static inline uint8_t elf_sym__type(const GElf_Sym *sym) |
| 493 | { |
| 494 | return GELF_ST_TYPE(sym->st_info); |
| 495 | } |
| 496 | |
| 497 | static inline int elf_sym__is_function(const GElf_Sym *sym) |
| 498 | { |
| 499 | return elf_sym__type(sym) == STT_FUNC && |
| 500 | sym->st_name != 0 && |
Arnaldo Carvalho de Melo | 8183313 | 2009-10-05 23:35:03 -0300 | [diff] [blame] | 501 | sym->st_shndx != SHN_UNDEF; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 502 | } |
| 503 | |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 504 | static inline int elf_sym__is_label(const GElf_Sym *sym) |
| 505 | { |
| 506 | return elf_sym__type(sym) == STT_NOTYPE && |
| 507 | sym->st_name != 0 && |
| 508 | sym->st_shndx != SHN_UNDEF && |
| 509 | sym->st_shndx != SHN_ABS; |
| 510 | } |
| 511 | |
| 512 | static inline const char *elf_sec__name(const GElf_Shdr *shdr, |
| 513 | const Elf_Data *secstrs) |
| 514 | { |
| 515 | return secstrs->d_buf + shdr->sh_name; |
| 516 | } |
| 517 | |
| 518 | static inline int elf_sec__is_text(const GElf_Shdr *shdr, |
| 519 | const Elf_Data *secstrs) |
| 520 | { |
| 521 | return strstr(elf_sec__name(shdr, secstrs), "text") != NULL; |
| 522 | } |
| 523 | |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 524 | static inline const char *elf_sym__name(const GElf_Sym *sym, |
| 525 | const Elf_Data *symstrs) |
| 526 | { |
| 527 | return symstrs->d_buf + sym->st_name; |
| 528 | } |
| 529 | |
| 530 | static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep, |
| 531 | GElf_Shdr *shp, const char *name, |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 532 | size_t *idx) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 533 | { |
| 534 | Elf_Scn *sec = NULL; |
| 535 | size_t cnt = 1; |
| 536 | |
| 537 | while ((sec = elf_nextscn(elf, sec)) != NULL) { |
| 538 | char *str; |
| 539 | |
| 540 | gelf_getshdr(sec, shp); |
| 541 | str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name); |
| 542 | if (!strcmp(name, str)) { |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 543 | if (idx) |
| 544 | *idx = cnt; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 545 | break; |
| 546 | } |
| 547 | ++cnt; |
| 548 | } |
| 549 | |
| 550 | return sec; |
| 551 | } |
| 552 | |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 553 | #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \ |
| 554 | for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \ |
| 555 | idx < nr_entries; \ |
| 556 | ++idx, pos = gelf_getrel(reldata, idx, &pos_mem)) |
| 557 | |
| 558 | #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \ |
| 559 | for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \ |
| 560 | idx < nr_entries; \ |
| 561 | ++idx, pos = gelf_getrela(reldata, idx, &pos_mem)) |
| 562 | |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 563 | /* |
| 564 | * We need to check if we have a .dynsym, so that we can handle the |
| 565 | * .plt, synthesizing its symbols, that aren't on the symtabs (be it |
| 566 | * .dynsym or .symtab). |
| 567 | * And always look at the original dso, not at debuginfo packages, that |
| 568 | * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS). |
| 569 | */ |
Arnaldo Carvalho de Melo | 8216416 | 2009-11-16 13:48:11 -0200 | [diff] [blame] | 570 | static int dso__synthesize_plt_symbols(struct dso *self, struct map *map, |
| 571 | symbol_filter_t filter) |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 572 | { |
| 573 | uint32_t nr_rel_entries, idx; |
| 574 | GElf_Sym sym; |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 575 | u64 plt_offset; |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 576 | GElf_Shdr shdr_plt; |
| 577 | struct symbol *f; |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 578 | GElf_Shdr shdr_rel_plt, shdr_dynsym; |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 579 | Elf_Data *reldata, *syms, *symstrs; |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 580 | Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym; |
| 581 | size_t dynsym_idx; |
| 582 | GElf_Ehdr ehdr; |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 583 | char sympltname[1024]; |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 584 | Elf *elf; |
| 585 | int nr = 0, symidx, fd, err = 0; |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 586 | |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 587 | fd = open(self->long_name, O_RDONLY); |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 588 | if (fd < 0) |
| 589 | goto out; |
| 590 | |
Marti Raudsepp | 8408712 | 2009-10-24 19:10:36 +0300 | [diff] [blame] | 591 | elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 592 | if (elf == NULL) |
| 593 | goto out_close; |
| 594 | |
| 595 | if (gelf_getehdr(elf, &ehdr) == NULL) |
| 596 | goto out_elf_end; |
| 597 | |
| 598 | scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym, |
| 599 | ".dynsym", &dynsym_idx); |
| 600 | if (scn_dynsym == NULL) |
| 601 | goto out_elf_end; |
| 602 | |
| 603 | 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] | 604 | ".rela.plt", NULL); |
| 605 | if (scn_plt_rel == NULL) { |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 606 | 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] | 607 | ".rel.plt", NULL); |
| 608 | if (scn_plt_rel == NULL) |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 609 | goto out_elf_end; |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 610 | } |
| 611 | |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 612 | err = -1; |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 613 | |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 614 | if (shdr_rel_plt.sh_link != dynsym_idx) |
| 615 | goto out_elf_end; |
| 616 | |
| 617 | if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL) |
| 618 | goto out_elf_end; |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 619 | |
| 620 | /* |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 621 | * 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] | 622 | * and the symbols in the .dynsym they refer to. |
| 623 | */ |
| 624 | reldata = elf_getdata(scn_plt_rel, NULL); |
| 625 | if (reldata == NULL) |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 626 | goto out_elf_end; |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 627 | |
| 628 | syms = elf_getdata(scn_dynsym, NULL); |
| 629 | if (syms == NULL) |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 630 | goto out_elf_end; |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 631 | |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 632 | scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link); |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 633 | if (scn_symstrs == NULL) |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 634 | goto out_elf_end; |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 635 | |
| 636 | symstrs = elf_getdata(scn_symstrs, NULL); |
| 637 | if (symstrs == NULL) |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 638 | goto out_elf_end; |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 639 | |
| 640 | nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize; |
| 641 | plt_offset = shdr_plt.sh_offset; |
| 642 | |
| 643 | if (shdr_rel_plt.sh_type == SHT_RELA) { |
| 644 | GElf_Rela pos_mem, *pos; |
| 645 | |
| 646 | elf_section__for_each_rela(reldata, pos, pos_mem, idx, |
| 647 | nr_rel_entries) { |
| 648 | symidx = GELF_R_SYM(pos->r_info); |
| 649 | plt_offset += shdr_plt.sh_entsize; |
| 650 | gelf_getsym(syms, symidx, &sym); |
| 651 | snprintf(sympltname, sizeof(sympltname), |
| 652 | "%s@plt", elf_sym__name(&sym, symstrs)); |
| 653 | |
| 654 | f = symbol__new(plt_offset, shdr_plt.sh_entsize, |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 655 | sympltname); |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 656 | if (!f) |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 657 | goto out_elf_end; |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 658 | |
Arnaldo Carvalho de Melo | 8216416 | 2009-11-16 13:48:11 -0200 | [diff] [blame] | 659 | if (filter && filter(map, f)) |
| 660 | symbol__delete(f); |
| 661 | else { |
| 662 | dso__insert_symbol(self, f); |
| 663 | ++nr; |
| 664 | } |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 665 | } |
| 666 | } else if (shdr_rel_plt.sh_type == SHT_REL) { |
| 667 | GElf_Rel pos_mem, *pos; |
| 668 | elf_section__for_each_rel(reldata, pos, pos_mem, idx, |
| 669 | nr_rel_entries) { |
| 670 | symidx = GELF_R_SYM(pos->r_info); |
| 671 | plt_offset += shdr_plt.sh_entsize; |
| 672 | gelf_getsym(syms, symidx, &sym); |
| 673 | snprintf(sympltname, sizeof(sympltname), |
| 674 | "%s@plt", elf_sym__name(&sym, symstrs)); |
| 675 | |
| 676 | f = symbol__new(plt_offset, shdr_plt.sh_entsize, |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 677 | sympltname); |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 678 | if (!f) |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 679 | goto out_elf_end; |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 680 | |
Arnaldo Carvalho de Melo | 8216416 | 2009-11-16 13:48:11 -0200 | [diff] [blame] | 681 | if (filter && filter(map, f)) |
| 682 | symbol__delete(f); |
| 683 | else { |
| 684 | dso__insert_symbol(self, f); |
| 685 | ++nr; |
| 686 | } |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 687 | } |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 688 | } |
| 689 | |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 690 | err = 0; |
| 691 | out_elf_end: |
| 692 | elf_end(elf); |
| 693 | out_close: |
| 694 | close(fd); |
| 695 | |
| 696 | if (err == 0) |
| 697 | return nr; |
| 698 | out: |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 699 | pr_warning("%s: problems reading %s PLT info.\n", |
| 700 | __func__, self->long_name); |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 701 | return 0; |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 702 | } |
| 703 | |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 704 | static int dso__load_sym(struct dso *self, struct map *map, const char *name, |
| 705 | int fd, symbol_filter_t filter, int kernel, |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 706 | int kmodule) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 707 | { |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 708 | struct map *curr_map = map; |
| 709 | struct dso *curr_dso = self; |
| 710 | size_t dso_name_len = strlen(self->short_name); |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 711 | Elf_Data *symstrs, *secstrs; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 712 | uint32_t nr_syms; |
| 713 | int err = -1; |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 714 | uint32_t idx; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 715 | GElf_Ehdr ehdr; |
| 716 | GElf_Shdr shdr; |
| 717 | Elf_Data *syms; |
| 718 | GElf_Sym sym; |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 719 | Elf_Scn *sec, *sec_strndx; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 720 | Elf *elf; |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 721 | int nr = 0; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 722 | |
Marti Raudsepp | 8408712 | 2009-10-24 19:10:36 +0300 | [diff] [blame] | 723 | elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 724 | if (elf == NULL) { |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 725 | pr_err("%s: cannot read %s ELF file.\n", __func__, name); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 726 | goto out_close; |
| 727 | } |
| 728 | |
| 729 | if (gelf_getehdr(elf, &ehdr) == NULL) { |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 730 | pr_err("%s: cannot get elf header.\n", __func__); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 731 | goto out_elf_end; |
| 732 | } |
| 733 | |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 734 | sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL); |
| 735 | if (sec == NULL) { |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 736 | sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL); |
| 737 | if (sec == NULL) |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 738 | goto out_elf_end; |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 739 | } |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 740 | |
| 741 | syms = elf_getdata(sec, NULL); |
| 742 | if (syms == NULL) |
| 743 | goto out_elf_end; |
| 744 | |
| 745 | sec = elf_getscn(elf, shdr.sh_link); |
| 746 | if (sec == NULL) |
| 747 | goto out_elf_end; |
| 748 | |
| 749 | symstrs = elf_getdata(sec, NULL); |
| 750 | if (symstrs == NULL) |
| 751 | goto out_elf_end; |
| 752 | |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 753 | sec_strndx = elf_getscn(elf, ehdr.e_shstrndx); |
| 754 | if (sec_strndx == NULL) |
| 755 | goto out_elf_end; |
| 756 | |
| 757 | secstrs = elf_getdata(sec_strndx, NULL); |
Stoyan Gaydarov | 9b30a26 | 2009-07-30 05:25:29 -0500 | [diff] [blame] | 758 | if (secstrs == NULL) |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 759 | goto out_elf_end; |
| 760 | |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 761 | nr_syms = shdr.sh_size / shdr.sh_entsize; |
| 762 | |
Arjan van de Ven | e9fbc9d | 2009-06-06 21:22:33 +0200 | [diff] [blame] | 763 | memset(&sym, 0, sizeof(sym)); |
Mike Galbraith | d20ff6b | 2009-07-20 14:01:38 +0200 | [diff] [blame] | 764 | if (!kernel) { |
| 765 | self->adjust_symbols = (ehdr.e_type == ET_EXEC || |
Arnaldo Carvalho de Melo | 30d7a77 | 2009-07-02 21:24:14 -0300 | [diff] [blame] | 766 | elf_section_by_name(elf, &ehdr, &shdr, |
| 767 | ".gnu.prelink_undo", |
| 768 | NULL) != NULL); |
Mike Galbraith | d20ff6b | 2009-07-20 14:01:38 +0200 | [diff] [blame] | 769 | } else self->adjust_symbols = 0; |
| 770 | |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 771 | elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) { |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 772 | struct symbol *f; |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 773 | const char *elf_name; |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 774 | char *demangled = NULL; |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 775 | int is_label = elf_sym__is_label(&sym); |
| 776 | const char *section_name; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 777 | |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 778 | if (!is_label && !elf_sym__is_function(&sym)) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 779 | continue; |
| 780 | |
| 781 | sec = elf_getscn(elf, sym.st_shndx); |
| 782 | if (!sec) |
| 783 | goto out_elf_end; |
| 784 | |
| 785 | gelf_getshdr(sec, &shdr); |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 786 | |
| 787 | if (is_label && !elf_sec__is_text(&shdr, secstrs)) |
| 788 | continue; |
| 789 | |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 790 | elf_name = elf_sym__name(&sym, symstrs); |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 791 | section_name = elf_sec__name(&shdr, secstrs); |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 792 | |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 793 | if (kernel || kmodule) { |
| 794 | char dso_name[PATH_MAX]; |
| 795 | |
| 796 | if (strcmp(section_name, |
| 797 | curr_dso->short_name + dso_name_len) == 0) |
| 798 | goto new_symbol; |
| 799 | |
| 800 | if (strcmp(section_name, ".text") == 0) { |
| 801 | curr_map = map; |
| 802 | curr_dso = self; |
| 803 | goto new_symbol; |
| 804 | } |
| 805 | |
| 806 | snprintf(dso_name, sizeof(dso_name), |
| 807 | "%s%s", self->short_name, section_name); |
| 808 | |
| 809 | curr_map = kernel_maps__find_by_dso_name(dso_name); |
| 810 | if (curr_map == NULL) { |
| 811 | u64 start = sym.st_value; |
| 812 | |
| 813 | if (kmodule) |
| 814 | start += map->start + shdr.sh_offset; |
| 815 | |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 816 | curr_dso = dso__new(dso_name); |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 817 | if (curr_dso == NULL) |
| 818 | goto out_elf_end; |
| 819 | curr_map = map__new2(start, curr_dso); |
| 820 | if (curr_map == NULL) { |
| 821 | dso__delete(curr_dso); |
| 822 | goto out_elf_end; |
| 823 | } |
Arnaldo Carvalho de Melo | ed52ce2 | 2009-10-19 17:17:57 -0200 | [diff] [blame] | 824 | curr_map->map_ip = identity__map_ip; |
| 825 | curr_map->unmap_ip = identity__map_ip; |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 826 | curr_dso->origin = DSO__ORIG_KERNEL; |
| 827 | kernel_maps__insert(curr_map); |
| 828 | dsos__add(curr_dso); |
| 829 | } else |
| 830 | curr_dso = curr_map->dso; |
| 831 | |
| 832 | goto new_symbol; |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 833 | } |
| 834 | |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 835 | if (curr_dso->adjust_symbols) { |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 836 | pr_debug2("adjusting symbol: st_value: %Lx sh_addr: " |
| 837 | "%Lx sh_offset: %Lx\n", (u64)sym.st_value, |
| 838 | (u64)shdr.sh_addr, (u64)shdr.sh_offset); |
Arnaldo Carvalho de Melo | f5812a7 | 2009-06-30 11:43:17 -0300 | [diff] [blame] | 839 | sym.st_value -= shdr.sh_addr - shdr.sh_offset; |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 840 | } |
Arnaldo Carvalho de Melo | 28ac909 | 2009-07-20 14:14:12 -0300 | [diff] [blame] | 841 | /* |
| 842 | * We need to figure out if the object was created from C++ sources |
| 843 | * DWARF DW_compile_unit has this, but we don't always have access |
| 844 | * to it... |
| 845 | */ |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 846 | demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI); |
Arnaldo Carvalho de Melo | 28ac909 | 2009-07-20 14:14:12 -0300 | [diff] [blame] | 847 | if (demangled != NULL) |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 848 | elf_name = demangled; |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 849 | new_symbol: |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 850 | f = symbol__new(sym.st_value, sym.st_size, elf_name); |
Arnaldo Carvalho de Melo | 28ac909 | 2009-07-20 14:14:12 -0300 | [diff] [blame] | 851 | free(demangled); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 852 | if (!f) |
| 853 | goto out_elf_end; |
| 854 | |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 855 | if (filter && filter(curr_map, f)) |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 856 | symbol__delete(f); |
Arnaldo Carvalho de Melo | 69ee69f | 2009-05-28 14:55:26 -0300 | [diff] [blame] | 857 | else { |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 858 | dso__insert_symbol(curr_dso, f); |
Arnaldo Carvalho de Melo | 69ee69f | 2009-05-28 14:55:26 -0300 | [diff] [blame] | 859 | nr++; |
| 860 | } |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 861 | } |
| 862 | |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 863 | /* |
| 864 | * For misannotated, zeroed, ASM function sizes. |
| 865 | */ |
| 866 | if (nr > 0) |
| 867 | dso__fixup_sym_end(self); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 868 | err = nr; |
| 869 | out_elf_end: |
| 870 | elf_end(elf); |
| 871 | out_close: |
| 872 | return err; |
| 873 | } |
| 874 | |
Frederic Weisbecker | 57f395a | 2009-11-11 04:51:04 +0100 | [diff] [blame] | 875 | bool fetch_build_id_table(struct list_head *head) |
| 876 | { |
| 877 | bool have_buildid = false; |
| 878 | struct dso *pos; |
| 879 | |
| 880 | list_for_each_entry(pos, &dsos, node) { |
| 881 | struct build_id_list *new; |
| 882 | struct build_id_event b; |
| 883 | size_t len; |
| 884 | |
| 885 | if (filename__read_build_id(pos->long_name, |
| 886 | &b.build_id, |
| 887 | sizeof(b.build_id)) < 0) |
| 888 | continue; |
| 889 | have_buildid = true; |
| 890 | memset(&b.header, 0, sizeof(b.header)); |
| 891 | len = strlen(pos->long_name) + 1; |
| 892 | len = ALIGN(len, 64); |
| 893 | b.header.size = sizeof(b) + len; |
| 894 | |
| 895 | new = malloc(sizeof(*new)); |
| 896 | if (!new) |
| 897 | die("No memory\n"); |
| 898 | |
| 899 | memcpy(&new->event, &b, sizeof(b)); |
| 900 | new->dso_name = pos->long_name; |
| 901 | new->len = len; |
| 902 | |
| 903 | list_add_tail(&new->list, head); |
| 904 | } |
| 905 | |
| 906 | return have_buildid; |
| 907 | } |
| 908 | |
Arnaldo Carvalho de Melo | 2643ce114 | 2009-11-03 21:46:10 -0200 | [diff] [blame] | 909 | int filename__read_build_id(const char *filename, void *bf, size_t size) |
Arnaldo Carvalho de Melo | 4d1e00a | 2009-08-05 19:02:49 -0300 | [diff] [blame] | 910 | { |
Arnaldo Carvalho de Melo | 2643ce114 | 2009-11-03 21:46:10 -0200 | [diff] [blame] | 911 | int fd, err = -1; |
Arnaldo Carvalho de Melo | 4d1e00a | 2009-08-05 19:02:49 -0300 | [diff] [blame] | 912 | GElf_Ehdr ehdr; |
| 913 | GElf_Shdr shdr; |
| 914 | Elf_Data *build_id_data; |
| 915 | Elf_Scn *sec; |
Arnaldo Carvalho de Melo | 4d1e00a | 2009-08-05 19:02:49 -0300 | [diff] [blame] | 916 | Elf *elf; |
Arnaldo Carvalho de Melo | 4d1e00a | 2009-08-05 19:02:49 -0300 | [diff] [blame] | 917 | |
Arnaldo Carvalho de Melo | 2643ce114 | 2009-11-03 21:46:10 -0200 | [diff] [blame] | 918 | if (size < BUILD_ID_SIZE) |
| 919 | goto out; |
| 920 | |
| 921 | fd = open(filename, O_RDONLY); |
Arnaldo Carvalho de Melo | 4d1e00a | 2009-08-05 19:02:49 -0300 | [diff] [blame] | 922 | if (fd < 0) |
| 923 | goto out; |
| 924 | |
Marti Raudsepp | 8408712 | 2009-10-24 19:10:36 +0300 | [diff] [blame] | 925 | elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); |
Arnaldo Carvalho de Melo | 4d1e00a | 2009-08-05 19:02:49 -0300 | [diff] [blame] | 926 | if (elf == NULL) { |
Arnaldo Carvalho de Melo | 8d06367 | 2009-11-04 18:50:43 -0200 | [diff] [blame] | 927 | pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename); |
Arnaldo Carvalho de Melo | 4d1e00a | 2009-08-05 19:02:49 -0300 | [diff] [blame] | 928 | goto out_close; |
| 929 | } |
| 930 | |
| 931 | if (gelf_getehdr(elf, &ehdr) == NULL) { |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 932 | pr_err("%s: cannot get elf header.\n", __func__); |
Arnaldo Carvalho de Melo | 4d1e00a | 2009-08-05 19:02:49 -0300 | [diff] [blame] | 933 | goto out_elf_end; |
| 934 | } |
| 935 | |
Arnaldo Carvalho de Melo | 2643ce114 | 2009-11-03 21:46:10 -0200 | [diff] [blame] | 936 | sec = elf_section_by_name(elf, &ehdr, &shdr, |
| 937 | ".note.gnu.build-id", NULL); |
Arnaldo Carvalho de Melo | 4d1e00a | 2009-08-05 19:02:49 -0300 | [diff] [blame] | 938 | if (sec == NULL) |
| 939 | goto out_elf_end; |
| 940 | |
| 941 | build_id_data = elf_getdata(sec, NULL); |
| 942 | if (build_id_data == NULL) |
| 943 | goto out_elf_end; |
Arnaldo Carvalho de Melo | 2643ce114 | 2009-11-03 21:46:10 -0200 | [diff] [blame] | 944 | memcpy(bf, build_id_data->d_buf + 16, BUILD_ID_SIZE); |
| 945 | err = BUILD_ID_SIZE; |
| 946 | out_elf_end: |
| 947 | elf_end(elf); |
| 948 | out_close: |
| 949 | close(fd); |
| 950 | out: |
| 951 | return err; |
| 952 | } |
| 953 | |
| 954 | static char *dso__read_build_id(struct dso *self) |
| 955 | { |
Arnaldo Carvalho de Melo | 8d06367 | 2009-11-04 18:50:43 -0200 | [diff] [blame] | 956 | int len; |
| 957 | char *build_id = NULL; |
| 958 | unsigned char rawbf[BUILD_ID_SIZE]; |
Arnaldo Carvalho de Melo | 2643ce114 | 2009-11-03 21:46:10 -0200 | [diff] [blame] | 959 | |
| 960 | len = filename__read_build_id(self->long_name, rawbf, sizeof(rawbf)); |
| 961 | if (len < 0) |
| 962 | goto out; |
| 963 | |
| 964 | build_id = malloc(len * 2 + 1); |
Arnaldo Carvalho de Melo | 4d1e00a | 2009-08-05 19:02:49 -0300 | [diff] [blame] | 965 | if (build_id == NULL) |
Arnaldo Carvalho de Melo | 2643ce114 | 2009-11-03 21:46:10 -0200 | [diff] [blame] | 966 | goto out; |
Arnaldo Carvalho de Melo | 4d1e00a | 2009-08-05 19:02:49 -0300 | [diff] [blame] | 967 | |
Arnaldo Carvalho de Melo | 8d06367 | 2009-11-04 18:50:43 -0200 | [diff] [blame] | 968 | build_id__sprintf(rawbf, len, build_id); |
Arnaldo Carvalho de Melo | 4d1e00a | 2009-08-05 19:02:49 -0300 | [diff] [blame] | 969 | out: |
| 970 | return build_id; |
| 971 | } |
| 972 | |
Arnaldo Carvalho de Melo | 94cb9e3 | 2009-08-06 14:43:17 -0300 | [diff] [blame] | 973 | char dso__symtab_origin(const struct dso *self) |
| 974 | { |
| 975 | static const char origin[] = { |
| 976 | [DSO__ORIG_KERNEL] = 'k', |
| 977 | [DSO__ORIG_JAVA_JIT] = 'j', |
| 978 | [DSO__ORIG_FEDORA] = 'f', |
| 979 | [DSO__ORIG_UBUNTU] = 'u', |
| 980 | [DSO__ORIG_BUILDID] = 'b', |
| 981 | [DSO__ORIG_DSO] = 'd', |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 982 | [DSO__ORIG_KMODULE] = 'K', |
Arnaldo Carvalho de Melo | 94cb9e3 | 2009-08-06 14:43:17 -0300 | [diff] [blame] | 983 | }; |
| 984 | |
| 985 | if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND) |
| 986 | return '!'; |
| 987 | return origin[self->origin]; |
| 988 | } |
| 989 | |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 990 | int dso__load(struct dso *self, struct map *map, symbol_filter_t filter) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 991 | { |
Arnaldo Carvalho de Melo | 4d1e00a | 2009-08-05 19:02:49 -0300 | [diff] [blame] | 992 | int size = PATH_MAX; |
| 993 | char *name = malloc(size), *build_id = NULL; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 994 | int ret = -1; |
| 995 | int fd; |
| 996 | |
Arnaldo Carvalho de Melo | 8d06367 | 2009-11-04 18:50:43 -0200 | [diff] [blame] | 997 | self->loaded = 1; |
Arnaldo Carvalho de Melo | 66bd842 | 2009-10-28 21:51:21 -0200 | [diff] [blame] | 998 | |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 999 | if (!name) |
| 1000 | return -1; |
| 1001 | |
Arnaldo Carvalho de Melo | 30d7a77 | 2009-07-02 21:24:14 -0300 | [diff] [blame] | 1002 | self->adjust_symbols = 0; |
Arnaldo Carvalho de Melo | f5812a7 | 2009-06-30 11:43:17 -0300 | [diff] [blame] | 1003 | |
Arnaldo Carvalho de Melo | 94cb9e3 | 2009-08-06 14:43:17 -0300 | [diff] [blame] | 1004 | if (strncmp(self->name, "/tmp/perf-", 10) == 0) { |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 1005 | ret = dso__load_perf_map(self, map, filter); |
Arnaldo Carvalho de Melo | 94cb9e3 | 2009-08-06 14:43:17 -0300 | [diff] [blame] | 1006 | self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT : |
| 1007 | DSO__ORIG_NOT_FOUND; |
| 1008 | return ret; |
| 1009 | } |
| 1010 | |
| 1011 | self->origin = DSO__ORIG_FEDORA - 1; |
Pekka Enberg | 80d496b | 2009-06-08 21:12:48 +0300 | [diff] [blame] | 1012 | |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1013 | more: |
| 1014 | do { |
Arnaldo Carvalho de Melo | 8d06367 | 2009-11-04 18:50:43 -0200 | [diff] [blame] | 1015 | int berr = 0; |
| 1016 | |
Arnaldo Carvalho de Melo | 94cb9e3 | 2009-08-06 14:43:17 -0300 | [diff] [blame] | 1017 | self->origin++; |
| 1018 | switch (self->origin) { |
| 1019 | case DSO__ORIG_FEDORA: |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1020 | snprintf(name, size, "/usr/lib/debug%s.debug", |
| 1021 | self->long_name); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1022 | break; |
Arnaldo Carvalho de Melo | 94cb9e3 | 2009-08-06 14:43:17 -0300 | [diff] [blame] | 1023 | case DSO__ORIG_UBUNTU: |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1024 | snprintf(name, size, "/usr/lib/debug%s", |
| 1025 | self->long_name); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1026 | break; |
Arnaldo Carvalho de Melo | 94cb9e3 | 2009-08-06 14:43:17 -0300 | [diff] [blame] | 1027 | case DSO__ORIG_BUILDID: |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 1028 | build_id = dso__read_build_id(self); |
Arnaldo Carvalho de Melo | 4d1e00a | 2009-08-05 19:02:49 -0300 | [diff] [blame] | 1029 | if (build_id != NULL) { |
| 1030 | snprintf(name, size, |
| 1031 | "/usr/lib/debug/.build-id/%.2s/%s.debug", |
| 1032 | build_id, build_id + 2); |
Arnaldo Carvalho de Melo | 8d06367 | 2009-11-04 18:50:43 -0200 | [diff] [blame] | 1033 | goto compare_build_id; |
Arnaldo Carvalho de Melo | 4d1e00a | 2009-08-05 19:02:49 -0300 | [diff] [blame] | 1034 | } |
Arnaldo Carvalho de Melo | 94cb9e3 | 2009-08-06 14:43:17 -0300 | [diff] [blame] | 1035 | self->origin++; |
Arnaldo Carvalho de Melo | 4d1e00a | 2009-08-05 19:02:49 -0300 | [diff] [blame] | 1036 | /* Fall thru */ |
Arnaldo Carvalho de Melo | 94cb9e3 | 2009-08-06 14:43:17 -0300 | [diff] [blame] | 1037 | case DSO__ORIG_DSO: |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1038 | snprintf(name, size, "%s", self->long_name); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1039 | break; |
| 1040 | |
| 1041 | default: |
| 1042 | goto out; |
| 1043 | } |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1044 | |
Arnaldo Carvalho de Melo | 8d06367 | 2009-11-04 18:50:43 -0200 | [diff] [blame] | 1045 | if (self->has_build_id) { |
| 1046 | bool match; |
| 1047 | build_id = malloc(BUILD_ID_SIZE); |
| 1048 | if (build_id == NULL) |
| 1049 | goto more; |
| 1050 | berr = filename__read_build_id(name, build_id, |
| 1051 | BUILD_ID_SIZE); |
| 1052 | compare_build_id: |
| 1053 | match = berr > 0 && memcmp(build_id, self->build_id, |
| 1054 | sizeof(self->build_id)) == 0; |
| 1055 | free(build_id); |
| 1056 | build_id = NULL; |
| 1057 | if (!match) |
| 1058 | goto more; |
| 1059 | } |
| 1060 | |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1061 | fd = open(name, O_RDONLY); |
| 1062 | } while (fd < 0); |
| 1063 | |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 1064 | ret = dso__load_sym(self, map, name, fd, filter, 0, 0); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1065 | close(fd); |
| 1066 | |
| 1067 | /* |
| 1068 | * Some people seem to have debuginfo files _WITHOUT_ debug info!?!? |
| 1069 | */ |
| 1070 | if (!ret) |
| 1071 | goto more; |
| 1072 | |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 1073 | if (ret > 0) { |
Arnaldo Carvalho de Melo | 8216416 | 2009-11-16 13:48:11 -0200 | [diff] [blame] | 1074 | int nr_plt = dso__synthesize_plt_symbols(self, map, filter); |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 1075 | if (nr_plt > 0) |
| 1076 | ret += nr_plt; |
| 1077 | } |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1078 | out: |
| 1079 | free(name); |
Arnaldo Carvalho de Melo | 1340e6b | 2009-08-11 17:04:36 -0300 | [diff] [blame] | 1080 | if (ret < 0 && strstr(self->name, " (deleted)") != NULL) |
| 1081 | return 0; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1082 | return ret; |
| 1083 | } |
| 1084 | |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1085 | struct map *kernel_map; |
| 1086 | |
| 1087 | static void kernel_maps__insert(struct map *map) |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 1088 | { |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1089 | maps__insert(&kernel_maps, map); |
| 1090 | } |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 1091 | |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1092 | struct symbol *kernel_maps__find_symbol(u64 ip, struct map **mapp) |
| 1093 | { |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1094 | struct map *map = maps__find(&kernel_maps, ip); |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1095 | |
| 1096 | if (mapp) |
| 1097 | *mapp = map; |
| 1098 | |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 1099 | if (map) { |
| 1100 | ip = map->map_ip(map, ip); |
| 1101 | return map->dso->find_symbol(map->dso, ip); |
| 1102 | } |
| 1103 | |
| 1104 | return NULL; |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1105 | } |
| 1106 | |
| 1107 | struct map *kernel_maps__find_by_dso_name(const char *name) |
| 1108 | { |
| 1109 | struct rb_node *nd; |
| 1110 | |
| 1111 | for (nd = rb_first(&kernel_maps); nd; nd = rb_next(nd)) { |
| 1112 | struct map *map = rb_entry(nd, struct map, rb_node); |
| 1113 | |
| 1114 | if (map->dso && strcmp(map->dso->name, name) == 0) |
| 1115 | return map; |
| 1116 | } |
| 1117 | |
| 1118 | return NULL; |
| 1119 | } |
| 1120 | |
| 1121 | static int dso__load_module_sym(struct dso *self, struct map *map, |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 1122 | symbol_filter_t filter) |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1123 | { |
| 1124 | int err = 0, fd = open(self->long_name, O_RDONLY); |
| 1125 | |
Arnaldo Carvalho de Melo | 8d06367 | 2009-11-04 18:50:43 -0200 | [diff] [blame] | 1126 | self->loaded = 1; |
Arnaldo Carvalho de Melo | 66bd842 | 2009-10-28 21:51:21 -0200 | [diff] [blame] | 1127 | |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1128 | if (fd < 0) { |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 1129 | pr_err("%s: cannot open %s\n", __func__, self->long_name); |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 1130 | return err; |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1131 | } |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 1132 | |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 1133 | err = dso__load_sym(self, map, self->long_name, fd, filter, 0, 1); |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 1134 | close(fd); |
| 1135 | |
| 1136 | return err; |
| 1137 | } |
| 1138 | |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 1139 | static int dsos__load_modules_sym_dir(char *dirname, symbol_filter_t filter) |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 1140 | { |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1141 | struct dirent *dent; |
| 1142 | int nr_symbols = 0, err; |
| 1143 | DIR *dir = opendir(dirname); |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 1144 | |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1145 | if (!dir) { |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 1146 | pr_err("%s: cannot open %s dir\n", __func__, dirname); |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1147 | return -1; |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 1148 | } |
| 1149 | |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1150 | while ((dent = readdir(dir)) != NULL) { |
| 1151 | char path[PATH_MAX]; |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 1152 | |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1153 | if (dent->d_type == DT_DIR) { |
| 1154 | if (!strcmp(dent->d_name, ".") || |
| 1155 | !strcmp(dent->d_name, "..")) |
| 1156 | continue; |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 1157 | |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1158 | snprintf(path, sizeof(path), "%s/%s", |
| 1159 | dirname, dent->d_name); |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 1160 | err = dsos__load_modules_sym_dir(path, filter); |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1161 | if (err < 0) |
| 1162 | goto failure; |
| 1163 | } else { |
| 1164 | char *dot = strrchr(dent->d_name, '.'), |
| 1165 | dso_name[PATH_MAX]; |
| 1166 | struct map *map; |
| 1167 | struct rb_node *last; |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 1168 | |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1169 | if (dot == NULL || strcmp(dot, ".ko")) |
| 1170 | continue; |
| 1171 | snprintf(dso_name, sizeof(dso_name), "[%.*s]", |
| 1172 | (int)(dot - dent->d_name), dent->d_name); |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 1173 | |
Arnaldo Carvalho de Melo | a2a99e8 | 2009-10-05 14:26:18 -0300 | [diff] [blame] | 1174 | strxfrchar(dso_name, '-', '_'); |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1175 | map = kernel_maps__find_by_dso_name(dso_name); |
| 1176 | if (map == NULL) |
| 1177 | continue; |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 1178 | |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1179 | snprintf(path, sizeof(path), "%s/%s", |
| 1180 | dirname, dent->d_name); |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 1181 | |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1182 | map->dso->long_name = strdup(path); |
| 1183 | if (map->dso->long_name == NULL) |
| 1184 | goto failure; |
| 1185 | |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 1186 | err = dso__load_module_sym(map->dso, map, filter); |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1187 | if (err < 0) |
| 1188 | goto failure; |
| 1189 | last = rb_last(&map->dso->syms); |
| 1190 | if (last) { |
| 1191 | struct symbol *sym; |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 1192 | /* |
| 1193 | * We do this here as well, even having the |
| 1194 | * symbol size found in the symtab because |
| 1195 | * misannotated ASM symbols may have the size |
| 1196 | * set to zero. |
| 1197 | */ |
| 1198 | dso__fixup_sym_end(map->dso); |
| 1199 | |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1200 | sym = rb_entry(last, struct symbol, rb_node); |
| 1201 | map->end = map->start + sym->end; |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 1202 | } |
| 1203 | } |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1204 | nr_symbols += err; |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 1205 | } |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1206 | |
| 1207 | return nr_symbols; |
| 1208 | failure: |
| 1209 | closedir(dir); |
| 1210 | return -1; |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 1211 | } |
| 1212 | |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 1213 | static int dsos__load_modules_sym(symbol_filter_t filter) |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1214 | { |
| 1215 | struct utsname uts; |
| 1216 | char modules_path[PATH_MAX]; |
| 1217 | |
| 1218 | if (uname(&uts) < 0) |
| 1219 | return -1; |
| 1220 | |
| 1221 | snprintf(modules_path, sizeof(modules_path), "/lib/modules/%s/kernel", |
| 1222 | uts.release); |
| 1223 | |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 1224 | return dsos__load_modules_sym_dir(modules_path, filter); |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1225 | } |
| 1226 | |
| 1227 | /* |
| 1228 | * Constructor variant for modules (where we know from /proc/modules where |
| 1229 | * they are loaded) and for vmlinux, where only after we load all the |
| 1230 | * symbols we'll know where it starts and ends. |
| 1231 | */ |
| 1232 | static struct map *map__new2(u64 start, struct dso *dso) |
| 1233 | { |
| 1234 | struct map *self = malloc(sizeof(*self)); |
| 1235 | |
| 1236 | if (self != NULL) { |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1237 | /* |
Arnaldo Carvalho de Melo | afb7b4f | 2009-10-30 16:28:23 -0200 | [diff] [blame] | 1238 | * ->end will be filled after we load all the symbols |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1239 | */ |
Arnaldo Carvalho de Melo | afb7b4f | 2009-10-30 16:28:23 -0200 | [diff] [blame] | 1240 | map__init(self, start, 0, 0, dso); |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1241 | } |
Arnaldo Carvalho de Melo | afb7b4f | 2009-10-30 16:28:23 -0200 | [diff] [blame] | 1242 | |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1243 | return self; |
| 1244 | } |
| 1245 | |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 1246 | static int dsos__load_modules(void) |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1247 | { |
| 1248 | char *line = NULL; |
| 1249 | size_t n; |
| 1250 | FILE *file = fopen("/proc/modules", "r"); |
| 1251 | struct map *map; |
| 1252 | |
| 1253 | if (file == NULL) |
| 1254 | return -1; |
| 1255 | |
| 1256 | while (!feof(file)) { |
| 1257 | char name[PATH_MAX]; |
| 1258 | u64 start; |
| 1259 | struct dso *dso; |
| 1260 | char *sep; |
| 1261 | int line_len; |
| 1262 | |
| 1263 | line_len = getline(&line, &n, file); |
| 1264 | if (line_len < 0) |
| 1265 | break; |
| 1266 | |
| 1267 | if (!line) |
| 1268 | goto out_failure; |
| 1269 | |
| 1270 | line[--line_len] = '\0'; /* \n */ |
| 1271 | |
| 1272 | sep = strrchr(line, 'x'); |
| 1273 | if (sep == NULL) |
| 1274 | continue; |
| 1275 | |
| 1276 | hex2u64(sep + 1, &start); |
| 1277 | |
| 1278 | sep = strchr(line, ' '); |
| 1279 | if (sep == NULL) |
| 1280 | continue; |
| 1281 | |
| 1282 | *sep = '\0'; |
| 1283 | |
| 1284 | snprintf(name, sizeof(name), "[%s]", line); |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 1285 | dso = dso__new(name); |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1286 | |
| 1287 | if (dso == NULL) |
| 1288 | goto out_delete_line; |
| 1289 | |
| 1290 | map = map__new2(start, dso); |
| 1291 | if (map == NULL) { |
| 1292 | dso__delete(dso); |
| 1293 | goto out_delete_line; |
| 1294 | } |
| 1295 | |
| 1296 | dso->origin = DSO__ORIG_KMODULE; |
| 1297 | kernel_maps__insert(map); |
| 1298 | dsos__add(dso); |
| 1299 | } |
| 1300 | |
| 1301 | free(line); |
| 1302 | fclose(file); |
| 1303 | |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 1304 | return 0; |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1305 | |
| 1306 | out_delete_line: |
| 1307 | free(line); |
| 1308 | out_failure: |
| 1309 | return -1; |
| 1310 | } |
| 1311 | |
| 1312 | static int dso__load_vmlinux(struct dso *self, struct map *map, |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 1313 | const char *vmlinux, symbol_filter_t filter) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1314 | { |
| 1315 | int err, fd = open(vmlinux, O_RDONLY); |
| 1316 | |
Arnaldo Carvalho de Melo | 8d06367 | 2009-11-04 18:50:43 -0200 | [diff] [blame] | 1317 | self->loaded = 1; |
Arnaldo Carvalho de Melo | 66bd842 | 2009-10-28 21:51:21 -0200 | [diff] [blame] | 1318 | |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1319 | if (fd < 0) |
| 1320 | return -1; |
| 1321 | |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 1322 | err = dso__load_sym(self, map, self->long_name, fd, filter, 1, 0); |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 1323 | |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1324 | close(fd); |
| 1325 | |
| 1326 | return err; |
| 1327 | } |
| 1328 | |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 1329 | int dsos__load_kernel(const char *vmlinux, symbol_filter_t filter, |
| 1330 | int use_modules) |
Arnaldo Carvalho de Melo | a827c87 | 2009-05-28 14:55:19 -0300 | [diff] [blame] | 1331 | { |
| 1332 | int err = -1; |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 1333 | struct dso *dso = dso__new(vmlinux); |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1334 | |
| 1335 | if (dso == NULL) |
| 1336 | return -1; |
| 1337 | |
| 1338 | dso->short_name = "[kernel]"; |
| 1339 | kernel_map = map__new2(0, dso); |
| 1340 | if (kernel_map == NULL) |
| 1341 | goto out_delete_dso; |
| 1342 | |
Arnaldo Carvalho de Melo | ed52ce2 | 2009-10-19 17:17:57 -0200 | [diff] [blame] | 1343 | kernel_map->map_ip = kernel_map->unmap_ip = identity__map_ip; |
Arnaldo Carvalho de Melo | a827c87 | 2009-05-28 14:55:19 -0300 | [diff] [blame] | 1344 | |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 1345 | if (use_modules && dsos__load_modules() < 0) { |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 1346 | pr_warning("Failed to load list of modules in use! " |
| 1347 | "Continuing...\n"); |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 1348 | use_modules = 0; |
| 1349 | } |
| 1350 | |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 1351 | if (vmlinux) { |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 1352 | err = dso__load_vmlinux(dso, kernel_map, vmlinux, filter); |
Mike Galbraith | 508c4d0 | 2009-09-23 11:20:58 +0200 | [diff] [blame] | 1353 | if (err > 0 && use_modules) { |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 1354 | int syms = dsos__load_modules_sym(filter); |
Mike Galbraith | 508c4d0 | 2009-09-23 11:20:58 +0200 | [diff] [blame] | 1355 | |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 1356 | if (syms < 0) |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 1357 | pr_warning("Failed to read module symbols!" |
| 1358 | " Continuing...\n"); |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 1359 | else |
| 1360 | err += syms; |
Mike Galbraith | 508c4d0 | 2009-09-23 11:20:58 +0200 | [diff] [blame] | 1361 | } |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 1362 | } |
Arnaldo Carvalho de Melo | a827c87 | 2009-05-28 14:55:19 -0300 | [diff] [blame] | 1363 | |
Mike Galbraith | 9974f49 | 2009-07-02 08:05:58 +0200 | [diff] [blame] | 1364 | if (err <= 0) |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 1365 | err = kernel_maps__load_kallsyms(filter, use_modules); |
Arnaldo Carvalho de Melo | a827c87 | 2009-05-28 14:55:19 -0300 | [diff] [blame] | 1366 | |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1367 | if (err > 0) { |
| 1368 | struct rb_node *node = rb_first(&dso->syms); |
| 1369 | struct symbol *sym = rb_entry(node, struct symbol, rb_node); |
| 1370 | |
| 1371 | kernel_map->start = sym->start; |
| 1372 | node = rb_last(&dso->syms); |
| 1373 | sym = rb_entry(node, struct symbol, rb_node); |
| 1374 | kernel_map->end = sym->end; |
| 1375 | |
| 1376 | dso->origin = DSO__ORIG_KERNEL; |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 1377 | kernel_maps__insert(kernel_map); |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1378 | /* |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 1379 | * Now that we have all sorted out, just set the ->end of all |
| 1380 | * maps: |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1381 | */ |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 1382 | kernel_maps__fixup_end(); |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1383 | dsos__add(dso); |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 1384 | |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 1385 | if (verbose) |
| 1386 | kernel_maps__fprintf(stderr); |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1387 | } |
Arnaldo Carvalho de Melo | 94cb9e3 | 2009-08-06 14:43:17 -0300 | [diff] [blame] | 1388 | |
Arnaldo Carvalho de Melo | a827c87 | 2009-05-28 14:55:19 -0300 | [diff] [blame] | 1389 | return err; |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1390 | |
| 1391 | out_delete_dso: |
| 1392 | dso__delete(dso); |
| 1393 | return -1; |
Arnaldo Carvalho de Melo | a827c87 | 2009-05-28 14:55:19 -0300 | [diff] [blame] | 1394 | } |
| 1395 | |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 1396 | LIST_HEAD(dsos); |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 1397 | struct dso *vdso; |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 1398 | |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 1399 | const char *vmlinux_name = "vmlinux"; |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 1400 | int modules; |
| 1401 | |
| 1402 | static void dsos__add(struct dso *dso) |
| 1403 | { |
| 1404 | list_add_tail(&dso->node, &dsos); |
| 1405 | } |
| 1406 | |
| 1407 | static struct dso *dsos__find(const char *name) |
| 1408 | { |
| 1409 | struct dso *pos; |
| 1410 | |
| 1411 | list_for_each_entry(pos, &dsos, node) |
| 1412 | if (strcmp(pos->name, name) == 0) |
| 1413 | return pos; |
| 1414 | return NULL; |
| 1415 | } |
| 1416 | |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 1417 | struct dso *dsos__findnew(const char *name) |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 1418 | { |
| 1419 | struct dso *dso = dsos__find(name); |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 1420 | |
Arnaldo Carvalho de Melo | e420499 | 2009-10-20 14:25:40 -0200 | [diff] [blame] | 1421 | if (!dso) { |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 1422 | dso = dso__new(name); |
Arnaldo Carvalho de Melo | 66bd842 | 2009-10-28 21:51:21 -0200 | [diff] [blame] | 1423 | if (dso != NULL) |
Arnaldo Carvalho de Melo | e420499 | 2009-10-20 14:25:40 -0200 | [diff] [blame] | 1424 | dsos__add(dso); |
Arnaldo Carvalho de Melo | 66bd842 | 2009-10-28 21:51:21 -0200 | [diff] [blame] | 1425 | } |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 1426 | |
| 1427 | return dso; |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 1428 | } |
| 1429 | |
| 1430 | void dsos__fprintf(FILE *fp) |
| 1431 | { |
| 1432 | struct dso *pos; |
| 1433 | |
| 1434 | list_for_each_entry(pos, &dsos, node) |
| 1435 | dso__fprintf(pos, fp); |
| 1436 | } |
| 1437 | |
Arnaldo Carvalho de Melo | 9e03eb2 | 2009-11-16 16:32:44 -0200 | [diff] [blame] | 1438 | size_t dsos__fprintf_buildid(FILE *fp) |
| 1439 | { |
| 1440 | struct dso *pos; |
| 1441 | size_t ret = 0; |
| 1442 | |
| 1443 | list_for_each_entry(pos, &dsos, node) { |
| 1444 | ret += dso__fprintf_buildid(pos, fp); |
Arnaldo Carvalho de Melo | 1124ba7 | 2009-11-16 21:45:25 -0200 | [diff] [blame^] | 1445 | ret += fprintf(fp, " %s\n", pos->long_name); |
Arnaldo Carvalho de Melo | 9e03eb2 | 2009-11-16 16:32:44 -0200 | [diff] [blame] | 1446 | } |
| 1447 | return ret; |
| 1448 | } |
| 1449 | |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 1450 | int load_kernel(symbol_filter_t filter) |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 1451 | { |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 1452 | if (dsos__load_kernel(vmlinux_name, filter, modules) <= 0) |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 1453 | return -1; |
| 1454 | |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 1455 | vdso = dso__new("[vdso]"); |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 1456 | if (!vdso) |
| 1457 | return -1; |
| 1458 | |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 1459 | dsos__add(vdso); |
| 1460 | |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1461 | return 0; |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 1462 | } |
| 1463 | |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 1464 | void symbol__init(unsigned int priv_size) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1465 | { |
| 1466 | elf_version(EV_CURRENT); |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 1467 | symbol__priv_size = priv_size; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1468 | } |