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