2 * ktapio.c - ring buffer transport in userspace
4 * This file is part of ktap by Jovi Zhangwei.
6 * Copyright (C) 2012-2013 Jovi Zhangwei <jovi.zhangwei@gmail.com>.
8 * ktap is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2, as published by the Free Software Foundation.
12 * ktap is distributed in the hope it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
29 #include <sys/signal.h>
33 #define MAX_BUFLEN 131072
36 #define handle_error(str) do { perror(str); exit(-1); } while(0)
38 void sigfunc(int signo)
40 /* should not not reach here */
43 static void block_sigint()
48 sigaddset(&mask, SIGINT);
50 pthread_sigmask(SIG_BLOCK, &mask, NULL);
53 static void *reader_thread(void *data)
56 char filename[PATH_MAX];
57 const char *output = data;
58 int failed = 0, fd, out_fd, len;
63 out_fd = open(output, O_CREAT | O_WRONLY | O_TRUNC,
66 fprintf(stderr, "Cannot open output file %s\n", output);
72 sprintf(filename, "/sys/kernel/debug/ktap/trace_pipe_%d", getpid());
75 fd = open(filename, O_RDONLY);
80 fprintf(stderr, "Cannot open file %s\n", filename);
86 while ((len = read(fd, buf, sizeof(buf))) > 0)
87 write(out_fd, buf, len);
95 int ktapio_create(const char *output)
99 signal(SIGINT, sigfunc);
101 if (pthread_create(&reader, NULL, reader_thread, (void *)output) < 0)
102 handle_error("pthread_create reader_thread failed\n");