4 #ifdef HAVE_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, mode_t mode)
63 FILE *from_fp = fopen(from, "r"), *to_fp;
69 old_umask = umask(mode ^ 0777);
70 to_fp = fopen(to, "w");
75 while (getline(&line, &n, from_fp) > 0)
76 if (fputs(line, to_fp) == EOF)
88 int copyfile_mode(const char *from, const char *to, mode_t mode)
98 if (st.st_size == 0) /* /proc? do it slowly... */
99 return slow_copyfile(from, to, mode);
101 fromfd = open(from, O_RDONLY);
105 tofd = creat(to, mode);
109 addr = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fromfd, 0);
110 if (addr == MAP_FAILED)
113 if (write(tofd, addr, st.st_size) == st.st_size)
116 munmap(addr, st.st_size);
127 int copyfile(const char *from, const char *to)
129 return copyfile_mode(from, to, 0755);
132 unsigned long convert_unit(unsigned long value, char *unit)
154 int readn(int fd, void *buf, size_t n)
156 void *buf_start = buf;
159 int ret = read(fd, buf, n);
168 return buf - buf_start;
171 size_t hex_width(u64 v)
181 static int hex(char ch)
183 if ((ch >= '0') && (ch <= '9'))
185 if ((ch >= 'a') && (ch <= 'f'))
186 return ch - 'a' + 10;
187 if ((ch >= 'A') && (ch <= 'F'))
188 return ch - 'A' + 10;
193 * While we find nice hex chars, build a long_val.
194 * Return number of chars processed.
196 int hex2u64(const char *ptr, u64 *long_val)
202 const int hex_val = hex(*p);
207 *long_val = (*long_val << 4) | hex_val;
214 /* Obtain a backtrace and print it to stdout. */
215 #ifdef HAVE_BACKTRACE_SUPPORT
216 void dump_stack(void)
219 size_t size = backtrace(array, ARRAY_SIZE(array));
220 char **strings = backtrace_symbols(array, size);
223 printf("Obtained %zd stack frames.\n", size);
225 for (i = 0; i < size; i++)
226 printf("%s\n", strings[i]);
231 void dump_stack(void) {}
234 void get_term_dimensions(struct winsize *ws)
236 char *s = getenv("LINES");
239 ws->ws_row = atoi(s);
240 s = getenv("COLUMNS");
242 ws->ws_col = atoi(s);
243 if (ws->ws_row && ws->ws_col)
248 if (ioctl(1, TIOCGWINSZ, ws) == 0 &&
249 ws->ws_row && ws->ws_col)
256 static void set_tracing_events_path(const char *mountpoint)
258 snprintf(tracing_events_path, sizeof(tracing_events_path), "%s/%s",
259 mountpoint, "tracing/events");
262 const char *perf_debugfs_mount(const char *mountpoint)
266 mnt = debugfs_mount(mountpoint);
270 set_tracing_events_path(mnt);
275 void perf_debugfs_set_path(const char *mntpt)
277 snprintf(debugfs_mountpoint, strlen(debugfs_mountpoint), "%s", mntpt);
278 set_tracing_events_path(mntpt);
281 static const char *find_debugfs(void)
283 const char *path = perf_debugfs_mount(NULL);
286 fprintf(stderr, "Your kernel does not support the debugfs filesystem");
292 * Finds the path to the debugfs/tracing
293 * Allocates the string and stores it.
295 const char *find_tracing_dir(void)
297 static char *tracing;
298 static int tracing_found;
304 debugfs = find_debugfs();
308 tracing = malloc(strlen(debugfs) + 9);
312 sprintf(tracing, "%s/tracing", debugfs);
318 char *get_tracing_file(const char *name)
323 tracing = find_tracing_dir();
327 file = malloc(strlen(tracing) + strlen(name) + 2);
331 sprintf(file, "%s/%s", tracing, name);
335 void put_tracing_file(char *file)
340 int parse_nsec_time(const char *str, u64 *ptime)
342 u64 time_sec, time_nsec;
345 time_sec = strtoul(str, &end, 10);
346 if (*end != '.' && *end != '\0')
353 if (strlen(++end) > 9)
356 strncpy(nsec_buf, end, 9);
359 /* make it nsec precision */
360 for (i = strlen(nsec_buf); i < 9; i++)
363 time_nsec = strtoul(nsec_buf, &end, 10);
369 *ptime = time_sec * NSEC_PER_SEC + time_nsec;
373 unsigned long parse_tag_value(const char *str, struct parse_tag *tags)
375 struct parse_tag *i = tags;
380 s = strchr(str, i->tag);
382 unsigned long int value;
385 value = strtoul(str, &endptr, 10);
395 return (unsigned long) -1;
398 int filename__read_int(const char *filename, int *value)
401 int fd = open(filename, O_RDONLY), err = -1;
406 if (read(fd, line, sizeof(line)) > 0) {