6 #ifdef HAVE_BACKTRACE_SUPPORT
15 #include <linux/kernel.h>
17 #include "callchain.h"
19 struct callchain_param callchain_param = {
20 .mode = CHAIN_GRAPH_REL,
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 char tracing_events_path[PATH_MAX + 1] = "/sys/kernel/debug/tracing/events";
39 void event_attr_init(struct perf_event_attr *attr)
42 attr->exclude_host = 1;
44 attr->exclude_guest = 1;
45 /* to capture ABI version */
46 attr->size = sizeof(*attr);
49 int mkdir_p(char *path, mode_t mode)
58 if (stat(path, &st) == 0)
63 while ((d = strchr(d, '/'))) {
65 err = stat(path, &st) && mkdir(path, mode);
72 return (stat(path, &st) && mkdir(path, mode)) ? -1 : 0;
80 char namebuf[PATH_MAX];
86 while ((d = readdir(dir)) != NULL && !ret) {
89 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
92 scnprintf(namebuf, sizeof(namebuf), "%s/%s",
95 ret = stat(namebuf, &statbuf);
97 pr_debug("stat failed: %s\n", namebuf);
101 if (S_ISREG(statbuf.st_mode))
102 ret = unlink(namebuf);
103 else if (S_ISDIR(statbuf.st_mode))
104 ret = rm_rf(namebuf);
106 pr_debug("unknown file: %s\n", namebuf);
118 static int slow_copyfile(const char *from, const char *to)
123 FILE *from_fp = fopen(from, "r"), *to_fp;
128 to_fp = fopen(to, "w");
130 goto out_fclose_from;
132 while (getline(&line, &n, from_fp) > 0)
133 if (fputs(line, to_fp) == EOF)
145 int copyfile_offset(int ifd, loff_t off_in, int ofd, loff_t off_out, u64 size)
150 pgoff = off_in & ~(page_size - 1);
153 ptr = mmap(NULL, off_in + size, PROT_READ, MAP_PRIVATE, ifd, pgoff);
154 if (ptr == MAP_FAILED)
158 ssize_t ret = pwrite(ofd, ptr + off_in, size, off_out);
159 if (ret < 0 && errno == EINTR)
168 munmap(ptr, off_in + size);
170 return size ? -1 : 0;
173 int copyfile_mode(const char *from, const char *to, mode_t mode)
178 char *tmp = NULL, *ptr = NULL;
183 /* extra 'x' at the end is to reserve space for '.' */
184 if (asprintf(&tmp, "%s.XXXXXXx", to) < 0) {
188 ptr = strrchr(tmp, '/');
191 ptr = memmove(ptr + 1, ptr, strlen(ptr) - 1);
198 if (fchmod(tofd, mode))
201 if (st.st_size == 0) { /* /proc? do it slowly... */
202 err = slow_copyfile(from, tmp);
206 fromfd = open(from, O_RDONLY);
210 err = copyfile_offset(fromfd, 0, tofd, 0, st.st_size);
223 int copyfile(const char *from, const char *to)
225 return copyfile_mode(from, to, 0755);
228 unsigned long convert_unit(unsigned long value, char *unit)
250 static ssize_t ion(bool is_read, int fd, void *buf, size_t n)
252 void *buf_start = buf;
256 ssize_t ret = is_read ? read(fd, buf, left) :
257 write(fd, buf, left);
259 if (ret < 0 && errno == EINTR)
268 BUG_ON((size_t)(buf - buf_start) != n);
273 * Read exactly 'n' bytes or return an error.
275 ssize_t readn(int fd, void *buf, size_t n)
277 return ion(true, fd, buf, n);
281 * Write exactly 'n' bytes or return an error.
283 ssize_t writen(int fd, void *buf, size_t n)
285 return ion(false, fd, buf, n);
288 size_t hex_width(u64 v)
298 static int hex(char ch)
300 if ((ch >= '0') && (ch <= '9'))
302 if ((ch >= 'a') && (ch <= 'f'))
303 return ch - 'a' + 10;
304 if ((ch >= 'A') && (ch <= 'F'))
305 return ch - 'A' + 10;
310 * While we find nice hex chars, build a long_val.
311 * Return number of chars processed.
313 int hex2u64(const char *ptr, u64 *long_val)
319 const int hex_val = hex(*p);
324 *long_val = (*long_val << 4) | hex_val;
331 /* Obtain a backtrace and print it to stdout. */
332 #ifdef HAVE_BACKTRACE_SUPPORT
333 void dump_stack(void)
336 size_t size = backtrace(array, ARRAY_SIZE(array));
337 char **strings = backtrace_symbols(array, size);
340 printf("Obtained %zd stack frames.\n", size);
342 for (i = 0; i < size; i++)
343 printf("%s\n", strings[i]);
348 void dump_stack(void) {}
351 void sighandler_dump_stack(int sig)
353 psignal(sig, "perf");
358 void get_term_dimensions(struct winsize *ws)
360 char *s = getenv("LINES");
363 ws->ws_row = atoi(s);
364 s = getenv("COLUMNS");
366 ws->ws_col = atoi(s);
367 if (ws->ws_row && ws->ws_col)
372 if (ioctl(1, TIOCGWINSZ, ws) == 0 &&
373 ws->ws_row && ws->ws_col)
380 void set_term_quiet_input(struct termios *old)
386 tc.c_lflag &= ~(ICANON | ECHO);
389 tcsetattr(0, TCSANOW, &tc);
392 static void set_tracing_events_path(const char *tracing, const char *mountpoint)
394 snprintf(tracing_events_path, sizeof(tracing_events_path), "%s/%s%s",
395 mountpoint, tracing, "events");
398 static const char *__perf_tracefs_mount(const char *mountpoint)
402 mnt = tracefs_mount(mountpoint);
406 set_tracing_events_path("", mnt);
411 static const char *__perf_debugfs_mount(const char *mountpoint)
415 mnt = debugfs_mount(mountpoint);
419 set_tracing_events_path("tracing/", mnt);
424 const char *perf_debugfs_mount(const char *mountpoint)
428 mnt = __perf_tracefs_mount(mountpoint);
432 mnt = __perf_debugfs_mount(mountpoint);
437 void perf_debugfs_set_path(const char *mntpt)
439 snprintf(debugfs_mountpoint, strlen(debugfs_mountpoint), "%s", mntpt);
440 set_tracing_events_path("tracing/", mntpt);
443 static const char *find_tracefs(void)
445 const char *path = __perf_tracefs_mount(NULL);
450 static const char *find_debugfs(void)
452 const char *path = __perf_debugfs_mount(NULL);
455 fprintf(stderr, "Your kernel does not support the debugfs filesystem");
461 * Finds the path to the debugfs/tracing
462 * Allocates the string and stores it.
464 const char *find_tracing_dir(void)
466 const char *tracing_dir = "";
467 static char *tracing;
468 static int tracing_found;
474 debugfs = find_tracefs();
476 tracing_dir = "/tracing";
477 debugfs = find_debugfs();
482 if (asprintf(&tracing, "%s%s", debugfs, tracing_dir) < 0)
489 char *get_tracing_file(const char *name)
494 tracing = find_tracing_dir();
498 if (asprintf(&file, "%s/%s", tracing, name) < 0)
504 void put_tracing_file(char *file)
509 int parse_nsec_time(const char *str, u64 *ptime)
511 u64 time_sec, time_nsec;
514 time_sec = strtoul(str, &end, 10);
515 if (*end != '.' && *end != '\0')
522 if (strlen(++end) > 9)
525 strncpy(nsec_buf, end, 9);
528 /* make it nsec precision */
529 for (i = strlen(nsec_buf); i < 9; i++)
532 time_nsec = strtoul(nsec_buf, &end, 10);
538 *ptime = time_sec * NSEC_PER_SEC + time_nsec;
542 unsigned long parse_tag_value(const char *str, struct parse_tag *tags)
544 struct parse_tag *i = tags;
549 s = strchr(str, i->tag);
551 unsigned long int value;
554 value = strtoul(str, &endptr, 10);
558 if (value > ULONG_MAX / i->mult)
566 return (unsigned long) -1;
569 int filename__read_str(const char *filename, char **buf, size_t *sizep)
571 size_t size = 0, alloc_size = 0;
572 void *bf = NULL, *nbf;
574 char sbuf[STRERR_BUFSIZE];
576 fd = open(filename, O_RDONLY);
581 if (size == alloc_size) {
582 alloc_size += BUFSIZ;
583 nbf = realloc(bf, alloc_size);
592 n = read(fd, bf + size, alloc_size - size);
595 pr_warning("read failed %d: %s\n", errno,
596 strerror_r(errno, sbuf, sizeof(sbuf)));
617 const char *get_filename_for_perf_kvm(void)
619 const char *filename;
621 if (perf_host && !perf_guest)
622 filename = strdup("perf.data.host");
623 else if (!perf_host && perf_guest)
624 filename = strdup("perf.data.guest");
626 filename = strdup("perf.data.kvm");
631 int perf_event_paranoid(void)
635 if (sysctl__read_int("kernel/perf_event_paranoid", &value))
641 void mem_bswap_32(void *src, int byte_size)
644 while (byte_size > 0) {
646 byte_size -= sizeof(u32);
651 void mem_bswap_64(void *src, int byte_size)
655 while (byte_size > 0) {
657 byte_size -= sizeof(u64);
662 bool find_process(const char *name)
664 size_t len = strlen(name);
669 dir = opendir(procfs__mountpoint());
673 /* Walk through the directory. */
674 while (ret && (d = readdir(dir)) != NULL) {
679 if ((d->d_type != DT_DIR) ||
680 !strcmp(".", d->d_name) ||
681 !strcmp("..", d->d_name))
684 scnprintf(path, sizeof(path), "%s/%s/comm",
685 procfs__mountpoint(), d->d_name);
687 if (filename__read_str(path, &data, &size))
690 ret = strncmp(name, data, len);
695 return ret ? false : true;