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