Adds the multiple instances feature
[ltt-control.git] / lttd / lttd.c
1 /* lttd
2 *
3 * Linux Trace Toolkit Daemon
4 *
5 * This is a simple daemon that reads a few relay+debugfs channels and save
6 * them in a trace.
7 *
8 * CPU hot-plugging is supported using inotify.
9 *
10 * Copyright 2009-2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 */
26
27 #ifdef HAVE_CONFIG_H
28 #include <config.h>
29 #endif
30
31 #define _REENTRANT
32 #define _GNU_SOURCE
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <signal.h>
38 #include <errno.h>
39 #include <fcntl.h>
40 #include <dirent.h>
41 #include <sys/stat.h>
42
43 #include <liblttd/liblttd.h>
44
45 struct lttd_channel_data {
46 int trace;
47 };
48
49 struct liblttd_instance *instance;
50 static char path_trace[PATH_MAX];
51 static char *end_path_trace;
52 static int path_trace_len = 0;
53 static char *trace_name = NULL;
54 static char *channel_name = NULL;
55 static int daemon_mode = 0;
56 static int append_mode = 0;
57 static unsigned long num_threads = 1;
58 static int dump_flight_only = 0;
59 static int dump_normal_only = 0;
60 static int verbose_mode = 0;
61
62 static __thread int thread_pipe[2];
63
64 #define printf_verbose(fmt, args...) \
65 do { \
66 if (verbose_mode) \
67 printf(fmt, ##args); \
68 } while (0)
69
70 /* Args :
71 *
72 * -t directory Directory name of the trace to write to. Will be created.
73 * -c directory Root directory of the debugfs trace channels.
74 * -d Run in background (daemon).
75 * -a Trace append mode.
76 * -s Send SIGUSR1 to parent when ready for IO.
77 */
78 void show_arguments(void)
79 {
80 printf("Please use the following arguments :\n");
81 printf("\n");
82 printf("-t directory Directory name of the trace to write to.\n"
83 " It will be created.\n");
84 printf("-c directory Root directory of the debugfs trace channels.\n");
85 printf("-d Run in background (daemon).\n");
86 printf("-a Append to an possibly existing trace.\n");
87 printf("-N Number of threads to start.\n");
88 printf("-f Dump only flight recorder channels.\n");
89 printf("-n Dump only normal channels.\n");
90 printf("-v Verbose mode.\n");
91 printf("\n");
92 }
93
94
95 /* parse_arguments
96 *
97 * Parses the command line arguments.
98 *
99 * Returns 1 if the arguments were correct, but doesn't ask for program
100 * continuation. Returns -1 if the arguments are incorrect, or 0 if OK.
101 */
102 int parse_arguments(int argc, char **argv)
103 {
104 int ret = 0;
105 int argn = 1;
106
107 if(argc == 2) {
108 if(strcmp(argv[1], "-h") == 0) {
109 return 1;
110 }
111 }
112
113 while(argn < argc) {
114
115 switch(argv[argn][0]) {
116 case '-':
117 switch(argv[argn][1]) {
118 case 't':
119 if(argn+1 < argc) {
120 trace_name = argv[argn+1];
121 argn++;
122 }
123 break;
124 case 'c':
125 if(argn+1 < argc) {
126 channel_name = argv[argn+1];
127 argn++;
128 }
129 break;
130 case 'd':
131 daemon_mode = 1;
132 break;
133 case 'a':
134 append_mode = 1;
135 break;
136 case 'N':
137 if(argn+1 < argc) {
138 num_threads = strtoul(argv[argn+1], NULL, 0);
139 argn++;
140 }
141 break;
142 case 'f':
143 dump_flight_only = 1;
144 break;
145 case 'n':
146 dump_normal_only = 1;
147 break;
148 case 'v':
149 verbose_mode = 1;
150 break;
151 default:
152 printf("Invalid argument '%s'.\n", argv[argn]);
153 printf("\n");
154 ret = -1;
155 }
156 break;
157 default:
158 printf("Invalid argument '%s'.\n", argv[argn]);
159 printf("\n");
160 ret = -1;
161 }
162 argn++;
163 }
164
165 if(trace_name == NULL) {
166 printf("Please specify a trace name.\n");
167 printf("\n");
168 ret = -1;
169 }
170
171 if(channel_name == NULL) {
172 printf("Please specify a channel name.\n");
173 printf("\n");
174 ret = -1;
175 }
176
177 return ret;
178 }
179
180 void show_info(void)
181 {
182 printf("Linux Trace Toolkit Trace Daemon " VERSION "\n");
183 printf("\n");
184 printf("Reading from debugfs directory : %s\n", channel_name);
185 printf("Writing to trace directory : %s\n", trace_name);
186 printf("\n");
187 }
188
189
190 /* signal handling */
191
192 static void handler(int signo)
193 {
194 printf("Signal %d received : exiting cleanly\n", signo);
195 liblttd_stop_instance(instance);
196 }
197
198 int lttd_on_open_channel(struct liblttd_callbacks *data, struct fd_pair *pair, char *relative_channel_path)
199 {
200 int open_ret = 0;
201 int ret;
202 struct stat stat_buf;
203 struct lttd_channel_data *channel_data;
204
205 pair->user_data = malloc(sizeof(struct lttd_channel_data));
206 channel_data = pair->user_data;
207
208 strncpy(end_path_trace, relative_channel_path, PATH_MAX - path_trace_len);
209 printf_verbose("Creating trace file %s\n", path_trace);
210
211 ret = stat(path_trace, &stat_buf);
212 if(ret == 0) {
213 if(append_mode) {
214 printf_verbose("Appending to file %s as requested\n",
215 path_trace);
216
217 channel_data->trace = open(path_trace, O_WRONLY, S_IRWXU|S_IRWXG|S_IRWXO);
218 if(channel_data->trace == -1) {
219 perror(path_trace);
220 open_ret = -1;
221 goto end;
222 }
223 ret = lseek(channel_data->trace, 0, SEEK_END);
224 if (ret < 0) {
225 perror(path_trace);
226 open_ret = -1;
227 close(channel_data->trace);
228 goto end;
229 }
230 } else {
231 printf("File %s exists, cannot open. Try append mode.\n", path_trace);
232 open_ret = -1;
233 goto end;
234 }
235 } else {
236 if(errno == ENOENT) {
237 channel_data->trace = open(path_trace, O_WRONLY|O_CREAT|O_EXCL, S_IRWXU|S_IRWXG|S_IRWXO);
238 if(channel_data->trace == -1) {
239 perror(path_trace);
240 open_ret = -1;
241 goto end;
242 }
243 }
244 }
245
246 end:
247 return open_ret;
248
249 }
250
251 int lttd_on_close_channel(struct liblttd_callbacks *data, struct fd_pair *pair)
252 {
253 int ret;
254 ret = close(((struct lttd_channel_data *)(pair->user_data))->trace);
255 free(pair->user_data);
256 return ret;
257 }
258
259 int lttd_on_new_channels_folder(struct liblttd_callbacks *data, char *relative_folder_path)
260 {
261 int ret;
262 int open_ret = 0;
263
264 strncpy(end_path_trace, relative_folder_path, PATH_MAX - path_trace_len);
265 printf_verbose("Creating trace subdirectory %s\n", path_trace);
266
267 ret = mkdir(path_trace, S_IRWXU|S_IRWXG|S_IRWXO);
268 if(ret == -1) {
269 if(errno != EEXIST) {
270 perror(path_trace);
271 open_ret = -1;
272 goto end;
273 }
274 }
275
276 end:
277 return open_ret;
278 }
279
280 int lttd_on_read_subbuffer(struct liblttd_callbacks *data, struct fd_pair *pair, unsigned int len)
281 {
282 long ret;
283 off_t offset = 0;
284
285 while (len > 0) {
286 printf_verbose("splice chan to pipe offset %lu\n",
287 (unsigned long)offset);
288 ret = splice(pair->channel, &offset, thread_pipe[1], NULL,
289 len, SPLICE_F_MOVE | SPLICE_F_MORE);
290 printf_verbose("splice chan to pipe ret %ld\n", ret);
291 if (ret < 0) {
292 perror("Error in relay splice");
293 goto write_error;
294 }
295 ret = splice(thread_pipe[0], NULL,
296 ((struct lttd_channel_data *)(pair->user_data))->trace,
297 NULL, ret, SPLICE_F_MOVE | SPLICE_F_MORE);
298 printf_verbose("splice pipe to file %ld\n", ret);
299 if (ret < 0) {
300 perror("Error in file splice");
301 goto write_error;
302 }
303 len -= ret;
304 }
305
306 write_error:
307 return ret;
308 }
309
310 int lttd_on_new_thread(struct liblttd_callbacks *data, unsigned long thread_num)
311 {
312 int ret;
313 ret = pipe(thread_pipe);
314 if (ret < 0) {
315 perror("Error creating pipe");
316 return ret;
317 }
318 return 0;
319 }
320
321 int lttd_on_close_thread(struct liblttd_callbacks *data, unsigned long thread_num)
322 {
323 close(thread_pipe[0]); /* close read end */
324 close(thread_pipe[1]); /* close write end */
325 return 0;
326 }
327
328 int main(int argc, char ** argv)
329 {
330 int ret = 0;
331 struct sigaction act;
332
333 struct liblttd_callbacks callbacks = {
334 lttd_on_open_channel,
335 lttd_on_close_channel,
336 lttd_on_new_channels_folder,
337 lttd_on_read_subbuffer,
338 NULL,
339 lttd_on_new_thread,
340 lttd_on_close_thread,
341 NULL
342 };
343
344 ret = parse_arguments(argc, argv);
345
346 if(ret != 0) show_arguments();
347 if(ret < 0) return EINVAL;
348 if(ret > 0) return 0;
349
350 show_info();
351
352 /* Connect the signal handlers */
353 act.sa_handler = handler;
354 act.sa_flags = 0;
355 sigemptyset(&(act.sa_mask));
356 sigaddset(&(act.sa_mask), SIGTERM);
357 sigaddset(&(act.sa_mask), SIGQUIT);
358 sigaddset(&(act.sa_mask), SIGINT);
359 sigaction(SIGTERM, &act, NULL);
360 sigaction(SIGQUIT, &act, NULL);
361 sigaction(SIGINT, &act, NULL);
362
363 if(daemon_mode) {
364 ret = daemon(0, 0);
365
366 if(ret == -1) {
367 perror("An error occured while daemonizing.");
368 exit(-1);
369 }
370 }
371 strncpy(path_trace, trace_name, PATH_MAX-1);
372 path_trace_len = strlen(path_trace);
373 end_path_trace = path_trace + path_trace_len;
374
375 instance = liblttd_new_instance(&callbacks, channel_name, num_threads,
376 dump_flight_only, dump_normal_only, verbose_mode);
377 if(!instance) {
378 perror("An error occured while creating the liblttd instance");
379 return ret;
380 }
381
382 liblttd_start_instance(instance);
383
384 return ret;
385 }
386
This page took 0.036206 seconds and 4 git commands to generate.