build-id.c 21 KB

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