builtin-buildid-cache.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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, false);
  41. if (verbose)
  42. pr_info("Adding %s %s: %s\n", sbuild_id, filename,
  43. err ? "FAIL" : "Ok");
  44. return err;
  45. }
  46. static int build_id_cache__remove_file(const char *filename __used,
  47. const char *debugdir __used)
  48. {
  49. u8 build_id[BUILD_ID_SIZE];
  50. char sbuild_id[BUILD_ID_SIZE * 2 + 1];
  51. int err;
  52. if (filename__read_build_id(filename, &build_id, sizeof(build_id)) < 0) {
  53. pr_debug("Couldn't read a build-id in %s\n", filename);
  54. return -1;
  55. }
  56. build_id__sprintf(build_id, sizeof(build_id), sbuild_id);
  57. err = build_id_cache__remove_s(sbuild_id, debugdir);
  58. if (verbose)
  59. pr_info("Removing %s %s: %s\n", sbuild_id, filename,
  60. err ? "FAIL" : "Ok");
  61. return err;
  62. }
  63. static int __cmd_buildid_cache(void)
  64. {
  65. struct strlist *list;
  66. struct str_node *pos;
  67. char debugdir[PATH_MAX];
  68. snprintf(debugdir, sizeof(debugdir), "%s/%s", getenv("HOME"),
  69. DEBUG_CACHE_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, const char *prefix __used)
  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. return __cmd_buildid_cache();
  112. }