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