6 #ifdef HAVE_BACKTRACE_SUPPORT
15 #include <linux/kernel.h>
17 #include "callchain.h"
19 struct callchain_param callchain_param = {
20 .mode = CHAIN_GRAPH_REL,
22 .order = ORDER_CALLEE,
27 * XXX We need to find a better place for these things...
29 unsigned int page_size;
32 bool test_attr__enabled;
34 bool perf_host = true;
35 bool perf_guest = false;
37 char tracing_events_path[PATH_MAX + 1] = "/sys/kernel/debug/tracing/events";
39 void event_attr_init(struct perf_event_attr *attr)
42 attr->exclude_host = 1;
44 attr->exclude_guest = 1;
45 /* to capture ABI version */
46 attr->size = sizeof(*attr);
49 int mkdir_p(char *path, mode_t mode)
58 if (stat(path, &st) == 0)
63 while ((d = strchr(d, '/'))) {
65 err = stat(path, &st) && mkdir(path, mode);
72 return (stat(path, &st) && mkdir(path, mode)) ? -1 : 0;
75 static int slow_copyfile(const char *from, const char *to, mode_t mode)
80 FILE *from_fp = fopen(from, "r"), *to_fp;
86 old_umask = umask(mode ^ 0777);
87 to_fp = fopen(to, "w");
92 while (getline(&line, &n, from_fp) > 0)
93 if (fputs(line, to_fp) == EOF)
105 int copyfile_mode(const char *from, const char *to, mode_t mode)
115 if (st.st_size == 0) /* /proc? do it slowly... */
116 return slow_copyfile(from, to, mode);
118 fromfd = open(from, O_RDONLY);
122 tofd = creat(to, mode);
126 addr = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fromfd, 0);
127 if (addr == MAP_FAILED)
130 if (write(tofd, addr, st.st_size) == st.st_size)
133 munmap(addr, st.st_size);
144 int copyfile(const char *from, const char *to)
146 return copyfile_mode(from, to, 0755);
149 unsigned long convert_unit(unsigned long value, char *unit)
171 static ssize_t ion(bool is_read, int fd, void *buf, size_t n)
173 void *buf_start = buf;
177 ssize_t ret = is_read ? read(fd, buf, left) :
178 write(fd, buf, left);
180 if (ret < 0 && errno == EINTR)
189 BUG_ON((size_t)(buf - buf_start) != n);
194 * Read exactly 'n' bytes or return an error.
196 ssize_t readn(int fd, void *buf, size_t n)
198 return ion(true, fd, buf, n);
202 * Write exactly 'n' bytes or return an error.
204 ssize_t writen(int fd, void *buf, size_t n)
206 return ion(false, fd, buf, n);
209 size_t hex_width(u64 v)
219 static int hex(char ch)
221 if ((ch >= '0') && (ch <= '9'))
223 if ((ch >= 'a') && (ch <= 'f'))
224 return ch - 'a' + 10;
225 if ((ch >= 'A') && (ch <= 'F'))
226 return ch - 'A' + 10;
231 * While we find nice hex chars, build a long_val.
232 * Return number of chars processed.
234 int hex2u64(const char *ptr, u64 *long_val)
240 const int hex_val = hex(*p);
245 *long_val = (*long_val << 4) | hex_val;
252 /* Obtain a backtrace and print it to stdout. */
253 #ifdef HAVE_BACKTRACE_SUPPORT
254 void dump_stack(void)
257 size_t size = backtrace(array, ARRAY_SIZE(array));
258 char **strings = backtrace_symbols(array, size);
261 printf("Obtained %zd stack frames.\n", size);
263 for (i = 0; i < size; i++)
264 printf("%s\n", strings[i]);
269 void dump_stack(void) {}
272 void sighandler_dump_stack(int sig)
274 psignal(sig, "perf");
279 void get_term_dimensions(struct winsize *ws)
281 char *s = getenv("LINES");
284 ws->ws_row = atoi(s);
285 s = getenv("COLUMNS");
287 ws->ws_col = atoi(s);
288 if (ws->ws_row && ws->ws_col)
293 if (ioctl(1, TIOCGWINSZ, ws) == 0 &&
294 ws->ws_row && ws->ws_col)
301 void set_term_quiet_input(struct termios *old)
307 tc.c_lflag &= ~(ICANON | ECHO);
310 tcsetattr(0, TCSANOW, &tc);
313 static void set_tracing_events_path(const char *tracing, const char *mountpoint)
315 snprintf(tracing_events_path, sizeof(tracing_events_path), "%s/%s%s",
316 mountpoint, tracing, "events");
319 static const char *__perf_tracefs_mount(const char *mountpoint)
323 mnt = tracefs_mount(mountpoint);
327 set_tracing_events_path("", mnt);
332 static const char *__perf_debugfs_mount(const char *mountpoint)
336 mnt = debugfs_mount(mountpoint);
340 set_tracing_events_path("tracing/", mnt);
345 const char *perf_debugfs_mount(const char *mountpoint)
349 mnt = __perf_tracefs_mount(mountpoint);
353 mnt = __perf_debugfs_mount(mountpoint);
358 void perf_debugfs_set_path(const char *mntpt)
360 snprintf(debugfs_mountpoint, strlen(debugfs_mountpoint), "%s", mntpt);
361 set_tracing_events_path("tracing/", mntpt);
364 static const char *find_tracefs(void)
366 const char *path = __perf_tracefs_mount(NULL);
371 static const char *find_debugfs(void)
373 const char *path = __perf_debugfs_mount(NULL);
376 fprintf(stderr, "Your kernel does not support the debugfs filesystem");
382 * Finds the path to the debugfs/tracing
383 * Allocates the string and stores it.
385 const char *find_tracing_dir(void)
387 const char *tracing_dir = "";
388 static char *tracing;
389 static int tracing_found;
395 debugfs = find_tracefs();
397 tracing_dir = "/tracing";
398 debugfs = find_debugfs();
403 if (asprintf(&tracing, "%s%s", debugfs, tracing_dir) < 0)
410 char *get_tracing_file(const char *name)
415 tracing = find_tracing_dir();
419 if (asprintf(&file, "%s/%s", tracing, name) < 0)
425 void put_tracing_file(char *file)
430 int parse_nsec_time(const char *str, u64 *ptime)
432 u64 time_sec, time_nsec;
435 time_sec = strtoul(str, &end, 10);
436 if (*end != '.' && *end != '\0')
443 if (strlen(++end) > 9)
446 strncpy(nsec_buf, end, 9);
449 /* make it nsec precision */
450 for (i = strlen(nsec_buf); i < 9; i++)
453 time_nsec = strtoul(nsec_buf, &end, 10);
459 *ptime = time_sec * NSEC_PER_SEC + time_nsec;
463 unsigned long parse_tag_value(const char *str, struct parse_tag *tags)
465 struct parse_tag *i = tags;
470 s = strchr(str, i->tag);
472 unsigned long int value;
475 value = strtoul(str, &endptr, 10);
479 if (value > ULONG_MAX / i->mult)
487 return (unsigned long) -1;
490 int filename__read_str(const char *filename, char **buf, size_t *sizep)
492 size_t size = 0, alloc_size = 0;
493 void *bf = NULL, *nbf;
495 char sbuf[STRERR_BUFSIZE];
497 fd = open(filename, O_RDONLY);
502 if (size == alloc_size) {
503 alloc_size += BUFSIZ;
504 nbf = realloc(bf, alloc_size);
513 n = read(fd, bf + size, alloc_size - size);
516 pr_warning("read failed %d: %s\n", errno,
517 strerror_r(errno, sbuf, sizeof(sbuf)));
538 const char *get_filename_for_perf_kvm(void)
540 const char *filename;
542 if (perf_host && !perf_guest)
543 filename = strdup("perf.data.host");
544 else if (!perf_host && perf_guest)
545 filename = strdup("perf.data.guest");
547 filename = strdup("perf.data.kvm");
552 int perf_event_paranoid(void)
556 if (sysctl__read_int("kernel/perf_event_paranoid", &value))
562 void mem_bswap_32(void *src, int byte_size)
565 while (byte_size > 0) {
567 byte_size -= sizeof(u32);
572 void mem_bswap_64(void *src, int byte_size)
576 while (byte_size > 0) {
578 byte_size -= sizeof(u64);
583 bool find_process(const char *name)
585 size_t len = strlen(name);
590 dir = opendir(procfs__mountpoint());
594 /* Walk through the directory. */
595 while (ret && (d = readdir(dir)) != NULL) {
600 if ((d->d_type != DT_DIR) ||
601 !strcmp(".", d->d_name) ||
602 !strcmp("..", d->d_name))
605 scnprintf(path, sizeof(path), "%s/%s/comm",
606 procfs__mountpoint(), d->d_name);
608 if (filename__read_str(path, &data, &size))
611 ret = strncmp(name, data, len);
616 return ret ? false : true;