3 * Linux Trace Toolkit Control
5 * Small program that controls LTT through libltt.
8 * Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
15 #include <liblttctl/lttctl.h>
19 #include <sys/types.h>
27 /* Buffer for file copy : 4k seems optimal. */
38 CTL_OP_DAEMON_HYBRID_FINISH
,
42 static char *trace_name
= NULL
;
43 static char *trace_type
= "relay";
44 static char *mode_name
= NULL
;
45 static unsigned subbuf_size_low
= 0;
46 static unsigned n_subbufs_low
= 0;
47 static unsigned subbuf_size_med
= 0;
48 static unsigned n_subbufs_med
= 0;
49 static unsigned subbuf_size_high
= 0;
50 static unsigned n_subbufs_high
= 0;
51 static unsigned append_trace
= 0;
52 static enum trace_mode mode
= LTT_TRACE_NORMAL
;
53 static enum trace_ctl_op op
= CTL_OP_NONE
;
54 static char *channel_root
= NULL
;
55 static char *trace_root
= NULL
;
56 static char *num_threads
= "1";
61 void show_arguments(void)
63 printf("Please use the following arguments :\n");
65 printf("-n name Name of the trace.\n");
66 printf("-b Create trace channels and start tracing (no daemon).\n");
67 printf("-c Create trace channels.\n");
68 printf("-m mode Normal, flight recorder or hybrid mode.\n");
69 printf(" Mode values : normal (default), flight or hybrid.\n");
70 printf("-r Destroy trace channels.\n");
71 printf("-R Stop tracing and destroy trace channels.\n");
72 printf("-s Start tracing.\n");
73 //printf(" Note : will automatically create a normal trace if "
75 printf("-q Stop tracing.\n");
76 printf("-d Create trace, spawn a lttd daemon, start tracing.\n");
77 printf(" (optionally, you can set LTT_DAEMON\n");
78 printf(" env. var.)\n");
79 printf("-f Stop tracing, dump flight recorder trace, destroy channels\n");
80 printf(" (for hybrid traces)\n");
81 printf("-t Trace root path. (ex. /root/traces/example_trace)\n");
82 printf("-T Type of trace (ex. relay)\n");
83 printf("-l LTT channels root path. (ex. /mnt/debugfs/ltt)\n");
84 printf("-Z Size of the low data rate subbuffers (will be rounded to next page size)\n");
85 printf("-X Number of low data rate subbuffers\n");
86 printf("-V Size of the medium data rate subbuffers (will be rounded to next page size)\n");
87 printf("-B Number of medium data rate subbuffers\n");
88 printf("-z Size of the high data rate subbuffers (will be rounded to next page size)\n");
89 printf("-x Number of high data rate subbuffers\n");
90 printf("-a Append to trace\n");
91 printf("-N Number of lttd threads\n");
98 * Parses the command line arguments.
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.
103 int parse_arguments(int argc
, char **argv
)
109 if(strcmp(argv
[1], "-h") == 0) {
116 switch(argv
[argn
][0]) {
118 switch(argv
[argn
][1]) {
121 trace_name
= argv
[argn
+1];
124 printf("Specify a trace name after -n.\n");
131 op
= CTL_OP_CREATE_START
;
138 mode_name
= argv
[argn
+1];
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 if(strcmp(mode_name
, "hybrid") == 0)
145 mode
= LTT_TRACE_HYBRID
;
147 printf("Invalid mode '%s'.\n", argv
[argn
]);
152 printf("Specify a mode after -m.\n");
161 op
= CTL_OP_STOP_DESTROY
;
171 subbuf_size_low
= (unsigned)atoi(argv
[argn
+1]);
174 printf("Specify a number of low traffic subbuffers after -Z.\n");
181 n_subbufs_low
= (unsigned)atoi(argv
[argn
+1]);
184 printf("Specify a low traffic subbuffer size after -X.\n");
191 subbuf_size_med
= (unsigned)atoi(argv
[argn
+1]);
194 printf("Specify a number of medium traffic subbuffers after -V.\n");
201 n_subbufs_med
= (unsigned)atoi(argv
[argn
+1]);
204 printf("Specify a medium traffic subbuffer size after -B.\n");
211 subbuf_size_high
= (unsigned)atoi(argv
[argn
+1]);
214 printf("Specify a number of high traffic subbuffers after -z.\n");
221 n_subbufs_high
= (unsigned)atoi(argv
[argn
+1]);
224 printf("Specify a high traffic subbuffer size after -x.\n");
233 op
= CTL_OP_DAEMON_HYBRID_FINISH
;
237 trace_root
= argv
[argn
+1];
240 printf("Specify a trace root path after -t.\n");
247 channel_root
= argv
[argn
+1];
250 printf("Specify a channel root path after -l.\n");
260 num_threads
= argv
[argn
+1];
266 trace_type
= argv
[argn
+1];
269 printf("Specify a trace type after -T.\n");
275 printf("Invalid argument '%s'.\n", argv
[argn
]);
281 printf("Invalid argument '%s'.\n", argv
[argn
]);
288 if(trace_name
== NULL
) {
289 printf("Please specify a trace name.\n");
294 if(op
== CTL_OP_NONE
) {
295 printf("Please specify an operation.\n");
300 if(op
== CTL_OP_DAEMON
|| op
== CTL_OP_DAEMON_HYBRID_FINISH
) {
301 if(trace_root
== NULL
) {
302 printf("Please specify -t trace_root_path with the -d option.\n");
306 if(channel_root
== NULL
) {
307 printf("Please specify -l ltt_root_path with the -d option.\n");
318 printf("Linux Trace Toolkit Trace Control " VERSION
"\n");
320 if(trace_name
!= NULL
) {
321 printf("Controlling trace : %s\n", trace_name
);
326 int lttctl_daemon(struct lttctl_handle
*handle
, char *trace_name
)
328 char channel_path
[PATH_MAX
] = "";
331 char *lttd_path
= getenv("LTT_DAEMON");
333 if(lttd_path
== NULL
) lttd_path
=
334 PACKAGE_BIN_DIR
"/lttd";
336 strcat(channel_path
, channel_root
);
337 strcat(channel_path
, "/");
338 strcat(channel_path
, trace_name
);
341 ret
= lttctl_create_trace(handle
, trace_name
, mode
, trace_type
,
342 subbuf_size_low
, n_subbufs_low
,
343 subbuf_size_med
, n_subbufs_med
,
344 subbuf_size_high
, n_subbufs_high
);
345 if(ret
!= 0) goto create_error
;
353 ret
= waitpid(pid
, &status
, 0);
356 perror("Error in waitpid");
361 if(WIFEXITED(status
))
362 ret
= WEXITSTATUS(status
);
363 if(ret
) goto start_error
;
365 printf("Creating supplementary trace files\n");
366 ret
= create_eventdefs();
367 if(ret
) goto start_error
;
369 } else if(pid
== 0) {
372 if(mode
!= LTT_TRACE_HYBRID
) {
374 ret
= execlp(lttd_path
, lttd_path
, "-t", trace_root
, "-c",
375 channel_path
, "-d", "-a", "-N", num_threads
, NULL
);
377 ret
= execlp(lttd_path
, lttd_path
, "-t", trace_root
, "-c",
378 channel_path
, "-d", "-N", num_threads
, NULL
);
381 ret
= execlp(lttd_path
, lttd_path
, "-t", trace_root
, "-c",
382 channel_path
, "-d", "-a", "-N", num_threads
, "-n", NULL
);
384 ret
= execlp(lttd_path
, lttd_path
, "-t", trace_root
, "-c",
385 channel_path
, "-d", "-N", num_threads
, "-n", NULL
);
389 perror("Error in executing the lttd daemon");
394 perror("Error in forking for lttd daemon");
397 ret
= lttctl_start(handle
, trace_name
);
398 if(ret
!= 0) goto start_error
;
404 printf("Trace start error\n");
405 ret
|= lttctl_destroy_trace(handle
, trace_name
);
413 int lttctl_daemon_hybrid_finish(struct lttctl_handle
*handle
, char *trace_name
)
415 char channel_path
[PATH_MAX
] = "";
418 char *lttd_path
= getenv("LTT_DAEMON");
420 if(lttd_path
== NULL
) lttd_path
=
421 PACKAGE_BIN_DIR
"/lttd";
423 strcat(channel_path
, channel_root
);
424 strcat(channel_path
, "/");
425 strcat(channel_path
, trace_name
);
428 ret
= lttctl_stop(handle
, trace_name
);
429 if(ret
!= 0) goto stop_error
;
437 ret
= waitpid(pid
, &status
, 0);
440 perror("Error in waitpid");
445 if(WIFEXITED(status
))
446 ret
= WEXITSTATUS(status
);
447 if(ret
) goto destroy_error
;
449 } else if(pid
== 0) {
453 ret
= execlp(lttd_path
, lttd_path
, "-t", trace_root
, "-c",
454 channel_path
, "-d", "-a", "-N", num_threads
, "-f", NULL
);
456 ret
= execlp(lttd_path
, lttd_path
, "-t", trace_root
, "-c",
457 channel_path
, "-d", "-N", num_threads
, "-f", NULL
);
460 perror("Error in executing the lttd daemon");
465 perror("Error in forking for lttd daemon");
468 ret
= lttctl_destroy_trace(handle
, trace_name
);
469 if(ret
!= 0) goto destroy_error
;
475 printf("Hybrid trace destroy error\n");
482 int main(int argc
, char ** argv
)
485 struct lttctl_handle
*handle
;
487 ret
= parse_arguments(argc
, argv
);
489 if(ret
!= 0) show_arguments();
490 if(ret
== EINVAL
) return EINVAL
;
491 if(ret
== -1) return 0;
495 handle
= lttctl_create_handle();
497 if(handle
== NULL
) return -1;
500 case CTL_OP_CREATE_START
:
501 ret
= lttctl_create_trace(handle
, trace_name
, mode
, trace_type
,
502 subbuf_size_low
, n_subbufs_low
,
503 subbuf_size_med
, n_subbufs_med
,
504 subbuf_size_high
, n_subbufs_high
);
506 ret
= lttctl_start(handle
, trace_name
);
509 ret
= lttctl_create_trace(handle
, trace_name
, mode
, trace_type
,
510 subbuf_size_low
, n_subbufs_low
,
511 subbuf_size_med
, n_subbufs_med
,
512 subbuf_size_high
, n_subbufs_high
);
515 ret
= lttctl_destroy_trace(handle
, trace_name
);
517 case CTL_OP_STOP_DESTROY
:
518 ret
= lttctl_stop(handle
, trace_name
);
520 ret
= lttctl_destroy_trace(handle
, trace_name
);
523 ret
= lttctl_start(handle
, trace_name
);
526 ret
= lttctl_stop(handle
, trace_name
);
529 ret
= lttctl_daemon(handle
, trace_name
);
531 case CTL_OP_DAEMON_HYBRID_FINISH
:
532 ret
= lttctl_daemon_hybrid_finish(handle
, trace_name
);
538 ret
|= lttctl_destroy_handle(handle
);