trace control finished
[lttv.git] / ltt / branches / poly / lttctl / lttctl.c
1 /* lttctl
2 *
3 * Linux Trace Toolkit Control
4 *
5 * Small program that controls LTT through libltt.
6 *
7 * Copyright 2005 -
8 * Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
9 */
10
11 #ifdef HAVE_CONFIG_H
12 #include <config.h>
13 #endif
14
15 #include <libltt/libltt.h>
16 #include <errno.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <sys/types.h>
20 #include <sys/wait.h>
21 #include <unistd.h>
22 #include <signal.h>
23 #include <dirent.h>
24 #include <string.h>
25 #include <sys/stat.h>
26
27 /* Buffer for file copy : 4k seems optimal. */
28 #define BUF_SIZE 4194304
29
30 enum trace_ctl_op {
31 CTL_OP_CREATE_START,
32 CTL_OP_CREATE,
33 CTL_OP_DESTROY,
34 CTL_OP_STOP_DESTROY,
35 CTL_OP_START,
36 CTL_OP_STOP,
37 CTL_OP_DAEMON,
38 CTL_OP_DESCRIPTION,
39 CTL_OP_NONE
40 };
41
42 static char *trace_name = NULL;
43 static char *mode_name = NULL;
44 static unsigned subbuf_size = 0;
45 static unsigned n_subbufs = 0;
46 static unsigned append_trace = 0;
47 static enum trace_mode mode = LTT_TRACE_NORMAL;
48 static enum trace_ctl_op op = CTL_OP_NONE;
49 static char *channel_root = NULL;
50 static char *trace_root = NULL;
51
52 static int sigchld_received = 0;
53
54 void sigchld_handler(int signo)
55 {
56 printf("signal %d received\n", signo);
57 sigchld_received = 1;
58 }
59
60
61 /* Args :
62 *
63 */
64 void show_arguments(void)
65 {
66 printf("Please use the following arguments :\n");
67 printf("\n");
68 printf("-n name Name of the trace.\n");
69 printf("-b Create trace channels and start tracing (no daemon).\n");
70 printf("-c Create trace channels.\n");
71 printf("-m mode Normal or flight recorder mode.\n");
72 printf(" Mode values : normal (default) or flight.\n");
73 printf("-r Destroy trace channels.\n");
74 printf("-R Stop tracing and destroy trace channels.\n");
75 printf("-s Start tracing.\n");
76 //printf(" Note : will automatically create a normal trace if "
77 // "none exists.\n");
78 printf("-q Stop tracing.\n");
79 printf("-d Create trace, spawn a lttd daemon, start tracing.\n");
80 printf(" (optionnaly, you can set LTT_DAEMON\n");
81 printf(" and the LTT_FACILITIES env. vars.)\n");
82 printf("-t Trace root path. (ex. /root/traces/example_trace)\n");
83 printf("-l LTT channels root path. (ex. /mnt/relayfs/ltt)\n");
84 printf("-z Size of the subbuffers (will be rounded to next page size)\n");
85 printf("-x Number of subbuffers\n");
86 printf("-e Get XML facilities description\n");
87 printf("-a Append to trace\n");
88 printf("\n");
89 }
90
91
92 /* parse_arguments
93 *
94 * Parses the command line arguments.
95 *
96 * Returns 1 if the arguments were correct, but doesn't ask for program
97 * continuation. Returns -1 if the arguments are incorrect, or 0 if OK.
98 */
99 int parse_arguments(int argc, char **argv)
100 {
101 int ret = 0;
102 int argn = 1;
103
104 if(argc == 2) {
105 if(strcmp(argv[1], "-h") == 0) {
106 return 1;
107 }
108 }
109
110 while(argn < argc) {
111
112 switch(argv[argn][0]) {
113 case '-':
114 switch(argv[argn][1]) {
115 case 'n':
116 if(argn+1 < argc) {
117 trace_name = argv[argn+1];
118 argn++;
119 } else {
120 printf("Specify a trace name after -n.\n", argv[argn]);
121 printf("\n");
122 ret = -1;
123 }
124
125 break;
126 case 'b':
127 op = CTL_OP_CREATE_START;
128 case 'c':
129 op = CTL_OP_CREATE;
130 break;
131 case 'm':
132 if(argn+1 < argc) {
133 mode_name = argv[argn+1];
134 argn++;
135 if(strcmp(mode_name, "normal") == 0)
136 mode = LTT_TRACE_NORMAL;
137 else if(strcmp(mode_name, "flight") == 0)
138 mode = LTT_TRACE_FLIGHT;
139 else {
140 printf("Invalid mode '%s'.\n", argv[argn]);
141 printf("\n");
142 ret = -1;
143 }
144 } else {
145 printf("Specify a mode after -m.\n");
146 printf("\n");
147 ret = -1;
148 }
149 break;
150 case 'r':
151 op = CTL_OP_DESTROY;
152 break;
153 case 'R':
154 op = CTL_OP_STOP_DESTROY;
155 break;
156 case 's':
157 op = CTL_OP_START;
158 break;
159 case 'q':
160 op = CTL_OP_STOP;
161 break;
162 case 'z':
163 if(argn+1 < argc) {
164 subbuf_size = (unsigned)atoi(argv[argn+1]);
165 argn++;
166 } else {
167 printf("Specify a number of subbuffers after -z.\n");
168 printf("\n");
169 ret = -1;
170 }
171 break;
172 case 'x':
173 if(argn+1 < argc) {
174 n_subbufs = (unsigned)atoi(argv[argn+1]);
175 argn++;
176 } else {
177 printf("Specify a subbuffer size after -x.\n");
178 printf("\n");
179 ret = -1;
180 }
181 break;
182 case 'd':
183 op = CTL_OP_DAEMON;
184 break;
185 case 'e':
186 op = CTL_OP_DESCRIPTION;
187 break;
188 case 't':
189 if(argn+1 < argc) {
190 trace_root = argv[argn+1];
191 argn++;
192 } else {
193 printf("Specify a trace root path after -t.\n");
194 printf("\n");
195 ret = -1;
196 }
197 break;
198 case 'l':
199 if(argn+1 < argc) {
200 channel_root = argv[argn+1];
201 argn++;
202 } else {
203 printf("Specify a channel root path after -l.\n");
204 printf("\n");
205 ret = -1;
206 }
207 break;
208 case 'a':
209 append_trace = 1;
210 break;
211 default:
212 printf("Invalid argument '%s'.\n", argv[argn]);
213 printf("\n");
214 ret = -1;
215 }
216 break;
217 default:
218 printf("Invalid argument '%s'.\n", argv[argn]);
219 printf("\n");
220 ret = -1;
221 }
222 argn++;
223 }
224
225 if(op != CTL_OP_DESCRIPTION && trace_name == NULL) {
226 printf("Please specify a trace name.\n");
227 printf("\n");
228 ret = -1;
229 }
230
231 if(op == CTL_OP_NONE) {
232 printf("Please specify an operation.\n");
233 printf("\n");
234 ret = -1;
235 }
236
237 if(op == CTL_OP_DAEMON) {
238 if(trace_root == NULL) {
239 printf("Please specify -t trace_root_path with the -d option.\n");
240 printf("\n");
241 ret = -1;
242 }
243 if(channel_root == NULL) {
244 printf("Please specify -l ltt_root_path with the -d option.\n");
245 printf("\n");
246 ret = -1;
247 }
248 }
249
250 if(op == CTL_OP_DESCRIPTION) {
251 if(trace_root == NULL) {
252 printf("Please specify -t trace_root_path with the -e option.\n");
253 printf("\n");
254 ret = -1;
255 }
256 }
257
258 return ret;
259 }
260
261 void show_info(void)
262 {
263 printf("Linux Trace Toolkit Trace Control\n");
264 printf("\n");
265 if(trace_name != NULL) {
266 printf("Controlling trace : %s\n", trace_name);
267 printf("\n");
268 }
269 }
270
271 int create_eventdefs(void)
272 {
273 int ret = 0;
274 char eventdefs_path[PATH_MAX];
275 char eventdefs_file[PATH_MAX];
276 char facilities_file[PATH_MAX];
277 char read_buf[BUF_SIZE];
278 struct dirent *entry;
279 char *facilities_path = getenv("LTT_FACILITIES");
280 if(facilities_path == NULL) facilities_path =
281 PACKAGE_DATA_DIR "/" PACKAGE "/facilities";
282
283 ret = mkdir(trace_root, S_IRWXU|S_IRWXG|S_IRWXO);
284 if(ret == -1 && errno != EEXIST) {
285 perror("Cannot create trace_root directory");
286 printf("trace_root is %s\n", trace_root);
287 goto error;
288 }
289 ret = 0;
290
291 size_t trace_root_len = strlen(trace_root);
292 strncpy(eventdefs_path, trace_root, PATH_MAX);
293 strncat(eventdefs_path, "/eventdefs/", PATH_MAX - trace_root_len);
294 size_t eventdefs_path_len = strlen(eventdefs_path);
295 ret = mkdir(eventdefs_path, S_IRWXU|S_IRWXG|S_IRWXO);
296 if(ret == -1 && (!append_trace || errno != EEXIST)) {
297 perror("Cannot create eventdefs directory");
298 goto error;
299 }
300 ret = 0;
301
302 DIR *facilities_dir = opendir(facilities_path);
303
304 if(facilities_dir == NULL) {
305 perror("Cannot open facilities directory");
306 ret = -1;
307 goto error;
308 }
309
310 while((entry = readdir(facilities_dir)) != NULL) {
311 if(entry->d_name[0] == '.') continue;
312
313 printf("Appending facility file %s\n", entry->d_name);
314 strncpy(eventdefs_file, eventdefs_path, PATH_MAX);
315 strncat(eventdefs_file, entry->d_name, PATH_MAX - eventdefs_path_len);
316 /* Append to the file */
317 FILE *dest = fopen(eventdefs_file, "a");
318 if(!dest) {
319 perror("Cannot create eventdefs file");
320 continue;
321 }
322 strncpy(facilities_file, facilities_path, PATH_MAX);
323 size_t facilities_dir_len = strlen(facilities_path);
324 strncat(facilities_file, "/", PATH_MAX - facilities_dir_len);
325 strncat(facilities_file, entry->d_name, PATH_MAX - facilities_dir_len-1);
326 FILE *src = fopen(facilities_file, "r");
327 if(!src) {
328 perror("Cannot open eventdefs file for reading");
329 goto close_dest;
330 }
331
332 do {
333 size_t read_size, write_size;
334 read_size = fread(read_buf, sizeof(char), BUF_SIZE, src);
335 if(ferror(src)) {
336 perror("Cannot read eventdefs file");
337 goto close_src;
338 }
339 write_size = fwrite(read_buf, sizeof(char), read_size, dest);
340 if(ferror(dest)) {
341 perror("Cannot write eventdefs file");
342 goto close_src;
343 }
344 } while(!feof(src));
345
346 /* Add spacing between facilities */
347 fwrite("\n", 1, 1, dest);
348
349 close_src:
350 fclose(src);
351 close_dest:
352 fclose(dest);
353 }
354
355 closedir(facilities_dir);
356
357 error:
358 return ret;
359
360 }
361
362
363 int lttctl_daemon(struct lttctl_handle *handle, char *trace_name)
364 {
365 char channel_path[PATH_MAX] = "";
366 pid_t pid;
367 int ret;
368 char *lttd_path = getenv("LTT_DAEMON");
369 struct sigaction act;
370
371 if(lttd_path == NULL) lttd_path =
372 PACKAGE_BIN_DIR "/lttd";
373
374 strcat(channel_path, channel_root);
375 strcat(channel_path, "/");
376 strcat(channel_path, trace_name);
377
378
379 ret = lttctl_create_trace(handle, trace_name, mode, subbuf_size, n_subbufs);
380 if(ret != 0) goto create_error;
381
382 act.sa_handler = sigchld_handler;
383 sigemptyset(&(act.sa_mask));
384 sigaddset(&(act.sa_mask), SIGCHLD);
385 sigaction(SIGCHLD, &act, NULL);
386
387 pid = fork();
388
389 if(pid > 0) {
390 int status;
391 /* parent */
392 while(!(sigchld_received)) pause();
393
394 waitpid(pid, &status, 0);
395 ret = 0;
396 if(WIFEXITED(status))
397 ret = WEXITSTATUS(status);
398 if(ret) goto start_error;
399
400 printf("Creating supplementary trace files\n");
401 ret = create_eventdefs();
402 if(ret) goto start_error;
403
404 } else if(pid == 0) {
405 /* child */
406 int ret;
407 if(append_trace)
408 ret = execlp(lttd_path, lttd_path, "-t", trace_root, "-c",
409 channel_path, "-d", "-a", NULL);
410 else
411 ret = execlp(lttd_path, lttd_path, "-t", trace_root, "-c",
412 channel_path, "-d", NULL);
413 if(ret) {
414 perror("Error in executing the lttd daemon");
415 exit(-1);
416 }
417 } else {
418 /* error */
419 perror("Error in forking for lttd daemon");
420
421 }
422
423 ret = lttctl_start(handle, trace_name);
424 if(ret != 0) goto start_error;
425
426 return 0;
427
428 /* error handling */
429 start_error:
430 printf("Trace start error\n");
431 ret |= lttctl_destroy_trace(handle, trace_name);
432 create_error:
433 return ret;
434 }
435
436 int main(int argc, char ** argv)
437 {
438 int ret;
439 struct lttctl_handle *handle;
440
441 ret = parse_arguments(argc, argv);
442
443 if(ret != 0) show_arguments();
444 if(ret < 0) return EINVAL;
445 if(ret > 0) return 0;
446
447 show_info();
448
449 handle = lttctl_create_handle();
450
451 if(handle == NULL) return -1;
452
453 switch(op) {
454 case CTL_OP_CREATE_START:
455 ret = lttctl_create_trace(handle, trace_name, mode, subbuf_size,
456 n_subbufs);
457 if(!ret)
458 ret = lttctl_start(handle, trace_name);
459 break;
460 case CTL_OP_CREATE:
461 ret = lttctl_create_trace(handle, trace_name, mode, subbuf_size,
462 n_subbufs);
463 break;
464 case CTL_OP_DESTROY:
465 ret = lttctl_destroy_trace(handle, trace_name);
466 break;
467 case CTL_OP_STOP_DESTROY:
468 ret = lttctl_stop(handle, trace_name);
469 if(!ret)
470 ret = lttctl_destroy_trace(handle, trace_name);
471 break;
472 case CTL_OP_START:
473 ret = lttctl_start(handle, trace_name);
474 break;
475 case CTL_OP_STOP:
476 ret = lttctl_stop(handle, trace_name);
477 break;
478 case CTL_OP_DAEMON:
479 ret = lttctl_daemon(handle, trace_name);
480 break;
481 case CTL_OP_DESCRIPTION:
482 ret = create_eventdefs();
483 break;
484 case CTL_OP_NONE:
485 break;
486 }
487
488 ret |= lttctl_destroy_handle(handle);
489
490 return ret;
491 }
This page took 0.038465 seconds and 4 git commands to generate.