builtin-buildid-list.c 2.6 KB

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