builtin-buildid-cache.c 3.1 KB

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