builtin-buildid-cache.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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 <sys/types.h>
  10. #include <sys/time.h>
  11. #include <time.h>
  12. #include <dirent.h>
  13. #include <unistd.h>
  14. #include "builtin.h"
  15. #include "perf.h"
  16. #include "util/cache.h"
  17. #include "util/debug.h"
  18. #include "util/header.h"
  19. #include <subcmd/parse-options.h>
  20. #include "util/strlist.h"
  21. #include "util/build-id.h"
  22. #include "util/session.h"
  23. #include "util/symbol.h"
  24. static int build_id_cache__kcore_buildid(const char *proc_dir, char *sbuildid)
  25. {
  26. char root_dir[PATH_MAX];
  27. char *p;
  28. strlcpy(root_dir, proc_dir, sizeof(root_dir));
  29. p = strrchr(root_dir, '/');
  30. if (!p)
  31. return -1;
  32. *p = '\0';
  33. return sysfs__sprintf_build_id(root_dir, sbuildid);
  34. }
  35. static int build_id_cache__kcore_dir(char *dir, size_t sz)
  36. {
  37. return fetch_current_timestamp(dir, sz);
  38. }
  39. static bool same_kallsyms_reloc(const char *from_dir, char *to_dir)
  40. {
  41. char from[PATH_MAX];
  42. char to[PATH_MAX];
  43. const char *name;
  44. u64 addr1 = 0, addr2 = 0;
  45. int i;
  46. scnprintf(from, sizeof(from), "%s/kallsyms", from_dir);
  47. scnprintf(to, sizeof(to), "%s/kallsyms", to_dir);
  48. for (i = 0; (name = ref_reloc_sym_names[i]) != NULL; i++) {
  49. addr1 = kallsyms__get_function_start(from, name);
  50. if (addr1)
  51. break;
  52. }
  53. if (name)
  54. addr2 = kallsyms__get_function_start(to, name);
  55. return addr1 == addr2;
  56. }
  57. static int build_id_cache__kcore_existing(const char *from_dir, char *to_dir,
  58. size_t to_dir_sz)
  59. {
  60. char from[PATH_MAX];
  61. char to[PATH_MAX];
  62. char to_subdir[PATH_MAX];
  63. struct dirent *dent;
  64. int ret = -1;
  65. DIR *d;
  66. d = opendir(to_dir);
  67. if (!d)
  68. return -1;
  69. scnprintf(from, sizeof(from), "%s/modules", from_dir);
  70. while (1) {
  71. dent = readdir(d);
  72. if (!dent)
  73. break;
  74. if (dent->d_type != DT_DIR)
  75. continue;
  76. scnprintf(to, sizeof(to), "%s/%s/modules", to_dir,
  77. dent->d_name);
  78. scnprintf(to_subdir, sizeof(to_subdir), "%s/%s",
  79. to_dir, dent->d_name);
  80. if (!compare_proc_modules(from, to) &&
  81. same_kallsyms_reloc(from_dir, to_subdir)) {
  82. strlcpy(to_dir, to_subdir, to_dir_sz);
  83. ret = 0;
  84. break;
  85. }
  86. }
  87. closedir(d);
  88. return ret;
  89. }
  90. static int build_id_cache__add_kcore(const char *filename, bool force)
  91. {
  92. char dir[32], sbuildid[SBUILD_ID_SIZE];
  93. char from_dir[PATH_MAX], to_dir[PATH_MAX];
  94. char *p;
  95. strlcpy(from_dir, filename, sizeof(from_dir));
  96. p = strrchr(from_dir, '/');
  97. if (!p || strcmp(p + 1, "kcore"))
  98. return -1;
  99. *p = '\0';
  100. if (build_id_cache__kcore_buildid(from_dir, sbuildid) < 0)
  101. return -1;
  102. scnprintf(to_dir, sizeof(to_dir), "%s/%s/%s",
  103. buildid_dir, DSO__NAME_KCORE, sbuildid);
  104. if (!force &&
  105. !build_id_cache__kcore_existing(from_dir, to_dir, sizeof(to_dir))) {
  106. pr_debug("same kcore found in %s\n", to_dir);
  107. return 0;
  108. }
  109. if (build_id_cache__kcore_dir(dir, sizeof(dir)))
  110. return -1;
  111. scnprintf(to_dir, sizeof(to_dir), "%s/%s/%s/%s",
  112. buildid_dir, DSO__NAME_KCORE, sbuildid, dir);
  113. if (mkdir_p(to_dir, 0755))
  114. return -1;
  115. if (kcore_copy(from_dir, to_dir)) {
  116. /* Remove YYYYmmddHHMMSShh directory */
  117. if (!rmdir(to_dir)) {
  118. p = strrchr(to_dir, '/');
  119. if (p)
  120. *p = '\0';
  121. /* Try to remove buildid directory */
  122. if (!rmdir(to_dir)) {
  123. p = strrchr(to_dir, '/');
  124. if (p)
  125. *p = '\0';
  126. /* Try to remove [kernel.kcore] directory */
  127. rmdir(to_dir);
  128. }
  129. }
  130. return -1;
  131. }
  132. pr_debug("kcore added to build-id cache directory %s\n", to_dir);
  133. return 0;
  134. }
  135. static int build_id_cache__add_file(const char *filename)
  136. {
  137. char sbuild_id[SBUILD_ID_SIZE];
  138. u8 build_id[BUILD_ID_SIZE];
  139. int err;
  140. if (filename__read_build_id(filename, &build_id, sizeof(build_id)) < 0) {
  141. pr_debug("Couldn't read a build-id in %s\n", filename);
  142. return -1;
  143. }
  144. build_id__sprintf(build_id, sizeof(build_id), sbuild_id);
  145. err = build_id_cache__add_s(sbuild_id, filename,
  146. false, false);
  147. pr_debug("Adding %s %s: %s\n", sbuild_id, filename,
  148. err ? "FAIL" : "Ok");
  149. return err;
  150. }
  151. static int build_id_cache__remove_file(const char *filename)
  152. {
  153. u8 build_id[BUILD_ID_SIZE];
  154. char sbuild_id[SBUILD_ID_SIZE];
  155. int err;
  156. if (filename__read_build_id(filename, &build_id, sizeof(build_id)) < 0) {
  157. pr_debug("Couldn't read a build-id in %s\n", filename);
  158. return -1;
  159. }
  160. build_id__sprintf(build_id, sizeof(build_id), sbuild_id);
  161. err = build_id_cache__remove_s(sbuild_id);
  162. pr_debug("Removing %s %s: %s\n", sbuild_id, filename,
  163. err ? "FAIL" : "Ok");
  164. return err;
  165. }
  166. static int build_id_cache__purge_path(const char *pathname)
  167. {
  168. struct strlist *list;
  169. struct str_node *pos;
  170. int err;
  171. err = build_id_cache__list_build_ids(pathname, &list);
  172. if (err)
  173. goto out;
  174. strlist__for_each_entry(pos, list) {
  175. err = build_id_cache__remove_s(pos->s);
  176. pr_debug("Removing %s %s: %s\n", pos->s, pathname,
  177. err ? "FAIL" : "Ok");
  178. if (err)
  179. break;
  180. }
  181. strlist__delete(list);
  182. out:
  183. pr_debug("Purging %s: %s\n", pathname, err ? "FAIL" : "Ok");
  184. return err;
  185. }
  186. static bool dso__missing_buildid_cache(struct dso *dso, int parm __maybe_unused)
  187. {
  188. char filename[PATH_MAX];
  189. u8 build_id[BUILD_ID_SIZE];
  190. if (dso__build_id_filename(dso, filename, sizeof(filename)) &&
  191. filename__read_build_id(filename, build_id,
  192. sizeof(build_id)) != sizeof(build_id)) {
  193. if (errno == ENOENT)
  194. return false;
  195. pr_warning("Problems with %s file, consider removing it from the cache\n",
  196. filename);
  197. } else if (memcmp(dso->build_id, build_id, sizeof(dso->build_id))) {
  198. pr_warning("Problems with %s file, consider removing it from the cache\n",
  199. filename);
  200. }
  201. return true;
  202. }
  203. static int build_id_cache__fprintf_missing(struct perf_session *session, FILE *fp)
  204. {
  205. perf_session__fprintf_dsos_buildid(session, fp, dso__missing_buildid_cache, 0);
  206. return 0;
  207. }
  208. static int build_id_cache__update_file(const char *filename)
  209. {
  210. u8 build_id[BUILD_ID_SIZE];
  211. char sbuild_id[SBUILD_ID_SIZE];
  212. int err = 0;
  213. if (filename__read_build_id(filename, &build_id, sizeof(build_id)) < 0) {
  214. pr_debug("Couldn't read a build-id in %s\n", filename);
  215. return -1;
  216. }
  217. build_id__sprintf(build_id, sizeof(build_id), sbuild_id);
  218. if (build_id_cache__cached(sbuild_id))
  219. err = build_id_cache__remove_s(sbuild_id);
  220. if (!err)
  221. err = build_id_cache__add_s(sbuild_id, filename, false, false);
  222. pr_debug("Updating %s %s: %s\n", sbuild_id, filename,
  223. err ? "FAIL" : "Ok");
  224. return err;
  225. }
  226. int cmd_buildid_cache(int argc, const char **argv,
  227. const char *prefix __maybe_unused)
  228. {
  229. struct strlist *list;
  230. struct str_node *pos;
  231. int ret = 0;
  232. bool force = false;
  233. char const *add_name_list_str = NULL,
  234. *remove_name_list_str = NULL,
  235. *purge_name_list_str = NULL,
  236. *missing_filename = NULL,
  237. *update_name_list_str = NULL,
  238. *kcore_filename = NULL;
  239. char sbuf[STRERR_BUFSIZE];
  240. struct perf_data_file file = {
  241. .mode = PERF_DATA_MODE_READ,
  242. };
  243. struct perf_session *session = NULL;
  244. const struct option buildid_cache_options[] = {
  245. OPT_STRING('a', "add", &add_name_list_str,
  246. "file list", "file(s) to add"),
  247. OPT_STRING('k', "kcore", &kcore_filename,
  248. "file", "kcore file to add"),
  249. OPT_STRING('r', "remove", &remove_name_list_str, "file list",
  250. "file(s) to remove"),
  251. OPT_STRING('p', "purge", &purge_name_list_str, "path list",
  252. "path(s) to remove (remove old caches too)"),
  253. OPT_STRING('M', "missing", &missing_filename, "file",
  254. "to find missing build ids in the cache"),
  255. OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
  256. OPT_STRING('u', "update", &update_name_list_str, "file list",
  257. "file(s) to update"),
  258. OPT_INCR('v', "verbose", &verbose, "be more verbose"),
  259. OPT_END()
  260. };
  261. const char * const buildid_cache_usage[] = {
  262. "perf buildid-cache [<options>]",
  263. NULL
  264. };
  265. argc = parse_options(argc, argv, buildid_cache_options,
  266. buildid_cache_usage, 0);
  267. if (argc || (!add_name_list_str && !kcore_filename &&
  268. !remove_name_list_str && !purge_name_list_str &&
  269. !missing_filename && !update_name_list_str))
  270. usage_with_options(buildid_cache_usage, buildid_cache_options);
  271. if (missing_filename) {
  272. file.path = missing_filename;
  273. file.force = force;
  274. session = perf_session__new(&file, false, NULL);
  275. if (session == NULL)
  276. return -1;
  277. }
  278. if (symbol__init(session ? &session->header.env : NULL) < 0)
  279. goto out;
  280. setup_pager();
  281. if (add_name_list_str) {
  282. list = strlist__new(add_name_list_str, NULL);
  283. if (list) {
  284. strlist__for_each_entry(pos, list)
  285. if (build_id_cache__add_file(pos->s)) {
  286. if (errno == EEXIST) {
  287. pr_debug("%s already in the cache\n",
  288. pos->s);
  289. continue;
  290. }
  291. pr_warning("Couldn't add %s: %s\n",
  292. pos->s, str_error_r(errno, sbuf, sizeof(sbuf)));
  293. }
  294. strlist__delete(list);
  295. }
  296. }
  297. if (remove_name_list_str) {
  298. list = strlist__new(remove_name_list_str, NULL);
  299. if (list) {
  300. strlist__for_each_entry(pos, list)
  301. if (build_id_cache__remove_file(pos->s)) {
  302. if (errno == ENOENT) {
  303. pr_debug("%s wasn't in the cache\n",
  304. pos->s);
  305. continue;
  306. }
  307. pr_warning("Couldn't remove %s: %s\n",
  308. pos->s, str_error_r(errno, sbuf, sizeof(sbuf)));
  309. }
  310. strlist__delete(list);
  311. }
  312. }
  313. if (purge_name_list_str) {
  314. list = strlist__new(purge_name_list_str, NULL);
  315. if (list) {
  316. strlist__for_each_entry(pos, list)
  317. if (build_id_cache__purge_path(pos->s)) {
  318. if (errno == ENOENT) {
  319. pr_debug("%s wasn't in the cache\n",
  320. pos->s);
  321. continue;
  322. }
  323. pr_warning("Couldn't remove %s: %s\n",
  324. pos->s, str_error_r(errno, sbuf, sizeof(sbuf)));
  325. }
  326. strlist__delete(list);
  327. }
  328. }
  329. if (missing_filename)
  330. ret = build_id_cache__fprintf_missing(session, stdout);
  331. if (update_name_list_str) {
  332. list = strlist__new(update_name_list_str, NULL);
  333. if (list) {
  334. strlist__for_each_entry(pos, list)
  335. if (build_id_cache__update_file(pos->s)) {
  336. if (errno == ENOENT) {
  337. pr_debug("%s wasn't in the cache\n",
  338. pos->s);
  339. continue;
  340. }
  341. pr_warning("Couldn't update %s: %s\n",
  342. pos->s, str_error_r(errno, sbuf, sizeof(sbuf)));
  343. }
  344. strlist__delete(list);
  345. }
  346. }
  347. if (kcore_filename && build_id_cache__add_kcore(kcore_filename, force))
  348. pr_warning("Couldn't add %s\n", kcore_filename);
  349. out:
  350. perf_session__delete(session);
  351. return ret;
  352. }