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