builtin-buildid-list.c 4.9 KB

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