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