6 #include <sys/utsname.h>
7 #ifdef HAVE_BACKTRACE_SUPPORT
16 #include <linux/kernel.h>
17 #include <linux/log2.h>
19 #include "callchain.h"
22 #define CALLCHAIN_PARAM_DEFAULT \
23 .mode = CHAIN_GRAPH_ABS, \
25 .order = ORDER_CALLEE, \
26 .key = CCKEY_FUNCTION, \
27 .value = CCVAL_PERCENT, \
29 struct callchain_param callchain_param = {
30 CALLCHAIN_PARAM_DEFAULT
33 struct callchain_param callchain_param_default
= {
34 CALLCHAIN_PARAM_DEFAULT
38 * XXX We need to find a better place for these things...
40 unsigned int page_size
;
43 int sysctl_perf_event_max_stack
= PERF_MAX_STACK_DEPTH
;
44 int sysctl_perf_event_max_contexts_per_stack
= PERF_MAX_CONTEXTS_PER_STACK
;
46 bool test_attr__enabled
;
48 bool perf_host
= true;
49 bool perf_guest
= false;
51 void event_attr_init(struct perf_event_attr
*attr
)
54 attr
->exclude_host
= 1;
56 attr
->exclude_guest
= 1;
57 /* to capture ABI version */
58 attr
->size
= sizeof(*attr
);
61 int mkdir_p(char *path
, mode_t mode
)
70 if (stat(path
, &st
) == 0)
75 while ((d
= strchr(d
, '/'))) {
77 err
= stat(path
, &st
) && mkdir(path
, mode
);
84 return (stat(path
, &st
) && mkdir(path
, mode
)) ? -1 : 0;
92 char namebuf
[PATH_MAX
];
98 while ((d
= readdir(dir
)) != NULL
&& !ret
) {
101 if (!strcmp(d
->d_name
, ".") || !strcmp(d
->d_name
, ".."))
104 scnprintf(namebuf
, sizeof(namebuf
), "%s/%s",
107 /* We have to check symbolic link itself */
108 ret
= lstat(namebuf
, &statbuf
);
110 pr_debug("stat failed: %s\n", namebuf
);
114 if (S_ISDIR(statbuf
.st_mode
))
115 ret
= rm_rf(namebuf
);
117 ret
= unlink(namebuf
);
127 /* A filter which removes dot files */
128 bool lsdir_no_dot_filter(const char *name __maybe_unused
, struct dirent
*d
)
130 return d
->d_name
[0] != '.';
133 /* lsdir reads a directory and store it in strlist */
134 struct strlist
*lsdir(const char *name
,
135 bool (*filter
)(const char *, struct dirent
*))
137 struct strlist
*list
= NULL
;
145 list
= strlist__new(NULL
, NULL
);
151 while ((d
= readdir(dir
)) != NULL
) {
152 if (!filter
|| filter(name
, d
))
153 strlist__add(list
, d
->d_name
);
161 static int slow_copyfile(const char *from
, const char *to
)
166 FILE *from_fp
= fopen(from
, "r"), *to_fp
;
171 to_fp
= fopen(to
, "w");
173 goto out_fclose_from
;
175 while (getline(&line
, &n
, from_fp
) > 0)
176 if (fputs(line
, to_fp
) == EOF
)
188 int copyfile_offset(int ifd
, loff_t off_in
, int ofd
, loff_t off_out
, u64 size
)
193 pgoff
= off_in
& ~(page_size
- 1);
196 ptr
= mmap(NULL
, off_in
+ size
, PROT_READ
, MAP_PRIVATE
, ifd
, pgoff
);
197 if (ptr
== MAP_FAILED
)
201 ssize_t ret
= pwrite(ofd
, ptr
+ off_in
, size
, off_out
);
202 if (ret
< 0 && errno
== EINTR
)
211 munmap(ptr
, off_in
+ size
);
213 return size
? -1 : 0;
216 int copyfile_mode(const char *from
, const char *to
, mode_t mode
)
221 char *tmp
= NULL
, *ptr
= NULL
;
226 /* extra 'x' at the end is to reserve space for '.' */
227 if (asprintf(&tmp
, "%s.XXXXXXx", to
) < 0) {
231 ptr
= strrchr(tmp
, '/');
234 ptr
= memmove(ptr
+ 1, ptr
, strlen(ptr
) - 1);
241 if (fchmod(tofd
, mode
))
244 if (st
.st_size
== 0) { /* /proc? do it slowly... */
245 err
= slow_copyfile(from
, tmp
);
249 fromfd
= open(from
, O_RDONLY
);
253 err
= copyfile_offset(fromfd
, 0, tofd
, 0, st
.st_size
);
266 int copyfile(const char *from
, const char *to
)
268 return copyfile_mode(from
, to
, 0755);
271 unsigned long convert_unit(unsigned long value
, char *unit
)
293 static ssize_t
ion(bool is_read
, int fd
, void *buf
, size_t n
)
295 void *buf_start
= buf
;
299 ssize_t ret
= is_read
? read(fd
, buf
, left
) :
300 write(fd
, buf
, left
);
302 if (ret
< 0 && errno
== EINTR
)
311 BUG_ON((size_t)(buf
- buf_start
) != n
);
316 * Read exactly 'n' bytes or return an error.
318 ssize_t
readn(int fd
, void *buf
, size_t n
)
320 return ion(true, fd
, buf
, n
);
324 * Write exactly 'n' bytes or return an error.
326 ssize_t
writen(int fd
, void *buf
, size_t n
)
328 return ion(false, fd
, buf
, n
);
331 size_t hex_width(u64 v
)
341 static int hex(char ch
)
343 if ((ch
>= '0') && (ch
<= '9'))
345 if ((ch
>= 'a') && (ch
<= 'f'))
346 return ch
- 'a' + 10;
347 if ((ch
>= 'A') && (ch
<= 'F'))
348 return ch
- 'A' + 10;
353 * While we find nice hex chars, build a long_val.
354 * Return number of chars processed.
356 int hex2u64(const char *ptr
, u64
*long_val
)
362 const int hex_val
= hex(*p
);
367 *long_val
= (*long_val
<< 4) | hex_val
;
374 /* Obtain a backtrace and print it to stdout. */
375 #ifdef HAVE_BACKTRACE_SUPPORT
376 void dump_stack(void)
379 size_t size
= backtrace(array
, ARRAY_SIZE(array
));
380 char **strings
= backtrace_symbols(array
, size
);
383 printf("Obtained %zd stack frames.\n", size
);
385 for (i
= 0; i
< size
; i
++)
386 printf("%s\n", strings
[i
]);
391 void dump_stack(void) {}
394 void sighandler_dump_stack(int sig
)
396 psignal(sig
, "perf");
398 signal(sig
, SIG_DFL
);
402 int parse_nsec_time(const char *str
, u64
*ptime
)
404 u64 time_sec
, time_nsec
;
407 time_sec
= strtoul(str
, &end
, 10);
408 if (*end
!= '.' && *end
!= '\0')
415 if (strlen(++end
) > 9)
418 strncpy(nsec_buf
, end
, 9);
421 /* make it nsec precision */
422 for (i
= strlen(nsec_buf
); i
< 9; i
++)
425 time_nsec
= strtoul(nsec_buf
, &end
, 10);
431 *ptime
= time_sec
* NSEC_PER_SEC
+ time_nsec
;
435 unsigned long parse_tag_value(const char *str
, struct parse_tag
*tags
)
437 struct parse_tag
*i
= tags
;
442 s
= strchr(str
, i
->tag
);
444 unsigned long int value
;
447 value
= strtoul(str
, &endptr
, 10);
451 if (value
> ULONG_MAX
/ i
->mult
)
459 return (unsigned long) -1;
462 int get_stack_size(const char *str
, unsigned long *_size
)
466 unsigned long max_size
= round_down(USHRT_MAX
, sizeof(u64
));
468 size
= strtoul(str
, &endptr
, 0);
474 size
= round_up(size
, sizeof(u64
));
475 if (!size
|| size
> max_size
)
483 pr_err("callchain: Incorrect stack dump size (max %ld): %s\n",
488 int parse_callchain_record(const char *arg
, struct callchain_param
*param
)
490 char *tok
, *name
, *saveptr
= NULL
;
494 /* We need buffer that we know we can write to. */
495 buf
= malloc(strlen(arg
) + 1);
501 tok
= strtok_r((char *)buf
, ",", &saveptr
);
502 name
= tok
? : (char *)buf
;
505 /* Framepointer style */
506 if (!strncmp(name
, "fp", sizeof("fp"))) {
507 if (!strtok_r(NULL
, ",", &saveptr
)) {
508 param
->record_mode
= CALLCHAIN_FP
;
511 pr_err("callchain: No more arguments "
512 "needed for --call-graph fp\n");
516 } else if (!strncmp(name
, "dwarf", sizeof("dwarf"))) {
517 const unsigned long default_stack_dump_size
= 8192;
520 param
->record_mode
= CALLCHAIN_DWARF
;
521 param
->dump_size
= default_stack_dump_size
;
523 tok
= strtok_r(NULL
, ",", &saveptr
);
525 unsigned long size
= 0;
527 ret
= get_stack_size(tok
, &size
);
528 param
->dump_size
= size
;
530 } else if (!strncmp(name
, "lbr", sizeof("lbr"))) {
531 if (!strtok_r(NULL
, ",", &saveptr
)) {
532 param
->record_mode
= CALLCHAIN_LBR
;
535 pr_err("callchain: No more arguments "
536 "needed for --call-graph lbr\n");
539 pr_err("callchain: Unknown --call-graph option "
550 const char *get_filename_for_perf_kvm(void)
552 const char *filename
;
554 if (perf_host
&& !perf_guest
)
555 filename
= strdup("perf.data.host");
556 else if (!perf_host
&& perf_guest
)
557 filename
= strdup("perf.data.guest");
559 filename
= strdup("perf.data.kvm");
564 int perf_event_paranoid(void)
568 if (sysctl__read_int("kernel/perf_event_paranoid", &value
))
574 void mem_bswap_32(void *src
, int byte_size
)
577 while (byte_size
> 0) {
579 byte_size
-= sizeof(u32
);
584 void mem_bswap_64(void *src
, int byte_size
)
588 while (byte_size
> 0) {
590 byte_size
-= sizeof(u64
);
595 bool find_process(const char *name
)
597 size_t len
= strlen(name
);
602 dir
= opendir(procfs__mountpoint());
606 /* Walk through the directory. */
607 while (ret
&& (d
= readdir(dir
)) != NULL
) {
612 if ((d
->d_type
!= DT_DIR
) ||
613 !strcmp(".", d
->d_name
) ||
614 !strcmp("..", d
->d_name
))
617 scnprintf(path
, sizeof(path
), "%s/%s/comm",
618 procfs__mountpoint(), d
->d_name
);
620 if (filename__read_str(path
, &data
, &size
))
623 ret
= strncmp(name
, data
, len
);
628 return ret
? false : true;
632 fetch_kernel_version(unsigned int *puint
, char *str
,
635 struct utsname utsname
;
636 int version
, patchlevel
, sublevel
, err
;
641 if (str
&& str_size
) {
642 strncpy(str
, utsname
.release
, str_size
);
643 str
[str_size
- 1] = '\0';
646 err
= sscanf(utsname
.release
, "%d.%d.%d",
647 &version
, &patchlevel
, &sublevel
);
650 pr_debug("Unablt to get kernel version from uname '%s'\n",
656 *puint
= (version
<< 16) + (patchlevel
<< 8) + sublevel
;
660 const char *perf_tip(const char *dirpath
)
662 struct strlist
*tips
;
663 struct str_node
*node
;
665 struct strlist_config conf
= {
670 tips
= strlist__new("tips.txt", &conf
);
672 return errno
== ENOENT
? NULL
: "Tip: get more memory! ;-p";
674 if (strlist__nr_entries(tips
) == 0)
677 node
= strlist__entry(tips
, random() % strlist__nr_entries(tips
));
678 if (asprintf(&tip
, "Tip: %s", node
->s
) < 0)
679 tip
= (char *)"Tip: get more memory! ;-)";
682 strlist__delete(tips
);
687 bool is_regular_file(const char *file
)
694 return S_ISREG(st
.st_mode
);
697 int fetch_current_timestamp(char *buf
, size_t sz
)
703 if (gettimeofday(&tv
, NULL
) || !localtime_r(&tv
.tv_sec
, &tm
))
706 if (!strftime(dt
, sizeof(dt
), "%Y%m%d%H%M%S", &tm
))
709 scnprintf(buf
, sz
, "%s%02u", dt
, (unsigned)tv
.tv_usec
/ 10000);
714 void print_binary(unsigned char *data
, size_t len
,
715 size_t bytes_per_line
, print_binary_t printer
,
723 bytes_per_line
= roundup_pow_of_two(bytes_per_line
);
724 mask
= bytes_per_line
- 1;
726 printer(BINARY_PRINT_DATA_BEGIN
, 0, extra
);
727 for (i
= 0; i
< len
; i
++) {
728 if ((i
& mask
) == 0) {
729 printer(BINARY_PRINT_LINE_BEGIN
, -1, extra
);
730 printer(BINARY_PRINT_ADDR
, i
, extra
);
733 printer(BINARY_PRINT_NUM_DATA
, data
[i
], extra
);
735 if (((i
& mask
) == mask
) || i
== len
- 1) {
736 for (j
= 0; j
< mask
-(i
& mask
); j
++)
737 printer(BINARY_PRINT_NUM_PAD
, -1, extra
);
739 printer(BINARY_PRINT_SEP
, i
, extra
);
740 for (j
= i
& ~mask
; j
<= i
; j
++)
741 printer(BINARY_PRINT_CHAR_DATA
, data
[j
], extra
);
742 for (j
= 0; j
< mask
-(i
& mask
); j
++)
743 printer(BINARY_PRINT_CHAR_PAD
, i
, extra
);
744 printer(BINARY_PRINT_LINE_END
, -1, extra
);
747 printer(BINARY_PRINT_DATA_END
, -1, extra
);
750 int is_printable_array(char *p
, unsigned int len
)
754 if (!p
|| !len
|| p
[len
- 1] != 0)
759 for (i
= 0; i
< len
; i
++) {
760 if (!isprint(p
[i
]) && !isspace(p
[i
]))