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
;
42 char *opt_relayd_path
;
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'},
59 {"no-sessiond", 0, NULL
, 'n'},
60 {"sessiond-path", 1, NULL
, OPT_SESSION_PATH
},
61 {"relayd-path", 1, NULL
, OPT_RELAYD_PATH
},
62 {"list-options", 0, NULL
, OPT_DUMP_OPTIONS
},
63 {"list-commands", 0, NULL
, OPT_DUMP_COMMANDS
},
67 /* First level command */
68 static struct cmd_struct commands
[] = {
70 { "create", cmd_create
},
71 { "destroy", cmd_destroy
},
72 { "start", cmd_start
},
74 { "enable-event", cmd_enable_events
},
75 { "disable-event", cmd_disable_events
},
76 { "enable-channel", cmd_enable_channels
},
77 { "disable-channel", cmd_disable_channels
},
78 { "add-context", cmd_add_context
},
79 { "set-session", cmd_set_session
},
80 { "version", cmd_version
},
81 { "calibrate", cmd_calibrate
},
83 { "snapshot", cmd_snapshot
},
86 { NULL
, NULL
} /* Array closure */
89 static void usage(FILE *ofp
)
91 fprintf(ofp
, "LTTng Trace Control " VERSION
" - " VERSION_NAME
"%s\n\n",
92 GIT_VERSION
[0] == '\0' ? "" : " - " GIT_VERSION
);
93 fprintf(ofp
, "usage: lttng [OPTIONS] <COMMAND> [<ARGS>]\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
, " -m, --mi TYPE Machine Interface mode.\n");
103 fprintf(ofp
, " Type: xml\n");
104 fprintf(ofp
, " -g, --group NAME Unix tracing group name. (default: tracing)\n");
105 fprintf(ofp
, " -n, --no-sessiond Don't spawn a session daemon\n");
106 fprintf(ofp
, " --sessiond-path PATH Session daemon full path\n");
107 fprintf(ofp
, " --relayd-path PATH Relayd daemon full path\n");
109 fprintf(ofp
, "Commands:\n");
110 fprintf(ofp
, " add-context Add context to event and/or channel\n");
111 fprintf(ofp
, " calibrate Quantify LTTng overhead\n");
112 fprintf(ofp
, " create Create tracing session\n");
113 fprintf(ofp
, " destroy Tear down tracing session\n");
114 fprintf(ofp
, " enable-channel Enable tracing channel\n");
115 fprintf(ofp
, " enable-event Enable tracing event\n");
116 fprintf(ofp
, " disable-channel Disable tracing channel\n");
117 fprintf(ofp
, " disable-event Disable tracing event\n");
118 fprintf(ofp
, " list List possible tracing options\n");
119 fprintf(ofp
, " set-session Set current session name\n");
120 fprintf(ofp
, " snapshot Snapshot buffers of current session name\n");
121 fprintf(ofp
, " start Start tracing\n");
122 fprintf(ofp
, " stop Stop tracing\n");
123 fprintf(ofp
, " version Show version information\n");
124 fprintf(ofp
, " view Start trace viewer\n");
125 fprintf(ofp
, " save Save session configuration\n");
126 fprintf(ofp
, " load Load session configuration\n");
128 fprintf(ofp
, "Each command also has its own -h, --help option.\n");
130 fprintf(ofp
, "Please see the lttng(1) man page for full documentation.\n");
131 fprintf(ofp
, "See http://lttng.org for updates, bug reports and news.\n");
134 static void version(FILE *ofp
)
136 fprintf(ofp
, "%s (LTTng Trace Control) " VERSION
" - " VERSION_NAME
"%s\n",
138 GIT_VERSION
[0] == '\0' ? "" : " - " GIT_VERSION
);
142 * Find the MI output type enum from a string. This function is for the support
143 * of machine interface output.
145 static int mi_output_type(const char *output_type
)
149 if (!strncasecmp("xml", output_type
, 3)) {
152 /* Invalid output format */
153 ERR("MI output format not supported");
154 ret
= -LTTNG_ERR_MI_OUTPUT_TYPE
;
163 * List options line by line. This is mostly for bash auto completion and to
164 * avoid difficult parsing.
166 static void list_options(FILE *ofp
)
169 struct option
*option
= NULL
;
171 option
= &long_options
[i
];
172 while (option
->name
!= NULL
) {
173 fprintf(ofp
, "--%s\n", option
->name
);
175 if (isprint(option
->val
)) {
176 fprintf(ofp
, "-%c\n", option
->val
);
180 option
= &long_options
[i
];
187 static void clean_exit(int code
)
196 * Signal handler for the daemon
198 static void sighandler(int sig
)
204 DBG("SIGTERM caught");
205 clean_exit(EXIT_FAILURE
);
208 DBG("SIGCHLD caught");
209 waitpid(sessiond_pid
, &status
, 0);
210 recv_child_signal
= 1;
211 /* Indicate that the session daemon died */
213 ERR("Session daemon died (exit status %d)", WEXITSTATUS(status
));
217 recv_child_signal
= 1;
218 DBG("SIGUSR1 caught");
221 DBG("Unknown signal %d caught", sig
);
231 * Setup signal handler for SIGCHLD and SIGTERM.
233 static int set_signal_handler(void)
239 if ((ret
= sigemptyset(&sigset
)) < 0) {
240 perror("sigemptyset");
244 sa
.sa_handler
= sighandler
;
247 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
252 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
257 if ((ret
= sigaction(SIGCHLD
, &sa
, NULL
)) < 0) {
269 * Handle the full argv list of a first level command. Will find the command
270 * in the global commands array and call the function callback associated.
272 * If command not found, return -1
273 * else, return function command error code.
275 static int handle_command(int argc
, char **argv
)
278 struct cmd_struct
*cmd
;
286 while (cmd
->func
!= NULL
) {
288 if (strcmp(argv
[0], cmd
->name
) == 0) {
289 ret
= cmd
->func(argc
, (const char**) argv
);
296 /* Command not found */
306 * Spawn a session daemon by forking and execv.
308 static int spawn_sessiond(char *pathname
)
313 MSG("Spawning a session daemon");
314 recv_child_signal
= 0;
318 * Spawn session daemon and tell
319 * it to signal us when ready.
321 execlp(pathname
, "lttng-sessiond", "--sig-parent", "--quiet", NULL
);
322 /* execlp only returns if error happened */
323 if (errno
== ENOENT
) {
324 ERR("No session daemon found. Use --sessiond-path.");
328 kill(getppid(), SIGTERM
); /* wake parent */
330 } else if (pid
> 0) {
333 * Wait for lttng-sessiond to start. We need to use a flag to check if
334 * the signal has been sent to us, because the child can be scheduled
335 * before the parent, and thus send the signal before this check. In
336 * the signal handler, we set the recv_child_signal flag, so anytime we
337 * check it after the fork is fine. Note that sleep() is interrupted
338 * before the 1 second delay as soon as the signal is received, so it
339 * will not cause visible delay for the user.
341 while (!recv_child_signal
) {
345 * The signal handler will nullify sessiond_pid on SIGCHLD
364 * Check if the session daemon is available using
365 * the liblttngctl API for the check. If not, try to
368 static int check_sessiond(void)
371 char *pathname
= NULL
;
373 ret
= lttng_session_daemon_alive();
374 if (ret
== 0) { /* not alive */
375 /* Try command line option path */
376 pathname
= opt_sessiond_path
;
378 /* Try LTTNG_SESSIOND_PATH env variable */
379 if (pathname
== NULL
) {
380 pathname
= getenv(DEFAULT_SESSIOND_PATH_ENV
);
383 /* Try with configured path */
384 if (pathname
== NULL
) {
385 if (CONFIG_SESSIOND_BIN
[0] != '\0') {
386 pathname
= CONFIG_SESSIOND_BIN
;
390 /* Let's rock and roll while trying the default path */
391 if (pathname
== NULL
) {
392 pathname
= INSTALL_BIN_PATH
"/lttng-sessiond";
395 DBG("Session daemon at: %s", pathname
);
397 /* Check existence and permissions */
398 ret
= access(pathname
, F_OK
| X_OK
);
400 ERR("No such file or access denied: %s", pathname
);
404 ret
= spawn_sessiond(pathname
);
406 ERR("Problem occurred when starting %s", pathname
);
414 * Check args for specific options that *must* not trigger a session daemon
417 * Return 1 if match else 0.
419 static int check_args_no_sessiond(int argc
, char **argv
)
423 for (i
= 0; i
< argc
; i
++) {
424 if ((strncmp(argv
[i
], "-h", sizeof("-h")) == 0) ||
425 strncmp(argv
[i
], "--h", sizeof("--h")) == 0 ||
426 strncmp(argv
[i
], "--list-options", sizeof("--list-options")) == 0 ||
427 strncmp(argv
[i
], "--list-commands", sizeof("--list-commands")) == 0 ||
428 strncmp(argv
[i
], "version", sizeof("version")) == 0 ||
429 strncmp(argv
[i
], "view", sizeof("view")) == 0) {
438 * Parse command line arguments.
440 * Return 0 if OK, else -1
442 static int parse_args(int argc
, char **argv
)
449 clean_exit(EXIT_FAILURE
);
452 while ((opt
= getopt_long(argc
, argv
, "+Vhnvqg:m:", long_options
, NULL
)) != -1) {
463 /* There is only 3 possible level of verbosity. (-vvv) */
464 if (lttng_opt_verbose
< 3) {
465 lttng_opt_verbose
+= 1;
472 lttng_opt_mi
= mi_output_type(optarg
);
473 if (lttng_opt_mi
< 0) {
479 lttng_set_tracing_group(optarg
);
484 case OPT_SESSION_PATH
:
485 opt_sessiond_path
= strdup(optarg
);
487 case OPT_RELAYD_PATH
:
488 opt_relayd_path
= strdup(optarg
);
490 case OPT_DUMP_OPTIONS
:
491 list_options(stdout
);
494 case OPT_DUMP_COMMANDS
:
495 list_commands(commands
, stdout
);
505 /* If both options are specified, quiet wins */
506 if (lttng_opt_verbose
&& lttng_opt_quiet
) {
507 lttng_opt_verbose
= 0;
510 /* Spawn session daemon if needed */
511 if (opt_no_sessiond
== 0 && check_args_no_sessiond(argc
, argv
) == 0 &&
512 (check_sessiond() < 0)) {
517 /* No leftovers, print usage and quit */
518 if ((argc
- optind
) == 0) {
524 /* For Mathieu Desnoyers a.k.a. Dr. Tracing */
525 user
= getenv("USER");
526 if (user
!= NULL
&& ((strncmp(progname
, "drtrace", 7) == 0 ||
527 strncmp("compudj", user
, 7) == 0))) {
528 MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n", 27,1,33,27,0);
533 * Handle leftovers which is a first level command with the trailing
536 ret
= handle_command(argc
- optind
, argv
+ optind
);
539 WARN("Some command(s) went wrong");
542 ERR("Command error");
545 ERR("Undefined command");
550 case CMD_UNSUPPORTED
:
551 ERR("Unsupported command");
575 int main(int argc
, char *argv
[])
579 progname
= argv
[0] ? argv
[0] : "lttng";
581 ret
= set_signal_handler();
586 ret
= parse_args(argc
, argv
);