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 } else if(pid
== 0) {
368 if(mode
!= LTT_TRACE_HYBRID
) {
370 ret
= execlp(lttd_path
, lttd_path
, "-t", trace_root
, "-c",
371 channel_path
, "-d", "-a", "-N", num_threads
, NULL
);
373 ret
= execlp(lttd_path
, lttd_path
, "-t", trace_root
, "-c",
374 channel_path
, "-d", "-N", num_threads
, NULL
);
377 ret
= execlp(lttd_path
, lttd_path
, "-t", trace_root
, "-c",
378 channel_path
, "-d", "-a", "-N", num_threads
, "-n", NULL
);
380 ret
= execlp(lttd_path
, lttd_path
, "-t", trace_root
, "-c",
381 channel_path
, "-d", "-N", num_threads
, "-n", NULL
);
385 perror("Error in executing the lttd daemon");
390 perror("Error in forking for lttd daemon");
393 ret
= lttctl_start(handle
, trace_name
);
394 if(ret
!= 0) goto start_error
;
400 printf("Trace start error\n");
401 ret
|= lttctl_destroy_trace(handle
, trace_name
);
409 int lttctl_daemon_hybrid_finish(struct lttctl_handle
*handle
, char *trace_name
)
411 char channel_path
[PATH_MAX
] = "";
414 char *lttd_path
= getenv("LTT_DAEMON");
416 if(lttd_path
== NULL
) lttd_path
=
417 PACKAGE_BIN_DIR
"/lttd";
419 strcat(channel_path
, channel_root
);
420 strcat(channel_path
, "/");
421 strcat(channel_path
, trace_name
);
424 ret
= lttctl_stop(handle
, trace_name
);
425 if(ret
!= 0) goto stop_error
;
433 ret
= waitpid(pid
, &status
, 0);
436 perror("Error in waitpid");
441 if(WIFEXITED(status
))
442 ret
= WEXITSTATUS(status
);
443 if(ret
) goto destroy_error
;
445 } else if(pid
== 0) {
449 ret
= execlp(lttd_path
, lttd_path
, "-t", trace_root
, "-c",
450 channel_path
, "-d", "-a", "-N", num_threads
, "-f", NULL
);
452 ret
= execlp(lttd_path
, lttd_path
, "-t", trace_root
, "-c",
453 channel_path
, "-d", "-N", num_threads
, "-f", NULL
);
456 perror("Error in executing the lttd daemon");
461 perror("Error in forking for lttd daemon");
464 ret
= lttctl_destroy_trace(handle
, trace_name
);
465 if(ret
!= 0) goto destroy_error
;
471 printf("Hybrid trace destroy error\n");
478 int main(int argc
, char ** argv
)
481 struct lttctl_handle
*handle
;
483 ret
= parse_arguments(argc
, argv
);
485 if(ret
!= 0) show_arguments();
486 if(ret
== EINVAL
) return EINVAL
;
487 if(ret
== -1) return 0;
491 handle
= lttctl_create_handle();
493 if(handle
== NULL
) return -1;
496 case CTL_OP_CREATE_START
:
497 ret
= lttctl_create_trace(handle
, trace_name
, mode
, trace_type
,
498 subbuf_size_low
, n_subbufs_low
,
499 subbuf_size_med
, n_subbufs_med
,
500 subbuf_size_high
, n_subbufs_high
);
502 ret
= lttctl_start(handle
, trace_name
);
505 ret
= lttctl_create_trace(handle
, trace_name
, mode
, trace_type
,
506 subbuf_size_low
, n_subbufs_low
,
507 subbuf_size_med
, n_subbufs_med
,
508 subbuf_size_high
, n_subbufs_high
);
511 ret
= lttctl_destroy_trace(handle
, trace_name
);
513 case CTL_OP_STOP_DESTROY
:
514 ret
= lttctl_stop(handle
, trace_name
);
516 ret
= lttctl_destroy_trace(handle
, trace_name
);
519 ret
= lttctl_start(handle
, trace_name
);
522 ret
= lttctl_stop(handle
, trace_name
);
525 ret
= lttctl_daemon(handle
, trace_name
);
527 case CTL_OP_DAEMON_HYBRID_FINISH
:
528 ret
= lttctl_daemon_hybrid_finish(handle
, trace_name
);
534 ret
|= lttctl_destroy_handle(handle
);