builtin-buildid-cache.c 13 KB

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