builtin-buildid-list.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * builtin-buildid-list.c
  3. *
  4. * Builtin buildid-list command: list buildids in perf.data
  5. *
  6. * Copyright (C) 2009, Red Hat Inc.
  7. * Copyright (C) 2009, Arnaldo Carvalho de Melo <acme@redhat.com>
  8. */
  9. #include "builtin.h"
  10. #include "perf.h"
  11. #include "util/cache.h"
  12. #include "util/data_map.h"
  13. #include "util/debug.h"
  14. #include "util/header.h"
  15. #include "util/parse-options.h"
  16. #include "util/symbol.h"
  17. static char const *input_name = "perf.data";
  18. static int force;
  19. static const char *const buildid_list_usage[] = {
  20. "perf buildid-list [<options>]",
  21. NULL
  22. };
  23. static const struct option options[] = {
  24. OPT_STRING('i', "input", &input_name, "file",
  25. "input file name"),
  26. OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
  27. OPT_BOOLEAN('v', "verbose", &verbose,
  28. "be more verbose"),
  29. OPT_END()
  30. };
  31. static int perf_file_section__process_buildids(struct perf_file_section *self,
  32. int feat, int fd)
  33. {
  34. if (feat != HEADER_BUILD_ID)
  35. return 0;
  36. if (lseek(fd, self->offset, SEEK_SET) < 0) {
  37. pr_warning("Failed to lseek to %Ld offset for buildids!\n",
  38. self->offset);
  39. return -1;
  40. }
  41. if (perf_header__read_build_ids(fd, self->offset, self->size)) {
  42. pr_warning("Failed to read buildids!\n");
  43. return -1;
  44. }
  45. return 0;
  46. }
  47. static int __cmd_buildid_list(void)
  48. {
  49. int err = -1;
  50. struct perf_header *header;
  51. struct perf_file_header f_header;
  52. struct stat input_stat;
  53. int input = open(input_name, O_RDONLY);
  54. if (input < 0) {
  55. pr_err("failed to open file: %s", input_name);
  56. if (!strcmp(input_name, "perf.data"))
  57. pr_err(" (try 'perf record' first)");
  58. pr_err("\n");
  59. goto out;
  60. }
  61. err = fstat(input, &input_stat);
  62. if (err < 0) {
  63. perror("failed to stat file");
  64. goto out_close;
  65. }
  66. if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) {
  67. pr_err("file %s not owned by current user or root\n",
  68. input_name);
  69. goto out_close;
  70. }
  71. if (!input_stat.st_size) {
  72. pr_info("zero-sized file, nothing to do!\n");
  73. goto out_close;
  74. }
  75. err = -1;
  76. header = perf_header__new();
  77. if (header == NULL)
  78. goto out_close;
  79. if (perf_file_header__read(&f_header, header, input) < 0) {
  80. pr_warning("incompatible file format");
  81. goto out_close;
  82. }
  83. err = perf_header__process_sections(header, input,
  84. perf_file_section__process_buildids);
  85. if (err < 0)
  86. goto out_close;
  87. dsos__fprintf_buildid(stdout);
  88. out_close:
  89. close(input);
  90. out:
  91. return err;
  92. }
  93. int cmd_buildid_list(int argc, const char **argv, const char *prefix __used)
  94. {
  95. argc = parse_options(argc, argv, options, buildid_list_usage, 0);
  96. setup_pager();
  97. return __cmd_buildid_list();
  98. }