xref: /linux/tools/perf/util/util.c (revision 42fc2e9ef9603a7948aaa4ffd8dfb94b30294ad8)
1 // SPDX-License-Identifier: GPL-2.0
2 #include "../perf.h"
3 #include "util.h"
4 #include "debug.h"
5 #include "namespaces.h"
6 #include <api/fs/fs.h>
7 #include <sys/mman.h>
8 #include <sys/stat.h>
9 #include <sys/utsname.h>
10 #include <dirent.h>
11 #include <fcntl.h>
12 #include <inttypes.h>
13 #include <signal.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <errno.h>
18 #include <limits.h>
19 #include <linux/capability.h>
20 #include <linux/kernel.h>
21 #include <linux/log2.h>
22 #include <linux/time64.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 bool perf_singlethreaded = true;
33 
34 void perf_set_singlethreaded(void)
35 {
36 	perf_singlethreaded = true;
37 }
38 
39 void perf_set_multithreaded(void)
40 {
41 	perf_singlethreaded = false;
42 }
43 
44 unsigned int page_size;
45 
46 #ifdef _SC_LEVEL1_DCACHE_LINESIZE
47 #define cache_line_size(cacheline_sizep) *cacheline_sizep = sysconf(_SC_LEVEL1_DCACHE_LINESIZE)
48 #else
49 static void cache_line_size(int *cacheline_sizep)
50 {
51 	if (sysfs__read_int("devices/system/cpu/cpu0/cache/index0/coherency_line_size", cacheline_sizep))
52 		pr_debug("cannot determine cache line size");
53 }
54 #endif
55 
56 int cacheline_size(void)
57 {
58 	static int size;
59 
60 	if (!size)
61 		cache_line_size(&size);
62 
63 	return size;
64 }
65 
66 int sysctl_perf_event_max_stack = PERF_MAX_STACK_DEPTH;
67 int sysctl_perf_event_max_contexts_per_stack = PERF_MAX_CONTEXTS_PER_STACK;
68 
69 int sysctl__max_stack(void)
70 {
71 	int value;
72 
73 	if (sysctl__read_int("kernel/perf_event_max_stack", &value) == 0)
74 		sysctl_perf_event_max_stack = value;
75 
76 	if (sysctl__read_int("kernel/perf_event_max_contexts_per_stack", &value) == 0)
77 		sysctl_perf_event_max_contexts_per_stack = value;
78 
79 	return sysctl_perf_event_max_stack;
80 }
81 
82 bool test_attr__enabled;
83 
84 bool perf_host  = true;
85 bool perf_guest = false;
86 
87 void event_attr_init(struct perf_event_attr *attr)
88 {
89 	if (!perf_host)
90 		attr->exclude_host  = 1;
91 	if (!perf_guest)
92 		attr->exclude_guest = 1;
93 	/* to capture ABI version */
94 	attr->size = sizeof(*attr);
95 }
96 
97 int mkdir_p(char *path, mode_t mode)
98 {
99 	struct stat st;
100 	int err;
101 	char *d = path;
102 
103 	if (*d != '/')
104 		return -1;
105 
106 	if (stat(path, &st) == 0)
107 		return 0;
108 
109 	while (*++d == '/');
110 
111 	while ((d = strchr(d, '/'))) {
112 		*d = '\0';
113 		err = stat(path, &st) && mkdir(path, mode);
114 		*d++ = '/';
115 		if (err)
116 			return -1;
117 		while (*d == '/')
118 			++d;
119 	}
120 	return (stat(path, &st) && mkdir(path, mode)) ? -1 : 0;
121 }
122 
123 static bool match_pat(char *file, const char **pat)
124 {
125 	int i = 0;
126 
127 	if (!pat)
128 		return true;
129 
130 	while (pat[i]) {
131 		if (strglobmatch(file, pat[i]))
132 			return true;
133 
134 		i++;
135 	}
136 
137 	return false;
138 }
139 
140 /*
141  * The depth specify how deep the removal will go.
142  * 0       - will remove only files under the 'path' directory
143  * 1 .. x  - will dive in x-level deep under the 'path' directory
144  *
145  * If specified the pat is array of string patterns ended with NULL,
146  * which are checked upon every file/directory found. Only matching
147  * ones are removed.
148  *
149  * The function returns:
150  *    0 on success
151  *   -1 on removal failure with errno set
152  *   -2 on pattern failure
153  */
154 static int rm_rf_depth_pat(const char *path, int depth, const char **pat)
155 {
156 	DIR *dir;
157 	int ret;
158 	struct dirent *d;
159 	char namebuf[PATH_MAX];
160 	struct stat statbuf;
161 
162 	/* Do not fail if there's no file. */
163 	ret = lstat(path, &statbuf);
164 	if (ret)
165 		return 0;
166 
167 	/* Try to remove any file we get. */
168 	if (!(statbuf.st_mode & S_IFDIR))
169 		return unlink(path);
170 
171 	/* We have directory in path. */
172 	dir = opendir(path);
173 	if (dir == NULL)
174 		return -1;
175 
176 	while ((d = readdir(dir)) != NULL && !ret) {
177 
178 		if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
179 			continue;
180 
181 		if (!match_pat(d->d_name, pat))
182 			return -2;
183 
184 		scnprintf(namebuf, sizeof(namebuf), "%s/%s",
185 			  path, d->d_name);
186 
187 		/* We have to check symbolic link itself */
188 		ret = lstat(namebuf, &statbuf);
189 		if (ret < 0) {
190 			pr_debug("stat failed: %s\n", namebuf);
191 			break;
192 		}
193 
194 		if (S_ISDIR(statbuf.st_mode))
195 			ret = depth ? rm_rf_depth_pat(namebuf, depth - 1, pat) : 0;
196 		else
197 			ret = unlink(namebuf);
198 	}
199 	closedir(dir);
200 
201 	if (ret < 0)
202 		return ret;
203 
204 	return rmdir(path);
205 }
206 
207 int rm_rf_perf_data(const char *path)
208 {
209 	const char *pat[] = {
210 		"header",
211 		"data.*",
212 		NULL,
213 	};
214 
215 	return rm_rf_depth_pat(path, 0, pat);
216 }
217 
218 int rm_rf(const char *path)
219 {
220 	return rm_rf_depth_pat(path, INT_MAX, NULL);
221 }
222 
223 /* A filter which removes dot files */
224 bool lsdir_no_dot_filter(const char *name __maybe_unused, struct dirent *d)
225 {
226 	return d->d_name[0] != '.';
227 }
228 
229 /* lsdir reads a directory and store it in strlist */
230 struct strlist *lsdir(const char *name,
231 		      bool (*filter)(const char *, struct dirent *))
232 {
233 	struct strlist *list = NULL;
234 	DIR *dir;
235 	struct dirent *d;
236 
237 	dir = opendir(name);
238 	if (!dir)
239 		return NULL;
240 
241 	list = strlist__new(NULL, NULL);
242 	if (!list) {
243 		errno = ENOMEM;
244 		goto out;
245 	}
246 
247 	while ((d = readdir(dir)) != NULL) {
248 		if (!filter || filter(name, d))
249 			strlist__add(list, d->d_name);
250 	}
251 
252 out:
253 	closedir(dir);
254 	return list;
255 }
256 
257 static int slow_copyfile(const char *from, const char *to, struct nsinfo *nsi)
258 {
259 	int err = -1;
260 	char *line = NULL;
261 	size_t n;
262 	FILE *from_fp, *to_fp;
263 	struct nscookie nsc;
264 
265 	nsinfo__mountns_enter(nsi, &nsc);
266 	from_fp = fopen(from, "r");
267 	nsinfo__mountns_exit(&nsc);
268 	if (from_fp == NULL)
269 		goto out;
270 
271 	to_fp = fopen(to, "w");
272 	if (to_fp == NULL)
273 		goto out_fclose_from;
274 
275 	while (getline(&line, &n, from_fp) > 0)
276 		if (fputs(line, to_fp) == EOF)
277 			goto out_fclose_to;
278 	err = 0;
279 out_fclose_to:
280 	fclose(to_fp);
281 	free(line);
282 out_fclose_from:
283 	fclose(from_fp);
284 out:
285 	return err;
286 }
287 
288 int copyfile_offset(int ifd, loff_t off_in, int ofd, loff_t off_out, u64 size)
289 {
290 	void *ptr;
291 	loff_t pgoff;
292 
293 	pgoff = off_in & ~(page_size - 1);
294 	off_in -= pgoff;
295 
296 	ptr = mmap(NULL, off_in + size, PROT_READ, MAP_PRIVATE, ifd, pgoff);
297 	if (ptr == MAP_FAILED)
298 		return -1;
299 
300 	while (size) {
301 		ssize_t ret = pwrite(ofd, ptr + off_in, size, off_out);
302 		if (ret < 0 && errno == EINTR)
303 			continue;
304 		if (ret <= 0)
305 			break;
306 
307 		size -= ret;
308 		off_in += ret;
309 		off_out += ret;
310 	}
311 	munmap(ptr, off_in + size);
312 
313 	return size ? -1 : 0;
314 }
315 
316 static int copyfile_mode_ns(const char *from, const char *to, mode_t mode,
317 			    struct nsinfo *nsi)
318 {
319 	int fromfd, tofd;
320 	struct stat st;
321 	int err;
322 	char *tmp = NULL, *ptr = NULL;
323 	struct nscookie nsc;
324 
325 	nsinfo__mountns_enter(nsi, &nsc);
326 	err = stat(from, &st);
327 	nsinfo__mountns_exit(&nsc);
328 	if (err)
329 		goto out;
330 	err = -1;
331 
332 	/* extra 'x' at the end is to reserve space for '.' */
333 	if (asprintf(&tmp, "%s.XXXXXXx", to) < 0) {
334 		tmp = NULL;
335 		goto out;
336 	}
337 	ptr = strrchr(tmp, '/');
338 	if (!ptr)
339 		goto out;
340 	ptr = memmove(ptr + 1, ptr, strlen(ptr) - 1);
341 	*ptr = '.';
342 
343 	tofd = mkstemp(tmp);
344 	if (tofd < 0)
345 		goto out;
346 
347 	if (fchmod(tofd, mode))
348 		goto out_close_to;
349 
350 	if (st.st_size == 0) { /* /proc? do it slowly... */
351 		err = slow_copyfile(from, tmp, nsi);
352 		goto out_close_to;
353 	}
354 
355 	nsinfo__mountns_enter(nsi, &nsc);
356 	fromfd = open(from, O_RDONLY);
357 	nsinfo__mountns_exit(&nsc);
358 	if (fromfd < 0)
359 		goto out_close_to;
360 
361 	err = copyfile_offset(fromfd, 0, tofd, 0, st.st_size);
362 
363 	close(fromfd);
364 out_close_to:
365 	close(tofd);
366 	if (!err)
367 		err = link(tmp, to);
368 	unlink(tmp);
369 out:
370 	free(tmp);
371 	return err;
372 }
373 
374 int copyfile_ns(const char *from, const char *to, struct nsinfo *nsi)
375 {
376 	return copyfile_mode_ns(from, to, 0755, nsi);
377 }
378 
379 int copyfile_mode(const char *from, const char *to, mode_t mode)
380 {
381 	return copyfile_mode_ns(from, to, mode, NULL);
382 }
383 
384 int copyfile(const char *from, const char *to)
385 {
386 	return copyfile_mode(from, to, 0755);
387 }
388 
389 size_t hex_width(u64 v)
390 {
391 	size_t n = 1;
392 
393 	while ((v >>= 4))
394 		++n;
395 
396 	return n;
397 }
398 
399 int perf_event_paranoid(void)
400 {
401 	int value;
402 
403 	if (sysctl__read_int("kernel/perf_event_paranoid", &value))
404 		return INT_MAX;
405 
406 	return value;
407 }
408 
409 bool perf_event_paranoid_check(int max_level)
410 {
411 	return perf_cap__capable(CAP_SYS_ADMIN) ||
412 			perf_event_paranoid() <= max_level;
413 }
414 
415 static int
416 fetch_ubuntu_kernel_version(unsigned int *puint)
417 {
418 	ssize_t len;
419 	size_t line_len = 0;
420 	char *ptr, *line = NULL;
421 	int version, patchlevel, sublevel, err;
422 	FILE *vsig;
423 
424 	if (!puint)
425 		return 0;
426 
427 	vsig = fopen("/proc/version_signature", "r");
428 	if (!vsig) {
429 		pr_debug("Open /proc/version_signature failed: %s\n",
430 			 strerror(errno));
431 		return -1;
432 	}
433 
434 	len = getline(&line, &line_len, vsig);
435 	fclose(vsig);
436 	err = -1;
437 	if (len <= 0) {
438 		pr_debug("Reading from /proc/version_signature failed: %s\n",
439 			 strerror(errno));
440 		goto errout;
441 	}
442 
443 	ptr = strrchr(line, ' ');
444 	if (!ptr) {
445 		pr_debug("Parsing /proc/version_signature failed: %s\n", line);
446 		goto errout;
447 	}
448 
449 	err = sscanf(ptr + 1, "%d.%d.%d",
450 		     &version, &patchlevel, &sublevel);
451 	if (err != 3) {
452 		pr_debug("Unable to get kernel version from /proc/version_signature '%s'\n",
453 			 line);
454 		goto errout;
455 	}
456 
457 	*puint = (version << 16) + (patchlevel << 8) + sublevel;
458 	err = 0;
459 errout:
460 	free(line);
461 	return err;
462 }
463 
464 int
465 fetch_kernel_version(unsigned int *puint, char *str,
466 		     size_t str_size)
467 {
468 	struct utsname utsname;
469 	int version, patchlevel, sublevel, err;
470 	bool int_ver_ready = false;
471 
472 	if (access("/proc/version_signature", R_OK) == 0)
473 		if (!fetch_ubuntu_kernel_version(puint))
474 			int_ver_ready = true;
475 
476 	if (uname(&utsname))
477 		return -1;
478 
479 	if (str && str_size) {
480 		strncpy(str, utsname.release, str_size);
481 		str[str_size - 1] = '\0';
482 	}
483 
484 	if (!puint || int_ver_ready)
485 		return 0;
486 
487 	err = sscanf(utsname.release, "%d.%d.%d",
488 		     &version, &patchlevel, &sublevel);
489 
490 	if (err != 3) {
491 		pr_debug("Unable to get kernel version from uname '%s'\n",
492 			 utsname.release);
493 		return -1;
494 	}
495 
496 	*puint = (version << 16) + (patchlevel << 8) + sublevel;
497 	return 0;
498 }
499 
500 const char *perf_tip(const char *dirpath)
501 {
502 	struct strlist *tips;
503 	struct str_node *node;
504 	char *tip = NULL;
505 	struct strlist_config conf = {
506 		.dirname = dirpath,
507 		.file_only = true,
508 	};
509 
510 	tips = strlist__new("tips.txt", &conf);
511 	if (tips == NULL)
512 		return errno == ENOENT ? NULL :
513 			"Tip: check path of tips.txt or get more memory! ;-p";
514 
515 	if (strlist__nr_entries(tips) == 0)
516 		goto out;
517 
518 	node = strlist__entry(tips, random() % strlist__nr_entries(tips));
519 	if (asprintf(&tip, "Tip: %s", node->s) < 0)
520 		tip = (char *)"Tip: get more memory! ;-)";
521 
522 out:
523 	strlist__delete(tips);
524 
525 	return tip;
526 }
527 
528 char *perf_exe(char *buf, int len)
529 {
530 	int n = readlink("/proc/self/exe", buf, len);
531 	if (n > 0) {
532 		buf[n] = 0;
533 		return buf;
534 	}
535 	return strcpy(buf, "perf");
536 }
537