Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^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)  * builtin-buildid-cache.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Builtin buildid-cache command: Manages build-id cache
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Copyright (C) 2010, Red Hat Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * Copyright (C) 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 <sys/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <sys/time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <dirent.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include "builtin.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include "namespaces.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include "util/debug.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include "util/header.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <subcmd/pager.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <subcmd/parse-options.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include "util/strlist.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include "util/build-id.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include "util/session.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include "util/dso.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include "util/symbol.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include "util/time-utils.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #include "util/util.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #include "util/probe-file.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) static int build_id_cache__kcore_buildid(const char *proc_dir, char *sbuildid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	char root_dir[PATH_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	char *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	strlcpy(root_dir, proc_dir, sizeof(root_dir));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	p = strrchr(root_dir, '/');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	*p = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	return sysfs__sprintf_build_id(root_dir, sbuildid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) static int build_id_cache__kcore_dir(char *dir, size_t sz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	return fetch_current_timestamp(dir, sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) static bool same_kallsyms_reloc(const char *from_dir, char *to_dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	char from[PATH_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	char to[PATH_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	u64 addr1 = 0, addr2 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	int i, err = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	scnprintf(from, sizeof(from), "%s/kallsyms", from_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	scnprintf(to, sizeof(to), "%s/kallsyms", to_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	for (i = 0; (name = ref_reloc_sym_names[i]) != NULL; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		err = kallsyms__get_function_start(from, name, &addr1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	if (kallsyms__get_function_start(to, name, &addr2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	return addr1 == addr2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) static int build_id_cache__kcore_existing(const char *from_dir, char *to_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 					  size_t to_dir_sz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	char from[PATH_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	char to[PATH_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	char to_subdir[PATH_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	struct dirent *dent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	int ret = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	DIR *d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	d = opendir(to_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	if (!d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	scnprintf(from, sizeof(from), "%s/modules", from_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		dent = readdir(d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		if (!dent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		if (dent->d_type != DT_DIR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		scnprintf(to, sizeof(to), "%s/%s/modules", to_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 			  dent->d_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		scnprintf(to_subdir, sizeof(to_subdir), "%s/%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 			  to_dir, dent->d_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		if (!compare_proc_modules(from, to) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		    same_kallsyms_reloc(from_dir, to_subdir)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 			strlcpy(to_dir, to_subdir, to_dir_sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 			ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	closedir(d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) static int build_id_cache__add_kcore(const char *filename, bool force)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	char dir[32], sbuildid[SBUILD_ID_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	char from_dir[PATH_MAX], to_dir[PATH_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	char *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	strlcpy(from_dir, filename, sizeof(from_dir));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	p = strrchr(from_dir, '/');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	if (!p || strcmp(p + 1, "kcore"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	*p = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	if (build_id_cache__kcore_buildid(from_dir, sbuildid) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	scnprintf(to_dir, sizeof(to_dir), "%s/%s/%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		  buildid_dir, DSO__NAME_KCORE, sbuildid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	if (!force &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	    !build_id_cache__kcore_existing(from_dir, to_dir, sizeof(to_dir))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		pr_debug("same kcore found in %s\n", to_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	if (build_id_cache__kcore_dir(dir, sizeof(dir)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	scnprintf(to_dir, sizeof(to_dir), "%s/%s/%s/%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		  buildid_dir, DSO__NAME_KCORE, sbuildid, dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	if (mkdir_p(to_dir, 0755))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	if (kcore_copy(from_dir, to_dir)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		/* Remove YYYYmmddHHMMSShh directory */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		if (!rmdir(to_dir)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 			p = strrchr(to_dir, '/');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 			if (p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 				*p = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 			/* Try to remove buildid directory */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 			if (!rmdir(to_dir)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 				p = strrchr(to_dir, '/');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 				if (p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 					*p = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 				/* Try to remove [kernel.kcore] directory */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 				rmdir(to_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	pr_debug("kcore added to build-id cache directory %s\n", to_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) static int build_id_cache__add_file(const char *filename, struct nsinfo *nsi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	char sbuild_id[SBUILD_ID_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	struct build_id bid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	struct nscookie nsc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	nsinfo__mountns_enter(nsi, &nsc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	err = filename__read_build_id(filename, &bid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	nsinfo__mountns_exit(&nsc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		pr_debug("Couldn't read a build-id in %s\n", filename);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	build_id__sprintf(&bid, sbuild_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	err = build_id_cache__add_s(sbuild_id, filename, nsi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 				    false, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	pr_debug("Adding %s %s: %s\n", sbuild_id, filename,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		 err ? "FAIL" : "Ok");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) static int build_id_cache__remove_file(const char *filename, struct nsinfo *nsi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	char sbuild_id[SBUILD_ID_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	struct build_id bid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	struct nscookie nsc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	nsinfo__mountns_enter(nsi, &nsc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	err = filename__read_build_id(filename, &bid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	nsinfo__mountns_exit(&nsc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		pr_debug("Couldn't read a build-id in %s\n", filename);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	build_id__sprintf(&bid, sbuild_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	err = build_id_cache__remove_s(sbuild_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	pr_debug("Removing %s %s: %s\n", sbuild_id, filename,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		 err ? "FAIL" : "Ok");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) static int build_id_cache__purge_path(const char *pathname, struct nsinfo *nsi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	struct strlist *list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	struct str_node *pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	err = build_id_cache__list_build_ids(pathname, nsi, &list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	strlist__for_each_entry(pos, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 		err = build_id_cache__remove_s(pos->s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		pr_debug("Removing %s %s: %s\n", pos->s, pathname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 			 err ? "FAIL" : "Ok");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	strlist__delete(list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	pr_debug("Purging %s: %s\n", pathname, err ? "FAIL" : "Ok");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) static int build_id_cache__purge_all(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	struct strlist *list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	struct str_node *pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	char *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	list = build_id_cache__list_all(false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	if (!list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		pr_debug("Failed to get buildids: -%d\n", errno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	strlist__for_each_entry(pos, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		buf = build_id_cache__origname(pos->s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		err = build_id_cache__remove_s(pos->s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		pr_debug("Removing %s (%s): %s\n", buf, pos->s,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 			 err ? "FAIL" : "Ok");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		free(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	strlist__delete(list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	pr_debug("Purged all: %s\n", err ? "FAIL" : "Ok");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) static bool dso__missing_buildid_cache(struct dso *dso, int parm __maybe_unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	char filename[PATH_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	struct build_id bid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	if (dso__build_id_filename(dso, filename, sizeof(filename), false) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	    filename__read_build_id(filename, &bid) == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 		if (errno == ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 			return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 		pr_warning("Problems with %s file, consider removing it from the cache\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 			   filename);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	} else if (memcmp(dso->bid.data, bid.data, bid.size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		pr_warning("Problems with %s file, consider removing it from the cache\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 			   filename);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) static int build_id_cache__fprintf_missing(struct perf_session *session, FILE *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	perf_session__fprintf_dsos_buildid(session, fp, dso__missing_buildid_cache, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) static int build_id_cache__update_file(const char *filename, struct nsinfo *nsi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	char sbuild_id[SBUILD_ID_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	struct build_id bid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	struct nscookie nsc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	nsinfo__mountns_enter(nsi, &nsc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	err = filename__read_build_id(filename, &bid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	nsinfo__mountns_exit(&nsc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 		pr_debug("Couldn't read a build-id in %s\n", filename);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	build_id__sprintf(&bid, sbuild_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	if (build_id_cache__cached(sbuild_id))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 		err = build_id_cache__remove_s(sbuild_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		err = build_id_cache__add_s(sbuild_id, filename, nsi, false,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 					    false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	pr_debug("Updating %s %s: %s\n", sbuild_id, filename,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		 err ? "FAIL" : "Ok");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) static int build_id_cache__show_all(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	struct strlist *bidlist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	struct str_node *nd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	char *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	bidlist = build_id_cache__list_all(true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	if (!bidlist) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 		pr_debug("Failed to get buildids: -%d\n", errno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	strlist__for_each_entry(nd, bidlist) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 		buf = build_id_cache__origname(nd->s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		fprintf(stdout, "%s %s\n", nd->s, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 		free(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	strlist__delete(bidlist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) int cmd_buildid_cache(int argc, const char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	struct strlist *list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	struct str_node *pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	int ns_id = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	bool force = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	bool list_files = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	bool opts_flag = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	bool purge_all = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	char const *add_name_list_str = NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 		   *remove_name_list_str = NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 		   *purge_name_list_str = NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 		   *missing_filename = NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 		   *update_name_list_str = NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 		   *kcore_filename = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	char sbuf[STRERR_BUFSIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	struct perf_data data = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 		.mode  = PERF_DATA_MODE_READ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	struct perf_session *session = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	struct nsinfo *nsi = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	const struct option buildid_cache_options[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	OPT_STRING('a', "add", &add_name_list_str,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 		   "file list", "file(s) to add"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	OPT_STRING('k', "kcore", &kcore_filename,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 		   "file", "kcore file to add"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	OPT_STRING('r', "remove", &remove_name_list_str, "file list",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 		    "file(s) to remove"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	OPT_STRING('p', "purge", &purge_name_list_str, "file list",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 		    "file(s) to remove (remove old caches too)"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	OPT_BOOLEAN('P', "purge-all", &purge_all, "purge all cached files"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	OPT_BOOLEAN('l', "list", &list_files, "list all cached files"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	OPT_STRING('M', "missing", &missing_filename, "file",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 		   "to find missing build ids in the cache"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	OPT_STRING('u', "update", &update_name_list_str, "file list",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 		    "file(s) to update"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	OPT_INCR('v', "verbose", &verbose, "be more verbose"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	OPT_INTEGER(0, "target-ns", &ns_id, "target pid for namespace context"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	OPT_END()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	const char * const buildid_cache_usage[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 		"perf buildid-cache [<options>]",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 		NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	argc = parse_options(argc, argv, buildid_cache_options,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 			     buildid_cache_usage, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	opts_flag = add_name_list_str || kcore_filename ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 		remove_name_list_str || purge_name_list_str ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 		missing_filename || update_name_list_str ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 		purge_all;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	if (argc || !(list_files || opts_flag))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 		usage_with_options(buildid_cache_usage, buildid_cache_options);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	/* -l is exclusive. It can not be used with other options. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	if (list_files && opts_flag) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 		usage_with_options_msg(buildid_cache_usage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 			buildid_cache_options, "-l is exclusive.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	if (ns_id > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 		nsi = nsinfo__new(ns_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	if (missing_filename) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 		data.path  = missing_filename;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 		data.force = force;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 		session = perf_session__new(&data, false, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 		if (IS_ERR(session))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 			return PTR_ERR(session);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	if (symbol__init(session ? &session->header.env : NULL) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	setup_pager();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	if (list_files) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 		ret = build_id_cache__show_all();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	if (add_name_list_str) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 		list = strlist__new(add_name_list_str, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 		if (list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 			strlist__for_each_entry(pos, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 				if (build_id_cache__add_file(pos->s, nsi)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 					if (errno == EEXIST) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 						pr_debug("%s already in the cache\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 							 pos->s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 						continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 					}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 					pr_warning("Couldn't add %s: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 						   pos->s, str_error_r(errno, sbuf, sizeof(sbuf)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 			strlist__delete(list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	if (remove_name_list_str) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 		list = strlist__new(remove_name_list_str, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 		if (list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 			strlist__for_each_entry(pos, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 				if (build_id_cache__remove_file(pos->s, nsi)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 					if (errno == ENOENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 						pr_debug("%s wasn't in the cache\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 							 pos->s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 						continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 					}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 					pr_warning("Couldn't remove %s: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 						   pos->s, str_error_r(errno, sbuf, sizeof(sbuf)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 			strlist__delete(list);
^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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 	if (purge_name_list_str) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 		list = strlist__new(purge_name_list_str, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 		if (list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 			strlist__for_each_entry(pos, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 				if (build_id_cache__purge_path(pos->s, nsi)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 					if (errno == ENOENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 						pr_debug("%s wasn't in the cache\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 							 pos->s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 						continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 					}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 					pr_warning("Couldn't remove %s: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 						   pos->s, str_error_r(errno, sbuf, sizeof(sbuf)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 			strlist__delete(list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	if (purge_all) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 		if (build_id_cache__purge_all()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 			pr_warning("Couldn't remove some caches. Error: %s.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 				str_error_r(errno, sbuf, sizeof(sbuf)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	if (missing_filename)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 		ret = build_id_cache__fprintf_missing(session, stdout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 	if (update_name_list_str) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 		list = strlist__new(update_name_list_str, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 		if (list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 			strlist__for_each_entry(pos, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 				if (build_id_cache__update_file(pos->s, nsi)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 					if (errno == ENOENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 						pr_debug("%s wasn't in the cache\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 							 pos->s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 						continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 					}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 					pr_warning("Couldn't update %s: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 						   pos->s, str_error_r(errno, sbuf, sizeof(sbuf)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 			strlist__delete(list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 	if (kcore_filename && build_id_cache__add_kcore(kcore_filename, force))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 		pr_warning("Couldn't add %s\n", kcore_filename);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	perf_session__delete(session);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	nsinfo__zput(nsi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) }