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);