builtin-buildid-list.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 "util/build-id.h"
  12. #include "util/debug.h"
  13. #include "util/dso.h"
  14. #include "util/map.h"
  15. #include <subcmd/pager.h>
  16. #include <subcmd/parse-options.h>
  17. #include "util/session.h"
  18. #include "util/symbol.h"
  19. #include "util/data.h"
  20. #include "util/util.h"
  21. #include <errno.h>
  22. #include <inttypes.h>
  23. #include <linux/err.h>
  24. static int buildid__map_cb(struct map *map, void *arg __maybe_unused)
  25. {
  26. const struct dso *dso = map__dso(map);
  27. char bid_buf[SBUILD_ID_SIZE];
  28. const char *dso_long_name = dso__long_name(dso);
  29. const char *dso_short_name = dso__short_name(dso);
  30. memset(bid_buf, 0, sizeof(bid_buf));
  31. if (dso__has_build_id(dso))
  32. build_id__sprintf(dso__bid_const(dso), bid_buf);
  33. printf("%s %16" PRIx64 " %16" PRIx64, bid_buf, map__start(map), map__end(map));
  34. if (dso_long_name != NULL)
  35. printf(" %s", dso_long_name);
  36. else if (dso_short_name != NULL)
  37. printf(" %s", dso_short_name);
  38. printf("\n");
  39. return 0;
  40. }
  41. static void buildid__show_kernel_maps(void)
  42. {
  43. struct machine *machine;
  44. machine = machine__new_host();
  45. machine__for_each_kernel_map(machine, buildid__map_cb, NULL);
  46. machine__delete(machine);
  47. }
  48. static int sysfs__fprintf_build_id(FILE *fp)
  49. {
  50. char sbuild_id[SBUILD_ID_SIZE];
  51. int ret;
  52. ret = sysfs__sprintf_build_id("/", sbuild_id);
  53. if (ret != sizeof(sbuild_id))
  54. return ret < 0 ? ret : -EINVAL;
  55. return fprintf(fp, "%s\n", sbuild_id);
  56. }
  57. static int filename__fprintf_build_id(const char *name, FILE *fp)
  58. {
  59. char sbuild_id[SBUILD_ID_SIZE];
  60. int ret;
  61. ret = filename__sprintf_build_id(name, sbuild_id);
  62. if (ret != sizeof(sbuild_id))
  63. return ret < 0 ? ret : -EINVAL;
  64. return fprintf(fp, "%s\n", sbuild_id);
  65. }
  66. static bool dso__skip_buildid(struct dso *dso, int with_hits)
  67. {
  68. return with_hits && !dso__hit(dso);
  69. }
  70. static int perf_session__list_build_ids(bool force, bool with_hits)
  71. {
  72. struct perf_session *session;
  73. struct perf_data data = {
  74. .path = input_name,
  75. .mode = PERF_DATA_MODE_READ,
  76. .force = force,
  77. };
  78. struct perf_tool build_id__mark_dso_hit_ops;
  79. symbol__elf_init();
  80. /*
  81. * See if this is an ELF file first:
  82. */
  83. if (filename__fprintf_build_id(input_name, stdout) > 0)
  84. goto out;
  85. perf_tool__init(&build_id__mark_dso_hit_ops, /*ordered_events=*/true);
  86. build_id__mark_dso_hit_ops.sample = build_id__mark_dso_hit;
  87. build_id__mark_dso_hit_ops.mmap = perf_event__process_mmap;
  88. build_id__mark_dso_hit_ops.mmap2 = perf_event__process_mmap2;
  89. build_id__mark_dso_hit_ops.fork = perf_event__process_fork;
  90. build_id__mark_dso_hit_ops.exit = perf_event__exit_del_thread;
  91. build_id__mark_dso_hit_ops.attr = perf_event__process_attr;
  92. build_id__mark_dso_hit_ops.build_id = perf_event__process_build_id;
  93. session = perf_session__new(&data, &build_id__mark_dso_hit_ops);
  94. if (IS_ERR(session))
  95. return PTR_ERR(session);
  96. /*
  97. * We take all buildids when the file contains AUX area tracing data
  98. * because we do not decode the trace because it would take too long.
  99. */
  100. if (!perf_data__is_pipe(&data) &&
  101. perf_header__has_feat(&session->header, HEADER_AUXTRACE))
  102. with_hits = false;
  103. if (!perf_header__has_feat(&session->header, HEADER_BUILD_ID))
  104. with_hits = true;
  105. if (zstd_init(&(session->zstd_data), 0) < 0)
  106. pr_warning("Decompression initialization failed. Reported data may be incomplete.\n");
  107. /*
  108. * in pipe-mode, the only way to get the buildids is to parse
  109. * the record stream. Buildids are stored as RECORD_HEADER_BUILD_ID
  110. */
  111. if (with_hits || perf_data__is_pipe(&data))
  112. perf_session__process_events(session);
  113. perf_session__fprintf_dsos_buildid(session, stdout, dso__skip_buildid, with_hits);
  114. perf_session__delete(session);
  115. out:
  116. return 0;
  117. }
  118. int cmd_buildid_list(int argc, const char **argv)
  119. {
  120. bool show_kernel = false;
  121. bool show_kernel_maps = false;
  122. bool with_hits = false;
  123. bool force = false;
  124. const struct option options[] = {
  125. OPT_BOOLEAN('H', "with-hits", &with_hits, "Show only DSOs with hits"),
  126. OPT_STRING('i', "input", &input_name, "file", "input file name"),
  127. OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
  128. OPT_BOOLEAN('k', "kernel", &show_kernel, "Show current kernel build id"),
  129. OPT_BOOLEAN('m', "kernel-maps", &show_kernel_maps,
  130. "Show build id of current kernel + modules"),
  131. OPT_INCR('v', "verbose", &verbose, "be more verbose"),
  132. OPT_END()
  133. };
  134. const char * const buildid_list_usage[] = {
  135. "perf buildid-list [<options>]",
  136. NULL
  137. };
  138. argc = parse_options(argc, argv, options, buildid_list_usage, 0);
  139. setup_pager();
  140. if (show_kernel) {
  141. return !(sysfs__fprintf_build_id(stdout) > 0);
  142. } else if (show_kernel_maps) {
  143. buildid__show_kernel_maps();
  144. return 0;
  145. }
  146. return perf_session__list_build_ids(force, with_hits);
  147. }