0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <sys/types.h>
0011 #include <sys/time.h>
0012 #include <time.h>
0013 #include <dirent.h>
0014 #include <errno.h>
0015 #include <unistd.h>
0016 #include "builtin.h"
0017 #include "namespaces.h"
0018 #include "util/debug.h"
0019 #include "util/header.h"
0020 #include <subcmd/pager.h>
0021 #include <subcmd/parse-options.h>
0022 #include "util/strlist.h"
0023 #include "util/build-id.h"
0024 #include "util/session.h"
0025 #include "util/dso.h"
0026 #include "util/symbol.h"
0027 #include "util/time-utils.h"
0028 #include "util/util.h"
0029 #include "util/probe-file.h"
0030 #include "util/config.h"
0031 #include <linux/string.h>
0032 #include <linux/err.h>
0033
0034 static int build_id_cache__kcore_buildid(const char *proc_dir, char *sbuildid)
0035 {
0036 char root_dir[PATH_MAX];
0037 char *p;
0038
0039 strlcpy(root_dir, proc_dir, sizeof(root_dir));
0040
0041 p = strrchr(root_dir, '/');
0042 if (!p)
0043 return -1;
0044 *p = '\0';
0045 return sysfs__sprintf_build_id(root_dir, sbuildid);
0046 }
0047
0048 static int build_id_cache__kcore_dir(char *dir, size_t sz)
0049 {
0050 return fetch_current_timestamp(dir, sz);
0051 }
0052
0053 static bool same_kallsyms_reloc(const char *from_dir, char *to_dir)
0054 {
0055 char from[PATH_MAX];
0056 char to[PATH_MAX];
0057 const char *name;
0058 u64 addr1 = 0, addr2 = 0;
0059 int i, err = -1;
0060
0061 scnprintf(from, sizeof(from), "%s/kallsyms", from_dir);
0062 scnprintf(to, sizeof(to), "%s/kallsyms", to_dir);
0063
0064 for (i = 0; (name = ref_reloc_sym_names[i]) != NULL; i++) {
0065 err = kallsyms__get_function_start(from, name, &addr1);
0066 if (!err)
0067 break;
0068 }
0069
0070 if (err)
0071 return false;
0072
0073 if (kallsyms__get_function_start(to, name, &addr2))
0074 return false;
0075
0076 return addr1 == addr2;
0077 }
0078
0079 static int build_id_cache__kcore_existing(const char *from_dir, char *to_dir,
0080 size_t to_dir_sz)
0081 {
0082 char from[PATH_MAX];
0083 char to[PATH_MAX];
0084 char to_subdir[PATH_MAX];
0085 struct dirent *dent;
0086 int ret = -1;
0087 DIR *d;
0088
0089 d = opendir(to_dir);
0090 if (!d)
0091 return -1;
0092
0093 scnprintf(from, sizeof(from), "%s/modules", from_dir);
0094
0095 while (1) {
0096 dent = readdir(d);
0097 if (!dent)
0098 break;
0099 if (dent->d_type != DT_DIR)
0100 continue;
0101 scnprintf(to, sizeof(to), "%s/%s/modules", to_dir,
0102 dent->d_name);
0103 scnprintf(to_subdir, sizeof(to_subdir), "%s/%s",
0104 to_dir, dent->d_name);
0105 if (!compare_proc_modules(from, to) &&
0106 same_kallsyms_reloc(from_dir, to_subdir)) {
0107 strlcpy(to_dir, to_subdir, to_dir_sz);
0108 ret = 0;
0109 break;
0110 }
0111 }
0112
0113 closedir(d);
0114
0115 return ret;
0116 }
0117
0118 static int build_id_cache__add_kcore(const char *filename, bool force)
0119 {
0120 char dir[32], sbuildid[SBUILD_ID_SIZE];
0121 char from_dir[PATH_MAX], to_dir[PATH_MAX];
0122 char *p;
0123
0124 strlcpy(from_dir, filename, sizeof(from_dir));
0125
0126 p = strrchr(from_dir, '/');
0127 if (!p || strcmp(p + 1, "kcore"))
0128 return -1;
0129 *p = '\0';
0130
0131 if (build_id_cache__kcore_buildid(from_dir, sbuildid) < 0)
0132 return -1;
0133
0134 scnprintf(to_dir, sizeof(to_dir), "%s/%s/%s",
0135 buildid_dir, DSO__NAME_KCORE, sbuildid);
0136
0137 if (!force &&
0138 !build_id_cache__kcore_existing(from_dir, to_dir, sizeof(to_dir))) {
0139 pr_debug("same kcore found in %s\n", to_dir);
0140 return 0;
0141 }
0142
0143 if (build_id_cache__kcore_dir(dir, sizeof(dir)))
0144 return -1;
0145
0146 scnprintf(to_dir, sizeof(to_dir), "%s/%s/%s/%s",
0147 buildid_dir, DSO__NAME_KCORE, sbuildid, dir);
0148
0149 if (mkdir_p(to_dir, 0755))
0150 return -1;
0151
0152 if (kcore_copy(from_dir, to_dir)) {
0153
0154 if (!rmdir(to_dir)) {
0155 p = strrchr(to_dir, '/');
0156 if (p)
0157 *p = '\0';
0158
0159 if (!rmdir(to_dir)) {
0160 p = strrchr(to_dir, '/');
0161 if (p)
0162 *p = '\0';
0163
0164 rmdir(to_dir);
0165 }
0166 }
0167 return -1;
0168 }
0169
0170 pr_debug("kcore added to build-id cache directory %s\n", to_dir);
0171
0172 return 0;
0173 }
0174
0175 static int build_id_cache__add_file(const char *filename, struct nsinfo *nsi)
0176 {
0177 char sbuild_id[SBUILD_ID_SIZE];
0178 struct build_id bid;
0179 int err;
0180 struct nscookie nsc;
0181
0182 nsinfo__mountns_enter(nsi, &nsc);
0183 err = filename__read_build_id(filename, &bid);
0184 nsinfo__mountns_exit(&nsc);
0185 if (err < 0) {
0186 pr_debug("Couldn't read a build-id in %s\n", filename);
0187 return -1;
0188 }
0189
0190 build_id__sprintf(&bid, sbuild_id);
0191 err = build_id_cache__add_s(sbuild_id, filename, nsi,
0192 false, false);
0193 pr_debug("Adding %s %s: %s\n", sbuild_id, filename,
0194 err ? "FAIL" : "Ok");
0195 return err;
0196 }
0197
0198 static int build_id_cache__remove_file(const char *filename, struct nsinfo *nsi)
0199 {
0200 char sbuild_id[SBUILD_ID_SIZE];
0201 struct build_id bid;
0202 struct nscookie nsc;
0203
0204 int err;
0205
0206 nsinfo__mountns_enter(nsi, &nsc);
0207 err = filename__read_build_id(filename, &bid);
0208 nsinfo__mountns_exit(&nsc);
0209 if (err < 0) {
0210 pr_debug("Couldn't read a build-id in %s\n", filename);
0211 return -1;
0212 }
0213
0214 build_id__sprintf(&bid, sbuild_id);
0215 err = build_id_cache__remove_s(sbuild_id);
0216 pr_debug("Removing %s %s: %s\n", sbuild_id, filename,
0217 err ? "FAIL" : "Ok");
0218
0219 return err;
0220 }
0221
0222 static int build_id_cache__purge_path(const char *pathname, struct nsinfo *nsi)
0223 {
0224 struct strlist *list;
0225 struct str_node *pos;
0226 int err;
0227
0228 err = build_id_cache__list_build_ids(pathname, nsi, &list);
0229 if (err)
0230 goto out;
0231
0232 strlist__for_each_entry(pos, list) {
0233 err = build_id_cache__remove_s(pos->s);
0234 pr_debug("Removing %s %s: %s\n", pos->s, pathname,
0235 err ? "FAIL" : "Ok");
0236 if (err)
0237 break;
0238 }
0239 strlist__delete(list);
0240
0241 out:
0242 pr_debug("Purging %s: %s\n", pathname, err ? "FAIL" : "Ok");
0243
0244 return err;
0245 }
0246
0247 static int build_id_cache__purge_all(void)
0248 {
0249 struct strlist *list;
0250 struct str_node *pos;
0251 int err = 0;
0252 char *buf;
0253
0254 list = build_id_cache__list_all(false);
0255 if (!list) {
0256 pr_debug("Failed to get buildids: -%d\n", errno);
0257 return -EINVAL;
0258 }
0259
0260 strlist__for_each_entry(pos, list) {
0261 buf = build_id_cache__origname(pos->s);
0262 err = build_id_cache__remove_s(pos->s);
0263 pr_debug("Removing %s (%s): %s\n", buf, pos->s,
0264 err ? "FAIL" : "Ok");
0265 free(buf);
0266 if (err)
0267 break;
0268 }
0269 strlist__delete(list);
0270
0271 pr_debug("Purged all: %s\n", err ? "FAIL" : "Ok");
0272 return err;
0273 }
0274
0275 static bool dso__missing_buildid_cache(struct dso *dso, int parm __maybe_unused)
0276 {
0277 char filename[PATH_MAX];
0278 struct build_id bid;
0279
0280 if (dso__build_id_filename(dso, filename, sizeof(filename), false) &&
0281 filename__read_build_id(filename, &bid) == -1) {
0282 if (errno == ENOENT)
0283 return false;
0284
0285 pr_warning("Problems with %s file, consider removing it from the cache\n",
0286 filename);
0287 } else if (memcmp(dso->bid.data, bid.data, bid.size)) {
0288 pr_warning("Problems with %s file, consider removing it from the cache\n",
0289 filename);
0290 }
0291
0292 return true;
0293 }
0294
0295 static int build_id_cache__fprintf_missing(struct perf_session *session, FILE *fp)
0296 {
0297 perf_session__fprintf_dsos_buildid(session, fp, dso__missing_buildid_cache, 0);
0298 return 0;
0299 }
0300
0301 static int build_id_cache__update_file(const char *filename, struct nsinfo *nsi)
0302 {
0303 char sbuild_id[SBUILD_ID_SIZE];
0304 struct build_id bid;
0305 struct nscookie nsc;
0306
0307 int err;
0308
0309 nsinfo__mountns_enter(nsi, &nsc);
0310 err = filename__read_build_id(filename, &bid);
0311 nsinfo__mountns_exit(&nsc);
0312 if (err < 0) {
0313 pr_debug("Couldn't read a build-id in %s\n", filename);
0314 return -1;
0315 }
0316 err = 0;
0317
0318 build_id__sprintf(&bid, sbuild_id);
0319 if (build_id_cache__cached(sbuild_id))
0320 err = build_id_cache__remove_s(sbuild_id);
0321
0322 if (!err)
0323 err = build_id_cache__add_s(sbuild_id, filename, nsi, false,
0324 false);
0325
0326 pr_debug("Updating %s %s: %s\n", sbuild_id, filename,
0327 err ? "FAIL" : "Ok");
0328
0329 return err;
0330 }
0331
0332 static int build_id_cache__show_all(void)
0333 {
0334 struct strlist *bidlist;
0335 struct str_node *nd;
0336 char *buf;
0337
0338 bidlist = build_id_cache__list_all(true);
0339 if (!bidlist) {
0340 pr_debug("Failed to get buildids: -%d\n", errno);
0341 return -1;
0342 }
0343 strlist__for_each_entry(nd, bidlist) {
0344 buf = build_id_cache__origname(nd->s);
0345 fprintf(stdout, "%s %s\n", nd->s, buf);
0346 free(buf);
0347 }
0348 strlist__delete(bidlist);
0349 return 0;
0350 }
0351
0352 static int perf_buildid_cache_config(const char *var, const char *value, void *cb)
0353 {
0354 struct perf_debuginfod *di = cb;
0355
0356 if (!strcmp(var, "buildid-cache.debuginfod")) {
0357 di->urls = strdup(value);
0358 if (!di->urls)
0359 return -ENOMEM;
0360 di->set = true;
0361 }
0362
0363 return 0;
0364 }
0365
0366 int cmd_buildid_cache(int argc, const char **argv)
0367 {
0368 struct strlist *list;
0369 struct str_node *pos;
0370 int ret, ns_id = -1;
0371 bool force = false;
0372 bool list_files = false;
0373 bool opts_flag = false;
0374 bool purge_all = false;
0375 char const *add_name_list_str = NULL,
0376 *remove_name_list_str = NULL,
0377 *purge_name_list_str = NULL,
0378 *missing_filename = NULL,
0379 *update_name_list_str = NULL,
0380 *kcore_filename = NULL;
0381 struct perf_debuginfod debuginfod = { };
0382 char sbuf[STRERR_BUFSIZE];
0383
0384 struct perf_data data = {
0385 .mode = PERF_DATA_MODE_READ,
0386 };
0387 struct perf_session *session = NULL;
0388 struct nsinfo *nsi = NULL;
0389
0390 const struct option buildid_cache_options[] = {
0391 OPT_STRING('a', "add", &add_name_list_str,
0392 "file list", "file(s) to add"),
0393 OPT_STRING('k', "kcore", &kcore_filename,
0394 "file", "kcore file to add"),
0395 OPT_STRING('r', "remove", &remove_name_list_str, "file list",
0396 "file(s) to remove"),
0397 OPT_STRING('p', "purge", &purge_name_list_str, "file list",
0398 "file(s) to remove (remove old caches too)"),
0399 OPT_BOOLEAN('P', "purge-all", &purge_all, "purge all cached files"),
0400 OPT_BOOLEAN('l', "list", &list_files, "list all cached files"),
0401 OPT_STRING('M', "missing", &missing_filename, "file",
0402 "to find missing build ids in the cache"),
0403 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
0404 OPT_STRING('u', "update", &update_name_list_str, "file list",
0405 "file(s) to update"),
0406 OPT_STRING_OPTARG_SET(0, "debuginfod", &debuginfod.urls,
0407 &debuginfod.set, "debuginfod urls",
0408 "Enable debuginfod data retrieval from DEBUGINFOD_URLS or specified urls",
0409 "system"),
0410 OPT_INCR('v', "verbose", &verbose, "be more verbose"),
0411 OPT_INTEGER(0, "target-ns", &ns_id, "target pid for namespace context"),
0412 OPT_END()
0413 };
0414 const char * const buildid_cache_usage[] = {
0415 "perf buildid-cache [<options>]",
0416 NULL
0417 };
0418
0419 ret = perf_config(perf_buildid_cache_config, &debuginfod);
0420 if (ret)
0421 return ret;
0422
0423 argc = parse_options(argc, argv, buildid_cache_options,
0424 buildid_cache_usage, 0);
0425
0426 opts_flag = add_name_list_str || kcore_filename ||
0427 remove_name_list_str || purge_name_list_str ||
0428 missing_filename || update_name_list_str ||
0429 purge_all;
0430
0431 if (argc || !(list_files || opts_flag))
0432 usage_with_options(buildid_cache_usage, buildid_cache_options);
0433
0434 perf_debuginfod_setup(&debuginfod);
0435
0436
0437 if (list_files && opts_flag) {
0438 usage_with_options_msg(buildid_cache_usage,
0439 buildid_cache_options, "-l is exclusive.\n");
0440 }
0441
0442 if (ns_id > 0)
0443 nsi = nsinfo__new(ns_id);
0444
0445 if (missing_filename) {
0446 data.path = missing_filename;
0447 data.force = force;
0448
0449 session = perf_session__new(&data, NULL);
0450 if (IS_ERR(session))
0451 return PTR_ERR(session);
0452 }
0453
0454 if (symbol__init(session ? &session->header.env : NULL) < 0)
0455 goto out;
0456
0457 setup_pager();
0458
0459 if (list_files) {
0460 ret = build_id_cache__show_all();
0461 goto out;
0462 }
0463
0464 if (add_name_list_str) {
0465 list = strlist__new(add_name_list_str, NULL);
0466 if (list) {
0467 strlist__for_each_entry(pos, list)
0468 if (build_id_cache__add_file(pos->s, nsi)) {
0469 if (errno == EEXIST) {
0470 pr_debug("%s already in the cache\n",
0471 pos->s);
0472 continue;
0473 }
0474 pr_warning("Couldn't add %s: %s\n",
0475 pos->s, str_error_r(errno, sbuf, sizeof(sbuf)));
0476 }
0477
0478 strlist__delete(list);
0479 }
0480 }
0481
0482 if (remove_name_list_str) {
0483 list = strlist__new(remove_name_list_str, NULL);
0484 if (list) {
0485 strlist__for_each_entry(pos, list)
0486 if (build_id_cache__remove_file(pos->s, nsi)) {
0487 if (errno == ENOENT) {
0488 pr_debug("%s wasn't in the cache\n",
0489 pos->s);
0490 continue;
0491 }
0492 pr_warning("Couldn't remove %s: %s\n",
0493 pos->s, str_error_r(errno, sbuf, sizeof(sbuf)));
0494 }
0495
0496 strlist__delete(list);
0497 }
0498 }
0499
0500 if (purge_name_list_str) {
0501 list = strlist__new(purge_name_list_str, NULL);
0502 if (list) {
0503 strlist__for_each_entry(pos, list)
0504 if (build_id_cache__purge_path(pos->s, nsi)) {
0505 if (errno == ENOENT) {
0506 pr_debug("%s wasn't in the cache\n",
0507 pos->s);
0508 continue;
0509 }
0510 pr_warning("Couldn't remove %s: %s\n",
0511 pos->s, str_error_r(errno, sbuf, sizeof(sbuf)));
0512 }
0513
0514 strlist__delete(list);
0515 }
0516 }
0517
0518 if (purge_all) {
0519 if (build_id_cache__purge_all()) {
0520 pr_warning("Couldn't remove some caches. Error: %s.\n",
0521 str_error_r(errno, sbuf, sizeof(sbuf)));
0522 }
0523 }
0524
0525 if (missing_filename)
0526 ret = build_id_cache__fprintf_missing(session, stdout);
0527
0528 if (update_name_list_str) {
0529 list = strlist__new(update_name_list_str, NULL);
0530 if (list) {
0531 strlist__for_each_entry(pos, list)
0532 if (build_id_cache__update_file(pos->s, nsi)) {
0533 if (errno == ENOENT) {
0534 pr_debug("%s wasn't in the cache\n",
0535 pos->s);
0536 continue;
0537 }
0538 pr_warning("Couldn't update %s: %s\n",
0539 pos->s, str_error_r(errno, sbuf, sizeof(sbuf)));
0540 }
0541
0542 strlist__delete(list);
0543 }
0544 }
0545
0546 if (kcore_filename && build_id_cache__add_kcore(kcore_filename, force))
0547 pr_warning("Couldn't add %s\n", kcore_filename);
0548
0549 out:
0550 perf_session__delete(session);
0551 nsinfo__zput(nsi);
0552
0553 return ret;
0554 }