6 #ifdef HAVE_BACKTRACE_SUPPORT
15 #include <linux/kernel.h>
17 #include "callchain.h"
19 struct callchain_param callchain_param = {
20 .mode = CHAIN_GRAPH_ABS,
22 .order = ORDER_CALLEE,
27 * XXX We need to find a better place for these things...
29 unsigned int page_size;
32 bool test_attr__enabled;
34 bool perf_host = true;
35 bool perf_guest = false;
37 void event_attr_init(struct perf_event_attr *attr)
40 attr->exclude_host = 1;
42 attr->exclude_guest = 1;
43 /* to capture ABI version */
44 attr->size = sizeof(*attr);
47 int mkdir_p(char *path, mode_t mode)
56 if (stat(path, &st) == 0)
61 while ((d = strchr(d, '/'))) {
63 err = stat(path, &st) && mkdir(path, mode);
70 return (stat(path, &st) && mkdir(path, mode)) ? -1 : 0;
78 char namebuf[PATH_MAX];
84 while ((d = readdir(dir)) != NULL && !ret) {
87 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
90 scnprintf(namebuf, sizeof(namebuf), "%s/%s",
93 ret = stat(namebuf, &statbuf);
95 pr_debug("stat failed: %s\n", namebuf);
99 if (S_ISREG(statbuf.st_mode))
100 ret = unlink(namebuf);
101 else if (S_ISDIR(statbuf.st_mode))
102 ret = rm_rf(namebuf);
104 pr_debug("unknown file: %s\n", namebuf);
116 static int slow_copyfile(const char *from, const char *to)
121 FILE *from_fp = fopen(from, "r"), *to_fp;
126 to_fp = fopen(to, "w");
128 goto out_fclose_from;
130 while (getline(&line, &n, from_fp) > 0)
131 if (fputs(line, to_fp) == EOF)
143 int copyfile_offset(int ifd, loff_t off_in, int ofd, loff_t off_out, u64 size)
148 pgoff = off_in & ~(page_size - 1);
151 ptr = mmap(NULL, off_in + size, PROT_READ, MAP_PRIVATE, ifd, pgoff);
152 if (ptr == MAP_FAILED)
156 ssize_t ret = pwrite(ofd, ptr + off_in, size, off_out);
157 if (ret < 0 && errno == EINTR)
166 munmap(ptr, off_in + size);
168 return size ? -1 : 0;
171 int copyfile_mode(const char *from, const char *to, mode_t mode)
176 char *tmp = NULL, *ptr = NULL;
181 /* extra 'x' at the end is to reserve space for '.' */
182 if (asprintf(&tmp, "%s.XXXXXXx", to) < 0) {
186 ptr = strrchr(tmp, '/');
189 ptr = memmove(ptr + 1, ptr, strlen(ptr) - 1);
196 if (fchmod(tofd, mode))
199 if (st.st_size == 0) { /* /proc? do it slowly... */
200 err = slow_copyfile(from, tmp);
204 fromfd = open(from, O_RDONLY);
208 err = copyfile_offset(fromfd, 0, tofd, 0, st.st_size);
221 int copyfile(const char *from, const char *to)
223 return copyfile_mode(from, to, 0755);
226 unsigned long convert_unit(unsigned long value, char *unit)
248 static ssize_t ion(bool is_read, int fd, void *buf, size_t n)
250 void *buf_start = buf;
254 ssize_t ret = is_read ? read(fd, buf, left) :
255 write(fd, buf, left);
257 if (ret < 0 && errno == EINTR)
266 BUG_ON((size_t)(buf - buf_start) != n);
271 * Read exactly 'n' bytes or return an error.
273 ssize_t readn(int fd, void *buf, size_t n)
275 return ion(true, fd, buf, n);
279 * Write exactly 'n' bytes or return an error.
281 ssize_t writen(int fd, void *buf, size_t n)
283 return ion(false, fd, buf, n);
286 size_t hex_width(u64 v)
296 static int hex(char ch)
298 if ((ch >= '0') && (ch <= '9'))
300 if ((ch >= 'a') && (ch <= 'f'))
301 return ch - 'a' + 10;
302 if ((ch >= 'A') && (ch <= 'F'))
303 return ch - 'A' + 10;
308 * While we find nice hex chars, build a long_val.
309 * Return number of chars processed.
311 int hex2u64(const char *ptr, u64 *long_val)
317 const int hex_val = hex(*p);
322 *long_val = (*long_val << 4) | hex_val;
329 /* Obtain a backtrace and print it to stdout. */
330 #ifdef HAVE_BACKTRACE_SUPPORT
331 void dump_stack(void)
334 size_t size = backtrace(array, ARRAY_SIZE(array));
335 char **strings = backtrace_symbols(array, size);
338 printf("Obtained %zd stack frames.\n", size);
340 for (i = 0; i < size; i++)
341 printf("%s\n", strings[i]);
346 void dump_stack(void) {}
349 void sighandler_dump_stack(int sig)
351 psignal(sig, "perf");
356 void get_term_dimensions(struct winsize *ws)
358 char *s = getenv("LINES");
361 ws->ws_row = atoi(s);
362 s = getenv("COLUMNS");
364 ws->ws_col = atoi(s);
365 if (ws->ws_row && ws->ws_col)
370 if (ioctl(1, TIOCGWINSZ, ws) == 0 &&
371 ws->ws_row && ws->ws_col)
378 void set_term_quiet_input(struct termios *old)
384 tc.c_lflag &= ~(ICANON | ECHO);
387 tcsetattr(0, TCSANOW, &tc);
390 int parse_nsec_time(const char *str, u64 *ptime)
392 u64 time_sec, time_nsec;
395 time_sec = strtoul(str, &end, 10);
396 if (*end != '.' && *end != '\0')
403 if (strlen(++end) > 9)
406 strncpy(nsec_buf, end, 9);
409 /* make it nsec precision */
410 for (i = strlen(nsec_buf); i < 9; i++)
413 time_nsec = strtoul(nsec_buf, &end, 10);
419 *ptime = time_sec * NSEC_PER_SEC + time_nsec;
423 unsigned long parse_tag_value(const char *str, struct parse_tag *tags)
425 struct parse_tag *i = tags;
430 s = strchr(str, i->tag);
432 unsigned long int value;
435 value = strtoul(str, &endptr, 10);
439 if (value > ULONG_MAX / i->mult)
447 return (unsigned long) -1;
450 int get_stack_size(const char *str, unsigned long *_size)
454 unsigned long max_size = round_down(USHRT_MAX, sizeof(u64));
456 size = strtoul(str, &endptr, 0);
462 size = round_up(size, sizeof(u64));
463 if (!size || size > max_size)
471 pr_err("callchain: Incorrect stack dump size (max %ld): %s\n",
476 int parse_callchain_record(const char *arg, struct callchain_param *param)
478 char *tok, *name, *saveptr = NULL;
482 /* We need buffer that we know we can write to. */
483 buf = malloc(strlen(arg) + 1);
489 tok = strtok_r((char *)buf, ",", &saveptr);
490 name = tok ? : (char *)buf;
493 /* Framepointer style */
494 if (!strncmp(name, "fp", sizeof("fp"))) {
495 if (!strtok_r(NULL, ",", &saveptr)) {
496 param->record_mode = CALLCHAIN_FP;
499 pr_err("callchain: No more arguments "
500 "needed for --call-graph fp\n");
503 #ifdef HAVE_DWARF_UNWIND_SUPPORT
505 } else if (!strncmp(name, "dwarf", sizeof("dwarf"))) {
506 const unsigned long default_stack_dump_size = 8192;
509 param->record_mode = CALLCHAIN_DWARF;
510 param->dump_size = default_stack_dump_size;
512 tok = strtok_r(NULL, ",", &saveptr);
514 unsigned long size = 0;
516 ret = get_stack_size(tok, &size);
517 param->dump_size = size;
519 #endif /* HAVE_DWARF_UNWIND_SUPPORT */
520 } else if (!strncmp(name, "lbr", sizeof("lbr"))) {
521 if (!strtok_r(NULL, ",", &saveptr)) {
522 param->record_mode = CALLCHAIN_LBR;
525 pr_err("callchain: No more arguments "
526 "needed for --call-graph lbr\n");
529 pr_err("callchain: Unknown --call-graph option "
540 int filename__read_str(const char *filename, char **buf, size_t *sizep)
542 size_t size = 0, alloc_size = 0;
543 void *bf = NULL, *nbf;
545 char sbuf[STRERR_BUFSIZE];
547 fd = open(filename, O_RDONLY);
552 if (size == alloc_size) {
553 alloc_size += BUFSIZ;
554 nbf = realloc(bf, alloc_size);
563 n = read(fd, bf + size, alloc_size - size);
566 pr_warning("read failed %d: %s\n", errno,
567 strerror_r(errno, sbuf, sizeof(sbuf)));
588 const char *get_filename_for_perf_kvm(void)
590 const char *filename;
592 if (perf_host && !perf_guest)
593 filename = strdup("perf.data.host");
594 else if (!perf_host && perf_guest)
595 filename = strdup("perf.data.guest");
597 filename = strdup("perf.data.kvm");
602 int perf_event_paranoid(void)
606 if (sysctl__read_int("kernel/perf_event_paranoid", &value))
612 void mem_bswap_32(void *src, int byte_size)
615 while (byte_size > 0) {
617 byte_size -= sizeof(u32);
622 void mem_bswap_64(void *src, int byte_size)
626 while (byte_size > 0) {
628 byte_size -= sizeof(u64);
633 bool find_process(const char *name)
635 size_t len = strlen(name);
640 dir = opendir(procfs__mountpoint());
644 /* Walk through the directory. */
645 while (ret && (d = readdir(dir)) != NULL) {
650 if ((d->d_type != DT_DIR) ||
651 !strcmp(".", d->d_name) ||
652 !strcmp("..", d->d_name))
655 scnprintf(path, sizeof(path), "%s/%s/comm",
656 procfs__mountpoint(), d->d_name);
658 if (filename__read_str(path, &data, &size))
661 ret = strncmp(name, data, len);
666 return ret ? false : true;