builtin-buildid-cache.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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/session.h"
  18. #include "util/symbol.h"
  19. static int build_id_cache__add_file(const char *filename, const char *debugdir)
  20. {
  21. char sbuild_id[BUILD_ID_SIZE * 2 + 1];
  22. u8 build_id[BUILD_ID_SIZE];
  23. int err;
  24. if (filename__read_build_id(filename, &build_id, sizeof(build_id)) < 0) {
  25. pr_debug("Couldn't read a build-id in %s\n", filename);
  26. return -1;
  27. }
  28. build_id__sprintf(build_id, sizeof(build_id), sbuild_id);
  29. err = build_id_cache__add_s(sbuild_id, debugdir, filename,
  30. false, false);
  31. if (verbose)
  32. pr_info("Adding %s %s: %s\n", sbuild_id, filename,
  33. err ? "FAIL" : "Ok");
  34. return err;
  35. }
  36. static int build_id_cache__remove_file(const char *filename,
  37. const char *debugdir)
  38. {
  39. u8 build_id[BUILD_ID_SIZE];
  40. char sbuild_id[BUILD_ID_SIZE * 2 + 1];
  41. int err;
  42. if (filename__read_build_id(filename, &build_id, sizeof(build_id)) < 0) {
  43. pr_debug("Couldn't read a build-id in %s\n", filename);
  44. return -1;
  45. }
  46. build_id__sprintf(build_id, sizeof(build_id), sbuild_id);
  47. err = build_id_cache__remove_s(sbuild_id, debugdir);
  48. if (verbose)
  49. pr_info("Removing %s %s: %s\n", sbuild_id, filename,
  50. err ? "FAIL" : "Ok");
  51. return err;
  52. }
  53. static bool dso__missing_buildid_cache(struct dso *dso, int parm __maybe_unused)
  54. {
  55. char filename[PATH_MAX];
  56. u8 build_id[BUILD_ID_SIZE];
  57. if (dso__build_id_filename(dso, filename, sizeof(filename)) &&
  58. filename__read_build_id(filename, build_id,
  59. sizeof(build_id)) != sizeof(build_id)) {
  60. if (errno == ENOENT)
  61. return false;
  62. pr_warning("Problems with %s file, consider removing it from the cache\n",
  63. filename);
  64. } else if (memcmp(dso->build_id, build_id, sizeof(dso->build_id))) {
  65. pr_warning("Problems with %s file, consider removing it from the cache\n",
  66. filename);
  67. }
  68. return true;
  69. }
  70. static int build_id_cache__fprintf_missing(const char *filename, bool force, FILE *fp)
  71. {
  72. struct perf_session *session = perf_session__new(filename, O_RDONLY,
  73. force, false, NULL);
  74. if (session == NULL)
  75. return -1;
  76. perf_session__fprintf_dsos_buildid(session, fp, dso__missing_buildid_cache, 0);
  77. perf_session__delete(session);
  78. return 0;
  79. }
  80. static int build_id_cache__update_file(const char *filename,
  81. const char *debugdir)
  82. {
  83. u8 build_id[BUILD_ID_SIZE];
  84. char sbuild_id[BUILD_ID_SIZE * 2 + 1];
  85. int err;
  86. if (filename__read_build_id(filename, &build_id, sizeof(build_id)) < 0) {
  87. pr_debug("Couldn't read a build-id in %s\n", filename);
  88. return -1;
  89. }
  90. build_id__sprintf(build_id, sizeof(build_id), sbuild_id);
  91. err = build_id_cache__remove_s(sbuild_id, debugdir);
  92. if (!err) {
  93. err = build_id_cache__add_s(sbuild_id, debugdir, filename,
  94. false, false);
  95. }
  96. if (verbose)
  97. pr_info("Updating %s %s: %s\n", sbuild_id, filename,
  98. err ? "FAIL" : "Ok");
  99. return err;
  100. }
  101. int cmd_buildid_cache(int argc, const char **argv,
  102. const char *prefix __maybe_unused)
  103. {
  104. struct strlist *list;
  105. struct str_node *pos;
  106. int ret = 0;
  107. bool force = false;
  108. char debugdir[PATH_MAX];
  109. char const *add_name_list_str = NULL,
  110. *remove_name_list_str = NULL,
  111. *missing_filename = NULL,
  112. *update_name_list_str = NULL;
  113. const struct option buildid_cache_options[] = {
  114. OPT_STRING('a', "add", &add_name_list_str,
  115. "file list", "file(s) to add"),
  116. OPT_STRING('r', "remove", &remove_name_list_str, "file list",
  117. "file(s) to remove"),
  118. OPT_STRING('M', "missing", &missing_filename, "file",
  119. "to find missing build ids in the cache"),
  120. OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
  121. OPT_STRING('u', "update", &update_name_list_str, "file list",
  122. "file(s) to update"),
  123. OPT_INCR('v', "verbose", &verbose, "be more verbose"),
  124. OPT_END()
  125. };
  126. const char * const buildid_cache_usage[] = {
  127. "perf buildid-cache [<options>]",
  128. NULL
  129. };
  130. argc = parse_options(argc, argv, buildid_cache_options,
  131. buildid_cache_usage, 0);
  132. if (symbol__init() < 0)
  133. return -1;
  134. setup_pager();
  135. snprintf(debugdir, sizeof(debugdir), "%s", buildid_dir);
  136. if (add_name_list_str) {
  137. list = strlist__new(true, add_name_list_str);
  138. if (list) {
  139. strlist__for_each(pos, list)
  140. if (build_id_cache__add_file(pos->s, debugdir)) {
  141. if (errno == EEXIST) {
  142. pr_debug("%s already in the cache\n",
  143. pos->s);
  144. continue;
  145. }
  146. pr_warning("Couldn't add %s: %s\n",
  147. pos->s, strerror(errno));
  148. }
  149. strlist__delete(list);
  150. }
  151. }
  152. if (remove_name_list_str) {
  153. list = strlist__new(true, remove_name_list_str);
  154. if (list) {
  155. strlist__for_each(pos, list)
  156. if (build_id_cache__remove_file(pos->s, debugdir)) {
  157. if (errno == ENOENT) {
  158. pr_debug("%s wasn't in the cache\n",
  159. pos->s);
  160. continue;
  161. }
  162. pr_warning("Couldn't remove %s: %s\n",
  163. pos->s, strerror(errno));
  164. }
  165. strlist__delete(list);
  166. }
  167. }
  168. if (missing_filename)
  169. ret = build_id_cache__fprintf_missing(missing_filename, force, stdout);
  170. if (update_name_list_str) {
  171. list = strlist__new(true, update_name_list_str);
  172. if (list) {
  173. strlist__for_each(pos, list)
  174. if (build_id_cache__update_file(pos->s, debugdir)) {
  175. if (errno == ENOENT) {
  176. pr_debug("%s wasn't in the cache\n",
  177. pos->s);
  178. continue;
  179. }
  180. pr_warning("Couldn't update %s: %s\n",
  181. pos->s, strerror(errno));
  182. }
  183. strlist__delete(list);
  184. }
  185. }
  186. return ret;
  187. }