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.
25 #include <sys/types.h>
31 #include <lttng/lttng.h>
32 #include <common/error.h>
37 static char *progname
;
38 static int opt_no_sessiond
;
39 static char *opt_sessiond_path
;
40 static pid_t sessiond_pid
;
41 static volatile int recv_child_signal
;
43 char *opt_relayd_path
;
52 /* Getopt options. No first level command. */
53 static struct option long_options
[] = {
54 {"version", 0, NULL
, 'V'},
55 {"help", 0, NULL
, 'h'},
56 {"group", 1, NULL
, 'g'},
57 {"verbose", 0, NULL
, 'v'},
58 {"quiet", 0, NULL
, 'q'},
60 {"no-sessiond", 0, NULL
, 'n'},
61 {"sessiond-path", 1, NULL
, OPT_SESSION_PATH
},
62 {"relayd-path", 1, NULL
, OPT_RELAYD_PATH
},
63 {"list-options", 0, NULL
, OPT_DUMP_OPTIONS
},
64 {"list-commands", 0, NULL
, OPT_DUMP_COMMANDS
},
68 /* First level command */
69 static struct cmd_struct commands
[] = {
71 { "create", cmd_create
},
72 { "destroy", cmd_destroy
},
73 { "start", cmd_start
},
75 { "enable-event", cmd_enable_events
},
76 { "disable-event", cmd_disable_events
},
77 { "enable-channel", cmd_enable_channels
},
78 { "disable-channel", cmd_disable_channels
},
79 { "add-context", cmd_add_context
},
80 { "set-session", cmd_set_session
},
81 { "version", cmd_version
},
82 { "calibrate", cmd_calibrate
},
84 { "snapshot", cmd_snapshot
},
87 { NULL
, NULL
} /* Array closure */
90 static void usage(FILE *ofp
)
92 fprintf(ofp
, "LTTng Trace Control " VERSION
" - " VERSION_NAME
"%s\n\n",
93 GIT_VERSION
[0] == '\0' ? "" : " - " GIT_VERSION
);
94 fprintf(ofp
, "usage: lttng [OPTIONS] <COMMAND> [<ARGS>]\n");
96 fprintf(ofp
, "Options:\n");
97 fprintf(ofp
, " -V, --version Show version\n");
98 fprintf(ofp
, " -h, --help Show this help\n");
99 fprintf(ofp
, " --list-options Simple listing of lttng options\n");
100 fprintf(ofp
, " --list-commands Simple listing of lttng commands\n");
101 fprintf(ofp
, " -v, --verbose Increase verbosity\n");
102 fprintf(ofp
, " -q, --quiet Quiet mode\n");
103 fprintf(ofp
, " -m, --mi TYPE Machine Interface mode.\n");
104 fprintf(ofp
, " Type: xml\n");
105 fprintf(ofp
, " -g, --group NAME Unix tracing group name. (default: tracing)\n");
106 fprintf(ofp
, " -n, --no-sessiond Don't spawn a session daemon\n");
107 fprintf(ofp
, " --sessiond-path PATH Session daemon full path\n");
108 fprintf(ofp
, " --relayd-path PATH Relayd daemon full path\n");
110 fprintf(ofp
, "Commands:\n");
111 fprintf(ofp
, " add-context Add context to event and/or channel\n");
112 fprintf(ofp
, " calibrate Quantify LTTng overhead\n");
113 fprintf(ofp
, " create Create tracing session\n");
114 fprintf(ofp
, " destroy Tear down tracing session\n");
115 fprintf(ofp
, " enable-channel Enable tracing channel\n");
116 fprintf(ofp
, " enable-event Enable tracing event\n");
117 fprintf(ofp
, " disable-channel Disable tracing channel\n");
118 fprintf(ofp
, " disable-event Disable tracing event\n");
119 fprintf(ofp
, " list List possible tracing options\n");
120 fprintf(ofp
, " set-session Set current session name\n");
121 fprintf(ofp
, " snapshot Snapshot buffers of current session name\n");
122 fprintf(ofp
, " start Start tracing\n");
123 fprintf(ofp
, " stop Stop tracing\n");
124 fprintf(ofp
, " version Show version information\n");
125 fprintf(ofp
, " view Start trace viewer\n");
126 fprintf(ofp
, " save Save session configuration\n");
127 fprintf(ofp
, " load Load session configuration\n");
129 fprintf(ofp
, "Each command also has its own -h, --help option.\n");
131 fprintf(ofp
, "Please see the lttng(1) man page for full documentation.\n");
132 fprintf(ofp
, "See http://lttng.org for updates, bug reports and news.\n");
135 static void version(FILE *ofp
)
137 fprintf(ofp
, "%s (LTTng Trace Control) " VERSION
" - " VERSION_NAME
"%s\n",
139 GIT_VERSION
[0] == '\0' ? "" : " - " GIT_VERSION
);
143 * Find the MI output type enum from a string. This function is for the support
144 * of machine interface output.
146 static int mi_output_type(const char *output_type
)
150 if (!strncasecmp("xml", output_type
, 3)) {
153 /* Invalid output format */
154 ERR("MI output format not supported");
155 ret
= -LTTNG_ERR_MI_OUTPUT_TYPE
;
164 * List options line by line. This is mostly for bash auto completion and to
165 * avoid difficult parsing.
167 static void list_options(FILE *ofp
)
170 struct option
*option
= NULL
;
172 option
= &long_options
[i
];
173 while (option
->name
!= NULL
) {
174 fprintf(ofp
, "--%s\n", option
->name
);
176 if (isprint(option
->val
)) {
177 fprintf(ofp
, "-%c\n", option
->val
);
181 option
= &long_options
[i
];
188 static void clean_exit(int code
)
197 * Signal handler for the daemon
199 static void sighandler(int sig
)
205 DBG("SIGTERM caught");
206 clean_exit(EXIT_FAILURE
);
209 DBG("SIGCHLD caught");
210 waitpid(sessiond_pid
, &status
, 0);
211 recv_child_signal
= 1;
212 /* Indicate that the session daemon died */
214 ERR("Session daemon died (exit status %d)", WEXITSTATUS(status
));
218 recv_child_signal
= 1;
219 DBG("SIGUSR1 caught");
222 DBG("Unknown signal %d caught", sig
);
232 * Setup signal handler for SIGCHLD and SIGTERM.
234 static int set_signal_handler(void)
240 if ((ret
= sigemptyset(&sigset
)) < 0) {
241 perror("sigemptyset");
245 sa
.sa_handler
= sighandler
;
248 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
253 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
258 if ((ret
= sigaction(SIGCHLD
, &sa
, NULL
)) < 0) {
270 * Handle the full argv list of a first level command. Will find the command
271 * in the global commands array and call the function callback associated.
273 * If command not found, return -1
274 * else, return function command error code.
276 static int handle_command(int argc
, char **argv
)
279 struct cmd_struct
*cmd
;
287 while (cmd
->func
!= NULL
) {
289 if (strcmp(argv
[0], cmd
->name
) == 0) {
290 ret
= cmd
->func(argc
, (const char**) argv
);
297 /* Command not found */
307 * Spawn a session daemon by forking and execv.
309 static int spawn_sessiond(char *pathname
)
314 MSG("Spawning a session daemon");
315 recv_child_signal
= 0;
319 * Spawn session daemon and tell
320 * it to signal us when ready.
322 execlp(pathname
, "lttng-sessiond", "--sig-parent", "--quiet", NULL
);
323 /* execlp only returns if error happened */
324 if (errno
== ENOENT
) {
325 ERR("No session daemon found. Use --sessiond-path.");
329 kill(getppid(), SIGTERM
); /* wake parent */
331 } else if (pid
> 0) {
334 * Wait for lttng-sessiond to start. We need to use a flag to check if
335 * the signal has been sent to us, because the child can be scheduled
336 * before the parent, and thus send the signal before this check. In
337 * the signal handler, we set the recv_child_signal flag, so anytime we
338 * check it after the fork is fine. Note that sleep() is interrupted
339 * before the 1 second delay as soon as the signal is received, so it
340 * will not cause visible delay for the user.
342 while (!recv_child_signal
) {
346 * The signal handler will nullify sessiond_pid on SIGCHLD
365 * Check if the session daemon is available using
366 * the liblttngctl API for the check. If not, try to
369 static int check_sessiond(void)
372 char *pathname
= NULL
;
374 ret
= lttng_session_daemon_alive();
375 if (ret
== 0) { /* not alive */
376 /* Try command line option path */
377 pathname
= opt_sessiond_path
;
379 /* Try LTTNG_SESSIOND_PATH env variable */
380 if (pathname
== NULL
) {
381 pathname
= getenv(DEFAULT_SESSIOND_PATH_ENV
);
384 /* Try with configured path */
385 if (pathname
== NULL
) {
386 if (CONFIG_SESSIOND_BIN
[0] != '\0') {
387 pathname
= CONFIG_SESSIOND_BIN
;
391 /* Let's rock and roll while trying the default path */
392 if (pathname
== NULL
) {
393 pathname
= INSTALL_BIN_PATH
"/lttng-sessiond";
396 DBG("Session daemon at: %s", pathname
);
398 /* Check existence and permissions */
399 ret
= access(pathname
, F_OK
| X_OK
);
401 ERR("No such file or access denied: %s", pathname
);
405 ret
= spawn_sessiond(pathname
);
407 ERR("Problem occurred when starting %s", pathname
);
415 * Check args for specific options that *must* not trigger a session daemon
418 * Return 1 if match else 0.
420 static int check_args_no_sessiond(int argc
, char **argv
)
424 for (i
= 0; i
< argc
; i
++) {
425 if ((strncmp(argv
[i
], "-h", sizeof("-h")) == 0) ||
426 strncmp(argv
[i
], "--h", sizeof("--h")) == 0 ||
427 strncmp(argv
[i
], "--list-options", sizeof("--list-options")) == 0 ||
428 strncmp(argv
[i
], "--list-commands", sizeof("--list-commands")) == 0 ||
429 strncmp(argv
[i
], "version", sizeof("version")) == 0 ||
430 strncmp(argv
[i
], "view", sizeof("view")) == 0) {
439 * Parse command line arguments.
441 * Return 0 if OK, else -1
443 static int parse_args(int argc
, char **argv
)
450 clean_exit(EXIT_FAILURE
);
453 while ((opt
= getopt_long(argc
, argv
, "+Vhnvqg:m:", long_options
, NULL
)) != -1) {
464 /* There is only 3 possible level of verbosity. (-vvv) */
465 if (lttng_opt_verbose
< 3) {
466 lttng_opt_verbose
+= 1;
473 lttng_opt_mi
= mi_output_type(optarg
);
474 if (lttng_opt_mi
< 0) {
480 lttng_set_tracing_group(optarg
);
485 case OPT_SESSION_PATH
:
486 opt_sessiond_path
= strdup(optarg
);
488 case OPT_RELAYD_PATH
:
489 opt_relayd_path
= strdup(optarg
);
491 case OPT_DUMP_OPTIONS
:
492 list_options(stdout
);
495 case OPT_DUMP_COMMANDS
:
496 list_commands(commands
, stdout
);
506 /* If both options are specified, quiet wins */
507 if (lttng_opt_verbose
&& lttng_opt_quiet
) {
508 lttng_opt_verbose
= 0;
511 /* Spawn session daemon if needed */
512 if (opt_no_sessiond
== 0 && check_args_no_sessiond(argc
, argv
) == 0 &&
513 (check_sessiond() < 0)) {
518 /* No leftovers, print usage and quit */
519 if ((argc
- optind
) == 0) {
525 /* For Mathieu Desnoyers a.k.a. Dr. Tracing */
526 user
= getenv("USER");
527 if (user
!= NULL
&& ((strncmp(progname
, "drtrace", 7) == 0 ||
528 strncmp("compudj", user
, 7) == 0))) {
529 MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n", 27,1,33,27,0);
534 * Handle leftovers which is a first level command with the trailing
537 ret
= handle_command(argc
- optind
, argv
+ optind
);
540 WARN("Some command(s) went wrong");
543 ERR("Command error");
546 ERR("Undefined command");
551 case CMD_UNSUPPORTED
:
552 ERR("Unsupported command");
576 int main(int argc
, char *argv
[])
580 progname
= argv
[0] ? argv
[0] : "lttng";
582 ret
= set_signal_handler();
587 ret
= parse_args(argc
, argv
);