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