0e6cefd718230b9320a11375889779993122a9b7
3 * Linux Trace Toolkit Control
5 * Small program that controls LTT through libltt.
8 * Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
15 #include <libltt/libltt.h>
19 #include <sys/types.h>
26 /* Buffer for file copy : 4k seems optimal. */
27 #define BUF_SIZE 4194304
39 static char *trace_name
= NULL
;
40 static char *mode_name
= NULL
;
41 static unsigned subbuf_size
= 0;
42 static unsigned n_subbufs
= 0;
43 static enum trace_mode mode
= LTT_TRACE_NORMAL
;
44 static enum trace_ctl_op op
= CTL_OP_NONE
;
45 static char *channel_root
= NULL
;
46 static char *trace_root
= NULL
;
48 static int sigio_received
= 0;
50 void handler(int signo
)
52 printf("signal %d received\n", signo
);
60 void show_arguments(void)
62 printf("Please use the following arguments :\n");
64 printf("-n name Name of the trace.\n");
65 printf("-c Create trace channels.\n");
66 printf("-m mode Normal or flight recorder mode.\n");
67 printf(" Mode values : normal (default) or flight.\n");
68 printf("-r Destroy trace channels.\n");
69 printf("-s Start tracing.\n");
70 //printf(" Note : will automatically create a normal trace if "
72 printf("-q Stop tracing.\n");
73 printf("-d Create trace, spawn a lttd daemon, start tracing.\n");
74 printf(" (optionnaly, you can set LTT_DAEMON\n");
75 printf(" and the LTT_FACILITIES env. vars.)\n");
76 printf("-t Trace root path. (ex. /root/traces/example_trace)\n");
77 printf("-l LTT channels root path. (ex. /mnt/relayfs/ltt)\n");
78 printf("-z Size of the subbuffers (will be rounded to next page size)\n");
79 printf("-x Number of subbuffers\n");
80 printf("-e Get XML facilities description\n");
87 * Parses the command line arguments.
89 * Returns 1 if the arguments were correct, but doesn't ask for program
90 * continuation. Returns -1 if the arguments are incorrect, or 0 if OK.
92 int parse_arguments(int argc
, char **argv
)
98 if(strcmp(argv
[1], "-h") == 0) {
105 switch(argv
[argn
][0]) {
107 switch(argv
[argn
][1]) {
110 trace_name
= argv
[argn
+1];
113 printf("Specify a trace name after -n.\n", argv
[argn
]);
124 mode_name
= argv
[argn
+1];
126 if(strcmp(mode_name
, "normal") == 0)
127 mode
= LTT_TRACE_NORMAL
;
128 else if(strcmp(mode_name
, "flight") == 0)
129 mode
= LTT_TRACE_FLIGHT
;
131 printf("Invalid mode '%s'.\n", argv
[argn
]);
136 printf("Specify a mode after -m.\n");
152 subbuf_size
= (unsigned)atoi(argv
[argn
+1]);
155 printf("Specify a number of subbuffers after -z.\n");
162 n_subbufs
= (unsigned)atoi(argv
[argn
+1]);
165 printf("Specify a subbuffer size after -x.\n");
174 op
= CTL_OP_DESCRIPTION
;
178 trace_root
= argv
[argn
+1];
181 printf("Specify a trace root path after -t.\n");
188 channel_root
= argv
[argn
+1];
191 printf("Specify a channel root path after -l.\n");
197 printf("Invalid argument '%s'.\n", argv
[argn
]);
203 printf("Invalid argument '%s'.\n", argv
[argn
]);
210 if(op
!= CTL_OP_DESCRIPTION
&& trace_name
== NULL
) {
211 printf("Please specify a trace name.\n");
216 if(op
== CTL_OP_NONE
) {
217 printf("Please specify an operation.\n");
222 if(op
== CTL_OP_DAEMON
) {
223 if(trace_root
== NULL
) {
224 printf("Please specify -t trace_root_path with the -d option.\n");
228 if(channel_root
== NULL
) {
229 printf("Please specify -l ltt_root_path with the -d option.\n");
235 if(op
== CTL_OP_DESCRIPTION
) {
236 if(trace_root
== NULL
) {
237 printf("Please specify -t trace_root_path with the -e option.\n");
248 printf("Linux Trace Toolkit Trace Control\n");
250 if(trace_name
!= NULL
) {
251 printf("Controlling trace : %s\n", trace_name
);
256 int create_eventdefs(void)
259 char eventdefs_path
[PATH_MAX
];
260 char eventdefs_file
[PATH_MAX
];
261 char facilities_file
[PATH_MAX
];
262 char read_buf
[BUF_SIZE
];
263 struct dirent
*entry
;
264 char *facilities_path
= getenv("LTT_FACILITIES");
265 if(facilities_path
== NULL
) facilities_path
=
266 "/usr/share/LinuxTraceToolkitViewer/facilities";
268 ret
= mkdir(trace_root
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
269 if(ret
== -1 && errno
!= EEXIST
) {
270 perror("Cannot create trace_root directory");
271 printf("trace_root is %s\n", trace_root
);
276 size_t trace_root_len
= strlen(trace_root
);
277 strncpy(eventdefs_path
, trace_root
, PATH_MAX
);
278 strncat(eventdefs_path
, "/eventdefs/", PATH_MAX
- trace_root_len
);
279 size_t eventdefs_path_len
= strlen(eventdefs_path
);
280 ret
= mkdir(eventdefs_path
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
281 if(ret
== -1 && errno
!= EEXIST
) {
282 perror("Cannot create eventdefs directory");
287 DIR *facilities_dir
= opendir(facilities_path
);
289 while((entry
= readdir(facilities_dir
)) != NULL
) {
290 if(entry
->d_name
[0] == '.') continue;
292 printf("Appending facility file %s\n", entry
->d_name
);
293 strncpy(eventdefs_file
, eventdefs_path
, PATH_MAX
);
294 strncat(eventdefs_file
, entry
->d_name
, PATH_MAX
- eventdefs_path_len
);
295 /* Append to the file */
296 FILE *dest
= fopen(eventdefs_file
, "a");
298 perror("Cannot create eventdefs file");
301 strncpy(facilities_file
, facilities_path
, PATH_MAX
);
302 size_t facilities_dir_len
= strlen(facilities_path
);
303 strncat(facilities_file
, "/", PATH_MAX
- facilities_dir_len
);
304 strncat(facilities_file
, entry
->d_name
, PATH_MAX
- facilities_dir_len
-1);
305 FILE *src
= fopen(facilities_file
, "r");
307 perror("Cannot open eventdefs file for reading");
312 size_t read_size
, write_size
;
313 read_size
= fread(read_buf
, sizeof(char), BUF_SIZE
, src
);
315 perror("Cannot read eventdefs file");
318 write_size
= fwrite(read_buf
, sizeof(char), read_size
, dest
);
320 perror("Cannot write eventdefs file");
325 /* Add spacing between facilities */
326 fwrite("\n", 1, 1, dest
);
334 closedir(facilities_dir
);
342 int lttctl_daemon(struct lttctl_handle
*handle
, char *trace_name
)
344 char channel_path
[PATH_MAX
] = "";
347 char *lttd_path
= getenv("LTT_DAEMON");
348 struct sigaction act
;
350 if(lttd_path
== NULL
) lttd_path
= "lttd";
352 strcat(channel_path
, channel_root
);
353 strcat(channel_path
, "/");
354 strcat(channel_path
, trace_name
);
357 ret
= lttctl_create_trace(handle
, trace_name
, mode
, subbuf_size
, n_subbufs
);
358 if(ret
!= 0) goto create_error
;
360 act
.sa_handler
= handler
;
361 sigemptyset(&(act
.sa_mask
));
362 sigaddset(&(act
.sa_mask
), SIGIO
);
363 sigaction(SIGIO
, &act
, NULL
);
371 while(!sigio_received
) pause();
373 /* Now the trace is created, go on and create the supplementary files... */
375 printf("Creating supplementary trace files\n");
376 ret
= create_eventdefs();
377 if(ret
) goto start_error
;
379 } else if(pid
== 0) {
382 execlp(lttd_path
, lttd_path
, "-t", trace_root
, "-c", channel_path
, "-s", NULL
);
384 perror("Error in executing the lttd daemon");
389 perror("Error in forking for lttd daemon");
393 ret
= lttctl_start(handle
, trace_name
);
394 if(ret
!= 0) goto start_error
;
400 ret
|= lttctl_destroy_trace(handle
, trace_name
);
405 int main(int argc
, char ** argv
)
408 struct lttctl_handle
*handle
;
410 ret
= parse_arguments(argc
, argv
);
412 if(ret
!= 0) show_arguments();
413 if(ret
< 0) return EINVAL
;
414 if(ret
> 0) return 0;
418 handle
= lttctl_create_handle();
420 if(handle
== NULL
) return -1;
424 ret
= lttctl_create_trace(handle
, trace_name
, mode
, subbuf_size
,
428 ret
= lttctl_destroy_trace(handle
, trace_name
);
431 ret
= lttctl_start(handle
, trace_name
);
434 ret
= lttctl_stop(handle
, trace_name
);
437 ret
= lttctl_daemon(handle
, trace_name
);
439 case CTL_OP_DESCRIPTION
:
440 ret
= create_eventdefs();
446 ret
|= lttctl_destroy_handle(handle
);
This page took 0.03983 seconds and 4 git commands to generate.