builtin-buildid-list.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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(bool force, bool with_hits)
  41. {
  42. struct perf_session *session;
  43. symbol__elf_init();
  44. session = perf_session__new(input_name, O_RDONLY, force, false,
  45. &build_id__mark_dso_hit_ops);
  46. if (session == NULL)
  47. return -1;
  48. /*
  49. * See if this is an ELF file first:
  50. */
  51. if (filename__fprintf_build_id(session->filename, stdout))
  52. goto out;
  53. /*
  54. * in pipe-mode, the only way to get the buildids is to parse
  55. * the record stream. Buildids are stored as RECORD_HEADER_BUILD_ID
  56. */
  57. if (with_hits || session->fd_pipe)
  58. perf_session__process_events(session, &build_id__mark_dso_hit_ops);
  59. perf_session__fprintf_dsos_buildid(session, stdout, with_hits);
  60. out:
  61. perf_session__delete(session);
  62. return 0;
  63. }
  64. int cmd_buildid_list(int argc, const char **argv,
  65. const char *prefix __maybe_unused)
  66. {
  67. bool show_kernel = false;
  68. bool with_hits = false;
  69. bool force = false;
  70. const struct option options[] = {
  71. OPT_BOOLEAN('H', "with-hits", &with_hits, "Show only DSOs with hits"),
  72. OPT_STRING('i', "input", &input_name, "file", "input file name"),
  73. OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
  74. OPT_BOOLEAN('k', "kernel", &show_kernel, "Show current kernel build id"),
  75. OPT_INCR('v', "verbose", &verbose, "be more verbose"),
  76. OPT_END()
  77. };
  78. const char * const buildid_list_usage[] = {
  79. "perf buildid-list [<options>]",
  80. NULL
  81. };
  82. argc = parse_options(argc, argv, options, buildid_list_usage, 0);
  83. setup_pager();
  84. if (show_kernel)
  85. return sysfs__fprintf_build_id(stdout);
  86. return perf_session__list_build_ids(force, with_hits);
  87. }