builtin-buildid-cache.c 11 KB

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