4 #ifdef HAVE_BACKTRACE_SUPPORT
11 #include <linux/kernel.h>
14 * XXX We need to find a better place for these things...
16 unsigned int page_size;
18 bool test_attr__enabled;
20 bool perf_host = true;
21 bool perf_guest = false;
23 char tracing_events_path[PATH_MAX + 1] = "/sys/kernel/debug/tracing/events";
25 void event_attr_init(struct perf_event_attr *attr)
28 attr->exclude_host = 1;
30 attr->exclude_guest = 1;
31 /* to capture ABI version */
32 attr->size = sizeof(*attr);
35 int mkdir_p(char *path, mode_t mode)
44 if (stat(path, &st) == 0)
49 while ((d = strchr(d, '/'))) {
51 err = stat(path, &st) && mkdir(path, mode);
58 return (stat(path, &st) && mkdir(path, mode)) ? -1 : 0;
61 static int slow_copyfile(const char *from, const char *to, mode_t mode)
66 FILE *from_fp = fopen(from, "r"), *to_fp;
72 old_umask = umask(mode ^ 0777);
73 to_fp = fopen(to, "w");
78 while (getline(&line, &n, from_fp) > 0)
79 if (fputs(line, to_fp) == EOF)
91 int copyfile_mode(const char *from, const char *to, mode_t mode)
101 if (st.st_size == 0) /* /proc? do it slowly... */
102 return slow_copyfile(from, to, mode);
104 fromfd = open(from, O_RDONLY);
108 tofd = creat(to, mode);
112 addr = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fromfd, 0);
113 if (addr == MAP_FAILED)
116 if (write(tofd, addr, st.st_size) == st.st_size)
119 munmap(addr, st.st_size);
130 int copyfile(const char *from, const char *to)
132 return copyfile_mode(from, to, 0755);
135 unsigned long convert_unit(unsigned long value, char *unit)
157 static ssize_t ion(bool is_read, int fd, void *buf, size_t n)
159 void *buf_start = buf;
163 ssize_t ret = is_read ? read(fd, buf, left) :
164 write(fd, buf, left);
173 BUG_ON((size_t)(buf - buf_start) != n);
178 * Read exactly 'n' bytes or return an error.
180 ssize_t readn(int fd, void *buf, size_t n)
182 return ion(true, fd, buf, n);
186 * Write exactly 'n' bytes or return an error.
188 ssize_t writen(int fd, void *buf, size_t n)
190 return ion(false, fd, buf, n);
193 size_t hex_width(u64 v)
203 static int hex(char ch)
205 if ((ch >= '0') && (ch <= '9'))
207 if ((ch >= 'a') && (ch <= 'f'))
208 return ch - 'a' + 10;
209 if ((ch >= 'A') && (ch <= 'F'))
210 return ch - 'A' + 10;
215 * While we find nice hex chars, build a long_val.
216 * Return number of chars processed.
218 int hex2u64(const char *ptr, u64 *long_val)
224 const int hex_val = hex(*p);
229 *long_val = (*long_val << 4) | hex_val;
236 /* Obtain a backtrace and print it to stdout. */
237 #ifdef HAVE_BACKTRACE_SUPPORT
238 void dump_stack(void)
241 size_t size = backtrace(array, ARRAY_SIZE(array));
242 char **strings = backtrace_symbols(array, size);
245 printf("Obtained %zd stack frames.\n", size);
247 for (i = 0; i < size; i++)
248 printf("%s\n", strings[i]);
253 void dump_stack(void) {}
256 void get_term_dimensions(struct winsize *ws)
258 char *s = getenv("LINES");
261 ws->ws_row = atoi(s);
262 s = getenv("COLUMNS");
264 ws->ws_col = atoi(s);
265 if (ws->ws_row && ws->ws_col)
270 if (ioctl(1, TIOCGWINSZ, ws) == 0 &&
271 ws->ws_row && ws->ws_col)
278 static void set_tracing_events_path(const char *mountpoint)
280 snprintf(tracing_events_path, sizeof(tracing_events_path), "%s/%s",
281 mountpoint, "tracing/events");
284 const char *perf_debugfs_mount(const char *mountpoint)
288 mnt = debugfs_mount(mountpoint);
292 set_tracing_events_path(mnt);
297 void perf_debugfs_set_path(const char *mntpt)
299 snprintf(debugfs_mountpoint, strlen(debugfs_mountpoint), "%s", mntpt);
300 set_tracing_events_path(mntpt);
303 static const char *find_debugfs(void)
305 const char *path = perf_debugfs_mount(NULL);
308 fprintf(stderr, "Your kernel does not support the debugfs filesystem");
314 * Finds the path to the debugfs/tracing
315 * Allocates the string and stores it.
317 const char *find_tracing_dir(void)
319 static char *tracing;
320 static int tracing_found;
326 debugfs = find_debugfs();
330 tracing = malloc(strlen(debugfs) + 9);
334 sprintf(tracing, "%s/tracing", debugfs);
340 char *get_tracing_file(const char *name)
345 tracing = find_tracing_dir();
349 file = malloc(strlen(tracing) + strlen(name) + 2);
353 sprintf(file, "%s/%s", tracing, name);
357 void put_tracing_file(char *file)
362 int parse_nsec_time(const char *str, u64 *ptime)
364 u64 time_sec, time_nsec;
367 time_sec = strtoul(str, &end, 10);
368 if (*end != '.' && *end != '\0')
375 if (strlen(++end) > 9)
378 strncpy(nsec_buf, end, 9);
381 /* make it nsec precision */
382 for (i = strlen(nsec_buf); i < 9; i++)
385 time_nsec = strtoul(nsec_buf, &end, 10);
391 *ptime = time_sec * NSEC_PER_SEC + time_nsec;
395 unsigned long parse_tag_value(const char *str, struct parse_tag *tags)
397 struct parse_tag *i = tags;
402 s = strchr(str, i->tag);
404 unsigned long int value;
407 value = strtoul(str, &endptr, 10);
411 if (value > ULONG_MAX / i->mult)
419 return (unsigned long) -1;
422 int filename__read_int(const char *filename, int *value)
425 int fd = open(filename, O_RDONLY), err = -1;
430 if (read(fd, line, sizeof(line)) > 0) {
439 int filename__read_str(const char *filename, char **buf, size_t *sizep)
441 size_t size = 0, alloc_size = 0;
442 void *bf = NULL, *nbf;
445 fd = open(filename, O_RDONLY);
450 if (size == alloc_size) {
451 alloc_size += BUFSIZ;
452 nbf = realloc(bf, alloc_size);
461 n = read(fd, bf + size, alloc_size - size);
464 pr_warning("read failed %d: %s\n",
465 errno, strerror(errno));
486 const char *get_filename_for_perf_kvm(void)
488 const char *filename;
490 if (perf_host && !perf_guest)
491 filename = strdup("perf.data.host");
492 else if (!perf_host && perf_guest)
493 filename = strdup("perf.data.guest");
495 filename = strdup("perf.data.kvm");