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