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