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