builtin-buildid-cache.c 13 KB

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