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