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