build-id.c 20 KB

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