4 #ifdef BACKTRACE_SUPPORT
11 * XXX We need to find a better place for these things...
13 unsigned int page_size;
15 bool test_attr__enabled;
17 bool perf_host = true;
18 bool perf_guest = false;
20 char tracing_events_path[PATH_MAX + 1] = "/sys/kernel/debug/tracing/events";
22 void event_attr_init(struct perf_event_attr *attr)
25 attr->exclude_host = 1;
27 attr->exclude_guest = 1;
28 /* to capture ABI version */
29 attr->size = sizeof(*attr);
32 int mkdir_p(char *path, mode_t mode)
41 if (stat(path, &st) == 0)
46 while ((d = strchr(d, '/'))) {
48 err = stat(path, &st) && mkdir(path, mode);
55 return (stat(path, &st) && mkdir(path, mode)) ? -1 : 0;
58 static int slow_copyfile(const char *from, const char *to)
63 FILE *from_fp = fopen(from, "r"), *to_fp;
68 to_fp = fopen(to, "w");
72 while (getline(&line, &n, from_fp) > 0)
73 if (fputs(line, to_fp) == EOF)
85 int copyfile(const char *from, const char *to)
95 if (st.st_size == 0) /* /proc? do it slowly... */
96 return slow_copyfile(from, to);
98 fromfd = open(from, O_RDONLY);
102 tofd = creat(to, 0755);
106 addr = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fromfd, 0);
107 if (addr == MAP_FAILED)
110 if (write(tofd, addr, st.st_size) == st.st_size)
113 munmap(addr, st.st_size);
124 unsigned long convert_unit(unsigned long value, char *unit)
146 int readn(int fd, void *buf, size_t n)
148 void *buf_start = buf;
151 int ret = read(fd, buf, n);
160 return buf - buf_start;
163 size_t hex_width(u64 v)
173 static int hex(char ch)
175 if ((ch >= '0') && (ch <= '9'))
177 if ((ch >= 'a') && (ch <= 'f'))
178 return ch - 'a' + 10;
179 if ((ch >= 'A') && (ch <= 'F'))
180 return ch - 'A' + 10;
185 * While we find nice hex chars, build a long_val.
186 * Return number of chars processed.
188 int hex2u64(const char *ptr, u64 *long_val)
194 const int hex_val = hex(*p);
199 *long_val = (*long_val << 4) | hex_val;
206 /* Obtain a backtrace and print it to stdout. */
207 #ifdef BACKTRACE_SUPPORT
208 void dump_stack(void)
211 size_t size = backtrace(array, ARRAY_SIZE(array));
212 char **strings = backtrace_symbols(array, size);
215 printf("Obtained %zd stack frames.\n", size);
217 for (i = 0; i < size; i++)
218 printf("%s\n", strings[i]);
223 void dump_stack(void) {}
226 void get_term_dimensions(struct winsize *ws)
228 char *s = getenv("LINES");
231 ws->ws_row = atoi(s);
232 s = getenv("COLUMNS");
234 ws->ws_col = atoi(s);
235 if (ws->ws_row && ws->ws_col)
240 if (ioctl(1, TIOCGWINSZ, ws) == 0 &&
241 ws->ws_row && ws->ws_col)
248 static void set_tracing_events_path(const char *mountpoint)
250 snprintf(tracing_events_path, sizeof(tracing_events_path), "%s/%s",
251 mountpoint, "tracing/events");
254 const char *perf_debugfs_mount(const char *mountpoint)
258 mnt = debugfs_mount(mountpoint);
262 set_tracing_events_path(mnt);
267 void perf_debugfs_set_path(const char *mntpt)
269 snprintf(debugfs_mountpoint, strlen(debugfs_mountpoint), "%s", mntpt);
270 set_tracing_events_path(mntpt);
273 static const char *find_debugfs(void)
275 const char *path = perf_debugfs_mount(NULL);
278 fprintf(stderr, "Your kernel does not support the debugfs filesystem");
284 * Finds the path to the debugfs/tracing
285 * Allocates the string and stores it.
287 const char *find_tracing_dir(void)
289 static char *tracing;
290 static int tracing_found;
296 debugfs = find_debugfs();
300 tracing = malloc(strlen(debugfs) + 9);
304 sprintf(tracing, "%s/tracing", debugfs);
310 char *get_tracing_file(const char *name)
315 tracing = find_tracing_dir();
319 file = malloc(strlen(tracing) + strlen(name) + 2);
323 sprintf(file, "%s/%s", tracing, name);
327 void put_tracing_file(char *file)
332 int parse_nsec_time(const char *str, u64 *ptime)
334 u64 time_sec, time_nsec;
337 time_sec = strtoul(str, &end, 10);
338 if (*end != '.' && *end != '\0')
345 if (strlen(++end) > 9)
348 strncpy(nsec_buf, end, 9);
351 /* make it nsec precision */
352 for (i = strlen(nsec_buf); i < 9; i++)
355 time_nsec = strtoul(nsec_buf, &end, 10);
361 *ptime = time_sec * NSEC_PER_SEC + time_nsec;