builtin-buildid-list.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. * in pipe-mode, the only way to get the buildids is to parse
  64. * the record stream. Buildids are stored as RECORD_HEADER_BUILD_ID
  65. */
  66. if (with_hits || perf_data_file__is_pipe(&file))
  67. perf_session__process_events(session, &build_id__mark_dso_hit_ops);
  68. perf_session__fprintf_dsos_buildid(session, stdout, dso__skip_buildid, with_hits);
  69. perf_session__delete(session);
  70. out:
  71. return 0;
  72. }
  73. int cmd_buildid_list(int argc, const char **argv,
  74. const char *prefix __maybe_unused)
  75. {
  76. bool show_kernel = false;
  77. bool with_hits = false;
  78. bool force = false;
  79. const struct option options[] = {
  80. OPT_BOOLEAN('H', "with-hits", &with_hits, "Show only DSOs with hits"),
  81. OPT_STRING('i', "input", &input_name, "file", "input file name"),
  82. OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
  83. OPT_BOOLEAN('k', "kernel", &show_kernel, "Show current kernel build id"),
  84. OPT_INCR('v', "verbose", &verbose, "be more verbose"),
  85. OPT_END()
  86. };
  87. const char * const buildid_list_usage[] = {
  88. "perf buildid-list [<options>]",
  89. NULL
  90. };
  91. argc = parse_options(argc, argv, options, buildid_list_usage, 0);
  92. setup_pager();
  93. if (show_kernel)
  94. return sysfs__fprintf_build_id(stdout);
  95. return perf_session__list_build_ids(force, with_hits);
  96. }