1 // SPDX-License-Identifier: GPL-2.0
3 * builtin-buildid-cache.c
5 * Builtin buildid-cache command: Manages build-id cache
7 * Copyright (C) 2010, Red Hat Inc.
8 * Copyright (C) 2010, Arnaldo Carvalho de Melo <acme@redhat.com>
10 #include <sys/types.h>
18 #include "namespaces.h"
19 #include "util/cache.h"
20 #include "util/debug.h"
21 #include "util/header.h"
22 #include <subcmd/parse-options.h>
23 #include "util/strlist.h"
24 #include "util/build-id.h"
25 #include "util/session.h"
26 #include "util/symbol.h"
27 #include "util/time-utils.h"
28 #include "util/util.h"
29 #include "util/probe-file.h"
31 static int build_id_cache__kcore_buildid(const char *proc_dir, char *sbuildid)
33 char root_dir[PATH_MAX];
36 strlcpy(root_dir, proc_dir, sizeof(root_dir));
38 p = strrchr(root_dir, '/');
42 return sysfs__sprintf_build_id(root_dir, sbuildid);
45 static int build_id_cache__kcore_dir(char *dir, size_t sz)
47 return fetch_current_timestamp(dir, sz);
50 static bool same_kallsyms_reloc(const char *from_dir, char *to_dir)
55 u64 addr1 = 0, addr2 = 0;
58 scnprintf(from, sizeof(from), "%s/kallsyms", from_dir);
59 scnprintf(to, sizeof(to), "%s/kallsyms", to_dir);
61 for (i = 0; (name = ref_reloc_sym_names[i]) != NULL; i++) {
62 err = kallsyms__get_function_start(from, name, &addr1);
70 if (kallsyms__get_function_start(to, name, &addr2))
73 return addr1 == addr2;
76 static int build_id_cache__kcore_existing(const char *from_dir, char *to_dir,
81 char to_subdir[PATH_MAX];
90 scnprintf(from, sizeof(from), "%s/modules", from_dir);
96 if (dent->d_type != DT_DIR)
98 scnprintf(to, sizeof(to), "%s/%s/modules", to_dir,
100 scnprintf(to_subdir, sizeof(to_subdir), "%s/%s",
101 to_dir, dent->d_name);
102 if (!compare_proc_modules(from, to) &&
103 same_kallsyms_reloc(from_dir, to_subdir)) {
104 strlcpy(to_dir, to_subdir, to_dir_sz);
115 static int build_id_cache__add_kcore(const char *filename, bool force)
117 char dir[32], sbuildid[SBUILD_ID_SIZE];
118 char from_dir[PATH_MAX], to_dir[PATH_MAX];
121 strlcpy(from_dir, filename, sizeof(from_dir));
123 p = strrchr(from_dir, '/');
124 if (!p || strcmp(p + 1, "kcore"))
128 if (build_id_cache__kcore_buildid(from_dir, sbuildid) < 0)
131 scnprintf(to_dir, sizeof(to_dir), "%s/%s/%s",
132 buildid_dir, DSO__NAME_KCORE, sbuildid);
135 !build_id_cache__kcore_existing(from_dir, to_dir, sizeof(to_dir))) {
136 pr_debug("same kcore found in %s\n", to_dir);
140 if (build_id_cache__kcore_dir(dir, sizeof(dir)))
143 scnprintf(to_dir, sizeof(to_dir), "%s/%s/%s/%s",
144 buildid_dir, DSO__NAME_KCORE, sbuildid, dir);
146 if (mkdir_p(to_dir, 0755))
149 if (kcore_copy(from_dir, to_dir)) {
150 /* Remove YYYYmmddHHMMSShh directory */
151 if (!rmdir(to_dir)) {
152 p = strrchr(to_dir, '/');
155 /* Try to remove buildid directory */
156 if (!rmdir(to_dir)) {
157 p = strrchr(to_dir, '/');
160 /* Try to remove [kernel.kcore] directory */
167 pr_debug("kcore added to build-id cache directory %s\n", to_dir);
172 static int build_id_cache__add_file(const char *filename, struct nsinfo *nsi)
174 char sbuild_id[SBUILD_ID_SIZE];
175 u8 build_id[BUILD_ID_SIZE];
179 nsinfo__mountns_enter(nsi, &nsc);
180 err = filename__read_build_id(filename, &build_id, sizeof(build_id));
181 nsinfo__mountns_exit(&nsc);
183 pr_debug("Couldn't read a build-id in %s\n", filename);
187 build_id__sprintf(build_id, sizeof(build_id), sbuild_id);
188 err = build_id_cache__add_s(sbuild_id, filename, nsi,
190 pr_debug("Adding %s %s: %s\n", sbuild_id, filename,
191 err ? "FAIL" : "Ok");
195 static int build_id_cache__remove_file(const char *filename, struct nsinfo *nsi)
197 u8 build_id[BUILD_ID_SIZE];
198 char sbuild_id[SBUILD_ID_SIZE];
203 nsinfo__mountns_enter(nsi, &nsc);
204 err = filename__read_build_id(filename, &build_id, sizeof(build_id));
205 nsinfo__mountns_exit(&nsc);
207 pr_debug("Couldn't read a build-id in %s\n", filename);
211 build_id__sprintf(build_id, sizeof(build_id), sbuild_id);
212 err = build_id_cache__remove_s(sbuild_id);
213 pr_debug("Removing %s %s: %s\n", sbuild_id, filename,
214 err ? "FAIL" : "Ok");
219 static int build_id_cache__purge_path(const char *pathname, struct nsinfo *nsi)
221 struct strlist *list;
222 struct str_node *pos;
225 err = build_id_cache__list_build_ids(pathname, nsi, &list);
229 strlist__for_each_entry(pos, list) {
230 err = build_id_cache__remove_s(pos->s);
231 pr_debug("Removing %s %s: %s\n", pos->s, pathname,
232 err ? "FAIL" : "Ok");
236 strlist__delete(list);
239 pr_debug("Purging %s: %s\n", pathname, err ? "FAIL" : "Ok");
244 static int build_id_cache__purge_all(void)
246 struct strlist *list;
247 struct str_node *pos;
251 list = build_id_cache__list_all(false);
253 pr_debug("Failed to get buildids: -%d\n", errno);
257 strlist__for_each_entry(pos, list) {
258 buf = build_id_cache__origname(pos->s);
259 err = build_id_cache__remove_s(pos->s);
260 pr_debug("Removing %s (%s): %s\n", buf, pos->s,
261 err ? "FAIL" : "Ok");
266 strlist__delete(list);
268 pr_debug("Purged all: %s\n", err ? "FAIL" : "Ok");
272 static bool dso__missing_buildid_cache(struct dso *dso, int parm __maybe_unused)
274 char filename[PATH_MAX];
275 u8 build_id[BUILD_ID_SIZE];
277 if (dso__build_id_filename(dso, filename, sizeof(filename), false) &&
278 filename__read_build_id(filename, build_id,
279 sizeof(build_id)) != sizeof(build_id)) {
283 pr_warning("Problems with %s file, consider removing it from the cache\n",
285 } else if (memcmp(dso->build_id, build_id, sizeof(dso->build_id))) {
286 pr_warning("Problems with %s file, consider removing it from the cache\n",
293 static int build_id_cache__fprintf_missing(struct perf_session *session, FILE *fp)
295 perf_session__fprintf_dsos_buildid(session, fp, dso__missing_buildid_cache, 0);
299 static int build_id_cache__update_file(const char *filename, struct nsinfo *nsi)
301 u8 build_id[BUILD_ID_SIZE];
302 char sbuild_id[SBUILD_ID_SIZE];
307 nsinfo__mountns_enter(nsi, &nsc);
308 err = filename__read_build_id(filename, &build_id, sizeof(build_id));
309 nsinfo__mountns_exit(&nsc);
311 pr_debug("Couldn't read a build-id in %s\n", filename);
316 build_id__sprintf(build_id, sizeof(build_id), sbuild_id);
317 if (build_id_cache__cached(sbuild_id))
318 err = build_id_cache__remove_s(sbuild_id);
321 err = build_id_cache__add_s(sbuild_id, filename, nsi, false,
324 pr_debug("Updating %s %s: %s\n", sbuild_id, filename,
325 err ? "FAIL" : "Ok");
330 static int build_id_cache__show_all(void)
332 struct strlist *bidlist;
336 bidlist = build_id_cache__list_all(true);
338 pr_debug("Failed to get buildids: -%d\n", errno);
341 strlist__for_each_entry(nd, bidlist) {
342 buf = build_id_cache__origname(nd->s);
343 fprintf(stdout, "%s %s\n", nd->s, buf);
346 strlist__delete(bidlist);
350 int cmd_buildid_cache(int argc, const char **argv)
352 struct strlist *list;
353 struct str_node *pos;
357 bool list_files = false;
358 bool opts_flag = false;
359 bool purge_all = false;
360 char const *add_name_list_str = NULL,
361 *remove_name_list_str = NULL,
362 *purge_name_list_str = NULL,
363 *missing_filename = NULL,
364 *update_name_list_str = NULL,
365 *kcore_filename = NULL;
366 char sbuf[STRERR_BUFSIZE];
368 struct perf_data data = {
369 .mode = PERF_DATA_MODE_READ,
371 struct perf_session *session = NULL;
372 struct nsinfo *nsi = NULL;
374 const struct option buildid_cache_options[] = {
375 OPT_STRING('a', "add", &add_name_list_str,
376 "file list", "file(s) to add"),
377 OPT_STRING('k', "kcore", &kcore_filename,
378 "file", "kcore file to add"),
379 OPT_STRING('r', "remove", &remove_name_list_str, "file list",
380 "file(s) to remove"),
381 OPT_STRING('p', "purge", &purge_name_list_str, "file list",
382 "file(s) to remove (remove old caches too)"),
383 OPT_BOOLEAN('P', "purge-all", &purge_all, "purge all cached files"),
384 OPT_BOOLEAN('l', "list", &list_files, "list all cached files"),
385 OPT_STRING('M', "missing", &missing_filename, "file",
386 "to find missing build ids in the cache"),
387 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
388 OPT_STRING('u', "update", &update_name_list_str, "file list",
389 "file(s) to update"),
390 OPT_INCR('v', "verbose", &verbose, "be more verbose"),
391 OPT_INTEGER(0, "target-ns", &ns_id, "target pid for namespace context"),
394 const char * const buildid_cache_usage[] = {
395 "perf buildid-cache [<options>]",
399 argc = parse_options(argc, argv, buildid_cache_options,
400 buildid_cache_usage, 0);
402 opts_flag = add_name_list_str || kcore_filename ||
403 remove_name_list_str || purge_name_list_str ||
404 missing_filename || update_name_list_str ||
407 if (argc || !(list_files || opts_flag))
408 usage_with_options(buildid_cache_usage, buildid_cache_options);
410 /* -l is exclusive. It can not be used with other options. */
411 if (list_files && opts_flag) {
412 usage_with_options_msg(buildid_cache_usage,
413 buildid_cache_options, "-l is exclusive.\n");
417 nsi = nsinfo__new(ns_id);
419 if (missing_filename) {
420 data.path = missing_filename;
423 session = perf_session__new(&data, false, NULL);
428 if (symbol__init(session ? &session->header.env : NULL) < 0)
434 ret = build_id_cache__show_all();
438 if (add_name_list_str) {
439 list = strlist__new(add_name_list_str, NULL);
441 strlist__for_each_entry(pos, list)
442 if (build_id_cache__add_file(pos->s, nsi)) {
443 if (errno == EEXIST) {
444 pr_debug("%s already in the cache\n",
448 pr_warning("Couldn't add %s: %s\n",
449 pos->s, str_error_r(errno, sbuf, sizeof(sbuf)));
452 strlist__delete(list);
456 if (remove_name_list_str) {
457 list = strlist__new(remove_name_list_str, NULL);
459 strlist__for_each_entry(pos, list)
460 if (build_id_cache__remove_file(pos->s, nsi)) {
461 if (errno == ENOENT) {
462 pr_debug("%s wasn't in the cache\n",
466 pr_warning("Couldn't remove %s: %s\n",
467 pos->s, str_error_r(errno, sbuf, sizeof(sbuf)));
470 strlist__delete(list);
474 if (purge_name_list_str) {
475 list = strlist__new(purge_name_list_str, NULL);
477 strlist__for_each_entry(pos, list)
478 if (build_id_cache__purge_path(pos->s, nsi)) {
479 if (errno == ENOENT) {
480 pr_debug("%s wasn't in the cache\n",
484 pr_warning("Couldn't remove %s: %s\n",
485 pos->s, str_error_r(errno, sbuf, sizeof(sbuf)));
488 strlist__delete(list);
493 if (build_id_cache__purge_all()) {
494 pr_warning("Couldn't remove some caches. Error: %s.\n",
495 str_error_r(errno, sbuf, sizeof(sbuf)));
499 if (missing_filename)
500 ret = build_id_cache__fprintf_missing(session, stdout);
502 if (update_name_list_str) {
503 list = strlist__new(update_name_list_str, NULL);
505 strlist__for_each_entry(pos, list)
506 if (build_id_cache__update_file(pos->s, nsi)) {
507 if (errno == ENOENT) {
508 pr_debug("%s wasn't in the cache\n",
512 pr_warning("Couldn't update %s: %s\n",
513 pos->s, str_error_r(errno, sbuf, sizeof(sbuf)));
516 strlist__delete(list);
520 if (kcore_filename && build_id_cache__add_kcore(kcore_filename, force))
521 pr_warning("Couldn't add %s\n", kcore_filename);
524 perf_session__delete(session);