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