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