5 #ifdef HAVE_BACKTRACE_SUPPORT
14 #include <linux/kernel.h>
17 * XXX We need to find a better place for these things...
19 unsigned int page_size;
22 bool test_attr__enabled;
24 bool perf_host = true;
25 bool perf_guest = false;
27 char tracing_events_path[PATH_MAX + 1] = "/sys/kernel/debug/tracing/events";
29 void event_attr_init(struct perf_event_attr *attr)
32 attr->exclude_host = 1;
34 attr->exclude_guest = 1;
35 /* to capture ABI version */
36 attr->size = sizeof(*attr);
39 int mkdir_p(char *path, mode_t mode)
48 if (stat(path, &st) == 0)
53 while ((d = strchr(d, '/'))) {
55 err = stat(path, &st) && mkdir(path, mode);
62 return (stat(path, &st) && mkdir(path, mode)) ? -1 : 0;
65 static int slow_copyfile(const char *from, const char *to, mode_t mode)
70 FILE *from_fp = fopen(from, "r"), *to_fp;
76 old_umask = umask(mode ^ 0777);
77 to_fp = fopen(to, "w");
82 while (getline(&line, &n, from_fp) > 0)
83 if (fputs(line, to_fp) == EOF)
95 int copyfile_mode(const char *from, const char *to, mode_t mode)
105 if (st.st_size == 0) /* /proc? do it slowly... */
106 return slow_copyfile(from, to, mode);
108 fromfd = open(from, O_RDONLY);
112 tofd = creat(to, mode);
116 addr = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fromfd, 0);
117 if (addr == MAP_FAILED)
120 if (write(tofd, addr, st.st_size) == st.st_size)
123 munmap(addr, st.st_size);
134 int copyfile(const char *from, const char *to)
136 return copyfile_mode(from, to, 0755);
139 unsigned long convert_unit(unsigned long value, char *unit)
161 static ssize_t ion(bool is_read, int fd, void *buf, size_t n)
163 void *buf_start = buf;
167 ssize_t ret = is_read ? read(fd, buf, left) :
168 write(fd, buf, left);
170 if (ret < 0 && errno == EINTR)
179 BUG_ON((size_t)(buf - buf_start) != n);
184 * Read exactly 'n' bytes or return an error.
186 ssize_t readn(int fd, void *buf, size_t n)
188 return ion(true, fd, buf, n);
192 * Write exactly 'n' bytes or return an error.
194 ssize_t writen(int fd, void *buf, size_t n)
196 return ion(false, fd, buf, n);
199 size_t hex_width(u64 v)
209 static int hex(char ch)
211 if ((ch >= '0') && (ch <= '9'))
213 if ((ch >= 'a') && (ch <= 'f'))
214 return ch - 'a' + 10;
215 if ((ch >= 'A') && (ch <= 'F'))
216 return ch - 'A' + 10;
221 * While we find nice hex chars, build a long_val.
222 * Return number of chars processed.
224 int hex2u64(const char *ptr, u64 *long_val)
230 const int hex_val = hex(*p);
235 *long_val = (*long_val << 4) | hex_val;
242 /* Obtain a backtrace and print it to stdout. */
243 #ifdef HAVE_BACKTRACE_SUPPORT
244 void dump_stack(void)
247 size_t size = backtrace(array, ARRAY_SIZE(array));
248 char **strings = backtrace_symbols(array, size);
251 printf("Obtained %zd stack frames.\n", size);
253 for (i = 0; i < size; i++)
254 printf("%s\n", strings[i]);
259 void dump_stack(void) {}
262 void get_term_dimensions(struct winsize *ws)
264 char *s = getenv("LINES");
267 ws->ws_row = atoi(s);
268 s = getenv("COLUMNS");
270 ws->ws_col = atoi(s);
271 if (ws->ws_row && ws->ws_col)
276 if (ioctl(1, TIOCGWINSZ, ws) == 0 &&
277 ws->ws_row && ws->ws_col)
284 static void set_tracing_events_path(const char *mountpoint)
286 snprintf(tracing_events_path, sizeof(tracing_events_path), "%s/%s",
287 mountpoint, "tracing/events");
290 const char *perf_debugfs_mount(const char *mountpoint)
294 mnt = debugfs_mount(mountpoint);
298 set_tracing_events_path(mnt);
303 void perf_debugfs_set_path(const char *mntpt)
305 snprintf(debugfs_mountpoint, strlen(debugfs_mountpoint), "%s", mntpt);
306 set_tracing_events_path(mntpt);
309 static const char *find_debugfs(void)
311 const char *path = perf_debugfs_mount(NULL);
314 fprintf(stderr, "Your kernel does not support the debugfs filesystem");
320 * Finds the path to the debugfs/tracing
321 * Allocates the string and stores it.
323 const char *find_tracing_dir(void)
325 static char *tracing;
326 static int tracing_found;
332 debugfs = find_debugfs();
336 tracing = malloc(strlen(debugfs) + 9);
340 sprintf(tracing, "%s/tracing", debugfs);
346 char *get_tracing_file(const char *name)
351 tracing = find_tracing_dir();
355 file = malloc(strlen(tracing) + strlen(name) + 2);
359 sprintf(file, "%s/%s", tracing, name);
363 void put_tracing_file(char *file)
368 int parse_nsec_time(const char *str, u64 *ptime)
370 u64 time_sec, time_nsec;
373 time_sec = strtoul(str, &end, 10);
374 if (*end != '.' && *end != '\0')
381 if (strlen(++end) > 9)
384 strncpy(nsec_buf, end, 9);
387 /* make it nsec precision */
388 for (i = strlen(nsec_buf); i < 9; i++)
391 time_nsec = strtoul(nsec_buf, &end, 10);
397 *ptime = time_sec * NSEC_PER_SEC + time_nsec;
401 unsigned long parse_tag_value(const char *str, struct parse_tag *tags)
403 struct parse_tag *i = tags;
408 s = strchr(str, i->tag);
410 unsigned long int value;
413 value = strtoul(str, &endptr, 10);
417 if (value > ULONG_MAX / i->mult)
425 return (unsigned long) -1;
428 int filename__read_int(const char *filename, int *value)
431 int fd = open(filename, O_RDONLY), err = -1;
436 if (read(fd, line, sizeof(line)) > 0) {
445 int filename__read_str(const char *filename, char **buf, size_t *sizep)
447 size_t size = 0, alloc_size = 0;
448 void *bf = NULL, *nbf;
451 fd = open(filename, O_RDONLY);
456 if (size == alloc_size) {
457 alloc_size += BUFSIZ;
458 nbf = realloc(bf, alloc_size);
467 n = read(fd, bf + size, alloc_size - size);
470 pr_warning("read failed %d: %s\n",
471 errno, strerror(errno));
492 const char *get_filename_for_perf_kvm(void)
494 const char *filename;
496 if (perf_host && !perf_guest)
497 filename = strdup("perf.data.host");
498 else if (!perf_host && perf_guest)
499 filename = strdup("perf.data.guest");
501 filename = strdup("perf.data.kvm");
506 int perf_event_paranoid(void)
509 const char *procfs = procfs__mountpoint();
515 scnprintf(path, PATH_MAX, "%s/sys/kernel/perf_event_paranoid", procfs);
517 if (filename__read_int(path, &value))
523 void mem_bswap_32(void *src, int byte_size)
526 while (byte_size > 0) {
528 byte_size -= sizeof(u32);
533 void mem_bswap_64(void *src, int byte_size)
537 while (byte_size > 0) {
539 byte_size -= sizeof(u64);