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