build-id.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  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. /* Check if the given build_id cache is valid on current running system */
  175. static bool build_id_cache__valid_id(char *sbuild_id)
  176. {
  177. char real_sbuild_id[SBUILD_ID_SIZE] = "";
  178. char *pathname;
  179. int ret = 0;
  180. bool result = false;
  181. pathname = build_id_cache__origname(sbuild_id);
  182. if (!pathname)
  183. return false;
  184. if (!strcmp(pathname, DSO__NAME_KALLSYMS))
  185. ret = sysfs__sprintf_build_id("/", real_sbuild_id);
  186. else if (pathname[0] == '/')
  187. ret = filename__sprintf_build_id(pathname, real_sbuild_id);
  188. else
  189. ret = -EINVAL; /* Should we support other special DSO cache? */
  190. if (ret >= 0)
  191. result = (strcmp(sbuild_id, real_sbuild_id) == 0);
  192. free(pathname);
  193. return result;
  194. }
  195. static const char *build_id_cache__basename(bool is_kallsyms, bool is_vdso)
  196. {
  197. return is_kallsyms ? "kallsyms" : (is_vdso ? "vdso" : "elf");
  198. }
  199. char *dso__build_id_filename(const struct dso *dso, char *bf, size_t size)
  200. {
  201. bool is_kallsyms = dso__is_kallsyms((struct dso *)dso);
  202. bool is_vdso = dso__is_vdso((struct dso *)dso);
  203. char sbuild_id[SBUILD_ID_SIZE];
  204. char *linkname;
  205. bool alloc = (bf == NULL);
  206. int ret;
  207. if (!dso->has_build_id)
  208. return NULL;
  209. build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
  210. linkname = build_id_cache__linkname(sbuild_id, NULL, 0);
  211. if (!linkname)
  212. return NULL;
  213. /* Check if old style build_id cache */
  214. if (is_regular_file(linkname))
  215. ret = asnprintf(&bf, size, "%s", linkname);
  216. else
  217. ret = asnprintf(&bf, size, "%s/%s", linkname,
  218. build_id_cache__basename(is_kallsyms, is_vdso));
  219. if (ret < 0 || (!alloc && size < (unsigned int)ret))
  220. bf = NULL;
  221. free(linkname);
  222. return bf;
  223. }
  224. bool dso__build_id_is_kmod(const struct dso *dso, char *bf, size_t size)
  225. {
  226. char *id_name = NULL, *ch;
  227. struct stat sb;
  228. char sbuild_id[SBUILD_ID_SIZE];
  229. if (!dso->has_build_id)
  230. goto err;
  231. build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
  232. id_name = build_id_cache__linkname(sbuild_id, NULL, 0);
  233. if (!id_name)
  234. goto err;
  235. if (access(id_name, F_OK))
  236. goto err;
  237. if (lstat(id_name, &sb) == -1)
  238. goto err;
  239. if ((size_t)sb.st_size > size - 1)
  240. goto err;
  241. if (readlink(id_name, bf, size - 1) < 0)
  242. goto err;
  243. bf[sb.st_size] = '\0';
  244. /*
  245. * link should be:
  246. * ../../lib/modules/4.4.0-rc4/kernel/net/ipv4/netfilter/nf_nat_ipv4.ko/a09fe3eb3147dafa4e3b31dbd6257e4d696bdc92
  247. */
  248. ch = strrchr(bf, '/');
  249. if (!ch)
  250. goto err;
  251. if (ch - 3 < bf)
  252. goto err;
  253. free(id_name);
  254. return strncmp(".ko", ch - 3, 3) == 0;
  255. err:
  256. pr_err("Invalid build id: %s\n", id_name ? :
  257. dso->long_name ? :
  258. dso->short_name ? :
  259. "[unknown]");
  260. free(id_name);
  261. return false;
  262. }
  263. #define dsos__for_each_with_build_id(pos, head) \
  264. list_for_each_entry(pos, head, node) \
  265. if (!pos->has_build_id) \
  266. continue; \
  267. else
  268. static int write_buildid(const char *name, size_t name_len, u8 *build_id,
  269. pid_t pid, u16 misc, int fd)
  270. {
  271. int err;
  272. struct build_id_event b;
  273. size_t len;
  274. len = name_len + 1;
  275. len = PERF_ALIGN(len, NAME_ALIGN);
  276. memset(&b, 0, sizeof(b));
  277. memcpy(&b.build_id, build_id, BUILD_ID_SIZE);
  278. b.pid = pid;
  279. b.header.misc = misc;
  280. b.header.size = sizeof(b) + len;
  281. err = writen(fd, &b, sizeof(b));
  282. if (err < 0)
  283. return err;
  284. return write_padded(fd, name, name_len + 1, len);
  285. }
  286. static int machine__write_buildid_table(struct machine *machine, int fd)
  287. {
  288. int err = 0;
  289. char nm[PATH_MAX];
  290. struct dso *pos;
  291. u16 kmisc = PERF_RECORD_MISC_KERNEL,
  292. umisc = PERF_RECORD_MISC_USER;
  293. if (!machine__is_host(machine)) {
  294. kmisc = PERF_RECORD_MISC_GUEST_KERNEL;
  295. umisc = PERF_RECORD_MISC_GUEST_USER;
  296. }
  297. dsos__for_each_with_build_id(pos, &machine->dsos.head) {
  298. const char *name;
  299. size_t name_len;
  300. bool in_kernel = false;
  301. if (!pos->hit && !dso__is_vdso(pos))
  302. continue;
  303. if (dso__is_vdso(pos)) {
  304. name = pos->short_name;
  305. name_len = pos->short_name_len;
  306. } else if (dso__is_kcore(pos)) {
  307. machine__mmap_name(machine, nm, sizeof(nm));
  308. name = nm;
  309. name_len = strlen(nm);
  310. } else {
  311. name = pos->long_name;
  312. name_len = pos->long_name_len;
  313. }
  314. in_kernel = pos->kernel ||
  315. is_kernel_module(name,
  316. PERF_RECORD_MISC_CPUMODE_UNKNOWN);
  317. err = write_buildid(name, name_len, pos->build_id, machine->pid,
  318. in_kernel ? kmisc : umisc, fd);
  319. if (err)
  320. break;
  321. }
  322. return err;
  323. }
  324. int perf_session__write_buildid_table(struct perf_session *session, int fd)
  325. {
  326. struct rb_node *nd;
  327. int err = machine__write_buildid_table(&session->machines.host, fd);
  328. if (err)
  329. return err;
  330. for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
  331. struct machine *pos = rb_entry(nd, struct machine, rb_node);
  332. err = machine__write_buildid_table(pos, fd);
  333. if (err)
  334. break;
  335. }
  336. return err;
  337. }
  338. static int __dsos__hit_all(struct list_head *head)
  339. {
  340. struct dso *pos;
  341. list_for_each_entry(pos, head, node)
  342. pos->hit = true;
  343. return 0;
  344. }
  345. static int machine__hit_all_dsos(struct machine *machine)
  346. {
  347. return __dsos__hit_all(&machine->dsos.head);
  348. }
  349. int dsos__hit_all(struct perf_session *session)
  350. {
  351. struct rb_node *nd;
  352. int err;
  353. err = machine__hit_all_dsos(&session->machines.host);
  354. if (err)
  355. return err;
  356. for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
  357. struct machine *pos = rb_entry(nd, struct machine, rb_node);
  358. err = machine__hit_all_dsos(pos);
  359. if (err)
  360. return err;
  361. }
  362. return 0;
  363. }
  364. void disable_buildid_cache(void)
  365. {
  366. no_buildid_cache = true;
  367. }
  368. static bool lsdir_bid_head_filter(const char *name __maybe_unused,
  369. struct dirent *d __maybe_unused)
  370. {
  371. return (strlen(d->d_name) == 2) &&
  372. isxdigit(d->d_name[0]) && isxdigit(d->d_name[1]);
  373. }
  374. static bool lsdir_bid_tail_filter(const char *name __maybe_unused,
  375. struct dirent *d __maybe_unused)
  376. {
  377. int i = 0;
  378. while (isxdigit(d->d_name[i]) && i < SBUILD_ID_SIZE - 3)
  379. i++;
  380. return (i == SBUILD_ID_SIZE - 3) && (d->d_name[i] == '\0');
  381. }
  382. struct strlist *build_id_cache__list_all(bool validonly)
  383. {
  384. struct strlist *toplist, *linklist = NULL, *bidlist;
  385. struct str_node *nd, *nd2;
  386. char *topdir, *linkdir = NULL;
  387. char sbuild_id[SBUILD_ID_SIZE];
  388. /* for filename__ functions */
  389. if (validonly)
  390. symbol__init(NULL);
  391. /* Open the top-level directory */
  392. if (asprintf(&topdir, "%s/.build-id/", buildid_dir) < 0)
  393. return NULL;
  394. bidlist = strlist__new(NULL, NULL);
  395. if (!bidlist)
  396. goto out;
  397. toplist = lsdir(topdir, lsdir_bid_head_filter);
  398. if (!toplist) {
  399. pr_debug("Error in lsdir(%s): %d\n", topdir, errno);
  400. /* If there is no buildid cache, return an empty list */
  401. if (errno == ENOENT)
  402. goto out;
  403. goto err_out;
  404. }
  405. strlist__for_each_entry(nd, toplist) {
  406. if (asprintf(&linkdir, "%s/%s", topdir, nd->s) < 0)
  407. goto err_out;
  408. /* Open the lower-level directory */
  409. linklist = lsdir(linkdir, lsdir_bid_tail_filter);
  410. if (!linklist) {
  411. pr_debug("Error in lsdir(%s): %d\n", linkdir, errno);
  412. goto err_out;
  413. }
  414. strlist__for_each_entry(nd2, linklist) {
  415. if (snprintf(sbuild_id, SBUILD_ID_SIZE, "%s%s",
  416. nd->s, nd2->s) != SBUILD_ID_SIZE - 1)
  417. goto err_out;
  418. if (validonly && !build_id_cache__valid_id(sbuild_id))
  419. continue;
  420. if (strlist__add(bidlist, sbuild_id) < 0)
  421. goto err_out;
  422. }
  423. strlist__delete(linklist);
  424. zfree(&linkdir);
  425. }
  426. out_free:
  427. strlist__delete(toplist);
  428. out:
  429. free(topdir);
  430. return bidlist;
  431. err_out:
  432. strlist__delete(linklist);
  433. zfree(&linkdir);
  434. strlist__delete(bidlist);
  435. bidlist = NULL;
  436. goto out_free;
  437. }
  438. static bool str_is_build_id(const char *maybe_sbuild_id, size_t len)
  439. {
  440. size_t i;
  441. for (i = 0; i < len; i++) {
  442. if (!isxdigit(maybe_sbuild_id[i]))
  443. return false;
  444. }
  445. return true;
  446. }
  447. /* Return the valid complete build-id */
  448. char *build_id_cache__complement(const char *incomplete_sbuild_id)
  449. {
  450. struct strlist *bidlist;
  451. struct str_node *nd, *cand = NULL;
  452. char *sbuild_id = NULL;
  453. size_t len = strlen(incomplete_sbuild_id);
  454. if (len >= SBUILD_ID_SIZE ||
  455. !str_is_build_id(incomplete_sbuild_id, len))
  456. return NULL;
  457. bidlist = build_id_cache__list_all(true);
  458. if (!bidlist)
  459. return NULL;
  460. strlist__for_each_entry(nd, bidlist) {
  461. if (strncmp(nd->s, incomplete_sbuild_id, len) != 0)
  462. continue;
  463. if (cand) { /* Error: There are more than 2 candidates. */
  464. cand = NULL;
  465. break;
  466. }
  467. cand = nd;
  468. }
  469. if (cand)
  470. sbuild_id = strdup(cand->s);
  471. strlist__delete(bidlist);
  472. return sbuild_id;
  473. }
  474. char *build_id_cache__cachedir(const char *sbuild_id, const char *name,
  475. bool is_kallsyms, bool is_vdso)
  476. {
  477. char *realname = (char *)name, *filename;
  478. bool slash = is_kallsyms || is_vdso;
  479. if (!slash) {
  480. realname = realpath(name, NULL);
  481. if (!realname)
  482. return NULL;
  483. }
  484. if (asprintf(&filename, "%s%s%s%s%s", buildid_dir, slash ? "/" : "",
  485. is_vdso ? DSO__NAME_VDSO : realname,
  486. sbuild_id ? "/" : "", sbuild_id ?: "") < 0)
  487. filename = NULL;
  488. if (!slash)
  489. free(realname);
  490. return filename;
  491. }
  492. int build_id_cache__list_build_ids(const char *pathname,
  493. struct strlist **result)
  494. {
  495. char *dir_name;
  496. int ret = 0;
  497. dir_name = build_id_cache__cachedir(NULL, pathname, false, false);
  498. if (!dir_name)
  499. return -ENOMEM;
  500. *result = lsdir(dir_name, lsdir_no_dot_filter);
  501. if (!*result)
  502. ret = -errno;
  503. free(dir_name);
  504. return ret;
  505. }
  506. #if defined(HAVE_LIBELF_SUPPORT) && defined(HAVE_GELF_GETNOTE_SUPPORT)
  507. static int build_id_cache__add_sdt_cache(const char *sbuild_id,
  508. const char *realname)
  509. {
  510. struct probe_cache *cache;
  511. int ret;
  512. cache = probe_cache__new(sbuild_id);
  513. if (!cache)
  514. return -1;
  515. ret = probe_cache__scan_sdt(cache, realname);
  516. if (ret >= 0) {
  517. pr_debug4("Found %d SDTs in %s\n", ret, realname);
  518. if (probe_cache__commit(cache) < 0)
  519. ret = -1;
  520. }
  521. probe_cache__delete(cache);
  522. return ret;
  523. }
  524. #else
  525. #define build_id_cache__add_sdt_cache(sbuild_id, realname) (0)
  526. #endif
  527. int build_id_cache__add_s(const char *sbuild_id, const char *name,
  528. bool is_kallsyms, bool is_vdso)
  529. {
  530. const size_t size = PATH_MAX;
  531. char *realname = NULL, *filename = NULL, *dir_name = NULL,
  532. *linkname = zalloc(size), *tmp;
  533. int err = -1;
  534. if (!is_kallsyms) {
  535. realname = realpath(name, NULL);
  536. if (!realname)
  537. goto out_free;
  538. }
  539. dir_name = build_id_cache__cachedir(sbuild_id, name,
  540. is_kallsyms, is_vdso);
  541. if (!dir_name)
  542. goto out_free;
  543. /* Remove old style build-id cache */
  544. if (is_regular_file(dir_name))
  545. if (unlink(dir_name))
  546. goto out_free;
  547. if (mkdir_p(dir_name, 0755))
  548. goto out_free;
  549. /* Save the allocated buildid dirname */
  550. if (asprintf(&filename, "%s/%s", dir_name,
  551. build_id_cache__basename(is_kallsyms, is_vdso)) < 0) {
  552. filename = NULL;
  553. goto out_free;
  554. }
  555. if (access(filename, F_OK)) {
  556. if (is_kallsyms) {
  557. if (copyfile("/proc/kallsyms", filename))
  558. goto out_free;
  559. } else if (link(realname, filename) && errno != EEXIST &&
  560. copyfile(name, filename))
  561. goto out_free;
  562. }
  563. if (!build_id_cache__linkname(sbuild_id, linkname, size))
  564. goto out_free;
  565. tmp = strrchr(linkname, '/');
  566. *tmp = '\0';
  567. if (access(linkname, X_OK) && mkdir_p(linkname, 0755))
  568. goto out_free;
  569. *tmp = '/';
  570. tmp = dir_name + strlen(buildid_dir) - 5;
  571. memcpy(tmp, "../..", 5);
  572. if (symlink(tmp, linkname) == 0)
  573. err = 0;
  574. /* Update SDT cache : error is just warned */
  575. if (build_id_cache__add_sdt_cache(sbuild_id, realname) < 0)
  576. pr_debug4("Failed to update/scan SDT cache for %s\n", realname);
  577. out_free:
  578. if (!is_kallsyms)
  579. free(realname);
  580. free(filename);
  581. free(dir_name);
  582. free(linkname);
  583. return err;
  584. }
  585. static int build_id_cache__add_b(const u8 *build_id, size_t build_id_size,
  586. const char *name, bool is_kallsyms,
  587. bool is_vdso)
  588. {
  589. char sbuild_id[SBUILD_ID_SIZE];
  590. build_id__sprintf(build_id, build_id_size, sbuild_id);
  591. return build_id_cache__add_s(sbuild_id, name, is_kallsyms, is_vdso);
  592. }
  593. bool build_id_cache__cached(const char *sbuild_id)
  594. {
  595. bool ret = false;
  596. char *filename = build_id_cache__linkname(sbuild_id, NULL, 0);
  597. if (filename && !access(filename, F_OK))
  598. ret = true;
  599. free(filename);
  600. return ret;
  601. }
  602. int build_id_cache__remove_s(const char *sbuild_id)
  603. {
  604. const size_t size = PATH_MAX;
  605. char *filename = zalloc(size),
  606. *linkname = zalloc(size), *tmp;
  607. int err = -1;
  608. if (filename == NULL || linkname == NULL)
  609. goto out_free;
  610. if (!build_id_cache__linkname(sbuild_id, linkname, size))
  611. goto out_free;
  612. if (access(linkname, F_OK))
  613. goto out_free;
  614. if (readlink(linkname, filename, size - 1) < 0)
  615. goto out_free;
  616. if (unlink(linkname))
  617. goto out_free;
  618. /*
  619. * Since the link is relative, we must make it absolute:
  620. */
  621. tmp = strrchr(linkname, '/') + 1;
  622. snprintf(tmp, size - (tmp - linkname), "%s", filename);
  623. if (rm_rf(linkname))
  624. goto out_free;
  625. err = 0;
  626. out_free:
  627. free(filename);
  628. free(linkname);
  629. return err;
  630. }
  631. static int dso__cache_build_id(struct dso *dso, struct machine *machine)
  632. {
  633. bool is_kallsyms = dso__is_kallsyms(dso);
  634. bool is_vdso = dso__is_vdso(dso);
  635. const char *name = dso->long_name;
  636. char nm[PATH_MAX];
  637. if (dso__is_kcore(dso)) {
  638. is_kallsyms = true;
  639. machine__mmap_name(machine, nm, sizeof(nm));
  640. name = nm;
  641. }
  642. return build_id_cache__add_b(dso->build_id, sizeof(dso->build_id), name,
  643. is_kallsyms, is_vdso);
  644. }
  645. static int __dsos__cache_build_ids(struct list_head *head,
  646. struct machine *machine)
  647. {
  648. struct dso *pos;
  649. int err = 0;
  650. dsos__for_each_with_build_id(pos, head)
  651. if (dso__cache_build_id(pos, machine))
  652. err = -1;
  653. return err;
  654. }
  655. static int machine__cache_build_ids(struct machine *machine)
  656. {
  657. return __dsos__cache_build_ids(&machine->dsos.head, machine);
  658. }
  659. int perf_session__cache_build_ids(struct perf_session *session)
  660. {
  661. struct rb_node *nd;
  662. int ret;
  663. if (no_buildid_cache)
  664. return 0;
  665. if (mkdir(buildid_dir, 0755) != 0 && errno != EEXIST)
  666. return -1;
  667. ret = machine__cache_build_ids(&session->machines.host);
  668. for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
  669. struct machine *pos = rb_entry(nd, struct machine, rb_node);
  670. ret |= machine__cache_build_ids(pos);
  671. }
  672. return ret ? -1 : 0;
  673. }
  674. static bool machine__read_build_ids(struct machine *machine, bool with_hits)
  675. {
  676. return __dsos__read_build_ids(&machine->dsos.head, with_hits);
  677. }
  678. bool perf_session__read_build_ids(struct perf_session *session, bool with_hits)
  679. {
  680. struct rb_node *nd;
  681. bool ret = machine__read_build_ids(&session->machines.host, with_hits);
  682. for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
  683. struct machine *pos = rb_entry(nd, struct machine, rb_node);
  684. ret |= machine__read_build_ids(pos, with_hits);
  685. }
  686. return ret;
  687. }