builtin-buildid-list.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. static int sysfs__fprintf_build_id(FILE *fp)
  19. {
  20. u8 kallsyms_build_id[BUILD_ID_SIZE];
  21. char sbuild_id[BUILD_ID_SIZE * 2 + 1];
  22. if (sysfs__read_build_id("/sys/kernel/notes", kallsyms_build_id,
  23. sizeof(kallsyms_build_id)) != 0)
  24. return -1;
  25. build_id__sprintf(kallsyms_build_id, sizeof(kallsyms_build_id),
  26. sbuild_id);
  27. fprintf(fp, "%s\n", sbuild_id);
  28. return 0;
  29. }
  30. static int filename__fprintf_build_id(const char *name, FILE *fp)
  31. {
  32. u8 build_id[BUILD_ID_SIZE];
  33. char sbuild_id[BUILD_ID_SIZE * 2 + 1];
  34. if (filename__read_build_id(name, build_id,
  35. sizeof(build_id)) != sizeof(build_id))
  36. return 0;
  37. build_id__sprintf(build_id, sizeof(build_id), sbuild_id);
  38. return fprintf(fp, "%s\n", sbuild_id);
  39. }
  40. static int perf_session__list_build_ids(const char *input_name,
  41. bool force, bool with_hits)
  42. {
  43. struct perf_session *session;
  44. symbol__elf_init();
  45. session = perf_session__new(input_name, O_RDONLY, force, false,
  46. &build_id__mark_dso_hit_ops);
  47. if (session == NULL)
  48. return -1;
  49. /*
  50. * See if this is an ELF file first:
  51. */
  52. if (filename__fprintf_build_id(session->filename, stdout))
  53. goto out;
  54. /*
  55. * in pipe-mode, the only way to get the buildids is to parse
  56. * the record stream. Buildids are stored as RECORD_HEADER_BUILD_ID
  57. */
  58. if (with_hits || session->fd_pipe)
  59. perf_session__process_events(session, &build_id__mark_dso_hit_ops);
  60. perf_session__fprintf_dsos_buildid(session, stdout, with_hits);
  61. out:
  62. perf_session__delete(session);
  63. return 0;
  64. }
  65. int cmd_buildid_list(int argc, const char **argv,
  66. const char *prefix __maybe_unused)
  67. {
  68. bool show_kernel = false;
  69. bool with_hits = false;
  70. bool force = false;
  71. const char *input_name = NULL;
  72. const struct option options[] = {
  73. OPT_BOOLEAN('H', "with-hits", &with_hits, "Show only DSOs with hits"),
  74. OPT_STRING('i', "input", &input_name, "file", "input file name"),
  75. OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
  76. OPT_BOOLEAN('k', "kernel", &show_kernel, "Show current kernel build id"),
  77. OPT_INCR('v', "verbose", &verbose, "be more verbose"),
  78. OPT_END()
  79. };
  80. const char * const buildid_list_usage[] = {
  81. "perf buildid-list [<options>]",
  82. NULL
  83. };
  84. argc = parse_options(argc, argv, options, buildid_list_usage, 0);
  85. setup_pager();
  86. if (show_kernel)
  87. return sysfs__fprintf_build_id(stdout);
  88. return perf_session__list_build_ids(input_name, force, with_hits);
  89. }