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. */
28 #define BUF_SIZE 4194304
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
;
52 static int sigchld_received
= 0;
54 void sigchld_handler(int signo
)
56 printf("signal %d received\n", signo
);
64 void show_arguments(void)
66 printf("Please use the following arguments :\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 "
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");
94 * Parses the command line arguments.
96 * Returns -1 if the arguments were correct, but doesn't ask for program
97 * continuation. Returns EINVAL if the arguments are incorrect, or 0 if OK.
99 int parse_arguments(int argc
, char **argv
)
105 if(strcmp(argv
[1], "-h") == 0) {
112 switch(argv
[argn
][0]) {
114 switch(argv
[argn
][1]) {
117 trace_name
= argv
[argn
+1];
120 printf("Specify a trace name after -n.\n", argv
[argn
]);
127 op
= CTL_OP_CREATE_START
;
133 mode_name
= argv
[argn
+1];
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
;
140 printf("Invalid mode '%s'.\n", argv
[argn
]);
145 printf("Specify a mode after -m.\n");
154 op
= CTL_OP_STOP_DESTROY
;
164 subbuf_size
= (unsigned)atoi(argv
[argn
+1]);
167 printf("Specify a number of subbuffers after -z.\n");
174 n_subbufs
= (unsigned)atoi(argv
[argn
+1]);
177 printf("Specify a subbuffer size after -x.\n");
186 op
= CTL_OP_DESCRIPTION
;
190 trace_root
= argv
[argn
+1];
193 printf("Specify a trace root path after -t.\n");
200 channel_root
= argv
[argn
+1];
203 printf("Specify a channel root path after -l.\n");
212 printf("Invalid argument '%s'.\n", argv
[argn
]);
218 printf("Invalid argument '%s'.\n", argv
[argn
]);
225 if(op
!= CTL_OP_DESCRIPTION
&& trace_name
== NULL
) {
226 printf("Please specify a trace name.\n");
231 if(op
== CTL_OP_NONE
) {
232 printf("Please specify an operation.\n");
237 if(op
== CTL_OP_DAEMON
) {
238 if(trace_root
== NULL
) {
239 printf("Please specify -t trace_root_path with the -d option.\n");
243 if(channel_root
== NULL
) {
244 printf("Please specify -l ltt_root_path with the -d option.\n");
250 if(op
== CTL_OP_DESCRIPTION
) {
251 if(trace_root
== NULL
) {
252 printf("Please specify -t trace_root_path with the -e option.\n");
263 printf("Linux Trace Toolkit Trace Control\n");
265 if(trace_name
!= NULL
) {
266 printf("Controlling trace : %s\n", trace_name
);
271 int create_eventdefs(void)
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";
283 ret
= mkdir(trace_root
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
284 if(ret
== -1 && errno
!= EEXIST
) {
286 perror("Cannot create trace_root directory");
287 printf("trace_root is %s\n", trace_root
);
292 size_t trace_root_len
= strlen(trace_root
);
293 strncpy(eventdefs_path
, trace_root
, PATH_MAX
);
294 strncat(eventdefs_path
, "/eventdefs/", PATH_MAX
- trace_root_len
);
295 size_t eventdefs_path_len
= strlen(eventdefs_path
);
296 ret
= mkdir(eventdefs_path
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
297 if(ret
== -1 && (!append_trace
|| errno
!= EEXIST
)) {
299 perror("Cannot create eventdefs directory");
304 DIR *facilities_dir
= opendir(facilities_path
);
306 if(facilities_dir
== NULL
) {
307 perror("Cannot open facilities directory");
312 while((entry
= readdir(facilities_dir
)) != NULL
) {
313 if(entry
->d_name
[0] == '.') continue;
315 printf("Appending facility file %s\n", entry
->d_name
);
316 strncpy(eventdefs_file
, eventdefs_path
, PATH_MAX
);
317 strncat(eventdefs_file
, entry
->d_name
, PATH_MAX
- eventdefs_path_len
);
318 /* Append to the file */
319 FILE *dest
= fopen(eventdefs_file
, "a");
321 perror("Cannot create eventdefs file");
324 strncpy(facilities_file
, facilities_path
, PATH_MAX
);
325 size_t facilities_dir_len
= strlen(facilities_path
);
326 strncat(facilities_file
, "/", PATH_MAX
- facilities_dir_len
);
327 strncat(facilities_file
, entry
->d_name
, PATH_MAX
- facilities_dir_len
-1);
328 FILE *src
= fopen(facilities_file
, "r");
331 perror("Cannot open eventdefs file for reading");
336 size_t read_size
, write_size
;
337 read_size
= fread(read_buf
, sizeof(char), BUF_SIZE
, src
);
340 perror("Cannot read eventdefs file");
343 write_size
= fwrite(read_buf
, sizeof(char), read_size
, dest
);
346 perror("Cannot write eventdefs file");
351 /* Add spacing between facilities */
352 fwrite("\n", 1, 1, dest
);
360 closedir(facilities_dir
);
368 int lttctl_daemon(struct lttctl_handle
*handle
, char *trace_name
)
370 char channel_path
[PATH_MAX
] = "";
373 char *lttd_path
= getenv("LTT_DAEMON");
374 struct sigaction act
;
376 if(lttd_path
== NULL
) lttd_path
=
377 PACKAGE_BIN_DIR
"/lttd";
379 strcat(channel_path
, channel_root
);
380 strcat(channel_path
, "/");
381 strcat(channel_path
, trace_name
);
384 ret
= lttctl_create_trace(handle
, trace_name
, mode
, subbuf_size
, n_subbufs
);
385 if(ret
!= 0) goto create_error
;
387 act
.sa_handler
= sigchld_handler
;
388 sigemptyset(&(act
.sa_mask
));
389 sigaddset(&(act
.sa_mask
), SIGCHLD
);
390 sigaction(SIGCHLD
, &act
, NULL
);
397 while(!(sigchld_received
)) pause();
399 waitpid(pid
, &status
, 0);
401 if(WIFEXITED(status
))
402 ret
= WEXITSTATUS(status
);
403 if(ret
) goto start_error
;
405 printf("Creating supplementary trace files\n");
406 ret
= create_eventdefs();
407 if(ret
) goto start_error
;
409 } else if(pid
== 0) {
413 ret
= execlp(lttd_path
, lttd_path
, "-t", trace_root
, "-c",
414 channel_path
, "-d", "-a", NULL
);
416 ret
= execlp(lttd_path
, lttd_path
, "-t", trace_root
, "-c",
417 channel_path
, "-d", NULL
);
420 perror("Error in executing the lttd daemon");
425 perror("Error in forking for lttd daemon");
428 ret
= lttctl_start(handle
, trace_name
);
429 if(ret
!= 0) goto start_error
;
435 printf("Trace start error\n");
436 ret
|= lttctl_destroy_trace(handle
, trace_name
);
441 int main(int argc
, char ** argv
)
444 struct lttctl_handle
*handle
;
446 ret
= parse_arguments(argc
, argv
);
448 if(ret
!= 0) show_arguments();
449 if(ret
== EINVAL
) return EINVAL
;
450 if(ret
== -1) return 0;
454 handle
= lttctl_create_handle();
456 if(handle
== NULL
) return -1;
459 case CTL_OP_CREATE_START
:
460 ret
= lttctl_create_trace(handle
, trace_name
, mode
, subbuf_size
,
463 ret
= lttctl_start(handle
, trace_name
);
466 ret
= lttctl_create_trace(handle
, trace_name
, mode
, subbuf_size
,
470 ret
= lttctl_destroy_trace(handle
, trace_name
);
472 case CTL_OP_STOP_DESTROY
:
473 ret
= lttctl_stop(handle
, trace_name
);
475 ret
= lttctl_destroy_trace(handle
, trace_name
);
478 ret
= lttctl_start(handle
, trace_name
);
481 ret
= lttctl_stop(handle
, trace_name
);
484 ret
= lttctl_daemon(handle
, trace_name
);
486 case CTL_OP_DESCRIPTION
:
487 ret
= create_eventdefs();
493 ret
|= lttctl_destroy_handle(handle
);
This page took 0.062913 seconds and 4 git commands to generate.