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