builtin-buildid-cache.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * builtin-buildid-cache.c
  3. *
  4. * Builtin buildid-cache command: Manages build-id cache
  5. *
  6. * Copyright (C) 2010, Red Hat Inc.
  7. * Copyright (C) 2010, Arnaldo Carvalho de Melo <acme@redhat.com>
  8. */
  9. #include "builtin.h"
  10. #include "perf.h"
  11. #include "util/cache.h"
  12. #include "util/debug.h"
  13. #include "util/header.h"
  14. #include "util/parse-options.h"
  15. #include "util/strlist.h"
  16. #include "util/build-id.h"
  17. #include "util/session.h"
  18. #include "util/symbol.h"
  19. static int build_id_cache__add_file(const char *filename, const char *debugdir)
  20. {
  21. char sbuild_id[BUILD_ID_SIZE * 2 + 1];
  22. u8 build_id[BUILD_ID_SIZE];
  23. int err;
  24. if (filename__read_build_id(filename, &build_id, sizeof(build_id)) < 0) {
  25. pr_debug("Couldn't read a build-id in %s\n", filename);
  26. return -1;
  27. }
  28. build_id__sprintf(build_id, sizeof(build_id), sbuild_id);
  29. err = build_id_cache__add_s(sbuild_id, debugdir, filename,
  30. false, false);
  31. if (verbose)
  32. pr_info("Adding %s %s: %s\n", sbuild_id, filename,
  33. err ? "FAIL" : "Ok");
  34. return err;
  35. }
  36. static int build_id_cache__remove_file(const char *filename,
  37. const char *debugdir)
  38. {
  39. u8 build_id[BUILD_ID_SIZE];
  40. char sbuild_id[BUILD_ID_SIZE * 2 + 1];
  41. int err;
  42. if (filename__read_build_id(filename, &build_id, sizeof(build_id)) < 0) {
  43. pr_debug("Couldn't read a build-id in %s\n", filename);
  44. return -1;
  45. }
  46. build_id__sprintf(build_id, sizeof(build_id), sbuild_id);
  47. err = build_id_cache__remove_s(sbuild_id, debugdir);
  48. if (verbose)
  49. pr_info("Removing %s %s: %s\n", sbuild_id, filename,
  50. err ? "FAIL" : "Ok");
  51. return err;
  52. }
  53. static bool dso__missing_buildid_cache(struct dso *dso, int parm __maybe_unused)
  54. {
  55. char filename[PATH_MAX];
  56. u8 build_id[BUILD_ID_SIZE];
  57. if (dso__build_id_filename(dso, filename, sizeof(filename)) &&
  58. filename__read_build_id(filename, build_id,
  59. sizeof(build_id)) != sizeof(build_id)) {
  60. if (errno == ENOENT)
  61. return false;
  62. pr_warning("Problems with %s file, consider removing it from the cache\n",
  63. filename);
  64. } else if (memcmp(dso->build_id, build_id, sizeof(dso->build_id))) {
  65. pr_warning("Problems with %s file, consider removing it from the cache\n",
  66. filename);
  67. }
  68. return true;
  69. }
  70. static int build_id_cache__fprintf_missing(const char *filename, bool force, FILE *fp)
  71. {
  72. struct perf_session *session = perf_session__new(filename, O_RDONLY,
  73. force, false, NULL);
  74. if (session == NULL)
  75. return -1;
  76. perf_session__fprintf_dsos_buildid(session, fp, dso__missing_buildid_cache, 0);
  77. perf_session__delete(session);
  78. return 0;
  79. }
  80. int cmd_buildid_cache(int argc, const char **argv,
  81. const char *prefix __maybe_unused)
  82. {
  83. struct strlist *list;
  84. struct str_node *pos;
  85. int ret = 0;
  86. bool force = false;
  87. char debugdir[PATH_MAX];
  88. char const *add_name_list_str = NULL,
  89. *remove_name_list_str = NULL,
  90. *missing_filename = NULL;
  91. const struct option buildid_cache_options[] = {
  92. OPT_STRING('a', "add", &add_name_list_str,
  93. "file list", "file(s) to add"),
  94. OPT_STRING('r', "remove", &remove_name_list_str, "file list",
  95. "file(s) to remove"),
  96. OPT_STRING('M', "missing", &missing_filename, "file",
  97. "to find missing build ids in the cache"),
  98. OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
  99. OPT_INCR('v', "verbose", &verbose, "be more verbose"),
  100. OPT_END()
  101. };
  102. const char * const buildid_cache_usage[] = {
  103. "perf buildid-cache [<options>]",
  104. NULL
  105. };
  106. argc = parse_options(argc, argv, buildid_cache_options,
  107. buildid_cache_usage, 0);
  108. if (symbol__init() < 0)
  109. return -1;
  110. setup_pager();
  111. snprintf(debugdir, sizeof(debugdir), "%s", buildid_dir);
  112. if (add_name_list_str) {
  113. list = strlist__new(true, add_name_list_str);
  114. if (list) {
  115. strlist__for_each(pos, list)
  116. if (build_id_cache__add_file(pos->s, debugdir)) {
  117. if (errno == EEXIST) {
  118. pr_debug("%s already in the cache\n",
  119. pos->s);
  120. continue;
  121. }
  122. pr_warning("Couldn't add %s: %s\n",
  123. pos->s, strerror(errno));
  124. }
  125. strlist__delete(list);
  126. }
  127. }
  128. if (remove_name_list_str) {
  129. list = strlist__new(true, remove_name_list_str);
  130. if (list) {
  131. strlist__for_each(pos, list)
  132. if (build_id_cache__remove_file(pos->s, debugdir)) {
  133. if (errno == ENOENT) {
  134. pr_debug("%s wasn't in the cache\n",
  135. pos->s);
  136. continue;
  137. }
  138. pr_warning("Couldn't remove %s: %s\n",
  139. pos->s, strerror(errno));
  140. }
  141. strlist__delete(list);
  142. }
  143. }
  144. if (missing_filename)
  145. ret = build_id_cache__fprintf_missing(missing_filename, force, stdout);
  146. return ret;
  147. }