Arnaldo Carvalho de Melo | 5aab621 | 2010-03-25 19:59:00 -0300 | [diff] [blame] | 1 | #define _GNU_SOURCE |
| 2 | #include <ctype.h> |
| 3 | #include <dirent.h> |
| 4 | #include <errno.h> |
| 5 | #include <libgen.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <stdio.h> |
| 8 | #include <string.h> |
| 9 | #include <sys/types.h> |
| 10 | #include <sys/stat.h> |
| 11 | #include <sys/param.h> |
| 12 | #include <fcntl.h> |
| 13 | #include <unistd.h> |
Arnaldo Carvalho de Melo | b36f19d | 2010-05-20 12:15:33 -0300 | [diff] [blame] | 14 | #include "build-id.h" |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 15 | #include "debug.h" |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 16 | #include "symbol.h" |
Arnaldo Carvalho de Melo | 5aab621 | 2010-03-25 19:59:00 -0300 | [diff] [blame] | 17 | #include "strlist.h" |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 18 | |
| 19 | #include <libelf.h> |
| 20 | #include <gelf.h> |
| 21 | #include <elf.h> |
Arnaldo Carvalho de Melo | f1617b4 | 2009-11-18 20:20:52 -0200 | [diff] [blame] | 22 | #include <limits.h> |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 23 | #include <sys/utsname.h> |
Peter Zijlstra | 2cdbc46 | 2009-08-05 14:05:16 +0200 | [diff] [blame] | 24 | |
Arnaldo Carvalho de Melo | c12e15e | 2009-11-21 14:31:25 -0200 | [diff] [blame] | 25 | #ifndef NT_GNU_BUILD_ID |
| 26 | #define NT_GNU_BUILD_ID 3 |
| 27 | #endif |
| 28 | |
Dave Martin | 21916c3 | 2010-07-30 09:08:08 -0300 | [diff] [blame] | 29 | static bool dso__build_id_equal(const struct dso *self, u8 *build_id); |
| 30 | static int elf_read_build_id(Elf *elf, void *bf, size_t size); |
Arnaldo Carvalho de Melo | b0da954 | 2009-11-27 16:29:14 -0200 | [diff] [blame] | 31 | 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] | 32 | 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] | 33 | 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] | 34 | symbol_filter_t filter); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 35 | static int dso__load_guest_kernel_sym(struct dso *self, struct map *map, |
| 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, |
David Ahern | ec5761e | 2010-12-09 13:27:07 -0700 | [diff] [blame^] | 44 | .symfs = "", |
Arnaldo Carvalho de Melo | b32d133 | 2009-11-24 12:05:15 -0200 | [diff] [blame] | 45 | }; |
| 46 | |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 47 | int dso__name_len(const struct dso *self) |
| 48 | { |
| 49 | if (verbose) |
| 50 | return self->long_name_len; |
| 51 | |
| 52 | return self->short_name_len; |
| 53 | } |
| 54 | |
Arnaldo Carvalho de Melo | 3610583 | 2009-11-27 16:29:16 -0200 | [diff] [blame] | 55 | bool dso__loaded(const struct dso *self, enum map_type type) |
| 56 | { |
| 57 | return self->loaded & (1 << type); |
| 58 | } |
| 59 | |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 60 | bool dso__sorted_by_name(const struct dso *self, enum map_type type) |
| 61 | { |
| 62 | return self->sorted_by_name & (1 << type); |
| 63 | } |
| 64 | |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 65 | static void dso__set_sorted_by_name(struct dso *self, enum map_type type) |
| 66 | { |
| 67 | self->sorted_by_name |= (1 << type); |
| 68 | } |
| 69 | |
Arnaldo Carvalho de Melo | 36a3e64 | 2010-01-04 16:19:27 -0200 | [diff] [blame] | 70 | 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] | 71 | { |
| 72 | switch (map_type) { |
| 73 | case MAP__FUNCTION: |
| 74 | return symbol_type == 'T' || symbol_type == 'W'; |
Arnaldo Carvalho de Melo | f1dfa0b | 2009-12-11 14:50:39 -0200 | [diff] [blame] | 75 | case MAP__VARIABLE: |
| 76 | return symbol_type == 'D' || symbol_type == 'd'; |
Arnaldo Carvalho de Melo | 6893d4e | 2009-12-11 14:50:37 -0200 | [diff] [blame] | 77 | default: |
| 78 | return false; |
| 79 | } |
| 80 | } |
| 81 | |
Arnaldo Carvalho de Melo | fcf1203 | 2009-11-24 13:01:52 -0200 | [diff] [blame] | 82 | static void symbols__fixup_end(struct rb_root *self) |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 83 | { |
Arnaldo Carvalho de Melo | fcf1203 | 2009-11-24 13:01:52 -0200 | [diff] [blame] | 84 | struct rb_node *nd, *prevnd = rb_first(self); |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 85 | struct symbol *curr, *prev; |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 86 | |
| 87 | if (prevnd == NULL) |
| 88 | return; |
| 89 | |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 90 | curr = rb_entry(prevnd, struct symbol, rb_node); |
| 91 | |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 92 | for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) { |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 93 | prev = curr; |
| 94 | curr = rb_entry(nd, struct symbol, rb_node); |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 95 | |
| 96 | if (prev->end == prev->start) |
| 97 | prev->end = curr->start - 1; |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 98 | } |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 99 | |
| 100 | /* Last entry */ |
| 101 | if (curr->end == curr->start) |
| 102 | curr->end = roundup(curr->start, 4096); |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 103 | } |
| 104 | |
Arnaldo Carvalho de Melo | 9958e1f | 2009-12-11 14:50:36 -0200 | [diff] [blame] | 105 | 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] | 106 | { |
| 107 | struct map *prev, *curr; |
Arnaldo Carvalho de Melo | 95011c6 | 2009-11-27 16:29:20 -0200 | [diff] [blame] | 108 | struct rb_node *nd, *prevnd = rb_first(&self->maps[type]); |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 109 | |
| 110 | if (prevnd == NULL) |
| 111 | return; |
| 112 | |
| 113 | curr = rb_entry(prevnd, struct map, rb_node); |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 114 | |
| 115 | for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) { |
| 116 | prev = curr; |
| 117 | curr = rb_entry(nd, struct map, rb_node); |
| 118 | prev->end = curr->start - 1; |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 119 | } |
Arnaldo Carvalho de Melo | 90c8321 | 2009-11-21 14:31:24 -0200 | [diff] [blame] | 120 | |
| 121 | /* |
| 122 | * We still haven't the actual symbols, so guess the |
| 123 | * last map final address. |
| 124 | */ |
Ian Munsie | 9d1faba | 2010-11-25 15:12:53 +1100 | [diff] [blame] | 125 | curr->end = ~0ULL; |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 126 | } |
| 127 | |
Arnaldo Carvalho de Melo | 9958e1f | 2009-12-11 14:50:36 -0200 | [diff] [blame] | 128 | static void map_groups__fixup_end(struct map_groups *self) |
Arnaldo Carvalho de Melo | 23ea4a3 | 2009-11-27 16:29:19 -0200 | [diff] [blame] | 129 | { |
| 130 | int i; |
| 131 | for (i = 0; i < MAP__NR_TYPES; ++i) |
Arnaldo Carvalho de Melo | 9958e1f | 2009-12-11 14:50:36 -0200 | [diff] [blame] | 132 | __map_groups__fixup_end(self, i); |
Arnaldo Carvalho de Melo | 23ea4a3 | 2009-11-27 16:29:19 -0200 | [diff] [blame] | 133 | } |
| 134 | |
Arnaldo Carvalho de Melo | c408fed | 2010-08-05 12:59:47 -0300 | [diff] [blame] | 135 | static struct symbol *symbol__new(u64 start, u64 len, u8 binding, |
| 136 | const char *name) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 137 | { |
Arnaldo Carvalho de Melo | 0085c954 | 2009-05-28 14:55:13 -0300 | [diff] [blame] | 138 | size_t namelen = strlen(name) + 1; |
Arnaldo Carvalho de Melo | 5aab621 | 2010-03-25 19:59:00 -0300 | [diff] [blame] | 139 | struct symbol *self = calloc(1, (symbol_conf.priv_size + |
| 140 | sizeof(*self) + namelen)); |
Arnaldo Carvalho de Melo | 3647948 | 2009-11-24 12:05:16 -0200 | [diff] [blame] | 141 | if (self == NULL) |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 142 | return NULL; |
| 143 | |
Arnaldo Carvalho de Melo | 75be6cf | 2009-12-15 20:04:39 -0200 | [diff] [blame] | 144 | if (symbol_conf.priv_size) |
| 145 | self = ((void *)self) + symbol_conf.priv_size; |
Arnaldo Carvalho de Melo | 3647948 | 2009-11-24 12:05:16 -0200 | [diff] [blame] | 146 | |
Arnaldo Carvalho de Melo | fefb0b9 | 2010-05-10 13:57:51 -0300 | [diff] [blame] | 147 | self->start = start; |
| 148 | self->end = len ? start + len - 1 : start; |
Arnaldo Carvalho de Melo | c408fed | 2010-08-05 12:59:47 -0300 | [diff] [blame] | 149 | self->binding = binding; |
Arnaldo Carvalho de Melo | fefb0b9 | 2010-05-10 13:57:51 -0300 | [diff] [blame] | 150 | self->namelen = namelen - 1; |
Arnaldo Carvalho de Melo | e420499 | 2009-10-20 14:25:40 -0200 | [diff] [blame] | 151 | |
Arnaldo Carvalho de Melo | 29a9f66 | 2010-02-03 16:52:06 -0200 | [diff] [blame] | 152 | pr_debug4("%s: %s %#Lx-%#Lx\n", __func__, name, start, self->end); |
Arnaldo Carvalho de Melo | e420499 | 2009-10-20 14:25:40 -0200 | [diff] [blame] | 153 | |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 154 | memcpy(self->name, name, namelen); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 155 | |
| 156 | return self; |
| 157 | } |
| 158 | |
Arnaldo Carvalho de Melo | 628ada0 | 2010-02-25 12:57:40 -0300 | [diff] [blame] | 159 | void symbol__delete(struct symbol *self) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 160 | { |
Arnaldo Carvalho de Melo | 75be6cf | 2009-12-15 20:04:39 -0200 | [diff] [blame] | 161 | free(((void *)self) - symbol_conf.priv_size); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | static size_t symbol__fprintf(struct symbol *self, FILE *fp) |
| 165 | { |
Arnaldo Carvalho de Melo | c408fed | 2010-08-05 12:59:47 -0300 | [diff] [blame] | 166 | return fprintf(fp, " %llx-%llx %c %s\n", |
| 167 | self->start, self->end, |
| 168 | self->binding == STB_GLOBAL ? 'g' : |
| 169 | self->binding == STB_LOCAL ? 'l' : 'w', |
| 170 | self->name); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 171 | } |
| 172 | |
Arnaldo Carvalho de Melo | b7cece76 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 173 | void dso__set_long_name(struct dso *self, char *name) |
Arnaldo Carvalho de Melo | cfc10d3 | 2009-11-17 15:40:53 -0200 | [diff] [blame] | 174 | { |
Arnaldo Carvalho de Melo | ef6ae72 | 2009-11-20 20:51:29 -0200 | [diff] [blame] | 175 | if (name == NULL) |
| 176 | return; |
Arnaldo Carvalho de Melo | cfc10d3 | 2009-11-17 15:40:53 -0200 | [diff] [blame] | 177 | self->long_name = name; |
| 178 | self->long_name_len = strlen(name); |
| 179 | } |
| 180 | |
Arnaldo Carvalho de Melo | b63be8d | 2010-03-15 15:03:50 -0300 | [diff] [blame] | 181 | static void dso__set_short_name(struct dso *self, const char *name) |
| 182 | { |
| 183 | if (name == NULL) |
| 184 | return; |
| 185 | self->short_name = name; |
| 186 | self->short_name_len = strlen(name); |
| 187 | } |
| 188 | |
Arnaldo Carvalho de Melo | cfc10d3 | 2009-11-17 15:40:53 -0200 | [diff] [blame] | 189 | static void dso__set_basename(struct dso *self) |
| 190 | { |
Arnaldo Carvalho de Melo | b63be8d | 2010-03-15 15:03:50 -0300 | [diff] [blame] | 191 | dso__set_short_name(self, basename(self->long_name)); |
Arnaldo Carvalho de Melo | cfc10d3 | 2009-11-17 15:40:53 -0200 | [diff] [blame] | 192 | } |
| 193 | |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 194 | struct dso *dso__new(const char *name) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 195 | { |
Arnaldo Carvalho de Melo | 5aab621 | 2010-03-25 19:59:00 -0300 | [diff] [blame] | 196 | struct dso *self = calloc(1, sizeof(*self) + strlen(name) + 1); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 197 | |
| 198 | if (self != NULL) { |
Arnaldo Carvalho de Melo | 6a4694a | 2009-11-27 16:29:17 -0200 | [diff] [blame] | 199 | int i; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 200 | strcpy(self->name, name); |
Arnaldo Carvalho de Melo | cfc10d3 | 2009-11-17 15:40:53 -0200 | [diff] [blame] | 201 | dso__set_long_name(self, self->name); |
Arnaldo Carvalho de Melo | b63be8d | 2010-03-15 15:03:50 -0300 | [diff] [blame] | 202 | dso__set_short_name(self, self->name); |
Arnaldo Carvalho de Melo | 6a4694a | 2009-11-27 16:29:17 -0200 | [diff] [blame] | 203 | for (i = 0; i < MAP__NR_TYPES; ++i) |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 204 | self->symbols[i] = self->symbol_names[i] = RB_ROOT; |
Arnaldo Carvalho de Melo | 52d422d | 2009-07-10 22:47:28 -0300 | [diff] [blame] | 205 | self->slen_calculated = 0; |
Arnaldo Carvalho de Melo | 94cb9e3 | 2009-08-06 14:43:17 -0300 | [diff] [blame] | 206 | self->origin = DSO__ORIG_NOT_FOUND; |
Arnaldo Carvalho de Melo | 8d06367 | 2009-11-04 18:50:43 -0200 | [diff] [blame] | 207 | self->loaded = 0; |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 208 | self->sorted_by_name = 0; |
Arnaldo Carvalho de Melo | 8d06367 | 2009-11-04 18:50:43 -0200 | [diff] [blame] | 209 | self->has_build_id = 0; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 210 | self->kernel = DSO_TYPE_USER; |
Masami Hiramatsu | 0ab061c | 2010-04-21 15:56:16 -0400 | [diff] [blame] | 211 | INIT_LIST_HEAD(&self->node); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | return self; |
| 215 | } |
| 216 | |
Arnaldo Carvalho de Melo | fcf1203 | 2009-11-24 13:01:52 -0200 | [diff] [blame] | 217 | static void symbols__delete(struct rb_root *self) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 218 | { |
| 219 | struct symbol *pos; |
Arnaldo Carvalho de Melo | fcf1203 | 2009-11-24 13:01:52 -0200 | [diff] [blame] | 220 | struct rb_node *next = rb_first(self); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 221 | |
| 222 | while (next) { |
| 223 | pos = rb_entry(next, struct symbol, rb_node); |
| 224 | next = rb_next(&pos->rb_node); |
Arnaldo Carvalho de Melo | fcf1203 | 2009-11-24 13:01:52 -0200 | [diff] [blame] | 225 | rb_erase(&pos->rb_node, self); |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 226 | symbol__delete(pos); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | |
| 230 | void dso__delete(struct dso *self) |
| 231 | { |
Arnaldo Carvalho de Melo | 6a4694a | 2009-11-27 16:29:17 -0200 | [diff] [blame] | 232 | int i; |
| 233 | for (i = 0; i < MAP__NR_TYPES; ++i) |
| 234 | symbols__delete(&self->symbols[i]); |
Arnaldo Carvalho de Melo | 6e40625 | 2010-07-29 15:11:30 -0300 | [diff] [blame] | 235 | if (self->sname_alloc) |
| 236 | free((char *)self->short_name); |
| 237 | if (self->lname_alloc) |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 238 | free(self->long_name); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 239 | free(self); |
| 240 | } |
| 241 | |
Arnaldo Carvalho de Melo | 8d06367 | 2009-11-04 18:50:43 -0200 | [diff] [blame] | 242 | void dso__set_build_id(struct dso *self, void *build_id) |
| 243 | { |
| 244 | memcpy(self->build_id, build_id, sizeof(self->build_id)); |
| 245 | self->has_build_id = 1; |
| 246 | } |
| 247 | |
Arnaldo Carvalho de Melo | fcf1203 | 2009-11-24 13:01:52 -0200 | [diff] [blame] | 248 | 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] | 249 | { |
Arnaldo Carvalho de Melo | fcf1203 | 2009-11-24 13:01:52 -0200 | [diff] [blame] | 250 | struct rb_node **p = &self->rb_node; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 251 | struct rb_node *parent = NULL; |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 252 | const u64 ip = sym->start; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 253 | struct symbol *s; |
| 254 | |
| 255 | while (*p != NULL) { |
| 256 | parent = *p; |
| 257 | s = rb_entry(parent, struct symbol, rb_node); |
| 258 | if (ip < s->start) |
| 259 | p = &(*p)->rb_left; |
| 260 | else |
| 261 | p = &(*p)->rb_right; |
| 262 | } |
| 263 | rb_link_node(&sym->rb_node, parent, p); |
Arnaldo Carvalho de Melo | fcf1203 | 2009-11-24 13:01:52 -0200 | [diff] [blame] | 264 | rb_insert_color(&sym->rb_node, self); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 265 | } |
| 266 | |
Arnaldo Carvalho de Melo | fcf1203 | 2009-11-24 13:01:52 -0200 | [diff] [blame] | 267 | 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] | 268 | { |
| 269 | struct rb_node *n; |
| 270 | |
| 271 | if (self == NULL) |
| 272 | return NULL; |
| 273 | |
Arnaldo Carvalho de Melo | fcf1203 | 2009-11-24 13:01:52 -0200 | [diff] [blame] | 274 | n = self->rb_node; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 275 | |
| 276 | while (n) { |
| 277 | struct symbol *s = rb_entry(n, struct symbol, rb_node); |
| 278 | |
| 279 | if (ip < s->start) |
| 280 | n = n->rb_left; |
| 281 | else if (ip > s->end) |
| 282 | n = n->rb_right; |
| 283 | else |
| 284 | return s; |
| 285 | } |
| 286 | |
| 287 | return NULL; |
| 288 | } |
| 289 | |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 290 | struct symbol_name_rb_node { |
| 291 | struct rb_node rb_node; |
| 292 | struct symbol sym; |
| 293 | }; |
| 294 | |
| 295 | static void symbols__insert_by_name(struct rb_root *self, struct symbol *sym) |
| 296 | { |
| 297 | struct rb_node **p = &self->rb_node; |
| 298 | struct rb_node *parent = NULL; |
Rabin Vincent | 02a9d03 | 2010-11-23 22:08:18 +0530 | [diff] [blame] | 299 | struct symbol_name_rb_node *symn, *s; |
| 300 | |
| 301 | symn = container_of(sym, struct symbol_name_rb_node, sym); |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 302 | |
| 303 | while (*p != NULL) { |
| 304 | parent = *p; |
| 305 | s = rb_entry(parent, struct symbol_name_rb_node, rb_node); |
| 306 | if (strcmp(sym->name, s->sym.name) < 0) |
| 307 | p = &(*p)->rb_left; |
| 308 | else |
| 309 | p = &(*p)->rb_right; |
| 310 | } |
| 311 | rb_link_node(&symn->rb_node, parent, p); |
| 312 | rb_insert_color(&symn->rb_node, self); |
| 313 | } |
| 314 | |
| 315 | static void symbols__sort_by_name(struct rb_root *self, struct rb_root *source) |
| 316 | { |
| 317 | struct rb_node *nd; |
| 318 | |
| 319 | for (nd = rb_first(source); nd; nd = rb_next(nd)) { |
| 320 | struct symbol *pos = rb_entry(nd, struct symbol, rb_node); |
| 321 | symbols__insert_by_name(self, pos); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | static struct symbol *symbols__find_by_name(struct rb_root *self, const char *name) |
| 326 | { |
| 327 | struct rb_node *n; |
| 328 | |
| 329 | if (self == NULL) |
| 330 | return NULL; |
| 331 | |
| 332 | n = self->rb_node; |
| 333 | |
| 334 | while (n) { |
| 335 | struct symbol_name_rb_node *s; |
| 336 | int cmp; |
| 337 | |
| 338 | s = rb_entry(n, struct symbol_name_rb_node, rb_node); |
| 339 | cmp = strcmp(name, s->sym.name); |
| 340 | |
| 341 | if (cmp < 0) |
| 342 | n = n->rb_left; |
| 343 | else if (cmp > 0) |
| 344 | n = n->rb_right; |
| 345 | else |
| 346 | return &s->sym; |
| 347 | } |
| 348 | |
| 349 | return NULL; |
| 350 | } |
| 351 | |
| 352 | struct symbol *dso__find_symbol(struct dso *self, |
| 353 | enum map_type type, u64 addr) |
Arnaldo Carvalho de Melo | fcf1203 | 2009-11-24 13:01:52 -0200 | [diff] [blame] | 354 | { |
Arnaldo Carvalho de Melo | 6a4694a | 2009-11-27 16:29:17 -0200 | [diff] [blame] | 355 | return symbols__find(&self->symbols[type], addr); |
Arnaldo Carvalho de Melo | fcf1203 | 2009-11-24 13:01:52 -0200 | [diff] [blame] | 356 | } |
| 357 | |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 358 | struct symbol *dso__find_symbol_by_name(struct dso *self, enum map_type type, |
| 359 | const char *name) |
| 360 | { |
| 361 | return symbols__find_by_name(&self->symbol_names[type], name); |
| 362 | } |
| 363 | |
| 364 | void dso__sort_by_name(struct dso *self, enum map_type type) |
| 365 | { |
| 366 | dso__set_sorted_by_name(self, type); |
| 367 | return symbols__sort_by_name(&self->symbol_names[type], |
| 368 | &self->symbols[type]); |
| 369 | } |
| 370 | |
Arnaldo Carvalho de Melo | ef12a14 | 2010-01-20 15:28:45 -0200 | [diff] [blame] | 371 | 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] | 372 | { |
| 373 | char *bid = bf; |
Arnaldo Carvalho de Melo | ef12a14 | 2010-01-20 15:28:45 -0200 | [diff] [blame] | 374 | const u8 *raw = self; |
Arnaldo Carvalho de Melo | 8d06367 | 2009-11-04 18:50:43 -0200 | [diff] [blame] | 375 | int i; |
| 376 | |
| 377 | for (i = 0; i < len; ++i) { |
| 378 | sprintf(bid, "%02x", *raw); |
| 379 | ++raw; |
| 380 | bid += 2; |
| 381 | } |
| 382 | |
| 383 | return raw - self; |
| 384 | } |
| 385 | |
Arnaldo Carvalho de Melo | 9e03eb2 | 2009-11-16 16:32:44 -0200 | [diff] [blame] | 386 | size_t dso__fprintf_buildid(struct dso *self, FILE *fp) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 387 | { |
Arnaldo Carvalho de Melo | 8d06367 | 2009-11-04 18:50:43 -0200 | [diff] [blame] | 388 | char sbuild_id[BUILD_ID_SIZE * 2 + 1]; |
Arnaldo Carvalho de Melo | 8d06367 | 2009-11-04 18:50:43 -0200 | [diff] [blame] | 389 | |
| 390 | 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] | 391 | return fprintf(fp, "%s", sbuild_id); |
| 392 | } |
| 393 | |
Srikar Dronamraju | 90f18e6 | 2010-08-25 19:13:29 +0530 | [diff] [blame] | 394 | size_t dso__fprintf_symbols_by_name(struct dso *self, enum map_type type, FILE *fp) |
| 395 | { |
| 396 | size_t ret = 0; |
| 397 | struct rb_node *nd; |
| 398 | struct symbol_name_rb_node *pos; |
| 399 | |
| 400 | for (nd = rb_first(&self->symbol_names[type]); nd; nd = rb_next(nd)) { |
| 401 | pos = rb_entry(nd, struct symbol_name_rb_node, rb_node); |
| 402 | fprintf(fp, "%s\n", pos->sym.name); |
| 403 | } |
| 404 | |
| 405 | return ret; |
| 406 | } |
| 407 | |
Arnaldo Carvalho de Melo | 95011c6 | 2009-11-27 16:29:20 -0200 | [diff] [blame] | 408 | 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] | 409 | { |
| 410 | struct rb_node *nd; |
| 411 | size_t ret = fprintf(fp, "dso: %s (", self->short_name); |
| 412 | |
Arnaldo Carvalho de Melo | 3846df2 | 2010-02-22 16:15:39 -0300 | [diff] [blame] | 413 | if (self->short_name != self->long_name) |
| 414 | ret += fprintf(fp, "%s, ", self->long_name); |
| 415 | ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type], |
| 416 | self->loaded ? "" : "NOT "); |
Arnaldo Carvalho de Melo | 9e03eb2 | 2009-11-16 16:32:44 -0200 | [diff] [blame] | 417 | ret += dso__fprintf_buildid(self, fp); |
Arnaldo Carvalho de Melo | 6a4694a | 2009-11-27 16:29:17 -0200 | [diff] [blame] | 418 | ret += fprintf(fp, ")\n"); |
Arnaldo Carvalho de Melo | 95011c6 | 2009-11-27 16:29:20 -0200 | [diff] [blame] | 419 | for (nd = rb_first(&self->symbols[type]); nd; nd = rb_next(nd)) { |
| 420 | struct symbol *pos = rb_entry(nd, struct symbol, rb_node); |
| 421 | ret += symbol__fprintf(pos, fp); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | return ret; |
| 425 | } |
| 426 | |
Arnaldo Carvalho de Melo | 9e20144 | 2010-01-14 18:30:06 -0200 | [diff] [blame] | 427 | int kallsyms__parse(const char *filename, void *arg, |
| 428 | int (*process_symbol)(void *arg, const char *name, |
Arnaldo Carvalho de Melo | 682b335 | 2010-01-04 16:19:26 -0200 | [diff] [blame] | 429 | char type, u64 start)) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 430 | { |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 431 | char *line = NULL; |
| 432 | size_t n; |
Arnaldo Carvalho de Melo | 682b335 | 2010-01-04 16:19:26 -0200 | [diff] [blame] | 433 | int err = 0; |
Arnaldo Carvalho de Melo | 9e20144 | 2010-01-14 18:30:06 -0200 | [diff] [blame] | 434 | FILE *file = fopen(filename, "r"); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 435 | |
| 436 | if (file == NULL) |
| 437 | goto out_failure; |
| 438 | |
| 439 | while (!feof(file)) { |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 440 | u64 start; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 441 | int line_len, len; |
| 442 | char symbol_type; |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 443 | char *symbol_name; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 444 | |
| 445 | line_len = getline(&line, &n, file); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 446 | if (line_len < 0 || !line) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 447 | break; |
| 448 | |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 449 | line[--line_len] = '\0'; /* \n */ |
| 450 | |
Arnaldo Carvalho de Melo | a0055ae | 2009-06-01 17:50:19 -0300 | [diff] [blame] | 451 | len = hex2u64(line, &start); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 452 | |
| 453 | len++; |
| 454 | if (len + 2 >= line_len) |
| 455 | continue; |
| 456 | |
| 457 | symbol_type = toupper(line[len]); |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 458 | symbol_name = line + len + 2; |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 459 | |
Arnaldo Carvalho de Melo | 682b335 | 2010-01-04 16:19:26 -0200 | [diff] [blame] | 460 | err = process_symbol(arg, symbol_name, symbol_type, start); |
| 461 | if (err) |
| 462 | break; |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | free(line); |
| 466 | fclose(file); |
Arnaldo Carvalho de Melo | 682b335 | 2010-01-04 16:19:26 -0200 | [diff] [blame] | 467 | return err; |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 468 | |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 469 | out_failure: |
| 470 | return -1; |
| 471 | } |
| 472 | |
Arnaldo Carvalho de Melo | 682b335 | 2010-01-04 16:19:26 -0200 | [diff] [blame] | 473 | struct process_kallsyms_args { |
| 474 | struct map *map; |
| 475 | struct dso *dso; |
| 476 | }; |
| 477 | |
Arnaldo Carvalho de Melo | c408fed | 2010-08-05 12:59:47 -0300 | [diff] [blame] | 478 | static u8 kallsyms2elf_type(char type) |
| 479 | { |
| 480 | if (type == 'W') |
| 481 | return STB_WEAK; |
| 482 | |
| 483 | return isupper(type) ? STB_GLOBAL : STB_LOCAL; |
| 484 | } |
| 485 | |
Arnaldo Carvalho de Melo | 682b335 | 2010-01-04 16:19:26 -0200 | [diff] [blame] | 486 | static int map__process_kallsym_symbol(void *arg, const char *name, |
| 487 | char type, u64 start) |
| 488 | { |
| 489 | struct symbol *sym; |
| 490 | struct process_kallsyms_args *a = arg; |
| 491 | struct rb_root *root = &a->dso->symbols[a->map->type]; |
| 492 | |
| 493 | if (!symbol_type__is_a(type, a->map->type)) |
| 494 | return 0; |
| 495 | |
| 496 | /* |
| 497 | * Will fix up the end later, when we have all symbols sorted. |
| 498 | */ |
Arnaldo Carvalho de Melo | c408fed | 2010-08-05 12:59:47 -0300 | [diff] [blame] | 499 | sym = symbol__new(start, 0, kallsyms2elf_type(type), name); |
Arnaldo Carvalho de Melo | 682b335 | 2010-01-04 16:19:26 -0200 | [diff] [blame] | 500 | |
| 501 | if (sym == NULL) |
| 502 | return -ENOMEM; |
| 503 | /* |
| 504 | * We will pass the symbols to the filter later, in |
| 505 | * map__split_kallsyms, when we have split the maps per module |
| 506 | */ |
| 507 | symbols__insert(root, sym); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 508 | |
Arnaldo Carvalho de Melo | 682b335 | 2010-01-04 16:19:26 -0200 | [diff] [blame] | 509 | return 0; |
| 510 | } |
| 511 | |
| 512 | /* |
| 513 | * Loads the function entries in /proc/kallsyms into kernel_map->dso, |
| 514 | * so that we can in the next step set the symbol ->end address and then |
| 515 | * call kernel_maps__split_kallsyms. |
| 516 | */ |
Arnaldo Carvalho de Melo | 9e20144 | 2010-01-14 18:30:06 -0200 | [diff] [blame] | 517 | static int dso__load_all_kallsyms(struct dso *self, const char *filename, |
| 518 | struct map *map) |
Arnaldo Carvalho de Melo | 682b335 | 2010-01-04 16:19:26 -0200 | [diff] [blame] | 519 | { |
| 520 | struct process_kallsyms_args args = { .map = map, .dso = self, }; |
Arnaldo Carvalho de Melo | 9e20144 | 2010-01-14 18:30:06 -0200 | [diff] [blame] | 521 | return kallsyms__parse(filename, &args, map__process_kallsym_symbol); |
Arnaldo Carvalho de Melo | 682b335 | 2010-01-04 16:19:26 -0200 | [diff] [blame] | 522 | } |
| 523 | |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 524 | /* |
| 525 | * Split the symbols into maps, making sure there are no overlaps, i.e. the |
| 526 | * kernel range is broken in several maps, named [kernel].N, as we don't have |
| 527 | * the original ELF section names vmlinux have. |
| 528 | */ |
Arnaldo Carvalho de Melo | 9958e1f | 2009-12-11 14:50:36 -0200 | [diff] [blame] | 529 | 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] | 530 | symbol_filter_t filter) |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 531 | { |
Arnaldo Carvalho de Melo | 9de89fe | 2010-02-03 16:52:00 -0200 | [diff] [blame] | 532 | struct map_groups *kmaps = map__kmap(map)->kmaps; |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 533 | struct machine *machine = kmaps->machine; |
Arnaldo Carvalho de Melo | 4e06255 | 2009-11-27 16:29:18 -0200 | [diff] [blame] | 534 | struct map *curr_map = map; |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 535 | struct symbol *pos; |
Arnaldo Carvalho de Melo | 8a95331 | 2010-11-29 12:44:15 -0200 | [diff] [blame] | 536 | int count = 0, moved = 0; |
Arnaldo Carvalho de Melo | 4e06255 | 2009-11-27 16:29:18 -0200 | [diff] [blame] | 537 | struct rb_root *root = &self->symbols[map->type]; |
| 538 | struct rb_node *next = rb_first(root); |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 539 | int kernel_range = 0; |
| 540 | |
| 541 | while (next) { |
| 542 | char *module; |
| 543 | |
| 544 | pos = rb_entry(next, struct symbol, rb_node); |
| 545 | next = rb_next(&pos->rb_node); |
| 546 | |
| 547 | module = strchr(pos->name, '\t'); |
| 548 | if (module) { |
Arnaldo Carvalho de Melo | 75be6cf | 2009-12-15 20:04:39 -0200 | [diff] [blame] | 549 | if (!symbol_conf.use_modules) |
Arnaldo Carvalho de Melo | 1de8e24 | 2009-11-27 16:29:21 -0200 | [diff] [blame] | 550 | goto discard_symbol; |
| 551 | |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 552 | *module++ = '\0'; |
| 553 | |
Arnaldo Carvalho de Melo | b7cece76 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 554 | if (strcmp(curr_map->dso->short_name, module)) { |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 555 | if (curr_map != map && |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 556 | self->kernel == DSO_TYPE_GUEST_KERNEL && |
| 557 | machine__is_default_guest(machine)) { |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 558 | /* |
| 559 | * We assume all symbols of a module are |
| 560 | * continuous in * kallsyms, so curr_map |
| 561 | * points to a module and all its |
| 562 | * symbols are in its kmap. Mark it as |
| 563 | * loaded. |
| 564 | */ |
| 565 | dso__set_loaded(curr_map->dso, |
| 566 | curr_map->type); |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 567 | } |
Arnaldo Carvalho de Melo | b7cece76 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 568 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 569 | curr_map = map_groups__find_by_name(kmaps, |
| 570 | map->type, module); |
| 571 | if (curr_map == NULL) { |
Arnaldo Carvalho de Melo | 2f51903 | 2010-05-17 17:57:59 -0300 | [diff] [blame] | 572 | pr_debug("%s/proc/{kallsyms,modules} " |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 573 | "inconsistency while looking " |
| 574 | "for \"%s\" module!\n", |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 575 | machine->root_dir, module); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 576 | curr_map = map; |
| 577 | goto discard_symbol; |
| 578 | } |
| 579 | |
| 580 | if (curr_map->dso->loaded && |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 581 | !machine__is_default_guest(machine)) |
Arnaldo Carvalho de Melo | b7cece76 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 582 | goto discard_symbol; |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 583 | } |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 584 | /* |
| 585 | * So that we look just like we get from .ko files, |
| 586 | * i.e. not prelinked, relative to map->start. |
| 587 | */ |
Arnaldo Carvalho de Melo | 4e06255 | 2009-11-27 16:29:18 -0200 | [diff] [blame] | 588 | pos->start = curr_map->map_ip(curr_map, pos->start); |
| 589 | pos->end = curr_map->map_ip(curr_map, pos->end); |
| 590 | } else if (curr_map != map) { |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 591 | char dso_name[PATH_MAX]; |
| 592 | struct dso *dso; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 593 | |
Arnaldo Carvalho de Melo | 8a95331 | 2010-11-29 12:44:15 -0200 | [diff] [blame] | 594 | if (count == 0) { |
| 595 | curr_map = map; |
| 596 | goto filter_symbol; |
| 597 | } |
| 598 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 599 | if (self->kernel == DSO_TYPE_GUEST_KERNEL) |
| 600 | snprintf(dso_name, sizeof(dso_name), |
| 601 | "[guest.kernel].%d", |
| 602 | kernel_range++); |
| 603 | else |
| 604 | snprintf(dso_name, sizeof(dso_name), |
| 605 | "[kernel].%d", |
| 606 | kernel_range++); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 607 | |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 608 | dso = dso__new(dso_name); |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 609 | if (dso == NULL) |
| 610 | return -1; |
| 611 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 612 | dso->kernel = self->kernel; |
| 613 | |
Arnaldo Carvalho de Melo | 4e06255 | 2009-11-27 16:29:18 -0200 | [diff] [blame] | 614 | curr_map = map__new2(pos->start, dso, map->type); |
Zhang, Yanmin | 37fe5fc | 2010-02-25 11:00:51 +0800 | [diff] [blame] | 615 | if (curr_map == NULL) { |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 616 | dso__delete(dso); |
| 617 | return -1; |
| 618 | } |
| 619 | |
Arnaldo Carvalho de Melo | 4e06255 | 2009-11-27 16:29:18 -0200 | [diff] [blame] | 620 | 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] | 621 | map_groups__insert(kmaps, curr_map); |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 622 | ++kernel_range; |
| 623 | } |
Arnaldo Carvalho de Melo | 8a95331 | 2010-11-29 12:44:15 -0200 | [diff] [blame] | 624 | filter_symbol: |
Arnaldo Carvalho de Melo | 4e06255 | 2009-11-27 16:29:18 -0200 | [diff] [blame] | 625 | if (filter && filter(curr_map, pos)) { |
Arnaldo Carvalho de Melo | 1de8e24 | 2009-11-27 16:29:21 -0200 | [diff] [blame] | 626 | discard_symbol: rb_erase(&pos->rb_node, root); |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 627 | symbol__delete(pos); |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 628 | } else { |
Arnaldo Carvalho de Melo | 4e06255 | 2009-11-27 16:29:18 -0200 | [diff] [blame] | 629 | if (curr_map != map) { |
| 630 | rb_erase(&pos->rb_node, root); |
| 631 | symbols__insert(&curr_map->dso->symbols[curr_map->type], pos); |
Arnaldo Carvalho de Melo | 8a95331 | 2010-11-29 12:44:15 -0200 | [diff] [blame] | 632 | ++moved; |
| 633 | } else |
| 634 | ++count; |
Mike Galbraith | 9974f49 | 2009-07-02 08:05:58 +0200 | [diff] [blame] | 635 | } |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 636 | } |
| 637 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 638 | if (curr_map != map && |
| 639 | self->kernel == DSO_TYPE_GUEST_KERNEL && |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 640 | machine__is_default_guest(kmaps->machine)) { |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 641 | dso__set_loaded(curr_map->dso, curr_map->type); |
| 642 | } |
| 643 | |
Arnaldo Carvalho de Melo | 8a95331 | 2010-11-29 12:44:15 -0200 | [diff] [blame] | 644 | return count + moved; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 645 | } |
| 646 | |
Arnaldo Carvalho de Melo | 9de89fe | 2010-02-03 16:52:00 -0200 | [diff] [blame] | 647 | int dso__load_kallsyms(struct dso *self, const char *filename, |
| 648 | struct map *map, symbol_filter_t filter) |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 649 | { |
Arnaldo Carvalho de Melo | 9e20144 | 2010-01-14 18:30:06 -0200 | [diff] [blame] | 650 | if (dso__load_all_kallsyms(self, filename, map) < 0) |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 651 | return -1; |
| 652 | |
Arnaldo Carvalho de Melo | 4e06255 | 2009-11-27 16:29:18 -0200 | [diff] [blame] | 653 | symbols__fixup_end(&self->symbols[map->type]); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 654 | if (self->kernel == DSO_TYPE_GUEST_KERNEL) |
| 655 | self->origin = DSO__ORIG_GUEST_KERNEL; |
| 656 | else |
| 657 | self->origin = DSO__ORIG_KERNEL; |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 658 | |
Arnaldo Carvalho de Melo | 9de89fe | 2010-02-03 16:52:00 -0200 | [diff] [blame] | 659 | return dso__split_kallsyms(self, map, filter); |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 660 | } |
| 661 | |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 662 | 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] | 663 | symbol_filter_t filter) |
Pekka Enberg | 80d496b | 2009-06-08 21:12:48 +0300 | [diff] [blame] | 664 | { |
| 665 | char *line = NULL; |
| 666 | size_t n; |
| 667 | FILE *file; |
| 668 | int nr_syms = 0; |
| 669 | |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 670 | file = fopen(self->long_name, "r"); |
Pekka Enberg | 80d496b | 2009-06-08 21:12:48 +0300 | [diff] [blame] | 671 | if (file == NULL) |
| 672 | goto out_failure; |
| 673 | |
| 674 | while (!feof(file)) { |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 675 | u64 start, size; |
Pekka Enberg | 80d496b | 2009-06-08 21:12:48 +0300 | [diff] [blame] | 676 | struct symbol *sym; |
| 677 | int line_len, len; |
| 678 | |
| 679 | line_len = getline(&line, &n, file); |
| 680 | if (line_len < 0) |
| 681 | break; |
| 682 | |
| 683 | if (!line) |
| 684 | goto out_failure; |
| 685 | |
| 686 | line[--line_len] = '\0'; /* \n */ |
| 687 | |
| 688 | len = hex2u64(line, &start); |
| 689 | |
| 690 | len++; |
| 691 | if (len + 2 >= line_len) |
| 692 | continue; |
| 693 | |
| 694 | len += hex2u64(line + len, &size); |
| 695 | |
| 696 | len++; |
| 697 | if (len + 2 >= line_len) |
| 698 | continue; |
| 699 | |
Arnaldo Carvalho de Melo | c408fed | 2010-08-05 12:59:47 -0300 | [diff] [blame] | 700 | sym = symbol__new(start, size, STB_GLOBAL, line + len); |
Pekka Enberg | 80d496b | 2009-06-08 21:12:48 +0300 | [diff] [blame] | 701 | |
| 702 | if (sym == NULL) |
| 703 | goto out_delete_line; |
| 704 | |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 705 | if (filter && filter(map, sym)) |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 706 | symbol__delete(sym); |
Pekka Enberg | 80d496b | 2009-06-08 21:12:48 +0300 | [diff] [blame] | 707 | else { |
Arnaldo Carvalho de Melo | 6a4694a | 2009-11-27 16:29:17 -0200 | [diff] [blame] | 708 | symbols__insert(&self->symbols[map->type], sym); |
Pekka Enberg | 80d496b | 2009-06-08 21:12:48 +0300 | [diff] [blame] | 709 | nr_syms++; |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | free(line); |
| 714 | fclose(file); |
| 715 | |
| 716 | return nr_syms; |
| 717 | |
| 718 | out_delete_line: |
| 719 | free(line); |
| 720 | out_failure: |
| 721 | return -1; |
| 722 | } |
| 723 | |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 724 | /** |
| 725 | * elf_symtab__for_each_symbol - iterate thru all the symbols |
| 726 | * |
| 727 | * @self: struct elf_symtab instance to iterate |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 728 | * @idx: uint32_t idx |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 729 | * @sym: GElf_Sym iterator |
| 730 | */ |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 731 | #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \ |
| 732 | for (idx = 0, gelf_getsym(syms, idx, &sym);\ |
| 733 | idx < nr_syms; \ |
| 734 | idx++, gelf_getsym(syms, idx, &sym)) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 735 | |
| 736 | static inline uint8_t elf_sym__type(const GElf_Sym *sym) |
| 737 | { |
| 738 | return GELF_ST_TYPE(sym->st_info); |
| 739 | } |
| 740 | |
| 741 | static inline int elf_sym__is_function(const GElf_Sym *sym) |
| 742 | { |
| 743 | return elf_sym__type(sym) == STT_FUNC && |
| 744 | sym->st_name != 0 && |
Arnaldo Carvalho de Melo | 8183313 | 2009-10-05 23:35:03 -0300 | [diff] [blame] | 745 | sym->st_shndx != SHN_UNDEF; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 746 | } |
| 747 | |
Arnaldo Carvalho de Melo | f1dfa0b | 2009-12-11 14:50:39 -0200 | [diff] [blame] | 748 | static inline bool elf_sym__is_object(const GElf_Sym *sym) |
| 749 | { |
| 750 | return elf_sym__type(sym) == STT_OBJECT && |
| 751 | sym->st_name != 0 && |
| 752 | sym->st_shndx != SHN_UNDEF; |
| 753 | } |
| 754 | |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 755 | static inline int elf_sym__is_label(const GElf_Sym *sym) |
| 756 | { |
| 757 | return elf_sym__type(sym) == STT_NOTYPE && |
| 758 | sym->st_name != 0 && |
| 759 | sym->st_shndx != SHN_UNDEF && |
| 760 | sym->st_shndx != SHN_ABS; |
| 761 | } |
| 762 | |
| 763 | static inline const char *elf_sec__name(const GElf_Shdr *shdr, |
| 764 | const Elf_Data *secstrs) |
| 765 | { |
| 766 | return secstrs->d_buf + shdr->sh_name; |
| 767 | } |
| 768 | |
| 769 | static inline int elf_sec__is_text(const GElf_Shdr *shdr, |
| 770 | const Elf_Data *secstrs) |
| 771 | { |
| 772 | return strstr(elf_sec__name(shdr, secstrs), "text") != NULL; |
| 773 | } |
| 774 | |
Arnaldo Carvalho de Melo | f1dfa0b | 2009-12-11 14:50:39 -0200 | [diff] [blame] | 775 | static inline bool elf_sec__is_data(const GElf_Shdr *shdr, |
| 776 | const Elf_Data *secstrs) |
| 777 | { |
| 778 | return strstr(elf_sec__name(shdr, secstrs), "data") != NULL; |
| 779 | } |
| 780 | |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 781 | static inline const char *elf_sym__name(const GElf_Sym *sym, |
| 782 | const Elf_Data *symstrs) |
| 783 | { |
| 784 | return symstrs->d_buf + sym->st_name; |
| 785 | } |
| 786 | |
| 787 | static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep, |
| 788 | GElf_Shdr *shp, const char *name, |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 789 | size_t *idx) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 790 | { |
| 791 | Elf_Scn *sec = NULL; |
| 792 | size_t cnt = 1; |
| 793 | |
| 794 | while ((sec = elf_nextscn(elf, sec)) != NULL) { |
| 795 | char *str; |
| 796 | |
| 797 | gelf_getshdr(sec, shp); |
| 798 | str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name); |
| 799 | if (!strcmp(name, str)) { |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 800 | if (idx) |
| 801 | *idx = cnt; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 802 | break; |
| 803 | } |
| 804 | ++cnt; |
| 805 | } |
| 806 | |
| 807 | return sec; |
| 808 | } |
| 809 | |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 810 | #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \ |
| 811 | for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \ |
| 812 | idx < nr_entries; \ |
| 813 | ++idx, pos = gelf_getrel(reldata, idx, &pos_mem)) |
| 814 | |
| 815 | #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \ |
| 816 | for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \ |
| 817 | idx < nr_entries; \ |
| 818 | ++idx, pos = gelf_getrela(reldata, idx, &pos_mem)) |
| 819 | |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 820 | /* |
| 821 | * We need to check if we have a .dynsym, so that we can handle the |
| 822 | * .plt, synthesizing its symbols, that aren't on the symtabs (be it |
| 823 | * .dynsym or .symtab). |
| 824 | * And always look at the original dso, not at debuginfo packages, that |
| 825 | * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS). |
| 826 | */ |
Arnaldo Carvalho de Melo | 8216416 | 2009-11-16 13:48:11 -0200 | [diff] [blame] | 827 | static int dso__synthesize_plt_symbols(struct dso *self, struct map *map, |
| 828 | symbol_filter_t filter) |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 829 | { |
| 830 | uint32_t nr_rel_entries, idx; |
| 831 | GElf_Sym sym; |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 832 | u64 plt_offset; |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 833 | GElf_Shdr shdr_plt; |
| 834 | struct symbol *f; |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 835 | GElf_Shdr shdr_rel_plt, shdr_dynsym; |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 836 | Elf_Data *reldata, *syms, *symstrs; |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 837 | Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym; |
| 838 | size_t dynsym_idx; |
| 839 | GElf_Ehdr ehdr; |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 840 | char sympltname[1024]; |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 841 | Elf *elf; |
| 842 | int nr = 0, symidx, fd, err = 0; |
David Ahern | ec5761e | 2010-12-09 13:27:07 -0700 | [diff] [blame^] | 843 | char name[PATH_MAX]; |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 844 | |
David Ahern | ec5761e | 2010-12-09 13:27:07 -0700 | [diff] [blame^] | 845 | snprintf(name, sizeof(name), "%s%s", |
| 846 | symbol_conf.symfs, self->long_name); |
| 847 | fd = open(name, O_RDONLY); |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 848 | if (fd < 0) |
| 849 | goto out; |
| 850 | |
Marti Raudsepp | 8408712 | 2009-10-24 19:10:36 +0300 | [diff] [blame] | 851 | elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 852 | if (elf == NULL) |
| 853 | goto out_close; |
| 854 | |
| 855 | if (gelf_getehdr(elf, &ehdr) == NULL) |
| 856 | goto out_elf_end; |
| 857 | |
| 858 | scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym, |
| 859 | ".dynsym", &dynsym_idx); |
| 860 | if (scn_dynsym == NULL) |
| 861 | goto out_elf_end; |
| 862 | |
| 863 | 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] | 864 | ".rela.plt", NULL); |
| 865 | if (scn_plt_rel == NULL) { |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 866 | 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] | 867 | ".rel.plt", NULL); |
| 868 | if (scn_plt_rel == NULL) |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 869 | goto out_elf_end; |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 870 | } |
| 871 | |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 872 | err = -1; |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 873 | |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 874 | if (shdr_rel_plt.sh_link != dynsym_idx) |
| 875 | goto out_elf_end; |
| 876 | |
| 877 | if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL) |
| 878 | goto out_elf_end; |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 879 | |
| 880 | /* |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 881 | * 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] | 882 | * and the symbols in the .dynsym they refer to. |
| 883 | */ |
| 884 | reldata = elf_getdata(scn_plt_rel, NULL); |
| 885 | if (reldata == NULL) |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 886 | goto out_elf_end; |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 887 | |
| 888 | syms = elf_getdata(scn_dynsym, NULL); |
| 889 | if (syms == NULL) |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 890 | goto out_elf_end; |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 891 | |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 892 | scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link); |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 893 | if (scn_symstrs == NULL) |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 894 | goto out_elf_end; |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 895 | |
| 896 | symstrs = elf_getdata(scn_symstrs, NULL); |
| 897 | if (symstrs == NULL) |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 898 | goto out_elf_end; |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 899 | |
| 900 | nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize; |
| 901 | plt_offset = shdr_plt.sh_offset; |
| 902 | |
| 903 | if (shdr_rel_plt.sh_type == SHT_RELA) { |
| 904 | GElf_Rela pos_mem, *pos; |
| 905 | |
| 906 | elf_section__for_each_rela(reldata, pos, pos_mem, idx, |
| 907 | nr_rel_entries) { |
| 908 | symidx = GELF_R_SYM(pos->r_info); |
| 909 | plt_offset += shdr_plt.sh_entsize; |
| 910 | gelf_getsym(syms, symidx, &sym); |
| 911 | snprintf(sympltname, sizeof(sympltname), |
| 912 | "%s@plt", elf_sym__name(&sym, symstrs)); |
| 913 | |
| 914 | f = symbol__new(plt_offset, shdr_plt.sh_entsize, |
Arnaldo Carvalho de Melo | c408fed | 2010-08-05 12:59:47 -0300 | [diff] [blame] | 915 | STB_GLOBAL, sympltname); |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 916 | if (!f) |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 917 | goto out_elf_end; |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 918 | |
Arnaldo Carvalho de Melo | 8216416 | 2009-11-16 13:48:11 -0200 | [diff] [blame] | 919 | if (filter && filter(map, f)) |
| 920 | symbol__delete(f); |
| 921 | else { |
Arnaldo Carvalho de Melo | 6a4694a | 2009-11-27 16:29:17 -0200 | [diff] [blame] | 922 | symbols__insert(&self->symbols[map->type], f); |
Arnaldo Carvalho de Melo | 8216416 | 2009-11-16 13:48:11 -0200 | [diff] [blame] | 923 | ++nr; |
| 924 | } |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 925 | } |
| 926 | } else if (shdr_rel_plt.sh_type == SHT_REL) { |
| 927 | GElf_Rel pos_mem, *pos; |
| 928 | elf_section__for_each_rel(reldata, pos, pos_mem, idx, |
| 929 | nr_rel_entries) { |
| 930 | symidx = GELF_R_SYM(pos->r_info); |
| 931 | plt_offset += shdr_plt.sh_entsize; |
| 932 | gelf_getsym(syms, symidx, &sym); |
| 933 | snprintf(sympltname, sizeof(sympltname), |
| 934 | "%s@plt", elf_sym__name(&sym, symstrs)); |
| 935 | |
| 936 | f = symbol__new(plt_offset, shdr_plt.sh_entsize, |
Arnaldo Carvalho de Melo | c408fed | 2010-08-05 12:59:47 -0300 | [diff] [blame] | 937 | STB_GLOBAL, sympltname); |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 938 | if (!f) |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 939 | goto out_elf_end; |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 940 | |
Arnaldo Carvalho de Melo | 8216416 | 2009-11-16 13:48:11 -0200 | [diff] [blame] | 941 | if (filter && filter(map, f)) |
| 942 | symbol__delete(f); |
| 943 | else { |
Arnaldo Carvalho de Melo | 6a4694a | 2009-11-27 16:29:17 -0200 | [diff] [blame] | 944 | symbols__insert(&self->symbols[map->type], f); |
Arnaldo Carvalho de Melo | 8216416 | 2009-11-16 13:48:11 -0200 | [diff] [blame] | 945 | ++nr; |
| 946 | } |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 947 | } |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 948 | } |
| 949 | |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 950 | err = 0; |
| 951 | out_elf_end: |
| 952 | elf_end(elf); |
| 953 | out_close: |
| 954 | close(fd); |
| 955 | |
| 956 | if (err == 0) |
| 957 | return nr; |
| 958 | out: |
Arnaldo Carvalho de Melo | fe2197b | 2010-03-11 20:12:40 -0300 | [diff] [blame] | 959 | pr_debug("%s: problems reading %s PLT info.\n", |
| 960 | __func__, self->long_name); |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 961 | return 0; |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 962 | } |
| 963 | |
Arnaldo Carvalho de Melo | d45868d | 2009-12-11 14:50:38 -0200 | [diff] [blame] | 964 | static bool elf_sym__is_a(GElf_Sym *self, enum map_type type) |
| 965 | { |
| 966 | switch (type) { |
| 967 | case MAP__FUNCTION: |
| 968 | return elf_sym__is_function(self); |
Arnaldo Carvalho de Melo | f1dfa0b | 2009-12-11 14:50:39 -0200 | [diff] [blame] | 969 | case MAP__VARIABLE: |
| 970 | return elf_sym__is_object(self); |
Arnaldo Carvalho de Melo | d45868d | 2009-12-11 14:50:38 -0200 | [diff] [blame] | 971 | default: |
| 972 | return false; |
| 973 | } |
| 974 | } |
| 975 | |
| 976 | static bool elf_sec__is_a(GElf_Shdr *self, Elf_Data *secstrs, enum map_type type) |
| 977 | { |
| 978 | switch (type) { |
| 979 | case MAP__FUNCTION: |
| 980 | return elf_sec__is_text(self, secstrs); |
Arnaldo Carvalho de Melo | f1dfa0b | 2009-12-11 14:50:39 -0200 | [diff] [blame] | 981 | case MAP__VARIABLE: |
| 982 | return elf_sec__is_data(self, secstrs); |
Arnaldo Carvalho de Melo | d45868d | 2009-12-11 14:50:38 -0200 | [diff] [blame] | 983 | default: |
| 984 | return false; |
| 985 | } |
| 986 | } |
| 987 | |
Eric B Munson | 70c3856 | 2010-06-14 14:56:33 +0100 | [diff] [blame] | 988 | static size_t elf_addr_to_index(Elf *elf, GElf_Addr addr) |
| 989 | { |
| 990 | Elf_Scn *sec = NULL; |
| 991 | GElf_Shdr shdr; |
| 992 | size_t cnt = 1; |
| 993 | |
| 994 | while ((sec = elf_nextscn(elf, sec)) != NULL) { |
| 995 | gelf_getshdr(sec, &shdr); |
| 996 | |
| 997 | if ((addr >= shdr.sh_addr) && |
| 998 | (addr < (shdr.sh_addr + shdr.sh_size))) |
| 999 | return cnt; |
| 1000 | |
| 1001 | ++cnt; |
| 1002 | } |
| 1003 | |
| 1004 | return -1; |
| 1005 | } |
| 1006 | |
Arnaldo Carvalho de Melo | 9de89fe | 2010-02-03 16:52:00 -0200 | [diff] [blame] | 1007 | static int dso__load_sym(struct dso *self, struct map *map, const char *name, |
Dave Martin | 6da80ce | 2010-07-30 09:50:09 -0300 | [diff] [blame] | 1008 | int fd, symbol_filter_t filter, int kmodule, |
| 1009 | int want_symtab) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1010 | { |
Arnaldo Carvalho de Melo | 9de89fe | 2010-02-03 16:52:00 -0200 | [diff] [blame] | 1011 | struct kmap *kmap = self->kernel ? map__kmap(map) : NULL; |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 1012 | struct map *curr_map = map; |
| 1013 | struct dso *curr_dso = self; |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 1014 | Elf_Data *symstrs, *secstrs; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1015 | uint32_t nr_syms; |
| 1016 | int err = -1; |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 1017 | uint32_t idx; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1018 | GElf_Ehdr ehdr; |
Eric B Munson | 70c3856 | 2010-06-14 14:56:33 +0100 | [diff] [blame] | 1019 | GElf_Shdr shdr, opdshdr; |
| 1020 | Elf_Data *syms, *opddata = NULL; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1021 | GElf_Sym sym; |
Eric B Munson | 70c3856 | 2010-06-14 14:56:33 +0100 | [diff] [blame] | 1022 | Elf_Scn *sec, *sec_strndx, *opdsec; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1023 | Elf *elf; |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1024 | int nr = 0; |
Eric B Munson | 70c3856 | 2010-06-14 14:56:33 +0100 | [diff] [blame] | 1025 | size_t opdidx = 0; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1026 | |
Marti Raudsepp | 8408712 | 2009-10-24 19:10:36 +0300 | [diff] [blame] | 1027 | elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1028 | if (elf == NULL) { |
Dave Martin | 8b1389e | 2010-07-30 09:36:08 -0300 | [diff] [blame] | 1029 | pr_debug("%s: cannot read %s ELF file.\n", __func__, name); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1030 | goto out_close; |
| 1031 | } |
| 1032 | |
| 1033 | if (gelf_getehdr(elf, &ehdr) == NULL) { |
Dave Martin | 8b1389e | 2010-07-30 09:36:08 -0300 | [diff] [blame] | 1034 | pr_debug("%s: cannot get elf header.\n", __func__); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1035 | goto out_elf_end; |
| 1036 | } |
| 1037 | |
Dave Martin | 6da80ce | 2010-07-30 09:50:09 -0300 | [diff] [blame] | 1038 | /* Always reject images with a mismatched build-id: */ |
Dave Martin | 21916c3 | 2010-07-30 09:08:08 -0300 | [diff] [blame] | 1039 | if (self->has_build_id) { |
| 1040 | u8 build_id[BUILD_ID_SIZE]; |
| 1041 | |
| 1042 | if (elf_read_build_id(elf, build_id, |
| 1043 | BUILD_ID_SIZE) != BUILD_ID_SIZE) |
| 1044 | goto out_elf_end; |
| 1045 | |
| 1046 | if (!dso__build_id_equal(self, build_id)) |
| 1047 | goto out_elf_end; |
| 1048 | } |
| 1049 | |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 1050 | sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL); |
| 1051 | if (sec == NULL) { |
Dave Martin | 6da80ce | 2010-07-30 09:50:09 -0300 | [diff] [blame] | 1052 | if (want_symtab) |
| 1053 | goto out_elf_end; |
| 1054 | |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 1055 | sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL); |
| 1056 | if (sec == NULL) |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 1057 | goto out_elf_end; |
Arnaldo Carvalho de Melo | 8ce998d | 2009-06-03 00:54:33 -0300 | [diff] [blame] | 1058 | } |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1059 | |
Eric B Munson | 70c3856 | 2010-06-14 14:56:33 +0100 | [diff] [blame] | 1060 | opdsec = elf_section_by_name(elf, &ehdr, &opdshdr, ".opd", &opdidx); |
| 1061 | if (opdsec) |
| 1062 | opddata = elf_rawdata(opdsec, NULL); |
| 1063 | |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1064 | syms = elf_getdata(sec, NULL); |
| 1065 | if (syms == NULL) |
| 1066 | goto out_elf_end; |
| 1067 | |
| 1068 | sec = elf_getscn(elf, shdr.sh_link); |
| 1069 | if (sec == NULL) |
| 1070 | goto out_elf_end; |
| 1071 | |
| 1072 | symstrs = elf_getdata(sec, NULL); |
| 1073 | if (symstrs == NULL) |
| 1074 | goto out_elf_end; |
| 1075 | |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 1076 | sec_strndx = elf_getscn(elf, ehdr.e_shstrndx); |
| 1077 | if (sec_strndx == NULL) |
| 1078 | goto out_elf_end; |
| 1079 | |
| 1080 | secstrs = elf_getdata(sec_strndx, NULL); |
Stoyan Gaydarov | 9b30a26 | 2009-07-30 05:25:29 -0500 | [diff] [blame] | 1081 | if (secstrs == NULL) |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 1082 | goto out_elf_end; |
| 1083 | |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1084 | nr_syms = shdr.sh_size / shdr.sh_entsize; |
| 1085 | |
Arjan van de Ven | e9fbc9d | 2009-06-06 21:22:33 +0200 | [diff] [blame] | 1086 | memset(&sym, 0, sizeof(sym)); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1087 | if (self->kernel == DSO_TYPE_USER) { |
Mike Galbraith | d20ff6b | 2009-07-20 14:01:38 +0200 | [diff] [blame] | 1088 | self->adjust_symbols = (ehdr.e_type == ET_EXEC || |
Arnaldo Carvalho de Melo | 30d7a77 | 2009-07-02 21:24:14 -0300 | [diff] [blame] | 1089 | elf_section_by_name(elf, &ehdr, &shdr, |
| 1090 | ".gnu.prelink_undo", |
| 1091 | NULL) != NULL); |
Mike Galbraith | d20ff6b | 2009-07-20 14:01:38 +0200 | [diff] [blame] | 1092 | } else self->adjust_symbols = 0; |
| 1093 | |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 1094 | elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) { |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1095 | struct symbol *f; |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 1096 | const char *elf_name = elf_sym__name(&sym, symstrs); |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 1097 | char *demangled = NULL; |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 1098 | int is_label = elf_sym__is_label(&sym); |
| 1099 | const char *section_name; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1100 | |
Arnaldo Carvalho de Melo | 9de89fe | 2010-02-03 16:52:00 -0200 | [diff] [blame] | 1101 | if (kmap && kmap->ref_reloc_sym && kmap->ref_reloc_sym->name && |
| 1102 | strcmp(elf_name, kmap->ref_reloc_sym->name) == 0) |
| 1103 | kmap->ref_reloc_sym->unrelocated_addr = sym.st_value; |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 1104 | |
Arnaldo Carvalho de Melo | d45868d | 2009-12-11 14:50:38 -0200 | [diff] [blame] | 1105 | if (!is_label && !elf_sym__is_a(&sym, map->type)) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1106 | continue; |
| 1107 | |
Dave Martin | 696b97a | 2010-08-09 12:21:18 +0100 | [diff] [blame] | 1108 | /* Reject ARM ELF "mapping symbols": these aren't unique and |
| 1109 | * don't identify functions, so will confuse the profile |
| 1110 | * output: */ |
| 1111 | if (ehdr.e_machine == EM_ARM) { |
| 1112 | if (!strcmp(elf_name, "$a") || |
| 1113 | !strcmp(elf_name, "$d") || |
| 1114 | !strcmp(elf_name, "$t")) |
| 1115 | continue; |
| 1116 | } |
| 1117 | |
Eric B Munson | 70c3856 | 2010-06-14 14:56:33 +0100 | [diff] [blame] | 1118 | if (opdsec && sym.st_shndx == opdidx) { |
| 1119 | u32 offset = sym.st_value - opdshdr.sh_addr; |
| 1120 | u64 *opd = opddata->d_buf + offset; |
| 1121 | sym.st_value = *opd; |
| 1122 | sym.st_shndx = elf_addr_to_index(elf, sym.st_value); |
| 1123 | } |
| 1124 | |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1125 | sec = elf_getscn(elf, sym.st_shndx); |
| 1126 | if (!sec) |
| 1127 | goto out_elf_end; |
| 1128 | |
| 1129 | gelf_getshdr(sec, &shdr); |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 1130 | |
Arnaldo Carvalho de Melo | d45868d | 2009-12-11 14:50:38 -0200 | [diff] [blame] | 1131 | if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type)) |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 1132 | continue; |
| 1133 | |
| 1134 | section_name = elf_sec__name(&shdr, secstrs); |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 1135 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1136 | if (self->kernel != DSO_TYPE_USER || kmodule) { |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 1137 | char dso_name[PATH_MAX]; |
| 1138 | |
| 1139 | if (strcmp(section_name, |
Arnaldo Carvalho de Melo | b63be8d | 2010-03-15 15:03:50 -0300 | [diff] [blame] | 1140 | (curr_dso->short_name + |
| 1141 | self->short_name_len)) == 0) |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 1142 | goto new_symbol; |
| 1143 | |
| 1144 | if (strcmp(section_name, ".text") == 0) { |
| 1145 | curr_map = map; |
| 1146 | curr_dso = self; |
| 1147 | goto new_symbol; |
| 1148 | } |
| 1149 | |
| 1150 | snprintf(dso_name, sizeof(dso_name), |
| 1151 | "%s%s", self->short_name, section_name); |
| 1152 | |
Arnaldo Carvalho de Melo | 9de89fe | 2010-02-03 16:52:00 -0200 | [diff] [blame] | 1153 | 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] | 1154 | if (curr_map == NULL) { |
| 1155 | u64 start = sym.st_value; |
| 1156 | |
| 1157 | if (kmodule) |
| 1158 | start += map->start + shdr.sh_offset; |
| 1159 | |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 1160 | curr_dso = dso__new(dso_name); |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 1161 | if (curr_dso == NULL) |
| 1162 | goto out_elf_end; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1163 | curr_dso->kernel = self->kernel; |
Arnaldo Carvalho de Melo | 3610583 | 2009-11-27 16:29:16 -0200 | [diff] [blame] | 1164 | curr_map = map__new2(start, curr_dso, |
Arnaldo Carvalho de Melo | 6275ce2 | 2010-02-03 16:52:01 -0200 | [diff] [blame] | 1165 | map->type); |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 1166 | if (curr_map == NULL) { |
| 1167 | dso__delete(curr_dso); |
| 1168 | goto out_elf_end; |
| 1169 | } |
Arnaldo Carvalho de Melo | ed52ce2 | 2009-10-19 17:17:57 -0200 | [diff] [blame] | 1170 | curr_map->map_ip = identity__map_ip; |
| 1171 | curr_map->unmap_ip = identity__map_ip; |
Arnaldo Carvalho de Melo | b0a9ab6 | 2010-03-15 11:46:58 -0300 | [diff] [blame] | 1172 | curr_dso->origin = self->origin; |
Arnaldo Carvalho de Melo | 9de89fe | 2010-02-03 16:52:00 -0200 | [diff] [blame] | 1173 | map_groups__insert(kmap->kmaps, curr_map); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1174 | dsos__add(&self->node, curr_dso); |
Arnaldo Carvalho de Melo | 6275ce2 | 2010-02-03 16:52:01 -0200 | [diff] [blame] | 1175 | dso__set_loaded(curr_dso, map->type); |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 1176 | } else |
| 1177 | curr_dso = curr_map->dso; |
| 1178 | |
| 1179 | goto new_symbol; |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 1180 | } |
| 1181 | |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 1182 | if (curr_dso->adjust_symbols) { |
Arnaldo Carvalho de Melo | 29a9f66 | 2010-02-03 16:52:06 -0200 | [diff] [blame] | 1183 | pr_debug4("%s: adjusting symbol: st_value: %#Lx " |
| 1184 | "sh_addr: %#Lx sh_offset: %#Lx\n", __func__, |
| 1185 | (u64)sym.st_value, (u64)shdr.sh_addr, |
| 1186 | (u64)shdr.sh_offset); |
Arnaldo Carvalho de Melo | f5812a7 | 2009-06-30 11:43:17 -0300 | [diff] [blame] | 1187 | sym.st_value -= shdr.sh_addr - shdr.sh_offset; |
Arnaldo Carvalho de Melo | af427bf | 2009-10-05 14:26:17 -0300 | [diff] [blame] | 1188 | } |
Arnaldo Carvalho de Melo | 28ac909 | 2009-07-20 14:14:12 -0300 | [diff] [blame] | 1189 | /* |
| 1190 | * We need to figure out if the object was created from C++ sources |
| 1191 | * DWARF DW_compile_unit has this, but we don't always have access |
| 1192 | * to it... |
| 1193 | */ |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 1194 | demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI); |
Arnaldo Carvalho de Melo | 28ac909 | 2009-07-20 14:14:12 -0300 | [diff] [blame] | 1195 | if (demangled != NULL) |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 1196 | elf_name = demangled; |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 1197 | new_symbol: |
Arnaldo Carvalho de Melo | c408fed | 2010-08-05 12:59:47 -0300 | [diff] [blame] | 1198 | f = symbol__new(sym.st_value, sym.st_size, |
| 1199 | GELF_ST_BIND(sym.st_info), elf_name); |
Arnaldo Carvalho de Melo | 28ac909 | 2009-07-20 14:14:12 -0300 | [diff] [blame] | 1200 | free(demangled); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1201 | if (!f) |
| 1202 | goto out_elf_end; |
| 1203 | |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 1204 | if (filter && filter(curr_map, f)) |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 1205 | symbol__delete(f); |
Arnaldo Carvalho de Melo | 69ee69f | 2009-05-28 14:55:26 -0300 | [diff] [blame] | 1206 | else { |
Arnaldo Carvalho de Melo | 6a4694a | 2009-11-27 16:29:17 -0200 | [diff] [blame] | 1207 | symbols__insert(&curr_dso->symbols[curr_map->type], f); |
Arnaldo Carvalho de Melo | 69ee69f | 2009-05-28 14:55:26 -0300 | [diff] [blame] | 1208 | nr++; |
| 1209 | } |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1210 | } |
| 1211 | |
Arnaldo Carvalho de Melo | 2e538c4 | 2009-10-07 13:48:56 -0300 | [diff] [blame] | 1212 | /* |
| 1213 | * For misannotated, zeroed, ASM function sizes. |
| 1214 | */ |
Arnaldo Carvalho de Melo | 6275ce2 | 2010-02-03 16:52:01 -0200 | [diff] [blame] | 1215 | if (nr > 0) { |
Arnaldo Carvalho de Melo | 6a4694a | 2009-11-27 16:29:17 -0200 | [diff] [blame] | 1216 | symbols__fixup_end(&self->symbols[map->type]); |
Arnaldo Carvalho de Melo | 6275ce2 | 2010-02-03 16:52:01 -0200 | [diff] [blame] | 1217 | if (kmap) { |
| 1218 | /* |
| 1219 | * We need to fixup this here too because we create new |
| 1220 | * maps here, for things like vsyscall sections. |
| 1221 | */ |
| 1222 | __map_groups__fixup_end(kmap->kmaps, map->type); |
| 1223 | } |
| 1224 | } |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1225 | err = nr; |
| 1226 | out_elf_end: |
| 1227 | elf_end(elf); |
| 1228 | out_close: |
| 1229 | return err; |
| 1230 | } |
| 1231 | |
Arnaldo Carvalho de Melo | 78075ca | 2009-11-20 20:51:26 -0200 | [diff] [blame] | 1232 | static bool dso__build_id_equal(const struct dso *self, u8 *build_id) |
| 1233 | { |
| 1234 | return memcmp(self->build_id, build_id, sizeof(self->build_id)) == 0; |
| 1235 | } |
| 1236 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1237 | bool __dsos__read_build_ids(struct list_head *head, bool with_hits) |
Frederic Weisbecker | 57f395a | 2009-11-11 04:51:04 +0100 | [diff] [blame] | 1238 | { |
Arnaldo Carvalho de Melo | e30a3d1 | 2009-11-18 20:20:51 -0200 | [diff] [blame] | 1239 | bool have_build_id = false; |
Frederic Weisbecker | 57f395a | 2009-11-11 04:51:04 +0100 | [diff] [blame] | 1240 | struct dso *pos; |
| 1241 | |
Arnaldo Carvalho de Melo | 6122e4e | 2010-02-03 16:52:05 -0200 | [diff] [blame] | 1242 | list_for_each_entry(pos, head, node) { |
| 1243 | if (with_hits && !pos->hit) |
| 1244 | continue; |
Arnaldo Carvalho de Melo | f6e1467 | 2010-05-19 11:13:02 -0300 | [diff] [blame] | 1245 | if (pos->has_build_id) { |
| 1246 | have_build_id = true; |
| 1247 | continue; |
| 1248 | } |
Arnaldo Carvalho de Melo | e30a3d1 | 2009-11-18 20:20:51 -0200 | [diff] [blame] | 1249 | if (filename__read_build_id(pos->long_name, pos->build_id, |
| 1250 | sizeof(pos->build_id)) > 0) { |
| 1251 | have_build_id = true; |
| 1252 | pos->has_build_id = true; |
| 1253 | } |
Arnaldo Carvalho de Melo | 6122e4e | 2010-02-03 16:52:05 -0200 | [diff] [blame] | 1254 | } |
Frederic Weisbecker | 57f395a | 2009-11-11 04:51:04 +0100 | [diff] [blame] | 1255 | |
Arnaldo Carvalho de Melo | e30a3d1 | 2009-11-18 20:20:51 -0200 | [diff] [blame] | 1256 | return have_build_id; |
Frederic Weisbecker | 57f395a | 2009-11-11 04:51:04 +0100 | [diff] [blame] | 1257 | } |
| 1258 | |
Arnaldo Carvalho de Melo | fd7a346 | 2009-11-20 20:51:25 -0200 | [diff] [blame] | 1259 | /* |
| 1260 | * Align offset to 4 bytes as needed for note name and descriptor data. |
| 1261 | */ |
| 1262 | #define NOTE_ALIGN(n) (((n) + 3) & -4U) |
| 1263 | |
Dave Martin | 21916c3 | 2010-07-30 09:08:08 -0300 | [diff] [blame] | 1264 | static int elf_read_build_id(Elf *elf, void *bf, size_t size) |
Arnaldo Carvalho de Melo | 4d1e00a | 2009-08-05 19:02:49 -0300 | [diff] [blame] | 1265 | { |
Dave Martin | 21916c3 | 2010-07-30 09:08:08 -0300 | [diff] [blame] | 1266 | int err = -1; |
Arnaldo Carvalho de Melo | 4d1e00a | 2009-08-05 19:02:49 -0300 | [diff] [blame] | 1267 | GElf_Ehdr ehdr; |
| 1268 | GElf_Shdr shdr; |
Arnaldo Carvalho de Melo | fd7a346 | 2009-11-20 20:51:25 -0200 | [diff] [blame] | 1269 | Elf_Data *data; |
Arnaldo Carvalho de Melo | 4d1e00a | 2009-08-05 19:02:49 -0300 | [diff] [blame] | 1270 | Elf_Scn *sec; |
Pekka Enberg | e57cfcd | 2009-11-22 12:29:44 +0200 | [diff] [blame] | 1271 | Elf_Kind ek; |
Arnaldo Carvalho de Melo | fd7a346 | 2009-11-20 20:51:25 -0200 | [diff] [blame] | 1272 | void *ptr; |
Arnaldo Carvalho de Melo | 4d1e00a | 2009-08-05 19:02:49 -0300 | [diff] [blame] | 1273 | |
Arnaldo Carvalho de Melo | 2643ce114 | 2009-11-03 21:46:10 -0200 | [diff] [blame] | 1274 | if (size < BUILD_ID_SIZE) |
| 1275 | goto out; |
| 1276 | |
Pekka Enberg | e57cfcd | 2009-11-22 12:29:44 +0200 | [diff] [blame] | 1277 | ek = elf_kind(elf); |
| 1278 | if (ek != ELF_K_ELF) |
Dave Martin | 21916c3 | 2010-07-30 09:08:08 -0300 | [diff] [blame] | 1279 | goto out; |
Pekka Enberg | e57cfcd | 2009-11-22 12:29:44 +0200 | [diff] [blame] | 1280 | |
Arnaldo Carvalho de Melo | 4d1e00a | 2009-08-05 19:02:49 -0300 | [diff] [blame] | 1281 | if (gelf_getehdr(elf, &ehdr) == NULL) { |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 1282 | pr_err("%s: cannot get elf header.\n", __func__); |
Dave Martin | 21916c3 | 2010-07-30 09:08:08 -0300 | [diff] [blame] | 1283 | goto out; |
Arnaldo Carvalho de Melo | 4d1e00a | 2009-08-05 19:02:49 -0300 | [diff] [blame] | 1284 | } |
| 1285 | |
Arnaldo Carvalho de Melo | 2643ce114 | 2009-11-03 21:46:10 -0200 | [diff] [blame] | 1286 | sec = elf_section_by_name(elf, &ehdr, &shdr, |
| 1287 | ".note.gnu.build-id", NULL); |
Arnaldo Carvalho de Melo | fd7a346 | 2009-11-20 20:51:25 -0200 | [diff] [blame] | 1288 | if (sec == NULL) { |
| 1289 | sec = elf_section_by_name(elf, &ehdr, &shdr, |
| 1290 | ".notes", NULL); |
| 1291 | if (sec == NULL) |
Dave Martin | 21916c3 | 2010-07-30 09:08:08 -0300 | [diff] [blame] | 1292 | goto out; |
Arnaldo Carvalho de Melo | fd7a346 | 2009-11-20 20:51:25 -0200 | [diff] [blame] | 1293 | } |
| 1294 | |
| 1295 | data = elf_getdata(sec, NULL); |
| 1296 | if (data == NULL) |
Dave Martin | 21916c3 | 2010-07-30 09:08:08 -0300 | [diff] [blame] | 1297 | goto out; |
Arnaldo Carvalho de Melo | 4d1e00a | 2009-08-05 19:02:49 -0300 | [diff] [blame] | 1298 | |
Arnaldo Carvalho de Melo | fd7a346 | 2009-11-20 20:51:25 -0200 | [diff] [blame] | 1299 | ptr = data->d_buf; |
| 1300 | while (ptr < (data->d_buf + data->d_size)) { |
| 1301 | GElf_Nhdr *nhdr = ptr; |
| 1302 | int namesz = NOTE_ALIGN(nhdr->n_namesz), |
| 1303 | descsz = NOTE_ALIGN(nhdr->n_descsz); |
| 1304 | const char *name; |
| 1305 | |
| 1306 | ptr += sizeof(*nhdr); |
| 1307 | name = ptr; |
| 1308 | ptr += namesz; |
| 1309 | if (nhdr->n_type == NT_GNU_BUILD_ID && |
| 1310 | nhdr->n_namesz == sizeof("GNU")) { |
| 1311 | if (memcmp(name, "GNU", sizeof("GNU")) == 0) { |
| 1312 | memcpy(bf, ptr, BUILD_ID_SIZE); |
| 1313 | err = BUILD_ID_SIZE; |
| 1314 | break; |
| 1315 | } |
| 1316 | } |
| 1317 | ptr += descsz; |
| 1318 | } |
Dave Martin | 21916c3 | 2010-07-30 09:08:08 -0300 | [diff] [blame] | 1319 | |
| 1320 | out: |
| 1321 | return err; |
| 1322 | } |
| 1323 | |
| 1324 | int filename__read_build_id(const char *filename, void *bf, size_t size) |
| 1325 | { |
| 1326 | int fd, err = -1; |
| 1327 | Elf *elf; |
| 1328 | |
| 1329 | if (size < BUILD_ID_SIZE) |
| 1330 | goto out; |
| 1331 | |
| 1332 | fd = open(filename, O_RDONLY); |
| 1333 | if (fd < 0) |
| 1334 | goto out; |
| 1335 | |
| 1336 | elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); |
| 1337 | if (elf == NULL) { |
| 1338 | pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename); |
| 1339 | goto out_close; |
| 1340 | } |
| 1341 | |
| 1342 | err = elf_read_build_id(elf, bf, size); |
| 1343 | |
Arnaldo Carvalho de Melo | 2643ce114 | 2009-11-03 21:46:10 -0200 | [diff] [blame] | 1344 | elf_end(elf); |
| 1345 | out_close: |
| 1346 | close(fd); |
| 1347 | out: |
| 1348 | return err; |
| 1349 | } |
| 1350 | |
Arnaldo Carvalho de Melo | f1617b4 | 2009-11-18 20:20:52 -0200 | [diff] [blame] | 1351 | int sysfs__read_build_id(const char *filename, void *build_id, size_t size) |
| 1352 | { |
| 1353 | int fd, err = -1; |
| 1354 | |
| 1355 | if (size < BUILD_ID_SIZE) |
| 1356 | goto out; |
| 1357 | |
| 1358 | fd = open(filename, O_RDONLY); |
| 1359 | if (fd < 0) |
| 1360 | goto out; |
| 1361 | |
| 1362 | while (1) { |
| 1363 | char bf[BUFSIZ]; |
| 1364 | GElf_Nhdr nhdr; |
| 1365 | int namesz, descsz; |
| 1366 | |
| 1367 | if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr)) |
| 1368 | break; |
| 1369 | |
Arnaldo Carvalho de Melo | fd7a346 | 2009-11-20 20:51:25 -0200 | [diff] [blame] | 1370 | namesz = NOTE_ALIGN(nhdr.n_namesz); |
| 1371 | descsz = NOTE_ALIGN(nhdr.n_descsz); |
Arnaldo Carvalho de Melo | f1617b4 | 2009-11-18 20:20:52 -0200 | [diff] [blame] | 1372 | if (nhdr.n_type == NT_GNU_BUILD_ID && |
| 1373 | nhdr.n_namesz == sizeof("GNU")) { |
| 1374 | if (read(fd, bf, namesz) != namesz) |
| 1375 | break; |
| 1376 | if (memcmp(bf, "GNU", sizeof("GNU")) == 0) { |
| 1377 | if (read(fd, build_id, |
| 1378 | BUILD_ID_SIZE) == BUILD_ID_SIZE) { |
| 1379 | err = 0; |
| 1380 | break; |
| 1381 | } |
| 1382 | } else if (read(fd, bf, descsz) != descsz) |
| 1383 | break; |
| 1384 | } else { |
| 1385 | int n = namesz + descsz; |
| 1386 | if (read(fd, bf, n) != n) |
| 1387 | break; |
| 1388 | } |
| 1389 | } |
| 1390 | close(fd); |
| 1391 | out: |
| 1392 | return err; |
| 1393 | } |
| 1394 | |
Arnaldo Carvalho de Melo | 94cb9e3 | 2009-08-06 14:43:17 -0300 | [diff] [blame] | 1395 | char dso__symtab_origin(const struct dso *self) |
| 1396 | { |
| 1397 | static const char origin[] = { |
| 1398 | [DSO__ORIG_KERNEL] = 'k', |
| 1399 | [DSO__ORIG_JAVA_JIT] = 'j', |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 1400 | [DSO__ORIG_BUILD_ID_CACHE] = 'B', |
Arnaldo Carvalho de Melo | 94cb9e3 | 2009-08-06 14:43:17 -0300 | [diff] [blame] | 1401 | [DSO__ORIG_FEDORA] = 'f', |
| 1402 | [DSO__ORIG_UBUNTU] = 'u', |
| 1403 | [DSO__ORIG_BUILDID] = 'b', |
| 1404 | [DSO__ORIG_DSO] = 'd', |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1405 | [DSO__ORIG_KMODULE] = 'K', |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1406 | [DSO__ORIG_GUEST_KERNEL] = 'g', |
| 1407 | [DSO__ORIG_GUEST_KMODULE] = 'G', |
Arnaldo Carvalho de Melo | 94cb9e3 | 2009-08-06 14:43:17 -0300 | [diff] [blame] | 1408 | }; |
| 1409 | |
| 1410 | if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND) |
| 1411 | return '!'; |
| 1412 | return origin[self->origin]; |
| 1413 | } |
| 1414 | |
Arnaldo Carvalho de Melo | 9de89fe | 2010-02-03 16:52:00 -0200 | [diff] [blame] | 1415 | 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] | 1416 | { |
Arnaldo Carvalho de Melo | 4d1e00a | 2009-08-05 19:02:49 -0300 | [diff] [blame] | 1417 | int size = PATH_MAX; |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 1418 | char *name; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1419 | int ret = -1; |
| 1420 | int fd; |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 1421 | struct machine *machine; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1422 | const char *root_dir; |
Dave Martin | 6da80ce | 2010-07-30 09:50:09 -0300 | [diff] [blame] | 1423 | int want_symtab; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1424 | |
Arnaldo Carvalho de Melo | 3610583 | 2009-11-27 16:29:16 -0200 | [diff] [blame] | 1425 | dso__set_loaded(self, map->type); |
Arnaldo Carvalho de Melo | 66bd842 | 2009-10-28 21:51:21 -0200 | [diff] [blame] | 1426 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1427 | if (self->kernel == DSO_TYPE_KERNEL) |
Arnaldo Carvalho de Melo | 9de89fe | 2010-02-03 16:52:00 -0200 | [diff] [blame] | 1428 | return dso__load_kernel_sym(self, map, filter); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1429 | else if (self->kernel == DSO_TYPE_GUEST_KERNEL) |
| 1430 | return dso__load_guest_kernel_sym(self, map, filter); |
| 1431 | |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 1432 | if (map->groups && map->groups->machine) |
| 1433 | machine = map->groups->machine; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1434 | else |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 1435 | machine = NULL; |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 1436 | |
| 1437 | name = malloc(size); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1438 | if (!name) |
| 1439 | return -1; |
| 1440 | |
Arnaldo Carvalho de Melo | 30d7a77 | 2009-07-02 21:24:14 -0300 | [diff] [blame] | 1441 | self->adjust_symbols = 0; |
Arnaldo Carvalho de Melo | f5812a7 | 2009-06-30 11:43:17 -0300 | [diff] [blame] | 1442 | |
Arnaldo Carvalho de Melo | 94cb9e3 | 2009-08-06 14:43:17 -0300 | [diff] [blame] | 1443 | if (strncmp(self->name, "/tmp/perf-", 10) == 0) { |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 1444 | ret = dso__load_perf_map(self, map, filter); |
Arnaldo Carvalho de Melo | 94cb9e3 | 2009-08-06 14:43:17 -0300 | [diff] [blame] | 1445 | self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT : |
| 1446 | DSO__ORIG_NOT_FOUND; |
| 1447 | return ret; |
| 1448 | } |
| 1449 | |
Dave Martin | 6da80ce | 2010-07-30 09:50:09 -0300 | [diff] [blame] | 1450 | /* Iterate over candidate debug images. |
| 1451 | * On the first pass, only load images if they have a full symtab. |
| 1452 | * Failing that, do a second pass where we accept .dynsym also |
| 1453 | */ |
| 1454 | for (self->origin = DSO__ORIG_BUILD_ID_CACHE, want_symtab = 1; |
| 1455 | self->origin != DSO__ORIG_NOT_FOUND; |
| 1456 | self->origin++) { |
Arnaldo Carvalho de Melo | 94cb9e3 | 2009-08-06 14:43:17 -0300 | [diff] [blame] | 1457 | switch (self->origin) { |
Dave Martin | 6da80ce | 2010-07-30 09:50:09 -0300 | [diff] [blame] | 1458 | case DSO__ORIG_BUILD_ID_CACHE: |
David Ahern | ec5761e | 2010-12-09 13:27:07 -0700 | [diff] [blame^] | 1459 | /* skip the locally configured cache if a symfs is given */ |
| 1460 | if (symbol_conf.symfs[0] || |
| 1461 | (dso__build_id_filename(self, name, size) == NULL)) { |
Dave Martin | 6da80ce | 2010-07-30 09:50:09 -0300 | [diff] [blame] | 1462 | continue; |
David Ahern | ec5761e | 2010-12-09 13:27:07 -0700 | [diff] [blame^] | 1463 | } |
Dave Martin | 6da80ce | 2010-07-30 09:50:09 -0300 | [diff] [blame] | 1464 | break; |
Arnaldo Carvalho de Melo | 94cb9e3 | 2009-08-06 14:43:17 -0300 | [diff] [blame] | 1465 | case DSO__ORIG_FEDORA: |
David Ahern | ec5761e | 2010-12-09 13:27:07 -0700 | [diff] [blame^] | 1466 | snprintf(name, size, "%s/usr/lib/debug%s.debug", |
| 1467 | symbol_conf.symfs, self->long_name); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1468 | break; |
Arnaldo Carvalho de Melo | 94cb9e3 | 2009-08-06 14:43:17 -0300 | [diff] [blame] | 1469 | case DSO__ORIG_UBUNTU: |
David Ahern | ec5761e | 2010-12-09 13:27:07 -0700 | [diff] [blame^] | 1470 | snprintf(name, size, "%s/usr/lib/debug%s", |
| 1471 | symbol_conf.symfs, self->long_name); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1472 | break; |
Dave Martin | 6da80ce | 2010-07-30 09:50:09 -0300 | [diff] [blame] | 1473 | case DSO__ORIG_BUILDID: { |
| 1474 | char build_id_hex[BUILD_ID_SIZE * 2 + 1]; |
| 1475 | |
| 1476 | if (!self->has_build_id) |
| 1477 | continue; |
| 1478 | |
| 1479 | build_id__sprintf(self->build_id, |
| 1480 | sizeof(self->build_id), |
| 1481 | build_id_hex); |
| 1482 | snprintf(name, size, |
David Ahern | ec5761e | 2010-12-09 13:27:07 -0700 | [diff] [blame^] | 1483 | "%s/usr/lib/debug/.build-id/%.2s/%s.debug", |
| 1484 | symbol_conf.symfs, build_id_hex, build_id_hex + 2); |
Arnaldo Carvalho de Melo | 4d1e00a | 2009-08-05 19:02:49 -0300 | [diff] [blame] | 1485 | } |
Dave Martin | 6da80ce | 2010-07-30 09:50:09 -0300 | [diff] [blame] | 1486 | break; |
Arnaldo Carvalho de Melo | 94cb9e3 | 2009-08-06 14:43:17 -0300 | [diff] [blame] | 1487 | case DSO__ORIG_DSO: |
David Ahern | ec5761e | 2010-12-09 13:27:07 -0700 | [diff] [blame^] | 1488 | snprintf(name, size, "%s%s", |
| 1489 | symbol_conf.symfs, self->long_name); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1490 | break; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1491 | case DSO__ORIG_GUEST_KMODULE: |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 1492 | if (map->groups && map->groups->machine) |
| 1493 | root_dir = map->groups->machine->root_dir; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1494 | else |
| 1495 | root_dir = ""; |
David Ahern | ec5761e | 2010-12-09 13:27:07 -0700 | [diff] [blame^] | 1496 | snprintf(name, size, "%s%s%s", symbol_conf.symfs, |
| 1497 | root_dir, self->long_name); |
| 1498 | break; |
| 1499 | |
| 1500 | case DSO__ORIG_KMODULE: |
| 1501 | snprintf(name, size, "%s%s", symbol_conf.symfs, |
| 1502 | self->long_name); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1503 | break; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1504 | |
| 1505 | default: |
Dave Martin | 6da80ce | 2010-07-30 09:50:09 -0300 | [diff] [blame] | 1506 | /* |
| 1507 | * If we wanted a full symtab but no image had one, |
| 1508 | * relax our requirements and repeat the search. |
| 1509 | */ |
| 1510 | if (want_symtab) { |
| 1511 | want_symtab = 0; |
| 1512 | self->origin = DSO__ORIG_BUILD_ID_CACHE; |
| 1513 | } else |
| 1514 | continue; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1515 | } |
Dave Martin | 6da80ce | 2010-07-30 09:50:09 -0300 | [diff] [blame] | 1516 | |
| 1517 | /* Name is now the name of the next image to try */ |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1518 | fd = open(name, O_RDONLY); |
Dave Martin | 6da80ce | 2010-07-30 09:50:09 -0300 | [diff] [blame] | 1519 | if (fd < 0) |
| 1520 | continue; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1521 | |
Dave Martin | 6da80ce | 2010-07-30 09:50:09 -0300 | [diff] [blame] | 1522 | ret = dso__load_sym(self, map, name, fd, filter, 0, |
| 1523 | want_symtab); |
| 1524 | close(fd); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1525 | |
Dave Martin | 6da80ce | 2010-07-30 09:50:09 -0300 | [diff] [blame] | 1526 | /* |
| 1527 | * Some people seem to have debuginfo files _WITHOUT_ debug |
| 1528 | * info!?!? |
| 1529 | */ |
| 1530 | if (!ret) |
| 1531 | continue; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1532 | |
Dave Martin | 6da80ce | 2010-07-30 09:50:09 -0300 | [diff] [blame] | 1533 | if (ret > 0) { |
| 1534 | int nr_plt = dso__synthesize_plt_symbols(self, map, filter); |
| 1535 | if (nr_plt > 0) |
| 1536 | ret += nr_plt; |
| 1537 | break; |
| 1538 | } |
Arnaldo Carvalho de Melo | a25e46c | 2009-07-11 12:18:36 -0300 | [diff] [blame] | 1539 | } |
Dave Martin | 6da80ce | 2010-07-30 09:50:09 -0300 | [diff] [blame] | 1540 | |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1541 | free(name); |
Arnaldo Carvalho de Melo | 1340e6b | 2009-08-11 17:04:36 -0300 | [diff] [blame] | 1542 | if (ret < 0 && strstr(self->name, " (deleted)") != NULL) |
| 1543 | return 0; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1544 | return ret; |
| 1545 | } |
| 1546 | |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 1547 | struct map *map_groups__find_by_name(struct map_groups *self, |
| 1548 | enum map_type type, const char *name) |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1549 | { |
| 1550 | struct rb_node *nd; |
| 1551 | |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 1552 | 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] | 1553 | struct map *map = rb_entry(nd, struct map, rb_node); |
| 1554 | |
Arnaldo Carvalho de Melo | b7cece76 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 1555 | if (map->dso && strcmp(map->dso->short_name, name) == 0) |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1556 | return map; |
| 1557 | } |
| 1558 | |
| 1559 | return NULL; |
| 1560 | } |
| 1561 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1562 | static int dso__kernel_module_get_build_id(struct dso *self, |
| 1563 | const char *root_dir) |
Arnaldo Carvalho de Melo | b7cece76 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 1564 | { |
| 1565 | char filename[PATH_MAX]; |
| 1566 | /* |
| 1567 | * kernel module short names are of the form "[module]" and |
| 1568 | * we need just "module" here. |
| 1569 | */ |
| 1570 | const char *name = self->short_name + 1; |
| 1571 | |
| 1572 | snprintf(filename, sizeof(filename), |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1573 | "%s/sys/module/%.*s/notes/.note.gnu.build-id", |
| 1574 | root_dir, (int)strlen(name) - 1, name); |
Arnaldo Carvalho de Melo | b7cece76 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 1575 | |
| 1576 | if (sysfs__read_build_id(filename, self->build_id, |
| 1577 | sizeof(self->build_id)) == 0) |
| 1578 | self->has_build_id = true; |
| 1579 | |
| 1580 | return 0; |
| 1581 | } |
| 1582 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1583 | static int map_groups__set_modules_path_dir(struct map_groups *self, |
| 1584 | const char *dir_name) |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 1585 | { |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1586 | struct dirent *dent; |
Arnaldo Carvalho de Melo | 5aab621 | 2010-03-25 19:59:00 -0300 | [diff] [blame] | 1587 | DIR *dir = opendir(dir_name); |
Gui Jianfeng | 7453434 | 2010-06-24 15:04:02 +0800 | [diff] [blame] | 1588 | int ret = 0; |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 1589 | |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1590 | if (!dir) { |
Arnaldo Carvalho de Melo | 5aab621 | 2010-03-25 19:59:00 -0300 | [diff] [blame] | 1591 | pr_debug("%s: cannot open %s dir\n", __func__, dir_name); |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1592 | return -1; |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 1593 | } |
| 1594 | |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1595 | while ((dent = readdir(dir)) != NULL) { |
| 1596 | char path[PATH_MAX]; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1597 | struct stat st; |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 1598 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1599 | /*sshfs might return bad dent->d_type, so we have to stat*/ |
| 1600 | sprintf(path, "%s/%s", dir_name, dent->d_name); |
| 1601 | if (stat(path, &st)) |
| 1602 | continue; |
| 1603 | |
| 1604 | if (S_ISDIR(st.st_mode)) { |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1605 | if (!strcmp(dent->d_name, ".") || |
| 1606 | !strcmp(dent->d_name, "..")) |
| 1607 | continue; |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 1608 | |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1609 | snprintf(path, sizeof(path), "%s/%s", |
Arnaldo Carvalho de Melo | 5aab621 | 2010-03-25 19:59:00 -0300 | [diff] [blame] | 1610 | dir_name, dent->d_name); |
Gui Jianfeng | 7453434 | 2010-06-24 15:04:02 +0800 | [diff] [blame] | 1611 | ret = map_groups__set_modules_path_dir(self, path); |
| 1612 | if (ret < 0) |
| 1613 | goto out; |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1614 | } else { |
| 1615 | char *dot = strrchr(dent->d_name, '.'), |
| 1616 | dso_name[PATH_MAX]; |
| 1617 | struct map *map; |
Arnaldo Carvalho de Melo | cfc10d3 | 2009-11-17 15:40:53 -0200 | [diff] [blame] | 1618 | char *long_name; |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 1619 | |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1620 | if (dot == NULL || strcmp(dot, ".ko")) |
| 1621 | continue; |
| 1622 | snprintf(dso_name, sizeof(dso_name), "[%.*s]", |
| 1623 | (int)(dot - dent->d_name), dent->d_name); |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 1624 | |
Arnaldo Carvalho de Melo | a2a99e8 | 2009-10-05 14:26:18 -0300 | [diff] [blame] | 1625 | strxfrchar(dso_name, '-', '_'); |
Arnaldo Carvalho de Melo | 9de89fe | 2010-02-03 16:52:00 -0200 | [diff] [blame] | 1626 | 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] | 1627 | if (map == NULL) |
| 1628 | continue; |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 1629 | |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1630 | snprintf(path, sizeof(path), "%s/%s", |
Arnaldo Carvalho de Melo | 5aab621 | 2010-03-25 19:59:00 -0300 | [diff] [blame] | 1631 | dir_name, dent->d_name); |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 1632 | |
Arnaldo Carvalho de Melo | cfc10d3 | 2009-11-17 15:40:53 -0200 | [diff] [blame] | 1633 | long_name = strdup(path); |
Gui Jianfeng | 7453434 | 2010-06-24 15:04:02 +0800 | [diff] [blame] | 1634 | if (long_name == NULL) { |
| 1635 | ret = -1; |
| 1636 | goto out; |
| 1637 | } |
Arnaldo Carvalho de Melo | cfc10d3 | 2009-11-17 15:40:53 -0200 | [diff] [blame] | 1638 | dso__set_long_name(map->dso, long_name); |
Arnaldo Carvalho de Melo | 6e40625 | 2010-07-29 15:11:30 -0300 | [diff] [blame] | 1639 | map->dso->lname_alloc = 1; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1640 | dso__kernel_module_get_build_id(map->dso, ""); |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 1641 | } |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 1642 | } |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1643 | |
Gui Jianfeng | 7453434 | 2010-06-24 15:04:02 +0800 | [diff] [blame] | 1644 | out: |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1645 | closedir(dir); |
Gui Jianfeng | 7453434 | 2010-06-24 15:04:02 +0800 | [diff] [blame] | 1646 | return ret; |
Mike Galbraith | 6cfcc53 | 2009-07-02 08:08:36 +0200 | [diff] [blame] | 1647 | } |
| 1648 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1649 | static char *get_kernel_version(const char *root_dir) |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1650 | { |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1651 | char version[PATH_MAX]; |
| 1652 | FILE *file; |
| 1653 | char *name, *tmp; |
| 1654 | const char *prefix = "Linux version "; |
| 1655 | |
| 1656 | sprintf(version, "%s/proc/version", root_dir); |
| 1657 | file = fopen(version, "r"); |
| 1658 | if (!file) |
| 1659 | return NULL; |
| 1660 | |
| 1661 | version[0] = '\0'; |
| 1662 | tmp = fgets(version, sizeof(version), file); |
| 1663 | fclose(file); |
| 1664 | |
| 1665 | name = strstr(version, prefix); |
| 1666 | if (!name) |
| 1667 | return NULL; |
| 1668 | name += strlen(prefix); |
| 1669 | tmp = strchr(name, ' '); |
| 1670 | if (tmp) |
| 1671 | *tmp = '\0'; |
| 1672 | |
| 1673 | return strdup(name); |
| 1674 | } |
| 1675 | |
Arnaldo Carvalho de Melo | d28c622 | 2010-04-27 21:20:43 -0300 | [diff] [blame] | 1676 | static int machine__set_modules_path(struct machine *self) |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1677 | { |
| 1678 | char *version; |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1679 | char modules_path[PATH_MAX]; |
| 1680 | |
Arnaldo Carvalho de Melo | d28c622 | 2010-04-27 21:20:43 -0300 | [diff] [blame] | 1681 | version = get_kernel_version(self->root_dir); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1682 | if (!version) |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1683 | return -1; |
| 1684 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1685 | snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s/kernel", |
Arnaldo Carvalho de Melo | d28c622 | 2010-04-27 21:20:43 -0300 | [diff] [blame] | 1686 | self->root_dir, version); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1687 | free(version); |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1688 | |
Arnaldo Carvalho de Melo | d28c622 | 2010-04-27 21:20:43 -0300 | [diff] [blame] | 1689 | return map_groups__set_modules_path_dir(&self->kmaps, modules_path); |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1690 | } |
| 1691 | |
| 1692 | /* |
| 1693 | * Constructor variant for modules (where we know from /proc/modules where |
| 1694 | * they are loaded) and for vmlinux, where only after we load all the |
| 1695 | * symbols we'll know where it starts and ends. |
| 1696 | */ |
Arnaldo Carvalho de Melo | 3610583 | 2009-11-27 16:29:16 -0200 | [diff] [blame] | 1697 | 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] | 1698 | { |
Arnaldo Carvalho de Melo | 5aab621 | 2010-03-25 19:59:00 -0300 | [diff] [blame] | 1699 | struct map *self = calloc(1, (sizeof(*self) + |
| 1700 | (dso->kernel ? sizeof(struct kmap) : 0))); |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1701 | if (self != NULL) { |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1702 | /* |
Arnaldo Carvalho de Melo | afb7b4f | 2009-10-30 16:28:23 -0200 | [diff] [blame] | 1703 | * ->end will be filled after we load all the symbols |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1704 | */ |
Arnaldo Carvalho de Melo | 3610583 | 2009-11-27 16:29:16 -0200 | [diff] [blame] | 1705 | map__init(self, type, start, 0, 0, dso); |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1706 | } |
Arnaldo Carvalho de Melo | afb7b4f | 2009-10-30 16:28:23 -0200 | [diff] [blame] | 1707 | |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1708 | return self; |
| 1709 | } |
| 1710 | |
Arnaldo Carvalho de Melo | d28c622 | 2010-04-27 21:20:43 -0300 | [diff] [blame] | 1711 | struct map *machine__new_module(struct machine *self, u64 start, |
| 1712 | const char *filename) |
Arnaldo Carvalho de Melo | b7cece76 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 1713 | { |
| 1714 | struct map *map; |
Arnaldo Carvalho de Melo | d28c622 | 2010-04-27 21:20:43 -0300 | [diff] [blame] | 1715 | struct dso *dso = __dsos__findnew(&self->kernel_dsos, filename); |
Arnaldo Carvalho de Melo | b7cece76 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 1716 | |
| 1717 | if (dso == NULL) |
| 1718 | return NULL; |
| 1719 | |
| 1720 | map = map__new2(start, dso, MAP__FUNCTION); |
| 1721 | if (map == NULL) |
| 1722 | return NULL; |
| 1723 | |
Arnaldo Carvalho de Melo | d28c622 | 2010-04-27 21:20:43 -0300 | [diff] [blame] | 1724 | if (machine__is_host(self)) |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1725 | dso->origin = DSO__ORIG_KMODULE; |
| 1726 | else |
| 1727 | dso->origin = DSO__ORIG_GUEST_KMODULE; |
Arnaldo Carvalho de Melo | d28c622 | 2010-04-27 21:20:43 -0300 | [diff] [blame] | 1728 | map_groups__insert(&self->kmaps, map); |
Arnaldo Carvalho de Melo | b7cece76 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 1729 | return map; |
| 1730 | } |
| 1731 | |
Arnaldo Carvalho de Melo | d28c622 | 2010-04-27 21:20:43 -0300 | [diff] [blame] | 1732 | static int machine__create_modules(struct machine *self) |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1733 | { |
| 1734 | char *line = NULL; |
| 1735 | size_t n; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1736 | FILE *file; |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1737 | struct map *map; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1738 | const char *modules; |
| 1739 | char path[PATH_MAX]; |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1740 | |
Arnaldo Carvalho de Melo | d28c622 | 2010-04-27 21:20:43 -0300 | [diff] [blame] | 1741 | if (machine__is_default_guest(self)) |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1742 | modules = symbol_conf.default_guest_modules; |
| 1743 | else { |
Arnaldo Carvalho de Melo | d28c622 | 2010-04-27 21:20:43 -0300 | [diff] [blame] | 1744 | sprintf(path, "%s/proc/modules", self->root_dir); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1745 | modules = path; |
| 1746 | } |
| 1747 | |
| 1748 | file = fopen(modules, "r"); |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1749 | if (file == NULL) |
| 1750 | return -1; |
| 1751 | |
| 1752 | while (!feof(file)) { |
| 1753 | char name[PATH_MAX]; |
| 1754 | u64 start; |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1755 | char *sep; |
| 1756 | int line_len; |
| 1757 | |
| 1758 | line_len = getline(&line, &n, file); |
| 1759 | if (line_len < 0) |
| 1760 | break; |
| 1761 | |
| 1762 | if (!line) |
| 1763 | goto out_failure; |
| 1764 | |
| 1765 | line[--line_len] = '\0'; /* \n */ |
| 1766 | |
| 1767 | sep = strrchr(line, 'x'); |
| 1768 | if (sep == NULL) |
| 1769 | continue; |
| 1770 | |
| 1771 | hex2u64(sep + 1, &start); |
| 1772 | |
| 1773 | sep = strchr(line, ' '); |
| 1774 | if (sep == NULL) |
| 1775 | continue; |
| 1776 | |
| 1777 | *sep = '\0'; |
| 1778 | |
| 1779 | snprintf(name, sizeof(name), "[%s]", line); |
Arnaldo Carvalho de Melo | d28c622 | 2010-04-27 21:20:43 -0300 | [diff] [blame] | 1780 | map = machine__new_module(self, start, name); |
Arnaldo Carvalho de Melo | b7cece76 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 1781 | if (map == NULL) |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1782 | goto out_delete_line; |
Arnaldo Carvalho de Melo | d28c622 | 2010-04-27 21:20:43 -0300 | [diff] [blame] | 1783 | dso__kernel_module_get_build_id(map->dso, self->root_dir); |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1784 | } |
| 1785 | |
| 1786 | free(line); |
| 1787 | fclose(file); |
| 1788 | |
Arnaldo Carvalho de Melo | d28c622 | 2010-04-27 21:20:43 -0300 | [diff] [blame] | 1789 | return machine__set_modules_path(self); |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1790 | |
| 1791 | out_delete_line: |
| 1792 | free(line); |
| 1793 | out_failure: |
| 1794 | return -1; |
| 1795 | } |
| 1796 | |
Arnaldo Carvalho de Melo | 9958e1f | 2009-12-11 14:50:36 -0200 | [diff] [blame] | 1797 | 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] | 1798 | const char *vmlinux, symbol_filter_t filter) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1799 | { |
Arnaldo Carvalho de Melo | fbd733b | 2009-11-20 20:51:28 -0200 | [diff] [blame] | 1800 | int err = -1, fd; |
David Ahern | ec5761e | 2010-12-09 13:27:07 -0700 | [diff] [blame^] | 1801 | char symfs_vmlinux[PATH_MAX]; |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1802 | |
David Ahern | ec5761e | 2010-12-09 13:27:07 -0700 | [diff] [blame^] | 1803 | snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s/%s", |
| 1804 | symbol_conf.symfs, vmlinux); |
| 1805 | fd = open(symfs_vmlinux, O_RDONLY); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1806 | if (fd < 0) |
| 1807 | return -1; |
| 1808 | |
Arnaldo Carvalho de Melo | 3610583 | 2009-11-27 16:29:16 -0200 | [diff] [blame] | 1809 | dso__set_loaded(self, map->type); |
David Ahern | ec5761e | 2010-12-09 13:27:07 -0700 | [diff] [blame^] | 1810 | err = dso__load_sym(self, map, symfs_vmlinux, fd, filter, 0, 0); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1811 | close(fd); |
| 1812 | |
Arnaldo Carvalho de Melo | 3846df2 | 2010-02-22 16:15:39 -0300 | [diff] [blame] | 1813 | if (err > 0) |
David Ahern | ec5761e | 2010-12-09 13:27:07 -0700 | [diff] [blame^] | 1814 | pr_debug("Using %s for symbols\n", symfs_vmlinux); |
Arnaldo Carvalho de Melo | 3846df2 | 2010-02-22 16:15:39 -0300 | [diff] [blame] | 1815 | |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 1816 | return err; |
| 1817 | } |
| 1818 | |
Arnaldo Carvalho de Melo | a19afe4 | 2010-01-27 21:05:50 -0200 | [diff] [blame] | 1819 | 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] | 1820 | symbol_filter_t filter) |
Arnaldo Carvalho de Melo | a19afe4 | 2010-01-27 21:05:50 -0200 | [diff] [blame] | 1821 | { |
| 1822 | int i, err = 0; |
Arnaldo Carvalho de Melo | 5ad90e4 | 2010-05-26 13:26:02 -0300 | [diff] [blame] | 1823 | char *filename; |
Arnaldo Carvalho de Melo | a19afe4 | 2010-01-27 21:05:50 -0200 | [diff] [blame] | 1824 | |
| 1825 | pr_debug("Looking at the vmlinux_path (%d entries long)\n", |
Arnaldo Carvalho de Melo | 5ad90e4 | 2010-05-26 13:26:02 -0300 | [diff] [blame] | 1826 | vmlinux_path__nr_entries + 1); |
| 1827 | |
| 1828 | filename = dso__build_id_filename(self, NULL, 0); |
| 1829 | if (filename != NULL) { |
| 1830 | err = dso__load_vmlinux(self, map, filename, filter); |
| 1831 | if (err > 0) { |
| 1832 | dso__set_long_name(self, filename); |
| 1833 | goto out; |
| 1834 | } |
| 1835 | free(filename); |
| 1836 | } |
Arnaldo Carvalho de Melo | a19afe4 | 2010-01-27 21:05:50 -0200 | [diff] [blame] | 1837 | |
| 1838 | for (i = 0; i < vmlinux_path__nr_entries; ++i) { |
Arnaldo Carvalho de Melo | 9de89fe | 2010-02-03 16:52:00 -0200 | [diff] [blame] | 1839 | err = dso__load_vmlinux(self, map, vmlinux_path[i], filter); |
Arnaldo Carvalho de Melo | a19afe4 | 2010-01-27 21:05:50 -0200 | [diff] [blame] | 1840 | if (err > 0) { |
Arnaldo Carvalho de Melo | a19afe4 | 2010-01-27 21:05:50 -0200 | [diff] [blame] | 1841 | dso__set_long_name(self, strdup(vmlinux_path[i])); |
| 1842 | break; |
| 1843 | } |
| 1844 | } |
Arnaldo Carvalho de Melo | 5ad90e4 | 2010-05-26 13:26:02 -0300 | [diff] [blame] | 1845 | out: |
Arnaldo Carvalho de Melo | a19afe4 | 2010-01-27 21:05:50 -0200 | [diff] [blame] | 1846 | return err; |
| 1847 | } |
| 1848 | |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 1849 | 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] | 1850 | symbol_filter_t filter) |
Arnaldo Carvalho de Melo | a827c87 | 2009-05-28 14:55:19 -0300 | [diff] [blame] | 1851 | { |
Arnaldo Carvalho de Melo | cc612d8 | 2009-11-23 16:39:10 -0200 | [diff] [blame] | 1852 | int err; |
Arnaldo Carvalho de Melo | 9e20144 | 2010-01-14 18:30:06 -0200 | [diff] [blame] | 1853 | const char *kallsyms_filename = NULL; |
| 1854 | char *kallsyms_allocated_filename = NULL; |
Arnaldo Carvalho de Melo | dc8d6ab | 2010-01-19 10:36:14 -0200 | [diff] [blame] | 1855 | /* |
David Ahern | b226a5a7 | 2010-12-07 19:39:46 -0700 | [diff] [blame] | 1856 | * Step 1: if the user specified a kallsyms or vmlinux filename, use |
| 1857 | * it and only it, reporting errors to the user if it cannot be used. |
Arnaldo Carvalho de Melo | dc8d6ab | 2010-01-19 10:36:14 -0200 | [diff] [blame] | 1858 | * |
| 1859 | * For instance, try to analyse an ARM perf.data file _without_ a |
| 1860 | * build-id, or if the user specifies the wrong path to the right |
| 1861 | * vmlinux file, obviously we can't fallback to another vmlinux (a |
| 1862 | * x86_86 one, on the machine where analysis is being performed, say), |
| 1863 | * or worse, /proc/kallsyms. |
| 1864 | * |
| 1865 | * If the specified file _has_ a build-id and there is a build-id |
| 1866 | * section in the perf.data file, we will still do the expected |
| 1867 | * validation in dso__load_vmlinux and will bail out if they don't |
| 1868 | * match. |
| 1869 | */ |
David Ahern | b226a5a7 | 2010-12-07 19:39:46 -0700 | [diff] [blame] | 1870 | if (symbol_conf.kallsyms_name != NULL) { |
| 1871 | kallsyms_filename = symbol_conf.kallsyms_name; |
| 1872 | goto do_kallsyms; |
| 1873 | } |
| 1874 | |
Arnaldo Carvalho de Melo | dc8d6ab | 2010-01-19 10:36:14 -0200 | [diff] [blame] | 1875 | if (symbol_conf.vmlinux_name != NULL) { |
Arnaldo Carvalho de Melo | 9de89fe | 2010-02-03 16:52:00 -0200 | [diff] [blame] | 1876 | err = dso__load_vmlinux(self, map, |
Arnaldo Carvalho de Melo | dc8d6ab | 2010-01-19 10:36:14 -0200 | [diff] [blame] | 1877 | symbol_conf.vmlinux_name, filter); |
Arnaldo Carvalho de Melo | e7dadc0 | 2010-06-03 18:35:55 -0300 | [diff] [blame] | 1878 | if (err > 0) { |
| 1879 | dso__set_long_name(self, |
| 1880 | strdup(symbol_conf.vmlinux_name)); |
| 1881 | goto out_fixup; |
| 1882 | } |
| 1883 | return err; |
Arnaldo Carvalho de Melo | dc8d6ab | 2010-01-19 10:36:14 -0200 | [diff] [blame] | 1884 | } |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1885 | |
Arnaldo Carvalho de Melo | cc612d8 | 2009-11-23 16:39:10 -0200 | [diff] [blame] | 1886 | if (vmlinux_path != NULL) { |
Arnaldo Carvalho de Melo | 9de89fe | 2010-02-03 16:52:00 -0200 | [diff] [blame] | 1887 | err = dso__load_vmlinux_path(self, map, filter); |
Arnaldo Carvalho de Melo | a19afe4 | 2010-01-27 21:05:50 -0200 | [diff] [blame] | 1888 | if (err > 0) |
| 1889 | goto out_fixup; |
Arnaldo Carvalho de Melo | cc612d8 | 2009-11-23 16:39:10 -0200 | [diff] [blame] | 1890 | } |
| 1891 | |
David Ahern | ec5761e | 2010-12-09 13:27:07 -0700 | [diff] [blame^] | 1892 | /* do not try local files if a symfs was given */ |
| 1893 | if (symbol_conf.symfs[0] != 0) |
| 1894 | return -1; |
| 1895 | |
Arnaldo Carvalho de Melo | b7cece76 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 1896 | /* |
| 1897 | * Say the kernel DSO was created when processing the build-id header table, |
| 1898 | * we have a build-id, so check if it is the same as the running kernel, |
| 1899 | * using it if it is. |
| 1900 | */ |
| 1901 | if (self->has_build_id) { |
| 1902 | u8 kallsyms_build_id[BUILD_ID_SIZE]; |
Arnaldo Carvalho de Melo | 9e20144 | 2010-01-14 18:30:06 -0200 | [diff] [blame] | 1903 | char sbuild_id[BUILD_ID_SIZE * 2 + 1]; |
Arnaldo Carvalho de Melo | b7cece76 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 1904 | |
| 1905 | 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] | 1906 | sizeof(kallsyms_build_id)) == 0) { |
Arnaldo Carvalho de Melo | 9e20144 | 2010-01-14 18:30:06 -0200 | [diff] [blame] | 1907 | if (dso__build_id_equal(self, kallsyms_build_id)) { |
| 1908 | kallsyms_filename = "/proc/kallsyms"; |
Arnaldo Carvalho de Melo | 8d0591f6 | 2010-01-14 18:30:05 -0200 | [diff] [blame] | 1909 | goto do_kallsyms; |
Arnaldo Carvalho de Melo | 9e20144 | 2010-01-14 18:30:06 -0200 | [diff] [blame] | 1910 | } |
Arnaldo Carvalho de Melo | 8d0591f6 | 2010-01-14 18:30:05 -0200 | [diff] [blame] | 1911 | } |
Arnaldo Carvalho de Melo | dc8d6ab | 2010-01-19 10:36:14 -0200 | [diff] [blame] | 1912 | /* |
| 1913 | * Now look if we have it on the build-id cache in |
| 1914 | * $HOME/.debug/[kernel.kallsyms]. |
| 1915 | */ |
Arnaldo Carvalho de Melo | 9e20144 | 2010-01-14 18:30:06 -0200 | [diff] [blame] | 1916 | build_id__sprintf(self->build_id, sizeof(self->build_id), |
| 1917 | sbuild_id); |
| 1918 | |
| 1919 | if (asprintf(&kallsyms_allocated_filename, |
| 1920 | "%s/.debug/[kernel.kallsyms]/%s", |
Arnaldo Carvalho de Melo | 3846df2 | 2010-02-22 16:15:39 -0300 | [diff] [blame] | 1921 | getenv("HOME"), sbuild_id) == -1) { |
| 1922 | pr_err("Not enough memory for kallsyms file lookup\n"); |
Arnaldo Carvalho de Melo | 8d0591f6 | 2010-01-14 18:30:05 -0200 | [diff] [blame] | 1923 | return -1; |
Arnaldo Carvalho de Melo | 3846df2 | 2010-02-22 16:15:39 -0300 | [diff] [blame] | 1924 | } |
Arnaldo Carvalho de Melo | 8d0591f6 | 2010-01-14 18:30:05 -0200 | [diff] [blame] | 1925 | |
Arnaldo Carvalho de Melo | 19fc2de | 2010-01-22 14:35:02 -0200 | [diff] [blame] | 1926 | kallsyms_filename = kallsyms_allocated_filename; |
| 1927 | |
Arnaldo Carvalho de Melo | dc8d6ab | 2010-01-19 10:36:14 -0200 | [diff] [blame] | 1928 | if (access(kallsyms_filename, F_OK)) { |
Arnaldo Carvalho de Melo | 3846df2 | 2010-02-22 16:15:39 -0300 | [diff] [blame] | 1929 | pr_err("No kallsyms or vmlinux with build-id %s " |
| 1930 | "was found\n", sbuild_id); |
Arnaldo Carvalho de Melo | dc8d6ab | 2010-01-19 10:36:14 -0200 | [diff] [blame] | 1931 | free(kallsyms_allocated_filename); |
| 1932 | return -1; |
| 1933 | } |
Arnaldo Carvalho de Melo | dc8d6ab | 2010-01-19 10:36:14 -0200 | [diff] [blame] | 1934 | } else { |
| 1935 | /* |
| 1936 | * Last resort, if we don't have a build-id and couldn't find |
| 1937 | * any vmlinux file, try the running kernel kallsyms table. |
| 1938 | */ |
| 1939 | kallsyms_filename = "/proc/kallsyms"; |
Arnaldo Carvalho de Melo | ef6ae72 | 2009-11-20 20:51:29 -0200 | [diff] [blame] | 1940 | } |
Arnaldo Carvalho de Melo | a827c87 | 2009-05-28 14:55:19 -0300 | [diff] [blame] | 1941 | |
Arnaldo Carvalho de Melo | dc8d6ab | 2010-01-19 10:36:14 -0200 | [diff] [blame] | 1942 | do_kallsyms: |
Arnaldo Carvalho de Melo | 9de89fe | 2010-02-03 16:52:00 -0200 | [diff] [blame] | 1943 | err = dso__load_kallsyms(self, kallsyms_filename, map, filter); |
Arnaldo Carvalho de Melo | 3846df2 | 2010-02-22 16:15:39 -0300 | [diff] [blame] | 1944 | if (err > 0) |
| 1945 | pr_debug("Using %s for symbols\n", kallsyms_filename); |
Arnaldo Carvalho de Melo | dc8d6ab | 2010-01-19 10:36:14 -0200 | [diff] [blame] | 1946 | free(kallsyms_allocated_filename); |
| 1947 | |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1948 | if (err > 0) { |
Arnaldo Carvalho de Melo | cc612d8 | 2009-11-23 16:39:10 -0200 | [diff] [blame] | 1949 | out_fixup: |
Arnaldo Carvalho de Melo | e1c7c6a | 2010-01-22 14:35:01 -0200 | [diff] [blame] | 1950 | if (kallsyms_filename != NULL) |
Arnaldo Carvalho de Melo | dc8d6ab | 2010-01-19 10:36:14 -0200 | [diff] [blame] | 1951 | dso__set_long_name(self, strdup("[kernel.kallsyms]")); |
Arnaldo Carvalho de Melo | 6a4694a | 2009-11-27 16:29:17 -0200 | [diff] [blame] | 1952 | map__fixup_start(map); |
| 1953 | map__fixup_end(map); |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 1954 | } |
Arnaldo Carvalho de Melo | 94cb9e3 | 2009-08-06 14:43:17 -0300 | [diff] [blame] | 1955 | |
Arnaldo Carvalho de Melo | a827c87 | 2009-05-28 14:55:19 -0300 | [diff] [blame] | 1956 | return err; |
| 1957 | } |
| 1958 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1959 | static int dso__load_guest_kernel_sym(struct dso *self, struct map *map, |
| 1960 | symbol_filter_t filter) |
| 1961 | { |
| 1962 | int err; |
| 1963 | const char *kallsyms_filename = NULL; |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 1964 | struct machine *machine; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1965 | char path[PATH_MAX]; |
| 1966 | |
| 1967 | if (!map->groups) { |
| 1968 | pr_debug("Guest kernel map hasn't the point to groups\n"); |
| 1969 | return -1; |
| 1970 | } |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 1971 | machine = map->groups->machine; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1972 | |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 1973 | if (machine__is_default_guest(machine)) { |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1974 | /* |
| 1975 | * if the user specified a vmlinux filename, use it and only |
| 1976 | * it, reporting errors to the user if it cannot be used. |
| 1977 | * Or use file guest_kallsyms inputted by user on commandline |
| 1978 | */ |
| 1979 | if (symbol_conf.default_guest_vmlinux_name != NULL) { |
| 1980 | err = dso__load_vmlinux(self, map, |
| 1981 | symbol_conf.default_guest_vmlinux_name, filter); |
| 1982 | goto out_try_fixup; |
| 1983 | } |
| 1984 | |
| 1985 | kallsyms_filename = symbol_conf.default_guest_kallsyms; |
| 1986 | if (!kallsyms_filename) |
| 1987 | return -1; |
| 1988 | } else { |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 1989 | sprintf(path, "%s/proc/kallsyms", machine->root_dir); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1990 | kallsyms_filename = path; |
| 1991 | } |
| 1992 | |
| 1993 | err = dso__load_kallsyms(self, kallsyms_filename, map, filter); |
| 1994 | if (err > 0) |
| 1995 | pr_debug("Using %s for symbols\n", kallsyms_filename); |
| 1996 | |
| 1997 | out_try_fixup: |
| 1998 | if (err > 0) { |
| 1999 | if (kallsyms_filename != NULL) { |
Arnaldo Carvalho de Melo | 48ea8f5 | 2010-04-27 21:19:05 -0300 | [diff] [blame] | 2000 | machine__mmap_name(machine, path, sizeof(path)); |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 2001 | dso__set_long_name(self, strdup(path)); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 2002 | } |
| 2003 | map__fixup_start(map); |
| 2004 | map__fixup_end(map); |
| 2005 | } |
| 2006 | |
| 2007 | return err; |
| 2008 | } |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 2009 | |
Arnaldo Carvalho de Melo | b0da954 | 2009-11-27 16:29:14 -0200 | [diff] [blame] | 2010 | static void dsos__add(struct list_head *head, struct dso *dso) |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 2011 | { |
Arnaldo Carvalho de Melo | b0da954 | 2009-11-27 16:29:14 -0200 | [diff] [blame] | 2012 | list_add_tail(&dso->node, head); |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 2013 | } |
| 2014 | |
Arnaldo Carvalho de Melo | b0da954 | 2009-11-27 16:29:14 -0200 | [diff] [blame] | 2015 | static struct dso *dsos__find(struct list_head *head, const char *name) |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 2016 | { |
| 2017 | struct dso *pos; |
| 2018 | |
Arnaldo Carvalho de Melo | b0da954 | 2009-11-27 16:29:14 -0200 | [diff] [blame] | 2019 | list_for_each_entry(pos, head, node) |
Arnaldo Carvalho de Melo | cf4e5b0 | 2010-01-14 23:45:27 -0200 | [diff] [blame] | 2020 | if (strcmp(pos->long_name, name) == 0) |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 2021 | return pos; |
| 2022 | return NULL; |
| 2023 | } |
| 2024 | |
Arnaldo Carvalho de Melo | a89e5abe | 2010-01-07 19:59:39 -0200 | [diff] [blame] | 2025 | struct dso *__dsos__findnew(struct list_head *head, const char *name) |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 2026 | { |
Arnaldo Carvalho de Melo | a89e5abe | 2010-01-07 19:59:39 -0200 | [diff] [blame] | 2027 | struct dso *dso = dsos__find(head, name); |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 2028 | |
Arnaldo Carvalho de Melo | e420499 | 2009-10-20 14:25:40 -0200 | [diff] [blame] | 2029 | if (!dso) { |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 2030 | dso = dso__new(name); |
Arnaldo Carvalho de Melo | cfc10d3 | 2009-11-17 15:40:53 -0200 | [diff] [blame] | 2031 | if (dso != NULL) { |
Arnaldo Carvalho de Melo | a89e5abe | 2010-01-07 19:59:39 -0200 | [diff] [blame] | 2032 | dsos__add(head, dso); |
Arnaldo Carvalho de Melo | cfc10d3 | 2009-11-17 15:40:53 -0200 | [diff] [blame] | 2033 | dso__set_basename(dso); |
| 2034 | } |
Arnaldo Carvalho de Melo | 66bd842 | 2009-10-28 21:51:21 -0200 | [diff] [blame] | 2035 | } |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 2036 | |
| 2037 | return dso; |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 2038 | } |
| 2039 | |
Arnaldo Carvalho de Melo | 1f626bc | 2010-05-09 19:57:08 -0300 | [diff] [blame] | 2040 | size_t __dsos__fprintf(struct list_head *head, FILE *fp) |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 2041 | { |
| 2042 | struct dso *pos; |
Arnaldo Carvalho de Melo | cbf6968 | 2010-04-27 21:22:44 -0300 | [diff] [blame] | 2043 | size_t ret = 0; |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 2044 | |
Arnaldo Carvalho de Melo | 95011c6 | 2009-11-27 16:29:20 -0200 | [diff] [blame] | 2045 | list_for_each_entry(pos, head, node) { |
| 2046 | int i; |
| 2047 | for (i = 0; i < MAP__NR_TYPES; ++i) |
Arnaldo Carvalho de Melo | cbf6968 | 2010-04-27 21:22:44 -0300 | [diff] [blame] | 2048 | ret += dso__fprintf(pos, i, fp); |
Arnaldo Carvalho de Melo | 95011c6 | 2009-11-27 16:29:20 -0200 | [diff] [blame] | 2049 | } |
Arnaldo Carvalho de Melo | cbf6968 | 2010-04-27 21:22:44 -0300 | [diff] [blame] | 2050 | |
| 2051 | return ret; |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 2052 | } |
| 2053 | |
Arnaldo Carvalho de Melo | cbf6968 | 2010-04-27 21:22:44 -0300 | [diff] [blame] | 2054 | size_t machines__fprintf_dsos(struct rb_root *self, FILE *fp) |
Arnaldo Carvalho de Melo | b0da954 | 2009-11-27 16:29:14 -0200 | [diff] [blame] | 2055 | { |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 2056 | struct rb_node *nd; |
Arnaldo Carvalho de Melo | cbf6968 | 2010-04-27 21:22:44 -0300 | [diff] [blame] | 2057 | size_t ret = 0; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 2058 | |
Arnaldo Carvalho de Melo | cbf6968 | 2010-04-27 21:22:44 -0300 | [diff] [blame] | 2059 | for (nd = rb_first(self); nd; nd = rb_next(nd)) { |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 2060 | struct machine *pos = rb_entry(nd, struct machine, rb_node); |
Arnaldo Carvalho de Melo | cbf6968 | 2010-04-27 21:22:44 -0300 | [diff] [blame] | 2061 | ret += __dsos__fprintf(&pos->kernel_dsos, fp); |
| 2062 | ret += __dsos__fprintf(&pos->user_dsos, fp); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 2063 | } |
Arnaldo Carvalho de Melo | cbf6968 | 2010-04-27 21:22:44 -0300 | [diff] [blame] | 2064 | |
| 2065 | return ret; |
Arnaldo Carvalho de Melo | b0da954 | 2009-11-27 16:29:14 -0200 | [diff] [blame] | 2066 | } |
| 2067 | |
Arnaldo Carvalho de Melo | 88d3d9b7 | 2010-01-14 23:45:30 -0200 | [diff] [blame] | 2068 | static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp, |
| 2069 | bool with_hits) |
Arnaldo Carvalho de Melo | 9e03eb2 | 2009-11-16 16:32:44 -0200 | [diff] [blame] | 2070 | { |
| 2071 | struct dso *pos; |
| 2072 | size_t ret = 0; |
| 2073 | |
Arnaldo Carvalho de Melo | b0da954 | 2009-11-27 16:29:14 -0200 | [diff] [blame] | 2074 | list_for_each_entry(pos, head, node) { |
Arnaldo Carvalho de Melo | 88d3d9b7 | 2010-01-14 23:45:30 -0200 | [diff] [blame] | 2075 | if (with_hits && !pos->hit) |
| 2076 | continue; |
Arnaldo Carvalho de Melo | 9e03eb2 | 2009-11-16 16:32:44 -0200 | [diff] [blame] | 2077 | ret += dso__fprintf_buildid(pos, fp); |
Arnaldo Carvalho de Melo | 1124ba7 | 2009-11-16 21:45:25 -0200 | [diff] [blame] | 2078 | ret += fprintf(fp, " %s\n", pos->long_name); |
Arnaldo Carvalho de Melo | 9e03eb2 | 2009-11-16 16:32:44 -0200 | [diff] [blame] | 2079 | } |
| 2080 | return ret; |
| 2081 | } |
| 2082 | |
Arnaldo Carvalho de Melo | f869097 | 2010-05-19 13:41:23 -0300 | [diff] [blame] | 2083 | size_t machine__fprintf_dsos_buildid(struct machine *self, FILE *fp, bool with_hits) |
| 2084 | { |
| 2085 | return __dsos__fprintf_buildid(&self->kernel_dsos, fp, with_hits) + |
| 2086 | __dsos__fprintf_buildid(&self->user_dsos, fp, with_hits); |
| 2087 | } |
| 2088 | |
Arnaldo Carvalho de Melo | cbf6968 | 2010-04-27 21:22:44 -0300 | [diff] [blame] | 2089 | size_t machines__fprintf_dsos_buildid(struct rb_root *self, FILE *fp, bool with_hits) |
Arnaldo Carvalho de Melo | b0da954 | 2009-11-27 16:29:14 -0200 | [diff] [blame] | 2090 | { |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 2091 | struct rb_node *nd; |
| 2092 | size_t ret = 0; |
| 2093 | |
Arnaldo Carvalho de Melo | cbf6968 | 2010-04-27 21:22:44 -0300 | [diff] [blame] | 2094 | for (nd = rb_first(self); nd; nd = rb_next(nd)) { |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 2095 | struct machine *pos = rb_entry(nd, struct machine, rb_node); |
Arnaldo Carvalho de Melo | f869097 | 2010-05-19 13:41:23 -0300 | [diff] [blame] | 2096 | ret += machine__fprintf_dsos_buildid(pos, fp, with_hits); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 2097 | } |
| 2098 | return ret; |
Arnaldo Carvalho de Melo | b0da954 | 2009-11-27 16:29:14 -0200 | [diff] [blame] | 2099 | } |
| 2100 | |
Arnaldo Carvalho de Melo | fd1d908 | 2010-01-27 21:05:51 -0200 | [diff] [blame] | 2101 | struct dso *dso__new_kernel(const char *name) |
| 2102 | { |
| 2103 | struct dso *self = dso__new(name ?: "[kernel.kallsyms]"); |
| 2104 | |
| 2105 | if (self != NULL) { |
Arnaldo Carvalho de Melo | b63be8d | 2010-03-15 15:03:50 -0300 | [diff] [blame] | 2106 | dso__set_short_name(self, "[kernel]"); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 2107 | self->kernel = DSO_TYPE_KERNEL; |
Arnaldo Carvalho de Melo | fd1d908 | 2010-01-27 21:05:51 -0200 | [diff] [blame] | 2108 | } |
| 2109 | |
| 2110 | return self; |
| 2111 | } |
| 2112 | |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 2113 | static struct dso *dso__new_guest_kernel(struct machine *machine, |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 2114 | const char *name) |
Arnaldo Carvalho de Melo | fd1d908 | 2010-01-27 21:05:51 -0200 | [diff] [blame] | 2115 | { |
Arnaldo Carvalho de Melo | 48ea8f5 | 2010-04-27 21:19:05 -0300 | [diff] [blame] | 2116 | char bf[PATH_MAX]; |
| 2117 | struct dso *self = dso__new(name ?: machine__mmap_name(machine, bf, sizeof(bf))); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 2118 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 2119 | if (self != NULL) { |
| 2120 | dso__set_short_name(self, "[guest.kernel]"); |
| 2121 | self->kernel = DSO_TYPE_GUEST_KERNEL; |
| 2122 | } |
| 2123 | |
| 2124 | return self; |
| 2125 | } |
| 2126 | |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 2127 | void dso__read_running_kernel_build_id(struct dso *self, struct machine *machine) |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 2128 | { |
| 2129 | char path[PATH_MAX]; |
| 2130 | |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 2131 | if (machine__is_default_guest(machine)) |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 2132 | return; |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 2133 | sprintf(path, "%s/sys/kernel/notes", machine->root_dir); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 2134 | if (sysfs__read_build_id(path, self->build_id, |
Arnaldo Carvalho de Melo | fd1d908 | 2010-01-27 21:05:51 -0200 | [diff] [blame] | 2135 | sizeof(self->build_id)) == 0) |
| 2136 | self->has_build_id = true; |
| 2137 | } |
| 2138 | |
Arnaldo Carvalho de Melo | 5c0541d | 2010-04-29 15:25:23 -0300 | [diff] [blame] | 2139 | static struct dso *machine__create_kernel(struct machine *self) |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 2140 | { |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 2141 | const char *vmlinux_name = NULL; |
| 2142 | struct dso *kernel; |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 2143 | |
Arnaldo Carvalho de Melo | 5c0541d | 2010-04-29 15:25:23 -0300 | [diff] [blame] | 2144 | if (machine__is_host(self)) { |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 2145 | vmlinux_name = symbol_conf.vmlinux_name; |
| 2146 | kernel = dso__new_kernel(vmlinux_name); |
| 2147 | } else { |
Arnaldo Carvalho de Melo | 5c0541d | 2010-04-29 15:25:23 -0300 | [diff] [blame] | 2148 | if (machine__is_default_guest(self)) |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 2149 | vmlinux_name = symbol_conf.default_guest_vmlinux_name; |
Arnaldo Carvalho de Melo | 5c0541d | 2010-04-29 15:25:23 -0300 | [diff] [blame] | 2150 | kernel = dso__new_guest_kernel(self, vmlinux_name); |
Arnaldo Carvalho de Melo | 8d92c02 | 2010-02-03 16:52:02 -0200 | [diff] [blame] | 2151 | } |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 2152 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 2153 | if (kernel != NULL) { |
Arnaldo Carvalho de Melo | 5c0541d | 2010-04-29 15:25:23 -0300 | [diff] [blame] | 2154 | dso__read_running_kernel_build_id(kernel, self); |
| 2155 | dsos__add(&self->kernel_dsos, kernel); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 2156 | } |
Arnaldo Carvalho de Melo | f1dfa0b | 2009-12-11 14:50:39 -0200 | [diff] [blame] | 2157 | return kernel; |
Arnaldo Carvalho de Melo | f1dfa0b | 2009-12-11 14:50:39 -0200 | [diff] [blame] | 2158 | } |
| 2159 | |
Ming Lei | d214afb | 2010-11-25 19:27:25 +0800 | [diff] [blame] | 2160 | struct process_args { |
| 2161 | u64 start; |
| 2162 | }; |
| 2163 | |
| 2164 | static int symbol__in_kernel(void *arg, const char *name, |
| 2165 | char type __used, u64 start) |
| 2166 | { |
| 2167 | struct process_args *args = arg; |
| 2168 | |
| 2169 | if (strchr(name, '[')) |
| 2170 | return 0; |
| 2171 | |
| 2172 | args->start = start; |
| 2173 | return 1; |
| 2174 | } |
| 2175 | |
| 2176 | /* Figure out the start address of kernel map from /proc/kallsyms */ |
| 2177 | static u64 machine__get_kernel_start_addr(struct machine *machine) |
| 2178 | { |
| 2179 | const char *filename; |
| 2180 | char path[PATH_MAX]; |
| 2181 | struct process_args args; |
| 2182 | |
| 2183 | if (machine__is_host(machine)) { |
| 2184 | filename = "/proc/kallsyms"; |
| 2185 | } else { |
| 2186 | if (machine__is_default_guest(machine)) |
| 2187 | filename = (char *)symbol_conf.default_guest_kallsyms; |
| 2188 | else { |
| 2189 | sprintf(path, "%s/proc/kallsyms", machine->root_dir); |
| 2190 | filename = path; |
| 2191 | } |
| 2192 | } |
| 2193 | |
| 2194 | if (kallsyms__parse(filename, &args, symbol__in_kernel) <= 0) |
| 2195 | return 0; |
| 2196 | |
| 2197 | return args.start; |
| 2198 | } |
| 2199 | |
Arnaldo Carvalho de Melo | d28c622 | 2010-04-27 21:20:43 -0300 | [diff] [blame] | 2200 | int __machine__create_kernel_maps(struct machine *self, struct dso *kernel) |
Arnaldo Carvalho de Melo | f1dfa0b | 2009-12-11 14:50:39 -0200 | [diff] [blame] | 2201 | { |
Arnaldo Carvalho de Melo | de17648 | 2010-01-04 16:19:29 -0200 | [diff] [blame] | 2202 | enum map_type type; |
Ming Lei | d214afb | 2010-11-25 19:27:25 +0800 | [diff] [blame] | 2203 | u64 start = machine__get_kernel_start_addr(self); |
Arnaldo Carvalho de Melo | f1dfa0b | 2009-12-11 14:50:39 -0200 | [diff] [blame] | 2204 | |
Arnaldo Carvalho de Melo | de17648 | 2010-01-04 16:19:29 -0200 | [diff] [blame] | 2205 | for (type = 0; type < MAP__NR_TYPES; ++type) { |
Arnaldo Carvalho de Melo | 9de89fe | 2010-02-03 16:52:00 -0200 | [diff] [blame] | 2206 | struct kmap *kmap; |
| 2207 | |
Ming Lei | d214afb | 2010-11-25 19:27:25 +0800 | [diff] [blame] | 2208 | self->vmlinux_maps[type] = map__new2(start, kernel, type); |
Arnaldo Carvalho de Melo | d28c622 | 2010-04-27 21:20:43 -0300 | [diff] [blame] | 2209 | if (self->vmlinux_maps[type] == NULL) |
Arnaldo Carvalho de Melo | de17648 | 2010-01-04 16:19:29 -0200 | [diff] [blame] | 2210 | return -1; |
Arnaldo Carvalho de Melo | f1dfa0b | 2009-12-11 14:50:39 -0200 | [diff] [blame] | 2211 | |
Arnaldo Carvalho de Melo | d28c622 | 2010-04-27 21:20:43 -0300 | [diff] [blame] | 2212 | self->vmlinux_maps[type]->map_ip = |
| 2213 | self->vmlinux_maps[type]->unmap_ip = identity__map_ip; |
Arnaldo Carvalho de Melo | 9de89fe | 2010-02-03 16:52:00 -0200 | [diff] [blame] | 2214 | |
Arnaldo Carvalho de Melo | d28c622 | 2010-04-27 21:20:43 -0300 | [diff] [blame] | 2215 | kmap = map__kmap(self->vmlinux_maps[type]); |
| 2216 | kmap->kmaps = &self->kmaps; |
| 2217 | map_groups__insert(&self->kmaps, self->vmlinux_maps[type]); |
Arnaldo Carvalho de Melo | f1dfa0b | 2009-12-11 14:50:39 -0200 | [diff] [blame] | 2218 | } |
| 2219 | |
Arnaldo Carvalho de Melo | f1dfa0b | 2009-12-11 14:50:39 -0200 | [diff] [blame] | 2220 | return 0; |
Arnaldo Carvalho de Melo | 2446042 | 2009-11-18 20:20:53 -0200 | [diff] [blame] | 2221 | } |
| 2222 | |
Arnaldo Carvalho de Melo | 076c6e45 | 2010-08-02 18:18:28 -0300 | [diff] [blame] | 2223 | void machine__destroy_kernel_maps(struct machine *self) |
| 2224 | { |
| 2225 | enum map_type type; |
| 2226 | |
| 2227 | for (type = 0; type < MAP__NR_TYPES; ++type) { |
| 2228 | struct kmap *kmap; |
| 2229 | |
| 2230 | if (self->vmlinux_maps[type] == NULL) |
| 2231 | continue; |
| 2232 | |
| 2233 | kmap = map__kmap(self->vmlinux_maps[type]); |
| 2234 | map_groups__remove(&self->kmaps, self->vmlinux_maps[type]); |
| 2235 | if (kmap->ref_reloc_sym) { |
| 2236 | /* |
| 2237 | * ref_reloc_sym is shared among all maps, so free just |
| 2238 | * on one of them. |
| 2239 | */ |
| 2240 | if (type == MAP__FUNCTION) { |
| 2241 | free((char *)kmap->ref_reloc_sym->name); |
| 2242 | kmap->ref_reloc_sym->name = NULL; |
| 2243 | free(kmap->ref_reloc_sym); |
| 2244 | } |
| 2245 | kmap->ref_reloc_sym = NULL; |
| 2246 | } |
| 2247 | |
| 2248 | map__delete(self->vmlinux_maps[type]); |
| 2249 | self->vmlinux_maps[type] = NULL; |
| 2250 | } |
| 2251 | } |
| 2252 | |
Arnaldo Carvalho de Melo | 5c0541d | 2010-04-29 15:25:23 -0300 | [diff] [blame] | 2253 | int machine__create_kernel_maps(struct machine *self) |
| 2254 | { |
| 2255 | struct dso *kernel = machine__create_kernel(self); |
| 2256 | |
| 2257 | if (kernel == NULL || |
| 2258 | __machine__create_kernel_maps(self, kernel) < 0) |
| 2259 | return -1; |
| 2260 | |
| 2261 | if (symbol_conf.use_modules && machine__create_modules(self) < 0) |
| 2262 | pr_debug("Problems creating module maps, continuing anyway...\n"); |
| 2263 | /* |
| 2264 | * Now that we have all the maps created, just set the ->end of them: |
| 2265 | */ |
| 2266 | map_groups__fixup_end(&self->kmaps); |
| 2267 | return 0; |
| 2268 | } |
| 2269 | |
Arnaldo Carvalho de Melo | cc612d8 | 2009-11-23 16:39:10 -0200 | [diff] [blame] | 2270 | static void vmlinux_path__exit(void) |
Arnaldo Carvalho de Melo | 2446042 | 2009-11-18 20:20:53 -0200 | [diff] [blame] | 2271 | { |
Arnaldo Carvalho de Melo | cc612d8 | 2009-11-23 16:39:10 -0200 | [diff] [blame] | 2272 | while (--vmlinux_path__nr_entries >= 0) { |
| 2273 | free(vmlinux_path[vmlinux_path__nr_entries]); |
| 2274 | vmlinux_path[vmlinux_path__nr_entries] = NULL; |
| 2275 | } |
| 2276 | |
| 2277 | free(vmlinux_path); |
| 2278 | vmlinux_path = NULL; |
| 2279 | } |
| 2280 | |
| 2281 | static int vmlinux_path__init(void) |
| 2282 | { |
| 2283 | struct utsname uts; |
| 2284 | char bf[PATH_MAX]; |
| 2285 | |
Arnaldo Carvalho de Melo | cc612d8 | 2009-11-23 16:39:10 -0200 | [diff] [blame] | 2286 | vmlinux_path = malloc(sizeof(char *) * 5); |
| 2287 | if (vmlinux_path == NULL) |
| 2288 | return -1; |
| 2289 | |
| 2290 | vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux"); |
| 2291 | if (vmlinux_path[vmlinux_path__nr_entries] == NULL) |
| 2292 | goto out_fail; |
| 2293 | ++vmlinux_path__nr_entries; |
| 2294 | vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux"); |
| 2295 | if (vmlinux_path[vmlinux_path__nr_entries] == NULL) |
| 2296 | goto out_fail; |
| 2297 | ++vmlinux_path__nr_entries; |
David Ahern | ec5761e | 2010-12-09 13:27:07 -0700 | [diff] [blame^] | 2298 | |
| 2299 | /* only try running kernel version if no symfs was given */ |
| 2300 | if (symbol_conf.symfs[0] != 0) |
| 2301 | return 0; |
| 2302 | |
| 2303 | if (uname(&uts) < 0) |
| 2304 | return -1; |
| 2305 | |
Arnaldo Carvalho de Melo | cc612d8 | 2009-11-23 16:39:10 -0200 | [diff] [blame] | 2306 | snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release); |
| 2307 | vmlinux_path[vmlinux_path__nr_entries] = strdup(bf); |
| 2308 | if (vmlinux_path[vmlinux_path__nr_entries] == NULL) |
| 2309 | goto out_fail; |
| 2310 | ++vmlinux_path__nr_entries; |
| 2311 | snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release); |
| 2312 | vmlinux_path[vmlinux_path__nr_entries] = strdup(bf); |
| 2313 | if (vmlinux_path[vmlinux_path__nr_entries] == NULL) |
| 2314 | goto out_fail; |
| 2315 | ++vmlinux_path__nr_entries; |
| 2316 | snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux", |
| 2317 | uts.release); |
| 2318 | vmlinux_path[vmlinux_path__nr_entries] = strdup(bf); |
| 2319 | if (vmlinux_path[vmlinux_path__nr_entries] == NULL) |
| 2320 | goto out_fail; |
| 2321 | ++vmlinux_path__nr_entries; |
| 2322 | |
| 2323 | return 0; |
| 2324 | |
| 2325 | out_fail: |
| 2326 | vmlinux_path__exit(); |
| 2327 | return -1; |
| 2328 | } |
| 2329 | |
Arnaldo Carvalho de Melo | 5ad90e4 | 2010-05-26 13:26:02 -0300 | [diff] [blame] | 2330 | size_t machine__fprintf_vmlinux_path(struct machine *self, FILE *fp) |
Arnaldo Carvalho de Melo | b0a9ab6 | 2010-03-15 11:46:58 -0300 | [diff] [blame] | 2331 | { |
| 2332 | int i; |
| 2333 | size_t printed = 0; |
Arnaldo Carvalho de Melo | 5ad90e4 | 2010-05-26 13:26:02 -0300 | [diff] [blame] | 2334 | struct dso *kdso = self->vmlinux_maps[MAP__FUNCTION]->dso; |
| 2335 | |
| 2336 | if (kdso->has_build_id) { |
| 2337 | char filename[PATH_MAX]; |
| 2338 | if (dso__build_id_filename(kdso, filename, sizeof(filename))) |
| 2339 | printed += fprintf(fp, "[0] %s\n", filename); |
| 2340 | } |
Arnaldo Carvalho de Melo | b0a9ab6 | 2010-03-15 11:46:58 -0300 | [diff] [blame] | 2341 | |
| 2342 | for (i = 0; i < vmlinux_path__nr_entries; ++i) |
Arnaldo Carvalho de Melo | 5ad90e4 | 2010-05-26 13:26:02 -0300 | [diff] [blame] | 2343 | printed += fprintf(fp, "[%d] %s\n", |
| 2344 | i + kdso->has_build_id, vmlinux_path[i]); |
Arnaldo Carvalho de Melo | b0a9ab6 | 2010-03-15 11:46:58 -0300 | [diff] [blame] | 2345 | |
| 2346 | return printed; |
| 2347 | } |
| 2348 | |
Arnaldo Carvalho de Melo | 655000e | 2009-12-15 20:04:40 -0200 | [diff] [blame] | 2349 | static int setup_list(struct strlist **list, const char *list_str, |
| 2350 | const char *list_name) |
| 2351 | { |
| 2352 | if (list_str == NULL) |
| 2353 | return 0; |
| 2354 | |
| 2355 | *list = strlist__new(true, list_str); |
| 2356 | if (!*list) { |
| 2357 | pr_err("problems parsing %s list\n", list_name); |
| 2358 | return -1; |
| 2359 | } |
| 2360 | return 0; |
| 2361 | } |
| 2362 | |
Arnaldo Carvalho de Melo | 75be6cf | 2009-12-15 20:04:39 -0200 | [diff] [blame] | 2363 | int symbol__init(void) |
Arnaldo Carvalho de Melo | cc612d8 | 2009-11-23 16:39:10 -0200 | [diff] [blame] | 2364 | { |
David Ahern | ec5761e | 2010-12-09 13:27:07 -0700 | [diff] [blame^] | 2365 | const char *symfs; |
| 2366 | |
Jovi Zhang | 85e00b5 | 2010-09-09 13:30:59 -0300 | [diff] [blame] | 2367 | if (symbol_conf.initialized) |
| 2368 | return 0; |
| 2369 | |
Arnaldo Carvalho de Melo | 95011c6 | 2009-11-27 16:29:20 -0200 | [diff] [blame] | 2370 | elf_version(EV_CURRENT); |
Arnaldo Carvalho de Melo | 75be6cf | 2009-12-15 20:04:39 -0200 | [diff] [blame] | 2371 | if (symbol_conf.sort_by_name) |
| 2372 | symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) - |
| 2373 | sizeof(struct symbol)); |
Arnaldo Carvalho de Melo | b32d133 | 2009-11-24 12:05:15 -0200 | [diff] [blame] | 2374 | |
Arnaldo Carvalho de Melo | 75be6cf | 2009-12-15 20:04:39 -0200 | [diff] [blame] | 2375 | if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0) |
Arnaldo Carvalho de Melo | cc612d8 | 2009-11-23 16:39:10 -0200 | [diff] [blame] | 2376 | return -1; |
| 2377 | |
Arnaldo Carvalho de Melo | c410a33 | 2009-12-15 20:04:41 -0200 | [diff] [blame] | 2378 | if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') { |
| 2379 | pr_err("'.' is the only non valid --field-separator argument\n"); |
| 2380 | return -1; |
| 2381 | } |
| 2382 | |
Arnaldo Carvalho de Melo | 655000e | 2009-12-15 20:04:40 -0200 | [diff] [blame] | 2383 | if (setup_list(&symbol_conf.dso_list, |
| 2384 | symbol_conf.dso_list_str, "dso") < 0) |
| 2385 | return -1; |
| 2386 | |
| 2387 | if (setup_list(&symbol_conf.comm_list, |
| 2388 | symbol_conf.comm_list_str, "comm") < 0) |
| 2389 | goto out_free_dso_list; |
| 2390 | |
| 2391 | if (setup_list(&symbol_conf.sym_list, |
| 2392 | symbol_conf.sym_list_str, "symbol") < 0) |
| 2393 | goto out_free_comm_list; |
| 2394 | |
David Ahern | ec5761e | 2010-12-09 13:27:07 -0700 | [diff] [blame^] | 2395 | /* |
| 2396 | * A path to symbols of "/" is identical to "" |
| 2397 | * reset here for simplicity. |
| 2398 | */ |
| 2399 | symfs = realpath(symbol_conf.symfs, NULL); |
| 2400 | if (symfs == NULL) |
| 2401 | symfs = symbol_conf.symfs; |
| 2402 | if (strcmp(symfs, "/") == 0) |
| 2403 | symbol_conf.symfs = ""; |
| 2404 | if (symfs != symbol_conf.symfs) |
| 2405 | free((void *)symfs); |
| 2406 | |
Jovi Zhang | 85e00b5 | 2010-09-09 13:30:59 -0300 | [diff] [blame] | 2407 | symbol_conf.initialized = true; |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 2408 | return 0; |
Arnaldo Carvalho de Melo | 655000e | 2009-12-15 20:04:40 -0200 | [diff] [blame] | 2409 | |
| 2410 | out_free_dso_list: |
| 2411 | strlist__delete(symbol_conf.dso_list); |
| 2412 | out_free_comm_list: |
| 2413 | strlist__delete(symbol_conf.comm_list); |
| 2414 | return -1; |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 2415 | } |
Arnaldo Carvalho de Melo | cc612d8 | 2009-11-23 16:39:10 -0200 | [diff] [blame] | 2416 | |
Arnaldo Carvalho de Melo | d65a458 | 2010-07-30 18:31:28 -0300 | [diff] [blame] | 2417 | void symbol__exit(void) |
| 2418 | { |
Jovi Zhang | 85e00b5 | 2010-09-09 13:30:59 -0300 | [diff] [blame] | 2419 | if (!symbol_conf.initialized) |
| 2420 | return; |
Arnaldo Carvalho de Melo | d65a458 | 2010-07-30 18:31:28 -0300 | [diff] [blame] | 2421 | strlist__delete(symbol_conf.sym_list); |
| 2422 | strlist__delete(symbol_conf.dso_list); |
| 2423 | strlist__delete(symbol_conf.comm_list); |
| 2424 | vmlinux_path__exit(); |
| 2425 | symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL; |
Jovi Zhang | 85e00b5 | 2010-09-09 13:30:59 -0300 | [diff] [blame] | 2426 | symbol_conf.initialized = false; |
Arnaldo Carvalho de Melo | d65a458 | 2010-07-30 18:31:28 -0300 | [diff] [blame] | 2427 | } |
| 2428 | |
Arnaldo Carvalho de Melo | d28c622 | 2010-04-27 21:20:43 -0300 | [diff] [blame] | 2429 | int machines__create_kernel_maps(struct rb_root *self, pid_t pid) |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 2430 | { |
Arnaldo Carvalho de Melo | d28c622 | 2010-04-27 21:20:43 -0300 | [diff] [blame] | 2431 | struct machine *machine = machines__findnew(self, pid); |
Arnaldo Carvalho de Melo | 9de89fe | 2010-02-03 16:52:00 -0200 | [diff] [blame] | 2432 | |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 2433 | if (machine == NULL) |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 2434 | return -1; |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 2435 | |
Arnaldo Carvalho de Melo | 5c0541d | 2010-04-29 15:25:23 -0300 | [diff] [blame] | 2436 | return machine__create_kernel_maps(machine); |
Frederic Weisbecker | cd84c2a | 2009-08-12 10:03:49 +0200 | [diff] [blame] | 2437 | } |
Arnaldo Carvalho de Melo | 5aab621 | 2010-03-25 19:59:00 -0300 | [diff] [blame] | 2438 | |
| 2439 | static int hex(char ch) |
| 2440 | { |
| 2441 | if ((ch >= '0') && (ch <= '9')) |
| 2442 | return ch - '0'; |
| 2443 | if ((ch >= 'a') && (ch <= 'f')) |
| 2444 | return ch - 'a' + 10; |
| 2445 | if ((ch >= 'A') && (ch <= 'F')) |
| 2446 | return ch - 'A' + 10; |
| 2447 | return -1; |
| 2448 | } |
| 2449 | |
| 2450 | /* |
| 2451 | * While we find nice hex chars, build a long_val. |
| 2452 | * Return number of chars processed. |
| 2453 | */ |
| 2454 | int hex2u64(const char *ptr, u64 *long_val) |
| 2455 | { |
| 2456 | const char *p = ptr; |
| 2457 | *long_val = 0; |
| 2458 | |
| 2459 | while (*p) { |
| 2460 | const int hex_val = hex(*p); |
| 2461 | |
| 2462 | if (hex_val < 0) |
| 2463 | break; |
| 2464 | |
| 2465 | *long_val = (*long_val << 4) | hex_val; |
| 2466 | p++; |
| 2467 | } |
| 2468 | |
| 2469 | return p - ptr; |
| 2470 | } |
| 2471 | |
| 2472 | char *strxfrchar(char *s, char from, char to) |
| 2473 | { |
| 2474 | char *p = s; |
| 2475 | |
| 2476 | while ((p = strchr(p, from)) != NULL) |
| 2477 | *p++ = to; |
| 2478 | |
| 2479 | return s; |
| 2480 | } |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 2481 | |
Arnaldo Carvalho de Melo | d28c622 | 2010-04-27 21:20:43 -0300 | [diff] [blame] | 2482 | int machines__create_guest_kernel_maps(struct rb_root *self) |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 2483 | { |
| 2484 | int ret = 0; |
| 2485 | struct dirent **namelist = NULL; |
| 2486 | int i, items = 0; |
| 2487 | char path[PATH_MAX]; |
| 2488 | pid_t pid; |
| 2489 | |
| 2490 | if (symbol_conf.default_guest_vmlinux_name || |
| 2491 | symbol_conf.default_guest_modules || |
| 2492 | symbol_conf.default_guest_kallsyms) { |
Arnaldo Carvalho de Melo | d28c622 | 2010-04-27 21:20:43 -0300 | [diff] [blame] | 2493 | machines__create_kernel_maps(self, DEFAULT_GUEST_KERNEL_ID); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 2494 | } |
| 2495 | |
| 2496 | if (symbol_conf.guestmount) { |
| 2497 | items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL); |
| 2498 | if (items <= 0) |
| 2499 | return -ENOENT; |
| 2500 | for (i = 0; i < items; i++) { |
| 2501 | if (!isdigit(namelist[i]->d_name[0])) { |
| 2502 | /* Filter out . and .. */ |
| 2503 | continue; |
| 2504 | } |
| 2505 | pid = atoi(namelist[i]->d_name); |
| 2506 | sprintf(path, "%s/%s/proc/kallsyms", |
| 2507 | symbol_conf.guestmount, |
| 2508 | namelist[i]->d_name); |
| 2509 | ret = access(path, R_OK); |
| 2510 | if (ret) { |
| 2511 | pr_debug("Can't access file %s\n", path); |
| 2512 | goto failure; |
| 2513 | } |
Arnaldo Carvalho de Melo | d28c622 | 2010-04-27 21:20:43 -0300 | [diff] [blame] | 2514 | machines__create_kernel_maps(self, pid); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 2515 | } |
| 2516 | failure: |
| 2517 | free(namelist); |
| 2518 | } |
| 2519 | |
| 2520 | return ret; |
| 2521 | } |
Arnaldo Carvalho de Melo | 5c0541d | 2010-04-29 15:25:23 -0300 | [diff] [blame] | 2522 | |
Arnaldo Carvalho de Melo | 076c6e45 | 2010-08-02 18:18:28 -0300 | [diff] [blame] | 2523 | void machines__destroy_guest_kernel_maps(struct rb_root *self) |
| 2524 | { |
| 2525 | struct rb_node *next = rb_first(self); |
| 2526 | |
| 2527 | while (next) { |
| 2528 | struct machine *pos = rb_entry(next, struct machine, rb_node); |
| 2529 | |
| 2530 | next = rb_next(&pos->rb_node); |
| 2531 | rb_erase(&pos->rb_node, self); |
| 2532 | machine__delete(pos); |
| 2533 | } |
| 2534 | } |
| 2535 | |
Arnaldo Carvalho de Melo | 5c0541d | 2010-04-29 15:25:23 -0300 | [diff] [blame] | 2536 | int machine__load_kallsyms(struct machine *self, const char *filename, |
| 2537 | enum map_type type, symbol_filter_t filter) |
| 2538 | { |
| 2539 | struct map *map = self->vmlinux_maps[type]; |
| 2540 | int ret = dso__load_kallsyms(map->dso, filename, map, filter); |
| 2541 | |
| 2542 | if (ret > 0) { |
| 2543 | dso__set_loaded(map->dso, type); |
| 2544 | /* |
| 2545 | * Since /proc/kallsyms will have multiple sessions for the |
| 2546 | * kernel, with modules between them, fixup the end of all |
| 2547 | * sections. |
| 2548 | */ |
| 2549 | __map_groups__fixup_end(&self->kmaps, type); |
| 2550 | } |
| 2551 | |
| 2552 | return ret; |
| 2553 | } |
| 2554 | |
| 2555 | int machine__load_vmlinux_path(struct machine *self, enum map_type type, |
| 2556 | symbol_filter_t filter) |
| 2557 | { |
| 2558 | struct map *map = self->vmlinux_maps[type]; |
| 2559 | int ret = dso__load_vmlinux_path(map->dso, map, filter); |
| 2560 | |
| 2561 | if (ret > 0) { |
| 2562 | dso__set_loaded(map->dso, type); |
| 2563 | map__reloc_vmlinux(map); |
| 2564 | } |
| 2565 | |
| 2566 | return ret; |
| 2567 | } |