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