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