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