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 { "snapshot", cmd_snapshot
},
79 { "enable-consumer", cmd_enable_consumer
}, /* OBSOLETE */
80 { "disable-consumer", cmd_disable_consumer
}, /* OBSOLETE */
81 { NULL
, NULL
} /* Array closure */
84 static void usage(FILE *ofp
)
86 fprintf(ofp
, "LTTng Trace Control " VERSION
" - " VERSION_NAME
"\n\n");
87 fprintf(ofp
, "usage: lttng [OPTIONS] <COMMAND> [<ARGS>]\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");
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");
117 fprintf(ofp
, "Each command also has its own -h, --help option.\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");
123 static void version(FILE *ofp
)
125 fprintf(ofp
, "%s (LTTng Trace Control) " VERSION
" - " VERSION_NAME
"\n",
132 * List options line by line. This is mostly for bash auto completion and to
133 * avoid difficult parsing.
135 static void list_options(FILE *ofp
)
138 struct option
*option
= NULL
;
140 option
= &long_options
[i
];
141 while (option
->name
!= NULL
) {
142 fprintf(ofp
, "--%s\n", option
->name
);
144 if (isprint(option
->val
)) {
145 fprintf(ofp
, "-%c\n", option
->val
);
149 option
= &long_options
[i
];
156 static void clean_exit(int code
)
165 * Signal handler for the daemon
167 static void sighandler(int sig
)
173 DBG("SIGTERM caught");
174 clean_exit(EXIT_FAILURE
);
177 DBG("SIGCHLD caught");
178 waitpid(sessiond_pid
, &status
, 0);
179 recv_child_signal
= 1;
180 /* Indicate that the session daemon died */
182 ERR("Session daemon died (exit status %d)", WEXITSTATUS(status
));
186 recv_child_signal
= 1;
187 DBG("SIGUSR1 caught");
190 DBG("Unknown signal %d caught", sig
);
200 * Setup signal handler for SIGCHLD and SIGTERM.
202 static int set_signal_handler(void)
208 if ((ret
= sigemptyset(&sigset
)) < 0) {
209 perror("sigemptyset");
213 sa
.sa_handler
= sighandler
;
216 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
221 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
226 if ((ret
= sigaction(SIGCHLD
, &sa
, NULL
)) < 0) {
238 * Handle the full argv list of a first level command. Will find the command
239 * in the global commands array and call the function callback associated.
241 * If command not found, return -1
242 * else, return function command error code.
244 static int handle_command(int argc
, char **argv
)
247 struct cmd_struct
*cmd
;
255 while (cmd
->func
!= NULL
) {
257 if (strcmp(argv
[0], cmd
->name
) == 0) {
258 ret
= cmd
->func(argc
, (const char**) argv
);
265 /* Command not found */
275 * Spawn a session daemon by forking and execv.
277 static int spawn_sessiond(char *pathname
)
282 MSG("Spawning a session daemon");
283 recv_child_signal
= 0;
287 * Spawn session daemon and tell
288 * it to signal us when ready.
290 execlp(pathname
, "lttng-sessiond", "--sig-parent", "--quiet", NULL
);
291 /* execlp only returns if error happened */
292 if (errno
== ENOENT
) {
293 ERR("No session daemon found. Use --sessiond-path.");
297 kill(getppid(), SIGTERM
); /* wake parent */
299 } else if (pid
> 0) {
302 * Wait for lttng-sessiond to start. We need to use a flag to check if
303 * the signal has been sent to us, because the child can be scheduled
304 * before the parent, and thus send the signal before this check. In
305 * the signal handler, we set the recv_child_signal flag, so anytime we
306 * check it after the fork is fine. Note that sleep() is interrupted
307 * before the 1 second delay as soon as the signal is received, so it
308 * will not cause visible delay for the user.
310 while (!recv_child_signal
) {
314 * The signal handler will nullify sessiond_pid on SIGCHLD
333 * Check if the session daemon is available using
334 * the liblttngctl API for the check. If not, try to
337 static int check_sessiond(void)
340 char *pathname
= NULL
;
342 ret
= lttng_session_daemon_alive();
343 if (ret
== 0) { /* not alive */
344 /* Try command line option path */
345 pathname
= opt_sessiond_path
;
347 /* Try LTTNG_SESSIOND_PATH env variable */
348 if (pathname
== NULL
) {
349 pathname
= getenv(DEFAULT_SESSIOND_PATH_ENV
);
352 /* Try with configured path */
353 if (pathname
== NULL
) {
354 if (CONFIG_SESSIOND_BIN
[0] != '\0') {
355 pathname
= CONFIG_SESSIOND_BIN
;
359 /* Let's rock and roll while trying the default path */
360 if (pathname
== NULL
) {
361 pathname
= INSTALL_BIN_PATH
"/lttng-sessiond";
364 DBG("Session daemon at: %s", pathname
);
366 /* Check existence and permissions */
367 ret
= access(pathname
, F_OK
| X_OK
);
369 ERR("No such file or access denied: %s", pathname
);
373 ret
= spawn_sessiond(pathname
);
375 ERR("Problem occurred when starting %s", pathname
);
383 * Check args for specific options that *must* not trigger a session daemon
386 * Return 1 if match else 0.
388 static int check_args_no_sessiond(int argc
, char **argv
)
392 for (i
= 0; i
< argc
; i
++) {
393 if ((strncmp(argv
[i
], "-h", sizeof("-h")) == 0) ||
394 strncmp(argv
[i
], "--h", sizeof("--h")) == 0 ||
395 strncmp(argv
[i
], "--list-options", sizeof("--list-options")) == 0 ||
396 strncmp(argv
[i
], "--list-commands", sizeof("--list-commands")) == 0 ||
397 strncmp(argv
[i
], "version", sizeof("version")) == 0 ||
398 strncmp(argv
[i
], "view", sizeof("view")) == 0) {
407 * Parse command line arguments.
409 * Return 0 if OK, else -1
411 static int parse_args(int argc
, char **argv
)
417 clean_exit(EXIT_FAILURE
);
420 while ((opt
= getopt_long(argc
, argv
, "+Vhnvqg:", long_options
, NULL
)) != -1) {
431 lttng_opt_verbose
+= 1;
437 lttng_set_tracing_group(optarg
);
442 case OPT_SESSION_PATH
:
443 opt_sessiond_path
= strdup(optarg
);
445 case OPT_DUMP_OPTIONS
:
446 list_options(stdout
);
449 case OPT_DUMP_COMMANDS
:
450 list_commands(commands
, stdout
);
460 /* If both options are specified, quiet wins */
461 if (lttng_opt_verbose
&& lttng_opt_quiet
) {
462 lttng_opt_verbose
= 0;
465 /* Spawn session daemon if needed */
466 if (opt_no_sessiond
== 0 && check_args_no_sessiond(argc
, argv
) == 0 &&
467 (check_sessiond() < 0)) {
472 /* No leftovers, print usage and quit */
473 if ((argc
- optind
) == 0) {
480 * Handle leftovers which is a first level command with the trailing
483 ret
= handle_command(argc
- optind
, argv
+ optind
);
486 WARN("Some command(s) went wrong");
489 ERR("Command error");
492 ERR("Undefined command");
497 case CMD_UNSUPPORTED
:
498 ERR("Unsupported command");
522 int main(int argc
, char *argv
[])
527 progname
= argv
[0] ? argv
[0] : "lttng";
529 /* For Mathieu Desnoyers a.k.a. Dr. Tracing */
530 user
= getenv("USER");
531 if (user
!= NULL
&& ((strncmp(progname
, "drtrace", 7) == 0 ||
532 strncmp("compudj", user
, 7) == 0))) {
533 MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n", 27,1,33,27,0);
537 ret
= set_signal_handler();
542 ret
= parse_args(argc
, argv
);