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 | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 164 | void dso__set_long_name(struct dso *self, char *name) |
Arnaldo Carvalho de Melo | cfc10d3 | 2009-11-17 15:40:53 -0200 | [diff] [blame] | 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 | { |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 179 | struct dso *self = zalloc(sizeof(*self) + strlen(name) + 1); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 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 | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 503 | if (strcmp(curr_map->dso->short_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} " |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 507 | "inconsistency while looking " |
| 508 | "for \"%s\" module!\n", module); |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 509 | return -1; |
| 510 | } |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 511 | |
| 512 | if (curr_map->dso->loaded) |
| 513 | goto discard_symbol; |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 514 | } |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 515 | /* |
| 516 | * So that we look just like we get from .ko files, |
| 517 | * i.e. not prelinked, relative to map->start. |
| 518 | */ |
Arnaldo Carvalho de Melo | 4e06255 | 2009-11-27 16:29:18 -0200 | [diff] [blame] | 519 | pos->start = curr_map->map_ip(curr_map, pos->start); |
| 520 | pos->end = curr_map->map_ip(curr_map, pos->end); |
| 521 | } else if (curr_map != map) { |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 522 | char dso_name[PATH_MAX]; |
| 523 | struct dso *dso; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 524 | |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 525 | snprintf(dso_name, sizeof(dso_name), "[kernel].%d", |
| 526 | kernel_range++); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 527 | |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 528 | dso = dso__new(dso_name); |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 529 | if (dso == NULL) |
| 530 | return -1; |
| 531 | |
Arnaldo Carvalho de Melo | 4e06255 | 2009-11-27 16:29:18 -0200 | [diff] [blame] | 532 | curr_map = map__new2(pos->start, dso, map->type); |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 533 | if (map == NULL) { |
| 534 | dso__delete(dso); |
| 535 | return -1; |
| 536 | } |
| 537 | |
Arnaldo Carvalho de Melo | 4e06255 | 2009-11-27 16:29:18 -0200 | [diff] [blame] | 538 | 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] | 539 | map_groups__insert(&session->kmaps, curr_map); |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 540 | ++kernel_range; |
| 541 | } |
| 542 | |
Arnaldo Carvalho de Melo | 4e06255 | 2009-11-27 16:29:18 -0200 | [diff] [blame] | 543 | if (filter && filter(curr_map, pos)) { |
Arnaldo Carvalho de Melo | 1de8e24 | 2009-11-27 16:29:21 -0200 | [diff] [blame] | 544 | discard_symbol: rb_erase(&pos->rb_node, root); |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 545 | symbol__delete(pos); |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 546 | } else { |
Arnaldo Carvalho de Melo | 4e06255 | 2009-11-27 16:29:18 -0200 | [diff] [blame] | 547 | if (curr_map != map) { |
| 548 | rb_erase(&pos->rb_node, root); |
| 549 | symbols__insert(&curr_map->dso->symbols[curr_map->type], pos); |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 550 | } |
Mike Galbraith | 9974f49 | 2009-07-02 08:05:58 +0200 | [diff] [blame] | 551 | count++; |
| 552 | } |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 553 | } |
| 554 | |
Mike Galbraith | 9974f49 | 2009-07-02 08:05:58 +0200 | [diff] [blame] | 555 | return count; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 556 | } |
| 557 | |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 558 | |
Arnaldo Carvalho de Melo | 4e06255 | 2009-11-27 16:29:18 -0200 | [diff] [blame] | 559 | 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] | 560 | struct perf_session *session, symbol_filter_t filter) |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 561 | { |
Arnaldo Carvalho de Melo | 4e06255 | 2009-11-27 16:29:18 -0200 | [diff] [blame] | 562 | if (dso__load_all_kallsyms(self, map) < 0) |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 563 | return -1; |
| 564 | |
Arnaldo Carvalho de Melo | 4e06255 | 2009-11-27 16:29:18 -0200 | [diff] [blame] | 565 | symbols__fixup_end(&self->symbols[map->type]); |
| 566 | self->origin = DSO__ORIG_KERNEL; |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 567 | |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 568 | return dso__split_kallsyms(self, map, session, filter); |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 569 | } |
| 570 | |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 571 | 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] | 572 | symbol_filter_t filter) |
Pekka Enberg | 80d496b | 2009-06-08 21:12:48 +0300 | [diff] [blame] | 573 | { |
| 574 | char *line = NULL; |
| 575 | size_t n; |
| 576 | FILE *file; |
| 577 | int nr_syms = 0; |
| 578 | |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 579 | file = fopen(self->long_name, "r"); |
Pekka Enberg | 80d496b | 2009-06-08 21:12:48 +0300 | [diff] [blame] | 580 | if (file == NULL) |
| 581 | goto out_failure; |
| 582 | |
| 583 | while (!feof(file)) { |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 584 | u64 start, size; |
Pekka Enberg | 80d496b | 2009-06-08 21:12:48 +0300 | [diff] [blame] | 585 | struct symbol *sym; |
| 586 | int line_len, len; |
| 587 | |
| 588 | line_len = getline(&line, &n, file); |
| 589 | if (line_len < 0) |
| 590 | break; |
| 591 | |
| 592 | if (!line) |
| 593 | goto out_failure; |
| 594 | |
| 595 | line[--line_len] = '\0'; /* \n */ |
| 596 | |
| 597 | len = hex2u64(line, &start); |
| 598 | |
| 599 | len++; |
| 600 | if (len + 2 >= line_len) |
| 601 | continue; |
| 602 | |
| 603 | len += hex2u64(line + len, &size); |
| 604 | |
| 605 | len++; |
| 606 | if (len + 2 >= line_len) |
| 607 | continue; |
| 608 | |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 609 | sym = symbol__new(start, size, line + len); |
Pekka Enberg | 80d496b | 2009-06-08 21:12:48 +0300 | [diff] [blame] | 610 | |
| 611 | if (sym == NULL) |
| 612 | goto out_delete_line; |
| 613 | |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 614 | if (filter && filter(map, sym)) |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 615 | symbol__delete(sym); |
Pekka Enberg | 80d496b | 2009-06-08 21:12:48 +0300 | [diff] [blame] | 616 | else { |
Arnaldo Carvalho de Melo | 6a4694a | 2009-11-27 16:29:17 -0200 | [diff] [blame] | 617 | symbols__insert(&self->symbols[map->type], sym); |
Pekka Enberg | 80d496b | 2009-06-08 21:12:48 +0300 | [diff] [blame] | 618 | nr_syms++; |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | free(line); |
| 623 | fclose(file); |
| 624 | |
| 625 | return nr_syms; |
| 626 | |
| 627 | out_delete_line: |
| 628 | free(line); |
| 629 | out_failure: |
| 630 | return -1; |
| 631 | } |
| 632 | |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 633 | /** |
| 634 | * elf_symtab__for_each_symbol - iterate thru all the symbols |
| 635 | * |
| 636 | * @self: struct elf_symtab instance to iterate |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 637 | * @idx: uint32_t idx |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 638 | * @sym: GElf_Sym iterator |
| 639 | */ |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 640 | #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \ |
| 641 | for (idx = 0, gelf_getsym(syms, idx, &sym);\ |
| 642 | idx < nr_syms; \ |
| 643 | idx++, gelf_getsym(syms, idx, &sym)) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 644 | |
| 645 | static inline uint8_t elf_sym__type(const GElf_Sym *sym) |
| 646 | { |
| 647 | return GELF_ST_TYPE(sym->st_info); |
| 648 | } |
| 649 | |
| 650 | static inline int elf_sym__is_function(const GElf_Sym *sym) |
| 651 | { |
| 652 | return elf_sym__type(sym) == STT_FUNC && |
| 653 | sym->st_name != 0 && |
Arnaldo Carvalho de Melo | 8183313 | 2009-10-05 23:35:03 -0300 | [diff] [blame] | 654 | sym->st_shndx != SHN_UNDEF; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 655 | } |
| 656 | |
Arnaldo Carvalho de Melo | f1dfa0b | 2009-12-11 14:50:39 -0200 | [diff] [blame] | 657 | static inline bool elf_sym__is_object(const GElf_Sym *sym) |
| 658 | { |
| 659 | return elf_sym__type(sym) == STT_OBJECT && |
| 660 | sym->st_name != 0 && |
| 661 | sym->st_shndx != SHN_UNDEF; |
| 662 | } |
| 663 | |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 664 | static inline int elf_sym__is_label(const GElf_Sym *sym) |
| 665 | { |
| 666 | return elf_sym__type(sym) == STT_NOTYPE && |
| 667 | sym->st_name != 0 && |
| 668 | sym->st_shndx != SHN_UNDEF && |
| 669 | sym->st_shndx != SHN_ABS; |
| 670 | } |
| 671 | |
| 672 | static inline const char *elf_sec__name(const GElf_Shdr *shdr, |
| 673 | const Elf_Data *secstrs) |
| 674 | { |
| 675 | return secstrs->d_buf + shdr->sh_name; |
| 676 | } |
| 677 | |
| 678 | static inline int elf_sec__is_text(const GElf_Shdr *shdr, |
| 679 | const Elf_Data *secstrs) |
| 680 | { |
| 681 | return strstr(elf_sec__name(shdr, secstrs), "text") != NULL; |
| 682 | } |
| 683 | |
Arnaldo Carvalho de Melo | f1dfa0b | 2009-12-11 14:50:39 -0200 | [diff] [blame] | 684 | static inline bool elf_sec__is_data(const GElf_Shdr *shdr, |
| 685 | const Elf_Data *secstrs) |
| 686 | { |
| 687 | return strstr(elf_sec__name(shdr, secstrs), "data") != NULL; |
| 688 | } |
| 689 | |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 690 | static inline const char *elf_sym__name(const GElf_Sym *sym, |
| 691 | const Elf_Data *symstrs) |
| 692 | { |
| 693 | return symstrs->d_buf + sym->st_name; |
| 694 | } |
| 695 | |
| 696 | static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep, |
| 697 | GElf_Shdr *shp, const char *name, |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 698 | size_t *idx) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 699 | { |
| 700 | Elf_Scn *sec = NULL; |
| 701 | size_t cnt = 1; |
| 702 | |
| 703 | while ((sec = elf_nextscn(elf, sec)) != NULL) { |
| 704 | char *str; |
| 705 | |
| 706 | gelf_getshdr(sec, shp); |
| 707 | str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name); |
| 708 | if (!strcmp(name, str)) { |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 709 | if (idx) |
| 710 | *idx = cnt; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 711 | break; |
| 712 | } |
| 713 | ++cnt; |
| 714 | } |
| 715 | |
| 716 | return sec; |
| 717 | } |
| 718 | |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 719 | #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \ |
| 720 | for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \ |
| 721 | idx < nr_entries; \ |
| 722 | ++idx, pos = gelf_getrel(reldata, idx, &pos_mem)) |
| 723 | |
| 724 | #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \ |
| 725 | for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \ |
| 726 | idx < nr_entries; \ |
| 727 | ++idx, pos = gelf_getrela(reldata, idx, &pos_mem)) |
| 728 | |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 729 | /* |
| 730 | * We need to check if we have a .dynsym, so that we can handle the |
| 731 | * .plt, synthesizing its symbols, that aren't on the symtabs (be it |
| 732 | * .dynsym or .symtab). |
| 733 | * And always look at the original dso, not at debuginfo packages, that |
| 734 | * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS). |
| 735 | */ |
Arnaldo Carvalho de Melo | 8216416 | 2009-11-16 13:48:11 -0200 | [diff] [blame] | 736 | static int dso__synthesize_plt_symbols(struct dso *self, struct map *map, |
| 737 | symbol_filter_t filter) |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 738 | { |
| 739 | uint32_t nr_rel_entries, idx; |
| 740 | GElf_Sym sym; |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 741 | u64 plt_offset; |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 742 | GElf_Shdr shdr_plt; |
| 743 | struct symbol *f; |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 744 | GElf_Shdr shdr_rel_plt, shdr_dynsym; |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 745 | Elf_Data *reldata, *syms, *symstrs; |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 746 | Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym; |
| 747 | size_t dynsym_idx; |
| 748 | GElf_Ehdr ehdr; |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 749 | char sympltname[1024]; |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 750 | Elf *elf; |
| 751 | int nr = 0, symidx, fd, err = 0; |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 752 | |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 753 | fd = open(self->long_name, O_RDONLY); |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 754 | if (fd < 0) |
| 755 | goto out; |
| 756 | |
Marti Raudsepp | 8408712 | 2009-10-24 19:10:36 +0300 | [diff] [blame] | 757 | elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 758 | if (elf == NULL) |
| 759 | goto out_close; |
| 760 | |
| 761 | if (gelf_getehdr(elf, &ehdr) == NULL) |
| 762 | goto out_elf_end; |
| 763 | |
| 764 | scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym, |
| 765 | ".dynsym", &dynsym_idx); |
| 766 | if (scn_dynsym == NULL) |
| 767 | goto out_elf_end; |
| 768 | |
| 769 | 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] | 770 | ".rela.plt", NULL); |
| 771 | if (scn_plt_rel == NULL) { |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 772 | 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] | 773 | ".rel.plt", NULL); |
| 774 | if (scn_plt_rel == NULL) |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 775 | goto out_elf_end; |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 776 | } |
| 777 | |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 778 | err = -1; |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 779 | |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 780 | if (shdr_rel_plt.sh_link != dynsym_idx) |
| 781 | goto out_elf_end; |
| 782 | |
| 783 | if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL) |
| 784 | goto out_elf_end; |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 785 | |
| 786 | /* |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 787 | * 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] | 788 | * and the symbols in the .dynsym they refer to. |
| 789 | */ |
| 790 | reldata = elf_getdata(scn_plt_rel, NULL); |
| 791 | if (reldata == 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 | |
| 794 | syms = elf_getdata(scn_dynsym, NULL); |
| 795 | if (syms == 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 | |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 798 | scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link); |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 799 | if (scn_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 | symstrs = elf_getdata(scn_symstrs, NULL); |
| 803 | if (symstrs == NULL) |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 804 | goto out_elf_end; |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 805 | |
| 806 | nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize; |
| 807 | plt_offset = shdr_plt.sh_offset; |
| 808 | |
| 809 | if (shdr_rel_plt.sh_type == SHT_RELA) { |
| 810 | GElf_Rela pos_mem, *pos; |
| 811 | |
| 812 | elf_section__for_each_rela(reldata, pos, pos_mem, idx, |
| 813 | nr_rel_entries) { |
| 814 | symidx = GELF_R_SYM(pos->r_info); |
| 815 | plt_offset += shdr_plt.sh_entsize; |
| 816 | gelf_getsym(syms, symidx, &sym); |
| 817 | snprintf(sympltname, sizeof(sympltname), |
| 818 | "%s@plt", elf_sym__name(&sym, symstrs)); |
| 819 | |
| 820 | f = symbol__new(plt_offset, shdr_plt.sh_entsize, |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 821 | sympltname); |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 822 | if (!f) |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 823 | goto out_elf_end; |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 824 | |
Arnaldo Carvalho de Melo | 8216416 | 2009-11-16 13:48:11 -0200 | [diff] [blame] | 825 | if (filter && filter(map, f)) |
| 826 | symbol__delete(f); |
| 827 | else { |
Arnaldo Carvalho de Melo | 6a4694a | 2009-11-27 16:29:17 -0200 | [diff] [blame] | 828 | symbols__insert(&self->symbols[map->type], f); |
Arnaldo Carvalho de Melo | 8216416 | 2009-11-16 13:48:11 -0200 | [diff] [blame] | 829 | ++nr; |
| 830 | } |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 831 | } |
| 832 | } else if (shdr_rel_plt.sh_type == SHT_REL) { |
| 833 | GElf_Rel pos_mem, *pos; |
| 834 | elf_section__for_each_rel(reldata, pos, pos_mem, idx, |
| 835 | nr_rel_entries) { |
| 836 | symidx = GELF_R_SYM(pos->r_info); |
| 837 | plt_offset += shdr_plt.sh_entsize; |
| 838 | gelf_getsym(syms, symidx, &sym); |
| 839 | snprintf(sympltname, sizeof(sympltname), |
| 840 | "%s@plt", elf_sym__name(&sym, symstrs)); |
| 841 | |
| 842 | f = symbol__new(plt_offset, shdr_plt.sh_entsize, |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 843 | sympltname); |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 844 | if (!f) |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 845 | goto out_elf_end; |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 846 | |
Arnaldo Carvalho de Melo | 8216416 | 2009-11-16 13:48:11 -0200 | [diff] [blame] | 847 | if (filter && filter(map, f)) |
| 848 | symbol__delete(f); |
| 849 | else { |
Arnaldo Carvalho de Melo | 6a4694a | 2009-11-27 16:29:17 -0200 | [diff] [blame] | 850 | symbols__insert(&self->symbols[map->type], f); |
Arnaldo Carvalho de Melo | 8216416 | 2009-11-16 13:48:11 -0200 | [diff] [blame] | 851 | ++nr; |
| 852 | } |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 853 | } |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 854 | } |
| 855 | |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 856 | err = 0; |
| 857 | out_elf_end: |
| 858 | elf_end(elf); |
| 859 | out_close: |
| 860 | close(fd); |
| 861 | |
| 862 | if (err == 0) |
| 863 | return nr; |
| 864 | out: |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 865 | pr_warning("%s: problems reading %s PLT info.\n", |
| 866 | __func__, self->long_name); |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 867 | return 0; |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 868 | } |
| 869 | |
Arnaldo Carvalho de Melo | d45868d | 2009-12-11 14:50:38 -0200 | [diff] [blame] | 870 | static bool elf_sym__is_a(GElf_Sym *self, enum map_type type) |
| 871 | { |
| 872 | switch (type) { |
| 873 | case MAP__FUNCTION: |
| 874 | return elf_sym__is_function(self); |
Arnaldo Carvalho de Melo | f1dfa0b | 2009-12-11 14:50:39 -0200 | [diff] [blame] | 875 | case MAP__VARIABLE: |
| 876 | return elf_sym__is_object(self); |
Arnaldo Carvalho de Melo | d45868d | 2009-12-11 14:50:38 -0200 | [diff] [blame] | 877 | default: |
| 878 | return false; |
| 879 | } |
| 880 | } |
| 881 | |
| 882 | static bool elf_sec__is_a(GElf_Shdr *self, Elf_Data *secstrs, enum map_type type) |
| 883 | { |
| 884 | switch (type) { |
| 885 | case MAP__FUNCTION: |
| 886 | return elf_sec__is_text(self, secstrs); |
Arnaldo Carvalho de Melo | f1dfa0b | 2009-12-11 14:50:39 -0200 | [diff] [blame] | 887 | case MAP__VARIABLE: |
| 888 | return elf_sec__is_data(self, secstrs); |
Arnaldo Carvalho de Melo | d45868d | 2009-12-11 14:50:38 -0200 | [diff] [blame] | 889 | default: |
| 890 | return false; |
| 891 | } |
| 892 | } |
| 893 | |
Arnaldo Carvalho de Melo | 95011c6 | 2009-11-27 16:29:20 -0200 | [diff] [blame] | 894 | 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] | 895 | struct perf_session *session, const char *name, int fd, |
Arnaldo Carvalho de Melo | 95011c6 | 2009-11-27 16:29:20 -0200 | [diff] [blame] | 896 | symbol_filter_t filter, int kernel, int kmodule) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 897 | { |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 898 | struct map *curr_map = map; |
| 899 | struct dso *curr_dso = self; |
| 900 | size_t dso_name_len = strlen(self->short_name); |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 901 | Elf_Data *symstrs, *secstrs; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 902 | uint32_t nr_syms; |
| 903 | int err = -1; |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 904 | uint32_t idx; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 905 | GElf_Ehdr ehdr; |
| 906 | GElf_Shdr shdr; |
| 907 | Elf_Data *syms; |
| 908 | GElf_Sym sym; |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 909 | Elf_Scn *sec, *sec_strndx; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 910 | Elf *elf; |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 911 | int nr = 0; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 912 | |
Marti Raudsepp | 8408712 | 2009-10-24 19:10:36 +0300 | [diff] [blame] | 913 | elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 914 | if (elf == NULL) { |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 915 | 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] | 916 | goto out_close; |
| 917 | } |
| 918 | |
| 919 | if (gelf_getehdr(elf, &ehdr) == NULL) { |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 920 | pr_err("%s: cannot get elf header.\n", __func__); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 921 | goto out_elf_end; |
| 922 | } |
| 923 | |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 924 | sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL); |
| 925 | if (sec == NULL) { |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 926 | sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL); |
| 927 | if (sec == NULL) |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 928 | goto out_elf_end; |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 929 | } |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 930 | |
| 931 | syms = elf_getdata(sec, NULL); |
| 932 | if (syms == NULL) |
| 933 | goto out_elf_end; |
| 934 | |
| 935 | sec = elf_getscn(elf, shdr.sh_link); |
| 936 | if (sec == NULL) |
| 937 | goto out_elf_end; |
| 938 | |
| 939 | symstrs = elf_getdata(sec, NULL); |
| 940 | if (symstrs == NULL) |
| 941 | goto out_elf_end; |
| 942 | |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 943 | sec_strndx = elf_getscn(elf, ehdr.e_shstrndx); |
| 944 | if (sec_strndx == NULL) |
| 945 | goto out_elf_end; |
| 946 | |
| 947 | secstrs = elf_getdata(sec_strndx, NULL); |
Stoyan Gaydarov | 9b30a26 | 2009-07-30 05:25:29 -0500 | [diff] [blame] | 948 | if (secstrs == NULL) |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 949 | goto out_elf_end; |
| 950 | |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 951 | nr_syms = shdr.sh_size / shdr.sh_entsize; |
| 952 | |
Arjan van de Ven | e9fbc9d | 2009-06-06 21:22:33 +0200 | [diff] [blame] | 953 | memset(&sym, 0, sizeof(sym)); |
Mike Galbraith | d20ff6b | 2009-07-20 14:01:38 +0200 | [diff] [blame] | 954 | if (!kernel) { |
| 955 | self->adjust_symbols = (ehdr.e_type == ET_EXEC || |
Arnaldo Carvalho de Melo | 30d7a77 | 2009-07-02 21:24:14 -0300 | [diff] [blame] | 956 | elf_section_by_name(elf, &ehdr, &shdr, |
| 957 | ".gnu.prelink_undo", |
| 958 | NULL) != NULL); |
Mike Galbraith | d20ff6b | 2009-07-20 14:01:38 +0200 | [diff] [blame] | 959 | } else self->adjust_symbols = 0; |
| 960 | |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 961 | elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) { |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 962 | struct symbol *f; |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 963 | const char *elf_name = elf_sym__name(&sym, symstrs); |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 964 | char *demangled = NULL; |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 965 | int is_label = elf_sym__is_label(&sym); |
| 966 | const char *section_name; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 967 | |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 968 | if (kernel && session->ref_reloc_sym.name != NULL && |
| 969 | strcmp(elf_name, session->ref_reloc_sym.name) == 0) |
| 970 | perf_session__reloc_vmlinux_maps(session, sym.st_value); |
| 971 | |
Arnaldo Carvalho de Melo | d45868d | 2009-12-11 14:50:38 -0200 | [diff] [blame] | 972 | if (!is_label && !elf_sym__is_a(&sym, map->type)) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 973 | continue; |
| 974 | |
| 975 | sec = elf_getscn(elf, sym.st_shndx); |
| 976 | if (!sec) |
| 977 | goto out_elf_end; |
| 978 | |
| 979 | gelf_getshdr(sec, &shdr); |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 980 | |
Arnaldo Carvalho de Melo | d45868d | 2009-12-11 14:50:38 -0200 | [diff] [blame] | 981 | if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type)) |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 982 | continue; |
| 983 | |
| 984 | section_name = elf_sec__name(&shdr, secstrs); |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 985 | |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 986 | if (kernel || kmodule) { |
| 987 | char dso_name[PATH_MAX]; |
| 988 | |
| 989 | if (strcmp(section_name, |
| 990 | curr_dso->short_name + dso_name_len) == 0) |
| 991 | goto new_symbol; |
| 992 | |
| 993 | if (strcmp(section_name, ".text") == 0) { |
| 994 | curr_map = map; |
| 995 | curr_dso = self; |
| 996 | goto new_symbol; |
| 997 | } |
| 998 | |
| 999 | snprintf(dso_name, sizeof(dso_name), |
| 1000 | "%s%s", self->short_name, section_name); |
| 1001 | |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 1002 | 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] | 1003 | if (curr_map == NULL) { |
| 1004 | u64 start = sym.st_value; |
| 1005 | |
| 1006 | if (kmodule) |
| 1007 | start += map->start + shdr.sh_offset; |
| 1008 | |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 1009 | curr_dso = dso__new(dso_name); |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 1010 | if (curr_dso == NULL) |
| 1011 | goto out_elf_end; |
Arnaldo Carvalho de Melo | 3610583 | 2009-11-27 16:29:16 -0200 | [diff] [blame] | 1012 | curr_map = map__new2(start, curr_dso, |
| 1013 | MAP__FUNCTION); |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 1014 | if (curr_map == NULL) { |
| 1015 | dso__delete(curr_dso); |
| 1016 | goto out_elf_end; |
| 1017 | } |
Arnaldo Carvalho de Melo | ed52ce2 | 2009-10-19 17:17:57 -0200 | [diff] [blame] | 1018 | curr_map->map_ip = identity__map_ip; |
| 1019 | curr_map->unmap_ip = identity__map_ip; |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 1020 | curr_dso->origin = DSO__ORIG_KERNEL; |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 1021 | map_groups__insert(&session->kmaps, curr_map); |
Arnaldo Carvalho de Melo | b0da954 | 2009-11-27 16:29:14 -0200 | [diff] [blame] | 1022 | dsos__add(&dsos__kernel, curr_dso); |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 1023 | } else |
| 1024 | curr_dso = curr_map->dso; |
| 1025 | |
| 1026 | goto new_symbol; |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 1027 | } |
| 1028 | |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 1029 | if (curr_dso->adjust_symbols) { |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 1030 | pr_debug2("adjusting symbol: st_value: %Lx sh_addr: " |
| 1031 | "%Lx sh_offset: %Lx\n", (u64)sym.st_value, |
| 1032 | (u64)shdr.sh_addr, (u64)shdr.sh_offset); |
Arnaldo Carvalho de Melo | f5812a7 | 2009-06-30 11:43:17 -0300 | [diff] [blame] | 1033 | sym.st_value -= shdr.sh_addr - shdr.sh_offset; |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 1034 | } |
Arnaldo Carvalho de Melo | 28ac909 | 2009-07-20 14:14:12 -0300 | [diff] [blame] | 1035 | /* |
| 1036 | * We need to figure out if the object was created from C++ sources |
| 1037 | * DWARF DW_compile_unit has this, but we don't always have access |
| 1038 | * to it... |
| 1039 | */ |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 1040 | demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI); |
Arnaldo Carvalho de Melo | 28ac909 | 2009-07-20 14:14:12 -0300 | [diff] [blame] | 1041 | if (demangled != NULL) |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 1042 | elf_name = demangled; |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 1043 | new_symbol: |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 1044 | 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] | 1045 | free(demangled); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1046 | if (!f) |
| 1047 | goto out_elf_end; |
| 1048 | |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 1049 | if (filter && filter(curr_map, f)) |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 1050 | symbol__delete(f); |
Arnaldo Carvalho de Melo | 69ee69f | 2009-05-28 14:55:26 -0300 | [diff] [blame] | 1051 | else { |
Arnaldo Carvalho de Melo | 6a4694a | 2009-11-27 16:29:17 -0200 | [diff] [blame] | 1052 | symbols__insert(&curr_dso->symbols[curr_map->type], f); |
Arnaldo Carvalho de Melo | 69ee69f | 2009-05-28 14:55:26 -0300 | [diff] [blame] | 1053 | nr++; |
| 1054 | } |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1055 | } |
| 1056 | |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 1057 | /* |
| 1058 | * For misannotated, zeroed, ASM function sizes. |
| 1059 | */ |
| 1060 | if (nr > 0) |
Arnaldo Carvalho de Melo | 6a4694a | 2009-11-27 16:29:17 -0200 | [diff] [blame] | 1061 | symbols__fixup_end(&self->symbols[map->type]); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1062 | err = nr; |
| 1063 | out_elf_end: |
| 1064 | elf_end(elf); |
| 1065 | out_close: |
| 1066 | return err; |
| 1067 | } |
| 1068 | |
Arnaldo Carvalho de Melo | 78075ca | 2009-11-20 20:51:26 -0200 | [diff] [blame] | 1069 | static bool dso__build_id_equal(const struct dso *self, u8 *build_id) |
| 1070 | { |
| 1071 | return memcmp(self->build_id, build_id, sizeof(self->build_id)) == 0; |
| 1072 | } |
| 1073 | |
Arnaldo Carvalho de Melo | b0da954 | 2009-11-27 16:29:14 -0200 | [diff] [blame] | 1074 | static bool __dsos__read_build_ids(struct list_head *head) |
Frederic Weisbecker | 57f395a | 2009-11-11 04:51:04 +0100 | [diff] [blame] | 1075 | { |
Arnaldo Carvalho de Melo | e30a3d1 | 2009-11-18 20:20:51 -0200 | [diff] [blame] | 1076 | bool have_build_id = false; |
Frederic Weisbecker | 57f395a | 2009-11-11 04:51:04 +0100 | [diff] [blame] | 1077 | struct dso *pos; |
| 1078 | |
Arnaldo Carvalho de Melo | b0da954 | 2009-11-27 16:29:14 -0200 | [diff] [blame] | 1079 | list_for_each_entry(pos, head, node) |
Arnaldo Carvalho de Melo | e30a3d1 | 2009-11-18 20:20:51 -0200 | [diff] [blame] | 1080 | if (filename__read_build_id(pos->long_name, pos->build_id, |
| 1081 | sizeof(pos->build_id)) > 0) { |
| 1082 | have_build_id = true; |
| 1083 | pos->has_build_id = true; |
| 1084 | } |
Frederic Weisbecker | 57f395a | 2009-11-11 04:51:04 +0100 | [diff] [blame] | 1085 | |
Arnaldo Carvalho de Melo | e30a3d1 | 2009-11-18 20:20:51 -0200 | [diff] [blame] | 1086 | return have_build_id; |
Frederic Weisbecker | 57f395a | 2009-11-11 04:51:04 +0100 | [diff] [blame] | 1087 | } |
| 1088 | |
Arnaldo Carvalho de Melo | b0da954 | 2009-11-27 16:29:14 -0200 | [diff] [blame] | 1089 | bool dsos__read_build_ids(void) |
| 1090 | { |
Arnaldo Carvalho de Melo | 8b4825b | 2009-12-09 20:09:37 -0200 | [diff] [blame] | 1091 | bool kbuildids = __dsos__read_build_ids(&dsos__kernel), |
| 1092 | ubuildids = __dsos__read_build_ids(&dsos__user); |
| 1093 | return kbuildids || ubuildids; |
Arnaldo Carvalho de Melo | b0da954 | 2009-11-27 16:29:14 -0200 | [diff] [blame] | 1094 | } |
| 1095 | |
Arnaldo Carvalho de Melo | fd7a346 | 2009-11-20 20:51:25 -0200 | [diff] [blame] | 1096 | /* |
| 1097 | * Align offset to 4 bytes as needed for note name and descriptor data. |
| 1098 | */ |
| 1099 | #define NOTE_ALIGN(n) (((n) + 3) & -4U) |
| 1100 | |
Arnaldo Carvalho de Melo | 2643ce1 | 2009-11-03 21:46:10 -0200 | [diff] [blame] | 1101 | 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] | 1102 | { |
Arnaldo Carvalho de Melo | 2643ce1 | 2009-11-03 21:46:10 -0200 | [diff] [blame] | 1103 | int fd, err = -1; |
Arnaldo Carvalho de Melo | 4d1e00a | 2009-08-05 19:02:49 -0300 | [diff] [blame] | 1104 | GElf_Ehdr ehdr; |
| 1105 | GElf_Shdr shdr; |
Arnaldo Carvalho de Melo | fd7a346 | 2009-11-20 20:51:25 -0200 | [diff] [blame] | 1106 | Elf_Data *data; |
Arnaldo Carvalho de Melo | 4d1e00a | 2009-08-05 19:02:49 -0300 | [diff] [blame] | 1107 | Elf_Scn *sec; |
Pekka Enberg | e57cfcd | 2009-11-22 12:29:44 +0200 | [diff] [blame] | 1108 | Elf_Kind ek; |
Arnaldo Carvalho de Melo | fd7a346 | 2009-11-20 20:51:25 -0200 | [diff] [blame] | 1109 | void *ptr; |
Arnaldo Carvalho de Melo | 4d1e00a | 2009-08-05 19:02:49 -0300 | [diff] [blame] | 1110 | Elf *elf; |
Arnaldo Carvalho de Melo | 4d1e00a | 2009-08-05 19:02:49 -0300 | [diff] [blame] | 1111 | |
Arnaldo Carvalho de Melo | 2643ce1 | 2009-11-03 21:46:10 -0200 | [diff] [blame] | 1112 | if (size < BUILD_ID_SIZE) |
| 1113 | goto out; |
| 1114 | |
| 1115 | fd = open(filename, O_RDONLY); |
Arnaldo Carvalho de Melo | 4d1e00a | 2009-08-05 19:02:49 -0300 | [diff] [blame] | 1116 | if (fd < 0) |
| 1117 | goto out; |
| 1118 | |
Marti Raudsepp | 8408712 | 2009-10-24 19:10:36 +0300 | [diff] [blame] | 1119 | elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); |
Arnaldo Carvalho de Melo | 4d1e00a | 2009-08-05 19:02:49 -0300 | [diff] [blame] | 1120 | if (elf == NULL) { |
Arnaldo Carvalho de Melo | 8d06367 | 2009-11-04 18:50:43 -0200 | [diff] [blame] | 1121 | 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] | 1122 | goto out_close; |
| 1123 | } |
| 1124 | |
Pekka Enberg | e57cfcd | 2009-11-22 12:29:44 +0200 | [diff] [blame] | 1125 | ek = elf_kind(elf); |
| 1126 | if (ek != ELF_K_ELF) |
| 1127 | goto out_elf_end; |
| 1128 | |
Arnaldo Carvalho de Melo | 4d1e00a | 2009-08-05 19:02:49 -0300 | [diff] [blame] | 1129 | if (gelf_getehdr(elf, &ehdr) == NULL) { |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 1130 | pr_err("%s: cannot get elf header.\n", __func__); |
Arnaldo Carvalho de Melo | 4d1e00a | 2009-08-05 19:02:49 -0300 | [diff] [blame] | 1131 | goto out_elf_end; |
| 1132 | } |
| 1133 | |
Arnaldo Carvalho de Melo | 2643ce1 | 2009-11-03 21:46:10 -0200 | [diff] [blame] | 1134 | sec = elf_section_by_name(elf, &ehdr, &shdr, |
| 1135 | ".note.gnu.build-id", NULL); |
Arnaldo Carvalho de Melo | fd7a346 | 2009-11-20 20:51:25 -0200 | [diff] [blame] | 1136 | if (sec == NULL) { |
| 1137 | sec = elf_section_by_name(elf, &ehdr, &shdr, |
| 1138 | ".notes", NULL); |
| 1139 | if (sec == NULL) |
| 1140 | goto out_elf_end; |
| 1141 | } |
| 1142 | |
| 1143 | data = elf_getdata(sec, NULL); |
| 1144 | if (data == NULL) |
Arnaldo Carvalho de Melo | 4d1e00a | 2009-08-05 19:02:49 -0300 | [diff] [blame] | 1145 | goto out_elf_end; |
| 1146 | |
Arnaldo Carvalho de Melo | fd7a346 | 2009-11-20 20:51:25 -0200 | [diff] [blame] | 1147 | ptr = data->d_buf; |
| 1148 | while (ptr < (data->d_buf + data->d_size)) { |
| 1149 | GElf_Nhdr *nhdr = ptr; |
| 1150 | int namesz = NOTE_ALIGN(nhdr->n_namesz), |
| 1151 | descsz = NOTE_ALIGN(nhdr->n_descsz); |
| 1152 | const char *name; |
| 1153 | |
| 1154 | ptr += sizeof(*nhdr); |
| 1155 | name = ptr; |
| 1156 | ptr += namesz; |
| 1157 | if (nhdr->n_type == NT_GNU_BUILD_ID && |
| 1158 | nhdr->n_namesz == sizeof("GNU")) { |
| 1159 | if (memcmp(name, "GNU", sizeof("GNU")) == 0) { |
| 1160 | memcpy(bf, ptr, BUILD_ID_SIZE); |
| 1161 | err = BUILD_ID_SIZE; |
| 1162 | break; |
| 1163 | } |
| 1164 | } |
| 1165 | ptr += descsz; |
| 1166 | } |
Arnaldo Carvalho de Melo | 2643ce1 | 2009-11-03 21:46:10 -0200 | [diff] [blame] | 1167 | out_elf_end: |
| 1168 | elf_end(elf); |
| 1169 | out_close: |
| 1170 | close(fd); |
| 1171 | out: |
| 1172 | return err; |
| 1173 | } |
| 1174 | |
Arnaldo Carvalho de Melo | f1617b4 | 2009-11-18 20:20:52 -0200 | [diff] [blame] | 1175 | int sysfs__read_build_id(const char *filename, void *build_id, size_t size) |
| 1176 | { |
| 1177 | int fd, err = -1; |
| 1178 | |
| 1179 | if (size < BUILD_ID_SIZE) |
| 1180 | goto out; |
| 1181 | |
| 1182 | fd = open(filename, O_RDONLY); |
| 1183 | if (fd < 0) |
| 1184 | goto out; |
| 1185 | |
| 1186 | while (1) { |
| 1187 | char bf[BUFSIZ]; |
| 1188 | GElf_Nhdr nhdr; |
| 1189 | int namesz, descsz; |
| 1190 | |
| 1191 | if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr)) |
| 1192 | break; |
| 1193 | |
Arnaldo Carvalho de Melo | fd7a346 | 2009-11-20 20:51:25 -0200 | [diff] [blame] | 1194 | namesz = NOTE_ALIGN(nhdr.n_namesz); |
| 1195 | descsz = NOTE_ALIGN(nhdr.n_descsz); |
Arnaldo Carvalho de Melo | f1617b4 | 2009-11-18 20:20:52 -0200 | [diff] [blame] | 1196 | if (nhdr.n_type == NT_GNU_BUILD_ID && |
| 1197 | nhdr.n_namesz == sizeof("GNU")) { |
| 1198 | if (read(fd, bf, namesz) != namesz) |
| 1199 | break; |
| 1200 | if (memcmp(bf, "GNU", sizeof("GNU")) == 0) { |
| 1201 | if (read(fd, build_id, |
| 1202 | BUILD_ID_SIZE) == BUILD_ID_SIZE) { |
| 1203 | err = 0; |
| 1204 | break; |
| 1205 | } |
| 1206 | } else if (read(fd, bf, descsz) != descsz) |
| 1207 | break; |
| 1208 | } else { |
| 1209 | int n = namesz + descsz; |
| 1210 | if (read(fd, bf, n) != n) |
| 1211 | break; |
| 1212 | } |
| 1213 | } |
| 1214 | close(fd); |
| 1215 | out: |
| 1216 | return err; |
| 1217 | } |
| 1218 | |
Arnaldo Carvalho de Melo | 94cb9e3 | 2009-08-06 14:43:17 -0300 | [diff] [blame] | 1219 | char dso__symtab_origin(const struct dso *self) |
| 1220 | { |
| 1221 | static const char origin[] = { |
| 1222 | [DSO__ORIG_KERNEL] = 'k', |
| 1223 | [DSO__ORIG_JAVA_JIT] = 'j', |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 1224 | [DSO__ORIG_BUILD_ID_CACHE] = 'B', |
Arnaldo Carvalho de Melo | 94cb9e3 | 2009-08-06 14:43:17 -0300 | [diff] [blame] | 1225 | [DSO__ORIG_FEDORA] = 'f', |
| 1226 | [DSO__ORIG_UBUNTU] = 'u', |
| 1227 | [DSO__ORIG_BUILDID] = 'b', |
| 1228 | [DSO__ORIG_DSO] = 'd', |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1229 | [DSO__ORIG_KMODULE] = 'K', |
Arnaldo Carvalho de Melo | 94cb9e3 | 2009-08-06 14:43:17 -0300 | [diff] [blame] | 1230 | }; |
| 1231 | |
| 1232 | if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND) |
| 1233 | return '!'; |
| 1234 | return origin[self->origin]; |
| 1235 | } |
| 1236 | |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 1237 | int dso__load(struct dso *self, struct map *map, struct perf_session *session, |
| 1238 | symbol_filter_t filter) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1239 | { |
Arnaldo Carvalho de Melo | 4d1e00a | 2009-08-05 19:02:49 -0300 | [diff] [blame] | 1240 | int size = PATH_MAX; |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 1241 | char *name; |
Arnaldo Carvalho de Melo | d3379ab | 2009-11-18 20:20:50 -0200 | [diff] [blame] | 1242 | u8 build_id[BUILD_ID_SIZE]; |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 1243 | char build_id_hex[BUILD_ID_SIZE * 2 + 1]; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1244 | int ret = -1; |
| 1245 | int fd; |
| 1246 | |
Arnaldo Carvalho de Melo | 3610583 | 2009-11-27 16:29:16 -0200 | [diff] [blame] | 1247 | dso__set_loaded(self, map->type); |
Arnaldo Carvalho de Melo | 66bd842 | 2009-10-28 21:51:21 -0200 | [diff] [blame] | 1248 | |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 1249 | if (self->kernel) |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 1250 | return dso__load_kernel_sym(self, map, session, filter); |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 1251 | |
| 1252 | name = malloc(size); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1253 | if (!name) |
| 1254 | return -1; |
| 1255 | |
Arnaldo Carvalho de Melo | 30d7a77 | 2009-07-02 21:24:14 -0300 | [diff] [blame] | 1256 | self->adjust_symbols = 0; |
Arnaldo Carvalho de Melo | f5812a7 | 2009-06-30 11:43:17 -0300 | [diff] [blame] | 1257 | |
Arnaldo Carvalho de Melo | 94cb9e3 | 2009-08-06 14:43:17 -0300 | [diff] [blame] | 1258 | if (strncmp(self->name, "/tmp/perf-", 10) == 0) { |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 1259 | ret = dso__load_perf_map(self, map, filter); |
Arnaldo Carvalho de Melo | 94cb9e3 | 2009-08-06 14:43:17 -0300 | [diff] [blame] | 1260 | self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT : |
| 1261 | DSO__ORIG_NOT_FOUND; |
| 1262 | return ret; |
| 1263 | } |
| 1264 | |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 1265 | self->origin = DSO__ORIG_BUILD_ID_CACHE; |
Pekka Enberg | 80d496b | 2009-06-08 21:12:48 +0300 | [diff] [blame] | 1266 | |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 1267 | if (self->has_build_id) { |
| 1268 | build_id__sprintf(self->build_id, sizeof(self->build_id), |
| 1269 | build_id_hex); |
| 1270 | snprintf(name, size, "%s/%s/.build-id/%.2s/%s", |
| 1271 | getenv("HOME"), DEBUG_CACHE_DIR, |
| 1272 | build_id_hex, build_id_hex + 2); |
| 1273 | goto open_file; |
| 1274 | } |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1275 | more: |
| 1276 | do { |
Arnaldo Carvalho de Melo | 94cb9e3 | 2009-08-06 14:43:17 -0300 | [diff] [blame] | 1277 | self->origin++; |
| 1278 | switch (self->origin) { |
| 1279 | case DSO__ORIG_FEDORA: |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1280 | snprintf(name, size, "/usr/lib/debug%s.debug", |
| 1281 | self->long_name); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1282 | break; |
Arnaldo Carvalho de Melo | 94cb9e3 | 2009-08-06 14:43:17 -0300 | [diff] [blame] | 1283 | case DSO__ORIG_UBUNTU: |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1284 | snprintf(name, size, "/usr/lib/debug%s", |
| 1285 | self->long_name); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1286 | break; |
Arnaldo Carvalho de Melo | 94cb9e3 | 2009-08-06 14:43:17 -0300 | [diff] [blame] | 1287 | case DSO__ORIG_BUILDID: |
Arnaldo Carvalho de Melo | d3379ab | 2009-11-18 20:20:50 -0200 | [diff] [blame] | 1288 | if (filename__read_build_id(self->long_name, build_id, |
| 1289 | sizeof(build_id))) { |
Arnaldo Carvalho de Melo | d3379ab | 2009-11-18 20:20:50 -0200 | [diff] [blame] | 1290 | build_id__sprintf(build_id, sizeof(build_id), |
| 1291 | build_id_hex); |
Arnaldo Carvalho de Melo | 4d1e00a | 2009-08-05 19:02:49 -0300 | [diff] [blame] | 1292 | snprintf(name, size, |
| 1293 | "/usr/lib/debug/.build-id/%.2s/%s.debug", |
Arnaldo Carvalho de Melo | d3379ab | 2009-11-18 20:20:50 -0200 | [diff] [blame] | 1294 | build_id_hex, build_id_hex + 2); |
| 1295 | if (self->has_build_id) |
| 1296 | goto compare_build_id; |
| 1297 | break; |
Arnaldo Carvalho de Melo | 4d1e00a | 2009-08-05 19:02:49 -0300 | [diff] [blame] | 1298 | } |
Arnaldo Carvalho de Melo | 94cb9e3 | 2009-08-06 14:43:17 -0300 | [diff] [blame] | 1299 | self->origin++; |
Arnaldo Carvalho de Melo | 4d1e00a | 2009-08-05 19:02:49 -0300 | [diff] [blame] | 1300 | /* Fall thru */ |
Arnaldo Carvalho de Melo | 94cb9e3 | 2009-08-06 14:43:17 -0300 | [diff] [blame] | 1301 | case DSO__ORIG_DSO: |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1302 | snprintf(name, size, "%s", self->long_name); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1303 | break; |
| 1304 | |
| 1305 | default: |
| 1306 | goto out; |
| 1307 | } |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1308 | |
Arnaldo Carvalho de Melo | 8d06367 | 2009-11-04 18:50:43 -0200 | [diff] [blame] | 1309 | if (self->has_build_id) { |
Arnaldo Carvalho de Melo | d3379ab | 2009-11-18 20:20:50 -0200 | [diff] [blame] | 1310 | if (filename__read_build_id(name, build_id, |
| 1311 | sizeof(build_id)) < 0) |
Arnaldo Carvalho de Melo | 8d06367 | 2009-11-04 18:50:43 -0200 | [diff] [blame] | 1312 | goto more; |
Arnaldo Carvalho de Melo | 8d06367 | 2009-11-04 18:50:43 -0200 | [diff] [blame] | 1313 | compare_build_id: |
Arnaldo Carvalho de Melo | 78075ca | 2009-11-20 20:51:26 -0200 | [diff] [blame] | 1314 | if (!dso__build_id_equal(self, build_id)) |
Arnaldo Carvalho de Melo | 8d06367 | 2009-11-04 18:50:43 -0200 | [diff] [blame] | 1315 | goto more; |
| 1316 | } |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 1317 | open_file: |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1318 | fd = open(name, O_RDONLY); |
| 1319 | } while (fd < 0); |
| 1320 | |
Arnaldo Carvalho de Melo | 95011c6 | 2009-11-27 16:29:20 -0200 | [diff] [blame] | 1321 | 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] | 1322 | close(fd); |
| 1323 | |
| 1324 | /* |
| 1325 | * Some people seem to have debuginfo files _WITHOUT_ debug info!?!? |
| 1326 | */ |
| 1327 | if (!ret) |
| 1328 | goto more; |
| 1329 | |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 1330 | if (ret > 0) { |
Arnaldo Carvalho de Melo | 8216416 | 2009-11-16 13:48:11 -0200 | [diff] [blame] | 1331 | int nr_plt = dso__synthesize_plt_symbols(self, map, filter); |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 1332 | if (nr_plt > 0) |
| 1333 | ret += nr_plt; |
| 1334 | } |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1335 | out: |
| 1336 | free(name); |
Arnaldo Carvalho de Melo | 1340e6b | 2009-08-11 17:04:36 -0300 | [diff] [blame] | 1337 | if (ret < 0 && strstr(self->name, " (deleted)") != NULL) |
| 1338 | return 0; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1339 | return ret; |
| 1340 | } |
| 1341 | |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 1342 | struct map *map_groups__find_by_name(struct map_groups *self, |
| 1343 | enum map_type type, const char *name) |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1344 | { |
| 1345 | struct rb_node *nd; |
| 1346 | |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 1347 | 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] | 1348 | struct map *map = rb_entry(nd, struct map, rb_node); |
| 1349 | |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 1350 | if (map->dso && strcmp(map->dso->short_name, name) == 0) |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1351 | return map; |
| 1352 | } |
| 1353 | |
| 1354 | return NULL; |
| 1355 | } |
| 1356 | |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 1357 | static int dso__kernel_module_get_build_id(struct dso *self) |
| 1358 | { |
| 1359 | char filename[PATH_MAX]; |
| 1360 | /* |
| 1361 | * kernel module short names are of the form "[module]" and |
| 1362 | * we need just "module" here. |
| 1363 | */ |
| 1364 | const char *name = self->short_name + 1; |
| 1365 | |
| 1366 | snprintf(filename, sizeof(filename), |
| 1367 | "/sys/module/%.*s/notes/.note.gnu.build-id", |
| 1368 | (int)strlen(name - 1), name); |
| 1369 | |
| 1370 | if (sysfs__read_build_id(filename, self->build_id, |
| 1371 | sizeof(self->build_id)) == 0) |
| 1372 | self->has_build_id = true; |
| 1373 | |
| 1374 | return 0; |
| 1375 | } |
| 1376 | |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 1377 | 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] | 1378 | { |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1379 | struct dirent *dent; |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1380 | DIR *dir = opendir(dirname); |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 1381 | |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1382 | if (!dir) { |
Arnaldo Carvalho de Melo | 87f8ea4 | 2009-11-22 13:21:41 -0200 | [diff] [blame] | 1383 | pr_debug("%s: cannot open %s dir\n", __func__, dirname); |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1384 | return -1; |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 1385 | } |
| 1386 | |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1387 | while ((dent = readdir(dir)) != NULL) { |
| 1388 | char path[PATH_MAX]; |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 1389 | |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1390 | if (dent->d_type == DT_DIR) { |
| 1391 | if (!strcmp(dent->d_name, ".") || |
| 1392 | !strcmp(dent->d_name, "..")) |
| 1393 | continue; |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 1394 | |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1395 | snprintf(path, sizeof(path), "%s/%s", |
| 1396 | dirname, dent->d_name); |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 1397 | if (perf_session__set_modules_path_dir(self, path) < 0) |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1398 | goto failure; |
| 1399 | } else { |
| 1400 | char *dot = strrchr(dent->d_name, '.'), |
| 1401 | dso_name[PATH_MAX]; |
| 1402 | struct map *map; |
Arnaldo Carvalho de Melo | cfc10d3 | 2009-11-17 15:40:53 -0200 | [diff] [blame] | 1403 | char *long_name; |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 1404 | |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1405 | if (dot == NULL || strcmp(dot, ".ko")) |
| 1406 | continue; |
| 1407 | snprintf(dso_name, sizeof(dso_name), "[%.*s]", |
| 1408 | (int)(dot - dent->d_name), dent->d_name); |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 1409 | |
Arnaldo Carvalho de Melo | a2a99e8 | 2009-10-05 14:26:18 -0300 | [diff] [blame] | 1410 | strxfrchar(dso_name, '-', '_'); |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 1411 | 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] | 1412 | if (map == NULL) |
| 1413 | continue; |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 1414 | |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1415 | snprintf(path, sizeof(path), "%s/%s", |
| 1416 | dirname, dent->d_name); |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 1417 | |
Arnaldo Carvalho de Melo | cfc10d3 | 2009-11-17 15:40:53 -0200 | [diff] [blame] | 1418 | long_name = strdup(path); |
| 1419 | if (long_name == NULL) |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1420 | goto failure; |
Arnaldo Carvalho de Melo | cfc10d3 | 2009-11-17 15:40:53 -0200 | [diff] [blame] | 1421 | dso__set_long_name(map->dso, long_name); |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 1422 | dso__kernel_module_get_build_id(map->dso); |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 1423 | } |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 1424 | } |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1425 | |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 1426 | return 0; |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1427 | failure: |
| 1428 | closedir(dir); |
| 1429 | return -1; |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 1430 | } |
| 1431 | |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 1432 | 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] | 1433 | { |
| 1434 | struct utsname uts; |
| 1435 | char modules_path[PATH_MAX]; |
| 1436 | |
| 1437 | if (uname(&uts) < 0) |
| 1438 | return -1; |
| 1439 | |
| 1440 | snprintf(modules_path, sizeof(modules_path), "/lib/modules/%s/kernel", |
| 1441 | uts.release); |
| 1442 | |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 1443 | return perf_session__set_modules_path_dir(self, modules_path); |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1444 | } |
| 1445 | |
| 1446 | /* |
| 1447 | * Constructor variant for modules (where we know from /proc/modules where |
| 1448 | * they are loaded) and for vmlinux, where only after we load all the |
| 1449 | * symbols we'll know where it starts and ends. |
| 1450 | */ |
Arnaldo Carvalho de Melo | 3610583 | 2009-11-27 16:29:16 -0200 | [diff] [blame] | 1451 | 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] | 1452 | { |
| 1453 | struct map *self = malloc(sizeof(*self)); |
| 1454 | |
| 1455 | if (self != NULL) { |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1456 | /* |
Arnaldo Carvalho de Melo | afb7b4f | 2009-10-30 16:28:23 -0200 | [diff] [blame] | 1457 | * ->end will be filled after we load all the symbols |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1458 | */ |
Arnaldo Carvalho de Melo | 3610583 | 2009-11-27 16:29:16 -0200 | [diff] [blame] | 1459 | map__init(self, type, start, 0, 0, dso); |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1460 | } |
Arnaldo Carvalho de Melo | afb7b4f | 2009-10-30 16:28:23 -0200 | [diff] [blame] | 1461 | |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1462 | return self; |
| 1463 | } |
| 1464 | |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 1465 | struct map *perf_session__new_module_map(struct perf_session *self, u64 start, |
| 1466 | const char *filename) |
| 1467 | { |
| 1468 | struct map *map; |
| 1469 | struct dso *dso = __dsos__findnew(&dsos__kernel, filename); |
| 1470 | |
| 1471 | if (dso == NULL) |
| 1472 | return NULL; |
| 1473 | |
| 1474 | map = map__new2(start, dso, MAP__FUNCTION); |
| 1475 | if (map == NULL) |
| 1476 | return NULL; |
| 1477 | |
| 1478 | dso->origin = DSO__ORIG_KMODULE; |
| 1479 | map_groups__insert(&self->kmaps, map); |
| 1480 | return map; |
| 1481 | } |
| 1482 | |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 1483 | 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] | 1484 | { |
| 1485 | char *line = NULL; |
| 1486 | size_t n; |
| 1487 | FILE *file = fopen("/proc/modules", "r"); |
| 1488 | struct map *map; |
| 1489 | |
| 1490 | if (file == NULL) |
| 1491 | return -1; |
| 1492 | |
| 1493 | while (!feof(file)) { |
| 1494 | char name[PATH_MAX]; |
| 1495 | u64 start; |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1496 | char *sep; |
| 1497 | int line_len; |
| 1498 | |
| 1499 | line_len = getline(&line, &n, file); |
| 1500 | if (line_len < 0) |
| 1501 | break; |
| 1502 | |
| 1503 | if (!line) |
| 1504 | goto out_failure; |
| 1505 | |
| 1506 | line[--line_len] = '\0'; /* \n */ |
| 1507 | |
| 1508 | sep = strrchr(line, 'x'); |
| 1509 | if (sep == NULL) |
| 1510 | continue; |
| 1511 | |
| 1512 | hex2u64(sep + 1, &start); |
| 1513 | |
| 1514 | sep = strchr(line, ' '); |
| 1515 | if (sep == NULL) |
| 1516 | continue; |
| 1517 | |
| 1518 | *sep = '\0'; |
| 1519 | |
| 1520 | snprintf(name, sizeof(name), "[%s]", line); |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 1521 | map = perf_session__new_module_map(self, start, name); |
| 1522 | if (map == NULL) |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1523 | goto out_delete_line; |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 1524 | dso__kernel_module_get_build_id(map->dso); |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1525 | } |
| 1526 | |
| 1527 | free(line); |
| 1528 | fclose(file); |
| 1529 | |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 1530 | return perf_session__set_modules_path(self); |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1531 | |
| 1532 | out_delete_line: |
| 1533 | free(line); |
| 1534 | out_failure: |
| 1535 | return -1; |
| 1536 | } |
| 1537 | |
Arnaldo Carvalho de Melo | 9958e1f | 2009-12-11 14:50:36 -0200 | [diff] [blame] | 1538 | 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] | 1539 | struct perf_session *session, |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 1540 | const char *vmlinux, symbol_filter_t filter) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1541 | { |
Arnaldo Carvalho de Melo | fbd733b | 2009-11-20 20:51:28 -0200 | [diff] [blame] | 1542 | int err = -1, fd; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1543 | |
Arnaldo Carvalho de Melo | fbd733b | 2009-11-20 20:51:28 -0200 | [diff] [blame] | 1544 | if (self->has_build_id) { |
| 1545 | u8 build_id[BUILD_ID_SIZE]; |
Arnaldo Carvalho de Melo | 66bd842 | 2009-10-28 21:51:21 -0200 | [diff] [blame] | 1546 | |
Arnaldo Carvalho de Melo | fbd733b | 2009-11-20 20:51:28 -0200 | [diff] [blame] | 1547 | if (filename__read_build_id(vmlinux, build_id, |
| 1548 | sizeof(build_id)) < 0) { |
| 1549 | pr_debug("No build_id in %s, ignoring it\n", vmlinux); |
| 1550 | return -1; |
| 1551 | } |
| 1552 | if (!dso__build_id_equal(self, build_id)) { |
| 1553 | char expected_build_id[BUILD_ID_SIZE * 2 + 1], |
| 1554 | vmlinux_build_id[BUILD_ID_SIZE * 2 + 1]; |
| 1555 | |
| 1556 | build_id__sprintf(self->build_id, |
| 1557 | sizeof(self->build_id), |
| 1558 | expected_build_id); |
| 1559 | build_id__sprintf(build_id, sizeof(build_id), |
| 1560 | vmlinux_build_id); |
| 1561 | pr_debug("build_id in %s is %s while expected is %s, " |
| 1562 | "ignoring it\n", vmlinux, vmlinux_build_id, |
| 1563 | expected_build_id); |
| 1564 | return -1; |
| 1565 | } |
| 1566 | } |
| 1567 | |
| 1568 | fd = open(vmlinux, O_RDONLY); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1569 | if (fd < 0) |
| 1570 | return -1; |
| 1571 | |
Arnaldo Carvalho de Melo | 3610583 | 2009-11-27 16:29:16 -0200 | [diff] [blame] | 1572 | dso__set_loaded(self, map->type); |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 1573 | 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] | 1574 | close(fd); |
| 1575 | |
| 1576 | return err; |
| 1577 | } |
| 1578 | |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 1579 | 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] | 1580 | struct perf_session *session, symbol_filter_t filter) |
Arnaldo Carvalho de Melo | a827c87 | 2009-05-28 14:55:19 -0300 | [diff] [blame] | 1581 | { |
Arnaldo Carvalho de Melo | cc612d8 | 2009-11-23 16:39:10 -0200 | [diff] [blame] | 1582 | int err; |
| 1583 | bool is_kallsyms; |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1584 | |
Arnaldo Carvalho de Melo | cc612d8 | 2009-11-23 16:39:10 -0200 | [diff] [blame] | 1585 | if (vmlinux_path != NULL) { |
| 1586 | int i; |
| 1587 | pr_debug("Looking at the vmlinux_path (%d entries long)\n", |
| 1588 | vmlinux_path__nr_entries); |
| 1589 | for (i = 0; i < vmlinux_path__nr_entries; ++i) { |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 1590 | err = dso__load_vmlinux(self, map, session, |
Arnaldo Carvalho de Melo | 95011c6 | 2009-11-27 16:29:20 -0200 | [diff] [blame] | 1591 | vmlinux_path[i], filter); |
Arnaldo Carvalho de Melo | cc612d8 | 2009-11-23 16:39:10 -0200 | [diff] [blame] | 1592 | if (err > 0) { |
| 1593 | pr_debug("Using %s for symbols\n", |
| 1594 | vmlinux_path[i]); |
| 1595 | dso__set_long_name(self, |
| 1596 | strdup(vmlinux_path[i])); |
| 1597 | goto out_fixup; |
| 1598 | } |
| 1599 | } |
| 1600 | } |
| 1601 | |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 1602 | /* |
| 1603 | * Say the kernel DSO was created when processing the build-id header table, |
| 1604 | * we have a build-id, so check if it is the same as the running kernel, |
| 1605 | * using it if it is. |
| 1606 | */ |
| 1607 | if (self->has_build_id) { |
| 1608 | u8 kallsyms_build_id[BUILD_ID_SIZE]; |
| 1609 | |
| 1610 | if (sysfs__read_build_id("/sys/kernel/notes", kallsyms_build_id, |
| 1611 | sizeof(kallsyms_build_id)) == 0) |
| 1612 | |
| 1613 | is_kallsyms = dso__build_id_equal(self, kallsyms_build_id); |
| 1614 | if (is_kallsyms) |
| 1615 | goto do_kallsyms; |
| 1616 | goto do_vmlinux; |
| 1617 | } |
| 1618 | |
Arnaldo Carvalho de Melo | cc612d8 | 2009-11-23 16:39:10 -0200 | [diff] [blame] | 1619 | is_kallsyms = self->long_name[0] == '['; |
| 1620 | if (is_kallsyms) |
| 1621 | goto do_kallsyms; |
| 1622 | |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 1623 | do_vmlinux: |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 1624 | 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] | 1625 | if (err <= 0) { |
Arnaldo Carvalho de Melo | cc612d8 | 2009-11-23 16:39:10 -0200 | [diff] [blame] | 1626 | pr_info("The file %s cannot be used, " |
| 1627 | "trying to use /proc/kallsyms...", self->long_name); |
Arnaldo Carvalho de Melo | cc612d8 | 2009-11-23 16:39:10 -0200 | [diff] [blame] | 1628 | do_kallsyms: |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 1629 | err = dso__load_kallsyms(self, map, session, filter); |
Arnaldo Carvalho de Melo | cc612d8 | 2009-11-23 16:39:10 -0200 | [diff] [blame] | 1630 | if (err > 0 && !is_kallsyms) |
Arnaldo Carvalho de Melo | ef6ae72 | 2009-11-20 20:51:29 -0200 | [diff] [blame] | 1631 | dso__set_long_name(self, strdup("[kernel.kallsyms]")); |
| 1632 | } |
Arnaldo Carvalho de Melo | a827c87 | 2009-05-28 14:55:19 -0300 | [diff] [blame] | 1633 | |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1634 | if (err > 0) { |
Arnaldo Carvalho de Melo | cc612d8 | 2009-11-23 16:39:10 -0200 | [diff] [blame] | 1635 | out_fixup: |
Arnaldo Carvalho de Melo | 6a4694a | 2009-11-27 16:29:17 -0200 | [diff] [blame] | 1636 | map__fixup_start(map); |
| 1637 | map__fixup_end(map); |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1638 | } |
Arnaldo Carvalho de Melo | 94cb9e3 | 2009-08-06 14:43:17 -0300 | [diff] [blame] | 1639 | |
Arnaldo Carvalho de Melo | a827c87 | 2009-05-28 14:55:19 -0300 | [diff] [blame] | 1640 | return err; |
| 1641 | } |
| 1642 | |
Arnaldo Carvalho de Melo | b0da954 | 2009-11-27 16:29:14 -0200 | [diff] [blame] | 1643 | LIST_HEAD(dsos__user); |
| 1644 | LIST_HEAD(dsos__kernel); |
Arnaldo Carvalho de Melo | cc612d8 | 2009-11-23 16:39:10 -0200 | [diff] [blame] | 1645 | struct dso *vdso; |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 1646 | |
Arnaldo Carvalho de Melo | b0da954 | 2009-11-27 16:29:14 -0200 | [diff] [blame] | 1647 | static void dsos__add(struct list_head *head, struct dso *dso) |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 1648 | { |
Arnaldo Carvalho de Melo | b0da954 | 2009-11-27 16:29:14 -0200 | [diff] [blame] | 1649 | list_add_tail(&dso->node, head); |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 1650 | } |
| 1651 | |
Arnaldo Carvalho de Melo | b0da954 | 2009-11-27 16:29:14 -0200 | [diff] [blame] | 1652 | static struct dso *dsos__find(struct list_head *head, const char *name) |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 1653 | { |
| 1654 | struct dso *pos; |
| 1655 | |
Arnaldo Carvalho de Melo | b0da954 | 2009-11-27 16:29:14 -0200 | [diff] [blame] | 1656 | list_for_each_entry(pos, head, node) |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 1657 | if (strcmp(pos->name, name) == 0) |
| 1658 | return pos; |
| 1659 | return NULL; |
| 1660 | } |
| 1661 | |
Arnaldo Carvalho de Melo | a89e5ab | 2010-01-07 19:59:39 -0200 | [diff] [blame] | 1662 | struct dso *__dsos__findnew(struct list_head *head, const char *name) |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 1663 | { |
Arnaldo Carvalho de Melo | a89e5ab | 2010-01-07 19:59:39 -0200 | [diff] [blame] | 1664 | struct dso *dso = dsos__find(head, name); |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 1665 | |
Arnaldo Carvalho de Melo | e420499 | 2009-10-20 14:25:40 -0200 | [diff] [blame] | 1666 | if (!dso) { |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 1667 | dso = dso__new(name); |
Arnaldo Carvalho de Melo | cfc10d3 | 2009-11-17 15:40:53 -0200 | [diff] [blame] | 1668 | if (dso != NULL) { |
Arnaldo Carvalho de Melo | a89e5ab | 2010-01-07 19:59:39 -0200 | [diff] [blame] | 1669 | dsos__add(head, dso); |
Arnaldo Carvalho de Melo | cfc10d3 | 2009-11-17 15:40:53 -0200 | [diff] [blame] | 1670 | dso__set_basename(dso); |
| 1671 | } |
Arnaldo Carvalho de Melo | 66bd842 | 2009-10-28 21:51:21 -0200 | [diff] [blame] | 1672 | } |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 1673 | |
| 1674 | return dso; |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 1675 | } |
| 1676 | |
Arnaldo Carvalho de Melo | b0da954 | 2009-11-27 16:29:14 -0200 | [diff] [blame] | 1677 | static void __dsos__fprintf(struct list_head *head, FILE *fp) |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 1678 | { |
| 1679 | struct dso *pos; |
| 1680 | |
Arnaldo Carvalho de Melo | 95011c6 | 2009-11-27 16:29:20 -0200 | [diff] [blame] | 1681 | list_for_each_entry(pos, head, node) { |
| 1682 | int i; |
| 1683 | for (i = 0; i < MAP__NR_TYPES; ++i) |
| 1684 | dso__fprintf(pos, i, fp); |
| 1685 | } |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 1686 | } |
| 1687 | |
Arnaldo Carvalho de Melo | b0da954 | 2009-11-27 16:29:14 -0200 | [diff] [blame] | 1688 | void dsos__fprintf(FILE *fp) |
| 1689 | { |
| 1690 | __dsos__fprintf(&dsos__kernel, fp); |
| 1691 | __dsos__fprintf(&dsos__user, fp); |
| 1692 | } |
| 1693 | |
| 1694 | 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] | 1695 | { |
| 1696 | struct dso *pos; |
| 1697 | size_t ret = 0; |
| 1698 | |
Arnaldo Carvalho de Melo | b0da954 | 2009-11-27 16:29:14 -0200 | [diff] [blame] | 1699 | list_for_each_entry(pos, head, node) { |
Arnaldo Carvalho de Melo | 9e03eb2 | 2009-11-16 16:32:44 -0200 | [diff] [blame] | 1700 | ret += dso__fprintf_buildid(pos, fp); |
Arnaldo Carvalho de Melo | 1124ba7 | 2009-11-16 21:45:25 -0200 | [diff] [blame] | 1701 | ret += fprintf(fp, " %s\n", pos->long_name); |
Arnaldo Carvalho de Melo | 9e03eb2 | 2009-11-16 16:32:44 -0200 | [diff] [blame] | 1702 | } |
| 1703 | return ret; |
| 1704 | } |
| 1705 | |
Arnaldo Carvalho de Melo | b0da954 | 2009-11-27 16:29:14 -0200 | [diff] [blame] | 1706 | size_t dsos__fprintf_buildid(FILE *fp) |
| 1707 | { |
| 1708 | return (__dsos__fprintf_buildid(&dsos__kernel, fp) + |
| 1709 | __dsos__fprintf_buildid(&dsos__user, fp)); |
| 1710 | } |
| 1711 | |
Arnaldo Carvalho de Melo | de17648 | 2010-01-04 16:19:29 -0200 | [diff] [blame] | 1712 | static struct dso *dsos__create_kernel(const char *vmlinux) |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 1713 | { |
Arnaldo Carvalho de Melo | 95011c6 | 2009-11-27 16:29:20 -0200 | [diff] [blame] | 1714 | struct dso *kernel = dso__new(vmlinux ?: "[kernel.kallsyms]"); |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 1715 | |
Arnaldo Carvalho de Melo | 2446042 | 2009-11-18 20:20:53 -0200 | [diff] [blame] | 1716 | if (kernel == NULL) |
Arnaldo Carvalho de Melo | f1dfa0b | 2009-12-11 14:50:39 -0200 | [diff] [blame] | 1717 | return NULL; |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 1718 | |
Arnaldo Carvalho de Melo | 4e06255 | 2009-11-27 16:29:18 -0200 | [diff] [blame] | 1719 | kernel->short_name = "[kernel]"; |
| 1720 | kernel->kernel = 1; |
Arnaldo Carvalho de Melo | 2446042 | 2009-11-18 20:20:53 -0200 | [diff] [blame] | 1721 | |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 1722 | vdso = dso__new("[vdso]"); |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 1723 | if (vdso == NULL) |
Arnaldo Carvalho de Melo | f1dfa0b | 2009-12-11 14:50:39 -0200 | [diff] [blame] | 1724 | goto out_delete_kernel_dso; |
Arnaldo Carvalho de Melo | 3610583 | 2009-11-27 16:29:16 -0200 | [diff] [blame] | 1725 | dso__set_loaded(vdso, MAP__FUNCTION); |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 1726 | |
Arnaldo Carvalho de Melo | 2446042 | 2009-11-18 20:20:53 -0200 | [diff] [blame] | 1727 | if (sysfs__read_build_id("/sys/kernel/notes", kernel->build_id, |
| 1728 | sizeof(kernel->build_id)) == 0) |
| 1729 | kernel->has_build_id = true; |
| 1730 | |
Arnaldo Carvalho de Melo | b0da954 | 2009-11-27 16:29:14 -0200 | [diff] [blame] | 1731 | dsos__add(&dsos__kernel, kernel); |
| 1732 | dsos__add(&dsos__user, vdso); |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 1733 | |
Arnaldo Carvalho de Melo | f1dfa0b | 2009-12-11 14:50:39 -0200 | [diff] [blame] | 1734 | return kernel; |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 1735 | |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 1736 | out_delete_kernel_dso: |
| 1737 | dso__delete(kernel); |
Arnaldo Carvalho de Melo | f1dfa0b | 2009-12-11 14:50:39 -0200 | [diff] [blame] | 1738 | return NULL; |
| 1739 | } |
| 1740 | |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 1741 | int __map_groups__create_kernel_maps(struct map_groups *self, |
| 1742 | struct map *vmlinux_maps[MAP__NR_TYPES], |
| 1743 | struct dso *kernel) |
Arnaldo Carvalho de Melo | f1dfa0b | 2009-12-11 14:50:39 -0200 | [diff] [blame] | 1744 | { |
Arnaldo Carvalho de Melo | de17648 | 2010-01-04 16:19:29 -0200 | [diff] [blame] | 1745 | enum map_type type; |
Arnaldo Carvalho de Melo | f1dfa0b | 2009-12-11 14:50:39 -0200 | [diff] [blame] | 1746 | |
Arnaldo Carvalho de Melo | de17648 | 2010-01-04 16:19:29 -0200 | [diff] [blame] | 1747 | for (type = 0; type < MAP__NR_TYPES; ++type) { |
| 1748 | vmlinux_maps[type] = map__new2(0, kernel, type); |
| 1749 | if (vmlinux_maps[type] == NULL) |
| 1750 | return -1; |
Arnaldo Carvalho de Melo | f1dfa0b | 2009-12-11 14:50:39 -0200 | [diff] [blame] | 1751 | |
Arnaldo Carvalho de Melo | de17648 | 2010-01-04 16:19:29 -0200 | [diff] [blame] | 1752 | vmlinux_maps[type]->map_ip = |
| 1753 | vmlinux_maps[type]->unmap_ip = identity__map_ip; |
| 1754 | map_groups__insert(self, vmlinux_maps[type]); |
Arnaldo Carvalho de Melo | f1dfa0b | 2009-12-11 14:50:39 -0200 | [diff] [blame] | 1755 | } |
| 1756 | |
Arnaldo Carvalho de Melo | f1dfa0b | 2009-12-11 14:50:39 -0200 | [diff] [blame] | 1757 | return 0; |
Arnaldo Carvalho de Melo | 2446042 | 2009-11-18 20:20:53 -0200 | [diff] [blame] | 1758 | } |
| 1759 | |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 1760 | static int map_groups__create_kernel_maps(struct map_groups *self, |
| 1761 | struct map *vmlinux_maps[MAP__NR_TYPES], |
| 1762 | const char *vmlinux) |
| 1763 | { |
| 1764 | struct dso *kernel = dsos__create_kernel(vmlinux); |
| 1765 | |
| 1766 | if (kernel == NULL) |
| 1767 | return -1; |
| 1768 | |
| 1769 | return __map_groups__create_kernel_maps(self, vmlinux_maps, kernel); |
| 1770 | } |
| 1771 | |
Arnaldo Carvalho de Melo | cc612d8 | 2009-11-23 16:39:10 -0200 | [diff] [blame] | 1772 | static void vmlinux_path__exit(void) |
Arnaldo Carvalho de Melo | 2446042 | 2009-11-18 20:20:53 -0200 | [diff] [blame] | 1773 | { |
Arnaldo Carvalho de Melo | cc612d8 | 2009-11-23 16:39:10 -0200 | [diff] [blame] | 1774 | while (--vmlinux_path__nr_entries >= 0) { |
| 1775 | free(vmlinux_path[vmlinux_path__nr_entries]); |
| 1776 | vmlinux_path[vmlinux_path__nr_entries] = NULL; |
| 1777 | } |
| 1778 | |
| 1779 | free(vmlinux_path); |
| 1780 | vmlinux_path = NULL; |
| 1781 | } |
| 1782 | |
| 1783 | static int vmlinux_path__init(void) |
| 1784 | { |
| 1785 | struct utsname uts; |
| 1786 | char bf[PATH_MAX]; |
| 1787 | |
| 1788 | if (uname(&uts) < 0) |
Arnaldo Carvalho de Melo | 2446042 | 2009-11-18 20:20:53 -0200 | [diff] [blame] | 1789 | return -1; |
| 1790 | |
Arnaldo Carvalho de Melo | cc612d8 | 2009-11-23 16:39:10 -0200 | [diff] [blame] | 1791 | vmlinux_path = malloc(sizeof(char *) * 5); |
| 1792 | if (vmlinux_path == NULL) |
| 1793 | return -1; |
| 1794 | |
| 1795 | vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux"); |
| 1796 | if (vmlinux_path[vmlinux_path__nr_entries] == NULL) |
| 1797 | goto out_fail; |
| 1798 | ++vmlinux_path__nr_entries; |
| 1799 | vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux"); |
| 1800 | if (vmlinux_path[vmlinux_path__nr_entries] == NULL) |
| 1801 | goto out_fail; |
| 1802 | ++vmlinux_path__nr_entries; |
| 1803 | snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release); |
| 1804 | vmlinux_path[vmlinux_path__nr_entries] = strdup(bf); |
| 1805 | if (vmlinux_path[vmlinux_path__nr_entries] == NULL) |
| 1806 | goto out_fail; |
| 1807 | ++vmlinux_path__nr_entries; |
| 1808 | snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release); |
| 1809 | vmlinux_path[vmlinux_path__nr_entries] = strdup(bf); |
| 1810 | if (vmlinux_path[vmlinux_path__nr_entries] == NULL) |
| 1811 | goto out_fail; |
| 1812 | ++vmlinux_path__nr_entries; |
| 1813 | snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux", |
| 1814 | uts.release); |
| 1815 | vmlinux_path[vmlinux_path__nr_entries] = strdup(bf); |
| 1816 | if (vmlinux_path[vmlinux_path__nr_entries] == NULL) |
| 1817 | goto out_fail; |
| 1818 | ++vmlinux_path__nr_entries; |
| 1819 | |
| 1820 | return 0; |
| 1821 | |
| 1822 | out_fail: |
| 1823 | vmlinux_path__exit(); |
| 1824 | return -1; |
| 1825 | } |
| 1826 | |
Arnaldo Carvalho de Melo | 655000e | 2009-12-15 20:04:40 -0200 | [diff] [blame] | 1827 | static int setup_list(struct strlist **list, const char *list_str, |
| 1828 | const char *list_name) |
| 1829 | { |
| 1830 | if (list_str == NULL) |
| 1831 | return 0; |
| 1832 | |
| 1833 | *list = strlist__new(true, list_str); |
| 1834 | if (!*list) { |
| 1835 | pr_err("problems parsing %s list\n", list_name); |
| 1836 | return -1; |
| 1837 | } |
| 1838 | return 0; |
| 1839 | } |
| 1840 | |
Arnaldo Carvalho de Melo | 75be6cf | 2009-12-15 20:04:39 -0200 | [diff] [blame] | 1841 | int symbol__init(void) |
Arnaldo Carvalho de Melo | cc612d8 | 2009-11-23 16:39:10 -0200 | [diff] [blame] | 1842 | { |
Arnaldo Carvalho de Melo | 95011c6 | 2009-11-27 16:29:20 -0200 | [diff] [blame] | 1843 | elf_version(EV_CURRENT); |
Arnaldo Carvalho de Melo | 75be6cf | 2009-12-15 20:04:39 -0200 | [diff] [blame] | 1844 | if (symbol_conf.sort_by_name) |
| 1845 | symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) - |
| 1846 | sizeof(struct symbol)); |
Arnaldo Carvalho de Melo | b32d133 | 2009-11-24 12:05:15 -0200 | [diff] [blame] | 1847 | |
Arnaldo Carvalho de Melo | 75be6cf | 2009-12-15 20:04:39 -0200 | [diff] [blame] | 1848 | if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0) |
Arnaldo Carvalho de Melo | cc612d8 | 2009-11-23 16:39:10 -0200 | [diff] [blame] | 1849 | return -1; |
| 1850 | |
Arnaldo Carvalho de Melo | c410a33 | 2009-12-15 20:04:41 -0200 | [diff] [blame] | 1851 | if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') { |
| 1852 | pr_err("'.' is the only non valid --field-separator argument\n"); |
| 1853 | return -1; |
| 1854 | } |
| 1855 | |
Arnaldo Carvalho de Melo | 655000e | 2009-12-15 20:04:40 -0200 | [diff] [blame] | 1856 | if (setup_list(&symbol_conf.dso_list, |
| 1857 | symbol_conf.dso_list_str, "dso") < 0) |
| 1858 | return -1; |
| 1859 | |
| 1860 | if (setup_list(&symbol_conf.comm_list, |
| 1861 | symbol_conf.comm_list_str, "comm") < 0) |
| 1862 | goto out_free_dso_list; |
| 1863 | |
| 1864 | if (setup_list(&symbol_conf.sym_list, |
| 1865 | symbol_conf.sym_list_str, "symbol") < 0) |
| 1866 | goto out_free_comm_list; |
| 1867 | |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 1868 | return 0; |
Arnaldo Carvalho de Melo | 655000e | 2009-12-15 20:04:40 -0200 | [diff] [blame] | 1869 | |
| 1870 | out_free_dso_list: |
| 1871 | strlist__delete(symbol_conf.dso_list); |
| 1872 | out_free_comm_list: |
| 1873 | strlist__delete(symbol_conf.comm_list); |
| 1874 | return -1; |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 1875 | } |
Arnaldo Carvalho de Melo | cc612d8 | 2009-11-23 16:39:10 -0200 | [diff] [blame] | 1876 | |
Arnaldo Carvalho de Melo | 75be6cf | 2009-12-15 20:04:39 -0200 | [diff] [blame] | 1877 | int perf_session__create_kernel_maps(struct perf_session *self) |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 1878 | { |
Arnaldo Carvalho de Melo | de17648 | 2010-01-04 16:19:29 -0200 | [diff] [blame] | 1879 | 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] | 1880 | symbol_conf.vmlinux_name) < 0) |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 1881 | return -1; |
| 1882 | |
Arnaldo Carvalho de Melo | 75be6cf | 2009-12-15 20:04:39 -0200 | [diff] [blame] | 1883 | if (symbol_conf.use_modules && |
| 1884 | perf_session__create_module_maps(self) < 0) |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 1885 | pr_debug("Failed to load list of modules for session %s, " |
| 1886 | "continuing...\n", self->filename); |
Arnaldo Carvalho de Melo | 90c8321 | 2009-11-21 14:31:24 -0200 | [diff] [blame] | 1887 | /* |
| 1888 | * Now that we have all the maps created, just set the ->end of them: |
| 1889 | */ |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 1890 | map_groups__fixup_end(&self->kmaps); |
Arnaldo Carvalho de Melo | 6671cb1 | 2009-11-20 20:51:24 -0200 | [diff] [blame] | 1891 | return 0; |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 1892 | } |