Add --version command-line option to lttng.
[lttng-tools.git] / src / bin / lttng / lttng.c
CommitLineData
826d496d
MD
1/*
2 * Copyright (c) 2011 David Goulet <david.goulet@polymtl.ca>
fac6795d
DG
3 *
4 * This program is free software; you can redistribute it and/or modify
d14d33bf
AM
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
fac6795d
DG
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.
fac6795d
DG
16 */
17
18#define _GNU_SOURCE
fac6795d 19#include <getopt.h>
f3ed775e 20#include <signal.h>
fac6795d
DG
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
8db8d1dc
DG
24#include <sys/types.h>
25#include <sys/wait.h>
fac6795d 26#include <unistd.h>
3bd1e081 27#include <config.h>
730389d9 28#include <ctype.h>
fac6795d 29
5b97ec60 30#include <lttng/lttng.h>
db758600 31#include <common/error.h>
fac6795d 32
c399183f 33#include "command.h"
fac6795d
DG
34
35/* Variables */
36static char *progname;
f3ed775e
DG
37static int opt_no_sessiond;
38static char *opt_sessiond_path;
8db8d1dc 39static pid_t sessiond_pid;
5a532b68 40static volatile int recv_child_signal;
f3ed775e
DG
41
42enum {
f3ed775e 43 OPT_SESSION_PATH,
865abf65
SM
44 OPT_DUMP_OPTIONS,
45 OPT_DUMP_COMMANDS,
f3ed775e
DG
46};
47
48/* Getopt options. No first level command. */
49static struct option long_options[] = {
70318c38 50 {"version", 0, NULL, 'V'},
f3ed775e
DG
51 {"help", 0, NULL, 'h'},
52 {"group", 1, NULL, 'g'},
53 {"verbose", 0, NULL, 'v'},
54 {"quiet", 0, NULL, 'q'},
8490ae7b 55 {"no-sessiond", 0, NULL, 'n'},
f3ed775e 56 {"sessiond-path", 1, NULL, OPT_SESSION_PATH},
865abf65
SM
57 {"list-options", 0, NULL, OPT_DUMP_OPTIONS},
58 {"list-commands", 0, NULL, OPT_DUMP_COMMANDS},
f3ed775e
DG
59 {NULL, 0, NULL, 0}
60};
61
62/* First level command */
63static struct cmd_struct commands[] = {
64 { "list", cmd_list},
65 { "create", cmd_create},
66 { "destroy", cmd_destroy},
f3ed775e
DG
67 { "start", cmd_start},
68 { "stop", cmd_stop},
69 { "enable-event", cmd_enable_events},
e953ef25 70 { "disable-event", cmd_disable_events},
d36b8583 71 { "enable-channel", cmd_enable_channels},
26cc6b4e 72 { "disable-channel", cmd_disable_channels},
d65106b1 73 { "add-context", cmd_add_context},
3087ef18 74 { "set-session", cmd_set_session},
eb9cb8b7 75 { "version", cmd_version},
d0254c7c 76 { "calibrate", cmd_calibrate},
0c95f5b2 77 { "view", cmd_view},
9c6bda17
DG
78 { "enable-consumer", cmd_enable_consumer}, /* OBSOLETE */
79 { "disable-consumer", cmd_disable_consumer}, /* OBSOLETE */
f3ed775e
DG
80 { NULL, NULL} /* Array closure */
81};
82
83static void usage(FILE *ofp)
8c0faa1d 84{
c6d4a597 85 fprintf(ofp, "LTTng Trace Control " VERSION" - " VERSION_NAME"\n\n");
32a6298d 86 fprintf(ofp, "usage: lttng [OPTIONS] <COMMAND> [<ARGS>]\n");
f3ed775e
DG
87 fprintf(ofp, "\n");
88 fprintf(ofp, "Options:\n");
70318c38 89 fprintf(ofp, " -V, --version Show version\n");
99bab54f
TD
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");
f3ed775e
DG
98 fprintf(ofp, "\n");
99 fprintf(ofp, "Commands:\n");
00e2e675
DG
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");
00e2e675
DG
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");
f3ed775e 114 fprintf(ofp, "\n");
99bab54f
TD
115 fprintf(ofp, "Each command also has its own -h, --help option.\n");
116 fprintf(ofp, "\n");
f3ed775e
DG
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");
8c0faa1d
DG
119}
120
70318c38
SS
121static void version(FILE *ofp)
122{
123 fprintf(ofp, "%s (LTTng Trace Control) " VERSION" - " VERSION_NAME"\n",
124 progname);
125}
126
865abf65
SM
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 */
133static 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 */
157static 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
7442b2ba 170/*
f3ed775e 171 * clean_exit
96243366 172 */
f3ed775e 173static void clean_exit(int code)
96243366 174{
f3ed775e
DG
175 DBG("Clean exit");
176 exit(code);
894be886 177}
96243366 178
894be886 179/*
f3ed775e 180 * sighandler
2ef84c95 181 *
f3ed775e 182 * Signal handler for the daemon
2ef84c95 183 */
f3ed775e 184static void sighandler(int sig)
2ef84c95 185{
8db8d1dc
DG
186 int status;
187
f3ed775e
DG
188 switch (sig) {
189 case SIGTERM:
99bab54f 190 DBG("SIGTERM caught");
f3ed775e
DG
191 clean_exit(EXIT_FAILURE);
192 break;
193 case SIGCHLD:
99bab54f 194 DBG("SIGCHLD caught");
8db8d1dc 195 waitpid(sessiond_pid, &status, 0);
5a532b68 196 recv_child_signal = 1;
8db8d1dc
DG
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 */
5a532b68 203 recv_child_signal = 1;
99bab54f 204 DBG("SIGUSR1 caught");
f3ed775e
DG
205 break;
206 default:
99bab54f 207 DBG("Unknown signal %d caught", sig);
f3ed775e 208 break;
2ef84c95
DG
209 }
210
f3ed775e 211 return;
2ef84c95
DG
212}
213
214/*
f3ed775e 215 * set_signal_handler
894be886 216 *
f3ed775e 217 * Setup signal handler for SIGCHLD and SIGTERM.
894be886 218 */
f3ed775e 219static int set_signal_handler(void)
894be886 220{
f3ed775e
DG
221 int ret = 0;
222 struct sigaction sa;
223 sigset_t sigset;
33a2b854 224
f3ed775e
DG
225 if ((ret = sigemptyset(&sigset)) < 0) {
226 perror("sigemptyset");
33a2b854
DG
227 goto end;
228 }
229
f3ed775e
DG
230 sa.sa_handler = sighandler;
231 sa.sa_mask = sigset;
232 sa.sa_flags = 0;
8db8d1dc 233 if ((ret = sigaction(SIGUSR1, &sa, NULL)) < 0) {
f3ed775e 234 perror("sigaction");
96243366
DG
235 goto end;
236 }
237
f3ed775e
DG
238 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
239 perror("sigaction");
240 goto end;
894be886
DG
241 }
242
8db8d1dc
DG
243 if ((ret = sigaction(SIGCHLD, &sa, NULL)) < 0) {
244 perror("sigaction");
245 goto end;
246 }
247
96243366 248end:
57167058
DG
249 return ret;
250}
251
fac6795d 252/*
f3ed775e 253 * handle_command
fac6795d 254 *
f3ed775e
DG
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.
1c9f7941 257 *
f3ed775e
DG
258 * If command not found, return -1
259 * else, return function command error code.
1c9f7941 260 */
f3ed775e 261static int handle_command(int argc, char **argv)
1c9f7941 262{
f3ed775e
DG
263 int i = 0, ret;
264 struct cmd_struct *cmd;
1c9f7941 265
f3ed775e
DG
266 if (*argv == NULL) {
267 ret = CMD_SUCCESS;
47b74d63 268 goto end;
1c9f7941
DG
269 }
270
f3ed775e
DG
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);
f3ed775e 276 goto end;
894be886 277 }
f3ed775e
DG
278 i++;
279 cmd = &commands[i];
894be886
DG
280 }
281
f3ed775e 282 /* Command not found */
4ce78777 283 ret = CMD_UNDEFINED;
894be886
DG
284
285end:
f3ed775e 286 return ret;
8548ff30
DG
287}
288
5b8719f5
DG
289/*
290 * spawn_sessiond
291 *
292 * Spawn a session daemon by forking and execv.
293 */
294static int spawn_sessiond(char *pathname)
295{
296 int ret = 0;
297 pid_t pid;
298
f3ed775e 299 MSG("Spawning a session daemon");
5a532b68 300 recv_child_signal = 0;
5b8719f5
DG
301 pid = fork();
302 if (pid == 0) {
5e16da05
MD
303 /*
304 * Spawn session daemon and tell
5b8719f5
DG
305 * it to signal us when ready.
306 */
32258573 307 execlp(pathname, "lttng-sessiond", "--sig-parent", "--quiet", NULL);
5e16da05
MD
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");
5b8719f5 313 }
5a532b68 314 kill(getppid(), SIGTERM); /* wake parent */
5e16da05 315 exit(EXIT_FAILURE);
5b8719f5 316 } else if (pid > 0) {
8db8d1dc 317 sessiond_pid = pid;
5a532b68 318 /*
af87c45a
DG
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.
5a532b68
MD
326 */
327 while (!recv_child_signal) {
328 sleep(1);
329 }
af87c45a
DG
330 /*
331 * The signal handler will nullify sessiond_pid on SIGCHLD
332 */
8db8d1dc
DG
333 if (!sessiond_pid) {
334 exit(EXIT_FAILURE);
335 }
5b8719f5
DG
336 goto end;
337 } else {
338 perror("fork");
339 ret = -1;
340 goto end;
341 }
342
343end:
344 return ret;
345}
346
fac6795d 347/*
f3ed775e 348 * check_sessiond
fac6795d
DG
349 *
350 * Check if the session daemon is available using
5b8719f5
DG
351 * the liblttngctl API for the check. If not, try to
352 * spawn a daemon.
fac6795d 353 */
f3ed775e 354static int check_sessiond(void)
fac6795d
DG
355{
356 int ret;
b8ec3da8 357 char *pathname = NULL;
fac6795d 358
947308c4
DG
359 ret = lttng_session_daemon_alive();
360 if (ret == 0) { /* not alive */
5b8719f5 361 /* Try command line option path */
b8ec3da8
SM
362 pathname = opt_sessiond_path;
363
364 /* Try LTTNG_SESSIOND_PATH env variable */
365 if (pathname == NULL) {
bbccc3d2 366 pathname = getenv(DEFAULT_SESSIOND_PATH_ENV);
5b8719f5
DG
367 }
368
b8ec3da8 369 /* Try with configured path */
5b8719f5 370 if (pathname == NULL) {
b8ec3da8
SM
371 if (CONFIG_SESSIOND_BIN[0] != '\0') {
372 pathname = CONFIG_SESSIOND_BIN;
5b8719f5 373 }
b8ec3da8
SM
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;
5b8719f5
DG
388 }
389
390 ret = spawn_sessiond(pathname);
5b8719f5 391 if (ret < 0) {
3183dbb0 392 ERR("Problem occurred when starting %s", pathname);
5b8719f5 393 }
c2a79a67 394 }
5b8719f5 395end:
fac6795d
DG
396 return ret;
397}
398
bcfa8a05 399/*
679b4943
SM
400 * Check args for specific options that *must* not trigger a session daemon
401 * execution.
402 *
403 * Return 1 if match else 0.
bcfa8a05 404 */
679b4943 405static int check_args_no_sessiond(int argc, char **argv)
bcfa8a05
DG
406{
407 int i;
408
409 for (i = 0; i < argc; i++) {
ba2926ef
MD
410 if ((strncmp(argv[i], "-h", sizeof("-h")) == 0) ||
411 strncmp(argv[i], "--h", sizeof("--h")) == 0 ||
3183dbb0 412 strncmp(argv[i], "--list-options", sizeof("--list-options")) == 0 ||
4747a49b 413 strncmp(argv[i], "--list-commands", sizeof("--list-commands")) == 0 ||
0c95f5b2
DG
414 strncmp(argv[i], "version", sizeof("version")) == 0 ||
415 strncmp(argv[i], "view", sizeof("view")) == 0) {
bcfa8a05
DG
416 return 1;
417 }
418 }
419
420 return 0;
421}
422
5b8719f5 423/*
3183dbb0 424 * Parse command line arguments.
5b8719f5 425 *
3183dbb0 426 * Return 0 if OK, else -1
5b8719f5 427 */
f3ed775e 428static int parse_args(int argc, char **argv)
5b8719f5 429{
f3ed775e 430 int opt, ret;
5b8719f5 431
f3ed775e
DG
432 if (argc < 2) {
433 usage(stderr);
434 clean_exit(EXIT_FAILURE);
5b8719f5
DG
435 }
436
70318c38 437 while ((opt = getopt_long(argc, argv, "+Vhnvqg:", long_options, NULL)) != -1) {
f3ed775e 438 switch (opt) {
70318c38
SS
439 case 'V':
440 version(stdout);
441 ret = 0;
442 goto end;
f3ed775e 443 case 'h':
3183dbb0 444 usage(stdout);
ae856491 445 ret = 0;
3183dbb0 446 goto end;
f3ed775e 447 case 'v':
97e19046 448 lttng_opt_verbose += 1;
f3ed775e
DG
449 break;
450 case 'q':
97e19046 451 lttng_opt_quiet = 1;
f3ed775e
DG
452 break;
453 case 'g':
454 lttng_set_tracing_group(optarg);
455 break;
8490ae7b 456 case 'n':
f3ed775e
DG
457 opt_no_sessiond = 1;
458 break;
459 case OPT_SESSION_PATH:
460 opt_sessiond_path = strdup(optarg);
461 break;
865abf65
SM
462 case OPT_DUMP_OPTIONS:
463 list_options(stdout);
464 ret = 0;
3183dbb0 465 goto end;
865abf65
SM
466 case OPT_DUMP_COMMANDS:
467 list_commands(stdout);
468 ret = 0;
3183dbb0 469 goto end;
f3ed775e
DG
470 default:
471 usage(stderr);
ae856491 472 ret = 1;
f3ed775e
DG
473 goto error;
474 }
5b8719f5 475 }
fac6795d 476
f3ed775e 477 /* If both options are specified, quiet wins */
97e19046
DG
478 if (lttng_opt_verbose && lttng_opt_quiet) {
479 lttng_opt_verbose = 0;
5b8719f5
DG
480 }
481
f3ed775e 482 /* Spawn session daemon if needed */
679b4943 483 if (opt_no_sessiond == 0 && check_args_no_sessiond(argc, argv) == 0 &&
bcfa8a05 484 (check_sessiond() < 0)) {
8005f29a 485 ret = 1;
f3ed775e 486 goto error;
5b8719f5
DG
487 }
488
f3ed775e
DG
489 /* No leftovers, print usage and quit */
490 if ((argc - optind) == 0) {
491 usage(stderr);
8005f29a 492 ret = 1;
f3ed775e
DG
493 goto error;
494 }
7442b2ba 495
f3ed775e
DG
496 /*
497 * Handle leftovers which is a first level command with the trailing
498 * options.
499 */
500 ret = handle_command(argc - optind, argv + optind);
ae856491
DG
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;
4ce78777
DG
514 case CMD_UNSUPPORTED:
515 ERR("Unsupported command");
516 break;
ae856491
DG
517 case -1:
518 usage(stderr);
519 ret = 1;
520 break;
521 case 0:
522 break;
523 default:
42224349
DG
524 if (ret < 0) {
525 ret = -ret;
526 }
ae856491 527 break;
96243366
DG
528 }
529
3183dbb0 530end:
f3ed775e 531error:
ae856491 532 return ret;
fac6795d
DG
533}
534
f3ed775e 535
fac6795d 536/*
5b8719f5 537 * main
fac6795d
DG
538 */
539int main(int argc, char *argv[])
540{
541 int ret;
cbbb813a 542 char *user;
fac6795d
DG
543
544 progname = argv[0] ? argv[0] : "lttng";
545
99bab54f 546 /* For Mathieu Desnoyers a.k.a. Dr. Tracing */
cbbb813a
DG
547 user = getenv("USER");
548 if (user != NULL && ((strncmp(progname, "drtrace", 7) == 0 ||
549 strncmp("compudj", user, 7) == 0))) {
4666e71c 550 MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n", 27,1,33,27,0);
fac6795d 551 }
cbbb813a 552 /* Thanks Mathieu */
fac6795d 553
5b8719f5
DG
554 ret = set_signal_handler();
555 if (ret < 0) {
87378cf5 556 clean_exit(ret);
5b8719f5
DG
557 }
558
f3ed775e 559 ret = parse_args(argc, argv);
ae856491
DG
560 if (ret != 0) {
561 clean_exit(ret);
1fff1faa 562 }
87378cf5 563
fac6795d
DG
564 return 0;
565}
This page took 0.12106 seconds and 4 git commands to generate.