builtin-buildid-cache.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * builtin-buildid-cache.c
  4. *
  5. * Builtin buildid-cache command: Manages build-id cache
  6. *
  7. * Copyright (C) 2010, Red Hat Inc.
  8. * Copyright (C) 2010, Arnaldo Carvalho de Melo <acme@redhat.com>
  9. */
  10. #include <sys/types.h>
  11. #include <sys/time.h>
  12. #include <time.h>
  13. #include <dirent.h>
  14. #include <errno.h>
  15. #include <unistd.h>
  16. #include "builtin.h"
  17. #include "perf.h"
  18. #include "namespaces.h"
  19. #include "util/cache.h"
  20. #include "util/debug.h"
  21. #include "util/header.h"
  22. #include <subcmd/parse-options.h>
  23. #include "util/strlist.h"
  24. #include "util/build-id.h"
  25. #include "util/session.h"
  26. #include "util/symbol.h"
  27. #include "util/time-utils.h"
  28. #include "util/probe-file.h"
  29. static int build_id_cache__kcore_buildid(const char *proc_dir, char *sbuildid)
  30. {
  31. char root_dir[PATH_MAX];
  32. char *p;
  33. strlcpy(root_dir, proc_dir, sizeof(root_dir));
  34. p = strrchr(root_dir, '/');
  35. if (!p)
  36. return -1;
  37. *p = '\0';
  38. return sysfs__sprintf_build_id(root_dir, sbuildid);
  39. }
  40. static int build_id_cache__kcore_dir(char *dir, size_t sz)
  41. {
  42. return fetch_current_timestamp(dir, sz);
  43. }
  44. static bool same_kallsyms_reloc(const char *from_dir, char *to_dir)
  45. {
  46. char from[PATH_MAX];
  47. char to[PATH_MAX];
  48. const char *name;
  49. u64 addr1 = 0, addr2 = 0;
  50. int i, err = -1;
  51. scnprintf(from, sizeof(from), "%s/kallsyms", from_dir);
  52. scnprintf(to, sizeof(to), "%s/kallsyms", to_dir);
  53. for (i = 0; (name = ref_reloc_sym_names[i]) != NULL; i++) {
  54. err = kallsyms__get_function_start(from, name, &addr1);
  55. if (!err)
  56. break;
  57. }
  58. if (err)
  59. return false;
  60. if (kallsyms__get_function_start(to, name, &addr2))
  61. return false;
  62. return addr1 == addr2;
  63. }
  64. static int build_id_cache__kcore_existing(const char *from_dir, char *to_dir,
  65. size_t to_dir_sz)
  66. {
  67. char from[PATH_MAX];
  68. char to[PATH_MAX];
  69. char to_subdir[PATH_MAX];
  70. struct dirent *dent;
  71. int ret = -1;
  72. DIR *d;
  73. d = opendir(to_dir);
  74. if (!d)
  75. return -1;
  76. scnprintf(from, sizeof(from), "%s/modules", from_dir);
  77. while (1) {
  78. dent = readdir(d);
  79. if (!dent)
  80. break;
  81. if (dent->d_type != DT_DIR)
  82. continue;
  83. scnprintf(to, sizeof(to), "%s/%s/modules", to_dir,
  84. dent->d_name);
  85. scnprintf(to_subdir, sizeof(to_subdir), "%s/%s",
  86. to_dir, dent->d_name);
  87. if (!compare_proc_modules(from, to) &&
  88. same_kallsyms_reloc(from_dir, to_subdir)) {
  89. strlcpy(to_dir, to_subdir, to_dir_sz);
  90. ret = 0;
  91. break;
  92. }
  93. }
  94. closedir(d);
  95. return ret;
  96. }
  97. static int build_id_cache__add_kcore(const char *filename, bool force)
  98. {
  99. char dir[32], sbuildid[SBUILD_ID_SIZE];
  100. char from_dir[PATH_MAX], to_dir[PATH_MAX];
  101. char *p;
  102. strlcpy(from_dir, filename, sizeof(from_dir));
  103. p = strrchr(from_dir, '/');
  104. if (!p || strcmp(p + 1, "kcore"))
  105. return -1;
  106. *p = '\0';
  107. if (build_id_cache__kcore_buildid(from_dir, sbuildid) < 0)
  108. return -1;
  109. scnprintf(to_dir, sizeof(to_dir), "%s/%s/%s",
  110. buildid_dir, DSO__NAME_KCORE, sbuildid);
  111. if (!force &&
  112. !build_id_cache__kcore_existing(from_dir, to_dir, sizeof(to_dir))) {
  113. pr_debug("same kcore found in %s\n", to_dir);
  114. return 0;
  115. }
  116. if (build_id_cache__kcore_dir(dir, sizeof(dir)))
  117. return -1;
  118. scnprintf(to_dir, sizeof(to_dir), "%s/%s/%s/%s",
  119. buildid_dir, DSO__NAME_KCORE, sbuildid, dir);
  120. if (mkdir_p(to_dir, 0755))
  121. return -1;
  122. if (kcore_copy(from_dir, to_dir)) {
  123. /* Remove YYYYmmddHHMMSShh directory */
  124. if (!rmdir(to_dir)) {
  125. p = strrchr(to_dir, '/');
  126. if (p)
  127. *p = '\0';
  128. /* Try to remove buildid directory */
  129. if (!rmdir(to_dir)) {
  130. p = strrchr(to_dir, '/');
  131. if (p)
  132. *p = '\0';
  133. /* Try to remove [kernel.kcore] directory */
  134. rmdir(to_dir);
  135. }
  136. }
  137. return -1;
  138. }
  139. pr_debug("kcore added to build-id cache directory %s\n", to_dir);
  140. return 0;
  141. }
  142. static int build_id_cache__add_file(const char *filename, struct nsinfo *nsi)
  143. {
  144. char sbuild_id[SBUILD_ID_SIZE];
  145. u8 build_id[BUILD_ID_SIZE];
  146. int err;
  147. struct nscookie nsc;
  148. nsinfo__mountns_enter(nsi, &nsc);
  149. err = filename__read_build_id(filename, &build_id, sizeof(build_id));
  150. nsinfo__mountns_exit(&nsc);
  151. if (err < 0) {
  152. pr_debug("Couldn't read a build-id in %s\n", filename);
  153. return -1;
  154. }
  155. build_id__sprintf(build_id, sizeof(build_id), sbuild_id);
  156. err = build_id_cache__add_s(sbuild_id, filename, nsi,
  157. false, false);
  158. pr_debug("Adding %s %s: %s\n", sbuild_id, filename,
  159. err ? "FAIL" : "Ok");
  160. return err;
  161. }
  162. static int build_id_cache__remove_file(const char *filename, struct nsinfo *nsi)
  163. {
  164. u8 build_id[BUILD_ID_SIZE];
  165. char sbuild_id[SBUILD_ID_SIZE];
  166. struct nscookie nsc;
  167. int err;
  168. nsinfo__mountns_enter(nsi, &nsc);
  169. err = filename__read_build_id(filename, &build_id, sizeof(build_id));
  170. nsinfo__mountns_exit(&nsc);
  171. if (err < 0) {
  172. pr_debug("Couldn't read a build-id in %s\n", filename);
  173. return -1;
  174. }
  175. build_id__sprintf(build_id, sizeof(build_id), sbuild_id);
  176. err = build_id_cache__remove_s(sbuild_id);
  177. pr_debug("Removing %s %s: %s\n", sbuild_id, filename,
  178. err ? "FAIL" : "Ok");
  179. return err;
  180. }
  181. static int build_id_cache__purge_path(const char *pathname, struct nsinfo *nsi)
  182. {
  183. struct strlist *list;
  184. struct str_node *pos;
  185. int err;
  186. err = build_id_cache__list_build_ids(pathname, nsi, &list);
  187. if (err)
  188. goto out;
  189. strlist__for_each_entry(pos, list) {
  190. err = build_id_cache__remove_s(pos->s);
  191. pr_debug("Removing %s %s: %s\n", pos->s, pathname,
  192. err ? "FAIL" : "Ok");
  193. if (err)
  194. break;
  195. }
  196. strlist__delete(list);
  197. out:
  198. pr_debug("Purging %s: %s\n", pathname, err ? "FAIL" : "Ok");
  199. return err;
  200. }
  201. static int build_id_cache__purge_all(void)
  202. {
  203. struct strlist *list;
  204. struct str_node *pos;
  205. int err = 0;
  206. char *buf;
  207. list = build_id_cache__list_all(false);
  208. if (!list) {
  209. pr_debug("Failed to get buildids: -%d\n", errno);
  210. return -EINVAL;
  211. }
  212. strlist__for_each_entry(pos, list) {
  213. buf = build_id_cache__origname(pos->s);
  214. err = build_id_cache__remove_s(pos->s);
  215. pr_debug("Removing %s (%s): %s\n", buf, pos->s,
  216. err ? "FAIL" : "Ok");
  217. free(buf);
  218. if (err)
  219. break;
  220. }
  221. strlist__delete(list);
  222. pr_debug("Purged all: %s\n", err ? "FAIL" : "Ok");
  223. return err;
  224. }
  225. static bool dso__missing_buildid_cache(struct dso *dso, int parm __maybe_unused)
  226. {
  227. char filename[PATH_MAX];
  228. u8 build_id[BUILD_ID_SIZE];
  229. if (dso__build_id_filename(dso, filename, sizeof(filename), false) &&
  230. filename__read_build_id(filename, build_id,
  231. sizeof(build_id)) != sizeof(build_id)) {
  232. if (errno == ENOENT)
  233. return false;
  234. pr_warning("Problems with %s file, consider removing it from the cache\n",
  235. filename);
  236. } else if (memcmp(dso->build_id, build_id, sizeof(dso->build_id))) {
  237. pr_warning("Problems with %s file, consider removing it from the cache\n",
  238. filename);
  239. }
  240. return true;
  241. }
  242. static int build_id_cache__fprintf_missing(struct perf_session *session, FILE *fp)
  243. {
  244. perf_session__fprintf_dsos_buildid(session, fp, dso__missing_buildid_cache, 0);
  245. return 0;
  246. }
  247. static int build_id_cache__update_file(const char *filename, struct nsinfo *nsi)
  248. {
  249. u8 build_id[BUILD_ID_SIZE];
  250. char sbuild_id[SBUILD_ID_SIZE];
  251. struct nscookie nsc;
  252. int err;
  253. nsinfo__mountns_enter(nsi, &nsc);
  254. err = filename__read_build_id(filename, &build_id, sizeof(build_id));
  255. nsinfo__mountns_exit(&nsc);
  256. if (err < 0) {
  257. pr_debug("Couldn't read a build-id in %s\n", filename);
  258. return -1;
  259. }
  260. err = 0;
  261. build_id__sprintf(build_id, sizeof(build_id), sbuild_id);
  262. if (build_id_cache__cached(sbuild_id))
  263. err = build_id_cache__remove_s(sbuild_id);
  264. if (!err)
  265. err = build_id_cache__add_s(sbuild_id, filename, nsi, false,
  266. false);
  267. pr_debug("Updating %s %s: %s\n", sbuild_id, filename,
  268. err ? "FAIL" : "Ok");
  269. return err;
  270. }
  271. static int build_id_cache__show_all(void)
  272. {
  273. struct strlist *bidlist;
  274. struct str_node *nd;
  275. char *buf;
  276. bidlist = build_id_cache__list_all(true);
  277. if (!bidlist) {
  278. pr_debug("Failed to get buildids: -%d\n", errno);
  279. return -1;
  280. }
  281. strlist__for_each_entry(nd, bidlist) {
  282. buf = build_id_cache__origname(nd->s);
  283. fprintf(stdout, "%s %s\n", nd->s, buf);
  284. free(buf);
  285. }
  286. strlist__delete(bidlist);
  287. return 0;
  288. }
  289. int cmd_buildid_cache(int argc, const char **argv)
  290. {
  291. struct strlist *list;
  292. struct str_node *pos;
  293. int ret = 0;
  294. int ns_id = -1;
  295. bool force = false;
  296. bool list_files = false;
  297. bool opts_flag = false;
  298. bool purge_all = false;
  299. char const *add_name_list_str = NULL,
  300. *remove_name_list_str = NULL,
  301. *purge_name_list_str = NULL,
  302. *missing_filename = NULL,
  303. *update_name_list_str = NULL,
  304. *kcore_filename = NULL;
  305. char sbuf[STRERR_BUFSIZE];
  306. struct perf_data data = {
  307. .mode = PERF_DATA_MODE_READ,
  308. };
  309. struct perf_session *session = NULL;
  310. struct nsinfo *nsi = NULL;
  311. const struct option buildid_cache_options[] = {
  312. OPT_STRING('a', "add", &add_name_list_str,
  313. "file list", "file(s) to add"),
  314. OPT_STRING('k', "kcore", &kcore_filename,
  315. "file", "kcore file to add"),
  316. OPT_STRING('r', "remove", &remove_name_list_str, "file list",
  317. "file(s) to remove"),
  318. OPT_STRING('p', "purge", &purge_name_list_str, "file list",
  319. "file(s) to remove (remove old caches too)"),
  320. OPT_BOOLEAN('P', "purge-all", &purge_all, "purge all cached files"),
  321. OPT_BOOLEAN('l', "list", &list_files, "list all cached files"),
  322. OPT_STRING('M', "missing", &missing_filename, "file",
  323. "to find missing build ids in the cache"),
  324. OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
  325. OPT_STRING('u', "update", &update_name_list_str, "file list",
  326. "file(s) to update"),
  327. OPT_INCR('v', "verbose", &verbose, "be more verbose"),
  328. OPT_INTEGER(0, "target-ns", &ns_id, "target pid for namespace context"),
  329. OPT_END()
  330. };
  331. const char * const buildid_cache_usage[] = {
  332. "perf buildid-cache [<options>]",
  333. NULL
  334. };
  335. argc = parse_options(argc, argv, buildid_cache_options,
  336. buildid_cache_usage, 0);
  337. opts_flag = add_name_list_str || kcore_filename ||
  338. remove_name_list_str || purge_name_list_str ||
  339. missing_filename || update_name_list_str ||
  340. purge_all;
  341. if (argc || !(list_files || opts_flag))
  342. usage_with_options(buildid_cache_usage, buildid_cache_options);
  343. /* -l is exclusive. It can not be used with other options. */
  344. if (list_files && opts_flag) {
  345. usage_with_options_msg(buildid_cache_usage,
  346. buildid_cache_options, "-l is exclusive.\n");
  347. }
  348. if (ns_id > 0)
  349. nsi = nsinfo__new(ns_id);
  350. if (missing_filename) {
  351. data.file.path = missing_filename;
  352. data.force = force;
  353. session = perf_session__new(&data, false, NULL);
  354. if (session == NULL)
  355. return -1;
  356. }
  357. if (symbol__init(session ? &session->header.env : NULL) < 0)
  358. goto out;
  359. setup_pager();
  360. if (list_files) {
  361. ret = build_id_cache__show_all();
  362. goto out;
  363. }
  364. if (add_name_list_str) {
  365. list = strlist__new(add_name_list_str, NULL);
  366. if (list) {
  367. strlist__for_each_entry(pos, list)
  368. if (build_id_cache__add_file(pos->s, nsi)) {
  369. if (errno == EEXIST) {
  370. pr_debug("%s already in the cache\n",
  371. pos->s);
  372. continue;
  373. }
  374. pr_warning("Couldn't add %s: %s\n",
  375. pos->s, str_error_r(errno, sbuf, sizeof(sbuf)));
  376. }
  377. strlist__delete(list);
  378. }
  379. }
  380. if (remove_name_list_str) {
  381. list = strlist__new(remove_name_list_str, NULL);
  382. if (list) {
  383. strlist__for_each_entry(pos, list)
  384. if (build_id_cache__remove_file(pos->s, nsi)) {
  385. if (errno == ENOENT) {
  386. pr_debug("%s wasn't in the cache\n",
  387. pos->s);
  388. continue;
  389. }
  390. pr_warning("Couldn't remove %s: %s\n",
  391. pos->s, str_error_r(errno, sbuf, sizeof(sbuf)));
  392. }
  393. strlist__delete(list);
  394. }
  395. }
  396. if (purge_name_list_str) {
  397. list = strlist__new(purge_name_list_str, NULL);
  398. if (list) {
  399. strlist__for_each_entry(pos, list)
  400. if (build_id_cache__purge_path(pos->s, nsi)) {
  401. if (errno == ENOENT) {
  402. pr_debug("%s wasn't in the cache\n",
  403. pos->s);
  404. continue;
  405. }
  406. pr_warning("Couldn't remove %s: %s\n",
  407. pos->s, str_error_r(errno, sbuf, sizeof(sbuf)));
  408. }
  409. strlist__delete(list);
  410. }
  411. }
  412. if (purge_all) {
  413. if (build_id_cache__purge_all()) {
  414. pr_warning("Couldn't remove some caches. Error: %s.\n",
  415. str_error_r(errno, sbuf, sizeof(sbuf)));
  416. }
  417. }
  418. if (missing_filename)
  419. ret = build_id_cache__fprintf_missing(session, stdout);
  420. if (update_name_list_str) {
  421. list = strlist__new(update_name_list_str, NULL);
  422. if (list) {
  423. strlist__for_each_entry(pos, list)
  424. if (build_id_cache__update_file(pos->s, nsi)) {
  425. if (errno == ENOENT) {
  426. pr_debug("%s wasn't in the cache\n",
  427. pos->s);
  428. continue;
  429. }
  430. pr_warning("Couldn't update %s: %s\n",
  431. pos->s, str_error_r(errno, sbuf, sizeof(sbuf)));
  432. }
  433. strlist__delete(list);
  434. }
  435. }
  436. if (kcore_filename && build_id_cache__add_kcore(kcore_filename, force))
  437. pr_warning("Couldn't add %s\n", kcore_filename);
  438. out:
  439. perf_session__delete(session);
  440. nsinfo__zput(nsi);
  441. return ret;
  442. }