2 * Copyright (c) 2011 David Goulet <david.goulet@polymtl.ca>
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.
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.
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.
24 #include <sys/types.h>
30 #include <lttng/lttng.h>
31 #include <common/error.h>
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
;
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
},
62 /* First level command */
63 static struct cmd_struct commands
[] = {
65 { "create", cmd_create
},
66 { "destroy", cmd_destroy
},
67 { "start", cmd_start
},
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
},
78 { "enable-consumer", cmd_enable_consumer
}, /* OBSOLETE */
79 { "disable-consumer", cmd_disable_consumer
}, /* OBSOLETE */
80 { NULL
, NULL
} /* Array closure */
83 static void usage(FILE *ofp
)
85 fprintf(ofp
, "LTTng Trace Control " VERSION
" - " VERSION_NAME
"\n\n");
86 fprintf(ofp
, "usage: lttng [OPTIONS] <COMMAND> [<ARGS>]\n");
88 fprintf(ofp
, "Options:\n");
89 fprintf(ofp
, " -V, --version Show version\n");
90 fprintf(ofp
, " -h, --help Show this help\n");
91 fprintf(ofp
, " --list-options Simple listing of lttng options\n");
92 fprintf(ofp
, " --list-commands Simple listing of lttng commands\n");
93 fprintf(ofp
, " -v, --verbose Increase verbosity\n");
94 fprintf(ofp
, " -q, --quiet Quiet mode\n");
95 fprintf(ofp
, " -g, --group NAME Unix tracing group name. (default: tracing)\n");
96 fprintf(ofp
, " -n, --no-sessiond Don't spawn a session daemon\n");
97 fprintf(ofp
, " --sessiond-path PATH Session daemon full path\n");
99 fprintf(ofp
, "Commands:\n");
100 fprintf(ofp
, " add-context Add context to event and/or channel\n");
101 fprintf(ofp
, " calibrate Quantify LTTng overhead\n");
102 fprintf(ofp
, " create Create tracing session\n");
103 fprintf(ofp
, " destroy Tear down tracing session\n");
104 fprintf(ofp
, " enable-channel Enable tracing channel\n");
105 fprintf(ofp
, " enable-event Enable tracing event\n");
106 fprintf(ofp
, " disable-channel Disable tracing channel\n");
107 fprintf(ofp
, " disable-event Disable tracing event\n");
108 fprintf(ofp
, " list List possible tracing options\n");
109 fprintf(ofp
, " set-session Set current session name\n");
110 fprintf(ofp
, " start Start tracing\n");
111 fprintf(ofp
, " stop Stop tracing\n");
112 fprintf(ofp
, " version Show version information\n");
113 fprintf(ofp
, " view Start trace viewer\n");
115 fprintf(ofp
, "Each command also has its own -h, --help option.\n");
117 fprintf(ofp
, "Please see the lttng(1) man page for full documentation.\n");
118 fprintf(ofp
, "See http://lttng.org for updates, bug reports and news.\n");
121 static void version(FILE *ofp
)
123 fprintf(ofp
, "%s (LTTng Trace Control) " VERSION
" - " VERSION_NAME
"\n",
130 * List options line by line. This is mostly for bash auto completion and to
131 * avoid difficult parsing.
133 static void list_options(FILE *ofp
)
136 struct option
*option
= NULL
;
138 option
= &long_options
[i
];
139 while (option
->name
!= NULL
) {
140 fprintf(ofp
, "--%s\n", option
->name
);
142 if (isprint(option
->val
)) {
143 fprintf(ofp
, "-%c\n", option
->val
);
147 option
= &long_options
[i
];
154 * List commands line by line. This is mostly for bash auto completion and to
155 * avoid difficult parsing.
157 static void list_commands(FILE *ofp
)
160 struct cmd_struct
*cmd
= NULL
;
163 while (cmd
->name
!= NULL
) {
164 fprintf(ofp
, "%s\n", cmd
->name
);
173 static void clean_exit(int code
)
182 * Signal handler for the daemon
184 static void sighandler(int sig
)
190 DBG("SIGTERM caught");
191 clean_exit(EXIT_FAILURE
);
194 DBG("SIGCHLD caught");
195 waitpid(sessiond_pid
, &status
, 0);
196 recv_child_signal
= 1;
197 /* Indicate that the session daemon died */
199 ERR("Session daemon died (exit status %d)", WEXITSTATUS(status
));
203 recv_child_signal
= 1;
204 DBG("SIGUSR1 caught");
207 DBG("Unknown signal %d caught", sig
);
217 * Setup signal handler for SIGCHLD and SIGTERM.
219 static int set_signal_handler(void)
225 if ((ret
= sigemptyset(&sigset
)) < 0) {
226 perror("sigemptyset");
230 sa
.sa_handler
= sighandler
;
233 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
238 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
243 if ((ret
= sigaction(SIGCHLD
, &sa
, NULL
)) < 0) {
255 * Handle the full argv list of a first level command. Will find the command
256 * in the global commands array and call the function callback associated.
258 * If command not found, return -1
259 * else, return function command error code.
261 static int handle_command(int argc
, char **argv
)
264 struct cmd_struct
*cmd
;
272 while (cmd
->func
!= NULL
) {
274 if (strcmp(argv
[0], cmd
->name
) == 0) {
275 ret
= cmd
->func(argc
, (const char**) argv
);
282 /* Command not found */
292 * Spawn a session daemon by forking and execv.
294 static int spawn_sessiond(char *pathname
)
299 MSG("Spawning a session daemon");
300 recv_child_signal
= 0;
304 * Spawn session daemon and tell
305 * it to signal us when ready.
307 execlp(pathname
, "lttng-sessiond", "--sig-parent", "--quiet", NULL
);
308 /* execlp only returns if error happened */
309 if (errno
== ENOENT
) {
310 ERR("No session daemon found. Use --sessiond-path.");
314 kill(getppid(), SIGTERM
); /* wake parent */
316 } else if (pid
> 0) {
319 * Wait for lttng-sessiond to start. We need to use a flag to check if
320 * the signal has been sent to us, because the child can be scheduled
321 * before the parent, and thus send the signal before this check. In
322 * the signal handler, we set the recv_child_signal flag, so anytime we
323 * check it after the fork is fine. Note that sleep() is interrupted
324 * before the 1 second delay as soon as the signal is received, so it
325 * will not cause visible delay for the user.
327 while (!recv_child_signal
) {
331 * The signal handler will nullify sessiond_pid on SIGCHLD
350 * Check if the session daemon is available using
351 * the liblttngctl API for the check. If not, try to
354 static int check_sessiond(void)
357 char *pathname
= NULL
;
359 ret
= lttng_session_daemon_alive();
360 if (ret
== 0) { /* not alive */
361 /* Try command line option path */
362 pathname
= opt_sessiond_path
;
364 /* Try LTTNG_SESSIOND_PATH env variable */
365 if (pathname
== NULL
) {
366 pathname
= getenv(DEFAULT_SESSIOND_PATH_ENV
);
369 /* Try with configured path */
370 if (pathname
== NULL
) {
371 if (CONFIG_SESSIOND_BIN
[0] != '\0') {
372 pathname
= CONFIG_SESSIOND_BIN
;
376 /* Let's rock and roll while trying the default path */
377 if (pathname
== NULL
) {
378 pathname
= INSTALL_BIN_PATH
"/lttng-sessiond";
381 DBG("Session daemon at: %s", pathname
);
383 /* Check existence and permissions */
384 ret
= access(pathname
, F_OK
| X_OK
);
386 ERR("No such file or access denied: %s", pathname
);
390 ret
= spawn_sessiond(pathname
);
392 ERR("Problem occurred when starting %s", pathname
);
400 * Check args for specific options that *must* not trigger a session daemon
403 * Return 1 if match else 0.
405 static int check_args_no_sessiond(int argc
, char **argv
)
409 for (i
= 0; i
< argc
; i
++) {
410 if ((strncmp(argv
[i
], "-h", sizeof("-h")) == 0) ||
411 strncmp(argv
[i
], "--h", sizeof("--h")) == 0 ||
412 strncmp(argv
[i
], "--list-options", sizeof("--list-options")) == 0 ||
413 strncmp(argv
[i
], "--list-commands", sizeof("--list-commands")) == 0 ||
414 strncmp(argv
[i
], "version", sizeof("version")) == 0 ||
415 strncmp(argv
[i
], "view", sizeof("view")) == 0) {
424 * Parse command line arguments.
426 * Return 0 if OK, else -1
428 static int parse_args(int argc
, char **argv
)
434 clean_exit(EXIT_FAILURE
);
437 while ((opt
= getopt_long(argc
, argv
, "+Vhnvqg:", long_options
, NULL
)) != -1) {
448 lttng_opt_verbose
+= 1;
454 lttng_set_tracing_group(optarg
);
459 case OPT_SESSION_PATH
:
460 opt_sessiond_path
= strdup(optarg
);
462 case OPT_DUMP_OPTIONS
:
463 list_options(stdout
);
466 case OPT_DUMP_COMMANDS
:
467 list_commands(stdout
);
477 /* If both options are specified, quiet wins */
478 if (lttng_opt_verbose
&& lttng_opt_quiet
) {
479 lttng_opt_verbose
= 0;
482 /* Spawn session daemon if needed */
483 if (opt_no_sessiond
== 0 && check_args_no_sessiond(argc
, argv
) == 0 &&
484 (check_sessiond() < 0)) {
489 /* No leftovers, print usage and quit */
490 if ((argc
- optind
) == 0) {
497 * Handle leftovers which is a first level command with the trailing
500 ret
= handle_command(argc
- optind
, argv
+ optind
);
503 WARN("Some command(s) went wrong");
506 ERR("Command error");
509 ERR("Undefined command");
514 case CMD_UNSUPPORTED
:
515 ERR("Unsupported command");
539 int main(int argc
, char *argv
[])
544 progname
= argv
[0] ? argv
[0] : "lttng";
546 /* For Mathieu Desnoyers a.k.a. Dr. Tracing */
547 user
= getenv("USER");
548 if (user
!= NULL
&& ((strncmp(progname
, "drtrace", 7) == 0 ||
549 strncmp("compudj", user
, 7) == 0))) {
550 MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n", 27,1,33,27,0);
554 ret
= set_signal_handler();
559 ret
= parse_args(argc
, argv
);