Add --version command-line option to lttng.
[lttng-tools.git] / src / bin / lttng / lttng.c
1 /*
2 * Copyright (c) 2011 David Goulet <david.goulet@polymtl.ca>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #define _GNU_SOURCE
19 #include <getopt.h>
20 #include <signal.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/types.h>
25 #include <sys/wait.h>
26 #include <unistd.h>
27 #include <config.h>
28 #include <ctype.h>
29
30 #include <lttng/lttng.h>
31 #include <common/error.h>
32
33 #include "command.h"
34
35 /* Variables */
36 static char *progname;
37 static int opt_no_sessiond;
38 static char *opt_sessiond_path;
39 static pid_t sessiond_pid;
40 static volatile int recv_child_signal;
41
42 enum {
43 OPT_SESSION_PATH,
44 OPT_DUMP_OPTIONS,
45 OPT_DUMP_COMMANDS,
46 };
47
48 /* Getopt options. No first level command. */
49 static struct option long_options[] = {
50 {"version", 0, NULL, 'V'},
51 {"help", 0, NULL, 'h'},
52 {"group", 1, NULL, 'g'},
53 {"verbose", 0, NULL, 'v'},
54 {"quiet", 0, NULL, 'q'},
55 {"no-sessiond", 0, NULL, 'n'},
56 {"sessiond-path", 1, NULL, OPT_SESSION_PATH},
57 {"list-options", 0, NULL, OPT_DUMP_OPTIONS},
58 {"list-commands", 0, NULL, OPT_DUMP_COMMANDS},
59 {NULL, 0, NULL, 0}
60 };
61
62 /* First level command */
63 static struct cmd_struct commands[] = {
64 { "list", cmd_list},
65 { "create", cmd_create},
66 { "destroy", cmd_destroy},
67 { "start", cmd_start},
68 { "stop", cmd_stop},
69 { "enable-event", cmd_enable_events},
70 { "disable-event", cmd_disable_events},
71 { "enable-channel", cmd_enable_channels},
72 { "disable-channel", cmd_disable_channels},
73 { "add-context", cmd_add_context},
74 { "set-session", cmd_set_session},
75 { "version", cmd_version},
76 { "calibrate", cmd_calibrate},
77 { "view", cmd_view},
78 { "enable-consumer", cmd_enable_consumer}, /* OBSOLETE */
79 { "disable-consumer", cmd_disable_consumer}, /* OBSOLETE */
80 { NULL, NULL} /* Array closure */
81 };
82
83 static void usage(FILE *ofp)
84 {
85 fprintf(ofp, "LTTng Trace Control " VERSION" - " VERSION_NAME"\n\n");
86 fprintf(ofp, "usage: lttng [OPTIONS] <COMMAND> [<ARGS>]\n");
87 fprintf(ofp, "\n");
88 fprintf(ofp, "Options:\n");
89 fprintf(ofp, " -V, --version Show version\n");
90 fprintf(ofp, " -h, --help Show this help\n");
91 fprintf(ofp, " --list-options Simple listing of lttng options\n");
92 fprintf(ofp, " --list-commands Simple listing of lttng commands\n");
93 fprintf(ofp, " -v, --verbose Increase verbosity\n");
94 fprintf(ofp, " -q, --quiet Quiet mode\n");
95 fprintf(ofp, " -g, --group NAME Unix tracing group name. (default: tracing)\n");
96 fprintf(ofp, " -n, --no-sessiond Don't spawn a session daemon\n");
97 fprintf(ofp, " --sessiond-path PATH Session daemon full path\n");
98 fprintf(ofp, "\n");
99 fprintf(ofp, "Commands:\n");
100 fprintf(ofp, " add-context Add context to event and/or channel\n");
101 fprintf(ofp, " calibrate Quantify LTTng overhead\n");
102 fprintf(ofp, " create Create tracing session\n");
103 fprintf(ofp, " destroy Tear down tracing session\n");
104 fprintf(ofp, " enable-channel Enable tracing channel\n");
105 fprintf(ofp, " enable-event Enable tracing event\n");
106 fprintf(ofp, " disable-channel Disable tracing channel\n");
107 fprintf(ofp, " disable-event Disable tracing event\n");
108 fprintf(ofp, " list List possible tracing options\n");
109 fprintf(ofp, " set-session Set current session name\n");
110 fprintf(ofp, " start Start tracing\n");
111 fprintf(ofp, " stop Stop tracing\n");
112 fprintf(ofp, " version Show version information\n");
113 fprintf(ofp, " view Start trace viewer\n");
114 fprintf(ofp, "\n");
115 fprintf(ofp, "Each command also has its own -h, --help option.\n");
116 fprintf(ofp, "\n");
117 fprintf(ofp, "Please see the lttng(1) man page for full documentation.\n");
118 fprintf(ofp, "See http://lttng.org for updates, bug reports and news.\n");
119 }
120
121 static void version(FILE *ofp)
122 {
123 fprintf(ofp, "%s (LTTng Trace Control) " VERSION" - " VERSION_NAME"\n",
124 progname);
125 }
126
127 /*
128 * list_options
129 *
130 * List options line by line. This is mostly for bash auto completion and to
131 * avoid difficult parsing.
132 */
133 static void list_options(FILE *ofp)
134 {
135 int i = 0;
136 struct option *option = NULL;
137
138 option = &long_options[i];
139 while (option->name != NULL) {
140 fprintf(ofp, "--%s\n", option->name);
141
142 if (isprint(option->val)) {
143 fprintf(ofp, "-%c\n", option->val);
144 }
145
146 i++;
147 option = &long_options[i];
148 }
149 }
150
151 /*
152 * list_commands
153 *
154 * List commands line by line. This is mostly for bash auto completion and to
155 * avoid difficult parsing.
156 */
157 static void list_commands(FILE *ofp)
158 {
159 int i = 0;
160 struct cmd_struct *cmd = NULL;
161
162 cmd = &commands[i];
163 while (cmd->name != NULL) {
164 fprintf(ofp, "%s\n", cmd->name);
165 i++;
166 cmd = &commands[i];
167 }
168 }
169
170 /*
171 * clean_exit
172 */
173 static void clean_exit(int code)
174 {
175 DBG("Clean exit");
176 exit(code);
177 }
178
179 /*
180 * sighandler
181 *
182 * Signal handler for the daemon
183 */
184 static void sighandler(int sig)
185 {
186 int status;
187
188 switch (sig) {
189 case SIGTERM:
190 DBG("SIGTERM caught");
191 clean_exit(EXIT_FAILURE);
192 break;
193 case SIGCHLD:
194 DBG("SIGCHLD caught");
195 waitpid(sessiond_pid, &status, 0);
196 recv_child_signal = 1;
197 /* Indicate that the session daemon died */
198 sessiond_pid = 0;
199 ERR("Session daemon died (exit status %d)", WEXITSTATUS(status));
200 break;
201 case SIGUSR1:
202 /* Notify is done */
203 recv_child_signal = 1;
204 DBG("SIGUSR1 caught");
205 break;
206 default:
207 DBG("Unknown signal %d caught", sig);
208 break;
209 }
210
211 return;
212 }
213
214 /*
215 * set_signal_handler
216 *
217 * Setup signal handler for SIGCHLD and SIGTERM.
218 */
219 static int set_signal_handler(void)
220 {
221 int ret = 0;
222 struct sigaction sa;
223 sigset_t sigset;
224
225 if ((ret = sigemptyset(&sigset)) < 0) {
226 perror("sigemptyset");
227 goto end;
228 }
229
230 sa.sa_handler = sighandler;
231 sa.sa_mask = sigset;
232 sa.sa_flags = 0;
233 if ((ret = sigaction(SIGUSR1, &sa, NULL)) < 0) {
234 perror("sigaction");
235 goto end;
236 }
237
238 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
239 perror("sigaction");
240 goto end;
241 }
242
243 if ((ret = sigaction(SIGCHLD, &sa, NULL)) < 0) {
244 perror("sigaction");
245 goto end;
246 }
247
248 end:
249 return ret;
250 }
251
252 /*
253 * handle_command
254 *
255 * Handle the full argv list of a first level command. Will find the command
256 * in the global commands array and call the function callback associated.
257 *
258 * If command not found, return -1
259 * else, return function command error code.
260 */
261 static int handle_command(int argc, char **argv)
262 {
263 int i = 0, ret;
264 struct cmd_struct *cmd;
265
266 if (*argv == NULL) {
267 ret = CMD_SUCCESS;
268 goto end;
269 }
270
271 cmd = &commands[i];
272 while (cmd->func != NULL) {
273 /* Find command */
274 if (strcmp(argv[0], cmd->name) == 0) {
275 ret = cmd->func(argc, (const char**) argv);
276 goto end;
277 }
278 i++;
279 cmd = &commands[i];
280 }
281
282 /* Command not found */
283 ret = CMD_UNDEFINED;
284
285 end:
286 return ret;
287 }
288
289 /*
290 * spawn_sessiond
291 *
292 * Spawn a session daemon by forking and execv.
293 */
294 static int spawn_sessiond(char *pathname)
295 {
296 int ret = 0;
297 pid_t pid;
298
299 MSG("Spawning a session daemon");
300 recv_child_signal = 0;
301 pid = fork();
302 if (pid == 0) {
303 /*
304 * Spawn session daemon and tell
305 * it to signal us when ready.
306 */
307 execlp(pathname, "lttng-sessiond", "--sig-parent", "--quiet", NULL);
308 /* execlp only returns if error happened */
309 if (errno == ENOENT) {
310 ERR("No session daemon found. Use --sessiond-path.");
311 } else {
312 perror("execlp");
313 }
314 kill(getppid(), SIGTERM); /* wake parent */
315 exit(EXIT_FAILURE);
316 } else if (pid > 0) {
317 sessiond_pid = pid;
318 /*
319 * Wait for lttng-sessiond to start. We need to use a flag to check if
320 * the signal has been sent to us, because the child can be scheduled
321 * before the parent, and thus send the signal before this check. In
322 * the signal handler, we set the recv_child_signal flag, so anytime we
323 * check it after the fork is fine. Note that sleep() is interrupted
324 * before the 1 second delay as soon as the signal is received, so it
325 * will not cause visible delay for the user.
326 */
327 while (!recv_child_signal) {
328 sleep(1);
329 }
330 /*
331 * The signal handler will nullify sessiond_pid on SIGCHLD
332 */
333 if (!sessiond_pid) {
334 exit(EXIT_FAILURE);
335 }
336 goto end;
337 } else {
338 perror("fork");
339 ret = -1;
340 goto end;
341 }
342
343 end:
344 return ret;
345 }
346
347 /*
348 * check_sessiond
349 *
350 * Check if the session daemon is available using
351 * the liblttngctl API for the check. If not, try to
352 * spawn a daemon.
353 */
354 static int check_sessiond(void)
355 {
356 int ret;
357 char *pathname = NULL;
358
359 ret = lttng_session_daemon_alive();
360 if (ret == 0) { /* not alive */
361 /* Try command line option path */
362 pathname = opt_sessiond_path;
363
364 /* Try LTTNG_SESSIOND_PATH env variable */
365 if (pathname == NULL) {
366 pathname = getenv(DEFAULT_SESSIOND_PATH_ENV);
367 }
368
369 /* Try with configured path */
370 if (pathname == NULL) {
371 if (CONFIG_SESSIOND_BIN[0] != '\0') {
372 pathname = CONFIG_SESSIOND_BIN;
373 }
374 }
375
376 /* Let's rock and roll while trying the default path */
377 if (pathname == NULL) {
378 pathname = INSTALL_BIN_PATH "/lttng-sessiond";
379 }
380
381 DBG("Session daemon at: %s", pathname);
382
383 /* Check existence and permissions */
384 ret = access(pathname, F_OK | X_OK);
385 if (ret < 0) {
386 ERR("No such file or access denied: %s", pathname);
387 goto end;
388 }
389
390 ret = spawn_sessiond(pathname);
391 if (ret < 0) {
392 ERR("Problem occurred when starting %s", pathname);
393 }
394 }
395 end:
396 return ret;
397 }
398
399 /*
400 * Check args for specific options that *must* not trigger a session daemon
401 * execution.
402 *
403 * Return 1 if match else 0.
404 */
405 static int check_args_no_sessiond(int argc, char **argv)
406 {
407 int i;
408
409 for (i = 0; i < argc; i++) {
410 if ((strncmp(argv[i], "-h", sizeof("-h")) == 0) ||
411 strncmp(argv[i], "--h", sizeof("--h")) == 0 ||
412 strncmp(argv[i], "--list-options", sizeof("--list-options")) == 0 ||
413 strncmp(argv[i], "--list-commands", sizeof("--list-commands")) == 0 ||
414 strncmp(argv[i], "version", sizeof("version")) == 0 ||
415 strncmp(argv[i], "view", sizeof("view")) == 0) {
416 return 1;
417 }
418 }
419
420 return 0;
421 }
422
423 /*
424 * Parse command line arguments.
425 *
426 * Return 0 if OK, else -1
427 */
428 static int parse_args(int argc, char **argv)
429 {
430 int opt, ret;
431
432 if (argc < 2) {
433 usage(stderr);
434 clean_exit(EXIT_FAILURE);
435 }
436
437 while ((opt = getopt_long(argc, argv, "+Vhnvqg:", long_options, NULL)) != -1) {
438 switch (opt) {
439 case 'V':
440 version(stdout);
441 ret = 0;
442 goto end;
443 case 'h':
444 usage(stdout);
445 ret = 0;
446 goto end;
447 case 'v':
448 lttng_opt_verbose += 1;
449 break;
450 case 'q':
451 lttng_opt_quiet = 1;
452 break;
453 case 'g':
454 lttng_set_tracing_group(optarg);
455 break;
456 case 'n':
457 opt_no_sessiond = 1;
458 break;
459 case OPT_SESSION_PATH:
460 opt_sessiond_path = strdup(optarg);
461 break;
462 case OPT_DUMP_OPTIONS:
463 list_options(stdout);
464 ret = 0;
465 goto end;
466 case OPT_DUMP_COMMANDS:
467 list_commands(stdout);
468 ret = 0;
469 goto end;
470 default:
471 usage(stderr);
472 ret = 1;
473 goto error;
474 }
475 }
476
477 /* If both options are specified, quiet wins */
478 if (lttng_opt_verbose && lttng_opt_quiet) {
479 lttng_opt_verbose = 0;
480 }
481
482 /* Spawn session daemon if needed */
483 if (opt_no_sessiond == 0 && check_args_no_sessiond(argc, argv) == 0 &&
484 (check_sessiond() < 0)) {
485 ret = 1;
486 goto error;
487 }
488
489 /* No leftovers, print usage and quit */
490 if ((argc - optind) == 0) {
491 usage(stderr);
492 ret = 1;
493 goto error;
494 }
495
496 /*
497 * Handle leftovers which is a first level command with the trailing
498 * options.
499 */
500 ret = handle_command(argc - optind, argv + optind);
501 switch (ret) {
502 case CMD_WARNING:
503 WARN("Some command(s) went wrong");
504 break;
505 case CMD_ERROR:
506 ERR("Command error");
507 break;
508 case CMD_UNDEFINED:
509 ERR("Undefined command");
510 break;
511 case CMD_FATAL:
512 ERR("Fatal error");
513 break;
514 case CMD_UNSUPPORTED:
515 ERR("Unsupported command");
516 break;
517 case -1:
518 usage(stderr);
519 ret = 1;
520 break;
521 case 0:
522 break;
523 default:
524 if (ret < 0) {
525 ret = -ret;
526 }
527 break;
528 }
529
530 end:
531 error:
532 return ret;
533 }
534
535
536 /*
537 * main
538 */
539 int main(int argc, char *argv[])
540 {
541 int ret;
542 char *user;
543
544 progname = argv[0] ? argv[0] : "lttng";
545
546 /* For Mathieu Desnoyers a.k.a. Dr. Tracing */
547 user = getenv("USER");
548 if (user != NULL && ((strncmp(progname, "drtrace", 7) == 0 ||
549 strncmp("compudj", user, 7) == 0))) {
550 MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n", 27,1,33,27,0);
551 }
552 /* Thanks Mathieu */
553
554 ret = set_signal_handler();
555 if (ret < 0) {
556 clean_exit(ret);
557 }
558
559 ret = parse_args(argc, argv);
560 if (ret != 0) {
561 clean_exit(ret);
562 }
563
564 return 0;
565 }
This page took 0.040743 seconds and 5 git commands to generate.