#include #include #include #include #include #include #include #include #include static const char * debugfs_list[] = { "/debug/tracing", "/sys/kernel/debug/tracing", "/d/tracing", NULL, }; static const char *debugfs; static int markfd; static int trace_pipe_fd; static const char *find_debugfs(void) { struct stat st; int i; int r; for (i = 0; debugfs_list[i]; i++) { r = stat(debugfs_list[i], &st); if (r < 0) continue; if (S_ISDIR(st.st_mode)) return debugfs_list[i]; } return NULL; } static char * make_path(const char *file) { char *path; int size; size = strlen(debugfs) + strlen(file) + 2; path = malloc(size); if (!path) { perror("malloc"); exit(-1); } sprintf(path, "%s/%s", debugfs, file); return path; } static void mark_write(const char *str) { write(markfd, str, strlen(str)); } static void read_trace_pipe(void) { char buf[1024]; int r; while ((r = read(trace_pipe_fd, buf, 1024)) > 0) printf("%.*s", r, buf); } int main (int argc, char **argv) { struct epoll_event ee; char *marker; char *pipe; int efd; int ret; pid_t dwrt_pid; debugfs = find_debugfs(); if (!debugfs) { fprintf(stderr, "Could not find debugfs\n"); exit(-1); } marker = make_path("trace_marker"); pipe = make_path("trace_pipe"); markfd = open(marker, O_WRONLY); if (markfd < 0) { perror("marker"); exit(-1); } trace_pipe_fd = open(pipe, O_RDONLY|O_NONBLOCK); if (trace_pipe_fd < 0) { perror("trace_pipe"); exit(-1); } efd = epoll_create(1); if (efd < 0) { perror("epoll_create"); exit(-1); } mark_write("some data"); ret = epoll_ctl(efd, EPOLL_CTL_ADD, trace_pipe_fd, &ee); if (ret < 0) { perror("epoll_ctl"); exit(-1); } epoll_wait(efd, &ee, 1, -1); read_trace_pipe(); dwrt_pid = fork(); assert(dwrt_pid != -1); if (dwrt_pid == 0) { sleep(10); mark_write("more data"); printf("%d: written more data\n", getpid()); } else { printf("%d: waitting for more data......\n", getpid()); epoll_wait(efd, &ee, 1, -1); printf("%d: got more data\n", getpid()); read_trace_pipe(); } return 0; }