build-id.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. /*
  2. * build-id.c
  3. *
  4. * build-id support
  5. *
  6. * Copyright (C) 2009, 2010 Red Hat Inc.
  7. * Copyright (C) 2009, 2010 Arnaldo Carvalho de Melo <acme@redhat.com>
  8. */
  9. #include "util.h"
  10. #include <stdio.h>
  11. #include "build-id.h"
  12. #include "event.h"
  13. #include "symbol.h"
  14. #include <linux/kernel.h>
  15. #include "debug.h"
  16. #include "session.h"
  17. #include "tool.h"
  18. #include "header.h"
  19. #include "vdso.h"
  20. #include "probe-file.h"
  21. static bool no_buildid_cache;
  22. int build_id__mark_dso_hit(struct perf_tool *tool __maybe_unused,
  23. union perf_event *event,
  24. struct perf_sample *sample,
  25. struct perf_evsel *evsel __maybe_unused,
  26. struct machine *machine)
  27. {
  28. struct addr_location al;
  29. struct thread *thread = machine__findnew_thread(machine, sample->pid,
  30. sample->tid);
  31. if (thread == NULL) {
  32. pr_err("problem processing %d event, skipping it.\n",
  33. event->header.type);
  34. return -1;
  35. }
  36. thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, sample->ip, &al);
  37. if (al.map != NULL)
  38. al.map->dso->hit = 1;
  39. thread__put(thread);
  40. return 0;
  41. }
  42. static int perf_event__exit_del_thread(struct perf_tool *tool __maybe_unused,
  43. union perf_event *event,
  44. struct perf_sample *sample
  45. __maybe_unused,
  46. struct machine *machine)
  47. {
  48. struct thread *thread = machine__findnew_thread(machine,
  49. event->fork.pid,
  50. event->fork.tid);
  51. dump_printf("(%d:%d):(%d:%d)\n", event->fork.pid, event->fork.tid,
  52. event->fork.ppid, event->fork.ptid);
  53. if (thread) {
  54. machine__remove_thread(machine, thread);
  55. thread__put(thread);
  56. }
  57. return 0;
  58. }
  59. struct perf_tool build_id__mark_dso_hit_ops = {
  60. .sample = build_id__mark_dso_hit,
  61. .mmap = perf_event__process_mmap,
  62. .mmap2 = perf_event__process_mmap2,
  63. .fork = perf_event__process_fork,
  64. .exit = perf_event__exit_del_thread,
  65. .attr = perf_event__process_attr,
  66. .build_id = perf_event__process_build_id,
  67. .ordered_events = true,
  68. };
  69. int build_id__sprintf(const u8 *build_id, int len, char *bf)
  70. {
  71. char *bid = bf;
  72. const u8 *raw = build_id;
  73. int i;
  74. for (i = 0; i < len; ++i) {
  75. sprintf(bid, "%02x", *raw);
  76. ++raw;
  77. bid += 2;
  78. }
  79. return (bid - bf) + 1;
  80. }
  81. int sysfs__sprintf_build_id(const char *root_dir, char *sbuild_id)
  82. {
  83. char notes[PATH_MAX];
  84. u8 build_id[BUILD_ID_SIZE];
  85. int ret;
  86. if (!root_dir)
  87. root_dir = "";
  88. scnprintf(notes, sizeof(notes), "%s/sys/kernel/notes", root_dir);
  89. ret = sysfs__read_build_id(notes, build_id, sizeof(build_id));
  90. if (ret < 0)
  91. return ret;
  92. return build_id__sprintf(build_id, sizeof(build_id), sbuild_id);
  93. }
  94. int filename__sprintf_build_id(const char *pathname, char *sbuild_id)
  95. {
  96. u8 build_id[BUILD_ID_SIZE];
  97. int ret;
  98. ret = filename__read_build_id(pathname, build_id, sizeof(build_id));
  99. if (ret < 0)
  100. return ret;
  101. else if (ret != sizeof(build_id))
  102. return -EINVAL;
  103. return build_id__sprintf(build_id, sizeof(build_id), sbuild_id);
  104. }
  105. /* asnprintf consolidates asprintf and snprintf */
  106. static int asnprintf(char **strp, size_t size, const char *fmt, ...)
  107. {
  108. va_list ap;
  109. int ret;
  110. if (!strp)
  111. return -EINVAL;
  112. va_start(ap, fmt);
  113. if (*strp)
  114. ret = vsnprintf(*strp, size, fmt, ap);
  115. else
  116. ret = vasprintf(strp, fmt, ap);
  117. va_end(ap);
  118. return ret;
  119. }
  120. char *build_id_cache__kallsyms_path(const char *sbuild_id, char *bf,
  121. size_t size)
  122. {
  123. bool retry_old = true;
  124. snprintf(bf, size, "%s/%s/%s/kallsyms",
  125. buildid_dir, DSO__NAME_KALLSYMS, sbuild_id);
  126. retry:
  127. if (!access(bf, F_OK))
  128. return bf;
  129. if (retry_old) {
  130. /* Try old style kallsyms cache */
  131. snprintf(bf, size, "%s/%s/%s",
  132. buildid_dir, DSO__NAME_KALLSYMS, sbuild_id);
  133. retry_old = false;
  134. goto retry;
  135. }
  136. return NULL;
  137. }
  138. char *build_id_cache__linkname(const char *sbuild_id, char *bf, size_t size)
  139. {
  140. char *tmp = bf;
  141. int ret = asnprintf(&bf, size, "%s/.build-id/%.2s/%s", buildid_dir,
  142. sbuild_id, sbuild_id + 2);
  143. if (ret < 0 || (tmp && size < (unsigned int)ret))
  144. return NULL;
  145. return bf;
  146. }
  147. char *build_id_cache__origname(const char *sbuild_id)
  148. {
  149. char *linkname;
  150. char buf[PATH_MAX];
  151. char *ret = NULL, *p;
  152. size_t offs = 5; /* == strlen("../..") */
  153. linkname = build_id_cache__linkname(sbuild_id, NULL, 0);
  154. if (!linkname)
  155. return NULL;
  156. if (readlink(linkname, buf, PATH_MAX) < 0)
  157. goto out;
  158. /* The link should be "../..<origpath>/<sbuild_id>" */
  159. p = strrchr(buf, '/'); /* Cut off the "/<sbuild_id>" */
  160. if (p && (p > buf + offs)) {
  161. *p = '\0';
  162. if (buf[offs + 1] == '[')
  163. offs++; /*
  164. * This is a DSO name, like [kernel.kallsyms].
  165. * Skip the first '/', since this is not the
  166. * cache of a regular file.
  167. */
  168. ret = strdup(buf + offs); /* Skip "../..[/]" */
  169. }
  170. out:
  171. free(linkname);
  172. return ret;
  173. }
  174. static const char *build_id_cache__basename(bool is_kallsyms, bool is_vdso)
  175. {
  176. return is_kallsyms ? "kallsyms" : (is_vdso ? "vdso" : "elf");
  177. }
  178. char *dso__build_id_filename(const struct dso *dso, char *bf, size_t size)
  179. {
  180. bool is_kallsyms = dso__is_kallsyms((struct dso *)dso);
  181. bool is_vdso = dso__is_vdso((struct dso *)dso);
  182. char sbuild_id[SBUILD_ID_SIZE];
  183. char *linkname;
  184. bool alloc = (bf == NULL);
  185. int ret;
  186. if (!dso->has_build_id)
  187. return NULL;
  188. build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
  189. linkname = build_id_cache__linkname(sbuild_id, NULL, 0);
  190. if (!linkname)
  191. return NULL;
  192. /* Check if old style build_id cache */
  193. if (is_regular_file(linkname))
  194. ret = asnprintf(&bf, size, "%s", linkname);
  195. else
  196. ret = asnprintf(&bf, size, "%s/%s", linkname,
  197. build_id_cache__basename(is_kallsyms, is_vdso));
  198. if (ret < 0 || (!alloc && size < (unsigned int)ret))
  199. bf = NULL;
  200. free(linkname);
  201. return bf;
  202. }
  203. bool dso__build_id_is_kmod(const struct dso *dso, char *bf, size_t size)
  204. {
  205. char *id_name = NULL, *ch;
  206. struct stat sb;
  207. char sbuild_id[SBUILD_ID_SIZE];
  208. if (!dso->has_build_id)
  209. goto err;
  210. build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
  211. id_name = build_id_cache__linkname(sbuild_id, NULL, 0);
  212. if (!id_name)
  213. goto err;
  214. if (access(id_name, F_OK))
  215. goto err;
  216. if (lstat(id_name, &sb) == -1)
  217. goto err;
  218. if ((size_t)sb.st_size > size - 1)
  219. goto err;
  220. if (readlink(id_name, bf, size - 1) < 0)
  221. goto err;
  222. bf[sb.st_size] = '\0';
  223. /*
  224. * link should be:
  225. * ../../lib/modules/4.4.0-rc4/kernel/net/ipv4/netfilter/nf_nat_ipv4.ko/a09fe3eb3147dafa4e3b31dbd6257e4d696bdc92
  226. */
  227. ch = strrchr(bf, '/');
  228. if (!ch)
  229. goto err;
  230. if (ch - 3 < bf)
  231. goto err;
  232. free(id_name);
  233. return strncmp(".ko", ch - 3, 3) == 0;
  234. err:
  235. pr_err("Invalid build id: %s\n", id_name ? :
  236. dso->long_name ? :
  237. dso->short_name ? :
  238. "[unknown]");
  239. free(id_name);
  240. return false;
  241. }
  242. #define dsos__for_each_with_build_id(pos, head) \
  243. list_for_each_entry(pos, head, node) \
  244. if (!pos->has_build_id) \
  245. continue; \
  246. else
  247. static int write_buildid(const char *name, size_t name_len, u8 *build_id,
  248. pid_t pid, u16 misc, int fd)
  249. {
  250. int err;
  251. struct build_id_event b;
  252. size_t len;
  253. len = name_len + 1;
  254. len = PERF_ALIGN(len, NAME_ALIGN);
  255. memset(&b, 0, sizeof(b));
  256. memcpy(&b.build_id, build_id, BUILD_ID_SIZE);
  257. b.pid = pid;
  258. b.header.misc = misc;
  259. b.header.size = sizeof(b) + len;
  260. err = writen(fd, &b, sizeof(b));
  261. if (err < 0)
  262. return err;
  263. return write_padded(fd, name, name_len + 1, len);
  264. }
  265. static int machine__write_buildid_table(struct machine *machine, int fd)
  266. {
  267. int err = 0;
  268. char nm[PATH_MAX];
  269. struct dso *pos;
  270. u16 kmisc = PERF_RECORD_MISC_KERNEL,
  271. umisc = PERF_RECORD_MISC_USER;
  272. if (!machine__is_host(machine)) {
  273. kmisc = PERF_RECORD_MISC_GUEST_KERNEL;
  274. umisc = PERF_RECORD_MISC_GUEST_USER;
  275. }
  276. dsos__for_each_with_build_id(pos, &machine->dsos.head) {
  277. const char *name;
  278. size_t name_len;
  279. bool in_kernel = false;
  280. if (!pos->hit && !dso__is_vdso(pos))
  281. continue;
  282. if (dso__is_vdso(pos)) {
  283. name = pos->short_name;
  284. name_len = pos->short_name_len;
  285. } else if (dso__is_kcore(pos)) {
  286. machine__mmap_name(machine, nm, sizeof(nm));
  287. name = nm;
  288. name_len = strlen(nm);
  289. } else {
  290. name = pos->long_name;
  291. name_len = pos->long_name_len;
  292. }
  293. in_kernel = pos->kernel ||
  294. is_kernel_module(name,
  295. PERF_RECORD_MISC_CPUMODE_UNKNOWN);
  296. err = write_buildid(name, name_len, pos->build_id, machine->pid,
  297. in_kernel ? kmisc : umisc, fd);
  298. if (err)
  299. break;
  300. }
  301. return err;
  302. }
  303. int perf_session__write_buildid_table(struct perf_session *session, int fd)
  304. {
  305. struct rb_node *nd;
  306. int err = machine__write_buildid_table(&session->machines.host, fd);
  307. if (err)
  308. return err;
  309. for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
  310. struct machine *pos = rb_entry(nd, struct machine, rb_node);
  311. err = machine__write_buildid_table(pos, fd);
  312. if (err)
  313. break;
  314. }
  315. return err;
  316. }
  317. static int __dsos__hit_all(struct list_head *head)
  318. {
  319. struct dso *pos;
  320. list_for_each_entry(pos, head, node)
  321. pos->hit = true;
  322. return 0;
  323. }
  324. static int machine__hit_all_dsos(struct machine *machine)
  325. {
  326. return __dsos__hit_all(&machine->dsos.head);
  327. }
  328. int dsos__hit_all(struct perf_session *session)
  329. {
  330. struct rb_node *nd;
  331. int err;
  332. err = machine__hit_all_dsos(&session->machines.host);
  333. if (err)
  334. return err;
  335. for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
  336. struct machine *pos = rb_entry(nd, struct machine, rb_node);
  337. err = machine__hit_all_dsos(pos);
  338. if (err)
  339. return err;
  340. }
  341. return 0;
  342. }
  343. void disable_buildid_cache(void)
  344. {
  345. no_buildid_cache = true;
  346. }
  347. static bool lsdir_bid_head_filter(const char *name __maybe_unused,
  348. struct dirent *d __maybe_unused)
  349. {
  350. return (strlen(d->d_name) == 2) &&
  351. isxdigit(d->d_name[0]) && isxdigit(d->d_name[1]);
  352. }
  353. static bool lsdir_bid_tail_filter(const char *name __maybe_unused,
  354. struct dirent *d __maybe_unused)
  355. {
  356. int i = 0;
  357. while (isxdigit(d->d_name[i]) && i < SBUILD_ID_SIZE - 3)
  358. i++;
  359. return (i == SBUILD_ID_SIZE - 3) && (d->d_name[i] == '\0');
  360. }
  361. struct strlist *build_id_cache__list_all(void)
  362. {
  363. struct strlist *toplist, *linklist = NULL, *bidlist;
  364. struct str_node *nd, *nd2;
  365. char *topdir, *linkdir = NULL;
  366. char sbuild_id[SBUILD_ID_SIZE];
  367. /* Open the top-level directory */
  368. if (asprintf(&topdir, "%s/.build-id/", buildid_dir) < 0)
  369. return NULL;
  370. bidlist = strlist__new(NULL, NULL);
  371. if (!bidlist)
  372. goto out;
  373. toplist = lsdir(topdir, lsdir_bid_head_filter);
  374. if (!toplist) {
  375. pr_debug("Error in lsdir(%s): %d\n", topdir, errno);
  376. /* If there is no buildid cache, return an empty list */
  377. if (errno == ENOENT)
  378. goto out;
  379. goto err_out;
  380. }
  381. strlist__for_each_entry(nd, toplist) {
  382. if (asprintf(&linkdir, "%s/%s", topdir, nd->s) < 0)
  383. goto err_out;
  384. /* Open the lower-level directory */
  385. linklist = lsdir(linkdir, lsdir_bid_tail_filter);
  386. if (!linklist) {
  387. pr_debug("Error in lsdir(%s): %d\n", linkdir, errno);
  388. goto err_out;
  389. }
  390. strlist__for_each_entry(nd2, linklist) {
  391. if (snprintf(sbuild_id, SBUILD_ID_SIZE, "%s%s",
  392. nd->s, nd2->s) != SBUILD_ID_SIZE - 1)
  393. goto err_out;
  394. if (strlist__add(bidlist, sbuild_id) < 0)
  395. goto err_out;
  396. }
  397. strlist__delete(linklist);
  398. zfree(&linkdir);
  399. }
  400. out_free:
  401. strlist__delete(toplist);
  402. out:
  403. free(topdir);
  404. return bidlist;
  405. err_out:
  406. strlist__delete(linklist);
  407. zfree(&linkdir);
  408. strlist__delete(bidlist);
  409. bidlist = NULL;
  410. goto out_free;
  411. }
  412. char *build_id_cache__cachedir(const char *sbuild_id, const char *name,
  413. bool is_kallsyms, bool is_vdso)
  414. {
  415. char *realname = (char *)name, *filename;
  416. bool slash = is_kallsyms || is_vdso;
  417. if (!slash) {
  418. realname = realpath(name, NULL);
  419. if (!realname)
  420. return NULL;
  421. }
  422. if (asprintf(&filename, "%s%s%s%s%s", buildid_dir, slash ? "/" : "",
  423. is_vdso ? DSO__NAME_VDSO : realname,
  424. sbuild_id ? "/" : "", sbuild_id ?: "") < 0)
  425. filename = NULL;
  426. if (!slash)
  427. free(realname);
  428. return filename;
  429. }
  430. int build_id_cache__list_build_ids(const char *pathname,
  431. struct strlist **result)
  432. {
  433. char *dir_name;
  434. int ret = 0;
  435. dir_name = build_id_cache__cachedir(NULL, pathname, false, false);
  436. if (!dir_name)
  437. return -ENOMEM;
  438. *result = lsdir(dir_name, lsdir_no_dot_filter);
  439. if (!*result)
  440. ret = -errno;
  441. free(dir_name);
  442. return ret;
  443. }
  444. #ifdef HAVE_LIBELF_SUPPORT
  445. static int build_id_cache__add_sdt_cache(const char *sbuild_id,
  446. const char *realname)
  447. {
  448. struct probe_cache *cache;
  449. int ret;
  450. cache = probe_cache__new(sbuild_id);
  451. if (!cache)
  452. return -1;
  453. ret = probe_cache__scan_sdt(cache, realname);
  454. if (ret >= 0) {
  455. pr_debug("Found %d SDTs in %s\n", ret, realname);
  456. if (probe_cache__commit(cache) < 0)
  457. ret = -1;
  458. }
  459. probe_cache__delete(cache);
  460. return ret;
  461. }
  462. #else
  463. #define build_id_cache__add_sdt_cache(sbuild_id, realname) (0)
  464. #endif
  465. int build_id_cache__add_s(const char *sbuild_id, const char *name,
  466. bool is_kallsyms, bool is_vdso)
  467. {
  468. const size_t size = PATH_MAX;
  469. char *realname = NULL, *filename = NULL, *dir_name = NULL,
  470. *linkname = zalloc(size), *tmp;
  471. int err = -1;
  472. if (!is_kallsyms) {
  473. realname = realpath(name, NULL);
  474. if (!realname)
  475. goto out_free;
  476. }
  477. dir_name = build_id_cache__cachedir(sbuild_id, name,
  478. is_kallsyms, is_vdso);
  479. if (!dir_name)
  480. goto out_free;
  481. /* Remove old style build-id cache */
  482. if (is_regular_file(dir_name))
  483. if (unlink(dir_name))
  484. goto out_free;
  485. if (mkdir_p(dir_name, 0755))
  486. goto out_free;
  487. /* Save the allocated buildid dirname */
  488. if (asprintf(&filename, "%s/%s", dir_name,
  489. build_id_cache__basename(is_kallsyms, is_vdso)) < 0) {
  490. filename = NULL;
  491. goto out_free;
  492. }
  493. if (access(filename, F_OK)) {
  494. if (is_kallsyms) {
  495. if (copyfile("/proc/kallsyms", filename))
  496. goto out_free;
  497. } else if (link(realname, filename) && errno != EEXIST &&
  498. copyfile(name, filename))
  499. goto out_free;
  500. }
  501. if (!build_id_cache__linkname(sbuild_id, linkname, size))
  502. goto out_free;
  503. tmp = strrchr(linkname, '/');
  504. *tmp = '\0';
  505. if (access(linkname, X_OK) && mkdir_p(linkname, 0755))
  506. goto out_free;
  507. *tmp = '/';
  508. tmp = dir_name + strlen(buildid_dir) - 5;
  509. memcpy(tmp, "../..", 5);
  510. if (symlink(tmp, linkname) == 0)
  511. err = 0;
  512. /* Update SDT cache : error is just warned */
  513. if (build_id_cache__add_sdt_cache(sbuild_id, realname) < 0)
  514. pr_debug("Failed to update/scan SDT cache for %s\n", realname);
  515. out_free:
  516. if (!is_kallsyms)
  517. free(realname);
  518. free(filename);
  519. free(dir_name);
  520. free(linkname);
  521. return err;
  522. }
  523. static int build_id_cache__add_b(const u8 *build_id, size_t build_id_size,
  524. const char *name, bool is_kallsyms,
  525. bool is_vdso)
  526. {
  527. char sbuild_id[SBUILD_ID_SIZE];
  528. build_id__sprintf(build_id, build_id_size, sbuild_id);
  529. return build_id_cache__add_s(sbuild_id, name, is_kallsyms, is_vdso);
  530. }
  531. bool build_id_cache__cached(const char *sbuild_id)
  532. {
  533. bool ret = false;
  534. char *filename = build_id_cache__linkname(sbuild_id, NULL, 0);
  535. if (filename && !access(filename, F_OK))
  536. ret = true;
  537. free(filename);
  538. return ret;
  539. }
  540. int build_id_cache__remove_s(const char *sbuild_id)
  541. {
  542. const size_t size = PATH_MAX;
  543. char *filename = zalloc(size),
  544. *linkname = zalloc(size), *tmp;
  545. int err = -1;
  546. if (filename == NULL || linkname == NULL)
  547. goto out_free;
  548. if (!build_id_cache__linkname(sbuild_id, linkname, size))
  549. goto out_free;
  550. if (access(linkname, F_OK))
  551. goto out_free;
  552. if (readlink(linkname, filename, size - 1) < 0)
  553. goto out_free;
  554. if (unlink(linkname))
  555. goto out_free;
  556. /*
  557. * Since the link is relative, we must make it absolute:
  558. */
  559. tmp = strrchr(linkname, '/') + 1;
  560. snprintf(tmp, size - (tmp - linkname), "%s", filename);
  561. if (rm_rf(linkname))
  562. goto out_free;
  563. err = 0;
  564. out_free:
  565. free(filename);
  566. free(linkname);
  567. return err;
  568. }
  569. static int dso__cache_build_id(struct dso *dso, struct machine *machine)
  570. {
  571. bool is_kallsyms = dso__is_kallsyms(dso);
  572. bool is_vdso = dso__is_vdso(dso);
  573. const char *name = dso->long_name;
  574. char nm[PATH_MAX];
  575. if (dso__is_kcore(dso)) {
  576. is_kallsyms = true;
  577. machine__mmap_name(machine, nm, sizeof(nm));
  578. name = nm;
  579. }
  580. return build_id_cache__add_b(dso->build_id, sizeof(dso->build_id), name,
  581. is_kallsyms, is_vdso);
  582. }
  583. static int __dsos__cache_build_ids(struct list_head *head,
  584. struct machine *machine)
  585. {
  586. struct dso *pos;
  587. int err = 0;
  588. dsos__for_each_with_build_id(pos, head)
  589. if (dso__cache_build_id(pos, machine))
  590. err = -1;
  591. return err;
  592. }
  593. static int machine__cache_build_ids(struct machine *machine)
  594. {
  595. return __dsos__cache_build_ids(&machine->dsos.head, machine);
  596. }
  597. int perf_session__cache_build_ids(struct perf_session *session)
  598. {
  599. struct rb_node *nd;
  600. int ret;
  601. if (no_buildid_cache)
  602. return 0;
  603. if (mkdir(buildid_dir, 0755) != 0 && errno != EEXIST)
  604. return -1;
  605. ret = machine__cache_build_ids(&session->machines.host);
  606. for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
  607. struct machine *pos = rb_entry(nd, struct machine, rb_node);
  608. ret |= machine__cache_build_ids(pos);
  609. }
  610. return ret ? -1 : 0;
  611. }
  612. static bool machine__read_build_ids(struct machine *machine, bool with_hits)
  613. {
  614. return __dsos__read_build_ids(&machine->dsos.head, with_hits);
  615. }
  616. bool perf_session__read_build_ids(struct perf_session *session, bool with_hits)
  617. {
  618. struct rb_node *nd;
  619. bool ret = machine__read_build_ids(&session->machines.host, with_hits);
  620. for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
  621. struct machine *pos = rb_entry(nd, struct machine, rb_node);
  622. ret |= machine__read_build_ids(pos, with_hits);
  623. }
  624. return ret;
  625. }