builtin-buildid-cache.c 10 KB

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