builtin-buildid-list.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * builtin-buildid-list.c
  3. *
  4. * Builtin buildid-list command: list buildids in perf.data, in the running
  5. * kernel and in ELF files.
  6. *
  7. * Copyright (C) 2009, Red Hat Inc.
  8. * Copyright (C) 2009, Arnaldo Carvalho de Melo <acme@redhat.com>
  9. */
  10. #include "builtin.h"
  11. #include "perf.h"
  12. #include "util/build-id.h"
  13. #include "util/cache.h"
  14. #include "util/debug.h"
  15. #include "util/parse-options.h"
  16. #include "util/session.h"
  17. #include "util/symbol.h"
  18. #include "util/data.h"
  19. static int sysfs__fprintf_build_id(FILE *fp)
  20. {
  21. u8 kallsyms_build_id[BUILD_ID_SIZE];
  22. char sbuild_id[BUILD_ID_SIZE * 2 + 1];
  23. if (sysfs__read_build_id("/sys/kernel/notes", kallsyms_build_id,
  24. sizeof(kallsyms_build_id)) != 0)
  25. return -1;
  26. build_id__sprintf(kallsyms_build_id, sizeof(kallsyms_build_id),
  27. sbuild_id);
  28. fprintf(fp, "%s\n", sbuild_id);
  29. return 0;
  30. }
  31. static int filename__fprintf_build_id(const char *name, FILE *fp)
  32. {
  33. u8 build_id[BUILD_ID_SIZE];
  34. char sbuild_id[BUILD_ID_SIZE * 2 + 1];
  35. if (filename__read_build_id(name, build_id,
  36. sizeof(build_id)) != sizeof(build_id))
  37. return 0;
  38. build_id__sprintf(build_id, sizeof(build_id), sbuild_id);
  39. return fprintf(fp, "%s\n", sbuild_id);
  40. }
  41. static bool dso__skip_buildid(struct dso *dso, int with_hits)
  42. {
  43. return with_hits && !dso->hit;
  44. }
  45. static int perf_session__list_build_ids(bool force, bool with_hits)
  46. {
  47. struct perf_session *session;
  48. struct perf_data_file file = {
  49. .path = input_name,
  50. .mode = PERF_DATA_MODE_READ,
  51. .force = force,
  52. };
  53. symbol__elf_init();
  54. /*
  55. * See if this is an ELF file first:
  56. */
  57. if (filename__fprintf_build_id(input_name, stdout))
  58. goto out;
  59. session = perf_session__new(&file, false, &build_id__mark_dso_hit_ops);
  60. if (session == NULL)
  61. return -1;
  62. /*
  63. * We take all buildids when the file contains AUX area tracing data
  64. * because we do not decode the trace because it would take too long.
  65. */
  66. if (!perf_data_file__is_pipe(&file) &&
  67. perf_header__has_feat(&session->header, HEADER_AUXTRACE))
  68. with_hits = false;
  69. /*
  70. * in pipe-mode, the only way to get the buildids is to parse
  71. * the record stream. Buildids are stored as RECORD_HEADER_BUILD_ID
  72. */
  73. if (with_hits || perf_data_file__is_pipe(&file))
  74. perf_session__process_events(session);
  75. perf_session__fprintf_dsos_buildid(session, stdout, dso__skip_buildid, with_hits);
  76. perf_session__delete(session);
  77. out:
  78. return 0;
  79. }
  80. int cmd_buildid_list(int argc, const char **argv,
  81. const char *prefix __maybe_unused)
  82. {
  83. bool show_kernel = false;
  84. bool with_hits = false;
  85. bool force = false;
  86. const struct option options[] = {
  87. OPT_BOOLEAN('H', "with-hits", &with_hits, "Show only DSOs with hits"),
  88. OPT_STRING('i', "input", &input_name, "file", "input file name"),
  89. OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
  90. OPT_BOOLEAN('k', "kernel", &show_kernel, "Show current kernel build id"),
  91. OPT_INCR('v', "verbose", &verbose, "be more verbose"),
  92. OPT_END()
  93. };
  94. const char * const buildid_list_usage[] = {
  95. "perf buildid-list [<options>]",
  96. NULL
  97. };
  98. argc = parse_options(argc, argv, options, buildid_list_usage, 0);
  99. setup_pager();
  100. if (show_kernel)
  101. return sysfs__fprintf_build_id(stdout);
  102. return perf_session__list_build_ids(force, with_hits);
  103. }