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;
75 static int slow_copyfile(const char *from, const char *to, mode_t mode)
80 FILE *from_fp = fopen(from, "r"), *to_fp;
86 old_umask = umask(mode ^ 0777);
87 to_fp = fopen(to, "w");
92 while (getline(&line, &n, from_fp) > 0)
93 if (fputs(line, to_fp) == EOF)
105 int copyfile_mode(const char *from, const char *to, mode_t mode)
115 if (st.st_size == 0) /* /proc? do it slowly... */
116 return slow_copyfile(from, to, mode);
118 fromfd = open(from, O_RDONLY);
122 tofd = creat(to, mode);
126 addr = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fromfd, 0);
127 if (addr == MAP_FAILED)
130 if (write(tofd, addr, st.st_size) == st.st_size)
133 munmap(addr, st.st_size);
144 int copyfile(const char *from, const char *to)
146 return copyfile_mode(from, to, 0755);
149 unsigned long convert_unit(unsigned long value, char *unit)
171 static ssize_t ion(bool is_read, int fd, void *buf, size_t n)
173 void *buf_start = buf;
177 ssize_t ret = is_read ? read(fd, buf, left) :
178 write(fd, buf, left);
180 if (ret < 0 && errno == EINTR)
189 BUG_ON((size_t)(buf - buf_start) != n);
194 * Read exactly 'n' bytes or return an error.
196 ssize_t readn(int fd, void *buf, size_t n)
198 return ion(true, fd, buf, n);
202 * Write exactly 'n' bytes or return an error.
204 ssize_t writen(int fd, void *buf, size_t n)
206 return ion(false, fd, buf, n);
209 size_t hex_width(u64 v)
219 static int hex(char ch)
221 if ((ch >= '0') && (ch <= '9'))
223 if ((ch >= 'a') && (ch <= 'f'))
224 return ch - 'a' + 10;
225 if ((ch >= 'A') && (ch <= 'F'))
226 return ch - 'A' + 10;
231 * While we find nice hex chars, build a long_val.
232 * Return number of chars processed.
234 int hex2u64(const char *ptr, u64 *long_val)
240 const int hex_val = hex(*p);
245 *long_val = (*long_val << 4) | hex_val;
252 /* Obtain a backtrace and print it to stdout. */
253 #ifdef HAVE_BACKTRACE_SUPPORT
254 void dump_stack(void)
257 size_t size = backtrace(array, ARRAY_SIZE(array));
258 char **strings = backtrace_symbols(array, size);
261 printf("Obtained %zd stack frames.\n", size);
263 for (i = 0; i < size; i++)
264 printf("%s\n", strings[i]);
269 void dump_stack(void) {}
272 void get_term_dimensions(struct winsize *ws)
274 char *s = getenv("LINES");
277 ws->ws_row = atoi(s);
278 s = getenv("COLUMNS");
280 ws->ws_col = atoi(s);
281 if (ws->ws_row && ws->ws_col)
286 if (ioctl(1, TIOCGWINSZ, ws) == 0 &&
287 ws->ws_row && ws->ws_col)
294 void set_term_quiet_input(struct termios *old)
300 tc.c_lflag &= ~(ICANON | ECHO);
303 tcsetattr(0, TCSANOW, &tc);
306 static void set_tracing_events_path(const char *mountpoint)
308 snprintf(tracing_events_path, sizeof(tracing_events_path), "%s/%s",
309 mountpoint, "tracing/events");
312 const char *perf_debugfs_mount(const char *mountpoint)
316 mnt = debugfs_mount(mountpoint);
320 set_tracing_events_path(mnt);
325 void perf_debugfs_set_path(const char *mntpt)
327 snprintf(debugfs_mountpoint, strlen(debugfs_mountpoint), "%s", mntpt);
328 set_tracing_events_path(mntpt);
331 static const char *find_debugfs(void)
333 const char *path = perf_debugfs_mount(NULL);
336 fprintf(stderr, "Your kernel does not support the debugfs filesystem");
342 * Finds the path to the debugfs/tracing
343 * Allocates the string and stores it.
345 const char *find_tracing_dir(void)
347 static char *tracing;
348 static int tracing_found;
354 debugfs = find_debugfs();
358 if (asprintf(&tracing, "%s/tracing", debugfs) < 0)
365 char *get_tracing_file(const char *name)
370 tracing = find_tracing_dir();
374 if (asprintf(&file, "%s/%s", tracing, name) < 0)
380 void put_tracing_file(char *file)
385 int parse_nsec_time(const char *str, u64 *ptime)
387 u64 time_sec, time_nsec;
390 time_sec = strtoul(str, &end, 10);
391 if (*end != '.' && *end != '\0')
398 if (strlen(++end) > 9)
401 strncpy(nsec_buf, end, 9);
404 /* make it nsec precision */
405 for (i = strlen(nsec_buf); i < 9; i++)
408 time_nsec = strtoul(nsec_buf, &end, 10);
414 *ptime = time_sec * NSEC_PER_SEC + time_nsec;
418 unsigned long parse_tag_value(const char *str, struct parse_tag *tags)
420 struct parse_tag *i = tags;
425 s = strchr(str, i->tag);
427 unsigned long int value;
430 value = strtoul(str, &endptr, 10);
434 if (value > ULONG_MAX / i->mult)
442 return (unsigned long) -1;
445 int filename__read_int(const char *filename, int *value)
448 int fd = open(filename, O_RDONLY), err = -1;
453 if (read(fd, line, sizeof(line)) > 0) {
462 int filename__read_str(const char *filename, char **buf, size_t *sizep)
464 size_t size = 0, alloc_size = 0;
465 void *bf = NULL, *nbf;
467 char sbuf[STRERR_BUFSIZE];
469 fd = open(filename, O_RDONLY);
474 if (size == alloc_size) {
475 alloc_size += BUFSIZ;
476 nbf = realloc(bf, alloc_size);
485 n = read(fd, bf + size, alloc_size - size);
488 pr_warning("read failed %d: %s\n", errno,
489 strerror_r(errno, sbuf, sizeof(sbuf)));
510 const char *get_filename_for_perf_kvm(void)
512 const char *filename;
514 if (perf_host && !perf_guest)
515 filename = strdup("perf.data.host");
516 else if (!perf_host && perf_guest)
517 filename = strdup("perf.data.guest");
519 filename = strdup("perf.data.kvm");
524 int perf_event_paranoid(void)
527 const char *procfs = procfs__mountpoint();
533 scnprintf(path, PATH_MAX, "%s/sys/kernel/perf_event_paranoid", procfs);
535 if (filename__read_int(path, &value))
541 void mem_bswap_32(void *src, int byte_size)
544 while (byte_size > 0) {
546 byte_size -= sizeof(u32);
551 void mem_bswap_64(void *src, int byte_size)
555 while (byte_size > 0) {
557 byte_size -= sizeof(u64);
562 bool find_process(const char *name)
564 size_t len = strlen(name);
569 dir = opendir(procfs__mountpoint());
573 /* Walk through the directory. */
574 while (ret && (d = readdir(dir)) != NULL) {
579 if ((d->d_type != DT_DIR) ||
580 !strcmp(".", d->d_name) ||
581 !strcmp("..", d->d_name))
584 scnprintf(path, sizeof(path), "%s/%s/comm",
585 procfs__mountpoint(), d->d_name);
587 if (filename__read_str(path, &data, &size))
590 ret = strncmp(name, data, len);
595 return ret ? false : true;