aboutsummaryrefslogtreecommitdiffstats
path: root/tools/perf/util/symbol.c
diff options
context:
space:
mode:
Diffstat (limited to 'tools/perf/util/symbol.c')
-rw-r--r--tools/perf/util/symbol.c585
1 files changed, 455 insertions, 130 deletions
diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c
index c458c4a371d1..aaa51ba147df 100644
--- a/tools/perf/util/symbol.c
+++ b/tools/perf/util/symbol.c
@@ -1,13 +1,20 @@
1#include "util.h" 1#define _GNU_SOURCE
2#include "../perf.h" 2#include <ctype.h>
3#include "sort.h" 3#include <dirent.h>
4#include "string.h" 4#include <errno.h>
5#include <libgen.h>
6#include <stdlib.h>
7#include <stdio.h>
8#include <string.h>
9#include <sys/types.h>
10#include <sys/stat.h>
11#include <sys/param.h>
12#include <fcntl.h>
13#include <unistd.h>
14#include "build-id.h"
5#include "symbol.h" 15#include "symbol.h"
6#include "thread.h" 16#include "strlist.h"
7 17
8#include "debug.h"
9
10#include <asm/bug.h>
11#include <libelf.h> 18#include <libelf.h>
12#include <gelf.h> 19#include <gelf.h>
13#include <elf.h> 20#include <elf.h>
@@ -18,22 +25,12 @@
18#define NT_GNU_BUILD_ID 3 25#define NT_GNU_BUILD_ID 3
19#endif 26#endif
20 27
21enum dso_origin {
22 DSO__ORIG_KERNEL = 0,
23 DSO__ORIG_JAVA_JIT,
24 DSO__ORIG_BUILD_ID_CACHE,
25 DSO__ORIG_FEDORA,
26 DSO__ORIG_UBUNTU,
27 DSO__ORIG_BUILDID,
28 DSO__ORIG_DSO,
29 DSO__ORIG_KMODULE,
30 DSO__ORIG_NOT_FOUND,
31};
32
33static void dsos__add(struct list_head *head, struct dso *dso); 28static void dsos__add(struct list_head *head, struct dso *dso);
34static struct map *map__new2(u64 start, struct dso *dso, enum map_type type); 29static struct map *map__new2(u64 start, struct dso *dso, enum map_type type);
35static int dso__load_kernel_sym(struct dso *self, struct map *map, 30static int dso__load_kernel_sym(struct dso *self, struct map *map,
36 symbol_filter_t filter); 31 symbol_filter_t filter);
32static int dso__load_guest_kernel_sym(struct dso *self, struct map *map,
33 symbol_filter_t filter);
37static int vmlinux_path__nr_entries; 34static int vmlinux_path__nr_entries;
38static char **vmlinux_path; 35static char **vmlinux_path;
39 36
@@ -126,16 +123,17 @@ static void map_groups__fixup_end(struct map_groups *self)
126static struct symbol *symbol__new(u64 start, u64 len, const char *name) 123static struct symbol *symbol__new(u64 start, u64 len, const char *name)
127{ 124{
128 size_t namelen = strlen(name) + 1; 125 size_t namelen = strlen(name) + 1;
129 struct symbol *self = zalloc(symbol_conf.priv_size + 126 struct symbol *self = calloc(1, (symbol_conf.priv_size +
130 sizeof(*self) + namelen); 127 sizeof(*self) + namelen));
131 if (self == NULL) 128 if (self == NULL)
132 return NULL; 129 return NULL;
133 130
134 if (symbol_conf.priv_size) 131 if (symbol_conf.priv_size)
135 self = ((void *)self) + symbol_conf.priv_size; 132 self = ((void *)self) + symbol_conf.priv_size;
136 133
137 self->start = start; 134 self->start = start;
138 self->end = len ? start + len - 1 : start; 135 self->end = len ? start + len - 1 : start;
136 self->namelen = namelen - 1;
139 137
140 pr_debug4("%s: %s %#Lx-%#Lx\n", __func__, name, start, self->end); 138 pr_debug4("%s: %s %#Lx-%#Lx\n", __func__, name, start, self->end);
141 139
@@ -178,7 +176,7 @@ static void dso__set_basename(struct dso *self)
178 176
179struct dso *dso__new(const char *name) 177struct dso *dso__new(const char *name)
180{ 178{
181 struct dso *self = zalloc(sizeof(*self) + strlen(name) + 1); 179 struct dso *self = calloc(1, sizeof(*self) + strlen(name) + 1);
182 180
183 if (self != NULL) { 181 if (self != NULL) {
184 int i; 182 int i;
@@ -192,6 +190,8 @@ struct dso *dso__new(const char *name)
192 self->loaded = 0; 190 self->loaded = 0;
193 self->sorted_by_name = 0; 191 self->sorted_by_name = 0;
194 self->has_build_id = 0; 192 self->has_build_id = 0;
193 self->kernel = DSO_TYPE_USER;
194 INIT_LIST_HEAD(&self->node);
195 } 195 }
196 196
197 return self; 197 return self;
@@ -408,12 +408,9 @@ int kallsyms__parse(const char *filename, void *arg,
408 char *symbol_name; 408 char *symbol_name;
409 409
410 line_len = getline(&line, &n, file); 410 line_len = getline(&line, &n, file);
411 if (line_len < 0) 411 if (line_len < 0 || !line)
412 break; 412 break;
413 413
414 if (!line)
415 goto out_failure;
416
417 line[--line_len] = '\0'; /* \n */ 414 line[--line_len] = '\0'; /* \n */
418 415
419 len = hex2u64(line, &start); 416 len = hex2u64(line, &start);
@@ -465,6 +462,7 @@ static int map__process_kallsym_symbol(void *arg, const char *name,
465 * map__split_kallsyms, when we have split the maps per module 462 * map__split_kallsyms, when we have split the maps per module
466 */ 463 */
467 symbols__insert(root, sym); 464 symbols__insert(root, sym);
465
468 return 0; 466 return 0;
469} 467}
470 468
@@ -489,6 +487,7 @@ static int dso__split_kallsyms(struct dso *self, struct map *map,
489 symbol_filter_t filter) 487 symbol_filter_t filter)
490{ 488{
491 struct map_groups *kmaps = map__kmap(map)->kmaps; 489 struct map_groups *kmaps = map__kmap(map)->kmaps;
490 struct machine *machine = kmaps->machine;
492 struct map *curr_map = map; 491 struct map *curr_map = map;
493 struct symbol *pos; 492 struct symbol *pos;
494 int count = 0; 493 int count = 0;
@@ -510,15 +509,33 @@ static int dso__split_kallsyms(struct dso *self, struct map *map,
510 *module++ = '\0'; 509 *module++ = '\0';
511 510
512 if (strcmp(curr_map->dso->short_name, module)) { 511 if (strcmp(curr_map->dso->short_name, module)) {
513 curr_map = map_groups__find_by_name(kmaps, map->type, module); 512 if (curr_map != map &&
513 self->kernel == DSO_TYPE_GUEST_KERNEL &&
514 machine__is_default_guest(machine)) {
515 /*
516 * We assume all symbols of a module are
517 * continuous in * kallsyms, so curr_map
518 * points to a module and all its
519 * symbols are in its kmap. Mark it as
520 * loaded.
521 */
522 dso__set_loaded(curr_map->dso,
523 curr_map->type);
524 }
525
526 curr_map = map_groups__find_by_name(kmaps,
527 map->type, module);
514 if (curr_map == NULL) { 528 if (curr_map == NULL) {
515 pr_debug("/proc/{kallsyms,modules} " 529 pr_debug("%s/proc/{kallsyms,modules} "
516 "inconsistency while looking " 530 "inconsistency while looking "
517 "for \"%s\" module!\n", module); 531 "for \"%s\" module!\n",
518 return -1; 532 machine->root_dir, module);
533 curr_map = map;
534 goto discard_symbol;
519 } 535 }
520 536
521 if (curr_map->dso->loaded) 537 if (curr_map->dso->loaded &&
538 !machine__is_default_guest(machine))
522 goto discard_symbol; 539 goto discard_symbol;
523 } 540 }
524 /* 541 /*
@@ -531,13 +548,21 @@ static int dso__split_kallsyms(struct dso *self, struct map *map,
531 char dso_name[PATH_MAX]; 548 char dso_name[PATH_MAX];
532 struct dso *dso; 549 struct dso *dso;
533 550
534 snprintf(dso_name, sizeof(dso_name), "[kernel].%d", 551 if (self->kernel == DSO_TYPE_GUEST_KERNEL)
535 kernel_range++); 552 snprintf(dso_name, sizeof(dso_name),
553 "[guest.kernel].%d",
554 kernel_range++);
555 else
556 snprintf(dso_name, sizeof(dso_name),
557 "[kernel].%d",
558 kernel_range++);
536 559
537 dso = dso__new(dso_name); 560 dso = dso__new(dso_name);
538 if (dso == NULL) 561 if (dso == NULL)
539 return -1; 562 return -1;
540 563
564 dso->kernel = self->kernel;
565
541 curr_map = map__new2(pos->start, dso, map->type); 566 curr_map = map__new2(pos->start, dso, map->type);
542 if (curr_map == NULL) { 567 if (curr_map == NULL) {
543 dso__delete(dso); 568 dso__delete(dso);
@@ -561,6 +586,12 @@ discard_symbol: rb_erase(&pos->rb_node, root);
561 } 586 }
562 } 587 }
563 588
589 if (curr_map != map &&
590 self->kernel == DSO_TYPE_GUEST_KERNEL &&
591 machine__is_default_guest(kmaps->machine)) {
592 dso__set_loaded(curr_map->dso, curr_map->type);
593 }
594
564 return count; 595 return count;
565} 596}
566 597
@@ -571,7 +602,10 @@ int dso__load_kallsyms(struct dso *self, const char *filename,
571 return -1; 602 return -1;
572 603
573 symbols__fixup_end(&self->symbols[map->type]); 604 symbols__fixup_end(&self->symbols[map->type]);
574 self->origin = DSO__ORIG_KERNEL; 605 if (self->kernel == DSO_TYPE_GUEST_KERNEL)
606 self->origin = DSO__ORIG_GUEST_KERNEL;
607 else
608 self->origin = DSO__ORIG_KERNEL;
575 609
576 return dso__split_kallsyms(self, map, filter); 610 return dso__split_kallsyms(self, map, filter);
577} 611}
@@ -870,8 +904,8 @@ out_close:
870 if (err == 0) 904 if (err == 0)
871 return nr; 905 return nr;
872out: 906out:
873 pr_warning("%s: problems reading %s PLT info.\n", 907 pr_debug("%s: problems reading %s PLT info.\n",
874 __func__, self->long_name); 908 __func__, self->long_name);
875 return 0; 909 return 0;
876} 910}
877 911
@@ -958,7 +992,7 @@ static int dso__load_sym(struct dso *self, struct map *map, const char *name,
958 nr_syms = shdr.sh_size / shdr.sh_entsize; 992 nr_syms = shdr.sh_size / shdr.sh_entsize;
959 993
960 memset(&sym, 0, sizeof(sym)); 994 memset(&sym, 0, sizeof(sym));
961 if (!self->kernel) { 995 if (self->kernel == DSO_TYPE_USER) {
962 self->adjust_symbols = (ehdr.e_type == ET_EXEC || 996 self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
963 elf_section_by_name(elf, &ehdr, &shdr, 997 elf_section_by_name(elf, &ehdr, &shdr,
964 ".gnu.prelink_undo", 998 ".gnu.prelink_undo",
@@ -990,7 +1024,7 @@ static int dso__load_sym(struct dso *self, struct map *map, const char *name,
990 1024
991 section_name = elf_sec__name(&shdr, secstrs); 1025 section_name = elf_sec__name(&shdr, secstrs);
992 1026
993 if (self->kernel || kmodule) { 1027 if (self->kernel != DSO_TYPE_USER || kmodule) {
994 char dso_name[PATH_MAX]; 1028 char dso_name[PATH_MAX];
995 1029
996 if (strcmp(section_name, 1030 if (strcmp(section_name,
@@ -1017,6 +1051,7 @@ static int dso__load_sym(struct dso *self, struct map *map, const char *name,
1017 curr_dso = dso__new(dso_name); 1051 curr_dso = dso__new(dso_name);
1018 if (curr_dso == NULL) 1052 if (curr_dso == NULL)
1019 goto out_elf_end; 1053 goto out_elf_end;
1054 curr_dso->kernel = self->kernel;
1020 curr_map = map__new2(start, curr_dso, 1055 curr_map = map__new2(start, curr_dso,
1021 map->type); 1056 map->type);
1022 if (curr_map == NULL) { 1057 if (curr_map == NULL) {
@@ -1025,9 +1060,9 @@ static int dso__load_sym(struct dso *self, struct map *map, const char *name,
1025 } 1060 }
1026 curr_map->map_ip = identity__map_ip; 1061 curr_map->map_ip = identity__map_ip;
1027 curr_map->unmap_ip = identity__map_ip; 1062 curr_map->unmap_ip = identity__map_ip;
1028 curr_dso->origin = DSO__ORIG_KERNEL; 1063 curr_dso->origin = self->origin;
1029 map_groups__insert(kmap->kmaps, curr_map); 1064 map_groups__insert(kmap->kmaps, curr_map);
1030 dsos__add(&dsos__kernel, curr_dso); 1065 dsos__add(&self->node, curr_dso);
1031 dso__set_loaded(curr_dso, map->type); 1066 dso__set_loaded(curr_dso, map->type);
1032 } else 1067 } else
1033 curr_dso = curr_map->dso; 1068 curr_dso = curr_map->dso;
@@ -1089,7 +1124,7 @@ static bool dso__build_id_equal(const struct dso *self, u8 *build_id)
1089 return memcmp(self->build_id, build_id, sizeof(self->build_id)) == 0; 1124 return memcmp(self->build_id, build_id, sizeof(self->build_id)) == 0;
1090} 1125}
1091 1126
1092static bool __dsos__read_build_ids(struct list_head *head, bool with_hits) 1127bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
1093{ 1128{
1094 bool have_build_id = false; 1129 bool have_build_id = false;
1095 struct dso *pos; 1130 struct dso *pos;
@@ -1097,6 +1132,10 @@ static bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
1097 list_for_each_entry(pos, head, node) { 1132 list_for_each_entry(pos, head, node) {
1098 if (with_hits && !pos->hit) 1133 if (with_hits && !pos->hit)
1099 continue; 1134 continue;
1135 if (pos->has_build_id) {
1136 have_build_id = true;
1137 continue;
1138 }
1100 if (filename__read_build_id(pos->long_name, pos->build_id, 1139 if (filename__read_build_id(pos->long_name, pos->build_id,
1101 sizeof(pos->build_id)) > 0) { 1140 sizeof(pos->build_id)) > 0) {
1102 have_build_id = true; 1141 have_build_id = true;
@@ -1107,13 +1146,6 @@ static bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
1107 return have_build_id; 1146 return have_build_id;
1108} 1147}
1109 1148
1110bool dsos__read_build_ids(bool with_hits)
1111{
1112 bool kbuildids = __dsos__read_build_ids(&dsos__kernel, with_hits),
1113 ubuildids = __dsos__read_build_ids(&dsos__user, with_hits);
1114 return kbuildids || ubuildids;
1115}
1116
1117/* 1149/*
1118 * Align offset to 4 bytes as needed for note name and descriptor data. 1150 * Align offset to 4 bytes as needed for note name and descriptor data.
1119 */ 1151 */
@@ -1248,6 +1280,8 @@ char dso__symtab_origin(const struct dso *self)
1248 [DSO__ORIG_BUILDID] = 'b', 1280 [DSO__ORIG_BUILDID] = 'b',
1249 [DSO__ORIG_DSO] = 'd', 1281 [DSO__ORIG_DSO] = 'd',
1250 [DSO__ORIG_KMODULE] = 'K', 1282 [DSO__ORIG_KMODULE] = 'K',
1283 [DSO__ORIG_GUEST_KERNEL] = 'g',
1284 [DSO__ORIG_GUEST_KMODULE] = 'G',
1251 }; 1285 };
1252 1286
1253 if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND) 1287 if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
@@ -1260,14 +1294,22 @@ int dso__load(struct dso *self, struct map *map, symbol_filter_t filter)
1260 int size = PATH_MAX; 1294 int size = PATH_MAX;
1261 char *name; 1295 char *name;
1262 u8 build_id[BUILD_ID_SIZE]; 1296 u8 build_id[BUILD_ID_SIZE];
1263 char build_id_hex[BUILD_ID_SIZE * 2 + 1];
1264 int ret = -1; 1297 int ret = -1;
1265 int fd; 1298 int fd;
1299 struct machine *machine;
1300 const char *root_dir;
1266 1301
1267 dso__set_loaded(self, map->type); 1302 dso__set_loaded(self, map->type);
1268 1303
1269 if (self->kernel) 1304 if (self->kernel == DSO_TYPE_KERNEL)
1270 return dso__load_kernel_sym(self, map, filter); 1305 return dso__load_kernel_sym(self, map, filter);
1306 else if (self->kernel == DSO_TYPE_GUEST_KERNEL)
1307 return dso__load_guest_kernel_sym(self, map, filter);
1308
1309 if (map->groups && map->groups->machine)
1310 machine = map->groups->machine;
1311 else
1312 machine = NULL;
1271 1313
1272 name = malloc(size); 1314 name = malloc(size);
1273 if (!name) 1315 if (!name)
@@ -1283,15 +1325,8 @@ int dso__load(struct dso *self, struct map *map, symbol_filter_t filter)
1283 } 1325 }
1284 1326
1285 self->origin = DSO__ORIG_BUILD_ID_CACHE; 1327 self->origin = DSO__ORIG_BUILD_ID_CACHE;
1286 1328 if (dso__build_id_filename(self, name, size) != NULL)
1287 if (self->has_build_id) {
1288 build_id__sprintf(self->build_id, sizeof(self->build_id),
1289 build_id_hex);
1290 snprintf(name, size, "%s/%s/.build-id/%.2s/%s",
1291 getenv("HOME"), DEBUG_CACHE_DIR,
1292 build_id_hex, build_id_hex + 2);
1293 goto open_file; 1329 goto open_file;
1294 }
1295more: 1330more:
1296 do { 1331 do {
1297 self->origin++; 1332 self->origin++;
@@ -1307,6 +1342,7 @@ more:
1307 case DSO__ORIG_BUILDID: 1342 case DSO__ORIG_BUILDID:
1308 if (filename__read_build_id(self->long_name, build_id, 1343 if (filename__read_build_id(self->long_name, build_id,
1309 sizeof(build_id))) { 1344 sizeof(build_id))) {
1345 char build_id_hex[BUILD_ID_SIZE * 2 + 1];
1310 build_id__sprintf(build_id, sizeof(build_id), 1346 build_id__sprintf(build_id, sizeof(build_id),
1311 build_id_hex); 1347 build_id_hex);
1312 snprintf(name, size, 1348 snprintf(name, size,
@@ -1321,6 +1357,13 @@ more:
1321 case DSO__ORIG_DSO: 1357 case DSO__ORIG_DSO:
1322 snprintf(name, size, "%s", self->long_name); 1358 snprintf(name, size, "%s", self->long_name);
1323 break; 1359 break;
1360 case DSO__ORIG_GUEST_KMODULE:
1361 if (map->groups && map->groups->machine)
1362 root_dir = map->groups->machine->root_dir;
1363 else
1364 root_dir = "";
1365 snprintf(name, size, "%s%s", root_dir, self->long_name);
1366 break;
1324 1367
1325 default: 1368 default:
1326 goto out; 1369 goto out;
@@ -1374,7 +1417,8 @@ struct map *map_groups__find_by_name(struct map_groups *self,
1374 return NULL; 1417 return NULL;
1375} 1418}
1376 1419
1377static int dso__kernel_module_get_build_id(struct dso *self) 1420static int dso__kernel_module_get_build_id(struct dso *self,
1421 const char *root_dir)
1378{ 1422{
1379 char filename[PATH_MAX]; 1423 char filename[PATH_MAX];
1380 /* 1424 /*
@@ -1384,8 +1428,8 @@ static int dso__kernel_module_get_build_id(struct dso *self)
1384 const char *name = self->short_name + 1; 1428 const char *name = self->short_name + 1;
1385 1429
1386 snprintf(filename, sizeof(filename), 1430 snprintf(filename, sizeof(filename),
1387 "/sys/module/%.*s/notes/.note.gnu.build-id", 1431 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1388 (int)strlen(name - 1), name); 1432 root_dir, (int)strlen(name) - 1, name);
1389 1433
1390 if (sysfs__read_build_id(filename, self->build_id, 1434 if (sysfs__read_build_id(filename, self->build_id,
1391 sizeof(self->build_id)) == 0) 1435 sizeof(self->build_id)) == 0)
@@ -1394,26 +1438,33 @@ static int dso__kernel_module_get_build_id(struct dso *self)
1394 return 0; 1438 return 0;
1395} 1439}
1396 1440
1397static int map_groups__set_modules_path_dir(struct map_groups *self, char *dirname) 1441static int map_groups__set_modules_path_dir(struct map_groups *self,
1442 const char *dir_name)
1398{ 1443{
1399 struct dirent *dent; 1444 struct dirent *dent;
1400 DIR *dir = opendir(dirname); 1445 DIR *dir = opendir(dir_name);
1401 1446
1402 if (!dir) { 1447 if (!dir) {
1403 pr_debug("%s: cannot open %s dir\n", __func__, dirname); 1448 pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
1404 return -1; 1449 return -1;
1405 } 1450 }
1406 1451
1407 while ((dent = readdir(dir)) != NULL) { 1452 while ((dent = readdir(dir)) != NULL) {
1408 char path[PATH_MAX]; 1453 char path[PATH_MAX];
1454 struct stat st;
1455
1456 /*sshfs might return bad dent->d_type, so we have to stat*/
1457 sprintf(path, "%s/%s", dir_name, dent->d_name);
1458 if (stat(path, &st))
1459 continue;
1409 1460
1410 if (dent->d_type == DT_DIR) { 1461 if (S_ISDIR(st.st_mode)) {
1411 if (!strcmp(dent->d_name, ".") || 1462 if (!strcmp(dent->d_name, ".") ||
1412 !strcmp(dent->d_name, "..")) 1463 !strcmp(dent->d_name, ".."))
1413 continue; 1464 continue;
1414 1465
1415 snprintf(path, sizeof(path), "%s/%s", 1466 snprintf(path, sizeof(path), "%s/%s",
1416 dirname, dent->d_name); 1467 dir_name, dent->d_name);
1417 if (map_groups__set_modules_path_dir(self, path) < 0) 1468 if (map_groups__set_modules_path_dir(self, path) < 0)
1418 goto failure; 1469 goto failure;
1419 } else { 1470 } else {
@@ -1433,13 +1484,13 @@ static int map_groups__set_modules_path_dir(struct map_groups *self, char *dirna
1433 continue; 1484 continue;
1434 1485
1435 snprintf(path, sizeof(path), "%s/%s", 1486 snprintf(path, sizeof(path), "%s/%s",
1436 dirname, dent->d_name); 1487 dir_name, dent->d_name);
1437 1488
1438 long_name = strdup(path); 1489 long_name = strdup(path);
1439 if (long_name == NULL) 1490 if (long_name == NULL)
1440 goto failure; 1491 goto failure;
1441 dso__set_long_name(map->dso, long_name); 1492 dso__set_long_name(map->dso, long_name);
1442 dso__kernel_module_get_build_id(map->dso); 1493 dso__kernel_module_get_build_id(map->dso, "");
1443 } 1494 }
1444 } 1495 }
1445 1496
@@ -1449,18 +1500,47 @@ failure:
1449 return -1; 1500 return -1;
1450} 1501}
1451 1502
1452static int map_groups__set_modules_path(struct map_groups *self) 1503static char *get_kernel_version(const char *root_dir)
1453{ 1504{
1454 struct utsname uts; 1505 char version[PATH_MAX];
1506 FILE *file;
1507 char *name, *tmp;
1508 const char *prefix = "Linux version ";
1509
1510 sprintf(version, "%s/proc/version", root_dir);
1511 file = fopen(version, "r");
1512 if (!file)
1513 return NULL;
1514
1515 version[0] = '\0';
1516 tmp = fgets(version, sizeof(version), file);
1517 fclose(file);
1518
1519 name = strstr(version, prefix);
1520 if (!name)
1521 return NULL;
1522 name += strlen(prefix);
1523 tmp = strchr(name, ' ');
1524 if (tmp)
1525 *tmp = '\0';
1526
1527 return strdup(name);
1528}
1529
1530static int machine__set_modules_path(struct machine *self)
1531{
1532 char *version;
1455 char modules_path[PATH_MAX]; 1533 char modules_path[PATH_MAX];
1456 1534
1457 if (uname(&uts) < 0) 1535 version = get_kernel_version(self->root_dir);
1536 if (!version)
1458 return -1; 1537 return -1;
1459 1538
1460 snprintf(modules_path, sizeof(modules_path), "/lib/modules/%s/kernel", 1539 snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s/kernel",
1461 uts.release); 1540 self->root_dir, version);
1541 free(version);
1462 1542
1463 return map_groups__set_modules_path_dir(self, modules_path); 1543 return map_groups__set_modules_path_dir(&self->kmaps, modules_path);
1464} 1544}
1465 1545
1466/* 1546/*
@@ -1470,8 +1550,8 @@ static int map_groups__set_modules_path(struct map_groups *self)
1470 */ 1550 */
1471static struct map *map__new2(u64 start, struct dso *dso, enum map_type type) 1551static struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
1472{ 1552{
1473 struct map *self = zalloc(sizeof(*self) + 1553 struct map *self = calloc(1, (sizeof(*self) +
1474 (dso->kernel ? sizeof(struct kmap) : 0)); 1554 (dso->kernel ? sizeof(struct kmap) : 0)));
1475 if (self != NULL) { 1555 if (self != NULL) {
1476 /* 1556 /*
1477 * ->end will be filled after we load all the symbols 1557 * ->end will be filled after we load all the symbols
@@ -1482,11 +1562,11 @@ static struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
1482 return self; 1562 return self;
1483} 1563}
1484 1564
1485struct map *map_groups__new_module(struct map_groups *self, u64 start, 1565struct map *machine__new_module(struct machine *self, u64 start,
1486 const char *filename) 1566 const char *filename)
1487{ 1567{
1488 struct map *map; 1568 struct map *map;
1489 struct dso *dso = __dsos__findnew(&dsos__kernel, filename); 1569 struct dso *dso = __dsos__findnew(&self->kernel_dsos, filename);
1490 1570
1491 if (dso == NULL) 1571 if (dso == NULL)
1492 return NULL; 1572 return NULL;
@@ -1495,18 +1575,31 @@ struct map *map_groups__new_module(struct map_groups *self, u64 start,
1495 if (map == NULL) 1575 if (map == NULL)
1496 return NULL; 1576 return NULL;
1497 1577
1498 dso->origin = DSO__ORIG_KMODULE; 1578 if (machine__is_host(self))
1499 map_groups__insert(self, map); 1579 dso->origin = DSO__ORIG_KMODULE;
1580 else
1581 dso->origin = DSO__ORIG_GUEST_KMODULE;
1582 map_groups__insert(&self->kmaps, map);
1500 return map; 1583 return map;
1501} 1584}
1502 1585
1503static int map_groups__create_modules(struct map_groups *self) 1586static int machine__create_modules(struct machine *self)
1504{ 1587{
1505 char *line = NULL; 1588 char *line = NULL;
1506 size_t n; 1589 size_t n;
1507 FILE *file = fopen("/proc/modules", "r"); 1590 FILE *file;
1508 struct map *map; 1591 struct map *map;
1592 const char *modules;
1593 char path[PATH_MAX];
1594
1595 if (machine__is_default_guest(self))
1596 modules = symbol_conf.default_guest_modules;
1597 else {
1598 sprintf(path, "%s/proc/modules", self->root_dir);
1599 modules = path;
1600 }
1509 1601
1602 file = fopen(modules, "r");
1510 if (file == NULL) 1603 if (file == NULL)
1511 return -1; 1604 return -1;
1512 1605
@@ -1538,16 +1631,16 @@ static int map_groups__create_modules(struct map_groups *self)
1538 *sep = '\0'; 1631 *sep = '\0';
1539 1632
1540 snprintf(name, sizeof(name), "[%s]", line); 1633 snprintf(name, sizeof(name), "[%s]", line);
1541 map = map_groups__new_module(self, start, name); 1634 map = machine__new_module(self, start, name);
1542 if (map == NULL) 1635 if (map == NULL)
1543 goto out_delete_line; 1636 goto out_delete_line;
1544 dso__kernel_module_get_build_id(map->dso); 1637 dso__kernel_module_get_build_id(map->dso, self->root_dir);
1545 } 1638 }
1546 1639
1547 free(line); 1640 free(line);
1548 fclose(file); 1641 fclose(file);
1549 1642
1550 return map_groups__set_modules_path(self); 1643 return machine__set_modules_path(self);
1551 1644
1552out_delete_line: 1645out_delete_line:
1553 free(line); 1646 free(line);
@@ -1714,8 +1807,56 @@ out_fixup:
1714 return err; 1807 return err;
1715} 1808}
1716 1809
1717LIST_HEAD(dsos__user); 1810static int dso__load_guest_kernel_sym(struct dso *self, struct map *map,
1718LIST_HEAD(dsos__kernel); 1811 symbol_filter_t filter)
1812{
1813 int err;
1814 const char *kallsyms_filename = NULL;
1815 struct machine *machine;
1816 char path[PATH_MAX];
1817
1818 if (!map->groups) {
1819 pr_debug("Guest kernel map hasn't the point to groups\n");
1820 return -1;
1821 }
1822 machine = map->groups->machine;
1823
1824 if (machine__is_default_guest(machine)) {
1825 /*
1826 * if the user specified a vmlinux filename, use it and only
1827 * it, reporting errors to the user if it cannot be used.
1828 * Or use file guest_kallsyms inputted by user on commandline
1829 */
1830 if (symbol_conf.default_guest_vmlinux_name != NULL) {
1831 err = dso__load_vmlinux(self, map,
1832 symbol_conf.default_guest_vmlinux_name, filter);
1833 goto out_try_fixup;
1834 }
1835
1836 kallsyms_filename = symbol_conf.default_guest_kallsyms;
1837 if (!kallsyms_filename)
1838 return -1;
1839 } else {
1840 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
1841 kallsyms_filename = path;
1842 }
1843
1844 err = dso__load_kallsyms(self, kallsyms_filename, map, filter);
1845 if (err > 0)
1846 pr_debug("Using %s for symbols\n", kallsyms_filename);
1847
1848out_try_fixup:
1849 if (err > 0) {
1850 if (kallsyms_filename != NULL) {
1851 machine__mmap_name(machine, path, sizeof(path));
1852 dso__set_long_name(self, strdup(path));
1853 }
1854 map__fixup_start(map);
1855 map__fixup_end(map);
1856 }
1857
1858 return err;
1859}
1719 1860
1720static void dsos__add(struct list_head *head, struct dso *dso) 1861static void dsos__add(struct list_head *head, struct dso *dso)
1721{ 1862{
@@ -1747,21 +1888,32 @@ struct dso *__dsos__findnew(struct list_head *head, const char *name)
1747 return dso; 1888 return dso;
1748} 1889}
1749 1890
1750static void __dsos__fprintf(struct list_head *head, FILE *fp) 1891size_t __dsos__fprintf(struct list_head *head, FILE *fp)
1751{ 1892{
1752 struct dso *pos; 1893 struct dso *pos;
1894 size_t ret = 0;
1753 1895
1754 list_for_each_entry(pos, head, node) { 1896 list_for_each_entry(pos, head, node) {
1755 int i; 1897 int i;
1756 for (i = 0; i < MAP__NR_TYPES; ++i) 1898 for (i = 0; i < MAP__NR_TYPES; ++i)
1757 dso__fprintf(pos, i, fp); 1899 ret += dso__fprintf(pos, i, fp);
1758 } 1900 }
1901
1902 return ret;
1759} 1903}
1760 1904
1761void dsos__fprintf(FILE *fp) 1905size_t machines__fprintf_dsos(struct rb_root *self, FILE *fp)
1762{ 1906{
1763 __dsos__fprintf(&dsos__kernel, fp); 1907 struct rb_node *nd;
1764 __dsos__fprintf(&dsos__user, fp); 1908 size_t ret = 0;
1909
1910 for (nd = rb_first(self); nd; nd = rb_next(nd)) {
1911 struct machine *pos = rb_entry(nd, struct machine, rb_node);
1912 ret += __dsos__fprintf(&pos->kernel_dsos, fp);
1913 ret += __dsos__fprintf(&pos->user_dsos, fp);
1914 }
1915
1916 return ret;
1765} 1917}
1766 1918
1767static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp, 1919static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
@@ -1779,10 +1931,22 @@ static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
1779 return ret; 1931 return ret;
1780} 1932}
1781 1933
1782size_t dsos__fprintf_buildid(FILE *fp, bool with_hits) 1934size_t machine__fprintf_dsos_buildid(struct machine *self, FILE *fp, bool with_hits)
1783{ 1935{
1784 return (__dsos__fprintf_buildid(&dsos__kernel, fp, with_hits) + 1936 return __dsos__fprintf_buildid(&self->kernel_dsos, fp, with_hits) +
1785 __dsos__fprintf_buildid(&dsos__user, fp, with_hits)); 1937 __dsos__fprintf_buildid(&self->user_dsos, fp, with_hits);
1938}
1939
1940size_t machines__fprintf_dsos_buildid(struct rb_root *self, FILE *fp, bool with_hits)
1941{
1942 struct rb_node *nd;
1943 size_t ret = 0;
1944
1945 for (nd = rb_first(self); nd; nd = rb_next(nd)) {
1946 struct machine *pos = rb_entry(nd, struct machine, rb_node);
1947 ret += machine__fprintf_dsos_buildid(pos, fp, with_hits);
1948 }
1949 return ret;
1786} 1950}
1787 1951
1788struct dso *dso__new_kernel(const char *name) 1952struct dso *dso__new_kernel(const char *name)
@@ -1791,55 +1955,98 @@ struct dso *dso__new_kernel(const char *name)
1791 1955
1792 if (self != NULL) { 1956 if (self != NULL) {
1793 dso__set_short_name(self, "[kernel]"); 1957 dso__set_short_name(self, "[kernel]");
1794 self->kernel = 1; 1958 self->kernel = DSO_TYPE_KERNEL;
1959 }
1960
1961 return self;
1962}
1963
1964static struct dso *dso__new_guest_kernel(struct machine *machine,
1965 const char *name)
1966{
1967 char bf[PATH_MAX];
1968 struct dso *self = dso__new(name ?: machine__mmap_name(machine, bf, sizeof(bf)));
1969
1970 if (self != NULL) {
1971 dso__set_short_name(self, "[guest.kernel]");
1972 self->kernel = DSO_TYPE_GUEST_KERNEL;
1795 } 1973 }
1796 1974
1797 return self; 1975 return self;
1798} 1976}
1799 1977
1800void dso__read_running_kernel_build_id(struct dso *self) 1978void dso__read_running_kernel_build_id(struct dso *self, struct machine *machine)
1801{ 1979{
1802 if (sysfs__read_build_id("/sys/kernel/notes", self->build_id, 1980 char path[PATH_MAX];
1981
1982 if (machine__is_default_guest(machine))
1983 return;
1984 sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
1985 if (sysfs__read_build_id(path, self->build_id,
1803 sizeof(self->build_id)) == 0) 1986 sizeof(self->build_id)) == 0)
1804 self->has_build_id = true; 1987 self->has_build_id = true;
1805} 1988}
1806 1989
1807static struct dso *dsos__create_kernel(const char *vmlinux) 1990static struct dso *machine__create_kernel(struct machine *self)
1808{ 1991{
1809 struct dso *kernel = dso__new_kernel(vmlinux); 1992 const char *vmlinux_name = NULL;
1993 struct dso *kernel;
1810 1994
1811 if (kernel != NULL) { 1995 if (machine__is_host(self)) {
1812 dso__read_running_kernel_build_id(kernel); 1996 vmlinux_name = symbol_conf.vmlinux_name;
1813 dsos__add(&dsos__kernel, kernel); 1997 kernel = dso__new_kernel(vmlinux_name);
1998 } else {
1999 if (machine__is_default_guest(self))
2000 vmlinux_name = symbol_conf.default_guest_vmlinux_name;
2001 kernel = dso__new_guest_kernel(self, vmlinux_name);
1814 } 2002 }
1815 2003
2004 if (kernel != NULL) {
2005 dso__read_running_kernel_build_id(kernel, self);
2006 dsos__add(&self->kernel_dsos, kernel);
2007 }
1816 return kernel; 2008 return kernel;
1817} 2009}
1818 2010
1819int __map_groups__create_kernel_maps(struct map_groups *self, 2011int __machine__create_kernel_maps(struct machine *self, struct dso *kernel)
1820 struct map *vmlinux_maps[MAP__NR_TYPES],
1821 struct dso *kernel)
1822{ 2012{
1823 enum map_type type; 2013 enum map_type type;
1824 2014
1825 for (type = 0; type < MAP__NR_TYPES; ++type) { 2015 for (type = 0; type < MAP__NR_TYPES; ++type) {
1826 struct kmap *kmap; 2016 struct kmap *kmap;
1827 2017
1828 vmlinux_maps[type] = map__new2(0, kernel, type); 2018 self->vmlinux_maps[type] = map__new2(0, kernel, type);
1829 if (vmlinux_maps[type] == NULL) 2019 if (self->vmlinux_maps[type] == NULL)
1830 return -1; 2020 return -1;
1831 2021
1832 vmlinux_maps[type]->map_ip = 2022 self->vmlinux_maps[type]->map_ip =
1833 vmlinux_maps[type]->unmap_ip = identity__map_ip; 2023 self->vmlinux_maps[type]->unmap_ip = identity__map_ip;
1834 2024
1835 kmap = map__kmap(vmlinux_maps[type]); 2025 kmap = map__kmap(self->vmlinux_maps[type]);
1836 kmap->kmaps = self; 2026 kmap->kmaps = &self->kmaps;
1837 map_groups__insert(self, vmlinux_maps[type]); 2027 map_groups__insert(&self->kmaps, self->vmlinux_maps[type]);
1838 } 2028 }
1839 2029
1840 return 0; 2030 return 0;
1841} 2031}
1842 2032
2033int machine__create_kernel_maps(struct machine *self)
2034{
2035 struct dso *kernel = machine__create_kernel(self);
2036
2037 if (kernel == NULL ||
2038 __machine__create_kernel_maps(self, kernel) < 0)
2039 return -1;
2040
2041 if (symbol_conf.use_modules && machine__create_modules(self) < 0)
2042 pr_debug("Problems creating module maps, continuing anyway...\n");
2043 /*
2044 * Now that we have all the maps created, just set the ->end of them:
2045 */
2046 map_groups__fixup_end(&self->kmaps);
2047 return 0;
2048}
2049
1843static void vmlinux_path__exit(void) 2050static void vmlinux_path__exit(void)
1844{ 2051{
1845 while (--vmlinux_path__nr_entries >= 0) { 2052 while (--vmlinux_path__nr_entries >= 0) {
@@ -1895,6 +2102,17 @@ out_fail:
1895 return -1; 2102 return -1;
1896} 2103}
1897 2104
2105size_t vmlinux_path__fprintf(FILE *fp)
2106{
2107 int i;
2108 size_t printed = 0;
2109
2110 for (i = 0; i < vmlinux_path__nr_entries; ++i)
2111 printed += fprintf(fp, "[%d] %s\n", i, vmlinux_path[i]);
2112
2113 return printed;
2114}
2115
1898static int setup_list(struct strlist **list, const char *list_str, 2116static int setup_list(struct strlist **list, const char *list_str,
1899 const char *list_name) 2117 const char *list_name)
1900{ 2118{
@@ -1945,22 +2163,129 @@ out_free_comm_list:
1945 return -1; 2163 return -1;
1946} 2164}
1947 2165
1948int map_groups__create_kernel_maps(struct map_groups *self, 2166int machines__create_kernel_maps(struct rb_root *self, pid_t pid)
1949 struct map *vmlinux_maps[MAP__NR_TYPES])
1950{ 2167{
1951 struct dso *kernel = dsos__create_kernel(symbol_conf.vmlinux_name); 2168 struct machine *machine = machines__findnew(self, pid);
1952 2169
1953 if (kernel == NULL) 2170 if (machine == NULL)
1954 return -1; 2171 return -1;
1955 2172
1956 if (__map_groups__create_kernel_maps(self, vmlinux_maps, kernel) < 0) 2173 return machine__create_kernel_maps(machine);
1957 return -1; 2174}
1958 2175
1959 if (symbol_conf.use_modules && map_groups__create_modules(self) < 0) 2176static int hex(char ch)
1960 pr_debug("Problems creating module maps, continuing anyway...\n"); 2177{
1961 /* 2178 if ((ch >= '0') && (ch <= '9'))
1962 * Now that we have all the maps created, just set the ->end of them: 2179 return ch - '0';
1963 */ 2180 if ((ch >= 'a') && (ch <= 'f'))
1964 map_groups__fixup_end(self); 2181 return ch - 'a' + 10;
1965 return 0; 2182 if ((ch >= 'A') && (ch <= 'F'))
2183 return ch - 'A' + 10;
2184 return -1;
2185}
2186
2187/*
2188 * While we find nice hex chars, build a long_val.
2189 * Return number of chars processed.
2190 */
2191int hex2u64(const char *ptr, u64 *long_val)
2192{
2193 const char *p = ptr;
2194 *long_val = 0;
2195
2196 while (*p) {
2197 const int hex_val = hex(*p);
2198
2199 if (hex_val < 0)
2200 break;
2201
2202 *long_val = (*long_val << 4) | hex_val;
2203 p++;
2204 }
2205
2206 return p - ptr;
2207}
2208
2209char *strxfrchar(char *s, char from, char to)
2210{
2211 char *p = s;
2212
2213 while ((p = strchr(p, from)) != NULL)
2214 *p++ = to;
2215
2216 return s;
2217}
2218
2219int machines__create_guest_kernel_maps(struct rb_root *self)
2220{
2221 int ret = 0;
2222 struct dirent **namelist = NULL;
2223 int i, items = 0;
2224 char path[PATH_MAX];
2225 pid_t pid;
2226
2227 if (symbol_conf.default_guest_vmlinux_name ||
2228 symbol_conf.default_guest_modules ||
2229 symbol_conf.default_guest_kallsyms) {
2230 machines__create_kernel_maps(self, DEFAULT_GUEST_KERNEL_ID);
2231 }
2232
2233 if (symbol_conf.guestmount) {
2234 items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
2235 if (items <= 0)
2236 return -ENOENT;
2237 for (i = 0; i < items; i++) {
2238 if (!isdigit(namelist[i]->d_name[0])) {
2239 /* Filter out . and .. */
2240 continue;
2241 }
2242 pid = atoi(namelist[i]->d_name);
2243 sprintf(path, "%s/%s/proc/kallsyms",
2244 symbol_conf.guestmount,
2245 namelist[i]->d_name);
2246 ret = access(path, R_OK);
2247 if (ret) {
2248 pr_debug("Can't access file %s\n", path);
2249 goto failure;
2250 }
2251 machines__create_kernel_maps(self, pid);
2252 }
2253failure:
2254 free(namelist);
2255 }
2256
2257 return ret;
2258}
2259
2260int machine__load_kallsyms(struct machine *self, const char *filename,
2261 enum map_type type, symbol_filter_t filter)
2262{
2263 struct map *map = self->vmlinux_maps[type];
2264 int ret = dso__load_kallsyms(map->dso, filename, map, filter);
2265
2266 if (ret > 0) {
2267 dso__set_loaded(map->dso, type);
2268 /*
2269 * Since /proc/kallsyms will have multiple sessions for the
2270 * kernel, with modules between them, fixup the end of all
2271 * sections.
2272 */
2273 __map_groups__fixup_end(&self->kmaps, type);
2274 }
2275
2276 return ret;
2277}
2278
2279int machine__load_vmlinux_path(struct machine *self, enum map_type type,
2280 symbol_filter_t filter)
2281{
2282 struct map *map = self->vmlinux_maps[type];
2283 int ret = dso__load_vmlinux_path(map->dso, map, filter);
2284
2285 if (ret > 0) {
2286 dso__set_loaded(map->dso, type);
2287 map__reloc_vmlinux(map);
2288 }
2289
2290 return ret;
1966} 2291}