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