Commit 3f5a42722b9e78a434d5a4ee5e607dc33c69ac80

Authored by Anton Blanchard
Committed by Arnaldo Carvalho de Melo
1 parent adb0918463

perf symbols: /proc/kallsyms does not sort module symbols

kallsyms__parse assumes that /proc/kallsyms is sorted and sets the end
of the previous symbol to the start of the current one.

Unfortunately module symbols are not sorted, eg:

ffffffffa0081f30 t e1000_clean_rx_irq   [e1000e]
ffffffffa00817a0 t e1000_alloc_rx_buffers       [e1000e]

Some symbols end up with a negative length and others have a length
larger than they should. This results in confusing perf output.

We already have a function to fixup the end of zero length symbols so
use that instead.

Cc: Eric B Munson <emunson@mgebm.net>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Link: http://lkml.kernel.org/r/20110824065242.969681349@samba.org
Signed-off-by: Anton Blanchard <anton@samba.org>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

Showing 1 changed file with 11 additions and 22 deletions Inline Diff

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