18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * build-id.c
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * build-id support
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Copyright (C) 2009, 2010 Red Hat Inc.
88c2ecf20Sopenharmony_ci * Copyright (C) 2009, 2010 Arnaldo Carvalho de Melo <acme@redhat.com>
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci#include "util.h" // lsdir(), mkdir_p(), rm_rf()
118c2ecf20Sopenharmony_ci#include <dirent.h>
128c2ecf20Sopenharmony_ci#include <errno.h>
138c2ecf20Sopenharmony_ci#include <stdio.h>
148c2ecf20Sopenharmony_ci#include <sys/stat.h>
158c2ecf20Sopenharmony_ci#include <sys/types.h>
168c2ecf20Sopenharmony_ci#include "util/copyfile.h"
178c2ecf20Sopenharmony_ci#include "dso.h"
188c2ecf20Sopenharmony_ci#include "build-id.h"
198c2ecf20Sopenharmony_ci#include "event.h"
208c2ecf20Sopenharmony_ci#include "namespaces.h"
218c2ecf20Sopenharmony_ci#include "map.h"
228c2ecf20Sopenharmony_ci#include "symbol.h"
238c2ecf20Sopenharmony_ci#include "thread.h"
248c2ecf20Sopenharmony_ci#include <linux/kernel.h>
258c2ecf20Sopenharmony_ci#include "debug.h"
268c2ecf20Sopenharmony_ci#include "session.h"
278c2ecf20Sopenharmony_ci#include "tool.h"
288c2ecf20Sopenharmony_ci#include "header.h"
298c2ecf20Sopenharmony_ci#include "vdso.h"
308c2ecf20Sopenharmony_ci#include "path.h"
318c2ecf20Sopenharmony_ci#include "probe-file.h"
328c2ecf20Sopenharmony_ci#include "strlist.h"
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci#ifdef HAVE_DEBUGINFOD_SUPPORT
358c2ecf20Sopenharmony_ci#include <elfutils/debuginfod.h>
368c2ecf20Sopenharmony_ci#endif
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci#include <linux/ctype.h>
398c2ecf20Sopenharmony_ci#include <linux/zalloc.h>
408c2ecf20Sopenharmony_ci#include <asm/bug.h>
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_cistatic bool no_buildid_cache;
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ciint build_id__mark_dso_hit(struct perf_tool *tool __maybe_unused,
458c2ecf20Sopenharmony_ci			   union perf_event *event,
468c2ecf20Sopenharmony_ci			   struct perf_sample *sample,
478c2ecf20Sopenharmony_ci			   struct evsel *evsel __maybe_unused,
488c2ecf20Sopenharmony_ci			   struct machine *machine)
498c2ecf20Sopenharmony_ci{
508c2ecf20Sopenharmony_ci	struct addr_location al;
518c2ecf20Sopenharmony_ci	struct thread *thread = machine__findnew_thread(machine, sample->pid,
528c2ecf20Sopenharmony_ci							sample->tid);
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	if (thread == NULL) {
558c2ecf20Sopenharmony_ci		pr_err("problem processing %d event, skipping it.\n",
568c2ecf20Sopenharmony_ci			event->header.type);
578c2ecf20Sopenharmony_ci		return -1;
588c2ecf20Sopenharmony_ci	}
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	if (thread__find_map(thread, sample->cpumode, sample->ip, &al))
618c2ecf20Sopenharmony_ci		al.map->dso->hit = 1;
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	thread__put(thread);
648c2ecf20Sopenharmony_ci	return 0;
658c2ecf20Sopenharmony_ci}
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_cistatic int perf_event__exit_del_thread(struct perf_tool *tool __maybe_unused,
688c2ecf20Sopenharmony_ci				       union perf_event *event,
698c2ecf20Sopenharmony_ci				       struct perf_sample *sample
708c2ecf20Sopenharmony_ci				       __maybe_unused,
718c2ecf20Sopenharmony_ci				       struct machine *machine)
728c2ecf20Sopenharmony_ci{
738c2ecf20Sopenharmony_ci	struct thread *thread = machine__findnew_thread(machine,
748c2ecf20Sopenharmony_ci							event->fork.pid,
758c2ecf20Sopenharmony_ci							event->fork.tid);
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	dump_printf("(%d:%d):(%d:%d)\n", event->fork.pid, event->fork.tid,
788c2ecf20Sopenharmony_ci		    event->fork.ppid, event->fork.ptid);
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	if (thread) {
818c2ecf20Sopenharmony_ci		machine__remove_thread(machine, thread);
828c2ecf20Sopenharmony_ci		thread__put(thread);
838c2ecf20Sopenharmony_ci	}
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	return 0;
868c2ecf20Sopenharmony_ci}
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_cistruct perf_tool build_id__mark_dso_hit_ops = {
898c2ecf20Sopenharmony_ci	.sample	= build_id__mark_dso_hit,
908c2ecf20Sopenharmony_ci	.mmap	= perf_event__process_mmap,
918c2ecf20Sopenharmony_ci	.mmap2	= perf_event__process_mmap2,
928c2ecf20Sopenharmony_ci	.fork	= perf_event__process_fork,
938c2ecf20Sopenharmony_ci	.exit	= perf_event__exit_del_thread,
948c2ecf20Sopenharmony_ci	.attr		 = perf_event__process_attr,
958c2ecf20Sopenharmony_ci	.build_id	 = perf_event__process_build_id,
968c2ecf20Sopenharmony_ci	.ordered_events	 = true,
978c2ecf20Sopenharmony_ci};
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ciint build_id__sprintf(const struct build_id *build_id, char *bf)
1008c2ecf20Sopenharmony_ci{
1018c2ecf20Sopenharmony_ci	char *bid = bf;
1028c2ecf20Sopenharmony_ci	const u8 *raw = build_id->data;
1038c2ecf20Sopenharmony_ci	size_t i;
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	bf[0] = 0x0;
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	for (i = 0; i < build_id->size; ++i) {
1088c2ecf20Sopenharmony_ci		sprintf(bid, "%02x", *raw);
1098c2ecf20Sopenharmony_ci		++raw;
1108c2ecf20Sopenharmony_ci		bid += 2;
1118c2ecf20Sopenharmony_ci	}
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	return (bid - bf) + 1;
1148c2ecf20Sopenharmony_ci}
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ciint sysfs__sprintf_build_id(const char *root_dir, char *sbuild_id)
1178c2ecf20Sopenharmony_ci{
1188c2ecf20Sopenharmony_ci	char notes[PATH_MAX];
1198c2ecf20Sopenharmony_ci	struct build_id bid;
1208c2ecf20Sopenharmony_ci	int ret;
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	if (!root_dir)
1238c2ecf20Sopenharmony_ci		root_dir = "";
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	scnprintf(notes, sizeof(notes), "%s/sys/kernel/notes", root_dir);
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	ret = sysfs__read_build_id(notes, &bid);
1288c2ecf20Sopenharmony_ci	if (ret < 0)
1298c2ecf20Sopenharmony_ci		return ret;
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	return build_id__sprintf(&bid, sbuild_id);
1328c2ecf20Sopenharmony_ci}
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ciint filename__sprintf_build_id(const char *pathname, char *sbuild_id)
1358c2ecf20Sopenharmony_ci{
1368c2ecf20Sopenharmony_ci	struct build_id bid;
1378c2ecf20Sopenharmony_ci	int ret;
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	ret = filename__read_build_id(pathname, &bid);
1408c2ecf20Sopenharmony_ci	if (ret < 0)
1418c2ecf20Sopenharmony_ci		return ret;
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	return build_id__sprintf(&bid, sbuild_id);
1448c2ecf20Sopenharmony_ci}
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci/* asnprintf consolidates asprintf and snprintf */
1478c2ecf20Sopenharmony_cistatic int asnprintf(char **strp, size_t size, const char *fmt, ...)
1488c2ecf20Sopenharmony_ci{
1498c2ecf20Sopenharmony_ci	va_list ap;
1508c2ecf20Sopenharmony_ci	int ret;
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	if (!strp)
1538c2ecf20Sopenharmony_ci		return -EINVAL;
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	va_start(ap, fmt);
1568c2ecf20Sopenharmony_ci	if (*strp)
1578c2ecf20Sopenharmony_ci		ret = vsnprintf(*strp, size, fmt, ap);
1588c2ecf20Sopenharmony_ci	else
1598c2ecf20Sopenharmony_ci		ret = vasprintf(strp, fmt, ap);
1608c2ecf20Sopenharmony_ci	va_end(ap);
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	return ret;
1638c2ecf20Sopenharmony_ci}
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_cichar *build_id_cache__kallsyms_path(const char *sbuild_id, char *bf,
1668c2ecf20Sopenharmony_ci				    size_t size)
1678c2ecf20Sopenharmony_ci{
1688c2ecf20Sopenharmony_ci	bool retry_old = true;
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci	snprintf(bf, size, "%s/%s/%s/kallsyms",
1718c2ecf20Sopenharmony_ci		 buildid_dir, DSO__NAME_KALLSYMS, sbuild_id);
1728c2ecf20Sopenharmony_ciretry:
1738c2ecf20Sopenharmony_ci	if (!access(bf, F_OK))
1748c2ecf20Sopenharmony_ci		return bf;
1758c2ecf20Sopenharmony_ci	if (retry_old) {
1768c2ecf20Sopenharmony_ci		/* Try old style kallsyms cache */
1778c2ecf20Sopenharmony_ci		snprintf(bf, size, "%s/%s/%s",
1788c2ecf20Sopenharmony_ci			 buildid_dir, DSO__NAME_KALLSYMS, sbuild_id);
1798c2ecf20Sopenharmony_ci		retry_old = false;
1808c2ecf20Sopenharmony_ci		goto retry;
1818c2ecf20Sopenharmony_ci	}
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci	return NULL;
1848c2ecf20Sopenharmony_ci}
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_cichar *build_id_cache__linkname(const char *sbuild_id, char *bf, size_t size)
1878c2ecf20Sopenharmony_ci{
1888c2ecf20Sopenharmony_ci	char *tmp = bf;
1898c2ecf20Sopenharmony_ci	int ret = asnprintf(&bf, size, "%s/.build-id/%.2s/%s", buildid_dir,
1908c2ecf20Sopenharmony_ci			    sbuild_id, sbuild_id + 2);
1918c2ecf20Sopenharmony_ci	if (ret < 0 || (tmp && size < (unsigned int)ret))
1928c2ecf20Sopenharmony_ci		return NULL;
1938c2ecf20Sopenharmony_ci	return bf;
1948c2ecf20Sopenharmony_ci}
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci/* The caller is responsible to free the returned buffer. */
1978c2ecf20Sopenharmony_cichar *build_id_cache__origname(const char *sbuild_id)
1988c2ecf20Sopenharmony_ci{
1998c2ecf20Sopenharmony_ci	char *linkname;
2008c2ecf20Sopenharmony_ci	char buf[PATH_MAX];
2018c2ecf20Sopenharmony_ci	char *ret = NULL, *p;
2028c2ecf20Sopenharmony_ci	size_t offs = 5;	/* == strlen("../..") */
2038c2ecf20Sopenharmony_ci	ssize_t len;
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci	linkname = build_id_cache__linkname(sbuild_id, NULL, 0);
2068c2ecf20Sopenharmony_ci	if (!linkname)
2078c2ecf20Sopenharmony_ci		return NULL;
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci	len = readlink(linkname, buf, sizeof(buf) - 1);
2108c2ecf20Sopenharmony_ci	if (len <= 0)
2118c2ecf20Sopenharmony_ci		goto out;
2128c2ecf20Sopenharmony_ci	buf[len] = '\0';
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci	/* The link should be "../..<origpath>/<sbuild_id>" */
2158c2ecf20Sopenharmony_ci	p = strrchr(buf, '/');	/* Cut off the "/<sbuild_id>" */
2168c2ecf20Sopenharmony_ci	if (p && (p > buf + offs)) {
2178c2ecf20Sopenharmony_ci		*p = '\0';
2188c2ecf20Sopenharmony_ci		if (buf[offs + 1] == '[')
2198c2ecf20Sopenharmony_ci			offs++;	/*
2208c2ecf20Sopenharmony_ci				 * This is a DSO name, like [kernel.kallsyms].
2218c2ecf20Sopenharmony_ci				 * Skip the first '/', since this is not the
2228c2ecf20Sopenharmony_ci				 * cache of a regular file.
2238c2ecf20Sopenharmony_ci				 */
2248c2ecf20Sopenharmony_ci		ret = strdup(buf + offs);	/* Skip "../..[/]" */
2258c2ecf20Sopenharmony_ci	}
2268c2ecf20Sopenharmony_ciout:
2278c2ecf20Sopenharmony_ci	free(linkname);
2288c2ecf20Sopenharmony_ci	return ret;
2298c2ecf20Sopenharmony_ci}
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci/* Check if the given build_id cache is valid on current running system */
2328c2ecf20Sopenharmony_cistatic bool build_id_cache__valid_id(char *sbuild_id)
2338c2ecf20Sopenharmony_ci{
2348c2ecf20Sopenharmony_ci	char real_sbuild_id[SBUILD_ID_SIZE] = "";
2358c2ecf20Sopenharmony_ci	char *pathname;
2368c2ecf20Sopenharmony_ci	int ret = 0;
2378c2ecf20Sopenharmony_ci	bool result = false;
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	pathname = build_id_cache__origname(sbuild_id);
2408c2ecf20Sopenharmony_ci	if (!pathname)
2418c2ecf20Sopenharmony_ci		return false;
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci	if (!strcmp(pathname, DSO__NAME_KALLSYMS))
2448c2ecf20Sopenharmony_ci		ret = sysfs__sprintf_build_id("/", real_sbuild_id);
2458c2ecf20Sopenharmony_ci	else if (pathname[0] == '/')
2468c2ecf20Sopenharmony_ci		ret = filename__sprintf_build_id(pathname, real_sbuild_id);
2478c2ecf20Sopenharmony_ci	else
2488c2ecf20Sopenharmony_ci		ret = -EINVAL;	/* Should we support other special DSO cache? */
2498c2ecf20Sopenharmony_ci	if (ret >= 0)
2508c2ecf20Sopenharmony_ci		result = (strcmp(sbuild_id, real_sbuild_id) == 0);
2518c2ecf20Sopenharmony_ci	free(pathname);
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci	return result;
2548c2ecf20Sopenharmony_ci}
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_cistatic const char *build_id_cache__basename(bool is_kallsyms, bool is_vdso,
2578c2ecf20Sopenharmony_ci					    bool is_debug)
2588c2ecf20Sopenharmony_ci{
2598c2ecf20Sopenharmony_ci	return is_kallsyms ? "kallsyms" : (is_vdso ? "vdso" : (is_debug ?
2608c2ecf20Sopenharmony_ci	    "debug" : "elf"));
2618c2ecf20Sopenharmony_ci}
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_cichar *dso__build_id_filename(const struct dso *dso, char *bf, size_t size,
2648c2ecf20Sopenharmony_ci			     bool is_debug)
2658c2ecf20Sopenharmony_ci{
2668c2ecf20Sopenharmony_ci	bool is_kallsyms = dso__is_kallsyms((struct dso *)dso);
2678c2ecf20Sopenharmony_ci	bool is_vdso = dso__is_vdso((struct dso *)dso);
2688c2ecf20Sopenharmony_ci	char sbuild_id[SBUILD_ID_SIZE];
2698c2ecf20Sopenharmony_ci	char *linkname;
2708c2ecf20Sopenharmony_ci	bool alloc = (bf == NULL);
2718c2ecf20Sopenharmony_ci	int ret;
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci	if (!dso->has_build_id)
2748c2ecf20Sopenharmony_ci		return NULL;
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci	build_id__sprintf(&dso->bid, sbuild_id);
2778c2ecf20Sopenharmony_ci	linkname = build_id_cache__linkname(sbuild_id, NULL, 0);
2788c2ecf20Sopenharmony_ci	if (!linkname)
2798c2ecf20Sopenharmony_ci		return NULL;
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci	/* Check if old style build_id cache */
2828c2ecf20Sopenharmony_ci	if (is_regular_file(linkname))
2838c2ecf20Sopenharmony_ci		ret = asnprintf(&bf, size, "%s", linkname);
2848c2ecf20Sopenharmony_ci	else
2858c2ecf20Sopenharmony_ci		ret = asnprintf(&bf, size, "%s/%s", linkname,
2868c2ecf20Sopenharmony_ci			 build_id_cache__basename(is_kallsyms, is_vdso,
2878c2ecf20Sopenharmony_ci						  is_debug));
2888c2ecf20Sopenharmony_ci	if (ret < 0 || (!alloc && size < (unsigned int)ret))
2898c2ecf20Sopenharmony_ci		bf = NULL;
2908c2ecf20Sopenharmony_ci	free(linkname);
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci	return bf;
2938c2ecf20Sopenharmony_ci}
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci#define dsos__for_each_with_build_id(pos, head)	\
2968c2ecf20Sopenharmony_ci	list_for_each_entry(pos, head, node)	\
2978c2ecf20Sopenharmony_ci		if (!pos->has_build_id)		\
2988c2ecf20Sopenharmony_ci			continue;		\
2998c2ecf20Sopenharmony_ci		else
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_cistatic int write_buildid(const char *name, size_t name_len, struct build_id *bid,
3028c2ecf20Sopenharmony_ci			 pid_t pid, u16 misc, struct feat_fd *fd)
3038c2ecf20Sopenharmony_ci{
3048c2ecf20Sopenharmony_ci	int err;
3058c2ecf20Sopenharmony_ci	struct perf_record_header_build_id b;
3068c2ecf20Sopenharmony_ci	size_t len;
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci	len = name_len + 1;
3098c2ecf20Sopenharmony_ci	len = PERF_ALIGN(len, NAME_ALIGN);
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci	memset(&b, 0, sizeof(b));
3128c2ecf20Sopenharmony_ci	memcpy(&b.data, bid->data, bid->size);
3138c2ecf20Sopenharmony_ci	b.size = (u8) bid->size;
3148c2ecf20Sopenharmony_ci	misc |= PERF_RECORD_MISC_BUILD_ID_SIZE;
3158c2ecf20Sopenharmony_ci	b.pid = pid;
3168c2ecf20Sopenharmony_ci	b.header.misc = misc;
3178c2ecf20Sopenharmony_ci	b.header.size = sizeof(b) + len;
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci	err = do_write(fd, &b, sizeof(b));
3208c2ecf20Sopenharmony_ci	if (err < 0)
3218c2ecf20Sopenharmony_ci		return err;
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ci	return write_padded(fd, name, name_len + 1, len);
3248c2ecf20Sopenharmony_ci}
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_cistatic int machine__write_buildid_table(struct machine *machine,
3278c2ecf20Sopenharmony_ci					struct feat_fd *fd)
3288c2ecf20Sopenharmony_ci{
3298c2ecf20Sopenharmony_ci	int err = 0;
3308c2ecf20Sopenharmony_ci	struct dso *pos;
3318c2ecf20Sopenharmony_ci	u16 kmisc = PERF_RECORD_MISC_KERNEL,
3328c2ecf20Sopenharmony_ci	    umisc = PERF_RECORD_MISC_USER;
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci	if (!machine__is_host(machine)) {
3358c2ecf20Sopenharmony_ci		kmisc = PERF_RECORD_MISC_GUEST_KERNEL;
3368c2ecf20Sopenharmony_ci		umisc = PERF_RECORD_MISC_GUEST_USER;
3378c2ecf20Sopenharmony_ci	}
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_ci	dsos__for_each_with_build_id(pos, &machine->dsos.head) {
3408c2ecf20Sopenharmony_ci		const char *name;
3418c2ecf20Sopenharmony_ci		size_t name_len;
3428c2ecf20Sopenharmony_ci		bool in_kernel = false;
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci		if (!pos->hit && !dso__is_vdso(pos))
3458c2ecf20Sopenharmony_ci			continue;
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ci		if (dso__is_vdso(pos)) {
3488c2ecf20Sopenharmony_ci			name = pos->short_name;
3498c2ecf20Sopenharmony_ci			name_len = pos->short_name_len;
3508c2ecf20Sopenharmony_ci		} else if (dso__is_kcore(pos)) {
3518c2ecf20Sopenharmony_ci			name = machine->mmap_name;
3528c2ecf20Sopenharmony_ci			name_len = strlen(name);
3538c2ecf20Sopenharmony_ci		} else {
3548c2ecf20Sopenharmony_ci			name = pos->long_name;
3558c2ecf20Sopenharmony_ci			name_len = pos->long_name_len;
3568c2ecf20Sopenharmony_ci		}
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci		in_kernel = pos->kernel ||
3598c2ecf20Sopenharmony_ci				is_kernel_module(name,
3608c2ecf20Sopenharmony_ci					PERF_RECORD_MISC_CPUMODE_UNKNOWN);
3618c2ecf20Sopenharmony_ci		err = write_buildid(name, name_len, &pos->bid, machine->pid,
3628c2ecf20Sopenharmony_ci				    in_kernel ? kmisc : umisc, fd);
3638c2ecf20Sopenharmony_ci		if (err)
3648c2ecf20Sopenharmony_ci			break;
3658c2ecf20Sopenharmony_ci	}
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_ci	return err;
3688c2ecf20Sopenharmony_ci}
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ciint perf_session__write_buildid_table(struct perf_session *session,
3718c2ecf20Sopenharmony_ci				      struct feat_fd *fd)
3728c2ecf20Sopenharmony_ci{
3738c2ecf20Sopenharmony_ci	struct rb_node *nd;
3748c2ecf20Sopenharmony_ci	int err = machine__write_buildid_table(&session->machines.host, fd);
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci	if (err)
3778c2ecf20Sopenharmony_ci		return err;
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci	for (nd = rb_first_cached(&session->machines.guests); nd;
3808c2ecf20Sopenharmony_ci	     nd = rb_next(nd)) {
3818c2ecf20Sopenharmony_ci		struct machine *pos = rb_entry(nd, struct machine, rb_node);
3828c2ecf20Sopenharmony_ci		err = machine__write_buildid_table(pos, fd);
3838c2ecf20Sopenharmony_ci		if (err)
3848c2ecf20Sopenharmony_ci			break;
3858c2ecf20Sopenharmony_ci	}
3868c2ecf20Sopenharmony_ci	return err;
3878c2ecf20Sopenharmony_ci}
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_cistatic int __dsos__hit_all(struct list_head *head)
3908c2ecf20Sopenharmony_ci{
3918c2ecf20Sopenharmony_ci	struct dso *pos;
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci	list_for_each_entry(pos, head, node)
3948c2ecf20Sopenharmony_ci		pos->hit = true;
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci	return 0;
3978c2ecf20Sopenharmony_ci}
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_cistatic int machine__hit_all_dsos(struct machine *machine)
4008c2ecf20Sopenharmony_ci{
4018c2ecf20Sopenharmony_ci	return __dsos__hit_all(&machine->dsos.head);
4028c2ecf20Sopenharmony_ci}
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_ciint dsos__hit_all(struct perf_session *session)
4058c2ecf20Sopenharmony_ci{
4068c2ecf20Sopenharmony_ci	struct rb_node *nd;
4078c2ecf20Sopenharmony_ci	int err;
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci	err = machine__hit_all_dsos(&session->machines.host);
4108c2ecf20Sopenharmony_ci	if (err)
4118c2ecf20Sopenharmony_ci		return err;
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_ci	for (nd = rb_first_cached(&session->machines.guests); nd;
4148c2ecf20Sopenharmony_ci	     nd = rb_next(nd)) {
4158c2ecf20Sopenharmony_ci		struct machine *pos = rb_entry(nd, struct machine, rb_node);
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ci		err = machine__hit_all_dsos(pos);
4188c2ecf20Sopenharmony_ci		if (err)
4198c2ecf20Sopenharmony_ci			return err;
4208c2ecf20Sopenharmony_ci	}
4218c2ecf20Sopenharmony_ci
4228c2ecf20Sopenharmony_ci	return 0;
4238c2ecf20Sopenharmony_ci}
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_civoid disable_buildid_cache(void)
4268c2ecf20Sopenharmony_ci{
4278c2ecf20Sopenharmony_ci	no_buildid_cache = true;
4288c2ecf20Sopenharmony_ci}
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_cistatic bool lsdir_bid_head_filter(const char *name __maybe_unused,
4318c2ecf20Sopenharmony_ci				  struct dirent *d)
4328c2ecf20Sopenharmony_ci{
4338c2ecf20Sopenharmony_ci	return (strlen(d->d_name) == 2) &&
4348c2ecf20Sopenharmony_ci		isxdigit(d->d_name[0]) && isxdigit(d->d_name[1]);
4358c2ecf20Sopenharmony_ci}
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_cistatic bool lsdir_bid_tail_filter(const char *name __maybe_unused,
4388c2ecf20Sopenharmony_ci				  struct dirent *d)
4398c2ecf20Sopenharmony_ci{
4408c2ecf20Sopenharmony_ci	int i = 0;
4418c2ecf20Sopenharmony_ci	while (isxdigit(d->d_name[i]) && i < SBUILD_ID_SIZE - 3)
4428c2ecf20Sopenharmony_ci		i++;
4438c2ecf20Sopenharmony_ci	return (i == SBUILD_ID_SIZE - 3) && (d->d_name[i] == '\0');
4448c2ecf20Sopenharmony_ci}
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_cistruct strlist *build_id_cache__list_all(bool validonly)
4478c2ecf20Sopenharmony_ci{
4488c2ecf20Sopenharmony_ci	struct strlist *toplist, *linklist = NULL, *bidlist;
4498c2ecf20Sopenharmony_ci	struct str_node *nd, *nd2;
4508c2ecf20Sopenharmony_ci	char *topdir, *linkdir = NULL;
4518c2ecf20Sopenharmony_ci	char sbuild_id[SBUILD_ID_SIZE];
4528c2ecf20Sopenharmony_ci
4538c2ecf20Sopenharmony_ci	/* for filename__ functions */
4548c2ecf20Sopenharmony_ci	if (validonly)
4558c2ecf20Sopenharmony_ci		symbol__init(NULL);
4568c2ecf20Sopenharmony_ci
4578c2ecf20Sopenharmony_ci	/* Open the top-level directory */
4588c2ecf20Sopenharmony_ci	if (asprintf(&topdir, "%s/.build-id/", buildid_dir) < 0)
4598c2ecf20Sopenharmony_ci		return NULL;
4608c2ecf20Sopenharmony_ci
4618c2ecf20Sopenharmony_ci	bidlist = strlist__new(NULL, NULL);
4628c2ecf20Sopenharmony_ci	if (!bidlist)
4638c2ecf20Sopenharmony_ci		goto out;
4648c2ecf20Sopenharmony_ci
4658c2ecf20Sopenharmony_ci	toplist = lsdir(topdir, lsdir_bid_head_filter);
4668c2ecf20Sopenharmony_ci	if (!toplist) {
4678c2ecf20Sopenharmony_ci		pr_debug("Error in lsdir(%s): %d\n", topdir, errno);
4688c2ecf20Sopenharmony_ci		/* If there is no buildid cache, return an empty list */
4698c2ecf20Sopenharmony_ci		if (errno == ENOENT)
4708c2ecf20Sopenharmony_ci			goto out;
4718c2ecf20Sopenharmony_ci		goto err_out;
4728c2ecf20Sopenharmony_ci	}
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_ci	strlist__for_each_entry(nd, toplist) {
4758c2ecf20Sopenharmony_ci		if (asprintf(&linkdir, "%s/%s", topdir, nd->s) < 0)
4768c2ecf20Sopenharmony_ci			goto err_out;
4778c2ecf20Sopenharmony_ci		/* Open the lower-level directory */
4788c2ecf20Sopenharmony_ci		linklist = lsdir(linkdir, lsdir_bid_tail_filter);
4798c2ecf20Sopenharmony_ci		if (!linklist) {
4808c2ecf20Sopenharmony_ci			pr_debug("Error in lsdir(%s): %d\n", linkdir, errno);
4818c2ecf20Sopenharmony_ci			goto err_out;
4828c2ecf20Sopenharmony_ci		}
4838c2ecf20Sopenharmony_ci		strlist__for_each_entry(nd2, linklist) {
4848c2ecf20Sopenharmony_ci			if (snprintf(sbuild_id, SBUILD_ID_SIZE, "%s%s",
4858c2ecf20Sopenharmony_ci				     nd->s, nd2->s) != SBUILD_ID_SIZE - 1)
4868c2ecf20Sopenharmony_ci				goto err_out;
4878c2ecf20Sopenharmony_ci			if (validonly && !build_id_cache__valid_id(sbuild_id))
4888c2ecf20Sopenharmony_ci				continue;
4898c2ecf20Sopenharmony_ci			if (strlist__add(bidlist, sbuild_id) < 0)
4908c2ecf20Sopenharmony_ci				goto err_out;
4918c2ecf20Sopenharmony_ci		}
4928c2ecf20Sopenharmony_ci		strlist__delete(linklist);
4938c2ecf20Sopenharmony_ci		zfree(&linkdir);
4948c2ecf20Sopenharmony_ci	}
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_ciout_free:
4978c2ecf20Sopenharmony_ci	strlist__delete(toplist);
4988c2ecf20Sopenharmony_ciout:
4998c2ecf20Sopenharmony_ci	free(topdir);
5008c2ecf20Sopenharmony_ci
5018c2ecf20Sopenharmony_ci	return bidlist;
5028c2ecf20Sopenharmony_ci
5038c2ecf20Sopenharmony_cierr_out:
5048c2ecf20Sopenharmony_ci	strlist__delete(linklist);
5058c2ecf20Sopenharmony_ci	zfree(&linkdir);
5068c2ecf20Sopenharmony_ci	strlist__delete(bidlist);
5078c2ecf20Sopenharmony_ci	bidlist = NULL;
5088c2ecf20Sopenharmony_ci	goto out_free;
5098c2ecf20Sopenharmony_ci}
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_cistatic bool str_is_build_id(const char *maybe_sbuild_id, size_t len)
5128c2ecf20Sopenharmony_ci{
5138c2ecf20Sopenharmony_ci	size_t i;
5148c2ecf20Sopenharmony_ci
5158c2ecf20Sopenharmony_ci	for (i = 0; i < len; i++) {
5168c2ecf20Sopenharmony_ci		if (!isxdigit(maybe_sbuild_id[i]))
5178c2ecf20Sopenharmony_ci			return false;
5188c2ecf20Sopenharmony_ci	}
5198c2ecf20Sopenharmony_ci	return true;
5208c2ecf20Sopenharmony_ci}
5218c2ecf20Sopenharmony_ci
5228c2ecf20Sopenharmony_ci/* Return the valid complete build-id */
5238c2ecf20Sopenharmony_cichar *build_id_cache__complement(const char *incomplete_sbuild_id)
5248c2ecf20Sopenharmony_ci{
5258c2ecf20Sopenharmony_ci	struct strlist *bidlist;
5268c2ecf20Sopenharmony_ci	struct str_node *nd, *cand = NULL;
5278c2ecf20Sopenharmony_ci	char *sbuild_id = NULL;
5288c2ecf20Sopenharmony_ci	size_t len = strlen(incomplete_sbuild_id);
5298c2ecf20Sopenharmony_ci
5308c2ecf20Sopenharmony_ci	if (len >= SBUILD_ID_SIZE ||
5318c2ecf20Sopenharmony_ci	    !str_is_build_id(incomplete_sbuild_id, len))
5328c2ecf20Sopenharmony_ci		return NULL;
5338c2ecf20Sopenharmony_ci
5348c2ecf20Sopenharmony_ci	bidlist = build_id_cache__list_all(true);
5358c2ecf20Sopenharmony_ci	if (!bidlist)
5368c2ecf20Sopenharmony_ci		return NULL;
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_ci	strlist__for_each_entry(nd, bidlist) {
5398c2ecf20Sopenharmony_ci		if (strncmp(nd->s, incomplete_sbuild_id, len) != 0)
5408c2ecf20Sopenharmony_ci			continue;
5418c2ecf20Sopenharmony_ci		if (cand) {	/* Error: There are more than 2 candidates. */
5428c2ecf20Sopenharmony_ci			cand = NULL;
5438c2ecf20Sopenharmony_ci			break;
5448c2ecf20Sopenharmony_ci		}
5458c2ecf20Sopenharmony_ci		cand = nd;
5468c2ecf20Sopenharmony_ci	}
5478c2ecf20Sopenharmony_ci	if (cand)
5488c2ecf20Sopenharmony_ci		sbuild_id = strdup(cand->s);
5498c2ecf20Sopenharmony_ci	strlist__delete(bidlist);
5508c2ecf20Sopenharmony_ci
5518c2ecf20Sopenharmony_ci	return sbuild_id;
5528c2ecf20Sopenharmony_ci}
5538c2ecf20Sopenharmony_ci
5548c2ecf20Sopenharmony_cichar *build_id_cache__cachedir(const char *sbuild_id, const char *name,
5558c2ecf20Sopenharmony_ci			       struct nsinfo *nsi, bool is_kallsyms,
5568c2ecf20Sopenharmony_ci			       bool is_vdso)
5578c2ecf20Sopenharmony_ci{
5588c2ecf20Sopenharmony_ci	char *realname = (char *)name, *filename;
5598c2ecf20Sopenharmony_ci	bool slash = is_kallsyms || is_vdso;
5608c2ecf20Sopenharmony_ci
5618c2ecf20Sopenharmony_ci	if (!slash) {
5628c2ecf20Sopenharmony_ci		realname = nsinfo__realpath(name, nsi);
5638c2ecf20Sopenharmony_ci		if (!realname)
5648c2ecf20Sopenharmony_ci			return NULL;
5658c2ecf20Sopenharmony_ci	}
5668c2ecf20Sopenharmony_ci
5678c2ecf20Sopenharmony_ci	if (asprintf(&filename, "%s%s%s%s%s", buildid_dir, slash ? "/" : "",
5688c2ecf20Sopenharmony_ci		     is_vdso ? DSO__NAME_VDSO : realname,
5698c2ecf20Sopenharmony_ci		     sbuild_id ? "/" : "", sbuild_id ?: "") < 0)
5708c2ecf20Sopenharmony_ci		filename = NULL;
5718c2ecf20Sopenharmony_ci
5728c2ecf20Sopenharmony_ci	if (!slash)
5738c2ecf20Sopenharmony_ci		free(realname);
5748c2ecf20Sopenharmony_ci
5758c2ecf20Sopenharmony_ci	return filename;
5768c2ecf20Sopenharmony_ci}
5778c2ecf20Sopenharmony_ci
5788c2ecf20Sopenharmony_ciint build_id_cache__list_build_ids(const char *pathname, struct nsinfo *nsi,
5798c2ecf20Sopenharmony_ci				   struct strlist **result)
5808c2ecf20Sopenharmony_ci{
5818c2ecf20Sopenharmony_ci	char *dir_name;
5828c2ecf20Sopenharmony_ci	int ret = 0;
5838c2ecf20Sopenharmony_ci
5848c2ecf20Sopenharmony_ci	dir_name = build_id_cache__cachedir(NULL, pathname, nsi, false, false);
5858c2ecf20Sopenharmony_ci	if (!dir_name)
5868c2ecf20Sopenharmony_ci		return -ENOMEM;
5878c2ecf20Sopenharmony_ci
5888c2ecf20Sopenharmony_ci	*result = lsdir(dir_name, lsdir_no_dot_filter);
5898c2ecf20Sopenharmony_ci	if (!*result)
5908c2ecf20Sopenharmony_ci		ret = -errno;
5918c2ecf20Sopenharmony_ci	free(dir_name);
5928c2ecf20Sopenharmony_ci
5938c2ecf20Sopenharmony_ci	return ret;
5948c2ecf20Sopenharmony_ci}
5958c2ecf20Sopenharmony_ci
5968c2ecf20Sopenharmony_ci#if defined(HAVE_LIBELF_SUPPORT) && defined(HAVE_GELF_GETNOTE_SUPPORT)
5978c2ecf20Sopenharmony_cistatic int build_id_cache__add_sdt_cache(const char *sbuild_id,
5988c2ecf20Sopenharmony_ci					  const char *realname,
5998c2ecf20Sopenharmony_ci					  struct nsinfo *nsi)
6008c2ecf20Sopenharmony_ci{
6018c2ecf20Sopenharmony_ci	struct probe_cache *cache;
6028c2ecf20Sopenharmony_ci	int ret;
6038c2ecf20Sopenharmony_ci	struct nscookie nsc;
6048c2ecf20Sopenharmony_ci
6058c2ecf20Sopenharmony_ci	cache = probe_cache__new(sbuild_id, nsi);
6068c2ecf20Sopenharmony_ci	if (!cache)
6078c2ecf20Sopenharmony_ci		return -1;
6088c2ecf20Sopenharmony_ci
6098c2ecf20Sopenharmony_ci	nsinfo__mountns_enter(nsi, &nsc);
6108c2ecf20Sopenharmony_ci	ret = probe_cache__scan_sdt(cache, realname);
6118c2ecf20Sopenharmony_ci	nsinfo__mountns_exit(&nsc);
6128c2ecf20Sopenharmony_ci	if (ret >= 0) {
6138c2ecf20Sopenharmony_ci		pr_debug4("Found %d SDTs in %s\n", ret, realname);
6148c2ecf20Sopenharmony_ci		if (probe_cache__commit(cache) < 0)
6158c2ecf20Sopenharmony_ci			ret = -1;
6168c2ecf20Sopenharmony_ci	}
6178c2ecf20Sopenharmony_ci	probe_cache__delete(cache);
6188c2ecf20Sopenharmony_ci	return ret;
6198c2ecf20Sopenharmony_ci}
6208c2ecf20Sopenharmony_ci#else
6218c2ecf20Sopenharmony_ci#define build_id_cache__add_sdt_cache(sbuild_id, realname, nsi) (0)
6228c2ecf20Sopenharmony_ci#endif
6238c2ecf20Sopenharmony_ci
6248c2ecf20Sopenharmony_cistatic char *build_id_cache__find_debug(const char *sbuild_id,
6258c2ecf20Sopenharmony_ci					struct nsinfo *nsi)
6268c2ecf20Sopenharmony_ci{
6278c2ecf20Sopenharmony_ci	char *realname = NULL;
6288c2ecf20Sopenharmony_ci	char *debugfile;
6298c2ecf20Sopenharmony_ci	struct nscookie nsc;
6308c2ecf20Sopenharmony_ci	size_t len = 0;
6318c2ecf20Sopenharmony_ci
6328c2ecf20Sopenharmony_ci	debugfile = calloc(1, PATH_MAX);
6338c2ecf20Sopenharmony_ci	if (!debugfile)
6348c2ecf20Sopenharmony_ci		goto out;
6358c2ecf20Sopenharmony_ci
6368c2ecf20Sopenharmony_ci	len = __symbol__join_symfs(debugfile, PATH_MAX,
6378c2ecf20Sopenharmony_ci				   "/usr/lib/debug/.build-id/");
6388c2ecf20Sopenharmony_ci	snprintf(debugfile + len, PATH_MAX - len, "%.2s/%s.debug", sbuild_id,
6398c2ecf20Sopenharmony_ci		 sbuild_id + 2);
6408c2ecf20Sopenharmony_ci
6418c2ecf20Sopenharmony_ci	nsinfo__mountns_enter(nsi, &nsc);
6428c2ecf20Sopenharmony_ci	realname = realpath(debugfile, NULL);
6438c2ecf20Sopenharmony_ci	if (realname && access(realname, R_OK))
6448c2ecf20Sopenharmony_ci		zfree(&realname);
6458c2ecf20Sopenharmony_ci	nsinfo__mountns_exit(&nsc);
6468c2ecf20Sopenharmony_ci
6478c2ecf20Sopenharmony_ci#ifdef HAVE_DEBUGINFOD_SUPPORT
6488c2ecf20Sopenharmony_ci        if (realname == NULL) {
6498c2ecf20Sopenharmony_ci                debuginfod_client* c = debuginfod_begin();
6508c2ecf20Sopenharmony_ci                if (c != NULL) {
6518c2ecf20Sopenharmony_ci                        int fd = debuginfod_find_debuginfo(c,
6528c2ecf20Sopenharmony_ci                                                           (const unsigned char*)sbuild_id, 0,
6538c2ecf20Sopenharmony_ci                                                           &realname);
6548c2ecf20Sopenharmony_ci                        if (fd >= 0)
6558c2ecf20Sopenharmony_ci                                close(fd); /* retaining reference by realname */
6568c2ecf20Sopenharmony_ci                        debuginfod_end(c);
6578c2ecf20Sopenharmony_ci                }
6588c2ecf20Sopenharmony_ci        }
6598c2ecf20Sopenharmony_ci#endif
6608c2ecf20Sopenharmony_ci
6618c2ecf20Sopenharmony_ciout:
6628c2ecf20Sopenharmony_ci	free(debugfile);
6638c2ecf20Sopenharmony_ci	return realname;
6648c2ecf20Sopenharmony_ci}
6658c2ecf20Sopenharmony_ci
6668c2ecf20Sopenharmony_ciint build_id_cache__add_s(const char *sbuild_id, const char *name,
6678c2ecf20Sopenharmony_ci			  struct nsinfo *nsi, bool is_kallsyms, bool is_vdso)
6688c2ecf20Sopenharmony_ci{
6698c2ecf20Sopenharmony_ci	const size_t size = PATH_MAX;
6708c2ecf20Sopenharmony_ci	char *realname = NULL, *filename = NULL, *dir_name = NULL,
6718c2ecf20Sopenharmony_ci	     *linkname = zalloc(size), *tmp;
6728c2ecf20Sopenharmony_ci	char *debugfile = NULL;
6738c2ecf20Sopenharmony_ci	int err = -1;
6748c2ecf20Sopenharmony_ci
6758c2ecf20Sopenharmony_ci	if (!is_kallsyms) {
6768c2ecf20Sopenharmony_ci		if (!is_vdso)
6778c2ecf20Sopenharmony_ci			realname = nsinfo__realpath(name, nsi);
6788c2ecf20Sopenharmony_ci		else
6798c2ecf20Sopenharmony_ci			realname = realpath(name, NULL);
6808c2ecf20Sopenharmony_ci		if (!realname)
6818c2ecf20Sopenharmony_ci			goto out_free;
6828c2ecf20Sopenharmony_ci	}
6838c2ecf20Sopenharmony_ci
6848c2ecf20Sopenharmony_ci	dir_name = build_id_cache__cachedir(sbuild_id, name, nsi, is_kallsyms,
6858c2ecf20Sopenharmony_ci					    is_vdso);
6868c2ecf20Sopenharmony_ci	if (!dir_name)
6878c2ecf20Sopenharmony_ci		goto out_free;
6888c2ecf20Sopenharmony_ci
6898c2ecf20Sopenharmony_ci	/* Remove old style build-id cache */
6908c2ecf20Sopenharmony_ci	if (is_regular_file(dir_name))
6918c2ecf20Sopenharmony_ci		if (unlink(dir_name))
6928c2ecf20Sopenharmony_ci			goto out_free;
6938c2ecf20Sopenharmony_ci
6948c2ecf20Sopenharmony_ci	if (mkdir_p(dir_name, 0755))
6958c2ecf20Sopenharmony_ci		goto out_free;
6968c2ecf20Sopenharmony_ci
6978c2ecf20Sopenharmony_ci	/* Save the allocated buildid dirname */
6988c2ecf20Sopenharmony_ci	if (asprintf(&filename, "%s/%s", dir_name,
6998c2ecf20Sopenharmony_ci		     build_id_cache__basename(is_kallsyms, is_vdso,
7008c2ecf20Sopenharmony_ci		     false)) < 0) {
7018c2ecf20Sopenharmony_ci		filename = NULL;
7028c2ecf20Sopenharmony_ci		goto out_free;
7038c2ecf20Sopenharmony_ci	}
7048c2ecf20Sopenharmony_ci
7058c2ecf20Sopenharmony_ci	if (access(filename, F_OK)) {
7068c2ecf20Sopenharmony_ci		if (is_kallsyms) {
7078c2ecf20Sopenharmony_ci			if (copyfile("/proc/kallsyms", filename))
7088c2ecf20Sopenharmony_ci				goto out_free;
7098c2ecf20Sopenharmony_ci		} else if (nsi && nsi->need_setns) {
7108c2ecf20Sopenharmony_ci			if (copyfile_ns(name, filename, nsi))
7118c2ecf20Sopenharmony_ci				goto out_free;
7128c2ecf20Sopenharmony_ci		} else if (link(realname, filename) && errno != EEXIST &&
7138c2ecf20Sopenharmony_ci				copyfile(name, filename))
7148c2ecf20Sopenharmony_ci			goto out_free;
7158c2ecf20Sopenharmony_ci	}
7168c2ecf20Sopenharmony_ci
7178c2ecf20Sopenharmony_ci	/* Some binaries are stripped, but have .debug files with their symbol
7188c2ecf20Sopenharmony_ci	 * table.  Check to see if we can locate one of those, since the elf
7198c2ecf20Sopenharmony_ci	 * file itself may not be very useful to users of our tools without a
7208c2ecf20Sopenharmony_ci	 * symtab.
7218c2ecf20Sopenharmony_ci	 */
7228c2ecf20Sopenharmony_ci	if (!is_kallsyms && !is_vdso &&
7238c2ecf20Sopenharmony_ci	    strncmp(".ko", name + strlen(name) - 3, 3)) {
7248c2ecf20Sopenharmony_ci		debugfile = build_id_cache__find_debug(sbuild_id, nsi);
7258c2ecf20Sopenharmony_ci		if (debugfile) {
7268c2ecf20Sopenharmony_ci			zfree(&filename);
7278c2ecf20Sopenharmony_ci			if (asprintf(&filename, "%s/%s", dir_name,
7288c2ecf20Sopenharmony_ci			    build_id_cache__basename(false, false, true)) < 0) {
7298c2ecf20Sopenharmony_ci				filename = NULL;
7308c2ecf20Sopenharmony_ci				goto out_free;
7318c2ecf20Sopenharmony_ci			}
7328c2ecf20Sopenharmony_ci			if (access(filename, F_OK)) {
7338c2ecf20Sopenharmony_ci				if (nsi && nsi->need_setns) {
7348c2ecf20Sopenharmony_ci					if (copyfile_ns(debugfile, filename,
7358c2ecf20Sopenharmony_ci							nsi))
7368c2ecf20Sopenharmony_ci						goto out_free;
7378c2ecf20Sopenharmony_ci				} else if (link(debugfile, filename) &&
7388c2ecf20Sopenharmony_ci						errno != EEXIST &&
7398c2ecf20Sopenharmony_ci						copyfile(debugfile, filename))
7408c2ecf20Sopenharmony_ci					goto out_free;
7418c2ecf20Sopenharmony_ci			}
7428c2ecf20Sopenharmony_ci		}
7438c2ecf20Sopenharmony_ci	}
7448c2ecf20Sopenharmony_ci
7458c2ecf20Sopenharmony_ci	if (!build_id_cache__linkname(sbuild_id, linkname, size))
7468c2ecf20Sopenharmony_ci		goto out_free;
7478c2ecf20Sopenharmony_ci	tmp = strrchr(linkname, '/');
7488c2ecf20Sopenharmony_ci	*tmp = '\0';
7498c2ecf20Sopenharmony_ci
7508c2ecf20Sopenharmony_ci	if (access(linkname, X_OK) && mkdir_p(linkname, 0755))
7518c2ecf20Sopenharmony_ci		goto out_free;
7528c2ecf20Sopenharmony_ci
7538c2ecf20Sopenharmony_ci	*tmp = '/';
7548c2ecf20Sopenharmony_ci	tmp = dir_name + strlen(buildid_dir) - 5;
7558c2ecf20Sopenharmony_ci	memcpy(tmp, "../..", 5);
7568c2ecf20Sopenharmony_ci
7578c2ecf20Sopenharmony_ci	if (symlink(tmp, linkname) == 0)
7588c2ecf20Sopenharmony_ci		err = 0;
7598c2ecf20Sopenharmony_ci
7608c2ecf20Sopenharmony_ci	/* Update SDT cache : error is just warned */
7618c2ecf20Sopenharmony_ci	if (realname &&
7628c2ecf20Sopenharmony_ci	    build_id_cache__add_sdt_cache(sbuild_id, realname, nsi) < 0)
7638c2ecf20Sopenharmony_ci		pr_debug4("Failed to update/scan SDT cache for %s\n", realname);
7648c2ecf20Sopenharmony_ci
7658c2ecf20Sopenharmony_ciout_free:
7668c2ecf20Sopenharmony_ci	if (!is_kallsyms)
7678c2ecf20Sopenharmony_ci		free(realname);
7688c2ecf20Sopenharmony_ci	free(filename);
7698c2ecf20Sopenharmony_ci	free(debugfile);
7708c2ecf20Sopenharmony_ci	free(dir_name);
7718c2ecf20Sopenharmony_ci	free(linkname);
7728c2ecf20Sopenharmony_ci	return err;
7738c2ecf20Sopenharmony_ci}
7748c2ecf20Sopenharmony_ci
7758c2ecf20Sopenharmony_cistatic int build_id_cache__add_b(const struct build_id *bid,
7768c2ecf20Sopenharmony_ci				 const char *name, struct nsinfo *nsi,
7778c2ecf20Sopenharmony_ci				 bool is_kallsyms, bool is_vdso)
7788c2ecf20Sopenharmony_ci{
7798c2ecf20Sopenharmony_ci	char sbuild_id[SBUILD_ID_SIZE];
7808c2ecf20Sopenharmony_ci
7818c2ecf20Sopenharmony_ci	build_id__sprintf(bid, sbuild_id);
7828c2ecf20Sopenharmony_ci
7838c2ecf20Sopenharmony_ci	return build_id_cache__add_s(sbuild_id, name, nsi, is_kallsyms,
7848c2ecf20Sopenharmony_ci				     is_vdso);
7858c2ecf20Sopenharmony_ci}
7868c2ecf20Sopenharmony_ci
7878c2ecf20Sopenharmony_cibool build_id_cache__cached(const char *sbuild_id)
7888c2ecf20Sopenharmony_ci{
7898c2ecf20Sopenharmony_ci	bool ret = false;
7908c2ecf20Sopenharmony_ci	char *filename = build_id_cache__linkname(sbuild_id, NULL, 0);
7918c2ecf20Sopenharmony_ci
7928c2ecf20Sopenharmony_ci	if (filename && !access(filename, F_OK))
7938c2ecf20Sopenharmony_ci		ret = true;
7948c2ecf20Sopenharmony_ci	free(filename);
7958c2ecf20Sopenharmony_ci
7968c2ecf20Sopenharmony_ci	return ret;
7978c2ecf20Sopenharmony_ci}
7988c2ecf20Sopenharmony_ci
7998c2ecf20Sopenharmony_ciint build_id_cache__remove_s(const char *sbuild_id)
8008c2ecf20Sopenharmony_ci{
8018c2ecf20Sopenharmony_ci	const size_t size = PATH_MAX;
8028c2ecf20Sopenharmony_ci	char *filename = zalloc(size),
8038c2ecf20Sopenharmony_ci	     *linkname = zalloc(size), *tmp;
8048c2ecf20Sopenharmony_ci	int err = -1;
8058c2ecf20Sopenharmony_ci
8068c2ecf20Sopenharmony_ci	if (filename == NULL || linkname == NULL)
8078c2ecf20Sopenharmony_ci		goto out_free;
8088c2ecf20Sopenharmony_ci
8098c2ecf20Sopenharmony_ci	if (!build_id_cache__linkname(sbuild_id, linkname, size))
8108c2ecf20Sopenharmony_ci		goto out_free;
8118c2ecf20Sopenharmony_ci
8128c2ecf20Sopenharmony_ci	if (access(linkname, F_OK))
8138c2ecf20Sopenharmony_ci		goto out_free;
8148c2ecf20Sopenharmony_ci
8158c2ecf20Sopenharmony_ci	if (readlink(linkname, filename, size - 1) < 0)
8168c2ecf20Sopenharmony_ci		goto out_free;
8178c2ecf20Sopenharmony_ci
8188c2ecf20Sopenharmony_ci	if (unlink(linkname))
8198c2ecf20Sopenharmony_ci		goto out_free;
8208c2ecf20Sopenharmony_ci
8218c2ecf20Sopenharmony_ci	/*
8228c2ecf20Sopenharmony_ci	 * Since the link is relative, we must make it absolute:
8238c2ecf20Sopenharmony_ci	 */
8248c2ecf20Sopenharmony_ci	tmp = strrchr(linkname, '/') + 1;
8258c2ecf20Sopenharmony_ci	snprintf(tmp, size - (tmp - linkname), "%s", filename);
8268c2ecf20Sopenharmony_ci
8278c2ecf20Sopenharmony_ci	if (rm_rf(linkname))
8288c2ecf20Sopenharmony_ci		goto out_free;
8298c2ecf20Sopenharmony_ci
8308c2ecf20Sopenharmony_ci	err = 0;
8318c2ecf20Sopenharmony_ciout_free:
8328c2ecf20Sopenharmony_ci	free(filename);
8338c2ecf20Sopenharmony_ci	free(linkname);
8348c2ecf20Sopenharmony_ci	return err;
8358c2ecf20Sopenharmony_ci}
8368c2ecf20Sopenharmony_ci
8378c2ecf20Sopenharmony_cistatic int dso__cache_build_id(struct dso *dso, struct machine *machine)
8388c2ecf20Sopenharmony_ci{
8398c2ecf20Sopenharmony_ci	bool is_kallsyms = dso__is_kallsyms(dso);
8408c2ecf20Sopenharmony_ci	bool is_vdso = dso__is_vdso(dso);
8418c2ecf20Sopenharmony_ci	const char *name = dso->long_name;
8428c2ecf20Sopenharmony_ci
8438c2ecf20Sopenharmony_ci	if (dso__is_kcore(dso)) {
8448c2ecf20Sopenharmony_ci		is_kallsyms = true;
8458c2ecf20Sopenharmony_ci		name = machine->mmap_name;
8468c2ecf20Sopenharmony_ci	}
8478c2ecf20Sopenharmony_ci	return build_id_cache__add_b(&dso->bid, name, dso->nsinfo,
8488c2ecf20Sopenharmony_ci				     is_kallsyms, is_vdso);
8498c2ecf20Sopenharmony_ci}
8508c2ecf20Sopenharmony_ci
8518c2ecf20Sopenharmony_cistatic int __dsos__cache_build_ids(struct list_head *head,
8528c2ecf20Sopenharmony_ci				   struct machine *machine)
8538c2ecf20Sopenharmony_ci{
8548c2ecf20Sopenharmony_ci	struct dso *pos;
8558c2ecf20Sopenharmony_ci	int err = 0;
8568c2ecf20Sopenharmony_ci
8578c2ecf20Sopenharmony_ci	dsos__for_each_with_build_id(pos, head)
8588c2ecf20Sopenharmony_ci		if (dso__cache_build_id(pos, machine))
8598c2ecf20Sopenharmony_ci			err = -1;
8608c2ecf20Sopenharmony_ci
8618c2ecf20Sopenharmony_ci	return err;
8628c2ecf20Sopenharmony_ci}
8638c2ecf20Sopenharmony_ci
8648c2ecf20Sopenharmony_cistatic int machine__cache_build_ids(struct machine *machine)
8658c2ecf20Sopenharmony_ci{
8668c2ecf20Sopenharmony_ci	return __dsos__cache_build_ids(&machine->dsos.head, machine);
8678c2ecf20Sopenharmony_ci}
8688c2ecf20Sopenharmony_ci
8698c2ecf20Sopenharmony_ciint perf_session__cache_build_ids(struct perf_session *session)
8708c2ecf20Sopenharmony_ci{
8718c2ecf20Sopenharmony_ci	struct rb_node *nd;
8728c2ecf20Sopenharmony_ci	int ret;
8738c2ecf20Sopenharmony_ci
8748c2ecf20Sopenharmony_ci	if (no_buildid_cache)
8758c2ecf20Sopenharmony_ci		return 0;
8768c2ecf20Sopenharmony_ci
8778c2ecf20Sopenharmony_ci	if (mkdir(buildid_dir, 0755) != 0 && errno != EEXIST)
8788c2ecf20Sopenharmony_ci		return -1;
8798c2ecf20Sopenharmony_ci
8808c2ecf20Sopenharmony_ci	ret = machine__cache_build_ids(&session->machines.host);
8818c2ecf20Sopenharmony_ci
8828c2ecf20Sopenharmony_ci	for (nd = rb_first_cached(&session->machines.guests); nd;
8838c2ecf20Sopenharmony_ci	     nd = rb_next(nd)) {
8848c2ecf20Sopenharmony_ci		struct machine *pos = rb_entry(nd, struct machine, rb_node);
8858c2ecf20Sopenharmony_ci		ret |= machine__cache_build_ids(pos);
8868c2ecf20Sopenharmony_ci	}
8878c2ecf20Sopenharmony_ci	return ret ? -1 : 0;
8888c2ecf20Sopenharmony_ci}
8898c2ecf20Sopenharmony_ci
8908c2ecf20Sopenharmony_cistatic bool machine__read_build_ids(struct machine *machine, bool with_hits)
8918c2ecf20Sopenharmony_ci{
8928c2ecf20Sopenharmony_ci	return __dsos__read_build_ids(&machine->dsos.head, with_hits);
8938c2ecf20Sopenharmony_ci}
8948c2ecf20Sopenharmony_ci
8958c2ecf20Sopenharmony_cibool perf_session__read_build_ids(struct perf_session *session, bool with_hits)
8968c2ecf20Sopenharmony_ci{
8978c2ecf20Sopenharmony_ci	struct rb_node *nd;
8988c2ecf20Sopenharmony_ci	bool ret = machine__read_build_ids(&session->machines.host, with_hits);
8998c2ecf20Sopenharmony_ci
9008c2ecf20Sopenharmony_ci	for (nd = rb_first_cached(&session->machines.guests); nd;
9018c2ecf20Sopenharmony_ci	     nd = rb_next(nd)) {
9028c2ecf20Sopenharmony_ci		struct machine *pos = rb_entry(nd, struct machine, rb_node);
9038c2ecf20Sopenharmony_ci		ret |= machine__read_build_ids(pos, with_hits);
9048c2ecf20Sopenharmony_ci	}
9058c2ecf20Sopenharmony_ci
9068c2ecf20Sopenharmony_ci	return ret;
9078c2ecf20Sopenharmony_ci}
9088c2ecf20Sopenharmony_ci
9098c2ecf20Sopenharmony_civoid build_id__init(struct build_id *bid, const u8 *data, size_t size)
9108c2ecf20Sopenharmony_ci{
9118c2ecf20Sopenharmony_ci	WARN_ON(size > BUILD_ID_SIZE);
9128c2ecf20Sopenharmony_ci	memcpy(bid->data, data, size);
9138c2ecf20Sopenharmony_ci	bid->size = size;
9148c2ecf20Sopenharmony_ci}
915