Mi version command: add commit version to mi version
[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 41
8960e9cd
DG
42char *opt_relayd_path;
43
f3ed775e 44enum {
8960e9cd 45 OPT_RELAYD_PATH,
f3ed775e 46 OPT_SESSION_PATH,
865abf65
SM
47 OPT_DUMP_OPTIONS,
48 OPT_DUMP_COMMANDS,
f3ed775e
DG
49};
50
51/* Getopt options. No first level command. */
52static struct option long_options[] = {
70318c38 53 {"version", 0, NULL, 'V'},
f3ed775e
DG
54 {"help", 0, NULL, 'h'},
55 {"group", 1, NULL, 'g'},
56 {"verbose", 0, NULL, 'v'},
57 {"quiet", 0, NULL, 'q'},
c7e35b03 58 {"mi", 1, NULL, 'm'},
8490ae7b 59 {"no-sessiond", 0, NULL, 'n'},
f3ed775e 60 {"sessiond-path", 1, NULL, OPT_SESSION_PATH},
8960e9cd 61 {"relayd-path", 1, NULL, OPT_RELAYD_PATH},
865abf65
SM
62 {"list-options", 0, NULL, OPT_DUMP_OPTIONS},
63 {"list-commands", 0, NULL, OPT_DUMP_COMMANDS},
f3ed775e
DG
64 {NULL, 0, NULL, 0}
65};
66
67/* First level command */
68static struct cmd_struct commands[] = {
69 { "list", cmd_list},
70 { "create", cmd_create},
71 { "destroy", cmd_destroy},
f3ed775e
DG
72 { "start", cmd_start},
73 { "stop", cmd_stop},
74 { "enable-event", cmd_enable_events},
e953ef25 75 { "disable-event", cmd_disable_events},
d36b8583 76 { "enable-channel", cmd_enable_channels},
26cc6b4e 77 { "disable-channel", cmd_disable_channels},
d65106b1 78 { "add-context", cmd_add_context},
3087ef18 79 { "set-session", cmd_set_session},
eb9cb8b7 80 { "version", cmd_version},
d0254c7c 81 { "calibrate", cmd_calibrate},
0c95f5b2 82 { "view", cmd_view},
57f272ed 83 { "snapshot", cmd_snapshot},
c864d6d7 84 { "save", cmd_save},
8c42d845 85 { "load", cmd_load},
9c6bda17
DG
86 { "enable-consumer", cmd_enable_consumer}, /* OBSOLETE */
87 { "disable-consumer", cmd_disable_consumer}, /* OBSOLETE */
f3ed775e
DG
88 { NULL, NULL} /* Array closure */
89};
90
91static void usage(FILE *ofp)
8c0faa1d 92{
88564da0 93 fprintf(ofp, "LTTng Trace Control " FULL_VERSION" - " VERSION_NAME"\n\n");
32a6298d 94 fprintf(ofp, "usage: lttng [OPTIONS] <COMMAND> [<ARGS>]\n");
f3ed775e
DG
95 fprintf(ofp, "\n");
96 fprintf(ofp, "Options:\n");
70318c38 97 fprintf(ofp, " -V, --version Show version\n");
99bab54f
TD
98 fprintf(ofp, " -h, --help Show this help\n");
99 fprintf(ofp, " --list-options Simple listing of lttng options\n");
100 fprintf(ofp, " --list-commands Simple listing of lttng commands\n");
101 fprintf(ofp, " -v, --verbose Increase verbosity\n");
102 fprintf(ofp, " -q, --quiet Quiet mode\n");
c7e35b03
JR
103 fprintf(ofp, " -m, --mi TYPE Machine Interface mode.\n");
104 fprintf(ofp, " Type: xml\n");
99bab54f
TD
105 fprintf(ofp, " -g, --group NAME Unix tracing group name. (default: tracing)\n");
106 fprintf(ofp, " -n, --no-sessiond Don't spawn a session daemon\n");
107 fprintf(ofp, " --sessiond-path PATH Session daemon full path\n");
8960e9cd 108 fprintf(ofp, " --relayd-path PATH Relayd daemon full path\n");
f3ed775e
DG
109 fprintf(ofp, "\n");
110 fprintf(ofp, "Commands:\n");
00e2e675
DG
111 fprintf(ofp, " add-context Add context to event and/or channel\n");
112 fprintf(ofp, " calibrate Quantify LTTng overhead\n");
113 fprintf(ofp, " create Create tracing session\n");
114 fprintf(ofp, " destroy Tear down tracing session\n");
115 fprintf(ofp, " enable-channel Enable tracing channel\n");
116 fprintf(ofp, " enable-event Enable tracing event\n");
117 fprintf(ofp, " disable-channel Disable tracing channel\n");
118 fprintf(ofp, " disable-event Disable tracing event\n");
00e2e675
DG
119 fprintf(ofp, " list List possible tracing options\n");
120 fprintf(ofp, " set-session Set current session name\n");
57f272ed 121 fprintf(ofp, " snapshot Snapshot buffers of current session name\n");
00e2e675
DG
122 fprintf(ofp, " start Start tracing\n");
123 fprintf(ofp, " stop Stop tracing\n");
124 fprintf(ofp, " version Show version information\n");
125 fprintf(ofp, " view Start trace viewer\n");
c864d6d7 126 fprintf(ofp, " save Save session configuration\n");
8c42d845 127 fprintf(ofp, " load Load session configuration\n");
f3ed775e 128 fprintf(ofp, "\n");
99bab54f
TD
129 fprintf(ofp, "Each command also has its own -h, --help option.\n");
130 fprintf(ofp, "\n");
f3ed775e
DG
131 fprintf(ofp, "Please see the lttng(1) man page for full documentation.\n");
132 fprintf(ofp, "See http://lttng.org for updates, bug reports and news.\n");
8c0faa1d
DG
133}
134
70318c38
SS
135static void version(FILE *ofp)
136{
88564da0 137 fprintf(ofp, "%s (LTTng Trace Control) " FULL_VERSION" - " VERSION_NAME"\n",
70318c38
SS
138 progname);
139}
140
c7e35b03
JR
141/*
142 * Find the MI output type enum from a string. This function is for the support
143 * of machine interface output.
144 */
145static int mi_output_type(const char *output_type)
146{
147 int ret = 0;
148
149 if (!strncasecmp("xml", output_type, 3)) {
150 ret = LTTNG_MI_XML;
151 } else {
152 /* Invalid output format */
153 ERR("MI output format not supported");
154 ret = -LTTNG_ERR_MI_OUTPUT_TYPE;
155 }
156
157 return ret;
158}
159
865abf65
SM
160/*
161 * list_options
162 *
163 * List options line by line. This is mostly for bash auto completion and to
164 * avoid difficult parsing.
165 */
166static void list_options(FILE *ofp)
167{
168 int i = 0;
169 struct option *option = NULL;
170
171 option = &long_options[i];
172 while (option->name != NULL) {
173 fprintf(ofp, "--%s\n", option->name);
174
175 if (isprint(option->val)) {
176 fprintf(ofp, "-%c\n", option->val);
177 }
178
179 i++;
180 option = &long_options[i];
181 }
182}
183
7442b2ba 184/*
f3ed775e 185 * clean_exit
96243366 186 */
f3ed775e 187static void clean_exit(int code)
96243366 188{
f3ed775e
DG
189 DBG("Clean exit");
190 exit(code);
894be886 191}
96243366 192
894be886 193/*
f3ed775e 194 * sighandler
2ef84c95 195 *
f3ed775e 196 * Signal handler for the daemon
2ef84c95 197 */
f3ed775e 198static void sighandler(int sig)
2ef84c95 199{
8db8d1dc
DG
200 int status;
201
f3ed775e
DG
202 switch (sig) {
203 case SIGTERM:
99bab54f 204 DBG("SIGTERM caught");
f3ed775e
DG
205 clean_exit(EXIT_FAILURE);
206 break;
207 case SIGCHLD:
99bab54f 208 DBG("SIGCHLD caught");
8db8d1dc 209 waitpid(sessiond_pid, &status, 0);
5a532b68 210 recv_child_signal = 1;
8db8d1dc
DG
211 /* Indicate that the session daemon died */
212 sessiond_pid = 0;
213 ERR("Session daemon died (exit status %d)", WEXITSTATUS(status));
214 break;
215 case SIGUSR1:
216 /* Notify is done */
5a532b68 217 recv_child_signal = 1;
99bab54f 218 DBG("SIGUSR1 caught");
f3ed775e
DG
219 break;
220 default:
99bab54f 221 DBG("Unknown signal %d caught", sig);
f3ed775e 222 break;
2ef84c95
DG
223 }
224
f3ed775e 225 return;
2ef84c95
DG
226}
227
228/*
f3ed775e 229 * set_signal_handler
894be886 230 *
f3ed775e 231 * Setup signal handler for SIGCHLD and SIGTERM.
894be886 232 */
f3ed775e 233static int set_signal_handler(void)
894be886 234{
f3ed775e
DG
235 int ret = 0;
236 struct sigaction sa;
237 sigset_t sigset;
33a2b854 238
f3ed775e
DG
239 if ((ret = sigemptyset(&sigset)) < 0) {
240 perror("sigemptyset");
33a2b854
DG
241 goto end;
242 }
243
f3ed775e
DG
244 sa.sa_handler = sighandler;
245 sa.sa_mask = sigset;
246 sa.sa_flags = 0;
8db8d1dc 247 if ((ret = sigaction(SIGUSR1, &sa, NULL)) < 0) {
f3ed775e 248 perror("sigaction");
96243366
DG
249 goto end;
250 }
251
f3ed775e
DG
252 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
253 perror("sigaction");
254 goto end;
894be886
DG
255 }
256
8db8d1dc
DG
257 if ((ret = sigaction(SIGCHLD, &sa, NULL)) < 0) {
258 perror("sigaction");
259 goto end;
260 }
261
96243366 262end:
57167058
DG
263 return ret;
264}
265
fac6795d 266/*
f3ed775e 267 * handle_command
fac6795d 268 *
f3ed775e
DG
269 * Handle the full argv list of a first level command. Will find the command
270 * in the global commands array and call the function callback associated.
1c9f7941 271 *
f3ed775e
DG
272 * If command not found, return -1
273 * else, return function command error code.
1c9f7941 274 */
f3ed775e 275static int handle_command(int argc, char **argv)
1c9f7941 276{
f3ed775e
DG
277 int i = 0, ret;
278 struct cmd_struct *cmd;
1c9f7941 279
f3ed775e
DG
280 if (*argv == NULL) {
281 ret = CMD_SUCCESS;
47b74d63 282 goto end;
1c9f7941
DG
283 }
284
f3ed775e
DG
285 cmd = &commands[i];
286 while (cmd->func != NULL) {
287 /* Find command */
288 if (strcmp(argv[0], cmd->name) == 0) {
289 ret = cmd->func(argc, (const char**) argv);
f3ed775e 290 goto end;
894be886 291 }
f3ed775e
DG
292 i++;
293 cmd = &commands[i];
894be886
DG
294 }
295
f3ed775e 296 /* Command not found */
4ce78777 297 ret = CMD_UNDEFINED;
894be886
DG
298
299end:
f3ed775e 300 return ret;
8548ff30
DG
301}
302
5b8719f5
DG
303/*
304 * spawn_sessiond
305 *
306 * Spawn a session daemon by forking and execv.
307 */
308static int spawn_sessiond(char *pathname)
309{
310 int ret = 0;
311 pid_t pid;
312
f3ed775e 313 MSG("Spawning a session daemon");
5a532b68 314 recv_child_signal = 0;
5b8719f5
DG
315 pid = fork();
316 if (pid == 0) {
5e16da05
MD
317 /*
318 * Spawn session daemon and tell
5b8719f5
DG
319 * it to signal us when ready.
320 */
32258573 321 execlp(pathname, "lttng-sessiond", "--sig-parent", "--quiet", NULL);
5e16da05
MD
322 /* execlp only returns if error happened */
323 if (errno == ENOENT) {
324 ERR("No session daemon found. Use --sessiond-path.");
325 } else {
326 perror("execlp");
5b8719f5 327 }
5a532b68 328 kill(getppid(), SIGTERM); /* wake parent */
5e16da05 329 exit(EXIT_FAILURE);
5b8719f5 330 } else if (pid > 0) {
8db8d1dc 331 sessiond_pid = pid;
5a532b68 332 /*
af87c45a
DG
333 * Wait for lttng-sessiond to start. We need to use a flag to check if
334 * the signal has been sent to us, because the child can be scheduled
335 * before the parent, and thus send the signal before this check. In
336 * the signal handler, we set the recv_child_signal flag, so anytime we
337 * check it after the fork is fine. Note that sleep() is interrupted
338 * before the 1 second delay as soon as the signal is received, so it
339 * will not cause visible delay for the user.
5a532b68
MD
340 */
341 while (!recv_child_signal) {
342 sleep(1);
343 }
af87c45a
DG
344 /*
345 * The signal handler will nullify sessiond_pid on SIGCHLD
346 */
8db8d1dc
DG
347 if (!sessiond_pid) {
348 exit(EXIT_FAILURE);
349 }
5b8719f5
DG
350 goto end;
351 } else {
352 perror("fork");
353 ret = -1;
354 goto end;
355 }
356
357end:
358 return ret;
359}
360
fac6795d 361/*
f3ed775e 362 * check_sessiond
fac6795d
DG
363 *
364 * Check if the session daemon is available using
5b8719f5
DG
365 * the liblttngctl API for the check. If not, try to
366 * spawn a daemon.
fac6795d 367 */
f3ed775e 368static int check_sessiond(void)
fac6795d
DG
369{
370 int ret;
b8ec3da8 371 char *pathname = NULL;
fac6795d 372
947308c4
DG
373 ret = lttng_session_daemon_alive();
374 if (ret == 0) { /* not alive */
5b8719f5 375 /* Try command line option path */
b8ec3da8
SM
376 pathname = opt_sessiond_path;
377
378 /* Try LTTNG_SESSIOND_PATH env variable */
379 if (pathname == NULL) {
bbccc3d2 380 pathname = getenv(DEFAULT_SESSIOND_PATH_ENV);
5b8719f5
DG
381 }
382
b8ec3da8 383 /* Try with configured path */
5b8719f5 384 if (pathname == NULL) {
b8ec3da8
SM
385 if (CONFIG_SESSIOND_BIN[0] != '\0') {
386 pathname = CONFIG_SESSIOND_BIN;
5b8719f5 387 }
b8ec3da8
SM
388 }
389
390 /* Let's rock and roll while trying the default path */
391 if (pathname == NULL) {
392 pathname = INSTALL_BIN_PATH "/lttng-sessiond";
393 }
394
395 DBG("Session daemon at: %s", pathname);
396
397 /* Check existence and permissions */
398 ret = access(pathname, F_OK | X_OK);
399 if (ret < 0) {
400 ERR("No such file or access denied: %s", pathname);
401 goto end;
5b8719f5
DG
402 }
403
404 ret = spawn_sessiond(pathname);
5b8719f5 405 if (ret < 0) {
3183dbb0 406 ERR("Problem occurred when starting %s", pathname);
5b8719f5 407 }
c2a79a67 408 }
5b8719f5 409end:
fac6795d
DG
410 return ret;
411}
412
bcfa8a05 413/*
679b4943
SM
414 * Check args for specific options that *must* not trigger a session daemon
415 * execution.
416 *
417 * Return 1 if match else 0.
bcfa8a05 418 */
679b4943 419static int check_args_no_sessiond(int argc, char **argv)
bcfa8a05
DG
420{
421 int i;
422
423 for (i = 0; i < argc; i++) {
ba2926ef
MD
424 if ((strncmp(argv[i], "-h", sizeof("-h")) == 0) ||
425 strncmp(argv[i], "--h", sizeof("--h")) == 0 ||
3183dbb0 426 strncmp(argv[i], "--list-options", sizeof("--list-options")) == 0 ||
4747a49b 427 strncmp(argv[i], "--list-commands", sizeof("--list-commands")) == 0 ||
0c95f5b2
DG
428 strncmp(argv[i], "version", sizeof("version")) == 0 ||
429 strncmp(argv[i], "view", sizeof("view")) == 0) {
bcfa8a05
DG
430 return 1;
431 }
432 }
433
434 return 0;
435}
436
5b8719f5 437/*
3183dbb0 438 * Parse command line arguments.
5b8719f5 439 *
3183dbb0 440 * Return 0 if OK, else -1
5b8719f5 441 */
f3ed775e 442static int parse_args(int argc, char **argv)
5b8719f5 443{
f3ed775e 444 int opt, ret;
5b8719f5 445
f3ed775e
DG
446 if (argc < 2) {
447 usage(stderr);
448 clean_exit(EXIT_FAILURE);
5b8719f5
DG
449 }
450
c7e35b03 451 while ((opt = getopt_long(argc, argv, "+Vhnvqg:m:", long_options, NULL)) != -1) {
f3ed775e 452 switch (opt) {
70318c38
SS
453 case 'V':
454 version(stdout);
455 ret = 0;
456 goto end;
f3ed775e 457 case 'h':
3183dbb0 458 usage(stdout);
ae856491 459 ret = 0;
3183dbb0 460 goto end;
f3ed775e 461 case 'v':
849e5b7b
DG
462 /* There is only 3 possible level of verbosity. (-vvv) */
463 if (lttng_opt_verbose < 3) {
464 lttng_opt_verbose += 1;
465 }
f3ed775e
DG
466 break;
467 case 'q':
97e19046 468 lttng_opt_quiet = 1;
f3ed775e 469 break;
c7e35b03
JR
470 case 'm':
471 lttng_opt_mi = mi_output_type(optarg);
472 if (lttng_opt_mi < 0) {
473 ret = lttng_opt_mi;
474 goto error;
475 }
476 break;
f3ed775e
DG
477 case 'g':
478 lttng_set_tracing_group(optarg);
479 break;
8490ae7b 480 case 'n':
f3ed775e
DG
481 opt_no_sessiond = 1;
482 break;
483 case OPT_SESSION_PATH:
484 opt_sessiond_path = strdup(optarg);
485 break;
8960e9cd
DG
486 case OPT_RELAYD_PATH:
487 opt_relayd_path = strdup(optarg);
488 break;
865abf65
SM
489 case OPT_DUMP_OPTIONS:
490 list_options(stdout);
491 ret = 0;
3183dbb0 492 goto end;
865abf65 493 case OPT_DUMP_COMMANDS:
3c9bd23c 494 list_commands(commands, stdout);
865abf65 495 ret = 0;
3183dbb0 496 goto end;
f3ed775e
DG
497 default:
498 usage(stderr);
ae856491 499 ret = 1;
f3ed775e
DG
500 goto error;
501 }
5b8719f5 502 }
fac6795d 503
f3ed775e 504 /* If both options are specified, quiet wins */
97e19046
DG
505 if (lttng_opt_verbose && lttng_opt_quiet) {
506 lttng_opt_verbose = 0;
5b8719f5
DG
507 }
508
f3ed775e 509 /* Spawn session daemon if needed */
679b4943 510 if (opt_no_sessiond == 0 && check_args_no_sessiond(argc, argv) == 0 &&
bcfa8a05 511 (check_sessiond() < 0)) {
8005f29a 512 ret = 1;
f3ed775e 513 goto error;
5b8719f5
DG
514 }
515
f3ed775e
DG
516 /* No leftovers, print usage and quit */
517 if ((argc - optind) == 0) {
518 usage(stderr);
8005f29a 519 ret = 1;
f3ed775e
DG
520 goto error;
521 }
7442b2ba 522
f3ed775e
DG
523 /*
524 * Handle leftovers which is a first level command with the trailing
525 * options.
526 */
527 ret = handle_command(argc - optind, argv + optind);
ae856491
DG
528 switch (ret) {
529 case CMD_WARNING:
530 WARN("Some command(s) went wrong");
531 break;
532 case CMD_ERROR:
533 ERR("Command error");
534 break;
535 case CMD_UNDEFINED:
536 ERR("Undefined command");
537 break;
538 case CMD_FATAL:
539 ERR("Fatal error");
540 break;
4ce78777
DG
541 case CMD_UNSUPPORTED:
542 ERR("Unsupported command");
543 break;
ae856491
DG
544 case -1:
545 usage(stderr);
546 ret = 1;
547 break;
548 case 0:
549 break;
550 default:
42224349
DG
551 if (ret < 0) {
552 ret = -ret;
553 }
ae856491 554 break;
96243366
DG
555 }
556
3183dbb0 557end:
f3ed775e 558error:
ae856491 559 return ret;
fac6795d
DG
560}
561
f3ed775e 562
fac6795d 563/*
5b8719f5 564 * main
fac6795d
DG
565 */
566int main(int argc, char *argv[])
567{
568 int ret;
cbbb813a 569 char *user;
fac6795d
DG
570
571 progname = argv[0] ? argv[0] : "lttng";
572
99bab54f 573 /* For Mathieu Desnoyers a.k.a. Dr. Tracing */
cbbb813a
DG
574 user = getenv("USER");
575 if (user != NULL && ((strncmp(progname, "drtrace", 7) == 0 ||
576 strncmp("compudj", user, 7) == 0))) {
4666e71c 577 MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n", 27,1,33,27,0);
fac6795d 578 }
cbbb813a 579 /* Thanks Mathieu */
fac6795d 580
5b8719f5
DG
581 ret = set_signal_handler();
582 if (ret < 0) {
87378cf5 583 clean_exit(ret);
5b8719f5
DG
584 }
585
f3ed775e 586 ret = parse_args(argc, argv);
ae856491
DG
587 if (ret != 0) {
588 clean_exit(ret);
1fff1faa 589 }
87378cf5 590
fac6795d
DG
591 return 0;
592}
This page took 0.067701 seconds and 4 git commands to generate.