builtin-buildid-cache.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /*
  2. * builtin-buildid-cache.c
  3. *
  4. * Builtin buildid-cache command: Manages build-id cache
  5. *
  6. * Copyright (C) 2010, Red Hat Inc.
  7. * Copyright (C) 2010, Arnaldo Carvalho de Melo <acme@redhat.com>
  8. */
  9. #include <sys/types.h>
  10. #include <sys/time.h>
  11. #include <time.h>
  12. #include <dirent.h>
  13. #include <unistd.h>
  14. #include "builtin.h"
  15. #include "perf.h"
  16. #include "util/cache.h"
  17. #include "util/debug.h"
  18. #include "util/header.h"
  19. #include "util/parse-options.h"
  20. #include "util/strlist.h"
  21. #include "util/build-id.h"
  22. #include "util/session.h"
  23. #include "util/symbol.h"
  24. static int build_id_cache__kcore_buildid(const char *proc_dir, char *sbuildid)
  25. {
  26. char root_dir[PATH_MAX];
  27. char notes[PATH_MAX];
  28. u8 build_id[BUILD_ID_SIZE];
  29. char *p;
  30. strlcpy(root_dir, proc_dir, sizeof(root_dir));
  31. p = strrchr(root_dir, '/');
  32. if (!p)
  33. return -1;
  34. *p = '\0';
  35. scnprintf(notes, sizeof(notes), "%s/sys/kernel/notes", root_dir);
  36. if (sysfs__read_build_id(notes, build_id, sizeof(build_id)))
  37. return -1;
  38. build_id__sprintf(build_id, sizeof(build_id), sbuildid);
  39. return 0;
  40. }
  41. static int build_id_cache__kcore_dir(char *dir, size_t sz)
  42. {
  43. struct timeval tv;
  44. struct tm tm;
  45. char dt[32];
  46. if (gettimeofday(&tv, NULL) || !localtime_r(&tv.tv_sec, &tm))
  47. return -1;
  48. if (!strftime(dt, sizeof(dt), "%Y%m%d%H%M%S", &tm))
  49. return -1;
  50. scnprintf(dir, sz, "%s%02u", dt, (unsigned)tv.tv_usec / 10000);
  51. return 0;
  52. }
  53. static bool same_kallsyms_reloc(const char *from_dir, char *to_dir)
  54. {
  55. char from[PATH_MAX];
  56. char to[PATH_MAX];
  57. const char *name;
  58. u64 addr1 = 0, addr2 = 0;
  59. int i;
  60. scnprintf(from, sizeof(from), "%s/kallsyms", from_dir);
  61. scnprintf(to, sizeof(to), "%s/kallsyms", to_dir);
  62. for (i = 0; (name = ref_reloc_sym_names[i]) != NULL; i++) {
  63. addr1 = kallsyms__get_function_start(from, name);
  64. if (addr1)
  65. break;
  66. }
  67. if (name)
  68. addr2 = kallsyms__get_function_start(to, name);
  69. return addr1 == addr2;
  70. }
  71. static int build_id_cache__kcore_existing(const char *from_dir, char *to_dir,
  72. size_t to_dir_sz)
  73. {
  74. char from[PATH_MAX];
  75. char to[PATH_MAX];
  76. char to_subdir[PATH_MAX];
  77. struct dirent *dent;
  78. int ret = -1;
  79. DIR *d;
  80. d = opendir(to_dir);
  81. if (!d)
  82. return -1;
  83. scnprintf(from, sizeof(from), "%s/modules", from_dir);
  84. while (1) {
  85. dent = readdir(d);
  86. if (!dent)
  87. break;
  88. if (dent->d_type != DT_DIR)
  89. continue;
  90. scnprintf(to, sizeof(to), "%s/%s/modules", to_dir,
  91. dent->d_name);
  92. scnprintf(to_subdir, sizeof(to_subdir), "%s/%s",
  93. to_dir, dent->d_name);
  94. if (!compare_proc_modules(from, to) &&
  95. same_kallsyms_reloc(from_dir, to_subdir)) {
  96. strlcpy(to_dir, to_subdir, to_dir_sz);
  97. ret = 0;
  98. break;
  99. }
  100. }
  101. closedir(d);
  102. return ret;
  103. }
  104. static int build_id_cache__add_kcore(const char *filename, bool force)
  105. {
  106. char dir[32], sbuildid[BUILD_ID_SIZE * 2 + 1];
  107. char from_dir[PATH_MAX], to_dir[PATH_MAX];
  108. char *p;
  109. strlcpy(from_dir, filename, sizeof(from_dir));
  110. p = strrchr(from_dir, '/');
  111. if (!p || strcmp(p + 1, "kcore"))
  112. return -1;
  113. *p = '\0';
  114. if (build_id_cache__kcore_buildid(from_dir, sbuildid))
  115. return -1;
  116. scnprintf(to_dir, sizeof(to_dir), "%s/[kernel.kcore]/%s",
  117. buildid_dir, sbuildid);
  118. if (!force &&
  119. !build_id_cache__kcore_existing(from_dir, to_dir, sizeof(to_dir))) {
  120. pr_debug("same kcore found in %s\n", to_dir);
  121. return 0;
  122. }
  123. if (build_id_cache__kcore_dir(dir, sizeof(dir)))
  124. return -1;
  125. scnprintf(to_dir, sizeof(to_dir), "%s/[kernel.kcore]/%s/%s",
  126. buildid_dir, sbuildid, dir);
  127. if (mkdir_p(to_dir, 0755))
  128. return -1;
  129. if (kcore_copy(from_dir, to_dir)) {
  130. /* Remove YYYYmmddHHMMSShh directory */
  131. if (!rmdir(to_dir)) {
  132. p = strrchr(to_dir, '/');
  133. if (p)
  134. *p = '\0';
  135. /* Try to remove buildid directory */
  136. if (!rmdir(to_dir)) {
  137. p = strrchr(to_dir, '/');
  138. if (p)
  139. *p = '\0';
  140. /* Try to remove [kernel.kcore] directory */
  141. rmdir(to_dir);
  142. }
  143. }
  144. return -1;
  145. }
  146. pr_debug("kcore added to build-id cache directory %s\n", to_dir);
  147. return 0;
  148. }
  149. static int build_id_cache__add_file(const char *filename)
  150. {
  151. char sbuild_id[BUILD_ID_SIZE * 2 + 1];
  152. u8 build_id[BUILD_ID_SIZE];
  153. int err;
  154. if (filename__read_build_id(filename, &build_id, sizeof(build_id)) < 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,
  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)
  166. {
  167. u8 build_id[BUILD_ID_SIZE];
  168. char sbuild_id[BUILD_ID_SIZE * 2 + 1];
  169. int err;
  170. if (filename__read_build_id(filename, &build_id, sizeof(build_id)) < 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)
  181. {
  182. struct strlist *list;
  183. struct str_node *pos;
  184. int err;
  185. err = build_id_cache__list_build_ids(pathname, &list);
  186. if (err)
  187. goto out;
  188. strlist__for_each(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)) &&
  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)
  223. {
  224. u8 build_id[BUILD_ID_SIZE];
  225. char sbuild_id[BUILD_ID_SIZE * 2 + 1];
  226. int err = 0;
  227. if (filename__read_build_id(filename, &build_id, sizeof(build_id)) < 0) {
  228. pr_debug("Couldn't read a build-id in %s\n", filename);
  229. return -1;
  230. }
  231. build_id__sprintf(build_id, sizeof(build_id), sbuild_id);
  232. if (build_id_cache__cached(sbuild_id))
  233. err = build_id_cache__remove_s(sbuild_id);
  234. if (!err)
  235. err = build_id_cache__add_s(sbuild_id, filename, false, false);
  236. pr_debug("Updating %s %s: %s\n", sbuild_id, filename,
  237. err ? "FAIL" : "Ok");
  238. return err;
  239. }
  240. int cmd_buildid_cache(int argc, const char **argv,
  241. const char *prefix __maybe_unused)
  242. {
  243. struct strlist *list;
  244. struct str_node *pos;
  245. int ret = 0;
  246. bool force = false;
  247. char const *add_name_list_str = NULL,
  248. *remove_name_list_str = NULL,
  249. *purge_name_list_str = NULL,
  250. *missing_filename = NULL,
  251. *update_name_list_str = NULL,
  252. *kcore_filename = NULL;
  253. char sbuf[STRERR_BUFSIZE];
  254. struct perf_data_file file = {
  255. .mode = PERF_DATA_MODE_READ,
  256. };
  257. struct perf_session *session = NULL;
  258. const struct option buildid_cache_options[] = {
  259. OPT_STRING('a', "add", &add_name_list_str,
  260. "file list", "file(s) to add"),
  261. OPT_STRING('k', "kcore", &kcore_filename,
  262. "file", "kcore file to add"),
  263. OPT_STRING('r', "remove", &remove_name_list_str, "file list",
  264. "file(s) to remove"),
  265. OPT_STRING('p', "purge", &purge_name_list_str, "path list",
  266. "path(s) to remove (remove old caches too)"),
  267. OPT_STRING('M', "missing", &missing_filename, "file",
  268. "to find missing build ids in the cache"),
  269. OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
  270. OPT_STRING('u', "update", &update_name_list_str, "file list",
  271. "file(s) to update"),
  272. OPT_INCR('v', "verbose", &verbose, "be more verbose"),
  273. OPT_END()
  274. };
  275. const char * const buildid_cache_usage[] = {
  276. "perf buildid-cache [<options>]",
  277. NULL
  278. };
  279. argc = parse_options(argc, argv, buildid_cache_options,
  280. buildid_cache_usage, 0);
  281. if (argc || (!add_name_list_str && !kcore_filename &&
  282. !remove_name_list_str && !purge_name_list_str &&
  283. !missing_filename && !update_name_list_str))
  284. usage_with_options(buildid_cache_usage, buildid_cache_options);
  285. if (missing_filename) {
  286. file.path = missing_filename;
  287. file.force = force;
  288. session = perf_session__new(&file, false, NULL);
  289. if (session == NULL)
  290. return -1;
  291. }
  292. if (symbol__init(session ? &session->header.env : NULL) < 0)
  293. goto out;
  294. setup_pager();
  295. if (add_name_list_str) {
  296. list = strlist__new(true, add_name_list_str);
  297. if (list) {
  298. strlist__for_each(pos, list)
  299. if (build_id_cache__add_file(pos->s)) {
  300. if (errno == EEXIST) {
  301. pr_debug("%s already in the cache\n",
  302. pos->s);
  303. continue;
  304. }
  305. pr_warning("Couldn't add %s: %s\n",
  306. pos->s, strerror_r(errno, sbuf, sizeof(sbuf)));
  307. }
  308. strlist__delete(list);
  309. }
  310. }
  311. if (remove_name_list_str) {
  312. list = strlist__new(true, remove_name_list_str);
  313. if (list) {
  314. strlist__for_each(pos, list)
  315. if (build_id_cache__remove_file(pos->s)) {
  316. if (errno == ENOENT) {
  317. pr_debug("%s wasn't in the cache\n",
  318. pos->s);
  319. continue;
  320. }
  321. pr_warning("Couldn't remove %s: %s\n",
  322. pos->s, strerror_r(errno, sbuf, sizeof(sbuf)));
  323. }
  324. strlist__delete(list);
  325. }
  326. }
  327. if (purge_name_list_str) {
  328. list = strlist__new(true, purge_name_list_str);
  329. if (list) {
  330. strlist__for_each(pos, list)
  331. if (build_id_cache__purge_path(pos->s)) {
  332. if (errno == ENOENT) {
  333. pr_debug("%s wasn't in the cache\n",
  334. pos->s);
  335. continue;
  336. }
  337. pr_warning("Couldn't remove %s: %s\n",
  338. pos->s, strerror_r(errno, sbuf, sizeof(sbuf)));
  339. }
  340. strlist__delete(list);
  341. }
  342. }
  343. if (missing_filename)
  344. ret = build_id_cache__fprintf_missing(session, stdout);
  345. if (update_name_list_str) {
  346. list = strlist__new(true, update_name_list_str);
  347. if (list) {
  348. strlist__for_each(pos, list)
  349. if (build_id_cache__update_file(pos->s)) {
  350. if (errno == ENOENT) {
  351. pr_debug("%s wasn't in the cache\n",
  352. pos->s);
  353. continue;
  354. }
  355. pr_warning("Couldn't update %s: %s\n",
  356. pos->s, strerror_r(errno, sbuf, sizeof(sbuf)));
  357. }
  358. strlist__delete(list);
  359. }
  360. }
  361. if (kcore_filename && build_id_cache__add_kcore(kcore_filename, force))
  362. pr_warning("Couldn't add %s\n", kcore_filename);
  363. out:
  364. if (session)
  365. perf_session__delete(session);
  366. return ret;
  367. }