xref: /linux/tools/perf/util/util.c (revision 19a9ed115fda95317c98bef0c716ea8412cd8ce0)
1 // SPDX-License-Identifier: GPL-2.0
2 #include "perf.h"
3 #include "util.h"
4 #include "debug.h"
5 #include "event.h"
6 #include <api/fs/fs.h>
7 #include <sys/stat.h>
8 #include <sys/utsname.h>
9 #include <dirent.h>
10 #include <fcntl.h>
11 #include <inttypes.h>
12 #include <signal.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <errno.h>
17 #include <limits.h>
18 #include <linux/capability.h>
19 #include <linux/kernel.h>
20 #include <linux/log2.h>
21 #include <linux/time64.h>
22 #include <linux/overflow.h>
23 #include <unistd.h>
24 #include "cap.h"
25 #include "strlist.h"
26 #include "string2.h"
27 
28 /*
29  * XXX We need to find a better place for these things...
30  */
31 
32 const char *input_name;
33 
34 bool perf_singlethreaded = true;
35 
36 void perf_set_singlethreaded(void)
37 {
38 	perf_singlethreaded = true;
39 }
40 
41 void perf_set_multithreaded(void)
42 {
43 	perf_singlethreaded = false;
44 }
45 
46 int sysctl_perf_event_max_stack = PERF_MAX_STACK_DEPTH;
47 int sysctl_perf_event_max_contexts_per_stack = PERF_MAX_CONTEXTS_PER_STACK;
48 
49 int sysctl__max_stack(void)
50 {
51 	int value;
52 
53 	if (sysctl__read_int("kernel/perf_event_max_stack", &value) == 0)
54 		sysctl_perf_event_max_stack = value;
55 
56 	if (sysctl__read_int("kernel/perf_event_max_contexts_per_stack", &value) == 0)
57 		sysctl_perf_event_max_contexts_per_stack = value;
58 
59 	return sysctl_perf_event_max_stack;
60 }
61 
62 bool sysctl__nmi_watchdog_enabled(void)
63 {
64 	static bool cached;
65 	static bool nmi_watchdog;
66 	int value;
67 
68 	if (cached)
69 		return nmi_watchdog;
70 
71 	if (sysctl__read_int("kernel/nmi_watchdog", &value) < 0)
72 		return false;
73 
74 	nmi_watchdog = (value > 0) ? true : false;
75 	cached = true;
76 
77 	return nmi_watchdog;
78 }
79 
80 bool exclude_GH_default;
81 
82 bool perf_host  = true;
83 bool perf_guest = false;
84 
85 void event_attr_init(struct perf_event_attr *attr)
86 {
87 	/* to capture ABI version */
88 	attr->size = sizeof(*attr);
89 
90 	if (!exclude_GH_default)
91 		return;
92 
93 	if (!perf_host)
94 		attr->exclude_host  = 1;
95 	if (!perf_guest)
96 		attr->exclude_guest = 1;
97 }
98 
99 int mkdir_p(char *path, mode_t mode)
100 {
101 	struct stat st;
102 	int err;
103 	char *d = path;
104 
105 	if (*d != '/')
106 		return -1;
107 
108 	if (stat(path, &st) == 0)
109 		return 0;
110 
111 	while (*++d == '/');
112 
113 	while ((d = strchr(d, '/'))) {
114 		*d = '\0';
115 		err = stat(path, &st) && mkdir(path, mode);
116 		*d++ = '/';
117 		if (err)
118 			return -1;
119 		while (*d == '/')
120 			++d;
121 	}
122 	return (stat(path, &st) && mkdir(path, mode)) ? -1 : 0;
123 }
124 
125 static bool match_pat(char *file, const char **pat)
126 {
127 	int i = 0;
128 
129 	if (!pat)
130 		return true;
131 
132 	while (pat[i]) {
133 		if (strglobmatch(file, pat[i]))
134 			return true;
135 
136 		i++;
137 	}
138 
139 	return false;
140 }
141 
142 /*
143  * The depth specify how deep the removal will go.
144  * 0       - will remove only files under the 'path' directory
145  * 1 .. x  - will dive in x-level deep under the 'path' directory
146  *
147  * If specified the pat is array of string patterns ended with NULL,
148  * which are checked upon every file/directory found. Only matching
149  * ones are removed.
150  *
151  * The function returns:
152  *    0 on success
153  *   -1 on removal failure with errno set
154  *   -2 on pattern failure
155  */
156 static int rm_rf_depth_pat(const char *path, int depth, const char **pat)
157 {
158 	DIR *dir;
159 	int ret;
160 	struct dirent *d;
161 	char namebuf[PATH_MAX];
162 	struct stat statbuf;
163 
164 	/* Do not fail if there's no file. */
165 	ret = lstat(path, &statbuf);
166 	if (ret)
167 		return 0;
168 
169 	/* Try to remove any file we get. */
170 	if (!(statbuf.st_mode & S_IFDIR))
171 		return unlink(path);
172 
173 	/* We have directory in path. */
174 	dir = opendir(path);
175 	if (dir == NULL)
176 		return -1;
177 
178 	while ((d = readdir(dir)) != NULL && !ret) {
179 
180 		if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
181 			continue;
182 
183 		if (!match_pat(d->d_name, pat)) {
184 			ret =  -2;
185 			break;
186 		}
187 
188 		scnprintf(namebuf, sizeof(namebuf), "%s/%s",
189 			  path, d->d_name);
190 
191 		/* We have to check symbolic link itself */
192 		ret = lstat(namebuf, &statbuf);
193 		if (ret < 0) {
194 			pr_debug("stat failed: %s\n", namebuf);
195 			break;
196 		}
197 
198 		if (S_ISDIR(statbuf.st_mode))
199 			ret = depth ? rm_rf_depth_pat(namebuf, depth - 1, pat) : 0;
200 		else
201 			ret = unlink(namebuf);
202 	}
203 	closedir(dir);
204 
205 	if (ret < 0)
206 		return ret;
207 
208 	return rmdir(path);
209 }
210 
211 static int rm_rf_a_kcore_dir(const char *path, const char *name)
212 {
213 	char kcore_dir_path[PATH_MAX];
214 	const char *pat[] = {
215 		"kcore",
216 		"kallsyms",
217 		"modules",
218 		NULL,
219 	};
220 
221 	snprintf(kcore_dir_path, sizeof(kcore_dir_path), "%s/%s", path, name);
222 
223 	return rm_rf_depth_pat(kcore_dir_path, 0, pat);
224 }
225 
226 static bool kcore_dir_filter(const char *name __maybe_unused, struct dirent *d)
227 {
228 	const char *pat[] = {
229 		"kcore_dir",
230 		"kcore_dir__[1-9]*",
231 		NULL,
232 	};
233 
234 	return match_pat(d->d_name, pat);
235 }
236 
237 static int rm_rf_kcore_dir(const char *path)
238 {
239 	struct strlist *kcore_dirs;
240 	struct str_node *nd;
241 	int ret;
242 
243 	kcore_dirs = lsdir(path, kcore_dir_filter);
244 
245 	if (!kcore_dirs)
246 		return 0;
247 
248 	strlist__for_each_entry(nd, kcore_dirs) {
249 		ret = rm_rf_a_kcore_dir(path, nd->s);
250 		if (ret)
251 			return ret;
252 	}
253 
254 	strlist__delete(kcore_dirs);
255 
256 	return 0;
257 }
258 
259 void cpumask_to_cpulist(char *cpumask, char *cpulist)
260 {
261 	int i, j, bm_size, nbits;
262 	int len = strlen(cpumask);
263 	unsigned long *bm;
264 	char cpus[MAX_NR_CPUS];
265 
266 	for (i = 0; i < len; i++) {
267 		if (cpumask[i] == ',') {
268 			for (j = i; j < len; j++)
269 				cpumask[j] = cpumask[j + 1];
270 		}
271 	}
272 
273 	len = strlen(cpumask);
274 	bm_size = (len + 15) / 16;
275 	nbits = bm_size * 64;
276 	if (nbits <= 0)
277 		return;
278 
279 	bm = calloc(bm_size, sizeof(unsigned long));
280 	if (!bm)
281 		goto free_bm;
282 
283 	for (i = 0; i < bm_size; i++) {
284 		char blk[17];
285 		int blklen = len > 16 ? 16 : len;
286 
287 		strncpy(blk, cpumask + len - blklen, blklen);
288 		blk[blklen] = '\0';
289 		bm[i] = strtoul(blk, NULL, 16);
290 		cpumask[len - blklen] = '\0';
291 		len = strlen(cpumask);
292 	}
293 
294 	bitmap_scnprintf(bm, nbits, cpus, sizeof(cpus));
295 	strcpy(cpulist, cpus);
296 
297 free_bm:
298 	free(bm);
299 }
300 
301 void print_separator2(int pre_dash_cnt, const char *s, int post_dash_cnt)
302 {
303 	printf("%.*s%s%.*s\n", pre_dash_cnt, graph_dotted_line, s, post_dash_cnt,
304 	       graph_dotted_line);
305 }
306 
307 int rm_rf_perf_data(const char *path)
308 {
309 	const char *pat[] = {
310 		"data",
311 		"data.*",
312 		NULL,
313 	};
314 
315 	rm_rf_kcore_dir(path);
316 
317 	return rm_rf_depth_pat(path, 0, pat);
318 }
319 
320 int rm_rf(const char *path)
321 {
322 	return rm_rf_depth_pat(path, INT_MAX, NULL);
323 }
324 
325 /* A filter which removes dot files */
326 bool lsdir_no_dot_filter(const char *name __maybe_unused, struct dirent *d)
327 {
328 	return d->d_name[0] != '.';
329 }
330 
331 /* lsdir reads a directory and store it in strlist */
332 struct strlist *lsdir(const char *name,
333 		      bool (*filter)(const char *, struct dirent *))
334 {
335 	struct strlist *list = NULL;
336 	DIR *dir;
337 	struct dirent *d;
338 
339 	dir = opendir(name);
340 	if (!dir)
341 		return NULL;
342 
343 	list = strlist__new(NULL, NULL);
344 	if (!list) {
345 		errno = ENOMEM;
346 		goto out;
347 	}
348 
349 	while ((d = readdir(dir)) != NULL) {
350 		if (!filter || filter(name, d))
351 			strlist__add(list, d->d_name);
352 	}
353 
354 out:
355 	closedir(dir);
356 	return list;
357 }
358 
359 size_t hex_width(u64 v)
360 {
361 	size_t n = 1;
362 
363 	while ((v >>= 4))
364 		++n;
365 
366 	return n;
367 }
368 
369 int perf_event_paranoid(void)
370 {
371 	int value;
372 
373 	if (sysctl__read_int("kernel/perf_event_paranoid", &value))
374 		return INT_MAX;
375 
376 	return value;
377 }
378 
379 bool perf_event_paranoid_check(int max_level)
380 {
381 	bool used_root;
382 
383 	if (perf_cap__capable(CAP_SYS_ADMIN, &used_root))
384 		return true;
385 
386 	if (!used_root && perf_cap__capable(CAP_PERFMON, &used_root))
387 		return true;
388 
389 	return perf_event_paranoid() <= max_level;
390 }
391 
392 int perf_tip(char **strp, const char *dirpath)
393 {
394 	struct strlist *tips;
395 	struct str_node *node;
396 	struct strlist_config conf = {
397 		.dirname = dirpath,
398 		.file_only = true,
399 	};
400 	int ret = 0;
401 
402 	*strp = NULL;
403 	tips = strlist__new("tips.txt", &conf);
404 	if (tips == NULL)
405 		return -errno;
406 
407 	if (strlist__nr_entries(tips) == 0)
408 		goto out;
409 
410 	node = strlist__entry(tips, random() % strlist__nr_entries(tips));
411 	if (asprintf(strp, "Tip: %s", node->s) < 0)
412 		ret = -ENOMEM;
413 
414 out:
415 	strlist__delete(tips);
416 
417 	return ret;
418 }
419 
420 char *perf_exe(char *buf, int len)
421 {
422 	int n = readlink("/proc/self/exe", buf, len);
423 	if (n > 0) {
424 		buf[n] = 0;
425 		return buf;
426 	}
427 	return strcpy(buf, "perf");
428 }
429 
430 void perf_debuginfod_setup(struct perf_debuginfod *di)
431 {
432 	/*
433 	 * By default '!di->set' we clear DEBUGINFOD_URLS, so debuginfod
434 	 * processing is not triggered, otherwise we set it to 'di->urls'
435 	 * value. If 'di->urls' is "system" we keep DEBUGINFOD_URLS value.
436 	 */
437 	if (!di->set)
438 		setenv("DEBUGINFOD_URLS", "", 1);
439 	else if (di->urls && strcmp(di->urls, "system"))
440 		setenv("DEBUGINFOD_URLS", di->urls, 1);
441 
442 	pr_debug("DEBUGINFOD_URLS=%s\n", getenv("DEBUGINFOD_URLS"));
443 
444 #ifndef HAVE_DEBUGINFOD_SUPPORT
445 	if (di->set)
446 		pr_warning("WARNING: debuginfod support requested, but perf is not built with it\n");
447 #endif
448 }
449 
450 /*
451  * Return a new filename prepended with task's root directory if it's in
452  * a chroot.  Callers should free the returned string.
453  */
454 char *filename_with_chroot(int pid, const char *filename)
455 {
456 	char buf[PATH_MAX];
457 	char proc_root[32];
458 	char *new_name = NULL;
459 	int ret;
460 
461 	scnprintf(proc_root, sizeof(proc_root), "/proc/%d/root", pid);
462 	ret = readlink(proc_root, buf, sizeof(buf) - 1);
463 	if (ret <= 0)
464 		return NULL;
465 
466 	/* readlink(2) does not append a null byte to buf */
467 	buf[ret] = '\0';
468 
469 	if (!strcmp(buf, "/"))
470 		return NULL;
471 
472 	if (strstr(buf, "(deleted)"))
473 		return NULL;
474 
475 	if (asprintf(&new_name, "%s/%s", buf, filename) < 0)
476 		return NULL;
477 
478 	return new_name;
479 }
480 
481 /*
482  * Reallocate an array *arr of size *arr_sz so that it is big enough to contain
483  * x elements of size msz, initializing new entries to *init_val or zero if
484  * init_val is NULL
485  */
486 int do_realloc_array_as_needed(void **arr, size_t *arr_sz, size_t x, size_t msz, const void *init_val)
487 {
488 	size_t new_sz = *arr_sz;
489 	void *new_arr;
490 	size_t i;
491 
492 	if (!new_sz)
493 		new_sz = msz >= 64 ? 1 : roundup(64, msz); /* Start with at least 64 bytes */
494 	while (x >= new_sz) {
495 		if (check_mul_overflow(new_sz, (size_t)2, &new_sz))
496 			return -ENOMEM;
497 	}
498 	if (new_sz == *arr_sz)
499 		return 0;
500 	new_arr = calloc(new_sz, msz);
501 	if (!new_arr)
502 		return -ENOMEM;
503 	if (*arr_sz)
504 		memcpy(new_arr, *arr, *arr_sz * msz);
505 	if (init_val) {
506 		for (i = *arr_sz; i < new_sz; i++)
507 			memcpy(new_arr + (i * msz), init_val, msz);
508 	}
509 	*arr = new_arr;
510 	*arr_sz = new_sz;
511 	return 0;
512 }
513 
514 #ifndef HAVE_SCHED_GETCPU_SUPPORT
515 int sched_getcpu(void)
516 {
517 #ifdef __NR_getcpu
518 	unsigned int cpu;
519 	int err = syscall(__NR_getcpu, &cpu, NULL, NULL);
520 
521 	if (!err)
522 		return cpu;
523 #else
524 	errno = ENOSYS;
525 #endif
526 	return -1;
527 }
528 #endif
529 
530 #ifndef HAVE_SCANDIRAT_SUPPORT
531 int scandirat(int dirfd, const char *dirp,
532 	      struct dirent ***namelist,
533 	      int (*filter)(const struct dirent *),
534 	      int (*compar)(const struct dirent **, const struct dirent **))
535 {
536 	char path[PATH_MAX];
537 	int err, fd = openat(dirfd, dirp, O_PATH);
538 
539 	if (fd < 0)
540 		return fd;
541 
542 	snprintf(path, sizeof(path), "/proc/%d/fd/%d", getpid(), fd);
543 	err = scandir(path, namelist, filter, compar);
544 	close(fd);
545 	return err;
546 }
547 #endif
548 
549 /* basename version that takes a const input string */
550 const char *perf_basename(const char *path)
551 {
552 	const char *base = strrchr(path, '/');
553 
554 	return base ? base + 1 : path;
555 }
556