9845c5c600c81e5a7136550235b0e5697f99c098
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 mode Create trace channels in mode normal or flight recorder.\n");
66 printf(" Mode values : normal (default) or flight.\n");
67 printf("-r Destroy trace channels.\n");
68 printf("-s Start tracing.\n");
69 //printf(" Note : will automatically create a normal trace if "
71 printf("-q Stop tracing.\n");
72 printf("-d Create trace, spawn a lttd daemon, start tracing.\n");
73 printf(" (optionnaly, you can set LTT_DAEMON\n");
74 printf(" and the LTT_FACILITIES env. vars.)\n");
75 printf("-t Trace root path. (ex. /root/traces/example_trace)\n");
76 printf("-l LTT channels root path. (ex. /mnt/relayfs/ltt)\n");
77 printf("-z Size of the subbuffers (will be rounded to next page size)\n");
78 printf("-x Number of subbuffers\n");
79 printf("-e Get XML facilities description\n");
86 * Parses the command line arguments.
88 * Returns 1 if the arguments were correct, but doesn't ask for program
89 * continuation. Returns -1 if the arguments are incorrect, or 0 if OK.
91 int parse_arguments(int argc
, char **argv
)
97 if(strcmp(argv
[1], "-h") == 0) {
104 switch(argv
[argn
][0]) {
106 switch(argv
[argn
][1]) {
109 trace_name
= argv
[argn
+1];
112 printf("Specify a trace name after -n.\n", argv
[argn
]);
121 mode_name
= argv
[argn
+1];
123 if(strcmp(mode_name
, "normal") == 0)
124 mode
= LTT_TRACE_NORMAL
;
125 else if(strcmp(mode_name
, "flight") == 0)
126 mode
= LTT_TRACE_FLIGHT
;
128 printf("Invalid mode '%s'.\n", argv
[argn
]);
133 printf("Specify a mode after -c.\n");
149 subbuf_size
= (unsigned)atoi(argv
[argn
+1]);
152 printf("Specify a number of subbuffers after -z.\n");
159 n_subbufs
= (unsigned)atoi(argv
[argn
+1]);
162 printf("Specify a subbuffer size after -x.\n");
171 op
= CTL_OP_DESCRIPTION
;
175 trace_root
= argv
[argn
+1];
178 printf("Specify a trace root path after -t.\n");
185 channel_root
= argv
[argn
+1];
188 printf("Specify a channel root path after -l.\n");
194 printf("Invalid argument '%s'.\n", argv
[argn
]);
200 printf("Invalid argument '%s'.\n", argv
[argn
]);
207 if(op
!= CTL_OP_DESCRIPTION
&& trace_name
== NULL
) {
208 printf("Please specify a trace name.\n");
213 if(op
== CTL_OP_NONE
) {
214 printf("Please specify an operation.\n");
219 if(op
== CTL_OP_DAEMON
) {
220 if(trace_root
== NULL
) {
221 printf("Please specify -t trace_root_path with the -d option.\n");
225 if(channel_root
== NULL
) {
226 printf("Please specify -l ltt_root_path with the -d option.\n");
232 if(op
== CTL_OP_DESCRIPTION
) {
233 if(trace_root
== NULL
) {
234 printf("Please specify -t trace_root_path with the -e option.\n");
245 printf("Linux Trace Toolkit Trace Control\n");
247 if(trace_name
!= NULL
) {
248 printf("Controlling trace : %s\n", trace_name
);
253 int create_eventdefs(void)
256 char eventdefs_path
[PATH_MAX
];
257 char eventdefs_file
[PATH_MAX
];
258 char facilities_file
[PATH_MAX
];
259 char read_buf
[BUF_SIZE
];
260 struct dirent
*entry
;
261 char *facilities_path
= getenv("LTT_FACILITIES");
262 if(facilities_path
== NULL
) facilities_path
=
263 "/usr/share/LinuxTraceToolkitViewer/facilities";
265 ret
= mkdir(trace_root
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
266 if(ret
== -1 && errno
!= EEXIST
) {
267 perror("Cannot create trace_root directory");
268 printf("trace_root is %s\n", trace_root
);
273 size_t trace_root_len
= strlen(trace_root
);
274 strncpy(eventdefs_path
, trace_root
, PATH_MAX
);
275 strncat(eventdefs_path
, "/eventdefs/", PATH_MAX
- trace_root_len
);
276 size_t eventdefs_path_len
= strlen(eventdefs_path
);
277 ret
= mkdir(eventdefs_path
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
278 if(ret
== -1 && errno
!= EEXIST
) {
279 perror("Cannot create eventdefs directory");
284 DIR *facilities_dir
= opendir(facilities_path
);
286 while((entry
= readdir(facilities_dir
)) != NULL
) {
287 if(entry
->d_name
[0] == '.') continue;
289 printf("Appending facility file %s\n", entry
->d_name
);
290 strncpy(eventdefs_file
, eventdefs_path
, PATH_MAX
);
291 strncat(eventdefs_file
, entry
->d_name
, PATH_MAX
- eventdefs_path_len
);
292 /* Append to the file */
293 FILE *dest
= fopen(eventdefs_file
, "a");
295 perror("Cannot create eventdefs file");
298 strncpy(facilities_file
, facilities_path
, PATH_MAX
);
299 size_t facilities_dir_len
= strlen(facilities_path
);
300 strncat(facilities_file
, "/", PATH_MAX
- facilities_dir_len
);
301 strncat(facilities_file
, entry
->d_name
, PATH_MAX
- facilities_dir_len
-1);
302 FILE *src
= fopen(facilities_file
, "r");
304 perror("Cannot open eventdefs file for reading");
309 size_t read_size
, write_size
;
310 read_size
= fread(read_buf
, sizeof(char), BUF_SIZE
, src
);
312 perror("Cannot read eventdefs file");
315 write_size
= fwrite(read_buf
, sizeof(char), read_size
, dest
);
317 perror("Cannot write eventdefs file");
322 /* Add spacing between facilities */
323 fwrite("\n", 1, 1, dest
);
331 closedir(facilities_dir
);
339 int lttctl_daemon(struct lttctl_handle
*handle
, char *trace_name
)
341 char channel_path
[PATH_MAX
] = "";
344 char *lttd_path
= getenv("LTT_DAEMON");
345 struct sigaction act
;
347 if(lttd_path
== NULL
) lttd_path
= "lttd";
349 strcat(channel_path
, channel_root
);
350 strcat(channel_path
, "/");
351 strcat(channel_path
, trace_name
);
354 ret
= lttctl_create_trace(handle
, trace_name
, mode
, subbuf_size
, n_subbufs
);
355 if(ret
!= 0) goto create_error
;
357 act
.sa_handler
= handler
;
358 sigemptyset(&(act
.sa_mask
));
359 sigaddset(&(act
.sa_mask
), SIGIO
);
360 sigaction(SIGIO
, &act
, NULL
);
368 while(!sigio_received
) pause();
370 /* Now the trace is created, go on and create the supplementary files... */
372 printf("Creating supplementary trace files\n");
373 ret
= create_eventdefs();
374 if(ret
) goto start_error
;
376 } else if(pid
== 0) {
379 execlp(lttd_path
, lttd_path
, "-t", trace_root
, "-c", channel_path
, "-s", NULL
);
381 perror("Error in executing the lttd daemon");
386 perror("Error in forking for lttd daemon");
390 ret
= lttctl_start(handle
, trace_name
);
391 if(ret
!= 0) goto start_error
;
397 ret
|= lttctl_destroy_trace(handle
, trace_name
);
402 int main(int argc
, char ** argv
)
405 struct lttctl_handle
*handle
;
407 ret
= parse_arguments(argc
, argv
);
409 if(ret
!= 0) show_arguments();
410 if(ret
< 0) return EINVAL
;
411 if(ret
> 0) return 0;
415 handle
= lttctl_create_handle();
417 if(handle
== NULL
) return -1;
421 ret
= lttctl_create_trace(handle
, trace_name
, mode
, subbuf_size
,
425 ret
= lttctl_destroy_trace(handle
, trace_name
);
428 ret
= lttctl_start(handle
, trace_name
);
431 ret
= lttctl_stop(handle
, trace_name
);
434 ret
= lttctl_daemon(handle
, trace_name
);
436 case CTL_OP_DESCRIPTION
:
437 ret
= create_eventdefs();
443 ret
|= lttctl_destroy_handle(handle
);
This page took 0.050951 seconds and 4 git commands to generate.