6 #ifdef HAVE_BACKTRACE_SUPPORT
15 #include <linux/kernel.h>
19 * XXX We need to find a better place for these things...
21 unsigned int page_size;
24 bool test_attr__enabled;
26 bool perf_host = true;
27 bool perf_guest = false;
29 char tracing_events_path[PATH_MAX + 1] = "/sys/kernel/debug/tracing/events";
31 void event_attr_init(struct perf_event_attr *attr)
34 attr->exclude_host = 1;
36 attr->exclude_guest = 1;
37 /* to capture ABI version */
38 attr->size = sizeof(*attr);
41 int mkdir_p(char *path, mode_t mode)
50 if (stat(path, &st) == 0)
55 while ((d = strchr(d, '/'))) {
57 err = stat(path, &st) && mkdir(path, mode);
64 return (stat(path, &st) && mkdir(path, mode)) ? -1 : 0;
67 static int slow_copyfile(const char *from, const char *to, mode_t mode)
72 FILE *from_fp = fopen(from, "r"), *to_fp;
78 old_umask = umask(mode ^ 0777);
79 to_fp = fopen(to, "w");
84 while (getline(&line, &n, from_fp) > 0)
85 if (fputs(line, to_fp) == EOF)
97 int copyfile_mode(const char *from, const char *to, mode_t mode)
107 if (st.st_size == 0) /* /proc? do it slowly... */
108 return slow_copyfile(from, to, mode);
110 fromfd = open(from, O_RDONLY);
114 tofd = creat(to, mode);
118 addr = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fromfd, 0);
119 if (addr == MAP_FAILED)
122 if (write(tofd, addr, st.st_size) == st.st_size)
125 munmap(addr, st.st_size);
136 int copyfile(const char *from, const char *to)
138 return copyfile_mode(from, to, 0755);
141 unsigned long convert_unit(unsigned long value, char *unit)
163 static ssize_t ion(bool is_read, int fd, void *buf, size_t n)
165 void *buf_start = buf;
169 ssize_t ret = is_read ? read(fd, buf, left) :
170 write(fd, buf, left);
172 if (ret < 0 && errno == EINTR)
181 BUG_ON((size_t)(buf - buf_start) != n);
186 * Read exactly 'n' bytes or return an error.
188 ssize_t readn(int fd, void *buf, size_t n)
190 return ion(true, fd, buf, n);
194 * Write exactly 'n' bytes or return an error.
196 ssize_t writen(int fd, void *buf, size_t n)
198 return ion(false, fd, buf, n);
201 size_t hex_width(u64 v)
211 static int hex(char ch)
213 if ((ch >= '0') && (ch <= '9'))
215 if ((ch >= 'a') && (ch <= 'f'))
216 return ch - 'a' + 10;
217 if ((ch >= 'A') && (ch <= 'F'))
218 return ch - 'A' + 10;
223 * While we find nice hex chars, build a long_val.
224 * Return number of chars processed.
226 int hex2u64(const char *ptr, u64 *long_val)
232 const int hex_val = hex(*p);
237 *long_val = (*long_val << 4) | hex_val;
244 /* Obtain a backtrace and print it to stdout. */
245 #ifdef HAVE_BACKTRACE_SUPPORT
246 void dump_stack(void)
249 size_t size = backtrace(array, ARRAY_SIZE(array));
250 char **strings = backtrace_symbols(array, size);
253 printf("Obtained %zd stack frames.\n", size);
255 for (i = 0; i < size; i++)
256 printf("%s\n", strings[i]);
261 void dump_stack(void) {}
264 void get_term_dimensions(struct winsize *ws)
266 char *s = getenv("LINES");
269 ws->ws_row = atoi(s);
270 s = getenv("COLUMNS");
272 ws->ws_col = atoi(s);
273 if (ws->ws_row && ws->ws_col)
278 if (ioctl(1, TIOCGWINSZ, ws) == 0 &&
279 ws->ws_row && ws->ws_col)
286 void set_term_quiet_input(struct termios *old)
292 tc.c_lflag &= ~(ICANON | ECHO);
295 tcsetattr(0, TCSANOW, &tc);
298 static void set_tracing_events_path(const char *mountpoint)
300 snprintf(tracing_events_path, sizeof(tracing_events_path), "%s/%s",
301 mountpoint, "tracing/events");
304 const char *perf_debugfs_mount(const char *mountpoint)
308 mnt = debugfs_mount(mountpoint);
312 set_tracing_events_path(mnt);
317 void perf_debugfs_set_path(const char *mntpt)
319 snprintf(debugfs_mountpoint, strlen(debugfs_mountpoint), "%s", mntpt);
320 set_tracing_events_path(mntpt);
323 static const char *find_debugfs(void)
325 const char *path = perf_debugfs_mount(NULL);
328 fprintf(stderr, "Your kernel does not support the debugfs filesystem");
334 * Finds the path to the debugfs/tracing
335 * Allocates the string and stores it.
337 const char *find_tracing_dir(void)
339 static char *tracing;
340 static int tracing_found;
346 debugfs = find_debugfs();
350 if (asprintf(&tracing, "%s/tracing", debugfs) < 0)
357 char *get_tracing_file(const char *name)
362 tracing = find_tracing_dir();
366 if (asprintf(&file, "%s/%s", tracing, name) < 0)
372 void put_tracing_file(char *file)
377 int parse_nsec_time(const char *str, u64 *ptime)
379 u64 time_sec, time_nsec;
382 time_sec = strtoul(str, &end, 10);
383 if (*end != '.' && *end != '\0')
390 if (strlen(++end) > 9)
393 strncpy(nsec_buf, end, 9);
396 /* make it nsec precision */
397 for (i = strlen(nsec_buf); i < 9; i++)
400 time_nsec = strtoul(nsec_buf, &end, 10);
406 *ptime = time_sec * NSEC_PER_SEC + time_nsec;
410 unsigned long parse_tag_value(const char *str, struct parse_tag *tags)
412 struct parse_tag *i = tags;
417 s = strchr(str, i->tag);
419 unsigned long int value;
422 value = strtoul(str, &endptr, 10);
426 if (value > ULONG_MAX / i->mult)
434 return (unsigned long) -1;
437 int filename__read_int(const char *filename, int *value)
440 int fd = open(filename, O_RDONLY), err = -1;
445 if (read(fd, line, sizeof(line)) > 0) {
454 int filename__read_str(const char *filename, char **buf, size_t *sizep)
456 size_t size = 0, alloc_size = 0;
457 void *bf = NULL, *nbf;
459 char sbuf[STRERR_BUFSIZE];
461 fd = open(filename, O_RDONLY);
466 if (size == alloc_size) {
467 alloc_size += BUFSIZ;
468 nbf = realloc(bf, alloc_size);
477 n = read(fd, bf + size, alloc_size - size);
480 pr_warning("read failed %d: %s\n", errno,
481 strerror_r(errno, sbuf, sizeof(sbuf)));
502 const char *get_filename_for_perf_kvm(void)
504 const char *filename;
506 if (perf_host && !perf_guest)
507 filename = strdup("perf.data.host");
508 else if (!perf_host && perf_guest)
509 filename = strdup("perf.data.guest");
511 filename = strdup("perf.data.kvm");
516 int perf_event_paranoid(void)
519 const char *procfs = procfs__mountpoint();
525 scnprintf(path, PATH_MAX, "%s/sys/kernel/perf_event_paranoid", procfs);
527 if (filename__read_int(path, &value))
533 void mem_bswap_32(void *src, int byte_size)
536 while (byte_size > 0) {
538 byte_size -= sizeof(u32);
543 void mem_bswap_64(void *src, int byte_size)
547 while (byte_size > 0) {
549 byte_size -= sizeof(u64);
554 bool find_process(const char *name)
556 size_t len = strlen(name);
561 dir = opendir(procfs__mountpoint());
565 /* Walk through the directory. */
566 while (ret && (d = readdir(dir)) != NULL) {
571 if ((d->d_type != DT_DIR) ||
572 !strcmp(".", d->d_name) ||
573 !strcmp("..", d->d_name))
576 scnprintf(path, sizeof(path), "%s/%s/comm",
577 procfs__mountpoint(), d->d_name);
579 if (filename__read_str(path, &data, &size))
582 ret = strncmp(name, data, len);
587 return ret ? false : true;