Fix: move easter egg after arg parsing else it breaks the mi test
[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 char *opt_relayd_path;
43
44 enum {
45 OPT_RELAYD_PATH,
46 OPT_SESSION_PATH,
47 OPT_DUMP_OPTIONS,
48 OPT_DUMP_COMMANDS,
49 };
50
51 /* Getopt options. No first level command. */
52 static struct option long_options[] = {
53 {"version", 0, NULL, 'V'},
54 {"help", 0, NULL, 'h'},
55 {"group", 1, NULL, 'g'},
56 {"verbose", 0, NULL, 'v'},
57 {"quiet", 0, NULL, 'q'},
58 {"mi", 1, NULL, 'm'},
59 {"no-sessiond", 0, NULL, 'n'},
60 {"sessiond-path", 1, NULL, OPT_SESSION_PATH},
61 {"relayd-path", 1, NULL, OPT_RELAYD_PATH},
62 {"list-options", 0, NULL, OPT_DUMP_OPTIONS},
63 {"list-commands", 0, NULL, OPT_DUMP_COMMANDS},
64 {NULL, 0, NULL, 0}
65 };
66
67 /* First level command */
68 static struct cmd_struct commands[] = {
69 { "list", cmd_list},
70 { "create", cmd_create},
71 { "destroy", cmd_destroy},
72 { "start", cmd_start},
73 { "stop", cmd_stop},
74 { "enable-event", cmd_enable_events},
75 { "disable-event", cmd_disable_events},
76 { "enable-channel", cmd_enable_channels},
77 { "disable-channel", cmd_disable_channels},
78 { "add-context", cmd_add_context},
79 { "set-session", cmd_set_session},
80 { "version", cmd_version},
81 { "calibrate", cmd_calibrate},
82 { "view", cmd_view},
83 { "snapshot", cmd_snapshot},
84 { "save", cmd_save},
85 { "load", cmd_load},
86 { "enable-consumer", cmd_enable_consumer}, /* OBSOLETE */
87 { "disable-consumer", cmd_disable_consumer}, /* OBSOLETE */
88 { NULL, NULL} /* Array closure */
89 };
90
91 static void usage(FILE *ofp)
92 {
93 fprintf(ofp, "LTTng Trace Control " VERSION " - " VERSION_NAME "%s\n\n",
94 GIT_VERSION[0] == '\0' ? "" : " - " GIT_VERSION);
95 fprintf(ofp, "usage: lttng [OPTIONS] <COMMAND> [<ARGS>]\n");
96 fprintf(ofp, "\n");
97 fprintf(ofp, "Options:\n");
98 fprintf(ofp, " -V, --version Show version\n");
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");
104 fprintf(ofp, " -m, --mi TYPE Machine Interface mode.\n");
105 fprintf(ofp, " Type: xml\n");
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");
109 fprintf(ofp, " --relayd-path PATH Relayd daemon full path\n");
110 fprintf(ofp, "\n");
111 fprintf(ofp, "Commands:\n");
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");
120 fprintf(ofp, " list List possible tracing options\n");
121 fprintf(ofp, " set-session Set current session name\n");
122 fprintf(ofp, " snapshot Snapshot buffers of current session name\n");
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");
127 fprintf(ofp, " save Save session configuration\n");
128 fprintf(ofp, " load Load session configuration\n");
129 fprintf(ofp, "\n");
130 fprintf(ofp, "Each command also has its own -h, --help option.\n");
131 fprintf(ofp, "\n");
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");
134 }
135
136 static void version(FILE *ofp)
137 {
138 fprintf(ofp, "%s (LTTng Trace Control) " VERSION" - " VERSION_NAME "%s\n",
139 progname,
140 GIT_VERSION[0] == '\0' ? "" : " - " GIT_VERSION);
141 }
142
143 /*
144 * Find the MI output type enum from a string. This function is for the support
145 * of machine interface output.
146 */
147 static 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
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 */
168 static 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
186 /*
187 * clean_exit
188 */
189 static void clean_exit(int code)
190 {
191 DBG("Clean exit");
192 exit(code);
193 }
194
195 /*
196 * sighandler
197 *
198 * Signal handler for the daemon
199 */
200 static void sighandler(int sig)
201 {
202 int status;
203
204 switch (sig) {
205 case SIGTERM:
206 DBG("SIGTERM caught");
207 clean_exit(EXIT_FAILURE);
208 break;
209 case SIGCHLD:
210 DBG("SIGCHLD caught");
211 waitpid(sessiond_pid, &status, 0);
212 recv_child_signal = 1;
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 */
219 recv_child_signal = 1;
220 DBG("SIGUSR1 caught");
221 break;
222 default:
223 DBG("Unknown signal %d caught", sig);
224 break;
225 }
226
227 return;
228 }
229
230 /*
231 * set_signal_handler
232 *
233 * Setup signal handler for SIGCHLD and SIGTERM.
234 */
235 static int set_signal_handler(void)
236 {
237 int ret = 0;
238 struct sigaction sa;
239 sigset_t sigset;
240
241 if ((ret = sigemptyset(&sigset)) < 0) {
242 perror("sigemptyset");
243 goto end;
244 }
245
246 sa.sa_handler = sighandler;
247 sa.sa_mask = sigset;
248 sa.sa_flags = 0;
249 if ((ret = sigaction(SIGUSR1, &sa, NULL)) < 0) {
250 perror("sigaction");
251 goto end;
252 }
253
254 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
255 perror("sigaction");
256 goto end;
257 }
258
259 if ((ret = sigaction(SIGCHLD, &sa, NULL)) < 0) {
260 perror("sigaction");
261 goto end;
262 }
263
264 end:
265 return ret;
266 }
267
268 /*
269 * handle_command
270 *
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.
273 *
274 * If command not found, return -1
275 * else, return function command error code.
276 */
277 static int handle_command(int argc, char **argv)
278 {
279 int i = 0, ret;
280 struct cmd_struct *cmd;
281
282 if (*argv == NULL) {
283 ret = CMD_SUCCESS;
284 goto end;
285 }
286
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);
292 goto end;
293 }
294 i++;
295 cmd = &commands[i];
296 }
297
298 /* Command not found */
299 ret = CMD_UNDEFINED;
300
301 end:
302 return ret;
303 }
304
305 /*
306 * spawn_sessiond
307 *
308 * Spawn a session daemon by forking and execv.
309 */
310 static int spawn_sessiond(char *pathname)
311 {
312 int ret = 0;
313 pid_t pid;
314
315 MSG("Spawning a session daemon");
316 recv_child_signal = 0;
317 pid = fork();
318 if (pid == 0) {
319 /*
320 * Spawn session daemon and tell
321 * it to signal us when ready.
322 */
323 execlp(pathname, "lttng-sessiond", "--sig-parent", "--quiet", NULL);
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");
329 }
330 kill(getppid(), SIGTERM); /* wake parent */
331 exit(EXIT_FAILURE);
332 } else if (pid > 0) {
333 sessiond_pid = pid;
334 /*
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.
342 */
343 while (!recv_child_signal) {
344 sleep(1);
345 }
346 /*
347 * The signal handler will nullify sessiond_pid on SIGCHLD
348 */
349 if (!sessiond_pid) {
350 exit(EXIT_FAILURE);
351 }
352 goto end;
353 } else {
354 perror("fork");
355 ret = -1;
356 goto end;
357 }
358
359 end:
360 return ret;
361 }
362
363 /*
364 * check_sessiond
365 *
366 * Check if the session daemon is available using
367 * the liblttngctl API for the check. If not, try to
368 * spawn a daemon.
369 */
370 static int check_sessiond(void)
371 {
372 int ret;
373 char *pathname = NULL;
374
375 ret = lttng_session_daemon_alive();
376 if (ret == 0) { /* not alive */
377 /* Try command line option path */
378 pathname = opt_sessiond_path;
379
380 /* Try LTTNG_SESSIOND_PATH env variable */
381 if (pathname == NULL) {
382 pathname = getenv(DEFAULT_SESSIOND_PATH_ENV);
383 }
384
385 /* Try with configured path */
386 if (pathname == NULL) {
387 if (CONFIG_SESSIOND_BIN[0] != '\0') {
388 pathname = CONFIG_SESSIOND_BIN;
389 }
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;
404 }
405
406 ret = spawn_sessiond(pathname);
407 if (ret < 0) {
408 ERR("Problem occurred when starting %s", pathname);
409 }
410 }
411 end:
412 return ret;
413 }
414
415 /*
416 * Check args for specific options that *must* not trigger a session daemon
417 * execution.
418 *
419 * Return 1 if match else 0.
420 */
421 static int check_args_no_sessiond(int argc, char **argv)
422 {
423 int i;
424
425 for (i = 0; i < argc; i++) {
426 if ((strncmp(argv[i], "-h", sizeof("-h")) == 0) ||
427 strncmp(argv[i], "--h", sizeof("--h")) == 0 ||
428 strncmp(argv[i], "--list-options", sizeof("--list-options")) == 0 ||
429 strncmp(argv[i], "--list-commands", sizeof("--list-commands")) == 0 ||
430 strncmp(argv[i], "version", sizeof("version")) == 0 ||
431 strncmp(argv[i], "view", sizeof("view")) == 0) {
432 return 1;
433 }
434 }
435
436 return 0;
437 }
438
439 /*
440 * Parse command line arguments.
441 *
442 * Return 0 if OK, else -1
443 */
444 static int parse_args(int argc, char **argv)
445 {
446 int opt, ret;
447 char *user;
448
449 if (argc < 2) {
450 usage(stderr);
451 clean_exit(EXIT_FAILURE);
452 }
453
454 while ((opt = getopt_long(argc, argv, "+Vhnvqg:m:", long_options, NULL)) != -1) {
455 switch (opt) {
456 case 'V':
457 version(stdout);
458 ret = 0;
459 goto end;
460 case 'h':
461 usage(stdout);
462 ret = 0;
463 goto end;
464 case 'v':
465 /* There is only 3 possible level of verbosity. (-vvv) */
466 if (lttng_opt_verbose < 3) {
467 lttng_opt_verbose += 1;
468 }
469 break;
470 case 'q':
471 lttng_opt_quiet = 1;
472 break;
473 case 'm':
474 lttng_opt_mi = mi_output_type(optarg);
475 if (lttng_opt_mi < 0) {
476 ret = lttng_opt_mi;
477 goto error;
478 }
479 break;
480 case 'g':
481 lttng_set_tracing_group(optarg);
482 break;
483 case 'n':
484 opt_no_sessiond = 1;
485 break;
486 case OPT_SESSION_PATH:
487 opt_sessiond_path = strdup(optarg);
488 break;
489 case OPT_RELAYD_PATH:
490 opt_relayd_path = strdup(optarg);
491 break;
492 case OPT_DUMP_OPTIONS:
493 list_options(stdout);
494 ret = 0;
495 goto end;
496 case OPT_DUMP_COMMANDS:
497 list_commands(commands, stdout);
498 ret = 0;
499 goto end;
500 default:
501 usage(stderr);
502 ret = 1;
503 goto error;
504 }
505 }
506
507 /* If both options are specified, quiet wins */
508 if (lttng_opt_verbose && lttng_opt_quiet) {
509 lttng_opt_verbose = 0;
510 }
511
512 /* Spawn session daemon if needed */
513 if (opt_no_sessiond == 0 && check_args_no_sessiond(argc, argv) == 0 &&
514 (check_sessiond() < 0)) {
515 ret = 1;
516 goto error;
517 }
518
519 /* No leftovers, print usage and quit */
520 if ((argc - optind) == 0) {
521 usage(stderr);
522 ret = 1;
523 goto error;
524 }
525
526 /* For Mathieu Desnoyers a.k.a. Dr. Tracing */
527 user = getenv("USER");
528 if (user != NULL && ((strncmp(progname, "drtrace", 7) == 0 ||
529 strncmp("compudj", user, 7) == 0))) {
530 MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n", 27,1,33,27,0);
531 }
532 /* Thanks Mathieu */
533
534 /*
535 * Handle leftovers which is a first level command with the trailing
536 * options.
537 */
538 ret = handle_command(argc - optind, argv + optind);
539 switch (ret) {
540 case CMD_WARNING:
541 WARN("Some command(s) went wrong");
542 break;
543 case CMD_ERROR:
544 ERR("Command error");
545 break;
546 case CMD_UNDEFINED:
547 ERR("Undefined command");
548 break;
549 case CMD_FATAL:
550 ERR("Fatal error");
551 break;
552 case CMD_UNSUPPORTED:
553 ERR("Unsupported command");
554 break;
555 case -1:
556 usage(stderr);
557 ret = 1;
558 break;
559 case 0:
560 break;
561 default:
562 if (ret < 0) {
563 ret = -ret;
564 }
565 break;
566 }
567
568 end:
569 error:
570 return ret;
571 }
572
573
574 /*
575 * main
576 */
577 int main(int argc, char *argv[])
578 {
579 int ret;
580
581 progname = argv[0] ? argv[0] : "lttng";
582
583 ret = set_signal_handler();
584 if (ret < 0) {
585 clean_exit(ret);
586 }
587
588 ret = parse_args(argc, argv);
589 if (ret != 0) {
590 clean_exit(ret);
591 }
592
593 return 0;
594 }
This page took 0.041596 seconds and 5 git commands to generate.