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