multithreaded lttd
[lttv.git] / ltt / branches / poly / lttctl / lttctl.c
CommitLineData
2cdb6fcb 1/* lttctl
2 *
3 * Linux Trace Toolkit Control
4 *
5 * Small program that controls LTT through libltt.
6 *
7 * Copyright 2005 -
8 * Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
9 */
10
4e4d11b3 11#ifdef HAVE_CONFIG_H
12#include <config.h>
13#endif
14
5cec0a15 15#include <liblttctl/lttctl.h>
5adba60f 16#include <errno.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <sys/types.h>
86a65fdb 20#include <sys/wait.h>
5adba60f 21#include <unistd.h>
22#include <signal.h>
d5fbdb60 23#include <dirent.h>
24#include <string.h>
25#include <sys/stat.h>
26
27/* Buffer for file copy : 4k seems optimal. */
2312de30 28#define BUF_SIZE 4096
2cdb6fcb 29
5adba60f 30enum trace_ctl_op {
b7f08766 31 CTL_OP_CREATE_START,
5adba60f 32 CTL_OP_CREATE,
33 CTL_OP_DESTROY,
b7f08766 34 CTL_OP_STOP_DESTROY,
5adba60f 35 CTL_OP_START,
36 CTL_OP_STOP,
37 CTL_OP_DAEMON,
129fd24a 38 CTL_OP_DESCRIPTION,
5adba60f 39 CTL_OP_NONE
40};
2cdb6fcb 41
5adba60f 42static char *trace_name = NULL;
43static char *mode_name = NULL;
988614ed 44static unsigned subbuf_size = 0;
45static unsigned n_subbufs = 0;
45653836 46static unsigned append_trace = 0;
5adba60f 47static enum trace_mode mode = LTT_TRACE_NORMAL;
48static enum trace_ctl_op op = CTL_OP_NONE;
49static char *channel_root = NULL;
50static char *trace_root = NULL;
22acb617 51static char *num_threads = "1";
5adba60f 52
86a65fdb 53static int sigchld_received = 0;
ada84671 54
86a65fdb 55void sigchld_handler(int signo)
5adba60f 56{
57 printf("signal %d received\n", signo);
86a65fdb 58 sigchld_received = 1;
5adba60f 59}
60
61
62/* Args :
63 *
64 */
65void show_arguments(void)
66{
67 printf("Please use the following arguments :\n");
68 printf("\n");
69 printf("-n name Name of the trace.\n");
b7f08766 70 printf("-b Create trace channels and start tracing (no daemon).\n");
bc0fcb31 71 printf("-c Create trace channels.\n");
72 printf("-m mode Normal or flight recorder mode.\n");
5adba60f 73 printf(" Mode values : normal (default) or flight.\n");
74 printf("-r Destroy trace channels.\n");
b7f08766 75 printf("-R Stop tracing and destroy trace channels.\n");
5adba60f 76 printf("-s Start tracing.\n");
77 //printf(" Note : will automatically create a normal trace if "
78 // "none exists.\n");
79 printf("-q Stop tracing.\n");
80 printf("-d Create trace, spawn a lttd daemon, start tracing.\n");
d5fbdb60 81 printf(" (optionnaly, you can set LTT_DAEMON\n");
82 printf(" and the LTT_FACILITIES env. vars.)\n");
5adba60f 83 printf("-t Trace root path. (ex. /root/traces/example_trace)\n");
84 printf("-l LTT channels root path. (ex. /mnt/relayfs/ltt)\n");
4085af66 85 printf("-z Size of the subbuffers (will be rounded to next page size)\n");
988614ed 86 printf("-x Number of subbuffers\n");
129fd24a 87 printf("-e Get XML facilities description\n");
45653836 88 printf("-a Append to trace\n");
22acb617 89 printf("-N Number of ltt threads\n");
5adba60f 90 printf("\n");
91}
92
93
94/* parse_arguments
95 *
96 * Parses the command line arguments.
97 *
2baf08e5 98 * Returns -1 if the arguments were correct, but doesn't ask for program
99 * continuation. Returns EINVAL if the arguments are incorrect, or 0 if OK.
5adba60f 100 */
101int parse_arguments(int argc, char **argv)
102{
103 int ret = 0;
104 int argn = 1;
105
106 if(argc == 2) {
107 if(strcmp(argv[1], "-h") == 0) {
2baf08e5 108 return -1;
5adba60f 109 }
110 }
111
112 while(argn < argc) {
113
114 switch(argv[argn][0]) {
115 case '-':
116 switch(argv[argn][1]) {
117 case 'n':
118 if(argn+1 < argc) {
119 trace_name = argv[argn+1];
120 argn++;
121 } else {
8eab4866 122 printf("Specify a trace name after -n.\n");
5adba60f 123 printf("\n");
2baf08e5 124 ret = EINVAL;
5adba60f 125 }
126
127 break;
b7f08766 128 case 'b':
129 op = CTL_OP_CREATE_START;
5adba60f 130 case 'c':
131 op = CTL_OP_CREATE;
bc0fcb31 132 break;
133 case 'm':
5adba60f 134 if(argn+1 < argc) {
135 mode_name = argv[argn+1];
136 argn++;
137 if(strcmp(mode_name, "normal") == 0)
138 mode = LTT_TRACE_NORMAL;
139 else if(strcmp(mode_name, "flight") == 0)
140 mode = LTT_TRACE_FLIGHT;
141 else {
142 printf("Invalid mode '%s'.\n", argv[argn]);
143 printf("\n");
2baf08e5 144 ret = EINVAL;
5adba60f 145 }
146 } else {
bc0fcb31 147 printf("Specify a mode after -m.\n");
5adba60f 148 printf("\n");
2baf08e5 149 ret = EINVAL;
5adba60f 150 }
151 break;
152 case 'r':
153 op = CTL_OP_DESTROY;
154 break;
b7f08766 155 case 'R':
156 op = CTL_OP_STOP_DESTROY;
157 break;
5adba60f 158 case 's':
159 op = CTL_OP_START;
160 break;
161 case 'q':
162 op = CTL_OP_STOP;
163 break;
988614ed 164 case 'z':
165 if(argn+1 < argc) {
166 subbuf_size = (unsigned)atoi(argv[argn+1]);
167 argn++;
168 } else {
169 printf("Specify a number of subbuffers after -z.\n");
170 printf("\n");
2baf08e5 171 ret = EINVAL;
988614ed 172 }
173 break;
174 case 'x':
175 if(argn+1 < argc) {
176 n_subbufs = (unsigned)atoi(argv[argn+1]);
177 argn++;
178 } else {
179 printf("Specify a subbuffer size after -x.\n");
180 printf("\n");
2baf08e5 181 ret = EINVAL;
988614ed 182 }
183 break;
5adba60f 184 case 'd':
185 op = CTL_OP_DAEMON;
186 break;
129fd24a 187 case 'e':
188 op = CTL_OP_DESCRIPTION;
189 break;
5adba60f 190 case 't':
191 if(argn+1 < argc) {
192 trace_root = argv[argn+1];
193 argn++;
194 } else {
988614ed 195 printf("Specify a trace root path after -t.\n");
5adba60f 196 printf("\n");
2baf08e5 197 ret = EINVAL;
5adba60f 198 }
199 break;
200 case 'l':
201 if(argn+1 < argc) {
202 channel_root = argv[argn+1];
203 argn++;
204 } else {
988614ed 205 printf("Specify a channel root path after -l.\n");
5adba60f 206 printf("\n");
2baf08e5 207 ret = EINVAL;
5adba60f 208 }
209 break;
45653836 210 case 'a':
211 append_trace = 1;
212 break;
22acb617 213 case 'N':
214 if(argn+1 < argc) {
215 num_threads = argv[argn+1];
216 argn++;
217 }
218 break;
5adba60f 219 default:
220 printf("Invalid argument '%s'.\n", argv[argn]);
221 printf("\n");
2baf08e5 222 ret = EINVAL;
5adba60f 223 }
224 break;
225 default:
226 printf("Invalid argument '%s'.\n", argv[argn]);
227 printf("\n");
2baf08e5 228 ret = EINVAL;
5adba60f 229 }
230 argn++;
231 }
232
129fd24a 233 if(op != CTL_OP_DESCRIPTION && trace_name == NULL) {
5adba60f 234 printf("Please specify a trace name.\n");
235 printf("\n");
2baf08e5 236 ret = EINVAL;
5adba60f 237 }
238
239 if(op == CTL_OP_NONE) {
240 printf("Please specify an operation.\n");
241 printf("\n");
2baf08e5 242 ret = EINVAL;
5adba60f 243 }
244
245 if(op == CTL_OP_DAEMON) {
246 if(trace_root == NULL) {
247 printf("Please specify -t trace_root_path with the -d option.\n");
248 printf("\n");
2baf08e5 249 ret = EINVAL;
5adba60f 250 }
251 if(channel_root == NULL) {
252 printf("Please specify -l ltt_root_path with the -d option.\n");
253 printf("\n");
2baf08e5 254 ret = EINVAL;
5adba60f 255 }
256 }
257
129fd24a 258 if(op == CTL_OP_DESCRIPTION) {
259 if(trace_root == NULL) {
260 printf("Please specify -t trace_root_path with the -e option.\n");
261 printf("\n");
2baf08e5 262 ret = EINVAL;
129fd24a 263 }
264 }
265
5adba60f 266 return ret;
267}
268
269void show_info(void)
270{
271 printf("Linux Trace Toolkit Trace Control\n");
272 printf("\n");
129fd24a 273 if(trace_name != NULL) {
274 printf("Controlling trace : %s\n", trace_name);
275 printf("\n");
276 }
277}
278
279int create_eventdefs(void)
280{
281 int ret = 0;
282 char eventdefs_path[PATH_MAX];
283 char eventdefs_file[PATH_MAX];
284 char facilities_file[PATH_MAX];
285 char read_buf[BUF_SIZE];
286 struct dirent *entry;
287 char *facilities_path = getenv("LTT_FACILITIES");
288 if(facilities_path == NULL) facilities_path =
86a65fdb 289 PACKAGE_DATA_DIR "/" PACKAGE "/facilities";
129fd24a 290
291 ret = mkdir(trace_root, S_IRWXU|S_IRWXG|S_IRWXO);
292 if(ret == -1 && errno != EEXIST) {
618fbcc1 293 ret = errno;
129fd24a 294 perror("Cannot create trace_root directory");
295 printf("trace_root is %s\n", trace_root);
296 goto error;
297 }
bd55296c 298 ret = 0;
129fd24a 299
300 size_t trace_root_len = strlen(trace_root);
301 strncpy(eventdefs_path, trace_root, PATH_MAX);
302 strncat(eventdefs_path, "/eventdefs/", PATH_MAX - trace_root_len);
303 size_t eventdefs_path_len = strlen(eventdefs_path);
304 ret = mkdir(eventdefs_path, S_IRWXU|S_IRWXG|S_IRWXO);
45653836 305 if(ret == -1 && (!append_trace || errno != EEXIST)) {
2baf08e5 306 ret = errno;
618fbcc1 307 perror("Cannot create eventdefs directory");
129fd24a 308 goto error;
309 }
bd55296c 310 ret = 0;
129fd24a 311
312 DIR *facilities_dir = opendir(facilities_path);
313
86a65fdb 314 if(facilities_dir == NULL) {
315 perror("Cannot open facilities directory");
2baf08e5 316 ret = EEXIST;
86a65fdb 317 goto error;
318 }
319
129fd24a 320 while((entry = readdir(facilities_dir)) != NULL) {
321 if(entry->d_name[0] == '.') continue;
322
323 printf("Appending facility file %s\n", entry->d_name);
324 strncpy(eventdefs_file, eventdefs_path, PATH_MAX);
325 strncat(eventdefs_file, entry->d_name, PATH_MAX - eventdefs_path_len);
326 /* Append to the file */
327 FILE *dest = fopen(eventdefs_file, "a");
328 if(!dest) {
329 perror("Cannot create eventdefs file");
330 continue;
331 }
332 strncpy(facilities_file, facilities_path, PATH_MAX);
333 size_t facilities_dir_len = strlen(facilities_path);
334 strncat(facilities_file, "/", PATH_MAX - facilities_dir_len);
335 strncat(facilities_file, entry->d_name, PATH_MAX - facilities_dir_len-1);
336 FILE *src = fopen(facilities_file, "r");
337 if(!src) {
2baf08e5 338 ret = errno;
618fbcc1 339 perror("Cannot open eventdefs file for reading");
129fd24a 340 goto close_dest;
341 }
342
343 do {
344 size_t read_size, write_size;
345 read_size = fread(read_buf, sizeof(char), BUF_SIZE, src);
346 if(ferror(src)) {
2baf08e5 347 ret = errno;
618fbcc1 348 perror("Cannot read eventdefs file");
129fd24a 349 goto close_src;
350 }
351 write_size = fwrite(read_buf, sizeof(char), read_size, dest);
352 if(ferror(dest)) {
2baf08e5 353 ret = errno;
618fbcc1 354 perror("Cannot write eventdefs file");
129fd24a 355 goto close_src;
356 }
357 } while(!feof(src));
358
359 /* Add spacing between facilities */
360 fwrite("\n", 1, 1, dest);
361
362close_src:
363 fclose(src);
364close_dest:
365 fclose(dest);
366 }
367
368 closedir(facilities_dir);
369
86a65fdb 370error:
129fd24a 371 return ret;
372
5adba60f 373}
374
129fd24a 375
5adba60f 376int lttctl_daemon(struct lttctl_handle *handle, char *trace_name)
377{
378 char channel_path[PATH_MAX] = "";
379 pid_t pid;
380 int ret;
381 char *lttd_path = getenv("LTT_DAEMON");
382 struct sigaction act;
383
86a65fdb 384 if(lttd_path == NULL) lttd_path =
385 PACKAGE_BIN_DIR "/lttd";
5adba60f 386
387 strcat(channel_path, channel_root);
388 strcat(channel_path, "/");
389 strcat(channel_path, trace_name);
390
391
988614ed 392 ret = lttctl_create_trace(handle, trace_name, mode, subbuf_size, n_subbufs);
5adba60f 393 if(ret != 0) goto create_error;
394
86a65fdb 395 act.sa_handler = sigchld_handler;
5adba60f 396 sigemptyset(&(act.sa_mask));
86a65fdb 397 sigaddset(&(act.sa_mask), SIGCHLD);
398 sigaction(SIGCHLD, &act, NULL);
ada84671 399
5adba60f 400 pid = fork();
401
402 if(pid > 0) {
86a65fdb 403 int status;
5adba60f 404 /* parent */
86a65fdb 405 while(!(sigchld_received)) pause();
406
407 waitpid(pid, &status, 0);
408 ret = 0;
409 if(WIFEXITED(status))
410 ret = WEXITSTATUS(status);
411 if(ret) goto start_error;
5adba60f 412
5adba60f 413 printf("Creating supplementary trace files\n");
129fd24a 414 ret = create_eventdefs();
415 if(ret) goto start_error;
5adba60f 416
417 } else if(pid == 0) {
418 /* child */
45653836 419 int ret;
420 if(append_trace)
421 ret = execlp(lttd_path, lttd_path, "-t", trace_root, "-c",
22acb617 422 channel_path, "-d", "-a", "-N", num_threads, NULL);
45653836 423 else
424 ret = execlp(lttd_path, lttd_path, "-t", trace_root, "-c",
22acb617 425 channel_path, "-d", "-N", num_threads, NULL);
5adba60f 426 if(ret) {
618fbcc1 427 ret = errno;
5adba60f 428 perror("Error in executing the lttd daemon");
618fbcc1 429 exit(ret);
5adba60f 430 }
431 } else {
432 /* error */
433 perror("Error in forking for lttd daemon");
5adba60f 434 }
435
436 ret = lttctl_start(handle, trace_name);
437 if(ret != 0) goto start_error;
438
439 return 0;
440
441 /* error handling */
442start_error:
86a65fdb 443 printf("Trace start error\n");
5adba60f 444 ret |= lttctl_destroy_trace(handle, trace_name);
445create_error:
446 return ret;
447}
448
449int main(int argc, char ** argv)
450{
451 int ret;
452 struct lttctl_handle *handle;
453
454 ret = parse_arguments(argc, argv);
455
456 if(ret != 0) show_arguments();
2baf08e5 457 if(ret == EINVAL) return EINVAL;
458 if(ret == -1) return 0;
5adba60f 459
460 show_info();
461
462 handle = lttctl_create_handle();
463
464 if(handle == NULL) return -1;
465
466 switch(op) {
b7f08766 467 case CTL_OP_CREATE_START:
468 ret = lttctl_create_trace(handle, trace_name, mode, subbuf_size,
469 n_subbufs);
470 if(!ret)
471 ret = lttctl_start(handle, trace_name);
472 break;
5adba60f 473 case CTL_OP_CREATE:
988614ed 474 ret = lttctl_create_trace(handle, trace_name, mode, subbuf_size,
475 n_subbufs);
24ba6df0 476 break;
5adba60f 477 case CTL_OP_DESTROY:
478 ret = lttctl_destroy_trace(handle, trace_name);
479 break;
b7f08766 480 case CTL_OP_STOP_DESTROY:
481 ret = lttctl_stop(handle, trace_name);
482 if(!ret)
483 ret = lttctl_destroy_trace(handle, trace_name);
484 break;
5adba60f 485 case CTL_OP_START:
486 ret = lttctl_start(handle, trace_name);
487 break;
488 case CTL_OP_STOP:
489 ret = lttctl_stop(handle, trace_name);
490 break;
491 case CTL_OP_DAEMON:
492 ret = lttctl_daemon(handle, trace_name);
493 break;
129fd24a 494 case CTL_OP_DESCRIPTION:
495 ret = create_eventdefs();
496 break;
5adba60f 497 case CTL_OP_NONE:
498 break;
499 }
500
501 ret |= lttctl_destroy_handle(handle);
502
503 return ret;
504}
This page took 0.050384 seconds and 4 git commands to generate.