Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * build-id.c
0004  *
0005  * build-id support
0006  *
0007  * Copyright (C) 2009, 2010 Red Hat Inc.
0008  * Copyright (C) 2009, 2010 Arnaldo Carvalho de Melo <acme@redhat.com>
0009  */
0010 #include "util.h" // lsdir(), mkdir_p(), rm_rf()
0011 #include <dirent.h>
0012 #include <errno.h>
0013 #include <stdio.h>
0014 #include <sys/stat.h>
0015 #include <sys/types.h>
0016 #include "util/copyfile.h"
0017 #include "dso.h"
0018 #include "build-id.h"
0019 #include "event.h"
0020 #include "namespaces.h"
0021 #include "map.h"
0022 #include "symbol.h"
0023 #include "thread.h"
0024 #include <linux/kernel.h>
0025 #include "debug.h"
0026 #include "session.h"
0027 #include "tool.h"
0028 #include "header.h"
0029 #include "vdso.h"
0030 #include "path.h"
0031 #include "probe-file.h"
0032 #include "strlist.h"
0033 
0034 #ifdef HAVE_DEBUGINFOD_SUPPORT
0035 #include <elfutils/debuginfod.h>
0036 #endif
0037 
0038 #include <linux/ctype.h>
0039 #include <linux/zalloc.h>
0040 #include <linux/string.h>
0041 #include <asm/bug.h>
0042 
0043 static bool no_buildid_cache;
0044 
0045 int build_id__mark_dso_hit(struct perf_tool *tool __maybe_unused,
0046                union perf_event *event,
0047                struct perf_sample *sample,
0048                struct evsel *evsel __maybe_unused,
0049                struct machine *machine)
0050 {
0051     struct addr_location al;
0052     struct thread *thread = machine__findnew_thread(machine, sample->pid,
0053                             sample->tid);
0054 
0055     if (thread == NULL) {
0056         pr_err("problem processing %d event, skipping it.\n",
0057             event->header.type);
0058         return -1;
0059     }
0060 
0061     if (thread__find_map(thread, sample->cpumode, sample->ip, &al))
0062         al.map->dso->hit = 1;
0063 
0064     thread__put(thread);
0065     return 0;
0066 }
0067 
0068 static int perf_event__exit_del_thread(struct perf_tool *tool __maybe_unused,
0069                        union perf_event *event,
0070                        struct perf_sample *sample
0071                        __maybe_unused,
0072                        struct machine *machine)
0073 {
0074     struct thread *thread = machine__findnew_thread(machine,
0075                             event->fork.pid,
0076                             event->fork.tid);
0077 
0078     dump_printf("(%d:%d):(%d:%d)\n", event->fork.pid, event->fork.tid,
0079             event->fork.ppid, event->fork.ptid);
0080 
0081     if (thread) {
0082         machine__remove_thread(machine, thread);
0083         thread__put(thread);
0084     }
0085 
0086     return 0;
0087 }
0088 
0089 struct perf_tool build_id__mark_dso_hit_ops = {
0090     .sample = build_id__mark_dso_hit,
0091     .mmap   = perf_event__process_mmap,
0092     .mmap2  = perf_event__process_mmap2,
0093     .fork   = perf_event__process_fork,
0094     .exit   = perf_event__exit_del_thread,
0095     .attr        = perf_event__process_attr,
0096     .build_id    = perf_event__process_build_id,
0097     .ordered_events  = true,
0098 };
0099 
0100 int build_id__sprintf(const struct build_id *build_id, char *bf)
0101 {
0102     char *bid = bf;
0103     const u8 *raw = build_id->data;
0104     size_t i;
0105 
0106     bf[0] = 0x0;
0107 
0108     for (i = 0; i < build_id->size; ++i) {
0109         sprintf(bid, "%02x", *raw);
0110         ++raw;
0111         bid += 2;
0112     }
0113 
0114     return (bid - bf) + 1;
0115 }
0116 
0117 int sysfs__sprintf_build_id(const char *root_dir, char *sbuild_id)
0118 {
0119     char notes[PATH_MAX];
0120     struct build_id bid;
0121     int ret;
0122 
0123     if (!root_dir)
0124         root_dir = "";
0125 
0126     scnprintf(notes, sizeof(notes), "%s/sys/kernel/notes", root_dir);
0127 
0128     ret = sysfs__read_build_id(notes, &bid);
0129     if (ret < 0)
0130         return ret;
0131 
0132     return build_id__sprintf(&bid, sbuild_id);
0133 }
0134 
0135 int filename__sprintf_build_id(const char *pathname, char *sbuild_id)
0136 {
0137     struct build_id bid;
0138     int ret;
0139 
0140     ret = filename__read_build_id(pathname, &bid);
0141     if (ret < 0)
0142         return ret;
0143 
0144     return build_id__sprintf(&bid, sbuild_id);
0145 }
0146 
0147 /* asnprintf consolidates asprintf and snprintf */
0148 static int asnprintf(char **strp, size_t size, const char *fmt, ...)
0149 {
0150     va_list ap;
0151     int ret;
0152 
0153     if (!strp)
0154         return -EINVAL;
0155 
0156     va_start(ap, fmt);
0157     if (*strp)
0158         ret = vsnprintf(*strp, size, fmt, ap);
0159     else
0160         ret = vasprintf(strp, fmt, ap);
0161     va_end(ap);
0162 
0163     return ret;
0164 }
0165 
0166 char *build_id_cache__kallsyms_path(const char *sbuild_id, char *bf,
0167                     size_t size)
0168 {
0169     bool retry_old = true;
0170 
0171     snprintf(bf, size, "%s/%s/%s/kallsyms",
0172          buildid_dir, DSO__NAME_KALLSYMS, sbuild_id);
0173 retry:
0174     if (!access(bf, F_OK))
0175         return bf;
0176     if (retry_old) {
0177         /* Try old style kallsyms cache */
0178         snprintf(bf, size, "%s/%s/%s",
0179              buildid_dir, DSO__NAME_KALLSYMS, sbuild_id);
0180         retry_old = false;
0181         goto retry;
0182     }
0183 
0184     return NULL;
0185 }
0186 
0187 char *build_id_cache__linkname(const char *sbuild_id, char *bf, size_t size)
0188 {
0189     char *tmp = bf;
0190     int ret = asnprintf(&bf, size, "%s/.build-id/%.2s/%s", buildid_dir,
0191                 sbuild_id, sbuild_id + 2);
0192     if (ret < 0 || (tmp && size < (unsigned int)ret))
0193         return NULL;
0194     return bf;
0195 }
0196 
0197 /* The caller is responsible to free the returned buffer. */
0198 char *build_id_cache__origname(const char *sbuild_id)
0199 {
0200     char *linkname;
0201     char buf[PATH_MAX];
0202     char *ret = NULL, *p;
0203     size_t offs = 5;    /* == strlen("../..") */
0204     ssize_t len;
0205 
0206     linkname = build_id_cache__linkname(sbuild_id, NULL, 0);
0207     if (!linkname)
0208         return NULL;
0209 
0210     len = readlink(linkname, buf, sizeof(buf) - 1);
0211     if (len <= 0)
0212         goto out;
0213     buf[len] = '\0';
0214 
0215     /* The link should be "../..<origpath>/<sbuild_id>" */
0216     p = strrchr(buf, '/');  /* Cut off the "/<sbuild_id>" */
0217     if (p && (p > buf + offs)) {
0218         *p = '\0';
0219         if (buf[offs + 1] == '[')
0220             offs++; /*
0221                  * This is a DSO name, like [kernel.kallsyms].
0222                  * Skip the first '/', since this is not the
0223                  * cache of a regular file.
0224                  */
0225         ret = strdup(buf + offs);   /* Skip "../..[/]" */
0226     }
0227 out:
0228     free(linkname);
0229     return ret;
0230 }
0231 
0232 /* Check if the given build_id cache is valid on current running system */
0233 static bool build_id_cache__valid_id(char *sbuild_id)
0234 {
0235     char real_sbuild_id[SBUILD_ID_SIZE] = "";
0236     char *pathname;
0237     int ret = 0;
0238     bool result = false;
0239 
0240     pathname = build_id_cache__origname(sbuild_id);
0241     if (!pathname)
0242         return false;
0243 
0244     if (!strcmp(pathname, DSO__NAME_KALLSYMS))
0245         ret = sysfs__sprintf_build_id("/", real_sbuild_id);
0246     else if (pathname[0] == '/')
0247         ret = filename__sprintf_build_id(pathname, real_sbuild_id);
0248     else
0249         ret = -EINVAL;  /* Should we support other special DSO cache? */
0250     if (ret >= 0)
0251         result = (strcmp(sbuild_id, real_sbuild_id) == 0);
0252     free(pathname);
0253 
0254     return result;
0255 }
0256 
0257 static const char *build_id_cache__basename(bool is_kallsyms, bool is_vdso,
0258                         bool is_debug)
0259 {
0260     return is_kallsyms ? "kallsyms" : (is_vdso ? "vdso" : (is_debug ?
0261         "debug" : "elf"));
0262 }
0263 
0264 char *__dso__build_id_filename(const struct dso *dso, char *bf, size_t size,
0265                    bool is_debug, bool is_kallsyms)
0266 {
0267     bool is_vdso = dso__is_vdso((struct dso *)dso);
0268     char sbuild_id[SBUILD_ID_SIZE];
0269     char *linkname;
0270     bool alloc = (bf == NULL);
0271     int ret;
0272 
0273     if (!dso->has_build_id)
0274         return NULL;
0275 
0276     build_id__sprintf(&dso->bid, sbuild_id);
0277     linkname = build_id_cache__linkname(sbuild_id, NULL, 0);
0278     if (!linkname)
0279         return NULL;
0280 
0281     /* Check if old style build_id cache */
0282     if (is_regular_file(linkname))
0283         ret = asnprintf(&bf, size, "%s", linkname);
0284     else
0285         ret = asnprintf(&bf, size, "%s/%s", linkname,
0286              build_id_cache__basename(is_kallsyms, is_vdso,
0287                           is_debug));
0288     if (ret < 0 || (!alloc && size < (unsigned int)ret))
0289         bf = NULL;
0290     free(linkname);
0291 
0292     return bf;
0293 }
0294 
0295 char *dso__build_id_filename(const struct dso *dso, char *bf, size_t size,
0296                  bool is_debug)
0297 {
0298     bool is_kallsyms = dso__is_kallsyms((struct dso *)dso);
0299 
0300     return __dso__build_id_filename(dso, bf, size, is_debug, is_kallsyms);
0301 }
0302 
0303 static int write_buildid(const char *name, size_t name_len, struct build_id *bid,
0304              pid_t pid, u16 misc, struct feat_fd *fd)
0305 {
0306     int err;
0307     struct perf_record_header_build_id b;
0308     size_t len;
0309 
0310     len = name_len + 1;
0311     len = PERF_ALIGN(len, NAME_ALIGN);
0312 
0313     memset(&b, 0, sizeof(b));
0314     memcpy(&b.data, bid->data, bid->size);
0315     b.size = (u8) bid->size;
0316     misc |= PERF_RECORD_MISC_BUILD_ID_SIZE;
0317     b.pid = pid;
0318     b.header.misc = misc;
0319     b.header.size = sizeof(b) + len;
0320 
0321     err = do_write(fd, &b, sizeof(b));
0322     if (err < 0)
0323         return err;
0324 
0325     return write_padded(fd, name, name_len + 1, len);
0326 }
0327 
0328 static int machine__write_buildid_table(struct machine *machine,
0329                     struct feat_fd *fd)
0330 {
0331     int err = 0;
0332     struct dso *pos;
0333     u16 kmisc = PERF_RECORD_MISC_KERNEL,
0334         umisc = PERF_RECORD_MISC_USER;
0335 
0336     if (!machine__is_host(machine)) {
0337         kmisc = PERF_RECORD_MISC_GUEST_KERNEL;
0338         umisc = PERF_RECORD_MISC_GUEST_USER;
0339     }
0340 
0341     dsos__for_each_with_build_id(pos, &machine->dsos.head) {
0342         const char *name;
0343         size_t name_len;
0344         bool in_kernel = false;
0345 
0346         if (!pos->hit && !dso__is_vdso(pos))
0347             continue;
0348 
0349         if (dso__is_vdso(pos)) {
0350             name = pos->short_name;
0351             name_len = pos->short_name_len;
0352         } else if (dso__is_kcore(pos)) {
0353             name = machine->mmap_name;
0354             name_len = strlen(name);
0355         } else {
0356             name = pos->long_name;
0357             name_len = pos->long_name_len;
0358         }
0359 
0360         in_kernel = pos->kernel ||
0361                 is_kernel_module(name,
0362                     PERF_RECORD_MISC_CPUMODE_UNKNOWN);
0363         err = write_buildid(name, name_len, &pos->bid, machine->pid,
0364                     in_kernel ? kmisc : umisc, fd);
0365         if (err)
0366             break;
0367     }
0368 
0369     return err;
0370 }
0371 
0372 int perf_session__write_buildid_table(struct perf_session *session,
0373                       struct feat_fd *fd)
0374 {
0375     struct rb_node *nd;
0376     int err = machine__write_buildid_table(&session->machines.host, fd);
0377 
0378     if (err)
0379         return err;
0380 
0381     for (nd = rb_first_cached(&session->machines.guests); nd;
0382          nd = rb_next(nd)) {
0383         struct machine *pos = rb_entry(nd, struct machine, rb_node);
0384         err = machine__write_buildid_table(pos, fd);
0385         if (err)
0386             break;
0387     }
0388     return err;
0389 }
0390 
0391 static int __dsos__hit_all(struct list_head *head)
0392 {
0393     struct dso *pos;
0394 
0395     list_for_each_entry(pos, head, node)
0396         pos->hit = true;
0397 
0398     return 0;
0399 }
0400 
0401 static int machine__hit_all_dsos(struct machine *machine)
0402 {
0403     return __dsos__hit_all(&machine->dsos.head);
0404 }
0405 
0406 int dsos__hit_all(struct perf_session *session)
0407 {
0408     struct rb_node *nd;
0409     int err;
0410 
0411     err = machine__hit_all_dsos(&session->machines.host);
0412     if (err)
0413         return err;
0414 
0415     for (nd = rb_first_cached(&session->machines.guests); nd;
0416          nd = rb_next(nd)) {
0417         struct machine *pos = rb_entry(nd, struct machine, rb_node);
0418 
0419         err = machine__hit_all_dsos(pos);
0420         if (err)
0421             return err;
0422     }
0423 
0424     return 0;
0425 }
0426 
0427 void disable_buildid_cache(void)
0428 {
0429     no_buildid_cache = true;
0430 }
0431 
0432 static bool lsdir_bid_head_filter(const char *name __maybe_unused,
0433                   struct dirent *d)
0434 {
0435     return (strlen(d->d_name) == 2) &&
0436         isxdigit(d->d_name[0]) && isxdigit(d->d_name[1]);
0437 }
0438 
0439 static bool lsdir_bid_tail_filter(const char *name __maybe_unused,
0440                   struct dirent *d)
0441 {
0442     int i = 0;
0443     while (isxdigit(d->d_name[i]) && i < SBUILD_ID_SIZE - 3)
0444         i++;
0445     return (i >= SBUILD_ID_MIN_SIZE - 3) && (i <= SBUILD_ID_SIZE - 3) &&
0446         (d->d_name[i] == '\0');
0447 }
0448 
0449 struct strlist *build_id_cache__list_all(bool validonly)
0450 {
0451     struct strlist *toplist, *linklist = NULL, *bidlist;
0452     struct str_node *nd, *nd2;
0453     char *topdir, *linkdir = NULL;
0454     char sbuild_id[SBUILD_ID_SIZE];
0455 
0456     /* for filename__ functions */
0457     if (validonly)
0458         symbol__init(NULL);
0459 
0460     /* Open the top-level directory */
0461     if (asprintf(&topdir, "%s/.build-id/", buildid_dir) < 0)
0462         return NULL;
0463 
0464     bidlist = strlist__new(NULL, NULL);
0465     if (!bidlist)
0466         goto out;
0467 
0468     toplist = lsdir(topdir, lsdir_bid_head_filter);
0469     if (!toplist) {
0470         pr_debug("Error in lsdir(%s): %d\n", topdir, errno);
0471         /* If there is no buildid cache, return an empty list */
0472         if (errno == ENOENT)
0473             goto out;
0474         goto err_out;
0475     }
0476 
0477     strlist__for_each_entry(nd, toplist) {
0478         if (asprintf(&linkdir, "%s/%s", topdir, nd->s) < 0)
0479             goto err_out;
0480         /* Open the lower-level directory */
0481         linklist = lsdir(linkdir, lsdir_bid_tail_filter);
0482         if (!linklist) {
0483             pr_debug("Error in lsdir(%s): %d\n", linkdir, errno);
0484             goto err_out;
0485         }
0486         strlist__for_each_entry(nd2, linklist) {
0487             if (snprintf(sbuild_id, SBUILD_ID_SIZE, "%s%s",
0488                      nd->s, nd2->s) > SBUILD_ID_SIZE - 1)
0489                 goto err_out;
0490             if (validonly && !build_id_cache__valid_id(sbuild_id))
0491                 continue;
0492             if (strlist__add(bidlist, sbuild_id) < 0)
0493                 goto err_out;
0494         }
0495         strlist__delete(linklist);
0496         zfree(&linkdir);
0497     }
0498 
0499 out_free:
0500     strlist__delete(toplist);
0501 out:
0502     free(topdir);
0503 
0504     return bidlist;
0505 
0506 err_out:
0507     strlist__delete(linklist);
0508     zfree(&linkdir);
0509     strlist__delete(bidlist);
0510     bidlist = NULL;
0511     goto out_free;
0512 }
0513 
0514 static bool str_is_build_id(const char *maybe_sbuild_id, size_t len)
0515 {
0516     size_t i;
0517 
0518     for (i = 0; i < len; i++) {
0519         if (!isxdigit(maybe_sbuild_id[i]))
0520             return false;
0521     }
0522     return true;
0523 }
0524 
0525 /* Return the valid complete build-id */
0526 char *build_id_cache__complement(const char *incomplete_sbuild_id)
0527 {
0528     struct strlist *bidlist;
0529     struct str_node *nd, *cand = NULL;
0530     char *sbuild_id = NULL;
0531     size_t len = strlen(incomplete_sbuild_id);
0532 
0533     if (len >= SBUILD_ID_SIZE ||
0534         !str_is_build_id(incomplete_sbuild_id, len))
0535         return NULL;
0536 
0537     bidlist = build_id_cache__list_all(true);
0538     if (!bidlist)
0539         return NULL;
0540 
0541     strlist__for_each_entry(nd, bidlist) {
0542         if (strncmp(nd->s, incomplete_sbuild_id, len) != 0)
0543             continue;
0544         if (cand) { /* Error: There are more than 2 candidates. */
0545             cand = NULL;
0546             break;
0547         }
0548         cand = nd;
0549     }
0550     if (cand)
0551         sbuild_id = strdup(cand->s);
0552     strlist__delete(bidlist);
0553 
0554     return sbuild_id;
0555 }
0556 
0557 char *build_id_cache__cachedir(const char *sbuild_id, const char *name,
0558                    struct nsinfo *nsi, bool is_kallsyms,
0559                    bool is_vdso)
0560 {
0561     char *realname = (char *)name, *filename;
0562     bool slash = is_kallsyms || is_vdso;
0563 
0564     if (!slash)
0565         realname = nsinfo__realpath(name, nsi);
0566 
0567     if (asprintf(&filename, "%s%s%s%s%s", buildid_dir, slash ? "/" : "",
0568              is_vdso ? DSO__NAME_VDSO : (realname ? realname : name),
0569              sbuild_id ? "/" : "", sbuild_id ?: "") < 0)
0570         filename = NULL;
0571 
0572     if (!slash)
0573         free(realname);
0574 
0575     return filename;
0576 }
0577 
0578 int build_id_cache__list_build_ids(const char *pathname, struct nsinfo *nsi,
0579                    struct strlist **result)
0580 {
0581     char *dir_name;
0582     int ret = 0;
0583 
0584     dir_name = build_id_cache__cachedir(NULL, pathname, nsi, false, false);
0585     if (!dir_name)
0586         return -ENOMEM;
0587 
0588     *result = lsdir(dir_name, lsdir_no_dot_filter);
0589     if (!*result)
0590         ret = -errno;
0591     free(dir_name);
0592 
0593     return ret;
0594 }
0595 
0596 #if defined(HAVE_LIBELF_SUPPORT) && defined(HAVE_GELF_GETNOTE_SUPPORT)
0597 static int build_id_cache__add_sdt_cache(const char *sbuild_id,
0598                       const char *realname,
0599                       struct nsinfo *nsi)
0600 {
0601     struct probe_cache *cache;
0602     int ret;
0603     struct nscookie nsc;
0604 
0605     cache = probe_cache__new(sbuild_id, nsi);
0606     if (!cache)
0607         return -1;
0608 
0609     nsinfo__mountns_enter(nsi, &nsc);
0610     ret = probe_cache__scan_sdt(cache, realname);
0611     nsinfo__mountns_exit(&nsc);
0612     if (ret >= 0) {
0613         pr_debug4("Found %d SDTs in %s\n", ret, realname);
0614         if (probe_cache__commit(cache) < 0)
0615             ret = -1;
0616     }
0617     probe_cache__delete(cache);
0618     return ret;
0619 }
0620 #else
0621 #define build_id_cache__add_sdt_cache(sbuild_id, realname, nsi) (0)
0622 #endif
0623 
0624 static char *build_id_cache__find_debug(const char *sbuild_id,
0625                     struct nsinfo *nsi,
0626                     const char *root_dir)
0627 {
0628     const char *dirname = "/usr/lib/debug/.build-id/";
0629     char *realname = NULL;
0630     char dirbuf[PATH_MAX];
0631     char *debugfile;
0632     struct nscookie nsc;
0633     size_t len = 0;
0634 
0635     debugfile = calloc(1, PATH_MAX);
0636     if (!debugfile)
0637         goto out;
0638 
0639     if (root_dir) {
0640         path__join(dirbuf, PATH_MAX, root_dir, dirname);
0641         dirname = dirbuf;
0642     }
0643 
0644     len = __symbol__join_symfs(debugfile, PATH_MAX, dirname);
0645     snprintf(debugfile + len, PATH_MAX - len, "%.2s/%s.debug", sbuild_id,
0646          sbuild_id + 2);
0647 
0648     nsinfo__mountns_enter(nsi, &nsc);
0649     realname = realpath(debugfile, NULL);
0650     if (realname && access(realname, R_OK))
0651         zfree(&realname);
0652     nsinfo__mountns_exit(&nsc);
0653 
0654 #ifdef HAVE_DEBUGINFOD_SUPPORT
0655     if (realname == NULL) {
0656         debuginfod_client* c;
0657 
0658         pr_debug("Downloading debug info with build id %s\n", sbuild_id);
0659 
0660         c = debuginfod_begin();
0661         if (c != NULL) {
0662             int fd = debuginfod_find_debuginfo(c,
0663                     (const unsigned char*)sbuild_id, 0,
0664                     &realname);
0665             if (fd >= 0)
0666                 close(fd); /* retaining reference by realname */
0667             debuginfod_end(c);
0668         }
0669     }
0670 #endif
0671 
0672 out:
0673     free(debugfile);
0674     return realname;
0675 }
0676 
0677 int
0678 build_id_cache__add(const char *sbuild_id, const char *name, const char *realname,
0679             struct nsinfo *nsi, bool is_kallsyms, bool is_vdso,
0680             const char *proper_name, const char *root_dir)
0681 {
0682     const size_t size = PATH_MAX;
0683     char *filename = NULL, *dir_name = NULL, *linkname = zalloc(size), *tmp;
0684     char *debugfile = NULL;
0685     int err = -1;
0686 
0687     if (!proper_name)
0688         proper_name = name;
0689 
0690     dir_name = build_id_cache__cachedir(sbuild_id, proper_name, nsi, is_kallsyms,
0691                         is_vdso);
0692     if (!dir_name)
0693         goto out_free;
0694 
0695     /* Remove old style build-id cache */
0696     if (is_regular_file(dir_name))
0697         if (unlink(dir_name))
0698             goto out_free;
0699 
0700     if (mkdir_p(dir_name, 0755))
0701         goto out_free;
0702 
0703     /* Save the allocated buildid dirname */
0704     if (asprintf(&filename, "%s/%s", dir_name,
0705              build_id_cache__basename(is_kallsyms, is_vdso,
0706              false)) < 0) {
0707         filename = NULL;
0708         goto out_free;
0709     }
0710 
0711     if (access(filename, F_OK)) {
0712         if (is_kallsyms) {
0713             if (copyfile("/proc/kallsyms", filename))
0714                 goto out_free;
0715         } else if (nsi && nsinfo__need_setns(nsi)) {
0716             if (copyfile_ns(name, filename, nsi))
0717                 goto out_free;
0718         } else if (link(realname, filename) && errno != EEXIST &&
0719                 copyfile(name, filename))
0720             goto out_free;
0721     }
0722 
0723     /* Some binaries are stripped, but have .debug files with their symbol
0724      * table.  Check to see if we can locate one of those, since the elf
0725      * file itself may not be very useful to users of our tools without a
0726      * symtab.
0727      */
0728     if (!is_kallsyms && !is_vdso &&
0729         strncmp(".ko", name + strlen(name) - 3, 3)) {
0730         debugfile = build_id_cache__find_debug(sbuild_id, nsi, root_dir);
0731         if (debugfile) {
0732             zfree(&filename);
0733             if (asprintf(&filename, "%s/%s", dir_name,
0734                 build_id_cache__basename(false, false, true)) < 0) {
0735                 filename = NULL;
0736                 goto out_free;
0737             }
0738             if (access(filename, F_OK)) {
0739                 if (nsi && nsinfo__need_setns(nsi)) {
0740                     if (copyfile_ns(debugfile, filename,
0741                             nsi))
0742                         goto out_free;
0743                 } else if (link(debugfile, filename) &&
0744                         errno != EEXIST &&
0745                         copyfile(debugfile, filename))
0746                     goto out_free;
0747             }
0748         }
0749     }
0750 
0751     if (!build_id_cache__linkname(sbuild_id, linkname, size))
0752         goto out_free;
0753     tmp = strrchr(linkname, '/');
0754     *tmp = '\0';
0755 
0756     if (access(linkname, X_OK) && mkdir_p(linkname, 0755))
0757         goto out_free;
0758 
0759     *tmp = '/';
0760     tmp = dir_name + strlen(buildid_dir) - 5;
0761     memcpy(tmp, "../..", 5);
0762 
0763     if (symlink(tmp, linkname) == 0) {
0764         err = 0;
0765     } else if (errno == EEXIST) {
0766         char path[PATH_MAX];
0767         ssize_t len;
0768 
0769         len = readlink(linkname, path, sizeof(path) - 1);
0770         if (len <= 0) {
0771             pr_err("Can't read link: %s\n", linkname);
0772             goto out_free;
0773         }
0774         path[len] = '\0';
0775 
0776         if (strcmp(tmp, path)) {
0777             pr_debug("build <%s> already linked to %s\n",
0778                  sbuild_id, linkname);
0779         }
0780         err = 0;
0781     }
0782 
0783     /* Update SDT cache : error is just warned */
0784     if (realname &&
0785         build_id_cache__add_sdt_cache(sbuild_id, realname, nsi) < 0)
0786         pr_debug4("Failed to update/scan SDT cache for %s\n", realname);
0787 
0788 out_free:
0789     free(filename);
0790     free(debugfile);
0791     free(dir_name);
0792     free(linkname);
0793     return err;
0794 }
0795 
0796 int __build_id_cache__add_s(const char *sbuild_id, const char *name,
0797                 struct nsinfo *nsi, bool is_kallsyms, bool is_vdso,
0798                 const char *proper_name, const char *root_dir)
0799 {
0800     char *realname = NULL;
0801     int err = -1;
0802 
0803     if (!is_kallsyms) {
0804         if (!is_vdso)
0805             realname = nsinfo__realpath(name, nsi);
0806         else
0807             realname = realpath(name, NULL);
0808         if (!realname)
0809             goto out_free;
0810     }
0811 
0812     err = build_id_cache__add(sbuild_id, name, realname, nsi,
0813                   is_kallsyms, is_vdso, proper_name, root_dir);
0814 out_free:
0815     if (!is_kallsyms)
0816         free(realname);
0817     return err;
0818 }
0819 
0820 static int build_id_cache__add_b(const struct build_id *bid,
0821                  const char *name, struct nsinfo *nsi,
0822                  bool is_kallsyms, bool is_vdso,
0823                  const char *proper_name,
0824                  const char *root_dir)
0825 {
0826     char sbuild_id[SBUILD_ID_SIZE];
0827 
0828     build_id__sprintf(bid, sbuild_id);
0829 
0830     return __build_id_cache__add_s(sbuild_id, name, nsi, is_kallsyms,
0831                        is_vdso, proper_name, root_dir);
0832 }
0833 
0834 bool build_id_cache__cached(const char *sbuild_id)
0835 {
0836     bool ret = false;
0837     char *filename = build_id_cache__linkname(sbuild_id, NULL, 0);
0838 
0839     if (filename && !access(filename, F_OK))
0840         ret = true;
0841     free(filename);
0842 
0843     return ret;
0844 }
0845 
0846 int build_id_cache__remove_s(const char *sbuild_id)
0847 {
0848     const size_t size = PATH_MAX;
0849     char *filename = zalloc(size),
0850          *linkname = zalloc(size), *tmp;
0851     int err = -1;
0852 
0853     if (filename == NULL || linkname == NULL)
0854         goto out_free;
0855 
0856     if (!build_id_cache__linkname(sbuild_id, linkname, size))
0857         goto out_free;
0858 
0859     if (access(linkname, F_OK))
0860         goto out_free;
0861 
0862     if (readlink(linkname, filename, size - 1) < 0)
0863         goto out_free;
0864 
0865     if (unlink(linkname))
0866         goto out_free;
0867 
0868     /*
0869      * Since the link is relative, we must make it absolute:
0870      */
0871     tmp = strrchr(linkname, '/') + 1;
0872     snprintf(tmp, size - (tmp - linkname), "%s", filename);
0873 
0874     if (rm_rf(linkname))
0875         goto out_free;
0876 
0877     err = 0;
0878 out_free:
0879     free(filename);
0880     free(linkname);
0881     return err;
0882 }
0883 
0884 static int filename__read_build_id_ns(const char *filename,
0885                       struct build_id *bid,
0886                       struct nsinfo *nsi)
0887 {
0888     struct nscookie nsc;
0889     int ret;
0890 
0891     nsinfo__mountns_enter(nsi, &nsc);
0892     ret = filename__read_build_id(filename, bid);
0893     nsinfo__mountns_exit(&nsc);
0894 
0895     return ret;
0896 }
0897 
0898 static bool dso__build_id_mismatch(struct dso *dso, const char *name)
0899 {
0900     struct build_id bid;
0901 
0902     if (filename__read_build_id_ns(name, &bid, dso->nsinfo) < 0)
0903         return false;
0904 
0905     return !dso__build_id_equal(dso, &bid);
0906 }
0907 
0908 static int dso__cache_build_id(struct dso *dso, struct machine *machine,
0909                    void *priv __maybe_unused)
0910 {
0911     bool is_kallsyms = dso__is_kallsyms(dso);
0912     bool is_vdso = dso__is_vdso(dso);
0913     const char *name = dso->long_name;
0914     const char *proper_name = NULL;
0915     const char *root_dir = NULL;
0916     char *allocated_name = NULL;
0917     int ret = 0;
0918 
0919     if (!dso->has_build_id)
0920         return 0;
0921 
0922     if (dso__is_kcore(dso)) {
0923         is_kallsyms = true;
0924         name = machine->mmap_name;
0925     }
0926 
0927     if (!machine__is_host(machine)) {
0928         if (*machine->root_dir) {
0929             root_dir = machine->root_dir;
0930             ret = asprintf(&allocated_name, "%s/%s", root_dir, name);
0931             if (ret < 0)
0932                 return ret;
0933             proper_name = name;
0934             name = allocated_name;
0935         } else if (is_kallsyms) {
0936             /* Cannot get guest kallsyms */
0937             return 0;
0938         }
0939     }
0940 
0941     if (!is_kallsyms && dso__build_id_mismatch(dso, name))
0942         goto out_free;
0943 
0944     ret = build_id_cache__add_b(&dso->bid, name, dso->nsinfo,
0945                     is_kallsyms, is_vdso, proper_name, root_dir);
0946 out_free:
0947     free(allocated_name);
0948     return ret;
0949 }
0950 
0951 static int
0952 machines__for_each_dso(struct machines *machines, machine__dso_t fn, void *priv)
0953 {
0954     int ret = machine__for_each_dso(&machines->host, fn, priv);
0955     struct rb_node *nd;
0956 
0957     for (nd = rb_first_cached(&machines->guests); nd;
0958          nd = rb_next(nd)) {
0959         struct machine *pos = rb_entry(nd, struct machine, rb_node);
0960 
0961         ret |= machine__for_each_dso(pos, fn, priv);
0962     }
0963     return ret ? -1 : 0;
0964 }
0965 
0966 int __perf_session__cache_build_ids(struct perf_session *session,
0967                     machine__dso_t fn, void *priv)
0968 {
0969     if (no_buildid_cache)
0970         return 0;
0971 
0972     if (mkdir(buildid_dir, 0755) != 0 && errno != EEXIST)
0973         return -1;
0974 
0975     return machines__for_each_dso(&session->machines, fn, priv) ?  -1 : 0;
0976 }
0977 
0978 int perf_session__cache_build_ids(struct perf_session *session)
0979 {
0980     return __perf_session__cache_build_ids(session, dso__cache_build_id, NULL);
0981 }
0982 
0983 static bool machine__read_build_ids(struct machine *machine, bool with_hits)
0984 {
0985     return __dsos__read_build_ids(&machine->dsos.head, with_hits);
0986 }
0987 
0988 bool perf_session__read_build_ids(struct perf_session *session, bool with_hits)
0989 {
0990     struct rb_node *nd;
0991     bool ret = machine__read_build_ids(&session->machines.host, with_hits);
0992 
0993     for (nd = rb_first_cached(&session->machines.guests); nd;
0994          nd = rb_next(nd)) {
0995         struct machine *pos = rb_entry(nd, struct machine, rb_node);
0996         ret |= machine__read_build_ids(pos, with_hits);
0997     }
0998 
0999     return ret;
1000 }
1001 
1002 void build_id__init(struct build_id *bid, const u8 *data, size_t size)
1003 {
1004     WARN_ON(size > BUILD_ID_SIZE);
1005     memcpy(bid->data, data, size);
1006     bid->size = size;
1007 }
1008 
1009 bool build_id__is_defined(const struct build_id *bid)
1010 {
1011     return bid && bid->size ? !!memchr_inv(bid->data, 0, bid->size) : false;
1012 }