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