builtin-buildid-cache.c 3.1 KB

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