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