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