build-id.c 19 KB

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