builtin-buildid-cache.c 3.1 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", buildid_dir);
  69. if (add_name_list_str) {
  70. list = strlist__new(true, add_name_list_str);
  71. if (list) {
  72. strlist__for_each(pos, list)
  73. if (build_id_cache__add_file(pos->s, debugdir)) {
  74. if (errno == EEXIST) {
  75. pr_debug("%s already in the cache\n",
  76. pos->s);
  77. continue;
  78. }
  79. pr_warning("Couldn't add %s: %s\n",
  80. pos->s, strerror(errno));
  81. }
  82. strlist__delete(list);
  83. }
  84. }
  85. if (remove_name_list_str) {
  86. list = strlist__new(true, remove_name_list_str);
  87. if (list) {
  88. strlist__for_each(pos, list)
  89. if (build_id_cache__remove_file(pos->s, debugdir)) {
  90. if (errno == ENOENT) {
  91. pr_debug("%s wasn't in the cache\n",
  92. pos->s);
  93. continue;
  94. }
  95. pr_warning("Couldn't remove %s: %s\n",
  96. pos->s, strerror(errno));
  97. }
  98. strlist__delete(list);
  99. }
  100. }
  101. return 0;
  102. }
  103. int cmd_buildid_cache(int argc, const char **argv, const char *prefix __used)
  104. {
  105. argc = parse_options(argc, argv, buildid_cache_options,
  106. buildid_cache_usage, 0);
  107. if (symbol__init() < 0)
  108. return -1;
  109. setup_pager();
  110. return __cmd_buildid_cache();
  111. }