diff options
| -rw-r--r-- | tools/perf/Documentation/perf-buildid-cache.txt | 13 | ||||
| -rw-r--r-- | tools/perf/builtin-buildid-cache.c | 148 | ||||
| -rw-r--r-- | tools/perf/util/symbol-elf.c | 366 | ||||
| -rw-r--r-- | tools/perf/util/symbol-minimal.c | 6 | ||||
| -rw-r--r-- | tools/perf/util/symbol.c | 41 | ||||
| -rw-r--r-- | tools/perf/util/symbol.h | 3 |
6 files changed, 576 insertions, 1 deletions
diff --git a/tools/perf/Documentation/perf-buildid-cache.txt b/tools/perf/Documentation/perf-buildid-cache.txt index e9a8349a7172..fd77d81ea748 100644 --- a/tools/perf/Documentation/perf-buildid-cache.txt +++ b/tools/perf/Documentation/perf-buildid-cache.txt | |||
| @@ -21,6 +21,19 @@ OPTIONS | |||
| 21 | -a:: | 21 | -a:: |
| 22 | --add=:: | 22 | --add=:: |
| 23 | Add specified file to the cache. | 23 | Add specified file to the cache. |
| 24 | -k:: | ||
| 25 | --kcore:: | ||
| 26 | Add specified kcore file to the cache. For the current host that is | ||
| 27 | /proc/kcore which requires root permissions to read. Be aware that | ||
| 28 | running 'perf buildid-cache' as root may update root's build-id cache | ||
| 29 | not the user's. Use the -v option to see where the file is created. | ||
| 30 | Note that the copied file contains only code sections not the whole core | ||
| 31 | image. Note also that files "kallsyms" and "modules" must also be in the | ||
| 32 | same directory and are also copied. All 3 files are created with read | ||
| 33 | permissions for root only. kcore will not be added if there is already a | ||
| 34 | kcore in the cache (with the same build-id) that has the same modules at | ||
| 35 | the same addresses. Use the -v option to see if a copy of kcore is | ||
| 36 | actually made. | ||
| 24 | -r:: | 37 | -r:: |
| 25 | --remove=:: | 38 | --remove=:: |
| 26 | Remove specified file from the cache. | 39 | Remove specified file from the cache. |
diff --git a/tools/perf/builtin-buildid-cache.c b/tools/perf/builtin-buildid-cache.c index c96c8fa38243..8140b7b249fa 100644 --- a/tools/perf/builtin-buildid-cache.c +++ b/tools/perf/builtin-buildid-cache.c | |||
| @@ -6,6 +6,11 @@ | |||
| 6 | * Copyright (C) 2010, Red Hat Inc. | 6 | * Copyright (C) 2010, Red Hat Inc. |
| 7 | * Copyright (C) 2010, Arnaldo Carvalho de Melo <acme@redhat.com> | 7 | * Copyright (C) 2010, Arnaldo Carvalho de Melo <acme@redhat.com> |
| 8 | */ | 8 | */ |
| 9 | #include <sys/types.h> | ||
| 10 | #include <sys/time.h> | ||
| 11 | #include <time.h> | ||
| 12 | #include <dirent.h> | ||
| 13 | #include <unistd.h> | ||
| 9 | #include "builtin.h" | 14 | #include "builtin.h" |
| 10 | #include "perf.h" | 15 | #include "perf.h" |
| 11 | #include "util/cache.h" | 16 | #include "util/cache.h" |
| @@ -17,6 +22,140 @@ | |||
| 17 | #include "util/session.h" | 22 | #include "util/session.h" |
| 18 | #include "util/symbol.h" | 23 | #include "util/symbol.h" |
| 19 | 24 | ||
| 25 | static int build_id_cache__kcore_buildid(const char *proc_dir, char *sbuildid) | ||
| 26 | { | ||
| 27 | char root_dir[PATH_MAX]; | ||
| 28 | char notes[PATH_MAX]; | ||
| 29 | u8 build_id[BUILD_ID_SIZE]; | ||
| 30 | char *p; | ||
| 31 | |||
| 32 | strlcpy(root_dir, proc_dir, sizeof(root_dir)); | ||
| 33 | |||
| 34 | p = strrchr(root_dir, '/'); | ||
| 35 | if (!p) | ||
| 36 | return -1; | ||
| 37 | *p = '\0'; | ||
| 38 | |||
| 39 | scnprintf(notes, sizeof(notes), "%s/sys/kernel/notes", root_dir); | ||
| 40 | |||
| 41 | if (sysfs__read_build_id(notes, build_id, sizeof(build_id))) | ||
| 42 | return -1; | ||
| 43 | |||
| 44 | build_id__sprintf(build_id, sizeof(build_id), sbuildid); | ||
| 45 | |||
| 46 | return 0; | ||
| 47 | } | ||
| 48 | |||
| 49 | static int build_id_cache__kcore_dir(char *dir, size_t sz) | ||
| 50 | { | ||
| 51 | struct timeval tv; | ||
| 52 | struct tm tm; | ||
| 53 | char dt[32]; | ||
| 54 | |||
| 55 | if (gettimeofday(&tv, NULL) || !localtime_r(&tv.tv_sec, &tm)) | ||
| 56 | return -1; | ||
| 57 | |||
| 58 | if (!strftime(dt, sizeof(dt), "%Y%m%d%H%M%S", &tm)) | ||
| 59 | return -1; | ||
| 60 | |||
| 61 | scnprintf(dir, sz, "%s%02u", dt, (unsigned)tv.tv_usec / 10000); | ||
| 62 | |||
| 63 | return 0; | ||
| 64 | } | ||
| 65 | |||
| 66 | static int build_id_cache__kcore_existing(const char *from_dir, char *to_dir, | ||
| 67 | size_t to_dir_sz) | ||
| 68 | { | ||
| 69 | char from[PATH_MAX]; | ||
| 70 | char to[PATH_MAX]; | ||
| 71 | struct dirent *dent; | ||
| 72 | int ret = -1; | ||
| 73 | DIR *d; | ||
| 74 | |||
| 75 | d = opendir(to_dir); | ||
| 76 | if (!d) | ||
| 77 | return -1; | ||
| 78 | |||
| 79 | scnprintf(from, sizeof(from), "%s/modules", from_dir); | ||
| 80 | |||
| 81 | while (1) { | ||
| 82 | dent = readdir(d); | ||
| 83 | if (!dent) | ||
| 84 | break; | ||
| 85 | if (dent->d_type != DT_DIR) | ||
| 86 | continue; | ||
| 87 | scnprintf(to, sizeof(to), "%s/%s/modules", to_dir, | ||
| 88 | dent->d_name); | ||
| 89 | if (!compare_proc_modules(from, to)) { | ||
| 90 | scnprintf(to, sizeof(to), "%s/%s", to_dir, | ||
| 91 | dent->d_name); | ||
| 92 | strlcpy(to_dir, to, to_dir_sz); | ||
| 93 | ret = 0; | ||
| 94 | break; | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | closedir(d); | ||
| 99 | |||
| 100 | return ret; | ||
| 101 | } | ||
| 102 | |||
| 103 | static int build_id_cache__add_kcore(const char *filename, const char *debugdir) | ||
| 104 | { | ||
| 105 | char dir[32], sbuildid[BUILD_ID_SIZE * 2 + 1]; | ||
| 106 | char from_dir[PATH_MAX], to_dir[PATH_MAX]; | ||
| 107 | char *p; | ||
| 108 | |||
| 109 | strlcpy(from_dir, filename, sizeof(from_dir)); | ||
| 110 | |||
| 111 | p = strrchr(from_dir, '/'); | ||
| 112 | if (!p || strcmp(p + 1, "kcore")) | ||
| 113 | return -1; | ||
| 114 | *p = '\0'; | ||
| 115 | |||
| 116 | if (build_id_cache__kcore_buildid(from_dir, sbuildid)) | ||
| 117 | return -1; | ||
| 118 | |||
| 119 | scnprintf(to_dir, sizeof(to_dir), "%s/[kernel.kcore]/%s", | ||
| 120 | debugdir, sbuildid); | ||
| 121 | |||
| 122 | if (!build_id_cache__kcore_existing(from_dir, to_dir, sizeof(to_dir))) { | ||
| 123 | pr_debug("same kcore found in %s\n", to_dir); | ||
| 124 | return 0; | ||
| 125 | } | ||
| 126 | |||
| 127 | if (build_id_cache__kcore_dir(dir, sizeof(dir))) | ||
| 128 | return -1; | ||
| 129 | |||
| 130 | scnprintf(to_dir, sizeof(to_dir), "%s/[kernel.kcore]/%s/%s", | ||
| 131 | debugdir, sbuildid, dir); | ||
| 132 | |||
| 133 | if (mkdir_p(to_dir, 0755)) | ||
| 134 | return -1; | ||
| 135 | |||
| 136 | if (kcore_copy(from_dir, to_dir)) { | ||
| 137 | /* Remove YYYYmmddHHMMSShh directory */ | ||
| 138 | if (!rmdir(to_dir)) { | ||
| 139 | p = strrchr(to_dir, '/'); | ||
| 140 | if (p) | ||
| 141 | *p = '\0'; | ||
| 142 | /* Try to remove buildid directory */ | ||
| 143 | if (!rmdir(to_dir)) { | ||
| 144 | p = strrchr(to_dir, '/'); | ||
| 145 | if (p) | ||
| 146 | *p = '\0'; | ||
| 147 | /* Try to remove [kernel.kcore] directory */ | ||
| 148 | rmdir(to_dir); | ||
| 149 | } | ||
| 150 | } | ||
| 151 | return -1; | ||
| 152 | } | ||
| 153 | |||
| 154 | pr_debug("kcore added to build-id cache directory %s\n", to_dir); | ||
| 155 | |||
| 156 | return 0; | ||
| 157 | } | ||
| 158 | |||
| 20 | static int build_id_cache__add_file(const char *filename, const char *debugdir) | 159 | static int build_id_cache__add_file(const char *filename, const char *debugdir) |
| 21 | { | 160 | { |
| 22 | char sbuild_id[BUILD_ID_SIZE * 2 + 1]; | 161 | char sbuild_id[BUILD_ID_SIZE * 2 + 1]; |
| @@ -130,11 +269,14 @@ int cmd_buildid_cache(int argc, const char **argv, | |||
| 130 | char const *add_name_list_str = NULL, | 269 | char const *add_name_list_str = NULL, |
| 131 | *remove_name_list_str = NULL, | 270 | *remove_name_list_str = NULL, |
| 132 | *missing_filename = NULL, | 271 | *missing_filename = NULL, |
| 133 | *update_name_list_str = NULL; | 272 | *update_name_list_str = NULL, |
| 273 | *kcore_filename; | ||
| 134 | 274 | ||
| 135 | const struct option buildid_cache_options[] = { | 275 | const struct option buildid_cache_options[] = { |
| 136 | OPT_STRING('a', "add", &add_name_list_str, | 276 | OPT_STRING('a', "add", &add_name_list_str, |
| 137 | "file list", "file(s) to add"), | 277 | "file list", "file(s) to add"), |
| 278 | OPT_STRING('k', "kcore", &kcore_filename, | ||
| 279 | "file", "kcore file to add"), | ||
| 138 | OPT_STRING('r', "remove", &remove_name_list_str, "file list", | 280 | OPT_STRING('r', "remove", &remove_name_list_str, "file list", |
| 139 | "file(s) to remove"), | 281 | "file(s) to remove"), |
| 140 | OPT_STRING('M', "missing", &missing_filename, "file", | 282 | OPT_STRING('M', "missing", &missing_filename, "file", |
| @@ -217,5 +359,9 @@ int cmd_buildid_cache(int argc, const char **argv, | |||
| 217 | } | 359 | } |
| 218 | } | 360 | } |
| 219 | 361 | ||
| 362 | if (kcore_filename && | ||
| 363 | build_id_cache__add_kcore(kcore_filename, debugdir)) | ||
| 364 | pr_warning("Couldn't add %s\n", kcore_filename); | ||
| 365 | |||
| 220 | return ret; | 366 | return ret; |
| 221 | } | 367 | } |
diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c index 499c71d30225..d6b8af3ce344 100644 --- a/tools/perf/util/symbol-elf.c +++ b/tools/perf/util/symbol-elf.c | |||
| @@ -1202,6 +1202,372 @@ static off_t kcore__write(struct kcore *kcore) | |||
| 1202 | return elf_update(kcore->elf, ELF_C_WRITE); | 1202 | return elf_update(kcore->elf, ELF_C_WRITE); |
| 1203 | } | 1203 | } |
| 1204 | 1204 | ||
| 1205 | struct phdr_data { | ||
| 1206 | off_t offset; | ||
| 1207 | u64 addr; | ||
| 1208 | u64 len; | ||
| 1209 | }; | ||
| 1210 | |||
| 1211 | struct kcore_copy_info { | ||
| 1212 | u64 stext; | ||
| 1213 | u64 etext; | ||
| 1214 | u64 first_symbol; | ||
| 1215 | u64 last_symbol; | ||
| 1216 | u64 first_module; | ||
| 1217 | u64 last_module_symbol; | ||
| 1218 | struct phdr_data kernel_map; | ||
| 1219 | struct phdr_data modules_map; | ||
| 1220 | }; | ||
| 1221 | |||
| 1222 | static int kcore_copy__process_kallsyms(void *arg, const char *name, char type, | ||
| 1223 | u64 start) | ||
| 1224 | { | ||
| 1225 | struct kcore_copy_info *kci = arg; | ||
| 1226 | |||
| 1227 | if (!symbol_type__is_a(type, MAP__FUNCTION)) | ||
| 1228 | return 0; | ||
| 1229 | |||
| 1230 | if (strchr(name, '[')) { | ||
| 1231 | if (start > kci->last_module_symbol) | ||
| 1232 | kci->last_module_symbol = start; | ||
| 1233 | return 0; | ||
| 1234 | } | ||
| 1235 | |||
| 1236 | if (!kci->first_symbol || start < kci->first_symbol) | ||
| 1237 | kci->first_symbol = start; | ||
| 1238 | |||
| 1239 | if (!kci->last_symbol || start > kci->last_symbol) | ||
| 1240 | kci->last_symbol = start; | ||
| 1241 | |||
| 1242 | if (!strcmp(name, "_stext")) { | ||
| 1243 | kci->stext = start; | ||
| 1244 | return 0; | ||
| 1245 | } | ||
| 1246 | |||
| 1247 | if (!strcmp(name, "_etext")) { | ||
| 1248 | kci->etext = start; | ||
| 1249 | return 0; | ||
| 1250 | } | ||
| 1251 | |||
| 1252 | return 0; | ||
| 1253 | } | ||
| 1254 | |||
| 1255 | static int kcore_copy__parse_kallsyms(struct kcore_copy_info *kci, | ||
| 1256 | const char *dir) | ||
| 1257 | { | ||
| 1258 | char kallsyms_filename[PATH_MAX]; | ||
| 1259 | |||
| 1260 | scnprintf(kallsyms_filename, PATH_MAX, "%s/kallsyms", dir); | ||
| 1261 | |||
| 1262 | if (symbol__restricted_filename(kallsyms_filename, "/proc/kallsyms")) | ||
| 1263 | return -1; | ||
| 1264 | |||
| 1265 | if (kallsyms__parse(kallsyms_filename, kci, | ||
| 1266 | kcore_copy__process_kallsyms) < 0) | ||
| 1267 | return -1; | ||
| 1268 | |||
| 1269 | return 0; | ||
| 1270 | } | ||
| 1271 | |||
| 1272 | static int kcore_copy__process_modules(void *arg, | ||
| 1273 | const char *name __maybe_unused, | ||
| 1274 | u64 start) | ||
| 1275 | { | ||
| 1276 | struct kcore_copy_info *kci = arg; | ||
| 1277 | |||
| 1278 | if (!kci->first_module || start < kci->first_module) | ||
| 1279 | kci->first_module = start; | ||
| 1280 | |||
| 1281 | return 0; | ||
| 1282 | } | ||
| 1283 | |||
| 1284 | static int kcore_copy__parse_modules(struct kcore_copy_info *kci, | ||
| 1285 | const char *dir) | ||
| 1286 | { | ||
| 1287 | char modules_filename[PATH_MAX]; | ||
| 1288 | |||
| 1289 | scnprintf(modules_filename, PATH_MAX, "%s/modules", dir); | ||
| 1290 | |||
| 1291 | if (symbol__restricted_filename(modules_filename, "/proc/modules")) | ||
| 1292 | return -1; | ||
| 1293 | |||
| 1294 | if (modules__parse(modules_filename, kci, | ||
| 1295 | kcore_copy__process_modules) < 0) | ||
| 1296 | return -1; | ||
| 1297 | |||
| 1298 | return 0; | ||
| 1299 | } | ||
| 1300 | |||
| 1301 | static void kcore_copy__map(struct phdr_data *p, u64 start, u64 end, u64 pgoff, | ||
| 1302 | u64 s, u64 e) | ||
| 1303 | { | ||
| 1304 | if (p->addr || s < start || s >= end) | ||
| 1305 | return; | ||
| 1306 | |||
| 1307 | p->addr = s; | ||
| 1308 | p->offset = (s - start) + pgoff; | ||
| 1309 | p->len = e < end ? e - s : end - s; | ||
| 1310 | } | ||
| 1311 | |||
| 1312 | static int kcore_copy__read_map(u64 start, u64 len, u64 pgoff, void *data) | ||
| 1313 | { | ||
| 1314 | struct kcore_copy_info *kci = data; | ||
| 1315 | u64 end = start + len; | ||
| 1316 | |||
| 1317 | kcore_copy__map(&kci->kernel_map, start, end, pgoff, kci->stext, | ||
| 1318 | kci->etext); | ||
| 1319 | |||
| 1320 | kcore_copy__map(&kci->modules_map, start, end, pgoff, kci->first_module, | ||
| 1321 | kci->last_module_symbol); | ||
| 1322 | |||
| 1323 | return 0; | ||
| 1324 | } | ||
| 1325 | |||
| 1326 | static int kcore_copy__read_maps(struct kcore_copy_info *kci, Elf *elf) | ||
| 1327 | { | ||
| 1328 | if (elf_read_maps(elf, true, kcore_copy__read_map, kci) < 0) | ||
| 1329 | return -1; | ||
| 1330 | |||
| 1331 | return 0; | ||
| 1332 | } | ||
| 1333 | |||
| 1334 | static int kcore_copy__calc_maps(struct kcore_copy_info *kci, const char *dir, | ||
| 1335 | Elf *elf) | ||
| 1336 | { | ||
| 1337 | if (kcore_copy__parse_kallsyms(kci, dir)) | ||
| 1338 | return -1; | ||
| 1339 | |||
| 1340 | if (kcore_copy__parse_modules(kci, dir)) | ||
| 1341 | return -1; | ||
| 1342 | |||
| 1343 | if (kci->stext) | ||
| 1344 | kci->stext = round_down(kci->stext, page_size); | ||
| 1345 | else | ||
| 1346 | kci->stext = round_down(kci->first_symbol, page_size); | ||
| 1347 | |||
| 1348 | if (kci->etext) { | ||
| 1349 | kci->etext = round_up(kci->etext, page_size); | ||
| 1350 | } else if (kci->last_symbol) { | ||
| 1351 | kci->etext = round_up(kci->last_symbol, page_size); | ||
| 1352 | kci->etext += page_size; | ||
| 1353 | } | ||
| 1354 | |||
| 1355 | kci->first_module = round_down(kci->first_module, page_size); | ||
| 1356 | |||
| 1357 | if (kci->last_module_symbol) { | ||
| 1358 | kci->last_module_symbol = round_up(kci->last_module_symbol, | ||
| 1359 | page_size); | ||
| 1360 | kci->last_module_symbol += page_size; | ||
| 1361 | } | ||
| 1362 | |||
| 1363 | if (!kci->stext || !kci->etext) | ||
| 1364 | return -1; | ||
| 1365 | |||
| 1366 | if (kci->first_module && !kci->last_module_symbol) | ||
| 1367 | return -1; | ||
| 1368 | |||
| 1369 | return kcore_copy__read_maps(kci, elf); | ||
| 1370 | } | ||
| 1371 | |||
| 1372 | static int kcore_copy__copy_file(const char *from_dir, const char *to_dir, | ||
| 1373 | const char *name) | ||
| 1374 | { | ||
| 1375 | char from_filename[PATH_MAX]; | ||
| 1376 | char to_filename[PATH_MAX]; | ||
| 1377 | |||
| 1378 | scnprintf(from_filename, PATH_MAX, "%s/%s", from_dir, name); | ||
| 1379 | scnprintf(to_filename, PATH_MAX, "%s/%s", to_dir, name); | ||
| 1380 | |||
| 1381 | return copyfile_mode(from_filename, to_filename, 0400); | ||
| 1382 | } | ||
| 1383 | |||
| 1384 | static int kcore_copy__unlink(const char *dir, const char *name) | ||
| 1385 | { | ||
| 1386 | char filename[PATH_MAX]; | ||
| 1387 | |||
| 1388 | scnprintf(filename, PATH_MAX, "%s/%s", dir, name); | ||
| 1389 | |||
| 1390 | return unlink(filename); | ||
| 1391 | } | ||
| 1392 | |||
| 1393 | static int kcore_copy__compare_fds(int from, int to) | ||
| 1394 | { | ||
| 1395 | char *buf_from; | ||
| 1396 | char *buf_to; | ||
| 1397 | ssize_t ret; | ||
| 1398 | size_t len; | ||
| 1399 | int err = -1; | ||
| 1400 | |||
| 1401 | buf_from = malloc(page_size); | ||
| 1402 | buf_to = malloc(page_size); | ||
| 1403 | if (!buf_from || !buf_to) | ||
| 1404 | goto out; | ||
| 1405 | |||
| 1406 | while (1) { | ||
| 1407 | /* Use read because mmap won't work on proc files */ | ||
| 1408 | ret = read(from, buf_from, page_size); | ||
| 1409 | if (ret < 0) | ||
| 1410 | goto out; | ||
| 1411 | |||
| 1412 | if (!ret) | ||
| 1413 | break; | ||
| 1414 | |||
| 1415 | len = ret; | ||
| 1416 | |||
| 1417 | if (readn(to, buf_to, len) != (int)len) | ||
| 1418 | goto out; | ||
| 1419 | |||
| 1420 | if (memcmp(buf_from, buf_to, len)) | ||
| 1421 | goto out; | ||
| 1422 | } | ||
| 1423 | |||
| 1424 | err = 0; | ||
| 1425 | out: | ||
| 1426 | free(buf_to); | ||
| 1427 | free(buf_from); | ||
| 1428 | return err; | ||
| 1429 | } | ||
| 1430 | |||
| 1431 | static int kcore_copy__compare_files(const char *from_filename, | ||
| 1432 | const char *to_filename) | ||
| 1433 | { | ||
| 1434 | int from, to, err = -1; | ||
| 1435 | |||
| 1436 | from = open(from_filename, O_RDONLY); | ||
| 1437 | if (from < 0) | ||
| 1438 | return -1; | ||
| 1439 | |||
| 1440 | to = open(to_filename, O_RDONLY); | ||
| 1441 | if (to < 0) | ||
| 1442 | goto out_close_from; | ||
| 1443 | |||
| 1444 | err = kcore_copy__compare_fds(from, to); | ||
| 1445 | |||
| 1446 | close(to); | ||
| 1447 | out_close_from: | ||
| 1448 | close(from); | ||
| 1449 | return err; | ||
| 1450 | } | ||
| 1451 | |||
| 1452 | static int kcore_copy__compare_file(const char *from_dir, const char *to_dir, | ||
| 1453 | const char *name) | ||
| 1454 | { | ||
| 1455 | char from_filename[PATH_MAX]; | ||
| 1456 | char to_filename[PATH_MAX]; | ||
| 1457 | |||
| 1458 | scnprintf(from_filename, PATH_MAX, "%s/%s", from_dir, name); | ||
| 1459 | scnprintf(to_filename, PATH_MAX, "%s/%s", to_dir, name); | ||
| 1460 | |||
| 1461 | return kcore_copy__compare_files(from_filename, to_filename); | ||
| 1462 | } | ||
| 1463 | |||
| 1464 | /** | ||
| 1465 | * kcore_copy - copy kallsyms, modules and kcore from one directory to another. | ||
| 1466 | * @from_dir: from directory | ||
| 1467 | * @to_dir: to directory | ||
| 1468 | * | ||
| 1469 | * This function copies kallsyms, modules and kcore files from one directory to | ||
| 1470 | * another. kallsyms and modules are copied entirely. Only code segments are | ||
| 1471 | * copied from kcore. It is assumed that two segments suffice: one for the | ||
| 1472 | * kernel proper and one for all the modules. The code segments are determined | ||
| 1473 | * from kallsyms and modules files. The kernel map starts at _stext or the | ||
| 1474 | * lowest function symbol, and ends at _etext or the highest function symbol. | ||
| 1475 | * The module map starts at the lowest module address and ends at the highest | ||
| 1476 | * module symbol. Start addresses are rounded down to the nearest page. End | ||
| 1477 | * addresses are rounded up to the nearest page. An extra page is added to the | ||
| 1478 | * highest kernel symbol and highest module symbol to, hopefully, encompass that | ||
| 1479 | * symbol too. Because it contains only code sections, the resulting kcore is | ||
| 1480 | * unusual. One significant peculiarity is that the mapping (start -> pgoff) | ||
| 1481 | * is not the same for the kernel map and the modules map. That happens because | ||
| 1482 | * the data is copied adjacently whereas the original kcore has gaps. Finally, | ||
| 1483 | * kallsyms and modules files are compared with their copies to check that | ||
| 1484 | * modules have not been loaded or unloaded while the copies were taking place. | ||
| 1485 | * | ||
| 1486 | * Return: %0 on success, %-1 on failure. | ||
| 1487 | */ | ||
| 1488 | int kcore_copy(const char *from_dir, const char *to_dir) | ||
| 1489 | { | ||
| 1490 | struct kcore kcore; | ||
| 1491 | struct kcore extract; | ||
| 1492 | size_t count = 2; | ||
| 1493 | int idx = 0, err = -1; | ||
| 1494 | off_t offset = page_size, sz, modules_offset = 0; | ||
| 1495 | struct kcore_copy_info kci = { .stext = 0, }; | ||
| 1496 | char kcore_filename[PATH_MAX]; | ||
| 1497 | char extract_filename[PATH_MAX]; | ||
| 1498 | |||
| 1499 | if (kcore_copy__copy_file(from_dir, to_dir, "kallsyms")) | ||
| 1500 | return -1; | ||
| 1501 | |||
| 1502 | if (kcore_copy__copy_file(from_dir, to_dir, "modules")) | ||
| 1503 | goto out_unlink_kallsyms; | ||
| 1504 | |||
| 1505 | scnprintf(kcore_filename, PATH_MAX, "%s/kcore", from_dir); | ||
| 1506 | scnprintf(extract_filename, PATH_MAX, "%s/kcore", to_dir); | ||
| 1507 | |||
| 1508 | if (kcore__open(&kcore, kcore_filename)) | ||
| 1509 | goto out_unlink_modules; | ||
| 1510 | |||
| 1511 | if (kcore_copy__calc_maps(&kci, from_dir, kcore.elf)) | ||
| 1512 | goto out_kcore_close; | ||
| 1513 | |||
| 1514 | if (kcore__init(&extract, extract_filename, kcore.elfclass, false)) | ||
| 1515 | goto out_kcore_close; | ||
| 1516 | |||
| 1517 | if (!kci.modules_map.addr) | ||
| 1518 | count -= 1; | ||
| 1519 | |||
| 1520 | if (kcore__copy_hdr(&kcore, &extract, count)) | ||
| 1521 | goto out_extract_close; | ||
| 1522 | |||
| 1523 | if (kcore__add_phdr(&extract, idx++, offset, kci.kernel_map.addr, | ||
| 1524 | kci.kernel_map.len)) | ||
| 1525 | goto out_extract_close; | ||
| 1526 | |||
| 1527 | if (kci.modules_map.addr) { | ||
| 1528 | modules_offset = offset + kci.kernel_map.len; | ||
| 1529 | if (kcore__add_phdr(&extract, idx, modules_offset, | ||
| 1530 | kci.modules_map.addr, kci.modules_map.len)) | ||
| 1531 | goto out_extract_close; | ||
| 1532 | } | ||
| 1533 | |||
| 1534 | sz = kcore__write(&extract); | ||
| 1535 | if (sz < 0 || sz > offset) | ||
| 1536 | goto out_extract_close; | ||
| 1537 | |||
| 1538 | if (copy_bytes(kcore.fd, kci.kernel_map.offset, extract.fd, offset, | ||
| 1539 | kci.kernel_map.len)) | ||
| 1540 | goto out_extract_close; | ||
| 1541 | |||
| 1542 | if (modules_offset && copy_bytes(kcore.fd, kci.modules_map.offset, | ||
| 1543 | extract.fd, modules_offset, | ||
| 1544 | kci.modules_map.len)) | ||
| 1545 | goto out_extract_close; | ||
| 1546 | |||
| 1547 | if (kcore_copy__compare_file(from_dir, to_dir, "modules")) | ||
| 1548 | goto out_extract_close; | ||
| 1549 | |||
| 1550 | if (kcore_copy__compare_file(from_dir, to_dir, "kallsyms")) | ||
| 1551 | goto out_extract_close; | ||
| 1552 | |||
| 1553 | err = 0; | ||
| 1554 | |||
| 1555 | out_extract_close: | ||
| 1556 | kcore__close(&extract); | ||
| 1557 | if (err) | ||
| 1558 | unlink(extract_filename); | ||
| 1559 | out_kcore_close: | ||
| 1560 | kcore__close(&kcore); | ||
| 1561 | out_unlink_modules: | ||
| 1562 | if (err) | ||
| 1563 | kcore_copy__unlink(to_dir, "modules"); | ||
| 1564 | out_unlink_kallsyms: | ||
| 1565 | if (err) | ||
| 1566 | kcore_copy__unlink(to_dir, "kallsyms"); | ||
| 1567 | |||
| 1568 | return err; | ||
| 1569 | } | ||
| 1570 | |||
| 1205 | int kcore_extract__create(struct kcore_extract *kce) | 1571 | int kcore_extract__create(struct kcore_extract *kce) |
| 1206 | { | 1572 | { |
| 1207 | struct kcore kcore; | 1573 | struct kcore kcore; |
diff --git a/tools/perf/util/symbol-minimal.c b/tools/perf/util/symbol-minimal.c index 928556d2508f..2d2dd0532b5a 100644 --- a/tools/perf/util/symbol-minimal.c +++ b/tools/perf/util/symbol-minimal.c | |||
| @@ -317,6 +317,12 @@ void kcore_extract__delete(struct kcore_extract *kce __maybe_unused) | |||
| 317 | { | 317 | { |
| 318 | } | 318 | } |
| 319 | 319 | ||
| 320 | int kcore_copy(const char *from_dir __maybe_unused, | ||
| 321 | const char *to_dir __maybe_unused) | ||
| 322 | { | ||
| 323 | return -1; | ||
| 324 | } | ||
| 325 | |||
| 320 | void symbol__elf_init(void) | 326 | void symbol__elf_init(void) |
| 321 | { | 327 | { |
| 322 | } | 328 | } |
diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c index 76a9e933a7a5..b66c1eefc313 100644 --- a/tools/perf/util/symbol.c +++ b/tools/perf/util/symbol.c | |||
| @@ -893,6 +893,47 @@ static int read_proc_modules(const char *filename, struct rb_root *modules) | |||
| 893 | return 0; | 893 | return 0; |
| 894 | } | 894 | } |
| 895 | 895 | ||
| 896 | int compare_proc_modules(const char *from, const char *to) | ||
| 897 | { | ||
| 898 | struct rb_root from_modules = RB_ROOT; | ||
| 899 | struct rb_root to_modules = RB_ROOT; | ||
| 900 | struct rb_node *from_node, *to_node; | ||
| 901 | struct module_info *from_m, *to_m; | ||
| 902 | int ret = -1; | ||
| 903 | |||
| 904 | if (read_proc_modules(from, &from_modules)) | ||
| 905 | return -1; | ||
| 906 | |||
| 907 | if (read_proc_modules(to, &to_modules)) | ||
| 908 | goto out_delete_from; | ||
| 909 | |||
| 910 | from_node = rb_first(&from_modules); | ||
| 911 | to_node = rb_first(&to_modules); | ||
| 912 | while (from_node) { | ||
| 913 | if (!to_node) | ||
| 914 | break; | ||
| 915 | |||
| 916 | from_m = rb_entry(from_node, struct module_info, rb_node); | ||
| 917 | to_m = rb_entry(to_node, struct module_info, rb_node); | ||
| 918 | |||
| 919 | if (from_m->start != to_m->start || | ||
| 920 | strcmp(from_m->name, to_m->name)) | ||
| 921 | break; | ||
| 922 | |||
| 923 | from_node = rb_next(from_node); | ||
| 924 | to_node = rb_next(to_node); | ||
| 925 | } | ||
| 926 | |||
| 927 | if (!from_node && !to_node) | ||
| 928 | ret = 0; | ||
| 929 | |||
| 930 | delete_modules(&to_modules); | ||
| 931 | out_delete_from: | ||
| 932 | delete_modules(&from_modules); | ||
| 933 | |||
| 934 | return ret; | ||
| 935 | } | ||
| 936 | |||
| 896 | static int do_validate_kcore_modules(const char *filename, struct map *map, | 937 | static int do_validate_kcore_modules(const char *filename, struct map *map, |
| 897 | struct map_groups *kmaps) | 938 | struct map_groups *kmaps) |
| 898 | { | 939 | { |
diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h index fb107e13e925..07de8fea2f48 100644 --- a/tools/perf/util/symbol.h +++ b/tools/perf/util/symbol.h | |||
| @@ -270,4 +270,7 @@ struct kcore_extract { | |||
| 270 | int kcore_extract__create(struct kcore_extract *kce); | 270 | int kcore_extract__create(struct kcore_extract *kce); |
| 271 | void kcore_extract__delete(struct kcore_extract *kce); | 271 | void kcore_extract__delete(struct kcore_extract *kce); |
| 272 | 272 | ||
| 273 | int kcore_copy(const char *from_dir, const char *to_dir); | ||
| 274 | int compare_proc_modules(const char *from, const char *to); | ||
| 275 | |||
| 273 | #endif /* __PERF_SYMBOL */ | 276 | #endif /* __PERF_SYMBOL */ |
