xref: /linux/tools/perf/util/util.c (revision 2ec8107d8e0d1d285b2bbf1999e7f4e46b6b535b)
1 #include "../perf.h"
2 #include "util.h"
3 #include "debug.h"
4 #include <api/fs/fs.h>
5 #include <sys/mman.h>
6 #include <sys/utsname.h>
7 #ifdef HAVE_BACKTRACE_SUPPORT
8 #include <execinfo.h>
9 #endif
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <errno.h>
14 #include <limits.h>
15 #include <byteswap.h>
16 #include <linux/kernel.h>
17 #include <linux/log2.h>
18 #include <linux/time64.h>
19 #include <unistd.h>
20 #include "callchain.h"
21 #include "strlist.h"
22 
23 #define CALLCHAIN_PARAM_DEFAULT			\
24 	.mode		= CHAIN_GRAPH_ABS,	\
25 	.min_percent	= 0.5,			\
26 	.order		= ORDER_CALLEE,		\
27 	.key		= CCKEY_FUNCTION,	\
28 	.value		= CCVAL_PERCENT,	\
29 
30 struct callchain_param callchain_param = {
31 	CALLCHAIN_PARAM_DEFAULT
32 };
33 
34 struct callchain_param callchain_param_default = {
35 	CALLCHAIN_PARAM_DEFAULT
36 };
37 
38 /*
39  * XXX We need to find a better place for these things...
40  */
41 unsigned int page_size;
42 int cacheline_size;
43 
44 int sysctl_perf_event_max_stack = PERF_MAX_STACK_DEPTH;
45 int sysctl_perf_event_max_contexts_per_stack = PERF_MAX_CONTEXTS_PER_STACK;
46 
47 bool test_attr__enabled;
48 
49 bool perf_host  = true;
50 bool perf_guest = false;
51 
52 void event_attr_init(struct perf_event_attr *attr)
53 {
54 	if (!perf_host)
55 		attr->exclude_host  = 1;
56 	if (!perf_guest)
57 		attr->exclude_guest = 1;
58 	/* to capture ABI version */
59 	attr->size = sizeof(*attr);
60 }
61 
62 int mkdir_p(char *path, mode_t mode)
63 {
64 	struct stat st;
65 	int err;
66 	char *d = path;
67 
68 	if (*d != '/')
69 		return -1;
70 
71 	if (stat(path, &st) == 0)
72 		return 0;
73 
74 	while (*++d == '/');
75 
76 	while ((d = strchr(d, '/'))) {
77 		*d = '\0';
78 		err = stat(path, &st) && mkdir(path, mode);
79 		*d++ = '/';
80 		if (err)
81 			return -1;
82 		while (*d == '/')
83 			++d;
84 	}
85 	return (stat(path, &st) && mkdir(path, mode)) ? -1 : 0;
86 }
87 
88 int rm_rf(char *path)
89 {
90 	DIR *dir;
91 	int ret = 0;
92 	struct dirent *d;
93 	char namebuf[PATH_MAX];
94 
95 	dir = opendir(path);
96 	if (dir == NULL)
97 		return 0;
98 
99 	while ((d = readdir(dir)) != NULL && !ret) {
100 		struct stat statbuf;
101 
102 		if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
103 			continue;
104 
105 		scnprintf(namebuf, sizeof(namebuf), "%s/%s",
106 			  path, d->d_name);
107 
108 		/* We have to check symbolic link itself */
109 		ret = lstat(namebuf, &statbuf);
110 		if (ret < 0) {
111 			pr_debug("stat failed: %s\n", namebuf);
112 			break;
113 		}
114 
115 		if (S_ISDIR(statbuf.st_mode))
116 			ret = rm_rf(namebuf);
117 		else
118 			ret = unlink(namebuf);
119 	}
120 	closedir(dir);
121 
122 	if (ret < 0)
123 		return ret;
124 
125 	return rmdir(path);
126 }
127 
128 /* A filter which removes dot files */
129 bool lsdir_no_dot_filter(const char *name __maybe_unused, struct dirent *d)
130 {
131 	return d->d_name[0] != '.';
132 }
133 
134 /* lsdir reads a directory and store it in strlist */
135 struct strlist *lsdir(const char *name,
136 		      bool (*filter)(const char *, struct dirent *))
137 {
138 	struct strlist *list = NULL;
139 	DIR *dir;
140 	struct dirent *d;
141 
142 	dir = opendir(name);
143 	if (!dir)
144 		return NULL;
145 
146 	list = strlist__new(NULL, NULL);
147 	if (!list) {
148 		errno = ENOMEM;
149 		goto out;
150 	}
151 
152 	while ((d = readdir(dir)) != NULL) {
153 		if (!filter || filter(name, d))
154 			strlist__add(list, d->d_name);
155 	}
156 
157 out:
158 	closedir(dir);
159 	return list;
160 }
161 
162 static int slow_copyfile(const char *from, const char *to)
163 {
164 	int err = -1;
165 	char *line = NULL;
166 	size_t n;
167 	FILE *from_fp = fopen(from, "r"), *to_fp;
168 
169 	if (from_fp == NULL)
170 		goto out;
171 
172 	to_fp = fopen(to, "w");
173 	if (to_fp == NULL)
174 		goto out_fclose_from;
175 
176 	while (getline(&line, &n, from_fp) > 0)
177 		if (fputs(line, to_fp) == EOF)
178 			goto out_fclose_to;
179 	err = 0;
180 out_fclose_to:
181 	fclose(to_fp);
182 	free(line);
183 out_fclose_from:
184 	fclose(from_fp);
185 out:
186 	return err;
187 }
188 
189 int copyfile_offset(int ifd, loff_t off_in, int ofd, loff_t off_out, u64 size)
190 {
191 	void *ptr;
192 	loff_t pgoff;
193 
194 	pgoff = off_in & ~(page_size - 1);
195 	off_in -= pgoff;
196 
197 	ptr = mmap(NULL, off_in + size, PROT_READ, MAP_PRIVATE, ifd, pgoff);
198 	if (ptr == MAP_FAILED)
199 		return -1;
200 
201 	while (size) {
202 		ssize_t ret = pwrite(ofd, ptr + off_in, size, off_out);
203 		if (ret < 0 && errno == EINTR)
204 			continue;
205 		if (ret <= 0)
206 			break;
207 
208 		size -= ret;
209 		off_in += ret;
210 		off_out -= ret;
211 	}
212 	munmap(ptr, off_in + size);
213 
214 	return size ? -1 : 0;
215 }
216 
217 int copyfile_mode(const char *from, const char *to, mode_t mode)
218 {
219 	int fromfd, tofd;
220 	struct stat st;
221 	int err = -1;
222 	char *tmp = NULL, *ptr = NULL;
223 
224 	if (stat(from, &st))
225 		goto out;
226 
227 	/* extra 'x' at the end is to reserve space for '.' */
228 	if (asprintf(&tmp, "%s.XXXXXXx", to) < 0) {
229 		tmp = NULL;
230 		goto out;
231 	}
232 	ptr = strrchr(tmp, '/');
233 	if (!ptr)
234 		goto out;
235 	ptr = memmove(ptr + 1, ptr, strlen(ptr) - 1);
236 	*ptr = '.';
237 
238 	tofd = mkstemp(tmp);
239 	if (tofd < 0)
240 		goto out;
241 
242 	if (fchmod(tofd, mode))
243 		goto out_close_to;
244 
245 	if (st.st_size == 0) { /* /proc? do it slowly... */
246 		err = slow_copyfile(from, tmp);
247 		goto out_close_to;
248 	}
249 
250 	fromfd = open(from, O_RDONLY);
251 	if (fromfd < 0)
252 		goto out_close_to;
253 
254 	err = copyfile_offset(fromfd, 0, tofd, 0, st.st_size);
255 
256 	close(fromfd);
257 out_close_to:
258 	close(tofd);
259 	if (!err)
260 		err = link(tmp, to);
261 	unlink(tmp);
262 out:
263 	free(tmp);
264 	return err;
265 }
266 
267 int copyfile(const char *from, const char *to)
268 {
269 	return copyfile_mode(from, to, 0755);
270 }
271 
272 unsigned long convert_unit(unsigned long value, char *unit)
273 {
274 	*unit = ' ';
275 
276 	if (value > 1000) {
277 		value /= 1000;
278 		*unit = 'K';
279 	}
280 
281 	if (value > 1000) {
282 		value /= 1000;
283 		*unit = 'M';
284 	}
285 
286 	if (value > 1000) {
287 		value /= 1000;
288 		*unit = 'G';
289 	}
290 
291 	return value;
292 }
293 
294 static ssize_t ion(bool is_read, int fd, void *buf, size_t n)
295 {
296 	void *buf_start = buf;
297 	size_t left = n;
298 
299 	while (left) {
300 		ssize_t ret = is_read ? read(fd, buf, left) :
301 					write(fd, buf, left);
302 
303 		if (ret < 0 && errno == EINTR)
304 			continue;
305 		if (ret <= 0)
306 			return ret;
307 
308 		left -= ret;
309 		buf  += ret;
310 	}
311 
312 	BUG_ON((size_t)(buf - buf_start) != n);
313 	return n;
314 }
315 
316 /*
317  * Read exactly 'n' bytes or return an error.
318  */
319 ssize_t readn(int fd, void *buf, size_t n)
320 {
321 	return ion(true, fd, buf, n);
322 }
323 
324 /*
325  * Write exactly 'n' bytes or return an error.
326  */
327 ssize_t writen(int fd, void *buf, size_t n)
328 {
329 	return ion(false, fd, buf, n);
330 }
331 
332 size_t hex_width(u64 v)
333 {
334 	size_t n = 1;
335 
336 	while ((v >>= 4))
337 		++n;
338 
339 	return n;
340 }
341 
342 static int hex(char ch)
343 {
344 	if ((ch >= '0') && (ch <= '9'))
345 		return ch - '0';
346 	if ((ch >= 'a') && (ch <= 'f'))
347 		return ch - 'a' + 10;
348 	if ((ch >= 'A') && (ch <= 'F'))
349 		return ch - 'A' + 10;
350 	return -1;
351 }
352 
353 /*
354  * While we find nice hex chars, build a long_val.
355  * Return number of chars processed.
356  */
357 int hex2u64(const char *ptr, u64 *long_val)
358 {
359 	const char *p = ptr;
360 	*long_val = 0;
361 
362 	while (*p) {
363 		const int hex_val = hex(*p);
364 
365 		if (hex_val < 0)
366 			break;
367 
368 		*long_val = (*long_val << 4) | hex_val;
369 		p++;
370 	}
371 
372 	return p - ptr;
373 }
374 
375 /* Obtain a backtrace and print it to stdout. */
376 #ifdef HAVE_BACKTRACE_SUPPORT
377 void dump_stack(void)
378 {
379 	void *array[16];
380 	size_t size = backtrace(array, ARRAY_SIZE(array));
381 	char **strings = backtrace_symbols(array, size);
382 	size_t i;
383 
384 	printf("Obtained %zd stack frames.\n", size);
385 
386 	for (i = 0; i < size; i++)
387 		printf("%s\n", strings[i]);
388 
389 	free(strings);
390 }
391 #else
392 void dump_stack(void) {}
393 #endif
394 
395 void sighandler_dump_stack(int sig)
396 {
397 	psignal(sig, "perf");
398 	dump_stack();
399 	signal(sig, SIG_DFL);
400 	raise(sig);
401 }
402 
403 int parse_nsec_time(const char *str, u64 *ptime)
404 {
405 	u64 time_sec, time_nsec;
406 	char *end;
407 
408 	time_sec = strtoul(str, &end, 10);
409 	if (*end != '.' && *end != '\0')
410 		return -1;
411 
412 	if (*end == '.') {
413 		int i;
414 		char nsec_buf[10];
415 
416 		if (strlen(++end) > 9)
417 			return -1;
418 
419 		strncpy(nsec_buf, end, 9);
420 		nsec_buf[9] = '\0';
421 
422 		/* make it nsec precision */
423 		for (i = strlen(nsec_buf); i < 9; i++)
424 			nsec_buf[i] = '0';
425 
426 		time_nsec = strtoul(nsec_buf, &end, 10);
427 		if (*end != '\0')
428 			return -1;
429 	} else
430 		time_nsec = 0;
431 
432 	*ptime = time_sec * NSEC_PER_SEC + time_nsec;
433 	return 0;
434 }
435 
436 int timestamp__scnprintf_usec(u64 timestamp, char *buf, size_t sz)
437 {
438 	u64  sec = timestamp / NSEC_PER_SEC;
439 	u64 usec = (timestamp % NSEC_PER_SEC) / NSEC_PER_USEC;
440 
441 	return scnprintf(buf, sz, "%"PRIu64".%06"PRIu64, sec, usec);
442 }
443 
444 unsigned long parse_tag_value(const char *str, struct parse_tag *tags)
445 {
446 	struct parse_tag *i = tags;
447 
448 	while (i->tag) {
449 		char *s;
450 
451 		s = strchr(str, i->tag);
452 		if (s) {
453 			unsigned long int value;
454 			char *endptr;
455 
456 			value = strtoul(str, &endptr, 10);
457 			if (s != endptr)
458 				break;
459 
460 			if (value > ULONG_MAX / i->mult)
461 				break;
462 			value *= i->mult;
463 			return value;
464 		}
465 		i++;
466 	}
467 
468 	return (unsigned long) -1;
469 }
470 
471 int get_stack_size(const char *str, unsigned long *_size)
472 {
473 	char *endptr;
474 	unsigned long size;
475 	unsigned long max_size = round_down(USHRT_MAX, sizeof(u64));
476 
477 	size = strtoul(str, &endptr, 0);
478 
479 	do {
480 		if (*endptr)
481 			break;
482 
483 		size = round_up(size, sizeof(u64));
484 		if (!size || size > max_size)
485 			break;
486 
487 		*_size = size;
488 		return 0;
489 
490 	} while (0);
491 
492 	pr_err("callchain: Incorrect stack dump size (max %ld): %s\n",
493 	       max_size, str);
494 	return -1;
495 }
496 
497 int parse_callchain_record(const char *arg, struct callchain_param *param)
498 {
499 	char *tok, *name, *saveptr = NULL;
500 	char *buf;
501 	int ret = -1;
502 
503 	/* We need buffer that we know we can write to. */
504 	buf = malloc(strlen(arg) + 1);
505 	if (!buf)
506 		return -ENOMEM;
507 
508 	strcpy(buf, arg);
509 
510 	tok = strtok_r((char *)buf, ",", &saveptr);
511 	name = tok ? : (char *)buf;
512 
513 	do {
514 		/* Framepointer style */
515 		if (!strncmp(name, "fp", sizeof("fp"))) {
516 			if (!strtok_r(NULL, ",", &saveptr)) {
517 				param->record_mode = CALLCHAIN_FP;
518 				ret = 0;
519 			} else
520 				pr_err("callchain: No more arguments "
521 				       "needed for --call-graph fp\n");
522 			break;
523 
524 		/* Dwarf style */
525 		} else if (!strncmp(name, "dwarf", sizeof("dwarf"))) {
526 			const unsigned long default_stack_dump_size = 8192;
527 
528 			ret = 0;
529 			param->record_mode = CALLCHAIN_DWARF;
530 			param->dump_size = default_stack_dump_size;
531 
532 			tok = strtok_r(NULL, ",", &saveptr);
533 			if (tok) {
534 				unsigned long size = 0;
535 
536 				ret = get_stack_size(tok, &size);
537 				param->dump_size = size;
538 			}
539 		} else if (!strncmp(name, "lbr", sizeof("lbr"))) {
540 			if (!strtok_r(NULL, ",", &saveptr)) {
541 				param->record_mode = CALLCHAIN_LBR;
542 				ret = 0;
543 			} else
544 				pr_err("callchain: No more arguments "
545 					"needed for --call-graph lbr\n");
546 			break;
547 		} else {
548 			pr_err("callchain: Unknown --call-graph option "
549 			       "value: %s\n", arg);
550 			break;
551 		}
552 
553 	} while (0);
554 
555 	free(buf);
556 	return ret;
557 }
558 
559 const char *get_filename_for_perf_kvm(void)
560 {
561 	const char *filename;
562 
563 	if (perf_host && !perf_guest)
564 		filename = strdup("perf.data.host");
565 	else if (!perf_host && perf_guest)
566 		filename = strdup("perf.data.guest");
567 	else
568 		filename = strdup("perf.data.kvm");
569 
570 	return filename;
571 }
572 
573 int perf_event_paranoid(void)
574 {
575 	int value;
576 
577 	if (sysctl__read_int("kernel/perf_event_paranoid", &value))
578 		return INT_MAX;
579 
580 	return value;
581 }
582 
583 void mem_bswap_32(void *src, int byte_size)
584 {
585 	u32 *m = src;
586 	while (byte_size > 0) {
587 		*m = bswap_32(*m);
588 		byte_size -= sizeof(u32);
589 		++m;
590 	}
591 }
592 
593 void mem_bswap_64(void *src, int byte_size)
594 {
595 	u64 *m = src;
596 
597 	while (byte_size > 0) {
598 		*m = bswap_64(*m);
599 		byte_size -= sizeof(u64);
600 		++m;
601 	}
602 }
603 
604 bool find_process(const char *name)
605 {
606 	size_t len = strlen(name);
607 	DIR *dir;
608 	struct dirent *d;
609 	int ret = -1;
610 
611 	dir = opendir(procfs__mountpoint());
612 	if (!dir)
613 		return false;
614 
615 	/* Walk through the directory. */
616 	while (ret && (d = readdir(dir)) != NULL) {
617 		char path[PATH_MAX];
618 		char *data;
619 		size_t size;
620 
621 		if ((d->d_type != DT_DIR) ||
622 		     !strcmp(".", d->d_name) ||
623 		     !strcmp("..", d->d_name))
624 			continue;
625 
626 		scnprintf(path, sizeof(path), "%s/%s/comm",
627 			  procfs__mountpoint(), d->d_name);
628 
629 		if (filename__read_str(path, &data, &size))
630 			continue;
631 
632 		ret = strncmp(name, data, len);
633 		free(data);
634 	}
635 
636 	closedir(dir);
637 	return ret ? false : true;
638 }
639 
640 int
641 fetch_kernel_version(unsigned int *puint, char *str,
642 		     size_t str_size)
643 {
644 	struct utsname utsname;
645 	int version, patchlevel, sublevel, err;
646 
647 	if (uname(&utsname))
648 		return -1;
649 
650 	if (str && str_size) {
651 		strncpy(str, utsname.release, str_size);
652 		str[str_size - 1] = '\0';
653 	}
654 
655 	err = sscanf(utsname.release, "%d.%d.%d",
656 		     &version, &patchlevel, &sublevel);
657 
658 	if (err != 3) {
659 		pr_debug("Unablt to get kernel version from uname '%s'\n",
660 			 utsname.release);
661 		return -1;
662 	}
663 
664 	if (puint)
665 		*puint = (version << 16) + (patchlevel << 8) + sublevel;
666 	return 0;
667 }
668 
669 const char *perf_tip(const char *dirpath)
670 {
671 	struct strlist *tips;
672 	struct str_node *node;
673 	char *tip = NULL;
674 	struct strlist_config conf = {
675 		.dirname = dirpath,
676 		.file_only = true,
677 	};
678 
679 	tips = strlist__new("tips.txt", &conf);
680 	if (tips == NULL)
681 		return errno == ENOENT ? NULL : "Tip: get more memory! ;-p";
682 
683 	if (strlist__nr_entries(tips) == 0)
684 		goto out;
685 
686 	node = strlist__entry(tips, random() % strlist__nr_entries(tips));
687 	if (asprintf(&tip, "Tip: %s", node->s) < 0)
688 		tip = (char *)"Tip: get more memory! ;-)";
689 
690 out:
691 	strlist__delete(tips);
692 
693 	return tip;
694 }
695 
696 bool is_regular_file(const char *file)
697 {
698 	struct stat st;
699 
700 	if (stat(file, &st))
701 		return false;
702 
703 	return S_ISREG(st.st_mode);
704 }
705 
706 int fetch_current_timestamp(char *buf, size_t sz)
707 {
708 	struct timeval tv;
709 	struct tm tm;
710 	char dt[32];
711 
712 	if (gettimeofday(&tv, NULL) || !localtime_r(&tv.tv_sec, &tm))
713 		return -1;
714 
715 	if (!strftime(dt, sizeof(dt), "%Y%m%d%H%M%S", &tm))
716 		return -1;
717 
718 	scnprintf(buf, sz, "%s%02u", dt, (unsigned)tv.tv_usec / 10000);
719 
720 	return 0;
721 }
722 
723 void print_binary(unsigned char *data, size_t len,
724 		  size_t bytes_per_line, print_binary_t printer,
725 		  void *extra)
726 {
727 	size_t i, j, mask;
728 
729 	if (!printer)
730 		return;
731 
732 	bytes_per_line = roundup_pow_of_two(bytes_per_line);
733 	mask = bytes_per_line - 1;
734 
735 	printer(BINARY_PRINT_DATA_BEGIN, 0, extra);
736 	for (i = 0; i < len; i++) {
737 		if ((i & mask) == 0) {
738 			printer(BINARY_PRINT_LINE_BEGIN, -1, extra);
739 			printer(BINARY_PRINT_ADDR, i, extra);
740 		}
741 
742 		printer(BINARY_PRINT_NUM_DATA, data[i], extra);
743 
744 		if (((i & mask) == mask) || i == len - 1) {
745 			for (j = 0; j < mask-(i & mask); j++)
746 				printer(BINARY_PRINT_NUM_PAD, -1, extra);
747 
748 			printer(BINARY_PRINT_SEP, i, extra);
749 			for (j = i & ~mask; j <= i; j++)
750 				printer(BINARY_PRINT_CHAR_DATA, data[j], extra);
751 			for (j = 0; j < mask-(i & mask); j++)
752 				printer(BINARY_PRINT_CHAR_PAD, i, extra);
753 			printer(BINARY_PRINT_LINE_END, -1, extra);
754 		}
755 	}
756 	printer(BINARY_PRINT_DATA_END, -1, extra);
757 }
758 
759 int is_printable_array(char *p, unsigned int len)
760 {
761 	unsigned int i;
762 
763 	if (!p || !len || p[len - 1] != 0)
764 		return 0;
765 
766 	len--;
767 
768 	for (i = 0; i < len; i++) {
769 		if (!isprint(p[i]) && !isspace(p[i]))
770 			return 0;
771 	}
772 	return 1;
773 }
774