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