builtin-buildid-cache.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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, size_t sbuildid_size)
  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__snprintf_build_id(root_dir, sbuildid, sbuildid_size);
  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, sizeof(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 = { .size = 0, };
  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__snprintf(&bid, sbuild_id, sizeof(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 = { .size = 0, };
  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__snprintf(&bid, sbuild_id, sizeof(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 = { .size = 0, };
  233. int err;
  234. if (!dso__build_id_filename(dso, filename, sizeof(filename), false))
  235. return true;
  236. err = filename__read_build_id(filename, &bid);
  237. if (err < 0) {
  238. if (err == -ENOENT)
  239. return false;
  240. pr_warning("Problems with %s file, consider removing it from the cache\n",
  241. filename);
  242. } else if (memcmp(dso__bid(dso)->data, bid.data, bid.size)) {
  243. pr_warning("Problems with %s file, consider removing it from the cache\n",
  244. filename);
  245. }
  246. return true;
  247. }
  248. static int build_id_cache__fprintf_missing(struct perf_session *session, FILE *fp)
  249. {
  250. perf_session__fprintf_dsos_buildid(session, fp, dso__missing_buildid_cache, 0);
  251. return 0;
  252. }
  253. static int build_id_cache__update_file(const char *filename, struct nsinfo *nsi)
  254. {
  255. char sbuild_id[SBUILD_ID_SIZE];
  256. struct build_id bid = { .size = 0, };
  257. struct nscookie nsc;
  258. int err;
  259. nsinfo__mountns_enter(nsi, &nsc);
  260. err = filename__read_build_id(filename, &bid);
  261. nsinfo__mountns_exit(&nsc);
  262. if (err < 0) {
  263. pr_debug("Couldn't read a build-id in %s\n", filename);
  264. return -1;
  265. }
  266. err = 0;
  267. build_id__snprintf(&bid, sbuild_id, sizeof(sbuild_id));
  268. if (build_id_cache__cached(sbuild_id))
  269. err = build_id_cache__remove_s(sbuild_id);
  270. if (!err)
  271. err = build_id_cache__add_s(sbuild_id, filename, nsi, false,
  272. false);
  273. pr_debug("Updating %s %s: %s\n", sbuild_id, filename,
  274. err ? "FAIL" : "Ok");
  275. return err;
  276. }
  277. static int build_id_cache__show_all(void)
  278. {
  279. struct strlist *bidlist;
  280. struct str_node *nd;
  281. char *buf;
  282. bidlist = build_id_cache__list_all(true);
  283. if (!bidlist) {
  284. pr_debug("Failed to get buildids: -%d\n", errno);
  285. return -1;
  286. }
  287. strlist__for_each_entry(nd, bidlist) {
  288. buf = build_id_cache__origname(nd->s);
  289. fprintf(stdout, "%s %s\n", nd->s, buf);
  290. free(buf);
  291. }
  292. strlist__delete(bidlist);
  293. return 0;
  294. }
  295. static int perf_buildid_cache_config(const char *var, const char *value, void *cb)
  296. {
  297. struct perf_debuginfod *di = cb;
  298. if (!strcmp(var, "buildid-cache.debuginfod")) {
  299. di->urls = strdup(value);
  300. if (!di->urls)
  301. return -ENOMEM;
  302. di->set = true;
  303. }
  304. return 0;
  305. }
  306. int cmd_buildid_cache(int argc, const char **argv)
  307. {
  308. struct strlist *list;
  309. struct str_node *pos;
  310. int ret, ns_id = -1;
  311. bool force = false;
  312. bool list_files = false;
  313. bool opts_flag = false;
  314. bool purge_all = false;
  315. char const *add_name_list_str = NULL,
  316. *remove_name_list_str = NULL,
  317. *purge_name_list_str = NULL,
  318. *missing_filename = NULL,
  319. *update_name_list_str = NULL,
  320. *kcore_filename = NULL;
  321. struct perf_debuginfod debuginfod = { };
  322. char sbuf[STRERR_BUFSIZE];
  323. struct perf_data data = {
  324. .mode = PERF_DATA_MODE_READ,
  325. };
  326. struct perf_session *session = NULL;
  327. struct nsinfo *nsi = NULL;
  328. const struct option buildid_cache_options[] = {
  329. OPT_STRING('a', "add", &add_name_list_str,
  330. "file list", "file(s) to add"),
  331. OPT_STRING('k', "kcore", &kcore_filename,
  332. "file", "kcore file to add"),
  333. OPT_STRING('r', "remove", &remove_name_list_str, "file list",
  334. "file(s) to remove"),
  335. OPT_STRING('p', "purge", &purge_name_list_str, "file list",
  336. "file(s) to remove (remove old caches too)"),
  337. OPT_BOOLEAN('P', "purge-all", &purge_all, "purge all cached files"),
  338. OPT_BOOLEAN('l', "list", &list_files, "list all cached files"),
  339. OPT_STRING('M', "missing", &missing_filename, "file",
  340. "to find missing build ids in the cache"),
  341. OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
  342. OPT_STRING('u', "update", &update_name_list_str, "file list",
  343. "file(s) to update"),
  344. OPT_STRING_OPTARG_SET(0, "debuginfod", &debuginfod.urls,
  345. &debuginfod.set, "debuginfod urls",
  346. "Enable debuginfod data retrieval from DEBUGINFOD_URLS or specified urls",
  347. "system"),
  348. OPT_INCR('v', "verbose", &verbose, "be more verbose"),
  349. OPT_INTEGER(0, "target-ns", &ns_id, "target pid for namespace context"),
  350. OPT_END()
  351. };
  352. const char * const buildid_cache_usage[] = {
  353. "perf buildid-cache [<options>]",
  354. NULL
  355. };
  356. ret = perf_config(perf_buildid_cache_config, &debuginfod);
  357. if (ret)
  358. return ret;
  359. argc = parse_options(argc, argv, buildid_cache_options,
  360. buildid_cache_usage, 0);
  361. opts_flag = add_name_list_str || kcore_filename ||
  362. remove_name_list_str || purge_name_list_str ||
  363. missing_filename || update_name_list_str ||
  364. purge_all;
  365. if (argc || !(list_files || opts_flag))
  366. usage_with_options(buildid_cache_usage, buildid_cache_options);
  367. perf_debuginfod_setup(&debuginfod);
  368. /* -l is exclusive. It can not be used with other options. */
  369. if (list_files && opts_flag) {
  370. usage_with_options_msg(buildid_cache_usage,
  371. buildid_cache_options, "-l is exclusive.\n");
  372. }
  373. if (ns_id > 0)
  374. nsi = nsinfo__new(ns_id);
  375. if (missing_filename) {
  376. data.path = missing_filename;
  377. data.force = force;
  378. session = perf_session__new(&data, NULL);
  379. if (IS_ERR(session))
  380. return PTR_ERR(session);
  381. }
  382. if (symbol__init(session ? perf_session__env(session) : NULL) < 0)
  383. goto out;
  384. setup_pager();
  385. if (list_files) {
  386. ret = build_id_cache__show_all();
  387. goto out;
  388. }
  389. if (add_name_list_str) {
  390. list = strlist__new(add_name_list_str, NULL);
  391. if (list) {
  392. strlist__for_each_entry(pos, list)
  393. if (build_id_cache__add_file(pos->s, nsi)) {
  394. if (errno == EEXIST) {
  395. pr_debug("%s already in the cache\n",
  396. pos->s);
  397. continue;
  398. }
  399. pr_warning("Couldn't add %s: %s\n",
  400. pos->s, str_error_r(errno, sbuf, sizeof(sbuf)));
  401. }
  402. strlist__delete(list);
  403. }
  404. }
  405. if (remove_name_list_str) {
  406. list = strlist__new(remove_name_list_str, NULL);
  407. if (list) {
  408. strlist__for_each_entry(pos, list)
  409. if (build_id_cache__remove_file(pos->s, nsi)) {
  410. if (errno == ENOENT) {
  411. pr_debug("%s wasn't in the cache\n",
  412. pos->s);
  413. continue;
  414. }
  415. pr_warning("Couldn't remove %s: %s\n",
  416. pos->s, str_error_r(errno, sbuf, sizeof(sbuf)));
  417. }
  418. strlist__delete(list);
  419. }
  420. }
  421. if (purge_name_list_str) {
  422. list = strlist__new(purge_name_list_str, NULL);
  423. if (list) {
  424. strlist__for_each_entry(pos, list)
  425. if (build_id_cache__purge_path(pos->s, nsi)) {
  426. if (errno == ENOENT) {
  427. pr_debug("%s wasn't in the cache\n",
  428. pos->s);
  429. continue;
  430. }
  431. pr_warning("Couldn't remove %s: %s\n",
  432. pos->s, str_error_r(errno, sbuf, sizeof(sbuf)));
  433. }
  434. strlist__delete(list);
  435. }
  436. }
  437. if (purge_all) {
  438. if (build_id_cache__purge_all()) {
  439. pr_warning("Couldn't remove some caches. Error: %s.\n",
  440. str_error_r(errno, sbuf, sizeof(sbuf)));
  441. }
  442. }
  443. if (missing_filename)
  444. ret = build_id_cache__fprintf_missing(session, stdout);
  445. if (update_name_list_str) {
  446. list = strlist__new(update_name_list_str, NULL);
  447. if (list) {
  448. strlist__for_each_entry(pos, list)
  449. if (build_id_cache__update_file(pos->s, nsi)) {
  450. if (errno == ENOENT) {
  451. pr_debug("%s wasn't in the cache\n",
  452. pos->s);
  453. continue;
  454. }
  455. pr_warning("Couldn't update %s: %s\n",
  456. pos->s, str_error_r(errno, sbuf, sizeof(sbuf)));
  457. }
  458. strlist__delete(list);
  459. }
  460. }
  461. if (kcore_filename && build_id_cache__add_kcore(kcore_filename, force))
  462. pr_warning("Couldn't add %s\n", kcore_filename);
  463. out:
  464. perf_session__delete(session);
  465. nsinfo__zput(nsi);
  466. return ret;
  467. }