builtin-buildid-cache.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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/symbol.h"
  17. static char const *add_name_list_str, *remove_name_list_str;
  18. static const char * const buildid_cache_usage[] = {
  19. "perf buildid-cache [<options>]",
  20. NULL
  21. };
  22. static const struct option buildid_cache_options[] = {
  23. OPT_STRING('a', "add", &add_name_list_str,
  24. "file list", "file(s) to add"),
  25. OPT_STRING('r', "remove", &remove_name_list_str, "file list",
  26. "file(s) to remove"),
  27. OPT_INCR('v', "verbose", &verbose, "be more verbose"),
  28. OPT_END()
  29. };
  30. static int build_id_cache__add_file(const char *filename, const char *debugdir)
  31. {
  32. char sbuild_id[BUILD_ID_SIZE * 2 + 1];
  33. u8 build_id[BUILD_ID_SIZE];
  34. int err;
  35. if (filename__read_build_id(filename, &build_id, sizeof(build_id)) < 0) {
  36. pr_debug("Couldn't read a build-id in %s\n", filename);
  37. return -1;
  38. }
  39. build_id__sprintf(build_id, sizeof(build_id), sbuild_id);
  40. err = build_id_cache__add_s(sbuild_id, debugdir, filename,
  41. false, false);
  42. if (verbose)
  43. pr_info("Adding %s %s: %s\n", sbuild_id, filename,
  44. err ? "FAIL" : "Ok");
  45. return err;
  46. }
  47. static int build_id_cache__remove_file(const char *filename __maybe_unused,
  48. const char *debugdir __maybe_unused)
  49. {
  50. u8 build_id[BUILD_ID_SIZE];
  51. char sbuild_id[BUILD_ID_SIZE * 2 + 1];
  52. int err;
  53. if (filename__read_build_id(filename, &build_id, sizeof(build_id)) < 0) {
  54. pr_debug("Couldn't read a build-id in %s\n", filename);
  55. return -1;
  56. }
  57. build_id__sprintf(build_id, sizeof(build_id), sbuild_id);
  58. err = build_id_cache__remove_s(sbuild_id, debugdir);
  59. if (verbose)
  60. pr_info("Removing %s %s: %s\n", sbuild_id, filename,
  61. err ? "FAIL" : "Ok");
  62. return err;
  63. }
  64. static int __cmd_buildid_cache(void)
  65. {
  66. struct strlist *list;
  67. struct str_node *pos;
  68. char debugdir[PATH_MAX];
  69. snprintf(debugdir, sizeof(debugdir), "%s", buildid_dir);
  70. if (add_name_list_str) {
  71. list = strlist__new(true, add_name_list_str);
  72. if (list) {
  73. strlist__for_each(pos, list)
  74. if (build_id_cache__add_file(pos->s, debugdir)) {
  75. if (errno == EEXIST) {
  76. pr_debug("%s already in the cache\n",
  77. pos->s);
  78. continue;
  79. }
  80. pr_warning("Couldn't add %s: %s\n",
  81. pos->s, strerror(errno));
  82. }
  83. strlist__delete(list);
  84. }
  85. }
  86. if (remove_name_list_str) {
  87. list = strlist__new(true, remove_name_list_str);
  88. if (list) {
  89. strlist__for_each(pos, list)
  90. if (build_id_cache__remove_file(pos->s, debugdir)) {
  91. if (errno == ENOENT) {
  92. pr_debug("%s wasn't in the cache\n",
  93. pos->s);
  94. continue;
  95. }
  96. pr_warning("Couldn't remove %s: %s\n",
  97. pos->s, strerror(errno));
  98. }
  99. strlist__delete(list);
  100. }
  101. }
  102. return 0;
  103. }
  104. int cmd_buildid_cache(int argc, const char **argv,
  105. const char *prefix __maybe_unused)
  106. {
  107. argc = parse_options(argc, argv, buildid_cache_options,
  108. buildid_cache_usage, 0);
  109. if (symbol__init() < 0)
  110. return -1;
  111. setup_pager();
  112. return __cmd_buildid_cache();
  113. }