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