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