builtin-buildid-cache.c 10 KB

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