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