6 #include <sys/utsname.h>
7 #ifdef HAVE_BACKTRACE_SUPPORT
16 #include <linux/kernel.h>
18 #include "callchain.h"
21 struct callchain_param callchain_param
= {
22 .mode
= CHAIN_GRAPH_ABS
,
24 .order
= ORDER_CALLEE
,
25 .key
= CCKEY_FUNCTION
,
26 .value
= CCVAL_PERCENT
,
30 * XXX We need to find a better place for these things...
32 unsigned int page_size
;
35 bool test_attr__enabled
;
37 bool perf_host
= true;
38 bool perf_guest
= false;
40 void event_attr_init(struct perf_event_attr
*attr
)
43 attr
->exclude_host
= 1;
45 attr
->exclude_guest
= 1;
46 /* to capture ABI version */
47 attr
->size
= sizeof(*attr
);
50 int mkdir_p(char *path
, mode_t mode
)
59 if (stat(path
, &st
) == 0)
64 while ((d
= strchr(d
, '/'))) {
66 err
= stat(path
, &st
) && mkdir(path
, mode
);
73 return (stat(path
, &st
) && mkdir(path
, mode
)) ? -1 : 0;
81 char namebuf
[PATH_MAX
];
87 while ((d
= readdir(dir
)) != NULL
&& !ret
) {
90 if (!strcmp(d
->d_name
, ".") || !strcmp(d
->d_name
, ".."))
93 scnprintf(namebuf
, sizeof(namebuf
), "%s/%s",
96 ret
= stat(namebuf
, &statbuf
);
98 pr_debug("stat failed: %s\n", namebuf
);
102 if (S_ISREG(statbuf
.st_mode
))
103 ret
= unlink(namebuf
);
104 else if (S_ISDIR(statbuf
.st_mode
))
105 ret
= rm_rf(namebuf
);
107 pr_debug("unknown file: %s\n", namebuf
);
119 static int slow_copyfile(const char *from
, const char *to
)
124 FILE *from_fp
= fopen(from
, "r"), *to_fp
;
129 to_fp
= fopen(to
, "w");
131 goto out_fclose_from
;
133 while (getline(&line
, &n
, from_fp
) > 0)
134 if (fputs(line
, to_fp
) == EOF
)
146 int copyfile_offset(int ifd
, loff_t off_in
, int ofd
, loff_t off_out
, u64 size
)
151 pgoff
= off_in
& ~(page_size
- 1);
154 ptr
= mmap(NULL
, off_in
+ size
, PROT_READ
, MAP_PRIVATE
, ifd
, pgoff
);
155 if (ptr
== MAP_FAILED
)
159 ssize_t ret
= pwrite(ofd
, ptr
+ off_in
, size
, off_out
);
160 if (ret
< 0 && errno
== EINTR
)
169 munmap(ptr
, off_in
+ size
);
171 return size
? -1 : 0;
174 int copyfile_mode(const char *from
, const char *to
, mode_t mode
)
179 char *tmp
= NULL
, *ptr
= NULL
;
184 /* extra 'x' at the end is to reserve space for '.' */
185 if (asprintf(&tmp
, "%s.XXXXXXx", to
) < 0) {
189 ptr
= strrchr(tmp
, '/');
192 ptr
= memmove(ptr
+ 1, ptr
, strlen(ptr
) - 1);
199 if (fchmod(tofd
, mode
))
202 if (st
.st_size
== 0) { /* /proc? do it slowly... */
203 err
= slow_copyfile(from
, tmp
);
207 fromfd
= open(from
, O_RDONLY
);
211 err
= copyfile_offset(fromfd
, 0, tofd
, 0, st
.st_size
);
224 int copyfile(const char *from
, const char *to
)
226 return copyfile_mode(from
, to
, 0755);
229 unsigned long convert_unit(unsigned long value
, char *unit
)
251 static ssize_t
ion(bool is_read
, int fd
, void *buf
, size_t n
)
253 void *buf_start
= buf
;
257 ssize_t ret
= is_read
? read(fd
, buf
, left
) :
258 write(fd
, buf
, left
);
260 if (ret
< 0 && errno
== EINTR
)
269 BUG_ON((size_t)(buf
- buf_start
) != n
);
274 * Read exactly 'n' bytes or return an error.
276 ssize_t
readn(int fd
, void *buf
, size_t n
)
278 return ion(true, fd
, buf
, n
);
282 * Write exactly 'n' bytes or return an error.
284 ssize_t
writen(int fd
, void *buf
, size_t n
)
286 return ion(false, fd
, buf
, n
);
289 size_t hex_width(u64 v
)
299 static int hex(char ch
)
301 if ((ch
>= '0') && (ch
<= '9'))
303 if ((ch
>= 'a') && (ch
<= 'f'))
304 return ch
- 'a' + 10;
305 if ((ch
>= 'A') && (ch
<= 'F'))
306 return ch
- 'A' + 10;
311 * While we find nice hex chars, build a long_val.
312 * Return number of chars processed.
314 int hex2u64(const char *ptr
, u64
*long_val
)
320 const int hex_val
= hex(*p
);
325 *long_val
= (*long_val
<< 4) | hex_val
;
332 /* Obtain a backtrace and print it to stdout. */
333 #ifdef HAVE_BACKTRACE_SUPPORT
334 void dump_stack(void)
337 size_t size
= backtrace(array
, ARRAY_SIZE(array
));
338 char **strings
= backtrace_symbols(array
, size
);
341 printf("Obtained %zd stack frames.\n", size
);
343 for (i
= 0; i
< size
; i
++)
344 printf("%s\n", strings
[i
]);
349 void dump_stack(void) {}
352 void sighandler_dump_stack(int sig
)
354 psignal(sig
, "perf");
356 signal(sig
, SIG_DFL
);
360 int parse_nsec_time(const char *str
, u64
*ptime
)
362 u64 time_sec
, time_nsec
;
365 time_sec
= strtoul(str
, &end
, 10);
366 if (*end
!= '.' && *end
!= '\0')
373 if (strlen(++end
) > 9)
376 strncpy(nsec_buf
, end
, 9);
379 /* make it nsec precision */
380 for (i
= strlen(nsec_buf
); i
< 9; i
++)
383 time_nsec
= strtoul(nsec_buf
, &end
, 10);
389 *ptime
= time_sec
* NSEC_PER_SEC
+ time_nsec
;
393 unsigned long parse_tag_value(const char *str
, struct parse_tag
*tags
)
395 struct parse_tag
*i
= tags
;
400 s
= strchr(str
, i
->tag
);
402 unsigned long int value
;
405 value
= strtoul(str
, &endptr
, 10);
409 if (value
> ULONG_MAX
/ i
->mult
)
417 return (unsigned long) -1;
420 int get_stack_size(const char *str
, unsigned long *_size
)
424 unsigned long max_size
= round_down(USHRT_MAX
, sizeof(u64
));
426 size
= strtoul(str
, &endptr
, 0);
432 size
= round_up(size
, sizeof(u64
));
433 if (!size
|| size
> max_size
)
441 pr_err("callchain: Incorrect stack dump size (max %ld): %s\n",
446 int parse_callchain_record(const char *arg
, struct callchain_param
*param
)
448 char *tok
, *name
, *saveptr
= NULL
;
452 /* We need buffer that we know we can write to. */
453 buf
= malloc(strlen(arg
) + 1);
459 tok
= strtok_r((char *)buf
, ",", &saveptr
);
460 name
= tok
? : (char *)buf
;
463 /* Framepointer style */
464 if (!strncmp(name
, "fp", sizeof("fp"))) {
465 if (!strtok_r(NULL
, ",", &saveptr
)) {
466 param
->record_mode
= CALLCHAIN_FP
;
469 pr_err("callchain: No more arguments "
470 "needed for --call-graph fp\n");
473 #ifdef HAVE_DWARF_UNWIND_SUPPORT
475 } else if (!strncmp(name
, "dwarf", sizeof("dwarf"))) {
476 const unsigned long default_stack_dump_size
= 8192;
479 param
->record_mode
= CALLCHAIN_DWARF
;
480 param
->dump_size
= default_stack_dump_size
;
482 tok
= strtok_r(NULL
, ",", &saveptr
);
484 unsigned long size
= 0;
486 ret
= get_stack_size(tok
, &size
);
487 param
->dump_size
= size
;
489 #endif /* HAVE_DWARF_UNWIND_SUPPORT */
490 } else if (!strncmp(name
, "lbr", sizeof("lbr"))) {
491 if (!strtok_r(NULL
, ",", &saveptr
)) {
492 param
->record_mode
= CALLCHAIN_LBR
;
495 pr_err("callchain: No more arguments "
496 "needed for --call-graph lbr\n");
499 pr_err("callchain: Unknown --call-graph option "
510 int filename__read_str(const char *filename
, char **buf
, size_t *sizep
)
512 size_t size
= 0, alloc_size
= 0;
513 void *bf
= NULL
, *nbf
;
515 char sbuf
[STRERR_BUFSIZE
];
517 fd
= open(filename
, O_RDONLY
);
522 if (size
== alloc_size
) {
523 alloc_size
+= BUFSIZ
;
524 nbf
= realloc(bf
, alloc_size
);
533 n
= read(fd
, bf
+ size
, alloc_size
- size
);
536 pr_warning("read failed %d: %s\n", errno
,
537 strerror_r(errno
, sbuf
, sizeof(sbuf
)));
558 const char *get_filename_for_perf_kvm(void)
560 const char *filename
;
562 if (perf_host
&& !perf_guest
)
563 filename
= strdup("perf.data.host");
564 else if (!perf_host
&& perf_guest
)
565 filename
= strdup("perf.data.guest");
567 filename
= strdup("perf.data.kvm");
572 int perf_event_paranoid(void)
576 if (sysctl__read_int("kernel/perf_event_paranoid", &value
))
582 void mem_bswap_32(void *src
, int byte_size
)
585 while (byte_size
> 0) {
587 byte_size
-= sizeof(u32
);
592 void mem_bswap_64(void *src
, int byte_size
)
596 while (byte_size
> 0) {
598 byte_size
-= sizeof(u64
);
603 bool find_process(const char *name
)
605 size_t len
= strlen(name
);
610 dir
= opendir(procfs__mountpoint());
614 /* Walk through the directory. */
615 while (ret
&& (d
= readdir(dir
)) != NULL
) {
620 if ((d
->d_type
!= DT_DIR
) ||
621 !strcmp(".", d
->d_name
) ||
622 !strcmp("..", d
->d_name
))
625 scnprintf(path
, sizeof(path
), "%s/%s/comm",
626 procfs__mountpoint(), d
->d_name
);
628 if (filename__read_str(path
, &data
, &size
))
631 ret
= strncmp(name
, data
, len
);
636 return ret
? false : true;
640 fetch_kernel_version(unsigned int *puint
, char *str
,
643 struct utsname utsname
;
644 int version
, patchlevel
, sublevel
, err
;
649 if (str
&& str_size
) {
650 strncpy(str
, utsname
.release
, str_size
);
651 str
[str_size
- 1] = '\0';
654 err
= sscanf(utsname
.release
, "%d.%d.%d",
655 &version
, &patchlevel
, &sublevel
);
658 pr_debug("Unablt to get kernel version from uname '%s'\n",
664 *puint
= (version
<< 16) + (patchlevel
<< 8) + sublevel
;
668 const char *perf_tip(const char *dirpath
)
670 struct strlist
*tips
;
671 struct str_node
*node
;
673 struct strlist_config conf
= {
678 tips
= strlist__new("tips.txt", &conf
);
680 return errno
== ENOENT
? NULL
: "Tip: get more memory! ;-p";
682 if (strlist__nr_entries(tips
) == 0)
685 node
= strlist__entry(tips
, random() % strlist__nr_entries(tips
));
686 if (asprintf(&tip
, "Tip: %s", node
->s
) < 0)
687 tip
= (char *)"Tip: get more memory! ;-)";
690 strlist__delete(tips
);