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