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 {"help", 0, NULL
, 'h'},
51 {"group", 1, NULL
, 'g'},
52 {"verbose", 0, NULL
, 'v'},
53 {"quiet", 0, NULL
, 'q'},
54 {"no-sessiond", 0, NULL
, 'n'},
55 {"sessiond-path", 1, NULL
, OPT_SESSION_PATH
},
56 {"list-options", 0, NULL
, OPT_DUMP_OPTIONS
},
57 {"list-commands", 0, NULL
, OPT_DUMP_COMMANDS
},
61 /* First level command */
62 static struct cmd_struct commands
[] = {
64 { "create", cmd_create
},
65 { "destroy", cmd_destroy
},
66 { "start", cmd_start
},
68 { "enable-event", cmd_enable_events
},
69 { "disable-event", cmd_disable_events
},
70 { "enable-channel", cmd_enable_channels
},
71 { "disable-channel", cmd_disable_channels
},
72 { "add-context", cmd_add_context
},
73 { "set-session", cmd_set_session
},
74 { "version", cmd_version
},
75 { "calibrate", cmd_calibrate
},
77 { "enable-consumer", cmd_enable_consumer
},
78 { "disable-consumer", cmd_disable_consumer
},
79 { NULL
, NULL
} /* Array closure */
82 static void usage(FILE *ofp
)
84 fprintf(ofp
, "LTTng Trace Control " VERSION
" - " VERSION_NAME
"\n\n");
85 fprintf(ofp
, "usage: lttng [OPTIONS] <COMMAND> [<ARGS>]\n");
87 fprintf(ofp
, "Options:\n");
88 fprintf(ofp
, " -h, --help Show this help\n");
89 fprintf(ofp
, " --list-options Simple listing of lttng options\n");
90 fprintf(ofp
, " --list-commands Simple listing of lttng commands\n");
91 fprintf(ofp
, " -v, --verbose Increase verbosity\n");
92 fprintf(ofp
, " -q, --quiet Quiet mode\n");
93 fprintf(ofp
, " -g, --group NAME Unix tracing group name. (default: tracing)\n");
94 fprintf(ofp
, " -n, --no-sessiond Don't spawn a session daemon\n");
95 fprintf(ofp
, " --sessiond-path PATH Session daemon full path\n");
97 fprintf(ofp
, "Commands:\n");
98 fprintf(ofp
, " add-context Add context to event and/or channel\n");
99 fprintf(ofp
, " calibrate Quantify LTTng overhead\n");
100 fprintf(ofp
, " create Create tracing session\n");
101 fprintf(ofp
, " destroy Tear down tracing session\n");
102 fprintf(ofp
, " enable-channel Enable tracing channel\n");
103 fprintf(ofp
, " enable-event Enable tracing event\n");
104 fprintf(ofp
, " disable-channel Disable tracing channel\n");
105 fprintf(ofp
, " disable-event Disable tracing event\n");
106 fprintf(ofp
, " enable-consumer Enable local or streaming consumer\n");
107 fprintf(ofp
, " disable-consumer Disable consumer\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");
124 * List options line by line. This is mostly for bash auto completion and to
125 * avoid difficult parsing.
127 static void list_options(FILE *ofp
)
130 struct option
*option
= NULL
;
132 option
= &long_options
[i
];
133 while (option
->name
!= NULL
) {
134 fprintf(ofp
, "--%s\n", option
->name
);
136 if (isprint(option
->val
)) {
137 fprintf(ofp
, "-%c\n", option
->val
);
141 option
= &long_options
[i
];
148 * List commands line by line. This is mostly for bash auto completion and to
149 * avoid difficult parsing.
151 static void list_commands(FILE *ofp
)
154 struct cmd_struct
*cmd
= NULL
;
157 while (cmd
->name
!= NULL
) {
158 fprintf(ofp
, "%s\n", cmd
->name
);
167 static void clean_exit(int code
)
176 * Signal handler for the daemon
178 static void sighandler(int sig
)
184 DBG("SIGTERM caught");
185 clean_exit(EXIT_FAILURE
);
188 DBG("SIGCHLD caught");
189 waitpid(sessiond_pid
, &status
, 0);
190 recv_child_signal
= 1;
191 /* Indicate that the session daemon died */
193 ERR("Session daemon died (exit status %d)", WEXITSTATUS(status
));
197 recv_child_signal
= 1;
198 DBG("SIGUSR1 caught");
201 DBG("Unknown signal %d caught", sig
);
211 * Setup signal handler for SIGCHLD and SIGTERM.
213 static int set_signal_handler(void)
219 if ((ret
= sigemptyset(&sigset
)) < 0) {
220 perror("sigemptyset");
224 sa
.sa_handler
= sighandler
;
227 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
232 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
237 if ((ret
= sigaction(SIGCHLD
, &sa
, NULL
)) < 0) {
249 * Handle the full argv list of a first level command. Will find the command
250 * in the global commands array and call the function callback associated.
252 * If command not found, return -1
253 * else, return function command error code.
255 static int handle_command(int argc
, char **argv
)
258 struct cmd_struct
*cmd
;
266 while (cmd
->func
!= NULL
) {
268 if (strcmp(argv
[0], cmd
->name
) == 0) {
269 ret
= cmd
->func(argc
, (const char**) argv
);
276 /* Command not found */
286 * Spawn a session daemon by forking and execv.
288 static int spawn_sessiond(char *pathname
)
293 MSG("Spawning a session daemon");
294 recv_child_signal
= 0;
298 * Spawn session daemon and tell
299 * it to signal us when ready.
301 execlp(pathname
, "lttng-sessiond", "--sig-parent", "--quiet", NULL
);
302 /* execlp only returns if error happened */
303 if (errno
== ENOENT
) {
304 ERR("No session daemon found. Use --sessiond-path.");
308 kill(getppid(), SIGTERM
); /* wake parent */
310 } else if (pid
> 0) {
313 * Wait for lttng-sessiond to start. We need to use a flag to check if
314 * the signal has been sent to us, because the child can be scheduled
315 * before the parent, and thus send the signal before this check. In
316 * the signal handler, we set the recv_child_signal flag, so anytime we
317 * check it after the fork is fine. Note that sleep() is interrupted
318 * before the 1 second delay as soon as the signal is received, so it
319 * will not cause visible delay for the user.
321 while (!recv_child_signal
) {
325 * The signal handler will nullify sessiond_pid on SIGCHLD
344 * Check if the session daemon is available using
345 * the liblttngctl API for the check. If not, try to
348 static int check_sessiond(void)
351 char *pathname
= NULL
, *alloc_pathname
= NULL
;
353 ret
= lttng_session_daemon_alive();
354 if (ret
== 0) { /* not alive */
355 /* Try command line option path */
356 if (opt_sessiond_path
!= NULL
) {
357 ret
= access(opt_sessiond_path
, F_OK
| X_OK
);
359 ERR("No such file or access denied: %s", opt_sessiond_path
);
362 pathname
= opt_sessiond_path
;
364 /* Try LTTNG_SESSIOND_PATH env variable */
365 pathname
= getenv(DEFAULT_SESSIOND_PATH_ENV
);
368 /* Let's rock and roll */
369 if (pathname
== NULL
) {
370 ret
= asprintf(&alloc_pathname
, INSTALL_BIN_PATH
"/lttng-sessiond");
372 perror("asprintf spawn sessiond");
375 pathname
= alloc_pathname
;
378 ret
= spawn_sessiond(pathname
);
379 free(alloc_pathname
);
381 ERR("Problem occurred when starting %s", pathname
);
391 * Check args for specific options that *must* not trigger a session daemon
394 * Return 1 if match else 0.
396 static int check_args_no_sessiond(int argc
, char **argv
)
400 for (i
= 0; i
< argc
; i
++) {
401 if ((strncmp(argv
[i
], "-h", sizeof("-h")) == 0) ||
402 strncmp(argv
[i
], "--h", sizeof("--h")) == 0 ||
403 strncmp(argv
[i
], "--list-options", sizeof("--list-options")) == 0 ||
404 strncmp(argv
[i
], "--list-commands", sizeof("--list-commands")) == 0 ||
405 strncmp(argv
[i
], "version", sizeof("version")) == 0 ||
406 strncmp(argv
[i
], "view", sizeof("view")) == 0) {
415 * Parse command line arguments.
417 * Return 0 if OK, else -1
419 static int parse_args(int argc
, char **argv
)
425 clean_exit(EXIT_FAILURE
);
428 while ((opt
= getopt_long(argc
, argv
, "+hnvqg:", long_options
, NULL
)) != -1) {
435 lttng_opt_verbose
+= 1;
441 lttng_set_tracing_group(optarg
);
446 case OPT_SESSION_PATH
:
447 opt_sessiond_path
= strdup(optarg
);
449 case OPT_DUMP_OPTIONS
:
450 list_options(stdout
);
453 case OPT_DUMP_COMMANDS
:
454 list_commands(stdout
);
464 /* If both options are specified, quiet wins */
465 if (lttng_opt_verbose
&& lttng_opt_quiet
) {
466 lttng_opt_verbose
= 0;
469 /* Spawn session daemon if needed */
470 if (opt_no_sessiond
== 0 && check_args_no_sessiond(argc
, argv
) == 0 &&
471 (check_sessiond() < 0)) {
476 /* No leftovers, print usage and quit */
477 if ((argc
- optind
) == 0) {
484 * Handle leftovers which is a first level command with the trailing
487 ret
= handle_command(argc
- optind
, argv
+ optind
);
490 WARN("Some command(s) went wrong");
493 ERR("Command error");
496 ERR("Undefined command");
523 int main(int argc
, char *argv
[])
528 progname
= argv
[0] ? argv
[0] : "lttng";
530 /* For Mathieu Desnoyers a.k.a. Dr. Tracing */
531 user
= getenv("USER");
532 if (user
!= NULL
&& ((strncmp(progname
, "drtrace", 7) == 0 ||
533 strncmp("compudj", user
, 7) == 0))) {
534 MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n", 27,1,33,27,0);
538 ret
= set_signal_handler();
543 ret
= parse_args(argc
, argv
);