build-id.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * build-id.c
  4. *
  5. * build-id support
  6. *
  7. * Copyright (C) 2009, 2010 Red Hat Inc.
  8. * Copyright (C) 2009, 2010 Arnaldo Carvalho de Melo <acme@redhat.com>
  9. */
  10. #include "util.h"
  11. #include <dirent.h>
  12. #include <errno.h>
  13. #include <stdio.h>
  14. #include <sys/stat.h>
  15. #include <sys/types.h>
  16. #include "build-id.h"
  17. #include "event.h"
  18. #include "symbol.h"
  19. #include "thread.h"
  20. #include <linux/kernel.h>
  21. #include "debug.h"
  22. #include "session.h"
  23. #include "tool.h"
  24. #include "header.h"
  25. #include "vdso.h"
  26. #include "path.h"
  27. #include "probe-file.h"
  28. #include "strlist.h"
  29. #include "sane_ctype.h"
  30. static bool no_buildid_cache;
  31. int build_id__mark_dso_hit(struct perf_tool *tool __maybe_unused,
  32. union perf_event *event,
  33. struct perf_sample *sample,
  34. struct perf_evsel *evsel __maybe_unused,
  35. struct machine *machine)
  36. {
  37. struct addr_location al;
  38. struct thread *thread = machine__findnew_thread(machine, sample->pid,
  39. sample->tid);
  40. if (thread == NULL) {
  41. pr_err("problem processing %d event, skipping it.\n",
  42. event->header.type);
  43. return -1;
  44. }
  45. if (thread__find_map(thread, sample->cpumode, sample->ip, &al))
  46. al.map->dso->hit = 1;
  47. thread__put(thread);
  48. return 0;
  49. }
  50. static int perf_event__exit_del_thread(struct perf_tool *tool __maybe_unused,
  51. union perf_event *event,
  52. struct perf_sample *sample
  53. __maybe_unused,
  54. struct machine *machine)
  55. {
  56. struct thread *thread = machine__findnew_thread(machine,
  57. event->fork.pid,
  58. event->fork.tid);
  59. dump_printf("(%d:%d):(%d:%d)\n", event->fork.pid, event->fork.tid,
  60. event->fork.ppid, event->fork.ptid);
  61. if (thread) {
  62. machine__remove_thread(machine, thread);
  63. thread__put(thread);
  64. }
  65. return 0;
  66. }
  67. struct perf_tool build_id__mark_dso_hit_ops = {
  68. .sample = build_id__mark_dso_hit,
  69. .mmap = perf_event__process_mmap,
  70. .mmap2 = perf_event__process_mmap2,
  71. .fork = perf_event__process_fork,
  72. .exit = perf_event__exit_del_thread,
  73. .attr = perf_event__process_attr,
  74. .build_id = perf_event__process_build_id,
  75. .ordered_events = true,
  76. };
  77. int build_id__sprintf(const u8 *build_id, int len, char *bf)
  78. {
  79. char *bid = bf;
  80. const u8 *raw = build_id;
  81. int i;
  82. for (i = 0; i < len; ++i) {
  83. sprintf(bid, "%02x", *raw);
  84. ++raw;
  85. bid += 2;
  86. }
  87. return (bid - bf) + 1;
  88. }
  89. int sysfs__sprintf_build_id(const char *root_dir, char *sbuild_id)
  90. {
  91. char notes[PATH_MAX];
  92. u8 build_id[BUILD_ID_SIZE];
  93. int ret;
  94. if (!root_dir)
  95. root_dir = "";
  96. scnprintf(notes, sizeof(notes), "%s/sys/kernel/notes", root_dir);
  97. ret = sysfs__read_build_id(notes, build_id, sizeof(build_id));
  98. if (ret < 0)
  99. return ret;
  100. return build_id__sprintf(build_id, sizeof(build_id), sbuild_id);
  101. }
  102. int filename__sprintf_build_id(const char *pathname, char *sbuild_id)
  103. {
  104. u8 build_id[BUILD_ID_SIZE];
  105. int ret;
  106. ret = filename__read_build_id(pathname, build_id, sizeof(build_id));
  107. if (ret < 0)
  108. return ret;
  109. else if (ret != sizeof(build_id))
  110. return -EINVAL;
  111. return build_id__sprintf(build_id, sizeof(build_id), sbuild_id);
  112. }
  113. /* asnprintf consolidates asprintf and snprintf */
  114. static int asnprintf(char **strp, size_t size, const char *fmt, ...)
  115. {
  116. va_list ap;
  117. int ret;
  118. if (!strp)
  119. return -EINVAL;
  120. va_start(ap, fmt);
  121. if (*strp)
  122. ret = vsnprintf(*strp, size, fmt, ap);
  123. else
  124. ret = vasprintf(strp, fmt, ap);
  125. va_end(ap);
  126. return ret;
  127. }
  128. char *build_id_cache__kallsyms_path(const char *sbuild_id, char *bf,
  129. size_t size)
  130. {
  131. bool retry_old = true;
  132. snprintf(bf, size, "%s/%s/%s/kallsyms",
  133. buildid_dir, DSO__NAME_KALLSYMS, sbuild_id);
  134. retry:
  135. if (!access(bf, F_OK))
  136. return bf;
  137. if (retry_old) {
  138. /* Try old style kallsyms cache */
  139. snprintf(bf, size, "%s/%s/%s",
  140. buildid_dir, DSO__NAME_KALLSYMS, sbuild_id);
  141. retry_old = false;
  142. goto retry;
  143. }
  144. return NULL;
  145. }
  146. char *build_id_cache__linkname(const char *sbuild_id, char *bf, size_t size)
  147. {
  148. char *tmp = bf;
  149. int ret = asnprintf(&bf, size, "%s/.build-id/%.2s/%s", buildid_dir,
  150. sbuild_id, sbuild_id + 2);
  151. if (ret < 0 || (tmp && size < (unsigned int)ret))
  152. return NULL;
  153. return bf;
  154. }
  155. char *build_id_cache__origname(const char *sbuild_id)
  156. {
  157. char *linkname;
  158. char buf[PATH_MAX];
  159. char *ret = NULL, *p;
  160. size_t offs = 5; /* == strlen("../..") */
  161. ssize_t len;
  162. linkname = build_id_cache__linkname(sbuild_id, NULL, 0);
  163. if (!linkname)
  164. return NULL;
  165. len = readlink(linkname, buf, sizeof(buf) - 1);
  166. if (len <= 0)
  167. goto out;
  168. buf[len] = '\0';
  169. /* The link should be "../..<origpath>/<sbuild_id>" */
  170. p = strrchr(buf, '/'); /* Cut off the "/<sbuild_id>" */
  171. if (p && (p > buf + offs)) {
  172. *p = '\0';
  173. if (buf[offs + 1] == '[')
  174. offs++; /*
  175. * This is a DSO name, like [kernel.kallsyms].
  176. * Skip the first '/', since this is not the
  177. * cache of a regular file.
  178. */
  179. ret = strdup(buf + offs); /* Skip "../..[/]" */
  180. }
  181. out:
  182. free(linkname);
  183. return ret;
  184. }
  185. /* Check if the given build_id cache is valid on current running system */
  186. static bool build_id_cache__valid_id(char *sbuild_id)
  187. {
  188. char real_sbuild_id[SBUILD_ID_SIZE] = "";
  189. char *pathname;
  190. int ret = 0;
  191. bool result = false;
  192. pathname = build_id_cache__origname(sbuild_id);
  193. if (!pathname)
  194. return false;
  195. if (!strcmp(pathname, DSO__NAME_KALLSYMS))
  196. ret = sysfs__sprintf_build_id("/", real_sbuild_id);
  197. else if (pathname[0] == '/')
  198. ret = filename__sprintf_build_id(pathname, real_sbuild_id);
  199. else
  200. ret = -EINVAL; /* Should we support other special DSO cache? */
  201. if (ret >= 0)
  202. result = (strcmp(sbuild_id, real_sbuild_id) == 0);
  203. free(pathname);
  204. return result;
  205. }
  206. static const char *build_id_cache__basename(bool is_kallsyms, bool is_vdso,
  207. bool is_debug)
  208. {
  209. return is_kallsyms ? "kallsyms" : (is_vdso ? "vdso" : (is_debug ?
  210. "debug" : "elf"));
  211. }
  212. char *dso__build_id_filename(const struct dso *dso, char *bf, size_t size,
  213. bool is_debug)
  214. {
  215. bool is_kallsyms = dso__is_kallsyms((struct dso *)dso);
  216. bool is_vdso = dso__is_vdso((struct dso *)dso);
  217. char sbuild_id[SBUILD_ID_SIZE];
  218. char *linkname;
  219. bool alloc = (bf == NULL);
  220. int ret;
  221. if (!dso->has_build_id)
  222. return NULL;
  223. build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
  224. linkname = build_id_cache__linkname(sbuild_id, NULL, 0);
  225. if (!linkname)
  226. return NULL;
  227. /* Check if old style build_id cache */
  228. if (is_regular_file(linkname))
  229. ret = asnprintf(&bf, size, "%s", linkname);
  230. else
  231. ret = asnprintf(&bf, size, "%s/%s", linkname,
  232. build_id_cache__basename(is_kallsyms, is_vdso,
  233. is_debug));
  234. if (ret < 0 || (!alloc && size < (unsigned int)ret))
  235. bf = NULL;
  236. free(linkname);
  237. return bf;
  238. }
  239. #define dsos__for_each_with_build_id(pos, head) \
  240. list_for_each_entry(pos, head, node) \
  241. if (!pos->has_build_id) \
  242. continue; \
  243. else
  244. static int write_buildid(const char *name, size_t name_len, u8 *build_id,
  245. pid_t pid, u16 misc, struct feat_fd *fd)
  246. {
  247. int err;
  248. struct build_id_event b;
  249. size_t len;
  250. len = name_len + 1;
  251. len = PERF_ALIGN(len, NAME_ALIGN);
  252. memset(&b, 0, sizeof(b));
  253. memcpy(&b.build_id, build_id, BUILD_ID_SIZE);
  254. b.pid = pid;
  255. b.header.misc = misc;
  256. b.header.size = sizeof(b) + len;
  257. err = do_write(fd, &b, sizeof(b));
  258. if (err < 0)
  259. return err;
  260. return write_padded(fd, name, name_len + 1, len);
  261. }
  262. static int machine__write_buildid_table(struct machine *machine,
  263. struct feat_fd *fd)
  264. {
  265. int err = 0;
  266. struct dso *pos;
  267. u16 kmisc = PERF_RECORD_MISC_KERNEL,
  268. umisc = PERF_RECORD_MISC_USER;
  269. if (!machine__is_host(machine)) {
  270. kmisc = PERF_RECORD_MISC_GUEST_KERNEL;
  271. umisc = PERF_RECORD_MISC_GUEST_USER;
  272. }
  273. dsos__for_each_with_build_id(pos, &machine->dsos.head) {
  274. const char *name;
  275. size_t name_len;
  276. bool in_kernel = false;
  277. if (!pos->hit && !dso__is_vdso(pos))
  278. continue;
  279. if (dso__is_vdso(pos)) {
  280. name = pos->short_name;
  281. name_len = pos->short_name_len;
  282. } else if (dso__is_kcore(pos)) {
  283. name = machine->mmap_name;
  284. name_len = strlen(name);
  285. } else {
  286. name = pos->long_name;
  287. name_len = pos->long_name_len;
  288. }
  289. in_kernel = pos->kernel ||
  290. is_kernel_module(name,
  291. PERF_RECORD_MISC_CPUMODE_UNKNOWN);
  292. err = write_buildid(name, name_len, pos->build_id, machine->pid,
  293. in_kernel ? kmisc : umisc, fd);
  294. if (err)
  295. break;
  296. }
  297. return err;
  298. }
  299. int perf_session__write_buildid_table(struct perf_session *session,
  300. struct feat_fd *fd)
  301. {
  302. struct rb_node *nd;
  303. int err = machine__write_buildid_table(&session->machines.host, fd);
  304. if (err)
  305. return err;
  306. for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
  307. struct machine *pos = rb_entry(nd, struct machine, rb_node);
  308. err = machine__write_buildid_table(pos, fd);
  309. if (err)
  310. break;
  311. }
  312. return err;
  313. }
  314. static int __dsos__hit_all(struct list_head *head)
  315. {
  316. struct dso *pos;
  317. list_for_each_entry(pos, head, node)
  318. pos->hit = true;
  319. return 0;
  320. }
  321. static int machine__hit_all_dsos(struct machine *machine)
  322. {
  323. return __dsos__hit_all(&machine->dsos.head);
  324. }
  325. int dsos__hit_all(struct perf_session *session)
  326. {
  327. struct rb_node *nd;
  328. int err;
  329. err = machine__hit_all_dsos(&session->machines.host);
  330. if (err)
  331. return err;
  332. for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
  333. struct machine *pos = rb_entry(nd, struct machine, rb_node);
  334. err = machine__hit_all_dsos(pos);
  335. if (err)
  336. return err;
  337. }
  338. return 0;
  339. }
  340. void disable_buildid_cache(void)
  341. {
  342. no_buildid_cache = true;
  343. }
  344. static bool lsdir_bid_head_filter(const char *name __maybe_unused,
  345. struct dirent *d)
  346. {
  347. return (strlen(d->d_name) == 2) &&
  348. isxdigit(d->d_name[0]) && isxdigit(d->d_name[1]);
  349. }
  350. static bool lsdir_bid_tail_filter(const char *name __maybe_unused,
  351. struct dirent *d)
  352. {
  353. int i = 0;
  354. while (isxdigit(d->d_name[i]) && i < SBUILD_ID_SIZE - 3)
  355. i++;
  356. return (i == SBUILD_ID_SIZE - 3) && (d->d_name[i] == '\0');
  357. }
  358. struct strlist *build_id_cache__list_all(bool validonly)
  359. {
  360. struct strlist *toplist, *linklist = NULL, *bidlist;
  361. struct str_node *nd, *nd2;
  362. char *topdir, *linkdir = NULL;
  363. char sbuild_id[SBUILD_ID_SIZE];
  364. /* for filename__ functions */
  365. if (validonly)
  366. symbol__init(NULL);
  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 (validonly && !build_id_cache__valid_id(sbuild_id))
  395. continue;
  396. if (strlist__add(bidlist, sbuild_id) < 0)
  397. goto err_out;
  398. }
  399. strlist__delete(linklist);
  400. zfree(&linkdir);
  401. }
  402. out_free:
  403. strlist__delete(toplist);
  404. out:
  405. free(topdir);
  406. return bidlist;
  407. err_out:
  408. strlist__delete(linklist);
  409. zfree(&linkdir);
  410. strlist__delete(bidlist);
  411. bidlist = NULL;
  412. goto out_free;
  413. }
  414. static bool str_is_build_id(const char *maybe_sbuild_id, size_t len)
  415. {
  416. size_t i;
  417. for (i = 0; i < len; i++) {
  418. if (!isxdigit(maybe_sbuild_id[i]))
  419. return false;
  420. }
  421. return true;
  422. }
  423. /* Return the valid complete build-id */
  424. char *build_id_cache__complement(const char *incomplete_sbuild_id)
  425. {
  426. struct strlist *bidlist;
  427. struct str_node *nd, *cand = NULL;
  428. char *sbuild_id = NULL;
  429. size_t len = strlen(incomplete_sbuild_id);
  430. if (len >= SBUILD_ID_SIZE ||
  431. !str_is_build_id(incomplete_sbuild_id, len))
  432. return NULL;
  433. bidlist = build_id_cache__list_all(true);
  434. if (!bidlist)
  435. return NULL;
  436. strlist__for_each_entry(nd, bidlist) {
  437. if (strncmp(nd->s, incomplete_sbuild_id, len) != 0)
  438. continue;
  439. if (cand) { /* Error: There are more than 2 candidates. */
  440. cand = NULL;
  441. break;
  442. }
  443. cand = nd;
  444. }
  445. if (cand)
  446. sbuild_id = strdup(cand->s);
  447. strlist__delete(bidlist);
  448. return sbuild_id;
  449. }
  450. char *build_id_cache__cachedir(const char *sbuild_id, const char *name,
  451. struct nsinfo *nsi, bool is_kallsyms,
  452. bool is_vdso)
  453. {
  454. char *realname = (char *)name, *filename;
  455. bool slash = is_kallsyms || is_vdso;
  456. if (!slash) {
  457. realname = nsinfo__realpath(name, nsi);
  458. if (!realname)
  459. return NULL;
  460. }
  461. if (asprintf(&filename, "%s%s%s%s%s", buildid_dir, slash ? "/" : "",
  462. is_vdso ? DSO__NAME_VDSO : realname,
  463. sbuild_id ? "/" : "", sbuild_id ?: "") < 0)
  464. filename = NULL;
  465. if (!slash)
  466. free(realname);
  467. return filename;
  468. }
  469. int build_id_cache__list_build_ids(const char *pathname, struct nsinfo *nsi,
  470. struct strlist **result)
  471. {
  472. char *dir_name;
  473. int ret = 0;
  474. dir_name = build_id_cache__cachedir(NULL, pathname, nsi, false, false);
  475. if (!dir_name)
  476. return -ENOMEM;
  477. *result = lsdir(dir_name, lsdir_no_dot_filter);
  478. if (!*result)
  479. ret = -errno;
  480. free(dir_name);
  481. return ret;
  482. }
  483. #if defined(HAVE_LIBELF_SUPPORT) && defined(HAVE_GELF_GETNOTE_SUPPORT)
  484. static int build_id_cache__add_sdt_cache(const char *sbuild_id,
  485. const char *realname,
  486. struct nsinfo *nsi)
  487. {
  488. struct probe_cache *cache;
  489. int ret;
  490. struct nscookie nsc;
  491. cache = probe_cache__new(sbuild_id, nsi);
  492. if (!cache)
  493. return -1;
  494. nsinfo__mountns_enter(nsi, &nsc);
  495. ret = probe_cache__scan_sdt(cache, realname);
  496. nsinfo__mountns_exit(&nsc);
  497. if (ret >= 0) {
  498. pr_debug4("Found %d SDTs in %s\n", ret, realname);
  499. if (probe_cache__commit(cache) < 0)
  500. ret = -1;
  501. }
  502. probe_cache__delete(cache);
  503. return ret;
  504. }
  505. #else
  506. #define build_id_cache__add_sdt_cache(sbuild_id, realname, nsi) (0)
  507. #endif
  508. static char *build_id_cache__find_debug(const char *sbuild_id,
  509. struct nsinfo *nsi)
  510. {
  511. char *realname = NULL;
  512. char *debugfile;
  513. struct nscookie nsc;
  514. size_t len = 0;
  515. debugfile = calloc(1, PATH_MAX);
  516. if (!debugfile)
  517. goto out;
  518. len = __symbol__join_symfs(debugfile, PATH_MAX,
  519. "/usr/lib/debug/.build-id/");
  520. snprintf(debugfile + len, PATH_MAX - len, "%.2s/%s.debug", sbuild_id,
  521. sbuild_id + 2);
  522. nsinfo__mountns_enter(nsi, &nsc);
  523. realname = realpath(debugfile, NULL);
  524. if (realname && access(realname, R_OK))
  525. zfree(&realname);
  526. nsinfo__mountns_exit(&nsc);
  527. out:
  528. free(debugfile);
  529. return realname;
  530. }
  531. int build_id_cache__add_s(const char *sbuild_id, const char *name,
  532. struct nsinfo *nsi, bool is_kallsyms, bool is_vdso)
  533. {
  534. const size_t size = PATH_MAX;
  535. char *realname = NULL, *filename = NULL, *dir_name = NULL,
  536. *linkname = zalloc(size), *tmp;
  537. char *debugfile = NULL;
  538. int err = -1;
  539. if (!is_kallsyms) {
  540. if (!is_vdso)
  541. realname = nsinfo__realpath(name, nsi);
  542. else
  543. realname = realpath(name, NULL);
  544. if (!realname)
  545. goto out_free;
  546. }
  547. dir_name = build_id_cache__cachedir(sbuild_id, name, nsi, is_kallsyms,
  548. is_vdso);
  549. if (!dir_name)
  550. goto out_free;
  551. /* Remove old style build-id cache */
  552. if (is_regular_file(dir_name))
  553. if (unlink(dir_name))
  554. goto out_free;
  555. if (mkdir_p(dir_name, 0755))
  556. goto out_free;
  557. /* Save the allocated buildid dirname */
  558. if (asprintf(&filename, "%s/%s", dir_name,
  559. build_id_cache__basename(is_kallsyms, is_vdso,
  560. false)) < 0) {
  561. filename = NULL;
  562. goto out_free;
  563. }
  564. if (access(filename, F_OK)) {
  565. if (is_kallsyms) {
  566. if (copyfile("/proc/kallsyms", filename))
  567. goto out_free;
  568. } else if (nsi && nsi->need_setns) {
  569. if (copyfile_ns(name, filename, nsi))
  570. goto out_free;
  571. } else if (link(realname, filename) && errno != EEXIST &&
  572. copyfile(name, filename))
  573. goto out_free;
  574. }
  575. /* Some binaries are stripped, but have .debug files with their symbol
  576. * table. Check to see if we can locate one of those, since the elf
  577. * file itself may not be very useful to users of our tools without a
  578. * symtab.
  579. */
  580. if (!is_kallsyms && !is_vdso &&
  581. strncmp(".ko", name + strlen(name) - 3, 3)) {
  582. debugfile = build_id_cache__find_debug(sbuild_id, nsi);
  583. if (debugfile) {
  584. zfree(&filename);
  585. if (asprintf(&filename, "%s/%s", dir_name,
  586. build_id_cache__basename(false, false, true)) < 0) {
  587. filename = NULL;
  588. goto out_free;
  589. }
  590. if (access(filename, F_OK)) {
  591. if (nsi && nsi->need_setns) {
  592. if (copyfile_ns(debugfile, filename,
  593. nsi))
  594. goto out_free;
  595. } else if (link(debugfile, filename) &&
  596. errno != EEXIST &&
  597. copyfile(debugfile, filename))
  598. goto out_free;
  599. }
  600. }
  601. }
  602. if (!build_id_cache__linkname(sbuild_id, linkname, size))
  603. goto out_free;
  604. tmp = strrchr(linkname, '/');
  605. *tmp = '\0';
  606. if (access(linkname, X_OK) && mkdir_p(linkname, 0755))
  607. goto out_free;
  608. *tmp = '/';
  609. tmp = dir_name + strlen(buildid_dir) - 5;
  610. memcpy(tmp, "../..", 5);
  611. if (symlink(tmp, linkname) == 0)
  612. err = 0;
  613. /* Update SDT cache : error is just warned */
  614. if (realname &&
  615. build_id_cache__add_sdt_cache(sbuild_id, realname, nsi) < 0)
  616. pr_debug4("Failed to update/scan SDT cache for %s\n", realname);
  617. out_free:
  618. if (!is_kallsyms)
  619. free(realname);
  620. free(filename);
  621. free(debugfile);
  622. free(dir_name);
  623. free(linkname);
  624. return err;
  625. }
  626. static int build_id_cache__add_b(const u8 *build_id, size_t build_id_size,
  627. const char *name, struct nsinfo *nsi,
  628. bool is_kallsyms, bool is_vdso)
  629. {
  630. char sbuild_id[SBUILD_ID_SIZE];
  631. build_id__sprintf(build_id, build_id_size, sbuild_id);
  632. return build_id_cache__add_s(sbuild_id, name, nsi, is_kallsyms,
  633. is_vdso);
  634. }
  635. bool build_id_cache__cached(const char *sbuild_id)
  636. {
  637. bool ret = false;
  638. char *filename = build_id_cache__linkname(sbuild_id, NULL, 0);
  639. if (filename && !access(filename, F_OK))
  640. ret = true;
  641. free(filename);
  642. return ret;
  643. }
  644. int build_id_cache__remove_s(const char *sbuild_id)
  645. {
  646. const size_t size = PATH_MAX;
  647. char *filename = zalloc(size),
  648. *linkname = zalloc(size), *tmp;
  649. int err = -1;
  650. if (filename == NULL || linkname == NULL)
  651. goto out_free;
  652. if (!build_id_cache__linkname(sbuild_id, linkname, size))
  653. goto out_free;
  654. if (access(linkname, F_OK))
  655. goto out_free;
  656. if (readlink(linkname, filename, size - 1) < 0)
  657. goto out_free;
  658. if (unlink(linkname))
  659. goto out_free;
  660. /*
  661. * Since the link is relative, we must make it absolute:
  662. */
  663. tmp = strrchr(linkname, '/') + 1;
  664. snprintf(tmp, size - (tmp - linkname), "%s", filename);
  665. if (rm_rf(linkname))
  666. goto out_free;
  667. err = 0;
  668. out_free:
  669. free(filename);
  670. free(linkname);
  671. return err;
  672. }
  673. static int dso__cache_build_id(struct dso *dso, struct machine *machine)
  674. {
  675. bool is_kallsyms = dso__is_kallsyms(dso);
  676. bool is_vdso = dso__is_vdso(dso);
  677. const char *name = dso->long_name;
  678. if (dso__is_kcore(dso)) {
  679. is_kallsyms = true;
  680. name = machine->mmap_name;
  681. }
  682. return build_id_cache__add_b(dso->build_id, sizeof(dso->build_id), name,
  683. dso->nsinfo, is_kallsyms, is_vdso);
  684. }
  685. static int __dsos__cache_build_ids(struct list_head *head,
  686. struct machine *machine)
  687. {
  688. struct dso *pos;
  689. int err = 0;
  690. dsos__for_each_with_build_id(pos, head)
  691. if (dso__cache_build_id(pos, machine))
  692. err = -1;
  693. return err;
  694. }
  695. static int machine__cache_build_ids(struct machine *machine)
  696. {
  697. return __dsos__cache_build_ids(&machine->dsos.head, machine);
  698. }
  699. int perf_session__cache_build_ids(struct perf_session *session)
  700. {
  701. struct rb_node *nd;
  702. int ret;
  703. if (no_buildid_cache)
  704. return 0;
  705. if (mkdir(buildid_dir, 0755) != 0 && errno != EEXIST)
  706. return -1;
  707. ret = machine__cache_build_ids(&session->machines.host);
  708. for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
  709. struct machine *pos = rb_entry(nd, struct machine, rb_node);
  710. ret |= machine__cache_build_ids(pos);
  711. }
  712. return ret ? -1 : 0;
  713. }
  714. static bool machine__read_build_ids(struct machine *machine, bool with_hits)
  715. {
  716. return __dsos__read_build_ids(&machine->dsos.head, with_hits);
  717. }
  718. bool perf_session__read_build_ids(struct perf_session *session, bool with_hits)
  719. {
  720. struct rb_node *nd;
  721. bool ret = machine__read_build_ids(&session->machines.host, with_hits);
  722. for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
  723. struct machine *pos = rb_entry(nd, struct machine, rb_node);
  724. ret |= machine__read_build_ids(pos, with_hits);
  725. }
  726. return ret;
  727. }