Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf tools: Moving code in some files

Needed for later changes. No modified functionality.

Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: http://lkml.kernel.org/r/1323248577-11268-4-git-send-email-robert.richter@amd.com
Signed-off-by: Robert Richter <robert.richter@amd.com>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Robert Richter and committed by
Arnaldo Carvalho de Melo
1b549504 002c4fd9

+264 -267
+18 -18
tools/perf/builtin-buildid-list.c
··· 39 39 OPT_END() 40 40 }; 41 41 42 - static int perf_session__list_build_ids(void) 43 - { 44 - struct perf_session *session; 45 - 46 - session = perf_session__new(input_name, O_RDONLY, force, false, 47 - &build_id__mark_dso_hit_ops); 48 - if (session == NULL) 49 - return -1; 50 - 51 - if (with_hits) 52 - perf_session__process_events(session, &build_id__mark_dso_hit_ops); 53 - 54 - perf_session__fprintf_dsos_buildid(session, stdout, with_hits); 55 - 56 - perf_session__delete(session); 57 - return 0; 58 - } 59 - 60 42 static int sysfs__fprintf_build_id(FILE *fp) 61 43 { 62 44 u8 kallsyms_build_id[BUILD_ID_SIZE]; ··· 65 83 66 84 build_id__sprintf(build_id, sizeof(build_id), sbuild_id); 67 85 return fprintf(fp, "%s\n", sbuild_id); 86 + } 87 + 88 + static int perf_session__list_build_ids(void) 89 + { 90 + struct perf_session *session; 91 + 92 + session = perf_session__new(input_name, O_RDONLY, force, false, 93 + &build_id__mark_dso_hit_ops); 94 + if (session == NULL) 95 + return -1; 96 + 97 + if (with_hits) 98 + perf_session__process_events(session, &build_id__mark_dso_hit_ops); 99 + 100 + perf_session__fprintf_dsos_buildid(session, stdout, with_hits); 101 + 102 + perf_session__delete(session); 103 + return 0; 68 104 } 69 105 70 106 static int __cmd_buildid_list(void)
+246 -249
tools/perf/util/header.c
··· 28 28 static u32 header_argc; 29 29 static const char **header_argv; 30 30 31 - static int dsos__write_buildid_table(struct perf_header *header, int fd); 32 - static int perf_session__cache_build_ids(struct perf_session *session); 33 - 34 31 int perf_header__push_event(u64 id, const char *name) 35 32 { 36 33 if (strlen(name) > MAX_EVENT_NAME) ··· 182 185 header_argv[i] = argv[i]; 183 186 184 187 return 0; 188 + } 189 + 190 + #define dsos__for_each_with_build_id(pos, head) \ 191 + list_for_each_entry(pos, head, node) \ 192 + if (!pos->has_build_id) \ 193 + continue; \ 194 + else 195 + 196 + static int __dsos__write_buildid_table(struct list_head *head, pid_t pid, 197 + u16 misc, int fd) 198 + { 199 + struct dso *pos; 200 + 201 + dsos__for_each_with_build_id(pos, head) { 202 + int err; 203 + struct build_id_event b; 204 + size_t len; 205 + 206 + if (!pos->hit) 207 + continue; 208 + len = pos->long_name_len + 1; 209 + len = ALIGN(len, NAME_ALIGN); 210 + memset(&b, 0, sizeof(b)); 211 + memcpy(&b.build_id, pos->build_id, sizeof(pos->build_id)); 212 + b.pid = pid; 213 + b.header.misc = misc; 214 + b.header.size = sizeof(b) + len; 215 + err = do_write(fd, &b, sizeof(b)); 216 + if (err < 0) 217 + return err; 218 + err = write_padded(fd, pos->long_name, 219 + pos->long_name_len + 1, len); 220 + if (err < 0) 221 + return err; 222 + } 223 + 224 + return 0; 225 + } 226 + 227 + static int machine__write_buildid_table(struct machine *machine, int fd) 228 + { 229 + int err; 230 + u16 kmisc = PERF_RECORD_MISC_KERNEL, 231 + umisc = PERF_RECORD_MISC_USER; 232 + 233 + if (!machine__is_host(machine)) { 234 + kmisc = PERF_RECORD_MISC_GUEST_KERNEL; 235 + umisc = PERF_RECORD_MISC_GUEST_USER; 236 + } 237 + 238 + err = __dsos__write_buildid_table(&machine->kernel_dsos, machine->pid, 239 + kmisc, fd); 240 + if (err == 0) 241 + err = __dsos__write_buildid_table(&machine->user_dsos, 242 + machine->pid, umisc, fd); 243 + return err; 244 + } 245 + 246 + static int dsos__write_buildid_table(struct perf_header *header, int fd) 247 + { 248 + struct perf_session *session = container_of(header, 249 + struct perf_session, header); 250 + struct rb_node *nd; 251 + int err = machine__write_buildid_table(&session->host_machine, fd); 252 + 253 + if (err) 254 + return err; 255 + 256 + for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) { 257 + struct machine *pos = rb_entry(nd, struct machine, rb_node); 258 + err = machine__write_buildid_table(pos, fd); 259 + if (err) 260 + break; 261 + } 262 + return err; 263 + } 264 + 265 + int build_id_cache__add_s(const char *sbuild_id, const char *debugdir, 266 + const char *name, bool is_kallsyms) 267 + { 268 + const size_t size = PATH_MAX; 269 + char *realname, *filename = zalloc(size), 270 + *linkname = zalloc(size), *targetname; 271 + int len, err = -1; 272 + 273 + if (is_kallsyms) { 274 + if (symbol_conf.kptr_restrict) { 275 + pr_debug("Not caching a kptr_restrict'ed /proc/kallsyms\n"); 276 + return 0; 277 + } 278 + realname = (char *)name; 279 + } else 280 + realname = realpath(name, NULL); 281 + 282 + if (realname == NULL || filename == NULL || linkname == NULL) 283 + goto out_free; 284 + 285 + len = snprintf(filename, size, "%s%s%s", 286 + debugdir, is_kallsyms ? "/" : "", realname); 287 + if (mkdir_p(filename, 0755)) 288 + goto out_free; 289 + 290 + snprintf(filename + len, sizeof(filename) - len, "/%s", sbuild_id); 291 + 292 + if (access(filename, F_OK)) { 293 + if (is_kallsyms) { 294 + if (copyfile("/proc/kallsyms", filename)) 295 + goto out_free; 296 + } else if (link(realname, filename) && copyfile(name, filename)) 297 + goto out_free; 298 + } 299 + 300 + len = snprintf(linkname, size, "%s/.build-id/%.2s", 301 + debugdir, sbuild_id); 302 + 303 + if (access(linkname, X_OK) && mkdir_p(linkname, 0755)) 304 + goto out_free; 305 + 306 + snprintf(linkname + len, size - len, "/%s", sbuild_id + 2); 307 + targetname = filename + strlen(debugdir) - 5; 308 + memcpy(targetname, "../..", 5); 309 + 310 + if (symlink(targetname, linkname) == 0) 311 + err = 0; 312 + out_free: 313 + if (!is_kallsyms) 314 + free(realname); 315 + free(filename); 316 + free(linkname); 317 + return err; 318 + } 319 + 320 + static int build_id_cache__add_b(const u8 *build_id, size_t build_id_size, 321 + const char *name, const char *debugdir, 322 + bool is_kallsyms) 323 + { 324 + char sbuild_id[BUILD_ID_SIZE * 2 + 1]; 325 + 326 + build_id__sprintf(build_id, build_id_size, sbuild_id); 327 + 328 + return build_id_cache__add_s(sbuild_id, debugdir, name, is_kallsyms); 329 + } 330 + 331 + int build_id_cache__remove_s(const char *sbuild_id, const char *debugdir) 332 + { 333 + const size_t size = PATH_MAX; 334 + char *filename = zalloc(size), 335 + *linkname = zalloc(size); 336 + int err = -1; 337 + 338 + if (filename == NULL || linkname == NULL) 339 + goto out_free; 340 + 341 + snprintf(linkname, size, "%s/.build-id/%.2s/%s", 342 + debugdir, sbuild_id, sbuild_id + 2); 343 + 344 + if (access(linkname, F_OK)) 345 + goto out_free; 346 + 347 + if (readlink(linkname, filename, size - 1) < 0) 348 + goto out_free; 349 + 350 + if (unlink(linkname)) 351 + goto out_free; 352 + 353 + /* 354 + * Since the link is relative, we must make it absolute: 355 + */ 356 + snprintf(linkname, size, "%s/.build-id/%.2s/%s", 357 + debugdir, sbuild_id, filename); 358 + 359 + if (unlink(linkname)) 360 + goto out_free; 361 + 362 + err = 0; 363 + out_free: 364 + free(filename); 365 + free(linkname); 366 + return err; 367 + } 368 + 369 + static int dso__cache_build_id(struct dso *dso, const char *debugdir) 370 + { 371 + bool is_kallsyms = dso->kernel && dso->long_name[0] != '/'; 372 + 373 + return build_id_cache__add_b(dso->build_id, sizeof(dso->build_id), 374 + dso->long_name, debugdir, is_kallsyms); 375 + } 376 + 377 + static int __dsos__cache_build_ids(struct list_head *head, const char *debugdir) 378 + { 379 + struct dso *pos; 380 + int err = 0; 381 + 382 + dsos__for_each_with_build_id(pos, head) 383 + if (dso__cache_build_id(pos, debugdir)) 384 + err = -1; 385 + 386 + return err; 387 + } 388 + 389 + static int machine__cache_build_ids(struct machine *machine, const char *debugdir) 390 + { 391 + int ret = __dsos__cache_build_ids(&machine->kernel_dsos, debugdir); 392 + ret |= __dsos__cache_build_ids(&machine->user_dsos, debugdir); 393 + return ret; 394 + } 395 + 396 + static int perf_session__cache_build_ids(struct perf_session *session) 397 + { 398 + struct rb_node *nd; 399 + int ret; 400 + char debugdir[PATH_MAX]; 401 + 402 + snprintf(debugdir, sizeof(debugdir), "%s", buildid_dir); 403 + 404 + if (mkdir(debugdir, 0755) != 0 && errno != EEXIST) 405 + return -1; 406 + 407 + ret = machine__cache_build_ids(&session->host_machine, debugdir); 408 + 409 + for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) { 410 + struct machine *pos = rb_entry(nd, struct machine, rb_node); 411 + ret |= machine__cache_build_ids(pos, debugdir); 412 + } 413 + return ret ? -1 : 0; 414 + } 415 + 416 + static bool machine__read_build_ids(struct machine *machine, bool with_hits) 417 + { 418 + bool ret = __dsos__read_build_ids(&machine->kernel_dsos, with_hits); 419 + ret |= __dsos__read_build_ids(&machine->user_dsos, with_hits); 420 + return ret; 421 + } 422 + 423 + static bool perf_session__read_build_ids(struct perf_session *session, bool with_hits) 424 + { 425 + struct rb_node *nd; 426 + bool ret = machine__read_build_ids(&session->host_machine, with_hits); 427 + 428 + for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) { 429 + struct machine *pos = rb_entry(nd, struct machine, rb_node); 430 + ret |= machine__read_build_ids(pos, with_hits); 431 + } 432 + 433 + return ret; 185 434 } 186 435 187 436 static int write_trace_info(int fd, struct perf_header *h __used, ··· 1377 1134 perf_header__process_sections(header, fd, &hd, 1378 1135 perf_file_section__fprintf_info); 1379 1136 return 0; 1380 - } 1381 - 1382 - #define dsos__for_each_with_build_id(pos, head) \ 1383 - list_for_each_entry(pos, head, node) \ 1384 - if (!pos->has_build_id) \ 1385 - continue; \ 1386 - else 1387 - 1388 - static int __dsos__write_buildid_table(struct list_head *head, pid_t pid, 1389 - u16 misc, int fd) 1390 - { 1391 - struct dso *pos; 1392 - 1393 - dsos__for_each_with_build_id(pos, head) { 1394 - int err; 1395 - struct build_id_event b; 1396 - size_t len; 1397 - 1398 - if (!pos->hit) 1399 - continue; 1400 - len = pos->long_name_len + 1; 1401 - len = ALIGN(len, NAME_ALIGN); 1402 - memset(&b, 0, sizeof(b)); 1403 - memcpy(&b.build_id, pos->build_id, sizeof(pos->build_id)); 1404 - b.pid = pid; 1405 - b.header.misc = misc; 1406 - b.header.size = sizeof(b) + len; 1407 - err = do_write(fd, &b, sizeof(b)); 1408 - if (err < 0) 1409 - return err; 1410 - err = write_padded(fd, pos->long_name, 1411 - pos->long_name_len + 1, len); 1412 - if (err < 0) 1413 - return err; 1414 - } 1415 - 1416 - return 0; 1417 - } 1418 - 1419 - static int machine__write_buildid_table(struct machine *machine, int fd) 1420 - { 1421 - int err; 1422 - u16 kmisc = PERF_RECORD_MISC_KERNEL, 1423 - umisc = PERF_RECORD_MISC_USER; 1424 - 1425 - if (!machine__is_host(machine)) { 1426 - kmisc = PERF_RECORD_MISC_GUEST_KERNEL; 1427 - umisc = PERF_RECORD_MISC_GUEST_USER; 1428 - } 1429 - 1430 - err = __dsos__write_buildid_table(&machine->kernel_dsos, machine->pid, 1431 - kmisc, fd); 1432 - if (err == 0) 1433 - err = __dsos__write_buildid_table(&machine->user_dsos, 1434 - machine->pid, umisc, fd); 1435 - return err; 1436 - } 1437 - 1438 - static int dsos__write_buildid_table(struct perf_header *header, int fd) 1439 - { 1440 - struct perf_session *session = container_of(header, 1441 - struct perf_session, header); 1442 - struct rb_node *nd; 1443 - int err = machine__write_buildid_table(&session->host_machine, fd); 1444 - 1445 - if (err) 1446 - return err; 1447 - 1448 - for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) { 1449 - struct machine *pos = rb_entry(nd, struct machine, rb_node); 1450 - err = machine__write_buildid_table(pos, fd); 1451 - if (err) 1452 - break; 1453 - } 1454 - return err; 1455 - } 1456 - 1457 - int build_id_cache__add_s(const char *sbuild_id, const char *debugdir, 1458 - const char *name, bool is_kallsyms) 1459 - { 1460 - const size_t size = PATH_MAX; 1461 - char *realname, *filename = zalloc(size), 1462 - *linkname = zalloc(size), *targetname; 1463 - int len, err = -1; 1464 - 1465 - if (is_kallsyms) { 1466 - if (symbol_conf.kptr_restrict) { 1467 - pr_debug("Not caching a kptr_restrict'ed /proc/kallsyms\n"); 1468 - return 0; 1469 - } 1470 - realname = (char *)name; 1471 - } else 1472 - realname = realpath(name, NULL); 1473 - 1474 - if (realname == NULL || filename == NULL || linkname == NULL) 1475 - goto out_free; 1476 - 1477 - len = snprintf(filename, size, "%s%s%s", 1478 - debugdir, is_kallsyms ? "/" : "", realname); 1479 - if (mkdir_p(filename, 0755)) 1480 - goto out_free; 1481 - 1482 - snprintf(filename + len, sizeof(filename) - len, "/%s", sbuild_id); 1483 - 1484 - if (access(filename, F_OK)) { 1485 - if (is_kallsyms) { 1486 - if (copyfile("/proc/kallsyms", filename)) 1487 - goto out_free; 1488 - } else if (link(realname, filename) && copyfile(name, filename)) 1489 - goto out_free; 1490 - } 1491 - 1492 - len = snprintf(linkname, size, "%s/.build-id/%.2s", 1493 - debugdir, sbuild_id); 1494 - 1495 - if (access(linkname, X_OK) && mkdir_p(linkname, 0755)) 1496 - goto out_free; 1497 - 1498 - snprintf(linkname + len, size - len, "/%s", sbuild_id + 2); 1499 - targetname = filename + strlen(debugdir) - 5; 1500 - memcpy(targetname, "../..", 5); 1501 - 1502 - if (symlink(targetname, linkname) == 0) 1503 - err = 0; 1504 - out_free: 1505 - if (!is_kallsyms) 1506 - free(realname); 1507 - free(filename); 1508 - free(linkname); 1509 - return err; 1510 - } 1511 - 1512 - static int build_id_cache__add_b(const u8 *build_id, size_t build_id_size, 1513 - const char *name, const char *debugdir, 1514 - bool is_kallsyms) 1515 - { 1516 - char sbuild_id[BUILD_ID_SIZE * 2 + 1]; 1517 - 1518 - build_id__sprintf(build_id, build_id_size, sbuild_id); 1519 - 1520 - return build_id_cache__add_s(sbuild_id, debugdir, name, is_kallsyms); 1521 - } 1522 - 1523 - int build_id_cache__remove_s(const char *sbuild_id, const char *debugdir) 1524 - { 1525 - const size_t size = PATH_MAX; 1526 - char *filename = zalloc(size), 1527 - *linkname = zalloc(size); 1528 - int err = -1; 1529 - 1530 - if (filename == NULL || linkname == NULL) 1531 - goto out_free; 1532 - 1533 - snprintf(linkname, size, "%s/.build-id/%.2s/%s", 1534 - debugdir, sbuild_id, sbuild_id + 2); 1535 - 1536 - if (access(linkname, F_OK)) 1537 - goto out_free; 1538 - 1539 - if (readlink(linkname, filename, size - 1) < 0) 1540 - goto out_free; 1541 - 1542 - if (unlink(linkname)) 1543 - goto out_free; 1544 - 1545 - /* 1546 - * Since the link is relative, we must make it absolute: 1547 - */ 1548 - snprintf(linkname, size, "%s/.build-id/%.2s/%s", 1549 - debugdir, sbuild_id, filename); 1550 - 1551 - if (unlink(linkname)) 1552 - goto out_free; 1553 - 1554 - err = 0; 1555 - out_free: 1556 - free(filename); 1557 - free(linkname); 1558 - return err; 1559 - } 1560 - 1561 - static int dso__cache_build_id(struct dso *dso, const char *debugdir) 1562 - { 1563 - bool is_kallsyms = dso->kernel && dso->long_name[0] != '/'; 1564 - 1565 - return build_id_cache__add_b(dso->build_id, sizeof(dso->build_id), 1566 - dso->long_name, debugdir, is_kallsyms); 1567 - } 1568 - 1569 - static int __dsos__cache_build_ids(struct list_head *head, const char *debugdir) 1570 - { 1571 - struct dso *pos; 1572 - int err = 0; 1573 - 1574 - dsos__for_each_with_build_id(pos, head) 1575 - if (dso__cache_build_id(pos, debugdir)) 1576 - err = -1; 1577 - 1578 - return err; 1579 - } 1580 - 1581 - static int machine__cache_build_ids(struct machine *machine, const char *debugdir) 1582 - { 1583 - int ret = __dsos__cache_build_ids(&machine->kernel_dsos, debugdir); 1584 - ret |= __dsos__cache_build_ids(&machine->user_dsos, debugdir); 1585 - return ret; 1586 - } 1587 - 1588 - static int perf_session__cache_build_ids(struct perf_session *session) 1589 - { 1590 - struct rb_node *nd; 1591 - int ret; 1592 - char debugdir[PATH_MAX]; 1593 - 1594 - snprintf(debugdir, sizeof(debugdir), "%s", buildid_dir); 1595 - 1596 - if (mkdir(debugdir, 0755) != 0 && errno != EEXIST) 1597 - return -1; 1598 - 1599 - ret = machine__cache_build_ids(&session->host_machine, debugdir); 1600 - 1601 - for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) { 1602 - struct machine *pos = rb_entry(nd, struct machine, rb_node); 1603 - ret |= machine__cache_build_ids(pos, debugdir); 1604 - } 1605 - return ret ? -1 : 0; 1606 - } 1607 - 1608 - static bool machine__read_build_ids(struct machine *machine, bool with_hits) 1609 - { 1610 - bool ret = __dsos__read_build_ids(&machine->kernel_dsos, with_hits); 1611 - ret |= __dsos__read_build_ids(&machine->user_dsos, with_hits); 1612 - return ret; 1613 - } 1614 - 1615 - static bool perf_session__read_build_ids(struct perf_session *session, bool with_hits) 1616 - { 1617 - struct rb_node *nd; 1618 - bool ret = machine__read_build_ids(&session->host_machine, with_hits); 1619 - 1620 - for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) { 1621 - struct machine *pos = rb_entry(nd, struct machine, rb_node); 1622 - ret |= machine__read_build_ids(pos, with_hits); 1623 - } 1624 - 1625 - return ret; 1626 1137 } 1627 1138 1628 1139 static int do_write_feat(int fd, struct perf_header *h, int type,